| 85 | } |
| 86 | |
| 87 | pub fn read_memory(offset: u16, length: u16) -> EcResult<Vec<u8>> { |
| 88 | if !init() { |
| 89 | return Err(EcError::DeviceError("Failed to initialize".to_string())); |
| 90 | } |
| 91 | let mut rm = CrosEcReadMem { |
| 92 | offset: offset as u32, |
| 93 | bytes: length as u32, |
| 94 | buffer: [0_u8; EC_MEMMAP_SIZE as usize], |
| 95 | }; |
| 96 | |
| 97 | let const_ptr = &mut rm as *const _ as *const ::core::ffi::c_void; |
| 98 | let mut_ptr = &mut rm as *mut _ as *mut ::core::ffi::c_void; |
| 99 | let ptr_size = std::mem::size_of::<CrosEcReadMem>() as u32; |
| 100 | let retb: u32 = 0; |
| 101 | unsafe { |
| 102 | let device = DEVICE.lock().unwrap(); |
| 103 | let device = if let Some(device) = *device { |
| 104 | device |
| 105 | } else { |
| 106 | return EcResult::Err(EcError::DeviceError("No EC device".to_string())); |
| 107 | }; |
| 108 | DeviceIoControl( |
| 109 | device.0, |
| 110 | IOCTL_CROSEC_RDMEM, |
| 111 | Some(const_ptr), |
| 112 | ptr_size, |
| 113 | Some(mut_ptr), |
| 114 | ptr_size, |
| 115 | Some(retb as *mut u32), |
| 116 | None, |
| 117 | ) |
| 118 | .unwrap(); |
| 119 | } |
| 120 | let output = &rm.buffer[..(length as usize)]; |
| 121 | Ok(output.to_vec()) |
| 122 | } |
| 123 | |
| 124 | pub fn send_command(command: u16, command_version: u8, data: &[u8]) -> EcResult<Vec<u8>> { |
| 125 | init(); |