nca.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #pragma once
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. #include "nx/pfs0.h"
  6. #include "nx/romfs.h"
  7. #include "nx/ncm.h"
  8. #define NCA_HEADER_SIZE 0x400
  9. #define NCA_SECTION_HEADER_SIZE 0x200
  10. #define NCA_SECTOR_SIZE 0x200
  11. #define NCA_XTS_SECTION_SIZE 0xC00
  12. #define NCA_SECTION_TOTAL 0x4
  13. #define MEDIA_REAL(x)((x * 0x200))
  14. #define NCA0_MAGIC 0x3041434E
  15. #define NCA2_MAGIC 0x3241434E
  16. #define NCA3_MAGIC 0x3341434E
  17. #define NCA_PROGRAM_FIRST_SECTION_HEADER_BLOCK_OFFSET 0x400
  18. #define NCA_PROGRAM_SECOND_SECTION_HEADER_BLOCK_OFFSET 0x600
  19. #define NCA_PROGRAM_THIRD_SECTION_HEADER_BLOCK_OFFSET 0x800
  20. #define NCA_PROGRAM_LOGO_OFFSET 0x8000
  21. #define NCA_META_CNMT_OFFSET 0xC20
  22. typedef enum
  23. {
  24. NcaEncrpytMode_Decrypt,
  25. NcaEncrpytMode_Encrypt
  26. } NcaEncrpytMode;
  27. typedef enum
  28. {
  29. NcaDistributionType_System = 0x0,
  30. NcaDistributionType_GameCard = 0x1
  31. } NcaDistributionType;
  32. typedef enum
  33. {
  34. NcaContentType_Program = 0x0,
  35. NcaContentType_Meta = 0x1,
  36. NcaContentType_Control = 0x2,
  37. NcaContentType_Manual = 0x3,
  38. NcaContentType_Data = 0x4,
  39. NcaContentType_PublicData = 0x5,
  40. } NcaContentType;
  41. typedef enum
  42. {
  43. NcaOldKeyGeneration_100 = 0x0,
  44. NcaOldKeyGeneration_Unused = 0x1,
  45. NcaOldKeyGeneration_300 = 0x2,
  46. } NcaOldKeyGeneration;
  47. typedef enum
  48. {
  49. NcaKeyGeneration_301 = 0x3,
  50. NcaKeyGeneration_400 = 0x4,
  51. NcaKeyGeneration_500 = 0x5,
  52. NcaKeyGeneration_600 = 0x6,
  53. NcaKeyGeneration_620 = 0x7,
  54. NcaKeyGeneration_700 = 0x8,
  55. NcaKeyGeneration_810 = 0x9,
  56. NcaKeyGeneration_900 = 0x0A,
  57. NcaKeyGeneration_910 = 0x0B,
  58. NcaKeyGeneration_1210 = 0x0C,
  59. NcaKeyGeneration_1300 = 0x0D,
  60. NcaKeyGeneration_1400 = 0x0E,
  61. NcaKeyGeneration_1500 = 0x0F,
  62. NcaKeyGeneration_Invalid = 0xFF
  63. } NcaKeyGeneration;
  64. typedef enum
  65. {
  66. NcaKeyAreaEncryptionKeyIndex_Application = 0x0,
  67. NcaKeyAreaEncryptionKeyIndex_Ocean = 0x1,
  68. NcaKeyAreaEncryptionKeyIndex_System = 0x2
  69. } NcaKeyAreaEncryptionKeyIndex;
  70. typedef enum
  71. {
  72. NcaFileSystemType_RomFS = 0x0,
  73. NcaFileSystemType_PFS0 = 0x1
  74. } NcaFileSystemType;
  75. typedef enum
  76. {
  77. NcaHashType_Auto = 0x0,
  78. NcaHashType_HierarchicalSha256 = 0x2,
  79. NcaHashType_HierarchicalIntegrity = 0x3
  80. } NcaHashType;
  81. typedef enum
  82. {
  83. NcaEncryptionType_Auto = 0x0,
  84. NcaEncryptionType_None = 0x1,
  85. NcaEncryptionType_AesCtrOld = 0x2,
  86. NcaEncryptionType_AesCtr = 0x3,
  87. NcaEncryptionType_AesCtrEx = 0x4
  88. } NcaEncryptionType;
  89. typedef struct
  90. {
  91. uint32_t media_start_offset; // divided by 0x200.
  92. uint32_t media_end_offset; // divided by 0x200.
  93. uint32_t _0x8; // unkown.
  94. uint32_t _0xC; // unkown.
  95. } NcaSectionTableEntry_t;
  96. typedef struct
  97. {
  98. uint64_t offset;
  99. uint64_t size;
  100. uint32_t magic; // BKTR
  101. uint8_t _0x14[0x4];
  102. uint8_t _0x18[0x4];
  103. uint8_t _0x1C[0x4];
  104. } PatchInfo_t;
  105. typedef struct
  106. {
  107. uint16_t version; // always 2.
  108. uint8_t fs_type; // see NcaFileSystemType.
  109. uint8_t hash_type; // see NcaHashType.
  110. uint8_t encryption_type; // see NcaEncryptionType.
  111. uint8_t _0x5[0x3]; // empty.
  112. union
  113. {
  114. uint8_t section_data[0xF8];
  115. Pfs0Superblock_t pfs0_sb;
  116. RomfsSuperblock_t romfs_sb;
  117. // anything else?????
  118. };
  119. PatchInfo_t patch_info1;
  120. PatchInfo_t patch_info2;
  121. union
  122. {
  123. uint64_t section_ctr;
  124. struct
  125. {
  126. uint32_t section_ctr_low;
  127. uint32_t section_ctr_high;
  128. };
  129. };
  130. uint8_t sparse_info[0x30];
  131. uint8_t _0x178[0x88]; /* Padding. */
  132. } NcaFsHeader_t;
  133. typedef struct
  134. {
  135. uint8_t sha256[0x20];
  136. } NcaSectionHeaderHash_t;
  137. typedef struct
  138. {
  139. uint8_t area[0x10];
  140. } NcaKeyArea_t;
  141. typedef struct
  142. {
  143. uint8_t rights_id[0x10];
  144. } RightsId_t;
  145. typedef struct
  146. {
  147. uint64_t id; // belonging ID.
  148. NcaKeyArea_t key; // the actual key.
  149. } NcaKeySlot_t;
  150. typedef struct
  151. {
  152. uint8_t rsa_fixed_key[0x100];
  153. uint8_t rsa_npdm[0x100]; // key from npdm.
  154. uint32_t magic;
  155. uint8_t distribution_type; // see NcaDistributionType.
  156. uint8_t content_type; // see NcaContentType.
  157. uint8_t old_key_gen; // see NcaOldKeyGeneration.
  158. uint8_t kaek_index; // see NcaKeyAreaEncryptionKeyIndex.
  159. size_t size;
  160. uint64_t title_id;
  161. uint32_t context_id;
  162. uint32_t sdk_version;
  163. uint8_t key_gen; // see NcaKeyGeneration.
  164. uint8_t header_1_sig_key_gen;
  165. uint8_t _0x222[0xE]; // empty.
  166. FsRightsId rights_id;
  167. NcaSectionTableEntry_t section_table[NCA_SECTION_TOTAL];
  168. NcaSectionHeaderHash_t section_header_hash[NCA_SECTION_TOTAL];
  169. NcaKeyArea_t key_area[NCA_SECTION_TOTAL];
  170. uint8_t _0x340[0xC0]; // empty.
  171. NcaFsHeader_t section_header[NCA_SECTION_TOTAL];
  172. } NcaHeader_t;
  173. //
  174. bool nca_check_if_magic_valid(uint32_t magic);
  175. //
  176. const char *nca_get_magic_string(uint32_t magic);
  177. const char *nca_get_distribution_type_string(NcaDistributionType type);
  178. const char *nca_get_content_type_string(NcaContentType type);
  179. const char *nca_get_keak_index_string(NcaKeyAreaEncryptionKeyIndex index);
  180. const char *nca_get_file_system_type_string(NcaFileSystemType type);
  181. const char *nca_get_hash_type_string(NcaHashType type);
  182. const char *nca_get_encryption_type_string(NcaEncryptionType type);
  183. const char *nca_return_key_gen_string(uint8_t key_gen);
  184. //
  185. uint16_t nca_return_key_gen_int(uint8_t key_gen);
  186. bool nca_check_key_gen(uint8_t key_gen);
  187. //
  188. const char *nca_get_string_from_id(const NcmContentId *nca_id, char *out);
  189. const NcmContentId nca_get_id_from_string(const char *nca_in_string);
  190. bool nca_set_keyslot(uint64_t id, const uint8_t *key);
  191. NcaKeySlot_t *nca_get_keyslot(void);
  192. NcaKeyArea_t *nca_get_keyslot_key(void);
  193. uint64_t nca_get_keyslot_id(void);
  194. /*
  195. * nca header stuff.
  196. */
  197. //
  198. void nca_update_lower_ctr(uint8_t *ctr, uint64_t offset);
  199. void nca_update_upper_ctr(uint8_t *ctr, uint64_t offset);
  200. //
  201. bool nca_decrypt_keak(const NcaHeader_t *header, NcaKeyArea_t *out);
  202. bool nca_encrypt_keak(NcaHeader_t *header, const NcaKeyArea_t *decrypted_key, uint8_t key_gen);
  203. //
  204. bool nca_encrypt_header(const NcaHeader_t *in_header, NcaHeader_t *out_header);
  205. bool nca_decrypt_header(const NcaHeader_t *in_header, NcaHeader_t *out_header);
  206. //
  207. bool nca_get_header(FILE *fp, uint64_t offset, NcaHeader_t *header);
  208. bool nca_get_header_decrypted(FILE *fp, uint64_t offset, NcaHeader_t *header);
  209. /*
  210. * nca install stuff.
  211. */
  212. //
  213. bool nca_setup_placeholder(NcmInstall_t *out, size_t size, NcmContentId *content_id, NcmStorageId storage_id);
  214. //
  215. bool nca_start_install(const char *name, const NcmContentId *content_id, uint64_t offset, NcmStorageId storage_id, FILE *fp);
  216. /*
  217. * Debug
  218. */
  219. //
  220. void nca_print_header(const NcaHeader_t *header);
  221. void nca_print_key_area(const NcaKeyArea_t *keak);