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
|
|
|
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "DiscIO/NANDContentLoader.h"
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
2017-02-11 07:57:47 +00:00
|
|
|
#include <cinttypes>
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
#include "Common/Align.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-02-12 10:50:35 +00:00
|
|
|
#include "Common/Crypto/AES.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Common/Logging/Log.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"
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "Common/Swap.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "DiscIO/WiiWad.h"
|
2009-03-07 08:35:01 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2016-09-10 04:38:04 +00:00
|
|
|
CNANDContentData::~CNANDContentData() = default;
|
|
|
|
|
2017-02-10 21:46:38 +00:00
|
|
|
CSharedContent::CSharedContent(Common::FromWhichRoot root) : m_root(root)
|
2009-03-09 17:09:27 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_Elements.clear();
|
|
|
|
m_LastID = 0;
|
2017-02-10 21:46:38 +00:00
|
|
|
m_ContentMap = Common::RootUserPath(root) + "/shared1/content.map";
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
File::IOFile pFile(m_ContentMap, "rb");
|
|
|
|
SElement Element;
|
|
|
|
while (pFile.ReadArray(&Element, 1))
|
|
|
|
{
|
|
|
|
m_Elements.push_back(Element);
|
|
|
|
m_LastID++;
|
|
|
|
}
|
2009-03-09 17:09:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 21:46:38 +00:00
|
|
|
std::string CSharedContent::GetFilenameFromSHA1(const u8* hash) const
|
2009-03-09 17:09:27 +00:00
|
|
|
{
|
2017-02-10 21:46:38 +00:00
|
|
|
for (const auto& Element : m_Elements)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
if (memcmp(hash, Element.SHA1Hash, 20) == 0)
|
|
|
|
{
|
|
|
|
return StringFromFormat(
|
2017-02-10 21:46:38 +00:00
|
|
|
"%s/shared1/%c%c%c%c%c%c%c%c.app", Common::RootUserPath(m_root).c_str(),
|
2016-06-24 08:43:46 +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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "unk";
|
2009-03-09 17:09:27 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:44:24 +00:00
|
|
|
std::string CSharedContent::AddSharedContent(const u8* hash)
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string filename = GetFilenameFromSHA1(hash);
|
2014-06-03 05:08:54 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (strcasecmp(filename.c_str(), "unk") == 0)
|
|
|
|
{
|
|
|
|
std::string id = StringFromFormat("%08x", m_LastID);
|
|
|
|
SElement Element;
|
|
|
|
memcpy(Element.FileName, id.c_str(), 8);
|
|
|
|
memcpy(Element.SHA1Hash, hash, 20);
|
|
|
|
m_Elements.push_back(Element);
|
2010-12-15 14:47:13 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
File::CreateFullPath(m_ContentMap);
|
2011-03-11 10:21:46 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
File::IOFile pFile(m_ContentMap, "ab");
|
|
|
|
pFile.WriteArray(&Element, 1);
|
2011-03-11 10:21:46 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
filename =
|
2017-02-10 21:46:38 +00:00
|
|
|
StringFromFormat("%s/shared1/%s.app", Common::RootUserPath(m_root).c_str(), id.c_str());
|
2016-06-24 08:43:46 +00:00
|
|
|
m_LastID++;
|
|
|
|
}
|
2014-06-03 05:08:54 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return filename;
|
2010-05-13 04:50:18 +00:00
|
|
|
}
|
2009-03-09 17:09:27 +00:00
|
|
|
|
2016-10-15 00:12:16 +00:00
|
|
|
CNANDContentDataFile::CNANDContentDataFile(const std::string& filename) : m_filename{filename}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CNANDContentDataFile::~CNANDContentDataFile() = default;
|
|
|
|
|
2016-03-20 12:09:21 +00:00
|
|
|
void CNANDContentDataFile::EnsureOpen()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_file)
|
|
|
|
m_file = std::make_unique<File::IOFile>(m_filename, "rb");
|
|
|
|
else if (!m_file->IsOpen())
|
|
|
|
m_file->Open(m_filename, "rb");
|
2016-03-20 12:09:21 +00:00
|
|
|
}
|
|
|
|
void CNANDContentDataFile::Open()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
EnsureOpen();
|
2016-03-20 12:09:21 +00:00
|
|
|
}
|
2016-09-14 23:15:27 +00:00
|
|
|
std::vector<u8> CNANDContentDataFile::Get()
|
2016-03-16 19:08:37 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
EnsureOpen();
|
2016-09-14 23:41:38 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_file->IsGood())
|
2016-09-14 23:41:38 +00:00
|
|
|
return {};
|
2016-03-16 19:08:37 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
u64 size = m_file->GetSize();
|
|
|
|
if (size == 0)
|
2016-09-14 23:41:38 +00:00
|
|
|
return {};
|
2016-03-16 19:08:37 +00:00
|
|
|
|
2016-09-14 23:41:38 +00:00
|
|
|
std::vector<u8> result(size);
|
2016-06-24 08:43:46 +00:00
|
|
|
m_file->ReadBytes(result.data(), result.size());
|
2016-03-16 19:08:37 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return result;
|
2016-03-16 19:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNANDContentDataFile::GetRange(u32 start, u32 size, u8* buffer)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
EnsureOpen();
|
|
|
|
if (!m_file->IsGood())
|
|
|
|
return false;
|
2016-03-16 19:08:37 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_file->Seek(start, SEEK_SET))
|
|
|
|
return false;
|
2016-03-16 19:08:37 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return m_file->ReadBytes(buffer, static_cast<size_t>(size));
|
2016-03-20 12:09:21 +00:00
|
|
|
}
|
|
|
|
void CNANDContentDataFile::Close()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_file && m_file->IsOpen())
|
|
|
|
m_file->Close();
|
2016-03-16 19:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNANDContentDataBuffer::GetRange(u32 start, u32 size, u8* buffer)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (start + size > m_buffer.size())
|
|
|
|
return false;
|
2016-03-16 19:08:37 +00:00
|
|
|
|
2017-01-23 20:49:26 +00:00
|
|
|
std::copy_n(&m_buffer[start], size, buffer);
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
2016-03-16 19:08:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:44:24 +00:00
|
|
|
CNANDContentLoader::CNANDContentLoader(const std::string& content_name)
|
2009-03-07 08:35:01 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_Valid = Initialize(content_name);
|
2009-03-07 08:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CNANDContentLoader::~CNANDContentLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-14 12:15:02 +00:00
|
|
|
bool CNANDContentLoader::IsValid() const
|
|
|
|
{
|
2017-03-01 22:58:38 +00:00
|
|
|
return m_Valid;
|
2017-02-14 12:15:02 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 00:03:22 +00:00
|
|
|
const SNANDContent* CNANDContentLoader::GetContentByID(u32 id) const
|
|
|
|
{
|
|
|
|
const auto iterator = std::find_if(m_Content.begin(), m_Content.end(), [id](const auto& content) {
|
|
|
|
return content.m_metadata.id == id;
|
|
|
|
});
|
|
|
|
return iterator != m_Content.end() ? &*iterator : nullptr;
|
|
|
|
}
|
|
|
|
|
2015-12-19 18:46:01 +00:00
|
|
|
const SNANDContent* CNANDContentLoader::GetContentByIndex(int index) const
|
2009-03-07 08:35:01 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
for (auto& Content : m_Content)
|
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
if (Content.m_metadata.index == index)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
return &Content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (name.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_Path = name;
|
|
|
|
|
|
|
|
WiiWAD wad(name);
|
|
|
|
std::vector<u8> data_app;
|
|
|
|
|
|
|
|
if (wad.IsValid())
|
|
|
|
{
|
|
|
|
m_IsWAD = true;
|
2017-02-09 13:07:36 +00:00
|
|
|
m_ticket = wad.GetTicket();
|
2017-02-11 07:57:47 +00:00
|
|
|
m_tmd = wad.GetTMD();
|
2016-06-24 08:43:46 +00:00
|
|
|
data_app = wad.GetDataApp();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string tmd_filename(m_Path);
|
|
|
|
|
|
|
|
if (tmd_filename.back() == '/')
|
|
|
|
tmd_filename += "title.tmd";
|
|
|
|
else
|
|
|
|
m_Path = tmd_filename.substr(0, tmd_filename.find("title.tmd"));
|
|
|
|
|
|
|
|
File::IOFile tmd_file(tmd_filename, "rb");
|
|
|
|
if (!tmd_file)
|
|
|
|
{
|
|
|
|
WARN_LOG(DISCIO, "CreateFromDirectory: error opening %s", tmd_filename.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
std::vector<u8> bytes(File::GetSize(tmd_filename));
|
|
|
|
tmd_file.ReadBytes(bytes.data(), bytes.size());
|
|
|
|
m_tmd.SetBytes(std::move(bytes));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
m_ticket = FindSignedTicket(m_tmd.GetTitleId());
|
|
|
|
}
|
2017-02-09 13:07:36 +00:00
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
InitializeContentEntries(data_app);
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
2015-12-19 18:46:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
void CNANDContentLoader::InitializeContentEntries(const std::vector<u8>& data_app)
|
2015-12-19 18:46:01 +00:00
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
if (!m_ticket.IsValid())
|
|
|
|
{
|
|
|
|
ERROR_LOG(IOS_ES, "No valid ticket for title %016" PRIx64, m_tmd.GetTitleId());
|
|
|
|
return;
|
|
|
|
}
|
2009-07-03 22:34:51 +00:00
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
const std::vector<IOS::ES::Content> contents = m_tmd.GetContents();
|
|
|
|
m_Content.resize(contents.size());
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
u32 data_app_offset = 0;
|
|
|
|
const std::vector<u8> title_key = m_ticket.GetTitleKey();
|
2017-02-10 21:46:38 +00:00
|
|
|
CSharedContent shared_content{Common::FromWhichRoot::FROM_SESSION_ROOT};
|
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
for (size_t i = 0; i < contents.size(); ++i)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
const auto& content = contents.at(i);
|
2013-04-17 03:14:36 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_IsWAD)
|
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
// The content index is used as IV (2 bytes); the remaining 14 bytes are zeroes.
|
|
|
|
std::array<u8, 16> iv{};
|
|
|
|
iv[0] = static_cast<u8>(content.index >> 8) & 0xFF;
|
|
|
|
iv[1] = static_cast<u8>(content.index) & 0xFF;
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
u32 rounded_size = Common::AlignUp(static_cast<u32>(content.size), 0x40);
|
2015-12-19 18:46:01 +00:00
|
|
|
|
2017-02-12 10:50:35 +00:00
|
|
|
m_Content[i].m_Data = std::make_unique<CNANDContentDataBuffer>(Common::AES::Decrypt(
|
2017-02-11 07:57:47 +00:00
|
|
|
title_key.data(), iv.data(), &data_app[data_app_offset], rounded_size));
|
2016-06-24 08:43:46 +00:00
|
|
|
data_app_offset += rounded_size;
|
|
|
|
}
|
|
|
|
else
|
2017-02-11 07:57:47 +00:00
|
|
|
{
|
|
|
|
std::string filename;
|
2017-02-28 16:24:02 +00:00
|
|
|
if (content.IsShared())
|
2017-02-11 07:57:47 +00:00
|
|
|
filename = shared_content.GetFilenameFromSHA1(content.sha1.data());
|
|
|
|
else
|
|
|
|
filename = StringFromFormat("%s/%08x.app", m_Path.c_str(), content.id);
|
2016-03-16 19:08:37 +00:00
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
m_Content[i].m_Data = std::make_unique<CNANDContentDataFile>(filename);
|
|
|
|
}
|
2009-07-03 22:34:51 +00:00
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
m_Content[i].m_metadata = std::move(content);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2009-03-07 08:35:01 +00:00
|
|
|
}
|
2015-12-19 18:46: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
|
|
|
{
|
2016-06-24 08:43:46 +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
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
const CNANDContentLoader& CNANDContentManager::GetNANDLoader(u64 title_id,
|
|
|
|
Common::FromWhichRoot from)
|
2010-09-06 04:36:58 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +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
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
auto& loader = GetNANDLoader(title_id, from);
|
|
|
|
if (!loader.IsValid())
|
|
|
|
return false;
|
|
|
|
loader.RemoveTitle();
|
|
|
|
return GetNANDLoader(title_id, from).IsValid();
|
2015-06-21 17:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNANDContentManager::ClearCache()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_map.clear();
|
2011-03-05 05:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNANDContentLoader::RemoveTitle() const
|
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
const u64 title_id = m_tmd.GetTitleId();
|
|
|
|
INFO_LOG(DISCIO, "RemoveTitle %08x/%08x", (u32)(title_id >> 32), (u32)title_id);
|
2016-06-24 08:43:46 +00:00
|
|
|
if (IsValid())
|
|
|
|
{
|
|
|
|
// remove TMD?
|
2017-02-11 07:57:47 +00:00
|
|
|
for (const auto& content : m_Content)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-02-28 16:24:02 +00:00
|
|
|
if (!content.m_metadata.IsShared())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
std::string path = StringFromFormat("%s/%08x.app", m_Path.c_str(), content.m_metadata.id);
|
|
|
|
INFO_LOG(DISCIO, "Delete %s", path.c_str());
|
|
|
|
File::Delete(path);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CNANDContentManager::Access().ClearCache(); // deletes 'this'
|
|
|
|
}
|
2011-03-05 05:18:21 +00:00
|
|
|
}
|
2010-09-06 04:36:58 +00:00
|
|
|
|
2017-02-08 20:37:12 +00:00
|
|
|
cUIDsys::cUIDsys(Common::FromWhichRoot root)
|
2011-06-05 06:30:01 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_Elements.clear();
|
|
|
|
m_LastUID = 0x00001000;
|
2017-02-08 20:37:12 +00:00
|
|
|
m_UidSys = Common::RootUserPath(root) + "/sys/uid.sys";
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
File::IOFile pFile(m_UidSys, "rb");
|
|
|
|
SElement Element;
|
|
|
|
while (pFile.ReadArray(&Element, 1))
|
|
|
|
{
|
|
|
|
*(u32*)&(Element.UID) = Common::swap32(m_LastUID++);
|
|
|
|
m_Elements.push_back(Element);
|
|
|
|
}
|
|
|
|
pFile.Close();
|
|
|
|
|
|
|
|
if (m_Elements.empty())
|
|
|
|
{
|
|
|
|
*(u64*)&(Element.titleID) = Common::swap64(TITLEID_SYSMENU);
|
|
|
|
*(u32*)&(Element.UID) = Common::swap32(m_LastUID++);
|
|
|
|
|
|
|
|
File::CreateFullPath(m_UidSys);
|
|
|
|
pFile.Open(m_UidSys, "wb");
|
|
|
|
if (!pFile.WriteArray(&Element, 1))
|
|
|
|
ERROR_LOG(DISCIO, "Failed to write to %s", m_UidSys.c_str());
|
|
|
|
}
|
2010-05-13 04:50:18 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:44:24 +00:00
|
|
|
u32 cUIDsys::GetUIDFromTitle(u64 title_id)
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
for (auto& Element : m_Elements)
|
|
|
|
{
|
|
|
|
if (Common::swap64(title_id) == *(u64*)&(Element.titleID))
|
|
|
|
{
|
|
|
|
return Common::swap32(Element.UID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2010-05-13 04:50:18 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:44:24 +00:00
|
|
|
void cUIDsys::AddTitle(u64 title_id)
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (GetUIDFromTitle(title_id))
|
|
|
|
{
|
|
|
|
INFO_LOG(DISCIO, "Title %08x%08x, already exists in uid.sys", (u32)(title_id >> 32),
|
|
|
|
(u32)title_id);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-15 14:47:13 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
SElement Element;
|
|
|
|
*(u64*)&(Element.titleID) = Common::swap64(title_id);
|
|
|
|
*(u32*)&(Element.UID) = Common::swap32(m_LastUID++);
|
|
|
|
m_Elements.push_back(Element);
|
2011-03-11 10:21:46 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
File::CreateFullPath(m_UidSys);
|
|
|
|
File::IOFile pFile(m_UidSys, "ab");
|
2011-05-09 05:47:29 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!pFile.WriteArray(&Element, 1))
|
|
|
|
ERROR_LOG(DISCIO, "fwrite failed");
|
2010-05-13 04:50:18 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:44:24 +00:00
|
|
|
void cUIDsys::GetTitleIDs(std::vector<u64>& title_ids, bool owned)
|
2010-05-13 04:50:18 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
for (auto& Element : m_Elements)
|
|
|
|
{
|
|
|
|
if ((owned &&
|
|
|
|
Common::CheckTitleTIK(Common::swap64(Element.titleID), Common::FROM_SESSION_ROOT)) ||
|
|
|
|
(!owned &&
|
|
|
|
Common::CheckTitleTMD(Common::swap64(Element.titleID), Common::FROM_SESSION_ROOT)))
|
|
|
|
title_ids.push_back(Common::swap64(Element.titleID));
|
|
|
|
}
|
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
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (filename.find(".wad") == std::string::npos)
|
|
|
|
return 0;
|
|
|
|
const CNANDContentLoader& content_loader = GetNANDLoader(filename);
|
|
|
|
if (content_loader.IsValid() == false)
|
|
|
|
return 0;
|
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
const u64 title_id = content_loader.GetTMD().GetTitleId();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// copy WAD's TMD header and contents to content directory
|
|
|
|
|
|
|
|
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 tmd_file(tmd_filename, "wb");
|
|
|
|
if (!tmd_file)
|
|
|
|
{
|
|
|
|
PanicAlertT("WAD installation failed: error creating %s", tmd_filename.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
const auto& raw_tmd = content_loader.GetTMD().GetRawTMD();
|
|
|
|
tmd_file.WriteBytes(raw_tmd.data(), raw_tmd.size());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-02-10 21:46:38 +00:00
|
|
|
CSharedContent shared_content{Common::FromWhichRoot::FROM_CONFIGURED_ROOT};
|
2017-02-11 07:57:47 +00:00
|
|
|
for (const auto& content : content_loader.GetContent())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
std::string app_filename;
|
2017-02-28 16:24:02 +00:00
|
|
|
if (content.m_metadata.IsShared())
|
2017-02-11 07:57:47 +00:00
|
|
|
app_filename = shared_content.AddSharedContent(content.m_metadata.sha1.data());
|
2016-06-24 08:43:46 +00:00
|
|
|
else
|
2017-02-11 07:57:47 +00:00
|
|
|
app_filename = StringFromFormat("%s%08x.app", content_path.c_str(), content.m_metadata.id);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
if (!File::Exists(app_filename))
|
|
|
|
{
|
|
|
|
File::CreateFullPath(app_filename);
|
|
|
|
File::IOFile app_file(app_filename, "wb");
|
|
|
|
if (!app_file)
|
|
|
|
{
|
|
|
|
PanicAlertT("WAD installation failed: error creating %s", app_filename.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
app_file.WriteBytes(content.m_Data->Get().data(), content.m_metadata.size);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
INFO_LOG(DISCIO, "Content %s already exists.", app_filename.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract and copy WAD's ticket to ticket directory
|
2017-01-01 21:09:57 +00:00
|
|
|
if (!AddTicket(content_loader.GetTicket()))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
PanicAlertT("WAD installation failed: error creating ticket");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-08 20:37:12 +00:00
|
|
|
cUIDsys uid_sys{Common::FromWhichRoot::FROM_CONFIGURED_ROOT};
|
|
|
|
uid_sys.AddTitle(title_id);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
ClearCache();
|
|
|
|
|
|
|
|
return title_id;
|
2011-05-09 05:47:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
bool AddTicket(const IOS::ES::TicketReader& signed_ticket)
|
2011-08-22 01:20:22 +00:00
|
|
|
{
|
2017-02-09 13:07:36 +00:00
|
|
|
if (!signed_ticket.IsValid())
|
2017-01-01 21:09:57 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-09 13:07:36 +00:00
|
|
|
|
|
|
|
u64 title_id = signed_ticket.GetTitleId();
|
2017-01-01 21:09:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string ticket_filename = Common::GetTicketFileName(title_id, Common::FROM_CONFIGURED_ROOT);
|
|
|
|
File::CreateFullPath(ticket_filename);
|
2015-12-19 18:46:01 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
File::IOFile ticket_file(ticket_filename, "wb");
|
|
|
|
if (!ticket_file)
|
|
|
|
return false;
|
2015-12-19 18:46:01 +00:00
|
|
|
|
2017-02-09 13:07:36 +00:00
|
|
|
const std::vector<u8>& raw_ticket = signed_ticket.GetRawTicket();
|
|
|
|
return ticket_file.WriteBytes(raw_ticket.data(), raw_ticket.size());
|
2017-01-01 23:59:21 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
IOS::ES::TicketReader FindSignedTicket(u64 title_id)
|
2017-01-01 23:59:21 +00:00
|
|
|
{
|
|
|
|
std::string ticket_filename = Common::GetTicketFileName(title_id, Common::FROM_CONFIGURED_ROOT);
|
|
|
|
File::IOFile ticket_file(ticket_filename, "rb");
|
|
|
|
if (!ticket_file)
|
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
return IOS::ES::TicketReader{};
|
2017-01-01 23:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> signed_ticket(ticket_file.GetSize());
|
|
|
|
if (!ticket_file.ReadBytes(signed_ticket.data(), signed_ticket.size()))
|
|
|
|
{
|
2017-02-11 07:57:47 +00:00
|
|
|
return IOS::ES::TicketReader{};
|
2017-01-01 23:59:21 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 07:57:47 +00:00
|
|
|
return IOS::ES::TicketReader{std::move(signed_ticket)};
|
2017-01-02 04:32:08 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
} // namespace end
|