Skip to content

Commit e481afb

Browse files
authored
move padding to 4 bytes from CLI into library (#951)
Downstream applications needed to duplicate the padding to 4 bytes logic that the CLI application does (although that requirement was not documented). Otherwise flashing could fail with a RomError with error code 0.
1 parent ee7d835 commit e481afb

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- [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)
2020
- [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)
2121
- Fixed ESP32-S2 flash size detection issues (#950)
22+
- Images are now automatically padded to 4 bytes before writing by the library (previously this was done in the CLI) (#951)
2223

2324
### Removed
2425

espflash/src/cli/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,15 +1139,9 @@ pub fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> {
11391139
// Load the file to be flashed
11401140
let mut f = File::open(&args.file).into_diagnostic()?;
11411141

1142-
// If the file size is not divisible by 4, we need to pad `FF` bytes to the end
11431142
let size = f.metadata().into_diagnostic()?.len();
1144-
let mut padded_bytes = 0;
1145-
if size % 4 != 0 {
1146-
padded_bytes = 4 - (size % 4);
1147-
}
11481143
let mut buffer = Vec::with_capacity(size.try_into().into_diagnostic()?);
11491144
f.read_to_end(&mut buffer).into_diagnostic()?;
1150-
buffer.extend(std::iter::repeat_n(0xFF, padded_bytes as usize));
11511145

11521146
let mut flasher = connect(&args.connect_args, config, false, false)?;
11531147
print_board_info(&mut flasher)?;

espflash/src/flasher/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,10 +1076,21 @@ impl Flasher {
10761076
data: &[u8],
10771077
progress: &mut dyn ProgressCallbacks,
10781078
) -> Result<(), Error> {
1079-
let segment = Segment {
1079+
let mut segment = Segment {
10801080
addr,
10811081
data: Cow::from(data),
10821082
};
1083+
1084+
// If the file size is not divisible by 4, we need to pad `FF` bytes to the end
1085+
let size = segment.data.len();
1086+
if size % 4 != 0 {
1087+
let padded_bytes = 4 - (size % 4);
1088+
segment
1089+
.data
1090+
.to_mut()
1091+
.extend(std::iter::repeat_n(0xFF, padded_bytes));
1092+
}
1093+
10831094
self.write_bins_to_flash(&[segment], progress)?;
10841095

10851096
info!("Binary successfully written to flash!");

0 commit comments

Comments
 (0)