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-05 20:10:26 -06:00
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstddef>
|
2014-08-31 07:34:58 -06:00
|
|
|
#include <memory>
|
2017-06-04 02:33:14 -06:00
|
|
|
#include <optional>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <string>
|
2014-02-17 03:18:15 -07:00
|
|
|
|
2016-11-27 03:56:22 -07:00
|
|
|
#include "Common/Align.h"
|
2015-09-26 14:39:47 -06:00
|
|
|
#include "Common/Assert.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2014-06-05 17:29:54 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "DiscIO/Blob.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "DiscIO/WiiWad.h"
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2016-09-25 15:24:44 -06:00
|
|
|
namespace
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2017-06-06 03:49:01 -06:00
|
|
|
std::vector<u8> CreateWADEntry(BlobReader& reader, u32 size, u64 offset)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
if (size == 0)
|
|
|
|
return {};
|
2015-12-19 11:46:01 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
std::vector<u8> buffer(size);
|
2015-12-19 11:46:01 -07:00
|
|
|
|
2016-06-24 02:43:46 -06: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 11:46:01 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return buffer;
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:01 -06:00
|
|
|
bool IsWiiWAD(BlobReader& reader)
|
2016-09-25 15:24:44 -06:00
|
|
|
{
|
2017-06-04 02:33:14 -06: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 15:24:44 -06:00
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
2017-07-04 07:37:41 -06:00
|
|
|
WiiWAD::WiiWAD(const std::string& name) : WiiWAD(CreateBlobReader(name))
|
2016-09-25 15:24:44 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-04 07:37:41 -06:00
|
|
|
WiiWAD::WiiWAD(std::unique_ptr<BlobReader> blob_reader) : m_reader(std::move(blob_reader))
|
2016-09-25 15:24:44 -06:00
|
|
|
{
|
2017-07-04 07:37:41 -06:00
|
|
|
if (m_reader)
|
|
|
|
m_valid = ParseWAD();
|
2016-09-25 15:24:44 -06:00
|
|
|
}
|
|
|
|
|
2017-07-04 07:37:41 -06:00
|
|
|
WiiWAD::~WiiWAD() = default;
|
|
|
|
|
2017-05-14 04:05:43 -06:00
|
|
|
bool WiiWAD::ParseWAD()
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2017-05-19 13:23:00 -06:00
|
|
|
if (!IsWiiWAD(*m_reader))
|
2016-06-24 02:43:46 -06:00
|
|
|
return false;
|
|
|
|
|
2017-06-04 02:33:14 -06: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 02:43:46 -06:00
|
|
|
return false;
|
2017-06-04 02:33:14 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
if (MAX_LOGLEVEL >= LogTypes::LOG_LEVELS::LDEBUG)
|
2017-06-04 02:33:14 -06:00
|
|
|
_dbg_assert_msg_(BOOT, *reserved == 0x00, "WiiWAD: Reserved must be 0x00");
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
u32 offset = 0x40;
|
2017-06-04 02:33:14 -06: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 04:05:43 -06:00
|
|
|
m_data_app_offset = offset;
|
2017-06-04 02:33:14 -06: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 02:43:46 -06:00
|
|
|
|
|
|
|
return true;
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
|
2017-05-14 04:05:43 -06: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 15:24:44 -06:00
|
|
|
} // namespace DiscIO
|