Skip to content

Commit

Permalink
windows: Don't panic if driver not installed
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Schaefer <[email protected]>
  • Loading branch information
JohnAZoidberg committed Jul 17, 2024
1 parent 4c0b080 commit 03e8fc9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
43 changes: 26 additions & 17 deletions framework_lib/src/chromium_ec/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,40 @@ lazy_static! {
static ref DEVICE: Arc<Mutex<Option<HANDLE>>> = 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(
CreateFileW(
path,
FILE_GENERIC_READ | FILE_GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
None,
OPEN_EXISTING,
FILE_FLAGS_AND_ATTRIBUTES(0),
None,
)
.unwrap(),
);
}
let res = unsafe {
CreateFileW(
path,
FILE_GENERIC_READ | FILE_GENERIC_WRITE,
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(handle);
true
}

pub fn read_memory(offset: u16, length: u16) -> EcResult<Vec<u8>> {
init();
if !init() {
return Err(EcError::DeviceError("Failed to initialize".to_string()));
}
let mut rm = CrosEcReadMem {
offset: offset as u32,
bytes: length as u32,
Expand Down
3 changes: 2 additions & 1 deletion framework_lib/src/commandline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,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");
Expand Down

0 comments on commit 03e8fc9

Please sign in to comment.