summaryrefslogtreecommitdiff
path: root/Source/Core/DiscIO/NANDImporter.cpp
blob: 5ef1d68c4b1a43df117b29cd182e9a804927377f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "DiscIO/NANDImporter.h"

#include <algorithm>
#include <cstring>

#include "Common/Crypto/AES.h"
#include "Common/FileUtil.h"
#include "Common/IOFile.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
#include "Common/NandPaths.h"
#include "Core/IOS/ES/Formats.h"

namespace DiscIO
{
constexpr size_t NAND_SIZE = 0x20000000;
constexpr size_t NAND_KEYS_SIZE = 0x400;

NANDImporter::NANDImporter() : m_nand_root(File::GetUserPath(D_WIIROOT_IDX))
{
}
NANDImporter::~NANDImporter() = default;

void NANDImporter::ImportNANDBin(const std::string& path_to_bin, UpdateCallback update_callback,
                                 const std::function<std::string()>& get_otp_dump_path)
{
  m_update_callback = std::move(update_callback);

  if (!ReadNANDBin(path_to_bin, get_otp_dump_path))
    return;
  if (!FindSuperblock())
    return;

  ExportKeys();

  if (!ExtractFiles())
    return;

  ExtractCertificates();

  std::ignore = m_update_callback(Step::Extracting, m_progress_cur, m_progress_max);
}

bool NANDImporter::ReadNANDBin(const std::string& path_to_bin,
                               const std::function<std::string()>& get_otp_dump_path)
{
  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 =
      (NAND_BLOCK_SIZE + NAND_ECC_BLOCK_SIZE) * NAND_TOTAL_BLOCKS;  // 0x21000000

  File::IOFile file(path_to_bin, "rb");
  const u64 image_size = file.GetSize();
  if (image_size != NAND_BIN_SIZE + NAND_KEYS_SIZE && image_size != NAND_BIN_SIZE)
  {
    PanicAlertFmtT("This file does not look like a BootMii NAND backup.");
    return false;
  }

  m_nand.resize(NAND_SIZE);

  for (size_t i = 0; i < NAND_TOTAL_BLOCKS; i++)
  {
    if (m_update_callback(Step::Loading, int(i), int(NAND_TOTAL_BLOCKS)))
      return false;

    file.ReadBytes(&m_nand[i * NAND_BLOCK_SIZE], NAND_BLOCK_SIZE);

    // We don't care about the ECC blocks
    file.Seek(NAND_ECC_BLOCK_SIZE, File::SeekOrigin::Current);
  }

  m_nand_keys.resize(NAND_KEYS_SIZE);

  // 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);
}

bool NANDImporter::FindSuperblock()
{
  constexpr size_t NAND_SUPERBLOCK_START = 0x1fc00000;

  // There are 16 superblocks, choose the highest/newest version
  for (int i = 0; i < 16; i++)
  {
    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.data(), "SFFS", 4) != 0)
    {
      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);
  }

  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;
}

bool NANDImporter::ExtractFiles()
{
  constexpr u16 CLUSTER_CHAIN_END = 0xFFFB;
  m_progress_cur = 0;
  m_progress_max = std::ranges::count(m_superblock->fat, CLUSTER_CHAIN_END);
  std::bitset<FSTEntryCount> visited;
  return ProcessEntry(0, "", &visited);
}

std::string NANDImporter::GetPath(const NANDFSTEntry& entry, const std::string& parent_path)
{
  std::string name(entry.name, strnlen(entry.name, sizeof(NANDFSTEntry::name)));
  return parent_path + '/' + Common::EscapeFileName(name);
}

bool NANDImporter::ProcessEntry(u16 entry_number, const std::string& parent_path,
                                std::bitset<FSTEntryCount>* visited)
{
  while (entry_number != 0xffff)
  {
    if (entry_number >= m_superblock->fst.size())
    {
      ERROR_LOG_FMT(DISCIO, "FST entry number {} out of range", entry_number);
      return false;
    }

    if ((*visited)[entry_number])
    {
      ERROR_LOG_FMT(DISCIO, "FST entry number {} visited multiple times", entry_number);
      return false;
    }

    (*visited)[entry_number] = true;
    const NANDFSTEntry entry = m_superblock->fst[entry_number];

    const std::string path = entry_number == 0 ? parent_path : GetPath(entry, parent_path);
    INFO_LOG_FMT(DISCIO, "Entry: {} Path: {}", entry, path);

    Type type = static_cast<Type>(entry.mode & 3);
    if (type == Type::File)
    {
      if (m_update_callback(Step::Extracting, m_progress_cur++, m_progress_max))
        return false;
      std::vector<u8> data = GetEntryData(entry);
      File::IOFile file(m_nand_root + path, "wb");
      file.WriteBytes(data.data(), data.size());
    }
    else if (type == Type::Directory)
    {
      File::CreateDir(m_nand_root + path);
      if (!ProcessEntry(entry.sub, path, visited))
        return false;
    }
    else
    {
      ERROR_LOG_FMT(DISCIO, "Ignoring unknown entry type for {}", entry);
    }

    entry_number = entry.sib;
  }
  return true;
}

std::vector<u8> NANDImporter::GetEntryData(const NANDFSTEntry& entry) const
{
  constexpr size_t NAND_FAT_BLOCK_SIZE = 0x4000;

  u16 sub = entry.sub;
  size_t remaining_bytes = entry.size;
  std::vector<u8> data{};
  data.reserve(remaining_bytes);

  auto block = std::make_unique<u8[]>(NAND_FAT_BLOCK_SIZE);
  while (remaining_bytes > 0)
  {
    if (sub >= m_superblock->fat.size())
    {
      ERROR_LOG_FMT(DISCIO, "FAT block index {} out of range", sub);
      return {};
    }

    m_aes_ctx->CryptIvZero(&m_nand[NAND_FAT_BLOCK_SIZE * sub], block.get(), NAND_FAT_BLOCK_SIZE);

    size_t size = std::min(remaining_bytes, NAND_FAT_BLOCK_SIZE);
    data.insert(data.end(), block.get(), block.get() + size);
    remaining_bytes -= size;

    sub = m_superblock->fat[sub];
  }

  return data;
}

bool NANDImporter::ExtractCertificates()
{
  const std::string content_dir = m_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()))
  {
    ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not read IOS13 TMD");
    return false;
  }

  IOS::ES::TMDReader tmd(std::move(tmd_bytes));
  IOS::ES::Content content_metadata;
  if (!tmd.GetContent(tmd.GetBootIndex(), &content_metadata))
  {
    ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not get content ID from TMD");
    return false;
  }

  File::IOFile content_file(content_dir + fmt::format("{:08x}.app", content_metadata.id), "rb");
  std::vector<u8> content_bytes(content_file.GetSize());
  if (!content_file.ReadBytes(content_bytes.data(), content_bytes.size()))
  {
    ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not read IOS13 contents");
    return false;
  }

  struct PEMCertificate
  {
    std::string_view filename;
    std::array<u8, 4> search_bytes;
  };

  static constexpr std::array<PEMCertificate, 3> certificates{{
      {"/clientca.pem", {{0x30, 0x82, 0x03, 0xE9}}},
      {"/clientcakey.pem", {{0x30, 0x82, 0x02, 0x5D}}},
      {"/rootca.pem", {{0x30, 0x82, 0x03, 0x7D}}},
  }};

  for (const PEMCertificate& certificate : certificates)
  {
    const auto search_result = std::ranges::search(content_bytes, certificate.search_bytes);

    if (search_result.empty())
    {
      ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Could not find offset for certificate '{}'",
                    certificate.filename);
      return false;
    }

    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.begin());
    constexpr int min_offset = 2;
    if (certificate_offset < min_offset)
    {
      ERROR_LOG_FMT(
          DISCIO,
          "ExtractCertificates: Invalid certificate offset {:#x}, must be between {:#x} and {:#x}",
          certificate_offset, min_offset, content_bytes.size());
      return false;
    }
    const u16 certificate_size = Common::swap16(&content_bytes[certificate_offset - min_offset]);
    const size_t available_size = content_bytes.size() - static_cast<size_t>(certificate_offset);
    if (certificate_size > available_size)
    {
      ERROR_LOG_FMT(
          DISCIO,
          "ExtractCertificates: Invalid certificate size {:#x}, must be {:#x} bytes or smaller",
          certificate_size, available_size);
      return false;
    }
    INFO_LOG_FMT(DISCIO, "ExtractCertificates: '{}' offset: {:#x} size: {:#x}",
                 certificate.filename, certificate_offset, certificate_size);

    File::IOFile pem_file(pem_file_path, "wb");
    if (!pem_file.WriteBytes(&content_bytes[certificate_offset], certificate_size))
    {
      ERROR_LOG_FMT(DISCIO, "ExtractCertificates: Unable to write to file {}", pem_file_path);
      return false;
    }
  }
  return true;
}

void NANDImporter::ExportKeys()
{
  constexpr size_t NAND_AES_KEY_OFFSET = 0x158;

  m_aes_ctx = Common::AES::CreateContextDecrypt(&m_nand_keys[NAND_AES_KEY_OFFSET]);

  const std::string file_path = m_nand_root + "/keys.bin";
  File::IOFile file(file_path, "wb");
  if (!file.WriteBytes(m_nand_keys.data(), NAND_KEYS_SIZE))
    PanicAlertFmtT("Unable to write to file {0}", file_path);
}
}  // namespace DiscIO