Search code examples
uefiedk2gnu-efi

How to access efi executable present in another partition using gnu-efi?


I have to create 2 fat16 partitions in a disk and place one of the efi binary in another fat16 partition.

To begin with, wrote a basic HelloWorld.efi application (second stage) and for the first stage, used below-mentioned 2 apis to execute the HelloWorld.efi (both binaries are present in the same partition)

Currently I am trying to figure out a way to access the HelloWorld.efi present in another partition. Using these apis, I can access any file present in the same partition but don't know what api(s) to use to access another partition? Any suggestions? Thanks.

efi_status = BS->HandleProtocol(image_handle, &EFI_LOADED_IMAGE_GUID,
                (void **)&shim_li);

efi_status = BS->HandleProtocol(device, &EFI_SIMPLE_FILE_SYSTEM_GUID,
                (void **) &drive);

Solution

  • The simplest way would be to check if the file exists inside any filesystem.

    The code below uses EDK2 but should work with gnu-efi with minimal modifications.

    EFI_STATUS Status;
    EFI_HANDLE* Handles;
    UINTN HandleCount;
    UINTN HandleIndex;
    EFI_DEVICE_PATH_PROTOCOL* FilePath;
    EFI_HANDLE ImageHandle;
    CHAR16* ExitData;
    UINTN ExitDataLength;
    
    Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid, NULL, &HandleCount, &Handles);
    // error handling
    
    for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
        FilePath = FileDevicePath(Handles[HandleIndex], L"MyFolder\\HelloWorld.efi");
        if (FilePath == NULL) {
            continue;
        }
        
        Status = gBS->LoadImage(TRUE, gImageHandle, FilePath, NULL, 0, &ImageHandle);
        gBS->FreePool(FilePath);
        if (Status != EFI_SUCCESS) {
            continue;
        }
        // Set LoadOptions and Watchdog if needed
        Status = gBS->StartImage(ImageHandle, &ExitDataLength, &ExitData);
        // error handling
    }
    gBS->FreePool(Handles);
    

    If you know the partition id you can parse the device path of the filesystem and search for the correct partition.