2017-03-19 07:00:49 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "DiscIO/NANDImporter.h"
|
|
|
|
|
2017-06-19 02:57:30 +00:00
|
|
|
#include <algorithm>
|
2017-03-19 07:00:49 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
|
|
|
|
2020-10-14 16:34:45 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2017-03-19 07:00:49 +00:00
|
|
|
#include "Common/Crypto/AES.h"
|
|
|
|
#include "Common/FileUtil.h"
|
2020-09-15 10:29:41 +00:00
|
|
|
#include "Common/IOFile.h"
|
2017-05-15 00:47:02 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-03-19 07:00:49 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
#include "Common/Swap.h"
|
2017-06-19 02:57:30 +00:00
|
|
|
#include "Core/IOS/ES/Formats.h"
|
2017-03-19 07:00:49 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
|
|
|
constexpr size_t NAND_SIZE = 0x20000000;
|
|
|
|
constexpr size_t NAND_KEYS_SIZE = 0x400;
|
|
|
|
|
|
|
|
NANDImporter::NANDImporter() = default;
|
|
|
|
NANDImporter::~NANDImporter() = default;
|
|
|
|
|
|
|
|
void NANDImporter::ImportNANDBin(const std::string& path_to_bin,
|
2017-10-26 19:22:16 +00:00
|
|
|
std::function<void()> update_callback,
|
|
|
|
std::function<std::string()> get_otp_dump_path)
|
2017-03-19 07:00:49 +00:00
|
|
|
{
|
|
|
|
m_update_callback = std::move(update_callback);
|
|
|
|
|
2017-10-26 19:22:16 +00:00
|
|
|
if (!ReadNANDBin(path_to_bin, get_otp_dump_path))
|
2017-03-19 07:00:49 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
const std::string nand_root = File::GetUserPath(D_WIIROOT_IDX);
|
2017-05-15 00:47:02 +00:00
|
|
|
m_nand_root_length = nand_root.length();
|
|
|
|
if (nand_root.back() == '/')
|
|
|
|
m_nand_root_length++;
|
|
|
|
|
2017-03-19 07:00:49 +00:00
|
|
|
FindSuperblock();
|
|
|
|
ProcessEntry(0, nand_root);
|
|
|
|
ExportKeys(nand_root);
|
|
|
|
ExtractCertificates(nand_root);
|
|
|
|
}
|
|
|
|
|
2017-10-26 19:22:16 +00:00
|
|
|
bool NANDImporter::ReadNANDBin(const std::string& path_to_bin,
|
|
|
|
std::function<std::string()> get_otp_dump_path)
|
2017-03-19 07:00:49 +00:00
|
|
|
{
|
|
|
|
constexpr size_t NAND_TOTAL_BLOCKS = 0x40000;
|
|
|
|
constexpr size_t NAND_BLOCK_SIZE = 0x800;
|
|
|
|
constexpr size_t NAND_ECC_BLOCK_SIZE = 0x40;
|
|
|
|
constexpr size_t NAND_BIN_SIZE =
|
2017-10-26 19:22:16 +00:00
|
|
|
(NAND_BLOCK_SIZE + NAND_ECC_BLOCK_SIZE) * NAND_TOTAL_BLOCKS; // 0x21000000
|
2017-03-19 07:00:49 +00:00
|
|
|
|
|
|
|
File::IOFile file(path_to_bin, "rb");
|
2017-10-26 19:22:16 +00:00
|
|
|
const u64 image_size = file.GetSize();
|
|
|
|
if (image_size != NAND_BIN_SIZE + NAND_KEYS_SIZE && image_size != NAND_BIN_SIZE)
|
2017-03-19 07:00:49 +00:00
|
|
|
{
|
2020-11-11 06:06:33 +00:00
|
|
|
PanicAlertFmtT("This file does not look like a BootMii NAND backup.");
|
2017-03-19 07:00:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_nand.resize(NAND_SIZE);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < NAND_TOTAL_BLOCKS; i++)
|
|
|
|
{
|
2017-05-07 03:26:46 +00:00
|
|
|
// Instead of updating on every cycle, we only update every 1000 cycles for a balance between
|
|
|
|
// not updating fast enough vs updating too fast
|
|
|
|
if (i % 1000 == 0)
|
|
|
|
m_update_callback();
|
|
|
|
|
2017-03-19 07:00:49 +00:00
|
|
|
file.ReadBytes(&m_nand[i * NAND_BLOCK_SIZE], NAND_BLOCK_SIZE);
|
|
|
|
file.Seek(NAND_ECC_BLOCK_SIZE, SEEK_CUR); // We don't care about the ECC blocks
|
|
|
|
}
|
|
|
|
|
|
|
|
m_nand_keys.resize(NAND_KEYS_SIZE);
|
2017-10-26 19:22:16 +00:00
|
|
|
|
|
|
|
// Read the OTP/SEEPROM dump.
|
|
|
|
// If it is not included in the NAND image, get a path to the dump and read key data from it.
|
|
|
|
if (image_size == NAND_BIN_SIZE)
|
|
|
|
{
|
|
|
|
const std::string otp_dump_path = get_otp_dump_path();
|
|
|
|
if (otp_dump_path.empty())
|
|
|
|
return false;
|
|
|
|
File::IOFile keys_file{otp_dump_path, "rb"};
|
|
|
|
return keys_file.ReadBytes(m_nand_keys.data(), NAND_KEYS_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, just read the key data from the NAND image.
|
|
|
|
return file.ReadBytes(m_nand_keys.data(), NAND_KEYS_SIZE);
|
2017-03-19 07:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void 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)
|
|
|
|
{
|
|
|
|
if (!memcmp(m_nand.data() + pos, "SFFS", 4))
|
|
|
|
{
|
2020-10-21 18:58:08 +00:00
|
|
|
const u32 version = Common::swap32(&m_nand[pos + 4]);
|
|
|
|
INFO_LOG_FMT(DISCIO, "Found superblock at {:#x} with version {:#x}", pos, version);
|
2017-03-19 07:00:49 +00:00
|
|
|
if (superblock == 0 || version > newest_version)
|
|
|
|
{
|
|
|
|
superblock = pos;
|
|
|
|
newest_version = version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_nand_fat_offset = superblock + 0xC;
|
|
|
|
m_nand_fst_offset = m_nand_fat_offset + 0x10000;
|
2020-10-21 18:58:08 +00:00
|
|
|
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);
|
2017-03-19 07:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string NANDImporter::GetPath(const NANDFSTEntry& entry, const std::string& parent_path)
|
|
|
|
{
|
2017-10-28 20:40:05 +00:00
|
|
|
std::string name(entry.name, strnlen(entry.name, sizeof(NANDFSTEntry::name)));
|
2017-03-19 07:00:49 +00:00
|
|
|
|
|
|
|
if (name.front() == '/' || parent_path.back() == '/')
|
|
|
|
return parent_path + name;
|
|
|
|
|
|
|
|
return parent_path + '/' + name;
|
|
|
|
}
|
|
|
|
|
2017-05-15 00:47:02 +00:00
|
|
|
std::string NANDImporter::FormatDebugString(const NANDFSTEntry& entry)
|
|
|
|
{
|
2020-10-14 16:34:45 +00:00
|
|
|
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);
|
2017-05-15 00:47:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-19 07:00:49 +00:00
|
|
|
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);
|
2017-05-15 00:47:02 +00:00
|
|
|
else
|
2020-10-21 18:58:08 +00:00
|
|
|
ERROR_LOG_FMT(DISCIO, "Unknown mode: {}", FormatDebugString(entry));
|
2017-03-19 07:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NANDImporter::ProcessDirectory(const NANDFSTEntry& entry, const std::string& parent_path)
|
|
|
|
{
|
2017-05-07 03:26:46 +00:00
|
|
|
m_update_callback();
|
2020-10-21 18:58:08 +00:00
|
|
|
INFO_LOG_FMT(DISCIO, "Path: {}", FormatDebugString(entry));
|
2017-05-07 03:26:46 +00:00
|
|
|
|
2017-03-19 07:00:49 +00:00
|
|
|
const std::string path = GetPath(entry, parent_path);
|
|
|
|
File::CreateDir(path);
|
|
|
|
|
|
|
|
if (entry.sub != 0xffff)
|
|
|
|
ProcessEntry(entry.sub, path);
|
2017-05-15 00:47:02 +00:00
|
|
|
|
2020-10-21 18:58:08 +00:00
|
|
|
INFO_LOG_FMT(DISCIO, "Path: {}", parent_path.data() + m_nand_root_length);
|
2017-03-19 07:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& parent_path)
|
|
|
|
{
|
|
|
|
constexpr size_t NAND_AES_KEY_OFFSET = 0x158;
|
|
|
|
constexpr size_t NAND_FAT_BLOCK_SIZE = 0x4000;
|
|
|
|
|
2017-05-07 03:26:46 +00:00
|
|
|
m_update_callback();
|
2020-10-21 18:58:08 +00:00
|
|
|
INFO_LOG_FMT(DISCIO, "File: {}", FormatDebugString(entry));
|
2017-05-07 03:26:46 +00:00
|
|
|
|
2017-03-19 07:00:49 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
remaining_bytes -= size;
|
|
|
|
sub = Common::swap16(&m_nand[m_nand_fat_offset + 2 * sub]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 02:57:30 +00:00
|
|
|
bool NANDImporter::ExtractCertificates(const std::string& nand_root)
|
2017-03-19 07:00:49 +00:00
|
|
|
{
|
2017-06-19 02:57:30 +00:00
|
|
|
const std::string content_dir = nand_root + "/title/00000001/0000000d/content/";
|
|
|
|
|
|
|
|
File::IOFile tmd_file(content_dir + "title.tmd", "rb");
|
|
|
|
std::vector<u8> tmd_bytes(tmd_file.GetSize());
|
|
|
|
if (!tmd_file.ReadBytes(tmd_bytes.data(), tmd_bytes.size()))
|
2017-03-19 07:00:49 +00:00
|
|
|
{
|
2020-10-21 18:58:08 +00:00
|
|
|
ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not read IOS13 TMD");
|
2017-06-19 02:57:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOS::ES::TMDReader tmd(std::move(tmd_bytes));
|
|
|
|
IOS::ES::Content content_metadata;
|
|
|
|
if (!tmd.GetContent(tmd.GetBootIndex(), &content_metadata))
|
|
|
|
{
|
2020-10-21 18:58:08 +00:00
|
|
|
ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not get content ID from TMD");
|
2017-06-19 02:57:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-14 16:34:45 +00:00
|
|
|
File::IOFile content_file(content_dir + fmt::format("{:08x}.app", content_metadata.id), "rb");
|
2017-06-19 02:57:30 +00:00
|
|
|
std::vector<u8> content_bytes(content_file.GetSize());
|
|
|
|
if (!content_file.ReadBytes(content_bytes.data(), content_bytes.size()))
|
|
|
|
{
|
2020-10-21 18:58:08 +00:00
|
|
|
ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not read IOS13 contents");
|
2017-06-19 02:57:30 +00:00
|
|
|
return false;
|
2017-03-19 07:00:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 02:57:30 +00:00
|
|
|
struct PEMCertificate
|
2017-03-19 07:00:49 +00:00
|
|
|
{
|
2020-10-21 18:58:08 +00:00
|
|
|
std::string_view filename;
|
2017-06-19 02:57:30 +00:00
|
|
|
std::array<u8, 4> search_bytes;
|
2017-03-19 07:00:49 +00:00
|
|
|
};
|
2017-06-19 02:57:30 +00:00
|
|
|
|
2020-10-21 18:58:08 +00:00
|
|
|
static constexpr std::array<PEMCertificate, 3> certificates{{
|
2017-06-19 02:57:30 +00:00
|
|
|
{"/clientca.pem", {{0x30, 0x82, 0x03, 0xE9}}},
|
|
|
|
{"/clientcakey.pem", {{0x30, 0x82, 0x02, 0x5D}}},
|
|
|
|
{"/rootca.pem", {{0x30, 0x82, 0x03, 0x7D}}},
|
|
|
|
}};
|
|
|
|
|
|
|
|
for (const PEMCertificate& certificate : certificates)
|
2017-03-19 07:00:49 +00:00
|
|
|
{
|
2017-06-19 02:57:30 +00:00
|
|
|
const auto search_result =
|
|
|
|
std::search(content_bytes.begin(), content_bytes.end(), certificate.search_bytes.begin(),
|
|
|
|
certificate.search_bytes.end());
|
|
|
|
|
|
|
|
if (search_result == content_bytes.end())
|
|
|
|
{
|
2020-10-21 18:58:08 +00:00
|
|
|
ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not find offset for certficate '{}'",
|
|
|
|
certificate.filename);
|
2017-06-19 02:57:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-21 18:58:08 +00:00
|
|
|
const std::string pem_file_path = nand_root + std::string(certificate.filename);
|
2017-06-19 02:57:30 +00:00
|
|
|
const ptrdiff_t certificate_offset = std::distance(content_bytes.begin(), search_result);
|
|
|
|
const u16 certificate_size = Common::swap16(&content_bytes[certificate_offset - 2]);
|
2020-10-21 18:58:08 +00:00
|
|
|
INFO_LOG_FMT(DISCIO, "ExtractCertificates: '{}' offset: {:#x} size: {:#x}",
|
|
|
|
certificate.filename, certificate_offset, certificate_size);
|
2017-03-19 07:00:49 +00:00
|
|
|
|
|
|
|
File::IOFile pem_file(pem_file_path, "wb");
|
2017-06-19 02:57:30 +00:00
|
|
|
if (!pem_file.WriteBytes(&content_bytes[certificate_offset], certificate_size))
|
|
|
|
{
|
2020-10-21 18:58:08 +00:00
|
|
|
ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Unable to write to file {}", pem_file_path);
|
2017-06-19 02:57:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-03-19 07:00:49 +00:00
|
|
|
}
|
2017-06-19 02:57:30 +00:00
|
|
|
return true;
|
2017-03-19 07:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NANDImporter::ExportKeys(const std::string& nand_root)
|
|
|
|
{
|
|
|
|
const std::string file_path = nand_root + "/keys.bin";
|
|
|
|
File::IOFile file(file_path, "wb");
|
|
|
|
if (!file.WriteBytes(m_nand_keys.data(), NAND_KEYS_SIZE))
|
2020-11-16 12:28:11 +00:00
|
|
|
PanicAlertFmtT("Unable to write to file {0}", file_path);
|
2017-03-19 07:00:49 +00:00
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace DiscIO
|