Skip to content

Commit 88a72ae

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 cc26a69 commit 88a72ae

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

espflash/src/image_format.rs

Lines changed: 28 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,23 @@ 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+
let bootloader_sha_start =
175+
calc_bootloader_size + (16 - ((calc_bootloader_size + 1) % 16)) + 1;
176+
println!("{}", bootloader_sha_start);
161177
let mut hasher = Sha256::new();
162-
hasher.update(&bootloader[..bootloader_len - 32]);
178+
hasher.update(&bootloader[..bootloader_sha_start]);
163179
let hash = hasher.finalize();
164-
bootloader.to_mut()[bootloader_len - 32..].copy_from_slice(&hash);
180+
println!(
181+
"before: {:X?}",
182+
&bootloader[bootloader_sha_start..bootloader_sha_start + 32]
183+
);
184+
bootloader.to_mut()[bootloader_sha_start..bootloader_sha_start + 32].copy_from_slice(&hash);
185+
println!(
186+
"after: {:X?}",
187+
&bootloader[bootloader_sha_start..bootloader_sha_start + 32]
188+
);
165189

166190
// write the header of the app
167191
// use the same settings as the bootloader

0 commit comments

Comments
 (0)