@@ -83,8 +83,8 @@ size_t nca_bktr_section_physical_fread(nca_section_ctx_t *ctx, void *buffer, siz
83
83
nca_update_bktr_ctr (ctx -> ctr , subsec -> ctr_val , ctx -> bktr_ctx .bktr_seek + ctx -> offset );
84
84
fseeko64 (ctx -> file , (ctx -> offset + ctx -> bktr_ctx .bktr_seek ) & ~0xF , SEEK_SET );
85
85
uint32_t block_ofs ;
86
- bktr_subsection_entry_t * next_subsec = bktr_get_subsection ( ctx -> bktr_ctx . subsection_block , ctx -> bktr_ctx . bktr_seek + count ) ;
87
- if (next_subsec == subsec || ( ctx -> bktr_ctx .bktr_seek + count == next_subsec -> offset && next_subsec == subsec + 1 ) ) {
86
+ bktr_subsection_entry_t * next_subsec = subsec + 1 ;
87
+ if (ctx -> bktr_ctx .bktr_seek + count <= next_subsec -> offset ) {
88
88
/* Easy path, reading *only* within the subsection. */
89
89
if ((block_ofs = ctx -> bktr_ctx .bktr_seek & 0xF ) != 0 ) {
90
90
if ((read = fread (block_buf , 1 , 0x10 , ctx -> file )) != 0x10 ) {
@@ -397,7 +397,7 @@ void nca_process(nca_ctx_t *ctx) {
397
397
} else {
398
398
/* Decrypt title key. */
399
399
if (ctx -> tool_ctx -> settings .has_titlekey ) {
400
- aes_ctx_t * aes_ctx = new_aes_ctx (ctx -> tool_ctx -> settings .keyset .titlekeks [ctx -> crypto_type ], 16 , AES_MODE_CTR );
400
+ aes_ctx_t * aes_ctx = new_aes_ctx (ctx -> tool_ctx -> settings .keyset .titlekeks [ctx -> crypto_type ], 16 , AES_MODE_ECB );
401
401
aes_decrypt (aes_ctx , ctx -> tool_ctx -> settings .dec_titlekey , ctx -> tool_ctx -> settings .titlekey , 0x10 );
402
402
free_aes_ctx (aes_ctx );
403
403
}
@@ -901,7 +901,7 @@ void nca_process_bktr_section(nca_section_ctx_t *ctx) {
901
901
exit (EXIT_FAILURE );
902
902
}
903
903
/* Allocate space for an extra (fake) subsection entry, to simplify our logic. */
904
- void * subs = calloc (1 , sb -> subsection_header .size + sizeof (bktr_subsection_entry_t ));
904
+ void * subs = calloc (1 , sb -> subsection_header .size + 2 * sizeof (bktr_subsection_entry_t ));
905
905
if (subs == NULL ) {
906
906
fprintf (stderr , "Failed to allocate subsection header!\n" );
907
907
exit (EXIT_FAILURE );
@@ -921,11 +921,22 @@ void nca_process_bktr_section(nca_section_ctx_t *ctx) {
921
921
ctx -> bktr_ctx .relocation_block = relocs ;
922
922
ctx -> bktr_ctx .subsection_block = subs ;
923
923
924
+ if (ctx -> bktr_ctx .subsection_block -> bktr_entry_offset != sb -> subsection_header .offset ) {
925
+ free (relocs );
926
+ free (subs );
927
+ ctx -> bktr_ctx .relocation_block = NULL ;
928
+ ctx -> bktr_ctx .subsection_block = NULL ;
929
+ ctx -> superblock_hash_validity = VALIDITY_INVALID ;
930
+ return ;
931
+ }
932
+
924
933
/* This simplifies logic greatly... */
925
934
ctx -> bktr_ctx .relocation_block -> entries [ctx -> bktr_ctx .relocation_block -> num_entries ].virt_offset = ctx -> bktr_ctx .relocation_block -> patch_romfs_size ;
926
935
ctx -> bktr_ctx .subsection_block -> entries [ctx -> bktr_ctx .subsection_block -> num_entries ].offset = sb -> relocation_header .offset ;
927
936
ctx -> bktr_ctx .subsection_block -> entries [ctx -> bktr_ctx .subsection_block -> num_entries ].ctr_val = ctx -> header -> section_ctr_low ;
928
-
937
+ ctx -> bktr_ctx .subsection_block -> entries [ctx -> bktr_ctx .subsection_block -> num_entries + 1 ].offset = ctx -> size ;
938
+ ctx -> bktr_ctx .subsection_block -> entries [ctx -> bktr_ctx .subsection_block -> num_entries + 1 ].ctr_val = 0 ;
939
+
929
940
930
941
/* Now parse out the romfs stuff. */
931
942
for (unsigned int i = 0 ; i < IVFC_MAX_LEVEL ; i ++ ) {
@@ -1110,8 +1121,10 @@ void nca_save_section(nca_section_ctx_t *ctx) {
1110
1121
size = ctx -> romfs_ctx .ivfc_levels [IVFC_MAX_LEVEL - 1 ].data_size ;
1111
1122
break ;
1112
1123
case BKTR :
1113
- offset = ctx -> bktr_ctx .ivfc_levels [IVFC_MAX_LEVEL - 1 ].data_offset ;
1114
- size = ctx -> bktr_ctx .ivfc_levels [IVFC_MAX_LEVEL - 1 ].data_size ;
1124
+ if (ctx -> tool_ctx -> base_file != NULL ) {
1125
+ offset = ctx -> bktr_ctx .ivfc_levels [IVFC_MAX_LEVEL - 1 ].data_offset ;
1126
+ size = ctx -> bktr_ctx .ivfc_levels [IVFC_MAX_LEVEL - 1 ].data_size ;
1127
+ }
1115
1128
break ;
1116
1129
case INVALID :
1117
1130
break ;
@@ -1131,6 +1144,7 @@ void nca_save_section(nca_section_ctx_t *ctx) {
1131
1144
}
1132
1145
if (secpath != NULL && secpath -> valid == VALIDITY_VALID ) {
1133
1146
printf ("Saving Section %" PRId32 " to %s...\n" , ctx -> section_num , secpath -> char_path );
1147
+ printf ("Size: %012" PRIx64 "\n" , size );
1134
1148
nca_save_section_file (ctx , offset , size , secpath );
1135
1149
}
1136
1150
0 commit comments