Skip to content

Commit 94a6116

Browse files
committed
Add example app for ShellParams protocol
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent ee9dbc4 commit 94a6116

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// ANCHOR: all
2+
// ANCHOR: features
3+
#![no_main]
4+
#![no_std]
5+
// ANCHOR_END: features
6+
7+
use log::error;
8+
// ANCHOR: use
9+
//use log::info;
10+
use uefi::CStr16;
11+
use uefi::{
12+
prelude::*,
13+
proto::shell_params::ShellParameters,
14+
table::boot::{OpenProtocolAttributes, OpenProtocolParams, SearchType},
15+
Identify,
16+
};
17+
use uefi_services::println;
18+
19+
extern crate alloc;
20+
use alloc::string::String;
21+
use alloc::vec::Vec;
22+
// ANCHOR_END: use
23+
24+
// ANCHOR: entry
25+
#[entry]
26+
fn main(_image_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
27+
// ANCHOR_END: entry
28+
// ANCHOR: services
29+
uefi_services::init(&mut system_table).unwrap();
30+
let boot_services = system_table.boot_services();
31+
// ANCHOR_END: services
32+
33+
// ANCHOR: params
34+
let shell_params_h = boot_services
35+
.locate_handle_buffer(SearchType::ByProtocol(&ShellParameters::GUID));
36+
let shell_params_h = match shell_params_h {
37+
Ok(s) => s,
38+
Err(e) => {
39+
error!("Failed to get ShellParameters protocol");
40+
return e.status();
41+
}
42+
};
43+
println!("Found {} ShellParams handles", (*shell_params_h).len());
44+
for handle in &*shell_params_h {
45+
let params_handle = unsafe {
46+
boot_services
47+
.open_protocol::<ShellParameters>(
48+
OpenProtocolParams {
49+
handle: *handle,
50+
agent: boot_services.image_handle(),
51+
controller: None,
52+
},
53+
OpenProtocolAttributes::GetProtocol,
54+
)
55+
.expect("Failed to open ShellParams handle")
56+
};
57+
58+
// TODO: Ehm why are there two and one has no args?
59+
// Maybe one is the shell itself?
60+
if params_handle.argc == 0 {
61+
continue;
62+
}
63+
64+
// Get as Vec of String, only with alloc feature
65+
let args: Vec<String> = params_handle.get_args().collect();
66+
println!("Args: {:?}", args);
67+
68+
// Or without allocating, get a slice of the pointers
69+
let args = params_handle.get_args_slice();
70+
println!("Num args: {}", args.len());
71+
if args.len() > 1 {
72+
unsafe {
73+
println!("First real arg: '{}'", CStr16::from_ptr(args[1]));
74+
}
75+
}
76+
}
77+
// ANCHOR_END: params
78+
79+
// ANCHOR: return
80+
Status::SUCCESS
81+
}
82+
// ANCHOR_END: return
83+
// ANCHOR_END: all

0 commit comments

Comments
 (0)