ncch.h 7.0 KB

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