2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 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.
|
2009-07-03 16:34:51 -06:00
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstddef>
|
2015-09-28 09:57:16 -06:00
|
|
|
#include <cstring>
|
2017-03-16 03:28:17 -06:00
|
|
|
#include <locale>
|
2015-04-09 09:44:53 -06:00
|
|
|
#include <map>
|
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>
|
2009-07-03 16:34:51 -06:00
|
|
|
|
2016-11-27 03:56:22 -07:00
|
|
|
#include "Common/Align.h"
|
2016-10-30 16:39:12 -06:00
|
|
|
#include "Common/Assert.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2016-06-24 02:43:46 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/StringUtil.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"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "DiscIO/Volume.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "DiscIO/VolumeWad.h"
|
2017-11-02 10:05:45 -06:00
|
|
|
#include "DiscIO/WiiSaveBanner.h"
|
2009-07-03 16:34:51 -06:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2017-06-06 03:49:01 -06:00
|
|
|
VolumeWAD::VolumeWAD(std::unique_ptr<BlobReader> reader) : m_reader(std::move(reader))
|
2009-07-03 16:34:51 -06:00
|
|
|
{
|
2018-03-14 18:34:35 -06:00
|
|
|
ASSERT(m_reader);
|
2016-10-30 16:39:12 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// Source: http://wiibrew.org/wiki/WAD_files
|
2017-06-04 02:33:14 -06:00
|
|
|
m_hdr_size = m_reader->ReadSwapped<u32>(0x00).value_or(0);
|
2019-03-21 16:04:44 -06:00
|
|
|
m_cert_chain_size = m_reader->ReadSwapped<u32>(0x08).value_or(0);
|
2019-03-23 06:05:42 -06:00
|
|
|
m_ticket_size = m_reader->ReadSwapped<u32>(0x10).value_or(0);
|
2017-06-04 02:33:14 -06: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 10:33:49 -07:00
|
|
|
|
2019-03-21 16:04:44 -06: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 10:42:31 -06: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 00:57:47 -07:00
|
|
|
|
2019-03-23 06:05:42 -06: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 03:23:11 -06:00
|
|
|
if (!IOS::ES::IsValidTMDSize(m_tmd_size))
|
2017-02-11 00:57:47 -07: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 04:51:24 -06:00
|
|
|
Read(m_tmd_offset, m_tmd_size, tmd_buffer.data());
|
2017-02-11 00:57:47 -07:00
|
|
|
m_tmd.SetBytes(std::move(tmd_buffer));
|
2019-03-21 16:04:44 -06: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 16:34:51 -06:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
VolumeWAD::~VolumeWAD()
|
2009-07-03 16:34:51 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
bool VolumeWAD::Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const
|
2009-07-03 16:34:51 -06:00
|
|
|
{
|
2015-06-13 04:51:24 -06:00
|
|
|
if (partition != PARTITION_NONE)
|
|
|
|
return false;
|
2014-12-28 02:35:48 -07:00
|
|
|
|
2017-01-23 10:06:08 -07:00
|
|
|
return m_reader->Read(offset, length, buffer);
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
|
|
|
|
2017-08-02 10:16:56 -06:00
|
|
|
const FileSystem* VolumeWAD::GetFileSystem(const Partition& partition) const
|
|
|
|
{
|
|
|
|
// TODO: Implement this?
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
Region VolumeWAD::GetRegion() const
|
2009-07-03 16:34:51 -06:00
|
|
|
{
|
2017-02-11 00:57:47 -07:00
|
|
|
if (!m_tmd.IsValid())
|
2018-03-31 06:04:13 -06:00
|
|
|
return Region::Unknown;
|
2017-02-11 00:57:47 -07:00
|
|
|
return m_tmd.GetRegion();
|
2016-12-23 10:41:21 -07:00
|
|
|
}
|
2009-07-03 16:34:51 -06:00
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
Country VolumeWAD::GetCountry(const Partition& partition) const
|
2016-12-23 10:41:21 -07:00
|
|
|
{
|
2017-02-11 00:57:47 -07:00
|
|
|
if (!m_tmd.IsValid())
|
2018-03-31 06:04:13 -06:00
|
|
|
return Country::Unknown;
|
2015-02-23 17:50:19 -07:00
|
|
|
|
2018-10-28 03:33:16 -06:00
|
|
|
const u8 country_byte = static_cast<u8>(m_tmd.GetTitleId() & 0xff);
|
|
|
|
if (country_byte == 2) // SYSMENU
|
2017-03-17 14:16:59 -06:00
|
|
|
return TypicalCountryForRegion(GetSysMenuRegion(m_tmd.GetTitleVersion()));
|
2015-02-23 17:50:19 -07:00
|
|
|
|
2018-10-28 03:33:16 -06:00
|
|
|
const Region region = GetRegion();
|
2019-03-22 13:47:05 -06:00
|
|
|
const std::optional<u16> revision = GetRevision();
|
|
|
|
if (CountryCodeToRegion(country_byte, Platform::WiiWAD, region, revision) != region)
|
2018-10-28 03:33:16 -06:00
|
|
|
return TypicalCountryForRegion(region);
|
|
|
|
|
2019-03-22 13:47:05 -06:00
|
|
|
return CountryCodeToCountry(country_byte, Platform::WiiWAD, region, revision);
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-23 06:05:42 -06:00
|
|
|
const IOS::ES::TicketReader& VolumeWAD::GetTicket(const Partition& partition) const
|
|
|
|
{
|
|
|
|
return m_ticket;
|
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
const IOS::ES::TMDReader& VolumeWAD::GetTMD(const Partition& partition) const
|
2017-01-23 10:33:49 -07:00
|
|
|
{
|
2017-02-11 00:57:47 -07:00
|
|
|
return m_tmd;
|
2017-01-23 10:33:49 -07:00
|
|
|
}
|
|
|
|
|
2019-03-21 16:04:44 -06:00
|
|
|
const std::vector<u8>& VolumeWAD::GetCertificateChain(const Partition& partition) const
|
|
|
|
{
|
|
|
|
return m_cert_chain;
|
|
|
|
}
|
|
|
|
|
2019-03-30 10:42:31 -06: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;
|
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::string VolumeWAD::GetGameID(const Partition& partition) const
|
2009-07-03 16:34:51 -06:00
|
|
|
{
|
2017-03-16 03:28:17 -06:00
|
|
|
return m_tmd.GetGameID();
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
|
|
|
|
2019-02-23 09:49:06 -07:00
|
|
|
std::string VolumeWAD::GetGameTDBID(const Partition& partition) const
|
|
|
|
{
|
|
|
|
return m_tmd.GetGameTDBID();
|
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::string VolumeWAD::GetMakerID(const Partition& partition) const
|
2009-07-03 16:34:51 -06:00
|
|
|
{
|
2017-03-16 03:28:17 -06:00
|
|
|
char temp[2];
|
2015-06-13 04:51:24 -06:00
|
|
|
if (!Read(0x198 + m_tmd_offset, 2, (u8*)temp, partition))
|
2017-03-16 03:28:17 -06:00
|
|
|
return "00";
|
|
|
|
|
|
|
|
// Some weird channels use 0x0000 in place of the MakerID, so we need a check here
|
|
|
|
const std::locale& c_locale = std::locale::classic();
|
|
|
|
if (!std::isprint(temp[0], c_locale) || !std::isprint(temp[1], c_locale))
|
2016-06-24 02:43:46 -06:00
|
|
|
return "00";
|
2009-07-03 16:34:51 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return DecodeString(temp);
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::optional<u64> VolumeWAD::GetTitleID(const Partition& partition) const
|
2009-07-03 16:34:51 -06:00
|
|
|
{
|
2019-03-23 06:05:42 -06:00
|
|
|
return ReadSwapped<u64>(m_ticket_offset + 0x01DC, partition);
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::optional<u16> VolumeWAD::GetRevision(const Partition& partition) const
|
2015-01-30 18:43:48 -07:00
|
|
|
{
|
2017-02-11 00:57:47 -07:00
|
|
|
if (!m_tmd.IsValid())
|
2017-06-04 02:33:14 -06:00
|
|
|
return {};
|
2015-01-30 18:43:48 -07:00
|
|
|
|
2017-02-11 00:57:47 -07:00
|
|
|
return m_tmd.GetTitleVersion();
|
2015-01-30 18:43:48 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
Platform VolumeWAD::GetVolumeType() const
|
2015-01-17 05:21:02 -07:00
|
|
|
{
|
2018-03-31 06:04:13 -06:00
|
|
|
return Platform::WiiWAD;
|
2015-01-17 05:21:02 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
std::map<Language, std::string> VolumeWAD::GetLongNames() const
|
2011-12-18 22:42:20 -07:00
|
|
|
{
|
2017-03-18 08:01:03 -06:00
|
|
|
if (!m_tmd.IsValid() || !IOS::ES::IsChannel(m_tmd.GetTitleId()))
|
|
|
|
return {};
|
|
|
|
|
2017-11-02 14:05:37 -06: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 12:33:05 -06:00
|
|
|
return std::map<Language, std::string>();
|
2017-11-02 14:05:37 -06:00
|
|
|
return ReadWiiNames(names);
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
|
|
|
|
2018-03-10 14:41:49 -07:00
|
|
|
std::vector<u32> VolumeWAD::GetBanner(u32* width, u32* 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();
|
|
|
|
if (!title_id)
|
2016-06-24 02:43:46 -06:00
|
|
|
return std::vector<u32>();
|
2015-12-03 09:29:59 -07:00
|
|
|
|
2017-11-02 10:05:45 -06:00
|
|
|
return WiiSaveBanner(*title_id).GetBanner(width, height);
|
2015-12-03 09:29:59 -07:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
BlobType VolumeWAD::GetBlobType() const
|
2015-09-26 07:24:29 -06:00
|
|
|
{
|
2016-10-30 16:39:12 -06:00
|
|
|
return m_reader->GetBlobType();
|
2015-09-26 07:24:29 -06:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
u64 VolumeWAD::GetSize() const
|
2009-07-03 16:34:51 -06:00
|
|
|
{
|
2016-10-30 16:39:12 -06:00
|
|
|
return m_reader->GetDataSize();
|
2013-04-09 11:58:56 -06:00
|
|
|
}
|
|
|
|
|
2019-03-21 15:20:23 -06:00
|
|
|
bool VolumeWAD::IsSizeAccurate() const
|
|
|
|
{
|
|
|
|
return m_reader->IsDataSizeAccurate();
|
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
u64 VolumeWAD::GetRawSize() const
|
2013-04-09 11:58:56 -06:00
|
|
|
{
|
2016-10-30 16:39:12 -06:00
|
|
|
return m_reader->GetRawSize();
|
2009-07-03 16:34:51 -06:00
|
|
|
}
|
|
|
|
|
2019-05-05 17:48:12 -06:00
|
|
|
} // namespace DiscIO
|