Skip to content

Commit

Permalink
add windows loader mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamesits committed Apr 23, 2020
1 parent af60471 commit 3b75b28
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@
#include "effy/src/nstdlib.h"
#include "effy/src/xsdt.h"
#include "effy/src/acpi_checksum.h"
#include "effy/src/dirtool.h"
#include "effy/src/chainload.h"

// #define LOAD_WINDOWS

// loads an efi executable if not a DEBUG build;
// otherwise try to read the file and verify if it is an efi executable, then quit
EFI_STATUS load_efi_image(DIRTOOL_FILE* file, EFI_HANDLE ImageHandle)
{
#if defined(_DEBUG)
Print(L"ChainloadByDevicePath %s ignored in debug build\n", file->Path);
CHAR8* buf = dirtool_read_file(file);
Print(L"buf: %c%c\n", buf[0], buf[1]); // should print "MZ"
return EFI_SUCCESS;
#else
ClearInputBuf();
EFI_STATUS ret = ChainloadByDevicePath(file->DevicePath, ImageHandle);
return ret;
#endif
}

// Application entrypoint (must be set to 'efi_main' for gnu-efi crt0 compatibility)
EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable)
Expand Down Expand Up @@ -119,6 +139,54 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable)
Print(L"%EdropWPBT done%N\n\n");
#endif

#if defined(LOAD_WINDOWS)
// directly load Windows
Print(L"%HSearching Microsoft bootloader...%N\n");
CHAR16 MSBootloaderPath1[] = L"EFI\\Microsoft\\Boot\\bootmgfw.efi";
DIRTOOL_STATE DirToolState;
DirToolState.initialized = 0;
// Print(L"dirtool_init\n");
EFI_STATUS status = dirtool_init(&DirToolState, ImageHandle);
if (EFI_ERROR(status)) {
return status;
}

// first try current disk
DIRTOOL_DRIVE* drive;
drive = dirtool_get_current_drive(&DirToolState, 0);
dirtool_open_drive(&DirToolState, drive);
DIRTOOL_FILE* pwd = dirtool_cd_multi(&(drive->RootFile), MSBootloaderPath1);
if (pwd) load_efi_image(pwd, ImageHandle);
dirtool_close_drive(&DirToolState, drive);

if (!pwd) {
Print(L"%Ebootmgfw.efi not found on current ESP partition, searching all disks%N\n");

#if defined(_DEBUG)
pause();
#endif

// then try to search every readable disk for bootmgfw.efi
DIRTOOL_DRIVE_ITERATOR* iterator = dirtool_drive_iterator_start(&DirToolState);
while ((drive = dirtool_drive_iterator_next(&DirToolState, iterator)) != NULL)
{
DIRTOOL_FILE* pwd = dirtool_open_drive(&DirToolState, drive);
if (pwd == NULL) continue;
pwd = dirtool_cd_multi(pwd, MSBootloaderPath1);
if (pwd) load_efi_image(pwd, ImageHandle);
#if defined(_DEBUG)
Print(L"before dirtool_close_drive()\n");
pause();
#endif
dirtool_close_drive(&DirToolState, drive);
}
dirtool_drive_iterator_end(&DirToolState, iterator);
iterator = NULL;
}

dirtool_deinit(&DirToolState);
#endif

return ret;
}

0 comments on commit 3b75b28

Please sign in to comment.