Skip to content

Commit 4440a51

Browse files
committed
better elf errors
1 parent 4a8b6ed commit 4440a51

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

espflash/src/error.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ pub enum Error {
1818
code(espflash::invalid_elf),
1919
help("Try running `cargo clean` and rebuilding the image")
2020
)]
21-
InvalidElf,
22-
#[error("Supplied elf image can not be ran from ram")]
21+
InvalidElf(#[from] ElfError),
22+
#[error("Supplied elf image can not be ran from ram as it includes segments mapped to rom addresses")]
2323
#[diagnostic(
2424
code(espflash::not_ram_loadable),
2525
help("Either build the binary to be all in ram or remove the `--ram` option to load the image to flash")
@@ -153,6 +153,7 @@ impl From<binread::Error> for Error {
153153
#[derive(Copy, Clone, Debug, Error, Diagnostic)]
154154
#[allow(dead_code)]
155155
#[repr(u8)]
156+
#[non_exhaustive]
156157
pub enum RomError {
157158
#[error("Invalid message received")]
158159
#[diagnostic(code(espflash::rom::invalid_message))]
@@ -274,3 +275,13 @@ impl PartitionTableError {
274275
fn pos_to_offset(pos: Position) -> SourceOffset {
275276
(pos.byte() as usize + 1).into()
276277
}
278+
279+
#[derive(Debug, Error)]
280+
#[error("{0}")]
281+
pub struct ElfError(&'static str);
282+
283+
impl From<&'static str> for ElfError {
284+
fn from(err: &'static str) -> Self {
285+
ElfError(err)
286+
}
287+
}

espflash/src/flasher.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use strum_macros::Display;
55
use std::thread::sleep;
66

77
use crate::elf::RomSegment;
8-
use crate::error::{ConnectionError, ResultExt};
8+
use crate::error::{ConnectionError, ElfError, ResultExt};
99
use crate::{
1010
chip::Chip, connection::Connection, elf::FirmwareImage, encoder::SlipEncoder, error::RomError,
1111
Error, PartitionTable,
@@ -469,7 +469,7 @@ impl Flasher {
469469
///
470470
/// Note that this will not touch the flash on the device
471471
pub fn load_elf_to_ram(&mut self, elf_data: &[u8]) -> Result<(), Error> {
472-
let image = FirmwareImage::from_data(elf_data).map_err(|_| Error::InvalidElf)?;
472+
let image = FirmwareImage::from_data(elf_data).map_err(ElfError::from)?;
473473

474474
let mut target = self.chip.ram_target();
475475
target.begin(&mut self.connection, &image).flashing()?;
@@ -500,7 +500,7 @@ impl Flasher {
500500
bootloader: Option<Vec<u8>>,
501501
partition_table: Option<PartitionTable>,
502502
) -> Result<(), Error> {
503-
let mut image = FirmwareImage::from_data(elf_data).map_err(|_| Error::InvalidElf)?;
503+
let mut image = FirmwareImage::from_data(elf_data).map_err(ElfError::from)?;
504504
image.flash_size = self.flash_size();
505505

506506
let mut target = self.chip.flash_target(self.spi_params);

0 commit comments

Comments
 (0)