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 6 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 @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- `save-image` now checks if the ELF contains the app descriptor (#920)
- Add support for secure padding for save-image (#876)

### Changed

Expand Down
2 changes: 2 additions & 0 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
config,
chip,
target_xtal_freq,
args.idf_format_args.secure_pad_v2,
);
let image_format = make_image_format(
&elf_data,
Expand Down Expand Up @@ -643,6 +644,7 @@ fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> {
config,
args.save_image_args.chip,
xtal_freq,
args.idf_format_args.secure_pad_v2,
);
let image_format = make_image_format(
&elf_data,
Expand Down
2 changes: 2 additions & 0 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
config,
chip,
target_xtal_freq,
args.idf_format_args.secure_pad_v2,
);
let image_format = make_image_format(
&elf_data,
Expand Down Expand Up @@ -370,6 +371,7 @@ fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> {
config,
args.save_image_args.chip,
xtal_freq,
args.idf_format_args.secure_pad_v2,
);
let image_format = make_image_format(
&elf_data,
Expand Down
5 changes: 5 additions & 0 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ pub struct IdfFormatArgs {
/// Label of target app partition
#[arg(long, value_name = "LABEL")]
pub target_app_partition: Option<String>,
/// Whether to apply padding for secure boot v2
#[arg(long)]
pub secure_pad_v2: bool,
}

/// Arguments for connection and monitoring
Expand Down Expand Up @@ -1100,6 +1103,7 @@ pub fn make_flash_data(
config: &Config,
chip: Chip,
xtal_freq: XtalFrequency,
secure_pad_v2: bool,
) -> FlashData {
// Create flash settings with precedence
let mode = flash_config_args
Expand All @@ -1121,6 +1125,7 @@ pub fn make_flash_data(
image_args.mmu_page_size,
chip,
xtal_freq,
secure_pad_v2,
)
}

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 @@ -488,6 +488,8 @@ pub struct FlashData {
pub chip: Chip,
/// Crystal frequency.
pub xtal_freq: XtalFrequency,
/// Whether to add secure boot V2 padding.
pub secure_pad_v2: bool,
}

impl FlashData {
Expand All @@ -498,13 +500,15 @@ impl FlashData {
mmu_page_size: Option<u32>,
chip: Chip,
xtal_freq: XtalFrequency,
secure_pad_v2: bool,
) -> Self {
FlashData {
flash_settings,
min_chip_rev,
mmu_page_size,
chip,
xtal_freq,
secure_pad_v2,
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions espflash/src/image_format/idf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,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