Skip to content

Commit

Permalink
Check (and display) app vs partition size before flashing
Browse files Browse the repository at this point in the history
  • Loading branch information
jendakol committed Sep 12, 2022
1 parent af1202f commit 686a0ae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions espflash/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub enum Error {
help("Either build the binary to be all in ram or remove the `--ram` option to load the image to flash")
)]
ElfNotRamLoadable,
#[error("Supplied elf image is too big and doesn't fit configured app partition")]
ElfTooBig,
#[error("The bootloader returned an error")]
#[diagnostic(transparent)]
RomError(#[from] RomError),
Expand Down
20 changes: 19 additions & 1 deletion espflash/src/image_format/esp32bootloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
error::{Error, FlashDetectError},
flasher::FlashSize,
image_format::{EspCommonHeader, ImageFormat, SegmentHeader, ESP_MAGIC, WP_PIN_DISABLED},
partition_table::{CoreType, Type},
partition_table::{CoreType, Partition, Type},
Chip, PartitionTable,
};

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

Self::check_partition_stats(factory_partition, &data)?;

let flash_segment = RomSegment {
addr: factory_partition.offset(),
data: Cow::Owned(data),
Expand All @@ -173,6 +175,22 @@ impl<'a> Esp32BootloaderFormat<'a> {
flash_segment,
})
}

fn check_partition_stats(part: &Partition, data: &Vec<u8>) -> Result<(), Error> {
let perc = data.len() as f32 / part.size as f32 * 100.0;
println!(
"App/part. size: {}/{} bytes, {:.2}%",
data.len(),
part.size,
perc
);

if perc > 100.0 {
return Err(Error::ElfTooBig);
}

Ok(())
}
}

impl<'a> ImageFormat<'a> for Esp32BootloaderFormat<'a> {
Expand Down
2 changes: 1 addition & 1 deletion espflash/src/partition_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ pub struct Partition {
ty: Type,
sub_type: SubType,
offset: u32,
size: u32,
pub(crate) size: u32,
#[br(count = 16)]
#[br(map = |s: Vec<u8>| String::from_utf8_lossy(&s).trim_matches(char::from(0)).to_string())]
name: String,
Expand Down

0 comments on commit 686a0ae

Please sign in to comment.