12#include "mcman-internal.h"
14static int mcman_curdirmaxent;
15static int mcman_curdirlength;
16static char mcman_curdirpath[1024];
17static const char *mcman_curdirname;
21int mcman_format2(
int port,
int slot)
23 register int r, i, size, ifc_index, indirect_offset, allocatable_clusters_per_card;
24 register int ifc_length, fat_length, fat_entry, alloc_offset;
25 register int j = 0, z = 0;
26 MCDevInfo *mcdi = &mcman_devinfos[port][slot];
29 DPRINTF(
"mcman_format2 port%d, slot%d cardform %d\n", port, slot, mcdi->cardform);
31 if (mcdi->cardform < 0) {
32 for (i = 0; i < 32; i++)
33 mcdi->bad_block_list[i] = -1;
34 mcdi->rootdir_cluster = 0;
35 mcdi->rootdir_cluster2 = 0;
39 if (mcdi->cardform > 0) {
40 if (((mcdi->version[0] - 48) >= 2) || ((mcdi->version[2] - 48) >= 2))
44 r = mcman_reportBadBlocks(port, slot);
45 if ((r != sceMcResSucceed) && (r != sceMcResNoFormat))
46 return sceMcResChangedCard;
50 memset((
void *)&mcdi->magic, 0, sizeof (mcdi->magic) + sizeof (mcdi->version));
51 strcpy(mcdi->magic, SUPERBLOCK_MAGIC);
52 strcat(mcdi->magic, SUPERBLOCK_VERSION);
55 size = mcdi->blocksize;
65 if (mcdi->blocksize <= 0)
69 for (i = 0; i < size; i++) {
70 r = mcman_writecluster(port, slot, i, 1);
72 return sceMcResNoFormat;
78 fat_length = (((mcdi->clusters_per_card << 2) - 1) / mcdi->cluster_size) + 1;
79 ifc_length = (((fat_length << 2) - 1) / mcdi->cluster_size) + 1;
81 if (!(ifc_length <= 32)) {
83 fat_length = mcdi->FATentries_per_cluster << 5;
90 if ((u32)i >= mcdi->clusters_per_card)
91 return sceMcResNoFormat;
94 if (mcman_writecluster(port, slot, i, 1) != 0)
98 }
while ((u32)i < mcdi->clusters_per_card);
100 if ((u32)i >= mcdi->clusters_per_card)
101 return sceMcResNoFormat;
103 mcdi->ifc_list[j] = i;
106 }
while (j < ifc_length);
110 if (fat_length > 0) {
114 ifc_index = j / mcdi->FATentries_per_cluster;
115 indirect_offset = j % mcdi->FATentries_per_cluster;
117 if (indirect_offset == 0) {
118 if (McReadCluster(port, slot, mcdi->ifc_list[ifc_index], &mce) != sceMcResSucceed)
123 if ((u32)i >= mcdi->clusters_per_card)
124 return sceMcResNoFormat;
127 r = mcman_writecluster(port, slot, i, 1);
135 }
while ((u32)i < mcdi->clusters_per_card);
137 if ((u32)i >= mcdi->clusters_per_card)
138 return sceMcResNoFormat;
142 fc->entry[indirect_offset] = i;
145 }
while (j < fat_length);
149 mcdi->backup_block1 = 0;
150 mcdi->backup_block2 = 0;
153 for (i = (mcdi->clusters_per_card / mcdi->clusters_per_block) - 1; i > 0; i--) {
155 r = mcman_writecluster(port, slot, mcdi->clusters_per_block * i, 1);
159 if ((r != 0) && (mcdi->backup_block1 == 0))
160 mcdi->backup_block1 = i;
161 else if ((r != 0) && (mcdi->backup_block2 == 0)) {
162 mcdi->backup_block2 = i;
168 if (mcman_eraseblock(port, slot, mcdi->backup_block2, NULL, NULL) != sceMcResSucceed)
173 long_multiply(mcdi->clusters_per_card, 0x10624dd3, &hi, &lo);
174 temp = (hi >> 6) - (mcdi->clusters_per_card >> 31);
175 allocatable_clusters_per_card = (((((temp << 5) - temp) << 2) + temp) << 3) + 1;
179 if ((u32)j < i * mcdi->clusters_per_block) {
182 r = mcman_writecluster(port, slot, j, 0);
185 mcdi->alloc_offset = j;
186 mcdi->rootdir_cluster = 0;
187 fat_entry = 0xffffffff;
190 fat_entry = ~0x80000000;
192 if (z == allocatable_clusters_per_card)
193 mcdi->max_allocatable_clusters = (j - mcdi->alloc_offset) + 1;
198 fat_entry = 0xfffffffd;
201 if (McSetFATentry(port, slot, j - mcdi->alloc_offset, fat_entry) != sceMcResSucceed)
205 }
while ((u32)j < (i * mcdi->clusters_per_block));
208 mcdi->alloc_end = (i * mcdi->clusters_per_block) - mcdi->alloc_offset;
210 if (mcdi->max_allocatable_clusters == 0)
211 mcdi->max_allocatable_clusters = i * mcdi->clusters_per_block;
213 if ((u32)z < mcdi->clusters_per_block)
214 return sceMcResNoFormat;
217 for (i = 0; (
unsigned int)i <
sizeof (
MCDevInfo); i += MCMAN_CLUSTERSIZE) {
219 size = i + (MCMAN_CLUSTERSIZE - 1);
223 if (McReadCluster(port, slot, size >> 10, &mce) != sceMcResSucceed)
226 size = MCMAN_CLUSTERSIZE;
232 memcpy((
void *)mce->cl_data, (
void *)(mcdi + i), size);
238 mcdi->rootdir_cluster2 = mcdi->rootdir_cluster;
241 if (McCreateDirentry(port, slot, 0, 0, 0, NULL) != sceMcResSucceed)
245 r = McFlushCache(port, slot);
246 if (r != sceMcResSucceed)
251 return sceMcResSucceed;
255int mcman_dread2(
int fd, MC_IO_DRE_T *dirent)
260 DPRINTF(
"mcman_dread2 fd %d\n", fd);
262 if (fh->position >= fh->filesize)
263 return sceMcResSucceed;
268 r = McReadDirEntry(fh->port, fh->slot, fh->freeclink, fh->position, &fse);
269 if (r != sceMcResSucceed)
272 if (fse->mode & sceMcFileAttrExists)
276 }
while (fh->position < fh->filesize);
278 if (fh->position >= fh->filesize)
279 return sceMcResSucceed;
282 mcman_wmemset((
void *)dirent,
sizeof (MC_IO_DRE_T), 0);
283 strcpy(dirent->name, fse->name);
284 *(u8 *)&dirent->name[32] = 0;
286 if (fse->mode & sceMcFileAttrReadable)
287 dirent->stat.mode |= MC_IO_S_RD;
288 if (fse->mode & sceMcFileAttrWriteable)
289 dirent->stat.mode |= MC_IO_S_WR;
290 if (fse->mode & sceMcFileAttrExecutable)
291 dirent->stat.mode |= MC_IO_S_EX;
292#if !MCMAN_ENABLE_EXTENDED_DEV_OPS
293 if (fse->mode & sceMcFileAttrPS1)
294 dirent->stat.mode |= sceMcFileAttrPS1;
295 if (fse->mode & sceMcFileAttrPDAExec)
296 dirent->stat.mode |= sceMcFileAttrPDAExec;
297 if (fse->mode & sceMcFileAttrDupProhibit)
298 dirent->stat.mode |= sceMcFileAttrDupProhibit;
300 if (fse->mode & sceMcFileAttrSubdir)
301 dirent->stat.mode |= MC_IO_S_DR;
303 dirent->stat.mode |= MC_IO_S_FL;
305 dirent->stat.attr = fse->attr;
306 dirent->stat.size = fse->length;
314int mcman_getstat2(
int port,
int slot,
const char *filename, MC_IO_STA_T *stat)
319 DPRINTF(
"mcman_getstat2 port%d slot%d filename %s\n", port, slot, filename);
321 r = mcman_cachedirentry(port, slot, filename, NULL, &fse, 1);
322 if (r != sceMcResSucceed)
325 mcman_wmemset((
void *)stat,
sizeof (MC_IO_STA_T), 0);
327 if (fse->mode & sceMcFileAttrReadable)
328 stat->mode |= MC_IO_S_RD;
329 if (fse->mode & sceMcFileAttrWriteable)
330 stat->mode |= MC_IO_S_WR;
331 if (fse->mode & sceMcFileAttrExecutable)
332 stat->mode |= MC_IO_S_EX;
333#if !MCMAN_ENABLE_EXTENDED_DEV_OPS
334 if (fse->mode & sceMcFileAttrPS1)
335 stat->mode |= sceMcFileAttrPS1;
336 if (fse->mode & sceMcFileAttrPDAExec)
337 stat->mode |= sceMcFileAttrPDAExec;
338 if (fse->mode & sceMcFileAttrDupProhibit)
339 stat->mode |= sceMcFileAttrDupProhibit;
341 if (fse->mode & sceMcFileAttrSubdir)
342 stat->mode |= MC_IO_S_DR;
344 stat->mode |= MC_IO_S_FL;
346 stat->attr = fse->attr;
348 if (!(fse->mode & sceMcFileAttrSubdir))
349 stat->size = fse->length;
354 return sceMcResSucceed;
358int mcman_setinfo2(
int port,
int slot,
const char *filename,
sceMcTblGetDir *
info,
int flags)
360 register int r, fmode;
365 DPRINTF(
"mcman_setinfo2 port%d slot%d filename %s flags %x\n", port, slot, filename, flags);
367 r = mcman_cachedirentry(port, slot, filename, &dirInfo, &fse, 1);
368 if (r != sceMcResSucceed)
371 if ((flags & sceMcFileAttrFile) != 0) {
372 u8 *pfsentry, *pfseend, *mfee;
374 if ((!strcmp(
".",
info->EntryName)) || (!strcmp(
"..",
info->EntryName)))
375 return sceMcResNoEntry;
377 if (
info->EntryName[0] == 0)
378 return sceMcResNoEntry;
380 r = mcman_chrpos(
info->EntryName,
'/');
382 return sceMcResNoEntry;
384 if (dirInfo.fsindex < 2)
385 return sceMcResNoEntry;
387 r = McReadDirEntry(port, slot, dirInfo.cluster, 0, &fse);
388 if (r != sceMcResSucceed)
391 r = McReadDirEntry(port, slot, fse->cluster, fse->dir_entry, &fse);
392 if (r != sceMcResSucceed)
395 pfsentry = (u8 *)fse;
397 pfseend = (u8 *)(pfsentry +
sizeof (
McFsEntry));
400 *((u32 *)mfee ) = *((u32 *)pfsentry );
401 *((u32 *)mfee+1) = *((u32 *)pfsentry+1);
402 *((u32 *)mfee+2) = *((u32 *)pfsentry+2);
403 *((u32 *)mfee+3) = *((u32 *)pfsentry+3);
406 }
while (pfsentry < pfseend);
408 r = mcman_getdirinfo(port, slot, &mfe,
info->EntryName, NULL, 1);
412 return sceMcResNoEntry;
418 return sceMcResDeniedPermit;
423 r = McReadDirEntry(port, slot, dirInfo.cluster, dirInfo.fsindex, &fse);
424 if (r != sceMcResSucceed)
427 Mc1stCacheEntSetWrFlagOff();
434 fse->mode =
info->AttrFile;
437 flags = sceMcFileAttrReadable|sceMcFileAttrWriteable;
441 if ((flags & sceMcFileAttrDupProhibit) != 0)
442 fse->attr =
info->Reserve2;
444 if ((flags & sceMcFileAttrExecutable) != 0) {
448 fse->mode = (fse->mode & ~fmode) | (
info->AttrFile & fmode);
451 if ((flags & sceMcFileCreateFile) != 0) {
455 fse->mode = (fse->mode & ~fmode) | (
info->AttrFile & fmode);
458 if ((flags & sceMcFileAttrReadable) != 0)
459 fse->created =
info->_Create;
461 if ((flags & sceMcFileAttrWriteable) != 0)
462 fse->modified =
info->_Modify;
464 if ((flags & sceMcFileAttrFile) != 0) {
465 strncpy(fse->name,
info->EntryName, 32);
469 return sceMcResSucceed;
473int mcman_read2(
int fd,
void *buffer,
int nbyte)
476 register MCDevInfo *mcdi = &mcman_devinfos[fh->port][fh->slot];
479 DPRINTF(
"mcman_read2 fd %d buf %x size %d\n", fd, (
int)buffer, nbyte);
481 if (fh->position < fh->filesize) {
482 register int temp, rpos;
484 temp = fh->filesize - fh->position;
492 register int r, size, offset;
494 offset = fh->position % mcdi->cluster_size;
495 temp = mcdi->cluster_size - offset;
501 r = mcman_fatRseek(fd);
506 r = McReadCluster(fh->port, fh->slot, r, &mce);
507 if (r != sceMcResSucceed)
510 memcpy((
void *)((u8 *)buffer + rpos), (
void *)((u8 *)(mce->cl_data) + offset), size);
515 fh->position += size;
526int mcman_write2(
int fd,
void *buffer,
int nbyte)
528 register int r, wpos;
530 register MCDevInfo *mcdi = &mcman_devinfos[fh->port][fh->slot];
534 if (fh->unknown2 == 0) {
536 r = mcman_close2(fd);
537 if (r != sceMcResSucceed)
539 r = McFlushCache(fh->port, fh->slot);
540 if (r != sceMcResSucceed)
548 register int r2, size, offset;
550 r = mcman_fatRseek(fd);
551 if (r == sceMcResFullDevice) {
553 r2 = mcman_fatWseek(fd);
555 return sceMcResFullDevice;
557 if (r2 != sceMcResSucceed)
560 r = mcman_fatRseek(fd);
567 r = McReadCluster(fh->port, fh->slot, r, &mce);
568 if (r != sceMcResSucceed)
573 offset = fh->position % mcdi->cluster_size;
574 r2 = mcdi->cluster_size - offset;
580 memcpy((
void *)((u8 *)(mce->cl_data) + offset), (
void *)((u8 *)buffer + wpos), size);
584 r = fh->position + size;
585 fh->position += size;
587 if ((u32)r < fh->filesize)
598 r = mcman_close2(fd);
599 if (r != sceMcResSucceed)
606int mcman_close2(
int fd)
608 register int r, fmode;
612 DPRINTF(
"mcman_close2 fd %d\n", fd);
614 r = McReadDirEntry(fh->port, fh->slot, fh->cluster, fh->fsindex, &fse1);
615 if (r != sceMcResSucceed)
618 if (fh->unknown2 == 0) {
619 fmode = fse1->mode | sceMcFileAttrClosed;
622 fmode = fse1->mode & 0xff7f;
626 mcman_getmcrtime(&fse1->modified);
628 fse1->cluster = fh->freeclink;
629 fse1->length = fh->filesize;
631 Mc1stCacheEntSetWrFlagOff();
633 mcman_fsmodtime = fse1->modified;
635 r = McReadDirEntry(fh->port, fh->slot, fh->parent_cluster, fh->parent_fsindex, &fse2);
636 if (r != sceMcResSucceed)
639 fse2->modified = mcman_fsmodtime;
641 Mc1stCacheEntSetWrFlagOff();
643 return sceMcResSucceed;
647int mcman_open2(
int port,
int slot,
const char *filename,
int flags)
649 register int fd, i, r, rdflag, wrflag, pos, mcfree;
655 u8 *pfsentry, *pcache, *pfseend;
659 DPRINTF(
"mcman_open2 port%d slot%d name %s flags %x\n", port, slot, filename, flags);
661 if ((flags & sceMcFileCreateFile) != 0)
662 flags |= sceMcFileAttrWriteable;
666 if (filename[0] == 0)
667 return sceMcResNoEntry;
674 }
while (++fd < MAX_FDHANDLES);
676 if (fd == MAX_FDHANDLES)
677 return sceMcResUpLimitHandle;
681 mcman_wmemset((
void *)fh,
sizeof (
MC_FHANDLE), 0);
683 mcdi = (
MCDevInfo *)&mcman_devinfos[port][slot];
685 if ((flags & (sceMcFileCreateFile | sceMcFileCreateDir)) == 0)
686 cacheDir.maxent = -1;
694 r = mcman_cachedirentry(port, slot, filename, &cacheDir, &fse1, 1);
699 pfsentry = (u8 *)fse1;
700 pcache = (u8 *)&mcman_dircache[1];
701 pfseend = (u8 *)(pfsentry +
sizeof(
McFsEntry));
704 *((u32 *)pcache ) = *((u32 *)pfsentry );
705 *((u32 *)pcache+1) = *((u32 *)pfsentry+1);
706 *((u32 *)pcache+2) = *((u32 *)pfsentry+2);
707 *((u32 *)pcache+3) = *((u32 *)pfsentry+3);
710 }
while (pfsentry < pfseend);
712 if ((flags == 0) && ((fse1->mode & sceMcFileAttrExists) == 0))
717 return sceMcResNoEntry;
720 return sceMcResDeniedPermit;
722 if ((r == 0) && ((flags & sceMcFileCreateDir) != 0))
723 return sceMcResNoEntry;
725 if ((r == 1) && ((flags & (sceMcFileCreateFile | sceMcFileCreateDir)) == 0))
726 return sceMcResNoEntry;
728 rdflag = flags & sceMcFileAttrReadable;
729 wrflag = flags & sceMcFileAttrWriteable;
732 fh->clust_offset = 0;
741 fh->cluster = cacheDir.cluster;
742 fh->fsindex = cacheDir.fsindex;
748 if ((wrflag != 0) && ((mcman_dircache[1].mode & sceMcFileAttrWriteable) == 0))
749 return sceMcResDeniedPermit;
752 if ((flags & sceMcFileAttrReadable) != 0) {
753 if ((mcman_dircache[1].mode & sceMcFileAttrReadable) == 0)
754 return sceMcResDeniedPermit;
757 r = McReadDirEntry(port, slot, cacheDir.cluster, 0, &fse2);
758 if (r != sceMcResSucceed)
761 fh->parent_cluster = fse2->cluster;
762 fh->parent_fsindex = fse2->dir_entry;
764 if ((mcman_dircache[1].mode & sceMcFileAttrSubdir) != 0) {
765 if ((mcman_dircache[1].mode & sceMcFileAttrReadable) == 0)
766 return sceMcResDeniedPermit;
768 fh->freeclink = mcman_dircache[1].cluster;
774 fh->filesize = mcman_dircache[1].length;
775 fh->clink = fh->freeclink;
780 if ((flags & sceMcFileAttrWriteable) != 0) {
787 if ((fh2->status == 0) || (fh2->port != port) || (fh2->slot != slot) \
788 || (fh2->cluster != (u32)(cacheDir.cluster)) || (fh2->fsindex != (u32)(cacheDir.fsindex)))
791 if (fh2->wrflag != 0)
792 return sceMcResDeniedPermit;
794 }
while (++i < MAX_FDHANDLES);
797 if ((flags & sceMcFileCreateFile) != 0) {
798 r = McSetDirEntryState(port, slot, cacheDir.cluster, cacheDir.fsindex, 0);
799 McFlushCache(port, slot);
801 if (r != sceMcResSucceed)
804 if (cacheDir.fsindex < cacheDir.maxent)
805 cacheDir.maxent = cacheDir.fsindex;
808 fh->freeclink = mcman_dircache[1].cluster;
809 fh->filesize = mcman_dircache[1].length;
810 fh->clink = fh->freeclink;
813 fh->rdflag = (*((u8 *)&mcman_dircache[1].mode)) & sceMcFileAttrReadable;
818 fh->wrflag = (mcman_dircache[1].mode >> 1) & sceMcFileAttrReadable;
828 fh->parent_cluster = cacheDir.cluster;
829 fh->parent_fsindex = cacheDir.fsindex;
832 r = McReadDirEntry(port, slot, fh->parent_cluster, fh->parent_fsindex, &fse1);
833 if (r != sceMcResSucceed)
836 pfsentry = (u8 *)fse1;
837 pcache = (u8 *)&mcman_dircache[2];
838 pfseend = (u8 *)(pfsentry +
sizeof(
McFsEntry));
841 *((u32 *)pcache ) = *((u32 *)pfsentry );
842 *((u32 *)pcache+1) = *((u32 *)pfsentry+1);
843 *((u32 *)pcache+2) = *((u32 *)pfsentry+2);
844 *((u32 *)pcache+3) = *((u32 *)pfsentry+3);
847 }
while (pfsentry < pfseend);
850 if (mcman_dircache[2].length == (u32)(cacheDir.maxent)) {
851 register int fsindex, fsoffset;
853 fsindex = mcman_dircache[2].length / (mcdi->cluster_size >> 9);
854 fsoffset = mcman_dircache[2].length % (mcdi->cluster_size >> 9);
857 register int fat_index;
859 fat_index = mcman_dircache[2].cluster;
862 if ((mcman_dircache[2].cluster == 0) && (i >= 2)) {
863 if (mcman_getFATindex(port, slot, i - 1) >= 0) {
864 fat_index = mcman_getFATindex(port, slot, i - 1);
873 r = McGetFATentry(port, slot, fat_index, &fat_entry);
874 if (r != sceMcResSucceed)
877 if (fat_entry >= -1) {
878 r = mcman_findfree2(port, slot, 1);
883 mce = mcman_get1stcacheEntp();
885 fat_entry |= 0x80000000;
887 r = McSetFATentry(port, slot, fat_index, fat_entry);
888 if (r != sceMcResSucceed)
891 mcman_addcacheentry(mce);
894 fat_index = fat_entry & ~0x80000000;
900 r = McFlushCache(port, slot);
901 if (r != sceMcResSucceed)
906 mcman_dircache[2].length++;
910 p = filename + i + 1;
912 r = mcman_chrpos(p,
'/');
922 if ((flags & sceMcFileCreateDir) != 0) {
923 r = mcman_findfree2(port, slot, 1);
929 mce = mcman_get1stcacheEntp();
931 mcman_getmcrtime(&mcman_dircache[2].modified);
933 r = McReadDirEntry(port, slot, mcman_dircache[2].cluster, cacheDir.maxent, &fse2);
934 if (r != sceMcResSucceed)
937 mcman_wmemset((
void *)fse2,
sizeof (
McFsEntry), 0);
939 strncpy(fse2->name, p, 32);
941 fse2->created = mcman_dircache[2].modified;
942 fse2->modified = mcman_dircache[2].modified;
944 Mc1stCacheEntSetWrFlagOff();
946 mcman_addcacheentry(mce);
951 if ((flags & sceMcFileCreateDir) != 0) {
953 fse2->mode = ((flags & sceMcFileAttrHidden) | sceMcFileAttrReadable | sceMcFileAttrWriteable \
954 | sceMcFileAttrExecutable | sceMcFileAttrSubdir | sceMcFile0400 | sceMcFileAttrExists)
955 | (flags & (sceMcFileAttrPS1 | sceMcFileAttrPDAExec));
957 fse2->cluster = mcfree;
959 r = McCreateDirentry(port, slot, mcman_dircache[2].cluster, cacheDir.maxent, mcfree, (
sceMcStDateTime *)&fse2->created);
960 if (r != sceMcResSucceed)
963 r = McReadDirEntry(port, slot, fh->parent_cluster, fh->parent_fsindex, &fse1);
964 if (r != sceMcResSucceed)
967 pfsentry = (u8 *)fse1;
968 pcache = (u8 *)&mcman_dircache[2];
969 pfseend = (u8 *)(pfsentry +
sizeof(
McFsEntry));
972 *((u32 *)pfsentry ) = *((u32 *)pcache );
973 *((u32 *)pfsentry+1) = *((u32 *)pcache+1);
974 *((u32 *)pfsentry+2) = *((u32 *)pcache+2);
975 *((u32 *)pfsentry+3) = *((u32 *)pcache+3);
978 }
while (pfsentry < pfseend);
980 Mc1stCacheEntSetWrFlagOff();
982 r = McFlushCache(port, slot);
983 if (r != sceMcResSucceed)
986 return sceMcResSucceed;
989 fse2->mode = ((flags & sceMcFileAttrHidden) | sceMcFileAttrReadable | sceMcFileAttrWriteable \
990 | sceMcFileAttrExecutable | sceMcFileAttrFile | sceMcFile0400 | sceMcFileAttrExists)
991 | (flags & (sceMcFileAttrPS1 | sceMcFileAttrPDAExec));
994 fh->cluster = mcman_dircache[2].cluster;
996 fh->fsindex = cacheDir.maxent;
998 r = McReadDirEntry(port, slot, fh->parent_cluster, fh->parent_fsindex, &fse1);
999 if (r != sceMcResSucceed)
1002 pfsentry = (u8 *)fse1;
1003 pcache = (u8 *)&mcman_dircache[2];
1004 pfseend = (u8 *)(pfsentry +
sizeof(
McFsEntry));
1007 *((u32 *)pfsentry ) = *((u32 *)pcache );
1008 *((u32 *)pfsentry+1) = *((u32 *)pcache+1);
1009 *((u32 *)pfsentry+2) = *((u32 *)pcache+2);
1010 *((u32 *)pfsentry+3) = *((u32 *)pcache+3);
1013 }
while (pfsentry < pfseend);
1015 Mc1stCacheEntSetWrFlagOff();
1017 r = McFlushCache(port, slot);
1018 if (r != sceMcResSucceed)
1026int mcman_chdir(
int port,
int slot,
const char *newdir,
char *currentdir)
1028 register int r, len, len2, cluster;
1029 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
1033 DPRINTF(
"mcman_chdir port%d slot%d newdir %s\n", port, slot, newdir);
1038 cacheDir.maxent = -1;
1040 r = mcman_cachedirentry(port, slot, newdir, &cacheDir, &fse, 1);
1044 if (((u32)(r - 1)) < 2)
1045 return sceMcResNoEntry;
1047 mcdi->rootdir_cluster2 = cacheDir.cluster;
1048 mcdi->unknown1 = cacheDir.fsindex;
1050 cluster = cacheDir.cluster;
1051 if (!strcmp(fse->name,
"..")) {
1052 r = McReadDirEntry(port, slot, cluster, 0, &fse);
1053 if (r != sceMcResSucceed)
1057 if (!strcmp(fse->name,
".")) {
1058 mcdi->rootdir_cluster2 = fse->cluster;
1059 mcdi->unknown1 = fse->dir_entry;
1061 cluster = fse->cluster;
1062 r = McReadDirEntry(port, slot, cluster, fse->dir_entry, &fse);
1063 if (r != sceMcResSucceed)
1070 if (strcmp(fse->name,
".")) {
1072 if (strlen(fse->name) < 32)
1073 len = strlen(fse->name);
1077 if (strlen(currentdir)) {
1078 len2 = strlen(currentdir);
1081 currentdir[1 + len2 + len] = currentdir[len2];
1082 }
while (--len2 >= 0);
1084 currentdir[len] =
'/';
1085 strncpy(currentdir, fse->name, len);
1088 strncpy(currentdir, fse->name, 32);
1092 r = McReadDirEntry(port, slot, cluster, 0, &fse);
1093 if (r != sceMcResSucceed)
1096 r = McReadDirEntry(port, slot, fse->cluster, fse->dir_entry, &fse);
1098 if (r == sceMcResSucceed)
1102 len = strlen(currentdir);
1106 currentdir[1 + len] = currentdir[len];
1107 }
while (--len >= 0);
1109 currentdir[0] =
'/';
1111 r = sceMcResSucceed;
1118int mcman_getdir2(
int port,
int slot,
const char *dirname,
int flags,
int maxent,
sceMcTblGetDir *
info)
1120 register int r, nument;
1121 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
1125 DPRINTF(
"mcman_getdir2 port%d slot%d dir=%s flags=%d maxent=%d\n", port, slot, dirname, flags, maxent);
1133 p = mcman_curdirpath;
1134 strncpy(p, dirname, 1023);
1135 mcman_curdirpath[1023] = 0;
1140 r = mcman_chrpos((
void *)&p[pos],
'/');
1153 mcman_curdirpath[pos] = 0;
1156 mcman_curdirname = &dirname[pos] + 1;
1158 r = mcman_cachedirentry(port, slot, mcman_curdirpath, NULL, &fse, 1);
1160 return sceMcResNoEntry;
1164 if (!(fse->mode & sceMcFileAttrSubdir)) {
1165 mcman_curdircluster = -1;
1166 return sceMcResNoEntry;
1169 mcman_curdircluster = fse->cluster;
1170 mcman_curdirlength = fse->length;
1172 if ((fse->cluster == mcdi->rootdir_cluster) && (fse->dir_entry == 0))
1173 mcman_curdirmaxent = 2;
1175 mcman_curdirmaxent = 0;
1178 if (mcman_curdircluster < 0)
1179 return sceMcResNoEntry;
1184 if (mcman_curdirmaxent >= mcman_curdirlength)
1187 r = McReadDirEntry(port, slot, mcman_curdircluster, mcman_curdirmaxent, &fse);
1188 if (r != sceMcResSucceed)
1191 mcman_curdirmaxent++;
1193 if (!(fse->mode & sceMcFileAttrExists))
1195 if ((fse->mode & sceMcFileAttrHidden) && (!PS1CardFlag))
1197 if (!mcman_checkdirpath(fse->name, mcman_curdirname))
1202 if (mcman_curdirmaxent == 2) {
1204 r = McReadDirEntry(port, slot, mcman_curdircluster, 0, &fse);
1205 if (r != sceMcResSucceed)
1208 r = McReadDirEntry(port, slot, fse->cluster, 0, &fse);
1209 if (r != sceMcResSucceed)
1212 r = McReadDirEntry(port, slot, fse->cluster, fse->dir_entry, &fse);
1213 if (r != sceMcResSucceed)
1216 info->EntryName[0] =
'.';
1217 info->EntryName[1] =
'.';
1218 info->EntryName[2] =
'\0';
1220 else if (mcman_curdirmaxent == 1) {
1222 r = McReadDirEntry(port, slot, mcman_curdircluster, 0, &fse);
1223 if (r != sceMcResSucceed)
1226 r = McReadDirEntry(port, slot, fse->cluster, fse->dir_entry, &fse);
1227 if (r != sceMcResSucceed)
1230 info->EntryName[0] =
'.';
1231 info->EntryName[1] =
'\0';
1234 strncpy(
info->EntryName, fse->name, 32);
1237 info->AttrFile = fse->mode;
1238 info->Reserve1 = fse->unused;
1240 info->_Create = fse->created;
1241 info->_Modify = fse->modified;
1243 if (!(fse->mode & sceMcFileAttrSubdir))
1244 info->FileSizeByte = fse->length;
1257int mcman_delete2(
int port,
int slot,
const char *filename,
int flags)
1263 DPRINTF(
"mcman_delete2 port%d slot%d filename %s flags %x\n", port, slot, filename, flags);
1268 r = mcman_cachedirentry(port, slot, filename, &cacheDir, &fse1, ((u32)(flags < 1)) ? 1 : 0);
1270 return sceMcResNoEntry;
1275 if (!(fse1->mode & sceMcFileAttrExists))
1276 return sceMcResNoEntry;
1279 if (fse1->mode & sceMcFileAttrExists)
1280 return sceMcResNoEntry;
1283 if ((!PS1CardFlag) && (!flags)) {
1284 if (!(fse1->mode & sceMcFileAttrWriteable))
1285 return sceMcResDeniedPermit;
1288 if ((!fse1->cluster) && (!fse1->dir_entry))
1289 return sceMcResNoEntry;
1292 if ((!flags) && (fse1->mode & sceMcFileAttrSubdir) && ((u32)i < fse1->length)) {
1295 r = McReadDirEntry(port, slot, fse1->cluster, i, &fse2);
1296 if (r != sceMcResSucceed)
1299 if (fse2->mode & sceMcFileAttrExists)
1300 return sceMcResNotEmpty;
1302 }
while ((u32)(++i) < fse1->length);
1305 r = McSetDirEntryState(port, slot, cacheDir.cluster, cacheDir.fsindex, flags);
1306 if (r != sceMcResSucceed)
1309 r = McFlushCache(port, slot);
1310 if (r != sceMcResSucceed)
1313 return sceMcResSucceed;
1317int mcman_unformat2(
int port,
int slot)
1319 register int r, i, j, z, l, pageword_cnt, page, blocks_on_card, erase_byte, err_cnt;
1320 register u32 erase_value;
1321 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
1323 DPRINTF(
"mcman_unformat2 port%d slot%d\n", port, slot);
1325 pageword_cnt = mcdi->pagesize >> 2;
1326 blocks_on_card = mcdi->clusters_per_card / mcdi->clusters_per_block;
1328 erase_value = 0xffffffff;
1329 if (!(mcdi->cardflags & CF_ERASE_ZEROES))
1330 erase_value = 0x00000000;
1332 for (i = 0; i < pageword_cnt; i++)
1335 for (i = 0; i < 128; i++)
1336 *((u32 *)&mcman_eccdata + i) = erase_value;
1339 if (i < blocks_on_card) {
1340 erase_byte = erase_value & 0xff;
1342 page = i * mcdi->blocksize;
1343 if (mcdi->cardform > 0) {
1345 for (j = 0; j < 16; j++) {
1346 if (mcdi->bad_block_list[j] <= 0) {
1350 if (mcdi->bad_block_list[j] == i)
1358 if (mcdi->blocksize > 0) {
1361 if (r == sceMcResNoFormat) {
1365 if (r != sceMcResSucceed)
1368 if ((mcdi->cardflags & CF_USE_ECC) == 0) {
1369 for (l = 0; l < mcdi->pagesize; l++) {
1372 if ((u32)err_cnt >= (mcdi->clusters_per_block << 6)) {
1380 }
while (++z < mcdi->blocksize);
1384 if (((mcdi->cardflags & CF_USE_ECC) != 0) && (j == -1))
1388 r = mcman_eraseblock(port, slot, i, NULL, NULL);
1389 if (r != sceMcResSucceed)
1393 for (l = 0; l < pageword_cnt; l++)
1396 if (mcdi->blocksize > 0) {
1399 r = McWritePage(port, slot, page + z, &
mcman_pagebuf, mcman_eccdata);
1400 if (r != sceMcResSucceed)
1402 }
while (++z < mcdi->blocksize);
1405 }
while (++i < blocks_on_card);
1408 r = mcman_eraseblock(port, slot, 0, NULL, NULL);
1409 if (r != sceMcResSucceed)
1412 return sceMcResSucceed;