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 for files whose lengths are not divisible by 4 by padding FF bytes #780

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +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)

### Fixed

Expand Down
30 changes: 27 additions & 3 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},
io::Read,
fs::{self, File, OpenOptions},
io::{Read, Seek, SeekFrom, Write},
path::PathBuf,
};

Expand Down Expand Up @@ -342,11 +342,35 @@ 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)?;

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 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 size = f.metadata().into_diagnostic()?.len();
let mut buffer = Vec::with_capacity(size.try_into().into_diagnostic()?);
f.read_to_end(&mut buffer).into_diagnostic()?;
Expand Down
Loading