content_archive.h 3.3 KB

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