Skip to content

Commit 31cdb6f

Browse files
committed
chromium_ec: Allow flashing single EC FW
Only RO, only RW or both. Signed-off-by: Daniel Schaefer <[email protected]>
1 parent bc530fb commit 31cdb6f

File tree

4 files changed

+86
-27
lines changed

4 files changed

+86
-27
lines changed

framework_lib/src/chromium_ec/mod.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ const FLASH_RW_BASE: u32 = 0x40000;
5959
const FLASH_RW_SIZE: u32 = 0x39000;
6060
const FLASH_PROGRAM_OFFSET: u32 = 0x1000;
6161

62+
#[derive(Clone, Debug, PartialEq)]
63+
pub enum EcFlashType {
64+
Full,
65+
Ro,
66+
Rw,
67+
}
68+
6269
#[derive(PartialEq)]
6370
pub enum MecFlashNotify {
6471
AccessSpi = 0x00,
@@ -385,37 +392,49 @@ impl CrosEc {
385392
/// | 3C000 | 3FFFF | 04000 | Preserved |
386393
/// | 40000 | 3C000 | 39000 | RO Region |
387394
/// | 79000 | 79FFF | 07000 | Preserved |
388-
pub fn reflash(&self, data: &[u8]) -> EcResult<()> {
395+
pub fn reflash(&self, data: &[u8], ft: EcFlashType) -> EcResult<()> {
389396
let mut _flash_bin: Vec<u8> = Vec::with_capacity(EC_FLASH_SIZE);
390397
println!("Unlocking flash");
391398
self.flash_notify(MecFlashNotify::AccessSpi)?;
392399
self.flash_notify(MecFlashNotify::FirmwareStart)?;
393400

394-
//println!("Erasing RO region");
395-
//self.erase_ec_flash(FLASH_BASE + FLASH_RO_BASE, FLASH_RO_SIZE)?;
396-
println!("Erasing RW region");
397-
self.erase_ec_flash(FLASH_BASE + FLASH_RW_BASE, FLASH_RW_SIZE)?;
401+
if ft == EcFlashType::Full || ft == EcFlashType::Ro {
402+
println!("Erasing RO region");
403+
self.erase_ec_flash(FLASH_BASE + FLASH_RO_BASE, FLASH_RO_SIZE)?;
404+
}
405+
if ft == EcFlashType::Full || ft == EcFlashType::Rw {
406+
println!("Erasing RW region");
407+
self.erase_ec_flash(FLASH_BASE + FLASH_RW_BASE, FLASH_RW_SIZE)?;
408+
}
398409

399410
let ro_data = &data[FLASH_RO_BASE as usize..(FLASH_RO_BASE + FLASH_RO_SIZE) as usize];
400-
//println!("Writing RO region");
401-
//self.write_ec_flash(FLASH_BASE + FLASH_RO_BASE, ro_data);
411+
if ft == EcFlashType::Full || ft == EcFlashType::Ro {
412+
println!("Writing RO region");
413+
self.write_ec_flash(FLASH_BASE + FLASH_RO_BASE, ro_data)?;
414+
}
402415

403416
let rw_data = &data[FLASH_RW_BASE as usize..(FLASH_RW_BASE + FLASH_RW_SIZE) as usize];
404-
println!("Writing RW region");
405-
self.write_ec_flash(FLASH_BASE + FLASH_RW_BASE, rw_data)?;
417+
if ft == EcFlashType::Full || ft == EcFlashType::Rw {
418+
println!("Writing RW region");
419+
self.write_ec_flash(FLASH_BASE + FLASH_RW_BASE, rw_data)?;
420+
}
406421

407422
println!("Verifying");
408-
let flash_ro_data = self.read_ec_flash(FLASH_BASE + FLASH_RO_BASE, FLASH_RO_SIZE)?;
409-
if ro_data == flash_ro_data {
410-
println!("RO verify success");
411-
} else {
412-
println!("RO verify fail");
423+
if ft == EcFlashType::Full || ft == EcFlashType::Ro {
424+
let flash_ro_data = self.read_ec_flash(FLASH_BASE + FLASH_RO_BASE, FLASH_RO_SIZE)?;
425+
if ro_data == flash_ro_data {
426+
println!("RO verify success");
427+
} else {
428+
println!("RO verify fail");
429+
}
413430
}
414-
let flash_rw_data = self.read_ec_flash(FLASH_BASE + FLASH_RW_BASE, FLASH_RW_SIZE)?;
415-
if rw_data == flash_rw_data {
416-
println!("RW verify success");
417-
} else {
418-
println!("RW verify fail");
431+
if ft == EcFlashType::Full || ft == EcFlashType::Rw {
432+
let flash_rw_data = self.read_ec_flash(FLASH_BASE + FLASH_RW_BASE, FLASH_RW_SIZE)?;
433+
if rw_data == flash_rw_data {
434+
println!("RW verify success");
435+
} else {
436+
println!("RW verify fail");
437+
}
419438
}
420439

421440
println!("Locking flash");

framework_lib/src/commandline/clap_std.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ struct ClapCli {
8585
#[arg(long)]
8686
flash_ec: Option<std::path::PathBuf>,
8787

88+
/// Flash EC with new RO firmware from file
89+
#[arg(long)]
90+
flash_ro_ec: Option<std::path::PathBuf>,
91+
92+
/// Flash EC with new RW firmware from file
93+
#[arg(long)]
94+
flash_rw_ec: Option<std::path::PathBuf>,
95+
8896
/// Show status of intrusion switch
8997
#[arg(long)]
9098
intrusion: bool,
@@ -170,6 +178,12 @@ pub fn parse(args: &[String]) -> Cli {
170178
flash_ec: args
171179
.flash_ec
172180
.map(|x| x.into_os_string().into_string().unwrap()),
181+
flash_ro_ec: args
182+
.flash_ro_ec
183+
.map(|x| x.into_os_string().into_string().unwrap()),
184+
flash_rw_ec: args
185+
.flash_rw_ec
186+
.map(|x| x.into_os_string().into_string().unwrap()),
173187
intrusion: args.intrusion,
174188
inputmodules: args.inputmodules,
175189
input_deck_mode: args.input_deck_mode,

framework_lib/src/commandline/mod.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ use crate::ccgx::{self, SiliconId::*};
3232
use crate::chromium_ec;
3333
use crate::chromium_ec::commands::DeckStateMode;
3434
use crate::chromium_ec::commands::FpLedBrightnessLevel;
35-
use crate::chromium_ec::print_err;
36-
use crate::chromium_ec::EcError;
37-
use crate::chromium_ec::EcResult;
35+
use crate::chromium_ec::{print_err, EcFlashType};
36+
use crate::chromium_ec::{EcError, EcResult};
3837
#[cfg(feature = "linux")]
3938
use crate::csme;
4039
use crate::ec_binary;
@@ -130,6 +129,8 @@ pub struct Cli {
130129
pub ho2_capsule: Option<String>,
131130
pub dump_ec_flash: Option<String>,
132131
pub flash_ec: Option<String>,
132+
pub flash_ro_ec: Option<String>,
133+
pub flash_rw_ec: Option<String>,
133134
pub driver: Option<CrosEcDriverType>,
134135
pub test: bool,
135136
pub intrusion: bool,
@@ -395,7 +396,7 @@ fn print_esrt() {
395396
}
396397
}
397398

398-
fn flash_ec(ec: &CrosEc, ec_bin_path: &str) {
399+
fn flash_ec(ec: &CrosEc, ec_bin_path: &str, flash_type: EcFlashType) {
399400
#[cfg(feature = "uefi")]
400401
let data = crate::uefi::fs::shell_read_file(ec_bin_path);
401402
#[cfg(not(feature = "uefi"))]
@@ -412,7 +413,7 @@ fn flash_ec(ec: &CrosEc, ec_bin_path: &str) {
412413
println!("File");
413414
println!(" Size: {:>20} B", data.len());
414415
println!(" Size: {:>20} KB", data.len() / 1024);
415-
if let Err(err) = ec.reflash(&data) {
416+
if let Err(err) = ec.reflash(&data, flash_type) {
416417
println!("Error: {:?}", err);
417418
} else {
418419
println!("Success!");
@@ -701,9 +702,14 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
701702
}
702703
} else if let Some(dump_path) = &args.dump_ec_flash {
703704
println!("Dumping to {}", dump_path);
705+
// TODO: Should have progress indicator
704706
dump_ec_flash(&ec, dump_path);
705707
} else if let Some(ec_bin_path) = &args.flash_ec {
706-
flash_ec(&ec, ec_bin_path);
708+
flash_ec(&ec, ec_bin_path, EcFlashType::Full);
709+
} else if let Some(ec_bin_path) = &args.flash_ro_ec {
710+
flash_ec(&ec, ec_bin_path, EcFlashType::Ro);
711+
} else if let Some(ec_bin_path) = &args.flash_rw_ec {
712+
flash_ec(&ec, ec_bin_path, EcFlashType::Rw);
707713
} else if let Some(hash_file) = &args.hash {
708714
println!("Hashing file: {}", hash_file);
709715
#[cfg(feature = "uefi")]
@@ -751,6 +757,9 @@ Options:
751757
--pd-bin <PD_BIN> Parse versions from PD firmware binary file
752758
--ec-bin <EC_BIN> Parse versions from EC firmware binary file
753759
--capsule <CAPSULE> Parse UEFI Capsule information from binary file
760+
--flash-ec <FLASH_EC> Flash EC with new firmware from file
761+
--flash-ro-ec <FLASH_EC> Flash EC with new firmware from file
762+
--flash-rw-ec <FLASH_EC> Flash EC with new firmware from file
754763
--intrusion Show status of intrusion switch
755764
--inputmodules Show status of the input modules (Framework 16 only)
756765
--charge-limit [<VAL>] Get or set battery charge limit (Percentage number as arg, e.g. '100')
@@ -759,7 +768,6 @@ Options:
759768
--console <CONSOLE> Get EC console, choose whether recent or to follow the output [possible values: recent, follow]
760769
--reboot-ec <REBOOT_EC>Control EC RO/RW jump [possible values: ro, rw, cancel-jump, disable-jump]
761770
--dump-ec-flash <DUMP_EC_FLASH> Dump EC flash contents
762-
--flash-ec <FLASH_EC> Flash EC with new firmware from file
763771
-t, --test Run self-test to check if interaction with EC is possible
764772
-h, --help Print help information
765773
"#

framework_lib/src/commandline/uefi.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ pub fn parse(args: &[String]) -> Cli {
6969
ec_bin: None,
7070
dump_ec_flash: None,
7171
flash_ec: None,
72+
flash_ro_ec: None,
73+
flash_rw_ec: None,
7274
capsule: None,
7375
dump: None,
7476
ho2_capsule: None,
@@ -304,7 +306,23 @@ pub fn parse(args: &[String]) -> Cli {
304306
cli.flash_ec = if args.len() > i + 1 {
305307
Some(args[i + 1].clone())
306308
} else {
307-
println!("--flash_ec requires extra argument to denote input file");
309+
println!("--flash-ec requires extra argument to denote input file");
310+
None
311+
};
312+
found_an_option = true;
313+
} else if arg == "--flash-ro-ec" {
314+
cli.flash_ro_ec = if args.len() > i + 1 {
315+
Some(args[i + 1].clone())
316+
} else {
317+
println!("--flash-ro-ec requires extra argument to denote input file");
318+
None
319+
};
320+
found_an_option = true;
321+
} else if arg == "--flash-rw-ec" {
322+
cli.flash_rw_ec = if args.len() > i + 1 {
323+
Some(args[i + 1].clone())
324+
} else {
325+
println!("--flash-rw-ec requires extra argument to denote input file");
308326
None
309327
};
310328
found_an_option = true;

0 commit comments

Comments
 (0)