content_archive.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <optional>
  6. #include <utility>
  7. #include "common/logging/log.h"
  8. #include "common/polyfill_ranges.h"
  9. #include "core/crypto/aes_util.h"
  10. #include "core/crypto/ctr_encryption_layer.h"
  11. #include "core/crypto/key_manager.h"
  12. #include "core/file_sys/content_archive.h"
  13. #include "core/file_sys/nca_patch.h"
  14. #include "core/file_sys/partition_filesystem.h"
  15. #include "core/file_sys/vfs_offset.h"
  16. #include "core/loader/loader.h"
  17. namespace FileSys {
  18. // Media offsets in headers are stored divided by 512. Mult. by this to get real offset.
  19. constexpr u64 MEDIA_OFFSET_MULTIPLIER = 0x200;
  20. constexpr u64 SECTION_HEADER_SIZE = 0x200;
  21. constexpr u64 SECTION_HEADER_OFFSET = 0x400;
  22. constexpr u32 IVFC_MAX_LEVEL = 6;
  23. enum class NCASectionFilesystemType : u8 {
  24. PFS0 = 0x2,
  25. ROMFS = 0x3,
  26. };
  27. struct IVFCLevel {
  28. u64_le offset;
  29. u64_le size;
  30. u32_le block_size;
  31. u32_le reserved;
  32. };
  33. static_assert(sizeof(IVFCLevel) == 0x18, "IVFCLevel has incorrect size.");
  34. struct IVFCHeader {
  35. u32_le magic;
  36. u32_le magic_number;
  37. INSERT_PADDING_BYTES_NOINIT(8);
  38. std::array<IVFCLevel, 6> levels;
  39. INSERT_PADDING_BYTES_NOINIT(64);
  40. };
  41. static_assert(sizeof(IVFCHeader) == 0xE0, "IVFCHeader has incorrect size.");
  42. struct NCASectionHeaderBlock {
  43. INSERT_PADDING_BYTES_NOINIT(3);
  44. NCASectionFilesystemType filesystem_type;
  45. NCASectionCryptoType crypto_type;
  46. INSERT_PADDING_BYTES_NOINIT(3);
  47. };
  48. static_assert(sizeof(NCASectionHeaderBlock) == 0x8, "NCASectionHeaderBlock has incorrect size.");
  49. struct NCASectionRaw {
  50. NCASectionHeaderBlock header;
  51. std::array<u8, 0x138> block_data;
  52. std::array<u8, 0x8> section_ctr;
  53. INSERT_PADDING_BYTES_NOINIT(0xB8);
  54. };
  55. static_assert(sizeof(NCASectionRaw) == 0x200, "NCASectionRaw has incorrect size.");
  56. struct PFS0Superblock {
  57. NCASectionHeaderBlock header_block;
  58. std::array<u8, 0x20> hash;
  59. u32_le size;
  60. INSERT_PADDING_BYTES_NOINIT(4);
  61. u64_le hash_table_offset;
  62. u64_le hash_table_size;
  63. u64_le pfs0_header_offset;
  64. u64_le pfs0_size;
  65. INSERT_PADDING_BYTES_NOINIT(0x1B0);
  66. };
  67. static_assert(sizeof(PFS0Superblock) == 0x200, "PFS0Superblock has incorrect size.");
  68. struct RomFSSuperblock {
  69. NCASectionHeaderBlock header_block;
  70. IVFCHeader ivfc;
  71. INSERT_PADDING_BYTES_NOINIT(0x118);
  72. };
  73. static_assert(sizeof(RomFSSuperblock) == 0x200, "RomFSSuperblock has incorrect size.");
  74. struct BKTRHeader {
  75. u64_le offset;
  76. u64_le size;
  77. u32_le magic;
  78. INSERT_PADDING_BYTES_NOINIT(0x4);
  79. u32_le number_entries;
  80. INSERT_PADDING_BYTES_NOINIT(0x4);
  81. };
  82. static_assert(sizeof(BKTRHeader) == 0x20, "BKTRHeader has incorrect size.");
  83. struct BKTRSuperblock {
  84. NCASectionHeaderBlock header_block;
  85. IVFCHeader ivfc;
  86. INSERT_PADDING_BYTES_NOINIT(0x18);
  87. BKTRHeader relocation;
  88. BKTRHeader subsection;
  89. INSERT_PADDING_BYTES_NOINIT(0xC0);
  90. };
  91. static_assert(sizeof(BKTRSuperblock) == 0x200, "BKTRSuperblock has incorrect size.");
  92. union NCASectionHeader {
  93. NCASectionRaw raw{};
  94. PFS0Superblock pfs0;
  95. RomFSSuperblock romfs;
  96. BKTRSuperblock bktr;
  97. };
  98. static_assert(sizeof(NCASectionHeader) == 0x200, "NCASectionHeader has incorrect size.");
  99. static bool IsValidNCA(const NCAHeader& header) {
  100. // TODO(DarkLordZach): Add NCA2/NCA0 support.
  101. return header.magic == Common::MakeMagic('N', 'C', 'A', '3');
  102. }
  103. NCA::NCA(VirtualFile file_, VirtualFile bktr_base_romfs_, u64 bktr_base_ivfc_offset)
  104. : file(std::move(file_)),
  105. bktr_base_romfs(std::move(bktr_base_romfs_)), keys{Core::Crypto::KeyManager::Instance()} {
  106. if (file == nullptr) {
  107. status = Loader::ResultStatus::ErrorNullFile;
  108. return;
  109. }
  110. if (sizeof(NCAHeader) != file->ReadObject(&header)) {
  111. LOG_ERROR(Loader, "File reader errored out during header read.");
  112. status = Loader::ResultStatus::ErrorBadNCAHeader;
  113. return;
  114. }
  115. if (!HandlePotentialHeaderDecryption()) {
  116. return;
  117. }
  118. has_rights_id = std::ranges::any_of(header.rights_id, [](char c) { return c != '\0'; });
  119. const std::vector<NCASectionHeader> sections = ReadSectionHeaders();
  120. is_update = std::ranges::any_of(sections, [](const NCASectionHeader& nca_header) {
  121. return nca_header.raw.header.crypto_type == NCASectionCryptoType::BKTR;
  122. });
  123. if (!ReadSections(sections, bktr_base_ivfc_offset)) {
  124. return;
  125. }
  126. status = Loader::ResultStatus::Success;
  127. }
  128. NCA::~NCA() = default;
  129. bool NCA::CheckSupportedNCA(const NCAHeader& nca_header) {
  130. if (nca_header.magic == Common::MakeMagic('N', 'C', 'A', '2')) {
  131. status = Loader::ResultStatus::ErrorNCA2;
  132. return false;
  133. }
  134. if (nca_header.magic == Common::MakeMagic('N', 'C', 'A', '0')) {
  135. status = Loader::ResultStatus::ErrorNCA0;
  136. return false;
  137. }
  138. return true;
  139. }
  140. bool NCA::HandlePotentialHeaderDecryption() {
  141. if (IsValidNCA(header)) {
  142. return true;
  143. }
  144. if (!CheckSupportedNCA(header)) {
  145. return false;
  146. }
  147. NCAHeader dec_header{};
  148. Core::Crypto::AESCipher<Core::Crypto::Key256> cipher(
  149. keys.GetKey(Core::Crypto::S256KeyType::Header), Core::Crypto::Mode::XTS);
  150. cipher.XTSTranscode(&header, sizeof(NCAHeader), &dec_header, 0, 0x200,
  151. Core::Crypto::Op::Decrypt);
  152. if (IsValidNCA(dec_header)) {
  153. header = dec_header;
  154. encrypted = true;
  155. } else {
  156. if (!CheckSupportedNCA(dec_header)) {
  157. return false;
  158. }
  159. if (keys.HasKey(Core::Crypto::S256KeyType::Header)) {
  160. status = Loader::ResultStatus::ErrorIncorrectHeaderKey;
  161. } else {
  162. status = Loader::ResultStatus::ErrorMissingHeaderKey;
  163. }
  164. return false;
  165. }
  166. return true;
  167. }
  168. std::vector<NCASectionHeader> NCA::ReadSectionHeaders() const {
  169. const std::ptrdiff_t number_sections =
  170. std::ranges::count_if(header.section_tables, [](const NCASectionTableEntry& entry) {
  171. return entry.media_offset > 0;
  172. });
  173. std::vector<NCASectionHeader> sections(number_sections);
  174. const auto length_sections = SECTION_HEADER_SIZE * number_sections;
  175. if (encrypted) {
  176. auto raw = file->ReadBytes(length_sections, SECTION_HEADER_OFFSET);
  177. Core::Crypto::AESCipher<Core::Crypto::Key256> cipher(
  178. keys.GetKey(Core::Crypto::S256KeyType::Header), Core::Crypto::Mode::XTS);
  179. cipher.XTSTranscode(raw.data(), length_sections, sections.data(), 2, SECTION_HEADER_SIZE,
  180. Core::Crypto::Op::Decrypt);
  181. } else {
  182. file->ReadBytes(sections.data(), length_sections, SECTION_HEADER_OFFSET);
  183. }
  184. return sections;
  185. }
  186. bool NCA::ReadSections(const std::vector<NCASectionHeader>& sections, u64 bktr_base_ivfc_offset) {
  187. for (std::size_t i = 0; i < sections.size(); ++i) {
  188. const auto& section = sections[i];
  189. if (section.raw.header.filesystem_type == NCASectionFilesystemType::ROMFS) {
  190. if (!ReadRomFSSection(section, header.section_tables[i], bktr_base_ivfc_offset)) {
  191. return false;
  192. }
  193. } else if (section.raw.header.filesystem_type == NCASectionFilesystemType::PFS0) {
  194. if (!ReadPFS0Section(section, header.section_tables[i])) {
  195. return false;
  196. }
  197. }
  198. }
  199. return true;
  200. }
  201. bool NCA::ReadRomFSSection(const NCASectionHeader& section, const NCASectionTableEntry& entry,
  202. u64 bktr_base_ivfc_offset) {
  203. const std::size_t base_offset = entry.media_offset * MEDIA_OFFSET_MULTIPLIER;
  204. ivfc_offset = section.romfs.ivfc.levels[IVFC_MAX_LEVEL - 1].offset;
  205. const std::size_t romfs_offset = base_offset + ivfc_offset;
  206. const std::size_t romfs_size = section.romfs.ivfc.levels[IVFC_MAX_LEVEL - 1].size;
  207. auto raw = std::make_shared<OffsetVfsFile>(file, romfs_size, romfs_offset);
  208. auto dec = Decrypt(section, raw, romfs_offset);
  209. if (dec == nullptr) {
  210. if (status != Loader::ResultStatus::Success)
  211. return false;
  212. if (has_rights_id)
  213. status = Loader::ResultStatus::ErrorIncorrectTitlekeyOrTitlekek;
  214. else
  215. status = Loader::ResultStatus::ErrorIncorrectKeyAreaKey;
  216. return false;
  217. }
  218. if (section.raw.header.crypto_type == NCASectionCryptoType::BKTR) {
  219. if (section.bktr.relocation.magic != Common::MakeMagic('B', 'K', 'T', 'R') ||
  220. section.bktr.subsection.magic != Common::MakeMagic('B', 'K', 'T', 'R')) {
  221. status = Loader::ResultStatus::ErrorBadBKTRHeader;
  222. return false;
  223. }
  224. if (section.bktr.relocation.offset + section.bktr.relocation.size !=
  225. section.bktr.subsection.offset) {
  226. status = Loader::ResultStatus::ErrorBKTRSubsectionNotAfterRelocation;
  227. return false;
  228. }
  229. const u64 size = MEDIA_OFFSET_MULTIPLIER * (entry.media_end_offset - entry.media_offset);
  230. if (section.bktr.subsection.offset + section.bktr.subsection.size != size) {
  231. status = Loader::ResultStatus::ErrorBKTRSubsectionNotAtEnd;
  232. return false;
  233. }
  234. const u64 offset = section.romfs.ivfc.levels[IVFC_MAX_LEVEL - 1].offset;
  235. RelocationBlock relocation_block{};
  236. if (dec->ReadObject(&relocation_block, section.bktr.relocation.offset - offset) !=
  237. sizeof(RelocationBlock)) {
  238. status = Loader::ResultStatus::ErrorBadRelocationBlock;
  239. return false;
  240. }
  241. SubsectionBlock subsection_block{};
  242. if (dec->ReadObject(&subsection_block, section.bktr.subsection.offset - offset) !=
  243. sizeof(RelocationBlock)) {
  244. status = Loader::ResultStatus::ErrorBadSubsectionBlock;
  245. return false;
  246. }
  247. std::vector<RelocationBucketRaw> relocation_buckets_raw(
  248. (section.bktr.relocation.size - sizeof(RelocationBlock)) / sizeof(RelocationBucketRaw));
  249. if (dec->ReadBytes(relocation_buckets_raw.data(),
  250. section.bktr.relocation.size - sizeof(RelocationBlock),
  251. section.bktr.relocation.offset + sizeof(RelocationBlock) - offset) !=
  252. section.bktr.relocation.size - sizeof(RelocationBlock)) {
  253. status = Loader::ResultStatus::ErrorBadRelocationBuckets;
  254. return false;
  255. }
  256. std::vector<SubsectionBucketRaw> subsection_buckets_raw(
  257. (section.bktr.subsection.size - sizeof(SubsectionBlock)) / sizeof(SubsectionBucketRaw));
  258. if (dec->ReadBytes(subsection_buckets_raw.data(),
  259. section.bktr.subsection.size - sizeof(SubsectionBlock),
  260. section.bktr.subsection.offset + sizeof(SubsectionBlock) - offset) !=
  261. section.bktr.subsection.size - sizeof(SubsectionBlock)) {
  262. status = Loader::ResultStatus::ErrorBadSubsectionBuckets;
  263. return false;
  264. }
  265. std::vector<RelocationBucket> relocation_buckets(relocation_buckets_raw.size());
  266. std::ranges::transform(relocation_buckets_raw, relocation_buckets.begin(),
  267. &ConvertRelocationBucketRaw);
  268. std::vector<SubsectionBucket> subsection_buckets(subsection_buckets_raw.size());
  269. std::ranges::transform(subsection_buckets_raw, subsection_buckets.begin(),
  270. &ConvertSubsectionBucketRaw);
  271. u32 ctr_low;
  272. std::memcpy(&ctr_low, section.raw.section_ctr.data(), sizeof(ctr_low));
  273. subsection_buckets.back().entries.push_back({section.bktr.relocation.offset, {0}, ctr_low});
  274. subsection_buckets.back().entries.push_back({size, {0}, 0});
  275. std::optional<Core::Crypto::Key128> key;
  276. if (encrypted) {
  277. if (has_rights_id) {
  278. status = Loader::ResultStatus::Success;
  279. key = GetTitlekey();
  280. if (!key) {
  281. status = Loader::ResultStatus::ErrorMissingTitlekey;
  282. return false;
  283. }
  284. } else {
  285. key = GetKeyAreaKey(NCASectionCryptoType::BKTR);
  286. if (!key) {
  287. status = Loader::ResultStatus::ErrorMissingKeyAreaKey;
  288. return false;
  289. }
  290. }
  291. }
  292. if (bktr_base_romfs == nullptr) {
  293. status = Loader::ResultStatus::ErrorMissingBKTRBaseRomFS;
  294. return false;
  295. }
  296. auto bktr = std::make_shared<BKTR>(
  297. bktr_base_romfs, std::make_shared<OffsetVfsFile>(file, romfs_size, base_offset),
  298. relocation_block, relocation_buckets, subsection_block, subsection_buckets, encrypted,
  299. encrypted ? *key : Core::Crypto::Key128{}, base_offset, bktr_base_ivfc_offset,
  300. section.raw.section_ctr);
  301. // BKTR applies to entire IVFC, so make an offset version to level 6
  302. files.push_back(std::make_shared<OffsetVfsFile>(
  303. bktr, romfs_size, section.romfs.ivfc.levels[IVFC_MAX_LEVEL - 1].offset));
  304. } else {
  305. files.push_back(std::move(dec));
  306. }
  307. romfs = files.back();
  308. return true;
  309. }
  310. bool NCA::ReadPFS0Section(const NCASectionHeader& section, const NCASectionTableEntry& entry) {
  311. const u64 offset = (static_cast<u64>(entry.media_offset) * MEDIA_OFFSET_MULTIPLIER) +
  312. section.pfs0.pfs0_header_offset;
  313. const u64 size = MEDIA_OFFSET_MULTIPLIER * (entry.media_end_offset - entry.media_offset);
  314. auto dec = Decrypt(section, std::make_shared<OffsetVfsFile>(file, size, offset), offset);
  315. if (dec != nullptr) {
  316. auto npfs = std::make_shared<PartitionFilesystem>(std::move(dec));
  317. if (npfs->GetStatus() == Loader::ResultStatus::Success) {
  318. dirs.push_back(std::move(npfs));
  319. if (IsDirectoryExeFS(dirs.back()))
  320. exefs = dirs.back();
  321. else if (IsDirectoryLogoPartition(dirs.back()))
  322. logo = dirs.back();
  323. } else {
  324. if (has_rights_id)
  325. status = Loader::ResultStatus::ErrorIncorrectTitlekeyOrTitlekek;
  326. else
  327. status = Loader::ResultStatus::ErrorIncorrectKeyAreaKey;
  328. return false;
  329. }
  330. } else {
  331. if (status != Loader::ResultStatus::Success)
  332. return false;
  333. if (has_rights_id)
  334. status = Loader::ResultStatus::ErrorIncorrectTitlekeyOrTitlekek;
  335. else
  336. status = Loader::ResultStatus::ErrorIncorrectKeyAreaKey;
  337. return false;
  338. }
  339. return true;
  340. }
  341. u8 NCA::GetCryptoRevision() const {
  342. u8 master_key_id = header.crypto_type;
  343. if (header.crypto_type_2 > master_key_id)
  344. master_key_id = header.crypto_type_2;
  345. if (master_key_id > 0)
  346. --master_key_id;
  347. return master_key_id;
  348. }
  349. std::optional<Core::Crypto::Key128> NCA::GetKeyAreaKey(NCASectionCryptoType type) const {
  350. const auto master_key_id = GetCryptoRevision();
  351. if (!keys.HasKey(Core::Crypto::S128KeyType::KeyArea, master_key_id, header.key_index)) {
  352. return std::nullopt;
  353. }
  354. std::vector<u8> key_area(header.key_area.begin(), header.key_area.end());
  355. Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(
  356. keys.GetKey(Core::Crypto::S128KeyType::KeyArea, master_key_id, header.key_index),
  357. Core::Crypto::Mode::ECB);
  358. cipher.Transcode(key_area.data(), key_area.size(), key_area.data(), Core::Crypto::Op::Decrypt);
  359. Core::Crypto::Key128 out{};
  360. if (type == NCASectionCryptoType::XTS) {
  361. std::copy(key_area.begin(), key_area.begin() + 0x10, out.begin());
  362. } else if (type == NCASectionCryptoType::CTR || type == NCASectionCryptoType::BKTR) {
  363. std::copy(key_area.begin() + 0x20, key_area.begin() + 0x30, out.begin());
  364. } else {
  365. LOG_CRITICAL(Crypto, "Called GetKeyAreaKey on invalid NCASectionCryptoType type={:02X}",
  366. type);
  367. }
  368. u128 out_128{};
  369. std::memcpy(out_128.data(), out.data(), sizeof(u128));
  370. LOG_TRACE(Crypto, "called with crypto_rev={:02X}, kak_index={:02X}, key={:016X}{:016X}",
  371. master_key_id, header.key_index, out_128[1], out_128[0]);
  372. return out;
  373. }
  374. std::optional<Core::Crypto::Key128> NCA::GetTitlekey() {
  375. const auto master_key_id = GetCryptoRevision();
  376. u128 rights_id{};
  377. memcpy(rights_id.data(), header.rights_id.data(), 16);
  378. if (rights_id == u128{}) {
  379. status = Loader::ResultStatus::ErrorInvalidRightsID;
  380. return std::nullopt;
  381. }
  382. auto titlekey = keys.GetKey(Core::Crypto::S128KeyType::Titlekey, rights_id[1], rights_id[0]);
  383. if (titlekey == Core::Crypto::Key128{}) {
  384. status = Loader::ResultStatus::ErrorMissingTitlekey;
  385. return std::nullopt;
  386. }
  387. if (!keys.HasKey(Core::Crypto::S128KeyType::Titlekek, master_key_id)) {
  388. status = Loader::ResultStatus::ErrorMissingTitlekek;
  389. return std::nullopt;
  390. }
  391. Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(
  392. keys.GetKey(Core::Crypto::S128KeyType::Titlekek, master_key_id), Core::Crypto::Mode::ECB);
  393. cipher.Transcode(titlekey.data(), titlekey.size(), titlekey.data(), Core::Crypto::Op::Decrypt);
  394. return titlekey;
  395. }
  396. VirtualFile NCA::Decrypt(const NCASectionHeader& s_header, VirtualFile in, u64 starting_offset) {
  397. if (!encrypted)
  398. return in;
  399. switch (s_header.raw.header.crypto_type) {
  400. case NCASectionCryptoType::NONE:
  401. LOG_TRACE(Crypto, "called with mode=NONE");
  402. return in;
  403. case NCASectionCryptoType::CTR:
  404. // During normal BKTR decryption, this entire function is skipped. This is for the metadata,
  405. // which uses the same CTR as usual.
  406. case NCASectionCryptoType::BKTR:
  407. LOG_TRACE(Crypto, "called with mode=CTR, starting_offset={:016X}", starting_offset);
  408. {
  409. std::optional<Core::Crypto::Key128> key;
  410. if (has_rights_id) {
  411. status = Loader::ResultStatus::Success;
  412. key = GetTitlekey();
  413. if (!key) {
  414. if (status == Loader::ResultStatus::Success)
  415. status = Loader::ResultStatus::ErrorMissingTitlekey;
  416. return nullptr;
  417. }
  418. } else {
  419. key = GetKeyAreaKey(NCASectionCryptoType::CTR);
  420. if (!key) {
  421. status = Loader::ResultStatus::ErrorMissingKeyAreaKey;
  422. return nullptr;
  423. }
  424. }
  425. auto out = std::make_shared<Core::Crypto::CTREncryptionLayer>(std::move(in), *key,
  426. starting_offset);
  427. Core::Crypto::CTREncryptionLayer::IVData iv{};
  428. for (std::size_t i = 0; i < 8; ++i) {
  429. iv[i] = s_header.raw.section_ctr[8 - i - 1];
  430. }
  431. out->SetIV(iv);
  432. return std::static_pointer_cast<VfsFile>(out);
  433. }
  434. case NCASectionCryptoType::XTS:
  435. // TODO(DarkLordZach): Find a test case for XTS-encrypted NCAs
  436. default:
  437. LOG_ERROR(Crypto, "called with unhandled crypto type={:02X}",
  438. s_header.raw.header.crypto_type);
  439. return nullptr;
  440. }
  441. }
  442. Loader::ResultStatus NCA::GetStatus() const {
  443. return status;
  444. }
  445. std::vector<VirtualFile> NCA::GetFiles() const {
  446. if (status != Loader::ResultStatus::Success) {
  447. return {};
  448. }
  449. return files;
  450. }
  451. std::vector<VirtualDir> NCA::GetSubdirectories() const {
  452. if (status != Loader::ResultStatus::Success) {
  453. return {};
  454. }
  455. return dirs;
  456. }
  457. std::string NCA::GetName() const {
  458. return file->GetName();
  459. }
  460. VirtualDir NCA::GetParentDirectory() const {
  461. return file->GetContainingDirectory();
  462. }
  463. NCAContentType NCA::GetType() const {
  464. return header.content_type;
  465. }
  466. u64 NCA::GetTitleId() const {
  467. if (is_update || status == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS)
  468. return header.title_id | 0x800;
  469. return header.title_id;
  470. }
  471. std::array<u8, 16> NCA::GetRightsId() const {
  472. return header.rights_id;
  473. }
  474. u32 NCA::GetSDKVersion() const {
  475. return header.sdk_version;
  476. }
  477. bool NCA::IsUpdate() const {
  478. return is_update;
  479. }
  480. VirtualFile NCA::GetRomFS() const {
  481. return romfs;
  482. }
  483. VirtualDir NCA::GetExeFS() const {
  484. return exefs;
  485. }
  486. VirtualFile NCA::GetBaseFile() const {
  487. return file;
  488. }
  489. u64 NCA::GetBaseIVFCOffset() const {
  490. return ivfc_offset;
  491. }
  492. VirtualDir NCA::GetLogoPartition() const {
  493. return logo;
  494. }
  495. } // namespace FileSys