PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
mcman-internal.h
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright (c) 2009 jimmikaelkael
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#ifndef __MCMAN_INTERNAL_H__
12#define __MCMAN_INTERNAL_H__
13
14#ifndef MCMAN_ENABLE_EXTENDED_DEV_OPS
15#if defined(BUILDING_VMCMAN)
16#define MCMAN_ENABLE_EXTENDED_DEV_OPS 1
17#else
18#define MCMAN_ENABLE_EXTENDED_DEV_OPS 0
19#endif
20#endif
21
22#include <loadcore.h>
23#include <intrman.h>
24#include <sysclib.h>
25#include <thbase.h>
26#include <thsemap.h>
27#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
28#include <timrman.h>
29#include <modload.h>
30#include <secrman.h>
31#endif
32#if MCMAN_ENABLE_EXTENDED_DEV_OPS
33#include <iomanX.h>
34#else
35#include <ioman.h>
36#endif
37#ifdef _IOP
38#include <cdvdman.h>
39#else
40#include <time.h>
41#endif
42#include <stdio.h>
43#include <errno.h>
44#ifndef BUILDING_VMCMAN
45#ifdef BUILDING_XFROMMAN
46#include <fls.h>
47#else
48#ifdef BUILDING_XMCMAN
49#ifndef SIO2MAN_V2
50#include <xsio2man.h>
51#else
52#include <rsio2man.h>
53#endif
54#else
55#include <sio2man.h>
56#endif
57#endif
58#endif
59
60#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
61#define MODNAME "mcman_cex"
62#elif defined(BUILDING_VMCMAN)
63#define MODNAME "vmcman"
64#elif defined(BUILDING_XFROMMAN)
65#define MODNAME "xfromman"
66#endif
67#define MODVER 0x20b
68
69#ifndef MCMAN_ENTRYPOINT
70#ifdef _IOP
71#define MCMAN_ENTRYPOINT _start
72#else
73#define MCMAN_ENTRYPOINT mcman_start
74#endif
75#endif
76
77#ifdef SIO_DEBUG
78 #include <sior.h>
79 #define DEBUG
80 #define DPRINTF(format, args...) \
81 sio_printf(MODNAME ": " format, ##args)
82#else
83 #ifdef DEBUG
84 #define DPRINTF(format, args...) \
85 printf(MODNAME ": " format, ##args)
86 #else
87 #define DPRINTF(format, args...)
88 #endif
89#endif
90
91typedef struct _MCCacheDir {
92 int cluster; // 0
93 int fsindex; // 4
94 int maxent; // 8
95 u32 unused;
97
98// Card Flags
99#define CF_USE_ECC 0x01
100#define CF_BAD_BLOCK 0x08
101#define CF_ERASE_ZEROES 0x10
102
103#ifdef BUILDING_VMCMAN
104#define MCMAN_MAXSLOT 10
105#else
106#define MCMAN_MAXSLOT 4
107#endif
108#define MCMAN_CLUSTERSIZE 1024
109#define MCMAN_CLUSTERFATENTRIES 256
110
111typedef struct _McFatCluster {
112 int entry[MCMAN_CLUSTERFATENTRIES];
114
115#define MAX_CACHEENTRY 0x24
116
117typedef struct {
118 int entry[1 + (MCMAN_CLUSTERFATENTRIES * 2)];
119} McFatCache;
120
121#define MAX_CACHEDIRENTRY 0x3
122
123typedef struct { // size = 48
124 u8 status; // 0
125 u8 wrflag; // 1
126 u8 rdflag; // 2
127 u8 unknown1; // 3
128 u8 drdflag; // 4
129 u8 unknown2; // 5
130 u16 port; // 6
131 u16 slot; // 8
132 u16 unknown3; // 10
133 u32 position; // 12
134 u32 filesize; // 16
135 u32 freeclink; // 20 link to next free cluster
136 u32 clink; // 24 link to next cluster
137 u32 clust_offset;// 28
138 u32 cluster; // 32
139 u32 fsindex; // 36
140 u32 parent_cluster; // 40
141 u32 parent_fsindex; // 44
142} MC_FHANDLE;
143
144#define MAX_FDHANDLES 3
145
146#if MCMAN_ENABLE_EXTENDED_DEV_OPS
147#define MC_IO_DEV_T iomanX_iop_device_t
148#define MC_IO_FIL_T iomanX_iop_file_t
149#define MC_IO_DRE_T iox_dirent_t
150#define MC_IO_STA_T iox_stat_t
151#define MC_IO_OPS_T iomanX_iop_device_ops_t
152
153// FIXME: The r/w/x flag order is opposite from iomanX
154#define MC_IO_S_RD (FIO_S_IRUSR | FIO_S_IRGRP | FIO_S_IROTH)
155#define MC_IO_S_WR (FIO_S_IWUSR | FIO_S_IWGRP | FIO_S_IWOTH)
156#define MC_IO_S_EX (FIO_S_IXUSR | FIO_S_IXGRP | FIO_S_IXOTH)
157#define MC_IO_S_FL FIO_S_IFREG
158#define MC_IO_S_DR FIO_S_IFDIR
159
160#define MC_IO_CST_ATTR FIO_CST_ATTR
161#define MC_IO_CST_MODE FIO_CST_MODE
162#define MC_IO_CST_CT FIO_CST_CT
163#define MC_IO_CST_MT FIO_CST_MT
164#else
165#define MC_IO_DEV_T iop_device_t
166#define MC_IO_FIL_T iop_file_t
167#define MC_IO_DRE_T io_dirent_t
168#define MC_IO_STA_T io_stat_t
169#define MC_IO_OPS_T iop_device_ops_t
170
171#define MC_IO_S_RD SCE_STM_R
172#define MC_IO_S_WR SCE_STM_W
173#define MC_IO_S_EX SCE_STM_X
174#define MC_IO_S_FL SCE_STM_F
175#define MC_IO_S_DR SCE_STM_D
176
177#define MC_IO_CST_ATTR SCE_CST_ATTR
178#define MC_IO_CST_MODE SCE_CST_MODE
179#define MC_IO_CST_CT SCE_CST_CT
180#define MC_IO_CST_MT SCE_CST_MT
181#endif
182
183// internal functions prototypes
184#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
185int mcsio2_transfer(int port, int slot, sio2_transfer_data_t *sio2data);
186#endif
187void long_multiply(u32 v1, u32 v2, u32 *HI, u32 *LO);
188int mcman_chrpos(const char *str, int chr);
189void mcman_wmemset(void *buf, int size, int value);
190int mcman_calcEDC(void *buf, int size);
191int mcman_checkpath(const char *str);
192int mcman_checkdirpath(const char *str1, const char *str2);
193void mcman_invhandles(int port, int slot);
194int McCloseAll(void);
195int mcman_detectcard(int port, int slot);
196int mcman_dread(int fd, MC_IO_DRE_T *dirent);
197int mcman_getstat(int port, int slot, const char *filename, MC_IO_STA_T *stat);
198int mcman_getmcrtime(sceMcStDateTime *tm);
199void mcman_initPS2com(void);
200void mcman_deinitPS2com(void);
201#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
202void sio2packet_add(int port, int slot, int cmd, u8 *buf);
203#endif
204int mcman_eraseblock(int port, int slot, int block, void **pagebuf, void *eccbuf);
205int mcman_readpage(int port, int slot, int page, void *buf, void *eccbuf);
206int mcman_cardchanged(int port, int slot);
207int mcman_resetauth(int port, int slot);
208int mcman_probePS2Card2(int port, int slot);
209int mcman_probePS2Card(int port, int slot);
210#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
211int secrman_mc_command(int port, int slot, sio2_transfer_data_t *sio2data);
212#endif
213int mcman_getcnum (int port, int slot);
214int mcman_correctdata(void *buf, void *ecc);
215int mcman_sparesize(int port, int slot);
216int mcman_setdevspec(int port, int slot);
217int mcman_reportBadBlocks(int port, int slot);
218int mcman_setdevinfos(int port, int slot);
219int mcman_format2(int port, int slot);
220int mcman_fatRseek(int fd);
221int mcman_fatWseek(int fd);
222int mcman_findfree2(int port, int slot, int reserve);
223int mcman_dread2(int fd, MC_IO_DRE_T *dirent);
224int mcman_getstat2(int port, int slot, const char *filename, MC_IO_STA_T *stat);
225int mcman_setinfo2(int port, int slot, const char *filename, sceMcTblGetDir *info, int flags);
226int mcman_read2(int fd, void *buffer, int nbyte);
227int mcman_write2(int fd, void *buffer, int nbyte);
228int mcman_close2(int fd);
229int mcman_getentspace(int port, int slot, const char *dirname);
230int mcman_cachedirentry(int port, int slot, const char *filename, McCacheDir *pcacheDir, McFsEntry **pfse, int unknown_flag);
231int mcman_getdirinfo(int port, int slot, McFsEntry *pfse, const char *filename, McCacheDir *pcd, int unknown_flag);
232int mcman_open2(int port, int slot, const char *filename, int flags);
233int mcman_chdir(int port, int slot, const char *newdir, char *currentdir);
234int mcman_writecluster(int port, int slot, int cluster, int flag);
235int mcman_getdir2(int port, int slot, const char *dirname, int flags, int maxent, sceMcTblGetDir *info);
236int mcman_delete2(int port, int slot, const char *filename, int flags);
237int mcman_checkBackupBlocks(int port, int slot);
238int mcman_unformat2(int port, int slot);
239void mcman_initPS1PDAcom(void);
240void mcman_deinitPS1PDAcom(void);
241int mcman_probePS1Card2(int port, int slot);
242int mcman_probePS1Card(int port, int slot);
243int mcman_probePDACard(int port, int slot);
244int mcman_setPS1devinfos(int port, int slot);
245int mcman_format1(int port, int slot);
246int mcman_open1(int port, int slot, const char *filename, int flags);
247int mcman_read1(int fd, void *buffer, int nbyte);
248int mcman_write1(int fd, void *buffer, int nbyte);
249int mcman_getPS1direntry(int port, int slot, const char *filename, McFsEntryPS1 **pfse, int flag);
250int mcman_dread1(int fd, MC_IO_DRE_T *dirent);
251int mcman_getstat1(int port, int slot, const char *filename, MC_IO_STA_T *stat);
252int mcman_setinfo1(int port, int slot, const char *filename, sceMcTblGetDir *info, int flags);
253int mcman_getdir1(int port, int slot, const char *dirname, int flags, int maxent, sceMcTblGetDir *info);
254int mcman_clearPS1direntry(int port, int slot, int cluster, int flags);
255int mcman_delete1(int port, int slot, const char *filename, int flags);
256int mcman_close1(int fd);
257int mcman_findfree1(int port, int slot, int reserve);
258int mcman_fatRseekPS1(int fd);
259int mcman_fatWseekPS1(int fd);
260int mcman_FNC8ca4(int port, int slot, MC_FHANDLE *fh);
261int mcman_PS1pagetest(int port, int slot, int page);
262int mcman_unformat1(int port, int slot);
263int mcman_cachePS1dirs(int port, int slot);
264int mcman_fillPS1backuparea(int port, int slot, int block);
265void mcman_initcache(void);
266int mcman_clearcache(int port, int slot);
267McCacheEntry *mcman_getcacheentry(int port, int slot, int cluster);
268void mcman_freecluster(int port, int slot, int cluster);
269int mcman_getFATindex(int port, int slot, int num);
270McCacheEntry *mcman_get1stcacheEntp(void);
271void mcman_addcacheentry(McCacheEntry *mce);
272int mcman_flushcacheentry(McCacheEntry *mce);
273int mcman_readdirentryPS1(int port, int slot, int cluster, McFsEntryPS1 **pfse);
274int mcman_readclusterPS1(int port, int slot, int cluster, McCacheEntry **pmce);
275int mcman_replaceBackupBlock(int port, int slot, int block);
276int mcman_fillbackupblock1(int port, int slot, int block, void **pagedata, void *eccdata);
277int mcman_clearsuperblock(int port, int slot);
278int mcman_ioerrcode(int errcode);
279int mcman_modloadcb(const char *filename, int *port, int *slot); // used as callback by modload
280void mcman_unit2card(u32 unit);
281int mcman_initdev(void);
282void mcman_deinitdev(void);
283
284#if defined(BUILDING_VMCMAN)
285int mcman_iomanx_backing_mount(int port, int slot, const char *filename);
286int mcman_iomanx_backing_umount(int port, int slot);
287int mcman_iomanx_backing_getcardspec(int port, int slot, s16 *pagesize, u16 *blocksize, int *cardsize, u8 *flags);
288int mcman_iomanx_backing_erase(int port, int slot, int page);
289int mcman_iomanx_backing_write(int port, int slot, int page, void *pagebuf, void *eccbuf);
290int mcman_iomanx_backing_read(int port, int slot, int page, void *pagebuf, void *eccbuf);
291#endif
292
293typedef struct { // size = 384
294 char magic[28]; // Superblock magic, on PS2 MC : "Sony PS2 Memory Card Format "
295 u8 version[12]; // Version number of the format used, 1.2 indicates full support for bad_block_list
296 s16 pagesize; // size in bytes of a memory card page
297 u16 pages_per_cluster; // number of pages in a cluster
298 u16 blocksize; // number of pages in an erase block
299 u16 unused; // unused
300 u32 clusters_per_card; // total size in clusters of the memory card
301 u32 alloc_offset; // Cluster offset of the first allocatable cluster. Cluster values in the FAT and directory entries are relative to this. This is the cluster immediately after the FAT
302 u32 alloc_end; // The cluster after the highest allocatable cluster. Relative to alloc_offset. Not used
303 u32 rootdir_cluster; // First cluster of the root directory. Relative to alloc_offset. Must be zero
304 u32 backup_block1; // Erase block used as a backup area during programming. Normally the the last block on the card, it may have a different value if that block was found to be bad
305 u32 backup_block2; // This block should be erased to all ones. Normally the the second last block on the card
306 u8 unused2[8];
307 u32 ifc_list[32]; // List of indirect FAT clusters. On a standard 8M card there's only one indirect FAT cluster
308 int bad_block_list[32]; // List of erase blocks that have errors and shouldn't be used
309 u8 cardtype; // Memory card type. Must be 2, indicating that this is a PS2 memory card
310 u8 cardflags; // Physical characteristics of the memory card
311 u16 unused3;
312 u32 cluster_size;
313 u32 FATentries_per_cluster;
314 u32 clusters_per_block;
315 int cardform;
316 u32 rootdir_cluster2;
317 u32 unknown1;
318 u32 unknown2;
319 u32 max_allocatable_clusters;
320 u32 unknown3;
321 u32 unknown4;
322 int unknown5;
323} MCDevInfo;
324
326 u32 word[1056/sizeof(u32)];
327 u8 byte[1056/sizeof(u8)];
328 char magic[1056/sizeof(char)];
329};
330
332 u32 word[128/sizeof(u32)];
333 u16 half[128/sizeof(u16)];
334 u8 byte[128/sizeof(u8)];
335};
336
337// Defined in main.c
338
339extern char SUPERBLOCK_MAGIC[];
340extern char SUPERBLOCK_VERSION[];
341
342extern int mcman_wr_port;
343extern int mcman_wr_slot;
344extern int mcman_wr_block;
345extern int mcman_wr_flag3;
346extern int mcman_curdircluster;
347
348extern union mcman_pagebuf mcman_pagebuf;
350
351#ifndef BUILDING_XFROMMAN
352extern int timer_ID;
353#endif
354extern int PS1CardFlag;
355
356extern McFsEntry mcman_dircache[MAX_CACHEDIRENTRY];
357
358extern MC_FHANDLE mcman_fdhandles[MAX_FDHANDLES];
359extern MCDevInfo mcman_devinfos[4][MCMAN_MAXSLOT];
360
361extern u8 mcman_eccdata[512]; // size for 32 ecc
362
363// Defined in mcsio2.c
364extern u8 mcman_sio2outbufs_PS1PDA[0x90];
365
366#endif // __MCMAN_INTERNAL_H__
unsigned int version
Definition fileXio.h:3