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>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <string>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
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"
|
|
|
|
#include "Common/MathUtil.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
|
|
|
|
{
|
|
|
|
|
2014-08-31 13:34:58 +00:00
|
|
|
WiiWAD::WiiWAD(const std::string& name)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
std::unique_ptr<IBlobReader> reader(CreateBlobReader(name));
|
2014-08-31 13:34:58 +00:00
|
|
|
if (reader == nullptr || File::IsDirectory(name))
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
m_valid = false;
|
2013-03-19 13:59:41 +00:00
|
|
|
return;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
m_valid = ParseWAD(*reader);
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WiiWAD::~WiiWAD()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
std::vector<u8> WiiWAD::CreateWADEntry(IBlobReader& reader, u32 size, u64 offset)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
if (size == 0)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::vector<u8> buffer(size);
|
|
|
|
|
|
|
|
if (!reader.Read(offset, size, buffer.data()))
|
2013-03-19 13:59:41 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
ERROR_LOG(DISCIO, "WiiWAD: Could not read from file");
|
|
|
|
PanicAlertT("WiiWAD: Could not read from file");
|
2013-03-19 13:59:41 +00:00
|
|
|
}
|
2015-12-19 18:46:01 +00:00
|
|
|
|
|
|
|
return buffer;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
bool WiiWAD::ParseWAD(IBlobReader& reader)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
CBlobBigEndianReader big_endian_reader(reader);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
if (!IsWiiWAD(big_endian_reader))
|
2013-03-19 13:59:41 +00:00
|
|
|
return false;
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
u32 certificate_chain_size;
|
|
|
|
u32 reserved;
|
|
|
|
u32 ticket_size;
|
|
|
|
u32 tmd_size;
|
|
|
|
u32 data_app_size;
|
|
|
|
u32 footer_size;
|
|
|
|
|
|
|
|
if (!big_endian_reader.ReadSwapped(0x08, &certificate_chain_size) ||
|
|
|
|
!big_endian_reader.ReadSwapped(0x0C, &reserved) ||
|
|
|
|
!big_endian_reader.ReadSwapped(0x10, &ticket_size) ||
|
|
|
|
!big_endian_reader.ReadSwapped(0x14, &tmd_size) ||
|
|
|
|
!big_endian_reader.ReadSwapped(0x18, &data_app_size) ||
|
|
|
|
!big_endian_reader.ReadSwapped(0x1C, &footer_size))
|
2013-03-19 13:59:41 +00:00
|
|
|
return false;
|
|
|
|
|
2014-08-22 00:11:52 +00:00
|
|
|
if (MAX_LOGLEVEL >= LogTypes::LOG_LEVELS::LDEBUG)
|
2015-12-19 18:46:01 +00:00
|
|
|
_dbg_assert_msg_(BOOT, reserved == 0x00, "WiiWAD: Reserved must be 0x00");
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
u32 offset = 0x40;
|
|
|
|
m_certificate_chain = CreateWADEntry(reader, certificate_chain_size, offset); offset += ROUND_UP(certificate_chain_size, 0x40);
|
|
|
|
m_ticket = CreateWADEntry(reader, ticket_size, offset); offset += ROUND_UP(ticket_size, 0x40);
|
|
|
|
m_tmd = CreateWADEntry(reader, tmd_size, offset); offset += ROUND_UP(tmd_size, 0x40);
|
|
|
|
m_data_app = CreateWADEntry(reader, data_app_size, offset); offset += ROUND_UP(data_app_size, 0x40);
|
|
|
|
m_footer = CreateWADEntry(reader, footer_size, offset); offset += ROUND_UP(footer_size, 0x40);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
bool WiiWAD::IsWiiWAD(const CBlobBigEndianReader& reader)
|
2015-10-03 19:25:43 +00:00
|
|
|
{
|
|
|
|
u32 header_size = 0;
|
|
|
|
u32 header_type = 0;
|
|
|
|
reader.ReadSwapped(0x0, &header_size);
|
|
|
|
reader.ReadSwapped(0x4, &header_type);
|
|
|
|
return header_size == 0x20 && (header_type == 0x49730000 || header_type == 0x69620000);
|
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2015-10-03 19:25:43 +00:00
|
|
|
} // namespace end
|