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))
|
||||
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<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;
|
||||
}
|
||||
|
||||
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<u8> 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;
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
@ -47,11 +48,22 @@ public:
|
|||
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);
|
||||
|
@ -61,8 +73,7 @@ private:
|
|||
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::unique_ptr<NANDSuperblock> m_superblock;
|
||||
std::function<void()> m_update_callback;
|
||||
};
|
||||
} // namespace DiscIO
|
||||
|
|
Loading…
Reference in New Issue