Skip to content

Commit 9b8c59d

Browse files
committed
[FREELDR] Refactor LoadBootDeviceDriver
1 parent 393b1f8 commit 9b8c59d

File tree

1 file changed

+6
-74
lines changed

1 file changed

+6
-74
lines changed

boot/freeldr/freeldr/disk/scsiport.c

Lines changed: 6 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,11 +1634,7 @@ extern char __ImageBase;
16341634
ULONG
16351635
LoadBootDeviceDriver(VOID)
16361636
{
1637-
PIMAGE_NT_HEADERS NtHeaders;
1638-
LIST_ENTRY ModuleListHead;
1639-
PIMAGE_IMPORT_DESCRIPTOR ImportTable;
1640-
ULONG ImportTableSize;
1641-
PLDR_DATA_TABLE_ENTRY BootDdDTE, FreeldrDTE;
1637+
PLDR_DATA_TABLE_ENTRY BootDdDTE;
16421638
CHAR NtBootDdPath[MAX_PATH];
16431639
PVOID ImageBase = NULL;
16441640
ULONG (NTAPI *EntryPoint)(IN PVOID DriverObject, IN PVOID RegistryPath);
@@ -1650,85 +1646,21 @@ LoadBootDeviceDriver(VOID)
16501646
HalpInitBusHandler();
16511647
#endif
16521648

1653-
/* Initialize the loaded module list */
1654-
InitializeListHead(&ModuleListHead);
1655-
16561649
/* Create full ntbootdd.sys path */
16571650
strcpy(NtBootDdPath, FrLdrBootPath);
16581651
strcat(NtBootDdPath, "\\NTBOOTDD.SYS");
16591652

1660-
/* Load file */
1661-
Success = PeLdrLoadImage(NtBootDdPath, LoaderBootDriver, &ImageBase);
1653+
/* Load ntbootdd.sys */
1654+
Success = PeLdrLoadBootImage(NtBootDdPath,
1655+
"ntbootdd.sys",
1656+
ImageBase,
1657+
&BootDdDTE);
16621658
if (!Success)
16631659
{
16641660
/* That's OK, file simply doesn't exist */
16651661
return ESUCCESS;
16661662
}
16671663

1668-
/* Allocate a DTE for ntbootdd */
1669-
Success = PeLdrAllocateDataTableEntry(&ModuleListHead, "ntbootdd.sys",
1670-
"NTBOOTDD.SYS", ImageBase, &BootDdDTE);
1671-
if (!Success)
1672-
{
1673-
/* Cleanup and bail out */
1674-
MmFreeMemory(ImageBase);
1675-
return EIO;
1676-
}
1677-
1678-
/* Add the PE part of freeldr.sys to the list of loaded executables, it
1679-
contains ScsiPort* exports, imported by ntbootdd.sys */
1680-
Success = PeLdrAllocateDataTableEntry(&ModuleListHead, "scsiport.sys",
1681-
"FREELDR.SYS", &__ImageBase, &FreeldrDTE);
1682-
if (!Success)
1683-
{
1684-
/* Cleanup and bail out */
1685-
PeLdrFreeDataTableEntry(BootDdDTE);
1686-
MmFreeMemory(ImageBase);
1687-
return EIO;
1688-
}
1689-
1690-
/* Fix imports */
1691-
Success = PeLdrScanImportDescriptorTable(&ModuleListHead, "", BootDdDTE);
1692-
if (!Success)
1693-
{
1694-
/* Cleanup and bail out */
1695-
PeLdrFreeDataTableEntry(FreeldrDTE);
1696-
PeLdrFreeDataTableEntry(BootDdDTE);
1697-
MmFreeMemory(ImageBase);
1698-
return EIO;
1699-
}
1700-
1701-
/* Now unlink the DTEs, they won't be valid later */
1702-
RemoveEntryList(&BootDdDTE->InLoadOrderLinks);
1703-
RemoveEntryList(&FreeldrDTE->InLoadOrderLinks);
1704-
1705-
/* Change imports to PA */
1706-
ImportTable = (PIMAGE_IMPORT_DESCRIPTOR)RtlImageDirectoryEntryToData(VaToPa(BootDdDTE->DllBase),
1707-
TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &ImportTableSize);
1708-
for (;(ImportTable->Name != 0) && (ImportTable->FirstThunk != 0);ImportTable++)
1709-
{
1710-
PIMAGE_THUNK_DATA ThunkData = (PIMAGE_THUNK_DATA)VaToPa(RVA(BootDdDTE->DllBase, ImportTable->FirstThunk));
1711-
1712-
while (((PIMAGE_THUNK_DATA)ThunkData)->u1.AddressOfData != 0)
1713-
{
1714-
ThunkData->u1.Function = (ULONG_PTR)VaToPa((PVOID)ThunkData->u1.Function);
1715-
ThunkData++;
1716-
}
1717-
}
1718-
1719-
/* Relocate image to PA */
1720-
NtHeaders = RtlImageNtHeader(VaToPa(BootDdDTE->DllBase));
1721-
if (!NtHeaders)
1722-
return EIO;
1723-
Success = (BOOLEAN)LdrRelocateImageWithBias(VaToPa(BootDdDTE->DllBase),
1724-
NtHeaders->OptionalHeader.ImageBase - (ULONG_PTR)BootDdDTE->DllBase,
1725-
"FreeLdr",
1726-
TRUE,
1727-
TRUE, /* In case of conflict still return success */
1728-
FALSE);
1729-
if (!Success)
1730-
return EIO;
1731-
17321664
/* Call the entrypoint */
17331665
EntryPoint = VaToPa(BootDdDTE->EntryPoint);
17341666
(*EntryPoint)(NULL, NULL);

0 commit comments

Comments
 (0)