-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlauncher2.c
2780 lines (2389 loc) · 80 KB
/
launcher2.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
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
/*
_____ ___ ____
____| | ____| PS2 Open Source Project
| ___| |____
---------------------------------------------------------------------------
Copyright (C) 2008 - Neme & jimmikaelkael (www.psx-scene.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the Free McBoot License.
This program and any related documentation is provided "as is"
WITHOUT ANY WARRANTIES, either express or implied, including, but not
limited to, implied warranties of fitness for a particular purpose. The
entire risk arising out of use or performance of the software remains
with you.
In no event shall the author be liable for any damages whatsoever
(including, without limitation, damages to your hardware or equipment,
environmental damage, loss of health, or any kind of pecuniary loss)
arising out of the use of or inability to use this software or
documentation, even if the author has been advised of the possibility of
such damages.
You should have received a copy of the Free McBoot License along with
this program; if not, please report at psx-scene :
http://psx-scene.com/forums/freevast/
---------------------------------------------------------------------------
Init module for cvdvd init fix and multilanguage fix are from :
ffgriever (www.psx-scene.com)
Modifications for working just from mc0:/BOOT/ are from :
Howling Wolf & Chelsea
---------------------------------------------------------------------------
*/
#define FMCBVER "1.0.0"
#include <tamtypes.h>
#include <kernel.h>
#include <sifrpc.h>
#include <fileio.h>
#include <iopcontrol.h>
#include <iopheap.h>
#include <sbv_patches.h>
#include <loadfile.h>
#include <libpad.h>
#include <string.h>
#include <stdio.h>
#include <osd_config.h>
#include <libcdvdmod.h>
#include <malloc.h>
#include <debug.h>
#include "splash.h"
//#include "loading.h"
void _ps2sdk_libc_init() {}
void _ps2sdk_libc_deinit() {}
#define MAX_PATH 260
extern void iomanx_irx;
extern int size_iomanx_irx;
extern void init_irx;
extern int size_init_irx;
extern void chkesr_irx;
extern int size_chkesr_irx;
extern void elf_loader;
extern int size_elf_loader;
#define DEBUG
#define RGBA(r, g, b, a) \
((u64)(r) | ((u64)(g) << 8) | ((u64)(b) << 16) | ((u64)(a) << 24))
#ifdef DEBUG
#define SETBG(r, g, b) *(u64 *)0x120000e0 = RGBA(r, g, b, 0)
#else
#define SETBG(...)
#endif
// GS related
#define NTSC 2
#define PAL 3
int VMode;
// Externals:
// chkesr_rpc.c
extern int chkesr_rpc_Init(void);
extern int Check_ESR_Disc(void);
// timer.c
extern void TimerInit(void);
extern u64 Timer(void);
extern void TimerEnd(void);
// pad.c
extern u32 new_pad;
extern int readPad(void);
extern void waitAnyPadReady(void);
extern int setupPad(void);
// gs.c
typedef enum {
PAL_640_512_32,
NTSC_640_448_32
} gs_video_mode;
extern void gs_reset(void);
extern int gs_init(gs_video_mode mode);
extern void gs_set_fill_color(u8 r, u8 g, u8 b);
extern void gs_fill_rect(u16 x0, u16 y0, u16 x1, u16 y1);
extern u16 gs_get_max_x(void);
extern u16 gs_get_max_y(void);
extern void gs_print_bitmap(u16 x, u16 y, u16 w, u16 h, u32 *data);
// function prototypes
void wipeUserMem(void);
int file_exists(char *filepath);
int get_CNF_string(unsigned char **CNF_p_p, unsigned char **name_p_p, unsigned char **value_p_p);
int loadConfig(void);
int Read_SYSTEM_CNF(char *boot_path, char *ver);
u8 *find_bytes_with_mask(u8 *buf, u32 bufsize, u8 *bytes, u8 *mask, u32 len);
u8 *find_string(const u8 *string, u8 *buf, u32 bufsize);
const char *get_string_pointer(const char **strings, u32 index);
int handle_menu_selection(int selected);
void patch_menu(u8 *osd);
void draw_menu_item_selected(int X, int Y, u32 *color, int alpha, const char *string, int num);
void draw_menu_item_unselected(int X, int Y, u32 *color, int alpha, const char *string, int num);
void patch_draw_menu(u8 *osd);
void get_buttons_panel_type(int type);
void draw_nonselectable_item_left(int X, int Y, u32 *color, int alpha, const char *string);
void draw_nonselectable_item_right(int X, int Y, u32 *color, int alpha, const char *string);
void draw_icon_left(int type, int X, int Y, int alpha);
void draw_icon_right(int type, int X, int Y, int alpha);
void patch_button_panel(u8 *osd);
int deviated_exec_dvdv(void);
int deviated_exec_ps2dvd(void);
void patch_auto_launch(u8 *osd);
void patch_skip_disc(u8 *osd);
void patch_loop_menu(u8 *osd);
void patch_force_video_mode(u8 *osd);
void patch_skip_hdd(u8 *osd);
void patch_and_execute_osdsys(void *epc, void *gp);
void IOP_Reset(void);
void load_modules(void);
void load_chkesr_module(void);
void CleanUp(int iop_reset);
void OSDSYS_CleanUp(void);
void launch_osdsys(void);
void check_path(void);
void reload_osdsys(void);
void load_elf(char *elf_path);
void check_ESR_paths(void);
int FastBoot_Disc(void);
void check_exec_paths(void);
void Set_Default_Settings(void);
void SetOsdConfig(void);
void FMCB_loader_Init(void);
// region letter list
char *MG_region[8] = {"A", "E", "U", "J", "M", "O", "R", "C"};
u8 MG_REGION[1];
// Paths that can be changed with those from cnf.
char esr_path[3][MAX_PATH] = {
"mass:/BOOT/ESR.ELF",
"mc?:/BOOT/ESR.ELF",
"mc?:/B?DATA-SYSTEM/ESR.ELF"};
// Hardcoded paths
char default_path[15][30] = {
"mc?:/BOOT/ULE.ELF", // Auto index : 0
"mc?:/BOOT/BOOT2.ELF",
"mc?:/APPS/ULE.ELF",
"mass:/BOOT/BOOT4.ELF", // L2 index : 3
"mc?:/BOOT/BOOT4.ELF",
"mc?:/B?DATA-SYSTEM/BOOT4.ELF",
"mass:/BOOT/BOOT2.ELF", // R2 index : 6
"mc?:/BOOT/BOOT2.ELF",
"mc?:/B?DATA-SYSTEM/BOOT2.ELF",
"mass:/BOOT/BOOT3.ELF", // L1 index : 9
"mc?:/BOOT/BOOT3.ELF",
"mc?:/B?DATA-SYSTEM/BOOT3.ELF",
"mass:/RESCUE.ELF", // R1 index : 12
"mc?:/BOOT/BOOT1.ELF",
"mc?:/B?DATA-SYSTEM/BOOT1.ELF"};
char rescue_path[30] = "mass:/RESCUE.ELF";
char cnf_path_usb[30] = "mass:/FUNTUNA.CNF";
char cnf_path[30] = "mc0:/BOOT/FUNTUNA.CNF";
/**********************USBD.IRX******************************/
char usbd_irx_path[30] = "mc0:/BOOT/USBD.IRX";
char usbd_irx_path_sysconf[30] = "mc0:/SYS-CONF/USBD.IRX";
/**********************USBHDFSD.IRX************************/
char usb_mass_irx_path[30] = "mc0:/BOOT/USBHDFSD.IRX";
char usb_mass_irx_path_sysconf[30] = "mc0:/SYS-CONF/USBHDFSD.IRX";
// DVD-Player Update path
char dvdpl_path[] = "mc0:/BREXEC-DVDPLAYER/dvdplayer.elf";
// Buttons ID must be kept in this order !
// They are readed in a loop depending on their libpad value
char LK_ID[17][10] = {
"Auto",
"Select", // 0x0001
"L3", // 0x0002
"R3", // 0x0004
"Start", // 0x0008
"Up", // 0x0010
"Right", // 0x0020
"Down", // 0x0040
"Left", // 0x0080
"L2", // 0x0100
"R2", // 0x0200
"L1", // 0x0400
"R1", // 0x0800
"Triangle", // 0x1000
"Circle", // 0x2000
"Cross", // 0x4000
"Square" // 0x8000
};
#define NEWITEMS 100 // the number of max added menu items in osdsys
// OSDSYS settings struct, contains all configurable OSDSYS settings
// takes 1700 bytes with 100 items
typedef struct
{
int hack_enabled; // Enable/Disable OSDSYS hacking
int skip_mc; // Enable/Disable MC Update check
int skip_hdd; // Enable/Disable HDD Update check
int skip_disc; // Enable/Disable disc boot while inserting them while OSDSYS is loaded
int skip_logo; // Enable/Disable Sony Entertainment logo while loading OSDSYS
int goto_inner_browser; // Enable/Disable goes to inner_browser while loading OSDSYS
char *video_mode; // Set OSDSYS Video mode : AUTO, PAL or NTSC
int scroll_menu; // Enable/Disable scrolling menu
int menu_x; // Set menu X coordinate (menu center)
int menu_y; // Set menu Y coordinate (menu center), only for scroll menu
int enter_x; // Set "Enter" button X coordinate (at main OSDSYS menu)
int enter_y; // Set "Enter" button Y coordinate (at main OSDSYS menu)
int version_x; // Set "Version" button X coordinate (at main OSDSYS menu)
int version_y; // Set "Version" button Y coordinate (at main OSDSYS menu)
int cursor_max_velocity; // Set the cursors movement amplitude, only for scroll menu
int cursor_acceleration; // Set the cursors speed, only for scroll menu
char *left_cursor; // Set the left cursor text, only for scroll menu
char *right_cursor; // Set the right cursor text, only for scroll menu
char *menu_top_delimiter; // Set the top menu delimiter text, only for scroll menu
char *menu_bottom_delimiter; // Set the bottom menu delimiter text, only for scroll menu
u32 selected_color[4]; // Set the menu items color when selected
u32 unselected_color[4]; // Set the menu items color when not selected
int num_displayed_items; // Set the number of menu items displayed, only for scroll menu
char *item_name[NEWITEMS]; // Set menu items text
char *item_path[NEWITEMS][3]; // Set 3 paths for each menu items
} osdsys_settings;
// FMCB settings struct, loader settings
typedef struct
{
int pad_delay; // Set the total pad press delay
int debug_screen; // Enable/Disable green debug screen when all 3 paths are not valid
int fastboot; // Enable/Disable FastBoot for PS1, PS2, ESR, Video DVD, Audio CD Discs
char *p_LK_Path[17][3]; // Paths pointers for pad
char *p_ESR_Path[3]; // Paths pointers for ESR
int autolaunch_patch; // Enable/Disable autolaunch_patch (The one that allows ESR disc to be
} fmcb_settings; // booted, and PS2 DVD fix on some older PS2), not user configureable
// variables to hold FMCB & OSDSYS settings
osdsys_settings OSDSYS;
fmcb_settings FMCB;
char romver_region_char[1];
char *p_ExecPath;
char *p_pad_ExecPath[3];
char cdboot_path[MAX_PATH];
char eromdrv_arg[MAX_PATH];
char dvdpl_arg[MAX_PATH];
int item_cnt = 0; // Counter for OSDSYS menu printable items (existing and checked)
int pad_inited = 0;
int timer_inited = 0;
int cdvdrpc_inited = 0;
int fastboot_delay = 10; // not user configurable, 10ms minimum
int old_dvdelf = 1; // DVDELF version, Fat PS2 one by default
char *valid_ESR_path; // Tested valid ESR path
u8 romver[16];
int call_from_osdsys = 0; // flag to be set to 1 when our functions are called from OSDSYS
//int loading_print = 0; // flag to set to 1 if we want to print "loading" bitmap when load_elf func is called
u32 bios_version = 0; // Bios revision (acquired from init.irx)
int isEarlyJap = 0; // To determine if the ps2 is an early Jap
int dummy_memalloc = 1;
int boot_from_mc = 0; // To determine from which mc FMCB has been booted
unsigned char *CNF_RAM_p; // pointer to CNF file into memory
//--------------------------------------------------------------
void wipeUserMem(void)
{ // Clean user memory
int i;
for (i = 0x100000; i < 0x2000000; i += 64) {
asm(
"\tsq $0, 0(%0) \n"
"\tsq $0, 16(%0) \n"
"\tsq $0, 32(%0) \n"
"\tsq $0, 48(%0) \n" ::"r"(i));
}
}
//--------------------------------------------------------------
int file_exists(char *filepath)
{
int fdn;
fdn = open(filepath, O_RDONLY);
if (fdn < 0)
return 0;
close(fdn);
return 1;
}
//________________ From uLaunchELF ______________________
//---------------------------------------------------------------------------
// get_CNF_string is the main CNF parser called for each CNF variable in a
// CNF file. Input and output data is handled via its pointer parameters.
// The return value flags 'false' when no variable is found. (normal at EOF)
//---------------------------------------------------------------------------
int get_CNF_string(unsigned char **CNF_p_p,
unsigned char **name_p_p,
unsigned char **value_p_p)
{
unsigned char *np, *vp, *tp = *CNF_p_p;
start_line:
while ((*tp <= ' ') && (*tp > '\0'))
tp += 1; //Skip leading whitespace, if any
if (*tp == '\0')
return 0; //but exit at EOF
np = tp; //Current pos is potential name
if (*tp < 'A') //but may be a comment line
{ //We must skip a comment line
while ((*tp != '\r') && (*tp != '\n') && (*tp > '\0'))
tp += 1; //Seek line end
goto start_line; //Go back to try next line
}
while ((*tp >= 'A') || ((*tp >= '0') && (*tp <= '9')))
tp += 1; //Seek name end
if (*tp == '\0')
return 0; //but exit at EOF
while ((*tp <= ' ') && (*tp > '\0'))
*tp++ = '\0'; //zero&skip post-name whitespace
if (*tp != '=')
return 0; //exit (syntax error) if '=' missing
*tp++ = '\0'; //zero '=' (possibly terminating name)
while ((*tp <= ' ') && (*tp > '\0') //Skip pre-value whitespace, if any
&& (*tp != '\r') && (*tp != '\n') //but do not pass the end of the line
&& (*tp != '\7') //allow ctrl-G (BEL) in value
)
tp += 1;
if (*tp == '\0')
return 0; //but exit at EOF
vp = tp; //Current pos is potential value
while ((*tp != '\r') && (*tp != '\n') && (*tp != '\0'))
tp += 1; //Seek line end
if (*tp != '\0')
*tp++ = '\0'; //terminate value (passing if not EOF)
while ((*tp <= ' ') && (*tp > '\0'))
tp += 1; //Skip following whitespace, if any
*CNF_p_p = tp; //return new CNF file position
*name_p_p = np; //return found variable name
*value_p_p = vp; //return found variable value
return 1; //return control to caller
} //Ends get_CNF_string
//---------------------------------------------------------------------------------------
char *replace_var(char *str, char *orig, char *rep)
{
static char buffer[60];
char *p;
if (!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
return str;
strncpy(buffer, str, p - str); // Copy characters from 'str' start to 'orig' st$
buffer[p - str] = '\0';
sprintf(buffer + (p - str), "%s%s", rep, p + strlen(orig));
return buffer;
}
//---------------------------------------------------------
//replace_wildcards
//---------------------------------------------------------
//----------------------------------------------------------------
// Load CNF
//----------------------------------------------------------------
int loadConfig(void)
{
char *CNF_LOADED;
char *version = "1.0.1";
int i, j, fd, var_cnt, CNF_version;
size_t CNF_size;
char tsts[64];
char path[MAX_PATH];
unsigned char *CNF_p, *name, *value;
char hexvalue_buf[4];
u8 *dummy_p = NULL;
u32 dummy_sz;
strcpy(path, cnf_path_usb);
fd = -1;
fd = open(path, O_RDONLY); // Try to open cnf from USB first
if (fd < 0) {
strcpy(path, cnf_path);
fd = -1;
if (boot_from_mc == 1) //if booting from MC1
path[2] = '1'; //try with MC1 first
fd = open(path, O_RDONLY); // Try to open cnf from the MC that FMCB was booted from
if (fd < 0) {
if (boot_from_mc == 1)
path[2] = '0';
else
path[2] = '1';
fd = -1;
fd = open(path, O_RDONLY); // Try to open cnf from the other MC
if (boot_from_mc == 1)
CNF_LOADED = "mc0"; //set %CNF% wildcard
else
CNF_LOADED = "mc1"; //set %CNF% wildcard
} else {
if (boot_from_mc == 1)
CNF_LOADED = "mc1"; //set %CNF% wildcard
else
CNF_LOADED = "mc0"; //set %CNF% wildcard
}
if (fd < 0) {
failed_load:
return 0; // This point is only reached after succefully opening CNF
}
} else
CNF_LOADED = "mass"; //set %CNF% wildcard
CNF_size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
//init_scr();
//scr_clear();
if (dummy_memalloc) {
// This part ensures that CNF content loads at 0x01f00000 leaving sufficient space for big cnf files
// If we don't do this, memory will be allocated just after the loader, something like 0x000ecb00
// leading to leave unsufficient space for big cnf files since OSDSYS needs to load at 0x00100000
//scr_printf("\n\t allocating dummy space\n");
dummy_p = (char *)malloc(32);
dummy_sz = 0x01f00000 - (u32)(dummy_p)-16;
free(dummy_p);
dummy_p = (char *)malloc(dummy_sz);
//scr_printf("\t dummy_sz: %08x\n", (u32)dummy_sz);
//scr_printf("\t dummy_p: %08x\n", (u32)dummy_p);
}
CNF_RAM_p = (char *)malloc(CNF_size);
//scr_printf("\n\t CNF_RAM_p: %08x\n", (u32)CNF_RAM_p);
if (dummy_p != NULL) {
free(dummy_p);
dummy_memalloc = 0;
}
CNF_p = CNF_RAM_p;
if (CNF_p == NULL) {
close(fd);
goto failed_load;
}
read(fd, CNF_p, CNF_size); // Read CNF as one long string
close(fd);
CNF_p[CNF_size] = '\0'; // Terminate the CNF string
CNF_version = 0; // The CNF version is still unidentified
for (var_cnt = 0; get_CNF_string(&CNF_p, &name, &value); var_cnt++) {
// A variable was found, now we dispose of its value.
if (!strcmp(name, "CNF_version")) {
CNF_version = atoi(value);
continue;
} else if (CNF_version == 0) {
goto failed_load; // Refuse unidentified CNF
}
if (!strcmp(name, "OSDSYS_video_mode")) {
OSDSYS.video_mode = value;
continue;
}
for (i = 0; i < 3; i++) {
sprintf(tsts, "ESR_Path_E%d", i + 1);
if (!strcmp(name, tsts)) {
FMCB.p_ESR_Path[i] = value;
continue;
}
}
if (!strcmp(name, "pad_delay")) {
FMCB.pad_delay = atoi(value);
continue;
}
if (!strcmp(name, "hacked_OSDSYS")) {
OSDSYS.hack_enabled = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_scroll_menu")) {
OSDSYS.scroll_menu = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_menu_x")) {
OSDSYS.menu_x = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_menu_y")) {
OSDSYS.menu_y = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_enter_x")) {
OSDSYS.enter_x = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_enter_y")) {
OSDSYS.enter_y = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_version_x")) {
OSDSYS.version_x = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_version_y")) {
OSDSYS.version_y = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_cursor_max_velocity")) {
OSDSYS.cursor_max_velocity = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_cursor_acceleration")) {
OSDSYS.cursor_acceleration = atoi(value);
continue;
} ///CNF READER
if (!strcmp(name, "OSDSYS_left_cursor")) {
OSDSYS.left_cursor = value;
continue;
}
if (!strcmp(name, "OSDSYS_right_cursor")) {
OSDSYS.right_cursor = value;
continue;
}
if (!strcmp(name, "OSDSYS_menu_top_delimiter")) {
version[strlen(version)] = '0'; // kill null terminator (besause the string might continue after wildcard)
value = replace_var(value, "%VER%", version);
OSDSYS.menu_top_delimiter = value;
continue;
}
if (!strcmp(name, "OSDSYS_menu_bottom_delimiter")) {
/*if (!strcmp(value, "@CNFPATH"))
{
char* vall = "c0r0.60y+99Loaded CNF %CNF%y-00r0.00";;
CNF_LOADED[strlen(CNF_LOADED)] = ':';
///sprintf(vall,"c0r0.60y+99Loaded CNF %%CNF%%y-00r0.00");
OSDSYS.menu_bottom_delimiter = replace_var(vall, "%CNF%", CNF_LOADED);
continue;
}//*/
OSDSYS.menu_bottom_delimiter = value;
continue;
}
if (!strcmp(name, "OSDSYS_num_displayed_items")) {
OSDSYS.num_displayed_items = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_Skip_Disc")) {
OSDSYS.skip_disc = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_Skip_Logo")) {
OSDSYS.skip_logo = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_Inner_Browser")) {
OSDSYS.goto_inner_browser = atoi(value);
continue;
}
if (!strcmp(name, "FastBoot")) {
FMCB.fastboot = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_Skip_MC")) {
OSDSYS.skip_mc = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_Skip_HDD")) {
OSDSYS.skip_hdd = atoi(value);
continue;
}
if (!strcmp(name, "Debug_Screen")) {
FMCB.debug_screen = atoi(value);
continue;
}
if (!strcmp(name, "OSDSYS_selected_color")) {
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
hexvalue_buf[j] = value[j];
}
OSDSYS.selected_color[i] = strtol(hexvalue_buf, NULL, 16);
value += 5;
}
continue;
}
if (!strcmp(name, "OSDSYS_unselected_color")) {
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
hexvalue_buf[j] = value[j];
}
OSDSYS.unselected_color[i] = strtol(hexvalue_buf, NULL, 16);
value += 5;
}
continue;
}
for (i = 0; i < NEWITEMS; i++) {
sprintf(tsts, "name_OSDSYS_ITEM_%d", i + 1);
if (!strcmp(name, tsts)) {
OSDSYS.item_name[i] = value;
item_cnt++;
break;
}
}
for (i = 0; i < NEWITEMS; i++) {
for (j = 0; j < 3; j++) {
sprintf(tsts, "path%1d_OSDSYS_ITEM_%d", j + 1, i + 1);
if (!strcmp(name, tsts)) {
OSDSYS.item_path[i][j] = value;
break;
}
}
}
for (i = 0; i < 17; i++) {
for (j = 0; j < 3; j++) {
sprintf(tsts, "LK_%s_E%d", LK_ID[i], j + 1);
if (!strcmp(name, tsts)) {
FMCB.p_LK_Path[i][j] = value;
break;
}
}
}
} // ends for
// We don't want to release CNF_RAM_p now as we have pointers to it.
//free(CNF_RAM_p);
return 1;
}
//----------------------------------------------------------------
int Read_SYSTEM_CNF(char *boot_path, char *ver)
{
// Returns disc type : 0 = failed; 1 = PS1; 2 = PS2;
int var_cnt;
size_t CNF_size;
unsigned char *RAM_p, *CNF_p, *name, *value;
int fd = -1;
int Disc_Type = -1; // -1 = Internal : Not Tested;
//place 3 question mark in ver string
strcpy(ver, "???");
fd = open("cdrom0:\\SYSTEM.CNF;1", O_RDONLY);
if (fd < 0) {
failed_load:
if (Disc_Type == -1) {
// Test PS1 special cases
if (file_exists("cdrom0:\\PSXMYST\\MYST.CCS;1")) {
strcpy(boot_path, "SLPS_000.24");
Disc_Type = 1;
} else if (file_exists("cdrom0:\\CDROM\\LASTPHOT\\ALL_C.NBN;1")) {
strcpy(boot_path, "SLPS_000.65");
Disc_Type = 1;
} else if (file_exists("cdrom0:\\PSX.EXE;1")) {
//place 3 question mark in pathname
strcpy(boot_path, "???");
Disc_Type = 1;
}
}
if (Disc_Type == -1)
Disc_Type = 0;
return Disc_Type;
} // This point is only reached after succefully opening CNF
Disc_Type = 0;
CNF_size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
RAM_p = (char *)malloc(CNF_size);
CNF_p = RAM_p;
if (CNF_p == NULL) {
close(fd);
goto failed_load;
}
read(fd, CNF_p, CNF_size); // Read CNF as one long string
close(fd);
CNF_p[CNF_size] = '\0'; // Terminate the CNF string
strcpy(boot_path, "???"); //place 3 question mark in boot path
for (var_cnt = 0; get_CNF_string(&CNF_p, &name, &value); var_cnt++) {
// A variable was found, now we dispose of its value.
if (!strcmp(name, "BOOT2")) { // check for BOOT2 entry
strcpy(boot_path, value);
Disc_Type = 2; // If found, PS2 disc type
break;
}
if (!strcmp(name, "BOOT")) { // check for BOOT entry
strcpy(boot_path, value);
Disc_Type = 1; // If found, PS1 disc type
continue;
}
if (!strcmp(name, "VER")) { // check for VER entry
strcpy(ver, value);
continue;
}
} // ends for
free(RAM_p);
return Disc_Type;
}
//________________ Neme's OSDSYS loading & hacking code ______________________
#define OSD_MAGIC 0x39390000 // arbitrary number to identify added menu items
// Define the new items here.
// Then define their behaviour in the handle_menu_selection() function.
static char *menuitems[NEWITEMS];
//static int menuitem_index[NEWITEMS];
static char *menuitem_path[NEWITEMS];
static u32 osdmenu[4 + NEWITEMS * 2];
struct osd_menu_info
{
u32 unknown1;
u32 *menu_ptr;
u32 num_entries;
u32 unknown2;
u32 curr_selection;
};
static struct osd_menu_info *menuinfo = NULL;
static u32 execps2_code[] = {
0x24030007, // li v1, 7
0x0000000c, // syscall
0x03e00008, // jr ra
0x00000000 // nop
};
static u32 execps2_mask[] = {
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff};
static u32 pattern1[] = {
0x00000001, // unknown
0x00000000, // pointer to osdmenu
0x00000002, // number of entries
0x00000003, // unknown
0x00000000 // current selection
};
static u32 pattern1_mask[] = {
0xffffffff,
0xff800000,
0xffffffff,
0xffffffff,
0xffffffff};
static u32 pattern2[] = {
// Search pattern in the osd string function:
0x10000005, // beq zero, zero, L2
0x8c620000, // lw v0, $xxxx(v1)
0x00101880, // L1: sll v1, s0, 2 # string index * 4
0x8c440000, // lw a0, $xxxx(v0) # osd string pointer array
0x00641821, // addu v1, v1, a0 # byte offset into array
0x8c620000, // lw v0, $0000(v1) # pointer to string
0xdfbf0010 // L2: ld ra, $0010(sp)
};
static u32 pattern2_mask[] = {
0xffffffff,
0xffff0000,
0xffffffff,
0xffff0000,
0xffffffff,
0xffffffff,
0xffffffff};
static u32 pattern3[] = {
// Search pattern in the user input handling function:
0x1000000e, // beq zero, zero, exit
0xdfbf0010, // ld ra, $0010(sp)
0x24040001, // L1: li a0, 1 # the 2nd menu item (sys conf)
0x8c430000, // lw v1, $xxxx(v0) # current selection
0x1464000a, // bne v1, a0, exit # sys config not selected?
0xdfbf0010, // ld ra, $0010(sp)
0x0c000000, // jal StartSysConfig
0x00000000, // nop
0x10000006, // beq zero, zero, exit
0xdfbf0010 // ld ra, $0010(sp)
};
static u32 pattern3_mask[] = {
0xffffffff,
0xffffffff,
0xffffffff,
0xffff0000,
0xffffffff,
0xffffffff,
0xfc000000,
0xffffffff,
0xffffffff,
0xffffffff};
//--------------------------------------------------------------
u8 *find_bytes_with_mask(u8 *buf, u32 bufsize, u8 *bytes, u8 *mask, u32 len)
{
u32 i, j;
for (i = 0; i < bufsize - len; i++) {
for (j = 0; j < len; j++) {
if ((buf[i + j] & mask[j]) != bytes[j])
break;
}
if (j == len)
return &buf[i];
}
return NULL;
}
//--------------------------------------------------------------
u8 *find_string(const u8 *string, u8 *buf, u32 bufsize)
{
u32 i;
const u8 *s, *p;
for (i = 0; i < bufsize; i++) {
s = string;
for (p = buf + i; *s && *s == *p; s++, p++)
;
if (!*s)
return (buf + i);
}
return NULL;
}
//--------------------------------------------------------------
const char *get_string_pointer(const char **strings, u32 index)
{
if ((index & 0xffff0000) == OSD_MAGIC)
return menuitems[index & 0xffff];
return strings[index];
}
//--------------------------------------------------------------
int handle_menu_selection(int selected)
{
if (selected == 1)
return 1;
//if (selected >= 2 + NEWITEMS)
if (selected >= 2 + item_cnt)
return 0;
if ((selected - 2) >= 0) {
call_from_osdsys = 1;
//loading_print = 0;
load_elf(menuitem_path[selected - 2]);
}
return 0;
}
//--------------------------------------------------------------
void patch_menu(u8 *osd)
{
u8 *ptr;
u32 tmp, menu_addr, p2_addr, p3_addr, i;
//------------------------------------------------------------------------
// Search for all patterns and return if one of them not found
//------------------------------------------------------------------------
for (tmp = 0; tmp < 0x100000; tmp = (u32)(ptr - osd + 4)) {
ptr = find_bytes_with_mask(osd + tmp, 0x100000 - tmp, (u8 *)pattern1, (u8 *)pattern1_mask, sizeof(pattern1));
if (!ptr)
return;
if (_lw((u32)ptr + 4) == (u32)ptr - 4 * 4)
break;
}
menu_addr = (u32)ptr;
menuinfo = (struct osd_menu_info *)menu_addr;
ptr = find_bytes_with_mask(osd, 0x00100000, (u8 *)pattern2, (u8 *)pattern2_mask, sizeof(pattern2));
if (!ptr)
return;
p2_addr = (u32)ptr;
ptr = find_bytes_with_mask(osd, 0x00100000, (u8 *)pattern3, (u8 *)pattern3_mask, sizeof(pattern3));
if (!ptr)
return;
p3_addr = (u32)ptr;
//------------------------------------------------------------------------
// Patch the osd string function
//------------------------------------------------------------------------
tmp = 0x0c000000;
tmp |= ((u32)get_string_pointer >> 2);
_sw(0x0200282d, p2_addr + 2 * 4); // daddu a1, s0, zero
// lw a0, $xxxx(v0)
_sw(tmp, p2_addr + 4 * 4); // jal get_string_pointer
_sw(0x00000000, p2_addr + 5 * 4); // nop
//------------------------------------------------------------------------
// Patch the user input handling function
//------------------------------------------------------------------------
tmp = 0x0c000000;
tmp |= ((u32)handle_menu_selection >> 2);
_sw(tmp, p3_addr + 2 * 4); // jal handle_menu_selection
tmp = _lw(p3_addr + 3 * 4) & 0xffff;
tmp |= 0x8c440000;
_sw(tmp, p3_addr + 3 * 4); // lw a0, $xxxx(v0)
_sw(0x1040000a, p3_addr + 4 * 4); // beq v0, zero, exit
//------------------------------------------------------------------------
// Build the osd menu
//------------------------------------------------------------------------
osdmenu[0] = _lw(menu_addr - 4 * 4); // "Browser"
osdmenu[1] = _lw(menu_addr - 3 * 4);
osdmenu[2] = _lw(menu_addr - 2 * 4); // "System Configuration"
osdmenu[3] = _lw(menu_addr - 1 * 4);
//for (i = 0; i < NEWITEMS; i++) {
for (i = 0; i < item_cnt; i++) {
osdmenu[4 + i * 2] = OSD_MAGIC + i;
osdmenu[5 + i * 2] = 0;
}
menuinfo->menu_ptr = osdmenu; // store menu pointer
menuinfo->num_entries = 2 + item_cnt; // store number of menu items
//menuinfo->num_entries = 2 + NEWITEMS;
}
//=========================================================================
// The draw menu item hack :-)
//
// You can change menu items' color and position for selected and unselected
// items separately in the following two functions:
//
// draw_menu_item_selected()
// draw_menu_item_unselected()
//
// Be careful what you write in these functions as they get called every
// frame for every menu item! For positioning the menu, update both
// functions with the same calculations, using the X/Y variables.
//
// Default values of the variables in V12 OSDSYS:
// X = 430 (this is the center of the menu)