From 34acb80635430aac31f949808dc3134119c04644 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Fri, 18 Mar 2022 20:11:26 -0700 Subject: [PATCH] ncm: update client code to better reflect latest sysupdate --- .../stratosphere/kvdb/kvdb_bounded_string.hpp | 100 ++-- .../kvdb/kvdb_file_key_value_cache.hpp | 2 +- .../kvdb/kvdb_memory_key_value_store.hpp | 8 +- .../stratosphere/ncm/ncm_content_meta.hpp | 19 + .../ncm/ncm_content_meta_extended_data.hpp | 473 ++++++++++++++++++ .../ncm/ncm_content_meta_utils.hpp | 9 +- .../stratosphere/ncm/ncm_mapped_memory.hpp | 54 ++ .../source/gc/impl/gc_gc_crypto.cpp | 4 +- .../source/kvdb/kvdb_file_key_value_store.cpp | 6 +- .../source/ncm/ncm_content_info_utils.cpp | 1 + .../ncm/ncm_content_management_utils.cpp | 4 +- .../source/ncm/ncm_content_manager_impl.cpp | 4 +- .../source/ncm/ncm_content_meta.cpp | 113 ++++- .../source/ncm/ncm_content_meta_utils.cpp | 106 +++- .../source/ncm/ncm_content_storage_impl.cpp | 10 +- .../source/ncm/ncm_extended_data_mapper.hpp | 429 ++++++++++++++++ .../source/ncm/ncm_file_mapper_file.hpp | 123 +++++ .../source/ncm/ncm_fs_utils.hpp | 6 + .../source/ncm/ncm_install_task_base.cpp | 24 +- .../source/ncm/ncm_make_path.cpp | 14 +- .../ncm/ncm_package_install_task_base.cpp | 10 +- .../ncm/ncm_package_system_update_task.cpp | 2 +- .../source/ncm/ncm_placeholder_accessor.cpp | 2 +- .../ncm_read_only_content_storage_impl.cpp | 4 +- .../pgl/srv/pgl_srv_shell_host_utils.cpp | 2 +- .../include/vapours/results/ncm_results.hpp | 8 + .../crypto/impl/crypto_cbc_mac_impl.cpp | 1 + .../source/sysupdater/sysupdater_service.cpp | 4 +- 28 files changed, 1432 insertions(+), 110 deletions(-) create mode 100644 libraries/libstratosphere/include/stratosphere/ncm/ncm_mapped_memory.hpp create mode 100644 libraries/libstratosphere/source/ncm/ncm_extended_data_mapper.hpp create mode 100644 libraries/libstratosphere/source/ncm/ncm_file_mapper_file.hpp diff --git a/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_bounded_string.hpp b/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_bounded_string.hpp index 92935c736..39615c21a 100644 --- a/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_bounded_string.hpp +++ b/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_bounded_string.hpp @@ -24,11 +24,6 @@ namespace ams::kvdb { static_assert(N > 0, "BoundedString requires non-zero backing buffer!"); private: char m_buffer[N]; - private: - /* Utility. */ - static inline void CheckLength(size_t len) { - AMS_ABORT_UNLESS(len < N); - } public: /* Constructors. */ constexpr BoundedString() { @@ -36,7 +31,8 @@ namespace ams::kvdb { } explicit constexpr BoundedString(const char *s) { - this->Set(s); + AMS_ABORT_UNLESS(static_cast(util::Strnlen(s, N)) < N); + util::Strlcpy(m_buffer, s, N); } /* Static constructors. */ @@ -44,102 +40,114 @@ namespace ams::kvdb { return BoundedString(s); } - static constexpr BoundedString MakeFormat(const char *format, ...) __attribute__((format (printf, 1, 2))) { + static BoundedString MakeFormat(const char *format, ...) __attribute__((format (printf, 1, 2))) { BoundedString string; - std::va_list args; - va_start(args, format); - CheckLength(util::VSNPrintf(string.m_buffer, N, format, args)); - string.m_buffer[N - 1] = 0; - va_end(args); + std::va_list vl; + va_start(vl, format); + AMS_ABORT_UNLESS(static_cast(util::VSNPrintf(string.m_buffer, N, format, vl)) < N); + va_end(vl); return string; } /* Getters. */ - size_t GetLength() const { + constexpr size_t GetLength() const { return util::Strnlen(m_buffer, N); } - const char *Get() const { + constexpr const char *Get() const { return m_buffer; } - operator const char *() const { + constexpr operator const char *() const { return m_buffer; } - /* Setters. */ - void Set(const char *s) { - /* Ensure string can fit in our buffer. */ - CheckLength(util::Strnlen(s, N)); - std::strncpy(m_buffer, s, N); - m_buffer[N - 1] = 0; + /* Assignment. */ + constexpr BoundedString &Assign(const char *s) { + AMS_ABORT_UNLESS(static_cast(util::Strnlen(s, N)) < N); + util::Strlcpy(m_buffer, s, N); + + return *this; } - void SetFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) { - /* Format into the buffer, abort if too large. */ - std::va_list args; - va_start(args, format); - CheckLength(util::VSNPrintf(m_buffer, N, format, args)); - va_end(args); + BoundedString &AssignFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) { + std::va_list vl; + va_start(vl, format); + AMS_ABORT_UNLESS(static_cast(util::VSNPrintf(m_buffer, N, format, vl)) < N); + va_end(vl); + + return *this; } /* Append to existing. */ - void Append(const char *s) { + BoundedString &Append(const char *s) { const size_t length = GetLength(); - CheckLength(length + util::Strnlen(s, N)); + AMS_ABORT_UNLESS(length + static_cast(util::Strnlen(s, N)) < N); std::strncat(m_buffer, s, N - length - 1); + + return *this; } - void Append(char c) { + BoundedString &Append(char c) { const size_t length = GetLength(); - CheckLength(length + 1); + AMS_ABORT_UNLESS(length + 1 < N); m_buffer[length] = c; m_buffer[length + 1] = 0; + + return *this; } - void AppendFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) { + BoundedString &AppendFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) { const size_t length = GetLength(); - std::va_list args; - va_start(args, format); - CheckLength(util::VSNPrintf(m_buffer + length, N - length, format, args) + length); - va_end(args); + + std::va_list vl; + va_start(vl, format); + AMS_ABORT_UNLESS(static_cast(util::VSNPrintf(m_buffer + length, N - length, format, vl)) < static_cast(N - length)); + va_end(vl); + + return *this; } /* Substring utilities. */ - void GetSubstring(char *dst, size_t dst_size, size_t offset, size_t length) const { + void GetSubString(char *dst, size_t dst_size, size_t offset, size_t length) const { /* Make sure output buffer can hold the substring. */ AMS_ABORT_UNLESS(offset + length <= GetLength()); AMS_ABORT_UNLESS(dst_size > length); + /* Copy substring to dst. */ std::strncpy(dst, m_buffer + offset, length); dst[length] = 0; } - BoundedString GetSubstring(size_t offset, size_t length) const { + BoundedString MakeSubString(size_t offset, size_t length) const { BoundedString string; - GetSubstring(string.m_buffer, N, offset, length); + GetSubString(string.m_buffer, N, offset, length); return string; } /* Comparison. */ + constexpr bool Equals(const char *s, size_t offset = 0) const { + if (std::is_constant_evaluated()) { + return util::Strncmp(m_buffer + offset, s, N - offset) == 0; + } else { + return std::strncmp(m_buffer + offset, s, N - offset) == 0; + } + } + constexpr bool operator==(const BoundedString &rhs) const { - return std::strncmp(m_buffer, rhs.m_buffer, N) == 0; + return this->Equals(rhs.m_buffer); } constexpr bool operator!=(const BoundedString &rhs) const { return !(*this == rhs); } - bool EndsWith(const char *s, size_t offset) const { - return std::strncmp(m_buffer + offset, s, N - offset) == 0; - } - - bool EndsWith(const char *s) const { + constexpr bool EqualsPostfix(const char *s) const { const size_t suffix_length = util::Strnlen(s, N); const size_t length = GetLength(); - return suffix_length <= length && EndsWith(s, length - suffix_length); + return suffix_length <= length && this->Equals(s, length - suffix_length); } }; diff --git a/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_file_key_value_cache.hpp b/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_file_key_value_cache.hpp index 0a7be09b5..9cd6fed6b 100644 --- a/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_file_key_value_cache.hpp +++ b/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_file_key_value_cache.hpp @@ -77,7 +77,7 @@ namespace ams::kvdb { /* Setup member variables. */ m_keys = static_cast(buf); - m_file_path.Set(path); + m_file_path.Assign(path); std::memset(m_keys, 0, BufferSize); /* Open file. */ diff --git a/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_memory_key_value_store.hpp b/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_memory_key_value_store.hpp index d6be4466e..9498e9037 100644 --- a/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_memory_key_value_store.hpp +++ b/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_memory_key_value_store.hpp @@ -269,8 +269,8 @@ namespace ams::kvdb { R_UNLESS(entry_type == fs::DirectoryEntryType_Directory, fs::ResultPathNotFound()); /* Set paths. */ - m_path.SetFormat("%s%s", dir, "/imkvdb.arc"); - m_temp_path.SetFormat("%s%s", dir, "/imkvdb.tmp"); + m_path.AssignFormat("%s%s", dir, "/imkvdb.arc"); + m_temp_path.AssignFormat("%s%s", dir, "/imkvdb.tmp"); /* Initialize our index. */ R_TRY(m_index.Initialize(capacity, mr)); @@ -282,8 +282,8 @@ namespace ams::kvdb { Result Initialize(size_t capacity, MemoryResource *mr) { /* This initializes without an archive file. */ /* A store initialized this way cannot have its contents loaded from or flushed to disk. */ - m_path.Set(""); - m_temp_path.Set(""); + m_path.Assign(""); + m_temp_path.Assign(""); /* Initialize our index. */ R_TRY(m_index.Initialize(capacity, mr)); diff --git a/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta.hpp b/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta.hpp index eb5cd327b..e660bb147 100644 --- a/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta.hpp +++ b/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta.hpp @@ -335,6 +335,10 @@ namespace ams::ncm { size_t CalculateConvertContentMetaSize() const; void ConvertToContentMeta(void *dst, size_t size, const ContentInfo &meta); + size_t CalculateConvertFragmentOnlyInstallContentMetaSize(s32 fragment_count) const { + return CalculateSizeImpl(this->GetExtendedHeaderSize(), fragment_count + 1, 0, 0, false); + } + Result CalculateConvertFragmentOnlyInstallContentMetaSize(size_t *out_size, u32 source_version) const; Result ConvertToFragmentOnlyInstallContentMeta(void *dst, size_t size, const InstallContentInfo &content_info, u32 source_version); @@ -343,6 +347,10 @@ namespace ams::ncm { static constexpr size_t CalculateSize(ContentMetaType type, size_t content_count, size_t content_meta_count, size_t extended_data_size) { return ContentMetaAccessor::CalculateSize(type, content_count, content_meta_count, extended_data_size, true); } + + size_t GetExtendedDataOffset() const { + return this->GetExtendedDataAddress() - reinterpret_cast(this->GetData()); + } }; class InstallContentMetaReader : public ContentMetaAccessor { @@ -368,4 +376,15 @@ namespace ams::ncm { using ContentMetaAccessor::SetStorageId; }; + class PatchMetaExtendedDataAccessor; + struct PatchDeltaHeader; + class AutoBuffer; + + class MetaConverter { + public: + static Result CountContentExceptForMeta(s32 *out, PatchMetaExtendedDataAccessor *accessor, const PatchDeltaHeader &header, s32 delta_index); + static Result FindDeltaIndex(s32 *out, PatchMetaExtendedDataAccessor *accessor, u32 source_version, u32 destination_version); + static Result GetFragmentOnlyInstallContentMeta(AutoBuffer *out, const InstallContentInfo &content_info, const PackagedContentMetaReader &reader, PatchMetaExtendedDataAccessor *accessor, u32 source_version); + }; + } diff --git a/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta_extended_data.hpp b/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta_extended_data.hpp index 505d9e6b9..9c60a600f 100644 --- a/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta_extended_data.hpp +++ b/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta_extended_data.hpp @@ -18,6 +18,7 @@ #include #include #include +#include namespace ams::ncm { @@ -381,4 +382,476 @@ namespace ams::ncm { constexpr SystemUpdateMetaExtendedDataReader(const void *data, size_t size) : SystemUpdateMetaExtendedDataReaderWriterBase(data, size) { /* ... */ } }; + template + class ReadableStructPin; + + class AccessorBase { + public: + template + class PinBase { + private: + AccessorBase *m_accessor; + u64 m_pin_id; + T *m_data; + size_t m_size; + public: + PinBase() : m_accessor(nullptr), m_data(nullptr), m_size(0) { + /* ... */ + } + + PinBase(const PinBase &) = delete; + PinBase &operator=(const PinBase &) = delete; + + PinBase(PinBase &&rhs) : m_accessor(rhs.m_accessor), m_pin_id(rhs.m_pin_id), m_data(rhs.m_data), m_size(rhs.m_size) { + rhs.m_accessor = nullptr; + } + + PinBase &operator=(PinBase &&rhs) { + m_accessor = rhs.m_accessor; + m_pin_id = rhs.m_pin_id; + m_data = rhs.m_data; + m_size = rhs.m_size; + rhs.m_accessor = nullptr; + + return *this; + } + + virtual ~PinBase() { + this->Reset(); + } + public: + void Reset() { + if (m_accessor != nullptr) { + m_accessor->ReleasePin(m_pin_id); + m_accessor = nullptr; + } + } + + void Reset(AccessorBase *accessor, u64 pin_id, void *data, size_t size) { + AMS_ASSERT(data != nullptr || size == 0); + + this->Reset(); + + m_accessor = accessor; + m_pin_id = pin_id; + m_data = reinterpret_cast(data); + m_size = size; + } + + T *GetData() const { + return m_data; + } + + size_t GetDataSize() const { + return m_size; + } + }; + private: + IMapper *m_mapper; + public: + AccessorBase(IMapper *mapper) : m_mapper(mapper) { + /* ... */ + } + + template + Result AcquireReadableStructPin(ReadableStructPin *out, size_t offset) { + /* Acquire mapped memory for the pin. */ + MappedMemory memory = {}; + R_TRY(m_mapper->GetMappedMemory(std::addressof(memory), offset, sizeof(T))); + + /* Mark the memory as in use. */ + R_RETURN(m_mapper->MarkUsing(memory.id)); + + /* Setup the pin. */ + out->Reset(this, memory.id, memory.GetBuffer(offset, sizeof(T)), sizeof(T)); + R_SUCCEED(); + } + + Result ReleasePin(u64 id) { + R_RETURN(m_mapper->UnmarkUsing(id)); + } + + template + Result ReadStruct(T *out, size_t offset) { + /* Acquire mapped memory for the pin. */ + MappedMemory memory = {}; + R_TRY(m_mapper->GetMappedMemory(std::addressof(memory), offset, sizeof(T))); + + /* Mark the memory as in use. */ + R_RETURN(m_mapper->MarkUsing(memory.id)); + ON_SCOPE_EXIT { this->ReleasePin(memory.id); }; + + /* Copy out the struct. */ + *out = *reinterpret_cast(memory.GetBuffer(offset, sizeof(T))); + R_SUCCEED(); + } + }; + + template + class ReadableStructPin final : public AccessorBase::PinBase { + public: + using PinBase::PinBase; + using PinBase::operator=; + + const T *Get() const { + return reinterpret_cast(this->GetData()); + } + + size_t GetSize() const { + return this->GetDataSize(); + } + + const T &operator*() const { return *this->Get(); } + const T *operator->() const { return this->Get(); } + }; + + class PatchMetaExtendedDataAccessor : public AccessorBase { + private: + struct CachedCount { + s32 index; + s32 count; + }; + private: + util::optional m_cached_history_content_count = util::nullopt; + util::optional m_cached_delta_content_count = util::nullopt; + util::optional m_cached_fragment_set_count = util::nullopt; + util::optional m_cached_fragment_indicator_count = util::nullopt; + util::optional m_header = util::nullopt; + public: + using AccessorBase::AccessorBase; + public: + Result GetHeader(ReadableStructPin *out) { return this->AcquireReadableStructPin(out, 0); } + Result GetHeader(PatchMetaExtendedDataHeader *out) { return this->template ReadStruct(out, 0); } + + Result GetHistoryHeader(ReadableStructPin *out, s32 index) { + /* Ensure we have our header. */ + R_TRY(this->EnsureHeader()); + + /* Check that the index is valid. */ + R_UNLESS(0 <= index && static_cast(index) < m_header->history_count, ncm::ResultInvalidOffset()); + + /* Get the header. */ + const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * index; + R_RETURN(this->AcquireReadableStructPin(out, offset)); + } + + Result GetPatchDeltaHistory(ReadableStructPin *out, s32 index) { + /* Ensure we have our header. */ + R_TRY(this->EnsureHeader()); + + /* Check that the index is valid. */ + R_UNLESS(0 <= index && static_cast(index) < m_header->delta_history_count, ncm::ResultInvalidOffset()); + + /* Get the history. */ + const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * index; + R_RETURN(this->AcquireReadableStructPin(out, offset)); + } + + Result GetPatchDeltaHeader(ReadableStructPin *out, s32 index) { + /* Ensure we have our header. */ + R_TRY(this->EnsureHeader()); + + /* Check that the index is valid. */ + R_UNLESS(0 <= index && static_cast(index) < m_header->delta_count, ncm::ResultInvalidOffset()); + + /* Get the header. */ + const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * index; + R_RETURN(this->AcquireReadableStructPin(out, offset)); + } + + Result GetFragmentSet(ReadableStructPin *out, s32 delta_index, s32 fragment_set_index) { + /* Ensure we have our header. */ + R_TRY(this->EnsureHeader()); + + /* Check that the index is valid. */ + R_UNLESS(0 <= delta_index && static_cast(delta_index) < m_header->delta_count, ncm::ResultInvalidOffset()); + + /* Get the previous fragment set count. */ + s32 previous_fragment_set_count = 0; + R_TRY(this->CountFragmentSet(std::addressof(previous_fragment_set_count), delta_index)); + + /* Get the set. */ + const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * (previous_fragment_set_count + fragment_set_index); + R_RETURN(this->AcquireReadableStructPin(out, offset)); + } + + Result GetFragmentSetDirectly(ReadableStructPin *out, s32 fragment_set_direct_index) { + /* Ensure we have our header. */ + R_TRY(this->EnsureHeader()); + + /* Check that the index is valid. */ + R_UNLESS(0 <= fragment_set_direct_index && static_cast(fragment_set_direct_index) < m_header->fragment_set_count, ncm::ResultInvalidOffset()); + + /* Get the set. */ + const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * (fragment_set_direct_index); + R_RETURN(this->AcquireReadableStructPin(out, offset)); + } + + Result GetPatchHistoryContentInfo(ReadableStructPin *out, s32 history_index, s32 content_index) { + /* Ensure we have our header. */ + R_TRY(this->EnsureHeader()); + + /* Check that the index is valid. */ + R_UNLESS(0 <= history_index && static_cast(history_index) < m_header->history_count, ncm::ResultInvalidOffset()); + + /* Determine the true history content index. */ + s32 prev_history_count = 0; + R_TRY(this->CountHistoryContentInfo(std::addressof(prev_history_count), history_index)); + + /* Adjust and check the content index. */ + content_index += prev_history_count; + R_UNLESS(0 <= content_index && static_cast(content_index) < m_header->history_content_total_count, ncm::ResultInvalidOffset()); + + /* Get the info. */ + const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * m_header->fragment_set_count + sizeof(ContentInfo) * content_index; + R_RETURN(this->AcquireReadableStructPin(out, offset)); + } + + Result GetPatchDeltaContentInfo(ReadableStructPin *out, s32 delta_index, s32 content_index) { + /* Ensure we have our header. */ + R_TRY(this->EnsureHeader()); + + /* Check that the index is valid. */ + R_UNLESS(0 <= delta_index && static_cast(delta_index) < m_header->delta_count, ncm::ResultInvalidOffset()); + + /* Determine the true delta content index. */ + s32 prev_delta_count = 0; + R_TRY(this->CountDeltaContentInfo(std::addressof(prev_delta_count), delta_index)); + + /* Adjust and check the content index. */ + content_index += prev_delta_count; + R_UNLESS(0 <= content_index && static_cast(content_index) < m_header->delta_content_total_count, ncm::ResultInvalidOffset()); + + /* Get the info. */ + const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * m_header->fragment_set_count + sizeof(ContentInfo) * m_header->history_content_total_count + sizeof(PackagedContentInfo) * content_index; + R_RETURN(this->AcquireReadableStructPin(out, offset)); + } + + Result GetFragmentIndicator(ReadableStructPin *out, s32 delta_index, s32 fragment_set_index, s32 index) { + /* Ensure we have our header. */ + R_TRY(this->EnsureHeader()); + + /* Check that the index is valid. */ + R_UNLESS(0 <= delta_index && static_cast(delta_index) < m_header->delta_count, ncm::ResultInvalidOffset()); + + /* Get the previous fragment set count. */ + s32 previous_fragment_set_count = 0; + R_TRY(this->CountFragmentSet(std::addressof(previous_fragment_set_count), delta_index)); + + /* Get the previous fragment indicator count. */ + s32 previous_fragment_count = 0; + R_TRY(this->CountFragmentIndicator(std::addressof(previous_fragment_count), previous_fragment_count + fragment_set_index)); + + /* Get the info. */ + const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * m_header->fragment_set_count + sizeof(ContentInfo) * m_header->history_content_total_count + sizeof(PackagedContentInfo) * m_header->delta_content_total_count + sizeof(FragmentIndicator) * (previous_fragment_count + index); + R_RETURN(this->AcquireReadableStructPin(out, offset)); + } + + Result FindFragmentIndicator(ReadableStructPin *out, s32 delta_index, s32 fragment_set_index, s32 fragment_index) { + /* Ensure we have our header. */ + R_TRY(this->EnsureHeader()); + + /* Check that the index is valid. */ + R_UNLESS(0 <= delta_index && static_cast(delta_index) < m_header->delta_count, ncm::ResultInvalidOffset()); + + /* Get the fragment count. */ + s32 fragment_count = 0; + { + ReadableStructPin set; + R_TRY(this->GetFragmentSet(std::addressof(set), delta_index, fragment_set_index)); + + fragment_count = set->fragment_count; + } + + /* Get the previous fragment set count. */ + s32 previous_fragment_set_count = 0; + R_TRY(this->CountFragmentSet(std::addressof(previous_fragment_set_count), delta_index)); + + /* Get the previous fragment indicator count. */ + s32 previous_fragment_count = 0; + R_TRY(this->CountFragmentIndicator(std::addressof(previous_fragment_count), previous_fragment_count + fragment_set_index)); + + /* Look for a correct indicator. */ + for (auto i = 0; i < fragment_count; ++i) { + /* Get the current info. */ + ReadableStructPin indicator; + const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * m_header->fragment_set_count + sizeof(ContentInfo) * m_header->history_content_total_count + sizeof(PackagedContentInfo) * m_header->delta_content_total_count + sizeof(FragmentIndicator) * (previous_fragment_count + i); + R_TRY(this->AcquireReadableStructPin(std::addressof(indicator), offset)); + + /* If it matches, return it. */ + if (indicator->fragment_index == fragment_index) { + *out = std::move(indicator); + R_SUCCEED(); + } + } + + /* We didn't find an indicator. */ + R_THROW(ncm::ResultFragmentIndicatorNotFound()); + } + + Result GetHistoryHeader(PatchHistoryHeader *out, s32 index) { + /* Get the pin. */ + ReadableStructPin pin; + R_TRY(this->GetHistoryHeader(std::addressof(pin), index)); + + /* Copy it out. */ + *out = *pin; + R_SUCCEED(); + } + + Result GetPatchDeltaHistory(PatchDeltaHistory *out, s32 index) { + /* Get the pin. */ + ReadableStructPin pin; + R_TRY(this->GetPatchDeltaHistory(std::addressof(pin), index)); + + /* Copy it out. */ + *out = *pin; + R_SUCCEED(); + } + + Result GetPatchDeltaHeader(PatchDeltaHeader *out, s32 index) { + /* Get the pin. */ + ReadableStructPin pin; + R_TRY(this->GetPatchDeltaHeader(std::addressof(pin), index)); + + /* Copy it out. */ + *out = *pin; + R_SUCCEED(); + } + + Result GetFragmentSet(FragmentSet *out, s32 delta_index, s32 fragment_set_index) { + /* Get the pin. */ + ReadableStructPin pin; + R_TRY(this->GetFragmentSet(std::addressof(pin), delta_index, fragment_set_index)); + + /* Copy it out. */ + *out = *pin; + R_SUCCEED(); + } + + Result GetPatchHistoryContentInfo(ContentInfo *out, s32 history_index, s32 content_index) { + /* Get the header. */ + ReadableStructPin pin; + R_TRY(this->GetPatchHistoryContentInfo(std::addressof(pin), history_index, content_index)); + + /* Copy it out. */ + *out = *pin; + R_SUCCEED(); + } + + Result GetPatchDeltaContentInfo(PackagedContentInfo *out, s32 delta_index, s32 content_index) { + /* Get the header. */ + ReadableStructPin pin; + R_TRY(this->GetPatchDeltaContentInfo(std::addressof(pin), delta_index, content_index)); + + /* Copy it out. */ + *out = *pin; + R_SUCCEED(); + } + + Result GetFragmentIndicator(FragmentIndicator *out, s32 delta_index, s32 fragment_set_index, s32 index) { + /* Get the header. */ + ReadableStructPin pin; + R_TRY(this->GetFragmentIndicator(std::addressof(pin), delta_index, fragment_set_index, index)); + + /* Copy it out. */ + *out = *pin; + R_SUCCEED(); + } + + Result FindFragmentIndicator(FragmentIndicator *out, s32 delta_index, s32 fragment_set_index, s32 fragment_index) { + /* Get the header. */ + ReadableStructPin pin; + R_TRY(this->FindFragmentIndicator(std::addressof(pin), delta_index, fragment_set_index, fragment_index)); + + /* Copy it out. */ + *out = *pin; + R_SUCCEED(); + } + + Result CountHistoryContentInfo(s32 *out, s32 index) { + R_RETURN(this->CountImpl(out, index, m_cached_history_content_count, [&](s32 *out, s32 i) -> Result { + /* Get the history header. */ + ReadableStructPin header; + R_TRY(this->GetHistoryHeader(std::addressof(header), i)); + + /* Set the content count. */ + *out = header->content_count; + R_SUCCEED(); + })); + } + + Result CountDeltaContentInfo(s32 *out, s32 index) { + R_RETURN(this->CountImpl(out, index, m_cached_delta_content_count, [&](s32 *out, s32 i) -> Result { + /* Get the history header. */ + ReadableStructPin header; + R_TRY(this->GetPatchDeltaHeader(std::addressof(header), i)); + + /* Set the content count. */ + *out = header->content_count; + R_SUCCEED(); + })); + } + + Result CountFragmentSet(s32 *out, s32 index) { + R_RETURN(this->CountImpl(out, index, m_cached_fragment_set_count, [&](s32 *out, s32 i) -> Result { + /* Get the history header. */ + ReadableStructPin header; + R_TRY(this->GetPatchDeltaHeader(std::addressof(header), i)); + + /* Set the fragment set count. */ + *out = header->delta.fragment_set_count; + R_SUCCEED(); + })); + } + + Result CountFragmentIndicator(s32 *out, s32 index) { + R_RETURN(this->CountImpl(out, index, m_cached_fragment_indicator_count, [&](s32 *out, s32 i) -> Result { + /* Get the history header. */ + ReadableStructPin set; + R_TRY(this->GetFragmentSetDirectly(std::addressof(set), i)); + + /* Set the indicator count. */ + *out = set->fragment_count; + R_SUCCEED(); + })); + } + private: + Result CountImpl(s32 *out, s32 index, util::optional &cache, auto get_count_impl) const { + /* Ensure the value is cached. */ + if (!(cache.has_value() && cache->index == index)) { + /* Determine the count. */ + CachedCount calc = { .index = index, .count = 0 }; + for (auto i = 0; i < index; ++i) { + s32 cur_count = 0; + R_TRY(get_count_impl(std::addressof(cur_count), i)); + + calc.count += cur_count; + } + + /* Cache the count. */ + cache = calc; + } + + /* Set the output count. */ + *out = cache->count; + R_SUCCEED(); + } + private: + Result EnsureHeader() { + /* If we have our header, we're good. */ + R_SUCCEED_IF(m_header.has_value()); + + /* Get our header. */ + PatchMetaExtendedDataHeader header; + R_TRY(this->GetHeader(std::addressof(header))); + + /* Set our header. */ + m_header.emplace(header); + R_SUCCEED(); + } + + + }; + } diff --git a/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta_utils.hpp b/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta_utils.hpp index 104edf1a4..3fd41f1b8 100644 --- a/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta_utils.hpp +++ b/libraries/libstratosphere/include/stratosphere/ncm/ncm_content_meta_utils.hpp @@ -25,7 +25,14 @@ namespace ams::ncm { using MountContentMetaFunction = Result (*)(const char *mount_name, const char *path); - Result ReadContentMetaPath(AutoBuffer *out, const char *path); + bool IsContentMetaFileName(const char *name); + + Result ReadContentMetaPathAlongWithExtendedDataAndDigest(AutoBuffer *out, const char *path); + Result ReadContentMetaPathAlongWithExtendedDataAndDigestSuppressingFsAbort(AutoBuffer *out, const char *path); + + Result ReadContentMetaPathWithoutExtendedDataOrDigest(AutoBuffer *out, const char *path); + Result ReadContentMetaPathWithoutExtendedDataOrDigestSuppressingFsAbort(AutoBuffer *out, const char *path); + Result ReadVariationContentMetaInfoList(s32 *out_count, std::unique_ptr *out_meta_infos, const Path &path, FirmwareVariationId firmware_variation_id); void SetMountContentMetaFunction(MountContentMetaFunction func); diff --git a/libraries/libstratosphere/include/stratosphere/ncm/ncm_mapped_memory.hpp b/libraries/libstratosphere/include/stratosphere/ncm/ncm_mapped_memory.hpp new file mode 100644 index 000000000..021dd12ae --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/ncm/ncm_mapped_memory.hpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include + +namespace ams::ncm { + + struct MappedMemory { + u64 id; + size_t offset; + u8 *buffer; + size_t buffer_size; + + bool IsIncluded(size_t o, size_t sz) const { + return this->offset <= o && sz <= this->buffer_size && (o + sz) <= (this->offset + this->buffer_size); + } + + u8 *GetBuffer(size_t o, size_t sz) const { + AMS_ASSERT(this->buffer != nullptr); + AMS_ASSERT(this->IsIncluded(o, sz)); + + return this->buffer + (o - this->offset); + } + }; + static_assert(util::is_pod::value); + + class IMapper { + public: + virtual ~IMapper() { /* ... */ } + public: + virtual Result GetMappedMemory(MappedMemory *out, size_t offset, size_t size) = 0; + virtual Result MarkUsing(u64 id) = 0; + virtual Result UnmarkUsing(u64 id) = 0; + virtual Result MarkDirty(u64 id) = 0; + protected: + virtual Result MapImpl(MappedMemory *out, Span data, size_t offset, size_t size) = 0; + virtual Result UnmapImpl(MappedMemory *mem) = 0; + virtual bool IsAccessibleSizeUpdatable() = 0; + }; + +} \ No newline at end of file diff --git a/libraries/libstratosphere/source/gc/impl/gc_gc_crypto.cpp b/libraries/libstratosphere/source/gc/impl/gc_gc_crypto.cpp index 81b7fb602..49023d780 100644 --- a/libraries/libstratosphere/source/gc/impl/gc_gc_crypto.cpp +++ b/libraries/libstratosphere/source/gc/impl/gc_gc_crypto.cpp @@ -52,7 +52,7 @@ namespace ams::gc::impl { Result GcCrypto::VerifyT1CardCertificate(const void *cert_buffer, size_t cert_size) { /* Check pre-conditions. */ - AMS_ASSERT(cert_size == sizeof(T1CardCertificate)); + R_UNLESS(cert_size == sizeof(T1CardCertificate), fs::ResultGameCardPreconditionViolation()); /* Get cert buffer as type. */ const auto * const cert = static_cast(cert_buffer); @@ -75,7 +75,7 @@ namespace ams::gc::impl { Result GcCrypto::VerifyCa10Certificate(const void *cert_buffer, size_t cert_size) { /* Check pre-conditions. */ - AMS_ASSERT(cert_size == sizeof(Ca10Certificate)); + R_UNLESS(cert_size == sizeof(Ca10Certificate), fs::ResultGameCardPreconditionViolation()); /* Get header buffer as type. */ const auto * const cert = static_cast(cert_buffer); diff --git a/libraries/libstratosphere/source/kvdb/kvdb_file_key_value_store.cpp b/libraries/libstratosphere/source/kvdb/kvdb_file_key_value_store.cpp index 8748e1395..734c87596 100644 --- a/libraries/libstratosphere/source/kvdb/kvdb_file_key_value_store.cpp +++ b/libraries/libstratosphere/source/kvdb/kvdb_file_key_value_store.cpp @@ -154,7 +154,7 @@ namespace ams::kvdb { const size_t file_name_len = file_name.GetLength(); const size_t key_name_len = file_name_len - FileExtensionLength; R_UNLESS(file_name_len >= FileExtensionLength + 2, kvdb::ResultInvalidKeyValue()); - R_UNLESS(file_name.EndsWith(FileExtension), kvdb::ResultInvalidKeyValue()); + R_UNLESS(file_name.EqualsPostfix(FileExtension), kvdb::ResultInvalidKeyValue()); R_UNLESS(util::IsAligned(key_name_len, 2), kvdb::ResultInvalidKeyValue()); /* Validate that we have space for the converted key. */ @@ -165,7 +165,7 @@ namespace ams::kvdb { u8 *out_key = static_cast(_out_key); for (size_t i = 0; i < key_size; i++) { char substr[2 * sizeof(u8) + 1]; - file_name.GetSubstring(substr, sizeof(substr), 2 * i, sizeof(substr) - 1); + file_name.GetSubString(substr, sizeof(substr), 2 * i, sizeof(substr) - 1); out_key[i] = static_cast(std::strtoul(substr, nullptr, 0x10)); } @@ -184,7 +184,7 @@ namespace ams::kvdb { R_UNLESS(entry_type == fs::DirectoryEntryType_Directory, fs::ResultPathNotFound()); /* Set path. */ - m_dir_path.Set(dir); + m_dir_path.Assign(dir); /* Initialize our cache. */ R_TRY(m_cache.Initialize(cache_buffer, cache_buffer_size, cache_capacity)); diff --git a/libraries/libstratosphere/source/ncm/ncm_content_info_utils.cpp b/libraries/libstratosphere/source/ncm/ncm_content_info_utils.cpp index 3fb2c88bc..a18969074 100644 --- a/libraries/libstratosphere/source/ncm/ncm_content_info_utils.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_content_info_utils.cpp @@ -18,6 +18,7 @@ namespace ams::ncm { namespace { + constexpr inline s64 EncryptionMetadataSize = 16_KB; constexpr inline s64 ConcatenationFileSizeMax = 4_GB; diff --git a/libraries/libstratosphere/source/ncm/ncm_content_management_utils.cpp b/libraries/libstratosphere/source/ncm/ncm_content_management_utils.cpp index 22549d219..42a087129 100644 --- a/libraries/libstratosphere/source/ncm/ncm_content_management_utils.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_content_management_utils.cpp @@ -37,7 +37,7 @@ namespace ams::ncm { char path[MaxPackagePathLength]; R_TRY(ConvertToFsCommonPath(path, sizeof(path), package_root_path, entry.name)); - return ncm::ReadContentMetaPath(out, path); + return ncm::ReadContentMetaPathWithoutExtendedDataOrDigest(out, path); } template @@ -108,7 +108,7 @@ namespace ams::ncm { /* Read the content meta path, and build. */ ncm::AutoBuffer package_meta; - if (R_SUCCEEDED(ncm::ReadContentMetaPath(std::addressof(package_meta), path.str))) { + if (R_SUCCEEDED(ncm::ReadContentMetaPathWithoutExtendedDataOrDigest(std::addressof(package_meta), path.str))) { /* Get the size of the content. */ s64 size; R_TRY(storage->GetSize(std::addressof(size), content_id)); diff --git a/libraries/libstratosphere/source/ncm/ncm_content_manager_impl.cpp b/libraries/libstratosphere/source/ncm/ncm_content_manager_impl.cpp index d55dd1a59..2b521bd55 100644 --- a/libraries/libstratosphere/source/ncm/ncm_content_manager_impl.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_content_manager_impl.cpp @@ -255,11 +255,11 @@ namespace ams::ncm { /* Print the savedata path. */ PathString savedata_db_path; - savedata_db_path.SetFormat("%s/%s", root->path, "imkvdb.arc"); + savedata_db_path.AssignFormat("%s/%s", root->path, "imkvdb.arc"); /* Print a path for the mounted partition. */ PathString bis_db_path; - bis_db_path.SetFormat("%s:/%s", import_mount_name, path); + bis_db_path.AssignFormat("%s:/%s", import_mount_name, path); /* Mount the savedata. */ R_TRY(fs::MountSystemSaveData(root->mount_name, root->info.space_id, root->info.id)); diff --git a/libraries/libstratosphere/source/ncm/ncm_content_meta.cpp b/libraries/libstratosphere/source/ncm/ncm_content_meta.cpp index 6d6143ab0..d2d1c2175 100644 --- a/libraries/libstratosphere/source/ncm/ncm_content_meta.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_content_meta.cpp @@ -229,10 +229,10 @@ namespace ams::ncm { R_TRY(FindDeltaIndex(std::addressof(index), reader, source_version, this->GetKey().version)); /* Get the fragment count. */ - auto fragment_count = CountContentExceptForMeta(reader, index); + const auto fragment_count = CountContentExceptForMeta(reader, index); /* Recalculate. */ - *out_size = CalculateSizeImpl(this->GetExtendedHeaderSize(), fragment_count + 1, 0, this->GetExtendedDataSize(), false); + *out_size = this->CalculateConvertFragmentOnlyInstallContentMetaSize(fragment_count); return ResultSuccess(); } @@ -325,4 +325,113 @@ namespace ams::ncm { } } + Result MetaConverter::CountContentExceptForMeta(s32 *out, PatchMetaExtendedDataAccessor *accessor, const PatchDeltaHeader &header, s32 delta_index) { + /* Get the count. */ + s32 count = 0; + + for (auto i = 0; i < static_cast(header.content_count); ++i) { + /* Get the delta content info. */ + PackagedContentInfo content_info; + R_TRY(accessor->GetPatchDeltaContentInfo(std::addressof(content_info), delta_index, i)); + + if (content_info.GetType() != ContentType::Meta) { + ++count; + } + } + + *out = count; + R_SUCCEED(); + } + + Result MetaConverter::FindDeltaIndex(s32 *out, PatchMetaExtendedDataAccessor *accessor, u32 source_version, u32 destination_version) { + /* Get the header. */ + PatchMetaExtendedDataHeader header; + header.delta_count = 0; + R_TRY(accessor->GetHeader(std::addressof(header))); + + /* Iterate over all deltas. */ + for (s32 i = 0; i < static_cast(header.delta_count); i++) { + /* Get the current patch delta header. */ + PatchDeltaHeader delta_header; + R_TRY(accessor->GetPatchDeltaHeader(std::addressof(delta_header), i)); + + /* Check if the current delta matches the versions. */ + if ((source_version == 0 || delta_header.delta.source_version == source_version) && delta_header.delta.destination_version == destination_version) { + *out = i; + R_SUCCEED(); + } + } + + /* We didn't find the delta. */ + R_THROW(ncm::ResultDeltaNotFound()); + } + + Result MetaConverter::GetFragmentOnlyInstallContentMeta(AutoBuffer *out, const InstallContentInfo &meta, const PackagedContentMetaReader &reader, PatchMetaExtendedDataAccessor *accessor, u32 source_version) { + /* Find the appropriate delta index. */ + s32 delta_index = 0; + R_TRY(FindDeltaIndex(std::addressof(delta_index), accessor, source_version, reader.GetHeader()->version)); + + /* Get the delta header. */ + PatchDeltaHeader delta_header; + R_TRY(accessor->GetPatchDeltaHeader(std::addressof(delta_header), delta_index)); + + /* Count content except for meta. */ + s32 fragment_count = 0; + R_TRY(CountContentExceptForMeta(std::addressof(fragment_count), accessor, delta_header, delta_index)); + + /* Determine the required size. */ + const size_t meta_size = reader.CalculateConvertFragmentOnlyInstallContentMetaSize(fragment_count); + + /* Initialize the out buffer. */ + R_TRY(out->Initialize(meta_size)); + + /* Prepare for conversion. */ + const auto *packaged_header = reader.GetHeader(); + uintptr_t dst_addr = reinterpret_cast(out->Get()); + + /* Convert the header. */ + InstallContentMetaHeader header; + ConvertPackageContentMetaHeaderToInstallContentMetaHeader(std::addressof(header), *packaged_header); + header.install_type = ContentInstallType::FragmentOnly; + + /* Set the content count. */ + header.content_count = static_cast(fragment_count) + 1; + + /* Copy the header. */ + std::memcpy(reinterpret_cast(dst_addr), std::addressof(header), sizeof(header)); + dst_addr += sizeof(header); + + /* Copy the extended header. */ + std::memcpy(reinterpret_cast(dst_addr), reader.GetExtendedHeader(), packaged_header->extended_header_size); + dst_addr += packaged_header->extended_header_size; + + /* Copy the top level meta. */ + std::memcpy(reinterpret_cast(dst_addr), std::addressof(meta), sizeof(meta)); + dst_addr += sizeof(meta); + + s32 count = 0; + for (s32 i = 0; i < static_cast(delta_header.content_count); i++) { + /* Get the delta content info. */ + PackagedContentInfo content_info; + R_TRY(accessor->GetPatchDeltaContentInfo(std::addressof(content_info), delta_index, i)); + + if (content_info.GetType() != ContentType::Meta) { + /* Create the install content info. */ + InstallContentInfo install_content_info = InstallContentInfo::Make(content_info, packaged_header->type); + + /* Copy the info. */ + std::memcpy(reinterpret_cast(dst_addr), std::addressof(install_content_info), sizeof(InstallContentInfo)); + dst_addr += sizeof(InstallContentInfo); + + /* Increment the count. */ + count++; + } + } + + /* Assert that we copied the right number of infos. */ + AMS_ASSERT(count == fragment_count); + + R_SUCCEED(); + } + } diff --git a/libraries/libstratosphere/source/ncm/ncm_content_meta_utils.cpp b/libraries/libstratosphere/source/ncm/ncm_content_meta_utils.cpp index e5798aab7..8bab15042 100644 --- a/libraries/libstratosphere/source/ncm/ncm_content_meta_utils.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_content_meta_utils.cpp @@ -20,12 +20,6 @@ namespace ams::ncm { namespace { - using FilePathString = kvdb::BoundedString<64>; - - bool IsContentMetaFileName(const char *name) { - return impl::PathView(name).HasSuffix(".cnmt"); - } - Result MountContentMetaByRemoteFileSystemProxy(const char *mount_name, const char *path) { return fs::MountContent(mount_name, path, fs::ContentType_Meta); } @@ -34,10 +28,22 @@ namespace ams::ncm { } - Result ReadContentMetaPath(AutoBuffer *out, const char *path) { + namespace impl { + + Result MountContentMetaImpl(const char *mount_name, const char *path) { + R_RETURN(g_mount_content_meta_func(mount_name, path)); + } + + } + + bool IsContentMetaFileName(const char *name) { + return impl::PathView(name).HasSuffix(".cnmt"); + } + + Result ReadContentMetaPathAlongWithExtendedDataAndDigest(AutoBuffer *out, const char *path) { /* Mount the content. */ auto mount_name = impl::CreateUniqueMountName(); - R_TRY(g_mount_content_meta_func(mount_name.str, path)); + R_TRY(impl::MountContentMetaImpl(mount_name.str, path)); ON_SCOPE_EXIT { fs::Unmount(mount_name.str); }; /* Open the root directory. */ @@ -59,7 +65,7 @@ namespace ams::ncm { /* If this is the content meta file, parse it. */ if (IsContentMetaFileName(entry.name)) { /* Create the file path. */ - FilePathString file_path(root_path.str); + impl::FilePathString file_path(root_path.str); file_path.Append(entry.name); /* Open the content meta file. */ @@ -76,19 +82,89 @@ namespace ams::ncm { R_TRY(out->Initialize(meta_size)); /* Read the meta into the buffer. */ - return fs::ReadFile(file, 0, out->Get(), meta_size); + R_RETURN(fs::ReadFile(file, 0, out->Get(), meta_size)); } } - return ncm::ResultContentMetaNotFound(); + R_THROW(ncm::ResultContentMetaNotFound()); + } + + Result ReadContentMetaPathAlongWithExtendedDataAndDigestSuppressingFsAbort(AutoBuffer *out, const char *path) { + fs::ScopedAutoAbortDisabler aad; + R_RETURN(ReadContentMetaPathAlongWithExtendedDataAndDigest(out, path)); + } + + Result ReadContentMetaPathWithoutExtendedDataOrDigest(AutoBuffer *out, const char *path) { + /* Mount the content. */ + auto mount_name = impl::CreateUniqueMountName(); + R_TRY(impl::MountContentMetaImpl(mount_name.str, path)); + ON_SCOPE_EXIT { fs::Unmount(mount_name.str); }; + + /* Open the root directory. */ + auto root_path = impl::GetRootDirectoryPath(mount_name); + fs::DirectoryHandle dir; + R_TRY(fs::OpenDirectory(std::addressof(dir), root_path.str, fs::OpenDirectoryMode_File)); + ON_SCOPE_EXIT { fs::CloseDirectory(dir); }; + + /* Loop directory reading until we find the entry we're looking for. */ + while (true) { + /* Read one entry, and finish when we fail to read. */ + fs::DirectoryEntry entry; + s64 num_read; + R_TRY(fs::ReadDirectory(std::addressof(num_read), std::addressof(entry), dir, 1)); + if (num_read == 0) { + break; + } + + /* If this is the content meta file, parse it. */ + if (IsContentMetaFileName(entry.name)) { + /* Create the file path. */ + impl::FilePathString file_path(root_path.str); + file_path.Append(entry.name); + + /* Open the content meta file. */ + fs::FileHandle file; + R_TRY(fs::OpenFile(std::addressof(file), file_path, fs::OpenMode_Read)); + ON_SCOPE_EXIT { fs::CloseFile(file); }; + + /* Get the meta size. */ + s64 file_size; + R_TRY(fs::GetFileSize(std::addressof(file_size), file)); + const size_t meta_file_size = static_cast(file_size); + + /* Check that the meta size is large enough. */ + R_UNLESS(meta_file_size >= sizeof(PackagedContentMetaHeader), ncm::ResultInvalidContentMetaFileSize()); + + /* Read the header. */ + PackagedContentMetaHeader header; + size_t read_size = 0; + R_TRY(fs::ReadFile(std::addressof(read_size), file, 0, std::addressof(header), sizeof(header))); + + /* Check the right size was read. */ + R_UNLESS(read_size == sizeof(PackagedContentMetaHeader), ncm::ResultInvalidContentMetaFileSize()); + + /* Determine the meta size. */ + const size_t meta_size = PackagedContentMetaReader(std::addressof(header), sizeof(header)).GetExtendedDataOffset(); + + /* Create a buffer for the meta. */ + R_TRY(out->Initialize(meta_size)); + + /* Read the meta into the buffer. */ + R_RETURN(fs::ReadFile(file, 0, out->Get(), meta_size)); + } + } + + R_THROW(ncm::ResultContentMetaNotFound()); + } + + Result ReadContentMetaPathWithoutExtendedDataOrDigestSuppressingFsAbort(AutoBuffer *out, const char *path) { + fs::ScopedAutoAbortDisabler aad; + R_RETURN(ReadContentMetaPathAlongWithExtendedDataAndDigest(out, path)); } Result ReadVariationContentMetaInfoList(s32 *out_count, std::unique_ptr *out_meta_infos, const Path &path, FirmwareVariationId firmware_variation_id) { AutoBuffer meta; - { - fs::ScopedAutoAbortDisabler aad; - R_TRY(ReadContentMetaPath(std::addressof(meta), path.str)); - } + R_TRY(ReadContentMetaPathAlongWithExtendedDataAndDigestSuppressingFsAbort(std::addressof(meta), path.str)); /* Create a reader for the content meta. */ PackagedContentMetaReader reader(meta.Get(), meta.GetSize()); diff --git a/libraries/libstratosphere/source/ncm/ncm_content_storage_impl.cpp b/libraries/libstratosphere/source/ncm/ncm_content_storage_impl.cpp index a09a92599..608928957 100644 --- a/libraries/libstratosphere/source/ncm/ncm_content_storage_impl.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_content_storage_impl.cpp @@ -24,7 +24,7 @@ namespace ams::ncm { constexpr inline const char * const BaseContentDirectory = "/registered"; void MakeBaseContentDirectoryPath(PathString *out, const char *root_path) { - out->SetFormat("%s%s", root_path, BaseContentDirectory); + out->AssignFormat("%s%s", root_path, BaseContentDirectory); } void MakeContentPath(PathString *out, ContentId id, MakeContentPathFunction func, const char *root_path) { @@ -83,7 +83,7 @@ namespace ams::ncm { /* Path of the current entry. */ PathString current_path; - current_path.SetFormat("%s/%s", root_path, entry.name); + current_path.AssignFormat("%s/%s", root_path, entry.name); /* Call the process function. */ bool should_continue = true; @@ -202,7 +202,7 @@ namespace ams::ncm { Result ContentStorageImpl::ContentIterator::OpenDirectory(const char *dir) { /* Set our current path. */ - m_path.Set(dir); + m_path.Assign(dir); /* Open the directory. */ return this->OpenCurrentDirectory(); @@ -230,7 +230,7 @@ namespace ams::ncm { if (m_depth < m_max_depth) { /* Construct the full path for the subdirectory. */ PathString entry_path; - entry_path.SetFormat("%s/%s", m_path.Get(), entry.name); + entry_path.AssignFormat("%s/%s", m_path.Get(), entry.name); /* Open the subdirectory. */ R_TRY(this->OpenDirectory(entry_path.Get())); @@ -288,7 +288,7 @@ namespace ams::ncm { } /* Set the path to the parent directory. */ - m_path.Set(m_path.GetSubstring(0, i + 1)); + m_path = m_path.MakeSubString(0, i + 1); /* Try to load again from the parent directory. */ return this->LoadEntries(); diff --git a/libraries/libstratosphere/source/ncm/ncm_extended_data_mapper.hpp b/libraries/libstratosphere/source/ncm/ncm_extended_data_mapper.hpp new file mode 100644 index 000000000..97e248a90 --- /dev/null +++ b/libraries/libstratosphere/source/ncm/ncm_extended_data_mapper.hpp @@ -0,0 +1,429 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include +#include "ncm_file_mapper_file.hpp" +#include "ncm_fs_utils.hpp" + +namespace ams::ncm { + + namespace impl { + + template + concept IsMappedMemorySpan = std::same_as>; + + constexpr inline u64 InitialIdCounterValue = 0x12345; + + } + + class SingleCacheMapperBase : public IMapper { + private: + bool m_is_mapped; + MappedMemory m_mapped_memory; + size_t m_accessible_size; + bool m_is_using; + bool m_is_dirty; + u64 m_id_counter; + u8 *m_buffer; + size_t m_buffer_size; + public: + SingleCacheMapperBase(Span span) : m_is_mapped(false), m_mapped_memory{}, m_accessible_size(0), m_is_using(false), m_is_dirty(false), m_id_counter(impl::InitialIdCounterValue), m_buffer(span.data()), m_buffer_size(span.size_bytes()) { + /* ... */ + } + protected: + void Finalize() { + /* If we're unused and mapped, we should unmap. */ + if (!m_is_using && m_is_mapped) { + this->Unmap(); + } + } + private: + Result Unmap() { + /* Check pre-conditions. */ + AMS_ASSERT(m_is_mapped); + + /* If we're dirty, we'll need to flush the entry. */ + if (m_is_dirty) { + /* Unmap our memory. */ + MappedMemory mem = m_mapped_memory; + if (mem.offset + mem.buffer_size > m_accessible_size) { + mem.buffer_size = m_accessible_size - mem.offset; + } + + R_TRY(this->UnmapImpl(std::addressof(mem))); + } + + /* Set as dirty/not mapped. */ + m_is_dirty = false; + m_is_mapped = false; + } + + Result GetMappedMemoryImpl(MappedMemory *out, size_t offset, size_t size) { + /* Check pre-conditions. */ + AMS_ASSERT(m_is_mapped); + + /* Ensure the accessible size works. */ + const bool can_update = this->IsAccessibleSizeUpdatable(); + R_UNLESS((offset + size <= m_accessible_size || can_update), ncm::ResultMapperInvalidArgument()); + + /* Update our accessible size. */ + m_accessible_size = std::max(m_accessible_size, size + offset); + + /* Set the output memory. */ + *out = m_mapped_memory; + out->buffer_size = std::min(out->buffer_size, m_accessible_size - out->offset); + R_SUCCEED(); + } + public: + virtual Result GetMappedMemory(MappedMemory *out, size_t offset, size_t size) override final { + /* Ensure our memory is valid, if it's already mapped. */ + if (m_is_mapped) { + /* If we can re-use the previous mapping, do so. */ + if (m_mapped_memory.IsIncluded(offset, size)) { + /* If the memory is in use, we can't get a new mapping. */ + R_UNLESS(!m_is_using, ncm::ResultMapperBusy()); + + /* Get the output memory. */ + R_RETURN(this->GetMappedMemoryImpl(out, offset, size)); + } + + /* We don't have the correct data mapped, so we need to map. */ + R_TRY(this->Unmap()); + } + + /* Map. */ + R_TRY(this->MapImpl(std::addressof(m_mapped_memory), Span(m_buffer, m_buffer_size), offset, size)); + + /* Set our mapping id. */ + m_mapped_memory.id = m_id_counter++; + + /* Get the output memory. */ + R_RETURN(this->GetMappedMemoryImpl(out, offset, size)); + } + + virtual Result MarkUsing(u64 id) override final { + /* Check that the mapping is correct. */ + R_UNLESS(m_is_mapped, ncm::ResultMapperNotMapped()); + R_UNLESS(m_mapped_memory.id == id, ncm::ResultMapperNotMapped()); + + /* Mark as using. */ + m_is_using = true; + R_SUCCEED(); + } + + virtual Result UnmarkUsing(u64 id) override final { + /* Check that the mapping is correct. */ + R_UNLESS(m_is_mapped, ncm::ResultMapperNotMapped()); + R_UNLESS(m_mapped_memory.id == id, ncm::ResultMapperNotMapped()); + + /* Mark as not using. */ + m_is_using = false; + R_SUCCEED(); + } + + virtual Result MarkDirty(u64 id) override final { + /* Check that the mapping is correct. */ + R_UNLESS(m_is_mapped, ncm::ResultMapperNotMapped()); + R_UNLESS(m_mapped_memory.id == id, ncm::ResultMapperNotMapped()); + + /* Mark as dirty. */ + m_is_dirty = true; + R_SUCCEED(); + } + }; + + template + class MultiCacheReadonlyMapperBase : public IMapper { + private: + struct Entry { + MappedMemory memory; + u64 lru_counter; + u32 use_count; + bool is_mapped; + u8 *buffer; + size_t buffer_size; + }; + private: + Entry m_entry_storages[MaxEntries]; + Entry * const m_entries; + size_t m_entry_count; + u64 m_id_counter; + u64 m_lru_counter; + size_t m_accessible_size; + public: + template + MultiCacheReadonlyMapperBase(Args... args) : m_entries(m_entry_storages), m_entry_count(sizeof...(Args)), m_id_counter(impl::InitialIdCounterValue), m_lru_counter(1), m_accessible_size(0) { + /* Check the argument count is valid. */ + static_assert(sizeof...(Args) <= MaxEntries); + + /* Initialize entries. */ + auto InitializeEntry = [](Entry *entry, Span span) ALWAYS_INLINE_LAMBDA -> void { + *entry = {}; + entry->buffer = span.data(); + entry->buffer_size = span.size_bytes(); + }; + + Entry *cur_entry = m_entries; + (InitializeEntry(cur_entry++, args), ...); + } + + size_t GetSize() { + return m_accessible_size; + } + protected: + void SetSize(size_t size) { + m_accessible_size = size; + } + + void Finalize() { + /* Mark all entries as unmapped. */ + for (size_t i = 0; i < m_entry_count; ++i) { + /* We can't mark unmapped an entry which is in use. */ + if (m_entries[i].use_count > 0) { + break; + } + + if (m_entries[i].is_mapped) { + m_entries[i].is_mapped = false; + } + } + } + private: + Result GetMappedMemoryImpl(MappedMemory *out, const MappedMemory &src) { + /* Set the output memory. */ + *out = src; + out->buffer_size = std::min(out->buffer_size, m_accessible_size - out->offset); + R_SUCCEED(); + } + public: + virtual Result GetMappedMemory(MappedMemory *out, size_t offset, size_t size) override final { + /* Try to find an entry which contains the desired region. */ + for (size_t i = 0; i < m_entry_count; ++i) { + if (m_entries[i].is_mapped && m_entries[i].memory.IsIncluded(offset, size)) { + R_RETURN(this->GetMappedMemoryImpl(out, m_entries[i].memory)); + } + } + + /* Find the oldest entry. */ + Entry *oldest = nullptr; + Entry *best_entry = nullptr; + for (size_t i = 0; i < m_entry_count; ++i) { + if (m_entries[i].is_mapped) { + if (m_entries[i].use_count == 0) { + if (oldest == nullptr || m_entries[i].lru_counter < oldest->lru_counter) { + oldest = std::addressof(m_entries[i]); + } + } + } else { + best_entry = std::addressof(m_entries[i]); + } + } + + /* If we didn't find a free entry, use the oldest. */ + best_entry = best_entry != nullptr ? best_entry : oldest; + R_UNLESS(best_entry != nullptr, ncm::ResultMapperBusy()); + + /* Ensure the best entry isn't mapped. */ + if (best_entry->is_mapped) { + best_entry->is_mapped = false; + } + + /* Map. */ + R_TRY(this->MapImpl(std::addressof(best_entry->memory), Span(best_entry->buffer, best_entry->buffer_size), offset, size)); + + /* Set our mapping id. */ + best_entry->memory.id = m_id_counter++; + + /* Get the output memory. */ + R_RETURN(this->GetMappedMemoryImpl(out, best_entry->memory)); + } + + virtual Result MarkUsing(u64 id) override final { + /* Try to unmark the entry. */ + for (size_t i = 0; i < m_entry_count; ++i) { + if (m_entries[i].memory.id == id) { + ++m_entries[i].use_count; + m_entries[i].lru_counter = m_lru_counter++; + R_SUCCEED(); + } + } + + /* We failed to unmark. */ + R_THROW(ncm::ResultMapperNotMapped()); + } + + virtual Result UnmarkUsing(u64 id) override final { + /* Try to unmark the entry. */ + for (size_t i = 0; i < m_entry_count; ++i) { + if (m_entries[i].memory.id == id) { + --m_entries[i].use_count; + R_SUCCEED(); + } + } + + /* We failed to unmark. */ + R_THROW(ncm::ResultMapperNotMapped()); + } + + virtual Result MarkDirty(u64) override final{ + R_THROW(ncm::ResultMapperNotSupported()); + } + }; + + template + class ExtendedDataMapperBase : public CacheMapperBase { + private: + static constexpr size_t MappingAlignment = 1_KB; + private: + util::optional m_mount_name = util::nullopt; + ncm::FileMapperFile m_file_mapper{}; + size_t m_extended_data_offset; + bool m_suppress_fs_auto_abort; + public: + template + ExtendedDataMapperBase(Args &&... args) : CacheMapperBase(std::forward(args)...) { /* ... */ } + + virtual ~ExtendedDataMapperBase() override { + /* Finalize. */ + this->Finalize(); + } + + Result Initialize(const char *content_path, bool suppress_fs_auto_abort) { + /* Set whether we should suppress fs aborts. */ + m_suppress_fs_auto_abort = suppress_fs_auto_abort; + + /* Suppress fs auto abort, if we need to. */ + auto disable_aborts = this->GetFsAutoAbortDisabler(); + + /* Mount the content. */ + auto mount_name = impl::CreateUniqueMountName(); + R_TRY(impl::MountContentMetaImpl(mount_name.str, content_path)); + + /* Set our mount name. */ + m_mount_name.emplace(mount_name.str); + + /* Open the root directory. */ + auto root_path = impl::GetRootDirectoryPath(mount_name); + fs::DirectoryHandle dir; + R_TRY(fs::OpenDirectory(std::addressof(dir), root_path.str, fs::OpenDirectoryMode_File)); + ON_SCOPE_EXIT { fs::CloseDirectory(dir); }; + + /* Loop directory reading until we find the entry we're looking for. */ + while (true) { + /* Read one entry, and finish when we fail to read. */ + fs::DirectoryEntry entry; + s64 num_read; + R_TRY(fs::ReadDirectory(std::addressof(num_read), std::addressof(entry), dir, 1)); + if (num_read == 0) { + break; + } + + /* If this is the content meta file, parse it. */ + if (IsContentMetaFileName(entry.name)) { + /* Create the file path. */ + impl::FilePathString file_path(root_path.str); + file_path.Append(entry.name); + + /* Setup our file mapped. */ + R_TRY(m_file_mapper.Initialize(file_path, FileMapperFile::OpenMode::Read)); + + /* Read the extended header. */ + PackagedContentMetaHeader pkg_header; + R_TRY(m_file_mapper.Read(0, std::addressof(pkg_header), sizeof(pkg_header))); + + /* Set our extended data offset. */ + m_extended_data_offset = PackagedContentMetaReader(std::addressof(pkg_header), sizeof(pkg_header)).GetExtendedDataOffset(); + + const size_t accessible_size = m_file_mapper.GetFileSize() >= m_extended_data_offset; + R_UNLESS(accessible_size, ncm::ResultInvalidContentMetaFileSize()); + + /* Set our accessible size. */ + this->SetSize(accessible_size); + R_SUCCEED(); + } + } + + R_THROW(ncm::ResultContentMetaNotFound()); + } + + void Finalize() { + /* Suppress fs auto abort, if we need to. */ + auto disable_aborts = this->GetFsAutoAbortDisabler(); + + /* Finalize our implementation. */ + CacheMapperBase::Finalize(); + + /* Finalize our file mapper. */ + m_file_mapper.Finalize(); + + /* Finalize our mount name. */ + if (m_mount_name.has_value()) { + fs::Unmount(m_mount_name.value().Get()); + m_mount_name = util::nullopt; + } + } + protected: + virtual Result MapImpl(MappedMemory *out, Span data, size_t offset, size_t size) override final { + /* Suppress fs auto abort, if we need to. */ + auto disable_aborts = this->GetFsAutoAbortDisabler(); + + /* Get the requested map offset/size. */ + u8 *map_data = data.data(); + size_t map_size = data.size_bytes(); + + /* Align the mapping, and ensure it remains valid. */ + const size_t aligned_offset = util::AlignDown(offset, MappingAlignment); + R_UNLESS((offset + size) - aligned_offset <= map_size, ncm::ResultMapperInvalidArgument()); + + /* Read the data. */ + const size_t map_offset = m_extended_data_offset + aligned_offset; + if (map_offset + map_size >= m_file_mapper.GetFileSize()) { + map_size = m_file_mapper.GetFileSize() - map_offset; + } + R_TRY(m_file_mapper.Read(map_offset, map_data, map_size)); + + /* Create the output mapped memory. */ + *out = MappedMemory { + .id = 0, + .offset = aligned_offset, + .buffer = map_data, + .buffer_size = map_size, + }; + R_SUCCEED(); + } + + virtual Result UnmapImpl(MappedMemory *) override final { + R_THROW(ncm::ResultMapperNotSupported()); + } + + virtual bool IsAccessibleSizeUpdatable() override final { + return false; + } + private: + util::optional GetFsAutoAbortDisabler() { + /* Create an abort disabler, if we should disable aborts. */ + util::optional disable_abort{util::nullopt}; + if (m_suppress_fs_auto_abort) { + disable_abort.emplace(); + } + return disable_abort; + } + }; + + template + using MultiCacheReadonlyMapper = ExtendedDataMapperBase>; + +} \ No newline at end of file diff --git a/libraries/libstratosphere/source/ncm/ncm_file_mapper_file.hpp b/libraries/libstratosphere/source/ncm/ncm_file_mapper_file.hpp new file mode 100644 index 000000000..7e8fb9caf --- /dev/null +++ b/libraries/libstratosphere/source/ncm/ncm_file_mapper_file.hpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include + +namespace ams::ncm { + + class FileMapperFile { + public: + enum class OpenMode { + Read, + ReadWrite, + ReadWriteAppend, + }; + private: + const char *m_path; + OpenMode m_mode; + util::optional m_file; + size_t m_file_size; + size_t m_max_size; + public: + FileMapperFile() : m_file(util::nullopt) { /* ... */ } + + ~FileMapperFile() { + this->Finalize(); + } + + Result Initialize(const char *path, OpenMode mode) { + /* Set our path/mode. */ + m_path = path; + m_mode = mode; + + /* Ensure we're open. */ + R_TRY(this->EnsureOpen()); + + /* Get the file size. */ + s64 size; + R_TRY(fs::GetFileSize(std::addressof(size), m_file.value())); + + /* Set our file size/loaded size. */ + m_file_size = static_cast(size); + m_max_size = static_cast(size); + + R_SUCCEED(); + } + + void Finalize() { + /* If we have a file, close (and flush) it. */ + if (m_file.has_value()) { + if (m_mode != OpenMode::Read) { + R_ABORT_UNLESS(fs::FlushFile(m_file.value())); + } + fs::CloseFile(m_file.value()); + m_file = util::nullopt; + } + } + + size_t GetFileSize() const { return m_file_size; } + size_t GetMaxSize() const { return m_max_size; } + + Result Read(size_t offset, void *dst, size_t size) { + /* Determine the end offset. */ + const size_t end_offset = offset + size; + + /* Unless we're allowed to append, we need to have a big enough file. */ + if (m_mode != OpenMode::ReadWriteAppend) { + R_UNLESS(end_offset <= m_file_size, ncm::ResultMapperInvalidArgument()); + } + + /* Clear the output. */ + std::memset(dst, 0, size); + + /* Check that our offset is valid. */ + R_UNLESS(offset <= m_file_size, ncm::ResultMapperInvalidArgument()); + + /* Ensure we're open. */ + R_TRY(this->EnsureOpen()); + + /* Read what we can. */ + const size_t read_size = (offset + size >= m_file_size) ? (m_file_size - offset) : size; + AMS_ASSERT(read_size >= size); + + R_TRY(fs::ReadFile(m_file.value(), offset, dst, read_size)); + + /* Update our max size. */ + m_max_size = std::max(m_max_size, offset + read_size); + + R_SUCCEED(); + } + private: + Result EnsureOpen() { + /* If we've opened, we're done. */ + R_SUCCEED_IF(m_file.has_value()); + + /* Open based on our mode. */ + fs::FileHandle file; + switch (m_mode) { + case OpenMode::Read: R_TRY(fs::OpenFile(std::addressof(file), m_path, fs::OpenMode_Read)); break; + case OpenMode::ReadWrite: R_TRY(fs::OpenFile(std::addressof(file), m_path, fs::OpenMode_ReadWrite)); break; + case OpenMode::ReadWriteAppend: R_TRY(fs::OpenFile(std::addressof(file), m_path, fs::OpenMode_All)); break; + AMS_UNREACHABLE_DEFAULT_CASE(); + } + + /* Set our file. */ + m_file = file; + R_SUCCEED(); + } + }; + +} \ No newline at end of file diff --git a/libraries/libstratosphere/source/ncm/ncm_fs_utils.hpp b/libraries/libstratosphere/source/ncm/ncm_fs_utils.hpp index 6a977fa52..4499a2046 100644 --- a/libraries/libstratosphere/source/ncm/ncm_fs_utils.hpp +++ b/libraries/libstratosphere/source/ncm/ncm_fs_utils.hpp @@ -18,6 +18,8 @@ namespace ams::ncm::impl { + using FilePathString = kvdb::BoundedString<64>; + Result CopyFile(const char *dst_path, const char *src_path); class PathView { @@ -34,6 +36,8 @@ namespace ams::ncm::impl { char str[fs::MountNameLengthMax + 1]; }; + using MountNameString = kvdb::BoundedString; + struct RootDirectoryPath { char str[fs::MountNameLengthMax + 3]; /* mount name + :/ */ }; @@ -41,4 +45,6 @@ namespace ams::ncm::impl { MountName CreateUniqueMountName(); RootDirectoryPath GetRootDirectoryPath(const MountName &mount_name); + Result MountContentMetaImpl(const char *mount_name, const char *path); + } diff --git a/libraries/libstratosphere/source/ncm/ncm_install_task_base.cpp b/libraries/libstratosphere/source/ncm/ncm_install_task_base.cpp index 784851919..3fd97d8c6 100644 --- a/libraries/libstratosphere/source/ncm/ncm_install_task_base.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_install_task_base.cpp @@ -14,6 +14,7 @@ * along with this program. If not, see . */ #include +#include "ncm_extended_data_mapper.hpp" namespace ams::ncm { @@ -989,10 +990,7 @@ namespace ams::ncm { Result InstallTaskBase::GetInstallContentMetaDataFromPath(AutoBuffer *out, const Path &path, const InstallContentInfo &content_info, util::optional source_version) { AutoBuffer meta; - { - fs::ScopedAutoAbortDisabler aad; - R_TRY(ReadContentMetaPath(std::addressof(meta), path.str)); - } + R_TRY(ReadContentMetaPathWithoutExtendedDataOrDigestSuppressingFsAbort(std::addressof(meta), path.str)); /* Create a reader. */ PackagedContentMetaReader reader(meta.Get(), meta.GetSize()); @@ -1000,10 +998,20 @@ namespace ams::ncm { AutoBuffer install_meta_data; if (source_version) { - /* Convert to fragment only install content meta. */ - R_TRY(reader.CalculateConvertFragmentOnlyInstallContentMetaSize(std::addressof(meta_size), *source_version)); - R_TRY(install_meta_data.Initialize(meta_size)); - reader.ConvertToFragmentOnlyInstallContentMeta(install_meta_data.Get(), install_meta_data.GetSize(), content_info, *source_version); + /* Declare buffers. */ + constexpr size_t BufferCount = 2; + constexpr size_t BufferSize = 3_KB; + u8 buffers[BufferCount][BufferSize]; + + /* Create a mapper. */ + auto mapper = MultiCacheReadonlyMapper<4>(Span(buffers[0], sizeof(buffers[0])), Span(buffers[1], sizeof(buffers[1]))); + R_TRY(mapper.Initialize(path.str, true)); + + /* Create an accessor. */ + auto accessor = PatchMetaExtendedDataAccessor{std::addressof(mapper)}; + + /* Convert to fragment only install meta. */ + R_TRY(MetaConverter::GetFragmentOnlyInstallContentMeta(std::addressof(install_meta_data), content_info, reader, std::addressof(accessor), source_version.value())); } else { /* Convert to install content meta. */ meta_size = reader.CalculateConvertInstallContentMetaSize(); diff --git a/libraries/libstratosphere/source/ncm/ncm_make_path.cpp b/libraries/libstratosphere/source/ncm/ncm_make_path.cpp index e300d586a..63ce44034 100644 --- a/libraries/libstratosphere/source/ncm/ncm_make_path.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_make_path.cpp @@ -20,7 +20,7 @@ namespace ams::ncm { namespace { void MakeContentName(PathString *out, ContentId id) { - out->SetFormat("%s.nca", GetContentIdString(id).data); + out->AssignFormat("%s.nca", GetContentIdString(id).data); } void MakePlaceHolderName(PathString *out, PlaceHolderId id) { @@ -63,7 +63,7 @@ namespace ams::ncm { MakeContentName(std::addressof(content_name), content_id); /* Format the output path. */ - out->SetFormat("%s/%s", root_path, content_name.Get()); + out->AssignFormat("%s/%s", root_path, content_name.Get()); } void MakeSha256HierarchicalContentFilePath_ForFat4KCluster(PathString *out, ContentId content_id, const char *root_path) { @@ -77,7 +77,7 @@ namespace ams::ncm { MakeContentName(std::addressof(content_name), content_id); /* Format the output path. */ - out->SetFormat("%s/%08X/%08X/%s", root_path, hash_upper, hash_lower, content_name.Get()); + out->AssignFormat("%s/%08X/%08X/%s", root_path, hash_upper, hash_lower, content_name.Get()); } void MakeSha256HierarchicalContentFilePath_ForFat32KCluster(PathString *out, ContentId content_id, const char *root_path) { @@ -89,7 +89,7 @@ namespace ams::ncm { MakeContentName(std::addressof(content_name), content_id); /* Format the output path. */ - out->SetFormat("%s/%08X/%s", root_path, hash, content_name.Get()); + out->AssignFormat("%s/%08X/%s", root_path, hash, content_name.Get()); } void MakeSha256HierarchicalContentFilePath_ForFat16KCluster(PathString *out, ContentId content_id, const char *root_path) { @@ -101,7 +101,7 @@ namespace ams::ncm { MakeContentName(std::addressof(content_name), content_id); /* Format the output path. */ - out->SetFormat("%s/%08X/%s", root_path, hash_byte, content_name.Get()); + out->AssignFormat("%s/%08X/%s", root_path, hash_byte, content_name.Get()); } size_t GetHierarchicalContentDirectoryDepth(MakeContentPathFunction func) { @@ -123,7 +123,7 @@ namespace ams::ncm { MakePlaceHolderName(std::addressof(placeholder_name), placeholder_id); /* Format the output path. */ - out->SetFormat("%s/%s", root_path, placeholder_name.Get()); + out->AssignFormat("%s/%s", root_path, placeholder_name.Get()); } void MakeSha256HierarchicalPlaceHolderFilePath_ForFat16KCluster(PathString *out, PlaceHolderId placeholder_id, const char *root_path) { @@ -135,7 +135,7 @@ namespace ams::ncm { MakePlaceHolderName(std::addressof(placeholder_name), placeholder_id); /* Format the output path. */ - out->SetFormat("%s/%08X/%s", root_path, hash_byte, placeholder_name.Get()); + out->AssignFormat("%s/%08X/%s", root_path, hash_byte, placeholder_name.Get()); } size_t GetHierarchicalPlaceHolderDirectoryDepth(MakePlaceHolderPathFunction func) { diff --git a/libraries/libstratosphere/source/ncm/ncm_package_install_task_base.cpp b/libraries/libstratosphere/source/ncm/ncm_package_install_task_base.cpp index e9f875946..c8068fc75 100644 --- a/libraries/libstratosphere/source/ncm/ncm_package_install_task_base.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_package_install_task_base.cpp @@ -19,7 +19,7 @@ namespace ams::ncm { Result PackageInstallTaskBase::Initialize(const char *package_root_path, void *buffer, size_t buffer_size, StorageId storage_id, InstallTaskDataBase *data, u32 config) { R_TRY(InstallTaskBase::Initialize(storage_id, data, config)); - m_package_root.Set(package_root_path); + m_package_root.Assign(package_root_path); m_buffer = buffer; m_buffer_size = buffer_size; return ResultSuccess(); @@ -110,25 +110,25 @@ namespace ams::ncm { void PackageInstallTaskBase::CreateContentPath(PackagePath *out_path, ContentId content_id) { char str[ContentIdStringLength + 1] = {}; GetStringFromContentId(str, sizeof(str), content_id); - out_path->SetFormat("%s%s%s", m_package_root.Get(), str, ".nca"); + out_path->AssignFormat("%s%s%s", m_package_root.Get(), str, ".nca"); } void PackageInstallTaskBase::CreateContentMetaPath(PackagePath *out_path, ContentId content_id) { char str[ContentIdStringLength + 1] = {}; GetStringFromContentId(str, sizeof(str), content_id); - out_path->SetFormat("%s%s%s", m_package_root.Get(), str, ".cnmt.nca"); + out_path->AssignFormat("%s%s%s", m_package_root.Get(), str, ".cnmt.nca"); } void PackageInstallTaskBase::CreateTicketPath(PackagePath *out_path, fs::RightsId id) { char str[RightsIdStringLength + 1] = {}; GetStringFromRightsId(str, sizeof(str), id); - out_path->SetFormat("%s%s%s", m_package_root.Get(), str, ".tik"); + out_path->AssignFormat("%s%s%s", m_package_root.Get(), str, ".tik"); } void PackageInstallTaskBase::CreateCertificatePath(PackagePath *out_path, fs::RightsId id) { char str[RightsIdStringLength + 1] = {}; GetStringFromRightsId(str, sizeof(str), id); - out_path->SetFormat("%s%s%s", m_package_root.Get(), str, ".cert"); + out_path->AssignFormat("%s%s%s", m_package_root.Get(), str, ".cert"); } } diff --git a/libraries/libstratosphere/source/ncm/ncm_package_system_update_task.cpp b/libraries/libstratosphere/source/ncm/ncm_package_system_update_task.cpp index 9b3fb3d62..d9941e1d7 100644 --- a/libraries/libstratosphere/source/ncm/ncm_package_system_update_task.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_package_system_update_task.cpp @@ -66,7 +66,7 @@ namespace ams::ncm { meta_db_guard.Cancel(); /* Set the context path. */ - m_context_path.Set(context_path); + m_context_path.Assign(context_path); return ResultSuccess(); } diff --git a/libraries/libstratosphere/source/ncm/ncm_placeholder_accessor.cpp b/libraries/libstratosphere/source/ncm/ncm_placeholder_accessor.cpp index ab5e49a0b..81b8ce703 100644 --- a/libraries/libstratosphere/source/ncm/ncm_placeholder_accessor.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_placeholder_accessor.cpp @@ -30,7 +30,7 @@ namespace ams::ncm { constexpr inline size_t PlaceHolderFileNameLength = PlaceHolderFileNameLengthWithoutExtension + PlaceHolderExtensionLength; void MakeBasePlaceHolderDirectoryPath(PathString *out, const char *root_path) { - out->SetFormat("%s%s", root_path, BasePlaceHolderDirectory); + out->AssignFormat("%s%s", root_path, BasePlaceHolderDirectory); } void MakePlaceHolderFilePath(PathString *out, PlaceHolderId id, MakePlaceHolderPathFunction func, const char *root_path) { diff --git a/libraries/libstratosphere/source/ncm/ncm_read_only_content_storage_impl.cpp b/libraries/libstratosphere/source/ncm/ncm_read_only_content_storage_impl.cpp index cee0cca32..f3702e43f 100644 --- a/libraries/libstratosphere/source/ncm/ncm_read_only_content_storage_impl.cpp +++ b/libraries/libstratosphere/source/ncm/ncm_read_only_content_storage_impl.cpp @@ -31,7 +31,7 @@ namespace ams::ncm { func(std::addressof(path), id, root_path); /* Substitute the .nca extension with .cmnt.nca. */ - *out = path.GetSubstring(0, path.GetLength() - 4); + *out = path.MakeSubString(0, path.GetLength() - 4); out->Append(".cnmt.nca"); } @@ -55,7 +55,7 @@ namespace ams::ncm { Result ReadOnlyContentStorageImpl::Initialize(const char *path, MakeContentPathFunction content_path_func) { R_TRY(this->EnsureEnabled()); - m_root_path.Set(path); + m_root_path.Assign(path); m_make_content_path_func = content_path_func; return ResultSuccess(); } diff --git a/libraries/libstratosphere/source/pgl/srv/pgl_srv_shell_host_utils.cpp b/libraries/libstratosphere/source/pgl/srv/pgl_srv_shell_host_utils.cpp index a8e1acc62..e1ad2fa9a 100644 --- a/libraries/libstratosphere/source/pgl/srv/pgl_srv_shell_host_utils.cpp +++ b/libraries/libstratosphere/source/pgl/srv/pgl_srv_shell_host_utils.cpp @@ -263,7 +263,7 @@ namespace ams::pgl::srv { R_UNLESS(has_content, pgl::ResultContentMetaNotFound()); /* Read the content meta buffer. */ - return ncm::ReadContentMetaPath(std::addressof(m_content_meta_buffer), meta_path.str); + return ncm::ReadContentMetaPathWithoutExtendedDataOrDigest(std::addressof(m_content_meta_buffer), meta_path.str); } Result SearchContent(bool *out, lr::Path *out_path, const char *extension, fs::OpenDirectoryMode mode) const { diff --git a/libraries/libvapours/include/vapours/results/ncm_results.hpp b/libraries/libvapours/include/vapours/results/ncm_results.hpp index a3b4fb80e..dd46af9e0 100644 --- a/libraries/libvapours/include/vapours/results/ncm_results.hpp +++ b/libraries/libvapours/include/vapours/results/ncm_results.hpp @@ -44,12 +44,14 @@ namespace ams::ncm { R_DEFINE_ERROR_RESULT(ContentInfoNotFound, 220); R_DEFINE_ERROR_RESULT(DeltaNotFound, 237); R_DEFINE_ERROR_RESULT(InvalidContentMetaKey, 240); + R_DEFINE_ERROR_RESULT(FragmentIndicatorNotFound, 242); R_DEFINE_ERROR_RESULT(IgnorableInstallTicketFailure, 280); R_DEFINE_ERROR_RESULT(ContentStorageBaseNotFound, 310); R_DEFINE_ERROR_RESULT(ListPartiallyNotCommitted, 330); R_DEFINE_ERROR_RESULT(UnexpectedContentMetaPrepared, 360); R_DEFINE_ERROR_RESULT(InvalidFirmwareVariation, 380); + R_DEFINE_ERROR_RESULT(InvalidContentMetaFileSize, 390); R_DEFINE_ERROR_RANGE(ContentStorageNotActive, 250, 258); R_DEFINE_ERROR_RESULT(GameCardContentStorageNotActive, 251); @@ -69,6 +71,12 @@ namespace ams::ncm { R_DEFINE_ERROR_RESULT(CreatePlaceHolderCancelled, 291); R_DEFINE_ERROR_RESULT(WritePlaceHolderCancelled, 292); + /* TODO: Range */ + R_DEFINE_ERROR_RESULT(MapperBusy, 1010); + R_DEFINE_ERROR_RESULT(MapperInvalidArgument, 1030); + R_DEFINE_ERROR_RESULT(MapperNotSupported, 1040); + R_DEFINE_ERROR_RESULT(MapperNotMapped, 1050); + R_DEFINE_ERROR_RESULT(InvalidOperation, 8180); R_DEFINE_ERROR_RANGE(InvalidArgument, 8181, 8191); R_DEFINE_ERROR_RESULT(InvalidOffset, 8182); diff --git a/libraries/libvapours/source/crypto/impl/crypto_cbc_mac_impl.cpp b/libraries/libvapours/source/crypto/impl/crypto_cbc_mac_impl.cpp index f4b047f6d..c7d5e4f4b 100644 --- a/libraries/libvapours/source/crypto/impl/crypto_cbc_mac_impl.cpp +++ b/libraries/libvapours/source/crypto/impl/crypto_cbc_mac_impl.cpp @@ -51,6 +51,7 @@ namespace ams::crypto::impl { /* Check pre-conditions. */ AMS_ASSERT(m_state == State_Initialized || m_state == State_Done); AMS_ASSERT(mac_size >= BlockSize); + AMS_UNUSED(mac_size); /* Ensure we're done. */ if (m_state == State_Initialized) { diff --git a/stratosphere/ams_mitm/source/sysupdater/sysupdater_service.cpp b/stratosphere/ams_mitm/source/sysupdater/sysupdater_service.cpp index 0893a1ada..aa497d72d 100644 --- a/stratosphere/ams_mitm/source/sysupdater/sysupdater_service.cpp +++ b/stratosphere/ams_mitm/source/sysupdater/sysupdater_service.cpp @@ -69,7 +69,7 @@ namespace ams::mitm::sysupdater { char path[ams::fs::EntryNameLengthMax]; R_TRY(ConvertToFsCommonPath(path, sizeof(path), package_root_path, entry.name)); - return ncm::ReadContentMetaPath(out, path); + return ncm::ReadContentMetaPathAlongWithExtendedDataAndDigest(out, path); } Result ReadContentMetaPath(ncm::AutoBuffer *out, const char *package_root, const ncm::ContentInfo &content_info) { @@ -84,7 +84,7 @@ namespace ams::mitm::sysupdater { R_TRY(ConvertToFsCommonPath(content_path.str, sizeof(content_path.str), package_root, cnmt_nca_name)); /* Read the content meta path. */ - return ncm::ReadContentMetaPath(out, content_path.str); + return ncm::ReadContentMetaPathAlongWithExtendedDataAndDigest(out, content_path.str); } Result GetSystemUpdateUpdateContentInfoFromPackage(ncm::ContentInfo *out, const char *package_root) {