From 226fbd0e2182e8daed0a1c20ba68ad7a3cd2af34 Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Tue, 28 Apr 2020 00:38:24 -0400 Subject: [PATCH] Small PFS fix. --- README.md | 25 ++++++++++++++++++++++--- source/pfs.c | 8 ++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9415e70..8aac20a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -todo: +``` +todo: hfs0: filelist generation methods @@ -9,12 +10,30 @@ romfs: data replacement methods romfs: filelist generation methods + + nacp mod: + + 1. Patch StartupUserAccount in control.nacp to 0x01. + 2. Recalculate hash for the control.nacp file. Store it in the RomFS file hash table. -> Not so sure about this... + 3. Recalculate hashes for the modified blocks, then perform chain hash calculation / replacement per each IVFC level up until reaching superblock hash. + 4. Reencrypt all the modified blocks using the already known crypto properties. Keep the modified + encrypted blocks in memory. + 5. Recalculate RomFS section header SHA-256 hash. Replace the section hash in the NCA header. Keep a copy of the modified NCA header in memory. + 6. While generating the output dump, replace sections in the Control NCA with the previously modified + reencrypted blocks before writing data. Also replace the NCA header. - - + Result txIsFat32(bool *mode) { + Result rc = serviceDispatch(&g_tx, 137); + if (rc == 0xa08) { + *mode = false; + return 0; + } else if (rc == 0) { + *mode = true; + } + return rc; + } +``` diff --git a/source/pfs.c b/source/pfs.c index ad7c944..9f66958 100644 --- a/source/pfs.c +++ b/source/pfs.c @@ -158,9 +158,9 @@ bool pfsGenerateEntryPatch(PartitionFileSystemContext *ctx, PartitionFileSystemE u64 data_block_start_offset = (ctx->offset + ALIGN_DOWN(partition_offset, block_size)); u64 data_block_end_offset = (ctx->offset + ALIGN_UP(partition_offset + data_size, block_size)); + if (data_block_end_offset > (ctx->offset + ctx->size)) data_block_end_offset = (ctx->offset + ctx->size); u64 data_block_size = (data_block_end_offset - data_block_start_offset); - u64 block_count = (hash_block_size / SHA256_HASH_SIZE); u64 new_data_offset = (partition_offset - ALIGN_DOWN(partition_offset, block_size)); u8 *hash_table = NULL, *data_block = NULL; @@ -201,7 +201,11 @@ bool pfsGenerateEntryPatch(PartitionFileSystemContext *ctx, PartitionFileSystemE memcpy(data_block + new_data_offset, data, data_size); /* Recalculate hashes */ - for(u64 i = 0; i < block_count; i++) sha256CalculateHash(hash_table + hash_block_start_offset + (i * SHA256_HASH_SIZE), data_block + (i * block_size), block_size); + for(u64 i = 0, j = 0; i < data_block_size; i += block_size, j++) + { + if (block_size > (data_block_size - i)) block_size = (data_block_size - i); + sha256CalculateHash(hash_table + hash_block_start_offset + (j * SHA256_HASH_SIZE), data_block + i, block_size); + } /* Reencrypt hash block */ out->hash_block = ncaGenerateEncryptedFsSectionBlock(ctx->nca_fs_ctx, hash_table + hash_block_start_offset, hash_block_size, hash_table_offset + hash_block_start_offset, \