-
-
Notifications
You must be signed in to change notification settings - Fork 170
ShellParams: Add subshell test #922
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
Closed
JohnAZoidberg
wants to merge
5
commits into
rust-osdev:main
from
FrameworkComputer:shell-params-subshell-test
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
81f73c5
Add ShellParams protocol
JohnAZoidberg 2b76381
Add example app for ShellParams protocol
JohnAZoidberg fa9f26c
uefi-test-runner: Add test for ShellParams protocol
JohnAZoidberg 02e7898
ShellParams: Use open_protocol_exclusive in example
JohnAZoidberg bf3d480
ShellParams: Add subshell test
JohnAZoidberg 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 hidden or 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 hidden or 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,57 @@ | ||
// ANCHOR: all | ||
// ANCHOR: features | ||
#![no_main] | ||
#![no_std] | ||
// ANCHOR_END: features | ||
|
||
use log::error; | ||
// ANCHOR: use | ||
//use log::info; | ||
use uefi::CStr16; | ||
use uefi::{prelude::*, proto::shell_params::ShellParameters}; | ||
use uefi_services::println; | ||
|
||
extern crate alloc; | ||
use alloc::string::String; | ||
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.get_args().collect(); | ||
println!("Args: {:?}", args); | ||
|
||
// Or without allocating, get a slice of the pointers | ||
let args = shell_params.get_args_slice(); | ||
println!("Num args: {}", args.len()); | ||
if args.len() > 1 { | ||
unsafe { | ||
println!("First real arg: '{}'", CStr16::from_ptr(args[1])); | ||
} | ||
} | ||
// ANCHOR_END: params | ||
|
||
// ANCHOR: return | ||
Status::SUCCESS | ||
} | ||
// ANCHOR_END: return | ||
// ANCHOR_END: all |
This file contains hidden or 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 hidden or 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 hidden or 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,114 @@ | ||
use alloc::string::String; | ||
use alloc::vec::Vec; | ||
use log::info; | ||
use uefi::prelude::*; | ||
use uefi::proto::device_path::build::{self, DevicePathBuilder}; | ||
use uefi::proto::device_path::{DevicePath, DeviceSubType, DeviceType, LoadedImageDevicePath}; | ||
use uefi::proto::loaded_image::LoadedImage; | ||
use uefi::proto::shell_params::ShellParameters; | ||
use uefi::table::boot::{BootServices, LoadImageSource}; | ||
use uefi::CStr16; | ||
|
||
pub fn test(image: Handle, bt: &BootServices) { | ||
info!("Running loaded image protocol test"); | ||
|
||
let shell_params = bt | ||
.open_protocol_exclusive::<ShellParameters>(image) | ||
.expect("Failed to open ShellParameters protocol"); | ||
|
||
info!("Argc: {}", shell_params.argc); | ||
info!("Args:"); | ||
for arg in shell_params.get_args_slice() { | ||
let arg_str = unsafe { CStr16::from_ptr(*arg) }; | ||
info!(" '{}'", arg_str); | ||
} | ||
|
||
assert_eq!(shell_params.argc, shell_params.get_args_slice().len()); | ||
|
||
// By default a single argument, the executable's path | ||
assert_eq!(shell_params.argc, 1); | ||
|
||
subshell_runner(image, bt); | ||
} | ||
|
||
/// Get the device path of the shell app. This is the same as the | ||
/// currently-loaded image's device path, but with the file path part changed. | ||
fn get_shell_app_device_path<'a>( | ||
boot_services: &BootServices, | ||
storage: &'a mut Vec<u8>, | ||
) -> &'a DevicePath { | ||
let loaded_image_device_path = boot_services | ||
.open_protocol_exclusive::<LoadedImageDevicePath>(boot_services.image_handle()) | ||
.expect("failed to open LoadedImageDevicePath protocol"); | ||
|
||
let mut builder = DevicePathBuilder::with_vec(storage); | ||
for node in loaded_image_device_path.node_iter() { | ||
if node.full_type() == (DeviceType::MEDIA, DeviceSubType::MEDIA_FILE_PATH) { | ||
break; | ||
} | ||
builder = builder.push(&node).unwrap(); | ||
} | ||
builder = builder | ||
.push(&build::media::FilePath { | ||
path_name: cstr16!(r"efi\boot\shell.efi"), | ||
}) | ||
.unwrap(); | ||
builder.finalize().unwrap() | ||
} | ||
|
||
fn subshell_runner(image: Handle, boot_services: &BootServices) { | ||
let mut storage = Vec::new(); | ||
let shell_image_path = get_shell_app_device_path(boot_services, &mut storage); | ||
|
||
// Load the shell app. | ||
let shell_image_handle = boot_services | ||
.load_image( | ||
image, | ||
LoadImageSource::FromDevicePath { | ||
device_path: shell_image_path, | ||
from_boot_manager: false, | ||
}, | ||
) | ||
.expect("failed to load shell app"); | ||
|
||
// Set the command line passed to the shell app so that it will run the | ||
// test-runner app. This automatically turns off the five-second delay. | ||
let mut shell_loaded_image = boot_services | ||
.open_protocol_exclusive::<LoadedImage>(shell_image_handle) | ||
.expect("failed to open LoadedImage protocol"); | ||
let load_options = cstr16!(r"shell.efi test_runner.efi arg1 arg2"); | ||
unsafe { | ||
shell_loaded_image.set_load_options( | ||
load_options.as_ptr().cast(), | ||
load_options.num_bytes() as u32, | ||
); | ||
} | ||
|
||
info!("launching the sub shell app"); | ||
boot_services | ||
.start_image(shell_image_handle) | ||
.expect("failed to launch the shell app"); | ||
} | ||
|
||
pub fn test_subshell(image: Handle, boot_services: &BootServices) { | ||
info!("Running test from subshell"); | ||
|
||
let shell_params = boot_services | ||
.open_protocol_exclusive::<ShellParameters>(image) | ||
.expect("Failed to open ShellParameters protocol"); | ||
|
||
info!("Argc: {}", shell_params.argc); | ||
info!("Args:"); | ||
for arg in shell_params.get_args_slice() { | ||
let arg_str = unsafe { CStr16::from_ptr(*arg) }; | ||
info!(" '{}'", arg_str); | ||
} | ||
|
||
assert_eq!(shell_params.argc, shell_params.get_args_slice().len()); | ||
|
||
let args: Vec<String> = shell_params.get_args().collect(); | ||
assert_eq!(args, vec![r"FS0:\efi\boot\test_runner.efi", "arg1", "arg2"]); | ||
|
||
// test_runner.efi arg1 arg2 | ||
assert_eq!(shell_params.argc, 3); | ||
} |
This file contains hidden or 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 hidden or 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,49 @@ | ||
//! `ShellParams` protocol | ||
|
||
use crate::proto::unsafe_protocol; | ||
use crate::Char16; | ||
use core::ffi::c_void; | ||
use core::slice::from_raw_parts; | ||
|
||
#[cfg(feature = "alloc")] | ||
use crate::CStr16; | ||
#[cfg(feature = "alloc")] | ||
use alloc::string::String; | ||
#[cfg(feature = "alloc")] | ||
use alloc::string::ToString; | ||
|
||
type ShellFileHandle = *const c_void; | ||
|
||
/// The ShellParameters protocol. | ||
#[repr(C)] | ||
#[unsafe_protocol("752f3136-4e16-4fdc-a22a-e5f46812f4ca")] | ||
pub struct ShellParameters { | ||
/// Pointer to a list of arguments | ||
pub argv: *const *const Char16, | ||
/// Number of arguments | ||
pub argc: usize, | ||
/// Handle of the standard input | ||
std_in: ShellFileHandle, | ||
/// Handle of the standard output | ||
std_out: ShellFileHandle, | ||
/// Handle of the standard error output | ||
std_err: ShellFileHandle, | ||
} | ||
|
||
impl ShellParameters { | ||
/// Get an iterator of the shell parameter arguments | ||
#[cfg(feature = "alloc")] | ||
pub fn get_args(&self) -> impl Iterator<Item = String> { | ||
unsafe { | ||
from_raw_parts(self.argv, self.argc) | ||
.iter() | ||
.map(|x| CStr16::from_ptr(*x).to_string()) | ||
} | ||
} | ||
|
||
/// Get a slice of the args, as Char16 pointers | ||
#[must_use] | ||
pub fn get_args_slice(&self) -> &[*const Char16] { | ||
unsafe { from_raw_parts(self.argv, self.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.