aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / shell / mod.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! EFI Shell Protocol v2.2 |
| 4 | |
| 5 | use crate::proto::unsafe_protocol; |
| 6 | use crate::{CStr16, Char16, Error, Result, Status, StatusExt}; |
| 7 | |
| 8 | use core::marker::PhantomData; |
| 9 | use core::ptr; |
| 10 | use uefi_raw::protocol::shell::ShellProtocol; |
| 11 | |
| 12 | /// Shell Protocol |
| 13 | #[derive(Debug)] |
| 14 | #[repr(transparent)] |
| 15 | #[unsafe_protocol(ShellProtocol::GUID)] |
| 16 | pub struct Shell(ShellProtocol); |
| 17 | |
| 18 | /// Trait for implementing the var function |
| 19 | pub trait ShellVarProvider { |
| 20 | /// Gets the value of the specified environment variable |
| 21 | fn var(&self, name: &CStr16) -> Option<&CStr16>; |
| 22 | } |
| 23 | |
| 24 | /// Iterator over the names of environmental variables obtained from the Shell protocol. |
| 25 | #[derive(Debug)] |
| 26 | pub struct Vars<'a, T: ShellVarProvider> { |
| 27 | /// Char16 containing names of environment variables |
| 28 | names: *const Char16, |
| 29 | /// Reference to Shell Protocol |
| 30 | protocol: *const T, |
| 31 | /// Marker to attach a lifetime to `Vars` |
| 32 | _marker: PhantomData<&'a CStr16>, |
| 33 | } |
| 34 | |
| 35 | impl<'a, T: ShellVarProvider + 'a> Iterator for Vars<'a, T> { |
| 36 | type Item = (&'a CStr16, Option<&'a CStr16>); |
| 37 | // We iterate a list of NUL terminated CStr16s. |
| 38 | // The list is terminated with a double NUL. |
| 39 | fn next(&mut self) -> Option<Self::Item> { |
| 40 | // SAFETY: The memory is valid. |
| 41 | let s = unsafe { CStr16::from_ptr(self.names) }; |
| 42 | if s.is_empty() { |
| 43 | None |
| 44 | } else { |
| 45 | // SAFETY: The memory is valid. |
| 46 | self.names = unsafe { self.names.add(s.num_chars() + 1) }; |
| 47 | // SAFETY: The handle is known to point to live storage here. |
| 48 | Some((s, unsafe { self.protocol.as_ref().unwrap().var(s) })) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl ShellVarProvider for Shell { |
| 54 | /// Gets the value of the specified environment variable |
| 55 | fn var(&self, name: &CStr16) -> Option<&CStr16> { |
| 56 | self.var(name) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl Shell { |
| 61 | /// Returns the current directory on the specified device. |
| 62 | /// |
| 63 | /// # Arguments |
| 64 | /// |
| 65 | /// * `file_system_mapping` - The file system mapping for which to get |
| 66 | /// the current directory |
| 67 | /// |
| 68 | /// The returned string is owned by the shell and is valid until the |
| 69 | /// current directory changes, see [`Self::set_current_dir`]. |
| 70 | /// |
| 71 | /// # Errors |
| 72 | /// |
| 73 | /// * [`Status::NOT_FOUND`] - Could not retrieve current directory |
| 74 | pub fn current_dir(&self, file_system_mapping: Option<&CStr16>) -> Result<&CStr16> { |
| 75 | let mapping_ptr: *const Char16 = file_system_mapping.map_or(ptr::null(), CStr16::as_ptr); |
| 76 | // SAFETY: The memory is valid. |
| 77 | let cur_dir = unsafe { (self.0.get_cur_dir)(mapping_ptr.cast()) }; |
| 78 | if cur_dir.is_null() { |
| 79 | Err(Error::new(Status::NOT_FOUND, ())) |
| 80 | } else { |
| 81 | // SAFETY: The memory is valid. |
| 82 | unsafe { Ok(CStr16::from_ptr(cur_dir.cast())) } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// Changes the current directory on the specified device |
| 87 | /// |
| 88 | /// # Arguments |
| 89 | /// |
| 90 | /// * `file_system` - File system's mapped name. |
| 91 | /// * `directory` - Directory on the device specified by |
| 92 | /// `file_system`. |
| 93 | /// |
| 94 | /// # Errors |
| 95 | /// |
| 96 | /// * [`Status::NOT_FOUND`] - The directory does not exist |
| 97 | pub fn set_current_dir( |
| 98 | &mut self, |
| 99 | file_system: Option<&CStr16>, |
| 100 | directory: Option<&CStr16>, |
| 101 | ) -> Result { |
| 102 | let fs_ptr: *const Char16 = file_system.map_or(ptr::null(), |x| x.as_ptr()); |
| 103 | let dir_ptr: *const Char16 = directory.map_or(ptr::null(), |x| x.as_ptr()); |
| 104 | // SAFETY: The memory is valid. |
| 105 | unsafe { (self.0.set_cur_dir)(fs_ptr.cast(), dir_ptr.cast()) }.to_result() |
| 106 | } |
| 107 | |
| 108 | /// Gets the value of the specified environment variable |
| 109 | /// |
| 110 | /// # Arguments |
| 111 | /// |
| 112 | /// * `name` - The environment variable name of which to retrieve the |
| 113 | /// value. |
| 114 | /// |
| 115 | /// # Returns |
| 116 | /// |
| 117 | /// * `Some(<env_value>)` - &CStr16 containing the value of the |
| 118 | /// environment variable |
| 119 | /// * `None` - If environment variable does not exist |
| 120 | /// |
| 121 | /// The returned string is owned by the shell and is valid until the |
| 122 | /// variable changes, see [`Self::set_var`]. |
| 123 | #[must_use] |
| 124 | pub fn var(&self, name: &CStr16) -> Option<&CStr16> { |
| 125 | let name_ptr: *const Char16 = name.as_ptr(); |
| 126 | // SAFETY: The memory is valid. |
| 127 | let var_val = unsafe { (self.0.get_env)(name_ptr.cast()) }; |
| 128 | if var_val.is_null() { |
| 129 | None |
| 130 | } else { |
| 131 | // SAFETY: The memory is valid. |
| 132 | unsafe { Some(CStr16::from_ptr(var_val.cast())) } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// Gets an iterator over the names of all environment variables |
| 137 | /// |
| 138 | /// # Returns |
| 139 | /// |
| 140 | /// * `Vars` - Iterator over the names of the environment variables |
| 141 | #[must_use] |
| 142 | pub fn vars(&self) -> Vars<'_, Self> { |
| 143 | // The shell returns NULL instead of a list if there are no |
| 144 | // variables or if it fails to allocate the list. |
| 145 | static EMPTY_LIST: [u16; 1] = [0]; |
| 146 | |
| 147 | // SAFETY: The memory is valid. |
| 148 | let env_ptr = unsafe { (self.0.get_env)(ptr::null()) }; |
| 149 | let names = if env_ptr.is_null() { |
| 150 | EMPTY_LIST.as_ptr().cast::<Char16>() |
| 151 | } else { |
| 152 | env_ptr.cast::<Char16>() |
| 153 | }; |
| 154 | Vars { |
| 155 | names, |
| 156 | protocol: self, |
| 157 | _marker: PhantomData, |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /// Sets the environment variable |
| 162 | /// |
| 163 | /// # Arguments |
| 164 | /// |
| 165 | /// * `name` - The environment variable for which to set the value |
| 166 | /// * `value` - The new value of the environment variable |
| 167 | /// * `volatile` - Indicates whether the variable is volatile or |
| 168 | /// not |
| 169 | /// |
| 170 | /// # Returns |
| 171 | /// |
| 172 | /// * `Status::SUCCESS` - The variable was successfully set |
| 173 | pub fn set_var(&mut self, name: &CStr16, value: &CStr16, volatile: bool) -> Result { |
| 174 | let name_ptr: *const Char16 = name.as_ptr(); |
| 175 | let value_ptr: *const Char16 = value.as_ptr(); |
| 176 | // SAFETY: The memory is valid. |
| 177 | unsafe { (self.0.set_env)(name_ptr.cast(), value_ptr.cast(), volatile.into()) }.to_result() |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | #[cfg(test)] |
| 182 | mod tests { |
| 183 | use super::*; |
| 184 | use alloc::collections::BTreeMap; |
| 185 | use alloc::vec::Vec; |
| 186 | use core::mem::MaybeUninit; |
| 187 | use uefi::cstr16; |
| 188 | |
| 189 | /// Mock of `ShellProtocol::get_env` that returns no variable list. |
| 190 | unsafe extern "efiapi" fn mock_get_env_null( |
| 191 | _name: *const uefi_raw::Char16, |
| 192 | ) -> *const uefi_raw::Char16 { |
| 193 | ptr::null() |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn test_vars_null_list() { |
| 198 | // Only `get_env` is called, so leave the other fields uninitialised. |
| 199 | let mut raw = MaybeUninit::<ShellProtocol>::uninit(); |
| 200 | // SAFETY: Writes one field of the allocated value. |
| 201 | unsafe { (&raw mut (*raw.as_mut_ptr()).get_env).write(mock_get_env_null) }; |
| 202 | // SAFETY: `Shell` is a transparent wrapper and only reads `get_env`. |
| 203 | let shell = unsafe { &*raw.as_ptr().cast::<Shell>() }; |
| 204 | assert_eq!(shell.vars().count(), 0); |
| 205 | } |
| 206 | |
| 207 | struct ShellMock<'a> { |
| 208 | inner: BTreeMap<&'a CStr16, &'a CStr16>, |
| 209 | } |
| 210 | |
| 211 | impl<'a> ShellMock<'a> { |
| 212 | fn new(pairs: impl IntoIterator<Item = (&'a CStr16, &'a CStr16)>) -> ShellMock<'a> { |
| 213 | let mut inner_map = BTreeMap::new(); |
| 214 | for (name, val) in pairs.into_iter() { |
| 215 | inner_map.insert(name, val); |
| 216 | } |
| 217 | ShellMock { inner: inner_map } |
| 218 | } |
| 219 | } |
| 220 | impl<'a> ShellVarProvider for ShellMock<'a> { |
| 221 | fn var(&self, name: &CStr16) -> Option<&CStr16> { |
| 222 | if let Some(val) = self.inner.get(name) { |
| 223 | Some(*val) |
| 224 | } else { |
| 225 | None |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /// Testing Vars struct |
| 231 | #[test] |
| 232 | fn test_vars() { |
| 233 | // Empty Vars |
| 234 | let mut vars_mock = Vec::<u16>::new(); |
| 235 | vars_mock.extend_from_slice( |
| 236 | b"\0\0" |
| 237 | .into_iter() |
| 238 | .map(|&x| x as u16) |
| 239 | .collect::<Vec<_>>() |
| 240 | .as_slice(), |
| 241 | ); |
| 242 | let mut vars = Vars { |
| 243 | names: vars_mock.as_ptr().cast(), |
| 244 | protocol: &ShellMock::new(Vec::new()), |
| 245 | _marker: PhantomData, |
| 246 | }; |
| 247 | |
| 248 | assert!(vars.next().is_none()); |
| 249 | |
| 250 | // One environment variable in Vars |
| 251 | let mut vars_mock = Vec::<u16>::new(); |
| 252 | vars_mock.extend_from_slice( |
| 253 | b"foo\0\0" |
| 254 | .into_iter() |
| 255 | .map(|&x| x as u16) |
| 256 | .collect::<Vec<_>>() |
| 257 | .as_slice(), |
| 258 | ); |
| 259 | let vars = Vars { |
| 260 | names: vars_mock.as_ptr().cast(), |
| 261 | protocol: &ShellMock::new(Vec::from([(cstr16!("foo"), cstr16!("value"))])), |
| 262 | _marker: PhantomData, |
| 263 | }; |
| 264 | assert_eq!( |
| 265 | vars.collect::<Vec<_>>(), |
| 266 | Vec::from([(cstr16!("foo"), Some(cstr16!("value")))]) |
| 267 | ); |
| 268 | |
| 269 | // Multiple environment variables in Vars |
| 270 | let mut vars_mock = Vec::<u16>::new(); |
| 271 | vars_mock.extend_from_slice( |
| 272 | b"foo1\0bar\0baz2\0\0" |
| 273 | .into_iter() |
| 274 | .map(|&x| x as u16) |
| 275 | .collect::<Vec<_>>() |
| 276 | .as_slice(), |
| 277 | ); |
| 278 | |
| 279 | let vars = Vars { |
| 280 | names: vars_mock.as_ptr().cast(), |
| 281 | protocol: &ShellMock::new(Vec::from([ |
| 282 | (cstr16!("foo1"), cstr16!("value")), |
| 283 | (cstr16!("bar"), cstr16!("one")), |
| 284 | (cstr16!("baz2"), cstr16!("two")), |
| 285 | ])), |
| 286 | _marker: PhantomData, |
| 287 | }; |
| 288 | assert_eq!( |
| 289 | vars.collect::<Vec<_>>(), |
| 290 | Vec::from([ |
| 291 | (cstr16!("foo1"), Some(cstr16!("value"))), |
| 292 | (cstr16!("bar"), Some(cstr16!("one"))), |
| 293 | (cstr16!("baz2"), Some(cstr16!("two"))) |
| 294 | ]) |
| 295 | ); |
| 296 | } |
| 297 | } |