How to programatically boot Linux with UEFI?

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
sleepy762
Posts: 6
Joined: Wed Aug 11, 2021 4:58 am

How to programatically boot Linux with UEFI?

Post by sleepy762 »

I'm trying to boot into Linux using UEFI code.
I found that the kernel has an "EFI boot stub" which allows the kernel to be booted directly from the UEFI Shell with arguments but I can't figure out how to do the same in code.
I thought that maybe I could use StartImage() on it, but then how do I pass arguments to it? There must be a different solution.
davmac314
Member
Member
Posts: 121
Joined: Mon Jul 05, 2021 6:57 pm

Re: How to programatically boot Linux with UEFI?

Post by davmac314 »

You pass command-line arguments by setting LoadOptions (it's a void *, but you can set to a CHAR16 * representing a command line) in the EFI_LOADED_IMAGE_PROTOCOL, before calling StartImage.

Roughly (in C++, hence const_cast):

Code: Select all

    chained_image_LIP->LoadOptions = const_cast<void *>((const void *)cmdline);
    chained_image_LIP->LoadOptionsSize = (string_length(cmdline) + 1) * sizeof(CHAR16);

    status = EBS->StartImage(loaded_handle, nullptr, nullptr);
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: How to programatically boot Linux with UEFI?

Post by kzinti »

davmac314 wrote:

Code: Select all

    chained_image_LIP->LoadOptions = const_cast<void *>((const void *)cmdline);
If you are going to cast it to const just to remove the const in the same statement, why not just cast to non-const void?

Code: Select all

    chained_image_LIP->LoadOptions = (void *)cmdline;
davmac314
Member
Member
Posts: 121
Joined: Mon Jul 05, 2021 6:57 pm

Re: How to programatically boot Linux with UEFI?

Post by davmac314 »

kzinti wrote: If you are going to cast it to const just to remove the const in the same statement, why not just cast to non-const void?

Code: Select all

    chained_image_LIP->LoadOptions = (void *)cmdline;
Indeed, that slipped in. You could cast using a c-style cast directly to void * like you suggest, or you could nest a static_cast<const void *> inside the const_cast if you preferred to use only C++-style casts, which is what I'd intended (though now I'm reconsidering).

However, the code as written does work even though it mixes casting styles.
Post Reply