program_metadata.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /**
  30. * Helper which implements an interface to parse Program Description Metadata (NPDM)
  31. * Data can either be loaded from a file path or with data and an offset into it.
  32. */
  33. class ProgramMetadata {
  34. public:
  35. using KernelCapabilityDescriptors = std::vector<u32>;
  36. ProgramMetadata();
  37. ~ProgramMetadata();
  38. ProgramMetadata(const ProgramMetadata&) = default;
  39. ProgramMetadata& operator=(const ProgramMetadata&) = default;
  40. ProgramMetadata(ProgramMetadata&&) = default;
  41. ProgramMetadata& operator=(ProgramMetadata&&) = default;
  42. /// Gets a default ProgramMetadata configuration, should only be used for homebrew formats where
  43. /// we do not have an NPDM file
  44. static ProgramMetadata GetDefault();
  45. Loader::ResultStatus Load(VirtualFile file);
  46. /// Load from parameters instead of NPDM file, used for KIP
  47. void LoadManual(bool is_64_bit, ProgramAddressSpaceType address_space, s32 main_thread_prio,
  48. u32 main_thread_core, u32 main_thread_stack_size, u64 title_id,
  49. u64 filesystem_permissions, u32 system_resource_size,
  50. KernelCapabilityDescriptors capabilities);
  51. bool Is64BitProgram() const;
  52. ProgramAddressSpaceType GetAddressSpaceType() const;
  53. u8 GetMainThreadPriority() const;
  54. u8 GetMainThreadCore() const;
  55. u32 GetMainThreadStackSize() const;
  56. u64 GetTitleID() const;
  57. u64 GetFilesystemPermissions() const;
  58. u32 GetSystemResourceSize() const;
  59. const KernelCapabilityDescriptors& GetKernelCapabilities() const;
  60. void Print() const;
  61. private:
  62. struct Header {
  63. std::array<char, 4> magic;
  64. std::array<u8, 8> reserved;
  65. union {
  66. u8 flags;
  67. BitField<0, 1, u8> has_64_bit_instructions;
  68. BitField<1, 3, ProgramAddressSpaceType> address_space_type;
  69. BitField<4, 4, u8> reserved_2;
  70. };
  71. u8 reserved_3;
  72. u8 main_thread_priority;
  73. u8 main_thread_cpu;
  74. std::array<u8, 4> reserved_4;
  75. u32_le system_resource_size;
  76. u32_le process_category;
  77. u32_le main_stack_size;
  78. std::array<u8, 0x10> application_name;
  79. std::array<u8, 0x40> reserved_5;
  80. u32_le aci_offset;
  81. u32_le aci_size;
  82. u32_le acid_offset;
  83. u32_le acid_size;
  84. };
  85. static_assert(sizeof(Header) == 0x80, "NPDM header structure size is wrong");
  86. struct AcidHeader {
  87. std::array<u8, 0x100> signature;
  88. std::array<u8, 0x100> nca_modulus;
  89. std::array<char, 4> magic;
  90. u32_le nca_size;
  91. std::array<u8, 0x4> reserved;
  92. union {
  93. u32 flags;
  94. BitField<0, 1, u32> is_retail;
  95. BitField<1, 31, u32> flags_unk;
  96. };
  97. u64_le title_id_min;
  98. u64_le title_id_max;
  99. u32_le fac_offset;
  100. u32_le fac_size;
  101. u32_le sac_offset;
  102. u32_le sac_size;
  103. u32_le kac_offset;
  104. u32_le kac_size;
  105. INSERT_PADDING_BYTES(0x8);
  106. };
  107. static_assert(sizeof(AcidHeader) == 0x240, "ACID header structure size is wrong");
  108. struct AciHeader {
  109. std::array<char, 4> magic;
  110. std::array<u8, 0xC> reserved;
  111. u64_le title_id;
  112. INSERT_PADDING_BYTES(0x8);
  113. u32_le fah_offset;
  114. u32_le fah_size;
  115. u32_le sac_offset;
  116. u32_le sac_size;
  117. u32_le kac_offset;
  118. u32_le kac_size;
  119. INSERT_PADDING_BYTES(0x8);
  120. };
  121. static_assert(sizeof(AciHeader) == 0x40, "ACI0 header structure size is wrong");
  122. // FileAccessControl and FileAccessHeader need loaded per-component: this layout does not
  123. // reflect the real layout to avoid reference binding to misaligned addresses
  124. struct FileAccessControl {
  125. u8 version;
  126. // 3 padding bytes
  127. u64_le permissions;
  128. std::array<u8, 0x20> unknown;
  129. };
  130. struct FileAccessHeader {
  131. u8 version;
  132. // 3 padding bytes
  133. u64_le permissions;
  134. u32_le unk_offset;
  135. u32_le unk_size;
  136. u32_le unk_offset_2;
  137. u32_le unk_size_2;
  138. };
  139. Header npdm_header;
  140. AciHeader aci_header;
  141. AcidHeader acid_header;
  142. FileAccessControl acid_file_access;
  143. FileAccessHeader aci_file_access;
  144. KernelCapabilityDescriptors aci_kernel_capabilities;
  145. };
  146. } // namespace FileSys