registered_cache.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <random>
  5. #include <regex>
  6. #include <mbedtls/sha256.h>
  7. #include "common/assert.h"
  8. #include "common/fs/path_util.h"
  9. #include "common/hex_util.h"
  10. #include "common/logging/log.h"
  11. #include "core/crypto/key_manager.h"
  12. #include "core/file_sys/card_image.h"
  13. #include "core/file_sys/common_funcs.h"
  14. #include "core/file_sys/content_archive.h"
  15. #include "core/file_sys/nca_metadata.h"
  16. #include "core/file_sys/registered_cache.h"
  17. #include "core/file_sys/submission_package.h"
  18. #include "core/file_sys/vfs_concat.h"
  19. #include "core/loader/loader.h"
  20. namespace FileSys {
  21. // The size of blocks to use when vfs raw copying into nand.
  22. constexpr size_t VFS_RC_LARGE_COPY_BLOCK = 0x400000;
  23. std::string ContentProviderEntry::DebugInfo() const {
  24. return fmt::format("title_id={:016X}, content_type={:02X}", title_id, static_cast<u8>(type));
  25. }
  26. bool operator<(const ContentProviderEntry& lhs, const ContentProviderEntry& rhs) {
  27. return (lhs.title_id < rhs.title_id) || (lhs.title_id == rhs.title_id && lhs.type < rhs.type);
  28. }
  29. bool operator==(const ContentProviderEntry& lhs, const ContentProviderEntry& rhs) {
  30. return std::tie(lhs.title_id, lhs.type) == std::tie(rhs.title_id, rhs.type);
  31. }
  32. bool operator!=(const ContentProviderEntry& lhs, const ContentProviderEntry& rhs) {
  33. return !operator==(lhs, rhs);
  34. }
  35. static bool FollowsTwoDigitDirFormat(std::string_view name) {
  36. static const std::regex two_digit_regex("000000[0-9A-F]{2}", std::regex_constants::ECMAScript |
  37. std::regex_constants::icase);
  38. return std::regex_match(name.begin(), name.end(), two_digit_regex);
  39. }
  40. static bool FollowsNcaIdFormat(std::string_view name) {
  41. static const std::regex nca_id_regex("[0-9A-F]{32}\\.nca", std::regex_constants::ECMAScript |
  42. std::regex_constants::icase);
  43. static const std::regex nca_id_cnmt_regex(
  44. "[0-9A-F]{32}\\.cnmt.nca", std::regex_constants::ECMAScript | std::regex_constants::icase);
  45. return (name.size() == 36 && std::regex_match(name.begin(), name.end(), nca_id_regex)) ||
  46. (name.size() == 41 && std::regex_match(name.begin(), name.end(), nca_id_cnmt_regex));
  47. }
  48. static std::string GetRelativePathFromNcaID(const std::array<u8, 16>& nca_id, bool second_hex_upper,
  49. bool within_two_digit, bool cnmt_suffix) {
  50. if (!within_two_digit) {
  51. const auto format_str = fmt::runtime(cnmt_suffix ? "{}.cnmt.nca" : "/{}.nca");
  52. return fmt::format(format_str, Common::HexToString(nca_id, second_hex_upper));
  53. }
  54. Core::Crypto::SHA256Hash hash{};
  55. mbedtls_sha256_ret(nca_id.data(), nca_id.size(), hash.data(), 0);
  56. const auto format_str =
  57. fmt::runtime(cnmt_suffix ? "/000000{:02X}/{}.cnmt.nca" : "/000000{:02X}/{}.nca");
  58. return fmt::format(format_str, hash[0], Common::HexToString(nca_id, second_hex_upper));
  59. }
  60. static std::string GetCNMTName(TitleType type, u64 title_id) {
  61. constexpr std::array<const char*, 9> TITLE_TYPE_NAMES{
  62. "SystemProgram",
  63. "SystemData",
  64. "SystemUpdate",
  65. "BootImagePackage",
  66. "BootImagePackageSafe",
  67. "Application",
  68. "Patch",
  69. "AddOnContent",
  70. "" ///< Currently unknown 'DeltaTitle'
  71. };
  72. auto index = static_cast<std::size_t>(type);
  73. // If the index is after the jump in TitleType, subtract it out.
  74. if (index >= static_cast<std::size_t>(TitleType::Application)) {
  75. index -= static_cast<std::size_t>(TitleType::Application) -
  76. static_cast<std::size_t>(TitleType::FirmwarePackageB);
  77. }
  78. return fmt::format("{}_{:016x}.cnmt", TITLE_TYPE_NAMES[index], title_id);
  79. }
  80. ContentRecordType GetCRTypeFromNCAType(NCAContentType type) {
  81. switch (type) {
  82. case NCAContentType::Program:
  83. // TODO(DarkLordZach): Differentiate between Program and Patch
  84. return ContentRecordType::Program;
  85. case NCAContentType::Meta:
  86. return ContentRecordType::Meta;
  87. case NCAContentType::Control:
  88. return ContentRecordType::Control;
  89. case NCAContentType::Data:
  90. case NCAContentType::PublicData:
  91. return ContentRecordType::Data;
  92. case NCAContentType::Manual:
  93. // TODO(DarkLordZach): Peek at NCA contents to differentiate Manual and Legal.
  94. return ContentRecordType::HtmlDocument;
  95. default:
  96. ASSERT_MSG(false, "Invalid NCAContentType={:02X}", type);
  97. return ContentRecordType{};
  98. }
  99. }
  100. ContentProvider::~ContentProvider() = default;
  101. bool ContentProvider::HasEntry(ContentProviderEntry entry) const {
  102. return HasEntry(entry.title_id, entry.type);
  103. }
  104. VirtualFile ContentProvider::GetEntryUnparsed(ContentProviderEntry entry) const {
  105. return GetEntryUnparsed(entry.title_id, entry.type);
  106. }
  107. VirtualFile ContentProvider::GetEntryRaw(ContentProviderEntry entry) const {
  108. return GetEntryRaw(entry.title_id, entry.type);
  109. }
  110. std::unique_ptr<NCA> ContentProvider::GetEntry(ContentProviderEntry entry) const {
  111. return GetEntry(entry.title_id, entry.type);
  112. }
  113. std::vector<ContentProviderEntry> ContentProvider::ListEntries() const {
  114. return ListEntriesFilter(std::nullopt, std::nullopt, std::nullopt);
  115. }
  116. PlaceholderCache::PlaceholderCache(VirtualDir dir_) : dir(std::move(dir_)) {}
  117. bool PlaceholderCache::Create(const NcaID& id, u64 size) const {
  118. const auto path = GetRelativePathFromNcaID(id, false, true, false);
  119. if (dir->GetFileRelative(path) != nullptr) {
  120. return false;
  121. }
  122. Core::Crypto::SHA256Hash hash{};
  123. mbedtls_sha256_ret(id.data(), id.size(), hash.data(), 0);
  124. const auto dirname = fmt::format("000000{:02X}", hash[0]);
  125. const auto dir2 = GetOrCreateDirectoryRelative(dir, dirname);
  126. if (dir2 == nullptr)
  127. return false;
  128. const auto file = dir2->CreateFile(fmt::format("{}.nca", Common::HexToString(id, false)));
  129. if (file == nullptr)
  130. return false;
  131. return file->Resize(size);
  132. }
  133. bool PlaceholderCache::Delete(const NcaID& id) const {
  134. const auto path = GetRelativePathFromNcaID(id, false, true, false);
  135. if (dir->GetFileRelative(path) == nullptr) {
  136. return false;
  137. }
  138. Core::Crypto::SHA256Hash hash{};
  139. mbedtls_sha256_ret(id.data(), id.size(), hash.data(), 0);
  140. const auto dirname = fmt::format("000000{:02X}", hash[0]);
  141. const auto dir2 = GetOrCreateDirectoryRelative(dir, dirname);
  142. const auto res = dir2->DeleteFile(fmt::format("{}.nca", Common::HexToString(id, false)));
  143. return res;
  144. }
  145. bool PlaceholderCache::Exists(const NcaID& id) const {
  146. const auto path = GetRelativePathFromNcaID(id, false, true, false);
  147. return dir->GetFileRelative(path) != nullptr;
  148. }
  149. bool PlaceholderCache::Write(const NcaID& id, u64 offset, const std::vector<u8>& data) const {
  150. const auto path = GetRelativePathFromNcaID(id, false, true, false);
  151. const auto file = dir->GetFileRelative(path);
  152. if (file == nullptr)
  153. return false;
  154. return file->WriteBytes(data, offset) == data.size();
  155. }
  156. bool PlaceholderCache::Register(RegisteredCache* cache, const NcaID& placeholder,
  157. const NcaID& install) const {
  158. const auto path = GetRelativePathFromNcaID(placeholder, false, true, false);
  159. const auto file = dir->GetFileRelative(path);
  160. if (file == nullptr)
  161. return false;
  162. const auto res = cache->RawInstallNCA(NCA{file}, &VfsRawCopy, false, install);
  163. if (res != InstallResult::Success)
  164. return false;
  165. return Delete(placeholder);
  166. }
  167. bool PlaceholderCache::CleanAll() const {
  168. return dir->GetParentDirectory()->CleanSubdirectoryRecursive(dir->GetName());
  169. }
  170. std::optional<std::array<u8, 0x10>> PlaceholderCache::GetRightsID(const NcaID& id) const {
  171. const auto path = GetRelativePathFromNcaID(id, false, true, false);
  172. const auto file = dir->GetFileRelative(path);
  173. if (file == nullptr)
  174. return std::nullopt;
  175. NCA nca{file};
  176. if (nca.GetStatus() != Loader::ResultStatus::Success &&
  177. nca.GetStatus() != Loader::ResultStatus::ErrorMissingBKTRBaseRomFS) {
  178. return std::nullopt;
  179. }
  180. const auto rights_id = nca.GetRightsId();
  181. if (rights_id == NcaID{})
  182. return std::nullopt;
  183. return rights_id;
  184. }
  185. u64 PlaceholderCache::Size(const NcaID& id) const {
  186. const auto path = GetRelativePathFromNcaID(id, false, true, false);
  187. const auto file = dir->GetFileRelative(path);
  188. if (file == nullptr)
  189. return 0;
  190. return file->GetSize();
  191. }
  192. bool PlaceholderCache::SetSize(const NcaID& id, u64 new_size) const {
  193. const auto path = GetRelativePathFromNcaID(id, false, true, false);
  194. const auto file = dir->GetFileRelative(path);
  195. if (file == nullptr)
  196. return false;
  197. return file->Resize(new_size);
  198. }
  199. std::vector<NcaID> PlaceholderCache::List() const {
  200. std::vector<NcaID> out;
  201. for (const auto& sdir : dir->GetSubdirectories()) {
  202. for (const auto& file : sdir->GetFiles()) {
  203. const auto name = file->GetName();
  204. if (name.length() == 36 && name.ends_with(".nca")) {
  205. out.push_back(Common::HexStringToArray<0x10>(name.substr(0, 32)));
  206. }
  207. }
  208. }
  209. return out;
  210. }
  211. NcaID PlaceholderCache::Generate() {
  212. std::random_device device;
  213. std::mt19937 gen(device());
  214. std::uniform_int_distribution<u64> distribution(1, std::numeric_limits<u64>::max());
  215. NcaID out{};
  216. const auto v1 = distribution(gen);
  217. const auto v2 = distribution(gen);
  218. std::memcpy(out.data(), &v1, sizeof(u64));
  219. std::memcpy(out.data() + sizeof(u64), &v2, sizeof(u64));
  220. return out;
  221. }
  222. VirtualFile RegisteredCache::OpenFileOrDirectoryConcat(const VirtualDir& open_dir,
  223. std::string_view path) const {
  224. const auto file = open_dir->GetFileRelative(path);
  225. if (file != nullptr) {
  226. return file;
  227. }
  228. const auto nca_dir = open_dir->GetDirectoryRelative(path);
  229. if (nca_dir == nullptr) {
  230. return nullptr;
  231. }
  232. const auto files = nca_dir->GetFiles();
  233. if (files.size() == 1 && files[0]->GetName() == "00") {
  234. return files[0];
  235. }
  236. std::vector<VirtualFile> concat;
  237. // Since the files are a two-digit hex number, max is FF.
  238. for (std::size_t i = 0; i < 0x100; ++i) {
  239. auto next = nca_dir->GetFile(fmt::format("{:02X}", i));
  240. if (next != nullptr) {
  241. concat.push_back(std::move(next));
  242. } else {
  243. next = nca_dir->GetFile(fmt::format("{:02x}", i));
  244. if (next != nullptr) {
  245. concat.push_back(std::move(next));
  246. } else {
  247. break;
  248. }
  249. }
  250. }
  251. if (concat.empty()) {
  252. return nullptr;
  253. }
  254. return ConcatenatedVfsFile::MakeConcatenatedFile(concat, concat.front()->GetName());
  255. }
  256. VirtualFile RegisteredCache::GetFileAtID(NcaID id) const {
  257. VirtualFile file;
  258. // Try all five relevant modes of file storage:
  259. // (bit 2 = uppercase/lower, bit 1 = within a two-digit dir, bit 0 = .cnmt suffix)
  260. // 000: /000000**/{:032X}.nca
  261. // 010: /{:032X}.nca
  262. // 100: /000000**/{:032x}.nca
  263. // 110: /{:032x}.nca
  264. // 111: /{:032x}.cnmt.nca
  265. for (u8 i = 0; i < 8; ++i) {
  266. if ((i % 2) == 1 && i != 7)
  267. continue;
  268. const auto path =
  269. GetRelativePathFromNcaID(id, (i & 0b100) == 0, (i & 0b010) == 0, (i & 0b001) == 0b001);
  270. file = OpenFileOrDirectoryConcat(dir, path);
  271. if (file != nullptr)
  272. return file;
  273. }
  274. return file;
  275. }
  276. static std::optional<NcaID> CheckMapForContentRecord(const std::map<u64, CNMT>& map, u64 title_id,
  277. ContentRecordType type) {
  278. const auto cmnt_iter = map.find(title_id);
  279. if (cmnt_iter == map.cend()) {
  280. return std::nullopt;
  281. }
  282. const auto& cnmt = cmnt_iter->second;
  283. const auto& content_records = cnmt.GetContentRecords();
  284. const auto iter = std::find_if(content_records.cbegin(), content_records.cend(),
  285. [type](const ContentRecord& rec) { return rec.type == type; });
  286. if (iter == content_records.cend()) {
  287. return std::nullopt;
  288. }
  289. return std::make_optional(iter->nca_id);
  290. }
  291. std::optional<NcaID> RegisteredCache::GetNcaIDFromMetadata(u64 title_id,
  292. ContentRecordType type) const {
  293. if (type == ContentRecordType::Meta && meta_id.find(title_id) != meta_id.end())
  294. return meta_id.at(title_id);
  295. const auto res1 = CheckMapForContentRecord(yuzu_meta, title_id, type);
  296. if (res1)
  297. return res1;
  298. return CheckMapForContentRecord(meta, title_id, type);
  299. }
  300. std::vector<NcaID> RegisteredCache::AccumulateFiles() const {
  301. std::vector<NcaID> ids;
  302. for (const auto& d2_dir : dir->GetSubdirectories()) {
  303. if (FollowsNcaIdFormat(d2_dir->GetName())) {
  304. ids.push_back(Common::HexStringToArray<0x10, true>(d2_dir->GetName().substr(0, 0x20)));
  305. continue;
  306. }
  307. if (!FollowsTwoDigitDirFormat(d2_dir->GetName()))
  308. continue;
  309. for (const auto& nca_dir : d2_dir->GetSubdirectories()) {
  310. if (nca_dir == nullptr || !FollowsNcaIdFormat(nca_dir->GetName())) {
  311. continue;
  312. }
  313. ids.push_back(Common::HexStringToArray<0x10, true>(nca_dir->GetName().substr(0, 0x20)));
  314. }
  315. for (const auto& nca_file : d2_dir->GetFiles()) {
  316. if (nca_file == nullptr || !FollowsNcaIdFormat(nca_file->GetName())) {
  317. continue;
  318. }
  319. ids.push_back(
  320. Common::HexStringToArray<0x10, true>(nca_file->GetName().substr(0, 0x20)));
  321. }
  322. }
  323. for (const auto& d2_file : dir->GetFiles()) {
  324. if (FollowsNcaIdFormat(d2_file->GetName()))
  325. ids.push_back(Common::HexStringToArray<0x10, true>(d2_file->GetName().substr(0, 0x20)));
  326. }
  327. return ids;
  328. }
  329. void RegisteredCache::ProcessFiles(const std::vector<NcaID>& ids) {
  330. for (const auto& id : ids) {
  331. const auto file = GetFileAtID(id);
  332. if (file == nullptr)
  333. continue;
  334. const auto nca = std::make_shared<NCA>(parser(file, id), nullptr, 0);
  335. if (nca->GetStatus() != Loader::ResultStatus::Success ||
  336. nca->GetType() != NCAContentType::Meta) {
  337. continue;
  338. }
  339. const auto section0 = nca->GetSubdirectories()[0];
  340. for (const auto& section0_file : section0->GetFiles()) {
  341. if (section0_file->GetExtension() != "cnmt")
  342. continue;
  343. meta.insert_or_assign(nca->GetTitleId(), CNMT(section0_file));
  344. meta_id.insert_or_assign(nca->GetTitleId(), id);
  345. break;
  346. }
  347. }
  348. }
  349. void RegisteredCache::AccumulateYuzuMeta() {
  350. const auto meta_dir = dir->GetSubdirectory("yuzu_meta");
  351. if (meta_dir == nullptr) {
  352. return;
  353. }
  354. for (const auto& file : meta_dir->GetFiles()) {
  355. if (file->GetExtension() != "cnmt") {
  356. continue;
  357. }
  358. CNMT cnmt(file);
  359. yuzu_meta.insert_or_assign(cnmt.GetTitleID(), std::move(cnmt));
  360. }
  361. }
  362. void RegisteredCache::Refresh() {
  363. if (dir == nullptr) {
  364. return;
  365. }
  366. const auto ids = AccumulateFiles();
  367. ProcessFiles(ids);
  368. AccumulateYuzuMeta();
  369. }
  370. RegisteredCache::RegisteredCache(VirtualDir dir_, ContentProviderParsingFunction parsing_function)
  371. : dir(std::move(dir_)), parser(std::move(parsing_function)) {
  372. Refresh();
  373. }
  374. RegisteredCache::~RegisteredCache() = default;
  375. bool RegisteredCache::HasEntry(u64 title_id, ContentRecordType type) const {
  376. return GetEntryRaw(title_id, type) != nullptr;
  377. }
  378. VirtualFile RegisteredCache::GetEntryUnparsed(u64 title_id, ContentRecordType type) const {
  379. const auto id = GetNcaIDFromMetadata(title_id, type);
  380. return id ? GetFileAtID(*id) : nullptr;
  381. }
  382. std::optional<u32> RegisteredCache::GetEntryVersion(u64 title_id) const {
  383. const auto meta_iter = meta.find(title_id);
  384. if (meta_iter != meta.cend()) {
  385. return meta_iter->second.GetTitleVersion();
  386. }
  387. const auto yuzu_meta_iter = yuzu_meta.find(title_id);
  388. if (yuzu_meta_iter != yuzu_meta.cend()) {
  389. return yuzu_meta_iter->second.GetTitleVersion();
  390. }
  391. return std::nullopt;
  392. }
  393. VirtualFile RegisteredCache::GetEntryRaw(u64 title_id, ContentRecordType type) const {
  394. const auto id = GetNcaIDFromMetadata(title_id, type);
  395. return id ? parser(GetFileAtID(*id), *id) : nullptr;
  396. }
  397. std::unique_ptr<NCA> RegisteredCache::GetEntry(u64 title_id, ContentRecordType type) const {
  398. const auto raw = GetEntryRaw(title_id, type);
  399. if (raw == nullptr)
  400. return nullptr;
  401. return std::make_unique<NCA>(raw, nullptr, 0);
  402. }
  403. template <typename T>
  404. void RegisteredCache::IterateAllMetadata(
  405. std::vector<T>& out, std::function<T(const CNMT&, const ContentRecord&)> proc,
  406. std::function<bool(const CNMT&, const ContentRecord&)> filter) const {
  407. for (const auto& kv : meta) {
  408. const auto& cnmt = kv.second;
  409. if (filter(cnmt, EMPTY_META_CONTENT_RECORD))
  410. out.push_back(proc(cnmt, EMPTY_META_CONTENT_RECORD));
  411. for (const auto& rec : cnmt.GetContentRecords()) {
  412. if (GetFileAtID(rec.nca_id) != nullptr && filter(cnmt, rec)) {
  413. out.push_back(proc(cnmt, rec));
  414. }
  415. }
  416. }
  417. for (const auto& kv : yuzu_meta) {
  418. const auto& cnmt = kv.second;
  419. for (const auto& rec : cnmt.GetContentRecords()) {
  420. if (GetFileAtID(rec.nca_id) != nullptr && filter(cnmt, rec)) {
  421. out.push_back(proc(cnmt, rec));
  422. }
  423. }
  424. }
  425. }
  426. std::vector<ContentProviderEntry> RegisteredCache::ListEntriesFilter(
  427. std::optional<TitleType> title_type, std::optional<ContentRecordType> record_type,
  428. std::optional<u64> title_id) const {
  429. std::vector<ContentProviderEntry> out;
  430. IterateAllMetadata<ContentProviderEntry>(
  431. out,
  432. [](const CNMT& c, const ContentRecord& r) {
  433. return ContentProviderEntry{c.GetTitleID(), r.type};
  434. },
  435. [&title_type, &record_type, &title_id](const CNMT& c, const ContentRecord& r) {
  436. if (title_type && *title_type != c.GetType())
  437. return false;
  438. if (record_type && *record_type != r.type)
  439. return false;
  440. if (title_id && *title_id != c.GetTitleID())
  441. return false;
  442. return true;
  443. });
  444. return out;
  445. }
  446. static std::shared_ptr<NCA> GetNCAFromNSPForID(const NSP& nsp, const NcaID& id) {
  447. auto file = nsp.GetFile(fmt::format("{}.nca", Common::HexToString(id, false)));
  448. if (file == nullptr) {
  449. return nullptr;
  450. }
  451. return std::make_shared<NCA>(std::move(file));
  452. }
  453. InstallResult RegisteredCache::InstallEntry(const XCI& xci, bool overwrite_if_exists,
  454. const VfsCopyFunction& copy) {
  455. return InstallEntry(*xci.GetSecurePartitionNSP(), overwrite_if_exists, copy);
  456. }
  457. InstallResult RegisteredCache::InstallEntry(const NSP& nsp, bool overwrite_if_exists,
  458. const VfsCopyFunction& copy) {
  459. const auto ncas = nsp.GetNCAsCollapsed();
  460. const auto meta_iter = std::find_if(ncas.begin(), ncas.end(), [](const auto& nca) {
  461. return nca->GetType() == NCAContentType::Meta;
  462. });
  463. if (meta_iter == ncas.end()) {
  464. LOG_ERROR(Loader, "The file you are attempting to install does not have a metadata NCA and "
  465. "is therefore malformed. Check your encryption keys.");
  466. return InstallResult::ErrorMetaFailed;
  467. }
  468. const auto meta_id_raw = (*meta_iter)->GetName().substr(0, 32);
  469. const auto meta_id_data = Common::HexStringToArray<16>(meta_id_raw);
  470. if ((*meta_iter)->GetSubdirectories().empty()) {
  471. LOG_ERROR(Loader,
  472. "The file you are attempting to install does not contain a section0 within the "
  473. "metadata NCA and is therefore malformed. Verify that the file is valid.");
  474. return InstallResult::ErrorMetaFailed;
  475. }
  476. const auto section0 = (*meta_iter)->GetSubdirectories()[0];
  477. if (section0->GetFiles().empty()) {
  478. LOG_ERROR(Loader,
  479. "The file you are attempting to install does not contain a CNMT within the "
  480. "metadata NCA and is therefore malformed. Verify that the file is valid.");
  481. return InstallResult::ErrorMetaFailed;
  482. }
  483. const auto cnmt_file = section0->GetFiles()[0];
  484. const CNMT cnmt(cnmt_file);
  485. const auto title_id = cnmt.GetTitleID();
  486. const auto version = cnmt.GetTitleVersion();
  487. if (title_id == GetBaseTitleID(title_id) && version == 0) {
  488. return InstallResult::ErrorBaseInstall;
  489. }
  490. const auto result = RemoveExistingEntry(title_id);
  491. // Install Metadata File
  492. const auto res = RawInstallNCA(**meta_iter, copy, overwrite_if_exists, meta_id_data);
  493. if (res != InstallResult::Success) {
  494. return res;
  495. }
  496. // Install all the other NCAs
  497. for (const auto& record : cnmt.GetContentRecords()) {
  498. // Ignore DeltaFragments, they are not useful to us
  499. if (record.type == ContentRecordType::DeltaFragment) {
  500. continue;
  501. }
  502. const auto nca = GetNCAFromNSPForID(nsp, record.nca_id);
  503. if (nca == nullptr) {
  504. return InstallResult::ErrorCopyFailed;
  505. }
  506. const auto res2 = RawInstallNCA(*nca, copy, overwrite_if_exists, record.nca_id);
  507. if (res2 != InstallResult::Success) {
  508. return res2;
  509. }
  510. }
  511. Refresh();
  512. if (result) {
  513. return InstallResult::OverwriteExisting;
  514. }
  515. return InstallResult::Success;
  516. }
  517. InstallResult RegisteredCache::InstallEntry(const NCA& nca, TitleType type,
  518. bool overwrite_if_exists, const VfsCopyFunction& copy) {
  519. const CNMTHeader header{
  520. .title_id = nca.GetTitleId(),
  521. .title_version = 0,
  522. .type = type,
  523. .reserved = {},
  524. .table_offset = 0x10,
  525. .number_content_entries = 1,
  526. .number_meta_entries = 0,
  527. .attributes = 0,
  528. .reserved2 = {},
  529. .is_committed = 0,
  530. .required_download_system_version = 0,
  531. .reserved3 = {},
  532. };
  533. const OptionalHeader opt_header{0, 0};
  534. ContentRecord c_rec{{}, {}, {}, GetCRTypeFromNCAType(nca.GetType()), {}};
  535. const auto& data = nca.GetBaseFile()->ReadBytes(0x100000);
  536. mbedtls_sha256_ret(data.data(), data.size(), c_rec.hash.data(), 0);
  537. std::memcpy(&c_rec.nca_id, &c_rec.hash, 16);
  538. const CNMT new_cnmt(header, opt_header, {c_rec}, {});
  539. if (!RawInstallYuzuMeta(new_cnmt)) {
  540. return InstallResult::ErrorMetaFailed;
  541. }
  542. return RawInstallNCA(nca, copy, overwrite_if_exists, c_rec.nca_id);
  543. }
  544. bool RegisteredCache::RemoveExistingEntry(u64 title_id) const {
  545. const auto delete_nca = [this](const NcaID& id) {
  546. const auto path = GetRelativePathFromNcaID(id, false, true, false);
  547. const bool isFile = dir->GetFileRelative(path) != nullptr;
  548. const bool isDir = dir->GetDirectoryRelative(path) != nullptr;
  549. if (isFile) {
  550. return dir->DeleteFile(path);
  551. } else if (isDir) {
  552. return dir->DeleteSubdirectoryRecursive(path);
  553. }
  554. return false;
  555. };
  556. // If an entry exists in the registered cache, remove it
  557. if (HasEntry(title_id, ContentRecordType::Meta)) {
  558. LOG_INFO(Loader,
  559. "Previously installed entry (v{}) for title_id={:016X} detected! "
  560. "Attempting to remove...",
  561. GetEntryVersion(title_id).value_or(0), title_id);
  562. // Get all the ncas associated with the current CNMT and delete them
  563. const auto meta_old_id =
  564. GetNcaIDFromMetadata(title_id, ContentRecordType::Meta).value_or(NcaID{});
  565. const auto program_id =
  566. GetNcaIDFromMetadata(title_id, ContentRecordType::Program).value_or(NcaID{});
  567. const auto data_id =
  568. GetNcaIDFromMetadata(title_id, ContentRecordType::Data).value_or(NcaID{});
  569. const auto control_id =
  570. GetNcaIDFromMetadata(title_id, ContentRecordType::Control).value_or(NcaID{});
  571. const auto html_id =
  572. GetNcaIDFromMetadata(title_id, ContentRecordType::HtmlDocument).value_or(NcaID{});
  573. const auto legal_id =
  574. GetNcaIDFromMetadata(title_id, ContentRecordType::LegalInformation).value_or(NcaID{});
  575. const auto deleted_meta = delete_nca(meta_old_id);
  576. const auto deleted_program = delete_nca(program_id);
  577. const auto deleted_data = delete_nca(data_id);
  578. const auto deleted_control = delete_nca(control_id);
  579. const auto deleted_html = delete_nca(html_id);
  580. const auto deleted_legal = delete_nca(legal_id);
  581. return deleted_meta && (deleted_meta || deleted_program || deleted_data ||
  582. deleted_control || deleted_html || deleted_legal);
  583. }
  584. return false;
  585. }
  586. InstallResult RegisteredCache::RawInstallNCA(const NCA& nca, const VfsCopyFunction& copy,
  587. bool overwrite_if_exists,
  588. std::optional<NcaID> override_id) {
  589. const auto in = nca.GetBaseFile();
  590. Core::Crypto::SHA256Hash hash{};
  591. // Calculate NcaID
  592. // NOTE: Because computing the SHA256 of an entire NCA is quite expensive (especially if the
  593. // game is massive), we're going to cheat and only hash the first MB of the NCA.
  594. // Also, for XCIs the NcaID matters, so if the override id isn't none, use that.
  595. NcaID id{};
  596. if (override_id) {
  597. id = *override_id;
  598. } else {
  599. const auto& data = in->ReadBytes(0x100000);
  600. mbedtls_sha256_ret(data.data(), data.size(), hash.data(), 0);
  601. memcpy(id.data(), hash.data(), 16);
  602. }
  603. std::string path = GetRelativePathFromNcaID(id, false, true, false);
  604. if (GetFileAtID(id) != nullptr && !overwrite_if_exists) {
  605. LOG_WARNING(Loader, "Attempting to overwrite existing NCA. Skipping...");
  606. return InstallResult::ErrorAlreadyExists;
  607. }
  608. if (GetFileAtID(id) != nullptr) {
  609. LOG_WARNING(Loader, "Overwriting existing NCA...");
  610. VirtualDir c_dir;
  611. { c_dir = dir->GetFileRelative(path)->GetContainingDirectory(); }
  612. c_dir->DeleteFile(Common::FS::GetFilename(path));
  613. }
  614. auto out = dir->CreateFileRelative(path);
  615. if (out == nullptr) {
  616. return InstallResult::ErrorCopyFailed;
  617. }
  618. return copy(in, out, VFS_RC_LARGE_COPY_BLOCK) ? InstallResult::Success
  619. : InstallResult::ErrorCopyFailed;
  620. }
  621. bool RegisteredCache::RawInstallYuzuMeta(const CNMT& cnmt) {
  622. // Reasoning behind this method can be found in the comment for InstallEntry, NCA overload.
  623. const auto meta_dir = dir->CreateDirectoryRelative("yuzu_meta");
  624. const auto filename = GetCNMTName(cnmt.GetType(), cnmt.GetTitleID());
  625. if (meta_dir->GetFile(filename) == nullptr) {
  626. auto out = meta_dir->CreateFile(filename);
  627. const auto buffer = cnmt.Serialize();
  628. out->Resize(buffer.size());
  629. out->WriteBytes(buffer);
  630. } else {
  631. auto out = meta_dir->GetFile(filename);
  632. CNMT old_cnmt(out);
  633. // Returns true on change
  634. if (old_cnmt.UnionRecords(cnmt)) {
  635. out->Resize(0);
  636. const auto buffer = old_cnmt.Serialize();
  637. out->Resize(buffer.size());
  638. out->WriteBytes(buffer);
  639. }
  640. }
  641. Refresh();
  642. return std::find_if(yuzu_meta.begin(), yuzu_meta.end(),
  643. [&cnmt](const std::pair<u64, CNMT>& kv) {
  644. return kv.second.GetType() == cnmt.GetType() &&
  645. kv.second.GetTitleID() == cnmt.GetTitleID();
  646. }) != yuzu_meta.end();
  647. }
  648. ContentProviderUnion::~ContentProviderUnion() = default;
  649. void ContentProviderUnion::SetSlot(ContentProviderUnionSlot slot, ContentProvider* provider) {
  650. providers[slot] = provider;
  651. }
  652. void ContentProviderUnion::ClearSlot(ContentProviderUnionSlot slot) {
  653. providers[slot] = nullptr;
  654. }
  655. void ContentProviderUnion::Refresh() {
  656. for (auto& provider : providers) {
  657. if (provider.second == nullptr)
  658. continue;
  659. provider.second->Refresh();
  660. }
  661. }
  662. bool ContentProviderUnion::HasEntry(u64 title_id, ContentRecordType type) const {
  663. for (const auto& provider : providers) {
  664. if (provider.second == nullptr)
  665. continue;
  666. if (provider.second->HasEntry(title_id, type))
  667. return true;
  668. }
  669. return false;
  670. }
  671. std::optional<u32> ContentProviderUnion::GetEntryVersion(u64 title_id) const {
  672. for (const auto& provider : providers) {
  673. if (provider.second == nullptr)
  674. continue;
  675. const auto res = provider.second->GetEntryVersion(title_id);
  676. if (res != std::nullopt)
  677. return res;
  678. }
  679. return std::nullopt;
  680. }
  681. VirtualFile ContentProviderUnion::GetEntryUnparsed(u64 title_id, ContentRecordType type) const {
  682. for (const auto& provider : providers) {
  683. if (provider.second == nullptr)
  684. continue;
  685. const auto res = provider.second->GetEntryUnparsed(title_id, type);
  686. if (res != nullptr)
  687. return res;
  688. }
  689. return nullptr;
  690. }
  691. VirtualFile ContentProviderUnion::GetEntryRaw(u64 title_id, ContentRecordType type) const {
  692. for (const auto& provider : providers) {
  693. if (provider.second == nullptr)
  694. continue;
  695. const auto res = provider.second->GetEntryRaw(title_id, type);
  696. if (res != nullptr)
  697. return res;
  698. }
  699. return nullptr;
  700. }
  701. std::unique_ptr<NCA> ContentProviderUnion::GetEntry(u64 title_id, ContentRecordType type) const {
  702. for (const auto& provider : providers) {
  703. if (provider.second == nullptr)
  704. continue;
  705. auto res = provider.second->GetEntry(title_id, type);
  706. if (res != nullptr)
  707. return res;
  708. }
  709. return nullptr;
  710. }
  711. std::vector<ContentProviderEntry> ContentProviderUnion::ListEntriesFilter(
  712. std::optional<TitleType> title_type, std::optional<ContentRecordType> record_type,
  713. std::optional<u64> title_id) const {
  714. std::vector<ContentProviderEntry> out;
  715. for (const auto& provider : providers) {
  716. if (provider.second == nullptr)
  717. continue;
  718. const auto vec = provider.second->ListEntriesFilter(title_type, record_type, title_id);
  719. std::copy(vec.begin(), vec.end(), std::back_inserter(out));
  720. }
  721. std::sort(out.begin(), out.end());
  722. out.erase(std::unique(out.begin(), out.end()), out.end());
  723. return out;
  724. }
  725. std::vector<std::pair<ContentProviderUnionSlot, ContentProviderEntry>>
  726. ContentProviderUnion::ListEntriesFilterOrigin(std::optional<ContentProviderUnionSlot> origin,
  727. std::optional<TitleType> title_type,
  728. std::optional<ContentRecordType> record_type,
  729. std::optional<u64> title_id) const {
  730. std::vector<std::pair<ContentProviderUnionSlot, ContentProviderEntry>> out;
  731. for (const auto& provider : providers) {
  732. if (provider.second == nullptr)
  733. continue;
  734. if (origin.has_value() && *origin != provider.first)
  735. continue;
  736. const auto vec = provider.second->ListEntriesFilter(title_type, record_type, title_id);
  737. std::transform(vec.begin(), vec.end(), std::back_inserter(out),
  738. [&provider](const ContentProviderEntry& entry) {
  739. return std::make_pair(provider.first, entry);
  740. });
  741. }
  742. std::sort(out.begin(), out.end());
  743. out.erase(std::unique(out.begin(), out.end()), out.end());
  744. return out;
  745. }
  746. std::optional<ContentProviderUnionSlot> ContentProviderUnion::GetSlotForEntry(
  747. u64 title_id, ContentRecordType type) const {
  748. const auto iter =
  749. std::find_if(providers.begin(), providers.end(), [title_id, type](const auto& provider) {
  750. return provider.second != nullptr && provider.second->HasEntry(title_id, type);
  751. });
  752. if (iter == providers.end()) {
  753. return std::nullopt;
  754. }
  755. return iter->first;
  756. }
  757. ManualContentProvider::~ManualContentProvider() = default;
  758. void ManualContentProvider::AddEntry(TitleType title_type, ContentRecordType content_type,
  759. u64 title_id, VirtualFile file) {
  760. entries.insert_or_assign({title_type, content_type, title_id}, file);
  761. }
  762. void ManualContentProvider::ClearAllEntries() {
  763. entries.clear();
  764. }
  765. void ManualContentProvider::Refresh() {}
  766. bool ManualContentProvider::HasEntry(u64 title_id, ContentRecordType type) const {
  767. return GetEntryRaw(title_id, type) != nullptr;
  768. }
  769. std::optional<u32> ManualContentProvider::GetEntryVersion(u64 title_id) const {
  770. return std::nullopt;
  771. }
  772. VirtualFile ManualContentProvider::GetEntryUnparsed(u64 title_id, ContentRecordType type) const {
  773. return GetEntryRaw(title_id, type);
  774. }
  775. VirtualFile ManualContentProvider::GetEntryRaw(u64 title_id, ContentRecordType type) const {
  776. const auto iter =
  777. std::find_if(entries.begin(), entries.end(), [title_id, type](const auto& entry) {
  778. const auto content_type = std::get<1>(entry.first);
  779. const auto e_title_id = std::get<2>(entry.first);
  780. return content_type == type && e_title_id == title_id;
  781. });
  782. if (iter == entries.end())
  783. return nullptr;
  784. return iter->second;
  785. }
  786. std::unique_ptr<NCA> ManualContentProvider::GetEntry(u64 title_id, ContentRecordType type) const {
  787. const auto res = GetEntryRaw(title_id, type);
  788. if (res == nullptr)
  789. return nullptr;
  790. return std::make_unique<NCA>(res, nullptr, 0);
  791. }
  792. std::vector<ContentProviderEntry> ManualContentProvider::ListEntriesFilter(
  793. std::optional<TitleType> title_type, std::optional<ContentRecordType> record_type,
  794. std::optional<u64> title_id) const {
  795. std::vector<ContentProviderEntry> out;
  796. for (const auto& entry : entries) {
  797. const auto [e_title_type, e_content_type, e_title_id] = entry.first;
  798. if ((title_type == std::nullopt || e_title_type == *title_type) &&
  799. (record_type == std::nullopt || e_content_type == *record_type) &&
  800. (title_id == std::nullopt || e_title_id == *title_id)) {
  801. out.emplace_back(ContentProviderEntry{e_title_id, e_content_type});
  802. }
  803. }
  804. std::sort(out.begin(), out.end());
  805. out.erase(std::unique(out.begin(), out.end()), out.end());
  806. return out;
  807. }
  808. } // namespace FileSys