Skip to content

Commit

Permalink
set partition table offset with --partition-table-offset flag (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: Myk Melez <myk@junelife.com>
  • Loading branch information
jnross and mykmelez committed Aug 22, 2023
1 parent 5a16115 commit 327c35a
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 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.partition_table_offset,
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
1 change: 1 addition & 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.partition_table_offset,
args.flash_args.format,
args.flash_config_args.flash_mode,
args.flash_config_args.flash_size,
Expand Down
6 changes: 6 additions & 0 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ pub struct FlashArgs {
/// Path to a CSV file containing partition table
#[arg(long, value_name = "FILE")]
pub partition_table: Option<PathBuf>,
#[arg(long)]
pub partition_table_offset: Option<u32>,
/// Load the application to RAM instead of Flash
#[arg(long)]
pub ram: bool,
Expand Down Expand Up @@ -330,6 +332,7 @@ pub fn save_elf_as_image(
&image,
bootloader,
partition_table,
None,
image_format,
None,
flash_mode,
Expand Down Expand Up @@ -370,6 +373,7 @@ pub fn save_elf_as_image(
&image,
None,
None,
None,
image_format,
None,
flash_mode,
Expand Down Expand Up @@ -451,6 +455,7 @@ pub fn flash_elf_image(
elf_data: &[u8],
bootloader: Option<&Path>,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
image_format: Option<ImageFormatKind>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
Expand All @@ -473,6 +478,7 @@ pub fn flash_elf_image(
elf_data,
bootloader,
partition_table,
partition_table_offset,
image_format,
flash_mode,
flash_size,
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>,
partition_table_offset: Option<u32>,
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,
partition_table_offset,
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>,
partition_table_offset: Option<u32>,
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,
partition_table_offset,
None,
flash_mode,
flash_size,
Expand Down
22 changes: 18 additions & 4 deletions espflash/src/image_format/idf_bootloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct IdfBootloaderFormat<'a> {
params: Esp32Params,
bootloader: Cow<'a, [u8]>,
partition_table: PartitionTable,
partition_table_offset: u32,
flash_segment: RomSegment<'a>,
app_size: u32,
part_size: u32,
Expand All @@ -35,6 +36,7 @@ impl<'a> IdfBootloaderFormat<'a> {
chip: Chip,
params: Esp32Params,
partition_table: Option<PartitionTable>,
partition_table_offset: Option<u32>,
bootloader: Option<Vec<u8>>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
Expand All @@ -48,6 +50,8 @@ impl<'a> IdfBootloaderFormat<'a> {
Cow::Borrowed(params.default_bootloader)
};

let partition_table_offset = partition_table_offset.unwrap_or(params.partition_addr);

// fetch the generated header from the bootloader
let mut header: ImageHeader = *from_bytes(&bootloader[0..size_of::<ImageHeader>()]);
if header.magic != ESP_MAGIC {
Expand Down Expand Up @@ -171,6 +175,7 @@ impl<'a> IdfBootloaderFormat<'a> {
params,
bootloader,
partition_table,
partition_table_offset,
flash_segment,
app_size,
part_size,
Expand All @@ -189,7 +194,7 @@ impl<'a> ImageFormat<'a> for IdfBootloaderFormat<'a> {
data: Cow::Borrowed(&self.bootloader),
})
.chain(once(RomSegment {
addr: self.params.partition_addr,
addr: self.partition_table_offset,
data: Cow::Owned(self.partition_table.to_bin().unwrap()),
}))
.chain(once(self.flash_segment.borrow())),
Expand Down Expand Up @@ -313,9 +318,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>,
partition_table_offset: Option<u32>,
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,
partition_table_offset,
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>,
partition_table_offset: Option<u32>,
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,
partition_table_offset,
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>,
partition_table_offset: Option<u32>,
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,
partition_table_offset,
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>,
partition_table_offset: Option<u32>,
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,
partition_table_offset,
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>,
partition_table_offset: Option<u32>,
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,
partition_table_offset,
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>,
partition_table_offset: Option<u32>,
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,
partition_table_offset,
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>,
partition_table_offset: Option<u32>,
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,
partition_table_offset,
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>,
_partition_table_offset: Option<u32>,
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>,
partition_table_offset: Option<u32>,
image_format: Option<ImageFormatKind>,
chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
Expand Down

0 comments on commit 327c35a

Please sign in to comment.