/
NandUtils.cpp
364 lines (312 loc) · 12.4 KB
/
NandUtils.cpp
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <algorithm>
#include <array>
#include <cctype>
#include <cinttypes>
#include <functional>
#include <iterator>
#include <string>
#include <unordered_set>
#include <vector>
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/NandPaths.h"
#include "Common/StringUtil.h"
#include "Core/IOS/ES/ES.h"
#include "Core/IOS/ES/Formats.h"
namespace IOS
{
namespace HLE
{
namespace Device
{
static IOS::ES::TMDReader FindTMD(FS::FileSystem* fs, u64 title_id, const std::string& tmd_path)
{
const auto file = fs->OpenFile(PID_KERNEL, PID_KERNEL, tmd_path, FS::Mode::Read);
if (!file)
return {};
std::vector<u8> tmd_bytes(file->GetStatus()->size);
if (!file->Read(tmd_bytes.data(), tmd_bytes.size()))
return {};
return IOS::ES::TMDReader{std::move(tmd_bytes)};
}
IOS::ES::TMDReader ES::FindImportTMD(u64 title_id) const
{
return FindTMD(m_ios.GetFS().get(), title_id,
Common::GetImportTitlePath(title_id) + "/content/title.tmd");
}
IOS::ES::TMDReader ES::FindInstalledTMD(u64 title_id) const
{
return FindTMD(m_ios.GetFS().get(), title_id, Common::GetTMDFileName(title_id));
}
IOS::ES::TicketReader ES::FindSignedTicket(u64 title_id) const
{
const std::string path = Common::GetTicketFileName(title_id);
const auto ticket_file = m_ios.GetFS()->OpenFile(PID_KERNEL, PID_KERNEL, path, FS::Mode::Read);
if (!ticket_file)
return {};
std::vector<u8> signed_ticket(ticket_file->GetStatus()->size);
if (!ticket_file->Read(signed_ticket.data(), signed_ticket.size()))
return {};
return IOS::ES::TicketReader{std::move(signed_ticket)};
}
static bool IsValidPartOfTitleID(const std::string& string)
{
if (string.length() != 8)
return false;
return std::all_of(string.begin(), string.end(),
[](const auto character) { return std::isxdigit(character) != 0; });
}
static std::vector<u64> GetTitlesInTitleOrImport(FS::FileSystem* fs, const std::string& titles_dir)
{
const auto entries = fs->ReadDirectory(PID_KERNEL, PID_KERNEL, titles_dir);
if (!entries)
{
ERROR_LOG(IOS_ES, "%s is not a directory", titles_dir.c_str());
return {};
}
std::vector<u64> title_ids;
// The /title and /import directories contain one directory per title type, and each of them has
// a directory per title (where the name is the low 32 bits of the title ID in %08x format).
for (const std::string& title_type : *entries)
{
if (!IsValidPartOfTitleID(title_type))
continue;
const auto title_entries =
fs->ReadDirectory(PID_KERNEL, PID_KERNEL, titles_dir + '/' + title_type);
if (!title_entries)
continue;
for (const std::string& title_identifier : *title_entries)
{
if (!IsValidPartOfTitleID(title_identifier))
continue;
if (!fs->ReadDirectory(PID_KERNEL, PID_KERNEL,
titles_dir + '/' + title_type + '/' + title_identifier))
continue;
const u32 type = std::stoul(title_type, nullptr, 16);
const u32 identifier = std::stoul(title_identifier, nullptr, 16);
title_ids.push_back(static_cast<u64>(type) << 32 | identifier);
}
}
// On a real Wii, the title list is not in any particular order. However, because of how
// the flash filesystem works, titles such as 1-2 are *never* in the first position.
// We must keep this behaviour, or some versions of the System Menu may break.
std::sort(title_ids.begin(), title_ids.end(), std::greater<>());
return title_ids;
}
std::vector<u64> ES::GetInstalledTitles() const
{
return GetTitlesInTitleOrImport(m_ios.GetFS().get(), "/title");
}
std::vector<u64> ES::GetTitleImports() const
{
return GetTitlesInTitleOrImport(m_ios.GetFS().get(), "/import");
}
std::vector<u64> ES::GetTitlesWithTickets() const
{
const auto fs = m_ios.GetFS();
const auto entries = fs->ReadDirectory(PID_KERNEL, PID_KERNEL, "/ticket");
if (!entries)
{
ERROR_LOG(IOS_ES, "/ticket is not a directory");
return {};
}
std::vector<u64> title_ids;
// The /ticket directory contains one directory per title type, and each of them contains
// one ticket per title (where the name is the low 32 bits of the title ID in %08x format).
for (const std::string& title_type : *entries)
{
if (!IsValidPartOfTitleID(title_type))
continue;
const auto sub_entries = fs->ReadDirectory(PID_KERNEL, PID_KERNEL, "/ticket/" + title_type);
if (!sub_entries)
continue;
for (const std::string& file_name : *sub_entries)
{
const std::string name_without_ext = file_name.substr(0, 8);
if (fs->ReadDirectory(PID_KERNEL, PID_KERNEL, "/ticket/" + title_type + '/' + file_name) ||
!IsValidPartOfTitleID(name_without_ext) || name_without_ext + ".tik" != file_name)
{
continue;
}
const u32 type = std::stoul(title_type, nullptr, 16);
const u32 identifier = std::stoul(name_without_ext, nullptr, 16);
title_ids.push_back(static_cast<u64>(type) << 32 | identifier);
}
}
return title_ids;
}
std::vector<IOS::ES::Content> ES::GetStoredContentsFromTMD(const IOS::ES::TMDReader& tmd) const
{
if (!tmd.IsValid())
return {};
const IOS::ES::SharedContentMap map{m_ios.GetFS()};
const std::vector<IOS::ES::Content> contents = tmd.GetContents();
std::vector<IOS::ES::Content> stored_contents;
std::copy_if(contents.begin(), contents.end(), std::back_inserter(stored_contents),
[this, &tmd, &map](const IOS::ES::Content& content) {
const std::string path = GetContentPath(tmd.GetTitleId(), content, map);
return !path.empty() &&
m_ios.GetFS()->GetMetadata(PID_KERNEL, PID_KERNEL, path).Succeeded();
});
return stored_contents;
}
u32 ES::GetSharedContentsCount() const
{
const auto entries = m_ios.GetFS()->ReadDirectory(PID_KERNEL, PID_KERNEL, "/shared1");
return static_cast<u32>(
std::count_if(entries->begin(), entries->end(), [this](const std::string& entry) {
return !m_ios.GetFS()->ReadDirectory(PID_KERNEL, PID_KERNEL, "/shared1/" + entry) &&
entry.size() == 12 && entry.compare(8, 4, ".app") == 0;
}));
}
std::vector<std::array<u8, 20>> ES::GetSharedContents() const
{
const IOS::ES::SharedContentMap map{m_ios.GetFS()};
return map.GetHashes();
}
static bool DeleteDirectoriesIfEmpty(FS::FileSystem* fs, const std::string& path)
{
std::string::size_type position = std::string::npos;
do
{
const auto directory = fs->ReadDirectory(PID_KERNEL, PID_KERNEL, path.substr(0, position));
if ((directory && directory->empty()) ||
(!directory && directory.Error() != FS::ResultCode::NotFound))
{
if (fs->Delete(PID_KERNEL, PID_KERNEL, path.substr(0, position)) != FS::ResultCode::Success)
return false;
}
position = path.find_last_of('/', position - 1);
} while (position != 0);
return true;
}
bool ES::InitImport(const IOS::ES::TMDReader& tmd)
{
const auto fs = m_ios.GetFS();
const std::string content_dir = Common::GetTitleContentPath(tmd.GetTitleId());
const std::string import_content_dir = Common::GetImportTitlePath(tmd.GetTitleId()) + "/content";
const auto result1 = fs->CreateFullPath(PID_KERNEL, PID_KERNEL, content_dir + '/', 0,
FS::Mode::ReadWrite, FS::Mode::ReadWrite, FS::Mode::Read);
const auto result2 = fs->SetMetadata(PID_KERNEL, content_dir, PID_KERNEL, PID_KERNEL, 0,
FS::Mode::ReadWrite, FS::Mode::ReadWrite, FS::Mode::None);
const auto result3 = fs->CreateFullPath(PID_KERNEL, PID_KERNEL, import_content_dir + '/', 0,
FS::Mode::ReadWrite, FS::Mode::ReadWrite, FS::Mode::None);
if (result1 != FS::ResultCode::Success || result2 != FS::ResultCode::Success ||
result3 != FS::ResultCode::Success)
{
ERROR_LOG(IOS_ES, "InitImport: Failed to create content dir for %016" PRIx64, tmd.GetTitleId());
return false;
}
const std::string data_dir = Common::GetTitleDataPath(tmd.GetTitleId());
const auto data_dir_contents = fs->ReadDirectory(PID_KERNEL, PID_KERNEL, data_dir);
if (!data_dir_contents &&
(data_dir_contents.Error() != FS::ResultCode::NotFound ||
fs->CreateDirectory(PID_KERNEL, PID_KERNEL, data_dir, 0, FS::Mode::ReadWrite, FS::Mode::None,
FS::Mode::None) != FS::ResultCode::Success))
{
return false;
}
IOS::ES::UIDSys uid_sys{fs};
const u32 uid = uid_sys.GetOrInsertUIDForTitle(tmd.GetTitleId());
if (fs->SetMetadata(0, data_dir, uid, tmd.GetGroupId(), 0, FS::Mode::ReadWrite, FS::Mode::None,
FS::Mode::None) != FS::ResultCode::Success)
{
return false;
}
// IOS moves the title content directory to /import if the TMD exists during an import.
const auto file_info =
fs->GetMetadata(PID_KERNEL, PID_KERNEL, Common::GetTMDFileName(tmd.GetTitleId()));
if (!file_info || !file_info->is_file)
return true;
const auto result = fs->Rename(PID_KERNEL, PID_KERNEL, content_dir, import_content_dir);
if (result != FS::ResultCode::Success)
{
ERROR_LOG(IOS_ES, "InitImport: Failed to move content dir for %016" PRIx64, tmd.GetTitleId());
return false;
}
DeleteDirectoriesIfEmpty(m_ios.GetFS().get(), import_content_dir);
return true;
}
bool ES::FinishImport(const IOS::ES::TMDReader& tmd)
{
const auto fs = m_ios.GetFS();
const u64 title_id = tmd.GetTitleId();
const std::string import_content_dir = Common::GetImportTitlePath(title_id) + "/content";
// Remove everything not listed in the TMD.
std::unordered_set<std::string> expected_entries = {"title.tmd"};
for (const auto& content_info : tmd.GetContents())
expected_entries.insert(StringFromFormat("%08x.app", content_info.id));
const auto entries = fs->ReadDirectory(PID_KERNEL, PID_KERNEL, import_content_dir);
if (!entries)
return false;
for (const std::string& name : *entries)
{
const std::string absolute_path = import_content_dir + '/' + name;
// There should not be any directory in there. Remove it.
if (fs->ReadDirectory(PID_KERNEL, PID_KERNEL, absolute_path))
fs->Delete(PID_KERNEL, PID_KERNEL, absolute_path);
else if (expected_entries.find(name) == expected_entries.end())
fs->Delete(PID_KERNEL, PID_KERNEL, absolute_path);
}
const std::string content_dir = Common::GetTitleContentPath(title_id);
if (fs->Rename(PID_KERNEL, PID_KERNEL, import_content_dir, content_dir) !=
FS::ResultCode::Success)
{
ERROR_LOG(IOS_ES, "FinishImport: Failed to rename import directory to %s", content_dir.c_str());
return false;
}
return true;
}
bool ES::WriteImportTMD(const IOS::ES::TMDReader& tmd)
{
const auto fs = m_ios.GetFS();
const std::string tmd_path = "/tmp/title.tmd";
{
fs->CreateFile(PID_KERNEL, PID_KERNEL, tmd_path, 0, FS::Mode::ReadWrite, FS::Mode::ReadWrite,
FS::Mode::None);
const auto file = fs->OpenFile(PID_KERNEL, PID_KERNEL, tmd_path, FS::Mode::Write);
if (!file || !file->Write(tmd.GetBytes().data(), tmd.GetBytes().size()))
return false;
}
const std::string dest = Common::GetImportTitlePath(tmd.GetTitleId()) + "/content/title.tmd";
return fs->Rename(PID_KERNEL, PID_KERNEL, tmd_path, dest) == FS::ResultCode::Success;
}
void ES::FinishStaleImport(u64 title_id)
{
const auto fs = m_ios.GetFS();
const auto import_tmd = FindImportTMD(title_id);
if (!import_tmd.IsValid())
{
fs->Delete(PID_KERNEL, PID_KERNEL, Common::GetImportTitlePath(title_id) + "/content");
DeleteDirectoriesIfEmpty(fs.get(), Common::GetImportTitlePath(title_id));
DeleteDirectoriesIfEmpty(fs.get(), Common::GetTitlePath(title_id));
}
else
{
FinishImport(import_tmd);
}
}
void ES::FinishAllStaleImports()
{
const std::vector<u64> titles = GetTitleImports();
for (const u64& title_id : titles)
FinishStaleImport(title_id);
}
std::string ES::GetContentPath(const u64 title_id, const IOS::ES::Content& content,
const IOS::ES::SharedContentMap& content_map) const
{
if (content.IsShared())
return content_map.GetFilenameFromSHA1(content.sha1).value_or("");
return Common::GetTitleContentPath(title_id) + StringFromFormat("/%08x.app", content.id);
}
std::string ES::GetContentPath(const u64 title_id, const IOS::ES::Content& content) const
{
IOS::ES::SharedContentMap map{m_ios.GetFS()};
return GetContentPath(title_id, content, map);
}
} // namespace Device
} // namespace HLE
} // namespace IOS