Skip to content

Enhancement/efi shell interface #1679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

RenTrieu
Copy link
Contributor

This PR will implement the wrappers for EFI Shell Protocol functions to address #448 . This PR does branch off of the changes made in #596 so big thanks to @timrobertsdev for laying the groundwork!

Currently, I've implemented wrappers and wrote tests for GetEnv(), SetEnv(), GetCurDir() and SetCurDir(). My plan is to first write tests for and finish implementing Execute(), CloseFile(), CreateFile(), FindFiles(), FindFilesInDir(), and GetFileSize() since they have already been started. Then I can implement the rest in either this PR or subsequent follow up PRs (whichever is preferable).

I do have some questions about my get_env() implementation. UEFI's GetEnv() returns the value of a variable if its name is specified. However if its name is not specified, it returns all known variable names in a *const Char16 where the names are delimited by a NULL and the end of the list is terminated by a double NULL.

My initial approach was to parse the *const Char16 into a Vec in this case. I wrapped the return value in an Enum that can be unpacked into a single &CStr16 or into a vector of &CStr16s depending on which is expected. Is this approach okay? I was concerned that if I simply returned a &CStr16 in the "name list" case that the true size of the string wouldn't be visible since the names are separated by NULL. Also, is it okay for me to use alloc::vec::Vec? I saw that a lot of other protocols don't use any Vec at all so I'm not clear on if there are some restrictions for doing so.

Checklist

  • Execute
  • GetEnv
  • SetEnv
  • GetAlias
  • SetAlias
  • GetHelpText
  • GetDevicePathFromMap
  • GetMapFromDevicePath
  • GetDevicePathFromFilePath
  • GetFilePathFromDevicePath
  • SetMap
  • GetCurDir
  • SetCurDir
  • OpenFileList
  • FreeFileList
  • RemoveDupInFileList
  • BatchIsActive
  • IsRootShell
  • EnablePageBreak
  • DisablePageBreak
  • GetPageBreak
  • GetDeviceName
  • GetFileInfo
  • SetFileInfo
  • OpenFileByName
  • CloseFile
  • CreateFile
  • ReadFile
  • WriteFile
  • DeleteFile
  • DeleteFileByName
  • GetFilePosition
  • SetFilePosition
  • FlushFile
  • FindFiles
  • FindFilesInDir
  • GetFileSize
  • OpenRoot
  • OpenRootByHandle
  • ExecutionBreak
  • Version Variables
  • RegisterGuidName
  • GetGuidName
  • GetGuidFromName
  • GetEnvEx

/// * `Some(Vec<env_names>)` - Vector of environment variable names
/// * `None` - Environment variable doesn't exist
#[must_use]
pub fn get_env<'a>(&'a self, name: Option<&CStr16>) -> Option<EnvOutput<'a>> {
Copy link
Member

@phip1611 phip1611 May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd much rather have the high-level API to not replicate the low-level interface here. Instead, we can use the low-level interface to create an API similar to:

Then we also don't need the EnvOutput type effectively acting as a Either type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed get_env() to return an Option<Vec<&CStr16>>. In the case where name is specified, the function will return a Vec of length 1 where the resulting &CStr16 contains the value of that environment variable. Otherwise in the name=None case, the Vec would contain the names of all environment variables. That way, the caller can just deal with the one type.

Is this more so along the lines of what you are thinking?

Also, I've chosen to wrap the return value in Option since the specification returns a NULL pointer in the case that name does not point to a valid environment variable, so I don't believe there are any errors we'd expect to return.

Copy link
Member

@phip1611 phip1611 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! As a general rule of thumb: High-level wrappers should be close to the API of std and not replicate "ugly" or unusual details of the low-level API. See my suggestions for get_var

/// TODO
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct ListEntry {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having now lifetime here makes it easy to introduce bugs. Also, this is code that we can't test using miri unfortunately.

@nicholasbishop
Copy link
Member

Some general guidance:

  1. Start with a PR that just updates uefi-raw. (As Philipp noted here, the definition of the shell protocol that matches the C spec should go in uefi-raw. See for example RngProtocol. api_guidelines has some notes about how to implement stuff in uefi-raw.)
  2. Once that's merged, do small PRs that add ergonomic wrappers to the uefi crate. Small PRs will help get review done faster.
  3. Regarding Vec, it's generally OK to use, but does need to be gated by the alloc feature. (I haven't looked at the code yet though, I'm just commenting generally.)

@RenTrieu
Copy link
Contributor Author

Thanks for the feedback! I will start with making the PR to update uefi-raw as Nicholas suggested, then come back to address the other comments.

RenTrieu added 2 commits June 2, 2025 14:56
This commit implements wrappers for the following EFI Shell Protocol
functions: set_env(), get_env(), set_cur_dir(), and get_cur_dir().
This commit includes tests for the following EFI Shell Protocol functions:
get_env(), set_env(), get_cur_dir(), and set_cur_dir().
@RenTrieu
Copy link
Contributor Author

RenTrieu commented Jun 3, 2025

Hi all. Now that #1680 is merged, I was wondering if it would be alright to use this PR to work on the 4 ergonomic wrappers I've implemented (GetEnv(), SetEnv(), GetCurDir(), SetCurDir()). Is this sufficiently small enough? Or should I split this into two PRs- one for the Env functions and the other for CurDir?

If it is okay to continue to use this PR, then I plan to rebase this branch onto the current main, squash the commits, and then continue from there.

@nicholasbishop
Copy link
Member

That sounds good, start with doing that in this PR. Once the changes are in this PR it's possible I might ask for it to be split up into more than one PR for review, but we can start with the assumption that one PR is OK.

@RenTrieu RenTrieu force-pushed the enhancement/efi_shell_interface branch from b9a3be8 to 0f30078 Compare June 3, 2025 18:31
@RenTrieu RenTrieu marked this pull request as ready for review June 3, 2025 20:06
@RenTrieu
Copy link
Contributor Author

RenTrieu commented Jun 3, 2025

I've rebased this branch onto the current main and updated it to disclude the ShellProtocol definition moved to uefi-raw. I was wondering if there is any guidance for writing tests. So far, I have done my best to look at other files and imitate the format such as uefi-test-runner/src/proto/media.rs and uefi-test-runner/src/proto/rng.rs, but if there is anything more specific I can revise what I have.

/// * `Some(Vec<env_names>)` - Vector of environment variable names
/// * `None` - Environment variable doesn't exist
#[must_use]
pub fn get_env<'a>(&'a self, name: Option<&CStr16>) -> Option<Vec<&'a CStr16>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't export this unintuitive/unrusty UEFI API in this high-level wrapper. Instead, please aim to stay close to std. This means:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Philipp, thanks again for the resources. However, it is not clear to me what you are asking. Are you asking for get_env() to have a similar return type to the var() and vars() functions? Or are you asking for get_env() to be split up into two separate functions- one dedicated to returning the value of an environment variable and the other dedicated to returning the current environment variables?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion!

Or are you asking for get_env() to be split up into two separate functions- one dedicated to returning the value of an environment variable and the other dedicated to returning the current environment variables?

This.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries! I can see how having separate dedicated functions is more intuitive. In the spirit of mirroring std, I have updated the protocol with these two new functions using the same naming convention as var and vars where:
get_env() - retrieves the value of an environment variable
get_envs() - retrieves the names of all environment variables

The UEFI get_env() implementation is used for getting individual environment
variable values and also environment variable names. This is not intuitive
so this commit splits the function into two dedicated ones: one for accessing
values and the other for accessing names.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants