Run elf files

This commit is contained in:
2025-08-27 22:49:44 -04:00
parent 11c17cb246
commit 38b34c9cf9
3 changed files with 26 additions and 0 deletions
+18
View File
@@ -18,6 +18,7 @@ pub struct Cart {
impl Cart {
pub fn load(file_path: &Path, sim_id: SimId) -> Result<Self> {
let rom = fs::read(file_path)?;
let rom = try_parse_elf(&rom).unwrap_or(rom);
let mut sram_file = File::options()
.read(true)
@@ -55,6 +56,23 @@ impl Cart {
}
}
fn try_parse_elf(data: &[u8]) -> Option<Vec<u8>> {
let parsed = elf::ElfBytes::<elf::endian::AnyEndian>::minimal_parse(data).ok()?;
let mut bytes = vec![];
let mut pstart = None;
for phdr in parsed.segments()? {
if phdr.p_filesz == 0 {
continue;
}
let start = pstart.unwrap_or(phdr.p_paddr);
pstart = Some(start);
bytes.resize((phdr.p_paddr - start) as usize, 0);
let data = parsed.segment_data(&phdr).ok()?;
bytes.extend_from_slice(data);
}
Some(bytes)
}
fn sram_path(file_path: &Path, sim_id: SimId) -> PathBuf {
match sim_id {
SimId::Player1 => file_path.with_extension("p1.sram"),