Skip to content

Commit ff4a5fa

Browse files
committed
[FREELDR] Improve disk detection
1 parent 81edd1f commit ff4a5fa

File tree

1 file changed

+63
-46
lines changed

1 file changed

+63
-46
lines changed

boot/freeldr/freeldr/arch/uefi/uefidisk.c

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ static ULONG OffsetToBoot;
5858
static ULONG PublicBootArcDisk;
5959
static INTERNAL_UEFI_DISK* InternalUefiDisk = NULL;
6060
static EFI_GUID bioGuid = BLOCK_IO_PROTOCOL;
61+
static EFI_GUID loadedImageGuid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
6162
static EFI_BLOCK_IO* bio;
63+
static EFI_LOADED_IMAGE_PROTOCOL *loadedImage;
6264
static EFI_HANDLE* handles = NULL;
6365

6466
/* FUNCTIONS *****************************************************************/
@@ -434,28 +436,41 @@ UefiSetupBlockDevices(VOID)
434436
ULONG increment = 0;
435437
ULONG i;
436438

437-
/* 3) Grab the offset into the array of handles and decrement per volume (valid partition) */
438-
for (increment = OffsetToBoot; increment > 0; increment--)
439+
Status = GlobalSystemTable->BootServices->HandleProtocol(
440+
GlobalImageHandle,
441+
&loadedImageGuid,
442+
(void**)&loadedImage
443+
);
444+
if (EFI_ERROR(Status))
445+
continue;
446+
447+
for (increment = 0; increment < SystemHandleCount; ++increment) {
448+
if (handles[increment] == loadedImage->DeviceHandle) {
449+
break;
450+
}
451+
}
452+
453+
if (SystemHandleCount == increment)
454+
continue;
455+
456+
GlobalSystemTable->BootServices->HandleProtocol(handles[increment], &bioGuid, (void**)&bio);
457+
if (bio->Media->LogicalPartition == FALSE)
439458
{
440-
GlobalSystemTable->BootServices->HandleProtocol(handles[increment], &bioGuid, (void**)&bio);
441-
if (bio->Media->LogicalPartition == FALSE)
442-
{
443-
TRACE("Found root at increment %u\n", increment);
444-
UefiBootRootIdentifier = increment;
459+
TRACE("Found root at increment %u\n", increment);
460+
UefiBootRootIdentifier = increment;
445461

446-
for (i = 0; i <= PcBiosDiskCount; ++i)
462+
for (i = 0; i <= PcBiosDiskCount; ++i)
463+
{
464+
/* Now only of the root drive number is equal to this drive we found above */
465+
if (InternalUefiDisk[i].UefiRootNumber == UefiBootRootIdentifier)
447466
{
448-
/* Now only of the root drive number is equal to this drive we found above */
449-
if (InternalUefiDisk[i].UefiRootNumber == UefiBootRootIdentifier)
450-
{
451-
InternalUefiDisk[i].IsThisTheBootDrive = TRUE;
452-
PublicBootArcDisk = i;
453-
TRACE("Found Boot drive\n");
454-
}
467+
InternalUefiDisk[i].IsThisTheBootDrive = TRUE;
468+
PublicBootArcDisk = i;
469+
TRACE("Found Boot drive\n");
455470
}
456-
457-
break;
458471
}
472+
473+
break;
459474
}
460475
}
461476
}
@@ -499,49 +514,51 @@ BOOLEAN
499514
UefiInitializeBootDevices(VOID)
500515
{
501516
ULONG i = 0;
517+
PMASTER_BOOT_RECORD Mbr;
518+
PULONG Buffer;
519+
ULONG ChecksumSize = 512;
520+
ULONG Checksum = 0;
521+
ULONG Signature;
502522

503523
DiskReadBufferSize = EFI_PAGE_SIZE;
504524
DiskReadBuffer = MmAllocateMemoryWithType(DiskReadBufferSize, LoaderFirmwareTemporary);
505525
UefiSetupBlockDevices();
506526
UefiSetBootpath();
507527

508-
/* Add it, if it's a cdrom */
528+
/* Add it */
509529
GlobalSystemTable->BootServices->HandleProtocol(handles[UefiBootRootIdentifier], &bioGuid, (void**)&bio);
510-
if (bio->Media->RemovableMedia == TRUE && bio->Media->BlockSize == 2048)
530+
531+
/* Read the MBR */
532+
if (!MachDiskReadLogicalSectors(FrldrBootDrive, 16ULL, 1, DiskReadBuffer))
511533
{
512-
PMASTER_BOOT_RECORD Mbr;
513-
PULONG Buffer;
514-
ULONG Checksum = 0;
515-
ULONG Signature;
534+
ERR("Reading MBR failed\n");
535+
return FALSE;
536+
}
516537

517-
/* Read the MBR */
518-
if (!MachDiskReadLogicalSectors(FrldrBootDrive, 16ULL, 1, DiskReadBuffer))
519-
{
520-
ERR("Reading MBR failed\n");
521-
return FALSE;
522-
}
538+
Buffer = (ULONG*)DiskReadBuffer;
539+
Mbr = (PMASTER_BOOT_RECORD)DiskReadBuffer;
523540

524-
Buffer = (ULONG*)DiskReadBuffer;
525-
Mbr = (PMASTER_BOOT_RECORD)DiskReadBuffer;
541+
Signature = Mbr->Signature;
542+
TRACE("Signature: %x\n", Signature);
526543

527-
Signature = Mbr->Signature;
528-
TRACE("Signature: %x\n", Signature);
544+
if (bio->Media->BlockSize == 2048)
545+
ChecksumSize = bio->Media->BlockSize;
529546

530-
/* Calculate the MBR checksum */
531-
for (i = 0; i < 2048 / sizeof(ULONG); i++)
532-
{
533-
Checksum += Buffer[i];
534-
}
535-
Checksum = ~Checksum + 1;
536-
TRACE("Checksum: %x\n", Checksum);
547+
/* Calculate the MBR checksum */
548+
for (i = 0; i < ChecksumSize / sizeof(ULONG); i++)
549+
{
550+
Checksum += Buffer[i];
551+
}
552+
Checksum = ~Checksum + 1;
553+
TRACE("Checksum: %x\n", Checksum);
554+
555+
/* Fill out the ARC disk block */
556+
AddReactOSArcDiskInfo(FrLdrBootPath, Signature, Checksum, TRUE);
537557

538-
/* Fill out the ARC disk block */
539-
AddReactOSArcDiskInfo(FrLdrBootPath, Signature, Checksum, TRUE);
558+
FsRegisterDevice(FrLdrBootPath, &UefiDiskVtbl);
559+
PcBiosDiskCount++; // This is not accounted for in the number of pre-enumerated BIOS drives!
560+
TRACE("Additional boot drive detected: 0x%02X\n", (int)FrldrBootDrive);
540561

541-
FsRegisterDevice(FrLdrBootPath, &UefiDiskVtbl);
542-
PcBiosDiskCount++; // This is not accounted for in the number of pre-enumerated BIOS drives!
543-
TRACE("Additional boot drive detected: 0x%02X\n", (int)FrldrBootDrive);
544-
}
545562
return TRUE;
546563
}
547564

0 commit comments

Comments
 (0)