1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "DiscIO/VolumeWii.h"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstring>
#include <map>
#include <mbedtls/aes.h>
#include <mbedtls/sha1.h>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "Common/Assert.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
#include "Common/Swap.h"
#include "DiscIO/Blob.h"
#include "DiscIO/DiscExtractor.h"
#include "DiscIO/Enums.h"
#include "DiscIO/FileSystemGCWii.h"
#include "DiscIO/Filesystem.h"
#include "DiscIO/Volume.h"
#include "DiscIO/WiiSaveBanner.h"
namespace DiscIO
{
VolumeWii::VolumeWii(std::unique_ptr<BlobReader> reader)
: m_reader(std::move(reader)), m_game_partition(PARTITION_NONE),
m_last_decrypted_block(UINT64_MAX)
{
ASSERT(m_reader);
m_encrypted = m_reader->ReadSwapped<u32>(0x60) == u32(0);
for (u32 partition_group = 0; partition_group < 4; ++partition_group)
{
const std::optional<u32> number_of_partitions =
m_reader->ReadSwapped<u32>(0x40000 + (partition_group * 8));
if (!number_of_partitions)
continue;
const std::optional<u64> partition_table_offset =
ReadSwappedAndShifted(0x40000 + (partition_group * 8) + 4, PARTITION_NONE);
if (!partition_table_offset)
continue;
for (u32 i = 0; i < number_of_partitions; i++)
{
const std::optional<u64> partition_offset =
ReadSwappedAndShifted(*partition_table_offset + (i * 8), PARTITION_NONE);
if (!partition_offset)
continue;
const Partition partition(*partition_offset);
const std::optional<u32> partition_type =
m_reader->ReadSwapped<u32>(*partition_table_offset + (i * 8) + 4);
if (!partition_type)
continue;
// If this is the game partition, set m_game_partition
if (m_game_partition == PARTITION_NONE && *partition_type == 0)
m_game_partition = partition;
auto get_ticket = [this, partition]() -> IOS::ES::TicketReader {
std::vector<u8> ticket_buffer(sizeof(IOS::ES::Ticket));
if (!m_reader->Read(partition.offset, ticket_buffer.size(), ticket_buffer.data()))
return INVALID_TICKET;
return IOS::ES::TicketReader{std::move(ticket_buffer)};
};
auto get_tmd = [this, partition]() -> IOS::ES::TMDReader {
const std::optional<u32> tmd_size = m_reader->ReadSwapped<u32>(partition.offset + 0x2a4);
const std::optional<u64> tmd_address =
ReadSwappedAndShifted(partition.offset + 0x2a8, PARTITION_NONE);
if (!tmd_size || !tmd_address)
return INVALID_TMD;
if (!IOS::ES::IsValidTMDSize(*tmd_size))
{
// This check is normally done by ES in ES_DiVerify, but that would happen too late
// (after allocating the buffer), so we do the check here.
PanicAlert("Invalid TMD size");
return INVALID_TMD;
}
std::vector<u8> tmd_buffer(*tmd_size);
if (!m_reader->Read(partition.offset + *tmd_address, *tmd_size, tmd_buffer.data()))
return INVALID_TMD;
return IOS::ES::TMDReader{std::move(tmd_buffer)};
};
auto get_key = [this, partition]() -> std::unique_ptr<mbedtls_aes_context> {
const IOS::ES::TicketReader& ticket = *m_partitions[partition].ticket;
if (!ticket.IsValid())
return nullptr;
const std::array<u8, 16> key = ticket.GetTitleKey();
std::unique_ptr<mbedtls_aes_context> aes_context = std::make_unique<mbedtls_aes_context>();
mbedtls_aes_setkey_dec(aes_context.get(), key.data(), 128);
return aes_context;
};
auto get_file_system = [this, partition]() -> std::unique_ptr<FileSystem> {
auto file_system = std::make_unique<FileSystemGCWii>(this, partition);
return file_system->IsValid() ? std::move(file_system) : nullptr;
};
auto get_data_offset = [this, partition]() -> u64 {
return ReadSwappedAndShifted(partition.offset + 0x2b8, PARTITION_NONE).value_or(0);
};
m_partitions.emplace(
partition, PartitionDetails{Common::Lazy<std::unique_ptr<mbedtls_aes_context>>(get_key),
Common::Lazy<IOS::ES::TicketReader>(get_ticket),
Common::Lazy<IOS::ES::TMDReader>(get_tmd),
Common::Lazy<std::unique_ptr<FileSystem>>(get_file_system),
Common::Lazy<u64>(get_data_offset), *partition_type});
}
}
}
VolumeWii::~VolumeWii()
{
}
bool VolumeWii::Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const
{
if (partition == PARTITION_NONE)
return m_reader->Read(offset, length, buffer);
auto it = m_partitions.find(partition);
if (it == m_partitions.end())
return false;
const PartitionDetails& partition_details = it->second;
if (!m_encrypted)
{
return m_reader->Read(partition.offset + *partition_details.data_offset + offset, length,
buffer);
}
if (m_reader->SupportsReadWiiDecrypted())
return m_reader->ReadWiiDecrypted(offset, length, buffer, partition.offset);
mbedtls_aes_context* aes_context = partition_details.key->get();
if (!aes_context)
return false;
std::vector<u8> read_buffer(BLOCK_TOTAL_SIZE);
while (length > 0)
{
// Calculate offsets
u64 block_offset_on_disc = partition.offset + *partition_details.data_offset +
offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE;
u64 data_offset_in_block = offset % BLOCK_DATA_SIZE;
if (m_last_decrypted_block != block_offset_on_disc)
{
// Read the current block
if (!m_reader->Read(block_offset_on_disc, BLOCK_TOTAL_SIZE, read_buffer.data()))
return false;
// Decrypt the block's data.
// 0x3D0 - 0x3DF in read_buffer will be overwritten,
// but that won't affect anything, because we won't
// use the content of read_buffer anymore after this
mbedtls_aes_crypt_cbc(aes_context, MBEDTLS_AES_DECRYPT, BLOCK_DATA_SIZE, &read_buffer[0x3D0],
&read_buffer[BLOCK_HEADER_SIZE], m_last_decrypted_block_data);
m_last_decrypted_block = block_offset_on_disc;
// The only thing we currently use from the 0x000 - 0x3FF part
// of the block is the IV (at 0x3D0), but it also contains SHA-1
// hashes that IOS uses to check that discs aren't tampered with.
// http://wiibrew.org/wiki/Wii_Disc#Encrypted
}
// Copy the decrypted data
u64 copy_size = std::min(length, BLOCK_DATA_SIZE - data_offset_in_block);
memcpy(buffer, &m_last_decrypted_block_data[data_offset_in_block],
static_cast<size_t>(copy_size));
// Update offsets
length -= copy_size;
buffer += copy_size;
offset += copy_size;
}
return true;
}
bool VolumeWii::IsEncryptedAndHashed() const
{
return m_encrypted;
}
std::vector<Partition> VolumeWii::GetPartitions() const
{
std::vector<Partition> partitions;
for (const auto& pair : m_partitions)
partitions.push_back(pair.first);
return partitions;
}
Partition VolumeWii::GetGamePartition() const
{
return m_game_partition;
}
std::optional<u32> VolumeWii::GetPartitionType(const Partition& partition) const
{
auto it = m_partitions.find(partition);
return it != m_partitions.end() ? it->second.type : std::optional<u32>();
}
std::optional<u64> VolumeWii::GetTitleID(const Partition& partition) const
{
const IOS::ES::TicketReader& ticket = GetTicket(partition);
if (!ticket.IsValid())
return {};
return ticket.GetTitleId();
}
const IOS::ES::TicketReader& VolumeWii::GetTicket(const Partition& partition) const
{
auto it = m_partitions.find(partition);
return it != m_partitions.end() ? *it->second.ticket : INVALID_TICKET;
}
const IOS::ES::TMDReader& VolumeWii::GetTMD(const Partition& partition) const
{
auto it = m_partitions.find(partition);
return it != m_partitions.end() ? *it->second.tmd : INVALID_TMD;
}
const FileSystem* VolumeWii::GetFileSystem(const Partition& partition) const
{
auto it = m_partitions.find(partition);
return it != m_partitions.end() ? it->second.file_system->get() : nullptr;
}
u64 VolumeWii::EncryptedPartitionOffsetToRawOffset(u64 offset, const Partition& partition,
u64 partition_data_offset)
{
if (partition == PARTITION_NONE)
return offset;
return partition.offset + partition_data_offset + (offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE) +
(offset % BLOCK_DATA_SIZE);
}
u64 VolumeWii::PartitionOffsetToRawOffset(u64 offset, const Partition& partition) const
{
auto it = m_partitions.find(partition);
if (it == m_partitions.end())
return offset;
const u64 data_offset = *it->second.data_offset;
if (!m_encrypted)
return partition.offset + data_offset + offset;
return EncryptedPartitionOffsetToRawOffset(offset, partition, data_offset);
}
std::string VolumeWii::GetGameID(const Partition& partition) const
{
char id[6];
if (!Read(0, sizeof(id), reinterpret_cast<u8*>(id), partition))
return std::string();
return DecodeString(id);
}
Region VolumeWii::GetRegion() const
{
const std::optional<u32> region_code = m_reader->ReadSwapped<u32>(0x4E000);
if (!region_code)
return Region::Unknown;
const Region region = static_cast<Region>(*region_code);
return region <= Region::NTSC_K ? region : Region::Unknown;
}
Country VolumeWii::GetCountry(const Partition& partition) const
{
// The 0 that we use as a default value is mapped to Country::Unknown and Region::Unknown
const u8 country_byte = ReadSwapped<u8>(3, partition).value_or(0);
const Region region = GetRegion();
if (CountryCodeToRegion(country_byte, Platform::WiiDisc, region) != region)
return TypicalCountryForRegion(region);
return CountryCodeToCountry(country_byte, Platform::WiiDisc, region);
}
std::string VolumeWii::GetMakerID(const Partition& partition) const
{
char maker_id[2];
if (!Read(0x4, sizeof(maker_id), reinterpret_cast<u8*>(&maker_id), partition))
return std::string();
return DecodeString(maker_id);
}
std::optional<u16> VolumeWii::GetRevision(const Partition& partition) const
{
std::optional<u8> revision = ReadSwapped<u8>(7, partition);
return revision ? *revision : std::optional<u16>();
}
std::string VolumeWii::GetInternalName(const Partition& partition) const
{
char name_buffer[0x60];
if (Read(0x20, sizeof(name_buffer), reinterpret_cast<u8*>(&name_buffer), partition))
return DecodeString(name_buffer);
return "";
}
std::map<Language, std::string> VolumeWii::GetLongNames() const
{
std::vector<char16_t> names(NAMES_TOTAL_CHARS);
names.resize(ReadFile(*this, GetGamePartition(), "opening.bnr",
reinterpret_cast<u8*>(names.data()), NAMES_TOTAL_BYTES, 0x5C));
return ReadWiiNames(names);
}
std::vector<u32> VolumeWii::GetBanner(u32* width, u32* height) const
{
*width = 0;
*height = 0;
const std::optional<u64> title_id = GetTitleID(GetGamePartition());
if (!title_id)
return std::vector<u32>();
return WiiSaveBanner(*title_id).GetBanner(width, height);
}
std::string VolumeWii::GetApploaderDate(const Partition& partition) const
{
char date[16];
if (!Read(0x2440, sizeof(date), reinterpret_cast<u8*>(&date), partition))
return std::string();
return DecodeString(date);
}
Platform VolumeWii::GetVolumeType() const
{
return Platform::WiiDisc;
}
std::optional<u8> VolumeWii::GetDiscNumber(const Partition& partition) const
{
return ReadSwapped<u8>(6, partition);
}
BlobType VolumeWii::GetBlobType() const
{
return m_reader->GetBlobType();
}
u64 VolumeWii::GetSize() const
{
return m_reader->GetDataSize();
}
u64 VolumeWii::GetRawSize() const
{
return m_reader->GetRawSize();
}
bool VolumeWii::CheckIntegrity(const Partition& partition) const
{
if (!m_encrypted)
return false;
// Get the decryption key for the partition
auto it = m_partitions.find(partition);
if (it == m_partitions.end())
return false;
const PartitionDetails& partition_details = it->second;
mbedtls_aes_context* aes_context = partition_details.key->get();
if (!aes_context)
return false;
// Get partition data size
const auto part_data_size = ReadSwappedAndShifted(partition.offset + 0x2BC, PARTITION_NONE);
if (!part_data_size)
return false;
const u32 num_clusters = static_cast<u32>(part_data_size.value() / 0x8000);
for (u32 cluster_id = 0; cluster_id < num_clusters; ++cluster_id)
{
const u64 cluster_offset =
partition.offset + *partition_details.data_offset + static_cast<u64>(cluster_id) * 0x8000;
// Read and decrypt the cluster metadata
u8 cluster_metadata_crypted[0x400];
u8 cluster_metadata[0x400];
u8 iv[16] = {0};
if (!m_reader->Read(cluster_offset, sizeof(cluster_metadata_crypted), cluster_metadata_crypted))
{
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read metadata", cluster_id);
return false;
}
mbedtls_aes_crypt_cbc(aes_context, MBEDTLS_AES_DECRYPT, sizeof(cluster_metadata), iv,
cluster_metadata_crypted, cluster_metadata);
// Some clusters have invalid data and metadata because they aren't
// meant to be read by the game (for example, holes between files). To
// try to avoid reporting errors because of these clusters, we check
// the 0x00 paddings in the metadata.
//
// This may cause some false negatives though: some bad clusters may be
// skipped because they are *too* bad and are not even recognized as
// valid clusters. To be improved.
const u8* pad_begin = cluster_metadata + 0x26C;
const u8* pad_end = pad_begin + 0x14;
const bool meaningless = std::any_of(pad_begin, pad_end, [](u8 val) { return val != 0; });
if (meaningless)
continue;
u8 cluster_data[0x7C00];
if (!Read(cluster_id * sizeof(cluster_data), sizeof(cluster_data), cluster_data, partition))
{
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read data", cluster_id);
return false;
}
for (u32 hash_id = 0; hash_id < 31; ++hash_id)
{
u8 hash[20];
mbedtls_sha1(cluster_data + hash_id * sizeof(cluster_metadata), sizeof(cluster_metadata),
hash);
// Note that we do not use strncmp here
if (memcmp(hash, cluster_metadata + hash_id * sizeof(hash), sizeof(hash)))
{
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: hash %d is invalid", cluster_id,
hash_id);
return false;
}
}
}
return true;
}
} // namespace
|