sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
libpfs.h
Go to the documentation of this file.
1
2/*
3# _____ ___ ____ ___ ____
4# ____| | ____| | | |____|
5# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
6#-----------------------------------------------------------------------
7# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
8# Licenced under Academic Free License version 2.0
9# Review ps2sdk README & LICENSE files for further details.
10*/
11
16#ifndef __LIBPFS_H__
17#define __LIBPFS_H__
18
19#include <types.h>
20
21#ifndef PFS_OPT_H
22#define PFS_OPT_H "pfs-opt.h"
23#endif
24
25#include PFS_OPT_H
26
27// General constants
28#define PFS_BLOCKSIZE 0x2000
29#define PFS_SUPER_MAGIC 0x50465300 // "PFS\0" aka Playstation Filesystem
30#define PFS_JOUNRNAL_MAGIC 0x5046534C // "PFSL" aka PFS Log
31#define PFS_SEGD_MAGIC 0x53454744 // "SEGD" aka segment descriptor direct
32#define PFS_SEGI_MAGIC 0x53454749 // "SEGI" aka segment descriptor indirect
33#define PFS_MAX_SUBPARTS 64
34#define PFS_NAME_LEN 255
35#define PFS_FORMAT_VERSION 3
36#define PFS_INODE_MAX_BLOCKS 114
37
38// attribute flags
39#define PFS_FIO_ATTR_READABLE 0x0001
40#define PFS_FIO_ATTR_WRITEABLE 0x0002
41#define PFS_FIO_ATTR_EXECUTABLE 0x0004
42#define PFS_FIO_ATTR_COPYPROTECT 0x0008
43#define PFS_FIO_ATTR_UNK0010 0x0010
44#define PFS_FIO_ATTR_SUBDIR 0x0020
45#define PFS_FIO_ATTR_UNK0040 0x0040
46#define PFS_FIO_ATTR_CLOSED 0x0080
47#define PFS_FIO_ATTR_UNK0100 0x0100
48#define PFS_FIO_ATTR_UNK0200 0x0200
49#define PFS_FIO_ATTR_UNK0400 0x0400
50#define PFS_FIO_ATTR_PDA 0x0800
51#define PFS_FIO_ATTR_PSX 0x1000
52#define PFS_FIO_ATTR_UNK2000 0x2000
53#define PFS_FIO_ATTR_HIDDEN 0x4000
54
55// cache flags (status)
56#define PFS_CACHE_FLAG_DIRTY 0x01
57#define PFS_CACHE_FLAG_NOLOAD 0x02
58#define PFS_CACHE_FLAG_MASKSTATUS 0x0F
59
60// cache flags (types)
61#define PFS_CACHE_FLAG_NOTHING 0x00
62#define PFS_CACHE_FLAG_SEGD 0x10
63#define PFS_CACHE_FLAG_SEGI 0x20
64#define PFS_CACHE_FLAG_BITMAP 0x40
65#define PFS_CACHE_FLAG_MASKTYPE 0xF0
66
67// fsck stats
68#define PFS_FSCK_STAT_OK 0x00
69#define PFS_FSCK_STAT_WRITE_ERROR 0x01
70#define PFS_FSCK_STAT_ERRORS_FIXED 0x02
71
72// odd and end
73#define PFS_MODE_SET_FLAG 0x00
74#define PFS_MODE_REMOVE_FLAG 0x01
75#define PFS_MODE_CHECK_FLAG 0x02
76
77// UID and GID
78/* UID and GID are fixed with constants.
79 Files (and directories) created by the system have UID and GID set to 0,
80 as they were made with an older library.
81
82 As of release v2.3, the UID and GID were changed to 0xFFFF.
83 The HDD Browser probably changes the UID and GID to 0xFFFF with chstat,
84 although I have not verified that.
85
86 If the UID and GID are not set to 0xFFFF, then the software may ignore the entry. */
87#define PFS_UID 0xFFFF
88#define PFS_GID 0xFFFF
89
90// journal/log
91typedef struct {
92 u32 magic; // =PFS_JOUNRNAL_MAGIC
93 u16 num; //
94 u16 checksum; //
95 struct{
96 u32 sector; // block/sector for partition
97 u16 sub; // main(0)/sub(+1) partition
98 u16 logSector; // block/sector offset in journal area
99 } log[127];
101
102// Attribute Entry
103typedef struct{
104 u8 kLen; // key len/used for offset in to str for value
105 u8 vLen; // value len
106 u16 aLen; // allocated length == ((kLen+vLen+7) & ~3)
107 char str[3]; // size = 3 so sizeof pfs_aentry_t=7 :P
109
110// Directory Entry
111typedef struct {
112 u32 inode;
113 u8 sub;
114 u8 pLen; // path length
115 u16 aLen; // allocated length == ((pLen+8+3) & ~3)
116 char path[512-8];
118
119// Block number/count pair (used in inodes)
120typedef struct {
121 u32 number; //
122 u16 subpart; //
123 u16 count; //
125
126// Date/time descriptor
127typedef struct {
128 u8 unused; //
129 u8 sec; //
130 u8 min; //
131 u8 hour; //
132 u8 day; //
133 u8 month; //
134 u16 year; //
136
137// Superblock structure
138typedef struct {
139 u32 magic; //
140 u32 version; //
141 u32 modver; //
142 u32 pfsFsckStat; //
143 u32 zone_size; //
144 u32 num_subs; // number of subs attached to filesystem
145 pfs_blockinfo_t log; // block info for metadata log
146 pfs_blockinfo_t root; // block info for root directory
148
149// Inode structure
150typedef struct {
151 u32 checksum; // Sum of all other words in the inode
152 u32 magic; //
153 pfs_blockinfo_t inode_block; // start block of inode
154 pfs_blockinfo_t next_segment; // next segment descriptor inode
155 pfs_blockinfo_t last_segment; // last segment descriptor inode
156 pfs_blockinfo_t unused; //
157 pfs_blockinfo_t data[PFS_INODE_MAX_BLOCKS]; //
158 u16 mode; // file mode
159 u16 attr; // file attributes
160 u16 uid; //
161 u16 gid; //
162 pfs_datetime_t atime; //
163 pfs_datetime_t ctime; //
164 pfs_datetime_t mtime; //
165 u64 size; //
166 u32 number_blocks; // number of blocks/zones used by file
167 u32 number_data; // number of used entries in data array
168 u32 number_segdesg; // number of "indirect blocks"/next segment descriptor's
169 u32 subpart; // subpart of inode
170 u32 reserved[4]; //
172
173typedef struct {
174 char *devName;
175 int (*transfer)(int fd, void *buffer, /*u16*/u32 sub, u32 sector, u32 size, u32 mode);
176 u32 (*getSubNumber)(int fd);
177 u32 (*getSize)(int fd, /*u16*/u32 sub/*0=main 1+=subs*/);
178 void (*setPartitionError)(int fd); // set open partition as having an error
179 int (*flushCache)(int fd);
181
182typedef struct {
183 pfs_block_device_t *blockDev; // call table for hdd(hddCallTable)
184 int fd; //
185 u32 flags; // rename to attr ones checked
186 u32 total_zones; // number of zones in the filesystem
187 u32 zfree; // zone free
188 u32 sector_scale; // Number of sectors within a zone
189 u32 inode_scale; // Number of inodes within a zone
190 u32 zsize; // zone size
191 u32 num_subs; // number of sub partitions in the filesystem
192 pfs_blockinfo_t root_dir; // block info for root directory
193 pfs_blockinfo_t log; // block info for the log
194 pfs_blockinfo_t current_dir; // block info for current directory
195 u32 lastError; // 0 if no error :)
196 u32 free_zone[65]; // free zones in each partition (1 main + 64 possible subs)
198
199typedef struct pfs_cache_s {
200 struct pfs_cache_s *next; //
201 struct pfs_cache_s *prev; //
202 u16 flags; //
203 u16 nused; //
204 pfs_mount_t *pfsMount; //
205 u32 sub; // main(0)/sub(+1) partition
206 u32 block; // block within for partition
207 union{ //
208 void *data;
209 pfs_inode_t *inode;
210 pfs_aentry_t *aentry;
211 pfs_dentry_t *dentry;
212 pfs_super_block_t *superblock;
213 u32 *bitmap;
214 } u;
215} pfs_cache_t;
216
217typedef struct
218{
219 pfs_cache_t *inode;
220 u32 block_segment; // index into data array in inode structure for current zone segment
221 u32 block_offset; // block offset from start of current zone segment
222 u32 byte_offset; // byte offset into current zone
224
226// Super Block functions
227
228#define PFS_SUPER_SECTOR 8192
229#define PFS_SUPER_BACKUP_SECTOR 8193
230
231extern int pfsCheckZoneSize(u32 zone_size);
232#ifdef PFS_SUPPORT_BHDD
233extern int pfsCheckExtendedZoneSize(u32 zone_size);
234#endif
235extern u32 pfsGetBitmapSizeSectors(int zoneScale, u32 partSize);
236extern u32 pfsGetBitmapSizeBlocks(int scale, u32 mainsize);
237extern int pfsFormatSub(pfs_block_device_t *blockDev, int fd, u32 sub, u32 reserved, u32 scale, u32 fragment);
238extern int pfsFormat(pfs_block_device_t *blockDev, int fd, int zonesize, int fragment);
239extern int pfsUpdateSuperBlock(pfs_mount_t *pfsMount, pfs_super_block_t *superblock, u32 sub);
240extern int pfsMountSuperBlock(pfs_mount_t *pfsMount);
241
243// Cache functions
244
245extern void pfsCacheFree(pfs_cache_t *clink);
246extern void pfsCacheLink(pfs_cache_t *clink, pfs_cache_t *cnew);
247extern pfs_cache_t *pfsCacheUnLink(pfs_cache_t *clink);
248extern pfs_cache_t *pfsCacheUsedAdd(pfs_cache_t *clink);
249extern int pfsCacheTransfer(pfs_cache_t* clink, int mode);
250extern void pfsCacheFlushAllDirty(pfs_mount_t *pfsMount);
251extern pfs_cache_t *pfsCacheAlloc(pfs_mount_t *pfsMount, u16 sub, u32 block, int flags, int *result);
252extern pfs_cache_t *pfsCacheGetData(pfs_mount_t *pfsMount, u16 sub, u32 block, int flags, int *result);
253extern pfs_cache_t *pfsCacheAllocClean(int *result);
254extern int pfsCacheIsFull(void);
255extern int pfsCacheInit(u32 numBuf, u32 bufSize);
256extern int pfsCacheDeinit(void);
257extern void pfsCacheClose(pfs_mount_t *pfsMount);
258extern void pfsCacheMarkClean(const pfs_mount_t *pfsMount, u32 subpart, u32 blockStart, u32 blockEnd);
259
261// Bitmap functions
262
263#define PFS_BITMAP_ALLOC 0
264#define PFS_BITMAP_FREE 1
265
266typedef struct
267{
268 u32 chunk;
269 u32 index;
270 u32 bit;
271 u32 partitionChunks;
272 u32 partitionRemainder;
274
275extern void pfsBitmapSetupInfo(pfs_mount_t *pfsMount, pfs_bitmapInfo_t *info, u32 subpart, u32 number);
276extern void pfsBitmapAllocFree(pfs_cache_t *clink, u32 operation, u32 subpart, u32 chunk, u32 index, u32 _bit, u32 count);
277extern int pfsBitmapAllocateAdditionalZones(pfs_mount_t *pfsMount, pfs_blockinfo_t *bi, u32 count);
278extern int pfsBitmapAllocZones(pfs_mount_t *pfsMount, pfs_blockinfo_t *bi, u32 amount);
279extern int pfsBitmapSearchFreeZone(pfs_mount_t *pfsMount, pfs_blockinfo_t *bi, u32 max_count);
280extern void pfsBitmapFreeBlockSegment(pfs_mount_t *pfsMount, pfs_blockinfo_t *bi);
281extern int pfsBitmapCalcFreeZones(pfs_mount_t *pfsMount, int sub);
282extern void pfsBitmapShow(pfs_mount_t *pfsMount);
283extern void pfsBitmapFreeInodeBlocks(pfs_cache_t *clink);
284
286// Block functions
287
288extern int pfsBlockSeekNextSegment(pfs_cache_t *clink, pfs_blockpos_t *blockpos);
289extern u32 pfsBlockSyncPos(pfs_blockpos_t *blockpos, u64 size);
290extern int pfsBlockInitPos(pfs_cache_t *clink, pfs_blockpos_t *blockpos, u64 position);
291extern int pfsBlockExpandSegment(pfs_cache_t *clink, pfs_blockpos_t *blockpos, u32 count);
292extern int pfsBlockAllocNewSegment(pfs_cache_t *clink, pfs_blockpos_t *blockpos, u32 blocks);
293extern pfs_blockinfo_t* pfsBlockGetCurrent(pfs_blockpos_t *blockpos);
294extern pfs_cache_t *pfsBlockGetNextSegment(pfs_cache_t *clink, int *result);
295extern pfs_cache_t *pfsBlockGetLastSegmentDescriptorInode(pfs_cache_t *clink, int *result);
296
298// Directory-Entry (DEntry) inode functions
299
300extern pfs_cache_t *pfsGetDentry(pfs_cache_t *clink, char *path, pfs_dentry_t **dentry, u32 *size, int option);
301extern int pfsGetNextDentry(pfs_cache_t *clink, pfs_blockpos_t *blockpos, u32 *position, char *name, pfs_blockinfo_t *bi);
302extern pfs_cache_t *pfsFillDentry(pfs_cache_t *clink, pfs_dentry_t *dentry, char *path1, pfs_blockinfo_t *bi, u32 len, u16 mode);
303extern pfs_cache_t *pfsDirAddEntry(pfs_cache_t *dir, char *filename, pfs_blockinfo_t *bi, u16 mode, int *result);
304extern pfs_cache_t *pfsDirRemoveEntry(pfs_cache_t *clink, char *path);
305extern int pfsCheckDirForFiles(pfs_cache_t *clink);
306extern void pfsFillSelfAndParentDentries(pfs_cache_t *clink, pfs_blockinfo_t *self, pfs_blockinfo_t *parent);
307extern pfs_cache_t* pfsSetDentryParent(pfs_cache_t *clink, pfs_blockinfo_t *bi, int *result);
308extern pfs_cache_t *pfsInodeGetFileInDir(pfs_cache_t *dirInode, char *path, int *result);
309extern pfs_cache_t *pfsInodeGetFile(pfs_mount_t *pfsMount, pfs_cache_t *clink, const char *name, int *result);
310extern void pfsInodeFill(pfs_cache_t *ci, pfs_blockinfo_t *bi, u16 mode, u16 uid, u16 gid);
311extern int pfsInodeRemove(pfs_cache_t *parent, pfs_cache_t *inode, char *path);
312extern pfs_cache_t *pfsInodeGetParent(pfs_mount_t *pfsMount, pfs_cache_t *clink, const char *filename, char *path, int *result);
313extern pfs_cache_t *pfsInodeCreate(pfs_cache_t *clink, u16 mode, u16 uid, u16 gid, int *result);
314extern int pfsCheckAccess(pfs_cache_t *clink, int flags);
315extern char* pfsSplitPath(char *filename, char *path, int *result);
316extern u16 pfsGetMaxIndex(pfs_mount_t *pfsMount);
317
318extern int pfsAllocZones(pfs_cache_t *clink, int msize, int mode);
319extern void pfsFreeZones(pfs_cache_t *pfree);
320
322// Inode functions
323
324extern void pfsInodePrint(pfs_inode_t *inode);
325extern int pfsInodeCheckSum(pfs_inode_t *inode);
326extern void pfsInodeSetTime(pfs_cache_t *clink);
327extern void pfsInodeSetTimeParent(pfs_cache_t *parent, pfs_cache_t *self);
328extern pfs_cache_t *pfsInodeGetData(pfs_mount_t *pfsMount, u16 sub, u32 inode, int *result);
329extern int pfsInodeSync(pfs_blockpos_t *blockpos, u64 size, u32 used_segments);
330extern pfs_cache_t *pfsGetDentriesChunk(pfs_blockpos_t *position, int *result);
331extern pfs_cache_t *pfsGetDentriesAtPos(pfs_cache_t *clink, u64 position, int *offset, int *result);
332
334// Journal functions
335
336extern int pfsJournalChecksum(void *header);
337extern void pfsJournalWrite(pfs_mount_t *pfsMount, pfs_cache_t *clink, u32 pfsCacheNumBuffers);
338extern int pfsJournalReset(pfs_mount_t *pfsMount);
339extern int pfsJournalFlush(pfs_mount_t *pfsMount);
340extern int pfsJournalRestore(pfs_mount_t *pfsMount);
341extern int pfsJournalResetThis(pfs_block_device_t *blockDev, int fd, u32 sector);
342
344// Function declerations
345
346extern int pfsFsckStat(pfs_mount_t *pfsMount, pfs_super_block_t *superblock, u32 stat, int mode);
347
348extern void *pfsAllocMem(int size);
349extern void pfsFreeMem(void *buffer);
350extern int pfsGetTime(pfs_datetime_t *tm);
351extern void pfsPrintBitmap(const u32 *bitmap);
352
353extern pfs_block_device_t *pfsGetBlockDeviceTable(const char *name);
354extern u32 pfsGetScale(u32 num, u32 size);
355extern u32 pfsFixIndex(u32 index);
356
357#endif /* _LIBPFS_H */
u32 count
start sector of fragmented bd/file