NANDImporter: Make a class variable for the NAND root

This commit is contained in:
Starsam80 2021-12-29 15:19:21 -07:00
parent ea116fbbdf
commit 643057fea2
No known key found for this signature in database
GPG Key ID: 4E48BB48BA7E9026
4 changed files with 20 additions and 22 deletions

View File

@ -22,7 +22,9 @@ namespace DiscIO
constexpr size_t NAND_SIZE = 0x20000000; constexpr size_t NAND_SIZE = 0x20000000;
constexpr size_t NAND_KEYS_SIZE = 0x400; constexpr size_t NAND_KEYS_SIZE = 0x400;
NANDImporter::NANDImporter() = default; NANDImporter::NANDImporter() : m_nand_root(File::GetUserPath(D_WIIROOT_IDX))
{
}
NANDImporter::~NANDImporter() = default; NANDImporter::~NANDImporter() = default;
void NANDImporter::ImportNANDBin(const std::string& path_to_bin, 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)) if (!ReadNANDBin(path_to_bin, get_otp_dump_path))
return; 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(); FindSuperblock();
ProcessEntry(0, nand_root); ProcessEntry(0, "");
ExportKeys(nand_root); ExportKeys();
ExtractCertificates(nand_root); ExtractCertificates();
} }
bool NANDImporter::ReadNANDBin(const std::string& path_to_bin, 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)); INFO_LOG_FMT(DISCIO, "Path: {}", FormatDebugString(entry));
const std::string path = GetPath(entry, parent_path); const std::string path = GetPath(entry, parent_path);
File::CreateDir(path); File::CreateDir(m_nand_root + path);
if (entry.sub != 0xffff) if (entry.sub != 0xffff)
ProcessEntry(entry.sub, path); 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) 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)); INFO_LOG_FMT(DISCIO, "File: {}", FormatDebugString(entry));
const std::string path = GetPath(entry, parent_path); const std::string path = GetPath(entry, parent_path);
File::IOFile file(path, "wb"); File::IOFile file(m_nand_root + path, "wb");
std::array<u8, 16> key{}; std::array<u8, 16> key{};
std::copy(&m_nand_keys[NAND_AES_KEY_OFFSET], &m_nand_keys[NAND_AES_KEY_OFFSET + key.size()], std::copy(&m_nand_keys[NAND_AES_KEY_OFFSET], &m_nand_keys[NAND_AES_KEY_OFFSET + key.size()],
key.begin()); 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"); File::IOFile tmd_file(content_dir + "title.tmd", "rb");
std::vector<u8> tmd_bytes(tmd_file.GetSize()); std::vector<u8> tmd_bytes(tmd_file.GetSize());
@ -251,7 +249,7 @@ bool NANDImporter::ExtractCertificates(const std::string& nand_root)
return false; 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 ptrdiff_t certificate_offset = std::distance(content_bytes.begin(), search_result);
const u16 certificate_size = Common::swap16(&content_bytes[certificate_offset - 2]); const u16 certificate_size = Common::swap16(&content_bytes[certificate_offset - 2]);
INFO_LOG_FMT(DISCIO, "ExtractCertificates: '{}' offset: {:#x} size: {:#x}", INFO_LOG_FMT(DISCIO, "ExtractCertificates: '{}' offset: {:#x} size: {:#x}",
@ -267,9 +265,9 @@ bool NANDImporter::ExtractCertificates(const std::string& nand_root)
return true; 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"); File::IOFile file(file_path, "wb");
if (!file.WriteBytes(m_nand_keys.data(), NAND_KEYS_SIZE)) if (!file.WriteBytes(m_nand_keys.data(), NAND_KEYS_SIZE))
PanicAlertFmtT("Unable to write to file {0}", file_path); PanicAlertFmtT("Unable to write to file {0}", file_path);

View File

@ -22,7 +22,7 @@ public:
// get_otp_dump_path will be called to get a path to it. // 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, void ImportNANDBin(const std::string& path_to_bin, std::function<void()> update_callback,
std::function<std::string()> get_otp_dump_path); std::function<std::string()> get_otp_dump_path);
bool ExtractCertificates(const std::string& nand_root); bool ExtractCertificates();
private: private:
#pragma pack(push, 1) #pragma pack(push, 1)
@ -48,13 +48,13 @@ private:
void ProcessEntry(u16 entry_number, const std::string& parent_path); void ProcessEntry(u16 entry_number, const std::string& parent_path);
void ProcessFile(const NANDFSTEntry& entry, 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 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<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; size_t m_nand_fat_offset = 0;
size_t m_nand_fst_offset = 0; size_t m_nand_fst_offset = 0;
std::function<void()> m_update_callback; std::function<void()> m_update_callback;
size_t m_nand_root_length = 0;
}; };
} // namespace DiscIO } // namespace DiscIO

View File

@ -1175,7 +1175,7 @@ void MenuBar::CheckNAND()
void MenuBar::NANDExtractCertificates() void MenuBar::NANDExtractCertificates()
{ {
if (DiscIO::NANDImporter().ExtractCertificates(File::GetUserPath(D_WIIROOT_IDX))) if (DiscIO::NANDImporter().ExtractCertificates())
{ {
ModalMessageBox::information(this, tr("Success"), ModalMessageBox::information(this, tr("Success"),
tr("Successfully extracted certificates from NAND")); tr("Successfully extracted certificates from NAND"));

View File

@ -30,12 +30,12 @@ static void ShowResult(QWidget* parent, WiiUtils::UpdateResult result)
case WiiUtils::UpdateResult::Succeeded: case WiiUtils::UpdateResult::Succeeded:
ModalMessageBox::information(parent, QObject::tr("Update completed"), ModalMessageBox::information(parent, QObject::tr("Update completed"),
QObject::tr("The emulated Wii console has been updated.")); QObject::tr("The emulated Wii console has been updated."));
DiscIO::NANDImporter().ExtractCertificates(File::GetUserPath(D_WIIROOT_IDX)); DiscIO::NANDImporter().ExtractCertificates();
break; break;
case WiiUtils::UpdateResult::AlreadyUpToDate: case WiiUtils::UpdateResult::AlreadyUpToDate:
ModalMessageBox::information(parent, QObject::tr("Update completed"), ModalMessageBox::information(parent, QObject::tr("Update completed"),
QObject::tr("The emulated Wii console is already up-to-date.")); QObject::tr("The emulated Wii console is already up-to-date."));
DiscIO::NANDImporter().ExtractCertificates(File::GetUserPath(D_WIIROOT_IDX)); DiscIO::NANDImporter().ExtractCertificates();
break; break;
case WiiUtils::UpdateResult::ServerFailed: case WiiUtils::UpdateResult::ServerFailed:
ModalMessageBox::critical(parent, QObject::tr("Update failed"), ModalMessageBox::critical(parent, QObject::tr("Update failed"),