Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

extract ISO to filesystem

Layle75488e4f7126
diff --git a/crates/gctool/src/cli.rs b/crates/gctool/src/cli.rs
index dc20db8..d95f728 100644
--- a/crates/gctool/src/cli.rs
+++ b/crates/gctool/src/cli.rs
@@ -52,6 +52,9 @@ pub enum Command {
Iso {
/// Input ISO file
file: String,
+ /// Extract files from the ISO to an "output" directory
+ #[arg(long)]
+ extract: bool,
},
}
diff --git a/crates/gctool/src/iso.rs b/crates/gctool/src/iso.rs
index 27bc15b..144a306 100644
--- a/crates/gctool/src/iso.rs
+++ b/crates/gctool/src/iso.rs
@@ -1,4 +1,4 @@
-use image::dvd::FstNode;
+use image::{Executable, dvd::{DVD_APPLOADER_OFFSET, FstNode}};
use owo_colors::OwoColorize;
use termtree::Tree;
@@ -8,36 +8,41 @@ pub fn info(data: Vec<u8>) {
let game_name = String::from_utf8_lossy(&dvd.header.game_name);
let game_name = game_name.trim_end_matches('\0');
- println!(" {:<16} {}", "Game Name:".bold(), game_name);
+ println!(" {:<24} {}", "Game Name:".bold(), game_name);
println!(
- " {:<16} {}",
+ " {:<24} {}",
"Game Code:".bold(),
String::from_utf8_lossy(&dvd.header.game_code)
);
println!(
- " {:<16} {}",
+ " {:<24} {}",
"Maker Code:".bold(),
String::from_utf8_lossy(&dvd.header.maker_code)
);
println!(
- " {:<16} {} / {}",
+ " {:<24} {} / {}",
"Disk / Version:".bold(),
dvd.header.disk_id,
dvd.header.version
);
- println!(" {:<16} {:08X}", "Magic:".bold(), u32::from_be_bytes(dvd.header.magic));
+ println!(" {:<24} {:08X}", "Magic:".bold(), u32::from_be_bytes(dvd.header.magic));
println!(
- " {:<16} {}",
+ " {:<24} {}",
"Apploader Date:".bold(),
String::from_utf8_lossy(&dvd.apploader.timestamp)
);
println!(
- " {:<16} {:08X}",
+ " {:<24} {:08X}",
+ "Apploader Entrypoint:".bold(),
+ dvd.apploader.entrypoint.get()
+ );
+ println!(
+ " {:<24} {:08X}",
"Main DOL Offset:".bold(),
dvd.header.offset_main_executable.get()
);
println!(
- " {:<16} {:08X} ({} bytes)",
+ " {:<24} {:08X} ({} bytes)",
"FST Offset:".bold(),
dvd.header.offset_filesystem.get(),
dvd.header.filesystem_size.get()
@@ -67,3 +72,42 @@ fn build_tree(node: &FstNode) -> Tree<String> {
}
}
}
+
+pub fn extract(data: Vec<u8>) {
+ let dvd = image::dvd::Dvd::parse(data);
+ let output_path = std::path::Path::new("output");
+ extract_filesystem(&dvd, &dvd.filesystem, output_path);
+ extract_apploader(&dvd, output_path);
+ extract_main_dol(&dvd, output_path);
+ println!("Extracted ISO contents to {}", output_path.display());
+}
+
+fn extract_filesystem(dvd: &image::dvd::Dvd, node: &FstNode, path: &std::path::Path) {
+ match node {
+ FstNode::Directory { name, children } => {
+ let dir_path = path.join(name);
+ std::fs::create_dir_all(&dir_path).unwrap();
+ for child in children {
+ extract_filesystem(dvd, child, &dir_path);
+ }
+ }
+ FstNode::File { name, offset, size } => {
+ let file_path = path.join(name);
+ let data = &dvd.data()[*offset as usize..(*offset + *size) as usize];
+ std::fs::write(file_path, data).unwrap();
+ println!("Extracted file: {}", name);
+ }
+ }
+}
+
+fn extract_apploader(dvd: &image::dvd::Dvd, path: &std::path::Path) {
+ let apploader = &dvd.apploader;
+ let data = &dvd.data()[DVD_APPLOADER_OFFSET..(DVD_APPLOADER_OFFSET + apploader.size.get() as usize)];
+ std::fs::write(path.join("apploader.bin"), data).unwrap();
+}
+
+fn extract_main_dol(dvd: &image::dvd::Dvd, path: &std::path::Path) {
+ let dol_offset = dvd.header.offset_main_executable.get() as usize;
+ let main_dol = image::Dol::parse(dvd.data()[dol_offset..].to_vec());
+ std::fs::write(path.join("main.dol"), &main_dol.data()[..main_dol.size()]).unwrap();
+}
\ No newline at end of file
diff --git a/crates/gctool/src/main.rs b/crates/gctool/src/main.rs
index af44e7c..66bc9e8 100644
--- a/crates/gctool/src/main.rs
+++ b/crates/gctool/src/main.rs
@@ -46,8 +46,12 @@ fn main() {
Command::Ipl { action, file, output } => {
ipl::process(file, output.as_deref(), *action);
}
- Command::Iso { file } => {
- iso::info(read_file_or_exit(file));
+ Command::Iso { file, extract } => {
+ if *extract {
+ iso::extract(read_file_or_exit(file));
+ } else {
+ iso::info(read_file_or_exit(file));
+ }
}
}
}
diff --git a/crates/image/src/dvd.rs b/crates/image/src/dvd.rs
index a9345d3..04558f6 100644
--- a/crates/image/src/dvd.rs
+++ b/crates/image/src/dvd.rs
@@ -1,6 +1,8 @@
use zerocopy::byteorder::big_endian::U32;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
+use crate::{Dol, Executable, Section};
+
pub const DVD_HEADER_SIZE: usize = 0x440;
pub const DVD_HEADER_INFO_SIZE: usize = 0x2000;
pub const DVD_APPLOADER_SIZE: usize = 0x2000;
@@ -55,11 +57,11 @@ pub struct Apploader {
pub size: U32,
pub trailer_size: U32,
_unused1: [u8; 4],
- pub apploader_code: [u8; DVD_APPLOADER_SIZE - 0x20],
+ pub code: [u8; DVD_APPLOADER_SIZE - 0x20],
}
pub enum FstNode {
- File { name: String, dvd_offset: u32, size: u32 },
+ File { name: String, offset: u32, size: u32 },
Directory { name: String, children: Vec<FstNode> },
}
@@ -91,7 +93,7 @@ impl FstNode {
if flags == 0 {
entries.push(Self::File {
name,
- dvd_offset: offset,
+ offset,
size: length,
});
} else {
@@ -126,6 +128,8 @@ pub struct Dvd {
pub header_info: HeaderInfo,
pub apploader: Apploader,
pub filesystem: FstNode,
+ data: Vec<u8>,
+ sections: Vec<Section>,
}
impl Dvd {
@@ -141,11 +145,48 @@ impl Dvd {
let fst_end = fst_start + header.filesystem_size.get() as usize;
let filesystem = FstNode::parse(&data[fst_start..fst_end]);
+ let apploader_section = Section {
+ offset: DVD_APPLOADER_OFFSET as u32,
+ vaddr: 0x81200000,
+ size: apploader.size.get(),
+ };
+
+ let main_dol = Dol::parse(data[header.offset_main_executable.get() as usize..].to_vec());
+ let main_section = Section {
+ offset: header.offset_main_executable.get(),
+ vaddr: main_dol.entry_point(),
+ size: main_dol.size() as u32,
+ };
+
Dvd {
header,
header_info,
apploader,
filesystem,
+ data,
+ sections: vec![apploader_section, main_section],
}
}
}
+
+impl Executable for Dvd {
+ fn entry_point(&self) -> u32 {
+ self.apploader.entrypoint.get()
+ }
+
+ fn data(&self) -> &[u8] {
+ &self.data
+ }
+
+ fn text_sections(&self) -> &[crate::Section] {
+ &self.sections
+ }
+
+ fn data_sections(&self) -> &[crate::Section] {
+ &[]
+ }
+
+ fn bss(&self) -> (u32, u32) {
+ (0, 0)
+ }
+}
diff --git a/crates/image/src/lib.rs b/crates/image/src/lib.rs
index cf08188..6d42b38 100644
--- a/crates/image/src/lib.rs
+++ b/crates/image/src/lib.rs
@@ -65,6 +65,17 @@ impl Dol {
}
}
+impl Dol {
+ pub fn size(&self) -> usize {
+ self.text_sections
+ .iter()
+ .chain(self.data_sections.iter())
+ .map(|s| (s.offset + s.size) as usize)
+ .max()
+ .unwrap_or(0)
+ }
+}
+
impl Executable for Dol {
fn text_sections(&self) -> &[Section] {
&self.text_sections