* Sync to trunk r63845.
[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 #include <freeldr.h>
21 #include <debug.h>
22
23 DBG_DEFAULT_CHANNEL(DISK);
24
25 #include <pshpack2.h>
26 typedef struct
27 {
28 UCHAR PacketSize; // 00h - Size of packet (10h or 18h)
29 UCHAR Reserved; // 01h - Reserved (0)
30 USHORT LBABlockCount; // 02h - Number of blocks to transfer (max 007Fh for Phoenix EDD)
31 USHORT TransferBufferOffset; // 04h - Transfer buffer offset (seg:off)
32 USHORT TransferBufferSegment; // Transfer buffer segment (seg:off)
33 ULONGLONG LBAStartBlock; // 08h - Starting absolute block number
34 //ULONGLONG TransferBuffer64; // 10h - (EDD-3.0, optional) 64-bit flat address of transfer buffer
35 // used if DWORD at 04h is FFFFh:FFFFh
36 // Commented since some earlier BIOSes refuse to work with
37 // such extended structure
38 } I386_DISK_ADDRESS_PACKET, *PI386_DISK_ADDRESS_PACKET;
39 #include <poppack.h>
40
41 /////////////////////////////////////////////////////////////////////////////////////////////
42 // FUNCTIONS
43 /////////////////////////////////////////////////////////////////////////////////////////////
44
45 static BOOLEAN PcDiskResetController(UCHAR DriveNumber)
46 {
47 REGS RegsIn;
48 REGS RegsOut;
49
50 WARN("PcDiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber);
51
52 // BIOS Int 13h, function 0 - Reset disk system
53 // AH = 00h
54 // DL = drive (if bit 7 is set both hard disks and floppy disks reset)
55 // Return:
56 // AH = status
57 // CF clear if successful
58 // CF set on error
59 RegsIn.b.ah = 0x00;
60 RegsIn.b.dl = DriveNumber;
61
62 // Reset the disk controller
63 Int386(0x13, &RegsIn, &RegsOut);
64
65 return INT386_SUCCESS(RegsOut);
66 }
67
68 static BOOLEAN PcDiskReadLogicalSectorsLBA(UCHAR DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
69 {
70 REGS RegsIn;
71 REGS RegsOut;
72 ULONG RetryCount;
73 PI386_DISK_ADDRESS_PACKET Packet = (PI386_DISK_ADDRESS_PACKET)(BIOSCALLBUFFER);
74
75 TRACE("PcDiskReadLogicalSectorsLBA() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer);
76 ASSERT(((ULONG_PTR)Buffer) <= 0xFFFFF);
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 = (USHORT)SectorCount;
89 ASSERT(Packet->LBABlockCount == SectorCount);
90 Packet->TransferBufferOffset = ((ULONG_PTR)Buffer) & 0x0F;
91 Packet->TransferBufferSegment = (USHORT)(((ULONG_PTR)Buffer) >> 4);
92 Packet->LBAStartBlock = SectorNumber;
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 ERR("Disk Read Failed in LBA mode: %x (DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d)\n", RegsOut.b.ah, DriveNumber, SectorNumber, SectorCount);
129
130 return FALSE;
131 }
132
133 static BOOLEAN PcDiskReadLogicalSectorsCHS(UCHAR DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
134 {
135 UCHAR PhysicalSector;
136 UCHAR PhysicalHead;
137 ULONG PhysicalTrack;
138 GEOMETRY DriveGeometry;
139 ULONG NumberOfSectorsToRead;
140 REGS RegsIn;
141 REGS RegsOut;
142 ULONG RetryCount;
143
144 TRACE("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 // Note: DriveGeometry.Sectors < 64
162 //
163 PhysicalSector = 1 + (UCHAR)(SectorNumber % DriveGeometry.Sectors);
164 PhysicalHead = (UCHAR)((SectorNumber / DriveGeometry.Sectors) % DriveGeometry.Heads);
165 PhysicalTrack = (ULONG)((SectorNumber / DriveGeometry.Sectors) / DriveGeometry.Heads);
166
167 //
168 // Calculate how many sectors we need to read this round
169 //
170 if (PhysicalSector > 1)
171 {
172 if (SectorCount >= (DriveGeometry.Sectors - (PhysicalSector - 1)))
173 NumberOfSectorsToRead = (DriveGeometry.Sectors - (PhysicalSector - 1));
174 else
175 NumberOfSectorsToRead = SectorCount;
176 }
177 else
178 {
179 if (SectorCount >= DriveGeometry.Sectors)
180 NumberOfSectorsToRead = DriveGeometry.Sectors;
181 else
182 NumberOfSectorsToRead = SectorCount;
183 }
184
185 //
186 // Make sure the read is within the geometry boundaries
187 //
188 if ((PhysicalHead >= DriveGeometry.Heads) ||
189 (PhysicalTrack >= DriveGeometry.Cylinders) ||
190 ((NumberOfSectorsToRead + PhysicalSector) > (DriveGeometry.Sectors + 1)) ||
191 (PhysicalSector > DriveGeometry.Sectors))
192 {
193 DiskError("Disk read exceeds drive geometry limits.", 0);
194 return FALSE;
195 }
196
197 // BIOS Int 13h, function 2 - Read Disk Sectors
198 // AH = 02h
199 // AL = number of sectors to read (must be nonzero)
200 // CH = low eight bits of cylinder number
201 // CL = sector number 1-63 (bits 0-5)
202 // high two bits of cylinder (bits 6-7, hard disk only)
203 // DH = head number
204 // DL = drive number (bit 7 set for hard disk)
205 // ES:BX -> data buffer
206 // Return:
207 // CF set on error
208 // if AH = 11h (corrected ECC error), AL = burst length
209 // CF clear if successful
210 // AH = status
211 // AL = number of sectors transferred
212 // (only valid if CF set for some BIOSes)
213 RegsIn.b.ah = 0x02;
214 RegsIn.b.al = (UCHAR)NumberOfSectorsToRead;
215 RegsIn.b.ch = (PhysicalTrack & 0xFF);
216 RegsIn.b.cl = (UCHAR)(PhysicalSector + ((PhysicalTrack & 0x300) >> 2));
217 RegsIn.b.dh = PhysicalHead;
218 RegsIn.b.dl = DriveNumber;
219 RegsIn.w.es = (USHORT)(((ULONG_PTR)Buffer) >> 4);
220 RegsIn.w.bx = ((ULONG_PTR)Buffer) & 0x0F;
221
222 //
223 // Perform the read
224 // Retry 3 times
225 //
226 for (RetryCount=0; RetryCount<3; RetryCount++)
227 {
228 Int386(0x13, &RegsIn, &RegsOut);
229
230 // If it worked break out
231 if (INT386_SUCCESS(RegsOut))
232 {
233 break;
234 }
235 // If it was a corrected ECC error then the data is still good
236 else if (RegsOut.b.ah == 0x11)
237 {
238 break;
239 }
240 // If it failed the do the next retry
241 else
242 {
243 PcDiskResetController(DriveNumber);
244
245 continue;
246 }
247 }
248
249 // If we retried 3 times then fail
250 if (RetryCount >= 3)
251 {
252 ERR("Disk Read Failed in CHS mode, after retrying 3 times: %x\n", RegsOut.b.ah);
253 return FALSE;
254 }
255
256 // I have learned that not all bioses return
257 // the sector read count in the AL register (at least mine doesn't)
258 // even if the sectors were read correctly. So instead
259 // of checking the sector read count we will rely solely
260 // on the carry flag being set on error
261
262 Buffer = (PVOID)((ULONG_PTR)Buffer + (NumberOfSectorsToRead * DriveGeometry.BytesPerSector));
263 SectorCount -= NumberOfSectorsToRead;
264 SectorNumber += NumberOfSectorsToRead;
265 }
266
267 return TRUE;
268 }
269
270 BOOLEAN PcDiskReadLogicalSectors(UCHAR DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
271 {
272 BOOLEAN ExtensionsSupported;
273
274 TRACE("PcDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer);
275
276 //
277 // Check to see if it is a fixed disk drive
278 // If so then check to see if Int13 extensions work
279 // If they do then use them, otherwise default back to BIOS calls
280 //
281 ExtensionsSupported = DiskInt13ExtensionsSupported(DriveNumber);
282
283 if ((DriveNumber >= 0x80) && ExtensionsSupported)
284 {
285 TRACE("Using Int 13 Extensions for read. DiskInt13ExtensionsSupported(%d) = %s\n", DriveNumber, ExtensionsSupported ? "TRUE" : "FALSE");
286
287 //
288 // LBA is easy, nothing to calculate
289 // Just do the read
290 //
291 return PcDiskReadLogicalSectorsLBA(DriveNumber, SectorNumber, SectorCount, Buffer);
292 }
293 else
294 {
295 // LBA is not supported default to the CHS calls
296 return PcDiskReadLogicalSectorsCHS(DriveNumber, SectorNumber, SectorCount, Buffer);
297 }
298
299 return TRUE;
300 }
301
302 BOOLEAN
303 PcDiskGetDriveGeometry(UCHAR DriveNumber, PGEOMETRY Geometry)
304 {
305 EXTENDED_GEOMETRY ExtGeometry;
306 REGS RegsIn;
307 REGS RegsOut;
308 ULONG Cylinders;
309
310 TRACE("DiskGetDriveGeometry()\n");
311
312 /* Try to get the extended geometry first */
313 ExtGeometry.Size = sizeof(EXTENDED_GEOMETRY);
314 if (DiskGetExtendedDriveParameters(DriveNumber, &ExtGeometry, ExtGeometry.Size))
315 {
316 Geometry->Cylinders = ExtGeometry.Cylinders;
317 Geometry->Heads = ExtGeometry.Heads;
318 Geometry->Sectors = ExtGeometry.SectorsPerTrack;
319 Geometry->BytesPerSector = ExtGeometry.BytesPerSector;
320
321 return TRUE;
322 }
323
324 /* BIOS Int 13h, function 08h - Get drive parameters
325 * AH = 08h
326 * DL = drive (bit 7 set for hard disk)
327 * ES:DI = 0000h:0000h to guard against BIOS bugs
328 * Return:
329 * CF set on error
330 * AH = status (07h)
331 * CF clear if successful
332 * AH = 00h
333 * AL = 00h on at least some BIOSes
334 * BL = drive type (AT/PS2 floppies only)
335 * CH = low eight bits of maximum cylinder number
336 * CL = maximum sector number (bits 5-0)
337 * high two bits of maximum cylinder number (bits 7-6)
338 * DH = maximum head number
339 * DL = number of drives
340 * ES:DI -> drive parameter table (floppies only)
341 */
342 RegsIn.b.ah = 0x08;
343 RegsIn.b.dl = DriveNumber;
344 RegsIn.w.es = 0x0000;
345 RegsIn.w.di = 0x0000;
346
347 /* Get drive parameters */
348 Int386(0x13, &RegsIn, &RegsOut);
349
350 if (! INT386_SUCCESS(RegsOut))
351 {
352 return FALSE;
353 }
354
355 Cylinders = (RegsOut.b.cl & 0xC0) << 2;
356 Cylinders += RegsOut.b.ch;
357 Cylinders++;
358 Geometry->Cylinders = Cylinders;
359 Geometry->Heads = RegsOut.b.dh + 1;
360 Geometry->Sectors = RegsOut.b.cl & 0x3F;
361 Geometry->BytesPerSector = 512; /* Just assume 512 bytes per sector */
362
363 return TRUE;
364 }
365
366 ULONG
367 PcDiskGetCacheableBlockCount(UCHAR DriveNumber)
368 {
369 GEOMETRY Geometry;
370
371 /* If LBA is supported then the block size will be 64 sectors (32k)
372 * If not then the block size is the size of one track */
373 if (DiskInt13ExtensionsSupported(DriveNumber))
374 {
375 return 64;
376 }
377 /* Get the disk geometry
378 * If this fails then we will just return 1 sector to be safe */
379 else if (! PcDiskGetDriveGeometry(DriveNumber, &Geometry))
380 {
381 return 1;
382 }
383 else
384 {
385 return Geometry.Sectors;
386 }
387 }
388
389 BOOLEAN
390 PcDiskGetBootPath(char *BootPath, unsigned Size)
391 {
392 if (PxeInit())
393 {
394 strcpy(BootPath, "net(0)");
395 return 0;
396 }
397 return DiskGetBootPath(BootPath, Size);
398 }
399
400 /* EOF */