Skip to content

Commit 661ed32

Browse files
committed
feat: Update the way we pad bins
1 parent 473701c commit 661ed32

File tree

1 file changed

+7
-27
lines changed

1 file changed

+7
-27
lines changed

espflash/src/bin/espflash.rs

Lines changed: 7 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,18 @@ 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 padded_bytes = 4 - (size % 4);
375354
let mut buffer = Vec::with_capacity(size.try_into().into_diagnostic()?);
376355
f.read_to_end(&mut buffer).into_diagnostic()?;
356+
buffer.extend_from_slice(&vec![0xFF; padded_bytes as usize]);
377357

378358
flasher.write_bin_to_flash(
379359
args.address,

0 commit comments

Comments
 (0)