From 1968a71e88cc0609755de3fba1d0364dbbdf6046 Mon Sep 17 00:00:00 2001 From: thetek Date: Wed, 28 May 2025 12:30:01 +0200 Subject: [PATCH 1/6] Make reset module pub again --- espflash/src/connection/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/espflash/src/connection/mod.rs b/espflash/src/connection/mod.rs index 03d09a7a..c2850e0e 100644 --- a/espflash/src/connection/mod.rs +++ b/espflash/src/connection/mod.rs @@ -37,7 +37,7 @@ use crate::{ target::Chip, }; -pub(crate) mod reset; +pub mod reset; pub use reset::{ResetAfterOperation, ResetBeforeOperation}; From 2aa591b94cf1b3c926378151ae06845d001ee2cf Mon Sep 17 00:00:00 2001 From: thetek Date: Thu, 5 Jun 2025 10:16:08 +0200 Subject: [PATCH 2/6] Add secure-pad-v2 option with separate padding segment --- espflash/src/cli/mod.rs | 5 +++++ espflash/src/flasher/mod.rs | 4 ++++ espflash/src/image_format/idf.rs | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index e666ee54..a50596d3 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -279,6 +279,9 @@ pub struct IdfFormatArgs { /// Label of target app partition #[arg(long, value_name = "LABEL")] pub target_app_partition: Option, + /// Whether to apply padding for secure boot v2 + #[arg(long)] + pub secure_pad_v2: bool, } /// Arguments for connection and monitoring @@ -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 @@ -1121,6 +1125,7 @@ pub fn make_flash_data( image_args.mmu_page_size, chip, xtal_freq, + secure_pad_v2, ) } diff --git a/espflash/src/flasher/mod.rs b/espflash/src/flasher/mod.rs index 8cb0356a..48926c21 100644 --- a/espflash/src/flasher/mod.rs +++ b/espflash/src/flasher/mod.rs @@ -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 { @@ -498,6 +500,7 @@ impl FlashData { mmu_page_size: Option, chip: Chip, xtal_freq: XtalFrequency, + secure_pad_v2: bool, ) -> Self { FlashData { flash_settings, @@ -505,6 +508,7 @@ impl FlashData { mmu_page_size, chip, xtal_freq, + secure_pad_v2, } } } diff --git a/espflash/src/image_format/idf.rs b/espflash/src/image_format/idf.rs index 7223b260..801ab562 100644 --- a/espflash/src/image_format/idf.rs +++ b/espflash/src/image_format/idf.rs @@ -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)?; From b0cbd304e944b869b295fcc66ed73c4a5fca9b51 Mon Sep 17 00:00:00 2001 From: thetek Date: Thu, 5 Jun 2025 11:32:52 +0200 Subject: [PATCH 3/6] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b381e44c..83503644 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `DefaultProgressCallback` which implements `ProgressCallbacks` but all methods are no-ops (#904) - `ProgressCallbacks` now has a `verifying` method to notify when post-flash checksum checking has begun (#908) - Implement `From for Port` and both `From for Connection` and `Port` conversions (#915) +- Add support for serial port configuration files. (#777) +- Add support for secure padding for save-image (#876) ### Changed From 3f10da584bee8f659e7fbd4c3e6bce4cc9e47c74 Mon Sep 17 00:00:00 2001 From: thetek Date: Mon, 7 Jul 2025 10:53:30 +0200 Subject: [PATCH 4/6] Fix compilation --- CHANGELOG.md | 2 +- cargo-espflash/src/main.rs | 2 ++ espflash/src/bin/espflash.rs | 2 ++ espflash/src/connection/reset.rs | 5 +++++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83503644..847307a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -48,7 +49,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `ProgressCallbacks` now has a `verifying` method to notify when post-flash checksum checking has begun (#908) - Implement `From for Port` and both `From for Connection` and `Port` conversions (#915) - Add support for serial port configuration files. (#777) -- Add support for secure padding for save-image (#876) ### Changed diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index 41b1c7e9..dd5f7af9 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -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, @@ -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, diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index bde23e8d..fd8c629e 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -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, @@ -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, diff --git a/espflash/src/connection/reset.rs b/espflash/src/connection/reset.rs index f225cb45..4792f99b 100644 --- a/espflash/src/connection/reset.rs +++ b/espflash/src/connection/reset.rs @@ -30,20 +30,25 @@ const EXTRA_RESET_DELAY: u64 = 500; // ms /// Reset strategies for resetting a target device. pub trait ResetStrategy { + /// Resets the target device. fn reset(&self, serial_port: &mut Port) -> Result<(), Error>; + /// Sets DTR (data terminal ready) to a specified level. fn set_dtr(&self, serial_port: &mut Port, level: bool) -> Result<(), Error> { serial_port.write_data_terminal_ready(level)?; Ok(()) } + /// Sets RTS (request to send) to a specified level. fn set_rts(&self, serial_port: &mut Port, level: bool) -> Result<(), Error> { serial_port.write_request_to_send(level)?; Ok(()) } + /// Sets RTS (request to send) and DTS (data termina lready) to specified + /// levels. #[cfg(unix)] fn set_dtr_rts( &self, From cc2ee530dc9c2f6b6361ee18a30f9d8fb4e55388 Mon Sep 17 00:00:00 2001 From: thetek Date: Mon, 7 Jul 2025 11:21:55 +0200 Subject: [PATCH 5/6] Make reset module private again --- espflash/src/connection/mod.rs | 2 +- espflash/src/connection/reset.rs | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/espflash/src/connection/mod.rs b/espflash/src/connection/mod.rs index c2850e0e..03d09a7a 100644 --- a/espflash/src/connection/mod.rs +++ b/espflash/src/connection/mod.rs @@ -37,7 +37,7 @@ use crate::{ target::Chip, }; -pub mod reset; +pub(crate) mod reset; pub use reset::{ResetAfterOperation, ResetBeforeOperation}; diff --git a/espflash/src/connection/reset.rs b/espflash/src/connection/reset.rs index 4792f99b..f225cb45 100644 --- a/espflash/src/connection/reset.rs +++ b/espflash/src/connection/reset.rs @@ -30,25 +30,20 @@ const EXTRA_RESET_DELAY: u64 = 500; // ms /// Reset strategies for resetting a target device. pub trait ResetStrategy { - /// Resets the target device. fn reset(&self, serial_port: &mut Port) -> Result<(), Error>; - /// Sets DTR (data terminal ready) to a specified level. fn set_dtr(&self, serial_port: &mut Port, level: bool) -> Result<(), Error> { serial_port.write_data_terminal_ready(level)?; Ok(()) } - /// Sets RTS (request to send) to a specified level. fn set_rts(&self, serial_port: &mut Port, level: bool) -> Result<(), Error> { serial_port.write_request_to_send(level)?; Ok(()) } - /// Sets RTS (request to send) and DTS (data termina lready) to specified - /// levels. #[cfg(unix)] fn set_dtr_rts( &self, From 994d97ba287066c9ab181d6d00ea4173a08e2b12 Mon Sep 17 00:00:00 2001 From: thetek Date: Mon, 7 Jul 2025 11:26:30 +0200 Subject: [PATCH 6/6] Clean up messed up changelog merge --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 847307a4..e7da34fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,7 +48,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `DefaultProgressCallback` which implements `ProgressCallbacks` but all methods are no-ops (#904) - `ProgressCallbacks` now has a `verifying` method to notify when post-flash checksum checking has begun (#908) - Implement `From for Port` and both `From for Connection` and `Port` conversions (#915) -- Add support for serial port configuration files. (#777) ### Changed