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
chris-subtlebytes committed Jan 22, 2025
1 parent de2ecaf commit 7a83fc7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Update the app image SHA in the correct location for padded images (#715)

### Removed

## [3.3.0] - 2025-01-13
Expand Down
3 changes: 1 addition & 2 deletions espflash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ directories = { version = "5.0.1", optional = true }
env_logger = { version = "0.11.6", optional = true }
esp-idf-part = "0.5.0"
flate2 = "1.0.35"
hex = { version = "0.4.3", features = ["serde"], optional = true }
hex = { version = "0.4.3", features = ["serde"]}
indicatif = { version = "0.17.9", optional = true }
lazy_static = { version = "1.5.0", optional = true }
log = "0.4.22"
Expand Down Expand Up @@ -79,7 +79,6 @@ cli = [
"dep:dialoguer",
"dep:directories",
"dep:env_logger",
"dep:hex",
"dep:indicatif",
"dep:lazy_static",
"dep:parse_int",
Expand Down
33 changes: 29 additions & 4 deletions espflash/src/image_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{borrow::Cow, io::Write, iter::once, mem::size_of};

use bytemuck::{bytes_of, from_bytes, Pod, Zeroable};
use esp_idf_part::{Partition, PartitionTable, Type};
use log;
use sha2::{Digest, Sha256};

use crate::{
Expand Down Expand Up @@ -135,11 +136,22 @@ 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>()],
);
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,24 @@ 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;

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);
log::info!(
"Updating bootloader SHA256 from {} to {}",
hex::encode(&bootloader[bootloader_sha_start..bootloader_sha_end]),
hex::encode(hash)
);
bootloader.to_mut()[bootloader_sha_start..bootloader_sha_end].copy_from_slice(&hash);

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

0 comments on commit 7a83fc7

Please sign in to comment.