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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // #if defined(__i386__) || defined(_M_AMD64)
26 DBG_DEFAULT_CHANNEL(DISK
);
32 UCHAR PacketSize
; // 00h - Size of packet (10h or 18h)
33 UCHAR Reserved
; // 01h - Reserved (0)
34 USHORT LBABlockCount
; // 02h - Number of blocks to transfer (max 007Fh for Phoenix EDD)
35 USHORT TransferBufferOffset
; // 04h - Transfer buffer offset (seg:off)
36 USHORT TransferBufferSegment
; // Transfer buffer segment (seg:off)
37 ULONGLONG LBAStartBlock
; // 08h - Starting absolute block number
38 // ULONGLONG TransferBuffer64; // 10h - (EDD-3.0, optional) 64-bit flat address of transfer buffer
39 // used if DWORD at 04h is FFFFh:FFFFh
40 // Commented since some earlier BIOSes refuse to work with
41 // such extended structure
42 } I386_DISK_ADDRESS_PACKET
, *PI386_DISK_ADDRESS_PACKET
;
46 UCHAR PacketSize
; // 00h - Size of packet in bytes (13h)
47 UCHAR MediaType
; // 01h - Boot media type (see #00282)
48 UCHAR DriveNumber
; /* 02h - Drive number:
50 * 80h Bootable hard disk
51 * 81h-FFh Nonbootable or no emulation
53 UCHAR Controller
; // 03h - CD-ROM controller number
54 ULONG LBAImage
; // 04h - Logical Block Address of disk image to emulate
55 USHORT DeviceSpec
; /* 08h - Device specification (see also #00282)
57 * Drive is slave instead of master
63 USHORT Buffer
; // 0Ah - Segment of 3K buffer for caching CD-ROM reads
64 USHORT LoadSeg
; // 0Ch - Load segment for initial boot image.
65 // If 0000h, load at segment 07C0h.
66 USHORT SectorCount
; // 0Eh - Number of 512-byte virtual sectors to load
67 // (only valid for AH=4Ch).
68 UCHAR CHSGeometry
[3]; /* 10h - Low byte of cylinder count (for INT 13/AH=08h)
69 * 11h - Sector count, high bits of cylinder count (for INT 13/AH=08h)
70 * 12h - Head count (for INT 13/AH=08h)
73 } I386_CDROM_SPEC_PACKET
, *PI386_CDROM_SPEC_PACKET
;
77 /* FUNCTIONS *****************************************************************/
79 BOOLEAN
DiskResetController(UCHAR DriveNumber
)
83 WARN("DiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber
);
86 * BIOS Int 13h, function 0 - Reset disk system
88 * DL = drive (if bit 7 is set both hard disks and floppy disks reset)
91 * CF clear if successful
95 RegsIn
.b
.dl
= DriveNumber
;
97 /* Reset the disk controller */
98 Int386(0x13, &RegsIn
, &RegsOut
);
100 return INT386_SUCCESS(RegsOut
);
103 static BOOLEAN
PcDiskReadLogicalSectorsLBA(UCHAR DriveNumber
, ULONGLONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
105 REGS RegsIn
, RegsOut
;
107 PI386_DISK_ADDRESS_PACKET Packet
= (PI386_DISK_ADDRESS_PACKET
)(BIOSCALLBUFFER
);
109 TRACE("PcDiskReadLogicalSectorsLBA() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
110 ASSERT(((ULONG_PTR
)Buffer
) <= 0xFFFFF);
112 /* Setup disk address packet */
113 RtlZeroMemory(Packet
, sizeof(*Packet
));
114 Packet
->PacketSize
= sizeof(*Packet
);
115 Packet
->Reserved
= 0;
116 Packet
->LBABlockCount
= (USHORT
)SectorCount
;
117 ASSERT(Packet
->LBABlockCount
== SectorCount
);
118 Packet
->TransferBufferOffset
= ((ULONG_PTR
)Buffer
) & 0x0F;
119 Packet
->TransferBufferSegment
= (USHORT
)(((ULONG_PTR
)Buffer
) >> 4);
120 Packet
->LBAStartBlock
= SectorNumber
;
123 * BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
125 * CF clear if successful
129 * Disk address packet's block count field set to the
130 * number of blocks successfully transferred.
132 RegsIn
.b
.ah
= 0x42; // Subfunction 42h
133 RegsIn
.b
.dl
= DriveNumber
; // Drive number in DL (0 - floppy, 0x80 - harddisk)
134 RegsIn
.x
.ds
= BIOSCALLBUFSEGMENT
; // DS:SI -> disk address packet
135 RegsIn
.w
.si
= BIOSCALLBUFOFFSET
;
138 for (RetryCount
=0; RetryCount
<3; RetryCount
++)
140 Int386(0x13, &RegsIn
, &RegsOut
);
142 /* If it worked return TRUE */
143 if (INT386_SUCCESS(RegsOut
))
147 /* If it was a corrected ECC error then the data is still good */
148 else if (RegsOut
.b
.ah
== 0x11)
152 /* If it failed then do the next retry */
155 DiskResetController(DriveNumber
);
160 /* If we get here then the read failed */
161 ERR("Disk Read Failed in LBA mode: %x (DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d)\n", RegsOut
.b
.ah
, DriveNumber
, SectorNumber
, SectorCount
);
166 static BOOLEAN
PcDiskReadLogicalSectorsCHS(UCHAR DriveNumber
, ULONGLONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
168 UCHAR PhysicalSector
;
171 GEOMETRY DriveGeometry
;
172 ULONG NumberOfSectorsToRead
;
173 REGS RegsIn
, RegsOut
;
176 TRACE("PcDiskReadLogicalSectorsCHS()\n");
178 /* Get the drive geometry */
179 if (!MachDiskGetDriveGeometry(DriveNumber
, &DriveGeometry
) ||
180 DriveGeometry
.Sectors
== 0 ||
181 DriveGeometry
.Heads
== 0)
189 * Calculate the physical disk offsets.
190 * Note: DriveGeometry.Sectors < 64
192 PhysicalSector
= 1 + (UCHAR
)(SectorNumber
% DriveGeometry
.Sectors
);
193 PhysicalHead
= (UCHAR
)((SectorNumber
/ DriveGeometry
.Sectors
) % DriveGeometry
.Heads
);
194 PhysicalTrack
= (ULONG
)((SectorNumber
/ DriveGeometry
.Sectors
) / DriveGeometry
.Heads
);
196 /* Calculate how many sectors we need to read this round */
197 if (PhysicalSector
> 1)
199 if (SectorCount
>= (DriveGeometry
.Sectors
- (PhysicalSector
- 1)))
200 NumberOfSectorsToRead
= (DriveGeometry
.Sectors
- (PhysicalSector
- 1));
202 NumberOfSectorsToRead
= SectorCount
;
206 if (SectorCount
>= DriveGeometry
.Sectors
)
207 NumberOfSectorsToRead
= DriveGeometry
.Sectors
;
209 NumberOfSectorsToRead
= SectorCount
;
212 /* Make sure the read is within the geometry boundaries */
213 if ((PhysicalHead
>= DriveGeometry
.Heads
) ||
214 (PhysicalTrack
>= DriveGeometry
.Cylinders
) ||
215 ((NumberOfSectorsToRead
+ PhysicalSector
) > (DriveGeometry
.Sectors
+ 1)) ||
216 (PhysicalSector
> DriveGeometry
.Sectors
))
218 DiskError("Disk read exceeds drive geometry limits.", 0);
223 * BIOS Int 13h, function 2 - Read Disk Sectors
225 * AL = number of sectors to read (must be nonzero)
226 * CH = low eight bits of cylinder number
227 * CL = sector number 1-63 (bits 0-5)
228 * high two bits of cylinder (bits 6-7, hard disk only)
230 * DL = drive number (bit 7 set for hard disk)
231 * ES:BX -> data buffer
234 * if AH = 11h (corrected ECC error), AL = burst length
235 * CF clear if successful
237 * AL = number of sectors transferred
238 * (only valid if CF set for some BIOSes)
241 RegsIn
.b
.al
= (UCHAR
)NumberOfSectorsToRead
;
242 RegsIn
.b
.ch
= (PhysicalTrack
& 0xFF);
243 RegsIn
.b
.cl
= (UCHAR
)(PhysicalSector
+ ((PhysicalTrack
& 0x300) >> 2));
244 RegsIn
.b
.dh
= PhysicalHead
;
245 RegsIn
.b
.dl
= DriveNumber
;
246 RegsIn
.w
.es
= (USHORT
)(((ULONG_PTR
)Buffer
) >> 4);
247 RegsIn
.w
.bx
= ((ULONG_PTR
)Buffer
) & 0x0F;
249 /* Perform the read. Retry 3 times. */
250 for (RetryCount
=0; RetryCount
<3; RetryCount
++)
252 Int386(0x13, &RegsIn
, &RegsOut
);
254 /* If it worked break out */
255 if (INT386_SUCCESS(RegsOut
))
259 /* If it was a corrected ECC error then the data is still good */
260 else if (RegsOut
.b
.ah
== 0x11)
264 /* If it failed the do the next retry */
267 DiskResetController(DriveNumber
);
272 /* If we retried 3 times then fail */
275 ERR("Disk Read Failed in CHS mode, after retrying 3 times: %x\n", RegsOut
.b
.ah
);
279 // I have learned that not all BIOSes return
280 // the sector read count in the AL register (at least mine doesn't)
281 // even if the sectors were read correctly. So instead
282 // of checking the sector read count we will rely solely
283 // on the carry flag being set on error
285 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ (NumberOfSectorsToRead
* DriveGeometry
.BytesPerSector
));
286 SectorCount
-= NumberOfSectorsToRead
;
287 SectorNumber
+= NumberOfSectorsToRead
;
293 static BOOLEAN
DiskInt13ExtensionsSupported(UCHAR DriveNumber
)
295 static UCHAR LastDriveNumber
= 0xff;
296 static BOOLEAN LastSupported
;
297 REGS RegsIn
, RegsOut
;
299 TRACE("DiskInt13ExtensionsSupported()\n");
301 if (DriveNumber
== LastDriveNumber
)
303 TRACE("Using cached value %s for drive 0x%x\n",
304 LastSupported
? "TRUE" : "FALSE", DriveNumber
);
305 return LastSupported
;
309 * Some BIOSes report that extended disk access functions are not supported
310 * when booting from a CD (e.g. Phoenix BIOS v6.00PG and Insyde BIOS shipping
311 * with Intel Macs). Therefore we just return TRUE if we're booting from a CD -
312 * we can assume that all El Torito capable BIOSes support INT 13 extensions.
313 * We simply detect whether we're booting from CD by checking whether the drive
314 * number is >= 0x8A. It's 0x90 on the Insyde BIOS, and 0x9F on most other BIOSes.
316 if (DriveNumber
>= 0x8A)
318 LastSupported
= TRUE
;
322 LastDriveNumber
= DriveNumber
;
325 * IBM/MS INT 13 Extensions - INSTALLATION CHECK
328 * DL = drive (80h-FFh)
330 * CF set on error (extensions not supported)
331 * AH = 01h (invalid function)
332 * CF clear if successful
333 * BX = AA55h if installed
334 * AH = major version of extensions
336 * 20h = 2.0 / EDD-1.0
337 * 21h = 2.1 / EDD-1.1
340 * CX = API subset support bitmap
341 * DH = extension version (v2.0+ ??? -- not present in 1.x)
343 * Bitfields for IBM/MS INT 13 Extensions API support bitmap
344 * Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
345 * Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
346 * Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
347 * extended drive parameter table is valid
351 RegsIn
.w
.bx
= 0x55AA;
352 RegsIn
.b
.dl
= DriveNumber
;
354 /* Reset the disk controller */
355 Int386(0x13, &RegsIn
, &RegsOut
);
357 if (!INT386_SUCCESS(RegsOut
))
359 /* CF set on error (extensions not supported) */
360 LastSupported
= FALSE
;
364 if (RegsOut
.w
.bx
!= 0xAA55)
366 /* BX = AA55h if installed */
367 LastSupported
= FALSE
;
371 if (!(RegsOut
.w
.cx
& 0x0001))
374 * CX = API subset support bitmap.
375 * Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported.
377 DbgPrint("Suspicious API subset support bitmap 0x%x on device 0x%lx\n",
378 RegsOut
.w
.cx
, DriveNumber
);
379 LastSupported
= FALSE
;
383 LastSupported
= TRUE
;
387 BOOLEAN
PcDiskReadLogicalSectors(UCHAR DriveNumber
, ULONGLONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
389 BOOLEAN ExtensionsSupported
;
391 TRACE("PcDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n",
392 DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
395 * Check to see if it is a fixed disk drive.
396 * If so then check to see if Int13 extensions work.
397 * If they do then use them, otherwise default back to BIOS calls.
399 ExtensionsSupported
= DiskInt13ExtensionsSupported(DriveNumber
);
401 if ((DriveNumber
>= 0x80) && ExtensionsSupported
)
403 TRACE("Using Int 13 Extensions for read. DiskInt13ExtensionsSupported(%d) = %s\n", DriveNumber
, ExtensionsSupported
? "TRUE" : "FALSE");
405 /* LBA is easy, nothing to calculate. Just do the read. */
406 return PcDiskReadLogicalSectorsLBA(DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
410 /* LBA is not supported default to the CHS calls */
411 return PcDiskReadLogicalSectorsCHS(DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
417 VOID
DiskStopFloppyMotor(VOID
)
419 WRITE_PORT_UCHAR((PUCHAR
)0x3F2, 0);
422 BOOLEAN
DiskGetExtendedDriveParameters(UCHAR DriveNumber
, PVOID Buffer
, USHORT BufferSize
)
424 REGS RegsIn
, RegsOut
;
425 PUSHORT Ptr
= (PUSHORT
)(BIOSCALLBUFFER
);
427 TRACE("DiskGetExtendedDriveParameters()\n");
429 if (!DiskInt13ExtensionsSupported(DriveNumber
))
432 /* Initialize transfer buffer */
436 * BIOS Int 13h, function 48h - Get drive parameters
438 * DL = drive (bit 7 set for hard disk)
439 * DS:SI = result buffer
443 * CF clear if successful
445 * DS:SI -> result buffer
448 RegsIn
.b
.dl
= DriveNumber
;
449 RegsIn
.x
.ds
= BIOSCALLBUFSEGMENT
; // DS:SI -> result buffer
450 RegsIn
.w
.si
= BIOSCALLBUFOFFSET
;
452 /* Get drive parameters */
453 Int386(0x13, &RegsIn
, &RegsOut
);
454 if (!INT386_SUCCESS(RegsOut
))
457 memcpy(Buffer
, Ptr
, BufferSize
);
460 TRACE("size of buffer: %x\n", Ptr
[0]);
461 TRACE("information flags: %x\n", Ptr
[1]);
462 TRACE("number of physical cylinders on drive: %u\n", *(PULONG
)&Ptr
[2]);
463 TRACE("number of physical heads on drive: %u\n", *(PULONG
)&Ptr
[4]);
464 TRACE("number of physical sectors per track: %u\n", *(PULONG
)&Ptr
[6]);
465 TRACE("total number of sectors on drive: %I64u\n", *(unsigned long long*)&Ptr
[8]);
466 TRACE("bytes per sector: %u\n", Ptr
[12]);
469 TRACE("EED configuration parameters: %x:%x\n", Ptr
[13], Ptr
[14]);
470 if (Ptr
[13] != 0xffff && Ptr
[14] != 0xffff)
472 PUCHAR SpecPtr
= (PUCHAR
)(ULONG_PTR
)((Ptr
[13] << 4) + Ptr
[14]);
473 TRACE("SpecPtr: %x\n", SpecPtr
);
474 TRACE("physical I/O port base address: %x\n", *(PUSHORT
)&SpecPtr
[0]);
475 TRACE("disk-drive control port address: %x\n", *(PUSHORT
)&SpecPtr
[2]);
476 TRACE("drive flags: %x\n", SpecPtr
[4]);
477 TRACE("proprietary information: %x\n", SpecPtr
[5]);
478 TRACE("IRQ for drive: %u\n", SpecPtr
[6]);
479 TRACE("sector count for multi-sector transfers: %u\n", SpecPtr
[7]);
480 TRACE("DMA control: %x\n", SpecPtr
[8]);
481 TRACE("programmed I/O control: %x\n", SpecPtr
[9]);
482 TRACE("drive options: %x\n", *(PUSHORT
)&SpecPtr
[10]);
487 TRACE("signature: %x\n", Ptr
[15]);
495 PcDiskGetDriveGeometry(UCHAR DriveNumber
, PGEOMETRY Geometry
)
497 EXTENDED_GEOMETRY ExtGeometry
;
498 REGS RegsIn
, RegsOut
;
501 TRACE("DiskGetDriveGeometry()\n");
503 /* Try to get the extended geometry first */
504 ExtGeometry
.Size
= sizeof(ExtGeometry
);
505 if (DiskGetExtendedDriveParameters(DriveNumber
, &ExtGeometry
, ExtGeometry
.Size
))
507 Geometry
->Cylinders
= ExtGeometry
.Cylinders
;
508 Geometry
->Heads
= ExtGeometry
.Heads
;
509 Geometry
->Sectors
= ExtGeometry
.SectorsPerTrack
;
510 Geometry
->BytesPerSector
= ExtGeometry
.BytesPerSector
;
515 * BIOS Int 13h, function 08h - Get drive parameters
517 * DL = drive (bit 7 set for hard disk)
518 * ES:DI = 0000h:0000h to guard against BIOS bugs
522 * CF clear if successful
524 * AL = 00h on at least some BIOSes
525 * BL = drive type (AT/PS2 floppies only)
526 * CH = low eight bits of maximum cylinder number
527 * CL = maximum sector number (bits 5-0)
528 * high two bits of maximum cylinder number (bits 7-6)
529 * DH = maximum head number
530 * DL = number of drives
531 * ES:DI -> drive parameter table (floppies only)
534 RegsIn
.b
.dl
= DriveNumber
;
535 RegsIn
.w
.es
= 0x0000;
536 RegsIn
.w
.di
= 0x0000;
538 /* Get drive parameters */
539 Int386(0x13, &RegsIn
, &RegsOut
);
540 if (!INT386_SUCCESS(RegsOut
))
543 Cylinders
= (RegsOut
.b
.cl
& 0xC0) << 2;
544 Cylinders
+= RegsOut
.b
.ch
;
546 Geometry
->Cylinders
= Cylinders
;
547 Geometry
->Heads
= RegsOut
.b
.dh
+ 1;
548 Geometry
->Sectors
= RegsOut
.b
.cl
& 0x3F;
549 Geometry
->BytesPerSector
= 512; /* Just assume 512 bytes per sector */
555 PcDiskGetCacheableBlockCount(UCHAR DriveNumber
)
559 /* If LBA is supported then the block size will be 64 sectors (32k)
560 * If not then the block size is the size of one track. */
561 if (DiskInt13ExtensionsSupported(DriveNumber
))
565 /* Get the disk geometry. If this fails then we will
566 * just return 1 sector to be safe. */
567 else if (! PcDiskGetDriveGeometry(DriveNumber
, &Geometry
))
573 return Geometry
.Sectors
;
578 PcDiskGetBootPath(OUT PCHAR BootPath
, IN ULONG Size
)
580 // FIXME: Keep it there, or put it in DiskGetBootPath?
581 // Or, abstract the notion of network booting to make
582 // sense for other platforms than the PC (and this idea
583 // already exists), then we would need to check whether
584 // we were booting from network (and: PC --> PXE, etc...)
585 // and if so, set the correct ARC path. But then this new
586 // logic could be moved back to DiskGetBootPath...
590 /* Copy back the buffer */
591 if (Size
< strlen(FrldrBootPath
) + 1)
593 strncpy(BootPath
, FrldrBootPath
, Size
);
597 // FIXME! FIXME! Do this in some drive recognition procedure!!!!
600 strcpy(BootPath
, "net(0)");
603 return DiskGetBootPath(BootPath
, Size
);