[FREELDR] Limit the usage of DiskStopFloppyMotor() in hardware/platform-specific...
[reactos.git] / boot / freeldr / freeldr / arch / i386 / pcdisk.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 // #if defined(__i386__) || defined(_M_AMD64)
21
22 #include <freeldr.h>
23
24 #include <debug.h>
25 DBG_DEFAULT_CHANNEL(DISK);
26
27 #include <pshpack2.h>
28
29 typedef struct
30 {
31 UCHAR PacketSize; // 00h - Size of packet (10h or 18h)
32 UCHAR Reserved; // 01h - Reserved (0)
33 USHORT LBABlockCount; // 02h - Number of blocks to transfer (max 007Fh for Phoenix EDD)
34 USHORT TransferBufferOffset; // 04h - Transfer buffer offset (seg:off)
35 USHORT TransferBufferSegment; // Transfer buffer segment (seg:off)
36 ULONGLONG LBAStartBlock; // 08h - Starting absolute block number
37 // ULONGLONG TransferBuffer64; // 10h - (EDD-3.0, optional) 64-bit flat address of transfer buffer
38 // used if DWORD at 04h is FFFFh:FFFFh
39 // Commented since some earlier BIOSes refuse to work with
40 // such extended structure
41 } I386_DISK_ADDRESS_PACKET, *PI386_DISK_ADDRESS_PACKET;
42
43 typedef struct
44 {
45 UCHAR PacketSize; // 00h - Size of packet in bytes (13h)
46 UCHAR MediaType; // 01h - Boot media type (see #00282)
47 UCHAR DriveNumber; /* 02h - Drive number:
48 * 00h Floppy image
49 * 80h Bootable hard disk
50 * 81h-FFh Nonbootable or no emulation
51 */
52 UCHAR Controller; // 03h - CD-ROM controller number
53 ULONG LBAImage; // 04h - Logical Block Address of disk image to emulate
54 USHORT DeviceSpec; /* 08h - Device specification (see also #00282)
55 * (IDE) Bit 0:
56 * Drive is slave instead of master
57 * (SCSI) Bits 7-0:
58 * LUN and PUN
59 * Bits 15-8:
60 * Bus number
61 */
62 USHORT Buffer; // 0Ah - Segment of 3K buffer for caching CD-ROM reads
63 USHORT LoadSeg; // 0Ch - Load segment for initial boot image.
64 // If 0000h, load at segment 07C0h.
65 USHORT SectorCount; // 0Eh - Number of 512-byte virtual sectors to load
66 // (only valid for AH=4Ch).
67 UCHAR CHSGeometry[3]; /* 10h - Low byte of cylinder count (for INT 13/AH=08h)
68 * 11h - Sector count, high bits of cylinder count (for INT 13/AH=08h)
69 * 12h - Head count (for INT 13/AH=08h)
70 */
71 UCHAR Reserved;
72 } I386_CDROM_SPEC_PACKET, *PI386_CDROM_SPEC_PACKET;
73
74 #include <poppack.h>
75
76 /* DISK IO ERROR SUPPORT *****************************************************/
77
78 static BOOLEAN bReportError = TRUE;
79
80 VOID DiskReportError(BOOLEAN bError)
81 {
82 bReportError = bError;
83 }
84
85 static PCSTR DiskGetErrorCodeString(ULONG ErrorCode)
86 {
87 switch (ErrorCode)
88 {
89 case 0x00: return "no error";
90 case 0x01: return "bad command passed to driver";
91 case 0x02: return "address mark not found or bad sector";
92 case 0x03: return "diskette write protect error";
93 case 0x04: return "sector not found";
94 case 0x05: return "fixed disk reset failed";
95 case 0x06: return "diskette changed or removed";
96 case 0x07: return "bad fixed disk parameter table";
97 case 0x08: return "DMA overrun";
98 case 0x09: return "DMA access across 64k boundary";
99 case 0x0A: return "bad fixed disk sector flag";
100 case 0x0B: return "bad fixed disk cylinder";
101 case 0x0C: return "unsupported track/invalid media";
102 case 0x0D: return "invalid number of sectors on fixed disk format";
103 case 0x0E: return "fixed disk controlled data address mark detected";
104 case 0x0F: return "fixed disk DMA arbitration level out of range";
105 case 0x10: return "ECC/CRC error on disk read";
106 case 0x11: return "recoverable fixed disk data error, data fixed by ECC";
107 case 0x20: return "controller error (NEC for floppies)";
108 case 0x40: return "seek failure";
109 case 0x80: return "time out, drive not ready";
110 case 0xAA: return "fixed disk drive not ready";
111 case 0xBB: return "fixed disk undefined error";
112 case 0xCC: return "fixed disk write fault on selected drive";
113 case 0xE0: return "fixed disk status error/Error reg = 0";
114 case 0xFF: return "sense operation failed";
115
116 default: return "unknown error code";
117 }
118 }
119
120 static VOID DiskError(PCSTR ErrorString, ULONG ErrorCode)
121 {
122 CHAR ErrorCodeString[200];
123
124 if (bReportError == FALSE)
125 return;
126
127 sprintf(ErrorCodeString, "%s\n\nError Code: 0x%lx\nError: %s",
128 ErrorString, ErrorCode, DiskGetErrorCodeString(ErrorCode));
129
130 TRACE("%s\n", ErrorCodeString);
131
132 UiMessageBox(ErrorCodeString);
133 }
134
135 /* FUNCTIONS *****************************************************************/
136
137 BOOLEAN DiskResetController(UCHAR DriveNumber)
138 {
139 REGS RegsIn, RegsOut;
140
141 WARN("DiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber);
142
143 /*
144 * BIOS Int 13h, function 0 - Reset disk system
145 * AH = 00h
146 * DL = drive (if bit 7 is set both hard disks and floppy disks reset)
147 * Return:
148 * AH = status
149 * CF clear if successful
150 * CF set on error
151 */
152 RegsIn.b.ah = 0x00;
153 RegsIn.b.dl = DriveNumber;
154
155 /* Reset the disk controller */
156 Int386(0x13, &RegsIn, &RegsOut);
157
158 return INT386_SUCCESS(RegsOut);
159 }
160
161 static BOOLEAN PcDiskReadLogicalSectorsLBA(UCHAR DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
162 {
163 REGS RegsIn, RegsOut;
164 ULONG RetryCount;
165 PI386_DISK_ADDRESS_PACKET Packet = (PI386_DISK_ADDRESS_PACKET)(BIOSCALLBUFFER);
166
167 TRACE("PcDiskReadLogicalSectorsLBA() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer);
168 ASSERT(((ULONG_PTR)Buffer) <= 0xFFFFF);
169
170 /* Setup disk address packet */
171 RtlZeroMemory(Packet, sizeof(*Packet));
172 Packet->PacketSize = sizeof(*Packet);
173 Packet->Reserved = 0;
174 Packet->LBABlockCount = (USHORT)SectorCount;
175 ASSERT(Packet->LBABlockCount == SectorCount);
176 Packet->TransferBufferOffset = ((ULONG_PTR)Buffer) & 0x0F;
177 Packet->TransferBufferSegment = (USHORT)(((ULONG_PTR)Buffer) >> 4);
178 Packet->LBAStartBlock = SectorNumber;
179
180 /*
181 * BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
182 * Return:
183 * CF clear if successful
184 * AH = 00h
185 * CF set on error
186 * AH = error code
187 * Disk address packet's block count field set to the
188 * number of blocks successfully transferred.
189 */
190 RegsIn.b.ah = 0x42; // Subfunction 42h
191 RegsIn.b.dl = DriveNumber; // Drive number in DL (0 - floppy, 0x80 - harddisk)
192 RegsIn.x.ds = BIOSCALLBUFSEGMENT; // DS:SI -> disk address packet
193 RegsIn.w.si = BIOSCALLBUFOFFSET;
194
195 /* Retry 3 times */
196 for (RetryCount=0; RetryCount<3; RetryCount++)
197 {
198 Int386(0x13, &RegsIn, &RegsOut);
199
200 /* If it worked return TRUE */
201 if (INT386_SUCCESS(RegsOut))
202 {
203 return TRUE;
204 }
205 /* If it was a corrected ECC error then the data is still good */
206 else if (RegsOut.b.ah == 0x11)
207 {
208 return TRUE;
209 }
210 /* If it failed then do the next retry */
211 else
212 {
213 DiskResetController(DriveNumber);
214 continue;
215 }
216 }
217
218 /* If we get here then the read failed */
219 DiskError("Disk Read Failed in LBA mode", RegsOut.b.ah);
220 ERR("Disk Read Failed in LBA mode: %x (%s) (DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d)\n",
221 RegsOut.b.ah, DiskGetErrorCodeString(RegsOut.b.ah),
222 DriveNumber, SectorNumber, SectorCount);
223
224 return FALSE;
225 }
226
227 static BOOLEAN PcDiskReadLogicalSectorsCHS(UCHAR DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
228 {
229 UCHAR PhysicalSector;
230 UCHAR PhysicalHead;
231 ULONG PhysicalTrack;
232 GEOMETRY DriveGeometry;
233 ULONG NumberOfSectorsToRead;
234 REGS RegsIn, RegsOut;
235 ULONG RetryCount;
236
237 TRACE("PcDiskReadLogicalSectorsCHS()\n");
238
239 /* Get the drive geometry */
240 if (!MachDiskGetDriveGeometry(DriveNumber, &DriveGeometry) ||
241 DriveGeometry.Sectors == 0 ||
242 DriveGeometry.Heads == 0)
243 {
244 return FALSE;
245 }
246
247 while (SectorCount)
248 {
249 /*
250 * Calculate the physical disk offsets.
251 * Note: DriveGeometry.Sectors < 64
252 */
253 PhysicalSector = 1 + (UCHAR)(SectorNumber % DriveGeometry.Sectors);
254 PhysicalHead = (UCHAR)((SectorNumber / DriveGeometry.Sectors) % DriveGeometry.Heads);
255 PhysicalTrack = (ULONG)((SectorNumber / DriveGeometry.Sectors) / DriveGeometry.Heads);
256
257 /* Calculate how many sectors we need to read this round */
258 if (PhysicalSector > 1)
259 {
260 if (SectorCount >= (DriveGeometry.Sectors - (PhysicalSector - 1)))
261 NumberOfSectorsToRead = (DriveGeometry.Sectors - (PhysicalSector - 1));
262 else
263 NumberOfSectorsToRead = SectorCount;
264 }
265 else
266 {
267 if (SectorCount >= DriveGeometry.Sectors)
268 NumberOfSectorsToRead = DriveGeometry.Sectors;
269 else
270 NumberOfSectorsToRead = SectorCount;
271 }
272
273 /* Make sure the read is within the geometry boundaries */
274 if ((PhysicalHead >= DriveGeometry.Heads) ||
275 (PhysicalTrack >= DriveGeometry.Cylinders) ||
276 ((NumberOfSectorsToRead + PhysicalSector) > (DriveGeometry.Sectors + 1)) ||
277 (PhysicalSector > DriveGeometry.Sectors))
278 {
279 DiskError("Disk read exceeds drive geometry limits.", 0);
280 return FALSE;
281 }
282
283 /*
284 * BIOS Int 13h, function 2 - Read Disk Sectors
285 * AH = 02h
286 * AL = number of sectors to read (must be nonzero)
287 * CH = low eight bits of cylinder number
288 * CL = sector number 1-63 (bits 0-5)
289 * high two bits of cylinder (bits 6-7, hard disk only)
290 * DH = head number
291 * DL = drive number (bit 7 set for hard disk)
292 * ES:BX -> data buffer
293 * Return:
294 * CF set on error
295 * if AH = 11h (corrected ECC error), AL = burst length
296 * CF clear if successful
297 * AH = status
298 * AL = number of sectors transferred
299 * (only valid if CF set for some BIOSes)
300 */
301 RegsIn.b.ah = 0x02;
302 RegsIn.b.al = (UCHAR)NumberOfSectorsToRead;
303 RegsIn.b.ch = (PhysicalTrack & 0xFF);
304 RegsIn.b.cl = (UCHAR)(PhysicalSector + ((PhysicalTrack & 0x300) >> 2));
305 RegsIn.b.dh = PhysicalHead;
306 RegsIn.b.dl = DriveNumber;
307 RegsIn.w.es = (USHORT)(((ULONG_PTR)Buffer) >> 4);
308 RegsIn.w.bx = ((ULONG_PTR)Buffer) & 0x0F;
309
310 /* Perform the read. Retry 3 times. */
311 for (RetryCount=0; RetryCount<3; RetryCount++)
312 {
313 Int386(0x13, &RegsIn, &RegsOut);
314
315 /* If it worked break out */
316 if (INT386_SUCCESS(RegsOut))
317 {
318 break;
319 }
320 /* If it was a corrected ECC error then the data is still good */
321 else if (RegsOut.b.ah == 0x11)
322 {
323 break;
324 }
325 /* If it failed the do the next retry */
326 else
327 {
328 DiskResetController(DriveNumber);
329 continue;
330 }
331 }
332
333 /* If we retried 3 times then fail */
334 if (RetryCount >= 3)
335 {
336 DiskError("Disk Read Failed in CHS mode, after retrying 3 times", RegsOut.b.ah);
337 ERR("Disk Read Failed in CHS mode, after retrying 3 times: %x (%s) (DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d)\n",
338 RegsOut.b.ah, DiskGetErrorCodeString(RegsOut.b.ah),
339 DriveNumber, SectorNumber, SectorCount);
340 return FALSE;
341 }
342
343 // I have learned that not all BIOSes return
344 // the sector read count in the AL register (at least mine doesn't)
345 // even if the sectors were read correctly. So instead
346 // of checking the sector read count we will rely solely
347 // on the carry flag being set on error
348
349 Buffer = (PVOID)((ULONG_PTR)Buffer + (NumberOfSectorsToRead * DriveGeometry.BytesPerSector));
350 SectorCount -= NumberOfSectorsToRead;
351 SectorNumber += NumberOfSectorsToRead;
352 }
353
354 return TRUE;
355 }
356
357 static BOOLEAN DiskInt13ExtensionsSupported(UCHAR DriveNumber)
358 {
359 static UCHAR LastDriveNumber = 0xff;
360 static BOOLEAN LastSupported;
361 REGS RegsIn, RegsOut;
362
363 TRACE("DiskInt13ExtensionsSupported()\n");
364
365 if (DriveNumber == LastDriveNumber)
366 {
367 TRACE("Using cached value %s for drive 0x%x\n",
368 LastSupported ? "TRUE" : "FALSE", DriveNumber);
369 return LastSupported;
370 }
371
372 /*
373 * Some BIOSes report that extended disk access functions are not supported
374 * when booting from a CD (e.g. Phoenix BIOS v6.00PG and Insyde BIOS shipping
375 * with Intel Macs). Therefore we just return TRUE if we're booting from a CD -
376 * we can assume that all El Torito capable BIOSes support INT 13 extensions.
377 * We simply detect whether we're booting from CD by checking whether the drive
378 * number is >= 0x8A. It's 0x90 on the Insyde BIOS, and 0x9F on most other BIOSes.
379 */
380 if (DriveNumber >= 0x8A)
381 {
382 LastSupported = TRUE;
383 return TRUE;
384 }
385
386 LastDriveNumber = DriveNumber;
387
388 /*
389 * IBM/MS INT 13 Extensions - INSTALLATION CHECK
390 * AH = 41h
391 * BX = 55AAh
392 * DL = drive (80h-FFh)
393 * Return:
394 * CF set on error (extensions not supported)
395 * AH = 01h (invalid function)
396 * CF clear if successful
397 * BX = AA55h if installed
398 * AH = major version of extensions
399 * 01h = 1.x
400 * 20h = 2.0 / EDD-1.0
401 * 21h = 2.1 / EDD-1.1
402 * 30h = EDD-3.0
403 * AL = internal use
404 * CX = API subset support bitmap
405 * DH = extension version (v2.0+ ??? -- not present in 1.x)
406 *
407 * Bitfields for IBM/MS INT 13 Extensions API support bitmap
408 * Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
409 * Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
410 * Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
411 * extended drive parameter table is valid
412 * Bits 3-15 reserved
413 */
414 RegsIn.b.ah = 0x41;
415 RegsIn.w.bx = 0x55AA;
416 RegsIn.b.dl = DriveNumber;
417
418 /* Reset the disk controller */
419 Int386(0x13, &RegsIn, &RegsOut);
420
421 if (!INT386_SUCCESS(RegsOut))
422 {
423 /* CF set on error (extensions not supported) */
424 LastSupported = FALSE;
425 return FALSE;
426 }
427
428 if (RegsOut.w.bx != 0xAA55)
429 {
430 /* BX = AA55h if installed */
431 LastSupported = FALSE;
432 return FALSE;
433 }
434
435 if (!(RegsOut.w.cx & 0x0001))
436 {
437 /*
438 * CX = API subset support bitmap.
439 * Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported.
440 */
441 DbgPrint("Suspicious API subset support bitmap 0x%x on device 0x%lx\n",
442 RegsOut.w.cx, DriveNumber);
443 LastSupported = FALSE;
444 return FALSE;
445 }
446
447 LastSupported = TRUE;
448 return TRUE;
449 }
450
451 BOOLEAN PcDiskReadLogicalSectors(UCHAR DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
452 {
453 BOOLEAN ExtensionsSupported;
454
455 TRACE("PcDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n",
456 DriveNumber, SectorNumber, SectorCount, Buffer);
457
458 /*
459 * Check to see if it is a fixed disk drive.
460 * If so then check to see if Int13 extensions work.
461 * If they do then use them, otherwise default back to BIOS calls.
462 */
463 ExtensionsSupported = DiskInt13ExtensionsSupported(DriveNumber);
464
465 if ((DriveNumber >= 0x80) && ExtensionsSupported)
466 {
467 TRACE("Using Int 13 Extensions for read. DiskInt13ExtensionsSupported(%d) = %s\n", DriveNumber, ExtensionsSupported ? "TRUE" : "FALSE");
468
469 /* LBA is easy, nothing to calculate. Just do the read. */
470 return PcDiskReadLogicalSectorsLBA(DriveNumber, SectorNumber, SectorCount, Buffer);
471 }
472 else
473 {
474 /* LBA is not supported default to the CHS calls */
475 return PcDiskReadLogicalSectorsCHS(DriveNumber, SectorNumber, SectorCount, Buffer);
476 }
477
478 return TRUE;
479 }
480
481 #if defined(__i386__) || defined(_M_AMD64)
482 VOID __cdecl DiskStopFloppyMotor(VOID)
483 {
484 WRITE_PORT_UCHAR((PUCHAR)0x3F2, 0x0C); // DOR_FDC_ENABLE | DOR_DMA_IO_INTERFACE_ENABLE
485 }
486 #endif // defined __i386__ || defined(_M_AMD64)
487
488 BOOLEAN DiskGetExtendedDriveParameters(UCHAR DriveNumber, PVOID Buffer, USHORT BufferSize)
489 {
490 REGS RegsIn, RegsOut;
491 PUSHORT Ptr = (PUSHORT)(BIOSCALLBUFFER);
492
493 TRACE("DiskGetExtendedDriveParameters()\n");
494
495 if (!DiskInt13ExtensionsSupported(DriveNumber))
496 return FALSE;
497
498 /* Initialize transfer buffer */
499 *Ptr = BufferSize;
500
501 /*
502 * BIOS Int 13h, function 48h - Get drive parameters
503 * AH = 48h
504 * DL = drive (bit 7 set for hard disk)
505 * DS:SI = result buffer
506 * Return:
507 * CF set on error
508 * AH = status (07h)
509 * CF clear if successful
510 * AH = 00h
511 * DS:SI -> result buffer
512 */
513 RegsIn.b.ah = 0x48;
514 RegsIn.b.dl = DriveNumber;
515 RegsIn.x.ds = BIOSCALLBUFSEGMENT; // DS:SI -> result buffer
516 RegsIn.w.si = BIOSCALLBUFOFFSET;
517
518 /* Get drive parameters */
519 Int386(0x13, &RegsIn, &RegsOut);
520 if (!INT386_SUCCESS(RegsOut))
521 return FALSE;
522
523 memcpy(Buffer, Ptr, BufferSize);
524
525 #if DBG
526 TRACE("size of buffer: %x\n", Ptr[0]);
527 TRACE("information flags: %x\n", Ptr[1]);
528 TRACE("number of physical cylinders on drive: %u\n", *(PULONG)&Ptr[2]);
529 TRACE("number of physical heads on drive: %u\n", *(PULONG)&Ptr[4]);
530 TRACE("number of physical sectors per track: %u\n", *(PULONG)&Ptr[6]);
531 TRACE("total number of sectors on drive: %I64u\n", *(unsigned long long*)&Ptr[8]);
532 TRACE("bytes per sector: %u\n", Ptr[12]);
533 if (Ptr[0] >= 0x1e)
534 {
535 TRACE("EED configuration parameters: %x:%x\n", Ptr[13], Ptr[14]);
536 if (Ptr[13] != 0xffff && Ptr[14] != 0xffff)
537 {
538 PUCHAR SpecPtr = (PUCHAR)(ULONG_PTR)((Ptr[13] << 4) + Ptr[14]);
539 TRACE("SpecPtr: %x\n", SpecPtr);
540 TRACE("physical I/O port base address: %x\n", *(PUSHORT)&SpecPtr[0]);
541 TRACE("disk-drive control port address: %x\n", *(PUSHORT)&SpecPtr[2]);
542 TRACE("drive flags: %x\n", SpecPtr[4]);
543 TRACE("proprietary information: %x\n", SpecPtr[5]);
544 TRACE("IRQ for drive: %u\n", SpecPtr[6]);
545 TRACE("sector count for multi-sector transfers: %u\n", SpecPtr[7]);
546 TRACE("DMA control: %x\n", SpecPtr[8]);
547 TRACE("programmed I/O control: %x\n", SpecPtr[9]);
548 TRACE("drive options: %x\n", *(PUSHORT)&SpecPtr[10]);
549 }
550 }
551 if (Ptr[0] >= 0x42)
552 {
553 TRACE("signature: %x\n", Ptr[15]);
554 }
555 #endif
556
557 return TRUE;
558 }
559
560 BOOLEAN
561 PcDiskGetDriveGeometry(UCHAR DriveNumber, PGEOMETRY Geometry)
562 {
563 EXTENDED_GEOMETRY ExtGeometry;
564 REGS RegsIn, RegsOut;
565 ULONG Cylinders;
566
567 TRACE("DiskGetDriveGeometry()\n");
568
569 /* Try to get the extended geometry first */
570 ExtGeometry.Size = sizeof(ExtGeometry);
571 if (DiskGetExtendedDriveParameters(DriveNumber, &ExtGeometry, ExtGeometry.Size))
572 {
573 Geometry->Cylinders = ExtGeometry.Cylinders;
574 Geometry->Heads = ExtGeometry.Heads;
575 Geometry->Sectors = ExtGeometry.SectorsPerTrack;
576 Geometry->BytesPerSector = ExtGeometry.BytesPerSector;
577 return TRUE;
578 }
579
580 /*
581 * BIOS Int 13h, function 08h - Get drive parameters
582 * AH = 08h
583 * DL = drive (bit 7 set for hard disk)
584 * ES:DI = 0000h:0000h to guard against BIOS bugs
585 * Return:
586 * CF set on error
587 * AH = status (07h)
588 * CF clear if successful
589 * AH = 00h
590 * AL = 00h on at least some BIOSes
591 * BL = drive type (AT/PS2 floppies only)
592 * CH = low eight bits of maximum cylinder number
593 * CL = maximum sector number (bits 5-0)
594 * high two bits of maximum cylinder number (bits 7-6)
595 * DH = maximum head number
596 * DL = number of drives
597 * ES:DI -> drive parameter table (floppies only)
598 */
599 RegsIn.b.ah = 0x08;
600 RegsIn.b.dl = DriveNumber;
601 RegsIn.w.es = 0x0000;
602 RegsIn.w.di = 0x0000;
603
604 /* Get drive parameters */
605 Int386(0x13, &RegsIn, &RegsOut);
606 if (!INT386_SUCCESS(RegsOut))
607 return FALSE;
608
609 Cylinders = (RegsOut.b.cl & 0xC0) << 2;
610 Cylinders += RegsOut.b.ch;
611 Cylinders++;
612 Geometry->Cylinders = Cylinders;
613 Geometry->Heads = RegsOut.b.dh + 1;
614 Geometry->Sectors = RegsOut.b.cl & 0x3F;
615 Geometry->BytesPerSector = 512; /* Just assume 512 bytes per sector */
616
617 return TRUE;
618 }
619
620 ULONG
621 PcDiskGetCacheableBlockCount(UCHAR DriveNumber)
622 {
623 GEOMETRY Geometry;
624
625 /* If LBA is supported then the block size will be 64 sectors (32k)
626 * If not then the block size is the size of one track. */
627 if (DiskInt13ExtensionsSupported(DriveNumber))
628 {
629 return 64;
630 }
631 /* Get the disk geometry. If this fails then we will
632 * just return 1 sector to be safe. */
633 else if (!PcDiskGetDriveGeometry(DriveNumber, &Geometry))
634 {
635 return 1;
636 }
637 else
638 {
639 return Geometry.Sectors;
640 }
641 }
642
643 /* EOF */