-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.c
More file actions
1228 lines (1108 loc) · 39.3 KB
/
main.c
File metadata and controls
1228 lines (1108 loc) · 39.3 KB
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>
#include <fcntl.h>
#include <tamtypes.h>
#include <kernel.h>
#include <sifrpc.h>
#include <loadfile.h>
#include <debug.h>
#include <iopcontrol.h>
#include <iop_regs.h>
#include <iopheap.h>
#include <sbv_patches.h>
#include <ps2sdkapi.h>
#include <usbhdfsd-common.h>
#include <osd_config.h>
#include <libpad.h>
#include <libmc.h>
#include <libcdvd.h>
#define NEWLIB_PORT_AWARE
#ifdef HDD
#include <hdd-ioctl.h>
#include <io_common.h>
#include <assert.h>
#include <libpwroff.h>
char PART[128] = "\0";
int HDD_USABLE = 0;
#define MPART PART
int LoadHDDIRX(void); // Load HDD IRXes
int MountParty(const char* path); ///processes strings in the format `hdd0:/$PARTITION:pfs:$PATH_TO_FILE/` to mount partition
int mnt(const char* path); ///mount partition specified on path
void HDDChecker();
void poweroffCallback(void *arg);
#else //this ensures that when HDD support is not available, loaded ELFs dont have any extra arg...
#define MPART NULL
#endif
#ifdef MX4SIO
int LookForBDMDevice(void);
#endif
#ifdef FILEXIO
#include <fileXio_rpc.h>
int LoadFIO(void); // Load FileXio and it´s dependencies
#endif
#include "debugprintf.h"
#include "pad.h"
#include "util.h"
#include "common.h"
#include "libcdvd_add.h"
#include "dvdplayer.h"
#include "OSDInit.h"
#include "OSDConfig.h"
#include "OSDHistory.h"
#include "ps1.h"
#include "ps2.h"
#include "modelname.h"
#include "banner.h"
#ifdef PSX
#include <iopcontrol_special.h>
#include "psx/plibcdvd_add.h"
#endif
#ifdef DEV9
static int dev9_loaded = 0;
int loadDEV9(void);
#endif
#ifdef UDPTTY
void loadUDPTTY();
#endif
// For avoiding define NEWLIB_AWARE
void fioInit();
#define RBG2INT(R, G, B) ((0 << 24) + (R << 16) + (G << 8) + B)
#define IMPORT_BIN2C(_n) \
extern unsigned char _n[]; \
extern unsigned int size_##_n
// --------------- IRX/IOPRP extern --------------- //
IMPORT_BIN2C(sio2man_irx);
IMPORT_BIN2C(mcman_irx);
IMPORT_BIN2C(mcserv_irx);
IMPORT_BIN2C(padman_irx);
#ifdef PSX
IMPORT_BIN2C(psx_ioprp);
#endif
#ifdef FILEXIO
IMPORT_BIN2C(iomanX_irx);
IMPORT_BIN2C(fileXio_irx);
#endif
#ifdef HDD
IMPORT_BIN2C(poweroff_irx);
IMPORT_BIN2C(ps2atad_irx);
IMPORT_BIN2C(ps2hdd_irx);
IMPORT_BIN2C(ps2fs_irx);
#endif
#ifdef UDPTTY
IMPORT_BIN2C(ps2ip_irx);
IMPORT_BIN2C(udptty_irx);
IMPORT_BIN2C(netman_irx);
IMPORT_BIN2C(smap_irx);
#endif
#ifdef MX4SIO
IMPORT_BIN2C(mx4sio_bd_irx);
#ifdef USE_ROM_SIO2MAN
#error MX4SIO needs Homebrew SIO2MAN to work
#endif
#endif
#ifdef DEV9
IMPORT_BIN2C(ps2dev9_irx);
#endif
#ifdef HAS_EMBEDDED_IRX
IMPORT_BIN2C(usbd_irx);
#ifdef NO_BDM
IMPORT_BIN2C(usb_mass_irx);
#else
IMPORT_BIN2C(bdm_irx);
IMPORT_BIN2C(bdmfs_fatfs_irx);
IMPORT_BIN2C(usbmass_bd_irx);
#endif // NO_BDM
#endif // HAS_EMBEDDED_IRX
// --------------- func defs --------------- //
void RunLoaderElf(char *filename, char *party);
void EMERGENCY(void);
void ResetIOP(void);
void SetDefaultSettings(void);
void TimerInit(void);
u64 Timer(void);
void TimerEnd(void);
char *CheckPath(char *path);
static void AlarmCallback(s32 alarm_id, u16 time, void *common);
int dischandler();
void CDVDBootCertify(u8 romver[16]);
void credits(void);
void CleanUp(void);
int LoadUSBIRX(void);
void runOSDNoUpdate(void);
#ifdef PSX
static void InitPSX();
#endif
#ifndef NO_TEMP_DISP
void PrintTemperature();
#endif
#ifdef HDD
int LoadHDDIRX(void); // Load HDD IRXes
int LoadFIO(void); // Load FileXio and it´s dependencies
int MountParty(const char* path); ///processes strings in the format `hdd0:/$PARTITION:pfs:$PATH_TO_FILE/` to mount partition
int mnt(const char* path); ///mount partition specified on path
#endif
// --------------- glob stuff --------------- //
typedef struct
{
int SKIPLOGO;
char *KEYPATHS[17][3];
int DELAY;
int OSDHISTORY_READ;
int TRAYEJECT;
int LOGO_DISP; //0: NO, 1: Only Console info, any other value: YES
} CONFIG;
CONFIG GLOBCFG;
char *EXECPATHS[3];
u8 ROMVER[16];
int PAD = 0;
static int config_source = SOURCE_INVALID;
int main(int argc, char *argv[])
{
u32 STAT;
u64 tstart;
int button, x, j, cnf_size, is_PCMCIA = 0, fd, result;
static int num_buttons = 4, pad_button = 0x0100; // first pad button is L2
unsigned char *RAM_p = NULL;
char *CNFBUFF, *name, *value;
#ifndef NO_DECKARD
#define XPARAM_PARAM_ADDR *((uint32_t *)0xFFFE01A0)
#define XPARAM_VALUE_ADDR *((uint32_t *)0xFFFE01A4)
if (IOP_CPU_TYPE == IOP_TYPE_POWERPC) { // this PS2 is DECKARD. reset the XPARAMS to their default value in case we are not coming from a reset
const u32 xparam_default[0x12] = {
0x01F4, 0x07D0, 0x0023, 0x07D0,
0x0014, 0x0000, 0x0001, 0x0020,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x229C, 0x06EC, 0x06EC,
0x0001, 0x0090
};
for (x = 0; x < 0x12; x++) {
XPARAM_PARAM_ADDR = 0xFFFFFFFF;
XPARAM_VALUE_ADDR = 0x0;
XPARAM_PARAM_ADDR = x;
XPARAM_VALUE_ADDR = xparam_default[x];
}
}
#endif
ResetIOP();
SifInitIopHeap(); // Initialize SIF services for loading modules and files.
SifLoadFileInit();
fioInit(); // NO scr_printf BEFORE here
init_scr();
scr_setCursor(0); // get rid of annoying that cursor.
DPRINTF_INIT()
#ifndef NO_DPRINTF
DPRINTF("PS2BBL: starting with %d argumments:\n", argc);
for (x = 0; x < argc; x++)
DPRINTF("\targv[%d] = [%s]\n", x, argv[x]);
#endif
scr_printf(".\n"); // GBS control does not detect image output with scr debug till the first char is printed
// print a simple dot to allow gbs control to start displaying video before banner and pad timeout begins to run. othersiwe, users with timeout lower than 4000 will have issues to respond in time
DPRINTF("enabling LoadModuleBuffer\n");
sbv_patch_enable_lmb(); // The old IOP kernel has no support for LoadModuleBuffer. Apply the patch to enable it.
DPRINTF("disabling MODLOAD device blacklist/whitelist\n");
sbv_patch_disable_prefix_check(); /* disable the MODLOAD module black/white list, allowing executables to be freely loaded from any device. */
#ifdef UDPTTY
if (loadDEV9())
loadUDPTTY();
#endif
#ifdef USE_ROM_SIO2MAN
j = SifLoadStartModule("rom0:SIO2MAN", 0, NULL, &x);
DPRINTF(" [SIO2MAN]: ID=%d, ret=%d\n", j, x);
#else
j = SifExecModuleBuffer(sio2man_irx, size_sio2man_irx, 0, NULL, &x);
DPRINTF(" [SIO2MAN]: ID=%d, ret=%d\n", j, x);
#endif
#ifdef USE_ROM_MCMAN
j = SifLoadStartModule("rom0:MCMAN", 0, NULL, &x);
DPRINTF(" [MCMAN]: ID=%d, ret=%d\n", j, x);
j = SifLoadStartModule("rom0:MCSERV", 0, NULL, &x);
DPRINTF(" [MCSERV]: ID=%d, ret=%d\n", j, x);
mcInit(MC_TYPE_MC);
#else
j = SifExecModuleBuffer(mcman_irx, size_mcman_irx, 0, NULL, &x);
DPRINTF(" [MCMAN]: ID=%d, ret=%d\n", j, x);
j = SifExecModuleBuffer(mcserv_irx, size_mcserv_irx, 0, NULL, &x);
DPRINTF(" [MCSERV]: ID=%d, ret=%d\n", j, x);
mcInit(MC_TYPE_XMC);
#endif
#ifdef USE_ROM_PADMAN
j = SifLoadStartModule("rom0:PADMAN", 0, NULL, &x);
DPRINTF(" [PADMAN]: ID=%d, ret=%d\n", j, x);
#else
j = SifExecModuleBuffer(padman_irx, size_padman_irx, 0, NULL, &x);
DPRINTF(" [PADMAN]: ID=%d, ret=%d\n", j, x);
#endif
j = LoadUSBIRX();
if (j != 0)
{
scr_setfontcolor(0x0000ff);
scr_printf("ERROR: could not load USB modules (%d)\n", j);
scr_setfontcolor(0xffffff);
#ifdef HAS_EMBEDDED_IRX //we have embedded IRX... something bad is going on if this condition executes. add a wait time for user to know something is wrong
sleep(1);
#endif
}
#ifdef FILEXIO
if (LoadFIO() < 0)
{scr_setbgcolor(0xff0000); scr_clear(); sleep(4);}
#endif
#ifdef MX4SIO
j = SifExecModuleBuffer(mx4sio_bd_irx, size_mx4sio_bd_irx, 0, NULL, &x);
DPRINTF(" [MX4SIO_BD]: ID=%d, ret=%d\n", j, x);
#endif
#ifdef HDD
else if (LoadHDDIRX() < 0) // only load HDD crap if filexio and iomanx are up and running
{scr_setbgcolor(0x0000ff); scr_clear(); sleep(4);}
#endif
if ((fd = open("rom0:ROMVER", O_RDONLY)) >= 0) {
read(fd, ROMVER, sizeof(ROMVER));
close(fd);
}
j = SifLoadModule("rom0:ADDDRV", 0, NULL); // Load ADDDRV. The OSD has it listed in rom0:OSDCNF/IOPBTCONF, but it is otherwise not loaded automatically.
DPRINTF(" [ADDDRV]: %d\n", j);
// Initialize libcdvd & supplement functions (which are not part of the ancient libcdvd library we use).
sceCdInit(SCECdINoD);
cdInitAdd();
DPRINTF("init OSD system paths\n");
OSDInitSystemPaths();
#ifndef PSX
DPRINTF("Certifying CDVD Boot\n");
CDVDBootCertify(ROMVER); /* This is not required for the PSX, as its OSDSYS will do it before booting the update. */
#endif
DPRINTF("init OSD\n");
InitOsd(); // Initialize OSD so kernel patches can do their magic
DPRINTF("init ROMVER, model name ps1dvr and dvdplayer ver\n");
OSDInitROMVER(); // Initialize ROM version (must be done first).
ModelNameInit(); // Initialize model name
PS1DRVInit(); // Initialize PlayStation Driver (PS1DRV)
DVDPlayerInit(); // Initialize ROM DVD player. It is normal for this to fail on consoles that have no DVD ROM chip (i.e. DEX or the SCPH-10000/SCPH-15000).
if (OSDConfigLoad() != 0) // Load OSD configuration
{ // OSD configuration not initialized. Defaults loaded.
scr_setfontcolor(0x00ffff);
DPRINTF("OSD Configuration not initialized. Defaults loaded.\n");
scr_setfontcolor(0xffffff);
}
DPRINTF("Saving OSD configuration\n");
OSDConfigApply();
/* Try to enable the remote control, if it is enabled.
Indicate no hardware support for it, if it cannot be enabled. */
DPRINTF("trying to enable remote control\n");
do {
result = sceCdRcBypassCtl(OSDConfigGetRcGameFunction() ^ 1, &STAT);
if (STAT & 0x100) { // Not supported by the PlayStation 2.
// Note: it does not seem like the browser updates the NVRAM here to change this status.
OSDConfigSetRcEnabled(0);
OSDConfigSetRcSupported(0);
break;
}
} while ((STAT & 0x80) || (result == 0));
// Remember to set the video output option (RGB or Y Cb/Pb Cr/Pr) accordingly, before SetGsCrt() is called.
DPRINTF("Setting vmode\n");
SetGsVParam(OSDConfigGetVideoOutput() == VIDEO_OUTPUT_RGB ? VIDEO_OUTPUT_RGB : VIDEO_OUTPUT_COMPONENT);
DPRINTF("Init pads\n");
PadInitPads();
DPRINTF("Init timer and wait for rescue mode key\n");
TimerInit();
tstart = Timer();
while (Timer() <= (tstart + 2000)) {
PAD = ReadCombinedPadStatus();
if ((PAD & PAD_R1) && (PAD & PAD_START)) // if ONLY R1+START are pressed...
EMERGENCY();
}
TimerEnd();
DPRINTF("load default settings\n");
SetDefaultSettings();
FILE *fp;
for (x = SOURCE_CWD; x >= SOURCE_MC0; x--) {
char* T = CheckPath(CONFIG_PATHS[x]);
fp = fopen(T, "r");
if (fp != NULL) {
config_source = x;
break;
}
}
if (config_source != SOURCE_INVALID) {
DPRINTF("valid config on device '%s', reading now\n", SOURCES[config_source]);
pad_button = 0x0001; // on valid config, change the value of `pad_button` so the pad detection loop iterates all the buttons instead of only those configured on default paths
num_buttons = 16;
fseek(fp, 0, SEEK_END);
cnf_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
DPRINTF("Allocating %d bytes for RAM_p\n", cnf_size);
RAM_p = (unsigned char *)malloc(cnf_size + 1);
if (RAM_p != NULL) {
CNFBUFF = RAM_p;
int temp;
if ((temp = fread(RAM_p, 1, cnf_size, fp)) == cnf_size) {
DPRINTF("Reading finished... Closing fp*\n");
fclose(fp);
CNFBUFF[cnf_size] = '\0';
int var_cnt = 0;
char TMP[64];
for (var_cnt = 0; get_CNF_string(&CNFBUFF, &name, &value); var_cnt++) {
// DPRINTF("reading entry %d", var_cnt);
if (!strcmp("OSDHISTORY_READ", name)) {
GLOBCFG.OSDHISTORY_READ = atoi(value);
continue;
}
if (!strncmp("LOAD_IRX_E", name, 10)) {
j = SifLoadStartModule(CheckPath(value), 0, NULL, &x);
DPRINTF("# Loaded IRX from config entry [%s] -> [%s]: ID=%d, ret=%d\n", name, value, j, x);
continue;
}
if (!strcmp("SKIP_PS2LOGO", name)) {
GLOBCFG.SKIPLOGO = atoi(value);
continue;
}
if (!strcmp("KEY_READ_WAIT_TIME", name)) {
GLOBCFG.DELAY = atoi(value);
continue;
}
if (!strcmp("EJECT_TRAY", name)) {
GLOBCFG.TRAYEJECT = atoi(value);
continue;
}
if (!strcmp("LOGO_DISPLAY", name)) {
GLOBCFG.LOGO_DISP = atoi(value);
continue;
}
if (!strncmp("LK_", name, 3)) {
for (x = 0; x < 17; x++) {
for (j = 0; j < 3; j++) {
sprintf(TMP, "LK_%s_E%d", KEYS_ID[x], j + 1);
if (!strcmp(name, TMP)) {
GLOBCFG.KEYPATHS[x][j] = value;
break;
}
}
}
}
}
free(RAM_p);
} else {
fclose(fp);
DPRINTF("\tERROR: could not read %d bytes of config file, only %d readed\n", cnf_size, temp);
#ifdef REPORT_FATAL_ERRORS
scr_setfontcolor(0x0000ff);
scr_printf("\tERROR: could not read %d bytes of config file, only %d readed\n", cnf_size, temp);
scr_setfontcolor(0xffffff);
#endif
}
} else {
DPRINTF("\tFailed to allocate %d+1 bytes!\n", cnf_size);
#ifdef REPORT_FATAL_ERRORS
scr_setbgcolor(0x0000ff);
scr_clear();
scr_printf("\tFailed to allocate %d+1 bytes!\n", cnf_size);
sleep(3);
scr_setbgcolor(0x000000);
scr_clear();
#endif
}
#ifdef HDD
if (config_source == SOURCE_HDD)
{
if (fileXioUmount("pfs0:") < 0)
DPRINTF("ERROR: Could not unmount 'pfs0:'\n");
}
#endif
} else {
scr_printf("Can't find config, loading hardcoded paths\n");
for (x = 0; x < 5; x++)
for (j = 0; j < 3; j++)
GLOBCFG.KEYPATHS[x][j] = CheckPath(DEFPATH[3 * x + j]);
sleep(1);
}
if (RAM_p != NULL)
free(RAM_p);
int R = 0x80, G = 0x80, B = 0x80;
if (GLOBCFG.OSDHISTORY_READ && (GLOBCFG.LOGO_DISP > 1)) {
j = 1;
// Try to load the history file from memory card slot 1
if (LoadHistoryFile(0) < 0) { // Try memory card slot 2
if (LoadHistoryFile(1) < 0) {
DPRINTF("no history files found\n\n");
j = 0;
}
}
if (j) {
for (j = 0; j < MAX_HISTORY_ENTRIES; j++) {
switch (j % 3) {
case 0:
R += (HistoryEntries[j].LaunchCount * 2);
break;
case 1:
G += (HistoryEntries[j].LaunchCount * 2);
break;
case 2:
B += (HistoryEntries[j].LaunchCount * 2);
break;
default:
B += (HistoryEntries[j].LaunchCount * 2);
}
}
scr_setfontcolor(RBG2INT(B, G, R));
DPRINTF("New banner color is: #%8x\n", RBG2INT(B, G, R));
} else
{
DPRINTF("can't find any osd history for banner color\n");
}
}
// Stores last key during DELAY msec
scr_clear();
if (GLOBCFG.LOGO_DISP > 1)
scr_printf("\n\n\n\n%s", BANNER);
scr_setfontcolor(0xffffff);
if (GLOBCFG.LOGO_DISP > 1)
scr_printf(BANNER_FOOTER);
if (GLOBCFG.LOGO_DISP > 0) {
scr_printf("\n\n\tModel:\t\t%s\n"
"\tPlayStation Driver:\t%s\n"
"\tDVD Player:\t%s\n"
"\tConfig source:\t%s\n",
ModelNameGet(),
PS1DRVGetVersion(),
DVDPlayerGetVersion(),
SOURCES[config_source]);
#ifndef NO_TEMP_DISP
PrintTemperature();
#endif
}
DPRINTF("Timer starts!\n");
TimerInit();
tstart = Timer();
while (Timer() <= (tstart + GLOBCFG.DELAY)) {
button = pad_button; // reset the value so we can iterate (bit-shift) again
PAD = ReadCombinedPadStatus_raw();
for (x = 0; x < num_buttons; x++) { // check all pad buttons
if (PAD & button) {
DPRINTF("PAD detected\n");
// if button detected , copy path to corresponding index
for (j = 0; j < 3; j++) {
EXECPATHS[j] = CheckPath(GLOBCFG.KEYPATHS[x + 1][j]);
if (exist(EXECPATHS[j])) {
scr_setfontcolor(0x00ff00);
scr_printf("\tLoading %s\n", EXECPATHS[j]);
if (!is_PCMCIA)
PadDeinitPads();
RunLoaderElf(EXECPATHS[j], MPART);
} else {
scr_setfontcolor(0x00ffff);
DPRINTF("%s not found\n", EXECPATHS[j]);
scr_setfontcolor(0xffffff);
}
}
break;
}
button = button << 1; // sll of 1 cleared bit to move to next pad button
}
}
DPRINTF("Wait time consummed. running AUTO entry\n");
TimerEnd();
for (j = 0; j < 3; j++) {
EXECPATHS[j] = CheckPath(GLOBCFG.KEYPATHS[0][j]);
if (exist(EXECPATHS[j])) {
scr_setfontcolor(0x00ff00);
scr_printf("\tLoading %s\n", EXECPATHS[j]);
if (!is_PCMCIA)
PadDeinitPads();
RunLoaderElf(EXECPATHS[j], MPART);
} else {
DPRINTF("%s not found\n", EXECPATHS[j]);
}
}
scr_clear();
scr_setfontcolor(0x00ffff);
scr_printf("\n\n\tEND OF EXECUTION REACHED\nCould not find any of the default applications\nCheck your config file for the LK_AUTO_E# entries\nOr press a key while logo displays to run the bound application\npress R1+START to enter emergency mode");
scr_setfontcolor(0xffffff);
while (1) {
sleep(1);
PAD = ReadCombinedPadStatus_raw();
if ((PAD & PAD_R1) && (PAD & PAD_START)) // if ONLY R1+START are pressed...
EMERGENCY();
}
return 0;
}
void EMERGENCY(void)
{
scr_clear();
scr_printf("\n\n\n\tEmergency mode\n\n\t doing infinite attempts to boot\n\t\tmass:/RESCUE.ELF\n");
scr_setfontcolor(0xffffff);
while (1) {
scr_printf(".");
sleep(1);
if (exist("mass:/RESCUE.ELF")) {
PadDeinitPads();
RunLoaderElf("mass:/RESCUE.ELF", NULL);
}
}
}
void runKELF(const char *kelfpath)
{
char arg3[64];
char *args[4] = {"-m rom0:SIO2MAN", "-m rom0:MCMAN", "-m rom0:MCSERV", arg3};
sprintf(arg3, "-x %s", kelfpath);
PadDeinitPads();
LoadExecPS2("moduleload", 4, args);
}
char *CheckPath(char *path)
{
if (path[0] == '$') // we found a program command
{
if (!strcmp("$CDVD", path))
dischandler();
if (!strcmp("$CDVD_NO_PS2LOGO", path)) {
GLOBCFG.SKIPLOGO = 1;
dischandler();
}
#ifdef HDD
if (!strcmp("$HDDCHECKER", path))
HDDChecker();
#endif
if (!strcmp("$CREDITS", path))
credits();
if (!strcmp("$OSDSYS", path))
runOSDNoUpdate();
if (!strncmp("$RUNKELF:", path, strlen("$RUNKELF:"))) {
runKELF(CheckPath(path + strlen("$RUNKELF:"))); // pass to runKELF the path without the command token, digested again by CheckPath()
}
}
if (!strncmp("mc?", path, 3)) {
path[2] = (config_source == SOURCE_MC1) ? '1' : '0';
if (exist(path)) {
return path;
} else {
path[2] = (config_source == SOURCE_MC1) ? '0' : '1';
if (exist(path))
return path;
}
#ifdef HDD
} else if (!strncmp("hdd", path, 3)) {
if (MountParty(path) < 0)
{
DPRINTF("-{%s}-\n", path);
return path;
}
else
{
DPRINTF("--{%s}--{%s}\n", path, strstr(path, "pfs:"));
return strstr(path, "pfs:");
} // leave path as pfs:/blabla
if (!MountParty(path))
return strstr(path, "pfs:");
#endif
#ifdef MX4SIO
} else if (!strncmp("massX:", path, 6)) {
int x = LookForBDMDevice();
if (x >= 0)
path[4] = '0' + x;
#endif
}
return path;
}
void SetDefaultSettings(void)
{
int i, j;
for (i = 0; i < 17; i++)
for (j = 0; j < 3; j++)
GLOBCFG.KEYPATHS[i][j] = "isra:/";
GLOBCFG.SKIPLOGO = 0;
GLOBCFG.OSDHISTORY_READ = 1;
GLOBCFG.DELAY = DEFDELAY;
GLOBCFG.TRAYEJECT = 0;
GLOBCFG.LOGO_DISP = 2;
}
int LoadUSBIRX(void)
{
int ID, RET;
// ------------------------------------------------------------------------------------ //
#ifdef HAS_EMBEDDED_IRX
ID = SifExecModuleBuffer(bdm_irx, size_bdm_irx, 0, NULL, &RET);
#else
ID = loadIRXFile("mc?:/PS2BBL/BDM.IRX", 0, NULL, &RET);
#endif
DPRINTF(" [BDM]: ret=%d, ID=%d\n", RET, ID);
if (ID < 0 || RET == 1) return -1;
// ------------------------------------------------------------------------------------ //
#ifdef HAS_EMBEDDED_IRX
ID = SifExecModuleBuffer(bdmfs_fatfs_irx, size_bdmfs_fatfs_irx, 0, NULL, &RET);
#else
ID = loadIRXFile("mc?:/PS2BBL/BDMFS_FATFS.IRX", 0, NULL, &RET);
#endif
DPRINTF(" [BDMFS_FATFS]: ret=%d, ID=%d\n", RET, ID);
if (ID < 0 || RET == 1) return -2;
// ------------------------------------------------------------------------------------ //
#ifdef HAS_EMBEDDED_IRX
ID = SifExecModuleBuffer(usbd_irx, size_usbd_irx, 0, NULL, &RET);
#else
ID = loadIRXFile("mc?:/PS2BBL/USBD.IRX", 0, NULL, &RET);
#endif
delay(3);
DPRINTF(" [USBD]: ret=%d, ID=%d\n", RET, ID);
if (ID < 0 || RET == 1) return -3;
// ------------------------------------------------------------------------------------ //
#ifdef HAS_EMBEDDED_IRX
ID = SifExecModuleBuffer(usbmass_bd_irx, size_usbmass_bd_irx, 0, NULL, &RET);
#else
ID = loadIRXFile("mc?:/PS2BBL/USBMASS_BD.IRX", 0, NULL, &RET);
#endif
DPRINTF(" [USBMASS_BD]: ret=%d, ID=%d\n", RET, ID);
if (ID < 0 || RET == 1) return -4;
// ------------------------------------------------------------------------------------ //
struct stat buffer;
int ret = -1;
int retries = 50;
while (ret != 0 && retries > 0) {
ret = stat("mass:/", &buffer);
/* Wait until the device is ready */
nopdelay();
retries--;
}
return 0;
}
#ifdef MX4SIO
int LookForBDMDevice(void)
{
static char mass_path[] = "massX:";
static char DEVID[5];
int dd;
int x = 0;
for (x = 0; x < 5; x++)
{
mass_path[4] = '0' + x;
if ((dd = fileXioDopen(mass_path)) >= 0) {
int *intptr_ctl = (int *)DEVID;
*intptr_ctl = fileXioIoctl(dd, USBMASS_IOCTL_GET_DRIVERNAME, "");
close(dd);
if (!strncmp(DEVID, "sdc", 3))
{
DPRINTF("%s: Found MX4SIO device at mass%d:/\n", __func__, x);
return x;
}
}
}
return -1;
}
#endif
#ifdef FILEXIO
int LoadFIO(void)
{
int ID, RET;
ID = SifExecModuleBuffer(&iomanX_irx, size_iomanX_irx, 0, NULL, &RET);
DPRINTF(" [IOMANX]: ret=%d, ID=%d\n", RET, ID);
if (ID < 0 || RET == 1)
return -1;
/* FILEXIO.IRX */
ID = SifExecModuleBuffer(&fileXio_irx, size_fileXio_irx, 0, NULL, &RET);
DPRINTF(" [FILEXIO]: ret=%d, ID=%d\n", RET, ID);
if (ID < 0 || RET == 1)
return -2;
RET = fileXioInit();
DPRINTF("fileXioInit: %d\n", RET);
return 0;
}
#endif
#ifdef DEV9
int loadDEV9(void)
{
if (!dev9_loaded)
{
int ID, RET;
ID = SifExecModuleBuffer(&ps2dev9_irx, size_ps2dev9_irx, 0, NULL, &RET);
DPRINTF("[DEV9]: ret=%d, ID=%d\n", RET, ID);
if (ID < 0 && RET == 1) // ID smaller than 0: issue reported from modload | RET == 1: driver returned no resident end
return 0;
dev9_loaded = 1;
}
return 1;
}
#endif
#ifdef UDPTTY
void loadUDPTTY()
{
int ID, RET;
ID = SifExecModuleBuffer(&netman_irx, size_netman_irx, 0, NULL, &RET);
DPRINTF(" [NETMAN]: ret=%d, ID=%d\n", RET, ID);
ID = SifExecModuleBuffer(&smap_irx, size_smap_irx, 0, NULL, &RET);
DPRINTF(" [SMAP]: ret=%d, ID=%d\n", RET, ID);
ID = SifExecModuleBuffer(&ps2ip_irx, size_ps2ip_irx, 0, NULL, &RET);
DPRINTF(" [PS2IP]: ret=%d, ID=%d\n", RET, ID);
ID = SifExecModuleBuffer(&udptty_irx, size_udptty_irx, 0, NULL, &RET);
DPRINTF(" [UDPTTY]: ret=%d, ID=%d\n", RET, ID);
sleep(3);
}
#endif
#ifdef HDD
static int CheckHDD(void) {
int ret = fileXioDevctl("hdd0:", HDIOC_STATUS, NULL, 0, NULL, 0);
/* 0 = HDD connected and formatted, 1 = not formatted, 2 = HDD not usable, 3 = HDD not connected. */
DPRINTF("%s: HDD status is %d\n", __func__, ret);
if ((ret >= 3) || (ret < 0))
return -1;
return ret;
}
int LoadHDDIRX(void)
{
int ID, RET, HDDSTAT;
static const char hddarg[] = "-o" "\0" "4" "\0" "-n" "\0" "20";
//static const char pfsarg[] = "-n\0" "24\0" "-o\0" "8";
if (!loadDEV9())
return -1;
ID = SifExecModuleBuffer(&poweroff_irx, size_poweroff_irx, 0, NULL, &RET);
DPRINTF(" [POWEROFF]: ret=%d, ID=%d\n", RET, ID);
if (ID < 0 || RET == 1)
return -2;
poweroffInit();
poweroffSetCallback(&poweroffCallback, NULL);
DPRINTF("PowerOFF Callback installed...\n");
ID = SifExecModuleBuffer(&ps2atad_irx, size_ps2atad_irx, 0, NULL, &RET);
DPRINTF(" [ATAD]: ret=%d, ID=%d\n", RET, ID);
if (ID < 0 || RET == 1)
return -3;
ID = SifExecModuleBuffer(&ps2hdd_irx, size_ps2hdd_irx, sizeof(hddarg), hddarg, &RET);
DPRINTF(" [PS2HDD]: ret=%d, ID=%d\n", RET, ID);
if (ID < 0 || RET == 1)
return -4;
HDDSTAT = CheckHDD();
HDD_USABLE = !(HDDSTAT < 0);
/* PS2FS.IRX */
if (HDD_USABLE)
{
ID = SifExecModuleBuffer(&ps2fs_irx, size_ps2fs_irx, 0, NULL, &RET);
DPRINTF(" [PS2FS]: ret=%d, ID=%d\n", RET, ID);
if (ID < 0 || RET == 1)
return -5;
}
return 0;
}
int MountParty(const char* path)
{
int ret = -1;
DPRINTF("%s: %s\n", __func__, path);
char* BUF = NULL;
BUF = strdup(path); //use strdup, otherwise, path will become `hdd0:`
char MountPoint[40];
if (getMountInfo(BUF, NULL, MountPoint, NULL))
{
mnt(MountPoint);
if (BUF != NULL)
free(BUF);
strcpy(PART, MountPoint);
strcat(PART, ":");
return 0;
} else {
DPRINTF("ERROR: could not process path '%s'\n", path);
PART[0] = '\0';
}
if (BUF != NULL)
free(BUF);
return ret;
}
int mnt(const char* path)
{
DPRINTF("Mounting '%s'\n", path);
if (fileXioMount("pfs0:", path, FIO_MT_RDONLY ) < 0) // mount
{
DPRINTF("Mount failed. unmounting pfs0 and trying again...\n");
if (fileXioUmount("pfs0:") < 0) //try to unmount then mount again in case it got mounted by something else
{
DPRINTF("Unmount failed!!!\n");
}
if (fileXioMount("pfs0:", path, FIO_MT_RDONLY ) < 0)
{
DPRINTF("mount failed again!\n");
return -4;
} else {
DPRINTF("Second mount succed!\n");
}
} else DPRINTF("mount successfull on first attemp\n");
return 0;
}
void HDDChecker()
{
char ErrorPartName[64];
const char* HEADING = "HDD Diagnosis routine";
int ret = -1;
scr_clear();
scr_printf("\n\n%*s%s\n", ((80 - strlen(HEADING)) / 2), "", HEADING);
scr_setfontcolor(0x0000FF);
ret = fileXioDevctl("hdd0:", HDIOC_STATUS, NULL, 0, NULL, 0);
if (ret == 0 || ret == 1) scr_setfontcolor(0x00FF00);
if (ret != 3)
{
scr_printf("\t\t - HDD CONNECTION STATUS: %d\n", ret);
/* Check ATA device S.M.A.R.T. status. */
ret = fileXioDevctl("hdd0:", HDIOC_SMARTSTAT, NULL, 0, NULL, 0);
if (ret != 0) scr_setfontcolor(0x0000ff); else scr_setfontcolor(0x00FF00);
scr_printf("\t\t - S.M.A.R.T STATUS: %d\n", ret);
/* Check for unrecoverable I/O errors on sectors. */
ret = fileXioDevctl("hdd0:", HDIOC_GETSECTORERROR, NULL, 0, NULL, 0);
if (ret != 0) scr_setfontcolor(0x0000ff); else scr_setfontcolor(0x00FF00);
scr_printf("\t\t - SECTOR ERRORS: %d\n", ret);
/* Check for partitions that have errors. */
ret = fileXioDevctl("hdd0:", HDIOC_GETERRORPARTNAME, NULL, 0, ErrorPartName, sizeof(ErrorPartName));
if (ret != 0) scr_setfontcolor(0x0000ff); else scr_setfontcolor(0x00FF00);
scr_printf("\t\t - CORRUPTED PARTITIONS: %d\n", ret);
if (ret != 0)
{
scr_printf("\t\tpartition: %s\n", ErrorPartName);
}
} else scr_setfontcolor(0x00FFFF), scr_printf("Skipping test, HDD is not connected\n");
scr_setfontcolor(0xFFFFFF);
scr_printf("\t\tWaiting for 10 seconds...\n");
sleep(10);
}
/// @brief poweroff callback function
/// @note only expansion bay models will properly make use of this. the other models will run the callback but will poweroff themselves before reaching function end...
void poweroffCallback(void *arg)
{
fileXioDevctl("pfs:", PDIOC_CLOSEALL, NULL, 0, NULL, 0);
while (fileXioDevctl("dev9x:", DDIOC_OFF, NULL, 0, NULL, 0) < 0) {};
// As required by some (typically 2.5") HDDs, issue the SCSI STOP UNIT command to avoid causing an emergency park.
fileXioDevctl("mass:", USBMASS_DEVCTL_STOP_ALL, NULL, 0, NULL, 0);
/* Power-off the PlayStation 2. */
poweroffShutdown();
}
#endif
int dischandler()
{
int OldDiscType, DiscType, ValidDiscInserted, result, first_run = 1;
u32 STAT;
scr_clear();
scr_printf("\n\t%s: Activated\n", __func__);
scr_printf("\t\tEnabling Diagnosis...\n");
do { // 0 = enable, 1 = disable.
result = sceCdAutoAdjustCtrl(0, &STAT);
} while ((STAT & 0x08) || (result == 0));
// For this demo, wait for a valid disc to be inserted.
scr_printf("\tWaiting for disc to be inserted...\n\n");
ValidDiscInserted = 0;
OldDiscType = -1;
while (!ValidDiscInserted) {
DiscType = sceCdGetDiskType();
if (DiscType != OldDiscType) {
scr_printf("\tNew Disc:\t");
OldDiscType = DiscType;
switch (DiscType) {
case SCECdNODISC:
if (first_run) {
if (GLOBCFG.TRAYEJECT) // if tray eject is allowed on empty tray...
sceCdTrayReq(0, NULL);
first_run = 0;
}
scr_setfontcolor(0x0000ff);
scr_printf("No Disc\n");
scr_setfontcolor(0xffffff);
break;
case SCECdDETCT:
case SCECdDETCTCD:
case SCECdDETCTDVDS:
case SCECdDETCTDVDD:
scr_printf("Reading...\n");
break;
case SCECdPSCD:
case SCECdPSCDDA:
scr_setfontcolor(0x00ff00);
scr_printf("PlayStation\n");
scr_setfontcolor(0xffffff);
ValidDiscInserted = 1;
break;
case SCECdPS2CD:
case SCECdPS2CDDA: