This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfile.c
656 lines (493 loc) · 17.5 KB
/
file.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
#include "file.h"
#include "graphics.h"
char *getEbootTitle(char *src_path) {
unsigned char hex_buffer[16] = "";
char hexstring[128];
static char return_str[128];
int paramsfo_start = 0x28; //should always be 0x28
int category_block_start;
int variables_block_start;
int number_of_parameters;
int i, j, a, b, e; // c, d,
/// open the eboot file
FILE *fp = NULL;
if ( ( fp = fopen ( src_path, "rb")) == NULL) {
return "Error opening EBOOT!?";
}
/// check if this is where the param.sfo starts (if valid)
fseek(fp, paramsfo_start, SEEK_SET);
fread( &hex_buffer, 8, 1, fp);
if ( hex_buffer[0] == 0x00 && hex_buffer[1] == 0x50 && hex_buffer[2] == 0x53 && hex_buffer[3] == 0x46 ) { //because every PARAM.SFO starts with 00 50 53 46 01 01 00 00
sprintf(hexstring, "0x%02x%02x%02x%02x", hex_buffer[3], hex_buffer[2], hex_buffer[1], hex_buffer[0]);
category_block_start = (int)strtol(hexstring, NULL, 0); //0xF4 aka 244
memset(hexstring,0,sizeof(hexstring)); //clear the string
/// read in category_block_start / the offset where the "Category Block starts"
fread( &hex_buffer, 4, 1, fp);
sprintf(hexstring, "0x%02x%02x%02x%02x", hex_buffer[3], hex_buffer[2], hex_buffer[1], hex_buffer[0]);
category_block_start = (int)strtol(hexstring, NULL, 0); //0xF4 aka 244
memset(hexstring,0,sizeof(hexstring)); //clear the string
/// read in variables_block_start / the offset where the "Variables Block starts"
fread( &hex_buffer, 4, 1, fp);
sprintf(hexstring, "0x%02x%02x%02x%02x", hex_buffer[3], hex_buffer[2], hex_buffer[1], hex_buffer[0]);
variables_block_start = (int)strtol(hexstring, NULL, 0); //0x84 0x01 = 0x184 aka 388
memset(hexstring,0,sizeof(hexstring)); //clear the string
/// read in number of Parameters there are in the sfo
fread( &hex_buffer, 4, 1, fp);
sprintf(hexstring, "0x%02x%02x%02x%02x", hex_buffer[3], hex_buffer[2], hex_buffer[1], hex_buffer[0]);
number_of_parameters = (int)strtol(hexstring, NULL, 0); //0x0E aka 14
memset(hexstring,0,sizeof(hexstring)); //clear the string
/// iterate through offset info for all parameters
for ( i = 0; i < number_of_parameters; i++ ) {
fseek(fp, paramsfo_start + 0x14 + 0x10*i, SEEK_SET); //0x14 start of block + blocksize * i (which one)
fread( &hex_buffer, 16, 1, fp);
//hex_buffer[0-1] -> from "category_block_start" to start of parameter title
//hex_buffer[2-3] -> type of parameter (04 02 = UTF8 & 04 04 = int32)
//hex_buffer[4-7] -> actually used in variable block (length kinda)
//hex_buffer[8-11] -> length of parameter
//hex_buffer[12-15] -> from "variables_block_start" to start of data
//eg. 00 00 - 04 02 - 06 00 00 00 - 08 00 00 00 - 00 00 00 00
// --a-- --b-- -----c----- -----d----- -----e-----
sprintf(hexstring, "0x%02x%02x", hex_buffer[1], hex_buffer[0] );
a = (int)strtol(hexstring, NULL, 0);
memset(hexstring,0,sizeof(hexstring)); //clear the string
sprintf(hexstring, "0x%02x%02x", hex_buffer[3], hex_buffer[2] );
b = (int)strtol(hexstring, NULL, 0);
memset(hexstring,0,sizeof(hexstring)); //clear the string
if ( b == 0x204 ) { //because TITLE is 04 02 = UTF8
/// read in category block name
fseek(fp, paramsfo_start + category_block_start + a, SEEK_SET);
j = 1;
while ( j ) {
fread( &hex_buffer, 1, 1, fp);
if ( hex_buffer[0] == 0 ) j = 0;
sprintf(hexstring, "%s%c", hexstring, hex_buffer[0]);
}
if ( strcmp(hexstring, "TITLE") == 0 ) {
memset(hexstring,0,sizeof(hexstring)); //clear the string
/*sprintf(hexstring, "0x%02x%02x%02x%02x", hex_buffer[7], hex_buffer[6], hex_buffer[5], hex_buffer[4] );
c = (int)strtol(hexstring, NULL, 0);
memset(hexstring,0,sizeof(hexstring)); //clear the string
sprintf(hexstring, "0x%02x%02x%02x%02x", hex_buffer[11], hex_buffer[10], hex_buffer[9], hex_buffer[8] );
d = (int)strtol(hexstring, NULL, 0);
memset(hexstring,0,sizeof(hexstring)); //clear the string*/
sprintf(hexstring, "0x%02x%02x%02x%02x", hex_buffer[15], hex_buffer[14], hex_buffer[13], hex_buffer[12] );
e = (int)strtol(hexstring, NULL, 0);
memset(hexstring,0,sizeof(hexstring)); //clear the string
/// read in the Eboots title! yay :)
fseek(fp, paramsfo_start + variables_block_start + e, SEEK_SET);
j = 1;
while ( j ) {
fread( &hex_buffer, 1, 1, fp);
if ( hex_buffer[0] == 0 ) j = 0;
sprintf(hexstring, "%s%c", hexstring, hex_buffer[0]);
}
strcpy(return_str, hexstring); //termination fix
fclose(fp);
return return_str;
}
}
}
} else {
fclose(fp);
return "Not a valid EBOOT?!";
}
fclose(fp);
return "Could not find TITLE in EBOOT";
}
int doesFileExist(const char* path) {
SceUID dir = sceIoOpen(path, SCE_O_RDONLY, 0777);
if (dir >= 0) {
sceIoClose(dir);
return 1;
} else {
return 0;
}
}
int doesDirExist(const char* path) {
SceUID dir = sceIoDopen(path);
if (dir >= 0) {
sceIoDclose(dir);
return 1;
} else {
return 0;
}
}
int getFileSize(const char *file) { //thx Flow VitaShell
SceUID fd = sceIoOpen(file, SCE_O_RDONLY, 0);
if (fd < 0)
return fd;
int fileSize = sceIoLseek(fd, 0, SCE_SEEK_END);
sceIoClose(fd);
return fileSize;
}
int copyFile(char *src_path, char *dst_path) { //thx Flow VitaShell
// The source and destination paths are identical
if (strcmp(src_path, dst_path) == 0) return -1;
// The destination is a subfolder of the source folder
int len = strlen(src_path);
if (strncmp(src_path, dst_path, len) == 0 && dst_path[len] == '/') return -1;
SceUID fdsrc = sceIoOpen(src_path, SCE_O_RDONLY, 0);
if (fdsrc < 0) return fdsrc;
SceUID fddst = sceIoOpen(dst_path, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777);
if (fddst < 0) {
sceIoClose(fdsrc);
return fddst;
}
void *buf = malloc(TRANSFER_SIZE);
int read;
while ((read = sceIoRead(fdsrc, buf, TRANSFER_SIZE)) > 0) {
sceIoWrite(fddst, buf, read);
}
free(buf);
sceIoClose(fddst);
sceIoClose(fdsrc);
return 0;
}
int createEmptyFile(char *path) {
FILE *file = fopen(path, "w");
if (file == NULL){
return -1;
} else {
//fprintf(file, "");
}
fclose(file);
return 0;
}
int makePath(const char *dir) { //thx molecule
char dir_copy[0x400] = {0};
snprintf(dir_copy, sizeof(dir_copy) - 2, "%s", dir);
dir_copy[strlen(dir_copy)] = '/';
char *c;
for (c = dir_copy; *c; ++c) {
if (*c == '/') {
*c = '\0';
sceIoMkdir(dir_copy, 0777);
*c = '/';
}
}
//test
SceUID test = sceIoDopen(dir);
if (test >= 0) {
sceIoDclose(test);
return 0; //success
} else {
return 1;
}
}
int removePath(char *path) { //thx Flow VitaShell
SceUID dfd = sceIoDopen(path);
if (dfd >= 0) {
int res = 0;
do {
SceIoDirent dir;
memset(&dir, 0, sizeof(SceIoDirent));
res = sceIoDread(dfd, &dir);
if (res > 0) {
if (strcmp(dir.d_name, ".") == 0 || strcmp(dir.d_name, "..") == 0)
continue;
char *new_path = malloc(strlen(path) + strlen(dir.d_name) + 2);
snprintf(new_path, MAX_PATH_LENGTH, "%s/%s", path, dir.d_name);
if (SCE_S_ISDIR(dir.d_stat.st_mode)) {
int ret = removePath(new_path);
if (ret <= 0) {
free(new_path);
sceIoDclose(dfd);
return ret;
}
} else {
int ret = sceIoRemove(new_path);
if (ret < 0) {
free(new_path);
sceIoDclose(dfd);
return ret;
}
}
free(new_path);
}
} while (res > 0);
sceIoDclose(dfd);
int ret = sceIoRmdir(path);
if (ret < 0)
return ret;
} else {
int ret = sceIoRemove(path);
if (ret < 0)
return ret;
}
return 1;
}
int download_file(const char *src, const char *dst) { //thx molecule offline installer
int ret;
int coord = psvDebugScreenGetX(); //save current position of screen once
int tpl = sceHttpCreateTemplate("henkaku offline", 2, 1);
if (tpl < 0) {
printf("sceHttpCreateTemplate: 0x%x ", tpl);
return tpl;
}
int conn = sceHttpCreateConnectionWithURL(tpl, src, 0);
if (conn < 0) {
printf("sceHttpCreateConnectionWithURL: 0x%x ", conn);
return conn;
}
int req = sceHttpCreateRequestWithURL(conn, 0, src, 0);
if (req < 0) {
printf("sceHttpCreateRequestWithURL: 0x%x ", req);
return req;
}
ret = sceHttpSendRequest(req, NULL, 0);
if (ret < 0) {
printf("sceHttpSendRequest: 0x%x ", ret);
return ret;
}
unsigned char buf[4096] = {0};
long long unsigned length = 0;
ret = sceHttpGetResponseContentLength(req, &length);
int fd = sceIoOpen(dst, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 6);
int total_read = 0;
if (fd < 0) {
printf("sceIoOpen: 0x%x ", fd);
return fd;
}
while (1) {
int read = sceHttpReadData(req, buf, sizeof(buf));
if (read < 0) {
printf("sceHttpReadData error! 0x%x ", read);
return read;
}
if (read == 0) break; //done downloading
ret = sceIoWrite(fd, buf, read);
if (ret < 0 || ret != read) {
printf("sceIoWrite error! 0x%x ", ret);
if (ret < 0)
return ret;
return -1;
}
total_read += read;
psvDebugScreenSetXY( coord, psvDebugScreenGetY() );
printf("(%i bytes) ", total_read);
}
ret = sceIoClose(fd);
if (ret < 0)
printf("sceIoClose: 0x%x ", ret);
return 0;
}
void get_id_of_psp_game_that_adrenaline_is_installed_to(char *taiconfig, char *path) { //return PSP GAME ID of which adrenaline is installed to (and save it globally)
char buffer[255];
char pathbuff[1024];
sprintf(pathbuff, "%s/adrenaline.suprx", path );
static char GAME_ID[32];
FILE* config = fopen(taiconfig, "r");
if (config == NULL){
if (config != NULL) fclose(config);
} else {
while (fscanf(config, "%s", buffer) == 1) {
//read in the Title_ID starting with a *
if( strstr(&buffer[0], "*") ) memmove(GAME_ID, buffer+1, sizeof(buffer));
if( strstr(buffer, pathbuff) ) {
sprintf(PSP_GAME_ID, "%s", GAME_ID); //save global
//return GAME_ID;
}
}
fclose(config);
}
//return "Not found";
}
int write_adrenaline_to_config(char *taiconfig, char *path, char *id) {
char buffer[1024];
char pathbuff[1024];
FILE *file = fopen(taiconfig, "r");
FILE *temp = fopen("ur0:temp.txt", "w");
if (file == NULL){
//Error: Couldn't open for writing
return 1;
} else {
while (fgets(buffer, sizeof(buffer), file) != NULL) {
if ( buffer[0] != '\n' ) { //empty row fix
fprintf(temp, "%s", buffer);
}
if ( buffer[strlen(buffer)-1] != '\n' ) fprintf(temp, "\n"); //whitespaces fix
if( (strstr(buffer, "*KERNEL")) != NULL ) {
sprintf(pathbuff, "%s/adrenaline.skprx\n", path );
fprintf(temp, pathbuff);
}
}
//end of file (append the rest)
fprintf(temp, "*%s\n", id);
if ( strcmp(path, "ux0:pspemu/adrenaline") == 0 ) //this is only for the initial release with ux0:pspemu/adr..
fprintf(temp, "vs0:sys/external/libpgf.suprx\n");
sprintf(pathbuff, "%s/adrenaline.suprx\n", path );
fprintf(temp, pathbuff);
}
fclose(file);
fclose(temp);
sceIoRemove(taiconfig);
copyFile("ur0:temp.txt", taiconfig);
sceIoRemove("ur0:temp.txt");
return 0;
}
int delete_adrenaline_from_config(char *taiconfig, char *path, char *id) {
char buffer[1024];
char pathbuff[1024];
FILE *file = fopen(taiconfig, "r");
FILE *temp = fopen("ur0:temp.txt", "w");
if (file == NULL){
//Error: Couldn't open for writing
return 1;
} else {
while (fgets(buffer, sizeof(buffer), file) != NULL) {
sprintf(pathbuff, "%s/adrenaline.skprx", path );
if( (strstr(buffer, pathbuff)) != NULL ) continue;
if( (strstr(buffer, id)) != NULL ) continue;
if ( strcmp(path, "ux0:pspemu/adrenaline") == 0 ) //this is only for the initial release with ux0:pspemu/adr..
if( (strstr(buffer, "vs0:sys/external/libpgf.suprx")) != NULL ) continue; //only for the first version
sprintf(pathbuff, "%s/adrenaline.suprx", path );
if( (strstr(buffer, pathbuff)) != NULL ) continue;
fprintf(temp, "%s", buffer);
}
}
fclose(file);
fclose(temp);
sceIoRemove(taiconfig);
copyFile("ur0:temp.txt", taiconfig);
sceIoRemove("ur0:temp.txt");
return 0;
}
int check_for_psp_content(char *path) { //returns number of valid PSP games in *path* and saves title id and name in global arrays
SceUID dfd = sceIoDopen(path);
char pathbuff[256];
if (dfd >= 0) {
int res = 0;
int count = 0;
do {
SceIoDirent dir;
memset(&dir, 0, sizeof(SceIoDirent));
res = sceIoDread(dfd, &dir);
if (res > 0) {
if (strcmp(dir.d_name, ".") == 0 || strcmp(dir.d_name, "..") == 0)
continue;
sprintf(pathbuff, "%s%s%s", path, dir.d_name, "/__sce_ebootpbp");
if ( doesFileExist(pathbuff) ) {
//save TitleID in global array
strcpy(content_array[count].titleID, dir.d_name);
//save Eboot Name in global array
sprintf(pathbuff, "%s%s/EBOOT.PBP", path, dir.d_name );
strcpy(content_array[count].title, getEbootTitle(pathbuff));
count++;
}
}
} while (res > 0);
sceIoDclose(dfd);
return count;
}
return 0;
}
void trigger_update_database() { //deleting or creating a null-byte makes no difference it seems
/// 1) delete id.dat -> System is confused which card it should use? (internal/external on SLim/PSTV -> Change location Dialog will appear)
//sceIoRemove("ux0:id.dat");
/// 2) delete id.dat and write empty id.dat -> same result as 1
/*sceIoRemove("ux0:id.dat");
SceUID id = sceIoOpen("ux0:id.dat", SCE_O_WRONLY|SCE_O_CREAT, 0777);
sceIoWrite(id, NULL, 0x0);
sceIoClose(id);*/
/// 3) delete one row in id.dat (or write INVALID_XXX=)
/// - delete MID row -> triggers update and regenerates and writes MID in the last row
/// - delete SVR row -> no update dialog but will regenerate the line
/// Writing a replacement "MID=" without data works too and keeps right order of id.dat
static char datbuff[512];
static char mid_string[] = "MID=";
FILE *dat = fopen("ux0:id.dat", "r");
FILE *tempdat = fopen("ux0:temp.dat", "w");
if (dat == NULL){
//Error: Couldn't open for writing
} else {
while (fgets(datbuff, 512, dat) != NULL) {
if( strstr(datbuff, mid_string) ) fprintf(tempdat, "%s\n", mid_string);
else fprintf(tempdat, "%s", datbuff);
}
}
fclose(dat);
fclose(tempdat);
sceIoRemove("ux0:id.dat");
copyFile("ux0:temp.dat", "ux0:id.dat");
sceIoRemove("ux0:temp.dat");
/// 4) delete all rows and write empty "MID=" "DIG=" ..... - triggers change location for savedata too
/*sceIoRemove("ux0:id.dat");
SceUID id = sceIoOpen("ux0:id.dat", SCE_O_WRONLY|SCE_O_CREAT, 0777);
sceIoWrite(id, "MID=\nDIG=\nDID=\nAID=\nOID=\nSVR=", 31);
sceIoClose(id);*/
/// 5) replace id.dat with folder called id.dat -> EVIL
}
void trigger_rebuild_database() {
/**
* Simply removing app.db still shows up as "updating database" but rebuilds app.db obviously with icon layout and everything.
* The CAFEBABE method does not affect the icon layout and does not really rebuild everything.. (eg appmeta resources still there)
* BUT shows up as "rebuild"..
*
* -> Soooo we simply combine them :)
*/
char magik[] = { 0xBE, 0xBA, 0xFE, 0xCA };
SceUID id;
id = sceIoOpen("ur0:shell/db/dbr.db-err", SCE_O_WRONLY|SCE_O_CREAT, 0777);
sceIoWrite(id, magik, 4);
sceIoClose(id);
sceIoRemove("ur0:shell/db/app.db");
}
int writeChangeinfo(const char* id) { // return 1 on success
char temp_buffer[256];
int ret;
sprintf(temp_buffer, "ux0:patch/%s/sce_sys/changeinfo", id);
ret = makePath(temp_buffer); //0 on success
if ( ret == 0 ) { //if success
sprintf(temp_buffer, "ux0:patch/%s/sce_sys/changeinfo/changeinfo.xml", id);
ret = copyFile("app0:sce_sys/changeinfo/changeinfo.xml", temp_buffer); //0 on success
if ( ret == 0 ) return 1; //success
}
return 0;
}
int checkUnsafeHomebrew() { //a tai syscall to check for this would be cool
FILE *fp = NULL;
if ( ( fp = fopen ( "os0:psp2bootconfig.skprx", "rb")) == NULL) {
return 1; //on error
}
fclose(fp);
return 0;
}
char *getRealFirmwareVersion(int arg) { //with arg = 1 [3.60] with arg = 0 [0x03600000]
FILE *fp = NULL;
unsigned char hex[32] = "";
static char version[16];
if ( ( fp = fopen ( "os0:psp2bootconfig.skprx", "rb")) == NULL) {
return "Error!?";
}
fseek(fp, 146, SEEK_SET);
fread( &hex, 8, 1, fp);
fclose(fp);
if ( arg == 1) {
sprintf(version, "%x.%02x", hex[3], hex[2]);
} else {
sprintf(version, "0x%02x%02x%02x%02x", hex[3], hex[2], hex[1], hex[0]);
}
return version;
}
char *read_installed_adrenaline_version(char *path) {
static char readbuffer[256];
FILE *file = fopen(path, "r");
if (file == NULL){ //Error: Couldn't open
return "error";
} else {
fgets(readbuffer, sizeof(readbuffer), file);
}
fclose(file);
return readbuffer;
}
int write_installed_adrenaline_version(char *path, char *arg) {
//sceIoRemove(path);
FILE *file = fopen(path, "w");
if (file == NULL){ //Error: Couldn't open for writing
return -1;
} else {
fprintf(file, arg);
}
fclose(file);
return 1;
}