2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2009-07-03 22:34:51 +00:00
|
|
|
|
2019-07-14 13:01:07 +00:00
|
|
|
#include <algorithm>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <cstddef>
|
2015-09-28 15:57:16 +00:00
|
|
|
#include <cstring>
|
2015-04-09 15:44:53 +00:00
|
|
|
#include <map>
|
2015-08-31 23:27:18 +00:00
|
|
|
#include <memory>
|
2017-06-04 08:33:14 +00:00
|
|
|
#include <optional>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <string>
|
2015-08-31 23:27:18 +00:00
|
|
|
#include <utility>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <vector>
|
2009-07-03 22:34:51 +00:00
|
|
|
|
2019-07-14 13:01:07 +00:00
|
|
|
#include <mbedtls/aes.h>
|
|
|
|
#include <mbedtls/sha1.h>
|
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
#include "Common/Align.h"
|
2016-10-30 22:39:12 +00:00
|
|
|
#include "Common/Assert.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2019-07-14 13:01:07 +00:00
|
|
|
#include "Core/IOS/IOSC.h"
|
2014-02-21 00:47:53 +00:00
|
|
|
#include "DiscIO/Blob.h"
|
2016-07-06 18:33:05 +00:00
|
|
|
#include "DiscIO/Enums.h"
|
2014-02-21 00:47:53 +00:00
|
|
|
#include "DiscIO/Volume.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DiscIO/VolumeWad.h"
|
2017-11-02 16:05:45 +00:00
|
|
|
#include "DiscIO/WiiSaveBanner.h"
|
2009-07-03 22:34:51 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2017-06-06 09:49:01 +00:00
|
|
|
VolumeWAD::VolumeWAD(std::unique_ptr<BlobReader> reader) : m_reader(std::move(reader))
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2018-03-15 00:34:35 +00:00
|
|
|
ASSERT(m_reader);
|
2016-10-30 22:39:12 +00:00
|
|
|
|
2015-02-28 22:24:02 +00:00
|
|
|
// Source: http://wiibrew.org/wiki/WAD_files
|
2017-06-04 08:33:14 +00:00
|
|
|
m_hdr_size = m_reader->ReadSwapped<u32>(0x00).value_or(0);
|
2019-03-21 22:04:44 +00:00
|
|
|
m_cert_chain_size = m_reader->ReadSwapped<u32>(0x08).value_or(0);
|
2019-03-23 12:05:42 +00:00
|
|
|
m_ticket_size = m_reader->ReadSwapped<u32>(0x10).value_or(0);
|
2017-06-04 08:33:14 +00:00
|
|
|
m_tmd_size = m_reader->ReadSwapped<u32>(0x14).value_or(0);
|
|
|
|
m_data_size = m_reader->ReadSwapped<u32>(0x18).value_or(0);
|
2017-01-23 17:33:49 +00:00
|
|
|
|
2019-03-21 22:04:44 +00:00
|
|
|
m_cert_chain_offset = Common::AlignUp(m_hdr_size, 0x40);
|
|
|
|
m_ticket_offset = m_cert_chain_offset + Common::AlignUp(m_cert_chain_size, 0x40);
|
|
|
|
m_tmd_offset = m_ticket_offset + Common::AlignUp(m_ticket_size, 0x40);
|
2019-03-30 16:42:31 +00:00
|
|
|
m_data_offset = m_tmd_offset + Common::AlignUp(m_tmd_size, 0x40);
|
|
|
|
m_opening_bnr_offset = m_data_offset + Common::AlignUp(m_data_size, 0x40);
|
2017-02-11 07:57:47 +00:00
|
|
|
|
2019-03-23 12:05:42 +00:00
|
|
|
std::vector<u8> ticket_buffer(m_ticket_size);
|
|
|
|
Read(m_ticket_offset, m_ticket_size, ticket_buffer.data());
|
|
|
|
m_ticket.SetBytes(std::move(ticket_buffer));
|
|
|
|
|
2017-05-26 09:23:11 +00:00
|
|
|
if (!IOS::ES::IsValidTMDSize(m_tmd_size))
|
2017-02-11 07:57:47 +00:00
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "TMD is too large: %u bytes", m_tmd_size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> tmd_buffer(m_tmd_size);
|
2015-06-13 10:51:24 +00:00
|
|
|
Read(m_tmd_offset, m_tmd_size, tmd_buffer.data());
|
2017-02-11 07:57:47 +00:00
|
|
|
m_tmd.SetBytes(std::move(tmd_buffer));
|
2019-03-21 22:04:44 +00:00
|
|
|
|
|
|
|
m_cert_chain.resize(m_cert_chain_size);
|
|
|
|
Read(m_cert_chain_offset, m_cert_chain_size, m_cert_chain.data());
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
bool VolumeWAD::Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2015-06-13 10:51:24 +00:00
|
|
|
if (partition != PARTITION_NONE)
|
|
|
|
return false;
|
2014-12-28 09:35:48 +00:00
|
|
|
|
2017-01-23 17:06:08 +00:00
|
|
|
return m_reader->Read(offset, length, buffer);
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 16:16:56 +00:00
|
|
|
const FileSystem* VolumeWAD::GetFileSystem(const Partition& partition) const
|
|
|
|
{
|
|
|
|
// TODO: Implement this?
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
Region VolumeWAD::GetRegion() const
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
if (!m_tmd.IsValid())
|
2018-03-31 12:04:13 +00:00
|
|
|
return Region::Unknown;
|
2017-02-11 07:57:47 +00:00
|
|
|
return m_tmd.GetRegion();
|
2016-12-23 17:41:21 +00:00
|
|
|
}
|
2009-07-03 22:34:51 +00:00
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
Country VolumeWAD::GetCountry(const Partition& partition) const
|
2016-12-23 17:41:21 +00:00
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
if (!m_tmd.IsValid())
|
2018-03-31 12:04:13 +00:00
|
|
|
return Country::Unknown;
|
2015-02-24 00:50:19 +00:00
|
|
|
|
2018-10-28 09:33:16 +00:00
|
|
|
const u8 country_byte = static_cast<u8>(m_tmd.GetTitleId() & 0xff);
|
|
|
|
if (country_byte == 2) // SYSMENU
|
2017-03-17 20:16:59 +00:00
|
|
|
return TypicalCountryForRegion(GetSysMenuRegion(m_tmd.GetTitleVersion()));
|
2015-02-24 00:50:19 +00:00
|
|
|
|
2018-10-28 09:33:16 +00:00
|
|
|
const Region region = GetRegion();
|
2019-03-22 19:47:05 +00:00
|
|
|
const std::optional<u16> revision = GetRevision();
|
|
|
|
if (CountryCodeToRegion(country_byte, Platform::WiiWAD, region, revision) != region)
|
2018-10-28 09:33:16 +00:00
|
|
|
return TypicalCountryForRegion(region);
|
|
|
|
|
2019-03-22 19:47:05 +00:00
|
|
|
return CountryCodeToCountry(country_byte, Platform::WiiWAD, region, revision);
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 12:05:42 +00:00
|
|
|
const IOS::ES::TicketReader& VolumeWAD::GetTicket(const Partition& partition) const
|
|
|
|
{
|
|
|
|
return m_ticket;
|
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
const IOS::ES::TMDReader& VolumeWAD::GetTMD(const Partition& partition) const
|
2017-01-23 17:33:49 +00:00
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
return m_tmd;
|
2017-01-23 17:33:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 22:04:44 +00:00
|
|
|
const std::vector<u8>& VolumeWAD::GetCertificateChain(const Partition& partition) const
|
|
|
|
{
|
|
|
|
return m_cert_chain;
|
|
|
|
}
|
|
|
|
|
2019-07-14 16:00:14 +00:00
|
|
|
std::vector<u8> VolumeWAD::GetContent(u16 index) const
|
|
|
|
{
|
|
|
|
u64 offset = m_data_offset;
|
|
|
|
for (const IOS::ES::Content& content : m_tmd.GetContents())
|
|
|
|
{
|
|
|
|
const u64 aligned_size = Common::AlignUp(content.size, 0x40);
|
|
|
|
if (content.index == index)
|
|
|
|
{
|
|
|
|
std::vector<u8> data(aligned_size);
|
|
|
|
if (!m_reader->Read(offset, aligned_size, data.data()))
|
|
|
|
return {};
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
offset += aligned_size;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-03-30 16:42:31 +00:00
|
|
|
std::vector<u64> VolumeWAD::GetContentOffsets() const
|
|
|
|
{
|
|
|
|
const std::vector<IOS::ES::Content> contents = m_tmd.GetContents();
|
|
|
|
std::vector<u64> content_offsets;
|
|
|
|
content_offsets.reserve(contents.size());
|
|
|
|
u64 offset = m_data_offset;
|
|
|
|
for (const IOS::ES::Content& content : contents)
|
|
|
|
{
|
|
|
|
content_offsets.emplace_back(offset);
|
|
|
|
offset += Common::AlignUp(content.size, 0x40);
|
|
|
|
}
|
|
|
|
|
|
|
|
return content_offsets;
|
|
|
|
}
|
|
|
|
|
2019-07-14 13:01:07 +00:00
|
|
|
bool VolumeWAD::CheckContentIntegrity(const IOS::ES::Content& content,
|
|
|
|
const std::vector<u8>& encrypted_data,
|
|
|
|
const IOS::ES::TicketReader& ticket) const
|
|
|
|
{
|
2019-08-05 11:58:27 +00:00
|
|
|
if (encrypted_data.size() != Common::AlignUp(content.size, 0x40))
|
|
|
|
return false;
|
|
|
|
|
2019-07-14 13:01:07 +00:00
|
|
|
mbedtls_aes_context context;
|
|
|
|
const std::array<u8, 16> key = ticket.GetTitleKey();
|
|
|
|
mbedtls_aes_setkey_dec(&context, key.data(), 128);
|
|
|
|
|
|
|
|
std::array<u8, 16> iv{};
|
|
|
|
iv[0] = static_cast<u8>(content.index >> 8);
|
|
|
|
iv[1] = static_cast<u8>(content.index & 0xFF);
|
|
|
|
|
|
|
|
std::vector<u8> decrypted_data(encrypted_data.size());
|
|
|
|
mbedtls_aes_crypt_cbc(&context, MBEDTLS_AES_DECRYPT, decrypted_data.size(), iv.data(),
|
|
|
|
encrypted_data.data(), decrypted_data.data());
|
|
|
|
|
|
|
|
std::array<u8, 20> sha1;
|
|
|
|
mbedtls_sha1_ret(decrypted_data.data(), content.size, sha1.data());
|
|
|
|
return sha1 == content.sha1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VolumeWAD::CheckContentIntegrity(const IOS::ES::Content& content, u64 content_offset,
|
|
|
|
const IOS::ES::TicketReader& ticket) const
|
|
|
|
{
|
|
|
|
std::vector<u8> encrypted_data(Common::AlignUp(content.size, 0x40));
|
|
|
|
if (!m_reader->Read(content_offset, encrypted_data.size(), encrypted_data.data()))
|
|
|
|
return false;
|
|
|
|
return CheckContentIntegrity(content, encrypted_data, ticket);
|
|
|
|
}
|
|
|
|
|
|
|
|
IOS::ES::TicketReader VolumeWAD::GetTicketWithFixedCommonKey() const
|
|
|
|
{
|
|
|
|
if (!m_ticket.IsValid() || !m_tmd.IsValid())
|
|
|
|
return m_ticket;
|
|
|
|
|
|
|
|
const std::vector<u8> sig = m_ticket.GetSignatureData();
|
|
|
|
if (!std::all_of(sig.cbegin(), sig.cend(), [](u8 a) { return a == 0; }))
|
|
|
|
{
|
|
|
|
// This does not look like a typical "invalid common key index" ticket, so let's assume
|
|
|
|
// the index is correct. This saves some time when reading properly signed titles.
|
|
|
|
return m_ticket;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<IOS::ES::Content> contents = m_tmd.GetContents();
|
|
|
|
if (contents.empty())
|
|
|
|
return m_ticket;
|
|
|
|
|
|
|
|
// Find the smallest content so that we spend as little time as possible in CheckContentIntegrity
|
|
|
|
IOS::ES::Content smallest_content = contents[0];
|
|
|
|
u64 offset_of_smallest_content = m_data_offset;
|
|
|
|
|
|
|
|
u64 offset = m_data_offset;
|
|
|
|
for (const IOS::ES::Content& content : contents)
|
|
|
|
{
|
|
|
|
if (content.size < smallest_content.size)
|
|
|
|
{
|
|
|
|
smallest_content = content;
|
|
|
|
offset_of_smallest_content = offset;
|
|
|
|
}
|
|
|
|
offset += Common::AlignUp(content.size, 0x40);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> content_data(Common::AlignUp(smallest_content.size, 0x40));
|
|
|
|
if (!m_reader->Read(offset_of_smallest_content, content_data.size(), content_data.data()))
|
|
|
|
return m_ticket;
|
|
|
|
|
|
|
|
const u8 specified_index = m_ticket.GetCommonKeyIndex();
|
|
|
|
if (specified_index < IOS::HLE::IOSC::COMMON_KEY_HANDLES.size() &&
|
|
|
|
CheckContentIntegrity(smallest_content, content_data, m_ticket))
|
|
|
|
{
|
|
|
|
return m_ticket; // The common key index is already correct
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try every common key index except the one we already tried
|
|
|
|
IOS::ES::TicketReader new_ticket = m_ticket;
|
|
|
|
for (u8 i = 0; i < IOS::HLE::IOSC::COMMON_KEY_HANDLES.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i != specified_index)
|
|
|
|
{
|
|
|
|
new_ticket.OverwriteCommonKeyIndex(i);
|
|
|
|
if (CheckContentIntegrity(smallest_content, content_data, new_ticket))
|
|
|
|
return new_ticket; // We've found the common key index that should be used
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ERROR_LOG(DISCIO, "Couldn't find valid common key for WAD file (%u specified)", specified_index);
|
|
|
|
return m_ticket;
|
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::string VolumeWAD::GetGameID(const Partition& partition) const
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2017-03-16 09:28:17 +00:00
|
|
|
return m_tmd.GetGameID();
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 16:49:06 +00:00
|
|
|
std::string VolumeWAD::GetGameTDBID(const Partition& partition) const
|
|
|
|
{
|
|
|
|
return m_tmd.GetGameTDBID();
|
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::string VolumeWAD::GetMakerID(const Partition& partition) const
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2017-03-16 09:28:17 +00:00
|
|
|
char temp[2];
|
2015-06-13 10:51:24 +00:00
|
|
|
if (!Read(0x198 + m_tmd_offset, 2, (u8*)temp, partition))
|
2017-03-16 09:28:17 +00:00
|
|
|
return "00";
|
|
|
|
|
|
|
|
// Some weird channels use 0x0000 in place of the MakerID, so we need a check here
|
2019-12-30 09:48:11 +00:00
|
|
|
if (!IsPrintableCharacter(temp[0]) || !IsPrintableCharacter(temp[1]))
|
2009-07-03 22:34:51 +00:00
|
|
|
return "00";
|
|
|
|
|
2015-06-18 12:45:26 +00:00
|
|
|
return DecodeString(temp);
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::optional<u64> VolumeWAD::GetTitleID(const Partition& partition) const
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2019-03-23 12:05:42 +00:00
|
|
|
return ReadSwapped<u64>(m_ticket_offset + 0x01DC, partition);
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::optional<u16> VolumeWAD::GetRevision(const Partition& partition) const
|
2015-01-31 01:43:48 +00:00
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
if (!m_tmd.IsValid())
|
2017-06-04 08:33:14 +00:00
|
|
|
return {};
|
2015-01-31 01:43:48 +00:00
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
return m_tmd.GetTitleVersion();
|
2015-01-31 01:43:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
Platform VolumeWAD::GetVolumeType() const
|
2015-01-17 12:21:02 +00:00
|
|
|
{
|
2018-03-31 12:04:13 +00:00
|
|
|
return Platform::WiiWAD;
|
2015-01-17 12:21:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 12:51:13 +00:00
|
|
|
bool VolumeWAD::IsDatelDisc() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::map<Language, std::string> VolumeWAD::GetLongNames() const
|
2011-12-19 05:42:20 +00:00
|
|
|
{
|
2017-03-18 14:01:03 +00:00
|
|
|
if (!m_tmd.IsValid() || !IOS::ES::IsChannel(m_tmd.GetTitleId()))
|
|
|
|
return {};
|
|
|
|
|
2017-11-02 20:05:37 +00:00
|
|
|
std::vector<char16_t> names(NAMES_TOTAL_CHARS);
|
|
|
|
if (!Read(m_opening_bnr_offset + 0x9C, NAMES_TOTAL_BYTES, reinterpret_cast<u8*>(names.data())))
|
2016-07-06 18:33:05 +00:00
|
|
|
return std::map<Language, std::string>();
|
2017-11-02 20:05:37 +00:00
|
|
|
return ReadWiiNames(names);
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 21:41:49 +00:00
|
|
|
std::vector<u32> VolumeWAD::GetBanner(u32* width, u32* height) const
|
2015-12-03 16:29:59 +00:00
|
|
|
{
|
|
|
|
*width = 0;
|
|
|
|
*height = 0;
|
|
|
|
|
2017-06-03 19:29:08 +00:00
|
|
|
const std::optional<u64> title_id = GetTitleID();
|
|
|
|
if (!title_id)
|
2015-12-03 16:29:59 +00:00
|
|
|
return std::vector<u32>();
|
|
|
|
|
2017-11-02 16:05:45 +00:00
|
|
|
return WiiSaveBanner(*title_id).GetBanner(width, height);
|
2015-12-03 16:29:59 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
BlobType VolumeWAD::GetBlobType() const
|
2015-09-26 13:24:29 +00:00
|
|
|
{
|
2016-10-30 22:39:12 +00:00
|
|
|
return m_reader->GetBlobType();
|
2015-09-26 13:24:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
u64 VolumeWAD::GetSize() const
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2016-10-30 22:39:12 +00:00
|
|
|
return m_reader->GetDataSize();
|
2013-04-09 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 21:20:23 +00:00
|
|
|
bool VolumeWAD::IsSizeAccurate() const
|
|
|
|
{
|
|
|
|
return m_reader->IsDataSizeAccurate();
|
|
|
|
}
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
u64 VolumeWAD::GetRawSize() const
|
2013-04-09 17:58:56 +00:00
|
|
|
{
|
2016-10-30 22:39:12 +00:00
|
|
|
return m_reader->GetRawSize();
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 12:11:00 +00:00
|
|
|
const BlobReader& VolumeWAD::GetBlobReader() const
|
|
|
|
{
|
|
|
|
return *m_reader;
|
|
|
|
}
|
|
|
|
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace DiscIO
|