From 643057fea2c7818318fdc42e54a01583374db30c Mon Sep 17 00:00:00 2001 From: Starsam80 Date: Wed, 29 Dec 2021 15:19:21 -0700 Subject: [PATCH] NANDImporter: Make a class variable for the NAND root --- Source/Core/DiscIO/NANDImporter.cpp | 30 ++++++++++++++--------------- Source/Core/DiscIO/NANDImporter.h | 6 +++--- Source/Core/DolphinQt/MenuBar.cpp | 2 +- Source/Core/DolphinQt/WiiUpdate.cpp | 4 ++-- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/Source/Core/DiscIO/NANDImporter.cpp b/Source/Core/DiscIO/NANDImporter.cpp index ccd1525b62..85be1630e2 100644 --- a/Source/Core/DiscIO/NANDImporter.cpp +++ b/Source/Core/DiscIO/NANDImporter.cpp @@ -22,7 +22,9 @@ namespace DiscIO constexpr size_t NAND_SIZE = 0x20000000; constexpr size_t NAND_KEYS_SIZE = 0x400; -NANDImporter::NANDImporter() = default; +NANDImporter::NANDImporter() : m_nand_root(File::GetUserPath(D_WIIROOT_IDX)) +{ +} NANDImporter::~NANDImporter() = default; void NANDImporter::ImportNANDBin(const std::string& path_to_bin, @@ -34,14 +36,10 @@ void NANDImporter::ImportNANDBin(const std::string& path_to_bin, if (!ReadNANDBin(path_to_bin, get_otp_dump_path)) return; - std::string nand_root = File::GetUserPath(D_WIIROOT_IDX); - nand_root.pop_back(); // remove trailing path separator - m_nand_root_length = nand_root.length(); - FindSuperblock(); - ProcessEntry(0, nand_root); - ExportKeys(nand_root); - ExtractCertificates(nand_root); + ProcessEntry(0, ""); + ExportKeys(); + ExtractCertificates(); } bool NANDImporter::ReadNANDBin(const std::string& path_to_bin, @@ -162,12 +160,12 @@ void NANDImporter::ProcessDirectory(const NANDFSTEntry& entry, const std::string INFO_LOG_FMT(DISCIO, "Path: {}", FormatDebugString(entry)); const std::string path = GetPath(entry, parent_path); - File::CreateDir(path); + File::CreateDir(m_nand_root + path); if (entry.sub != 0xffff) ProcessEntry(entry.sub, path); - INFO_LOG_FMT(DISCIO, "Path: {}", parent_path.data() + m_nand_root_length); + INFO_LOG_FMT(DISCIO, "Path: {}", parent_path); } void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& parent_path) @@ -179,7 +177,7 @@ void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& par INFO_LOG_FMT(DISCIO, "File: {}", FormatDebugString(entry)); const std::string path = GetPath(entry, parent_path); - File::IOFile file(path, "wb"); + File::IOFile file(m_nand_root + path, "wb"); std::array key{}; std::copy(&m_nand_keys[NAND_AES_KEY_OFFSET], &m_nand_keys[NAND_AES_KEY_OFFSET + key.size()], key.begin()); @@ -198,9 +196,9 @@ void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& par } } -bool NANDImporter::ExtractCertificates(const std::string& nand_root) +bool NANDImporter::ExtractCertificates() { - const std::string content_dir = nand_root + "/title/00000001/0000000d/content/"; + const std::string content_dir = m_nand_root + "/title/00000001/0000000d/content/"; File::IOFile tmd_file(content_dir + "title.tmd", "rb"); std::vector tmd_bytes(tmd_file.GetSize()); @@ -251,7 +249,7 @@ bool NANDImporter::ExtractCertificates(const std::string& nand_root) return false; } - const std::string pem_file_path = nand_root + std::string(certificate.filename); + const std::string pem_file_path = m_nand_root + std::string(certificate.filename); const ptrdiff_t certificate_offset = std::distance(content_bytes.begin(), search_result); const u16 certificate_size = Common::swap16(&content_bytes[certificate_offset - 2]); INFO_LOG_FMT(DISCIO, "ExtractCertificates: '{}' offset: {:#x} size: {:#x}", @@ -267,9 +265,9 @@ bool NANDImporter::ExtractCertificates(const std::string& nand_root) return true; } -void NANDImporter::ExportKeys(const std::string& nand_root) +void NANDImporter::ExportKeys() { - const std::string file_path = nand_root + "/keys.bin"; + const std::string file_path = m_nand_root + "/keys.bin"; File::IOFile file(file_path, "wb"); if (!file.WriteBytes(m_nand_keys.data(), NAND_KEYS_SIZE)) PanicAlertFmtT("Unable to write to file {0}", file_path); diff --git a/Source/Core/DiscIO/NANDImporter.h b/Source/Core/DiscIO/NANDImporter.h index f60a5b06f8..e5a36188e3 100644 --- a/Source/Core/DiscIO/NANDImporter.h +++ b/Source/Core/DiscIO/NANDImporter.h @@ -22,7 +22,7 @@ public: // get_otp_dump_path will be called to get a path to it. void ImportNANDBin(const std::string& path_to_bin, std::function update_callback, std::function get_otp_dump_path); - bool ExtractCertificates(const std::string& nand_root); + bool ExtractCertificates(); private: #pragma pack(push, 1) @@ -48,13 +48,13 @@ private: void ProcessEntry(u16 entry_number, const std::string& parent_path); void ProcessFile(const NANDFSTEntry& entry, const std::string& parent_path); void ProcessDirectory(const NANDFSTEntry& entry, const std::string& parent_path); - void ExportKeys(const std::string& nand_root); + void ExportKeys(); + std::string m_nand_root; std::vector m_nand; std::vector m_nand_keys; size_t m_nand_fat_offset = 0; size_t m_nand_fst_offset = 0; std::function m_update_callback; - size_t m_nand_root_length = 0; }; } // namespace DiscIO diff --git a/Source/Core/DolphinQt/MenuBar.cpp b/Source/Core/DolphinQt/MenuBar.cpp index 0c08dd3390..7f162c4914 100644 --- a/Source/Core/DolphinQt/MenuBar.cpp +++ b/Source/Core/DolphinQt/MenuBar.cpp @@ -1175,7 +1175,7 @@ void MenuBar::CheckNAND() void MenuBar::NANDExtractCertificates() { - if (DiscIO::NANDImporter().ExtractCertificates(File::GetUserPath(D_WIIROOT_IDX))) + if (DiscIO::NANDImporter().ExtractCertificates()) { ModalMessageBox::information(this, tr("Success"), tr("Successfully extracted certificates from NAND")); diff --git a/Source/Core/DolphinQt/WiiUpdate.cpp b/Source/Core/DolphinQt/WiiUpdate.cpp index edd3ba9593..0cfc336c51 100644 --- a/Source/Core/DolphinQt/WiiUpdate.cpp +++ b/Source/Core/DolphinQt/WiiUpdate.cpp @@ -30,12 +30,12 @@ static void ShowResult(QWidget* parent, WiiUtils::UpdateResult result) case WiiUtils::UpdateResult::Succeeded: ModalMessageBox::information(parent, QObject::tr("Update completed"), QObject::tr("The emulated Wii console has been updated.")); - DiscIO::NANDImporter().ExtractCertificates(File::GetUserPath(D_WIIROOT_IDX)); + DiscIO::NANDImporter().ExtractCertificates(); break; case WiiUtils::UpdateResult::AlreadyUpToDate: ModalMessageBox::information(parent, QObject::tr("Update completed"), QObject::tr("The emulated Wii console is already up-to-date.")); - DiscIO::NANDImporter().ExtractCertificates(File::GetUserPath(D_WIIROOT_IDX)); + DiscIO::NANDImporter().ExtractCertificates(); break; case WiiUtils::UpdateResult::ServerFailed: ModalMessageBox::critical(parent, QObject::tr("Update failed"),