-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsystem.c
More file actions
executable file
·2998 lines (2501 loc) · 87 KB
/
system.c
File metadata and controls
executable file
·2998 lines (2501 loc) · 87 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 <iopheap.h>
#include <kernel.h>
#include <fileXio_rpc.h>
#include <libcdvd.h>
#include <libmc.h>
#include <libpad.h>
#include <libsecr-common.h>
#include <hdd-ioctl.h>
#include <loadfile.h>
#include <malloc.h>
#include <sifcmd.h>
#include <sifrpc.h>
#include <stdio.h>
#include <string.h>
#include <timer.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <libgs.h>
#include "main.h"
#include "pad.h"
#include "libsecr.h"
#include "mctools_rpc.h"
#include "system.h"
#include "graphics.h"
#include "UI.h"
#include "menu.h"
#include "ReqSpaceCalc.h"
#define IO_BLOCK_SIZE (256*512)
extern void *_gp;
extern int errno __attribute__((section("data")));
extern unsigned short int SelectButton, CancelButton;
int GetBootDeviceID(const char *path){
int result;
if(!strncmp(path, "mass:", 5) || !strncmp(path, "mass0:", 6)) result=BOOT_DEVICE_MASS;
else result=BOOT_DEVICE_UNKNOWN;
return result;
}
int GetConsoleRegion(void)
{
static int region = -1;
FILE *file;
if(region < 0)
{
if((file = fopen("rom0:ROMVER", "r")) != NULL)
{
fseek(file, 4, SEEK_SET);
switch(fgetc(file))
{
case 'J':
region = CONSOLE_REGION_JAPAN;
break;
case 'A':
case 'H':
region = CONSOLE_REGION_USA;
break;
case 'E':
region = CONSOLE_REGION_EUROPE;
break;
case 'C':
region = CONSOLE_REGION_CHINA;
break;
}
fclose(file);
}
}
return region;
}
int GetConsoleVMode(void)
{
switch(GetConsoleRegion())
{
case CONSOLE_REGION_EUROPE:
return 1;
default:
return 0;
}
}
/* Some things to take note of while making a multi-install:
1. The original release of the SCPH-10000 has boot ROM v1.00, which requires an update to its OSDSYS for argument passing (osdsys.elf).
2. The SCPH-10000 and SCPH-15000 look for osd110.elf. Both require an update to their OSDSYS programs for argument passing (osd110.elf).
3. The SCPH-18000 looks for osd130.elf.
4. All of the above are in BIEXEC-SYSTEM, which should have different content from BAEXEC-SYSTEM, BEEXEC-SYSTEM and BCEXEC-SYSTEM.
To satisfy the above 4 conditions:
1. Check the region. If it's Japan, the BIEXEC-SYSTEM folder already exists. Otherwise, create it.
2. If the console is not a Japanese console or isn't a first-generation SCPH-10000, don't install the system driver updates to BIEXEC-SYSTEM. Unless it's a cross-regional or cross-model install.
3. Cross link the OSD update files in the system folder, except for the system driver updates for the early Japanese PCMCIA units.
4. Cross link the remaining region folders with each other (e.g. BEEXEC-SYSTEM is a cross-linked with BAEXEC-SYSTEM if the console uses BAEXEC-SYSTEM). */
#define ROM100J_UPDATE_NUM_FILES 1
static struct InstallationFile BootROM100JUpdateFiles[ROM100J_UPDATE_NUM_FILES]={
{
"SYSTEM/OSDSYS.XLF",
"BIEXEC-SYSTEM/osdsys.elf",
FILE_IS_KELF
}
};
#define ROM101J_UPDATE_NUM_FILES 1
static struct InstallationFile BootROM101JUpdateFiles[ROM101J_UPDATE_NUM_FILES]={
{
"SYSTEM/OSD110.XLF",
"BIEXEC-SYSTEM/osd110.elf",
FILE_IS_KELF
}
};
#define PS2_SYS_INSTALL_NUM_FILES 2
static struct InstallationFile PS2SysFiles[PS2_SYS_INSTALL_NUM_FILES]={
{
"SYSTEM/FMCB.XLF",
"BREXEC-SYSTEM/osdmain.elf",
FILE_IS_KELF
},
{
"SYSTEM/ENDVDPL.XRX",
"SYS-CONF/endvdpl.irx",
FILE_IS_KELF
}
};
#define DEX_SYS_INSTALL_NUM_FILES 1
static struct InstallationFile DEXSysFiles[DEX_SYS_INSTALL_NUM_FILES]={
{
"SYSTEM/FMCB.XLF",
"BREXEC-SYSTEM/osdmain.elf",
FILE_IS_KELF
}
};
#define PS2_SYS_HDDLOAD_INSTALL_NUM_FILES 2
static struct InstallationFile PS2HDDLOADSysFiles[PS2_SYS_HDDLOAD_INSTALL_NUM_FILES]={
{
"SYSTEM/ATAD.IRX",
"BIEXEC-SYSTEM/atad.irx",
0
},
{
"SYSTEM/HDDLOAD.IRX",
"BIEXEC-SYSTEM/hddload.irx",
0
}
};
#define PSX_SYS_INSTALL_NUM_FILES 3
static struct InstallationFile PSXSysFiles[PSX_SYS_INSTALL_NUM_FILES]={
{
"SYSTEM/XFMCB.XLF",
"BREXEC-SYSTEM/xosdmain.elf",
FILE_IS_KELF
},
{
"SYSTEM/XUDNL.XRX",
"BREXEC-SYSTEM/xosdmain.irx",
FILE_IS_KELF
},
{
"SYSTEM/XENDVDPL.XRX",
"BREXEC-SYSTEM/xendvdpl.irx",
FILE_IS_KELF
}
};
#define SYS_FOLDER_RESOURCES_NUM_FILES 2
static struct InstallationFile SysResourceFiles[SYS_FOLDER_RESOURCES_NUM_FILES]={
{
"SYSTEM/FMCB.ICN",
"BREXEC-SYSTEM/FMCB.icn",
0
},
{
"SYSTEM/ICON.SYS",
"BREXEC-SYSTEM/icon.sys",
0
}
};
#define BASE_INSTALL_NUM_FILES 6
static struct InstallationFile BaseFiles[BASE_INSTALL_NUM_FILES]={
{
"SYS-CONF/FMCB_CFG.ELF",
"SYS-CONF/FMCB_CFG.ELF",
0
},
{
"SYS-CONF/FREEMCB.CNF",
"SYS-CONF/FREEMCB.CNF",
0
},
{
"SYS-CONF/ICON.SYS",
"SYS-CONF/icon.sys",
0
},
{
"SYS-CONF/SYSCONF.ICN",
"SYS-CONF/sysconf.icn",
0
},
{
"SYS-CONF/USBD.IRX",
"SYS-CONF/USBD.IRX",
0
},
{
"SYS-CONF/USBHDFSD.IRX",
"SYS-CONF/USBHDFSD.IRX",
0
}
};
#define HDD_BASE_INSTALL_NUM_FILES 22
static struct InstallationFile HDDBaseFiles[HDD_BASE_INSTALL_NUM_FILES]={
{
"SYS-CONF/FREEHDB.CNF",
"hdd0:__sysconf:pfs:/FMCB/FREEHDB.CNF",
0
},
{
"SYS-CONF/FMCB_CFG.ELF",
"hdd0:__sysconf:pfs:/FMCB/FMCB_CFG.ELF",
0
},
{
"SYS-CONF/USBD.IRX",
"hdd0:__sysconf:pfs:/FMCB/USBD.IRX",
0
},
{
"SYS-CONF/USBHDFSD.IRX",
"hdd0:__sysconf:pfs:/FMCB/USBHDFSD.IRX",
0
},
//FSCK
{
"SYSTEM/FSCK/FSCK.XLF",
"hdd0:__system:pfs:/fsck/fsck.elf",
FILE_IS_KELF
},
{
"SYSTEM/FSCK/LANG/NotoSans-Bold.ttf",
"hdd0:__system:pfs:/fsck/lang/NotoSans-Bold.ttf",
0
},
{
"SYSTEM/FSCK/LANG/NotoSansCJKjp-Bold.otf",
"hdd0:__system:pfs:/fsck/lang/NotoSansCJKjp-Bold.otf",
0
},
{
"SYSTEM/FSCK/LANG/fonts.txt",
"hdd0:__system:pfs:/fsck/lang/fonts.txt",
0
},
{
"SYSTEM/FSCK/LANG/strings_JA.txt",
"hdd0:__system:pfs:/fsck/lang/strings_JA.txt",
0
},
{
"SYSTEM/FSCK/LANG/labels_JA.txt",
"hdd0:__system:pfs:/fsck/lang/labels_JA.txt",
0
},
{
"SYSTEM/FSCK/LANG/strings_FR.txt",
"hdd0:__system:pfs:/fsck/lang/strings_FR.txt",
0
},
{
"SYSTEM/FSCK/LANG/labels_FR.txt",
"hdd0:__system:pfs:/fsck/lang/labels_FR.txt",
0
},
{
"SYSTEM/FSCK/LANG/strings_SP.txt",
"hdd0:__system:pfs:/fsck/lang/strings_SP.txt",
0
},
{
"SYSTEM/FSCK/LANG/labels_SP.txt",
"hdd0:__system:pfs:/fsck/lang/labels_SP.txt",
0
},
{
"SYSTEM/FSCK/LANG/strings_GE.txt",
"hdd0:__system:pfs:/fsck/lang/strings_GE.txt",
0
},
{
"SYSTEM/FSCK/LANG/labels_GE.txt",
"hdd0:__system:pfs:/fsck/lang/labels_GE.txt",
0
},
{
"SYSTEM/FSCK/LANG/strings_IT.txt",
"hdd0:__system:pfs:/fsck/lang/strings_IT.txt",
0
},
{
"SYSTEM/FSCK/LANG/labels_IT.txt",
"hdd0:__system:pfs:/fsck/lang/labels_IT.txt",
0
},
{
"SYSTEM/FSCK/LANG/strings_DU.txt",
"hdd0:__system:pfs:/fsck/lang/strings_DU.txt",
0
},
{
"SYSTEM/FSCK/LANG/labels_DU.txt",
"hdd0:__system:pfs:/fsck/lang/labels_DU.txt",
0
},
{
"SYSTEM/FSCK/LANG/strings_PO.txt",
"hdd0:__system:pfs:/fsck/lang/strings_PO.txt",
0
},
{
"SYSTEM/FSCK/LANG/labels_PO.txt",
"hdd0:__system:pfs:/fsck/lang/labels_PO.txt",
0
},
};
#define PS2_SYS_HDD_INSTALL_NUM_FILES 3
static struct InstallationFile PS2SysHDDFiles[PS2_SYS_HDD_INSTALL_NUM_FILES]={
{
"SYSTEM/MBR.XLF",
"hdd0:__mbr",
FILE_IS_KELF
},
{
"SYSTEM/FHDB.XLF",
"hdd0:__system:pfs:/osd/osdmain.elf",
FILE_IS_KELF
},
{
"SYSTEM/ENDVDPL.XRX",
"hdd0:__sysconf:pfs:/FMCB/endvdpl.irx",
FILE_IS_KELF
}
};
#define DEX_SYS_HDD_INSTALL_NUM_FILES 2
static struct InstallationFile DEXSysHDDFiles[DEX_SYS_HDD_INSTALL_NUM_FILES]={
{
"SYSTEM/MBR.XLF",
"hdd0:__mbr",
FILE_IS_KELF
},
{
"SYSTEM/FHDB.XLF",
"hdd0:__system:pfs:/osd/osdmain.elf",
FILE_IS_KELF
}
};
/* Remember to update NUM_CROSSLINKED_FILES in main.h appropriately!
If folders are to be added/deleted from the list of supported folders, remember to update these functions:
CreateBasicFolders()
CleanupTarget()
CleanupMultiInstallation()
PerformInstallation()
Within PerformInstallation, add/remove code which adds entries for the icon resources into the file copy list, for the new folder.
Remember to adjust the system folder count for the code (Which multiplies by SYS_FOLDER_RESOURCES_NUM_FILES) which determines how many times the resource files must be copied, as well!
Note: The names of the folders (which the files go into) must be uniform. They must fit the format of SysExecFolder.
*/
static struct FileAlias FileAlias[NUM_CROSSLINKED_FILES]={
{0x8415, "BIEXEC-SYSTEM/osd130.elf"},
{0x8415, "BIEXEC-SYSTEM/osdmain.elf"},
{0x8415, "BEEXEC-SYSTEM/osd130.elf"},
{0x8415, "BEEXEC-SYSTEM/osdmain.elf"},
{0x8415, "BAEXEC-SYSTEM/osd120.elf"},
{0x8415, "BAEXEC-SYSTEM/osd130.elf"},
{0x8415, "BAEXEC-SYSTEM/osdmain.elf"},
{0x8415, "BCEXEC-SYSTEM/osdmain.elf"}
};
static char MGFolderRegion, PS2SystemType;
static unsigned short int ROMVersion;
static char SysExecFolder[]="BREXEC-SYSTEM"; //Read above.
static char PSXSysExecFolder[]="BIEXEC-SYSTEM";
static char SysExecFile[12]; /* E.g. "osdmain.elf" or "osd110.elf" */
static char romver[16];
static int GetMcFreeSpace(int port, int slot);
static int CopyFiles(const char *RootFolder, unsigned char port, unsigned char slot, const struct FileCopyTarget *FileCopyList, unsigned int NumFilesEntries, unsigned int TotalNumBytes, unsigned int flags);
static int SignKELF(void *buffer, int size, unsigned char port, unsigned char slot){
int result;
result=1;
if(SecrDownloadFile(2+port, slot, buffer)==NULL){
DEBUG_PRINTF("Error signing file.\n");
result=-EINVAL;
}
return result;
}
static void GetKbitAndKc(void *buffer, u8 *Kbit, u8 *Kc){
int offset;
unsigned char OffsetByte;
SecrKELFHeader_t *header;
header = (SecrKELFHeader_t*)buffer;
offset = 0x20;
if(header->BIT_count > 0) offset+=header->BIT_count*0x10;
if((*(unsigned int*)&header->flags)&1){
OffsetByte = ((u8*)buffer)[offset];
offset+=OffsetByte+1;
}
if(((*(unsigned int*)&header->flags)&0xF000)==0) offset+=8;
memcpy(Kbit, &((u8*)buffer)[offset], 16);
memcpy(Kc, &((u8*)buffer)[offset + 16], 16);
}
static int SetKbitAndKc(void *buffer, u8 *Kbit, u8 *Kc){
int offset;
unsigned char OffsetByte;
SecrKELFHeader_t *header;
header = (SecrKELFHeader_t*)buffer;
offset = 0x20;
if(header->BIT_count > 0) offset+=header->BIT_count*0x10;
if((*(unsigned int*)&header->flags)&1){
OffsetByte = ((u8*)buffer)[offset];
offset+=OffsetByte+1;
}
if(((*(unsigned int*)&header->flags)&0xF000)==0) offset+=8;
memcpy(&((u8*)buffer)[offset], Kbit, 16);
memcpy(&((u8*)buffer)[offset + 16], Kc, 16);
}
static int TwinSignKELF(const char *RootFolder, void *buffer, int size, unsigned char port, unsigned char slot)
{
int result, i, iFileSize;
struct InstallationFile *PS2ExecFile;
FILE *iFile;
void *iFileBuffer;
char *iFilePath;
u8 Kbit[16], Kc[16];
//Try to locate a system PS2 KELF
for(i = 0, PS2ExecFile = NULL; i < PS2_SYS_INSTALL_NUM_FILES; i++)
{
if(PS2SysFiles[i].flags & FILE_IS_KELF)
{
PS2ExecFile = &PS2SysFiles[i];
break;
}
}
if(PS2ExecFile == NULL) //Can't happen, but what if?
return -1;
if((iFilePath = malloc(strlen(RootFolder) + strlen(PS2ExecFile->SrcRelPath) + 2)) != NULL)
{
sprintf(iFilePath, "%s/%s", RootFolder, PS2ExecFile->SrcRelPath);
if((iFile = fopen(iFilePath, "rb")) != NULL)
{
fseek(iFile, 0, SEEK_END);
iFileSize = ftell(iFile);
rewind(iFile);
if((iFileBuffer = memalign(64, iFileSize)) != NULL)
{
if(fread(iFileBuffer, 1, iFileSize, iFile) == iFileSize)
{
if((result = SignKELF(iFileBuffer, iFileSize, port, slot)) >= 0)
{
GetKbitAndKc(iFileBuffer, Kbit, Kc);
SetKbitAndKc(buffer, Kbit, Kc);
result = 0;
}
} else
result = -EIO;
free(iFileBuffer);
} else
result = -ENOMEM;
fclose(iFile);
} else
result = -errno;
free(iFilePath);
} else
result = -ENOMEM;
return result;
}
static char GetMGFolderLetter(unsigned char region){
unsigned char FolderLetter;
switch(region){
case 'C':
FolderLetter='C';
break;
case 'J':
FolderLetter='I';
break;
case 'H':
case 'A':
FolderLetter='A';
break;
case 'E':
FolderLetter='E';
break;
default:
DEBUG_PRINTF("Unrecognized region code: %c.\n", region);
FolderLetter='R';
}
return FolderLetter;
}
void UpdateRegionalPaths(void){
char ROMVersionNumStr[5], RegionCode;
int size;
FILE *file;
/* Acquire the MG region folder letter of this console. */
file = fopen("rom0:ROMVER", "r");
fseek(file, 0, SEEK_END);
size=ftell(file);
rewind(file);
fread(romver, 1, size>sizeof(romver)?sizeof(romver):size, file);
fclose(file);
strncpy(ROMVersionNumStr, romver, 4);
RegionCode=romver[4];
/* NULL terminate the version number. */
ROMVersionNumStr[4]='\0';
ROMVersion=strtoul(ROMVersionNumStr, NULL, 16);
if(romver[5] == 'D'){
PS2SystemType=PS2_SYSTEM_TYPE_DEX;
}else{
if((file = fopen("rom0:PSXVER", "r")) != NULL){
fclose(file);
PS2SystemType=PS2_SYSTEM_TYPE_PSX;
}
else{
PS2SystemType=PS2_SYSTEM_TYPE_PS2;
}
}
if(ROMVersion<0x130){
/* Consoles sporting a boot ROM older than v1.30 will look for OSDSYS updates named osdXXX.elf.
* The value of XXX seems to be the nearest boot ROM version number rounded UP to the nearest 10 (E.g. a console with ROM v1.01 will look for osd110.elf).
* Since the version numbers are handled in this function in HEX, it will be rounded up to the nearest 16 instead
* Since kernels v1.01 and v1.00 require specialized patching, the update file they should use would be osd130.elf.
*/
if(ROMVersion==0x100 || ROMVersion==0x101){
strcpy(SysExecFile, "osd130.elf");
}
else{
sprintf(SysExecFile, "osd%03x.elf", (ROMVersion+0x10)&~0x0F);
}
}
else{
strcpy(SysExecFile, "osdmain.elf");
}
DEBUG_PRINTF("ROMVER: %03x SysExec: %s\n", ROMVersion, SysExecFile);
MGFolderRegion=GetMGFolderLetter(RegionCode);
SysExecFolder[1]=MGFolderRegion;
}
int GetPs2Type(void){
return PS2SystemType;
}
static const char *GetMountParams(const char *command, char *BlockDevice){
const char *MountPath;
int BlockDeviceNameLen;
if((MountPath=strchr(&command[5], ':'))!=NULL){
BlockDeviceNameLen=(unsigned int)MountPath-(unsigned int)command;
strncpy(BlockDevice, command, BlockDeviceNameLen);
BlockDevice[BlockDeviceNameLen]='\0';
MountPath++; //This is the location of the mount path;
}
return MountPath;
}
static int CreateBasicFolders(int port, int slot, unsigned int flags){
unsigned int i;
int result;
char folders[][16]={
"APPS",
"BOOT",
"SYS-CONF",
"\0"
};
for(i=0,result=0; folders[i][0]!='\0' && result>=0; i++){
if((result=mcMkDir(port, slot, folders[i]))==0){
mcSync(0, NULL, &result);
if(result==-4) result=0; //EEXIST doesn't count as an error.
}
}
if(result>=0){
if(!(flags&INSTALL_MODE_FLAG_MULTI_INST) && !(flags&INSTALL_MODE_FLAG_CROSS_REG)){
if((result=mcMkDir(port, slot, (flags & INSTALL_MODE_FLAG_CROSS_PSX) ? PSXSysExecFolder : SysExecFolder))==0){
mcSync(0, NULL, &result);
if(result==-4) result=0; //EEXIST doesn't count as an error.
}
}
else{
if((result=mcMkDir(port, slot, "BIEXEC-SYSTEM"))==0){
mcSync(0, NULL, &result);
if(result==-4) result=0; //EEXIST doesn't count as an error.
}
if(result>=0){
if((result=mcMkDir(port, slot, "BEEXEC-SYSTEM"))==0){
mcSync(0, NULL, &result);
if(result==-4) result=0; //EEXIST doesn't count as an error.
}
}
if(result>=0){
if((result=mcMkDir(port, slot, "BAEXEC-SYSTEM"))==0){
mcSync(0, NULL, &result);
if(result==-4) result=0; //EEXIST doesn't count as an error.
}
}
if(result>=0){
if((result=mcMkDir(port, slot, "BCEXEC-SYSTEM"))==0){
mcSync(0, NULL, &result);
if(result==-4) result=0; //EEXIST doesn't count as an error.
}
}
}
}
return result;
}
struct DirentToDelete{
struct DirentToDelete *next;
char *filename;
};
static int DeleteFolder(const char *folder){
int fd, result;
char *path;
iox_dirent_t dirent;
struct DirentToDelete *head, *start;
result=0;
start=head=NULL;
if((fd=fileXioDopen(folder))>=0){
/* Generate a list of files in the directory. */
while(fileXioDread(fd, &dirent)>0){
if((strcmp(dirent.name, ".")==0)||((strcmp(dirent.name, "..")==0))) continue;
if(FIO_S_ISDIR(dirent.stat.mode)){
if((path=malloc(strlen(folder)+strlen(dirent.name)+2))!=NULL){
sprintf(path, "%s/%s", folder, dirent.name);
result=DeleteFolder(path);
free(path);
}
}
else{
if(start==NULL){
if((start=head=malloc(sizeof(struct DirentToDelete)))==NULL){
break;
}
}
else{
if((head->next=malloc(sizeof(struct DirentToDelete)))==NULL){
break;
}
head=head->next;
}
head->next=NULL;
if((head->filename=malloc(strlen(dirent.name)+1))!=NULL){
strcpy(head->filename, dirent.name);
}
else break;
}
}
fileXioDclose(fd);
}
else result=fd;
if(result>=0){
/* Delete the files. */
for(head=start; head!=NULL; head=start){
if(head->filename!=NULL){
if((path=malloc(strlen(folder)+strlen(head->filename)+2))!=NULL){
sprintf(path, "%s/%s", folder, head->filename);
DEBUG_PRINTF("Deleting %s...", path);
result=fileXioRemove(path);
DEBUG_PRINTF("%d\n", result);
free(path);
}
free(head->filename);
}
start=head->next;
free(head);
}
if(result>=0){
DEBUG_PRINTF("Deleting folder %s...", folder);
result=fileXioRmdir(folder);
DEBUG_PRINTF("%d\n", result);
}
}
return result;
}
static int DeleteFolderIfEmpty(const char *folder){
iox_dirent_t dirent;
unsigned int NumFilesInFolder;
int fd, result;
result=0;
NumFilesInFolder=0;
if((fd=fileXioDopen(folder))>=0){
while(fileXioDread(fd, &dirent)>0){
if((strcmp(dirent.name, ".")==0)||((strcmp(dirent.name, "..")==0))) continue;
NumFilesInFolder++;
}
fileXioDclose(fd);
}
else result=fd;
if(result>=0 && NumFilesInFolder==0){
result=DeleteFolder(folder);
}
return result;
}
int CleanupTarget(int port, int slot){
char PathToFolder[66];
sprintf(PathToFolder, "mc%u:BIEXEC-SYSTEM", port);
DeleteFolder(PathToFolder);
sprintf(PathToFolder, "mc%u:BAEXEC-SYSTEM", port);
DeleteFolder(PathToFolder);
sprintf(PathToFolder, "mc%u:BEEXEC-SYSTEM", port);
DeleteFolder(PathToFolder);
sprintf(PathToFolder, "mc%u:BCEXEC-SYSTEM", port);
DeleteFolder(PathToFolder);
return 0;
}
static int AddDirContentsToFileCopyList(const char *RootFolderPath, const char *srcRelativePath, const char *destination, unsigned int CurrentLevel, struct FileCopyTarget **FileCopyList, unsigned int *CurrentNumFiles, unsigned int *CurrentNumDirs, unsigned int *TotalRequiredSpaceForFiles){
char *path;
void *TempFileCopyListPtr;
int result, fd;
iox_dirent_t dirent;
struct FileCopyTarget *NewFileCopyTarget;
unsigned int CurrentNumFileEnts;
#ifdef DEBUG_TTY_FEEDBACK
unsigned int i;
u64 FileSize;
#endif
result=0;
if(srcRelativePath!=NULL){
path=malloc(strlen(RootFolderPath)+strlen(srcRelativePath)+2);
sprintf(path, "%s/%s", RootFolderPath, srcRelativePath);
}
else{
path=malloc(strlen(RootFolderPath)+2);
sprintf(path, "%s/", RootFolderPath);
}
DEBUG_PRINTF("Path: %s\n", path);
if((fd=fileXioDopen(path))>=0){
while(fileXioDread(fd, &dirent)>0){
#ifdef DEBUG_TTY_FEEDBACK
for(i=0; i<CurrentLevel; i++) printf("\t");
FileSize=(u64)dirent.stat.hisize<<32|dirent.stat.size;
printf("%c%c%c%c%c%c%c%c%c%c %lu %s\n", FIO_S_ISDIR(dirent.stat.mode)?'d':'-', dirent.stat.mode&FIO_S_IRUSR?'r':'-', dirent.stat.mode&FIO_S_IWUSR?'w':'-', dirent.stat.mode&FIO_S_IXUSR?'x':'-', dirent.stat.mode&FIO_S_IRGRP?'r':'-', dirent.stat.mode&FIO_S_IWGRP?'w':'-', dirent.stat.mode&FIO_S_IXGRP?'x':'-', dirent.stat.mode&FIO_S_IROTH?'r':'-', dirent.stat.mode&FIO_S_IWOTH?'w':'-', dirent.stat.mode&FIO_S_IXOTH?'x':'-', FileSize, dirent.name);
#endif
if(strcmp(dirent.name, ".")==0 || strcmp(dirent.name, "..")==0) continue;
if(FIO_S_ISDIR(dirent.stat.mode)){
(*CurrentNumDirs)++;
}
else{
(*TotalRequiredSpaceForFiles)+=dirent.stat.size;
(*CurrentNumFiles)++;
}
CurrentNumFileEnts=*CurrentNumFiles+*CurrentNumDirs;
if((TempFileCopyListPtr=realloc(*FileCopyList, sizeof(struct FileCopyTarget)*CurrentNumFileEnts))!=NULL){
*FileCopyList=TempFileCopyListPtr;
NewFileCopyTarget=&(*FileCopyList)[CurrentNumFileEnts-1];
memset(NewFileCopyTarget, 0, sizeof(struct FileCopyTarget));
if((NewFileCopyTarget->source=malloc((srcRelativePath!=NULL?strlen(srcRelativePath)+2:1)+strlen(dirent.name)))!=NULL){
if(srcRelativePath!=NULL){
sprintf(NewFileCopyTarget->source, "%s/%s", srcRelativePath, dirent.name);
}
else{
strcpy(NewFileCopyTarget->source, dirent.name);
}
if((NewFileCopyTarget->target=malloc(strlen(destination)+strlen(dirent.name)+2))!=NULL){
sprintf(NewFileCopyTarget->target, "%s/%s", destination, dirent.name);
NewFileCopyTarget->mode=dirent.stat.mode;
NewFileCopyTarget->flags=0;
if(FIO_S_ISDIR(dirent.stat.mode)){
NewFileCopyTarget->size=0;
result=AddDirContentsToFileCopyList(RootFolderPath, NewFileCopyTarget->source, NewFileCopyTarget->target, CurrentLevel+1, FileCopyList, CurrentNumFiles, CurrentNumDirs, TotalRequiredSpaceForFiles);
if(result < 0)
break;
}
else{
NewFileCopyTarget->size=dirent.stat.size;
}
}
else{
result=-ENOMEM;
break;
}
}
else{
result=-ENOMEM;
break;
}
}
else{
DEBUG_PRINTF("Can't alloc for file copy list. Num dirents: %u\n", CurrentNumFileEnts);
result=-ENOMEM;
break;
}
}
fileXioDclose(fd);
}
else
result = fd == -ENOENT ? 0 : fd;
free(path);
return result;
}
static int GetMcFreeSpace(int port, int slot){
int result;
int type, space, format;
mcGetInfo(port, slot, &type, &space, &format);
mcSync(0, NULL, &result);
if(result < -1 || type!=MC_TYPE_PS2){
space=0;
}
return space;
}
static int EnableHDDBooting(void){
int OpResult, result;
unsigned char OSDConfigBuffer[15];
do{
sceCdOpenConfig(0, 0, 1, &OpResult);
}while(OpResult&9);
do{
result=sceCdReadConfig(OSDConfigBuffer, &OpResult);
}while(OpResult&9 || result==0);
do{
result=sceCdCloseConfig(&OpResult);
}while(OpResult&9 || result==0);
if((OSDConfigBuffer[0]&3)!=2){ //If ATAD support and HDD booting are not already activated.
OSDConfigBuffer[0]=(OSDConfigBuffer[0]&~3)|2;
do{
sceCdOpenConfig(0, 1, 1, &OpResult);
}while(OpResult&9);
do{
result=sceCdWriteConfig(OSDConfigBuffer, &OpResult);
}while(OpResult&9 || result==0);
do{
result=sceCdCloseConfig(&OpResult);
}while(OpResult&9 || result==0);
result=0;
}
else result=1;
return result;
}
int IsHDDBootingEnabled(void)
{
int OpResult, result;
unsigned char OSDConfigBuffer[15];
do{
sceCdOpenConfig(0, 0, 1, &OpResult);
}while(OpResult&9);
do{
result=sceCdReadConfig(OSDConfigBuffer, &OpResult);
}while(OpResult&9 || result==0);
do{
result=sceCdCloseConfig(&OpResult);
}while(OpResult&9 || result==0);
return((OSDConfigBuffer[0]&3) == 2);
}
/* Don't set this to be too large, as FILEXIO's RPC receive buffer is only about 0x4C00 bytes large */
#define MBR_WRITE_BLOCK_SIZE 2
static inline int InstallMBRToHDD(FILE *file, void *IOBuffer, unsigned int size)
{
hddSetOsdMBR_t OSDData;
unsigned short int NumSectorsToWrite, MBR_NumSectors, i;
iox_stat_t stat;
int result;
unsigned int MBR_Sector, remaining, tLengthBytes;
if((result=fileXioGetStat("hdd0:__mbr", &stat))>=0)
{
MBR_Sector=stat.private_5+0x2000;
MBR_NumSectors=((size+0x1FF)&~0x1FF)/512;
for(i=0,remaining = size; i<MBR_NumSectors && result >= 0; i+=NumSectorsToWrite,remaining-=tLengthBytes)
{
NumSectorsToWrite=((MBR_NumSectors-i)>MBR_WRITE_BLOCK_SIZE)?MBR_WRITE_BLOCK_SIZE:MBR_NumSectors-i;
tLengthBytes = remaining > (MBR_WRITE_BLOCK_SIZE * 512) ? (MBR_WRITE_BLOCK_SIZE * 512) : remaining;
((hddAtaTransfer_t *)IOBuffer)->lba=MBR_Sector+i;
((hddAtaTransfer_t *)IOBuffer)->size=NumSectorsToWrite;
if(fread(((hddAtaTransfer_t *)IOBuffer)->data, 1, tLengthBytes, file) == tLengthBytes)
{
if(MBR_WRITE_BLOCK_SIZE*512 - tLengthBytes > 0)
memset(((hddAtaTransfer_t *)IOBuffer)->data + tLengthBytes, 0, MBR_WRITE_BLOCK_SIZE*512 - tLengthBytes);
result = fileXioDevctl("hdd0:", APA_DEVCTL_ATA_WRITE, IOBuffer, MBR_WRITE_BLOCK_SIZE*512+sizeof(hddAtaTransfer_t), NULL, 0);
}
else
result = -EIO;
}
if(result>=0)
{
OSDData.start=MBR_Sector;
OSDData.size=MBR_NumSectors;
fileXioDevctl("hdd0:", APA_DEVCTL_SET_OSDMBR, &OSDData, sizeof(OSDData), NULL, 0);
}
}
return result;