submission_package.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <string_view>
  7. #include <fmt/ostream.h>
  8. #include "common/hex_util.h"
  9. #include "common/logging/log.h"
  10. #include "core/crypto/key_manager.h"
  11. #include "core/file_sys/content_archive.h"
  12. #include "core/file_sys/nca_metadata.h"
  13. #include "core/file_sys/partition_filesystem.h"
  14. #include "core/file_sys/submission_package.h"
  15. #include "core/loader/loader.h"
  16. namespace FileSys {
  17. namespace {
  18. void SetTicketKeys(const std::vector<VirtualFile>& files) {
  19. Core::Crypto::KeyManager keys;
  20. for (const auto& ticket_file : files) {
  21. if (ticket_file == nullptr) {
  22. continue;
  23. }
  24. if (ticket_file->GetExtension() != "tik") {
  25. continue;
  26. }
  27. if (ticket_file->GetSize() <
  28. Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) {
  29. continue;
  30. }
  31. Core::Crypto::Key128 key{};
  32. ticket_file->Read(key.data(), key.size(), Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET);
  33. std::string_view name_only(ticket_file->GetName());
  34. name_only.remove_suffix(4);
  35. const auto rights_id_raw = Common::HexStringToArray<16>(name_only);
  36. u128 rights_id;
  37. std::memcpy(rights_id.data(), rights_id_raw.data(), sizeof(u128));
  38. keys.SetKey(Core::Crypto::S128KeyType::Titlekey, key, rights_id[1], rights_id[0]);
  39. }
  40. }
  41. } // Anonymous namespace
  42. NSP::NSP(VirtualFile file_)
  43. : file(std::move(file_)), status{Loader::ResultStatus::Success},
  44. pfs(std::make_shared<PartitionFilesystem>(file)) {
  45. if (pfs->GetStatus() != Loader::ResultStatus::Success) {
  46. status = pfs->GetStatus();
  47. return;
  48. }
  49. const auto files = pfs->GetFiles();
  50. if (IsDirectoryExeFS(pfs)) {
  51. extracted = true;
  52. InitializeExeFSAndRomFS(files);
  53. return;
  54. }
  55. SetTicketKeys(files);
  56. ReadNCAs(files);
  57. }
  58. NSP::~NSP() = default;
  59. Loader::ResultStatus NSP::GetStatus() const {
  60. return status;
  61. }
  62. Loader::ResultStatus NSP::GetProgramStatus(u64 title_id) const {
  63. const auto iter = program_status.find(title_id);
  64. if (iter == program_status.end())
  65. return Loader::ResultStatus::ErrorNSPMissingProgramNCA;
  66. return iter->second;
  67. }
  68. u64 NSP::GetFirstTitleID() const {
  69. if (program_status.empty())
  70. return 0;
  71. return program_status.begin()->first;
  72. }
  73. u64 NSP::GetProgramTitleID() const {
  74. const auto out = GetFirstTitleID();
  75. if ((out & 0x800) == 0)
  76. return out;
  77. const auto ids = GetTitleIDs();
  78. const auto iter =
  79. std::find_if(ids.begin(), ids.end(), [](u64 tid) { return (tid & 0x800) == 0; });
  80. return iter == ids.end() ? out : *iter;
  81. }
  82. std::vector<u64> NSP::GetTitleIDs() const {
  83. std::vector<u64> out;
  84. out.reserve(ncas.size());
  85. for (const auto& kv : ncas)
  86. out.push_back(kv.first);
  87. return out;
  88. }
  89. bool NSP::IsExtractedType() const {
  90. return extracted;
  91. }
  92. VirtualFile NSP::GetRomFS() const {
  93. return romfs;
  94. }
  95. VirtualDir NSP::GetExeFS() const {
  96. return exefs;
  97. }
  98. std::vector<std::shared_ptr<NCA>> NSP::GetNCAsCollapsed() const {
  99. if (extracted)
  100. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  101. std::vector<std::shared_ptr<NCA>> out;
  102. for (const auto& map : ncas) {
  103. for (const auto& inner_map : map.second)
  104. out.push_back(inner_map.second);
  105. }
  106. return out;
  107. }
  108. std::multimap<u64, std::shared_ptr<NCA>> NSP::GetNCAsByTitleID() const {
  109. if (extracted)
  110. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  111. std::multimap<u64, std::shared_ptr<NCA>> out;
  112. for (const auto& map : ncas) {
  113. for (const auto& inner_map : map.second)
  114. out.emplace(map.first, inner_map.second);
  115. }
  116. return out;
  117. }
  118. std::map<u64, std::map<ContentRecordType, std::shared_ptr<NCA>>> NSP::GetNCAs() const {
  119. return ncas;
  120. }
  121. std::shared_ptr<NCA> NSP::GetNCA(u64 title_id, ContentRecordType type) const {
  122. if (extracted)
  123. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  124. const auto title_id_iter = ncas.find(title_id);
  125. if (title_id_iter == ncas.end())
  126. return nullptr;
  127. const auto type_iter = title_id_iter->second.find(type);
  128. if (type_iter == title_id_iter->second.end())
  129. return nullptr;
  130. return type_iter->second;
  131. }
  132. VirtualFile NSP::GetNCAFile(u64 title_id, ContentRecordType type) const {
  133. if (extracted)
  134. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  135. const auto nca = GetNCA(title_id, type);
  136. if (nca != nullptr)
  137. return nca->GetBaseFile();
  138. return nullptr;
  139. }
  140. std::vector<Core::Crypto::Key128> NSP::GetTitlekey() const {
  141. if (extracted)
  142. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  143. std::vector<Core::Crypto::Key128> out;
  144. for (const auto& ticket_file : ticket_files) {
  145. if (ticket_file == nullptr ||
  146. ticket_file->GetSize() <
  147. Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) {
  148. continue;
  149. }
  150. out.emplace_back();
  151. ticket_file->Read(out.back().data(), out.back().size(),
  152. Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET);
  153. }
  154. return out;
  155. }
  156. std::vector<VirtualFile> NSP::GetFiles() const {
  157. return pfs->GetFiles();
  158. }
  159. std::vector<VirtualDir> NSP::GetSubdirectories() const {
  160. return pfs->GetSubdirectories();
  161. }
  162. std::string NSP::GetName() const {
  163. return file->GetName();
  164. }
  165. VirtualDir NSP::GetParentDirectory() const {
  166. return file->GetContainingDirectory();
  167. }
  168. bool NSP::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
  169. return false;
  170. }
  171. void NSP::InitializeExeFSAndRomFS(const std::vector<VirtualFile>& files) {
  172. exefs = pfs;
  173. const auto romfs_iter = std::find_if(files.begin(), files.end(), [](const VirtualFile& file) {
  174. return file->GetName().rfind(".romfs") != std::string::npos;
  175. });
  176. if (romfs_iter == files.end()) {
  177. return;
  178. }
  179. romfs = *romfs_iter;
  180. }
  181. void NSP::ReadNCAs(const std::vector<VirtualFile>& files) {
  182. for (const auto& outer_file : files) {
  183. if (outer_file->GetName().substr(outer_file->GetName().size() - 9) != ".cnmt.nca") {
  184. continue;
  185. }
  186. const auto nca = std::make_shared<NCA>(outer_file);
  187. if (nca->GetStatus() != Loader::ResultStatus::Success) {
  188. program_status[nca->GetTitleId()] = nca->GetStatus();
  189. continue;
  190. }
  191. const auto section0 = nca->GetSubdirectories()[0];
  192. for (const auto& inner_file : section0->GetFiles()) {
  193. if (inner_file->GetExtension() != "cnmt")
  194. continue;
  195. const CNMT cnmt(inner_file);
  196. auto& ncas_title = ncas[cnmt.GetTitleID()];
  197. ncas_title[ContentRecordType::Meta] = nca;
  198. for (const auto& rec : cnmt.GetContentRecords()) {
  199. const auto id_string = Common::HexArrayToString(rec.nca_id, false);
  200. const auto next_file = pfs->GetFile(fmt::format("{}.nca", id_string));
  201. if (next_file == nullptr) {
  202. LOG_WARNING(Service_FS,
  203. "NCA with ID {}.nca is listed in content metadata, but cannot "
  204. "be found in PFS. NSP appears to be corrupted.",
  205. id_string);
  206. continue;
  207. }
  208. auto next_nca = std::make_shared<NCA>(next_file);
  209. if (next_nca->GetType() == NCAContentType::Program)
  210. program_status[cnmt.GetTitleID()] = next_nca->GetStatus();
  211. if (next_nca->GetStatus() == Loader::ResultStatus::Success)
  212. ncas_title[rec.type] = std::move(next_nca);
  213. }
  214. break;
  215. }
  216. }
  217. }
  218. } // namespace FileSys