Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `board-info` decodes the embedded flash and PSRAM of ESP32-S3 chips from their eFuses like esptool, instead of reporting "Embedded Flash" or "Embedded PSRAM" based on the eFuse block version. It lists IEEE802.15.4 for ESP32-C6 and ESP32-H2, and shows ESP32 coding scheme 3 as "None (may contain encoding data)" instead of "Invalid"
- `board-info` decodes the embedded flash and PSRAM of ESP32-S3 chips from their eFuses like esptool, instead of reporting "Embedded Flash" or "Embedded PSRAM" based on the eFuse block version. It lists IEEE802.15.4 for ESP32-C6 and ESP32-H2, and shows ESP32 coding scheme 3 as "None (may contain encoding data)" instead of "Invalid" (#1126)
- `board-info` decodes the embedded flash and PSRAM of ESP32-S3 chips from their eFuses like esptool, instead of reporting "Embedded Flash" or "Embedded PSRAM" based on the eFuse block version. It reports the embedded flash of ESP32-C2, ESP32-C3 and ESP32-C6, lists IEEE802.15.4 for ESP32-C6 and ESP32-H2, and shows ESP32 coding scheme 3 as "None (may contain encoding data)" instead of "Invalid" (#1126)
- Secure Download Mode is only assumed when the ROM loader rejects reading a register, like esptool. A chip that does not answer, e.g. with `--before no-reset-no-sync` while the application runs, is reported as a connection error instead of with a false Secure Download Mode warning (#1145)

### Removed

Expand Down
126 changes: 119 additions & 7 deletions espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,9 @@ impl Flasher {
return Err(Box::new((e, connection)));
}

detect_sdm(&mut connection);
if let Err(e) = detect_sdm(&mut connection) {
return Err(Box::new((e, connection)));
}

let detected_chip = if connection.before_operation() != ResetBeforeOperation::NoResetNoSync
{
Expand Down Expand Up @@ -1536,17 +1538,27 @@ fn flash_size_from_id(flash_id: u32) -> Option<FlashSize> {
}

#[cfg(feature = "serialport")]
fn detect_sdm(connection: &mut Connection) {
fn detect_sdm(connection: &mut Connection) -> Result<(), Error> {
if let Ok(security_info) = connection.security_info(false) {
// Newer chips tell us if SDM is enabled.
connection.secure_download_mode =
security_info.security_flag_status("SECURE_DOWNLOAD_ENABLE");
} else if connection.read_reg(CHIP_DETECT_MAGIC_REG_ADDR).is_err() {
// On older chips, we have to guess by reading something. On these
// chips, there is always something readable at 0x40001000.
log::warn!("Secure Download Mode is enabled on this chip");
connection.secure_download_mode = true;
return Ok(());
}

// On older chips, we have to guess by reading something that is always
// readable. Like esptool, only a ROM loader that rejects the command means
// Secure Download Mode, other errors mean that the chip is not answering.
match connection.read_reg(CHIP_DETECT_MAGIC_REG_ADDR) {
Ok(_) => {}
Err(Error::RomError(_)) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esptool only treats Secure Download Mode as a ReadReg reply whose opcode does not match and whose error is 0x05 (https://github.com/espressif/esptool/blob/6ec584a39e046c63f507d8022cb0b7df25e2d4de/esptool/loader.py#L615), but command() skips those frames until it times out, so this arm never runs and an SDM chip on this path fails to connect.

warn!("Secure Download Mode is enabled on this chip");
connection.secure_download_mode = true;
}
Err(e) => return Err(e),
}

Ok(())
}

#[cfg(feature = "serialport")]
Expand All @@ -1564,3 +1576,103 @@ impl From<Flasher> for Port {
connection.into()
}
}

#[cfg(all(test, unix, feature = "serialport"))]
mod tests {
use std::{io::Read, thread};

use serialport::UsbPortInfo;

use super::*;
use crate::connection::reset::ResetAfterOperation;

/// Reads one SLIP frame from the device end of the port.
fn read_frame(device: &mut Port) {
let mut ends = 0;
while ends < 2 {
let mut byte = [0];
device.read_exact(&mut byte).unwrap();
ends += usize::from(byte[0] == 0xc0);
}
}

/// A ROM loader response frame for `ty` with the given status.
fn response(ty: CommandType, status: u8, error: u8) -> Vec<u8> {
vec![
0xc0, 0x01, ty as u8, 0x04, 0x00, 0, 0, 0, 0, status, error, 0, 0, 0xc0,
]
}

/// Runs `detect_sdm` against a device that rejects `GetSecurityInfo`, as
/// the ESP32 ROM loader does, and then runs `read_reg`.
fn detect_sdm_with(
read_reg: impl FnOnce(&mut Port) + Send + 'static,
) -> (Result<(), Error>, bool) {
let (mut device_port, host_port) = Port::pair().unwrap();
let port_info = UsbPortInfo {
vid: 0,
pid: 0,
serial_number: None,
manufacturer: None,
product: None,
};
let mut connection = Connection::new(
host_port,
port_info,
ResetAfterOperation::HardReset,
ResetBeforeOperation::NoReset,
115_200,
);
// The thread returns its port to keep it open until detection finishes
let device = thread::spawn(move || {
read_frame(&mut device_port);
device_port
.write_all(&response(CommandType::GetSecurityInfo, 1, 0x05))
.unwrap();
read_reg(&mut device_port);
device_port
});

let result = detect_sdm(&mut connection);
drop(device.join().unwrap());
(result, connection.secure_download_mode)
}

#[test]
fn rejected_read_reg_means_secure_download_mode() {
let (result, sdm) = detect_sdm_with(|device| {
read_frame(device);
device
.write_all(&response(CommandType::ReadReg, 1, 0x05))
.unwrap();
});
assert!(result.is_ok(), "{result:?}");
assert!(sdm);
}

#[test]
fn answered_read_reg_is_not_secure_download_mode() {
let (result, sdm) = detect_sdm_with(|device| {
read_frame(device);
device
.write_all(&response(CommandType::ReadReg, 0, 0))
.unwrap();
});
assert!(result.is_ok(), "{result:?}");
assert!(!sdm);
}

#[test]
fn read_reg_timeout_is_a_connection_error() {
let (result, sdm) = detect_sdm_with(|_| {});
assert!(
matches!(
&result,
Err(Error::Connection(err))
if matches!(err.downcast_ref(), Some(ConnectionError::Timeout(_)))
),
"expected a timeout, got {result:?}"
);
assert!(!sdm);
}
}
Loading