Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

es device

Layle4a5f11d03a0b
diff --git a/crates/gecko/src/hollywood/ipc.rs b/crates/gecko/src/hollywood/ipc.rs
index 36042f5..a0a53b3 100644
--- a/crates/gecko/src/hollywood/ipc.rs
+++ b/crates/gecko/src/hollywood/ipc.rs
@@ -1,4 +1,5 @@
pub mod di;
+pub mod es;
pub mod fs;
pub mod stm;
diff --git a/crates/gecko/src/hollywood/ipc/es.rs b/crates/gecko/src/hollywood/ipc/es.rs
new file mode 100644
index 0000000..8605945
--- /dev/null
+++ b/crates/gecko/src/hollywood/ipc/es.rs
@@ -0,0 +1,96 @@
+use crate::hollywood::ipc::IosDevice;
+
+pub const IOCTL_GET_DATA_DIR: u32 = 0x1D;
+pub const IOCTL_GET_TITLE_ID: u32 = 0x20;
+
+pub const ES_EINVAL: i32 = -1017;
+pub const ES_ENOMEM: i32 = -1024;
+pub const ES_NO_TICKET: i32 = -1028;
+pub const ES_INVALID_TICKET: i32 = -1029;
+
+pub struct ETicketServices;
+
+impl IosDevice for ETicketServices {
+ fn ioctlv(
+ &mut self,
+ ctx: &mut super::DeviceContext<'_>,
+ cmd: u32,
+ in_count: u32,
+ io_count: u32,
+ vec_ptr: u32,
+ ) -> i32 {
+ match cmd {
+ 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 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);
+
+ tracing::debug!(
+ title_id = format!("{:016X}", dvd.tmd_title_id()),
+ out_buf = format!("{out_buf:#010X}"),
+ "ES_GetTitleID"
+ );
+ 0
+ }
+ IOCTL_GET_DATA_DIR => {
+ if in_count != 1 || io_count != 1 {
+ tracing::warn!(in_count, io_count, "ES_GetDataDir: unexpected vector counts");
+ 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 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);
+ if (path.len() as u32) + 1 > out_len {
+ tracing::warn!(
+ out_len,
+ needed = path.len() + 1,
+ "ES_GetDataDir: output buffer too small"
+ );
+ 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);
+
+ tracing::debug!(
+ title_id = format!("{requested_title_id:016X}"),
+ %path,
+ out_buf = format!("{out_buf:#010X}"),
+ "ES_GetDataDir"
+ );
+ 0
+ }
+ _ => {
+ tracing::warn!(
+ cmd = format!("{cmd:#010X}"),
+ in_count,
+ io_count,
+ vec_ptr = format!("{vec_ptr:#010X}"),
+ "ETicketServices: unimplemented ioctlv"
+ );
+ ES_EINVAL
+ }
+ }
+ }
+}
diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rs
index 6cbd50f..5dcfddc 100644
--- a/crates/gecko/src/starlet.rs
+++ b/crates/gecko/src/starlet.rs
@@ -68,6 +68,7 @@ impl System<{ crate::WII }> {
self.starlet.register("/dev/fs", Box::new(ipc::fs::FileSystem));
self.starlet
.register("/dev/di", Box::new(ipc::di::DiskInterface::new()));
+ self.starlet.register("/dev/es", Box::new(ipc::es::ETicketServices));
}
pub fn create_device_context(&mut self) -> (&mut Starlet, DeviceContext<'_>) {
diff --git a/crates/image/src/dvd.rs b/crates/image/src/dvd.rs
index 6f43a87..a763a8b 100644
--- a/crates/image/src/dvd.rs
+++ b/crates/image/src/dvd.rs
@@ -16,6 +16,9 @@ pub const PARTITION_TMD_SIZE_OFFSET: usize = 0x2A4;
/// Offset of the `ios_title_id` field within the TMD body.
pub const TMD_IOS_TITLE_ID_OFFSET: usize = 0x184;
+/// Offset of the `title_id` field within the TMD body.
+pub const TMD_TITLE_ID_OFFSET: usize = 0x18C;
+
#[repr(C, packed)]
#[derive(FromBytes, IntoBytes, Immutable, KnownLayout, Debug, Clone, Copy)]
pub struct Header {
diff --git a/crates/image/src/lib.rs b/crates/image/src/lib.rs
index 7ac51cb..d05236b 100644
--- a/crates/image/src/lib.rs
+++ b/crates/image/src/lib.rs
@@ -30,12 +30,7 @@ pub trait Dvd: Send {
fn read_raw_disc(&self, offset: usize, buf: &mut [u8]);
- fn tmd_ios_title_id(&self) -> u64 {
- assert!(
- self.header().is_wii(),
- "TMD IOS title ID is only applicable to Wii discs"
- );
-
+ fn tmd_body_offset(&self) -> usize {
let partition_offset = self.data_partition_offset() as usize;
// Partition header layout: ticket at +0, TMD descriptor at +0x2A4.
@@ -43,12 +38,27 @@ pub trait Dvd: Send {
let mut tmd_descriptor = [0u8; 8];
self.read_raw_disc(partition_offset + dvd::PARTITION_TMD_SIZE_OFFSET, &mut tmd_descriptor);
let tmd_offset_field = u32::from_be_bytes(tmd_descriptor[4..8].try_into().unwrap());
- let tmd_body = partition_offset + ((tmd_offset_field as usize) << 2);
+ partition_offset + ((tmd_offset_field as usize) << 2)
+ }
+
+ fn tmd_ios_title_id(&self) -> u64 {
+ assert!(
+ self.header().is_wii(),
+ "TMD IOS title ID is only applicable to Wii discs"
+ );
let mut ios_title_id = [0u8; 8];
- self.read_raw_disc(tmd_body + dvd::TMD_IOS_TITLE_ID_OFFSET, &mut ios_title_id);
+ self.read_raw_disc(self.tmd_body_offset() + dvd::TMD_IOS_TITLE_ID_OFFSET, &mut ios_title_id);
u64::from_be_bytes(ios_title_id)
}
+
+ fn tmd_title_id(&self) -> u64 {
+ assert!(self.header().is_wii(), "TMD title ID is only applicable to Wii discs");
+
+ let mut title_id = [0u8; 8];
+ self.read_raw_disc(self.tmd_body_offset() + dvd::TMD_TITLE_ID_OFFSET, &mut title_id);
+ u64::from_be_bytes(title_id)
+ }
}
impl<T: Dvd + ?Sized> Dvd for Box<T> {
@@ -75,6 +85,10 @@ impl<T: Dvd + ?Sized> Dvd for Box<T> {
fn tmd_ios_title_id(&self) -> u64 {
(**self).tmd_ios_title_id()
}
+
+ fn tmd_title_id(&self) -> u64 {
+ (**self).tmd_title_id()
+ }
}
pub fn load_dvd(data: Vec<u8>) -> Box<dyn Dvd> {