diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/core/file_sys/card_image.cpp | 4 | ||||
-rw-r--r-- | src/core/file_sys/ips_layer.cpp | 88 | ||||
-rw-r--r-- | src/core/file_sys/ips_layer.h | 15 | ||||
-rw-r--r-- | src/core/file_sys/patch_manager.cpp | 163 | ||||
-rw-r--r-- | src/core/file_sys/patch_manager.h | 19 | ||||
-rw-r--r-- | src/core/file_sys/romfs_factory.cpp | 51 | ||||
-rw-r--r-- | src/core/file_sys/submission_package.cpp | 169 | ||||
-rw-r--r-- | src/core/file_sys/submission_package.h | 5 | ||||
-rw-r--r-- | src/core/hle/service/aoc/aoc_u.cpp | 93 | ||||
-rw-r--r-- | src/core/hle/service/aoc/aoc_u.h | 4 | ||||
-rw-r--r-- | src/core/hle/service/filesystem/fsp_srv.cpp | 2 | ||||
-rw-r--r-- | src/core/hle/service/lbl/lbl.cpp | 38 | ||||
-rw-r--r-- | src/core/loader/deconstructed_rom_directory.cpp | 3 | ||||
-rw-r--r-- | src/core/loader/nso.cpp | 18 | ||||
-rw-r--r-- | src/core/loader/nso.h | 4 |
16 files changed, 520 insertions, 158 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 23fd6e920..a98dbb9ab 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -34,6 +34,8 @@ add_library(core STATIC file_sys/errors.h file_sys/fsmitm_romfsbuild.cpp file_sys/fsmitm_romfsbuild.h + file_sys/ips_layer.cpp + file_sys/ips_layer.h file_sys/mode.h file_sys/nca_metadata.cpp file_sys/nca_metadata.h diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index edfc1bbd4..8f5142a07 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp @@ -20,7 +20,9 @@ namespace FileSys { constexpr std::array<const char*, 0x4> partition_names = {"update", "normal", "secure", "logo"}; -XCI::XCI(VirtualFile file_) : file(std::move(file_)), partitions(0x4) { +XCI::XCI(VirtualFile file_) + : file(std::move(file_)), program_nca_status{Loader::ResultStatus::ErrorXCIMissingProgramNCA}, + partitions(0x4) { if (file->ReadObject(&header) != sizeof(GamecardHeader)) { status = Loader::ResultStatus::ErrorBadXCIHeader; return; diff --git a/src/core/file_sys/ips_layer.cpp b/src/core/file_sys/ips_layer.cpp new file mode 100644 index 000000000..df933ee36 --- /dev/null +++ b/src/core/file_sys/ips_layer.cpp @@ -0,0 +1,88 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/assert.h" +#include "common/swap.h" +#include "core/file_sys/ips_layer.h" +#include "core/file_sys/vfs_vector.h" + +namespace FileSys { + +enum class IPSFileType { + IPS, + IPS32, + Error, +}; + +static IPSFileType IdentifyMagic(const std::vector<u8>& magic) { + if (magic.size() != 5) + return IPSFileType::Error; + if (magic == std::vector<u8>{'P', 'A', 'T', 'C', 'H'}) + return IPSFileType::IPS; + if (magic == std::vector<u8>{'I', 'P', 'S', '3', '2'}) + return IPSFileType::IPS32; + return IPSFileType::Error; +} + +VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) { + if (in == nullptr || ips == nullptr) + return nullptr; + + const auto type = IdentifyMagic(ips->ReadBytes(0x5)); + if (type == IPSFileType::Error) + return nullptr; + + auto in_data = in->ReadAllBytes(); + + std::vector<u8> temp(type == IPSFileType::IPS ? 3 : 4); + u64 offset = 5; // After header + while (ips->Read(temp.data(), temp.size(), offset) == temp.size()) { + offset += temp.size(); + if (type == IPSFileType::IPS32 && temp == std::vector<u8>{'E', 'E', 'O', 'F'} || + type == IPSFileType::IPS && temp == std::vector<u8>{'E', 'O', 'F'}) { + break; + } + + u32 real_offset{}; + if (type == IPSFileType::IPS32) + real_offset = (temp[0] << 24) | (temp[1] << 16) | (temp[2] << 8) | temp[3]; + else + real_offset = (temp[0] << 16) | (temp[1] << 8) | temp[2]; + + u16 data_size{}; + if (ips->ReadObject(&data_size, offset) != sizeof(u16)) + return nullptr; + data_size = Common::swap16(data_size); + offset += sizeof(u16); + + if (data_size == 0) { // RLE + u16 rle_size{}; + if (ips->ReadObject(&rle_size, offset) != sizeof(u16)) + return nullptr; + rle_size = Common::swap16(data_size); + offset += sizeof(u16); + + const auto data = ips->ReadByte(offset++); + if (data == boost::none) + return nullptr; + + if (real_offset + rle_size > in_data.size()) + rle_size = in_data.size() - real_offset; + std::memset(in_data.data() + real_offset, data.get(), rle_size); + } else { // Standard Patch + auto read = data_size; + if (real_offset + read > in_data.size()) + read = in_data.size() - real_offset; + if (ips->Read(in_data.data() + real_offset, read, offset) != data_size) + return nullptr; + offset += data_size; + } + } + + if (temp != std::vector<u8>{'E', 'E', 'O', 'F'} && temp != std::vector<u8>{'E', 'O', 'F'}) + return nullptr; + return std::make_shared<VectorVfsFile>(in_data, in->GetName(), in->GetContainingDirectory()); +} + +} // namespace FileSys diff --git a/src/core/file_sys/ips_layer.h b/src/core/file_sys/ips_layer.h new file mode 100644 index 000000000..81c163494 --- /dev/null +++ b/src/core/file_sys/ips_layer.h @@ -0,0 +1,15 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <memory> + +#include "core/file_sys/vfs.h" + +namespace FileSys { + +VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips); + +} // namespace FileSys diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 4b3b5e665..539698f6e 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -2,22 +2,36 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include <algorithm> #include <array> #include <cstddef> +#include <cstring> +#include "common/hex_util.h" #include "common/logging/log.h" #include "core/file_sys/content_archive.h" #include "core/file_sys/control_metadata.h" +#include "core/file_sys/ips_layer.h" #include "core/file_sys/patch_manager.h" #include "core/file_sys/registered_cache.h" #include "core/file_sys/romfs.h" #include "core/file_sys/vfs_layered.h" +#include "core/file_sys/vfs_vector.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/loader/loader.h" namespace FileSys { constexpr u64 SINGLE_BYTE_MODULUS = 0x100; +constexpr u64 DLC_BASE_TITLE_ID_MASK = 0xFFFFFFFFFFFFE000; + +struct NSOBuildHeader { + u32_le magic; + INSERT_PADDING_BYTES(0x3C); + std::array<u8, 0x20> build_id; + INSERT_PADDING_BYTES(0xA0); +}; +static_assert(sizeof(NSOBuildHeader) == 0x100, "NSOBuildHeader has incorrect size."); std::string FormatTitleVersion(u32 version, TitleVersionFormat format) { std::array<u8, sizeof(u32)> bytes{}; @@ -32,15 +46,6 @@ std::string FormatTitleVersion(u32 version, TitleVersionFormat format) { return fmt::format("v{}.{}.{}", bytes[3], bytes[2], bytes[1]); } -constexpr std::array<const char*, 2> PATCH_TYPE_NAMES{ - "Update", - "LayeredFS", -}; - -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; @@ -68,6 +73,79 @@ VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const { return exefs; } +static std::vector<VirtualFile> CollectIPSPatches(const std::vector<VirtualDir>& patch_dirs, + const std::string& build_id) { + std::vector<VirtualFile> ips; + ips.reserve(patch_dirs.size()); + for (const auto& subdir : patch_dirs) { + auto exefs_dir = subdir->GetSubdirectory("exefs"); + if (exefs_dir != nullptr) { + for (const auto& file : exefs_dir->GetFiles()) { + if (file->GetExtension() != "ips") + continue; + auto name = file->GetName(); + const auto p1 = name.substr(0, name.find('.')); + const auto this_build_id = p1.substr(0, p1.find_last_not_of('0') + 1); + + if (build_id == this_build_id) + ips.push_back(file); + } + } + } + + return ips; +} + +std::vector<u8> PatchManager::PatchNSO(const std::vector<u8>& nso) const { + if (nso.size() < 0x100) + return nso; + + NSOBuildHeader header; + std::memcpy(&header, nso.data(), sizeof(NSOBuildHeader)); + + if (header.magic != Common::MakeMagic('N', 'S', 'O', '0')) + return nso; + + const auto build_id_raw = Common::HexArrayToString(header.build_id); + const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1); + + LOG_INFO(Loader, "Patching NSO for build_id={}", build_id); + + const auto load_dir = Service::FileSystem::GetModificationLoadRoot(title_id); + auto patch_dirs = load_dir->GetSubdirectories(); + std::sort(patch_dirs.begin(), patch_dirs.end(), + [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); }); + const auto ips = CollectIPSPatches(patch_dirs, build_id); + + auto out = nso; + for (const auto& ips_file : ips) { + LOG_INFO(Loader, " - Appling IPS patch from mod \"{}\"", + ips_file->GetContainingDirectory()->GetParentDirectory()->GetName()); + const auto patched = PatchIPS(std::make_shared<VectorVfsFile>(out), ips_file); + if (patched != nullptr) + out = patched->ReadAllBytes(); + } + + if (out.size() < 0x100) + return nso; + std::memcpy(out.data(), &header, sizeof(NSOBuildHeader)); + return out; +} + +bool PatchManager::HasNSOPatch(const std::array<u8, 32>& build_id_) const { + const auto build_id_raw = Common::HexArrayToString(build_id_); + const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1); + + LOG_INFO(Loader, "Querying NSO patch existence for build_id={}", build_id); + + const auto load_dir = Service::FileSystem::GetModificationLoadRoot(title_id); + auto patch_dirs = load_dir->GetSubdirectories(); + std::sort(patch_dirs.begin(), patch_dirs.end(), + [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); }); + + return !CollectIPSPatches(patch_dirs, build_id).empty(); +} + static void ApplyLayeredFS(VirtualFile& romfs, u64 title_id, ContentRecordType type) { const auto load_dir = Service::FileSystem::GetModificationLoadRoot(title_id); if (type != ContentRecordType::Program || load_dir == nullptr || load_dir->GetSize() <= 0) { @@ -135,31 +213,80 @@ VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset, return romfs; } -std::map<PatchType, std::string> PatchManager::GetPatchVersionNames() const { - std::map<PatchType, std::string> out; +static void AppendCommaIfNotEmpty(std::string& to, const std::string& with) { + if (to.empty()) + to += with; + else + to += ", " + with; +} + +static bool IsDirValidAndNonEmpty(const VirtualDir& dir) { + return dir != nullptr && (!dir->GetFiles().empty() || !dir->GetSubdirectories().empty()); +} + +std::map<std::string, std::string, std::less<>> PatchManager::GetPatchVersionNames() const { + std::map<std::string, std::string, std::less<>> out; const auto installed = Service::FileSystem::GetUnionContents(); + // Game Updates 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(); + out.insert_or_assign("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] = ""; + out.insert_or_assign("Update", ""); } else { - out[PatchType::Update] = - FormatTitleVersion(meta_ver.get(), TitleVersionFormat::ThreeElements); + out.insert_or_assign( + "Update", + FormatTitleVersion(meta_ver.get(), TitleVersionFormat::ThreeElements)); } } } - const auto lfs_dir = Service::FileSystem::GetModificationLoadRoot(title_id); - if (lfs_dir != nullptr && lfs_dir->GetSize() > 0) - out.insert_or_assign(PatchType::LayeredFS, ""); + // General Mods (LayeredFS and IPS) + const auto mod_dir = Service::FileSystem::GetModificationLoadRoot(title_id); + if (mod_dir != nullptr && mod_dir->GetSize() > 0) { + for (const auto& mod : mod_dir->GetSubdirectories()) { + std::string types; + if (IsDirValidAndNonEmpty(mod->GetSubdirectory("exefs"))) + AppendCommaIfNotEmpty(types, "IPS"); + if (IsDirValidAndNonEmpty(mod->GetSubdirectory("romfs"))) + AppendCommaIfNotEmpty(types, "LayeredFS"); + + if (types.empty()) + continue; + + out.insert_or_assign(mod->GetName(), types); + } + } + + // DLC + const auto dlc_entries = installed->ListEntriesFilter(TitleType::AOC, ContentRecordType::Data); + std::vector<RegisteredCacheEntry> dlc_match; + dlc_match.reserve(dlc_entries.size()); + std::copy_if(dlc_entries.begin(), dlc_entries.end(), std::back_inserter(dlc_match), + [this, &installed](const RegisteredCacheEntry& entry) { + return (entry.title_id & DLC_BASE_TITLE_ID_MASK) == title_id && + installed->GetEntry(entry)->GetStatus() == + Loader::ResultStatus::Success; + }); + if (!dlc_match.empty()) { + // Ensure sorted so DLC IDs show in order. + std::sort(dlc_match.begin(), dlc_match.end()); + + std::string list; + for (size_t i = 0; i < dlc_match.size() - 1; ++i) + list += fmt::format("{}, ", dlc_match[i].title_id & 0x7FF); + + list += fmt::format("{}", dlc_match.back().title_id & 0x7FF); + + out.insert_or_assign("DLC", std::move(list)); + } return out; } diff --git a/src/core/file_sys/patch_manager.h b/src/core/file_sys/patch_manager.h index 464f17515..6a864ec43 100644 --- a/src/core/file_sys/patch_manager.h +++ b/src/core/file_sys/patch_manager.h @@ -24,13 +24,6 @@ enum class TitleVersionFormat : u8 { std::string FormatTitleVersion(u32 version, TitleVersionFormat format = TitleVersionFormat::ThreeElements); -enum class PatchType { - Update, - LayeredFS, -}; - -std::string FormatPatchTypeName(PatchType type); - // A centralized class to manage patches to games. class PatchManager { public: @@ -41,6 +34,14 @@ public: // - Game Updates VirtualDir PatchExeFS(VirtualDir exefs) const; + // Currently tracked NSO patches: + // - IPS + std::vector<u8> PatchNSO(const std::vector<u8>& nso) const; + + // Checks to see if PatchNSO() will have any effect given the NSO's build ID. + // Used to prevent expensive copies in NSO loader. + bool HasNSOPatch(const std::array<u8, 0x20>& build_id) const; + // Currently tracked RomFS patches: // - Game Updates // - LayeredFS @@ -48,8 +49,8 @@ public: 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; + // i.e. Update 3.2.2 will return {"Update", "3.2.2"} + std::map<std::string, std::string, std::less<>> 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. diff --git a/src/core/file_sys/romfs_factory.cpp b/src/core/file_sys/romfs_factory.cpp index d027a8d59..4994c2532 100644 --- a/src/core/file_sys/romfs_factory.cpp +++ b/src/core/file_sys/romfs_factory.cpp @@ -39,36 +39,35 @@ ResultVal<VirtualFile> RomFSFactory::OpenCurrentProcess() { } ResultVal<VirtualFile> RomFSFactory::Open(u64 title_id, StorageId storage, ContentRecordType type) { + std::shared_ptr<NCA> res; + switch (storage) { - case StorageId::NandSystem: { - const auto res = Service::FileSystem::GetSystemNANDContents()->GetEntry(title_id, type); - if (res == nullptr) { - // TODO(DarkLordZach): Find the right error code to use here - return ResultCode(-1); - } - const auto romfs = res->GetRomFS(); - if (romfs == nullptr) { - // TODO(DarkLordZach): Find the right error code to use here - return ResultCode(-1); - } - return MakeResult<VirtualFile>(romfs); - } - case StorageId::NandUser: { - const auto res = Service::FileSystem::GetUserNANDContents()->GetEntry(title_id, type); - if (res == nullptr) { - // TODO(DarkLordZach): Find the right error code to use here - return ResultCode(-1); - } - const auto romfs = res->GetRomFS(); - if (romfs == nullptr) { - // TODO(DarkLordZach): Find the right error code to use here - return ResultCode(-1); - } - return MakeResult<VirtualFile>(romfs); - } + case StorageId::None: + res = Service::FileSystem::GetUnionContents()->GetEntry(title_id, type); + break; + case StorageId::NandSystem: + res = Service::FileSystem::GetSystemNANDContents()->GetEntry(title_id, type); + break; + case StorageId::NandUser: + res = Service::FileSystem::GetUserNANDContents()->GetEntry(title_id, type); + break; + case StorageId::SdCard: + res = Service::FileSystem::GetSDMCContents()->GetEntry(title_id, type); + break; default: UNIMPLEMENTED_MSG("Unimplemented storage_id={:02X}", static_cast<u8>(storage)); } + + if (res == nullptr) { + // TODO(DarkLordZach): Find the right error code to use here + return ResultCode(-1); + } + const auto romfs = res->GetRomFS(); + if (romfs == nullptr) { + // TODO(DarkLordZach): Find the right error code to use here + return ResultCode(-1); + } + return MakeResult<VirtualFile>(romfs); } } // namespace FileSys diff --git a/src/core/file_sys/submission_package.cpp b/src/core/file_sys/submission_package.cpp index 11264878d..09bf077cd 100644 --- a/src/core/file_sys/submission_package.cpp +++ b/src/core/file_sys/submission_package.cpp @@ -18,6 +18,39 @@ #include "core/loader/loader.h" namespace FileSys { +namespace { +void SetTicketKeys(const std::vector<VirtualFile>& files) { + Core::Crypto::KeyManager keys; + + for (const auto& ticket_file : files) { + if (ticket_file == nullptr) { + continue; + } + + if (ticket_file->GetExtension() != "tik") { + continue; + } + + if (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); + + // We get the name without the extension in order to create the rights ID. + std::string name_only(ticket_file->GetName()); + name_only.erase(name_only.size() - 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]); + } +} +} // Anonymous namespace + NSP::NSP(VirtualFile file_) : file(std::move(file_)), status{Loader::ResultStatus::Success}, pfs(std::make_shared<PartitionFilesystem>(file)) { @@ -26,83 +59,16 @@ NSP::NSP(VirtualFile file_) return; } + const auto files = pfs->GetFiles(); + 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; + InitializeExeFSAndRomFS(files); 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; - } - } - } + SetTicketKeys(files); + ReadNCAs(files); } NSP::~NSP() = default; @@ -242,4 +208,63 @@ VirtualDir NSP::GetParentDirectory() const { bool NSP::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { return false; } + +void NSP::InitializeExeFSAndRomFS(const std::vector<VirtualFile>& files) { + exefs = pfs; + + const auto romfs_iter = std::find_if(files.begin(), files.end(), [](const VirtualFile& file) { + return file->GetName().rfind(".romfs") != std::string::npos; + }); + + if (romfs_iter == files.end()) { + return; + } + + romfs = *romfs_iter; +} + +void NSP::ReadNCAs(const std::vector<VirtualFile>& files) { + for (const auto& outer_file : files) { + if (outer_file->GetName().substr(outer_file->GetName().size() - 9) != ".cnmt.nca") { + continue; + } + + 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; + } + } +} } // namespace FileSys diff --git a/src/core/file_sys/submission_package.h b/src/core/file_sys/submission_package.h index e85a2b76e..da3dc5e9f 100644 --- a/src/core/file_sys/submission_package.h +++ b/src/core/file_sys/submission_package.h @@ -59,9 +59,12 @@ protected: bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; private: + void InitializeExeFSAndRomFS(const std::vector<VirtualFile>& files); + void ReadNCAs(const std::vector<VirtualFile>& files); + VirtualFile file; - bool extracted; + bool extracted = false; Loader::ResultStatus status; std::map<u64, Loader::ResultStatus> program_status; diff --git a/src/core/hle/service/aoc/aoc_u.cpp b/src/core/hle/service/aoc/aoc_u.cpp index d9eeac9ec..79580bcd9 100644 --- a/src/core/hle/service/aoc/aoc_u.cpp +++ b/src/core/hle/service/aoc/aoc_u.cpp @@ -2,22 +2,57 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include <algorithm> +#include <numeric> +#include <vector> #include "common/logging/log.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/registered_cache.h" #include "core/hle/ipc_helpers.h" +#include "core/hle/kernel/process.h" #include "core/hle/service/aoc/aoc_u.h" +#include "core/hle/service/filesystem/filesystem.h" +#include "core/loader/loader.h" namespace Service::AOC { -AOC_U::AOC_U() : ServiceFramework("aoc:u") { +constexpr u64 DLC_BASE_TITLE_ID_MASK = 0xFFFFFFFFFFFFE000; +constexpr u64 DLC_BASE_TO_AOC_ID_MASK = 0x1000; + +static bool CheckAOCTitleIDMatchesBase(u64 base, u64 aoc) { + return (aoc & DLC_BASE_TITLE_ID_MASK) == base; +} + +static std::vector<u64> AccumulateAOCTitleIDs() { + std::vector<u64> add_on_content; + const auto rcu = FileSystem::GetUnionContents(); + const auto list = + rcu->ListEntriesFilter(FileSys::TitleType::AOC, FileSys::ContentRecordType::Data); + std::transform(list.begin(), list.end(), std::back_inserter(add_on_content), + [](const FileSys::RegisteredCacheEntry& rce) { return rce.title_id; }); + add_on_content.erase( + std::remove_if( + add_on_content.begin(), add_on_content.end(), + [&rcu](u64 tid) { + return rcu->GetEntry(tid, FileSys::ContentRecordType::Data)->GetStatus() != + Loader::ResultStatus::Success; + }), + add_on_content.end()); + return add_on_content; +} + +AOC_U::AOC_U() : ServiceFramework("aoc:u"), add_on_content(AccumulateAOCTitleIDs()) { static const FunctionInfo functions[] = { {0, nullptr, "CountAddOnContentByApplicationId"}, {1, nullptr, "ListAddOnContentByApplicationId"}, {2, &AOC_U::CountAddOnContent, "CountAddOnContent"}, {3, &AOC_U::ListAddOnContent, "ListAddOnContent"}, {4, nullptr, "GetAddOnContentBaseIdByApplicationId"}, - {5, nullptr, "GetAddOnContentBaseId"}, + {5, &AOC_U::GetAddOnContentBaseId, "GetAddOnContentBaseId"}, {6, nullptr, "PrepareAddOnContentByApplicationId"}, - {7, nullptr, "PrepareAddOnContent"}, + {7, &AOC_U::PrepareAddOnContent, "PrepareAddOnContent"}, {8, nullptr, "GetAddOnContentListChangedEvent"}, }; RegisterHandlers(functions); @@ -28,15 +63,59 @@ AOC_U::~AOC_U() = default; void AOC_U::CountAddOnContent(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 4}; rb.Push(RESULT_SUCCESS); - rb.Push<u64>(0); - LOG_WARNING(Service_AOC, "(STUBBED) called"); + + const auto current = Core::System::GetInstance().CurrentProcess()->GetTitleID(); + rb.Push<u32>(std::count_if(add_on_content.begin(), add_on_content.end(), [¤t](u64 tid) { + return (tid & DLC_BASE_TITLE_ID_MASK) == current; + })); } void AOC_U::ListAddOnContent(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + + const auto offset = rp.PopRaw<u32>(); + auto count = rp.PopRaw<u32>(); + + const auto current = Core::System::GetInstance().CurrentProcess()->GetTitleID(); + + std::vector<u32> out; + for (size_t i = 0; i < add_on_content.size(); ++i) { + if ((add_on_content[i] & DLC_BASE_TITLE_ID_MASK) == current) + out.push_back(static_cast<u32>(add_on_content[i] & 0x7FF)); + } + + if (out.size() < offset) { + IPC::ResponseBuilder rb{ctx, 2}; + // TODO(DarkLordZach): Find the correct error code. + rb.Push(ResultCode(-1)); + return; + } + + count = std::min<size_t>(out.size() - offset, count); + std::rotate(out.begin(), out.begin() + offset, out.end()); + out.resize(count); + + ctx.WriteBuffer(out); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(RESULT_SUCCESS); +} + +void AOC_U::GetAddOnContentBaseId(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 4}; rb.Push(RESULT_SUCCESS); - rb.Push<u64>(0); - LOG_WARNING(Service_AOC, "(STUBBED) called"); + rb.Push(Core::System::GetInstance().CurrentProcess()->GetTitleID() | DLC_BASE_TO_AOC_ID_MASK); +} + +void AOC_U::PrepareAddOnContent(Kernel::HLERequestContext& ctx) { + IPC::RequestParser rp{ctx}; + + const auto aoc_id = rp.PopRaw<u32>(); + + LOG_WARNING(Service_AOC, "(STUBBED) called with aoc_id={:08X}", aoc_id); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(RESULT_SUCCESS); } void InstallInterfaces(SM::ServiceManager& service_manager) { diff --git a/src/core/hle/service/aoc/aoc_u.h b/src/core/hle/service/aoc/aoc_u.h index 29ce8f488..b3c7cab7a 100644 --- a/src/core/hle/service/aoc/aoc_u.h +++ b/src/core/hle/service/aoc/aoc_u.h @@ -16,6 +16,10 @@ public: private: void CountAddOnContent(Kernel::HLERequestContext& ctx); void ListAddOnContent(Kernel::HLERequestContext& ctx); + void GetAddOnContentBaseId(Kernel::HLERequestContext& ctx); + void PrepareAddOnContent(Kernel::HLERequestContext& ctx); + + std::vector<u64> add_on_content; }; /// Registers all AOC services with the specified service manager. diff --git a/src/core/hle/service/filesystem/fsp_srv.cpp b/src/core/hle/service/filesystem/fsp_srv.cpp index cabaf5a55..d5dced429 100644 --- a/src/core/hle/service/filesystem/fsp_srv.cpp +++ b/src/core/hle/service/filesystem/fsp_srv.cpp @@ -468,6 +468,7 @@ FSP_SRV::FSP_SRV() : ServiceFramework("fsp-srv") { {80, nullptr, "OpenSaveDataMetaFile"}, {81, nullptr, "OpenSaveDataTransferManager"}, {82, nullptr, "OpenSaveDataTransferManagerVersion2"}, + {83, nullptr, "OpenSaveDataTransferProhibiterForCloudBackUp"}, {100, nullptr, "OpenImageDirectoryFileSystem"}, {110, nullptr, "OpenContentStorageFileSystem"}, {200, &FSP_SRV::OpenDataStorageByCurrentProcess, "OpenDataStorageByCurrentProcess"}, @@ -495,6 +496,7 @@ FSP_SRV::FSP_SRV() : ServiceFramework("fsp-srv") { {613, nullptr, "VerifySaveDataFileSystemBySaveDataSpaceId"}, {614, nullptr, "CorruptSaveDataFileSystemBySaveDataSpaceId"}, {615, nullptr, "QuerySaveDataInternalStorageTotalSize"}, + {616, nullptr, "GetSaveDataCommitId"}, {620, nullptr, "SetSdCardEncryptionSeed"}, {630, nullptr, "SetSdCardAccessibility"}, {631, nullptr, "IsSdCardAccessible"}, diff --git a/src/core/hle/service/lbl/lbl.cpp b/src/core/hle/service/lbl/lbl.cpp index 8fc8b1057..7321584e1 100644 --- a/src/core/hle/service/lbl/lbl.cpp +++ b/src/core/hle/service/lbl/lbl.cpp @@ -21,29 +21,29 @@ public: {0, nullptr, "Unknown1"}, {1, nullptr, "Unknown2"}, {2, nullptr, "Unknown3"}, - {3, nullptr, "Unknown4"}, - {4, nullptr, "Unknown5"}, - {5, nullptr, "Unknown6"}, + {3, nullptr, "GetCurrentBacklightLevel"}, + {4, nullptr, "Unknown4"}, + {5, nullptr, "GetAlsComputedBacklightLevel"}, {6, nullptr, "TurnOffBacklight"}, {7, nullptr, "TurnOnBacklight"}, {8, nullptr, "GetBacklightStatus"}, - {9, nullptr, "Unknown7"}, - {10, nullptr, "Unknown8"}, - {11, nullptr, "Unknown9"}, - {12, nullptr, "Unknown10"}, - {13, nullptr, "Unknown11"}, - {14, nullptr, "Unknown12"}, - {15, nullptr, "Unknown13"}, + {9, nullptr, "Unknown5"}, + {10, nullptr, "Unknown6"}, + {11, nullptr, "Unknown7"}, + {12, nullptr, "Unknown8"}, + {13, nullptr, "Unknown9"}, + {14, nullptr, "Unknown10"}, + {15, nullptr, "GetAutoBrightnessSetting"}, {16, nullptr, "ReadRawLightSensor"}, - {17, nullptr, "Unknown14"}, - {18, nullptr, "Unknown15"}, - {19, nullptr, "Unknown16"}, - {20, nullptr, "Unknown17"}, - {21, nullptr, "Unknown18"}, - {22, nullptr, "Unknown19"}, - {23, nullptr, "Unknown20"}, - {24, nullptr, "Unknown21"}, - {25, nullptr, "Unknown22"}, + {17, nullptr, "Unknown11"}, + {18, nullptr, "Unknown12"}, + {19, nullptr, "Unknown13"}, + {20, nullptr, "Unknown14"}, + {21, nullptr, "Unknown15"}, + {22, nullptr, "Unknown16"}, + {23, nullptr, "Unknown17"}, + {24, nullptr, "Unknown18"}, + {25, nullptr, "Unknown19"}, {26, &LBL::EnableVrMode, "EnableVrMode"}, {27, &LBL::DisableVrMode, "DisableVrMode"}, {28, &LBL::GetVrMode, "GetVrMode"}, diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp index c1824b9c3..9a86e5824 100644 --- a/src/core/loader/deconstructed_rom_directory.cpp +++ b/src/core/loader/deconstructed_rom_directory.cpp @@ -130,6 +130,7 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(Kernel::Process& process) } process.LoadFromMetadata(metadata); + const FileSys::PatchManager pm(metadata.GetTitleID()); // Load NSO modules const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress(); @@ -139,7 +140,7 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(Kernel::Process& process) const FileSys::VirtualFile module_file = dir->GetFile(module); if (module_file != nullptr) { const VAddr load_addr = next_load_addr; - next_load_addr = AppLoader_NSO::LoadModule(module_file, load_addr); + next_load_addr = AppLoader_NSO::LoadModule(module_file, load_addr, pm); LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr); // Register module with GDBStub GDBStub::RegisterModule(module, load_addr, next_load_addr - 1, false); diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index cbe2a3e53..2186b02af 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -10,6 +10,7 @@ #include "common/logging/log.h" #include "common/swap.h" #include "core/core.h" +#include "core/file_sys/patch_manager.h" #include "core/gdbstub/gdbstub.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" @@ -36,8 +37,7 @@ struct NsoHeader { INSERT_PADDING_WORDS(1); u8 flags; std::array<NsoSegmentHeader, 3> segments; // Text, RoData, Data (in that order) - u32_le bss_size; - INSERT_PADDING_BYTES(0x1c); + std::array<u8, 0x20> build_id; std::array<u32_le, 3> segments_compressed_size; bool IsSegmentCompressed(size_t segment_num) const { @@ -93,7 +93,8 @@ static constexpr u32 PageAlignSize(u32 size) { return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; } -VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base) { +VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base, + boost::optional<FileSys::PatchManager> pm) { if (file == nullptr) return {}; @@ -142,6 +143,17 @@ VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base) { const u32 image_size{PageAlignSize(static_cast<u32>(program_image.size()) + bss_size)}; program_image.resize(image_size); + // Apply patches if necessary + if (pm != boost::none && pm->HasNSOPatch(nso_header.build_id)) { + std::vector<u8> pi_header(program_image.size() + 0x100); + std::memcpy(pi_header.data(), &nso_header, sizeof(NsoHeader)); + std::memcpy(pi_header.data() + 0x100, program_image.data(), program_image.size()); + + pi_header = pm->PatchNSO(pi_header); + + std::memcpy(program_image.data(), pi_header.data() + 0x100, program_image.size()); + } + // Load codeset for current process codeset->name = file->GetName(); codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 7f142405b..05353d4d9 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -5,6 +5,7 @@ #pragma once #include "common/common_types.h" +#include "core/file_sys/patch_manager.h" #include "core/loader/linker.h" #include "core/loader/loader.h" @@ -26,7 +27,8 @@ public: return IdentifyType(file); } - static VAddr LoadModule(FileSys::VirtualFile file, VAddr load_base); + static VAddr LoadModule(FileSys::VirtualFile file, VAddr load_base, + boost::optional<FileSys::PatchManager> pm = boost::none); ResultStatus Load(Kernel::Process& process) override; }; |