submission_package.cpp 8.0 KB

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