diff --git a/agb-gbafix/Cargo.lock b/agb-gbafix/Cargo.lock index 4677f650..e23beada 100644 --- a/agb-gbafix/Cargo.lock +++ b/agb-gbafix/Cargo.lock @@ -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", +] diff --git a/agb-gbafix/Cargo.toml b/agb-gbafix/Cargo.toml index ee86b776..de91941c 100644 --- a/agb-gbafix/Cargo.toml +++ b/agb-gbafix/Cargo.toml @@ -6,4 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -elf = "0.7" \ No newline at end of file +elf = "0.7" +gbafix = "1" +bytemuck = "1" \ No newline at end of file diff --git a/agb-gbafix/src/main.rs b/agb-gbafix/src/main.rs index 1f07d457..039c1896 100644 --- a/agb-gbafix/src/main.rs +++ b/agb-gbafix/src/main.rs @@ -1,6 +1,6 @@ use std::{ fs, - io::{self, Write}, + io::{BufWriter, Write}, path::PathBuf, }; @@ -11,16 +11,16 @@ fn main() -> Result<(), Box> { let elf_file = elf::ElfBytes::::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> { continue; } - println!("{section_name}"); - - let (data, compression) = elf_file.section_data(§ion_header)?; + let (mut data, compression) = elf_file.section_data(§ion_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; + } + + output.write_all(data)?; } - buf_writer.flush()?; + output.flush()?; Ok(()) }