program_metadata.h 3.9 KB

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