program_metadata.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <vector>
  6. #include "common/bit_field.h"
  7. #include "common/common_funcs.h"
  8. #include "common/common_types.h"
  9. #include "common/swap.h"
  10. #include "core/file_sys/vfs_types.h"
  11. namespace Loader {
  12. enum class ResultStatus : u16;
  13. }
  14. namespace FileSys {
  15. enum class ProgramAddressSpaceType : u8 {
  16. Is32Bit = 0,
  17. Is36Bit = 1,
  18. Is32BitNoMap = 2,
  19. Is39Bit = 3,
  20. };
  21. enum class ProgramFilePermission : u64 {
  22. MountContent = 1ULL << 0,
  23. SaveDataBackup = 1ULL << 5,
  24. SdCard = 1ULL << 21,
  25. Calibration = 1ULL << 34,
  26. Bit62 = 1ULL << 62,
  27. Everything = 1ULL << 63,
  28. };
  29. enum class PoolPartition : u32 {
  30. Application = 0,
  31. Applet = 1,
  32. System = 2,
  33. SystemNonSecure = 3,
  34. };
  35. /**
  36. * Helper which implements an interface to parse Program Description Metadata (NPDM)
  37. * Data can either be loaded from a file path or with data and an offset into it.
  38. */
  39. class ProgramMetadata {
  40. public:
  41. using KernelCapabilityDescriptors = std::vector<u32>;
  42. ProgramMetadata();
  43. ~ProgramMetadata();
  44. ProgramMetadata(const ProgramMetadata&) = default;
  45. ProgramMetadata& operator=(const ProgramMetadata&) = default;
  46. ProgramMetadata(ProgramMetadata&&) = default;
  47. ProgramMetadata& operator=(ProgramMetadata&&) = default;
  48. /// Gets a default ProgramMetadata configuration, should only be used for homebrew formats where
  49. /// we do not have an NPDM file
  50. static ProgramMetadata GetDefault();
  51. Loader::ResultStatus Load(VirtualFile file);
  52. Loader::ResultStatus Reload(VirtualFile file);
  53. /// Load from parameters instead of NPDM file, used for KIP
  54. void LoadManual(bool is_64_bit, ProgramAddressSpaceType address_space, s32 main_thread_prio,
  55. u32 main_thread_core, u32 main_thread_stack_size, u64 title_id,
  56. u64 filesystem_permissions, u32 system_resource_size,
  57. KernelCapabilityDescriptors capabilities);
  58. bool Is64BitProgram() const;
  59. ProgramAddressSpaceType GetAddressSpaceType() const;
  60. u8 GetMainThreadPriority() const;
  61. u8 GetMainThreadCore() const;
  62. u32 GetMainThreadStackSize() const;
  63. u64 GetTitleID() const;
  64. u64 GetFilesystemPermissions() const;
  65. u32 GetSystemResourceSize() const;
  66. PoolPartition GetPoolPartition() const;
  67. const KernelCapabilityDescriptors& GetKernelCapabilities() const;
  68. const std::array<u8, 0x10>& GetName() const {
  69. return npdm_header.application_name;
  70. }
  71. void Print() const;
  72. private:
  73. struct Header {
  74. std::array<char, 4> magic;
  75. std::array<u8, 8> reserved;
  76. union {
  77. u8 flags;
  78. BitField<0, 1, u8> has_64_bit_instructions;
  79. BitField<1, 3, ProgramAddressSpaceType> address_space_type;
  80. BitField<4, 4, u8> reserved_2;
  81. };
  82. u8 reserved_3;
  83. u8 main_thread_priority;
  84. u8 main_thread_cpu;
  85. std::array<u8, 4> reserved_4;
  86. u32_le system_resource_size;
  87. u32_le process_category;
  88. u32_le main_stack_size;
  89. std::array<u8, 0x10> application_name;
  90. std::array<u8, 0x40> reserved_5;
  91. u32_le aci_offset;
  92. u32_le aci_size;
  93. u32_le acid_offset;
  94. u32_le acid_size;
  95. };
  96. static_assert(sizeof(Header) == 0x80, "NPDM header structure size is wrong");
  97. struct AcidHeader {
  98. std::array<u8, 0x100> signature;
  99. std::array<u8, 0x100> nca_modulus;
  100. std::array<char, 4> magic;
  101. u32_le nca_size;
  102. std::array<u8, 0x4> reserved;
  103. union {
  104. u32 flags;
  105. BitField<0, 1, u32> production_flag;
  106. BitField<1, 1, u32> unqualified_approval;
  107. BitField<2, 4, PoolPartition> pool_partition;
  108. };
  109. u64_le title_id_min;
  110. u64_le title_id_max;
  111. u32_le fac_offset;
  112. u32_le fac_size;
  113. u32_le sac_offset;
  114. u32_le sac_size;
  115. u32_le kac_offset;
  116. u32_le kac_size;
  117. INSERT_PADDING_BYTES(0x8);
  118. };
  119. static_assert(sizeof(AcidHeader) == 0x240, "ACID header structure size is wrong");
  120. struct AciHeader {
  121. std::array<char, 4> magic;
  122. std::array<u8, 0xC> reserved;
  123. u64_le title_id;
  124. INSERT_PADDING_BYTES(0x8);
  125. u32_le fah_offset;
  126. u32_le fah_size;
  127. u32_le sac_offset;
  128. u32_le sac_size;
  129. u32_le kac_offset;
  130. u32_le kac_size;
  131. INSERT_PADDING_BYTES(0x8);
  132. };
  133. static_assert(sizeof(AciHeader) == 0x40, "ACI0 header structure size is wrong");
  134. // FileAccessControl and FileAccessHeader need loaded per-component: this layout does not
  135. // reflect the real layout to avoid reference binding to misaligned addresses
  136. struct FileAccessControl {
  137. u8 version;
  138. // 3 padding bytes
  139. u64_le permissions;
  140. std::array<u8, 0x20> unknown;
  141. };
  142. struct FileAccessHeader {
  143. u8 version;
  144. // 3 padding bytes
  145. u64_le permissions;
  146. u32_le unk_offset;
  147. u32_le unk_size;
  148. u32_le unk_offset_2;
  149. u32_le unk_size_2;
  150. };
  151. Header npdm_header{};
  152. AciHeader aci_header{};
  153. AcidHeader acid_header{};
  154. FileAccessControl acid_file_access{};
  155. FileAccessHeader aci_file_access{};
  156. KernelCapabilityDescriptors aci_kernel_capabilities{};
  157. };
  158. } // namespace FileSys