Skip to content

Commit 68cfb8d

Browse files
committed
Added tests for set_cur_dir and get_cur_dir
1 parent 350e505 commit 68cfb8d

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

uefi-test-runner/src/proto/shell.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use uefi::boot;
2-
use uefi::data_types::Char16;
3-
use uefi::proto::shell::{Shell, ShellFileHandle};
2+
use uefi::proto::shell::Shell;
43
use uefi::CStr16;
4+
use uefi_raw::Status;
55

66
pub fn test() {
77
info!("Running shell protocol tests");
@@ -15,13 +15,14 @@ pub fn test() {
1515
// let mut test_buf = [0u16; 12];
1616
// let test_str = CStr16::from_str_with_buf("test", &mut test_buf).unwrap();
1717

18+
let mut test_buf = [0u16; 128];
19+
1820
/* Test retrieving list of environment variable names (null input) */
1921
let cur_env_vec = shell
2022
.get_env(None)
2123
.expect("Could not get environment variable")
2224
.vec()
2325
.unwrap();
24-
let mut test_buf = [0u16; 128];
2526
assert_eq!(
2627
*cur_env_vec.get(0).unwrap(),
2728
CStr16::from_str_with_buf("path", &mut test_buf).unwrap()
@@ -52,23 +53,18 @@ pub fn test() {
5253
.unwrap();
5354
assert_eq!(cur_env_str, test_val);
5455

55-
// let mut cur_fs_buf = [0u16; 32];
56-
// let cur_fs_str = CStr16::from_str_with_buf("", &mut cur_fs_buf).unwrap();
57-
// info!("cur_fs_str size 1: {}", cur_fs_str.num_chars());
56+
/* Test setting and getting current directory */
57+
let mut fs_buf = [0u16; 16];
58+
let fs_var = CStr16::from_str_with_buf("fs0:", &mut fs_buf).unwrap();
59+
let mut dir_buf = [0u16; 32];
60+
let dir_var = CStr16::from_str_with_buf("/", &mut dir_buf).unwrap();
61+
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
62+
assert_eq!(status, Status::SUCCESS);
5863

59-
// let cur_fs_str = shell.get_cur_dir(None).expect("Could not get the current file system mapping");
60-
// info!("cur_fs_str size: {}", cur_fs_str.num_chars());
61-
// info!("cur_fs_str: {}", cur_fs_str);
62-
63-
// for (i, c) in cur_fs_str.iter().enumerate() {
64-
// info!("cur_fs_str: i: {}, c: {}", i, c);
65-
// }
64+
let cur_fs_str = shell.get_cur_dir(Some(fs_var)).expect("Could not get the current file system mapping");
65+
let expected_fs_str = CStr16::from_str_with_buf("FS0:\\", &mut test_buf).unwrap();
66+
assert_eq!(cur_fs_str, expected_fs_str);
6667

67-
// unsafe {
68-
// info!("cur_fs_str: {}", cur_fs_str);
69-
// let mut expected_fs_str_buf = [0u16; 32];
70-
// assert_eq!(cur_fs_str, CStr16::from_str_with_buf("", &mut expected_fs_str_buf).unwrap());
71-
// //
7268
// Create a file
7369
// let status = shell.create_file(test_str, 0).expect("Could not create file");
7470
// let mut size: u64 = 0;

uefi/src/proto/shell/mod.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct Shell {
3535
set_map: usize,
3636

3737
get_cur_dir: extern "efiapi" fn(file_system_mapping: *const Char16) -> *const Char16,
38-
set_cur_dir: usize,
38+
set_cur_dir: extern "efiapi" fn(file_system: *const Char16, directory: *const Char16) -> Status,
3939
open_file_list: usize,
4040
free_file_list: extern "efiapi" fn(file_list: *mut *mut ShellFileInfo),
4141
remove_dup_in_file_list: usize,
@@ -221,11 +221,20 @@ impl Shell {
221221
(self.set_env)(name_ptr, value_ptr, volatile)
222222
}
223223

224-
/// TODO
224+
/// Returns the current directory on the specified device
225+
///
226+
/// # Arguments
227+
///
228+
/// * `file_system_mapping` - The file system mapping for which to get
229+
/// the current directory
230+
/// # Returns
231+
///
232+
/// * `Some(cwd)` - CStr16 containing the current working directory
233+
/// * `None` - Could not retrieve current directory
225234
#[must_use]
226235
pub fn get_cur_dir<'a>(&'a self, file_system_mapping: Option<&CStr16>) -> Option<&'a CStr16> {
227236
let mapping_ptr: *const Char16 =
228-
file_system_mapping.map_or(core::ptr::null(), |x| (x as *const CStr16).cast());
237+
file_system_mapping.map_or(ptr::null(), |x| (x.as_ptr()));
229238
let cur_dir = (self.get_cur_dir)(mapping_ptr);
230239
if cur_dir.is_null() {
231240
None
@@ -234,6 +243,26 @@ impl Shell {
234243
}
235244
}
236245

246+
/// Changes the current directory on the specified device
247+
///
248+
/// # Arguments
249+
///
250+
/// * `file_system` - Pointer to the file system's mapped name.
251+
/// * `directory` - Points to the directory on the device specified by
252+
/// `file_system`.
253+
/// # Returns
254+
///
255+
/// * `Status::SUCCESS` The directory was successfully set
256+
///
257+
/// # Errors
258+
///
259+
/// * `Status::EFI_NOT_FOUND` The directory does not exist
260+
pub fn set_cur_dir(&self, file_system: Option<&CStr16>, directory: Option<&CStr16>) -> Status {
261+
let fs_ptr: *const Char16 = file_system.map_or(ptr::null(), |x| (x.as_ptr()));
262+
let dir_ptr: *const Char16 = directory.map_or(ptr::null(), |x| (x.as_ptr()));
263+
(self.set_cur_dir)(fs_ptr, dir_ptr)
264+
}
265+
237266
/// Returns `true` if any script files are currently being processed.
238267
#[must_use]
239268
pub fn batch_is_active(&self) -> bool {

0 commit comments

Comments
 (0)