diff --git a/framework_lib/src/chromium_ec/mod.rs b/framework_lib/src/chromium_ec/mod.rs index 9da39b6..5c568cb 100644 --- a/framework_lib/src/chromium_ec/mod.rs +++ b/framework_lib/src/chromium_ec/mod.rs @@ -590,39 +590,93 @@ impl CrosEc { } pub fn test_ec_flash_read(&self) -> EcResult<()> { + let mut res = Ok(()); // TODO: Perhaps we could have some more global flag to avoid setting and unsetting that ever time self.flash_notify(MecFlashNotify::AccessSpi)?; - println!(" EC Test"); - println!(" Read first row of flash."); - // Make sure we can read a full flash row + println!(" Read first row of flash (RO FW)"); let data = self.read_ec_flash(0, 0x80).unwrap(); - if data[0..4] != [0x10, 0x00, 0x00, 0xF7] { + + //debug!("Expected 10 00 00 F7 and rest all FF"); + debug!("Expecting 5E 4D 3B 2A and 0x20-0x40 all 00"); + debug!("{:02X?}", data); + println!(" {:02X?}", &data[..8]); + if data.iter().all(|x| *x == 0xFF) { + println!(" Erased!"); + } + + // Make sure we can read a full flash row + let legacy_start = [0x10, 0x00, 0x00, 0xF7]; + let zephyr_start = [0x5E, 0x4D, 0x3B, 0x2A]; + if data[0..4] != legacy_start && data[0..4] != zephyr_start { println!(" INVALID start"); - return Err(EcError::DeviceError("INVALID start".to_string())); + res = Err(EcError::DeviceError("INVALID start".to_string())); } - if !data[4..].iter().all(|x| *x == 0xFF) { + let legacy_comp = !data[4..].iter().all(|x| *x == 0xFF); + let zephyr_comp = !data[0x20..0x40].iter().all(|x| *x == 0x00); + if legacy_comp && zephyr_comp { println!(" INVALID end"); - return Err(EcError::DeviceError("INVALID end".to_string())); + res = Err(EcError::DeviceError("INVALID end".to_string())); + } + + // ----- RW image ------ + println!(" Read first row of RW FW"); + let data = self.read_ec_flash(0x40000, 0x80).unwrap(); + + // //debug!("Expected TODO and rest all FF"); + // debug!("Expecting 80 7D 0C 20 and 0x20-0x2C all 00"); + println!(" {:02X?}", &data[..8]); + if data.iter().all(|x| *x == 0xFF) { + println!(" Erased!"); + res = Err(EcError::DeviceError("RW Erased".to_string())); + } + + // // Make sure we can read a full flash row + // let legacy_start = []; // TODO: + // let zephyr_start = [0x80, 0x7D, 0x0C, 0x20]; + // if data[0..4] != legacy_start && data[0..4] != zephyr_start { + // println!(" INVALID start"); + // res = Err(EcError::DeviceError("INVALID start".to_string())); + // } + // let legacy_comp = !data[4..].iter().all(|x| *x == 0xFF); + // let zephyr_comp = !data[0x20..0x2C].iter().all(|x| *x == 0x00); + // if legacy_comp && zephyr_comp { + // println!(" INVALID end"); + // res = Err(EcError::DeviceError("INVALID end".to_string())); + // } + + // TODO: I don't think there's are magic bytes on zephyr firmware + //println!(" Read first 16 bytes of firmware."); + //// Make sure we can read at an offset and with arbitrary length + //let data = self.read_ec_flash(FLASH_PROGRAM_OFFSET, 16).unwrap(); + //if data[0..4] != [0x50, 0x48, 0x43, 0x4D] { + // println!(" INVALID: {:02X?}", &data[0..3]); + // res = Err(EcError::DeviceError(format!( + // "INVALID: {:02X?}", + // &data[0..3] + // ))); + //} + //debug!("Expected beginning with 50 48 43 4D ('PHCM' in ASCII)"); + //debug!("{:02X?}", data); + + println!(" Read flash flags"); + let data = self.read_ec_flash(0x7F000, 0x80).unwrap(); // NPC + // let data = self.read_ec_flash(0x80000, 0x80).unwrap(); // MEC + let flash_flags_magic = [0xA3, 0xF1, 0x00, 0x00]; + let flash_flags_ver = [0x01, 0x0, 0x00, 0x00]; + // All 0xFF if just reflashed and not reinitialized by EC + if data[0..4] == flash_flags_magic && data[8..12] == flash_flags_ver { + println!(" Valid flash flags"); + } else if data.iter().all(|x| *x == 0xFF) { + println!(" Erased flash flags"); + } else { + println!(" INVALID flash flags"); + res = Err(EcError::DeviceError("INVALID flash flags".to_string())); } - debug!("Expected 10 00 00 F7 and rest all FF"); - debug!("{:02X?}", data); - - println!(" Read first 16 bytes of firmware."); - // Make sure we can read at an offset and with arbitrary length - let data = self.read_ec_flash(FLASH_PROGRAM_OFFSET, 16).unwrap(); - if data[0..4] != [0x50, 0x48, 0x43, 0x4D] { - println!(" INVALID: {:02X?}", &data[0..3]); - return Err(EcError::DeviceError(format!( - "INVALID: {:02X?}", - &data[0..3] - ))); - } - debug!("Expected beginning with 50 48 43 4D ('PHCM' in ASCII)"); - debug!("{:02X?}", data); self.flash_notify(MecFlashNotify::AccessSpiDone)?; - Ok(()) + + res } /// Requests recent console output from EC and constantly asks for more