submission_package.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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<Core::Crypto::Key128> NSP::GetTitlekey() const {
  136. if (extracted)
  137. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  138. std::vector<Core::Crypto::Key128> out;
  139. for (const auto& ticket_file : ticket_files) {
  140. if (ticket_file == nullptr ||
  141. ticket_file->GetSize() <
  142. Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) {
  143. continue;
  144. }
  145. out.emplace_back();
  146. ticket_file->Read(out.back().data(), out.back().size(),
  147. Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET);
  148. }
  149. return out;
  150. }
  151. std::vector<VirtualFile> NSP::GetFiles() const {
  152. return pfs->GetFiles();
  153. }
  154. std::vector<VirtualDir> NSP::GetSubdirectories() const {
  155. return pfs->GetSubdirectories();
  156. }
  157. std::string NSP::GetName() const {
  158. return file->GetName();
  159. }
  160. VirtualDir NSP::GetParentDirectory() const {
  161. return file->GetContainingDirectory();
  162. }
  163. void NSP::SetTicketKeys(const std::vector<VirtualFile>& files) {
  164. for (const auto& ticket_file : files) {
  165. if (ticket_file == nullptr) {
  166. continue;
  167. }
  168. if (ticket_file->GetExtension() != "tik") {
  169. continue;
  170. }
  171. if (ticket_file->GetSize() <
  172. Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) {
  173. continue;
  174. }
  175. Core::Crypto::Key128 key{};
  176. ticket_file->Read(key.data(), key.size(), Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET);
  177. // We get the name without the extension in order to create the rights ID.
  178. std::string name_only(ticket_file->GetName());
  179. name_only.erase(name_only.size() - 4);
  180. const auto rights_id_raw = Common::HexStringToArray<16>(name_only);
  181. u128 rights_id;
  182. std::memcpy(rights_id.data(), rights_id_raw.data(), sizeof(u128));
  183. keys.SetKey(Core::Crypto::S128KeyType::Titlekey, key, rights_id[1], rights_id[0]);
  184. }
  185. }
  186. void NSP::InitializeExeFSAndRomFS(const std::vector<VirtualFile>& files) {
  187. exefs = pfs;
  188. const auto iter = std::find_if(files.begin(), files.end(), [](const VirtualFile& entry) {
  189. return entry->GetName().rfind(".romfs") != std::string::npos;
  190. });
  191. if (iter == files.end()) {
  192. return;
  193. }
  194. romfs = *iter;
  195. }
  196. void NSP::ReadNCAs(const std::vector<VirtualFile>& files) {
  197. for (const auto& outer_file : files) {
  198. if (outer_file->GetName().size() < 9 ||
  199. outer_file->GetName().substr(outer_file->GetName().size() - 9) != ".cnmt.nca") {
  200. continue;
  201. }
  202. const auto nca = std::make_shared<NCA>(outer_file);
  203. if (nca->GetStatus() != Loader::ResultStatus::Success || nca->GetSubdirectories().empty()) {
  204. program_status[nca->GetTitleId()] = nca->GetStatus();
  205. continue;
  206. }
  207. const auto section0 = nca->GetSubdirectories()[0];
  208. for (const auto& inner_file : section0->GetFiles()) {
  209. if (inner_file->GetExtension() != "cnmt") {
  210. continue;
  211. }
  212. const CNMT cnmt(inner_file);
  213. ncas[cnmt.GetTitleID()][{cnmt.GetType(), ContentRecordType::Meta}] = nca;
  214. for (const auto& rec : cnmt.GetContentRecords()) {
  215. const auto id_string = Common::HexToString(rec.nca_id, false);
  216. auto next_file = pfs->GetFile(fmt::format("{}.nca", id_string));
  217. if (next_file == nullptr) {
  218. if (rec.type != ContentRecordType::DeltaFragment) {
  219. LOG_WARNING(Service_FS,
  220. "NCA with ID {}.nca is listed in content metadata, but cannot "
  221. "be found in PFS. NSP appears to be corrupted.",
  222. id_string);
  223. }
  224. continue;
  225. }
  226. auto next_nca = std::make_shared<NCA>(std::move(next_file));
  227. if (next_nca->GetType() == NCAContentType::Program) {
  228. program_status[next_nca->GetTitleId()] = next_nca->GetStatus();
  229. program_ids.insert(next_nca->GetTitleId() & 0xFFFFFFFFFFFFF000);
  230. }
  231. if (next_nca->GetStatus() != Loader::ResultStatus::Success &&
  232. next_nca->GetStatus() != Loader::ResultStatus::ErrorMissingBKTRBaseRomFS) {
  233. continue;
  234. }
  235. // If the last 3 hexadecimal digits of the CNMT TitleID is 0x800 or is missing the
  236. // BKTRBaseRomFS, this is an update NCA. Otherwise, this is a base NCA.
  237. if ((cnmt.GetTitleID() & 0x800) != 0 ||
  238. next_nca->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS) {
  239. // If the last 3 hexadecimal digits of the NCA's TitleID is between 0x1 and
  240. // 0x7FF, this is a multi-program update NCA. Otherwise, this is a regular
  241. // update NCA.
  242. if ((next_nca->GetTitleId() & 0x7FF) != 0 &&
  243. (next_nca->GetTitleId() & 0x800) == 0) {
  244. ncas[next_nca->GetTitleId()][{cnmt.GetType(), rec.type}] =
  245. std::move(next_nca);
  246. } else {
  247. ncas[cnmt.GetTitleID()][{cnmt.GetType(), rec.type}] = std::move(next_nca);
  248. }
  249. } else {
  250. ncas[next_nca->GetTitleId()][{cnmt.GetType(), rec.type}] = std::move(next_nca);
  251. }
  252. }
  253. break;
  254. }
  255. }
  256. }
  257. } // namespace FileSys