A GameCube/Wii emulator and debugger written in Rust.
real fs
Layle9ce224533d80
diff --git a/.gitignore b/.gitignoreindex e2c38e9..1133b92 100644--- a/.gitignore+++ b/.gitignore@@ -7,4 +7,5 @@ out*.txtprivate/*.logout/-__pycache__/\ No newline at end of file+__pycache__/+fs/\ No newline at end of filediff --git a/crates/gecko/src/hollywood/ipc/fs.rs b/crates/gecko/src/hollywood/ipc/fs.rsindex a0267d5..ed60cf6 100644--- a/crates/gecko/src/hollywood/ipc/fs.rs+++ b/crates/gecko/src/hollywood/ipc/fs.rs@@ -1,5 +1,128 @@-use crate::hollywood::ipc::IosDevice;+use crate::hollywood::ipc::{IPC_EINVAL, IosDevice};+use std::fs::{File, OpenOptions};+use std::io::{Read, Seek, SeekFrom, Write};+use std::path::{Path, PathBuf};pub struct FileSystem;impl IosDevice for FileSystem {}++const SEEK_SET: i32 = 0;+const SEEK_CUR: i32 = 1;+const SEEK_END: i32 = 2;++const IOCTL_GET_FILE_STATS: u32 = 0xB;++pub struct HostBackedFile {+ path: PathBuf,+ file: File,+}++impl HostBackedFile {+ pub fn try_open(host_root: &Path, nand_path: &str, mode: u32) -> Option<Self> {+ let host_path = self::nand_to_host(host_root, nand_path);+ if !host_path.is_file() {+ return None;+ }++ let mut opts = OpenOptions::new();+ match mode {+ 0 | 1 => {+ opts.read(true);+ }+ 2 => {+ opts.write(true);+ }+ _ => {+ opts.read(true).write(true);+ }+ };++ match opts.open(&host_path) {+ Ok(file) => Some(Self { path: host_path, file }),+ Err(err) => {+ tracing::warn!(+ path = format!("{}", host_path.display()),+ %err,+ "HostBackedFile: open failed"+ );+ None+ }+ }+ }+}++impl IosDevice for HostBackedFile {+ fn read(&mut self, ctx: &mut super::DeviceContext<'_>, out_ptr: u32, out_len: u32) -> i32 {+ let mut buf = vec![0u8; out_len as usize];+ let n = match self.file.read(&mut buf) {+ Ok(n) => n,+ Err(err) => {+ tracing::warn!(path = format!("{}", self.path.display()), %err, "HostBackedFile: read failed");+ return IPC_EINVAL;+ }+ };+ ctx.mmio.phys_slice_mut(out_ptr, n).copy_from_slice(&buf[..n]);+ n as i32+ }++ fn write(&mut self, ctx: &mut super::DeviceContext<'_>, in_ptr: u32, in_len: u32) -> i32 {+ let buf = ctx.mmio.phys_slice(in_ptr, in_len as usize).to_vec();+ match self.file.write_all(&buf) {+ Ok(()) => in_len as i32,+ Err(err) => {+ tracing::warn!(path = format!("{}", self.path.display()), %err, "HostBackedFile: write failed");+ IPC_EINVAL+ }+ }+ }++ fn seek(&mut self, _ctx: &mut super::DeviceContext<'_>, offset: i32, whence: i32) -> i32 {+ let target = match whence {+ SEEK_SET => SeekFrom::Start(offset as u64),+ SEEK_CUR => SeekFrom::Current(offset as i64),+ SEEK_END => SeekFrom::End(offset as i64),+ _ => {+ tracing::warn!(whence, "HostBackedFile: invalid whence");+ return IPC_EINVAL;+ }+ };++ match self.file.seek(target) {+ Ok(pos) => pos as i32,+ Err(err) => {+ tracing::warn!(path = format!("{}", self.path.display()), %err, "HostBackedFile: seek failed");+ IPC_EINVAL+ }+ }+ }++ fn ioctl(+ &mut self,+ ctx: &mut super::DeviceContext<'_>,+ cmd: u32,+ _in_ptr: u32,+ _in_len: u32,+ out_ptr: u32,+ _out_len: u32,+ ) -> i32 {+ match cmd {+ IOCTL_GET_FILE_STATS => {+ let size = self.file.metadata().map(|m| m.len()).unwrap_or(0) as u32;+ let pos = self.file.stream_position().unwrap_or(0) as u32;+ ctx.mmio.phys_write_u32(out_ptr, size);+ ctx.mmio.phys_write_u32(out_ptr + 4, pos);+ 0+ }+ _ => {+ tracing::warn!(cmd = format!("{cmd:#X}"), "HostBackedFile: unhandled ioctl");+ IPC_EINVAL+ }+ }+ }+}++fn nand_to_host(host_root: &Path, nand_path: &str) -> PathBuf {+ let trimmed = nand_path.trim_start_matches('/');+ host_root.join(trimmed)+}diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rsindex 5dcfddc..fafb532 100644--- a/crates/gecko/src/starlet.rs+++ b/crates/gecko/src/starlet.rs@@ -4,6 +4,7 @@ use crate::{hollywood::ipc::{DeviceContext, IosDevice},};use std::collections::{HashMap, VecDeque};+use std::path::PathBuf;// As per zaydconst FINALIZE_DELAY_CYCLES: u64 = 10_000;@@ -14,11 +15,20 @@ pub struct PendingResponse {pub result: i32,}+/// Backing storage for an open fd. Control devices share state across fds via+/// a path key into the registry; host backed files own their device instance+/// directly so each fd has its own cursor.+enum FdEntry {+ Shared(String),+ Owned(Box<dyn IosDevice>),+}+pub struct Starlet {devices: HashMap<String, Box<dyn IosDevice>>,- handles: HashMap<i32, String>,+ handles: HashMap<i32, FdEntry>,next_fd: i32,pub pending: VecDeque<PendingResponse>,+ pub host_fs_root: PathBuf,}impl Starlet {@@ -28,6 +38,7 @@ impl Starlet {handles: HashMap::new(),next_fd: 1,pending: VecDeque::new(),+ host_fs_root: self::default_host_fs_root(),}}@@ -36,27 +47,69 @@ impl Starlet {self.devices.insert(path.to_owned(), dev);}- /// Allocate a fresh fd and bind it to `path`.+ /// Allocate a fresh fd bound to a registered (shared) device path.pub fn allocate_fd(&mut self, path: &str) -> i32 {- let fd = self.next_fd;- self.next_fd = self.next_fd.checked_add(1).expect("Starlet fd overflow");- self.handles.insert(fd, path.to_owned());+ let fd = self.fresh_fd();+ self.handles.insert(fd, FdEntry::Shared(path.to_owned()));fd}- /// Drop the fd to path mapping. Returns the path the fd was bound to.- pub fn release_fd(&mut self, fd: i32) -> Option<String> {- self.handles.remove(&fd)+ /// Allocate a fresh fd that owns the given device instance. Used for+ /// per-open resources (host-backed real files).+ pub fn allocate_owned_fd(&mut self, dev: Box<dyn IosDevice>) -> i32 {+ let fd = self.fresh_fd();+ self.handles.insert(fd, FdEntry::Owned(dev));+ fd+ }++ fn fresh_fd(&mut self) -> i32 {+ let fd = self.next_fd;+ self.next_fd = self.next_fd.checked_add(1).expect("Starlet fd overflow");+ fd}pub fn device_for_path(&mut self, path: &str) -> Option<&mut Box<dyn IosDevice>> {self.devices.get_mut(path)}- pub fn device_for_fd(&mut self, fd: i32) -> Option<&mut Box<dyn IosDevice>> {- let path = self.handles.get(&fd)?.clone();- self.devices.get_mut(&path)+ pub fn device_for_fd(&mut self, fd: i32) -> Option<&mut dyn IosDevice> {+ let path = match self.handles.get(&fd)? {+ FdEntry::Shared(p) => Some(p.clone()),+ FdEntry::Owned(_) => None,+ };++ if let Some(path) = path {+ return self.devices.get_mut(&path).map(|b| b.as_mut() as &mut dyn IosDevice);+ }++ match self.handles.get_mut(&fd)? {+ FdEntry::Owned(dev) => Some(dev.as_mut()),+ FdEntry::Shared(_) => unreachable!(),+ }+ }++ /// Drop an fd. Calls `close` on the underlying device first; for owned+ /// fds the device is dropped after close.+ pub fn close_fd(&mut self, fd: i32, ctx: &mut DeviceContext<'_>) -> i32 {+ let Some(entry) = self.handles.remove(&fd) else {+ return crate::hollywood::ipc::IPC_EINVAL;+ };++ match entry {+ FdEntry::Owned(mut dev) => dev.close(ctx),+ FdEntry::Shared(path) => match self.devices.get_mut(&path) {+ Some(dev) => dev.close(ctx),+ None => crate::hollywood::ipc::IPC_EINVAL,+ },+ }+ }+}++fn default_host_fs_root() -> PathBuf {+ if let Some(custom) = std::env::var_os("GECKO_FS_ROOT") {+ return PathBuf::from(custom);}+ PathBuf::from("fs")}impl System<{ crate::WII }> {@@ -121,27 +174,24 @@ fn process_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr:let mode = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);let path = self::read_c_string(&mut ctx, path_ptr);- let Some(dev) = starlet.device_for_path(&path) else {+ let fd = if let Some(dev) = starlet.device_for_path(&path) {+ let rc = dev.open(&mut ctx, mode);+ if rc >= 0 { starlet.allocate_fd(&path) } else { rc }+ } else if let Some(real) =+ crate::hollywood::ipc::fs::HostBackedFile::try_open(&starlet.host_fs_root, &path, mode)+ {+ starlet.allocate_owned_fd(Box::new(real))+ } else {tracing::error!(%path, "IOS_Open: no device registered");- return IPC_ENOENT;+ IPC_ENOENT};- let rc = dev.open(&mut ctx, mode);- let fd = if rc >= 0 { starlet.allocate_fd(&path) } else { rc };tracing::info!(%path, mode, fd, "IOS_Open");fd}IOS_CLOSE => {tracing::info!(fd, "IOS_Close");-- let Some(path) = starlet.release_fd(fd) else {- return IPC_EINVAL;- };- let Some(dev) = starlet.device_for_path(&path) else {- return IPC_EINVAL;- };-- dev.close(&mut ctx)+ starlet.close_fd(fd, &mut ctx)}IOS_READ => {let out_ptr = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);