Skip to content

Commit 0ba42b5

Browse files
author
Christopher Dombroski
committed
Calculate end of bootloader to update SHA256
The current implementation uses the last 32 bytes of the bootloader file. When Secure Boot V2 is enabled, the bootloader is padded. The new implementation walks through the segments to find the end and adds the 16-byte aligned 1-byte checksum to update the SHA256 instead of incorrectly updating the padding. Closes #715
1 parent de2ecaf commit 0ba42b5

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Fixed
1515

16+
- Update the app image SHA in the correct location for padded images (#715)
17+
1618
### Removed
1719

1820
## [3.3.0] - 2025-01-13

espflash/src/image_format.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,23 @@ impl<'a> IdfBootloaderFormat<'a> {
135135
};
136136

137137
// fetch the generated header from the bootloader
138-
let mut header: ImageHeader = *from_bytes(&bootloader[0..size_of::<ImageHeader>()]);
138+
let mut calc_bootloader_size = 0;
139+
let bootloader_header_size = size_of::<ImageHeader>();
140+
calc_bootloader_size += bootloader_header_size;
141+
let mut header: ImageHeader = *from_bytes(&bootloader[0..bootloader_header_size]);
139142
if header.magic != ESP_MAGIC {
140143
return Err(Error::InvalidBootloader);
141144
}
142145

146+
for _ in 0..header.segment_count {
147+
let segment: SegmentHeader = *from_bytes(
148+
&bootloader
149+
[calc_bootloader_size..calc_bootloader_size + size_of::<SegmentHeader>()],
150+
);
151+
println!("{:X?}", segment);
152+
calc_bootloader_size += segment.length as usize + size_of::<SegmentHeader>();
153+
}
154+
143155
// update the header if a user has specified any custom arguments
144156
if let Some(mode) = flash_settings.mode {
145157
header.flash_mode = mode as u8;
@@ -157,11 +169,28 @@ impl<'a> IdfBootloaderFormat<'a> {
157169
);
158170

159171
// re-calculate hash of the bootloader - needed since we modified the header
160-
let bootloader_len = bootloader.len();
172+
// the hash is at the end of the bootloader, but the bootloader bytes are padded.
173+
// the real end of the bootloader is the end of the segments plus the 16-byte aligned 1-byte checksum.
174+
// the checksum is stored in the last byte so that the file is a multiple of 16 bytes.
175+
calc_bootloader_size += 1; // add checksum size
176+
calc_bootloader_size = calc_bootloader_size + ((16 - (calc_bootloader_size % 16)) % 16);
177+
let bootloader_sha_start = calc_bootloader_size;
178+
calc_bootloader_size += 32; // add sha256 size
179+
let bootloader_sha_end = calc_bootloader_size;
180+
181+
println!("{}", bootloader_sha_start);
161182
let mut hasher = Sha256::new();
162-
hasher.update(&bootloader[..bootloader_len - 32]);
183+
hasher.update(&bootloader[..bootloader_sha_start]);
163184
let hash = hasher.finalize();
164-
bootloader.to_mut()[bootloader_len - 32..].copy_from_slice(&hash);
185+
println!(
186+
"before: {:X?}",
187+
&bootloader[bootloader_sha_start..bootloader_sha_end]
188+
);
189+
bootloader.to_mut()[bootloader_sha_start..bootloader_sha_end].copy_from_slice(&hash);
190+
println!(
191+
"after: {:X?}",
192+
&bootloader[bootloader_sha_start..bootloader_sha_end]
193+
);
165194

166195
// write the header of the app
167196
// use the same settings as the bootloader

0 commit comments

Comments
 (0)