Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate end of bootloader to update SHA256 #716

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 31 additions & 5 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::debug;
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 @@ -156,12 +168,26 @@ impl<'a> IdfBootloaderFormat<'a> {
bytes_of(&header).iter().copied(),
);

// re-calculate hash of the bootloader - needed since we modified the header
let bootloader_len = bootloader.len();
// The header was modified so we need to recalculate the hash of the
// bootloader. The hash is at the end of the bootloader segments and
// 1-byte checksum at the end of a 16-byte padded boundary.
//
// Source: Point 3 of https://docs.espressif.com/projects/esp-idf/en/v5.4/esp32c3/api-reference/system/app_image_format.html
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);
debug!(
"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