Skip to content

Only assume Secure Download Mode when the ROM rejects ReadReg - #1145

Open
SergioGasquez wants to merge 2 commits into
esp-rs:mainfrom
SergioGasquez:fix/sdm-detection-timeouts
Open

SergioGasquez wants to merge 2 commits into
esp-rs:mainfrom
SergioGasquez:fix/sdm-detection-timeouts

Conversation

@SergioGasquez

Copy link
Copy Markdown
Member

Description

Fixes #1139.

When GET_SECURITY_INFO fails, detect_sdm reads 0x40001000 and assumed Secure Download Mode on any error, including timeouts. A chip that is not answering, e.g. with --before no-reset-no-sync while the application runs, got a false "Secure Download Mode is enabled on this chip" warning, took the SDM code paths, and then failed with a second timeout.

Like esptool (read_reg raises UnsupportedCommandError only for a nonzero status), only a ROM reply that rejects ReadReg (Error::RomError) now means SDM. Other errors, such as timeouts and I/O errors, are returned from Flasher::connect as connection errors.

An AI tool generated a significant part of this change. It is tested on the host and on local hardware, see Testing.

Testing

  • Three new unit tests drive detect_sdm over a pseudo-terminal pair, with a device that rejects GetSecurityInfo like the ESP32 ROM: a rejected ReadReg sets SDM, an answered ReadReg does not, and a ReadReg timeout returns a connection timeout error without setting SDM.
  • The reproduction from Timeouts are taken as Secure Download Mode when connecting #1139 (espflash reset, then board-info --before no-reset-no-sync --chip <chip>) on ESP32-C5, ESP32-S3, ESP32-C6 (USB-Serial-JTAG), ESP32-C61, ESP32-C2 and ESP32-H2 (CP2102N):
    • Before: WARN Secure Download Mode is enabled on this chip, then a ReadReg or GetSecurityInfo timeout after ~9.1 s.
    • After: no warning, Timeout while running ReadReg command after ~6.1 s.
  • --before no-reset-no-sync --no-stub against a chip left in the ROM loader (--no-stub --after no-reset) and plain board-info still work on all six boards, with no SDM reported.
  • cargo xtask run-tests all passed on ESP32-C5, ESP32-S3, ESP32-C6, ESP32-C61 and ESP32-H2. On ESP32-C2 (CP2102N), watchdog reset after flashing failed in 2 of 4 full runs and passed 3/3 when run alone; the C2 answers GET_SECURITY_INFO, so it never reaches the changed path.
  • Not covered locally: a chip with SDM enabled, and ESP32/ESP32-S2, where GET_SECURITY_INFO fails and the ReadReg path runs on every connect. The HIL rig covers ESP32 and ESP32-S2.

@SergioGasquez

Copy link
Copy Markdown
Member Author

/hil esp32 esp32s2 esp32c6 esp32s3

@SergioGasquez

Copy link
Copy Markdown
Member Author

/hil sdm

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Triggered chips HIL for PR #1145.

Revision: 79ab572d44ba3a3c1d7b8f87305ab47168c338d3

Chips: esp32 esp32s2 esp32c6 esp32s3; suite: standard

Run: https://github.com/esp-rs/espflash/actions/runs/44135570248

Status: ✅ HIL run succeeded.

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Triggered sdm HIL for PR #1145.

Revision: 79ab572d44ba3a3c1d7b8f87305ab47168c338d3

Chips: esp32c6; suite: sdm

Run: https://github.com/esp-rs/espflash/actions/runs/44135695744

Status: ✅ HIL run succeeded.

@SergioGasquez
SergioGasquez force-pushed the fix/sdm-detection-timeouts branch 2 times, most recently from aafc0fb to 7f9dd9f Compare November 19, 2026 11:47
detect_sdm treated any ReadReg error, including timeouts, as Secure Download
Mode. Like esptool, only a rejected command now means SDM; other errors are
returned as connection errors.

Fixes esp-rs#1139
@SergioGasquez
SergioGasquez force-pushed the fix/sdm-detection-timeouts branch from 7f9dd9f to 117b0de Compare November 20, 2026 14:09
// 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Timeouts are taken as Secure Download Mode when connecting

2 participants