Merge pull request #3362 from lioncash/memory

NANDContentLoader/WiiWAD: Get rid of raw delete and new
This commit is contained in:
Mathew Maidment 2015-12-22 06:39:12 -05:00
commit 1b69743ba1
8 changed files with 234 additions and 254 deletions

View File

@ -10,11 +10,10 @@
#include "Core/Boot/Boot_DOL.h" #include "Core/Boot/Boot_DOL.h"
#include "Core/HW/Memmap.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) CDolLoader::CDolLoader(const std::string& filename)
@ -27,19 +26,19 @@ CDolLoader::CDolLoader(const std::string& filename)
pStream.ReadBytes(temp_buffer.data(), temp_buffer.size()); 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() 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; return false;
memcpy(&m_dolheader, buffer, sizeof(SDolHeader)); memcpy(&m_dolheader, buffer.data(), sizeof(SDolHeader));
// swap memory // swap memory
u32* p = (u32*)&m_dolheader; u32* p = (u32*)&m_dolheader;
@ -56,11 +55,11 @@ bool CDolLoader::Initialize(u8* buffer, size_t size)
{ {
if (m_dolheader.textSize[i] != 0) 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; return false;
u8* text_start = buffer + m_dolheader.textOffset[i]; const u8* text_start = &buffer[m_dolheader.textOffset[i]];
m_text_sections.emplace_back(text_start, text_start + m_dolheader.textSize[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) 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 (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; return false;
u8* data_start = buffer + m_dolheader.dataOffset[i]; const u8* data_start = &buffer[m_dolheader.dataOffset[i]];
m_data_sections.emplace_back(data_start, data_start + m_dolheader.dataSize[i]); m_data_sections.emplace_back(data_start, &data_start[m_dolheader.dataSize[i]]);
} }
else else
{ {

View File

@ -13,7 +13,7 @@ class CDolLoader
{ {
public: public:
CDolLoader(const std::string& filename); CDolLoader(const std::string& filename);
CDolLoader(u8* buffer, size_t size); CDolLoader(const std::vector<u8>& buffer);
~CDolLoader(); ~CDolLoader();
bool IsValid() const { return m_is_valid; } bool IsValid() const { return m_is_valid; }
@ -54,5 +54,5 @@ private:
bool m_is_wii; bool m_is_wii;
// Copy sections to internal buffers // Copy sections to internal buffers
bool Initialize(u8* buffer, size_t size); bool Initialize(const std::vector<u8>& buffer);
}; };

View File

@ -97,13 +97,13 @@ bool CBoot::Boot_WiiWAD(const std::string& _pFilename)
WII_IPC_HLE_Interface::SetDefaultContentFile(_pFilename); WII_IPC_HLE_Interface::SetDefaultContentFile(_pFilename);
std::unique_ptr<CDolLoader> pDolLoader; 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 else
{ {
pDolLoader = std::make_unique<CDolLoader>(pContent->m_Filename); pDolLoader = std::make_unique<CDolLoader>(pContent->m_data);
} }
if (!pDolLoader->IsValid()) if (!pDolLoader->IsValid())
return false; return false;

View File

@ -228,7 +228,7 @@ u32 CWII_IPC_HLE_Device_es::OpenTitleContent(u32 CFD, u64 TitleID, u16 Index)
Access.m_TitleID = TitleID; Access.m_TitleID = TitleID;
Access.m_pFile = nullptr; Access.m_pFile = nullptr;
if (!pContent->m_pData) if (pContent->m_data.empty())
{ {
std::string Filename = pContent->m_Filename; std::string Filename = pContent->m_Filename;
INFO_LOG(WII_IPC_ES, "ES: load %s", Filename.c_str()); INFO_LOG(WII_IPC_ES, "ES: load %s", Filename.c_str());
@ -404,12 +404,7 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress)
{ {
if (pDest) if (pDest)
{ {
if (rContent.m_pContent->m_pData) if (rContent.m_pContent->m_data.empty())
{
u8* pSrc = &rContent.m_pContent->m_pData[rContent.m_Position];
memcpy(pDest, pSrc, Size);
}
else
{ {
auto& pFile = rContent.m_pFile; auto& pFile = rContent.m_pFile;
if (!pFile->Seek(rContent.m_Position, SEEK_SET)) if (!pFile->Seek(rContent.m_Position, SEEK_SET))
@ -422,8 +417,16 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress)
ERROR_LOG(WII_IPC_ES, "ES: short read; returning uninitialized data!"); 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; rContent.m_Position += Size;
} else { }
else
{
PanicAlert("IOCTL_ES_READCONTENT - bad destination"); PanicAlert("IOCTL_ES_READCONTENT - bad destination");
} }
} }
@ -581,7 +584,7 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress)
u32 retVal = 0; u32 retVal = 0;
const DiscIO::CNANDContentLoader& Loader = AccessContentDevice(TitleID); 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) if (!ViewCount)
{ {
@ -629,18 +632,9 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress)
const DiscIO::CNANDContentLoader& Loader = AccessContentDevice(TitleID); const DiscIO::CNANDContentLoader& Loader = AccessContentDevice(TitleID);
const u8 *Ticket = Loader.GetTIK(); const std::vector<u8>& ticket = Loader.GetTicket();
if (Ticket)
{ if (ticket.empty())
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
{ {
std::string TicketFilename = Common::GetTicketFileName(TitleID, Common::FROM_SESSION_ROOT); std::string TicketFilename = Common::GetTicketFileName(TitleID, Common::FROM_SESSION_ROOT);
if (File::Exists(TicketFilename)) if (File::Exists(TicketFilename))
@ -675,6 +669,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); 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); 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); Memory::Write_U32(retVal, _CommandAddress + 0x4);
@ -913,13 +918,13 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress)
{ {
tContentFile = Common::GetTitleContentPath(TitleID, Common::FROM_SESSION_ROOT); tContentFile = Common::GetTitleContentPath(TitleID, Common::FROM_SESSION_ROOT);
std::unique_ptr<CDolLoader> pDolLoader; 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 else
{ {
pDolLoader = std::make_unique<CDolLoader>(pContent->m_Filename); pDolLoader = std::make_unique<CDolLoader>(pContent->m_data);
} }
if (pDolLoader->IsValid()) if (pDolLoader->IsValid())

View File

@ -2,6 +2,8 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <array>
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
#include <cstring> #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_Valid(false)
, m_isWAD(false) , m_isWAD(false)
, m_TitleID(-1) , m_TitleID(-1)
, m_IosVersion(0x09) , m_IosVersion(0x09)
, m_BootIndex(-1) , m_BootIndex(-1)
, m_TIKSize(0)
, m_TIK(nullptr)
{ {
m_Valid = Initialize(_rName); m_Valid = Initialize(name);
} }
CNANDContentLoader::~CNANDContentLoader() 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) for (auto& Content : m_Content)
{ {
if (Content.m_Index == _Index) if (Content.m_Index == index)
{ {
return &Content; return &Content;
} }
@ -127,119 +117,124 @@ const SNANDContent* CNANDContentLoader::GetContentByIndex(int _Index) const
return nullptr; return nullptr;
} }
bool CNANDContentLoader::Initialize(const std::string& _rName) bool CNANDContentLoader::Initialize(const std::string& name)
{ {
if (_rName.empty()) if (name.empty())
return false; return false;
m_Path = _rName;
WiiWAD Wad(_rName); m_Path = name;
u8* pDataApp = nullptr;
u8* pTMD = nullptr; WiiWAD wad(name);
u8 DecryptTitleKey[16]; std::vector<u8> data_app;
u8 IV[16]; std::vector<u8> tmd;
if (Wad.IsValid()) std::vector<u8> decrypted_title_key;
if (wad.IsValid())
{ {
m_isWAD = true; m_isWAD = true;
m_TIKSize = Wad.GetTicketSize(); m_ticket = wad.GetTicket();
m_TIK = new u8[m_TIKSize]; decrypted_title_key = GetKeyFromTicket(m_ticket);
memcpy(m_TIK, Wad.GetTicket(), m_TIKSize); tmd = wad.GetTMD();
GetKeyFromTicket(m_TIK, DecryptTitleKey); data_app = wad.GetDataApp();
u32 pTMDSize = Wad.GetTMDSize();
pTMD = new u8[pTMDSize];
memcpy(pTMD, Wad.GetTMD(), pTMDSize);
pDataApp = Wad.GetDataApp();
} }
else else
{ {
std::string TMDFileName(m_Path); std::string tmd_filename(m_Path);
if (TMDFileName.back() == '/') if (tmd_filename.back() == '/')
TMDFileName += "title.tmd"; tmd_filename += "title.tmd";
else 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"); File::IOFile tmd_file(tmd_filename, "rb");
if (!pTMDFile) 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; return false;
} }
u32 pTMDSize = (u32)File::GetSize(TMDFileName);
pTMD = new u8[pTMDSize]; tmd.resize(static_cast<size_t>(File::GetSize(tmd_filename)));
pTMDFile.ReadBytes(pTMD, (size_t)pTMDSize); tmd_file.ReadBytes(tmd.data(), tmd.size());
pTMDFile.Close();
} }
memcpy(m_TMDView, pTMD + 0x180, TMD_VIEW_SIZE); std::copy(&tmd[0], &tmd[TMD_HEADER_SIZE], m_TMDHeader);
memcpy(m_TMDHeader, pTMD, TMD_HEADER_SIZE); 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 if (m_Country == 2) // SYSMENU
m_Country = GetSysMenuRegion(m_TitleVersion); 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); 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); SNANDContent& content = m_Content[i];
rContent.m_Index = Common::swap16(pTMD + 0x01e8 + 0x24*i); content.m_ContentID = Common::swap32(&tmd[entry_offset + 0x01E4]);
rContent.m_Type = Common::swap16(pTMD + 0x01ea + 0x24*i); content.m_Index = Common::swap16(&tmd[entry_offset + 0x01E8]);
rContent.m_Size = (u32)Common::swap64(pTMD + 0x01ec + 0x24*i); content.m_Type = Common::swap16(&tmd[entry_offset + 0x01EA]);
memcpy(rContent.m_SHA1Hash, pTMD + 0x01f4 + 0x24*i, 20); content.m_Size = static_cast<u32>(Common::swap64(&tmd[entry_offset + 0x01EC]));
memcpy(rContent.m_Header, pTMD + 0x01e4 + 0x24*i, 36); 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) if (m_isWAD)
{ {
u32 RoundedSize = ROUND_UP(rContent.m_Size, 0x40); u32 rounded_size = ROUND_UP(content.m_Size, 0x40);
rContent.m_pData = new u8[RoundedSize];
memset(IV, 0, sizeof IV); iv.fill(0);
memcpy(IV, pTMD + 0x01e8 + 0x24*i, 2); std::copy(&tmd[entry_offset + 0x01E8], &tmd[entry_offset + 0x01E8 + 2], iv.begin());
AESDecode(DecryptTitleKey, IV, pDataApp, RoundedSize, rContent.m_pData);
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; continue;
} }
rContent.m_pData = nullptr; if (content.m_Type & 0x8000) // shared app
content.m_Filename = CSharedContent::AccessInstance().GetFilenameFromSHA1(content.m_SHA1Hash);
if (rContent.m_Type & 0x8000) // shared app
rContent.m_Filename = CSharedContent::AccessInstance().GetFilenameFromSHA1(rContent.m_SHA1Hash);
else 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. // Be graceful about incorrect TMDs.
if (File::Exists(rContent.m_Filename)) if (File::Exists(content.m_Filename))
rContent.m_Size = (u32) File::GetSize(rContent.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}; mbedtls_aes_context aes_ctx;
u8 IV[16]; std::vector<u8> buffer(size);
memset(IV, 0, sizeof IV);
memcpy(IV, pTicket + 0x01dc, 8); mbedtls_aes_setkey_dec(&aes_ctx, key, 128);
AESDecode(CommonKey, IV, pTicket + 0x01bf, 16, pTicketKey); 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; return 0;
const CNANDContentLoader& ContentLoader = GetNANDLoader(fileName); const CNANDContentLoader& content_loader = GetNANDLoader(filename);
if (ContentLoader.IsValid() == false) if (content_loader.IsValid() == false)
return 0; return 0;
u64 TitleID = ContentLoader.GetTitleID(); u64 title_id = content_loader.GetTitleID();
//copy WAD's TMD header and contents to content directory //copy WAD's TMD header and contents to content directory
std::string ContentPath(Common::GetTitleContentPath(TitleID, Common::FROM_CONFIGURED_ROOT)); std::string content_path(Common::GetTitleContentPath(title_id, Common::FROM_CONFIGURED_ROOT));
std::string TMDFileName(Common::GetTMDFileName(TitleID, Common::FROM_CONFIGURED_ROOT)); std::string tmd_filename(Common::GetTMDFileName(title_id, Common::FROM_CONFIGURED_ROOT));
File::CreateFullPath(TMDFileName); File::CreateFullPath(tmd_filename);
File::IOFile pTMDFile(TMDFileName, "wb"); File::IOFile tmd_file(tmd_filename, "wb");
if (!pTMDFile) 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; 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; std::string app_filename;
if (Content.m_Type & 0x8000) //shared if (content.m_Type & 0x8000) //shared
APPFileName = CSharedContent::AccessInstance().AddSharedContent(Content.m_SHA1Hash); app_filename = CSharedContent::AccessInstance().AddSharedContent(content.m_SHA1Hash);
else 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::CreateFullPath(app_filename);
File::IOFile pAPPFile(APPFileName, "wb"); File::IOFile app_file(app_filename, "wb");
if (!pAPPFile) 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; return 0;
} }
pAPPFile.WriteBytes(Content.m_pData, Content.m_Size); app_file.WriteBytes(content.m_data.data(), content.m_Size);
} }
else 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 //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"); PanicAlertT("WAD installation failed: error creating ticket");
return 0; return 0;
} }
cUIDsys::AccessInstance().AddTitle(TitleID); cUIDsys::AccessInstance().AddTitle(title_id);
ClearCache(); 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); std::string ticket_filename = Common::GetTicketFileName(title_id, Common::FROM_CONFIGURED_ROOT);
File::CreateFullPath(TicketFileName); File::CreateFullPath(ticket_filename);
File::IOFile pTicketFile(TicketFileName, "wb");
if (!pTicketFile || !p_tik) File::IOFile ticket_file(ticket_filename, "wb");
{ if (!ticket_file)
//PanicAlertT("WAD installation failed: error creating %s", TicketFileName.c_str());
return false; return false;
}
return pTicketFile.WriteBytes(p_tik, tikSize); return ticket_file.WriteBytes(ticket.data(), ticket.size());
} }
} // namespace end } // namespace end

View File

@ -15,7 +15,8 @@
namespace DiscIO 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 struct SNANDContent
{ {
u32 m_ContentID; u32 m_ContentID;
@ -26,7 +27,7 @@ struct SNANDContent
u8 m_Header[36]; //all of the above u8 m_Header[36]; //all of the above
std::string m_Filename; std::string m_Filename;
u8* m_pData; std::vector<u8> m_data;
}; };
// Instances of this class must be created by CNANDContentManager // Instances of this class must be created by CNANDContentManager
@ -42,11 +43,10 @@ public:
u16 GetIosVersion() const { return m_IosVersion; } u16 GetIosVersion() const { return m_IosVersion; }
u32 GetBootIndex() const { return m_BootIndex; } u32 GetBootIndex() const { return m_BootIndex; }
size_t GetContentSize() const { return m_Content.size(); } 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* GetTMDView() const { return m_TMDView; }
const u8* GetTMDHeader() const { return m_TMDHeader; } const u8* GetTMDHeader() const { return m_TMDHeader; }
u32 GetTIKSize() const { return m_TIKSize; } const std::vector<u8>& GetTicket() const { return m_ticket; }
const u8* GetTIK() const { return m_TIK; }
const std::vector<SNANDContent>& GetContent() const { return m_Content; } const std::vector<SNANDContent>& GetContent() const { return m_Content; }
@ -64,6 +64,12 @@ public:
}; };
private: 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_Valid;
bool m_isWAD; bool m_isWAD;
std::string m_Path; std::string m_Path;
@ -74,18 +80,10 @@ private:
u16 m_TitleVersion; u16 m_TitleVersion;
u8 m_TMDView[TMD_VIEW_SIZE]; u8 m_TMDView[TMD_VIEW_SIZE];
u8 m_TMDHeader[TMD_HEADER_SIZE]; u8 m_TMDHeader[TMD_HEADER_SIZE];
u32 m_TIKSize; std::vector<u8> m_ticket;
u8* m_TIK;
u8 m_Country; u8 m_Country;
std::vector<SNANDContent> m_Content; 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);
}; };

View File

@ -20,76 +20,73 @@ namespace DiscIO
WiiWAD::WiiWAD(const std::string& name) 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)) if (reader == nullptr || File::IsDirectory(name))
{ {
m_Valid = false; m_valid = false;
return; return;
} }
m_Valid = ParseWAD(*reader); m_valid = ParseWAD(*reader);
} }
WiiWAD::~WiiWAD() 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) if (size == 0)
{ return {};
u8* pTmpBuffer = new u8[_Size];
_dbg_assert_msg_(BOOT, pTmpBuffer!=nullptr, "WiiWAD: Can't allocate memory for WAD entry");
if (!_rReader.Read(_Offset, _Size, pTmpBuffer)) std::vector<u8> buffer(size);
{
ERROR_LOG(DISCIO, "WiiWAD: Could not read from file"); if (!reader.Read(offset, size, buffer.data()))
PanicAlertT("WiiWAD: Could not read from file"); {
} ERROR_LOG(DISCIO, "WiiWAD: Could not read from file");
return pTmpBuffer; 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; return false;
u32 Reserved; u32 certificate_chain_size;
if (!ReaderBig.ReadSwapped(0x8, &m_CertificateChainSize) || u32 reserved;
!ReaderBig.ReadSwapped(0xC, &Reserved) || u32 ticket_size;
!ReaderBig.ReadSwapped(0x10, &m_TicketSize) || u32 tmd_size;
!ReaderBig.ReadSwapped(0x14, &m_TMDSize) || u32 data_app_size;
!ReaderBig.ReadSwapped(0x18, &m_DataAppSize) || u32 footer_size;
!ReaderBig.ReadSwapped(0x1C, &m_FooterSize))
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; return false;
if (MAX_LOGLEVEL >= LogTypes::LOG_LEVELS::LDEBUG) 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; u32 offset = 0x40;
m_pCertificateChain = CreateWADEntry(_rReader, m_CertificateChainSize, Offset); Offset += ROUND_UP(m_CertificateChainSize, 0x40); m_certificate_chain = CreateWADEntry(reader, certificate_chain_size, offset); offset += ROUND_UP(certificate_chain_size, 0x40);
m_pTicket = CreateWADEntry(_rReader, m_TicketSize, Offset); Offset += ROUND_UP(m_TicketSize, 0x40); m_ticket = CreateWADEntry(reader, ticket_size, offset); offset += ROUND_UP(ticket_size, 0x40);
m_pTMD = CreateWADEntry(_rReader, m_TMDSize, Offset); Offset += ROUND_UP(m_TMDSize, 0x40); m_tmd = CreateWADEntry(reader, tmd_size, offset); offset += ROUND_UP(tmd_size, 0x40);
m_pDataApp = CreateWADEntry(_rReader, m_DataAppSize, Offset); Offset += ROUND_UP(m_DataAppSize, 0x40); m_data_app = CreateWADEntry(reader, data_app_size, offset); offset += ROUND_UP(data_app_size, 0x40);
m_pFooter = CreateWADEntry(_rReader, m_FooterSize, Offset); Offset += ROUND_UP(m_FooterSize, 0x40); m_footer = CreateWADEntry(reader, footer_size, offset); offset += ROUND_UP(footer_size, 0x40);
return true; return true;
} }
bool WiiWAD::IsWiiWAD(const DiscIO::CBlobBigEndianReader& reader) bool WiiWAD::IsWiiWAD(const CBlobBigEndianReader& reader)
{ {
u32 header_size = 0; u32 header_size = 0;
u32 header_type = 0; u32 header_type = 0;

View File

@ -5,6 +5,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <vector>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -17,43 +18,29 @@ class CBlobBigEndianReader;
class WiiWAD class WiiWAD
{ {
public: public:
WiiWAD(const std::string& name);
WiiWAD(const std::string& _rName);
~WiiWAD(); ~WiiWAD();
bool IsValid() const { return m_Valid; } 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; }
u8* GetCertificateChain() const { return m_pCertificateChain; } const std::vector<u8>& GetCertificateChain() const { return m_certificate_chain; }
u8* GetTicket() const { return m_pTicket; } const std::vector<u8>& GetTicket() const { return m_ticket; }
u8* GetTMD() const { return m_pTMD; } const std::vector<u8>& GetTMD() const { return m_tmd; }
u8* GetDataApp() const { return m_pDataApp; } const std::vector<u8>& GetDataApp() const { return m_data_app; }
u8* GetFooter() const { return m_pFooter; } const std::vector<u8>& GetFooter() const { return m_footer; }
private: 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; std::vector<u8> m_certificate_chain;
u32 m_TicketSize; std::vector<u8> m_ticket;
u32 m_TMDSize; std::vector<u8> m_tmd;
u32 m_DataAppSize; std::vector<u8> m_data_app;
u32 m_FooterSize; std::vector<u8> m_footer;
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);
}; };
} }