Skip to content

Commit

Permalink
Calculate end of bootloader to update SHA256
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Christopher Dombroski committed Jan 19, 2025
1 parent cc26a69 commit db05d51
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support external log-processors (#705)
- Make the `libudev` dependency optional with a new - enabled by default - feature: `libudev` (#709)
- Address Clippy lints (#710)
- Update the app image SHA in the correct location for padded images (#715)

### Changed

Expand Down
37 changes: 33 additions & 4 deletions espflash/src/image_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,23 @@ impl<'a> IdfBootloaderFormat<'a> {
};

// fetch the generated header from the bootloader
let mut header: ImageHeader = *from_bytes(&bootloader[0..size_of::<ImageHeader>()]);
let mut calc_bootloader_size = 0;
let bootloader_header_size = size_of::<ImageHeader>();
calc_bootloader_size += bootloader_header_size;
let mut header: ImageHeader = *from_bytes(&bootloader[0..bootloader_header_size]);
if header.magic != ESP_MAGIC {
return Err(Error::InvalidBootloader);
}

for _ in 0..header.segment_count {
let segment: SegmentHeader = *from_bytes(
&bootloader
[calc_bootloader_size..calc_bootloader_size + size_of::<SegmentHeader>()],
);
println!("{:X?}", segment);
calc_bootloader_size += segment.length as usize + size_of::<SegmentHeader>();
}

// update the header if a user has specified any custom arguments
if let Some(mode) = flash_settings.mode {
header.flash_mode = mode as u8;
Expand All @@ -157,11 +169,28 @@ impl<'a> IdfBootloaderFormat<'a> {
);

// re-calculate hash of the bootloader - needed since we modified the header
let bootloader_len = bootloader.len();
// the hash is at the end of the bootloader, but the bootloader bytes are padded.
// the real end of the bootloader is the end of the segments plus the 16-byte aligned 1-byte checksum.
// the checksum is stored in the last byte so that the file is a multiple of 16 bytes.
calc_bootloader_size += 1; // add checksum size
calc_bootloader_size = calc_bootloader_size + ((16 - (calc_bootloader_size % 16)) % 16);
let bootloader_sha_start = calc_bootloader_size;
calc_bootloader_size += 32; // add sha256 size
let bootloader_sha_end = calc_bootloader_size;

println!("{}", bootloader_sha_start);
let mut hasher = Sha256::new();
hasher.update(&bootloader[..bootloader_len - 32]);
hasher.update(&bootloader[..bootloader_sha_start]);
let hash = hasher.finalize();
bootloader.to_mut()[bootloader_len - 32..].copy_from_slice(&hash);
println!(
"before: {:X?}",
&bootloader[bootloader_sha_start..bootloader_sha_end]
);
bootloader.to_mut()[bootloader_sha_start..bootloader_sha_end].copy_from_slice(&hash);
println!(
"after: {:X?}",
&bootloader[bootloader_sha_start..bootloader_sha_end]
);

// write the header of the app
// use the same settings as the bootloader
Expand Down

0 comments on commit db05d51

Please sign in to comment.