Help freeloader stop crashing from divide-by-zero, provoked by drain-bamaged BIOS.
[reactos.git] / reactos / 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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "freeldr.h"
21 #include "disk.h"
22 #include "rtl.h"
23 #include "arch.h"
24 #include "debug.h"
25 #include "portio.h"
26 #include "machine.h"
27 #include "machpc.h"
28
29
30 typedef struct
31 {
32 U8 PacketSize; // 00h - Size of packet (10h or 18h)
33 U8 Reserved; // 01h - Reserved (0)
34 U16 LBABlockCount; // 02h - Number of blocks to transfer (max 007Fh for Phoenix EDD)
35 U16 TransferBufferOffset; // 04h - Transfer buffer offset (seg:off)
36 U16 TransferBufferSegment; // Transfer buffer segment (seg:off)
37 U64 LBAStartBlock; // 08h - Starting absolute block number
38 U64 TransferBuffer64; // 10h - (EDD-3.0, optional) 64-bit flat address of transfer buffer
39 // used if DWORD at 04h is FFFFh:FFFFh
40 } PACKED I386_DISK_ADDRESS_PACKET, *PI386_DISK_ADDRESS_PACKET;
41
42 /////////////////////////////////////////////////////////////////////////////////////////////
43 // FUNCTIONS
44 /////////////////////////////////////////////////////////////////////////////////////////////
45
46 static BOOL PcDiskResetController(U32 DriveNumber)
47 {
48 REGS RegsIn;
49 REGS RegsOut;
50
51 DbgPrint((DPRINT_DISK, "PcDiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber));
52
53 // BIOS Int 13h, function 0 - Reset disk system
54 // AH = 00h
55 // DL = drive (if bit 7 is set both hard disks and floppy disks reset)
56 // Return:
57 // AH = status
58 // CF clear if successful
59 // CF set on error
60 RegsIn.b.ah = 0x00;
61 RegsIn.b.dl = DriveNumber;
62
63 // Reset the disk controller
64 Int386(0x13, &RegsIn, &RegsOut);
65
66 return INT386_SUCCESS(RegsOut);
67 }
68
69 static BOOL PcDiskReadLogicalSectorsLBA(U32 DriveNumber, U64 SectorNumber, U32 SectorCount, PVOID Buffer)
70 {
71 REGS RegsIn;
72 REGS RegsOut;
73 U32 RetryCount;
74 PI386_DISK_ADDRESS_PACKET Packet = (PI386_DISK_ADDRESS_PACKET)(BIOSCALLBUFFER);
75
76 DbgPrint((DPRINT_DISK, "PcDiskReadLogicalSectorsLBA() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer));
77
78 // BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
79 RegsIn.b.ah = 0x42; // Subfunction 42h
80 RegsIn.b.dl = DriveNumber; // Drive number in DL (0 - floppy, 0x80 - harddisk)
81 RegsIn.x.ds = BIOSCALLBUFSEGMENT; // DS:SI -> disk address packet
82 RegsIn.w.si = BIOSCALLBUFOFFSET;
83
84 // Setup disk address packet
85 RtlZeroMemory(Packet, sizeof(I386_DISK_ADDRESS_PACKET));
86 Packet->PacketSize = sizeof(I386_DISK_ADDRESS_PACKET);
87 Packet->Reserved = 0;
88 Packet->LBABlockCount = SectorCount;
89 Packet->TransferBufferOffset = ((U32)Buffer) & 0x0F;
90 Packet->TransferBufferSegment = ((U32)Buffer) >> 4;
91 Packet->LBAStartBlock = SectorNumber;
92 Packet->TransferBuffer64 = 0;
93
94 // BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
95 // Return:
96 // CF clear if successful
97 // AH = 00h
98 // CF set on error
99 // AH = error code
100 // disk address packet's block count field set to the
101 // number of blocks successfully transferred
102
103 // Retry 3 times
104 for (RetryCount=0; RetryCount<3; RetryCount++)
105 {
106 Int386(0x13, &RegsIn, &RegsOut);
107
108 // If it worked return TRUE
109 if (INT386_SUCCESS(RegsOut))
110 {
111 return TRUE;
112 }
113 // If it was a corrected ECC error then the data is still good
114 else if (RegsOut.b.ah == 0x11)
115 {
116 return TRUE;
117 }
118 // If it failed the do the next retry
119 else
120 {
121 PcDiskResetController(DriveNumber);
122
123 continue;
124 }
125 }
126
127 // If we get here then the read failed
128 DiskError("Disk Read Failed", RegsOut.b.ah);
129
130 return FALSE;
131 }
132
133 static BOOL PcDiskReadLogicalSectorsCHS(U32 DriveNumber, U64 SectorNumber, U32 SectorCount, PVOID Buffer)
134 {
135 U32 PhysicalSector;
136 U32 PhysicalHead;
137 U32 PhysicalTrack;
138 GEOMETRY DriveGeometry;
139 U32 NumberOfSectorsToRead;
140 REGS RegsIn;
141 REGS RegsOut;
142 U32 RetryCount;
143
144 DbgPrint((DPRINT_DISK, "PcDiskReadLogicalSectorsCHS()\n"));
145
146 //
147 // Get the drive geometry
148 //
149 if (!MachDiskGetDriveGeometry(DriveNumber, &DriveGeometry) ||
150 DriveGeometry.Sectors == 0 ||
151 DriveGeometry.Heads == 0)
152 {
153 return FALSE;
154 }
155
156 while (SectorCount)
157 {
158
159 //
160 // Calculate the physical disk offsets
161 //
162 PhysicalSector = 1 + (SectorNumber % DriveGeometry.Sectors);
163 PhysicalHead = (SectorNumber / DriveGeometry.Sectors) % DriveGeometry.Heads;
164 PhysicalTrack = (SectorNumber / DriveGeometry.Sectors) / DriveGeometry.Heads;
165
166 //
167 // Calculate how many sectors we need to read this round
168 //
169 if (PhysicalSector > 1)
170 {
171 if (SectorCount >= (DriveGeometry.Sectors - (PhysicalSector - 1)))
172 NumberOfSectorsToRead = (DriveGeometry.Sectors - (PhysicalSector - 1));
173 else
174 NumberOfSectorsToRead = SectorCount;
175 }
176 else
177 {
178 if (SectorCount >= DriveGeometry.Sectors)
179 NumberOfSectorsToRead = DriveGeometry.Sectors;
180 else
181 NumberOfSectorsToRead = SectorCount;
182 }
183
184 //
185 // Make sure the read is within the geometry boundaries
186 //
187 if ((PhysicalHead >= DriveGeometry.Heads) ||
188 (PhysicalTrack >= DriveGeometry.Cylinders) ||
189 ((NumberOfSectorsToRead + PhysicalSector) > (DriveGeometry.Sectors + 1)) ||
190 (PhysicalSector > DriveGeometry.Sectors))
191 {
192 DiskError("Disk read exceeds drive geometry limits.", 0);
193 return FALSE;
194 }
195
196 // BIOS Int 13h, function 2 - Read Disk Sectors
197 // AH = 02h
198 // AL = number of sectors to read (must be nonzero)
199 // CH = low eight bits of cylinder number
200 // CL = sector number 1-63 (bits 0-5)
201 // high two bits of cylinder (bits 6-7, hard disk only)
202 // DH = head number
203 // DL = drive number (bit 7 set for hard disk)
204 // ES:BX -> data buffer
205 // Return:
206 // CF set on error
207 // if AH = 11h (corrected ECC error), AL = burst length
208 // CF clear if successful
209 // AH = status
210 // AL = number of sectors transferred
211 // (only valid if CF set for some BIOSes)
212 RegsIn.b.ah = 0x02;
213 RegsIn.b.al = NumberOfSectorsToRead;
214 RegsIn.b.ch = (PhysicalTrack & 0xFF);
215 RegsIn.b.cl = (PhysicalSector + ((PhysicalTrack & 0x300) >> 2));
216 RegsIn.b.dh = PhysicalHead;
217 RegsIn.b.dl = DriveNumber;
218 RegsIn.w.es = ((U32)Buffer) >> 4;
219 RegsIn.w.bx = ((U32)Buffer) & 0x0F;
220
221 //
222 // Perform the read
223 // Retry 3 times
224 //
225 for (RetryCount=0; RetryCount<3; RetryCount++)
226 {
227 Int386(0x13, &RegsIn, &RegsOut);
228
229 // If it worked break out
230 if (INT386_SUCCESS(RegsOut))
231 {
232 break;
233 }
234 // If it was a corrected ECC error then the data is still good
235 else if (RegsOut.b.ah == 0x11)
236 {
237 break;
238 }
239 // If it failed the do the next retry
240 else
241 {
242 PcDiskResetController(DriveNumber);
243
244 continue;
245 }
246 }
247
248 // If we retried 3 times then fail
249 if (RetryCount >= 3)
250 {
251 DiskError("Disk Read Failed", RegsOut.b.ah);
252 return FALSE;
253 }
254
255 // I have learned that not all bioses return
256 // the sector read count in the AL register (at least mine doesn't)
257 // even if the sectors were read correctly. So instead
258 // of checking the sector read count we will rely solely
259 // on the carry flag being set on error
260
261 Buffer += (NumberOfSectorsToRead * DriveGeometry.BytesPerSector);
262 SectorCount -= NumberOfSectorsToRead;
263 SectorNumber += NumberOfSectorsToRead;
264 }
265
266 return TRUE;
267 }
268
269 static BOOL PcDiskInt13ExtensionsSupported(U32 DriveNumber)
270 {
271 static U32 LastDriveNumber = 0xffffffff;
272 static BOOL LastSupported;
273 REGS RegsIn;
274 REGS RegsOut;
275
276 DbgPrint((DPRINT_DISK, "PcDiskInt13ExtensionsSupported()\n"));
277
278 if (DriveNumber == LastDriveNumber)
279 {
280 DbgPrint((DPRINT_DISK, "Using cached value %s for drive 0x%x\n", LastSupported ? "TRUE" : "FALSE", DriveNumber));
281 return LastSupported;
282 }
283
284 LastDriveNumber = DriveNumber;
285
286 // IBM/MS INT 13 Extensions - INSTALLATION CHECK
287 // AH = 41h
288 // BX = 55AAh
289 // DL = drive (80h-FFh)
290 // Return:
291 // CF set on error (extensions not supported)
292 // AH = 01h (invalid function)
293 // CF clear if successful
294 // BX = AA55h if installed
295 // AH = major version of extensions
296 // 01h = 1.x
297 // 20h = 2.0 / EDD-1.0
298 // 21h = 2.1 / EDD-1.1
299 // 30h = EDD-3.0
300 // AL = internal use
301 // CX = API subset support bitmap
302 // DH = extension version (v2.0+ ??? -- not present in 1.x)
303 //
304 // Bitfields for IBM/MS INT 13 Extensions API support bitmap
305 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
306 // Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
307 // Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
308 // extended drive parameter table is valid
309 // Bits 3-15 reserved
310 RegsIn.b.ah = 0x41;
311 RegsIn.w.bx = 0x55AA;
312 RegsIn.b.dl = DriveNumber;
313
314 // Reset the disk controller
315 Int386(0x13, &RegsIn, &RegsOut);
316
317 if (!INT386_SUCCESS(RegsOut))
318 {
319 // CF set on error (extensions not supported)
320 LastSupported = FALSE;
321 return FALSE;
322 }
323
324 if (RegsOut.w.bx != 0xAA55)
325 {
326 // BX = AA55h if installed
327 LastSupported = FALSE;
328 return FALSE;
329 }
330
331 // Note:
332 // The original check is too strict because some BIOSes report that
333 // extended disk access functions are not suported when booting
334 // from a CD (e.g. Phoenix BIOS v6.00PG). Argh!
335 #if 0
336 if (!(RegsOut.w.cx & 0x0001))
337 {
338 // CX = API subset support bitmap
339 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
340 LastSupported = FALSE;
341 return FALSE;
342 }
343 #endif
344
345 // Use this relaxed check instead (most BIOSes seem to use 0x9f as CD-Rom)
346 if (RegsOut.w.cx == 0x0000 && DriveNumber != 0x9f)
347 {
348 // CX = API subset support bitmap
349 printf("Suspicious API subset support bitmap 0x%x on device 0x%x\n", RegsOut.w.cx, DriveNumber);
350 LastSupported = FALSE;
351 return LastSupported;
352 }
353
354 LastSupported = TRUE;
355 return TRUE;
356 }
357
358 BOOL PcDiskReadLogicalSectors(U32 DriveNumber, U64 SectorNumber, U32 SectorCount, PVOID Buffer)
359 {
360
361 DbgPrint((DPRINT_DISK, "PcDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer));
362
363 //
364 // Check to see if it is a fixed disk drive
365 // If so then check to see if Int13 extensions work
366 // If they do then use them, otherwise default back to BIOS calls
367 //
368 if ((DriveNumber >= 0x80) && PcDiskInt13ExtensionsSupported(DriveNumber))
369 {
370 DbgPrint((DPRINT_DISK, "Using Int 13 Extensions for read. PcDiskInt13ExtensionsSupported(%d) = %s\n", DriveNumber, PcDiskInt13ExtensionsSupported(DriveNumber) ? "TRUE" : "FALSE"));
371
372 //
373 // LBA is easy, nothing to calculate
374 // Just do the read
375 //
376 return PcDiskReadLogicalSectorsLBA(DriveNumber, SectorNumber, SectorCount, Buffer);
377 }
378 else
379 {
380 // LBA is not supported default to the CHS calls
381 return PcDiskReadLogicalSectorsCHS(DriveNumber, SectorNumber, SectorCount, Buffer);
382 }
383
384 return TRUE;
385 }
386
387 BOOL
388 PcDiskGetPartitionEntry(U32 DriveNumber, U32 PartitionNumber, PPARTITION_TABLE_ENTRY PartitionTableEntry)
389 {
390 /* Just use the standard routine */
391 return DiskGetPartitionEntry(DriveNumber, PartitionNumber, PartitionTableEntry);
392 }
393
394 BOOL
395 PcDiskGetDriveGeometry(U32 DriveNumber, PGEOMETRY Geometry)
396 {
397 REGS RegsIn;
398 REGS RegsOut;
399 U32 Cylinders;
400
401 DbgPrint((DPRINT_DISK, "DiskGetDriveGeometry()\n"));
402
403 /* BIOS Int 13h, function 08h - Get drive parameters
404 * AH = 08h
405 * DL = drive (bit 7 set for hard disk)
406 * ES:DI = 0000h:0000h to guard against BIOS bugs
407 * Return:
408 * CF set on error
409 * AH = status (07h)
410 * CF clear if successful
411 * AH = 00h
412 * AL = 00h on at least some BIOSes
413 * BL = drive type (AT/PS2 floppies only)
414 * CH = low eight bits of maximum cylinder number
415 * CL = maximum sector number (bits 5-0)
416 * high two bits of maximum cylinder number (bits 7-6)
417 * DH = maximum head number
418 * DL = number of drives
419 * ES:DI -> drive parameter table (floppies only)
420 */
421 RegsIn.b.ah = 0x08;
422 RegsIn.b.dl = DriveNumber;
423 RegsIn.w.es = 0x0000;
424 RegsIn.w.di = 0x0000;
425
426 /* Get drive parameters */
427 Int386(0x13, &RegsIn, &RegsOut);
428
429 if (! INT386_SUCCESS(RegsOut))
430 {
431 return FALSE;
432 }
433
434 Cylinders = (RegsOut.b.cl & 0xC0) << 2;
435 Cylinders += RegsOut.b.ch;
436 Cylinders++;
437 Geometry->Cylinders = Cylinders;
438 Geometry->Heads = RegsOut.b.dh + 1;
439 Geometry->Sectors = RegsOut.b.cl & 0x3F;
440 Geometry->BytesPerSector = 512; /* Just assume 512 bytes per sector */
441
442 return TRUE;
443 }
444
445 U32
446 PcDiskGetCacheableBlockCount(U32 DriveNumber)
447 {
448 GEOMETRY Geometry;
449
450 /* If LBA is supported then the block size will be 64 sectors (32k)
451 * If not then the block size is the size of one track */
452 if (DiskInt13ExtensionsSupported(DriveNumber))
453 {
454 return 64;
455 }
456 /* Get the disk geometry
457 * If this fails then we will just return 1 sector to be safe */
458 else if (! PcDiskGetDriveGeometry(DriveNumber, &Geometry))
459 {
460 return 1;
461 }
462 else
463 {
464 return Geometry.Sectors;
465 }
466 }
467
468 /* EOF */