Skip to content

Commit b5c1410

Browse files
committed
Add --flash-full-ec to flash entire EC flash
This erases some persistent configuration Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent d725265 commit b5c1410

File tree

7 files changed

+73
-7
lines changed

7 files changed

+73
-7
lines changed

framework_lib/src/chromium_ec/mod.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ const EC_MEMMAP_SIZE: u16 = 0xFF;
6464
/// representing 'EC' in ASCII (0x20 == 'E', 0x21 == 'C')
6565
const EC_MEMMAP_ID: u16 = 0x20;
6666

67-
const FLASH_BASE: u32 = 0x0; // 0x80000
67+
const FLASH_BASE: u32 = 0x0;
68+
const FLASH_SIZE: u32 = 0x80000;
6869
const FLASH_RO_BASE: u32 = 0x0;
6970
const FLASH_RO_SIZE: u32 = 0x3C000;
7071
const FLASH_RW_BASE: u32 = 0x40000;
@@ -76,6 +77,7 @@ const FLASH_PROGRAM_OFFSET: u32 = 0x1000;
7677
#[derive(Clone, Debug, PartialEq)]
7778
pub enum EcFlashType {
7879
Full,
80+
Both,
7981
Ro,
8082
Rw,
8183
}
@@ -817,7 +819,7 @@ impl CrosEc {
817819
));
818820
};
819821

820-
if ft == EcFlashType::Full || ft == EcFlashType::Ro {
822+
if matches!(ft, EcFlashType::Full | EcFlashType::Both | EcFlashType::Ro) {
821823
if let Some(version) = ec_binary::read_ec_version(data, true) {
822824
println!("EC RO Version in File: {:?}", version.version);
823825
if version.details.platform != platform {
@@ -832,7 +834,7 @@ impl CrosEc {
832834
));
833835
}
834836
}
835-
if ft == EcFlashType::Full || ft == EcFlashType::Rw {
837+
if matches!(ft, EcFlashType::Full | EcFlashType::Both | EcFlashType::Rw) {
836838
if let Some(version) = ec_binary::read_ec_version(data, false) {
837839
println!("EC RW Version in File: {:?}", version.version);
838840
if version.details.platform != platform {
@@ -874,7 +876,41 @@ impl CrosEc {
874876
// 2. Read back two rows and make sure it's all 0xFF
875877
// 3. Write each row (128B) individually
876878

877-
if ft == EcFlashType::Full || ft == EcFlashType::Rw {
879+
if ft == EcFlashType::Full {
880+
if data.len() < FLASH_SIZE as usize {
881+
return Err(EcError::DeviceError(format!(
882+
"Firmware file too small for full flash. Expected {} bytes, got {}",
883+
FLASH_SIZE,
884+
data.len()
885+
)));
886+
}
887+
let data = &data[..FLASH_SIZE as usize];
888+
889+
println!(
890+
"Erasing full flash{}",
891+
if dry_run { " (DRY RUN)" } else { "" }
892+
);
893+
self.erase_ec_flash(FLASH_BASE, FLASH_SIZE, dry_run, info.erase_block_size)?;
894+
println!(" Done");
895+
896+
println!(
897+
"Writing full flash{}",
898+
if dry_run { " (DRY RUN)" } else { "" }
899+
);
900+
self.write_ec_flash(FLASH_BASE, data, dry_run)?;
901+
println!(" Done");
902+
903+
println!("Verifying full flash region");
904+
let flash_data = self.read_ec_flash(FLASH_BASE, FLASH_SIZE)?;
905+
if data == flash_data {
906+
println!(" flash verify success");
907+
} else {
908+
error!("Flash verify fail!");
909+
res = Err(EcError::DeviceError("Flash verify fail!".to_string()));
910+
}
911+
}
912+
913+
if ft == EcFlashType::Both || ft == EcFlashType::Rw {
878914
let rw_data = &data[FLASH_RW_BASE as usize..(FLASH_RW_BASE + FLASH_RW_SIZE) as usize];
879915

880916
println!(
@@ -906,7 +942,7 @@ impl CrosEc {
906942
}
907943
}
908944

909-
if ft == EcFlashType::Full || ft == EcFlashType::Ro {
945+
if ft == EcFlashType::Both || ft == EcFlashType::Ro {
910946
let ro_data = &data[FLASH_RO_BASE as usize..(FLASH_RO_BASE + FLASH_RO_SIZE) as usize];
911947

912948
println!("Erasing RO region");

framework_lib/src/commandline/clap_std.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ struct ClapCli {
143143
#[arg(long)]
144144
dump_ec_flash: Option<std::path::PathBuf>,
145145

146+
/// Flash full EC flash with new firmware from file - may render your hardware unbootable!
147+
#[arg(long)]
148+
flash_full_ec: Option<std::path::PathBuf>,
149+
146150
/// Flash EC (RO+RW) with new firmware from file - may render your hardware unbootable!
147151
#[arg(long)]
148152
flash_ec: Option<std::path::PathBuf>,
@@ -497,6 +501,9 @@ pub fn parse(args: &[String]) -> Cli {
497501
dump_ec_flash: args
498502
.dump_ec_flash
499503
.map(|x| x.into_os_string().into_string().unwrap()),
504+
flash_full_ec: args
505+
.flash_full_ec
506+
.map(|x| x.into_os_string().into_string().unwrap()),
500507
flash_ec: args
501508
.flash_ec
502509
.map(|x| x.into_os_string().into_string().unwrap()),

framework_lib/src/commandline/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ pub struct Cli {
185185
pub dump: Option<String>,
186186
pub h2o_capsule: Option<String>,
187187
pub dump_ec_flash: Option<String>,
188+
pub flash_full_ec: Option<String>,
188189
pub flash_ec: Option<String>,
189190
pub flash_ro_ec: Option<String>,
190191
pub flash_rw_ec: Option<String>,
@@ -273,6 +274,7 @@ pub fn parse(args: &[String]) -> Cli {
273274
dump: cli.dump,
274275
h2o_capsule: cli.h2o_capsule,
275276
// dump_ec_flash
277+
// flash_full_ec
276278
// flash_ec
277279
// flash_ro_ec
278280
// flash_rw_ec
@@ -1722,9 +1724,15 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
17221724
println!("Dumping to {}", dump_path);
17231725
// TODO: Should have progress indicator
17241726
dump_ec_flash(&ec, dump_path);
1725-
} else if let Some(ec_bin_path) = &args.flash_ec {
1727+
} else if let Some(ec_bin_path) = &args.flash_full_ec {
17261728
if args.force {
17271729
flash_ec(&ec, ec_bin_path, EcFlashType::Full, args.dry_run);
1730+
} else {
1731+
error!("Flashing full EC flash is unsafe. Use --flash-ec instead");
1732+
}
1733+
} else if let Some(ec_bin_path) = &args.flash_ec {
1734+
if args.force {
1735+
flash_ec(&ec, ec_bin_path, EcFlashType::Both, args.dry_run);
17281736
} else {
17291737
error!("Flashing EC RO region is unsafe. Use --flash-ec-rw instead");
17301738
}

framework_lib/src/commandline/uefi.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub fn parse(args: &[String]) -> Cli {
5757
pd_bin: None,
5858
ec_bin: None,
5959
dump_ec_flash: None,
60+
flash_full_ec: None,
6061
flash_ec: None,
6162
flash_ro_ec: None,
6263
flash_rw_ec: None,
@@ -654,6 +655,14 @@ pub fn parse(args: &[String]) -> Cli {
654655
None
655656
};
656657
found_an_option = true;
658+
} else if arg == "--flash-full-ec" {
659+
cli.flash_full_ec = if args.len() > i + 1 {
660+
Some(args[i + 1].clone())
661+
} else {
662+
println!("--flash-full-ec requires extra argument to denote input file");
663+
None
664+
};
665+
found_an_option = true;
657666
} else if arg == "--flash-ec" {
658667
cli.flash_ec = if args.len() > i + 1 {
659668
Some(args[i + 1].clone())

framework_tool/completions/bash/framework_tool

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ _framework_tool() {
2323

2424
case "${cmd}" in
2525
framework_tool)
26-
opts="-v -q -t -f -h --flash-gpu-descriptor --verbose --quiet --versions --version --features --esrt --device --compare-version --power --thermal --sensors --fansetduty --fansetrpm --autofanctrl --pdports --info --meinfo --pd-info --pd-reset --pd-disable --pd-enable --dp-hdmi-info --dp-hdmi-update --audio-card-info --privacy --pd-bin --ec-bin --capsule --dump --h2o-capsule --dump-ec-flash --flash-ec --flash-ro-ec --flash-rw-ec --intrusion --inputdeck --inputdeck-mode --expansion-bay --charge-limit --charge-current-limit --charge-rate-limit --get-gpio --fp-led-level --fp-brightness --kblight --remap-key --rgbkbd --ps2-enable --tablet-mode --touchscreen-enable --stylus-battery --console --reboot-ec --ec-hib-delay --uptimeinfo --s0ix-counter --hash --driver --pd-addrs --pd-ports --test --test-retimer --boardid --force --dry-run --flash-gpu-descriptor-file --dump-gpu-descriptor-file --nvidia --host-command --generate-completions --help"
26+
opts="-v -q -t -f -h --flash-gpu-descriptor --verbose --quiet --versions --version --features --esrt --device --compare-version --power --thermal --sensors --fansetduty --fansetrpm --autofanctrl --pdports --info --meinfo --pd-info --pd-reset --pd-disable --pd-enable --dp-hdmi-info --dp-hdmi-update --audio-card-info --privacy --pd-bin --ec-bin --capsule --dump --h2o-capsule --dump-ec-flash --flash-full-ec --flash-ec --flash-ro-ec --flash-rw-ec --intrusion --inputdeck --inputdeck-mode --expansion-bay --charge-limit --charge-current-limit --charge-rate-limit --get-gpio --fp-led-level --fp-brightness --kblight --remap-key --rgbkbd --ps2-enable --tablet-mode --touchscreen-enable --stylus-battery --console --reboot-ec --ec-hib-delay --uptimeinfo --s0ix-counter --hash --driver --pd-addrs --pd-ports --test --test-retimer --boardid --force --dry-run --flash-gpu-descriptor-file --dump-gpu-descriptor-file --nvidia --host-command --generate-completions --help"
2727
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
2828
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
2929
return 0
@@ -97,6 +97,10 @@ _framework_tool() {
9797
COMPREPLY=($(compgen -f "${cur}"))
9898
return 0
9999
;;
100+
--flash-full-ec)
101+
COMPREPLY=($(compgen -f "${cur}"))
102+
return 0
103+
;;
100104
--flash-ec)
101105
COMPREPLY=($(compgen -f "${cur}"))
102106
return 0

framework_tool/completions/fish/framework_tool.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ complete -c framework_tool -l capsule -d 'Parse UEFI Capsule information from bi
2222
complete -c framework_tool -l dump -d 'Dump extracted UX capsule bitmap image to a file' -r -F
2323
complete -c framework_tool -l h2o-capsule -d 'Parse UEFI Capsule information from binary file' -r -F
2424
complete -c framework_tool -l dump-ec-flash -d 'Dump EC flash contents' -r -F
25+
complete -c framework_tool -l flash-full-ec -d 'Flash full EC flash with new firmware from file - may render your hardware unbootable!' -r -F
2526
complete -c framework_tool -l flash-ec -d 'Flash EC (RO+RW) with new firmware from file - may render your hardware unbootable!' -r -F
2627
complete -c framework_tool -l flash-ro-ec -d 'Flash EC with new RO firmware from file - may render your hardware unbootable!' -r -F
2728
complete -c framework_tool -l flash-rw-ec -d 'Flash EC with new RW firmware from file' -r -F

framework_tool/completions/zsh/_framework_tool

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ _framework_tool() {
3232
'--dump=[Dump extracted UX capsule bitmap image to a file]:DUMP:_files' \
3333
'--h2o-capsule=[Parse UEFI Capsule information from binary file]:H2O_CAPSULE:_files' \
3434
'--dump-ec-flash=[Dump EC flash contents]:DUMP_EC_FLASH:_files' \
35+
'--flash-full-ec=[Flash full EC flash with new firmware from file - may render your hardware unbootable!]:FLASH_FULL_EC:_files' \
3536
'--flash-ec=[Flash EC (RO+RW) with new firmware from file - may render your hardware unbootable!]:FLASH_EC:_files' \
3637
'--flash-ro-ec=[Flash EC with new RO firmware from file - may render your hardware unbootable!]:FLASH_RO_EC:_files' \
3738
'--flash-rw-ec=[Flash EC with new RW firmware from file]:FLASH_RW_EC:_files' \

0 commit comments

Comments
 (0)