-
Notifications
You must be signed in to change notification settings - Fork 11
/
nca.c
710 lines (608 loc) · 29.2 KB
/
nca.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "nca.h"
#include "sha.h"
#include "filepath.h"
#include "romfs.h"
void nca_create_control(hbp_settings_t *settings, cnmt_ctx_t *cnmt_ctx)
{
printf("----> Creating Control NCA:\n");
printf("===> Creating NCA header\n");
nca_header_t nca_header;
memset(&nca_header, 0, sizeof(nca_header));
filepath_t control_nca_path;
filepath_init(&control_nca_path);
filepath_copy(&control_nca_path, &settings->out_dir);
filepath_append(&control_nca_path, "control.nca");
FILE *control_nca_file;
control_nca_file = os_fopen(control_nca_path.os_path, OS_MODE_WRITE_EDIT);
// Write placeholder for NCA header
printf("Writing NCA header placeholder to %s\n", control_nca_path.char_path);
if (control_nca_file != NULL)
fwrite(&nca_header, 1, sizeof(nca_header), control_nca_file);
else
{
fprintf(stderr, "Failed to create %s!\n", control_nca_path.char_path);
exit(EXIT_FAILURE);
}
printf("\n---> Creating Section 0:");
//Build RomFS
filepath_t romfs_control;
filepath_init(&romfs_control);
filepath_copy(&romfs_control, &settings->temp_dir);
filepath_append(&romfs_control, "control_sec0_romfs");
printf("\n===> Building RomFS\n");
romfs_build(&settings->control_romfs_dir, &romfs_control);
// Set IVFC levels temp filepaths
filepath_t ivfc_lvls_path[6];
for (int a = 0; a < 6; a++)
{
filepath_init(&ivfc_lvls_path[a]);
filepath_copy(&ivfc_lvls_path[a], &settings->temp_dir);
filepath_append(&ivfc_lvls_path[a], "control_sec0_ivfc_lvl%i", a + 1);
}
// Create IVFC levels
printf("\n===> Creating IVFC levels\n");
printf("Writing %s\n", ivfc_lvls_path[5].char_path);
ivfc_create_level6(&ivfc_lvls_path[5], &romfs_control, &nca_header.fs_headers[0].romfs_superblock.ivfc_header.level_headers[5].hash_data_size);
nca_header.fs_headers[0].romfs_superblock.ivfc_header.level_headers[5].block_size = 0x0E; // 0x4000
for (int b = 4; b >= 0; b--)
{
printf("Writing %s\n", ivfc_lvls_path[b].char_path);
ivfc_create_level(&ivfc_lvls_path[b], &ivfc_lvls_path[b + 1], &nca_header.fs_headers[0].romfs_superblock.ivfc_header.level_headers[b].hash_data_size);
nca_header.fs_headers[0].romfs_superblock.ivfc_header.level_headers[b].block_size = 0x0E; // 0x4000
}
// Set IVFC levels logical offset
nca_header.fs_headers[0].romfs_superblock.ivfc_header.level_headers[0].logical_offset = 0;
for (int i = 1; i <= 5; i++)
nca_header.fs_headers[0].romfs_superblock.ivfc_header.level_headers[i].logical_offset = nca_header.fs_headers[0].romfs_superblock.ivfc_header.level_headers[i - 1].logical_offset + nca_header.fs_headers[0].romfs_superblock.ivfc_header.level_headers[i - 1].hash_data_size;
// Write IVFC levels
printf("\n===> Writing IVFC levels\n");
for (int c = 0; c < 6; c++)
{
printf("Writing %s to %s\n", ivfc_lvls_path[c].char_path, control_nca_path.char_path);
nca_write_file(control_nca_file, &ivfc_lvls_path[c]);
}
// Write Padding if required
nca_write_padding(control_nca_file);
// Common values
nca_header.magic = MAGIC_NCA3;
nca_header.content_type = 0x2; // Control
nca_header.sdk_minor = 12;
nca_header.sdk_micro = 17;
nca_header.title_id = cnmt_ctx->cnmt_header.title_id;
nca_header.section_entries[0].media_start_offset = 0x6; // 0xC00 / 0x200
nca_header.section_entries[0].media_end_offset = (uint32_t)(ftello64(control_nca_file) / 0x200); // Section end offset / 200
nca_header.section_entries[0]._0x8[0] = 0x1; // Always 1
nca_header.fs_headers[0].fs_type = FS_TYPE_ROMFS;
nca_header.fs_headers[0].crypt_type = 0x3; // Regular crypto
nca_header.fs_headers[0]._0x0 = 0x2; // Always 2
nca_header.fs_headers[0].romfs_superblock.ivfc_header.magic = MAGIC_IVFC;
nca_header.fs_headers[0].romfs_superblock.ivfc_header.id = 0x20000; //Always 0x20000
nca_header.fs_headers[0].romfs_superblock.ivfc_header.master_hash_size = 0x20;
nca_header.fs_headers[0].romfs_superblock.ivfc_header.num_levels = 0x7;
// Calculate master hash and section hash
printf("\n===> Calculating Hashes:\n");
printf("Calculating Master hash\n");
ivfc_calculate_master_hash(&ivfc_lvls_path[0], nca_header.fs_headers[0].romfs_superblock.ivfc_header.master_hash);
printf("Calculating Section hash\n");
nca_calculate_section_hash(&nca_header.fs_headers[0], nca_header.section_hashes[0]);
printf("\n---> Finalizing:\n");
// Set encrypted key area key 2
memset(nca_header.encrypted_keys[2], 4, 0x10);
// Encrypt section 0
printf("===> Encrypting NCA\n");
printf("Encrypting section 0\n");
nca_encrypt_section(control_nca_file, &nca_header, 0);
// Encrypt header
printf("Getting NCA file size\n");
fseeko64(control_nca_file, 0, SEEK_END);
nca_header.nca_size = (uint64_t)ftello64(control_nca_file);
printf("Encrypting key area\n");
nca_encrypt_key_area(&nca_header, settings);
printf("Encrypting header\n");
nca_encrypt_header(&nca_header, settings);
// Write MCA header
printf("\n===> Writing NCA header\n");
printf("Writing NCA header to %s\n", control_nca_path.char_path);
fseeko64(control_nca_file, 0, SEEK_SET);
fwrite(&nca_header, 1, sizeof(nca_header), control_nca_file);
// Calculate hash and nca size
printf("\n===> Post creation process\n");
printf("Calculating NCA hash\n");
nca_calculate_hash(control_nca_file, cnmt_ctx, 1);
fclose(control_nca_file);
// Rename control.nca to ncaid.nca
filepath_t control_nca_final_path;
filepath_init(&control_nca_final_path);
filepath_copy(&control_nca_final_path, &settings->out_dir);
char control_nca_name[37];
hexBinaryString(cnmt_ctx->cnmt_content_records[1].ncaid, 16, control_nca_name, 33);
strcat(control_nca_name, ".nca");
control_nca_name[36] = '\0';
printf("Renaming control.nca to %s\n", control_nca_name);
filepath_append(&control_nca_final_path, "%s", control_nca_name);
os_rename(control_nca_path.os_path, control_nca_final_path.os_path);
printf("\n----> Created Control NCA: %s\n", control_nca_final_path.char_path);
}
void nca_create_program(hbp_settings_t *settings, cnmt_ctx_t *cnmt_ctx)
{
printf("----> Creating Program NCA:\n");
printf("===> Creating NCA header\n");
nca_header_t nca_header;
memset(&nca_header, 0, sizeof(nca_header));
filepath_t program_nca_path;
filepath_init(&program_nca_path);
filepath_copy(&program_nca_path, &settings->out_dir);
filepath_append(&program_nca_path, "program.nca");
FILE *program_nca_file;
program_nca_file = os_fopen(program_nca_path.os_path, OS_MODE_WRITE_EDIT);
// Write placeholder for NCA header
printf("Writing NCA header placeholder to %s\n", program_nca_path.char_path);
if (program_nca_file != NULL)
fwrite(&nca_header, 1, sizeof(nca_header), program_nca_file);
else
{
fprintf(stderr, "Failed to create %s!\n", program_nca_path.char_path);
exit(EXIT_FAILURE);
}
printf("\n---> Creating Section 0:");
//Build ExeFS
filepath_t program_exefs;
filepath_init(&program_exefs);
filepath_copy(&program_exefs, &settings->temp_dir);
filepath_append(&program_exefs, "program_sec0_exefs");
filepath_t program_exefs_hash_table;
filepath_init(&program_exefs_hash_table);
filepath_copy(&program_exefs_hash_table, &settings->temp_dir);
filepath_append(&program_exefs_hash_table, "program_sec0_exefs_hashtable");
printf("\n===> Building ExeFS\n");
pfs0_build(&settings->exefs_dir, &program_exefs, &nca_header.fs_headers[0].pfs0_superblock.pfs0_size);
printf("Calculating hash table\n");
pfs0_create_hashtable(&program_exefs, &program_exefs_hash_table, &nca_header.fs_headers[0].pfs0_superblock.hash_table_size, &nca_header.fs_headers[0].pfs0_superblock.pfs0_offset);
// Write ExeFS
printf("\n===> Writing ExeFS\n");
printf("Writing PFS0 hash table\n");
nca_write_file(program_nca_file, &program_exefs_hash_table);
printf("Writing PFS0\n");
nca_write_file(program_nca_file, &program_exefs);
// Write Padding if required
nca_write_padding(program_nca_file);
// Common values
nca_header.magic = MAGIC_NCA3;
nca_header.content_type = 0x0; // Program
nca_header.sdk_minor = 12;
nca_header.sdk_micro = 17;
nca_header.title_id = cnmt_ctx->cnmt_header.title_id;
nca_header.section_entries[0].media_start_offset = 0x6; // 0xC00 / 0x200
nca_header.section_entries[0].media_end_offset = (uint32_t)(ftello64(program_nca_file) / 0x200); // Section end offset / 200
nca_header.section_entries[0]._0x8[0] = 0x1; // Always 1
nca_header.fs_headers[0].fs_type = FS_TYPE_PFS0;
nca_header.fs_headers[0].partition_type = 0x1;
nca_header.fs_headers[0]._0x0 = 0x2; // Always 2
nca_header.fs_headers[0].crypt_type = 0x3; // Regular crypto
nca_header.fs_headers[0].pfs0_superblock.always_2 = 0x2;
nca_header.fs_headers[0].pfs0_superblock.block_size = PFS0_HASH_BLOCK_SIZE;
// Calculate master hash and section hash
printf("\n===> Calculating Hashes:\n");
printf("Calculating Master hash\n");
pfs0_calculate_master_hash(&program_exefs_hash_table, nca_header.fs_headers[0].pfs0_superblock.hash_table_size, nca_header.fs_headers[0].pfs0_superblock.master_hash);
printf("Calculating Section hash\n");
nca_calculate_section_hash(&nca_header.fs_headers[0], nca_header.section_hashes[0]);
if (settings->noromfs == 0)
{
printf("\n---> Creating Section 1:");
//Build RomFS
filepath_t romfs_program;
filepath_init(&romfs_program);
filepath_copy(&romfs_program, &settings->temp_dir);
filepath_append(&romfs_program, "program_sec1_romfs");
printf("\n===> Building RomFS\n");
romfs_build(&settings->romfs_dir, &romfs_program);
// Set IVFC levels temp filepaths
filepath_t ivfc_lvls_path[6];
for (int a = 0; a < 6; a++)
{
filepath_init(&ivfc_lvls_path[a]);
filepath_copy(&ivfc_lvls_path[a], &settings->temp_dir);
filepath_append(&ivfc_lvls_path[a], "program_sec1_ivfc_lvl%i", a + 1);
}
// Create IVFC levels
printf("\n===> Creating IVFC levels\n");
printf("Writing %s\n", ivfc_lvls_path[5].char_path);
ivfc_create_level6(&ivfc_lvls_path[5], &romfs_program, &nca_header.fs_headers[1].romfs_superblock.ivfc_header.level_headers[5].hash_data_size);
nca_header.fs_headers[1].romfs_superblock.ivfc_header.level_headers[5].block_size = 0x0E; // 0x4000
for (int b = 4; b >= 0; b--)
{
printf("Writing %s\n", ivfc_lvls_path[b].char_path);
ivfc_create_level(&ivfc_lvls_path[b], &ivfc_lvls_path[b + 1], &nca_header.fs_headers[1].romfs_superblock.ivfc_header.level_headers[b].hash_data_size);
nca_header.fs_headers[1].romfs_superblock.ivfc_header.level_headers[b].block_size = 0x0E; // 0x4000
}
// Set IVFC levels logical offset
nca_header.fs_headers[1].romfs_superblock.ivfc_header.level_headers[0].logical_offset = 0;
for (int i = 1; i <= 5; i++)
nca_header.fs_headers[1].romfs_superblock.ivfc_header.level_headers[i].logical_offset = nca_header.fs_headers[1].romfs_superblock.ivfc_header.level_headers[i - 1].logical_offset + nca_header.fs_headers[1].romfs_superblock.ivfc_header.level_headers[i - 1].hash_data_size;
// Write IVFC levels
printf("\n===> Writing IVFC levels\n");
for (int c = 0; c < 6; c++)
{
printf("Writing %s to %s\n", ivfc_lvls_path[c].char_path, program_nca_path.char_path);
nca_write_file(program_nca_file, &ivfc_lvls_path[c]);
}
// Write Padding if required
nca_write_padding(program_nca_file);
// Set header values
nca_header.section_entries[1].media_start_offset = nca_header.section_entries[0].media_end_offset;
nca_header.section_entries[1].media_end_offset = (uint32_t)(ftello64(program_nca_file) / 0x200);
nca_header.section_entries[1]._0x8[0] = 0x1; // Always 1
nca_header.fs_headers[1].fs_type = FS_TYPE_ROMFS;
nca_header.fs_headers[1].crypt_type = 0x3; // Regular crypto
nca_header.fs_headers[1]._0x0 = 0x2; // Always 2
nca_header.fs_headers[1].romfs_superblock.ivfc_header.magic = MAGIC_IVFC;
nca_header.fs_headers[1].romfs_superblock.ivfc_header.id = 0x20000; //Always 0x20000
nca_header.fs_headers[1].romfs_superblock.ivfc_header.master_hash_size = 0x20;
nca_header.fs_headers[1].romfs_superblock.ivfc_header.num_levels = 0x7;
// Calculate master hash and section hash
printf("\n===> Calculating Hashes:\n");
printf("Calculating Master hash\n");
ivfc_calculate_master_hash(&ivfc_lvls_path[0], nca_header.fs_headers[1].romfs_superblock.ivfc_header.master_hash);
printf("Calculating Section hash\n");
nca_calculate_section_hash(&nca_header.fs_headers[1], nca_header.section_hashes[1]);
}
if (settings->nologo == 0)
{
printf("\n---> Creating Section 2:");
//Build logo
filepath_t program_logo;
filepath_init(&program_logo);
filepath_copy(&program_logo, &settings->temp_dir);
filepath_append(&program_logo, "program_sec2_logo");
filepath_t program_logo_hash_table;
filepath_init(&program_logo_hash_table);
filepath_copy(&program_logo_hash_table, &settings->temp_dir);
filepath_append(&program_logo_hash_table, "program_sec2_logo_hashtable");
printf("\n===> Building PFS0\n");
pfs0_build(&settings->logo_dir, &program_logo, &nca_header.fs_headers[2].pfs0_superblock.pfs0_size);
printf("Calculating hash table\n");
pfs0_create_hashtable(&program_logo, &program_logo_hash_table, &nca_header.fs_headers[2].pfs0_superblock.hash_table_size, &nca_header.fs_headers[2].pfs0_superblock.pfs0_offset);
// Write IVFC levels
printf("\n===> Writing IVFC levels\n");
printf("Writing PFS0 hash table\n");
nca_write_file(program_nca_file, &program_logo_hash_table);
printf("Writing PFS0\n");
nca_write_file(program_nca_file, &program_logo);
// Write Padding if required
nca_write_padding(program_nca_file);
if (settings->noromfs == 0)
nca_header.section_entries[2].media_start_offset = nca_header.section_entries[1].media_end_offset;
else
nca_header.section_entries[2].media_start_offset = nca_header.section_entries[0].media_end_offset;
nca_header.section_entries[2].media_end_offset = (uint32_t)(ftello64(program_nca_file) / 0x200); // Section end offset / 200
nca_header.section_entries[2]._0x8[0] = 0x1; // Always 1
nca_header.fs_headers[2].fs_type = FS_TYPE_PFS0;
nca_header.fs_headers[2].partition_type = 0x1;
nca_header.fs_headers[2]._0x0 = 0x2; // Always 2
nca_header.fs_headers[2].crypt_type = 0x1; // Plain text
nca_header.fs_headers[2].pfs0_superblock.always_2 = 0x2;
nca_header.fs_headers[2].pfs0_superblock.block_size = PFS0_HASH_BLOCK_SIZE;
// Calculate master hash and section hash
printf("\n===> Calculating Hashes:\n");
printf("Calculating Master hash\n");
pfs0_calculate_master_hash(&program_logo_hash_table, nca_header.fs_headers[2].pfs0_superblock.hash_table_size, nca_header.fs_headers[2].pfs0_superblock.master_hash);
printf("Calculating Section hash\n");
nca_calculate_section_hash(&nca_header.fs_headers[2], nca_header.section_hashes[2]);
}
printf("\n---> Finalizing:\n");
// Set encrypted key area key 2
memset(nca_header.encrypted_keys[2], 4, 0x10);
// Encrypt sections
printf("===> Encrypting NCA\n");
printf("Encrypting section 0\n");
nca_encrypt_section(program_nca_file, &nca_header, 0);
if (settings->noromfs == 0)
{
printf("Encrypting section 1\n");
nca_encrypt_section(program_nca_file, &nca_header, 1);
}
// Encrypt header
printf("Getting NCA file size\n");
fseeko64(program_nca_file, 0, SEEK_END);
nca_header.nca_size = (uint64_t)ftello64(program_nca_file);
printf("Encrypting key area\n");
nca_encrypt_key_area(&nca_header, settings);
printf("Encrypting header\n");
nca_encrypt_header(&nca_header, settings);
// Write MCA header
printf("\n===> Writing NCA header\n");
printf("Writing NCA header to %s\n", program_nca_path.char_path);
fseeko64(program_nca_file, 0, SEEK_SET);
fwrite(&nca_header, 1, sizeof(nca_header), program_nca_file);
// Calculate hash and nca size
printf("\n===> Post creation process\n");
printf("Calculating NCA hash\n");
nca_calculate_hash(program_nca_file, cnmt_ctx, 0);
fclose(program_nca_file);
// Rename control.nca to ncaid.nca
filepath_t program_nca_final_path;
filepath_init(&program_nca_final_path);
filepath_copy(&program_nca_final_path, &settings->out_dir);
char program_nca_name[37];
hexBinaryString(cnmt_ctx->cnmt_content_records[0].ncaid, 16, program_nca_name, 33);
strcat(program_nca_name, ".nca");
program_nca_name[36] = '\0';
printf("Renaming program.nca to %s\n", program_nca_name);
filepath_append(&program_nca_final_path, "%s", program_nca_name);
os_rename(program_nca_path.os_path, program_nca_final_path.os_path);
printf("\n----> Created Program NCA: %s\n", program_nca_final_path.char_path);
}
void nca_create_meta(hbp_settings_t *settings, cnmt_ctx_t *cnmt_ctx)
{
printf("----> Creating Meta NCA:\n");
printf("===> Creating NCA header\n");
nca_header_t nca_header;
memset(&nca_header, 0, sizeof(nca_header));
filepath_t meta_nca_path;
filepath_init(&meta_nca_path);
filepath_copy(&meta_nca_path, &settings->out_dir);
filepath_append(&meta_nca_path, "meta.nca");
FILE *meta_nca_file;
meta_nca_file = os_fopen(meta_nca_path.os_path, OS_MODE_WRITE_EDIT);
// Write placeholder for NCA header
printf("Writing NCA header placeholder to %s\n", meta_nca_path.char_path);
if (meta_nca_file != NULL)
fwrite(&nca_header, 1, sizeof(nca_header), meta_nca_file);
else
{
fprintf(stderr, "Failed to create %s!\n", meta_nca_path.char_path);
exit(EXIT_FAILURE);
}
filepath_t cnmt_path;
filepath_init(&cnmt_path);
filepath_copy(&cnmt_path, &settings->temp_dir);
filepath_append(&cnmt_path, "cnmt");
filepath_t cnmt_dir_path;
filepath_init(&cnmt_dir_path);
filepath_copy(&cnmt_dir_path, &cnmt_path);
// Create cnmt directory if required
os_makedir(cnmt_dir_path.os_path);
// Cnmt filename = Application_tid.cnmt
filepath_append(&cnmt_path, "Application_%016" PRIx64 ".cnmt", cnmt_ctx->cnmt_header.title_id);
printf("\n===> Creating Metadata file\n");
cnmt_create(cnmt_ctx, &cnmt_path);
//Build PFS0
filepath_t meta_pfs0;
filepath_init(&meta_pfs0);
filepath_copy(&meta_pfs0, &settings->temp_dir);
filepath_append(&meta_pfs0, "meta_sec0_pfs0");
filepath_t meta_pfs0_hash_table;
filepath_init(&meta_pfs0_hash_table);
filepath_copy(&meta_pfs0_hash_table, &settings->temp_dir);
filepath_append(&meta_pfs0_hash_table, "meta_sec0_pfs0_hashtable");
printf("\n===> Building PFS0\n");
pfs0_build(&cnmt_dir_path, &meta_pfs0, &nca_header.fs_headers[0].pfs0_superblock.pfs0_size);
printf("Calculating hash table\n");
pfs0_create_hashtable(&meta_pfs0, &meta_pfs0_hash_table, &nca_header.fs_headers[0].pfs0_superblock.hash_table_size, &nca_header.fs_headers[0].pfs0_superblock.pfs0_offset);
// Write ExeFS
printf("\n===> Writing PFS0 section\n");
printf("Writing PFS0 hash table\n");
nca_write_file(meta_nca_file, &meta_pfs0_hash_table);
printf("Writing PFS0\n");
nca_write_file(meta_nca_file, &meta_pfs0);
// Write Padding if required
nca_write_padding(meta_nca_file);
// Common values
nca_header.magic = MAGIC_NCA3;
nca_header.content_type = 0x1; // Meta
nca_header.sdk_minor = 12;
nca_header.sdk_micro = 17;
nca_header.title_id = cnmt_ctx->cnmt_header.title_id;
nca_header.section_entries[0].media_start_offset = 0x6; // 0xC00 / 0x200
nca_header.section_entries[0].media_end_offset = (uint32_t)(ftello64(meta_nca_file) / 0x200); // Section end offset / 200
nca_header.section_entries[0]._0x8[0] = 0x1; // Always 1
nca_header.fs_headers[0].fs_type = FS_TYPE_PFS0;
nca_header.fs_headers[0].partition_type = 0x1;
nca_header.fs_headers[0]._0x0 = 0x2; // Always 2
nca_header.fs_headers[0].crypt_type = 0x3; // Regular crypto
nca_header.fs_headers[0].pfs0_superblock.always_2 = 0x2;
nca_header.fs_headers[0].pfs0_superblock.block_size = PFS0_HASH_BLOCK_SIZE;
// Calculate master hash and section hash
printf("\n===> Calculating Hashes:\n");
printf("Calculating Master hash\n");
pfs0_calculate_master_hash(&meta_pfs0_hash_table, nca_header.fs_headers[0].pfs0_superblock.hash_table_size, nca_header.fs_headers[0].pfs0_superblock.master_hash);
printf("Calculating Section hash\n");
nca_calculate_section_hash(&nca_header.fs_headers[0], nca_header.section_hashes[0]);
printf("\n---> Finalizing:\n");
// Set encrypted key area key 2
memset(nca_header.encrypted_keys[2], 4, 0x10);
// Encrypt section 0
printf("===> Encrypting NCA\n");
printf("Encrypting section 0\n");
nca_encrypt_section(meta_nca_file, &nca_header, 0);
// Encrypt header
printf("Getting NCA file size\n");
fseeko64(meta_nca_file, 0, SEEK_END);
nca_header.nca_size = (uint64_t)ftello64(meta_nca_file);
printf("Encrypting key area\n");
nca_encrypt_key_area(&nca_header, settings);
printf("Encrypting header\n");
nca_encrypt_header(&nca_header, settings);
// Write MCA header
printf("\n===> Writing NCA header\n");
printf("Writing NCA header to %s\n", meta_nca_path.char_path);
fseeko64(meta_nca_file, 0, SEEK_SET);
fwrite(&nca_header, 1, sizeof(nca_header), meta_nca_file);
// Calculate hash and nca size
printf("\n===> Post creation process\n");
printf("Calculating NCA hash\n");
nca_calculate_hash(meta_nca_file, cnmt_ctx, 2);
fclose(meta_nca_file);
// Rename meta.nca to ncaid.cnmt.nca
filepath_t meta_nca_final_path;
filepath_init(&meta_nca_final_path);
filepath_copy(&meta_nca_final_path, &settings->out_dir);
char meta_nca_name[42];
hexBinaryString(cnmt_ctx->cnmt_content_records[2].ncaid, 16, meta_nca_name, 33);
strcat(meta_nca_name, ".cnmt.nca");
meta_nca_name[41] = '\0';
printf("Renaming meta.nca to %s\n", meta_nca_name);
filepath_append(&meta_nca_final_path, "%s", meta_nca_name);
os_rename(meta_nca_path.os_path, meta_nca_final_path.os_path);
printf("\n----> Created Metadata NCA: %s\n", meta_nca_final_path.char_path);
}
void nca_write_file(FILE *nca_file, filepath_t *file_path)
{
uint64_t file_size;
FILE *fl;
fl = os_fopen(file_path->os_path, OS_MODE_READ);
if (fl == NULL)
{
fprintf(stderr, "Failed to open %s!\n", file_path->char_path);
exit(EXIT_FAILURE);
}
// Get IVFC level file filesize
fseeko64(fl, 0, SEEK_END);
file_size = ftello64(fl);
fseeko64(fl, 0, SEEK_SET);
uint64_t read_size = 0x61A8000; // 100 MB buffer.
unsigned char *buf = malloc(read_size);
if (buf == NULL)
{
fprintf(stderr, "Failed to allocate file-read buffer!\n");
exit(EXIT_FAILURE);
}
uint64_t ofs = 0;
while (ofs < file_size)
{
if (ofs + read_size >= file_size)
read_size = file_size - ofs;
if (fread(buf, 1, read_size, fl) != read_size)
{
fprintf(stderr, "Failed to read file %s\n", file_path->char_path);
exit(EXIT_FAILURE);
}
fwrite(buf, read_size, 1, nca_file);
ofs += read_size;
}
free(buf);
fclose(fl);
}
// Write padding for media_end_offset
void nca_write_padding(FILE *nca_file)
{
unsigned char *buf = (unsigned char *)calloc(1, 0x200);
uint64_t curr_offset = ftello64(nca_file);
uint64_t block_size = 0x200;
uint64_t padding_size = block_size - (curr_offset % block_size);
if (curr_offset % block_size != 0)
fwrite(buf, 1, padding_size, nca_file);
free(buf);
}
void nca_calculate_section_hash(nca_fs_header_t *fs_header, uint8_t *out_section_hash)
{
// Calculate hash
sha_ctx_t *sha_ctx = new_sha_ctx(HASH_TYPE_SHA256, 0);
sha_update(sha_ctx, fs_header, 0x200);
sha_get_hash(sha_ctx, (unsigned char *)out_section_hash);
free_sha_ctx(sha_ctx);
}
void nca_encrypt_key_area(nca_header_t *nca_header, hbp_settings_t *settings)
{
aes_ctx_t *aes_ctx = new_aes_ctx(settings->keyset.key_area_keys[0][0], 16, AES_MODE_ECB);
aes_encrypt(aes_ctx, nca_header->encrypted_keys, nca_header->encrypted_keys, 0x40);
free_aes_ctx(aes_ctx);
}
void nca_encrypt_header(nca_header_t *nca_header, hbp_settings_t *settings)
{
aes_ctx_t *hdr_aes_ctx = new_aes_ctx(settings->keyset.header_key, 32, AES_MODE_XTS);
aes_xts_encrypt(hdr_aes_ctx, nca_header, nca_header, 0xC00, 0, 0x200);
free_aes_ctx(hdr_aes_ctx);
}
void nca_encrypt_section(FILE *nca_file, nca_header_t *nca_header, uint8_t section_index)
{
uint64_t start_offset = nca_header->section_entries[section_index].media_start_offset * 0x200;
uint64_t end_offset = nca_header->section_entries[section_index].media_end_offset * 0x200;
uint64_t filesize = end_offset - start_offset;
// Calculate counter for section encryption
uint64_t ctr_ofs = start_offset >> 4;
unsigned char ctr[0x10] = {0};
for (unsigned int j = 0; j < 0x8; j++)
{
ctr[j] = nca_header->fs_headers[section_index].section_ctr[0x8 - j - 1];
ctr[0x10 - j - 1] = (unsigned char)(ctr_ofs & 0xFF);
ctr_ofs >>= 8;
}
uint64_t read_size = 0x1000000; // 16 MB buffer.
unsigned char *buf = malloc(read_size);
if (buf == NULL)
{
fprintf(stderr, "Failed to allocate file-read buffer!\n");
exit(EXIT_FAILURE);
}
uint64_t ofs = 0;
fseeko64(nca_file, start_offset, SEEK_SET);
aes_ctx_t *aes_ctx = new_aes_ctx(nca_header->encrypted_keys[2], 16, AES_MODE_CTR);
while (ofs < filesize)
{
if (ofs + read_size >= filesize)
read_size = filesize - ofs;
if (fread(buf, 1, read_size, nca_file) != read_size)
{
fprintf(stderr, "Failed to read file!\n");
exit(EXIT_FAILURE);
}
fseeko64(nca_file, start_offset + ofs, SEEK_SET);
aes_setiv(aes_ctx, ctr, 0x10);
aes_encrypt(aes_ctx, buf, buf, read_size);
fwrite(buf, 1, read_size, nca_file);
ofs += read_size;
nca_update_ctr(ctr, start_offset + ofs);
}
free(buf);
free_aes_ctx(aes_ctx);
}
/* Updates the CTR for an offset. */
void nca_update_ctr(unsigned char *ctr, uint64_t ofs)
{
ofs >>= 4;
for (unsigned int j = 0; j < 0x8; j++)
{
ctr[0x10 - j - 1] = (unsigned char)(ofs & 0xFF);
ofs >>= 8;
}
}
void nca_calculate_hash(FILE *nca_file, cnmt_ctx_t *cnmt_ctx, uint8_t cnmt_index)
{
uint64_t file_size;
// Get source file size
fseeko64(nca_file, 0, SEEK_END);
file_size = (uint64_t)ftello64(nca_file);
memcpy(cnmt_ctx->cnmt_content_records[cnmt_index].size, &file_size, 0x6);
sha_ctx_t *sha_ctx = new_sha_ctx(HASH_TYPE_SHA256, 0);
uint64_t read_size = 0x61A8000; // 100 MB buffer.
unsigned char *buf = malloc(read_size);
fseeko64(nca_file, 0, SEEK_SET);
if (buf == NULL)
{
fprintf(stderr, "Failed to allocate file-read buffer!\n");
exit(EXIT_FAILURE);
}
uint64_t ofs = 0;
while (ofs < file_size)
{
if (ofs + read_size >= file_size)
read_size = file_size - ofs;
if (fread(buf, 1, read_size, nca_file) != read_size)
{
fprintf(stderr, "Failed to read file!\n");
exit(EXIT_FAILURE);
}
sha_update(sha_ctx, buf, read_size);
ofs += read_size;
}
sha_get_hash(sha_ctx, cnmt_ctx->cnmt_content_records[cnmt_index].hash);
memcpy(cnmt_ctx->cnmt_content_records[cnmt_index].ncaid, &cnmt_ctx->cnmt_content_records[cnmt_index].hash, 0x10); // NcaID = first 16 bytes of hash
free(buf);
free_sha_ctx(sha_ctx);
}