title_metadata.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <string>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. #include "common/swap.h"
  9. namespace Loader {
  10. enum class ResultStatus;
  11. }
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. // FileSys namespace
  14. namespace FileSys {
  15. enum TMDSignatureType : u32 {
  16. Rsa4096Sha1 = 0x10000,
  17. Rsa2048Sha1 = 0x10001,
  18. EllipticSha1 = 0x10002,
  19. Rsa4096Sha256 = 0x10003,
  20. Rsa2048Sha256 = 0x10004,
  21. EcdsaSha256 = 0x10005
  22. };
  23. enum TMDContentTypeFlag : u16 {
  24. Encrypted = 1 << 1,
  25. Disc = 1 << 2,
  26. CFM = 1 << 3,
  27. Optional = 1 << 14,
  28. Shared = 1 << 15
  29. };
  30. /**
  31. * Helper which implements an interface to read and write Title Metadata (TMD) files.
  32. * If a file path is provided and the file exists, it can be parsed and used, otherwise
  33. * it must be created. The TMD file can then be interpreted, modified and/or saved.
  34. */
  35. class TitleMetadata {
  36. public:
  37. struct ContentChunk {
  38. u32_be id;
  39. u16_be index;
  40. u16_be type;
  41. u64_be size;
  42. std::array<u8, 0x20> hash;
  43. };
  44. static_assert(sizeof(ContentChunk) == 0x30, "TMD ContentChunk structure size is wrong");
  45. struct ContentInfo {
  46. u16_be index;
  47. u16_be command_count;
  48. std::array<u8, 0x20> hash;
  49. };
  50. static_assert(sizeof(ContentInfo) == 0x24, "TMD ContentInfo structure size is wrong");
  51. #pragma pack(push, 1)
  52. struct Body {
  53. std::array<u8, 0x40> issuer;
  54. u8 version;
  55. u8 ca_crl_version;
  56. u8 signer_crl_version;
  57. u8 reserved;
  58. u64_be system_version;
  59. u64_be title_id;
  60. u32_be title_type;
  61. u16_be group_id;
  62. u32_be savedata_size;
  63. u32_be srl_private_savedata_size;
  64. std::array<u8, 4> reserved_2;
  65. u8 srl_flag;
  66. std::array<u8, 0x31> reserved_3;
  67. u32_be access_rights;
  68. u16_be title_version;
  69. u16_be content_count;
  70. u16_be boot_content;
  71. std::array<u8, 2> reserved_4;
  72. std::array<u8, 0x20> contentinfo_hash;
  73. std::array<ContentInfo, 64> contentinfo;
  74. };
  75. static_assert(sizeof(Body) == 0x9C4, "TMD body structure size is wrong");
  76. #pragma pack(pop)
  77. explicit TitleMetadata(std::string& path) : filepath(std::move(path)) {}
  78. Loader::ResultStatus Load();
  79. Loader::ResultStatus Save();
  80. u64 GetTitleID() const;
  81. u32 GetTitleType() const;
  82. u16 GetTitleVersion() const;
  83. u64 GetSystemVersion() const;
  84. size_t GetContentCount() const;
  85. u32 GetBootContentID() const;
  86. u32 GetManualContentID() const;
  87. u32 GetDLPContentID() const;
  88. void SetTitleID(u64 title_id);
  89. void SetTitleType(u32 type);
  90. void SetTitleVersion(u16 version);
  91. void SetSystemVersion(u64 version);
  92. void AddContentChunk(const ContentChunk& chunk);
  93. void Print() const;
  94. private:
  95. enum TMDContentIndex { Main = 0, Manual = 1, DLP = 2 };
  96. Body tmd_body;
  97. u32_be signature_type;
  98. std::vector<u8> tmd_signature;
  99. std::vector<ContentChunk> tmd_chunks;
  100. std::string filepath;
  101. };
  102. } // namespace FileSys