Skip to content

Commit 1f69b28

Browse files
committed
chromium_ec: Improve flash reading selftest
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 9e32643 commit 1f69b28

File tree

1 file changed

+98
-24
lines changed
  • framework_lib/src/chromium_ec

1 file changed

+98
-24
lines changed

framework_lib/src/chromium_ec/mod.rs

Lines changed: 98 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const FLASH_RO_BASE: u32 = 0x0;
5757
const FLASH_RO_SIZE: u32 = 0x3C000;
5858
const FLASH_RW_BASE: u32 = 0x40000;
5959
const FLASH_RW_SIZE: u32 = 0x39000;
60-
const FLASH_PROGRAM_OFFSET: u32 = 0x1000;
60+
const _FLASH_PROGRAM_OFFSET: u32 = 0x1000;
6161

6262
#[derive(Clone, Debug, PartialEq)]
6363
pub enum EcFlashType {
@@ -594,39 +594,113 @@ impl CrosEc {
594594
}
595595

596596
pub fn test_ec_flash_read(&self) -> EcResult<()> {
597+
let mut res = Ok(());
597598
// TODO: Perhaps we could have some more global flag to avoid setting and unsetting that ever time
598599
self.flash_notify(MecFlashNotify::AccessSpi)?;
599600

600-
println!(" EC Test");
601-
println!(" Read first row of flash.");
602-
// Make sure we can read a full flash row
601+
// ===== Test 1 =====
602+
// Read the first row of flash.
603+
// It's the beginning of RO firmware
604+
println!(" Read first row of flash (RO FW)");
603605
let data = self.read_ec_flash(0, 0x80).unwrap();
604-
if data[0..4] != [0x10, 0x00, 0x00, 0xF7] {
606+
607+
debug!("{:02X?}", data);
608+
println!(" {:02X?}", &data[..8]);
609+
if data.iter().all(|x| *x == 0xFF) {
610+
println!(" Erased!");
611+
}
612+
613+
// 4 magic bytes at the beginning
614+
let legacy_start = [0x10, 0x00, 0x00, 0xF7];
615+
// TODO: Does zephyr always start like this?
616+
let zephyr_start = [0x5E, 0x4D, 0x3B, 0x2A];
617+
if data[0..4] != legacy_start && data[0..4] != zephyr_start {
605618
println!(" INVALID start");
606-
return Err(EcError::DeviceError("INVALID start".to_string()));
619+
res = Err(EcError::DeviceError("INVALID start".to_string()));
607620
}
608-
if !data[4..].iter().all(|x| *x == 0xFF) {
621+
// Legacy EC is all 0xFF until the end of the row
622+
// Zephyr EC I'm not quite sure byt it has a section of 0x00
623+
let legacy_comp = !data[4..].iter().all(|x| *x == 0xFF);
624+
let zephyr_comp = !data[0x20..0x40].iter().all(|x| *x == 0x00);
625+
if legacy_comp && zephyr_comp {
609626
println!(" INVALID end");
610-
return Err(EcError::DeviceError("INVALID end".to_string()));
627+
res = Err(EcError::DeviceError("INVALID end".to_string()));
628+
}
629+
630+
// ===== Test 2 =====
631+
// DISABLED
632+
// TODO: Haven't figure out a pattern yet
633+
//
634+
// Read the first row of the second half of flash
635+
// It's the beginning of RW firmware
636+
println!(" Read first row of RW FW");
637+
let data = self.read_ec_flash(0x40000, 0x80).unwrap();
638+
639+
println!(" {:02X?}", &data[..8]);
640+
if data.iter().all(|x| *x == 0xFF) {
641+
println!(" Erased!");
642+
res = Err(EcError::DeviceError("RW Erased".to_string()));
643+
}
644+
645+
// TODO: How can we identify if the RO image is valid?
646+
// //debug!("Expected TODO and rest all FF");
647+
// debug!("Expecting 80 7D 0C 20 and 0x20-0x2C all 00");
648+
// let legacy_start = []; // TODO
649+
// let zephyr_start = [0x80, 0x7D, 0x0C, 0x20];
650+
// if data[0..4] != legacy_start && data[0..4] != zephyr_start {
651+
// println!(" INVALID start");
652+
// res = Err(EcError::DeviceError("INVALID start".to_string()));
653+
// }
654+
// let legacy_comp = !data[4..].iter().all(|x| *x == 0xFF);
655+
// let zephyr_comp = !data[0x20..0x2C].iter().all(|x| *x == 0x00);
656+
// if legacy_comp && zephyr_comp {
657+
// println!(" INVALID end");
658+
// res = Err(EcError::DeviceError("INVALID end".to_string()));
659+
// }
660+
661+
// ===== Test 3 =====
662+
// DISABLED
663+
//
664+
// MEC EC has program code at 0x1000 with magic bytes.
665+
// Everything before is probably a header.
666+
// TODO: I don't think there's are magic bytes on zephyr firmware
667+
//
668+
//println!(" Read first 16 bytes of firmware.");
669+
//// Make sure we can read at an offset and with arbitrary length
670+
//let data = self.read_ec_flash(FLASH_PROGRAM_OFFSET, 16).unwrap();
671+
//if data[0..4] != [0x50, 0x48, 0x43, 0x4D] {
672+
// println!(" INVALID: {:02X?}", &data[0..3]);
673+
// res = Err(EcError::DeviceError(format!(
674+
// "INVALID: {:02X?}",
675+
// &data[0..3]
676+
// )));
677+
//}
678+
//debug!("Expected beginning with 50 48 43 4D ('PHCM' in ASCII)");
679+
//debug!("{:02X?}", data);
680+
681+
// ===== Test 4 =====
682+
println!(" Read flash flags");
683+
// NPC
684+
let data = self.read_ec_flash(0x7F000, 0x80).unwrap();
685+
// MEC
686+
// let data = self.read_ec_flash(0x80000, 0x80).unwrap();
687+
let flash_flags_magic = [0xA3, 0xF1, 0x00, 0x00];
688+
let flash_flags_ver = [0x01, 0x0, 0x00, 0x00];
689+
// All 0xFF if just reflashed and not reinitialized by EC
690+
if data[0..4] == flash_flags_magic && data[8..12] == flash_flags_ver {
691+
println!(" Valid flash flags");
692+
} else if data.iter().all(|x| *x == 0xFF) {
693+
println!(" Erased flash flags");
694+
res = Err(EcError::DeviceError("Erased flash flags".to_string()));
695+
} else {
696+
println!(" INVALID flash flags: {:02X?}", &data[0..12]);
697+
// TODO: Disable error until I confirm flash flags on MEC
698+
// res = Err(EcError::DeviceError("INVALID flash flags".to_string()));
611699
}
612-
debug!("Expected 10 00 00 F7 and rest all FF");
613-
debug!("{:02X?}", data);
614-
615-
println!(" Read first 16 bytes of firmware.");
616-
// Make sure we can read at an offset and with arbitrary length
617-
let data = self.read_ec_flash(FLASH_PROGRAM_OFFSET, 16).unwrap();
618-
if data[0..4] != [0x50, 0x48, 0x43, 0x4D] {
619-
println!(" INVALID: {:02X?}", &data[0..3]);
620-
return Err(EcError::DeviceError(format!(
621-
"INVALID: {:02X?}",
622-
&data[0..3]
623-
)));
624-
}
625-
debug!("Expected beginning with 50 48 43 4D ('PHCM' in ASCII)");
626-
debug!("{:02X?}", data);
627700

628701
self.flash_notify(MecFlashNotify::AccessSpiDone)?;
629-
Ok(())
702+
703+
res
630704
}
631705

632706
/// Requests recent console output from EC and constantly asks for more

0 commit comments

Comments
 (0)