Skip to content

Commit 6824174

Browse files
Merge pull request #100 from FrameworkComputer/win-no-panic
windows: Don't panic if driver not installed
2 parents 70d6649 + aa45491 commit 6824174

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)