[FREELDR] Other enhancements.
[reactos.git] / boot / freeldr / freeldr / arch / i386 / hwdisk.c
1 /*
2 * FreeLoader
3 *
4 * Copyright (C) 2003, 2004 Eric Kohl
5 * Copyright (C) 2009 Hervé Poussineau
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include <freeldr.h>
23
24 #include <debug.h>
25 DBG_DEFAULT_CHANNEL(HWDETECT);
26
27 /*
28 * This is the common code for harddisk for both the PC and the XBOX.
29 */
30
31 typedef struct tagDISKCONTEXT
32 {
33 UCHAR DriveNumber;
34 ULONG SectorSize;
35 ULONGLONG SectorOffset;
36 ULONGLONG SectorCount;
37 ULONGLONG SectorNumber;
38 } DISKCONTEXT;
39
40 static const CHAR Hex[] = "0123456789abcdef";
41
42 /* Data cache for BIOS disks pre-enumeration */
43 UCHAR PcBiosDiskCount = 0;
44 static CHAR PcDiskIdentifier[32][20];
45
46 PVOID DiskReadBuffer;
47 SIZE_T DiskReadBufferSize;
48
49
50 /* FUNCTIONS *****************************************************************/
51
52 static ARC_STATUS
53 DiskClose(ULONG FileId)
54 {
55 DISKCONTEXT* Context = FsGetDeviceSpecific(FileId);
56 FrLdrTempFree(Context, TAG_HW_DISK_CONTEXT);
57 return ESUCCESS;
58 }
59
60 static ARC_STATUS
61 DiskGetFileInformation(ULONG FileId, FILEINFORMATION* Information)
62 {
63 DISKCONTEXT* Context = FsGetDeviceSpecific(FileId);
64
65 RtlZeroMemory(Information, sizeof(*Information));
66 Information->EndingAddress.QuadPart = Context->SectorCount * Context->SectorSize;
67 Information->CurrentAddress.QuadPart = Context->SectorNumber * Context->SectorSize;
68
69 return ESUCCESS;
70 }
71
72 static ARC_STATUS
73 DiskOpen(CHAR* Path, OPENMODE OpenMode, ULONG* FileId)
74 {
75 DISKCONTEXT* Context;
76 UCHAR DriveNumber;
77 ULONG DrivePartition, SectorSize;
78 ULONGLONG SectorOffset = 0;
79 ULONGLONG SectorCount = 0;
80 PARTITION_TABLE_ENTRY PartitionTableEntry;
81
82 if (DiskReadBufferSize == 0)
83 {
84 ERR("DiskOpen(): DiskReadBufferSize is 0, something is wrong.\n");
85 ASSERT(FALSE);
86 return ENOMEM;
87 }
88
89 if (!DissectArcPath(Path, NULL, &DriveNumber, &DrivePartition))
90 return EINVAL;
91
92 if (DrivePartition == 0xff)
93 {
94 /* This is a CD-ROM device */
95 SectorSize = 2048;
96 }
97 else
98 {
99 /*
100 * This is either a floppy disk device (DrivePartition == 0) or
101 * a hard disk device (DrivePartition != 0 && DrivePartition != 0xFF)
102 * but it doesn't matter which one because they both have 512 bytes
103 * per sector.
104 */
105 SectorSize = 512;
106 }
107
108 if (DrivePartition != 0xff && DrivePartition != 0)
109 {
110 if (!DiskGetPartitionEntry(DriveNumber, DrivePartition, &PartitionTableEntry))
111 return EINVAL;
112
113 SectorOffset = PartitionTableEntry.SectorCountBeforePartition;
114 SectorCount = PartitionTableEntry.PartitionSectorCount;
115 }
116 #if 0 // FIXME: Investigate
117 else
118 {
119 SectorCount = 0; /* FIXME */
120 }
121 #endif
122
123 Context = FrLdrTempAlloc(sizeof(DISKCONTEXT), TAG_HW_DISK_CONTEXT);
124 if (!Context)
125 return ENOMEM;
126
127 Context->DriveNumber = DriveNumber;
128 Context->SectorSize = SectorSize;
129 Context->SectorOffset = SectorOffset;
130 Context->SectorCount = SectorCount;
131 Context->SectorNumber = 0;
132 FsSetDeviceSpecific(*FileId, Context);
133
134 return ESUCCESS;
135 }
136
137 static ARC_STATUS
138 DiskRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
139 {
140 DISKCONTEXT* Context = FsGetDeviceSpecific(FileId);
141 UCHAR* Ptr = (UCHAR*)Buffer;
142 ULONG Length, TotalSectors, MaxSectors, ReadSectors;
143 BOOLEAN ret;
144 ULONGLONG SectorOffset;
145
146 ASSERT(DiskReadBufferSize > 0);
147
148 TotalSectors = (N + Context->SectorSize - 1) / Context->SectorSize;
149 MaxSectors = DiskReadBufferSize / Context->SectorSize;
150 SectorOffset = Context->SectorNumber + Context->SectorOffset;
151
152 // If MaxSectors is 0, this will lead to infinite loop.
153 // In release builds assertions are disabled, however we also have sanity checks in DiskOpen()
154 ASSERT(MaxSectors > 0);
155
156 ret = TRUE;
157
158 while (TotalSectors)
159 {
160 ReadSectors = TotalSectors;
161 if (ReadSectors > MaxSectors)
162 ReadSectors = MaxSectors;
163
164 ret = MachDiskReadLogicalSectors(Context->DriveNumber,
165 SectorOffset,
166 ReadSectors,
167 DiskReadBuffer);
168 if (!ret)
169 break;
170
171 Length = ReadSectors * Context->SectorSize;
172 if (Length > N)
173 Length = N;
174
175 RtlCopyMemory(Ptr, DiskReadBuffer, Length);
176
177 Ptr += Length;
178 N -= Length;
179 SectorOffset += ReadSectors;
180 TotalSectors -= ReadSectors;
181 }
182
183 *Count = (ULONG)(Ptr - (UCHAR*)Buffer);
184
185 return (!ret) ? EIO : ESUCCESS;
186 }
187
188 static ARC_STATUS
189 DiskSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMode)
190 {
191 DISKCONTEXT* Context = FsGetDeviceSpecific(FileId);
192
193 if (SeekMode != SeekAbsolute)
194 return EINVAL;
195 if (Position->LowPart & (Context->SectorSize - 1))
196 return EINVAL;
197
198 Context->SectorNumber = Position->QuadPart / Context->SectorSize;
199 return ESUCCESS;
200 }
201
202 static const DEVVTBL DiskVtbl =
203 {
204 DiskClose,
205 DiskGetFileInformation,
206 DiskOpen,
207 DiskRead,
208 DiskSeek,
209 };
210
211
212 PCHAR
213 GetHarddiskIdentifier(UCHAR DriveNumber)
214 {
215 return PcDiskIdentifier[DriveNumber - 0x80];
216 }
217
218 static VOID
219 GetHarddiskInformation(UCHAR DriveNumber)
220 {
221 PMASTER_BOOT_RECORD Mbr;
222 PULONG Buffer;
223 ULONG i;
224 ULONG Checksum;
225 ULONG Signature;
226 BOOLEAN ValidPartitionTable;
227 CHAR ArcName[MAX_PATH];
228 PARTITION_TABLE_ENTRY PartitionTableEntry;
229 PCHAR Identifier = PcDiskIdentifier[DriveNumber - 0x80];
230
231 /* Detect disk partition type */
232 DiskDetectPartitionType(DriveNumber);
233
234 /* Read the MBR */
235 if (!MachDiskReadLogicalSectors(DriveNumber, 0ULL, 1, DiskReadBuffer))
236 {
237 ERR("Reading MBR failed\n");
238 /* We failed, use a default identifier */
239 sprintf(Identifier, "BIOSDISK%d", DriveNumber - 0x80 + 1);
240 return;
241 }
242
243 Buffer = (ULONG*)DiskReadBuffer;
244 Mbr = (PMASTER_BOOT_RECORD)DiskReadBuffer;
245
246 Signature = Mbr->Signature;
247 TRACE("Signature: %x\n", Signature);
248
249 /* Calculate the MBR checksum */
250 Checksum = 0;
251 for (i = 0; i < 512 / sizeof(ULONG); i++)
252 {
253 Checksum += Buffer[i];
254 }
255 Checksum = ~Checksum + 1;
256 TRACE("Checksum: %x\n", Checksum);
257
258 ValidPartitionTable = (Mbr->MasterBootRecordMagic == 0xAA55);
259
260 /* Fill out the ARC disk block */
261 sprintf(ArcName, "multi(0)disk(0)rdisk(%u)", DriveNumber - 0x80);
262 AddReactOSArcDiskInfo(ArcName, Signature, Checksum, ValidPartitionTable);
263
264 sprintf(ArcName, "multi(0)disk(0)rdisk(%u)partition(0)", DriveNumber - 0x80);
265 FsRegisterDevice(ArcName, &DiskVtbl);
266
267 /* Add partitions */
268 i = 1;
269 DiskReportError(FALSE);
270 while (DiskGetPartitionEntry(DriveNumber, i, &PartitionTableEntry))
271 {
272 if (PartitionTableEntry.SystemIndicator != PARTITION_ENTRY_UNUSED)
273 {
274 sprintf(ArcName, "multi(0)disk(0)rdisk(%u)partition(%lu)", DriveNumber - 0x80, i);
275 FsRegisterDevice(ArcName, &DiskVtbl);
276 }
277 i++;
278 }
279 DiskReportError(TRUE);
280
281 /* Convert checksum and signature to identifier string */
282 Identifier[0] = Hex[(Checksum >> 28) & 0x0F];
283 Identifier[1] = Hex[(Checksum >> 24) & 0x0F];
284 Identifier[2] = Hex[(Checksum >> 20) & 0x0F];
285 Identifier[3] = Hex[(Checksum >> 16) & 0x0F];
286 Identifier[4] = Hex[(Checksum >> 12) & 0x0F];
287 Identifier[5] = Hex[(Checksum >> 8) & 0x0F];
288 Identifier[6] = Hex[(Checksum >> 4) & 0x0F];
289 Identifier[7] = Hex[Checksum & 0x0F];
290 Identifier[8] = '-';
291 Identifier[9] = Hex[(Signature >> 28) & 0x0F];
292 Identifier[10] = Hex[(Signature >> 24) & 0x0F];
293 Identifier[11] = Hex[(Signature >> 20) & 0x0F];
294 Identifier[12] = Hex[(Signature >> 16) & 0x0F];
295 Identifier[13] = Hex[(Signature >> 12) & 0x0F];
296 Identifier[14] = Hex[(Signature >> 8) & 0x0F];
297 Identifier[15] = Hex[(Signature >> 4) & 0x0F];
298 Identifier[16] = Hex[Signature & 0x0F];
299 Identifier[17] = '-';
300 Identifier[18] = (ValidPartitionTable ? 'A' : 'X');
301 Identifier[19] = 0;
302 TRACE("Identifier: %s\n", Identifier);
303 }
304
305 static UCHAR
306 EnumerateHarddisks(OUT PBOOLEAN BootDriveReported)
307 {
308 UCHAR DiskCount, DriveNumber;
309 ULONG i;
310 BOOLEAN Changed;
311
312 *BootDriveReported = FALSE;
313
314 /* Count the number of visible harddisk drives */
315 DiskReportError(FALSE);
316 DiskCount = 0;
317 DriveNumber = 0x80;
318
319 ASSERT(DiskReadBufferSize > 0);
320
321 /*
322 * There are some really broken BIOSes out there. There are even BIOSes
323 * that happily report success when you ask them to read from non-existent
324 * harddisks. So, we set the buffer to known contents first, then try to
325 * read. If the BIOS reports success but the buffer contents haven't
326 * changed then we fail anyway.
327 */
328 memset(DiskReadBuffer, 0xcd, DiskReadBufferSize);
329 while (MachDiskReadLogicalSectors(DriveNumber, 0ULL, 1, DiskReadBuffer))
330 {
331 Changed = FALSE;
332 for (i = 0; !Changed && i < DiskReadBufferSize; i++)
333 {
334 Changed = ((PUCHAR)DiskReadBuffer)[i] != 0xcd;
335 }
336 if (!Changed)
337 {
338 TRACE("BIOS reports success for disk %d (0x%02X) but data didn't change\n",
339 (int)DiskCount, DriveNumber);
340 break;
341 }
342
343 /* Cache the BIOS hard disk information for later use */
344 GetHarddiskInformation(DriveNumber);
345
346 /* Check if we have seen the boot drive */
347 if (FrldrBootDrive == DriveNumber)
348 *BootDriveReported = TRUE;
349
350 DiskCount++;
351 DriveNumber++;
352 memset(DiskReadBuffer, 0xcd, DiskReadBufferSize);
353 }
354 DiskReportError(TRUE);
355
356 PcBiosDiskCount = DiskCount;
357 TRACE("BIOS reports %d harddisk%s\n",
358 (int)DiskCount, (DiskCount == 1) ? "" : "s");
359
360 return DiskCount;
361 }
362
363 static BOOLEAN
364 DiskIsDriveRemovable(UCHAR DriveNumber)
365 {
366 /*
367 * Hard disks use drive numbers >= 0x80 . So if the drive number
368 * indicates a hard disk then return FALSE.
369 * 0x49 is our magic ramdisk drive, so return FALSE for that too.
370 */
371 if ((DriveNumber >= 0x80) || (DriveNumber == 0x49))
372 return FALSE;
373
374 /* The drive is a floppy diskette so return TRUE */
375 return TRUE;
376 }
377
378 static BOOLEAN
379 DiskGetBootPath(BOOLEAN IsPxe)
380 {
381 if (*FrldrBootPath)
382 return TRUE;
383
384 // FIXME! FIXME! Do this in some drive recognition procedure!!!!
385 if (IsPxe)
386 {
387 RtlStringCbCopyA(FrldrBootPath, sizeof(FrldrBootPath), "net(0)");
388 }
389 else
390 /* 0x49 is our magic ramdisk drive, so try to detect it first */
391 if (FrldrBootDrive == 0x49)
392 {
393 /* This is the ramdisk. See ArmInitializeBootDevices() too... */
394 // RtlStringCbPrintfA(FrldrBootPath, sizeof(FrldrBootPath), "ramdisk(%u)", 0);
395 RtlStringCbCopyA(FrldrBootPath, sizeof(FrldrBootPath), "ramdisk(0)");
396 }
397 else if (FrldrBootDrive < 0x80)
398 {
399 /* This is a floppy */
400 RtlStringCbPrintfA(FrldrBootPath, sizeof(FrldrBootPath),
401 "multi(0)disk(0)fdisk(%u)", FrldrBootDrive);
402 }
403 else if (FrldrBootPartition == 0xFF)
404 {
405 /* Boot Partition 0xFF is the magic value that indicates booting from CD-ROM (see isoboot.S) */
406 RtlStringCbPrintfA(FrldrBootPath, sizeof(FrldrBootPath),
407 "multi(0)disk(0)cdrom(%u)", FrldrBootDrive - 0x80);
408 }
409 else
410 {
411 ULONG BootPartition;
412 PARTITION_TABLE_ENTRY PartitionEntry;
413
414 /* This is a hard disk */
415 if (!DiskGetBootPartitionEntry(FrldrBootDrive, &PartitionEntry, &BootPartition))
416 {
417 ERR("Failed to get boot partition entry\n");
418 return FALSE;
419 }
420
421 FrldrBootPartition = BootPartition;
422
423 RtlStringCbPrintfA(FrldrBootPath, sizeof(FrldrBootPath),
424 "multi(0)disk(0)rdisk(%u)partition(%lu)",
425 FrldrBootDrive - 0x80, FrldrBootPartition);
426 }
427
428 return TRUE;
429 }
430
431 BOOLEAN
432 PcInitializeBootDevices(VOID)
433 {
434 UCHAR DiskCount;
435 BOOLEAN BootDriveReported = FALSE;
436 ULONG i;
437
438 DiskCount = EnumerateHarddisks(&BootDriveReported);
439
440 /* Initialize FrldrBootPath, the boot path we're booting from (the "SystemPartition") */
441 DiskGetBootPath(PxeInit());
442
443 /* Add it, if it's a floppy or cdrom */
444 if ((FrldrBootDrive >= 0x80 && !BootDriveReported) ||
445 DiskIsDriveRemovable(FrldrBootDrive))
446 {
447 /* TODO: Check if it's really a CDROM drive */
448
449 PMASTER_BOOT_RECORD Mbr;
450 PULONG Buffer;
451 ULONG Checksum = 0;
452 ULONG Signature;
453
454 /* Read the MBR */
455 if (!MachDiskReadLogicalSectors(FrldrBootDrive, 16ULL, 1, DiskReadBuffer))
456 {
457 ERR("Reading MBR failed\n");
458 return FALSE;
459 }
460
461 Buffer = (ULONG*)DiskReadBuffer;
462 Mbr = (PMASTER_BOOT_RECORD)DiskReadBuffer;
463
464 Signature = Mbr->Signature;
465 TRACE("Signature: %x\n", Signature);
466
467 /* Calculate the MBR checksum */
468 for (i = 0; i < 2048 / sizeof(ULONG); i++)
469 {
470 Checksum += Buffer[i];
471 }
472 Checksum = ~Checksum + 1;
473 TRACE("Checksum: %x\n", Checksum);
474
475 /* Fill out the ARC disk block */
476 AddReactOSArcDiskInfo(FrldrBootPath, Signature, Checksum, TRUE);
477
478 FsRegisterDevice(FrldrBootPath, &DiskVtbl);
479 DiskCount++; // This is not accounted for in the number of pre-enumerated BIOS drives!
480 TRACE("Additional boot drive detected: 0x%02X\n", (int)FrldrBootDrive);
481 }
482
483 return (DiskCount != 0);
484 }