content_archive.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include <vector>
  10. #include "common/common_funcs.h"
  11. #include "common/common_types.h"
  12. #include "common/swap.h"
  13. #include "core/crypto/key_manager.h"
  14. #include "core/file_sys/vfs.h"
  15. namespace Loader {
  16. enum class ResultStatus : u16;
  17. }
  18. namespace FileSys {
  19. union NCASectionHeader;
  20. /// Describes the type of content within an NCA archive.
  21. enum class NCAContentType : u8 {
  22. /// Executable-related data
  23. Program = 0,
  24. /// Metadata.
  25. Meta = 1,
  26. /// Access control data.
  27. Control = 2,
  28. /// Information related to the game manual
  29. /// e.g. Legal information, etc.
  30. Manual = 3,
  31. /// System data.
  32. Data = 4,
  33. /// Data that can be accessed by applications.
  34. PublicData = 5,
  35. };
  36. enum class NCASectionCryptoType : u8 {
  37. NONE = 1,
  38. XTS = 2,
  39. CTR = 3,
  40. BKTR = 4,
  41. };
  42. struct NCASectionTableEntry {
  43. u32_le media_offset;
  44. u32_le media_end_offset;
  45. INSERT_PADDING_BYTES(0x8);
  46. };
  47. static_assert(sizeof(NCASectionTableEntry) == 0x10, "NCASectionTableEntry has incorrect size.");
  48. struct NCAHeader {
  49. std::array<u8, 0x100> rsa_signature_1;
  50. std::array<u8, 0x100> rsa_signature_2;
  51. u32_le magic;
  52. u8 is_system;
  53. NCAContentType content_type;
  54. u8 crypto_type;
  55. u8 key_index;
  56. u64_le size;
  57. u64_le title_id;
  58. INSERT_PADDING_BYTES(0x4);
  59. u32_le sdk_version;
  60. u8 crypto_type_2;
  61. INSERT_PADDING_BYTES(15);
  62. std::array<u8, 0x10> rights_id;
  63. std::array<NCASectionTableEntry, 0x4> section_tables;
  64. std::array<std::array<u8, 0x20>, 0x4> hash_tables;
  65. std::array<u8, 0x40> key_area;
  66. INSERT_PADDING_BYTES(0xC0);
  67. };
  68. static_assert(sizeof(NCAHeader) == 0x400, "NCAHeader has incorrect size.");
  69. inline bool IsDirectoryExeFS(const std::shared_ptr<VfsDirectory>& pfs) {
  70. // According to switchbrew, an exefs must only contain these two files:
  71. return pfs->GetFile("main") != nullptr && pfs->GetFile("main.npdm") != nullptr;
  72. }
  73. inline bool IsDirectoryLogoPartition(const VirtualDir& pfs) {
  74. // NintendoLogo is the static image in the top left corner while StartupMovie is the animation
  75. // in the bottom right corner.
  76. return pfs->GetFile("NintendoLogo.png") != nullptr &&
  77. pfs->GetFile("StartupMovie.gif") != nullptr;
  78. }
  79. // An implementation of VfsDirectory that represents a Nintendo Content Archive (NCA) conatiner.
  80. // After construction, use GetStatus to determine if the file is valid and ready to be used.
  81. class NCA : public ReadOnlyVfsDirectory {
  82. public:
  83. explicit NCA(VirtualFile file, VirtualFile bktr_base_romfs = nullptr,
  84. u64 bktr_base_ivfc_offset = 0,
  85. Core::Crypto::KeyManager keys = Core::Crypto::KeyManager());
  86. ~NCA() override;
  87. Loader::ResultStatus GetStatus() const;
  88. std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
  89. std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override;
  90. std::string GetName() const override;
  91. std::shared_ptr<VfsDirectory> GetParentDirectory() const override;
  92. NCAContentType GetType() const;
  93. u64 GetTitleId() const;
  94. std::array<u8, 0x10> GetRightsId() const;
  95. u32 GetSDKVersion() const;
  96. bool IsUpdate() const;
  97. VirtualFile GetRomFS() const;
  98. VirtualDir GetExeFS() const;
  99. VirtualFile GetBaseFile() const;
  100. // Returns the base ivfc offset used in BKTR patching.
  101. u64 GetBaseIVFCOffset() const;
  102. VirtualDir GetLogoPartition() const;
  103. private:
  104. bool CheckSupportedNCA(const NCAHeader& header);
  105. bool HandlePotentialHeaderDecryption();
  106. std::vector<NCASectionHeader> ReadSectionHeaders() const;
  107. bool ReadSections(const std::vector<NCASectionHeader>& sections, u64 bktr_base_ivfc_offset);
  108. bool ReadRomFSSection(const NCASectionHeader& section, const NCASectionTableEntry& entry,
  109. u64 bktr_base_ivfc_offset);
  110. bool ReadPFS0Section(const NCASectionHeader& section, const NCASectionTableEntry& entry);
  111. u8 GetCryptoRevision() const;
  112. std::optional<Core::Crypto::Key128> GetKeyAreaKey(NCASectionCryptoType type) const;
  113. std::optional<Core::Crypto::Key128> GetTitlekey();
  114. VirtualFile Decrypt(const NCASectionHeader& header, VirtualFile in, u64 starting_offset);
  115. std::vector<VirtualDir> dirs;
  116. std::vector<VirtualFile> files;
  117. VirtualFile romfs = nullptr;
  118. VirtualDir exefs = nullptr;
  119. VirtualDir logo = nullptr;
  120. VirtualFile file;
  121. VirtualFile bktr_base_romfs;
  122. u64 ivfc_offset = 0;
  123. NCAHeader header{};
  124. bool has_rights_id{};
  125. Loader::ResultStatus status{};
  126. bool encrypted = false;
  127. bool is_update = false;
  128. Core::Crypto::KeyManager keys;
  129. };
  130. } // namespace FileSys