2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
#include "DiscIO/VolumeWii.h"
|
2017-03-03 12:43:52 -07:00
|
|
|
|
2015-04-21 03:24:13 -06:00
|
|
|
#include <algorithm>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2015-04-09 09:44:53 -06:00
|
|
|
#include <map>
|
2016-06-24 02:43:46 -06:00
|
|
|
#include <mbedtls/aes.h>
|
|
|
|
#include <mbedtls/sha1.h>
|
2015-08-31 17:27:18 -06:00
|
|
|
#include <memory>
|
2017-06-04 02:33:14 -06:00
|
|
|
#include <optional>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <string>
|
2015-08-31 17:27:18 -06:00
|
|
|
#include <utility>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <vector>
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-10-30 16:39:12 -06:00
|
|
|
#include "Common/Assert.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-09-18 22:17:41 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2016-06-24 02:43:46 -06:00
|
|
|
#include "Common/MsgHandler.h"
|
2017-03-03 12:43:52 -07:00
|
|
|
#include "Common/Swap.h"
|
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "DiscIO/Blob.h"
|
2016-07-06 12:33:05 -06:00
|
|
|
#include "DiscIO/Enums.h"
|
2015-04-10 15:18:41 -06:00
|
|
|
#include "DiscIO/Filesystem.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "DiscIO/Volume.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2015-04-21 03:33:51 -06:00
|
|
|
constexpr u64 PARTITION_DATA_OFFSET = 0x20000;
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
VolumeWii::VolumeWii(std::unique_ptr<BlobReader> reader)
|
2015-06-13 04:51:24 -06:00
|
|
|
: m_pReader(std::move(reader)), m_game_partition(PARTITION_NONE), m_last_decrypted_block(-1)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-10-30 16:39:12 -06:00
|
|
|
_assert_(m_pReader);
|
|
|
|
|
Reimplement support for unencrypted Wii discs
You may want to read the PR #2047 comments before reading this.
Dolphin attempts to support an unencrypted type of Wii discs
that apparently is identified by a 4-byte integer at 0x60
being non-zero. I don't know what discs (if any) would be
using that format, so I haven't been able to test Dolphin's
support for it, but it has probably been broken for a while.
The old implementation is very short but also strange.
In CreateVolumeFromFilename, we read a 4-byte integer from
0x60, and if it's non-zero, we create a CVolumeGC object
instead of a CVolumeWiiCrypted object. This might seem like
it makes no sense, but it presumably worked in the past
because IsVolumeWiiDisc used to check the volume type by
reading the magic word for Wii straight from the disc,
meaning that CVolumeGC objects representing unencrypted Wii
discs would be treated as Wii discs by pretty much all of
Dolphin's code except for the volume implementation code.
(It wasn't possible to simply use CVolumeWiiCrypted, because
that class only handled encrypted discs, like the name says.)
However, that stopped working as intended because of ace0607.
And furthermore, bb93336 made it even more broken by making
parts of Dolphin expect that data read from Wii discs needed
to be decrypted (rather than the volume implementation
implicitly deciding whether to decrypt when Read was called).
Disclaimer: Like I said before, I haven't been able to test
any of this because I don't have any discs that use this
unencrypted Wii disc format, so this is all theoretical.
Later, PR #2047 tried to remove Dolphin's support for
the unencrypted Wii disc format because seemingly no
discs used it, but the PR got closed without being merged.
At the end of that PR, I said that I would make a new PR
with a better implementation for the format after PR #2353
was merged. Now that PR #2353 is merged (two years later...)
and PR #5521 is merged, the new implementation was easy to
make, and here it is!
Untested.
2017-06-05 06:54:37 -06:00
|
|
|
if (m_pReader->ReadSwapped<u32>(0x60) != u32(0))
|
|
|
|
{
|
|
|
|
// No partitions - just read unencrypted data like with a GC disc
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-20 10:33:36 -06:00
|
|
|
// Get tickets, TMDs, and decryption keys for all partitions
|
2015-06-13 04:51:24 -06:00
|
|
|
for (u32 partition_group = 0; partition_group < 4; ++partition_group)
|
|
|
|
{
|
2017-06-04 02:33:14 -06:00
|
|
|
const std::optional<u32> number_of_partitions =
|
|
|
|
m_pReader->ReadSwapped<u32>(0x40000 + (partition_group * 8));
|
|
|
|
if (!number_of_partitions)
|
2015-06-13 04:51:24 -06:00
|
|
|
continue;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::optional<u32> read_buffer =
|
|
|
|
m_pReader->ReadSwapped<u32>(0x40000 + (partition_group * 8) + 4);
|
2017-06-04 02:33:14 -06:00
|
|
|
if (!read_buffer)
|
2015-06-13 04:51:24 -06:00
|
|
|
continue;
|
2017-06-04 02:33:14 -06:00
|
|
|
const u64 partition_table_offset = static_cast<u64>(*read_buffer) << 2;
|
2014-05-29 04:38:39 -06:00
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
for (u32 i = 0; i < number_of_partitions; i++)
|
|
|
|
{
|
2017-05-20 10:33:36 -06:00
|
|
|
// Read the partition offset
|
2017-06-04 02:33:14 -06:00
|
|
|
read_buffer = m_pReader->ReadSwapped<u32>(partition_table_offset + (i * 8));
|
|
|
|
if (!read_buffer)
|
2015-06-13 04:51:24 -06:00
|
|
|
continue;
|
2017-06-04 02:33:14 -06:00
|
|
|
const u64 partition_offset = static_cast<u64>(*read_buffer) << 2;
|
2015-06-13 04:51:24 -06:00
|
|
|
|
2017-05-20 10:33:36 -06:00
|
|
|
// Set m_game_partition if this is the game partition
|
2015-06-13 04:51:24 -06:00
|
|
|
if (m_game_partition == PARTITION_NONE)
|
|
|
|
{
|
2017-06-04 02:33:14 -06:00
|
|
|
const std::optional<u32> partition_type =
|
|
|
|
m_pReader->ReadSwapped<u32>(partition_table_offset + (i * 8) + 4);
|
|
|
|
if (partition_type == u32(0))
|
2015-06-13 04:51:24 -06:00
|
|
|
m_game_partition = Partition(partition_offset);
|
|
|
|
}
|
|
|
|
|
2017-05-20 10:33:36 -06:00
|
|
|
// Read ticket
|
|
|
|
std::vector<u8> ticket_buffer(sizeof(IOS::ES::Ticket));
|
|
|
|
if (!m_pReader->Read(partition_offset, ticket_buffer.size(), ticket_buffer.data()))
|
|
|
|
continue;
|
|
|
|
IOS::ES::TicketReader ticket{std::move(ticket_buffer)};
|
2017-05-20 11:41:54 -06:00
|
|
|
if (!ticket.IsValid())
|
|
|
|
continue;
|
2017-05-20 10:33:36 -06:00
|
|
|
|
|
|
|
// Read TMD
|
2017-06-04 02:33:14 -06:00
|
|
|
const std::optional<u32> tmd_size = m_pReader->ReadSwapped<u32>(partition_offset + 0x2a4);
|
|
|
|
std::optional<u32> tmd_address = m_pReader->ReadSwapped<u32>(partition_offset + 0x2a8);
|
|
|
|
if (!tmd_size || !tmd_address)
|
2017-05-20 10:33:36 -06:00
|
|
|
continue;
|
2017-06-04 02:33:14 -06:00
|
|
|
*tmd_address <<= 2;
|
|
|
|
if (!IOS::ES::IsValidTMDSize(*tmd_size))
|
2017-05-20 10:33:36 -06:00
|
|
|
{
|
2017-05-26 03:23:11 -06:00
|
|
|
// 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");
|
2017-05-20 10:33:36 -06:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-04 02:33:14 -06:00
|
|
|
std::vector<u8> tmd_buffer(*tmd_size);
|
|
|
|
if (!m_pReader->Read(partition_offset + *tmd_address, *tmd_size, tmd_buffer.data()))
|
2017-05-20 10:33:36 -06:00
|
|
|
continue;
|
|
|
|
IOS::ES::TMDReader tmd{std::move(tmd_buffer)};
|
|
|
|
|
2017-05-20 11:41:54 -06:00
|
|
|
// Get the decryption key
|
|
|
|
const std::vector<u8> key = ticket.GetTitleKey();
|
|
|
|
if (key.size() != 16)
|
2015-06-13 04:51:24 -06:00
|
|
|
continue;
|
2017-05-20 11:41:54 -06:00
|
|
|
std::unique_ptr<mbedtls_aes_context> aes_context = std::make_unique<mbedtls_aes_context>();
|
|
|
|
mbedtls_aes_setkey_dec(aes_context.get(), key.data(), 128);
|
2017-05-20 10:33:36 -06:00
|
|
|
|
|
|
|
// We've read everything. Time to store it! (The reason we don't store anything
|
|
|
|
// earlier is because we want to be able to skip adding the partition if an error occurs.)
|
|
|
|
const Partition partition(partition_offset);
|
2017-05-20 11:41:54 -06:00
|
|
|
m_partition_keys[partition] = std::move(aes_context);
|
2017-05-20 10:33:36 -06:00
|
|
|
m_partition_tickets[partition] = std::move(ticket);
|
|
|
|
m_partition_tmds[partition] = std::move(tmd);
|
2015-06-13 04:51:24 -06:00
|
|
|
}
|
|
|
|
}
|
2014-05-29 04:38:39 -06:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
VolumeWii::~VolumeWii()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
bool VolumeWii::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, const Partition& partition) const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2015-06-13 04:51:24 -06:00
|
|
|
if (partition == PARTITION_NONE)
|
2016-06-24 02:43:46 -06:00
|
|
|
return m_pReader->Read(_ReadOffset, _Length, _pBuffer);
|
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
// Get the decryption key for the partition
|
2017-05-20 10:33:36 -06:00
|
|
|
auto it = m_partition_keys.find(partition);
|
|
|
|
if (it == m_partition_keys.end())
|
2015-06-13 04:51:24 -06:00
|
|
|
return false;
|
|
|
|
mbedtls_aes_context* aes_context = it->second.get();
|
|
|
|
|
2017-02-10 08:56:40 -07:00
|
|
|
std::vector<u8> read_buffer(BLOCK_TOTAL_SIZE);
|
2016-06-24 02:43:46 -06:00
|
|
|
while (_Length > 0)
|
|
|
|
{
|
2015-04-21 03:17:39 -06:00
|
|
|
// Calculate offsets
|
|
|
|
u64 block_offset_on_disc =
|
2015-06-13 04:51:24 -06:00
|
|
|
partition.offset + PARTITION_DATA_OFFSET + _ReadOffset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE;
|
2015-04-21 03:17:39 -06:00
|
|
|
u64 data_offset_in_block = _ReadOffset % BLOCK_DATA_SIZE;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-04-21 03:17:39 -06:00
|
|
|
if (m_last_decrypted_block != block_offset_on_disc)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
// Read the current block
|
2015-04-21 03:17:39 -06:00
|
|
|
if (!m_pReader->Read(block_offset_on_disc, BLOCK_TOTAL_SIZE, read_buffer.data()))
|
2016-06-24 02:43:46 -06:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Decrypt the block's data.
|
2015-04-21 03:17:39 -06:00
|
|
|
// 0x3D0 - 0x3DF in read_buffer will be overwritten,
|
2016-06-24 02:43:46 -06:00
|
|
|
// but that won't affect anything, because we won't
|
2015-04-21 03:17:39 -06:00
|
|
|
// use the content of read_buffer anymore after this
|
2015-06-13 04:51:24 -06:00
|
|
|
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);
|
2015-04-21 03:17:39 -06:00
|
|
|
m_last_decrypted_block = block_offset_on_disc;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// 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
|
2015-04-21 03:24:13 -06:00
|
|
|
u64 copy_size = std::min(_Length, BLOCK_DATA_SIZE - data_offset_in_block);
|
|
|
|
memcpy(_pBuffer, &m_last_decrypted_block_data[data_offset_in_block],
|
|
|
|
static_cast<size_t>(copy_size));
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// Update offsets
|
2015-04-21 03:24:13 -06:00
|
|
|
_Length -= copy_size;
|
|
|
|
_pBuffer += copy_size;
|
|
|
|
_ReadOffset += copy_size;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::vector<Partition> VolumeWii::GetPartitions() const
|
2010-01-14 00:19:10 -07:00
|
|
|
{
|
2015-06-13 04:51:24 -06:00
|
|
|
std::vector<Partition> partitions;
|
2017-05-20 10:33:36 -06:00
|
|
|
for (const auto& pair : m_partition_keys)
|
2015-06-13 04:51:24 -06:00
|
|
|
partitions.push_back(pair.first);
|
|
|
|
return partitions;
|
2010-01-14 00:19:10 -07:00
|
|
|
}
|
2014-12-03 02:54:09 -07:00
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
Partition VolumeWii::GetGamePartition() const
|
2015-06-13 04:51:24 -06:00
|
|
|
{
|
|
|
|
return m_game_partition;
|
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::optional<u64> VolumeWii::GetTitleID(const Partition& partition) const
|
2015-06-13 04:51:24 -06:00
|
|
|
{
|
2017-05-20 10:58:12 -06:00
|
|
|
const IOS::ES::TicketReader& ticket = GetTicket(partition);
|
|
|
|
if (!ticket.IsValid())
|
2017-06-03 13:29:08 -06:00
|
|
|
return {};
|
|
|
|
return ticket.GetTitleId();
|
2015-06-13 04:51:24 -06:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
const IOS::ES::TicketReader& VolumeWii::GetTicket(const Partition& partition) const
|
2017-02-26 09:26:08 -07:00
|
|
|
{
|
2017-05-20 10:33:36 -06:00
|
|
|
auto it = m_partition_tickets.find(partition);
|
|
|
|
return it != m_partition_tickets.end() ? it->second : INVALID_TICKET;
|
2017-02-26 09:26:08 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
const IOS::ES::TMDReader& VolumeWii::GetTMD(const Partition& partition) const
|
2010-09-07 18:20:19 -06:00
|
|
|
{
|
2017-05-20 10:33:36 -06:00
|
|
|
auto it = m_partition_tmds.find(partition);
|
|
|
|
return it != m_partition_tmds.end() ? it->second : INVALID_TMD;
|
2010-09-07 18:20:19 -06:00
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
u64 VolumeWii::PartitionOffsetToRawOffset(u64 offset, const Partition& partition)
|
2017-02-10 10:57:58 -07:00
|
|
|
{
|
2015-06-13 04:51:24 -06:00
|
|
|
if (partition == PARTITION_NONE)
|
|
|
|
return offset;
|
|
|
|
|
|
|
|
return partition.offset + PARTITION_DATA_OFFSET + (offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE) +
|
2017-02-10 10:57:58 -07:00
|
|
|
(offset % BLOCK_DATA_SIZE);
|
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::string VolumeWii::GetGameID(const Partition& partition) const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
char ID[6];
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
if (!Read(0, 6, (u8*)ID, partition))
|
2016-06-24 02:43:46 -06:00
|
|
|
return std::string();
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return DecodeString(ID);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
Region VolumeWii::GetRegion() const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2017-06-04 02:33:14 -06:00
|
|
|
const std::optional<u32> region_code = m_pReader->ReadSwapped<u32>(0x4E000);
|
|
|
|
return region_code ? static_cast<Region>(*region_code) : Region::UNKNOWN_REGION;
|
2016-12-23 10:41:21 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
Country VolumeWii::GetCountry(const Partition& partition) const
|
2016-12-23 10:41:21 -07:00
|
|
|
{
|
2017-06-04 02:33:14 -06:00
|
|
|
// The 0 that we use as a default value is mapped to COUNTRY_UNKNOWN and UNKNOWN_REGION
|
|
|
|
u8 country_byte = ReadSwapped<u8>(3, partition).value_or(0);
|
2016-12-23 10:41:21 -07:00
|
|
|
const Region region = GetRegion();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-17 14:16:59 -06:00
|
|
|
if (RegionSwitchWii(country_byte) != region)
|
|
|
|
return TypicalCountryForRegion(region);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-17 14:16:59 -06:00
|
|
|
return CountrySwitch(country_byte);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::string VolumeWii::GetMakerID(const Partition& partition) const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
char makerID[2];
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
if (!Read(0x4, 0x2, (u8*)&makerID, partition))
|
2016-06-24 02:43:46 -06:00
|
|
|
return std::string();
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return DecodeString(makerID);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::optional<u16> VolumeWii::GetRevision(const Partition& partition) const
|
2014-11-14 18:59:39 -07:00
|
|
|
{
|
2017-06-04 02:33:14 -06:00
|
|
|
std::optional<u8> revision = ReadSwapped<u8>(7, partition);
|
|
|
|
return revision ? *revision : std::optional<u16>();
|
2014-11-14 18:59:39 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::string VolumeWii::GetInternalName(const Partition& partition) const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
char name_buffer[0x60];
|
2015-06-13 04:51:24 -06:00
|
|
|
if (Read(0x20, 0x60, (u8*)&name_buffer, partition))
|
2016-06-24 02:43:46 -06:00
|
|
|
return DecodeString(name_buffer);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return "";
|
2015-04-10 14:10:49 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::map<Language, std::string> VolumeWii::GetLongNames() const
|
2015-04-10 14:10:49 -06:00
|
|
|
{
|
2017-06-06 03:49:01 -06:00
|
|
|
std::unique_ptr<FileSystem> file_system(CreateFileSystem(this, GetGamePartition()));
|
2015-06-03 05:19:28 -06:00
|
|
|
if (!file_system)
|
2017-06-03 10:14:22 -06:00
|
|
|
return {};
|
2015-06-03 05:19:28 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
std::vector<u8> opening_bnr(NAMES_TOTAL_BYTES);
|
2016-07-06 12:33:05 -06:00
|
|
|
size_t size = file_system->ReadFile("opening.bnr", opening_bnr.data(), opening_bnr.size(), 0x5C);
|
|
|
|
opening_bnr.resize(size);
|
2016-06-24 02:43:46 -06:00
|
|
|
return ReadWiiNames(opening_bnr);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::vector<u32> VolumeWii::GetBanner(int* width, int* height) const
|
2015-12-03 09:29:59 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
*width = 0;
|
|
|
|
*height = 0;
|
2015-12-03 09:29:59 -07:00
|
|
|
|
2017-06-03 13:29:08 -06:00
|
|
|
const std::optional<u64> title_id = GetTitleID(GetGamePartition());
|
|
|
|
if (!title_id)
|
2016-06-24 02:43:46 -06:00
|
|
|
return std::vector<u32>();
|
2015-12-03 09:29:59 -07:00
|
|
|
|
2017-06-03 13:29:08 -06:00
|
|
|
return GetWiiBanner(width, height, *title_id);
|
2015-12-03 09:29:59 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::string VolumeWii::GetApploaderDate(const Partition& partition) const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
char date[16];
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2015-06-13 04:51:24 -06:00
|
|
|
if (!Read(0x2440, 0x10, (u8*)&date, partition))
|
2016-06-24 02:43:46 -06:00
|
|
|
return std::string();
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return DecodeString(date);
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
Platform VolumeWii::GetVolumeType() const
|
2015-01-17 05:21:02 -07:00
|
|
|
{
|
2016-07-06 12:33:05 -06:00
|
|
|
return Platform::WII_DISC;
|
2015-01-17 05:21:02 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::optional<u8> VolumeWii::GetDiscNumber(const Partition& partition) const
|
2015-02-26 15:10:18 -07:00
|
|
|
{
|
2017-06-04 02:33:14 -06:00
|
|
|
return ReadSwapped<u8>(6, partition);
|
2015-02-26 15:10:18 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
BlobType VolumeWii::GetBlobType() const
|
2015-09-26 07:24:29 -06:00
|
|
|
{
|
2016-10-30 16:39:12 -06:00
|
|
|
return m_pReader->GetBlobType();
|
2015-09-26 07:24:29 -06:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
u64 VolumeWii::GetSize() const
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2016-10-30 16:39:12 -06:00
|
|
|
return m_pReader->GetDataSize();
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2009-02-22 06:59:06 -07:00
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
u64 VolumeWii::GetRawSize() const
|
2013-04-09 11:58:56 -06:00
|
|
|
{
|
2016-10-30 16:39:12 -06:00
|
|
|
return m_pReader->GetRawSize();
|
2013-04-09 11:58:56 -06:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
bool VolumeWii::CheckIntegrity(const Partition& partition) const
|
2012-05-04 04:49:10 -06:00
|
|
|
{
|
2015-06-13 04:51:24 -06:00
|
|
|
// Get the decryption key for the partition
|
2017-05-20 10:33:36 -06:00
|
|
|
auto it = m_partition_keys.find(partition);
|
|
|
|
if (it == m_partition_keys.end())
|
2015-06-13 04:51:24 -06:00
|
|
|
return false;
|
|
|
|
mbedtls_aes_context* aes_context = it->second.get();
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// Get partition data size
|
|
|
|
u32 partSizeDiv4;
|
2017-05-19 13:31:47 -06:00
|
|
|
m_pReader->Read(partition.offset + 0x2BC, 4, (u8*)&partSizeDiv4);
|
2016-06-24 02:43:46 -06:00
|
|
|
u64 partDataSize = (u64)Common::swap32(partSizeDiv4) * 4;
|
|
|
|
|
|
|
|
u32 nClusters = (u32)(partDataSize / 0x8000);
|
|
|
|
for (u32 clusterID = 0; clusterID < nClusters; ++clusterID)
|
|
|
|
{
|
2015-06-13 04:51:24 -06:00
|
|
|
u64 clusterOff = partition.offset + PARTITION_DATA_OFFSET + (u64)clusterID * 0x8000;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// Read and decrypt the cluster metadata
|
|
|
|
u8 clusterMDCrypted[0x400];
|
|
|
|
u8 clusterMD[0x400];
|
|
|
|
u8 IV[16] = {0};
|
2017-05-19 13:31:47 -06:00
|
|
|
if (!m_pReader->Read(clusterOff, 0x400, clusterMDCrypted))
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-09-24 17:06:47 -06:00
|
|
|
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read metadata", clusterID);
|
2016-06-24 02:43:46 -06:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-13 04:51:24 -06:00
|
|
|
mbedtls_aes_crypt_cbc(aes_context, MBEDTLS_AES_DECRYPT, 0x400, IV, clusterMDCrypted, clusterMD);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
bool meaningless = false;
|
|
|
|
for (u32 idx = 0x26C; idx < 0x280; ++idx)
|
|
|
|
if (clusterMD[idx] != 0)
|
|
|
|
meaningless = true;
|
|
|
|
|
|
|
|
if (meaningless)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
u8 clusterData[0x7C00];
|
2015-06-13 04:51:24 -06:00
|
|
|
if (!Read((u64)clusterID * 0x7C00, 0x7C00, clusterData, partition))
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-09-24 17:06:47 -06:00
|
|
|
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read data", clusterID);
|
2016-06-24 02:43:46 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u32 hashID = 0; hashID < 31; ++hashID)
|
|
|
|
{
|
|
|
|
u8 hash[20];
|
|
|
|
|
|
|
|
mbedtls_sha1(clusterData + hashID * 0x400, 0x400, hash);
|
|
|
|
|
|
|
|
// Note that we do not use strncmp here
|
|
|
|
if (memcmp(hash, clusterMD + hashID * 20, 20))
|
|
|
|
{
|
2016-09-24 17:06:47 -06:00
|
|
|
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: hash %d is invalid", clusterID,
|
|
|
|
hashID);
|
2016-06-24 02:43:46 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-05-04 04:49:10 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
} // namespace
|