Merge pull request #3113 from shinyquagsire23/sdl-cia-install
SDL CIA Installation
This commit is contained in:
commit
b7cf793814
@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
#include "citra/config.h"
|
#include "citra/config.h"
|
||||||
#include "citra/emu_window/emu_window_sdl2.h"
|
#include "citra/emu_window/emu_window_sdl2.h"
|
||||||
|
#include "common/file_util.h"
|
||||||
#include "common/logging/backend.h"
|
#include "common/logging/backend.h"
|
||||||
#include "common/logging/filter.h"
|
#include "common/logging/filter.h"
|
||||||
#include "common/logging/log.h"
|
#include "common/logging/log.h"
|
||||||
@ -33,7 +34,9 @@
|
|||||||
#include "common/scope_exit.h"
|
#include "common/scope_exit.h"
|
||||||
#include "common/string_util.h"
|
#include "common/string_util.h"
|
||||||
#include "core/core.h"
|
#include "core/core.h"
|
||||||
|
#include "core/file_sys/cia_container.h"
|
||||||
#include "core/gdbstub/gdbstub.h"
|
#include "core/gdbstub/gdbstub.h"
|
||||||
|
#include "core/hle/service/am/am.h"
|
||||||
#include "core/loader/loader.h"
|
#include "core/loader/loader.h"
|
||||||
#include "core/settings.h"
|
#include "core/settings.h"
|
||||||
|
|
||||||
@ -41,6 +44,7 @@ static void PrintHelp(const char* argv0) {
|
|||||||
std::cout << "Usage: " << argv0
|
std::cout << "Usage: " << argv0
|
||||||
<< " [options] <filename>\n"
|
<< " [options] <filename>\n"
|
||||||
"-g, --gdbport=NUMBER Enable gdb stub on port NUMBER\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"
|
"-h, --help Display this help and exit\n"
|
||||||
"-v, --version Output version information 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[] = {
|
static struct option long_options[] = {
|
||||||
{"gdbport", required_argument, 0, 'g'},
|
{"gdbport", required_argument, 0, 'g'},
|
||||||
|
{"install", required_argument, 0, 'i'},
|
||||||
{"help", no_argument, 0, 'h'},
|
{"help", no_argument, 0, 'h'},
|
||||||
{"version", no_argument, 0, 'v'},
|
{"version", no_argument, 0, 'v'},
|
||||||
{0, 0, 0, 0},
|
{0, 0, 0, 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
while (optind < argc) {
|
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) {
|
if (arg != -1) {
|
||||||
switch (arg) {
|
switch (arg) {
|
||||||
case 'g':
|
case 'g':
|
||||||
@ -89,6 +94,17 @@ int main(int argc, char** argv) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
break;
|
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':
|
case 'h':
|
||||||
PrintHelp(argv[0]);
|
PrintHelp(argv[0]);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -29,7 +29,7 @@ enum TMDSignatureType : u32 {
|
|||||||
};
|
};
|
||||||
|
|
||||||
enum TMDContentTypeFlag : u16 {
|
enum TMDContentTypeFlag : u16 {
|
||||||
Encrypted = 1 << 1,
|
Encrypted = 1 << 0,
|
||||||
Disc = 1 << 2,
|
Disc = 1 << 2,
|
||||||
CFM = 1 << 3,
|
CFM = 1 << 3,
|
||||||
Optional = 1 << 14,
|
Optional = 1 << 14,
|
||||||
|
@ -10,9 +10,7 @@
|
|||||||
#include "common/file_util.h"
|
#include "common/file_util.h"
|
||||||
#include "common/logging/log.h"
|
#include "common/logging/log.h"
|
||||||
#include "common/string_util.h"
|
#include "common/string_util.h"
|
||||||
#include "core/file_sys/cia_container.h"
|
|
||||||
#include "core/file_sys/errors.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/ncch_container.h"
|
||||||
#include "core/file_sys/title_metadata.h"
|
#include "core/file_sys/title_metadata.h"
|
||||||
#include "core/hle/ipc.h"
|
#include "core/hle/ipc.h"
|
||||||
@ -23,7 +21,6 @@
|
|||||||
#include "core/hle/kernel/handle_table.h"
|
#include "core/hle/kernel/handle_table.h"
|
||||||
#include "core/hle/kernel/server_session.h"
|
#include "core/hle/kernel/server_session.h"
|
||||||
#include "core/hle/kernel/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.h"
|
||||||
#include "core/hle/service/am/am_app.h"
|
#include "core/hle/service/am/am_app.h"
|
||||||
#include "core/hle/service/am/am_net.h"
|
#include "core/hle/service/am/am_net.h"
|
||||||
@ -37,13 +34,15 @@
|
|||||||
namespace Service {
|
namespace Service {
|
||||||
namespace AM {
|
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_UPDATE = 0x0004000E;
|
||||||
constexpr u32 TID_HIGH_DLC = 0x0004008C;
|
constexpr u32 TID_HIGH_DLC = 0x0004008C;
|
||||||
|
|
||||||
// CIA installation static context variables
|
// CIA installation static context variables
|
||||||
static bool cia_installing = false;
|
static bool cia_installing = false;
|
||||||
static u64 cia_installing_tid;
|
|
||||||
static Service::FS::MediaType cia_installing_media_type;
|
|
||||||
|
|
||||||
static bool lists_initialized = false;
|
static bool lists_initialized = false;
|
||||||
static std::array<std::vector<u64_le>, 3> am_title_list;
|
static std::array<std::vector<u64_le>, 3> am_title_list;
|
||||||
@ -78,190 +77,277 @@ struct TicketInfo {
|
|||||||
|
|
||||||
static_assert(sizeof(TicketInfo) == 0x18, "Ticket info structure size is wrong");
|
static_assert(sizeof(TicketInfo) == 0x18, "Ticket info structure size is wrong");
|
||||||
|
|
||||||
// A file handled returned for CIAs to be written into and subsequently installed.
|
ResultVal<size_t> CIAFile::Read(u64 offset, size_t length, u8* buffer) const {
|
||||||
class CIAFile final : public FileSys::FileBackend {
|
UNIMPLEMENTED();
|
||||||
public:
|
return MakeResult<size_t>(length);
|
||||||
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::WriteTitleMetadata(u64 offset, size_t length, const u8* buffer) {
|
||||||
UNIMPLEMENTED();
|
container.LoadTitleMetadata(data, container.GetTitleMetadataOffset());
|
||||||
return MakeResult<size_t>(length);
|
FileSys::TitleMetadata tmd = container.GetTitleMetadata();
|
||||||
|
tmd.Print();
|
||||||
|
|
||||||
|
// If a TMD already exists for this app (ie 00000000.tmd), the incoming TMD
|
||||||
|
// will be the same plus one, (ie 00000001.tmd), both will be kept until
|
||||||
|
// the install is finalized and old contents can be discarded.
|
||||||
|
if (FileUtil::Exists(GetTitleMetadataPath(media_type, tmd.GetTitleID())))
|
||||||
|
is_update = true;
|
||||||
|
|
||||||
|
std::string tmd_path = GetTitleMetadataPath(media_type, tmd.GetTitleID(), is_update);
|
||||||
|
|
||||||
|
// Create content/ folder if it doesn't exist
|
||||||
|
std::string tmd_folder;
|
||||||
|
Common::SplitPath(tmd_path, &tmd_folder, nullptr, nullptr);
|
||||||
|
FileUtil::CreateFullPath(tmd_folder);
|
||||||
|
|
||||||
|
// Save TMD so that we can start getting new .app paths
|
||||||
|
if (tmd.Save(tmd_path) != Loader::ResultStatus::Success)
|
||||||
|
return FileSys::ERROR_INSUFFICIENT_SPACE;
|
||||||
|
|
||||||
|
// Create any other .app folders which may not exist yet
|
||||||
|
std::string app_folder;
|
||||||
|
Common::SplitPath(GetTitleContentPath(media_type, tmd.GetTitleID(),
|
||||||
|
FileSys::TMDContentIndex::Main, is_update),
|
||||||
|
&app_folder, nullptr, nullptr);
|
||||||
|
FileUtil::CreateFullPath(app_folder);
|
||||||
|
|
||||||
|
content_written.resize(container.GetTitleMetadata().GetContentCount());
|
||||||
|
install_state = CIAInstallState::TMDLoaded;
|
||||||
|
|
||||||
|
return MakeResult<size_t>(length);
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
|
u64 offset_max = offset + length;
|
||||||
|
for (int i = 0; i < container.GetTitleMetadata().GetContentCount(); i++) {
|
||||||
|
if (content_written[i] < container.GetContentSize(i)) {
|
||||||
|
// The size, minimum unwritten offset, and maximum unwritten offset of this content
|
||||||
|
u64 size = container.GetContentSize(i);
|
||||||
|
u64 range_min = container.GetContentOffset(i) + content_written[i];
|
||||||
|
u64 range_max = container.GetContentOffset(i) + size;
|
||||||
|
|
||||||
|
// The unwritten range for this content is beyond the buffered data we have
|
||||||
|
// or comes before the buffered data we have, so skip this content ID.
|
||||||
|
if (range_min > offset_max || range_max < offset)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Figure out how much of this content ID we have just recieved/can write out
|
||||||
|
u64 available_to_write = std::min(offset_max, range_max) - range_min;
|
||||||
|
|
||||||
|
// 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),
|
||||||
|
content_written[i] ? "ab" : "wb");
|
||||||
|
|
||||||
|
if (!file.IsOpen())
|
||||||
|
return FileSys::ERROR_INSUFFICIENT_SPACE;
|
||||||
|
|
||||||
|
file.WriteBytes(buffer + (range_min - offset), available_to_write);
|
||||||
|
|
||||||
|
// Keep tabs on how much of this content ID has been written so new range_min
|
||||||
|
// values can be calculated.
|
||||||
|
content_written[i] += available_to_write;
|
||||||
|
LOG_DEBUG(Service_AM, "Wrote %" PRIx64 " to content %u, total %" PRIx64,
|
||||||
|
available_to_write, i, content_written[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<size_t> WriteTitleMetadata(u64 offset, size_t length, const u8* buffer) {
|
return MakeResult<size_t>(length);
|
||||||
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
|
ResultVal<size_t> CIAFile::Write(u64 offset, size_t length, bool flush, const u8* buffer) {
|
||||||
// will be the same plus one, (ie 00000001.tmd), both will be kept until
|
written += length;
|
||||||
// the install is finalized and old contents can be discarded.
|
|
||||||
if (FileUtil::Exists(GetTitleMetadataPath(media_type, tmd.GetTitleID())))
|
|
||||||
is_update = true;
|
|
||||||
|
|
||||||
std::string tmd_path = GetTitleMetadataPath(media_type, tmd.GetTitleID(), is_update);
|
// TODO(shinyquagsire23): Can we assume that things will only be written in sequence?
|
||||||
|
// Does AM send an error if we write to things out of order?
|
||||||
|
// Or does it just ignore offsets and assume a set sequence of incoming data?
|
||||||
|
|
||||||
// Create content/ folder if it doesn't exist
|
// The data in CIAs is always stored CIA Header > Cert > Ticket > TMD > Content > Meta.
|
||||||
std::string tmd_folder;
|
// The CIA Header describes Cert, Ticket, TMD, total content sizes, and TMD is needed for
|
||||||
Common::SplitPath(tmd_path, &tmd_folder, nullptr, nullptr);
|
// content sizes so it ends up becoming a problem of keeping track of how much has been
|
||||||
FileUtil::CreateFullPath(tmd_folder);
|
// written and what we have been able to pick up.
|
||||||
|
if (install_state == CIAInstallState::InstallStarted) {
|
||||||
|
size_t buf_copy_size = std::min(length, FileSys::CIA_HEADER_SIZE);
|
||||||
|
size_t buf_max_size =
|
||||||
|
std::min(static_cast<size_t>(offset + length), FileSys::CIA_HEADER_SIZE);
|
||||||
|
data.resize(buf_max_size);
|
||||||
|
memcpy(data.data() + offset, buffer, buf_copy_size);
|
||||||
|
|
||||||
// Save TMD so that we can start getting new .app paths
|
// We have enough data to load a CIA header and parse it.
|
||||||
if (tmd.Save(tmd_path) != Loader::ResultStatus::Success)
|
if (written >= FileSys::CIA_HEADER_SIZE) {
|
||||||
return FileSys::ERROR_INSUFFICIENT_SPACE;
|
container.LoadHeader(data);
|
||||||
|
container.Print();
|
||||||
// Create any other .app folders which may not exist yet
|
install_state = CIAInstallState::HeaderLoaded;
|
||||||
std::string app_folder;
|
}
|
||||||
Common::SplitPath(GetTitleContentPath(media_type, tmd.GetTitleID(),
|
|
||||||
FileSys::TMDContentIndex::Main, is_update),
|
|
||||||
&app_folder, nullptr, nullptr);
|
|
||||||
FileUtil::CreateFullPath(app_folder);
|
|
||||||
|
|
||||||
content_written.resize(container.GetTitleMetadata().GetContentCount());
|
|
||||||
install_state = CIAInstallState::TMDLoaded;
|
|
||||||
|
|
||||||
return MakeResult<size_t>(length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<size_t> WriteContentData(u64 offset, size_t length, const u8* buffer) {
|
// If we don't have a header yet, we can't pull offsets of other sections
|
||||||
// Data is not being buffered, so we have to keep track of how much of each <ID>.app
|
if (install_state == CIAInstallState::InstallStarted)
|
||||||
// has been written since we might get a written buffer which contains multiple .app
|
|
||||||
// contents or only part of a larger .app's contents.
|
|
||||||
u64 offset_max = offset + length;
|
|
||||||
for (int i = 0; i < container.GetTitleMetadata().GetContentCount(); i++) {
|
|
||||||
if (content_written[i] < container.GetContentSize(i)) {
|
|
||||||
// The size, minimum unwritten offset, and maximum unwritten offset of this content
|
|
||||||
u64 size = container.GetContentSize(i);
|
|
||||||
u64 range_min = container.GetContentOffset(i) + content_written[i];
|
|
||||||
u64 range_max = container.GetContentOffset(i) + size;
|
|
||||||
|
|
||||||
// The unwritten range for this content is beyond the buffered data we have
|
|
||||||
// or comes before the buffered data we have, so skip this content ID.
|
|
||||||
if (range_min > offset_max || range_max < offset)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Figure out how much of this content ID we have just recieved/can write out
|
|
||||||
u64 available_to_write = std::min(offset_max, range_max) - range_min;
|
|
||||||
|
|
||||||
// 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),
|
|
||||||
content_written[i] ? "ab" : "wb");
|
|
||||||
|
|
||||||
if (!file.IsOpen())
|
|
||||||
return FileSys::ERROR_INSUFFICIENT_SPACE;
|
|
||||||
|
|
||||||
file.WriteBytes(buffer + (range_min - offset), available_to_write);
|
|
||||||
|
|
||||||
// Keep tabs on how much of this content ID has been written so new range_min
|
|
||||||
// values can be calculated.
|
|
||||||
content_written[i] += available_to_write;
|
|
||||||
LOG_DEBUG(Service_AM, "Wrote %" PRIx64 " to content %u, total %" PRIx64,
|
|
||||||
available_to_write, i, content_written[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return MakeResult<size_t>(length);
|
return MakeResult<size_t>(length);
|
||||||
|
|
||||||
|
// If we have been given data before (or including) .app content, pull it into
|
||||||
|
// our buffer, but only pull *up to* the content offset, no further.
|
||||||
|
if (offset < container.GetContentOffset()) {
|
||||||
|
size_t buf_loaded = data.size();
|
||||||
|
size_t copy_offset = std::max(static_cast<size_t>(offset), buf_loaded);
|
||||||
|
size_t buf_offset = buf_loaded - offset;
|
||||||
|
size_t buf_copy_size =
|
||||||
|
std::min(length, static_cast<size_t>(container.GetContentOffset() - offset)) -
|
||||||
|
buf_loaded;
|
||||||
|
size_t buf_max_size = std::min(offset + length, container.GetContentOffset());
|
||||||
|
data.resize(buf_max_size);
|
||||||
|
memcpy(data.data() + copy_offset, buffer + buf_offset, buf_copy_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override {
|
// TODO(shinyquagsire23): Write out .tik files to nand?
|
||||||
written += length;
|
|
||||||
|
|
||||||
// TODO(shinyquagsire23): Can we assume that things will only be written in sequence?
|
// The end of our TMD is at the beginning of Content data, so ensure we have that much
|
||||||
// Does AM send an error if we write to things out of order?
|
// buffered before trying to parse.
|
||||||
// Or does it just ignore offsets and assume a set sequence of incoming data?
|
if (written >= container.GetContentOffset() && install_state != CIAInstallState::TMDLoaded) {
|
||||||
|
auto result = WriteTitleMetadata(offset, length, buffer);
|
||||||
// The data in CIAs is always stored CIA Header > Cert > Ticket > TMD > Content > Meta.
|
|
||||||
// The CIA Header describes Cert, Ticket, TMD, total content sizes, and TMD is needed for
|
|
||||||
// content sizes so it ends up becoming a problem of keeping track of how much has been
|
|
||||||
// written and what we have been able to pick up.
|
|
||||||
if (install_state == CIAInstallState::InstallStarted) {
|
|
||||||
size_t buf_copy_size = std::min(length, FileSys::CIA_HEADER_SIZE);
|
|
||||||
size_t buf_max_size =
|
|
||||||
std::min(static_cast<size_t>(offset + length), FileSys::CIA_HEADER_SIZE);
|
|
||||||
data.resize(buf_max_size);
|
|
||||||
memcpy(data.data() + offset, buffer, buf_copy_size);
|
|
||||||
|
|
||||||
// We have enough data to load a CIA header and parse it.
|
|
||||||
if (written >= FileSys::CIA_HEADER_SIZE) {
|
|
||||||
container.LoadHeader(data);
|
|
||||||
container.Print();
|
|
||||||
install_state = CIAInstallState::HeaderLoaded;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we don't have a header yet, we can't pull offsets of other sections
|
|
||||||
if (install_state == CIAInstallState::InstallStarted)
|
|
||||||
return MakeResult<size_t>(length);
|
|
||||||
|
|
||||||
// If we have been given data before (or including) .app content, pull it into
|
|
||||||
// our buffer, but only pull *up to* the content offset, no further.
|
|
||||||
if (offset < container.GetContentOffset()) {
|
|
||||||
size_t buf_loaded = data.size();
|
|
||||||
size_t copy_offset = std::max(static_cast<size_t>(offset), buf_loaded);
|
|
||||||
size_t buf_offset = buf_loaded - offset;
|
|
||||||
size_t buf_copy_size =
|
|
||||||
std::min(length, static_cast<size_t>(container.GetContentOffset() - offset)) -
|
|
||||||
buf_loaded;
|
|
||||||
size_t buf_max_size = std::min(offset + length, container.GetContentOffset());
|
|
||||||
data.resize(buf_max_size);
|
|
||||||
memcpy(data.data() + copy_offset, buffer + buf_offset, buf_copy_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(shinyquagsire23): Write out .tik files to nand?
|
|
||||||
|
|
||||||
// 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) {
|
|
||||||
auto result = WriteTitleMetadata(offset, length, buffer);
|
|
||||||
if (result.Failed())
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Content data sizes can only be retrieved from TMD data
|
|
||||||
if (install_state != CIAInstallState::TMDLoaded)
|
|
||||||
return MakeResult<size_t>(length);
|
|
||||||
|
|
||||||
// From this point forward, data will no longer be buffered in data
|
|
||||||
auto result = WriteContentData(offset, length, buffer);
|
|
||||||
if (result.Failed())
|
if (result.Failed())
|
||||||
return result;
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Content data sizes can only be retrieved from TMD data
|
||||||
|
if (install_state != CIAInstallState::TMDLoaded)
|
||||||
return MakeResult<size_t>(length);
|
return MakeResult<size_t>(length);
|
||||||
|
|
||||||
|
// From this point forward, data will no longer be buffered in data
|
||||||
|
auto result = WriteContentData(offset, length, buffer);
|
||||||
|
if (result.Failed())
|
||||||
|
return result;
|
||||||
|
|
||||||
|
return MakeResult<size_t>(length);
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 CIAFile::GetSize() const {
|
||||||
|
return written;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 GetSize() const override {
|
// Install aborted
|
||||||
return written;
|
if (!complete) {
|
||||||
}
|
LOG_ERROR(Service_AM, "CIAFile closed prematurely, aborting install...");
|
||||||
|
FileUtil::DeleteDir(GetTitlePath(media_type, container.GetTitleMetadata().GetTitleID()));
|
||||||
bool SetSize(u64 size) const override {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Close() const override {
|
|
||||||
return true;
|
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:
|
old_tmd.Load(old_tmd_path);
|
||||||
// Whether it's installing an update, and what step of installation it is at
|
new_tmd.Load(new_tmd_path);
|
||||||
bool is_update = false;
|
|
||||||
CIAInstallState install_state = CIAInstallState::InstallStarted;
|
// 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;
|
FileSys::CIAContainer container;
|
||||||
std::vector<u8> data;
|
if (container.Load(path) == Loader::ResultStatus::Success) {
|
||||||
std::vector<u64> content_written;
|
Service::AM::CIAFile installFile(
|
||||||
Service::FS::MediaType media_type;
|
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 GetTitleMetadataPath(Service::FS::MediaType media_type, u64 tid, bool update) {
|
||||||
std::string content_path = GetTitlePath(media_type, tid) + "content/";
|
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));
|
file->ClientConnected(std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions));
|
||||||
|
|
||||||
cia_installing = true;
|
cia_installing = true;
|
||||||
cia_installing_media_type = media_type;
|
|
||||||
|
|
||||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
||||||
rb.Push(RESULT_SUCCESS); // No error
|
rb.Push(RESULT_SUCCESS); // No error
|
||||||
@ -842,38 +927,7 @@ void EndImportProgram(Service::Interface* self) {
|
|||||||
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x0405, 0, 2); // 0x04050002
|
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x0405, 0, 2); // 0x04050002
|
||||||
auto cia_handle = rp.PopHandle();
|
auto cia_handle = rp.PopHandle();
|
||||||
|
|
||||||
// Clean up older content data if we installed newer content on top
|
Kernel::g_handle_table.Close(cia_handle);
|
||||||
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);
|
|
||||||
}
|
|
||||||
ScanForAllTitles();
|
ScanForAllTitles();
|
||||||
|
|
||||||
cia_installing = false;
|
cia_installing = false;
|
||||||
|
@ -4,8 +4,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "common/common_types.h"
|
#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 Service {
|
||||||
namespace FS {
|
namespace FS {
|
||||||
@ -38,6 +42,66 @@ enum class CIAInstallState : u32 {
|
|||||||
ContentWritten,
|
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
|
* Get the .tmd path for a title
|
||||||
* @param media_type the media the title exists on
|
* @param media_type the media the title exists on
|
||||||
|
Loading…
Reference in New Issue
Block a user