-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aprip.c
executable file
·1921 lines (1696 loc) · 84.5 KB
/
aprip.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
/*
BSD 3-Clause License
Copyright (c) 2022-2024, Alex Free
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
FILE *bin;
FILE *bin_2;
FILE *mem_dump_1;
FILE *mem_dump_2;
FILE *patch;
unsigned int bin_size;
unsigned int bin_size_2;
unsigned int lba;
unsigned int max_size;
unsigned int mem_dump_1_size;
unsigned int mem_dump_2_size;
unsigned int current_fpos;
unsigned int number_of_sectors;
unsigned int number_of_sectors_2;
unsigned int search_size;
unsigned int valid_mem_dump_size = 0x200000; // The exact file size generated when dumping RAM in the DuckStation emulator.
unsigned int gameshark_write_byte_address;
unsigned int get_start_of_pattern_pos;
unsigned int magic_word;
unsigned char *buf;
unsigned char *buf_2;
unsigned char *bytes;
unsigned char *mem_dump_1_buf;
unsigned char *mem_dump_2_buf;
unsigned char last_byte;
unsigned char gameshark_prefix;
unsigned char sectors[0x1000];
unsigned char sectors_2[0x1000];
const unsigned char sync = 0x00;
bool find_first_different_byte = true;
bool libcrypt = false;
const unsigned char anti_piracy_v1[] = { // The very first anti-piracy code. Found in PopoRogue, Ape Escape, etc. Does only 3 SCEX check for dumb non-stealth modchips.
0x01, 0x00, 0x01, 0x03, // GetStat
0x07, 0x00, 0x01, 0x03, // Motor on
0x02, 0x03, 0x01, 0x03, // SetLoc
0x16, 0x00, 0x01, 0x03, // SeekAudio
0x0E, 0x01, 0x01, 0x03, // SetMode
0x19, 0x01, 0x04, 0x03, // Subfunq X (19'04) //21st
0x0B, 0x00, 0x01, 0x03, // Mute
0x03, 0x00, 0x01, 0x03, // Play
0x19, 0x01, 0x02, 0x03, // Subfunq Y (19'05) //33rd
0x09, 0x00, 0x01, 0x03 // Pause
}; // 40 byte pattern
const unsigned char medievil_europe_ps_exe [] = { 0x53, 0x43, 0x45, 0x53, 0x5F, 0x30, 0x30, 0x33, 0x2E, 0x31, 0x31, 0x3B, 0x31 }; // SCES_003.11;1
const unsigned char medievil_france_ps_exe [] = { 0x53, 0x43, 0x45, 0x53, 0x5F, 0x30, 0x31, 0x34, 0x2E, 0x39, 0x32, 0x3B, 0x31 }; // SCES_014.92;1
const unsigned char medievil_germany_ps_exe [] = { 0x53, 0x43, 0x45, 0x53, 0x5F, 0x30, 0x31, 0x34, 0x2E, 0x39, 0x33, 0x3B, 0x31 }; // SCES_014.93;1
const unsigned char medievil_italian_ps_exe [] = { 0x53, 0x43, 0x45, 0x53, 0x5F, 0x30, 0x31, 0x34, 0x2E, 0x39, 0x34, 0x3B, 0x31 }; // SCES_014.94;1
const unsigned char medievil_spain_ps_exe [] = { 0x53, 0x43, 0x45, 0x53, 0x5F, 0x30, 0x31, 0x34, 0x2E, 0x39, 0x35, 0x3B, 0x31 }; // SCES_014.95;1
const unsigned char libcrypt_2_anti_pro_action_replay[] = {
0x80, 0xE1,
0x02, 0x3C,
0x00, 0x38,
0x82, 0x40
};
const unsigned char libcrypt_2_anti_mod_chip[] = {
0x08, 0x00,
0x20, 0x14,
0x02, 0x00, // not modified
0xE7, 0x30, // not modified
0x06, 0x00,
0xE0, 0x10,
0xAD, 0xFF, // not modified
0x84, 0x20, // not modified
0x04, 0x00,
0x80, 0x14,
0x00, 0x00, // not modified
0x00, 0x00 // not modified
};
const unsigned char libcrypt_2_magic_word[] = {
0x25, 0x30,
0x86, 0x00
};
// ICEPICK patch from TRSIMEDI, modified for aprip
const unsigned char libcrypt_1_medievil_icepick_based_patch[] = {
0x0A, // 0
0x00, // not a value checked // 1
0x80, // 2
0x14, // 3
0x00, // not a value checked // 4
0x00, // not a value checked // 5
0x00, // not a value checked // 6
0x00, // not a value checked // 7
0x00, // not a value checked // 8
0x00, // not a value checked // 9
0xA3, // 10
0x90, // 11
0x02, // 12
0x00, // not a value checked // 13
0x02, // 14
0x24, // 15
0x06, // 16
0x00, // not a value checked // 17
0x62, // 18
0x14, // 19
0x0E, // 20
0x80, // 21
0x03, // 22
0x3C, // 23
0x04, // 24
0x00, // not a value checked // 25
0xA3, // 26
0x90, // 27
0x53, // 28
0x00, // not a value checked // 29
0x02, // 30
0x24, // 31
0x02, // 32
0x00, // not a value checked // 33
0x62, // 34
0x14, // 35
0x0E, // 36
0x80, // 37
0x03, // 38
0x3C, // 39
0x07, // 40
0x00, // not a value checked // 41
0xA4, // 42
0x90, // 43
0x00, // byte 2 of MW // 44
0x00, // byte 1 of MW // 45
0x00, // 46
0x00 // 47
}; // 0x2F, 47 bytes
const unsigned char anti_piracy_v2_vc0_bypass[] = { // This was first seen in Dino Crisis? Does SCEX/GetTN/GetTD/ReadTOC but is standardized and very easy to bypass. Seems like all games after a certain point began copying in this code to add anti-piracy measures to their product.
0x01, 0x00, 0x01, 0x03, // GetStat
0x13, 0x00, 0x03, 0x03, // GetTN
0x14, 0x01, 0x03, 0x03, // GetTD
0x02, 0x03, 0x01, 0x03, // SetLoc
0x16, 0x00, 0x01, 0x05, // SeekAudio
0x0E, 0x01, 0x01, 0x03, // SetMode
0x0A, 0x00, 0x01, 0x05, // Init
0x0B, 0x00, 0x01, 0x03, // Mute
0x03, 0x00, 0x01, 0x03, // Play
0x19, 0x01, 0x01, 0x03, // Subfunq X (19'04) //37th
0x19, 0x01, 0x02, 0x03, // Subfunq Y (19'05) //41st
0x09, 0x00, 0x01, 0x05, // Pause
0x1E, 0x00, 0x01, 0x05, // ReadTOC //49th
0x1A, 0x00, 0x01, 0x05 // GetID
}; //52 byte pattern
const unsigned char anti_piracy_v2_pal_bypass[] = { // 16 byte pattern
0xC8, 0xBF, 0x03, 0x3C,
0x52, 0xFF, 0x63, 0x90,
0x45, 0x00, 0x02, 0x24,
0x0A, 0x00, 0x62, 0x10 // change to 0x0A, 0x00, 0x00, 0x18 to make the game think your using a PAL BIOS/console
};
const unsigned char beatmania_append_gotta_mix_system_cnf[] = { // 66 byte pattern
0x42, 0x4F, 0x4F, 0x54,
0x20, 0x3D, 0x20, 0x63,
0x64, 0x72, 0x6F, 0x6D,
0x3A, 0x5C, 0x53, 0x4C,
0x50, 0x4D, 0x5F, 0x38,
0x36, 0x32, 0x2E, 0x32,
0x39, 0x3B, 0x31, 0x0D,
0x0A, 0x54, 0x43, 0x42,
0x20, 0x3D, 0x20, 0x34,
0x0D, 0x0A, 0x45, 0x56,
0x45, 0x4E, 0x54, 0x20,
0x3D, 0x20, 0x31, 0x30,
0x0D, 0x0A, 0x53, 0x54,
0x41, 0x43, 0x4B, 0x20,
0x3D, 0x20, 0x38, 0x30,
0x31, 0x46, 0x46, 0x46,
0x30, 0x30
};
/*
BOOT = cdrom:\SLPM_866.92;1
TCB = 4
EVENT = 10
STACK = 801FFF00
*/
const unsigned char beatmania_append_3rd_mix_system_cnf[] = { // 66 byte pattern
0x42, 0x4F, 0x4F, 0x54,
0x20, 0x3D, 0x20, 0x63,
0x64, 0x72, 0x6F, 0x6D,
0x3A, 0x5C, 0x53, 0x4C,
0x50, 0x4D, 0x5F, 0x38,
0x36, 0x31, 0x2E, 0x38,
0x34, 0x3B, 0x31, 0x0D,
0x0A, 0x54, 0x43, 0x42,
0x20, 0x3D, 0x20, 0x34,
0x0D, 0x0A, 0x45, 0x56,
0x45, 0x4E, 0x54, 0x20,
0x3D, 0x20, 0x31, 0x30,
0x0D, 0x0A, 0x53, 0x54,
0x41, 0x43, 0x4B, 0x20,
0x3D, 0x20, 0x38, 0x30,
0x31, 0x46, 0x46, 0x46,
0x30, 0x30
};
/*
BOOT = cdrom:\SLPM_861.84;1
TCB = 4
EVENT = 10
STACK = 801FFF00
*/
const unsigned char append_exe_name[] = { 0x5C, 0x41, 0x50, 0x50, 0x45, 0x4E, 0x44, 0x2E, 0x45, 0x58, 0x45, }; // \APPEND.EXE
bool matched_anti_piracy_v1;
bool matched_anti_piracy_v2_pal_bypass;
bool matched_anti_piracy_v2_vc0_bypass;
bool last_sector;
bool matched_libcrypt_2_anti_pro_action_replay;
bool matched_libcrypt_2_anti_mod_chip;
bool matched_libcrypt_2_magic_word;
bool matched_libcrypt_2;
bool directory_record_sectors_maybe = true;
bool libcrypt_1;
bool libcrypt_2;
bool matched_libcrypt_1_icepick_based_patch;
bool matched_libcrypt_1_magic_word;
bool matched_libcrypt_1_part_2;
bool matched_libcrypt_1_part_3;
void bin_patch_custom(const char **argv)
{
/*
The patch could possibly start on the end of a sector and end at the beginning of the next sector. Each RAW sector is 0x930 bytes. The first 0x18 bytes are to be ignored as they are just header data. The next 0x800 bytes contains actual data we want to scan through.
Start at 0. Skip to 0x18. Read the next 0x800 bytes. Skip to a total of 0x930 bytes (one whole raw sector). Skip 0x18 bytes again and then read the next 0x800 bytes. We now have 2 sectors worth of straight up data in a buffer of 0x1000 bytes
Run search functions on the 0x1000 byte sized buffer.
*/
fseek(bin, 0, SEEK_END);
bin_size = ftell(bin);
if(bin_size > 0x2EE00000) // 750MB max, no PSX software comes even close to such a size
{
printf("Error: The CD image BIN file %s exceeds the maximum filesize of 750MB\n", argv[2]);
fclose(bin);
return;
}
fseek(bin, 0, SEEK_SET);
buf = (unsigned char *)malloc(bin_size * sizeof(unsigned char)); // Read entire BIN to memory for performance gain, I mean it's 2022 who doesn't have a free ~700MBs of RAM?!
if(fread(buf, 1, bin_size, bin) != bin_size)
{
printf("Error loading CD image BIN file: %s\n", argv[2]);
return;
}
printf("Successfully loaded CD image BIN file: %s (%d bytes in memory)\n", argv[2], bin_size);
max_size = bin_size;
number_of_sectors = (bin_size / 2352);
int line_count = 0;
char ch;
fseek(patch, 0, SEEK_SET);
// Count the number of newline characters in the file
while ((ch = fgetc(patch)) != EOF) {
if (ch == '\n') {
line_count++;
}
}
// line count is equal to one byte change, so if it exceeds 0x800 * 2 = 0x10000 then it will segfault
if(line_count > 0x1000)
{
printf("\nGenerated patch is too large, it spans over 2 sectors (MAX is 0x1000 byte pattern/4096 lines). Please try splitting up the patch into seperate 2-sector length max patches.\n");
fclose(bin);
fclose(patch);
return;
} else {
printf("\nPatch size verified. Line count: %d\n", line_count);
}
fseek(patch, 0, SEEK_SET);
unsigned char custom_patch_before[line_count];
unsigned char custom_patch_after[line_count];
bool custom_patch_match;
int i = 0;
while(fscanf(patch, "%hhX %hhX%*[^\n]", &custom_patch_before[i], &custom_patch_after[i]) == 2)
{
printf("%hhX %hhX\n", custom_patch_before[i], custom_patch_after[i]);
i++;
}
printf("Scanning %d sectors, please wait...\n", number_of_sectors);
while(1)
{
if(current_fpos > max_size)
break; // even number of sectors, done reading the file.
if((current_fpos + 0x930) == max_size) // odd number of sectors
last_sector = 1; // This function is reading 2 sectors at a time, so if there is an odd number of sectors we have to change the behavior to only search the last sector. Explicitly break loop when this is set.
for(int i=0; i < 0x800; i++)
{
sectors[i] = buf[current_fpos + i + 0x18]; // skip 0x18 header info per sector
}
if(!last_sector)
{
for(int i=0; i < 0x800; i++)
{
sectors[i + 0x800] = buf[current_fpos + i + 0x18 + 0x930]; // skip 0x18 header info then skip exactly 1 sector. Read the next 0x800 bytes. We now have an array's worth of data from 2 sectors which excludes EDC/Header data at the beginning and end of each.
}
search_size = 0x1000;
} else {
search_size = 0x800;
}
for(int s = 0; s < search_size; s++)
{
custom_patch_match = true;
for(int i=0; i < line_count; i++)
{
if((custom_patch_before[i] != sectors[s + i]) && (custom_patch_before[i] + custom_patch_after[i] != 0))
{
custom_patch_match = false;
}
}
if(custom_patch_match)
{
if(s < 0x800)
{
lba = ((current_fpos / 0x930) + 150);
} else {
lba = ((current_fpos / 0x930) + 151);
}
printf("Got custom match at LBA: %u\n", lba);
for(int i = 0; i < line_count; i++)
{
if(custom_patch_before[i] + custom_patch_after[i] != 0)
{
printf("Writing %hhX\n", custom_patch_after[i]);
sectors[s + i] = custom_patch_after[i];
}
}
}
}
for(int i=0; i < 0x800; i++)
{
buf[current_fpos + i + 0x18] = sectors[i]; // skip 0x18 header info per sector
}
if(!last_sector)
{
for(int i=0; i < 0x800; i++)
{
buf[current_fpos + i + 0x18 + 0x930] = sectors[0x800 + i]; // skip 0x18 header info then skip exactly 1 sector. Read the next 0x800 bytes. We now have an array's worth of data from 2 sectors which excludes EDC/Header data at the beginning and end of each.
}
} else {
break; // That was the last sector
}
current_fpos = (current_fpos + 0x930); // Advance one sector.
}
fseek(bin, 0, SEEK_SET);
fwrite(buf, bin_size, 1, bin);
fclose(bin);
free(buf);
}
void bin_patch_libcrypt(const char **argv)
{
/*
The AP table could possibly start on the end of a sector and end at the beginning of the next sector. Each RAW sector is 0x930 bytes. The first 0x18 bytes are to be ignored as they are just header data. The next 0x800 bytes contains actual data we want to scan through.
Start at 0. Skip to 0x18. Read the next 0x800 bytes. Skip to a total of 0x930 bytes (one whole raw sector). Skip 0x18 bytes again and then read the next 0x800 bytes. We now have 2 sectors worth of straight up data in a buffer of 0x1000 bytes
Run search functions on the 0x1000 byte sized buffer.
*/
fseek(bin, 0, SEEK_END);
bin_size = ftell(bin);
if(bin_size > 0x2EE00000) // 750MB max, no PSX software comes even close to such a size
{
printf("Error: The BIN file: %s exceeds the maximum filesize of 750MB in bin patch mode\n", argv[3]);
fclose(bin);
return;
}
unsigned int magic_word = strtoul(argv[2], NULL, 16);
printf("Magic Word: %08X\n", magic_word);
unsigned char *bytes;
bytes=(unsigned char *)&magic_word;
/*
printf("Bytes 0: %02X\n", bytes[0]);
printf("Bytes 1: %02X\n", bytes[1]);
printf("Bytes 2: %02X\n", bytes[2]);
printf("Bytes 3: %02X\n", bytes[3]);
*/
fseek(bin, 0, SEEK_SET);
buf = (unsigned char *)malloc(bin_size * sizeof(unsigned char)); // Read entire BIN to memory for performance gain, I mean it's 2022 who doesn't have a free ~700MBs of RAM?!
if(fread(buf, 1, bin_size, bin) != bin_size)
{
printf("Error loading BIN: file: %s\n", argv[2]);
return;
}
printf("Successfully loaded BIN file: %s (%d bytes in memory)\n", argv[3], bin_size);
max_size = bin_size;
number_of_sectors = (bin_size / 2352);
printf("Scanning %d sectors, please wait...\n", number_of_sectors);
libcrypt_2 = true;
while(1)
{
if(current_fpos > max_size)
break; // even number of sectors, done reading the file.
if((current_fpos + 0x930) == max_size) // odd number of sectors
last_sector = 1; // This function is reading 2 sectors at a time, so if there is an odd number of sectors we have to change the behavior to only search the last sector. Explicitly break loop when this is set.
if((current_fpos > (0x930 * 25))) // after 4 sectors (22, 23, 24, 25) stop looking for libcrypt 1 PS-EXE boot filename
directory_record_sectors_maybe = false;
for(int i=0; i < 0x800; i++)
{
sectors[i] = buf[current_fpos + i + 0x18]; // skip 0x18 header info per sector
}
if(!last_sector)
{
for(int i=0; i < 0x800; i++)
{
sectors[i + 0x800] = buf[current_fpos + i + 0x18 + 0x930]; // skip 0x18 header info then skip exactly 1 sector. Read the next 0x800 bytes. We now have an array's worth of data from 2 sectors which excludes EDC/Header data at the beggining and end of each.
}
search_size = 0x1000;
} else {
search_size = 0x800;
}
for(int s = 0; s < search_size; s++)
{
if((directory_record_sectors_maybe) && (!libcrypt_1))
{
libcrypt_1 = true;
for(int i=0; i < 13; i++)
{
if(
(medievil_europe_ps_exe[i] != sectors[s + i]) &&
(medievil_france_ps_exe[i] != sectors[s + i]) &&
(medievil_germany_ps_exe[i] != sectors[s + i]) &&
(medievil_italian_ps_exe[i] != sectors[s + i]) &&
(medievil_spain_ps_exe[i] != sectors[s + i])
)
{
libcrypt_1 = false;
}
}
}
if(libcrypt_1)
{
matched_libcrypt_1_icepick_based_patch = true;
for(int i=0; i < 47; i++)
{
if(libcrypt_1_medievil_icepick_based_patch[i] != sectors[s + i])
{
if(i != 1 && i != 4 && i != 5 && i != 6 && i != 7 && i != 8 && i != 9 && i != 13 && i != 17 && i != 25 && i != 29 && i != 33 && i != 41) // These are not matchable so they are 0x00 in the array and not checked here
{
matched_libcrypt_1_icepick_based_patch = false;
}
}
}
if(matched_libcrypt_1_icepick_based_patch)
{
if(s < 0x800)
{
lba = ((current_fpos / 0x930) + 150);
} else {
lba = ((current_fpos / 0x930) + 151);
}
printf("Got LibCrypt v1 bypass match (By ICEPICK) at LBA: %u\n", lba);
sectors[s + 0] = 0x00;
// skip 1
sectors[s + 2] = 0x00;
sectors[s + 3] = 0x00;
// skip 4
// skip 5
// skip 6
// skip 7
// skip 8
// skip 9
sectors[s + 10] = 0x00;
sectors[s + 11] = 0x00;
sectors[s + 12] = 0x00;
// skip 13
sectors[s + 14] = 0x00;
sectors[s + 15] = 0x00;
sectors[s + 16] = 0x00;
// skip 17
sectors[s + 18] = 0x00;
sectors[s + 19] = 0x00;
sectors[s + 20] = 0x00;
sectors[s + 21] = 0x00;
sectors[s + 22] = 0x00;
sectors[s + 23] = 0x00;
sectors[s + 24] = 0x00;
// skip 25
sectors[s + 26] = 0x00;
sectors[s + 27] = 0x00;
sectors[s + 28] = 0x00;
// skip 29
sectors[s + 30] = 0x00;
sectors[s + 31] = 0x00;
sectors[s + 32] = 0x00;
// skip 33
sectors[s + 34] = 0x00;
sectors[s + 35] = 0x00;
sectors[s + 36] = 0x00;
sectors[s + 37] = 0x00;
sectors[s + 38] = 0x00;
sectors[s + 39] = 0x00;
sectors[s + 40] = 0x00;
// skip 41
sectors[s + 42] = 0x00;
sectors[s + 43] = 0x00;
sectors[s + 44] = bytes[0]; // Magic Word byte 2
sectors[s + 45] = bytes[1]; // Magic Word byte 1
sectors[s + 46] = 0x04;
sectors[s + 47] = 0x24;
}
} else if(libcrypt_2) {
// LibCrypt v2 (majority of LibCrypt games)
matched_libcrypt_2_anti_pro_action_replay = true;
for(int i=0; i < 8; i++)
{
if(libcrypt_2_anti_pro_action_replay[i] != sectors[s + i])
matched_libcrypt_2_anti_pro_action_replay = false;
}
if(matched_libcrypt_2_anti_pro_action_replay)
{
if(s < 0x800)
{
lba = ((current_fpos / 0x930) + 150);
} else {
lba = ((current_fpos / 0x930) + 151);
}
printf("Got LibCrypt v2 Anti-Pro Action Replay bypass match (By B.A.D) at LBA: %u\n", lba);
sectors[s + 0] = 0x00;
sectors[s + 1] = 0x00;
sectors[s + 2] = 0x00;
sectors[s + 3] = 0x00;
sectors[s + 4] = 0x00;
sectors[s + 5] = 0x00;
sectors[s + 6] = 0x00;
sectors[s + 7] = 0x00;
}
matched_libcrypt_2_anti_mod_chip = true;
for(int i=0; i < 24; i++)
{
if(libcrypt_2_anti_mod_chip[i] != sectors[s + i])
matched_libcrypt_2_anti_mod_chip = false;
}
if(matched_libcrypt_2_anti_mod_chip)
{
if(s < 0x800)
{
lba = ((current_fpos / 0x930) + 150);
} else {
lba = ((current_fpos / 0x930) + 151);
}
printf("Got LibCrypt v2 Anti-Mod-Chip bypass match (By B.A.D) at LBA: %u\n", lba);
// Part 1
sectors[s + 0] = 0x00;
sectors[s + 1] = 0x00;
sectors[s + 2] = 0x00;
sectors[s + 3] = 0x00;
// Part 2
sectors[s + 4] = 0x02;
sectors[s + 5] = 0x00;
sectors[s + 6] = 0xE7;
sectors[s + 7] = 0x30;
// Part 3
sectors[s + 8] = 0x00;
sectors[s + 9] = 0x00;
sectors[s + 10] = 0x00;
sectors[s + 11] = 0x00;
// Part 4
sectors[s + 12] = 0xAD;
sectors[s + 13] = 0xFF;
sectors[s + 14] = 0x84;
sectors[s + 15] = 0x20;
// Part 5
sectors[s + 16] = 0x00;
sectors[s + 17] = 0x00;
sectors[s + 18] = 0x00;
sectors[s + 19] = 0x00;
// Part 6
sectors[s + 20] = 0x00;
sectors[s + 21] = 0x00;
sectors[s + 22] = 0x00;
sectors[s + 23] = 0x00;
}
matched_libcrypt_2_magic_word = true;
for(int i=0; i < 4; i++)
{
if(libcrypt_2_magic_word[i] != sectors[s + i])
matched_libcrypt_2_magic_word = false;
}
if(matched_libcrypt_2_magic_word)
{
if(s < 0x800)
{
lba = ((current_fpos / 0x930) + 150);
} else {
lba = ((current_fpos / 0x930) + 151);
}
printf("Got LibCrypt v2 Magic Word bypass match (By B.A.D) at LBA: %u\n", lba);
sectors[s + 0] = bytes[0];
sectors[s + 1] = bytes[1];
sectors[s + 2] = 0xC6;
sectors[s + 3] = 0x34;
// 6C 3A C6 34
libcrypt_2 = false;
}
}
}
for(int i=0; i < 0x800; i++)
{
buf[current_fpos + i + 0x18] = sectors[i]; // skip 0x18 header info per sector
}
if(!last_sector)
{
for(int i=0; i < 0x800; i++)
{
buf[current_fpos + i + 0x18 + 0x930] = sectors[0x800 + i]; // skip 0x18 header info then skip exactly 1 sector. Read the next 0x800 bytes. We now have an array's worth of data from 2 sectors which excludes EDC/Header data at the beginning and end of each.
}
} else {
break; // That was the last sector
}
current_fpos = (current_fpos + 0x930); // Advance one sector.
}
fseek(bin, 0, SEEK_SET);
fwrite(buf, bin_size, 1, bin);
fclose(bin);
free(buf);
}
void bin_patch(const char **argv)
{
bool is_beatmania_append_gotta_mix = false;
bool is_beatmania_append_3rd_mix = false;
/*
The AP table could possibly start on the end of a sector and end at the beginning of the next sector. Each RAW sector is 0x930 bytes. The first 0x18 bytes are to be ignored as they are just header data. The next 0x800 bytes contains actual data we want to scan through.
Start at 0. Skip to 0x18. Read the next 0x800 bytes. Skip to a total of 0x930 bytes (one whole raw sector). Skip 0x18 bytes again and then read the next 0x800 bytes. We now have 2 sectors worth of straight up data in a buffer of 0x1000 bytes
Run search functions on the 0x1000 byte sized buffer.
*/
fseek(bin, 0, SEEK_END);
bin_size = ftell(bin);
if(bin_size > 0x2EE00000) // 750MB max, no PSX software comes even close to such a size
{
printf("Error: The CD image BIN file %s exceeds the maximum filesize of 750MB\n", argv[2]);
fclose(bin);
return;
}
fseek(bin, 0, SEEK_SET);
buf = (unsigned char *)malloc(bin_size * sizeof(unsigned char)); // Read entire BIN to memory for performance gain, I mean it's 2022 who doesn't have a free ~700MBs of RAM?!
if(fread(buf, 1, bin_size, bin) != bin_size)
{
printf("Error loading CD image BIN file: %s\n", argv[2]);
return;
}
printf("Successfully loaded CD image BIN file: %s (%d bytes in memory)\n", argv[2], bin_size);
max_size = bin_size;
number_of_sectors = (bin_size / 2352);
printf("Scanning %d sectors, please wait...\n", number_of_sectors);
while(1)
{
if(current_fpos > max_size)
break; // even number of sectors, done reading the file.
if((current_fpos + 0x930) == max_size) // odd number of sectors
last_sector = 1; // This function is reading 2 sectors at a time, so if there is an odd number of sectors we have to change the behavior to only search the last sector. Explicitly break loop when this is set.
for(int i=0; i < 0x800; i++)
{
sectors[i] = buf[current_fpos + i + 0x18]; // skip 0x18 header info per sector
}
if(!last_sector)
{
for(int i=0; i < 0x800; i++)
{
sectors[i + 0x800] = buf[current_fpos + i + 0x18 + 0x930]; // skip 0x18 header info then skip exactly 1 sector. Read the next 0x800 bytes. We now have an array's worth of data from 2 sectors which excludes EDC/Header data at the beginning and end of each.
}
search_size = 0x1000;
} else {
search_size = 0x800;
}
for(int s = 0; s < search_size; s++)
{
// Sector 23 is where no swap matches occur (which is within directory sectors maybe range)
if((directory_record_sectors_maybe) && (!is_beatmania_append_gotta_mix))
{
is_beatmania_append_gotta_mix = true;
for(int i=0; i < 66; i++)
{
if(beatmania_append_gotta_mix_system_cnf[i] != sectors[s + i])
{
is_beatmania_append_gotta_mix = false;
}
}
if(is_beatmania_append_gotta_mix)
{
if(s < 0x800)
{
lba = ((current_fpos / 0x930) + 150);
} else {
lba = ((current_fpos / 0x930) + 151);
}
printf("Got Append No Swap Bypass match (By mdmdj) at LBA: %u\n", lba);
sectors[s + 14] = append_exe_name[0]; // /
sectors[s + 15] = append_exe_name[1]; // A
sectors[s + 16] = append_exe_name[2]; // P
sectors[s + 17] = append_exe_name[3]; // P
sectors[s + 18] = append_exe_name[4]; // E
sectors[s + 19] = append_exe_name[5]; // N
sectors[s + 20] = append_exe_name[6]; // D
sectors[s + 21] = append_exe_name[7]; // .
sectors[s + 22] = append_exe_name[8]; // E
sectors[s + 23] = append_exe_name[9]; // X
sectors[s + 24] = append_exe_name[10]; // E
}
}
if((directory_record_sectors_maybe) && (!is_beatmania_append_3rd_mix))
{
is_beatmania_append_3rd_mix = true;
for(int i=0; i < 66; i++)
{
if(beatmania_append_3rd_mix_system_cnf[i] != sectors[s + i])
{
is_beatmania_append_3rd_mix = false;
}
}
if(is_beatmania_append_3rd_mix)
{
if(s < 0x800)
{
lba = ((current_fpos / 0x930) + 150);
} else {
lba = ((current_fpos / 0x930) + 151);
}
printf("Got Append.exe bypass match (By mdmdj) at LBA: %u\n", lba);
sectors[s + 14] = append_exe_name[0]; // /
sectors[s + 15] = append_exe_name[1]; // A
sectors[s + 16] = append_exe_name[2]; // P
sectors[s + 17] = append_exe_name[3]; // P
sectors[s + 18] = append_exe_name[4]; // E
sectors[s + 19] = append_exe_name[5]; // N
sectors[s + 20] = append_exe_name[6]; // D
sectors[s + 21] = append_exe_name[7]; // .
sectors[s + 22] = append_exe_name[8]; // E
sectors[s + 23] = append_exe_name[9]; // X
sectors[s + 24] = append_exe_name[10]; // E
}
}
matched_anti_piracy_v1 = true;
for(int i=0; i < 40; i++)
{
if(anti_piracy_v1[i] != sectors[s + i])
{
if(i != 3 && i != 7 && i != 11 && i != 15 && i != 19 && i != 23 && i != 27 && i != 31 && i != 35 && i != 39) // These bytes could change, they can be 03 or 05 depending on the AP code in the game but the table itself remains consistent besides the value of every 4th byte and the commands are still obvious via this pattern
matched_anti_piracy_v1 = false;
}
}
if(matched_anti_piracy_v1)
{
if(s < 0x800)
{
lba = ((current_fpos / 0x930) + 150);
} else {
lba = ((current_fpos / 0x930) + 151);
}
printf("Got APv1 Zero bypass match (By Alex Free) at LBA: %u\n", lba);
sectors[s + 20] = sync; // Replace SubFunq X's bytes with '00' bytes
sectors[s + 21] = sync;
sectors[s + 22] = sync;
sectors[s + 23] = sync;
sectors[s + 32] = sync; /// Replace SubFunq Y's bytes with '00' bytes
sectors[s + 33] = sync;
sectors[s + 34] = sync;
sectors[s + 35] = sync;
}
matched_anti_piracy_v2_vc0_bypass = true;
for(int i=0; i < 52; i++)
{
if(anti_piracy_v2_vc0_bypass[i] != sectors[s + i])
{
if(i != 3 && i != 7 && i != 11 && i != 15 && i != 19 && i != 23 && i != 27 && i != 31 && i != 35 && i != 39 && i != 43 && i != 47 && i != 51) // These bytes could change, they can be 03 or 05 depending on the AP code in the game but the table itself remains consistent besides the value of every 4th byte and the commands are still obvious via this pattern
{
matched_anti_piracy_v2_vc0_bypass = false;
}
}
}
if(matched_anti_piracy_v2_vc0_bypass)
{
if(s < 0x800)
{
lba = ((current_fpos / 0x930) + 150);
} else {
lba = ((current_fpos / 0x930) + 151);
}
printf("Got APv2 Fake VC0 bypass match (By Alex Free) at LBA: %u\n", lba);
sectors[s + 48] = sync; // Replace ReadTOC's first byte with the first byte of the sync command. This seems to trigger the VC0 CDROM Controller BIOS Firmware behavior on all consoles. The VC0 CDROM Controller BIOS firmware does not have the ReadTOC command, it is found in the wild in early SCPH-3000 Japanese consoles and in all SCPH-1000 consoles.
}
matched_anti_piracy_v2_pal_bypass = true;
for(int i=0; i < 16; i++)
{
if(anti_piracy_v2_pal_bypass[i] != sectors[s + i])
matched_anti_piracy_v2_pal_bypass = false;
}
if(matched_anti_piracy_v2_pal_bypass)
{
if(s < 0x800)
{
lba = ((current_fpos / 0x930) + 150);
} else {
lba = ((current_fpos / 0x930) + 151);
}
printf("Got APv2 Fake PAL BIOS bypass match (By Alex Free & MottZilla) at LBA: %u\n", lba);
sectors[s + 14] = 0x00;
sectors[s + 15] = 0x18;
// 0x00, 0x18 to make the game think your using a PAL BIOS/console
}
}
for(int i=0; i < 0x800; i++)
{
buf[current_fpos + i + 0x18] = sectors[i]; // skip 0x18 header info per sector
}
if(!last_sector)
{
for(int i=0; i < 0x800; i++)
{
buf[current_fpos + i + 0x18 + 0x930] = sectors[0x800 + i]; // skip 0x18 header info then skip exactly 1 sector. Read the next 0x800 bytes. We now have an array's worth of data from 2 sectors which excludes EDC/Header data at the beginning and end of each.
}
} else {
break; // That was the last sector
}
current_fpos = (current_fpos + 0x930); // Advance one sector.
}
fseek(bin, 0, SEEK_SET);
fwrite(buf, bin_size, 1, bin);
fclose(bin);
free(buf);
}
void gameshark_gen(const char **argv)
{
unsigned int libcrypt_2_anti_mod_chip_offset = 0;
unsigned int libcrypt_2_anti_pro_action_replay_offset = 0;
fseek(mem_dump_1, 0, SEEK_END);
mem_dump_1_size = ftell(mem_dump_1);
if(mem_dump_1_size != valid_mem_dump_size)
{
printf("Error: the original game's RAM dump file: %s is not the expected size\n", argv[2]);
fclose(mem_dump_1);
free(buf);
if(mem_dump_1_size == 8388608)
printf("Do you have the 'Enable 8MB RAM' option enabled? Uncheck that option if so and make a new RAM dump\n");
return;
}
fseek(mem_dump_1, 0, SEEK_SET);
buf = (unsigned char *)malloc(mem_dump_1_size * sizeof(unsigned char));
if(fread(buf, 1, mem_dump_1_size, mem_dump_1) != mem_dump_1_size)
{
printf("Error loading RAM dump file: %s\n", argv[2]);
return;
}
printf("Loaded RAM dump file: %s (%d bytes in memory)\n", argv[2], mem_dump_1_size);
while(1)
{
if(current_fpos > valid_mem_dump_size)
break;
if(!libcrypt)
{
matched_anti_piracy_v1 = true;
for(int i=0; i < 40; i++)
{
if(anti_piracy_v1[i] != buf[current_fpos + i])
{
if(i != 3 && i != 7 && i != 11 && i != 15 && i != 19 && i != 23 && i != 27 && i != 31 && i != 35 && i != 39) // These bytes could change, they can be 03 or 05 depending on the AP code in the game but the table itself remains consistent besides the value of every 4th byte and the commands are still obvious via this pattern
matched_anti_piracy_v1 = false;
}
}
if(matched_anti_piracy_v1)
{
printf("\nGot APv1 Zero bypass (By Alex Free) starting at offset: 0x%08X\n", current_fpos);
gameshark_write_byte_address = (current_fpos + 20); // SubFunq X (19'04)
// First 2 bytes of 19'04
bytes=(unsigned char *)&gameshark_write_byte_address; // pos of 20 from match base
bytes[3] = 0xD0;
printf("%08X 0119\n", gameshark_write_byte_address); // Look for SubFunq X (first 2 bytes)
bytes=(unsigned char *)&gameshark_write_byte_address;
bytes[3] = 0x80;
gameshark_write_byte_address = bytes[0] + (bytes[1] << 8) + (bytes[2] << 16) + (bytes[3] << 24); // NOT BIG-ENDIAN SAFE
printf("%08X 0000\n\n", gameshark_write_byte_address); // Replace SubFunq X's first 2 bytes with '00's
// Last 2 bytes of 19'04
gameshark_write_byte_address = (gameshark_write_byte_address + 2); // pos of 22 from match base
bytes=(unsigned char *)&gameshark_write_byte_address;
last_byte = buf[current_fpos + 23];
bytes[3] = 0xD0;
printf("%08X %02X04\n", gameshark_write_byte_address, last_byte); // Look for SubFunq X (last 2 bytes). The last byte can change, usually it is 0x03 or 0x05. So we get that byte from the mem dump file.
bytes=(unsigned char *)&gameshark_write_byte_address;