Skip to content

Commit 1b23940

Browse files
jendakoljessebraham
authored andcommitted
Check (and display) app vs partition size before flashing
1 parent b8a9854 commit 1b23940

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

espflash/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ pub enum Error {
3838
help("Either build the binary to be all in ram or remove the `--ram` option to load the image to flash")
3939
)]
4040
ElfNotRamLoadable,
41+
#[error("Supplied elf image is too big and doesn't fit configured app partition")]
42+
ElfTooBig,
4143
#[error("The bootloader returned an error")]
4244
#[diagnostic(transparent)]
4345
RomError(#[from] RomError),

espflash/src/image_format/esp32bootloader.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
error::{Error, FlashDetectError},
1414
flasher::FlashSize,
1515
image_format::{EspCommonHeader, ImageFormat, SegmentHeader, ESP_MAGIC, WP_PIN_DISABLED},
16-
partition_table::{CoreType, Type},
16+
partition_table::{CoreType, Partition, Type},
1717
Chip, PartitionTable,
1818
};
1919

@@ -161,6 +161,8 @@ impl<'a> Esp32BootloaderFormat<'a> {
161161
.or_else(|| partition_table.find_by_type(Type::CoreType(CoreType::App)))
162162
.unwrap();
163163

164+
Self::check_partition_stats(factory_partition, &data)?;
165+
164166
let flash_segment = RomSegment {
165167
addr: factory_partition.offset(),
166168
data: Cow::Owned(data),
@@ -173,6 +175,22 @@ impl<'a> Esp32BootloaderFormat<'a> {
173175
flash_segment,
174176
})
175177
}
178+
179+
fn check_partition_stats(part: &Partition, data: &Vec<u8>) -> Result<(), Error> {
180+
let perc = data.len() as f32 / part.size as f32 * 100.0;
181+
println!(
182+
"App/part. size: {}/{} bytes, {:.2}%",
183+
data.len(),
184+
part.size,
185+
perc
186+
);
187+
188+
if perc > 100.0 {
189+
return Err(Error::ElfTooBig);
190+
}
191+
192+
Ok(())
193+
}
176194
}
177195

178196
impl<'a> ImageFormat<'a> for Esp32BootloaderFormat<'a> {

espflash/src/partition_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ pub struct Partition {
572572
ty: Type,
573573
sub_type: SubType,
574574
offset: u32,
575-
size: u32,
575+
pub(crate) size: u32,
576576
#[br(count = 16)]
577577
#[br(map = |s: Vec<u8>| String::from_utf8_lossy(&s).trim_matches(char::from(0)).to_string())]
578578
name: String,

0 commit comments

Comments
 (0)