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

@ -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<const T1CardCertificate *>(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<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 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<u8 *>(_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<u8>(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));

View file

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

View file

@ -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<typename F>
@ -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));

View file

@ -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));

View file

@ -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<InstallContentMetaHeader, InstallContentInfo>(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<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 {
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<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) {
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());

View file

@ -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();

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 {
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<sizeof(MountName{}.str)>;
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);
}

View file

@ -14,6 +14,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#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<u32> 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<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 {
/* Convert to install content meta. */
meta_size = reader.CalculateConvertInstallContentMetaSize();

View file

@ -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) {

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) {
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");
}
}

View file

@ -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();
}

View file

@ -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) {

View file

@ -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();
}

View file

@ -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 {