Skip to content

Commit c2beb78

Browse files
Merge pull request #154 from FrameworkComputer/mec-detection
MEC detection: Fail if permission denied
2 parents 7fbd80d + 42c816e commit c2beb78

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

framework_lib/src/chromium_ec/portio.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,6 @@ fn init() -> bool {
9090
Initialized::NotYet => {}
9191
}
9292

93-
// First try on MEC
94-
portio_mec::init();
95-
let ec_id = portio_mec::transfer_read(MEC_MEMMAP_OFFSET + EC_MEMMAP_ID, 2);
96-
if ec_id[0] == b'E' && ec_id[1] == b'C' {
97-
*init = Initialized::SucceededMec;
98-
return true;
99-
}
100-
10193
// In Linux userspace has to first request access to ioports
10294
// TODO: Close these again after we're done
10395
#[cfg(target_os = "linux")]
@@ -106,12 +98,25 @@ fn init() -> bool {
10698
*init = Initialized::Failed;
10799
return false;
108100
}
101+
102+
// First try on MEC
103+
if !portio_mec::init() {
104+
*init = Initialized::Failed;
105+
return false;
106+
}
107+
let ec_id = portio_mec::transfer_read(MEC_MEMMAP_OFFSET + EC_MEMMAP_ID, 2);
108+
if ec_id[0] == b'E' && ec_id[1] == b'C' {
109+
*init = Initialized::SucceededMec;
110+
return true;
111+
}
112+
109113
#[cfg(target_os = "linux")]
110114
unsafe {
111115
// 8 for request/response header, 0xFF for response
112116
let res = ioperm(EC_LPC_ADDR_HOST_ARGS as u64, 8 + 0xFF, 1);
113117
if res != 0 {
114118
error!("ioperm failed. portio driver is likely block by Linux kernel lockdown mode");
119+
*init = Initialized::Failed;
115120
return false;
116121
}
117122

framework_lib/src/chromium_ec/portio_mec.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ const _MEC_LPC_DATA_REGISTER1: u16 = 0x0805;
2222
const MEC_LPC_DATA_REGISTER2: u16 = 0x0806;
2323
const _MEC_LPC_DATA_REGISTER3: u16 = 0x0807;
2424

25-
pub fn init() {
25+
pub fn init() -> bool {
2626
#[cfg(target_os = "linux")]
2727
unsafe {
28-
ioperm(EC_LPC_ADDR_HOST_DATA as u64, 8, 1);
29-
ioperm(MEC_LPC_ADDRESS_REGISTER0 as u64, 10, 1);
28+
println!("Init MEC");
29+
let res = ioperm(EC_LPC_ADDR_HOST_DATA as u64, 8, 1);
30+
if res != 0 {
31+
error!("ioperm failed. portio driver is likely block by Linux kernel lockdown mode");
32+
return false;
33+
}
34+
let res = ioperm(MEC_LPC_ADDRESS_REGISTER0 as u64, 10, 1);
35+
assert_eq!(res, 0);
3036
}
37+
38+
true
3139
}
3240

3341
// TODO: Create a wrapper

0 commit comments

Comments
 (0)