diff --git a/include/core/pfs.h b/include/core/pfs.h index e9570dd..e8205ab 100644 --- a/include/core/pfs.h +++ b/include/core/pfs.h @@ -53,6 +53,7 @@ NXDT_ASSERT(PartitionFileSystemEntry, 0x18); /// Used with Partition FS sections from NCAs. typedef struct { NcaStorageContext storage_ctx; ///< Used to read NCA FS section data. + NcaFsSectionContext *nca_fs_ctx; ///< Same as storage_ctx.nca_fs_ctx. Placed here for convenience. u64 offset; ///< Partition offset (relative to the start of the NCA FS section). u64 size; ///< Partition size. bool is_exefs; ///< ExeFS flag. @@ -154,8 +155,8 @@ NX_INLINE PartitionFileSystemEntry *pfsGetEntryByName(PartitionFileSystemContext NX_INLINE void pfsWriteEntryPatchToMemoryBuffer(PartitionFileSystemContext *ctx, NcaHierarchicalSha256Patch *patch, void *buf, u64 buf_size, u64 buf_offset) { - if (!ctx || !ncaStorageIsValidContext(&(ctx->storage_ctx))) return; - ncaWriteHierarchicalSha256PatchToMemoryBuffer((NcaContext*)ctx->storage_ctx.nca_fs_ctx->nca_ctx, patch, buf, buf_size, buf_offset); + if (!ctx || !ncaStorageIsValidContext(&(ctx->storage_ctx)) || ctx->nca_fs_ctx != ctx->storage_ctx.nca_fs_ctx) return; + ncaWriteHierarchicalSha256PatchToMemoryBuffer((NcaContext*)ctx->nca_fs_ctx->nca_ctx, patch, buf, buf_size, buf_offset); } NX_INLINE void pfsFreeEntryPatch(NcaHierarchicalSha256Patch *patch) diff --git a/include/core/romfs.h b/include/core/romfs.h index ecf5b74..1927df2 100644 --- a/include/core/romfs.h +++ b/include/core/romfs.h @@ -108,6 +108,7 @@ NXDT_ASSERT(RomFileSystemFileEntry, 0x20); typedef struct { NcaStorageContext storage_ctx; ///< Used to read NCA FS section data. + NcaFsSectionContext *nca_fs_ctx; ///< Same as storage_ctx.nca_fs_ctx. Placed here for convenience. u64 offset; ///< RomFS offset (relative to the start of the NCA FS section). u64 size; ///< RomFS size. RomFileSystemHeader header; ///< RomFS header. @@ -194,10 +195,11 @@ NX_INLINE RomFileSystemFileEntry *romfsGetFileEntryByOffset(RomFileSystemContext NX_INLINE void romfsWriteFileEntryPatchToMemoryBuffer(RomFileSystemContext *ctx, RomFileSystemFileEntryPatch *patch, void *buf, u64 buf_size, u64 buf_offset) { - if (!ctx || !ncaStorageIsValidContext(&(ctx->storage_ctx)) || !patch || (!patch->use_old_format_patch && ctx->storage_ctx.nca_fs_ctx->section_type == NcaFsSectionType_Nca0RomFs) || \ - (patch->use_old_format_patch && ctx->storage_ctx.nca_fs_ctx->section_type != NcaFsSectionType_Nca0RomFs)) return; + if (!ctx || !ncaStorageIsValidContext(&(ctx->storage_ctx)) || ctx->nca_fs_ctx != ctx->storage_ctx.nca_fs_ctx || !patch || \ + (!patch->use_old_format_patch && ctx->nca_fs_ctx->section_type == NcaFsSectionType_Nca0RomFs) || \ + (patch->use_old_format_patch && ctx->nca_fs_ctx->section_type != NcaFsSectionType_Nca0RomFs)) return; - NcaContext *nca_ctx = (NcaContext*)ctx->storage_ctx.nca_fs_ctx->nca_ctx; + NcaContext *nca_ctx = (NcaContext*)ctx->nca_fs_ctx->nca_ctx; if (patch->use_old_format_patch) { diff --git a/source/core/nca_storage.c b/source/core/nca_storage.c index 1940a21..a497f4f 100644 --- a/source/core/nca_storage.c +++ b/source/core/nca_storage.c @@ -99,6 +99,9 @@ bool ncaStorageInitializeContext(NcaStorageContext *out, NcaFsSectionContext *nc out->base_storage_type = NcaStorageBaseStorageType_Compressed; } + /* Update output context. */ + out->nca_fs_ctx = nca_fs_ctx; + /* Update return value. */ success = true; diff --git a/source/core/npdm.c b/source/core/npdm.c index 7ef1d99..6072370 100644 --- a/source/core/npdm.c +++ b/source/core/npdm.c @@ -30,7 +30,7 @@ bool npdmInitializeContext(NpdmContext *out, PartitionFileSystemContext *pfs_ctx bool success = false, dump_meta_header = false, dump_acid_header = false, dump_aci_header = false; PartitionFileSystemEntry *pfs_entry = NULL; - if (!out || !pfs_ctx || !ncaStorageIsValidContext(&(pfs_ctx->storage_ctx)) || !(nca_ctx = (NcaContext*)pfs_ctx->storage_ctx.nca_fs_ctx->nca_ctx) || \ + if (!out || !pfs_ctx || !ncaStorageIsValidContext(&(pfs_ctx->storage_ctx)) || !(nca_ctx = (NcaContext*)pfs_ctx->nca_fs_ctx->nca_ctx) || \ nca_ctx->content_type != NcmContentType_Program || !pfs_ctx->offset || !pfs_ctx->size || !pfs_ctx->is_exefs || \ pfs_ctx->header_size <= sizeof(PartitionFileSystemHeader) || !pfs_ctx->header) { diff --git a/source/core/nso.c b/source/core/nso.c index 1383fa8..2dac6dc 100644 --- a/source/core/nso.c +++ b/source/core/nso.c @@ -35,7 +35,7 @@ bool nsoInitializeContext(NsoContext *out, PartitionFileSystemContext *pfs_ctx, u8 *rodata_buf = NULL; bool success = false, dump_nso_header = false; - if (!out || !pfs_ctx || !ncaStorageIsValidContext(&(pfs_ctx->storage_ctx)) || !(nca_ctx = (NcaContext*)pfs_ctx->storage_ctx.nca_fs_ctx->nca_ctx) || \ + if (!out || !pfs_ctx || !ncaStorageIsValidContext(&(pfs_ctx->storage_ctx)) || !(nca_ctx = (NcaContext*)pfs_ctx->nca_fs_ctx->nca_ctx) || \ nca_ctx->content_type != NcmContentType_Program || !pfs_ctx->offset || !pfs_ctx->size || !pfs_ctx->is_exefs || \ pfs_ctx->header_size <= sizeof(PartitionFileSystemHeader) || !pfs_ctx->header || !pfs_entry) { diff --git a/source/core/pfs.c b/source/core/pfs.c index 3b630c7..09e689e 100644 --- a/source/core/pfs.c +++ b/source/core/pfs.c @@ -54,6 +54,8 @@ bool pfsInitializeContext(PartitionFileSystemContext *out, NcaFsSectionContext * goto end; } + out->nca_fs_ctx = storage_ctx->nca_fs_ctx; + /* Get Partition FS offset and size. */ if (!ncaGetFsSectionHashTargetProperties(nca_fs_ctx, &(out->offset), &(out->size))) { @@ -235,7 +237,7 @@ bool pfsGenerateEntryPatch(PartitionFileSystemContext *ctx, PartitionFileSystemE u64 partition_offset = (ctx->header_size + fs_entry->offset + data_offset); - if (!ncaGenerateHierarchicalSha256Patch(ctx->storage_ctx.nca_fs_ctx, data, data_size, partition_offset, out)) + if (!ncaGenerateHierarchicalSha256Patch(ctx->nca_fs_ctx, data, data_size, partition_offset, out)) { LOG_MSG("Failed to generate 0x%lX bytes HierarchicalSha256 patch at offset 0x%lX for Partition FS entry!", data_size, partition_offset); return false; diff --git a/source/core/romfs.c b/source/core/romfs.c index e172e2a..b60c5f1 100644 --- a/source/core/romfs.c +++ b/source/core/romfs.c @@ -54,6 +54,8 @@ bool romfsInitializeContext(RomFileSystemContext *out, NcaFsSectionContext *nca_ goto end; } + out->nca_fs_ctx = storage_ctx->nca_fs_ctx; + /* Get RomFS offset and size. */ if (!ncaGetFsSectionHashTargetProperties(nca_fs_ctx, &(out->offset), &(out->size))) { @@ -312,7 +314,7 @@ RomFileSystemFileEntry *romfsGetFileEntryByPath(RomFileSystemContext *ctx, const RomFileSystemDirectoryEntry *dir_entry = NULL; NcaContext *nca_ctx = NULL; - if (!ctx || !ctx->file_table || !ctx->file_table_size || !ncaStorageIsValidContext(&(ctx->storage_ctx)) || !(nca_ctx = (NcaContext*)ctx->storage_ctx.nca_fs_ctx->nca_ctx) || \ + if (!ctx || !ctx->file_table || !ctx->file_table_size || !ncaStorageIsValidContext(&(ctx->storage_ctx)) || !(nca_ctx = (NcaContext*)ctx->nca_fs_ctx->nca_ctx) || \ !path || *path != '/' || (path_len = strlen(path)) <= 1) { LOG_MSG("Invalid parameters!"); @@ -505,14 +507,14 @@ bool romfsGeneratePathFromFileEntry(RomFileSystemContext *ctx, RomFileSystemFile bool romfsGenerateFileEntryPatch(RomFileSystemContext *ctx, RomFileSystemFileEntry *file_entry, const void *data, u64 data_size, u64 data_offset, RomFileSystemFileEntryPatch *out) { if (!ctx || !ncaStorageIsValidContext(&(ctx->storage_ctx)) || ctx->storage_ctx.base_storage_type != NcaStorageBaseStorageType_Regular || !ctx->body_offset || \ - (ctx->storage_ctx.nca_fs_ctx->section_type != NcaFsSectionType_Nca0RomFs && ctx->storage_ctx.nca_fs_ctx->section_type != NcaFsSectionType_RomFs) || !file_entry || \ + (ctx->nca_fs_ctx->section_type != NcaFsSectionType_Nca0RomFs && ctx->nca_fs_ctx->section_type != NcaFsSectionType_RomFs) || !file_entry || \ !file_entry->size || (file_entry->offset + file_entry->size) > ctx->size || !data || !data_size || (data_offset + data_size) > file_entry->size || !out) { LOG_MSG("Invalid parameters!"); return false; } - NcaFsSectionContext *nca_fs_ctx = ctx->storage_ctx.nca_fs_ctx; + NcaFsSectionContext *nca_fs_ctx = ctx->nca_fs_ctx; u64 fs_offset = (ctx->body_offset + file_entry->offset + data_offset); bool success = false;