diff --git a/CHANGELOG.md b/CHANGELOG.md index 61c2e119..b4500774 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [Windows] Fixed a crash in monitor when espflash is connected via USB Serial/JTAG, and the user is typing into the monitor but the device is not reading serial input. (#943) - [Linux/MacOS] Fixed espflash hanging when espflash is connected via USB Serial/JTAG, and the user is typing into the monitor but the device is not reading serial input. (#944, #945) - Fixed ESP32-S2 flash size detection issues (#950) +- Images are now automatically padded to 4 bytes before writing by the library (previously this was done in the CLI) (#951) ### Removed diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index f6abdc31..8627f1c4 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -1139,15 +1139,9 @@ pub fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> { // Load the file to be flashed 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(std::iter::repeat_n(0xFF, padded_bytes as usize)); let mut flasher = connect(&args.connect_args, config, false, false)?; print_board_info(&mut flasher)?; diff --git a/espflash/src/flasher/mod.rs b/espflash/src/flasher/mod.rs index 8cb0356a..f68a2549 100644 --- a/espflash/src/flasher/mod.rs +++ b/espflash/src/flasher/mod.rs @@ -1076,10 +1076,21 @@ impl Flasher { data: &[u8], progress: &mut dyn ProgressCallbacks, ) -> Result<(), Error> { - let segment = Segment { + let mut segment = Segment { addr, data: Cow::from(data), }; + + // If the file size is not divisible by 4, we need to pad `FF` bytes to the end + let size = segment.data.len(); + if size % 4 != 0 { + let padded_bytes = 4 - (size % 4); + segment + .data + .to_mut() + .extend(std::iter::repeat_n(0xFF, padded_bytes)); + } + self.write_bins_to_flash(&[segment], progress)?; info!("Binary successfully written to flash!");