gc.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /*
  2. * This code is bad.
  3. * Don't read it.
  4. * It works, that's all that matters.
  5. *
  6. * I'll clean it up one day. Today is not that day.
  7. */
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <switch.h>
  13. #include "ui/gc.h"
  14. #include "ui/menu.h"
  15. #include "ui/settings.h"
  16. #include "nx/fs.h"
  17. #include "nx/ns.h"
  18. #include "nx/nca.h"
  19. #include "nx/ncm.h"
  20. #include "nx/cnmt.h"
  21. #include "nx/set.h"
  22. #include "nx/crypto.h"
  23. #include "nx/es.h"
  24. #include "gfx/image.h"
  25. #include "gfx/text.h"
  26. #include "util/dir.h"
  27. #include "util/file.h"
  28. #include "util/util.h"
  29. #include "util/error.h"
  30. #include "util/log.h"
  31. typedef struct
  32. {
  33. uint64_t id;
  34. uint16_t base_total;
  35. uint16_t upp_total;
  36. uint16_t dlc_total;
  37. } sort_t;
  38. bool g_gamecard_mounted = false;
  39. uint16_t g_game_pos = 0;
  40. GameCard_t GAMECARD = {0};
  41. FsDeviceOperator g_dop = {0};
  42. FsGameCardHandle g_gc_handle = {0};
  43. bool init_gc(void)
  44. {
  45. if (!fs_open_device_operator(&g_dop))
  46. {
  47. write_log("failed to mount gcop\n");
  48. ui_display_error_box(ErrorCode_Init_Gc, __func__);
  49. return false;
  50. }
  51. return true;
  52. }
  53. void exit_gc(void)
  54. {
  55. gc_unmount();
  56. fs_close_device_operator(&g_dop);
  57. }
  58. bool gc_poll(void)
  59. {
  60. return fs_is_gamecard_inserted(&g_dop);
  61. }
  62. /*
  63. * GameCard Mount / Unmount.
  64. */
  65. bool __gc_setup_file_tabel(GameCard_t *gamecard)
  66. {
  67. if (!gamecard)
  68. {
  69. write_log("missing args in %s\n", __func__);
  70. return false;
  71. }
  72. DIR *dir = opendir(".");
  73. struct dirent *d = {0};
  74. if (!dir)
  75. {
  76. write_log("failed to open folder %s\n", __func__);
  77. return false;
  78. }
  79. memset(&gamecard->file_table, 0, sizeof(GameCardFileTable_t));
  80. while ((d = readdir(dir)))
  81. {
  82. char *found = strchr(d->d_name, '.');
  83. if (strcmp(found, ".cnmt.nca") == 0)
  84. gamecard->file_table.cnmt_count++;
  85. else if (strcmp(found, ".nca") == 0)
  86. gamecard->file_table.nca_count++;
  87. else if (strcmp(found, ".tik") == 0)
  88. gamecard->file_table.tik_count++;
  89. else if (strcmp(found, ".cert") == 0)
  90. gamecard->file_table.cert_count++;
  91. gamecard->file_table.file_count++;
  92. }
  93. closedir(dir);
  94. return true;
  95. }
  96. bool __gc_setup_string_table(GameCard_t *gamecard)
  97. {
  98. if (!gamecard)
  99. {
  100. write_log("missing args in %s\n", __func__);
  101. return false;
  102. }
  103. gamecard->string_table = calloc(gamecard->file_table.file_count, sizeof(GameCardStringTable_t));
  104. if (!gamecard->string_table)
  105. {
  106. write_log("no file table");
  107. return false;
  108. }
  109. DIR *dir = opendir(".");
  110. struct dirent *d = {0};
  111. if (!dir)
  112. {
  113. write_log("failed to open folder %s\n", __func__);
  114. ui_display_error_box(ErrorCode_OpenDir, __func__);
  115. return false;
  116. }
  117. uint16_t i = 0;
  118. while ((d = readdir(dir)) && i < gamecard->file_table.file_count)
  119. {
  120. strncpy(gamecard->string_table[i].name, d->d_name, 0x30);
  121. write_log("table %s\n", gamecard->string_table[i].name);
  122. i++;
  123. }
  124. closedir(dir);
  125. return true;
  126. }
  127. bool __id_match_check(sort_t *sorts, uint16_t count, uint16_t *pos, uint64_t id, NcmContentMetaType type)
  128. {
  129. for (uint16_t i = 0; i < count; i++)
  130. {
  131. if (sorts[i].id == ncm_get_app_id_from_title_id(id, type))
  132. {
  133. *pos = i;
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. bool __idk_what_to_call_this(sort_t *sorts, uint16_t pos, NcmContentMetaType type)
  140. {
  141. if (!sorts)
  142. {
  143. write_log("missing params in %s\n", __func__);
  144. return false;
  145. }
  146. switch (type)
  147. {
  148. case NcmContentMetaType_Application:
  149. sorts[pos].base_total++;
  150. return true;
  151. case NcmContentMetaType_Patch:
  152. sorts[pos].upp_total++;
  153. return true;
  154. case NcmContentMetaType_AddOnContent:
  155. sorts[pos].dlc_total++;
  156. return true;
  157. default:
  158. write_log("got incorrect ncm_meta_type: %u\n", type);
  159. return false;
  160. }
  161. }
  162. bool __gc_setup_entry(GameCardGameEntries_t *entries, const Cnmt_t *cnmt, uint8_t key_gen, uint16_t pos, NcmContentMetaType type)
  163. {
  164. if (!entries || !cnmt)
  165. {
  166. write_log("missing params in __gc_sort_cnmt\n");
  167. return false;
  168. }
  169. switch (type)
  170. {
  171. case NcmContentMetaType_Application:
  172. memcpy(&GAMECARD.entries[pos].base[GAMECARD.entries[pos].base_count].cnmt, cnmt, sizeof(Cnmt_t));
  173. GAMECARD.entries[pos].base[GAMECARD.entries[pos].base_count].size = ncm_calculate_content_infos_size(GAMECARD.entries[pos].base[GAMECARD.entries[pos].base_count].cnmt.content_infos, GAMECARD.entries[pos].base[GAMECARD.entries[pos].base_count].cnmt.header.content_count);
  174. GAMECARD.entries[pos].base_size += GAMECARD.entries[pos].base[GAMECARD.entries[pos].base_count].size;
  175. GAMECARD.entries[pos].total_size += GAMECARD.entries[pos].base[GAMECARD.entries[pos].base_count].size;
  176. GAMECARD.entries[pos].base[GAMECARD.entries[pos].base_count].key_gen = key_gen;
  177. GAMECARD.entries[pos].base_count++;
  178. break;
  179. case NcmContentMetaType_Patch:
  180. memcpy(&GAMECARD.entries[pos].upp[GAMECARD.entries[pos].upp_count].cnmt, cnmt, sizeof(Cnmt_t));
  181. GAMECARD.entries[pos].upp[GAMECARD.entries[pos].base_count].size = ncm_calculate_content_infos_size(GAMECARD.entries[pos].upp[GAMECARD.entries[pos].upp_count].cnmt.content_infos, GAMECARD.entries[pos].upp[GAMECARD.entries[pos].upp_count].cnmt.header.content_count);
  182. GAMECARD.entries[pos].upp_size += GAMECARD.entries[pos].upp[GAMECARD.entries[pos].upp_count].size;
  183. GAMECARD.entries[pos].total_size += GAMECARD.entries[pos].upp[GAMECARD.entries[pos].upp_count].size;
  184. GAMECARD.entries[pos].upp[GAMECARD.entries[pos].upp_count].key_gen = key_gen;
  185. GAMECARD.entries[pos].upp_count++;
  186. break;
  187. case NcmContentMetaType_AddOnContent:
  188. memcpy(&GAMECARD.entries[pos].dlc[GAMECARD.entries[pos].dlc_count].cnmt, cnmt, sizeof(Cnmt_t));
  189. GAMECARD.entries[pos].dlc[GAMECARD.entries[pos].dlc_count].size = ncm_calculate_content_infos_size(GAMECARD.entries[pos].dlc[GAMECARD.entries[pos].dlc_count].cnmt.content_infos, GAMECARD.entries[pos].dlc[GAMECARD.entries[pos].dlc_count].cnmt.header.content_count);
  190. GAMECARD.entries[pos].dlc_size += GAMECARD.entries[pos].dlc[GAMECARD.entries[pos].dlc_count].size;
  191. GAMECARD.entries[pos].total_size += GAMECARD.entries[pos].dlc[GAMECARD.entries[pos].dlc_count].size;
  192. GAMECARD.entries[pos].dlc[GAMECARD.entries[pos].dlc_count].key_gen = key_gen;
  193. GAMECARD.entries[pos].dlc_count++;
  194. break;
  195. default:
  196. write_log("got incorrect ncm_meta_type: %u\n", type);
  197. return false;
  198. }
  199. GAMECARD.entries[pos].total_count++;
  200. return true;
  201. }
  202. bool __gc_parse_cnmt(void)
  203. {
  204. // get the total cnmt.nca.
  205. if (!__gc_setup_file_tabel(&GAMECARD))
  206. {
  207. //TODO
  208. return false;
  209. }
  210. if (!GAMECARD.file_table.cnmt_count)
  211. {
  212. write_log("no cnmt's found!? How on earth is that possible\n");
  213. ui_display_error_box(ErrorCode_Mount_NoMeta, __func__);
  214. return false;
  215. }
  216. if (!__gc_setup_string_table(&GAMECARD))
  217. {
  218. //TODO
  219. return false;
  220. }
  221. Cnmt_t *cnmt = calloc(GAMECARD.file_table.cnmt_count, sizeof(Cnmt_t));
  222. if (!cnmt)
  223. {
  224. write_log("failed to alloc cnmt\n");
  225. ui_display_error_box(ErrorCode_Alloc, __func__);
  226. return false;
  227. }
  228. sort_t *sorts = calloc(GAMECARD.file_table.cnmt_count, sizeof(sort_t));
  229. if (!sorts)
  230. {
  231. write_log("failed to alloc sorts\n");
  232. free(cnmt);
  233. ui_display_error_box(ErrorCode_Alloc, __func__);
  234. return false;
  235. }
  236. uint8_t *key_gens = calloc(GAMECARD.file_table.cnmt_count, sizeof(uint8_t));
  237. if (!key_gens)
  238. {
  239. write_log("failed to alloc key_gens\n");
  240. free(sorts);
  241. free(cnmt);
  242. ui_display_error_box(ErrorCode_Alloc, __func__);
  243. return false;
  244. }
  245. // lets get each cnmt and parse it.
  246. for (uint16_t i = 0, j = 0; i < GAMECARD.file_table.file_count && j < GAMECARD.file_table.cnmt_count; i++)
  247. {
  248. // we only want cnmt.nca.
  249. if (!strstr(GAMECARD.string_table[i].name, "cnmt.nca"))
  250. {
  251. continue;
  252. }
  253. // open cnmt.nca
  254. FILE *fp = fopen(GAMECARD.string_table[i].name, "rb");
  255. if (!fp)
  256. {
  257. write_log("failed to open %s\n", GAMECARD.string_table[i].name);
  258. free(key_gens);
  259. free(cnmt);
  260. free(sorts);
  261. ui_display_error_box(ErrorCode_OpenFile, __func__);
  262. return false;
  263. }
  264. // get the decrypted header.
  265. NcaHeader_t header = {0};
  266. if (!nca_get_header_decrypted(fp, 0, &header))
  267. {
  268. free(key_gens);
  269. free(cnmt);
  270. free(sorts);
  271. fclose(fp);
  272. ui_display_error_box(ErrorCode_DecryptNcaHeader, __func__);
  273. return false;
  274. }
  275. nca_print_header(&header);
  276. // sort keygen.
  277. key_gens[j] = header.key_gen ? header.key_gen : header.old_key_gen;
  278. // check if we can decrypt the keak.
  279. if (!crypto_has_key_gen(header.kaek_index, key_gens[j]))
  280. {
  281. free(key_gens);
  282. free(cnmt);
  283. free(sorts);
  284. fclose(fp);
  285. ui_display_error_box(ErrorCode_KeyGen, __func__);
  286. }
  287. // decrypt the keak. (cnmt.nca are always standard crypto).
  288. NcaKeyArea_t decrypted_key = {0};
  289. if (!nca_decrypt_keak(&header, &decrypted_key))
  290. {
  291. write_log("failed to decrypt stuff\n");
  292. free(key_gens);
  293. free(cnmt);
  294. free(sorts);
  295. fclose(fp);
  296. ui_display_error_box(ErrorCode_DecryptNcaKeak, __func__);
  297. return false;
  298. }
  299. // ensure that the first cnmt section is pfs0 (should always be true).
  300. if (header.section_header[0].fs_type != NcaFileSystemType_PFS0)
  301. {
  302. write_log("cnmt section 0 is not a pfs0!\n");
  303. free(key_gens);
  304. free(cnmt);
  305. free(sorts);
  306. fclose(fp);
  307. ui_display_error_box(ErrorCode_WrongFsType, __func__);
  308. return false;
  309. }
  310. // calc the offset.
  311. uint64_t section_offset = MEDIA_REAL(header.section_table[0].media_start_offset);
  312. // read section (pfs0 is always the first and only(?) section for a cnmt.nca)
  313. void *pfs0_data = calloc(1, header.section_header[0].pfs0_sb.pfs0_size);
  314. if (!pfs0_data)
  315. {
  316. write_log("failed to alloc pfs0_data\n");
  317. free(key_gens);
  318. free(cnmt);
  319. free(sorts);
  320. fclose(fp);
  321. ui_display_error_box(ErrorCode_Alloc, __func__);
  322. return false;
  323. }
  324. read_file(pfs0_data, header.section_header[0].pfs0_sb.pfs0_size, (section_offset + header.section_header[0].pfs0_sb.pfs0_offset), fp);
  325. fclose(fp);
  326. // decrypt section
  327. uint8_t ctr[0x10] = {0};
  328. crypto_aes_ctr(pfs0_data, pfs0_data, decrypted_key.area, ctr, header.section_header[0].pfs0_sb.pfs0_size, section_offset + header.section_header[0].pfs0_sb.pfs0_offset);
  329. // using fmemopen because i havent yet re-wrote the pfs0 section yet (works fine anyway).
  330. FILE *pfs0_fp = fmemopen(pfs0_data, header.section_header[0].pfs0_sb.pfs0_size, "r");
  331. if (!pfs0_fp)
  332. {
  333. write_log("failed to open cnmt data stream as a file\n");
  334. free(key_gens);
  335. free(cnmt);
  336. free(sorts);
  337. ui_display_error_box(ErrorCode_OpenFile, __func__);
  338. return false;
  339. }
  340. pfs0_struct_ptr ptr = {0};
  341. if (!pfs0_process(&ptr, 0, pfs0_fp))
  342. {
  343. write_log("failed to process pfs0\n");
  344. free(key_gens);
  345. free(cnmt);
  346. free(sorts);
  347. fclose(pfs0_fp);
  348. free(pfs0_data);
  349. ui_display_error_box(ErrorCode_Pfs0Prase, __func__);
  350. return false;
  351. }
  352. fclose(pfs0_fp);
  353. if (ptr.header.total_files > 1)
  354. {
  355. write_log("WARNING: More than one file found in cnmt pfs0\n");
  356. }
  357. NcmContentInfo cnmt_info = {0};
  358. ncmU64ToContentInfoSize(header.size, &cnmt_info);
  359. cnmt_info.content_id = nca_get_id_from_string(GAMECARD.string_table[i].name);
  360. cnmt_info.content_type = NcmContentType_Meta;
  361. cnmt_info.id_offset = 0;
  362. if (!cnmt_parse(pfs0_data, ptr.raw_data_offset + ptr.file_table[0].data_offset, &cnmt_info, &cnmt[j]))
  363. {
  364. write_log("failed to parse cnmt\n");
  365. free(key_gens);
  366. free(cnmt);
  367. free(sorts);
  368. free(pfs0_data);
  369. ui_display_error_box(ErrorCode_CnmtPrase, __func__);
  370. return false;
  371. }
  372. // add stuff to sorts
  373. uint16_t pos = 0;
  374. if (__id_match_check(sorts, GAMECARD.file_table.game_count, &pos, cnmt[j].key.id, cnmt[j].key.type))
  375. {
  376. __idk_what_to_call_this(sorts, pos, cnmt[j].key.type);
  377. }
  378. else
  379. {
  380. __idk_what_to_call_this(sorts, GAMECARD.file_table.game_count, cnmt[j].key.type);
  381. sorts[GAMECARD.file_table.game_count].id = ncm_get_app_id_from_title_id(cnmt[j].key.id, cnmt[j].key.type);
  382. GAMECARD.file_table.game_count++;
  383. }
  384. pfs0_free_structs(&ptr);
  385. free(pfs0_data);
  386. j++;
  387. }
  388. GAMECARD.entries = calloc(GAMECARD.file_table.game_count, sizeof(GameCardGameEntries_t));
  389. if (!GAMECARD.entries)
  390. {
  391. ui_display_error_box(ErrorCode_Alloc, __func__);
  392. write_log("failed to alloc entries\n");
  393. free(key_gens);
  394. free(sorts);
  395. free(cnmt);
  396. return false;
  397. }
  398. for (uint16_t i = 0; i < GAMECARD.file_table.game_count; i++)
  399. {
  400. if (sorts[i].base_total)
  401. {
  402. GAMECARD.entries[i].base = calloc(sorts[i].base_total, sizeof(GameCardEntry_t));
  403. if (!GAMECARD.entries[i].base)
  404. {
  405. ui_display_error_box(ErrorCode_Alloc, __func__);
  406. write_log("failed to alloc entry base: %u\n", i);
  407. free(key_gens);
  408. free(sorts);
  409. free(cnmt);
  410. return false;
  411. }
  412. }
  413. if (sorts[i].upp_total)
  414. {
  415. GAMECARD.entries[i].upp = calloc(sorts[i].upp_total, sizeof(GameCardEntry_t));
  416. if (!GAMECARD.entries[i].upp)
  417. {
  418. ui_display_error_box(ErrorCode_Alloc, __func__);
  419. write_log("failed to alloc entry upp: %u\n", i);
  420. free(key_gens);
  421. free(sorts);
  422. free(cnmt);
  423. return false;
  424. }
  425. }
  426. if (sorts[i].dlc_total)
  427. {
  428. GAMECARD.entries[i].dlc = calloc(sorts[i].dlc_total, sizeof(GameCardEntry_t));
  429. if (!GAMECARD.entries[i].dlc)
  430. {
  431. ui_display_error_box(ErrorCode_Alloc, __func__);
  432. write_log("failed to alloc entry dlc: %u\n", i);
  433. free(key_gens);
  434. free(sorts);
  435. free(cnmt);
  436. return false;
  437. }
  438. }
  439. }
  440. for (uint16_t i = 0; i < GAMECARD.file_table.cnmt_count; i++)
  441. {
  442. uint16_t pos = 0;
  443. if (!__id_match_check(sorts, GAMECARD.file_table.game_count, &pos, cnmt[i].key.id, cnmt[i].key.type))
  444. {
  445. write_log("\n\nWARNING. __id_match_check failed: %u\n\n", i);
  446. }
  447. __gc_setup_entry(GAMECARD.entries, &cnmt[i], key_gens[i], pos, cnmt[i].key.type);
  448. }
  449. free(key_gens);
  450. free(sorts);
  451. free(cnmt);
  452. return true;
  453. }
  454. bool gc_mount(void)
  455. {
  456. if (!fs_get_gamecard_handle_from_device_operator(&g_dop, &g_gc_handle))
  457. {
  458. write_log("failed to get gc handle\n");
  459. ui_display_error_box(ErrorCode_Mount_Handle, __func__);
  460. return false;
  461. }
  462. if (!fs_mount_gamecard_secure(&g_gc_handle))
  463. {
  464. write_log("failed to mount gc\n");
  465. ui_display_error_box(ErrorCode_Mount_Secure, __func__);
  466. return false;
  467. }
  468. if (!change_dir("%s%s", GAMECARD_MOUNT_SECURE, ":/"))
  469. {
  470. write_log("failed to change path to gc\n");
  471. ui_display_error_box(ErrorCode_Mount_Chdir, __func__);
  472. fs_unmount_device(GAMECARD_MOUNT_SECURE);
  473. return false;
  474. }
  475. return __gc_parse_cnmt();
  476. }
  477. bool gc_unmount(void)
  478. {
  479. fs_unmount_device(GAMECARD_MOUNT_SECURE);
  480. fs_close_gamecard_handle(&g_gc_handle);
  481. if (GAMECARD.entries)
  482. {
  483. for (uint16_t i = 0; i < GAMECARD.file_table.game_count; i++)
  484. {
  485. if (&GAMECARD.entries[i])
  486. {
  487. if (GAMECARD.entries[i].base_count && GAMECARD.entries[i].base)
  488. {
  489. free(GAMECARD.entries[i].base);
  490. GAMECARD.entries[i].base = NULL;
  491. }
  492. if (GAMECARD.entries[i].upp_count && GAMECARD.entries[i].upp)
  493. {
  494. free(GAMECARD.entries[i].upp);
  495. GAMECARD.entries[i].upp = NULL;
  496. }
  497. if (GAMECARD.entries[i].dlc_count && GAMECARD.entries[i].dlc)
  498. {
  499. free(GAMECARD.entries[i].dlc);
  500. GAMECARD.entries[i].dlc = NULL;
  501. }
  502. }
  503. }
  504. free(GAMECARD.entries);
  505. GAMECARD.entries = NULL;
  506. }
  507. if (GAMECARD.string_table)
  508. {
  509. free(GAMECARD.string_table);
  510. GAMECARD.string_table = NULL;
  511. }
  512. memset(&GAMECARD, 0, sizeof(GameCard_t));
  513. g_game_pos = 0;
  514. change_dir("sdmc:/");
  515. return true;
  516. }
  517. /*
  518. * GameCard Getters.
  519. */
  520. uint16_t gc_get_game_count(void)
  521. {
  522. return GAMECARD.file_table.game_count;
  523. }
  524. uint16_t gc_get_base_count(uint16_t game_pos)
  525. {
  526. return GAMECARD.entries[game_pos].base_count;
  527. }
  528. uint16_t gc_get_upp_count(uint16_t game_pos)
  529. {
  530. return GAMECARD.entries[game_pos].upp_count;
  531. }
  532. uint16_t gc_get_dlc_count(uint16_t game_pos)
  533. {
  534. return GAMECARD.entries[game_pos].dlc_count;
  535. }
  536. uint16_t gc_get_current_base_count(void)
  537. {
  538. return GAMECARD.entries[g_game_pos].base_count;
  539. }
  540. uint16_t gc_get_current_upp_count(void)
  541. {
  542. return GAMECARD.entries[g_game_pos].upp_count;
  543. }
  544. uint16_t gc_get_current_dlc_count(void)
  545. {
  546. return GAMECARD.entries[g_game_pos].dlc_count;
  547. }
  548. /*
  549. * Change GameInfo.
  550. */
  551. const GameCardEntry_t *__gc_get_gamecard_entry(uint16_t game_pos, uint16_t entry_pos)
  552. {
  553. // ensure that we have a game in pos and a game-entry.
  554. if (game_pos >= GAMECARD.file_table.game_count || entry_pos >= GAMECARD.entries[game_pos].total_count)
  555. {
  556. write_log("missing params in %s\n", __func__);
  557. return NULL;
  558. }
  559. if (entry_pos < GAMECARD.entries[game_pos].base_count)
  560. {
  561. return &GAMECARD.entries[game_pos].base[entry_pos];
  562. }
  563. else if (entry_pos < GAMECARD.entries[game_pos].base_count + GAMECARD.entries[game_pos].upp_count)
  564. {
  565. return &GAMECARD.entries[game_pos].upp[entry_pos - GAMECARD.entries[game_pos].base_count];
  566. }
  567. else
  568. {
  569. return &GAMECARD.entries[game_pos].dlc[entry_pos - (GAMECARD.entries[game_pos].base_count + GAMECARD.entries[game_pos].upp_count)];
  570. }
  571. }
  572. bool gc_setup_game_info(GameInfo_t *out_info, uint16_t game_pos)
  573. {
  574. if (!out_info || game_pos >= GAMECARD.file_table.game_count)
  575. {
  576. write_log("missing params in %s\n", __func__);
  577. return false;
  578. }
  579. // get the first entry.
  580. // this ensures that even if the user mounts an xci with only a update / dlc entry, the info will still be shown.
  581. const GameCardEntry_t *tmp_entry = __gc_get_gamecard_entry(game_pos, 0);
  582. if (!tmp_entry)
  583. {
  584. return false;
  585. }
  586. // reset the info.
  587. memset(out_info, 0, sizeof(GameInfo_t));
  588. // calculate the size in GiB. If size is less than 10MiB, show size in MiB.
  589. char size_type_text[] = "GiB";
  590. float game_size = (float)GAMECARD.entries[game_pos].total_size / 0x40000000;
  591. if (game_size < 0.01)
  592. {
  593. game_size = (float)GAMECARD.entries[game_pos].total_size / 0x100000;
  594. strcpy(size_type_text, "MiB");
  595. }
  596. // store the values.
  597. out_info->app_id = tmp_entry->cnmt.key.id;
  598. out_info->key_gen = tmp_entry->key_gen;
  599. out_info->base_count = GAMECARD.entries[game_pos].base_count;
  600. out_info->upp_count = GAMECARD.entries[game_pos].upp_count;
  601. out_info->dlc_count = GAMECARD.entries[game_pos].dlc_count;
  602. out_info->total_count = GAMECARD.entries[game_pos].total_count;
  603. // create the textures.
  604. out_info->text_app_id = create_text(&FONT_TEXT[QFontSize_18], 50, 495, Colour_Nintendo_White, "App-ID: 0%lX", tmp_entry->cnmt.key.id);
  605. out_info->text_key_gen = create_text(&FONT_TEXT[QFontSize_18], 50, 535, Colour_Nintendo_White, "Key-Gen: %u (%s)", out_info->key_gen, nca_return_key_gen_string(out_info->key_gen));
  606. out_info->text_size = create_text(&FONT_TEXT[QFontSize_18], 50, 575, Colour_Nintendo_White, "Size: %.2f %s", game_size, size_type_text);
  607. out_info->text_entry_contents = create_text(&FONT_TEXT[QFontSize_18], 50, 615, Colour_Nintendo_White, "Base: %u Upp: %u DLC: %u", GAMECARD.entries[game_pos].base_count, GAMECARD.entries[game_pos].upp_count, GAMECARD.entries[game_pos].dlc_count);
  608. enable_text_clip(out_info->text_app_id, 0, 325);
  609. enable_text_clip(out_info->text_key_gen, 0, 325);
  610. enable_text_clip(out_info->text_size, 0, 325);
  611. enable_text_clip(out_info->text_entry_contents, 0, 325);
  612. // TODO: manually parse the control.nacp if this fails.
  613. NsApplicationControlData control_data = {0};
  614. size_t control_data_size = ns_get_app_control_data(&control_data, out_info->app_id);
  615. if (control_data_size)
  616. {
  617. size_t icon_size = control_data_size - sizeof(NacpStruct);
  618. out_info->icon = create_image_from_mem(&control_data.icon, icon_size, 90, 130, 0, 0);
  619. out_info->title = create_text(&FONT_TEXT[QFontSize_18], 50, 415, Colour_Nintendo_White, control_data.nacp.lang[0].name);
  620. out_info->author = create_text(&FONT_TEXT[QFontSize_18], 50, 455, Colour_Nintendo_White, control_data.nacp.lang[0].author);
  621. enable_text_clip(out_info->title, 0, 325);
  622. enable_text_clip(out_info->author, 0, 325);
  623. }
  624. return true;
  625. }
  626. bool gc_setup_detailed_game_info(GameInfoDetailed_t *info_out, uint16_t entry)
  627. {
  628. if (!info_out || entry >= GAMECARD.entries[g_game_pos].total_count)
  629. {
  630. write_log("missing params in %s\n", __func__);
  631. return false;
  632. }
  633. // get the entry.
  634. const GameCardEntry_t *tmp_entry = __gc_get_gamecard_entry(g_game_pos, entry);
  635. if (!tmp_entry)
  636. {
  637. return false;
  638. }
  639. // setup the vars.
  640. info_out->type = tmp_entry->cnmt.key.type;
  641. info_out->id = tmp_entry->cnmt.key.id;
  642. info_out->keygen = tmp_entry->key_gen;
  643. info_out->version = tmp_entry->cnmt.key.version;
  644. info_out->content_count = tmp_entry->cnmt.header.content_count;
  645. info_out->content_meta_count = tmp_entry->cnmt.header.content_meta_count;
  646. // create the textures.
  647. info_out->text_type = create_text(&FONT_TEXT[QFontSize_18], 230, 360, Colour_Nintendo_White, "Type: %s", ncm_get_meta_type_string(info_out->type));
  648. info_out->text_id = create_text(&FONT_TEXT[QFontSize_18], 230, 390, Colour_Nintendo_White, "ID: %lX", info_out->id);
  649. info_out->text_keygen = create_text(&FONT_TEXT[QFontSize_18], 230, 420, Colour_Nintendo_White, "Key-Gen: %u", info_out->keygen);
  650. info_out->text_version = create_text(&FONT_TEXT[QFontSize_18], 230, 450, Colour_Nintendo_White, "Version: %u (%u.%u.%u.%u)", info_out->version, (info_out->version >> 26) & 0x3F, (info_out->version >> 20) & 0x2F, (info_out->version >> 16) & 0xF, (uint16_t)info_out->version);
  651. info_out->text_content_count = create_text(&FONT_TEXT[QFontSize_18], 230, 480, Colour_Nintendo_White, "Content Count: %u", info_out->content_count);
  652. info_out->text_content_meta_count = create_text(&FONT_TEXT[QFontSize_18], 230, 510, Colour_Nintendo_White, "Content Meta Count: %u", info_out->content_meta_count);
  653. info_out->entry = calloc(info_out->content_count, sizeof(GameInfoEntry_t));
  654. if (!info_out->entry)
  655. {
  656. ui_display_error_box(ErrorCode_Alloc, __func__);
  657. write_log("failed to alloc entry %s\n", __func__);
  658. return false;
  659. }
  660. // setup the textures.
  661. for (uint16_t i = 0, y = 195; i < info_out->content_count; i++, y += 47)
  662. {
  663. char name_buf[0x30] = {0};
  664. info_out->entry[i].name = create_text(&FONT_TEXT[QFontSize_18], 575, y, Colour_Nintendo_BrightSilver, "%s%s", nca_get_string_from_id(&tmp_entry->cnmt.content_infos[i].content_id, name_buf), tmp_entry->cnmt.content_infos[i].content_type == NcmContentType_Meta ? ".cnmt.nca" : ".nca");
  665. info_out->entry[i].type = create_text(&FONT_TEXT[QFontSize_18], 575, y += 27, Colour_Nintendo_BrightSilver, "Type: %s", ncm_get_content_type_string(tmp_entry->cnmt.content_infos[i].content_type));
  666. info_out->entry[i].size = create_text(&FONT_TEXT[QFontSize_18], info_out->entry[i].type->rect.x + info_out->entry[i].type->rect.w + 30, y, Colour_Nintendo_BrightSilver, "Size: %lu", ncm_calculate_content_info_size(&tmp_entry->cnmt.content_infos[i]));
  667. }
  668. return true;
  669. }
  670. bool gc_next_game(GameInfo_t *info_out)
  671. {
  672. if (!info_out)
  673. {
  674. write_log("missing params in %s\n", __func__);
  675. return false;
  676. }
  677. if (GAMECARD.file_table.game_count == 0)
  678. {
  679. return true;
  680. }
  681. g_game_pos = g_game_pos == GAMECARD.file_table.game_count - 1 ? 0 : g_game_pos + 1;
  682. return gc_setup_game_info(info_out, g_game_pos);
  683. }
  684. bool gc_prev_game(GameInfo_t *info_out)
  685. {
  686. if (!info_out)
  687. {
  688. write_log("missing params in %s\n", __func__);
  689. return false;
  690. }
  691. if (GAMECARD.file_table.game_count == 0)
  692. {
  693. return true;
  694. }
  695. g_game_pos = g_game_pos == 0 ? GAMECARD.file_table.game_count - 1 : g_game_pos - 1;
  696. return gc_setup_game_info(info_out, g_game_pos);
  697. }
  698. bool gc_change_game(GameInfo_t *info_out, uint16_t game_pos)
  699. {
  700. if (!info_out)
  701. {
  702. write_log("missing params in %s\n", __func__);
  703. return false;
  704. }
  705. if (game_pos >= GAMECARD.file_table.game_count)
  706. {
  707. write_log("cannot swap game, pos too high %s\n", __func__);
  708. return true;
  709. }
  710. g_game_pos = game_pos;
  711. return gc_setup_game_info(info_out, g_game_pos);
  712. }
  713. /*
  714. * GameCard Install.
  715. */
  716. bool __gc_do_ticket_magic(const char *ticket_path, uint64_t id) //nice function name bro.
  717. {
  718. if (!ticket_path)
  719. {
  720. write_log("missing params in %s\n", __func__);
  721. return false;
  722. }
  723. uint64_t gen = strtoul(ticket_path + 0x10, NULL, 0x10);
  724. FILE *fp = fopen(ticket_path, "rb");
  725. if (!fp)
  726. {
  727. ui_display_error_box(ErrorCode_OpenFile, __func__);
  728. write_log("failed to open file %s\n", __func__);
  729. return false;
  730. }
  731. uint8_t key[0x10] = {0};
  732. // double fseek because a single fseek doesn't work.
  733. // i've been told that i have to align reads, but i like the fact that double fseek fixes a bug ;)
  734. fseek(fp, 0x180, SEEK_SET);
  735. fseek(fp, 0x180, SEEK_SET);
  736. fread(key, 0x10, 1, fp);
  737. fclose(fp);
  738. // decrypt the key.
  739. crypto_aes(key, key, crypto_get_titlekek_from_keys(gen), EncryptMode_Decrypt);
  740. // set the key in keyslot.
  741. // eventually i will re-write this so that i just pass a struct into nca_install.
  742. // this will have the ticket info in that struct.
  743. nca_set_keyslot(id, key);
  744. return true;
  745. }
  746. void __gc_matching_ticket(const GameCard_t *gamecard, const GameCardEntry_t *entry)
  747. {
  748. // check if we have any tickets.
  749. if (!gamecard->file_table.tik_count || !gamecard->file_table.cert_count)
  750. {
  751. return;
  752. }
  753. // check if we already have the key.
  754. if (setting_get_install_lower_key_gen() && entry->cnmt.key.id == nca_get_keyslot_id())
  755. {
  756. return;
  757. }
  758. bool found_tik = false;
  759. bool found_cert = false;
  760. uint16_t tik_pos = 0;
  761. uint16_t cert_pos = 0;
  762. char str_id[0x11] = {0};
  763. snprintf(str_id, 0x11, "%016lx", (entry->cnmt.key.id));
  764. for (uint16_t i = 0; i < gamecard->file_table.file_count; i++)
  765. {
  766. char *ext = strchr(gamecard->string_table[i].name, '.');
  767. if (!ext)
  768. {
  769. continue;
  770. }
  771. if (strcmp(ext, ".tik") == 0 && strstr(gamecard->string_table[i].name, str_id))
  772. {
  773. write_log("found ticket\n");
  774. if (setting_get_install_lower_key_gen())
  775. {
  776. __gc_do_ticket_magic(gamecard->string_table[i].name, entry->cnmt.key.id);
  777. return;
  778. }
  779. tik_pos = i;
  780. found_tik = true;
  781. }
  782. else if (strcmp(ext, ".cert") == 0 && strstr(gamecard->string_table[i].name, str_id))
  783. {
  784. write_log("found cert\n");
  785. cert_pos = i;
  786. found_cert = true;
  787. }
  788. }
  789. if (found_tik && found_cert)
  790. {
  791. write_log("installing ticket %s %s\n", gamecard->string_table[tik_pos].name, gamecard->string_table[cert_pos].name);
  792. size_t tik_size = 0;
  793. size_t cert_size = 0;
  794. void *tik_buf = load_file_into_mem(gamecard->string_table[tik_pos].name, &tik_size);
  795. void *cert_buf = load_file_into_mem(gamecard->string_table[cert_pos].name, &cert_size);
  796. if (!tik_buf) write_log("no tik buf\n");
  797. if (!tik_size) write_log("no tik size\n");
  798. if (!cert_buf) write_log("no cert buf\n");
  799. if (!cert_size) write_log("no tik size\n");
  800. es_import_tik_and_cert(tik_buf, tik_size, cert_buf, cert_size);
  801. free(tik_buf);
  802. free(cert_buf);
  803. }
  804. return;
  805. }
  806. bool __gc_install(const GameCardEntry_t *entry, NcmStorageId storage_id)
  807. {
  808. if (!entry)
  809. {
  810. write_log("missing params in %s\n", __func__);
  811. return false;
  812. }
  813. // add a settings option to make this optional.
  814. cnmt_set_extended_header(entry->cnmt.extended_header, entry->cnmt.key.type);
  815. // set the db and push record.
  816. if (!cnmt_set_db(&entry->cnmt.key, &entry->cnmt.header, entry->cnmt.extended_header, entry->cnmt.content_infos, storage_id))
  817. {
  818. ui_display_error_box(ErrorCode_NcmDb, __func__);
  819. return false;
  820. }
  821. if (!cnmt_push_record(&entry->cnmt.key, storage_id))
  822. {
  823. ui_display_error_box(ErrorCode_AppRecord, __func__);
  824. return false;
  825. }
  826. // clear unused data after pushing record.
  827. // ie, installing v2 over v1. v1 will still exist in ncm, this will delete it.
  828. // can be done manually, and will be done so soon.
  829. // this will also fix mistakes from bad installers that do not clean up left over ncas.
  830. nsDeleteRedundantApplicationEntity();
  831. for (uint16_t i = 0; i < entry->cnmt.header.content_count; i++)
  832. {
  833. // TODO: add check to make sure that the nca is in the filetable.
  834. char nca_name_buffer[0x301] = {0};
  835. snprintf(nca_name_buffer, 0x301, "%s%s", nca_get_string_from_id(&entry->cnmt.content_infos[i].content_id, nca_name_buffer), entry->cnmt.content_infos[i].content_type == NcmContentType_Meta ? ".cnmt.nca" : ".nca");
  836. FILE *fp = fopen(nca_name_buffer, "rb");
  837. if (!fp)
  838. {
  839. ui_display_error_box(ErrorCode_OpenFile, __func__);
  840. write_log("failed to open: %s\n", nca_name_buffer);
  841. return false;
  842. }
  843. if (!nca_start_install(nca_name_buffer, &entry->cnmt.content_infos[i].content_id, 0, storage_id, fp))
  844. {
  845. write_log("failed to install: %s\n", nca_name_buffer);
  846. fclose(fp);
  847. return false;
  848. }
  849. fclose(fp);
  850. }
  851. return true;
  852. }
  853. bool gc_install_ex(uint16_t game_pos, NcmStorageId storage_id)
  854. {
  855. if (storage_id != NcmStorageId_BuiltInUser && storage_id != NcmStorageId_SdCard)
  856. {
  857. write_log("got wrong storage id %u %s\n", storage_id, __func__);
  858. return false;
  859. }
  860. if (setting_get_install_lower_key_gen() == SettingFlag_On && !crypto_has_key_gen_from_keys(NcaKeyAreaEncryptionKeyIndex_Application, 0))
  861. {
  862. ui_display_error_box(ErrorCode_NoKeyFile, "");
  863. return false;
  864. }
  865. /*
  866. * Get the override install location from settings.
  867. * TODO: actually implement this into the settings menu.
  868. */
  869. SettingsInstallLocation base_location = setting_get_install_base_location();
  870. if (base_location == SettingsInstallLocation_Default) base_location = storage_id;
  871. SettingsInstallLocation upp_location = setting_get_install_upp_location();
  872. if (upp_location == SettingsInstallLocation_Default) upp_location = storage_id;
  873. SettingsInstallLocation dlc_location = setting_get_install_dlc_location();
  874. if (dlc_location == SettingsInstallLocation_Default) dlc_location = storage_id;
  875. SettingFlag base_flag = setting_get_install_base();
  876. SettingFlag upp_flag = setting_get_install_upp();
  877. SettingFlag dlc_flag = setting_get_install_dlc();
  878. // we will allow for split installs soon. so we need to check both storage sizes.
  879. size_t total_install_size_nand = 0;
  880. size_t total_install_size_sd = 0;
  881. // calculate the size of install for each storage.
  882. if (base_flag && GAMECARD.entries[game_pos].base_count)
  883. {
  884. if (base_location == SettingsInstallLocation_User)
  885. {
  886. total_install_size_nand += GAMECARD.entries[game_pos].base_size;
  887. }
  888. else
  889. {
  890. total_install_size_sd += GAMECARD.entries[game_pos].base_size;
  891. }
  892. }
  893. if (upp_flag && GAMECARD.entries[game_pos].upp_count)
  894. {
  895. if (base_location == SettingsInstallLocation_User)
  896. {
  897. total_install_size_nand += GAMECARD.entries[game_pos].upp_size;
  898. }
  899. else
  900. {
  901. total_install_size_sd += GAMECARD.entries[game_pos].upp_size;
  902. }
  903. }
  904. if (dlc_flag && GAMECARD.entries[game_pos].dlc_count)
  905. {
  906. if (base_location == SettingsInstallLocation_User)
  907. {
  908. total_install_size_nand += GAMECARD.entries[game_pos].dlc_size;
  909. }
  910. else
  911. {
  912. total_install_size_sd += GAMECARD.entries[game_pos].dlc_size;
  913. }
  914. }
  915. if (total_install_size_nand)
  916. {
  917. if (ns_get_storage_free_space(storage_id) <= total_install_size_nand)
  918. {
  919. write_log("not enough free nand space.\n");
  920. ui_display_error_box(ErrorCode_NoSpace, __func__);
  921. return false;
  922. }
  923. }
  924. if (total_install_size_sd)
  925. {
  926. if (ns_get_storage_free_space(storage_id) <= total_install_size_sd)
  927. {
  928. write_log("not enough free sd space.\n");
  929. ui_display_error_box(ErrorCode_NoSpace, __func__);
  930. return false;
  931. }
  932. }
  933. /*
  934. * Actually installing stuff now.
  935. * Loop though array.
  936. */
  937. // base.
  938. if (base_flag == SettingFlag_On)
  939. {
  940. for (uint16_t i = 0; i < GAMECARD.entries[game_pos].base_count; i++)
  941. {
  942. if (!ncm_is_key_newer(&GAMECARD.entries[game_pos].base[i].cnmt.key) && setting_get_overwrite_newer_version() == SettingFlag_Off)
  943. {
  944. continue;
  945. }
  946. __gc_matching_ticket(&GAMECARD, &GAMECARD.entries[game_pos].base[i]);
  947. if (!__gc_install(&GAMECARD.entries[game_pos].base[i], base_location))
  948. {
  949. return false;
  950. }
  951. }
  952. }
  953. // upp.
  954. if (upp_flag == SettingFlag_On)
  955. {
  956. for (uint16_t i = 0; i < GAMECARD.entries[game_pos].upp_count; i++)
  957. {
  958. if (!ncm_is_key_newer(&GAMECARD.entries[game_pos].upp[i].cnmt.key) && setting_get_overwrite_newer_version() == SettingFlag_Off)
  959. {
  960. continue;
  961. }
  962. __gc_matching_ticket(&GAMECARD, &GAMECARD.entries[game_pos].upp[i]);
  963. if (!__gc_install(&GAMECARD.entries[game_pos].upp[i], upp_location))
  964. {
  965. return false;
  966. }
  967. }
  968. }
  969. // dlc.
  970. if (dlc_flag == SettingFlag_On)
  971. {
  972. for (uint16_t i = 0; i < GAMECARD.entries[game_pos].dlc_count; i++)
  973. {
  974. if (!ncm_is_key_newer(&GAMECARD.entries[game_pos].dlc[i].cnmt.key) && setting_get_overwrite_newer_version() == SettingFlag_Off)
  975. {
  976. continue;
  977. }
  978. __gc_matching_ticket(&GAMECARD, &GAMECARD.entries[game_pos].dlc[i]);
  979. if (!__gc_install(&GAMECARD.entries[game_pos].dlc[i], dlc_location))
  980. {
  981. return false;
  982. }
  983. }
  984. }
  985. return true;
  986. }
  987. bool gc_install(NcmStorageId storage_id)
  988. {
  989. return gc_install_ex(g_game_pos, storage_id);
  990. }