Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix write-bin padding #788

Merged
merged 7 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
15 changes: 9 additions & 6 deletions .github/workflows/hil.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ jobs:
- name: hold-in-reset test
run: timeout 5 bash espflash/tests/scripts/hold-in-reset.sh

- name: timeout 5 reset test
run: bash espflash/tests/scripts/reset.sh
- name: reset test
run: timeout 5 bash espflash/tests/scripts/reset.sh

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

- name: timeout 10 list-ports test
run: bash espflash/tests/scripts/list-ports.sh
- name: list-ports test
run: timeout 10 bash espflash/tests/scripts/list-ports.sh

- name: write-bin test
run: timeout 20 bash espflash/tests/scripts/write-bin.sh
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Normalized arguments of the CLI commands (#759)
- `board-info` now prints `Security information`. (#758)
- The `command`, `elf` and `error` modules are no longer public (#772)
- `write-bin` now works for files whose lengths are not divisible by 4 (#780)
- `write-bin` now works for files whose lengths are not divisible by 4 (#780, #788)

### Fixed

Expand Down
37 changes: 10 additions & 27 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fs::{self, File, OpenOptions},
io::{Read, Seek, SeekFrom, Write},
fs::{self, File},
io::Read,
path::PathBuf,
};

Expand Down Expand Up @@ -342,38 +342,21 @@ fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> {
Ok(())
}

fn pad_to(file: &mut File, alignment: u64, pad_character: u8) -> Result<()> {
let current_size = file.metadata().into_diagnostic()?.len();
let pad_mod = current_size % alignment;

if pad_mod != 0 {
let pad_size = alignment - pad_mod;

// Move the file cursor to the end of the file
file.seek(SeekFrom::End(0)).into_diagnostic()?;

file.write_all(&vec![pad_character; pad_size as usize])
.into_diagnostic()?;
}

Ok(())
}

fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> {
let mut flasher = connect(&args.connect_args, config, false, false)?;
print_board_info(&mut flasher)?;

// if the file size is not divisible by 4, we need to pad FF bytes to the end of
// the file, that's why we need `write` permission as well
let mut f = OpenOptions::new()
.read(true)
.write(true)
.open(&args.file)
.into_diagnostic()?;
pad_to(&mut f, 4, 0xFF)?;
let mut f = File::open(&args.file).into_diagnostic()?;

// If the file size is not divisible by 4, we need to pad `FF` bytes to the end
let size = f.metadata().into_diagnostic()?.len();
let mut padded_bytes = 0;
if size % 4 != 0 {
padded_bytes = 4 - (size % 4);
}
let mut buffer = Vec::with_capacity(size.try_into().into_diagnostic()?);
f.read_to_end(&mut buffer).into_diagnostic()?;
buffer.extend_from_slice(&vec![0xFF; padded_bytes as usize]);

flasher.write_bin_to_flash(
args.address,
Expand Down
17 changes: 0 additions & 17 deletions espflash/tests/README.md

This file was deleted.

22 changes: 22 additions & 0 deletions espflash/tests/scripts/write-bin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# https://github.com/esp-rs/espflash/issues/622 reproducer
echo -ne "\x01\xa0" >binary_file.bin
result=$(espflash write-bin 0x0 binary_file.bin 2>&1)
echo "$result"
if [[ ! $result =~ "Binary successfully written to flash!" ]]; then
echo "Failed to write binary"
exit 1
fi

result=$(espflash read-flash 0 64 flash_content.bin 2>&1)
echo "$result"
if [[ ! $result =~ "Flash content successfully read and written to" ]]; then
echo "Failed to read flash content"
exit 1
fi
# Check that the flash_content.bin contains the '01 a0' bytes
if ! grep -q -a -F $'\x01\xa0' flash_content.bin; then
echo "Failed verifying content"
exit 1
fi
Loading