Skip to content

Commit aa45491

Browse files
committed
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 <[email protected]>
1 parent 70d6649 commit aa45491

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

framework_lib/src/chromium_ec/windows.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,40 @@ lazy_static! {
2525
static ref DEVICE: Arc<Mutex<Option<DevHandle>>> = Arc::new(Mutex::new(None));
2626
}
2727

28-
fn init() {
28+
fn init() -> bool {
2929
let mut device = DEVICE.lock().unwrap();
3030
if (*device).is_some() {
31-
return;
31+
return true;
3232
}
3333

3434
let path = w!(r"\\.\GLOBALROOT\Device\CrosEC");
35-
unsafe {
36-
*device = Some(DevHandle(
37-
CreateFileW(
38-
path,
39-
FILE_GENERIC_READ.0 | FILE_GENERIC_WRITE.0,
40-
FILE_SHARE_READ | FILE_SHARE_WRITE,
41-
None,
42-
OPEN_EXISTING,
43-
FILE_FLAGS_AND_ATTRIBUTES(0),
44-
None,
45-
)
46-
.unwrap(),
47-
));
48-
}
35+
let res = unsafe {
36+
CreateFileW(
37+
path,
38+
FILE_GENERIC_READ.0 | FILE_GENERIC_WRITE.0,
39+
FILE_SHARE_READ | FILE_SHARE_WRITE,
40+
None,
41+
OPEN_EXISTING,
42+
FILE_FLAGS_AND_ATTRIBUTES(0),
43+
None,
44+
)
45+
};
46+
let handle = match res {
47+
Ok(h) => h,
48+
Err(err) => {
49+
error!("Failed to find Windows driver. {:?}", err);
50+
return false;
51+
}
52+
};
53+
54+
*device = Some(DevHandle(handle));
55+
true
4956
}
5057

5158
pub fn read_memory(offset: u16, length: u16) -> EcResult<Vec<u8>> {
52-
init();
59+
if !init() {
60+
return Err(EcError::DeviceError("Failed to initialize".to_string()));
61+
}
5362
let mut rm = CrosEcReadMem {
5463
offset: offset as u32,
5564
bytes: length as u32,

framework_lib/src/commandline/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,8 @@ fn selftest(ec: &CrosEc) -> Option<()> {
11111111
if let Some(mem) = ec.dump_mem_region() {
11121112
util::print_multiline_buffer(&mem, 0);
11131113
} else {
1114-
println!(" Failed to read EC memory region")
1114+
println!(" Failed to read EC memory region");
1115+
return None;
11151116
}
11161117

11171118
println!(" Checking EC memory mapped magic bytes");

0 commit comments

Comments
 (0)