Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check (and display) app vs partition size before flashing #232

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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