Skip to content

Commit e41be4f

Browse files
Fix write-bin padding (#788)
* feat: Update the way we pad bins * tests: Add HIL test for writting bins * ci: Fix timeouts * test: Fix HIL write-bin test * docs: Update changelog * docs: Remove outdate file * feat: Avoid vec allocation
1 parent 473701c commit e41be4f

File tree

5 files changed

+42
-51
lines changed

5 files changed

+42
-51
lines changed

.github/workflows/hil.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,14 @@ jobs:
107107
- name: hold-in-reset test
108108
run: timeout 5 bash espflash/tests/scripts/hold-in-reset.sh
109109

110-
- name: timeout 5 reset test
111-
run: bash espflash/tests/scripts/reset.sh
110+
- name: reset test
111+
run: timeout 5 bash espflash/tests/scripts/reset.sh
112112

113-
- name: timeout 40 checksum-md5 test
114-
run: bash espflash/tests/scripts/checksum-md5.sh
113+
- name: checksum-md5 test
114+
run: timeout 40 bash espflash/tests/scripts/checksum-md5.sh
115115

116-
- name: timeout 10 list-ports test
117-
run: bash espflash/tests/scripts/list-ports.sh
116+
- name: list-ports test
117+
run: timeout 10 bash espflash/tests/scripts/list-ports.sh
118+
119+
- name: write-bin test
120+
run: timeout 20 bash espflash/tests/scripts/write-bin.sh

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Normalized arguments of the CLI commands (#759)
2121
- `board-info` now prints `Security information`. (#758)
2222
- The `command`, `elf` and `error` modules are no longer public (#772)
23-
- `write-bin` now works for files whose lengths are not divisible by 4 (#780)
23+
- `write-bin` now works for files whose lengths are not divisible by 4 (#780, #788)
2424

2525
### Fixed
2626

espflash/src/bin/espflash.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
2-
fs::{self, File, OpenOptions},
3-
io::{Read, Seek, SeekFrom, Write},
2+
fs::{self, File},
3+
io::Read,
44
path::PathBuf,
55
};
66

@@ -342,38 +342,21 @@ fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> {
342342
Ok(())
343343
}
344344

345-
fn pad_to(file: &mut File, alignment: u64, pad_character: u8) -> Result<()> {
346-
let current_size = file.metadata().into_diagnostic()?.len();
347-
let pad_mod = current_size % alignment;
348-
349-
if pad_mod != 0 {
350-
let pad_size = alignment - pad_mod;
351-
352-
// Move the file cursor to the end of the file
353-
file.seek(SeekFrom::End(0)).into_diagnostic()?;
354-
355-
file.write_all(&vec![pad_character; pad_size as usize])
356-
.into_diagnostic()?;
357-
}
358-
359-
Ok(())
360-
}
361-
362345
fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> {
363346
let mut flasher = connect(&args.connect_args, config, false, false)?;
364347
print_board_info(&mut flasher)?;
365348

366-
// if the file size is not divisible by 4, we need to pad FF bytes to the end of
367-
// the file, that's why we need `write` permission as well
368-
let mut f = OpenOptions::new()
369-
.read(true)
370-
.write(true)
371-
.open(&args.file)
372-
.into_diagnostic()?;
373-
pad_to(&mut f, 4, 0xFF)?;
349+
let mut f = File::open(&args.file).into_diagnostic()?;
350+
351+
// If the file size is not divisible by 4, we need to pad `FF` bytes to the end
374352
let size = f.metadata().into_diagnostic()?.len();
353+
let mut padded_bytes = 0;
354+
if size % 4 != 0 {
355+
padded_bytes = 4 - (size % 4);
356+
}
375357
let mut buffer = Vec::with_capacity(size.try_into().into_diagnostic()?);
376358
f.read_to_end(&mut buffer).into_diagnostic()?;
359+
buffer.extend(std::iter::repeat(0xFF).take(padded_bytes as usize));
377360

378361
flasher.write_bin_to_flash(
379362
args.address,

espflash/tests/README.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

espflash/tests/scripts/write-bin.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
# https://github.com/esp-rs/espflash/issues/622 reproducer
4+
echo -ne "\x01\xa0" >binary_file.bin
5+
result=$(espflash write-bin 0x0 binary_file.bin 2>&1)
6+
echo "$result"
7+
if [[ ! $result =~ "Binary successfully written to flash!" ]]; then
8+
echo "Failed to write binary"
9+
exit 1
10+
fi
11+
12+
result=$(espflash read-flash 0 64 flash_content.bin 2>&1)
13+
echo "$result"
14+
if [[ ! $result =~ "Flash content successfully read and written to" ]]; then
15+
echo "Failed to read flash content"
16+
exit 1
17+
fi
18+
# Check that the flash_content.bin contains the '01 a0' bytes
19+
if ! grep -q -a -F $'\x01\xa0' flash_content.bin; then
20+
echo "Failed verifying content"
21+
exit 1
22+
fi

0 commit comments

Comments
 (0)