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