content_archive.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 <optional>
  7. #include <utility>
  8. #include "common/logging/log.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_UNION_PADDING_BYTES(8);
  38. std::array<IVFCLevel, 6> levels;
  39. INSERT_UNION_PADDING_BYTES(64);
  40. };
  41. static_assert(sizeof(IVFCHeader) == 0xE0, "IVFCHeader has incorrect size.");
  42. struct NCASectionHeaderBlock {
  43. INSERT_UNION_PADDING_BYTES(3);
  44. NCASectionFilesystemType filesystem_type;
  45. NCASectionCryptoType crypto_type;
  46. INSERT_UNION_PADDING_BYTES(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_UNION_PADDING_BYTES(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_UNION_PADDING_BYTES(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_UNION_PADDING_BYTES(0x1B0);
  66. };
  67. static_assert(sizeof(PFS0Superblock) == 0x200, "PFS0Superblock has incorrect size.");
  68. struct RomFSSuperblock {
  69. NCASectionHeaderBlock header_block;
  70. IVFCHeader ivfc;
  71. INSERT_UNION_PADDING_BYTES(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_UNION_PADDING_BYTES(0x4);
  79. u32_le number_entries;
  80. INSERT_UNION_PADDING_BYTES(0x4);
  81. };
  82. static_assert(sizeof(BKTRHeader) == 0x20, "BKTRHeader has incorrect size.");
  83. struct BKTRSuperblock {
  84. NCASectionHeaderBlock header_block;
  85. IVFCHeader ivfc;
  86. INSERT_UNION_PADDING_BYTES(0x18);
  87. BKTRHeader relocation;
  88. BKTRHeader subsection;
  89. INSERT_UNION_PADDING_BYTES(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::any_of(header.rights_id.begin(), header.rights_id.end(),
  119. [](char c) { return c != '\0'; });
  120. const std::vector<NCASectionHeader> sections = ReadSectionHeaders();
  121. is_update = std::any_of(sections.begin(), sections.end(), [](const NCASectionHeader& header) {
  122. return header.raw.header.crypto_type == NCASectionCryptoType::BKTR;
  123. });
  124. if (!ReadSections(sections, bktr_base_ivfc_offset)) {
  125. return;
  126. }
  127. status = Loader::ResultStatus::Success;
  128. }
  129. NCA::~NCA() = default;
  130. bool NCA::CheckSupportedNCA(const NCAHeader& nca_header) {
  131. if (nca_header.magic == Common::MakeMagic('N', 'C', 'A', '2')) {
  132. status = Loader::ResultStatus::ErrorNCA2;
  133. return false;
  134. }
  135. if (nca_header.magic == Common::MakeMagic('N', 'C', 'A', '0')) {
  136. status = Loader::ResultStatus::ErrorNCA0;
  137. return false;
  138. }
  139. return true;
  140. }
  141. bool NCA::HandlePotentialHeaderDecryption() {
  142. if (IsValidNCA(header)) {
  143. return true;
  144. }
  145. if (!CheckSupportedNCA(header)) {
  146. return false;
  147. }
  148. NCAHeader dec_header{};
  149. Core::Crypto::AESCipher<Core::Crypto::Key256> cipher(
  150. keys.GetKey(Core::Crypto::S256KeyType::Header), Core::Crypto::Mode::XTS);
  151. cipher.XTSTranscode(&header, sizeof(NCAHeader), &dec_header, 0, 0x200,
  152. Core::Crypto::Op::Decrypt);
  153. if (IsValidNCA(dec_header)) {
  154. header = dec_header;
  155. encrypted = true;
  156. } else {
  157. if (!CheckSupportedNCA(dec_header)) {
  158. return false;
  159. }
  160. if (keys.HasKey(Core::Crypto::S256KeyType::Header)) {
  161. status = Loader::ResultStatus::ErrorIncorrectHeaderKey;
  162. } else {
  163. status = Loader::ResultStatus::ErrorMissingHeaderKey;
  164. }
  165. return false;
  166. }
  167. return true;
  168. }
  169. std::vector<NCASectionHeader> NCA::ReadSectionHeaders() const {
  170. const std::ptrdiff_t number_sections =
  171. std::count_if(std::begin(header.section_tables), std::end(header.section_tables),
  172. [](NCASectionTableEntry entry) { return entry.media_offset > 0; });
  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::transform(relocation_buckets_raw.begin(), relocation_buckets_raw.end(),
  267. relocation_buckets.begin(), &ConvertRelocationBucketRaw);
  268. std::vector<SubsectionBucket> subsection_buckets(subsection_buckets_raw.size());
  269. std::transform(subsection_buckets_raw.begin(), subsection_buckets_raw.end(),
  270. subsection_buckets.begin(), &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 {};
  353. std::vector<u8> key_area(header.key_area.begin(), header.key_area.end());
  354. Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(
  355. keys.GetKey(Core::Crypto::S128KeyType::KeyArea, master_key_id, header.key_index),
  356. Core::Crypto::Mode::ECB);
  357. cipher.Transcode(key_area.data(), key_area.size(), key_area.data(), Core::Crypto::Op::Decrypt);
  358. Core::Crypto::Key128 out;
  359. if (type == NCASectionCryptoType::XTS)
  360. std::copy(key_area.begin(), key_area.begin() + 0x10, out.begin());
  361. else if (type == NCASectionCryptoType::CTR || type == NCASectionCryptoType::BKTR)
  362. std::copy(key_area.begin() + 0x20, key_area.begin() + 0x30, out.begin());
  363. else
  364. LOG_CRITICAL(Crypto, "Called GetKeyAreaKey on invalid NCASectionCryptoType type={:02X}",
  365. static_cast<u8>(type));
  366. u128 out_128{};
  367. memcpy(out_128.data(), out.data(), 16);
  368. LOG_TRACE(Crypto, "called with crypto_rev={:02X}, kak_index={:02X}, key={:016X}{:016X}",
  369. master_key_id, header.key_index, out_128[1], out_128[0]);
  370. return out;
  371. }
  372. std::optional<Core::Crypto::Key128> NCA::GetTitlekey() {
  373. const auto master_key_id = GetCryptoRevision();
  374. u128 rights_id{};
  375. memcpy(rights_id.data(), header.rights_id.data(), 16);
  376. if (rights_id == u128{}) {
  377. status = Loader::ResultStatus::ErrorInvalidRightsID;
  378. return std::nullopt;
  379. }
  380. auto titlekey = keys.GetKey(Core::Crypto::S128KeyType::Titlekey, rights_id[1], rights_id[0]);
  381. if (titlekey == Core::Crypto::Key128{}) {
  382. status = Loader::ResultStatus::ErrorMissingTitlekey;
  383. return std::nullopt;
  384. }
  385. if (!keys.HasKey(Core::Crypto::S128KeyType::Titlekek, master_key_id)) {
  386. status = Loader::ResultStatus::ErrorMissingTitlekek;
  387. return std::nullopt;
  388. }
  389. Core::Crypto::AESCipher<Core::Crypto::Key128> cipher(
  390. keys.GetKey(Core::Crypto::S128KeyType::Titlekek, master_key_id), Core::Crypto::Mode::ECB);
  391. cipher.Transcode(titlekey.data(), titlekey.size(), titlekey.data(), Core::Crypto::Op::Decrypt);
  392. return titlekey;
  393. }
  394. VirtualFile NCA::Decrypt(const NCASectionHeader& s_header, VirtualFile in, u64 starting_offset) {
  395. if (!encrypted)
  396. return in;
  397. switch (s_header.raw.header.crypto_type) {
  398. case NCASectionCryptoType::NONE:
  399. LOG_TRACE(Crypto, "called with mode=NONE");
  400. return in;
  401. case NCASectionCryptoType::CTR:
  402. // During normal BKTR decryption, this entire function is skipped. This is for the metadata,
  403. // which uses the same CTR as usual.
  404. case NCASectionCryptoType::BKTR:
  405. LOG_TRACE(Crypto, "called with mode=CTR, starting_offset={:016X}", starting_offset);
  406. {
  407. std::optional<Core::Crypto::Key128> key;
  408. if (has_rights_id) {
  409. status = Loader::ResultStatus::Success;
  410. key = GetTitlekey();
  411. if (!key) {
  412. if (status == Loader::ResultStatus::Success)
  413. status = Loader::ResultStatus::ErrorMissingTitlekey;
  414. return nullptr;
  415. }
  416. } else {
  417. key = GetKeyAreaKey(NCASectionCryptoType::CTR);
  418. if (!key) {
  419. status = Loader::ResultStatus::ErrorMissingKeyAreaKey;
  420. return nullptr;
  421. }
  422. }
  423. auto out = std::make_shared<Core::Crypto::CTREncryptionLayer>(std::move(in), *key,
  424. starting_offset);
  425. Core::Crypto::CTREncryptionLayer::IVData iv{};
  426. for (std::size_t i = 0; i < 8; ++i) {
  427. iv[i] = s_header.raw.section_ctr[8 - i - 1];
  428. }
  429. out->SetIV(iv);
  430. return std::static_pointer_cast<VfsFile>(out);
  431. }
  432. case NCASectionCryptoType::XTS:
  433. // TODO(DarkLordZach): Find a test case for XTS-encrypted NCAs
  434. default:
  435. LOG_ERROR(Crypto, "called with unhandled crypto type={:02X}",
  436. static_cast<u8>(s_header.raw.header.crypto_type));
  437. return nullptr;
  438. }
  439. }
  440. Loader::ResultStatus NCA::GetStatus() const {
  441. return status;
  442. }
  443. std::vector<std::shared_ptr<VfsFile>> NCA::GetFiles() const {
  444. if (status != Loader::ResultStatus::Success)
  445. return {};
  446. return files;
  447. }
  448. std::vector<std::shared_ptr<VfsDirectory>> NCA::GetSubdirectories() const {
  449. if (status != Loader::ResultStatus::Success)
  450. return {};
  451. return dirs;
  452. }
  453. std::string NCA::GetName() const {
  454. return file->GetName();
  455. }
  456. std::shared_ptr<VfsDirectory> NCA::GetParentDirectory() const {
  457. return file->GetContainingDirectory();
  458. }
  459. NCAContentType NCA::GetType() const {
  460. return header.content_type;
  461. }
  462. u64 NCA::GetTitleId() const {
  463. if (is_update || status == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS)
  464. return header.title_id | 0x800;
  465. return header.title_id;
  466. }
  467. std::array<u8, 16> NCA::GetRightsId() const {
  468. return header.rights_id;
  469. }
  470. u32 NCA::GetSDKVersion() const {
  471. return header.sdk_version;
  472. }
  473. bool NCA::IsUpdate() const {
  474. return is_update;
  475. }
  476. VirtualFile NCA::GetRomFS() const {
  477. return romfs;
  478. }
  479. VirtualDir NCA::GetExeFS() const {
  480. return exefs;
  481. }
  482. VirtualFile NCA::GetBaseFile() const {
  483. return file;
  484. }
  485. u64 NCA::GetBaseIVFCOffset() const {
  486. return ivfc_offset;
  487. }
  488. VirtualDir NCA::GetLogoPartition() const {
  489. return logo;
  490. }
  491. } // namespace FileSys