ncm: update client code to better reflect latest sysupdate

This commit is contained in:
Michael Scire 2022-03-18 20:11:26 -07:00 committed by SciresM
parent ca142889c4
commit 320a946fc7
28 changed files with 1432 additions and 110 deletions

View file

@ -24,11 +24,6 @@ namespace ams::kvdb {
static_assert(N > 0, "BoundedString requires non-zero backing buffer!"); static_assert(N > 0, "BoundedString requires non-zero backing buffer!");
private: private:
char m_buffer[N]; char m_buffer[N];
private:
/* Utility. */
static inline void CheckLength(size_t len) {
AMS_ABORT_UNLESS(len < N);
}
public: public:
/* Constructors. */ /* Constructors. */
constexpr BoundedString() { constexpr BoundedString() {
@ -36,7 +31,8 @@ namespace ams::kvdb {
} }
explicit constexpr BoundedString(const char *s) { explicit constexpr BoundedString(const char *s) {
this->Set(s); AMS_ABORT_UNLESS(static_cast<size_t>(util::Strnlen(s, N)) < N);
util::Strlcpy(m_buffer, s, N);
} }
/* Static constructors. */ /* Static constructors. */
@ -44,102 +40,114 @@ namespace ams::kvdb {
return BoundedString<N>(s); return BoundedString<N>(s);
} }
static constexpr BoundedString<N> MakeFormat(const char *format, ...) __attribute__((format (printf, 1, 2))) { static BoundedString<N> MakeFormat(const char *format, ...) __attribute__((format (printf, 1, 2))) {
BoundedString<N> string; BoundedString<N> string;
std::va_list args; std::va_list vl;
va_start(args, format); va_start(vl, format);
CheckLength(util::VSNPrintf(string.m_buffer, N, format, args)); AMS_ABORT_UNLESS(static_cast<size_t>(util::VSNPrintf(string.m_buffer, N, format, vl)) < N);
string.m_buffer[N - 1] = 0; va_end(vl);
va_end(args);
return string; return string;
} }
/* Getters. */ /* Getters. */
size_t GetLength() const { constexpr size_t GetLength() const {
return util::Strnlen(m_buffer, N); return util::Strnlen(m_buffer, N);
} }
const char *Get() const { constexpr const char *Get() const {
return m_buffer; return m_buffer;
} }
operator const char *() const { constexpr operator const char *() const {
return m_buffer; return m_buffer;
} }
/* Setters. */ /* Assignment. */
void Set(const char *s) { constexpr BoundedString<N> &Assign(const char *s) {
/* Ensure string can fit in our buffer. */ AMS_ABORT_UNLESS(static_cast<size_t>(util::Strnlen(s, N)) < N);
CheckLength(util::Strnlen(s, N)); util::Strlcpy(m_buffer, s, N);
std::strncpy(m_buffer, s, N);
m_buffer[N - 1] = 0; return *this;
} }
void SetFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) { BoundedString<N> &AssignFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) {
/* Format into the buffer, abort if too large. */ std::va_list vl;
std::va_list args; va_start(vl, format);
va_start(args, format); AMS_ABORT_UNLESS(static_cast<size_t>(util::VSNPrintf(m_buffer, N, format, vl)) < N);
CheckLength(util::VSNPrintf(m_buffer, N, format, args)); va_end(vl);
va_end(args);
return *this;
} }
/* Append to existing. */ /* Append to existing. */
void Append(const char *s) { BoundedString<N> &Append(const char *s) {
const size_t length = GetLength(); const size_t length = GetLength();
CheckLength(length + util::Strnlen(s, N)); AMS_ABORT_UNLESS(length + static_cast<size_t>(util::Strnlen(s, N)) < N);
std::strncat(m_buffer, s, N - length - 1); std::strncat(m_buffer, s, N - length - 1);
return *this;
} }
void Append(char c) { BoundedString<N> &Append(char c) {
const size_t length = GetLength(); const size_t length = GetLength();
CheckLength(length + 1); AMS_ABORT_UNLESS(length + 1 < N);
m_buffer[length] = c; m_buffer[length] = c;
m_buffer[length + 1] = 0; m_buffer[length + 1] = 0;
return *this;
} }
void AppendFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) { BoundedString<N> &AppendFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) {
const size_t length = GetLength(); const size_t length = GetLength();
std::va_list args;
va_start(args, format); std::va_list vl;
CheckLength(util::VSNPrintf(m_buffer + length, N - length, format, args) + length); va_start(vl, format);
va_end(args); AMS_ABORT_UNLESS(static_cast<size_t>(util::VSNPrintf(m_buffer + length, N - length, format, vl)) < static_cast<size_t>(N - length));
va_end(vl);
return *this;
} }
/* Substring utilities. */ /* 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. */ /* Make sure output buffer can hold the substring. */
AMS_ABORT_UNLESS(offset + length <= GetLength()); AMS_ABORT_UNLESS(offset + length <= GetLength());
AMS_ABORT_UNLESS(dst_size > length); AMS_ABORT_UNLESS(dst_size > length);
/* Copy substring to dst. */ /* Copy substring to dst. */
std::strncpy(dst, m_buffer + offset, length); std::strncpy(dst, m_buffer + offset, length);
dst[length] = 0; dst[length] = 0;
} }
BoundedString<N> GetSubstring(size_t offset, size_t length) const { BoundedString<N> MakeSubString(size_t offset, size_t length) const {
BoundedString<N> string; BoundedString<N> string;
GetSubstring(string.m_buffer, N, offset, length); GetSubString(string.m_buffer, N, offset, length);
return string; return string;
} }
/* Comparison. */ /* 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<N> &rhs) const { constexpr bool operator==(const BoundedString<N> &rhs) const {
return std::strncmp(m_buffer, rhs.m_buffer, N) == 0; return this->Equals(rhs.m_buffer);
} }
constexpr bool operator!=(const BoundedString<N> &rhs) const { constexpr bool operator!=(const BoundedString<N> &rhs) const {
return !(*this == rhs); return !(*this == rhs);
} }
bool EndsWith(const char *s, size_t offset) const { constexpr bool EqualsPostfix(const char *s) const {
return std::strncmp(m_buffer + offset, s, N - offset) == 0;
}
bool EndsWith(const char *s) const {
const size_t suffix_length = util::Strnlen(s, N); const size_t suffix_length = util::Strnlen(s, N);
const size_t length = GetLength(); const size_t length = GetLength();
return suffix_length <= length && EndsWith(s, length - suffix_length); return suffix_length <= length && this->Equals(s, length - suffix_length);
} }
}; };

View file

@ -77,7 +77,7 @@ namespace ams::kvdb {
/* Setup member variables. */ /* Setup member variables. */
m_keys = static_cast<Key *>(buf); m_keys = static_cast<Key *>(buf);
m_file_path.Set(path); m_file_path.Assign(path);
std::memset(m_keys, 0, BufferSize); std::memset(m_keys, 0, BufferSize);
/* Open file. */ /* Open file. */

View file

@ -269,8 +269,8 @@ namespace ams::kvdb {
R_UNLESS(entry_type == fs::DirectoryEntryType_Directory, fs::ResultPathNotFound()); R_UNLESS(entry_type == fs::DirectoryEntryType_Directory, fs::ResultPathNotFound());
/* Set paths. */ /* Set paths. */
m_path.SetFormat("%s%s", dir, "/imkvdb.arc"); m_path.AssignFormat("%s%s", dir, "/imkvdb.arc");
m_temp_path.SetFormat("%s%s", dir, "/imkvdb.tmp"); m_temp_path.AssignFormat("%s%s", dir, "/imkvdb.tmp");
/* Initialize our index. */ /* Initialize our index. */
R_TRY(m_index.Initialize(capacity, mr)); R_TRY(m_index.Initialize(capacity, mr));
@ -282,8 +282,8 @@ namespace ams::kvdb {
Result Initialize(size_t capacity, MemoryResource *mr) { Result Initialize(size_t capacity, MemoryResource *mr) {
/* This initializes without an archive file. */ /* This initializes without an archive file. */
/* A store initialized this way cannot have its contents loaded from or flushed to disk. */ /* A store initialized this way cannot have its contents loaded from or flushed to disk. */
m_path.Set(""); m_path.Assign("");
m_temp_path.Set(""); m_temp_path.Assign("");
/* Initialize our index. */ /* Initialize our index. */
R_TRY(m_index.Initialize(capacity, mr)); R_TRY(m_index.Initialize(capacity, mr));

View file

@ -335,6 +335,10 @@ namespace ams::ncm {
size_t CalculateConvertContentMetaSize() const; size_t CalculateConvertContentMetaSize() const;
void ConvertToContentMeta(void *dst, size_t size, const ContentInfo &meta); void ConvertToContentMeta(void *dst, size_t size, const ContentInfo &meta);
size_t CalculateConvertFragmentOnlyInstallContentMetaSize(s32 fragment_count) const {
return CalculateSizeImpl<InstallContentMetaHeader, InstallContentInfo>(this->GetExtendedHeaderSize(), fragment_count + 1, 0, 0, false);
}
Result CalculateConvertFragmentOnlyInstallContentMetaSize(size_t *out_size, u32 source_version) const; Result CalculateConvertFragmentOnlyInstallContentMetaSize(size_t *out_size, u32 source_version) const;
Result ConvertToFragmentOnlyInstallContentMeta(void *dst, size_t size, const InstallContentInfo &content_info, u32 source_version); 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) { 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); return ContentMetaAccessor::CalculateSize(type, content_count, content_meta_count, extended_data_size, true);
} }
size_t GetExtendedDataOffset() const {
return this->GetExtendedDataAddress() - reinterpret_cast<uintptr_t>(this->GetData());
}
}; };
class InstallContentMetaReader : public ContentMetaAccessor<InstallContentMetaHeader, InstallContentInfo> { class InstallContentMetaReader : public ContentMetaAccessor<InstallContentMetaHeader, InstallContentInfo> {
@ -368,4 +376,15 @@ namespace ams::ncm {
using ContentMetaAccessor::SetStorageId; 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);
};
} }

View file

@ -18,6 +18,7 @@
#include <stratosphere/ncm/ncm_content_meta_id.hpp> #include <stratosphere/ncm/ncm_content_meta_id.hpp>
#include <stratosphere/ncm/ncm_content_meta.hpp> #include <stratosphere/ncm/ncm_content_meta.hpp>
#include <stratosphere/ncm/ncm_firmware_variation.hpp> #include <stratosphere/ncm/ncm_firmware_variation.hpp>
#include <stratosphere/ncm/ncm_mapped_memory.hpp>
namespace ams::ncm { namespace ams::ncm {
@ -381,4 +382,476 @@ namespace ams::ncm {
constexpr SystemUpdateMetaExtendedDataReader(const void *data, size_t size) : SystemUpdateMetaExtendedDataReaderWriterBase(data, size) { /* ... */ } constexpr SystemUpdateMetaExtendedDataReader(const void *data, size_t size) : SystemUpdateMetaExtendedDataReaderWriterBase(data, size) { /* ... */ }
}; };
template<typename T>
class ReadableStructPin;
class AccessorBase {
public:
template<typename T>
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<T *>(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<typename T>
Result AcquireReadableStructPin(ReadableStructPin<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));
/* 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<typename T>
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<const T *>(memory.GetBuffer(offset, sizeof(T)));
R_SUCCEED();
}
};
template<typename T>
class ReadableStructPin final : public AccessorBase::PinBase<const u8> {
public:
using PinBase::PinBase;
using PinBase::operator=;
const T *Get() const {
return reinterpret_cast<const T *>(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<CachedCount> m_cached_history_content_count = util::nullopt;
util::optional<CachedCount> m_cached_delta_content_count = util::nullopt;
util::optional<CachedCount> m_cached_fragment_set_count = util::nullopt;
util::optional<CachedCount> m_cached_fragment_indicator_count = util::nullopt;
util::optional<PatchMetaExtendedDataHeader> m_header = util::nullopt;
public:
using AccessorBase::AccessorBase;
public:
Result GetHeader(ReadableStructPin<PatchMetaExtendedDataHeader> *out) { return this->AcquireReadableStructPin(out, 0); }
Result GetHeader(PatchMetaExtendedDataHeader *out) { return this->template ReadStruct<PatchMetaExtendedDataHeader>(out, 0); }
Result GetHistoryHeader(ReadableStructPin<PatchHistoryHeader> *out, s32 index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= index && static_cast<u32>(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<PatchDeltaHistory> *out, s32 index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= index && static_cast<u32>(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<PatchDeltaHeader> *out, s32 index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= index && static_cast<u32>(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<FragmentSet> *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<u32>(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<FragmentSet> *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<u32>(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<ContentInfo> *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<u32>(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<u32>(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<PackagedContentInfo> *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<u32>(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<u32>(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<FragmentIndicator> *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<u32>(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<FragmentIndicator> *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<u32>(delta_index) < m_header->delta_count, ncm::ResultInvalidOffset());
/* Get the fragment count. */
s32 fragment_count = 0;
{
ReadableStructPin<FragmentSet> 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<FragmentIndicator> 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<PatchHistoryHeader> 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<PatchDeltaHistory> 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<PatchDeltaHeader> 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<FragmentSet> 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<ContentInfo> 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<PackagedContentInfo> 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<FragmentIndicator> 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<FragmentIndicator> 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<ncm::PatchHistoryHeader> 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<ncm::PatchDeltaHeader> 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<ncm::PatchDeltaHeader> 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<ncm::FragmentSet> 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<CachedCount> &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();
}
};
} }

View file

@ -25,7 +25,14 @@ namespace ams::ncm {
using MountContentMetaFunction = Result (*)(const char *mount_name, const char *path); 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<ContentMetaInfo[]> *out_meta_infos, const Path &path, FirmwareVariationId firmware_variation_id); Result ReadVariationContentMetaInfoList(s32 *out_count, std::unique_ptr<ContentMetaInfo[]> *out_meta_infos, const Path &path, FirmwareVariationId firmware_variation_id);
void SetMountContentMetaFunction(MountContentMetaFunction func); void SetMountContentMetaFunction(MountContentMetaFunction func);

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
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<MappedMemory>::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<u8> data, size_t offset, size_t size) = 0;
virtual Result UnmapImpl(MappedMemory *mem) = 0;
virtual bool IsAccessibleSizeUpdatable() = 0;
};
}

View file

@ -52,7 +52,7 @@ namespace ams::gc::impl {
Result GcCrypto::VerifyT1CardCertificate(const void *cert_buffer, size_t cert_size) { Result GcCrypto::VerifyT1CardCertificate(const void *cert_buffer, size_t cert_size) {
/* Check pre-conditions. */ /* Check pre-conditions. */
AMS_ASSERT(cert_size == sizeof(T1CardCertificate)); R_UNLESS(cert_size == sizeof(T1CardCertificate), fs::ResultGameCardPreconditionViolation());
/* Get cert buffer as type. */ /* Get cert buffer as type. */
const auto * const cert = static_cast<const T1CardCertificate *>(cert_buffer); const auto * const cert = static_cast<const T1CardCertificate *>(cert_buffer);
@ -75,7 +75,7 @@ namespace ams::gc::impl {
Result GcCrypto::VerifyCa10Certificate(const void *cert_buffer, size_t cert_size) { Result GcCrypto::VerifyCa10Certificate(const void *cert_buffer, size_t cert_size) {
/* Check pre-conditions. */ /* Check pre-conditions. */
AMS_ASSERT(cert_size == sizeof(Ca10Certificate)); R_UNLESS(cert_size == sizeof(Ca10Certificate), fs::ResultGameCardPreconditionViolation());
/* Get header buffer as type. */ /* Get header buffer as type. */
const auto * const cert = static_cast<const Ca10Certificate *>(cert_buffer); const auto * const cert = static_cast<const Ca10Certificate *>(cert_buffer);

View file

@ -154,7 +154,7 @@ namespace ams::kvdb {
const size_t file_name_len = file_name.GetLength(); const size_t file_name_len = file_name.GetLength();
const size_t key_name_len = file_name_len - FileExtensionLength; const size_t key_name_len = file_name_len - FileExtensionLength;
R_UNLESS(file_name_len >= FileExtensionLength + 2, kvdb::ResultInvalidKeyValue()); 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()); R_UNLESS(util::IsAligned(key_name_len, 2), kvdb::ResultInvalidKeyValue());
/* Validate that we have space for the converted key. */ /* Validate that we have space for the converted key. */
@ -165,7 +165,7 @@ namespace ams::kvdb {
u8 *out_key = static_cast<u8 *>(_out_key); u8 *out_key = static_cast<u8 *>(_out_key);
for (size_t i = 0; i < key_size; i++) { for (size_t i = 0; i < key_size; i++) {
char substr[2 * sizeof(u8) + 1]; 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<u8>(std::strtoul(substr, nullptr, 0x10)); out_key[i] = static_cast<u8>(std::strtoul(substr, nullptr, 0x10));
} }
@ -184,7 +184,7 @@ namespace ams::kvdb {
R_UNLESS(entry_type == fs::DirectoryEntryType_Directory, fs::ResultPathNotFound()); R_UNLESS(entry_type == fs::DirectoryEntryType_Directory, fs::ResultPathNotFound());
/* Set path. */ /* Set path. */
m_dir_path.Set(dir); m_dir_path.Assign(dir);
/* Initialize our cache. */ /* Initialize our cache. */
R_TRY(m_cache.Initialize(cache_buffer, cache_buffer_size, cache_capacity)); R_TRY(m_cache.Initialize(cache_buffer, cache_buffer_size, cache_capacity));

View file

@ -18,6 +18,7 @@
namespace ams::ncm { namespace ams::ncm {
namespace { namespace {
constexpr inline s64 EncryptionMetadataSize = 16_KB; constexpr inline s64 EncryptionMetadataSize = 16_KB;
constexpr inline s64 ConcatenationFileSizeMax = 4_GB; constexpr inline s64 ConcatenationFileSizeMax = 4_GB;

View file

@ -37,7 +37,7 @@ namespace ams::ncm {
char path[MaxPackagePathLength]; char path[MaxPackagePathLength];
R_TRY(ConvertToFsCommonPath(path, sizeof(path), package_root_path, entry.name)); R_TRY(ConvertToFsCommonPath(path, sizeof(path), package_root_path, entry.name));
return ncm::ReadContentMetaPath(out, path); return ncm::ReadContentMetaPathWithoutExtendedDataOrDigest(out, path);
} }
template<typename F> template<typename F>
@ -108,7 +108,7 @@ namespace ams::ncm {
/* Read the content meta path, and build. */ /* Read the content meta path, and build. */
ncm::AutoBuffer package_meta; 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. */ /* Get the size of the content. */
s64 size; s64 size;
R_TRY(storage->GetSize(std::addressof(size), content_id)); R_TRY(storage->GetSize(std::addressof(size), content_id));

View file

@ -255,11 +255,11 @@ namespace ams::ncm {
/* Print the savedata path. */ /* Print the savedata path. */
PathString savedata_db_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. */ /* Print a path for the mounted partition. */
PathString bis_db_path; 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. */ /* Mount the savedata. */
R_TRY(fs::MountSystemSaveData(root->mount_name, root->info.space_id, root->info.id)); R_TRY(fs::MountSystemSaveData(root->mount_name, root->info.space_id, root->info.id));

View file

@ -229,10 +229,10 @@ namespace ams::ncm {
R_TRY(FindDeltaIndex(std::addressof(index), reader, source_version, this->GetKey().version)); R_TRY(FindDeltaIndex(std::addressof(index), reader, source_version, this->GetKey().version));
/* Get the fragment count. */ /* Get the fragment count. */
auto fragment_count = CountContentExceptForMeta(reader, index); const auto fragment_count = CountContentExceptForMeta(reader, index);
/* Recalculate. */ /* Recalculate. */
*out_size = CalculateSizeImpl<InstallContentMetaHeader, InstallContentInfo>(this->GetExtendedHeaderSize(), fragment_count + 1, 0, this->GetExtendedDataSize(), false); *out_size = this->CalculateConvertFragmentOnlyInstallContentMetaSize(fragment_count);
return ResultSuccess(); 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<int>(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<s32>(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<uintptr_t>(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<u16>(fragment_count) + 1;
/* Copy the header. */
std::memcpy(reinterpret_cast<void *>(dst_addr), std::addressof(header), sizeof(header));
dst_addr += sizeof(header);
/* Copy the extended header. */
std::memcpy(reinterpret_cast<void *>(dst_addr), reader.GetExtendedHeader<void>(), packaged_header->extended_header_size);
dst_addr += packaged_header->extended_header_size;
/* Copy the top level meta. */
std::memcpy(reinterpret_cast<void *>(dst_addr), std::addressof(meta), sizeof(meta));
dst_addr += sizeof(meta);
s32 count = 0;
for (s32 i = 0; i < static_cast<s32>(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<void *>(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();
}
} }

View file

@ -20,12 +20,6 @@ namespace ams::ncm {
namespace { 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) { Result MountContentMetaByRemoteFileSystemProxy(const char *mount_name, const char *path) {
return fs::MountContent(mount_name, path, fs::ContentType_Meta); 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. */ /* Mount the content. */
auto mount_name = impl::CreateUniqueMountName(); 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); }; ON_SCOPE_EXIT { fs::Unmount(mount_name.str); };
/* Open the root directory. */ /* Open the root directory. */
@ -59,7 +65,7 @@ namespace ams::ncm {
/* If this is the content meta file, parse it. */ /* If this is the content meta file, parse it. */
if (IsContentMetaFileName(entry.name)) { if (IsContentMetaFileName(entry.name)) {
/* Create the file path. */ /* Create the file path. */
FilePathString file_path(root_path.str); impl::FilePathString file_path(root_path.str);
file_path.Append(entry.name); file_path.Append(entry.name);
/* Open the content meta file. */ /* Open the content meta file. */
@ -76,19 +82,89 @@ namespace ams::ncm {
R_TRY(out->Initialize(meta_size)); R_TRY(out->Initialize(meta_size));
/* Read the meta into the buffer. */ /* 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<size_t>(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<ContentMetaInfo[]> *out_meta_infos, const Path &path, FirmwareVariationId firmware_variation_id) { Result ReadVariationContentMetaInfoList(s32 *out_count, std::unique_ptr<ContentMetaInfo[]> *out_meta_infos, const Path &path, FirmwareVariationId firmware_variation_id) {
AutoBuffer meta; AutoBuffer meta;
{ R_TRY(ReadContentMetaPathAlongWithExtendedDataAndDigestSuppressingFsAbort(std::addressof(meta), path.str));
fs::ScopedAutoAbortDisabler aad;
R_TRY(ReadContentMetaPath(std::addressof(meta), path.str));
}
/* Create a reader for the content meta. */ /* Create a reader for the content meta. */
PackagedContentMetaReader reader(meta.Get(), meta.GetSize()); PackagedContentMetaReader reader(meta.Get(), meta.GetSize());

View file

@ -24,7 +24,7 @@ namespace ams::ncm {
constexpr inline const char * const BaseContentDirectory = "/registered"; constexpr inline const char * const BaseContentDirectory = "/registered";
void MakeBaseContentDirectoryPath(PathString *out, const char *root_path) { 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) { void MakeContentPath(PathString *out, ContentId id, MakeContentPathFunction func, const char *root_path) {
@ -83,7 +83,7 @@ namespace ams::ncm {
/* Path of the current entry. */ /* Path of the current entry. */
PathString current_path; 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. */ /* Call the process function. */
bool should_continue = true; bool should_continue = true;
@ -202,7 +202,7 @@ namespace ams::ncm {
Result ContentStorageImpl::ContentIterator::OpenDirectory(const char *dir) { Result ContentStorageImpl::ContentIterator::OpenDirectory(const char *dir) {
/* Set our current path. */ /* Set our current path. */
m_path.Set(dir); m_path.Assign(dir);
/* Open the directory. */ /* Open the directory. */
return this->OpenCurrentDirectory(); return this->OpenCurrentDirectory();
@ -230,7 +230,7 @@ namespace ams::ncm {
if (m_depth < m_max_depth) { if (m_depth < m_max_depth) {
/* Construct the full path for the subdirectory. */ /* Construct the full path for the subdirectory. */
PathString entry_path; 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. */ /* Open the subdirectory. */
R_TRY(this->OpenDirectory(entry_path.Get())); R_TRY(this->OpenDirectory(entry_path.Get()));
@ -288,7 +288,7 @@ namespace ams::ncm {
} }
/* Set the path to the parent directory. */ /* 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. */ /* Try to load again from the parent directory. */
return this->LoadEntries(); return this->LoadEntries();

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "ncm_file_mapper_file.hpp"
#include "ncm_fs_utils.hpp"
namespace ams::ncm {
namespace impl {
template<typename T>
concept IsMappedMemorySpan = std::same_as<T, Span<u8>>;
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<u8> 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<size_t>(m_accessible_size, size + offset);
/* Set the output memory. */
*out = m_mapped_memory;
out->buffer_size = std::min<size_t>(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<u8>(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<size_t MaxEntries>
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<impl::IsMappedMemorySpan... Args>
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<u8> 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<size_t>(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<u8>(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<typename CacheMapperBase>
class ExtendedDataMapperBase : public CacheMapperBase {
private:
static constexpr size_t MappingAlignment = 1_KB;
private:
util::optional<impl::MountNameString> m_mount_name = util::nullopt;
ncm::FileMapperFile m_file_mapper{};
size_t m_extended_data_offset;
bool m_suppress_fs_auto_abort;
public:
template<typename... Args>
ExtendedDataMapperBase(Args &&... args) : CacheMapperBase(std::forward<Args>(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<u8> 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<fs::ScopedAutoAbortDisabler> GetFsAutoAbortDisabler() {
/* Create an abort disabler, if we should disable aborts. */
util::optional<fs::ScopedAutoAbortDisabler> disable_abort{util::nullopt};
if (m_suppress_fs_auto_abort) {
disable_abort.emplace();
}
return disable_abort;
}
};
template<size_t N>
using MultiCacheReadonlyMapper = ExtendedDataMapperBase<MultiCacheReadonlyMapperBase<N>>;
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::ncm {
class FileMapperFile {
public:
enum class OpenMode {
Read,
ReadWrite,
ReadWriteAppend,
};
private:
const char *m_path;
OpenMode m_mode;
util::optional<fs::FileHandle> 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_t>(size);
m_max_size = static_cast<size_t>(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<size_t>(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();
}
};
}

View file

@ -18,6 +18,8 @@
namespace ams::ncm::impl { namespace ams::ncm::impl {
using FilePathString = kvdb::BoundedString<64>;
Result CopyFile(const char *dst_path, const char *src_path); Result CopyFile(const char *dst_path, const char *src_path);
class PathView { class PathView {
@ -34,6 +36,8 @@ namespace ams::ncm::impl {
char str[fs::MountNameLengthMax + 1]; char str[fs::MountNameLengthMax + 1];
}; };
using MountNameString = kvdb::BoundedString<sizeof(MountName{}.str)>;
struct RootDirectoryPath { struct RootDirectoryPath {
char str[fs::MountNameLengthMax + 3]; /* mount name + :/ */ char str[fs::MountNameLengthMax + 3]; /* mount name + :/ */
}; };
@ -41,4 +45,6 @@ namespace ams::ncm::impl {
MountName CreateUniqueMountName(); MountName CreateUniqueMountName();
RootDirectoryPath GetRootDirectoryPath(const MountName &mount_name); RootDirectoryPath GetRootDirectoryPath(const MountName &mount_name);
Result MountContentMetaImpl(const char *mount_name, const char *path);
} }

View file

@ -14,6 +14,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stratosphere.hpp> #include <stratosphere.hpp>
#include "ncm_extended_data_mapper.hpp"
namespace ams::ncm { namespace ams::ncm {
@ -989,10 +990,7 @@ namespace ams::ncm {
Result InstallTaskBase::GetInstallContentMetaDataFromPath(AutoBuffer *out, const Path &path, const InstallContentInfo &content_info, util::optional<u32> source_version) { Result InstallTaskBase::GetInstallContentMetaDataFromPath(AutoBuffer *out, const Path &path, const InstallContentInfo &content_info, util::optional<u32> source_version) {
AutoBuffer meta; AutoBuffer meta;
{ R_TRY(ReadContentMetaPathWithoutExtendedDataOrDigestSuppressingFsAbort(std::addressof(meta), path.str));
fs::ScopedAutoAbortDisabler aad;
R_TRY(ReadContentMetaPath(std::addressof(meta), path.str));
}
/* Create a reader. */ /* Create a reader. */
PackagedContentMetaReader reader(meta.Get(), meta.GetSize()); PackagedContentMetaReader reader(meta.Get(), meta.GetSize());
@ -1000,10 +998,20 @@ namespace ams::ncm {
AutoBuffer install_meta_data; AutoBuffer install_meta_data;
if (source_version) { if (source_version) {
/* Convert to fragment only install content meta. */ /* Declare buffers. */
R_TRY(reader.CalculateConvertFragmentOnlyInstallContentMetaSize(std::addressof(meta_size), *source_version)); constexpr size_t BufferCount = 2;
R_TRY(install_meta_data.Initialize(meta_size)); constexpr size_t BufferSize = 3_KB;
reader.ConvertToFragmentOnlyInstallContentMeta(install_meta_data.Get(), install_meta_data.GetSize(), content_info, *source_version); u8 buffers[BufferCount][BufferSize];
/* Create a mapper. */
auto mapper = MultiCacheReadonlyMapper<4>(Span<u8>(buffers[0], sizeof(buffers[0])), Span<u8>(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 { } else {
/* Convert to install content meta. */ /* Convert to install content meta. */
meta_size = reader.CalculateConvertInstallContentMetaSize(); meta_size = reader.CalculateConvertInstallContentMetaSize();

View file

@ -20,7 +20,7 @@ namespace ams::ncm {
namespace { namespace {
void MakeContentName(PathString *out, ContentId id) { 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) { void MakePlaceHolderName(PathString *out, PlaceHolderId id) {
@ -63,7 +63,7 @@ namespace ams::ncm {
MakeContentName(std::addressof(content_name), content_id); MakeContentName(std::addressof(content_name), content_id);
/* Format the output path. */ /* 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) { 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); MakeContentName(std::addressof(content_name), content_id);
/* Format the output path. */ /* 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) { 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); MakeContentName(std::addressof(content_name), content_id);
/* Format the output path. */ /* 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) { 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); MakeContentName(std::addressof(content_name), content_id);
/* Format the output path. */ /* 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) { size_t GetHierarchicalContentDirectoryDepth(MakeContentPathFunction func) {
@ -123,7 +123,7 @@ namespace ams::ncm {
MakePlaceHolderName(std::addressof(placeholder_name), placeholder_id); MakePlaceHolderName(std::addressof(placeholder_name), placeholder_id);
/* Format the output path. */ /* 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) { 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); MakePlaceHolderName(std::addressof(placeholder_name), placeholder_id);
/* Format the output path. */ /* 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) { size_t GetHierarchicalPlaceHolderDirectoryDepth(MakePlaceHolderPathFunction func) {

View file

@ -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) { 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)); 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 = buffer;
m_buffer_size = buffer_size; m_buffer_size = buffer_size;
return ResultSuccess(); return ResultSuccess();
@ -110,25 +110,25 @@ namespace ams::ncm {
void PackageInstallTaskBase::CreateContentPath(PackagePath *out_path, ContentId content_id) { void PackageInstallTaskBase::CreateContentPath(PackagePath *out_path, ContentId content_id) {
char str[ContentIdStringLength + 1] = {}; char str[ContentIdStringLength + 1] = {};
GetStringFromContentId(str, sizeof(str), content_id); 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) { void PackageInstallTaskBase::CreateContentMetaPath(PackagePath *out_path, ContentId content_id) {
char str[ContentIdStringLength + 1] = {}; char str[ContentIdStringLength + 1] = {};
GetStringFromContentId(str, sizeof(str), content_id); 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) { void PackageInstallTaskBase::CreateTicketPath(PackagePath *out_path, fs::RightsId id) {
char str[RightsIdStringLength + 1] = {}; char str[RightsIdStringLength + 1] = {};
GetStringFromRightsId(str, sizeof(str), id); 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) { void PackageInstallTaskBase::CreateCertificatePath(PackagePath *out_path, fs::RightsId id) {
char str[RightsIdStringLength + 1] = {}; char str[RightsIdStringLength + 1] = {};
GetStringFromRightsId(str, sizeof(str), id); 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");
} }
} }

View file

@ -66,7 +66,7 @@ namespace ams::ncm {
meta_db_guard.Cancel(); meta_db_guard.Cancel();
/* Set the context path. */ /* Set the context path. */
m_context_path.Set(context_path); m_context_path.Assign(context_path);
return ResultSuccess(); return ResultSuccess();
} }

View file

@ -30,7 +30,7 @@ namespace ams::ncm {
constexpr inline size_t PlaceHolderFileNameLength = PlaceHolderFileNameLengthWithoutExtension + PlaceHolderExtensionLength; constexpr inline size_t PlaceHolderFileNameLength = PlaceHolderFileNameLengthWithoutExtension + PlaceHolderExtensionLength;
void MakeBasePlaceHolderDirectoryPath(PathString *out, const char *root_path) { 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) { void MakePlaceHolderFilePath(PathString *out, PlaceHolderId id, MakePlaceHolderPathFunction func, const char *root_path) {

View file

@ -31,7 +31,7 @@ namespace ams::ncm {
func(std::addressof(path), id, root_path); func(std::addressof(path), id, root_path);
/* Substitute the .nca extension with .cmnt.nca. */ /* 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"); out->Append(".cnmt.nca");
} }
@ -55,7 +55,7 @@ namespace ams::ncm {
Result ReadOnlyContentStorageImpl::Initialize(const char *path, MakeContentPathFunction content_path_func) { Result ReadOnlyContentStorageImpl::Initialize(const char *path, MakeContentPathFunction content_path_func) {
R_TRY(this->EnsureEnabled()); R_TRY(this->EnsureEnabled());
m_root_path.Set(path); m_root_path.Assign(path);
m_make_content_path_func = content_path_func; m_make_content_path_func = content_path_func;
return ResultSuccess(); return ResultSuccess();
} }

View file

@ -263,7 +263,7 @@ namespace ams::pgl::srv {
R_UNLESS(has_content, pgl::ResultContentMetaNotFound()); R_UNLESS(has_content, pgl::ResultContentMetaNotFound());
/* Read the content meta buffer. */ /* 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 { Result SearchContent(bool *out, lr::Path *out_path, const char *extension, fs::OpenDirectoryMode mode) const {

View file

@ -44,12 +44,14 @@ namespace ams::ncm {
R_DEFINE_ERROR_RESULT(ContentInfoNotFound, 220); R_DEFINE_ERROR_RESULT(ContentInfoNotFound, 220);
R_DEFINE_ERROR_RESULT(DeltaNotFound, 237); R_DEFINE_ERROR_RESULT(DeltaNotFound, 237);
R_DEFINE_ERROR_RESULT(InvalidContentMetaKey, 240); R_DEFINE_ERROR_RESULT(InvalidContentMetaKey, 240);
R_DEFINE_ERROR_RESULT(FragmentIndicatorNotFound, 242);
R_DEFINE_ERROR_RESULT(IgnorableInstallTicketFailure, 280); R_DEFINE_ERROR_RESULT(IgnorableInstallTicketFailure, 280);
R_DEFINE_ERROR_RESULT(ContentStorageBaseNotFound, 310); R_DEFINE_ERROR_RESULT(ContentStorageBaseNotFound, 310);
R_DEFINE_ERROR_RESULT(ListPartiallyNotCommitted, 330); R_DEFINE_ERROR_RESULT(ListPartiallyNotCommitted, 330);
R_DEFINE_ERROR_RESULT(UnexpectedContentMetaPrepared, 360); R_DEFINE_ERROR_RESULT(UnexpectedContentMetaPrepared, 360);
R_DEFINE_ERROR_RESULT(InvalidFirmwareVariation, 380); R_DEFINE_ERROR_RESULT(InvalidFirmwareVariation, 380);
R_DEFINE_ERROR_RESULT(InvalidContentMetaFileSize, 390);
R_DEFINE_ERROR_RANGE(ContentStorageNotActive, 250, 258); R_DEFINE_ERROR_RANGE(ContentStorageNotActive, 250, 258);
R_DEFINE_ERROR_RESULT(GameCardContentStorageNotActive, 251); R_DEFINE_ERROR_RESULT(GameCardContentStorageNotActive, 251);
@ -69,6 +71,12 @@ namespace ams::ncm {
R_DEFINE_ERROR_RESULT(CreatePlaceHolderCancelled, 291); R_DEFINE_ERROR_RESULT(CreatePlaceHolderCancelled, 291);
R_DEFINE_ERROR_RESULT(WritePlaceHolderCancelled, 292); 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_RESULT(InvalidOperation, 8180);
R_DEFINE_ERROR_RANGE(InvalidArgument, 8181, 8191); R_DEFINE_ERROR_RANGE(InvalidArgument, 8181, 8191);
R_DEFINE_ERROR_RESULT(InvalidOffset, 8182); R_DEFINE_ERROR_RESULT(InvalidOffset, 8182);

View file

@ -51,6 +51,7 @@ namespace ams::crypto::impl {
/* Check pre-conditions. */ /* Check pre-conditions. */
AMS_ASSERT(m_state == State_Initialized || m_state == State_Done); AMS_ASSERT(m_state == State_Initialized || m_state == State_Done);
AMS_ASSERT(mac_size >= BlockSize); AMS_ASSERT(mac_size >= BlockSize);
AMS_UNUSED(mac_size);
/* Ensure we're done. */ /* Ensure we're done. */
if (m_state == State_Initialized) { if (m_state == State_Initialized) {

View file

@ -69,7 +69,7 @@ namespace ams::mitm::sysupdater {
char path[ams::fs::EntryNameLengthMax]; char path[ams::fs::EntryNameLengthMax];
R_TRY(ConvertToFsCommonPath(path, sizeof(path), package_root_path, entry.name)); 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) { 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)); R_TRY(ConvertToFsCommonPath(content_path.str, sizeof(content_path.str), package_root, cnmt_nca_name));
/* Read the content meta path. */ /* 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) { Result GetSystemUpdateUpdateContentInfoFromPackage(ncm::ContentInfo *out, const char *package_root) {