program_metadata.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <string>
  7. #include <vector>
  8. #include "common/bit_field.h"
  9. #include "common/common_types.h"
  10. #include "common/swap.h"
  11. #include "partition_filesystem.h"
  12. namespace Loader {
  13. enum class ResultStatus;
  14. }
  15. namespace FileSys {
  16. enum class ProgramAddressSpaceType : u8 {
  17. Is64Bit = 1,
  18. Is32Bit = 2,
  19. };
  20. enum class ProgramFilePermission : u64 {
  21. MountContent = 1ULL << 0,
  22. SaveDataBackup = 1ULL << 5,
  23. SdCard = 1ULL << 21,
  24. Calibration = 1ULL << 34,
  25. Bit62 = 1ULL << 62,
  26. Everything = 1ULL << 63,
  27. };
  28. /**
  29. * Helper which implements an interface to parse Program Description Metadata (NPDM)
  30. * Data can either be loaded from a file path or with data and an offset into it.
  31. */
  32. class ProgramMetadata {
  33. public:
  34. Loader::ResultStatus Load(VirtualFile file);
  35. bool Is64BitProgram() const;
  36. ProgramAddressSpaceType GetAddressSpaceType() const;
  37. u8 GetMainThreadPriority() const;
  38. u8 GetMainThreadCore() const;
  39. u32 GetMainThreadStackSize() const;
  40. u64 GetTitleID() const;
  41. u64 GetFilesystemPermissions() const;
  42. void Print() const;
  43. private:
  44. // TODO(DarkLordZach): BitField is not trivially copyable.
  45. struct Header {
  46. std::array<char, 4> magic;
  47. std::array<u8, 8> reserved;
  48. union {
  49. u8 flags;
  50. BitField<0, 1, u8> has_64_bit_instructions;
  51. BitField<1, 3, ProgramAddressSpaceType> address_space_type;
  52. BitField<4, 4, u8> reserved_2;
  53. };
  54. u8 reserved_3;
  55. u8 main_thread_priority;
  56. u8 main_thread_cpu;
  57. std::array<u8, 8> reserved_4;
  58. u32_le process_category;
  59. u32_le main_stack_size;
  60. std::array<u8, 0x10> application_name;
  61. std::array<u8, 0x40> reserved_5;
  62. u32_le aci_offset;
  63. u32_le aci_size;
  64. u32_le acid_offset;
  65. u32_le acid_size;
  66. };
  67. static_assert(sizeof(Header) == 0x80, "NPDM header structure size is wrong");
  68. // TODO(DarkLordZach): BitField is not trivially copyable.
  69. struct AcidHeader {
  70. std::array<u8, 0x100> signature;
  71. std::array<u8, 0x100> nca_modulus;
  72. std::array<char, 4> magic;
  73. u32_le nca_size;
  74. std::array<u8, 0x4> reserved;
  75. union {
  76. u32 flags;
  77. BitField<0, 1, u32> is_retail;
  78. BitField<1, 31, u32> flags_unk;
  79. };
  80. u64_le title_id_min;
  81. u64_le title_id_max;
  82. u32_le fac_offset;
  83. u32_le fac_size;
  84. u32_le sac_offset;
  85. u32_le sac_size;
  86. u32_le kac_offset;
  87. u32_le kac_size;
  88. INSERT_PADDING_BYTES(0x8);
  89. };
  90. static_assert(sizeof(AcidHeader) == 0x240, "ACID header structure size is wrong");
  91. struct AciHeader {
  92. std::array<char, 4> magic;
  93. std::array<u8, 0xC> reserved;
  94. u64_le title_id;
  95. INSERT_PADDING_BYTES(0x8);
  96. u32_le fah_offset;
  97. u32_le fah_size;
  98. u32_le sac_offset;
  99. u32_le sac_size;
  100. u32_le kac_offset;
  101. u32_le kac_size;
  102. INSERT_PADDING_BYTES(0x8);
  103. };
  104. static_assert(sizeof(AciHeader) == 0x40, "ACI0 header structure size is wrong");
  105. #pragma pack(push, 1)
  106. struct FileAccessControl {
  107. u8 version;
  108. INSERT_PADDING_BYTES(3);
  109. u64_le permissions;
  110. std::array<u8, 0x20> unknown;
  111. };
  112. static_assert(sizeof(FileAccessControl) == 0x2C, "FS access control structure size is wrong");
  113. struct FileAccessHeader {
  114. u8 version;
  115. INSERT_PADDING_BYTES(3);
  116. u64_le permissions;
  117. u32_le unk_offset;
  118. u32_le unk_size;
  119. u32_le unk_offset_2;
  120. u32_le unk_size_2;
  121. };
  122. static_assert(sizeof(FileAccessHeader) == 0x1C, "FS access header structure size is wrong");
  123. #pragma pack(pop)
  124. Header npdm_header;
  125. AciHeader aci_header;
  126. AcidHeader acid_header;
  127. FileAccessControl acid_file_access;
  128. FileAccessHeader aci_file_access;
  129. };
  130. } // namespace FileSys