Load other efi file from an efi file

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
robin1201
Posts: 17
Joined: Sun Aug 22, 2021 3:11 pm
Location: Germany

Load other efi file from an efi file

Post 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;
}
robin1201
Posts: 17
Joined: Sun Aug 22, 2021 3:11 pm
Location: Germany

Re: Load other efi file from an efi file

Post by robin1201 »

i fixed it
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Load other efi file from an efi file

Post by kzinti »

And how did you fix it? This information could be useful to someone in the future running into the same issue.
User avatar
zaval
Member
Member
Posts: 656
Joined: Fri Feb 17, 2017 4:01 pm
Location: Ukraine, Bachmut
Contact:

Re: Load other efi file from an efi file

Post 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. :)
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).
robin1201
Posts: 17
Joined: Sun Aug 22, 2021 3:11 pm
Location: Germany

Re: Load other efi file from an efi file

Post 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;
}
Post Reply