Skip to content

Commit bb7747d

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

File tree

1 file changed

+63
-47
lines changed

1 file changed

+63
-47
lines changed

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

Lines changed: 63 additions & 47 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 *****************************************************************/
@@ -432,30 +434,42 @@ UefiSetupBlockDevices(VOID)
432434
else if (handles[i] == PublicBootHandle)
433435
{
434436
ULONG increment = 0;
435-
ULONG i;
436437

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

446-
for (i = 0; i <= PcBiosDiskCount; ++i)
461+
for (i = 0; i <= PcBiosDiskCount; ++i)
462+
{
463+
/* Now only of the root drive number is equal to this drive we found above */
464+
if (InternalUefiDisk[i].UefiRootNumber == UefiBootRootIdentifier)
447465
{
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-
}
466+
InternalUefiDisk[i].IsThisTheBootDrive = TRUE;
467+
PublicBootArcDisk = i;
468+
TRACE("Found Boot drive\n");
455469
}
456-
457-
break;
458470
}
471+
472+
break;
459473
}
460474
}
461475
}
@@ -499,49 +513,51 @@ BOOLEAN
499513
UefiInitializeBootDevices(VOID)
500514
{
501515
ULONG i = 0;
516+
PMASTER_BOOT_RECORD Mbr;
517+
PULONG Buffer;
518+
ULONG ChecksumSize = 512;
519+
ULONG Checksum = 0;
520+
ULONG Signature;
502521

503522
DiskReadBufferSize = EFI_PAGE_SIZE;
504523
DiskReadBuffer = MmAllocateMemoryWithType(DiskReadBufferSize, LoaderFirmwareTemporary);
505524
UefiSetupBlockDevices();
506525
UefiSetBootpath();
507526

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

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

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

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

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);
546+
/* Calculate the MBR checksum */
547+
for (i = 0; i < ChecksumSize / sizeof(ULONG); i++)
548+
{
549+
Checksum += Buffer[i];
550+
}
551+
Checksum = ~Checksum + 1;
552+
TRACE("Checksum: %x\n", Checksum);
553+
554+
/* Fill out the ARC disk block */
555+
AddReactOSArcDiskInfo(FrLdrBootPath, Signature, Checksum, TRUE);
537556

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

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-
}
545561
return TRUE;
546562
}
547563

0 commit comments

Comments
 (0)