summaryrefslogtreecommitdiff
path: root/Source/Core/DiscIO/NANDContentLoader.cpp
blob: fe5d77d15e65b0760c9b81a617f918274e334966 (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
// Copyright 2009 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "DiscIO/NANDContentLoader.h"

#include <algorithm>
#include <array>
#include <cinttypes>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <string>
#include <utility>
#include <vector>

#include "Common/Align.h"
#include "Common/CommonTypes.h"
#include "Common/Crypto/AES.h"
#include "Common/File.h"
#include "Common/FileUtil.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
#include "Common/NandPaths.h"
#include "Common/StringUtil.h"
#include "Common/Swap.h"
// TODO: kill this dependency.
#include "Core/IOS/ES/Formats.h"

#include "DiscIO/WiiWad.h"

namespace DiscIO
{
NANDContentData::~NANDContentData() = default;

NANDContentDataFile::NANDContentDataFile(const std::string& filename) : m_filename{filename}
{
}

NANDContentDataFile::~NANDContentDataFile() = default;

void NANDContentDataFile::EnsureOpen()
{
  if (!m_file)
    m_file = std::make_unique<File::IOFile>(m_filename, "rb");
  else if (!m_file->IsOpen())
    m_file->Open(m_filename, "rb");
}
void NANDContentDataFile::Open()
{
  EnsureOpen();
}
std::vector<u8> NANDContentDataFile::Get()
{
  EnsureOpen();

  if (!m_file->IsGood())
    return {};

  u64 size = m_file->GetSize();
  if (size == 0)
    return {};

  std::vector<u8> result(size);
  m_file->ReadBytes(result.data(), result.size());

  return result;
}

bool NANDContentDataFile::GetRange(u32 start, u32 size, u8* buffer)
{
  EnsureOpen();
  if (!m_file->IsGood())
    return false;

  if (!m_file->Seek(start, SEEK_SET))
    return false;

  return m_file->ReadBytes(buffer, static_cast<size_t>(size));
}
void NANDContentDataFile::Close()
{
  if (m_file && m_file->IsOpen())
    m_file->Close();
}

bool NANDContentDataBuffer::GetRange(u32 start, u32 size, u8* buffer)
{
  if (start + size > m_buffer.size())
    return false;

  std::copy_n(&m_buffer[start], size, buffer);
  return true;
}

NANDContentLoader::NANDContentLoader(const std::string& content_name, Common::FromWhichRoot from)
    : m_root(from)
{
  m_Valid = Initialize(content_name);
}

NANDContentLoader::~NANDContentLoader()
{
}

bool NANDContentLoader::IsValid() const
{
  return m_Valid;
}

const NANDContent* NANDContentLoader::GetContentByID(u32 id) const
{
  const auto iterator = std::find_if(m_Content.begin(), m_Content.end(), [id](const auto& content) {
    return content.m_metadata.id == id;
  });
  return iterator != m_Content.end() ? &*iterator : nullptr;
}

const NANDContent* NANDContentLoader::GetContentByIndex(int index) const
{
  for (auto& Content : m_Content)
  {
    if (Content.m_metadata.index == index)
    {
      return &Content;
    }
  }
  return nullptr;
}

bool NANDContentLoader::Initialize(const std::string& name)
{
  if (name.empty())
    return false;

  m_Path = name;

  WiiWAD wad(name);
  std::vector<u8> data_app;

  if (wad.IsValid())
  {
    m_IsWAD = true;
    m_ticket = wad.GetTicket();
    m_tmd = wad.GetTMD();
    data_app = wad.GetDataApp();
  }
  else
  {
    std::string tmd_filename(m_Path);

    if (tmd_filename.back() == '/')
      tmd_filename += "title.tmd";
    else
      m_Path = tmd_filename.substr(0, tmd_filename.find("title.tmd"));

    File::IOFile tmd_file(tmd_filename, "rb");
    if (!tmd_file)
    {
      WARN_LOG(DISCIO, "CreateFromDirectory: error opening %s", tmd_filename.c_str());
      return false;
    }

    std::vector<u8> bytes(File::GetSize(tmd_filename));
    tmd_file.ReadBytes(bytes.data(), bytes.size());
    m_tmd.SetBytes(std::move(bytes));

    m_ticket = FindSignedTicket(m_tmd.GetTitleId());
  }

  InitializeContentEntries(data_app);
  return true;
}

void NANDContentLoader::InitializeContentEntries(const std::vector<u8>& data_app)
{
  if (!m_ticket.IsValid())
  {
    ERROR_LOG(IOS_ES, "No valid ticket for title %016" PRIx64, m_tmd.GetTitleId());
    return;
  }

  const std::vector<IOS::ES::Content> contents = m_tmd.GetContents();
  m_Content.resize(contents.size());

  u32 data_app_offset = 0;
  const std::vector<u8> title_key = m_ticket.GetTitleKey();
  IOS::ES::SharedContentMap shared_content{m_root};

  for (size_t i = 0; i < contents.size(); ++i)
  {
    const auto& content = contents.at(i);

    if (m_IsWAD)
    {
      // The content index is used as IV (2 bytes); the remaining 14 bytes are zeroes.
      std::array<u8, 16> iv{};
      iv[0] = static_cast<u8>(content.index >> 8) & 0xFF;
      iv[1] = static_cast<u8>(content.index) & 0xFF;

      u32 rounded_size = Common::AlignUp(static_cast<u32>(content.size), 0x40);

      m_Content[i].m_Data = std::make_unique<NANDContentDataBuffer>(Common::AES::Decrypt(
          title_key.data(), iv.data(), &data_app[data_app_offset], rounded_size));
      data_app_offset += rounded_size;
    }
    else
    {
      std::string filename;
      if (content.IsShared())
        filename = *shared_content.GetFilenameFromSHA1(content.sha1);
      else
        filename = StringFromFormat("%s/%08x.app", m_Path.c_str(), content.id);

      m_Content[i].m_Data = std::make_unique<NANDContentDataFile>(filename);
    }

    m_Content[i].m_metadata = std::move(content);
  }
}

NANDContentManager::~NANDContentManager()
{
}

const NANDContentLoader& NANDContentManager::GetNANDLoader(const std::string& content_path,
                                                           Common::FromWhichRoot from)
{
  auto it = m_map.find(content_path);
  if (it != m_map.end())
    return *it->second;
  return *m_map
              .emplace_hint(it, std::make_pair(content_path, std::make_unique<NANDContentLoader>(
                                                                 content_path, from)))
              ->second;
}

const NANDContentLoader& NANDContentManager::GetNANDLoader(u64 title_id, Common::FromWhichRoot from)
{
  std::string path = Common::GetTitleContentPath(title_id, from);
  return GetNANDLoader(path, from);
}

void NANDContentManager::ClearCache()
{
  m_map.clear();
}

IOS::ES::TicketReader FindSignedTicket(u64 title_id)
{
  std::string ticket_filename = Common::GetTicketFileName(title_id, Common::FROM_CONFIGURED_ROOT);
  File::IOFile ticket_file(ticket_filename, "rb");
  if (!ticket_file)
  {
    return IOS::ES::TicketReader{};
  }

  std::vector<u8> signed_ticket(ticket_file.GetSize());
  if (!ticket_file.ReadBytes(signed_ticket.data(), signed_ticket.size()))
  {
    return IOS::ES::TicketReader{};
  }

  return IOS::ES::TicketReader{std::move(signed_ticket)};
}
}  // namespace end