-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ShellParams protocol #772
Merged
nicholasbishop
merged 3 commits into
rust-osdev:main
from
FrameworkComputer:shell-params
Sep 11, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ pub mod driver; | |
pub mod loaded_image; | ||
pub mod memory_protection; | ||
pub mod rng; | ||
pub mod shell_params; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::{guid, Char16, Guid}; | ||
use core::ffi::c_void; | ||
|
||
pub type ShellFileHandle = *const c_void; | ||
|
||
#[derive(Debug)] | ||
#[repr(C)] | ||
pub struct ShellParametersProtocol { | ||
/// Pointer to a list of arguments. | ||
pub argv: *const *const Char16, | ||
/// Number of arguments. | ||
pub argc: usize, | ||
/// Handle of the standard input. | ||
pub std_in: ShellFileHandle, | ||
/// Handle of the standard output. | ||
pub std_out: ShellFileHandle, | ||
/// Handle of the standard error output. | ||
pub std_err: ShellFileHandle, | ||
} | ||
|
||
impl ShellParametersProtocol { | ||
pub const GUID: Guid = guid!("752f3136-4e16-4fdc-a22a-e5f46812f4ca"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// ANCHOR: all | ||
// ANCHOR: features | ||
#![no_main] | ||
#![no_std] | ||
// ANCHOR_END: features | ||
|
||
use log::error; | ||
// ANCHOR: use | ||
use uefi::{prelude::*, proto::shell_params::ShellParameters}; | ||
use uefi_services::println; | ||
|
||
extern crate alloc; | ||
use alloc::string::{String, ToString}; | ||
use alloc::vec::Vec; | ||
// ANCHOR_END: use | ||
|
||
// ANCHOR: entry | ||
#[entry] | ||
fn main(image_handle: Handle, mut system_table: SystemTable<Boot>) -> Status { | ||
// ANCHOR_END: entry | ||
// ANCHOR: services | ||
uefi_services::init(&mut system_table).unwrap(); | ||
let boot_services = system_table.boot_services(); | ||
// ANCHOR_END: services | ||
|
||
// ANCHOR: params | ||
let shell_params = | ||
boot_services.open_protocol_exclusive::<ShellParameters>(image_handle); | ||
let shell_params = match shell_params { | ||
Ok(s) => s, | ||
Err(e) => { | ||
error!("Failed to get ShellParameters protocol"); | ||
return e.status(); | ||
} | ||
}; | ||
|
||
// Get as Vec of String, only with alloc feature | ||
let args: Vec<String> = | ||
shell_params.args().map(|x| x.to_string()).collect(); | ||
println!("Args: {:?}", args); | ||
|
||
// Or without allocating, get a slice of the pointers | ||
println!("Num args: {}", args.len()); | ||
if shell_params.args_len() > 1 { | ||
println!("First real arg: '{}'", args[1]); | ||
} | ||
// ANCHOR_END: params | ||
|
||
// ANCHOR: return | ||
Status::SUCCESS | ||
} | ||
// ANCHOR_END: return | ||
// ANCHOR_END: all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use uefi::proto::shell_params::ShellParameters; | ||
use uefi::table::boot::BootServices; | ||
|
||
use alloc::string::ToString; | ||
use alloc::vec::Vec; | ||
|
||
pub fn test(bt: &BootServices) { | ||
info!("Running loaded image protocol test"); | ||
|
||
let image = bt | ||
.get_handle_for_protocol::<ShellParameters>() | ||
.expect("No ShellParameters handles"); | ||
let shell_params = bt | ||
.open_protocol_exclusive::<ShellParameters>(image) | ||
.expect("Failed to open ShellParameters protocol"); | ||
|
||
assert_eq!(shell_params.args_len(), 4); | ||
assert_eq!( | ||
shell_params | ||
.args() | ||
.map(|x| x.to_string()) | ||
.collect::<Vec<_>>(), | ||
&["shell.efi", "test_runner.efi", "arg1", "arg2"] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//! `ShellParams` protocol | ||
|
||
use crate::proto::unsafe_protocol; | ||
use crate::{data_types, Char16}; | ||
use core::slice::from_raw_parts; | ||
use uefi_raw::protocol::shell_params::ShellParametersProtocol; | ||
|
||
use crate::CStr16; | ||
|
||
/// The ShellParameters protocol. | ||
#[derive(Debug)] | ||
#[repr(transparent)] | ||
#[unsafe_protocol(ShellParametersProtocol::GUID)] | ||
pub struct ShellParameters(ShellParametersProtocol); | ||
|
||
impl ShellParameters { | ||
/// Get the number of shell parameter arguments | ||
#[must_use] | ||
pub fn args_len(&self) -> usize { | ||
self.0.argc | ||
} | ||
|
||
/// Get an iterator of the shell parameter arguments | ||
pub fn args(&self) -> impl Iterator<Item = &CStr16> { | ||
self.args_slice() | ||
.iter() | ||
.map(|x| unsafe { CStr16::from_ptr(*x) }) | ||
} | ||
|
||
/// Get a slice of the args, as Char16 pointers | ||
#[must_use] | ||
fn args_slice(&self) -> &[*const Char16] { | ||
unsafe { | ||
from_raw_parts( | ||
self.0.argv.cast::<*const data_types::chars::Char16>(), | ||
self.0.argc, | ||
) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These
ANCHOR
comments are just used by mdbook (see https://rust-osdev.github.io/uefi-rs/HEAD/how_to/protocols.html#walkthrough for example), so unless you are looking to add this example to the book, you can leave these out.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to have a shell application example in the book.
However it would be more useful once the shell protocol is merged.