OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 2:02 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Load other efi file from an efi file
PostPosted: Sun Dec 18, 2022 4:16 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: Load other efi file from an efi file
PostPosted: Sun Dec 18, 2022 4:43 pm 
Offline

Joined: Sun Aug 22, 2021 3:11 pm
Posts: 17
Location: Germany
i fixed it


Top
 Profile  
 
 Post subject: Re: Load other efi file from an efi file
PostPosted: Sun Dec 18, 2022 5:16 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
And how did you fix it? This information could be useful to someone in the future running into the same issue.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: Load other efi file from an efi file
PostPosted: Sat Dec 24, 2022 12:18 pm 
Offline
Member
Member
User avatar

Joined: Fri Feb 17, 2017 4:01 pm
Posts: 642
Location: Ukraine, Bachmut
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:
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).


Top
 Profile  
 
 Post subject: Re: Load other efi file from an efi file
PostPosted: Sat Dec 24, 2022 4:13 pm 
Offline

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 106 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group