A GameCube/Wii emulator and debugger written in Rust.
mostly dvd work
Layle6a119516b414
diff --git a/Cargo.lock b/Cargo.lockindex 011e012..2addfe7 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1270,6 +1270,7 @@ dependencies = ["rustc-hash 2.1.2","tracing","twox-hash",+ "zerocopy",][[package]]diff --git a/crates/gecko/Cargo.toml b/crates/gecko/Cargo.tomlindex 9cdb387..6e4dedc 100644--- a/crates/gecko/Cargo.toml+++ b/crates/gecko/Cargo.toml@@ -28,3 +28,4 @@ crossbeam-channel = { version = "0.5.15", optional = true }multiversion = "0.8.0"rustc-hash = "2.1.2"crc32fast = "1.5.0"+zerocopy = { version = "0.8.48", features = ["derive"] }diff --git a/crates/gecko/src/hollywood/ipc.rs b/crates/gecko/src/hollywood/ipc.rsindex c7b4686..36042f5 100644--- a/crates/gecko/src/hollywood/ipc.rs+++ b/crates/gecko/src/hollywood/ipc.rs@@ -2,6 +2,7 @@ pub mod di;pub mod fs;pub mod stm;+use crate::dvd::DvdInterface;use crate::hollywood::regs::{ArmCtrl, ArmMsg, PpcCtrl, PpcMsg};use crate::mmio::Mmio;use crate::scheduler::Scheduler;@@ -13,6 +14,7 @@ pub const IPC_ENOENT: i32 = -6;pub struct DeviceContext<'a> {pub mmio: &'a mut Mmio<{ WII }>,pub scheduler: &'a mut Scheduler<{ WII }>,+ pub di: &'a mut DvdInterface,}pub trait IosDevice: Send {@@ -24,47 +26,47 @@ pub trait IosDevice: Send {0}- fn read(&mut self, _ctx: &mut DeviceContext<'_>, buf: u32, len: u32) -> i32 {- tracing::warn!(buf = format!("{buf:#010X}"), len, "IOS_Read: unimplemented");+ fn read(&mut self, _ctx: &mut DeviceContext<'_>, out_ptr: u32, out_len: u32) -> i32 {+ tracing::warn!(out_ptr = format!("{out_ptr:#010X}"), out_len, "IOS_Read: unimplemented");IPC_EINVAL}- fn write(&mut self, _ctx: &mut DeviceContext<'_>, buf: u32, len: u32) -> i32 {- tracing::warn!(buf = format!("{buf:#010X}"), len, "IOS_Write: unimplemented");+ fn write(&mut self, _ctx: &mut DeviceContext<'_>, in_ptr: u32, in_len: u32) -> i32 {+ tracing::warn!(in_ptr = format!("{in_ptr:#010X}"), in_len, "IOS_Write: unimplemented");IPC_EINVAL}- fn seek(&mut self, _ctx: &mut DeviceContext<'_>, where_: i32, whence: i32) -> i32 {- tracing::warn!(where_ = where_, whence = whence, "IOS_Seek: unimplemented");+ fn seek(&mut self, _ctx: &mut DeviceContext<'_>, offset: i32, whence: i32) -> i32 {+ tracing::warn!(offset, whence, "IOS_Seek: unimplemented");0}fn ioctl(&mut self,- _ctx: &mut DeviceContext<'_>,+ ctx: &mut DeviceContext<'_>,cmd: u32,- in_buf: u32,+ in_ptr: u32,in_len: u32,- out_buf: u32,+ out_ptr: u32,out_len: u32,) -> i32 {tracing::warn!(cmd = format!("{cmd:#010X}"),- in_buf = format!("{in_buf:#010X}"),- in_len = in_len,- out_buf = format!("{out_buf:#010X}"),- out_len = out_len,+ in_buf = format!("{:02X?}", ctx.mmio.phys_slice(in_ptr, in_len as usize)),+ in_len,+ out_buf = format!("{:02X?}", ctx.mmio.phys_slice(out_ptr, out_len as usize)),+ out_len,"IOS_Ioctl: unimplemented");IPC_EINVAL}- fn ioctlv(&mut self, _ctx: &mut DeviceContext<'_>, cmd: u32, argcin: u32, argcio: u32, vec: u32) -> i32 {+ fn ioctlv(&mut self, _ctx: &mut DeviceContext<'_>, cmd: u32, in_count: u32, io_count: u32, vec_ptr: u32) -> i32 {tracing::warn!(cmd = format!("{cmd:#010X}"),- argcin = argcin,- argcio = argcio,- vec = format!("{vec:#010X}"),+ in_count,+ io_count,+ vec_ptr = format!("{vec_ptr:#010X}"),"IOS_Ioctlv: unimplemented");IPC_EINVALdiff --git a/crates/gecko/src/hollywood/ipc/di.rs b/crates/gecko/src/hollywood/ipc/di.rsindex 796441a..4b600db 100644--- a/crates/gecko/src/hollywood/ipc/di.rs+++ b/crates/gecko/src/hollywood/ipc/di.rs@@ -1,5 +1,190 @@-use crate::hollywood::ipc::{DeviceContext, IosDevice};+use crate::hollywood::ipc::{DeviceContext, IPC_EINVAL, IosDevice};+use zerocopy::byteorder::big_endian::U32;+use zerocopy::{FromBytes, Immutable, KnownLayout};-pub struct DiskInterface;+pub const IOCTL_DVD_LOW_UNENCRYPTED_READ: u32 = 0x8D;+pub const IOCTL_DVD_LOW_REQUEST_ERROR: u32 = 0xE0;+pub const IOCTL_DVD_LOW_REPORT_KEY: u32 = 0xA4;-impl IosDevice for DiskInterface {}+const DI_RET_OK: i32 = 1;+const DI_RET_ERROR: i32 = 2;+const DI_RET_INVALID_RANGE: i32 = 0x20;+const DI_RET_BAD_ALIGNMENT: i32 = 0x80;++const DI_ERROR_OK: u32 = 0x00000000;+const DI_ERROR_LBA_OUT_OF_RANGE: u32 = 0x00052100;+const DI_ERROR_INVALID_COMMAND: u32 = 0x00052000;++pub struct DiskInterface {+ pub last_error: u32,+}++impl DiskInterface {+ pub fn new() -> Self {+ Self {+ last_error: DI_ERROR_OK,+ }+ }+}++impl IosDevice for DiskInterface {+ fn ioctl(+ &mut self,+ ctx: &mut DeviceContext<'_>,+ cmd: u32,+ in_ptr: u32,+ in_len: u32,+ out_ptr: u32,+ out_len: u32,+ ) -> i32 {+ match cmd {+ IOCTL_DVD_LOW_UNENCRYPTED_READ => self.dvd_low_unencrypted_read(ctx, in_ptr, out_ptr, out_len),+ IOCTL_DVD_LOW_REQUEST_ERROR => self.dvd_low_request_error(ctx, in_ptr, out_ptr, out_len),+ IOCTL_DVD_LOW_REPORT_KEY => self.dvd_low_report_key(ctx, in_ptr, out_ptr, out_len),+ _ => {+ tracing::warn!(+ cmd = format!("{cmd:08X}"),+ in_buf = format!("{:02X?}", ctx.mmio.phys_slice(in_ptr, in_len as usize)),+ in_len,+ out_buf = format!("{:02X?}", ctx.mmio.phys_slice(out_ptr, out_len as usize)),+ out_len,+ "Unknown IOCTL"+ );+ IPC_EINVAL+ }+ }+ }+}++impl DiskInterface {+ #[inline(always)]+ fn dvd_low_unencrypted_read(+ &mut self,+ ctx: &mut DeviceContext<'_>,+ in_ptr: u32,+ out_ptr: u32,+ out_len: u32,+ ) -> i32 {+ let input = ctx.mmio.phys_read_struct::<DvdLowUnencryptedRead>(in_ptr);+ let length = input.length.get();+ let pos_bytes = input.position_bytes();+ let end_bytes = pos_bytes + length as u64;++ if (out_ptr & 0x1F) != 0 || (length & 0x1F) != 0 {+ return DI_RET_BAD_ALIGNMENT;+ }++ if out_len < length {+ return DI_RET_BAD_ALIGNMENT;+ }++ let range_idx = UNENCRYPTED_RANGES+ .iter()+ .position(|r| pos_bytes >= r.start && end_bytes <= r.end);+ let Some(range_idx) = range_idx else {+ return DI_RET_INVALID_RANGE;+ };++ if range_idx == 0 {+ let Some(dvd) = ctx.di.dvd.as_ref() else {+ self.last_error = DI_ERROR_LBA_OUT_OF_RANGE;+ return DI_RET_ERROR;+ };++ let dst = ctx.mmio.phys_slice_mut(out_ptr, length as usize);+ dvd.read_raw_disc(pos_bytes as usize, dst);+ self.last_error = DI_ERROR_OK;++ tracing::debug!(+ pos = format!("{pos_bytes:#011X}"),+ len = format!("{length:#X}"),+ dst = format!("{out_ptr:#010X}"),+ "DVDLowUnencryptedRead"+ );++ DI_RET_OK+ } else {+ self.last_error = DI_ERROR_LBA_OUT_OF_RANGE;++ tracing::warn!(+ pos = format!("{pos_bytes:#011X}"),+ len = format!("{length:#X}"),+ range_idx,+ "DVDLowUnencryptedRead: LBA out of range, if you see this error it's likely a Nintendo anti-piracy check. Ignore it."+ );++ DI_RET_ERROR+ }+ }++ #[inline(always)]+ fn dvd_low_request_error(&mut self, ctx: &mut DeviceContext<'_>, _in_ptr: u32, out_ptr: u32, _out_len: u32) -> i32 {+ ctx.mmio.phys_write_u32(out_ptr, self.last_error);+ DI_RET_OK+ }++ #[inline(always)]+ fn dvd_low_report_key(&mut self, ctx: &mut DeviceContext<'_>, in_ptr: u32, _out_ptr: u32, _out_len: u32) -> i32 {+ let input = ctx.mmio.phys_read_struct::<DvdLowReportKey>(in_ptr);+ let param1 = input.param1;+ let param2 = input.param2.get();++ // Real retail drives reject this ioctl unconditionally. Nintendo titles+ // probe with (param1=4, param2=0) immediately after the unencrypted+ // read anti-piracy check and require return=2 with the drive error+ // set to "invalid command operation code".+ self.last_error = DI_ERROR_INVALID_COMMAND;++ tracing::warn!(+ param1 = format!("{param1:#04X}"),+ param2 = format!("{param2:#010X}"),+ "DVDLowReportKey"+ );++ DI_RET_ERROR+ }+}++#[repr(C, packed)]+#[derive(FromBytes, KnownLayout, Immutable)]+struct DvdLowUnencryptedRead {+ pub cmd: u8,+ _pad: [u8; 3],+ pub length: U32,+ pub position: U32,+}++impl DvdLowUnencryptedRead {+ fn position_bytes(&self) -> u64 {+ (self.position.get() as u64) << 2+ }+}++#[repr(C, packed)]+#[derive(FromBytes, KnownLayout, Immutable)]+struct DvdLowReportKey {+ pub cmd: u8,+ _pad: [u8; 6],+ pub param1: u8,+ pub param2: U32,+}++struct UnencryptedRange {+ start: u64,+ end: u64,+}++const UNENCRYPTED_RANGES: [UnencryptedRange; 3] = [+ UnencryptedRange {+ start: 0x0_0000_0000,+ end: 0x0_0005_0000,+ },+ UnencryptedRange {+ start: 0x1_1828_0000,+ end: 0x1_1828_0020,+ },+ UnencryptedRange {+ start: 0x1_FB50_0000,+ end: 0x1_FB50_0020,+ },+];diff --git a/crates/gecko/src/hollywood/ipc/fs.rs b/crates/gecko/src/hollywood/ipc/fs.rsindex ace5551..a0267d5 100644--- a/crates/gecko/src/hollywood/ipc/fs.rs+++ b/crates/gecko/src/hollywood/ipc/fs.rs@@ -1,4 +1,4 @@-use crate::hollywood::ipc::{DeviceContext, IosDevice};+use crate::hollywood::ipc::IosDevice;pub struct FileSystem;diff --git a/crates/gecko/src/hollywood/ipc/stm/immediate.rs b/crates/gecko/src/hollywood/ipc/stm/immediate.rsindex 2f3783c..5a2c3da 100644--- a/crates/gecko/src/hollywood/ipc/stm/immediate.rs+++ b/crates/gecko/src/hollywood/ipc/stm/immediate.rs@@ -1,5 +1,32 @@use crate::hollywood::ipc::{DeviceContext, IPC_EINVAL, IosDevice};+const IOCTL_VIDIMMING: u32 = 0x5001;+pub struct Immediate;-impl IosDevice for Immediate {}+impl IosDevice for Immediate {+ fn ioctl(+ &mut self,+ ctx: &mut DeviceContext<'_>,+ cmd: u32,+ in_ptr: u32,+ in_len: u32,+ out_ptr: u32,+ out_len: u32,+ ) -> i32 {+ match cmd {+ IOCTL_VIDIMMING => 0,+ _ => {+ tracing::warn!(+ cmd = format!("{cmd:#010X}"),+ in_buf = format!("{:02X?}", ctx.mmio.phys_slice(in_ptr, in_len as usize)),+ in_len,+ out_buf = format!("{:02X?}", ctx.mmio.phys_slice(out_ptr, out_len as usize)),+ out_len,+ "STM_Immediate: unimplemented ioctl"+ );+ IPC_EINVAL+ }+ }+ }+}diff --git a/crates/gecko/src/mmio.rs b/crates/gecko/src/mmio.rsindex 46e9e31..b346f93 100644--- a/crates/gecko/src/mmio.rs+++ b/crates/gecko/src/mmio.rs@@ -177,6 +177,19 @@ impl<const SYSTEM: SystemId> Mmio<SYSTEM> {&mut slice[offset..offset + len]}+ /// Copy `size_of::<T>()` bytes starting at `addr` into a fresh `T`. The+ /// caller is responsible for declaring `T` with `#[repr(C)]` and using+ /// big-endian wrappers (e.g. `zerocopy::byteorder::big_endian::U32`) for+ /// any multibyte integer fields, since PPC memory is big-endian.+ #[inline(always)]+ pub fn phys_read_struct<T>(&self, addr: u32) -> T+ where+ T: zerocopy::FromBytes + zerocopy::KnownLayout + zerocopy::Immutable,+ {+ let bytes = self.phys_slice(addr, core::mem::size_of::<T>());+ T::read_from_bytes(bytes).expect("phys_read_struct: layout error")+ }+/// Return a slice of virtual memory starting at `addr` with length `len`/// This is just a thin wrapper around `phys_slice` that applies virtual-to-physical translation#[inline(always)]diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rsindex 7a9bae4..6cbd50f 100644--- a/crates/gecko/src/starlet.rs+++ b/crates/gecko/src/starlet.rs@@ -66,7 +66,8 @@ impl System<{ crate::WII }> {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));- self.starlet.register("/dev/di", Box::new(ipc::di::DiskInterface));+ self.starlet+ .register("/dev/di", Box::new(ipc::di::DiskInterface::new()));}pub fn create_device_context(&mut self) -> (&mut Starlet, DeviceContext<'_>) {@@ -75,6 +76,7 @@ impl System<{ crate::WII }> {DeviceContext {mmio: &mut self.mmio,scheduler: &mut self.scheduler,+ di: &mut self.di,},)}@@ -117,14 +119,16 @@ fn process_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr:let path_ptr = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);let mode = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);let path = self::read_c_string(&mut ctx, path_ptr);- tracing::info!(%path, mode, "IOS_Open");let Some(dev) = starlet.device_for_path(&path) else {tracing::error!(%path, "IOS_Open: no device registered");return IPC_ENOENT;};let rc = dev.open(&mut ctx, mode);- if rc >= 0 { starlet.allocate_fd(&path) } else { rc }+ 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");@@ -139,62 +143,62 @@ fn process_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr:dev.close(&mut ctx)}IOS_READ => {- let buf = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);- let len = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);+ let out_ptr = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);+ let out_len = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);- tracing::info!(fd, buf = format!("{buf:#010X}"), len, "IOS_Read");+ tracing::info!(fd, out_ptr = format!("{out_ptr:#010X}"), out_len, "IOS_Read");match starlet.device_for_fd(fd) {- Some(dev) => dev.read(&mut ctx, buf, len),+ Some(dev) => dev.read(&mut ctx, out_ptr, out_len),None => IPC_EINVAL,}}IOS_WRITE => {- let buf = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);- let len = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);+ let in_ptr = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);+ let in_len = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);- tracing::info!(fd, buf = format!("{buf:#010X}"), len, "IOS_Write");+ tracing::info!(fd, in_ptr = format!("{in_ptr:#010X}"), in_len, "IOS_Write");match starlet.device_for_fd(fd) {- Some(dev) => dev.write(&mut ctx, buf, len),+ Some(dev) => dev.write(&mut ctx, in_ptr, in_len),None => IPC_EINVAL,}}IOS_SEEK => {- let where_ = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C) as i32;+ let offset = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C) as i32;let whence = ctx.mmio.phys_read_u32(cmd_paddr + 0x10) as i32;- tracing::info!(fd, where_, whence, "IOS_Seek");+ tracing::info!(fd, offset, whence, "IOS_Seek");match starlet.device_for_fd(fd) {- Some(dev) => dev.seek(&mut ctx, where_, whence),+ Some(dev) => dev.seek(&mut ctx, offset, whence),None => IPC_EINVAL,}}IOS_IOCTL => {let ioctl_cmd = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);- let in_buf = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);+ let in_ptr = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);let in_len = ctx.mmio.phys_read_u32(cmd_paddr + 0x14);- let out_buf = ctx.mmio.phys_read_u32(cmd_paddr + 0x18);+ let out_ptr = ctx.mmio.phys_read_u32(cmd_paddr + 0x18);let out_len = ctx.mmio.phys_read_u32(cmd_paddr + 0x1C);- tracing::info!(fd, ioctl_cmd, "IOS_Ioctl");+ tracing::info!(fd, ioctl_cmd = format!("{ioctl_cmd:#010X}"), "IOS_Ioctl");match starlet.device_for_fd(fd) {- Some(dev) => dev.ioctl(&mut ctx, ioctl_cmd, in_buf, in_len, out_buf, out_len),+ Some(dev) => dev.ioctl(&mut ctx, ioctl_cmd, in_ptr, in_len, out_ptr, out_len),None => IPC_EINVAL,}}IOS_IOCTLV => {let ioctl_cmd = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);- let argcin = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);- let argcio = ctx.mmio.phys_read_u32(cmd_paddr + 0x14);- let vec = ctx.mmio.phys_read_u32(cmd_paddr + 0x18);+ let in_count = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);+ let io_count = ctx.mmio.phys_read_u32(cmd_paddr + 0x14);+ let vec_ptr = ctx.mmio.phys_read_u32(cmd_paddr + 0x18);- tracing::info!(fd, ioctl_cmd, "IOS_Ioctlv");+ tracing::info!(fd, ioctl_cmd = format!("{ioctl_cmd:#010X}"), "IOS_Ioctlv");match starlet.device_for_fd(fd) {- Some(dev) => dev.ioctlv(&mut ctx, ioctl_cmd, argcin, argcio, vec),+ Some(dev) => dev.ioctlv(&mut ctx, ioctl_cmd, in_count, io_count, vec_ptr),None => IPC_EINVAL,}}