Skip to content

Add secure padding for save-image #876

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add support for the ESP32-C5 (#863)
- `--after` options now work with `espflash board-info`, `espflash read-flash` and `espflash checksum-md5` (#867)
- Add support for serial port configuration files. (#777)
- Add support for secure padding for save-image (#876)

### Changed

Expand Down
4 changes: 4 additions & 0 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ pub struct ImageArgs {
/// MMU page size.
#[arg(long, value_name = "MMU_PAGE_SIZE", value_parser = parse_u32)]
pub mmu_page_size: Option<u32>,
/// Whether to apply padding for secure boot v2
#[arg(long)]
pub secure_pad_v2: bool,
}

#[derive(Debug, Args)]
Expand Down Expand Up @@ -1060,6 +1063,7 @@ pub fn make_flash_data(
flash_settings,
image_args.min_chip_rev,
image_args.mmu_page_size,
image_args.secure_pad_v2,
)
}

Expand Down
2 changes: 1 addition & 1 deletion espflash/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
};

pub(crate) mod command;
pub(crate) mod reset;
pub mod reset;

const MAX_CONNECT_ATTEMPTS: usize = 7;
const MAX_SYNC_ATTEMPTS: usize = 5;
Expand Down
4 changes: 4 additions & 0 deletions espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,11 @@ pub struct FlashData {
pub flash_settings: FlashSettings,
pub min_chip_rev: u16,
pub mmu_page_size: Option<u32>,
pub secure_pad_v2: bool,
}

impl FlashData {
#[allow(clippy::too_many_arguments)]
pub fn new(
bootloader: Option<&Path>,
partition_table: Option<&Path>,
Expand All @@ -488,6 +490,7 @@ impl FlashData {
flash_settings: FlashSettings,
min_chip_rev: u16,
mmu_page_size: Option<u32>,
secure_pad_v2: bool,
) -> Result<Self, Error> {
// If the '--bootloader' option is provided, load the binary file at the
// specified path.
Expand Down Expand Up @@ -520,6 +523,7 @@ impl FlashData {
flash_settings,
min_chip_rev,
mmu_page_size,
secure_pad_v2,
})
}
}
Expand Down
12 changes: 12 additions & 0 deletions espflash/src/image_format/esp_idf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,18 @@ impl<'a> IdfBootloaderFormat<'a> {
segment_count += 1;
}

if flash_data.secure_pad_v2 {
let current_size = data.len();
let padding_size = (65536 - ((current_size + 56) % 65536)) % 65536;
let padding_bytes = vec![0; padding_size];
let segment = Segment {
addr: 0,
data: Cow::Owned(padding_bytes),
};
checksum = save_segment(&mut data, &segment, checksum)?;
segment_count += 1;
}

let padding = 15 - (data.len() % 16);
let padding = &[0u8; 16][0..padding];
data.write_all(padding)?;
Expand Down
Loading