ncch.h 6.0 KB

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