content_archive.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <string>
  8. #include <vector>
  9. #include <boost/optional.hpp>
  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. enum class NCAContentType : u8 {
  21. Program = 0,
  22. Meta = 1,
  23. Control = 2,
  24. Manual = 3,
  25. Data = 4,
  26. Data_Unknown5 = 5, ///< Seems to be used on some system archives
  27. };
  28. enum class NCASectionCryptoType : u8 {
  29. NONE = 1,
  30. XTS = 2,
  31. CTR = 3,
  32. BKTR = 4,
  33. };
  34. struct NCASectionTableEntry {
  35. u32_le media_offset;
  36. u32_le media_end_offset;
  37. INSERT_PADDING_BYTES(0x8);
  38. };
  39. static_assert(sizeof(NCASectionTableEntry) == 0x10, "NCASectionTableEntry has incorrect size.");
  40. struct NCAHeader {
  41. std::array<u8, 0x100> rsa_signature_1;
  42. std::array<u8, 0x100> rsa_signature_2;
  43. u32_le magic;
  44. u8 is_system;
  45. NCAContentType content_type;
  46. u8 crypto_type;
  47. u8 key_index;
  48. u64_le size;
  49. u64_le title_id;
  50. INSERT_PADDING_BYTES(0x4);
  51. u32_le sdk_version;
  52. u8 crypto_type_2;
  53. INSERT_PADDING_BYTES(15);
  54. std::array<u8, 0x10> rights_id;
  55. std::array<NCASectionTableEntry, 0x4> section_tables;
  56. std::array<std::array<u8, 0x20>, 0x4> hash_tables;
  57. std::array<u8, 0x40> key_area;
  58. INSERT_PADDING_BYTES(0xC0);
  59. };
  60. static_assert(sizeof(NCAHeader) == 0x400, "NCAHeader has incorrect size.");
  61. inline bool IsDirectoryExeFS(const std::shared_ptr<VfsDirectory>& pfs) {
  62. // According to switchbrew, an exefs must only contain these two files:
  63. return pfs->GetFile("main") != nullptr && pfs->GetFile("main.npdm") != nullptr;
  64. }
  65. bool IsValidNCA(const NCAHeader& header);
  66. // An implementation of VfsDirectory that represents a Nintendo Content Archive (NCA) conatiner.
  67. // After construction, use GetStatus to determine if the file is valid and ready to be used.
  68. class NCA : public ReadOnlyVfsDirectory {
  69. public:
  70. explicit NCA(VirtualFile file, VirtualFile bktr_base_romfs = nullptr,
  71. u64 bktr_base_ivfc_offset = 0);
  72. ~NCA() override;
  73. Loader::ResultStatus GetStatus() const;
  74. std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
  75. std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override;
  76. std::string GetName() const override;
  77. std::shared_ptr<VfsDirectory> GetParentDirectory() const override;
  78. NCAContentType GetType() const;
  79. u64 GetTitleId() const;
  80. bool IsUpdate() const;
  81. VirtualFile GetRomFS() const;
  82. VirtualDir GetExeFS() const;
  83. VirtualFile GetBaseFile() const;
  84. // Returns the base ivfc offset used in BKTR patching.
  85. u64 GetBaseIVFCOffset() const;
  86. protected:
  87. bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;
  88. private:
  89. u8 GetCryptoRevision() const;
  90. boost::optional<Core::Crypto::Key128> GetKeyAreaKey(NCASectionCryptoType type) const;
  91. boost::optional<Core::Crypto::Key128> GetTitlekey();
  92. VirtualFile Decrypt(NCASectionHeader header, VirtualFile in, u64 starting_offset);
  93. std::vector<VirtualDir> dirs;
  94. std::vector<VirtualFile> files;
  95. VirtualFile romfs = nullptr;
  96. VirtualDir exefs = nullptr;
  97. VirtualFile file;
  98. VirtualFile bktr_base_romfs;
  99. u64 ivfc_offset;
  100. NCAHeader header{};
  101. bool has_rights_id{};
  102. Loader::ResultStatus status{};
  103. bool encrypted;
  104. bool is_update;
  105. Core::Crypto::KeyManager keys;
  106. };
  107. } // namespace FileSys