12#include "mcman-internal.h"
14static int mcman_PS1curcluster;
15static char mcman_PS1curdir[64];
18int mcman_format1(
int port,
int slot)
21 register MCDevInfo *mcdi = &mcman_devinfos[port][slot];
23 DPRINTF(
"mcman_format1 port%d slot%d\n", port, slot);
25 mcman_invhandles(port, slot);
29 for (i = 0; i < 56; i++) {
30 r = mcman_PS1pagetest(port, slot, i);
32 return sceMcResNoFormat;
44 for (i = 1; i < 15; i++) {
46 if (r != sceMcResSucceed)
54 for (i = 0; i < 20; i++) {
59 if (r != sceMcResSucceed)
70 if (r != sceMcResSucceed)
75 return sceMcResSucceed;
79int mcman_open1(
int port,
int slot,
const char *filename,
int flags)
81 register int r, i, fd = 0, cluster, temp;
85 const char *p = filename;
87 DPRINTF(
"mcman_open1 port%d slot%d filename %s flags %x\n", port, slot, filename, flags);
89 if ((flags & sceMcFileCreateFile) != 0)
90 flags |= sceMcFileAttrWriteable;
92 for (fd = 0; fd < MAX_FDHANDLES; fd++) {
98 if (fd == MAX_FDHANDLES)
101 mcman_wmemset((
void *)fh,
sizeof (
MC_FHANDLE), 0);
109 if ((*p == 0) || ((*p ==
'.') && (p[1] == 0))) {
116 r = mcman_getPS1direntry(port, slot, p, &fse, 1);
117 if ((fse->mode & 0xf0) == 0xa0)
123 if (fse->field_7d == 1)
124 fh->filesize = fse->field_38;
126 fh->filesize = fse->length;
131 fh->rdflag = flags & sceMcFileAttrReadable;
132 fh->wrflag = flags & sceMcFileAttrWriteable;
135 if ((flags & sceMcFileCreateFile) == 0) {
137 return sceMcResNoEntry;
143 for (i = 0; i < MAX_FDHANDLES; i++) {
144 if ((mcman_fdhandles[i].status != 0) && \
145 (mcman_fdhandles[i].port == port) && (mcman_fdhandles[i].slot == slot) && \
146 (mcman_fdhandles[i].freeclink == (u32)r) && (fh->wrflag != 0))
147 return sceMcResDeniedPermit;
151 r = mcman_clearPS1direntry(port, slot, r, 0);
152 if (r != sceMcResSucceed)
157 for (i = 0; i < 15; i++) {
158 r = mcman_readdirentryPS1(port, slot, i, &fse);
159 if (r != sceMcResSucceed)
162 if ((fse->mode & 0xf0) == 0xa0) {
166 if (fse->mode != 0xa0) {
170 fse->linked_block = -1;
171 fse->edc = mcman_calcEDC((
void *)fse, 127);
173 mce = mcman_get1stcacheEntp();
188 temp = (i + 1) - temp;
195 mce->wr_flag |= 1 << temp;
200 r = McFlushCache(port, slot);
201 if (r != sceMcResSucceed)
205 return sceMcResFullDevice;
207 r = mcman_readdirentryPS1(port, slot, cluster, &fse);
208 if (r != sceMcResSucceed)
215 fse->linked_block = -1;
217 strncpy(fse->name, p, 20);
219 if ((flags & sceMcFileAttrPDAExec) != 0)
222 fse->edc = mcman_calcEDC((
void *)fse, 127);
223 fh->freeclink = cluster;
225 mce = mcman_get1stcacheEntp();
229 if ((cluster + 1) < 0)
240 temp = (cluster + 1) - temp;
244 mce->wr_flag |= 1 << temp;
246 r = McFlushCache(port, slot);
247 if (r != sceMcResSucceed)
257int mcman_read1(
int fd,
void *buffer,
int nbyte)
262 u8 *p = (u8 *)buffer;
264 if (fh->position >= fh->filesize)
267 if ((u32)nbyte >= (fh->filesize - fh->position))
268 nbyte = fh->filesize - fh->position;
273 register int r, size, temp, offset, maxsize;
275 if ((
int)(fh->position) < 0)
276 temp = fh->position + 0x3ff;
280 offset = (fh->position - (temp & ~0x000003ff));
281 maxsize = MCMAN_CLUSTERSIZE - offset;
287 r = mcman_fatRseekPS1(fd);
291 r = mcman_readclusterPS1(fh->port, fh->slot, r, &mce);
292 if (r != sceMcResSucceed)
295 memcpy(&p[rpos], (
void *)(mce->cl_data + offset), size);
299 fh->position += size;
308int mcman_write1(
int fd,
void *buffer,
int nbyte)
310 register int r, wpos;
313 u8 *p = (u8 *)buffer;
316 if (fh->unknown2 == 0) {
318 r = mcman_close1(fd);
319 if (r != sceMcResSucceed)
321 r = McFlushCache(fh->port, fh->slot);
322 if (r != sceMcResSucceed)
331 register int size, temp, offset, maxsize;
333 r = mcman_fatRseekPS1(fd);
335 if (r == sceMcResFullDevice) {
336 r = mcman_fatWseekPS1(fd);
337 if (r == sceMcResFullDevice)
339 if (r!= sceMcResSucceed)
342 r = mcman_fatRseekPS1(fd);
349 r = mcman_readclusterPS1(fh->port, fh->slot, r, &mce);
350 if (r != sceMcResSucceed)
353 if ((
int)(fh->position) < 0)
354 temp = fh->position + 0x3ff;
358 offset = fh->position - (temp & ~0x000003ff);
359 maxsize = MCMAN_CLUSTERSIZE - offset;
365 memcpy((
void *)(mce->cl_data + offset), &p[wpos], size);
368 fh->position += size;
369 if (fh->position >= fh->filesize)
370 fh->filesize = fh->position;
385int mcman_dread1(
int fd, MC_IO_DRE_T *dirent)
390 if (fh->position < fh->filesize) {
394 r = mcman_readdirentryPS1(fh->port, fh->slot, fh->position, &fse);
395 if (r != sceMcResSucceed)
398 if (fse->mode == 0x51)
401 }
while (++fh->position < fh->filesize);
404 if (fh->position >= fh->filesize)
408 mcman_wmemset((
void *)dirent,
sizeof(MC_IO_DRE_T), 0);
410 strncpy(dirent->name, fse->name, 20);
411 dirent->name[20] = 0;
413 dirent->stat.mode = MC_IO_S_RD | MC_IO_S_WR | MC_IO_S_EX | MC_IO_S_FL;
415#if !MCMAN_ENABLE_EXTENDED_DEV_OPS
416 dirent->stat.mode |= sceMcFileAttrDupProhibit | sceMcFileAttrPS1;
418 if (fse->field_7e == 1)
419 dirent->stat.mode |= sceMcFileAttrPDAExec;
422 if (fse->field_7d == 1) {
425 dirent->stat.size = fse->field_38;
426 dirent->stat.attr = fse->field_28;
429 dirent->stat.size = fse->length;
436int mcman_getstat1(
int port,
int slot,
const char *filename, MC_IO_STA_T *stat)
441 DPRINTF(
"mcman_getstat1 port%d slot%d filename %s\n", port, slot, filename);
443 r = mcman_getPS1direntry(port, slot, filename, &fse, 1);
445 if ((fse->mode & 0xf0) == 0xa0)
449 return sceMcResNoEntry;
451 mcman_wmemset(stat,
sizeof(MC_IO_STA_T), 0);
453 stat->mode = MC_IO_S_RD | MC_IO_S_WR | MC_IO_S_EX | MC_IO_S_FL;
455#if !MCMAN_ENABLE_EXTENDED_DEV_OPS
456 stat->mode |= sceMcFileAttrDupProhibit;
459 if (fse->field_7d == 1) {
461#if !MCMAN_ENABLE_EXTENDED_DEV_OPS
462 if ((fse->field_2c & sceMcFileAttrClosed) != 0)
463 stat->mode |= sceMcFileAttrClosed;
469 stat->size = fse->field_38;
470 stat->attr = fse->field_28;
472 return sceMcResSucceed;
475 stat->size = fse->length;
477 return sceMcResSucceed;
481int mcman_setinfo1(
int port,
int slot,
const char *filename,
sceMcTblGetDir *
info,
int flags)
483 register int r, temp, ret;
484#ifdef BUILDING_XMCMAN
490 DPRINTF(
"mcman_setinfo1 port%d slot%d filename %s flags %x\n", port, slot, filename, flags);
493#ifdef BUILDING_XMCMAN
494 if ((flags & sceMcFileAttrFile) != 0) {
495 r = mcman_getPS1direntry(port, slot,
info->EntryName, &fse1, 1);
497 if (r != sceMcResNoEntry) {
501 if ((!strcmp(
".",
info->EntryName)) || (!strcmp(
"..",
info->EntryName)) || (
info->EntryName[0] == 0))
502 ret = sceMcResNoEntry;
508 r = mcman_getPS1direntry(port, slot, filename, &fse2, 1);
510 if ((fse2->mode & 0xf0) == 0)
516 if (ret != sceMcResSucceed)
517 return sceMcResNoEntry;
519 mce = mcman_get1stcacheEntp();
529 temp = (r + 1) - temp;
533 mce->wr_flag |= 1 << temp;
535#ifdef BUILDING_XMCMAN
540 if(fse2->field_7d != 1) {
542 fse2->field_38 = fse2->length;
546 if ((flags & sceMcFileAttrExecutable) != 0) {
547 if ((
info->AttrFile & sceMcFileAttrPDAExec) != 0)
560 flags = sceMcFileAttrReadable|sceMcFileAttrWriteable;
564 if ((flags & sceMcFileAttrDupProhibit) != 0)
565 fse2->field_28 =
info->Reserve2;
567 if ((flags & sceMcFileAttrReadable) != 0)
568 fse2->created =
info->_Create;
570 if ((flags & sceMcFileAttrWriteable) != 0)
571 fse2->modified =
info->_Modify;
573 if ((flags & sceMcFileAttrFile) != 0)
574 strncpy(fse2->name,
info->EntryName, 20);
578 r = McFlushCache(port, slot);
584int mcman_getdir1(
int port,
int slot,
const char *dirname,
int flags,
int maxent,
sceMcTblGetDir *
info)
589 DPRINTF(
"mcman_getdir1 port%d slot%d dirname %s maxent %d flags %x\n", port, slot, dirname, maxent, flags);
598 mcman_PS1curcluster = 0;
601 r = mcman_chrpos(p,
'/');
604 p += mcman_chrpos(p,
'/') + 1;
607 strncpy(mcman_PS1curdir, p, 63);
613 if (mcman_PS1curcluster >= 15)
616 r = mcman_readdirentryPS1(port, slot, mcman_PS1curcluster, &fse);
617 if (r != sceMcResSucceed)
620 mcman_PS1curcluster++;
622 if (fse->mode != 0x51)
625 if (!mcman_checkdirpath(fse->name, mcman_PS1curdir))
630 info->AttrFile = 0x9417;
632 if (fse->field_7e == 1)
633 info->AttrFile = 0x9c17;
635 if (fse->field_7d == 1) {
636 if ((fse->field_2c & sceMcFileAttrClosed) != 0) {
637 info->AttrFile |= sceMcFileAttrClosed;
639 info->Reserve1 = fse->field_2e;
640 info->_Create = fse->created;
641 info->_Modify = fse->modified;
642 info->FileSizeByte = fse->field_38;
643 info->Reserve2 = fse->field_28;
646 info->FileSizeByte = fse->length;
649 strncpy(
info->EntryName, fse->name, 20);
650 info->EntryName[20] = 0;
661int mcman_delete1(
int port,
int slot,
const char *filename,
int flags)
666 DPRINTF(
"mcman_delete1 port%d slot%d filename %s flags %x\n", port, slot, filename, flags);
668 r = mcman_getPS1direntry(port, slot, filename, &fse, ((u32)flags < 1) ? 1 : 0);
672 r = mcman_clearPS1direntry(port, slot, r, flags);
678int mcman_close1(
int fd)
680 register int r, temp;
685 DPRINTF(
"mcman_close1 fd %d\n", fd);
687 r = mcman_readdirentryPS1(fh->port, fh->slot, fh->freeclink, &fse);
688#ifdef BUILDING_XMCMAN
689 if (r != sceMcResSucceed)
693 mce = mcman_get1stcacheEntp();
695 if ((
int)(fh->freeclink + 1) < 0)
696 temp = fh->freeclink + 8;
698 temp = fh->freeclink + 1;
701 temp = (fh->freeclink + 1) - temp;
705 mce->wr_flag |= 1 << temp;
707 if (fh->filesize == 0) {
708 fse->length = 0x2000;
711 if ((
int)(fh->filesize - 1) < 0)
712 temp = (fh->filesize + 8190) >> 13;
714 temp = (fh->filesize - 1) >> 13;
717 fse->length = temp << 13;
720#ifdef BUILDING_XMCMAN
724 memset((
void *)&fse->created, 0, 8);
725 memset((
void *)&fse->modified, 0, 8);
729 fse->field_38 = fh->filesize;
730 mcman_getmcrtime(&fse->modified);
733 fse->edc = mcman_calcEDC((
void *)fse, 127);
735 r = McFlushCache(fh->port, fh->slot);
736 if (r != sceMcResSucceed)
739 return sceMcResSucceed;
743int mcman_unformat1(
int port,
int slot)
748 DPRINTF(
"mcman_unformat1 port%d slot%d\n", port, slot);
751 for (i = 0; i < 32; i++)
754 for (i = 0; i < 1024; i++) {
756 if (r != sceMcResSucceed)