2020-04-26 09:35:01 +01:00
|
|
|
/*
|
2020-07-03 10:31:22 +01:00
|
|
|
* pfs.h
|
2020-04-26 09:35:01 +01:00
|
|
|
*
|
2023-04-08 12:42:22 +01:00
|
|
|
* Copyright (c) 2020-2023, DarkMatterCore <pabloacurielz@gmail.com>.
|
2020-07-03 10:31:22 +01:00
|
|
|
*
|
|
|
|
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
|
|
|
|
*
|
2021-03-25 19:26:58 +00:00
|
|
|
* nxdumptool is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2020-04-26 09:35:01 +01:00
|
|
|
*
|
2021-03-25 19:26:58 +00:00
|
|
|
* nxdumptool is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2020-04-26 09:35:01 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-03-25 19:26:58 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-04-26 09:35:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifndef __PFS_H__
|
|
|
|
#define __PFS_H__
|
|
|
|
|
2022-07-04 00:36:01 +01:00
|
|
|
#include "nca_storage.h"
|
2020-04-26 09:35:01 +01:00
|
|
|
|
2021-03-24 17:25:19 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
#define PFS0_MAGIC 0x50465330 /* "PFS0". */
|
2020-04-26 09:35:01 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
u32 magic; ///< "PFS0".
|
|
|
|
u32 entry_count;
|
|
|
|
u32 name_table_size;
|
|
|
|
u8 reserved[0x4];
|
|
|
|
} PartitionFileSystemHeader;
|
|
|
|
|
2021-03-24 17:25:19 +00:00
|
|
|
NXDT_ASSERT(PartitionFileSystemHeader, 0x10);
|
|
|
|
|
2020-04-26 09:35:01 +01:00
|
|
|
typedef struct {
|
|
|
|
u64 offset;
|
|
|
|
u64 size;
|
|
|
|
u32 name_offset;
|
|
|
|
u8 reserved[0x4];
|
|
|
|
} PartitionFileSystemEntry;
|
|
|
|
|
2021-03-24 17:25:19 +00:00
|
|
|
NXDT_ASSERT(PartitionFileSystemEntry, 0x18);
|
|
|
|
|
2020-10-21 05:27:48 +01:00
|
|
|
/// Used with Partition FS sections from NCAs.
|
2020-04-26 09:35:01 +01:00
|
|
|
typedef struct {
|
2022-07-04 00:36:01 +01:00
|
|
|
NcaStorageContext storage_ctx; ///< Used to read NCA FS section data.
|
2022-07-04 01:20:51 +01:00
|
|
|
NcaFsSectionContext *nca_fs_ctx; ///< Same as storage_ctx.nca_fs_ctx. Placed here for convenience.
|
2020-04-26 09:35:01 +01:00
|
|
|
u64 offset; ///< Partition offset (relative to the start of the NCA FS section).
|
|
|
|
u64 size; ///< Partition size.
|
|
|
|
bool is_exefs; ///< ExeFS flag.
|
|
|
|
u64 header_size; ///< Full header size.
|
|
|
|
u8 *header; ///< PartitionFileSystemHeader + (PartitionFileSystemEntry * entry_count) + Name Table.
|
|
|
|
} PartitionFileSystemContext;
|
|
|
|
|
2023-11-03 01:22:47 +00:00
|
|
|
/// Used to generate Partition FS images (e.g. NSPs).
|
2020-10-21 05:27:48 +01:00
|
|
|
typedef struct {
|
|
|
|
PartitionFileSystemHeader header; ///< Partition FS header. Holds the entry count and name table size.
|
|
|
|
PartitionFileSystemEntry *entries; ///< Partition FS entries.
|
|
|
|
char *name_table; ///< Name table.
|
|
|
|
u64 fs_size; ///< Partition FS data size. Updated each time a new entry is added.
|
2023-11-03 01:22:47 +00:00
|
|
|
} PartitionFileSystemImageContext;
|
2020-10-21 05:27:48 +01:00
|
|
|
|
2020-10-02 10:53:58 +01:00
|
|
|
/// Initializes a Partition FS context.
|
2020-04-26 09:35:01 +01:00
|
|
|
bool pfsInitializeContext(PartitionFileSystemContext *out, NcaFsSectionContext *nca_fs_ctx);
|
|
|
|
|
2020-10-02 10:53:58 +01:00
|
|
|
/// Reads raw partition data using a Partition FS context.
|
|
|
|
/// Input offset must be relative to the start of the Partition FS.
|
2020-04-26 09:35:01 +01:00
|
|
|
bool pfsReadPartitionData(PartitionFileSystemContext *ctx, void *out, u64 read_size, u64 offset);
|
|
|
|
|
2020-10-02 10:53:58 +01:00
|
|
|
/// Reads data from a previously retrieved PartitionFileSystemEntry using a Partition FS context.
|
|
|
|
/// Input offset must be relative to the start of the Partition FS entry.
|
2020-04-26 09:35:01 +01:00
|
|
|
bool pfsReadEntryData(PartitionFileSystemContext *ctx, PartitionFileSystemEntry *fs_entry, void *out, u64 read_size, u64 offset);
|
|
|
|
|
2020-10-02 10:53:58 +01:00
|
|
|
/// Retrieves a Partition FS entry index by its name.
|
2020-07-06 01:10:07 +01:00
|
|
|
bool pfsGetEntryIndexByName(PartitionFileSystemContext *ctx, const char *name, u32 *out_idx);
|
|
|
|
|
2020-10-02 10:53:58 +01:00
|
|
|
/// Calculates the extracted Partition FS size.
|
2020-07-06 01:10:07 +01:00
|
|
|
bool pfsGetTotalDataSize(PartitionFileSystemContext *ctx, u64 *out_size);
|
|
|
|
|
2020-10-02 10:53:58 +01:00
|
|
|
/// Generates HierarchicalSha256 FS section patch data using a Partition FS context + entry, which can be used to seamlessly replace NCA data.
|
|
|
|
/// Input offset must be relative to the start of the Partition FS entry data.
|
2020-04-28 09:58:17 +01:00
|
|
|
/// This function shares the same limitations as ncaGenerateHierarchicalSha256Patch().
|
2020-07-22 21:35:23 +01:00
|
|
|
/// Use the pfsWriteEntryPatchToMemoryBuffer() wrapper to write patch data generated by this function.
|
2020-04-28 09:58:17 +01:00
|
|
|
bool pfsGenerateEntryPatch(PartitionFileSystemContext *ctx, PartitionFileSystemEntry *fs_entry, const void *data, u64 data_size, u64 data_offset, NcaHierarchicalSha256Patch *out);
|
2020-04-26 09:35:01 +01:00
|
|
|
|
2023-11-03 01:22:47 +00:00
|
|
|
/// Adds a new Partition FS entry to an existing PartitionFileSystemImageContext, using the provided entry name and size.
|
2020-10-21 05:27:48 +01:00
|
|
|
/// If 'out_entry_idx' is a valid pointer, the index to the new Partition FS entry will be saved to it.
|
2023-11-03 01:22:47 +00:00
|
|
|
bool pfsAddEntryInformationToImageContext(PartitionFileSystemImageContext *ctx, const char *entry_name, u64 entry_size, u32 *out_entry_idx);
|
2020-10-21 05:27:48 +01:00
|
|
|
|
2023-11-03 01:22:47 +00:00
|
|
|
/// Updates the name from a Partition FS entry in an existing PartitionFileSystemImageContext, using an entry index and the new entry name.
|
|
|
|
bool pfsUpdateEntryNameFromImageContext(PartitionFileSystemImageContext *ctx, u32 entry_idx, const char *new_entry_name);
|
2020-10-21 05:27:48 +01:00
|
|
|
|
2023-11-03 01:22:47 +00:00
|
|
|
/// Generates a full Partition FS header from an existing PartitionFileSystemImageContext and writes it to the provided memory buffer.
|
|
|
|
bool pfsWriteImageContextHeaderToMemoryBuffer(PartitionFileSystemImageContext *ctx, void *buf, u64 buf_size, u64 *out_header_size);
|
2020-10-21 05:27:48 +01:00
|
|
|
|
2020-04-26 09:35:01 +01:00
|
|
|
/// Miscellaneous functions.
|
|
|
|
|
2020-07-22 21:35:23 +01:00
|
|
|
NX_INLINE void pfsFreeContext(PartitionFileSystemContext *ctx)
|
|
|
|
{
|
|
|
|
if (!ctx) return;
|
2022-07-04 00:36:01 +01:00
|
|
|
ncaStorageFreeContext(&(ctx->storage_ctx));
|
2020-07-22 21:35:23 +01:00
|
|
|
if (ctx->header) free(ctx->header);
|
|
|
|
memset(ctx, 0, sizeof(PartitionFileSystemContext));
|
|
|
|
}
|
|
|
|
|
2020-04-26 09:35:01 +01:00
|
|
|
NX_INLINE u32 pfsGetEntryCount(PartitionFileSystemContext *ctx)
|
|
|
|
{
|
|
|
|
if (!ctx || !ctx->header_size || !ctx->header) return 0;
|
|
|
|
return ((PartitionFileSystemHeader*)ctx->header)->entry_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
NX_INLINE PartitionFileSystemEntry *pfsGetEntryByIndex(PartitionFileSystemContext *ctx, u32 idx)
|
|
|
|
{
|
|
|
|
if (idx >= pfsGetEntryCount(ctx)) return NULL;
|
|
|
|
return (PartitionFileSystemEntry*)(ctx->header + sizeof(PartitionFileSystemHeader) + (idx * sizeof(PartitionFileSystemEntry)));
|
|
|
|
}
|
|
|
|
|
|
|
|
NX_INLINE char *pfsGetNameTable(PartitionFileSystemContext *ctx)
|
|
|
|
{
|
|
|
|
u32 entry_count = pfsGetEntryCount(ctx);
|
|
|
|
if (!entry_count) return NULL;
|
|
|
|
return (char*)(ctx->header + sizeof(PartitionFileSystemHeader) + (entry_count * sizeof(PartitionFileSystemEntry)));
|
|
|
|
}
|
|
|
|
|
2020-10-11 16:22:26 +01:00
|
|
|
NX_INLINE char *pfsGetEntryName(PartitionFileSystemContext *ctx, PartitionFileSystemEntry *fs_entry)
|
|
|
|
{
|
|
|
|
char *name_table = pfsGetNameTable(ctx);
|
|
|
|
if (!name_table || !fs_entry || fs_entry->name_offset >= ((PartitionFileSystemHeader*)ctx->header)->name_table_size || !name_table[fs_entry->name_offset]) return NULL;
|
|
|
|
return (name_table + fs_entry->name_offset);
|
|
|
|
}
|
|
|
|
|
2020-04-26 09:35:01 +01:00
|
|
|
NX_INLINE char *pfsGetEntryNameByIndex(PartitionFileSystemContext *ctx, u32 idx)
|
|
|
|
{
|
|
|
|
PartitionFileSystemEntry *fs_entry = pfsGetEntryByIndex(ctx, idx);
|
|
|
|
char *name_table = pfsGetNameTable(ctx);
|
|
|
|
if (!fs_entry || !name_table) return NULL;
|
|
|
|
return (name_table + fs_entry->name_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
NX_INLINE PartitionFileSystemEntry *pfsGetEntryByName(PartitionFileSystemContext *ctx, const char *name)
|
|
|
|
{
|
|
|
|
u32 idx = 0;
|
|
|
|
if (!pfsGetEntryIndexByName(ctx, name, &idx)) return NULL;
|
|
|
|
return pfsGetEntryByIndex(ctx, idx);
|
|
|
|
}
|
|
|
|
|
2020-07-22 21:35:23 +01:00
|
|
|
NX_INLINE void pfsWriteEntryPatchToMemoryBuffer(PartitionFileSystemContext *ctx, NcaHierarchicalSha256Patch *patch, void *buf, u64 buf_size, u64 buf_offset)
|
|
|
|
{
|
2022-07-04 13:30:48 +01:00
|
|
|
if (!ctx || !ncaStorageIsValidContext(&(ctx->storage_ctx)) || ctx->nca_fs_ctx != ctx->storage_ctx.nca_fs_ctx || \
|
|
|
|
ctx->storage_ctx.base_storage_type != NcaStorageBaseStorageType_Regular) return;
|
2022-09-12 19:19:10 +01:00
|
|
|
ncaWriteHierarchicalSha256PatchToMemoryBuffer(ctx->nca_fs_ctx->nca_ctx, patch, buf, buf_size, buf_offset);
|
2020-07-22 21:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NX_INLINE void pfsFreeEntryPatch(NcaHierarchicalSha256Patch *patch)
|
|
|
|
{
|
|
|
|
ncaFreeHierarchicalSha256Patch(patch);
|
|
|
|
}
|
|
|
|
|
2023-11-03 01:22:47 +00:00
|
|
|
NX_INLINE void pfsFreeImageContext(PartitionFileSystemImageContext *ctx)
|
2020-10-21 05:27:48 +01:00
|
|
|
{
|
|
|
|
if (!ctx) return;
|
|
|
|
if (ctx->entries) free(ctx->entries);
|
|
|
|
if (ctx->name_table) free(ctx->name_table);
|
2023-11-03 01:22:47 +00:00
|
|
|
memset(ctx, 0, sizeof(PartitionFileSystemImageContext));
|
2020-10-21 05:27:48 +01:00
|
|
|
}
|
|
|
|
|
2023-11-03 01:22:47 +00:00
|
|
|
NX_INLINE void pfsInitializeImageContext(PartitionFileSystemImageContext *ctx)
|
2020-10-21 05:27:48 +01:00
|
|
|
{
|
|
|
|
if (!ctx) return;
|
2023-11-03 01:22:47 +00:00
|
|
|
pfsFreeImageContext(ctx);
|
2020-10-21 05:27:48 +01:00
|
|
|
ctx->header.magic = __builtin_bswap32(PFS0_MAGIC);
|
|
|
|
}
|
|
|
|
|
2023-11-03 01:22:47 +00:00
|
|
|
NX_INLINE u32 pfsGetEntryCountFromImageContext(PartitionFileSystemImageContext *ctx)
|
2020-10-26 06:39:33 +00:00
|
|
|
{
|
|
|
|
return (ctx ? ctx->header.entry_count : 0);
|
|
|
|
}
|
|
|
|
|
2023-11-03 01:22:47 +00:00
|
|
|
NX_INLINE PartitionFileSystemEntry *pfsGetEntryByIndexFromImageContext(PartitionFileSystemImageContext *ctx, u32 idx)
|
2020-10-26 06:39:33 +00:00
|
|
|
{
|
2023-11-03 01:22:47 +00:00
|
|
|
if (idx >= pfsGetEntryCountFromImageContext(ctx)) return NULL;
|
2020-10-26 06:39:33 +00:00
|
|
|
return &(ctx->entries[idx]);
|
|
|
|
}
|
|
|
|
|
2023-11-03 01:22:47 +00:00
|
|
|
NX_INLINE char *pfsGetEntryNameByIndexFromImageContext(PartitionFileSystemImageContext *ctx, u32 idx)
|
2020-10-26 06:39:33 +00:00
|
|
|
{
|
2023-11-03 01:22:47 +00:00
|
|
|
PartitionFileSystemEntry *fs_entry = pfsGetEntryByIndexFromImageContext(ctx, idx);
|
2020-10-26 06:39:33 +00:00
|
|
|
if (!fs_entry || !ctx->name_table) return NULL;
|
|
|
|
return (ctx->name_table + fs_entry->name_offset);
|
|
|
|
}
|
|
|
|
|
2021-03-23 14:06:52 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2021-03-24 17:25:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __PFS_H__ */
|