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_params.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! `ShellParams` protocol |
| 4 | |
| 5 | use crate::proto::unsafe_protocol; |
| 6 | use crate::{Char16, data_types}; |
| 7 | use core::slice::from_raw_parts; |
| 8 | use uefi_raw::protocol::shell_params::ShellParametersProtocol; |
| 9 | |
| 10 | use crate::CStr16; |
| 11 | |
| 12 | /// The ShellParameters [`Protocol`]. |
| 13 | /// |
| 14 | /// [`Protocol`]: uefi::proto::Protocol |
| 15 | #[derive(Debug)] |
| 16 | #[repr(transparent)] |
| 17 | #[unsafe_protocol(ShellParametersProtocol::GUID)] |
| 18 | pub struct ShellParameters(ShellParametersProtocol); |
| 19 | |
| 20 | impl ShellParameters { |
| 21 | /// Get the number of shell parameter arguments |
| 22 | #[must_use] |
| 23 | pub const fn args_len(&self) -> usize { |
| 24 | self.0.argc |
| 25 | } |
| 26 | |
| 27 | /// Get an iterator of the shell parameter arguments |
| 28 | pub fn args(&self) -> impl Iterator<Item = &CStr16> { |
| 29 | self.args_slice() |
| 30 | .iter() |
| 31 | // SAFETY: The memory is valid. |
| 32 | .map(|x| unsafe { CStr16::from_ptr(*x) }) |
| 33 | } |
| 34 | |
| 35 | /// Get a slice of the args, as Char16 pointers |
| 36 | #[must_use] |
| 37 | const fn args_slice(&self) -> &[*const Char16] { |
| 38 | // The EDK2 shell sets `argv` to NULL for an empty command line. |
| 39 | if self.0.argv.is_null() || self.0.argc == 0 { |
| 40 | return &[]; |
| 41 | } |
| 42 | // SAFETY: The memory is valid. |
| 43 | unsafe { |
| 44 | from_raw_parts( |
| 45 | self.0.argv.cast::<*const data_types::chars::Char16>(), |
| 46 | self.0.argc, |
| 47 | ) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | #[cfg(test)] |
| 53 | mod tests { |
| 54 | use super::*; |
| 55 | use core::ptr; |
| 56 | |
| 57 | #[test] |
| 58 | fn test_args_null_argv() { |
| 59 | let raw = ShellParametersProtocol { |
| 60 | argv: ptr::null(), |
| 61 | argc: 0, |
| 62 | std_in: ptr::null_mut(), |
| 63 | std_out: ptr::null_mut(), |
| 64 | std_err: ptr::null_mut(), |
| 65 | }; |
| 66 | // SAFETY: `ShellParameters` is a transparent wrapper. |
| 67 | let params = unsafe { &*ptr::from_ref(&raw).cast::<ShellParameters>() }; |
| 68 | assert_eq!(params.args_len(), 0); |
| 69 | assert_eq!(params.args().count(), 0); |
| 70 | } |
| 71 | } |