mgh: fix for default HDD DMA mode, that wasn't correctly set
[open-ps2-loader.git] / modules / ps2fs / pfs.c
blobdc0c9f537dcf5ef05412afa3de30d75a222a5c77
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
10 # $Id: pfs.c 1491 2009-01-04 23:46:22Z oopo $
11 # PFS startup and misc code
14 #include "pfs.h"
16 IRX_ID("pfs_driver", 1, 1);
18 ///////////////////////////////////////////////////////////////////////////////
19 // Globals
21 iop_device_ops_t pfsOps = {
22 pfsInit,
23 pfsDeinit,
24 pfsFormat,
25 pfsOpen,
26 pfsClose,
27 pfsRead,
28 pfsWrite,
29 pfsLseek,
30 pfsIoctl,
31 pfsRemove,
32 pfsMkdir,
33 pfsRmdir,
34 pfsDopen,
35 pfsClose,
36 pfsDread,
37 pfsGetstat,
38 pfsChstat,
39 pfsRename,
40 pfsChdir,
41 pfsSync,
42 pfsMount,
43 pfsUmount,
44 pfsLseek64,
45 pfsDevctl,
46 pfsUnsupported /*pfsSymlink*/,
47 pfsUnsupported /*pfsReadlink*/,
48 pfsIoctl2
51 iop_device_t pfsFioDev = {
52 "pfs",
53 (IOP_DT_FS | IOP_DT_FSEXT),
55 "PFS",
56 &pfsOps
59 pfs_config_t pfsConfig = { 1, 2 };
60 pfs_mount_t *pfsMountBuf;
61 char *pfsFilename = NULL;
62 u32 metaSize = 1024; // size of each metadata structure
63 int pfsDebug = 0;
65 ///////////////////////////////////////////////////////////////////////////////
66 // Local function declerations
68 void printInfo();
69 int printPfsArgError();
70 int allocateMountBuffer(int size);
71 int allocateBuffers(u32 numBuf, u32 bufSize);
73 ///////////////////////////////////////////////////////////////////////////////
74 // Function defenitions
76 void printInfo()
78 printf("ps2fs: Playstation Filesystem Driver v%d.%d\nps2fs: (c) 2003 Sjeep, Vector and Florin Sasu\n", PFS_MAJOR, PFS_MINOR);
81 int printPfsArgError()
83 printf("ps2fs: ERROR: Usage: %s [-m <maxmount>] [-o <maxopen>] [-n <numbuffer>]\n", pfsFilename);
85 return MODULE_NO_RESIDENT_END;
88 int allocateMountBuffer(int size)
90 int tsize = size * sizeof(pfs_mount_t);
92 pfsMountBuf = allocMem(tsize);
93 if(!pfsMountBuf)
94 return -ENOMEM;
96 memset(pfsMountBuf, 0, tsize);
98 return 0;
102 void clearMount(pfs_mount_t *pfsMount)
104 memset(pfsMount, 0, sizeof(pfs_mount_t));
107 pfs_mount_t * getMountedUnit(u32 unit)
108 { // get mounted unit
109 if(unit>=pfsConfig.maxMount)
110 return NULL;
112 if(!(pfsMountBuf[unit].flags & MOUNT_BUSY))
113 return NULL;
115 return &pfsMountBuf[unit];
118 int _start(int argc, char *argv[])
120 char *filename;
121 int number;
122 int numBuf = 8;
123 int reqBuf;
124 int size;
126 printInfo();
128 // Get filename of IRX
129 filename = strrchr(argv[0], '/');
130 if(filename++)
131 pfsFilename = filename;
132 else
133 pfsFilename = argv[0];
135 argc--;
136 argv++;
138 // Parse arguments
139 while(argc > 0)
141 if(argv[0][0] != '-')
142 break;
144 if(!strcmp(argv[0], "-m"))
146 if(--argc <= 0)
147 return printPfsArgError();
148 argv++;
150 number = strtol(argv[0], 0, 10);
152 if(number <= 32)
153 pfsConfig.maxMount = number;
155 else if(!strcmp(argv[0], "-o"))
157 if(--argc <= 0)
158 return printPfsArgError();
159 argv++;
161 number = strtol(argv[0], 0, 10);
163 if(number <= 32)
164 pfsConfig.maxOpen = number;
166 else if(!strcmp(argv[0], "-n"))
168 if(--argc <= 0)
169 return printPfsArgError();
170 argv++;
172 number = strtol(argv[0], 0, 10);
174 if(number > numBuf)
175 numBuf = number;
177 if(numBuf > 127) {
178 printf("ps2fs: ERROR: Number of buffers is larger than 127!\n");
179 return -EINVAL;
182 else if(!strcmp(argv[0], "-debug"))
183 pfsDebug = 1;
184 else
185 return printPfsArgError();
187 argc--;
188 argv++;
191 printf("ps2fs: Max mount: %ld, Max open: %ld, Number of buffers: %d\n", pfsConfig.maxMount,
192 pfsConfig.maxOpen, numBuf);
194 // Do we have enough buffers ?
195 reqBuf = (pfsConfig.maxOpen * 2) + 8;
196 if(numBuf < reqBuf)
197 printf("ps2fs: Warning: %d buffers may be needed, but only %d buffers are allocated\n", reqBuf, numBuf);
199 if(allocateMountBuffer(pfsConfig.maxMount) < 0)
200 return MODULE_NO_RESIDENT_END;
202 // Allocate and zero memory for file slots
203 size = pfsConfig.maxOpen * sizeof(pfs_file_slot_t);
204 fileSlots = allocMem(size);
205 if(!fileSlots) {
206 printf("ps2fs: Error: Failed to allocate memory!\n");
207 return MODULE_NO_RESIDENT_END;
210 memset(fileSlots, 0, size);
212 if(cacheInit(numBuf, metaSize) < 0)
213 return MODULE_NO_RESIDENT_END;
215 DelDrv("pfs");
216 AddDrv(&pfsFioDev);
218 printf("ps2fs: Driver start.\n");
220 return MODULE_RESIDENT_END;