-
Notifications
You must be signed in to change notification settings - Fork 20
/
OSDInit.c
645 lines (546 loc) · 16.5 KB
/
OSDInit.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
/*
OSD initer, modified by Fobes.
Originally licensed as AFL under the PS2IDENT project
*/
#include <stdio.h>
#include <kernel.h>
#include <string.h>
#include <libcdvd.h>
#include <osd_config.h>
#include <tamtypes.h>
#include <unistd.h>
#include <fcntl.h>
#include <libcdvd.h>
#include "OSDInit.h"
/* Parsing of values from the EEPROM and setting them into the EE kernel
was done in different ways, across different browser versions.
The early browsers of ROM v1.00 and v1.01 (SCPH-10000/SCPH-15000)
parsed the values within the EEPROM into global variables,
which are used to set the individual fields in the OSD configuration.
The newer browsers parsed the values into a bitfield structure,
which does not have the same layout as the OSD configuration structure.
Both designs had the parsing and the preparation of the OSD
configuration data separated, presumably for clarity of code and
to achieve low-coupling (perhaps they belonged to different modules). */
static int ConsoleRegion = -1, ConsoleOSDRegion = -1, ConsoleOSDLanguage = -1;
static int ConsoleOSDRegionInitStatus = 0, ConsoleRegionParamInitStatus = 0; // 0 = Not init. 1 = Init complete. <0 = Init failed.
static u8 ConsoleRegionData[16] = {0, 0, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Perhaps it once used to read more configuration blocks (original capacity was 7 blocks).
static u8 OSDConfigBuffer[CONFIG_BLOCK_SIZE * 2];
// Local function prototypes
static int InitMGRegion(void);
static int ConsoleInitRegion(void);
static int ConsoleRegionParamsInitPS1DRV(const char* romver);
static int GetConsoleRegion(void);
static int CdReadOSDRegionParams(char* OSDVer);
static int GetOSDRegion(void);
static void InitOSDDefaultLanguage(int region, const char* language);
static int ReadOSDConfigPS2(OSDConfig2_t* config, const OSDConfigStore_t* OSDConfigBuffer);
static void ReadOSDConfigPS1(OSDConfig1_t* config, const OSDConfigStore_t* OSDConfigBuffer);
static void WriteOSDConfigPS1(OSDConfigStore_t* OSDConfigBuffer, const OSDConfig1_t* config);
static int WriteOSDConfigPS2(OSDConfigStore_t* OSDConfigBuffer, const OSDConfig2_t* config, u8 invalid);
static void ReadConfigFromNVM(u8* buffer);
static void WriteConfigToNVM(const u8* buffer);
// Directory names
static char SystemDataFolder[] = "BRDATA-SYSTEM";
static char SystemExecFolder[] = "BREXEC-SYSTEM";
static char DVDPLExecFolder[] = "BREXEC-DVDPLAYER";
char ConsoleROMVER[ROMVER_MAX_LEN];
static u8 MECHACON_CMD_S36_supported = 0, MECHACON_CMD_S27_supported = 0;
// Initialize add-on functions. Currently only retrieves the MECHACON's version to determine what sceCdAltGetRegionParams() should do.
int cdInitAdd(void)
{
u32 result, status, i;
u8 MECHA_version_data[3];
unsigned int MECHA_version;
// Like how CDVDMAN checks sceCdMV(), do not continuously attempt to get the MECHACON version because some consoles (e.g. DTL-H301xx) can't return one.
for (i = 0; i <= 100; i++)
{
if ((result = sceCdMV(MECHA_version_data, &status)) != 0 && ((status & 0x80) == 0))
{
MECHA_version = MECHA_version_data[2] | ((unsigned int)MECHA_version_data[1] << 8) | ((unsigned int)MECHA_version_data[0] << 16);
MECHACON_CMD_S36_supported = (0x5FFFF < MECHA_version); // v6.0 and later
MECHACON_CMD_S27_supported = (0x501FF < MECHA_version); // v5.2 and later
return 0;
}
}
// printf("Failed to get MECHACON version: %d 0x%x\n", result, status);
return -1;
}
/*
This function provides an equivalent of the sceCdGetRegionParams function from the newer CDVDMAN modules. The old CDVDFSV and CDVDMAN modules don't support this S-command.
It's supported by only slimline consoles, and returns regional information (e.g. MECHACON version, MG region mask, DVD player region letter etc.).
*/
int sceCdAltReadRegionParams(u8* data, u32* stat)
{
unsigned char RegionData[15];
int result;
memset(data, 0, 13);
if (MECHACON_CMD_S36_supported)
{
if ((result = sceCdApplySCmd(0x36, NULL, 0, RegionData)) != 0)
{
*stat = RegionData[0];
memcpy(data, &RegionData[1], 13);
}
}
else
{
*stat = 0x100;
result = 1;
}
return result;
}
static int InitMGRegion(void)
{
u32 stat;
int result;
if (ConsoleRegionParamInitStatus == 0)
{
do
{
if ((result = sceCdAltReadRegionParams(ConsoleRegionData, &stat)) == 0)
{ // Failed.
ConsoleRegionParamInitStatus = 1;
}
else
{
if (stat & 0x100)
{
// MECHACON does not support this function.
ConsoleRegionParamInitStatus = -1;
break;
}
else
{ // Status OK, but the result yielded an error.
ConsoleRegionParamInitStatus = 1;
}
}
} while ((result == 0) || (stat & 0x80));
}
return ConsoleRegionParamInitStatus;
}
void OSDInitSystemPaths(void)
{
int region;
char regions[CONSOLE_REGION_COUNT] = {'I', 'A', 'E', 'C'};
region = OSDGetConsoleRegion();
if (region >= 0 && region < CONSOLE_REGION_COUNT)
{
SystemDataFolder[1] = regions[region];
SystemExecFolder[1] = regions[region];
DVDPLExecFolder[1] = regions[region];
}
}
int OSDGetMGRegion(void)
{
return ((InitMGRegion() >= 0) ? ConsoleRegionData[8] : 0);
}
static int ConsoleInitRegion(void)
{
GetOSDRegion();
return InitMGRegion();
}
static int ConsoleRegionParamsInitPS1DRV(const char* romver)
{
int result;
if (ConsoleInitRegion() >= 0)
{
ConsoleRegionData[2] = romver[4];
result = 1;
}
else
result = 0;
return result;
}
int OSDGetPS1DRVRegion(char* region)
{
int result;
if (ConsoleInitRegion() >= 0)
{
*region = ConsoleRegionData[2];
result = 1;
}
else
result = 0;
return result;
}
int OSDGetDVDPlayerRegion(char* region)
{
int result;
if (ConsoleInitRegion() >= 0)
{
*region = ConsoleRegionData[8];
result = 1;
}
else
result = 0;
return result;
}
static int GetConsoleRegion(void)
{
char romver[16];
int fd, result;
if ((result = ConsoleRegion) < 0)
{
if ((fd = open("rom0:ROMVER", O_RDONLY)) >= 0)
{
read(fd, romver, sizeof(romver));
close(fd);
ConsoleRegionParamsInitPS1DRV(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;
case 'J':
ConsoleRegion = CONSOLE_REGION_JAPAN;
}
result = ConsoleRegion;
}
else
result = -1;
}
return result;
}
static int CdReadOSDRegionParams(char* OSDVer)
{
int result;
if (OSDVer[4] == '?')
{
if (InitMGRegion() >= 0)
{
result = 1;
OSDVer[4] = ConsoleRegionData[3];
OSDVer[5] = ConsoleRegionData[4];
OSDVer[6] = ConsoleRegionData[5];
OSDVer[7] = ConsoleRegionData[6];
}
else
result = 0;
}
else
{
ConsoleRegionParamInitStatus = -256;
result = 0;
}
return result;
}
static int GetOSDRegion(void)
{
char OSDVer[16];
int fd;
if (ConsoleOSDRegionInitStatus == 0 || ConsoleOSDRegion == -1)
{
ConsoleOSDRegionInitStatus = 1;
if ((fd = open("rom0:OSDVER", O_RDONLY)) >= 0)
{
read(fd, OSDVer, sizeof(OSDVer));
close(fd);
CdReadOSDRegionParams(OSDVer);
switch (OSDVer[4])
{
case 'A':
ConsoleOSDRegion = OSD_REGION_USA;
break;
case 'C':
ConsoleOSDRegion = OSD_REGION_CHINA;
break;
case 'E':
ConsoleOSDRegion = OSD_REGION_EUROPE;
break;
case 'H':
ConsoleOSDRegion = OSD_REGION_ASIA;
break;
case 'J':
ConsoleOSDRegion = OSD_REGION_JAPAN;
break;
case 'K':
ConsoleOSDRegion = OSD_REGION_KOREA;
break;
case 'R':
ConsoleOSDRegion = OSD_REGION_RUSSIA;
break;
default:
ConsoleOSDRegion = OSD_REGION_INVALID;
}
if (ConsoleOSDRegion != OSD_REGION_INVALID)
InitOSDDefaultLanguage(ConsoleOSDRegion, &OSDVer[5]);
}
else
ConsoleOSDRegion = OSD_REGION_INVALID;
}
return ConsoleOSDRegion;
}
static void InitOSDDefaultLanguage(int region, const char* language)
{
int DefaultLang;
DefaultLang = -1;
if (ConsoleOSDLanguage == -1)
{
if (language != NULL)
{
if (strncmp(language, "jpn", 3) == 0)
DefaultLang = LANGUAGE_JAPANESE;
else if (strncmp(language, "eng", 3) == 0)
DefaultLang = LANGUAGE_ENGLISH;
else if (strncmp(language, "fre", 3) == 0)
DefaultLang = LANGUAGE_FRENCH;
else if (strncmp(language, "spa", 3) == 0)
DefaultLang = LANGUAGE_SPANISH;
else if (strncmp(language, "ger", 3) == 0)
DefaultLang = LANGUAGE_GERMAN;
else if (strncmp(language, "ita", 3) == 0)
DefaultLang = LANGUAGE_ITALIAN;
else if (strncmp(language, "dut", 3) == 0)
DefaultLang = LANGUAGE_DUTCH;
else if (strncmp(language, "por", 3) == 0)
DefaultLang = LANGUAGE_PORTUGUESE;
else if (strncmp(language, "rus", 3) == 0)
DefaultLang = LANGUAGE_RUSSIAN;
else if (strncmp(language, "kor", 3) == 0)
DefaultLang = LANGUAGE_KOREAN;
else if (strncmp(language, "tch", 3) == 0)
DefaultLang = LANGUAGE_TRAD_CHINESE;
else if (strncmp(language, "sch", 3) == 0)
DefaultLang = LANGUAGE_SIMPL_CHINESE;
else
DefaultLang = -1;
}
// Check if the specified language is valid for the region
if (!OSDIsLanguageValid(region, DefaultLang))
{
switch (region)
{
case OSD_REGION_JAPAN:
DefaultLang = LANGUAGE_JAPANESE;
break;
case OSD_REGION_CHINA:
DefaultLang = LANGUAGE_SIMPL_CHINESE;
break;
case OSD_REGION_RUSSIA:
DefaultLang = LANGUAGE_RUSSIAN;
break;
case OSD_REGION_KOREA:
DefaultLang = LANGUAGE_KOREAN;
break;
case OSD_REGION_USA:
case OSD_REGION_EUROPE:
case OSD_REGION_ASIA:
default:
DefaultLang = LANGUAGE_ENGLISH;
}
}
ConsoleOSDLanguage = DefaultLang;
}
}
int OSDIsLanguageValid(int region, int language)
{
switch (region)
{
case OSD_REGION_JAPAN:
return (language == LANGUAGE_JAPANESE || language == LANGUAGE_ENGLISH) ? language : -1;
case OSD_REGION_CHINA:
return (language == LANGUAGE_ENGLISH || language == LANGUAGE_SIMPL_CHINESE) ? language : -1;
case OSD_REGION_RUSSIA:
return (language == LANGUAGE_ENGLISH || language == LANGUAGE_RUSSIAN) ? language : -1;
case OSD_REGION_KOREA:
return (language == LANGUAGE_ENGLISH || language == LANGUAGE_KOREAN) ? language : -1;
case OSD_REGION_ASIA:
return (language == LANGUAGE_ENGLISH || language == LANGUAGE_TRAD_CHINESE) ? language : -1;
case OSD_REGION_USA:
case OSD_REGION_EUROPE:
default:
return (language <= LANGUAGE_PORTUGUESE && region > OSD_REGION_JAPAN) ? language : -1;
}
}
int OSDGetConsoleRegion(void)
{ // Default to Japan, if the region cannot be obtained.
int result;
result = GetConsoleRegion();
return (result < 0 ? 0 : result);
}
int OSDGetVideoMode(void)
{
return (GetConsoleRegion() == CONSOLE_REGION_EUROPE);
}
int OSDGetRegion(void)
{
int region;
if ((region = GetOSDRegion()) < 0)
{
region = OSDGetConsoleRegion();
InitOSDDefaultLanguage(region, NULL);
}
return region;
}
int OSDGetDefaultLanguage(void)
{
if (ConsoleOSDLanguage == -1)
OSDGetRegion();
return ConsoleOSDLanguage;
}
/* Notes:
Version = 0 (Protokernel consoles only) NTSC-J, 1 = ROM versions up to v1.70, 2 = v1.80 and later. 2 = support for extended languages (Osd2 bytes 3 and 4)
In the homebrew PS2SDK, this was previously known as the "region".
japLanguage = 0 (Japanese, protokernel consoles only), 1 = non-Japanese (Protokernel consoles only). Newer browsers have this set always to 1.
*/
static int ReadOSDConfigPS2(OSDConfig2_t* config, const OSDConfigStore_t* OSDConfigBuffer)
{
config->spdifMode = OSDConfigBuffer->PS2.spdifMode;
config->screenType = OSDConfigBuffer->PS2.screenType;
config->videoOutput = OSDConfigBuffer->PS2.videoOutput;
if (OSDConfigBuffer->PS2.extendedLanguage) // Extended/Basic language set
{ // One of the 8 standard languages
config->language = OSDConfigBuffer->PS2.language;
}
else
{ // Japanese/English
config->language = OSDConfigBuffer->PS2.japLanguage;
}
config->daylightSaving = OSDConfigBuffer->PS2.daylightSaving;
config->timeFormat = OSDConfigBuffer->PS2.timeFormat;
config->dateFormat = OSDConfigBuffer->PS2.dateFormat;
config->timezoneOffset = OSDConfigBuffer->PS2.timezoneOffsetLo | ((u32)OSDConfigBuffer->PS2.timezoneOffsetHi) << 8;
config->timezone = OSDConfigBuffer->PS2.timezoneLo | (((u32)OSDConfigBuffer->PS2.timezoneHi) << 8);
config->rcEnabled = OSDConfigBuffer->PS2.rcEnabled;
config->rcGameFunction = OSDConfigBuffer->PS2.rcGameFunction;
config->rcSupported = OSDConfigBuffer->PS2.rcSupported;
config->dvdpProgressive = OSDConfigBuffer->PS2.dvdpProgressive;
return (OSDConfigBuffer->PS2.osdInit ^ 1);
}
static void ReadOSDConfigPS1(OSDConfig1_t* config, const OSDConfigStore_t* OSDConfigBuffer)
{
int i;
for (i = 0; i < CONFIG_BLOCK_SIZE; i++)
config->data[i] = OSDConfigBuffer->PS1.bytes[i];
}
static void WriteOSDConfigPS1(OSDConfigStore_t* OSDConfigBuffer, const OSDConfig1_t* config)
{
int i;
for (i = 0; i < CONFIG_BLOCK_SIZE; i++)
OSDConfigBuffer->PS1.bytes[i] = config->data[i];
}
static int WriteOSDConfigPS2(OSDConfigStore_t* OSDConfigBuffer, const OSDConfig2_t* config, u8 invalid)
{
int japLanguage, version, osdInitValue;
osdInitValue = invalid ^ 1;
version = OSDConfigBuffer->PS2.extendedLanguage == 0 ? 1 : OSDConfigBuffer->PS2.extendedLanguage;
if (config->language <= LANGUAGE_ENGLISH)
japLanguage = config->language;
else // Do not update the legacy language option if the language was changed to something unsupported.
japLanguage = OSDConfigBuffer->PS2.japLanguage;
// 0x0F
OSDConfigBuffer->PS2.videoOutput = config->videoOutput;
OSDConfigBuffer->PS2.japLanguage = japLanguage;
OSDConfigBuffer->PS2.extendedLanguage = 1;
OSDConfigBuffer->PS2.spdifMode = config->spdifMode;
OSDConfigBuffer->PS2.screenType = config->screenType;
// 0x10
OSDConfigBuffer->PS2.language = config->language;
OSDConfigBuffer->PS2.version = version;
// 0x11
OSDConfigBuffer->PS2.timezoneOffsetHi = config->timezoneOffset >> 8;
OSDConfigBuffer->PS2.dateFormat = config->dateFormat;
OSDConfigBuffer->PS2.timeFormat = config->timeFormat;
OSDConfigBuffer->PS2.daylightSaving = config->daylightSaving;
OSDConfigBuffer->PS2.osdInit = osdInitValue;
// 0x12
OSDConfigBuffer->PS2.timezoneOffsetLo = config->timezoneOffset;
// 0x13
OSDConfigBuffer->PS2.timezoneHi = config->timezone >> 8;
OSDConfigBuffer->PS2.unknownB13_01 = OSDConfigBuffer->PS2.unknownB13_01; // Carry over
OSDConfigBuffer->PS2.rcEnabled = config->rcEnabled;
OSDConfigBuffer->PS2.rcGameFunction = config->rcGameFunction;
OSDConfigBuffer->PS2.rcSupported = config->rcSupported;
OSDConfigBuffer->PS2.dvdpProgressive = config->dvdpProgressive;
// 0x14
OSDConfigBuffer->PS2.timezoneLo = config->timezone;
return 0;
}
static void ReadConfigFromNVM(u8* buffer)
{ /* Hmm. What should the check for stat be? In v1.xx, it seems to be a check against 0x9. In v2.20, it checks against 0x81.
In the HDD Browser, reading checks against 0x81, while writing checks against 0x9.
But because we are targeting all consoles, it would be probably safer to follow the HDD Browser. */
int result;
u32 stat;
do
{
sceCdOpenConfig(1, 0, 2, &stat);
} while (stat & 0x81);
do
{
result = sceCdReadConfig(buffer, &stat);
} while ((stat & 0x81) || (result == 0));
do
{
result = sceCdCloseConfig(&stat);
} while ((stat & 0x81) || (result == 0));
}
static void WriteConfigToNVM(const u8* buffer)
{ // Read the comment in ReadConfigFromNVM() about the error status bits.
u32 stat;
int result;
do
{
sceCdOpenConfig(1, 1, 2, &stat);
} while (stat & 0x09);
do
{
result = sceCdWriteConfig(buffer, &stat);
} while ((stat & 0x09) || (result == 0));
do
{
result = sceCdCloseConfig(&stat);
} while ((stat & 9) || (result == 0));
}
int OSDLoadConfigFromNVM(OSDConfig1_t* osdConfigPS1, OSDConfig2_t* osdConfigPS2)
{
int result;
ReadConfigFromNVM(OSDConfigBuffer);
result = ReadOSDConfigPS2(osdConfigPS2, (const OSDConfigStore_t*)OSDConfigBuffer);
ReadOSDConfigPS1(osdConfigPS1, (const OSDConfigStore_t*)OSDConfigBuffer);
return result;
}
int OSDSaveConfigToNVM(const OSDConfig1_t* osdConfigPS1, const OSDConfig2_t* osdConfigPS2, u8 invalid)
{
WriteOSDConfigPS1((OSDConfigStore_t*)OSDConfigBuffer, osdConfigPS1);
WriteOSDConfigPS2((OSDConfigStore_t*)OSDConfigBuffer, osdConfigPS2, invalid);
WriteConfigToNVM(OSDConfigBuffer);
return 0;
}
// Directory management
const char* OSDGetHistoryDataFolder(void)
{
return SystemDataFolder;
}
const char* OSDGetSystemDataFolder(void)
{
return SystemDataFolder;
}
const char* OSDGetSystemExecFolder(void)
{
return SystemExecFolder;
}
const char* OSDGetDVDPLExecFolder(void)
{
return DVDPLExecFolder;
}
int OSDInitROMVER(void)
{
int fd;
memset(ConsoleROMVER, 0, ROMVER_MAX_LEN);
if ((fd = open("rom0:ROMVER", O_RDONLY)) >= 0)
{
read(fd, ConsoleROMVER, ROMVER_MAX_LEN);
close(fd);
}
return 0;
}