registered_cache.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 <functional>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include <boost/container/flat_map.hpp>
  11. #include "common/common_types.h"
  12. #include "core/crypto/key_manager.h"
  13. #include "core/file_sys/vfs.h"
  14. namespace FileSys {
  15. class CNMT;
  16. class NCA;
  17. class NSP;
  18. class XCI;
  19. enum class ContentRecordType : u8;
  20. enum class NCAContentType : u8;
  21. enum class TitleType : u8;
  22. struct ContentRecord;
  23. struct MetaRecord;
  24. class RegisteredCache;
  25. using NcaID = std::array<u8, 0x10>;
  26. using ContentProviderParsingFunction = std::function<VirtualFile(const VirtualFile&, const NcaID&)>;
  27. using VfsCopyFunction = std::function<bool(const VirtualFile&, const VirtualFile&, size_t)>;
  28. enum class InstallResult {
  29. Success,
  30. ErrorAlreadyExists,
  31. ErrorCopyFailed,
  32. ErrorMetaFailed,
  33. };
  34. struct ContentProviderEntry {
  35. u64 title_id;
  36. ContentRecordType type;
  37. std::string DebugInfo() const;
  38. };
  39. constexpr u64 GetUpdateTitleID(u64 base_title_id) {
  40. return base_title_id | 0x800;
  41. }
  42. ContentRecordType GetCRTypeFromNCAType(NCAContentType type);
  43. // boost flat_map requires operator< for O(log(n)) lookups.
  44. bool operator<(const ContentProviderEntry& lhs, const ContentProviderEntry& rhs);
  45. // std unique requires operator== to identify duplicates.
  46. bool operator==(const ContentProviderEntry& lhs, const ContentProviderEntry& rhs);
  47. bool operator!=(const ContentProviderEntry& lhs, const ContentProviderEntry& rhs);
  48. class ContentProvider {
  49. public:
  50. virtual ~ContentProvider();
  51. virtual void Refresh() = 0;
  52. virtual bool HasEntry(u64 title_id, ContentRecordType type) const = 0;
  53. virtual bool HasEntry(ContentProviderEntry entry) const;
  54. virtual std::optional<u32> GetEntryVersion(u64 title_id) const = 0;
  55. virtual VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const = 0;
  56. virtual VirtualFile GetEntryUnparsed(ContentProviderEntry entry) const;
  57. virtual VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const = 0;
  58. virtual VirtualFile GetEntryRaw(ContentProviderEntry entry) const;
  59. virtual std::unique_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const = 0;
  60. virtual std::unique_ptr<NCA> GetEntry(ContentProviderEntry entry) const;
  61. virtual std::vector<ContentProviderEntry> ListEntries() const;
  62. // If a parameter is not std::nullopt, it will be filtered for from all entries.
  63. virtual std::vector<ContentProviderEntry> ListEntriesFilter(
  64. std::optional<TitleType> title_type = {}, std::optional<ContentRecordType> record_type = {},
  65. std::optional<u64> title_id = {}) const = 0;
  66. protected:
  67. // A single instance of KeyManager to be used by GetEntry()
  68. Core::Crypto::KeyManager keys;
  69. };
  70. class PlaceholderCache {
  71. public:
  72. explicit PlaceholderCache(VirtualDir dir);
  73. bool Create(const NcaID& id, u64 size) const;
  74. bool Delete(const NcaID& id) const;
  75. bool Exists(const NcaID& id) const;
  76. bool Write(const NcaID& id, u64 offset, const std::vector<u8>& data) const;
  77. bool Register(RegisteredCache* cache, const NcaID& placeholder, const NcaID& install) const;
  78. bool CleanAll() const;
  79. std::optional<std::array<u8, 0x10>> GetRightsID(const NcaID& id) const;
  80. u64 Size(const NcaID& id) const;
  81. bool SetSize(const NcaID& id, u64 new_size) const;
  82. std::vector<NcaID> List() const;
  83. static NcaID Generate();
  84. private:
  85. VirtualDir dir;
  86. };
  87. /*
  88. * A class that catalogues NCAs in the registered directory structure.
  89. * Nintendo's registered format follows this structure:
  90. *
  91. * Root
  92. * | 000000XX <- XX is the ____ two digits of the NcaID
  93. * | <hash>.nca <- hash is the NcaID (first half of SHA256 over entire file) (folder)
  94. * | 00
  95. * | 01 <- Actual content split along 4GB boundaries. (optional)
  96. *
  97. * (This impl also supports substituting the nca dir for an nca file, as that's more convenient
  98. * when 4GB splitting can be ignored.)
  99. */
  100. class RegisteredCache : public ContentProvider {
  101. friend class PlaceholderCache;
  102. public:
  103. // Parsing function defines the conversion from raw file to NCA. If there are other steps
  104. // besides creating the NCA from the file (e.g. NAX0 on SD Card), that should go in a custom
  105. // parsing function.
  106. explicit RegisteredCache(VirtualDir dir,
  107. ContentProviderParsingFunction parsing_function =
  108. [](const VirtualFile& file, const NcaID& id) { return file; });
  109. ~RegisteredCache() override;
  110. void Refresh() override;
  111. bool HasEntry(u64 title_id, ContentRecordType type) const override;
  112. std::optional<u32> GetEntryVersion(u64 title_id) const override;
  113. VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const override;
  114. VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const override;
  115. std::unique_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const override;
  116. // If a parameter is not std::nullopt, it will be filtered for from all entries.
  117. std::vector<ContentProviderEntry> ListEntriesFilter(
  118. std::optional<TitleType> title_type = {}, std::optional<ContentRecordType> record_type = {},
  119. std::optional<u64> title_id = {}) const override;
  120. // Raw copies all the ncas from the xci/nsp to the csache. Does some quick checks to make sure
  121. // there is a meta NCA and all of them are accessible.
  122. InstallResult InstallEntry(const XCI& xci, bool overwrite_if_exists = false,
  123. const VfsCopyFunction& copy = &VfsRawCopy);
  124. InstallResult InstallEntry(const NSP& nsp, bool overwrite_if_exists = false,
  125. const VfsCopyFunction& copy = &VfsRawCopy);
  126. // Due to the fact that we must use Meta-type NCAs to determine the existance of files, this
  127. // poses quite a challenge. Instead of creating a new meta NCA for this file, yuzu will create a
  128. // dir inside the NAND called 'yuzu_meta' and store the raw CNMT there.
  129. // TODO(DarkLordZach): Author real meta-type NCAs and install those.
  130. InstallResult InstallEntry(const NCA& nca, TitleType type, bool overwrite_if_exists = false,
  131. const VfsCopyFunction& copy = &VfsRawCopy);
  132. private:
  133. template <typename T>
  134. void IterateAllMetadata(std::vector<T>& out,
  135. std::function<T(const CNMT&, const ContentRecord&)> proc,
  136. std::function<bool(const CNMT&, const ContentRecord&)> filter) const;
  137. std::vector<NcaID> AccumulateFiles() const;
  138. void ProcessFiles(const std::vector<NcaID>& ids);
  139. void AccumulateYuzuMeta();
  140. std::optional<NcaID> GetNcaIDFromMetadata(u64 title_id, ContentRecordType type) const;
  141. VirtualFile GetFileAtID(NcaID id) const;
  142. VirtualFile OpenFileOrDirectoryConcat(const VirtualDir& dir, std::string_view path) const;
  143. InstallResult RawInstallNCA(const NCA& nca, const VfsCopyFunction& copy,
  144. bool overwrite_if_exists, std::optional<NcaID> override_id = {});
  145. bool RawInstallYuzuMeta(const CNMT& cnmt);
  146. VirtualDir dir;
  147. ContentProviderParsingFunction parser;
  148. // maps tid -> NcaID of meta
  149. std::map<u64, NcaID> meta_id;
  150. // maps tid -> meta
  151. std::map<u64, CNMT> meta;
  152. // maps tid -> meta for CNMT in yuzu_meta
  153. std::map<u64, CNMT> yuzu_meta;
  154. };
  155. enum class ContentProviderUnionSlot {
  156. SysNAND, ///< System NAND
  157. UserNAND, ///< User NAND
  158. SDMC, ///< SD Card
  159. FrontendManual, ///< Frontend-defined game list or similar
  160. };
  161. // Combines multiple ContentProvider(s) (i.e. SysNAND, UserNAND, SDMC) into one interface.
  162. class ContentProviderUnion : public ContentProvider {
  163. public:
  164. ~ContentProviderUnion() override;
  165. void SetSlot(ContentProviderUnionSlot slot, ContentProvider* provider);
  166. void ClearSlot(ContentProviderUnionSlot slot);
  167. void Refresh() override;
  168. bool HasEntry(u64 title_id, ContentRecordType type) const override;
  169. std::optional<u32> GetEntryVersion(u64 title_id) const override;
  170. VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const override;
  171. VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const override;
  172. std::unique_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const override;
  173. std::vector<ContentProviderEntry> ListEntriesFilter(
  174. std::optional<TitleType> title_type, std::optional<ContentRecordType> record_type,
  175. std::optional<u64> title_id) const override;
  176. std::vector<std::pair<ContentProviderUnionSlot, ContentProviderEntry>> ListEntriesFilterOrigin(
  177. std::optional<ContentProviderUnionSlot> origin = {},
  178. std::optional<TitleType> title_type = {}, std::optional<ContentRecordType> record_type = {},
  179. std::optional<u64> title_id = {}) const;
  180. std::optional<ContentProviderUnionSlot> GetSlotForEntry(u64 title_id,
  181. ContentRecordType type) const;
  182. private:
  183. std::map<ContentProviderUnionSlot, ContentProvider*> providers;
  184. };
  185. class ManualContentProvider : public ContentProvider {
  186. public:
  187. ~ManualContentProvider() override;
  188. void AddEntry(TitleType title_type, ContentRecordType content_type, u64 title_id,
  189. VirtualFile file);
  190. void ClearAllEntries();
  191. void Refresh() override;
  192. bool HasEntry(u64 title_id, ContentRecordType type) const override;
  193. std::optional<u32> GetEntryVersion(u64 title_id) const override;
  194. VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const override;
  195. VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const override;
  196. std::unique_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const override;
  197. std::vector<ContentProviderEntry> ListEntriesFilter(
  198. std::optional<TitleType> title_type, std::optional<ContentRecordType> record_type,
  199. std::optional<u64> title_id) const override;
  200. private:
  201. std::map<std::tuple<TitleType, ContentRecordType, u64>, VirtualFile> entries;
  202. };
  203. } // namespace FileSys