Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

impl ios realods and disc reopening for wii titles

Layle73b6284abd2e
diff --git a/crates/gecko/src/hollywood/ipc.rs b/crates/gecko/src/hollywood/ipc.rs
index 07c5257..deb077a 100644
--- a/crates/gecko/src/hollywood/ipc.rs
+++ b/crates/gecko/src/hollywood/ipc.rs
@@ -25,6 +25,26 @@ pub struct DeviceContext<'a> {
pub di: &'a mut DvdInterface,
pub pi: &'a mut ProcessorInterface,
pub device_path: String,
+ pub reload_ios: Option<&'static crate::wii::ios::MemoryValues>,
+}
+
+/// Guest address of ioctlv vector `idx`.
+#[inline(always)]
+pub(crate) fn vec_data<const SYS: SystemId>(mmio: &Mmio<SYS>, vec_ptr: u32, idx: u32) -> u32 {
+ mmio.phys_read_u32(vec_ptr + idx * 8)
+}
+
+/// Length of ioctlv vector `idx`.
+#[inline(always)]
+pub(crate) fn vec_len<const SYS: SystemId>(mmio: &Mmio<SYS>, vec_ptr: u32, idx: u32) -> u32 {
+ mmio.phys_read_u32(vec_ptr + idx * 8 + 4)
+}
+
+/// Whether the ioctlv vectors have exactly the lengths in `lens`.
+pub(crate) fn vec_lens_match<const SYS: SystemId>(mmio: &Mmio<SYS>, vec_ptr: u32, lens: &[u32]) -> bool {
+ (0..)
+ .zip(lens)
+ .all(|(idx, &len)| self::vec_len(mmio, vec_ptr, idx) == len)
}
pub trait IosDevice: Send {
diff --git a/crates/gecko/src/hollywood/ipc/di.rs b/crates/gecko/src/hollywood/ipc/di.rs
index df0b6c8..863b24f 100644
--- a/crates/gecko/src/hollywood/ipc/di.rs
+++ b/crates/gecko/src/hollywood/ipc/di.rs
@@ -3,9 +3,11 @@ use crate::hollywood::ipc::{DeviceContext, IPC_EINVAL, IosDevice};
use zerocopy::byteorder::big_endian::U32;
use zerocopy::{FromBytes, Immutable, KnownLayout};
+pub const IOCTL_DVD_READ_DISC_ID: u32 = 0x70;
pub const IOCTL_DVD_LOW_READ: u32 = 0x71;
pub const IOCTL_DVD_LOW_GET_COVER_REGISTER: u32 = 0x7A;
pub const IOCTL_DVD_LOW_CLEAR_COVER_INTERRUPT: u32 = 0x86;
+pub const IOCTL_DVD_LOW_OPEN_PARTITION: u32 = 0x8B;
pub const IOCTL_DVD_LOW_UNENCRYPTED_READ: u32 = 0x8D;
pub const IOCTL_DVD_LOW_GET_STATUS_REGISTER: u32 = 0x95;
pub const IOCTL_DVD_LOW_GET_CONTROL_REGISTER: u32 = 0x96;
@@ -36,6 +38,22 @@ impl DiskInterface {
}
impl IosDevice for DiskInterface {
+ fn ioctlv(&mut self, ctx: &mut DeviceContext<'_>, cmd: u32, in_count: u32, io_count: u32, vec_ptr: u32) -> i32 {
+ match cmd {
+ IOCTL_DVD_LOW_OPEN_PARTITION => self.dvd_low_open_partition(ctx, in_count, io_count, vec_ptr),
+ _ => {
+ tracing::warn!(
+ cmd = format!("{cmd:#010X}"),
+ in_count,
+ io_count,
+ vec_ptr = format!("{vec_ptr:#010X}"),
+ "Unknown IOCTLV"
+ );
+ IPC_EINVAL
+ }
+ }
+ }
+
fn save_state(&mut self, w: &mut crate::savestate::StateWriter) {
w.u32(self.last_error);
}
@@ -55,6 +73,7 @@ impl IosDevice for DiskInterface {
out_len: u32,
) -> i32 {
match cmd {
+ IOCTL_DVD_READ_DISC_ID => self.dvd_read_disc_id(ctx, out_ptr, out_len),
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),
@@ -81,6 +100,86 @@ impl IosDevice for DiskInterface {
}
impl DiskInterface {
+ fn dvd_low_open_partition(
+ &mut self,
+ ctx: &mut DeviceContext<'_>,
+ in_count: u32,
+ io_count: u32,
+ vec_ptr: u32,
+ ) -> i32 {
+ if in_count != 3 || io_count != 2 {
+ return DI_RET_SECURITY_ERROR;
+ }
+ if !super::vec_lens_match(ctx.mmio, vec_ptr, &[0x20]) || super::vec_len(ctx.mmio, vec_ptr, 4) < 4 {
+ return DI_RET_SECURITY_ERROR;
+ }
+
+ let in_ptr = super::vec_data(ctx.mmio, vec_ptr, 0);
+ let tmd_ptr = super::vec_data(ctx.mmio, vec_ptr, 3);
+ let result_ptr = super::vec_data(ctx.mmio, vec_ptr, 4);
+ if (in_ptr & 3) != 0 || (tmd_ptr & 0x1F) != 0 || (result_ptr & 3) != 0 {
+ return DI_RET_BAD_ALIGNMENT;
+ }
+
+ let partition_offset = (ctx.mmio.phys_read_u32(in_ptr + 4) as u64) << 2;
+ let Some(dvd) = ctx.di.dvd.as_ref() else {
+ self.last_error = DI_ERROR_LBA_OUT_OF_RANGE;
+ return DI_RET_ERROR;
+ };
+
+ if !dvd.header().is_wii() || partition_offset != dvd.data_partition_offset() {
+ self.last_error = DI_ERROR_LBA_OUT_OF_RANGE;
+ return DI_RET_ERROR;
+ }
+
+ let (tmd_size, tmd_offset) = dvd.tmd_descriptor();
+ if super::vec_len(ctx.mmio, vec_ptr, 3) < tmd_size {
+ return DI_RET_SECURITY_ERROR;
+ }
+
+ dvd.read_raw_disc(tmd_offset, ctx.mmio.phys_slice_mut(tmd_ptr, tmd_size as usize));
+ ctx.mmio.phys_write_u32(result_ptr, 0);
+
+ #[cfg(feature = "jit")]
+ ctx.mmio.queue_icbi_for_range(tmd_ptr, tmd_size);
+
+ self.last_error = DI_ERROR_OK;
+
+ tracing::debug!(
+ partition_offset = format!("{partition_offset:#011X}"),
+ tmd_size,
+ dst = format!("{tmd_ptr:#010X}"),
+ "DVDLowOpenPartition"
+ );
+ DI_RET_OK
+ }
+
+ #[inline(always)]
+ fn dvd_read_disc_id(&mut self, ctx: &mut DeviceContext<'_>, out_ptr: u32, out_len: u32) -> i32 {
+ if (out_ptr & 0x1F) != 0 {
+ return DI_RET_BAD_ALIGNMENT;
+ }
+ if out_len < 0x20 {
+ return DI_RET_SECURITY_ERROR;
+ }
+
+ 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, 0x20);
+ dvd.read_raw_disc(0, dst);
+
+ #[cfg(feature = "jit")]
+ ctx.mmio.queue_icbi_for_range(out_ptr, 0x20);
+
+ self.last_error = DI_ERROR_OK;
+
+ tracing::debug!(dst = format!("{out_ptr:#010X}"), "DVDLowReadDiskID");
+ DI_RET_OK
+ }
+
#[inline(always)]
fn dvd_low_unencrypted_read(
&mut self,
diff --git a/crates/gecko/src/hollywood/ipc/es.rs b/crates/gecko/src/hollywood/ipc/es.rs
index 52032df..2c48380 100644
--- a/crates/gecko/src/hollywood/ipc/es.rs
+++ b/crates/gecko/src/hollywood/ipc/es.rs
@@ -1,8 +1,9 @@
-use image::dvd::PARTITION_TMD_SIZE_OFFSET;
-
use crate::hollywood::ipc::IosDevice;
+use crate::wii::ios::MemoryValues;
+pub const IOCTL_LAUNCH: u32 = 0x08;
pub const IOCTL_GET_NUM_TICKET_VIEWS: u32 = 0x12;
+pub const IOCTL_GET_TICKET_VIEWS: u32 = 0x13;
pub const IOCTL_GET_CONSUMPTION: u32 = 0x16;
pub const IOCTL_DI_GET_TICKET_VIEW: u32 = 0x1B;
pub const IOCTL_GET_DATA_DIR: u32 = 0x1D;
@@ -15,6 +16,8 @@ pub const ES_ENOMEM: i32 = -1024;
pub const ES_NO_TICKET: i32 = -1028;
pub const ES_INVALID_TICKET: i32 = -1029;
+const TICKET_VIEW_SIZE: u32 = 0xD8;
+
pub struct ETicketServices;
impl IosDevice for ETicketServices {
@@ -27,44 +30,75 @@ impl IosDevice for ETicketServices {
vec_ptr: u32,
) -> i32 {
match cmd {
- IOCTL_GET_NUM_TICKET_VIEWS => {
- if in_count != 1 || io_count != 1 {
+ IOCTL_LAUNCH => {
+ if in_count != 2 || io_count != 0 || !super::vec_lens_match(ctx.mmio, vec_ptr, &[8, TICKET_VIEW_SIZE]) {
return ES_EINVAL;
}
- if ctx.mmio.phys_read_u32(vec_ptr + 4) != 8 || ctx.mmio.phys_read_u32(vec_ptr + 12) != 4 {
+
+ let title_id = self::read_title_id(ctx, vec_ptr);
+ let Some(imv) = self::hle_ios(title_id) else {
+ return super::fs::FS_ENOENT;
+ };
+
+ tracing::debug!(title_id = format!("{title_id:016X}"), "ES_LaunchTitle: reload IOS");
+ ctx.reload_ios = Some(imv);
+ 0
+ }
+ IOCTL_GET_NUM_TICKET_VIEWS => {
+ if in_count != 1 || io_count != 1 || !super::vec_lens_match(ctx.mmio, vec_ptr, &[8, 4]) {
return ES_EINVAL;
}
- let in_buf = ctx.mmio.phys_read_u32(vec_ptr);
- let title_id =
- ((ctx.mmio.phys_read_u32(in_buf) as u64) << 32) | ctx.mmio.phys_read_u32(in_buf + 4) as u64;
- let out_buf = ctx.mmio.phys_read_u32(vec_ptr + 8);
-
- let ios_number = title_id as u32;
- let available =
- title_id >> 32 == 1 && ios_number < 0x100 && crate::wii::ios::for_ios(ios_number as u16).is_some();
- let count = available as u32;
- ctx.mmio.phys_write_u32(out_buf, count);
+ let title_id = self::read_title_id(ctx, vec_ptr);
+ let count = self::hle_ios(title_id).is_some() as u32;
+ ctx.mmio.phys_write_u32(super::vec_data(ctx.mmio, vec_ptr, 1), count);
tracing::debug!(title_id = format!("{title_id:016X}"), count, "ES_GetNumTicketViews");
0
}
+ IOCTL_GET_TICKET_VIEWS => {
+ if in_count != 2 || io_count != 1 || !super::vec_lens_match(ctx.mmio, vec_ptr, &[8, 4]) {
+ return ES_EINVAL;
+ }
+
+ let title_id = self::read_title_id(ctx, vec_ptr);
+ let requested_count = ctx.mmio.phys_read_u32(super::vec_data(ctx.mmio, vec_ptr, 1));
+ let has_view = self::hle_ios(title_id).is_some() && requested_count != 0;
+ if has_view {
+ if super::vec_len(ctx.mmio, vec_ptr, 2) < TICKET_VIEW_SIZE {
+ return ES_EINVAL;
+ }
+
+ let out_buf = super::vec_data(ctx.mmio, vec_ptr, 2);
+ let view = ctx.mmio.phys_slice_mut(out_buf, TICKET_VIEW_SIZE as usize);
+ view.fill(0);
+ view[0x10..0x18].copy_from_slice(&title_id.to_be_bytes());
+ }
+
+ tracing::debug!(
+ title_id = format!("{title_id:016X}"),
+ requested_count,
+ count = has_view as u32,
+ "ES_GetTicketViews"
+ );
+ 0
+ }
IOCTL_GET_TITLE_ID => {
if in_count != 0 || io_count != 1 {
tracing::warn!(in_count, io_count, "ES_GetTitleID: unexpected vector counts");
return ES_EINVAL;
}
- let out_buf = ctx.mmio.phys_read_u32(vec_ptr);
+ let out_buf = super::vec_data(ctx.mmio, vec_ptr, 0);
let Some(dvd) = ctx.di.dvd.as_ref() else {
return ES_EINVAL;
};
- ctx.mmio.phys_write_u32(out_buf, (dvd.tmd_title_id() >> 32) as u32);
- ctx.mmio.phys_write_u32(out_buf + 4, dvd.tmd_title_id() as u32);
+ let title_id = dvd.tmd_title_id();
+ ctx.mmio.phys_write_u64(out_buf, title_id);
tracing::debug!(
- title_id = format!("{:016X}", dvd.tmd_title_id()),
+ title_id = format!("{title_id:016X}"),
out_buf = format!("{out_buf:#010X}"),
"ES_GetTitleID"
);
@@ -84,18 +118,15 @@ impl IosDevice for ETicketServices {
return ES_EINVAL;
}
- let in_buf = ctx.mmio.phys_read_u32(vec_ptr);
- let requested_title_id =
- ((ctx.mmio.phys_read_u32(in_buf) as u64) << 32) | (ctx.mmio.phys_read_u32(in_buf + 4) as u64);
-
+ let requested_title_id = self::read_title_id(ctx, vec_ptr);
let path = format!(
"/title/{:08x}/{:08x}/data",
(requested_title_id >> 32) as u32,
requested_title_id as u32
);
- let out_buf = ctx.mmio.phys_read_u32(vec_ptr + 8);
- let out_len = ctx.mmio.phys_read_u32(vec_ptr + 12);
+ let out_buf = super::vec_data(ctx.mmio, vec_ptr, 1);
+ let out_len = super::vec_len(ctx.mmio, vec_ptr, 1);
if (path.len() as u32) + 1 > out_len {
tracing::warn!(
out_len,
@@ -105,10 +136,9 @@ impl IosDevice for ETicketServices {
return ES_EINVAL;
}
- for (i, b) in path.bytes().enumerate() {
- ctx.mmio.phys_write_u8(out_buf + i as u32, b);
- }
- ctx.mmio.phys_write_u8(out_buf + path.len() as u32, 0);
+ let out = ctx.mmio.phys_slice_mut(out_buf, path.len() + 1);
+ out[..path.len()].copy_from_slice(path.as_bytes());
+ out[path.len()] = 0;
tracing::debug!(
title_id = format!("{requested_title_id:016X}"),
@@ -119,41 +149,27 @@ impl IosDevice for ETicketServices {
0
}
IOCTL_DI_GET_TMD_SIZE => {
- if in_count != 0 || io_count != 1 {
- return ES_EINVAL;
- }
- if ctx.mmio.phys_read_u32(vec_ptr + 4) != 4 {
+ if in_count != 0 || io_count != 1 || !super::vec_lens_match(ctx.mmio, vec_ptr, &[4]) {
return ES_EINVAL;
}
let Some(dvd) = ctx.di.dvd.as_ref() else {
return ES_EINVAL;
};
- let tmd_size_addr = dvd.data_partition_offset() as usize + PARTITION_TMD_SIZE_OFFSET;
-
- let mut size = [0u8; 4];
- dvd.read_raw_disc(tmd_size_addr, &mut size);
- let size = u32::from_be_bytes(size);
-
- tracing::debug!(addr = format!("{:#010X}", tmd_size_addr), size, "ES_DIGetTmdSize");
-
- let out_buf = ctx.mmio.phys_read_u32(vec_ptr);
- ctx.mmio.phys_write_u32(out_buf, size);
+ let (size, _) = dvd.tmd_descriptor();
+ ctx.mmio.phys_write_u32(super::vec_data(ctx.mmio, vec_ptr, 0), size);
+ tracing::debug!(size, "ES_DIGetTmdSize");
0
}
IOCTL_DI_GET_TMD => {
- if in_count != 1 || io_count != 1 {
- return ES_EINVAL;
- }
- if ctx.mmio.phys_read_u32(vec_ptr + 4) != 4 {
+ if in_count != 1 || io_count != 1 || !super::vec_lens_match(ctx.mmio, vec_ptr, &[4]) {
return ES_EINVAL;
}
- let in_buf = ctx.mmio.phys_read_u32(vec_ptr);
- let requested_size = ctx.mmio.phys_read_u32(in_buf);
- let out_buf = ctx.mmio.phys_read_u32(vec_ptr + 8);
- let out_len = ctx.mmio.phys_read_u32(vec_ptr + 12);
+ let requested_size = ctx.mmio.phys_read_u32(super::vec_data(ctx.mmio, vec_ptr, 0));
+ let out_buf = super::vec_data(ctx.mmio, vec_ptr, 1);
+ let out_len = super::vec_len(ctx.mmio, vec_ptr, 1);
if requested_size != out_len {
return ES_EINVAL;
}
@@ -162,17 +178,11 @@ impl IosDevice for ETicketServices {
return ES_EINVAL;
};
- let tmd_size_addr = dvd.data_partition_offset() as usize + PARTITION_TMD_SIZE_OFFSET;
-
- let mut size = [0u8; 4];
- dvd.read_raw_disc(tmd_size_addr, &mut size);
- let size = u32::from_be_bytes(size);
-
+ let (size, tmd_addr) = dvd.tmd_descriptor();
if out_len < size {
return ES_EINVAL;
}
- let tmd_addr = dvd.tmd_body_offset();
dvd.read_raw_disc(tmd_addr, ctx.mmio.phys_slice_mut(out_buf, size as usize));
tracing::debug!(
@@ -197,3 +207,15 @@ impl IosDevice for ETicketServices {
}
}
}
+
+fn read_title_id(ctx: &super::DeviceContext<'_>, vec_ptr: u32) -> u64 {
+ ctx.mmio.phys_read_u64(super::vec_data(ctx.mmio, vec_ptr, 0))
+}
+
+fn hle_ios(title_id: u64) -> Option<&'static MemoryValues> {
+ let ios_number = title_id as u32;
+ if title_id >> 32 != 1 || ios_number >= 0x100 {
+ return None;
+ }
+ crate::wii::ios::for_ios(ios_number as u16)
+}
diff --git a/crates/gecko/src/hollywood/ipc/fs.rs b/crates/gecko/src/hollywood/ipc/fs.rs
index b6a411e..56c2f95 100644
--- a/crates/gecko/src/hollywood/ipc/fs.rs
+++ b/crates/gecko/src/hollywood/ipc/fs.rs
@@ -137,11 +137,11 @@ impl FileSystem {
}
};
- let path_ptr = self::vec_data(ctx, vec_ptr, 0);
- let path_len = self::vec_len(ctx, vec_ptr, 0) as usize;
+ let path_ptr = super::vec_data(ctx.mmio, vec_ptr, 0);
+ let path_len = super::vec_len(ctx.mmio, vec_ptr, 0) as usize;
let count_idx = if with_names { 3 } else { 1 };
- let count_ptr = self::vec_data(ctx, vec_ptr, count_idx);
- let count_len = self::vec_len(ctx, vec_ptr, count_idx);
+ let count_ptr = super::vec_data(ctx.mmio, vec_ptr, count_idx);
+ let count_len = super::vec_len(ctx.mmio, vec_ptr, count_idx);
if path_len < FS_MAX_PATH || count_len < 4 {
tracing::warn!(path_len, count_len, "FS_ReadDir: invalid vector sizes");
@@ -203,10 +203,10 @@ impl FileSystem {
return 0;
}
- let max_count_ptr = self::vec_data(ctx, vec_ptr, 1);
- let max_count_len = self::vec_len(ctx, vec_ptr, 1);
- let names_ptr = self::vec_data(ctx, vec_ptr, 2);
- let names_len = self::vec_len(ctx, vec_ptr, 2);
+ let max_count_ptr = super::vec_data(ctx.mmio, vec_ptr, 1);
+ let max_count_len = super::vec_len(ctx.mmio, vec_ptr, 1);
+ let names_ptr = super::vec_data(ctx.mmio, vec_ptr, 2);
+ let names_len = super::vec_len(ctx.mmio, vec_ptr, 2);
if max_count_len < 4 {
tracing::warn!(max_count_len, "FS_ReadDir: invalid max count vector size");
@@ -310,12 +310,12 @@ impl FileSystem {
return FS_EINVAL;
}
- let path_ptr = self::vec_data(ctx, vec_ptr, 0);
- let path_len = self::vec_len(ctx, vec_ptr, 0) as usize;
- let used_clusters_ptr = self::vec_data(ctx, vec_ptr, 1);
- let used_clusters_len = self::vec_len(ctx, vec_ptr, 1);
- let used_inodes_ptr = self::vec_data(ctx, vec_ptr, 2);
- let used_inodes_len = self::vec_len(ctx, vec_ptr, 2);
+ let path_ptr = super::vec_data(ctx.mmio, vec_ptr, 0);
+ let path_len = super::vec_len(ctx.mmio, vec_ptr, 0) as usize;
+ let used_clusters_ptr = super::vec_data(ctx.mmio, vec_ptr, 1);
+ let used_clusters_len = super::vec_len(ctx.mmio, vec_ptr, 1);
+ let used_inodes_ptr = super::vec_data(ctx.mmio, vec_ptr, 2);
+ let used_inodes_len = super::vec_len(ctx.mmio, vec_ptr, 2);
if path_len < FS_MAX_PATH || used_clusters_len < 4 || used_inodes_len < 4 {
tracing::warn!(
@@ -671,13 +671,3 @@ fn read_guest_path(ctx: &DeviceContext<'_>, ptr: u32, len: usize) -> Option<Stri
let end = bytes.iter().position(|&b| b == 0)?;
Some(String::from_utf8_lossy(&bytes[..end]).into_owned())
}
-
-#[inline(always)]
-fn vec_data(ctx: &DeviceContext<'_>, vec_ptr: u32, idx: u32) -> u32 {
- ctx.mmio.phys_read_u32(vec_ptr + idx * 8)
-}
-
-#[inline(always)]
-fn vec_len(ctx: &DeviceContext<'_>, vec_ptr: u32, idx: u32) -> u32 {
- ctx.mmio.phys_read_u32(vec_ptr + idx * 8 + 4)
-}
diff --git a/crates/gecko/src/hollywood/ipc/usb.rs b/crates/gecko/src/hollywood/ipc/usb.rs
index c10b96e..c8a61e9 100644
--- a/crates/gecko/src/hollywood/ipc/usb.rs
+++ b/crates/gecko/src/hollywood/ipc/usb.rs
@@ -105,21 +105,21 @@ impl IosDevice for Bluetooth {
impl Bluetooth {
fn handle_control(&mut self, ctx: &mut DeviceContext<'_>, vec_ptr: u32) -> i32 {
- let bm_request = ctx.mmio.phys_read_u8(self::vec_data(ctx.mmio, vec_ptr, 0));
- let b_request = ctx.mmio.phys_read_u8(self::vec_data(ctx.mmio, vec_ptr, 1));
+ let bm_request = ctx.mmio.phys_read_u8(super::vec_data(ctx.mmio, vec_ptr, 0));
+ let b_request = ctx.mmio.phys_read_u8(super::vec_data(ctx.mmio, vec_ptr, 1));
if bm_request != 0x20 || b_request != 0 {
return 0;
}
let w_length = {
- let p = self::vec_data(ctx.mmio, vec_ptr, 4);
+ let p = super::vec_data(ctx.mmio, vec_ptr, 4);
u16::from_le_bytes([ctx.mmio.phys_read_u8(p), ctx.mmio.phys_read_u8(p + 1)])
};
if w_length < 3 {
return 0;
}
- let data_ptr = self::vec_data(ctx.mmio, vec_ptr, 6);
+ let data_ptr = super::vec_data(ctx.mmio, vec_ptr, 6);
let packet = ctx.mmio.phys_slice(data_ptr, w_length as usize).to_vec();
if packet.len() < 2 {
return 0;
@@ -130,10 +130,10 @@ impl Bluetooth {
}
fn handle_bulk(&mut self, ctx: &mut DeviceContext<'_>, vec_ptr: u32) -> i32 {
- let endpoint = ctx.mmio.phys_read_u8(self::vec_data(ctx.mmio, vec_ptr, 0));
- let len_p = self::vec_data(ctx.mmio, vec_ptr, 1);
+ let endpoint = ctx.mmio.phys_read_u8(super::vec_data(ctx.mmio, vec_ptr, 0));
+ let len_p = super::vec_data(ctx.mmio, vec_ptr, 1);
let w_length = u16::from_le_bytes([ctx.mmio.phys_read_u8(len_p), ctx.mmio.phys_read_u8(len_p + 1)]);
- let buf_ptr = self::vec_data(ctx.mmio, vec_ptr, 2);
+ let buf_ptr = super::vec_data(ctx.mmio, vec_ptr, 2);
match endpoint {
EP_BULK_OUT => {
@@ -173,7 +173,7 @@ impl Bluetooth {
/// HCI event receive (interrupt IN endpoint 0x81). Deliver the next
/// queued event, or reply length 0 (the SDK seems to tolerate that).
fn handle_interrupt(&mut self, ctx: &mut DeviceContext<'_>, vec_ptr: u32) -> i32 {
- if ctx.mmio.phys_read_u8(self::vec_data(ctx.mmio, vec_ptr, 0)) != EP_INT_IN {
+ if ctx.mmio.phys_read_u8(super::vec_data(ctx.mmio, vec_ptr, 0)) != EP_INT_IN {
return 0;
}
@@ -181,7 +181,7 @@ impl Bluetooth {
return 0;
};
- let buf_ptr = self::vec_data(ctx.mmio, vec_ptr, 2);
+ let buf_ptr = super::vec_data(ctx.mmio, vec_ptr, 2);
ctx.mmio.phys_slice_mut(buf_ptr, event.len()).copy_from_slice(&event);
tracing::debug!(
@@ -322,8 +322,3 @@ fn push_capped(queue: &mut VecDeque<Vec<u8>>, frame: Vec<u8>) {
queue.push_back(frame);
}
-
-#[inline(always)]
-fn vec_data<const SYS: crate::system::SystemId>(mmio: &crate::mmio::Mmio<SYS>, vec_ptr: u32, idx: u32) -> u32 {
- mmio.phys_read_u32(vec_ptr + (idx * 8))
-}
diff --git a/crates/gecko/src/mmio.rs b/crates/gecko/src/mmio.rs
index c0e422d..6a2d94f 100644
--- a/crates/gecko/src/mmio.rs
+++ b/crates/gecko/src/mmio.rs
@@ -381,6 +381,17 @@ impl<const SYSTEM: SystemId> Mmio<SYSTEM> {
value
}
+ #[inline(always)]
+ pub fn phys_read_u64(&self, addr: u32) -> u64 {
+ ((self.phys_read_u32(addr) as u64) << 32) | self.phys_read_u32(addr + 4) as u64
+ }
+
+ #[inline(always)]
+ pub fn phys_write_u64(&mut self, addr: u32, value: u64) {
+ self.phys_write_u32(addr, (value >> 32) as u32);
+ self.phys_write_u32(addr + 4, value as u32);
+ }
+
#[inline(always)]
pub fn phys_write_u8(&mut self, addr: u32, value: u8) {
let (slice, offset) = self.resolve_mut(addr);
diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rs
index 2545627..157c605 100644
--- a/crates/gecko/src/starlet.rs
+++ b/crates/gecko/src/starlet.rs
@@ -17,11 +17,15 @@ const ACK_TO_NEXT_DELAY_US: u64 = 100;
const DEVICE_STATE_CAPACITY: usize = 256;
-pub struct PendingResponse {
- pub cmd_paddr: u32,
- pub result: i32,
+pub enum PendingResponse {
+ Reply { cmd_paddr: u32, result: i32 },
+ // A bare ACK without a reply, e.g. the second ACK of an IOS reload.
+ Ack,
}
+// Savestate encoding of `PendingResponse::Ack`, never a real IPC result.
+const SAVED_ACK_RESULT: i32 = i32::MIN + 1;
+
/// 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.
@@ -181,6 +185,27 @@ impl Starlet {
}
impl System<{ crate::WII }> {
+ fn reload_ios(&mut self, imv: &crate::wii::ios::MemoryValues) {
+ // Refresh IOS memory without rerunning the disc boot
+ // setup, which would overwrite the running loader's state.
+ Self::write_ios_low_memory(self, imv);
+
+ self.scheduler.cancel(crate::scheduler::Handler::IpcDeliver);
+
+ let fs_root = self.starlet.host_fs_root.clone();
+ let sysconf = self.starlet.devices.remove("/shared2/sys/SYSCONF");
+ self.starlet = Starlet::new();
+ self.starlet.host_fs_root = fs_root;
+
+ self.initialize_starlet_devices();
+
+ if let Some(sysconf) = sysconf {
+ self.starlet.register("/shared2/sys/SYSCONF", sysconf);
+ }
+
+ tracing::debug!(ios = imv.ios_number, "IOS reloaded");
+ }
+
pub fn initialize_starlet_devices(&mut self) {
use crate::hollywood::ipc::{self, stm};
@@ -214,19 +239,38 @@ impl System<{ crate::WII }> {
di: &mut self.di,
pi: &mut self.pi,
device_path: "<unbound>".to_owned(),
+ reload_ios: None,
},
)
}
}
pub fn dispatch_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr: u32) {
- let result = self::process_command(sys, cmd_paddr);
+ assert!(SYSTEM == crate::WII, "Starlet dispatch reached on non-Wii system");
+
+ let wii: &mut crate::Wii = unsafe { ::core::mem::transmute(sys) };
+
+ let (result, reload_ios) = self::process_command(wii, cmd_paddr);
+ if let Some(imv) = reload_ios {
+ crate::hollywood::ipc::deliver_ack(wii);
+ wii.reload_ios(imv);
+ // libogc's relaunch request waits for two ACKs. Queue the second one separately so the PPC can observe and
+ // clear the first.
+ wii.starlet.pending.push_back(PendingResponse::Ack);
+ self::ensure_delivery_scheduled(wii, ACK_TO_NEXT_DELAY_US);
+ return;
+ }
+
if result == crate::hollywood::ipc::IPC_HUNG {
- crate::hollywood::ipc::deliver_ack(sys);
+ crate::hollywood::ipc::deliver_ack(wii);
return;
}
- sys.starlet.pending.push_back(PendingResponse { cmd_paddr, result });
- self::ensure_delivery_scheduled::<SYSTEM>(sys, FINALIZE_DELAY_US);
+
+ wii.starlet
+ .pending
+ .push_back(PendingResponse::Reply { cmd_paddr, result });
+
+ self::ensure_delivery_scheduled(wii, FINALIZE_DELAY_US);
}
pub fn schedule_drain<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
@@ -253,13 +297,10 @@ const IOS_SEEK: u32 = 5;
const IOS_IOCTL: u32 = 6;
const IOS_IOCTLV: u32 = 7;
-fn process_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr: u32) -> i32 {
+fn process_command(wii: &mut crate::Wii, cmd_paddr: u32) -> (i32, Option<&'static crate::wii::ios::MemoryValues>) {
use crate::hollywood::ipc::fs::FS_ENOENT;
use crate::hollywood::ipc::{IPC_EINVAL, IPC_ENOENT};
- assert!(SYSTEM == crate::WII, "Starlet dispatch reached on non-Wii system");
-
- let wii: &mut crate::Wii = unsafe { ::core::mem::transmute(sys) };
let cmd = wii.mmio.phys_read_u32(cmd_paddr);
let fd = wii.mmio.phys_read_u32(cmd_paddr + 0x08) as i32;
@@ -267,7 +308,7 @@ fn process_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr:
let (starlet, mut ctx) = wii.create_device_context();
- match cmd {
+ let result = match cmd {
IOS_OPEN => {
let path_ptr = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);
let mode = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);
@@ -393,7 +434,9 @@ fn process_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr:
tracing::error!(cmd = other, "unimplemented IOS command");
IPC_EINVAL
}
- }
+ };
+
+ (result, ctx.reload_ios)
}
fn sync_ios_buffers(wii: &mut crate::Wii, cmd_paddr: u32, cmd: u32) {
@@ -456,10 +499,15 @@ fn bind_fd_context(starlet: &Starlet, ctx: &mut DeviceContext<'_>, fd: i32) -> S
pub(crate) fn deliver_pending<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
sys.starlet.delivery_scheduled = false;
- if sys.hollywood.ipc.ppcctrl.arm_response() {
+ let ppcctrl = &sys.hollywood.ipc.ppcctrl;
+ let busy = match sys.starlet.pending.front() {
+ Some(PendingResponse::Ack) => ppcctrl.arm_response() || ppcctrl.arm_post_ack(),
+ _ => ppcctrl.arm_response(),
+ };
+ if busy {
tracing::trace!(
queue_len = sys.starlet.pending.len(),
- "deliver_pending: arm_response still set, skipping"
+ "deliver_pending: waiting for PPC to consume the previous IPC signal"
);
if !sys.starlet.pending.is_empty() {
@@ -474,14 +522,21 @@ pub(crate) fn deliver_pending<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>)
return;
};
- tracing::trace!(
- cmd_paddr = format!("{:#010X}", p.cmd_paddr),
- result = p.result,
- remaining = sys.starlet.pending.len(),
- "deliver_pending"
- );
-
- crate::hollywood::ipc::deliver_response(sys, p.cmd_paddr, p.result);
+ match p {
+ PendingResponse::Reply { cmd_paddr, result } => {
+ tracing::trace!(
+ cmd_paddr = format!("{cmd_paddr:#010X}"),
+ result,
+ remaining = sys.starlet.pending.len(),
+ "deliver_pending"
+ );
+ crate::hollywood::ipc::deliver_response(sys, cmd_paddr, result);
+ }
+ PendingResponse::Ack => {
+ tracing::trace!(remaining = sys.starlet.pending.len(), "deliver_pending: ack");
+ crate::hollywood::ipc::deliver_ack(sys);
+ }
+ }
if !sys.starlet.pending.is_empty() {
self::ensure_delivery_scheduled::<SYSTEM>(sys, ACK_TO_NEXT_DELAY_US);
@@ -495,8 +550,12 @@ impl Starlet {
w.u32(self.pending.len() as u32);
for response in &self.pending {
- w.u32(response.cmd_paddr);
- w.i32(response.result);
+ let (cmd_paddr, result) = match *response {
+ PendingResponse::Reply { cmd_paddr, result } => (cmd_paddr, result),
+ PendingResponse::Ack => (0, SAVED_ACK_RESULT),
+ };
+ w.u32(cmd_paddr);
+ w.i32(result);
}
let mut devices: Vec<_> = self.devices.iter_mut().collect();
@@ -544,9 +603,10 @@ impl Starlet {
self.pending.clear();
let pending_count = r.u32()?;
for _ in 0..pending_count {
- self.pending.push_back(PendingResponse {
- cmd_paddr: r.u32()?,
- result: r.i32()?,
+ let (cmd_paddr, result) = (r.u32()?, r.i32()?);
+ self.pending.push_back(match result {
+ SAVED_ACK_RESULT => PendingResponse::Ack,
+ _ => PendingResponse::Reply { cmd_paddr, result },
});
}
diff --git a/crates/gecko/src/wii.rs b/crates/gecko/src/wii.rs
index 4b6cd6a..acedf92 100644
--- a/crates/gecko/src/wii.rs
+++ b/crates/gecko/src/wii.rs
@@ -148,6 +148,24 @@ impl Wii {
}
// Cross referenced with beanwii and Dolphin!!
+ pub(crate) fn write_ios_low_memory(emu: &mut Wii, imv: &ios::MemoryValues) {
+ // MEM2 layout.
+ emu.mmio.phys_write_u32(0x0000_3118, imv.mem2_physical_size);
+ emu.mmio.phys_write_u32(0x0000_311C, imv.mem2_simulated_size);
+ emu.mmio.phys_write_u32(0x0000_3120, imv.mem2_end);
+ emu.mmio.phys_write_u32(0x0000_3124, imv.mem2_arena_begin);
+ emu.mmio.phys_write_u32(0x0000_3128, imv.mem2_arena_end);
+
+ // IPC buffer (ARM <-> PPC shared region).
+ emu.mmio.phys_write_u32(0x0000_3130, imv.ipc_buffer_begin);
+ emu.mmio.phys_write_u32(0x0000_3134, imv.ipc_buffer_end);
+
+ // IOS version + reserved heap.
+ emu.mmio.phys_write_u32(0x0000_3140, imv.ios_version);
+ emu.mmio.phys_write_u32(0x0000_3148, imv.ios_reserved_begin);
+ emu.mmio.phys_write_u32(0x0000_314C, imv.ios_reserved_end);
+ }
+
fn setup_low_memory_common(emu: &mut Wii, imv: &ios::MemoryValues) {
// Ripped from the apploader.
emu.mmio
@@ -183,25 +201,12 @@ impl Wii {
// entry is >= 0x80004000; this is the fallback.
emu.mmio.phys_write_u32(0x0000_3110, imv.mem1_arena_end);
- // MEM2 layout.
- emu.mmio.phys_write_u32(0x0000_3118, imv.mem2_physical_size);
- emu.mmio.phys_write_u32(0x0000_311C, imv.mem2_simulated_size);
- emu.mmio.phys_write_u32(0x0000_3120, imv.mem2_end);
- emu.mmio.phys_write_u32(0x0000_3124, imv.mem2_arena_begin);
- emu.mmio.phys_write_u32(0x0000_3128, imv.mem2_arena_end);
-
- // IPC buffer (ARM <-> PPC shared region).
- emu.mmio.phys_write_u32(0x0000_3130, imv.ipc_buffer_begin);
- emu.mmio.phys_write_u32(0x0000_3134, imv.ipc_buffer_end);
+ Self::write_ios_low_memory(emu, imv);
// Hollywood revision.
emu.mmio.phys_write_u32(0x0000_3138, imv.hollywood_revision);
- // IOS version + date + reserved heap.
- emu.mmio.phys_write_u32(0x0000_3140, imv.ios_version);
emu.mmio.phys_write_u32(0x0000_3144, imv.ios_date);
- emu.mmio.phys_write_u32(0x0000_3148, imv.ios_reserved_begin);
- emu.mmio.phys_write_u32(0x0000_314C, imv.ios_reserved_end);
// GDDR vendor code.
emu.mmio.phys_write_u32(0x0000_3158, imv.ram_vendor);
diff --git a/crates/image/src/lib.rs b/crates/image/src/lib.rs
index cc060fa..60c775f 100644
--- a/crates/image/src/lib.rs
+++ b/crates/image/src/lib.rs
@@ -35,15 +35,21 @@ pub trait Dvd: Send {
fn read_raw_disc(&self, offset: usize, buf: &mut [u8]);
- fn tmd_body_offset(&self) -> usize {
+ /// Returns the data partition's TMD `(size, absolute body offset)`.
+ fn tmd_descriptor(&self) -> (u32, usize) {
let partition_offset = self.data_partition_offset() as usize;
- // Partition header layout: ticket at +0, TMD descriptor at +0x2A4.
- // We only need the TMD offset field (>>2).
+ // Partition header layout: ticket at +0, TMD descriptor at +0x2A4:
+ // size, then offset (>>2).
let mut tmd_descriptor = [0u8; 8];
self.read_raw_disc(partition_offset + dvd::PARTITION_TMD_SIZE_OFFSET, &mut tmd_descriptor);
+ let size = u32::from_be_bytes(tmd_descriptor[0..4].try_into().unwrap());
let tmd_offset_field = u32::from_be_bytes(tmd_descriptor[4..8].try_into().unwrap());
- partition_offset + ((tmd_offset_field as usize) << 2)
+ (size, partition_offset + ((tmd_offset_field as usize) << 2))
+ }
+
+ fn tmd_body_offset(&self) -> usize {
+ self.tmd_descriptor().1
}
fn tmd_ios_title_id(&self) -> u64 {