Skip to content

Commit b7d9849

Browse files
authored
Fix read-flash which didn't work with some lengths (#804)
* wip * finalize + hil test hil test * changelog entry * fix
1 parent eb963d7 commit b7d9849

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

.github/workflows/hil.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,6 @@ jobs:
132132

133133
- name: write-bin test
134134
run: timeout 20 bash espflash/tests/scripts/write-bin.sh
135+
136+
- name: read-flash test
137+
run: timeout 20 bash espflash/tests/scripts/read-flash.sh

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3434
- Fix `-s` argument collision (#731)
3535
- `address` and `size` in `erase-region` have to be multiples of 4096 (#771)
3636
- Fixed typos in error variant names (#782)
37+
- Fix `read-flash` which didn't work with some lengths (#804)
3738

3839
### Removed
3940

espflash/src/connection/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,35 @@ impl Connection {
347347
result
348348
}
349349

350+
/// Read the response from a serial port
351+
pub fn read_flash_response(&mut self) -> Result<Option<CommandResponse>, Error> {
352+
let mut response = Vec::new();
353+
354+
self.decoder.decode(&mut self.serial, &mut response)?;
355+
356+
if response.is_empty() {
357+
return Ok(None);
358+
}
359+
let value = CommandResponseValue::Vector(response.clone());
360+
361+
let header = CommandResponse {
362+
resp: 1_u8,
363+
return_op: CommandType::ReadFlash as u8,
364+
return_length: response.len() as u16,
365+
value,
366+
error: 0_u8,
367+
status: 0_u8,
368+
};
369+
370+
Ok(Some(header))
371+
}
372+
350373
/// Read the response from a serial port
351374
pub fn read_response(&mut self) -> Result<Option<CommandResponse>, Error> {
352375
match self.read(10)? {
353376
None => Ok(None),
354377
Some(response) => {
355-
// Here is what esptool does: https://github.com/espressif/esptool/blob/master/esptool/loader.py#L458
378+
// Here is what esptool does: https://github.com/espressif/esptool/blob/81b2eaee261aed0d3d754e32c57959d6b235bfed/esptool/loader.py#L518
356379
// from esptool: things are a bit weird here, bear with us
357380

358381
// We rely on the known and expected response sizes which should be fine for now

espflash/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub enum Error {
8787
#[diagnostic(code(espflash::read_flash::incorrect_digest_length))]
8888
IncorrectDigestLength(usize),
8989

90-
#[error("Incorrect response from the sutb/ROM loader")]
90+
#[error("Incorrect response from the stub/ROM loader")]
9191
#[diagnostic(code(espflash::read_flash::incorrect_response))]
9292
IncorrectResponse,
9393

espflash/src/flasher/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ impl Flasher {
12421242
})?;
12431243

12441244
while data.len() < size as usize {
1245-
let response = self.connection.read_response()?;
1245+
let response = self.connection.read_flash_response()?;
12461246
let chunk: Vec<u8> = if let Some(response) = response {
12471247
response.value.try_into()?
12481248
} else {
@@ -1262,7 +1262,7 @@ impl Flasher {
12621262
return Err(Error::ReadMoreThanExpected);
12631263
}
12641264

1265-
let response = self.connection.read_response()?;
1265+
let response = self.connection.read_flash_response()?;
12661266
let digest: Vec<u8> = if let Some(response) = response {
12671267
response.value.try_into()?
12681268
} else {

espflash/tests/scripts/read-flash.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
KNOWN_PATTERN=$'\x01\xa0\x02\xB3\x04\xC4\x08\xD5\x10\xE6\x20\xF7\x40\x88\x50\x99'
4+
KNOWN_PATTERN+=$'\x60\xAA\x70\xBB\x80\xCC\x90\xDD\xA0\xEE\xB0\xFF\xC0\x11\xD0\x22'
5+
KNOWN_PATTERN+=$'\xE0\x33\xF0\x44\x05\x55\x15\x66\x25\x77\x35\x88\x45\x99\x55\xAA'
6+
KNOWN_PATTERN+=$'\x65\xBB\x75\xCC\x85\xDD\x95\xEE\xA5\xFF\xB5\x00\xC5\x11\xD5\x22'
7+
KNOWN_PATTERN+=$'\xE5\x33\xF5\x44\x06\x55\x16\x66\x26\x77\x36\x88\x46\x99\x56\xAA'
8+
KNOWN_PATTERN+=$'\x66\xBB\x76\xCC\x86\xDD\x96\xEE\xA6\xFF\xB6\x00\xC6\x11\xD6\x22'
9+
10+
echo -ne "$KNOWN_PATTERN" > pattern.bin
11+
result=$(espflash write-bin 0x0 pattern.bin 2>&1)
12+
echo "$result"
13+
if [[ ! $result =~ "Binary successfully written to flash!" ]]; then
14+
echo "Failed to write binary to flash"
15+
exit 1
16+
fi
17+
18+
lengths=(2 5 10 26 44 86)
19+
20+
for len in "${lengths[@]}"; do
21+
echo "Testing read-flash with length: $len"
22+
23+
result=$(espflash read-flash 0 "$len" flash_content.bin 2>&1)
24+
echo "$result"
25+
if [[ ! $result =~ "Flash content successfully read and written to" ]]; then
26+
echo "Failed to read $len bytes from flash"
27+
exit 1
28+
fi
29+
30+
EXPECTED=$(echo -ne "$KNOWN_PATTERN" | head -c "$len")
31+
32+
if ! cmp -s <(echo -ne "$EXPECTED") flash_content.bin; then
33+
echo "Verification failed: content does not match expected for length"
34+
exit 1
35+
fi
36+
done
37+
38+
echo "All read-flash tests passed!"

0 commit comments

Comments
 (0)