2020-10-03 15:22:44 +00:00
|
|
|
#include "ultra64.h"
|
|
|
|
#include "global.h"
|
2020-07-07 00:15:01 +00:00
|
|
|
|
|
|
|
s32 osPfsInitPak(OSMesgQueue* queue, OSPfs* pfs, s32 channel) {
|
2020-08-23 21:50:30 +00:00
|
|
|
s32 ret;
|
2020-07-07 00:15:01 +00:00
|
|
|
u16 sum;
|
|
|
|
u16 isum;
|
|
|
|
u8 temp[BLOCKSIZE];
|
|
|
|
__OSPackId* id;
|
|
|
|
__OSPackId newid;
|
|
|
|
|
|
|
|
__osSiGetAccess();
|
|
|
|
|
|
|
|
ret = __osPfsGetStatus(queue, channel);
|
|
|
|
|
|
|
|
__osSiRelAccess();
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfs->queue = queue;
|
|
|
|
pfs->channel = channel;
|
|
|
|
pfs->status = 0;
|
|
|
|
|
|
|
|
if ((ret = __osPfsCheckRamArea(pfs)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if ((ret = __osPfsSelectBank(pfs, 0)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if ((ret = __osContRamRead(pfs->queue, pfs->channel, PFS_ID_0AREA, temp)) != 0) {
|
|
|
|
return (ret);
|
|
|
|
}
|
2020-08-23 21:50:30 +00:00
|
|
|
|
2020-07-07 00:15:01 +00:00
|
|
|
__osIdCheckSum((u16*)temp, &sum, &isum);
|
|
|
|
id = (__OSPackId*)temp;
|
|
|
|
if ((id->checksum != sum) || (id->invertedChecksum != isum)) {
|
|
|
|
if ((ret = __osCheckPackId(pfs, id)) != 0) {
|
|
|
|
pfs->status |= PFS_ID_BROKEN;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((id->deviceid & 0x01) == 0) {
|
|
|
|
ret = __osRepairPackId(pfs, id, &newid);
|
|
|
|
if (ret) {
|
|
|
|
if (ret == PFS_ERR_ID_FATAL) {
|
|
|
|
pfs->status |= PFS_ID_BROKEN;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
id = &newid;
|
|
|
|
if ((id->deviceid & 0x01) == 0) {
|
|
|
|
return PFS_ERR_DEVICE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bcopy(id, pfs->id, BLOCKSIZE);
|
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
if (1) {}
|
2020-08-23 21:50:30 +00:00
|
|
|
|
|
|
|
pfs->version = id->version;
|
2020-07-07 00:15:01 +00:00
|
|
|
pfs->banks = id->banks;
|
|
|
|
pfs->inodeStartPage = 1 + DEF_DIR_PAGES + (2 * pfs->banks);
|
|
|
|
pfs->dir_size = DEF_DIR_PAGES * PFS_ONE_PAGE;
|
|
|
|
pfs->inode_table = 1 * PFS_ONE_PAGE;
|
|
|
|
pfs->minode_table = (1 + pfs->banks) * PFS_ONE_PAGE;
|
|
|
|
pfs->dir_table = pfs->minode_table + (pfs->banks * PFS_ONE_PAGE);
|
|
|
|
|
2020-08-23 21:50:30 +00:00
|
|
|
if ((ret = __osContRamRead(pfs->queue, pfs->channel, PFS_LABEL_AREA, pfs->label)) != 0) {
|
2020-07-07 00:15:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = osPfsChecker(pfs);
|
|
|
|
pfs->status |= PFS_INITIALIZED;
|
2020-08-23 21:50:30 +00:00
|
|
|
|
2020-07-07 00:15:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 __osPfsCheckRamArea(OSPfs* pfs) {
|
|
|
|
s32 i = 0;
|
|
|
|
s32 ret = 0;
|
|
|
|
u8 temp1[BLOCKSIZE];
|
|
|
|
u8 temp2[BLOCKSIZE];
|
|
|
|
u8 saveReg[BLOCKSIZE];
|
|
|
|
|
|
|
|
if ((ret = __osPfsSelectBank(pfs, PFS_ID_BANK_256K)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if ((ret = __osContRamRead(pfs->queue, pfs->channel, 0, saveReg)) != 0) {
|
2021-02-14 00:49:40 +00:00
|
|
|
return ret;
|
2020-07-07 00:15:01 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < BLOCKSIZE; i++) {
|
|
|
|
temp1[i] = i;
|
|
|
|
}
|
|
|
|
if ((ret = __osContRamWrite(pfs->queue, pfs->channel, 0, temp1, 0)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if ((ret = __osContRamRead(pfs->queue, pfs->channel, 0, temp2)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (bcmp(temp1, temp2, BLOCKSIZE) != 0) {
|
|
|
|
return PFS_ERR_DEVICE;
|
|
|
|
}
|
2021-02-14 00:49:40 +00:00
|
|
|
return __osContRamWrite(pfs->queue, pfs->channel, 0, saveReg, 0);
|
2020-07-07 00:15:01 +00:00
|
|
|
}
|