submission_package.cpp 7.2 KB

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