-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
316 lines (250 loc) · 8.05 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* ================================================================ *\
* *
* >> Savegame Deemer << *
* a tool to read and write back savegames *
* with the ability to gather the unique game key for FW2.00+ games *
* *
* by ---==> HELLCAT <==--- *
* *
\* ================================================================ */
// //
// System Hook Module (CFW Plugin) //
// taking care of system call patching and gathering the gamekey //
////////////////////////////////////////////////////////////////////////
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //
// THIS SOURCE IS PUBLISHED UNDER THE GPL //
// everything released based on this MUST //
// be released containing it's source and //
// be under the GPL as well //
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //
#include <pspsdk.h>
#include <pspdisplay.h>
#include <pspiofilemgr.h>
#include <pspkernel.h>
#include <psputility.h>
#include <pspctrl.h>
#include <pspsysmem_kernel.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "deemerd.h"
PSP_MODULE_INFO("hcSGDeemerSysHook", 0x1006, 1, 11);
PSP_MAIN_THREAD_ATTR(0);
//#define printf pspDebugScreenPrintf
//////////////////////////////////////
// //
// Declarations and definitions //
// //
//////////////////////////////////////
typedef struct SceUtilitySavedataSavenames
{
char name1[20];
char name2[20];
char name3[20];
char name4[20];
char name5[20];
char terminator[20];
} SceUtilitySavedataSavenames;
typedef struct SceUtilitySavedataParamEx
{
/*
This is almost completely a copy of SceUtilitySavedataParam
from psputility_savedata.h, exept for some additions to include
the gamekey for 2.00+ games and the pointer to multiple savenames
*/
pspUtilityDialogCommon base;
int mode;
int unknown12;
int unknown13;
char gameName[16];
char saveName[20];
SceUtilitySavedataSavenames* saveNames;
char fileName[16];
void *dataBuf;
SceSize dataBufSize;
SceSize dataSize;
PspUtilitySavedataSFOParam sfoParam;
PspUtilitySavedataFileData icon0FileData;
PspUtilitySavedataFileData icon1FileData;
PspUtilitySavedataFileData pic1FileData;
PspUtilitySavedataFileData snd0FileData;
unsigned char unknown17[4];
unsigned char unknown18[20];
unsigned char gamekey[16];
unsigned char unknown19[20];
} SceUtilitySavedataParamEx;
SceUtilitySavedataParamEx* SDParamBuffer;
///////////////////
// //
// Functions //
// //
///////////////////
void hcSanitycheckFilename(char* filename)
{
/*
Some games happened to put weird stuff into the params fields
used for creating the filenames when they didn't need them.
So, let's perform a check and filter out any illegal chars....
*/
int i,j, ok;
char allowedchars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-./:";
for(i=0; i<strlen(filename); i++)
{
ok = 0;
for(j=0; j<=sizeof(allowedchars); j++)
{
if( filename[i] == allowedchars[j] ) { ok = 1; }
}
if( ok == 0 ) { filename[i] = 0x30; }
}
}
int hcReadFile(char* file, void* buf, int size)
{
hcSanitycheckFilename(file);
SceUID fd = sceIoOpen(file, PSP_O_RDONLY, 0777);
if (fd < 0)
{
return -1;
}
int read = sceIoRead(fd, buf, size);
if (sceIoClose(fd) < 0)
return -1;
return read;
}
int hcWriteFile(char* file, void* buffer, int size)
{
int i;
int pathlen = 0;
char path[128];
hcSanitycheckFilename(file);
// because I'm so lazy, I need folders created "on the fly"
// this does so, create every folder in the path of the
// file we are to save.
// A little bruty forcy, yah, but it does the job :-D
for(i=1; i<(strlen(file)); i++)
{
if(strncmp(file+i-1, "/", 1) == 0)
{
pathlen=i-1;
strncpy(path, file, pathlen);
path[pathlen] = 0;
sceIoMkdir(path, 0777);
}
}
// now up to the file write....
SceUID fd = sceIoOpen(file, PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777);
if (fd < 0)
{
return -1;
}
int written = sceIoWrite(fd, buffer, size);
if (sceIoClose(fd) < 0)
return -1;
return written;
}
/*void debugIssueError(char* msg)
{
pspDebugScreenInit();
while( 1==1 ){ pspDebugScreenSetXY(0,0); printf("%s",msg); }
}
void debugIssueMessage(char* msg, int frames)
{
int i = 0;
pspDebugScreenInit();
while( i<frames )
{
sceDisplayWaitVblankStart();
pspDebugScreenSetXY(0,0);
printf("%s",msg);
i++;
}
}*/
int hcDeemerCapturedSDParamsCallback(int arg1, int arg2, void *arg)
{
char s[512];
SceUtilitySavedataParamEx* SDParams;
PspUtilitySavedataSFOParam sfoParams;
int patchSFO = 0;
int btn;
SceCtrlData pad;
// check if user holds the button to trigger overwriting/patching
// the SFO stuff
sceKernelDelayThread(500000);
sceCtrlReadBufferPositive(&pad, 1);
btn = pad.Buttons & 0xFFFF;
if(btn & PSP_CTRL_LTRIGGER)
{
patchSFO = 1;
}
SDParams = (SceUtilitySavedataParamEx*)arg2;
sprintf(s, "ms0:/PSP/SAVEPLAIN/%s%s/%s.bin", SDParams->gameName, SDParams->saveName , SDParams->gameName);
hcWriteFile(s, (u8*)arg2, SDParams->base.size);
if( (SDParams->mode == 1) || (SDParams->mode == 3) || (SDParams->mode == 5) )
{
sprintf(s, "ms0:/PSP/SAVEPLAIN/%s%s/SDDATA.BIN", SDParams->gameName, SDParams->saveName);
hcWriteFile(s, SDParams->dataBuf, SDParams->dataBufSize);
sprintf(s, "ms0:/PSP/SAVEPLAIN/%s%s/SDINFO.BIN", SDParams->gameName, SDParams->saveName);
if( patchSFO == 1 )
{
// if the user wanted to overwrite the SFO info, load it....
hcReadFile(s, &sfoParams, sizeof(PspUtilitySavedataSFOParam));
// ....and overwrite the games SFO info with the loaded one
memcpy(&SDParams->sfoParam, &sfoParams, sizeof(PspUtilitySavedataSFOParam));
} else {
// if the user did not request to overwrite the SFO data, dump the games one
hcWriteFile(s, &SDParams->sfoParam, sizeof(PspUtilitySavedataSFOParam));
}
}
// remember the pointer to the params struct if needed in the GetStatus() callback
if( (SDParams->mode == 0) || (SDParams->mode == 2) || (SDParams->mode == 4) )
{
SDParamBuffer = SDParams;
} else {
SDParamBuffer = 0;
}
return 0;
}
int hcDeemerSavedataGetStatusCallback(int arg1, int arg2, void *arg)
{
int i;
int r;
char s[512];
SceUtilitySavedataParamEx* SDParams;
// check if we remembered the param struct address
// 'cause if we did, we're in loading mode, and might want to load something ;-)
i = (int)SDParamBuffer;
if( i != 0 )
{
SDParams = SDParamBuffer;
sprintf(s, "ms0:/PSP/SAVEPLAIN/%s%s/SDDATA.BIN", SDParams->gameName, SDParams->saveName);
r = hcReadFile(s, SDParams->dataBuf, SDParams->dataBufSize);
sprintf(s, "ms0:/PSP/SAVEPLAIN/%s%s/SDINFO.BIN", SDParams->gameName, SDParams->saveName);
r = hcReadFile(s, &SDParams->sfoParam, sizeof(PspUtilitySavedataSFOParam));
SDParamBuffer = 0;
}
return 0;
}
int mymain(SceSize args, void *argp)
{
int r;
int SDCB1, SDCB2;
sceKernelDelayThread(7000000);
SDCB1 = sceKernelCreateCallback("SaveDataStartCallback", hcDeemerCapturedSDParamsCallback, NULL);
SDCB2 = sceKernelCreateCallback("SaveDataGetStatusCallback", hcDeemerSavedataGetStatusCallback, NULL);
hcDeemerDriverSetupCallbackCapturedSDParams(SDCB1, SDCB2);
r = hcDeemerDriverPatchSavedataInitStart();
sceKernelSleepThreadCB();
return 0;
}
int module_start(SceSize args, void *argp)
{
int tid;
int r;
tid = sceKernelCreateThread("SGDHMainThread", mymain, 0x18, 0x10000, 0, NULL);
r = sceKernelStartThread(tid, 0, NULL);
return 0;
}
int module_stop()
{
return 0;
}