-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.c
3677 lines (3108 loc) · 96.9 KB
/
main.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/
---------------------------------------------------------------------------
Main Installer code
---------------------------------------------------------------------------
*/
#include "fmcb.h"
#include "sjpcm.h"
//#define PCSX2_DEBUG
// gui.c
extern void Setup_GS(int gs_vmode);
extern void gfx_set_defaults(void);
extern void load_Textures(void);
extern void Clear_Screen(void);
extern int Draw_INTRO(void);
extern int Draw_GUI(int logo, int selected_button, int highlight_pulse, int highlight_blw, int log, int dialog);
extern int Draw_OUTRO(void);
extern void Render_GUI(void);
extern void Play_Sound(void);
extern int SCREEN_WIDTH;
extern int SCREEN_HEIGHT;
extern int SCREEN_X;
extern int SCREEN_Y;
extern int FONT_WIDTH;
extern int FONT_HEIGHT;
extern int FONT_SPACING;
extern int FONT_Y;
#define DIALOG_YES_NO 1
#define DIALOG_OK 2
#define DIALOG_OK_CANCEL 3
#define DIALOG_ABORT 4
#define DIALOG_1_2 5
#define DIALOG_NONE 6
#define ICON_DIALOG_OK 1
#define ICON_DIALOG_WARNING 2
#define ICON_DIALOG_ERROR 3
#define ICON_DIALOG_NONE 4
#define MAX_DIALOG_LINES 14
#define MAX_LOG_LINES 22
extern int dialog_type;
extern int dialog_icon;
extern char* dialog_buffer[MAX_DIALOG_LINES];
extern int selected_dialog_button;
extern char* log_job_buffer[MAX_LOG_LINES];
extern int log_result[MAX_LOG_LINES];
// sound related
extern u16 *pSampleBuf;
extern int nSizeSample;
extern int snd_pos;
extern int snd_finished;
extern int SAMPLES_TICK;
// mcsp_rpc.c
typedef struct {
char name [16];
u16 size_name;
int page;
} fileentry;
typedef struct {
u16 attr;
u32 size;
u16 entry_cluster;
char name [16];
} returnentry;
extern int mcsp_rpc_Init(void);
extern int MC_OSD_Scan(u16 port, u16 slot, char *file, u16 size);
extern int MC_Dummies_Patch(u16 port, u16 slot, void *file_entry, int num_entries, int linking_cluster, int filesize, void *return_entry);
extern int MC_Dummies_UnPatch(u16 port, u16 slot, void* uninstall_entry, u16 num_entries, u32 linked_filesize, u16 linked_cluster);
// build_osd.c
extern int build_OSD(u8 *original_dvdelf, u8 *injected_dvdelf, u8 *hacked_dvdelf);
// embed.c
extern int embed(u8 *decrypted_dvdelf, u8 *boot_elf, int sizebootelf, u8 *launcher2_elf, int sizelauncher2elf, u8 *output_elf);
extern int verify_blocks(u8 *decrypted_dvdelf, u8 *injected_dvdelf);
// osdname.c
extern u8 *Get_OSD_Name(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);
// mcid.c
extern int mcsio2_rpc_Init(void);
extern int Card_Auth(int port);
extern void *Decrypt_Card_File(int port, void *buf);
extern void *Decrypt_Disk_File(void *buf);
extern void *Sign_Encrypted_Disk_File(int port, void *buf);
// main
int TV_mode;
u8 romver[16];
int selected_button;
int logo_displayed;
int highlight_pulse;
int highlight_blw;
int log_displayed;
int dialog;
int log_index;
typedef struct {
u32 unknown1;
u8 unknown2_1;
u8 unknown2_2;
u8 unknown2_3;
u8 mg_region;
u16 unknown3_half;
u16 version;
u32 unknown4;
} mg_ELF_region_t;
char *MG_region[8] = {"A", "E", "U", "J", "M", "O", "R", "C"};
#define NUM_INSTALL_FILES 20
#define NUM_OSD_FILES 3
#define FOLDER_EXEC 2
#define EXEC_ICON_SYS 0
#define EXEC_ICON_ICN 1
#define BOOT_ICON_SYS 3
#define BOOT_ICON_ICN 4
#define BOOT_FOLDER 5
#define SYSCONF_FOLDER 6
#define SYSCONF_FILE 7
#define SYSCONF_ICON_SYS 8
#define SYSCONF_ICON_ICN 9
#define FMCB_CONFIGURATOR 10
#define USB_IRX 11
#define USB_MASS_IRX 12
#define OLD_CNF_FOLDER 13
#define APPS_FOLDER 17
#define APPS_ICON_SYS 18
#define APPS_ICON_ICN 19
char* install_tab[NUM_INSTALL_FILES]= {
"/B?EXEC-SYSTEM/icon.sys",
"/B?EXEC-SYSTEM/FMCB.icn",
"/B?EXEC-SYSTEM", //2 exec folder
"/BOOT/icon.sys",
"/BOOT/BOOT.icn",
"/BOOT", //5 optional boot folder
"/SYS-CONF", //6
"/SYS-CONF/FREEMCB.CNF", //7
"/SYS-CONF/icon.sys", //8
"/SYS-CONF/sysconf.icn", //9
"/SYS-CONF/FMCB_CFG.ELF", //10
"/SYS-CONF/USBD.IRX", //11
"/SYS-CONF/USBHDFSD.IRX", //12
"/FMCB-CNF", //13
"/FMCB-CNF/FREEMCB.CNF",
"/FMCB-CNF/icon.sys",
"/FMCB-CNF/FMCB.icn",
"/APPS", //17 optional apps folder
"/APPS/icon.sys",
"/APPS/FMCBapps.icn"
};
#define NUM_DUMMIES_FILES 21
#define DUMMIES_UNINSTALL 21
#define DUMMIES_UNINSTALL2 22
char* dummies_tab[23]= {
"/B?EXEC-SYSTEM/osdsys.elf",
"/B?EXEC-SYSTEM/osd100.elf",
"/B?EXEC-SYSTEM/osd110.elf",
"/B?EXEC-SYSTEM/osd120.elf",
"/B?EXEC-SYSTEM/osd130.elf",
"/B?EXEC-SYSTEM/osd140.elf",
"/B?EXEC-SYSTEM/osd150.elf",
"/B?EXEC-SYSTEM/osd160.elf",
"/B?EXEC-SYSTEM/osd170.elf",
"/B?EXEC-SYSTEM/osd180.elf",
"/B?EXEC-SYSTEM/osd190.elf",
"/B?EXEC-SYSTEM/osd200.elf",
"/B?EXEC-SYSTEM/osd210.elf",
"/B?EXEC-SYSTEM/osd220.elf",
"/B?EXEC-SYSTEM/osd230.elf",
"/B?EXEC-SYSTEM/osd240.elf",
"/B?EXEC-SYSTEM/osd250.elf",
"/B?EXEC-SYSTEM/osd260.elf",
"/B?EXEC-SYSTEM/osd270.elf",
"/B?EXEC-SYSTEM/osd280.elf",
"/B?EXEC-SYSTEM/osd290.elf",
"/SYS-CONF/uninstall.dat",
"/FMCB-CNF/uninstall.dat"
};
#define XSIO2MAN_IRX 0
#define XMCMAN_IRX 1
#define XMCSERV_IRX 2
#define XPADMAN_IRX 3
#define USBD_IRX 4
#define USBHDFSD_IRX 5
#define FOLDER_CFG 6
#define FMCB_CNF 7
#define FMCB_CFG_ELF 8
#define DVDPL_ELF 9
#define EMBED_ELF 10
#define FOLDER_INSTALL 11
#define FOLDER_APPS 12
char* install_files_tab[13]= {
"INSTALL/MODULES/XSIO2MAN",
"INSTALL/MODULES/XMCMAN",
"INSTALL/MODULES/XMCSERV",
"INSTALL/MODULES/XPADMAN",
"INSTALL/MODULES/USBD.IRX",
"INSTALL/MODULES/USBHDFSD.IRX",
"INSTALL/FMCB_CFG/",
"INSTALL/FMCB_CFG/FREEMCB.CNF",
"INSTALL/FMCB_CFG/FMCB_CFG.ELF",
"INSTALL/INJECT/DVDELF.BIN",
"INSTALL/INJECT/EMBED.ELF",
"INSTALL/",
"INSTALL/APPS/"
};
char boot_elf_filepath[] = "INSTALL/BOOT.ELF";
enum
{
MAX_NAME = 256,
MAX_PATH = 1025,
MAX_ENTRY = 2048,
MAX_PARTITIONS=500
};
typedef struct{
char name[MAX_NAME];
char title[32*2+1];
mcTable stats;
} FILEINFO;
#define MC_ATTR_norm_folder 0x8427 //Normal folder on PS2 MC
#define MC_ATTR_prot_folder 0x842F //Protected folder on PS2 MC
#define MC_ATTR_PS1_folder 0x9027 //PS1 save folder on PS2 MC
#define MC_ATTR_norm_file 0x8497 //file (PS2/PS1) on PS2 MC
#define MC_ATTR_prot_file 0x8417 //file (PS2/PS1) on PS2 MC
#define MC_ATTR_PS1_file 0x9417 //PS1 save file on PS1 MC
int mc_Type, mc_Free, mc_Format;
int mctype_PSx;
int size_valid = 0;
int time_valid = 0;
char elf_list[MAX_ENTRY][MAX_PATH];
int elf_number = 0;
char file_list[MAX_ENTRY][MAX_PATH];
int file_number = 0;
typedef struct {
char path_installed[MAX_PATH];
int filesize;
} FILE_INFOS;
int cnf_overwrite = 1;
char dummy[] = "dummy";
int size_dummy = 5;
int install_with_dummies = 0;
char run_path[MAX_PATH];
int filesize;
u8 *hacked_dvdelf;
char dvdplpath[MAX_PATH];
char tmp1[MAX_PATH];
char tmp2[MAX_PATH];
char msg1[MAX_PATH];
char msg2[MAX_PATH];
int cdboot = 0;
char FMCB_Path[MAX_PATH];
//--------------------------------------------------------------
// functions prototypes
void load_elf(char *elf_path);
void IOP_Reset(void);
void CleanUp(void);
void delay(int count);
int load_X_modules(void);
int load_IOX_modules(void);
int load_USB_modules(void);
void load_Cd_modules(void);
int load_mcsio2_module(void);
int load_mcsp_module(void);
int File_Exist(char *filepath);
int Create_File(int port, char *filepath, u8 *buf, int size);
int Format_MC(int port);
void clear_mcTable(mcTable *mcT);
int readMASS(const char *path, FILEINFO *info, int max);
int readMC(const char *path, FILEINFO *info, int max);
int readCD(const char *path, FILEINFO *info, int max);
int getDir(const char *path, FILEINFO *info);
int MemoryCard_Check(void);
int MemoryCard_Format(int port);
int FMCB_Needed_Space(int port);
int unpatch_dummies(int port, int verbose);
int Install_Hack(int port);
int Run_Hack(int port);
int check_FMCB_exists(int port);
void launch_FMCB(void);
void NonModal_Dialog(int flag, int type, int icon);
int Modal_Dialog(int type, int icon);
int check_FMCB_configurator(char *execPath);
int multiversion_uninstall(int port);
void Update_GUI(void);
//--------------------------------------------------------------
// ELF-header structures and identifiers
#define ELF_MAGIC 0x464c457f
#define ELF_PT_LOAD 1
typedef struct
{
u8 ident[16];
u16 type;
u16 machine;
u32 version;
u32 entry;
u32 phoff;
u32 shoff;
u32 flags;
u16 ehsize;
u16 phentsize;
u16 phnum;
u16 shentsize;
u16 shnum;
u16 shstrndx;
} elf_header_t;
typedef struct
{
u32 type;
u32 offset;
void *vaddr;
u32 paddr;
u32 filesz;
u32 memsz;
u32 flags;
u32 align;
} elf_pheader_t;
//--------------------------------------------------------------
void load_elf(char *elf_path)
{
u8 *boot_elf;
elf_header_t *boot_header;
elf_pheader_t *boot_pheader;
int i;
char *args[1];
// The loader is embedded
boot_elf = (u8 *)&elf_loader;
// Get Elf header
boot_header = (elf_header_t *)boot_elf;
// Check elf magic
if ((*(u32*)boot_header->ident) != ELF_MAGIC)
return;
// Get program headers
boot_pheader = (elf_pheader_t *)(boot_elf + boot_header->phoff);
// Scan through the ELF's program headers and copy them into apropriate RAM
// section, then padd with zeros if needed.
for (i = 0; i < boot_header->phnum; i++)
{
if (boot_pheader[i].type != ELF_PT_LOAD)
continue;
memcpy(boot_pheader[i].vaddr, boot_elf + boot_pheader[i].offset, boot_pheader[i].filesz);
if (boot_pheader[i].memsz > boot_pheader[i].filesz)
memset(boot_pheader[i].vaddr + boot_pheader[i].filesz, 0, boot_pheader[i].memsz - boot_pheader[i].filesz);
}
CleanUp();
args[0] = elf_path;
// Execute Elf Loader
ExecPS2((void *)boot_header->entry, 0, 1, args);
}
//--------------------------------------------------------------
void IOP_Reset(void)
{
while(!SifIopReset("rom0:UDNL rom0:EELOADCNF",0));
while(!SifIopSync());
fioExit();
SifExitIopHeap();
SifLoadFileExit();
SifExitRpc();
SifExitCmd();
SifInitRpc(0);
FlushCache(0);
FlushCache(2);
}
//--------------------------------------------------------------
void CleanUp(void)
{
//cdInit(CDVD_INIT_EXIT);
//padPortClose(0,0);
//padPortClose(1,0);
//padEnd();
TimerEnd();
mcReset();
while(!SifIopReset("rom0:UDNL rom0:EELOADCNF",0));
while(!SifIopSync());
fioExit();
SifExitIopHeap();
SifLoadFileExit();
SifExitRpc();
SifExitCmd();
SifInitRpc(0);
FlushCache(0);
FlushCache(2);
SifLoadFileInit();
load_USB_modules();
load_IOX_modules();
SifLoadModule("rom0:SIO2MAN", 0, 0);
SifLoadModule("rom0:MCMAN", 0, 0);
SifLoadModule("rom0:MCSERV", 0, 0);
SifLoadModule("rom0:PADMAN", 0, 0);
fioExit();
SifExitIopHeap();
SifLoadFileExit();
SifExitRpc();
SifExitCmd();
FlushCache(0);
FlushCache(2);
Clear_Screen();
}
//------------------------------------------------------------------------------------------------------------------------
void delay(int count)
{
int i;
int ret;
for (i = 0; i < count; i++) {
ret = 0x01000000;
while(ret--) asm("nop\nnop\nnop\nnop");
}
}
//------------------------------------------------------------------------------------------------------------------------
int load_X_modules(void) // Loads XSIO2MAN, XMCMAN, XMCSERV, XPADMAN
{
int id;
char launch_path[MAX_PATH];
char tmp_path[MAX_PATH];
char *p;
int i;
strcpy(launch_path, run_path);
if ( ((p=strrchr(launch_path, '/'))==NULL)
&&((p=strrchr(launch_path, '\\'))==NULL)
) p=strrchr(launch_path, ':');
if (p!=NULL)
*(p+1)=0;
//The above cuts away the ELF filename from Launch Dir, leaving a pure path
sprintf(tmp_path, "%s%s", launch_path, install_files_tab[XSIO2MAN_IRX]); // check for user X module before
if(!strncmp(tmp_path, "cdrom0", 6)) {
//Transform the modules path to cdrom0 standard
for(i=0; tmp_path[i]!=0; i++){
if(tmp_path[i] == '/')
tmp_path[i] = '\\';
}
}
if ((id = SifLoadModule(tmp_path, 0, NULL)) < 0) {
if ((id = SifLoadModule("rom0:XSIO2MAN", 0, NULL)) < 0) {
scr_printf("\tERROR: cannot load Xsio2man: %d.\n", id);
return 0;
}
}
sprintf(tmp_path, "%s%s", launch_path, install_files_tab[XMCMAN_IRX]); // check for user X module before
if(!strncmp(tmp_path, "cdrom0", 6)) {
//Transform the modules path to cdrom0 standard
for(i=0; tmp_path[i]!=0; i++){
if(tmp_path[i] == '/')
tmp_path[i] = '\\';
}
}
if ((id = SifLoadModule(tmp_path, 0, NULL)) < 0) {
if ((id = SifLoadModule("rom0:XMCMAN", 0, NULL)) < 0) {
scr_printf("\tERROR: cannot load Xmcman: %d.\n", id);
return 0;
}
}
sprintf(tmp_path, "%s%s", launch_path, install_files_tab[XMCSERV_IRX]); // check for user X module before
if(!strncmp(tmp_path, "cdrom0", 6)) {
//Transform the modules path to cdrom0 standard
for(i=0; tmp_path[i]!=0; i++){
if(tmp_path[i] == '/')
tmp_path[i] = '\\';
}
}
if ((id = SifLoadModule(tmp_path, 0, NULL)) < 0) {
if ((id = SifLoadModule("rom0:XMCSERV", 0, NULL)) < 0) {
scr_printf("\tERROR: cannot load Xmcserv: %d.\n", id);
return 0;
}
}
sprintf(tmp_path, "%s%s", launch_path, install_files_tab[XPADMAN_IRX]); // check for user X module before
if(!strncmp(tmp_path, "cdrom0", 6)) {
//Transform the modules path to cdrom0 standard
for(i=0; tmp_path[i]!=0; i++){
if(tmp_path[i] == '/')
tmp_path[i] = '\\';
}
}
if ((id = SifLoadModule(tmp_path, 0, NULL)) < 0) {
if ((id = SifLoadModule("rom0:XPADMAN", 0, NULL)) < 0) {
scr_printf("\tERROR: cannot load Xpadman: %d.\n", id);
return 0;
}
}
return 1;
}
//------------------------------------------------------------------------------------------------------------------------
int load_IOX_modules(void) // Loads iomanX and fileXio
{
int ret, id;
if ((id = SifExecModuleBuffer(&iomanx_irx, size_iomanx_irx, 0, NULL, &ret)) < 0) {
scr_printf("\tERROR: cannot load iomanX.\n");
return 0;
}
if ((id = SifExecModuleBuffer(&filexio_irx, size_filexio_irx, 0, NULL, &ret)) < 0) {
scr_printf("\tERROR: cannot load fileXio.\n");
return 0;
}
return 1;
}
//------------------------------------------------------------------------------------------------------------------------
int load_USB_modules(void) // Loads usbd and usbhdfsd drivers
{
int ret, id;
if ((id = SifExecModuleBuffer(&usbd_irx, size_usbd_irx, 0, NULL, &ret)) < 0) {
scr_printf("\tERROR: cannot load usbd.\n");
return 0;
}
if ((id = SifExecModuleBuffer(&usb_mass_irx, size_usb_mass_irx, 0, NULL, &ret)) < 0) {
scr_printf("\tERROR: cannot load usbhdfsd.\n");
return 0;
}
delay(1);
return 1;
}
//--------------------------------------------------------------
void load_Cd_modules(void)
{
int ret;
int i;
//SifLoadModule("rom0:CDVDFSV", 0, NULL);
//SifLoadModule("rom0:CDVDMAN", 0, NULL);
SifExecModuleBuffer(&cdvd_irx, size_cdvd_irx, 0, NULL, &ret);
cdInit(CDVD_INIT_INIT);
CDVD_Init();
i = 0x10000;
while(i--) asm("nop\nnop\nnop\nnop");
}
//--------------------------------------------------------------
int load_mcsio2_module(void) // Loads mcsio2 (sio2 commands handler for MC)
{ // mcsio2 needs X modules to be loaded.
int ret, id;
if ((id = SifExecModuleBuffer(&mcsio2_irx, size_mcsio2_irx, 0, NULL, &ret)) < 0) {
scr_printf("\tERROR: cannot load mcsio2.\n");
return 0;
}
return 1;
}
//------------------------------------------------------------------------------------------------------------------------
int load_mcsp_module(void) // Loads mcsp module (Scanning MC for osd file, creating and patching dummies)
{
int ret, id;
if ((id = SifExecModuleBuffer(&mcsp_irx, size_mcsp_irx, 0, NULL, &ret)) < 0) {
scr_printf("\tERROR: cannot load mcsp.\n");
return 0;
}
return 1;
}
//------------------------------------------------------------------------------------------------------------------------
int File_Exist(char *filepath) // Check that a file is existing
{
int fd;
fd = open(filepath, O_RDONLY);
if(fd < 0)
return 0;
close(fd);
return 1;
}
//--------------------------------------------------------------
int Create_File(int port, char *filepath, u8 *buf, int size) // Creates a file on mc?:
{
int fd;
char filepath2[0x40];
sprintf(filepath2, "mc%1d:%s", port, filepath);
fd = fioOpen(filepath2, O_WRONLY | O_CREAT);
if(fd < 0)
return 0;
if (fioWrite(fd, buf, size) < 0) {
fioClose(fd);
return 0;
}
fioClose(fd);
return 1;
}
//--------------------------------------------------------------
int Format_MC(int port) // format mc: !!!
{
int ret;
ret = mcFormat(port, 0);
mcSync(0, NULL, &ret);
if (ret < 0)
return 0;
else
return 1;
}
//--------------------------------------------------------------
//----- From uLE -----------------------------------------------------------------------------------------------------
void clear_mcTable(mcTable *mcT) // Clear up mcTable struct
{
memset((void *) mcT, 0, sizeof(mcTable));
}
//----- From uLE -----------------------------------------------------------------------------------------------------
int readMASS(const char *path, FILEINFO *info, int max) // read a dir on mass: and fill info struct
{
fio_dirent_t record;
int n=0, dd=-1;
if ((dd = fioDopen(path)) < 0) goto exit; //exit if error opening directory
while(fioDread(dd, &record) > 0){
if((FIO_SO_ISDIR(record.stat.mode))
&& (!strcmp(record.name,".") || !strcmp(record.name,".."))
) continue; //Skip entry if pseudo-folder "." or ".."
strcpy(info[n].name, record.name);
clear_mcTable(&info[n].stats);
if(FIO_SO_ISDIR(record.stat.mode)){
info[n].stats.attrFile = MC_ATTR_norm_folder;
}
else if(FIO_SO_ISREG(record.stat.mode)){
info[n].stats.attrFile = MC_ATTR_norm_file;
info[n].stats.fileSizeByte = record.stat.size;
}
else
continue; //Skip entry which is neither a file nor a folder
strncpy(info[n].stats.name, info[n].name, 32);
memcpy((void *) &info[n].stats._create, record.stat.ctime, 8);
memcpy((void *) &info[n].stats._modify, record.stat.mtime, 8);
n++;
if(n==max) break;
} //ends while
size_valid = 1;
time_valid = 1;
exit:
if(dd >= 0) fioDclose(dd); //Close directory if opened above
return n;
}
//----- From uLE ------------------------------------------------------------------------------------------------------
int readMC(const char *path, FILEINFO *info, int max) // read a dir on mc: and fill info struct
{
static mcTable mcDir[MAX_ENTRY] __attribute__((aligned(64)));
char dir[MAX_PATH];
int i, j, ret;
mcSync(0,NULL,NULL);
mcGetInfo(path[2]-'0', 0, &mctype_PSx, NULL, NULL);
mcSync(0, NULL, &ret);
if (mctype_PSx == 2) //PS2 MC ?
time_valid = 1;
size_valid = 1;
strcpy(dir, &path[4]); strcat(dir, "*");
mcGetDir(path[2]-'0', 0, dir, 0, MAX_ENTRY-2, (mcTable *) mcDir);
mcSync(0, NULL, &ret);
for(i=j=0; i<ret; i++)
{
if(mcDir[i].attrFile & MC_ATTR_SUBDIR &&
(!strcmp(mcDir[i].name,".") || !strcmp(mcDir[i].name,"..")))
continue; //Skip pseudopaths "." and ".."
strcpy(info[j].name, mcDir[i].name);
info[j].stats = mcDir[i];
j++;
}
return j;
}
//----- From uLE ------------------------------------------------------------------------------------------------------
int readCD(const char *path, FILEINFO *info, int max)
{
static struct TocEntry TocEntryList[MAX_ENTRY];
char dir[MAX_PATH];
int i, j, n;
strcpy(dir, &path[5]);
CDVD_FlushCache();
n = CDVD_GetDir(dir, NULL, CDVD_GET_FILES_AND_DIRS, TocEntryList, MAX_ENTRY, dir);
for(i=j=0; i<n; i++)
{
if(TocEntryList[i].fileProperties & 0x02 &&
(!strcmp(TocEntryList[i].filename,".") ||
!strcmp(TocEntryList[i].filename,"..")))
continue; //Skip pseudopaths "." and ".."
strcpy(info[j].name, TocEntryList[i].filename);
clear_mcTable(&info[j].stats);
if(TocEntryList[i].fileProperties & 0x02){
info[j].stats.attrFile = MC_ATTR_norm_folder;
}
else{
info[j].stats.attrFile = MC_ATTR_norm_file;
info[j].stats.fileSizeByte = TocEntryList[i].fileSize;
}
j++;
}
size_valid = 1;
return j;
}
//----- From uLE ------------------------------------------------------------------------------------------------------
int getDir(const char *path, FILEINFO *info) // Handles read directory for mass or mc
{
int max=MAX_ENTRY-2;
int n;
if(!strncmp(path, "mc", 2)) n = readMC(path, info, max);
else if(!strncmp(path, "mass", 4)) n = readMASS(path, info, max);
else if(!strncmp(path, "cdfs", 4)) n = readCD(path, info, max);
else return 0;
return n;
}
//--------------------------------------------------------------
int MemoryCard_Check(void)
{
// check and wait presence of mc0: or mc1:
// return selected mc port or -1 if aborted or failed.
int ret0, ret1, ret, r;
u64 WaitTime;
int insert;
int check_mc;
int check_cnt;
int mc0type, mc0free, mc0format;
int mc1type, mc1free, mc1format;
int port = -1;
insert = 0;
dialog_buffer[0] = "Detecting Memory Card...";
NonModal_Dialog(0, DIALOG_NONE, ICON_DIALOG_NONE);
NonModal_Dialog(1, DIALOG_NONE, ICON_DIALOG_NONE);
// mc0: Fist GetInfo call -1 should be returned if valid memcard is found
mcGetInfo(0, 0, &mc0type, &mc0free, &mc0format);
mcSync(0, NULL, &ret);
// Assuming that the same memory card is connected, this should return 0
mcGetInfo(0, 0, &mc0type, &mc0free, &mc0format);
mcSync(0, NULL, &ret0);
// mc1: Fist GetInfo call -1 should be returned if valid memcard is found
mcGetInfo(1, 0, &mc1type, &mc1free, &mc1format);
mcSync(0, NULL, &ret);
// Assuming that the same memory card is connected, this should return 0
mcGetInfo(1, 0, &mc1type, &mc1free, &mc1format);
mcSync(0, NULL, &ret1);
// 2 Memory Cards connected
if ((ret0 == 0) && (ret1 == 0)) {
NonModal_Dialog(2, DIALOG_NONE, ICON_DIALOG_NONE);
dialog_buffer[0] = "2 Memory Cards detected, which one to use ?";
ret = Modal_Dialog(DIALOG_1_2, ICON_DIALOG_WARNING);
if (ret == 1) {
port = 0;
mc_Type = mc0type;
mc_Free = mc0free;
mc_Format = mc0format;
}
else {
port = 1;
mc_Type = mc1type;
mc_Free = mc1free;
mc_Format = mc1format;
}
NonModal_Dialog(2, DIALOG_NONE, ICON_DIALOG_NONE);
goto mc_inserted;
}
// only mc0 connected
if (ret0 == 0) {
port = 0;
mc_Type = mc0type;
mc_Free = mc0free;
mc_Format = mc0format;
NonModal_Dialog(2, DIALOG_NONE, ICON_DIALOG_NONE);
goto mc_inserted;
}
// only mc1 connected
if (ret1 == 0) {
port = 1;
mc_Type = mc1type;
mc_Free = mc1free;
mc_Format = mc1format;
NonModal_Dialog(2, DIALOG_NONE, ICON_DIALOG_NONE);
goto mc_inserted;
}
// No memory card connected
if ((ret0 != 0) && (ret1 != 0)) {
NonModal_Dialog(2, DIALOG_NONE, ICON_DIALOG_NONE);
dialog_buffer[0] = "Plug-in your Memory Card";
NonModal_Dialog(0, DIALOG_ABORT, ICON_DIALOG_NONE);
insert = 1;
check_mc = 0;
check_cnt = 0;
mcGetInfo(check_mc, 0, &mc_Type, &mc_Free, &mc_Format);
do
{
r = mcSync(1, NULL, &ret); // Check mc function state
if (r == 1) { // mc function finished
if (check_mc == 0) {
check_cnt++;
if (check_cnt == 2) { // Wait 2nd check before to switch to other mc
if (ret == 0) {
port = 0;
NonModal_Dialog(2, DIALOG_ABORT, ICON_DIALOG_NONE);
goto mc_inserted;
}
check_cnt = 0;
check_mc = 1;
}
}
else if (check_mc == 1) {
check_cnt++;
if (check_cnt == 2) { // Wait 2nd check before to switch to other mc
if (ret == 0) {
port = 1;
NonModal_Dialog(2, DIALOG_ABORT, ICON_DIALOG_NONE);
goto mc_inserted;
}
check_cnt = 0;
check_mc = 0;
}
}
// Send another mc function
mcGetInfo(check_mc, 0, &mc_Type, &mc_Free, &mc_Format);
}
if (ret != 0) {
NonModal_Dialog(1, DIALOG_ABORT, ICON_DIALOG_NONE);
}