Skip to content

Commit ce62b00

Browse files
committed
ShellParams: Add protocol
Useful to get the shell arguments for a commandline application. Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 9981866 commit ce62b00

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Added `core::error::Error` implementations to all error types.
2020
- `SystemTable::exit_boot_services` now takes one param `memory_type` to ensure
2121
the memory type of memory map.
22+
- Added the `ShellParams` protocol
2223

2324
### Removed
2425
- `BootServices::memmove` and `BootServices::set_mem` have been removed, use

uefi-raw/src/protocol/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ pub mod driver;
1212
pub mod loaded_image;
1313
pub mod memory_protection;
1414
pub mod rng;
15+
pub mod shell_params;

uefi-raw/src/protocol/shell_params.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::{guid, Char16, Guid};
2+
use core::ffi::c_void;
3+
4+
pub type ShellFileHandle = *const c_void;
5+
6+
#[derive(Debug)]
7+
#[repr(C)]
8+
pub struct ShellParametersProtocol {
9+
/// Pointer to a list of arguments
10+
pub argv: *const *const Char16,
11+
/// Number of arguments
12+
pub argc: usize,
13+
/// Handle of the standard input
14+
pub std_in: ShellFileHandle,
15+
/// Handle of the standard output
16+
pub std_out: ShellFileHandle,
17+
/// Handle of the standard error output
18+
pub std_err: ShellFileHandle,
19+
}
20+
21+
impl ShellParametersProtocol {
22+
pub const GUID: Guid = guid!("752f3136-4e16-4fdc-a22a-e5f46812f4ca");
23+
}

uefi/src/proto/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub mod network;
7676
pub mod pi;
7777
pub mod rng;
7878
pub mod security;
79+
pub mod shell_params;
7980
pub mod shim;
8081
pub mod string;
8182
pub mod tcg;

uefi/src/proto/shell_params.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! `ShellParams` protocol
2+
3+
use crate::proto::unsafe_protocol;
4+
use crate::{data_types, Char16};
5+
use core::slice::from_raw_parts;
6+
use uefi_raw::protocol::shell_params::ShellParametersProtocol;
7+
8+
#[cfg(feature = "alloc")]
9+
use crate::CStr16;
10+
#[cfg(feature = "alloc")]
11+
use alloc::string::String;
12+
#[cfg(feature = "alloc")]
13+
use alloc::string::ToString;
14+
15+
/// The ShellParameters protocol.
16+
#[derive(Debug)]
17+
#[repr(transparent)]
18+
#[unsafe_protocol(ShellParametersProtocol::GUID)]
19+
pub struct ShellParameters(ShellParametersProtocol);
20+
21+
impl ShellParameters {
22+
/// Get an iterator of the shell parameter arguments
23+
#[cfg(feature = "alloc")]
24+
pub fn get_args(&self) -> impl Iterator<Item = String> {
25+
unsafe {
26+
from_raw_parts(
27+
self.0.argv.cast::<*const data_types::chars::Char16>(),
28+
self.0.argc,
29+
)
30+
.iter()
31+
.map(|x| CStr16::from_ptr(*x).to_string())
32+
}
33+
}
34+
35+
/// Get a slice of the args, as Char16 pointers
36+
#[must_use]
37+
pub fn get_args_slice(&self) -> &[*const Char16] {
38+
unsafe {
39+
from_raw_parts(
40+
self.0.argv.cast::<*const data_types::chars::Char16>(),
41+
self.0.argc,
42+
)
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)