Skip to content

Commit 1548cae

Browse files
committed
fix wii web patching
1 parent c9c8d64 commit 1548cae

File tree

5 files changed

+90
-95
lines changed

5 files changed

+90
-95
lines changed

geckolib/src/crypto/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ pub mod consts {
1515
pub const WII_HASH_SIZE: usize = 160 / 8; // 160 bits with 8 bits per byte
1616
pub const WII_KEY_SIZE: usize = 16;
1717
pub const WII_CKEY_AMNT: usize = 3;
18-
pub const WII_H3_OFFSET: usize = 0x8000;
19-
pub const WII_H3_SIZE: usize = 0x18000;
18+
pub const WII_H3_OFFSET: u64 = 0x8000;
19+
pub const WII_H3_SIZE: u64 = 0x18000;
2020
pub const WII_HX_OFFSETS: [usize; 3] = [0, 0x280, 0x340];
2121
pub const WII_SECTOR_HASH_SIZE: usize = 0x400;
2222
pub const WII_SECTOR_SIZE: usize = 0x8000;
23-
pub const WII_SECTOR_DATA_SIZE: usize = WII_SECTOR_SIZE - WII_SECTOR_HASH_SIZE;
24-
pub const WII_SECTOR_IV_OFF: usize = 0x3D0;
25-
pub const WII_PARTITION_INFO_OFF: usize = 0x40000;
26-
pub const WII_SECTOR_DATA_HASH_SIZE: usize = 0x400; // Size of chunks of data hashed together for h0
23+
pub const WII_SECTOR_DATA_SIZE: u64 = (WII_SECTOR_SIZE - WII_SECTOR_HASH_SIZE) as u64;
24+
pub const WII_SECTOR_IV_OFF: u64 = 0x3D0;
25+
pub const WII_PARTITION_INFO_OFFSET: u64 = 0x40000;
26+
pub const WII_SECTOR_DATA_HASH_SIZE: u64 = 0x400; // Size of chunks of data hashed together for h0
2727
/// Number of chunks of data which are hashed for h0.
28-
pub const WII_SECTOR_DATA_HASH_COUNT: usize = WII_SECTOR_DATA_SIZE / WII_SECTOR_DATA_HASH_SIZE;
28+
pub const WII_SECTOR_DATA_HASH_COUNT: u64 = WII_SECTOR_DATA_SIZE / WII_SECTOR_DATA_HASH_SIZE;
2929
}
3030

3131
const COMMON_KEY_: [[u8; consts::WII_KEY_SIZE]; consts::WII_CKEY_AMNT] = [

geckolib/src/iso/disc.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,17 @@ pub async fn disc_get_part_info_async<R: AsyncRead + AsyncSeek>(
244244
let mut entries: Vec<PartInfoEntry> = Vec::new();
245245
let mut buf: [u8; 8] = [0u8; 8];
246246
reader
247-
.seek(SeekFrom::Start(consts::WII_PARTITION_INFO_OFF as u64))
247+
.seek(SeekFrom::Start(consts::WII_PARTITION_INFO_OFFSET))
248248
.await?;
249249
reader.read(&mut buf).await?;
250-
let n_part: usize = BE::read_u32(&buf[..]) as usize;
250+
let n_part = BE::read_u32(&buf[..]) as u64;
251251
let part_info_offset = (BE::read_u32(&buf[4..]) as u64) << 2;
252252
crate::debug!(
253253
"Found {:} entries, partition info at offset 0x{:08X}",
254254
n_part,
255255
part_info_offset
256256
);
257-
for i in 0..n_part as u64 {
257+
for i in 0..n_part {
258258
reader
259259
.seek(SeekFrom::Start(part_info_offset + (8 * i)))
260260
.await?;
@@ -275,11 +275,11 @@ pub fn disc_set_part_info(buffer: &mut [u8], pi: &PartInfo) {
275275
BE::write_u32(&mut buffer[0x40004..], (pi.offset >> 2) as u32);
276276
for (i, entry) in pi.entries.iter().enumerate() {
277277
BE::write_u32(
278-
&mut buffer[pi.offset as usize + (8 * i)..],
278+
&mut buffer[(pi.offset + (8 * i as u64)) as usize..],
279279
(entry.offset >> 2) as u32,
280280
);
281281
BE::write_u32(
282-
&mut buffer[pi.offset as usize + (8 * i) + 4..],
282+
&mut buffer[(pi.offset + (8 * i as u64) + 4) as usize..],
283283
entry.part_type,
284284
);
285285
}
@@ -743,7 +743,7 @@ pub struct WiiDisc {
743743
#[derive(Debug, Copy, Clone)]
744744
#[repr(C)]
745745
pub struct WiiSectorHash {
746-
pub(crate) h0: [[u8; consts::WII_HASH_SIZE]; consts::WII_SECTOR_DATA_HASH_COUNT],
746+
pub(crate) h0: [[u8; consts::WII_HASH_SIZE]; consts::WII_SECTOR_DATA_HASH_COUNT as usize],
747747
_h0_padding: [u8; 20],
748748
pub(crate) h1: [[u8; consts::WII_HASH_SIZE]; 8],
749749
_h1_padding: [u8; 32],
@@ -758,7 +758,7 @@ const_assert_eq!(
758758
impl Default for WiiSectorHash {
759759
fn default() -> Self {
760760
WiiSectorHash {
761-
h0: [[0u8; consts::WII_HASH_SIZE]; consts::WII_SECTOR_DATA_HASH_COUNT],
761+
h0: [[0u8; consts::WII_HASH_SIZE]; consts::WII_SECTOR_DATA_HASH_COUNT as usize],
762762
_h0_padding: [0u8; 20],
763763
h1: [[0u8; consts::WII_HASH_SIZE]; 8],
764764
_h1_padding: [0u8; 32],
@@ -781,7 +781,7 @@ impl WiiSectorHash {
781781
unsafe { std::mem::transmute(self) }
782782
}
783783

784-
pub fn get_h0_ref(&self) -> &[u8; consts::WII_HASH_SIZE * consts::WII_SECTOR_DATA_HASH_COUNT] {
784+
pub fn get_h0_ref(&self) -> &[u8; consts::WII_HASH_SIZE * consts::WII_SECTOR_DATA_HASH_COUNT as usize] {
785785
unsafe { std::mem::transmute(&self.h0) }
786786
}
787787

@@ -795,7 +795,7 @@ impl WiiSectorHash {
795795

796796
pub fn get_h0_mut(
797797
&mut self,
798-
) -> &mut [u8; consts::WII_HASH_SIZE * consts::WII_SECTOR_DATA_HASH_COUNT] {
798+
) -> &mut [u8; consts::WII_HASH_SIZE * consts::WII_SECTOR_DATA_HASH_COUNT as usize] {
799799
unsafe { std::mem::transmute(&mut self.h0) }
800800
}
801801

@@ -812,15 +812,15 @@ impl WiiSectorHash {
812812
#[repr(C)]
813813
pub struct WiiSector {
814814
pub(crate) hash: WiiSectorHash,
815-
pub(crate) data: [u8; consts::WII_SECTOR_DATA_SIZE],
815+
pub(crate) data: [u8; consts::WII_SECTOR_DATA_SIZE as usize],
816816
}
817817
const_assert_eq!(std::mem::size_of::<WiiSector>(), consts::WII_SECTOR_SIZE);
818818

819819
impl Default for WiiSector {
820820
fn default() -> Self {
821821
WiiSector {
822822
hash: WiiSectorHash::default(),
823-
data: [0u8; consts::WII_SECTOR_DATA_SIZE],
823+
data: [0u8; consts::WII_SECTOR_DATA_SIZE as usize],
824824
}
825825
}
826826
}

geckolib/src/iso/read.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,12 @@ where
286286
return Poll::Ready(Ok(0));
287287
}
288288
// Calculate the size and bounds of what has to be read.
289-
let read_size = std::cmp::min(buf.len(), (decrypted_size - state.cursor) as usize);
289+
let read_size = std::cmp::min(buf.len() as u64, decrypted_size - state.cursor);
290290
// The "virtual" start and end, in the sense that they are the positions within the decrypted partition.
291291
let vstart = state.cursor;
292292
let vend = vstart + read_size as u64;
293-
let start_blk_idx = (vstart / consts::WII_SECTOR_DATA_SIZE as u64) as usize;
294-
let end_blk_idx = ((vend - 1) / consts::WII_SECTOR_DATA_SIZE as u64) as usize;
293+
let start_blk_idx = vstart / consts::WII_SECTOR_DATA_SIZE;
294+
let end_blk_idx = (vend - 1) / consts::WII_SECTOR_DATA_SIZE;
295295
crate::trace!(
296296
"Loading data from 0x{:08X} to 0x{:08X} (spanning {} block(s))",
297297
vstart,
@@ -303,12 +303,12 @@ where
303303
WiiDiscReaderState::Seeking => {
304304
let start_blk_addr = part.part_offset
305305
+ part.header.data_offset
306-
+ (start_blk_idx * consts::WII_SECTOR_SIZE) as u64;
306+
+ (start_blk_idx * consts::WII_SECTOR_SIZE as u64);
307307
crate::trace!("Seeking to 0x{:08X}", start_blk_addr);
308308
ready!(pin!(&mut this.reader).poll_seek(cx, SeekFrom::Start(start_blk_addr)))?;
309309
crate::trace!("Seeking succeeded");
310310
let n_blk = end_blk_idx - start_blk_idx + 1;
311-
let buf = vec![0u8; n_blk * consts::WII_SECTOR_SIZE];
311+
let buf = vec![0u8; (n_blk * consts::WII_SECTOR_SIZE as u64) as usize];
312312
state.state = WiiDiscReaderState::Reading(buf);
313313
cx.waker().wake_by_ref();
314314
Poll::Pending
@@ -334,7 +334,7 @@ where
334334
let decrypt_process = move |data: &mut &mut [u8]| {
335335
let mut iv = [0_u8; consts::WII_KEY_SIZE];
336336
iv[..consts::WII_KEY_SIZE].copy_from_slice(
337-
&data[consts::WII_SECTOR_IV_OFF..][..consts::WII_KEY_SIZE],
337+
&data[consts::WII_SECTOR_IV_OFF as usize..][..consts::WII_KEY_SIZE],
338338
);
339339
crate::trace!("iv: {:?}", iv);
340340
crate::trace!("before: {:?}", &data[consts::WII_SECTOR_HASH_SIZE..][..6]);
@@ -346,7 +346,7 @@ where
346346
&part_key,
347347
);
348348
aes_decrypt_inplace(
349-
&mut data[consts::WII_SECTOR_HASH_SIZE..][..consts::WII_SECTOR_DATA_SIZE],
349+
&mut data[consts::WII_SECTOR_HASH_SIZE..][..consts::WII_SECTOR_DATA_SIZE as usize],
350350
&iv,
351351
&part_key,
352352
);
@@ -361,21 +361,20 @@ where
361361
crate::trace!("Decryption done");
362362
for (i, block) in data_pool.iter().enumerate() {
363363
let block_pos =
364-
(start_blk_idx + i) as u64 * consts::WII_SECTOR_DATA_SIZE as u64;
365-
let buf_write_start =
366-
std::cmp::max(0, block_pos as i64 - vstart as i64) as usize;
367-
let buf_write_end: usize = std::cmp::min(
364+
(start_blk_idx + i as u64) * consts::WII_SECTOR_DATA_SIZE;
365+
let buf_write_start: u64 =
366+
std::cmp::max(0, block_pos as i64 - vstart as i64) as u64;
367+
let buf_write_end: u64 = std::cmp::min(
368368
read_size,
369-
((block_pos + consts::WII_SECTOR_DATA_SIZE as u64) as i64 - vstart as i64)
370-
as usize,
369+
((block_pos + consts::WII_SECTOR_DATA_SIZE) as i64 - vstart as i64) as u64,
371370
);
372371
let block_read_start =
373372
std::cmp::max(0, vstart as i64 - block_pos as i64) as usize;
374373
let block_read_end = std::cmp::min(
375-
consts::WII_SECTOR_DATA_SIZE as u64,
374+
consts::WII_SECTOR_DATA_SIZE,
376375
(vstart + read_size as u64) - block_pos,
377376
) as usize;
378-
buf[buf_write_start..buf_write_end].copy_from_slice(
377+
buf[buf_write_start as usize..buf_write_end as usize].copy_from_slice(
379378
&block[consts::WII_SECTOR_HASH_SIZE..][block_read_start..block_read_end],
380379
);
381380
}

geckolib/src/iso/write.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ enum WiiDiscWriterState {
3131
/// Setups the data to be written
3232
Init,
3333
/// Parse the data to be written
34-
Parse(u64, usize, Vec<u8>),
34+
Parse(u64, u64, Vec<u8>),
3535
/// Writing the accumulated data
36-
Writing(u64, usize, Vec<u8>, Vec<u8>),
36+
Writing(u64, u64, Vec<u8>, Vec<u8>),
3737

3838
// States for the closing process
3939
SeekToLastGroup(u64, Vec<u8>),
@@ -79,10 +79,10 @@ fn h0_process(sector: &mut WiiSector) {
7979
let hash = &mut sector.hash;
8080
let data = &sector.data;
8181
for j in 0..consts::WII_SECTOR_DATA_HASH_COUNT {
82-
hash.h0[j].copy_from_slice(
82+
hash.h0[j as usize].copy_from_slice(
8383
&Sha1::from(
84-
&data[j * consts::WII_SECTOR_DATA_HASH_SIZE
85-
..(j + 1) * consts::WII_SECTOR_DATA_HASH_SIZE],
84+
&data[(j * consts::WII_SECTOR_DATA_HASH_SIZE) as usize
85+
..((j + 1) * consts::WII_SECTOR_DATA_HASH_SIZE) as usize],
8686
)
8787
.digest()
8888
.bytes(),
@@ -161,13 +161,13 @@ fn hash_group(group: &mut WiiGroup) -> [u8; consts::WII_HASH_SIZE] {
161161
/// Implementation of the Segher's fake signing algorithm
162162
fn fake_sign(part: &mut WiiPartition, hashes: &[[u8; consts::WII_HASH_SIZE]]) {
163163
let content = &mut part.tmd.contents[0];
164-
let mut hashes_ = Vec::with_capacity(consts::WII_H3_SIZE);
164+
let mut hashes_ = Vec::with_capacity(consts::WII_H3_SIZE as usize);
165165
hashes_.extend(hashes.iter().flatten());
166-
hashes_.resize(consts::WII_H3_SIZE, 0);
166+
hashes_.resize(consts::WII_H3_SIZE as usize, 0);
167167
crate::debug!(
168168
"[fake_sign] Hashes size: 0x{:08X}; Hashes padding size: 0x{:08X}; H3 size: 0x{:08X}",
169169
hashes.len() * consts::WII_HASH_SIZE,
170-
consts::WII_H3_SIZE - hashes.len() * consts::WII_HASH_SIZE,
170+
consts::WII_H3_SIZE - hashes.len() as u64 * consts::WII_HASH_SIZE as u64,
171171
hashes_.len()
172172
);
173173
content
@@ -199,7 +199,7 @@ fn encrypt_group(group: &mut WiiGroup, part_key: AesKey) {
199199
let mut iv = [0u8; consts::WII_KEY_SIZE];
200200
aes_encrypt_inplace(sector.hash.as_array_mut(), &iv, &part_key);
201201
iv[..consts::WII_KEY_SIZE].copy_from_slice(
202-
&sector.hash.as_array_mut()[consts::WII_SECTOR_IV_OFF..][..consts::WII_KEY_SIZE],
202+
&sector.hash.as_array_mut()[consts::WII_SECTOR_IV_OFF as usize..][..consts::WII_KEY_SIZE],
203203
);
204204
aes_encrypt_inplace(&mut sector.data, &iv, &part_key);
205205
};
@@ -307,7 +307,7 @@ where
307307
part.header.tmd_offset = PartHeader::BLOCK_SIZE as u64;
308308
part.header.cert_offset = part.header.tmd_offset + part.header.tmd_size as u64;
309309
part.header.h3_offset = std::cmp::max(
310-
consts::WII_H3_OFFSET as u64,
310+
consts::WII_H3_OFFSET,
311311
part.header.cert_offset + part.header.cert_size as u64,
312312
);
313313
part.header.data_offset =
@@ -351,15 +351,15 @@ fn prepare_header(part: &mut WiiPartition, hashes: &[[u8; consts::WII_HASH_SIZE]
351351
let part_offset = part.part_offset;
352352
crate::debug!("Partition offset: 0x{part_offset:08X?}");
353353
crate::trace!("Partition Header: {:?}", part.header);
354-
let mut buf = Vec::with_capacity(part.header.h3_offset as usize + consts::WII_H3_SIZE);
354+
let mut buf = Vec::with_capacity((part.header.h3_offset + consts::WII_H3_SIZE) as usize);
355355
let h3_padding =
356356
part.header.h3_offset as usize - (PartHeader::BLOCK_SIZE + part.tmd.get_size());
357357
//let mut buf = vec![0u8; PartHeader::BLOCK_SIZE + part.tmd.get_size()];
358358
buf.extend_from_slice(&<[u8; PartHeader::BLOCK_SIZE]>::from(&part.header));
359359
buf.extend(std::iter::repeat(0).take(part.tmd.get_size() + h3_padding));
360360
buf.extend(hashes.iter().flatten());
361361
buf.extend(
362-
std::iter::repeat(0).take(consts::WII_H3_SIZE - hashes.len() * consts::WII_HASH_SIZE),
362+
std::iter::repeat(0).take((consts::WII_H3_SIZE - hashes.len() as u64 * consts::WII_HASH_SIZE as u64) as usize),
363363
);
364364
TitleMetaData::set_partition(&mut buf, PartHeader::BLOCK_SIZE, &part.tmd);
365365
buf
@@ -393,8 +393,8 @@ where
393393
// The "virtual" start and end, in the sense that they are the positions within the decrypted partition.
394394
let vstart = status.cursor;
395395
let vend = vstart + buf.len() as u64;
396-
let start_blk_idx = (vstart / consts::WII_SECTOR_DATA_SIZE as u64) as usize;
397-
let end_blk_idx = ((vend - 1) / consts::WII_SECTOR_DATA_SIZE as u64) as usize;
396+
let start_blk_idx = vstart / consts::WII_SECTOR_DATA_SIZE;
397+
let end_blk_idx = (vend - 1) / consts::WII_SECTOR_DATA_SIZE;
398398
let start_group_idx = start_blk_idx / 64;
399399
let start_block_idx_in_group = start_blk_idx % 64;
400400
let end_group_idx = end_blk_idx / 64;
@@ -445,12 +445,12 @@ where
445445
for i in start_blk..=end_blk {
446446
// Offsets in the group buffer (decrypted address)
447447
let buffer_start =
448-
std::cmp::max(cursor, (i as u64) * consts::WII_SECTOR_DATA_SIZE as u64)
449-
% consts::WII_SECTOR_DATA_SIZE as u64;
448+
std::cmp::max(cursor, i * consts::WII_SECTOR_DATA_SIZE)
449+
% consts::WII_SECTOR_DATA_SIZE;
450450
let buffer_end = std::cmp::min(
451451
(cursor + in_buf.len() as u64) - 1,
452-
(i as u64 + 1) * consts::WII_SECTOR_DATA_SIZE as u64 - 1,
453-
) % consts::WII_SECTOR_DATA_SIZE as u64
452+
(i + 1) * consts::WII_SECTOR_DATA_SIZE - 1,
453+
) % consts::WII_SECTOR_DATA_SIZE
454454
+ 1;
455455
let size = (buffer_end - buffer_start) as usize;
456456
assert!(size <= 0x7C00);
@@ -459,30 +459,30 @@ where
459459
i,
460460
buffer_start,
461461
buffer_end,
462-
curr_buf.len() - (buffer_end - buffer_start) as usize,
462+
curr_buf.len() as u64 - (buffer_end - buffer_start),
463463
);
464464
let data;
465465
(data, curr_buf) = curr_buf.split_at(size);
466-
status.group.sub_groups[(i / 8) % 8].sectors[i % 8].data
466+
status.group.sub_groups[((i / 8) % 8) as usize].sectors[(i % 8) as usize].data
467467
[buffer_start as usize..buffer_end as usize]
468468
.copy_from_slice(data);
469-
if buffer_end == consts::WII_SECTOR_DATA_SIZE as u64 * 64 {
469+
if buffer_end == consts::WII_SECTOR_DATA_SIZE * 64 {
470470
crate::trace!("Reached end of group #{}", group_idx);
471471
}
472472
}
473473
if (status.cursor + (buf.len() - curr_buf.len()) as u64)
474-
% (consts::WII_SECTOR_DATA_SIZE as u64 * 64)
474+
% (consts::WII_SECTOR_DATA_SIZE * 64)
475475
== 0
476476
{
477477
// We are at the start of a group. We can hash and encrypt the group and write it.
478478
crate::trace!("Hashing and encrypting group #{}", group_idx);
479-
if status.hashes.len() <= group_idx {
479+
if status.hashes.len() as u64 <= group_idx {
480480
status
481481
.hashes
482-
.resize(group_idx + 1, [0u8; consts::WII_HASH_SIZE]);
482+
.resize(group_idx as usize + 1, [0u8; consts::WII_HASH_SIZE]);
483483
}
484484
let group_hash = hash_group(&mut status.group);
485-
status.hashes[group_idx].copy_from_slice(&group_hash);
485+
status.hashes[group_idx as usize].copy_from_slice(&group_hash);
486486
let part_key =
487487
decrypt_title_key(&status.disc.partitions.partitions[part_idx].header.ticket);
488488
if !status.disc.disc_header.disable_disc_encrypt {
@@ -528,11 +528,11 @@ where
528528
cx.waker().wake_by_ref();
529529
Poll::Pending
530530
} else {
531-
if cursor % (consts::WII_SECTOR_DATA_SIZE as u64 * 64) == 0 {
532-
cursor += consts::WII_SECTOR_DATA_SIZE as u64 * 64;
531+
if cursor % (consts::WII_SECTOR_DATA_SIZE * 64) == 0 {
532+
cursor += consts::WII_SECTOR_DATA_SIZE * 64;
533533
} else {
534-
cursor = (cursor / (consts::WII_SECTOR_DATA_SIZE as u64 * 64) + 1)
535-
* consts::WII_SECTOR_DATA_SIZE as u64
534+
cursor = (cursor / (consts::WII_SECTOR_DATA_SIZE * 64) + 1)
535+
* consts::WII_SECTOR_DATA_SIZE
536536
* 64;
537537
}
538538
if curr_buf.is_empty() {
@@ -614,7 +614,7 @@ where
614614
encrypt_group(&mut status.group, part_key);
615615
}
616616

617-
status.state = if status.cursor % (consts::WII_SECTOR_DATA_SIZE as u64 * 64) != 0 {
617+
status.state = if status.cursor % (consts::WII_SECTOR_DATA_SIZE * 64) != 0 {
618618
WiiDiscWriterState::SeekToLastGroup(n_group - 1, status.group.to_vec())
619619
} else {
620620
let hashes = status.hashes.clone();

0 commit comments

Comments
 (0)