A GameCube/Wii emulator and debugger written in Rust.
set attr ioctl + wc24 offline stub (fixes mario kart wii on fresh nands)
Layleeb585bd9a2e3
diff --git a/crates/gecko/src/hollywood/ipc.rs b/crates/gecko/src/hollywood/ipc.rsindex a84f441..07c5257 100644--- a/crates/gecko/src/hollywood/ipc.rs+++ b/crates/gecko/src/hollywood/ipc.rs@@ -1,6 +1,7 @@pub mod di;pub mod es;pub mod fs;+pub mod net;pub mod sdio;pub mod stm;pub mod sysconf;diff --git a/crates/gecko/src/hollywood/ipc/fs.rs b/crates/gecko/src/hollywood/ipc/fs.rsindex 884f42d..609a8cd 100644--- a/crates/gecko/src/hollywood/ipc/fs.rs+++ b/crates/gecko/src/hollywood/ipc/fs.rs@@ -7,6 +7,7 @@ use std::path::{Path, PathBuf};pub const IOCTL_GET_STATS: u32 = 0x2;pub const IOCTL_CREATE_DIR: u32 = 0x3;pub const IOCTL_READ_DIR: u32 = 0x4;+pub const IOCTL_SET_ATTR: u32 = 0x5;pub const IOCTL_GET_ATTR: u32 = 0x6;pub const IOCTL_DELETE: u32 = 0x7;pub const IOCTL_RENAME: u32 = 0x8;@@ -61,6 +62,7 @@ impl IosDevice for FileSystem {match cmd {IOCTL_GET_STATS => self.get_stats(ctx, out_ptr, out_len),IOCTL_CREATE_DIR => self.create_dir(ctx, in_ptr, in_len),+ IOCTL_SET_ATTR => self.set_attr(ctx, in_ptr, in_len),IOCTL_GET_ATTR => self.get_attr(ctx, in_ptr, in_len, out_ptr, out_len),IOCTL_DELETE => self.delete(ctx, in_ptr, in_len),IOCTL_RENAME => self.rename(ctx, in_ptr, in_len),@@ -96,6 +98,35 @@ impl IosDevice for FileSystem {}impl FileSystem {+ fn set_attr(&self, ctx: &mut DeviceContext<'_>, in_ptr: u32, in_len: u32) -> i32 {+ if (in_len as usize) < FS_CREATE_INPUT_LEN {+ return FS_EINVAL;+ }++ let Some(path) = self::read_guest_path(ctx, in_ptr + FS_ATTR_PATH_OFFSET, FS_MAX_PATH) else {+ return FS_EINVAL;+ };++ for offset in [+ FS_ATTR_OWNER_PERM_OFFSET,+ FS_ATTR_GROUP_PERM_OFFSET,+ FS_ATTR_OTHER_PERM_OFFSET,+ ] {+ if ctx.mmio.phys_read_u8(in_ptr + offset) > 3 {+ return FS_EINVAL;+ }+ }++ let host_path = host::nand_to_host(&self.host_root, &path);+ if !host_path.exists() {+ return FS_ENOENT;+ }++ tracing::debug!(%path, "FS_SetAttr");++ 0+ }+fn read_dir(&self, ctx: &mut DeviceContext<'_>, in_count: u32, io_count: u32, vec_ptr: u32) -> i32 {let with_names = match (in_count, io_count) {(1, 1) => false,diff --git a/crates/gecko/src/hollywood/ipc/fs/nand.rs b/crates/gecko/src/hollywood/ipc/fs/nand.rsindex c50debf..a5ef3b3 100644--- a/crates/gecko/src/hollywood/ipc/fs/nand.rs+++ b/crates/gecko/src/hollywood/ipc/fs/nand.rs@@ -8,6 +8,9 @@ const SETTING_TXT_PATH: &str = "title/00000001/00000002/data/setting.txt";const SETTING_SEED: u32 = 0x73B5_DBFA;const SERIAL_NUMBER: &str = "696969420";+pub const WC24_CONFIG_PATH: &str = "shared2/wc24/nwc24msg.cfg";+const WC24_DIR: &str = "shared2/wc24";+pub fn ensure_skeleton(root: &Path) {for dir in BASE_DIRS {let path = root.join(dir);@@ -66,6 +69,138 @@ pub fn ensure_title_dirs(root: &Path, title_id: u64) {}}+// https://github.com/kiwi515/ogws/tree/master/include/revolution/NWC24/internal+pub fn ensure_wc24_files(root: &Path) {+ let cfg = root.join(WC24_CONFIG_PATH);+ let cbk = cfg.with_extension("cbk");++ if !cfg.exists() || !cbk.exists() {+ let msg = std::fs::read(&cfg)+ .or_else(|_| std::fs::read(&cbk))+ .unwrap_or_else(|_| self::wc24_msg_cfg());+ for path in [&cfg, &cbk] {+ if !path.exists() {+ self::write_new(path, &msg);+ }+ }+ }++ let dir = root.join(WC24_DIR);+ let files: [(&str, fn() -> Vec<u8>); 7] = [+ ("nwc24fl.bin", || {+ // NWC24iFLHeader: magic "WcFl", version, capacity+ self::be_words(0x8060, &[0x5763466C, 2, 100])+ }),+ ("nwc24fls.bin", || {+ // NWC24iSecretFLHeader: magic "WcFs", version, undocumented +0x08 default+ self::be_words(0x3200, &[0x57634673, 2, 0x150])+ }),+ ("nwc24dl.bin", self::wc24_dl),+ ("mbox/wc24send.ctl", || self::wc24_ctl(0x4000, 127, 0x200000)),+ ("mbox/wc24recv.ctl", || self::wc24_ctl(0x8000, 255, 0x700000)),+ ("mbox/wc24send.mbx", || self::wc24_mbx(0x200000)),+ ("mbox/wc24recv.mbx", || self::wc24_mbx(0x700000)),+ ];++ for (name, build) in files {+ let path = dir.join(name);+ if !path.exists() {+ self::write_new(&path, &build());+ }+ }+}++fn put_u32(data: &mut [u8], offset: usize, value: u32) {+ data[offset..offset + 4].copy_from_slice(&value.to_be_bytes());+}++fn be_words(size: usize, words: &[u32]) -> Vec<u8> {+ let mut data = vec![0; size];+ for (i, &word) in words.iter().enumerate() {+ self::put_u32(&mut data, i * 4, word);+ }+ data+}++fn wc24_msg_cfg() -> Vec<u8> {+ // https://github.com/kiwi515/ogws/blob/master/include/revolution/NWC24/internal/NWC24iConfig.h+ // https://wiibrew.org/wiki//shared2/wc24/nwc24msg.cfg+ let header = [+ 0x57634366, // +0x00: "WcCf" magic+ 8, // +0x04: format version+ 0, // +0x08: dummy user ID, high word+ 0x69696, // +0x0C: dummy user ID, low word+ 1, // +0x10: ID creation counter+ 1, // +0x14: creation stage+ ];+ let mut data = self::be_words(0x400, &header); // 1 KiB config, including checksum+ // https://github.com/kiwi515/ogws/blob/master/src/revolution/NWC24/NWC24Config.c+ let checksum = header.iter().fold(0u32, |sum, &v| sum.wrapping_add(v));+ self::put_u32(&mut data, 0x3FC, checksum);+ data+}++fn wc24_dl() -> Vec<u8> {+ // https://github.com/kiwi515/ogws/blob/master/include/revolution/NWC24/internal/NWC24iDownload.h+ // https://wiibrew.org/wiki//shared2/wc24/nwc24dl.bin+ let header = [+ 0x5763446C, // +0x00: "WcDl" magic+ 1, // +0x04: format version+ 0, // +0x08: unknown/reserved+ 0, // +0x0C: unknown/reserved+ 0x00200008, // +0x10: u16 max subtasks (32), u16 private tasks (8)+ 120 << 16, // +0x14: u16 task capacity (120), then two zero bytes+ ];+ let mut data = self::be_words(0xF800, &header);+ for i in 0..120u32 {+ self::put_u32(&mut data, 0x800 + i as usize * 0x200, (i << 16) | 0xFF00);+ }+ data+}++fn wc24_ctl(size: usize, max_entries: u32, mbx_size: u32) -> Vec<u8> {+ // https://github.com/kiwi515/ogws/blob/master/include/revolution/NWC24/internal/NWC24iMBoxCtrl.h+ let mut data = self::be_words(+ size,+ &[+ 0x57635466, // +0x00: "WcTf" magic+ 4, // +0x04: format version+ 0, // +0x08: current message count+ max_entries, // +0x0C: message capacity+ 0, // +0x10: total stored message bytes+ size as u32, // +0x14: control file size in bytes+ 1, // +0x18: next message ID+ 128, // +0x1C: first free entry, immediately after the 0x80-byte header+ 0, // +0x20: oldest message ID (none)+ mbx_size, // +0x24: initial mailbox free-space budget+ ],+ );+ // Thx Dolphin+ data[0x58..0x7F].fill(b'0');+ for offset in (0x80..size - 0x80).step_by(0x80) {+ self::put_u32(&mut data, offset + 12, (offset + 0x80) as u32);+ }+ data+}++fn wc24_mbx(size: usize) -> Vec<u8> {+ // https://wiibrew.org/wiki/VFF+ let mut data = self::be_words(+ size,+ &[+ 0x56464620, // +0x00: "VFF " magic+ 0xFEFF0100, // +0x04: ??+ size as u32, // +0x08: total volume size in bytes+ 0x00200000, // +0x0C: u16 header size (32 bytes), then zero padding+ ],+ );+ let fat_size = (size / 512 * 2 + 511) & !511;+ for offset in [32, 32 + fat_size] {+ self::put_u32(&mut data, offset, 0xF0FFFFFF);+ }+ data+}+pub fn write_new(path: &Path, data: &[u8]) {if let Some(parent) = path.parent() {if let Err(err) = std::fs::create_dir_all(parent) {diff --git a/crates/gecko/src/hollywood/ipc/net.rs b/crates/gecko/src/hollywood/ipc/net.rsnew file mode 100644index 0000000..87032b3--- /dev/null+++ b/crates/gecko/src/hollywood/ipc/net.rs@@ -0,0 +1,60 @@+use super::fs::nand;+use super::{DeviceContext, IPC_EINVAL, IosDevice};+use std::path::{Path, PathBuf};++const IOCTL_SUSPEND_SCHEDULER: u32 = 0x01;+const IOCTL_TRY_SUSPEND_SCHEDULER: u32 = 0x02;+const IOCTL_RESUME_SCHEDULER: u32 = 0x03;+const IOCTL_REQUEST_GENERATED_USER_ID: u32 = 0x0F;+const IOCTL_SET_RTC_COUNTER: u32 = 0x17;++const WC24_ID_REGISTERED: i32 = -36;+const WC24_ID_GENERATED: i32 = -35;++pub struct Wc24 {+ config: PathBuf,+}++impl Wc24 {+ pub fn new(root: &Path) -> Self {+ Self {+ config: root.join(nand::WC24_CONFIG_PATH),+ }+ }+}++impl IosDevice for Wc24 {+ fn ioctl(&mut self, ctx: &mut DeviceContext<'_>, cmd: u32, _: u32, _: u32, out: u32, len: u32) -> i32 {+ if out == 0 || len < 32 {+ return IPC_EINVAL;+ }++ match cmd {+ IOCTL_SUSPEND_SCHEDULER | IOCTL_TRY_SUSPEND_SCHEDULER | IOCTL_RESUME_SCHEDULER | IOCTL_SET_RTC_COUNTER => {+ ctx.mmio.phys_slice_mut(out, 32).fill(0);+ }+ IOCTL_REQUEST_GENERATED_USER_ID => {+ let Ok(config) = std::fs::read(&self.config) else {+ return IPC_EINVAL;+ };+ if config.len() < 24 {+ return IPC_EINVAL;+ }++ let status = if config[20..24] == 2u32.to_be_bytes() {+ WC24_ID_REGISTERED+ } else {+ WC24_ID_GENERATED+ };++ ctx.mmio.phys_slice_mut(out, 32).fill(0);+ ctx.mmio.phys_write_u32(out, status as u32);+ ctx.mmio.phys_slice_mut(out + 4, 8).copy_from_slice(&config[8..16]);+ ctx.mmio.phys_slice_mut(out + 12, 4).copy_from_slice(&config[20..24]);+ }+ _ => return IPC_EINVAL,+ }++ 0+ }+}diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rsindex 5427402..8aa896e 100644--- a/crates/gecko/src/starlet.rs+++ b/crates/gecko/src/starlet.rs@@ -186,6 +186,7 @@ impl System<{ crate::WII }> {let fs_root = self.starlet.host_fs_root.clone();ipc::fs::nand::ensure_skeleton(&fs_root);+ ipc::fs::nand::ensure_wc24_files(&fs_root);self.starlet.register("/dev/stm/immediate", Box::new(stm::Immediate));self.starlet.register("/dev/stm/eventhook", Box::new(stm::EventHook));@@ -196,6 +197,9 @@ impl System<{ crate::WII }> {self.starlet.register("/dev/di", Box::new(ipc::di::DiskInterface::new()));self.starlet.register("/dev/es", Box::new(ipc::es::ETicketServices));+ for path in ["/dev/net/kd/request", "/dev/net/kd/time"] {+ self.starlet.register(path, Box::new(ipc::net::Wc24::new(&fs_root)));+ }self.starlet.register(WIIMOTE_DEVICE_PATH, Box::new(ipc::usb::Bluetooth::new()));self.starlet.register("/dev/sdio/slot0", Box::new(ipc::sdio::SdCard));