rendered paste body#include <pspiofilemgr.h>
#include <pspsdk.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspidstorage.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <psppower.h>
/* http://psp.jim.sh/pspsdk-doc/group__Utils.html include SHA-1 checks on dump/restore */
void ReadKey(u16 key, void *buffer)
{
sceIdStorageReadLeaf(key, buffer);
return;
}
void WriteKey(u16 key, void *buffer)
{
sceIdStorageWriteLeaf(key, buffer);
sceIdStorageFlush();
return;
}
int CheckDummy(int key)
{
char buffdummy[512];
char buffdcmp[512];
int resdummy;
memset(buffdummy, 0, 512);
memset(buffdcmp, 0, 512);
sceIdStorageReadLeaf(key, buffdummy);
resdummy = memcmp(buffdummy, buffdcmp, 512);
if (resdummy == 0) return 0;
else return -1;
}
int CheckHW(void)
{
char chwbuff[512];
sceIdStorageReadLeaf(0x0050, chwbuff);
if (chwbuff[33] == 0x02) return -1;
else if (chwbuff[33] == 0x01) return 0;
else return -2;
}
int BackupKeyToFile(u16 key, char filepath[50])
{
char dumpbuff[512];
memset(dumpbuff, 0, 512);
int f = sceIoOpen(filepath, PSP_O_WRONLY | PSP_O_CREAT, 0777);
if (f < 0) return -1;
sceIdStorageReadLeaf(key, dumpbuff);
sceIoWrite(f, dumpbuff, 0x200);
sceIoClose(f);
return 0;
}
int BrickPSP(void)
{
char brickbuff[512];
if (sceIdStorageIsReadOnly()) return -1;
memset(brickbuff, 0, 512);
sceIdStorageWriteLeaf(0x0005, brickbuff);
sceIdStorageWriteLeaf(0x0006, brickbuff);
return 0;
}
int chkDowngrade(void)
{
char dgbuff[512];
sceIdStorageReadLeaf(0x0005, dgbuff);
if (dgbuff[0] == 0x6E)
{
if (dgbuff[13] == 0x01) return 0;
}
else
{
return -1;
}
}
int chkByte(u16 key, u32 offset, char val)
{
int buffbyte;
sceIdStorageLookup(key, offset, &buffbyte, 1);
if(val == buffbyte) return 0;
else return -1;
}
int chkRegion(void)
{
int reg;
sceIdStorageLookup(0x100, 61, ®, 1);
if (reg == 0x00) return -1;
else if (reg == 0x03) return 3;
else if (reg == 0x04) return 4;
else if (reg == 0x05) return 5;
else if (reg == 0x06) return 6;
else if (reg == 0x0A) return 7;
else if (reg == 0x0B) return 8;
else if (reg == 0x09) return 9;
else if (reg == 0x0C) return 10;
else if (reg == 0x0D) return 11;
else return reg;
}
int dgTA082(void)
{
u8 leaf[512];
memset(leaf, 0, 512);
if (chkDowngrade() < 0) return -1;
if (scePowerGetBatteryLifePercent() < 75) return -2;
if (sceIdStorageIsReadOnly()) return -4;
if (CheckHW() == -1) {
sceIdStorageReadLeaf(0x0006, leaf);
sceIdStorageWriteLeaf(0x0042, leaf);
sceIdStorageReadLeaf(0x0005, leaf);
sceIdStorageWriteLeaf(0x0046, leaf);
sceIdStorageReadLeaf(0x0005, leaf);
sceIdStorageWriteLeaf(0x0006, leaf);
sceIdStorageReadLeaf(0x0004, leaf);
sceIdStorageWriteLeaf(0x0005, leaf);
return 0;
}
else return -3;
}
int RestoreKeyFromFile(u16 key, char filepath[50])
{
char buffer[512];
int f;
memset(buffer, 0, 512);
if (sceIdStorageIsReadOnly()) return -2;
f = sceIoOpen(filepath, PSP_O_RDONLY, 0777);
if(f <= 0) {
return -1;
}
sceIoRead(f, buffer, 512);
sceIdStorageWriteLeaf(key, buffer);
sceIoClose(f);
return 0;
}
int compareKeyBuffer(u16 key, void *buffer)
{
char buff[512];
int x;
sceIdStorageReadLeaf(key, buff);
x = memcmp(buff, buffer, 512);
if (x == 0) return 0;
else return -1;
}
int getMAC(void *buffer)
{
sceIdStorageLookup(0x044, 0, buffer, 6);
return 0;
}
int getSerial(void *buffer)
{
sceIdStorageLookup(0x050, 0, buffer, 30);
return 0;
}
int BackupIdStorageKeys(char folderpath[70], char successpath[70], char errorpath[70], char unknownpath[70])
{
char buffer[512];
int x;
char y[33];
char z[33];
char a[50];
char b[33];
u16 key;
sceIoMkdir(folderpath, 0777);
int f = sceIoOpen(successpath, PSP_O_WRONLY | PSP_O_CREAT, 0777);
if (f < 0) return -1;
int g = sceIoOpen(errorpath, PSP_O_WRONLY | PSP_O_CREAT, 0777);
if (g < 0) return -1;
for(key=0x0001;key<=0x9999;key++)
{
x = sceIdStorageReadLeaf(key, buffer);
if (x < 0) {
sprintf(y, "Error: Key 0x%04X does not exist.\n", key);
sceIoWrite(g, y, 32);
}
else if (x == 0) {
sprintf(z, "Success: Key 0x%04X DOES exist.\n", key);
sprintf(a, "%s0x%04X.bin", folderpath, key);
BackupKeyToFile(key, a);
sceIoWrite(f, z, 30);
}
else {
int h = sceIoOpen(unknownpath, PSP_O_WRONLY | PSP_O_CREAT, 0777);
if (h < 0) return -1;
sprintf(b, "Unknown error on key 0x%04X.\n", key);
sceIoWrite(h, b, 34);
sceIoClose(h);
}
}
sceIoClose(f);
sceIoClose(g);
return 0;
}
int RestoreIdStorageKeys(char folderpath[70], char successpath[70], char errorpath[70])
{
char a[50];
char b[33];
char buffer[512];
char leaf[512];
int r = 0;
int f;
u16 key;
if (sceIdStorageIsReadOnly()) return -1;
sceKernelDelayThread(4*1000*1000);
for(key=0x0001;key<=0x9999;key++)
{
int x = sceIdStorageReadLeaf(key, buffer);
if (x == 0) {
sprintf(a, "%s0x%04X.bin", folderpath, key);
f = sceIoOpen(a, PSP_O_RDONLY, 0777);
if(f < 0)
{
sprintf(b, "Could not find: 0x%04X.bin", key);
int y = sceIoOpen(errorpath, PSP_O_WRONLY | PSP_O_CREAT, 0777);
sceIoWrite(y, b, 32);
sceIoClose(y);
r--;
}
else {
sceIoRead(f, leaf, 512);
sceIdStorageWriteLeaf(key, leaf);
sprintf(a, "Success key 0x%04X.bin", key);
int z = sceIoOpen(successpath, PSP_O_WRONLY | PSP_O_CREAT, 0777);
sceIoWrite(z, a, 34);
sceKernelDelayThread(2000 * 2000);
sceIoClose(z);
sceIoClose(f);
}
}
else if (x < 0)
{
sprintf(a, "%s0x%04X.bin", folderpath, key);
f = sceIoOpen(a, PSP_O_RDONLY, 0777);
if (f < 0) sceIoClose(f);
else {
sceIoRead(f, leaf, 512);
sceIdStorageCreateLeaf(key);
sceIdStorageWriteLeaf(key, leaf);
sprintf(a, "Successfully created and wrote 0x%04X.bin\n", key);
int z = sceIoOpen(successpath, PSP_O_WRONLY | PSP_O_CREAT, 0777);
sceIoWrite(z, a, 40);
sceIoClose(f);
sceIoClose(z);
}
}
}
return r;
}