How to access command line arguments in UEFI?

7.2k views Asked by At

What part of the specification details how command line arguments can be obtained?

2

There are 2 answers

0
AudioBubble On

You need to be careful with this one.

As you are probably aware, there is a UEFI LoadedImage protocol - this protocol returns a structure called EFI_LOADED_IMAGE which in turn has a LoadOptions member.

The UEFI shell will set this LoadOptions variable to whatever you type on the command line. Alternatively, I believe you can set this through the BootOptions EFI Variable, which is where care is needed - the first "argument" is not the process path in this case.

So what you need to do is process the one-long-string you get to deduce the "arguments" as you want them.

To use the LoadedImage protocol, do this:

EFI_STATUS status = EFI_SUCCESS;
EFI_LOADED_IMAGE* loaded_image;
EFI_GUID loaded_image_protocol = LOADED_IMAGE_PROTOCOL;
status = gBS->HandleProtocol(ImageHandle, 
                             &loaded_image_protocol, 
                             (void**) &loaded_image);

You can then get the length of the (0-terminated) string that was passed in by:

loaded_image->LoadOptionsSize;

Be aware this is the size in bytes, not the length. For that, you could use a library function:

LoadOptionsLength = StrLen((CHAR16 *)li->LoadOptions);

Finally, the actual string itself is available from:

CHAR16* CommandLineArgs = (CHAR16 *)li->LoadOptions;

There is a UEFI Shell Specification available for free which determines the protocols the shell speaks. You can actually talk directly to it, but I'm yet to experiment with that.

0
user2707695 On

You can find the details of the EFI_SHELL_PARAMETERS_PROTOCOL on page 55 of the UEFI_SHELL 2.0 Specs. Use handleprotocol() to obtain the address of the protocol interface. You can pass the handle of your image which you obtain in rcx on entry. The protocol structure gives the number of parameters and a pointer to a list of parameter pointers. The parameters are zero terminated ucs2 strings. The first parameter is the path from where your image was loaded. Space separators and double quotes around parameters are stripped. (Note: a none matching double quote is not stripped!) I dont know if there are limits to the number or the size of params. My test with 20 long parameters worked fine.