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.
27 UCHAR PacketSize
; // 00h - Size of packet (10h or 18h)
28 UCHAR Reserved
; // 01h - Reserved (0)
29 USHORT LBABlockCount
; // 02h - Number of blocks to transfer (max 007Fh for Phoenix EDD)
30 USHORT TransferBufferOffset
; // 04h - Transfer buffer offset (seg:off)
31 USHORT TransferBufferSegment
; // Transfer buffer segment (seg:off)
32 ULONGLONG LBAStartBlock
; // 08h - Starting absolute block number
33 ULONGLONG TransferBuffer64
; // 10h - (EDD-3.0, optional) 64-bit flat address of transfer buffer
34 // used if DWORD at 04h is FFFFh:FFFFh
35 } PACKED I386_DISK_ADDRESS_PACKET
, *PI386_DISK_ADDRESS_PACKET
;
37 /////////////////////////////////////////////////////////////////////////////////////////////
39 /////////////////////////////////////////////////////////////////////////////////////////////
41 static BOOLEAN
PcDiskResetController(ULONG DriveNumber
)
46 DbgPrint((DPRINT_DISK
, "PcDiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber
));
48 // BIOS Int 13h, function 0 - Reset disk system
50 // DL = drive (if bit 7 is set both hard disks and floppy disks reset)
53 // CF clear if successful
56 RegsIn
.b
.dl
= DriveNumber
;
58 // Reset the disk controller
59 Int386(0x13, &RegsIn
, &RegsOut
);
61 return INT386_SUCCESS(RegsOut
);
64 static BOOLEAN
PcDiskReadLogicalSectorsLBA(ULONG DriveNumber
, ULONGLONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
69 PI386_DISK_ADDRESS_PACKET Packet
= (PI386_DISK_ADDRESS_PACKET
)(BIOSCALLBUFFER
);
71 DbgPrint((DPRINT_DISK
, "PcDiskReadLogicalSectorsLBA() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber
, SectorNumber
, SectorCount
, Buffer
));
73 // BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
74 RegsIn
.b
.ah
= 0x42; // Subfunction 42h
75 RegsIn
.b
.dl
= DriveNumber
; // Drive number in DL (0 - floppy, 0x80 - harddisk)
76 RegsIn
.x
.ds
= BIOSCALLBUFSEGMENT
; // DS:SI -> disk address packet
77 RegsIn
.w
.si
= BIOSCALLBUFOFFSET
;
79 // Setup disk address packet
80 RtlZeroMemory(Packet
, sizeof(I386_DISK_ADDRESS_PACKET
));
81 Packet
->PacketSize
= sizeof(I386_DISK_ADDRESS_PACKET
);
83 Packet
->LBABlockCount
= SectorCount
;
84 Packet
->TransferBufferOffset
= ((ULONG
)Buffer
) & 0x0F;
85 Packet
->TransferBufferSegment
= ((ULONG
)Buffer
) >> 4;
86 Packet
->LBAStartBlock
= SectorNumber
;
87 Packet
->TransferBuffer64
= 0;
89 // BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
91 // CF clear if successful
95 // disk address packet's block count field set to the
96 // number of blocks successfully transferred
99 for (RetryCount
=0; RetryCount
<3; RetryCount
++)
101 Int386(0x13, &RegsIn
, &RegsOut
);
103 // If it worked return TRUE
104 if (INT386_SUCCESS(RegsOut
))
108 // If it was a corrected ECC error then the data is still good
109 else if (RegsOut
.b
.ah
== 0x11)
113 // If it failed the do the next retry
116 PcDiskResetController(DriveNumber
);
122 // If we get here then the read failed
123 DiskError("Disk Read Failed", RegsOut
.b
.ah
);
128 static BOOLEAN
PcDiskReadLogicalSectorsCHS(ULONG DriveNumber
, ULONGLONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
130 ULONG PhysicalSector
;
133 GEOMETRY DriveGeometry
;
134 ULONG NumberOfSectorsToRead
;
139 DbgPrint((DPRINT_DISK
, "PcDiskReadLogicalSectorsCHS()\n"));
142 // Get the drive geometry
144 if (!MachDiskGetDriveGeometry(DriveNumber
, &DriveGeometry
) ||
145 DriveGeometry
.Sectors
== 0 ||
146 DriveGeometry
.Heads
== 0)
155 // Calculate the physical disk offsets
157 PhysicalSector
= 1 + (SectorNumber
% DriveGeometry
.Sectors
);
158 PhysicalHead
= (SectorNumber
/ DriveGeometry
.Sectors
) % DriveGeometry
.Heads
;
159 PhysicalTrack
= (SectorNumber
/ DriveGeometry
.Sectors
) / DriveGeometry
.Heads
;
162 // Calculate how many sectors we need to read this round
164 if (PhysicalSector
> 1)
166 if (SectorCount
>= (DriveGeometry
.Sectors
- (PhysicalSector
- 1)))
167 NumberOfSectorsToRead
= (DriveGeometry
.Sectors
- (PhysicalSector
- 1));
169 NumberOfSectorsToRead
= SectorCount
;
173 if (SectorCount
>= DriveGeometry
.Sectors
)
174 NumberOfSectorsToRead
= DriveGeometry
.Sectors
;
176 NumberOfSectorsToRead
= SectorCount
;
180 // Make sure the read is within the geometry boundaries
182 if ((PhysicalHead
>= DriveGeometry
.Heads
) ||
183 (PhysicalTrack
>= DriveGeometry
.Cylinders
) ||
184 ((NumberOfSectorsToRead
+ PhysicalSector
) > (DriveGeometry
.Sectors
+ 1)) ||
185 (PhysicalSector
> DriveGeometry
.Sectors
))
187 DiskError("Disk read exceeds drive geometry limits.", 0);
191 // BIOS Int 13h, function 2 - Read Disk Sectors
193 // AL = number of sectors to read (must be nonzero)
194 // CH = low eight bits of cylinder number
195 // CL = sector number 1-63 (bits 0-5)
196 // high two bits of cylinder (bits 6-7, hard disk only)
198 // DL = drive number (bit 7 set for hard disk)
199 // ES:BX -> data buffer
202 // if AH = 11h (corrected ECC error), AL = burst length
203 // CF clear if successful
205 // AL = number of sectors transferred
206 // (only valid if CF set for some BIOSes)
208 RegsIn
.b
.al
= NumberOfSectorsToRead
;
209 RegsIn
.b
.ch
= (PhysicalTrack
& 0xFF);
210 RegsIn
.b
.cl
= (PhysicalSector
+ ((PhysicalTrack
& 0x300) >> 2));
211 RegsIn
.b
.dh
= PhysicalHead
;
212 RegsIn
.b
.dl
= DriveNumber
;
213 RegsIn
.w
.es
= ((ULONG
)Buffer
) >> 4;
214 RegsIn
.w
.bx
= ((ULONG
)Buffer
) & 0x0F;
220 for (RetryCount
=0; RetryCount
<3; RetryCount
++)
222 Int386(0x13, &RegsIn
, &RegsOut
);
224 // If it worked break out
225 if (INT386_SUCCESS(RegsOut
))
229 // If it was a corrected ECC error then the data is still good
230 else if (RegsOut
.b
.ah
== 0x11)
234 // If it failed the do the next retry
237 PcDiskResetController(DriveNumber
);
243 // If we retried 3 times then fail
246 DiskError("Disk Read Failed", RegsOut
.b
.ah
);
250 // I have learned that not all bioses return
251 // the sector read count in the AL register (at least mine doesn't)
252 // even if the sectors were read correctly. So instead
253 // of checking the sector read count we will rely solely
254 // on the carry flag being set on error
256 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ (NumberOfSectorsToRead
* DriveGeometry
.BytesPerSector
));
257 SectorCount
-= NumberOfSectorsToRead
;
258 SectorNumber
+= NumberOfSectorsToRead
;
264 static BOOLEAN
PcDiskInt13ExtensionsSupported(ULONG DriveNumber
)
266 static ULONG LastDriveNumber
= 0xffffffff;
267 static BOOLEAN LastSupported
;
271 DbgPrint((DPRINT_DISK
, "PcDiskInt13ExtensionsSupported()\n"));
273 if (DriveNumber
== LastDriveNumber
)
275 DbgPrint((DPRINT_DISK
, "Using cached value %s for drive 0x%x\n", LastSupported
? "TRUE" : "FALSE", DriveNumber
));
276 return LastSupported
;
279 LastDriveNumber
= DriveNumber
;
281 // IBM/MS INT 13 Extensions - INSTALLATION CHECK
284 // DL = drive (80h-FFh)
286 // CF set on error (extensions not supported)
287 // AH = 01h (invalid function)
288 // CF clear if successful
289 // BX = AA55h if installed
290 // AH = major version of extensions
292 // 20h = 2.0 / EDD-1.0
293 // 21h = 2.1 / EDD-1.1
296 // CX = API subset support bitmap
297 // DH = extension version (v2.0+ ??? -- not present in 1.x)
299 // Bitfields for IBM/MS INT 13 Extensions API support bitmap
300 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
301 // Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
302 // Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
303 // extended drive parameter table is valid
304 // Bits 3-15 reserved
306 RegsIn
.w
.bx
= 0x55AA;
307 RegsIn
.b
.dl
= DriveNumber
;
309 // Reset the disk controller
310 Int386(0x13, &RegsIn
, &RegsOut
);
312 if (!INT386_SUCCESS(RegsOut
))
314 // CF set on error (extensions not supported)
315 LastSupported
= FALSE
;
319 if (RegsOut
.w
.bx
!= 0xAA55)
321 // BX = AA55h if installed
322 LastSupported
= FALSE
;
327 // The original check is too strict because some BIOSes report that
328 // extended disk access functions are not suported when booting
329 // from a CD (e.g. Phoenix BIOS v6.00PG). Argh!
331 if (!(RegsOut
.w
.cx
& 0x0001))
333 // CX = API subset support bitmap
334 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
335 LastSupported
= FALSE
;
340 // Use this relaxed check instead (most BIOSes seem to use 0x9f as CD-Rom)
341 if (RegsOut
.w
.cx
== 0x0000 && DriveNumber
!= 0x9f)
343 // CX = API subset support bitmap
344 printf("Suspicious API subset support bitmap 0x%x on device 0x%lx\n", RegsOut
.w
.cx
, DriveNumber
);
345 LastSupported
= FALSE
;
346 return LastSupported
;
349 LastSupported
= TRUE
;
353 BOOLEAN
PcDiskReadLogicalSectors(ULONG DriveNumber
, ULONGLONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
356 DbgPrint((DPRINT_DISK
, "PcDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber
, SectorNumber
, SectorCount
, Buffer
));
359 // Check to see if it is a fixed disk drive
360 // If so then check to see if Int13 extensions work
361 // If they do then use them, otherwise default back to BIOS calls
363 if ((DriveNumber
>= 0x80) && PcDiskInt13ExtensionsSupported(DriveNumber
))
365 DbgPrint((DPRINT_DISK
, "Using Int 13 Extensions for read. PcDiskInt13ExtensionsSupported(%d) = %s\n", DriveNumber
, PcDiskInt13ExtensionsSupported(DriveNumber
) ? "TRUE" : "FALSE"));
368 // LBA is easy, nothing to calculate
371 return PcDiskReadLogicalSectorsLBA(DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
375 // LBA is not supported default to the CHS calls
376 return PcDiskReadLogicalSectorsCHS(DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
383 PcDiskGetPartitionEntry(ULONG DriveNumber
, ULONG PartitionNumber
, PPARTITION_TABLE_ENTRY PartitionTableEntry
)
385 /* Just use the standard routine */
386 return DiskGetPartitionEntry(DriveNumber
, PartitionNumber
, PartitionTableEntry
);
390 PcDiskGetDriveGeometry(ULONG DriveNumber
, PGEOMETRY Geometry
)
396 DbgPrint((DPRINT_DISK
, "DiskGetDriveGeometry()\n"));
398 /* BIOS Int 13h, function 08h - Get drive parameters
400 * DL = drive (bit 7 set for hard disk)
401 * ES:DI = 0000h:0000h to guard against BIOS bugs
405 * CF clear if successful
407 * AL = 00h on at least some BIOSes
408 * BL = drive type (AT/PS2 floppies only)
409 * CH = low eight bits of maximum cylinder number
410 * CL = maximum sector number (bits 5-0)
411 * high two bits of maximum cylinder number (bits 7-6)
412 * DH = maximum head number
413 * DL = number of drives
414 * ES:DI -> drive parameter table (floppies only)
417 RegsIn
.b
.dl
= DriveNumber
;
418 RegsIn
.w
.es
= 0x0000;
419 RegsIn
.w
.di
= 0x0000;
421 /* Get drive parameters */
422 Int386(0x13, &RegsIn
, &RegsOut
);
424 if (! INT386_SUCCESS(RegsOut
))
429 Cylinders
= (RegsOut
.b
.cl
& 0xC0) << 2;
430 Cylinders
+= RegsOut
.b
.ch
;
432 Geometry
->Cylinders
= Cylinders
;
433 Geometry
->Heads
= RegsOut
.b
.dh
+ 1;
434 Geometry
->Sectors
= RegsOut
.b
.cl
& 0x3F;
435 Geometry
->BytesPerSector
= 512; /* Just assume 512 bytes per sector */
441 PcDiskGetCacheableBlockCount(ULONG DriveNumber
)
445 /* If LBA is supported then the block size will be 64 sectors (32k)
446 * If not then the block size is the size of one track */
447 if (DiskInt13ExtensionsSupported(DriveNumber
))
451 /* Get the disk geometry
452 * If this fails then we will just return 1 sector to be safe */
453 else if (! PcDiskGetDriveGeometry(DriveNumber
, &Geometry
))
459 return Geometry
.Sectors
;