Merge pull request #10322 from Starsam80/nand

NANDImporter: Various improvements and cleanup
This commit is contained in:
Léo Lam 2022-03-02 11:56:11 +01:00 committed by GitHub
commit 666db19436
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 134 additions and 106 deletions

View File

@ -4,17 +4,13 @@
#include "DiscIO/NANDImporter.h"
#include <algorithm>
#include <array>
#include <cstring>
#include <fmt/format.h>
#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
@ -22,7 +18,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,
@ -33,15 +31,12 @@ void NANDImporter::ImportNANDBin(const std::string& path_to_bin,
if (!ReadNANDBin(path_to_bin, get_otp_dump_path))
return;
if (!FindSuperblock())
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);
ExportKeys();
ProcessEntry(0, "");
ExtractCertificates();
}
bool NANDImporter::ReadNANDBin(const std::string& path_to_bin,
@ -93,32 +88,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<NANDSuperblock>();
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;
}
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);
INFO_LOG_FMT(DISCIO, "Superblock #{} has version {:#x}", i, superblock->version);
if (!m_superblock || superblock->version > m_superblock->version)
m_superblock = std::move(superblock);
}
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)
@ -131,76 +131,65 @@ 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)],
sizeof(NANDFSTEntry));
if (entry.sib != 0xffff)
ProcessEntry(entry.sib, parent_path);
if ((entry.mode & 3) == 1)
ProcessFile(entry, parent_path);
else if ((entry.mode & 3) == 2)
ProcessDirectory(entry, parent_path);
else
ERROR_LOG_FMT(DISCIO, "Unknown mode: {}", FormatDebugString(entry));
}
void NANDImporter::ProcessDirectory(const NANDFSTEntry& entry, const std::string& parent_path)
while (entry_number != 0xffff)
{
m_update_callback();
INFO_LOG_FMT(DISCIO, "Path: {}", FormatDebugString(entry));
const NANDFSTEntry entry = m_superblock->fst[entry_number];
const std::string path = GetPath(entry, parent_path);
File::CreateDir(path);
INFO_LOG_FMT(DISCIO, "Entry: {} Path: {}", entry, path);
m_update_callback();
if (entry.sub != 0xffff)
Type type = static_cast<Type>(entry.mode & 3);
if (type == Type::File)
{
std::vector<u8> data = GetEntryData(entry);
File::IOFile file(m_nand_root + path, "wb");
file.WriteBytes(data.data(), data.size());
}
else if (type == Type::Directory)
{
File::CreateDir(m_nand_root + path);
ProcessEntry(entry.sub, path);
INFO_LOG_FMT(DISCIO, "Path: {}", parent_path.data() + m_nand_root_length);
}
else
{
ERROR_LOG_FMT(DISCIO, "Ignoring unknown entry type for {}", entry);
}
void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& parent_path)
entry_number = entry.sib;
}
}
std::vector<u8> NANDImporter::GetEntryData(const NANDFSTEntry& entry)
{
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(path, "wb");
std::array<u8, 16> 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;
size_t remaining_bytes = entry.size;
std::vector<u8> data{};
data.reserve(remaining_bytes);
while (remaining_bytes > 0)
{
std::array<u8, 16> iv{};
std::vector<u8> 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);
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);
remaining_bytes -= size;
sub = Common::swap16(&m_nand[m_nand_fat_offset + 2 * sub]);
}
sub = m_superblock->fat[sub];
}
bool NANDImporter::ExtractCertificates(const std::string& nand_root)
return data;
}
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<u8> tmd_bytes(tmd_file.GetSize());
@ -251,7 +240,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 +256,13 @@ 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";
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))
PanicAlertFmtT("Unable to write to file {0}", file_path);

View File

@ -3,11 +3,16 @@
#pragma once
#include <array>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include <fmt/format.h>
#include "Common/CommonTypes.h"
#include "Common/Swap.h"
namespace DiscIO
{
@ -22,39 +27,69 @@ public:
// get_otp_dump_path will be called to get a path to it.
void ImportNANDBin(const std::string& path_to_bin, std::function<void()> update_callback,
std::function<std::string()> get_otp_dump_path);
bool ExtractCertificates(const std::string& nand_root);
bool ExtractCertificates();
enum class Type
{
File = 1,
Directory = 2,
};
private:
#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<u16> sub;
Common::BigEndianValue<u16> sib;
Common::BigEndianValue<u32> size;
Common::BigEndianValue<u32> uid;
Common::BigEndianValue<u16> gid;
Common::BigEndianValue<u32> x3;
};
static_assert(sizeof(NANDFSTEntry) == 0x20, "Wrong size");
struct NANDSuperblock
{
char magic[4]; // "SFFS"
Common::BigEndianValue<u32> version;
Common::BigEndianValue<u32> unknown;
Common::BigEndianValue<u16> 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<std::string()> 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);
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);
std::vector<u8> GetEntryData(const NANDFSTEntry& entry);
void ExportKeys();
std::string m_nand_root;
std::vector<u8> m_nand;
std::vector<u8> m_nand_keys;
size_t m_nand_fat_offset = 0;
size_t m_nand_fst_offset = 0;
std::array<u8, 16> m_aes_key;
std::unique_ptr<NANDSuperblock> m_superblock;
std::function<void()> m_update_callback;
size_t m_nand_root_length = 0;
};
} // namespace DiscIO
template <>
struct fmt::formatter<DiscIO::NANDImporter::NANDFSTEntry>
{
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
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);
}
};

View File

@ -1176,7 +1176,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"));

View File

@ -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"),