Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

generate NAND FS

Laylebe8d18148677
diff --git a/crates/gecko/src/hollywood/ipc/fs.rs b/crates/gecko/src/hollywood/ipc/fs.rs
index c119097..b764873 100644
--- a/crates/gecko/src/hollywood/ipc/fs.rs
+++ b/crates/gecko/src/hollywood/ipc/fs.rs
@@ -1,4 +1,5 @@
pub mod host;
+pub mod nand;
use crate::hollywood::ipc::{DeviceContext, IosDevice};
use std::path::{Path, PathBuf};
diff --git a/crates/gecko/src/hollywood/ipc/fs/nand.rs b/crates/gecko/src/hollywood/ipc/fs/nand.rs
new file mode 100644
index 0000000..4ce7ede
--- /dev/null
+++ b/crates/gecko/src/hollywood/ipc/fs/nand.rs
@@ -0,0 +1,156 @@
+use std::path::Path;
+
+const BASE_DIRS: [&str; 9] = [
+ "sys", "ticket", "title", "shared1", "shared2", "tmp", "import", "meta", "wfs",
+];
+
+const SETTING_TXT_PATH: &str = "title/00000001/00000002/data/setting.txt";
+const SETTING_SEED: u32 = 0x73B5_DBFA;
+const SERIAL_NUMBER: &str = "123456789";
+
+pub fn ensure_skeleton(root: &Path) {
+ for dir in BASE_DIRS {
+ let path = root.join(dir);
+ if let Err(err) = std::fs::create_dir_all(&path) {
+ tracing::warn!(path = %path.display(), %err, "NAND: create dir failed");
+ }
+ }
+}
+
+pub fn ensure_setting_txt(root: &Path, game_code: [u8; 4]) {
+ let path = root.join(SETTING_TXT_PATH);
+ if path.exists() {
+ return;
+ }
+
+ self::write_new(&path, &RegionSetting::from_game_code(game_code).encode());
+}
+
+pub fn ensure_title_dirs(root: &Path, title_id: u64) {
+ let title_dir = root.join(format!("title/{:08x}/{:08x}", (title_id >> 32) as u32, title_id as u32));
+
+ for sub in ["content", "data"] {
+ let path = title_dir.join(sub);
+ if let Err(err) = std::fs::create_dir_all(&path) {
+ tracing::warn!(path = %path.display(), %err, "NAND: create title dir failed");
+ }
+ }
+}
+
+pub fn write_new(path: &Path, data: &[u8]) {
+ if let Some(parent) = path.parent() {
+ if let Err(err) = std::fs::create_dir_all(parent) {
+ tracing::warn!(path = %parent.display(), %err, "NAND: create parent failed");
+ return;
+ }
+ }
+
+ match std::fs::write(path, data) {
+ Ok(()) => tracing::info!(path = %path.display(), "NAND: generated default"),
+ Err(err) => tracing::warn!(path = %path.display(), %err, "NAND: write failed"),
+ }
+}
+
+struct RegionSetting {
+ area: &'static str,
+ video: &'static str,
+ code: &'static str,
+ game: &'static str,
+}
+
+impl RegionSetting {
+ fn from_game_code(game_code: [u8; 4]) -> Self {
+ match game_code[3] {
+ b'J' => Self {
+ area: "JPN",
+ video: "NTSC",
+ code: "JP",
+ game: "LJH",
+ },
+ b'P' | b'D' | b'F' | b'S' | b'I' | b'H' | b'U' | b'X' | b'Y' | b'Z' => Self {
+ area: "EUR",
+ video: "PAL",
+ code: "EU",
+ game: "LEH",
+ },
+ b'K' | b'Q' | b'T' => Self {
+ area: "KOR",
+ video: "NTSC",
+ code: "KR",
+ game: "LKH",
+ },
+ _ => Self {
+ area: "USA",
+ video: "NTSC",
+ code: "US",
+ game: "LU",
+ },
+ }
+ }
+
+ fn encode(&self) -> Vec<u8> {
+ let model = format!("RVL-001({})", self.area);
+ let mut writer = SettingWriter::new();
+ writer.add("AREA", self.area);
+ writer.add("MODEL", &model);
+ writer.add("DVD", "0");
+ writer.add("MPCH", "0x7FFE");
+ writer.add("CODE", self.code);
+ writer.add("SERNO", SERIAL_NUMBER);
+ writer.add("VIDEO", self.video);
+ writer.add("GAME", self.game);
+ writer.finish()
+ }
+}
+
+struct SettingWriter {
+ buffer: [u8; 0x100],
+ position: usize,
+ key: u32,
+}
+
+impl SettingWriter {
+ fn new() -> Self {
+ Self {
+ buffer: [0u8; 0x100],
+ position: 0,
+ key: SETTING_SEED,
+ }
+ }
+
+ fn add(&mut self, key: &str, value: &str) {
+ self.write_line(&format!("{key}={value}\r\n"));
+ }
+
+ fn write_line(&mut self, line: &str) {
+ loop {
+ let old_position = self.position;
+ let old_key = self.key;
+ for b in line.bytes() {
+ self.write_byte(b);
+ }
+
+ if !self.buffer[old_position..self.position].contains(&0) {
+ return;
+ }
+
+ self.position = old_position;
+ self.key = old_key;
+ self.write_byte(b'\n');
+ }
+ }
+
+ fn write_byte(&mut self, b: u8) {
+ if self.position >= self.buffer.len() {
+ return;
+ }
+
+ self.buffer[self.position] = b ^ (self.key as u8);
+ self.position += 1;
+ self.key = self.key.rotate_left(1);
+ }
+
+ fn finish(self) -> Vec<u8> {
+ self.buffer.to_vec()
+ }
+}
diff --git a/crates/gecko/src/hollywood/ipc/sysconf.rs b/crates/gecko/src/hollywood/ipc/sysconf.rs
index 0a3776f..bccc456 100644
--- a/crates/gecko/src/hollywood/ipc/sysconf.rs
+++ b/crates/gecko/src/hollywood/ipc/sysconf.rs
@@ -1,4 +1,6 @@
+use crate::hollywood::ipc::fs::nand;
use crate::hollywood::ipc::{IPC_EINVAL, IosDevice};
+use std::path::Path;
const SYSCONF_SIZE: usize = 0x4000;
const FOOTER_OFFSET: usize = 0x3FFC;
@@ -25,11 +27,15 @@ pub struct SysConf {
}
impl SysConf {
- pub fn new() -> Self {
- Self {
- blob: self::build_blob(),
- pos: 0,
+ pub fn new(host_fs_root: &Path) -> Self {
+ let blob = self::build_blob();
+
+ let path = host_fs_root.join("shared2/sys/SYSCONF");
+ if !path.exists() {
+ nand::write_new(&path, &blob);
}
+
+ Self { blob, pos: 0 }
}
}
diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rs
index 9de667e..57ac509 100644
--- a/crates/gecko/src/starlet.rs
+++ b/crates/gecko/src/starlet.rs
@@ -152,6 +152,13 @@ fn default_host_fs_root() -> PathBuf {
if let Some(custom) = std::env::var_os("GECKO_FS_ROOT") {
return PathBuf::from(custom);
}
+
+ if let Ok(exe) = std::env::current_exe() {
+ if let Some(dir) = exe.parent() {
+ return dir.join("fs");
+ }
+ }
+
PathBuf::from("fs")
}
@@ -159,14 +166,15 @@ impl System<{ crate::WII }> {
pub fn initialize_starlet_devices(&mut self) {
use crate::hollywood::ipc::{self, stm};
+ let fs_root = self.starlet.host_fs_root.clone();
+ ipc::fs::nand::ensure_skeleton(&fs_root);
+
self.starlet.register("/dev/stm/immediate", Box::new(stm::Immediate));
self.starlet.register("/dev/stm/eventhook", Box::new(stm::EventHook));
- self.starlet.register(
- "/dev/fs",
- Box::new(ipc::fs::FileSystem::new(self.starlet.host_fs_root.clone())),
- );
self.starlet
- .register("/shared2/sys/SYSCONF", Box::new(ipc::sysconf::SysConf::new()));
+ .register("/dev/fs", Box::new(ipc::fs::FileSystem::new(fs_root.clone())));
+ self.starlet
+ .register("/shared2/sys/SYSCONF", Box::new(ipc::sysconf::SysConf::new(&fs_root)));
self.starlet
.register("/dev/di", Box::new(ipc::di::DiskInterface::new()));
self.starlet.register("/dev/es", Box::new(ipc::es::ETicketServices));
diff --git a/crates/gecko/src/wii.rs b/crates/gecko/src/wii.rs
index 9974df3..9ebb9cc 100644
--- a/crates/gecko/src/wii.rs
+++ b/crates/gecko/src/wii.rs
@@ -255,6 +255,13 @@ impl Wii {
let video_mode: u32 = if header.is_ntsc() { 0 } else { 1 };
emu.mmio.phys_write_u32(0x0000_00CC, video_mode);
+ // Generate the System Menu setting.txt for this disc's region if the
+ // host fs root doesn't already carry one.
+ crate::hollywood::ipc::fs::nand::ensure_setting_txt(&emu.starlet.host_fs_root, header.game_code);
+
+ // Create the booting title's content/data dirs so saves can be written.
+ crate::hollywood::ipc::fs::nand::ensure_title_dirs(&emu.starlet.host_fs_root, dvd.tmd_title_id());
+
// Game ID dup.
emu.mmio.phys_write_u32(0x0000_3180, game_id);