From 184f89ba53220424ceee750dfb3b25e4076821ee Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Sun, 18 May 2025 13:27:50 +0800 Subject: [PATCH 1/3] Also require root before initializing MEC portio Signed-off-by: Daniel Schaefer --- framework_lib/src/chromium_ec/portio.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/framework_lib/src/chromium_ec/portio.rs b/framework_lib/src/chromium_ec/portio.rs index 152d5f40..df489921 100644 --- a/framework_lib/src/chromium_ec/portio.rs +++ b/framework_lib/src/chromium_ec/portio.rs @@ -90,14 +90,6 @@ fn init() -> bool { Initialized::NotYet => {} } - // First try on MEC - portio_mec::init(); - let ec_id = portio_mec::transfer_read(MEC_MEMMAP_OFFSET + EC_MEMMAP_ID, 2); - if ec_id[0] == b'E' && ec_id[1] == b'C' { - *init = Initialized::SucceededMec; - return true; - } - // In Linux userspace has to first request access to ioports // TODO: Close these again after we're done #[cfg(target_os = "linux")] @@ -106,6 +98,15 @@ fn init() -> bool { *init = Initialized::Failed; return false; } + + // First try on MEC + portio_mec::init(); + let ec_id = portio_mec::transfer_read(MEC_MEMMAP_OFFSET + EC_MEMMAP_ID, 2); + if ec_id[0] == b'E' && ec_id[1] == b'C' { + *init = Initialized::SucceededMec; + return true; + } + #[cfg(target_os = "linux")] unsafe { // 8 for request/response header, 0xFF for response From 056ec3103f6c577e0d809ba8744804159c30d158 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Sun, 18 May 2025 13:28:16 +0800 Subject: [PATCH 2/3] mec init: Fail if ioperm didn't succeed Signed-off-by: Daniel Schaefer --- framework_lib/src/chromium_ec/portio_mec.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/framework_lib/src/chromium_ec/portio_mec.rs b/framework_lib/src/chromium_ec/portio_mec.rs index 9d3664e2..774acb99 100644 --- a/framework_lib/src/chromium_ec/portio_mec.rs +++ b/framework_lib/src/chromium_ec/portio_mec.rs @@ -22,12 +22,20 @@ const _MEC_LPC_DATA_REGISTER1: u16 = 0x0805; const MEC_LPC_DATA_REGISTER2: u16 = 0x0806; const _MEC_LPC_DATA_REGISTER3: u16 = 0x0807; -pub fn init() { +pub fn init() -> bool { #[cfg(target_os = "linux")] unsafe { - ioperm(EC_LPC_ADDR_HOST_DATA as u64, 8, 1); - ioperm(MEC_LPC_ADDRESS_REGISTER0 as u64, 10, 1); + println!("Init MEC"); + let res = ioperm(EC_LPC_ADDR_HOST_DATA as u64, 8, 1); + if res != 0 { + error!("ioperm failed. portio driver is likely block by Linux kernel lockdown mode"); + return false; + } + let res = ioperm(MEC_LPC_ADDRESS_REGISTER0 as u64, 10, 1); + assert_eq!(res, 0); } + + true } // TODO: Create a wrapper From 42c816ef07bef8a5191dd26ede384ca4eaa08ade Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Sun, 18 May 2025 22:17:55 +0800 Subject: [PATCH 3/3] portio: Don't segfault if ioperm failed Have to return and never try again Signed-off-by: Daniel Schaefer --- framework_lib/src/chromium_ec/portio.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/framework_lib/src/chromium_ec/portio.rs b/framework_lib/src/chromium_ec/portio.rs index df489921..d76f8a6b 100644 --- a/framework_lib/src/chromium_ec/portio.rs +++ b/framework_lib/src/chromium_ec/portio.rs @@ -100,7 +100,10 @@ fn init() -> bool { } // First try on MEC - portio_mec::init(); + if !portio_mec::init() { + *init = Initialized::Failed; + return false; + } let ec_id = portio_mec::transfer_read(MEC_MEMMAP_OFFSET + EC_MEMMAP_ID, 2); if ec_id[0] == b'E' && ec_id[1] == b'C' { *init = Initialized::SucceededMec; @@ -113,6 +116,7 @@ fn init() -> bool { let res = ioperm(EC_LPC_ADDR_HOST_ARGS as u64, 8 + 0xFF, 1); if res != 0 { error!("ioperm failed. portio driver is likely block by Linux kernel lockdown mode"); + *init = Initialized::Failed; return false; }