content_archive.h 3.3 KB

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