Skip to content

Commit 1c4e7b6

Browse files
committed
ShellParams: Use open_protocol_exclusive in example
It's safer. Signed-off-by: Daniel Schaefer <[email protected]>
1 parent b6977ef commit 1c4e7b6

File tree

1 file changed

+14
-40
lines changed

1 file changed

+14
-40
lines changed

uefi-test-runner/examples/shell_params.rs

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ use log::error;
88
// ANCHOR: use
99
//use log::info;
1010
use uefi::CStr16;
11-
use uefi::{
12-
prelude::*,
13-
proto::shell_params::ShellParameters,
14-
table::boot::{OpenProtocolAttributes, OpenProtocolParams, SearchType},
15-
Identify,
16-
};
11+
use uefi::{prelude::*, proto::shell_params::ShellParameters};
1712
use uefi_services::println;
1813

1914
extern crate alloc;
@@ -23,55 +18,34 @@ use alloc::vec::Vec;
2318

2419
// ANCHOR: entry
2520
#[entry]
26-
fn main(_image_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
21+
fn main(image_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
2722
// ANCHOR_END: entry
2823
// ANCHOR: services
2924
uefi_services::init(&mut system_table).unwrap();
3025
let boot_services = system_table.boot_services();
3126
// ANCHOR_END: services
3227

3328
// 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 {
29+
let shell_params =
30+
boot_services.open_protocol_exclusive::<ShellParameters>(image_handle);
31+
let shell_params = match shell_params {
3732
Ok(s) => s,
3833
Err(e) => {
3934
error!("Failed to get ShellParameters protocol");
4035
return e.status();
4136
}
4237
};
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-
};
5738

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);
39+
// Get as Vec of String, only with alloc feature
40+
let args: Vec<String> = shell_params.get_args().collect();
41+
println!("Args: {:?}", args);
6742

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-
}
43+
// Or without allocating, get a slice of the pointers
44+
let args = shell_params.get_args_slice();
45+
println!("Num args: {}", args.len());
46+
if args.len() > 1 {
47+
unsafe {
48+
println!("First real arg: '{}'", CStr16::from_ptr(args[1]));
7549
}
7650
}
7751
// ANCHOR_END: params

0 commit comments

Comments
 (0)