Skip to content

Commit fd795df

Browse files
authored
Add board-info command support in Secure Download Mode (#838)
* Cut off the write_bin part from original #832 * Changelog entry * Akela missed... * Dumb * Address reviews
1 parent 37f28f6 commit fd795df

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
234234
- Make the default flashing frequency target specific (#389)
235235
- Add note about permissions on Linux (#391)
236236
- Add a diagnostic to tell the user about the partition table format (#397)
237+
- Add `board-info` command support in Secure Download Mode (#838)
237238

238239
### Fixed
239240

espflash/src/cli/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,17 +575,21 @@ pub fn parse_chip_rev(chip_rev: &str) -> Result<u16> {
575575
/// Print information about a chip
576576
pub fn print_board_info(flasher: &mut Flasher) -> Result<()> {
577577
let info = flasher.device_info()?;
578-
579578
print!("Chip type: {}", info.chip);
579+
580580
if let Some((major, minor)) = info.revision {
581581
println!(" (revision v{major}.{minor})");
582582
} else {
583583
println!();
584584
}
585+
585586
println!("Crystal frequency: {}", info.crystal_frequency);
586587
println!("Flash size: {}", info.flash_size);
587588
println!("Features: {}", info.features.join(", "));
588-
println!("MAC address: {}", info.mac_address);
589+
590+
if let Some(mac) = info.mac_address {
591+
println!("MAC address: {}", mac);
592+
}
589593

590594
Ok(())
591595
}

espflash/src/connection/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub struct Connection {
124124
decoder: SlipDecoder,
125125
after_operation: ResetAfterOperation,
126126
before_operation: ResetBeforeOperation,
127+
pub(crate) secure_download_mode: bool,
127128
}
128129

129130
impl Connection {
@@ -139,6 +140,7 @@ impl Connection {
139140
decoder: SlipDecoder::new(),
140141
after_operation,
141142
before_operation,
143+
secure_download_mode: false,
142144
}
143145
}
144146

@@ -449,6 +451,7 @@ impl Connection {
449451
// - https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/serial-protocol.html?highlight=md5#response-packet
450452
// - https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/serial-protocol.html?highlight=md5#status-bytes
451453
// - https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/serial-protocol.html?highlight=md5#verifying-uploaded-data
454+
452455
let status_len = if response.len() == 10 || response.len() == 26 {
453456
2
454457
} else {
@@ -483,8 +486,8 @@ impl Connection {
483486
return_op: response[1],
484487
return_length: u16::from_le_bytes(response[2..][..2].try_into().unwrap()),
485488
value,
486-
error: response[response.len() - status_len],
487-
status: response[response.len() - status_len + 1],
489+
error: response[response.len() - status_len + 1],
490+
status: response[response.len() - status_len],
488491
};
489492

490493
Ok(Some(header))
@@ -524,11 +527,10 @@ impl Connection {
524527
pub fn command(&mut self, command: Command<'_>) -> Result<CommandResponseValue, Error> {
525528
let ty = command.command_type();
526529
self.write_command(command).for_command(ty)?;
527-
528530
for _ in 0..100 {
529531
match self.read_response().for_command(ty)? {
530532
Some(response) if response.return_op == ty as u8 => {
531-
return if response.error != 0 {
533+
return if response.status != 0 {
532534
let _error = self.flush();
533535
Err(Error::RomError(RomError::new(
534536
command.command_type(),

espflash/src/flasher/mod.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ pub struct DeviceInfo {
634634
/// Device features
635635
pub features: Vec<String>,
636636
/// MAC address
637-
pub mac_address: String,
637+
pub mac_address: Option<String>,
638638
}
639639

640640
/// Connect to and flash a target device
@@ -710,14 +710,19 @@ impl Flasher {
710710
return Ok(flasher);
711711
}
712712

713-
// Load flash stub if enabled
714-
if use_stub {
715-
info!("Using flash stub");
716-
flasher.load_stub()?;
713+
if !flasher.connection.secure_download_mode {
714+
// Load flash stub if enabled.
715+
if use_stub {
716+
info!("Using flash stub");
717+
flasher.load_stub()?;
718+
}
719+
// Flash size autodetection doesn't work in Secure Download Mode.
720+
flasher.spi_autodetect()?;
721+
} else if use_stub {
722+
warn!("Stub is not supported in Secure Download Mode, setting --no-stub");
723+
flasher.use_stub = false;
717724
}
718725

719-
flasher.spi_autodetect()?;
720-
721726
// Now that we have established a connection and detected the chip and flash
722727
// size, we can set the baud rate of the connection to the configured value.
723728
if let Some(baud) = speed {
@@ -985,14 +990,21 @@ impl Flasher {
985990
let chip = self.chip();
986991
let target = chip.into_target();
987992

988-
let revision = Some(target.chip_revision(self.connection())?);
993+
// chip_revision reads from efuse, which is not possible in Secure Download Mode
994+
let revision = (!self.connection.secure_download_mode)
995+
.then(|| target.chip_revision(self.connection()))
996+
.transpose()?;
997+
989998
let crystal_frequency = target.crystal_freq(self.connection())?;
990999
let features = target
9911000
.chip_features(self.connection())?
9921001
.iter()
9931002
.map(|s| s.to_string())
9941003
.collect::<Vec<_>>();
995-
let mac_address = target.mac_address(self.connection())?;
1004+
1005+
let mac_address = (!self.connection.secure_download_mode)
1006+
.then(|| target.mac_address(self.connection()))
1007+
.transpose()?;
9961008

9971009
let info = DeviceInfo {
9981010
chip,
@@ -1099,13 +1111,23 @@ impl Flasher {
10991111
segments: &[Segment<'_>],
11001112
mut progress: Option<&mut dyn ProgressCallbacks>,
11011113
) -> Result<(), Error> {
1114+
if self.connection.secure_download_mode {
1115+
return Err(Error::UnsupportedFeature {
1116+
chip: self.chip,
1117+
feature: "writing binaries in Secure Download Mode currently".into(),
1118+
});
1119+
}
1120+
11021121
let mut target = self
11031122
.chip
11041123
.flash_target(self.spi_params, self.use_stub, false, false);
1124+
11051125
target.begin(&mut self.connection).flashing()?;
1126+
11061127
for segment in segments {
11071128
target.write_segment(&mut self.connection, segment.borrow(), &mut progress)?;
11081129
}
1130+
11091131
target.finish(&mut self.connection, true).flashing()?;
11101132

11111133
Ok(())

0 commit comments

Comments
 (0)