Skip to content

Commit d23f986

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

File tree

1 file changed

+77
-23
lines changed
  • framework_lib/src/chromium_ec

1 file changed

+77
-23
lines changed

framework_lib/src/chromium_ec/mod.rs

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -590,39 +590,93 @@ impl CrosEc {
590590
}
591591

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

596-
println!(" EC Test");
597-
println!(" Read first row of flash.");
598-
// Make sure we can read a full flash row
597+
println!(" Read first row of flash (RO FW)");
599598
let data = self.read_ec_flash(0, 0x80).unwrap();
600-
if data[0..4] != [0x10, 0x00, 0x00, 0xF7] {
599+
600+
//debug!("Expected 10 00 00 F7 and rest all FF");
601+
debug!("Expecting 5E 4D 3B 2A and 0x20-0x40 all 00");
602+
debug!("{:02X?}", data);
603+
println!(" {:02X?}", &data[..8]);
604+
if data.iter().all(|x| *x == 0xFF) {
605+
println!(" Erased!");
606+
}
607+
608+
// Make sure we can read a full flash row
609+
let legacy_start = [0x10, 0x00, 0x00, 0xF7];
610+
let zephyr_start = [0x5E, 0x4D, 0x3B, 0x2A];
611+
if data[0..4] != legacy_start && data[0..4] != zephyr_start {
601612
println!(" INVALID start");
602-
return Err(EcError::DeviceError("INVALID start".to_string()));
613+
res = Err(EcError::DeviceError("INVALID start".to_string()));
603614
}
604-
if !data[4..].iter().all(|x| *x == 0xFF) {
615+
let legacy_comp = !data[4..].iter().all(|x| *x == 0xFF);
616+
let zephyr_comp = !data[0x20..0x40].iter().all(|x| *x == 0x00);
617+
if legacy_comp && zephyr_comp {
605618
println!(" INVALID end");
606-
return Err(EcError::DeviceError("INVALID end".to_string()));
619+
res = Err(EcError::DeviceError("INVALID end".to_string()));
620+
}
621+
622+
// ----- RW image ------
623+
println!(" Read first row of RW FW");
624+
let data = self.read_ec_flash(0x40000, 0x80).unwrap();
625+
626+
// //debug!("Expected TODO and rest all FF");
627+
// debug!("Expecting 80 7D 0C 20 and 0x20-0x2C all 00");
628+
println!(" {:02X?}", &data[..8]);
629+
if data.iter().all(|x| *x == 0xFF) {
630+
println!(" Erased!");
631+
res = Err(EcError::DeviceError("RW Erased".to_string()));
632+
}
633+
634+
// // Make sure we can read a full flash row
635+
// let legacy_start = []; // TODO:
636+
// let zephyr_start = [0x80, 0x7D, 0x0C, 0x20];
637+
// if data[0..4] != legacy_start && data[0..4] != zephyr_start {
638+
// println!(" INVALID start");
639+
// res = Err(EcError::DeviceError("INVALID start".to_string()));
640+
// }
641+
// let legacy_comp = !data[4..].iter().all(|x| *x == 0xFF);
642+
// let zephyr_comp = !data[0x20..0x2C].iter().all(|x| *x == 0x00);
643+
// if legacy_comp && zephyr_comp {
644+
// println!(" INVALID end");
645+
// res = Err(EcError::DeviceError("INVALID end".to_string()));
646+
// }
647+
648+
// TODO: I don't think there's are magic bytes on zephyr firmware
649+
//println!(" Read first 16 bytes of firmware.");
650+
//// Make sure we can read at an offset and with arbitrary length
651+
//let data = self.read_ec_flash(FLASH_PROGRAM_OFFSET, 16).unwrap();
652+
//if data[0..4] != [0x50, 0x48, 0x43, 0x4D] {
653+
// println!(" INVALID: {:02X?}", &data[0..3]);
654+
// res = Err(EcError::DeviceError(format!(
655+
// "INVALID: {:02X?}",
656+
// &data[0..3]
657+
// )));
658+
//}
659+
//debug!("Expected beginning with 50 48 43 4D ('PHCM' in ASCII)");
660+
//debug!("{:02X?}", data);
661+
662+
println!(" Read flash flags");
663+
let data = self.read_ec_flash(0x7F000, 0x80).unwrap(); // NPC
664+
// let data = self.read_ec_flash(0x80000, 0x80).unwrap(); // MEC
665+
let flash_flags_magic = [0xA3, 0xF1, 0x00, 0x00];
666+
let flash_flags_ver = [0x01, 0x0, 0x00, 0x00];
667+
// All 0xFF if just reflashed and not reinitialized by EC
668+
if data[0..4] == flash_flags_magic && data[8..12] == flash_flags_ver {
669+
println!(" Valid flash flags");
670+
} else if data.iter().all(|x| *x == 0xFF) {
671+
println!(" Erased flash flags");
672+
} else {
673+
println!(" INVALID flash flags");
674+
res = Err(EcError::DeviceError("INVALID flash flags".to_string()));
607675
}
608-
debug!("Expected 10 00 00 F7 and rest all FF");
609-
debug!("{:02X?}", data);
610-
611-
println!(" Read first 16 bytes of firmware.");
612-
// Make sure we can read at an offset and with arbitrary length
613-
let data = self.read_ec_flash(FLASH_PROGRAM_OFFSET, 16).unwrap();
614-
if data[0..4] != [0x50, 0x48, 0x43, 0x4D] {
615-
println!(" INVALID: {:02X?}", &data[0..3]);
616-
return Err(EcError::DeviceError(format!(
617-
"INVALID: {:02X?}",
618-
&data[0..3]
619-
)));
620-
}
621-
debug!("Expected beginning with 50 48 43 4D ('PHCM' in ASCII)");
622-
debug!("{:02X?}", data);
623676

624677
self.flash_notify(MecFlashNotify::AccessSpiDone)?;
625-
Ok(())
678+
679+
res
626680
}
627681

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

0 commit comments

Comments
 (0)