From 8e900a301a1094d74f68f173c7dec67b12baec25 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 25 Aug 2018 19:03:45 -0400 Subject: file_sys: Add class to manage game patches Right now only includes Updates, but should eventually contain all of the other patches we need. --- src/core/file_sys/patch_manager.cpp | 90 +++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/core/file_sys/patch_manager.cpp (limited to 'src/core/file_sys/patch_manager.cpp') diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp new file mode 100644 index 000000000..697b8a4c9 --- /dev/null +++ b/src/core/file_sys/patch_manager.cpp @@ -0,0 +1,90 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/file_sys/patch_manager.h" +#include "core/file_sys/registered_cache.h" +#include "core/hle/service/filesystem/filesystem.h" + +namespace FileSys { + +union TitleVersion { + u32 version; + + struct { + u8 v_revision; + u8 v_micro; + u8 v_minor; + u8 v_major; + }; +}; + +std::string FormatTitleVersion(u32 version_, bool full) { + TitleVersion ver{}; + ver.version = version_; + + if (full) + return fmt::format("v{}.{}.{}.{}", ver.v_major, ver.v_minor, ver.v_minor, ver.v_revision); + return fmt::format("v{}.{}.{}", ver.v_major, ver.v_minor, ver.v_micro); +} + +constexpr std::array PATCH_TYPE_NAMES{ + "Update", +}; + +std::string FormatPatchTypeName(PatchType type) { + return PATCH_TYPE_NAMES.at(static_cast(type)); +} + +PatchManager::PatchManager(u64 title_id) : title_id(title_id) {} + +VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const { + 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) + exefs = update->GetExeFS(); + } + + return exefs; +} + +VirtualFile PatchManager::PatchRomFS(VirtualFile romfs) const { + 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, ContentRecordType::Program); + if (update != nullptr) { + const auto nca = std::make_shared(update, romfs); + if (nca->GetStatus() == Loader::ResultStatus::Success && nca->GetRomFS() != nullptr) + romfs = nca->GetRomFS(); + } + + return romfs; +} + +std::map PatchManager::GetPatchVersionNames() const { + std::map out; + const auto installed = Service::FileSystem::GetUnionContents(); + + const auto update_tid = GetUpdateTitleID(title_id); + const auto update_version = installed->GetEntryVersion(update_tid); + if (update_version != boost::none && + installed->HasEntry(update_tid, ContentRecordType::Program)) + out[PatchType::Update] = update_version.get(); + + return out; +} + +} // namespace FileSys -- cgit v1.2.3 From 9664ce255db09f4501db642c1e82d8cf8f274a22 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 26 Aug 2018 10:53:31 -0400 Subject: bktr: Fix missing includes and optimize style --- src/core/file_sys/patch_manager.cpp | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'src/core/file_sys/patch_manager.cpp') diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 697b8a4c9..5e853c2c0 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -8,24 +8,19 @@ namespace FileSys { -union TitleVersion { - u32 version; - - struct { - u8 v_revision; - u8 v_micro; - u8 v_minor; - u8 v_major; - }; -}; - -std::string FormatTitleVersion(u32 version_, bool full) { - TitleVersion ver{}; - ver.version = version_; +constexpr u64 SINGLE_BYTE_MODULUS = 0x100; + +std::string FormatTitleVersion(u32 version, TitleVersionFormat format) { + std::array bytes{}; + bytes[0] = version % SINGLE_BYTE_MODULUS; + for (size_t i = 1; i < bytes.size(); ++i) { + version /= SINGLE_BYTE_MODULUS; + bytes[i] = version % SINGLE_BYTE_MODULUS; + } - if (full) - return fmt::format("v{}.{}.{}.{}", ver.v_major, ver.v_minor, ver.v_minor, ver.v_revision); - return fmt::format("v{}.{}.{}", ver.v_major, ver.v_minor, ver.v_micro); + 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 PATCH_TYPE_NAMES{ @@ -49,8 +44,9 @@ VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const { const auto update = installed->GetEntry(update_tid, ContentRecordType::Program); if (update != nullptr) { if (update->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS && - update->GetExeFS() != nullptr) + update->GetExeFS() != nullptr) { exefs = update->GetExeFS(); + } } return exefs; @@ -81,8 +77,9 @@ std::map PatchManager::GetPatchVersionNames() const { const auto update_tid = GetUpdateTitleID(title_id); const auto update_version = installed->GetEntryVersion(update_tid); if (update_version != boost::none && - installed->HasEntry(update_tid, ContentRecordType::Program)) + installed->HasEntry(update_tid, ContentRecordType::Program)) { out[PatchType::Update] = update_version.get(); + } return out; } -- cgit v1.2.3 From cbd517d8cc1ba70d149adb57299a62c7a4e5fd72 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 28 Aug 2018 22:38:35 -0400 Subject: bktr: Add logging on successful patch --- src/core/file_sys/patch_manager.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'src/core/file_sys/patch_manager.cpp') diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 5e853c2c0..8b7d79773 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -34,6 +34,8 @@ std::string FormatPatchTypeName(PatchType type) { PatchManager::PatchManager(u64 title_id) : title_id(title_id) {} VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const { + LOG_INFO(Loader, "Patching ExeFS for title_id={:016X}", title_id); + if (exefs == nullptr) return exefs; @@ -45,6 +47,8 @@ VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const { 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(); } } @@ -52,7 +56,11 @@ VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const { return exefs; } -VirtualFile PatchManager::PatchRomFS(VirtualFile romfs) const { +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(type)); + if (romfs == nullptr) return romfs; @@ -60,11 +68,15 @@ VirtualFile PatchManager::PatchRomFS(VirtualFile romfs) const { // Game Updates const auto update_tid = GetUpdateTitleID(title_id); - const auto update = installed->GetEntryRaw(update_tid, ContentRecordType::Program); + const auto update = installed->GetEntryRaw(update_tid, type); if (update != nullptr) { - const auto nca = std::make_shared(update, romfs); - if (nca->GetStatus() == Loader::ResultStatus::Success && nca->GetRomFS() != nullptr) - romfs = nca->GetRomFS(); + const auto new_nca = std::make_shared(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; -- cgit v1.2.3 From c91b60a421a3bd0dc85d80e0a5a2d261370df340 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 1 Sep 2018 13:11:30 -0400 Subject: game_list: Fix version display on non-NAND titles --- src/core/file_sys/patch_manager.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'src/core/file_sys/patch_manager.cpp') diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 8b7d79773..b6e25f7eb 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -82,15 +82,31 @@ VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset, return romfs; } -std::map PatchManager::GetPatchVersionNames() const { - std::map out; +std::map PatchManager::GetPatchVersionNames() const { + std::map out; const auto installed = Service::FileSystem::GetUnionContents(); const auto update_tid = GetUpdateTitleID(title_id); - const auto update_version = installed->GetEntryVersion(update_tid); - if (update_version != boost::none && - installed->HasEntry(update_tid, ContentRecordType::Program)) { - out[PatchType::Update] = update_version.get(); + const auto update_control = installed->GetEntry(title_id, ContentRecordType::Control); + if (update_control != nullptr) { + do { + const auto romfs = + PatchRomFS(update_control->GetRomFS(), update_control->GetBaseIVFCOffset(), + FileSys::ContentRecordType::Control); + if (romfs == nullptr) + break; + + const auto control_dir = FileSys::ExtractRomFS(romfs); + if (control_dir == nullptr) + break; + + const auto nacp_file = control_dir->GetFile("control.nacp"); + if (nacp_file == nullptr) + break; + + FileSys::NACP nacp(nacp_file); + out[PatchType::Update] = nacp.GetVersionString(); + } while (false); } return out; -- cgit v1.2.3 From 23a16c1720ee522f6ac7d1f426a2d4a918ce41c9 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Mon, 3 Sep 2018 18:57:52 -0400 Subject: patch_manager: Centralize Control-type NCA parsing --- src/core/file_sys/patch_manager.cpp | 75 +++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 20 deletions(-) (limited to 'src/core/file_sys/patch_manager.cpp') diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index b6e25f7eb..fa2fbe5e1 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include "core/file_sys/control_metadata.h" #include "core/file_sys/patch_manager.h" #include "core/file_sys/registered_cache.h" #include "core/hle/service/filesystem/filesystem.h" @@ -87,29 +88,63 @@ std::map PatchManager::GetPatchVersionNames() const { const auto installed = Service::FileSystem::GetUnionContents(); const auto update_tid = GetUpdateTitleID(title_id); - const auto update_control = installed->GetEntry(title_id, ContentRecordType::Control); - if (update_control != nullptr) { - do { - const auto romfs = - PatchRomFS(update_control->GetRomFS(), update_control->GetBaseIVFCOffset(), - FileSys::ContentRecordType::Control); - if (romfs == nullptr) - break; - - const auto control_dir = FileSys::ExtractRomFS(romfs); - if (control_dir == nullptr) - break; - - const auto nacp_file = control_dir->GetFile("control.nacp"); - if (nacp_file == nullptr) - break; - - FileSys::NACP nacp(nacp_file); - out[PatchType::Update] = nacp.GetVersionString(); - } while (false); + 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, 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, VirtualFile> PatchManager::ParseControlNCA( + const std::shared_ptr& 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_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 -- cgit v1.2.3 From c913136eb215699f9c8d51a8fd56490b9df7657f Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 4 Sep 2018 17:01:40 -0400 Subject: bktr: Fix bucket overlap error --- src/core/file_sys/patch_manager.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/core/file_sys/patch_manager.cpp') diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index fa2fbe5e1..40675de35 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -2,10 +2,13 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#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 { -- cgit v1.2.3