Add the correct header

This commit is contained in:
Gwilym Inzani 2023-04-06 21:54:26 +01:00
parent aa77255720
commit dce7a6726e
3 changed files with 47 additions and 14 deletions

17
agb-gbafix/Cargo.lock generated
View file

@ -6,11 +6,28 @@ version = 3
name = "agb-gbafix"
version = "0.1.0"
dependencies = [
"bytemuck",
"elf",
"gbafix",
]
[[package]]
name = "bytemuck"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17febce684fd15d89027105661fec94afb475cb995fbc59d2865198446ba2eea"
[[package]]
name = "elf"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2b183d6ce6ca4cf30e3db37abf5b52568b5f9015c97d9fbdd7026aa5dcdd758"
[[package]]
name = "gbafix"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7e47af9d5377b8b0def53d9916f6c28521f2e5b97c5fec137797417c26ab4b7"
dependencies = [
"bytemuck",
]

View file

@ -7,3 +7,5 @@ edition = "2021"
[dependencies]
elf = "0.7"
gbafix = "1"
bytemuck = "1"

View file

@ -1,6 +1,6 @@
use std::{
fs,
io::{self, Write},
io::{BufWriter, Write},
path::PathBuf,
};
@ -11,16 +11,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let elf_file = elf::ElfBytes::<elf::endian::AnyEndian>::minimal_parse(file_data)?;
let (section_headers, strtab) = elf_file.section_headers_with_strtab()?;
let section_headers = section_headers.expect("Expected section headers");
let strtab = strtab.expect("Expected string table");
let section_headers = elf_file
.section_headers()
.expect("Expected section headers");
let output = fs::File::create("out.gba")?;
let mut buf_writer = io::BufWriter::new(output);
let mut output = BufWriter::new(fs::File::create("out.gba")?);
let mut header = gbafix::GBAHeader::default();
let mut written_header = false;
for section_header in section_headers.iter() {
let section_name = strtab.get(section_header.sh_name as usize)?;
const SHT_NOBITS: u32 = 8;
const SHT_NULL: u32 = 0;
const SHF_ALLOC: u64 = 2;
@ -31,17 +31,31 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
continue;
}
println!("{section_name}");
let (data, compression) = elf_file.section_data(&section_header)?;
let (mut data, compression) = elf_file.section_data(&section_header)?;
if let Some(compression) = compression {
panic!("Cannot decompress elf content, but got compression header {compression:?}");
}
buf_writer.write_all(data)?;
if !written_header {
assert!(
data.len() > 192,
"first section must be at least as big as the gba header"
);
header.start_code = data[0..4].try_into().unwrap();
header.update_checksum();
let header_bytes = bytemuck::bytes_of(&header);
output.write_all(header_bytes)?;
data = &data[192..];
written_header = true;
}
buf_writer.flush()?;
output.write_all(data)?;
}
output.flush()?;
Ok(())
}