Skip to content

Commit

Permalink
Extract Wii region.bin
Browse files Browse the repository at this point in the history
  • Loading branch information
encounter committed Oct 5, 2024
1 parent d99ef72 commit f463836
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ strip = "debuginfo"
codegen-units = 1

[workspace.package]
version = "1.4.1"
version = "1.4.2"
edition = "2021"
rust-version = "1.74"
authors = ["Luke Street <[email protected]>"]
Expand Down
1 change: 1 addition & 0 deletions nod/src/disc/gcn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ pub(crate) fn read_part_meta(
raw_apploader: raw_apploader.into_boxed_slice(),
raw_fst,
raw_dol: raw_dol.into_boxed_slice(),
raw_region: None,
raw_ticket: None,
raw_tmd: None,
raw_cert_chain: None,
Expand Down
4 changes: 3 additions & 1 deletion nod/src/disc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) mod wii;

pub use fst::{Fst, Node, NodeKind};
pub use streams::{FileStream, OwnedFileStream, WindowedStream};
pub use wii::{SignedHeader, Ticket, TicketLimit, TmdHeader};
pub use wii::{SignedHeader, Ticket, TicketLimit, TmdHeader, REGION_SIZE};

/// Size in bytes of a disc sector.
pub const SECTOR_SIZE: usize = 0x8000;
Expand Down Expand Up @@ -365,6 +365,8 @@ pub struct PartitionMeta {
pub raw_fst: Box<[u8]>,
/// Main binary (main.dol)
pub raw_dol: Box<[u8]>,
/// Disc region info (region.bin, Wii only)
pub raw_region: Option<Box<[u8; REGION_SIZE]>>,
/// Ticket (ticket.bin, Wii only)
pub raw_ticket: Option<Box<[u8]>>,
/// TMD (tmd.bin, Wii only)
Expand Down
19 changes: 18 additions & 1 deletion nod/src/disc/wii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ use crate::{
KeyBytes,
},
static_assert,
util::{div_rem, read::read_box_slice},
util::{
div_rem,
read::{read_box, read_box_slice},
},
Error, OpenOptions, Result, ResultContext,
};

Expand All @@ -31,6 +34,12 @@ pub(crate) const HASHES_SIZE: usize = 0x400;
/// Size in bytes of the data block in a Wii disc sector (excluding hashes)
pub(crate) const SECTOR_DATA_SIZE: usize = SECTOR_SIZE - HASHES_SIZE; // 0x7C00

/// Size of the disc region info (region.bin)
pub const REGION_SIZE: usize = 0x20;

/// Offset of the disc region info
pub const REGION_OFFSET: u64 = 0x4E000;

// ppki (Retail)
const RVL_CERT_ISSUER_PPKI_TICKET: &str = "Root-CA00000001-XS00000003";
#[rustfmt::skip]
Expand Down Expand Up @@ -270,6 +279,7 @@ pub struct PartitionWii {
sector: u32,
pos: u64,
verify: bool,
raw_region: Box<[u8; REGION_SIZE]>,
raw_tmd: Box<[u8]>,
raw_cert_chain: Box<[u8]>,
raw_h3_table: Box<[u8]>,
Expand All @@ -287,6 +297,7 @@ impl Clone for PartitionWii {
sector: u32::MAX,
pos: 0,
verify: self.verify,
raw_region: self.raw_region.clone(),
raw_tmd: self.raw_tmd.clone(),
raw_cert_chain: self.raw_cert_chain.clone(),
raw_h3_table: self.raw_h3_table.clone(),
Expand All @@ -304,6 +315,10 @@ impl PartitionWii {
let block_size = inner.block_size();
let mut reader = PartitionGC::new(inner, disc_header)?;

// Read region
reader.seek(SeekFrom::Start(REGION_OFFSET)).context("Seeking to region offset")?;
let raw_region: Box<[u8; REGION_SIZE]> = read_box(&mut reader).context("Reading region")?;

// Read TMD, cert chain, and H3 table
let offset = partition.start_sector as u64 * SECTOR_SIZE as u64;
reader
Expand Down Expand Up @@ -333,6 +348,7 @@ impl PartitionWii {
sector: u32::MAX,
pos: 0,
verify: options.validate_hashes,
raw_region,
raw_tmd,
raw_cert_chain,
raw_h3_table,
Expand Down Expand Up @@ -482,6 +498,7 @@ impl PartitionBase for PartitionWii {
fn meta(&mut self) -> Result<Box<PartitionMeta>> {
self.seek(SeekFrom::Start(0)).context("Seeking to partition header")?;
let mut meta = read_part_meta(self, true)?;
meta.raw_region = Some(self.raw_region.clone());
meta.raw_ticket = Some(Box::from(self.partition.header.ticket.as_bytes()));
meta.raw_tmd = Some(self.raw_tmd.clone());
meta.raw_cert_chain = Some(self.raw_cert_chain.clone());
Expand Down
27 changes: 15 additions & 12 deletions nodtool/src/cmd/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,21 @@ fn extract_sys_files(
fs::create_dir_all(&disc_dir)
.with_context(|| format!("Creating directory {}", display(&disc_dir)))?;
extract_file(&header.as_bytes()[..0x100], &disc_dir.join("header.bin"), quiet)?;
}
if let Some(ticket) = data.raw_ticket.as_deref() {
extract_file(ticket, &out_dir.join("ticket.bin"), quiet)?;
}
if let Some(tmd) = data.raw_tmd.as_deref() {
extract_file(tmd, &out_dir.join("tmd.bin"), quiet)?;
}
if let Some(cert_chain) = data.raw_cert_chain.as_deref() {
extract_file(cert_chain, &out_dir.join("cert.bin"), quiet)?;
}
if let Some(h3_table) = data.raw_h3_table.as_deref() {
extract_file(h3_table, &out_dir.join("h3.bin"), quiet)?;
if let Some(region) = data.raw_region.as_deref() {
extract_file(region, &disc_dir.join("region.bin"), quiet)?;
}
if let Some(ticket) = data.raw_ticket.as_deref() {
extract_file(ticket, &out_dir.join("ticket.bin"), quiet)?;
}
if let Some(tmd) = data.raw_tmd.as_deref() {
extract_file(tmd, &out_dir.join("tmd.bin"), quiet)?;
}
if let Some(cert_chain) = data.raw_cert_chain.as_deref() {
extract_file(cert_chain, &out_dir.join("cert.bin"), quiet)?;
}
if let Some(h3_table) = data.raw_h3_table.as_deref() {
extract_file(h3_table, &out_dir.join("h3.bin"), quiet)?;
}
}
Ok(())
}
Expand Down

0 comments on commit f463836

Please sign in to comment.