Skip to content

Commit

Permalink
Don't panic if windows 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 Oct 30, 2023
1 parent 4115a75 commit de469fd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
47 changes: 29 additions & 18 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 Expand Up @@ -73,7 +82,9 @@ pub fn read_memory(offset: u16, length: u16) -> EcResult<Vec<u8>> {
}

pub fn send_command(command: u16, command_version: u8, data: &[u8]) -> EcResult<Vec<u8>> {
init();
if !init() {
return Err(EcError::DeviceError("Failed to initialize".to_string()));
}

let mut cmd = CrosEcCommand {
version: command_version 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 @@ -685,7 +685,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 de469fd

Please sign in to comment.