Skip to content

Commit

Permalink
Add --target-app-partition argument to flash command. (For #459)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnross committed Aug 22, 2023
1 parent f59218f commit 6d4730b
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 11 deletions.
2 changes: 2 additions & 0 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
bootloader,
partition_table,
args.flash_args.partition_table_offset,
args.flash_args.target_app_partition,
args.flash_args.format.or(metadata.format),
args.build_args.flash_config_args.flash_mode,
args.build_args.flash_config_args.flash_size,
Expand Down Expand Up @@ -506,6 +507,7 @@ fn save_image(args: SaveImageArgs) -> Result<()> {
args.save_image_args.merge,
bootloader,
partition_table,
args.save_image_args.target_app_partition,
args.save_image_args.skip_padding,
)?;

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 @@ -234,6 +234,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
bootloader,
partition_table,
args.flash_args.partition_table_offset,
args.flash_args.target_app_partition,
args.flash_args.format,
args.flash_config_args.flash_mode,
args.flash_config_args.flash_size,
Expand Down Expand Up @@ -295,6 +296,7 @@ fn save_image(args: SaveImageArgs) -> Result<()> {
args.save_image_args.merge,
args.save_image_args.bootloader,
args.save_image_args.partition_table,
args.save_image_args.target_app_partition,
args.save_image_args.skip_padding,
)?;

Expand Down
11 changes: 11 additions & 0 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ pub struct FlashArgs {
pub partition_table: Option<PathBuf>,
#[arg(long)]
pub partition_table_offset: Option<u32>,
/// Label of target app partition
#[arg(long, value_name = "LABEL")]
pub target_app_partition: Option<String>,
/// Load the application to RAM instead of Flash
#[arg(long)]
pub ram: bool,
Expand Down Expand Up @@ -148,6 +151,9 @@ pub struct SaveImageArgs {
/// Custom partition table for merging
#[arg(long, short = 'T', requires = "merge", value_name = "FILE")]
pub partition_table: Option<PathBuf>,
/// Label of target app partition
#[arg(long, value_name = "LABEL")]
pub target_app_partition: Option<String>,
/// Don't pad the image to the flash size
#[arg(long, short = 'P', requires = "merge")]
pub skip_padding: bool,
Expand Down Expand Up @@ -314,6 +320,7 @@ pub fn save_elf_as_image(
merge: bool,
bootloader_path: Option<PathBuf>,
partition_table_path: Option<PathBuf>,
target_app_partition: Option<String>,
skip_padding: bool,
) -> Result<()> {
let image = ElfFirmwareImage::try_from(elf_data)?;
Expand Down Expand Up @@ -356,6 +363,7 @@ pub fn save_elf_as_image(
bootloader,
partition_table,
None,
target_app_partition,
image_format,
None,
flash_mode,
Expand Down Expand Up @@ -397,6 +405,7 @@ pub fn save_elf_as_image(
None,
None,
None,
None,
image_format,
None,
flash_mode,
Expand Down Expand Up @@ -479,6 +488,7 @@ pub fn flash_elf_image(
bootloader: Option<&Path>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
Expand All @@ -502,6 +512,7 @@ pub fn flash_elf_image(
bootloader,
partition_table,
partition_table_offset,
target_app_partition,
image_format,
flash_mode,
flash_size,
Expand Down
4 changes: 4 additions & 0 deletions espflash/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ use crate::{
#[derive(Debug, Diagnostic, Error)]
#[non_exhaustive]
pub enum Error {
#[error("App partition not found")]
#[diagnostic(code(espflash::app_partition_not_found))]
AppPartitionNotFound,

#[error("Operation was cancelled by the user")]
#[diagnostic(code(espflash::cancelled))]
Cancelled,
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 @@ -723,6 +723,7 @@ impl Flasher {
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
Expand Down Expand Up @@ -751,6 +752,7 @@ impl Flasher {
bootloader,
partition_table,
partition_table_offset,
target_app_partition,
image_format,
chip_revision,
flash_mode,
Expand Down Expand Up @@ -800,6 +802,7 @@ impl Flasher {
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
flash_freq: Option<FlashFrequency>,
Expand All @@ -810,6 +813,7 @@ impl Flasher {
bootloader,
partition_table,
partition_table_offset,
target_app_partition,
None,
flash_mode,
flash_size,
Expand Down
33 changes: 22 additions & 11 deletions espflash/src/image_format/idf_bootloader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, io::Write, iter::once, mem::size_of};

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

use crate::{
Expand Down Expand Up @@ -37,6 +37,7 @@ impl<'a> IdfBootloaderFormat<'a> {
params: Esp32Params,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
bootloader: Option<Vec<u8>>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
Expand Down Expand Up @@ -148,17 +149,27 @@ impl<'a> IdfBootloaderFormat<'a> {
let hash = hasher.finalize();
data.write_all(&hash)?;

// The default partition table contains the "factory" partition, and if a user
// provides a partition table via command-line then the validation step confirms
// that at least one "app" partition is present. We prefer the "factory"
// partition, and use any available "app" partitions if not present.
let factory_partition = partition_table
.find("factory")
.or_else(|| partition_table.find_by_type(Type::App))
.unwrap();
let target_app_partition: &Partition =
// Use the target app partition if provided
if let Some(target_partition) = target_app_partition {
partition_table
.find(&target_partition)
.ok_or_else(|| Error::AppPartitionNotFound)?
} else {

// The default partition table contains the "factory" partition, and if a user
// provides a partition table via command-line then the validation step confirms
// that at least one "app" partition is present. We prefer the "factory"
// partition, and use any available "app" partitions if not present.

partition_table
.find("factory")
.or_else(|| partition_table.find_by_type(Type::App))
.ok_or_else(|| Error::AppPartitionNotFound)?
};

let app_size = data.len() as u32;
let part_size = factory_partition.size();
let part_size = target_app_partition.size();

// The size of the application must not exceed the size of the factory
// partition.
Expand All @@ -167,7 +178,7 @@ impl<'a> IdfBootloaderFormat<'a> {
}

let flash_segment = RomSegment {
addr: factory_partition.offset(),
addr: target_app_partition.offset(),
data: Cow::Owned(data),
};

Expand Down
2 changes: 2 additions & 0 deletions espflash/src/targets/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ impl Target for Esp32 {
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -171,6 +172,7 @@ impl Target for Esp32 {
PARAMS,
partition_table,
partition_table_offset,
target_app_partition,
bootloader,
flash_mode,
flash_size,
Expand Down
2 changes: 2 additions & 0 deletions espflash/src/targets/esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl Target for Esp32c2 {
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -105,6 +106,7 @@ impl Target for Esp32c2 {
PARAMS,
partition_table,
partition_table_offset,
target_app_partition,
bootloader,
flash_mode,
flash_size,
Expand Down
2 changes: 2 additions & 0 deletions espflash/src/targets/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl Target for Esp32c3 {
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -92,6 +93,7 @@ impl Target for Esp32c3 {
PARAMS,
partition_table,
partition_table_offset,
target_app_partition,
bootloader,
flash_mode,
flash_size,
Expand Down
2 changes: 2 additions & 0 deletions espflash/src/targets/esp32c6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Target for Esp32c6 {
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -89,6 +90,7 @@ impl Target for Esp32c6 {
PARAMS,
partition_table,
partition_table_offset,
target_app_partition,
bootloader,
flash_mode,
flash_size,
Expand Down
2 changes: 2 additions & 0 deletions espflash/src/targets/esp32h2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl Target for Esp32h2 {
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -97,6 +98,7 @@ impl Target for Esp32h2 {
PARAMS,
partition_table,
partition_table_offset,
target_app_partition,
bootloader,
flash_mode,
flash_size,
Expand Down
2 changes: 2 additions & 0 deletions espflash/src/targets/esp32s2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ impl Target for Esp32s2 {
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -157,6 +158,7 @@ impl Target for Esp32s2 {
PARAMS,
partition_table,
partition_table_offset,
target_app_partition,
bootloader,
flash_mode,
flash_size,
Expand Down
2 changes: 2 additions & 0 deletions espflash/src/targets/esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl Target for Esp32s3 {
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -111,6 +112,7 @@ impl Target for Esp32s3 {
PARAMS,
partition_table,
partition_table_offset,
target_app_partition,
bootloader,
flash_mode,
flash_size,
Expand Down
1 change: 1 addition & 0 deletions espflash/src/targets/esp8266.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl Target for Esp8266 {
_bootloader: Option<Vec<u8>>,
_partition_table: Option<PartitionTable>,
_partition_table_offset: Option<u32>,
_target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand Down
1 change: 1 addition & 0 deletions espflash/src/targets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ pub trait Target: ReadEFuse {
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand Down

0 comments on commit 6d4730b

Please sign in to comment.