summaryrefslogtreecommitdiff
path: root/common/cmd_fastboot.c
blob: c9c15b4cfdfe1e157bc6fe64229e23e913aafb94 (plain)
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
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
/*
 * Copyright (C) 2010 Texas Instruments
 *
 * Author : Mohammed Afzal M A <afzal@ti.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 *
 *
 * Fastboot is implemented using gadget stack, many of the ideas are
 * derived from fastboot implemented in OmapZoom by
 * Tom Rix <Tom.Rix@windriver.com>, and portion of the code has been
 * ported from OmapZoom.
 *
 * Part of OmapZoom was copied from Android project, Android source
 * (legacy bootloader) was used indirectly here by using OmapZoom.
 *
 * This is Android's Copyright:
 *
 * Copyright (C) 2008 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <common.h>
#include <command.h>
#include <fastboot.h>
#include <linux/mtd/nand.h>
#include <sparse.h>
#include <../include/configs/wmt.h>
#include <../include/version.h>

#include "../board/wmt/include/wmt_iomux.h"

#include "../define.h"
#define WMT_UBOOT_BUILD_ID "BUILDID_"  BUILD_TIME

//#define	ERR       1
//#define	WARN    1
//#define	INFO     1
//#define      DEBUG   1

#ifdef DEBUG
#define FBTDBG(fmt,args...)\
        printf("DEBUG: [%s]: %d: \n"fmt, __FUNCTION__, __LINE__,##args)
#else
#define FBTDBG(fmt,args...) do{}while(0)
#endif

#ifdef INFO
#define FBTINFO(fmt,args...)\
        printf("INFO: [%s]: "fmt, __FUNCTION__, ##args)
#else
#define FBTINFO(fmt,args...) do{}while(0)
#endif

#ifdef WARN
#define FBTWARN(fmt,args...)\
        printf("WARNING: [%s]: "fmt, __FUNCTION__, ##args)
#else
#define FBTWARN(fmt,args...) do{}while(0)
#endif

#ifdef ERR
#define FBTERR(fmt,args...)\
        printf("ERROR: [%s]: "fmt, __FUNCTION__, ##args)
#else
#define FBTERR(fmt,args...) do{}while(0)
#endif

#include <nand.h>
#include <environment.h>
/* USB specific */
#include <usb_defs.h>
#ifndef _GLOBAL_H_
#include "../board/wmt/include/global.h"
#endif
#include "compiler.h"
#include "usbdevice.h"


extern void restore_original_string_serial(void);
extern struct nand_chip *get_current_nand_chip(void);
extern int tellme_nandinfo(struct nand_chip *nand);
extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern void udc_endpoint_write (struct usb_endpoint_instance *endpoint);
extern int  udc_init (void);
extern void udc_enable(struct usb_device_instance *device);
extern void udc_disable(void);
extern void udc_connect(void);
extern void udc_disconnect(void);
extern void udc_setup_ep (struct usb_device_instance *device,
                          unsigned int ep, struct usb_endpoint_instance *endpoint);
extern int wmt_udc_irq(void);
extern void udc_startup_events(struct usb_device_instance *device);
extern int WMTSaveImageToNAND(struct nand_chip *nand, unsigned long long naddr, unsigned int dwImageStart,
                              unsigned int dwImageLength);
extern int WMTSaveImageToNAND2(struct nand_chip *nand, unsigned long long naddr, unsigned int dwImageStart,
                               unsigned int dwImageLength, int oob_offs, unsigned int need_erase, unsigned long long *eaddr);
extern int WMTEraseNAND(struct nand_chip *nand, unsigned int block, unsigned int block_nums, unsigned int all);
void *memcpy_itp(void *d, const void *s, size_t n);
extern int mmc_fb_write(unsigned dev_num, ulong blknr, ulong blkcnt, uchar *src);
extern flash_info_t flash_info[];	/* info for FLASH chips */
extern int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int mmc_part_update(unsigned char *buffer);
extern int do_fat_fsstore (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int image_rsa_check(
        unsigned int image_mem_addr,
        unsigned int image_size,
        unsigned int sig_addr,
        unsigned int sig_size);
/*extern void memdump(void *pv, int num);*/
extern env_t *env_ptr;
extern int curr_device;
extern int udc_download_flag;

#define STR_LANG		0x00
#define STR_MANUFACTURER	0x01
#define STR_PRODUCT		0x02
#define STR_SERIAL		0x03
#define STR_CONFIGURATION	0x04
#define STR_INTERFACE		0x05
#define STR_COUNT		0x06

#define CONFIG_USBD_CONFIGURATION_STR	"Android Fastboot Configuration"
#define CONFIG_USBD_INTERFACE_STR	"Android Fastboot Interface"

#define USBFBT_BCD_DEVICE	0x00
#define	USBFBT_MAXPOWER		0x32

#define	NUM_CONFIGS	1
#define	NUM_INTERFACES	1
#define	NUM_ENDPOINTS	2

#define	RX_EP_INDEX	1
#define	TX_EP_INDEX	2

#define SZ_1M                           0x00100000
#define SZ_2M                           0x00200000
#define SZ_4M                           0x00400000
#define SZ_8M                           0x00800000
#define SZ_16M                         0x01000000
#define SZ_31M                         0x01F00000
#define SZ_32M                         0x02000000
#define SZ_64M                         0x04000000
#define SZ_128M                       0x08000000
#define SZ_256M                       0x10000000
#define SZ_288M                       0x12000000
#define SZ_320M                       0x14000000
#define SZ_512M                       0x20000000
#define SZ_1024M                     0x40000000
#define SZ_640M                       0x28000000
#define SZ_768M                       0x30000000
#define SZ_896M                       0x38000000
#define SIZE_REMAINING           (~0U)

#define MAX_DOWNLOAD_SIZE      SZ_256M

//for emmc
#define PART_SYSTEM 1
#define PART_BOOT 2
#define PART_RECOVERY 3
#define PART_CACHE 5
#define PART_SWAP 6
#define PART_U_BOOT_LOGO 7
#define PART_KERNEL_LOGO 8
#define PART_MISC 9
#define PART_EFS 10
#define PART_RADIO 11
#define PART_KEYDATA 12
#define PART_USERDATA 13

//for nand flash
#define NF_PART_LOGO 0
#define NF_PART_BOOT 1
#define NF_PART_RECOVERY 2
#define NF_PART_MISC 3
#define NF_PART_KEYDATA 4
#define NF_PART_SYSTEM 5
#define NF_PART_CACHE 6
#define NF_PART_SWAP 7
#define NF_PART_DATA 8
#define NF_PART_MAX  9


#define STR_EXT4 1
#define STR_SIG 2
#define STR_ROM 3
#define STR_FAT 4
#define STR_NOFS 5
#define STR_RAM 6
#define STR_NAND 7
#define STR_UBI 8

#define ROM_WLOAD 0
#define ROM_UBOOT 1
#define ROM_ENV1 2
#define ROM_ENV2 3


#define EP0_MAX_PACKET_SIZE 64
#define PHYS_SDRAM_F         0
#define CONFIG_FASTBOOT_TRANSFER_BUFFER         	(PHYS_SDRAM_F + SZ_128M)
#define CONFIG_FASTBOOT_TRANSFER_BUFFER_SIZE    	(SZ_1024M - SZ_128M)
/*define signature buffer addr 127M*/
#define CONFIG_FASTBOOT_SIG_BUFFER 				(PHYS_SDRAM_F + SZ_128M - SZ_1M)
/* configuration modifiers
 */
#define BMATTRIBUTE_RESERVED		0x80
#define BMATTRIBUTE_SELF_POWERED	0x40

#define    FASTBOOT_PRODUCT_NAME         "wmid"
#define    FASTBOOT_NAND_BLOCK_SIZE                 8192
#define    FASTBOOT_NAND_OOB_SIZE                   64
#define 	 MTDPART_OFS_APPEND	(-1)

#define mdelay(n) ({unsigned long msec=(n); while (msec--) udelay(1000);})

struct _fbt_config_desc {
	struct usb_configuration_descriptor configuration_desc;
	struct usb_interface_descriptor interface_desc;
	struct usb_endpoint_descriptor endpoint_desc[NUM_ENDPOINTS];
};

static int fbt_handle_response(void);
/* defined and used by gadget/ep0.c */
extern struct usb_string_descriptor **usb_strings;

//static struct cmd_fastboot_interface priv;
struct cmd_fastboot_interface priv;
/* USB Descriptor Strings */
static char serial_number[28]; /* what should be the length ?, 28 ? */
static u8 wstr_lang[4] = {4, USB_DT_STRING, 0x9, 0x4};
static u8 wstr_manufacturer[2 + 2 * (sizeof(CONFIG_USBD_MANUFACTURER) - 1)];
static u8 wstr_product[2 + 2 * (sizeof(CONFIG_USBD_PRODUCT_NAME) - 1)];
static u8 wstr_serial[2 + 2 * (sizeof(serial_number) - 1)];
static u8 wstr_configuration[2 + 2 * (sizeof(CONFIG_USBD_CONFIGURATION_STR) - 1)];
static u8 wstr_interface[2 + 2 * (sizeof(CONFIG_USBD_INTERFACE_STR) - 1)];

/* USB descriptors */
static struct usb_device_descriptor device_descriptor __attribute__((aligned (32))) = {
	.bLength = sizeof(struct usb_device_descriptor),
	.bDescriptorType =	USB_DT_DEVICE,
	.bcdUSB =		cpu_to_le16(USB_BCD_VERSION),
	.bDeviceClass =		0xFF,
	.bDeviceSubClass =	0x00,
	.bDeviceProtocol =	0x00,
	.bMaxPacketSize0 =	EP0_MAX_PACKET_SIZE,
	.idVendor =		cpu_to_le16(CONFIG_USBD_VENDORID),
	.bcdDevice =		cpu_to_le16(USBFBT_BCD_DEVICE),
	.iManufacturer =	STR_MANUFACTURER,
	.iProduct =		STR_PRODUCT,
	.iSerialNumber =	STR_SERIAL,
	.bNumConfigurations =	NUM_CONFIGS
};

static struct _fbt_config_desc fbt_config_desc __attribute__((aligned (32))) = {
	.configuration_desc = {
		.bLength = sizeof(struct usb_configuration_descriptor),
		.bDescriptorType = USB_DT_CONFIG,
		.wTotalLength =	cpu_to_le16(sizeof(struct _fbt_config_desc)),
		.bNumInterfaces = NUM_INTERFACES,
		.bConfigurationValue = 1,
		.iConfiguration = STR_CONFIGURATION,
		.bmAttributes =	BMATTRIBUTE_SELF_POWERED | BMATTRIBUTE_RESERVED,
		.bMaxPower = USBFBT_MAXPOWER,
	},
	.interface_desc = {
		.bLength  = sizeof(struct usb_interface_descriptor),
		.bDescriptorType = USB_DT_INTERFACE,
		.bInterfaceNumber = 0,
		.bAlternateSetting = 0,
		.bNumEndpoints = 0x2,
		.bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
		.bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
		.bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
		.iInterface = STR_INTERFACE,
	},
	.endpoint_desc = {
		{
			.bLength = sizeof(struct usb_endpoint_descriptor),
			.bDescriptorType = USB_DT_ENDPOINT,
			.bEndpointAddress = RX_EP_INDEX | USB_DIR_IN,
			.bmAttributes =	USB_ENDPOINT_XFER_BULK,
			.bInterval = 0,
		},
		{
			.bLength = sizeof(struct usb_endpoint_descriptor),
			.bDescriptorType = USB_DT_ENDPOINT,
			.bEndpointAddress = TX_EP_INDEX | USB_DIR_OUT,
			.bmAttributes = USB_ENDPOINT_XFER_BULK,
			.bInterval = 0,
		},
	},
};

static struct usb_interface_descriptor interface_descriptors[NUM_INTERFACES]__attribute__((aligned (32)));
static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS]__attribute__((aligned (32)));
static struct usb_string_descriptor *fbt_string_table[STR_COUNT]__attribute__((aligned (32)));
static struct usb_device_instance device_instance[1]__attribute__((aligned (32)));
static struct usb_bus_instance bus_instance[1]__attribute__((aligned (32)));
static struct usb_configuration_instance config_instance[NUM_CONFIGS]__attribute__((aligned (32)));
static struct usb_interface_instance interface_instance[NUM_INTERFACES]__attribute__((aligned (32)));
static struct usb_alternate_instance alternate_instance[NUM_INTERFACES]__attribute__((aligned (32)));
static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS + 1]__attribute__((aligned (32)));

/* FASBOOT specific */

#define	GETVARLEN	30
#define	SECURE		"yes"
#define  fb_version_baseband  "0.01.01"
/* U-boot version */
#define fb_version_bootloader 	U_BOOT_VERSION" (" __DATE__ " - " __TIME__ ")"

//static struct cmd_fastboot_interface priv =
struct cmd_fastboot_interface priv = {
	.transfer_buffer       = (unsigned char *)CONFIG_FASTBOOT_TRANSFER_BUFFER,
	.transfer_buffer_size  = (unsigned int)CONFIG_FASTBOOT_TRANSFER_BUFFER_SIZE,
};
/*define for all image signature buffer */
unsigned int  sig_buf  = CONFIG_FASTBOOT_SIG_BUFFER;
unsigned int  sig_size;
extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];

static int fbt_init_endpoints (void);
/* Use do_bootm and do_go for fastboot's 'boot' command */
extern int do_go (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
/* Use do_setenv and do_saveenv to permenantly save data */
extern int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);

/* Use do_setenv and do_saveenv to permenantly save data */
extern int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int do_syncenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int do_switch_ecc(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int do_mmc_write (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern int fastboot_oem(const char *cmd);

extern unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base);
extern int getenv_f(const char *name, char *buf, unsigned len);
extern int udc_deinit(void);
extern int wmt_read_ostc(int *val);
extern void do_wmt_poweroff(void);
extern int usb_plugin(void);

#define MTDPART_SIZ_FULL 0

/* Initialize nand the name of fastboot mappings */
static fastboot_ptentry ptable[16] = {
	{
		.name		= "logo",
		.start		= MTDPART_OFS_APPEND,
		.length		= 0x01000000, /*16M*/
		.flags  = FASTBOOT_PTENTRY_FLAGS_WRITE_I |
		FASTBOOT_PTENTRY_FLAGS_WRITE_SW_ECC,
	},
	{
		.name		= "boot",
		.start		= MTDPART_OFS_APPEND,
		.length		= 0x01000000, /*16M*/
		.flags  = FASTBOOT_PTENTRY_FLAGS_WRITE_I |
		FASTBOOT_PTENTRY_FLAGS_WRITE_SW_ECC,
	},
	{
		.name		= "recovery",
		.start		= MTDPART_OFS_APPEND,
		.length		= 0x01000000, /*16M*/
		.flags  = FASTBOOT_PTENTRY_FLAGS_WRITE_I |
		FASTBOOT_PTENTRY_FLAGS_WRITE_SW_ECC,
	},
	{
		.name		= "misc",
		.start		= MTDPART_OFS_APPEND,
		.length		= 0x01000000, /*16M*/
		.flags  = FASTBOOT_PTENTRY_FLAGS_WRITE_I |
		FASTBOOT_PTENTRY_FLAGS_WRITE_SW_ECC,
	},
	{
		.name		= "keydata",
		.start		= MTDPART_OFS_APPEND,
		.length		= 0x04000000, /*64M*/
		.flags  = FASTBOOT_PTENTRY_FLAGS_WRITE_I |
		FASTBOOT_PTENTRY_FLAGS_WRITE_SW_ECC,
	},
	{
		.name		= "system",
		.start		= MTDPART_OFS_APPEND,
		.length		= 0x40000000, /*1G*/
		.flags  = FASTBOOT_PTENTRY_FLAGS_WRITE_SW_ECC |
		FASTBOOT_PTENTRY_FLAGS_WRITE_JFFS2,
	},
	{
		.name		= "cache",
		.start		= MTDPART_OFS_APPEND,
		.length		= 0x20000000, /*512M*/
		.flags  = FASTBOOT_PTENTRY_FLAGS_WRITE_SW_ECC |
		FASTBOOT_PTENTRY_FLAGS_WRITE_JFFS2,
	},
	{
		.name		= "swap",
		.start		= MTDPART_OFS_APPEND,
		.length		= 0x10000000,  /*256M*/
		.flags  = FASTBOOT_PTENTRY_FLAGS_WRITE_SW_ECC |
		FASTBOOT_PTENTRY_FLAGS_WRITE_JFFS2,
	},
	{
		.name		= "data",
		.start		= MTDPART_OFS_APPEND,
		.length		= MTDPART_SIZ_FULL, /*rest*/
		.flags  = FASTBOOT_PTENTRY_FLAGS_WRITE_SW_ECC |
		FASTBOOT_PTENTRY_FLAGS_WRITE_JFFS2,
	},
};

/* To support the Android-style naming of flash */
#define MAX_PTN 16
static unsigned int pcount = 0;
static int static_pcount = 16;
static fastboot_ptentry ptable[16];

static unsigned long long sparse_download_size = 0;
static unsigned long long sparse_remained_size = 0;
static unsigned long long sparse_nand_addr = 0;

int mmc_controller_no = 0;
#define MBR  0
#define GPT  1
unsigned char mmc_part_type = MBR;
int detect_mptool;
static int mptool_is_ready;

/* USB specific */

/* utility function for converting char* to wide string used by USB */
static void str2wide (char *str, u16 *wide)
{
	int i;
	for (i = 0; i < strlen (str) && str[i]; i++) {
#if defined(__LITTLE_ENDIAN)
		wide[i] = (u16) str[i];
#elif defined(__BIG_ENDIAN)
		wide[i] = ((u16)(str[i]) << 8);
#endif
	}
}

/* fastboot_init has to be called before this fn to get correct serial string */
static int fbt_init_strings(void)
{
	struct usb_string_descriptor *string;
	FBTDBG("%s\n", __FUNCTION__);

	fbt_string_table[STR_LANG] =
	        (struct usb_string_descriptor *)wstr_lang;

	string = (struct usb_string_descriptor *) wstr_manufacturer;
	string->bLength = sizeof(wstr_manufacturer);
	string->bDescriptorType = USB_DT_STRING;
	str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
	fbt_string_table[STR_MANUFACTURER] = string;

	string = (struct usb_string_descriptor *) wstr_product;
	string->bLength = sizeof(wstr_product);
	string->bDescriptorType = USB_DT_STRING;
	str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
	fbt_string_table[STR_PRODUCT] = string;

	string = (struct usb_string_descriptor *) wstr_serial;
	string->bLength = sizeof(wstr_serial);
	string->bDescriptorType = USB_DT_STRING;
	str2wide (serial_number, string->wData);
	fbt_string_table[STR_SERIAL] = string;

	string = (struct usb_string_descriptor *) wstr_configuration;
	string->bLength = sizeof(wstr_configuration);
	string->bDescriptorType = USB_DT_STRING;
	str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
	fbt_string_table[STR_CONFIGURATION] = string;

	string = (struct usb_string_descriptor *) wstr_interface;
	string->bLength = sizeof(wstr_interface);
	string->bDescriptorType = USB_DT_STRING;
	str2wide (CONFIG_USBD_INTERFACE_STR, string->wData);
	fbt_string_table[STR_INTERFACE] = string;

	/* Now, initialize the string table for ep0 handling */
	usb_strings = fbt_string_table;

	return 0;
}

static void fbt_event_handler (struct usb_device_instance *device,
                               usb_device_event_t event, int data)
{
	FBTDBG("%s\n", __FUNCTION__);

	switch (event) {
	case DEVICE_RESET:
	case DEVICE_BUS_INACTIVE:
		priv.configured = 0;
		sparse_remained_size = 0;
		sparse_download_size = 0;
		restore_original_string_serial();
		break;
	case DEVICE_CONFIGURED:
		priv.configured = 1;
		break;

	case DEVICE_ADDRESS_ASSIGNED:
		fbt_init_endpoints ();

	default:
		break;
	}
}

/* fastboot_init has to be called before this fn to get correct serial string */
static int fbt_init_instances(void)
{
	int i;
	FBTDBG("%s\n", __FUNCTION__);

	/* initialize device instance */
	memset (device_instance, 0, sizeof (struct usb_device_instance));
	device_instance->device_state = STATE_INIT;
	device_instance->device_descriptor = &device_descriptor;
	device_instance->event = fbt_event_handler;
	device_instance->cdc_recv_setup = NULL;
	device_instance->bus = bus_instance;
	device_instance->configurations = NUM_CONFIGS;
	device_instance->configuration_instance_array = config_instance;

	/* XXX: what is this bus instance for ?, can't it be removed by moving
	    endpoint_array and serial_number_str is moved to device instance */
	/* initialize bus instance */
	memset (bus_instance, 0, sizeof (struct usb_bus_instance));
	bus_instance->device = device_instance;
	bus_instance->endpoint_array = endpoint_instance;
	/* XXX: what is the relevance of max_endpoints & maxpacketsize ? */
	bus_instance->max_endpoints = 3;
	bus_instance->maxpacketsize = 64;
	bus_instance->serial_number_str = serial_number;

	/* configuration instance */
	memset (config_instance, 0,
	        sizeof (struct usb_configuration_instance));
	config_instance->interfaces = NUM_INTERFACES;
	config_instance->configuration_descriptor =
	        (struct usb_configuration_descriptor *)&fbt_config_desc;
	config_instance->interface_instance_array = interface_instance;

	/* XXX: is alternate instance required in case of no alternate ? */
	/* interface instance */
	memset (interface_instance, 0,
	        sizeof (struct usb_interface_instance));
	interface_instance->alternates = 1;
	interface_instance->alternates_instance_array = alternate_instance;

	/* alternates instance */
	memset (alternate_instance, 0,
	        sizeof (struct usb_alternate_instance));
	alternate_instance->interface_descriptor = interface_descriptors;
	alternate_instance->endpoints = NUM_ENDPOINTS;
	alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;

	/* endpoint instances */
	memset (&endpoint_instance[0], 0,
	        sizeof (struct usb_endpoint_instance));
	endpoint_instance[0].endpoint_address = 0;
	endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
	endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
	endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
	endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
	/* XXX: following statement to done along with other endpoints
		at another place ? */
	udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
	for (i = 1; i <= NUM_ENDPOINTS; i++) {

		memset (&endpoint_instance[i], 0,
		        sizeof (struct usb_endpoint_instance));

		endpoint_instance[i].endpoint_address =
		        ep_descriptor_ptrs[i - 1]->bEndpointAddress;

		endpoint_instance[i].rcv_attributes =
		        ep_descriptor_ptrs[i - 1]->bmAttributes;
		if (i == 1)
			endpoint_instance[i].rcv_packetSize = 512;
		else
			endpoint_instance[i].rcv_packetSize = 512 ;

		endpoint_instance[i].tx_attributes =
		        ep_descriptor_ptrs[i - 1]->bmAttributes;
		if (i == 1)
			endpoint_instance[i].tx_packetSize = 512;
		else
			endpoint_instance[i].tx_packetSize = 512 ;

		endpoint_instance[i].tx_attributes =
		        ep_descriptor_ptrs[i - 1]->bmAttributes;

		urb_link_init (&endpoint_instance[i].rcv);
		urb_link_init (&endpoint_instance[i].rdy);
		urb_link_init (&endpoint_instance[i].tx);
		urb_link_init (&endpoint_instance[i].done);

		if (endpoint_instance[i].endpoint_address & USB_DIR_IN) {
			endpoint_instance[i].tx_urb =
			        usbd_alloc_urb (device_instance,
			                        &endpoint_instance[i]);
		} else {
			endpoint_instance[i].rcv_urb =
			        usbd_alloc_urb (device_instance,
			                        &endpoint_instance[i]);
		}
	}

	return 0;
}

/* XXX: ep_descriptor_ptrs can be removed by making better use of
	fbt_config_desc.endpoint_desc */
static int fbt_init_endpoint_ptrs(void)
{
	FBTDBG("%s\n", __FUNCTION__);

	ep_descriptor_ptrs[0] = &fbt_config_desc.endpoint_desc[0];
	ep_descriptor_ptrs[1] = &fbt_config_desc.endpoint_desc[1];
	ep_descriptor_ptrs[2] = &fbt_config_desc.endpoint_desc[2];

	return 0;
}

static int fbt_init_endpoints(void)
{
	int i;
	FBTDBG("%s\n", __FUNCTION__);

	/* XXX: should it be moved to some other function ? */
	bus_instance->max_endpoints = NUM_ENDPOINTS + 1;

	/* XXX: is this for loop required ?, yes for MUSB it is */
	for (i = 1; i <= NUM_ENDPOINTS; i++) {

		/* configure packetsize based on HS negotiation status */
		if (device_instance->speed == USB_SPEED_FULL) {
			FBTINFO("setting up FS USB device ep%x\n",
			        endpoint_instance[i].endpoint_address);
			ep_descriptor_ptrs[i - 1]->wMaxPacketSize =
			        CONFIG_USBD_FASTBOOT_BULK_PKTSIZE_FS;
		} else if (device_instance->speed == USB_SPEED_HIGH) {
			FBTINFO("setting up HS USB device ep%x\n",
			        endpoint_instance[i].endpoint_address);
			ep_descriptor_ptrs[i - 1]->wMaxPacketSize =
			        CONFIG_USBD_FASTBOOT_BULK_PKTSIZE_HS;
		}

		endpoint_instance[i].tx_packetSize =
		        le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);
		endpoint_instance[i].rcv_packetSize =
		        le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);

		udc_setup_ep(device_instance, i, &endpoint_instance[i]);

	}

	return 0;
}

static struct urb *next_urb (struct usb_device_instance *device,
                             struct usb_endpoint_instance *endpoint) {
	struct urb *current_urb = NULL;
	int space;
	FBTDBG("%s\n", __FUNCTION__);

	/* If there's a queue, then we should add to the last urb */
	if (!endpoint->tx_queue) {
		current_urb = endpoint->tx_urb;
	} else {
		/* Last urb from tx chain */
		current_urb =
		        p2surround (struct urb, link, endpoint->tx.prev);
	}

	/* Make sure this one has enough room */
	space = current_urb->buffer_length - current_urb->actual_length;
	if (space > 0) {
		return current_urb;
	} else {		/* No space here */
		/* First look at done list */
		current_urb = first_urb_detached (&endpoint->done);
		if (!current_urb) {
			current_urb = usbd_alloc_urb (device, endpoint);
		}

		urb_append (&endpoint->tx, current_urb);
		endpoint->tx_queue++;
	}
	return current_urb;
}

/* FASBOOT specific */
void fastboot_flash_reset_ptn(void)
{
	pcount = 0;
}

/*
 * Android style flash utilties */
void fastboot_flash_add_ptn(fastboot_ptentry *ptn)
{
	if(pcount < MAX_PTN) {
		memcpy(ptable + pcount, ptn, sizeof(*ptn));
		pcount++;
	}
}

void fastboot_flash_dump_ptn(void)
{
	unsigned int n;
	for(n = 0; n < pcount; n++) {
		fastboot_ptentry *ptn = ptable + n;
		printf("dump ptn %d name='%s' start=%d len=%d\n",
		       n, ptn->name, ptn->start, ptn->length);
	}
}
/*support GPT partiton*/
fastboot_ptentry *fastboot_flash_find_ptn(const char *name)
{
	unsigned int n;

	for(n = 0; n < pcount; n++) {
		FBTINFO("strlen(name):%d, ptable[%d].name:%s\n",
		        strlen(name), n, ptable[n].name);

		/* Make sure a substring is not accepted */
		if (strlen(name) == strlen(ptable[n].name)) {
			if(0 == strcmp(ptable[n].name, name))
				return ptable + n;
		}
	}
	return 0;
}

fastboot_ptentry *fastboot_flash_find_nand_ptn(int part_no)
{
	unsigned int n, i;
	n = part_no;
	for (i = 0; i <= part_no; i++)
		FBTINFO("ptable[%d].name:%s,ptable[%d].start:0x%x,ptable[%d].length:0x%x\n",
		        i, ptable[i].name, i, ptable[i].start, i, ptable[i].length);
	return ptable + n;

}


static int get_partition_name(const char *src, char **endpp, char *buffer)
{
	int i = 0;
	if(NULL == src || NULL == buffer) {
		return -1;
	}

	while(*src != ':') {
		*buffer++ = *src++;
		i++;
	}
	*endpp = (char *)src;
	buffer[i] = '\0';
	return i;
}

static int get_partition_idx(char *string, int *ret)
{
	int i, err = -1;
	for (i = 0; i < NF_PART_MAX; i++) {
		//	printk(KERN_DEBUG "MTD dev%d size: %8.8llx \"%s\"\n",
		//i, nand_partitions[i].size, nand_partitions[i].name);
		if (strcmp(string, ptable[i].name) == 0) {
			*ret = i;
			break;
		}
	}
	return err;
}

void init_nand_partitions(void)
{
	char partition_name[32];
	char *pstr, *s = NULL, *tmp = NULL;
	struct nand_chip *chip = get_current_nand_chip();
	__u64 part_size, start_off, chip_size;
	int index, i;

	if(chip == NULL) return;

	if(chip->mfr == NAND_HYNIX) {
		//hynix nand flash support eslc , which have different partition table
		ptable[NF_PART_LOGO].length = 0x02000000;//32M
		ptable[NF_PART_BOOT].length = 0x02000000;//32M
		ptable[NF_PART_RECOVERY].length = 0x02000000;//32M
		if(chip->realplanenum == 1) {
			ptable[NF_PART_LOGO].length = 0x04000000;//64M
			ptable[NF_PART_BOOT].length = 0x04000000;//64M
			ptable[NF_PART_RECOVERY].length = 0x04000000;//64M
			if(chip->dwECCBitNum == 60) {
				ptable[NF_PART_LOGO].length = 0x03800000;//56M
				ptable[NF_PART_BOOT].length = 0x03800000;//56M
				ptable[NF_PART_RECOVERY].length = 0x03800000;//56M
				ptable[NF_PART_MISC].length = 0x01c00000;
				ptable[NF_PART_KEYDATA].length = 0x7000000;
				ptable[NF_PART_SYSTEM].length = 0x31000000;
				ptable[NF_PART_CACHE].length = 0x21400000;
				ptable[NF_PART_SWAP].length = 0x1c000000;
				ptable[NF_PART_DATA].length = MTDPART_SIZ_FULL;
			}
		}
	}

	chip_size = (__u64)(chip->numchips > 1 ? chip->numchips : 1) * (__u64)chip->dwBlockCount * (__u64)chip->dwPageCount * (__u64)chip->oobblock;   

	pstr = getenv("wmt.nand.parition");
	if(pstr) {
		s = pstr;
		while(*s != '\0') {
			index = NF_PART_MAX;
			memset(partition_name, 0, 32);
			get_partition_name(s, &tmp, partition_name);
			get_partition_idx(partition_name, &index);
			s = tmp + 1;
			part_size = simple_strtoul(s, &tmp, 16);
			s = tmp;
			if(*s == ':')
				s++;

			//data can't be resized by uboot env, its size is left whole nand.
			if((index >= 0) &&  (index < NF_PART_MAX) && (part_size < chip_size)) {
				ptable[index].length = part_size;
			} else {
				printf("Invalid parameter \"wmt.nand.partition\". Use default partition size for \"%s\" partition.\n", partition_name);
			}
		}
	}


	start_off = 0;
	for(i = 0; i < NF_PART_MAX; i++) {
		ptable[i].start = start_off;
		start_off += ptable[i].length;
	}

	if(NF_PART_MAX > 0) {
		if(ptable[NF_PART_MAX - 1].length == MTDPART_SIZ_FULL) {
			ptable[NF_PART_MAX - 1].length = chip_size - ptable[NF_PART_MAX - 1].start;
		}
	}

	printf("Nand Partitions: 0x%x x 0x%x x 0x%x = 0x%04x%08x\n",
	       chip->dwBlockCount, chip->dwPageCount, chip->oobblock, (unsigned long)(chip_size >> 32), (unsigned long)chip_size);
	for(i = 0; i < NF_PART_MAX; i++) {
		//uboot printf have problem with __u64
		start_off = ptable[i].start + ptable[i].length;
		printf("0x%04x%08x -  0x%04x%08x \"%s\"\n", (unsigned long)(ptable[i].start >> 32), (unsigned long)ptable[i].start,
		       (unsigned long)(start_off >> 32), (unsigned long)start_off, ptable[i].name);
	}
}

extern block_dev_desc_t *get_dev (char *, int);
extern int mmc_init(int verbose, int device_num);
unsigned int  part_no = -1; //init
unsigned char rsa_check_flag = 0;
unsigned char logo_name = 0;
int sys_sub_image = -1;
unsigned char sub_sys_lt_256M = 0;
unsigned char sub_sys_cnt = -1;

fastboot_ptentry *fastboot_flash_get_ptn(unsigned int n)
{
	if(n < pcount) {
		return ptable + n;
	} else {
		return 0;
	}
}

unsigned int fastboot_flash_get_ptn_count(void)
{
	return pcount;
}
static void set_env(char *var, char *val)
{
	char *setenv[4]  = { "setenv", NULL, NULL, NULL, };

	setenv[1] = var;
	setenv[2] = val;

	do_setenv(NULL, 0, 3, setenv);
}

static int write_to_ptn(int part_no, int ss, char *key, int haveoob)
{
	int ret = 1;
	char length[32];
	char wstart[32], wlength[32], addr[32], oobsize[32];
	char *write[6]  = { "nandrw", "w", NULL, NULL, NULL, NULL, };
	static unsigned char last_ss = 0xFF;
	int status;
	unsigned int image_start;
	unsigned int image_size;
	static unsigned int offset = 0;
	static unsigned int res_len = 0;
	struct fastboot_ptentry *ptn;

	ptn = fastboot_flash_find_nand_ptn(part_no);

	write[2] = addr;
	write[3] = wstart;
	write[4] = wlength;

	FBTINFO("Flashing nand part_no:%d\n", part_no);

	/* Which flavor of write to use, WMT not support */

	sprintf(length, "0x%x", ptn->length);

	/* Normal case */
	if ((part_no == NF_PART_SYSTEM) ||
	    (part_no == NF_PART_DATA) ||
	    (part_no == NF_PART_CACHE)) {
		if(haveoob) {
			write[1] = "wo";
			write[5] = oobsize;
		}

		if(sparse_download_size != 0) {
			unsigned long long theaddr = sparse_nand_addr;
			struct nand_chip *chip = get_current_nand_chip();
			image_start = SZ_128M;
			image_size = priv.d_bytes;

			printf("Nand write sparse image, naddr:0x%04x%08x image_start:0x%x image_size:0x%x, oob%d\n",
			       (unsigned long)(sparse_nand_addr >> 32), (unsigned long)sparse_nand_addr, image_start, image_size, haveoob);
			status = WMTSaveImageToNAND2(chip, theaddr, image_start, image_size, haveoob ? FASTBOOT_NAND_OOB_SIZE : 0, 0, &sparse_nand_addr);
			if(!status) {
				last_ss = 0xFF;
				priv.transfer_buffer = (unsigned char *)SZ_128M;
				sprintf(priv.response, "OKAY");
				return 0;
			} else {
				printf("Nand part_no:%d FAILED!\n", part_no);
				sprintf(priv.response, "FAIL nand write FAIL");
				goto fail;
			}
		}
		/*cache part_no = 3, system part_no = 4,userdata part_no = 11*/
		else if (ss == 0xFF) { /* system, userdata, cache with yaffs2 system */

			if (key) {
				/*if len over 256MB ,rsa check 256M*/
				if (priv.d_bytes > SZ_256M)
					status = image_rsa_check((unsigned int)priv.transfer_buffer, 0x10000000, sig_buf, sig_size);
				else
					status = image_rsa_check((unsigned int)priv.transfer_buffer, priv.d_bytes, sig_buf, sig_size);
				if (status != 0 ) {
					printf("part_no:%d rsa check fail, rcode :%x\n", part_no, status);
					sprintf(priv.response, "FAIL nand rsa check FAIL");
					goto fail;
				} else {
					printf("nand part_no:%d rsa check OK\n", part_no);
				}
			}
			image_start = SZ_128M;
			image_size = priv.d_bytes;
			if (last_ss % 2 == 0) {
				image_size = priv.d_bytes + SZ_256M;
			}

			//uboot printf's have problem with __u64
			printf("Nand image last_ss:0x%x , image_start:0x%x, image_size:0x%x, priv.transfer_buffer:0x%x, ptn->start:0x%04x%08x, offset:0x%x, oob:%d\n",
			       last_ss, image_start, image_size, priv.transfer_buffer, (unsigned long)(ptn->start >> 32), (unsigned long)ptn->start, offset, haveoob);
			sprintf(addr, "0x%x", image_start);
			sprintf(wstart, "0x%04x%08x", (unsigned long)((ptn->start + offset) >> 32), (unsigned long)(ptn->start + offset));
			sprintf(wlength, "0x%x", image_size);
			if(haveoob)sprintf(oobsize, "0x%x", FASTBOOT_NAND_OOB_SIZE);

			ret = do_nand(NULL, 0, haveoob ? 6 : 5, write);
			if (!ret) {
				last_ss = 0xFF;
				priv.transfer_buffer = (unsigned char *)SZ_128M;
				printf("Nand part_no:%d DONE!\n", part_no);
				sprintf(priv.response, "OKAY");
				return 0;
			} else {
				printf("Nand part_no:%d FAILED!\n", part_no);
				sprintf(priv.response, "FAIL nand write FAIL");
				goto fail;
			}
		} else { /* sub-system  */
			last_ss = ss;
			image_start = SZ_128M;
			image_size = priv.d_bytes;

			if (ss % 2 == 0) {
				image_start = SZ_128M;
			} else {
				image_start = SZ_128M + SZ_256M;
			}
			priv.transfer_buffer = (unsigned char *)(SZ_128M + 0x10000000 * ((ss % 2) ? 0 : 1));
			printf("Nand sub-system last_ss:0x%x , image_start:0x%x, priv.transfer_buffer:0x%x\n", last_ss, image_start, priv.transfer_buffer);

			if (key) {
				status = image_rsa_check(image_start, image_size, sig_buf, sig_size);
				if (status != 0 ) {
					printf("nand ss-%d rsa check fail, rcode:0x%X\n", ss, status);
					sprintf(priv.response, "FAIL nand rsa check FAIL");
					goto fail;
				} else {
					if (ss % 2 == 0) {
						printf("nand ss-%d rsa check OK\n", ss);
						sprintf(priv.response, "OKAY");
						return 0;
					} else {
						printf("nand ss-%d rsa check OK\n", ss);
					}
				}
			}

			if (ss % 2 != 0) {
				/*boundary need handle page count and bad block number ?*/
				printf("Nand last_ss:0x%x, offset:0x%x, res_len:0x%x\n", last_ss, offset, res_len);
				sprintf(addr, "0x%x", (unsigned char *)(SZ_128M - res_len));
				//uboot printf have __u64 problem, but i didn't fix it here, as i don't know what sub-system's usage
				sprintf(wstart, "0x%x", ptn->start + offset ); //?
				if(haveoob)sprintf(oobsize, "0x%x", FASTBOOT_NAND_OOB_SIZE);
				res_len = 0x20000000 % (0x100000 + 0x80 * 0x40);//mode 1M
				sprintf(wlength, "0x%x", (0x20000000 - res_len));
				offset +=  (0x20000000 - res_len);
				memcpy((void *)(SZ_128M - res_len), (void *)(0x20000000 + SZ_128M - res_len), res_len);

				/* sub-system1,3,5,... */
				ret = do_nand(NULL, 0, haveoob ? 6 : 5, write);

				if (!ret) {
					printf("Writing nand part_no:%d DONE!\n", part_no);
					sprintf(priv.response, "OKAY");
					return 0;
				} else {
					printf("Writing nand part_no:%d FAILED!\n", part_no);
					sprintf(priv.response, "FAIL nand write FAIL");
					goto fail;
				}
			} else {
				/* sub-system0,2,4,... */
				printf("Writing nand part_no:%d DONE!\n", part_no);
				sprintf(priv.response, "OKAY");
				return 0;
			}
		}
	} else {
		/*raw data*/
		if (key && (part_no != 5) && (part_no != 6) ) {
			status = image_rsa_check((unsigned int)priv.transfer_buffer, priv.d_bytes, sig_buf, sig_size);
			if (status != 0 ) {
				printf("nand part_no:%d rsa check fail, rcode :%x\n", part_no, status);
				sprintf(priv.response, "FAIL nand rsa check FAIL");
				goto fail;
			} else {
				printf("nand part_no:%d rsa check OK\n", part_no);
			}
		}
		image_start = SZ_128M;
		image_size = priv.d_bytes;
		offset = 0;
		//uboot printf's have problem with __u64
		printf("Nand  raw image_start:0x%x, image_size:0x%x, priv.transfer_buffer:0x%x, ptn->start:0x%04x%08x\n",
		       image_start, image_size, priv.transfer_buffer, (unsigned long)(ptn->start >> 32), (unsigned long)ptn->start);

		sprintf(addr, "0x%x", image_start);
		sprintf(wstart, "0x%04x%08x", (unsigned long)((ptn->start + offset) >> 32), (unsigned long)(ptn->start + offset));
		sprintf(wlength, "0x%x", image_size);
		ret = do_nand(NULL, 0, 5, write);

		if (!ret) {
			priv.transfer_buffer = (unsigned char *)SZ_128M;
			printf("Writing nand part_no:%d DONE!\n", part_no);
			sprintf(priv.response, "OKAY");
			return 0;
		} else {
			printf("Writing nand part_no:%d FAILED!\n", part_no);
			sprintf(priv.response, "FAIL nand write FAIL");
			goto fail;
		}

	}
fail:
	/* reset variables as fail */
	last_ss = 0xFF;
	priv.transfer_buffer = (unsigned char *)SZ_128M;

	return ret;
}

static unsigned long long memparse(char *ptr, char **retptr)
{
	char *endptr;	/* local pointer to end of parsed string */
	FBTDBG("%s\n", __FUNCTION__);

	unsigned long ret = simple_strtoul(ptr, &endptr, 0);

	switch (*endptr) {
	case 'M':
	case 'm':
		ret <<= 10;
	case 'K':
	case 'k':
		ret <<= 10;
		endptr++;
	default:
		break;
	}

	if (retptr)
		*retptr = endptr;

	return ret;
}
static int add_partition_from_environment(char *s, char **retptr)
{
	unsigned long size;
	unsigned long offset = 0;
	char *name;
	int name_len;
	int delim;
	unsigned int flags;
	struct fastboot_ptentry part;
	FBTDBG("%s\n", __FUNCTION__);

	/* fetch the partition size */
	size = memparse(s, &s);
	if (0 == size) {
		FBTERR("size of partition is 0\n");
		/*for x0,x4 reservered partition , do not return error*/
		//return 1;
	}
	FBTINFO("size of partition is 0x%x\n", size);

	/* fetch partition name and flags */
	flags = 0; /* this is going to be a regular partition */
	delim = 0;
	/* check for offset */
	if (*s == '@') {
		s++;
		offset = memparse(s, &s);
	} else {
		FBTERR("offset of parition is not given\n");
		return 1;
	}
	FBTINFO("offset of partition is 0x%x\n", offset);

	/* now look for name */
	if (*s == '(')
		delim = ')';

	if (delim) {
		char *p;

		name = ++s;
		p = strchr((const char *)name, delim);
		if (!p) {
			FBTERR("no closing %c found in partition name\n", delim);
			return 1;
		}
		name_len = p - name;
		s = p + 1;
	} else {
		FBTERR("no partition name for \'%s\'\n", s);
		return 1;
	}
	FBTINFO("name of partition is %s ,name_len :0x%x\n", name, name_len);

	/* test for options */
	while (1) {
		if (strncmp(s, "i", 1) == 0) {
			flags |= FASTBOOT_PTENTRY_FLAGS_WRITE_I;
			s += 1;
		} else if (strncmp(s, "jffs2", 5) == 0) {
			/* yaffs */
			flags |= FASTBOOT_PTENTRY_FLAGS_WRITE_JFFS2;
			s += 5;
		} else if (strncmp(s, "swecc", 5) == 0) {
			/* swecc */
			flags |= FASTBOOT_PTENTRY_FLAGS_WRITE_SW_ECC;
			s += 5;
		} else if (strncmp(s, "hwecc", 5) == 0) {
			/* hwecc */
			flags |= FASTBOOT_PTENTRY_FLAGS_WRITE_HW_ECC;
			s += 5;
		} else {
			break;
		}
		if (strncmp(s, "|", 1) == 0)
			s += 1;
	}

	/* enter this partition (offset will be calculated later if it is zero at this point) */
	part.length = size;
	part.start = offset;
	part.flags = flags;
	FBTINFO("offset 0x%8.8x, size 0x%8.8x, flags 0x%8.8x\n",
	        part.start, part.length, part.flags);

	if (name) {
		if (name_len >= sizeof(part.name)) {
			FBTERR("partition name is too long\n");
			return 1;
		}
		strncpy(&part.name[0], name, name_len);
		/* name is not null terminated */
		part.name[name_len] = '\0';
	} else {
		FBTERR("no name\n");
		return 1;
	}


	FBTINFO("Adding: %s, offset 0x%8.8x, size 0x%8.8x, flags 0x%8.8x\n",
	        part.name, part.start, part.length, part.flags);
	fastboot_flash_add_ptn(&part);

	/* return (updated) pointer command line string */
	*retptr = s;

	/* return partition table */
	return 0;
}

static int fbt_add_partitions_from_environment(void)
{
	char fbparts[4096], *env;
	FBTDBG("%s\n", __FUNCTION__);

	/*
	 * Place the runtime partitions at the end of the
	 * static paritions.  First save the start off so
	 * it can be saved from run to run.
	 */

	env = getenv("fbparts");
	if (env) {
		unsigned int len;
		len = strlen(env);
		if (len && len < 4096) {
			char *s, *e;
			if (memcmp(env, "WMT.nand:", 9) == 0) {

				memcpy(&fbparts[0], env, len + 1);
				printf("Adding partitions from environment\n");
				s = &fbparts[9];
				e = s + len - 9;

				while (s < e) {
					if (add_partition_from_environment(s, &s)) {
						printf("Abort adding partitions\n");
						/* reset back to static */
						pcount = static_pcount;
						break;
					}
					/* Skip a bunch of delimiters */
					while (s < e) {
						if ((' ' == *s) ||
						    ('\t' == *s) ||
						    ('\n' == *s) ||
						    ('\r' == *s) ||
						    (',' == *s)) {
							s++;
						} else {
							break;
						}
					}
				}
			} else {
				printf("ERROR! illegal fbparts found. \n");
			}

		}
	} else {
		printf("ERROR! no fbparts found. \n");
		return 1;
	}
	printf("pcount:0x%x,static_pcount:0x%x\n", pcount, static_pcount);

	return 0;
}

static void set_serial_number(void)
{
	FBTDBG("%s\n", __FUNCTION__);

	char *dieid = getenv("dieid#");
	if (dieid == NULL) {
		priv.serial_no = "00123";
	} else {
		int len;

		memset(&serial_number[0], 0, 28);
		len = strlen(dieid);
		if (len > 28)
			len = 26;

		strncpy(&serial_number[0], dieid, len);

		priv.serial_no = &serial_number[0];
	}

}

#define  fb_env_name  "wmt.fb.param"
struct fb_operation_t g_fb_param;

static int fbt_fastboot_init(void)
{
	unsigned char status = 0;

	if (parse_fb_param(fb_env_name) == 0) {
		if(g_fb_param.device > 0x01 ) {
			printf("wmt.fb.param device set error!, set 1:0:0:0 for NAND, 1:1:1:0 for EMMC1 \n");
			return -1;
		}
	} else {
		printf("Fastboot device disabled.  \n");
		return -1;
	}

	if (g_fb_param.device == NAND) {
		status = fbt_add_partitions_from_environment();
		if (status)  {
			printf("Warning !Fastboot nand device no partitions.\n");
			printf("Please flash env1 and env2.\n");
		}

		//init nand parrtition here
		init_nand_partitions();
	}

	priv.flag = 0;
	priv.d_size = 0;
	priv.d_bytes = 0;
	priv.u_size = 0;
	priv.u_bytes = 0;
	priv.exit = 0;
	priv.product_name = FASTBOOT_PRODUCT_NAME;
	set_serial_number();

	if (g_fb_param.device == NAND) {

		priv.nand_block_size               = FASTBOOT_NAND_BLOCK_SIZE;
		priv.nand_oob_size                 = FASTBOOT_NAND_OOB_SIZE;
		priv.storage_medium               = NAND;
	} else {
		priv.storage_medium               = EMMC;
		mmc_controller_no = g_fb_param.controller;
		mmc_part_type = g_fb_param.part_type;
		/*CharlesTu,2013.03.28,patch boot press enter before run logocmd*/
		if (mmc_init(1, mmc_controller_no)) {
			printf("mmc%d init failed!\n", mmc_controller_no);
			return -1;
		}

	}
	FBTDBG("%s,priv.storage_medium %x,mmc_controller_no %x\n", __FUNCTION__, priv.storage_medium, mmc_controller_no );

	return 0;
}

static int fbt_handle_erase(int part_no)
{
	char start[32], length[32];
	int status = 0;
	char *erase[5]  = { "nandrw", "erase",  NULL, NULL, NULL, };
	FBTDBG("%s,storage_medium:0x%x \n", __FUNCTION__, priv.storage_medium);
	if (priv.storage_medium == NAND) {
		struct fastboot_ptentry *ptn;
		ptn = fastboot_flash_find_nand_ptn(part_no);

		if (ptn == 0) {
			printf("FAILpartition does not exist\n");
			sprintf(priv.response, "FAILpartition does not exist");
		} else {
			struct nand_chip *chip = get_current_nand_chip();
			unsigned long block_size = chip->dwPageCount * chip->oobblock;
			unsigned long startblk = ptn->start / block_size;
			unsigned long blknum = ptn->length / block_size;
			//FBTINFO("erasing '%s'\n", ptn->name);
			erase[2] = start;
			erase[3] = length;
			printf("erasing '%s',start blk = 0x%x , blk num = 0x%x\n",
			       ptn->name, startblk, blknum);
			sprintf (start, "0x%08x", startblk);
			sprintf (length, "0x%08x",  blknum);
			status = do_nand (NULL, 0, 4, erase);

			if (status) {
				printf("Nand partition '%s' erased FAIL\n", ptn->name);
				sprintf(priv.response, "FAILfailed to erase nand partition");
			} else {
				printf("Nand partition '%s' erased\n", ptn->name);
				sprintf(priv.response, "OKAY");
			}
		}
	}
	return status;
}

extern int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);

/**
emmc:
# /dev/sdx1  system      1024MB   ext4
# /dev/sdx2  boot        16MB    ext4
# /dev/sdx3  recovery    16MB    ext4
# /dev/sdx4  extended partition
# /dev/sdx5  cache       512MB   ext4
# /dev/sdx6  swap        128MB   vfat
# /dev/sdx7  u-boot-logo 2MB     vfat
# /dev/sdx8  kernel-logo 2MB     vfat
# /dev/sdx9  misc        2MB     ext4
# /dev/sdx10 efs         2MB     ext4
# /dev/sdx11 radio       16MB    ext4
# /dev/sdx12 keydata     2MB     ext4
# /dev/sdx13 userdata            ext4
 */
/**
 * return:
 *    Bit[7:0]  :STR_TYPE including STR_EXT4, STR_SIG, STR_ROM, STR_FAT.
 *    Bit[15:8] :Partition No.
 *    Bit[23:16]:Sub information for Partition No.
 *    Bit[31]   :Status bit. 1: Fail, 0: Success.
 */
unsigned int parse_flash_cmd(char *cmdbuf)
{
	char *system_str[] = {
		"sub-system0", "sub-system1", "sub-system2", "sub-system3",
		"sub-system4", "sub-system5", "sub-system6", "sub-system7",
		"sub-system8", "sub-system9", "sub-system10", "sub-system11",
		"sub-system12", "sub-system13", "sub-system14", "sub-system15"
	};
	char *userdata_str[] = {
		"sub-userdata0", "sub-userdata1", "sub-userdata2", "sub-userdata3",
		"sub-userdata4", "sub-userdata5", "sub-userdata6", "sub-userdata7",
		"sub-userdata8", "sub-userdata9", "sub-userdata10", "sub-userdata11",
		"sub-userdata12", "sub-userdata13", "sub-userdata14", "sub-userdata15"
	};
	char *cache_str[] = {
		"sub-cache0", "sub-cache1", "sub-cache2", "sub-cache3",
		"sub-cache4", "sub-cache5", "sub-cache6", "sub-cache7",
		"sub-cache8", "sub-cache9", "sub-cache10", "sub-cache11",
		"sub-cache12", "sub-cache13", "sub-cache14", "sub-cache15"
	};

	char *rom_str[] = {"w-load", "u-boot", "env1", "env2"};
	//char *u_boot_logo_str[] = {"u-boot-logo", "u-boot-logo-1", "u-boot-logo-2", "u-boot-logo-3","u-boot-logo-4", "u-boot-logo-5"};
	//char *kernel_logo_str[] = {"kernel-logo", "kernel-logo-1", "kernel-logo-2", "kernel-logo-3", "kernel-logo-4", "kernel-logo-5"};

	unsigned int i;

	/* For STR_RAM */
	if (memcmp(cmdbuf + 6, "ram:", 4) == 0) {
		return STR_RAM;
	}

	/* For STR_NOFS */
	/* For STR_EXT4, partition == 1 */
	if (strcmp("boot", cmdbuf + 6) == 0) {
		if (priv.storage_medium == EMMC) {
			return (STR_NOFS | (PART_BOOT << 8) | 0xFF0000);
		} else {
			return (STR_NAND | (NF_PART_BOOT << 8) | 0xFF0000);
		}
	}

	/* For STR_EXT4, partition == 1 */
	if (strcmp("recovery", cmdbuf + 6) == 0) {
		if (priv.storage_medium == EMMC)
			return (STR_NOFS | (PART_RECOVERY << 8) | 0xFF0000);
		else
			return (STR_NAND |  (NF_PART_RECOVERY << 8) | 0xFF0000);

	}
	if (strcmp("misc", cmdbuf + 6) == 0) {
		if (priv.storage_medium == EMMC)
			return (STR_NOFS | (PART_MISC << 8) | 0xFF0000);
		else
			return (STR_NAND |  (NF_PART_MISC << 8) | 0xFF0000);

	}
	/*
	if (strcmp("efs", cmdbuf + 6) == 0) {
		if (priv.storage_medium == EMMC)
			return (STR_NOFS | (PART_EFS << 8) | 0xFF0000);
		else
			return (STR_NAND |  (NF_PART_EFS << 8) | 0xFF0000);

	}
	if (strcmp("radio", cmdbuf + 6) == 0) {
		if (priv.storage_medium == EMMC)
			return (STR_NOFS | (PART_RADIO<< 8) | 0xFF0000);
		else
			return (STR_NAND |  (NF_PART_RADIO << 8) | 0xFF0000);

	}
	if (strcmp("swap", cmdbuf + 6) == 0) {
		if (priv.storage_medium == EMMC)
			return (STR_NOFS | (PART_SWAP << 8) | 0xFF0000);
		else
			return (STR_NAND |  (NF_PART_SWAP << 8) | 0xFF0000);

	}
	*/
	if (strcmp("keydata", cmdbuf + 6) == 0) {
		if (priv.storage_medium == EMMC)
			return (STR_NOFS | (PART_KEYDATA << 8) | 0xFF0000);
		else
			return (STR_NAND |  (NF_PART_KEYDATA << 8) | 0xFF0000);

	}
	/* For STR_EXT4, partition == PART_SYSTEM */
	for (i = 0; i < 16; i++)
		if (strcmp(system_str[i], cmdbuf + 6) == 0) {
			if (priv.storage_medium == EMMC)
				return (STR_EXT4 | (PART_SYSTEM << 8) | (i << 16));
			else
				return (STR_NAND | (NF_PART_SYSTEM << 8) | (i << 16));
		}

	if (strcmp("system:ubi", cmdbuf + 6) == 0) {
		return (STR_UBI | (NF_PART_SYSTEM << 8) | 0xFF0000);
	}

	/* For STR_EXT4, partition == 1 */
	if (strcmp("system", cmdbuf + 6) == 0) {
		if (priv.storage_medium == EMMC)
			return (STR_EXT4 | (PART_SYSTEM << 8) | 0xFF0000);
		else
			return (STR_NAND | (NF_PART_SYSTEM << 8) | 0xFF0000);
	}

	/* For STR_EXT4, partition == PART_USERDATA */
	for (i = 0; i < 16; i++) {
		if (strcmp(userdata_str[i], cmdbuf + 6) == 0) {
			if (priv.storage_medium == EMMC)
				return (STR_EXT4 | (PART_USERDATA << 8) | (i << 16));
			else
				return (STR_NAND | (NF_PART_DATA << 8) | (i << 16));
		}
	}

	if (strcmp("data:ubi", cmdbuf + 6) == 0) {
		return (STR_UBI | (NF_PART_DATA << 8) | 0xFF0000);
	}
	/* For STR_EXT4, partition other than PART_USERDATA */
	if (strcmp("data", cmdbuf + 6) == 0) {
		if (priv.storage_medium == EMMC)
			return (STR_EXT4 | (PART_USERDATA << 8) | 0xFF0000);
		else
			return (STR_NAND | (NF_PART_DATA << 8) | 0xFF0000);

	}




	/* For STR_EXT4, partition == PART_CACHE */
	for (i = 0; i < 16; i++) {
		if (strcmp(cache_str[i], cmdbuf + 6) == 0) {
			if (priv.storage_medium == EMMC)
				return (STR_EXT4 | (PART_CACHE << 8) | (i << 16));
			else
				return (STR_NAND | (NF_PART_CACHE << 8) | (i << 16));
		}
	}
	if (strcmp("cache", cmdbuf + 6) == 0) {
		if (priv.storage_medium == EMMC)
			return (STR_EXT4 | (PART_CACHE << 8) | 0xFF0000);
		else
			return (STR_NAND | (NF_PART_CACHE << 8) | 0xFF0000);

	}

	/* For STR_SIG */
	if (strcmp("sig", cmdbuf + 6) == 0) {
		return STR_SIG;
	}

	/* For STR_ROM */
	for (i = 0; i < 4; i++)
		if (strcmp(rom_str[i], cmdbuf + 6) == 0)
			return (STR_ROM | (i << 8));

	if (strcmp("logo", cmdbuf + 6) == 0) {
		if (priv.storage_medium == EMMC)
			return (STR_EXT4 | (PART_U_BOOT_LOGO << 8) | 0xFF0000);
		else
			return (STR_NAND | (NF_PART_LOGO << 8) | 0xFF0000);

	}
	/* For STR_FAT(u_boot_logo) */
	/*
	for (i = 0;i < 6; i++) {
		if (strcmp(u_boot_logo_str[i], cmdbuf + 6) == 0) {
			if (priv.storage_medium == EMMC)
				return (STR_FAT | (PART_U_BOOT_LOGO << 8) | (i << 16));
			else
				return (STR_NAND | (NF_PART_U_BOOT_LOGO << 8) | (i << 16));
		}
	}
	*/
	/* For STR_FAT(kernel_logo) */
	/*
	for (i = 0; i < 6; i++) {
		if (strcmp(kernel_logo_str[i], cmdbuf + 6) == 0) {
			if (priv.storage_medium == EMMC)
				return (STR_FAT | (PART_KERNEL_LOGO << 8) | (i << 16));
			else
				return (STR_NAND | (NF_PART_KERNEL_LOGO << 8) | (i << 16));

		}
	}
	*/
	return 0x80000000;
}

int handle_flash_nand(int part_no, int ss, char *key, int haveoob)
{
	static unsigned char erase_part = 0;
	struct fastboot_ptentry *ptn;

	/*erase partition first*/
	if ((part_no == NF_PART_SYSTEM) ||
	    (part_no == NF_PART_CACHE) ||
	    (part_no == NF_PART_DATA)) {
		if(sparse_download_size != 0) {
			if(sparse_download_size == sparse_remained_size) {
				fbt_handle_erase(part_no);
				ptn = fastboot_flash_find_nand_ptn(part_no);
				sparse_nand_addr = ptn->start;
			}
			sparse_remained_size -= priv.d_bytes;
		} else {
			if (ss == 0 ) {
				fbt_handle_erase(part_no);
				erase_part = 1;
			}
			if ((ss == 0xff) && (erase_part == 0)) {
				fbt_handle_erase(part_no);
			} else if ((ss == 0xff) && (erase_part == 1)) {
				erase_part = 0;//clear
			}
		}
	} else {
		fbt_handle_erase(part_no);
	}

	if (part_no == NF_PART_DATA) {
		/* erase swap partition first*/
		printf("Erase swap partition\n");
		fbt_handle_erase(NF_PART_SWAP);
	}

	/* Normal case */
	if (write_to_ptn(part_no, ss, key, haveoob)) {
		printf("write to nand part_no:%d FAIL\n", part_no);
	} else {
		sprintf(priv.response, "OKAY");
	}
	return 0;
}

int handle_flash_ext4(int part_no, int ss, char *key)
{
	static unsigned char last_ss = 255;
	int status;
	unsigned int image_start;
	unsigned int image_size;
	block_dev_desc_t *dev_desc = NULL;
	disk_partition_t info;
	char slot_no[32];
	unsigned char init;

	sprintf(slot_no, "%d", mmc_controller_no);

	/*get mmc dev descriptor*/
	dev_desc = get_dev("mmc", mmc_controller_no);
	if (dev_desc == NULL) {
		printf ("mmc Invalid boot device\n");
		goto fail;
	}
	/**
	 * info.start, info.size, info.blksz
	 */
	if(!get_partition_info(dev_desc, part_no, &info)) {
		printf("MBR part_no:%d, part_start:0x%x, part_length:%d MB\n",
		       part_no, info.start, (info.blksz * info.size) / (1024 * 1024));
	} else {
		printf ("** Partition %d not valid on device %d **", part_no, dev_desc->dev);
		sprintf(priv.response, "FAIL partition not valid");
		goto fail;
	}

	if (ss == 0xFF) { /* system, userdata, cache */

		if (key) {
			if (priv.d_bytes > 0x10000000)
				status = image_rsa_check((unsigned int)priv.transfer_buffer, 0x10000000, sig_buf, sig_size);
			else
				status = image_rsa_check((unsigned int)priv.transfer_buffer, priv.d_bytes, sig_buf, sig_size);
			if (status != 0 ) {
				printf("Ext4 rsa check fail, rcode :%x\n", status);
				sprintf(priv.response, "FAIL Ext4 rsa check FAIL");
				goto fail;
			} else {
				printf("Ext4 rsa check OK\n");
			}
		}
		image_start = SZ_128M;
		image_size = priv.d_bytes;
		if (last_ss % 2 == 0) {
			image_size = priv.d_bytes + 0x10000000;
		}
		init = ((last_ss == 0xFF) | (last_ss == 0)) ? 1 : 0; /* system only or system + ss0 or system + ss0 + ssXX */
		if (priv.d_bytes > 0x10000000)
			init = 2;
		if (!do_unsparse(
		            init,
		            (unsigned char *)SZ_128M, //priv.transfer_buffer,
		            info.start,
		            (info.blksz >> 9) * info.size,	/* in unit of 512B */
		            slot_no)) {
			last_ss = 0xFF;
			priv.transfer_buffer = (unsigned char *)SZ_128M;
			printf("Writing sparsed part_no:%d DONE!\n", part_no);
			sprintf(priv.response, "OKAY");
			return 0;
		} else {
			printf("Writing sparsed part_no:%d FAILED!\n", part_no);
			sprintf(priv.response, "FAIL Sparsed Write");
			goto fail;
		}
	} else { /* sub-system */
		last_ss = ss;
		image_start = SZ_128M;
		image_size = priv.d_bytes;

		if (ss % 2 == 0)
			image_start = SZ_128M;
		else
			image_start = SZ_128M + 0x10000000;

		priv.transfer_buffer = (unsigned char *)(SZ_128M + 0x10000000 * ((ss % 2) ? 0 : 1));

		if (key) {
			status = image_rsa_check(image_start, image_size, sig_buf, sig_size);
			if (status != 0 ) {
				printf("ss-%d rsa check fail, rcode:0x%X\n", ss, status);
				sprintf(priv.response, "FAIL image RSA check fail");
				goto fail;
			} else {
				if (ss % 2 == 0) {
					printf("ss-%d rsa check OK\n", ss);
					sprintf(priv.response, "OKAY");
					return 0;
				}
			}
		}
		if (ss % 2 != 0) {
			/* sub-system1,3,5,... */
			if (!do_unsparse(
			            (ss == 1) ? 1 : 0, /* ss0 + ss1, it is necessary to trigger init of unsparse */
			            (unsigned char *)SZ_128M, //priv.transfer_buffer,
			            info.start,
			            (info.blksz >> 9) * info.size,	/* in unit of 512B */
			            slot_no)) {
				printf("Writing sparsed part_no:%d DONE!\n", part_no);
				sprintf(priv.response, "OKAY");
				return 0;
			} else {
				printf("Writing sparsed part_no:%d FAILED!\n", part_no);
				sprintf(priv.response, "FAIL Sparsed Write");
				goto fail;
			}
		} else {
			/* sub-system0,2,4,... */
			printf("Writing sparsed part_no:%d DONE!\n", part_no);
			sprintf(priv.response, "OKAY");
			return 0;
		}


	}
fail:
	/* reset variables as fail */
	last_ss = 0xFF;
	priv.transfer_buffer = (unsigned char *)SZ_128M;

	return 1;
}

static env_t *flash_addr_1 = (env_t *)(CFG_ENV_ADDR);
static env_t *flash_addr_new_2 = (env_t *)(CFG_ENV_ADDR + CFG_ENV_SIZE);
int handle_flash_rom(int part_no, int ss, char *key)
{
	int status;
	char *sf_prot[4] = {"protect", "off", "bank", "1"};
	char *sf_copy[4]  = {"cp.b", NULL, NULL, NULL};
	char *sf_erase[3] = {"erase", NULL, NULL};
	char start_addr[32], dest_addr[32], length[32];
	char spi_addr[32], spi_size[32];
	char *p = NULL;
	static unsigned char flash_env1 = 0, flash_env2 = 0;
	int crc1_ok = 0, crc2_ok = 0;
	FBTINFO("Protect off all spi flash\n" );

	if (key) {
		/*spi flash rsa check*/
		status = image_rsa_check((unsigned int)priv.transfer_buffer, priv.d_bytes, (unsigned int)sig_buf, sig_size);
		if (status != 0 ) {
			printf("Rom rsa check fail, rcode :%x\n", status);
			sprintf(priv.response, "FAIL rom rsa check FAIL");
			return status;  /* rsa verify fail */
		} else {
			printf("Rom rsa check OK\n");
			sprintf(priv.response, "OKAY");
		}
	}

	sf_erase[1] = spi_addr;
	sf_erase[2] = spi_size;
	sf_copy[1] = start_addr;
	sf_copy[2] = dest_addr;
	sf_copy[3] = length;

	status = do_protect(NULL, 0, 4, sf_prot);
	if (status) {
		printf("FAIL protect off all SF\n");
		sprintf(priv.response, "FAIL protect off all SF");
		return 1;
	} else {
		sprintf(priv.response, "OKAY");
	}

	switch (part_no) {
	case ROM_WLOAD : /* w-load */
		sprintf(spi_addr, "0x%x", 0xffff0000);
		sprintf(spi_size, "+%x", priv.d_bytes); // 1 sectors 64KB
		sprintf(start_addr, "0x%x", priv.transfer_buffer);
		sprintf(dest_addr, "0x%x", 0xffff0000);
		sprintf(length, "0x%x", priv.d_bytes);
		break;
	case ROM_UBOOT : /* u-boot */
		sprintf(spi_addr, "0x%x", 0xfff80000);
		sprintf(spi_size, "+%x", priv.d_bytes);
		sprintf(start_addr, "0x%x", priv.transfer_buffer);
		sprintf(dest_addr, "0x%x", 0xfff80000);
		sprintf(length, "0x%x", priv.d_bytes);
		break;
	case ROM_ENV1 : /* env1 */
		sprintf(spi_addr, "0x%x", 0xfffd0000);
		sprintf(spi_size, "+%x", priv.d_bytes);
		sprintf(start_addr, "0x%x", priv.transfer_buffer);
		sprintf(dest_addr, "0x%x", 0xfffd0000);
		sprintf(length, "0x%x", priv.d_bytes);
		flash_env1 = 1;
		break;
	case ROM_ENV2: /* env2 */
		sprintf(spi_addr, "0x%x", 0xfffe0000);
		sprintf(spi_size, "+%x", priv.d_bytes);
		sprintf(start_addr, "0x%x", priv.transfer_buffer);
		sprintf(dest_addr, "0x%x", 0xfffe0000);
		sprintf(length, "0x%x", priv.d_bytes);
		flash_env2 = 1;
		break;
	default :
		printf("FAIL unknown partition no\n");
		sprintf(priv.response, "FAIL unknown partition no");
		return 1;
		break;
	}

	status = do_flerase(NULL, 0, 3, sf_erase);
	if (status) {
		printf("FAIL erase of SF\n");
		sprintf(priv.response, "FAIL erase of SF");
		return 1;
	} else {
		sprintf(priv.response, "OKAY");
	}
	status = do_mem_cp(NULL, 0, 4, sf_copy);
	if (status) {
		sprintf(priv.response, "FAIL copy of SF");
		printf("FAIL copy of SF");
		return 1;
	} else {
		sprintf(priv.response, "OKAY");
        
    if(part_no == ROM_ENV1)
        esync();
#if 0
		if (flash_env1 && flash_env2) {
			FBTINFO("flash_addr_1->data:0x%x, flash_addr_new_2->data:0x%x, ENV_SIZE:0x%x\n",
			        flash_addr_1->data, flash_addr_new_2->data, ENV_SIZE);
			crc1_ok = (crc32(0, flash_addr_1->data, ENV_SIZE) == flash_addr_1->crc);
			if (crc1_ok) {
				if (flash_addr_1->flags) {
					memcpy (env_ptr, (void *)flash_addr_1, CFG_ENV_SIZE);
					p = getenv("fbparts");
					printf("new SF env fbparts= %s, crc1_ok:%x\n", p, crc1_ok);
				} else {
					crc2_ok = (crc32(0, flash_addr_new_2->data, ENV_SIZE) == flash_addr_new_2->crc);
					if (crc2_ok) {
						if (flash_addr_new_2->flags) {
							memcpy (env_ptr, (void *)flash_addr_new_2, CFG_ENV_SIZE);
							p = getenv("fbparts");
							printf("new SF env fbparts= %s,crc2_ok:%x\n", p, crc2_ok);
						}
					}
				}
				if ((g_fb_param.device == NAND) && p) {
					status = fbt_add_partitions_from_environment();
					if (status)
						return 1;
				}
			}
			flash_env1 = 0;
			flash_env2 = 0;
		}
#endif
	}
	return 0;
}

int handle_flash_fat(int part_no, int ss, char *key)
{
	int status;
	char slot_no[32], source[32], length[32];
	char *fat_write[6]  = {"fatstore", NULL, NULL, NULL, NULL, NULL};

	if (key) {
		/*spi flash rsa check*/
		status = image_rsa_check((unsigned int)priv.transfer_buffer, priv.d_bytes, (unsigned int)sig_buf, sig_size);
		if (status != 0 ) {
			printf("Fat rsa check fail, rcode :%x\n", status);
			sprintf(priv.response, "FAIL image RSA Check Fail");
			return status;  /* rsa verify fail */
		} else {
			printf("Fat rsa check OK\n");
			sprintf(priv.response, "OKAY");
		}
	}

	sprintf(slot_no, "%d", mmc_controller_no);

	fat_write[1] = "mmc";
	fat_write[2] = slot_no;
	fat_write[3] = source;
	fat_write[5] = length;

	sprintf(slot_no, "%d:%d", mmc_controller_no, part_no);
	sprintf(source, "0x%x", priv.transfer_buffer);
	sprintf(length, "0x%x", priv.d_bytes);

	if (part_no == PART_U_BOOT_LOGO) {
		if (ss == 0)
			fat_write[4] = "u-boot-logo.data";
		else if (ss == 1)
			fat_write[4] = "u-boot-logo-1.data";
		else if (ss == 2)
			fat_write[4] = "u-boot-logo-2.data";
		else if (ss == 3)
			fat_write[4] = "u-boot-logo-3.data";
		else if (ss == 4)
			fat_write[4] = "u-boot-logo-4.data";
		else if (ss == 5)
			fat_write[4] = "u-boot-logo-5.data";
	} else {
		if (ss == 0)
			fat_write[4] = "kernel-logo.data";
		else if (ss == 1)
			fat_write[4] = "kernel-logo-1.data";
		else if (ss == 2)
			fat_write[4] = "kernel-logo-2.data";
		else if (ss == 3)
			fat_write[4] = "kernel-logo-3.data";
		else if (ss == 4)
			fat_write[4] = "kernel-logo-4.data";
		else if (ss == 5)
			fat_write[4] = "kernel-logo-5.data";
	}

	if (do_fat_fsstore(NULL, 0, 6, fat_write)) {
		printf("Writing part_no:'%d' FAILED!\n", part_no);
		sprintf(priv.response, "FAIL write logo");
		return 1;
	} else {
		printf("Writing part_no:'%d' DONE!\n", part_no);
		sprintf(priv.response, "OKAY");
	}

	return 0;
}

int handle_flash_nofs(int part_no, int ss, char *key)
{
	int status;
	block_dev_desc_t *dev_desc = NULL;
	disk_partition_t info;
	char slot_no[32], source[32], dest[32], length[32];
	char *mmc_write[6]  = {"mmc", NULL, "write", NULL, NULL, NULL};

	/*get mmc dev descriptor*/
	dev_desc = get_dev("mmc", mmc_controller_no);
	if (dev_desc == NULL) {
		printf ("mmc Invalid boot device \n");
		goto fail;
	}

	/**
	 * info.start, info.size, info.blksz
	 */
	if(!get_partition_info(dev_desc, part_no, &info)) {
		printf("MBR part_no:%d, info.start:0x%x, length:%d MB \n",
		       part_no, info.start, info.size * info.blksz / (1024 * 1024));
	} else {
		printf ("** Partition %d not valid on device %d **",
		        part_no, dev_desc->dev);
		goto fail;
	}
	/*no need to do rsa check*/
	if (0) {
		status = image_rsa_check((unsigned int)priv.transfer_buffer, priv.d_bytes, (unsigned int)sig_buf, sig_size);
		if (status != 0 ) {
			printf("Nofs rsa check fail, rcode :%x\n", status);
			sprintf(priv.response, "FAIL nofs rsa check FAIL");
			return status;  /* rsa verify fail */
		} else {
			printf("Nofs rsa check OK\n");
			sprintf(priv.response, "OKAY");
		}
	}

	sprintf(slot_no, "%d", mmc_controller_no);

	mmc_write[1] = slot_no;
	mmc_write[2] = source;
	mmc_write[3] = dest;
	mmc_write[4] = length;

	sprintf(slot_no, "%d:%d", mmc_controller_no, part_no);
	sprintf(source, "0x%x", priv.transfer_buffer);
	sprintf(dest, "0x%x", info.start);
	sprintf(length, "0x%x", priv.d_bytes);

	if (do_mmc_write(NULL, 0, 5, mmc_write)) {
		printf("Writing part_no:'%d' FAILED!\n", part_no);
		sprintf(priv.response, "FAIL write partition");
	} else {
		printf("Writing part_no:'%d' DONE!\n", part_no);
		sprintf(priv.response, "OKAY");
	}
fail:
	return 0;
}

int fbt_handle_flash(char *cmdbuf)
{
	unsigned int flash_cmd_sts, ram_addr, cp_loop, i;
	unsigned char str_type, part_no, ss;
	char *key = NULL;
	char new_file[64];

	printf("\ncmd:[%s]\n", cmdbuf);
	key = getenv("wmt.rsa.pem");
	if (!key)
		printf("No RSA public key !! \n");

	flash_cmd_sts = parse_flash_cmd(cmdbuf);
	if (flash_cmd_sts & 0x80000000) {
		printf("FAIL parse flash cmd fail\n");
		sprintf(priv.response, "FAIL parse flash cmd FAIL");
		return 1;
	}

	str_type = flash_cmd_sts & 0xFF;
	part_no = (flash_cmd_sts & 0xFF00) >> 8;
	ss = (flash_cmd_sts & 0xFF0000) >> 16;

	printf("str_type:%d, part_no:%d, ss:%d\n", str_type, part_no, ss);

	switch (str_type) {
	case STR_EXT4 :
		handle_flash_ext4(part_no, ss, key);
		break;
	case STR_SIG :
		memset((void *)sig_buf, 0, priv.d_bytes);
		memcpy((void *)sig_buf, priv.transfer_buffer, priv.d_bytes);
		sig_size = priv.d_bytes;
		printf("cmd:[%s]:OKAY,sig_size:0x%x\n", cmdbuf, sig_size);
		sprintf(priv.response, "OKAY");
		break;
	case STR_ROM :
		handle_flash_rom(part_no, ss, key);
		break;
	case STR_FAT :
		handle_flash_fat(part_no, ss, key);
		break;
	case STR_NOFS :
		handle_flash_nofs(part_no, ss, key);
		break;
	case STR_RAM :
		ram_addr =  simple_strtoul ((void *)(cmdbuf + 10), NULL, 16);
		/*uboot u-boot.map start from 0x03f80000 to 0x0414990c*/
		if ((ram_addr >= 0x03f80000) && (ram_addr <= 0x04200000)) {
			printf("cmd:[%s]:FAIL, ram addr between u-boot.map, please change ram addr.\n", cmdbuf);
			sprintf(priv.response, "FAILu-boot.map address");
		} else {
			if (ram_addr == 0x8000000) {
				printf("ram_addr = 0x8000000, no need copy\n");
			} else if ((ram_addr > 0x8000000) && (ram_addr <= (0x8000000 + priv.d_bytes))) {
				cp_loop = priv.d_bytes / 32;
				if (priv.d_bytes % 32)
					cp_loop++;
				for (i = cp_loop; i > 0; i--)
					memcpy((void *)ram_addr + (i - 1) * 32  , priv.transfer_buffer + (i - 1) * 32, 32);
			} else {
				memcpy_itp((void *)ram_addr, priv.transfer_buffer, priv.d_bytes);
			}
			printf("cmd:[%s]:OKAY,ram addr:0x%x,size:0x%x\n", cmdbuf, ram_addr, priv.d_bytes);
			sprintf(priv.response, "OKAY");
			sprintf(new_file, "%lX", priv.d_bytes);
			setenv("filesize", new_file);
		}

		break;
	case STR_NAND :
		handle_flash_nand(part_no, ss, key, 1);
		break;
	case STR_UBI:
		handle_flash_nand(part_no, ss, key, 0);
		break;
	default:
		printf("FAIL unknown storage type\n");
		sprintf(priv.response, "FAIL unknown storage type");
		break;
	}
	return 0;
}

static int fbt_handle_getvar(char *cmdbuf)
{
	FBTDBG("%s\n", __FUNCTION__);

	strcpy(priv.response, "OKAY");
	if (!memcmp(cmdbuf + strlen("getvar:"), "version", 7)) {
		FBTINFO("getvar version\n");
		strcpy(priv.response + 4, FASTBOOT_VERSION);
	}
	if (!memcmp(cmdbuf + strlen("getvar:"), "version-bootload", 16)) {
		FBTINFO("getvar version-bootloader\n");
		strcpy(priv.response + 4, fb_version_bootloader);
	}
	if (!memcmp(cmdbuf + strlen("getvar:"), "version-baseband", 16)) {
		FBTINFO("getvar version-baseband\n");
		strcpy(priv.response + 4, fb_version_baseband);
	}
	if (!memcmp(cmdbuf + strlen("getvar:"), "secure", 6)) {
		FBTINFO("getvar secure\n");
		strcpy(priv.response + 4, SECURE);
	}
	if (!memcmp(cmdbuf + strlen("getvar:"), "product", 7)) {
		FBTINFO("getvar product\n");
		if (priv.product_name)
			strcpy(priv.response + 4, priv.product_name);
	}
	if (!memcmp(cmdbuf + strlen("getvar:"), "serialno", 8)) {
		FBTINFO("getvar serialno\n");
		if (priv.serial_no)
			strcpy(priv.response + 4, priv.serial_no);
	}
	priv.flag |= FASTBOOT_FLAG_RESPONSE;

	return 0;
}
static int fbt_handle_reboot(char *cmdbuf)
{
	FBTDBG("%s\n", __FUNCTION__);

	strcpy(priv.response, "OKAY");
	priv.flag |= FASTBOOT_FLAG_RESPONSE;
	fbt_handle_response();
	udelay (1000000); /* 1 sec */
	disable_interrupts();
	reset_cpu (0);
	return 0;
}

static int fbt_handle_boot(char *cmdbuf)
{
	FBTDBG("%s\n", __FUNCTION__);

	if ((priv.d_bytes) &&
	    (CONFIG_FASTBOOT_MKBOOTIMAGE_PAGE_SIZE < priv.d_bytes)) {
		char start[32];
		char *bootm[3] = { "bootm", NULL, NULL, };
		char *go[3]    = { "go",    NULL, NULL, };

		/*
		 * Use this later to determine if a command line was passed
		 * for the kernel.
		 */
		struct fastboot_boot_img_hdr *fb_hdr =
		        (struct fastboot_boot_img_hdr *) priv.transfer_buffer;

		/* Skip the mkbootimage header */
		image_header_t *hdr = (image_header_t *)
		                      &priv.transfer_buffer[CONFIG_FASTBOOT_MKBOOTIMAGE_PAGE_SIZE];

		bootm[1] = go[1] = start;
		sprintf (start, "0x%x", hdr);

		/* Execution should jump to kernel so send the response
		   now and wait a bit.  */
		sprintf(priv.response, "OKAY");
		priv.flag |= FASTBOOT_FLAG_RESPONSE;
		fbt_handle_response();
		udelay (1000000); /* 1 sec */

		if (ntohl(hdr->ih_magic) == IH_MAGIC) {
			/* Looks like a kernel.. */
			FBTINFO("Booting kernel..\n");

			/*
			 * Check if the user sent a bootargs down.
			 * If not, do not override what is already there
			 */
			if (strlen ((char *) &fb_hdr->cmdline[0]))
				set_env ("bootargs", (char *) &fb_hdr->cmdline[0]);

			do_bootm (NULL, 0, 2, bootm);
		} else {
			/* Raw image, maybe another uboot */
			FBTINFO("Booting raw image..\n");

			do_go (NULL, 0, 2, go);
		}

		FBTERR("booting failed, reset the board\n");
	}
	sprintf(priv.response, "FAILinvalid boot image");

	return 0;
}

#ifdef	FASTBOOT_UPLOAD
static int fbt_handle_upload(char *cmdbuf)
{
	unsigned int adv, delim_index, len;
	struct fastboot_ptentry *ptn;
	unsigned int is_raw = 0;

	/* Is this a raw read ? */
	if (memcmp(cmdbuf, "uploadraw:", 10) == 0) {
		is_raw = 1;
		adv = 10;
	} else {
		adv = 7;
	}

	/* Scan to the next ':' to find when the size starts */
	len = strlen(cmdbuf);
	for (delim_index = adv;	delim_index < len; delim_index++) {
		if (cmdbuf[delim_index] == ':') {
			/* WARNING, cmdbuf is being modified. */
			*((char *) &cmdbuf[delim_index]) = 0;
			break;
		}
	}

	ptn = fastboot_flash_find_ptn(cmdbuf + adv);
	if (ptn == 0) {
		sprintf(priv.response, "FAIL partition does not exist");
	} else {
		/* This is how much the user is expecting */
		unsigned int user_size;
		/*
		 * This is the maximum size needed for
		 * this partition
		 */
		unsigned int size;
		/* This is the length of the data */
		unsigned int length;
		/*
		 * Used to check previous write of
		 * the parition
		 */
		char env_ptn_length_var[128];
		char *env_ptn_length_val;

		user_size = 0;
		if (delim_index < len)
			user_size = simple_strtoul(cmdbuf + delim_index +
			                           1, NULL, 16);
		/* Make sure output is padded to block size */
		length = ptn->length;
		sprintf(env_ptn_length_var, "%s_nand_size", ptn->name);
		env_ptn_length_val = getenv(env_ptn_length_var);
		if (env_ptn_length_val) {
			length = simple_strtoul(env_ptn_length_val, NULL, 16);
			/* Catch possible problems */
			if (!length)
				length = ptn->length;
		}
		size = length / priv.nand_block_size;
		size *= priv.nand_block_size;
		if (length % priv.nand_block_size)
			size += priv.nand_block_size;
		if (is_raw)
			size += (size / priv.nand_block_size) *
			        priv.nand_oob_size;
		if (size > priv.transfer_buffer_size) {
			sprintf(priv.response, "FAILdata too large");
		} else if (user_size == 0) {
			/* Send the data response */
			sprintf(priv.response, "DATA%08x", size);
		} else if (user_size != size) {
			/* This is the wrong size */
			sprintf(priv.response, "FAIL");
		} else {
			/*
			 * This is where the transfer
			 * buffer is populated
			 */
			unsigned char *buf = priv.transfer_buffer;
			char start[32], length[32], type[32], addr[32];
			char *read[6] = { "nand", NULL, NULL,
			                  NULL, NULL, NULL,
			                };
			/*
			 * Setting upload_size causes
			 * transfer to happen in main loop
			 */
			priv.u_size = size;
			priv.u_bytes = 0;

			/*
			 * Poison the transfer buffer, 0xff
			 * is erase value of nand
			 */
			memset(buf, 0xff, priv.u_size);
			/* Which flavor of read to use */
			if (is_raw)
				sprintf(type, "read.raw");
			else
				sprintf(type, "read.i");

			sprintf(addr, "0x%x", priv.transfer_buffer);
			sprintf(start, "0x%x", ptn->start);
			sprintf(length, "0x%x", priv.u_size);

			read[1] = type;
			read[2] = addr;
			read[3] = start;
			read[4] = length;

			do_nand(NULL, 0, 5, read);

			/* Send the data response */
			sprintf(priv.response, "DATA%08x", size);
		}
	}

	return 0;
}
#endif

static int fbt_handle_getenv(char *name)
{
	char value[128] = {0};
	int n = -1;
	n =  getenv_f(name, value, sizeof(value));
	if(n > 0 && strlen(value) > 0) {
		sprintf(priv.response, "OKAYvalue=%s", value);
		printf("getenv %s %s\n", name, value);
	}
	return n;
}


/* cmdbuf max size is URB_BUF_SIZE , which is 128 byte */
static int fbt_handle_setenv(char *cmdbuf)
{
	char *p1, *p2;
	char name[64] = {0};
	char value[128] = {0};
	int  nameLen = 0;
	FBTDBG("%s\n", __FUNCTION__);

	strcpy(priv.response, "OKAY");

	p1 = cmdbuf + strlen("setenv:");
	p2 = strstr(p1, "=");
	nameLen = p2 - p1;

	strncpy(name, p1, nameLen);
	strcpy(value, p2 + 1);

	set_env(name, value);

	return 0;
}

static int fbt_handle_syncenv(char *cmdbuf)
{
	char *p1, *p2;
	char envaddr[16] = {0};
	char blacklistaddr[16] = {0};
	char *syncenv[4]  = { "syncenv", NULL, NULL, NULL, };
	int len = 0;

	FBTDBG("%s\n", __FUNCTION__);

	strcpy(priv.response, "OKAY");

	syncenv[1] = envaddr;
	syncenv[2] = blacklistaddr;

	p1 = cmdbuf + strlen("syncenv:");
	p2 = strstr(p1, " ");
	len = p2 - p1;

	strncpy(envaddr, p1, len);
	strcpy(blacklistaddr, p2 + 1);

	do_syncenv(NULL, 0, 3, syncenv);

	return 0;
}

static int wload_buildid(char *ver, int len)
{
	char *wload = (char *)0xffff0000;
	int wload_len = 0x10000;
	int i = 0, found = 0;

	for(; i < wload_len; i++) {
		if(memcmp(wload + i, "BUILDID_", 8) == 0) {
			found = 1;
			break;
		}
	}

	if(found) {
		int begin = i, end = 0;
		int vlen = 0;
		for(end = begin; end < wload_len && (wload[end] != '\0' && wload[end] != ' '); end++);
		if(end >= wload_len || end == begin) return -1;
		vlen = end - begin;
		if(vlen > len) {
			printf("version value is too long\n");
			return -1;
		}
		strncpy(ver, wload + begin, vlen);
		return 0;
	}

	return -1;
}

static void fastboot_led_gpio(int on)
{
	GPIO_ENV led_gpio;
	int ret;

	/*
	*
	* For example:
	* 	set wmt.fastboot.led 7:1
	* 	pull high gpio 7 to light led
	*/
	ret = parse_gpio_env("wmt.fastboot.led", &led_gpio);
	if(ret == 0) {
		if(on) {
			gpio_setpull(led_gpio.gpiono, led_gpio.active ? GPIO_PULL_UP : GPIO_PULL_DOWN);
			gpio_direction_output(led_gpio.gpiono, led_gpio.active ? 1 : 0);
		} else {
			gpio_setpull(led_gpio.gpiono, led_gpio.active ? GPIO_PULL_DOWN : GPIO_PULL_UP);
			gpio_direction_output(led_gpio.gpiono, led_gpio.active ? 0 : 1);
		}
	}
}

extern void show_text_to_screen_no_backlight(char *p_text, unsigned int rgb);

/* XXX: Replace magic number & strings with macros */
static int fbt_rx_process(unsigned char *buffer, int length)
{
	int ret = 1;
	int keycode;
	unsigned int erase_cmd_sts;
	/* Generic failed response */
	strcpy(priv.response, "FAIL");

	if (!priv.d_size) {
		/* command */
		char *cmdbuf = (char *) buffer;
		//{JHT
		*(cmdbuf + length) = 0;
		//}

		printf("receive command '%s'\n", cmdbuf);

		if(memcmp(cmdbuf, "getvar:", 7) == 0) {
			FBTDBG("getvar\n");
			fbt_handle_getvar(cmdbuf);
		}

		else if(memcmp(cmdbuf, "erase:", 6) == 0) {
			//fbt_handle_erase(cmdbuf);
			erase_cmd_sts = parse_flash_cmd(cmdbuf);
			if (erase_cmd_sts & 0x80000000) {
				printf("FAIL parse flash cmd fail\n");
				sprintf(priv.response, "FAIL parse flash cmd FAIL");
				return 1;
			}
			part_no = (erase_cmd_sts & 0xFF00) >> 8;
			//part_no  = simple_strtoul ((void *)(cmdbuf + 6), NULL, 16);
			FBTDBG("erase nand part_no:0x%x\n", part_no);
			fbt_handle_erase(part_no);
		}

		else if(memcmp(cmdbuf, "flash:", 6) == 0) {
			FBTDBG("flash\n");
			fbt_handle_flash(cmdbuf);
		}

		else if((strcmp("reboot", cmdbuf) == 0) ||
		        (strcmp("reboot-bootloader", cmdbuf) == 0)) {
			FBTDBG("reboot/reboot-bootloader\n");
			if (strcmp("reboot-bootloader", cmdbuf) == 0) {
				printf("reboot-bootloader\n");
				*((unsigned int *)0xd8130040) |= 0x100;
			}

			fbt_handle_reboot(cmdbuf);
		}

		else if(memcmp(cmdbuf, "continue", 8) == 0) {
			FBTDBG("continue\n");
			strcpy(priv.response, "OKAY");
			priv.exit = 1;
		}

		else if(memcmp(cmdbuf, "boot", 4) == 0) {
			FBTDBG("boot\n");
			fbt_handle_boot(cmdbuf);
		}

		else if(memcmp(cmdbuf, "download:", 9) == 0) {
			FBTDBG("download\n");

			/* XXX: need any check for size & bytes ? */
			priv.d_size = simple_strtoul (cmdbuf + 9, NULL, 16);
			priv.d_bytes = 0;

			FBTINFO ("Starting download of %d bytes\n",
			         priv.d_size);

			if (priv.d_size == 0) {
				strcpy(priv.response, "FAILdata invalid size");
			} else if (priv.d_size >
			           priv.transfer_buffer_size) {
				priv.d_size = 0;
				strcpy(priv.response, "FAILdata too large");
			} else {
				sprintf(priv.response, "DATA%08x", priv.d_size);
			}

		}

		else if (memcmp(cmdbuf, "oem ", 4) == 0) {
			cmdbuf += 4;
			FBTDBG("oem\n");
			if (priv.storage_medium == EMMC) {

				/* fastboot oem format */
				if(memcmp(cmdbuf, "format", 6) == 0) {
					FBTDBG("oem format\n");

					ret = fastboot_oem(cmdbuf);
					if (ret < 0) {
						strcpy(priv.response, "FAIL");
					} else {
						strcpy(priv.response, "OKAY");
					}
				}

				/* fastboot oem recovery */
				else if(memcmp(cmdbuf, "recovery", 8) == 0) {
					sprintf(priv.response, "OKAY");
					fbt_handle_response();

					while(1);
				}

				/* fastboot oem unlock */
				else if(memcmp(cmdbuf, "unlock", 6) == 0) {
					sprintf(priv.response, "FAIL");
					printf("\nfastboot: oem unlock "\
					       "not implemented yet!!\n");
				}

			}

			if(memcmp(cmdbuf, "nand:", 5) == 0) {
				/*only one nand now*/
				FBTDBG("oem %s\n", cmdbuf);
				char *nandmfr = "UNKNOWN";
				struct nand_chip *chip = get_current_nand_chip();
				tellme_nandinfo(chip);
				strcpy(priv.response, "OKAY");
				switch (chip->mfr) {
				case NAND_HYNIX:
					nandmfr = "HYNIX";
					break;
				case NAND_SAMSUNG:
					nandmfr = "SAMSUNG";
					break;
				case NAND_TOSHIBA:
					nandmfr = "TOSHIBA";
					break;
				case NAND_SANDISK:
					nandmfr = "SANDISK";
					break;
				case NAND_MICRON:
					nandmfr = "MICRON";
					break;
				case NAND_INTEL:
					nandmfr = "INTEL";
					break;
				case NAND_MXIC:
					nandmfr = "MXIC";
					break;
				case NAND_MIRA:
					nandmfr = "MIRA";
					break;
				}
                if(chip->dwECCBitNum == 60) 
                {
                    sprintf(priv.response + 4, "mfr=%s:block=%d:page=%d:oob=%d",
					        nandmfr, chip->l_erasesize, chip->l_oobblock, FASTBOOT_NAND_OOB_SIZE);                
                }
                else
                {                    
                    int num = (chip->realplanenum == 1) ? 2 : 1; 
                    sprintf(priv.response + 4, "mfr=%s:block=%d:page=%d:oob=%d",
                            nandmfr, chip->erasesize * num, chip->oobblock * num, FASTBOOT_NAND_OOB_SIZE);
                }
                printf("nand info chip->realplanenum \n");     
			}

			else if(memcmp(cmdbuf, "setenv:", 7) == 0) {
				fbt_handle_setenv(cmdbuf);
			}

			else if(memcmp(cmdbuf, "saveenv", 7) == 0) {
				char *saveenv[2]  = { "saveenv", NULL,};
				strcpy(priv.response, "OKAY");
				do_saveenv(NULL, 0, 1, saveenv);
			}

			else if(memcmp(cmdbuf, "syncenv:", 8) == 0) {
				fbt_handle_syncenv(cmdbuf);
			}

			else if(memcmp(cmdbuf, "getenv:", 7) == 0) {
				int n = -1;
				char *name = strchr(cmdbuf, ':') + 1;
				if(strlen(cmdbuf) > (name - cmdbuf)) {
					n = fbt_handle_getenv(name);
				}
				if(n < 0)
					strcpy(priv.response, "OKAY");
			}

			else if(memcmp(cmdbuf, "uboot-buildid", 13) == 0) {
				//priv.response is 65 bytes
				sprintf(priv.response, "OKAY%s", WMT_UBOOT_BUILD_ID);
			}

			else if(memcmp(cmdbuf, "wload-buildid", 13) == 0) {
				char wloadbid[64] = {0};

				if(wload_buildid(wloadbid, sizeof(wloadbid)) >= 0) {
					sprintf(priv.response, "OKAY%s", wloadbid);
				}
			}

			else if(memcmp(cmdbuf, "max-download-size", 17) == 0) {
				struct nand_chip *chip = get_current_nand_chip();
				if(memcmp(cmdbuf + 18, "yaffs2", 6) == 0) {
					unsigned long blockSize = 0;
					unsigned long blockSizeWithOOB = 0;                    
                    if(chip->dwECCBitNum == 60) 
                    {
                        blockSize = chip->l_oobblock * chip->dwPageCount;
						blockSizeWithOOB = (chip->l_oobblock + FASTBOOT_NAND_OOB_SIZE) * chip->dwPageCount;
                        printf("yaffs2 max-download-size chip->oobblock=%d,blockSize=%d, max_block=%d\n", chip->l_oobblock, blockSize, (MAX_DOWNLOAD_SIZE / blockSize));
                    }
                    else
                    {
                        int num = (chip->realplanenum == 1) ? 2 : 1; 
                        blockSize = chip->oobblock * num * chip->dwPageCount;
					    blockSizeWithOOB = (chip->oobblock * num + FASTBOOT_NAND_OOB_SIZE) * chip->dwPageCount;
                        printf("yaffs2 max-download-size chip->oobblock=%d,blockSize=%d, max_block=%d\n", chip->oobblock, blockSize, (MAX_DOWNLOAD_SIZE / blockSize));
                    }										
					sprintf(priv.response, "OKAY%d", (MAX_DOWNLOAD_SIZE / blockSize)*blockSizeWithOOB);
				} else {
                    unsigned long blockSize;
                    if(chip->dwECCBitNum == 60) 
                    {
                       blockSize = chip->l_oobblock * chip->dwPageCount;
                    }
                    else
                    {
					   blockSize = chip->oobblock * chip->dwPageCount;
					   blockSize = blockSize * (chip->realplanenum == 1) ? 2 : 1 ;
                    }
					sprintf(priv.response, "OKAY%d", (MAX_DOWNLOAD_SIZE / blockSize)*blockSize);
				}
			}

			else if(memcmp(cmdbuf, "sparsedownload:", 15) == 0) {
				char *p = cmdbuf + 15;
				if(sparse_download_size == 0) {
					sparse_download_size = simple_strtoull(p, NULL, 10);
					sparse_remained_size = sparse_download_size;
					printf("sparese download 0x%04x%08x\n", (unsigned long)(sparse_download_size >> 32), (unsigned long)sparse_download_size);
					strcpy(priv.response, "OKAY");
				} else {
					sparse_remained_size = 0;
					sparse_download_size = 0;
					strcpy(priv.response, "FAIL");
				}
				sparse_nand_addr = 0;
			}

			else if(memcmp(cmdbuf, "sparsedownload end", 18) == 0) {
				if(sparse_remained_size == 0) {
					strcpy(priv.response, "OKAY");
				} else {
					strcpy(priv.response, "FAIL");
					sparse_remained_size = 0;
				}
				sparse_download_size = 0;
				sparse_nand_addr = 0;
			}

			else if(memcmp(cmdbuf, "update_begin", 12) == 0) {
				strcpy(priv.response, "OKAY");
				fastboot_led_gpio(1);
				printf("Fastboot Upgrade Start\n");
				show_text_to_screen_no_backlight("Fastboot Upgrade Start", 0xFF00);
			}

			else if(memcmp(cmdbuf, "update_end", 10) == 0) {
				strcpy(priv.response, "OKAY");
				fastboot_led_gpio(0);
				printf("Upgrade Complete. Please Remove USB adapter\n");
				show_text_to_screen_no_backlight("Upgrade Complete. Please Remove USB adapter", 0xFF00);
				udc_disable();
				udc_disconnect();
				while(1) {
					if(!usb_plugin()) {
						printf("USB Plugout!\nPower off!\n");
						do_wmt_poweroff();
					}

					if (tstc()) {
						keycode = getc();
						if(keycode == 0x0d || keycode == 0x03) {
							if(keycode == 0x0d)
							printf("Got 'Enter' key. Cancel waiting for remove USB adapter\n");
							else
								printf("Got 'Ctrl+C'. Cancel waiting for remove USB adapter\n");

							priv.exit = 1;
							break;
						}
					}

					mdelay(50);
				}
			}

			else if(memcmp(cmdbuf, "enterfastboot", 13) == 0) {
				strcpy(priv.response, "OKAY");
				mptool_is_ready = 1;
			}
			else if(memcmp(cmdbuf, "runcommand:", 11) == 0)
			{
			    char *command = cmdbuf + 11;
				printf("run uboot command :%s\n",command);
				if(command)
				{					
					if (run_command(command, 0) != -1 ){				
					    strcpy(priv.response, "OKAY");				
					}					    
					else{
						strcpy(priv.response, "FAIL");
					}
				}
				else{
					strcpy(priv.response, "FAIL");
            	}
			}
		}

		else if((memcmp(cmdbuf, "upload:", 7) == 0) ||
		        (memcmp(cmdbuf, "uploadraw", 10) == 0)) {
			FBTDBG("upload/uploadraw\n");
			if (g_fb_param.device == NAND) {
#ifdef	FASTBOOT_UPLOAD
				fbt_handle_upload(cmdbuf);
#endif
			}
		}
		priv.flag |= FASTBOOT_FLAG_RESPONSE;
	} else {
		if (length) {
			unsigned int xfr_size;
			xfr_size = priv.d_size - priv.d_bytes;
			if (xfr_size > length)
				xfr_size = length;
			priv.d_bytes += xfr_size;

			//FBTDBG("buffer : 0x%x, reeived bytes: 0x%x, required bytes: 0x%x\n", buffer, priv.d_bytes, priv.d_size);

			if (! (priv.d_bytes % (16 * priv.nand_block_size)))
				printf(".");

			if (priv.d_bytes >= priv.d_size) {
				udc_download_flag = 0;
				priv.d_size = 0;
				strcpy(priv.response, "OKAY");
				priv.flag |= FASTBOOT_FLAG_RESPONSE;
#ifdef	INFO
				printf(".\n");
#endif
				FBTINFO("\ndownloaded %d bytes\n", priv.d_bytes);
			}
		} else
			FBTWARN("empty buffer download\n");
	}

	return 0;
}

extern unsigned char *bulk_out_dma1;
extern int udc_cmd_flag;
extern unsigned int UBE_MAX_DMA;
static int fbt_handle_rx(void)
{
	struct usb_endpoint_instance *ep = &endpoint_instance[TX_EP_INDEX];
	if (ep->rcv_urb->actual_length) {
		FBTDBG("ep->rcv_urb->actual_length: 0x%x\n", ep->rcv_urb->actual_length);
		if (udc_download_flag && !udc_cmd_flag) {
			fbt_rx_process(priv.transfer_buffer + priv.d_bytes, ep->rcv_urb->actual_length);
		} else {
			fbt_rx_process(ep->rcv_urb->buffer, ep->rcv_urb->actual_length);
		}
		if (udc_cmd_flag || (ep->rcv_urb->actual_length < UBE_MAX_DMA)) {
			memset(ep->rcv_urb->buffer, 0, ep->rcv_urb->actual_length);
		}
		ep->rcv_urb->actual_length = 0;
	}

	return 0;
}
#define MIN(a,b) ((a) < (b) ? (a) : (b))
static int fbt_response_process(void)
{
	struct usb_endpoint_instance *ep = &endpoint_instance[RX_EP_INDEX];
	struct urb *current_urb = NULL;
	unsigned char *dest = NULL;
	int n, ret = 0;
	FBTDBG("%s\n", __FUNCTION__);

	current_urb = next_urb (device_instance, ep);
	if (!current_urb) {
		FBTERR("%s: current_urb NULL", __func__);
		return -1;
	}

	dest = current_urb->buffer + current_urb->actual_length;
	n = MIN(FASTBOOT_RESPONSE_BUF_SZ, strlen(priv.response));
	memcpy(dest, priv.response, n);
	current_urb->actual_length += n;
	printf("response urb length: %u (%s)\n", current_urb->actual_length, priv.response);
	if (ep->last == 0) {
		udc_endpoint_write(ep);
	}

	return ret;
}

static int fbt_handle_response(void)
{

	if (priv.flag & FASTBOOT_FLAG_RESPONSE) {
		fbt_response_process();
		priv.flag &= ~FASTBOOT_FLAG_RESPONSE;
	}

	return 0;
}

#ifdef	FASTBOOT_UPLOAD
static int fbt_tx_process(void)
{
	struct usb_endpoint_instance *ep = &endpoint_instance[RX_EP_INDEX];
	struct urb *current_urb = NULL;
	unsigned char *dest = NULL;
	int n = 0, ret = 0;

	current_urb = next_urb (device_instance, ep);
	if (!current_urb) {
		FBTERR("%s: current_urb NULL", __func__);
		return -1;
	}

	dest = current_urb->buffer + current_urb->actual_length;
	n = MIN (64, priv.u_size - priv.u_bytes);
	memcpy(dest, priv.transfer_buffer + priv.u_bytes, n);
	current_urb->actual_length += n;
	if (ep->last == 0) {
		udc_endpoint_write (ep);
		/* XXX: "ret = n" should be done iff n bytes has been
		 * transmitted, "udc_endpoint_write" to be changed for it,
		 * now it always return 0.
		 */
		return n;
	}

	return ret;
}

static int fbt_handle_tx(void)
{
	if (priv.u_size) {
		int bytes_written = fbt_tx_process();

		if (bytes_written > 0) {
			/* XXX: is this the right way to update priv.u_bytes ?,
			 * may be "udc_endpoint_write()" can be modified to
			 * return number of bytes transmitted or error and
			 * update based on hence obtained value
			 */
			priv.u_bytes += bytes_written;
#ifdef	INFO
			/* Inform via prompt that upload is happening */
			if (! (priv.d_bytes % (16 * priv.nand_block_size)))
				printf(".");
			if (! (priv.d_bytes % (80 * 16 * priv.nand_block_size)))
				printf("\n");
#endif
			if (priv.u_bytes >= priv.u_size)
#ifdef	INFO
				printf(".\n");
#endif
			priv.u_size = priv.u_bytes = 0;
			FBTINFO("data upload finished\n");
		} else {
			FBTERR("bytes_written: %d\n", bytes_written);
			return -1;
		}

	}

	return 0;
}
#endif

//--> added by howayhuo for checking if the device's USB connects to PC
static int s_udc_init_ok;

void udc_fastboot_transfer(void)
{
	wmt_udc_irq();
	if (priv.configured) {
		fbt_handle_rx();
		fbt_handle_response();
	}
}

void udc_fastboot_exit(void)
{
	if(s_udc_init_ok)
		udc_fastboot_transfer();

	detect_mptool = 0;

	if(s_udc_init_ok == 0)
		return;

	udc_disable();
	udc_disconnect();
	restore_original_string_serial();
	udc_deinit();
	priv.configured = 0;
	sparse_remained_size = 0;
	sparse_download_size = 0;

	mptool_is_ready = 0;
	detect_mptool = 0;

	s_udc_init_ok = 0;
	printf("Fastboot end\n");
}

int udc_fastboot_init(void)
{
	int ret = 0;

	if(s_udc_init_ok)
		udc_fastboot_exit();

	mptool_is_ready = 0;
	detect_mptool = 1;

	ret = fbt_fastboot_init();
	if (ret  < 0) {
		printf("Fastboot init failure\n");
		detect_mptool = 0;
		s_udc_init_ok = 0;
		return ret;
	}

	ret = fbt_init_endpoint_ptrs();

	if ((ret = udc_init()) < 0) {
		printf("MUSB UDC init failure\n");
		detect_mptool = 0;
		s_udc_init_ok = 0;
		return ret;
	}

	ret = fbt_init_strings();
	ret = fbt_init_instances();

	udc_startup_events (device_instance);
	udc_connect();

	s_udc_init_ok = 1;

	printf("Fastboot initialized\n");

	return 0;
}

int udc_fastboot_is_init(void)
{
	return s_udc_init_ok;
}

int wmt_mptool_ready(void)
{
	return mptool_is_ready;
}

int fastboot_loop(int check_usb_plugin)
{
	int i, ret = 0;
	ret = fbt_fastboot_init();
	if (ret  < 0) {
		printf("Fastboot init failure\n");
		return ret;
	}

	ret = fbt_init_endpoint_ptrs();

	if ((ret = udc_init()) < 0) {
		printf("MUSB UDC init failure\n");
		return ret;
	}

	ret = fbt_init_strings();
	ret = fbt_init_instances();

	udc_startup_events (device_instance);
	udc_connect();

	printf("Fastboot initialized\n");

	i = 0;
	while(1) {
		wmt_udc_irq();
		if (priv.configured) {
			fbt_handle_rx();
			fbt_handle_response();
		}

		priv.exit |= ctrlc();
		if (priv.exit) {
			udc_disable();
			udc_disconnect();
			restore_original_string_serial();
			udc_deinit();
			priv.configured = 0;
			sparse_remained_size = 0;
			sparse_download_size = 0;
			FBTINFO("Fastboot end\n");
			break;
		}

		if(check_usb_plugin) {
			i++;
			if(i == 50) {
				i = 0;
				if(!usb_plugin()) {
					printf("USB adapter removed. Power off!\n");
					do_wmt_poweroff();
				}
			}
		}

		udelay(1000); /* 1 msec of delay */
	}

	return ret;
}

//<-- end add

/* command */
int do_fastboot(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int ret = 0;
	ret = fbt_fastboot_init();
	if (ret  < 0) {
		printf("Fastboot init failure\n");
		return ret;
	}

	ret = fbt_init_endpoint_ptrs();

	if ((ret = udc_init()) < 0) {
		printf("MUSB UDC init failure\n");
		return ret;
	}

	ret = fbt_init_strings();
	ret = fbt_init_instances();

	udc_startup_events (device_instance);
	udc_connect();

	printf("Fastboot initialized\n");

	while(1) {
		wmt_udc_irq();
		if (priv.configured) {
			fbt_handle_rx();
			fbt_handle_response();
#ifdef	FASTBOOT_UPLOAD
			fbt_handle_tx();
#endif
		}
		priv.exit |= ctrlc();
		if (priv.exit) {
			udc_disable();
			udc_disconnect();
			restore_original_string_serial();
			udc_deinit();
			priv.configured = 0;
			sparse_remained_size = 0;
			sparse_download_size = 0;
			FBTINFO("Fastboot end\n");
			break;
		}
		/* TODO: It is a workaround for fastboot file download hang issue */
		udelay(1000); /* 1 msec of delay */
	}

	return ret;
}

int parse_fb_param(char *name)
{
	unsigned char idx_max = 8;
	char *p;
	char ps[idx_max];
	char *endp;
	int i = 0;
	/*default EMMC1*/

	p = getenv(name);
	if (!p) {
		printf("%s is not set(1:0:0:0 for NAND ,1:1:1:0 for EMMC), use default value 1:0:0:0\n", name);
		p = "1:0:0:0";
	} else
		printf("wmt.fb.param  %s\n", p);

	while (i < idx_max) {
		ps[i++] = simple_strtoul(p, &endp, 16);
		if (*endp == '\0')
			break;
		p = endp + 1;
		if (*p == '\0')
			break;
	}
	if (ps[0] == 1) {
		g_fb_param.enable = ps[0];
		g_fb_param.device = ps[1];
		g_fb_param.controller = ps[2];
		g_fb_param.part_type = ps[3];

	} else {
		g_fb_param.enable = 0;
		g_fb_param.device = 0;
		g_fb_param.controller = 0;
		g_fb_param.part_type = 0;
		return -1;
	}
	FBTINFO("g_fb_param.enable:%d, g_fb_param.device:%d, g_fb_param.controller :%d\n", g_fb_param.enable, g_fb_param.device, g_fb_param.controller);
	return 0;
}

U_BOOT_CMD(fastboot, 2,	1, do_fastboot,
           "fastboot- use USB Fastboot protocol\n", NULL);