submission_package.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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<std::pair<TitleType, ContentRecordType>, std::shared_ptr<NCA>>>
  120. NSP::GetNCAs() const {
  121. return ncas;
  122. }
  123. std::shared_ptr<NCA> NSP::GetNCA(u64 title_id, ContentRecordType type, TitleType title_type) const {
  124. if (extracted)
  125. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  126. const auto title_id_iter = ncas.find(title_id);
  127. if (title_id_iter == ncas.end())
  128. return nullptr;
  129. const auto type_iter = title_id_iter->second.find({title_type, type});
  130. if (type_iter == title_id_iter->second.end())
  131. return nullptr;
  132. return type_iter->second;
  133. }
  134. VirtualFile NSP::GetNCAFile(u64 title_id, ContentRecordType type, TitleType title_type) const {
  135. if (extracted)
  136. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  137. const auto nca = GetNCA(title_id, type);
  138. if (nca != nullptr)
  139. return nca->GetBaseFile();
  140. return nullptr;
  141. }
  142. std::vector<Core::Crypto::Key128> NSP::GetTitlekey() const {
  143. if (extracted)
  144. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  145. std::vector<Core::Crypto::Key128> out;
  146. for (const auto& ticket_file : ticket_files) {
  147. if (ticket_file == nullptr ||
  148. ticket_file->GetSize() <
  149. Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) {
  150. continue;
  151. }
  152. out.emplace_back();
  153. ticket_file->Read(out.back().data(), out.back().size(),
  154. Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET);
  155. }
  156. return out;
  157. }
  158. std::vector<VirtualFile> NSP::GetFiles() const {
  159. return pfs->GetFiles();
  160. }
  161. std::vector<VirtualDir> NSP::GetSubdirectories() const {
  162. return pfs->GetSubdirectories();
  163. }
  164. std::string NSP::GetName() const {
  165. return file->GetName();
  166. }
  167. VirtualDir NSP::GetParentDirectory() const {
  168. return file->GetContainingDirectory();
  169. }
  170. void NSP::InitializeExeFSAndRomFS(const std::vector<VirtualFile>& files) {
  171. exefs = pfs;
  172. const auto romfs_iter = std::find_if(files.begin(), files.end(), [](const VirtualFile& file) {
  173. return file->GetName().rfind(".romfs") != std::string::npos;
  174. });
  175. if (romfs_iter == files.end()) {
  176. return;
  177. }
  178. romfs = *romfs_iter;
  179. }
  180. void NSP::ReadNCAs(const std::vector<VirtualFile>& files) {
  181. for (const auto& outer_file : files) {
  182. if (outer_file->GetName().substr(outer_file->GetName().size() - 9) != ".cnmt.nca") {
  183. continue;
  184. }
  185. const auto nca = std::make_shared<NCA>(outer_file);
  186. if (nca->GetStatus() != Loader::ResultStatus::Success) {
  187. program_status[nca->GetTitleId()] = nca->GetStatus();
  188. continue;
  189. }
  190. const auto section0 = nca->GetSubdirectories()[0];
  191. for (const auto& inner_file : section0->GetFiles()) {
  192. if (inner_file->GetExtension() != "cnmt")
  193. continue;
  194. const CNMT cnmt(inner_file);
  195. auto& ncas_title = ncas[cnmt.GetTitleID()];
  196. ncas_title[{cnmt.GetType(), ContentRecordType::Meta}] = nca;
  197. for (const auto& rec : cnmt.GetContentRecords()) {
  198. const auto id_string = Common::HexArrayToString(rec.nca_id, false);
  199. const auto next_file = pfs->GetFile(fmt::format("{}.nca", id_string));
  200. if (next_file == nullptr) {
  201. LOG_WARNING(Service_FS,
  202. "NCA with ID {}.nca is listed in content metadata, but cannot "
  203. "be found in PFS. NSP appears to be corrupted.",
  204. id_string);
  205. continue;
  206. }
  207. auto next_nca = std::make_shared<NCA>(next_file, nullptr, 0, keys);
  208. if (next_nca->GetType() == NCAContentType::Program)
  209. program_status[cnmt.GetTitleID()] = next_nca->GetStatus();
  210. if (next_nca->GetStatus() == Loader::ResultStatus::Success ||
  211. (next_nca->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS &&
  212. (cnmt.GetTitleID() & 0x800) != 0)) {
  213. ncas_title[{cnmt.GetType(), rec.type}] = std::move(next_nca);
  214. }
  215. }
  216. break;
  217. }
  218. }
  219. }
  220. } // namespace FileSys