Skip to content

Commit

Permalink
uefi: Fix filesystem usage
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Schaefer <[email protected]>
  • Loading branch information
JohnAZoidberg committed Aug 8, 2024
1 parent d13f8d4 commit 26c4d84
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 49 deletions.
8 changes: 4 additions & 4 deletions framework_lib/src/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ pub fn dump_winux_image(data: &[u8], header: &DisplayCapsule, filename: &str) {
}
#[cfg(feature = "uefi")]
{
//let ret = crate::uefi::fs::shell_write_file(filename, image);
//if let Err(err) = ret {
// println!("Failed to dump winux image: {:?}", err);
//}
let ret = crate::uefi::fs::shell_write_file(filename, image);
if let Err(err) = ret {
println!("Failed to dump winux image: {:?}", err);
}
}
}

Expand Down
20 changes: 10 additions & 10 deletions framework_lib/src/commandline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ fn print_esrt() {

fn flash_ec(ec: &CrosEc, ec_bin_path: &str, flash_type: EcFlashType) {
#[cfg(feature = "uefi")]
let data: Option<Vec<u8>> = None; //crate::uefi::fs::shell_read_file(ec_bin_path);
let data: Option<Vec<u8>> = crate::uefi::fs::shell_read_file(ec_bin_path);
#[cfg(not(feature = "uefi"))]
let data: Option<Vec<u8>> = {
let _data = match fs::read(ec_bin_path) {
Expand Down Expand Up @@ -458,10 +458,10 @@ fn dump_ec_flash(ec: &CrosEc, dump_path: &str) {
}
#[cfg(feature = "uefi")]
{
//let ret = crate::uefi::fs::shell_write_file(dump_path, &flash_bin);
//if ret.is_err() {
// println!("Failed to dump EC FW image.");
//}
let ret = crate::uefi::fs::shell_write_file(dump_path, &flash_bin);
if ret.is_err() {
println!("Failed to dump EC FW image.");
}
}
}

Expand Down Expand Up @@ -653,7 +653,7 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
// raw_command(&args[1..]);
} else if let Some(pd_bin_path) = &args.pd_bin {
#[cfg(feature = "uefi")]
let data: Option<Vec<u8>> = None; //crate::uefi::fs::shell_read_file(pd_bin_path);
let data: Option<Vec<u8>> = crate::uefi::fs::shell_read_file(pd_bin_path);
#[cfg(not(feature = "uefi"))]
let data = match fs::read(pd_bin_path) {
Ok(data) => Some(data),
Expand All @@ -672,7 +672,7 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
}
} else if let Some(ec_bin_path) = &args.ec_bin {
#[cfg(feature = "uefi")]
let data: Option<Vec<u8>> = None; //crate::uefi::fs::shell_read_file(ec_bin_path);
let data: Option<Vec<u8>> = crate::uefi::fs::shell_read_file(ec_bin_path);
#[cfg(not(feature = "uefi"))]
let data = match fs::read(ec_bin_path) {
Ok(data) => Some(data),
Expand All @@ -691,7 +691,7 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
}
} else if let Some(capsule_path) = &args.capsule {
#[cfg(feature = "uefi")]
let data: Option<Vec<u8>> = None; //crate::uefi::fs::shell_read_file(capsule_path);
let data: Option<Vec<u8>> = crate::uefi::fs::shell_read_file(capsule_path);
#[cfg(not(feature = "uefi"))]
let data = match fs::read(capsule_path) {
Ok(data) => Some(data),
Expand Down Expand Up @@ -720,7 +720,7 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
}
} else if let Some(capsule_path) = &args.ho2_capsule {
#[cfg(feature = "uefi")]
let data = Some([]); //crate::uefi::fs::shell_read_file(capsule_path);
let data = crate::uefi::fs::shell_read_file(capsule_path);
#[cfg(not(feature = "uefi"))]
let data = match fs::read(capsule_path) {
Ok(data) => Some(data),
Expand Down Expand Up @@ -759,7 +759,7 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
} else if let Some(hash_file) = &args.hash {
println!("Hashing file: {}", hash_file);
#[cfg(feature = "uefi")]
let data = Some([]); //crate::uefi::fs::shell_read_file(hash_file);
let data = crate::uefi::fs::shell_read_file(hash_file);
#[cfg(not(feature = "uefi"))]
let data = match fs::read(hash_file) {
Ok(data) => Some(data),
Expand Down
2 changes: 1 addition & 1 deletion framework_lib/src/commandline/uefi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn get_args(bt: &BootServices) -> Vec<String> {
let shell_params = bt.open_protocol_exclusive::<ShellParameters>(bt.image_handle());
let shell_params = match shell_params {
Ok(s) => s,
Err(e) => {
Err(_e) => {
error!("Failed to get ShellParameters protocol");
// TODO: Should probably return an error here
return vec![];
Expand Down
18 changes: 11 additions & 7 deletions framework_lib/src/uefi/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use uefi::prelude::*;
use uefi::proto::shell::FileOpenMode;
use uefi::Result;

use super::find_shell_handle;
use super::shell::find_shell_handle;

pub fn wstr(string: &str) -> Vec<u16> {
let mut wstring = vec![];
Expand All @@ -18,7 +18,9 @@ pub fn wstr(string: &str) -> Vec<u16> {
}

pub fn shell_read_file(path: &str) -> Option<Vec<u8>> {
let shell = if let Some(shell) = find_shell_handle() {
let st = uefi::table::system_table_boot()?;
let bt = st.boot_services();
let shell = if let Some(shell) = find_shell_handle(bt) {
shell
} else {
println!("Failed to open Shell Protocol");
Expand Down Expand Up @@ -60,11 +62,13 @@ pub fn shell_read_file(path: &str) -> Option<Vec<u8>> {
}

pub fn shell_write_file(path: &str, data: &[u8]) -> Result {
let shell = if let Some(shell) = find_shell_handle() {
let st = uefi::table::system_table_boot().ok_or(Status::LOAD_ERROR)?;
let bt = st.boot_services();
let shell = if let Some(shell) = find_shell_handle(bt) {
shell
} else {
println!("Failed to open Shell Protocol");
return Status::LOAD_ERROR.into();
return Status::LOAD_ERROR.to_result();
};

debug_assert_eq!(shell.major_version, 2);
Expand All @@ -77,13 +81,13 @@ pub fn shell_write_file(path: &str, data: &[u8]) -> Result {
handle
} else {
println!("Failed to open file: {:?}", handle);
return Status::LOAD_ERROR.into();
return Status::LOAD_ERROR.to_result();
};
let handle = if let Some(handle) = handle {
handle
} else {
println!("Failed to open file: {:?}", handle);
return Status::LOAD_ERROR.into();
return Status::LOAD_ERROR.to_result();
};
let file_handle = handle;

Expand Down Expand Up @@ -128,5 +132,5 @@ pub fn shell_write_file(path: &str, data: &[u8]) -> Result {

shell.close_file(file_handle).unwrap();

Status::SUCCESS.into()
Status::SUCCESS.to_result()
}
2 changes: 1 addition & 1 deletion framework_lib/src/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::slice;
use log::{debug, error, info, trace};
use uefi::table::cfg::{SMBIOS3_GUID, SMBIOS_GUID};

//pub mod fs;
pub mod fs;
pub mod shell;

#[repr(packed)]
Expand Down
49 changes: 23 additions & 26 deletions framework_lib/src/uefi/shell.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams, SearchType};
use uefi::table::boot::{BootServices, OpenProtocolAttributes, OpenProtocolParams, SearchType};

#[allow(unused_imports)]
use log::{debug, error, info, trace};
use uefi::proto::shell::Shell;
use uefi::Identify;

//use uefi::table::boot::ScopedProtocol;
//fn find_shell_handle() -> Option<ScopedProtocol<'static, Shell>> {
// let st = uefi::table::system_table_boot()?;
// let boot_services = st.boot_services();
// let shell_handles = boot_services.locate_handle_buffer(SearchType::ByProtocol(&Shell::GUID));
// if let Ok(sh_buf) = shell_handles {
// for handle in &*sh_buf {
// return Some(unsafe {
// boot_services
// .open_protocol::<Shell>(
// OpenProtocolParams {
// handle: *handle,
// agent: boot_services.image_handle(),
// controller: None,
// },
// OpenProtocolAttributes::GetProtocol,
// )
// .expect("Failed to open Shell handle")
// });
// }
// } else {
// panic!("No shell handle found!");
// }
// None
//}
use uefi::table::boot::ScopedProtocol;
pub fn find_shell_handle(bt: &BootServices) -> Option<ScopedProtocol<Shell>> {
let shell_handles = bt.locate_handle_buffer(SearchType::ByProtocol(&Shell::GUID));
if let Ok(sh_buf) = shell_handles {
if let Some(handle) = (*sh_buf).iter().next() {
return Some(unsafe {
bt.open_protocol::<Shell>(
OpenProtocolParams {
handle: *handle,
agent: bt.image_handle(),
controller: None,
},
OpenProtocolAttributes::GetProtocol,
)
.expect("Failed to open Shell handle")
});
}
} else {
panic!("No shell handle found!");
}
None
}

/// Returns true when the execution break was requested, false otherwise
pub fn get_execution_break_flag() -> Option<bool> {
Expand Down

0 comments on commit 26c4d84

Please sign in to comment.