Page 1 of 1

Load other efi file from an efi file

Posted: Sun Dec 18, 2022 4:16 pm
by robin1201
Hello guys,

at the moment i am trying to make a uefi bootloader using gnu-efi and i want to load an other efi file which is located on the same device but in an other directory and give control to it.
My problem is that it says Failed to load the JellyBoot.EFI! Reason: Invalid Parameter. I do not know how to fix this problem i hope anyone can help me.

Source code:

Code: Select all

// Import libraries
#include <efi.h>
#include <efilib.h>

EFI_STATUS
EFIAPI
efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {

    // Initialize the UEFI
    InitializeLib(ImageHandle, SystemTable);

    // Define variables
    EFI_STATUS Status;    
    EFI_HANDLE LoadedImageHandle;
    EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
    EFI_DEVICE_PATH *DevicePath;

    CHAR16 *FilePath = L"\\EFI\\JellyBoot\\JellyBoot.EFI";

    // Get a the loaded image protocol for the current image
    LoadedImage = (EFI_LOADED_IMAGE_PROTOCOL *)(ImageHandle);

    // Create a device path for the file
    DevicePath = FileDevicePath(
        LoadedImage->DeviceHandle,
        FilePath
    );
    if(DevicePath == NULL) {
        Print(L"Failed to create device path for file!\n");
    }

    // Load the JellyBoot.EFI into memory
    Status = SystemTable->BootServices->LoadImage(
        FALSE,
        LoadedImage->DeviceHandle,
        DevicePath,
        NULL,
        0,
        &LoadedImageHandle
    );
    if(EFI_ERROR(Status)) {
        Print(L"Failed to load the JellyBoot.EFI!\nReason: %r\n", Status);
    }

    // Start the JellyBoot.EFI and check if it started successfully
    Status = SystemTable->BootServices->StartImage(
        LoadedImageHandle,
        NULL,
        NULL
    );
    if(EFI_ERROR(Status)) {
        Print(L"Failed to start the JellyBoot.EFI!\nReason %r\n", Status);
    } else {
        Print(L"JellyBoot.EFI started successfully!\n");
    }

    while(1){};

    return EFI_SUCCESS;
}

Re: Load other efi file from an efi file

Posted: Sun Dec 18, 2022 4:43 pm
by robin1201
i fixed it

Re: Load other efi file from an efi file

Posted: Sun Dec 18, 2022 5:16 pm
by kzinti
And how did you fix it? This information could be useful to someone in the future running into the same issue.

Re: Load other efi file from an efi file

Posted: Sat Dec 24, 2022 12:18 pm
by zaval
kzinti wrote:And how did you fix it? This information could be useful to someone in the future running into the same issue.
probably this

Code: Select all

LoadedImage = (EFI_LOADED_IMAGE_PROTOCOL *)(ImageHandle);
it's not supposed to have been done this way. :)

Re: Load other efi file from an efi file

Posted: Sat Dec 24, 2022 4:13 pm
by robin1201
kzinti wrote:And how did you fix it? This information could be useful to someone in the future running into the same issue.
That is my code that works

Code: Select all

// Import libraries from GNU-EFI
#include <efi.h>
#include <efilib.h>

EFI_STATUS
EFIAPI
efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {

    // Initialize the UEFI
    InitializeLib(ImageHandle, SystemTable);

    // Define variables
    EFI_STATUS Status;    
    EFI_HANDLE LoadedImageHandle;
    EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
    EFI_DEVICE_PATH *DevicePath;

    CHAR16 *FilePath = L"\\EFI\\JellyBoot\\JellyBoot.efi";

    // Get the loaded image protocol for the current image
    Status = uefi_call_wrapper(
        SystemTable->BootServices->OpenProtocol,
        6,
        ImageHandle,
        &LoadedImageProtocol,
        (VOID **)&LoadedImage,
        ImageHandle,
        NULL,
        EFI_OPEN_PROTOCOL_GET_PROTOCOL
    );
    if(EFI_ERROR(Status)) {
        Print(L"Failed to get LoadedImageProtocol!\nReason: %r\n", Status);
    }

    // Create a device path for the file
    DevicePath = FileDevicePath(
        LoadedImage->DeviceHandle,
        FilePath
    );
    if(DevicePath == NULL) {
        Print(L"Failed to create device path for file!\n");
    }

    // Load JellyBoot.EFI
    Status = uefi_call_wrapper(
        SystemTable->BootServices->LoadImage,
        6,
        FALSE,
        ImageHandle,
        DevicePath,
        NULL, 
        0,
        &LoadedImageHandle
    );
    if(EFI_ERROR(Status)) {
        Print(L"Failed to load the JellyBoot.EFI!\nReason: %r\n", Status);
    }

    // Start JellyBoot.EFI and check if it started successfully
    Status = uefi_call_wrapper(
        SystemTable->BootServices->StartImage,
        3, 
        LoadedImageHandle,
        NULL,
        NULL
    );
    if(EFI_ERROR(Status)) {
        Print(L"Failed to start the JellyBoot.EFI!\nReason %r\n", Status);
    } else {
        uefi_call_wrapper(
            SystemTable->RuntimeServices->ResetSystem,
            4,
            EfiResetWarm,
            EFI_SUCCESS,
            0,
            NULL
        );
    }

    return EFI_SUCCESS;
}