Skip to content

Commit 8dea4e6

Browse files
authored
Add chip detection using security info (#814)
* Add the ability to create a `Chip` from its corresponding chip ID * Attempt to detect chip using security info first, and use magic value if this fails * Update timeouts in HIL workflow * Update `CHANGELOG.md` * Increase more timeouts for HIL
1 parent a9c1686 commit 8dea4e6

File tree

14 files changed

+98
-42
lines changed

14 files changed

+98
-42
lines changed

.github/workflows/hil.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ jobs:
103103
run: timeout 10 bash espflash/tests/scripts/board-info.sh
104104

105105
- name: flash test
106-
run: timeout 25 bash espflash/tests/scripts/flash.sh ${{ matrix.board.mcu }}
106+
run: timeout 60 bash espflash/tests/scripts/flash.sh ${{ matrix.board.mcu }}
107107

108108
- name: monitor test
109109
run: timeout 10 bash espflash/tests/scripts/monitor.sh
@@ -116,13 +116,13 @@ jobs:
116116
timeout 90 bash espflash/tests/scripts/save-image_write-bin.sh ${{ matrix.board.mcu }}
117117
118118
- name: erase-region test
119-
run: timeout 10 bash espflash/tests/scripts/erase-region.sh
119+
run: timeout 15 bash espflash/tests/scripts/erase-region.sh
120120

121121
- name: hold-in-reset test
122-
run: timeout 5 bash espflash/tests/scripts/hold-in-reset.sh
122+
run: timeout 10 bash espflash/tests/scripts/hold-in-reset.sh
123123

124124
- name: reset test
125-
run: timeout 5 bash espflash/tests/scripts/reset.sh
125+
run: timeout 10 bash espflash/tests/scripts/reset.sh
126126

127127
- name: checksum-md5 test
128128
run: timeout 40 bash espflash/tests/scripts/checksum-md5.sh
@@ -134,4 +134,4 @@ jobs:
134134
run: timeout 20 bash espflash/tests/scripts/write-bin.sh
135135

136136
- name: read-flash test
137-
run: timeout 30 bash espflash/tests/scripts/read-flash.sh
137+
run: timeout 60 bash espflash/tests/scripts/read-flash.sh

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- Add `ROM` version of `read-flash` command (#812)
2020
- `espflash` can detect the log format automatically from ESP-HAL metadata. Reqires `esp-println` 0.14 (presumably, yet to be released) (#809)
2121
- Add `--output-format` option to monitor (#818)
22+
- Added chip detection based on security info, where supported (#814)
2223

2324
### Changed
2425

espflash/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ pub enum Error {
3131
#[diagnostic(code(espflash::cancelled))]
3232
Cancelled,
3333

34-
#[error("Unrecognized magic value: {0:#x}")]
34+
#[error("{0}")]
3535
#[diagnostic(
3636
code(espflash::chip_detect_error),
3737
help("Supported chips are: {}\n\
3838
If your chip is supported, try hard-resetting the device and try again",
3939
Chip::VARIANTS.join(", "))
4040
)]
41-
ChipDetectError(u32),
41+
ChipDetectError(String),
4242

4343
#[error("Chip provided with `-c/--chip` ({0}) does not match the detected chip ({1})")]
4444
#[diagnostic(

espflash/src/flasher/mod.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,7 @@ impl Flasher {
676676

677677
let detected_chip = if before_operation != ResetBeforeOperation::NoResetNoSync {
678678
// Detect which chip we are connected to.
679-
let magic = connection.read_reg(CHIP_DETECT_MAGIC_REG_ADDR)?;
680-
let detected_chip = Chip::from_magic(magic)?;
679+
let detected_chip = detect_chip(&mut connection, use_stub)?;
681680
if let Some(chip) = chip {
682681
if chip != detected_chip {
683682
return Err(Error::ChipMismatch(
@@ -686,6 +685,7 @@ impl Flasher {
686685
));
687686
}
688687
}
688+
689689
detected_chip
690690
} else if before_operation == ResetBeforeOperation::NoResetNoSync && chip.is_some() {
691691
chip.unwrap()
@@ -792,8 +792,7 @@ impl Flasher {
792792
}?;
793793

794794
// Re-detect chip to check stub is up
795-
let magic = self.connection.read_reg(CHIP_DETECT_MAGIC_REG_ADDR)?;
796-
let chip = Chip::from_magic(magic)?;
795+
let chip = detect_chip(&mut self.connection, self.use_stub)?;
797796
debug!("Re-detected chip: {:?}", chip);
798797

799798
Ok(())
@@ -1126,25 +1125,7 @@ impl Flasher {
11261125

11271126
/// Get security info
11281127
pub fn get_security_info(&mut self) -> Result<SecurityInfo, Error> {
1129-
self.connection
1130-
.with_timeout(CommandType::GetSecurityInfo.timeout(), |connection| {
1131-
let response = connection.command(Command::GetSecurityInfo)?;
1132-
// Extract raw bytes and convert them into `SecurityInfo`
1133-
if let crate::connection::CommandResponseValue::Vector(data) = response {
1134-
// HACK: Not quite sure why there seem to be 4 extra bytes at the end of the
1135-
// response when the stub is not being used...
1136-
let end = if self.use_stub {
1137-
data.len()
1138-
} else {
1139-
data.len() - 4
1140-
};
1141-
SecurityInfo::try_from(&data[..end])
1142-
} else {
1143-
Err(Error::InvalidResponse(
1144-
"response was not a vector of bytes".into(),
1145-
))
1146-
}
1147-
})
1128+
get_security_info(&mut self.connection, self.use_stub)
11481129
}
11491130

11501131
pub fn change_baud(&mut self, speed: u32) -> Result<(), Error> {
@@ -1367,3 +1348,39 @@ impl Flasher {
13671348
Ok(())
13681349
}
13691350
}
1351+
1352+
#[cfg(feature = "serialport")]
1353+
fn get_security_info(connection: &mut Connection, use_stub: bool) -> Result<SecurityInfo, Error> {
1354+
connection.with_timeout(CommandType::GetSecurityInfo.timeout(), |connection| {
1355+
let response = connection.command(Command::GetSecurityInfo)?;
1356+
// Extract raw bytes and convert them into `SecurityInfo`
1357+
if let crate::connection::CommandResponseValue::Vector(data) = response {
1358+
// HACK: Not quite sure why there seem to be 4 extra bytes at the end of the
1359+
// response when the stub is not being used...
1360+
let end = if use_stub { data.len() } else { data.len() - 4 };
1361+
SecurityInfo::try_from(&data[..end])
1362+
} else {
1363+
Err(Error::InvalidResponse(
1364+
"response was not a vector of bytes".into(),
1365+
))
1366+
}
1367+
})
1368+
}
1369+
1370+
#[cfg(feature = "serialport")]
1371+
fn detect_chip(connection: &mut Connection, use_stub: bool) -> Result<Chip, Error> {
1372+
match get_security_info(connection, use_stub) {
1373+
Ok(info) if info.chip_id.is_some() => {
1374+
let chip_id = info.chip_id.unwrap() as u16;
1375+
let chip = Chip::try_from(chip_id)?;
1376+
1377+
Ok(chip)
1378+
}
1379+
_ => {
1380+
let magic = connection.read_reg(CHIP_DETECT_MAGIC_REG_ADDR)?;
1381+
let chip = Chip::from_magic(magic)?;
1382+
1383+
Ok(chip)
1384+
}
1385+
}
1386+
}

espflash/src/targets/esp32.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::{
1111
Error,
1212
};
1313

14+
pub(crate) const CHIP_ID: u16 = 0;
15+
1416
const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[0x00f0_1d83];
1517

1618
const FLASH_RANGES: &[Range<u32>] = &[
@@ -179,7 +181,7 @@ impl Target for Esp32 {
179181
0x1000,
180182
0x1_0000,
181183
0x3f_0000,
182-
0,
184+
CHIP_ID,
183185
FlashFrequency::_40Mhz,
184186
booloader,
185187
);

espflash/src/targets/esp32c2.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::{
1212
Error,
1313
};
1414

15+
pub(crate) const CHIP_ID: u16 = 12;
16+
1517
const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[
1618
0x6f51_306f, // ECO0
1719
0x7c41_a06f, // ECO1
@@ -116,7 +118,7 @@ impl Target for Esp32c2 {
116118
0x0,
117119
0x1_0000,
118120
0x1f_0000,
119-
12,
121+
CHIP_ID,
120122
FlashFrequency::_30Mhz,
121123
booloader,
122124
);

espflash/src/targets/esp32c3.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::{
1111
Error,
1212
};
1313

14+
pub(crate) const CHIP_ID: u16 = 5;
15+
1416
const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[
1517
0x6921_506f, // ECO1 + ECO2
1618
0x1b31_506f, // ECO3
@@ -27,7 +29,7 @@ const PARAMS: Esp32Params = Esp32Params::new(
2729
0x0,
2830
0x1_0000,
2931
0x3f_0000,
30-
5,
32+
CHIP_ID,
3133
FlashFrequency::_40Mhz,
3234
include_bytes!("../../resources/bootloaders/esp32c3-bootloader.bin"),
3335
);

espflash/src/targets/esp32c6.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::{
1111
Error,
1212
};
1313

14+
pub(crate) const CHIP_ID: u16 = 13;
15+
1416
const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[0x2CE0_806F];
1517

1618
const FLASH_RANGES: &[Range<u32>] = &[
@@ -22,7 +24,7 @@ const PARAMS: Esp32Params = Esp32Params::new(
2224
0x0,
2325
0x1_0000,
2426
0x3f_0000,
25-
13,
27+
CHIP_ID,
2628
FlashFrequency::_40Mhz,
2729
include_bytes!("../../resources/bootloaders/esp32c6-bootloader.bin"),
2830
);

espflash/src/targets/esp32h2.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::{
1111
Error,
1212
};
1313

14+
pub(crate) const CHIP_ID: u16 = 16;
15+
1416
const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[0xD7B7_3E80];
1517

1618
const FLASH_RANGES: &[Range<u32>] = &[
@@ -22,7 +24,7 @@ const PARAMS: Esp32Params = Esp32Params::new(
2224
0x0,
2325
0x1_0000,
2426
0x3f_0000,
25-
16,
27+
CHIP_ID,
2628
FlashFrequency::_24Mhz,
2729
include_bytes!("../../resources/bootloaders/esp32h2-bootloader.bin"),
2830
);

espflash/src/targets/esp32p4.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::{
1111
Error,
1212
};
1313

14+
pub(crate) const CHIP_ID: u16 = 18;
15+
1416
const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[0x0, 0x0ADDBAD0];
1517

1618
const FLASH_RANGES: &[Range<u32>] = &[
@@ -22,7 +24,7 @@ const PARAMS: Esp32Params = Esp32Params::new(
2224
0x2000,
2325
0x1_0000,
2426
0x3f_0000,
25-
18,
27+
CHIP_ID,
2628
FlashFrequency::_40Mhz,
2729
include_bytes!("../../resources/bootloaders/esp32p4-bootloader.bin"),
2830
);

0 commit comments

Comments
 (0)