ncch.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include "common/common_types.h"
  7. #include "core/loader/loader.h"
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////
  9. /// NCCH header (Note: "NCCH" appears to be a publically unknown acronym)
  10. struct NCCH_Header {
  11. u8 signature[0x100];
  12. u32 magic;
  13. u32 content_size;
  14. u8 partition_id[8];
  15. u16 maker_code;
  16. u16 version;
  17. u8 reserved_0[4];
  18. u8 program_id[8];
  19. u8 reserved_1[0x10];
  20. u8 logo_region_hash[0x20];
  21. u8 product_code[0x10];
  22. u8 extended_header_hash[0x20];
  23. u32 extended_header_size;
  24. u8 reserved_2[4];
  25. u8 flags[8];
  26. u32 plain_region_offset;
  27. u32 plain_region_size;
  28. u32 logo_region_offset;
  29. u32 logo_region_size;
  30. u32 exefs_offset;
  31. u32 exefs_size;
  32. u32 exefs_hash_region_size;
  33. u8 reserved_3[4];
  34. u32 romfs_offset;
  35. u32 romfs_size;
  36. u32 romfs_hash_region_size;
  37. u8 reserved_4[4];
  38. u8 exefs_super_block_hash[0x20];
  39. u8 romfs_super_block_hash[0x20];
  40. };
  41. static_assert(sizeof(NCCH_Header) == 0x200, "NCCH header structure size is wrong");
  42. ////////////////////////////////////////////////////////////////////////////////////////////////////
  43. // ExeFS (executable file system) headers
  44. struct ExeFs_SectionHeader {
  45. char name[8];
  46. u32 offset;
  47. u32 size;
  48. };
  49. struct ExeFs_Header {
  50. ExeFs_SectionHeader section[8];
  51. u8 reserved[0x80];
  52. u8 hashes[8][0x20];
  53. };
  54. ////////////////////////////////////////////////////////////////////////////////////////////////////
  55. // ExHeader (executable file system header) headers
  56. struct ExHeader_SystemInfoFlags{
  57. u8 reserved[5];
  58. u8 flag;
  59. u8 remaster_version[2];
  60. };
  61. struct ExHeader_CodeSegmentInfo{
  62. u32 address;
  63. u32 num_max_pages;
  64. u32 code_size;
  65. };
  66. struct ExHeader_CodeSetInfo {
  67. u8 name[8];
  68. ExHeader_SystemInfoFlags flags;
  69. ExHeader_CodeSegmentInfo text;
  70. u32 stack_size;
  71. ExHeader_CodeSegmentInfo ro;
  72. u8 reserved[4];
  73. ExHeader_CodeSegmentInfo data;
  74. u32 bss_size;
  75. };
  76. struct ExHeader_DependencyList{
  77. u8 program_id[0x30][8];
  78. };
  79. struct ExHeader_SystemInfo{
  80. u64 save_data_size;
  81. u8 jump_id[8];
  82. u8 reserved_2[0x30];
  83. };
  84. struct ExHeader_StorageInfo{
  85. u8 ext_save_data_id[8];
  86. u8 system_save_data_id[8];
  87. u8 reserved[8];
  88. u8 access_info[7];
  89. u8 other_attributes;
  90. };
  91. struct ExHeader_ARM11_SystemLocalCaps{
  92. u8 program_id[8];
  93. u32 core_version;
  94. u8 flags[3];
  95. u8 priority;
  96. u8 resource_limit_descriptor[0x10][2];
  97. ExHeader_StorageInfo storage_info;
  98. u8 service_access_control[0x20][8];
  99. u8 ex_service_access_control[0x2][8];
  100. u8 reserved[0xf];
  101. u8 resource_limit_category;
  102. };
  103. struct ExHeader_ARM11_KernelCaps{
  104. u8 descriptors[28][4];
  105. u8 reserved[0x10];
  106. };
  107. struct ExHeader_ARM9_AccessControl{
  108. u8 descriptors[15];
  109. u8 descversion;
  110. };
  111. struct ExHeader_Header{
  112. ExHeader_CodeSetInfo codeset_info;
  113. ExHeader_DependencyList dependency_list;
  114. ExHeader_SystemInfo system_info;
  115. ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps;
  116. ExHeader_ARM11_KernelCaps arm11_kernel_caps;
  117. ExHeader_ARM9_AccessControl arm9_access_control;
  118. struct {
  119. u8 signature[0x100];
  120. u8 ncch_public_key_modulus[0x100];
  121. ExHeader_ARM11_SystemLocalCaps arm11_system_local_caps;
  122. ExHeader_ARM11_KernelCaps arm11_kernel_caps;
  123. ExHeader_ARM9_AccessControl arm9_access_control;
  124. } access_desc;
  125. };
  126. static_assert(sizeof(ExHeader_Header) == 0x800, "ExHeader structure size is wrong");
  127. ////////////////////////////////////////////////////////////////////////////////////////////////////
  128. // Loader namespace
  129. namespace Loader {
  130. /// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI)
  131. class AppLoader_NCCH final : public AppLoader {
  132. public:
  133. AppLoader_NCCH(std::unique_ptr<FileUtil::IOFile>&& file) : AppLoader(std::move(file)) { }
  134. /**
  135. * Returns the type of the file
  136. * @param file FileUtil::IOFile open file
  137. * @return FileType found, or FileType::Error if this loader doesn't know it
  138. */
  139. static FileType IdentifyType(FileUtil::IOFile& file);
  140. /**
  141. * Load the application
  142. * @return ResultStatus result of function
  143. */
  144. ResultStatus Load() override;
  145. /**
  146. * Get the code (typically .code section) of the application
  147. * @param buffer Reference to buffer to store data
  148. * @return ResultStatus result of function
  149. */
  150. ResultStatus ReadCode(std::vector<u8>& buffer) const override;
  151. /**
  152. * Get the icon (typically icon section) of the application
  153. * @param buffer Reference to buffer to store data
  154. * @return ResultStatus result of function
  155. */
  156. ResultStatus ReadIcon(std::vector<u8>& buffer) const override;
  157. /**
  158. * Get the banner (typically banner section) of the application
  159. * @param buffer Reference to buffer to store data
  160. * @return ResultStatus result of function
  161. */
  162. ResultStatus ReadBanner(std::vector<u8>& buffer) const override;
  163. /**
  164. * Get the logo (typically logo section) of the application
  165. * @param buffer Reference to buffer to store data
  166. * @return ResultStatus result of function
  167. */
  168. ResultStatus ReadLogo(std::vector<u8>& buffer) const override;
  169. /**
  170. * Get the RomFS of the application
  171. * @param buffer Reference to buffer to store data
  172. * @return ResultStatus result of function
  173. */
  174. ResultStatus ReadRomFS(std::vector<u8>& buffer) const override;
  175. /*
  176. * Gets the program id from the NCCH header
  177. * @return u64 Program id
  178. */
  179. u64 GetProgramId() const;
  180. private:
  181. /**
  182. * Reads an application ExeFS section of an NCCH file into AppLoader (e.g. .code, .logo, etc.)
  183. * @param name Name of section to read out of NCCH file
  184. * @param buffer Vector to read data into
  185. * @return ResultStatus result of function
  186. */
  187. ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const;
  188. /**
  189. * Loads .code section into memory for booting
  190. * @return ResultStatus result of function
  191. */
  192. ResultStatus LoadExec() const;
  193. bool is_compressed = false;
  194. u32 entry_point = 0;
  195. u32 code_size = 0;
  196. u32 stack_size = 0;
  197. u32 bss_size = 0;
  198. u32 core_version = 0;
  199. u8 priority = 0;
  200. u8 resource_limit_category = 0;
  201. u32 ncch_offset = 0; // Offset to NCCH header, can be 0 or after NCSD header
  202. u32 exefs_offset = 0;
  203. NCCH_Header ncch_header;
  204. ExeFs_Header exefs_header;
  205. ExHeader_Header exheader_header;
  206. };
  207. } // namespace Loader