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-03-07 08:35:01 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2015-09-23 22:39:34 +00:00
|
|
|
#include <mbedtls/aes.h>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2016-01-01 20:33:14 +00:00
|
|
|
#include "Common/CommonFuncs.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"
|
2014-02-19 01:02:01 +00:00
|
|
|
#include "Common/MathUtil.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-21 00:47:53 +00:00
|
|
|
#include "Common/NandPaths.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2014-06-05 23:29:54 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "DiscIO/NANDContentLoader.h"
|
2014-02-21 00:47:53 +00:00
|
|
|
#include "DiscIO/Volume.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DiscIO/WiiWad.h"
|
2009-03-07 08:35:01 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2011-06-05 06:30:01 +00:00
|
|
|
|
2011-06-05 07:42:40 +00:00
|
|
|
CSharedContent::CSharedContent()
|
2011-06-05 06:30:01 +00:00
|
|
|
{
|
2011-06-05 07:42:40 +00:00
|
|
|
UpdateLocation();
|
2011-06-05 06:30:01 +00:00
|
|
|
}
|
2009-03-09 17:09:27 +00:00
|
|
|
|
2011-06-05 07:42:40 +00:00
|
|
|
void CSharedContent::UpdateLocation()
|
2009-03-09 17:09:27 +00:00
|
|
|
{
|
2011-06-05 07:42:40 +00:00
|
|
|
m_Elements.clear();
|
2014-09-01 19:48:02 +00:00
|
|
|
m_lastID = 0;
|
2014-11-18 01:59:14 +00:00
|
|
|
m_contentMap = StringFromFormat("%s/shared1/content.map", File::GetUserPath(D_WIIROOT_IDX).c_str());
|
2010-05-28 05:30:03 +00:00
|
|
|
|
2014-09-01 19:48:02 +00:00
|
|
|
File::IOFile pFile(m_contentMap, "rb");
|
2011-03-11 10:21:46 +00:00
|
|
|
SElement Element;
|
|
|
|
while (pFile.ReadArray(&Element, 1))
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
m_Elements.push_back(Element);
|
2014-09-01 19:48:02 +00:00
|
|
|
m_lastID++;
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
2009-03-09 17:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CSharedContent::~CSharedContent()
|
2011-06-05 07:42:40 +00:00
|
|
|
{}
|
2009-03-09 17:09:27 +00:00
|
|
|
|
2013-08-29 22:25:12 +00:00
|
|
|
std::string CSharedContent::GetFilenameFromSHA1(const u8* _pHash)
|
2009-03-09 17:09:27 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& Element : m_Elements)
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
if (memcmp(_pHash, Element.SHA1Hash, 20) == 0)
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2014-11-18 01:59:14 +00:00
|
|
|
return StringFromFormat("%s/shared1/%c%c%c%c%c%c%c%c.app", File::GetUserPath(D_WIIROOT_IDX).c_str(),
|
2014-06-03 05:08:54 +00:00
|
|
|
Element.FileName[0], Element.FileName[1], Element.FileName[2], Element.FileName[3],
|
|
|
|
Element.FileName[4], Element.FileName[5], Element.FileName[6], Element.FileName[7]);
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "unk";
|
2009-03-09 17:09:27 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:25:12 +00:00
|
|
|
std::string CSharedContent::AddSharedContent(const u8* _pHash)
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2014-06-03 05:08:54 +00:00
|
|
|
std::string filename = GetFilenameFromSHA1(_pHash);
|
|
|
|
|
|
|
|
if (strcasecmp(filename.c_str(), "unk") == 0)
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2014-09-01 19:48:02 +00:00
|
|
|
std::string id = StringFromFormat("%08x", m_lastID);
|
2010-05-13 04:50:18 +00:00
|
|
|
SElement Element;
|
2014-06-03 05:08:54 +00:00
|
|
|
memcpy(Element.FileName, id.c_str(), 8);
|
2010-05-13 04:50:18 +00:00
|
|
|
memcpy(Element.SHA1Hash, _pHash, 20);
|
|
|
|
m_Elements.push_back(Element);
|
2010-12-15 14:47:13 +00:00
|
|
|
|
2014-09-01 19:48:02 +00:00
|
|
|
File::CreateFullPath(m_contentMap);
|
2011-03-11 10:21:46 +00:00
|
|
|
|
2014-09-01 19:48:02 +00:00
|
|
|
File::IOFile pFile(m_contentMap, "ab");
|
2011-03-11 10:21:46 +00:00
|
|
|
pFile.WriteArray(&Element, 1);
|
|
|
|
|
2014-11-18 01:59:14 +00:00
|
|
|
filename = StringFromFormat("%s/shared1/%s.app", File::GetUserPath(D_WIIROOT_IDX).c_str(), id.c_str());
|
2014-09-01 19:48:02 +00:00
|
|
|
m_lastID++;
|
2010-05-13 04:50:18 +00:00
|
|
|
}
|
2014-06-03 05:08:54 +00:00
|
|
|
|
|
|
|
return filename;
|
2010-05-13 04:50:18 +00:00
|
|
|
}
|
2009-03-09 17:09:27 +00:00
|
|
|
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
CNANDContentLoader::CNANDContentLoader(const std::string& name)
|
2009-09-01 18:34:26 +00:00
|
|
|
: m_Valid(false)
|
2011-05-09 05:47:29 +00:00
|
|
|
, m_isWAD(false)
|
2009-09-01 18:34:26 +00:00
|
|
|
, m_TitleID(-1)
|
2009-06-08 18:07:45 +00:00
|
|
|
, m_IosVersion(0x09)
|
2009-09-01 18:34:26 +00:00
|
|
|
, m_BootIndex(-1)
|
2009-03-07 08:35:01 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
m_Valid = Initialize(name);
|
2009-03-07 08:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CNANDContentLoader::~CNANDContentLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
const SNANDContent* CNANDContentLoader::GetContentByIndex(int index) const
|
2009-03-07 08:35:01 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& Content : m_Content)
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
if (Content.m_Index == index)
|
2013-08-29 22:25:12 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
return &Content;
|
2013-08-29 22:25:12 +00:00
|
|
|
}
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2009-03-07 08:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
bool CNANDContentLoader::Initialize(const std::string& name)
|
2009-03-07 08:35:01 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
if (name.empty())
|
2011-08-22 04:09:18 +00:00
|
|
|
return false;
|
2015-12-19 18:46:01 +00:00
|
|
|
|
|
|
|
m_Path = name;
|
|
|
|
|
|
|
|
WiiWAD wad(name);
|
|
|
|
std::vector<u8> data_app;
|
|
|
|
std::vector<u8> tmd;
|
|
|
|
std::vector<u8> decrypted_title_key;
|
|
|
|
|
|
|
|
if (wad.IsValid())
|
2011-03-11 10:21:46 +00:00
|
|
|
{
|
2011-05-09 05:47:29 +00:00
|
|
|
m_isWAD = true;
|
2015-12-19 18:46:01 +00:00
|
|
|
m_ticket = wad.GetTicket();
|
|
|
|
decrypted_title_key = GetKeyFromTicket(m_ticket);
|
|
|
|
tmd = wad.GetTMD();
|
|
|
|
data_app = wad.GetDataApp();
|
2009-03-07 08:35:01 +00:00
|
|
|
}
|
2011-05-09 05:47:29 +00:00
|
|
|
else
|
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
std::string tmd_filename(m_Path);
|
2011-07-18 01:47:55 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
if (tmd_filename.back() == '/')
|
|
|
|
tmd_filename += "title.tmd";
|
2011-05-09 05:47:29 +00:00
|
|
|
else
|
2015-12-19 18:46:01 +00:00
|
|
|
m_Path = tmd_filename.substr(0, tmd_filename.find("title.tmd"));
|
2011-07-18 01:47:55 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
File::IOFile tmd_file(tmd_filename, "rb");
|
|
|
|
if (!tmd_file)
|
2011-05-09 05:47:29 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
WARN_LOG(DISCIO, "CreateFromDirectory: error opening %s", tmd_filename.c_str());
|
2011-05-09 05:47:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-19 18:46:01 +00:00
|
|
|
|
|
|
|
tmd.resize(static_cast<size_t>(File::GetSize(tmd_filename)));
|
|
|
|
tmd_file.ReadBytes(tmd.data(), tmd.size());
|
2011-05-09 05:47:29 +00:00
|
|
|
}
|
2013-09-01 03:39:26 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
std::copy(&tmd[0], &tmd[TMD_HEADER_SIZE], m_TMDHeader);
|
|
|
|
std::copy(&tmd[0x180], &tmd[0x180 + TMD_VIEW_SIZE], m_TMDView);
|
2009-03-07 08:35:01 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
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);
|
2009-03-09 23:21:05 +00:00
|
|
|
|
2010-12-16 07:36:26 +00:00
|
|
|
if (m_Country == 2) // SYSMENU
|
2011-05-09 05:47:29 +00:00
|
|
|
m_Country = GetSysMenuRegion(m_TitleVersion);
|
2009-07-03 22:34:51 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
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)
|
|
|
|
{
|
2009-07-03 22:34:51 +00:00
|
|
|
m_Content.resize(m_numEntries);
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
std::array<u8, 16> iv;
|
|
|
|
u32 data_app_offset = 0;
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
for (u32 i = 0; i < m_numEntries; i++)
|
2009-07-03 22:34:51 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
const u32 entry_offset = 0x24 * i;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
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]));
|
2016-01-01 20:33:14 +00:00
|
|
|
|
|
|
|
const auto header_begin = std::next(tmd.begin(), entry_offset + 0x01E4);
|
|
|
|
const auto header_end = std::next(header_begin, ArraySize(content.m_Header));
|
|
|
|
std::copy(header_begin, header_end, content.m_Header);
|
|
|
|
|
|
|
|
const auto hash_begin = std::next(tmd.begin(), entry_offset + 0x01F4);
|
|
|
|
const auto hash_end = std::next(hash_begin, ArraySize(content.m_SHA1Hash));
|
|
|
|
std::copy(hash_begin, hash_end, content.m_SHA1Hash);
|
2013-04-17 03:14:36 +00:00
|
|
|
|
2011-05-09 05:47:29 +00:00
|
|
|
if (m_isWAD)
|
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
u32 rounded_size = ROUND_UP(content.m_Size, 0x40);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
iv.fill(0);
|
|
|
|
std::copy(&tmd[entry_offset + 0x01E8], &tmd[entry_offset + 0x01E8 + 2], iv.begin());
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
content.m_data = AESDecode(decrypted_title_key.data(), iv.data(), &data_app[data_app_offset], rounded_size);
|
|
|
|
|
|
|
|
data_app_offset += rounded_size;
|
2013-04-17 03:14:36 +00:00
|
|
|
continue;
|
2011-05-09 05:47:29 +00:00
|
|
|
}
|
2009-07-03 22:34:51 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
if (content.m_Type & 0x8000) // shared app
|
|
|
|
content.m_Filename = CSharedContent::AccessInstance().GetFilenameFromSHA1(content.m_SHA1Hash);
|
2009-07-03 22:34:51 +00:00
|
|
|
else
|
2015-12-19 18:46:01 +00:00
|
|
|
content.m_Filename = StringFromFormat("%s/%08x.app", m_Path.c_str(), content.m_ContentID);
|
2009-07-03 22:34:51 +00:00
|
|
|
|
2015-02-28 22:24:02 +00:00
|
|
|
// Be graceful about incorrect TMDs.
|
2015-12-19 18:46:01 +00:00
|
|
|
if (File::Exists(content.m_Filename))
|
|
|
|
content.m_Size = static_cast<u32>(File::GetSize(content.m_Filename));
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
2009-03-07 08:35:01 +00:00
|
|
|
}
|
2015-12-19 18:46:01 +00:00
|
|
|
|
|
|
|
std::vector<u8> CNANDContentLoader::AESDecode(const u8* key, u8* iv, const u8* src, u32 size)
|
2009-03-07 08:35:01 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
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());
|
2009-03-07 08:35:01 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
return buffer;
|
2009-03-07 08:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
std::vector<u8> CNANDContentLoader::GetKeyFromTicket(const std::vector<u8>& ticket)
|
2009-03-07 08:35:01 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
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);
|
2009-03-07 08:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-01 16:14:35 +00:00
|
|
|
DiscIO::IVolume::ECountry CNANDContentLoader::GetCountry() const
|
2009-03-14 19:13:21 +00:00
|
|
|
{
|
2009-07-03 22:34:51 +00:00
|
|
|
if (!IsValid())
|
|
|
|
return DiscIO::IVolume::COUNTRY_UNKNOWN;
|
|
|
|
|
2010-12-16 07:36:26 +00:00
|
|
|
return CountrySwitch(m_Country);
|
2009-07-03 22:34:51 +00:00
|
|
|
}
|
2009-03-07 08:35:01 +00:00
|
|
|
|
2009-03-13 16:15:15 +00:00
|
|
|
|
|
|
|
CNANDContentManager::~CNANDContentManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-06-21 17:19:52 +00:00
|
|
|
const CNANDContentLoader& CNANDContentManager::GetNANDLoader(const std::string& content_path)
|
2009-03-13 16:15:15 +00:00
|
|
|
{
|
2015-06-21 17:19:52 +00:00
|
|
|
auto it = m_map.find(content_path);
|
|
|
|
if (it != m_map.end())
|
|
|
|
return *it->second;
|
|
|
|
return *m_map.emplace_hint(it, std::make_pair(content_path, std::make_unique<CNANDContentLoader>(content_path)))->second;
|
2009-03-13 16:15:15 +00:00
|
|
|
}
|
|
|
|
|
2015-06-21 17:19:52 +00:00
|
|
|
const CNANDContentLoader& CNANDContentManager::GetNANDLoader(u64 title_id, Common::FromWhichRoot from)
|
2010-09-06 04:36:58 +00:00
|
|
|
{
|
2015-06-21 17:19:52 +00:00
|
|
|
std::string path = Common::GetTitleContentPath(title_id, from);
|
|
|
|
return GetNANDLoader(path);
|
2010-09-06 04:36:58 +00:00
|
|
|
}
|
2015-06-21 17:19:52 +00:00
|
|
|
|
|
|
|
bool CNANDContentManager::RemoveTitle(u64 title_id, Common::FromWhichRoot from)
|
2011-03-05 05:18:21 +00:00
|
|
|
{
|
2015-06-21 17:19:52 +00:00
|
|
|
auto& loader = GetNANDLoader(title_id, from);
|
|
|
|
if (!loader.IsValid())
|
2011-03-05 05:18:21 +00:00
|
|
|
return false;
|
2015-06-21 17:19:52 +00:00
|
|
|
loader.RemoveTitle();
|
|
|
|
return GetNANDLoader(title_id, from).IsValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNANDContentManager::ClearCache()
|
|
|
|
{
|
|
|
|
m_map.clear();
|
2011-03-05 05:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNANDContentLoader::RemoveTitle() const
|
|
|
|
{
|
|
|
|
INFO_LOG(DISCIO, "RemoveTitle %08x/%08x", (u32)(m_TitleID >> 32), (u32)m_TitleID);
|
2014-03-10 11:30:55 +00:00
|
|
|
if (IsValid())
|
2011-03-05 05:18:21 +00:00
|
|
|
{
|
2015-02-28 22:24:02 +00:00
|
|
|
// remove TMD?
|
2011-03-05 05:18:21 +00:00
|
|
|
for (u32 i = 0; i < m_numEntries; i++)
|
|
|
|
{
|
|
|
|
if (!(m_Content[i].m_Type & 0x8000)) // skip shared apps
|
|
|
|
{
|
2015-06-21 17:19:52 +00:00
|
|
|
std::string filename = StringFromFormat("%s/%08x.app", m_Path.c_str(), m_Content[i].m_ContentID);
|
2014-06-03 05:08:54 +00:00
|
|
|
INFO_LOG(DISCIO, "Delete %s", filename.c_str());
|
|
|
|
File::Delete(filename);
|
2011-03-05 05:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-21 17:19:52 +00:00
|
|
|
CNANDContentManager::Access().ClearCache(); // deletes 'this'
|
2011-03-05 05:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-06 04:36:58 +00:00
|
|
|
|
2011-06-05 07:42:40 +00:00
|
|
|
cUIDsys::cUIDsys()
|
2011-06-05 06:30:01 +00:00
|
|
|
{
|
2011-06-05 07:42:40 +00:00
|
|
|
UpdateLocation();
|
2011-06-05 06:30:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cUIDsys::UpdateLocation()
|
|
|
|
{
|
2011-06-05 07:42:40 +00:00
|
|
|
m_Elements.clear();
|
2014-09-01 19:48:02 +00:00
|
|
|
m_lastUID = 0x00001000;
|
2014-11-18 01:59:14 +00:00
|
|
|
m_uidSys = File::GetUserPath(D_SESSION_WIIROOT_IDX) + "/sys/uid.sys";
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-09-01 19:48:02 +00:00
|
|
|
File::IOFile pFile(m_uidSys, "rb");
|
2011-03-11 10:21:46 +00:00
|
|
|
SElement Element;
|
|
|
|
while (pFile.ReadArray(&Element, 1))
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2014-09-01 19:48:02 +00:00
|
|
|
*(u32*)&(Element.UID) = Common::swap32(m_lastUID++);
|
2011-03-11 10:21:46 +00:00
|
|
|
m_Elements.push_back(Element);
|
2010-05-13 04:50:18 +00:00
|
|
|
}
|
2011-03-11 10:21:46 +00:00
|
|
|
pFile.Close();
|
|
|
|
|
|
|
|
if (m_Elements.empty())
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2010-09-06 04:36:58 +00:00
|
|
|
*(u64*)&(Element.titleID) = Common::swap64(TITLEID_SYSMENU);
|
2014-09-01 19:48:02 +00:00
|
|
|
*(u32*)&(Element.UID) = Common::swap32(m_lastUID++);
|
2010-05-28 05:30:03 +00:00
|
|
|
|
2014-09-01 19:48:02 +00:00
|
|
|
File::CreateFullPath(m_uidSys);
|
|
|
|
pFile.Open(m_uidSys, "wb");
|
2011-03-11 10:21:46 +00:00
|
|
|
if (!pFile.WriteArray(&Element, 1))
|
2014-09-01 19:48:02 +00:00
|
|
|
ERROR_LOG(DISCIO, "Failed to write to %s", m_uidSys.c_str());
|
2010-05-13 04:50:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cUIDsys::~cUIDsys()
|
2011-06-05 07:42:40 +00:00
|
|
|
{}
|
2010-05-13 04:50:18 +00:00
|
|
|
|
|
|
|
u32 cUIDsys::GetUIDFromTitle(u64 _Title)
|
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& Element : m_Elements)
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
if (Common::swap64(_Title) == *(u64*)&(Element.titleID))
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
return Common::swap32(Element.UID);
|
2010-05-13 04:50:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-09 05:47:29 +00:00
|
|
|
void cUIDsys::AddTitle(u64 _TitleID)
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
|
|
|
if (GetUIDFromTitle(_TitleID))
|
|
|
|
{
|
2011-05-09 05:47:29 +00:00
|
|
|
INFO_LOG(DISCIO, "Title %08x%08x, already exists in uid.sys", (u32)(_TitleID >> 32), (u32)_TitleID);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-15 14:47:13 +00:00
|
|
|
|
2011-05-09 05:47:29 +00:00
|
|
|
SElement Element;
|
|
|
|
*(u64*)&(Element.titleID) = Common::swap64(_TitleID);
|
2014-09-01 19:48:02 +00:00
|
|
|
*(u32*)&(Element.UID) = Common::swap32(m_lastUID++);
|
2011-05-09 05:47:29 +00:00
|
|
|
m_Elements.push_back(Element);
|
2011-03-11 10:21:46 +00:00
|
|
|
|
2014-09-01 19:48:02 +00:00
|
|
|
File::CreateFullPath(m_uidSys);
|
|
|
|
File::IOFile pFile(m_uidSys, "ab");
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2012-03-31 03:07:11 +00:00
|
|
|
if (!pFile.WriteArray(&Element, 1))
|
2011-05-09 05:47:29 +00:00
|
|
|
ERROR_LOG(DISCIO, "fwrite failed");
|
2010-05-13 04:50:18 +00:00
|
|
|
}
|
|
|
|
|
2010-09-07 06:06:08 +00:00
|
|
|
void cUIDsys::GetTitleIDs(std::vector<u64>& _TitleIDs, bool _owned)
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& Element : m_Elements)
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2015-06-21 17:19:52 +00:00
|
|
|
if ((_owned && Common::CheckTitleTIK(Common::swap64(Element.titleID), Common::FROM_SESSION_ROOT)) ||
|
|
|
|
(!_owned && Common::CheckTitleTMD(Common::swap64(Element.titleID), Common::FROM_SESSION_ROOT)))
|
2013-10-29 05:09:01 +00:00
|
|
|
_TitleIDs.push_back(Common::swap64(Element.titleID));
|
2010-05-28 05:30:03 +00:00
|
|
|
}
|
2010-05-13 04:50:18 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
u64 CNANDContentManager::Install_WiiWAD(const std::string& filename)
|
2011-05-09 05:47:29 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
if (filename.find(".wad") == std::string::npos)
|
2011-05-09 05:47:29 +00:00
|
|
|
return 0;
|
2015-12-19 18:46:01 +00:00
|
|
|
const CNANDContentLoader& content_loader = GetNANDLoader(filename);
|
|
|
|
if (content_loader.IsValid() == false)
|
2011-05-09 05:47:29 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
u64 title_id = content_loader.GetTitleID();
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2015-02-28 22:24:02 +00:00
|
|
|
//copy WAD's TMD header and contents to content directory
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
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);
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
File::IOFile tmd_file(tmd_filename, "wb");
|
|
|
|
if (!tmd_file)
|
2011-05-09 05:47:29 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
PanicAlertT("WAD installation failed: error creating %s", tmd_filename.c_str());
|
2011-05-09 05:47:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
tmd_file.WriteBytes(content_loader.GetTMDHeader(), CNANDContentLoader::TMD_HEADER_SIZE);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
for (u32 i = 0; i < content_loader.GetContentSize(); i++)
|
2011-05-09 05:47:29 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
const SNANDContent& content = content_loader.GetContent()[i];
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
tmd_file.WriteBytes(content.m_Header, CNANDContentLoader::CONTENT_HEADER_SIZE);
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
std::string app_filename;
|
|
|
|
if (content.m_Type & 0x8000) //shared
|
|
|
|
app_filename = CSharedContent::AccessInstance().AddSharedContent(content.m_SHA1Hash);
|
2011-05-09 05:47:29 +00:00
|
|
|
else
|
2015-12-19 18:46:01 +00:00
|
|
|
app_filename = StringFromFormat("%s%08x.app", content_path.c_str(), content.m_ContentID);
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
if (!File::Exists(app_filename))
|
2011-05-09 05:47:29 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
File::CreateFullPath(app_filename);
|
|
|
|
File::IOFile app_file(app_filename, "wb");
|
|
|
|
if (!app_file)
|
2011-05-09 05:47:29 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
PanicAlertT("WAD installation failed: error creating %s", app_filename.c_str());
|
2011-05-09 05:47:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
app_file.WriteBytes(content.m_data.data(), content.m_Size);
|
2011-05-09 05:47:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
INFO_LOG(DISCIO, "Content %s already exists.", app_filename.c_str());
|
2011-05-09 05:47:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-22 01:20:22 +00:00
|
|
|
//Extract and copy WAD's ticket to ticket directory
|
2015-12-19 18:46:01 +00:00
|
|
|
if (!AddTicket(title_id, content_loader.GetTicket()))
|
2011-05-09 05:47:29 +00:00
|
|
|
{
|
2011-08-22 01:20:22 +00:00
|
|
|
PanicAlertT("WAD installation failed: error creating ticket");
|
|
|
|
return 0;
|
2011-05-09 05:47:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
cUIDsys::AccessInstance().AddTitle(title_id);
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2015-06-21 17:19:52 +00:00
|
|
|
ClearCache();
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
return title_id;
|
2011-05-09 05:47:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
bool AddTicket(u64 title_id, const std::vector<u8>& ticket)
|
2011-08-22 01:20:22 +00:00
|
|
|
{
|
2015-12-19 18:46:01 +00:00
|
|
|
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)
|
2011-08-22 01:20:22 +00:00
|
|
|
return false;
|
2015-12-19 18:46:01 +00:00
|
|
|
|
|
|
|
return ticket_file.WriteBytes(ticket.data(), ticket.size());
|
2011-08-22 01:20:22 +00:00
|
|
|
}
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2009-03-07 08:35:01 +00:00
|
|
|
} // namespace end
|
|
|
|
|