submission_package.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/program_metadata.h"
  15. #include "core/file_sys/submission_package.h"
  16. #include "core/loader/loader.h"
  17. namespace FileSys {
  18. namespace {
  19. void SetTicketKeys(const std::vector<VirtualFile>& files) {
  20. Core::Crypto::KeyManager& keys = Core::Crypto::KeyManager::instance();
  21. for (const auto& ticket_file : files) {
  22. if (ticket_file == nullptr) {
  23. continue;
  24. }
  25. if (ticket_file->GetExtension() != "tik") {
  26. continue;
  27. }
  28. if (ticket_file->GetSize() <
  29. Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) {
  30. continue;
  31. }
  32. Core::Crypto::Key128 key{};
  33. ticket_file->Read(key.data(), key.size(), Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET);
  34. // We get the name without the extension in order to create the rights ID.
  35. std::string name_only(ticket_file->GetName());
  36. name_only.erase(name_only.size() - 4);
  37. const auto rights_id_raw = Common::HexStringToArray<16>(name_only);
  38. u128 rights_id;
  39. std::memcpy(rights_id.data(), rights_id_raw.data(), sizeof(u128));
  40. keys.SetKey(Core::Crypto::S128KeyType::Titlekey, key, rights_id[1], rights_id[0]);
  41. }
  42. }
  43. } // Anonymous namespace
  44. NSP::NSP(VirtualFile file_)
  45. : file(std::move(file_)), status{Loader::ResultStatus::Success},
  46. pfs(std::make_shared<PartitionFilesystem>(file)) {
  47. if (pfs->GetStatus() != Loader::ResultStatus::Success) {
  48. status = pfs->GetStatus();
  49. return;
  50. }
  51. const auto files = pfs->GetFiles();
  52. if (IsDirectoryExeFS(pfs)) {
  53. extracted = true;
  54. InitializeExeFSAndRomFS(files);
  55. return;
  56. }
  57. SetTicketKeys(files);
  58. ReadNCAs(files);
  59. }
  60. NSP::~NSP() = default;
  61. Loader::ResultStatus NSP::GetStatus() const {
  62. return status;
  63. }
  64. Loader::ResultStatus NSP::GetProgramStatus(u64 title_id) const {
  65. if (IsExtractedType() && GetExeFS() != nullptr && FileSys::IsDirectoryExeFS(GetExeFS())) {
  66. return Loader::ResultStatus::Success;
  67. }
  68. const auto iter = program_status.find(title_id);
  69. if (iter == program_status.end())
  70. return Loader::ResultStatus::ErrorNSPMissingProgramNCA;
  71. return iter->second;
  72. }
  73. u64 NSP::GetFirstTitleID() const {
  74. if (IsExtractedType()) {
  75. return GetProgramTitleID();
  76. }
  77. if (program_status.empty())
  78. return 0;
  79. return program_status.begin()->first;
  80. }
  81. u64 NSP::GetProgramTitleID() const {
  82. if (IsExtractedType()) {
  83. if (GetExeFS() == nullptr || !IsDirectoryExeFS(GetExeFS())) {
  84. return 0;
  85. }
  86. ProgramMetadata meta;
  87. if (meta.Load(GetExeFS()->GetFile("main.npdm")) == Loader::ResultStatus::Success) {
  88. return meta.GetTitleID();
  89. } else {
  90. return 0;
  91. }
  92. }
  93. const auto out = GetFirstTitleID();
  94. if ((out & 0x800) == 0)
  95. return out;
  96. const auto ids = GetTitleIDs();
  97. const auto iter =
  98. std::find_if(ids.begin(), ids.end(), [](u64 tid) { return (tid & 0x800) == 0; });
  99. return iter == ids.end() ? out : *iter;
  100. }
  101. std::vector<u64> NSP::GetTitleIDs() const {
  102. if (IsExtractedType()) {
  103. return {GetProgramTitleID()};
  104. }
  105. std::vector<u64> out;
  106. out.reserve(ncas.size());
  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.emplace(map.first, inner_map.second);
  137. }
  138. return out;
  139. }
  140. std::map<u64, std::map<std::pair<TitleType, ContentRecordType>, std::shared_ptr<NCA>>>
  141. NSP::GetNCAs() const {
  142. return ncas;
  143. }
  144. std::shared_ptr<NCA> NSP::GetNCA(u64 title_id, ContentRecordType type, TitleType title_type) const {
  145. if (extracted)
  146. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  147. const auto title_id_iter = ncas.find(title_id);
  148. if (title_id_iter == ncas.end())
  149. return nullptr;
  150. const auto type_iter = title_id_iter->second.find({title_type, type});
  151. if (type_iter == title_id_iter->second.end())
  152. return nullptr;
  153. return type_iter->second;
  154. }
  155. VirtualFile NSP::GetNCAFile(u64 title_id, ContentRecordType type, TitleType title_type) const {
  156. if (extracted)
  157. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  158. const auto nca = GetNCA(title_id, type);
  159. if (nca != nullptr)
  160. return nca->GetBaseFile();
  161. return nullptr;
  162. }
  163. std::vector<Core::Crypto::Key128> NSP::GetTitlekey() const {
  164. if (extracted)
  165. LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
  166. std::vector<Core::Crypto::Key128> out;
  167. for (const auto& ticket_file : ticket_files) {
  168. if (ticket_file == nullptr ||
  169. ticket_file->GetSize() <
  170. Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) {
  171. continue;
  172. }
  173. out.emplace_back();
  174. ticket_file->Read(out.back().data(), out.back().size(),
  175. Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET);
  176. }
  177. return out;
  178. }
  179. std::vector<VirtualFile> NSP::GetFiles() const {
  180. return pfs->GetFiles();
  181. }
  182. std::vector<VirtualDir> NSP::GetSubdirectories() const {
  183. return pfs->GetSubdirectories();
  184. }
  185. std::string NSP::GetName() const {
  186. return file->GetName();
  187. }
  188. VirtualDir NSP::GetParentDirectory() const {
  189. return file->GetContainingDirectory();
  190. }
  191. void NSP::InitializeExeFSAndRomFS(const std::vector<VirtualFile>& files) {
  192. exefs = pfs;
  193. const auto romfs_iter = std::find_if(files.begin(), files.end(), [](const VirtualFile& file) {
  194. return file->GetName().rfind(".romfs") != std::string::npos;
  195. });
  196. if (romfs_iter == files.end()) {
  197. return;
  198. }
  199. romfs = *romfs_iter;
  200. }
  201. void NSP::ReadNCAs(const std::vector<VirtualFile>& files) {
  202. for (const auto& outer_file : files) {
  203. if (outer_file->GetName().size() < 9 ||
  204. outer_file->GetName().substr(outer_file->GetName().size() - 9) != ".cnmt.nca") {
  205. continue;
  206. }
  207. const auto nca = std::make_shared<NCA>(outer_file);
  208. if (nca->GetStatus() != Loader::ResultStatus::Success) {
  209. program_status[nca->GetTitleId()] = nca->GetStatus();
  210. continue;
  211. }
  212. const auto section0 = nca->GetSubdirectories()[0];
  213. for (const auto& inner_file : section0->GetFiles()) {
  214. if (inner_file->GetExtension() != "cnmt") {
  215. continue;
  216. }
  217. const CNMT cnmt(inner_file);
  218. auto& ncas_title = ncas[cnmt.GetTitleID()];
  219. ncas_title[{cnmt.GetType(), ContentRecordType::Meta}] = nca;
  220. for (const auto& rec : cnmt.GetContentRecords()) {
  221. const auto id_string = Common::HexToString(rec.nca_id, false);
  222. auto next_file = pfs->GetFile(fmt::format("{}.nca", id_string));
  223. if (next_file == nullptr) {
  224. if (rec.type != ContentRecordType::DeltaFragment) {
  225. LOG_WARNING(Service_FS,
  226. "NCA with ID {}.nca is listed in content metadata, but cannot "
  227. "be found in PFS. NSP appears to be corrupted.",
  228. id_string);
  229. }
  230. continue;
  231. }
  232. auto next_nca = std::make_shared<NCA>(std::move(next_file), nullptr, 0);
  233. if (next_nca->GetType() == NCAContentType::Program) {
  234. program_status[cnmt.GetTitleID()] = next_nca->GetStatus();
  235. }
  236. if (next_nca->GetStatus() == Loader::ResultStatus::Success ||
  237. (next_nca->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS &&
  238. (cnmt.GetTitleID() & 0x800) != 0)) {
  239. ncas_title[{cnmt.GetType(), rec.type}] = std::move(next_nca);
  240. }
  241. }
  242. break;
  243. }
  244. }
  245. }
  246. } // namespace FileSys