Skip to content

Commit

Permalink
Add --target-app-partition argument to flash command (#461)
Browse files Browse the repository at this point in the history
* Add `--target-app-partition` argument to flash command. (For #459)

# Conflicts:
#	cargo-espflash/src/main.rs
#	espflash/src/bin/espflash.rs
#	espflash/src/cli/mod.rs
#	espflash/src/flasher/mod.rs
#	espflash/src/image_format/idf_bootloader.rs
#	espflash/src/targets/esp32.rs
#	espflash/src/targets/esp32c2.rs
#	espflash/src/targets/esp32c3.rs
#	espflash/src/targets/esp32c6.rs
#	espflash/src/targets/esp32h2.rs
#	espflash/src/targets/esp32s2.rs
#	espflash/src/targets/esp32s3.rs
#	espflash/src/targets/esp8266.rs
#	espflash/src/targets/mod.rs

* Update CHANGELOG.md
  • Loading branch information
jnross authored Nov 4, 2023
1 parent eaf14ae commit 39fbdac
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Read esp-println generated defmt messages (#466)
- Add --target-app-partition argument to flash command (#461)

### Fixed

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 @@ -323,6 +323,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
&elf_data,
bootloader,
partition_table,
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 @@ -564,6 +565,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 @@ -245,6 +245,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
&elf_data,
bootloader,
partition_table,
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 @@ -307,6 +308,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 @@ -135,6 +135,9 @@ pub struct FlashArgs {
/// Path to a CSV file containing partition table
#[arg(long, value_name = "FILE")]
pub partition_table: Option<PathBuf>,
/// 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 @@ -178,6 +181,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 @@ -325,6 +331,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 @@ -366,6 +373,7 @@ pub fn save_elf_as_image(
&image,
bootloader,
partition_table,
target_app_partition,
image_format,
None,
flash_mode,
Expand Down Expand Up @@ -406,6 +414,7 @@ pub fn save_elf_as_image(
&image,
None,
None,
None,
image_format,
None,
flash_mode,
Expand Down Expand Up @@ -519,6 +528,7 @@ pub fn flash_elf_image(
elf_data: &[u8],
bootloader: Option<&Path>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
Expand All @@ -541,6 +551,7 @@ pub fn flash_elf_image(
elf_data,
bootloader,
partition_table,
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 @@ -769,6 +769,7 @@ impl Flasher {
elf_data: &[u8],
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
Expand Down Expand Up @@ -796,6 +797,7 @@ impl Flasher {
&image,
bootloader,
partition_table,
target_app_partition,
image_format,
chip_revision,
flash_mode,
Expand Down Expand Up @@ -844,6 +846,7 @@ impl Flasher {
elf_data: &[u8],
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
flash_freq: Option<FlashFrequency>,
Expand All @@ -853,6 +856,7 @@ impl Flasher {
elf_data,
bootloader,
partition_table,
target_app_partition,
None,
flash_mode,
flash_size,
Expand Down
48 changes: 34 additions & 14 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 @@ -35,6 +35,7 @@ impl<'a> IdfBootloaderFormat<'a> {
chip: Chip,
params: Esp32Params,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
bootloader: Option<Vec<u8>>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
Expand Down Expand Up @@ -144,17 +145,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(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(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 @@ -163,7 +174,7 @@ impl<'a> IdfBootloaderFormat<'a> {
}

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

Expand Down Expand Up @@ -313,9 +324,18 @@ pub mod tests {
let expected_bin = fs::read("tests/resources/esp32_hal_blinky.bin").unwrap();

let image = ElfFirmwareImage::try_from(input_bytes.as_slice()).unwrap();
let flash_image =
IdfBootloaderFormat::new(&image, Chip::Esp32, PARAMS, None, None, None, None, None)
.unwrap();
let flash_image = IdfBootloaderFormat::new(
&image,
Chip::Esp32,
PARAMS,
None,
None,
None,
None,
None,
None,
)
.unwrap();

let segments = flash_image.flash_segments().collect::<Vec<_>>();
assert_eq!(segments.len(), 3);
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 @@ -155,6 +155,7 @@ impl Target for Esp32 {
image: &'a dyn FirmwareImage<'a>,
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -169,6 +170,7 @@ impl Target for Esp32 {
Chip::Esp32,
PARAMS,
partition_table,
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 @@ -89,6 +89,7 @@ impl Target for Esp32c2 {
image: &'a dyn FirmwareImage<'a>,
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -103,6 +104,7 @@ impl Target for Esp32c2 {
Chip::Esp32c2,
PARAMS,
partition_table,
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 @@ -76,6 +76,7 @@ impl Target for Esp32c3 {
image: &'a dyn FirmwareImage<'a>,
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -90,6 +91,7 @@ impl Target for Esp32c3 {
Chip::Esp32c3,
PARAMS,
partition_table,
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 @@ -73,6 +73,7 @@ impl Target for Esp32c6 {
image: &'a dyn FirmwareImage<'a>,
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -87,6 +88,7 @@ impl Target for Esp32c6 {
Chip::Esp32c6,
PARAMS,
partition_table,
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 @@ -81,6 +81,7 @@ impl Target for Esp32h2 {
image: &'a dyn FirmwareImage<'a>,
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -95,6 +96,7 @@ impl Target for Esp32h2 {
Chip::Esp32h2,
PARAMS,
partition_table,
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 @@ -141,6 +141,7 @@ impl Target for Esp32s2 {
image: &'a dyn FirmwareImage<'a>,
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -155,6 +156,7 @@ impl Target for Esp32s2 {
Chip::Esp32s2,
PARAMS,
partition_table,
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 @@ -95,6 +95,7 @@ impl Target for Esp32s3 {
image: &'a dyn FirmwareImage<'a>,
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand All @@ -109,6 +110,7 @@ impl Target for Esp32s3 {
Chip::Esp32s3,
PARAMS,
partition_table,
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 {
image: &'a dyn FirmwareImage<'a>,
_bootloader: Option<Vec<u8>>,
_partition_table: Option<PartitionTable>,
_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 @@ -297,6 +297,7 @@ pub trait Target: ReadEFuse {
image: &'a dyn FirmwareImage<'a>,
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand Down

0 comments on commit 39fbdac

Please sign in to comment.