ncch.h 6.2 KB

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