summaryrefslogtreecommitdiffstats
path: root/src/core/file_sys
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/file_sys')
-rw-r--r--src/core/file_sys/bis_factory.cpp3
-rw-r--r--src/core/file_sys/bis_factory.h7
-rw-r--r--src/core/file_sys/card_image.cpp54
-rw-r--r--src/core/file_sys/card_image.h19
-rw-r--r--src/core/file_sys/content_archive.cpp187
-rw-r--r--src/core/file_sys/content_archive.h21
-rw-r--r--src/core/file_sys/control_metadata.cpp22
-rw-r--r--src/core/file_sys/control_metadata.h19
-rw-r--r--src/core/file_sys/directory.h2
-rw-r--r--src/core/file_sys/nca_metadata.cpp9
-rw-r--r--src/core/file_sys/nca_metadata.h2
-rw-r--r--src/core/file_sys/nca_patch.cpp210
-rw-r--r--src/core/file_sys/nca_patch.h150
-rw-r--r--src/core/file_sys/partition_filesystem.cpp12
-rw-r--r--src/core/file_sys/partition_filesystem.h4
-rw-r--r--src/core/file_sys/patch_manager.cpp159
-rw-r--r--src/core/file_sys/patch_manager.h64
-rw-r--r--src/core/file_sys/program_metadata.cpp11
-rw-r--r--src/core/file_sys/program_metadata.h7
-rw-r--r--src/core/file_sys/registered_cache.cpp151
-rw-r--r--src/core/file_sys/registered_cache.h58
-rw-r--r--src/core/file_sys/romfs.cpp11
-rw-r--r--src/core/file_sys/romfs.h1
-rw-r--r--src/core/file_sys/romfs_factory.cpp16
-rw-r--r--src/core/file_sys/romfs_factory.h3
-rw-r--r--src/core/file_sys/savedata_factory.cpp14
-rw-r--r--src/core/file_sys/savedata_factory.h2
-rw-r--r--src/core/file_sys/submission_package.cpp245
-rw-r--r--src/core/file_sys/submission_package.h76
-rw-r--r--src/core/file_sys/vfs.cpp22
-rw-r--r--src/core/file_sys/vfs.h32
-rw-r--r--src/core/file_sys/vfs_concat.cpp12
-rw-r--r--src/core/file_sys/vfs_concat.h10
-rw-r--r--src/core/file_sys/vfs_offset.cpp26
-rw-r--r--src/core/file_sys/vfs_offset.h27
-rw-r--r--src/core/file_sys/vfs_real.cpp14
-rw-r--r--src/core/file_sys/vfs_real.h28
-rw-r--r--src/core/file_sys/vfs_vector.cpp2
-rw-r--r--src/core/file_sys/vfs_vector.h1
-rw-r--r--src/core/file_sys/xts_archive.cpp22
-rw-r--r--src/core/file_sys/xts_archive.h5
41 files changed, 1554 insertions, 186 deletions
diff --git a/src/core/file_sys/bis_factory.cpp b/src/core/file_sys/bis_factory.cpp
index 08a7cea5a..205492897 100644
--- a/src/core/file_sys/bis_factory.cpp
+++ b/src/core/file_sys/bis_factory.cpp
@@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#include "core/file_sys/bis_factory.h"
+#include "core/file_sys/registered_cache.h"
namespace FileSys {
@@ -13,6 +14,8 @@ BISFactory::BISFactory(VirtualDir nand_root_)
usrnand_cache(std::make_shared<RegisteredCache>(
GetOrCreateDirectoryRelative(nand_root, "/user/Contents/registered"))) {}
+BISFactory::~BISFactory() = default;
+
std::shared_ptr<RegisteredCache> BISFactory::GetSystemNANDContents() const {
return sysnand_cache;
}
diff --git a/src/core/file_sys/bis_factory.h b/src/core/file_sys/bis_factory.h
index a970a5e2e..9523dd864 100644
--- a/src/core/file_sys/bis_factory.h
+++ b/src/core/file_sys/bis_factory.h
@@ -5,17 +5,20 @@
#pragma once
#include <memory>
-#include "core/loader/loader.h"
-#include "registered_cache.h"
+
+#include "core/file_sys/vfs.h"
namespace FileSys {
+class RegisteredCache;
+
/// File system interface to the Built-In Storage
/// This is currently missing accessors to BIS partitions, but seemed like a good place for the NAND
/// registered caches.
class BISFactory {
public:
explicit BISFactory(VirtualDir nand_root);
+ ~BISFactory();
std::shared_ptr<RegisteredCache> GetSystemNANDContents() const;
std::shared_ptr<RegisteredCache> GetUserNANDContents() const;
diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp
index d61a2ebe1..edfc1bbd4 100644
--- a/src/core/file_sys/card_image.cpp
+++ b/src/core/file_sys/card_image.cpp
@@ -9,7 +9,10 @@
#include "common/logging/log.h"
#include "core/file_sys/card_image.h"
+#include "core/file_sys/content_archive.h"
+#include "core/file_sys/nca_metadata.h"
#include "core/file_sys/partition_filesystem.h"
+#include "core/file_sys/submission_package.h"
#include "core/file_sys/vfs_offset.h"
#include "core/loader/loader.h"
@@ -38,20 +41,25 @@ XCI::XCI(VirtualFile file_) : file(std::move(file_)), partitions(0x4) {
for (XCIPartition partition :
{XCIPartition::Update, XCIPartition::Normal, XCIPartition::Secure, XCIPartition::Logo}) {
- auto raw = main_hfs.GetFile(partition_names[static_cast<size_t>(partition)]);
+ auto raw = main_hfs.GetFile(partition_names[static_cast<std::size_t>(partition)]);
if (raw != nullptr)
- partitions[static_cast<size_t>(partition)] = std::make_shared<PartitionFilesystem>(raw);
+ partitions[static_cast<std::size_t>(partition)] =
+ std::make_shared<PartitionFilesystem>(raw);
}
- program_nca_status = Loader::ResultStatus::ErrorXCIMissingProgramNCA;
+ secure_partition = std::make_shared<NSP>(
+ main_hfs.GetFile(partition_names[static_cast<std::size_t>(XCIPartition::Secure)]));
- auto result = AddNCAFromPartition(XCIPartition::Secure);
- if (result != Loader::ResultStatus::Success) {
- status = result;
- return;
- }
+ const auto secure_ncas = secure_partition->GetNCAsCollapsed();
+ std::copy(secure_ncas.begin(), secure_ncas.end(), std::back_inserter(ncas));
- result = AddNCAFromPartition(XCIPartition::Update);
+ program =
+ secure_partition->GetNCA(secure_partition->GetProgramTitleID(), ContentRecordType::Program);
+ program_nca_status = secure_partition->GetProgramStatus(secure_partition->GetProgramTitleID());
+ if (program_nca_status == Loader::ResultStatus::ErrorNSPMissingProgramNCA)
+ program_nca_status = Loader::ResultStatus::ErrorXCIMissingProgramNCA;
+
+ auto result = AddNCAFromPartition(XCIPartition::Update);
if (result != Loader::ResultStatus::Success) {
status = result;
return;
@@ -74,6 +82,8 @@ XCI::XCI(VirtualFile file_) : file(std::move(file_)), partitions(0x4) {
status = Loader::ResultStatus::Success;
}
+XCI::~XCI() = default;
+
Loader::ResultStatus XCI::GetStatus() const {
return status;
}
@@ -83,7 +93,11 @@ Loader::ResultStatus XCI::GetProgramNCAStatus() const {
}
VirtualDir XCI::GetPartition(XCIPartition partition) const {
- return partitions[static_cast<size_t>(partition)];
+ return partitions[static_cast<std::size_t>(partition)];
+}
+
+std::shared_ptr<NSP> XCI::GetSecurePartitionNSP() const {
+ return secure_partition;
}
VirtualDir XCI::GetSecurePartition() const {
@@ -102,6 +116,20 @@ VirtualDir XCI::GetLogoPartition() const {
return GetPartition(XCIPartition::Logo);
}
+u64 XCI::GetProgramTitleID() const {
+ return secure_partition->GetProgramTitleID();
+}
+
+std::shared_ptr<NCA> XCI::GetProgramNCA() const {
+ return program;
+}
+
+VirtualFile XCI::GetProgramNCAFile() const {
+ if (GetProgramNCA() == nullptr)
+ return nullptr;
+ return GetProgramNCA()->GetBaseFile();
+}
+
const std::vector<std::shared_ptr<NCA>>& XCI::GetNCAs() const {
return ncas;
}
@@ -141,11 +169,11 @@ bool XCI::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
}
Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) {
- if (partitions[static_cast<size_t>(part)] == nullptr) {
+ if (partitions[static_cast<std::size_t>(part)] == nullptr) {
return Loader::ResultStatus::ErrorXCIMissingPartition;
}
- for (const VirtualFile& file : partitions[static_cast<size_t>(part)]->GetFiles()) {
+ for (const VirtualFile& file : partitions[static_cast<std::size_t>(part)]->GetFiles()) {
if (file->GetExtension() != "nca")
continue;
auto nca = std::make_shared<NCA>(file);
@@ -160,7 +188,7 @@ Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) {
} else {
const u16 error_id = static_cast<u16>(nca->GetStatus());
LOG_CRITICAL(Loader, "Could not load NCA {}/{}, failed with error code {:04X} ({})",
- partition_names[static_cast<size_t>(part)], nca->GetName(), error_id,
+ partition_names[static_cast<std::size_t>(part)], nca->GetName(), error_id,
nca->GetStatus());
}
}
diff --git a/src/core/file_sys/card_image.h b/src/core/file_sys/card_image.h
index 54ab828d1..ce514dfa0 100644
--- a/src/core/file_sys/card_image.h
+++ b/src/core/file_sys/card_image.h
@@ -5,15 +5,22 @@
#pragma once
#include <array>
+#include <memory>
#include <vector>
#include "common/common_types.h"
#include "common/swap.h"
-#include "core/file_sys/content_archive.h"
#include "core/file_sys/vfs.h"
-#include "core/loader/loader.h"
+
+namespace Loader {
+enum class ResultStatus : u16;
+}
namespace FileSys {
+class NCA;
+enum class NCAContentType : u8;
+class NSP;
+
enum class GamecardSize : u8 {
S_1GB = 0xFA,
S_2GB = 0xF8,
@@ -57,6 +64,7 @@ enum class XCIPartition : u8 { Update, Normal, Secure, Logo };
class XCI : public ReadOnlyVfsDirectory {
public:
explicit XCI(VirtualFile file);
+ ~XCI() override;
Loader::ResultStatus GetStatus() const;
Loader::ResultStatus GetProgramNCAStatus() const;
@@ -64,11 +72,16 @@ public:
u8 GetFormatVersion() const;
VirtualDir GetPartition(XCIPartition partition) const;
+ std::shared_ptr<NSP> GetSecurePartitionNSP() const;
VirtualDir GetSecurePartition() const;
VirtualDir GetNormalPartition() const;
VirtualDir GetUpdatePartition() const;
VirtualDir GetLogoPartition() const;
+ u64 GetProgramTitleID() const;
+
+ std::shared_ptr<NCA> GetProgramNCA() const;
+ VirtualFile GetProgramNCAFile() const;
const std::vector<std::shared_ptr<NCA>>& GetNCAs() const;
std::shared_ptr<NCA> GetNCAByType(NCAContentType type) const;
VirtualFile GetNCAFileByType(NCAContentType type) const;
@@ -94,6 +107,8 @@ private:
Loader::ResultStatus program_nca_status;
std::vector<VirtualDir> partitions;
+ std::shared_ptr<NSP> secure_partition;
+ std::shared_ptr<NCA> program;
std::vector<std::shared_ptr<NCA>> ncas;
};
} // namespace FileSys
diff --git a/src/core/file_sys/content_archive.cpp b/src/core/file_sys/content_archive.cpp
index e8b5d6ece..aa1b3c17d 100644
--- a/src/core/file_sys/content_archive.cpp
+++ b/src/core/file_sys/content_archive.cpp
@@ -3,12 +3,17 @@
// Refer to the license.txt file included.
#include <algorithm>
+#include <cstring>
#include <utility>
+
#include <boost/optional.hpp>
+
#include "common/logging/log.h"
#include "core/crypto/aes_util.h"
#include "core/crypto/ctr_encryption_layer.h"
#include "core/file_sys/content_archive.h"
+#include "core/file_sys/nca_patch.h"
+#include "core/file_sys/partition_filesystem.h"
#include "core/file_sys/romfs.h"
#include "core/file_sys/vfs_offset.h"
#include "core/loader/loader.h"
@@ -64,10 +69,31 @@ struct RomFSSuperblock {
};
static_assert(sizeof(RomFSSuperblock) == 0x200, "RomFSSuperblock has incorrect size.");
+struct BKTRHeader {
+ u64_le offset;
+ u64_le size;
+ u32_le magic;
+ INSERT_PADDING_BYTES(0x4);
+ u32_le number_entries;
+ INSERT_PADDING_BYTES(0x4);
+};
+static_assert(sizeof(BKTRHeader) == 0x20, "BKTRHeader has incorrect size.");
+
+struct BKTRSuperblock {
+ NCASectionHeaderBlock header_block;
+ IVFCHeader ivfc;
+ INSERT_PADDING_BYTES(0x18);
+ BKTRHeader relocation;
+ BKTRHeader subsection;
+ INSERT_PADDING_BYTES(0xC0);
+};
+static_assert(sizeof(BKTRSuperblock) == 0x200, "BKTRSuperblock has incorrect size.");
+
union NCASectionHeader {
NCASectionRaw raw;
PFS0Superblock pfs0;
RomFSSuperblock romfs;
+ BKTRSuperblock bktr;
};
static_assert(sizeof(NCASectionHeader) == 0x200, "NCASectionHeader has incorrect size.");
@@ -100,7 +126,7 @@ boost::optional<Core::Crypto::Key128> NCA::GetKeyAreaKey(NCASectionCryptoType ty
Core::Crypto::Key128 out;
if (type == NCASectionCryptoType::XTS)
std::copy(key_area.begin(), key_area.begin() + 0x10, out.begin());
- else if (type == NCASectionCryptoType::CTR)
+ else if (type == NCASectionCryptoType::CTR || type == NCASectionCryptoType::BKTR)
std::copy(key_area.begin() + 0x20, key_area.begin() + 0x30, out.begin());
else
LOG_CRITICAL(Crypto, "Called GetKeyAreaKey on invalid NCASectionCryptoType type={:02X}",
@@ -150,6 +176,9 @@ VirtualFile NCA::Decrypt(NCASectionHeader s_header, VirtualFile in, u64 starting
LOG_DEBUG(Crypto, "called with mode=NONE");
return in;
case NCASectionCryptoType::CTR:
+ // During normal BKTR decryption, this entire function is skipped. This is for the metadata,
+ // which uses the same CTR as usual.
+ case NCASectionCryptoType::BKTR:
LOG_DEBUG(Crypto, "called with mode=CTR, starting_offset={:016X}", starting_offset);
{
boost::optional<Core::Crypto::Key128> key = boost::none;
@@ -186,7 +215,9 @@ VirtualFile NCA::Decrypt(NCASectionHeader s_header, VirtualFile in, u64 starting
}
}
-NCA::NCA(VirtualFile file_) : file(std::move(file_)) {
+NCA::NCA(VirtualFile file_, VirtualFile bktr_base_romfs_, u64 bktr_base_ivfc_offset)
+ : file(std::move(file_)),
+ bktr_base_romfs(bktr_base_romfs_ ? std::move(bktr_base_romfs_) : nullptr) {
status = Loader::ResultStatus::Success;
if (file == nullptr) {
@@ -261,22 +292,21 @@ NCA::NCA(VirtualFile file_) : file(std::move(file_)) {
is_update = std::find_if(sections.begin(), sections.end(), [](const NCASectionHeader& header) {
return header.raw.header.crypto_type == NCASectionCryptoType::BKTR;
}) != sections.end();
+ ivfc_offset = 0;
for (std::ptrdiff_t i = 0; i < number_sections; ++i) {
auto section = sections[i];
if (section.raw.header.filesystem_type == NCASectionFilesystemType::ROMFS) {
- const size_t romfs_offset =
- header.section_tables[i].media_offset * MEDIA_OFFSET_MULTIPLIER +
- section.romfs.ivfc.levels[IVFC_MAX_LEVEL - 1].offset;
- const size_t romfs_size = section.romfs.ivfc.levels[IVFC_MAX_LEVEL - 1].size;
- auto dec =
- Decrypt(section, std::make_shared<OffsetVfsFile>(file, romfs_size, romfs_offset),
- romfs_offset);
- if (dec != nullptr) {
- files.push_back(std::move(dec));
- romfs = files.back();
- } else {
+ const std::size_t base_offset =
+ header.section_tables[i].media_offset * MEDIA_OFFSET_MULTIPLIER;
+ ivfc_offset = section.romfs.ivfc.levels[IVFC_MAX_LEVEL - 1].offset;
+ const std::size_t romfs_offset = base_offset + ivfc_offset;
+ const std::size_t romfs_size = section.romfs.ivfc.levels[IVFC_MAX_LEVEL - 1].size;
+ auto raw = std::make_shared<OffsetVfsFile>(file, romfs_size, romfs_offset);
+ auto dec = Decrypt(section, raw, romfs_offset);
+
+ if (dec == nullptr) {
if (status != Loader::ResultStatus::Success)
return;
if (has_rights_id)
@@ -285,6 +315,117 @@ NCA::NCA(VirtualFile file_) : file(std::move(file_)) {
status = Loader::ResultStatus::ErrorIncorrectKeyAreaKey;
return;
}
+
+ if (section.raw.header.crypto_type == NCASectionCryptoType::BKTR) {
+ if (section.bktr.relocation.magic != Common::MakeMagic('B', 'K', 'T', 'R') ||
+ section.bktr.subsection.magic != Common::MakeMagic('B', 'K', 'T', 'R')) {
+ status = Loader::ResultStatus::ErrorBadBKTRHeader;
+ return;
+ }
+
+ if (section.bktr.relocation.offset + section.bktr.relocation.size !=
+ section.bktr.subsection.offset) {
+ status = Loader::ResultStatus::ErrorBKTRSubsectionNotAfterRelocation;
+ return;
+ }
+
+ const u64 size =
+ MEDIA_OFFSET_MULTIPLIER * (header.section_tables[i].media_end_offset -
+ header.section_tables[i].media_offset);
+ if (section.bktr.subsection.offset + section.bktr.subsection.size != size) {
+ status = Loader::ResultStatus::ErrorBKTRSubsectionNotAtEnd;
+ return;
+ }
+
+ const u64 offset = section.romfs.ivfc.levels[IVFC_MAX_LEVEL - 1].offset;
+ RelocationBlock relocation_block{};
+ if (dec->ReadObject(&relocation_block, section.bktr.relocation.offset - offset) !=
+ sizeof(RelocationBlock)) {
+ status = Loader::ResultStatus::ErrorBadRelocationBlock;
+ return;
+ }
+ SubsectionBlock subsection_block{};
+ if (dec->ReadObject(&subsection_block, section.bktr.subsection.offset - offset) !=
+ sizeof(RelocationBlock)) {
+ status = Loader::ResultStatus::ErrorBadSubsectionBlock;
+ return;
+ }
+
+ std::vector<RelocationBucketRaw> relocation_buckets_raw(
+ (section.bktr.relocation.size - sizeof(RelocationBlock)) /
+ sizeof(RelocationBucketRaw));
+ if (dec->ReadBytes(relocation_buckets_raw.data(),
+ section.bktr.relocation.size - sizeof(RelocationBlock),
+ section.bktr.relocation.offset + sizeof(RelocationBlock) -
+ offset) !=
+ section.bktr.relocation.size - sizeof(RelocationBlock)) {
+ status = Loader::ResultStatus::ErrorBadRelocationBuckets;
+ return;
+ }
+
+ std::vector<SubsectionBucketRaw> subsection_buckets_raw(
+ (section.bktr.subsection.size - sizeof(SubsectionBlock)) /
+ sizeof(SubsectionBucketRaw));
+ if (dec->ReadBytes(subsection_buckets_raw.data(),
+ section.bktr.subsection.size - sizeof(SubsectionBlock),
+ section.bktr.subsection.offset + sizeof(SubsectionBlock) -
+ offset) !=
+ section.bktr.subsection.size - sizeof(SubsectionBlock)) {
+ status = Loader::ResultStatus::ErrorBadSubsectionBuckets;
+ return;
+ }
+
+ std::vector<RelocationBucket> relocation_buckets(relocation_buckets_raw.size());
+ std::transform(relocation_buckets_raw.begin(), relocation_buckets_raw.end(),
+ relocation_buckets.begin(), &ConvertRelocationBucketRaw);
+ std::vector<SubsectionBucket> subsection_buckets(subsection_buckets_raw.size());
+ std::transform(subsection_buckets_raw.begin(), subsection_buckets_raw.end(),
+ subsection_buckets.begin(), &ConvertSubsectionBucketRaw);
+
+ u32 ctr_low;
+ std::memcpy(&ctr_low, section.raw.section_ctr.data(), sizeof(ctr_low));
+ subsection_buckets.back().entries.push_back(
+ {section.bktr.relocation.offset, {0}, ctr_low});
+ subsection_buckets.back().entries.push_back({size, {0}, 0});
+
+ boost::optional<Core::Crypto::Key128> key = boost::none;
+ if (encrypted) {
+ if (has_rights_id) {
+ status = Loader::ResultStatus::Success;
+ key = GetTitlekey();
+ if (key == boost::none) {
+ status = Loader::ResultStatus::ErrorMissingTitlekey;
+ return;
+ }
+ } else {
+ key = GetKeyAreaKey(NCASectionCryptoType::BKTR);
+ if (key == boost::none) {
+ status = Loader::ResultStatus::ErrorMissingKeyAreaKey;
+ return;
+ }
+ }
+ }
+
+ if (bktr_base_romfs == nullptr) {
+ status = Loader::ResultStatus::ErrorMissingBKTRBaseRomFS;
+ return;
+ }
+
+ auto bktr = std::make_shared<BKTR>(
+ bktr_base_romfs, std::make_shared<OffsetVfsFile>(file, romfs_size, base_offset),
+ relocation_block, relocation_buckets, subsection_block, subsection_buckets,
+ encrypted, encrypted ? key.get() : Core::Crypto::Key128{}, base_offset,
+ bktr_base_ivfc_offset, section.raw.section_ctr);
+
+ // BKTR applies to entire IVFC, so make an offset version to level 6
+
+ files.push_back(std::make_shared<OffsetVfsFile>(
+ bktr, romfs_size, section.romfs.ivfc.levels[IVFC_MAX_LEVEL - 1].offset));
+ romfs = files.back();
+ } else {
+ files.push_back(std::move(dec));
+ romfs = files.back();
+ }
} else if (section.raw.header.filesystem_type == NCASectionFilesystemType::PFS0) {
u64 offset = (static_cast<u64>(header.section_tables[i].media_offset) *
MEDIA_OFFSET_MULTIPLIER) +
@@ -300,6 +441,12 @@ NCA::NCA(VirtualFile file_) : file(std::move(file_)) {
dirs.push_back(std::move(npfs));
if (IsDirectoryExeFS(dirs.back()))
exefs = dirs.back();
+ } else {
+ if (has_rights_id)
+ status = Loader::ResultStatus::ErrorIncorrectTitlekeyOrTitlekek;
+ else
+ status = Loader::ResultStatus::ErrorIncorrectKeyAreaKey;
+ return;
}
} else {
if (status != Loader::ResultStatus::Success)
@@ -316,6 +463,8 @@ NCA::NCA(VirtualFile file_) : file(std::move(file_)) {
status = Loader::ResultStatus::Success;
}
+NCA::~NCA() = default;
+
Loader::ResultStatus NCA::GetStatus() const {
return status;
}
@@ -345,11 +494,15 @@ NCAContentType NCA::GetType() const {
}
u64 NCA::GetTitleId() const {
- if (status != Loader::ResultStatus::Success)
- return {};
+ if (is_update || status == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS)
+ return header.title_id | 0x800;
return header.title_id;
}
+bool NCA::IsUpdate() const {
+ return is_update;
+}
+
VirtualFile NCA::GetRomFS() const {
return romfs;
}
@@ -362,8 +515,8 @@ VirtualFile NCA::GetBaseFile() const {
return file;
}
-bool NCA::IsUpdate() const {
- return is_update;
+u64 NCA::GetBaseIVFCOffset() const {
+ return ivfc_offset;
}
bool NCA::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
diff --git a/src/core/file_sys/content_archive.h b/src/core/file_sys/content_archive.h
index b961cfde7..f9f66cae9 100644
--- a/src/core/file_sys/content_archive.h
+++ b/src/core/file_sys/content_archive.h
@@ -12,10 +12,12 @@
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "common/swap.h"
-#include "control_metadata.h"
#include "core/crypto/key_manager.h"
-#include "core/file_sys/partition_filesystem.h"
-#include "core/loader/loader.h"
+#include "core/file_sys/vfs.h"
+
+namespace Loader {
+enum class ResultStatus : u16;
+}
namespace FileSys {
@@ -77,7 +79,10 @@ bool IsValidNCA(const NCAHeader& header);
// After construction, use GetStatus to determine if the file is valid and ready to be used.
class NCA : public ReadOnlyVfsDirectory {
public:
- explicit NCA(VirtualFile file);
+ explicit NCA(VirtualFile file, VirtualFile bktr_base_romfs = nullptr,
+ u64 bktr_base_ivfc_offset = 0);
+ ~NCA() override;
+
Loader::ResultStatus GetStatus() const;
std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
@@ -87,13 +92,15 @@ public:
NCAContentType GetType() const;
u64 GetTitleId() const;
+ bool IsUpdate() const;
VirtualFile GetRomFS() const;
VirtualDir GetExeFS() const;
VirtualFile GetBaseFile() const;
- bool IsUpdate() const;
+ // Returns the base ivfc offset used in BKTR patching.
+ u64 GetBaseIVFCOffset() const;
protected:
bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
@@ -110,14 +117,16 @@ private:
VirtualFile romfs = nullptr;
VirtualDir exefs = nullptr;
VirtualFile file;
+ VirtualFile bktr_base_romfs;
+ u64 ivfc_offset;
NCAHeader header{};
bool has_rights_id{};
- bool is_update{};
Loader::ResultStatus status{};
bool encrypted;
+ bool is_update;
Core::Crypto::KeyManager keys;
};
diff --git a/src/core/file_sys/control_metadata.cpp b/src/core/file_sys/control_metadata.cpp
index ae21ad5b9..5b1177a03 100644
--- a/src/core/file_sys/control_metadata.cpp
+++ b/src/core/file_sys/control_metadata.cpp
@@ -8,6 +8,14 @@
namespace FileSys {
+const std::array<const char*, 15> LANGUAGE_NAMES = {
+ "AmericanEnglish", "BritishEnglish", "Japanese",
+ "French", "German", "LatinAmericanSpanish",
+ "Spanish", "Italian", "Dutch",
+ "CanadianFrench", "Portugese", "Russian",
+ "Korean", "Taiwanese", "Chinese",
+};
+
std::string LanguageEntry::GetApplicationName() const {
return Common::StringFromFixedZeroTerminatedBuffer(application_name.data(), 0x200);
}
@@ -20,8 +28,20 @@ NACP::NACP(VirtualFile file) : raw(std::make_unique<RawNACP>()) {
file->ReadObject(raw.get());
}
+NACP::~NACP() = default;
+
const LanguageEntry& NACP::GetLanguageEntry(Language language) const {
- return raw->language_entries.at(static_cast<u8>(language));
+ if (language != Language::Default) {
+ return raw->language_entries.at(static_cast<u8>(language));
+ }
+
+ for (const auto& language_entry : raw->language_entries) {
+ if (!language_entry.GetApplicationName().empty())
+ return language_entry;
+ }
+
+ // Fallback to English
+ return GetLanguageEntry(Language::AmericanEnglish);
}
std::string NACP::GetApplicationName(Language language) const {
diff --git a/src/core/file_sys/control_metadata.h b/src/core/file_sys/control_metadata.h
index 8c2cc1a2a..43d6f0719 100644
--- a/src/core/file_sys/control_metadata.h
+++ b/src/core/file_sys/control_metadata.h
@@ -8,6 +8,8 @@
#include <memory>
#include <string>
#include "common/common_funcs.h"
+#include "common/common_types.h"
+#include "common/swap.h"
#include "core/file_sys/vfs.h"
namespace FileSys {
@@ -60,23 +62,22 @@ enum class Language : u8 {
Korean = 12,
Taiwanese = 13,
Chinese = 14,
+
+ Default = 255,
};
-static constexpr std::array<const char*, 15> LANGUAGE_NAMES = {
- "AmericanEnglish", "BritishEnglish", "Japanese",
- "French", "German", "LatinAmericanSpanish",
- "Spanish", "Italian", "Dutch",
- "CanadianFrench", "Portugese", "Russian",
- "Korean", "Taiwanese", "Chinese"};
+extern const std::array<const char*, 15> LANGUAGE_NAMES;
// A class representing the format used by NX metadata files, typically named Control.nacp.
// These store application name, dev name, title id, and other miscellaneous data.
class NACP {
public:
explicit NACP(VirtualFile file);
- const LanguageEntry& GetLanguageEntry(Language language = Language::AmericanEnglish) const;
- std::string GetApplicationName(Language language = Language::AmericanEnglish) const;
- std::string GetDeveloperName(Language language = Language::AmericanEnglish) const;
+ ~NACP();
+
+ const LanguageEntry& GetLanguageEntry(Language language = Language::Default) const;
+ std::string GetApplicationName(Language language = Language::Default) const;
+ std::string GetDeveloperName(Language language = Language::Default) const;
u64 GetTitleId() const;
std::string GetVersionString() const;
diff --git a/src/core/file_sys/directory.h b/src/core/file_sys/directory.h
index 3759e743a..12bb90ec8 100644
--- a/src/core/file_sys/directory.h
+++ b/src/core/file_sys/directory.h
@@ -25,7 +25,7 @@ enum EntryType : u8 {
struct Entry {
Entry(std::string_view view, EntryType entry_type, u64 entry_size)
: type{entry_type}, file_size{entry_size} {
- const size_t copy_size = view.copy(filename, std::size(filename) - 1);
+ const std::size_t copy_size = view.copy(filename, std::size(filename) - 1);
filename[copy_size] = '\0';
}
diff --git a/src/core/file_sys/nca_metadata.cpp b/src/core/file_sys/nca_metadata.cpp
index 449244444..6f34b7836 100644
--- a/src/core/file_sys/nca_metadata.cpp
+++ b/src/core/file_sys/nca_metadata.cpp
@@ -3,20 +3,19 @@
// Refer to the license.txt file included.
#include <cstring>
-#include "common/common_funcs.h"
+#include "common/common_types.h"
#include "common/logging/log.h"
#include "common/swap.h"
-#include "content_archive.h"
#include "core/file_sys/nca_metadata.h"
namespace FileSys {
bool operator>=(TitleType lhs, TitleType rhs) {
- return static_cast<size_t>(lhs) >= static_cast<size_t>(rhs);
+ return static_cast<std::size_t>(lhs) >= static_cast<std::size_t>(rhs);
}
bool operator<=(TitleType lhs, TitleType rhs) {
- return static_cast<size_t>(lhs) <= static_cast<size_t>(rhs);
+ return static_cast<std::size_t>(lhs) <= static_cast<std::size_t>(rhs);
}
CNMT::CNMT(VirtualFile file) {
@@ -52,6 +51,8 @@ CNMT::CNMT(CNMTHeader header, OptionalHeader opt_header, std::vector<ContentReco
: header(std::move(header)), opt_header(std::move(opt_header)),
content_records(std::move(content_records)), meta_records(std::move(meta_records)) {}
+CNMT::~CNMT() = default;
+
u64 CNMT::GetTitleID() const {
return header.title_id;
}
diff --git a/src/core/file_sys/nca_metadata.h b/src/core/file_sys/nca_metadata.h
index ce05b4c1d..a05d155f4 100644
--- a/src/core/file_sys/nca_metadata.h
+++ b/src/core/file_sys/nca_metadata.h
@@ -4,7 +4,6 @@
#pragma once
-#include <cstring>
#include <memory>
#include <vector>
#include "common/common_funcs.h"
@@ -88,6 +87,7 @@ public:
explicit CNMT(VirtualFile file);
CNMT(CNMTHeader header, OptionalHeader opt_header, std::vector<ContentRecord> content_records,
std::vector<MetaRecord> meta_records);
+ ~CNMT();
u64 GetTitleID() const;
u32 GetTitleVersion() const;
diff --git a/src/core/file_sys/nca_patch.cpp b/src/core/file_sys/nca_patch.cpp
new file mode 100644
index 000000000..0090cc6c4
--- /dev/null
+++ b/src/core/file_sys/nca_patch.cpp
@@ -0,0 +1,210 @@
+// Copyright 2018 yuzu emulator team
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <algorithm>
+#include <cstddef>
+#include <cstring>
+
+#include "common/assert.h"
+#include "core/crypto/aes_util.h"
+#include "core/file_sys/nca_patch.h"
+
+namespace FileSys {
+
+BKTR::BKTR(VirtualFile base_romfs_, VirtualFile bktr_romfs_, RelocationBlock relocation_,
+ std::vector<RelocationBucket> relocation_buckets_, SubsectionBlock subsection_,
+ std::vector<SubsectionBucket> subsection_buckets_, bool is_encrypted_,
+ Core::Crypto::Key128 key_, u64 base_offset_, u64 ivfc_offset_,
+ std::array<u8, 8> section_ctr_)
+ : relocation(relocation_), relocation_buckets(std::move(relocation_buckets_)),
+ subsection(subsection_), subsection_buckets(std::move(subsection_buckets_)),
+ base_romfs(std::move(base_romfs_)), bktr_romfs(std::move(bktr_romfs_)),
+ encrypted(is_encrypted_), key(key_), base_offset(base_offset_), ivfc_offset(ivfc_offset_),
+ section_ctr(section_ctr_) {
+ for (std::size_t i = 0; i < relocation.number_buckets - 1; ++i) {
+ relocation_buckets[i].entries.push_back({relocation.base_offsets[i + 1], 0, 0});
+ }
+
+ for (std::size_t i = 0; i < subsection.number_buckets - 1; ++i) {
+ subsection_buckets[i].entries.push_back({subsection_buckets[i + 1].entries[0].address_patch,
+ {0},
+ subsection_buckets[i + 1].entries[0].ctr});
+ }
+
+ relocation_buckets.back().entries.push_back({relocation.size, 0, 0});
+}
+
+BKTR::~BKTR() = default;
+
+std::size_t BKTR::Read(u8* data, std::size_t length, std::size_t offset) const {
+ // Read out of bounds.
+ if (offset >= relocation.size)
+ return 0;
+ const auto relocation = GetRelocationEntry(offset);
+ const auto section_offset = offset - relocation.address_patch + relocation.address_source;
+ const auto bktr_read = relocation.from_patch;
+
+ const auto next_relocation = GetNextRelocationEntry(offset);
+
+ if (offset + length > next_relocation.address_patch) {
+ const u64 partition = next_relocation.address_patch - offset;
+ return Read(data, partition, offset) +
+ Read(data + partition, length - partition, offset + partition);
+ }
+
+ if (!bktr_read) {
+ ASSERT_MSG(section_offset >= ivfc_offset, "Offset calculation negative.");
+ return base_romfs->Read(data, length, section_offset - ivfc_offset);
+ }
+
+ if (!encrypted) {
+ return bktr_romfs->Read(data, length, section_offset);
+ }
+
+ const auto subsection = GetSubsectionEntry(section_offset);
+ Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(key, Core::Crypto::Mode::CTR);
+
+ // Calculate AES IV
+ std::vector<u8> iv(16);
+ auto subsection_ctr = subsection.ctr;
+ auto offset_iv = section_offset + base_offset;
+ for (std::size_t i = 0; i < section_ctr.size(); ++i)
+ iv[i] = section_ctr[0x8 - i - 1];
+ offset_iv >>= 4;
+ for (std::size_t i = 0; i < sizeof(u64); ++i) {
+ iv[0xF - i] = static_cast<u8>(offset_iv & 0xFF);
+ offset_iv >>= 8;
+ }
+ for (std::size_t i = 0; i < sizeof(u32); ++i) {
+ iv[0x7 - i] = static_cast<u8>(subsection_ctr & 0xFF);
+ subsection_ctr >>= 8;
+ }
+ cipher.SetIV(iv);
+
+ const auto next_subsection = GetNextSubsectionEntry(section_offset);
+
+ if (section_offset + length > next_subsection.address_patch) {
+ const u64 partition = next_subsection.address_patch - section_offset;
+ return Read(data, partition, offset) +
+ Read(data + partition, length - partition, offset + partition);
+ }
+
+ const auto block_offset = section_offset & 0xF;
+ if (block_offset != 0) {
+ auto block = bktr_romfs->ReadBytes(0x10, section_offset & ~0xF);
+ cipher.Transcode(block.data(), block.size(), block.data(), Core::Crypto::Op::Decrypt);
+ if (length + block_offset < 0x10) {
+ std::memcpy(data, block.data() + block_offset, std::min(length, block.size()));
+ return std::min(length, block.size());
+ }
+
+ const auto read = 0x10 - block_offset;
+ std::memcpy(data, block.data() + block_offset, read);
+ return read + Read(data + read, length - read, offset + read);
+ }
+
+ const auto raw_read = bktr_romfs->Read(data, length, section_offset);
+ cipher.Transcode(data, raw_read, data, Core::Crypto::Op::Decrypt);
+ return raw_read;
+}
+
+template <bool Subsection, typename BlockType, typename BucketType>
+std::pair<std::size_t, std::size_t> BKTR::SearchBucketEntry(u64 offset, BlockType block,
+ BucketType buckets) const {
+ if constexpr (Subsection) {
+ const auto last_bucket = buckets[block.number_buckets - 1];
+ if (offset >= last_bucket.entries[last_bucket.number_entries].address_patch)
+ return {block.number_buckets - 1, last_bucket.number_entries};
+ } else {
+ ASSERT_MSG(offset <= block.size, "Offset is out of bounds in BKTR relocation block.");
+ }
+
+ std::size_t bucket_id = std::count_if(
+ block.base_offsets.begin() + 1, block.base_offsets.begin() + block.number_buckets,
+ [&offset](u64 base_offset) { return base_offset <= offset; });
+
+ const auto bucket = buckets[bucket_id];
+
+ if (bucket.number_entries == 1)
+ return {bucket_id, 0};
+
+ std::size_t low = 0;
+ std::size_t mid = 0;
+ std::size_t high = bucket.number_entries - 1;
+ while (low <= high) {
+ mid = (low + high) / 2;
+ if (bucket.entries[mid].address_patch > offset) {
+ high = mid - 1;
+ } else {
+ if (mid == bucket.number_entries - 1 ||
+ bucket.entries[mid + 1].address_patch > offset) {
+ return {bucket_id, mid};
+ }
+
+ low = mid + 1;
+ }
+ }
+
+ UNREACHABLE_MSG("Offset could not be found in BKTR block.");
+}
+
+RelocationEntry BKTR::GetRelocationEntry(u64 offset) const {
+ const auto res = SearchBucketEntry<false>(offset, relocation, relocation_buckets);
+ return relocation_buckets[res.first].entries[res.second];
+}
+
+RelocationEntry BKTR::GetNextRelocationEntry(u64 offset) const {
+ const auto res = SearchBucketEntry<false>(offset, relocation, relocation_buckets);
+ const auto bucket = relocation_buckets[res.first];
+ if (res.second + 1 < bucket.entries.size())
+ return bucket.entries[res.second + 1];
+ return relocation_buckets[res.first + 1].entries[0];
+}
+
+SubsectionEntry BKTR::GetSubsectionEntry(u64 offset) const {
+ const auto res = SearchBucketEntry<true>(offset, subsection, subsection_buckets);
+ return subsection_buckets[res.first].entries[res.second];
+}
+
+SubsectionEntry BKTR::GetNextSubsectionEntry(u64 offset) const {
+ const auto res = SearchBucketEntry<true>(offset, subsection, subsection_buckets);
+ const auto bucket = subsection_buckets[res.first];
+ if (res.second + 1 < bucket.entries.size())
+ return bucket.entries[res.second + 1];
+ return subsection_buckets[res.first + 1].entries[0];
+}
+
+std::string BKTR::GetName() const {
+ return base_romfs->GetName();
+}
+
+std::size_t BKTR::GetSize() const {
+ return relocation.size;
+}
+
+bool BKTR::Resize(std::size_t new_size) {
+ return false;
+}
+
+std::shared_ptr<VfsDirectory> BKTR::GetContainingDirectory() const {
+ return base_romfs->GetContainingDirectory();
+}
+
+bool BKTR::IsWritable() const {
+ return false;
+}
+
+bool BKTR::IsReadable() const {
+ return true;
+}
+
+std::size_t BKTR::Write(const u8* data, std::size_t length, std::size_t offset) {
+ return 0;
+}
+
+bool BKTR::Rename(std::string_view name) {
+ return base_romfs->Rename(name);
+}
+
+} // namespace FileSys
diff --git a/src/core/file_sys/nca_patch.h b/src/core/file_sys/nca_patch.h
new file mode 100644
index 000000000..8e64e8378
--- /dev/null
+++ b/src/core/file_sys/nca_patch.h
@@ -0,0 +1,150 @@
+// Copyright 2018 yuzu emulator team
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <array>
+#include <memory>
+#include <vector>
+
+#include "common/common_funcs.h"
+#include "common/common_types.h"
+#include "common/swap.h"
+#include "core/crypto/key_manager.h"
+
+namespace FileSys {
+
+#pragma pack(push, 1)
+struct RelocationEntry {
+ u64_le address_patch;
+ u64_le address_source;
+ u32 from_patch;
+};
+#pragma pack(pop)
+static_assert(sizeof(RelocationEntry) == 0x14, "RelocationEntry has incorrect size.");
+
+struct RelocationBucketRaw {
+ INSERT_PADDING_BYTES(4);
+ u32_le number_entries;
+ u64_le end_offset;
+ std::array<RelocationEntry, 0x332> relocation_entries;
+ INSERT_PADDING_BYTES(8);
+};
+static_assert(sizeof(RelocationBucketRaw) == 0x4000, "RelocationBucketRaw has incorrect size.");
+
+// Vector version of RelocationBucketRaw
+struct RelocationBucket {
+ u32 number_entries;
+ u64 end_offset;
+ std::vector<RelocationEntry> entries;
+};
+
+struct RelocationBlock {
+ INSERT_PADDING_BYTES(4);
+ u32_le number_buckets;
+ u64_le size;
+ std::array<u64, 0x7FE> base_offsets;
+};
+static_assert(sizeof(RelocationBlock) == 0x4000, "RelocationBlock has incorrect size.");
+
+struct SubsectionEntry {
+ u64_le address_patch;
+ INSERT_PADDING_BYTES(0x4);
+ u32_le ctr;
+};
+static_assert(sizeof(SubsectionEntry) == 0x10, "SubsectionEntry has incorrect size.");
+
+struct SubsectionBucketRaw {
+ INSERT_PADDING_BYTES(4);
+ u32_le number_entries;
+ u64_le end_offset;
+ std::array<SubsectionEntry, 0x3FF> subsection_entries;
+};
+static_assert(sizeof(SubsectionBucketRaw) == 0x4000, "SubsectionBucketRaw has incorrect size.");
+
+// Vector version of SubsectionBucketRaw
+struct SubsectionBucket {
+ u32 number_entries;
+ u64 end_offset;
+ std::vector<SubsectionEntry> entries;
+};
+
+struct SubsectionBlock {
+ INSERT_PADDING_BYTES(4);
+ u32_le number_buckets;
+ u64_le size;
+ std::array<u64, 0x7FE> base_offsets;
+};
+static_assert(sizeof(SubsectionBlock) == 0x4000, "SubsectionBlock has incorrect size.");
+
+inline RelocationBucket ConvertRelocationBucketRaw(RelocationBucketRaw raw) {
+ return {raw.number_entries,
+ raw.end_offset,
+ {raw.relocation_entries.begin(), raw.relocation_entries.begin() + raw.number_entries}};
+}
+
+inline SubsectionBucket ConvertSubsectionBucketRaw(SubsectionBucketRaw raw) {
+ return {raw.number_entries,
+ raw.end_offset,
+ {raw.subsection_entries.begin(), raw.subsection_entries.begin() + raw.number_entries}};
+}
+
+class BKTR : public VfsFile {
+public:
+ BKTR(VirtualFile base_romfs, VirtualFile bktr_romfs, RelocationBlock relocation,
+ std::vector<RelocationBucket> relocation_buckets, SubsectionBlock subsection,
+ std::vector<SubsectionBucket> subsection_buckets, bool is_encrypted,
+ Core::Crypto::Key128 key, u64 base_offset, u64 ivfc_offset, std::array<u8, 8> section_ctr);
+ ~BKTR() override;
+
+ std::size_t Read(u8* data, std::size_t length, std::size_t offset) const override;
+
+ std::string GetName() const override;
+
+ std::size_t GetSize() const override;
+
+ bool Resize(std::size_t new_size) override;
+
+ std::shared_ptr<VfsDirectory> GetContainingDirectory() const override;
+
+ bool IsWritable() const override;
+
+ bool IsReadable() const override;
+
+ std::size_t Write(const u8* data, std::size_t length, std::size_t offset) override;
+
+ bool Rename(std::string_view name) override;
+
+private:
+ template <bool Subsection, typename BlockType, typename BucketType>
+ std::pair<std::size_t, std::size_t> SearchBucketEntry(u64 offset, BlockType block,
+ BucketType buckets) const;
+
+ RelocationEntry GetRelocationEntry(u64 offset) const;
+ RelocationEntry GetNextRelocationEntry(u64 offset) const;
+
+ SubsectionEntry GetSubsectionEntry(u64 offset) const;
+ SubsectionEntry GetNextSubsectionEntry(u64 offset) const;
+
+ RelocationBlock relocation;
+ std::vector<RelocationBucket> relocation_buckets;
+ SubsectionBlock subsection;
+ std::vector<SubsectionBucket> subsection_buckets;
+
+ // Should be the raw base romfs, decrypted.
+ VirtualFile base_romfs;
+ // Should be the raw BKTR romfs, (located at media_offset with size media_size).
+ VirtualFile bktr_romfs;
+
+ bool encrypted;
+ Core::Crypto::Key128 key;
+
+ // Base offset into NCA, used for IV calculation.
+ u64 base_offset;
+ // Distance between IVFC start and RomFS start, used for base reads
+ u64 ivfc_offset;
+ std::array<u8, 8> section_ctr;
+};
+
+} // namespace FileSys
diff --git a/src/core/file_sys/partition_filesystem.cpp b/src/core/file_sys/partition_filesystem.cpp
index c377edc9c..5791c76ff 100644
--- a/src/core/file_sys/partition_filesystem.cpp
+++ b/src/core/file_sys/partition_filesystem.cpp
@@ -42,21 +42,21 @@ PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) {
is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0');
- size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry);
- size_t metadata_size =
+ std::size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry);
+ std::size_t metadata_size =
sizeof(Header) + (pfs_header.num_entries * entry_size) + pfs_header.strtab_size;
// Actually read in now...
std::vector<u8> file_data = file->ReadBytes(metadata_size);
- const size_t total_size = file_data.size();
+ const std::size_t total_size = file_data.size();
if (total_size != metadata_size) {
status = Loader::ResultStatus::ErrorIncorrectPFSFileSize;
return;
}
- size_t entries_offset = sizeof(Header);
- size_t strtab_offset = entries_offset + (pfs_header.num_entries * entry_size);
+ std::size_t entries_offset = sizeof(Header);
+ std::size_t strtab_offset = entries_offset + (pfs_header.num_entries * entry_size);
content_offset = strtab_offset + pfs_header.strtab_size;
for (u16 i = 0; i < pfs_header.num_entries; i++) {
FSEntry entry;
@@ -72,6 +72,8 @@ PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) {
status = Loader::ResultStatus::Success;
}
+PartitionFilesystem::~PartitionFilesystem() = default;
+
Loader::ResultStatus PartitionFilesystem::GetStatus() const {
return status;
}
diff --git a/src/core/file_sys/partition_filesystem.h b/src/core/file_sys/partition_filesystem.h
index be7bc32a8..739c63a7f 100644
--- a/src/core/file_sys/partition_filesystem.h
+++ b/src/core/file_sys/partition_filesystem.h
@@ -25,6 +25,8 @@ namespace FileSys {
class PartitionFilesystem : public ReadOnlyVfsDirectory {
public:
explicit PartitionFilesystem(std::shared_ptr<VfsFile> file);
+ ~PartitionFilesystem() override;
+
Loader::ResultStatus GetStatus() const;
std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
@@ -79,7 +81,7 @@ private:
Header pfs_header{};
bool is_hfs = false;
- size_t content_offset = 0;
+ std::size_t content_offset = 0;
std::vector<VirtualFile> pfs_files;
std::vector<VirtualDir> pfs_dirs;
diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp
new file mode 100644
index 000000000..aebc69d52
--- /dev/null
+++ b/src/core/file_sys/patch_manager.cpp
@@ -0,0 +1,159 @@
+// Copyright 2018 yuzu emulator team
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <array>
+#include <cstddef>
+
+#include "common/logging/log.h"
+#include "core/file_sys/content_archive.h"
+#include "core/file_sys/control_metadata.h"
+#include "core/file_sys/patch_manager.h"
+#include "core/file_sys/registered_cache.h"
+#include "core/file_sys/romfs.h"
+#include "core/hle/service/filesystem/filesystem.h"
+#include "core/loader/loader.h"
+
+namespace FileSys {
+
+constexpr u64 SINGLE_BYTE_MODULUS = 0x100;
+
+std::string FormatTitleVersion(u32 version, TitleVersionFormat format) {
+ std::array<u8, sizeof(u32)> bytes{};
+ bytes[0] = version % SINGLE_BYTE_MODULUS;
+ for (std::size_t i = 1; i < bytes.size(); ++i) {
+ version /= SINGLE_BYTE_MODULUS;
+ bytes[i] = version % SINGLE_BYTE_MODULUS;
+ }
+
+ if (format == TitleVersionFormat::FourElements)
+ return fmt::format("v{}.{}.{}.{}", bytes[3], bytes[2], bytes[1], bytes[0]);
+ return fmt::format("v{}.{}.{}", bytes[3], bytes[2], bytes[1]);
+}
+
+constexpr std::array<const char*, 1> PATCH_TYPE_NAMES{
+ "Update",
+};
+
+std::string FormatPatchTypeName(PatchType type) {
+ return PATCH_TYPE_NAMES.at(static_cast<std::size_t>(type));
+}
+
+PatchManager::PatchManager(u64 title_id) : title_id(title_id) {}
+
+PatchManager::~PatchManager() = default;
+
+VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const {
+ LOG_INFO(Loader, "Patching ExeFS for title_id={:016X}", title_id);
+
+ if (exefs == nullptr)
+ return exefs;
+
+ const auto installed = Service::FileSystem::GetUnionContents();
+
+ // Game Updates
+ const auto update_tid = GetUpdateTitleID(title_id);
+ const auto update = installed->GetEntry(update_tid, ContentRecordType::Program);
+ if (update != nullptr) {
+ if (update->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS &&
+ update->GetExeFS() != nullptr) {
+ LOG_INFO(Loader, " ExeFS: Update ({}) applied successfully",
+ FormatTitleVersion(installed->GetEntryVersion(update_tid).get_value_or(0)));
+ exefs = update->GetExeFS();
+ }
+ }
+
+ return exefs;
+}
+
+VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset,
+ ContentRecordType type) const {
+ LOG_INFO(Loader, "Patching RomFS for title_id={:016X}, type={:02X}", title_id,
+ static_cast<u8>(type));
+
+ if (romfs == nullptr)
+ return romfs;
+
+ const auto installed = Service::FileSystem::GetUnionContents();
+
+ // Game Updates
+ const auto update_tid = GetUpdateTitleID(title_id);
+ const auto update = installed->GetEntryRaw(update_tid, type);
+ if (update != nullptr) {
+ const auto new_nca = std::make_shared<NCA>(update, romfs, ivfc_offset);
+ if (new_nca->GetStatus() == Loader::ResultStatus::Success &&
+ new_nca->GetRomFS() != nullptr) {
+ LOG_INFO(Loader, " RomFS: Update ({}) applied successfully",
+ FormatTitleVersion(installed->GetEntryVersion(update_tid).get_value_or(0)));
+ romfs = new_nca->GetRomFS();
+ }
+ }
+
+ return romfs;
+}
+
+std::map<PatchType, std::string> PatchManager::GetPatchVersionNames() const {
+ std::map<PatchType, std::string> out;
+ const auto installed = Service::FileSystem::GetUnionContents();
+
+ const auto update_tid = GetUpdateTitleID(title_id);
+ PatchManager update{update_tid};
+ auto [nacp, discard_icon_file] = update.GetControlMetadata();
+
+ if (nacp != nullptr) {
+ out[PatchType::Update] = nacp->GetVersionString();
+ } else {
+ if (installed->HasEntry(update_tid, ContentRecordType::Program)) {
+ const auto meta_ver = installed->GetEntryVersion(update_tid);
+ if (meta_ver == boost::none || meta_ver.get() == 0) {
+ out[PatchType::Update] = "";
+ } else {
+ out[PatchType::Update] =
+ FormatTitleVersion(meta_ver.get(), TitleVersionFormat::ThreeElements);
+ }
+ }
+ }
+
+ return out;
+}
+
+std::pair<std::shared_ptr<NACP>, VirtualFile> PatchManager::GetControlMetadata() const {
+ const auto& installed{Service::FileSystem::GetUnionContents()};
+
+ const auto base_control_nca = installed->GetEntry(title_id, ContentRecordType::Control);
+ if (base_control_nca == nullptr)
+ return {};
+
+ return ParseControlNCA(base_control_nca);
+}
+
+std::pair<std::shared_ptr<NACP>, VirtualFile> PatchManager::ParseControlNCA(
+ const std::shared_ptr<NCA>& nca) const {
+ const auto base_romfs = nca->GetRomFS();
+ if (base_romfs == nullptr)
+ return {};
+
+ const auto romfs = PatchRomFS(base_romfs, nca->GetBaseIVFCOffset(), ContentRecordType::Control);
+ if (romfs == nullptr)
+ return {};
+
+ const auto extracted = ExtractRomFS(romfs);
+ if (extracted == nullptr)
+ return {};
+
+ auto nacp_file = extracted->GetFile("control.nacp");
+ if (nacp_file == nullptr)
+ nacp_file = extracted->GetFile("Control.nacp");
+
+ const auto nacp = nacp_file == nullptr ? nullptr : std::make_shared<NACP>(nacp_file);
+
+ VirtualFile icon_file;
+ for (const auto& language : FileSys::LANGUAGE_NAMES) {
+ icon_file = extracted->GetFile("icon_" + std::string(language) + ".dat");
+ if (icon_file != nullptr)
+ break;
+ }
+
+ return {nacp, icon_file};
+}
+} // namespace FileSys
diff --git a/src/core/file_sys/patch_manager.h b/src/core/file_sys/patch_manager.h
new file mode 100644
index 000000000..209cab1dc
--- /dev/null
+++ b/src/core/file_sys/patch_manager.h
@@ -0,0 +1,64 @@
+// Copyright 2018 yuzu emulator team
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <map>
+#include <memory>
+#include <string>
+#include "common/common_types.h"
+#include "core/file_sys/nca_metadata.h"
+#include "core/file_sys/vfs.h"
+
+namespace FileSys {
+
+class NCA;
+class NACP;
+
+enum class TitleVersionFormat : u8 {
+ ThreeElements, ///< vX.Y.Z
+ FourElements, ///< vX.Y.Z.W
+};
+
+std::string FormatTitleVersion(u32 version,
+ TitleVersionFormat format = TitleVersionFormat::ThreeElements);
+
+enum class PatchType {
+ Update,
+};
+
+std::string FormatPatchTypeName(PatchType type);
+
+// A centralized class to manage patches to games.
+class PatchManager {
+public:
+ explicit PatchManager(u64 title_id);
+ ~PatchManager();
+
+ // Currently tracked ExeFS patches:
+ // - Game Updates
+ VirtualDir PatchExeFS(VirtualDir exefs) const;
+
+ // Currently tracked RomFS patches:
+ // - Game Updates
+ VirtualFile PatchRomFS(VirtualFile base, u64 ivfc_offset,
+ ContentRecordType type = ContentRecordType::Program) const;
+
+ // Returns a vector of pairs between patch names and patch versions.
+ // i.e. Update v80 will return {Update, 80}
+ std::map<PatchType, std::string> GetPatchVersionNames() const;
+
+ // Given title_id of the program, attempts to get the control data of the update and parse it,
+ // falling back to the base control data.
+ std::pair<std::shared_ptr<NACP>, VirtualFile> GetControlMetadata() const;
+
+ // Version of GetControlMetadata that takes an arbitrary NCA
+ std::pair<std::shared_ptr<NACP>, VirtualFile> ParseControlNCA(
+ const std::shared_ptr<NCA>& nca) const;
+
+private:
+ u64 title_id;
+};
+
+} // namespace FileSys
diff --git a/src/core/file_sys/program_metadata.cpp b/src/core/file_sys/program_metadata.cpp
index 279f987d4..02319ce0f 100644
--- a/src/core/file_sys/program_metadata.cpp
+++ b/src/core/file_sys/program_metadata.cpp
@@ -2,15 +2,22 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
-#include "common/file_util.h"
+#include <cstddef>
+#include <cstring>
+#include <vector>
+
#include "common/logging/log.h"
#include "core/file_sys/program_metadata.h"
#include "core/loader/loader.h"
namespace FileSys {
+ProgramMetadata::ProgramMetadata() = default;
+
+ProgramMetadata::~ProgramMetadata() = default;
+
Loader::ResultStatus ProgramMetadata::Load(VirtualFile file) {
- size_t total_size = static_cast<size_t>(file->GetSize());
+ std::size_t total_size = static_cast<std::size_t>(file->GetSize());
if (total_size < sizeof(Header))
return Loader::ResultStatus::ErrorBadNPDMHeader;
diff --git a/src/core/file_sys/program_metadata.h b/src/core/file_sys/program_metadata.h
index 74a91052b..1143e36c4 100644
--- a/src/core/file_sys/program_metadata.h
+++ b/src/core/file_sys/program_metadata.h
@@ -5,12 +5,10 @@
#pragma once
#include <array>
-#include <string>
-#include <vector>
#include "common/bit_field.h"
#include "common/common_types.h"
#include "common/swap.h"
-#include "partition_filesystem.h"
+#include "core/file_sys/vfs.h"
namespace Loader {
enum class ResultStatus : u16;
@@ -38,6 +36,9 @@ enum class ProgramFilePermission : u64 {
*/
class ProgramMetadata {
public:
+ ProgramMetadata();
+ ~ProgramMetadata();
+
Loader::ResultStatus Load(VirtualFile file);
bool Is64BitProgram() const;
diff --git a/src/core/file_sys/registered_cache.cpp b/src/core/file_sys/registered_cache.cpp
index dacf8568b..dad7ae10b 100644
--- a/src/core/file_sys/registered_cache.cpp
+++ b/src/core/file_sys/registered_cache.cpp
@@ -5,13 +5,17 @@
#include <regex>
#include <mbedtls/sha256.h>
#include "common/assert.h"
+#include "common/file_util.h"
#include "common/hex_util.h"
#include "common/logging/log.h"
-#include "core/crypto/encryption_layer.h"
+#include "core/crypto/key_manager.h"
#include "core/file_sys/card_image.h"
+#include "core/file_sys/content_archive.h"
#include "core/file_sys/nca_metadata.h"
#include "core/file_sys/registered_cache.h"
+#include "core/file_sys/submission_package.h"
#include "core/file_sys/vfs_concat.h"
+#include "core/loader/loader.h"
namespace FileSys {
std::string RegisteredCacheEntry::DebugInfo() const {
@@ -58,11 +62,11 @@ static std::string GetCNMTName(TitleType type, u64 title_id) {
"" ///< Currently unknown 'DeltaTitle'
};
- auto index = static_cast<size_t>(type);
+ auto index = static_cast<std::size_t>(type);
// If the index is after the jump in TitleType, subtract it out.
- if (index >= static_cast<size_t>(TitleType::Application)) {
- index -= static_cast<size_t>(TitleType::Application) -
- static_cast<size_t>(TitleType::FirmwarePackageB);
+ if (index >= static_cast<std::size_t>(TitleType::Application)) {
+ index -= static_cast<std::size_t>(TitleType::Application) -
+ static_cast<std::size_t>(TitleType::FirmwarePackageB);
}
return fmt::format("{}_{:016x}.cnmt", TITLE_TYPE_NAMES[index], title_id);
}
@@ -101,7 +105,7 @@ VirtualFile RegisteredCache::OpenFileOrDirectoryConcat(const VirtualDir& dir,
} else {
std::vector<VirtualFile> concat;
// Since the files are a two-digit hex number, max is FF.
- for (size_t i = 0; i < 0x100; ++i) {
+ for (std::size_t i = 0; i < 0x100; ++i) {
auto next = nca_dir->GetFile(fmt::format("{:02X}", i));
if (next != nullptr) {
concat.push_back(std::move(next));
@@ -276,6 +280,18 @@ VirtualFile RegisteredCache::GetEntryUnparsed(RegisteredCacheEntry entry) const
return GetEntryUnparsed(entry.title_id, entry.type);
}
+boost::optional<u32> RegisteredCache::GetEntryVersion(u64 title_id) const {
+ const auto meta_iter = meta.find(title_id);
+ if (meta_iter != meta.end())
+ return meta_iter->second.GetTitleVersion();
+
+ const auto yuzu_meta_iter = yuzu_meta.find(title_id);
+ if (yuzu_meta_iter != yuzu_meta.end())
+ return yuzu_meta_iter->second.GetTitleVersion();
+
+ return boost::none;
+}
+
VirtualFile RegisteredCache::GetEntryRaw(u64 title_id, ContentRecordType type) const {
const auto id = GetNcaIDFromMetadata(title_id, type);
if (id == boost::none)
@@ -355,17 +371,21 @@ std::vector<RegisteredCacheEntry> RegisteredCache::ListEntriesFilter(
return out;
}
-static std::shared_ptr<NCA> GetNCAFromXCIForID(std::shared_ptr<XCI> xci, const NcaID& id) {
- const auto filename = fmt::format("{}.nca", Common::HexArrayToString(id, false));
- const auto iter =
- std::find_if(xci->GetNCAs().begin(), xci->GetNCAs().end(),
- [&filename](std::shared_ptr<NCA> nca) { return nca->GetName() == filename; });
- return iter == xci->GetNCAs().end() ? nullptr : *iter;
+static std::shared_ptr<NCA> GetNCAFromNSPForID(std::shared_ptr<NSP> nsp, const NcaID& id) {
+ const auto file = nsp->GetFile(fmt::format("{}.nca", Common::HexArrayToString(id, false)));
+ if (file == nullptr)
+ return nullptr;
+ return std::make_shared<NCA>(file);
}
InstallResult RegisteredCache::InstallEntry(std::shared_ptr<XCI> xci, bool overwrite_if_exists,
const VfsCopyFunction& copy) {
- const auto& ncas = xci->GetNCAs();
+ return InstallEntry(xci->GetSecurePartitionNSP(), overwrite_if_exists, copy);
+}
+
+InstallResult RegisteredCache::InstallEntry(std::shared_ptr<NSP> nsp, bool overwrite_if_exists,
+ const VfsCopyFunction& copy) {
+ const auto& ncas = nsp->GetNCAsCollapsed();
const auto& meta_iter = std::find_if(ncas.begin(), ncas.end(), [](std::shared_ptr<NCA> nca) {
return nca->GetType() == NCAContentType::Meta;
});
@@ -389,7 +409,7 @@ InstallResult RegisteredCache::InstallEntry(std::shared_ptr<XCI> xci, bool overw
const auto cnmt_file = section0->GetFiles()[0];
const CNMT cnmt(cnmt_file);
for (const auto& record : cnmt.GetContentRecords()) {
- const auto nca = GetNCAFromXCIForID(xci, record.nca_id);
+ const auto nca = GetNCAFromNSPForID(nsp, record.nca_id);
if (nca == nullptr)
return InstallResult::ErrorCopyFailed;
const auto res2 = RawInstallNCA(nca, copy, overwrite_if_exists, record.nca_id);
@@ -490,4 +510,107 @@ bool RegisteredCache::RawInstallYuzuMeta(const CNMT& cnmt) {
kv.second.GetTitleID() == cnmt.GetTitleID();
}) != yuzu_meta.end();
}
+
+RegisteredCacheUnion::RegisteredCacheUnion(std::vector<std::shared_ptr<RegisteredCache>> caches)
+ : caches(std::move(caches)) {}
+
+void RegisteredCacheUnion::Refresh() {
+ for (const auto& c : caches)
+ c->Refresh();
+}
+
+bool RegisteredCacheUnion::HasEntry(u64 title_id, ContentRecordType type) const {
+ return std::any_of(caches.begin(), caches.end(), [title_id, type](const auto& cache) {
+ return cache->HasEntry(title_id, type);
+ });
+}
+
+bool RegisteredCacheUnion::HasEntry(RegisteredCacheEntry entry) const {
+ return HasEntry(entry.title_id, entry.type);
+}
+
+boost::optional<u32> RegisteredCacheUnion::GetEntryVersion(u64 title_id) const {
+ for (const auto& c : caches) {
+ const auto res = c->GetEntryVersion(title_id);
+ if (res != boost::none)
+ return res;
+ }
+
+ return boost::none;
+}
+
+VirtualFile RegisteredCacheUnion::GetEntryUnparsed(u64 title_id, ContentRecordType type) const {
+ for (const auto& c : caches) {
+ const auto res = c->GetEntryUnparsed(title_id, type);
+ if (res != nullptr)
+ return res;
+ }
+
+ return nullptr;
+}
+
+VirtualFile RegisteredCacheUnion::GetEntryUnparsed(RegisteredCacheEntry entry) const {
+ return GetEntryUnparsed(entry.title_id, entry.type);
+}
+
+VirtualFile RegisteredCacheUnion::GetEntryRaw(u64 title_id, ContentRecordType type) const {
+ for (const auto& c : caches) {
+ const auto res = c->GetEntryRaw(title_id, type);
+ if (res != nullptr)
+ return res;
+ }
+
+ return nullptr;
+}
+
+VirtualFile RegisteredCacheUnion::GetEntryRaw(RegisteredCacheEntry entry) const {
+ return GetEntryRaw(entry.title_id, entry.type);
+}
+
+std::shared_ptr<NCA> RegisteredCacheUnion::GetEntry(u64 title_id, ContentRecordType type) const {
+ const auto raw = GetEntryRaw(title_id, type);
+ if (raw == nullptr)
+ return nullptr;
+ return std::make_shared<NCA>(raw);
+}
+
+std::shared_ptr<NCA> RegisteredCacheUnion::GetEntry(RegisteredCacheEntry entry) const {
+ return GetEntry(entry.title_id, entry.type);
+}
+
+std::vector<RegisteredCacheEntry> RegisteredCacheUnion::ListEntries() const {
+ std::vector<RegisteredCacheEntry> out;
+ for (const auto& c : caches) {
+ c->IterateAllMetadata<RegisteredCacheEntry>(
+ out,
+ [](const CNMT& c, const ContentRecord& r) {
+ return RegisteredCacheEntry{c.GetTitleID(), r.type};
+ },
+ [](const CNMT& c, const ContentRecord& r) { return true; });
+ }
+ return out;
+}
+
+std::vector<RegisteredCacheEntry> RegisteredCacheUnion::ListEntriesFilter(
+ boost::optional<TitleType> title_type, boost::optional<ContentRecordType> record_type,
+ boost::optional<u64> title_id) const {
+ std::vector<RegisteredCacheEntry> out;
+ for (const auto& c : caches) {
+ c->IterateAllMetadata<RegisteredCacheEntry>(
+ out,
+ [](const CNMT& c, const ContentRecord& r) {
+ return RegisteredCacheEntry{c.GetTitleID(), r.type};
+ },
+ [&title_type, &record_type, &title_id](const CNMT& c, const ContentRecord& r) {
+ if (title_type != boost::none && title_type.get() != c.GetType())
+ return false;
+ if (record_type != boost::none && record_type.get() != r.type)
+ return false;
+ if (title_id != boost::none && title_id.get() != c.GetTitleID())
+ return false;
+ return true;
+ });
+ }
+ return out;
+}
} // namespace FileSys
diff --git a/src/core/file_sys/registered_cache.h b/src/core/file_sys/registered_cache.h
index 7b8955dfa..f487b0cf0 100644
--- a/src/core/file_sys/registered_cache.h
+++ b/src/core/file_sys/registered_cache.h
@@ -11,15 +11,19 @@
#include <string>
#include <vector>
#include <boost/container/flat_map.hpp>
-#include "common/common_funcs.h"
#include "common/common_types.h"
-#include "content_archive.h"
-#include "core/file_sys/nca_metadata.h"
#include "core/file_sys/vfs.h"
namespace FileSys {
-class XCI;
class CNMT;
+class NCA;
+class NSP;
+class XCI;
+
+enum class ContentRecordType : u8;
+enum class TitleType : u8;
+
+struct ContentRecord;
using NcaID = std::array<u8, 0x10>;
using RegisteredCacheParsingFunction = std::function<VirtualFile(const VirtualFile&, const NcaID&)>;
@@ -39,6 +43,10 @@ struct RegisteredCacheEntry {
std::string DebugInfo() const;
};
+constexpr u64 GetUpdateTitleID(u64 base_title_id) {
+ return base_title_id | 0x800;
+}
+
// boost flat_map requires operator< for O(log(n)) lookups.
bool operator<(const RegisteredCacheEntry& lhs, const RegisteredCacheEntry& rhs);
@@ -56,6 +64,8 @@ bool operator<(const RegisteredCacheEntry& lhs, const RegisteredCacheEntry& rhs)
* 4GB splitting can be ignored.)
*/
class RegisteredCache {
+ friend class RegisteredCacheUnion;
+
public:
// Parsing function defines the conversion from raw file to NCA. If there are other steps
// besides creating the NCA from the file (e.g. NAX0 on SD Card), that should go in a custom
@@ -70,6 +80,8 @@ public:
bool HasEntry(u64 title_id, ContentRecordType type) const;
bool HasEntry(RegisteredCacheEntry entry) const;
+ boost::optional<u32> GetEntryVersion(u64 title_id) const;
+
VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const;
VirtualFile GetEntryUnparsed(RegisteredCacheEntry entry) const;
@@ -86,10 +98,12 @@ public:
boost::optional<ContentRecordType> record_type = boost::none,
boost::optional<u64> title_id = boost::none) const;
- // Raw copies all the ncas from the xci to the csache. Does some quick checks to make sure there
- // is a meta NCA and all of them are accessible.
+ // Raw copies all the ncas from the xci/nsp to the csache. Does some quick checks to make sure
+ // there is a meta NCA and all of them are accessible.
InstallResult InstallEntry(std::shared_ptr<XCI> xci, bool overwrite_if_exists = false,
const VfsCopyFunction& copy = &VfsRawCopy);
+ InstallResult InstallEntry(std::shared_ptr<NSP> nsp, bool overwrite_if_exists = false,
+ const VfsCopyFunction& copy = &VfsRawCopy);
// Due to the fact that we must use Meta-type NCAs to determine the existance of files, this
// poses quite a challenge. Instead of creating a new meta NCA for this file, yuzu will create a
@@ -125,4 +139,36 @@ private:
boost::container::flat_map<u64, CNMT> yuzu_meta;
};
+// Combines multiple RegisteredCaches (i.e. SysNAND, UserNAND, SDMC) into one interface.
+class RegisteredCacheUnion {
+public:
+ explicit RegisteredCacheUnion(std::vector<std::shared_ptr<RegisteredCache>> caches);
+
+ void Refresh();
+
+ bool HasEntry(u64 title_id, ContentRecordType type) const;
+ bool HasEntry(RegisteredCacheEntry entry) const;
+
+ boost::optional<u32> GetEntryVersion(u64 title_id) const;
+
+ VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const;
+ VirtualFile GetEntryUnparsed(RegisteredCacheEntry entry) const;
+
+ VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const;
+ VirtualFile GetEntryRaw(RegisteredCacheEntry entry) const;
+
+ std::shared_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const;
+ std::shared_ptr<NCA> GetEntry(RegisteredCacheEntry entry) const;
+
+ std::vector<RegisteredCacheEntry> ListEntries() const;
+ // If a parameter is not boost::none, it will be filtered for from all entries.
+ std::vector<RegisteredCacheEntry> ListEntriesFilter(
+ boost::optional<TitleType> title_type = boost::none,
+ boost::optional<ContentRecordType> record_type = boost::none,
+ boost::optional<u64> title_id = boost::none) const;
+
+private:
+ std::vector<std::shared_ptr<RegisteredCache>> caches;
+};
+
} // namespace FileSys
diff --git a/src/core/file_sys/romfs.cpp b/src/core/file_sys/romfs.cpp
index e490c8ace..9f6e41cdf 100644
--- a/src/core/file_sys/romfs.cpp
+++ b/src/core/file_sys/romfs.cpp
@@ -49,7 +49,7 @@ struct FileEntry {
static_assert(sizeof(FileEntry) == 0x20, "FileEntry has incorrect size.");
template <typename Entry>
-static std::pair<Entry, std::string> GetEntry(const VirtualFile& file, size_t offset) {
+static std::pair<Entry, std::string> GetEntry(const VirtualFile& file, std::size_t offset) {
Entry entry{};
if (file->ReadObject(&entry, offset) != sizeof(Entry))
return {};
@@ -59,8 +59,8 @@ static std::pair<Entry, std::string> GetEntry(const VirtualFile& file, size_t of
return {entry, string};
}
-void ProcessFile(VirtualFile file, size_t file_offset, size_t data_offset, u32 this_file_offset,
- std::shared_ptr<VectorVfsDirectory> parent) {
+void ProcessFile(VirtualFile file, std::size_t file_offset, std::size_t data_offset,
+ u32 this_file_offset, std::shared_ptr<VectorVfsDirectory> parent) {
while (true) {
auto entry = GetEntry<FileEntry>(file, file_offset + this_file_offset);
@@ -74,8 +74,9 @@ void ProcessFile(VirtualFile file, size_t file_offset, size_t data_offset, u32 t
}
}
-void ProcessDirectory(VirtualFile file, size_t dir_offset, size_t file_offset, size_t data_offset,
- u32 this_dir_offset, std::shared_ptr<VectorVfsDirectory> parent) {
+void ProcessDirectory(VirtualFile file, std::size_t dir_offset, std::size_t file_offset,
+ std::size_t data_offset, u32 this_dir_offset,
+ std::shared_ptr<VectorVfsDirectory> parent) {
while (true) {
auto entry = GetEntry<DirectoryEntry>(file, dir_offset + this_dir_offset);
auto current = std::make_shared<VectorVfsDirectory>(
diff --git a/src/core/file_sys/romfs.h b/src/core/file_sys/romfs.h
index 03a876d22..e54a7d7a9 100644
--- a/src/core/file_sys/romfs.h
+++ b/src/core/file_sys/romfs.h
@@ -6,6 +6,7 @@
#include <array>
#include "common/common_funcs.h"
+#include "common/common_types.h"
#include "common/swap.h"
#include "core/file_sys/vfs.h"
diff --git a/src/core/file_sys/romfs_factory.cpp b/src/core/file_sys/romfs_factory.cpp
index eb4e6c865..3d1a3685e 100644
--- a/src/core/file_sys/romfs_factory.cpp
+++ b/src/core/file_sys/romfs_factory.cpp
@@ -2,11 +2,14 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
-#include <algorithm>
#include <memory>
+#include "common/assert.h"
#include "common/common_types.h"
#include "common/logging/log.h"
+#include "core/core.h"
+#include "core/file_sys/content_archive.h"
#include "core/file_sys/nca_metadata.h"
+#include "core/file_sys/patch_manager.h"
#include "core/file_sys/registered_cache.h"
#include "core/file_sys/romfs_factory.h"
#include "core/hle/kernel/process.h"
@@ -20,10 +23,19 @@ RomFSFactory::RomFSFactory(Loader::AppLoader& app_loader) {
if (app_loader.ReadRomFS(file) != Loader::ResultStatus::Success) {
LOG_ERROR(Service_FS, "Unable to read RomFS!");
}
+
+ updatable = app_loader.IsRomFSUpdatable();
+ ivfc_offset = app_loader.ReadRomFSIVFCOffset();
}
+RomFSFactory::~RomFSFactory() = default;
+
ResultVal<VirtualFile> RomFSFactory::OpenCurrentProcess() {
- return MakeResult<VirtualFile>(file);
+ if (!updatable)
+ return MakeResult<VirtualFile>(file);
+
+ const PatchManager patch_manager(Core::CurrentProcess()->program_id);
+ return MakeResult<VirtualFile>(patch_manager.PatchRomFS(file, ivfc_offset));
}
ResultVal<VirtualFile> RomFSFactory::Open(u64 title_id, StorageId storage, ContentRecordType type) {
diff --git a/src/core/file_sys/romfs_factory.h b/src/core/file_sys/romfs_factory.h
index f38ddc4f7..2cace8180 100644
--- a/src/core/file_sys/romfs_factory.h
+++ b/src/core/file_sys/romfs_factory.h
@@ -30,12 +30,15 @@ enum class StorageId : u8 {
class RomFSFactory {
public:
explicit RomFSFactory(Loader::AppLoader& app_loader);
+ ~RomFSFactory();
ResultVal<VirtualFile> OpenCurrentProcess();
ResultVal<VirtualFile> Open(u64 title_id, StorageId storage, ContentRecordType type);
private:
VirtualFile file;
+ bool updatable;
+ u64 ivfc_offset;
};
} // namespace FileSys
diff --git a/src/core/file_sys/savedata_factory.cpp b/src/core/file_sys/savedata_factory.cpp
index 952bd74b3..9b2c51bbd 100644
--- a/src/core/file_sys/savedata_factory.cpp
+++ b/src/core/file_sys/savedata_factory.cpp
@@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#include <memory>
+#include "common/assert.h"
#include "common/common_types.h"
#include "common/logging/log.h"
#include "core/core.h"
@@ -19,6 +20,8 @@ std::string SaveDataDescriptor::DebugInfo() const {
SaveDataFactory::SaveDataFactory(VirtualDir save_directory) : dir(std::move(save_directory)) {}
+SaveDataFactory::~SaveDataFactory() = default;
+
ResultVal<VirtualDir> SaveDataFactory::Open(SaveDataSpaceId space, SaveDataDescriptor meta) {
if (meta.type == SaveDataType::SystemSaveData || meta.type == SaveDataType::SaveData) {
if (meta.zero_1 != 0) {
@@ -84,10 +87,10 @@ std::string SaveDataFactory::GetFullPath(SaveDataSpaceId space, SaveDataType typ
switch (space) {
case SaveDataSpaceId::NandSystem:
- out = "/system/save/";
+ out = "/system/";
break;
case SaveDataSpaceId::NandUser:
- out = "/user/save/";
+ out = "/user/";
break;
default:
ASSERT_MSG(false, "Unrecognized SaveDataSpaceId: {:02X}", static_cast<u8>(space));
@@ -95,9 +98,12 @@ std::string SaveDataFactory::GetFullPath(SaveDataSpaceId space, SaveDataType typ
switch (type) {
case SaveDataType::SystemSaveData:
- return fmt::format("{}{:016X}/{:016X}{:016X}", out, save_id, user_id[1], user_id[0]);
+ return fmt::format("{}save/{:016X}/{:016X}{:016X}", out, save_id, user_id[1], user_id[0]);
case SaveDataType::SaveData:
- return fmt::format("{}{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0],
+ return fmt::format("{}save/{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0],
+ title_id);
+ case SaveDataType::TemporaryStorage:
+ return fmt::format("{}temp/{:016X}/{:016X}{:016X}/{:016X}", out, 0, user_id[1], user_id[0],
title_id);
default:
ASSERT_MSG(false, "Unrecognized SaveDataType: {:02X}", static_cast<u8>(type));
diff --git a/src/core/file_sys/savedata_factory.h b/src/core/file_sys/savedata_factory.h
index c6f9549f0..d69ef6741 100644
--- a/src/core/file_sys/savedata_factory.h
+++ b/src/core/file_sys/savedata_factory.h
@@ -6,6 +6,7 @@
#include <memory>
#include <string>
+#include "common/common_funcs.h"
#include "common/common_types.h"
#include "common/swap.h"
#include "core/file_sys/vfs.h"
@@ -47,6 +48,7 @@ static_assert(sizeof(SaveDataDescriptor) == 0x40, "SaveDataDescriptor has incorr
class SaveDataFactory {
public:
explicit SaveDataFactory(VirtualDir dir);
+ ~SaveDataFactory();
ResultVal<VirtualDir> Open(SaveDataSpaceId space, SaveDataDescriptor meta);
diff --git a/src/core/file_sys/submission_package.cpp b/src/core/file_sys/submission_package.cpp
new file mode 100644
index 000000000..11264878d
--- /dev/null
+++ b/src/core/file_sys/submission_package.cpp
@@ -0,0 +1,245 @@
+// Copyright 2018 yuzu emulator team
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <algorithm>
+#include <cstring>
+#include <string_view>
+
+#include <fmt/ostream.h>
+
+#include "common/hex_util.h"
+#include "common/logging/log.h"
+#include "core/crypto/key_manager.h"
+#include "core/file_sys/content_archive.h"
+#include "core/file_sys/nca_metadata.h"
+#include "core/file_sys/partition_filesystem.h"
+#include "core/file_sys/submission_package.h"
+#include "core/loader/loader.h"
+
+namespace FileSys {
+NSP::NSP(VirtualFile file_)
+ : file(std::move(file_)), status{Loader::ResultStatus::Success},
+ pfs(std::make_shared<PartitionFilesystem>(file)) {
+ if (pfs->GetStatus() != Loader::ResultStatus::Success) {
+ status = pfs->GetStatus();
+ return;
+ }
+
+ if (IsDirectoryExeFS(pfs)) {
+ extracted = true;
+ exefs = pfs;
+
+ const auto& files = pfs->GetFiles();
+ const auto romfs_iter =
+ std::find_if(files.begin(), files.end(), [](const FileSys::VirtualFile& file) {
+ return file->GetName().find(".romfs") != std::string::npos;
+ });
+ if (romfs_iter != files.end())
+ romfs = *romfs_iter;
+ return;
+ }
+
+ extracted = false;
+ const auto files = pfs->GetFiles();
+
+ Core::Crypto::KeyManager keys;
+ for (const auto& ticket_file : files) {
+ if (ticket_file->GetExtension() == "tik") {
+ if (ticket_file == nullptr ||
+ ticket_file->GetSize() <
+ Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) {
+ continue;
+ }
+
+ Core::Crypto::Key128 key{};
+ ticket_file->Read(key.data(), key.size(), Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET);
+ std::string_view name_only(ticket_file->GetName());
+ name_only.remove_suffix(4);
+ const auto rights_id_raw = Common::HexStringToArray<16>(name_only);
+ u128 rights_id;
+ std::memcpy(rights_id.data(), rights_id_raw.data(), sizeof(u128));
+ keys.SetKey(Core::Crypto::S128KeyType::Titlekey, key, rights_id[1], rights_id[0]);
+ }
+ }
+
+ for (const auto& outer_file : files) {
+ if (outer_file->GetName().substr(outer_file->GetName().size() - 9) == ".cnmt.nca") {
+ const auto nca = std::make_shared<NCA>(outer_file);
+ if (nca->GetStatus() != Loader::ResultStatus::Success) {
+ program_status[nca->GetTitleId()] = nca->GetStatus();
+ continue;
+ }
+
+ const auto section0 = nca->GetSubdirectories()[0];
+
+ for (const auto& inner_file : section0->GetFiles()) {
+ if (inner_file->GetExtension() != "cnmt")
+ continue;
+
+ const CNMT cnmt(inner_file);
+ auto& ncas_title = ncas[cnmt.GetTitleID()];
+
+ ncas_title[ContentRecordType::Meta] = nca;
+ for (const auto& rec : cnmt.GetContentRecords()) {
+ const auto id_string = Common::HexArrayToString(rec.nca_id, false);
+ const auto next_file = pfs->GetFile(fmt::format("{}.nca", id_string));
+ if (next_file == nullptr) {
+ LOG_WARNING(Service_FS,
+ "NCA with ID {}.nca is listed in content metadata, but cannot "
+ "be found in PFS. NSP appears to be corrupted.",
+ id_string);
+ continue;
+ }
+
+ auto next_nca = std::make_shared<NCA>(next_file);
+ if (next_nca->GetType() == NCAContentType::Program)
+ program_status[cnmt.GetTitleID()] = next_nca->GetStatus();
+ if (next_nca->GetStatus() == Loader::ResultStatus::Success)
+ ncas_title[rec.type] = std::move(next_nca);
+ }
+
+ break;
+ }
+ }
+ }
+}
+
+NSP::~NSP() = default;
+
+Loader::ResultStatus NSP::GetStatus() const {
+ return status;
+}
+
+Loader::ResultStatus NSP::GetProgramStatus(u64 title_id) const {
+ const auto iter = program_status.find(title_id);
+ if (iter == program_status.end())
+ return Loader::ResultStatus::ErrorNSPMissingProgramNCA;
+ return iter->second;
+}
+
+u64 NSP::GetFirstTitleID() const {
+ if (program_status.empty())
+ return 0;
+ return program_status.begin()->first;
+}
+
+u64 NSP::GetProgramTitleID() const {
+ const auto out = GetFirstTitleID();
+ if ((out & 0x800) == 0)
+ return out;
+
+ const auto ids = GetTitleIDs();
+ const auto iter =
+ std::find_if(ids.begin(), ids.end(), [](u64 tid) { return (tid & 0x800) == 0; });
+ return iter == ids.end() ? out : *iter;
+}
+
+std::vector<u64> NSP::GetTitleIDs() const {
+ std::vector<u64> out;
+ out.reserve(ncas.size());
+ for (const auto& kv : ncas)
+ out.push_back(kv.first);
+ return out;
+}
+
+bool NSP::IsExtractedType() const {
+ return extracted;
+}
+
+VirtualFile NSP::GetRomFS() const {
+ return romfs;
+}
+
+VirtualDir NSP::GetExeFS() const {
+ return exefs;
+}
+
+std::vector<std::shared_ptr<NCA>> NSP::GetNCAsCollapsed() const {
+ if (extracted)
+ LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
+ std::vector<std::shared_ptr<NCA>> out;
+ for (const auto& map : ncas) {
+ for (const auto& inner_map : map.second)
+ out.push_back(inner_map.second);
+ }
+ return out;
+}
+
+std::multimap<u64, std::shared_ptr<NCA>> NSP::GetNCAsByTitleID() const {
+ if (extracted)
+ LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
+ std::multimap<u64, std::shared_ptr<NCA>> out;
+ for (const auto& map : ncas) {
+ for (const auto& inner_map : map.second)
+ out.emplace(map.first, inner_map.second);
+ }
+ return out;
+}
+
+std::map<u64, std::map<ContentRecordType, std::shared_ptr<NCA>>> NSP::GetNCAs() const {
+ return ncas;
+}
+
+std::shared_ptr<NCA> NSP::GetNCA(u64 title_id, ContentRecordType type) const {
+ if (extracted)
+ LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
+
+ const auto title_id_iter = ncas.find(title_id);
+ if (title_id_iter == ncas.end())
+ return nullptr;
+
+ const auto type_iter = title_id_iter->second.find(type);
+ if (type_iter == title_id_iter->second.end())
+ return nullptr;
+
+ return type_iter->second;
+}
+
+VirtualFile NSP::GetNCAFile(u64 title_id, ContentRecordType type) const {
+ if (extracted)
+ LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
+ const auto nca = GetNCA(title_id, type);
+ if (nca != nullptr)
+ return nca->GetBaseFile();
+ return nullptr;
+}
+
+std::vector<Core::Crypto::Key128> NSP::GetTitlekey() const {
+ if (extracted)
+ LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
+ std::vector<Core::Crypto::Key128> out;
+ for (const auto& ticket_file : ticket_files) {
+ if (ticket_file == nullptr ||
+ ticket_file->GetSize() <
+ Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) {
+ continue;
+ }
+
+ out.emplace_back();
+ ticket_file->Read(out.back().data(), out.back().size(),
+ Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET);
+ }
+ return out;
+}
+
+std::vector<VirtualFile> NSP::GetFiles() const {
+ return pfs->GetFiles();
+}
+
+std::vector<VirtualDir> NSP::GetSubdirectories() const {
+ return pfs->GetSubdirectories();
+}
+
+std::string NSP::GetName() const {
+ return file->GetName();
+}
+
+VirtualDir NSP::GetParentDirectory() const {
+ return file->GetContainingDirectory();
+}
+
+bool NSP::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
+ return false;
+}
+} // namespace FileSys
diff --git a/src/core/file_sys/submission_package.h b/src/core/file_sys/submission_package.h
new file mode 100644
index 000000000..e85a2b76e
--- /dev/null
+++ b/src/core/file_sys/submission_package.h
@@ -0,0 +1,76 @@
+// Copyright 2018 yuzu emulator team
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <map>
+#include <memory>
+#include <vector>
+#include "common/common_types.h"
+#include "core/file_sys/vfs.h"
+
+namespace Loader {
+enum class ResultStatus : u16;
+}
+
+namespace FileSys {
+
+class NCA;
+class PartitionFilesystem;
+
+enum class ContentRecordType : u8;
+
+class NSP : public ReadOnlyVfsDirectory {
+public:
+ explicit NSP(VirtualFile file);
+ ~NSP() override;
+
+ Loader::ResultStatus GetStatus() const;
+ Loader::ResultStatus GetProgramStatus(u64 title_id) const;
+ // Should only be used when one title id can be assured.
+ u64 GetFirstTitleID() const;
+ u64 GetProgramTitleID() const;
+ std::vector<u64> GetTitleIDs() const;
+
+ bool IsExtractedType() const;
+
+ // Common (Can be safely called on both types)
+ VirtualFile GetRomFS() const;
+ VirtualDir GetExeFS() const;
+
+ // Type 0 Only (Collection of NCAs + Certificate + Ticket + Meta XML)
+ std::vector<std::shared_ptr<NCA>> GetNCAsCollapsed() const;
+ std::multimap<u64, std::shared_ptr<NCA>> GetNCAsByTitleID() const;
+ std::map<u64, std::map<ContentRecordType, std::shared_ptr<NCA>>> GetNCAs() const;
+ std::shared_ptr<NCA> GetNCA(u64 title_id, ContentRecordType type) const;
+ VirtualFile GetNCAFile(u64 title_id, ContentRecordType type) const;
+ std::vector<Core::Crypto::Key128> GetTitlekey() const;
+
+ std::vector<VirtualFile> GetFiles() const override;
+
+ std::vector<VirtualDir> GetSubdirectories() const override;
+
+ std::string GetName() const override;
+
+ VirtualDir GetParentDirectory() const override;
+
+protected:
+ bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
+
+private:
+ VirtualFile file;
+
+ bool extracted;
+ Loader::ResultStatus status;
+ std::map<u64, Loader::ResultStatus> program_status;
+
+ std::shared_ptr<PartitionFilesystem> pfs;
+ // Map title id -> {map type -> NCA}
+ std::map<u64, std::map<ContentRecordType, std::shared_ptr<NCA>>> ncas;
+ std::vector<VirtualFile> ticket_files;
+
+ VirtualFile romfs;
+ VirtualDir exefs;
+};
+} // namespace FileSys
diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp
index 146c839f4..d7b52abfd 100644
--- a/src/core/file_sys/vfs.cpp
+++ b/src/core/file_sys/vfs.cpp
@@ -167,18 +167,18 @@ std::string VfsFile::GetExtension() const {
VfsDirectory::~VfsDirectory() = default;
-boost::optional<u8> VfsFile::ReadByte(size_t offset) const {
+boost::optional<u8> VfsFile::ReadByte(std::size_t offset) const {
u8 out{};
- size_t size = Read(&out, 1, offset);
+ std::size_t size = Read(&out, 1, offset);
if (size == 1)
return out;
return boost::none;
}
-std::vector<u8> VfsFile::ReadBytes(size_t size, size_t offset) const {
+std::vector<u8> VfsFile::ReadBytes(std::size_t size, std::size_t offset) const {
std::vector<u8> out(size);
- size_t read_size = Read(out.data(), size, offset);
+ std::size_t read_size = Read(out.data(), size, offset);
out.resize(read_size);
return out;
}
@@ -187,11 +187,11 @@ std::vector<u8> VfsFile::ReadAllBytes() const {
return ReadBytes(GetSize());
}
-bool VfsFile::WriteByte(u8 data, size_t offset) {
+bool VfsFile::WriteByte(u8 data, std::size_t offset) {
return Write(&data, 1, offset) == 1;
}
-size_t VfsFile::WriteBytes(const std::vector<u8>& data, size_t offset) {
+std::size_t VfsFile::WriteBytes(const std::vector<u8>& data, std::size_t offset) {
return Write(data.data(), data.size(), offset);
}
@@ -215,7 +215,7 @@ std::shared_ptr<VfsFile> VfsDirectory::GetFileRelative(std::string_view path) co
}
auto dir = GetSubdirectory(vec[0]);
- for (size_t component = 1; component < vec.size() - 1; ++component) {
+ for (std::size_t component = 1; component < vec.size() - 1; ++component) {
if (dir == nullptr) {
return nullptr;
}
@@ -249,7 +249,7 @@ std::shared_ptr<VfsDirectory> VfsDirectory::GetDirectoryRelative(std::string_vie
}
auto dir = GetSubdirectory(vec[0]);
- for (size_t component = 1; component < vec.size(); ++component) {
+ for (std::size_t component = 1; component < vec.size(); ++component) {
if (dir == nullptr) {
return nullptr;
}
@@ -286,7 +286,7 @@ bool VfsDirectory::IsRoot() const {
return GetParentDirectory() == nullptr;
}
-size_t VfsDirectory::GetSize() const {
+std::size_t VfsDirectory::GetSize() const {
const auto& files = GetFiles();
const auto sum_sizes = [](const auto& range) {
return std::accumulate(range.begin(), range.end(), 0ULL,
@@ -434,13 +434,13 @@ bool ReadOnlyVfsDirectory::Rename(std::string_view name) {
return false;
}
-bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, size_t block_size) {
+bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, std::size_t block_size) {
if (file1->GetSize() != file2->GetSize())
return false;
std::vector<u8> f1_v(block_size);
std::vector<u8> f2_v(block_size);
- for (size_t i = 0; i < file1->GetSize(); i += block_size) {
+ for (std::size_t i = 0; i < file1->GetSize(); i += block_size) {
auto f1_vs = file1->Read(f1_v.data(), block_size, i);
auto f2_vs = file2->Read(f2_v.data(), block_size, i);
diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h
index 5142a3e86..74489b452 100644
--- a/src/core/file_sys/vfs.h
+++ b/src/core/file_sys/vfs.h
@@ -92,9 +92,9 @@ public:
// Retrieves the extension of the file name.
virtual std::string GetExtension() const;
// Retrieves the size of the file.
- virtual size_t GetSize() const = 0;
+ virtual std::size_t GetSize() const = 0;
// Resizes the file to new_size. Returns whether or not the operation was successful.
- virtual bool Resize(size_t new_size) = 0;
+ virtual bool Resize(std::size_t new_size) = 0;
// Gets a pointer to the directory containing this file, returning nullptr if there is none.
virtual std::shared_ptr<VfsDirectory> GetContainingDirectory() const = 0;
@@ -105,15 +105,15 @@ public:
// The primary method of reading from the file. Reads length bytes into data starting at offset
// into file. Returns number of bytes successfully read.
- virtual size_t Read(u8* data, size_t length, size_t offset = 0) const = 0;
+ virtual std::size_t Read(u8* data, std::size_t length, std::size_t offset = 0) const = 0;
// The primary method of writing to the file. Writes length bytes from data starting at offset
// into file. Returns number of bytes successfully written.
- virtual size_t Write(const u8* data, size_t length, size_t offset = 0) = 0;
+ virtual std::size_t Write(const u8* data, std::size_t length, std::size_t offset = 0) = 0;
// Reads exactly one byte at the offset provided, returning boost::none on error.
- virtual boost::optional<u8> ReadByte(size_t offset = 0) const;
+ virtual boost::optional<u8> ReadByte(std::size_t offset = 0) const;
// Reads size bytes starting at offset in file into a vector.
- virtual std::vector<u8> ReadBytes(size_t size, size_t offset = 0) const;
+ virtual std::vector<u8> ReadBytes(std::size_t size, std::size_t offset = 0) const;
// Reads all the bytes from the file into a vector. Equivalent to 'file->Read(file->GetSize(),
// 0)'
virtual std::vector<u8> ReadAllBytes() const;
@@ -121,7 +121,7 @@ public:
// Reads an array of type T, size number_elements starting at offset.
// Returns the number of bytes (sizeof(T)*number_elements) read successfully.
template <typename T>
- size_t ReadArray(T* data, size_t number_elements, size_t offset = 0) const {
+ std::size_t ReadArray(T* data, std::size_t number_elements, std::size_t offset = 0) const {
static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
return Read(reinterpret_cast<u8*>(data), number_elements * sizeof(T), offset);
@@ -130,7 +130,7 @@ public:
// Reads size bytes into the memory starting at data starting at offset into the file.
// Returns the number of bytes read successfully.
template <typename T>
- size_t ReadBytes(T* data, size_t size, size_t offset = 0) const {
+ std::size_t ReadBytes(T* data, std::size_t size, std::size_t offset = 0) const {
static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
return Read(reinterpret_cast<u8*>(data), size, offset);
}
@@ -138,22 +138,22 @@ public:
// Reads one object of type T starting at offset in file.
// Returns the number of bytes read successfully (sizeof(T)).
template <typename T>
- size_t ReadObject(T* data, size_t offset = 0) const {
+ std::size_t ReadObject(T* data, std::size_t offset = 0) const {
static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
return Read(reinterpret_cast<u8*>(data), sizeof(T), offset);
}
// Writes exactly one byte to offset in file and retuns whether or not the byte was written
// successfully.
- virtual bool WriteByte(u8 data, size_t offset = 0);
+ virtual bool WriteByte(u8 data, std::size_t offset = 0);
// Writes a vector of bytes to offset in file and returns the number of bytes successfully
// written.
- virtual size_t WriteBytes(const std::vector<u8>& data, size_t offset = 0);
+ virtual std::size_t WriteBytes(const std::vector<u8>& data, std::size_t offset = 0);
// Writes an array of type T, size number_elements to offset in file.
// Returns the number of bytes (sizeof(T)*number_elements) written successfully.
template <typename T>
- size_t WriteArray(const T* data, size_t number_elements, size_t offset = 0) {
+ std::size_t WriteArray(const T* data, std::size_t number_elements, std::size_t offset = 0) {
static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
return Write(data, number_elements * sizeof(T), offset);
}
@@ -161,7 +161,7 @@ public:
// Writes size bytes starting at memory location data to offset in file.
// Returns the number of bytes written successfully.
template <typename T>
- size_t WriteBytes(const T* data, size_t size, size_t offset = 0) {
+ std::size_t WriteBytes(const T* data, std::size_t size, std::size_t offset = 0) {
static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
return Write(reinterpret_cast<const u8*>(data), size, offset);
}
@@ -169,7 +169,7 @@ public:
// Writes one object of type T to offset in file.
// Returns the number of bytes written successfully (sizeof(T)).
template <typename T>
- size_t WriteObject(const T& data, size_t offset = 0) {
+ std::size_t WriteObject(const T& data, std::size_t offset = 0) {
static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
return Write(&data, sizeof(T), offset);
}
@@ -221,7 +221,7 @@ public:
// Returns the name of the directory.
virtual std::string GetName() const = 0;
// Returns the total size of all files and subdirectories in this directory.
- virtual size_t GetSize() const;
+ virtual std::size_t GetSize() const;
// Returns the parent directory of this directory. Returns nullptr if this directory is root or
// has no parent.
virtual std::shared_ptr<VfsDirectory> GetParentDirectory() const = 0;
@@ -311,7 +311,7 @@ public:
};
// Compare the two files, byte-for-byte, in increments specificed by block_size
-bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, size_t block_size = 0x200);
+bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, std::size_t block_size = 0x200);
// A method that copies the raw data between two different implementations of VirtualFile. If you
// are using the same implementation, it is probably better to use the Copy method in the parent
diff --git a/src/core/file_sys/vfs_concat.cpp b/src/core/file_sys/vfs_concat.cpp
index e6bf586a3..dc7a279a9 100644
--- a/src/core/file_sys/vfs_concat.cpp
+++ b/src/core/file_sys/vfs_concat.cpp
@@ -20,13 +20,15 @@ VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string name) {
ConcatenatedVfsFile::ConcatenatedVfsFile(std::vector<VirtualFile> files_, std::string name)
: name(std::move(name)) {
- size_t next_offset = 0;
+ std::size_t next_offset = 0;
for (const auto& file : files_) {
files[next_offset] = file;
next_offset += file->GetSize();
}
}
+ConcatenatedVfsFile::~ConcatenatedVfsFile() = default;
+
std::string ConcatenatedVfsFile::GetName() const {
if (files.empty())
return "";
@@ -35,13 +37,13 @@ std::string ConcatenatedVfsFile::GetName() const {
return files.begin()->second->GetName();
}
-size_t ConcatenatedVfsFile::GetSize() const {
+std::size_t ConcatenatedVfsFile::GetSize() const {
if (files.empty())
return 0;
return files.rbegin()->first + files.rbegin()->second->GetSize();
}
-bool ConcatenatedVfsFile::Resize(size_t new_size) {
+bool ConcatenatedVfsFile::Resize(std::size_t new_size) {
return false;
}
@@ -59,7 +61,7 @@ bool ConcatenatedVfsFile::IsReadable() const {
return true;
}
-size_t ConcatenatedVfsFile::Read(u8* data, size_t length, size_t offset) const {
+std::size_t ConcatenatedVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
auto entry = files.end();
for (auto iter = files.begin(); iter != files.end(); ++iter) {
if (iter->first > offset) {
@@ -84,7 +86,7 @@ size_t ConcatenatedVfsFile::Read(u8* data, size_t length, size_t offset) const {
return entry->second->Read(data, length, offset - entry->first);
}
-size_t ConcatenatedVfsFile::Write(const u8* data, size_t length, size_t offset) {
+std::size_t ConcatenatedVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) {
return 0;
}
diff --git a/src/core/file_sys/vfs_concat.h b/src/core/file_sys/vfs_concat.h
index 686d32515..717d04bdc 100644
--- a/src/core/file_sys/vfs_concat.h
+++ b/src/core/file_sys/vfs_concat.h
@@ -22,14 +22,16 @@ class ConcatenatedVfsFile : public VfsFile {
ConcatenatedVfsFile(std::vector<VirtualFile> files, std::string name);
public:
+ ~ConcatenatedVfsFile() override;
+
std::string GetName() const override;
- size_t GetSize() const override;
- bool Resize(size_t new_size) override;
+ std::size_t GetSize() const override;
+ bool Resize(std::size_t new_size) override;
std::shared_ptr<VfsDirectory> GetContainingDirectory() const override;
bool IsWritable() const override;
bool IsReadable() const override;
- size_t Read(u8* data, size_t length, size_t offset) const override;
- size_t Write(const u8* data, size_t length, size_t offset) override;
+ std::size_t Read(u8* data, std::size_t length, std::size_t offset) const override;
+ std::size_t Write(const u8* data, std::size_t length, std::size_t offset) override;
bool Rename(std::string_view name) override;
private:
diff --git a/src/core/file_sys/vfs_offset.cpp b/src/core/file_sys/vfs_offset.cpp
index 847cde2f5..a4c6719a0 100644
--- a/src/core/file_sys/vfs_offset.cpp
+++ b/src/core/file_sys/vfs_offset.cpp
@@ -9,20 +9,22 @@
namespace FileSys {
-OffsetVfsFile::OffsetVfsFile(std::shared_ptr<VfsFile> file_, size_t size_, size_t offset_,
+OffsetVfsFile::OffsetVfsFile(std::shared_ptr<VfsFile> file_, std::size_t size_, std::size_t offset_,
std::string name_, VirtualDir parent_)
: file(file_), offset(offset_), size(size_), name(std::move(name_)),
parent(parent_ == nullptr ? file->GetContainingDirectory() : std::move(parent_)) {}
+OffsetVfsFile::~OffsetVfsFile() = default;
+
std::string OffsetVfsFile::GetName() const {
return name.empty() ? file->GetName() : name;
}
-size_t OffsetVfsFile::GetSize() const {
+std::size_t OffsetVfsFile::GetSize() const {
return size;
}
-bool OffsetVfsFile::Resize(size_t new_size) {
+bool OffsetVfsFile::Resize(std::size_t new_size) {
if (offset + new_size < file->GetSize()) {
size = new_size;
} else {
@@ -47,22 +49,22 @@ bool OffsetVfsFile::IsReadable() const {
return file->IsReadable();
}
-size_t OffsetVfsFile::Read(u8* data, size_t length, size_t r_offset) const {
+std::size_t OffsetVfsFile::Read(u8* data, std::size_t length, std::size_t r_offset) const {
return file->Read(data, TrimToFit(length, r_offset), offset + r_offset);
}
-size_t OffsetVfsFile::Write(const u8* data, size_t length, size_t r_offset) {
+std::size_t OffsetVfsFile::Write(const u8* data, std::size_t length, std::size_t r_offset) {
return file->Write(data, TrimToFit(length, r_offset), offset + r_offset);
}
-boost::optional<u8> OffsetVfsFile::ReadByte(size_t r_offset) const {
+boost::optional<u8> OffsetVfsFile::ReadByte(std::size_t r_offset) const {
if (r_offset < size)
return file->ReadByte(offset + r_offset);
return boost::none;
}
-std::vector<u8> OffsetVfsFile::ReadBytes(size_t r_size, size_t r_offset) const {
+std::vector<u8> OffsetVfsFile::ReadBytes(std::size_t r_size, std::size_t r_offset) const {
return file->ReadBytes(TrimToFit(r_size, r_offset), offset + r_offset);
}
@@ -70,14 +72,14 @@ std::vector<u8> OffsetVfsFile::ReadAllBytes() const {
return file->ReadBytes(size, offset);
}
-bool OffsetVfsFile::WriteByte(u8 data, size_t r_offset) {
+bool OffsetVfsFile::WriteByte(u8 data, std::size_t r_offset) {
if (r_offset < size)
return file->WriteByte(data, offset + r_offset);
return false;
}
-size_t OffsetVfsFile::WriteBytes(const std::vector<u8>& data, size_t r_offset) {
+std::size_t OffsetVfsFile::WriteBytes(const std::vector<u8>& data, std::size_t r_offset) {
return file->Write(data.data(), TrimToFit(data.size(), r_offset), offset + r_offset);
}
@@ -85,12 +87,12 @@ bool OffsetVfsFile::Rename(std::string_view name) {
return file->Rename(name);
}
-size_t OffsetVfsFile::GetOffset() const {
+std::size_t OffsetVfsFile::GetOffset() const {
return offset;
}
-size_t OffsetVfsFile::TrimToFit(size_t r_size, size_t r_offset) const {
- return std::clamp(r_size, size_t{0}, size - r_offset);
+std::size_t OffsetVfsFile::TrimToFit(std::size_t r_size, std::size_t r_offset) const {
+ return std::clamp(r_size, std::size_t{0}, size - r_offset);
}
} // namespace FileSys
diff --git a/src/core/file_sys/vfs_offset.h b/src/core/file_sys/vfs_offset.h
index cb92d1570..8062702a7 100644
--- a/src/core/file_sys/vfs_offset.h
+++ b/src/core/file_sys/vfs_offset.h
@@ -17,33 +17,34 @@ namespace FileSys {
// the size of this wrapper.
class OffsetVfsFile : public VfsFile {
public:
- OffsetVfsFile(std::shared_ptr<VfsFile> file, size_t size, size_t offset = 0,
+ OffsetVfsFile(std::shared_ptr<VfsFile> file, std::size_t size, std::size_t offset = 0,
std::string new_name = "", VirtualDir new_parent = nullptr);
+ ~OffsetVfsFile() override;
std::string GetName() const override;
- size_t GetSize() const override;
- bool Resize(size_t new_size) override;
+ std::size_t GetSize() const override;
+ bool Resize(std::size_t new_size) override;
std::shared_ptr<VfsDirectory> GetContainingDirectory() const override;
bool IsWritable() const override;
bool IsReadable() const override;
- size_t Read(u8* data, size_t length, size_t offset) const override;
- size_t Write(const u8* data, size_t length, size_t offset) override;
- boost::optional<u8> ReadByte(size_t offset) const override;
- std::vector<u8> ReadBytes(size_t size, size_t offset) const override;
+ std::size_t Read(u8* data, std::size_t length, std::size_t offset) const override;
+ std::size_t Write(const u8* data, std::size_t length, std::size_t offset) override;
+ boost::optional<u8> ReadByte(std::size_t offset) const override;
+ std::vector<u8> ReadBytes(std::size_t size, std::size_t offset) const override;
std::vector<u8> ReadAllBytes() const override;
- bool WriteByte(u8 data, size_t offset) override;
- size_t WriteBytes(const std::vector<u8>& data, size_t offset) override;
+ bool WriteByte(u8 data, std::size_t offset) override;
+ std::size_t WriteBytes(const std::vector<u8>& data, std::size_t offset) override;
bool Rename(std::string_view name) override;
- size_t GetOffset() const;
+ std::size_t GetOffset() const;
private:
- size_t TrimToFit(size_t r_size, size_t r_offset) const;
+ std::size_t TrimToFit(std::size_t r_size, std::size_t r_offset) const;
std::shared_ptr<VfsFile> file;
- size_t offset;
- size_t size;
+ std::size_t offset;
+ std::size_t size;
std::string name;
VirtualDir parent;
};
diff --git a/src/core/file_sys/vfs_real.cpp b/src/core/file_sys/vfs_real.cpp
index 2b8ac7103..5e242e20f 100644
--- a/src/core/file_sys/vfs_real.cpp
+++ b/src/core/file_sys/vfs_real.cpp
@@ -8,6 +8,7 @@
#include <utility>
#include "common/assert.h"
#include "common/common_paths.h"
+#include "common/file_util.h"
#include "common/logging/log.h"
#include "core/file_sys/vfs_real.h"
@@ -39,6 +40,7 @@ static std::string ModeFlagsToString(Mode mode) {
}
RealVfsFilesystem::RealVfsFilesystem() : VfsFilesystem(nullptr) {}
+RealVfsFilesystem::~RealVfsFilesystem() = default;
std::string RealVfsFilesystem::GetName() const {
return "Real";
@@ -219,15 +221,17 @@ RealVfsFile::RealVfsFile(RealVfsFilesystem& base_, std::shared_ptr<FileUtil::IOF
parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)),
perms(perms_) {}
+RealVfsFile::~RealVfsFile() = default;
+
std::string RealVfsFile::GetName() const {
return path_components.back();
}
-size_t RealVfsFile::GetSize() const {
+std::size_t RealVfsFile::GetSize() const {
return backing->GetSize();
}
-bool RealVfsFile::Resize(size_t new_size) {
+bool RealVfsFile::Resize(std::size_t new_size) {
return backing->Resize(new_size);
}
@@ -243,13 +247,13 @@ bool RealVfsFile::IsReadable() const {
return (perms & Mode::ReadWrite) != 0;
}
-size_t RealVfsFile::Read(u8* data, size_t length, size_t offset) const {
+std::size_t RealVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
if (!backing->Seek(offset, SEEK_SET))
return 0;
return backing->ReadBytes(data, length);
}
-size_t RealVfsFile::Write(const u8* data, size_t length, size_t offset) {
+std::size_t RealVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) {
if (!backing->Seek(offset, SEEK_SET))
return 0;
return backing->WriteBytes(data, length);
@@ -312,6 +316,8 @@ RealVfsDirectory::RealVfsDirectory(RealVfsFilesystem& base_, const std::string&
FileUtil::CreateDir(path);
}
+RealVfsDirectory::~RealVfsDirectory() = default;
+
std::shared_ptr<VfsFile> RealVfsDirectory::GetFileRelative(std::string_view path) const {
const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path));
if (!FileUtil::Exists(full_path) || FileUtil::IsDirectory(full_path))
diff --git a/src/core/file_sys/vfs_real.h b/src/core/file_sys/vfs_real.h
index 989803d43..681c43e82 100644
--- a/src/core/file_sys/vfs_real.h
+++ b/src/core/file_sys/vfs_real.h
@@ -6,15 +6,19 @@
#include <string_view>
#include <boost/container/flat_map.hpp>
-#include "common/file_util.h"
#include "core/file_sys/mode.h"
#include "core/file_sys/vfs.h"
+namespace FileUtil {
+class IOFile;
+}
+
namespace FileSys {
class RealVfsFilesystem : public VfsFilesystem {
public:
RealVfsFilesystem();
+ ~RealVfsFilesystem() override;
std::string GetName() const override;
bool IsReadable() const override;
@@ -40,21 +44,23 @@ class RealVfsFile : public VfsFile {
friend class RealVfsDirectory;
friend class RealVfsFilesystem;
- RealVfsFile(RealVfsFilesystem& base, std::shared_ptr<FileUtil::IOFile> backing,
- const std::string& path, Mode perms = Mode::Read);
-
public:
+ ~RealVfsFile() override;
+
std::string GetName() const override;
- size_t GetSize() const override;
- bool Resize(size_t new_size) override;
+ std::size_t GetSize() const override;
+ bool Resize(std::size_t new_size) override;
std::shared_ptr<VfsDirectory> GetContainingDirectory() const override;
bool IsWritable() const override;
bool IsReadable() const override;
- size_t Read(u8* data, size_t length, size_t offset) const override;
- size_t Write(const u8* data, size_t length, size_t offset) override;
+ std::size_t Read(u8* data, std::size_t length, std::size_t offset) const override;
+ std::size_t Write(const u8* data, std::size_t length, std::size_t offset) override;
bool Rename(std::string_view name) override;
private:
+ RealVfsFile(RealVfsFilesystem& base, std::shared_ptr<FileUtil::IOFile> backing,
+ const std::string& path, Mode perms = Mode::Read);
+
bool Close();
RealVfsFilesystem& base;
@@ -70,9 +76,9 @@ private:
class RealVfsDirectory : public VfsDirectory {
friend class RealVfsFilesystem;
- RealVfsDirectory(RealVfsFilesystem& base, const std::string& path, Mode perms = Mode::Read);
-
public:
+ ~RealVfsDirectory() override;
+
std::shared_ptr<VfsFile> GetFileRelative(std::string_view path) const override;
std::shared_ptr<VfsDirectory> GetDirectoryRelative(std::string_view path) const override;
std::shared_ptr<VfsFile> GetFile(std::string_view name) const override;
@@ -97,6 +103,8 @@ protected:
bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
private:
+ RealVfsDirectory(RealVfsFilesystem& base, const std::string& path, Mode perms = Mode::Read);
+
template <typename T, typename R>
std::vector<std::shared_ptr<R>> IterateEntries() const;
diff --git a/src/core/file_sys/vfs_vector.cpp b/src/core/file_sys/vfs_vector.cpp
index 98e7c4598..ec7f735b5 100644
--- a/src/core/file_sys/vfs_vector.cpp
+++ b/src/core/file_sys/vfs_vector.cpp
@@ -13,6 +13,8 @@ VectorVfsDirectory::VectorVfsDirectory(std::vector<VirtualFile> files_,
: files(std::move(files_)), dirs(std::move(dirs_)), parent(std::move(parent_)),
name(std::move(name_)) {}
+VectorVfsDirectory::~VectorVfsDirectory() = default;
+
std::vector<std::shared_ptr<VfsFile>> VectorVfsDirectory::GetFiles() const {
return files;
}
diff --git a/src/core/file_sys/vfs_vector.h b/src/core/file_sys/vfs_vector.h
index 179f62e4b..cba44a7a6 100644
--- a/src/core/file_sys/vfs_vector.h
+++ b/src/core/file_sys/vfs_vector.h
@@ -15,6 +15,7 @@ public:
explicit VectorVfsDirectory(std::vector<VirtualFile> files = {},
std::vector<VirtualDir> dirs = {}, std::string name = "",
VirtualDir parent = nullptr);
+ ~VectorVfsDirectory() override;
std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override;
diff --git a/src/core/file_sys/xts_archive.cpp b/src/core/file_sys/xts_archive.cpp
index 552835738..b2b164368 100644
--- a/src/core/file_sys/xts_archive.cpp
+++ b/src/core/file_sys/xts_archive.cpp
@@ -10,6 +10,7 @@
#include <mbedtls/md.h>
#include <mbedtls/sha256.h>
#include "common/assert.h"
+#include "common/file_util.h"
#include "common/hex_util.h"
#include "common/logging/log.h"
#include "core/crypto/aes_util.h"
@@ -24,14 +25,11 @@ namespace FileSys {
constexpr u64 NAX_HEADER_PADDING_SIZE = 0x4000;
template <typename SourceData, typename SourceKey, typename Destination>
-static bool CalculateHMAC256(Destination* out, const SourceKey* key, size_t key_length,
- const SourceData* data, size_t data_length) {
+static bool CalculateHMAC256(Destination* out, const SourceKey* key, std::size_t key_length,
+ const SourceData* data, std::size_t data_length) {
mbedtls_md_context_t context;
mbedtls_md_init(&context);
- const auto key_f = reinterpret_cast<const u8*>(key);
- const std::vector<u8> key_v(key_f, key_f + key_length);
-
if (mbedtls_md_setup(&context, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1) ||
mbedtls_md_hmac_starts(&context, reinterpret_cast<const u8*>(key), key_length) ||
mbedtls_md_hmac_update(&context, reinterpret_cast<const u8*>(data), data_length) ||
@@ -44,7 +42,7 @@ static bool CalculateHMAC256(Destination* out, const SourceKey* key, size_t key_
return true;
}
-NAX::NAX(VirtualFile file_) : file(std::move(file_)), header(std::make_unique<NAXHeader>()) {
+NAX::NAX(VirtualFile file_) : header(std::make_unique<NAXHeader>()), file(std::move(file_)) {
std::string path = FileUtil::SanitizePath(file->GetFullPath());
static const std::regex nax_path_regex("/registered/(000000[0-9A-F]{2})/([0-9A-F]{32})\\.nca",
std::regex_constants::ECMAScript |
@@ -64,13 +62,15 @@ NAX::NAX(VirtualFile file_) : file(std::move(file_)), header(std::make_unique<NA
}
NAX::NAX(VirtualFile file_, std::array<u8, 0x10> nca_id)
- : file(std::move(file_)), header(std::make_unique<NAXHeader>()) {
+ : header(std::make_unique<NAXHeader>()), file(std::move(file_)) {
Core::Crypto::SHA256Hash hash{};
mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0);
status = Parse(fmt::format("/registered/000000{:02X}/{}.nca", hash[0],
Common::HexArrayToString(nca_id, false)));
}
+NAX::~NAX() = default;
+
Loader::ResultStatus NAX::Parse(std::string_view path) {
if (file->ReadObject(header.get()) != sizeof(NAXHeader))
return Loader::ResultStatus::ErrorBadNAXHeader;
@@ -90,7 +90,7 @@ Loader::ResultStatus NAX::Parse(std::string_view path) {
const auto enc_keys = header->key_area;
- size_t i = 0;
+ std::size_t i = 0;
for (; i < sd_keys.size(); ++i) {
std::array<Core::Crypto::Key128, 2> nax_keys{};
if (!CalculateHMAC256(nax_keys.data(), sd_keys[i].data(), 0x10, std::string(path).c_str(),
@@ -98,7 +98,7 @@ Loader::ResultStatus NAX::Parse(std::string_view path) {
return Loader::ResultStatus::ErrorNAXKeyHMACFailed;
}
- for (size_t j = 0; j < nax_keys.size(); ++j) {
+ for (std::size_t j = 0; j < nax_keys.size(); ++j) {
Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(nax_keys[j],
Core::Crypto::Mode::ECB);
cipher.Transcode(enc_keys[j].data(), 0x10, header->key_area[j].data(),
@@ -137,9 +137,9 @@ VirtualFile NAX::GetDecrypted() const {
return dec_file;
}
-std::shared_ptr<NCA> NAX::AsNCA() const {
+std::unique_ptr<NCA> NAX::AsNCA() const {
if (type == NAXContentType::NCA)
- return std::make_shared<NCA>(GetDecrypted());
+ return std::make_unique<NCA>(GetDecrypted());
return nullptr;
}
diff --git a/src/core/file_sys/xts_archive.h b/src/core/file_sys/xts_archive.h
index 55d2154a6..8fedd8585 100644
--- a/src/core/file_sys/xts_archive.h
+++ b/src/core/file_sys/xts_archive.h
@@ -33,12 +33,13 @@ class NAX : public ReadOnlyVfsDirectory {
public:
explicit NAX(VirtualFile file);
explicit NAX(VirtualFile file, std::array<u8, 0x10> nca_id);
+ ~NAX() override;
Loader::ResultStatus GetStatus() const;
VirtualFile GetDecrypted() const;
- std::shared_ptr<NCA> AsNCA() const;
+ std::unique_ptr<NCA> AsNCA() const;
NAXContentType GetContentType() const;
@@ -60,7 +61,7 @@ private:
VirtualFile file;
Loader::ResultStatus status;
- NAXContentType type;
+ NAXContentType type{};
VirtualFile dec_file;