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: misc.c 1370 2007-01-17 02:28:14Z jbit $
11 # Miscellaneous routines
16 ///////////////////////////////////////////////////////////////////////////////
17 // Function defenitions
19 void *allocMem(int size
)
24 CpuSuspendIntr(&intrStat
);
25 mem
= AllocSysMemory(ALLOC_FIRST
, size
, NULL
);
26 CpuResumeIntr(intrStat
);
31 int getPs2Time(pfs_datetime
*tm
)
35 static pfs_datetime timeBuf
={
36 0, 0x0D, 0x0E, 0x0A, 0x0D, 1, 2003 // used if can not get time...
39 if(CdReadClock(&cdtime
)!=0 && cdtime
.stat
==0)
42 timeBuf
.sec
=(u32
)(((tmp
<<2)+tmp
)<<1)+(cdtime
.second
&0x0F);
44 timeBuf
.min
=(((tmp
<<2)+tmp
)<<1)+(cdtime
.minute
&0x0F);
46 timeBuf
.hour
=(((tmp
<<2)+tmp
)<<1)+(cdtime
.hour
&0x0F);
47 // timeBuf.hour = (timeBuf.hour + 4) % 24; // TEMP FIX (need to deal with timezones?) ... aparently not!
49 timeBuf
.day
=(((tmp
<<2)+tmp
)<<1)+(cdtime
.day
&0x0F);
51 timeBuf
.month
=(((tmp
<<2)+tmp
)<<1)+(cdtime
.month
&0x0F);
53 timeBuf
.year
=(((tmp
<<2)+tmp
)<<1)+(cdtime
.year
&0xF)+2000;
55 memcpy(tm
, &timeBuf
, sizeof(pfs_datetime
));
59 int fsckStat(pfs_mount_t
*pfsMount
, pfs_super_block
*superblock
,
61 { // mode 0=set flag, 1=remove flag, else check stat
63 if(pfsMount
->blockDev
->transfer(pfsMount
->fd
, superblock
, 0, PFS_BLOCKSIZE
, 1,
64 IOCTL2_TMODE_READ
)==0)
68 case MODE_SET_FLAG
: // set flag
69 superblock
->fsckStat
|=stat
;
71 case MODE_REMOVE_FLAG
: // remove flag
72 superblock
->fsckStat
&=~stat
;
74 default/*MODE_CHECK_FLAG*/:// check flag
75 return 0 < (superblock
->fsckStat
& stat
);
77 pfsMount
->blockDev
->transfer(pfsMount
->fd
, superblock
, 0, PFS_BLOCKSIZE
, 1,
79 pfsMount
->blockDev
->flushCache(pfsMount
->fd
);
84 void printBitmap(u32
*bitmap
) {
86 char a
[48+1], b
[16+1];
89 for (i
=0; i
< 32; i
++){
91 for (j
=0; j
< 16; j
++){
92 char *c
=(char*)bitmap
+j
+i
*16;
94 sprintf(a
+j
*3, "%02x ", *c
);
95 b
[j
] = ((*c
>=0) && (look_ctype_table(*c
) & 0x17)) ?
98 printf("%s%s\n", a
, b
);
102 int getScale(int num
, int size
)
106 while((size
<< scale
) != num
)
112 u32
fixIndex(u32 index
)
122 int checkAccess(pfs_cache_t
*clink
, int flags
)
126 // Bail if trying to write to read-only mount
127 if ((clink
->pfsMount
->flags
& FIO_MT_RDONLY
) && (flags
& O_WRONLY
))
130 if (((clink
->u
.inode
->mode
& FIO_S_IFMT
) != FIO_S_IFDIR
) &&
131 ((clink
->u
.inode
->mode
& 0111) == 0))
135 return ((mode
& flags
) & 7) == flags
? 0 : -EACCES
;
138 char* splitPath(char *filename
, char *path
, int *result
)
143 for (i
=0; filename
[i
] == '/'; i
++)
146 for (; i
<1024 && filename
[i
] != '/'; i
++){
147 if (filename
[i
] == 0) break;
149 path
[j
++] = filename
[i
];
155 while (filename
[i
] == '/')
163 *result
=-ENAMETOOLONG
;
167 u16
getMaxthIndex(pfs_mount_t
*pfsMount
)
169 u32 max
=0, maxI
=0, i
, v
;
171 for (i
=0; i
< pfsMount
->num_subs
+ 1; i
++) //enumerate all subs
173 v
= (pfsMount
->free_zone
[i
] * (u64
)100) /
174 (pfsMount
->blockDev
->getSize(pfsMount
->fd
, i
) >> pfsMount
->sector_scale
);
184 ///////////////////////////////////////////////////////////////////////////////
185 // Functions to work with hdd.irx
187 #define NUM_SUPPORTED_DEVICES 1
188 block_device deviceCallTable
[NUM_SUPPORTED_DEVICES
] = {
199 block_device
*getDeviceTable(const char *name
)
207 while(name
[0] == ' ')
210 end
= strchr(name
, ':');
212 printf("ps2fs: Error: Unknown block device '%s'\n", name
);
216 len
= (u32
)end
- (u32
)name
;
217 strncpy(devname
, name
, len
);
220 // Loop until digit is found, then terminate string at that digit.
221 // Should then have just the device name left, minus any front spaces or trailing digits.
223 while(!(look_ctype_table(tmp
[0]) & 0x04))
227 for(i
= 0; i
< NUM_SUPPORTED_DEVICES
; i
++)
228 if(!strcmp(deviceCallTable
[i
].devName
, devname
))
229 return &deviceCallTable
[i
];
234 int hddTransfer(int fd
, void *buffer
, u32 sub
/*0=main 1+=subs*/, u32 sector
,
235 u32 size
/* in sectors*/, u32 mode
)
237 hdd_ioctl2_transfer_t t
;
245 return ioctl2(fd
, APA_IOCTL2_TRANSFER_DATA
, &t
, 0, NULL
, 0);
248 u32
hddGetSubNumber(int fd
)
250 return ioctl2(fd
, APA_IOCTL2_NUMBER_OF_SUBS
, NULL
, 0, NULL
, 0);
253 u32
hddGetSize(int fd
, u32 sub
/*0=main 1+=subs*/)
255 return ioctl2(fd
, APA_IOCTL2_GETSIZE
, &sub
, 0, NULL
, 0);
258 void hddSetPartError(int fd
)
260 ioctl2(fd
, APA_IOCTL2_SET_PART_ERROR
, NULL
, 0, NULL
, 0);
263 int hddFlushCache(int fd
)
265 return ioctl2(fd
, APA_IOCTL2_FLUSH_CACHE
, NULL
, 0, NULL
, 0);