submission_package.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <fmt/ostream.h>
  6. #include "common/hex_util.h"
  7. #include "common/logging/log.h"
  8. #include "core/crypto/key_manager.h"
  9. #include "core/file_sys/content_archive.h"
  10. #include "core/file_sys/nca_metadata.h"
  11. #include "core/file_sys/partition_filesystem.h"
  12. #include "core/file_sys/program_metadata.h"
  13. #include "core/file_sys/submission_package.h"
  14. #include "core/loader/loader.h"
  15. namespace FileSys {
  16. NSP::NSP(VirtualFile file_, u64 title_id_, std::size_t program_index_)
  17. : file(std::move(file_)), expected_program_id(title_id_),
  18. program_index(program_index_), status{Loader::ResultStatus::Success},
  19. pfs(std::make_shared<PartitionFilesystem>(file)), keys{Core::Crypto::KeyManager::Instance()} {
  20. if (pfs->GetStatus() != Loader::ResultStatus::Success) {
  21. status = pfs->GetStatus();
  22. return;
  23. }
  24. const auto files = pfs->GetFiles();
  25. if (IsDirectoryExeFS(pfs)) {
  26. extracted = true;
  27. InitializeExeFSAndRomFS(files);
  28. return;
  29. }
  30. SetTicketKeys(files);
  31. ReadNCAs(files);
  32. }
  33. NSP::~NSP() = default;
  34. Loader::ResultStatus NSP::GetStatus() const {
  35. return status;
  36. }
  37. Loader::ResultStatus NSP::GetProgramStatus() const {
  38. if (IsExtractedType() && GetExeFS() != nullptr && FileSys::IsDirectoryExeFS(GetExeFS())) {
  39. return Loader::ResultStatus::Success;
  40. }
  41. const auto iter = program_status.find(GetProgramTitleID());
  42. if (iter == program_status.end())
  43. return Loader::ResultStatus::ErrorNSPMissingProgramNCA;
  44. return iter->second;
  45. }
  46. u64 NSP::GetProgramTitleID() const {
  47. if (IsExtractedType()) {
  48. return GetExtractedTitleID() + program_index;
  49. }
  50. auto program_id = expected_program_id;
  51. if (program_id == 0) {
  52. if (!program_status.empty()) {
  53. program_id = program_status.begin()->first;
  54. }
  55. }
  56. program_id = program_id + program_index;
  57. if (program_status.find(program_id) != program_status.end()) {
  58. return program_id;
  59. }
  60. const auto ids = GetProgramTitleIDs();
  61. const auto iter =
  62. std::find_if(ids.begin(), ids.end(), [](u64 tid) { return (tid & 0x800) == 0; });
  63. return iter == ids.end() ? 0 : *iter;
  64. }
  65. u64 NSP::GetExtractedTitleID() const {
  66. if (GetExeFS() == nullptr || !IsDirectoryExeFS(GetExeFS())) {
  67. return 0;
  68. }
  69. ProgramMetadata meta;
  70. if (meta.Load(GetExeFS()->GetFile("main.npdm")) == Loader::ResultStatus::Success) {
  71. return meta.GetTitleID();
  72. } else {
  73. return 0;
  74. }
  75. }
  76. std::vector<u64> NSP::GetProgramTitleIDs() const {
  77. if (IsExtractedType()) {
  78. return {GetExtractedTitleID()};
  79. }
  80. std::vector<u64> out{program_ids.cbegin(), program_ids.cend()};
  81. return out;
  82. }
  83. bool NSP::IsExtractedType() const {
  84. return extracted;
  85. }
  86. VirtualFile NSP::GetRomFS() const {
  87. return romfs;
  88. }
  89. VirtualDir NSP::GetExeFS() const {
  90. return exefs;
  91. }
  92. std::vector<std::shared_ptr<NCA>> NSP::GetNCAsCollapsed() const {
  93. if (extracted)
  94. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  95. std::vector<std::shared_ptr<NCA>> out;
  96. for (const auto& map : ncas) {
  97. for (const auto& inner_map : map.second)
  98. out.push_back(inner_map.second);
  99. }
  100. return out;
  101. }
  102. std::multimap<u64, std::shared_ptr<NCA>> NSP::GetNCAsByTitleID() const {
  103. if (extracted)
  104. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  105. std::multimap<u64, std::shared_ptr<NCA>> out;
  106. for (const auto& map : ncas) {
  107. for (const auto& inner_map : map.second)
  108. out.emplace(map.first, inner_map.second);
  109. }
  110. return out;
  111. }
  112. std::map<u64, std::map<std::pair<TitleType, ContentRecordType>, std::shared_ptr<NCA>>>
  113. NSP::GetNCAs() const {
  114. return ncas;
  115. }
  116. std::shared_ptr<NCA> NSP::GetNCA(u64 title_id, ContentRecordType type, TitleType title_type) const {
  117. if (extracted)
  118. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  119. const auto title_id_iter = ncas.find(title_id);
  120. if (title_id_iter == ncas.end())
  121. return nullptr;
  122. const auto type_iter = title_id_iter->second.find({title_type, type});
  123. if (type_iter == title_id_iter->second.end())
  124. return nullptr;
  125. return type_iter->second;
  126. }
  127. VirtualFile NSP::GetNCAFile(u64 title_id, ContentRecordType type, TitleType title_type) const {
  128. if (extracted)
  129. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  130. const auto nca = GetNCA(title_id, type, title_type);
  131. if (nca != nullptr)
  132. return nca->GetBaseFile();
  133. return nullptr;
  134. }
  135. std::vector<VirtualFile> NSP::GetFiles() const {
  136. return pfs->GetFiles();
  137. }
  138. std::vector<VirtualDir> NSP::GetSubdirectories() const {
  139. return pfs->GetSubdirectories();
  140. }
  141. std::string NSP::GetName() const {
  142. return file->GetName();
  143. }
  144. VirtualDir NSP::GetParentDirectory() const {
  145. return file->GetContainingDirectory();
  146. }
  147. void NSP::SetTicketKeys(const std::vector<VirtualFile>& files) {
  148. for (const auto& ticket_file : files) {
  149. if (ticket_file == nullptr) {
  150. continue;
  151. }
  152. if (ticket_file->GetExtension() != "tik") {
  153. continue;
  154. }
  155. auto ticket = Core::Crypto::Ticket::Read(ticket_file);
  156. if (!keys.AddTicket(ticket)) {
  157. LOG_WARNING(Common_Filesystem, "Could not load NSP ticket {}", ticket_file->GetName());
  158. continue;
  159. }
  160. }
  161. }
  162. void NSP::InitializeExeFSAndRomFS(const std::vector<VirtualFile>& files) {
  163. exefs = pfs;
  164. const auto iter = std::find_if(files.begin(), files.end(), [](const VirtualFile& entry) {
  165. return entry->GetName().rfind(".romfs") != std::string::npos;
  166. });
  167. if (iter == files.end()) {
  168. return;
  169. }
  170. romfs = *iter;
  171. }
  172. void NSP::ReadNCAs(const std::vector<VirtualFile>& files) {
  173. for (const auto& outer_file : files) {
  174. if (outer_file->GetName().size() < 9 ||
  175. outer_file->GetName().substr(outer_file->GetName().size() - 9) != ".cnmt.nca") {
  176. continue;
  177. }
  178. const auto nca = std::make_shared<NCA>(outer_file);
  179. if (nca->GetStatus() != Loader::ResultStatus::Success || nca->GetSubdirectories().empty()) {
  180. program_status[nca->GetTitleId()] = nca->GetStatus();
  181. continue;
  182. }
  183. const auto section0 = nca->GetSubdirectories()[0];
  184. for (const auto& inner_file : section0->GetFiles()) {
  185. if (inner_file->GetExtension() != "cnmt") {
  186. continue;
  187. }
  188. const CNMT cnmt(inner_file);
  189. ncas[cnmt.GetTitleID()][{cnmt.GetType(), ContentRecordType::Meta}] = nca;
  190. for (const auto& rec : cnmt.GetContentRecords()) {
  191. const auto id_string = Common::HexToString(rec.nca_id, false);
  192. auto next_file = pfs->GetFile(fmt::format("{}.nca", id_string));
  193. if (next_file == nullptr) {
  194. if (rec.type != ContentRecordType::DeltaFragment) {
  195. LOG_WARNING(Service_FS,
  196. "NCA with ID {}.nca is listed in content metadata, but cannot "
  197. "be found in PFS. NSP appears to be corrupted.",
  198. id_string);
  199. }
  200. continue;
  201. }
  202. auto next_nca = std::make_shared<NCA>(std::move(next_file));
  203. if (next_nca->GetType() == NCAContentType::Program) {
  204. program_status[next_nca->GetTitleId()] = next_nca->GetStatus();
  205. program_ids.insert(next_nca->GetTitleId() & 0xFFFFFFFFFFFFF000);
  206. }
  207. if (next_nca->GetStatus() != Loader::ResultStatus::Success &&
  208. next_nca->GetStatus() != Loader::ResultStatus::ErrorMissingBKTRBaseRomFS) {
  209. continue;
  210. }
  211. // If the last 3 hexadecimal digits of the CNMT TitleID is 0x800 or is missing the
  212. // BKTRBaseRomFS, this is an update NCA. Otherwise, this is a base NCA.
  213. if ((cnmt.GetTitleID() & 0x800) != 0 ||
  214. next_nca->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS) {
  215. // If the last 3 hexadecimal digits of the NCA's TitleID is between 0x1 and
  216. // 0x7FF, this is a multi-program update NCA. Otherwise, this is a regular
  217. // update NCA.
  218. if ((next_nca->GetTitleId() & 0x7FF) != 0 &&
  219. (next_nca->GetTitleId() & 0x800) == 0) {
  220. ncas[next_nca->GetTitleId()][{cnmt.GetType(), rec.type}] =
  221. std::move(next_nca);
  222. } else {
  223. ncas[cnmt.GetTitleID()][{cnmt.GetType(), rec.type}] = std::move(next_nca);
  224. }
  225. } else {
  226. ncas[next_nca->GetTitleId()][{cnmt.GetType(), rec.type}] = std::move(next_nca);
  227. }
  228. }
  229. break;
  230. }
  231. }
  232. }
  233. } // namespace FileSys