lin
2025-01-10 2e0fe69425adee0529756dc3381ac1838197f3ac
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
/*
 * (C) Copyright 2018 allwinnertech  <wangwei@allwinnertech.com>
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
 
#include "usb_base.h"
#include <asm/arch/timer.h>
#include "usb_burn.h"
#include "usb_efex.h"
#include <smc.h>
#include <securestorage.h>
#include <sunxi_board.h>
#include <sys_partition.h>
#include <sunxi_keybox.h>
DECLARE_GLOBAL_DATA_PTR;
volatile int sunxi_usb_burn_from_boot_handshake, sunxi_usb_burn_from_boot_init,
   sunxi_usb_burn_from_boot_setup, sunxi_auto_fel_from_boot_handshake;
volatile int sunxi_usb_probe_update_action;
volatile int sunxi_auto_fel_runnning;
 
__attribute__((weak))
int sunxi_key_ladder_verify_rotpk_hash(void *input_hash_buf, int len)
{
   printf("call weak fun: %s\n", __func__);
   return -1;
}
 
__attribute__((weak))
int smc_set_sst_crypt_name(char *name)
{
   printf("call weak fun: %s\n",__func__);
   return 0;
}
 
__attribute__((weak))
int sunxi_verify_rotpk_hash(void *input_hash_buf, int len)
{
   printf("call weak fun: %s\n",__func__);
   return -1;
}
 
__attribute__((weak))
int sunxi_deal_hdcp_key(char *keydata, int keylen)
{
   printf("call weak fun: %s\n",__func__);
   return 0;
}
 
__attribute__((weak))
int sunxi_secure_object_down( const char *name , char *buf, int len, int encrypt, int write_protect)
 
{
   printf("call weak fun: %s\n",__func__);
   return 0;
}
 
__attribute__((weak))
int sunxi_efuse_write(void *key_buf)
{
   printf("call weak fun: %s\n",__func__);
   return -1;
}
 
__attribute__((weak))
int sunxi_efuse_read(void *key_name, void *read_buf,int *len)
{
   printf("call weak fun: %s\n",__func__);
   return -1;
}
 
static  int sunxi_usb_pburn_write_enable = 0;
#if defined(SUNXI_USB_30)
static  int sunxi_usb_pburn_status_enable = 1;
#endif
static  int sunxi_usb_pburn_status = SUNXI_USB_PBURN_IDLE;
 
static  pburn_trans_set_t  trans_data;
#define DATA_BUFFER_MAX_SIZE                    (1*1024*1024)
 
static  u8 *private_data_ext_buff_step = NULL;
static u8 *private_data_ext_buff = NULL;
 
long long burn_part_bytes = 0;
extern volatile int sunxi_usb_burn_from_boot_handshake;
extern volatile int sunxi_usb_burn_from_boot_setup;
extern int sunxi_get_securemode(void);
#ifdef CONFIG_BURN_NORMAL_EFUSE
extern int sunxi_efuse_write(void *key_buf);
extern int  sunxi_efuse_read(void *key_name, void *read_buf,int *len);
#endif
 
__attribute__((weak)) int sunxi_keybox_has_key(char *key_name)
{
   printf("call weak fun: %s\n", __func__);
   return 0;
}
 
void set_usb_burn_boot_init_flag(int flag )
{
   sunxi_usb_burn_from_boot_init = flag;
}
 
static int hex2char(void *dst, void *src,int len)
{
   int i;
   char *p_out = dst ,*p_in = src;
 
   if ((src ==NULL)||(dst == NULL))
       return -1;
 
   memset(dst,0x00,len*2);
   for(i=0;i<len;i++)
   {
       sprintf(p_out,"%02x",p_in[i]);
       p_out += 2;
   }
 
   return 0;
}
 
/*
*******************************************************************************
*                     do_usb_req_set_interface
*
* Description:
*    void
*
* Parameters:
*    void
*
* Return value:
*    void
*
* note:
*    void
*
*******************************************************************************
*/
static int __usb_set_interface(struct usb_device_request *req)
{
   sunxi_usb_dbg("set interface\n");
   /* Only support interface 0, alternate 0 */
   if((0 == req->wIndex) && (0 == req->wValue))
   {
       sunxi_udc_ep_reset();
   }
   else
   {
       printf("err: invalid wIndex and wValue, (0, %d), (0, %d)\n", req->wIndex, req->wValue);
       return SUNXI_USB_REQ_OP_ERR;
   }
 
   return SUNXI_USB_REQ_SUCCESSED;
}
 
/*
*******************************************************************************
*                     do_usb_req_set_address
*
* Description:
*    void
*
* Parameters:
*    void
*
* Return value:
*    void
*
* note:
*    void
*
*******************************************************************************
*/
static int __usb_set_address(struct usb_device_request *req)
{
   uchar address;
 
   address = req->wValue & 0x7f;
   printf("set address 0x%x\n", address);
 
   sunxi_udc_set_address(address);
 
   return SUNXI_USB_REQ_SUCCESSED;
}
 
/*
*******************************************************************************
*                     do_usb_req_set_configuration
*
* Description:
*    void
*
* Parameters:
*    void
*
* Return value:
*    void
*
* note:
*    void
*
*******************************************************************************
*/
static int __usb_set_configuration(struct usb_device_request *req)
{
   sunxi_usb_dbg("set configuration\n");
   /* Only support 1 configuration so nak anything else */
   if (1 == req->wValue)
   {
       sunxi_udc_ep_reset();
   }
   else
   {
       printf("err: invalid wValue, (0, %d)\n", req->wValue);
 
       return SUNXI_USB_REQ_OP_ERR;
   }
 
   sunxi_udc_set_configuration(req->wValue);
 
   return SUNXI_USB_REQ_SUCCESSED;
}
/*
*******************************************************************************
*                     do_usb_req_get_descriptor
*
* Description:
*    void
*
* Parameters:
*    void
*
* Return value:
*    void
*
* note:
*    void
*
*******************************************************************************
*/
static int __usb_get_descriptor(struct usb_device_request *req, uchar *buffer)
{
   int ret = SUNXI_USB_REQ_SUCCESSED;
 
   //获取描述符
   switch(req->wValue >> 8)
   {
       case USB_DT_DEVICE:        //设备描述符
       {
           struct usb_device_descriptor *dev_dscrptr;
 
           sunxi_usb_dbg("get device descriptor\n");
 
           dev_dscrptr = (struct usb_device_descriptor *)buffer;
           memset((void *)dev_dscrptr, 0, sizeof(struct usb_device_descriptor));
 
           dev_dscrptr->bLength = MIN(req->wLength, sizeof (struct usb_device_descriptor));
           dev_dscrptr->bDescriptorType    = USB_DT_DEVICE;
#ifdef CONFIG_USB_1_1_DEVICE
           dev_dscrptr->bcdUSB             = 0x110;
#else
           dev_dscrptr->bcdUSB             = 0x200;
#endif
           dev_dscrptr->bDeviceClass       = 0;
           dev_dscrptr->bDeviceSubClass    = 0;
           dev_dscrptr->bDeviceProtocol    = 0;
           dev_dscrptr->bMaxPacketSize0    = 0x40;
           dev_dscrptr->idVendor           = DRIVER_VENDOR_ID;
           dev_dscrptr->idProduct          = DRIVER_PRODUCT_ID;
           dev_dscrptr->bcdDevice          = 0xffff;
           //ignored
           //dev_dscrptr->iManufacturer      = SUNXI_USB_STRING_IMANUFACTURER;
           //dev_dscrptr->iProduct           = SUNXI_USB_STRING_IPRODUCT;
           //dev_dscrptr->iSerialNumber      = SUNXI_USB_STRING_ISERIALNUMBER;
           dev_dscrptr->iManufacturer      = 0;
           dev_dscrptr->iProduct           = 0;
           dev_dscrptr->iSerialNumber      = 0;
           dev_dscrptr->bNumConfigurations = 1;
 
           sunxi_udc_send_setup(dev_dscrptr->bLength, buffer);
       }
       break;
 
       case USB_DT_CONFIG:        //配置描述符
       {
           struct usb_configuration_descriptor *config_dscrptr;
           struct usb_interface_descriptor     *inter_dscrptr;
           struct usb_endpoint_descriptor         *ep_in, *ep_out;
           unsigned char bytes_remaining = req->wLength;
           unsigned char bytes_total = 0;
 
           sunxi_usb_dbg("get config descriptor\n");
 
           bytes_total = sizeof (struct usb_configuration_descriptor) + \
                         sizeof (struct usb_interface_descriptor)        + \
                         sizeof (struct usb_endpoint_descriptor)        + \
                         sizeof (struct usb_endpoint_descriptor);
 
           memset(buffer, 0, bytes_total);
 
           config_dscrptr = (struct usb_configuration_descriptor *)(buffer + 0);
           inter_dscrptr  = (struct usb_interface_descriptor       *)(buffer +                         \
                                                                    sizeof(struct usb_configuration_descriptor));
           ep_in            = (struct usb_endpoint_descriptor       *)(buffer +                         \
                                                                    sizeof(struct usb_configuration_descriptor) +     \
                                                                    sizeof(struct usb_interface_descriptor));
           ep_out            = (struct usb_endpoint_descriptor       *)(buffer +                         \
                                                                    sizeof(struct usb_configuration_descriptor) +     \
                                                                    sizeof(struct usb_interface_descriptor)     +    \
                                                                    sizeof(struct usb_endpoint_descriptor));
 
           /* configuration */
           config_dscrptr->bLength                = MIN(bytes_remaining, sizeof (struct usb_configuration_descriptor));
           config_dscrptr->bDescriptorType        = USB_DT_CONFIG;
           config_dscrptr->wTotalLength         = bytes_total;
           config_dscrptr->bNumInterfaces         = 1;
           config_dscrptr->bConfigurationValue    = 1;
           config_dscrptr->iConfiguration         = 0;
           config_dscrptr->bmAttributes           = 0x80;        //not self powered
           config_dscrptr->bMaxPower              = 0xFA;        //最大电流500ms(0xfa * 2)
 
           bytes_remaining                    -= config_dscrptr->bLength;
           /* interface */
           inter_dscrptr->bLength             = MIN (bytes_remaining, sizeof(struct usb_interface_descriptor));
           inter_dscrptr->bDescriptorType     = USB_DT_INTERFACE;
           inter_dscrptr->bInterfaceNumber    = 0x00;
           inter_dscrptr->bAlternateSetting   = 0x00;
           inter_dscrptr->bNumEndpoints       = 0x02;
           inter_dscrptr->bInterfaceClass     = 0xff;
           inter_dscrptr->bInterfaceSubClass  = 0xff;
           inter_dscrptr->bInterfaceProtocol  = 0xff;
           inter_dscrptr->iInterface          = 0;
 
           bytes_remaining                   -= inter_dscrptr->bLength;
           /* ep_in */
           ep_in->bLength            = MIN (bytes_remaining, sizeof (struct usb_endpoint_descriptor));
           ep_in->bDescriptorType    = USB_DT_ENDPOINT;
           ep_in->bEndpointAddress   = sunxi_udc_get_ep_in_type(); /* IN */
           ep_in->bmAttributes       = USB_ENDPOINT_XFER_BULK;
           ep_in->wMaxPacketSize       = sunxi_udc_get_ep_max();
           ep_in->bInterval          = 0x00;
 
           bytes_remaining          -= ep_in->bLength;
           /* ep_out */
           ep_out->bLength            = MIN (bytes_remaining, sizeof (struct usb_endpoint_descriptor));
           ep_out->bDescriptorType    = USB_DT_ENDPOINT;
           ep_out->bEndpointAddress   = sunxi_udc_get_ep_out_type(); /* OUT */
           ep_out->bmAttributes       = USB_ENDPOINT_XFER_BULK;
           ep_out->wMaxPacketSize        = sunxi_udc_get_ep_max();
           ep_out->bInterval          = 0x00;
 
           bytes_remaining           -= ep_out->bLength;
 
           sunxi_udc_send_setup(MIN(req->wLength, bytes_total), buffer);
       }
       break;
 
       case USB_DT_STRING:
       {
           unsigned char bLength = 0;
           unsigned char string_index = req->wValue & 0xff;
 
           sunxi_usb_dbg("get string descriptor\n");
 
           /* Language ID */
           if(string_index == 0)
           {
               bLength = MIN(4, req->wLength);
 
               buffer[0] = bLength;
               buffer[1] = USB_DT_STRING;
               buffer[2] = 9;
               buffer[3] = 4;
               sunxi_udc_send_setup(bLength, (void *)buffer);
           }
           else
           {
               printf("sunxi usb err: string line %d is not supported\n", string_index);
           }
       }
       break;
 
       case USB_DT_DEVICE_QUALIFIER:
       {
#ifdef CONFIG_USB_1_1_DEVICE
           /* This is an invalid request for usb 1.1, nak it */
           USBC_Dev_EpSendStall(sunxi_udc_source.usbc_hd, USBC_EP_TYPE_EP0);
#else
           struct usb_qualifier_descriptor *qua_dscrpt;
 
           sunxi_usb_dbg("get qualifier descriptor\n");
 
           qua_dscrpt = (struct usb_qualifier_descriptor *)buffer;
           memset(&buffer, 0, sizeof(struct usb_qualifier_descriptor));
 
           qua_dscrpt->bLength = MIN(req->wLength, sizeof(struct usb_qualifier_descriptor));
           qua_dscrpt->bDescriptorType    = USB_DT_DEVICE_QUALIFIER;
           qua_dscrpt->bcdUSB             = 0x200;
           qua_dscrpt->bDeviceClass       = 0xff;
           qua_dscrpt->bDeviceSubClass    = 0xff;
           qua_dscrpt->bDeviceProtocol    = 0xff;
           qua_dscrpt->bMaxPacketSize0    = 0x40;
           qua_dscrpt->bNumConfigurations = 1;
           qua_dscrpt->breserved          = 0;
 
           sunxi_udc_send_setup(qua_dscrpt->bLength, buffer);
#endif
       }
       break;
 
       default:
           printf("err: unkown wValue(%d)\n", req->wValue);
 
           ret = SUNXI_USB_REQ_OP_ERR;
   }
 
   return ret;
}
 
/*
*******************************************************************************
*                     do_usb_req_get_status
*
* Description:
*    void
*
* Parameters:
*    void
*
* Return value:
*    void
*
* note:
*    void
*
*******************************************************************************
*/
static int __usb_get_status(struct usb_device_request *req, uchar *buffer)
{
   unsigned char bLength = 0;
 
   sunxi_usb_dbg("get status\n");
   if(0 == req->wLength)
   {
       /* sent zero packet */
       sunxi_udc_send_setup(0, NULL);
 
       return SUNXI_USB_REQ_OP_ERR;
   }
 
   bLength = MIN(req->wValue, 2);
 
   buffer[0] = 1;
   buffer[1] = 0;
 
   sunxi_udc_send_setup(bLength, buffer);
 
   return SUNXI_USB_REQ_SUCCESSED;
}
/*
************************************************************************************************************
*
*                                             function
*
*    name          :
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
static int __sunxi_pburn_send_status(void *buffer, unsigned int buffer_size)
{
   return sunxi_udc_send_data((uchar *)buffer, buffer_size);
}
/*
************************************************************************************************************
*
*                                             function
*
*    name          :
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
int __sunxi_burn_key(u8 *buff, uint buff_len)
{
   sunxi_usb_burn_main_info_t *key_main = (sunxi_usb_burn_main_info_t *)buff;
   sunxi_usb_burn_key_info_t  *key_list;
   __attribute__((unused)) int ret;
   sunxi_efuse_key_info_t    efuse_key_info;
 
   int key_count;
   int offset;
   u8  *p_buff = (u8 *)&key_main->key_info;
   //比较key主体的信息
   if(strcmp((const char *)key_main->magic, "key-group-db"))
   {
       printf("key data magic unmatch, err\n");
 
       return -1;
   }
   key_count = key_main->count;
   printf("key_count=%d\n", key_count);
#ifdef CONFIG_SUNXI_SECURE_STORAGE
   if(sunxi_secure_storage_init())
   {
       printf("%s secure storage init failed\n", __func__);
 
       return -1;
   }
#endif
   for(;key_count>0;key_count--, key_list++)
   {
       key_list = (sunxi_usb_burn_key_info_t *)p_buff;
       printf("^^^^^^^^^^^^^^^^^^^\n");
       printf("key index=%d\n", key_main->count - key_count);
       printf("key name=%s\n", key_list->name);
       printf("key type=%d\n", key_list->type);
       printf("key len=%d\n", key_list->len);
       printf("key if_burn=%d\n", key_list->if_burn);
       printf("key if_replace=%d\n", key_list->if_replace);
       printf("key if_crypt=%d\n", key_list->if_crypt);
       printf("key data:\n");
       sunxi_dump(key_list->key_data, key_list->len);
       printf("###################\n");
       offset = (sizeof(sunxi_usb_burn_key_info_t)) + ((key_list->len + 15) & (~15));
       printf("offset=%d\n", offset);
       p_buff += offset;
 
       if(!key_list->type)
       {
           memset(&efuse_key_info, 0, sizeof(efuse_key_info));
           strcpy(efuse_key_info.name, (const char *)key_list->name);
           efuse_key_info.len = key_list->len;
           efuse_key_info.key_data = (u8 *)key_list->key_data;
 
           if(!strcmp(efuse_key_info.name, "rotpk"))
           {
               printf("ready to burn rootkey\n");
               #ifdef CONFIG_SUNXI_KEY_LADDER
               if (sunxi_key_ladder_verify_rotpk_hash(key_list->key_data, key_list->len)) {
                   printf("verity the rootkey failed, stop burn it\n");
                   return -1;
               }
               #else
               if(sunxi_verify_rotpk_hash(key_list->key_data, key_list->len))
               {
                   printf("verity the rootkey failed, stop burn it\n");
 
                   return -1;
               }
               #endif
#ifdef CONFIG_SUNXI_CHECK_CUSTOMER_RESERVED_ID
           } else if (!strncmp(key_list->name, "customer_reserved", sizeof("customer_reserved"))) {
               u32 sunxi_handle_customer_reserved_id(u32 soft_protect_id);
               u32 customer_reserved_id;
               u8 *key_buf = (u8 *)key_list->key_data;
               customer_reserved_id = (key_buf[0] << 8) | key_buf[1];
               customer_reserved_id = sunxi_handle_customer_reserved_id(customer_reserved_id);
               key_buf[0] = 0;
               key_buf[1] = 0;
               key_buf[2] = customer_reserved_id & 0xff;
               key_buf[3] = (customer_reserved_id >> 8) & 0xff;
               efuse_key_info.len = key_list->len = 4;
#endif
           }
 
           if ((sunxi_get_securemode() == SUNXI_SECURE_MODE_WITH_SECUREOS) || (sunxi_probe_secure_monitor()))
           {
               ret = arm_svc_efuse_write(&efuse_key_info);
               if (ret) {
                   printf("sunxi_efuse_write failed with:%d\n",
                          ret);
                   return -1;
               }
           } else {
               if (sunxi_efuse_write(&efuse_key_info)) {
                   return -1;
               }
           }
       }
#ifdef CONFIG_SUNXI_SECURE_STORAGE
       else
       {
           if(!strcmp("hdcpkey", key_list->name))
           {
               ret = sunxi_deal_hdcp_key((char *)key_list->key_data, key_list->len);
               if(ret)
               {
                   printf("sunxi deal with hdcp key failed\n");
                   return -1;
               }
           }
#ifdef CONFIG_SUNXI_KEYBOX
           else if (sunxi_keybox_has_key(key_list->name)) {
               ret = sunxi_keybox_burn_key(
                   key_list->name,
                   (char *)key_list->key_data,
                   key_list->len, key_list->if_crypt,
                   key_list->if_replace);
               printf("sunxi deal with %s key resutl:%d\n",
                      key_list->name, ret);
               if (ret) {
                   return -1;
               }
           }
#endif
           else {
               sunxi_secure_object_set(key_list->name,
                                       key_list->if_crypt,
                                       key_list->if_replace,
                                       0,0,0 ); /*set secure storage feature*/
 
               printf("write key to secure storage\n");
               ret = sunxi_secure_object_write(key_list->name, (char *)key_list->key_data, key_list->len);
               if(ret)
               {
                   printf("write key to secure storage failed\n");
                   return -1;
               }
 
           }
       }
#endif
   }
#ifdef    CONFIG_SUNXI_SECURE_STORAGE
   if(sunxi_secure_storage_exit())
   {
       printf("sunxi_secure_storage_exit err\n");
 
       return -1;
   }
#endif
   return 0;
}
 
/*
************************************************************************************************************
*
*                                             function
*
*    name          :    __sunxi_read_key_by_name
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
int __sunxi_read_key_by_name(void *buffer, uint buff_len, int *read_len)
{
   sunxi_usb_burn_key_info_t *key_info = NULL;
 
   __attribute__((unused)) int ret = -1;
   __attribute__((unused)) int key_data_len = 0;
   char data_buff[4096]={0};
 
   if(buffer == NULL)
   {
       printf("the memory input or output is NULL\n");
       return -1;
   }
#ifdef CONFIG_SUNXI_SECURE_STORAGE
   if(sunxi_secure_storage_init())
   {
       printf("%s secure storage init failed\n", __func__);
       return -1;
   }
#endif
   key_info = (sunxi_usb_burn_key_info_t *)buffer;
 
   printf("key name=%s\n", key_info->name);
   printf("key type=%d\n", key_info->type);
   printf("key len=%d\n", key_info->len);
   printf("key if_burn=%d\n", key_info->if_burn);
   printf("key if_replace=%d\n", key_info->if_replace);
   printf("key if_crypt=%d\n", key_info->if_crypt);
 
   *read_len = sizeof(sunxi_usb_burn_key_info_t);
 
   if(!key_info->type)
   {
       if ((gd->securemode == SUNXI_SECURE_MODE_WITH_SECUREOS) || (sunxi_probe_secure_monitor()))
       {
           key_data_len = arm_svc_efuse_read(key_info->name, (void *)data_buff);
           if (key_data_len <= 0)
           {
               printf("efuse read %s err\n", key_info->name);
               return -1;
           }
 
       } else {
           if (sunxi_efuse_read(key_info->name, (void *)data_buff, &key_data_len)) {
               printf("efuse read %s err\n", key_info->name);
               return -1;
           }
       }
 
       /* convert hex key to char,so that dragonkey tool can display it */
       hex2char((void *)(key_info->key_data), (void *)(data_buff), key_data_len);
       *read_len +=key_data_len*2;
       return 0;
   }
   else
   {
       printf("read key form securestorage\n");
       ret = sunxi_secure_object_read(key_info->name, data_buff, sizeof(sunxi_usb_burn_key_info_t), &key_data_len);
       if (ret < 0)
           printf("read %s form securestorage failed\n", key_info->name);
       else {
           key_info->len = key_data_len;
           memcpy((void *)(key_info->key_data),
               (void *)data_buff, key_data_len);
           *read_len += key_data_len;
           return 0;
       }
   }
 
   return ret;
}
 
/*
************************************************************************************************************
*
*                                             function
*
*    name          :    __sunxi_read_key_by_name
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
int __sunxi_erase_key(void)
{
    __attribute__((unused)) int ret;
#ifdef CONFIG_SUNXI_SECURE_STORAGE
   ret = sunxi_secure_storage_init();
   if(ret < 0)
   {
       printf("secure storage init err\n");
       return -1;
   }
 
   ret = sunxi_secure_storage_erase_all();
   if(ret < 0)
   {
       printf("erase secure storage failed\n");
       return -1;
   }
 
   printf("erase securestorage success\n");
   sunxi_secure_storage_exit();
#endif
   return 0;
}
 
/*
************************************************************************************************************
*
*                                             function
*
*    name          :    __sunxi_read_key_by_name
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
int __sunxi_burn_flag(void)
{
#ifdef CONFIG_SUNXI_SECURE_STORAGE
   int ret;
   ret = sunxi_secure_storage_init();
   if(ret < 0)
   {
       printf("secure storage init err\n");
       return -1;
   }
   if(sunxi_secure_object_write("key_burned_flag", "key_burned", strlen("key_burned")))
   {
       printf("save burned flag to securestorage failed\n");
       return -1;
   }
 
   printf("save burned flag to securestorage success\n");
   sunxi_secure_storage_exit();
#endif
   return 0;
}
 
/*
************************************************************************************************************
*
*                                             function
*
*    name          :
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
static int sunxi_pburn_init(void)
{
   sunxi_usb_dbg("sunxi_pburn_init\n");
   memset(&trans_data, 0, sizeof(pburn_trans_set_t));
   sunxi_usb_pburn_write_enable = 0;
    sunxi_usb_pburn_status = SUNXI_USB_PBURN_IDLE;
 
    trans_data.base_recv_buffer =
       (u8 *)malloc_align(SUNXI_PBURN_RECV_MEM_SIZE, 64);
    if(!trans_data.base_recv_buffer)
    {
        printf("sunxi usb pburn err: unable to malloc memory for pburn receive\n");
 
        return -1;
    }
 
    trans_data.base_send_buffer =
       (u8 *)malloc_align(SUNXI_PBURN_SEND_MEM_SIZE, 64);
    if(!trans_data.base_send_buffer)
    {
        printf("sunxi usb pburn err: unable to malloc memory for pburn send\n");
   free_align(trans_data.base_recv_buffer);
 
   return -1;
    }
   sunxi_usb_dbg("recv addr 0x%x\n", (uint)trans_data.base_recv_buffer);
    sunxi_usb_dbg("send addr 0x%x\n", (uint)trans_data.base_send_buffer);
 
    return 0;
}
/*
************************************************************************************************************
*
*                                             function
*
*    name          :
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
static int sunxi_pburn_exit(void)
{
   sunxi_usb_dbg("sunxi_pburn_exit\n");
    if(trans_data.base_recv_buffer)
    {
       free_align(trans_data.base_recv_buffer);
       trans_data.base_recv_buffer = NULL;
    }
    if(trans_data.base_send_buffer)
    {
       free_align(trans_data.base_send_buffer);
       trans_data.base_send_buffer = NULL;
    }
 
    return 0;
}
/*
************************************************************************************************************
*
*                                             function
*
*    name          :
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
static void sunxi_pburn_reset(void)
{
   sunxi_usb_pburn_write_enable = 0;
    sunxi_usb_pburn_status = SUNXI_USB_PBURN_IDLE;
}
/*
************************************************************************************************************
*
*                                             function
*
*    name          :
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
static void  sunxi_pburn_usb_rx_dma_isr(void *p_arg)
{
   sunxi_usb_dbg("dma int for usb rx occur\n");
   //通知主循环,可以写入数据
   sunxi_usb_pburn_write_enable = 1;
}
/*
************************************************************************************************************
*
*                                             function
*
*    name          :
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
static void  sunxi_pburn_usb_tx_dma_isr(void *p_arg)
{
   sunxi_usb_dbg("dma int for usb tx occur\n");
#if defined(SUNXI_USB_30)
   sunxi_usb_pburn_status_enable = 1;
#endif
}
/*
************************************************************************************************************
*
*                                             function
*
*    name          :
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
static int sunxi_pburn_standard_req_op(uint cmd, struct usb_device_request *req, uchar *buffer)
{
   int ret = SUNXI_USB_REQ_OP_ERR;
 
   switch(cmd)
   {
       case USB_REQ_GET_STATUS:
       {
           ret = __usb_get_status(req, buffer);
 
           break;
       }
       //case USB_REQ_CLEAR_FEATURE:
       //case USB_REQ_SET_FEATURE:
       case USB_REQ_SET_ADDRESS:
       {
           ret = __usb_set_address(req);
 
           break;
       }
       case USB_REQ_GET_DESCRIPTOR:
       //case USB_REQ_SET_DESCRIPTOR:
       case USB_REQ_GET_CONFIGURATION:
       {
           ret = __usb_get_descriptor(req, buffer);
 
           break;
       }
       case USB_REQ_SET_CONFIGURATION:
       {
           ret = __usb_set_configuration(req);
 
           break;
       }
       //case USB_REQ_GET_INTERFACE:
       case USB_REQ_SET_INTERFACE:
       {
           ret = __usb_set_interface(req);
 
           break;
       }
       //case USB_REQ_SYNCH_FRAME:
       default:
       {
           printf("sunxi pburn error: standard req is not supported\n");
 
           ret = SUNXI_USB_REQ_DEVICE_NOT_SUPPORTED;
 
           break;
       }
   }
 
   return ret;
}
/*
************************************************************************************************************
*
*                                             function
*
*    name          :
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
static int sunxi_pburn_nonstandard_req_op(uint cmd, struct usb_device_request *req, uchar *buffer, uint data_status)
{
   int ret = SUNXI_USB_REQ_SUCCESSED;
 
   switch(req->bmRequestType)        //PBURN 特有请求
   {
       case 161:
           if(req->bRequest == 0xFE)
           {
               sunxi_usb_dbg("pburn ask for max lun\n");
 
               buffer[0] = 0;
 
               sunxi_udc_send_setup(1, buffer);
           }
           else
           {
               printf("sunxi usb err: unknown ep0 req in pburn\n");
 
               ret = SUNXI_USB_REQ_DEVICE_NOT_SUPPORTED;
           }
           break;
 
       default:
           printf("sunxi usb err: unknown non standard ep0 req\n");
 
           ret = SUNXI_USB_REQ_DEVICE_NOT_SUPPORTED;
 
           break;
   }
 
   return ret;
}
 
static uint burn_private_start, burn_private_len;
static uint pburn_flash_start;
static void sunxi_usb_burn_private_partition(struct umass_bbb_cbw_t *cbw,
                        struct umass_bbb_csw_t *csw)
{
   disk_partition_t disk_part_info;
 
   sunxi_usb_dbg("usb burn private\n");
   printf("usb command = %d\n", cbw->CBWCDB[1]);
   switch (cbw->CBWCDB[1]) {
   case 0: //握手
   {
       __usb_handshake_t *handshake =
           (__usb_handshake_t *)trans_data.base_send_buffer;
 
       if (sunxi_partition_get_info("private", &disk_part_info)) {
           printf("private partition is not exist\n");
 
           csw->bCSWStatus = -1;
       }
       burn_private_start = disk_part_info.start;
       burn_private_len   = disk_part_info.size;
 
       csw->bCSWStatus = 0;
 
       memset(handshake, 0, sizeof(__usb_handshake_t));
       /* for private partition, use __usb_handshake_t, magic "usb_burn_handsha" */
       /* for secure storage, use __usb_handshake_sec_t, magic "usb_burn_handshake" */
       strncpy(handshake->magic, "usb_burn_handshake", 16);
       handshake->sizelo      = burn_private_len;
       handshake->sizehi      = 0;
       sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
 
       sunxi_usb_burn_from_boot_setup     = 1;
       sunxi_usb_burn_from_boot_handshake = 1;
 
       trans_data.act_send_buffer = trans_data.base_send_buffer;
       trans_data.send_size       = min(cbw->dCBWDataTransferLength,
                      sizeof(__usb_handshake_t));
   } break;
 
   case 1: //小机端接收数据
   {
       memcpy(&pburn_flash_start, (cbw->CBWCDB + 4), 4);
 
       trans_data.recv_size       = cbw->dCBWDataTransferLength;
       trans_data.act_recv_buffer = trans_data.base_recv_buffer;
 
       pburn_flash_start += burn_private_start;
 
       sunxi_usb_pburn_write_enable = 0;
       sunxi_udc_start_recv_by_dma(
           trans_data.act_recv_buffer,
           trans_data.recv_size); //start dma to receive data
 
       sunxi_usb_pburn_status = SUNXI_USB_PBURN_RECEIVE_DATA;
   } break;
 
   case 3: //小机端发送数据
   {
       uint start, sectors, ret;
 
       //start   = *(int *)(cbw->CBWCDB + 4);        //读数据的偏移量
       //sectors = *(int *)(cbw->CBWCDB + 8);        //扇区数;
       memcpy(&start, (cbw->CBWCDB + 4), 4);
       memcpy(&sectors, (cbw->CBWCDB + 8), 4);
 
       printf("start=%d, sectors=%d\n", start, sectors);
 
       trans_data.send_size =
           min(cbw->dCBWDataTransferLength, sectors * 512);
       trans_data.act_send_buffer = trans_data.base_send_buffer;
 
       printf("send size=%d\n", trans_data.send_size);
 
       ret = sunxi_flash_read(start + burn_private_start, sectors,
                      trans_data.base_send_buffer);
       if (!ret) {
           printf("sunxi flash read err: start,0x%x sectors 0x%x\n",
                  start, sectors);
 
           csw->bCSWStatus = 1;
       } else {
           csw->bCSWStatus = 0;
       }
 
       sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
   } break;
 
   case 4: //关闭usb
   {
       __usb_handshake_ext_t *handshake =
           (__usb_handshake_ext_t *)trans_data.base_send_buffer;
 
       memset(handshake, 0, sizeof(__usb_handshake_ext_t));
       strcpy(handshake->magic, "usb_burn_finish");
 
       trans_data.act_send_buffer = trans_data.base_send_buffer;
       trans_data.send_size       = min(cbw->dCBWDataTransferLength,
                      sizeof(__usb_handshake_ext_t));
 
       sunxi_udc_send_data((void *)trans_data.act_send_buffer,
                   trans_data.send_size);
 
       csw->bCSWStatus = 0;
 
       sunxi_flash_flush();
       sunxi_usb_pburn_status = SUNXI_USB_PBURN_EXIT;
 
   } break;
 
   case 5: {
       __usb_handshake_ext_t *handshake =
           (__usb_handshake_ext_t *)trans_data.base_send_buffer;
       char buffer[512];
 
       memset(handshake, 0, sizeof(__usb_handshake_ext_t));
       strcpy(handshake->magic, "usb_burn_saved");
 
       trans_data.act_send_buffer = trans_data.base_send_buffer;
       trans_data.send_size       = min(cbw->dCBWDataTransferLength,
                      sizeof(__usb_handshake_ext_t));
 
       csw->bCSWStatus = 0;
 
       sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
 
       memset(buffer, 0, 512);
       strcpy(buffer, "key_burned");
 
       if (!sunxi_flash_write(burn_private_start + burn_private_len -
                          (8192 + 512) / 512,
                      1, buffer)) {
           printf("save burned flag err\n");
 
           csw->bCSWStatus = -1;
       }
       sunxi_flash_flush();
#ifdef CONFIG_SUNXI_SECURE_STORAGE
       if (sunxi_secure_storage_init()) {
           printf("init secure storage failed\n");
 
           csw->bCSWStatus = -1;
       } else {
           if (sunxi_secure_object_write("key_burned_flag",
                             "key_burned",
                             strlen("key_burned"))) {
               printf("save burned flag err\n");
 
               csw->bCSWStatus = -1;
           }
       }
       sunxi_secure_storage_exit();
#endif
   } break;
 
   default:
       break;
   }
}
 
static void sunxi_auto_fel_process(struct umass_bbb_cbw_t *cbw,
                  struct umass_bbb_csw_t *csw)
{
   printf("usb update probe \n");
   printf("usb command = %d\n", cbw->CBWCDB[1]);
   switch (cbw->CBWCDB[1]) {
   case 0: //handshake
   {
       __usb_handshake_sec_t *handshake =
           (__usb_handshake_sec_t *)trans_data.base_send_buffer;
 
       memset(handshake, 0, sizeof(__usb_handshake_sec_t));
       strcpy(handshake->magic, "usb_update_probe_ok");
       sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
 
       trans_data.act_send_buffer = trans_data.base_send_buffer;
       trans_data.send_size       = min(cbw->dCBWDataTransferLength,
                      sizeof(__usb_handshake_sec_t));
 
       csw->bCSWStatus               = 0;
       sunxi_auto_fel_from_boot_handshake = 1;
 
   } break;
   case 1: //exit
   {
       __usb_handshake_sec_t *handshake =
           (__usb_handshake_sec_t *)trans_data.base_send_buffer;
 
       memset(handshake, 0, sizeof(__usb_handshake_sec_t));
       strcpy(handshake->magic, "usb_update_exit");
       sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
 
       trans_data.act_send_buffer = trans_data.base_send_buffer;
       trans_data.send_size       = min(cbw->dCBWDataTransferLength,
                      sizeof(__usb_handshake_sec_t));
       sunxi_usb_probe_update_action = 2;
       csw->bCSWStatus              = 0;
   } break;
   case 2: //enter FEL
   {
       __usb_handshake_sec_t *handshake =
           (__usb_handshake_sec_t *)trans_data.base_send_buffer;
 
       memset(handshake, 0, sizeof(__usb_handshake_sec_t));
       strcpy(handshake->magic, "usb_update_efex");
       sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
 
       trans_data.act_send_buffer = trans_data.base_send_buffer;
       trans_data.send_size       = min(cbw->dCBWDataTransferLength,
                      sizeof(__usb_handshake_sec_t));
       sunxi_usb_probe_update_action = 1;
       csw->bCSWStatus              = 0;
   } break;
   default:
       printf("usb command = %d not support\n", cbw->CBWCDB[1]);
       break;
   }
}
/*
************************************************************************************************************
*
*                                             function
*
*    name          :
*
*    parmeters     :
*
*    return        :
*
*    note          :
*
*
************************************************************************************************************
*/
static int sunxi_pburn_state_loop(void  *buffer)
{
   static struct umass_bbb_cbw_t  *cbw;
   static struct umass_bbb_csw_t  csw;
   static struct sunxi_efex_cbw_t *efex_cbw;
   int    ret;
   sunxi_ubuf_t *sunxi_ubuf = (sunxi_ubuf_t *)buffer;
 
   switch(sunxi_usb_pburn_status)
   {
       case SUNXI_USB_PBURN_IDLE:
           if(sunxi_ubuf->rx_ready_for_data == 1)
           {
               sunxi_usb_pburn_status = SUNXI_USB_PBURN_SETUP;
           }
 
           break;
 
       case SUNXI_USB_PBURN_SETUP:
 
           sunxi_usb_dbg("SUNXI_USB_PBURN_SETUP\n");
           if(sunxi_ubuf->rx_req_length == sizeof(struct sunxi_efex_cbw_t))
           {
               efex_cbw = (struct sunxi_efex_cbw_t *)sunxi_ubuf->rx_req_buffer;
               if(efex_cbw->magic == CBW_MAGIC)
               {
                   printf("try to update \n");
                   sunxi_usb_pburn_status = SUNXI_USB_PBURN_STATUS;
                   trans_data.recv_size = cbw->dCBWDataTransferLength;
                   trans_data.act_recv_buffer = trans_data.base_recv_buffer;
                   sunxi_usb_pburn_write_enable = 0;
                       sunxi_udc_start_recv_by_dma(trans_data.act_recv_buffer, 16);    //start dma to receive data
                   break;
               }
           }
           if(sunxi_ubuf->rx_req_length != sizeof(struct umass_bbb_cbw_t))
           {
               printf("sunxi usb error: received bytes 0x%x is not equal cbw struct size 0x%x\n", sunxi_ubuf->rx_req_length, sizeof(struct umass_bbb_cbw_t));
 
               sunxi_ubuf->rx_ready_for_data = 0;
               sunxi_usb_pburn_status = SUNXI_USB_PBURN_IDLE;
 
               break;
           }
 
           cbw = (struct umass_bbb_cbw_t *)sunxi_ubuf->rx_req_buffer;
           if(CBWSIGNATURE != cbw->dCBWSignature)
           {
               printf("sunxi usb error: the cbw signature 0x%x is bad, need 0x%x\n", cbw->dCBWSignature, CBWSIGNATURE);
 
               sunxi_ubuf->rx_ready_for_data = 0;
               sunxi_usb_pburn_status = SUNXI_USB_PBURN_IDLE;
 
               break;
           }
 
           csw.dCSWSignature = CSWSIGNATURE;
           csw.dCSWTag       = cbw->dCBWTag;
 
#if defined(SUNXI_USB_30)
           sunxi_usb_pburn_status_enable = 1;
#endif
           sunxi_usb_dbg("usb cbw command = 0x%x\n", cbw->CBWCDB[0]);
 
           switch(cbw->CBWCDB[0])
             {
               case 0xf8: /*probe MP tools*/
                   if (sunxi_auto_fel_runnning) {
                       sunxi_auto_fel_process(cbw,
                                      &csw);
                   } else {
                       /*ignore*/
                   }
                   break;
               case 0xf0:            //burn user data
                     sunxi_usb_dbg("usb burn secure storage data\n");
                     printf("usb command = %d\n", cbw->CBWCDB[1]);
                     switch(cbw->CBWCDB[1])
                     {
                         case 0:                //握手
                         {
                             __usb_handshake_sec_t  *handshake = (__usb_handshake_sec_t *)trans_data.base_send_buffer;
 
                            memset(handshake, 0, sizeof(__usb_handshake_sec_t));
                           strcpy(handshake->magic, "usb_burn_handshake");
                           sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
 
                             trans_data.act_send_buffer = trans_data.base_send_buffer;
                             trans_data.send_size = min(cbw->dCBWDataTransferLength, sizeof(__usb_handshake_sec_t));
 
                           sunxi_usb_burn_from_boot_setup = 1;
 
                           if(private_data_ext_buff == NULL)
                           {
                               printf("malloc memory\n");
                               private_data_ext_buff =
                                   (u8 *)malloc_align(
                                       DATA_BUFFER_MAX_SIZE,
                                       64);
                               if(private_data_ext_buff == NULL)
                               {
                                   printf("there is no memory to store all user key data\n");
                                   csw.bCSWStatus = -1;
                               }
                               else
                                 {
                                     csw.bCSWStatus = 0;
                                     sunxi_usb_burn_from_boot_handshake = 1;
                                 }
                           }
                           else
                             {
                                 csw.bCSWStatus = 0;
                                 sunxi_usb_burn_from_boot_handshake = 1;
                             }
                             private_data_ext_buff_step = private_data_ext_buff;
                       }
                       break;
 
                       case 1:                //小机端接收数据
                       {
                           trans_data.recv_size = cbw->dCBWDataTransferLength;
 
                            sunxi_usb_pburn_write_enable = 0;
 
                            memset(private_data_ext_buff, 0, DATA_BUFFER_MAX_SIZE);
                           sunxi_udc_start_recv_by_dma(private_data_ext_buff_step, trans_data.recv_size);    //start dma to receive data
 
                           printf("recv_size=%d\n", trans_data.recv_size);
                           sunxi_dump(private_data_ext_buff, trans_data.recv_size);
 
                           sunxi_usb_pburn_status = SUNXI_USB_PBURN_RECEIVE_NULL;
                       }
                       break;
 
                       case 2:             //工具端声明数据传输已经完毕,要求获取烧录状态
                       {
                           __usb_handshake_ext_t  *handshake = (__usb_handshake_ext_t *)trans_data.base_send_buffer;
 
                           memset(handshake, 0, sizeof(__usb_handshake_ext_t));
                           sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
 
                           printf("recv_size=%d\n", trans_data.recv_size);
                           sunxi_dump(private_data_ext_buff, trans_data.recv_size);
 
                           int ret = __sunxi_burn_key(private_data_ext_buff, trans_data.recv_size);
 
                             trans_data.act_send_buffer = trans_data.base_send_buffer;
                             trans_data.send_size = min(cbw->dCBWDataTransferLength, sizeof(__usb_handshake_ext_t));
                             //开始根据数据类型进行烧录动作
 
                             if(!ret)    //数据烧写成功
                             {
                                 strcpy(handshake->magic, "usb_burn_success");
                                 csw.bCSWStatus = 0;
                             }
                             else    //数据烧写失败
                             {
                                 strcpy(handshake->magic, "usb_burn_error");
                                 csw.bCSWStatus = -1;
                             }
                       }
                       break;
 
                       case 4:                //关闭usb
                       {
                           __usb_handshake_ext_t  *handshake = (__usb_handshake_ext_t *)trans_data.base_send_buffer;
 
                            memset(handshake, 0, sizeof(__usb_handshake_ext_t));
                           strcpy(handshake->magic, "usb_burn_finish");
 
                           trans_data.act_send_buffer = trans_data.base_send_buffer;
                             trans_data.send_size = min(cbw->dCBWDataTransferLength, sizeof(__usb_handshake_ext_t));
 
                           sunxi_udc_send_data((void *)trans_data.act_send_buffer, trans_data.send_size);
 
                             csw.bCSWStatus = 0;
 
                           sunxi_flash_flush();
 
                           if(private_data_ext_buff)
                           {
                               free_align(
                                   private_data_ext_buff);
                               private_data_ext_buff =
                                   NULL;
                           }
 
                           sunxi_usb_pburn_status = SUNXI_USB_PBURN_EXIT;
                       }
                       break;
 
                       case 5:    //设置烧写key flag
                       {
                           int ret;
                           __usb_handshake_ext_t  *handshake = (__usb_handshake_ext_t *)trans_data.base_send_buffer;
 
                           memset(handshake, 0, sizeof(__usb_handshake_ext_t));
                           strcpy(handshake->magic, "usb_burn_saved");
 
                           trans_data.act_send_buffer = trans_data.base_send_buffer;
                             trans_data.send_size = min(cbw->dCBWDataTransferLength, sizeof(__usb_handshake_ext_t));
 
                             csw.bCSWStatus = 0;
 
                           ret = __sunxi_burn_flag();
                           if(ret < 0)
                           {
                               printf("sunxi burn flag failed\n");
                               csw.bCSWStatus = -1;
                           }
 
                           sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
                       }
                       break;
                       case 6:        //huk
                       {
                           u8  huk[32];
                           int ret = -1;
 
                           memset(huk, 0, 32);
                           //ret = smc_create_huk(huk, 32);
 
                           csw.bCSWStatus = 0;
                           trans_data.act_send_buffer = trans_data.base_send_buffer;
                           trans_data.send_size = min(cbw->dCBWDataTransferLength, (u32)32);
 
                           if(ret < 0)
                           {
                               printf("smc_create_huk failed\n");
                               csw.bCSWStatus = -1;
                           }
                           else
                           {
                               if(!ret)
                                   printf("smc_create_huk successed\n");
                               else
                                   printf("smc_create_huk already exist\n");
                               memcpy((void *)trans_data.act_send_buffer, huk, 32);
                           }
 
                             sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
                       }
                       break;
 
                       case 7:        //read key one by one
                       {
                           int ret;
                           int read_len = 0;
 
                           sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
 
                           ret = __sunxi_read_key_by_name(private_data_ext_buff, DATA_BUFFER_MAX_SIZE, &read_len);
                           if(!ret)
                           {
                               printf("read key success\n");
                               csw.bCSWStatus = 0;
                           }
                           else
                           {
                               printf("read key failed\n");
                               csw.bCSWStatus = 1;
                           }
 
                           trans_data.act_send_buffer = trans_data.base_send_buffer;
                           trans_data.send_size = max(cbw->dCBWDataTransferLength, (u32)read_len);
 
                           memcpy((void *)trans_data.act_send_buffer, (void *)private_data_ext_buff, read_len);
                           printf("read_len=%d\n", read_len);
                           printf("the send data:\n");
                           sunxi_dump((void *)private_data_ext_buff, read_len);
                       }
                       break;
 
                       case 8:            //erase all key
                       {
                           int ret;
                           __usb_handshake_ext_t  *handshake = (__usb_handshake_ext_t *)trans_data.base_send_buffer;
 
                           memset(handshake, 0, sizeof(__usb_handshake_ext_t));
 
                           csw.bCSWStatus = 0;
                           sunxi_usb_pburn_status = SUNXI_USB_PBURN_SEND_DATA;
 
                           trans_data.act_send_buffer = trans_data.base_send_buffer;
                           trans_data.send_size = min(cbw->dCBWDataTransferLength, sizeof(__usb_handshake_ext_t));
 
                           ret = __sunxi_erase_key();
                           if(ret < 0)
                           {
                               printf("sunxi erase key failed\n");
                               strcpy(handshake->magic, "usb_erase_failed");
                               handshake->err_no = ERR_NO_ERASE_KEY_FAILED;
                               break;
                           }
 
                           strcpy(handshake->magic, "usb_erase_success");
                           handshake->err_no = ERR_NO_SUCCESS;
                           printf("sunxi erase all key success\n");
                       }
                       break;
                   default:
                       break;
                 }
                 break;
 
               case 0xf3: //自定义命令,用于烧录用户数据
                   sunxi_usb_burn_private_partition(cbw, &csw);
               break;
           }
           break;
 
         case SUNXI_USB_PBURN_SEND_DATA:
 
             sunxi_usb_dbg("SUNXI_USB_SEND_DATA\n");
 
           sunxi_usb_pburn_status = SUNXI_USB_PBURN_STATUS;
           printf("SUNXI_USB_SEND_DATA=%d\n", trans_data.send_size);
           sunxi_udc_send_data((void *)trans_data.act_send_buffer, trans_data.send_size);
#if defined(SUNXI_USB_30)
           sunxi_usb_pburn_status_enable = 0;
#endif
             break;
 
         case SUNXI_USB_PBURN_RECEIVE_DATA:
 
             sunxi_usb_dbg("SUNXI_USB_RECEIVE_DATA\n");
 
           if(sunxi_usb_pburn_write_enable == 1)
           {
               sunxi_usb_dbg("write flash, start 0x%x, sectors 0x%x\n", pburn_flash_start, trans_data.recv_size/512);
               ret = sunxi_flash_write(pburn_flash_start, (trans_data.recv_size+511)/512, (void *)trans_data.act_recv_buffer);
               if(!ret)
               {
                   printf("sunxi flash write err: start,0x%x sectors 0x%x\n", pburn_flash_start, (trans_data.recv_size+511)/512);
 
                   csw.bCSWStatus = 1;
               }
               else
               {
                   csw.bCSWStatus = 0;
                  }
               sunxi_usb_pburn_write_enable = 0;
 
                  sunxi_usb_pburn_status = SUNXI_USB_PBURN_STATUS;
           }
 
             break;
 
       case SUNXI_USB_PBURN_STATUS:
 
           sunxi_usb_dbg("SUNXI_USB_PBURN_STATUS\n");
#if defined(SUNXI_USB_30)
           if(sunxi_usb_pburn_status_enable)
#endif
           {
               sunxi_usb_pburn_status = SUNXI_USB_PBURN_IDLE;
               sunxi_ubuf->rx_ready_for_data = 0;
               __sunxi_pburn_send_status(&csw, sizeof(struct umass_bbb_csw_t));
           }
 
           break;
 
       case SUNXI_USB_PBURN_EXIT:
 
           printf("SUNXI_USB_PBURN_EXIT\n");
 
           sunxi_usb_pburn_status = SUNXI_USB_PBURN_IDLE;
           sunxi_ubuf->rx_ready_for_data = 0;
           __sunxi_pburn_send_status(&csw, sizeof(struct umass_bbb_csw_t));
 
           printf("Device will shutdown in 3 Secends...\n");
           __msdelay(3000);
 
           return SUNXI_UPDATE_NEXT_ACTION_SHUTDOWN;
 
         case SUNXI_USB_PBURN_RECEIVE_NULL:
 
             sunxi_usb_dbg("SUNXI_USB_PBURN_RECEIVE_NULL\n");
           if(sunxi_usb_pburn_write_enable == 1)
           {
               csw.bCSWStatus = 0;
               sunxi_usb_pburn_write_enable = 0;
                  sunxi_usb_pburn_status = SUNXI_USB_PBURN_STATUS;
           }
             break;
 
         default:
             break;
   }
 
   return 0;
}
 
sunxi_usb_module_init(SUNXI_USB_DEVICE_BURN,                    \
                     sunxi_pburn_init,                            \
                     sunxi_pburn_exit,                            \
                     sunxi_pburn_reset,                            \
                     sunxi_pburn_standard_req_op,                \
                     sunxi_pburn_nonstandard_req_op,            \
                     sunxi_pburn_state_loop,                    \
                     sunxi_pburn_usb_rx_dma_isr,                \
                     sunxi_pburn_usb_tx_dma_isr                    \
                     );