forked from TnA-Plastic/FreeMcBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcid.c
1368 lines (1020 loc) · 34.5 KB
/
mcid.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/
---------------------------------------------------------------------------
mcid.c
Handles file decryption and file signing
Decrypt_Card_File & Sign_Encrypted_Disk_File uses mcsio2 module, and
XSIO2MAN, XMCMAN, XMCSERV that must be loaded.
main funcs :
int mcsio2_rpc_Init(void); : Inits mcsio2 module rpc (prior to call functions below)
int Card_Auth(int port); : Authentificate a Memory Card by Mechacon
void *Decrypt_Card_File(int port, void *buf); : decrypt an encrypted file for memory card.
void *Decrypt_Disk_File(void *buf); : decrypt an encrypted file for disk or rom.
void *Sign_Encrypted_Disk_File(int port, void *buf);: sign an encrypted (that's the sony trick) file from disk or rom to memory card.
*/
#include <tamtypes.h>
#include <kernel.h>
#include <sifrpc.h>
#include <stdio.h>
#include <libcdvd.h>
#include <string.h>
//#include <sysclib.h>
#include <malloc.h>
int mcsio2_rpc_Init(void);
int Card_Auth(int port);
void *Decrypt_Card_File(int port, void *buf);
void *Decrypt_Disk_File(void *buf);
void *Sign_Encrypted_Disk_File(int port, void *buf);
// Encrypted file Data Block struct
typedef struct {
u32 size; // Size of data block
u32 flags; // Flags : 3 -> block to decrypt, 0 -> block to take in input file, 2 -> block to prepare.
u8 checksum[8];
} Block_Data_t;
// Encrypted file bit table struct
typedef struct {
u32 headersize; // header size (same as mg_ELF_Header_t->mg_header_size)
u8 block_count; // Number of encrypted blocks
u8 pad1;
u8 pad2;
u8 pad3;
Block_Data_t blocks[63]; // Block description, see Block_Data_t
} Bit_Data_t;
// Encrypted file header struct
typedef struct {
u32 unknown1;
u32 unknown2;
u16 unknown3_half;
u16 version;
u32 unknown4;
u32 ELF_size; // Size of data blocks = Decrypted elf size
u16 mg_header_size; // header size
u16 unknown5;
u16 flags; // ? for header purpose
u16 BIT_count; // ?
u32 mg_zones;
} mg_ELF_Header_t;
// sio2 packet structs
struct _sio2_dma_arg { // size 12
void *addr;
int size;
int count;
};
typedef struct {
u32 stat6c;
u32 port_ctrl1[4];
u32 port_ctrl2[4];
u32 stat70;
u32 regdata[16];
u32 stat74;
u32 in_size;
u32 out_size;
u8 *in;
u8 *out;
struct _sio2_dma_arg in_dma;
struct _sio2_dma_arg out_dma;
} Sio2Packet;
// rpc packets structs
typedef struct {
u32 ret;
u16 port;
u32 stat6c;
u32 port_ctrl1[4];
u32 port_ctrl2[4];
u32 stat70;
u32 regdata[16];
u32 stat74;
u32 in_size;
u32 out_size;
u8 in[14];
u8 out[14];
} Rpc_Packet_Send_SIO2;
typedef struct {
int ret;
u16 port;
u16 slot;
u16 a2;
} Rpc_Packet_Send_CARDAUTH;
int mcman_getcnum (int port, int slot);
#define MCSIO2_IRX 0x0E0E0E0
static SifRpcClientData_t mcsio2 __attribute__((aligned(64)));
static int Rpc_Buffer[1024] __attribute__((aligned(64)));
int MCSIO2_Inited = 0;
//--------------------------------------------------------------
int mcsio2BindRpc(void) {
int ret;
int retryCount = 0x1000;
while(retryCount--) {
ret = SifBindRpc( &mcsio2, MCSIO2_IRX, 0);
if ( ret < 0) {
printf("mcsio2: EE Bind RPC Error.\n");
return -1;
}
if (mcsio2.server != 0){
printf("mcsio2: EE Bind RPC Set.\n");
break;
}
// short delay
ret = 0x10000;
while(ret--) asm("nop\nnop\nnop\nnop");
}
MCSIO2_Inited = 1;
return retryCount;
}
//--------------------------------------------------------------
int mcsio2_rpc_Init(void)
{
mcsio2BindRpc();
if(!MCSIO2_Inited)
{
printf("\tmcsio2 RPC Init failed\n");
return -1;
}
return 1;
}
//--------------------------------------------------------------
int mcsio2_transfer(int port, Sio2Packet *sio2data)
{
// Perform sio2data transfer
int i;
Rpc_Packet_Send_SIO2 *Packet = (Rpc_Packet_Send_SIO2 *)Rpc_Buffer;
if(!MCSIO2_Inited) {
if (!mcsio2_rpc_Init())
return -1;
}
Packet->port = port;
Packet->stat6c = sio2data->stat6c;
for(i = 0; i < 4; i++) {
Packet->port_ctrl1[i] = sio2data->port_ctrl1[i];
Packet->port_ctrl2[i] = sio2data->port_ctrl2[i];
}
Packet->stat70 = sio2data->stat70;
for(i = 0; i < 16; i++)
Packet->regdata[i] = sio2data->regdata[i];
Packet->stat74 = sio2data->stat74;
Packet->in_size = sio2data->in_size;
Packet->out_size = sio2data->out_size;
memcpy(Packet->in, sio2data->in, sio2data->in_size);
memcpy(Packet->out, sio2data->out, sio2data->out_size);
if ((SifCallRpc(&mcsio2, 1, 0, (void*)Rpc_Buffer, sizeof(Rpc_Packet_Send_SIO2), (void*)Rpc_Buffer, sizeof(Rpc_Packet_Send_SIO2),0,0)) < 0) {
printf("\tmcsio2_transfer: RPC error\n");
return -1;
}
sio2data->stat6c = Packet->stat6c;
for(i = 0; i < 4; i++) {
sio2data->port_ctrl1[i] = Packet->port_ctrl1[i];
sio2data->port_ctrl2[i] = Packet->port_ctrl2[i];
}
sio2data->stat70 = Packet->stat70;
for(i = 0; i < 16; i++)
sio2data->regdata[i] = Packet->regdata[i];
sio2data->stat74 = Packet->stat74;
sio2data->in_size = Packet->in_size;
sio2data->out_size = Packet->out_size;
memcpy(sio2data->in, Packet->in, Packet->in_size);
memcpy(sio2data->out, Packet->out, Packet->out_size);
return Packet->ret;
}
//--------------------------------------------------------------
int Card_Auth(int port)
{
// Authentificate a Memory Card by Mechacon
Rpc_Packet_Send_CARDAUTH *Packet = (Rpc_Packet_Send_CARDAUTH *)Rpc_Buffer;
if(!MCSIO2_Inited) {
if (!mcsio2_rpc_Init())
return -1;
}
Packet->port = port;
Packet->slot = 0;
Packet->a2 = mcman_getcnum (Packet->port, Packet->slot);
if ((SifCallRpc(&mcsio2, 2, 0, (void*)Rpc_Buffer, sizeof(Rpc_Packet_Send_CARDAUTH), (void*)Rpc_Buffer, sizeof(int),0,0)) < 0) {
printf("\tCard_Auth: RPC error\n");
return -1;
}
return Packet->ret;
}
//_______________________________________________________________________________
int mcman_getcnum (int port, int slot)
{
// Get Memory Card num like MCMAN does
return (port & 1) << 3;
}
//---------------------------------------------------------------------------------------------------------------
int mcsio2_buf_checksum(u8 *buf, int size)
{
// Calculate checksum of a sio2data transfer buf
int i;
int checksum = 0;
if (size <= 0) return checksum & 0xFF;
for (i = 0; i < size; i++) checksum ^= buf[i];
return checksum & 0xFF;
}
//_______________________________________________________________________________
int mcsio2_MagicGate_Cmd_2(int port, int slot, u8 *buf, int a3, int cmd)
{
// Send some MagicGate commands, used to generate mcid
int i;
u8 sio2buf[148];
u8 wrbuf[14];
u8 rdbuf[14];
Sio2Packet *sio2packet = (Sio2Packet *) sio2buf;
sio2packet->in = (u8 *)&wrbuf;
sio2packet->out = (u8 *)&rdbuf;
for (i=0; i < 4; i++) {
sio2packet->port_ctrl1[i] = 0;
sio2packet->port_ctrl2[i] = 0;
}
sio2packet->port_ctrl1[port] = 0xFF060505;
sio2packet->port_ctrl2[port] = 0x0003FFFF;
sio2packet->regdata[0] = (port & 3) | 0x00380E40;
sio2packet->regdata[1] = 0;
sio2packet->out_size = 14;
sio2packet->in_size = 14;
memset(wrbuf, 0, 14);
wrbuf[0] = 0x81;
wrbuf[1] = (u8)a3;
wrbuf[2] = (u8)cmd;
for (i = 0; i < 8; i++) wrbuf[3 + i] = buf[7 - i];
wrbuf[11] = (u8)mcsio2_buf_checksum(buf, 8);
if (!(mcsio2_transfer(port, (Sio2Packet *)sio2packet))) {
printf("mcsio2 transfer error %d\n", cmd & 0xFF);
return 0;
}
if (((sio2packet->stat6c >> 13) & 1) || ((sio2packet->stat6c >> 14) & 3)) {
printf("sio2 command error %d\n", cmd & 0xFF);
return 0;
}
if (rdbuf[12] != 0x2B) {
printf("ID error %d\n", cmd & 0xFF);
return 0;
}
if (rdbuf[13] == 0x66) {
printf("result failed %d\n", cmd & 0xFF);
return 0;
}
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int mcsio2_MagicGate_Cmd_1(int port, int slot, u8 *buf, int a3, int cmd)
{
// Send some MagicGate commands, used to generate mcid
int i;
u8 sio2buf[148];
u8 wrbuf[14];
u8 rdbuf[14];
Sio2Packet *sio2packet = (Sio2Packet *) sio2buf;
sio2packet->in = (u8 *)&wrbuf;
sio2packet->out = (u8 *)&rdbuf;
for (i = 0; i < 4; i++) {
sio2packet->port_ctrl1[i] = 0;
sio2packet->port_ctrl2[i] = 0;
}
sio2packet->port_ctrl1[port] = 0xFF060505;
sio2packet->port_ctrl2[port] = 0x0003FFFF;
sio2packet->regdata[0] = (port & 3) | 0x00380E40;
sio2packet->regdata[1] = 0;
sio2packet->out_size = 14;
sio2packet->in_size = 14;
memset(wrbuf, 0, 14);
wrbuf[0] = 0x81;
wrbuf[1] = (u8)a3;
wrbuf[2] = (u8)cmd;
if (!(mcsio2_transfer(port, (Sio2Packet *)sio2packet))) {
printf("mcsio2 transfer error %d\n", cmd & 0xFF);
return 0;
}
if (((sio2packet->stat6c >> 13) & 1) || ((sio2packet->stat6c >> 14) & 3)) {
printf("sio2 command error %d\n", cmd & 0xFF);
return 0;
}
if (rdbuf[3] != 0x2B) {
printf("ID error %d\n", cmd & 0xFF);
return 0;
}
if (rdbuf[13] == 0x66) {
printf("result failed %d\n", cmd & 0xFF);
return 0;
}
if (((u8)mcsio2_buf_checksum(rdbuf + 4, 8) & 0xFF) != rdbuf[12]) {
printf("mcsio2 checksum error %d\n", cmd & 0xFF);
return 0;
}
for (i = 0; i < 8; i++)
buf[7 - i] = rdbuf[4 + i];
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int mcsio2_MagicGate_Cmd_0(int port, int slot, int a2, int cmd)
{
// Send some MagicGate commands, used to generate mcid
int i;
u8 sio2buf[148];
u8 wrbuf[5];
u8 rdbuf[5];
Sio2Packet *sio2packet = (Sio2Packet *) sio2buf;
sio2packet->in = (u8 *)&wrbuf;
sio2packet->out = (u8 *)&rdbuf;
for (i=0; i < 4; i++) {
sio2packet->port_ctrl1[i] = 0;
sio2packet->port_ctrl2[i] = 0;
}
sio2packet->port_ctrl1[port] = 0xFF060505;
sio2packet->port_ctrl2[port] = 0x0003FFFF;
sio2packet->regdata[0] = (port & 3) | 0x00140540;
sio2packet->regdata[1] = 0;
sio2packet->out_size = 5;
sio2packet->in_size = 5;
memset(wrbuf, 0, 5);
wrbuf[0] = 0x81;
wrbuf[1] = (u8)a2;
wrbuf[2] = (u8)cmd;
if (!(mcsio2_transfer(port, (Sio2Packet *)sio2packet))) {
printf("mcsio2 transfer error %d\n", cmd & 0xFF);
return 0;
}
if (((sio2packet->stat6c >> 13) & 1) || ((sio2packet->stat6c >> 14) & 3)) {
printf("sio2 command error %d\n", cmd & 0xFF);
return 0;
}
if (rdbuf[3] != 0x2B) {
printf("ID error %d\n", cmd & 0xFF);
return 0;
}
if (rdbuf[4] == 0x66) {
printf("result failed %d\n", cmd & 0xFF);
return 0;
}
return 1;
}
//_______________________________________________________________________________
int mecha_command(int cmd, u8 *argp, int arglen, u8 *buf)
{
// Send S-command to the Mechacon
return cdApplySCmd(cmd, argp, arglen, buf, 16);
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_write_data(void *data, int size)
{
// Send some datas to the Mechacon
u8 ret[1];
u8 mecha_param[16];
if (size > 16)
return 0;
if (size > 0)
memcpy(mecha_param, data, size);
if (!(mecha_command(0x8D, mecha_param, size, ret)))
return 0;
if (!((int)ret[0]))
return 1;
return 0;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_read_data(void *data, int size)
{
// Get back datas from Mechacon
u8 ret[16];
if (size > 16)
return 0;
if (!(mecha_command(0x8E, NULL, 0, ret)))
return 0;
if (size <= 0)
return 1;
memcpy(data, ret, size);
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_pol_cal_cmplt(void)
{
// Send the command that tells to Mechacon to decrypt datas received
u8 ret[1];
do {
if (!(mecha_command(0x8F, NULL, 0, ret)))
return 0;
}
while ((int)ret[0]);
if (!((int)ret[0]))
return 1;
return 0;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_ReadKbit1(void* kbit_part1)
{
// Send the Mechacon command that generate first half (8 bytes) of kbit
u8 ret[9];
if (!(mecha_command(0x94, NULL, 0, ret)))
return 0;
if ((int)ret[0]!= 0)
return 0;
memcpy(kbit_part1, ret + 1, 8);
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_ReadKbit2(void* kbit_part2)
{
// Send the Mechacon command that generate second half (8 bytes) of kbit
u8 ret[9];
if (!(mecha_command(0x95, NULL, 0, ret)))
return 0;
if ((int)ret[0]!= 0)
return 0;
memcpy(kbit_part2, ret + 1, 8);
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_ReadKc1(void* kc_part1)
{
// Send the Mechacon command that generate first half (8 bytes) of kc
u8 ret[9];
if (!(mecha_command(0x96, NULL, 0, ret)))
return 0;
if (((int)ret[0])!= 0)
return 0;
memcpy(kc_part1, ret + 1, 8);
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_ReadKc2(void* kc_part2)
{
// Send the Mechacon command that generate second half (8 bytes) of kc
u8 ret[9];
if (!(mecha_command(0x97, NULL, 0, ret)))
return 0;
if (((int)ret[0])!= 0)
return 0;
memcpy(kc_part2, ret + 1, 8);
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_ReadICVPS2(void* icvps2)
{
// Send the Mechacon command that generate ICVPS2 (8 bytes), this is never used in encrypted we knows...
u8 ret[9];
if (!(mecha_command(0x98, NULL, 0, ret)))
return 0;
if (((int)ret[0])!= 0)
return 0;
memcpy(icvps2, ret + 1, 8);
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_write_HD_start(int hcode, int cnum, int a2, int hlength)
{
// Send Mechacon command that inits it to receive encrypted file header
u8 ret[1];
u8 param_buf[5];
param_buf[0] = (u8)hcode;
param_buf[1] = (u8)hlength;
param_buf[2] = (u8)(hlength>>8);
param_buf[3] = (u8)cnum;
param_buf[4] = (u8)a2;
if (!(mecha_command(0x90, param_buf, 5, ret)))
return 0;
if (!((int)ret[0]))
return 1;
return 0;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_ReadBITLength(int *bit_size)
{
// Send Mechacon command that inits it to get back decrypted BIT table
u8 ret[3];
if (!(mecha_command(0x91, NULL, 0, ret)))
return 0;
if (((int)ret[0])!= 0)
return 0;
*bit_size = (int)ret[1] + (int)(ret[2]<<8);
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_WriteDataInLength(int size)
{
// Send Mechacon command that inits it to receive some datas
u8 ret[1];
u8 param_buf[2];
param_buf[0] = (u8)size;
param_buf[1] = (u8)(size>>8);
if (!(mecha_command(0x92, param_buf, 2, ret)))
return 0;
if (!((int)ret[0]))
return 1;
return 0;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_WriteDataOutLength(int size)
{
// Send Mechacon command that inits it to get back some datas
u8 ret[1];
u8 param_buf[2];
param_buf[0] = (u8)size;
param_buf[1] = (u8)(size>>8);
if (!(mecha_command(0x93, param_buf, 2, ret)))
return 0;
if (!((int)ret[0]))
return 1;
return 0;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_ReadBIT(void* bit)
{
// Get back full decrypted BIT table from Mechacon
int offset;
int bit_length, bitlength;
int *p_bitlength = &bit_length;
if (!(Mecha_ReadBITLength(p_bitlength))) // send mecha command 0x91
return 0;
bitlength = bit_length;
if ((bit_length) > 0) {
offset = 0;
do {
if ((bit_length & 0xFFFF) < 16) {
if (!(Mecha_read_data(bit + offset, bit_length & 0xFFFF))) // send mecha command 0x8E
return 0;
bit_length = 0;
}
else {
if (!(Mecha_read_data(bit + offset, 16))) // send mecha command 0x8E
return 0;
offset += 16;
bit_length -= 16;
}
} while ((bit_length & 0xFFFF) > 0);
}
return bitlength;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_ReadBLOCK(void* buf, int size)
{
// Get back full decrypted data block from Mechacon
int offset;
if (!(Mecha_WriteDataOutLength(size & 0xFFFF))) // send mecha command 0x93
return 0;
if (size <= 0)
return 1;
offset = 0;
do {
if (size < 16) {
if (!(Mecha_read_data(buf + offset, size))) // send mecha command 0x8E
return 0;
size = 0;
}
else {
if (!(Mecha_read_data(buf + offset, 16))) // send mecha command 0x8E
return 0;
offset += 16;
size -= 16;
}
} while (size > 0);
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_Set_Header(int hcode, int cnum, int a2, void* buf)
{
// Send encrypted file header to Mechacon, and send command to decrypt it
int hlength, offset;
int r;
hlength = ((mg_ELF_Header_t *) buf)->mg_header_size;
printf("header length %d\n", hlength & 0xFFFF);
if (!Mecha_write_HD_start(hcode & 0x00FF, cnum, a2, hlength & 0xFFFF)) { // send mecha command 0x90
printf("Mecha_set_header: fail Mecha_write_HD_start\n");
return 0;
}
if ((hlength & 0xFFFF) > 0) { // assume 0 < hlength < 65536
offset = 0;
do {
if ((hlength & 0xFFFF) < 16) {
r = Mecha_write_data(buf + offset, hlength & 0xFFFF); // send mecha command 0x8D
hlength = 0;
}
else {
r = Mecha_write_data(buf + offset, 16); // send mecha command 0x8D
offset += 16;
hlength -= 16;
}
if (r == 0) {
printf("Mecha_set_header: fail write_data\n");
return 0;
}
} while ((hlength & 0xFFFF) > 0);
}
if (!(Mecha_pol_cal_cmplt())) { // send mecha command 0x8F
printf("Mecha_set_header: fail pol_cal_cmplt\n");
return 0;
}
printf("Mecha_set_header: OK\n");
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int Mecha_Set_Block(void *buf, int size)
{
// Send encrypted file data block to Mechacon, and send command to decrypt it
int offset, r;
if (!(Mecha_WriteDataInLength(size & 0xFFFF))) // send mecha command 0x92
return 0;
if (size > 0) {
offset = 0;
do {
if (size < 16) {
r = Mecha_write_data(buf + offset, size); // send mecha command 0x8D
size = 0;
}
else {
r = Mecha_write_data(buf + offset, 16); // send mecha command 0x8D
offset += 16;
size -= 16;
}
if (r == 0)
return 0;
} while (size > 0);
}
if (!(Mecha_pol_cal_cmplt())) // send mecha command 0x8F
return 0;
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int GetKbit(void *kbit)
{
// Get the 16 bytes kbit from Mechacon
if (!(Mecha_ReadKbit1(kbit)))
return 0;
if (!(Mecha_ReadKbit2(kbit + 8)))
return 0;
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int GetKc(void *kc)
{
// Get the 16 bytes kc from Mechacon
if (!(Mecha_ReadKc1(kc)))
return 0;
if (!(Mecha_ReadKc2(kc + 8)))
return 0;
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int GetICVPS2(void *icvps2)
{
// Get the 8 bytes icvps2 from Mechacon
if (!(Mecha_ReadICVPS2(icvps2)))
return 0;
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int Card_Encrypt(int port, int slot, void *buf)
{
// Perform the MagicGate encryption of a 8 bytes data buffer
if (!(mcsio2_MagicGate_Cmd_0(port, slot, 0xF2, 0x50)))
return 0;
if (!(mcsio2_MagicGate_Cmd_2(port, slot, buf, 0xF2, 0x51)))
return 0;
if (!(mcsio2_MagicGate_Cmd_0(port, slot, 0xF2, 0x52)))
return 0;
if (!(mcsio2_MagicGate_Cmd_1(port, slot, buf, 0xF2, 0x53)))
return 0;
return 1;
}
//---------------------------------------------------------------------------------------------------------------
int Card_Decrypt(int port, int slot, void *buf)
{
// Perform the MagicGate decryption of a 8 bytes data buffer
if (!(mcsio2_MagicGate_Cmd_0(port, slot, 0xF1, 0x40)))
return 0;
if (!(mcsio2_MagicGate_Cmd_2(port, slot, buf, 0xF1, 0x41)))
return 0;
if (!(mcsio2_MagicGate_Cmd_0(port, slot, 0xF1, 0x42)))
return 0;
if (!(mcsio2_MagicGate_Cmd_1(port, slot, buf, 0xF1, 0x43)))
return 0;
return 1;
}
//---------------------------------------------------------------------------------------------------------------
void store_kbit(u8 *inbuf, u8 *kbit)
{
// Stores bit Key at right offset in a encrypted Header
mg_ELF_Header_t *header = (mg_ELF_Header_t *) inbuf;
int offset = 0x20;
if (header->BIT_count > 0)
offset += header->BIT_count * 16;
if ((*(u32*)&header->flags) & 1)
offset += inbuf[offset] + 1;
if (((*(u32*)&header->flags) & 0xf000) == 0)
offset += 8;
memcpy(inbuf + offset, kbit, 16);
}
//--------------------------------------------------------------
void store_kc(u8 *inbuf, u8 *kc)
{
// Stores content Key at right offset in a encrypted Header
mg_ELF_Header_t *header = (mg_ELF_Header_t *) inbuf;
int offset = 0x20;
if (header->BIT_count > 0)
offset += header->BIT_count * 16;
if ((*(u32*)&header->flags) & 1)
offset += inbuf[offset] + 1;
if (((*(u32*)&header->flags) & 0xf000) == 0)
offset += 8;
offset += 16;
memcpy(inbuf + offset, kc, 16);
}
//--------------------------------------------------------------
void store_icvps2(u8 *inbuf, u8 *icvps2)
{
// Stores ICVPS2 at right offset in a encrypted Header
mg_ELF_Header_t *header = (mg_ELF_Header_t *) inbuf;
int offset = header->mg_header_size - 8;
memcpy(inbuf + offset, icvps2, 8);
}
//--------------------------------------------------------------
void read_kbit(u8 *inbuf, u8 *kbit)
{
// Get kbit from right offset in a encrypted Header
int offset = 0x20;
mg_ELF_Header_t *header = (mg_ELF_Header_t *) inbuf;
if (header->BIT_count > 0)
offset += header->BIT_count * 16;
if ((*(u32*)&header->flags) & 1)
offset += inbuf[offset] + 1;