Page 1 of 1

How to programatically boot Linux with UEFI?

Posted: Tue Nov 16, 2021 2:04 pm
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.

Re: How to programatically boot Linux with UEFI?

Posted: Mon Nov 22, 2021 3:41 am
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);

Re: How to programatically boot Linux with UEFI?

Posted: Mon Nov 22, 2021 11:46 am
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;

Re: How to programatically boot Linux with UEFI?

Posted: Tue Nov 23, 2021 2:57 am
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.