From 1edbbf7f8ca3c8e83d0ab38e448e2a2bbffec7c4 Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Tue, 14 Nov 2017 14:57:44 -0700 Subject: [PATCH 1/8] Services/AM: Add GetTitleMediaType --- src/core/hle/service/am/am.cpp | 18 ++++++++++++++++++ src/core/hle/service/am/am.h | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 30c4828b8..c3d6bf392 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -37,6 +37,10 @@ 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; @@ -263,6 +267,20 @@ private: Service::FS::MediaType media_type; }; +Service::FS::MediaType GetTitleMediaType(u64 titleId) { + u16 platform = static_cast(titleId >> 48); + u16 category = static_cast((titleId >> 32) & 0xFFFF); + u8 variation = static_cast(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/"; diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index 04493d943..4cbd090b5 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -38,6 +38,13 @@ enum class CIAInstallState : u32 { ContentWritten, }; +/** + * 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 From 750e7e06b15c7af56791b3a9ceada602e9f5aa5d Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Tue, 14 Nov 2017 15:31:19 -0700 Subject: [PATCH 2/8] Services/AM: Expose CIAFile class in AM header --- src/core/hle/service/am/am.cpp | 303 +++++++++++++++------------------ src/core/hle/service/am/am.h | 32 ++++ 2 files changed, 171 insertions(+), 164 deletions(-) diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index c3d6bf392..0ce8f0971 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -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" @@ -82,190 +79,168 @@ 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 CIAFile::Read(u64 offset, size_t length, u8* buffer) const { + UNIMPLEMENTED(); + return MakeResult(length); +} - ResultVal Read(u64 offset, size_t length, u8* buffer) const override { - UNIMPLEMENTED(); - return MakeResult(length); +ResultVal 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 + // 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(length); +} + +ResultVal 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 .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 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(); + return MakeResult(length); +} - // 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; +ResultVal CIAFile::Write(u64 offset, size_t length, bool flush, const u8* buffer) { + written += length; - 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 - std::string tmd_folder; - Common::SplitPath(tmd_path, &tmd_folder, nullptr, nullptr); - FileUtil::CreateFullPath(tmd_folder); + // 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(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 - 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(length); + // 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; + } } - ResultVal 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 .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]); - } - } - + // If we don't have a header yet, we can't pull offsets of other sections + if (install_state == CIAInstallState::InstallStarted) return MakeResult(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(offset), buf_loaded); + size_t buf_offset = buf_loaded - offset; + size_t buf_copy_size = + std::min(length, static_cast(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 Write(u64 offset, size_t length, bool flush, const u8* buffer) override { - written += length; + // TODO(shinyquagsire23): Write out .tik files to nand? - // 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? - - // 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(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(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(offset), buf_loaded); - size_t buf_offset = buf_loaded - offset; - size_t buf_copy_size = - std::min(length, static_cast(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(length); - - // From this point forward, data will no longer be buffered in data - auto result = WriteContentData(offset, length, buffer); + // 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(length); - } - u64 GetSize() const override { - return written; - } + // From this point forward, data will no longer be buffered in data + auto result = WriteContentData(offset, length, buffer); + if (result.Failed()) + return result; - bool SetSize(u64 size) const override { - return false; - } + return MakeResult(length); +} - bool Close() const override { - return true; - } +u64 CIAFile::GetSize() const { + return written; +} - void Flush() const override {} +bool CIAFile::SetSize(u64 size) const { + return false; +} -private: - // Whether it's installing an update, and what step of installation it is at - bool is_update = false; - CIAInstallState install_state = CIAInstallState::InstallStarted; +bool CIAFile::Close() const { + return true; +} - // 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 data; - std::vector content_written; - Service::FS::MediaType media_type; -}; +void CIAFile::Flush() const {} Service::FS::MediaType GetTitleMediaType(u64 titleId) { u16 platform = static_cast(titleId >> 48); diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index 4cbd090b5..20ac52475 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -6,6 +6,9 @@ #include #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 +41,35 @@ enum class CIAInstallState : u32 { ContentWritten, }; +// 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 Read(u64 offset, size_t length, u8* buffer) const override; + ResultVal WriteTitleMetadata(u64 offset, size_t length, const u8* buffer); + ResultVal WriteContentData(u64 offset, size_t length, const u8* buffer); + ResultVal 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 data; + std::vector content_written; + Service::FS::MediaType media_type; +}; + /** * Get the mediatype for an installed title * @param titleId the installed title ID From 1f2aa29dee09116360b23492759a21ed1123e566 Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Tue, 14 Nov 2017 16:18:52 -0700 Subject: [PATCH 3/8] Services/AM: Move CIA finalization into Close(), Close() on destruction of class --- src/core/hle/service/am/am.cpp | 81 +++++++++++++++++++--------------- src/core/hle/service/am/am.h | 3 ++ 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 0ce8f0971..db3576ce4 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -43,8 +43,6 @@ 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, 3> am_title_list; @@ -87,7 +85,6 @@ ResultVal CIAFile::Read(u64 offset, size_t length, u8* buffer) const { ResultVal 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 @@ -237,6 +234,50 @@ bool CIAFile::SetSize(u64 size) const { } 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; + } + + // Install aborted + if (!complete) { + LOG_ERROR(Service_AM, "CIAFile closed prematurely, aborting install..."); + FileUtil::DeleteDir(GetTitlePath(media_type, container.GetTitleMetadata().GetTitleID())); + return true; + } + + // 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; + + 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; } @@ -820,7 +861,6 @@ void BeginImportProgram(Service::Interface* self) { file->ClientConnected(std::get>(sessions)); cia_installing = true; - cia_installing_media_type = media_type; IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); rb.Push(RESULT_SUCCESS); // No error @@ -835,38 +875,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; diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index 20ac52475..8199046a7 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -45,6 +45,9 @@ enum class CIAInstallState : u32 { class CIAFile final : public FileSys::FileBackend { public: explicit CIAFile(Service::FS::MediaType media_type) : media_type(media_type) {} + ~CIAFile() { + Close(); + } ResultVal Read(u64 offset, size_t length, u8* buffer) const override; ResultVal WriteTitleMetadata(u64 offset, size_t length, const u8* buffer); From 56e906f1e3a63525dceffe41717b566674325f34 Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Wed, 15 Nov 2017 12:01:49 -0700 Subject: [PATCH 4/8] Services/AM: Add InstallCIA function for frontends --- src/core/hle/service/am/am.cpp | 43 ++++++++++++++++++++++++++++++++++ src/core/hle/service/am/am.h | 13 ++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index db3576ce4..b03e06421 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -283,6 +283,49 @@ bool CIAFile::Close() const { void CIAFile::Flush() const {} +bool InstallCIA(const std::string& path, std::function&& 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 false; + } + + FileSys::CIAContainer container; + if (container.Load(path) == Loader::ResultStatus::Success) { + Service::AM::CIAFile installFile( + Service::AM::GetTitleMediaType(container.GetTitleMetadata().GetTitleID())); + + FileUtil::IOFile file(path, "rb"); + if (!file.IsOpen()) + return false; + + std::array 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(total_bytes_read), bytes_read, true, + static_cast(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 false; + } + total_bytes_read += bytes_read; + } + installFile.Close(); + + LOG_INFO(Service_AM, "Installed %s successfully.", path.c_str()); + return true; + } + + LOG_ERROR(Service_AM, "CIA file %s is invalid!", path.c_str()); + return false; +} + Service::FS::MediaType GetTitleMediaType(u64 titleId) { u16 platform = static_cast(titleId >> 48); u16 category = static_cast((titleId >> 32) & 0xFFFF); diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index 8199046a7..7deb09113 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "common/common_types.h" #include "core/file_sys/cia_container.h" @@ -41,6 +42,9 @@ enum class CIAInstallState : u32 { ContentWritten, }; +// 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: @@ -73,6 +77,15 @@ private: 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 + */ +bool InstallCIA(const std::string& path, + std::function&& update_callback = nullptr); + /** * Get the mediatype for an installed title * @param titleId the installed title ID From 553ca2bfe03a315c60ff7c803a93e3498711d53f Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Tue, 14 Nov 2017 14:59:18 -0700 Subject: [PATCH 5/8] SDL: Add command-line CIA installs --- src/citra/citra.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp index e524c5535..a29f8275e 100644 --- a/src/citra/citra.cpp +++ b/src/citra/citra.cpp @@ -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] \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,16 @@ 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)) + errno = EINVAL; + if (errno != 0) + exit(1); + break; + } case 'h': PrintHelp(argv[0]); return 0; From 253954930f52ffcd21b5c7c41a9ce69c35187fa6 Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Sun, 19 Nov 2017 21:56:02 -0700 Subject: [PATCH 6/8] Services/AM: Return InstallStatus for InstallCIA --- src/citra/citra.cpp | 3 ++- src/core/hle/service/am/am.cpp | 13 +++++++------ src/core/hle/service/am/am.h | 13 +++++++++++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp index a29f8275e..01a70ea39 100644 --- a/src/citra/citra.cpp +++ b/src/citra/citra.cpp @@ -98,7 +98,8 @@ int main(int argc, char** argv) { 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)) + if (Service::AM::InstallCIA(std::string(optarg), cia_progress) != + Service::AM::InstallStatus::Success) errno = EINVAL; if (errno != 0) exit(1); diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index b03e06421..b677ef6b4 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -283,12 +283,13 @@ bool CIAFile::Close() const { void CIAFile::Flush() const {} -bool InstallCIA(const std::string& path, std::function&& update_callback) { +InstallStatus InstallCIA(const std::string& path, + std::function&& 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 false; + return InstallStatus::ErrorFileNotFound; } FileSys::CIAContainer container; @@ -298,7 +299,7 @@ bool InstallCIA(const std::string& path, std::function&& updat FileUtil::IOFile file(path, "rb"); if (!file.IsOpen()) - return false; + return InstallStatus::ErrorFailedToOpenFile; std::array buffer; size_t total_bytes_read = 0; @@ -312,18 +313,18 @@ bool InstallCIA(const std::string& path, std::function&& updat if (result.Failed()) { LOG_ERROR(Service_AM, "CIA file installation aborted with error code %08x", result.Code()); - return false; + return InstallStatus::ErrorAborted; } total_bytes_read += bytes_read; } installFile.Close(); LOG_INFO(Service_AM, "Installed %s successfully.", path.c_str()); - return true; + return InstallStatus::Success; } LOG_ERROR(Service_AM, "CIA file %s is invalid!", path.c_str()); - return false; + return InstallStatus::ErrorInvalid; } Service::FS::MediaType GetTitleMediaType(u64 titleId) { diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index 7deb09113..d4d834172 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -42,6 +42,15 @@ 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); @@ -83,8 +92,8 @@ private: * @param update_callback callback function called during filesystem write * @returns bool whether the install was successful */ -bool InstallCIA(const std::string& path, - std::function&& update_callback = nullptr); +InstallStatus InstallCIA(const std::string& path, + std::function&& update_callback = nullptr); /** * Get the mediatype for an installed title From c0eb233bf1f847b291ede20ffa054776a1994eee Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Mon, 20 Nov 2017 20:45:06 -0700 Subject: [PATCH 7/8] file_sys/title_metadata: Fix encrypted bitmask --- src/core/file_sys/title_metadata.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/file_sys/title_metadata.h b/src/core/file_sys/title_metadata.h index 08de6424b..1d2302e0e 100644 --- a/src/core/file_sys/title_metadata.h +++ b/src/core/file_sys/title_metadata.h @@ -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, From 70ff3c72f18585781990ad00ed70c6cfc51061aa Mon Sep 17 00:00:00 2001 From: shinyquagsire23 Date: Mon, 20 Nov 2017 20:45:21 -0700 Subject: [PATCH 8/8] Services/AM: Detect encrypted CIAs --- src/core/hle/service/am/am.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index b677ef6b4..da6acdd80 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -297,6 +297,14 @@ InstallStatus InstallCIA(const std::string& path, 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;