diff options
| author | 2018-08-25 11:45:26 -0400 | |
|---|---|---|
| committer | 2018-09-04 14:25:54 -0400 | |
| commit | 93703431e2d5318ac4a901b81d31230c40942043 (patch) | |
| tree | 0989463072379d29c87fd1668e5d5aa6a8caa456 /src/core | |
| parent | drd: Load title ID from program metadata (diff) | |
| download | yuzu-93703431e2d5318ac4a901b81d31230c40942043.tar.gz yuzu-93703431e2d5318ac4a901b81d31230c40942043.tar.xz yuzu-93703431e2d5318ac4a901b81d31230c40942043.zip | |
file_sys: Add Nintendo Submission Package (NSP)
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/file_sys/submission_package.cpp | 226 | ||||
| -rw-r--r-- | src/core/file_sys/submission_package.h | 70 |
2 files changed, 296 insertions, 0 deletions
diff --git a/src/core/file_sys/submission_package.cpp b/src/core/file_sys/submission_package.cpp new file mode 100644 index 000000000..660771cf8 --- /dev/null +++ b/src/core/file_sys/submission_package.cpp | |||
| @@ -0,0 +1,226 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <fmt/ostream.h> | ||
| 6 | #include "common/assert.h" | ||
| 7 | #include "common/hex_util.h" | ||
| 8 | #include "core/file_sys/content_archive.h" | ||
| 9 | #include "core/file_sys/nca_metadata.h" | ||
| 10 | #include "core/file_sys/submission_package.h" | ||
| 11 | #include "core/loader/loader.h" | ||
| 12 | |||
| 13 | namespace FileSys { | ||
| 14 | NSP::NSP(VirtualFile file_) | ||
| 15 | : file(std::move(file_)), | ||
| 16 | pfs(std::make_shared<PartitionFilesystem>(file)), status{Loader::ResultStatus::Success} { | ||
| 17 | if (pfs->GetStatus() != Loader::ResultStatus::Success) { | ||
| 18 | status = pfs->GetStatus(); | ||
| 19 | return; | ||
| 20 | } | ||
| 21 | |||
| 22 | if (IsDirectoryExeFS(pfs)) { | ||
| 23 | extracted = true; | ||
| 24 | exefs = pfs; | ||
| 25 | |||
| 26 | const auto& files = pfs->GetFiles(); | ||
| 27 | const auto romfs_iter = | ||
| 28 | std::find_if(files.begin(), files.end(), [](const FileSys::VirtualFile& file) { | ||
| 29 | return file->GetName().find(".romfs") != std::string::npos; | ||
| 30 | }); | ||
| 31 | if (romfs_iter != files.end()) | ||
| 32 | romfs = *romfs_iter; | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | |||
| 36 | extracted = false; | ||
| 37 | const auto files = pfs->GetFiles(); | ||
| 38 | |||
| 39 | Core::Crypto::KeyManager keys; | ||
| 40 | for (const auto& ticket_file : files) { | ||
| 41 | if (ticket_file->GetExtension() == "tik") { | ||
| 42 | if (ticket_file == nullptr || | ||
| 43 | ticket_file->GetSize() < | ||
| 44 | Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) { | ||
| 45 | continue; | ||
| 46 | } | ||
| 47 | |||
| 48 | Core::Crypto::Key128 key{}; | ||
| 49 | ticket_file->Read(key.data(), key.size(), Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET); | ||
| 50 | std::string_view name_only(ticket_file->GetName()); | ||
| 51 | name_only.remove_suffix(4); | ||
| 52 | const auto rights_id_raw = Common::HexStringToArray<16>(name_only); | ||
| 53 | u128 rights_id; | ||
| 54 | std::memcpy(rights_id.data(), rights_id_raw.data(), sizeof(u128)); | ||
| 55 | keys.SetKey(Core::Crypto::S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | for (const auto& outer_file : files) { | ||
| 60 | if (outer_file->GetName().substr(outer_file->GetName().size() - 9) == ".cnmt.nca") { | ||
| 61 | const auto nca = std::make_shared<NCA>(outer_file); | ||
| 62 | if (nca->GetStatus() != Loader::ResultStatus::Success) | ||
| 63 | continue; | ||
| 64 | const auto section0 = nca->GetSubdirectories()[0]; | ||
| 65 | |||
| 66 | for (const auto& inner_file : section0->GetFiles()) { | ||
| 67 | if (inner_file->GetExtension() != "cnmt") | ||
| 68 | continue; | ||
| 69 | |||
| 70 | const CNMT cnmt(inner_file); | ||
| 71 | auto& ncas_title = ncas[cnmt.GetTitleID()]; | ||
| 72 | |||
| 73 | ncas_title[ContentRecordType::Meta] = nca; | ||
| 74 | for (const auto& rec : cnmt.GetContentRecords()) { | ||
| 75 | const auto next_file = pfs->GetFile( | ||
| 76 | fmt::format("{}.nca", Common::HexArrayToString(rec.nca_id, false))); | ||
| 77 | if (next_file == nullptr) { | ||
| 78 | LOG_WARNING(Service_FS, | ||
| 79 | "NCA with ID {}.nca is listed in content metadata, but cannot " | ||
| 80 | "be found in PFS. NSP appears to be corrupted.", | ||
| 81 | Common::HexArrayToString(rec.nca_id, false)); | ||
| 82 | continue; | ||
| 83 | } | ||
| 84 | |||
| 85 | const auto next_nca = std::make_shared<NCA>(next_file); | ||
| 86 | if (next_nca->GetType() == NCAContentType::Program) | ||
| 87 | program_status[cnmt.GetTitleID()] = next_nca->GetStatus(); | ||
| 88 | if (next_nca->GetStatus() == Loader::ResultStatus::Success) | ||
| 89 | ncas_title[rec.type] = next_nca; | ||
| 90 | } | ||
| 91 | |||
| 92 | break; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | Loader::ResultStatus NSP::GetStatus() const { | ||
| 99 | return status; | ||
| 100 | } | ||
| 101 | |||
| 102 | Loader::ResultStatus NSP::GetProgramStatus(u64 title_id) const { | ||
| 103 | if (program_status.find(title_id) != program_status.end()) | ||
| 104 | return program_status.at(title_id); | ||
| 105 | return Loader::ResultStatus::ErrorNSPMissingProgramNCA; | ||
| 106 | } | ||
| 107 | |||
| 108 | u64 NSP::GetFirstTitleID() const { | ||
| 109 | if (program_status.empty()) | ||
| 110 | return 0; | ||
| 111 | return program_status.begin()->first; | ||
| 112 | } | ||
| 113 | |||
| 114 | u64 NSP::GetProgramTitleID() const { | ||
| 115 | auto out = GetFirstTitleID(); | ||
| 116 | for (const auto other_tid : GetTitleIDs()) { | ||
| 117 | if ((out & 0x800) != 0) | ||
| 118 | out = other_tid; | ||
| 119 | } | ||
| 120 | return out; | ||
| 121 | } | ||
| 122 | |||
| 123 | std::vector<u64> NSP::GetTitleIDs() const { | ||
| 124 | std::vector<u64> out; | ||
| 125 | for (const auto& kv : ncas) | ||
| 126 | out.push_back(kv.first); | ||
| 127 | return out; | ||
| 128 | } | ||
| 129 | |||
| 130 | bool NSP::IsExtractedType() const { | ||
| 131 | return extracted; | ||
| 132 | } | ||
| 133 | |||
| 134 | VirtualFile NSP::GetRomFS() const { | ||
| 135 | return romfs; | ||
| 136 | } | ||
| 137 | |||
| 138 | VirtualDir NSP::GetExeFS() const { | ||
| 139 | return exefs; | ||
| 140 | } | ||
| 141 | |||
| 142 | std::vector<std::shared_ptr<NCA>> NSP::GetNCAsCollapsed() const { | ||
| 143 | if (extracted) | ||
| 144 | LOG_WARNING(Service_FS, "called on an NSP that is of type extracted."); | ||
| 145 | std::vector<std::shared_ptr<NCA>> out; | ||
| 146 | for (const auto& map : ncas) { | ||
| 147 | for (const auto& inner_map : map.second) | ||
| 148 | out.push_back(inner_map.second); | ||
| 149 | } | ||
| 150 | return out; | ||
| 151 | } | ||
| 152 | |||
| 153 | std::multimap<u64, std::shared_ptr<NCA>> NSP::GetNCAsByTitleID() const { | ||
| 154 | if (extracted) | ||
| 155 | LOG_WARNING(Service_FS, "called on an NSP that is of type extracted."); | ||
| 156 | std::multimap<u64, std::shared_ptr<NCA>> out; | ||
| 157 | for (const auto& map : ncas) { | ||
| 158 | for (const auto& inner_map : map.second) | ||
| 159 | out.insert({map.first, inner_map.second}); | ||
| 160 | } | ||
| 161 | return out; | ||
| 162 | } | ||
| 163 | |||
| 164 | std::map<u64, std::map<ContentRecordType, std::shared_ptr<NCA>>> NSP::GetNCAs() const { | ||
| 165 | return ncas; | ||
| 166 | } | ||
| 167 | |||
| 168 | std::shared_ptr<NCA> NSP::GetNCA(u64 title_id, ContentRecordType type) const { | ||
| 169 | if (extracted) | ||
| 170 | LOG_WARNING(Service_FS, "called on an NSP that is of type extracted."); | ||
| 171 | if (ncas.find(title_id) != ncas.end()) { | ||
| 172 | const auto& inner_map = ncas.at(title_id); | ||
| 173 | if (inner_map.find(type) != inner_map.end()) | ||
| 174 | return inner_map.at(type); | ||
| 175 | } | ||
| 176 | |||
| 177 | return nullptr; | ||
| 178 | } | ||
| 179 | |||
| 180 | VirtualFile NSP::GetNCAFile(u64 title_id, ContentRecordType type) const { | ||
| 181 | if (extracted) | ||
| 182 | LOG_WARNING(Service_FS, "called on an NSP that is of type extracted."); | ||
| 183 | const auto nca = GetNCA(title_id, type); | ||
| 184 | if (nca != nullptr) | ||
| 185 | return nca->GetBaseFile(); | ||
| 186 | return nullptr; | ||
| 187 | } | ||
| 188 | |||
| 189 | std::vector<Core::Crypto::Key128> NSP::GetTitlekey() const { | ||
| 190 | if (extracted) | ||
| 191 | LOG_WARNING(Service_FS, "called on an NSP that is of type extracted."); | ||
| 192 | std::vector<Core::Crypto::Key128> out; | ||
| 193 | for (const auto& ticket_file : ticket_files) { | ||
| 194 | if (ticket_file == nullptr || | ||
| 195 | ticket_file->GetSize() < | ||
| 196 | Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET + sizeof(Core::Crypto::Key128)) { | ||
| 197 | continue; | ||
| 198 | } | ||
| 199 | |||
| 200 | Core::Crypto::Key128 key{}; | ||
| 201 | ticket_file->Read(key.data(), key.size(), Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET); | ||
| 202 | out.push_back(key); | ||
| 203 | } | ||
| 204 | return out; | ||
| 205 | } | ||
| 206 | |||
| 207 | std::vector<VirtualFile> NSP::GetFiles() const { | ||
| 208 | return pfs->GetFiles(); | ||
| 209 | } | ||
| 210 | |||
| 211 | std::vector<VirtualDir> NSP::GetSubdirectories() const { | ||
| 212 | return pfs->GetSubdirectories(); | ||
| 213 | } | ||
| 214 | |||
| 215 | std::string NSP::GetName() const { | ||
| 216 | return file->GetName(); | ||
| 217 | } | ||
| 218 | |||
| 219 | VirtualDir NSP::GetParentDirectory() const { | ||
| 220 | return file->GetContainingDirectory(); | ||
| 221 | } | ||
| 222 | |||
| 223 | bool NSP::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { | ||
| 224 | return false; | ||
| 225 | } | ||
| 226 | } // namespace FileSys | ||
diff --git a/src/core/file_sys/submission_package.h b/src/core/file_sys/submission_package.h new file mode 100644 index 000000000..7b520df57 --- /dev/null +++ b/src/core/file_sys/submission_package.h | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <map> | ||
| 9 | #include <vector> | ||
| 10 | #include "common/common_types.h" | ||
| 11 | #include "common/swap.h" | ||
| 12 | #include "core/file_sys/content_archive.h" | ||
| 13 | #include "core/file_sys/vfs.h" | ||
| 14 | #include "core/loader/loader.h" | ||
| 15 | #include "romfs_factory.h" | ||
| 16 | |||
| 17 | namespace FileSys { | ||
| 18 | |||
| 19 | class NSP : public ReadOnlyVfsDirectory { | ||
| 20 | public: | ||
| 21 | explicit NSP(VirtualFile file); | ||
| 22 | |||
| 23 | Loader::ResultStatus GetStatus() const; | ||
| 24 | Loader::ResultStatus GetProgramStatus(u64 title_id) const; | ||
| 25 | // Should only be used when one title id can be assured. | ||
| 26 | u64 GetFirstTitleID() const; | ||
| 27 | u64 GetProgramTitleID() const; | ||
| 28 | std::vector<u64> GetTitleIDs() const; | ||
| 29 | |||
| 30 | bool IsExtractedType() const; | ||
| 31 | |||
| 32 | // Common (Can be safely called on both types) | ||
| 33 | VirtualFile GetRomFS() const; | ||
| 34 | VirtualDir GetExeFS() const; | ||
| 35 | |||
| 36 | // Type 0 Only (Collection of NCAs + Certificate + Ticket + Meta XML) | ||
| 37 | std::vector<std::shared_ptr<NCA>> GetNCAsCollapsed() const; | ||
| 38 | std::multimap<u64, std::shared_ptr<NCA>> GetNCAsByTitleID() const; | ||
| 39 | std::map<u64, std::map<ContentRecordType, std::shared_ptr<NCA>>> GetNCAs() const; | ||
| 40 | std::shared_ptr<NCA> GetNCA(u64 title_id, ContentRecordType type) const; | ||
| 41 | VirtualFile GetNCAFile(u64 title_id, ContentRecordType type) const; | ||
| 42 | std::vector<Core::Crypto::Key128> GetTitlekey() const; | ||
| 43 | |||
| 44 | std::vector<VirtualFile> GetFiles() const override; | ||
| 45 | |||
| 46 | std::vector<VirtualDir> GetSubdirectories() const override; | ||
| 47 | |||
| 48 | std::string GetName() const override; | ||
| 49 | |||
| 50 | VirtualDir GetParentDirectory() const override; | ||
| 51 | |||
| 52 | protected: | ||
| 53 | bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; | ||
| 54 | |||
| 55 | private: | ||
| 56 | VirtualFile file; | ||
| 57 | |||
| 58 | bool extracted; | ||
| 59 | Loader::ResultStatus status; | ||
| 60 | std::map<u64, Loader::ResultStatus> program_status; | ||
| 61 | |||
| 62 | std::shared_ptr<PartitionFilesystem> pfs; | ||
| 63 | // Map title id -> {map type -> NCA} | ||
| 64 | std::map<u64, std::map<ContentRecordType, std::shared_ptr<NCA>>> ncas; | ||
| 65 | std::vector<VirtualFile> ticket_files; | ||
| 66 | |||
| 67 | VirtualFile romfs; | ||
| 68 | VirtualDir exefs; | ||
| 69 | }; | ||
| 70 | } // namespace FileSys | ||