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
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 130 additions & 1 deletion uefi/src/proto/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,141 @@

//! EFI Shell Protocol v2.2

use crate::proto::unsafe_protocol;
#![cfg(feature = "alloc")]

use alloc::vec::Vec;
use uefi_macros::unsafe_protocol;
use uefi_raw::Status;

use core::ptr;

pub use uefi_raw::protocol::shell::ShellProtocol;

use crate::{CStr16, Char16};

/// Shell Protocol
#[derive(Debug)]
#[repr(transparent)]
#[unsafe_protocol(uefi_raw::protocol::shell::ShellProtocol::GUID)]
pub struct Shell(uefi_raw::protocol::shell::ShellProtocol);

impl Shell {
/// Gets the environment variable or list of environment variables
///
/// # Arguments
///
/// * `name` - The environment variable name of which to retrieve the
/// value
/// If None, will return all defined shell environment
/// variables
///
/// # Returns
///
/// * `Some(Vec<env_value>)` - Value of the environment variable
/// * `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>> {
let mut env_vec = Vec::new();
match name {
Some(n) => {
let name_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(n).cast();
let var_val = unsafe { (self.0.get_env)(name_ptr.cast()) };
if var_val.is_null() {
return None;
} else {
unsafe { env_vec.push(CStr16::from_ptr(var_val.cast())) };
}
}
None => {
let cur_env_ptr = unsafe { (self.0.get_env)(ptr::null()) };

let mut cur_start = cur_env_ptr;
let mut cur_len = 0;

let mut i = 0;
let mut null_count = 0;
unsafe {
while null_count <= 1 {
if (*(cur_env_ptr.add(i))) == Char16::from_u16_unchecked(0).into() {
if cur_len > 0 {
env_vec.push(CStr16::from_char16_with_nul_unchecked(
&(*ptr::slice_from_raw_parts(cur_start.cast(), cur_len + 1)),
));
}
cur_len = 0;
null_count += 1;
} else {
if null_count > 0 {
cur_start = cur_env_ptr.add(i);
}
null_count = 0;
cur_len += 1;
}
i += 1;
}
}
}
}
Some(env_vec)
}

/// Sets the environment variable
///
/// # Arguments
///
/// * `name` - The environment variable for which to set the value
/// * `value` - The new value of the environment variable
/// * `volatile` - Indicates whether or not the variable is volatile or
/// not
///
/// # Returns
///
/// * `Status::SUCCESS` The variable was successfully set
pub fn set_env(&self, name: &CStr16, value: &CStr16, volatile: bool) -> Status {
Copy link
Member

Choose a reason for hiding this comment

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

Here and on other methods, return Result (https://docs.rs/uefi/latest/uefi/type.Result.html) instead of a raw Status. This is more convenient for users since it works with ?.

Copy link
Member

Choose a reason for hiding this comment

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

set_var to match std

let name_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(name).cast();
Copy link
Member

Choose a reason for hiding this comment

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

Instead of core::ptr::from_ref, you can use name.as_ptr() (and ditto for value below).

let value_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(value).cast();
unsafe { (self.0.set_env)(name_ptr.cast(), value_ptr.cast(), volatile) }
}

/// Returns the current directory on the specified device
///
/// # Arguments
///
/// * `file_system_mapping` - The file system mapping for which to get
/// the current directory
/// # Returns
///
/// * `Some(cwd)` - CStr16 containing the current working directory
/// * `None` - Could not retrieve current directory
#[must_use]
pub fn get_cur_dir<'a>(&'a self, file_system_mapping: Option<&CStr16>) -> Option<&'a CStr16> {
let mapping_ptr: *const Char16 = file_system_mapping.map_or(ptr::null(), |x| (x.as_ptr()));
Copy link
Member

Choose a reason for hiding this comment

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

nit: parens not needed around x.as_ptr(). Might be clearer to write as map_or(ptr::null(), CStr16::as_ptr).

let cur_dir = unsafe { (self.0.get_cur_dir)(mapping_ptr.cast()) };
if cur_dir.is_null() {
None
} else {
unsafe { Some(CStr16::from_ptr(cur_dir.cast())) }
}
}

/// Changes the current directory on the specified device
///
/// # Arguments
///
/// * `file_system` - Pointer to the file system's mapped name.
/// * `directory` - Points to the directory on the device specified by
/// `file_system`.
/// # Returns
///
/// * `Status::SUCCESS` The directory was successfully set
///
/// # Errors
///
/// * `Status::EFI_NOT_FOUND` The directory does not exist
pub fn set_cur_dir(&self, file_system: Option<&CStr16>, directory: Option<&CStr16>) -> Status {
Copy link
Member

Choose a reason for hiding this comment

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

set_current_dir to match std

let fs_ptr: *const Char16 = file_system.map_or(ptr::null(), |x| (x.as_ptr()));
let dir_ptr: *const Char16 = directory.map_or(ptr::null(), |x| (x.as_ptr()));
unsafe { (self.0.set_cur_dir)(fs_ptr.cast(), dir_ptr.cast()) }
}
}