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
|
|
|
{
|
2014-08-31 13:34:58 +00:00
|
|
|
std::unique_ptr<IBlobReader> reader(DiscIO::CreateBlobReader(name));
|
|
|
|
if (reader == nullptr || File::IsDirectory(name))
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
|
|
|
m_Valid = false;
|
2013-03-19 13:59:41 +00:00
|
|
|
return;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2014-08-31 13:34:58 +00:00
|
|
|
m_Valid = ParseWAD(*reader);
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WiiWAD::~WiiWAD()
|
|
|
|
{
|
|
|
|
if (m_Valid)
|
|
|
|
{
|
2015-05-22 21:52:54 +00:00
|
|
|
delete[] m_pCertificateChain;
|
|
|
|
delete[] m_pTicket;
|
|
|
|
delete[] m_pTMD;
|
|
|
|
delete[] m_pDataApp;
|
|
|
|
delete[] m_pFooter;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* WiiWAD::CreateWADEntry(DiscIO::IBlobReader& _rReader, u32 _Size, u64 _Offset)
|
|
|
|
{
|
2013-03-19 13:59:41 +00:00
|
|
|
if (_Size > 0)
|
|
|
|
{
|
|
|
|
u8* pTmpBuffer = new u8[_Size];
|
2014-11-14 02:28:27 +00:00
|
|
|
_dbg_assert_msg_(BOOT, pTmpBuffer!=nullptr, "WiiWAD: Can't allocate memory for WAD entry");
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2013-03-19 13:59:41 +00:00
|
|
|
if (!_rReader.Read(_Offset, _Size, pTmpBuffer))
|
|
|
|
{
|
2009-07-06 02:10:26 +00:00
|
|
|
ERROR_LOG(DISCIO, "WiiWAD: Could not read from file");
|
2013-03-19 13:59:41 +00:00
|
|
|
PanicAlertT("WiiWAD: Could not read from file");
|
|
|
|
}
|
|
|
|
return pTmpBuffer;
|
|
|
|
}
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool WiiWAD::ParseWAD(DiscIO::IBlobReader& _rReader)
|
|
|
|
{
|
2013-03-19 13:59:41 +00:00
|
|
|
CBlobBigEndianReader ReaderBig(_rReader);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2015-10-03 19:25:43 +00:00
|
|
|
if (!IsWiiWAD(ReaderBig))
|
2013-03-19 13:59:41 +00:00
|
|
|
return false;
|
|
|
|
|
2015-10-03 19:25:43 +00:00
|
|
|
u32 Reserved;
|
|
|
|
if (!ReaderBig.ReadSwapped(0x8, &m_CertificateChainSize) ||
|
|
|
|
!ReaderBig.ReadSwapped(0xC, &Reserved) ||
|
|
|
|
!ReaderBig.ReadSwapped(0x10, &m_TicketSize) ||
|
|
|
|
!ReaderBig.ReadSwapped(0x14, &m_TMDSize) ||
|
|
|
|
!ReaderBig.ReadSwapped(0x18, &m_DataAppSize) ||
|
|
|
|
!ReaderBig.ReadSwapped(0x1C, &m_FooterSize))
|
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)
|
|
|
|
_dbg_assert_msg_(BOOT, Reserved==0x00, "WiiWAD: Reserved must be 0x00");
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2013-03-19 13:59:41 +00:00
|
|
|
u32 Offset = 0x40;
|
|
|
|
m_pCertificateChain = CreateWADEntry(_rReader, m_CertificateChainSize, Offset); Offset += ROUND_UP(m_CertificateChainSize, 0x40);
|
|
|
|
m_pTicket = CreateWADEntry(_rReader, m_TicketSize, Offset); Offset += ROUND_UP(m_TicketSize, 0x40);
|
|
|
|
m_pTMD = CreateWADEntry(_rReader, m_TMDSize, Offset); Offset += ROUND_UP(m_TMDSize, 0x40);
|
|
|
|
m_pDataApp = CreateWADEntry(_rReader, m_DataAppSize, Offset); Offset += ROUND_UP(m_DataAppSize, 0x40);
|
|
|
|
m_pFooter = CreateWADEntry(_rReader, m_FooterSize, Offset); Offset += ROUND_UP(m_FooterSize, 0x40);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:25:43 +00:00
|
|
|
bool WiiWAD::IsWiiWAD(const DiscIO::CBlobBigEndianReader& reader)
|
|
|
|
{
|
|
|
|
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
|