-
-
Notifications
You must be signed in to change notification settings - Fork 172
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
92e07bb
0f30078
5a27fb8
a21932f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
RenTrieu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// not | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Status::SUCCESS` The variable was successfully set | ||
pub fn set_env(&self, name: &CStr16, value: &CStr16, volatile: bool) -> Status { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and on other methods, return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
let name_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(name).cast(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of |
||
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())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: parens not needed around |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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()) } | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.