3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /////////////////////////////////////////////////////////////////////////////////////////////
27 /////////////////////////////////////////////////////////////////////////////////////////////
31 BOOL
DiskResetController(ULONG DriveNumber
)
36 DbgPrint((DPRINT_DISK
, "DiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber
));
38 // BIOS Int 13h, function 0 - Reset disk system
40 // DL = drive (if bit 7 is set both hard disks and floppy disks reset)
43 // CF clear if successful
46 RegsIn
.b
.dl
= DriveNumber
;
48 // Reset the disk controller
49 Int386(0x13, &RegsIn
, &RegsOut
);
51 return INT386_SUCCESS(RegsOut
);
54 BOOL
DiskInt13ExtensionsSupported(ULONG DriveNumber
)
59 DbgPrint((DPRINT_DISK
, "DiskInt13ExtensionsSupported()\n"));
61 // IBM/MS INT 13 Extensions - INSTALLATION CHECK
64 // DL = drive (80h-FFh)
66 // CF set on error (extensions not supported)
67 // AH = 01h (invalid function)
68 // CF clear if successful
69 // BX = AA55h if installed
70 // AH = major version of extensions
72 // 20h = 2.0 / EDD-1.0
73 // 21h = 2.1 / EDD-1.1
76 // CX = API subset support bitmap
77 // DH = extension version (v2.0+ ??? -- not present in 1.x)
79 // Bitfields for IBM/MS INT 13 Extensions API support bitmap
80 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
81 // Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
82 // Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
83 // extended drive parameter table is valid
87 RegsIn
.b
.dl
= DriveNumber
;
89 // Reset the disk controller
90 Int386(0x13, &RegsIn
, &RegsOut
);
92 if (!INT386_SUCCESS(RegsOut
))
94 // CF set on error (extensions not supported)
98 if (RegsOut
.w
.bx
!= 0xAA55)
100 // BX = AA55h if installed
105 // The original check is too strict because some BIOSes report that
106 // extended disk access functions are not suported when booting
107 // from a CD (e.g. Phoenix BIOS v6.00PG). Argh!
109 if (!(RegsOut
.w
.cx
& 0x0001))
111 // CX = API subset support bitmap
112 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
117 // Use this relaxed check instead
118 if (RegsOut
.w
.cx
== 0x0000)
120 // CX = API subset support bitmap
127 VOID
DiskStopFloppyMotor(VOID
)
129 WRITE_PORT_UCHAR((PUCHAR
)0x3F2, 0);
132 BOOL
DiskGetExtendedDriveParameters(ULONG DriveNumber
, PVOID Buffer
, USHORT BufferSize
)
136 PUSHORT Ptr
= (PUSHORT
)(BIOSCALLBUFFER
);
138 DbgPrint((DPRINT_DISK
, "DiskGetExtendedDriveParameters()\n"));
140 // Initialize transfer buffer
143 // BIOS Int 13h, function 48h - Get drive parameters
145 // DL = drive (bit 7 set for hard disk)
146 // DS:SI = result buffer
150 // CF clear if successful
152 // DS:SI -> result buffer
154 RegsIn
.b
.dl
= DriveNumber
;
155 RegsIn
.x
.ds
= BIOSCALLBUFSEGMENT
; // DS:SI -> result buffer
156 RegsIn
.w
.si
= BIOSCALLBUFOFFSET
;
158 // Get drive parameters
159 Int386(0x13, &RegsIn
, &RegsOut
);
161 if (!INT386_SUCCESS(RegsOut
))
166 memcpy(Buffer
, Ptr
, BufferSize
);
171 BOOL
i386DiskGetBootVolume(PULONG DriveNumber
, PULONGLONG StartSector
, PULONGLONG SectorCount
, int *FsType
)
173 PARTITION_TABLE_ENTRY PartitionTableEntry
;
176 DbgPrint((DPRINT_FILESYSTEM
, "FsOpenVolume() DriveNumber: 0x%x PartitionNumber: 0x%x\n", i386BootDrive
, i386BootPartition
));
178 // Check and see if it is a floppy drive
179 // If so then just assume FAT12 file system type
180 if (DiskIsDriveRemovable(i386BootDrive
))
182 DbgPrint((DPRINT_FILESYSTEM
, "Drive is a floppy diskette drive. Assuming FAT12 file system.\n"));
184 *DriveNumber
= i386BootDrive
;
186 *SectorCount
= 2 * 80 * 18; /* FIXME hardcoded for 1.44 Mb */
191 // Check for ISO9660 file system type
192 if (i386BootDrive
>= 0x80 && FsRecIsIso9660(i386BootDrive
))
194 DbgPrint((DPRINT_FILESYSTEM
, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n"));
196 *DriveNumber
= i386BootDrive
;
199 *FsType
= FS_ISO9660
;
203 // Get the requested partition entry
204 if (i386BootPartition
== 0)
206 // Partition requested was zero which means the boot partition
207 if (! DiskGetActivePartitionEntry(i386BootDrive
, &PartitionTableEntry
))
214 // Get requested partition
215 if (! MachDiskGetPartitionEntry(i386BootDrive
, i386BootPartition
, &PartitionTableEntry
))
221 // Check for valid partition
222 if (PartitionTableEntry
.SystemIndicator
== PARTITION_ENTRY_UNUSED
)
227 // Try to recognize the file system
228 if (!FsRecognizeVolume(i386BootDrive
, PartitionTableEntry
.SectorCountBeforePartition
, &VolumeType
))
233 *DriveNumber
= i386BootDrive
;
234 *StartSector
= PartitionTableEntry
.SectorCountBeforePartition
;
235 *SectorCount
= PartitionTableEntry
.PartitionSectorCount
;
237 //switch (PartitionTableEntry.SystemIndicator)
240 case PARTITION_FAT_12
:
241 case PARTITION_FAT_16
:
243 case PARTITION_XINT13
:
244 case PARTITION_FAT32
:
245 case PARTITION_FAT32_XINT13
:
263 i386DiskGetBootDevice(PULONG BootDevice
)
265 ((char *)BootDevice
)[0] = (char)i386BootDrive
;
266 ((char *)BootDevice
)[1] = (char)i386BootPartition
;
270 i386DiskBootingFromFloppy(VOID
)
272 return i386BootDrive
< 0x80;
275 #define IsRecognizedPartition(P) \
276 ((P) == PARTITION_FAT_12 || \
277 (P) == PARTITION_FAT_16 || \
278 (P) == PARTITION_HUGE || \
279 (P) == PARTITION_IFS || \
280 (P) == PARTITION_EXT2 || \
281 (P) == PARTITION_FAT32 || \
282 (P) == PARTITION_FAT32_XINT13 || \
283 (P) == PARTITION_XINT13)
285 BOOL
i386DiskGetSystemVolume(char *SystemPath
,
289 PULONGLONG StartSector
,
290 PULONGLONG SectorCount
,
293 ULONG PartitionNumber
;
294 PARTITION_TABLE_ENTRY PartitionTableEntry
;
297 unsigned i
, RosPartition
;
302 if (!DissectArcPath(SystemPath
, BootPath
, DriveNumber
, &PartitionNumber
))
306 if (NULL
!= RemainingPath
)
308 strcpy(RemainingPath
, BootPath
);
311 /* 0xff -> no partition table present, use whole device */
312 if (0xff == PartitionNumber
)
314 PartitionTableEntry
.SectorCountBeforePartition
= 0;
319 /* recalculate the boot partition for freeldr */
324 if (!MachDiskGetPartitionEntry(*DriveNumber
, ++i
, &PartitionTableEntry
))
328 if (IsRecognizedPartition(PartitionTableEntry
.SystemIndicator
))
330 if (++RosPartition
== PartitionNumber
)
338 /* Check for ISO9660 file system type */
339 if (*DriveNumber
>= 0x80 && FsRecIsIso9660(*DriveNumber
))
341 DbgPrint((DPRINT_FILESYSTEM
, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n"));
345 ((char *)Device
)[0] = (char)(*DriveNumber
);
346 ((char *)Device
)[1] = (char)i
;
350 *FsType
= FS_ISO9660
;
354 if (!FsRecognizeVolume(*DriveNumber
, PartitionTableEntry
.SectorCountBeforePartition
, &VolumeType
))
361 ((char *)Device
)[0] = (char)(*DriveNumber
);
362 ((char *)Device
)[1] = (char)i
;
364 *StartSector
= PartitionTableEntry
.SectorCountBeforePartition
;
365 *SectorCount
= PartitionTableEntry
.PartitionSectorCount
;
369 case PARTITION_FAT_12
:
370 case PARTITION_FAT_16
:
372 case PARTITION_XINT13
:
373 case PARTITION_FAT32
:
374 case PARTITION_FAT32_XINT13
:
392 i386DiskGetBootPath(char *BootPath
, unsigned Size
)
394 static char Path
[] = "multi(0)disk(0)";
397 itoa(i386BootDrive
, Device
, 10);
398 if (Size
<= sizeof(Path
) + 6 + strlen(Device
))
402 strcpy(BootPath
, Path
);
403 strcat(BootPath
, MachDiskBootingFromFloppy() ? "fdisk" : "cdrom");
404 strcat(strcat(strcat(BootPath
, "("), Device
), ")");
409 #endif /* defined __i386__ */