-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwindows.rs
More file actions
227 lines (205 loc) · 6.96 KB
/
windows.rs
File metadata and controls
227 lines (205 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use num::FromPrimitive;
use std::sync::{Arc, Mutex};
/// Implementation to talk to DHowett's Windows Chrome EC driver
#[allow(unused_imports)]
use windows::{
core::*,
Win32::Foundation::*,
Win32::{
Storage::FileSystem::*,
System::{Ioctl::*, IO::*},
},
};
use crate::chromium_ec::protocol::HEADER_LEN;
use crate::chromium_ec::EC_MEMMAP_SIZE;
use crate::chromium_ec::{EcError, EcResponseStatus, EcResult};
use crate::smbios;
use crate::util::Platform;
// Create a wrapper around HANDLE to mark it as Send.
// I'm not sure, but I think it's safe to do that for this type of HANDL.
#[derive(Copy, Clone)]
struct DevHandle(HANDLE);
unsafe impl Send for DevHandle {}
lazy_static! {
static ref DEVICE: Arc<Mutex<Option<DevHandle>>> = Arc::new(Mutex::new(None));
}
fn init() -> bool {
let mut device = DEVICE.lock().unwrap();
if (*device).is_some() {
return true;
}
let path = w!(r"\\.\GLOBALROOT\Device\CrosEC");
let res = unsafe {
CreateFileW(
path,
FILE_GENERIC_READ.0 | FILE_GENERIC_WRITE.0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
None,
OPEN_EXISTING,
FILE_FLAGS_AND_ATTRIBUTES(0),
None,
)
};
let handle = match res {
Ok(h) => h,
Err(err) => {
let platform = smbios::get_platform();
match platform {
Some(platform @ Platform::IntelGen11)
| Some(platform @ Platform::IntelGen12)
| Some(platform @ Platform::IntelGen13)
| Some(platform @ Platform::Framework13Amd7080)
| Some(platform @ Platform::Framework16Amd7080) => {
eprintln!("The windows driver is not enabled on {:?}.", platform);
eprintln!("Please stay tuned for future BIOS and driver updates.");
eprintln!();
}
Some(Platform::IntelCoreUltra1) => {
println!("The windows driver has been enabled since BIOS 3.06.");
println!("Please install the latest BIOS and drivers");
println!();
}
Some(Platform::Framework13Amd7080) => {
println!("The windows driver has been enabled since BIOS 3.16.");
println!("Please install the latest BIOS and drivers");
println!();
}
Some(Platform::Framework16Amd7080) => {
println!("The windows driver has been enabled since BIOS 3.06.");
println!("Please install the latest BIOS and drivers");
println!();
}
_ => (),
}
error!("Failed to find Windows driver. {:?}", err);
return false;
}
};
*device = Some(DevHandle(handle));
true
}
pub fn read_memory(offset: u16, length: u16) -> EcResult<Vec<u8>> {
if !init() {
return Err(EcError::DeviceError("Failed to initialize".to_string()));
}
let mut rm = CrosEcReadMem {
offset: offset as u32,
bytes: length as u32,
buffer: [0_u8; EC_MEMMAP_SIZE as usize],
};
let const_ptr = &mut rm as *const _ as *const ::core::ffi::c_void;
let mut_ptr = &mut rm as *mut _ as *mut ::core::ffi::c_void;
let ptr_size = std::mem::size_of::<CrosEcReadMem>() as u32;
let retb: u32 = 0;
unsafe {
let device = DEVICE.lock().unwrap();
let device = if let Some(device) = *device {
device
} else {
return EcResult::Err(EcError::DeviceError("No EC device".to_string()));
};
DeviceIoControl(
device.0,
IOCTL_CROSEC_RDMEM,
Some(const_ptr),
ptr_size,
Some(mut_ptr),
ptr_size,
Some(retb as *mut u32),
None,
)
.unwrap();
}
let output = &rm.buffer[..(length as usize)];
Ok(output.to_vec())
}
pub fn send_command(command: u16, command_version: u8, data: &[u8]) -> EcResult<Vec<u8>> {
init();
let mut cmd = CrosEcCommand {
version: command_version as u32,
command: command as u32,
outsize: data.len() as u32,
insize: (CROSEC_CMD_MAX_REQUEST - HEADER_LEN) as u32,
result: 0xFF,
buffer: [0_u8; CROSEC_CMD_MAX_REQUEST],
};
cmd.buffer[0..data.len()].clone_from_slice(data);
let buf_size = std::mem::size_of::<CrosEcCommand>();
// Must keep 8 bytes of space for the EC command request/response headers
let cmd_len = buf_size - HEADER_LEN;
let out_len = buf_size - HEADER_LEN;
let const_ptr = &mut cmd as *const _ as *const ::core::ffi::c_void;
let mut_ptr = &mut cmd as *mut _ as *mut ::core::ffi::c_void;
let mut returned: u32 = 0;
unsafe {
let device = DEVICE.lock().unwrap();
let device = if let Some(device) = *device {
device
} else {
return EcResult::Err(EcError::DeviceError("No EC device".to_string()));
};
DeviceIoControl(
device.0,
IOCTL_CROSEC_XCMD,
Some(const_ptr),
cmd_len.try_into().unwrap(),
Some(mut_ptr),
out_len.try_into().unwrap(),
Some(&mut returned as *mut u32),
None,
)
.unwrap();
}
match FromPrimitive::from_u32(cmd.result) {
None => return Err(EcError::UnknownResponseCode(cmd.result)),
Some(EcResponseStatus::Success) => {}
Some(status) => return Err(EcError::Response(status)),
}
// TODO: Figure out why that's sometimes bigger
let end = std::cmp::min(returned, CROSEC_CMD_MAX_REQUEST as u32);
let out_buffer = &cmd.buffer[..(end as usize)];
Ok(out_buffer.to_vec())
}
const CROSEC_CMD_MAX_REQUEST: usize = 0x100;
const FILE_DEVICE_CROS_EMBEDDED_CONTROLLER: u32 = 0x80EC;
const IOCTL_CROSEC_XCMD: u32 = ctl_code(
FILE_DEVICE_CROS_EMBEDDED_CONTROLLER,
0x801,
METHOD_BUFFERED,
FILE_READ_DATA.0 | FILE_WRITE_DATA.0,
);
const IOCTL_CROSEC_RDMEM: u32 = ctl_code(
FILE_DEVICE_CROS_EMBEDDED_CONTROLLER,
0x802,
METHOD_BUFFERED,
FILE_READ_ACCESS,
);
/// Shadows CTL_CODE from microsoft headers
const fn ctl_code(device_type: u32, function: u32, method: u32, access: u32) -> u32 {
((device_type) << 16) + ((access) << 14) + ((function) << 2) + method
}
#[repr(C)]
struct CrosEcReadMem {
/// Offset in memory mapped region
offset: u32,
/// How many bytes to read
bytes: u32,
/// Buffer to receive requested bytes
buffer: [u8; EC_MEMMAP_SIZE as usize],
}
#[derive(Copy, Clone, Debug)]
#[repr(C)]
struct CrosEcCommand {
/// Command version. Almost always 0
version: u32,
/// Command type
command: u32,
/// Size of request in bytes
outsize: u32,
/// Maximum response size in bytes
insize: u32,
/// Response status code
result: u32,
/// Request and response data buffer
buffer: [u8; CROSEC_CMD_MAX_REQUEST],
}