Skip to content

Commit

Permalink
framework_lib: Validate EC firmware is valid before flashing
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Schaefer <[email protected]>
  • Loading branch information
JohnAZoidberg committed May 11, 2024
1 parent 5b9332c commit fdf982c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions framework_lib/src/chromium_ec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! - `portio` - It uses raw port I/O. This works on UEFI and on Linux if the system isn't in lockdown mode (SecureBoot disabled).
//! - `windows` - It uses [DHowett's Windows driver](https://github.com/DHowett/FrameworkWindowsUtils)
use crate::ec_binary;
use crate::os_specific;
use crate::smbios;
#[cfg(feature = "uefi")]
Expand Down Expand Up @@ -399,6 +400,25 @@ impl CrosEc {
/// | 40000 | 3C000 | 39000 | RO Region |
/// | 79000 | 79FFF | 01000 | Flash Flags |
pub fn reflash(&self, data: &[u8], ft: EcFlashType) -> EcResult<()> {
if ft == EcFlashType::Full || ft == EcFlashType::Ro {
if let Some(version) = ec_binary::read_ec_version(data, true) {
println!("EC RO Version in File: {:?}", version.version);
} else {
return Err(EcError::DeviceError(
"File does not contain valid EC RO firmware".to_string(),
));
}
}
if ft == EcFlashType::Full || ft == EcFlashType::Rw {
if let Some(version) = ec_binary::read_ec_version(data, false) {
println!("EC RW Version in File: {:?}", version.version);
} else {
return Err(EcError::DeviceError(
"File does not contain valid EW RO firmware".to_string(),
));
}
}

if ft == EcFlashType::Full || ft == EcFlashType::Ro {
println!("For safety reasons flashing RO firmware is disabled.");
return Ok(());
Expand Down
6 changes: 6 additions & 0 deletions framework_lib/src/ec_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ pub fn read_ec_version(data: &[u8], ro: bool) -> Option<ImageVersionData> {
EC_RW_VER_OFFSET_ZEPHYR
};

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

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

0 comments on commit fdf982c

Please sign in to comment.