From c41e1242d87f94e75c4295488f02ce7c93b93d28 Mon Sep 17 00:00:00 2001 From: Michael Aspetsberger Date: Mon, 28 Mar 2022 00:05:55 +0200 Subject: [PATCH 1/2] Scale the app partition based on flash size. The pre-defined `app_sizes` for the various `Esp32Param` assume a flash size which is not necessarily correct, e.g. there are esp32-c3 chips with 2MB. --- espflash/src/chip/esp32/mod.rs | 7 +++++-- espflash/src/flasher.rs | 16 ++++++++++++++++ espflash/src/image_format/esp32bootloader.rs | 3 ++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/espflash/src/chip/esp32/mod.rs b/espflash/src/chip/esp32/mod.rs index c2fd727b..cafbe697 100644 --- a/espflash/src/chip/esp32/mod.rs +++ b/espflash/src/chip/esp32/mod.rs @@ -26,14 +26,17 @@ pub struct Esp32Params { } impl Esp32Params { - pub fn default_partition_table(&self) -> PartitionTable { + /// Generates a default partition table. + /// `flash_size` is used to scale app partition when present, otherwise the param defaults are used. + pub fn default_partition_table(&self, flash_size: Option) -> PartitionTable { PartitionTable::basic( self.nvs_addr, self.nvs_size, self.phy_init_data_addr, self.phy_init_data_size, self.app_addr, - self.app_size, + flash_size.map_or(self.app_size, + |size| size - self.app_addr), ) } } diff --git a/espflash/src/flasher.rs b/espflash/src/flasher.rs index b47b5180..56aa7655 100644 --- a/espflash/src/flasher.rs +++ b/espflash/src/flasher.rs @@ -65,6 +65,22 @@ impl FlashSize { _ => Err(Error::UnsupportedFlash(FlashDetectError::from(value))), } } + + /// Returns the flash size in bytes + pub fn size(self) -> u32 { + match self { + FlashSize::Flash256Kb => 0x0040000, + FlashSize::Flash512Kb => 0x0080000, + FlashSize::Flash1Mb => 0x0100000, + FlashSize::Flash2Mb => 0x0200000, + FlashSize::Flash4Mb => 0x0400000, + FlashSize::Flash8Mb => 0x0800000, + FlashSize::Flash16Mb => 0x1000000, + FlashSize::Flash32Mb => 0x2000000, + FlashSize::Flash64Mb => 0x4000000, + FlashSize::Flash128Mb => 0x8000000, + } + } } impl FromStr for FlashSize { diff --git a/espflash/src/image_format/esp32bootloader.rs b/espflash/src/image_format/esp32bootloader.rs index ee386e7f..874eac75 100644 --- a/espflash/src/image_format/esp32bootloader.rs +++ b/espflash/src/image_format/esp32bootloader.rs @@ -32,7 +32,8 @@ impl<'a> Esp32BootloaderFormat<'a> { partition_table: Option, bootloader: Option>, ) -> Result { - let partition_table = partition_table.unwrap_or_else(|| params.default_partition_table()); + let partition_table = partition_table.unwrap_or_else(|| + params.default_partition_table(image.flash_size.map(|v| v.size()))); let mut bootloader = if let Some(bytes) = bootloader { Cow::Owned(bytes) } else { From 54df66c88d3d36fba9bb9455e37786914e0b2029 Mon Sep 17 00:00:00 2001 From: Michael Aspetsberger Date: Mon, 28 Mar 2022 23:36:38 +0200 Subject: [PATCH 2/2] Fix formatting. --- espflash/src/chip/esp32/mod.rs | 3 +-- espflash/src/flasher.rs | 14 +++++++------- espflash/src/image_format/esp32bootloader.rs | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/espflash/src/chip/esp32/mod.rs b/espflash/src/chip/esp32/mod.rs index cafbe697..4aacb704 100644 --- a/espflash/src/chip/esp32/mod.rs +++ b/espflash/src/chip/esp32/mod.rs @@ -35,8 +35,7 @@ impl Esp32Params { self.phy_init_data_addr, self.phy_init_data_size, self.app_addr, - flash_size.map_or(self.app_size, - |size| size - self.app_addr), + flash_size.map_or(self.app_size, |size| size - self.app_addr), ) } } diff --git a/espflash/src/flasher.rs b/espflash/src/flasher.rs index 56aa7655..c8234e44 100644 --- a/espflash/src/flasher.rs +++ b/espflash/src/flasher.rs @@ -71,13 +71,13 @@ impl FlashSize { match self { FlashSize::Flash256Kb => 0x0040000, FlashSize::Flash512Kb => 0x0080000, - FlashSize::Flash1Mb => 0x0100000, - FlashSize::Flash2Mb => 0x0200000, - FlashSize::Flash4Mb => 0x0400000, - FlashSize::Flash8Mb => 0x0800000, - FlashSize::Flash16Mb => 0x1000000, - FlashSize::Flash32Mb => 0x2000000, - FlashSize::Flash64Mb => 0x4000000, + FlashSize::Flash1Mb => 0x0100000, + FlashSize::Flash2Mb => 0x0200000, + FlashSize::Flash4Mb => 0x0400000, + FlashSize::Flash8Mb => 0x0800000, + FlashSize::Flash16Mb => 0x1000000, + FlashSize::Flash32Mb => 0x2000000, + FlashSize::Flash64Mb => 0x4000000, FlashSize::Flash128Mb => 0x8000000, } } diff --git a/espflash/src/image_format/esp32bootloader.rs b/espflash/src/image_format/esp32bootloader.rs index 874eac75..a4583491 100644 --- a/espflash/src/image_format/esp32bootloader.rs +++ b/espflash/src/image_format/esp32bootloader.rs @@ -32,8 +32,8 @@ impl<'a> Esp32BootloaderFormat<'a> { partition_table: Option, bootloader: Option>, ) -> Result { - let partition_table = partition_table.unwrap_or_else(|| - params.default_partition_table(image.flash_size.map(|v| v.size()))); + let partition_table = partition_table + .unwrap_or_else(|| params.default_partition_table(image.flash_size.map(|v| v.size()))); let mut bootloader = if let Some(bytes) = bootloader { Cow::Owned(bytes) } else {