NANDContentLoader/WiiWAD: Get rid of raw delete and new
This commit is contained in:
parent
e638775bc7
commit
c78c54c546
|
@ -10,11 +10,10 @@
|
|||
|
||||
#include "Core/Boot/Boot_DOL.h"
|
||||
#include "Core/HW/Memmap.h"
|
||||
#include "Core/PowerPC/PowerPC.h"
|
||||
|
||||
CDolLoader::CDolLoader(u8* buffer, size_t size)
|
||||
CDolLoader::CDolLoader(const std::vector<u8>& buffer)
|
||||
{
|
||||
m_is_valid = Initialize(buffer, size);
|
||||
m_is_valid = Initialize(buffer);
|
||||
}
|
||||
|
||||
CDolLoader::CDolLoader(const std::string& filename)
|
||||
|
@ -27,19 +26,19 @@ CDolLoader::CDolLoader(const std::string& filename)
|
|||
pStream.ReadBytes(temp_buffer.data(), temp_buffer.size());
|
||||
}
|
||||
|
||||
m_is_valid = Initialize(temp_buffer.data(), temp_buffer.size());
|
||||
m_is_valid = Initialize(temp_buffer);
|
||||
}
|
||||
|
||||
CDolLoader::~CDolLoader()
|
||||
{
|
||||
}
|
||||
|
||||
bool CDolLoader::Initialize(u8* buffer, size_t size)
|
||||
bool CDolLoader::Initialize(const std::vector<u8>& buffer)
|
||||
{
|
||||
if (size < sizeof(SDolHeader))
|
||||
if (buffer.size() < sizeof(SDolHeader))
|
||||
return false;
|
||||
|
||||
memcpy(&m_dolheader, buffer, sizeof(SDolHeader));
|
||||
memcpy(&m_dolheader, buffer.data(), sizeof(SDolHeader));
|
||||
|
||||
// swap memory
|
||||
u32* p = (u32*)&m_dolheader;
|
||||
|
@ -56,11 +55,11 @@ bool CDolLoader::Initialize(u8* buffer, size_t size)
|
|||
{
|
||||
if (m_dolheader.textSize[i] != 0)
|
||||
{
|
||||
if (size < m_dolheader.textOffset[i] + m_dolheader.textSize[i])
|
||||
if (buffer.size() < m_dolheader.textOffset[i] + m_dolheader.textSize[i])
|
||||
return false;
|
||||
|
||||
u8* text_start = buffer + m_dolheader.textOffset[i];
|
||||
m_text_sections.emplace_back(text_start, text_start + m_dolheader.textSize[i]);
|
||||
const u8* text_start = &buffer[m_dolheader.textOffset[i]];
|
||||
m_text_sections.emplace_back(text_start, &text_start[m_dolheader.textSize[i]]);
|
||||
|
||||
for (unsigned int j = 0; !m_is_wii && j < (m_dolheader.textSize[i] / sizeof(u32)); ++j)
|
||||
{
|
||||
|
@ -81,11 +80,11 @@ bool CDolLoader::Initialize(u8* buffer, size_t size)
|
|||
{
|
||||
if (m_dolheader.dataSize[i] != 0)
|
||||
{
|
||||
if (size < m_dolheader.dataOffset[i] + m_dolheader.dataSize[i])
|
||||
if (buffer.size() < m_dolheader.dataOffset[i] + m_dolheader.dataSize[i])
|
||||
return false;
|
||||
|
||||
u8* data_start = buffer + m_dolheader.dataOffset[i];
|
||||
m_data_sections.emplace_back(data_start, data_start + m_dolheader.dataSize[i]);
|
||||
const u8* data_start = &buffer[m_dolheader.dataOffset[i]];
|
||||
m_data_sections.emplace_back(data_start, &data_start[m_dolheader.dataSize[i]]);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -13,7 +13,7 @@ class CDolLoader
|
|||
{
|
||||
public:
|
||||
CDolLoader(const std::string& filename);
|
||||
CDolLoader(u8* buffer, size_t size);
|
||||
CDolLoader(const std::vector<u8>& buffer);
|
||||
~CDolLoader();
|
||||
|
||||
bool IsValid() const { return m_is_valid; }
|
||||
|
@ -54,5 +54,5 @@ private:
|
|||
bool m_is_wii;
|
||||
|
||||
// Copy sections to internal buffers
|
||||
bool Initialize(u8* buffer, size_t size);
|
||||
bool Initialize(const std::vector<u8>& buffer);
|
||||
};
|
||||
|
|
|
@ -97,13 +97,13 @@ bool CBoot::Boot_WiiWAD(const std::string& _pFilename)
|
|||
WII_IPC_HLE_Interface::SetDefaultContentFile(_pFilename);
|
||||
|
||||
std::unique_ptr<CDolLoader> pDolLoader;
|
||||
if (pContent->m_pData)
|
||||
if (pContent->m_data.empty())
|
||||
{
|
||||
pDolLoader = std::make_unique<CDolLoader>(pContent->m_pData, pContent->m_Size);
|
||||
pDolLoader = std::make_unique<CDolLoader>(pContent->m_Filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
pDolLoader = std::make_unique<CDolLoader>(pContent->m_Filename);
|
||||
pDolLoader = std::make_unique<CDolLoader>(pContent->m_data);
|
||||
}
|
||||
if (!pDolLoader->IsValid())
|
||||
return false;
|
||||
|
|
|
@ -227,7 +227,7 @@ u32 CWII_IPC_HLE_Device_es::OpenTitleContent(u32 CFD, u64 TitleID, u16 Index)
|
|||
Access.m_TitleID = TitleID;
|
||||
Access.m_pFile = nullptr;
|
||||
|
||||
if (!pContent->m_pData)
|
||||
if (pContent->m_data.empty())
|
||||
{
|
||||
std::string Filename = pContent->m_Filename;
|
||||
INFO_LOG(WII_IPC_ES, "ES: load %s", Filename.c_str());
|
||||
|
@ -403,12 +403,7 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress)
|
|||
{
|
||||
if (pDest)
|
||||
{
|
||||
if (rContent.m_pContent->m_pData)
|
||||
{
|
||||
u8* pSrc = &rContent.m_pContent->m_pData[rContent.m_Position];
|
||||
memcpy(pDest, pSrc, Size);
|
||||
}
|
||||
else
|
||||
if (rContent.m_pContent->m_data.empty())
|
||||
{
|
||||
auto& pFile = rContent.m_pFile;
|
||||
if (!pFile->Seek(rContent.m_Position, SEEK_SET))
|
||||
|
@ -421,8 +416,16 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress)
|
|||
ERROR_LOG(WII_IPC_ES, "ES: short read; returning uninitialized data!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const u8* src = &rContent.m_pContent->m_data[rContent.m_Position];
|
||||
memcpy(pDest, src, Size);
|
||||
}
|
||||
|
||||
rContent.m_Position += Size;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
PanicAlert("IOCTL_ES_READCONTENT - bad destination");
|
||||
}
|
||||
}
|
||||
|
@ -580,7 +583,7 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress)
|
|||
|
||||
u32 retVal = 0;
|
||||
const DiscIO::CNANDContentLoader& Loader = AccessContentDevice(TitleID);
|
||||
u32 ViewCount = Loader.GetTIKSize() / DiscIO::CNANDContentLoader::TICKET_SIZE;
|
||||
u32 ViewCount = static_cast<u32>(Loader.GetTicket().size()) / DiscIO::CNANDContentLoader::TICKET_SIZE;
|
||||
|
||||
if (!ViewCount)
|
||||
{
|
||||
|
@ -628,18 +631,9 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress)
|
|||
|
||||
const DiscIO::CNANDContentLoader& Loader = AccessContentDevice(TitleID);
|
||||
|
||||
const u8 *Ticket = Loader.GetTIK();
|
||||
if (Ticket)
|
||||
{
|
||||
u32 viewCnt = Loader.GetTIKSize() / DiscIO::CNANDContentLoader::TICKET_SIZE;
|
||||
for (unsigned int View = 0; View != maxViews && View < viewCnt; ++View)
|
||||
{
|
||||
Memory::Write_U32(View, Buffer.PayloadBuffer[0].m_Address + View * 0xD8);
|
||||
Memory::CopyToEmu(Buffer.PayloadBuffer[0].m_Address + 4 + View * 0xD8,
|
||||
Ticket + 0x1D0 + (View * DiscIO::CNANDContentLoader::TICKET_SIZE), 212);
|
||||
}
|
||||
}
|
||||
else
|
||||
const std::vector<u8>& ticket = Loader.GetTicket();
|
||||
|
||||
if (ticket.empty())
|
||||
{
|
||||
std::string TicketFilename = Common::GetTicketFileName(TitleID, Common::FROM_SESSION_ROOT);
|
||||
if (File::Exists(TicketFilename))
|
||||
|
@ -674,6 +668,17 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress)
|
|||
PanicAlertT("IOCTL_ES_GETVIEWS: Tried to get data from an unknown ticket: %08x/%08x", (u32)(TitleID >> 32), (u32)TitleID);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
u32 view_count = static_cast<u32>(Loader.GetTicket().size()) / DiscIO::CNANDContentLoader::TICKET_SIZE;
|
||||
for (unsigned int view = 0; view != maxViews && view < view_count; ++view)
|
||||
{
|
||||
Memory::Write_U32(view, Buffer.PayloadBuffer[0].m_Address + view * 0xD8);
|
||||
Memory::CopyToEmu(Buffer.PayloadBuffer[0].m_Address + 4 + view * 0xD8,
|
||||
&ticket[0x1D0 + (view * DiscIO::CNANDContentLoader::TICKET_SIZE)], 212);
|
||||
}
|
||||
}
|
||||
|
||||
INFO_LOG(WII_IPC_ES, "IOCTL_ES_GETVIEWS for titleID: %08x/%08x (MaxViews = %i)", (u32)(TitleID >> 32), (u32)TitleID, maxViews);
|
||||
|
||||
Memory::Write_U32(retVal, _CommandAddress + 0x4);
|
||||
|
@ -912,13 +917,13 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress)
|
|||
{
|
||||
tContentFile = Common::GetTitleContentPath(TitleID, Common::FROM_SESSION_ROOT);
|
||||
std::unique_ptr<CDolLoader> pDolLoader;
|
||||
if (pContent->m_pData)
|
||||
if (pContent->m_data.empty())
|
||||
{
|
||||
pDolLoader = std::make_unique<CDolLoader>(pContent->m_pData, pContent->m_Size);
|
||||
pDolLoader = std::make_unique<CDolLoader>(pContent->m_Filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
pDolLoader = std::make_unique<CDolLoader>(pContent->m_Filename);
|
||||
pDolLoader = std::make_unique<CDolLoader>(pContent->m_data);
|
||||
}
|
||||
|
||||
if (pDolLoader->IsValid())
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
@ -89,37 +91,25 @@ std::string CSharedContent::AddSharedContent(const u8* _pHash)
|
|||
}
|
||||
|
||||
|
||||
CNANDContentLoader::CNANDContentLoader(const std::string& _rName)
|
||||
CNANDContentLoader::CNANDContentLoader(const std::string& name)
|
||||
: m_Valid(false)
|
||||
, m_isWAD(false)
|
||||
, m_TitleID(-1)
|
||||
, m_IosVersion(0x09)
|
||||
, m_BootIndex(-1)
|
||||
, m_TIKSize(0)
|
||||
, m_TIK(nullptr)
|
||||
{
|
||||
m_Valid = Initialize(_rName);
|
||||
m_Valid = Initialize(name);
|
||||
}
|
||||
|
||||
CNANDContentLoader::~CNANDContentLoader()
|
||||
{
|
||||
for (auto& content : m_Content)
|
||||
{
|
||||
delete [] content.m_pData;
|
||||
}
|
||||
m_Content.clear();
|
||||
if (m_TIK)
|
||||
{
|
||||
delete []m_TIK;
|
||||
m_TIK = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
const SNANDContent* CNANDContentLoader::GetContentByIndex(int _Index) const
|
||||
const SNANDContent* CNANDContentLoader::GetContentByIndex(int index) const
|
||||
{
|
||||
for (auto& Content : m_Content)
|
||||
{
|
||||
if (Content.m_Index == _Index)
|
||||
if (Content.m_Index == index)
|
||||
{
|
||||
return &Content;
|
||||
}
|
||||
|
@ -127,119 +117,124 @@ const SNANDContent* CNANDContentLoader::GetContentByIndex(int _Index) const
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool CNANDContentLoader::Initialize(const std::string& _rName)
|
||||
bool CNANDContentLoader::Initialize(const std::string& name)
|
||||
{
|
||||
if (_rName.empty())
|
||||
if (name.empty())
|
||||
return false;
|
||||
m_Path = _rName;
|
||||
WiiWAD Wad(_rName);
|
||||
u8* pDataApp = nullptr;
|
||||
u8* pTMD = nullptr;
|
||||
u8 DecryptTitleKey[16];
|
||||
u8 IV[16];
|
||||
if (Wad.IsValid())
|
||||
|
||||
m_Path = name;
|
||||
|
||||
WiiWAD wad(name);
|
||||
std::vector<u8> data_app;
|
||||
std::vector<u8> tmd;
|
||||
std::vector<u8> decrypted_title_key;
|
||||
|
||||
if (wad.IsValid())
|
||||
{
|
||||
m_isWAD = true;
|
||||
m_TIKSize = Wad.GetTicketSize();
|
||||
m_TIK = new u8[m_TIKSize];
|
||||
memcpy(m_TIK, Wad.GetTicket(), m_TIKSize);
|
||||
GetKeyFromTicket(m_TIK, DecryptTitleKey);
|
||||
u32 pTMDSize = Wad.GetTMDSize();
|
||||
pTMD = new u8[pTMDSize];
|
||||
memcpy(pTMD, Wad.GetTMD(), pTMDSize);
|
||||
pDataApp = Wad.GetDataApp();
|
||||
m_ticket = wad.GetTicket();
|
||||
decrypted_title_key = GetKeyFromTicket(m_ticket);
|
||||
tmd = wad.GetTMD();
|
||||
data_app = wad.GetDataApp();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string TMDFileName(m_Path);
|
||||
std::string tmd_filename(m_Path);
|
||||
|
||||
if (TMDFileName.back() == '/')
|
||||
TMDFileName += "title.tmd";
|
||||
if (tmd_filename.back() == '/')
|
||||
tmd_filename += "title.tmd";
|
||||
else
|
||||
m_Path = TMDFileName.substr(0, TMDFileName.find("title.tmd"));
|
||||
m_Path = tmd_filename.substr(0, tmd_filename.find("title.tmd"));
|
||||
|
||||
File::IOFile pTMDFile(TMDFileName, "rb");
|
||||
if (!pTMDFile)
|
||||
File::IOFile tmd_file(tmd_filename, "rb");
|
||||
if (!tmd_file)
|
||||
{
|
||||
WARN_LOG(DISCIO, "CreateFromDirectory: error opening %s", TMDFileName.c_str());
|
||||
WARN_LOG(DISCIO, "CreateFromDirectory: error opening %s", tmd_filename.c_str());
|
||||
return false;
|
||||
}
|
||||
u32 pTMDSize = (u32)File::GetSize(TMDFileName);
|
||||
pTMD = new u8[pTMDSize];
|
||||
pTMDFile.ReadBytes(pTMD, (size_t)pTMDSize);
|
||||
pTMDFile.Close();
|
||||
|
||||
tmd.resize(static_cast<size_t>(File::GetSize(tmd_filename)));
|
||||
tmd_file.ReadBytes(tmd.data(), tmd.size());
|
||||
}
|
||||
|
||||
memcpy(m_TMDView, pTMD + 0x180, TMD_VIEW_SIZE);
|
||||
memcpy(m_TMDHeader, pTMD, TMD_HEADER_SIZE);
|
||||
std::copy(&tmd[0], &tmd[TMD_HEADER_SIZE], m_TMDHeader);
|
||||
std::copy(&tmd[0x180], &tmd[0x180 + TMD_VIEW_SIZE], m_TMDView);
|
||||
|
||||
m_TitleVersion = Common::swap16(&tmd[0x01DC]);
|
||||
m_numEntries = Common::swap16(&tmd[0x01DE]);
|
||||
m_BootIndex = Common::swap16(&tmd[0x01E0]);
|
||||
m_TitleID = Common::swap64(&tmd[0x018C]);
|
||||
m_IosVersion = Common::swap16(&tmd[0x018A]);
|
||||
m_Country = static_cast<u8>(m_TitleID & 0xFF);
|
||||
|
||||
m_TitleVersion = Common::swap16(pTMD + 0x01dc);
|
||||
m_numEntries = Common::swap16(pTMD + 0x01de);
|
||||
m_BootIndex = Common::swap16(pTMD + 0x01e0);
|
||||
m_TitleID = Common::swap64(pTMD + 0x018c);
|
||||
m_IosVersion = Common::swap16(pTMD + 0x018a);
|
||||
m_Country = *(u8*)&m_TitleID;
|
||||
if (m_Country == 2) // SYSMENU
|
||||
m_Country = GetSysMenuRegion(m_TitleVersion);
|
||||
|
||||
InitializeContentEntries(tmd, decrypted_title_key, data_app);
|
||||
return true;
|
||||
}
|
||||
|
||||
void CNANDContentLoader::InitializeContentEntries(const std::vector<u8>& tmd, const std::vector<u8>& decrypted_title_key, const std::vector<u8>& data_app)
|
||||
{
|
||||
m_Content.resize(m_numEntries);
|
||||
|
||||
std::array<u8, 16> iv;
|
||||
u32 data_app_offset = 0;
|
||||
|
||||
for (u32 i=0; i < m_numEntries; i++)
|
||||
for (u32 i = 0; i < m_numEntries; i++)
|
||||
{
|
||||
SNANDContent& rContent = m_Content[i];
|
||||
const u32 entry_offset = 0x24 * i;
|
||||
|
||||
rContent.m_ContentID = Common::swap32(pTMD + 0x01e4 + 0x24*i);
|
||||
rContent.m_Index = Common::swap16(pTMD + 0x01e8 + 0x24*i);
|
||||
rContent.m_Type = Common::swap16(pTMD + 0x01ea + 0x24*i);
|
||||
rContent.m_Size = (u32)Common::swap64(pTMD + 0x01ec + 0x24*i);
|
||||
memcpy(rContent.m_SHA1Hash, pTMD + 0x01f4 + 0x24*i, 20);
|
||||
memcpy(rContent.m_Header, pTMD + 0x01e4 + 0x24*i, 36);
|
||||
SNANDContent& content = m_Content[i];
|
||||
content.m_ContentID = Common::swap32(&tmd[entry_offset + 0x01E4]);
|
||||
content.m_Index = Common::swap16(&tmd[entry_offset + 0x01E8]);
|
||||
content.m_Type = Common::swap16(&tmd[entry_offset + 0x01EA]);
|
||||
content.m_Size = static_cast<u32>(Common::swap64(&tmd[entry_offset + 0x01EC]));
|
||||
std::copy(&tmd[entry_offset + 0x01E4], &tmd[entry_offset + 0x01E4 + 36], content.m_Header);
|
||||
std::copy(&tmd[entry_offset + 0x01F4], &tmd[entry_offset + 0x01F4 + 20], content.m_SHA1Hash);
|
||||
|
||||
if (m_isWAD)
|
||||
{
|
||||
u32 RoundedSize = ROUND_UP(rContent.m_Size, 0x40);
|
||||
rContent.m_pData = new u8[RoundedSize];
|
||||
u32 rounded_size = ROUND_UP(content.m_Size, 0x40);
|
||||
|
||||
memset(IV, 0, sizeof IV);
|
||||
memcpy(IV, pTMD + 0x01e8 + 0x24*i, 2);
|
||||
AESDecode(DecryptTitleKey, IV, pDataApp, RoundedSize, rContent.m_pData);
|
||||
iv.fill(0);
|
||||
std::copy(&tmd[entry_offset + 0x01E8], &tmd[entry_offset + 0x01E8 + 2], iv.begin());
|
||||
|
||||
pDataApp += RoundedSize;
|
||||
content.m_data = AESDecode(decrypted_title_key.data(), iv.data(), &data_app[data_app_offset], rounded_size);
|
||||
|
||||
data_app_offset += rounded_size;
|
||||
continue;
|
||||
}
|
||||
|
||||
rContent.m_pData = nullptr;
|
||||
|
||||
if (rContent.m_Type & 0x8000) // shared app
|
||||
rContent.m_Filename = CSharedContent::AccessInstance().GetFilenameFromSHA1(rContent.m_SHA1Hash);
|
||||
if (content.m_Type & 0x8000) // shared app
|
||||
content.m_Filename = CSharedContent::AccessInstance().GetFilenameFromSHA1(content.m_SHA1Hash);
|
||||
else
|
||||
rContent.m_Filename = StringFromFormat("%s/%08x.app", m_Path.c_str(), rContent.m_ContentID);
|
||||
content.m_Filename = StringFromFormat("%s/%08x.app", m_Path.c_str(), content.m_ContentID);
|
||||
|
||||
// Be graceful about incorrect TMDs.
|
||||
if (File::Exists(rContent.m_Filename))
|
||||
rContent.m_Size = (u32) File::GetSize(rContent.m_Filename);
|
||||
if (File::Exists(content.m_Filename))
|
||||
content.m_Size = static_cast<u32>(File::GetSize(content.m_Filename));
|
||||
}
|
||||
|
||||
delete [] pTMD;
|
||||
return true;
|
||||
}
|
||||
void CNANDContentLoader::AESDecode(u8* _pKey, u8* _IV, u8* _pSrc, u32 _Size, u8* _pDest)
|
||||
{
|
||||
mbedtls_aes_context AES_ctx;
|
||||
|
||||
mbedtls_aes_setkey_dec(&AES_ctx, _pKey, 128);
|
||||
mbedtls_aes_crypt_cbc(&AES_ctx, MBEDTLS_AES_DECRYPT, _Size, _IV, _pSrc, _pDest);
|
||||
}
|
||||
|
||||
void CNANDContentLoader::GetKeyFromTicket(u8* pTicket, u8* pTicketKey)
|
||||
std::vector<u8> CNANDContentLoader::AESDecode(const u8* key, u8* iv, const u8* src, u32 size)
|
||||
{
|
||||
u8 CommonKey[16] = {0xeb,0xe4,0x2a,0x22,0x5e,0x85,0x93,0xe4,0x48,0xd9,0xc5,0x45,0x73,0x81,0xaa,0xf7};
|
||||
u8 IV[16];
|
||||
memset(IV, 0, sizeof IV);
|
||||
memcpy(IV, pTicket + 0x01dc, 8);
|
||||
AESDecode(CommonKey, IV, pTicket + 0x01bf, 16, pTicketKey);
|
||||
mbedtls_aes_context aes_ctx;
|
||||
std::vector<u8> buffer(size);
|
||||
|
||||
mbedtls_aes_setkey_dec(&aes_ctx, key, 128);
|
||||
mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, size, iv, src, buffer.data());
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::vector<u8> CNANDContentLoader::GetKeyFromTicket(const std::vector<u8>& ticket)
|
||||
{
|
||||
const u8 common_key[16] = {0xeb,0xe4,0x2a,0x22,0x5e,0x85,0x93,0xe4,0x48,0xd9,0xc5,0x45,0x73,0x81,0xaa,0xf7};
|
||||
u8 iv[16] = {};
|
||||
|
||||
std::copy(&ticket[0x01DC], &ticket[0x01DC + 8], iv);
|
||||
return AESDecode(common_key, iv, &ticket[0x01BF], 16);
|
||||
}
|
||||
|
||||
|
||||
|
@ -380,86 +375,85 @@ void cUIDsys::GetTitleIDs(std::vector<u64>& _TitleIDs, bool _owned)
|
|||
}
|
||||
}
|
||||
|
||||
u64 CNANDContentManager::Install_WiiWAD(const std::string& fileName)
|
||||
u64 CNANDContentManager::Install_WiiWAD(const std::string& filename)
|
||||
{
|
||||
if (fileName.find(".wad") == std::string::npos)
|
||||
if (filename.find(".wad") == std::string::npos)
|
||||
return 0;
|
||||
const CNANDContentLoader& ContentLoader = GetNANDLoader(fileName);
|
||||
if (ContentLoader.IsValid() == false)
|
||||
const CNANDContentLoader& content_loader = GetNANDLoader(filename);
|
||||
if (content_loader.IsValid() == false)
|
||||
return 0;
|
||||
|
||||
u64 TitleID = ContentLoader.GetTitleID();
|
||||
u64 title_id = content_loader.GetTitleID();
|
||||
|
||||
//copy WAD's TMD header and contents to content directory
|
||||
|
||||
std::string ContentPath(Common::GetTitleContentPath(TitleID, Common::FROM_CONFIGURED_ROOT));
|
||||
std::string TMDFileName(Common::GetTMDFileName(TitleID, Common::FROM_CONFIGURED_ROOT));
|
||||
File::CreateFullPath(TMDFileName);
|
||||
std::string content_path(Common::GetTitleContentPath(title_id, Common::FROM_CONFIGURED_ROOT));
|
||||
std::string tmd_filename(Common::GetTMDFileName(title_id, Common::FROM_CONFIGURED_ROOT));
|
||||
File::CreateFullPath(tmd_filename);
|
||||
|
||||
File::IOFile pTMDFile(TMDFileName, "wb");
|
||||
if (!pTMDFile)
|
||||
File::IOFile tmd_file(tmd_filename, "wb");
|
||||
if (!tmd_file)
|
||||
{
|
||||
PanicAlertT("WAD installation failed: error creating %s", TMDFileName.c_str());
|
||||
PanicAlertT("WAD installation failed: error creating %s", tmd_filename.c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
pTMDFile.WriteBytes(ContentLoader.GetTMDHeader(), CNANDContentLoader::TMD_HEADER_SIZE);
|
||||
tmd_file.WriteBytes(content_loader.GetTMDHeader(), CNANDContentLoader::TMD_HEADER_SIZE);
|
||||
|
||||
for (u32 i = 0; i < ContentLoader.GetContentSize(); i++)
|
||||
for (u32 i = 0; i < content_loader.GetContentSize(); i++)
|
||||
{
|
||||
const SNANDContent& Content = ContentLoader.GetContent()[i];
|
||||
const SNANDContent& content = content_loader.GetContent()[i];
|
||||
|
||||
pTMDFile.WriteBytes(Content.m_Header, CNANDContentLoader::CONTENT_HEADER_SIZE);
|
||||
tmd_file.WriteBytes(content.m_Header, CNANDContentLoader::CONTENT_HEADER_SIZE);
|
||||
|
||||
std::string APPFileName;
|
||||
if (Content.m_Type & 0x8000) //shared
|
||||
APPFileName = CSharedContent::AccessInstance().AddSharedContent(Content.m_SHA1Hash);
|
||||
std::string app_filename;
|
||||
if (content.m_Type & 0x8000) //shared
|
||||
app_filename = CSharedContent::AccessInstance().AddSharedContent(content.m_SHA1Hash);
|
||||
else
|
||||
APPFileName = StringFromFormat("%s%08x.app", ContentPath.c_str(), Content.m_ContentID);
|
||||
app_filename = StringFromFormat("%s%08x.app", content_path.c_str(), content.m_ContentID);
|
||||
|
||||
if (!File::Exists(APPFileName))
|
||||
if (!File::Exists(app_filename))
|
||||
{
|
||||
File::CreateFullPath(APPFileName);
|
||||
File::IOFile pAPPFile(APPFileName, "wb");
|
||||
if (!pAPPFile)
|
||||
File::CreateFullPath(app_filename);
|
||||
File::IOFile app_file(app_filename, "wb");
|
||||
if (!app_file)
|
||||
{
|
||||
PanicAlertT("WAD installation failed: error creating %s", APPFileName.c_str());
|
||||
PanicAlertT("WAD installation failed: error creating %s", app_filename.c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
pAPPFile.WriteBytes(Content.m_pData, Content.m_Size);
|
||||
app_file.WriteBytes(content.m_data.data(), content.m_Size);
|
||||
}
|
||||
else
|
||||
{
|
||||
INFO_LOG(DISCIO, "Content %s already exists.", APPFileName.c_str());
|
||||
INFO_LOG(DISCIO, "Content %s already exists.", app_filename.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
//Extract and copy WAD's ticket to ticket directory
|
||||
if (!Add_Ticket(TitleID, ContentLoader.GetTIK(), ContentLoader.GetTIKSize()))
|
||||
if (!AddTicket(title_id, content_loader.GetTicket()))
|
||||
{
|
||||
PanicAlertT("WAD installation failed: error creating ticket");
|
||||
return 0;
|
||||
}
|
||||
|
||||
cUIDsys::AccessInstance().AddTitle(TitleID);
|
||||
cUIDsys::AccessInstance().AddTitle(title_id);
|
||||
|
||||
ClearCache();
|
||||
|
||||
return TitleID;
|
||||
return title_id;
|
||||
}
|
||||
|
||||
bool Add_Ticket(u64 TitleID, const u8* p_tik, u32 tikSize)
|
||||
bool AddTicket(u64 title_id, const std::vector<u8>& ticket)
|
||||
{
|
||||
std::string TicketFileName = Common::GetTicketFileName(TitleID, Common::FROM_CONFIGURED_ROOT);
|
||||
File::CreateFullPath(TicketFileName);
|
||||
File::IOFile pTicketFile(TicketFileName, "wb");
|
||||
if (!pTicketFile || !p_tik)
|
||||
{
|
||||
//PanicAlertT("WAD installation failed: error creating %s", TicketFileName.c_str());
|
||||
std::string ticket_filename = Common::GetTicketFileName(title_id, Common::FROM_CONFIGURED_ROOT);
|
||||
File::CreateFullPath(ticket_filename);
|
||||
|
||||
File::IOFile ticket_file(ticket_filename, "wb");
|
||||
if (!ticket_file)
|
||||
return false;
|
||||
}
|
||||
return pTicketFile.WriteBytes(p_tik, tikSize);
|
||||
|
||||
return ticket_file.WriteBytes(ticket.data(), ticket.size());
|
||||
}
|
||||
|
||||
} // namespace end
|
||||
|
|
|
@ -15,7 +15,8 @@
|
|||
|
||||
namespace DiscIO
|
||||
{
|
||||
bool Add_Ticket(u64 TitleID, const u8 *p_tik, u32 tikSize);
|
||||
bool AddTicket(u64 title_id, const std::vector<u8>& ticket);
|
||||
|
||||
struct SNANDContent
|
||||
{
|
||||
u32 m_ContentID;
|
||||
|
@ -26,7 +27,7 @@ struct SNANDContent
|
|||
u8 m_Header[36]; //all of the above
|
||||
|
||||
std::string m_Filename;
|
||||
u8* m_pData;
|
||||
std::vector<u8> m_data;
|
||||
};
|
||||
|
||||
// Instances of this class must be created by CNANDContentManager
|
||||
|
@ -42,11 +43,10 @@ public:
|
|||
u16 GetIosVersion() const { return m_IosVersion; }
|
||||
u32 GetBootIndex() const { return m_BootIndex; }
|
||||
size_t GetContentSize() const { return m_Content.size(); }
|
||||
const SNANDContent* GetContentByIndex(int _Index) const;
|
||||
const SNANDContent* GetContentByIndex(int index) const;
|
||||
const u8* GetTMDView() const { return m_TMDView; }
|
||||
const u8* GetTMDHeader() const { return m_TMDHeader; }
|
||||
u32 GetTIKSize() const { return m_TIKSize; }
|
||||
const u8* GetTIK() const { return m_TIK; }
|
||||
const std::vector<u8>& GetTicket() const { return m_ticket; }
|
||||
|
||||
const std::vector<SNANDContent>& GetContent() const { return m_Content; }
|
||||
|
||||
|
@ -64,6 +64,12 @@ public:
|
|||
};
|
||||
|
||||
private:
|
||||
bool Initialize(const std::string& name);
|
||||
void InitializeContentEntries(const std::vector<u8>& tmd, const std::vector<u8>& decrypted_title_key, const std::vector<u8>& data_app);
|
||||
|
||||
static std::vector<u8> AESDecode(const u8* key, u8* iv, const u8* src, u32 size);
|
||||
static std::vector<u8> GetKeyFromTicket(const std::vector<u8>& ticket);
|
||||
|
||||
bool m_Valid;
|
||||
bool m_isWAD;
|
||||
std::string m_Path;
|
||||
|
@ -74,18 +80,10 @@ private:
|
|||
u16 m_TitleVersion;
|
||||
u8 m_TMDView[TMD_VIEW_SIZE];
|
||||
u8 m_TMDHeader[TMD_HEADER_SIZE];
|
||||
u32 m_TIKSize;
|
||||
u8* m_TIK;
|
||||
std::vector<u8> m_ticket;
|
||||
u8 m_Country;
|
||||
|
||||
std::vector<SNANDContent> m_Content;
|
||||
|
||||
|
||||
bool Initialize(const std::string& _rName);
|
||||
|
||||
void AESDecode(u8* _pKey, u8* _IV, u8* _pSrc, u32 _Size, u8* _pDest);
|
||||
|
||||
void GetKeyFromTicket(u8* pTicket, u8* pTicketKey);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -20,76 +20,73 @@ namespace DiscIO
|
|||
|
||||
WiiWAD::WiiWAD(const std::string& name)
|
||||
{
|
||||
std::unique_ptr<IBlobReader> reader(DiscIO::CreateBlobReader(name));
|
||||
std::unique_ptr<IBlobReader> reader(CreateBlobReader(name));
|
||||
if (reader == nullptr || File::IsDirectory(name))
|
||||
{
|
||||
m_Valid = false;
|
||||
m_valid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
m_Valid = ParseWAD(*reader);
|
||||
m_valid = ParseWAD(*reader);
|
||||
}
|
||||
|
||||
WiiWAD::~WiiWAD()
|
||||
{
|
||||
if (m_Valid)
|
||||
{
|
||||
delete[] m_pCertificateChain;
|
||||
delete[] m_pTicket;
|
||||
delete[] m_pTMD;
|
||||
delete[] m_pDataApp;
|
||||
delete[] m_pFooter;
|
||||
}
|
||||
}
|
||||
|
||||
u8* WiiWAD::CreateWADEntry(DiscIO::IBlobReader& _rReader, u32 _Size, u64 _Offset)
|
||||
std::vector<u8> WiiWAD::CreateWADEntry(IBlobReader& reader, u32 size, u64 offset)
|
||||
{
|
||||
if (_Size > 0)
|
||||
{
|
||||
u8* pTmpBuffer = new u8[_Size];
|
||||
_dbg_assert_msg_(BOOT, pTmpBuffer!=nullptr, "WiiWAD: Can't allocate memory for WAD entry");
|
||||
if (size == 0)
|
||||
return {};
|
||||
|
||||
if (!_rReader.Read(_Offset, _Size, pTmpBuffer))
|
||||
{
|
||||
ERROR_LOG(DISCIO, "WiiWAD: Could not read from file");
|
||||
PanicAlertT("WiiWAD: Could not read from file");
|
||||
}
|
||||
return pTmpBuffer;
|
||||
std::vector<u8> buffer(size);
|
||||
|
||||
if (!reader.Read(offset, size, buffer.data()))
|
||||
{
|
||||
ERROR_LOG(DISCIO, "WiiWAD: Could not read from file");
|
||||
PanicAlertT("WiiWAD: Could not read from file");
|
||||
}
|
||||
return nullptr;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
bool WiiWAD::ParseWAD(DiscIO::IBlobReader& _rReader)
|
||||
bool WiiWAD::ParseWAD(IBlobReader& reader)
|
||||
{
|
||||
CBlobBigEndianReader ReaderBig(_rReader);
|
||||
CBlobBigEndianReader big_endian_reader(reader);
|
||||
|
||||
if (!IsWiiWAD(ReaderBig))
|
||||
if (!IsWiiWAD(big_endian_reader))
|
||||
return false;
|
||||
|
||||
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))
|
||||
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))
|
||||
return false;
|
||||
|
||||
if (MAX_LOGLEVEL >= LogTypes::LOG_LEVELS::LDEBUG)
|
||||
_dbg_assert_msg_(BOOT, Reserved==0x00, "WiiWAD: Reserved must be 0x00");
|
||||
_dbg_assert_msg_(BOOT, reserved == 0x00, "WiiWAD: Reserved must be 0x00");
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiiWAD::IsWiiWAD(const DiscIO::CBlobBigEndianReader& reader)
|
||||
bool WiiWAD::IsWiiWAD(const CBlobBigEndianReader& reader)
|
||||
{
|
||||
u32 header_size = 0;
|
||||
u32 header_type = 0;
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Common/CommonTypes.h"
|
||||
|
||||
|
@ -17,43 +18,29 @@ class CBlobBigEndianReader;
|
|||
class WiiWAD
|
||||
{
|
||||
public:
|
||||
|
||||
WiiWAD(const std::string& _rName);
|
||||
|
||||
WiiWAD(const std::string& name);
|
||||
~WiiWAD();
|
||||
|
||||
bool IsValid() const { return m_Valid; }
|
||||
u32 GetCertificateChainSize() const { return m_CertificateChainSize; }
|
||||
u32 GetTicketSize() const { return m_TicketSize; }
|
||||
u32 GetTMDSize() const { return m_TMDSize; }
|
||||
u32 GetDataAppSize() const { return m_DataAppSize; }
|
||||
u32 GetFooterSize() const { return m_FooterSize; }
|
||||
bool IsValid() const { return m_valid; }
|
||||
|
||||
u8* GetCertificateChain() const { return m_pCertificateChain; }
|
||||
u8* GetTicket() const { return m_pTicket; }
|
||||
u8* GetTMD() const { return m_pTMD; }
|
||||
u8* GetDataApp() const { return m_pDataApp; }
|
||||
u8* GetFooter() const { return m_pFooter; }
|
||||
const std::vector<u8>& GetCertificateChain() const { return m_certificate_chain; }
|
||||
const std::vector<u8>& GetTicket() const { return m_ticket; }
|
||||
const std::vector<u8>& GetTMD() const { return m_tmd; }
|
||||
const std::vector<u8>& GetDataApp() const { return m_data_app; }
|
||||
const std::vector<u8>& GetFooter() const { return m_footer; }
|
||||
|
||||
private:
|
||||
bool ParseWAD(IBlobReader& reader);
|
||||
static std::vector<u8> CreateWADEntry(IBlobReader& reader, u32 size, u64 offset);
|
||||
static bool IsWiiWAD(const CBlobBigEndianReader& reader);
|
||||
|
||||
bool m_Valid;
|
||||
bool m_valid;
|
||||
|
||||
u32 m_CertificateChainSize;
|
||||
u32 m_TicketSize;
|
||||
u32 m_TMDSize;
|
||||
u32 m_DataAppSize;
|
||||
u32 m_FooterSize;
|
||||
|
||||
u8* m_pCertificateChain;
|
||||
u8* m_pTicket;
|
||||
u8* m_pTMD;
|
||||
u8* m_pDataApp;
|
||||
u8* m_pFooter;
|
||||
|
||||
u8* CreateWADEntry(DiscIO::IBlobReader& _rReader, u32 _Size, u64 _Offset);
|
||||
bool ParseWAD(DiscIO::IBlobReader& _rReader);
|
||||
static bool IsWiiWAD(const DiscIO::CBlobBigEndianReader& reader);
|
||||
std::vector<u8> m_certificate_chain;
|
||||
std::vector<u8> m_ticket;
|
||||
std::vector<u8> m_tmd;
|
||||
std::vector<u8> m_data_app;
|
||||
std::vector<u8> m_footer;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue