Skip to content

Commit fdf982c

Browse files
committed
framework_lib: Validate EC firmware is valid before flashing
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 5b9332c commit fdf982c

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

framework_lib/src/chromium_ec/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! - `portio` - It uses raw port I/O. This works on UEFI and on Linux if the system isn't in lockdown mode (SecureBoot disabled).
99
//! - `windows` - It uses [DHowett's Windows driver](https://github.com/DHowett/FrameworkWindowsUtils)
1010
11+
use crate::ec_binary;
1112
use crate::os_specific;
1213
use crate::smbios;
1314
#[cfg(feature = "uefi")]
@@ -399,6 +400,25 @@ impl CrosEc {
399400
/// | 40000 | 3C000 | 39000 | RO Region |
400401
/// | 79000 | 79FFF | 01000 | Flash Flags |
401402
pub fn reflash(&self, data: &[u8], ft: EcFlashType) -> EcResult<()> {
403+
if ft == EcFlashType::Full || ft == EcFlashType::Ro {
404+
if let Some(version) = ec_binary::read_ec_version(data, true) {
405+
println!("EC RO Version in File: {:?}", version.version);
406+
} else {
407+
return Err(EcError::DeviceError(
408+
"File does not contain valid EC RO firmware".to_string(),
409+
));
410+
}
411+
}
412+
if ft == EcFlashType::Full || ft == EcFlashType::Rw {
413+
if let Some(version) = ec_binary::read_ec_version(data, false) {
414+
println!("EC RW Version in File: {:?}", version.version);
415+
} else {
416+
return Err(EcError::DeviceError(
417+
"File does not contain valid EW RO firmware".to_string(),
418+
));
419+
}
420+
}
421+
402422
if ft == EcFlashType::Full || ft == EcFlashType::Ro {
403423
println!("For safety reasons flashing RO firmware is disabled.");
404424
return Ok(());

framework_lib/src/ec_binary.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ pub fn read_ec_version(data: &[u8], ro: bool) -> Option<ImageVersionData> {
182182
EC_RW_VER_OFFSET_ZEPHYR
183183
};
184184

185+
if data.len() < offset + core::mem::size_of::<_ImageVersionData>() {
186+
return None;
187+
}
185188
let v: _ImageVersionData = unsafe { std::ptr::read(data[offset..].as_ptr() as *const _) };
186189
if v.cookie1 != CROS_EC_IMAGE_DATA_COOKIE1 {
187190
debug!("Failed to find Cookie 1. Found: {:X?}", { v.cookie1 });
@@ -191,6 +194,9 @@ pub fn read_ec_version(data: &[u8], ro: bool) -> Option<ImageVersionData> {
191194
return parse_ec_version(&v);
192195
}
193196

197+
if data.len() < offset_zephyr + core::mem::size_of::<_ImageVersionData>() {
198+
return None;
199+
}
194200
let v: _ImageVersionData =
195201
unsafe { std::ptr::read(data[offset_zephyr..].as_ptr() as *const _) };
196202
if v.cookie1 != CROS_EC_IMAGE_DATA_COOKIE1 {

0 commit comments

Comments
 (0)