Merge pull request #3113 from shinyquagsire23/sdl-cia-install

SDL CIA Installation
This commit is contained in:
James Rowe 2017-11-24 10:23:20 -07:00 committed by GitHub
commit b7cf793814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 333 additions and 199 deletions

View File

@ -26,6 +26,7 @@
#include "citra/config.h"
#include "citra/emu_window/emu_window_sdl2.h"
#include "common/file_util.h"
#include "common/logging/backend.h"
#include "common/logging/filter.h"
#include "common/logging/log.h"
@ -33,7 +34,9 @@
#include "common/scope_exit.h"
#include "common/string_util.h"
#include "core/core.h"
#include "core/file_sys/cia_container.h"
#include "core/gdbstub/gdbstub.h"
#include "core/hle/service/am/am.h"
#include "core/loader/loader.h"
#include "core/settings.h"
@ -41,6 +44,7 @@ static void PrintHelp(const char* argv0) {
std::cout << "Usage: " << argv0
<< " [options] <filename>\n"
"-g, --gdbport=NUMBER Enable gdb stub on port NUMBER\n"
"-i, --install=FILE Installs a specified CIA file\n"
"-h, --help Display this help and exit\n"
"-v, --version Output version information and exit\n";
}
@ -69,13 +73,14 @@ int main(int argc, char** argv) {
static struct option long_options[] = {
{"gdbport", required_argument, 0, 'g'},
{"install", required_argument, 0, 'i'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0},
};
while (optind < argc) {
char arg = getopt_long(argc, argv, "g:hv", long_options, &option_index);
char arg = getopt_long(argc, argv, "g:i:hv", long_options, &option_index);
if (arg != -1) {
switch (arg) {
case 'g':
@ -89,6 +94,17 @@ int main(int argc, char** argv) {
exit(1);
}
break;
case 'i': {
const auto cia_progress = [](size_t written, size_t total) {
LOG_INFO(Frontend, "%02zu%%", (written * 100 / total));
};
if (Service::AM::InstallCIA(std::string(optarg), cia_progress) !=
Service::AM::InstallStatus::Success)
errno = EINVAL;
if (errno != 0)
exit(1);
break;
}
case 'h':
PrintHelp(argv[0]);
return 0;

View File

@ -29,7 +29,7 @@ enum TMDSignatureType : u32 {
};
enum TMDContentTypeFlag : u16 {
Encrypted = 1 << 1,
Encrypted = 1 << 0,
Disc = 1 << 2,
CFM = 1 << 3,
Optional = 1 << 14,

View File

@ -10,9 +10,7 @@
#include "common/file_util.h"
#include "common/logging/log.h"
#include "common/string_util.h"
#include "core/file_sys/cia_container.h"
#include "core/file_sys/errors.h"
#include "core/file_sys/file_backend.h"
#include "core/file_sys/ncch_container.h"
#include "core/file_sys/title_metadata.h"
#include "core/hle/ipc.h"
@ -23,7 +21,6 @@
#include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/server_session.h"
#include "core/hle/kernel/session.h"
#include "core/hle/result.h"
#include "core/hle/service/am/am.h"
#include "core/hle/service/am/am_app.h"
#include "core/hle/service/am/am_net.h"
@ -37,13 +34,15 @@
namespace Service {
namespace AM {
constexpr u16 PLATFORM_CTR = 0x0004;
constexpr u16 CATEGORY_SYSTEM = 0x0010;
constexpr u16 CATEGORY_DLP = 0x0001;
constexpr u8 VARIATION_SYSTEM = 0x02;
constexpr u32 TID_HIGH_UPDATE = 0x0004000E;
constexpr u32 TID_HIGH_DLC = 0x0004008C;
// CIA installation static context variables
static bool cia_installing = false;
static u64 cia_installing_tid;
static Service::FS::MediaType cia_installing_media_type;
static bool lists_initialized = false;
static std::array<std::vector<u64_le>, 3> am_title_list;
@ -78,20 +77,14 @@ struct TicketInfo {
static_assert(sizeof(TicketInfo) == 0x18, "Ticket info structure size is wrong");
// A file handled returned for CIAs to be written into and subsequently installed.
class CIAFile final : public FileSys::FileBackend {
public:
explicit CIAFile(Service::FS::MediaType media_type) : media_type(media_type) {}
ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override {
ResultVal<size_t> CIAFile::Read(u64 offset, size_t length, u8* buffer) const {
UNIMPLEMENTED();
return MakeResult<size_t>(length);
}
}
ResultVal<size_t> WriteTitleMetadata(u64 offset, size_t length, const u8* buffer) {
ResultVal<size_t> CIAFile::WriteTitleMetadata(u64 offset, size_t length, const u8* buffer) {
container.LoadTitleMetadata(data, container.GetTitleMetadataOffset());
FileSys::TitleMetadata tmd = container.GetTitleMetadata();
cia_installing_tid = tmd.GetTitleID();
tmd.Print();
// If a TMD already exists for this app (ie 00000000.tmd), the incoming TMD
@ -122,9 +115,9 @@ public:
install_state = CIAInstallState::TMDLoaded;
return MakeResult<size_t>(length);
}
}
ResultVal<size_t> WriteContentData(u64 offset, size_t length, const u8* buffer) {
ResultVal<size_t> CIAFile::WriteContentData(u64 offset, size_t length, const u8* buffer) {
// Data is not being buffered, so we have to keep track of how much of each <ID>.app
// has been written since we might get a written buffer which contains multiple .app
// contents or only part of a larger .app's contents.
@ -147,8 +140,7 @@ public:
// Since the incoming TMD has already been written, we can use GetTitleContentPath
// to get the content paths to write to.
FileSys::TitleMetadata tmd = container.GetTitleMetadata();
FileUtil::IOFile file(
GetTitleContentPath(media_type, tmd.GetTitleID(), i, is_update),
FileUtil::IOFile file(GetTitleContentPath(media_type, tmd.GetTitleID(), i, is_update),
content_written[i] ? "ab" : "wb");
if (!file.IsOpen())
@ -165,9 +157,9 @@ public:
}
return MakeResult<size_t>(length);
}
}
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override {
ResultVal<size_t> CIAFile::Write(u64 offset, size_t length, bool flush, const u8* buffer) {
written += length;
// TODO(shinyquagsire23): Can we assume that things will only be written in sequence?
@ -215,8 +207,7 @@ public:
// The end of our TMD is at the beginning of Content data, so ensure we have that much
// buffered before trying to parse.
if (written >= container.GetContentOffset() &&
install_state != CIAInstallState::TMDLoaded) {
if (written >= container.GetContentOffset() && install_state != CIAInstallState::TMDLoaded) {
auto result = WriteTitleMetadata(offset, length, buffer);
if (result.Failed())
return result;
@ -232,36 +223,131 @@ public:
return result;
return MakeResult<size_t>(length);
}
}
u64 GetSize() const override {
u64 CIAFile::GetSize() const {
return written;
}
}
bool SetSize(u64 size) const override {
bool CIAFile::SetSize(u64 size) const {
return false;
}
bool CIAFile::Close() const {
bool complete = true;
for (size_t i = 0; i < container.GetTitleMetadata().GetContentCount(); i++) {
if (content_written[i] < container.GetContentSize(i))
complete = false;
}
bool Close() const override {
// Install aborted
if (!complete) {
LOG_ERROR(Service_AM, "CIAFile closed prematurely, aborting install...");
FileUtil::DeleteDir(GetTitlePath(media_type, container.GetTitleMetadata().GetTitleID()));
return true;
}
void Flush() const override {}
// Clean up older content data if we installed newer content on top
std::string old_tmd_path =
GetTitleMetadataPath(media_type, container.GetTitleMetadata().GetTitleID(), false);
std::string new_tmd_path =
GetTitleMetadataPath(media_type, container.GetTitleMetadata().GetTitleID(), true);
if (FileUtil::Exists(new_tmd_path) && old_tmd_path != new_tmd_path) {
FileSys::TitleMetadata old_tmd;
FileSys::TitleMetadata new_tmd;
private:
// Whether it's installing an update, and what step of installation it is at
bool is_update = false;
CIAInstallState install_state = CIAInstallState::InstallStarted;
old_tmd.Load(old_tmd_path);
new_tmd.Load(new_tmd_path);
// For each content ID in the old TMD, check if there is a matching ID in the new
// TMD. If a CIA contains (and wrote to) an identical ID, it should be kept while
// IDs which only existed for the old TMD should be deleted.
for (u16 old_index = 0; old_index < old_tmd.GetContentCount(); old_index++) {
bool abort = false;
for (u16 new_index = 0; new_index < new_tmd.GetContentCount(); new_index++) {
if (old_tmd.GetContentIDByIndex(old_index) ==
new_tmd.GetContentIDByIndex(new_index)) {
abort = true;
}
}
if (abort)
break;
FileUtil::Delete(GetTitleContentPath(media_type, old_tmd.GetTitleID(), old_index));
}
FileUtil::Delete(old_tmd_path);
}
return true;
}
void CIAFile::Flush() const {}
InstallStatus InstallCIA(const std::string& path,
std::function<ProgressCallback>&& update_callback) {
LOG_INFO(Service_AM, "Installing %s...", path.c_str());
if (!FileUtil::Exists(path)) {
LOG_ERROR(Service_AM, "File %s does not exist!", path.c_str());
return InstallStatus::ErrorFileNotFound;
}
// How much has been written total, CIAContainer for the installing CIA, buffer of all data
// prior to content data, how much of each content index has been written, and where the CIA
// is being installed to
u64 written = 0;
FileSys::CIAContainer container;
std::vector<u8> data;
std::vector<u64> content_written;
Service::FS::MediaType media_type;
};
if (container.Load(path) == Loader::ResultStatus::Success) {
Service::AM::CIAFile installFile(
Service::AM::GetTitleMediaType(container.GetTitleMetadata().GetTitleID()));
for (size_t i = 0; i < container.GetTitleMetadata().GetContentCount(); i++) {
if (container.GetTitleMetadata().GetContentTypeByIndex(i) &
FileSys::TMDContentTypeFlag::Encrypted) {
LOG_ERROR(Service_AM, "File %s is encrypted! Aborting...", path.c_str());
return InstallStatus::ErrorEncrypted;
}
}
FileUtil::IOFile file(path, "rb");
if (!file.IsOpen())
return InstallStatus::ErrorFailedToOpenFile;
std::array<u8, 0x10000> buffer;
size_t total_bytes_read = 0;
while (total_bytes_read != file.GetSize()) {
size_t bytes_read = file.ReadBytes(buffer.data(), buffer.size());
auto result = installFile.Write(static_cast<u64>(total_bytes_read), bytes_read, true,
static_cast<u8*>(buffer.data()));
if (update_callback)
update_callback(total_bytes_read, file.GetSize());
if (result.Failed()) {
LOG_ERROR(Service_AM, "CIA file installation aborted with error code %08x",
result.Code());
return InstallStatus::ErrorAborted;
}
total_bytes_read += bytes_read;
}
installFile.Close();
LOG_INFO(Service_AM, "Installed %s successfully.", path.c_str());
return InstallStatus::Success;
}
LOG_ERROR(Service_AM, "CIA file %s is invalid!", path.c_str());
return InstallStatus::ErrorInvalid;
}
Service::FS::MediaType GetTitleMediaType(u64 titleId) {
u16 platform = static_cast<u16>(titleId >> 48);
u16 category = static_cast<u16>((titleId >> 32) & 0xFFFF);
u8 variation = static_cast<u8>(titleId & 0xFF);
if (platform != PLATFORM_CTR)
return Service::FS::MediaType::NAND;
if (category & CATEGORY_SYSTEM || category & CATEGORY_DLP || variation & VARIATION_SYSTEM)
return Service::FS::MediaType::NAND;
return Service::FS::MediaType::SDMC;
}
std::string GetTitleMetadataPath(Service::FS::MediaType media_type, u64 tid, bool update) {
std::string content_path = GetTitlePath(media_type, tid) + "content/";
@ -827,7 +913,6 @@ void BeginImportProgram(Service::Interface* self) {
file->ClientConnected(std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions));
cia_installing = true;
cia_installing_media_type = media_type;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(RESULT_SUCCESS); // No error
@ -842,38 +927,7 @@ void EndImportProgram(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x0405, 0, 2); // 0x04050002
auto cia_handle = rp.PopHandle();
// Clean up older content data if we installed newer content on top
std::string old_tmd_path =
GetTitleMetadataPath(cia_installing_media_type, cia_installing_tid, false);
std::string new_tmd_path =
GetTitleMetadataPath(cia_installing_media_type, cia_installing_tid, true);
if (FileUtil::Exists(new_tmd_path) && old_tmd_path != new_tmd_path) {
FileSys::TitleMetadata old_tmd;
FileSys::TitleMetadata new_tmd;
old_tmd.Load(old_tmd_path);
new_tmd.Load(new_tmd_path);
// For each content ID in the old TMD, check if there is a matching ID in the new
// TMD. If a CIA contains (and wrote to) an identical ID, it should be kept while
// IDs which only existed for the old TMD should be deleted.
for (u16 old_index = 0; old_index < old_tmd.GetContentCount(); old_index++) {
bool abort = false;
for (u16 new_index = 0; new_index < new_tmd.GetContentCount(); new_index++) {
if (old_tmd.GetContentIDByIndex(old_index) ==
new_tmd.GetContentIDByIndex(new_index)) {
abort = true;
}
}
if (abort)
break;
FileUtil::Delete(
GetTitleContentPath(cia_installing_media_type, old_tmd.GetTitleID(), old_index));
}
FileUtil::Delete(old_tmd_path);
}
Kernel::g_handle_table.Close(cia_handle);
ScanForAllTitles();
cia_installing = false;

View File

@ -4,8 +4,12 @@
#pragma once
#include <functional>
#include <string>
#include "common/common_types.h"
#include "core/file_sys/cia_container.h"
#include "core/file_sys/file_backend.h"
#include "core/hle/result.h"
namespace Service {
namespace FS {
@ -38,6 +42,66 @@ enum class CIAInstallState : u32 {
ContentWritten,
};
enum class InstallStatus : u32 {
Success,
ErrorFailedToOpenFile,
ErrorFileNotFound,
ErrorAborted,
ErrorInvalid,
ErrorEncrypted,
};
// Progress callback for InstallCIA, recieves bytes written and total bytes
using ProgressCallback = void(size_t, size_t);
// A file handled returned for CIAs to be written into and subsequently installed.
class CIAFile final : public FileSys::FileBackend {
public:
explicit CIAFile(Service::FS::MediaType media_type) : media_type(media_type) {}
~CIAFile() {
Close();
}
ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
ResultVal<size_t> WriteTitleMetadata(u64 offset, size_t length, const u8* buffer);
ResultVal<size_t> WriteContentData(u64 offset, size_t length, const u8* buffer);
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;
u64 GetSize() const override;
bool SetSize(u64 size) const override;
bool Close() const override;
void Flush() const override;
private:
// Whether it's installing an update, and what step of installation it is at
bool is_update = false;
CIAInstallState install_state = CIAInstallState::InstallStarted;
// How much has been written total, CIAContainer for the installing CIA, buffer of all data
// prior to content data, how much of each content index has been written, and where the CIA
// is being installed to
u64 written = 0;
FileSys::CIAContainer container;
std::vector<u8> data;
std::vector<u64> content_written;
Service::FS::MediaType media_type;
};
/**
* Installs a CIA file from a specified file path.
* @param path file path of the CIA file to install
* @param update_callback callback function called during filesystem write
* @returns bool whether the install was successful
*/
InstallStatus InstallCIA(const std::string& path,
std::function<ProgressCallback>&& update_callback = nullptr);
/**
* Get the mediatype for an installed title
* @param titleId the installed title ID
* @returns MediaType which the installed title will reside on
*/
Service::FS::MediaType GetTitleMediaType(u64 titleId);
/**
* Get the .tmd path for a title
* @param media_type the media the title exists on