use crate::error::{Error, Result};
const EM_RISCV: u16 = 243;
const PT_LOAD: u32 = 1;
const PHENT_SIZE: usize = 56;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LoadSegment {
pub file_offset: u64,
pub vaddr: u64,
pub file_size: u64,
pub mem_size: u64,
}
#[derive(Clone, Debug)]
pub struct KernelImage {
pub entry: u64,
pub segments: Vec<LoadSegment>,
}
pub fn parse(image: &[u8]) -> Result<KernelImage> {
if image.len() < 64 {
return Err(Error::Elf("image shorter than an ELF64 header".into()));
}
if &image[0..4] != b"\x7fELF" {
return Err(Error::Elf("bad ELF magic".into()));
}
if image[4] != 2 {
return Err(Error::Elf("not an ELF64 image".into()));
}
if image[5] != 1 {
return Err(Error::Elf("not a little-endian image".into()));
}
let machine = u16::from_le_bytes([image[18], image[19]]);
if machine != EM_RISCV {
return Err(Error::Elf(format!(
"unexpected machine {machine} (expected RISC-V {EM_RISCV})"
)));
}
let entry = rd_u64(image, 24)?;
let phoff = rd_u64(image, 32)? as usize;
let phentsize = rd_u16(image, 54)? as usize;
let phnum = rd_u16(image, 56)? as usize;
if phentsize != PHENT_SIZE {
return Err(Error::Elf(format!(
"unexpected program-header entry size {phentsize}"
)));
}
let mut segments = Vec::new();
for i in 0..phnum {
let base = phoff + i * PHENT_SIZE;
if base + PHENT_SIZE > image.len() {
return Err(Error::Elf("program header table out of bounds".into()));
}
let p_type = rd_u32(image, base)?; if p_type != PT_LOAD {
continue;
}
let file_offset = rd_u64(image, base + 8)?; let vaddr = rd_u64(image, base + 16)?; let file_size = rd_u64(image, base + 32)?; let mem_size = rd_u64(image, base + 40)?;
let end = file_offset
.checked_add(file_size)
.ok_or_else(|| Error::Elf("segment file range overflows".into()))?;
if end > image.len() as u64 {
return Err(Error::Elf("segment file range out of bounds".into()));
}
if mem_size < file_size {
return Err(Error::Elf("segment mem_size smaller than file_size".into()));
}
segments.push(LoadSegment {
file_offset,
vaddr,
file_size,
mem_size,
});
}
Ok(KernelImage { entry, segments })
}
fn rd_u16(b: &[u8], off: usize) -> Result<u16> {
b.get(off..off + 2)
.map(|s| u16::from_le_bytes(s.try_into().unwrap()))
.ok_or_else(|| Error::Elf("truncated ELF field".into()))
}
fn rd_u32(b: &[u8], off: usize) -> Result<u32> {
b.get(off..off + 4)
.map(|s| u32::from_le_bytes(s.try_into().unwrap()))
.ok_or_else(|| Error::Elf("truncated ELF field".into()))
}
fn rd_u64(b: &[u8], off: usize) -> Result<u64> {
b.get(off..off + 8)
.map(|s| u64::from_le_bytes(s.try_into().unwrap()))
.ok_or_else(|| Error::Elf("truncated ELF field".into()))
}