From aa45491f9fe09ce6630d03bcaf4ce321e094a8f5 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Wed, 17 Jul 2024 14:13:25 +0800 Subject: [PATCH] windows: Don't panic if driver not installed Trying it out without running as admin to cause it to fail accessing the driver. ``` > cargo run --no-default-features --features "windows" -p framework_tool -- --test Self-Test SMBIOS Platform: Framework13AmdAi300 Dump EC memory region [ERROR] Failed to find Windows driver. Error { code: HRESULT(0x80070005), message: "Access is denied." } [ERROR] Failed to communicate with EC. Reason: "Failed to initialize" Failed to read EC memory region FAILED!! Error: "Fail" error: process didn't exit successfully: `target\debug\framework_tool.exe --test` (exit code: 1) > cargo run --no-default-features --features "windows" -p framework_tool -- --test Self-Test SMBIOS Platform: Framework13AmdAi300 Dump EC memory region thread 'main' panicked at framework_lib\src\chromium_ec\windows.rs:46:14: called `Result::unwrap()` on an `Err` value: Error { code: HRESULT(0x80070005), message: "Access is denied." } note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace error: process didn't exit successfully: `target\debug\framework_tool.exe --test` (exit code: 101) ``` Signed-off-by: Daniel Schaefer --- framework_lib/src/chromium_ec/windows.rs | 43 ++++++++++++++---------- framework_lib/src/commandline/mod.rs | 3 +- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/framework_lib/src/chromium_ec/windows.rs b/framework_lib/src/chromium_ec/windows.rs index 4e026663..6cd2db33 100644 --- a/framework_lib/src/chromium_ec/windows.rs +++ b/framework_lib/src/chromium_ec/windows.rs @@ -25,31 +25,40 @@ lazy_static! { static ref DEVICE: Arc>> = Arc::new(Mutex::new(None)); } -fn init() { +fn init() -> bool { let mut device = DEVICE.lock().unwrap(); if (*device).is_some() { - return; + return true; } let path = w!(r"\\.\GLOBALROOT\Device\CrosEC"); - unsafe { - *device = Some(DevHandle( - CreateFileW( - path, - FILE_GENERIC_READ.0 | FILE_GENERIC_WRITE.0, - FILE_SHARE_READ | FILE_SHARE_WRITE, - None, - OPEN_EXISTING, - FILE_FLAGS_AND_ATTRIBUTES(0), - None, - ) - .unwrap(), - )); - } + let res = unsafe { + CreateFileW( + path, + FILE_GENERIC_READ.0 | FILE_GENERIC_WRITE.0, + FILE_SHARE_READ | FILE_SHARE_WRITE, + None, + OPEN_EXISTING, + FILE_FLAGS_AND_ATTRIBUTES(0), + None, + ) + }; + let handle = match res { + Ok(h) => h, + Err(err) => { + error!("Failed to find Windows driver. {:?}", err); + return false; + } + }; + + *device = Some(DevHandle(handle)); + true } pub fn read_memory(offset: u16, length: u16) -> EcResult> { - init(); + if !init() { + return Err(EcError::DeviceError("Failed to initialize".to_string())); + } let mut rm = CrosEcReadMem { offset: offset as u32, bytes: length as u32, diff --git a/framework_lib/src/commandline/mod.rs b/framework_lib/src/commandline/mod.rs index 6dc42884..fa284951 100644 --- a/framework_lib/src/commandline/mod.rs +++ b/framework_lib/src/commandline/mod.rs @@ -1111,7 +1111,8 @@ fn selftest(ec: &CrosEc) -> Option<()> { if let Some(mem) = ec.dump_mem_region() { util::print_multiline_buffer(&mem, 0); } else { - println!(" Failed to read EC memory region") + println!(" Failed to read EC memory region"); + return None; } println!(" Checking EC memory mapped magic bytes");