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
# 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
  • Loading branch information
jnross committed Aug 24, 2023
1 parent c80b91b commit d42d67b
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 @@ -265,6 +265,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 @@ -505,6 +506,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 @@ -206,6 +206,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 @@ -267,6 +268,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 @@ -106,6 +106,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 @@ -146,6 +149,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 @@ -289,6 +295,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 @@ -330,6 +337,7 @@ pub fn save_elf_as_image(
&image,
bootloader,
partition_table,
target_app_partition,
image_format,
None,
flash_mode,
Expand Down Expand Up @@ -370,6 +378,7 @@ pub fn save_elf_as_image(
&image,
None,
None,
None,
image_format,
None,
flash_mode,
Expand Down Expand Up @@ -451,6 +460,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 @@ -473,6 +483,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 @@ -722,6 +722,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 @@ -749,6 +750,7 @@ impl Flasher {
&image,
bootloader,
partition_table,
target_app_partition,
image_format,
chip_revision,
flash_mode,
Expand Down Expand Up @@ -797,6 +799,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 @@ -806,6 +809,7 @@ impl Flasher {
elf_data,
bootloader,
partition_table,
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 @@ -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
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 @@ -74,6 +74,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 d42d67b

Please sign in to comment.