11#define SIO2MAN_EXPORTS_IMPLEMENTATION
13#include "mcman-internal.h"
20IRX_ID(MODNAME, 2, 11);
22char SUPERBLOCK_MAGIC[] =
"Sony PS2 Memory Card Format ";
23char SUPERBLOCK_VERSION[] =
"1.2.0.0";
25int mcman_wr_port = -1;
26int mcman_wr_slot = -1;
27int mcman_wr_block = -1;
28int mcman_wr_flag3 = -10;
29int mcman_curdircluster = -1;
31#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
39#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
41#elif defined(BUILDING_XFROMMAN)
45static u8 mcman_cachebuf[MAX_CACHEENTRY * MCMAN_CLUSTERSIZE];
52static void *mcman_pagedata[32];
53static u8 mcman_backupbuf[16384];
55static int mcman_badblock_port;
56static int mcman_badblock_slot;
57static int mcman_badblock;
58static int mcman_replacementcluster[16];
60static McFatCache mcman_fatcache[2][MCMAN_MAXSLOT];
61McFsEntry mcman_dircache[MAX_CACHEDIRENTRY];
64MCDevInfo mcman_devinfos[4][MCMAN_MAXSLOT];
70static const u8 mcman_xortable[256] = {
71 0x00, 0x87, 0x96, 0x11, 0xA5, 0x22, 0x33, 0xB4,
72 0xB4, 0x33, 0x22, 0xA5, 0x11, 0x96, 0x87, 0x00,
73 0xC3, 0x44, 0x55, 0xD2, 0x66, 0xE1, 0xF0, 0x77,
74 0x77, 0xF0, 0xE1, 0x66, 0xD2, 0x55, 0x44, 0xC3,
75 0xD2, 0x55, 0x44, 0xC3, 0x77, 0xF0, 0xE1, 0x66,
76 0x66, 0xE1, 0xF0, 0x77, 0xC3, 0x44, 0x55, 0xD2,
77 0x11, 0x96, 0x87, 0x00, 0xB4, 0x33, 0x22, 0xA5,
78 0xA5, 0x22, 0x33, 0xB4, 0x00, 0x87, 0x96, 0x11,
79 0xE1, 0x66, 0x77, 0xF0, 0x44, 0xC3, 0xD2, 0x55,
80 0x55, 0xD2, 0xC3, 0x44, 0xF0, 0x77, 0x66, 0xE1,
81 0x22, 0xA5, 0xB4, 0x33, 0x87, 0x00, 0x11, 0x96,
82 0x96, 0x11, 0x00, 0x87, 0x33, 0xB4, 0xA5, 0x22,
83 0x33, 0xB4, 0xA5, 0x22, 0x96, 0x11, 0x00, 0x87,
84 0x87, 0x00, 0x11, 0x96, 0x22, 0xA5, 0xB4, 0x33,
85 0xF0, 0x77, 0x66, 0xE1, 0x55, 0xD2, 0xC3, 0x44,
86 0x44, 0xC3, 0xD2, 0x55, 0xE1, 0x66, 0x77, 0xF0,
87 0xF0, 0x77, 0x66, 0xE1, 0x55, 0xD2, 0xC3, 0x44,
88 0x44, 0xC3, 0xD2, 0x55, 0xE1, 0x66, 0x77, 0xF0,
89 0x33, 0xB4, 0xA5, 0x22, 0x96, 0x11, 0x00, 0x87,
90 0x87, 0x00, 0x11, 0x96, 0x22, 0xA5, 0xB4, 0x33,
91 0x22, 0xA5, 0xB4, 0x33, 0x87, 0x00, 0x11, 0x96,
92 0x96, 0x11, 0x00, 0x87, 0x33, 0xB4, 0xA5, 0x22,
93 0xE1, 0x66, 0x77, 0xF0, 0x44, 0xC3, 0xD2, 0x55,
94 0x55, 0xD2, 0xC3, 0x44, 0xF0, 0x77, 0x66, 0xE1,
95 0x11, 0x96, 0x87, 0x00, 0xB4, 0x33, 0x22, 0xA5,
96 0xA5, 0x22, 0x33, 0xB4, 0x00, 0x87, 0x96, 0x11,
97 0xD2, 0x55, 0x44, 0xC3, 0x77, 0xF0, 0xE1, 0x66,
98 0x66, 0xE1, 0xF0, 0x77, 0xC3, 0x44, 0x55, 0xD2,
99 0xC3, 0x44, 0x55, 0xD2, 0x66, 0xE1, 0xF0, 0x77,
100 0x77, 0xF0, 0xE1, 0x66, 0xD2, 0x55, 0x44, 0xC3,
101 0x00, 0x87, 0x96, 0x11, 0xA5, 0x22, 0x33, 0xB4,
102 0xB4, 0x33, 0x22, 0xA5, 0x11, 0x96, 0x87, 0x00
107void long_multiply(u32 v1, u32 v2, u32 *HI, u32 *LO)
109 register long a, b, c, d;
112 a = (v1 >> 16) & 0xffff;
114 c = (v2 >> 16) & 0xffff;
119 y = ((*LO >> 16) & 0xffff) + x;
121 *LO = (*LO & 0xffff) | ((y & 0xffff) << 16);
122 *HI = (y >> 16) & 0xffff;
128int mcman_chrpos(
const char *str,
int chr)
135 if (*p == (chr & 0xff))
140 if (*p != (chr & 0xff))
147int MCMAN_ENTRYPOINT(
int argc,
char *argv[],
void *startaddr,
ModuleInfo_t *mi)
149int MCMAN_ENTRYPOINT(
int argc,
char *argv[])
157 sio_init(38400, 0, 0, 0, 0);
159 DPRINTF(
"_start...\n");
161#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
162 export_tbl = &_exp_mcman;
163#elif defined(BUILDING_XFROMMAN)
164 export_tbl = &_exp_xfromman;
177 if (export_tbl != NULL)
180 release_res = ReleaseLibraryEntries(export_tbl);
183 if (release_res == 0 || release_res == -213)
187 mcman_deinitPS2com();
188 mcman_deinitPS1PDAcom();
189 return MODULE_NO_RESIDENT_END;
191 return MODULE_REMOVABLE_END;
195 DPRINTF(
"registering exports...\n");
197 if (export_tbl != NULL)
199 if (RegisterLibraryEntries(export_tbl) != 0)
200 return MODULE_NO_RESIDENT_END;
207 DPRINTF(
"initPS2com...\n");
210 DPRINTF(
"initPS1PDAcom...\n");
211 mcman_initPS1PDAcom();
213 DPRINTF(
"initcache...\n");
216 DPRINTF(
"initdev...\n");
219#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
220 timer_ID = ReferHardTimer(1, 32, 0, 0x6309);
222 if (timer_ID != -150)
223 return MODULE_RESIDENT_END;
225 timer_ID = AllocHardTimer(1, 32, 1);
228 SetTimerMode(timer_ID, 0);
231 DPRINTF(
"_start returns MODULE_RESIDENT_END...\n");
234 if (mi && ((mi->
newflags & 2) != 0))
237 return MODULE_RESIDENT_END;
241int McGetFormat(
int port,
int slot)
243 DPRINTF(
"McGetFormat port%d slot%d\n", port, slot);
244 return mcman_devinfos[port][slot].cardform;
248int McGetMcType(
int port,
int slot)
250 DPRINTF(
"McGetMcType port%d slot%d\n", port, slot);
251 return mcman_devinfos[port][slot].cardtype;
256struct irx_id *McGetModuleInfo(
void)
258 DPRINTF(
"McGetModuleInfo\n");
264void McSetPS1CardFlag(
int flag)
266 DPRINTF(
"McSetPS1CardFlag flag %x\n", flag);
271int McGetFreeClusters(
int port,
int slot)
273 register int r, mcfree;
274 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
276 DPRINTF(
"McGetFreeClusters port%d slot%d\n", port, slot);
279 if (mcdi->cardform > 0) {
280 switch (mcdi->cardtype) {
282 mcfree = mcman_findfree2(port, slot, 0);
286 mcfree = mcman_findfree1(port, slot, 0);
288 case sceMcTypeNoCard:
292 if (mcfree == sceMcResFullDevice)
304void mcman_wmemset(
void *buf,
int size,
int value)
307 size = (size >> 2) - 1;
312 }
while (--size > -1);
317int mcman_calcEDC(
void *buf,
int size)
319 register u32 checksum;
329 while (size-- != -1) {
334 return checksum & 0xff;
338int mcman_checkpath(
const char *str)
345 if (((p[i] & 0xff) ==
'?') || ((p[i] & 0xff) ==
'*'))
347 if ((p[i] & 0xff) < 32)
355int mcman_checkdirpath(
const char *str1,
const char *str2)
358 const char *p1 = str1;
359 const char *p2 = str2;
362 register int pos1, pos2;
363 pos1 = mcman_chrpos(p2,
'?');
364 pos2 = mcman_chrpos(p2,
'*');
366 if ((pos1 < 0) && (pos2 < 0)) {
381 if (strncmp(p2, p1, pos) != 0)
387 while (p2[0] ==
'?') {
393 }
while (p2[0] !=
'*');
395 while((p2[0] ==
'*') || (p2[0] ==
'?'))
401 pos = mcman_chrpos(p1, (u8)p2[0]);
405 r = mcman_checkdirpath(p1, p2);
413void mcman_invhandles(
int port,
int slot)
419 if ((fh->port == port) && (fh->slot == slot))
422 }
while (++i < MAX_FDHANDLES);
428 register int fd = 0, rv = 0;
431 if (mcman_fdhandles[fd].status) {
440 }
while (fd < MAX_FDHANDLES);
446int McDetectCard(
int port,
int slot)
448#ifdef BUILDING_XMCMAN
449 return mcman_detectcard(port, slot);
451 return McDetectCard2(port, slot);
456int mcman_detectcard(
int port,
int slot)
461 DPRINTF(
"mcman_detectcard port%d slot%d\n", port, slot);
462 mcdi = (
MCDevInfo *)&mcman_devinfos[port][slot];
464#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
465 if ((mcdi->cardtype == sceMcTypeNoCard) || (mcdi->cardtype == sceMcTypePS2)) {
466 r = mcman_probePS2Card2(port, slot);
468 r = mcman_probePS1Card2(port, slot);
470 if (mcman_probePDACard(port, slot)) {
471 mcdi->cardtype = sceMcTypePS1;
472 return (!PS1CardFlag) ? sceMcResDeniedPS1Permit : r;
475 mcdi->cardtype = sceMcTypePDA;
481 mcdi->cardtype = sceMcTypePS2;
486 r = mcman_probePS1Card2(port, slot);
488 if ((r < -9) || (r >= 0)) {
489 r = mcman_probePS2Card2(port, slot);
491 mcdi->cardtype = sceMcTypePS2;
496 if (mcman_probePDACard(port, slot)) {
497 mcdi->cardtype = sceMcTypePS1;
498 return (!PS1CardFlag) ? sceMcResDeniedPS1Permit : r;
501 mcdi->cardtype = sceMcTypePDA;
508 return sceMcResSucceed;
509 if (mcdi->cardtype == sceMcTypePS1)
510 return sceMcResDeniedPS1Permit;
511 return sceMcResSucceed;
515 r = mcman_probePS2Card2(port, slot);
517 mcdi->cardtype = sceMcTypePS2;
524 mcman_invhandles(port, slot);
525 mcman_clearcache(port, slot);
531int McDetectCard2(
int port,
int slot)
536 DPRINTF(
"McDetectCard2 port%d slot%d\n", port, slot);
538 mcdi = (
MCDevInfo *)&mcman_devinfos[port][slot];
540#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
541 if ((mcdi->cardtype == sceMcTypeNoCard) || (mcdi->cardtype == sceMcTypePS2)) {
542 r = mcman_probePS2Card(port, slot);
544 r = mcman_probePS1Card(port, slot);
546 if (mcman_probePDACard(port, slot)) {
547 mcdi->cardtype = sceMcTypePS1;
548 return (!PS1CardFlag) ? sceMcResDeniedPS1Permit : r;
551 mcdi->cardtype = sceMcTypePDA;
557 mcdi->cardtype = sceMcTypePS2;
562 r = mcman_probePS1Card(port, slot);
564 if ((r < -9) || (r >= 0)) {
565 r = mcman_probePS2Card(port, slot);
567 mcdi->cardtype = sceMcTypePS2;
572 if (mcman_probePDACard(port, slot)) {
573 mcdi->cardtype = sceMcTypePS1;
574 return (!PS1CardFlag) ? sceMcResDeniedPS1Permit : r;
577 mcdi->cardtype = sceMcTypePDA;
584 return sceMcResSucceed;
585 if (mcdi->cardtype == sceMcTypePS1)
586 return sceMcResDeniedPS1Permit;
587 return sceMcResSucceed;
591 r = mcman_probePS2Card(port, slot);
593 mcdi->cardtype = sceMcTypePS2;
600 mcman_invhandles(port, slot);
601 mcman_clearcache(port, slot);
607int McOpen(
int port,
int slot,
const char *filename,
int flag)
611 r = McDetectCard(port, slot);
612 if (r != sceMcResSucceed)
618 if (mcman_devinfos[port][slot].cardtype == sceMcTypePS2)
619 r = mcman_open2(port, slot, filename, flag);
621 r = mcman_open1(port, slot, filename, flag);
624 mcman_invhandles(port, slot);
625 mcman_clearcache(port, slot);
637 if (!((u32)fd < MAX_FDHANDLES))
638 return sceMcResDeniedPermit;
642 return sceMcResDeniedPermit;
645 r = McDetectCard(fh->port, fh->slot);
646 if (r != sceMcResSucceed)
649 r = McFlushCache(fh->port, fh->slot);
651 mcman_invhandles(fh->port, fh->slot);
652 mcman_clearcache(fh->port, fh->slot);
655 if (r != sceMcResSucceed)
658 if (fh->unknown2 != 0) {
662 mcdi = (
MCDevInfo *)&mcman_devinfos[fh->port][fh->slot];
663 if (mcdi->cardtype == sceMcTypePS2)
664 r = mcman_close2(fd);
666 r = mcman_close1(fd);
669 mcman_invhandles(fh->port, fh->slot);
670 mcman_clearcache(fh->port, fh->slot);
673 if (r != sceMcResSucceed)
677 r = McFlushCache(fh->port, fh->slot);
679 mcman_invhandles(fh->port, fh->slot);
680 mcman_clearcache(fh->port, fh->slot);
692 if (!((u32)fd < MAX_FDHANDLES))
693 return sceMcResDeniedPermit;
697 return sceMcResDeniedPermit;
699 r = McDetectCard(fh->port, fh->slot);
700 if (r != sceMcResSucceed)
703 r = McFlushCache(fh->port, fh->slot);
705 mcman_invhandles(fh->port, fh->slot);
706 mcman_clearcache(fh->port, fh->slot);
709 if (r != sceMcResSucceed) {
714 if (fh->unknown2 != 0) {
718 mcdi = (
MCDevInfo *)&mcman_devinfos[fh->port][fh->slot];
719 if (mcdi->cardtype == sceMcTypePS2)
720 r = mcman_close2(fd);
722 r = mcman_close1(fd);
725 mcman_invhandles(fh->port, fh->slot);
726 mcman_clearcache(fh->port, fh->slot);
729 if (r != sceMcResSucceed) {
735 r = McFlushCache(fh->port, fh->slot);
740 mcman_invhandles(fh->port, fh->slot);
741 mcman_clearcache(fh->port, fh->slot);
748int McSeek(
int fd,
int offset,
int origin)
753 DPRINTF(
"McSeek fd %d offset %d origin %d\n", fd, offset, origin);
755 if (!((u32)fd < MAX_FDHANDLES))
756 return sceMcResDeniedPermit;
760 return sceMcResDeniedPermit;
762 r = McDetectCard(fh->port, fh->slot);
763 if (r != sceMcResSucceed)
769 r = fh->position + offset;
775 r = fh->filesize + offset;
779 return fh->position = (r < 0) ? 0 : r;
783int McRead(
int fd,
void *buf,
int length)
788 if (!((u32)fd < MAX_FDHANDLES))
789 return sceMcResDeniedPermit;
793 return sceMcResDeniedPermit;
796 return sceMcResDeniedPermit;
798 r = McDetectCard(fh->port, fh->slot);
799 if (r != sceMcResSucceed)
802 if (mcman_devinfos[fh->port][fh->slot].cardtype == sceMcTypePS2)
803 r = mcman_read2(fd, buf, length);
805 r = mcman_read1(fd, buf, length);
811 mcman_invhandles(fh->port, fh->slot);
812 mcman_clearcache(fh->port, fh->slot);
819int McWrite(
int fd,
void *buf,
int length)
824 if (!((u32)fd < MAX_FDHANDLES))
825 return sceMcResDeniedPermit;
829 return sceMcResDeniedPermit;
832 return sceMcResDeniedPermit;
834 r = McDetectCard(fh->port, fh->slot);
835 if (r != sceMcResSucceed)
838 if (mcman_devinfos[fh->port][fh->slot].cardtype == sceMcTypePS2)
839 r = mcman_write2(fd, buf, length);
841 r = mcman_write1(fd, buf, length);
847 mcman_invhandles(fh->port, fh->slot);
848 mcman_clearcache(fh->port, fh->slot);
855int McGetEntSpace(
int port,
int slot,
const char *dirname)
859 r = McDetectCard(port, slot);
860 if (r != sceMcResSucceed)
863 if (mcman_devinfos[port][slot].cardtype == sceMcTypePS2) {
864 r = mcman_getentspace(port, slot, dirname);
868 mcman_invhandles(port, slot);
869 mcman_clearcache(port, slot);
876int McGetDir(
int port,
int slot,
const char *dirname,
int flags,
int maxent,
sceMcTblGetDir *
info)
880 r = McDetectCard(port, slot);
881 if (r != sceMcResSucceed)
884 if (mcman_devinfos[port][slot].cardtype == sceMcTypePS2)
885 r = mcman_getdir2(port, slot, dirname, flags & 0xFFFF, maxent,
info);
887 r = mcman_getdir1(port, slot, dirname, flags & 0xFFFF, maxent,
info);
890 mcman_invhandles(port, slot);
891 mcman_clearcache(port, slot);
898int mcman_dread(
int fd, MC_IO_DRE_T *dirent)
903 if (!((u32)fd < MAX_FDHANDLES))
904 return sceMcResDeniedPermit;
908 return sceMcResDeniedPermit;
911 return sceMcResDeniedPermit;
913 r = McDetectCard(fh->port, fh->slot);
914 if (r != sceMcResSucceed)
917 if (mcman_devinfos[fh->port][fh->slot].cardtype == sceMcTypePS2)
918 r = mcman_dread2(fd, dirent);
920 r = mcman_dread1(fd, dirent);
926 mcman_invhandles(fh->port, fh->slot);
927 mcman_clearcache(fh->port, fh->slot);
934int mcman_getstat(
int port,
int slot,
const char *filename, MC_IO_STA_T *stat)
938 r = McDetectCard(port, slot);
939 if (r != sceMcResSucceed)
942 if (mcman_devinfos[port][slot].cardtype == sceMcTypePS2)
943 r = mcman_getstat2(port, slot, filename, stat);
945 r = mcman_getstat1(port, slot, filename, stat);
948 mcman_invhandles(port, slot);
949 mcman_clearcache(port, slot);
956int McSetFileInfo(
int port,
int slot,
const char *filename,
sceMcTblGetDir *
info,
int flags)
960 r = McDetectCard(port, slot);
961 if (r != sceMcResSucceed)
964 if (mcman_devinfos[port][slot].cardtype == sceMcTypePS2)
965 r = mcman_setinfo2(port, slot, filename,
info, flags);
967 r = mcman_setinfo1(port, slot, filename,
info, flags);
970 mcman_invhandles(port, slot);
971 mcman_clearcache(port, slot);
974 if (r == sceMcResSucceed) {
975 r = McFlushCache(port, slot);
977 mcman_invhandles(port, slot);
978 mcman_clearcache(port, slot);
986int McChDir(
int port,
int slot,
const char *newdir,
char *currentdir)
990 r = McDetectCard(port, slot);
991 if (r != sceMcResSucceed)
994 if (mcman_devinfos[port][slot].cardtype == sceMcTypePS2)
995 r = mcman_chdir(port, slot, newdir, currentdir);
1002 mcman_invhandles(port, slot);
1003 mcman_clearcache(port, slot);
1010int McDelete(
int port,
int slot,
const char *filename,
int flags)
1014 r = McDetectCard(port, slot);
1015 if (r != sceMcResSucceed)
1018 if (mcman_devinfos[port][slot].cardtype == sceMcTypePS2)
1019 r = mcman_delete2(port, slot, filename, flags);
1021 r = mcman_delete1(port, slot, filename, flags);
1024 mcman_invhandles(port, slot);
1025 mcman_clearcache(port, slot);
1032int McFormat(
int port,
int slot)
1036 mcman_invhandles(port, slot);
1038 r = McDetectCard(port, slot);
1042 mcman_clearcache(port, slot);
1044 if (mcman_devinfos[port][slot].cardtype == sceMcTypePS2)
1045 r = mcman_format2(port, slot);
1047 r = mcman_format1(port, slot);
1050 mcman_invhandles(port, slot);
1051 mcman_clearcache(port, slot);
1058int McUnformat(
int port,
int slot)
1062 mcman_invhandles(port, slot);
1064 r = McDetectCard(port, slot);
1068 mcman_clearcache(port, slot);
1070 if (mcman_devinfos[port][slot].cardtype == sceMcTypePS2)
1071 r = mcman_unformat2(port, slot);
1073 r = mcman_unformat1(port, slot);
1075 mcman_devinfos[port][slot].cardform = 0;
1078 mcman_invhandles(port, slot);
1079 mcman_clearcache(port, slot);
1089 register int retries;
1095 if (sceCdRC(&cdtime))
1097 }
while (--retries > 0);
1099 if (cdtime.
stat & 0x80) {
1110 tm->Sec = btoi(cdtime.
second);
1111 tm->Min = btoi(cdtime.
minute);
1112 tm->Hour = btoi(cdtime.
hour);
1113 tm->Day = btoi(cdtime.
day);
1115 if ((cdtime.
month & 0x10) != 0)
1116 tm->Month = (cdtime.
month & 0xf) + 0xa;
1118 tm->Month = cdtime.
month & 0xf;
1120 tm->Year = btoi(cdtime.
year) + 2000;
1127 rawtime += (-9 * 60 * 60);
1129 gmtime_s(&timeinfo, &rawtime);
1131 gmtime_r(&rawtime, &timeinfo);
1134 tm->Sec = timeinfo.tm_sec;
1135 tm->Min = timeinfo.tm_min;
1136 tm->Hour = timeinfo.tm_hour;
1137 tm->Day = timeinfo.tm_mday;
1138 tm->Month = timeinfo.tm_mon + 1;
1139 tm->Year = timeinfo.tm_year + 1900;
1146int McEraseBlock(
int port,
int block,
void **pagebuf,
void *eccbuf)
1148 return mcman_eraseblock(port, 0, block, (
void **)pagebuf, eccbuf);
1152int McEraseBlock2(
int port,
int slot,
int block,
void **pagebuf,
void *eccbuf)
1154 return mcman_eraseblock(port, slot, block, (
void **)pagebuf, eccbuf);
1158int McReadPage(
int port,
int slot,
int page,
void *buf)
1160 register int r, index, ecres, retries,
count, erase_byte;
1161 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
1165 count = (mcdi->pagesize + 127) >> 7;
1166 erase_byte = (mcdi->cardflags & CF_ERASE_ZEROES) ? 0x0 : 0xFF;
1169 ecres = sceMcResSucceed;
1171 if (!mcman_readpage(port, slot, page, buf, eccbuf)) {
1172 if (mcdi->cardflags & CF_USE_ECC) {
1174 if (eccbuf[mcman_sparesize(port, slot) - 1] == erase_byte)
1180 peccb = (u8 *)eccbuf;
1184 r = mcman_correctdata(pdata, peccb);
1190 }
while (++index <
count);
1193 if (ecres == sceMcResSucceed)
1196 if ((retries == 4) && (!(ecres < sceMcResNoFormat)))
1204 }
while (++retries < 5);
1207 return sceMcResSucceed;
1209 return (ecres != sceMcResSucceed) ? sceMcResNoFormat : sceMcResChangedCard;
1213void McDataChecksum(
void *buf,
void *ecc)
1215 register u8 *p, *p_ecc;
1216 register int i, a2, a3, t0;
1227 v = mcman_xortable[*p++];
1233 }
while (++i < 0x80);
1236 p_ecc[0] = ~a2 & 0x77;
1237 p_ecc[1] = ~a3 & 0x7F;
1238 p_ecc[2] = ~t0 & 0x7F;
1242int mcman_getcnum(
int port,
int slot)
1244 return ((port & 1) << 3) + slot;
1248int mcman_correctdata(
void *buf,
void *ecc)
1250 register int xor0, xor1, xor2, xor3, xor4;
1254 McDataChecksum(buf, eccbuf);
1256 xor0 = p[0] ^ eccbuf[0];
1257 xor1 = p[1] ^ eccbuf[1];
1258 xor2 = p[2] ^ eccbuf[2];
1261 xor4 = (xor0 & 0xf) ^ (xor0 >> 4);
1263 if (!xor0 && !xor1 && !xor2)
1266 if ((xor3 == 0x7f) && (xor4 == 0x7)) {
1267 p[xor2] ^= 1 << (xor0 >> 4);
1278 }
while (xor2 >= 0);
1286 }
while (xor2 >= 0);
1295int mcman_sparesize(
int port,
int slot)
1297 return (mcman_devinfos[port][slot].pagesize + 0x1F) >> 5;
1301int mcman_setdevspec(
int port,
int slot)
1304 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
1306 DPRINTF(
"mcman_setdevspec port%d, slot%d\n", port, slot);
1308 if (McGetCardSpec(port, slot, &mcdi->pagesize, &mcdi->blocksize, &cardsize, &mcdi->cardflags) != sceMcResSucceed)
1309 return sceMcResFullDevice;
1311 mcdi->pages_per_cluster = MCMAN_CLUSTERSIZE / mcdi->pagesize;
1312 mcdi->cluster_size = MCMAN_CLUSTERSIZE;
1315 mcdi->unused = 0xff00;
1316 mcdi->FATentries_per_cluster = MCMAN_CLUSTERFATENTRIES;
1317 mcdi->unknown5 = -1;
1318 mcdi->rootdir_cluster2 = mcdi->rootdir_cluster;
1319 mcdi->clusters_per_block = mcdi->blocksize / mcdi->pages_per_cluster;
1320 mcdi->clusters_per_card = (cardsize / mcdi->blocksize) * (mcdi->blocksize / mcdi->pages_per_cluster);
1322 return sceMcResSucceed;
1326int mcman_setdevinfos(
int port,
int slot)
1328 register int r, allocatable_clusters_per_card, iscluster_valid, current_allocatable_cluster, cluster_cnt;
1329 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
1332 DPRINTF(
"mcman_setdevinfos port%d slot%d\n", port, slot);
1334 mcman_wmemset((
void *)mcdi,
sizeof(
MCDevInfo), 0);
1338 r = mcman_setdevspec(port, slot);
1339 if (r != sceMcResSucceed)
1342 mcdi->cardform = -1;
1345 if (r == sceMcResNoFormat)
1346 return sceMcResNoFormat;
1347 if (r != sceMcResSucceed)
1350 if (strncmp(SUPERBLOCK_MAGIC,
mcman_pagebuf.magic, 28) != 0) {
1351 DPRINTF(
"mcman_setdevinfos No card format !!!\n");
1352 return sceMcResNoFormat;
1356 return sceMcResNoFormat;
1359 for (r=0; r<0x150; r++)
1362 mcdi->cardtype = sceMcTypePS2;
1364 r = mcman_checkBackupBlocks(port, slot);
1365 if (r != sceMcResSucceed)
1368 r = McReadDirEntry(port, slot, 0, 0, &pfse);
1369 if (r != sceMcResSucceed)
1370 return sceMcResNoFormat;
1372 if (strcmp(pfse->name,
".") != 0)
1373 return sceMcResNoFormat;
1375 if (McReadDirEntry(port, slot, 0, 1, &pfse) != sceMcResSucceed)
1378 if (strcmp(pfse->name,
"..") != 0)
1379 return sceMcResNoFormat;
1385 if ((mcdi->clusters_per_block * mcdi->backup_block2) == mcdi->alloc_end)
1386 mcdi->alloc_end = (mcdi->clusters_per_block * mcdi->backup_block2) - mcdi->alloc_offset;
1391 long_multiply(mcdi->clusters_per_card, 0x10624dd3, &hi, &lo);
1392 temp = (hi >> 6) - (mcdi->clusters_per_card >> 31);
1393 allocatable_clusters_per_card = (((((temp << 5) - temp) << 2) + temp) << 3) + 1;
1394 iscluster_valid = 0;
1396 current_allocatable_cluster = mcdi->alloc_offset;
1398 while (cluster_cnt < allocatable_clusters_per_card) {
1399 if ((u32)current_allocatable_cluster >= mcdi->clusters_per_card)
1402 if (((current_allocatable_cluster % mcdi->clusters_per_block) == 0) \
1403 || (mcdi->alloc_offset == (u32)current_allocatable_cluster)) {
1404 iscluster_valid = 1;
1405 for (r=0; r<16; r++) {
1406 if (current_allocatable_cluster / mcdi->clusters_per_block == (u32)(mcdi->bad_block_list[r]))
1407 iscluster_valid = 0;
1410 if (iscluster_valid == 1)
1412 current_allocatable_cluster++;
1415 mcdi->max_allocatable_clusters = current_allocatable_cluster - mcdi->alloc_offset;
1417 return sceMcResSucceed;
1421int mcman_reportBadBlocks(
int port,
int slot)
1423 register int bad_blocks, erase_byte, err_limit;
1424 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
1426 DPRINTF(
"mcman_reportBadBlocks port%d, slot%d\n", port, slot);
1428 mcman_wmemset((
void *)mcdi->bad_block_list, 128, -1);
1430 if ((mcdi->cardflags & CF_BAD_BLOCK) == 0)
1431 return sceMcResSucceed;
1433 err_limit = ((mcdi->pagesize & 0xffff) + (mcdi->pagesize & 0x1)) >> 1;
1436 if ((mcdi->cardflags & CF_ERASE_ZEROES) != 0)
1441 if ((mcdi->clusters_per_card / mcdi->clusters_per_block) > 0) {
1447 register int page, err_cnt;
1449 if (bad_blocks >= 16)
1457 r = McReadPage(port, slot, (block * mcdi->blocksize) + page, &
mcman_pagebuf);
1458 if (r == sceMcResNoFormat) {
1459 mcdi->bad_block_list[bad_blocks] = block;
1463 if (r != sceMcResSucceed)
1466 if ((mcdi->cardflags & CF_USE_ECC) == 0) {
1471 for (i = 0; i < mcdi->pagesize; i++) {
1473 if (*p++ != erase_byte)
1476 if (err_cnt >= err_limit) {
1477 mcdi->bad_block_list[bad_blocks] = block;
1483 }
while (++page < 2);
1485 }
while ((u32)(++block) < (mcdi->clusters_per_card / mcdi->clusters_per_block));
1488 return sceMcResSucceed;
1492int McCreateDirentry(
int port,
int slot,
int parent_cluster,
int num_entries,
int cluster,
const sceMcStDateTime *ctime)
1496 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
1499 DPRINTF(
"McCreateDirentry port%d slot%d parent_cluster %x num_entries %d cluster %x\n", port, slot, parent_cluster, num_entries, cluster);
1501 r = McReadCluster(port, slot, mcdi->alloc_offset + cluster, &mce);
1502 if (r != sceMcResSucceed)
1505 mcman_wmemset(mce->cl_data, MCMAN_CLUSTERSIZE, 0);
1510 mfe->mode = sceMcFileAttrReadable | sceMcFileAttrWriteable | sceMcFileAttrExecutable \
1511 | sceMcFileAttrSubdir | sceMcFile0400 | sceMcFileAttrExists;
1514 mcman_getmcrtime(&mfe->created);
1516 mfe->created = *ctime;
1518 mfe->modified = mfe->created;
1521 mfe->dir_entry = num_entries;
1522 mfe->cluster = parent_cluster;
1524 mfe->name[1] =
'\0';
1526 if ((parent_cluster == 0) && (num_entries == 0)) {
1528 mfe_next->created = mfe->created;
1532 mfe->mode = sceMcFileAttrWriteable | sceMcFileAttrExecutable | sceMcFileAttrSubdir \
1533 | sceMcFile0400 | sceMcFileAttrExists | sceMcFileAttrHidden;
1539 McReadDirEntry(port, slot, parent_cluster, 0, &pfse);
1541 mfe_next->created = pfse->created;
1544 mfe->mode = sceMcFileAttrReadable | sceMcFileAttrWriteable | sceMcFileAttrExecutable \
1545 | sceMcFileAttrSubdir | sceMcFile0400 | sceMcFileAttrExists;
1546 mfe->dir_entry = pfse->dir_entry;
1548 mfe->cluster = pfse->cluster;
1551 mfe->modified = mfe->created;
1556 mfe->name[2] =
'\0';
1560 return sceMcResSucceed;
1564int mcman_fatRseek(
int fd)
1566 register int entries_to_read, fat_index;
1568 register MCDevInfo *mcdi = &mcman_devinfos[fh->port][fh->slot];
1571 entries_to_read = fh->position / mcdi->cluster_size;
1575 if ((u32)entries_to_read < fh->clust_offset)
1576 fat_index = fh->freeclink;
1578 fat_index = fh->clink;
1579 entries_to_read -= fh->clust_offset;
1582 if (entries_to_read == 0) {
1584 return fat_index + mcdi->alloc_offset;
1586 return sceMcResFullDevice;
1592 r = McGetFATentry(fh->port, fh->slot, fat_index, &fat_entry);
1593 if (r != sceMcResSucceed)
1596 fat_index = fat_entry;
1598 if (fat_index >= -1)
1599 return sceMcResFullDevice;
1603 fat_index &= ~0x80000000;
1604 fh->clink = fat_index;
1605 fh->clust_offset = (fh->position / mcdi->cluster_size) - entries_to_read;
1607 }
while (entries_to_read > 0);
1609 return fat_index + mcdi->alloc_offset;
1613int mcman_fatWseek(
int fd)
1615 register int r, entries_to_write, fat_index;
1617 register MCDevInfo *mcdi = &mcman_devinfos[fh->port][fh->slot];
1621 entries_to_write = fh->position / mcdi->cluster_size;
1623 if ((fh->clust_offset == 0) || ((u32)entries_to_write < fh->clust_offset)) {
1624 fat_index = fh->freeclink;
1626 if (fat_index < 0) {
1627 fat_index = mcman_findfree2(fh->port, fh->slot, 1);
1630 return sceMcResFullDevice;
1633 fh->freeclink = fat_index;
1635 r = mcman_close2(fd);
1636 if (r != sceMcResSucceed)
1639 mcman_addcacheentry(mce);
1640 McFlushCache(fh->port, fh->slot);
1644 fat_index = fh->clink;
1645 entries_to_write -= fh->clust_offset;
1648 if (entries_to_write != 0) {
1650 r = McGetFATentry(fh->port, fh->slot, fat_index, &fat_entry);
1651 if (r != sceMcResSucceed)
1654 if ((
unsigned int)fat_entry >= 0xffffffff) {
1655 r = mcman_findfree2(fh->port, fh->slot, 1);
1659 fat_entry |= 0x80000000;
1663 r = McSetFATentry(fh->port, fh->slot, fat_index, fat_entry);
1664 if (r != sceMcResSucceed)
1667 mcman_addcacheentry(mce);
1671 fat_index = fat_entry & ~0x80000000;
1672 }
while (entries_to_write > 0);
1675 fh->clink = fat_index;
1676 fh->clust_offset = fh->position / mcdi->cluster_size;
1678 return sceMcResSucceed;
1682int mcman_findfree2(
int port,
int slot,
int reserve)
1684 register int r, rfree, ifc_index, indirect_offset, fat_index, block;
1685 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
1688 DPRINTF(
"mcman_findfree2 port%d slot%d reserve%d\n", port, slot, reserve);
1692 for (fat_index = mcdi->unknown2; (u32)fat_index < mcdi->max_allocatable_clusters; fat_index++) {
1693 register int indirect_index, fat_offset;
1695 indirect_index = fat_index / mcdi->FATentries_per_cluster;
1696 fat_offset = fat_index % mcdi->FATentries_per_cluster;
1698 if ((fat_offset == 0) || ((u32)fat_index == mcdi->unknown2)) {
1700 ifc_index = indirect_index / mcdi->FATentries_per_cluster;
1701 r = McReadCluster(port, slot, mcdi->ifc_list[ifc_index], &mce1);
1702 if (r != sceMcResSucceed)
1706 indirect_offset = indirect_index % mcdi->FATentries_per_cluster;
1708 r = McReadCluster(port, slot, fc->entry[indirect_offset], &mce2);
1709 if (r != sceMcResSucceed)
1715 if (fc->entry[fat_offset] >= 0) {
1716 block = (mcdi->alloc_offset + fat_offset) / mcdi->clusters_per_block;
1717 if (block != mcman_badblock) {
1719 fc->entry[fat_offset] = 0xffffffff;
1721 mcdi->unknown2 = fat_index;
1730 return sceMcResFullDevice;
1732 return (rfree) ? rfree : sceMcResFullDevice;
1736int mcman_getentspace(
int port,
int slot,
const char *dirname)
1738 register int r, i, entspace;
1742 u8 *pfsentry, *pmfe, *pfseend;
1744 DPRINTF(
"mcman_getentspace port%d slot%d dirname %s\n", port, slot, dirname);
1746 r = mcman_cachedirentry(port, slot, dirname, &cacheDir, &fse, 1);
1748 return sceMcResNoEntry;
1752 pfsentry = (u8 *)fse;
1754 pfseend = (u8 *)(pfsentry +
sizeof (
McFsEntry));
1757 *((u32 *)pmfe ) = *((u32 *)pfsentry );
1758 *((u32 *)pmfe+1) = *((u32 *)pfsentry+1);
1759 *((u32 *)pmfe+2) = *((u32 *)pfsentry+2);
1760 *((u32 *)pmfe+3) = *((u32 *)pfsentry+3);
1763 }
while (pfsentry < pfseend);
1765 entspace = mfe.length & 1;
1767 for (i = 0; (u32)i < mfe.length; i++) {
1769 r = McReadDirEntry(port, slot, mfe.cluster, i, &fse);
1770 if (r != sceMcResSucceed)
1772 if ((fse->mode & sceMcFileAttrExists) == 0)
1780int mcman_cachedirentry(
int port,
int slot,
const char *filename,
McCacheDir *pcacheDir,
McFsEntry **pfse,
int unknown_flag)
1782 register int r, fsindex, cluster, fmode;
1783 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
1786 u8 *pfsentry, *pcache, *pfseend;
1789 DPRINTF(
"mcman_cachedirentry port%d slot%d name %s\n", port, slot, filename);
1791 if (pcacheDir == NULL) {
1792 pcacheDir = &cacheDir;
1793 pcacheDir->maxent = -1;
1803 cluster = mcdi->rootdir_cluster2;
1804 fsindex = mcdi->unknown1;
1807 r = McReadDirEntry(port, slot, cluster, fsindex, &fse);
1808 if (r != sceMcResSucceed)
1812 if (!(fse->mode & sceMcFileAttrExists))
1815 if (pcacheDir == NULL) {
1817 return sceMcResSucceed;
1820 pfsentry = (u8 *)fse;
1821 pcache = (u8 *)&mcman_dircache[0];
1822 pfseend = (u8 *)(pfsentry +
sizeof (
McFsEntry));
1825 *((u32 *)pcache ) = *((u32 *)pfsentry );
1826 *((u32 *)pcache+1) = *((u32 *)pfsentry+1);
1827 *((u32 *)pcache+2) = *((u32 *)pfsentry+2);
1828 *((u32 *)pcache+3) = *((u32 *)pfsentry+3);
1831 }
while (pfsentry < pfseend);
1833 r = mcman_getdirinfo(port, slot, (
McFsEntry *)&mcman_dircache[0],
".", pcacheDir, unknown_flag);
1835 McReadDirEntry(port, slot, pcacheDir->cluster, pcacheDir->fsindex, pfse);
1845 fmode = sceMcFileAttrReadable | sceMcFileAttrExecutable;
1846 if ((fse->mode & fmode) != fmode)
1847 return sceMcResDeniedPermit;
1849 pfsentry = (u8 *)fse;
1850 pcache = (u8 *)&mcman_dircache[0];
1851 pfseend = (u8 *)(pfsentry +
sizeof(
McFsEntry));
1854 *((u32 *)pcache ) = *((u32 *)pfsentry );
1855 *((u32 *)pcache+1) = *((u32 *)pfsentry+1);
1856 *((u32 *)pcache+2) = *((u32 *)pfsentry+2);
1857 *((u32 *)pcache+3) = *((u32 *)pfsentry+3);
1860 }
while (pfsentry < pfseend);
1862 r = mcman_getdirinfo(port, slot, (
McFsEntry *)&mcman_dircache[0], p, pcacheDir, unknown_flag);
1865 if (mcman_chrpos(p,
'/') >= 0)
1868 pcacheDir->cluster = cluster;
1869 pcacheDir->fsindex = fsindex;
1874 r = mcman_chrpos(p,
'/');
1875 if ((r >= 0) && (p[r + 1] != 0)) {
1876 p += mcman_chrpos(p,
'/') + 1;
1877 cluster = pcacheDir->cluster;
1878 fsindex = pcacheDir->fsindex;
1880 McReadDirEntry(port, slot, cluster, fsindex, &fse);
1883 McReadDirEntry(port, slot, pcacheDir->cluster, pcacheDir->fsindex, pfse);
1885 return sceMcResSucceed;
1889 return sceMcResSucceed;
1893int mcman_getdirinfo(
int port,
int slot,
McFsEntry *pfse,
const char *filename,
McCacheDir *pcd,
int unknown_flag)
1895 register int i, r, ret, len, pos;
1897 u8 *pfsentry, *pfsee, *pfseend;
1899 DPRINTF(
"mcman_getdirinfo port%d slot%d name %s\n", port, slot, filename);
1901 pos = mcman_chrpos(filename,
'/');
1903 pos = strlen(filename);
1906 if ((pos == 2) && (!strncmp(filename,
"..", 2))) {
1908 r = McReadDirEntry(port, slot, pfse->cluster, 0, &fse);
1909 if (r != sceMcResSucceed)
1912 r = McReadDirEntry(port, slot, fse->cluster, 0, &fse);
1913 if (r != sceMcResSucceed)
1917 pcd->cluster = fse->cluster;
1918 pcd->fsindex = fse->dir_entry;
1921 r = McReadDirEntry(port, slot, fse->cluster, fse->dir_entry, &fse);
1922 if (r != sceMcResSucceed)
1925 pfsentry = (u8 *)fse;
1927 pfseend = (u8 *)(pfsentry +
sizeof(
McFsEntry));
1930 *((u32 *)pfsee ) = *((u32 *)pfsentry );
1931 *((u32 *)pfsee+1) = *((u32 *)pfsentry+1);
1932 *((u32 *)pfsee+2) = *((u32 *)pfsentry+2);
1933 *((u32 *)pfsee+3) = *((u32 *)pfsentry+3);
1936 }
while (pfsentry < pfseend);
1938 if ((fse->mode & sceMcFileAttrHidden) != 0) {
1942 if ((pcd == NULL) || (pcd->maxent < 0))
1947 if ((pcd == NULL) || (pcd->maxent < 0))
1948 return sceMcResSucceed;
1951 if ((pos == 1) && (!strncmp(filename,
".", 1))) {
1953 r = McReadDirEntry(port, slot, pfse->cluster, 0, &fse);
1954 if (r != sceMcResSucceed)
1958 pcd->cluster = fse->cluster;
1959 pcd->fsindex = fse->dir_entry;
1962 if ((fse->mode & sceMcFileAttrHidden) != 0) {
1966 if ((pcd == NULL) || (pcd->maxent < 0))
1970 if ((pcd == NULL) || (pcd->maxent < 0))
1971 return sceMcResSucceed;
1976 if ((pcd == NULL) || (pcd->maxent < 0))
1977 return sceMcResSucceed;
1982 if ((pcd) && (pcd->maxent >= 0))
1983 pcd->maxent = pfse->length;
1985 if (pfse->length > 0) {
1989 r = McReadDirEntry(port, slot, pfse->cluster, i, &fse);
1990 if (r != sceMcResSucceed)
1993 if (((fse->mode & sceMcFileAttrExists) == 0) && (pcd) && (i < pcd->maxent))
1997 if ((fse->mode & sceMcFileAttrExists) == 0)
2001 if ((fse->mode & sceMcFileAttrExists) != 0)
2008 if ((pos >= 11) && (!strncmp(&filename[10], &fse->name[10], pos-10))) {
2010 if (strlen(fse->name) >= (
unsigned int)pos)
2011 len = strlen(fse->name);
2013 if (!strncmp(filename, fse->name, len))
2014 goto continue_check;
2017 if (strlen(fse->name) >= (
unsigned int)pos)
2018 len = strlen(fse->name);
2022 if (strncmp(filename, fse->name, len))
2028 if ((fse->mode & sceMcFileAttrHidden) != 0) {
2037 pcd->cluster = pfse->cluster;
2039 if (pcd->maxent < 0)
2042 }
while ((u32)(++i) < pfse->length);
2048 return ((ret < 1) ? 1 : 0);
2052int mcman_writecluster(
int port,
int slot,
int cluster,
int flag)
2054 register int i, block;
2055 register u32 erase_value;
2056 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
2058 block = cluster / mcdi->clusters_per_block;
2060 if ((mcman_wr_port == port) && (mcman_wr_slot == slot) && (mcman_wr_block == block))
2061 return mcman_wr_flag3;
2063 mcman_wr_port = port;
2064 mcman_wr_slot = slot;
2065 mcman_wr_block = block;
2066 mcman_wr_flag3 = -9;
2068 for (i = 0; i < 16; i++) {
2069 if (mcdi->bad_block_list[i] < 0)
2071 if (mcdi->bad_block_list[i] == block) {
2073 return sceMcResSucceed;
2078 register int r, j, page, pageword_cnt;
2080 for (i = 1; i < mcdi->blocksize; i++)
2081 mcman_pagedata[i] = 0;
2085 pageword_cnt = mcdi->pagesize >> 2;
2086 page = block * mcdi->blocksize;
2088 if (mcdi->cardflags & CF_ERASE_ZEROES)
2089 erase_value = 0xffffffff;
2091 erase_value = 0x00000000;
2093 for (i = 0; i < pageword_cnt; i++)
2096 r = mcman_eraseblock(port, slot, block, (
void **)mcman_pagedata, (
void *)mcman_eccdata);
2097 if (r == sceMcResFailReplace)
2098 return sceMcResSucceed;
2099 if (r != sceMcResSucceed)
2100 return sceMcResChangedCard;
2102 for (i = 1; i < mcdi->blocksize; i++) {
2103 r = McWritePage(port, slot, page + i, &
mcman_pagebuf, mcman_eccdata);
2104 if (r == sceMcResFailReplace)
2105 return sceMcResSucceed;
2106 if (r != sceMcResSucceed)
2107 return sceMcResNoFormat;
2110 for (i = 1; i < mcdi->blocksize; i++) {
2112 if (r == sceMcResNoFormat)
2113 return sceMcResSucceed;
2114 if (r != sceMcResSucceed)
2115 return sceMcResFullDevice;
2117 for (j = 0; j < pageword_cnt; j++) {
2120 return sceMcResSucceed;
2125 r = mcman_eraseblock(port, slot, block, NULL, NULL);
2126 if (r != sceMcResSucceed)
2127 return sceMcResChangedCard;
2129 r = McWritePage(port, slot, page, &
mcman_pagebuf, mcman_eccdata);
2130 if (r == sceMcResFailReplace)
2131 return sceMcResSucceed;
2132 if (r != sceMcResSucceed)
2133 return sceMcResNoFormat;
2136 if (r == sceMcResNoFormat)
2137 return sceMcResSucceed;
2138 if (r != sceMcResSucceed)
2139 return sceMcResFullDevice;
2141 for (j = 0; j < pageword_cnt; j++) {
2144 return sceMcResSucceed;
2148 r = mcman_eraseblock(port, slot, block, NULL, NULL);
2149 if (r == sceMcResFailReplace)
2150 return sceMcResSucceed;
2151 if (r != sceMcResSucceed)
2152 return sceMcResFullDevice;
2154 erase_value = ~erase_value;
2156 for (i = 0; i < mcdi->blocksize; i++) {
2158 if (r != sceMcResSucceed)
2159 return sceMcResDeniedPermit;
2161 for (j = 0; j < pageword_cnt; j++) {
2164 return sceMcResSucceed;
2171 return mcman_wr_flag3;
2175int McSetDirEntryState(
int port,
int slot,
int cluster,
int fsindex,
int flags)
2177 register int r, i, fat_index;
2178 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
2182 r = McReadDirEntry(port, slot, cluster, fsindex, &fse);
2183 if (r != sceMcResSucceed)
2186 if (fse->name[0] ==
'.') {
2187 if ((fse->name[1] == 0) || (fse->name[1] ==
'.'))
2188 return sceMcResNoEntry;
2193 if (mcman_fdhandles[i].status == 0)
2196 if ((mcman_fdhandles[i].port != port) || (mcman_fdhandles[i].slot != slot))
2199 if (mcman_fdhandles[i].cluster != (u32)cluster)
2202 if (mcman_fdhandles[i].fsindex == (u32)fsindex)
2203 return sceMcResDeniedPermit;
2205 }
while (++i < MAX_FDHANDLES);
2208 fse->mode = fse->mode & (sceMcFileAttrExists - 1);
2210 fse->mode = fse->mode | sceMcFileAttrExists;
2212 Mc1stCacheEntSetWrFlagOff();
2214 fat_index = fse->cluster;
2216 if (fat_index >= 0) {
2217 if ((u32)fat_index < mcdi->unknown2)
2218 mcdi->unknown2 = fat_index;
2219 mcdi->unknown5 = -1;
2222 r = McGetFATentry(port, slot, fat_index, &fat_entry);
2223 if (r != sceMcResSucceed)
2227 fat_entry &= ~0x80000000;
2228 if ((u32)fat_index < mcdi->unknown2)
2229 mcdi->unknown2 = fat_entry;
2232 fat_entry |= 0x80000000;
2234 r = McSetFATentry(port, slot, fat_index, fat_entry);
2235 if (r != sceMcResSucceed)
2238 fat_index = fat_entry & ~0x80000000;
2240 }
while (fat_index != ~0x80000000);
2243 return sceMcResSucceed;
2247int mcman_checkBackupBlocks(
int port,
int slot)
2249 register int r1, r2, r, eccsize;
2250 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
2256 r1 = McReadPage(port, slot, mcdi->backup_block2 * mcdi->blocksize, &
mcman_pagebuf);
2259 if (((mcdi->cardflags & CF_ERASE_ZEROES) != 0) && (value1 == 0))
2260 value1 = 0xffffffff;
2261 if (value1 != 0xffffffff)
2262 value1 = value1 & ~0x80000000;
2264 r2 = McReadPage(port, slot, (mcdi->backup_block2 * mcdi->blocksize) + 1, &
mcman_pagebuf);
2267 if (((mcdi->cardflags & CF_ERASE_ZEROES) != 0) && (value2 == 0))
2268 value2 = 0xffffffff;
2269 if (value2 != 0xffffffff)
2270 value2 = value2 & ~0x80000000;
2272 if ((value1 != 0xffffffff) && (value2 == 0xffffffff))
2274 if ((r1 < 0) || (r2 < 0))
2277 if ((value1 == 0xffffffff) && (value1 == value2))
2278 return sceMcResSucceed;
2282 for (r1 = 0; (u32)r1 < mcdi->clusters_per_block; r1++) {
2284 McReadCluster(port, slot, (mcdi->backup_block1 * mcdi->clusters_per_block) + r1, &mce);
2287 for (r2 = 0; r2 < mcdi->pages_per_cluster; r2++) {
2288 mcman_pagedata[(r1 * ((mcdi->pages_per_cluster << 16) >> 16)) + r2] = \
2289 (
void *)(mce->cl_data + (r2 * mcdi->pagesize));
2294 r = mcman_eraseblock(port, slot, value1, (
void **)mcman_pagedata, (
void *)mcman_eccdata);
2295 if (r != sceMcResSucceed)
2299 for (r1 = 0; r1 < mcdi->blocksize; r1++) {
2301 eccsize = mcdi->pagesize;
2304 eccsize = eccsize >> 5;
2306 r = McWritePage(port, slot, (value1 * ((mcdi->blocksize << 16) >> 16)) + r1, \
2307 mcman_pagedata[r1], (
void *)(mcman_eccdata + (eccsize * r1)));
2309 if (r != sceMcResSucceed)
2313 for (r1 = 0; (u32)r1 < mcdi->clusters_per_block; r1++)
2314 mcman_freecluster(port, slot, (mcdi->backup_block1 * mcdi->clusters_per_block) + r1);
2318 return mcman_eraseblock(port, slot, mcdi->backup_block2, NULL, NULL);
2322int McCheckBlock(
int port,
int slot,
int block)
2324 register int r, i, j, page, ecc_count, pageword_cnt, flag, erase_value;
2325 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
2328 DPRINTF(
"McCheckBlock port%d slot%d block 0x%x\n", port, slot, block);
2331 page = block * mcdi->blocksize;
2332 pageword_cnt = mcdi->pagesize >> 2;
2334 ecc_count = mcdi->pagesize;
2335 if (mcdi->pagesize < 0)
2337 ecc_count = ecc_count >> 7;
2341 if (mcdi->cardform > 0) {
2342 for (i = 0; i < 16; i++) {
2343 if (mcdi->bad_block_list[i] <= 0)
2345 if (mcdi->bad_block_list[i] == block)
2352 for (i = 0; i < mcdi->blocksize; i++) {
2353 r = mcman_readpage(port, slot, page + i, &
mcman_pagebuf, mcman_eccdata);
2354 if (r != sceMcResSucceed)
2357 if ((mcdi->cardflags & CF_USE_ECC) != 0) {
2358 if (mcman_eccdata[mcman_sparesize(port, slot) - 1] != 0xff) {
2360 p_ecc = (
void *)mcman_eccdata;
2362 for (j = 0; j < ecc_count; j++) {
2363 r = mcman_correctdata(p_page, p_ecc);
2364 if (r != sceMcResSucceed) {
2368 p_ecc = (
void *)((u8 *)p_ecc + 3);
2369 p_page = (
void *)((u8 *)p_page + 128);
2375 for (j = 0; j < pageword_cnt; j++)
2378 for (j = 0; j < 128; j++)
2379 *((u32 *)&mcman_eccdata + j) = 0x5a5aa5a5;
2381 r = mcman_eraseblock(port, slot, block, NULL, NULL);
2382 if (r != sceMcResSucceed) {
2383 r = mcman_probePS2Card2(port, slot);
2384 if (r != sceMcResSucceed)
2390 for (i = 0; i < mcdi->blocksize; i++) {
2391 r = McWritePage(port, slot, page + i, &
mcman_pagebuf, mcman_eccdata);
2392 if (r != sceMcResSucceed) {
2393 r = mcman_probePS2Card2(port, slot);
2394 if (r != sceMcResSucceed)
2401 for (i = 0; i < mcdi->blocksize; i++) {
2402 r = mcman_readpage(port, slot, page + i, &
mcman_pagebuf, mcman_eccdata);
2403 if (r != sceMcResSucceed)
2406 for (j = 0; j < pageword_cnt; j++) {
2413 for (j = 0; j < 128; j++) {
2414 if (*((u32 *)&mcman_eccdata + j) != 0x5a5aa5a5) {
2421 for (j = 0; j < pageword_cnt; j++)
2424 for (j = 0; j < 128; j++)
2425 *((u32 *)&mcman_eccdata + j) = 0x05a55a5a;
2427 r = mcman_eraseblock(port, slot, block, NULL, NULL);
2428 if (r != sceMcResSucceed) {
2429 r = mcman_probePS2Card2(port, slot);
2430 if (r != sceMcResSucceed)
2436 for (i = 0; i < mcdi->blocksize; i++) {
2437 r = McWritePage(port, slot, page + i, &
mcman_pagebuf, mcman_eccdata);
2438 if (r != sceMcResSucceed) {
2439 r = mcman_probePS2Card2(port, slot);
2440 if (r != sceMcResSucceed)
2447 for (i = 0; i < mcdi->blocksize; i++) {
2448 r = mcman_readpage(port, slot, page + i, &
mcman_pagebuf, mcman_eccdata);
2449 if (r != sceMcResSucceed)
2452 for (j = 0; j < pageword_cnt; j++) {
2459 for (j = 0; j < 128; j++) {
2460 if (*((u32 *)&mcman_eccdata + j) != 0x05a55a5a) {
2469 mcman_eraseblock(port, slot, block, NULL, NULL);
2470 return sceMcResSucceed;
2473 erase_value = 0x00000000;
2474 if ((mcdi->cardflags & CF_ERASE_ZEROES) != 0)
2475 erase_value = 0xffffffff;
2477 for (j = 0; j < pageword_cnt; j++)
2480 for (j = 0; j < 128; j++)
2481 *((u32 *)&mcman_eccdata + j) = erase_value;
2483 for (i = 0; i < mcdi->blocksize; i++) {
2484 r = McWritePage(port, slot, page + i, &
mcman_pagebuf, mcman_eccdata);
2485 if (r != sceMcResSucceed) {
2486 r = mcman_probePS2Card2(port, slot);
2487 if (r != sceMcResSucceed)
2496int mcman_setPS1devinfos(
int port,
int slot)
2499 MCDevInfo *mcdi = &mcman_devinfos[port][slot];
2501 DPRINTF(
"mcman_setPS1devinfos port%d slot%d\n", port, slot);
2503 memset((
void *)mcdi, 0,
sizeof (
MCDevInfo));
2504 memset((
void *)&mcdi->bad_block_list[0], -1, 128);
2506 mcdi->pagesize = 128;
2507 mcdi->blocksize = 128;
2508 mcdi->pages_per_cluster = 64;
2509 mcdi->unused = 0xff00;
2510 mcdi->cluster_size = 8192;
2511 mcdi->FATentries_per_cluster = 2048;
2512 mcdi->clusters_per_card = 16;
2513 mcdi->clusters_per_block = 0;
2521#if !defined(BUILDING_XFROMMAN) && !defined(BUILDING_VMCMAN)
2522 if (mcman_sio2outbufs_PS1PDA[1] != 0)
2526 mcdi->cardform = -1;
2529 return sceMcResNoFormat;
2532 return sceMcResNoFormat;
2534 for (i = 0; i < 20; i++) {
2536 if (r != sceMcResSucceed)
2544 r = mcman_cachePS1dirs(port, slot);
2546 if (r != sceMcResSucceed)
2551 return sceMcResSucceed;
2555int mcman_getPS1direntry(
int port,
int slot,
const char *filename,
McFsEntryPS1 **pfse,
int flag)
2558 const char *p = filename;
2560 DPRINTF(
"mcman_getPS1direntry port%d slot%d file %s flag %x\n", port, slot, filename, flag);
2569 r = mcman_readdirentryPS1(port, slot, i, pfse);
2570 if (r != sceMcResSucceed)
2574 if (pfse[0]->mode != 0x51)
2578 if (pfse[0]->mode != 0xa1)
2582 if (!strcmp(p, pfse[0]->name))
2587 return sceMcResNoEntry;
2591int mcman_clearPS1direntry(
int port,
int slot,
int cluster,
int flags)
2593 register int r, i, temp;
2598 DPRINTF(
"mcman_clearPS1direntry port%d slot%d cluster %x flags %x\n", port, slot, cluster, flags);
2600 r = mcman_readdirentryPS1(port, slot, cluster, &fse);
2601 if (r != sceMcResSucceed)
2606 for (i = 0; i < MAX_FDHANDLES; i++) {
2607 if ((fh->status != 0) && (fh->port == port) && (fh->slot == slot)) {
2608 if (fh->freeclink == (u32)cluster)
2609 return sceMcResDeniedPermit;
2616 if (fse->mode != 0x51)
2617 return sceMcResNoEntry;
2620 if (fse->mode != 0xa1)
2621 return sceMcResNoEntry;
2626 mce = mcman_get1stcacheEntp();
2628 if (cluster + 1 < 0)
2633 temp &= ~0x00000007;
2634 temp = (cluster + 1) - temp;
2638 mce->wr_flag |= 1 << temp;
2641 temp = (fse->mode & 0xf) | 0xa0;
2643 temp = (fse->mode & 0xf) | 0x50;
2646 fse->edc = mcman_calcEDC((
void *)fse, 127);
2649 if (fse->linked_block < 0 || fse->linked_block >= 15) {
2654 cluster = fse->linked_block;
2656 r = mcman_readdirentryPS1(port, slot, cluster, &fse);
2657 if (r != sceMcResSucceed)
2661 if ((fse->mode & 0xf0) != 0x50)
2666 }
while ((fse->mode & 0xf0) == 0xa0);
2668 r = McFlushCache(port, slot);
2669 if (r != sceMcResSucceed)
2672 return sceMcResNoEntry;
2675 r = McFlushCache(port, slot);
2676 if (r != sceMcResSucceed)
2679 return sceMcResSucceed;
2683int mcman_findfree1(
int port,
int slot,
int reserve)
2685 register int i, free;
2688 DPRINTF(
"mcman_findfree1 port%d slot%d reserve %d\n", port, slot, reserve);
2696 r = mcman_readdirentryPS1(port, slot, i, &fse);
2697 if (r != sceMcResSucceed)
2700 if ((fse->mode & 0xf0) == 0xa0) {
2708 return sceMcResFullDevice;
2713 return sceMcResFullDevice;
2717int mcman_fatRseekPS1(
int fd)
2719 register int rpos, numclust, clust;
2721 MCDevInfo *mcdi = &mcman_devinfos[fh->port][fh->slot];
2724 numclust = fh->position / mcdi->cluster_size;
2726 clust = fh->freeclink;
2728 if (numclust > -1) {
2733 return sceMcResFullDevice;
2735 r = mcman_readdirentryPS1(fh->port, fh->slot, clust, &fse);
2736 if (r != sceMcResSucceed)
2738 clust = fse->linked_block;
2740 }
while (--numclust > -1);
2744 return sceMcResFullDevice;
2746 rpos = fh->position % mcdi->cluster_size;
2751 return ((clust + 1) << 6) + ((rpos >> 10) * (1024 / mcdi->pagesize));
2755int mcman_fatWseekPS1(
int fd)
2757 register int r, numclust;
2759 MCDevInfo *mcdi = &mcman_devinfos[fh->port][fh->slot];
2762 numclust = fh->position / mcdi->cluster_size;
2767 clust = fh->freeclink;
2769 r = mcman_readdirentryPS1(fh->port, fh->slot, clust, &fse);
2770 if (r != sceMcResSucceed)
2773 clust = fse->linked_block;
2776 r = mcman_FNC8ca4(fh->port, fh->slot, (
MC_FHANDLE *)fh);
2781 }
while (--numclust);
2783 r = McFlushCache(fh->port, fh->slot);
2789int mcman_FNC8ca4(
int port,
int slot,
MC_FHANDLE *fh)
2791 register int r, i, j, mcfree, cluster_size, pages_per_FATclust;
2793 MCDevInfo *mcdi = &mcman_devinfos[fh->port][fh->slot];
2797 DPRINTF(
"mcman_FNC8ca4 port%d slot%d\n", port, slot);
2799 if ((
int)(mcdi->cluster_size) < 0)
2800 cluster_size = mcdi->cluster_size + 1023;
2802 cluster_size = mcdi->cluster_size;
2804 cluster_size = cluster_size >> 10;
2806 pages_per_FATclust = 1024 / mcdi->pagesize;
2808 r = mcman_findfree1(port, slot, 1);
2818 while (i >= 0 && i < 15) {
2820 u8 *pfsentry, *pfsee, *pfseend;
2822 if (cluster_size > 0) {
2826 mcman_freecluster(port, slot, ((mcfree * cluster_size) + i) * pages_per_FATclust);
2828 r = mcman_readclusterPS1(port, slot, ((i * cluster_size) + i) * pages_per_FATclust, &mce);
2829 if (r != sceMcResSucceed)
2833 mce->cluster -= (i - mcfree) * cluster_size;
2835 }
while (++i < cluster_size);
2838 r = mcman_readdirentryPS1(port, slot, i, &fse1);
2839 if (r != sceMcResSucceed)
2842 r = mcman_readdirentryPS1(port, slot, mcfree, &fse2);
2843 if (r != sceMcResSucceed)
2846 mce = mcman_get1stcacheEntp();
2853 temp &= ~0x00000007;
2854 temp = (mcfree + 1) - temp;
2858 mce->wr_flag |= 1 << temp;
2860 pfsentry = (u8 *)fse1;
2865 *((u32 *)pfsee ) = *((u32 *)pfsentry );
2866 *((u32 *)pfsee+1) = *((u32 *)pfsentry+1);
2867 *((u32 *)pfsee+2) = *((u32 *)pfsentry+2);
2868 *((u32 *)pfsee+3) = *((u32 *)pfsentry+3);
2871 }
while (pfsentry < pfseend);
2874 r = mcman_readdirentryPS1(port, slot, j, &fse3);
2875 if (r != sceMcResSucceed)
2878 mce = mcman_get1stcacheEntp();
2892 temp &= ~0x00000007;
2893 temp = (j + 1) - temp;
2897 mce->wr_flag |= 1 << temp;
2898 fse3->linked_block = mcfree;
2899 fse3->edc = mcman_calcEDC((
void *)fse3, 127);
2902 fh->freeclink = mcfree;
2910 r = mcman_readdirentryPS1(port, slot, j, &fse1);
2911 if (r != sceMcResSucceed)
2914 i = fse1->linked_block;
2918 r = mcman_readdirentryPS1(port, slot, mcfree, &fse2);
2919 if (r != sceMcResSucceed)
2922 mce = mcman_get1stcacheEntp();
2929 temp &= ~0x00000007;
2930 temp = (mcfree + 1) - temp;
2934 mce->wr_flag |= 1 << temp;
2939 fse2->linked_block = -1;
2940 fse2->edc = mcman_calcEDC((
void *)fse2, 127);
2942 r = mcman_readdirentryPS1(port, slot, j, &fse3);
2943 if (r != sceMcResSucceed)
2946 mce = mcman_get1stcacheEntp();
2953 temp &= ~0x00000007;
2954 temp = (j + 1) - temp;
2958 mce->wr_flag |= 1 << temp;
2960 if (fse3->mode == 0x53)
2963 fse3->linked_block = mcfree;
2964 fse3->edc = mcman_calcEDC((
void *)fse3, 127);
2970int mcman_PS1pagetest(
int port,
int slot,
int page)
2974 for (i = 0; i < 32; i++)
2978 if (r != sceMcResSucceed)
2979 return sceMcResDeniedPermit;
2982 if (r != sceMcResSucceed)
2983 return sceMcResNotEmpty;
2985 for (i = 0; i < 32; i++) {
2991 for (i = 0; i < 32; i++)
2995 if (r != sceMcResSucceed)
2996 return sceMcResDeniedPermit;
2999 if (r != sceMcResSucceed)
3000 return sceMcResNotEmpty;
3002 for (i = 0; i < 32; i++) {
3012int mcman_cachePS1dirs(
int port,
int slot)
3014 register int i, j, temp1, temp2, index, linked_block;
3019 DPRINTF(
"mcman_cachePS1dirs port%d slot%d\n", port, slot);
3025 r = mcman_readdirentryPS1(port, slot, i, &fs_t[i]);
3026 if (r != sceMcResSucceed)
3029 fs_t[i]->field_38 = fs_t[i]->length;
3032 mce[0] = mcman_get1stcacheEntp();
3036 mce[1] = mcman_get1stcacheEntp();
3040 memset((
void *)cluster_t, -1,
sizeof(cluster_t));
3044 temp1 = fs_t[i]->mode & 0xf0;
3046 if ((fs_t[i]->mode & 0xf) != 1)
3051 linked_block = fs_t[i]->linked_block;
3053 while (linked_block >= 0 && linked_block < 15) {
3054 if ((fs_t[linked_block]->mode & 0xf0) != temp1)
3057 if (fs_t[linked_block]->mode == 0xa0)
3060 if (cluster_t[linked_block] != -1)
3063 cluster_t[linked_block] = i;
3064 linked_block = fs_t[linked_block]->linked_block;
3067 if ((linked_block < 0 || linked_block >= 15) && (temp1 != 0))
3072 if (cluster_t[j] != i)
3077 fs_t[j]->mode = 0xa0;
3078 fs_t[j]->linked_block = -1;
3079 fs_t[j]->edc = mcman_calcEDC((
void *)fs_t[j], 127);
3093 temp1 &= ~0x00000007;
3094 temp1 = temp2 - temp1;
3098 mce[index]->wr_flag |= 1 << temp1;
3106 if ((cluster_t[i] != -1) || (fs_t[i]->mode == 0xa0))
3111 fs_t[i]->mode = 0xa0;
3112 fs_t[i]->linked_block = cluster_t[i];
3113 fs_t[i]->edc = mcman_calcEDC((
void *)fs_t[i], 127);
3127 temp1 &= ~0x00000007;
3128 temp1 = temp2 - temp1;
3132 mce[index]->wr_flag |= 1 << temp1;
3136 return sceMcResSucceed;
3140int mcman_fillPS1backuparea(
int port,
int slot,
int block)
3142 register int r, i, curpage;
3143 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
3150 if (mcdi->bad_block_list[i] == block) {
3151 mcdi->bad_block_list[i] = block | 0x01000000;
3155 if (r != sceMcResSucceed)
3159 if (mcdi->bad_block_list[i] < 0) {
3163 if (r != sceMcResSucceed)
3165 mcdi->bad_block_list[i] = block;
3172 return sceMcResSucceed;
3176void mcman_initcache(
void)
3181 DPRINTF(
"mcman_initcache\n");
3183 j = MAX_CACHEENTRY - 1;
3184 p = (u8 *)mcman_cachebuf;
3186 for (i = 0; i < MAX_CACHEENTRY; i++) {
3187 mcman_entrycache[i].cl_data = (u8 *)p;
3188 mcman_mccache[i] = (
McCacheEntry *)&mcman_entrycache[j - i];
3189 mcman_entrycache[i].cluster = -1;
3190 p += MCMAN_CLUSTERSIZE;
3196 for (i = 0; i < MCMAN_MAXSLOT; i++) {
3197 mcman_devinfos[0][i].unknown3 = -1;
3198 mcman_devinfos[0][i].unknown4 = -1;
3199 mcman_devinfos[0][i].unknown5 = -1;
3200 mcman_devinfos[1][i].unknown3 = -1;
3201 mcman_devinfos[1][i].unknown4 = -1;
3202 mcman_devinfos[1][i].unknown5 = -1;
3205 memset((
void *)mcman_fatcache, -1,
sizeof (mcman_fatcache));
3207 for (i = 0; i < MCMAN_MAXSLOT; i++) {
3208 mcman_fatcache[0][i].entry[0] = 0;
3209 mcman_fatcache[1][i].entry[0] = 0;
3214int McRetOnly(
int fd)
3218 DPRINTF(
"McRetOnly param %x\n", fd);
3219 return sceMcResSucceed;
3223int mcman_clearcache(
int port,
int slot)
3229 DPRINTF(
"mcman_clearcache port%d, slot%d\n", port, slot);
3231 for (i = MAX_CACHEENTRY - 1; i >= 0; i--) {
3233 if ((mce->mc_port == port) && (mce->mc_slot == slot) && (mce->cluster >= 0)) {
3241 for (i = 0; i < (MAX_CACHEENTRY - 1); i++) {
3246 if (mce->cluster < 0) {
3247 for (j = i+1; j < MAX_CACHEENTRY; j++) {
3249 if (mce->cluster >= 0)
3252 if (j == MAX_CACHEENTRY)
3260 memset((
void *)&mcman_fatcache[port][slot], -1,
sizeof (
McFatCache));
3262 mcman_fatcache[port][slot].entry[0] = 0;
3264 return sceMcResSucceed;
3268McCacheEntry *mcman_getcacheentry(
int port,
int slot,
int cluster)
3275 for (i = 0; i < MAX_CACHEENTRY; i++) {
3276 if ((mce->mc_port == port) && (mce->mc_slot == slot) && (mce->cluster == cluster))
3285void mcman_freecluster(
int port,
int slot,
int cluster)
3290 for (i = 0; i < MAX_CACHEENTRY; i++) {
3291 if ((mce->mc_port == port) && (mce->mc_slot == slot) && (mce->cluster == cluster)) {
3300int mcman_getFATindex(
int port,
int slot,
int num)
3302 return mcman_fatcache[port][slot].entry[num];
3306void Mc1stCacheEntSetWrFlagOff(
void)
3316 return *pmcman_mccache;
3325 i = MAX_CACHEENTRY - 1;
3344 pmce[i] = pmce[i-1];
3352int McFlushCache(
int port,
int slot)
3357 DPRINTF(
"McFlushCache port%d slot%d\n", port, slot);
3359 i = MAX_CACHEENTRY - 1;
3366 if ((mce->mc_port == port) && (mce->mc_slot == slot) && (mce->wr_flag != 0)) {
3370 if (r != sceMcResSucceed)
3377 return sceMcResSucceed;
3383 register int r, i, j, ecc_count;
3384 register int temp1, temp2, offset, pageindex;
3385 static int clusters_per_block, blocksize, cardtype, pagesize, sparesize, flag, cluster, block, pages_per_fatclust;
3389 static u8 eccbuf[32];
3390 void *p_page, *p_ecc;
3392 DPRINTF(
"mcman_flushcacheentry mce %x cluster %x\n", (
int)mce, (
int)mce->cluster);
3394 if (mce->wr_flag == 0)
3395 return sceMcResSucceed;
3397 mcdi = (
MCDevInfo *)&mcman_devinfos[mce->mc_port][mce->mc_slot];
3400 pagesize = mcdi->pagesize;
3401 cardtype = mcdi->cardtype;
3403 if (cardtype == 0) {
3405 return sceMcResSucceed;
3408 if ((cardtype == sceMcTypePS1) || (cardtype == sceMcTypePDA)) {
3409 pages_per_fatclust = MCMAN_CLUSTERSIZE / pagesize;
3414 if (pages_per_fatclust > 0) {
3418 if (((mce->wr_flag >> j) & 1) != 0) {
3420 r = McWritePS1PDACard(mce->mc_port, mce->mc_slot, i, (
void *)(mce->cl_data + (pagesize * j)));
3421 if (r == sceMcResFailReplace)
3423 if (r != sceMcResSucceed)
3428 }
while (++j < pages_per_fatclust);
3431 if (j == pages_per_fatclust) {
3432 r = mcman_probePS1Card2(mce->mc_port, mce->mc_slot);
3434 r = sceMcResFailReplace;
3436 if (cardtype != sceMcTypePS1) {
3437 if (r == sceMcResFailReplace)
3441 return sceMcResSucceed;
3445 return sceMcResSucceed;
3448 if ((mce->wr_flag & ~(((u32)(-1)) << j)) == 0) {
3450 return sceMcResSucceed;
3455 return sceMcResSucceed;
3461 if (((mce->wr_flag >> j) & 1) != 0) {
3462 r = mcman_fillPS1backuparea(mce->mc_port, mce->mc_slot, i);
3463 if (r != sceMcResSucceed)
3464 return sceMcResFailReplace;
3471 return sceMcResSucceed;
3474 clusters_per_block = mcdi->clusters_per_block;
3475 block = mce->cluster / mcdi->clusters_per_block;
3476 blocksize = mcdi->blocksize;
3477 sparesize = mcman_sparesize(mce->mc_port, mce->mc_slot);
3480 memset((
void *)pmce, 0, 64);
3483 if (MAX_CACHEENTRY > 0) {
3486 if ((mcee->mc_slot == mce->mc_slot) && (mcee->mc_port == mce->mc_port)) {
3487 temp1 = mcee->cluster / clusters_per_block;
3488 temp2 = mcee->cluster % clusters_per_block;
3490 if (temp1 == block) {
3492 if (mcee->rd_flag == 0)
3497 }
while (++i < MAX_CACHEENTRY);
3500 if (clusters_per_block > 0) {
3503 cluster = block * clusters_per_block;
3509 for (j = 0; j < mcdi->pages_per_cluster; j++) {
3510 mcman_pagedata[pageindex + j] = (
void *)(pmce[i]->cl_data + offset);
3519 offset = (pageindex + j) * pagesize;
3520 mcman_pagedata[pageindex + j] = (
void *)(mcman_backupbuf + offset);
3522 r = McReadPage(mce->mc_port, mce->mc_slot, \
3523 ((cluster + i) * mcdi->pages_per_cluster) + j, \
3524 mcman_backupbuf + offset);
3525 if (r != sceMcResSucceed)
3528 }
while (++j < mcdi->pages_per_cluster);
3531 pageindex += mcdi->pages_per_cluster;
3532 }
while (++i < clusters_per_block);
3536 if ((flag != 0) && (mcman_badblock <= 0)) {
3537 r = mcman_eraseblock(mce->mc_port, mce->mc_slot, mcdi->backup_block1, (
void**)mcman_pagedata, mcman_eccdata);
3538 if (r == sceMcResFailReplace) {
3540 r = mcman_replaceBackupBlock(mce->mc_port, mce->mc_slot, mcdi->backup_block1);
3541 mcdi->backup_block1 = r;
3544 if (r != sceMcResSucceed)
3549 p_ecc = (
void *)eccbuf;
3554 ecc_count = (pagesize + 0x7f) >> 7;
3556 ecc_count = pagesize >> 7;
3561 McDataChecksum(p_page, p_ecc);
3563 p_ecc = (
void *)((u8 *)p_ecc + 3);
3564 p_page = (
void *)((u8 *)p_page + 128);
3569 r = McWritePage(mce->mc_port, mce->mc_slot, mcdi->backup_block2 * blocksize, &
mcman_pagebuf, eccbuf);
3570 if (r == sceMcResFailReplace)
3572 if (r != sceMcResSucceed)
3575 if (r < mcdi->blocksize) {
3577 p_ecc = (
void *)mcman_eccdata;
3580 r = McWritePage(mce->mc_port, mce->mc_slot, (mcdi->backup_block1 * blocksize) + i, mcman_pagedata[i], p_ecc);
3581 if (r == sceMcResFailReplace)
3583 if (r != sceMcResSucceed)
3585 p_ecc = (
void *)((u8 *)p_ecc + sparesize);
3586 }
while (++i < mcdi->blocksize);
3589 r = McWritePage(mce->mc_port, mce->mc_slot, (mcdi->backup_block2 * blocksize) + 1, &
mcman_pagebuf, eccbuf);
3590 if (r == sceMcResFailReplace)
3592 if (r != sceMcResSucceed)
3596 r = mcman_eraseblock(mce->mc_port, mce->mc_slot, block, (
void**)mcman_pagedata, mcman_eccdata);
3599 if (r == sceMcResFailReplace) {
3600 r = mcman_fillbackupblock1(mce->mc_port, mce->mc_slot, block, (
void**)mcman_pagedata, mcman_eccdata);
3601 for (i = 0; i < clusters_per_block; i++) {
3603 pmce[i]->wr_flag = 0;
3605 if (r == sceMcResFailReplace)
3609 if (r != sceMcResSucceed)
3612 if (mcdi->blocksize > 0) {
3614 p_ecc = (
void *)mcman_eccdata;
3617 if (pmce[i / mcdi->pages_per_cluster] == 0) {
3618 r = McWritePage(mce->mc_port, mce->mc_slot, (block * blocksize) + i, mcman_pagedata[i], p_ecc);
3619 if (r == sceMcResFailReplace) {
3620 r = mcman_fillbackupblock1(mce->mc_port, mce->mc_slot, block, (
void**)mcman_pagedata, mcman_eccdata);
3621 for (i = 0; i < clusters_per_block; i++) {
3623 pmce[i]->wr_flag = 0;
3625 if (r == sceMcResFailReplace)
3629 if (r != sceMcResSucceed)
3632 p_ecc = (
void *)((u8 *)p_ecc + sparesize);
3633 }
while (++i < mcdi->blocksize);
3636 if (mcdi->blocksize > 0) {
3638 p_ecc = (
void *)mcman_eccdata;
3641 if (pmce[i / mcdi->pages_per_cluster] != 0) {
3642 r = McWritePage(mce->mc_port, mce->mc_slot, (block * blocksize) + i, mcman_pagedata[i], p_ecc);
3643 if (r == sceMcResFailReplace) {
3644 r = mcman_fillbackupblock1(mce->mc_port, mce->mc_slot, block, (
void**)mcman_pagedata, mcman_eccdata);
3645 for (i = 0; i < clusters_per_block; i++) {
3647 pmce[i]->wr_flag = 0;
3649 if (r == sceMcResFailReplace)
3653 if (r != sceMcResSucceed)
3656 p_ecc = (
void *)((u8 *)p_ecc + sparesize);
3657 }
while (++i < mcdi->blocksize);
3660 if (clusters_per_block > 0) {
3664 pmce[i]->wr_flag = 0;
3665 }
while (++i < clusters_per_block);
3668 if ((flag != 0) && (mcman_badblock <= 0)) {
3669 r = mcman_eraseblock(mce->mc_port, mce->mc_slot, mcdi->backup_block2, NULL, NULL);
3670 if (r == sceMcResFailReplace) {
3673 if (r != sceMcResSucceed)
3679 r = mcman_replaceBackupBlock(mce->mc_port, mce->mc_slot, mcdi->backup_block2);
3680 mcdi->backup_block2 = r;
3684 return sceMcResSucceed;
3688int McReadCluster(
int port,
int slot,
int cluster,
McCacheEntry **pmce)
3691 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
3694 if (mcman_badblock > 0) {
3695 register int block, block_offset;
3697 block = cluster / mcdi->clusters_per_block;
3698 block_offset = cluster % mcdi->clusters_per_block;
3700 if ((block == mcman_badblock) && (mcman_badblock_port == port) && (mcman_badblock_slot == slot)) {
3701 cluster = (mcdi->backup_block1 * mcdi->clusters_per_block) + block_offset;
3705 for (i = 0; (u32)i < mcdi->clusters_per_block; i++) {
3706 if ((mcman_replacementcluster[i] != 0) && (mcman_replacementcluster[i] == cluster)) {
3707 block_offset = i % mcdi->clusters_per_block;
3708 cluster = (mcdi->backup_block1 * mcdi->clusters_per_block) + block_offset;
3715 mce = mcman_getcacheentry(port, slot, cluster);
3719 mce = pmcman_mccache[MAX_CACHEENTRY - 1];
3721 if (mce->wr_flag != 0) {
3723 if (r != sceMcResSucceed)
3727 mce->mc_port = port;
3728 mce->mc_slot = slot;
3729 mce->cluster = cluster;
3733 for (i = 0; i < mcdi->pages_per_cluster; i++) {
3734 r = McReadPage(port, slot, (cluster * mcdi->pages_per_cluster) + i, \
3735 (
void *)(mce->cl_data + (i * mcdi->pagesize)));
3736 if (r != sceMcResSucceed)
3741 mcman_addcacheentry(mce);
3744 return sceMcResSucceed;
3748int McReadDirEntry(
int port,
int slot,
int cluster,
int fsindex,
McFsEntry **pfse)
3751 static int maxent, index, clust;
3752 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
3753 register McFatCache *fci = &mcman_fatcache[port][slot];
3756 DPRINTF(
"McReadDirEntry port%d slot%d cluster %d fsindex %d\n", port, slot, cluster, fsindex);
3758 maxent = 0x402 / (mcdi->cluster_size >> 9);
3759 index = fsindex / (mcdi->cluster_size >> 9);
3763 if ((cluster == 0) && (index != 0)) {
3764 if (index < maxent) {
3765 if ((fci->entry[index]) >= 0 )
3766 clust = fci->entry[index];
3772 if (fci->entry[i] < 0)
3774 clust = fci->entry[i];
3775 }
while (++i < index);
3782 r = McGetFATentry(port, slot, clust, &clust);
3783 if (r != sceMcResSucceed)
3786 if ((
unsigned int)clust == 0xffffffff)
3787 return sceMcResNoEntry;
3788 clust &= ~0x80000000;
3793 fci->entry[i] = clust;
3795 }
while (i < index);
3798 r = McReadCluster(port ,slot, mcdi->alloc_offset + clust, &mce);
3799 if (r != sceMcResSucceed)
3802 *pfse = (
McFsEntry *)(mce->cl_data + ((fsindex % (mcdi->cluster_size >> 9)) << 9));
3804 return sceMcResSucceed;
3808int mcman_readdirentryPS1(
int port,
int slot,
int cluster,
McFsEntryPS1 **pfse)
3810 register int r, offset, index, pages_per_fatclust;
3812 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
3815 if (cluster < 0 || cluster >= 15)
3818 pages_per_fatclust = MCMAN_CLUSTERSIZE / mcdi->pagesize;
3821 index = cluster / pages_per_fatclust;
3822 offset = cluster % pages_per_fatclust;
3824 r = mcman_readclusterPS1(port, slot, index * pages_per_fatclust, &mce);
3825 if (r != sceMcResSucceed)
3828 *pfse = (
void *)&mce->cl_data[offset << 7];
3830#ifdef BUILDING_XMCMAN
3835 return sceMcResSucceed;
3839int McSetFATentry(
int port,
int slot,
int fat_index,
int fat_entry)
3841 register int r, ifc_index, indirect_index, indirect_offset, fat_offset;
3843 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
3847 indirect_index = fat_index / mcdi->FATentries_per_cluster;
3848 fat_offset = fat_index % mcdi->FATentries_per_cluster;
3850 ifc_index = indirect_index / mcdi->FATentries_per_cluster;
3851 indirect_offset = indirect_index % mcdi->FATentries_per_cluster;
3853 r = McReadCluster(port, slot, mcdi->ifc_list[ifc_index], &mce);
3854 if (r != sceMcResSucceed)
3859 r = McReadCluster(port, slot, fc->entry[indirect_offset], &mce);
3860 if (r != sceMcResSucceed)
3865 fc->entry[fat_offset] = fat_entry;
3868 return sceMcResSucceed;
3872int McGetFATentry(
int port,
int slot,
int fat_index,
int *fat_entry)
3874 register int r, ifc_index, indirect_index, indirect_offset, fat_offset;
3876 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
3878 indirect_index = fat_index / mcdi->FATentries_per_cluster;
3879 fat_offset = fat_index % mcdi->FATentries_per_cluster;
3881 ifc_index = indirect_index / mcdi->FATentries_per_cluster;
3882 indirect_offset = indirect_index % mcdi->FATentries_per_cluster;
3884 r = McReadCluster(port, slot, mcdi->ifc_list[ifc_index], &mce);
3885 if (r != sceMcResSucceed)
3890 r = McReadCluster(port, slot, fc->entry[indirect_offset], &mce);
3891 if (r != sceMcResSucceed)
3896 *fat_entry = fc->entry[fat_offset];
3898 return sceMcResSucceed;
3902int mcman_readclusterPS1(
int port,
int slot,
int cluster,
McCacheEntry **pmce)
3904 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
3907 mce = mcman_getcacheentry(port, slot, cluster);
3909 register int r, i, pages_per_fatclust;
3911 mce = pmcman_mccache[MAX_CACHEENTRY - 1];
3913 if (mce->wr_flag != 0) {
3915 if (r != sceMcResSucceed)
3919 mce->mc_port = port;
3920 mce->mc_slot = slot;
3921 mce->cluster = cluster;
3923 pages_per_fatclust = MCMAN_CLUSTERSIZE / mcdi->pagesize;
3925 for (i = 0; i < pages_per_fatclust; i++) {
3926 r = McReadPS1PDACard(port, slot, cluster + i, (
void *)(mce->cl_data + (i * mcdi->pagesize)));
3927 if (r != sceMcResSucceed)
3932 mcman_addcacheentry(mce);
3935 return sceMcResSucceed;
3939int mcman_replaceBackupBlock(
int port,
int slot,
int block)
3942 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
3944 if (mcman_badblock > 0)
3945 return sceMcResFailReplace;
3947 for (i = 0; i < 16; i++) {
3948 if (mcdi->bad_block_list[i] == -1)
3953 if ((mcdi->alloc_end - mcdi->max_allocatable_clusters) < 8)
3954 return sceMcResFullDevice;
3956 mcdi->alloc_end -= 8;
3957 mcdi->bad_block_list[i] = block;
3958 mcman_badblock_port = port;
3959 mcman_badblock_slot = slot;
3960 mcman_badblock = -1;
3962 return (mcdi->alloc_offset + mcdi->alloc_end) / mcdi->clusters_per_block;
3965 return sceMcResFullDevice;
3969int mcman_fillbackupblock1(
int port,
int slot,
int block,
void **pagedata,
void *eccdata)
3971 register int r, i, sparesize, page_offset;
3972 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
3975 DPRINTF(
"mcman_fillbackupblock1 port%d slot%d block %x mcman_badblock %x\n", port, slot, block, mcman_badblock);
3977 sparesize = mcman_sparesize(port, slot);
3979 if (mcman_badblock != 0) {
3980 if ((mcman_badblock != block) || (mcman_badblock_port != port) || (mcman_badblock_slot != slot))
3981 return sceMcResFailReplace;
3984 if ((mcdi->alloc_offset / mcdi->clusters_per_block) == (u32)block)
3985 return sceMcResFailReplace;
3987 r = mcman_eraseblock(port, slot, mcdi->backup_block2, NULL, NULL);
3988 if (r != sceMcResSucceed)
3991 r = mcman_eraseblock(port, slot, mcdi->backup_block1, NULL, NULL);
3992 if (r != sceMcResSucceed)
3995 for (i = 0; i < 16; i++) {
3996 if (mcdi->bad_block_list[i] == -1)
4001 return sceMcResFailReplace;
4003 page_offset = mcdi->backup_block1 * mcdi->blocksize;
4004 p_ecc = (u8 *)eccdata;
4006 for (i = 0; i < mcdi->blocksize; i++) {
4007 r = McWritePage(port, slot, page_offset + i, pagedata[i], p_ecc);
4008 if (r != sceMcResSucceed)
4013 mcman_badblock_port = port;
4014 mcman_badblock_slot = slot;
4015 mcman_badblock = block;
4019 mcman_replacementcluster[i] = 0;
4022 return sceMcResSucceed;
4026int McReplaceBadBlock(
void)
4028 register int r, i, curentry, clust, index, offset, numifc, fat_length, temp, length;
4029 register int value, value2, cluster, index2, offset2, s3;
4030 register MCDevInfo *mcdi = &mcman_devinfos[mcman_badblock_port][mcman_badblock_slot];
4036 DPRINTF(
"McReplaceBadBlock mcman_badblock_port%d mcman_badblock_slot%d mcman_badblock %x\n", mcman_badblock_port, mcman_badblock_slot, mcman_badblock);
4038 if (mcman_badblock == 0)
4039 return sceMcResSucceed;
4041 if (mcman_badblock >= 0) {
4042 McFlushCache(mcman_badblock_port, mcman_badblock_slot);
4043 mcman_clearcache(mcman_badblock_port, mcman_badblock_slot);
4045 for (i = 0; i <16; i++) {
4046 if (mcdi->bad_block_list[i] == -1)
4052 if (mcdi->alloc_end >= (mcdi->max_allocatable_clusters + 8)) {
4053 mcdi->max_allocatable_clusters += 8;
4054 mcdi->bad_block_list[i] = mcman_badblock;
4057 fat_length = (((mcdi->clusters_per_card << 2) - 1) / mcdi->cluster_size) + 1;
4058 numifc = (((fat_length << 2) - 1) / mcdi->cluster_size) + 1;
4062 fat_length = mcdi->FATentries_per_cluster << 5;
4066 value = ~(((u32)(-1)) << mcdi->clusters_per_block);
4068 clust = (mcman_badblock * mcdi->clusters_per_block) + i;
4069 if ((u32)clust < mcdi->alloc_offset) {
4073 r = McGetFATentry(mcman_badblock_port, mcman_badblock_slot, clust - mcdi->alloc_offset, &fat_entry[i]);
4074 if (r != sceMcResSucceed)
4077 if (((fat_entry[i] & 0x80000000) == 0) || ((fat_entry[i] < -1) && (fat_entry[i] >= -9)))
4080 }
while ((u32)(++i) < mcdi->clusters_per_block);
4082 if (mcdi->clusters_per_block > 0) {
4085 if ((value & (1 << i)) != 0) {
4086 r = mcman_findfree2(mcman_badblock_port, mcman_badblock_slot, 1);
4088 mcman_replacementcluster[i] = r;
4091 r += mcdi->alloc_offset;
4092 mcman_replacementcluster[i] = r;
4094 }
while ((u32)(++i) < mcdi->clusters_per_block);
4097 if (mcdi->clusters_per_block > 0) {
4100 if ((value & (1 << i)) != 0) {
4101 if (fat_entry[i] != 0) {
4102 index = ((fat_entry[i] & ~0x80000000) + mcdi->alloc_offset) / mcdi->clusters_per_block;
4103 offset = ((fat_entry[i] & ~0x80000000) + mcdi->alloc_offset) % mcdi->clusters_per_block;
4104 if (index == mcman_badblock) {
4105 fat_entry[i] = (mcman_replacementcluster[offset] - mcdi->alloc_offset) | 0x80000000;
4109 }
while ((u32)(++i) < mcdi->clusters_per_block);
4112 if (mcdi->clusters_per_block > 0) {
4115 if ((mcman_replacementcluster[i] != 0) && (fat_entry[i] != 0)) {
4116 r = McSetFATentry(mcman_badblock_port, mcman_badblock_slot, mcman_replacementcluster[i] + mcdi->alloc_offset, fat_entry[i]);
4117 if (r != sceMcResSucceed)
4120 }
while ((u32)(++i) < mcdi->clusters_per_block);
4123 for (i = 0; i < numifc; i++) {
4124 index = mcdi->ifc_list[i] / mcdi->clusters_per_block;
4125 offset = mcdi->ifc_list[i] % mcdi->clusters_per_block;
4127 if (index == mcman_badblock) {
4128 value &= ~(1 << offset);
4129 mcdi->ifc_list[i] = mcman_replacementcluster[i];
4136 for (i = 0; i < fat_length; i++) {
4137 index = i / mcdi->FATentries_per_cluster;
4138 offset = i % mcdi->FATentries_per_cluster;
4141 r = McReadCluster(mcman_badblock_port, mcman_badblock_slot, mcdi->ifc_list[index], &mce);
4142 if (r != sceMcResSucceed)
4145 offset = i % mcdi->FATentries_per_cluster;
4146 index2 = *((u32 *)&mce->cl_data[offset]) / mcdi->clusters_per_block;
4147 offset2 = *((u32 *)&mce->cl_data[offset]) % mcdi->clusters_per_block;
4149 if (index2 == mcman_badblock) {
4150 value &= ~(1 << offset2);
4151 *((u32 *)&mce->cl_data[offset]) = mcman_replacementcluster[offset2];
4156 McFlushCache(mcman_badblock_port, mcman_badblock_slot);
4157 mcman_clearcache(mcman_badblock_port, mcman_badblock_slot);
4164 for (i = 0; (u32)i < mcdi->alloc_end; i++) {
4165 r = McGetFATentry(mcman_badblock_port, mcman_badblock_slot, i, &fat_entry2);
4166 if (r != sceMcResSucceed)
4169 index = (u32)(((fat_entry2 & ~0x80000000) + mcdi->alloc_offset) / mcdi->clusters_per_block);
4170 offset = (u32)(((fat_entry2 & ~0x80000000) + mcdi->alloc_offset) % mcdi->clusters_per_block);
4172 if (index == mcman_badblock) {
4173 value &= ~(1 << offset);
4174 r = McSetFATentry(mcman_badblock_port, mcman_badblock_slot, i, (mcman_replacementcluster[offset] - mcdi->alloc_offset) | 0x80000000);
4175 if (r != sceMcResSucceed)
4180 if (value2 != value)
4181 McFlushCache(mcman_badblock_port, mcman_badblock_slot);
4183 r = McReadDirEntry(mcman_badblock_port, mcman_badblock_slot, 0, 0, &fse);
4184 if (r != sceMcResSucceed)
4189 length = fse->length;
4192 if (curentry >= length)
4195 r = McReadDirEntry(mcman_badblock_port, mcman_badblock_slot, s3, curentry, &fse);
4196 if (r != sceMcResSucceed)
4199 cluster = fse->cluster;
4200 index = (fse->cluster + mcdi->alloc_offset) / mcdi->clusters_per_block;
4201 offset = (fse->cluster + mcdi->alloc_offset) % mcdi->clusters_per_block;
4205 if (index == mcman_badblock) {
4206 fse->cluster = mcman_replacementcluster[offset] - mcdi->alloc_offset;
4207 Mc1stCacheEntSetWrFlagOff();
4210 if ((fse->mode & sceMcFileAttrSubdir) == 0) {
4215 r = McReadDirEntry(mcman_badblock_port, mcman_badblock_slot, cluster, 0, &fse);
4216 if (r != sceMcResSucceed)
4219 if ((fse->mode & sceMcFileAttrSubdir) == 0) {
4224 if (fse->cluster != 0) {
4229 if ((
int)(fse->dir_entry) != curentry) {
4236 if (strcmp(fse->name,
"."))
4245 r = McReadDirEntry(mcman_badblock_port, mcman_badblock_slot, s3, 1, &fse);
4246 if (r != sceMcResSucceed)
4249 index = (fse->cluster + mcdi->alloc_offset) / mcdi->clusters_per_block;
4250 offset = (fse->cluster + mcdi->alloc_offset) % mcdi->clusters_per_block;
4252 if (index == mcman_badblock) {
4253 fse->cluster = mcman_replacementcluster[offset] - mcdi->alloc_offset;
4254 Mc1stCacheEntSetWrFlagOff();
4257 r = McReadDirEntry(mcman_badblock_port, mcman_badblock_slot, fse->cluster, fse->dir_entry, &fse);
4258 if (r != sceMcResSucceed)
4261 length = fse->length;
4263 r = McReadDirEntry(mcman_badblock_port, mcman_badblock_slot, s3, 0, &fse);
4264 if (r != sceMcResSucceed)
4268 index = (fse->cluster + mcdi->alloc_offset) / mcdi->clusters_per_block;
4269 offset = (fse->cluster + mcdi->alloc_offset) % mcdi->clusters_per_block;
4270 curentry = fse->dir_entry + 1;
4272 if (index == mcman_badblock) {
4273 fse->cluster = mcman_replacementcluster[offset] - mcdi->alloc_offset;
4274 Mc1stCacheEntSetWrFlagOff();
4284 if (mcdi->clusters_per_block > 0) {
4287 clust = (mcman_badblock * mcdi->clusters_per_block) + i;
4288 if ((u32)clust >= mcdi->alloc_offset) {
4289 r = McSetFATentry(mcman_badblock_port, mcman_badblock_slot, clust - mcdi->alloc_offset, 0xfffffffd);
4290 if (r != sceMcResSucceed)
4293 }
while ((u32)(++i) < mcdi->clusters_per_block);
4296 McFlushCache(mcman_badblock_port, mcman_badblock_slot);
4297 mcman_clearcache(mcman_badblock_port, mcman_badblock_slot);
4301 if (mcdi->clusters_per_block > 0) {
4304 if (mcman_replacementcluster[i] != 0) {
4305 r = McReadCluster(mcman_badblock_port, mcman_badblock_slot, (mcdi->backup_block1 * mcdi->clusters_per_block) + i, &mce);
4306 if (r != sceMcResSucceed)
4309 mce->cluster = mcman_replacementcluster[i];
4312 }
while ((u32)(++i) < mcdi->clusters_per_block);
4315 McFlushCache(mcman_badblock_port, mcman_badblock_slot);
4321 r = mcman_clearsuperblock(mcman_badblock_port, mcman_badblock_slot);
4326 return sceMcResFailReplace;
4330int mcman_clearsuperblock(
int port,
int slot)
4333 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
4337 memset((
void *)mcdi->magic, 0, sizeof (mcdi->magic) + sizeof (mcdi->version));
4338 strcpy(mcdi->magic, SUPERBLOCK_MAGIC);
4339 strcat(mcdi->magic, SUPERBLOCK_VERSION);
4341 for (i = 0; (u32)((
unsigned int)i <
sizeof(
MCDevInfo)); i += 1024) {
4342 register int size, temp;
4347 r = McReadCluster(port, slot, temp >> 10, &mce);
4348 if (r != sceMcResSucceed)
4357 memcpy((
void *)mce->cl_data, (
void *)mcdi, size);
4360 r = McFlushCache(port, slot);
int CpuResumeIntr(int state)
int CpuSuspendIntr(int *state)
u32 count
start sector of fragmented bd/file