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-06 02:10:26 +00:00
|
|
|
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <cstddef>
|
2014-08-31 13:34:58 +00:00
|
|
|
#include <memory>
|
2017-06-04 08:33:14 +00:00
|
|
|
#include <optional>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <string>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
#include "Common/Align.h"
|
2015-09-26 20:39:47 +00:00
|
|
|
#include "Common/Assert.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2014-06-05 23:29:54 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-21 00:47:53 +00:00
|
|
|
#include "DiscIO/Blob.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DiscIO/WiiWad.h"
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2016-09-25 21:24:44 +00:00
|
|
|
namespace
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2016-09-25 21:24:44 +00:00
|
|
|
std::vector<u8> CreateWADEntry(IBlobReader& reader, u32 size, u64 offset)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (size == 0)
|
|
|
|
return {};
|
2015-12-19 18:46:01 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
std::vector<u8> buffer(size);
|
2015-12-19 18:46:01 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!reader.Read(offset, size, buffer.data()))
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "WiiWAD: Could not read from file");
|
|
|
|
PanicAlertT("WiiWAD: Could not read from file");
|
|
|
|
}
|
2015-12-19 18:46:01 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return buffer;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 19:23:00 +00:00
|
|
|
bool IsWiiWAD(IBlobReader& reader)
|
2016-09-25 21:24:44 +00:00
|
|
|
{
|
2017-06-04 08:33:14 +00:00
|
|
|
const std::optional<u32> header_size = reader.ReadSwapped<u32>(0x0);
|
|
|
|
const std::optional<u32> header_type = reader.ReadSwapped<u32>(0x4);
|
|
|
|
return header_size == u32(0x20) &&
|
|
|
|
(header_type == u32(0x49730000) || header_type == u32(0x69620000));
|
2016-09-25 21:24:44 +00:00
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
2017-05-14 10:05:43 +00:00
|
|
|
WiiWAD::WiiWAD(const std::string& name) : m_reader(CreateBlobReader(name))
|
2016-09-25 21:24:44 +00:00
|
|
|
{
|
2017-05-14 10:05:43 +00:00
|
|
|
if (m_reader == nullptr || File::IsDirectory(name))
|
2016-09-25 21:24:44 +00:00
|
|
|
{
|
|
|
|
m_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-14 10:05:43 +00:00
|
|
|
m_valid = ParseWAD();
|
2016-09-25 21:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WiiWAD::~WiiWAD()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-05-14 10:05:43 +00:00
|
|
|
bool WiiWAD::ParseWAD()
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2017-05-19 19:23:00 +00:00
|
|
|
if (!IsWiiWAD(*m_reader))
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
|
2017-06-04 08:33:14 +00:00
|
|
|
std::optional<u32> certificate_chain_size = m_reader->ReadSwapped<u32>(0x08);
|
|
|
|
std::optional<u32> reserved = m_reader->ReadSwapped<u32>(0x0C);
|
|
|
|
std::optional<u32> ticket_size = m_reader->ReadSwapped<u32>(0x10);
|
|
|
|
std::optional<u32> tmd_size = m_reader->ReadSwapped<u32>(0x14);
|
|
|
|
std::optional<u32> data_app_size = m_reader->ReadSwapped<u32>(0x18);
|
|
|
|
std::optional<u32> footer_size = m_reader->ReadSwapped<u32>(0x1C);
|
|
|
|
if (!certificate_chain_size || !reserved || !ticket_size || !tmd_size || !data_app_size ||
|
|
|
|
!footer_size)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
2017-06-04 08:33:14 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
if (MAX_LOGLEVEL >= LogTypes::LOG_LEVELS::LDEBUG)
|
2017-06-04 08:33:14 +00:00
|
|
|
_dbg_assert_msg_(BOOT, *reserved == 0x00, "WiiWAD: Reserved must be 0x00");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
u32 offset = 0x40;
|
2017-06-04 08:33:14 +00:00
|
|
|
m_certificate_chain = CreateWADEntry(*m_reader, *certificate_chain_size, offset);
|
|
|
|
offset += Common::AlignUp(*certificate_chain_size, 0x40);
|
|
|
|
m_ticket.SetBytes(CreateWADEntry(*m_reader, *ticket_size, offset));
|
|
|
|
offset += Common::AlignUp(*ticket_size, 0x40);
|
|
|
|
m_tmd.SetBytes(CreateWADEntry(*m_reader, *tmd_size, offset));
|
|
|
|
offset += Common::AlignUp(*tmd_size, 0x40);
|
2017-05-14 10:05:43 +00:00
|
|
|
m_data_app_offset = offset;
|
2017-06-04 08:33:14 +00:00
|
|
|
m_data_app = CreateWADEntry(*m_reader, *data_app_size, offset);
|
|
|
|
offset += Common::AlignUp(*data_app_size, 0x40);
|
|
|
|
m_footer = CreateWADEntry(*m_reader, *footer_size, offset);
|
|
|
|
offset += Common::AlignUp(*footer_size, 0x40);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
return true;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 10:05:43 +00:00
|
|
|
std::vector<u8> WiiWAD::GetContent(u16 index) const
|
|
|
|
{
|
|
|
|
u64 offset = m_data_app_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 {};
|
|
|
|
}
|
2016-09-25 21:24:44 +00:00
|
|
|
} // namespace DiscIO
|