Return an error when the ELF has unexpected contents
@@ -43,6 +43,10 @@ pub enum FileDownloadError {
IO(#[from] std::io::Error),
#[error("Object Error: {0}.")]
Object(&'static str),
+ #[error("{0}")]
+ Elf(#[from] goblin::error::Error),
+ #[error("No loadable ELF sections were found.")]
+ NoLoadableSegments,
}
/// Options for downloading a file onto a target chip.
@@ -171,11 +175,11 @@ fn download_elf<'b, T: Read + Seek>(
file: &'b mut T,
loader: &mut FlashLoader<'_, 'b>,
) -> Result<(), FileDownloadError> {
- file.read_to_end(buffer)?;
-
use goblin::elf::program_header::*;
- if let Ok(binary) = goblin::elf::Elf::parse(&buffer.as_slice()) {
+ file.read_to_end(buffer)?;
+
+ let binary = goblin::elf::Elf::parse(&buffer.as_slice())?;
let mut added_sections = vec![];
for ph in &binary.program_headers {
if ph.p_type == PT_LOAD && ph.p_filesz > 0 {
@@ -185,9 +189,9 @@ fn download_elf<'b, T: Read + Seek>(
ph.p_offset as u32..ph.p_offset as u32 + ph.p_filesz as u32;
for sh in &binary.section_headers {
- if sector.contains_range(
- &(sh.sh_offset as u32..sh.sh_offset as u32 + sh.sh_size as u32),
- ) {
+ if sector
+ .contains_range(&(sh.sh_offset as u32..sh.sh_offset as u32 + sh.sh_size as u32))
+ {
#[cfg(feature = "hexdump")]
for line in hexdump::hexdump_iter(
&buffer[sh.sh_offset as usize..][..sh.sh_size as usize],
@@ -195,11 +199,7 @@ fn download_elf<'b, T: Read + Seek>(
log::trace!("{}", line);
}
- added_sections.push((
- &binary.shdr_strtab[sh.sh_name],
- sh.sh_addr,
- sh.sh_size,
- ));
+ added_sections.push((&binary.shdr_strtab[sh.sh_name], sh.sh_addr, sh.sh_size));
}
}
@@ -211,6 +211,7 @@ fn download_elf<'b, T: Read + Seek>(
}
if added_sections.is_empty() {
log::warn!("No loadable segments were found in the ELF file.");
+ Err(FileDownloadError::NoLoadableSegments)
} else {
log::info!("Found {} loadable sections:", added_sections.len());
for section in added_sections {
@@ -222,7 +223,6 @@ fn download_elf<'b, T: Read + Seek>(
if section.2 == 1 { "" } else { "0" }
);
}
- }
- }
Ok(())
}
+}
