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