NANDImporter: Make superblocks less magical
Create a struct describing the superblock layout and use it directly without needing to specify offsets and such.
This commit is contained in:
parent
73151a5753
commit
80012ae253
|
@ -32,8 +32,9 @@ void NANDImporter::ImportNANDBin(const std::string& path_to_bin,
|
||||||
|
|
||||||
if (!ReadNANDBin(path_to_bin, get_otp_dump_path))
|
if (!ReadNANDBin(path_to_bin, get_otp_dump_path))
|
||||||
return;
|
return;
|
||||||
|
if (!FindSuperblock())
|
||||||
|
return;
|
||||||
|
|
||||||
FindSuperblock();
|
|
||||||
ProcessEntry(0, "");
|
ProcessEntry(0, "");
|
||||||
ExportKeys();
|
ExportKeys();
|
||||||
ExtractCertificates();
|
ExtractCertificates();
|
||||||
|
@ -88,32 +89,37 @@ bool NANDImporter::ReadNANDBin(const std::string& path_to_bin,
|
||||||
return file.ReadBytes(m_nand_keys.data(), NAND_KEYS_SIZE);
|
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_START = 0x1fc00000;
|
||||||
constexpr size_t NAND_SUPERBLOCK_SIZE = 0x40000;
|
|
||||||
|
|
||||||
size_t superblock = 0;
|
// There are 16 superblocks, choose the highest/newest version
|
||||||
u32 newest_version = 0;
|
for (int i = 0; i < 16; i++)
|
||||||
for (size_t pos = NAND_SUPERBLOCK_START; pos < NAND_SIZE; pos += NAND_SUPERBLOCK_SIZE)
|
|
||||||
{
|
{
|
||||||
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]);
|
ERROR_LOG_FMT(DISCIO, "Superblock #{} does not exist", i);
|
||||||
INFO_LOG_FMT(DISCIO, "Found superblock at {:#x} with version {:#x}", pos, version);
|
continue;
|
||||||
if (superblock == 0 || version > newest_version)
|
|
||||||
{
|
|
||||||
superblock = pos;
|
|
||||||
newest_version = version;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
if (!m_superblock)
|
||||||
m_nand_fst_offset = m_nand_fat_offset + 0x10000;
|
{
|
||||||
INFO_LOG_FMT(DISCIO,
|
PanicAlertFmtT("This file does not contain a valid Wii filesystem.");
|
||||||
"Using superblock version {:#x} at position {:#x}. FAT/FST offset: {:#x}/{:#x}",
|
return false;
|
||||||
newest_version, superblock, m_nand_fat_offset, m_nand_fst_offset);
|
}
|
||||||
|
|
||||||
|
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)
|
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)
|
void NANDImporter::ProcessEntry(u16 entry_number, const std::string& parent_path)
|
||||||
{
|
{
|
||||||
NANDFSTEntry entry;
|
|
||||||
while (entry_number != 0xffff)
|
while (entry_number != 0xffff)
|
||||||
{
|
{
|
||||||
memcpy(&entry, &m_nand[m_nand_fst_offset + sizeof(NANDFSTEntry) * entry_number],
|
const NANDFSTEntry entry = m_superblock->fst[entry_number];
|
||||||
sizeof(NANDFSTEntry));
|
|
||||||
|
|
||||||
const std::string path = GetPath(entry, parent_path);
|
const std::string path = GetPath(entry, parent_path);
|
||||||
INFO_LOG_FMT(DISCIO, "Entry: {} Path: {}", entry, path);
|
INFO_LOG_FMT(DISCIO, "Entry: {} Path: {}", entry, path);
|
||||||
|
@ -183,7 +187,7 @@ std::vector<u8> NANDImporter::GetEntryData(const NANDFSTEntry& entry)
|
||||||
data.insert(data.end(), block.begin(), block.begin() + size);
|
data.insert(data.end(), block.begin(), block.begin() + size);
|
||||||
remaining_bytes -= size;
|
remaining_bytes -= size;
|
||||||
|
|
||||||
sub = Common::swap16(&m_nand[m_nand_fat_offset + 2 * sub]);
|
sub = m_superblock->fat[sub];
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -47,11 +48,22 @@ public:
|
||||||
Common::BigEndianValue<u32> x3;
|
Common::BigEndianValue<u32> x3;
|
||||||
};
|
};
|
||||||
static_assert(sizeof(NANDFSTEntry) == 0x20, "Wrong size");
|
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)
|
#pragma pack(pop)
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool ReadNANDBin(const std::string& path_to_bin, std::function<std::string()> get_otp_dump_path);
|
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 GetPath(const NANDFSTEntry& entry, const std::string& parent_path);
|
||||||
std::string FormatDebugString(const NANDFSTEntry& entry);
|
std::string FormatDebugString(const NANDFSTEntry& entry);
|
||||||
void ProcessEntry(u16 entry_number, const std::string& parent_path);
|
void ProcessEntry(u16 entry_number, const std::string& parent_path);
|
||||||
|
@ -61,8 +73,7 @@ private:
|
||||||
std::string m_nand_root;
|
std::string m_nand_root;
|
||||||
std::vector<u8> m_nand;
|
std::vector<u8> m_nand;
|
||||||
std::vector<u8> m_nand_keys;
|
std::vector<u8> m_nand_keys;
|
||||||
size_t m_nand_fat_offset = 0;
|
std::unique_ptr<NANDSuperblock> m_superblock;
|
||||||
size_t m_nand_fst_offset = 0;
|
|
||||||
std::function<void()> m_update_callback;
|
std::function<void()> m_update_callback;
|
||||||
};
|
};
|
||||||
} // namespace DiscIO
|
} // namespace DiscIO
|
||||||
|
|
Loading…
Reference in New Issue