| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <bitset> |
| 8 |
#include <functional> |
| 9 |
#include <memory> |
| 10 |
#include <string> |
| 11 |
#include <vector> |
| 12 |
|
| 13 |
#include <fmt/format.h> |
| 14 |
|
| 15 |
#include "Common/CommonTypes.h" |
| 16 |
#include "Common/Crypto/AES.h" |
| 17 |
#include "Common/Swap.h" |
| 18 |
|
| 19 |
namespace DiscIO |
| 20 |
{ |
| 21 |
class NANDImporter final |
| 22 |
{ |
| 23 |
public: |
| 24 |
NANDImporter(); |
| 25 |
~NANDImporter(); |
| 26 |
|
| 27 |
enum class Step |
| 28 |
{ |
| 29 |
Loading, |
| 30 |
Extracting, |
| 31 |
}; |
| 32 |
|
| 33 |
using UpdateCallback = std::function<bool(Step step, int cur, int max)>; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
void ImportNANDBin(const std::string& path_to_bin, UpdateCallback update_callback, |
| 39 |
const std::function<std::string()>& get_otp_dump_path); |
| 40 |
bool ExtractCertificates(); |
| 41 |
|
| 42 |
enum class Type |
| 43 |
{ |
| 44 |
File = 1, |
| 45 |
Directory = 2, |
| 46 |
}; |
| 47 |
|
| 48 |
#pragma pack(push, 1) |
| 49 |
struct NANDFSTEntry |
| 50 |
{ |
| 51 |
char name[12]; |
| 52 |
u8 mode; |
| 53 |
u8 attr; |
| 54 |
Common::BigEndianValue<u16> sub; |
| 55 |
Common::BigEndianValue<u16> sib; |
| 56 |
Common::BigEndianValue<u32> size; |
| 57 |
Common::BigEndianValue<u32> uid; |
| 58 |
Common::BigEndianValue<u16> gid; |
| 59 |
Common::BigEndianValue<u32> x3; |
| 60 |
}; |
| 61 |
static_assert(sizeof(NANDFSTEntry) == 0x20, "Wrong size"); |
| 62 |
|
| 63 |
static constexpr u16 FSTEntryCount = 0x17FF; |
| 64 |
struct NANDSuperblock |
| 65 |
{ |
| 66 |
std::array<char, 4> magic; |
| 67 |
Common::BigEndianValue<u32> version; |
| 68 |
Common::BigEndianValue<u32> unknown; |
| 69 |
std::array<Common::BigEndianValue<u16>, 0x8000> fat; |
| 70 |
std::array<NANDFSTEntry, FSTEntryCount> fst; |
| 71 |
std::array<u8, 0x14> pad; |
| 72 |
}; |
| 73 |
static_assert(sizeof(NANDSuperblock) == 0x40000, "Wrong size"); |
| 74 |
#pragma pack(pop) |
| 75 |
|
| 76 |
private: |
| 77 |
bool ReadNANDBin(const std::string& path_to_bin, |
| 78 |
const std::function<std::string()>& get_otp_dump_path); |
| 79 |
bool FindSuperblock(); |
| 80 |
bool ExtractFiles(); |
| 81 |
std::string GetPath(const NANDFSTEntry& entry, const std::string& parent_path); |
| 82 |
std::string FormatDebugString(const NANDFSTEntry& entry); |
| 83 |
bool ProcessEntry(u16 entry_number, const std::string& parent_path, |
| 84 |
std::bitset<FSTEntryCount>* visited); |
| 85 |
std::vector<u8> GetEntryData(const NANDFSTEntry& entry) const; |
| 86 |
void ExportKeys(); |
| 87 |
|
| 88 |
std::string m_nand_root; |
| 89 |
std::vector<u8> m_nand; |
| 90 |
std::vector<u8> m_nand_keys; |
| 91 |
std::unique_ptr<Common::AES::Context> m_aes_ctx; |
| 92 |
std::unique_ptr<NANDSuperblock> m_superblock; |
| 93 |
UpdateCallback m_update_callback; |
| 94 |
u16 m_progress_cur = 0; |
| 95 |
u16 m_progress_max = 0; |
| 96 |
}; |
| 97 |
} |
| 98 |
|
| 99 |
template <> |
| 100 |
struct fmt::formatter<DiscIO::NANDImporter::NANDFSTEntry> |
| 101 |
{ |
| 102 |
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); } |
| 103 |
template <typename FormatContext> |
| 104 |
auto format(const DiscIO::NANDImporter::NANDFSTEntry& entry, FormatContext& ctx) const |
| 105 |
{ |
| 106 |
return fmt::format_to( |
| 107 |
ctx.out(), "{:12.12} {:#010b} {:#04x} {:#06x} {:#06x} {:#010x} {:#010x} {:#06x} {:#010x}", |
| 108 |
entry.name, entry.mode, entry.attr, entry.sub, entry.sib, entry.size, entry.uid, entry.gid, |
| 109 |
entry.x3); |
| 110 |
} |
| 111 |
}; |