forked from belek666/Open-PS2-Loader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.c
634 lines (536 loc) · 18.3 KB
/
util.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
/*
Copyright 2009, Ifcaro & volca
Licenced under Academic Free License version 3.0
Review OpenUsbLd README & LICENSE files for further details.
*/
#include "include/opl.h"
#include "include/util.h"
#include "include/ioman.h"
#include "include/system.h"
#include <string.h>
#include <malloc.h>
#include <osd_config.h>
#include "include/hdd.h"
extern void *icon_sys;
extern int size_icon_sys;
extern void *icon_icn;
extern int size_icon_icn;
static int mcID = -1;
void guiWarning(const char *text, int count);
int getmcID(void)
{
return mcID;
}
int getFileSize(int fd)
{
int size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
return size;
}
static void writeMCIcon(void)
{
int fd;
fd = openFile("mc?:OPL/opl.icn", O_WRONLY | O_CREAT | O_TRUNC);
if (fd >= 0) {
write(fd, &icon_icn, size_icon_icn);
close(fd);
}
fd = openFile("mc?:OPL/icon.sys", O_WRONLY | O_CREAT | O_TRUNC);
if (fd >= 0) {
write(fd, &icon_sys, size_icon_sys);
close(fd);
}
}
void checkMCFolder(void)
{
char path[32];
int fd;
snprintf(path, sizeof(path), "mc%d:OPL", mcID);
DIR *dir = opendir(path);
if (dir == NULL)
mkdir(path, 0777);
else
closedir(dir);
snprintf(path, sizeof(path), "mc%d:OPL/opl.icn", mcID);
fd = open(path, O_RDONLY, 0666);
if (fd < 0)
writeMCIcon();
close(fd);
snprintf(path, sizeof(path), "mc%d:OPL/icon.sys", mcID);
fd = open(path, O_RDONLY, 0666);
if (fd < 0)
writeMCIcon();
close(fd);
}
static int checkMC()
{
int fd;
if (mcID == -1) {
DIR *dir = opendir("mc0:OPL");
if (dir == NULL) {
dir = opendir("mc1:OPL");
if (dir == NULL) {
// No base dir found on any MC, check MC is inserted
fd = sysCheckMC();
if (fd >= 0) {
dir = opendir("mc0:");
if (dir != NULL) {
closedir(dir);
mcID = 0x30;
}
else {
dir = opendir("mc1:");
if (dir != NULL) {
closedir(dir);
mcID = 0x31;
}
}
}
} else {
closedir(dir);
mcID = 0x31;
}
} else {
closedir(dir);
mcID = 0x30;
}
}
return mcID;
}
static int checkFile(char *path, int mode)
{
// check if it is mc
if (strncmp(path, "mc", 2) == 0) {
// if user didn't explicitly asked for a MC (using '?' char)
if (path[2] == 0x3F) {
// Use default detected card
if (checkMC() >= 0)
path[2] = mcID;
else
return 0;
}
// in create mode, we check that the directory exist, or create it
if (mode & O_CREAT) {
char dirPath[256];
char *pos = strrchr(path, '/');
if (pos) {
memcpy(dirPath, path, pos - path);
dirPath[pos - path] = '\0';
DIR *dir = opendir(dirPath);
if (dir == NULL) {
if (mkdir(dirPath, 0777) < 0)
return 0;
} else
closedir(dir);
}
}
}
return 1;
}
int openFile(char *path, int mode)
{
if (checkFile(path, mode))
return open(path, mode, 0666);
else
return -1;
}
void *readFile(char *path, int align, int *size)
{
void *buffer = NULL;
int fd = openFile(path, O_RDONLY);
if (fd >= 0) {
unsigned int realSize = getFileSize(fd);
if ((*size > 0) && (*size != realSize)) {
LOG("UTIL Invalid filesize, expected: %d, got: %d\n", *size, realSize);
close(fd);
return NULL;
}
if (align > 0)
buffer = memalign(64, realSize); // The allocation is aligned to aid the DMA transfers
else
buffer = malloc(realSize);
if (!buffer) {
LOG("UTIL ReadFile: Failed allocation of %d bytes", realSize);
*size = 0;
} else {
read(fd, buffer, realSize);
close(fd);
*size = realSize;
}
}
return buffer;
}
int listDir(char *path, const char *separator, int maxElem,
int (*readEntry)(int index, const char *path, const char *separator, const char *name, unsigned int mode))
{
int index = 0;
struct stat st;
char filename[128];
if (checkFile(path, O_RDONLY)) {
DIR *dir = opendir(path);
struct dirent *dirent;
if (dir != NULL) {
while (index < maxElem && (dirent = readdir(dir)) != NULL) {
snprintf(filename, 128, "%s/%s", path, dirent->d_name);
stat(filename, &st);
index = readEntry(index, path, separator, dirent->d_name, st.st_mode);
}
closedir(dir);
}
}
return index;
}
/* size will be the maximum line size possible */
file_buffer_t *openFileBuffer(char *fpath, int mode, short allocResult, unsigned int size)
{
file_buffer_t *fileBuffer = NULL;
unsigned char bom[3];
int fd = openFile(fpath, mode);
if (fd >= 0) {
fileBuffer = (file_buffer_t *)malloc(sizeof(file_buffer_t));
fileBuffer->size = size;
fileBuffer->available = 0;
fileBuffer->buffer = (char *)malloc(size * sizeof(char));
if (mode == O_RDONLY) {
fileBuffer->lastPtr = NULL;
//Check for and skip the UTF-8 BOM sequence.
if ((read(fd, bom, sizeof(bom)) != 3) ||
(bom[0] != 0xEF || bom[1] != 0xBB || bom[2] != 0xBF)) {
//Not BOM, so rewind.
lseek(fd, 0, SEEK_SET);
}
} else
fileBuffer->lastPtr = fileBuffer->buffer;
fileBuffer->allocResult = allocResult;
fileBuffer->fd = fd;
fileBuffer->mode = mode;
}
return fileBuffer;
}
/* size will be the maximum line size possible */
file_buffer_t *openFileBufferBuffer(short allocResult, const void *buffer, unsigned int size)
{
file_buffer_t *fileBuffer = NULL;
fileBuffer = (file_buffer_t *)malloc(sizeof(file_buffer_t));
fileBuffer->size = size;
fileBuffer->available = size;
fileBuffer->buffer = (char *)malloc((size + 1) * sizeof(char));
fileBuffer->lastPtr = fileBuffer->buffer; //O_RDONLY, but with the data in the buffer.
fileBuffer->allocResult = allocResult;
fileBuffer->fd = -1;
fileBuffer->mode = O_RDONLY;
memcpy(fileBuffer->buffer, buffer, size);
fileBuffer->buffer[size] = '\0';
return fileBuffer;
}
int readFileBuffer(file_buffer_t *fileBuffer, char **outBuf)
{
int lineSize = 0, readSize, length;
char *posLF = NULL;
while (1) {
// if lastPtr is set, then we continue the read from this point as reference
if (fileBuffer->lastPtr) {
// Calculate the remaining chars to the right of lastPtr
lineSize = fileBuffer->available - (fileBuffer->lastPtr - fileBuffer->buffer);
/*LOG("##### Continue read, position: %X (total: %d) line size (\\0 not inc.): %d end: %x\n",
fileBuffer->lastPtr - fileBuffer->buffer, fileBuffer->available, lineSize, fileBuffer->lastPtr[lineSize]);*/
posLF = strchr(fileBuffer->lastPtr, '\n');
}
if (!posLF) { // We can come here either when the buffer is empty, or if the remaining chars don't have a LF
// if available, we shift the remaining chars to the left ...
if (lineSize) {
//LOG("##### LF not found, Shift %d characters from end to beginning\n", lineSize);
memmove(fileBuffer->buffer, fileBuffer->lastPtr, lineSize);
}
// ... and complete the buffer if we're not at EOF
if (fileBuffer->fd >= 0) {
// Load as many characters necessary to fill the buffer
length = fileBuffer->size - lineSize - 1;
//LOG("##### Asking for %d characters to complete buffer\n", length);
readSize = read(fileBuffer->fd, fileBuffer->buffer + lineSize, length);
fileBuffer->buffer[lineSize + readSize] = '\0';
// Search again (from the lastly added chars only), the result will be "analyzed" in next if
posLF = strchr(fileBuffer->buffer + lineSize, '\n');
// Now update read context info
lineSize = lineSize + readSize;
//LOG("##### %d characters really read, line size now (\\0 not inc.): %d\n", read, lineSize);
// If buffer not full it means we are at EOF
if (fileBuffer->size != lineSize + 1) {
//LOG("##### Reached EOF\n");
close(fileBuffer->fd);
fileBuffer->fd = -1;
}
}
fileBuffer->lastPtr = fileBuffer->buffer;
fileBuffer->available = lineSize;
}
if (posLF)
lineSize = posLF - fileBuffer->lastPtr;
// Check the previous char (on Windows there are CR/LF instead of single linux LF)
if (lineSize)
if (*(fileBuffer->lastPtr + lineSize - 1) == '\r')
lineSize--;
fileBuffer->lastPtr[lineSize] = '\0';
*outBuf = fileBuffer->lastPtr;
//LOG("##### Result line is \"%s\" size: %d avail: %d pos: %d\n", fileBuffer->lastPtr, lineSize, fileBuffer->available, fileBuffer->lastPtr - fileBuffer->buffer);
// If we are at EOF and no more chars available to scan, then we are finished
if (!lineSize && !fileBuffer->available && fileBuffer->fd == -1)
return 0;
if (fileBuffer->lastPtr[0] == '#') { // '#' for comment lines
if (posLF)
fileBuffer->lastPtr = posLF + 1;
else
fileBuffer->lastPtr = NULL;
continue;
}
if (lineSize && fileBuffer->allocResult) {
*outBuf = (char *)malloc((lineSize + 1) * sizeof(char));
memcpy(*outBuf, fileBuffer->lastPtr, lineSize + 1);
}
// Either move the pointer to next chars, or set it to null to force a whole buffer read (if possible)
if (posLF)
fileBuffer->lastPtr = posLF + 1;
else {
fileBuffer->lastPtr = NULL;
}
return 1;
}
}
void writeFileBuffer(file_buffer_t *fileBuffer, char *inBuf, int size)
{
//LOG("writeFileBuffer avail: %d size: %d\n", fileBuffer->available, size);
if (fileBuffer->available && fileBuffer->available + size > fileBuffer->size) {
//LOG("writeFileBuffer flushing: %d\n", fileBuffer->available);
write(fileBuffer->fd, fileBuffer->buffer, fileBuffer->available);
fileBuffer->lastPtr = fileBuffer->buffer;
fileBuffer->available = 0;
}
if (size > fileBuffer->size) {
//LOG("writeFileBuffer direct write: %d\n", size);
write(fileBuffer->fd, inBuf, size);
} else {
memcpy(fileBuffer->lastPtr, inBuf, size);
fileBuffer->lastPtr += size;
fileBuffer->available += size;
//LOG("writeFileBuffer lastPrt: %d\n", (fileBuffer->lastPtr - fileBuffer->buffer));
}
}
void closeFileBuffer(file_buffer_t *fileBuffer)
{
if (fileBuffer->fd >= 0) {
if (fileBuffer->mode != O_RDONLY && fileBuffer->available) {
//LOG("writeFileBuffer final write: %d\n", fileBuffer->available);
write(fileBuffer->fd, fileBuffer->buffer, fileBuffer->available);
}
close(fileBuffer->fd);
}
free(fileBuffer->buffer);
free(fileBuffer);
fileBuffer = NULL;
}
// a simple maximum of two
int max(int a, int b)
{
return a > b ? a : b;
}
// a simple minimum of two
int min(int a, int b)
{
return a < b ? a : b;
}
// single digit from hex decode
int fromHex(char digit)
{
if ((digit >= '0') && (digit <= '9')) {
return (digit - '0');
} else if ((digit >= 'A') && (digit <= 'F')) {
return (digit - 'A' + 10);
} else if ((digit >= 'a') && (digit <= 'f')) {
return (digit - 'a' + 10);
} else
return -1;
}
static const char htab[16] = "0123456789ABCDEF";
char toHex(int digit)
{
return htab[digit & 0x0F];
}
static short int ConsoleRegion = CONSOLE_REGION_INVALID;
static char SystemDataFolderPath[] = "BRDATA-SYSTEM";
static char SystemFolderLetter = 'R';
static void UpdateSystemPaths(void)
{
char regions[CONSOLE_REGION_COUNT] = {'I', 'A', 'E', 'C'};
SystemFolderLetter = regions[ConsoleRegion];
SystemDataFolderPath[1] = SystemFolderLetter;
}
int InitConsoleRegionData(void)
{
int result;
char romver[16];
if ((result = ConsoleRegion) < 0) {
GetRomName(romver);
switch (romver[4]) {
case 'C':
ConsoleRegion = CONSOLE_REGION_CHINA;
break;
case 'E':
ConsoleRegion = CONSOLE_REGION_EUROPE;
break;
case 'H':
case 'A':
ConsoleRegion = CONSOLE_REGION_USA;
break;
default: //For Japanese and unidentified consoles.
ConsoleRegion = CONSOLE_REGION_JAPAN;
}
result = ConsoleRegion;
UpdateSystemPaths();
}
return result;
}
const char *GetSystemDataPath(void)
{
return SystemDataFolderPath;
}
char GetSystemFolderLetter(void)
{
return SystemFolderLetter;
}
int GetSystemRegion(void)
{
return ConsoleRegion;
}
int CheckPS2Logo(int fd, u32 lba)
{
u8 logo[12 * 2048] ALIGNED(64);
void *buffer = logo;
u32 j, k, w;
u8 ValidPS2Logo = 0;
u32 ps2logochecksum = 0;
char text[1024];
w = 0;
if ((fd > 0) && (lba == 0)) // USB_MODE & ETH_MODE
w = read(fd, logo, sizeof(logo)) == sizeof(logo);
if ((lba > 0) && (fd == 0)) { // HDD_MODE
for (k = 0; k <= 12 * 4; k++) { // NB: Disc sector size (2048 bytes) and HDD sector size (512 bytes) differ, hence why we multiplied the number of sectors (12) by 4.
w = !(hddReadSectors(lba + k, 1, buffer));
if (!w)
break;
buffer += 512;
}
}
if (w) {
u8 key = logo[0];
if (logo[0] != 0) {
for (j = 0; j < (12 * 2048); j++) {
logo[j] ^= key;
logo[j] = (logo[j] << 3) | (logo[j] >> 5);
}
for (j = 0; j < (12 * 2048); j++) {
ps2logochecksum += (u32)logo[j];
}
// PS2LOGO NTSC Checksum = 0x120519
// PS2LOGO PAL Checksum = 0x1555AB
snprintf(text, sizeof(text), "%s Disc PS2 Logo(checksum 0x%06X) & %s console ", ((ps2logochecksum == 0x1555AB) ? "PAL" : "NTSC"), ps2logochecksum, ((ConsoleRegion == CONSOLE_REGION_EUROPE) ? "PAL" : "NTSC"));
if (((ps2logochecksum == 0x1555AB) && (ConsoleRegion == CONSOLE_REGION_EUROPE)) || ((ps2logochecksum == 0x120519) && (ConsoleRegion != CONSOLE_REGION_EUROPE))) {
ValidPS2Logo = 1;
strcat(text, "match.");
} else {
strcat(text, "don't match!");
}
if (!gDisableDebug)
guiWarning(text, 12);
} else {
if (!gDisableDebug)
guiWarning("Not a valid PS2 Logo first byte!", 12);
}
} else {
if (!gDisableDebug)
guiWarning("Error reading first 12 disc sectors (PS2 Logo)!", 25);
}
return ValidPS2Logo;
}
struct DirentToDelete {
struct DirentToDelete *next;
char *filename;
};
int sysDeleteFolder(const char *folder)
{
int result;
char *path;
struct dirent *dirent;
DIR *dir;
struct stat st;
struct DirentToDelete *head, *start;
result = 0;
start = head = NULL;
if((dir = opendir(folder)) != NULL) {
/* Generate a list of files in the directory. */
while((dirent = readdir(dir)) != NULL) {
if((strcmp(dirent->d_name, ".") == 0) || ((strcmp(dirent->d_name, "..") == 0)))
continue;
path = malloc(strlen(folder)+strlen(dirent->d_name) + 2);
sprintf(path, "%s/%s", folder, dirent->d_name);
stat(path, &st);
if(S_ISDIR(st.st_mode)) {
/* Recursive, delete all subfolders */
result = sysDeleteFolder(path);
free(path);
} else {
free(path);
if(start == NULL) {
head = malloc(sizeof(struct DirentToDelete));
if(head == NULL)
break;
start = head;
} else {
if((head->next = malloc(sizeof(struct DirentToDelete))) == NULL)
break;
head=head->next;
}
head->next=NULL;
if((head->filename = malloc(strlen(dirent->d_name) + 1)) != NULL)
strcpy(head->filename, dirent->d_name);
else
break;
}
}
closedir(dir);
} else
result = 0;
if (result >= 0) {
/* Delete the files. */
for (head = start; head != NULL; head = start) {
if(head->filename != NULL) {
if((path = malloc(strlen(folder) + strlen(head->filename) + 2)) != NULL) {
sprintf(path, "%s/%s", folder, head->filename);
result=unlink(path);
if (result < 0)
LOG("sysDeleteFolder: failed to remove %s: %d\n", path, result);
free(path);
}
free(head->filename);
}
start = head->next;
free(head);
}
if(result >= 0) {
result = rmdir(folder);
LOG("sysDeleteFolder: failed to rmdir %s: %d\n", folder, result);
}
}
return result;
}
/*----------------------------------------------------------------------------------------*/
/* NOP delay. */
/*----------------------------------------------------------------------------------------*/
void delay(int count)
{
int i, ret;
for (i = 0; i < count; i++) {
ret = 0x01000000;
while (ret--)
asm("nop\nnop\nnop\nnop");
}
}