-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathcobra.c
2439 lines (1987 loc) · 52.9 KB
/
cobra.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
// #include <sys/return_code.h>
#include <sys/timer.h>
#include <sys/memory.h>
#include <cell/cell_fs.h>
// #include <cell/usbd.h>
// #include <stdio.h>
// #include <string.h>
// #include <cell/hash/libmd5.h>
#define COBRA_ONLY
#define _COBRA_C
#include "cobra.h"
#include "storage_ext.h"
#include "psp.h"
// #include "syscall8.h"
// #include "scsi.h"
// #include "ufi.h"
// #include "base_mds.h"
#define TYPE_HOST2DEV (USB_REQTYPE_DIR_TO_DEVICE|USB_REQTYPE_TYPE_VENDOR)
#define TYPE_DEV2HOST (USB_REQTYPE_DIR_TO_HOST|USB_REQTYPE_TYPE_VENDOR)
#define FAILED -1
#define SWAP32(x) ((((x) & 0xff) << 24) | (((x) & 0xff00) << 8) | (((x) & 0xff0000) >> 8) | (((x) >> 24) & 0xff))
#define RANGE(a, b, c) ((a) <= (b) ? (b) : (a) >= (c) ? (c) : (a))
#define KB 1024
#define _16KB_ 16384UL
#define _128KB_ 131072UL
#define PSPL_PATH1 "/dev_hdd0//game/PSPM66820"
#define PSPL_ICON1 PSPL_PATH1 "/ICON0.PNG"
#define PSPL_PATH2 "/dev_hdd0//game/PSPC66820"
#define PSPL_ICON2 PSPL_PATH2 "/ICON0.PNG"
//#define PSPL_LAMBDA PSPL_PATH1 "/USRDIR/CONTENT/lambda.db"
//#define PSPL_LAMBDA_NONCE 0x0ab40b3bbd1f1a7bULL
#define DPRINTF(...)
enum
{
// Reads the flash rawly. Used by PC flasher.
CMD_SPI_FLASH_READ = 0x10,
// Reads flash and decrypts with keys 2. Deprecated since version 3.0.
CMD_SPI_FLASH_READ_AND_DECRYPT,
// Programs a page. Used by PC flasher.
CMD_SPI_FLASH_PAGE_PROGRAM,
// Decrypts buffer with key 0 and programs to flash. Used by PC flasher.
CMD_SPI_FLASH_DECRYPT_AND_PAGE_PROGRAM,
// Erase a sector. Used by PC flasher
CMD_SPI_FLASH_ERASE_SECTOR,
// Erases the chip.
CMD_SPI_FLASH_CHIP_ERASE,
// Read the scp flashrom. Used by PC flasher, but its value is not used currenttly.
CMD_SCP_FLASHROM_READ,
// Sets buffer for crypt operation. There is one special mode which should be forbidden if not in PS3 mode.
CMD_SCP_SET_BUFFER,
// Starts a decryption/encryption operation. Encryption operations are forbidden on preprogrammed keys. Used by PC flasher (key 1 and 3 decryption).
CMD_SCP_CRYPT,
// Starts a handshake operation.
CMD_SCP_HANDSHAKE,
// Sets user key for encryption/decryption
CMD_SCP_SET_USER_KEY,
// Unused scp jtag opcodes
CMD_SCP_SET_JTAG, /* UNUSED */
CMD_SCP_READ_TDO, /* UNUSED */
// Decrypts with key 2 and writes to eeprom. Used by PC flasher.
CMD_MCU_EEPROM_DECRYPT_AND_WRITE,
// Reboots MCU. Used by PC flasher, but no current firmware has used this opcode.
CMD_MCU_REBOOT,
// Starts bootloader. Used by PC flasher.
CMD_MCU_START_BOOTLOADER,
// Reads flash and decrypts with keys 1. Scurity panic if used and not ps3 mode.
CMD_SPI_FLASH_READ_AND_DECRYPT2,
// LEDs control. Added in firmware 3.0
CMD_LED_CONTROL,
// PS3 security. Added in firmware 3.0. IT MUST ONLY BE USED by PS3.
CMD_PS3_SECURITY_IN,
CMD_PS3_SECURITY_OUT,
// Sets ps3 mode
CMD_PS3_SET,
// Validates a PS3 encoded psid
CMD_PS3_VALIDATE,
// Hashes flash
CMD_SPI_FLASH_HASH,
// Set Hash size
CMD_SPI_FLASH_SET_HASH_SIZE
};
/*
typedef struct
{
char key[64];
int value;
} KeyValue;
typedef struct
{
char server[0x40];
char path[0x420];
u32 emu_mode;
u32 numtracks;
u16 port;
u8 pad[6];
ScsiTrackDescriptor tracks[32];
} __attribute__((packed)) netiso_args;
*/
void _memset(void *m, size_t n);
size_t read_file(const char *file, char *data, const size_t size, s32 offset);
int save_file(const char *file, const char *mem, s64 size);
int file_copy(const char *file1, char *file2);
int wait_for(const char *path, u8 timeout);
#define not_exists(path) (!file_exists(path))
static u8 file_exists(const char *path)
{
CellFsStat stat;
return (cellFsStat(path, &stat) == CELL_FS_SUCCEEDED);
}
/*
#define N_TITLE_IDS 102
#define N_TITLE_NAMES 24
static KeyValue emu_by_title_ids[N_TITLE_IDS] =
{
// First, games really fixed //
// Lunar silver star
{ "NPJH-90073", EMU_400 },
{ "NPUH-90053", EMU_400 },
{ "ULUS-10482", EMU_400 },
{ "NPEH-00076", EMU_400 },
{ "ULJM-05535", EMU_400 },
{ "ULUS-10514", EMU_400 },
// Tekken 6
{ "ULES-01376", EMU_400 },
{ "NPJH-50184", EMU_400 },
{ "ULJS-00224", EMU_400 },
{ "ULUS-10466", EMU_400 },
{ "ULAS-42214", EMU_400 },
{ "ULJS-19054", EMU_400 },
// BlazBlue - Continuum Shift II
{ "ULJM-05850", EMU_400 },
{ "ULUS-10579", EMU_400 },
// Exit
{ "ULES-00285", EMU_400 },
{ "ULUS-10074", EMU_400 },
{ "ULJM-05062", EMU_400 },
{ "ULJM-05271", EMU_400 },
// DJ max portable 3
{ "ULKS-46236", EMU_400 },
{ "ULUS-10538", EMU_400 },
{ "ULJM-05836", EMU_400 },
{ "CF00-20046", EMU_400 },
// Fate/Extra
{ "ULUS-10576", EMU_400 },
{ "NPJH-50247", EMU_400 },
{ "ULJS-00254", EMU_400 },
{ "ULJS-00253", EMU_400 },
{ "ULUS-10588", EMU_400 },
// 3rd birthday
{ "ULES-01513", EMU_400 },
{ "ULJM-05798", EMU_400 },
{ "ULUS-10567", EMU_400 },
// Lego pirates of the caribbean
{ "ULES-01528", EMU_400 },
{ "ULES-01529", EMU_400 },
{ "ULUS-10575", EMU_400 },
// Persona 2: innocent sin
{ "ULUS-10584", EMU_400 },
{ "ULJM-05759", EMU_400 },
{ "UCAS-40338", EMU_400 },
{ "NPJH-50329", EMU_400 },
// Resistance retribution
{ "NPEG-90013", EMU_400 },
{ "NPJG-90037", EMU_400 },
{ "NPUG-22850", EMU_400 },
{ "UCES-01184", EMU_400 },
{ "UCUS-98668", EMU_400 },
{ "UCJS-10090", EMU_400 },
// Soul calibur broken destiny
{ "ULES-01298", EMU_400 },
{ "ULJS-00202", EMU_400 },
{ "ULUS-10457", EMU_400 },
{ "ULJS-19055", EMU_400 },
// Star ocean first departure
{ "ULJM-05290", EMU_400 },
{ "ULJM-05298", EMU_400 },
{ "ULES-01154", EMU_400 },
{ "ULUS-10374", EMU_400 },
{ "ULJM-05590", EMU_400 },
// Valhalla Knights
{ "ULJS-00075", EMU_400 },
{ "ULUS-10230", EMU_400 },
{ "ULJM-00075", EMU_400 },
{ "ULKS-46133", EMU_400 },
{ "ULJS-19010", EMU_400 },
{ "ULES-00657", EMU_400 },
// White Knight Chronicles: origins
{ "UCJS-10115", EMU_400 },
{ "UCES-01511", EMU_400 },
// Z.H.P. : Unlosing Ranger Vs Darkdeath
{ "ULUS-10559", EMU_400 },
{ "ULJS-00262", EMU_400 },
{ "ULJS-00261", EMU_400 },
// Games that still don't work, but show VISIBLE progress //
// Ben 10 Ultimate Alien: Cosmic Destruction -> Warning, we don't have yet a proper title_name for this one, and we aer missing title id's
{ "ULUS-10542", EMU_400 },
// Final Fantasy Dissidia 012
{ "ULES-01505", EMU_400 },
{ "ULJM-05814", EMU_400 },
{ "ULUS-10566", EMU_400 },
// Final Fantasy IV Complete Collection
{ "ULJM-05855", EMU_400 },
{ "ULUS-10560", EMU_400 },
{ "NPJH-50414", EMU_400 },
// Gods Eater Burst -> Warning, we don't have yet a proper title name
{ "ULES-01519", EMU_400 },
{ "NPJH-50352", EMU_400 },
{ "ULUS-10563", EMU_400 },
{ "ULJS-00351", EMU_400 },
{ "ULJS-00350", EMU_400 },
{ "ULJS-19056", EMU_400 },
// Need for Speed: Most Wanted
{ "ULAS-42031", EMU_400 },
{ "ULES-00196", EMU_400 },
{ "ULJM-05073", EMU_400 },
{ "ULKS-46044", EMU_400 },
{ "ULUS-10036", EMU_400 },
{ "ULJM-05183", EMU_400 },
// Persona 3
{ "ULES-01523", EMU_400 },
{ "NPJH-50040", EMU_400 },
{ "ULUS-10512", EMU_400 },
{ "UCAS-40288", EMU_400 },
{ "ULJM-05489", EMU_400 },
{ "ULJM-08044", EMU_400 },
// Ridge Racer 1
{ "ULJS-00001", EMU_400 },
{ "UCES-00002", EMU_400 },
{ "UCAS-40015", EMU_400 },
{ "ULUS-10001", EMU_400 },
{ "UCKS-45002", EMU_400 },
{ "ULJS-19002", EMU_400 },
{ "UCKS-45053", EMU_400 },
// Tactics ogre
{ "ULJM-05753", EMU_400 },
{ "ULUS-10565", EMU_400 },
{ "ULES-01500", EMU_400 },
// Warhammer 40000 Squad Team
{ "NPEH-90001", EMU_400 },
{ "ULUD-90004", EMU_400 },
{ "ULES-00873", EMU_400 },
{ "ULUS-10313", EMU_400 },
};
static KeyValue emu_by_title_name[N_TITLE_NAMES] =
{
// First, games really fixed //
{ { 0x42, 0x4C, 0x41, 0x5A, 0x42, 0x4C, 0x55, 0x45, 0x20, 0x43, 0x4F, 0x4E, 0x54, 0x49,
0x4E, 0x55, 0x55, 0x4D, 0x20, 0x53, 0x48, 0x49, 0x46, 0x54, 0x20, 0xE2, 0x85, 0xA1, 0x00 }, EMU_400 }, // BLAZBLUE CONTINUUM SHIFT II
{ "DJMAX PORTABLE 3", EMU_400 },
{ "EXIT", EMU_400 },
{ "Fate/EXTRA", EMU_400 },
{ { 0x4C, 0x45, 0x47, 0x4F, 0xC2, 0xAE, 0x20, 0x50, 0x69, 0x72, 0x61, 0x74, 0x65, 0x73, 0x20, 0x6F,
0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x43, 0x61, 0x72, 0x69, 0x62, 0x62, 0x65, 0x61, 0x6E, 0x20,
0x54, 0x68, 0x65, 0x20, 0x56, 0x69, 0x64, 0x65, 0x6F, 0x20, 0x47, 0x61, 0x6D, 0x65, 0x20, 0x00 }, EMU_400 }, //LEGO® Pirates of the Caribbean The Video Game
{ "LUNAR: SILVER STAR HARMONY", EMU_400 },
{ { 0x52, 0x45, 0x53, 0x49, 0x53, 0x54, 0x41, 0x4E, 0x43, 0x45, 0xC2, 0xAE, 0x20, 0xEF, 0xBD, 0x9E,
0xE5, 0xA0, 0xB1, 0xE5, 0xBE, 0xA9, 0xE3, 0x81, 0xAE, 0xE5, 0x88, 0xBB, 0xEF, 0xBD, 0x9E, 0x00 }, EMU_400 }, // Resistance retribution (JP)
{ { 0x52, 0x65, 0x73, 0x69, 0x73, 0x74, 0x61, 0x6E, 0x63, 0x65, 0x3A, 0x20, 0x52, 0x65, 0x74, 0x72,
0x69, 0x62, 0x75, 0x74, 0x69, 0x6F, 0x6E, 0xE2, 0x84, 0xA2, 0x00 }, EMU_400 }, // Resistance: Retributionâ„¢
{ "SMT: Persona 2: Innocent Sin", EMU_400 },
{ { 0x50, 0x45, 0x52, 0x53, 0x4F, 0x4E, 0x41, 0x32, 0x20, 0xE7, 0xBD, 0xAA, 0x00 }, EMU_400 }, // Persona 2 jap
{ "SOULCALIBUR: Broken Destiny", EMU_400 },
{ "STAR OCEAN: First Departure", EMU_400 },
{ "TEKKEN 6", EMU_400 },
{ "The 3rd Birthday", EMU_400 },
{ "VALHALLAKNIGHTS", EMU_400 },
{ { 0x57, 0x68, 0x69, 0x74, 0x65, 0x20, 0x4B, 0x6E, 0x69, 0x67, 0x68, 0x74, 0x20, 0x43, 0x68, 0x72,
0x6F, 0x6E, 0x69, 0x63, 0x6C, 0x65, 0x73, 0xE2, 0x84, 0xA2, 0x3A, 0x20, 0x4F, 0x72, 0x69, 0x67,
0x69, 0x6E, 0x73, 0x00 }, EMU_400 }, // White Knight Chroniclesâ„¢: Origins
{ "Z.H.P.: Unlosing Ranger VS Darkdeath Evilman", EMU_400 },
// Games that don't work yet, but show progress //
{ "DISSIDIA duodecim FINAL FANTASY", EMU_400 },
{ "FINAL FANTASY IV Complete Collection", EMU_400 },
{ { 0x4E, 0x45, 0x45, 0x44, 0x20, 0x46, 0x4F, 0x52, 0x20, 0x53, 0x50, 0x45, 0x45, 0x44, 0xE2, 0x84,
0xA2, 0x20, 0x4D, 0x4F, 0x53, 0x54, 0x20, 0x57, 0x41, 0x4E, 0x54, 0x45, 0x44, 0x20, 0x35, 0x2D,
0x31, 0x2D, 0x30, 0x00 }, EMU_400 }, // NEED FOR SPEEDâ„¢ MOST WANTED 5-1-0
{ "Persona3 PORTABLE", EMU_400 },
{ "RIDGE RACER", EMU_400 },
{ "TACTICS OGRE Let Us Cling Together", EMU_400 },
{ "Warhammer 40,000: Squad Command", EMU_400 },
};
static u8 lambda_md5[16] =
{
0xE1, 0x99, 0xCA, 0x7D, 0x48, 0x3B, 0xC0, 0x7B, 0x4D, 0xC6, 0xE7, 0x4A, 0xE5, 0x53, 0x76, 0xCE
};
*/
static int translate_type(unsigned int type)
{
if(type == 0) return DISC_TYPE_NONE;
if(type == DEVICE_TYPE_PS3_BD) return DISC_TYPE_PS3_BD;
if(type == DEVICE_TYPE_PS3_DVD) return DISC_TYPE_PS3_DVD;
if(type == DEVICE_TYPE_PS2_DVD) return DISC_TYPE_PS2_DVD;
if(type == DEVICE_TYPE_PS2_CD) return DISC_TYPE_PS2_CD;
if(type == DEVICE_TYPE_PSX_CD) return DISC_TYPE_PSX_CD;
if(type == DEVICE_TYPE_BDROM) return DISC_TYPE_BDROM;
if(type == DEVICE_TYPE_BDMR_SR) return DISC_TYPE_BDMR_SR;
if(type == DEVICE_TYPE_BDMR_RR) return DISC_TYPE_BDMR_RR;
if(type == DEVICE_TYPE_BDMRE) return DISC_TYPE_BDMRE;
if(type == DEVICE_TYPE_DVD) return DISC_TYPE_DVD;
if(type == DEVICE_TYPE_CD) return DISC_TYPE_CD;
return DISC_TYPE_UNKNOWN;
}
/*
static char *get_blank_iso_path(void)
{
char *s = malloc(32);
strcpy(s, "/dev_hdd0/vsh/task.dat\0");
return s;
}
*/
static void build_iso_record(u8 *buf, u32 offset)
{
buf[offset] = 0x28;
buf[offset + 2] = buf[offset + 9] = 0x18;
buf[offset + 0x0B] = buf[offset + 0x10] = 8;
buf[offset + 0x12] = 0x6F;
buf[offset + 0x13] = 7;
buf[offset + 0x14] = 0x16;
buf[offset + 0x15] = 2;
buf[offset + 0x16] = 0x2B;
buf[offset + 0x17] = buf[offset + 0x19] = 2;
buf[offset + 0x1C] = buf[offset + 0x1F] = buf[offset + 0x20] = 1;
}
static void build_blank_iso(const char *title_id)
{
sys_addr_t sysmem = NULL;
if(sys_memory_allocate(_128KB_, SYS_MEMORY_PAGE_SIZE_64K, &sysmem) /*!= CELL_OK*/) return;
u8 *buf = (u8*)sysmem;
/*
// build task.dat from external template file
read_file("/dev_hdd0/tmp/wm_res/task.dat", (char*)buf, _128KB_, 0);
memcpy(buf + 0x810, title_id, 4);
buf[0x814] = '-';
memcpy(buf + 0x815, title_id+4, 5);
save_file("/dev_hdd0/vsh/task.dat", (char*)buf, _128KB_);
if(sysmem) sys_memory_free(sysmem);
return;
*/
_memset(buf, _128KB_);
buf[3] = 2;
buf[0x17] = 0x3F;
strcpy((char *)buf + 0x800, "PlayStation3");
memcpy(buf + 0x810, title_id, 4);
buf[0x814] = '-';
memcpy(buf + 0x815, title_id + 4, 5);
memset(buf + 0x81A, ' ', 0x16);
buf[0x8000] = 1;
strcpy((char *)buf + 0x8001, "CD001");
buf[0x8006] = 1;
memset(buf + 0x8008, ' ', 0x40);
memcpy(buf + 0x8028, "PS3VOLUME", 9);
buf[0x8050] = buf[0x8057] = 0x40;
buf[0x8078] = buf[0x807B] = buf[0x807C] = buf[0x807F] = 1;
buf[0x8081] = buf[0x8082] = 8;
buf[0x8084] = buf[0x808B] = 0xA;
buf[0x808C] = 0x14;
buf[0x8097] = 0x15;
build_iso_record(buf, 0x809C);
buf[0x809C] = 0x22;
memcpy(buf + 0x80BE, "PS3VOLUME", 9);
memset(buf + 0x80C7, ' ', 0x266);
strcpy((char *)buf + 0x832D, "20131111050513");
memset(buf + 0x833B, '0', 0x35);
buf[0x833D] = buf[0x834E] = buf[0x835F] = 0;
buf[0x8371] = 1;
buf[0x8800] = 2;
strcpy((char *)buf + 0x8801, "CD001");
buf[0x8806] = 1;
buf[0x8829] = 'P';
buf[0x882B] = 'S';
buf[0x882D] = '3';
buf[0x882F] = 'V';
buf[0x8831] = 'O';
buf[0x8833] = 'L';
buf[0x8835] = 'U';
buf[0x8837] = 'M';
buf[0x8839] = 'E';
buf[0x8850] = buf[0x8857] = buf[0x885A] = 0x40;
buf[0x8858] = 0x25;
buf[0x8859] = 0x2F;
buf[0x8878] = buf[0x887B] = buf[0x887C] = buf[0x887F] = 1;
buf[0x8881] = buf[0x8882] = 8;
buf[0x8884] = buf[0x888B] = 0xA;
buf[0x888C] = 0x16;
buf[0x8897] = 0x17;
build_iso_record(buf, 0x889C);
buf[0x889C] = 0x22;
buf[0x889E] = buf[0x88A5] = 0x19;
memcpy(buf + 0x88BF, buf + 0x8829, 17); // 'P S 3 V O L U M E'
memcpy((char *)buf + 0x8B2D, (char *)buf + 0x832D, 0x43);
buf[0x8B71] = 1;
buf[0x9000] = 0xFF;
strcpy((char *)buf + 0x9001, "CD001");
buf[0xA000] = buf[0xA006] = buf[0xA800] = buf[0xA807] = 1;
buf[0xA002] = buf[0xA805] = 0x18;
buf[0xB000] = buf[0xB006] = buf[0xB800] = buf[0xB807] = 1;
buf[0xB002] = buf[0xB805] = 0x19;
build_iso_record(buf, 0xC000);
build_iso_record(buf, 0xC028);
buf[0xC049] = 1;
memcpy(buf + 0xC800, buf + 0xC000, 0x50);
save_file("/dev_hdd0/vsh/task.dat", (char*)buf, _128KB_);
if(sysmem) sys_memory_free(sysmem);
return;
}
/*
static int copy_file(char *src, char *dst)
{
int ret;
int fd_s, fd_d;
ret = cellFsOpen(src, CELL_FS_O_RDONLY, &fd_s, NULL, 0);
if(ret == 0)
{
ret = cellFsOpen(dst, CELL_FS_O_CREAT | CELL_FS_O_WRONLY | CELL_FS_O_TRUNC, &fd_d, NULL, 0);
if(ret == 0)
{
const u32 buf_size = _16KB_;
u8 *buf = (u8 *)malloc(buf_size);
while(1)
{
u64 nread, nwritten;
ret = cellFsRead(fd_s, buf, buf_size, &nread);
if (ret != CELL_FS_SUCCEEDED || nread == 0) break;
ret = cellFsWrite(fd_d, buf, nread, &nwritten);
if (ret != CELL_FS_SUCCEEDED) break;
if (nwritten != nread)
{
ret = -1;
break;
}
}
cellFsClose(fd_d);
free(buf);
}
cellFsClose(fd_s);
if(ret)
{
cellFsUnlink(dst);
}
else
{
cellFsChmod(dst, CELL_FS_S_IFMT | 0777);
}
}
//DPRINTF("Copy file returning: %x\n", ret);
free(buf);
return ret;
}
*/
static int sys_get_hw_config(u8 *ret, u8 *config)
{
system_call_2(393, (u64)(u32)ret, (u64)(u32)config);
return (int)p1;
}
/*
static int sys_get_version(u32 *version)
{
system_call_2(SC_COBRA_SYSCALL8, SYSCALL8_OPCODE_GET_VERSION, (u64)(u32)version);
return (int)p1;
}
static int sys_get_version2(u16 *version)
{
system_call_2(SC_COBRA_SYSCALL8, SYSCALL8_OPCODE_GET_VERSION2, (u64)(u32)version);
return (int)p1;
}
*/
static int sys_read_cobra_config(CobraConfig *cfg)
{
system_call_2(SC_COBRA_SYSCALL8, SYSCALL8_OPCODE_READ_COBRA_CONFIG, (u64)(u32)cfg);
return (int)p1;
}
static int sys_write_cobra_config(CobraConfig *cfg)
{
system_call_2(SC_COBRA_SYSCALL8, SYSCALL8_OPCODE_WRITE_COBRA_CONFIG, (u64)(u32)cfg);
return (int)p1;
}
/*
static char *trim(char *str)
{
int len = strlen(str);
u8 *temp = (u8 *)strdup(str);
u8 *p = temp;
int i;
for (i = 0; i < len; i++)
{
if (temp[i] <= ' ')
p++;
else
break;
}
len = strlen((char *)p);
for (i = len-1; i >= 0; i--)
{
if (p[i] <= ' ')
p[i] = 0;
else
break;
}
strcpy(str, (char *)p);
free(temp);
return str;
}
static int parse_param_sfo(char *file, const char *field, char *title_name)
{
FILE *fp;
fp = fopen(file, "rb");
if (fp) {
unsigned len, pos, str;
unsigned char *mem = NULL;
fseek(fp, 0, SEEK_END);
len = ftell(fp);
mem = (unsigned char *) malloc(len + 16);
if (!mem) {
fclose(fp);
return -2;
}
_memset(mem, len + 16);
fseek(fp, 0, SEEK_SET);
fread((void *) mem, len, 1, fp);
fclose(fp);
str = (mem[8] + (mem[9] << 8));
pos = (mem[0xc] + (mem[0xd] << 8));
int indx = 0;
while (str < len) {
if (mem[str] == 0)
break;
if (!strcmp((char *) &mem[str], field)) {
strncpy(title_name, (char *) &mem[pos], 63);
free(mem);
return 0;
}
while (mem[str])
str++;
str++;
pos += (mem[0x1c + indx] + (mem[0x1d + indx] << 8));
indx += 16;
}
//if (mem) free(mem);
}
return -1;
}
*/
/*
static int sys_permissions_get_access(void)
{
system_call_1(SC_COBRA_SYSCALL8, SYSCALL8_OPCODE_GET_ACCESS);
return (int)p1;
}
static int sys_permissions_remove_access(void)
{
system_call_1(SC_COBRA_SYSCALL8, SYSCALL8_OPCODE_REMOVE_ACCESS);
return (int)p1;
}
static int cobra_usb_command(u8 command, int requestType, u32 addr, void *buf, u16 size)
{
system_call_6(SC_COBRA_SYSCALL8, SYSCALL8_OPCODE_COBRA_USB_COMMAND, command, requestType, addr, (u64)(u32)buf, size);
return (int)p1;
}
int cobra_lib_init(void)
{
return sys_permissions_get_access();
}
int cobra_lib_finalize(void)
{
return sys_permissions_remove_access();
}
*/
int cobra_get_disc_type(unsigned int *real_disctype, unsigned int *effective_disctype, unsigned int *iso_disctype)
{
unsigned int rdt, edt;
int ret;
ret = sys_storage_ext_get_disc_type(&rdt, &edt, NULL);
if (ret)
return ret;
if (real_disctype)
{
*real_disctype = translate_type(rdt);
}
if (effective_disctype)
{
*effective_disctype = translate_type(edt);
}
if (iso_disctype)
{
*iso_disctype = DISC_TYPE_NONE;
sys_emu_state_t emu_state;
emu_state.size = sizeof(sys_emu_state_t);
ret = sys_storage_ext_get_emu_state(&emu_state);
if (ret == 0)
{
int disc_emulation = emu_state.disc_emulation;
if (disc_emulation != EMU_OFF)
{
switch (disc_emulation)
{
case EMU_PS3:
*iso_disctype = DISC_TYPE_PS3_BD;
break;
case EMU_PS2_DVD:
*iso_disctype = DISC_TYPE_PS2_DVD;
break;
case EMU_PS2_CD:
*iso_disctype = DISC_TYPE_PS2_CD;
break;
case EMU_PSX:
*iso_disctype = DISC_TYPE_PSX_CD;
break;
case EMU_BD:
if (edt != DISC_TYPE_NONE)
*iso_disctype = edt;
else
*iso_disctype = DISC_TYPE_BDMR_SR;
break;
case EMU_DVD:
*iso_disctype = DISC_TYPE_DVD;
break;
default:
*iso_disctype = DISC_TYPE_UNKNOWN;
}
}
}
}
return 0;
}
int cobra_disc_auth(void)
{
unsigned int real_disctype;
int ret;
ret = sys_storage_ext_get_disc_type(&real_disctype, NULL, NULL);
if (ret)
return ret;
if (real_disctype == 0)
return ENODEV;
if (real_disctype == DEVICE_TYPE_PS3_BD || real_disctype == DEVICE_TYPE_PS3_DVD)
{
static u8 buf[1024]; _memset(buf, sizeof(buf));
sys_ss_disc_auth(0x5007, (u64)(u32)buf);
}
else
{
sys_ss_disc_auth(0x5004, 0x29);
}
return 0;
}
static unsigned int ejected_realdisc;
int cobra_send_fake_disc_eject_event(void)
{
sys_storage_ext_get_disc_type(&ejected_realdisc, NULL, NULL);
sys_storage_ext_fake_storage_event(4, 0, BDVD_DRIVE);
return sys_storage_ext_fake_storage_event(8, 0, BDVD_DRIVE);
}
int cobra_send_fake_disc_insert_event(void)
{
u64 param;
unsigned int real_disctype, effective_disctype, iso_disctype;
cobra_get_disc_type(&real_disctype, &effective_disctype, &iso_disctype);
if (ejected_realdisc == 0 && real_disctype == 0 && effective_disctype == 0 && iso_disctype == 0)
{
//printf("All disc types 0, aborting\n");
return EABORT;
}
param = (u64)(ejected_realdisc) << 32ULL;
sys_storage_ext_get_disc_type(&ejected_realdisc, NULL, NULL);
sys_storage_ext_fake_storage_event(7, 0, BDVD_DRIVE);
return sys_storage_ext_fake_storage_event(3, param, BDVD_DRIVE);
}
int cobra_mount_ps3_disc_image(char *files[], unsigned int num)
{
if(!files) return EINVAL;
return sys_storage_ext_mount_ps3_discfile(num, files);
}
int cobra_mount_dvd_disc_image(char *files[], unsigned int num)
{
if(!files) return EINVAL;
return sys_storage_ext_mount_dvd_discfile(num, files);
}
int cobra_mount_bd_disc_image(char *files[], unsigned int num)
{
if (!files) return EINVAL;
return sys_storage_ext_mount_bd_discfile(num, files);
}
static void init_tracks(int num_tracks, TrackDef *tracks, ScsiTrackDescriptor *scsi_tracks)
{
_memset(scsi_tracks, num_tracks * sizeof(ScsiTrackDescriptor));
for (int i = 0; i < num_tracks; i++)
{
scsi_tracks[i].adr_control = (tracks[i].is_audio) ? 0x10 : 0x14;
scsi_tracks[i].track_number = i + 1;
scsi_tracks[i].track_start_addr = tracks[i].lba;
}
}
int cobra_mount_psx_disc_image(char *file, TrackDef *tracks, unsigned int num_tracks)
{
if (!file || !tracks) return EINVAL;
num_tracks = RANGE(num_tracks, 1, MAX_TRACKS);
ScsiTrackDescriptor scsi_tracks[num_tracks];
init_tracks(num_tracks, tracks, scsi_tracks);
return sys_storage_ext_mount_psx_discfile(file, num_tracks, scsi_tracks);
}
int cobra_mount_ps2_disc_image(char *files[], int num, TrackDef *tracks, unsigned int num_tracks)
{
if (!files || !tracks) return EINVAL;
num_tracks = RANGE(num_tracks, 1, MAX_TRACKS);
ScsiTrackDescriptor scsi_tracks[num_tracks];
init_tracks(num_tracks, tracks, scsi_tracks);
num = RANGE(num, 1, 32);
return sys_storage_ext_mount_ps2_discfile(num, files, num_tracks, scsi_tracks);
}
int cobra_umount_disc_image(void)
{
int ret = sys_storage_ext_umount_discfile();
if (ret == FAILED)
ret = ENODEV;
return ret;
}
/*
int cobra_read_ps3_disc(void *buf, u64 sector, u32 count)
{
return sys_storage_ext_read_ps3_disc(buf, sector, count);
}
int cobra_get_disc_phys_info(u32 handle, u8 layer, DiscPhysInfo *info)
{
ScsiReadDiscStructureFormat0Response *response;
u8 scsi_cmd[56];
static u8 output[64] __attribute__((aligned(64)));
int ret;
if (!info)
return EINVAL;
ScsiCmdReadDiscStructure *cmd = (ScsiCmdReadDiscStructure *)scsi_cmd;
StorageCmdScsiData *data = (StorageCmdScsiData *)(scsi_cmd+32);
_memset(scsi_cmd, sizeof(scsi_cmd));
cmd->opcode = SCSI_CMD_READ_DISC_STRUCTURE;
cmd->alloc_length = sizeof(ScsiReadDiscStructureFormat0Response);
cmd->layer_num = layer;
data->inlen = 12;
data->unk1 = data->unk2 = data->unk3 = 1;
data->outlen = sizeof(ScsiReadDiscStructureFormat0Response);
response = (ScsiReadDiscStructureFormat0Response *)output;
ret = sys_storage_send_device_command(handle, STORAGE_COMMAND_NATIVE, scsi_cmd, sizeof(scsi_cmd), response, sizeof(ScsiReadDiscStructureFormat0Response));
if (ret)
return ret;
memcpy(info, output+4, 17);
return 0;
}
int cobra_get_cd_td(u32 handle, TrackDef *td, unsigned int max_tracks, unsigned int *num_tracks, u32 *lba_end)
{
u32 cd_num_tracks, cd_total_tracks;
u8 scsi_cmd[56];
int ret;
int i, j;
if (!td || !num_tracks)
return EINVAL;
ScsiCmdReadTocPmaAtip *cmd = (ScsiCmdReadTocPmaAtip *)scsi_cmd;
StorageCmdScsiData *data = (StorageCmdScsiData *)(scsi_cmd+32);
ScsiTocResponse toc_info;
_memset(scsi_cmd, sizeof(scsi_cmd));
cmd->opcode = SCSI_CMD_READ_TOC_PMA_ATIP;
cmd->alloc_length = sizeof(toc_info);
data->inlen = 12;
data->unk1 = data->unk2 = data->unk3 = 1;
data->outlen = cmd->alloc_length;
ret = sys_storage_send_device_command(handle, STORAGE_COMMAND_NATIVE, scsi_cmd, sizeof(scsi_cmd), &toc_info, sizeof(toc_info));
if (ret)
return ret;
cd_num_tracks = toc_info.last_track;
u8 *response = (u8 *) malloc(toc_info.toc_length+2);
cmd->alloc_length = toc_info.toc_length+2;
data->outlen = cmd->alloc_length;
ret = sys_storage_send_device_command(handle, STORAGE_COMMAND_NATIVE, scsi_cmd, sizeof(scsi_cmd), response, cmd->alloc_length);
if (ret)
{
free(response);
return ret;
}
if (toc_info.first_track != 1)
{
return ENOTSUP;
}
else if ((unsigned int)(toc_info.toc_length-2) < (unsigned int)toc_info.last_track*sizeof(ScsiTrackDescriptor))
{
return ENOTSUP;
}
ScsiTrackDescriptor *tracks = (ScsiTrackDescriptor *)(response+sizeof(ScsiTocResponse));
cd_total_tracks = (toc_info.toc_length-2) / sizeof(ScsiTrackDescriptor);
for (i = 1; i <= cd_num_tracks; i++)
{
int found = 0;
for (j = 0; j < cd_total_tracks; j++)
{
if (tracks[j].track_number == i)
{
int track_type = tracks[j].adr_control & 0xD;
int is_audio = (track_type == 0 || track_type == 1 || track_type == 8 || track_type == 9);
if (i <= max_tracks)
{
td[i-1].lba = tracks[j].track_start_addr;
td[i-1].is_audio = is_audio;
}
found = 1;
break;
}
}
if (!found)
{
ret = ENOTSUP;
break;
}
}
if (ret)
{
free(response);
return ret;
}
for (i = 0; i < cd_total_tracks; i++)
{
if (tracks[i].track_number == 0xAA)
{
if (lba_end)
{
*lba_end = tracks[i].track_start_addr;
}
break;
}
}
if (i == cd_total_tracks)
return ENOTSUP;
free(response);
if (cd_num_tracks > max_tracks)
*num_tracks = max_tracks;
else
*num_tracks = cd_num_tracks;
DPRINTF("Track dump\n");
for (i = 0; i < cd_num_tracks; i++)
{
DPRINTF("LBA:%08X, is_audio=%d\n", td[i].lba, td[i].is_audio);
}
DPRINTF("Last lba: %d\n", (lba_end) ? -7 : *lba_end);
return 0;
}