Skip to content

Enhancement/efi shell interface #1679

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 157 additions & 2 deletions uefi-test-runner/src/proto/shell.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,168 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use uefi::boot;
use uefi::boot::ScopedProtocol;
use uefi::proto::shell::Shell;
use uefi::{boot, cstr16};

/// Test `var()`, `vars()`, and `set_var()`
pub fn test_env(shell: &ScopedProtocol<Shell>) {
/* Test retrieving list of environment variable names */
let mut cur_env_vec = shell.vars();
assert_eq!(cur_env_vec.next().unwrap().0, cstr16!("path"));
// check pre-defined shell variables; see UEFI Shell spec
assert_eq!(cur_env_vec.next().unwrap().0, cstr16!("nonesting"));
let cur_env_vec = shell.vars();
let default_len = cur_env_vec.count();

/* Test setting and getting a specific environment variable */
let cur_env_vec = shell.vars();
let test_var = cstr16!("test_var");
let test_val = cstr16!("test_val");
assert!(shell.var(test_var).is_none());
let status = shell.set_var(test_var, test_val, false);
assert!(status.is_ok());
let cur_env_str = shell
.var(test_var)
.expect("Could not get environment variable");
assert_eq!(cur_env_str, test_val);

let mut found_var = false;
for (env_var, _) in cur_env_vec {
if env_var == test_var {
found_var = true;
}
}
assert!(!found_var);
let cur_env_vec = shell.vars();
let mut found_var = false;
for (env_var, _) in cur_env_vec {
if env_var == test_var {
found_var = true;
}
}
assert!(found_var);

let cur_env_vec = shell.vars();
assert_eq!(cur_env_vec.count(), default_len + 1);

/* Test deleting environment variable */
let test_val = cstr16!("");
let status = shell.set_var(test_var, test_val, false);
assert!(status.is_ok());
assert!(shell.var(test_var).is_none());

let cur_env_vec = shell.vars();
let mut found_var = false;
for (env_var, _) in cur_env_vec {
if env_var == test_var {
found_var = true;
}
}
assert!(!found_var);
let cur_env_vec = shell.vars();
assert_eq!(cur_env_vec.count(), default_len);
}

/// Test `current_dir()` and `set_current_dir()`
pub fn test_cur_dir(shell: &ScopedProtocol<Shell>) {
/* Test setting and getting current file system and current directory */
let fs_var = cstr16!("fs0:");
let dir_var = cstr16!("/");
let status = shell.set_current_dir(Some(fs_var), Some(dir_var));
assert!(status.is_ok());

let cur_fs_str = shell
.current_dir(Some(fs_var))
.expect("Could not get the current file system mapping");
let expected_fs_str = cstr16!("FS0:\\");
assert_eq!(cur_fs_str, expected_fs_str);

// Changing current file system
let fs_var = cstr16!("fs1:");
let dir_var = cstr16!("/");
let status = shell.set_current_dir(Some(fs_var), Some(dir_var));
assert!(status.is_ok());

let cur_fs_str = shell
.current_dir(Some(fs_var))
.expect("Could not get the current file system mapping");
assert_ne!(cur_fs_str, expected_fs_str);
let expected_fs_str = cstr16!("FS1:\\");
assert_eq!(cur_fs_str, expected_fs_str);

// Changing current file system and current directory
let fs_var = cstr16!("fs0:");
let dir_var = cstr16!("efi/");
let status = shell.set_current_dir(Some(fs_var), Some(dir_var));
assert!(status.is_ok());

let cur_fs_str = shell
.current_dir(Some(fs_var))
.expect("Could not get the current file system mapping");
assert_ne!(cur_fs_str, expected_fs_str);
let expected_fs_str = cstr16!("FS0:\\efi");
assert_eq!(cur_fs_str, expected_fs_str);

/* Test current working directory cases */

// At this point, the current working file system has not been set
// So we expect a NULL output
assert!(shell.current_dir(None).is_none());

// Setting the current working file system and current working directory
let dir_var = cstr16!("fs0:/");
let status = shell.set_current_dir(None, Some(dir_var));
assert!(status.is_ok());
let cur_fs_str = shell
.current_dir(Some(fs_var))
.expect("Could not get the current file system mapping");
let expected_fs_str = cstr16!("FS0:");
assert_eq!(cur_fs_str, expected_fs_str);

let cur_fs_str = shell
.current_dir(None)
.expect("Could not get the current file system mapping");
assert_eq!(cur_fs_str, expected_fs_str);

// Changing current working directory
let dir_var = cstr16!("/efi");
let status = shell.set_current_dir(None, Some(dir_var));
assert!(status.is_ok());
let cur_fs_str = shell
.current_dir(Some(fs_var))
.expect("Could not get the current file system mapping");
let expected_fs_str = cstr16!("FS0:\\efi");
assert_eq!(cur_fs_str, expected_fs_str);
let cur_fs_str = shell
.current_dir(None)
.expect("Could not get the current file system mapping");
assert_eq!(cur_fs_str, expected_fs_str);

// Changing current directory in a non-current working file system
let fs_var = cstr16!("fs0:");
let dir_var = cstr16!("efi/tools");
let status = shell.set_current_dir(Some(fs_var), Some(dir_var));
assert!(status.is_ok());
let cur_fs_str = shell
.current_dir(None)
.expect("Could not get the current file system mapping");
assert_ne!(cur_fs_str, expected_fs_str);

let expected_fs_str = cstr16!("FS0:\\efi\\tools");
let cur_fs_str = shell
.current_dir(Some(fs_var))
.expect("Could not get the current file system mapping");
assert_eq!(cur_fs_str, expected_fs_str);
}

pub fn test() {
info!("Running shell protocol tests");

let handle = boot::get_handle_for_protocol::<Shell>().expect("No Shell handles");

let mut _shell =
let shell =
boot::open_protocol_exclusive::<Shell>(handle).expect("Failed to open Shell protocol");

test_env(&shell);
test_cur_dir(&shell);
}
Loading
Loading