From 643057fea2c7818318fdc42e54a01583374db30c Mon Sep 17 00:00:00 2001 From: Starsam80 Date: Wed, 29 Dec 2021 15:19:21 -0700 Subject: [PATCH 1/6] 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"), From 2ccd9744719a03bd37b47ebbfde7d67d89491749 Mon Sep 17 00:00:00 2001 From: Starsam80 Date: Wed, 29 Dec 2021 16:26:46 -0700 Subject: [PATCH 2/6] NANDImporter: Improve NANDFSTEntry `uid` is a u32, not a u16. Also, everything is big endian, so we can simplify the code a little bit. --- Source/Core/DiscIO/NANDImporter.cpp | 24 +++++----------- Source/Core/DiscIO/NANDImporter.h | 43 ++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/Source/Core/DiscIO/NANDImporter.cpp b/Source/Core/DiscIO/NANDImporter.cpp index 85be1630e2..32dafdd58d 100644 --- a/Source/Core/DiscIO/NANDImporter.cpp +++ b/Source/Core/DiscIO/NANDImporter.cpp @@ -7,14 +7,11 @@ #include #include -#include - #include "Common/Crypto/AES.h" #include "Common/FileUtil.h" #include "Common/IOFile.h" #include "Common/Logging/Log.h" #include "Common/MsgHandler.h" -#include "Common/Swap.h" #include "Core/IOS/ES/Formats.h" namespace DiscIO @@ -129,29 +126,22 @@ std::string NANDImporter::GetPath(const NANDFSTEntry& entry, const std::string& return parent_path + '/' + name; } -std::string NANDImporter::FormatDebugString(const NANDFSTEntry& entry) -{ - return fmt::format( - "{:12.12} {:#04x} {:#04x} {:#06x} {:#06x} {:#010x} {:#06x} {:#06x} {:#06x} {:#010x}", - entry.name, entry.mode, entry.attr, entry.sub, entry.sib, entry.size, entry.x1, entry.uid, - entry.gid, entry.x3); -} - void NANDImporter::ProcessEntry(u16 entry_number, const std::string& parent_path) { NANDFSTEntry entry; - memcpy(&entry, &m_nand[m_nand_fst_offset + sizeof(NANDFSTEntry) * Common::swap16(entry_number)], + memcpy(&entry, &m_nand[m_nand_fst_offset + sizeof(NANDFSTEntry) * entry_number], sizeof(NANDFSTEntry)); if (entry.sib != 0xffff) ProcessEntry(entry.sib, parent_path); - if ((entry.mode & 3) == 1) + Type type = static_cast(entry.mode & 3); + if (type == Type::File) ProcessFile(entry, parent_path); - else if ((entry.mode & 3) == 2) + else if (type == Type::Directory) ProcessDirectory(entry, parent_path); else - ERROR_LOG_FMT(DISCIO, "Unknown mode: {}", FormatDebugString(entry)); + ERROR_LOG_FMT(DISCIO, "Ignoring unknown entry type for {}", entry); } void NANDImporter::ProcessDirectory(const NANDFSTEntry& entry, const std::string& parent_path) @@ -181,8 +171,8 @@ void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& par std::array key{}; std::copy(&m_nand_keys[NAND_AES_KEY_OFFSET], &m_nand_keys[NAND_AES_KEY_OFFSET + key.size()], key.begin()); - u16 sub = Common::swap16(entry.sub); - u32 remaining_bytes = Common::swap32(entry.size); + u16 sub = entry.sub; + u32 remaining_bytes = entry.size; while (remaining_bytes > 0) { diff --git a/Source/Core/DiscIO/NANDImporter.h b/Source/Core/DiscIO/NANDImporter.h index e5a36188e3..7c266d6840 100644 --- a/Source/Core/DiscIO/NANDImporter.h +++ b/Source/Core/DiscIO/NANDImporter.h @@ -7,7 +7,10 @@ #include #include +#include + #include "Common/CommonTypes.h" +#include "Common/Swap.h" namespace DiscIO { @@ -24,23 +27,29 @@ public: std::function get_otp_dump_path); bool ExtractCertificates(); -private: + enum class Type + { + File = 1, + Directory = 2, + }; + #pragma pack(push, 1) struct NANDFSTEntry { char name[12]; - u8 mode; // 0x0C - u8 attr; // 0x0D - u16 sub; // 0x0E - u16 sib; // 0x10 - u32 size; // 0x12 - u16 x1; // 0x16 - u16 uid; // 0x18 - u16 gid; // 0x1A - u32 x3; // 0x1C + u8 mode; + u8 attr; + Common::BigEndianValue sub; + Common::BigEndianValue sib; + Common::BigEndianValue size; + Common::BigEndianValue uid; + Common::BigEndianValue gid; + Common::BigEndianValue x3; }; + static_assert(sizeof(NANDFSTEntry) == 0x20, "Wrong size"); #pragma pack(pop) +private: bool ReadNANDBin(const std::string& path_to_bin, std::function get_otp_dump_path); void FindSuperblock(); std::string GetPath(const NANDFSTEntry& entry, const std::string& parent_path); @@ -58,3 +67,17 @@ private: std::function m_update_callback; }; } // namespace DiscIO + +template <> +struct fmt::formatter +{ + constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); } + template + auto format(const DiscIO::NANDImporter::NANDFSTEntry& entry, FormatContext& ctx) const + { + return fmt::format_to( + ctx.out(), "{:12.12} {:#010b} {:#04x} {:#06x} {:#06x} {:#010x} {:#010x} {:#06x} {:#010x}", + entry.name, entry.mode, entry.attr, entry.sub, entry.sib, entry.size, entry.uid, entry.gid, + entry.x3); + } +}; From 6758c77c39b094555ab2797b0419217de40e202e Mon Sep 17 00:00:00 2001 From: Starsam80 Date: Wed, 29 Dec 2021 17:49:16 -0700 Subject: [PATCH 3/6] NANDImporter: Reduce recursion in `ProcessEntry` It also simplifies the code flow, as it no longer goes backwards through the filesystem chain. --- Source/Core/DiscIO/NANDImporter.cpp | 46 +++++++++++++---------------- Source/Core/DiscIO/NANDImporter.h | 4 +-- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/Source/Core/DiscIO/NANDImporter.cpp b/Source/Core/DiscIO/NANDImporter.cpp index 32dafdd58d..322dd548d7 100644 --- a/Source/Core/DiscIO/NANDImporter.cpp +++ b/Source/Core/DiscIO/NANDImporter.cpp @@ -129,44 +129,38 @@ std::string NANDImporter::GetPath(const NANDFSTEntry& entry, const std::string& void NANDImporter::ProcessEntry(u16 entry_number, const std::string& parent_path) { NANDFSTEntry entry; - memcpy(&entry, &m_nand[m_nand_fst_offset + sizeof(NANDFSTEntry) * entry_number], - sizeof(NANDFSTEntry)); + while (entry_number != 0xffff) + { + memcpy(&entry, &m_nand[m_nand_fst_offset + sizeof(NANDFSTEntry) * entry_number], + sizeof(NANDFSTEntry)); - if (entry.sib != 0xffff) - ProcessEntry(entry.sib, parent_path); + const std::string path = GetPath(entry, parent_path); + INFO_LOG_FMT(DISCIO, "Entry: {} Path: {}", entry, path); + m_update_callback(); - Type type = static_cast(entry.mode & 3); - if (type == Type::File) - ProcessFile(entry, parent_path); - else if (type == Type::Directory) - ProcessDirectory(entry, parent_path); - else - ERROR_LOG_FMT(DISCIO, "Ignoring unknown entry type for {}", entry); + Type type = static_cast(entry.mode & 3); + if (type == Type::File) + ProcessFile(entry, path); + else if (type == Type::Directory) + ProcessDirectory(entry, path); + else + ERROR_LOG_FMT(DISCIO, "Ignoring unknown entry type for {}", entry); + + entry_number = entry.sib; + } } -void NANDImporter::ProcessDirectory(const NANDFSTEntry& entry, const std::string& parent_path) +void NANDImporter::ProcessDirectory(const NANDFSTEntry& entry, const std::string& path) { - m_update_callback(); - INFO_LOG_FMT(DISCIO, "Path: {}", FormatDebugString(entry)); - - const std::string path = GetPath(entry, parent_path); File::CreateDir(m_nand_root + path); - - if (entry.sub != 0xffff) - ProcessEntry(entry.sub, path); - - INFO_LOG_FMT(DISCIO, "Path: {}", parent_path); + ProcessEntry(entry.sub, path); } -void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& parent_path) +void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& path) { constexpr size_t NAND_AES_KEY_OFFSET = 0x158; constexpr size_t NAND_FAT_BLOCK_SIZE = 0x4000; - m_update_callback(); - INFO_LOG_FMT(DISCIO, "File: {}", FormatDebugString(entry)); - - const std::string path = GetPath(entry, parent_path); 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()], diff --git a/Source/Core/DiscIO/NANDImporter.h b/Source/Core/DiscIO/NANDImporter.h index 7c266d6840..eeb12c87bc 100644 --- a/Source/Core/DiscIO/NANDImporter.h +++ b/Source/Core/DiscIO/NANDImporter.h @@ -55,8 +55,8 @@ private: std::string GetPath(const NANDFSTEntry& entry, const std::string& parent_path); std::string FormatDebugString(const NANDFSTEntry& entry); 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 ProcessFile(const NANDFSTEntry& entry, const std::string& path); + void ProcessDirectory(const NANDFSTEntry& entry, const std::string& path); void ExportKeys(); std::string m_nand_root; From 73151a57536d7ce7e0b11904b7ab5467cd1fe466 Mon Sep 17 00:00:00 2001 From: Starsam80 Date: Wed, 29 Dec 2021 18:28:54 -0700 Subject: [PATCH 4/6] NANDImporter: Don't pass paths if we don't need to --- Source/Core/DiscIO/NANDImporter.cpp | 35 ++++++++++++++++++----------- Source/Core/DiscIO/NANDImporter.h | 3 +-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Source/Core/DiscIO/NANDImporter.cpp b/Source/Core/DiscIO/NANDImporter.cpp index 322dd548d7..1691ed5410 100644 --- a/Source/Core/DiscIO/NANDImporter.cpp +++ b/Source/Core/DiscIO/NANDImporter.cpp @@ -140,44 +140,53 @@ void NANDImporter::ProcessEntry(u16 entry_number, const std::string& parent_path Type type = static_cast(entry.mode & 3); if (type == Type::File) - ProcessFile(entry, path); + { + std::vector data = GetEntryData(entry); + File::IOFile file(m_nand_root + path, "wb"); + file.WriteBytes(data.data(), data.size()); + } else if (type == Type::Directory) - ProcessDirectory(entry, path); + { + File::CreateDir(m_nand_root + path); + ProcessEntry(entry.sub, path); + } else + { ERROR_LOG_FMT(DISCIO, "Ignoring unknown entry type for {}", entry); + } entry_number = entry.sib; } } -void NANDImporter::ProcessDirectory(const NANDFSTEntry& entry, const std::string& path) -{ - File::CreateDir(m_nand_root + path); - ProcessEntry(entry.sub, path); -} - -void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& path) +std::vector NANDImporter::GetEntryData(const NANDFSTEntry& entry) { constexpr size_t NAND_AES_KEY_OFFSET = 0x158; constexpr size_t NAND_FAT_BLOCK_SIZE = 0x4000; - 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()); + u16 sub = entry.sub; - u32 remaining_bytes = entry.size; + size_t remaining_bytes = entry.size; + std::vector data{}; + data.reserve(remaining_bytes); while (remaining_bytes > 0) { std::array iv{}; std::vector block = Common::AES::Decrypt( key.data(), iv.data(), &m_nand[NAND_FAT_BLOCK_SIZE * sub], NAND_FAT_BLOCK_SIZE); - u32 size = remaining_bytes < NAND_FAT_BLOCK_SIZE ? remaining_bytes : NAND_FAT_BLOCK_SIZE; - file.WriteBytes(block.data(), size); + + size_t size = std::min(remaining_bytes, block.size()); + data.insert(data.end(), block.begin(), block.begin() + size); remaining_bytes -= size; + sub = Common::swap16(&m_nand[m_nand_fat_offset + 2 * sub]); } + + return data; } bool NANDImporter::ExtractCertificates() diff --git a/Source/Core/DiscIO/NANDImporter.h b/Source/Core/DiscIO/NANDImporter.h index eeb12c87bc..2ebbbe1463 100644 --- a/Source/Core/DiscIO/NANDImporter.h +++ b/Source/Core/DiscIO/NANDImporter.h @@ -55,8 +55,7 @@ private: std::string GetPath(const NANDFSTEntry& entry, const std::string& parent_path); std::string FormatDebugString(const NANDFSTEntry& entry); void ProcessEntry(u16 entry_number, const std::string& parent_path); - void ProcessFile(const NANDFSTEntry& entry, const std::string& path); - void ProcessDirectory(const NANDFSTEntry& entry, const std::string& path); + std::vector GetEntryData(const NANDFSTEntry& entry); void ExportKeys(); std::string m_nand_root; From 80012ae2530e0138a1911782264e9d88c738b6ee Mon Sep 17 00:00:00 2001 From: Starsam80 Date: Wed, 29 Dec 2021 20:40:00 -0700 Subject: [PATCH 5/6] NANDImporter: Make superblocks less magical Create a struct describing the superblock layout and use it directly without needing to specify offsets and such. --- Source/Core/DiscIO/NANDImporter.cpp | 50 ++++++++++++++++------------- Source/Core/DiscIO/NANDImporter.h | 17 ++++++++-- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/Source/Core/DiscIO/NANDImporter.cpp b/Source/Core/DiscIO/NANDImporter.cpp index 1691ed5410..67be289b4c 100644 --- a/Source/Core/DiscIO/NANDImporter.cpp +++ b/Source/Core/DiscIO/NANDImporter.cpp @@ -32,8 +32,9 @@ void NANDImporter::ImportNANDBin(const std::string& path_to_bin, if (!ReadNANDBin(path_to_bin, get_otp_dump_path)) return; + if (!FindSuperblock()) + return; - FindSuperblock(); ProcessEntry(0, ""); ExportKeys(); ExtractCertificates(); @@ -88,32 +89,37 @@ bool NANDImporter::ReadNANDBin(const std::string& path_to_bin, return file.ReadBytes(m_nand_keys.data(), NAND_KEYS_SIZE); } -void NANDImporter::FindSuperblock() +bool NANDImporter::FindSuperblock() { constexpr size_t NAND_SUPERBLOCK_START = 0x1fc00000; - constexpr size_t NAND_SUPERBLOCK_SIZE = 0x40000; - size_t superblock = 0; - u32 newest_version = 0; - for (size_t pos = NAND_SUPERBLOCK_START; pos < NAND_SIZE; pos += NAND_SUPERBLOCK_SIZE) + // There are 16 superblocks, choose the highest/newest version + for (int i = 0; i < 16; i++) { - if (!memcmp(m_nand.data() + pos, "SFFS", 4)) + auto superblock = std::make_unique(); + std::memcpy(superblock.get(), &m_nand[NAND_SUPERBLOCK_START + i * sizeof(NANDSuperblock)], + sizeof(NANDSuperblock)); + + if (std::memcmp(superblock->magic, "SFFS", 4) != 0) { - const u32 version = Common::swap32(&m_nand[pos + 4]); - INFO_LOG_FMT(DISCIO, "Found superblock at {:#x} with version {:#x}", pos, version); - if (superblock == 0 || version > newest_version) - { - superblock = pos; - newest_version = version; - } + ERROR_LOG_FMT(DISCIO, "Superblock #{} does not exist", i); + continue; } + + INFO_LOG_FMT(DISCIO, "Superblock #{} has version {:#x}", i, superblock->version); + + if (!m_superblock || superblock->version > m_superblock->version) + m_superblock = std::move(superblock); } - m_nand_fat_offset = superblock + 0xC; - m_nand_fst_offset = m_nand_fat_offset + 0x10000; - INFO_LOG_FMT(DISCIO, - "Using superblock version {:#x} at position {:#x}. FAT/FST offset: {:#x}/{:#x}", - newest_version, superblock, m_nand_fat_offset, m_nand_fst_offset); + if (!m_superblock) + { + PanicAlertFmtT("This file does not contain a valid Wii filesystem."); + return false; + } + + INFO_LOG_FMT(DISCIO, "Using superblock version {:#x}", m_superblock->version); + return true; } std::string NANDImporter::GetPath(const NANDFSTEntry& entry, const std::string& parent_path) @@ -128,11 +134,9 @@ std::string NANDImporter::GetPath(const NANDFSTEntry& entry, const std::string& void NANDImporter::ProcessEntry(u16 entry_number, const std::string& parent_path) { - NANDFSTEntry entry; while (entry_number != 0xffff) { - memcpy(&entry, &m_nand[m_nand_fst_offset + sizeof(NANDFSTEntry) * entry_number], - sizeof(NANDFSTEntry)); + const NANDFSTEntry entry = m_superblock->fst[entry_number]; const std::string path = GetPath(entry, parent_path); INFO_LOG_FMT(DISCIO, "Entry: {} Path: {}", entry, path); @@ -183,7 +187,7 @@ std::vector NANDImporter::GetEntryData(const NANDFSTEntry& entry) data.insert(data.end(), block.begin(), block.begin() + size); remaining_bytes -= size; - sub = Common::swap16(&m_nand[m_nand_fat_offset + 2 * sub]); + sub = m_superblock->fat[sub]; } return data; diff --git a/Source/Core/DiscIO/NANDImporter.h b/Source/Core/DiscIO/NANDImporter.h index 2ebbbe1463..b0956e8d00 100644 --- a/Source/Core/DiscIO/NANDImporter.h +++ b/Source/Core/DiscIO/NANDImporter.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include @@ -47,11 +48,22 @@ public: Common::BigEndianValue x3; }; static_assert(sizeof(NANDFSTEntry) == 0x20, "Wrong size"); + + struct NANDSuperblock + { + char magic[4]; // "SFFS" + Common::BigEndianValue version; + Common::BigEndianValue unknown; + Common::BigEndianValue fat[0x8000]; + NANDFSTEntry fst[0x17FF]; + u8 pad[0x14]; + }; + static_assert(sizeof(NANDSuperblock) == 0x40000, "Wrong size"); #pragma pack(pop) private: bool ReadNANDBin(const std::string& path_to_bin, std::function get_otp_dump_path); - void FindSuperblock(); + bool FindSuperblock(); std::string GetPath(const NANDFSTEntry& entry, const std::string& parent_path); std::string FormatDebugString(const NANDFSTEntry& entry); void ProcessEntry(u16 entry_number, const std::string& parent_path); @@ -61,8 +73,7 @@ private: 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::unique_ptr m_superblock; std::function m_update_callback; }; } // namespace DiscIO From 41a336888965b1ef535084839644a061dc10eafe Mon Sep 17 00:00:00 2001 From: Starsam80 Date: Wed, 29 Dec 2021 21:03:50 -0700 Subject: [PATCH 6/6] NANDImporter: Only read the AES key once There is no need to constantly reset the key for every file entry. --- Source/Core/DiscIO/NANDImporter.cpp | 14 ++++++-------- Source/Core/DiscIO/NANDImporter.h | 2 ++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Source/Core/DiscIO/NANDImporter.cpp b/Source/Core/DiscIO/NANDImporter.cpp index 67be289b4c..4520930ca3 100644 --- a/Source/Core/DiscIO/NANDImporter.cpp +++ b/Source/Core/DiscIO/NANDImporter.cpp @@ -4,7 +4,6 @@ #include "DiscIO/NANDImporter.h" #include -#include #include #include "Common/Crypto/AES.h" @@ -35,8 +34,8 @@ void NANDImporter::ImportNANDBin(const std::string& path_to_bin, if (!FindSuperblock()) return; - ProcessEntry(0, ""); ExportKeys(); + ProcessEntry(0, ""); ExtractCertificates(); } @@ -165,13 +164,8 @@ void NANDImporter::ProcessEntry(u16 entry_number, const std::string& parent_path std::vector NANDImporter::GetEntryData(const NANDFSTEntry& entry) { - constexpr size_t NAND_AES_KEY_OFFSET = 0x158; constexpr size_t NAND_FAT_BLOCK_SIZE = 0x4000; - std::array key{}; - std::copy(&m_nand_keys[NAND_AES_KEY_OFFSET], &m_nand_keys[NAND_AES_KEY_OFFSET + key.size()], - key.begin()); - u16 sub = entry.sub; size_t remaining_bytes = entry.size; std::vector data{}; @@ -181,7 +175,7 @@ std::vector NANDImporter::GetEntryData(const NANDFSTEntry& entry) { std::array iv{}; std::vector block = Common::AES::Decrypt( - key.data(), iv.data(), &m_nand[NAND_FAT_BLOCK_SIZE * sub], NAND_FAT_BLOCK_SIZE); + m_aes_key.data(), iv.data(), &m_nand[NAND_FAT_BLOCK_SIZE * sub], NAND_FAT_BLOCK_SIZE); size_t size = std::min(remaining_bytes, block.size()); data.insert(data.end(), block.begin(), block.begin() + size); @@ -264,6 +258,10 @@ bool NANDImporter::ExtractCertificates() void NANDImporter::ExportKeys() { + constexpr size_t NAND_AES_KEY_OFFSET = 0x158; + + std::copy_n(&m_nand_keys[NAND_AES_KEY_OFFSET], m_aes_key.size(), m_aes_key.begin()); + 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)) diff --git a/Source/Core/DiscIO/NANDImporter.h b/Source/Core/DiscIO/NANDImporter.h index b0956e8d00..269a6c5483 100644 --- a/Source/Core/DiscIO/NANDImporter.h +++ b/Source/Core/DiscIO/NANDImporter.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include #include @@ -73,6 +74,7 @@ private: std::string m_nand_root; std::vector m_nand; std::vector m_nand_keys; + std::array m_aes_key; std::unique_ptr m_superblock; std::function m_update_callback; };