bl_Update.c
177 KB
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
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2005
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*****************************************************************************
*
* Filename:
* ---------
* bl_Update.c
*
* Project:
* --------
* Bootloader
*
* Description:
* ------------
*
*
* Author:
* -------
* -------
*
*============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
****************************************************************************/
#ifdef __CARD_DOWNLOAD__
#define STATIC
#include <string.h>
#include <bl_types.h>
#include <bl_init.h>
#include <bl_common.h>
#include <bl_features.h>
#include <bl_loader.h>
#include <msdc_adap_bl.h>
#include "bl_update.h"
#include "dcl.h"
#include "ftl.h"
#include "kbd_table.h"
#include "drvpdn.h"
#if defined(_NAND_FLASH_BOOTING_)
#include <nand_fdm.h>
#endif
#ifdef __FS_CARD_DOWNLOAD__
#include "fat_fs.h"
#endif /* __FS_CARD_DOWNLOAD__ */
#ifdef __SV5_ENABLED__
#include "cbr.h"
#include "br_GFH_parser.h"
#include "br_GFH_maui_info.h"
#include "br_GFH_flash_info.h"
#include "br_GFH_error.h"
#endif /* __SV5_ENABLED__ */
#ifdef __FOTA_DM__
#include "custom_img_config.h"
#include "fue_err.h"
#include "fue.h"
#include "SSF_ROMinfo.h"
#include "SSF_ROMInfo_type.h"
#endif
#ifdef __EXT_BOOTLOADER__
/*************************************************************************
* Macro and const definition
*************************************************************************/
//#define __TRANSMISSION_DEBUG__
#define __CDL_SUPPORT_UPDATE_FAT__
#define __AUTO_START_CARD_DOWNLOAD__
#define __CDL_SUPPORT_BOOTCERT_V3__ //It can be removed if bootcert is put into ILB
#define __CDL_SUPPORT_BOOTCERT_V5__ //It can be removed if bootcert is put into CBR
#define TRIGGER_KEY (DEVICE_KEY_DOWN) //This key is customizable. Please lookup kdb_table.h
#define HASH_LEN (20)
#define WORKING_BUF_LEN (16*1024) //Working buffer must larger than GFH + NFB Image header(image record at least)+signatures
#define MAX_BOOTCERT_LEN (1024) //1024 bytes
#define MAX_BOOTCERT_PAGE_NUM (2) //Smallest page size = 512 bytes, thus BOOTCERT will require 2 pages at most.
#ifdef _NAND_FLASH_BOOTING_
#define ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER(x) if(!(x)) {BL_PRINT(LOG_ERROR, "bl_Update ASSERT @ %d\n\r", __LINE__); return BL_CD_ERROR_INVALID_PARAM_IN_NAND_IMAGE_HEADER;}
#endif
#define ASSERT_VALID_PARAM_IN_XIM_BODY(x) if(!(x)) {BL_PRINT(LOG_ERROR, "bl_Update ASSERT @ %d\n\r", __LINE__); return BL_CD_ERROR_INVALID_PARAM_IN_XIM_BODY;}
#define DUMMY_FILENAME "DUMMY"
//Phase definition for displaying progress bar
#define INIT_PHASE 0
#define VERIFY_PHASE 1
#define UPDATE_PHASE 2
#define FINISH_PHASE 3
#ifdef _NAND_FLASH_BOOTING_
//Indexes in image list
#define IL_REGIONINFO_HEAD_OFFSET (0)
#define IL_REGIONINFO_TAIL_OFFSET (1)
//Indexes in extra info
#define EXTRAINFO_PMAUI_IDX (2)
//Indexes in NAND list
#define NANDLIST_PMAUI_IDX (3)
//Indexes in nand image
#define XIM_IMAGE_BOOTLOADER_IDX (0)
#define XIM_IMAGE_EXT_BOOTLOADER_IDX (1)
#ifndef __SV5_ENABLED__
#define XIM_IMAGE_IMAGE_LIST_BLOCK_IDX (2)
#define XIM_MAUI_IDX (3)
#define GetSpaceidByNANDimgid(x) (x - XIM_MAUI_IDX) //x must larger than XIM_MAUI_IDX
#define GetILEidByNANDimgid(x) (x - XIM_MAUI_IDX + ROMINFO_INDEX + 1)
#define GetExtraInfoidByNANDImgid(x) (x - NANDLIST_PMAUI_IDX + EXTRAINFO_PMAUI_IDX)
#define GetExtraInfoidByILEid(x) (x - (ROMINFO_INDEX + 1) + EXTRAINFO_PMAUI_IDX)
#else /* __SV5_ENABLED__ */
#define XIM_CBR_IDX (xim_cbr_index)
#define XIM_MAUI_IDX (xim_maui_index)
#define XIM_NAND_ADDED_NUM (2) //boot_info and main_info
#define GetExtraInfoidByNANDimgid(x) (x - XIM_NAND_ADDED_NUM) //x must larger than XIM_MAUI_IDX
#define GetFlashLayoutidByNANDimgid(x) (x - XIM_MAUI_IDX) //x must larger than XIM_MAUI_IDX
#endif /* __SV5_ENABLED__ */
#endif /* _NAND_FLASH_BOOTING_ */
#define IS_OVERLAP(s1, l1, s2, l2) (((s1)>=(s2) && (s1)<(s2)+(l2)) || ((s2)>=(s1) && (s2)<(s1)+(l1)))
#define REMAPPING_MASK (custom_RAM_baseaddr()-1)
#ifdef __RAM_FLASH_REMAP_DONE_IN_EMI_INIT__
#define MAUI_ROM_START_ADDR (custom_ROM_baseaddr()) //Memory is remapped by HW, no need to remap the address here.
#define ROM_ADDR_MASK (custom_RAM_baseaddr())
#else
#define MAUI_ROM_START_ADDR (custom_ROM_baseaddr() & REMAPPING_MASK)
#define ROM_ADDR_MASK (0)
#endif
#define BOOTLOADER_ROM_REGION_LEN (custom_ROM_baseaddr()-custom_RAM_baseaddr())
/*************************************************************************
* Structure definition
*************************************************************************/
#if defined(_NAND_FLASH_BOOTING_)
typedef struct
{
kal_uint16 page_size;
kal_uint16 block_size;
kal_uint16 plane_size;
kal_uint16 addr_cycle;
kal_uint8 io_width;
kal_uint8 feature_set[3];
kal_uint16 FDM_ver;
kal_uint16 ECC_ver;
#ifndef __SV5_ENABLED__
kal_uint32 FS_Start_FB;
kal_uint32 FS_Max_LB;
kal_uint32 FS_Start_PB;
kal_uint32 FS_FS_PBS;
#else /* __SV5_ENABLED__ */
kal_uint32 IM_Ext_Ver;
kal_uint32 reserve_1;
kal_uint32 reserve_2;
kal_uint32 reserve_3;
kal_uint32 Boot_Info_Addr;
kal_uint32 Main_Info_Addr;
kal_uint32 Control_Info_Addr;
kal_uint16 Boot_Info_Count;
kal_uint16 Main_Info_Count;
#endif /* __SV5_ENABLED__ */
} DL_PACKAGE_NAND_IMAGE_HEADER;
typedef struct
{
kal_uint32 start_block;
kal_uint32 blocks;
kal_uint32 feature_bit;
kal_uint32 max_block;
} DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD;
typedef struct
{
kal_uint32 size;
kal_uint32 log2phy[1];
} DL_PACKAGE_NAND_IMAGE_HEADER_FDM5_MAP_TBL;
typedef struct
{
kal_uint32 dummy;
} DL_PACKAGE_NOR_IMAGE_HEADER;
typedef struct {
kal_uint32 RPB_Size;
kal_uint32 GroupNum;
kal_uint32 PhyicalBlkNum;
kal_uint32 DataBlkNum;
kal_uint32 RvdBlkNum;
kal_uint32 RvdBlkStartAddr;
kal_uint32 RemapStartLogAddr;
kal_uint32 BRMTSize;
kal_uint16 PageSize; // Only useful for ImageMaker. In NAND writer case, this field is reserved.
kal_uint16 BlockSize; // Only useful for ImageMaker. In NAND writer case, this field is reserved.
kal_uint32 RVD[3];
} REGION_PARAM_BLOCK_HEADER;
typedef struct {
kal_uint32 GroupNo:8;
kal_uint32 PhyBlkAddr:24;
} BLOCK_MAPPING_TABLE_ENTRY;
typedef struct {
struct {
kal_uint32 Base;
kal_uint32 Size;
} fs[8];
} FS_LAYOUT;
#endif /* _NAND_FLASH_BOOTING_ */
/*************************************************************************
* External reference definition
*************************************************************************/
#if defined(_NAND_FLASH_BOOTING_)
extern BOOTL_HEADER BLHeader;
extern const kal_char SUPER_BLOCK_PATTERN[];
extern const kal_uint32 IMAGE_LIST_BLOCK_TAIL_PATTERN[];
extern const kal_uint32 IMAGE_LIST_BLOCK_TEMP_PATTERN[];
extern const kal_uint32 IMAGE_LIST_BLOCK_BACKUP_PATTERN[];
extern const kal_uint32 IMAGE_LIST_BLOCK_FOTA_PATTERN[];
extern const kal_uint32 IMAGE_LIST_BLOCK_DLPKG_PATTERN[];
extern kal_bool CompareILBTailTag(const kal_uint32 *pTag1, const kal_uint32 *pTag2);
#else /* _NAND_FLASH_BOOTING_ */
extern kal_uint32 Image$$EXT_READ_WRITE$$ZI$$Limit;
#endif /* _NAND_FLASH_BOOTING_ */
extern BL_Info_Wrapper_st BL_Shared_info;
extern kal_bool bl_Alg_Hash_Init(void);
extern kal_bool bl_Alg_Hash_Append(kal_uint32 addr, kal_uint32 len);
extern kal_bool bl_Alg_Hash_Finish(kal_uint32 digest, kal_uint32 len);
extern kal_bool bl_Alg_Asym_Decrypt(kal_uint32 dest_addr, kal_uint32 src_addr, kal_uint32 len, kal_uint32 key, kal_uint32 key_len);
extern kal_uint32 SST_Get_Platform_ID(kal_uint32 rom_base, kal_uint8 *ver_buf, kal_uint32 len);
extern kal_uint32 SST_Get_MAUI_Feature_Combination(kal_uint32 rom_base, kal_uint32 *pFeatureCombination);
extern kal_uint32 SST_Get_MAUI_Paired_Version(kal_uint32 rom_base);
extern kal_uint32 SST_Get_SW_Version(kal_uint32 rom_base, kal_uint8 *ver_buf, kal_uint32 len);
extern kal_bool CheckFeatureCompatibility(kal_uint32 featureSet);
extern kal_bool BL_kbd_IsKeyPressed(kal_uint8 key);
#ifdef __LCD_DRIVER_IN_BL__
extern void BL_LCDHWInit(void);
extern void BL_LCDSetBackLight(void);
extern void BL_ShowUpdateFirmwareInitBackground(void);
extern void BL_ShowUpdateFirmwareProgress(kal_uint16 percentage);
extern void BL_ShowUpdateFirmwareFail(kal_int32 r, kal_int32 g, kal_int32 b);
extern void BL_ShowUpdateFirmwareOK(void);
#endif /* __LCD_DRIVER_IN_BL__ */
extern kal_uint32 custom_get_CDL_asymmetric_key_len(void);
extern const kal_uint8* custom_get_CDL_asymmetric_key(void);
extern kal_bool custom_CDL_check_dl_package_version(kal_uint8 *romSwVer, kal_uint8 *pkgSwVer, kal_uint8 *pkgLimit);
extern kal_bool custom_CDL_check_dl_platform_id(kal_uint8 *romPID, kal_uint8 *pkgPID, kal_uint8 *pkgLimit);
extern kal_bool custom_CDL_customer_info_check(kal_uint32 *pCustomerInfo, kal_uint32 len);
extern FTL_FuncTbl ftlFuncTbl;
#ifdef __FOTA_DM__
extern kal_uint32 g_maui_image_info_validity;
extern MTK_FOTA_ROM_Info_v1_ST g_maui_image_info;
#endif
/*************************************************************************
* Global variables definition
*************************************************************************/
kal_uint32 page_buffer[MAX_PAGE_SIZE_WITH_SPARE/4]; //Read/write buffer for NAND driver
static kal_uint8 work_buf[WORKING_BUF_LEN]; //Working buffer for GFH, Image Header, signatures
static kal_uint32 dl_package_size = 0;
static kal_uint32 im_file_size = 0;
static kal_uint8 hash_value[HASH_LEN];
DL_PACKAGE_GFH *pDl_Package_GFH = NULL;
// Flash dependent variables
#if defined(_NAND_FLASH_BOOTING_)
static kal_uint32 remap_tbl[(64*1024)/4]; //REMAP table for 1. filesystem. the BRMT is 64K at most, 2. ENFB remap table, assume max item is 4096
static BLOCK_MAPPING_TABLE_ENTRY map_tbl[8192]; //MAP table for filesystem.
static DL_PACKAGE_NAND_IMAGE_HEADER *pDl_Package_Nand_Image_Header = NULL;
static DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pMAUIImage = NULL;
static DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pFSImage = NULL;
static DL_PACKAGE_NAND_IMAGE_HEADER_FDM5_MAP_TBL *pFDM5MappingTbl = NULL;
static kal_uint32 FDM5MappingTblLen = 0;
static kal_uint32 FDM5MappingTblEntryNum = 0;
static kal_uint32 pFDM5MappingTblOffset = 0;
// NAND-related parameters
kal_uint32 page_size = 0;
kal_uint32 page_per_block = 0;
static kal_uint32 block_size = 0;
static kal_uint32 page_size_with_spare = 0;
static kal_uint32 block_size_with_spare = 0;
Nand_ImageSpace_ST *pSpaceInfo = NULL;
#ifndef __SV5_ENABLED__
// Buffer pointers for ILB pages
static kal_uint32 rominfo_page[CDL_MAX_PAGE_SIZE/4];
static kal_uint32 space_page[CDL_MAX_PAGE_SIZE/4];
static kal_uint32 imagelist_page[CDL_MAX_PAGE_SIZE/4];
static Nand_ImageList_S *pImageList = NULL;
static Nand_ImageList_S card_img_info;
#endif /* __SV5_ENABLED__ */
#else /* _NAND_FLASH_BOOTING_ */
kal_uint32 page_size = NOR_PAGE_SIZE;
static kal_uint32 page_size_with_spare = NOR_PAGE_SIZE;
static kal_uint32 mauiFirstBlock = 0;
#ifndef __SV5_ENABLED__
static kal_uint32 rominfo_buf[(NOR_PAGE_SIZE*2)>>2]; //Doulbe size for possible tag on the boundary
#else
static kal_uint32 flash_pmaui_gfh_buf[NOR_PAGE_SIZE>>2];
#endif
#endif /* _NAND_FLASH_BOOTING_ */
// SVx dependent variables
#ifdef __SV5_ENABLED__
static kal_uint32 pmaui_gfh_buf[CDL_MAX_PAGE_SIZE/4];
static FlashLayout flash_layout_info;
static Nand_ImageSpace_ST space_info;
static kal_uint32* pMauiInfoInCard; // It will point into pmaui_gfh_buf
static UPDATING_RECORD UpdatingRecord;
static kal_uint32 xim_cbr_index;
static kal_uint32 xim_maui_index;
#else
static kal_uint32 macr_buf[MAX_PAGE_SIZE_WITH_SPARE/4];
#endif /* __SV5_ENABLED__ */
static kal_uint32 image_count = 0;
static kal_uint32 fs_image_count = 0;
static kal_uint32 nand_image_header_len = 0; //Length of NAND image header (excluding GFH)
static kal_uint32 header_block_count = 0;
static kal_uint32 extra_info_count = 0;
static kal_uint8 *pSignatureBegin = NULL;
static kal_uint32 signatureLength = 0; //2 signatures currently
static kal_uint32 codeRegionEndIndex = 0;
static kal_bool last_cdl_fail_flag = KAL_FALSE;
static kal_bool fsPartialUpdate = KAL_FALSE;
static kal_bool codePartialUpdate = KAL_FALSE;
static kal_bool ximFixedLayout = KAL_FALSE; //If XIM is MBA is FOTA load, this flag will be raise
#if defined(__CDL_SUPPORT_BOOTCERT_V3__) || defined(__CDL_SUPPORT_BOOTCERT_V5__)
static kal_bool isBootCertExist = KAL_FALSE;
#endif
#ifndef __SV5_ENABLED__
FTL_FuncTbl *g_ftlFuncTbl = &ftlFuncTbl;
#else
extern FTL_FuncTbl *g_ftlFuncTbl;
#endif
#ifdef __LCD_DRIVER_IN_BL__
static kal_bool lcd_inited = KAL_FALSE;
#endif /* __LCD_DRIVER_IN_BL__ */
#ifdef __FOTA_DM__
MTK_FOTA_ROM_Info_v1_ST fotaRomInfo;
#endif
/*************************************************************************
* Declaration
*************************************************************************/
BL_CD_ERROR_CODE bl_ReadXIMPage(kal_uint32 addr, kal_uint32 pageIdx, kal_uint32 page_count, kal_bool doHash);
STATIC kal_bool bl_IsValidBinInfoItem(GFH_DL_PACKAGE_EXTRA_INFO *pInfo);
STATIC kal_bool bl_IsMarkerFound(kal_uint32 mauiAddr);
#ifdef _NAND_FLASH_BOOTING_
#ifndef __SV5_ENABLED__
STATIC kal_uint32 bl_GetILBStart();
STATIC kal_uint32 bl_GetILBEnd();
BL_CD_ERROR_CODE bl_ScanILBArea(kal_uint32 ilbAreaStart, kal_uint32 ilbAreaEnd, kal_uint32 *pMainILB, kal_uint32 *pDlPkgILB);
#endif /* _NAND_FLASH_BOOTING_ */
#endif /* __SV5_ENABLED__ */
/*************************************************************************
* Test code
*************************************************************************/
#if defined(__TRANSMISSION_DEBUG__)
static const kal_uint32 CRC_TBL_LEN = 256;
static kal_uint32 crc_table[CRC_TBL_LEN]; /* Table of CRCs of all 8-bit messages. */
static kal_uint32 crc_init = 0;
STATIC void Build_CRC_TBL(void)
{
kal_uint32 c;
kal_int32 n, k;
for (n = 0; n < CRC_TBL_LEN; n++)
{
c = (kal_uint32)n;
for (k = 0; k < 8; k++)
{
if (c & 1)
{
c = TRANSMISSION_DBG_MAGIC ^ (c >> 1);
}
else
{
c = c >> 1;
}
}
crc_table[n] = c;
}
}
STATIC kal_uint32 Update_CRC(kal_uint32 crc, kal_uint8 *buf, kal_uint32 len)
{
kal_uint32 n, c = crc;
if(!crc_init)
{
Build_CRC_TBL();
crc_init = 702;
}
for (n = 0; n < len; n++)
{
c = crc_table[(c ^ buf[n]) & TRANSMISSION_WRAP_MASK] ^ (c >> 8);
}
return c;
}
#endif /* defined (__TRANSMISSION_DEBUG__) */
/*************************************************************************
* General storage adatpation interfaces
*************************************************************************/
#ifdef __FS_CARD_DOWNLOAD__
static FS_HANDLE bl_fs_handle; //For keeping the file handle got from FS
static kal_int32 bl_fs_seek_pointer; //For record the file position during cardDL
static kal_uint32 msdc_inited = KAL_FALSE; //For checking if the msdc is inited
/***************************************************************************//**
* @brief The interface for card DL to open file with File system existed.
*
* This function is not alowed to be re-entrant. The API will init HW driver and open specific file
* once be called.
*
* @param[in] filename It is not used yet. Reserved the flexibility for future.
*
* @return The error code. If the error code from FS is not recognizable, it just return
* the error code from FS.
*
******************************************************************************/
kal_int32 bl_DL_Open(const kal_char *filename)
{
if(msdc_inited == KAL_FALSE)
{
#ifndef __OLD_PDN_ARCH__
#if defined(__DRV_SUPPORT_LPWR__)
DRVPDN_Disable(PDN_DMA);
#else
PDN_CLR(PDN_DMA);
#endif
#endif
//Initialize the MS/SD host controller, It should be called only once at drv_init, or one BL stage.
BL_MSDC_Init();
msdc_inited = KAL_TRUE;
}
//Try to mount MSDC here to detect early msdc issue.
//FS_Open() might also try to mount the device, but it will return directly if the first mount is already success.
if(BL_MSDC_MountDevice(0, 0, 0) != 512)
{
BL_MSDC_DeInit();
return BL_CD_ERROR_NO_CARD_FOUND;
}
//The drive letter will be taken by FS and always be redirect to the correct card drive
//Thus we can use any drive letter here
bl_fs_handle = FS_Open(L"e:\\image.bin", 0);
//Check the result of FS API, and convert it to BL error code
if(bl_fs_handle >= FS_NO_ERROR)
{
return BL_CD_ERROR_NONE;
}
else if(bl_fs_handle == FS_DRIVE_NOT_FOUND)
{
return BL_CD_ERROR_NO_CARD_FOUND;
}
else if(bl_fs_handle == FS_FILE_NOT_FOUND)
{
return BL_CD_ERROR_NO_DL_PACKAGE_FOUND;
}
else //Something wrong in FS, just return the error code from FS
{
return bl_fs_handle;
}
}
/***************************************************************************//**
* @brief The interface for card DL to seek file with File system existed.
*
* This function redirects to the subset functionaility of FS_seek.
*
* @param[in] offset The offset of file from the file begin.
* @param[in] origin Reserved
*
* @return The error code. If the error code from FS is not recognizable, it just return
* the error code from FS.
*
******************************************************************************/
kal_int32 bl_DL_Seek(kal_uint32 offset, kal_int32 origin)
{
ASSERT(origin == 0);
bl_fs_seek_pointer = FS_Seek(bl_fs_handle, offset, FS_FILE_BEGIN);
//Check the result of FS API, and convert it to BL error code
if(bl_fs_seek_pointer >= FS_NO_ERROR)
{
return BL_CD_ERROR_NONE;
}
else
{
return bl_fs_seek_pointer;
}
}
/***************************************************************************//**
* @brief The interface for card DL to read file with File system existed.
*
* This function must be used after bl_DL_Open(). It will use the pre-defined file handle to
* get the file into buffer.
*
* @param[in] buffer The buffer to be put the read data.
* @param[in] len The length to read
*
* @return If there is no error, The return value is the same as the input len. It is due to its caller
* expect there is no error only when the return == len.
*
******************************************************************************/
kal_uint32 bl_DL_Read(void *buffer, kal_uint32 len)
{
kal_uint32 read;
kal_int32 status;
status = FS_Read(bl_fs_handle, buffer, len, &read);
//Check the result of FS API, and convert it to BL error code
if(status == FS_NO_ERROR)
{
// Ignore the remaining length if it encountered the file end
return len;
}
else
{
return 0;
}
}
/***************************************************************************//**
* @brief The interface for card DL to close file with File system existed.
*
* This function will close the file handler for cardDL, and deinit msdc.
*
******************************************************************************/
void bl_DL_Close()
{
//Close the file
if(bl_fs_handle)
{
FS_Close(bl_fs_handle);
}
//deinit msdc
if(msdc_inited == KAL_TRUE)
{
BL_MSDC_DeInit();
msdc_inited = KAL_FALSE;
}
}
#else /* __FS_CARD_DOWNLOAD__ */
static kal_uint32 msdc_seek_pointer = 0;
static kal_uint32 msdc_buffer_offset = 0;
static kal_uint32 msdc_sector_size = 512;
static kal_uint32 msdc_sector_count = 0;
static kal_uint32 msdc_buffer[4*1024/4]; //Internal use by the DL package adaption layer
static kal_uint32 msdc_inited = KAL_FALSE;
kal_int32 bl_DL_Open(const kal_char *filename)
{
kal_int32 ret;
FS_PartitionRecord DiskGeometry;
BYTE MediaDescriptor;
ASSERT(sizeof(msdc_buffer)%msdc_sector_size == 0);
if(msdc_inited == KAL_FALSE)
{
#ifndef __OLD_PDN_ARCH__
#if defined(__DRV_SUPPORT_LPWR__)
DRVPDN_Disable(PDN_DMA);
#else
PDN_CLR(PDN_DMA);
#endif
#endif
BL_MSDC_Init();
msdc_inited = KAL_TRUE;
}
if(BL_MSDC_MountDevice(0, 0, 0) != 512)
{
BL_MSDC_DeInit();
return BL_CD_ERROR_NO_CARD_FOUND;
}
ret = BL_MSDC_GetDiskGeometry(&DiskGeometry, &MediaDescriptor);
if(ret != 0)
{
BL_PRINT(LOG_CRIT, "BL_MSDC_GetDiskGeometry failed, ret=%d\n\r", ret);
BL_MSDC_DeInit();
return BL_CD_ERROR_MSDC_GET_DISK_GEO;
}
msdc_seek_pointer = 0;
msdc_buffer_offset = INVALID_OFFSET;
msdc_sector_size = 512;
msdc_sector_count = DiskGeometry.Sectors;
return 0;
}
kal_int32 bl_DL_Seek(kal_uint32 offset, kal_int32 origin)
{
ASSERT(origin == 0);
if(msdc_seek_pointer >= msdc_sector_size*msdc_sector_count)
{
return -1;
}
msdc_seek_pointer = offset;
return 0;
}
kal_uint32 bl_DL_Read(void *buffer, kal_uint32 len)
{
kal_uint8 *pBuf = buffer;
kal_uint32 toRead = len;
while(1)
{
if(msdc_buffer_offset != INVALID_OFFSET && msdc_seek_pointer >= msdc_buffer_offset)
{
kal_uint32 inbuf_offset = msdc_seek_pointer-msdc_buffer_offset;
if(inbuf_offset < sizeof(msdc_buffer))
{
kal_uint32 n = MIN(sizeof(msdc_buffer)-inbuf_offset, toRead);
memcpy(pBuf, ((kal_uint8*)msdc_buffer)+inbuf_offset, n);
msdc_seek_pointer += n;
pBuf += n;
toRead -= n;
}
}
if(toRead)
{
kal_uint32 start_sector = msdc_seek_pointer/msdc_sector_size;
kal_int32 msdc_driver_status;
#if defined (__TRANSMISSION_DEBUG__)
kal_uint32 i;
for(i=0; i<sizeof(msdc_buffer)/4; i++)
((kal_uint32*)(msdc_buffer))[i] = msdc_seek_pointer;
#endif /* defined (__TRANSMISSION_DEBUG__) */
msdc_driver_status = BL_MSDC_ReadSectors(start_sector, sizeof(msdc_buffer)/msdc_sector_size, msdc_buffer);
if(msdc_driver_status != 0)
{
msdc_buffer_offset = INVALID_OFFSET;
return 0;
}
msdc_buffer_offset = start_sector*msdc_sector_size;
}
else
{
break;
}
}
return len;
}
void bl_DL_Close()
{
if(msdc_inited == KAL_TRUE)
{
BL_MSDC_DeInit();
msdc_inited = KAL_FALSE;
}
}
#endif /* __FS_CARD_DOWNLOAD__ */
/*************************************************************************
* Public interface
*************************************************************************/
/**********************************************************
Description : To tell if CDL is under processing
Input : None
Output : None
***********************************************************/
kal_bool bl_IsCardDownloadGoing()
{
#ifndef _NAND_FLASH_BOOTING_
if( bl_IsMarkerFound(MAUI_ROM_START_ADDR) )
{
BL_PRINT(LOG_DEBUG, "Last Card Download is failed. Re-download Now...\n\r");
last_cdl_fail_flag = KAL_TRUE;
return KAL_TRUE;
}
#else /* _NAND_FLASH_BOOTING_ */
#ifdef __SV5_ENABLED__
//Check if there is updating record
if(CBR_GetRecordLen(E_CBR_IDX_CBR, CBR_RECORD_UPDATING_INFO)>0)
{
//Read the updating record
if(CBR_ReadRecord(E_CBR_IDX_CBR, CBR_RECORD_UPDATING_INFO, (kal_uint8*)&UpdatingRecord, sizeof(UPDATING_RECORD), NULL) <= 0)
{
return KAL_FALSE;
}
//Check if the updating record is belong to CDL
if(UpdatingRecord.m_info_type_magic == CDL_MARKER)
{
BL_PRINT(LOG_DEBUG, "Last Card Download is failed. Re-download Now...\n\r");
last_cdl_fail_flag = KAL_TRUE;
return KAL_TRUE;
}
}
#else /* __SV5_ENABLED__ */
kal_uint32 mainILB = 0;
kal_uint32 dlPkgILB = 0;
//Init the FTL driver, because bl_ScanILBArea() will need to do read on flash
if(g_ftlFuncTbl->FTL_Init(NULL) != FTL_SUCCESS)
{
return KAL_FALSE;
}
if(bl_ScanILBArea(bl_GetILBStart(), bl_GetILBEnd(), &mainILB, &dlPkgILB) != BL_CD_ERROR_NONE)
{
return KAL_FALSE;
}
if(dlPkgILB)
{
BL_PRINT(LOG_DEBUG, "Last Card Download is failed. Re-download Now...\n\r");
last_cdl_fail_flag = KAL_TRUE;
return KAL_TRUE;
}
#endif /* __SV5_ENABLED__ */
#endif /* _NAND_FLASH_BOOTING_ */
return KAL_FALSE;
}
kal_bool bl_CardDownloadTriggered()
{
#ifdef __AUTO_START_CARD_DOWNLOAD__
return KAL_TRUE;
#else
//Triggered by keypad
if(BL_kbd_IsKeyPressed(TRIGGER_KEY))
{
return KAL_TRUE;
}
return KAL_FALSE;
#endif
}
void bl_ClearCardDownloadTrigger()
{
#ifdef __AUTO_START_CARD_DOWNLOAD__
#else
//Clear dedicated flag if any
#endif
return;
}
void bl_DetectPowerOff()
{
//Query the power key, if it is pressed, do power off.
//Exclude the key used for triggering card download
if((BL_kbd_IsKeyPressed(DEVICE_KEY_POWER) || BL_kbd_IsKeyPressed(DEVICE_KEY_END))
&& !BL_kbd_IsKeyPressed(TRIGGER_KEY))
{
DCL_HANDLE pw_handle;
//Use dclpw to latch the power
pw_handle=DclPW_Open(DCL_PW, FLAGS_NONE);
DclPW_Control(pw_handle,PW_CMD_POWEROFF,NULL);
DclPW_Close(pw_handle);
}
}
/*************************************************************************
* Utilities
*************************************************************************/
#ifdef _NAND_FLASH_BOOTING_
kal_uint32 bl_PhyBlockIdx2Logical(kal_uint32 physicalIdx)
{
kal_uint32 logicalIdx = INVALID_BLOCK_IDX;
kal_uint32 i;
if(pFDM5MappingTbl)
{
for(i=0; i<FDM5MappingTblEntryNum; i++)
{
if(pFDM5MappingTbl->log2phy[i] == physicalIdx)
{
logicalIdx = i;
break;
}
}
}
else
{
kal_uint32 page = pFDM5MappingTblOffset/page_size;
kal_uint32 *p = (kal_uint32*)((kal_uint8*)page_buffer + pFDM5MappingTblOffset%page_size+4);
if(bl_ReadXIMPage((kal_uint32)page_buffer, page, 1, KAL_FALSE) != BL_CD_ERROR_NONE)
{
return INVALID_BLOCK_IDX;
}
for(i=0; i<FDM5MappingTblEntryNum; i++)
{
if(*p == physicalIdx)
{
//Debug only if(logicalIdx != INVALID_BLOCK_IDX) ASSERT(logicalIdx == i);
logicalIdx = i;
break;
}
if((kal_uint8*)(++p) >= (kal_uint8*)page_buffer+page_size)
{
page++;
if(bl_ReadXIMPage((kal_uint32)page_buffer, page, 1, KAL_FALSE) != BL_CD_ERROR_NONE)
{
return INVALID_BLOCK_IDX;
}
p = page_buffer;
}
}
}
return logicalIdx;
}
#else /* _NAND_FLASH_BOOTING_ */
kal_uint32 bl_AddrToBlockIdx(kal_uint32 addr, FTL_OptParam *opt_param)
{
kal_uint32 Block;
kal_uint32 Page;
kal_uint32 status;
status = g_ftlFuncTbl->FTL_AddrToBlockPage(addr, &Block, &Page, opt_param);
ASSERT(status == FTL_SUCCESS);
return Block;
}
#endif /* _NAND_FLASH_BOOTING_ */
STATIC kal_bool bl_IsAddrOnBoundary(kal_uint32 addr)
{
#ifdef _NAND_FLASH_BOOTING_
return (addr%block_size == 0);
#else
return (addr == 0 || (bl_AddrToBlockIdx(addr-1, NULL) + 1 == bl_AddrToBlockIdx(addr, NULL)));
#endif /* _NAND_FLASH_BOOTING_ */
}
kal_bool bl_IsRegionOverlap(kal_uint32 addr1, kal_uint32 len1, kal_uint32 addr2, kal_uint32 len2)
{
if(addr2>=addr1 && addr2<addr1+len1)
{
return KAL_TRUE;
}
if(addr1>=addr2 && addr1<addr2+len2)
{
return KAL_TRUE;
}
return KAL_FALSE;
}
/**********************************************************
Description : Called after flash driver is initialized. Do whatever check related to the flash
Input : None
Output : Status of the check
***********************************************************/
BL_CD_ERROR_CODE bl_CheckFlashDeviceStatus()
{
//NOR platform might not be block aligned
#ifdef _NAND_FLASH_BOOTING_
kal_uint32 i;
GFH_DL_PACKAGE_INFO_v2 *pPkgInfo = &pDl_Package_GFH->gfh_dl_package_info;
for(i=0; i<sizeof(pPkgInfo->m_extra_info)/sizeof(pPkgInfo->m_extra_info[0]); i++)
{
if(pPkgInfo->m_extra_info[i].m_bin_type == GFH_FILE_NONE)
{
break;
}
if(bl_IsValidBinInfoItem(&pPkgInfo->m_extra_info[i]))
{
if(!bl_IsAddrOnBoundary(pPkgInfo->m_extra_info[i].m_bin_start_addr) ||
!bl_IsAddrOnBoundary(pPkgInfo->m_extra_info[i].m_bin_start_addr + pPkgInfo->m_extra_info[i].m_bin_length) )
{
return BL_CD_ERROR_ADDRESS_OR_LENGTH_NOT_BLOCK_BOUNDARY;
}
}
}
#endif
return BL_CD_ERROR_NONE;
}
kal_bool bl_DL_SignatureVerify(kal_uint8 *pHash, kal_uint32 hash_len, kal_uint8 *sig, kal_uint32 sig_len)
{
char sig_buf[32];
bl_Alg_Asym_Decrypt((kal_uint32)sig_buf, (kal_uint32)sig, sig_len, (kal_uint32)custom_get_CDL_asymmetric_key(), custom_get_CDL_asymmetric_key_len());
/* Compare the hash value */
if (memcmp(pHash, sig_buf, hash_len) != 0)
{
return KAL_FALSE;
}
return KAL_TRUE;
}
void bl_DL_InitLCD()
{
#ifdef __LCD_DRIVER_IN_BL__
if(!lcd_inited)
{
BL_PRINT(LOG_INFO, "Init LCD\n\r");
BL_LCDHWInit();
BL_ShowUpdateFirmwareInitBackground();
BL_LCDSetBackLight();
{
DCL_HANDLE rtc_handler;
DCL_HANDLE pw_handle;
//before call dclpw, rtc is needed to be initialized
rtc_handler = DclRTC_Open(DCL_RTC,FLAGS_NONE);
DclRTC_Control(rtc_handler, RTC_CMD_SETXOSC, (DCL_CTRL_DATA_T *)NULL);
DclRTC_Control(rtc_handler, RTC_CMD_HW_INIT, (DCL_CTRL_DATA_T *)NULL);
DclRTC_Close(rtc_handler);
//Use dclpw to latch the power
pw_handle=DclPW_Open(DCL_PW, FLAGS_NONE);
DclPW_Control(pw_handle,PW_CMD_POWERON,NULL);
DclPW_Close(pw_handle);
}
lcd_inited = KAL_TRUE;
//Re-init MSDC driver to reset GPIO ping
bl_DL_Close();
bl_DL_Open(DUMMY_FILENAME);
}
#endif /* __LCD_DRIVER_IN_BL__ */
}
BL_CD_ERROR_CODE bl_UpdateProgress(kal_uint32 phase, kal_uint32 progress)
{
//phase 0: initialzation, 10%
//phase 1: verification, 30%
//phase 2: upgrading, 50%
//phase 3: finishing, 10%
const kal_uint32 phasePortion[] = {10, 30, 50, 10};
const kal_uint32 step = 1;
static kal_uint32 lastProgress = 0;
kal_uint32 totalProgress = 0;
kal_uint32 i;
ASSERT(phase<sizeof(phasePortion)/sizeof(*phasePortion) && progress<=100);
for(i=0; i<phase; i++)
{
totalProgress += phasePortion[i];
}
totalProgress += progress*phasePortion[i]/100;
WacthDogRestart();
if(totalProgress >= lastProgress+step)
{
#ifdef __LCD_DRIVER_IN_BL__
bl_DL_InitLCD();
BL_ShowUpdateFirmwareProgress(totalProgress);
#endif /* __LCD_DRIVER_IN_BL__ */
lastProgress = totalProgress;
}
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE FTL_ERROR_TO_CD_ERROR(FTL_STATUS_CODE ftlErrorCode)
{
switch(ftlErrorCode)
{
case FTL_SUCCESS:
case FTL_ERROR_ECC_CORRECTED:
return BL_CD_ERROR_NONE;
case FTL_ERROR_READ_FAILURE:
return BL_CD_ERROR_FLASH_READ;
case FTL_ERROR_WRITE_FAILURE:
return BL_CD_ERROR_FLASH_PROGRAM;
case FTL_ERROR_ERASE_FAILURE:
return BL_CD_ERROR_FLASH_ERASE;
case FTL_ERROR_BAD_BLOCK:
return BL_CD_ERROR_FLASH_BAD_BLOCK;
}
return BL_CD_ERROR_FLASH_OTHER_ERROR;
}
/*************************************************************************
* Flash high level access utilities
*************************************************************************/
STATIC FTL_STATUS_CODE bl_EraseAndMarkBad(kal_uint32 flashBlockIdx, FTL_OptParam *opt_param)
{
FTL_STATUS_CODE status;
status = g_ftlFuncTbl->FTL_CheckGoodBlock(flashBlockIdx, opt_param);
if(status == FTL_ERROR_BAD_BLOCK)
{
BL_PRINT(LOG_DEBUG, "Bad block found before erasing @ %d%s\n\r", flashBlockIdx, (opt_param==NULL)?"":"(P)");
}
else
{
status = g_ftlFuncTbl->FTL_EraseBlock(flashBlockIdx, opt_param);
if(status == FTL_ERROR_BAD_BLOCK)
{
g_ftlFuncTbl->FTL_MarkBadBlock(flashBlockIdx, opt_param);
BL_PRINT(LOG_DEBUG, "Runtime bad block found after erasing @ %d%s\n\r", flashBlockIdx, (opt_param==NULL)?"":"(P)");
}
}
return status;
}
/*************************************************************************
* XIM high level access utilities
*************************************************************************/
kal_int32 bl_GetGFHImgIdx(GFH_FILE_TYPE bin_type)
{
kal_uint32 i;
for(i = 0; i < GFH_DL_PKG_EXTRA_INFO_COUNT; i++)
{
if(pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_type == bin_type)
{
return i;
}
}
return -1;
}
STATIC kal_bool bl_IsValidBinInfoItem(GFH_DL_PACKAGE_EXTRA_INFO *pInfo)
{
if( pInfo->m_bin_start_addr != INVALID_ADDR && pInfo->m_bin_length != INVALID_LEN )
{
return KAL_TRUE;
}
return KAL_FALSE;
}
STATIC IM_OPERATION bl_ExtraInfoGetFSOperation(kal_uint32 fsIndex)
{
kal_uint32 i, index = 0;
GFH_DL_PACKAGE_INFO_v2 *pPkgInfo = &pDl_Package_GFH->gfh_dl_package_info;
for(i=0; i<sizeof(pPkgInfo->m_extra_info)/sizeof(pPkgInfo->m_extra_info[0]); i++)
{
if(pPkgInfo->m_extra_info[i].m_bin_type == GFH_FILE_NONE)
{
break;
}
if(pPkgInfo->m_extra_info[i].m_bin_type >= V_FILE_SYSTEM_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_FILE_SYSTEM_BINARY_END)
{
if(index == fsIndex)
{
return pPkgInfo->m_extra_info[i].m_operation;
}
index++;
}
}
return IM_DOWNLOAD;
}
STATIC IM_OPERATION bl_QueryBinaryOperation(kal_uint32 addr)
{
kal_uint32 i;
GFH_DL_PACKAGE_INFO_v2 *pPkgInfo = &pDl_Package_GFH->gfh_dl_package_info;
for(i=0; i<sizeof(pPkgInfo->m_extra_info)/sizeof(pPkgInfo->m_extra_info[0]); i++)
{
if(pPkgInfo->m_extra_info[i].m_bin_type == GFH_FILE_NONE)
{
break;
}
if(bl_IsValidBinInfoItem(&pPkgInfo->m_extra_info[i]) &&
pPkgInfo->m_extra_info[i].m_bin_type >= V_FILE_SYSTEM_BINARY &&
pPkgInfo->m_extra_info[i].m_bin_type < V_FILE_SYSTEM_BINARY_END)
{
if(addr >= pPkgInfo->m_extra_info[i].m_bin_start_addr &&
addr < pPkgInfo->m_extra_info[i].m_bin_start_addr + pPkgInfo->m_extra_info[i].m_bin_length)
{
return pPkgInfo->m_extra_info[i].m_operation;
}
}
}
return IM_DOWNLOAD;
}
BL_CD_ERROR_CODE bl_ReadXIMPageX(kal_uint32 addr, kal_uint32 pageIdx, kal_uint32 page_count, kal_bool with_spare, kal_bool doHash)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 offset = pDl_Package_GFH->gfh_file_info.m_content_offset + pageIdx*page_size_with_spare;
kal_uint32 read = 0;
if(bl_DL_Seek(offset, 0) != 0)
{
return BL_CD_ERROR_PACKAGE_SEEK_FAIL;
}
while(status == BL_CD_ERROR_NONE && read < page_count)
{
if(bl_DL_Read((kal_uint32*)addr, page_size_with_spare) != page_size_with_spare)
{
status = BL_CD_ERROR_PACKAGE_READ_FAIL;
}
if(status == BL_CD_ERROR_NONE && doHash)
{
bl_Alg_Hash_Append(addr, page_size_with_spare);
}
addr += (with_spare ? page_size_with_spare : page_size);
read++;
}
return status;
}
BL_CD_ERROR_CODE bl_ReadXIMPage(kal_uint32 addr, kal_uint32 pageIdx, kal_uint32 page_count, kal_bool doHash)
{
return bl_ReadXIMPageX(addr, pageIdx, page_count, KAL_FALSE, doHash);
}
//Write one block from XIM body to flash, without spare, via DAL, auto-find good block
BL_CD_ERROR_CODE bl_WriteXimBlockToFlash(kal_uint32 ximBodyOffset, kal_uint32 flashBlockIdx, kal_uint32 *pWrittenBlockIdx, kal_uint32 *pinfo, kal_uint32 OffsetInImage)
{
FTL_STATUS_CODE status = FTL_SUCCESS;
kal_uint32 i;
if( bl_DL_Seek(pDl_Package_GFH->gfh_file_info.m_content_offset + ximBodyOffset, 0) != 0)
{
return BL_CD_ERROR_PACKAGE_SEEK_FAIL;
}
for(;; flashBlockIdx++)
{
#if defined(__TRANSMISSION_DEBUG__)
kal_uint32 crc = crc_init;
#endif /* defined (__TRANSMISSION_DEBUG__) */
#ifdef _NAND_FLASH_BOOTING_
kal_uint32 pageToWrite = page_per_block;
#else
kal_uint32 pageToWrite = g_ftlFuncTbl->FTL_GetBlockSize(flashBlockIdx, NULL) / g_ftlFuncTbl->FTL_GetPageSize();
#endif /* _NAND_FLASH_BOOTING_ */
status = bl_EraseAndMarkBad(flashBlockIdx, NULL);
if(status == FTL_ERROR_BAD_BLOCK)
{
continue;
}
for(i=0; status==FTL_SUCCESS && i<pageToWrite; i++)
{
#if defined(__TRANSMISSION_DEBUG__)
memset(page_buffer, TRANSMISSION_DBG_PATTERN, page_size_with_spare);
#endif /* defined (__TRANSMISSION_DEBUG__) */
if(bl_DL_Read(page_buffer, page_size_with_spare) != page_size_with_spare)
{
return BL_CD_ERROR_PACKAGE_READ_FAIL;
}
#if defined(__TRANSMISSION_DEBUG__)
crc = Update_CRC(crc, (kal_uint8*)page_buffer, page_size);
#endif /* defined (__TRANSMISSION_DEBUG__) */
#ifndef __SV5_ENABLED__
#if defined(__BIND_TO_CHIP_BASIC__) || defined(__BIND_TO_CHIP__) || defined(__BIND_TO_KEY__)
if(pinfo)
{
SST_ContentPreprocess(pinfo, OffsetInImage, page_buffer, page_size);
}
#endif
#endif /* __SV5_ENABLED__ */
#ifdef __FOTA_DM__
#ifdef _NAND_FLASH_BOOTING_
if(g_maui_image_info_validity != MTK_MAUI_HEAD_INFO_VALID)
{
SSF_SearchMAUIImageHead((kal_uint8*)page_buffer, page_size, NULL, 0);
}
#endif
#endif
status = g_ftlFuncTbl->FTL_WritePage(flashBlockIdx, i, page_buffer, NULL);
OffsetInImage += page_size;
}
if(status != FTL_SUCCESS)
{
if(status != FTL_ERROR_BAD_BLOCK)
{
BL_PRINT(LOG_CRIT, "Unknown flash error!!!!! %d\n\r", status);
}
//skip this block and re-program
status = g_ftlFuncTbl->FTL_MarkBadBlock(flashBlockIdx, NULL);
continue;
}
#if defined(__TRANSMISSION_DEBUG__)
BL_PRINT(LOG_DEBUG, "(%x) ", crc);
#endif /* defined (__TRANSMISSION_DEBUG__) */
//success
break;
}
if(status == FTL_SUCCESS && pWrittenBlockIdx != NULL)
{
*pWrittenBlockIdx = flashBlockIdx;
}
return FTL_ERROR_TO_CD_ERROR(status);
}
//Get SVx specific information
#ifndef __SV5_ENABLED__
#if defined(__BIND_TO_CHIP_BASIC__) || defined(__BIND_TO_CHIP__) || defined(__BIND_TO_KEY__)
extern kal_uint32* SST_GetMACRInfo(kal_uint32 *pRomInfo, kal_uint32 *pOffset, kal_uint32 *pLen);
extern void *SST_Search_MAUI_Rom_Info(kal_uint32 rom_base, kal_uint32 length);
BL_CD_ERROR_CODE bl_getMACRInfo(kal_uint32 pmauiOffset, kal_uint32** pinfo)
{
kal_uint32* pMauiInfo = NULL;
volatile kal_uint32 macr_Offset = 0; //Use volatile to notify compiler that it will be modified by other function
volatile kal_uint32 macr_Len = 0;
#ifdef _NAND_FLASH_BOOTING_
pMauiInfo = SST_Search_MAUI_Rom_Info((kal_uint32)rominfo_page, sizeof(rominfo_page));
#else /* _NAND_FLASH_BOOTING_ */
pMauiInfo = SST_Search_MAUI_Rom_Info((kal_uint32)rominfo_buf, sizeof(rominfo_buf));
#endif /* _NAND_FLASH_BOOTING_ */
if(pMauiInfo == NULL)
{
return BL_CD_ERROR_GET_ROMINFO_FAIL;
}
//Get the MACR of MAUI info. It is the preprocess of binding P-Image to chip.
*pinfo = SST_GetMACRInfo(pMauiInfo, ¯_Offset, ¯_Len);
if(*pinfo == NULL)
{
//macr_buf should reserve enough space
if(macr_Len > sizeof(macr_buf));
{
return BL_CD_ERROR_INSUFFICIENT_MACR_BUF;
}
// TODO: check the offset
if(bl_DL_Seek(pmauiOffset + macr_Offset, 0) != 0)
{
return BL_CD_ERROR_PACKAGE_SEEK_FAIL;
}
if(bl_DL_Read(macr_buf, macr_Len) != macr_Len)
{
return BL_CD_ERROR_PACKAGE_READ_FAIL;
}
*pinfo = macr_buf;
}
return BL_CD_ERROR_NONE;
}
#endif /* defined(__BIND_TO_CHIP_BASIC__) || defined(__BIND_TO_CHIP__) || defined(__BIND_TO_KEY__) */
extern void bl_int_getFATregion(kal_uint32 *buf, kal_uint32* fat_addr, kal_uint32* fat_len);
extern void bl_int_getSDSregion(kal_uint32 *buf, kal_uint32* sds_addr, kal_uint32* sds_len);
void bl_getFATregion(kal_uint32 *buf, kal_uint32* fat_addr, kal_uint32* fat_len)
{
bl_int_getFATregion(buf, fat_addr, fat_len);
}
void bl_getSDSregion(kal_uint32 *buf, kal_uint32* sds_addr, kal_uint32* sds_len)
{
bl_int_getSDSregion(buf, sds_addr, sds_len);
}
#else /* __SV5_ENABLED__ */
void bl_getFATregion(kal_uint32 *buf, kal_uint32* fat_addr, kal_uint32* fat_len)
{
GFH_FLASH_INFO_v1 *pFlashInfo;
kal_uint32 addr, len;
*fat_addr = 0;
*fat_len = 0;
if(GFH_Find((U32)buf, GFH_FLASH_INFO, (void **)&pFlashInfo) == B_OK)
{
//The index of m_flash_info: 0-NOR, 1-NAND, 2-EMMC
addr = pFlashInfo->m_flash_info[pDl_Package_GFH->gfh_dl_package_info.m_im_device].m_fat_begin_addr;
len = pFlashInfo->m_flash_info[pDl_Package_GFH->gfh_dl_package_info.m_im_device].m_fat_length;
if(len!=0 && len!=INVALID_LEN && addr!=0 && addr!=INVALID_ADDR)
{
*fat_addr = addr;
*fat_len = len;
}
}
}
void bl_getSDSregion(kal_uint32 *buf, kal_uint32* sds_addr, kal_uint32* sds_len)
{
GFH_MAUI_INFO_v1 *pMauiInfo;
kal_uint32 addr, len;
*sds_addr = 0;
*sds_len = 0;
if(GFH_Find((U32)buf, GFH_MAUI_INFO, (void **)&pMauiInfo) == B_OK)
{
addr = pMauiInfo->m_sds_base_addr;
len = pMauiInfo->m_sds_len;
if(len!=0 && len!=INVALID_LEN && addr!=0 && addr!=INVALID_ADDR)
{
*sds_addr = addr;
*sds_len = len;
}
}
}
#endif /* __SV5_ENABLED__ */
/*************************************************************************
* Image updaters
*************************************************************************/
#ifdef _NAND_FLASH_BOOTING_
kal_uint32 bl_GetSpareSize(kal_uint32 page_size)
{
return page_size/32;
}
Nand_Update_Area_ST *bl_CDL_NewSpaceInfo(kal_uint32 n)
{
if (n < 10)
return &(pSpaceInfo->m_image_space[n]);
else
return &(pSpaceInfo->m_image_ext_space[n-10]);
}
STATIC kal_bool bl_isFSImageRecord(DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage)
{
return (pImage->feature_bit & FS_IMAGE_MASK) ? KAL_TRUE : KAL_FALSE;
}
STATIC kal_bool bl_isENFBImage(DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage)
{
return (pImage->feature_bit & ENFB_MASK) ? KAL_TRUE : KAL_FALSE;
}
STATIC kal_bool bl_isRSImage(DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage)
{
return (pImage->feature_bit & RS_IMAGE_MASK) ? KAL_TRUE : KAL_FALSE;
}
STATIC kal_bool bl_isPImage(DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage)
{
return (pImage->feature_bit & P_IMAGE_MASK) ? KAL_TRUE : KAL_FALSE;
}
STATIC kal_bool bl_isFOTAReservoir(DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage)
{
return (pImage->feature_bit & U_MASK) ? KAL_TRUE : KAL_FALSE;
}
STATIC kal_bool bl_isFOTABackupSpace(DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage)
{
return (pImage->feature_bit & B_MASK) ? KAL_TRUE : KAL_FALSE;
}
//Write 3rd MAP block to flash, without spare, via DAL, auto-find good block
BL_CD_ERROR_CODE bl_Write3rdROMBlockToNand(kal_uint32 *p3rdROMMapTbl, kal_uint32 size, kal_uint32 flashBlockIdx, kal_uint32 *pWrittenBlockIdx)
{
FTL_STATUS_CODE status = FTL_SUCCESS;
kal_uint32 i;
for(;; flashBlockIdx++)
{
kal_uint8 *p = (kal_uint8*)p3rdROMMapTbl;
kal_uint32 toWrite = size;
status = bl_EraseAndMarkBad(flashBlockIdx, NULL);
if(status == FTL_ERROR_BAD_BLOCK)
{
continue;
}
for(i=0; status==FTL_SUCCESS && i<page_per_block; i++)
{
memset(page_buffer, INVALID_1B_CONTENT, page_size);
if(toWrite)
{
kal_uint32 n = MIN(page_size, toWrite);
memcpy(page_buffer, p, n);
toWrite -= n;
p += n;
}
status = g_ftlFuncTbl->FTL_WritePage(flashBlockIdx, i, page_buffer, NULL);
}
if(status != FTL_SUCCESS)
{
if(status != FTL_ERROR_BAD_BLOCK)
{
BL_PRINT(LOG_CRIT, "Unknown Flash error!!!!! %d\n\r", status);
}
//skip this block and re-program
status = g_ftlFuncTbl->FTL_MarkBadBlock(flashBlockIdx, NULL);
continue;
}
//success
break;
}
if(status == FTL_SUCCESS && pWrittenBlockIdx != NULL)
{
*pWrittenBlockIdx = flashBlockIdx;
}
return FTL_ERROR_TO_CD_ERROR(status);
}
/***************************************************************************//**
* @brief The function for read data from flash
*
* This function is used for readout data from flash and store data into ram
*
* @param[in] buf_ptr The buffer pointer for storing data onto ram.
* @param[in] BlkNo The block number on flash to be read out. (read start position)
* @param[in] length The length to be read out
* @param[in] bDAL
*
* @return BL_CD_ERROR_CODE
*
******************************************************************************/
BL_CD_ERROR_CODE bl_ReadDataFromFlash(kal_uint32 *buf_ptr, kal_uint32 BlkNo, kal_int32 length, FTL_OptParam *opt_param)
{
kal_uint32 pageNo = 0;
kal_uint32 page_size = g_ftlFuncTbl->FTL_GetPageSize();
//kal_uint32 page_per_block = g_ftlFuncTbl->FTL_GetBlockSize(0, KAL_FALSE)/page_size;
kal_bool goodBlockChecked = KAL_FALSE;
if(length == 0)
{
return BL_CD_ERROR_NONE;
}
while(length>0)
{
if(!goodBlockChecked)
{
while( g_ftlFuncTbl->FTL_CheckGoodBlock(BlkNo, opt_param) == FTL_ERROR_BAD_BLOCK )
{
/* Skip the bad block */
BL_PRINT(LOG_INFO, "\n\rBad block at block %d\n\r", BlkNo);
BlkNo++;
}
goodBlockChecked = KAL_TRUE;
}
if(length >= page_size)
{
if( g_ftlFuncTbl->FTL_ReadPage(BlkNo, pageNo, buf_ptr, opt_param) != FTL_SUCCESS )
{
BL_PRINT(LOG_CRIT, "Read error at block %d, page %d\n\r", BlkNo, pageNo);
return BL_CD_ERROR_FLASH_READ;
}
pageNo++;
length -= page_size;
(kal_uint32)buf_ptr += page_size;
}
else
{
if( g_ftlFuncTbl->FTL_ReadPage(BlkNo, pageNo, page_buffer, opt_param) != FTL_SUCCESS )
{
BL_PRINT(LOG_CRIT, "Read error at block %d, page %d\n\r", BlkNo, pageNo);
return BL_CD_ERROR_FLASH_READ;
}
memcpy(buf_ptr,page_buffer,length);
pageNo++;
length = 0;
(kal_uint32)buf_ptr += length;
}
ASSERT(length>=0);
/* Looking for the next good block */
if (pageNo == page_per_block)
{
BlkNo++;
pageNo = 0;
goodBlockChecked = KAL_FALSE;
WacthDogRestart();
BL_PRINT(LOG_INFO, ".");
}
}
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE bl_DoUpdateMiscImg(kal_uint32 *pFlashBlockIdx, kal_uint32 imageIdx)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage = pMAUIImage+imageIdx;
kal_uint32 i;
kal_uint32 flash_max_block_num = 0;
//Partial update only updates the resource bins, which are not manipulate here.
if(codePartialUpdate == KAL_TRUE)
{
return status;
}
//Currently, bl_DoUpdateMiscImg only process FOTAReservoir and FOTABackupSpace
if(!bl_isFOTAReservoir(pImage) && !bl_isFOTABackupSpace(pImage))
{
return status;
}
BL_PRINT(LOG_DEBUG, "=>Erase data from xim=%d->%d: ", pImage->start_block, pImage->start_block+pImage->blocks-1);
for(i=0; i<pImage->blocks; i++)
{
BL_PRINT(LOG_DEBUG, "%d->%d, ", pImage->start_block+i, *pFlashBlockIdx);
//Do not need to take care the bad block here. So simply erase as many blocks as described in pImage->blocks
bl_EraseAndMarkBad(*pFlashBlockIdx, NULL);
bl_UpdateProgress(UPDATE_PHASE, ((pImage->start_block+i)*page_per_block*100/(im_file_size/page_size_with_spare)));
//Update the SpaceInfo for FOTA information
if(i == 0)
{
if(pSpaceInfo)
{
if(bl_isFOTAReservoir(pImage))
{
pSpaceInfo->m_package_start = *pFlashBlockIdx;
}
if(bl_isFOTABackupSpace(pImage))
{
pSpaceInfo->m_backup_start= *pFlashBlockIdx;
}
}
}
(*pFlashBlockIdx)++;
}
BL_PRINT(LOG_DEBUG, "done\n\r");
if(pSpaceInfo)
{
if(bl_isFOTAReservoir(pImage))
{
pSpaceInfo->m_package_last = *pFlashBlockIdx-1;
}
if(bl_isFOTABackupSpace(pImage))
{
pSpaceInfo->m_backup_last = *pFlashBlockIdx-1;
}
}
return status;
}
BL_CD_ERROR_CODE bl_checkToEraseFat(kal_uint32 code_boundary_blk)
{
kal_uint32 i,j;
kal_uint32 start_addr, fat_len, flashBlockIdx, numBlktoErase;
GFH_DL_PACKAGE_INFO_v2 *pPkgInfo = &pDl_Package_GFH->gfh_dl_package_info;
kal_uint32 legal_fat_addr, legal_fat_len;
kal_uint32 sds_addr, sds_len;
kal_uint32 code_boundary_address;
#ifdef __SV5_ENABLED__
bl_getFATregion(pmaui_gfh_buf, &legal_fat_addr, &legal_fat_len);
bl_getSDSregion(pmaui_gfh_buf, &sds_addr, &sds_len);
#else
bl_getFATregion(rominfo_page, &legal_fat_addr, &legal_fat_len);
bl_getSDSregion(rominfo_page, &sds_addr, &sds_len);
#endif
//Check if code region is overlapped with FAT region
if(legal_fat_len != 0)
{
code_boundary_address = code_boundary_blk*block_size - 1;
if((code_boundary_address >= legal_fat_addr) && (code_boundary_address < legal_fat_addr+legal_fat_len))
{
BL_PRINT(LOG_INFO, "Code-FAT overlap! last code address:%x, FAT address:%x, FAT len:%x\n\r\n\r", code_boundary_address, legal_fat_addr, legal_fat_len);
return BL_CD_ERROR_CODE_FAT_OVERLAPED;
}
}
//Scan whole extra_info to get the file system entry
for(i=0; i<sizeof(pPkgInfo->m_extra_info)/sizeof(pPkgInfo->m_extra_info[0]); i++)
{
if(pPkgInfo->m_extra_info[i].m_bin_type >= V_FILE_SYSTEM_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_FILE_SYSTEM_BINARY_END)
{
//It only process the case that the operation is ERASE
//Note that the given start adress and length should be block aligned
if(pPkgInfo->m_extra_info[i].m_operation == IM_ERASE)
{
start_addr = pPkgInfo->m_extra_info[i].m_bin_start_addr;
fat_len = pPkgInfo->m_extra_info[i].m_bin_length;
#ifdef __SECURE_DATA_STORAGE__
//Check the earse range regard with SDS rage
if(sds_len != 0)
{
if(bl_IsRegionOverlap(start_addr, fat_len, sds_addr, sds_len))
{
BL_PRINT(LOG_INFO, "SDS can't be erased! SDS address:%x, SDS length:%x\n\r\n\r", sds_addr, sds_len);
return BL_CD_ERROR_EARSE_SDS;
}
}
#endif /* __SECURE_DATA_STORAGE__ */
flashBlockIdx = (start_addr+block_size-1)/block_size; //make sure the start address is block aligned
numBlktoErase = fat_len/block_size;
BL_PRINT(LOG_DEBUG, "Start to Erase FAT region %d from %x, len=%x\n\r", i, start_addr, fat_len);
for(j=flashBlockIdx; j<flashBlockIdx+numBlktoErase; j++)
{
BL_PRINT(LOG_DEBUG, "%d, ", j);
bl_EraseAndMarkBad(j, NULL);
}
BL_PRINT(LOG_INFO, "Erase FAT %d done\n\r\n\r", i);
}
}
}
//Do nothing even bad block is found
return BL_CD_ERROR_NONE;
}
#ifdef __CDL_SUPPORT_UPDATE_FAT__
//Write REMAP buffer with spare from original REMAP block, without DAL, stop if bad block
BL_CD_ERROR_CODE bl_WriteNandRemapBlockToFS(kal_uint32 remapTblBlockIdx, kal_uint16 *remapTbl, kal_uint32 remapTblSize, kal_uint32 flashBlockIdx)
{
FTL_STATUS_CODE status = FTL_SUCCESS;
kal_uint32 i;
kal_uint32 remapTblSizeToWrite = remapTblSize;
FTL_OptParam opt_param = {KAL_TRUE, KAL_FALSE};
if( bl_DL_Seek(pDl_Package_GFH->gfh_file_info.m_content_offset + remapTblBlockIdx*(block_size_with_spare), 0) != 0)
{
return BL_CD_ERROR_PACKAGE_SEEK_FAIL;
}
status = bl_EraseAndMarkBad(flashBlockIdx, &opt_param);
for(i=0; status==FTL_SUCCESS && i<page_per_block; i++)
{
kal_uint32 toWrite = MIN(remapTblSizeToWrite, page_size);
if(bl_DL_Read(page_buffer, page_size_with_spare) != page_size_with_spare)
{
return BL_CD_ERROR_PACKAGE_READ_FAIL;
}
memcpy(page_buffer, (kal_uint8*)remapTbl+i*page_size, toWrite);
remapTblSizeToWrite -= toWrite;
status = NFB_ProgramPhysicalPageWithSpareX(flashBlockIdx, i, page_buffer, ((kal_uint8*)page_buffer)+page_size, KAL_FALSE);
if(status > 0)
{
if(status != page_size)
{
return BL_CD_ERROR_NFI_UNEXPECED_BEHAVIOR;
}
status = FTL_SUCCESS;
}
}
if(status != FTL_SUCCESS)
{
if(status != FTL_ERROR_BAD_BLOCK)
{
BL_PRINT(LOG_CRIT, "Unknown flash error!!!!! %d\n\r", status);
}
g_ftlFuncTbl->FTL_MarkBadBlock(flashBlockIdx, &opt_param);
}
return FTL_ERROR_TO_CD_ERROR(status);
}
//Write NAND block to flash, with spare, without DAL, stop if bad block
BL_CD_ERROR_CODE bl_WriteNandBlockToFS(kal_uint32 ximBlockIdx, kal_uint32 flashBlockIdx)
{
FTL_STATUS_CODE status = FTL_SUCCESS;
kal_uint32 i;
FTL_OptParam opt_param = {KAL_TRUE, KAL_FALSE};
if( bl_DL_Seek(pDl_Package_GFH->gfh_file_info.m_content_offset + ximBlockIdx*(block_size_with_spare), 0) != 0)
{
return BL_CD_ERROR_PACKAGE_SEEK_FAIL;
}
status = bl_EraseAndMarkBad(flashBlockIdx, &opt_param);
for(i=0; status==FTL_SUCCESS && i<page_per_block; i++)
{
if(bl_DL_Read(page_buffer, page_size_with_spare) != page_size_with_spare)
{
return BL_CD_ERROR_PACKAGE_READ_FAIL;
}
status = NFB_ProgramPhysicalPageWithSpareX(flashBlockIdx, i, page_buffer, ((kal_uint8*)page_buffer)+page_size, KAL_FALSE);
if(status > 0)
{
if(status != page_size)
{
return BL_CD_ERROR_NFI_UNEXPECED_BEHAVIOR;
}
status = FTL_SUCCESS;
}
}
if(status != FTL_SUCCESS)
{
if(status != FTL_ERROR_BAD_BLOCK)
{
BL_PRINT(LOG_CRIT, "Unknown flash error!!!!!\n\r", status);
}
g_ftlFuncTbl->FTL_MarkBadBlock(flashBlockIdx, &opt_param);
}
return FTL_ERROR_TO_CD_ERROR(status);
}
STATIC BLOCK_MAPPING_TABLE_ENTRY bl_GetMapEntry(kal_uint32 index)
{
return map_tbl[index];
}
STATIC kal_uint32 bl_FindNextReplacement(kal_uint32 startIndex, kal_uint32 endIndex, kal_uint32 group, kal_uint32 *pPhysicalBlock)
{
kal_uint32 i;
BLOCK_MAPPING_TABLE_ENTRY entry;
kal_int32 step = (endIndex>=startIndex) ? 1 : -1;
endIndex += step;
for(i=startIndex; i!=endIndex; i+=step)
{
entry = bl_GetMapEntry(i);
if(entry.GroupNo == group)
{
if(pPhysicalBlock)
{
*pPhysicalBlock = entry.PhyBlkAddr;
return i;
}
}
}
return 0;
}
BL_CD_ERROR_CODE bl_LoadBRMTFromIM(kal_uint32 BRMTXimIdx, kal_uint16 *pBRMT, kal_uint32 BRMTSize)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 toRead = BRMTSize;
kal_uint32 pageIdx = BRMTXimIdx*page_per_block;
kal_uint8 *pDst = (kal_uint8*)pBRMT;
while(toRead)
{
kal_uint32 n = MIN(toRead, page_size);
status = bl_ReadXIMPage((kal_uint32)page_buffer, pageIdx, 1, KAL_FALSE);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
memcpy(pDst, page_buffer, n);
toRead -= n;
pDst += n;
pageIdx++;
}
return status;
}
BL_CD_ERROR_CODE bl_DoUpdateFilesystemRegion(kal_uint32 regionStartIdx, kal_uint32 *pNextRegionStartIdx, IM_OPERATION op)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
REGION_PARAM_BLOCK_HEADER *pRegionBlock;
FS_LAYOUT fsLayout;
kal_uint32 BRMTXimIdx;
kal_uint32 dataBlockXimStart;
kal_uint32 dataBlockNum;
kal_uint32 fsBlockNum;
kal_uint32 groupNum;
kal_uint32 remapStartLogAddr;
kal_uint32 BRMTSize;
kal_uint32 reservedStartAddr;
kal_uint32 reservedBlockNum;
kal_uint32 flashBlockIdx;
kal_uint16* pNextReservedBlock;
kal_uint32 fsCount = 0;
kal_uint32 FDMVer;
kal_uint32 i = 0;
kal_uint16 *pBRMT = (kal_uint16*)remap_tbl;
kal_bool totalBBMIm = KAL_FALSE;
kal_bool useBRMTonFlash = KAL_FALSE;
kal_bool BRMTUpdated = KAL_FALSE;
kal_uint32 nextFs = 0;
FTL_OptParam opt_param = {KAL_TRUE, KAL_FALSE};
status = bl_ReadXIMPageX((kal_uint32)page_buffer, regionStartIdx*page_per_block, 1, KAL_TRUE, KAL_FALSE);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
pRegionBlock = (REGION_PARAM_BLOCK_HEADER*)page_buffer;
//Sanity test
ASSERT_VALID_PARAM_IN_XIM_BODY(sizeof(REGION_PARAM_BLOCK_HEADER) <= page_size_with_spare);
ASSERT_VALID_PARAM_IN_XIM_BODY(pRegionBlock->PhyicalBlkNum < sizeof(remap_tbl)/2-8);
ASSERT_VALID_PARAM_IN_XIM_BODY(pRegionBlock->BlockSize*1024 == block_size);
ASSERT_VALID_PARAM_IN_XIM_BODY(pRegionBlock->PageSize == page_size);
ASSERT_VALID_PARAM_IN_XIM_BODY(pRegionBlock->RPB_Size < sizeof(remap_tbl));
ASSERT_VALID_PARAM_IN_XIM_BODY(pRegionBlock->RemapStartLogAddr*pRegionBlock->GroupNum == pRegionBlock->RvdBlkStartAddr);
ASSERT_VALID_PARAM_IN_XIM_BODY(pRegionBlock->RvdBlkStartAddr+pRegionBlock->RvdBlkNum == pRegionBlock->PhyicalBlkNum);
BRMTXimIdx = regionStartIdx + pRegionBlock->RPB_Size;
dataBlockXimStart = BRMTXimIdx + 1;
dataBlockNum = pRegionBlock->DataBlkNum;
fsBlockNum = pRegionBlock->PhyicalBlkNum;
groupNum = pRegionBlock->GroupNum;
remapStartLogAddr = pRegionBlock->RemapStartLogAddr;
BRMTSize = pRegionBlock->BRMTSize;
reservedStartAddr = pRegionBlock->RvdBlkStartAddr;
reservedBlockNum = pRegionBlock->RvdBlkNum;
pNextReservedBlock= (kal_uint16*)(pBRMT + BRMTSize/sizeof(pBRMT[0]) - 8);
//Find next region
*pNextRegionStartIdx = dataBlockXimStart + dataBlockNum;
if(sizeof(remap_tbl) < BRMTSize)
{
return BL_CD_ERROR_TOO_LARGE_BRMT;
}
//Load BRMT on IM file
status = bl_LoadBRMTFromIM(BRMTXimIdx, pBRMT, BRMTSize);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
//Sanity test: IM's BRMT should be all zero in its remapping records
{
for(i=0; i<dataBlockNum; i++)
{
ASSERT_VALID_PARAM_IN_XIM_BODY(pBRMT[i] == 0);
}
}
FDMVer = (*(kal_uint32*)(pNextReservedBlock - 18)) & FDMVER_MASK;
ASSERT_VALID_PARAM_IN_XIM_BODY(FDMVer==FDMVER_205 || FDMVer==FDMVER_005 || (FDMVer&FDMVER_105_MASK)==FDMVER_105);
//Check if total BBM enabled
if(FDMVer == FDMVER_205)
{
totalBBMIm = KAL_TRUE;
}
#ifdef __NANDFDM_TOTAL_BBM__
if(!totalBBMIm)
{
return BL_CD_ERROR_NON_TOTALBBM_IM_ON_TOTALBBM_TARGET;
}
BL_PRINT(LOG_INFO, "TotalBBM enabled\n\r");
//Sanity test: If total BBM is enabled, there should be only one visible filesystem area
ASSERT_VALID_PARAM_IN_XIM_BODY(fs_image_count == 1);
//Copy the flash layout out from the BRMT in the IM file
memcpy(&fsLayout, pNextReservedBlock-50, sizeof(fsLayout));
//Since the target is total BBM enabled, try to load existing BRMT on the flash
{
kal_uint32 retSize;
kal_uint32 ret = FDM5_BLReadBRMT((kal_uint8*)pBRMT, sizeof(remap_tbl), &retSize);
if(ret == BLBRMT_ERRCODE_NOERR)
{
//Check if the 2 BRMT are matched in their layout
if(BRMTSize != retSize || memcmp(pNextReservedBlock-50, &fsLayout, sizeof(fsLayout)) != 0)
{
//Not allow only update some of file system when the layout is mismatched
if(fsPartialUpdate)
{
return BL_CD_ERROR_MISMATCHED_BRMT;
}
//Leave useBRMTonFlash false since the layout is change and BRMT need to be refreshed
BL_PRINT(LOG_WARN, "Mismatch BRMT, BRMT on flash will be completed overwritten\n\r");
//Re-read the BRMT on IM
status = bl_LoadBRMTFromIM(BRMTXimIdx, pBRMT, BRMTSize);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
}
else
{
BL_PRINT(LOG_INFO, "Use BRMT on flash, partially updating on totalBBM FS enabled\n\r");
useBRMTonFlash = KAL_TRUE;
}
}
else if(ret == BLBRMT_ERRCODE_BRMT_NOT_FOUND)
{
if(fsPartialUpdate)
{
return BL_CD_ERROR_NEED_TO_UPDATE_ALL_FS;
}
// It's fine
BL_PRINT(LOG_INFO, "No BRMT on flash, refresh \n\r");
}
else
{
BL_PRINT(LOG_ERROR, "Read BRMT failed, FDM error=%d\n\r", ret);
return BL_CD_ERROR_READ_BRMT_FAIL;
}
}
//Prepare filesystem layout
{
kal_uint32 base = fsLayout.fs[0].Base;
for(i=0; i<sizeof(fsLayout.fs)/sizeof(fsLayout.fs[0]); i++)
{
if(fsLayout.fs[i].Base == 0 && fsLayout.fs[i].Size == 0)
{
break;
}
BL_PRINT(LOG_INFO, "fs[%d] = (%x, %d)\r\n", i, fsLayout.fs[i].Base, fsLayout.fs[i].Size);
ASSERT_VALID_PARAM_IN_XIM_BODY(fsLayout.fs[i].Base % block_size == 0);
ASSERT_VALID_PARAM_IN_XIM_BODY(fsLayout.fs[i].Size % block_size == 0);
fsLayout.fs[i].Base -= base;
fsLayout.fs[i].Base /= block_size;
fsLayout.fs[i].Size /= block_size;
if(i != 0)
{
ASSERT_VALID_PARAM_IN_XIM_BODY(fsLayout.fs[i-1].Base+fsLayout.fs[i-1].Size == fsLayout.fs[i].Base);
}
fsCount++;
}
}
//The operation of total BBM IM is inside the filesystem, which will be looked up later
op = IM_DOWNLOAD;
#else
if(totalBBMIm)
{
return BL_CD_ERROR_TOTALBBM_IM_ON_NON_TOTALBBM_TARGET;
}
#endif /* __NANDFDM_TOTAL_BBM__ */
if(useBRMTonFlash == KAL_FALSE)
{
for(i=0; i<groupNum; i++)
{
ASSERT(pNextReservedBlock[i]==remapStartLogAddr); //Only for debug
pNextReservedBlock[i] = remapStartLogAddr;
}
}
//Load MAP table
{
kal_uint32 toRead = fsBlockNum*sizeof(BLOCK_MAPPING_TABLE_ENTRY);
kal_uint32 pageIdx = regionStartIdx*page_per_block;
kal_uint8 *pDst = (kal_uint8*)map_tbl;
kal_uint32 offset = sizeof(REGION_PARAM_BLOCK_HEADER);
while(toRead)
{
kal_uint32 n = MIN(toRead, page_size_with_spare-offset);
status = bl_ReadXIMPageX((kal_uint32)page_buffer, pageIdx, 1, KAL_TRUE, KAL_FALSE);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
memcpy(pDst, (kal_uint8*)page_buffer+offset, n);
offset = 0;
toRead -= n;
pDst += n;
pageIdx++;
}
}
//Check if the code region has been overlapped to file system due to too many bad blocks
{
//Use first entry of map table as the first logical block
kal_uint32 fsLogicalStart = bl_PhyBlockIdx2Logical(map_tbl[0].PhyBlkAddr);
if(fsLogicalStart == INVALID_BLOCK_IDX)
{
return BL_CD_ERROR_INVALID_FDM_MAPTBL;
}
if(fsLogicalStart < codeRegionEndIndex)
{
BL_PRINT(LOG_ERROR, "FS region (starting from %d) overlaps Code region (ended with %d) maybe due to too many badk block in bad block or invalid XIM file\n\r", fsLogicalStart, codeRegionEndIndex);
return BL_CD_ERROR_CODE_FS_OVERLAPED;
}
}
if(op == IM_SKIP)
{
BL_PRINT(LOG_INFO, "This region belongs to a filesystem which should be skiped\n\r");
return BL_CD_ERROR_NONE;
}
//Erase all reserved block if we are not going to use the BRMT in the flash drive
if(useBRMTonFlash == KAL_FALSE)
{
BL_PRINT(LOG_DEBUG, "=>Erase reserved block id=%d->%d: ", reservedStartAddr, reservedStartAddr+reservedBlockNum-1);
for(i=0; i<reservedBlockNum; i++)
{
flashBlockIdx = bl_GetMapEntry(reservedStartAddr+i).PhyBlkAddr;
if(flashBlockIdx == 0)
{
return BL_CD_ERROR_INVALID_XIM_CONTENT;
}
if(op == IM_DOWNLOAD || op == IM_ERASE)
{
BL_PRINT(LOG_DEBUG, "%d(P), ", flashBlockIdx);
bl_EraseAndMarkBad(flashBlockIdx, &opt_param);
}
}
BL_PRINT(LOG_DEBUG, "done\n\r");
}
BL_PRINT(LOG_DEBUG, "=>Writing data from fs_index=%d->%d: ", 0, dataBlockNum-1);
//Write all data block to fs region
for(i=0; i<dataBlockNum; i++)
{
kal_uint32 group;
BLOCK_MAPPING_TABLE_ENTRY entry;
//For total bbm, we have to distinguish the file system inside one IM binary, and get it's operation code
if(totalBBMIm)
{
if(nextFs < fsCount && i == fsLayout.fs[nextFs].Base)
{
op = bl_ExtraInfoGetFSOperation(nextFs);
BL_PRINT(LOG_INFO, "Switch total BBM fs=%d, op=%d\n\r", nextFs, op);
nextFs++;
}
}
entry = bl_GetMapEntry(i);
flashBlockIdx = entry.PhyBlkAddr;
group = entry.GroupNo;
bl_UpdateProgress(UPDATE_PHASE, ((dataBlockXimStart+i)*page_per_block*100/(im_file_size/page_size_with_spare)));
//When total BBM is enabled, bad blocks may have been remapped
if(pBRMT[i])
{
kal_uint32 replaceIdx = pBRMT[i]*groupNum;
entry = bl_GetMapEntry(replaceIdx);
BL_PRINT(LOG_DEBUG, "Found existing bad remap block %d(P)->id %d, %d(P)\n\r", flashBlockIdx, replaceIdx, entry.PhyBlkAddr);
flashBlockIdx = entry.PhyBlkAddr;
ASSERT(group == entry.GroupNo);
}
while(1)
{
if(op == IM_DOWNLOAD)
{
BL_PRINT(LOG_DEBUG, "%d->%d(P), ", i, flashBlockIdx);
status = bl_WriteNandBlockToFS(dataBlockXimStart+i, flashBlockIdx);
}
else if(op == IM_ERASE)
{
BL_PRINT(LOG_DEBUG, "%E %d(P), ", flashBlockIdx);
bl_EraseAndMarkBad(flashBlockIdx, &opt_param);
//Do nothing even bad block is found
status = BL_CD_ERROR_NONE;
}
else if(op == IM_SKIP)
{
BL_PRINT(LOG_DEBUG, "S", flashBlockIdx);
status = BL_CD_ERROR_NONE;
}
if(status == BL_CD_ERROR_NONE)
{
break;
}
else
{
kal_uint32 nextReplacementIdx;
if(status != BL_CD_ERROR_FLASH_BAD_BLOCK)
{
BL_PRINT(LOG_CRIT, "Unexpected error when processing FS block to %d(P), status=%d\n\r", flashBlockIdx, status);
return status;
}
nextReplacementIdx = bl_FindNextReplacement(pNextReservedBlock[group]*groupNum, fsBlockNum-1, group, &flashBlockIdx);
if(!nextReplacementIdx)
{
BL_PRINT(LOG_ERROR, "Too many bad block to find a placement, failed to update\n\r");
return BL_CD_ERROR_TOO_MANY_BAD_BLOCK;
}
BL_PRINT(LOG_DEBUG, "Found replacement @ id %d, %d(P)\n\r", nextReplacementIdx, flashBlockIdx);
pBRMT[i] = nextReplacementIdx/groupNum;
pNextReservedBlock[group] = nextReplacementIdx/groupNum+1;
BRMTUpdated = KAL_TRUE;
}
}
}
BL_PRINT(LOG_DEBUG, "done\n\r");
//Write BRMT block
//When to write BRMT?
//1. Total BBM + existing BRMT in FS + BRMT updated => FDM function
//2. Total BBM + first time => Raw write
//3. Non Total BBM => Raw write
if(totalBBMIm && useBRMTonFlash && BRMTUpdated)
{
#ifdef __NANDFDM_TOTAL_BBM__
kal_uint32 ret = FDM5_BLWriteBRMT((kal_uint8*)pBRMT, BRMTSize);
BL_PRINT(LOG_DEBUG, "=>Update BRMT in the drive, ret=%d\n\r", ret);
if(ret != BLBRMT_ERRCODE_NOERR)
{
return BL_CD_ERROR_WRITE_BRMT_FAIL;
}
#endif /* __NANDFDM_TOTAL_BBM__ */
}
else if((totalBBMIm == KAL_TRUE && useBRMTonFlash == KAL_FALSE) ||
(totalBBMIm == KAL_FALSE) )
{
kal_uint32 remapBlockIdx;
kal_uint32 remapReservedStart = pNextReservedBlock[0]*groupNum;
kal_uint32 remapReservedEnd = fsBlockNum-1;
remapBlockIdx = bl_FindNextReplacement(remapReservedEnd, remapReservedStart, 0, &flashBlockIdx);
remapReservedEnd--;
while(1)
{
BL_PRINT(LOG_DEBUG, "=>Writing BRMT to %d(P)\n\r", flashBlockIdx);
status = bl_WriteNandRemapBlockToFS(BRMTXimIdx, pBRMT, BRMTSize, flashBlockIdx);
if(status == BL_CD_ERROR_NONE)
{
break;
}
else
{
if(status != BL_CD_ERROR_FLASH_BAD_BLOCK)
{
BL_PRINT(LOG_CRIT, "Unexpected error when writing BRMT to %d(P), status=%d\n\r", flashBlockIdx, status);
return status;
}
remapBlockIdx = bl_FindNextReplacement(remapReservedEnd, remapReservedStart, 0, &flashBlockIdx);
remapReservedEnd--;
if(!remapBlockIdx)
{
BL_PRINT(LOG_ERROR, "Too many bad block to program BRMT, failed to update\n\r");
return BL_CD_ERROR_TOO_MANY_BAD_BLOCK;
}
BL_PRINT(LOG_DEBUG, "Found replacement @ %d, %d(P)\n\r", remapBlockIdx, flashBlockIdx);
}
}
}
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE bl_DoUpdateFilesystem(kal_uint32 imageIdx)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage = pFSImage + imageIdx;
kal_uint32 regionStartIdx = pImage->start_block;
kal_uint32 i;
IM_OPERATION op = bl_ExtraInfoGetFSOperation(imageIdx);
for(i=0; status==BL_CD_ERROR_NONE && regionStartIdx<pImage->start_block+pImage->blocks; i++)
{
BL_PRINT(LOG_INFO, "Updating region %d\n\r", i);
status = bl_DoUpdateFilesystemRegion(regionStartIdx, ®ionStartIdx, op);
}
return status;
}
#endif /* __CDL_SUPPORT_UPDATE_FAT__ */
BL_CD_ERROR_CODE bl_ProcessFilesystem(kal_uint32 flashBlockIdx)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 i;
//Check if it is want to erase FAT
//The normal FAT erase function is only for the card download without FAT donwload functionality.
//The fs_image_count is come from nand header. If user has no fs image for download,
//there will be no fs image in nand header.
//The normal FAT erase function just add a reacord in GFH header to notify CDL engine to do erase.
//There will no fs image in nand header.
if(0==fs_image_count)
{
status = bl_checkToEraseFat(flashBlockIdx);
}
#ifdef __CDL_SUPPORT_UPDATE_FAT__
//The function for update FAT is only available when there is fs image in nand header.
else if(0<fs_image_count)
{
codeRegionEndIndex = flashBlockIdx;
for(i=0; i<fs_image_count; i++)
{
BL_PRINT(LOG_INFO, "Updating FS %d\n\r", i);
status = bl_DoUpdateFilesystem(i);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
BL_PRINT(LOG_INFO, "Update of FS %d done\n\r\n\r", i);
}
}
#endif /* __CDL_SUPPORT_UPDATE_FAT__ */
return status;
}
#ifndef __SV5_ENABLED__
Nand_ImageInfo_S *bl_GetCardImageInfo(kal_uint32 n)
{
if (n < 10)
return &(card_img_info.m_image[n]);
else
return &(card_img_info.m_image_ext[n-10]);
}
Nand_ImageInfo_S *bl_CDL_NewImageInfo(kal_uint32 n)
{
if (n < 10)
return &(pImageList->m_image[n]);
else
return &(pImageList->m_image_ext[n-10]);
}
/***************************************************************************//**
* @brief Get the bin type by Image list index
*
* This function mapped the ILE index into extra-info's index, and get the image type information
* contained in extra-info.
* Use the extra info to help checking the image type of ILE (image list entry).
* Skip the images before pMAUI, since those image can not be mapped to ILE.
* It assumes the oder of maui images are the same between ILE and extra info .
*
* @param[in] ile_id The index in image list.
*
* @return GFH_FILE_TYPE
*
******************************************************************************/
GFH_FILE_TYPE bl_GetBinTypeByILEid(kal_uint32 ile_id)
{
kal_int32 extra_info_id;
GFH_DL_PACKAGE_INFO_v2 *pPkgInfo = &pDl_Package_GFH->gfh_dl_package_info;
//map the id into extra info
extra_info_id = GetExtraInfoidByILEid(ile_id);
if((extra_info_id >= EXTRAINFO_PMAUI_IDX) && (extra_info_id < GFH_DL_PKG_EXTRA_INFO_COUNT))
{
return pPkgInfo->m_extra_info[extra_info_id].m_bin_type;
}
else
{
return GFH_FILE_NONE;
}
}
GFH_FILE_TYPE bl_GetBinTypeByNANDImgid(kal_uint32 nand_img_id)
{
kal_int32 extra_info_id;
GFH_DL_PACKAGE_INFO_v2 *pPkgInfo = &pDl_Package_GFH->gfh_dl_package_info;
//map the id into extra info
extra_info_id = GetExtraInfoidByNANDImgid(nand_img_id);
if((extra_info_id >= EXTRAINFO_PMAUI_IDX) && (extra_info_id < GFH_DL_PKG_EXTRA_INFO_COUNT))
{
return pPkgInfo->m_extra_info[extra_info_id].m_bin_type;
}
else
{
return GFH_FILE_NONE;
}
}
#ifdef __CDL_SUPPORT_BOOTCERT_V3__
BL_CD_ERROR_CODE bl_backupBootCert(kal_uint32 blockidx, kal_uint32 pageidx)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 bootcertidx;
kal_uint32 bootcertblk;
kal_bool isFlashHasBootCert = KAL_FALSE;
kal_uint32 i;
//Check if there is BootCert and is needed to be updated
for(bootcertidx = 0; bootcertidx < BL_Shared_info.m_bl_image_list.m_image_count; bootcertidx++)
{
if(GetImageInfo(bootcertidx)->m_reserved == BCERT_ILE_MARKER)
{
isFlashHasBootCert = KAL_TRUE;
break;
}
}
if(isFlashHasBootCert && isBootCertExist)
{
bootcertblk = GetImageInfo(bootcertidx)->m_start_block;
//Do backup BootCert
for(i = 0; i <= MAX_BOOTCERT_PAGE_NUM; i++)
{
status = g_ftlFuncTbl->FTL_ReadPage(bootcertblk, i, page_buffer, NULL);
if(status != FTL_SUCCESS)
{
return FTL_ERROR_TO_CD_ERROR(status);
}
status = g_ftlFuncTbl->FTL_WritePage(blockidx, pageidx+i, page_buffer, NULL);
if(status != FTL_SUCCESS)
{
return FTL_ERROR_TO_CD_ERROR(status);
}
}
}
else if(!isFlashHasBootCert && !isBootCertExist)
{
//There is no bootcert
//return BL_CD_ERROR_NONE;
}
else
{
return BL_CD_ERROR_NO_BOOT_CERT_EXIST;
}
return status;
}
BL_CD_ERROR_CODE bl_restoreBootCert(kal_uint32 flashBlockIdx, kal_uint32 *pWrittenBlockIdx)
{
FTL_STATUS_CODE status = FTL_SUCCESS;
kal_uint32 i;
kal_uint32 src_pageidx = page_per_block-MAX_BOOTCERT_PAGE_NUM-1;
kal_uint32 mainILB = 0;
kal_uint32 dlPkgILB = 0;
bl_ScanILBArea(bl_GetILBStart(), bl_GetILBEnd(), &mainILB, &dlPkgILB);
for(;; flashBlockIdx++)
{
//The MAX_BOOTCERT_LEN must not larger than block size
if(MAX_BOOTCERT_LEN > g_ftlFuncTbl->FTL_GetBlockSize(flashBlockIdx, NULL))
{
return BL_CD_ERROR_BOOTCERT_EXCEED_BLOCKSIZE;
}
status = bl_EraseAndMarkBad(flashBlockIdx, NULL);
if(status == FTL_ERROR_BAD_BLOCK)
{
continue;
}
for(i = 0; (status==FTL_SUCCESS) && (i<MAX_BOOTCERT_PAGE_NUM); i++)
{
status = g_ftlFuncTbl->FTL_ReadPage(dlPkgILB, src_pageidx+i, page_buffer, NULL);
if(status != FTL_SUCCESS)
{
break;
}
status = g_ftlFuncTbl->FTL_WritePage(flashBlockIdx, i, page_buffer, NULL);
}
if(status != FTL_SUCCESS)
{
if(status != FTL_ERROR_BAD_BLOCK)
{
BL_PRINT(LOG_CRIT, "Unknown flash error!!!!! %d\n\r", status);
}
//skip this block and re-program
status = g_ftlFuncTbl->FTL_MarkBadBlock(flashBlockIdx, NULL);
continue;
}
break;
}
if(status == FTL_SUCCESS && pWrittenBlockIdx != NULL)
{
*pWrittenBlockIdx = flashBlockIdx;
}
return FTL_ERROR_TO_CD_ERROR(status);
}
#endif /* __CDL_SUPPORT_BOOTCERT_V3__ */
/***************************************************************************//**
* @brief The function will extract some information and update to image list
*
* This function will search the secinfo in p-maui and s-maui, then update their position into
* image list
*
******************************************************************************/
BL_CD_ERROR_CODE bl_PrepareImageList()
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 img_idx, blk_idx;
kal_int32 img_length;
kal_uint32 load_offset;
kal_uint32 blk_offset, badblk_count;
kal_uint32 update_ile_idx;
for(img_idx=0; img_idx<pImageList->m_image_count; img_idx++)
{
//Check the condition to be updated
if(bl_GetBinTypeByILEid(img_idx) == PRIMARY_MAUI)
{
update_ile_idx = IL_REGIONINFO_HEAD_OFFSET;
}
else if(bl_GetBinTypeByILEid(img_idx) == SECONDARY_MAUI)
{
update_ile_idx = IL_REGIONINFO_TAIL_OFFSET;
}
else
{
update_ile_idx = -1;
}
if((update_ile_idx == IL_REGIONINFO_HEAD_OFFSET) || (update_ile_idx == IL_REGIONINFO_TAIL_OFFSET))
{
load_offset = bl_CDL_NewImageInfo(update_ile_idx)->m_load_addr - bl_CDL_NewImageInfo(img_idx)->m_load_addr;
//calculate the theoretical block offset
blk_offset = bl_CDL_NewImageInfo(img_idx)->m_start_block + (load_offset/block_size);
img_length = bl_CDL_NewImageInfo(img_idx)->m_length;
badblk_count = 0;
//Count the bad block before region_info to calculate the correct region_info block offset in flash
for(blk_idx = bl_CDL_NewImageInfo(img_idx)->m_start_block; blk_idx <= blk_offset; blk_idx++)
{
status = g_ftlFuncTbl->FTL_CheckGoodBlock(blk_idx, NULL);
if(status == FTL_ERROR_BAD_BLOCK)
{
badblk_count++;
}
else if(status != FTL_SUCCESS)
{
return status;
}
img_length -= block_size;
}
bl_CDL_NewImageInfo(update_ile_idx)->m_start_block = blk_offset + badblk_count;
//m_reserved field is for record the offset in block
bl_CDL_NewImageInfo(update_ile_idx)->m_reserved = load_offset % block_size;
}
}
return status;
}
BL_CD_ERROR_CODE bl_DoUpdateMAUI(kal_uint32 *pFlashBlockIdx, kal_uint32 imageIdx, kal_uint32 *pInfo)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage = pMAUIImage+imageIdx;
kal_uint32 i;
kal_uint32 flash_max_block_num = 0;
kal_uint32 flash_bound_block = pImage->max_block ? (*pFlashBlockIdx + pImage->max_block -1) : INVALID_BLOCK_IDX;
kal_uint32 spaceInfoId = GetSpaceidByNANDimgid(imageIdx);
kal_uint32 ILEid = GetILEidByNANDimgid(imageIdx);
kal_uint32 *pRemapTbl = bl_isENFBImage(pImage) ? ((kal_uint32*)remap_tbl) : NULL;
kal_bool isBootCert = KAL_FALSE;
#ifdef __CDL_SUPPORT_BOOTCERT_V3__
//Check if it is boot cert. Boot Cert need special proccessing
if(bl_GetBinTypeByNANDImgid(imageIdx) == BOOT_CERT_CTRL)
{
isBootCert = KAL_TRUE;
}
#endif /* __CDL_SUPPORT_BOOTCERT_V3__ */
//Make sure the max_block is valid, except BootCert
if((ximFixedLayout == KAL_TRUE) && (isBootCert == KAL_FALSE))
{
if(pImage->max_block == 0)
{
return BL_CD_ERROR_INVALID_XIM_IMG_MAX_BLOCK_NUMBER;
}
}
if(pImage->blocks*4 > sizeof(remap_tbl))
{
BL_PRINT(LOG_ERROR, "Block count=%d, large then %d\n\r", pImage->blocks, sizeof(remap_tbl)/4);
return BL_CD_ERROR_3RDROM_REMAP_TBL_TOO_SMALL;
}
if(pRemapTbl)
{ //It is 3rd ROM
//Skip the first block for 3rd ROM. NAND XIM always put the mapping table at the first block.
//But new rule is to put it at last. So skip first block in XIM's 3rd ROM, and write the table after all 3rd ROM data are written.
pImage->start_block++;
pImage->blocks--;
//Find the true start page of 3rd-rom when doing partial update
if(codePartialUpdate == KAL_TRUE)
{
//The block id of first 3-rd rom is at the first word of remapping table.
//The mapping table is pointed by 3rd-rom's image list
*pFlashBlockIdx = GetImageInfo(ILEid)->m_start_block;
//read the remaping table of 3rd-rom from flash
status = g_ftlFuncTbl->FTL_ReadPage(*pFlashBlockIdx, 0, page_buffer, NULL);
if(status != FTL_SUCCESS)
{
return FTL_ERROR_TO_CD_ERROR((FTL_STATUS_CODE)status);
}
//The first block id for 3rd-rom is at the first entry of mapping table.
*pFlashBlockIdx = page_buffer[0];
//Re-calculate the bound block, it is becuase the start block index is updated.
flash_bound_block = pImage->max_block ? (*pFlashBlockIdx + pImage->max_block -1) : INVALID_BLOCK_IDX;
}
}
else
{ //It is normal image
if(codePartialUpdate == KAL_TRUE)
{
if(pSpaceInfo && (bl_CDL_NewSpaceInfo(spaceInfoId)->m_image_end != 0))
{
flash_max_block_num = bl_CDL_NewSpaceInfo(spaceInfoId)->m_image_end - bl_CDL_NewSpaceInfo(spaceInfoId)->m_image_start + 1;
//The new max block number must not be larger than original when doing partial update
if((flash_max_block_num < pImage->max_block) || (pImage->max_block < pImage->blocks))
{
return BL_CD_ERROR_XIM_IMG_SIZE_EXCEED_MAX_VALUE;
}
//The boundary block shoud keep the same as original while doing partial update.
flash_bound_block = bl_CDL_NewSpaceInfo(spaceInfoId)->m_image_end;
}
//The start block should use the original ILE info
*pFlashBlockIdx = GetImageInfo(ILEid)->m_start_block;
//Re-calculate the bound block, it is becuase the start block index is updated.
flash_bound_block = pImage->max_block ? (*pFlashBlockIdx + pImage->max_block -1) : INVALID_BLOCK_IDX;
}
}
BL_PRINT(LOG_DEBUG, "=>Writing data from xim=%d->%d: ", pImage->start_block, pImage->start_block+pImage->blocks-1);
for(i=0; i<pImage->blocks; i++)
{
kal_uint32 origBlock = *pFlashBlockIdx;
BL_PRINT(LOG_DEBUG, "%d->%d, ", pImage->start_block+i, *pFlashBlockIdx);
#ifdef __CDL_SUPPORT_BOOTCERT_V3__
if(isBootCert)
{ //If the image is bootcert, restore it from the backup in ILB region
status = bl_restoreBootCert(*pFlashBlockIdx, pFlashBlockIdx);
}
else
{
status = bl_WriteXimBlockToFlash((pImage->start_block+i)*block_size_with_spare, *pFlashBlockIdx, pFlashBlockIdx, pInfo, i*block_size);
}
#else /* __CDL_SUPPORT_BOOTCERT_V3__ */
status = bl_WriteXimBlockToFlash((pImage->start_block+i)*block_size_with_spare, *pFlashBlockIdx, pFlashBlockIdx, pInfo, i*block_size);
#endif /* __CDL_SUPPORT_BOOTCERT_V3__ */
if(origBlock != *pFlashBlockIdx && status == BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_DEBUG, "%d->%d, ", pImage->start_block+i, *pFlashBlockIdx);
}
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_ERROR, "Error when writing block %d, status%d\n\r", *pFlashBlockIdx, status);
return status;
}
bl_UpdateProgress(UPDATE_PHASE, ((pImage->start_block+i)*page_per_block*100/(im_file_size/page_size_with_spare)));
if(*pFlashBlockIdx > flash_bound_block)
{
BL_PRINT(LOG_ERROR, "Maximum block exceeds, max=%d\n\r", flash_bound_block);
return BL_CD_ERROR_OVER_RESERVED_BOUNDARY;
}
//Update the image list and space info when first block is successfully written
if(i == 0)
{
bl_CDL_NewImageInfo(ILEid)->m_load_addr = bl_GetCardImageInfo(ILEid)->m_load_addr;
bl_CDL_NewImageInfo(ILEid)->m_length = bl_GetCardImageInfo(ILEid)->m_length;
if(codePartialUpdate == KAL_FALSE)
{
bl_CDL_NewImageInfo(ILEid)->m_start_block = *pFlashBlockIdx;
if(pSpaceInfo)
{
bl_CDL_NewSpaceInfo(spaceInfoId)->m_image_start = *pFlashBlockIdx;
}
}
}
//Update 3rd ROM remap block if necessary
if(pRemapTbl)
{
pRemapTbl[i] = *pFlashBlockIdx;
}
(*pFlashBlockIdx)++;
}
BL_PRINT(LOG_DEBUG, "done\n\r");
//Record the last block in space info
if(pSpaceInfo)
{
bl_CDL_NewSpaceInfo(spaceInfoId)->m_image_last = *pFlashBlockIdx-1;
if(codePartialUpdate == KAL_FALSE)
{
if(flash_bound_block != INVALID_BLOCK_IDX)
{
bl_CDL_NewSpaceInfo(spaceInfoId)->m_image_end = flash_bound_block;
}
else
{
bl_CDL_NewSpaceInfo(spaceInfoId)->m_image_end = *pFlashBlockIdx-1;
}
}
}
//Write 3RD ROM table
if(pRemapTbl)
{
BL_PRINT(LOG_DEBUG, "=>Write 3rd ROM remap block @ %d...", *pFlashBlockIdx);
if( bl_Write3rdROMBlockToNand(pRemapTbl, pImage->blocks*4, *pFlashBlockIdx, pFlashBlockIdx) != BL_CD_ERROR_NONE)
{
return BL_CD_ERROR_3RDROM_REMAP_TBL_WRITE_FAILURE;
}
BL_PRINT(LOG_DEBUG, " done @ %d\n\r", *pFlashBlockIdx);
bl_CDL_NewImageInfo(ILEid)->m_start_block = *pFlashBlockIdx;
(*pFlashBlockIdx)++;
}
//Erase unused space if max block is specified
if(flash_bound_block != INVALID_BLOCK_IDX)
{
BL_PRINT(LOG_DEBUG, "=>Erase block for FOTA reserved space:");
for(;*pFlashBlockIdx <= flash_bound_block; (*pFlashBlockIdx)++)
{
BL_PRINT(LOG_DEBUG, "%d ", *pFlashBlockIdx);
bl_EraseAndMarkBad(*pFlashBlockIdx, NULL);
}
BL_PRINT(LOG_DEBUG, "done\n\r");
}
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE bl_DoUpdate()
{
kal_uint32 i,j;
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 flashImgIdx = 0; //For record the processed image index on flash
kal_uint32 *pInfo;
DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage;
kal_uint32 flashBlockIdx = GetImageInfo(ROMINFO_INDEX+1)->m_start_block; //MAUI starting block aligns current one
kal_uint32 extrainfoId;
for(i=0; i<image_count; i++)
{
if(i < XIM_MAUI_IDX)
{
//Bootloader and ILB are not updateable here
continue;
}
pInfo = NULL;
#if defined(__BIND_TO_CHIP_BASIC__) || defined(__BIND_TO_CHIP__) || defined(__BIND_TO_KEY__)
pImage = pMAUIImage + i;
if(bl_isPImage(pImage))
{
status = bl_getMACRInfo(pDl_Package_GFH->gfh_file_info.m_content_offset + pImage->start_block*block_size_with_spare, &pInfo);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
}
#endif /* defined(__BIND_TO_CHIP_BASIC__) || defined(__BIND_TO_CHIP__) || defined(__BIND_TO_KEY__) */
extrainfoId = GetExtraInfoidByNANDImgid(i);
if(extrainfoId < extra_info_count)
{
if(pDl_Package_GFH->gfh_dl_package_info.m_extra_info[extrainfoId].m_operation == IM_DOWNLOAD)
{
BL_PRINT(LOG_INFO, "Updating MAUI %d @ block %d\n\r", i, flashBlockIdx);
status = bl_DoUpdateMAUI(&flashBlockIdx, i, pInfo);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
BL_PRINT(LOG_INFO, "Update of MAUI %d done\n\r\n\r", i);
}
}
else
{
//Some nand img might not be able to map to flash layout. Process these img here.
BL_PRINT(LOG_INFO, "Updating Misc Img %d @ block %d\n\r", i, flashBlockIdx);
status = bl_DoUpdateMiscImg(&flashBlockIdx, i);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
BL_PRINT(LOG_INFO, "Update of Misc Img %d done\n\r\n\r", i);
}
}
//handle the filesystem
if(status == BL_CD_ERROR_NONE)
{
status = bl_ProcessFilesystem(flashBlockIdx);
}
#ifdef __MTK_SECURE_PLATFORM__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif /* __MTK_SECURE_PLATFORM__ */
return status;
}
#else /* __SV5_ENABLED__ */
#ifdef __CDL_SUPPORT_BOOTCERT_V5__
BL_CD_ERROR_CODE bl_backupBootCert(kal_uint32 backup_addr)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 bootcertidx;
kal_uint32 bootcertblk;
kal_bool isFlashHasBootCert = KAL_FALSE;
//Check if there is BootCert and is needed to be updated
for(bootcertidx = 0; bootcertidx < flash_layout_info.regionCount; bootcertidx++)
{
if(flash_layout_info.region[bootcertidx].binaryType == BOOT_CERT_CTRL)
{
isFlashHasBootCert = KAL_TRUE;
break;
}
}
if(isFlashHasBootCert && isBootCertExist)
{
//Do backup BootCert
bootcertblk = flash_layout_info.region[bootcertidx].u.nandEmmc.startPage/page_per_block;
status = bl_ReadDataFromFlash((kal_uint32*)backup_addr, bootcertblk, MAX_BOOTCERT_LEN, NULL);
}
else if(!isFlashHasBootCert && !isBootCertExist)
{
//There is no bootcert
//return BL_CD_ERROR_NONE;
}
else
{
return BL_CD_ERROR_NO_BOOT_CERT_EXIST;
}
return status;
}
BL_CD_ERROR_CODE bl_restoreBootCert(kal_uint32 backup_addr, kal_uint32 flashBlockIdx, kal_uint32 *pWrittenBlockIdx)
{
FTL_STATUS_CODE status = FTL_SUCCESS;
kal_uint32 i;
kal_uint32 pageToWrite = (MAX_BOOTCERT_LEN/page_size) + 1;
kal_uint32 remainingLength = MAX_BOOTCERT_LEN;
kal_uint32 lenthToWrite;
for(;; flashBlockIdx++)
{
//The MAX_BOOTCERT_LEN must not larger than block size
if(MAX_BOOTCERT_LEN > g_ftlFuncTbl->FTL_GetBlockSize(flashBlockIdx, NULL))
{
return BL_CD_ERROR_BOOTCERT_EXCEED_BLOCKSIZE;
}
status = bl_EraseAndMarkBad(flashBlockIdx, NULL);
if(status == FTL_ERROR_BAD_BLOCK)
{
continue;
}
for(i=0; status==FTL_SUCCESS && i<pageToWrite; i++)
{
lenthToWrite = (remainingLength>page_size) ? page_size : remainingLength;
memset(page_buffer, 0x0, page_size_with_spare);
memcpy(page_buffer, (kal_uint32*)backup_addr, lenthToWrite);
status = g_ftlFuncTbl->FTL_WritePage(flashBlockIdx, i, page_buffer, NULL);
remainingLength -= lenthToWrite;
backup_addr += lenthToWrite;
}
if(status != FTL_SUCCESS)
{
if(status != FTL_ERROR_BAD_BLOCK)
{
BL_PRINT(LOG_CRIT, "Unknown flash error!!!!! %d\n\r", status);
}
//skip this block and re-program
status = g_ftlFuncTbl->FTL_MarkBadBlock(flashBlockIdx, NULL);
continue;
}
break;
}
if(status == FTL_SUCCESS && pWrittenBlockIdx != NULL)
{
*pWrittenBlockIdx = flashBlockIdx;
}
return FTL_ERROR_TO_CD_ERROR(status);
}
#endif /* __CDL_SUPPORT_BOOTCERT_V5__ */
BL_CD_ERROR_CODE bl_DoUpdateMAUI(kal_uint32 *pFlashBlockIdx, kal_uint32 imageIdx)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage = pMAUIImage+imageIdx;
kal_uint32 i;
kal_uint32 flash_max_block_num = 0;
kal_uint32 flash_bound_block = pImage->max_block ? (*pFlashBlockIdx + pImage->max_block -1) : INVALID_BLOCK_IDX;
kal_uint32 FlashLayoutid = GetFlashLayoutidByNANDimgid(imageIdx);
kal_uint32 *pRemapTbl = bl_isENFBImage(pImage) ? ((kal_uint32*)remap_tbl) : NULL;
kal_uint32 pBootCertData = NULL;
#ifdef __CDL_SUPPORT_BOOTCERT_V5__
//Check if it is boot cert. Boot Cert need special proccessing
if(flash_layout_info.region[FlashLayoutid].binaryType == BOOT_CERT_CTRL)
{
pBootCertData = (kal_uint32)(UpdatingRecord.m_reserve);
}
#endif /* __CDL_SUPPORT_BOOTCERT_V5__ */
//Make sure the max_block is valid when it is fixed layout, except BootCert
if((ximFixedLayout == KAL_TRUE) && (pBootCertData == NULL))
{
if(pImage->max_block == 0)
{
return BL_CD_ERROR_INVALID_XIM_IMG_MAX_BLOCK_NUMBER;
}
}
if(codePartialUpdate == KAL_TRUE)
{
//The new max block number must not be larger than original when doing partial update
flash_max_block_num = (flash_layout_info.region[FlashLayoutid].u.nandEmmc.boundPage + 1 -
flash_layout_info.region[FlashLayoutid].u.nandEmmc.startPage)/page_per_block;
if((flash_max_block_num < pImage->max_block) || (pImage->max_block < pImage->blocks))
{
return BL_CD_ERROR_XIM_IMG_SIZE_EXCEED_MAX_VALUE;
}
//The start block should use the original flash_layout_info
*pFlashBlockIdx = flash_layout_info.region[FlashLayoutid].u.nandEmmc.startPage/page_per_block;
//The boundary block shoud keep the same as original while doing partial update.
flash_bound_block = flash_layout_info.region[FlashLayoutid].u.nandEmmc.boundPage/page_per_block;
}
if(pImage->blocks*4 > sizeof(remap_tbl))
{
BL_PRINT(LOG_ERROR, "Block count=%d, large then %d\n\r", pImage->blocks, sizeof(remap_tbl)/4);
return BL_CD_ERROR_3RDROM_REMAP_TBL_TOO_SMALL;
}
if(pRemapTbl)
{
//Skip the first block for 3rd ROM. NAND XIM always put the mapping table at the first block.
//But new rule is to put it at last. So skip first block in XIM's 3rd ROM, and write the table after all 3rd ROM data are written.
pImage->start_block++;
pImage->blocks--;
}
BL_PRINT(LOG_DEBUG, "=>Writing data from xim=%d->%d: ", pImage->start_block, pImage->start_block+pImage->blocks-1);
for(i=0; i<pImage->blocks; i++)
{
kal_uint32 origBlock = *pFlashBlockIdx;
BL_PRINT(LOG_DEBUG, "%d->%d, ", pImage->start_block+i, *pFlashBlockIdx);
#ifdef __CDL_SUPPORT_BOOTCERT_V5__
if(pBootCertData == NULL)
{ //write normal image from xim to flash
status = bl_WriteXimBlockToFlash((pImage->start_block+i)*block_size_with_spare, *pFlashBlockIdx, pFlashBlockIdx, NULL, i*block_size);
}
else
{ //Write back the backup BootCert
status = bl_restoreBootCert(pBootCertData, *pFlashBlockIdx, pFlashBlockIdx);
}
#else /* __CDL_SUPPORT_BOOTCERT_V5__ */
status = bl_WriteXimBlockToFlash((pImage->start_block+i)*block_size_with_spare, *pFlashBlockIdx, pFlashBlockIdx, NULL, i*block_size);
#endif /* __CDL_SUPPORT_BOOTCERT_V5__ */
if(origBlock != *pFlashBlockIdx && status == BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_DEBUG, "%d->%d, ", pImage->start_block+i, *pFlashBlockIdx);
}
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_ERROR, "Error when writing block %d, status%d\n\r", *pFlashBlockIdx, status);
return status;
}
bl_UpdateProgress(UPDATE_PHASE, ((pImage->start_block+i)*page_per_block*100/(im_file_size/page_size_with_spare)));
if(*pFlashBlockIdx > flash_bound_block)
{
BL_PRINT(LOG_ERROR, "Maximum block exceeds, max=%d\n\r", flash_bound_block);
return BL_CD_ERROR_OVER_RESERVED_BOUNDARY;
}
//Update the flash layout and space info when first block is successfully written.
//Do not update them when doing partial update.
//Note that we keep the flexity that the image boundary can be changed while doing full update.
if((i == 0) && (codePartialUpdate == KAL_FALSE))
{
flash_layout_info.region[FlashLayoutid].u.nandEmmc.startPage = (*pFlashBlockIdx)*page_per_block;
if(pSpaceInfo)
{
bl_CDL_NewSpaceInfo(FlashLayoutid)->m_image_start = *pFlashBlockIdx;
}
}
//Update 3rd ROM remap block if necessary
if(pRemapTbl)
{
pRemapTbl[i] = *pFlashBlockIdx;
}
(*pFlashBlockIdx)++;
}
BL_PRINT(LOG_DEBUG, "done\n\r");
//If it is 3RD ROM, append its remapping table
if(pRemapTbl)
{
BL_PRINT(LOG_DEBUG, "=>Write 3rd ROM remap block @ %d...", *pFlashBlockIdx);
if( bl_Write3rdROMBlockToNand(pRemapTbl, pImage->blocks*4, *pFlashBlockIdx, pFlashBlockIdx) != BL_CD_ERROR_NONE)
{
return BL_CD_ERROR_3RDROM_REMAP_TBL_WRITE_FAILURE;
}
BL_PRINT(LOG_DEBUG, " done @ %d\n\r", *pFlashBlockIdx);
(*pFlashBlockIdx)++;
}
//Update the last block in space info
if(pSpaceInfo)
{
bl_CDL_NewSpaceInfo(FlashLayoutid)->m_image_last = *pFlashBlockIdx-1;
//Do not update pSpaceInfo boundary when doing partial update.
//Note that we keep the flexity that the image boundary can be changed while doing full update.
if(codePartialUpdate == KAL_FALSE)
{
if(flash_bound_block != INVALID_BLOCK_IDX)
{
bl_CDL_NewSpaceInfo(FlashLayoutid)->m_image_end = flash_bound_block;
}
else
{
bl_CDL_NewSpaceInfo(FlashLayoutid)->m_image_end = *pFlashBlockIdx-1;
}
}
}
//Do not update flash layout when doing partial update.
//Note that we keep the flexity that the image boundary can be changed while doing full update.
if(codePartialUpdate == KAL_FALSE)
{
if(flash_bound_block != INVALID_BLOCK_IDX)
{
flash_layout_info.region[FlashLayoutid].u.nandEmmc.boundPage = ((flash_bound_block+1)*page_per_block)-1;
}
else
{
flash_layout_info.region[FlashLayoutid].u.nandEmmc.boundPage = ((*pFlashBlockIdx)*page_per_block)-1;
}
}
//Erase unused space if max block is specified
if(flash_bound_block != INVALID_BLOCK_IDX)
{
BL_PRINT(LOG_DEBUG, "=>Erase block for reserved space:");
for(;*pFlashBlockIdx <= flash_bound_block; (*pFlashBlockIdx)++)
{
BL_PRINT(LOG_DEBUG, "%d ", *pFlashBlockIdx);
bl_EraseAndMarkBad(*pFlashBlockIdx, NULL);
}
BL_PRINT(LOG_DEBUG, "done\n\r");
}
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE bl_DoUpdate()
{
kal_uint32 i;
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 flashImgIdx = 0; //For record the processed image index on flash
kal_uint32 ExtraInfoid;
DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD *pImage;
//All image listed in v5 img layout could be updated, thus use the index start from 0 to get the start block
kal_uint32 flashBlockIdx = (BL_Shared_info.m_bl_flash_layout.region[0].u.nandEmmc.startPage)/page_per_block;
//i is the nand xim image id
for(i=0; i<image_count; i++)
{
if(i < XIM_MAUI_IDX) //Bootloader and CBR are not updateable here
{
continue;
}
if(GetFlashLayoutidByNANDimgid(i) < flash_layout_info.regionCount)
{
//Here only process the nand img idx which can be mapped to flash_layout or extra_info.
//All valid operation combination should be checked in bl_ExtraInfoCheck()
//The philosophy here is to simply believe the operation is correct
if(pDl_Package_GFH->gfh_dl_package_info.m_extra_info[GetExtraInfoidByNANDimgid(i)].m_operation == IM_DOWNLOAD)
{
BL_PRINT(LOG_INFO, "Updating MAUI %d @ block %d\n\r", i, flashBlockIdx);
status = bl_DoUpdateMAUI(&flashBlockIdx, i);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
BL_PRINT(LOG_INFO, "Update of MAUI %d done\n\r\n\r", i);
}
}
else
{
//Some nand img might not be able to map to flash layout. Process these img here.
BL_PRINT(LOG_INFO, "Updating Misc Img %d @ block %d\n\r", i, flashBlockIdx);
status = bl_DoUpdateMiscImg(&flashBlockIdx, i);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
BL_PRINT(LOG_INFO, "Update of Misc Img %d done\n\r\n\r", i);
}
}
//handle the filesystem
if(status == BL_CD_ERROR_NONE)
{
status = bl_ProcessFilesystem(flashBlockIdx);
}
return status;
}
#endif /* __SV5_ENABLED__ */
#else /* _NAND_FLASH_BOOTING_ */
/***************************************************************************//**
* @brief The function is for partially updating NOR block from XIM data
*
* This function will readout the unchanged part in flash block to a buffer, then copy the data in
* card into such buffer. At the end, write the buffer back to the flash block to finish the NOR
* partial update.
*
* @param[in] update_start_addr The start address in flash to be updated
* @param[in] length The length to be readout in the card's block
* @param[in] op The operation to be proccesed, it could be IM_DOWNLOAD or IM_ERASE
*
* @return BL_CD_ERROR_CODE
*
******************************************************************************/
BL_CD_ERROR_CODE bl_WriteXimToPartialFlashBlock(kal_uint32 update_start_addr, kal_uint32 length, IM_OPERATION op, kal_uint32 *pinfo, kal_uint32 OffsetInImage)
{
kal_uint32 status;
kal_uint32 blk_start_addr, blk_end_addr, start_offset, end_offset;
kal_uint32 bakbuf = (kal_uint32)&Image$$EXT_READ_WRITE$$ZI$$Limit;
kal_uint32 flash_blk_idx = bl_AddrToBlockIdx(update_start_addr, NULL);
kal_uint32 flash_page_idx;
kal_uint32 update_end_addr = update_start_addr + length;
kal_uint32 blksize = g_ftlFuncTbl->FTL_GetBlockSize(flash_blk_idx, NULL);
kal_uint32 *bufptr;
//Get blk_start_addr
status = g_ftlFuncTbl->FTL_BlockPageToAddr(flash_blk_idx, 0, &blk_start_addr, NULL);
//Get blk_end_addr
blk_end_addr = blk_start_addr + blksize;
//Check the pre-request: the updated region can not cross other block
ASSERT(update_end_addr<=(blk_start_addr+blksize));
//Calculate the offset
start_offset = update_start_addr - blk_start_addr;
end_offset = update_end_addr - blk_start_addr;
//--Build the updated block data: --
//1.Prepare the upper part (original part)
//The input address is got from card, it is not remapped, thus here need to remap it if necessary
memcpy((kal_uint32*)bakbuf, (kal_uint32*)(blk_start_addr|ROM_ADDR_MASK), start_offset);
//2.Prepare the bottom part (original part)
//The input address is got from card, it is not remapped, thus here need to remap it if necessary
memcpy((kal_uint32*)(bakbuf+end_offset), (kal_uint32*)(update_end_addr|ROM_ADDR_MASK), blk_end_addr-update_end_addr);
//3.Prepare the middle part (updated part)
//Note that there is no spare region in NOR, thus here skip the relatied processing to simlify the flow.
if( bl_DL_Seek(pDl_Package_GFH->gfh_file_info.m_content_offset + update_start_addr, 0) != 0)
{
return BL_CD_ERROR_PACKAGE_SEEK_FAIL;
}
if(op == IM_DOWNLOAD)
{
if(bl_DL_Read((kal_uint32*)(bakbuf+start_offset), length)!= length)
{
return BL_CD_ERROR_PACKAGE_READ_FAIL;
}
#ifndef __SV5_ENABLED__
#if defined(__BIND_TO_CHIP_BASIC__) || defined(__BIND_TO_CHIP__) || defined(__BIND_TO_KEY__)
if(pinfo)
{
//The length must be 8 bytes aligned, but the region we interested must not in the unaligned last 8 bytes
//in the image's end. Thus here we simply discard the unaligned tail.
SST_ContentPreprocess(pinfo, OffsetInImage, (kal_uint32*)(bakbuf+start_offset), (length>>3)<<3);
}
#endif
#endif /* __SV5_ENABLED__ */
}
else if(op == IM_ERASE)
{
memset((kal_uint32*)(bakbuf+start_offset), (kal_int32)INVALID_4B_CONTENT, length);
}
//--The updated block data had been built --
//Erase flash block
if(bl_EraseAndMarkBad(flash_blk_idx, NULL) == FTL_ERROR_BAD_BLOCK)
{
return BL_CD_ERROR_FLASH_BAD_BLOCK;
}
//Write bakbuf to flash block
flash_page_idx = 0;
for(bufptr = (kal_uint32*)bakbuf; (kal_uint32)bufptr < bakbuf + blksize; (kal_uint32)bufptr += page_size)
{
status = g_ftlFuncTbl->FTL_WritePage(flash_blk_idx, flash_page_idx, bufptr, NULL);
if(status != FTL_SUCCESS)
{
return BL_CD_ERROR_FLASH_PROGRAM;
}
flash_page_idx++;
}
ASSERT(blksize==flash_page_idx*page_size);
return BL_CD_ERROR_NONE;
}
/***************************************************************************//**
* @brief The function is for partially updating NOR block with given data
*
* This function will readout the unchanged part in flash block to a buffer, then copy the data in
* card into such buffer. At the end, write the buffer back to the flash block to finish the NOR
* partial update.
*
* @param[in] update_start_addr The start address in flash to be updated
* @param[in] length The length to be readout in the card's block
* @param[in] src_data_addr The source data address to be write on to flash
*
* @return BL_CD_ERROR_CODE
*
******************************************************************************/
BL_CD_ERROR_CODE bl_PartialUpdateNORblock(kal_uint32 update_start_addr, kal_uint32 length, kal_uint32 src_data_addr)
{
kal_uint32 status;
kal_uint32 blk_start_addr, blk_end_addr, start_offset, end_offset;
kal_uint32 bakbuf = (kal_uint32)&Image$$EXT_READ_WRITE$$ZI$$Limit;
kal_uint32 flash_blk_idx = bl_AddrToBlockIdx(update_start_addr, NULL);
kal_uint32 flash_page_idx;
kal_uint32 update_end_addr = update_start_addr + length;
kal_uint32 blksize = g_ftlFuncTbl->FTL_GetBlockSize(flash_blk_idx, NULL);
kal_uint32 *bufptr;
//Get blk_start_addr
status = g_ftlFuncTbl->FTL_BlockPageToAddr(flash_blk_idx, 0, &blk_start_addr, NULL);
//Get blk_end_addr
blk_end_addr = blk_start_addr + blksize;
//Check the pre-request: the updated region can not cross other block
ASSERT(update_end_addr<=(blk_start_addr+blksize));
//Calculate the offset
start_offset = update_start_addr - blk_start_addr;
end_offset = update_end_addr - blk_start_addr;
//--Build the updated block data: --
//1.Prepare the upper part (original part)
memcpy((kal_uint32*)bakbuf, (kal_uint32*)blk_start_addr, start_offset);
//2.Prepare the bottom part (original part)
memcpy((kal_uint32*)(bakbuf+end_offset), (kal_uint32*)update_end_addr, blk_end_addr-update_end_addr);
//3.Prepare the middle part (updated part)
memcpy((kal_uint32*)(bakbuf+start_offset), (kal_uint32*)src_data_addr, length);
//--The updated block data had been built --
//Erase flash block
if(bl_EraseAndMarkBad(flash_blk_idx, NULL) == FTL_ERROR_BAD_BLOCK)
{
return BL_CD_ERROR_FLASH_BAD_BLOCK;
}
//Write bakbuf to flash block
flash_page_idx = 0;
for(bufptr = (kal_uint32*)bakbuf; (kal_uint32)bufptr < bakbuf + blksize; (kal_uint32)bufptr += page_size)
{
status = g_ftlFuncTbl->FTL_WritePage(flash_blk_idx, flash_page_idx, bufptr, NULL);
if(status != FTL_SUCCESS)
{
return BL_CD_ERROR_FLASH_PROGRAM;
}
flash_page_idx++;
}
ASSERT(blksize==flash_page_idx*page_size);
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE bl_DoUpdateMAUI(kal_uint32 addr, kal_uint32 length, IM_OPERATION op, kal_uint32* pInfo)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 i;
kal_uint32 flashBlockIdx = bl_AddrToBlockIdx(addr, NULL);
kal_uint32 blockEnd = bl_AddrToBlockIdx(addr+length-1, NULL);
kal_uint32 addrEnd = addr + length;
kal_uint32 partialUpdateStart, partialUpdateEnd, updatingLength;
kal_uint32 partialUpdateFlag = KAL_FALSE;
kal_uint32 p = addr;
ASSERT(op == IM_DOWNLOAD || op == IM_ERASE);
BL_PRINT(LOG_DEBUG, "=>Writing data from xim=%d->%d (0x%x~0x%x): ", flashBlockIdx, blockEnd, addr, addr+length);
for(; flashBlockIdx<=blockEnd; flashBlockIdx++)
{
kal_uint32 origBlock = flashBlockIdx;
//In MBA case, the start address of image might not align block size. Manipulate it here.
partialUpdateFlag = KAL_FALSE;
//Check if the first flash block need partial update
if((p==addr) && (!bl_IsAddrOnBoundary(p)))
{
partialUpdateStart = addr;
partialUpdateFlag = KAL_TRUE;
}
else
{
partialUpdateStart = p;
}
//Check if the last flash block need partial update
if((flashBlockIdx==blockEnd) && (!bl_IsAddrOnBoundary(addrEnd)))
{
partialUpdateEnd = addrEnd;
partialUpdateFlag = KAL_TRUE;
}
else
{
//Set partialUpdateEnd to the end of this block
g_ftlFuncTbl->FTL_BlockPageToAddr(flashBlockIdx+1, 0, &partialUpdateEnd, NULL);
}
//Do partial update if needed
if(partialUpdateFlag)
{
if(flashBlockIdx == mauiFirstBlock && flashBlockIdx == mauiFirstBlock+1)
{ //The first block of maui image must be block aligned
return BL_CD_ERROR_ADDRESS_OR_LENGTH_NOT_BLOCK_BOUNDARY;
}
updatingLength = partialUpdateEnd - partialUpdateStart;
BL_PRINT(LOG_DEBUG, "%d(0x%x-0x%x), ", flashBlockIdx, partialUpdateStart, partialUpdateEnd);
status = bl_WriteXimToPartialFlashBlock(partialUpdateStart, updatingLength, op, pInfo, p-addr);
p += updatingLength;
}
else
{
if(flashBlockIdx != mauiFirstBlock && flashBlockIdx != mauiFirstBlock+1)
{
if(op == IM_DOWNLOAD)
{
BL_PRINT(LOG_DEBUG, "%d(0x%x), ", flashBlockIdx, p);
status = bl_WriteXimBlockToFlash(p, flashBlockIdx, &flashBlockIdx, pInfo, p-addr);
if(origBlock != flashBlockIdx && status == BL_CD_ERROR_NONE)
{
//Impossible: There should not be bad blocks in NOR flash...
ASSERT(0);
}
}
else if(op == IM_ERASE)
{
BL_PRINT(LOG_DEBUG, "E %d(0x%x), ", flashBlockIdx, p);
bl_EraseAndMarkBad(flashBlockIdx, NULL);
status = BL_CD_ERROR_NONE;
}
}
p += g_ftlFuncTbl->FTL_GetBlockSize(flashBlockIdx, NULL);
}
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_ERROR, "Error when processing block %d, status%d\n\r", flashBlockIdx, status);
return status;
}
bl_UpdateProgress(UPDATE_PHASE, p*100/im_file_size);
}
BL_PRINT(LOG_DEBUG, "done\n\r");
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE bl_DoUpdateFilesystem(kal_uint32 addr, kal_uint32 length, IM_OPERATION op)
{
kal_uint32 sds_addr, sds_len;
if(op == IM_SKIP)
{
BL_PRINT(LOG_INFO, "This region belongs to a filesystem which should be skiped\n\r");
return BL_CD_ERROR_NONE;
}
#ifdef __SECURE_DATA_STORAGE__
#ifdef __SV5_ENABLED__
bl_getSDSregion(pmaui_gfh_buf, &sds_addr, &sds_len);
#else
bl_getSDSregion(rominfo_buf, &sds_addr, &sds_len);
#endif
//Check the earse range regard with SDS rage
if(sds_len != 0)
{
if(bl_IsRegionOverlap(addr, length, sds_addr, sds_len))
{
BL_PRINT(LOG_INFO, "SDS can't be erased! SDS address:%x, SDS length:%x\n\r\n\r", sds_addr, sds_len);
return BL_CD_ERROR_EARSE_SDS;
}
}
#endif /* __SECURE_DATA_STORAGE__ */
//No special procedure needed for NOR filesystem, just call routines for MAUI
return bl_DoUpdateMAUI(addr, length, op, NULL);
}
BL_CD_ERROR_CODE bl_DoUpdate()
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 i;
kal_uint32* pInfo;
//Find the first MAUI binary starting address
for(i=0; status == BL_CD_ERROR_NONE && i<GFH_DL_PKG_EXTRA_INFO_COUNT; i++)
{
if(pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_type == GFH_FILE_NONE)
{
break;
}
pInfo = NULL;
#ifndef __SV5_ENABLED__
#if defined(__BIND_TO_CHIP_BASIC__) || defined(__BIND_TO_CHIP__) || defined(__BIND_TO_KEY__)
if(pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_type == PRIMARY_MAUI)
{
status = bl_getMACRInfo(pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_start_addr, &pInfo);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
}
#endif /* defined(__BIND_TO_CHIP_BASIC__) || defined(__BIND_TO_CHIP__) || defined(__BIND_TO_KEY__) */
#endif /* __SV5_ENABLED__ */
if( bl_IsValidBinInfoItem(&pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i]) )
{
if((!codePartialUpdate) || (!ximFixedLayout))
{ //MAUI will be only updated in full update
if(( pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_type >= V_MAUI_BINARY &&
pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_type < V_MAUI_BINARY_END) ||
(pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_type == FOTA_UE))
{
status = bl_DoUpdateMAUI(pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_start_addr,
pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_length,
pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_operation,
pInfo);
}
}
//When MBA is on, scatter file will ensure each bin file is block aligned.
//Thus we do not need to consider the case that one page has the data from two bin.
if( pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_type >= V_RESOURCE_BINARY &&
pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_type < V_RESOURCE_BINARY_END)
{
status = bl_DoUpdateMAUI(pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_start_addr,
pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_length,
pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_operation,
NULL);
}
if( pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_type >= V_FILE_SYSTEM_BINARY &&
pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_type < V_FILE_SYSTEM_BINARY_END)
{
status = bl_DoUpdateFilesystem(pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_start_addr,
pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_bin_length,
pDl_Package_GFH->gfh_dl_package_info.m_extra_info[i].m_operation);
}
}
}
return status;
}
#endif /* _NAND_FLASH_BOOTING_ */
/*************************************************************************
* update markers
*************************************************************************/
#ifdef _NAND_FLASH_BOOTING_
#ifndef __SV5_ENABLED__
STATIC kal_uint32 bl_GetILBStart()
{
kal_uint32 ilbStart = BL_Shared_info.m_bl_ilb_info.m_bl_ilb_start;
ASSERT(ilbStart != 0);
return ilbStart;
}
STATIC kal_uint32 bl_GetILBEnd()
{
kal_uint32 ilbEnd = MIN(GetImageInfo(ROMINFO_INDEX+1)->m_start_block-1, bl_GetILBStart()+BL_MAX_ILB_COUNT-1);
return ilbEnd;
}
NFB_ILB_TYPE bl_GetILBType(kal_uint32 blockIdx)
{
extern BOOTL_HEADER BLHeader;
FTL_STATUS_CODE status;
status = g_ftlFuncTbl->FTL_ReadPage(blockIdx, 0, page_buffer, NULL);
if(status == FTL_SUCCESS)
{
// detect if it is a image list block
if (strcmp((kal_char *)page_buffer, SUPER_BLOCK_PATTERN) != 0)
{
//Not image list block
//BL_PRINT(LOG_ERROR, "Unrecognized block @ ILM area, idx=%d\n\r", blockIdx);
return NFB_ILB_EMPTY;
}
memset(page_buffer, 0, sizeof(page_buffer));
//Read the tail
status = g_ftlFuncTbl->FTL_ReadPage(blockIdx, BLHeader.pagesPerBlock-1, page_buffer, NULL);
if(status == FTL_SUCCESS || status == FTL_ERROR_READ_FAILURE)
{
if(CompareILBTailTag(page_buffer, IMAGE_LIST_BLOCK_TAIL_PATTERN))
{
return NFB_ILB_MAUI;
}
else if(CompareILBTailTag(page_buffer, IMAGE_LIST_BLOCK_TEMP_PATTERN))
{
return NFB_ILB_TEMP;
}
else if(CompareILBTailTag(page_buffer, IMAGE_LIST_BLOCK_BACKUP_PATTERN))
{
return NFB_ILB_BAKUP;
}
else if(CompareILBTailTag(page_buffer, IMAGE_LIST_BLOCK_FOTA_PATTERN))
{
return NFB_ILB_FOTA;
}
else if(CompareILBTailTag(page_buffer, IMAGE_LIST_BLOCK_DLPKG_PATTERN))
{
return NFB_ILB_DLPKG;
}
}
}
if(status == FTL_ERROR_BAD_BLOCK)
{
//Treat it as available block
return NFB_ILB_NONE;
}
return NFB_ILB_EMPTY;
}
kal_uint32 bl_SearchForILBType(kal_uint32 ilbAreaStart, kal_uint32 ilbAreaEnd, NFB_ILB_TYPE type)
{
kal_uint32 blockIdx;
for(blockIdx=ilbAreaStart; blockIdx<=ilbAreaEnd; blockIdx++)
{
if(bl_GetILBType(blockIdx) == type)
{
return blockIdx;
}
}
return 0;
}
/* Specific function to find a empty block that doesn't break the rule of Main ILB and dlPkgILB
To find a block for dlPkgIlb, search the block after MainILB and then the block before MainILB.
Finding a block for MainILB is done in a oppside manner */
/* flag is a helper to skip some block since we may have already known some blocks are malfunctioning, so that
we can just skip them to get better robustness */
kal_uint32 bl_SearchForEmptyILBSpace(kal_uint32 ilbAreaStart, kal_uint32 ilbAreaEnd, kal_uint32 starter, NFB_ILB_TYPE type, kal_uint32 *pFlag)
{
kal_int32 step = (type==NFB_ILB_DLPKG) ? -1 : 1;
kal_uint32 i;
NFB_ILB_TYPE ilbType;
ASSERT(ilbAreaEnd-ilbAreaStart+1 <= 32 && BL_MAX_ILB_COUNT<=32);
ASSERT(type == NFB_ILB_DLPKG || type == NFB_ILB_MAUI);
ASSERT(starter == 0 || (starter >= ilbAreaStart && starter<=ilbAreaEnd));
//Stage 1, search the empty block and there are no other blocks between the found one and target except bad ones
for(i=starter-step; starter!=0 && i>=ilbAreaStart && i<=ilbAreaEnd; i-=step)
{
if((1<<(i-ilbAreaStart)) & *pFlag)
{
continue;
}
ilbType = bl_GetILBType(i);
if(ilbType != NFB_ILB_NONE)
{
if(ilbType == NFB_ILB_EMPTY)
{
*pFlag |= (1<<(i-ilbAreaStart));
return i;
}
break;
}
}
//Let stage 2 handle the case without search base
if(starter == 0)
{
starter = ilbAreaStart-1;
step = 1;
}
//Stage 2, find any one empty block in the other way
for(i=starter+step; starter!=0 && i>=ilbAreaStart && i<=ilbAreaEnd; i+=step)
{
if((1<<(i-ilbAreaStart)) & *pFlag)
{
continue;
}
ilbType = bl_GetILBType(i);
if(ilbType == NFB_ILB_EMPTY)
{
*pFlag |= (1<<(i-ilbAreaStart));
return i;
}
}
return 0;
}
BL_CD_ERROR_CODE bl_ScanILBArea(kal_uint32 ilbAreaStart, kal_uint32 ilbAreaEnd, kal_uint32 *pMainILB, kal_uint32 *pDlPkgILB)
{
kal_uint32 blockIdx;
ASSERT(pMainILB && pDlPkgILB);
*pMainILB = 0;
*pDlPkgILB = 0;
//Search for the image list block, and do the sanity test by the way
for(blockIdx=ilbAreaStart; blockIdx<=ilbAreaEnd; blockIdx++)
{
NFB_ILB_TYPE ilbType = bl_GetILBType(blockIdx);
switch(ilbType)
{
case NFB_ILB_MAUI:
{
if(pMainILB)
{
if(*pMainILB != 0)
{
return BL_CD_ERROR_MULTIPLE_MAIN_ILB;
}
*pMainILB = blockIdx;
}
break;
}
case NFB_ILB_DLPKG:
{
if(pDlPkgILB)
{
if(*pDlPkgILB != 0)
{
return BL_CD_ERROR_MULTIPLE_DLPKG_ILB;
}
*pDlPkgILB = blockIdx;
}
break;
}
case NFB_ILB_EMPTY:
case NFB_ILB_NONE:
break;
default:
BL_PRINT(LOG_ERROR, "Found unexpacted ILB, type=%d @ block %d\n\r", ilbType, blockIdx);
return BL_CD_ERROR_UNEXPECTED_ILB_TYPE;
}
}
//Sanity test, if dlPkgILB is after Main ILB, then there should be no blocks other than bad ones
if(pMainILB && pDlPkgILB && *pMainILB && *pDlPkgILB)
{
if(*pMainILB < *pDlPkgILB)
{
kal_uint32 i;
for(i=*pMainILB+1; i<*pDlPkgILB; i++)
{
ASSERT(bl_GetILBType(i) == NFB_ILB_NONE);
}
}
}
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE bl_UpdateImageList(kal_uint32 ilbAreaStart, kal_uint32 ilbAreaEnd)
{
FTL_STATUS_CODE status;
kal_uint32 blockIdx;
kal_uint32 mainILB = 0;
kal_uint32 dlPkgILB = 0;
kal_uint32 i;
kal_uint32 validILBmask = 0;
kal_uint32 uaOffset = 0;
#ifdef __FOTA_DM__
uaOffset = 1;
#endif
//ROMInfo should be the in the first block of MAUI
pImageList->m_image[ROMINFO_INDEX].m_start_block = pImageList->m_image[ROMINFO_INDEX+1+uaOffset].m_start_block;
//Calculate the checksum in space info
if(pSpaceInfo)
{
kal_uint32 checksum = 0;
kal_uint32 *p = (kal_uint32*)pSpaceInfo;
for(; p <(kal_uint32*)(&pSpaceInfo->m_image_space_chksum); p++)
{
checksum += *p;
}
pSpaceInfo->m_image_space_chksum = checksum;
}
if(bl_ScanILBArea(ilbAreaStart, ilbAreaEnd, &mainILB, &dlPkgILB) != BL_CD_ERROR_NONE)
{
return BL_CD_ERROR_SCAN_ILB_FAIL;
}
if(!dlPkgILB)
{
ASSERT(0);
return BL_CD_ERROR_UNABLE_TO_FIND_DLPKG_ILB;
}
if(!mainILB)
{
blockIdx = bl_SearchForEmptyILBSpace(ilbAreaStart, ilbAreaEnd, dlPkgILB, NFB_ILB_MAUI, &validILBmask);
}
else
{
blockIdx = mainILB;
}
if(!blockIdx)
{
BL_PRINT(LOG_ERROR, "Unable to find a empty block for new MainILB\n\r");
return BL_CD_ERROR_OUT_OF_ILB_AREA;
}
while(1)
{
BL_PRINT(LOG_DEBUG, "Updating Main ILB(%d)", blockIdx);
status = bl_EraseAndMarkBad(blockIdx, NULL);
if(status == FTL_SUCCESS)
{
for(i=0; status==FTL_SUCCESS && i<page_per_block; i++)
{
kal_uint32 *pBuf;
if(i == IMAGE_SPACE_PAGE_INDEX && pSpaceInfo)
{
pBuf = space_page;
}
else if(i == IMAGE_LIST_PAGE_INDEX)
{
pBuf = imagelist_page;
}
else if(i == VERSION_INFO_PAGE_INDEX)
{
pBuf = rominfo_page;
}
else
{
if( bl_ReadXIMPage((kal_uint32)page_buffer, pMAUIImage[XIM_IMAGE_IMAGE_LIST_BLOCK_IDX].start_block*page_per_block+i, 1, KAL_FALSE) != 0 )
{
return BL_CD_ERROR_PACKAGE_READ_FAIL;
}
pBuf = page_buffer;
}
BL_PRINT(LOG_DEBUG, ".");
status = g_ftlFuncTbl->FTL_WritePage(blockIdx, i, pBuf, NULL);
}
if(status == FTL_SUCCESS)
{
BL_PRINT(LOG_DEBUG, "done\n\r", i, blockIdx);
break;
}
//Erase again to make sure no confusing data in it
NFB_ErasePhysicalBlockX(blockIdx, KAL_TRUE);
//Mark it bad if the falure is due to bad block
if(status == FTL_ERROR_BAD_BLOCK)
{
BL_PRINT(LOG_WARN, "\n\rBad block found in ILB area @ block %d page %d\n\r", blockIdx, i);
g_ftlFuncTbl->FTL_MarkBadBlock(blockIdx, NULL);
}
else
{
BL_PRINT(LOG_CRIT, "\n\rUnexpected error in ILB area @ block %d page %d, status=%d\n\r", blockIdx, i, status);
}
}
//Note: if occuring any abnormal NAND behavior, try our best to write the main ILB or the target won't boot up
blockIdx = bl_SearchForEmptyILBSpace(ilbAreaStart, ilbAreaEnd, dlPkgILB, NFB_ILB_MAUI, &validILBmask);
if(!blockIdx)
{
return BL_CD_ERROR_OUT_OF_ILB_AREA;
}
}
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE bl_MarkPkgDLB(kal_uint32 ilbAreaStart, kal_uint32 ilbAreaEnd)
{
FTL_STATUS_CODE status = BL_CD_ERROR_NONE;
kal_uint32 blockIdx;
kal_uint32 mainILB = 0;
kal_uint32 dlPkgILB = 0;
kal_uint32 validILBmask = 0;
//If last cdl is fail, we should not do the backup again. Because we might loss power during
//updating the ILB blocks.
if(last_cdl_fail_flag == KAL_TRUE)
{
return BL_CD_ERROR_NONE;
}
if(bl_ScanILBArea(ilbAreaStart, ilbAreaEnd, &mainILB, &dlPkgILB) != BL_CD_ERROR_NONE)
{
return BL_CD_ERROR_SCAN_ILB_FAIL;
}
ASSERT(!mainILB || BL_Shared_info.m_bl_ilb_info.m_bl_ilb_blk == mainILB);
if(dlPkgILB != 0)
{
//Alread in update state
BL_PRINT(LOG_WARN, "Already in Card DL state\n\r");
return BL_CD_ERROR_NONE;
}
blockIdx = bl_SearchForEmptyILBSpace(ilbAreaStart, ilbAreaEnd, mainILB, NFB_ILB_DLPKG, &validILBmask);
if(!blockIdx)
{
BL_PRINT(LOG_ERROR, "No free block between %d to %d to lunch update by card\n\r", ilbAreaStart, ilbAreaEnd);
return BL_CD_ERROR_OUT_OF_ILB_AREA;
}
while(1)
{
kal_uint32 i;
BL_PRINT(LOG_DEBUG, "Copying MainILB(%d)->dlPkgILB(%d)", mainILB, blockIdx);
status = bl_EraseAndMarkBad(blockIdx, NULL);
if(status == FTL_SUCCESS)
{
for(i=0; status==FTL_SUCCESS && i<page_per_block; i++)
{
status = g_ftlFuncTbl->FTL_ReadPage(mainILB, i, page_buffer, NULL);
if(status != FTL_SUCCESS)
{
BL_PRINT(LOG_CRIT, "\n\rUnable to read origianl main ILB, terminated\n\r");
return BL_CD_ERROR_ILB_READ_FAILURE;
}
if(i == page_per_block-1)
{
memcpy(page_buffer, IMAGE_LIST_BLOCK_DLPKG_PATTERN, 28);
}
#ifdef __CDL_SUPPORT_BOOTCERT_V3__
else if(i == page_per_block-MAX_BOOTCERT_PAGE_NUM-1)
{
//the number of page in a block should larger than 4 info page + 1 tail page + 2 bootcert bak page
ASSERT(page_per_block>=(4+1+MAX_BOOTCERT_PAGE_NUM));
//backup bootcert
bl_backupBootCert(blockIdx, i);
continue;
}
else if((i > page_per_block-MAX_BOOTCERT_PAGE_NUM-1) && (i!=(page_per_block-1)))
{
//bl_backupBootCert() will write the pages after page_per_block-MAX_BOOTCERT_PAGE_NUM-1
//Thus skip these pages to avoid bootcert data corruption.
continue;
}
#endif
BL_PRINT(LOG_DEBUG, ".");
status = g_ftlFuncTbl->FTL_WritePage(blockIdx, i, page_buffer, NULL);
}
if(status == FTL_SUCCESS)
{
break;
}
//Erase again to make sure no confusing data in it
g_ftlFuncTbl->FTL_EraseBlock(blockIdx, NULL);
//Mark it bad if the falure is due to bad block
if(status == FTL_ERROR_BAD_BLOCK)
{
BL_PRINT(LOG_WARN, "\n\rBad block found in ILB area @ block %d page %d\n\r", blockIdx, i);
g_ftlFuncTbl->FTL_MarkBadBlock(blockIdx, NULL);
}
else
{
BL_PRINT(LOG_CRIT, "\n\rUnexpected error in ILB area @ block %d page %d, status=%d\n\r", blockIdx, i, status);
}
}
if(status == FTL_ERROR_BAD_BLOCK)
{
blockIdx = bl_SearchForEmptyILBSpace(ilbAreaStart, ilbAreaEnd, mainILB, NFB_ILB_DLPKG, &validILBmask);
if(!blockIdx)
{
BL_PRINT(LOG_ERROR, "No replacement block between %d to %d to lunch update by card\n\r", ilbAreaStart, ilbAreaEnd);
return BL_CD_ERROR_OUT_OF_ILB_AREA;
}
}
else
{
BL_PRINT(LOG_CRIT, "Unexpectaed error in NAND device, stop updating for safe, status=%d\n\r", status);
return BL_CD_ERROR_FLASH_OTHER_ERROR;
}
}
BL_PRINT(LOG_DEBUG, "done\n\r");
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE bl_ErasePkgDLB(kal_uint32 ilbAreaStart, kal_uint32 ilbAreaEnd)
{
kal_uint32 mainILB = 0;
kal_uint32 dlPkgILB = 0;
bl_ScanILBArea(ilbAreaStart, ilbAreaEnd, &mainILB, &dlPkgILB);
if(mainILB)
{
//Erase DLPKG ILB to finish the update
ASSERT(dlPkgILB);
BL_PRINT(LOG_DEBUG, "Erasing dlPkgILB(%d)\n\r", dlPkgILB);
bl_EraseAndMarkBad(dlPkgILB, NULL);
//Copy update image to internal memory to keep on boot up
memcpy(&BL_Shared_info.m_bl_image_list, pImageList, sizeof(BL_Shared_info.m_bl_image_list));
if(pSpaceInfo)
{
memcpy(&BL_Shared_info.m_bl_image_space, pSpaceInfo, sizeof(BL_Shared_info.m_bl_image_space));
}
return BL_CD_ERROR_NONE;
}
else
{
BL_PRINT(LOG_CRIT, "MAIN ILB is not found, how to finished?\n\r");
ASSERT(0);
return BL_CD_ERROR_UNABLE_TO_FIND_MAIN_ILB;
}
}
#else /* __SV5_ENABLED__ */
BL_CD_ERROR_CODE bl_UpdateImageInfo(void)
{
//Calculate the checksum in space info
if(pSpaceInfo)
{
kal_uint32 checksum = 0;
kal_uint32 *p = (kal_uint32*)pSpaceInfo;
for(; p <(kal_uint32*)(&pSpaceInfo->m_image_space_chksum); p++)
{
checksum += *p;
}
pSpaceInfo->m_image_space_chksum = checksum;
}
if(CBR_WriteRecord(E_CBR_IDX_CBR, CBR_RECORD_FLASH_LAYOUT, (kal_uint8 *)&flash_layout_info, sizeof(FlashLayout), NULL) != CBR_SUCCESS)
{
return BL_CD_ERROR_FAIL_TO_WRITE_CBR_IMAGE_INFO;
}
if(CBR_WriteRecord(E_CBR_IDX_CBR, CBR_RECORD_FLASH_SPACE_INFO, (kal_uint8 *)&space_info, sizeof(Nand_ImageSpace_ST), NULL) != CBR_SUCCESS)
{
return BL_CD_ERROR_FAIL_TO_WRITE_CBR_SPACE_INFO;
}
if(CBR_WriteRecord(E_CBR_IDX_CBR, CBR_RECORD_MAUI_INFO, (kal_uint8 *)pMauiInfoInCard, sizeof(GFH_MAUI_INFO_v1), NULL) != CBR_SUCCESS)
{
BL_PRINT(LOG_INFO, "Note: Could not write maui info to CBR\n\r");
}
return BL_CD_ERROR_NONE;
}
#endif /* __SV5_ENABLED__ */
#else /* _NAND_FLASH_BOOTING_ */
STATIC kal_bool bl_IsMarkerFound(kal_uint32 mauiAddr)
{
kal_uint32 i;
kal_uint32 blockIdx;
kal_uint32 markerAddr;
g_ftlFuncTbl->FTL_Init(NULL);
blockIdx = bl_AddrToBlockIdx(mauiAddr, NULL);
markerAddr = mauiAddr + g_ftlFuncTbl->FTL_GetBlockSize(blockIdx, NULL);
for(i=0; i<page_size/4; i++)
{
if(*((kal_uint32*)(markerAddr)+i) != CDL_MARKER)
{
return KAL_FALSE;
}
}
return KAL_TRUE;
}
BL_CD_ERROR_CODE bl_MarkCDL(kal_uint32 mauiAddr)
{
FTL_STATUS_CODE status;
kal_uint32 i;
ASSERT(mauiAddr < MAUI_ROM_START_ADDR - BOOTLOADER_ROM_REGION_LEN + custom_get_NORFLASH_Size());
mauiFirstBlock = bl_AddrToBlockIdx(mauiAddr, NULL);
//If last cdl is fail, we should not do the backup again. Because we might loss power during
//updating the maui info block.
if(last_cdl_fail_flag == KAL_FALSE)
{
//Prepare CDL pattern
for(i=0; i<sizeof(page_buffer)/4; i++)
{
*((kal_uint32*)(page_buffer)+i) = CDL_MARKER;
}
//Erase the MAUI's second block to prevent ROM INFO
status = g_ftlFuncTbl->FTL_EraseBlock(mauiFirstBlock+1, NULL);
//Note that the minimum page number in 1 block is 2.
//It is due to the smallest block size of serial flash is 4kb.
//Assume maui bin is larger than 2 block
#ifndef __SV5_ENABLED__
//Write the ROM info to page 1
if(status == FTL_SUCCESS)
{
status = g_ftlFuncTbl->FTL_WritePage(mauiFirstBlock+1, 1, rominfo_buf, NULL);
}
#else /* __SV5_ENABLED__ */
//Write the ROM info to page 1
if(status == FTL_SUCCESS)
{
status = g_ftlFuncTbl->FTL_WritePage(mauiFirstBlock+1, 1, flash_pmaui_gfh_buf, NULL);
}
#endif /* __SV5_ENABLED__ */
//Write the marker @ page 0
if(status == FTL_SUCCESS)
{
status = g_ftlFuncTbl->FTL_WritePage(mauiFirstBlock+1, 0, page_buffer, NULL);
}
if(status != FTL_SUCCESS)
{
BL_PRINT(LOG_ERROR, "Cannot write maker to MAUI's 2nd block, status=%d\n\r", status);
return BL_CD_ERROR_ERASE_MARKER_BLOCK;
}
}
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE bl_WriteMarkerBlocks(kal_uint32 mauiAddr)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
ASSERT(mauiFirstBlock == bl_AddrToBlockIdx(mauiAddr, NULL));
BL_PRINT(LOG_DEBUG, "Write %d, 0x%x\n\r", mauiFirstBlock, mauiAddr);
//The MAUI address is at bank1 if it is remapped, the XIM address is start from 0x0, thus the mauiAddr needs to be remapped.
status = bl_WriteXimBlockToFlash(mauiAddr&REMAPPING_MASK, mauiFirstBlock, NULL, NULL, 0);
if(status == FTL_SUCCESS)
{
kal_uint32 p = mauiAddr + g_ftlFuncTbl->FTL_GetBlockSize(mauiFirstBlock, NULL);
BL_PRINT(LOG_DEBUG, "Write %d, 0x%x\n\r", mauiFirstBlock+1, p);
//The MAUI address is at bank1 if it is remapped, the XIM address is start from 0x0, thus the p needs to be remapped.
status = bl_WriteXimBlockToFlash(p&REMAPPING_MASK, mauiFirstBlock+1, NULL, NULL, 0);
}
if(status != FTL_SUCCESS)
{
BL_PRINT(LOG_ERROR, "Cannot write data to makers, status=%d\n\r", status);
return BL_CD_ERROR_WRITE_MARKER_BLOCK;
}
return BL_CD_ERROR_NONE;
}
#endif /* _NAND_FLASH_BOOTING_ */
/*************************************************************************
* load rom_info/maui_info
*************************************************************************/
#ifndef __SV5_ENABLED__
#ifdef _NAND_FLASH_BOOTING_
BL_CD_ERROR_CODE bl_LoadILBInXIM()
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 i;
//Read space info in XIM
if(status == BL_CD_ERROR_NONE)
{
status = bl_ReadXIMPage((kal_uint32)space_page, pMAUIImage[XIM_IMAGE_IMAGE_LIST_BLOCK_IDX].start_block*page_per_block+IMAGE_SPACE_PAGE_INDEX, 1, KAL_FALSE);
}
//Read image list in XIM
if(status == BL_CD_ERROR_NONE)
{
status = bl_ReadXIMPage((kal_uint32)imagelist_page, pMAUIImage[XIM_IMAGE_IMAGE_LIST_BLOCK_IDX].start_block*page_per_block+IMAGE_LIST_PAGE_INDEX, 1, KAL_FALSE);
}
//Read ROM info in XIM
if(status == BL_CD_ERROR_NONE)
{
status = bl_ReadXIMPage((kal_uint32)rominfo_page, pMAUIImage[XIM_IMAGE_IMAGE_LIST_BLOCK_IDX].start_block*page_per_block+VERSION_INFO_PAGE_INDEX, 1, KAL_FALSE);
}
if(status == BL_CD_ERROR_NONE)
{
//The default spaceinfo is from flash
//It will be modified during the updating
if((((Nand_ImageSpace_ST*)space_page)->m_image_space_id == FOTA_IMAGE_END_ID) ||
(((Nand_ImageSpace_ST*)space_page)->m_image_count != 0))
{
pSpaceInfo = (Nand_ImageSpace_ST*)space_page;
memcpy((kal_uint32*)pSpaceInfo, (kal_uint32*)(&BL_Shared_info.m_bl_image_space), sizeof(Nand_ImageSpace_ST));
}
//The default image list info is from card, except the start block is from flash
//It will be modified during the updating
pImageList = (Nand_ImageList_S*)imagelist_page;
//Save the image info from card
memcpy((kal_uint32*)(&card_img_info), (kal_uint32*)pImageList, sizeof(Nand_ImageList_S));
//The default image info is from flash, it will be modified during update, the new info might be from card
memcpy((kal_uint32*)pImageList, (kal_uint32*)(&BL_Shared_info.m_bl_image_list), sizeof(Nand_ImageList_S));
}
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_ERROR, "bl_LoadILBInXIM falure = %d\n\r", status);
}
return status;
}
#else /* _NAND_FLASH_BOOTING_ */
BL_CD_ERROR_CODE bl_LoadRomInfoInXIM()
{
extern void *SST_Search_MAUI_Rom_Info(kal_uint32 rom_base, kal_uint32 length);
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
GFH_DL_PACKAGE_INFO_v2 *pPkgInfo = &pDl_Package_GFH->gfh_dl_package_info;
kal_bool found = KAL_FALSE;
kal_uint32 i, pageIdx;
kal_uint32 totalPages = im_file_size/page_size;
kal_uint32 *p = NULL;
//Get the start address of p-maui, we search the infomation directly from it
//to avoid ambiguous. It is because the searched pattern might occur in bootloader region.
kal_uint32 mauiSearchBase = MAUI_ROM_START_ADDR;
//Here we use page_size_with_spare as the divider to calculate the pageIdx. It is
//because bl_ReadXIMPage use this number as page unit. (The XIM header is excluded)
pageIdx = mauiSearchBase/page_size_with_spare;
//Always pre-load the next page in case the structure lies on the boundary of pages
status = bl_ReadXIMPage((kal_uint32)rominfo_buf, pageIdx, 2, KAL_FALSE);
//The first 2 page is already loaded into rominfo_buf
pageIdx += 2;
for(; status == BL_CD_ERROR_NONE && pageIdx<totalPages; pageIdx++)
{
p = (kal_uint32*)SST_Search_MAUI_Rom_Info((kal_uint32)rominfo_buf, sizeof(rominfo_buf));
if(p && p < rominfo_buf + page_size/sizeof(*rominfo_buf))
{
memcpy(rominfo_buf, p, page_size);
found = KAL_TRUE;
break;
}
memcpy(rominfo_buf, rominfo_buf + page_size/sizeof(*rominfo_buf), page_size);
status = bl_ReadXIMPage((kal_uint32)rominfo_buf + page_size, pageIdx, 1, KAL_FALSE);
}
if(status == BL_CD_ERROR_NONE)
{
if(!found)
{
BL_PRINT(LOG_ERROR, "No ROM Info found in PKG\n\r");
status = BL_CD_ERROR_NO_PKG_ROM_INFO_FOUND;
}
}
else
{
BL_PRINT(LOG_ERROR, "bl_LoadRomInfoInXIM falure = %d\n\r", status);
}
return status;
}
#endif /* _NAND_FLASH_BOOTING_ */
#else /* __SV5_ENABLED__ */
BL_CD_ERROR_CODE bl_LoadpMauiGFHInfoInXIM()
{
kal_uint32 status = BL_CD_ERROR_NONE;
GFH_FILE_INFO_v1 *FileInfo;
#ifdef __FOTA_DM__
kal_uint32 uaOffset = 1;
#else
kal_uint32 uaOffset = 0;
#endif
#ifdef _NAND_FLASH_BOOTING_
kal_uint32 pageidx = (pMAUIImage[XIM_MAUI_IDX+uaOffset].start_block)*page_per_block;
#else /* _NAND_FLASH_BOOTING_ */
kal_uint32 pageidx = (pDl_Package_GFH->gfh_dl_package_info.m_extra_info[bl_GetGFHImgIdx(PRIMARY_MAUI)].m_bin_start_addr)/page_size_with_spare;
#endif /* _NAND_FLASH_BOOTING_ */
//Read at least first 512 bytes of p-MAUI from it's begining
//The page size is at least 512 bytes, thus here simply read a page from begining.
status = bl_ReadXIMPage((kal_uint32)pmaui_gfh_buf, pageidx, 1, KAL_FALSE);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
if(GFH_Find((U32)pmaui_gfh_buf, GFH_FILE_INFO, (void **)&FileInfo) != B_OK)
{
return BL_CD_ERROR_NO_PKG_GFH_FILE_INFO_FOUND;
}
//Make sure the size of pmaui_gfh_buf contains the full gfh
if(FileInfo->m_content_offset > sizeof(pmaui_gfh_buf))
{
return BL_CD_ERROR_INSUFFICIENT_GFH_INFO_BUF;
}
//Read the whole GFH
status = bl_ReadXIMPage((kal_uint32)pmaui_gfh_buf, pageidx, (FileInfo->m_content_offset/page_size)+1, KAL_FALSE);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
//Get the MAUI info
if(GFH_Find((U32)pmaui_gfh_buf, GFH_MAUI_INFO, (void **)&pMauiInfoInCard) != B_OK)
{
return BL_CD_ERROR_NO_PKG_GFH_MAUI_INFO_FOUND;
}
#ifdef _NAND_FLASH_BOOTING_
//Get the flash layout from flash instead of from card. These value will be re-written during update.
if(CBR_ReadRecord(E_CBR_IDX_CBR, CBR_RECORD_FLASH_LAYOUT, (kal_uint8 *)&flash_layout_info, sizeof(FlashLayout), NULL) <= 0)
{
return BL_CD_ERROR_UNABLE_TO_FIND_CBR_IMAGE_INFO;
}
//Get the space info from flash instead of from card. These value will be re-written during update.
if(CBR_ReadRecord(E_CBR_IDX_CBR, CBR_RECORD_FLASH_SPACE_INFO, (kal_uint8 *)&space_info, sizeof(Nand_ImageSpace_ST), NULL) <= 0)
{
return BL_CD_ERROR_UNABLE_TO_FIND_CBR_SPACE_INFO;
}
pSpaceInfo = &space_info;
#endif /* _NAND_FLASH_BOOTING_ */
return BL_CD_ERROR_NONE;
}
#endif /* __SV5_ENABLED__ */
/*************************************************************************
* MAIN functions
*************************************************************************/
BL_CD_ERROR_CODE bl_InitialUpdate()
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
#ifdef _NAND_FLASH_BOOTING_
#ifndef __SV5_ENABLED__
status = bl_MarkPkgDLB(bl_GetILBStart(), bl_GetILBEnd());
#else /* __SV5_ENABLED__ */
//If there is no CBR_RECORD_UPDATING_INFO, than we add it
if(CBR_GetRecordLen(E_CBR_IDX_CBR, CBR_RECORD_UPDATING_INFO)<=0)
{
UpdatingRecord.m_info_type_magic = CDL_MARKER;
#ifdef __CDL_SUPPORT_BOOTCERT_V5__
//BootCert is backuped in CBR
status = bl_backupBootCert((kal_uint32)UpdatingRecord.m_reserve);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
#endif /* #ifdef __CDL_SUPPORT_BOOTCERT_V5__ */
//The content of UpdatingRecord should be got during bl_VerifyPKGBody()
if(CBR_AddRecord(E_CBR_IDX_CBR, CBR_RECORD_UPDATING_INFO, (kal_uint8 *)&UpdatingRecord, sizeof(UPDATING_RECORD), SDS_DP_NONE, NULL) != CBR_SUCCESS)
{
return BL_CD_ERROR_FAIL_TO_ADD_CBR_UPDATING_INFO;
}
}
else
{ //If CBR_RECORD_UPDATING_INFO is exist, it means some UA is doing update.
if(last_cdl_fail_flag == KAL_FALSE)
{ //It means other update agent is doing update. CDL agent should not try to do CDL.
return BL_CD_ERROR_OTHER_UA_IS_DOING_UPDATE;
}
//else, we are doing cdl again to recover last cdl fail
}
#endif /* __SV5_ENABLED__ */
#else /* _NAND_FLASH_BOOTING_ */
status = bl_MarkCDL(MAUI_ROM_START_ADDR);
#endif /* _NAND_FLASH_BOOTING_ */
return status;
}
BL_CD_ERROR_CODE bl_FinishUpdate()
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
#ifdef _NAND_FLASH_BOOTING_
#ifndef __SV5_ENABLED__
status = bl_ErasePkgDLB(bl_GetILBStart(), bl_GetILBEnd());
#else /* __SV5_ENABLED__ */
if(CBR_DelRecord(E_CBR_IDX_CBR, CBR_RECORD_UPDATING_INFO) != CBR_SUCCESS)
{
return BL_CD_ERROR_FAIL_TO_DEL_CBR_UPDATING_INFO;
}
#endif /* __SV5_ENABLED__ */
#else
status = bl_WriteMarkerBlocks(MAUI_ROM_START_ADDR);
#ifdef __MTK_SECURE_PLATFORM__
#ifndef __SV5_ENABLED__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif /* __SV5_ENABLED__ */
#endif /* __MTK_SECURE_PLATFORM__ */
#endif
if(status == BL_CD_ERROR_NONE)
{
bl_ClearCardDownloadTrigger();
}
return status;
}
BL_CD_ERROR_CODE bl_ExtraInfoCheck()
{
kal_int32 i, j;
kal_uint32 extraInfoFsCount = 0;
kal_uint32 numUpdateCodeImg = 0; //For checking if all maui image are all updated or not updated
kal_uint32 codeImgExist = KAL_FALSE; //For checking if all maui image are all updated or not updated
GFH_DL_PACKAGE_INFO_v2 *pPkgInfo = &pDl_Package_GFH->gfh_dl_package_info;
for(i=0; i<sizeof(pPkgInfo->m_extra_info)/sizeof(pPkgInfo->m_extra_info[0]); i++)
{
if(pPkgInfo->m_extra_info[i].m_bin_type == GFH_FILE_NONE)
{
extra_info_count = i;
break;
}
BL_PRINT(LOG_DEBUG, "extraInfo[%d] bin_type=%x, addr=%x, size=%d, op=%d\n\r", i, pPkgInfo->m_extra_info[i].m_bin_type, pPkgInfo->m_extra_info[i].m_bin_start_addr, pPkgInfo->m_extra_info[i].m_bin_length, pPkgInfo->m_extra_info[i].m_operation);
if(bl_IsValidBinInfoItem(&pPkgInfo->m_extra_info[i]) == KAL_FALSE)
{
continue;
}
//Check all valid operations
if(!(pPkgInfo->m_extra_info[i].m_operation >= IM_DOWNLOAD && pPkgInfo->m_extra_info[i].m_operation <= IM_ERASE))
{
return BL_CD_ERROR_INVALID_BINARY_OPERATION;
}
//Only FS area allow erasing
if(pPkgInfo->m_extra_info[i].m_operation == IM_ERASE)
{
if(!(pPkgInfo->m_extra_info[i].m_bin_type >= V_FILE_SYSTEM_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_FILE_SYSTEM_BINARY_END))
{
return BL_CD_ERROR_INVALID_BINARY_OPERATION;
}
}
#if defined(__CDL_SUPPORT_BOOTCERT_V3__) || defined(__CDL_SUPPORT_BOOTCERT_V5__)
if(pPkgInfo->m_extra_info[i].m_bin_type == BOOT_CERT_CTRL)
{
isBootCertExist = KAL_TRUE;
}
#endif
//Bootloader can not be updated
if(((pPkgInfo->m_extra_info[i].m_bin_type == ARM_BL) || (pPkgInfo->m_extra_info[i].m_bin_type == ARM_EXT_BL) || (pPkgInfo->m_extra_info[i].m_bin_type == DUALMAC_DSP_BL)) &&
(pPkgInfo->m_extra_info[i].m_operation != IM_SKIP) )
{
return BL_CD_ERROR_BOOTLOADER_CANNOT_BE_UPDATED;
}
//Mark it if not all file system are going to be updated
if((pPkgInfo->m_extra_info[i].m_operation >= IM_SKIP && pPkgInfo->m_extra_info[i].m_operation < IM_INVALID) &&
(pPkgInfo->m_extra_info[i].m_bin_type >= V_FILE_SYSTEM_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_FILE_SYSTEM_BINARY_END))
{
fsPartialUpdate = KAL_TRUE;
}
// TODO: remove the restriction
//The resource images and 3rd rom must be all updated, the only allowed operation for them is download.
if((pPkgInfo->m_extra_info[i].m_bin_type >= V_RESOURCE_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_RESOURCE_BINARY_END) ||
(pPkgInfo->m_extra_info[i].m_bin_type == THIRD_ROM))
{
if(pPkgInfo->m_extra_info[i].m_operation != IM_DOWNLOAD)
{
return BL_CD_ERROR_INVALID_BINARY_OPERATION;
}
}
//Check if the xim has FOTA or MBA layout
if((pPkgInfo->m_extra_info[i].m_bin_type >= V_RESOURCE_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_RESOURCE_BINARY_END) ||
(pPkgInfo->m_extra_info[i].m_bin_type == FOTA_UE))
{
ximFixedLayout = KAL_TRUE;
}
//Check if it is partial update
//For other images (except filesystem), they must be all updated, or all not updated
if(((pPkgInfo->m_extra_info[i].m_bin_type >= V_MAUI_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_MAUI_BINARY_END) ||
(pPkgInfo->m_extra_info[i].m_bin_type >= V_MISC_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_MISC_BINARY_END) ||
(pPkgInfo->m_extra_info[i].m_bin_type >= V_SECURE_RO && pPkgInfo->m_extra_info[i].m_bin_type < V_SECURE_RO_END) ||
(pPkgInfo->m_extra_info[i].m_bin_type >= V_SRD && pPkgInfo->m_extra_info[i].m_bin_type < V_SRD_END)) &&
(pPkgInfo->m_extra_info[i].m_bin_type != THIRD_ROM))
{
codeImgExist = KAL_TRUE;
//Count the image to be updated.
if(pPkgInfo->m_extra_info[i].m_operation == IM_DOWNLOAD)
{
numUpdateCodeImg++;
}
//If there are some images do not want to be updated, it must be partial update mode.
else
{
codePartialUpdate = KAL_TRUE;
}
//If codePartialUpdate is true, only resource and 3rd rom can be marked as IM_DOWNLOAD
if(codePartialUpdate && numUpdateCodeImg)
{
return BL_CD_ERROR_INVALID_PARTIAL_UPDATE_PACKAGE;
}
}
#ifdef _NAND_FLASH_BOOTING_
//Addr and Length is NA for NAND. IM has all information already, except for FAT region
if(pPkgInfo->m_extra_info[i].m_bin_start_addr || pPkgInfo->m_extra_info[i].m_bin_length)
{
if(!(pPkgInfo->m_extra_info[i].m_bin_type >= V_FILE_SYSTEM_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_FILE_SYSTEM_BINARY_END))
{
return BL_CD_ERROR_XIM_INVALID_PARAM;
}
}
if(pPkgInfo->m_extra_info[i].m_bin_type >= V_FILE_SYSTEM_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_FILE_SYSTEM_BINARY_END)
{
extraInfoFsCount++;
}
#else
//Check no one can touch bootloader region!!!
//The NOR flash start address is pretended at 0x0. The DL package also assume flash is at 0x0.
if(IS_OVERLAP(0, BOOTLOADER_ROM_REGION_LEN, pPkgInfo->m_extra_info[i].m_bin_start_addr, pPkgInfo->m_extra_info[i].m_bin_length) &&
(pPkgInfo->m_extra_info[i].m_bin_type != ARM_BL) && (pPkgInfo->m_extra_info[i].m_bin_type != ARM_EXT_BL)
&& (pPkgInfo->m_extra_info[i].m_bin_type != DUALMAC_DSP_BL)
&& (pPkgInfo->m_extra_info[i].m_bin_type != FOTA_UE))
{
return BL_CD_ERROR_BOOTLOADER_CANNOT_BE_UPDATED;
}
//Check if any invalid binary is specified
if(!( (pPkgInfo->m_extra_info[i].m_bin_type >= V_MAUI_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_MAUI_BINARY_END) ||
(pPkgInfo->m_extra_info[i].m_bin_type >= V_RESOURCE_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_RESOURCE_BINARY_END) ||
(pPkgInfo->m_extra_info[i].m_bin_type >= V_FILE_SYSTEM_BINARY && pPkgInfo->m_extra_info[i].m_bin_type < V_FILE_SYSTEM_BINARY_END) ||
(pPkgInfo->m_extra_info[i].m_bin_type == ARM_BL) ||
(pPkgInfo->m_extra_info[i].m_bin_type == ARM_EXT_BL) ||
(pPkgInfo->m_extra_info[i].m_bin_type == DUALMAC_DSP_BL) ||
(pPkgInfo->m_extra_info[i].m_bin_type == FOTA_UE) ) )
{
return BL_CD_ERROR_INVALID_UPDATING_BINARY;
}
if(pPkgInfo->m_extra_info[i].m_bin_type == SECONDARY_MAUI ||
pPkgInfo->m_extra_info[i].m_bin_type == ON_DEMAND_PAGING ||
pPkgInfo->m_extra_info[i].m_bin_type == THIRD_ROM )
{
return BL_CD_ERROR_INVALID_UPDATING_BINARY;
}
//MAUI should start with the address as the current one
if(pPkgInfo->m_extra_info[i].m_bin_type == PRIMARY_MAUI)
{
//Since the DL-pkg is not remapped, the MAUI_ROM_START_ADDR should not remapped
if(pPkgInfo->m_extra_info[i].m_bin_start_addr != (MAUI_ROM_START_ADDR&REMAPPING_MASK))
{
return BL_CD_ERROR_INVALID_BINARY_ADDRESS;
}
}
//Check if there is any region overlapping the others
for(j=0; j<i-1; j++)
{
if(bl_IsValidBinInfoItem(&pPkgInfo->m_extra_info[j]))
{
if( IS_OVERLAP(pPkgInfo->m_extra_info[i].m_bin_start_addr, pPkgInfo->m_extra_info[i].m_bin_length,
pPkgInfo->m_extra_info[j].m_bin_start_addr, pPkgInfo->m_extra_info[j].m_bin_length ))
return BL_CD_ERROR_REGION_OVERLAP;
}
}
#endif /* _NAND_FLASH_BOOTING_ */
}
//[CAUTION] extra_info_count is only used for NAND SV3 case, it should exclude the FS count.
extra_info_count -= extraInfoFsCount;
//If the package is for partial update, There might be no records for code image in extra_info.
//Thus use codeImgExist to do negative checking
if(!codeImgExist)
{
codePartialUpdate = KAL_TRUE;
}
if((codePartialUpdate == KAL_TRUE) && (ximFixedLayout == KAL_FALSE))
{
return BL_CD_ERROR_INVALID_BINARY_OPERATION;
}
return BL_CD_ERROR_NONE;
}
//Working buffer structure
// GFH <-- pDl_Package_GFH
// NAND_IMAEG_HEADER <-- pDl_Package_Nand_Image_Header (NAND Only)
// FDM MAPPING tbl <-- pFDM5MappingTbl (May be NULL if mapping table is too big to load) (NAND Only)
// Signature * 2 <-- pSignatureBegin
BL_CD_ERROR_CODE bl_LoadAndCheckPKGHeader()
{
//Read the GFH
pDl_Package_GFH = (DL_PACKAGE_GFH*)work_buf;
memset(pDl_Package_GFH, 0, sizeof(DL_PACKAGE_GFH));
if( bl_DL_Read(pDl_Package_GFH, sizeof(DL_PACKAGE_GFH)) != sizeof(DL_PACKAGE_GFH) )
{
return BL_CD_ERROR_NO_DL_PACKAGE_FOUND;
}
//Check if there are marks of DL package
if(GFH_GET_MAGIC(pDl_Package_GFH->gfh_file_info.m_gfh_hdr.m_magic_ver) != GFH_HDR_MAGIC ||
GFH_GET_MAGIC(pDl_Package_GFH->gfh_dl_package_info.m_gfh_hdr.m_magic_ver) != GFH_HDR_MAGIC ||
pDl_Package_GFH->gfh_file_info.m_file_type != CARD_DOWNLOAD_PACKAGE ||
pDl_Package_GFH->gfh_dl_package_info.m_gfh_hdr.m_type != GFH_DL_PACKAGE_INFO ||
strcmp((kal_char*)pDl_Package_GFH->gfh_dl_package_info.m_identifier, GFH_DL_PACKAGE_ID)
)
{
return BL_CD_ERROR_NO_DL_PACKAGE_FOUND;
}
//Validate the GFH
//Check version
ASSERT_VALID_PARAM_IN_XIM_BODY(GFH_GET_VER(pDl_Package_GFH->gfh_file_info.m_gfh_hdr.m_magic_ver) == 1);
ASSERT_VALID_PARAM_IN_XIM_BODY(GFH_GET_VER(pDl_Package_GFH->gfh_dl_package_info.m_gfh_hdr.m_magic_ver) == 2);
//Sanity test
ASSERT_VALID_PARAM_IN_XIM_BODY(pDl_Package_GFH->gfh_file_info.m_content_offset < pDl_Package_GFH->gfh_file_info.m_file_len);
ASSERT_VALID_PARAM_IN_XIM_BODY(pDl_Package_GFH->gfh_file_info.m_file_len - pDl_Package_GFH->gfh_file_info.m_content_offset > 2048 );
#ifdef _NAND_FLASH_BOOTING_
ASSERT_VALID_PARAM_IN_XIM_BODY(pDl_Package_GFH->gfh_dl_package_info.m_im_device == IM_NAND);
#else
ASSERT_VALID_PARAM_IN_XIM_BODY(pDl_Package_GFH->gfh_dl_package_info.m_im_device == IM_NOR);
#endif
dl_package_size = pDl_Package_GFH->gfh_file_info.m_file_len;
im_file_size = dl_package_size - pDl_Package_GFH->gfh_file_info.m_content_offset - pDl_Package_GFH->gfh_dl_package_info.m_sig_len*2;
signatureLength = pDl_Package_GFH->gfh_dl_package_info.m_sig_len*2;
//Sanity test: Check the sig offset
ASSERT_VALID_PARAM_IN_XIM_BODY(pDl_Package_GFH->gfh_dl_package_info.m_sig_offset == dl_package_size - pDl_Package_GFH->gfh_dl_package_info.m_sig_len*2);
#ifdef _NAND_FLASH_BOOTING_
//Read the beginning of NAND header to determin the page size
pDl_Package_Nand_Image_Header = (DL_PACKAGE_NAND_IMAGE_HEADER*)((kal_uint8*)work_buf+sizeof(DL_PACKAGE_GFH));
if( bl_DL_Read(pDl_Package_Nand_Image_Header, 2048) != 2048)
{
return BL_CD_ERROR_PACKAGE_READ_FAIL;
}
//Sanity test for DL_PACKAGE_NAND_IMAGE_HEADER. to enrich
//Only support FDM 5.0
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER(pDl_Package_Nand_Image_Header->FDM_ver == IM_FDMVER_500);
//The block & page size in NAND Image header must be reasonable
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER((pDl_Package_Nand_Image_Header->block_size*1024)%pDl_Package_Nand_Image_Header->page_size == 0);
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER(pDl_Package_Nand_Image_Header->page_size == 512 || pDl_Package_Nand_Image_Header->page_size == 2048 || pDl_Package_Nand_Image_Header->page_size == 4096);
//Compare NAND parameters with current ones
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER(pDl_Package_Nand_Image_Header->page_size == BLHeader.NFIinfo.pageSize);
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER(pDl_Package_Nand_Image_Header->block_size*1024 == BLHeader.pagesPerBlock*BLHeader.NFIinfo.pageSize);
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER((pDl_Package_Nand_Image_Header->io_width==8 && BLHeader.NFIinfo.IOInterface==IO_8BITS) || (pDl_Package_Nand_Image_Header->io_width==16 && BLHeader.NFIinfo.IOInterface==IO_16BITS));
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER(pDl_Package_Nand_Image_Header->addr_cycle == BLHeader.NFIinfo.addressCycle);
//Copy necessary parameter to local variables
block_size = pDl_Package_Nand_Image_Header->block_size*1024;
page_size = pDl_Package_Nand_Image_Header->page_size;
page_per_block = block_size/page_size;
page_size_with_spare = pDl_Package_Nand_Image_Header->page_size + bl_GetSpareSize(pDl_Package_Nand_Image_Header->page_size);
block_size_with_spare = page_size_with_spare*page_per_block;
//Sanity test: The size of IM file must be multiple of block size
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER(im_file_size%block_size_with_spare == 0);
#endif /* _NAND_FLASH_BOOTING_ */
//Do basic test on the layout decription
{
BL_CD_ERROR_CODE status = bl_ExtraInfoCheck();
if(status != BL_CD_ERROR_NONE)
{
return status;
}
}
return BL_CD_ERROR_NONE;
}
BL_CD_ERROR_CODE bl_ParseAndVerifyPKGHeader()
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint8 *pWorkBufEnd = ((kal_uint8*)work_buf) + sizeof(work_buf);
kal_uint32 read_page;
//Calculate the hash value of header part
bl_Alg_Hash_Init();
bl_Alg_Hash_Append((kal_uint32)pDl_Package_GFH, sizeof(*pDl_Package_GFH));
#ifdef _NAND_FLASH_BOOTING_
{
//Read the header data with all available buffer
DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD emptyRecord, *pImage;
memset(&emptyRecord, INVALID_1B_CONTENT, sizeof(emptyRecord));
//Read a block at most since the minimum length of the NFB header is one block
//Read more than 1 block may cause the need of re-calcuation the hash
//Also assume all the image records can be loaded in to the working buffer, or error BL_CD_ERROR_INSUFFICIENT_WORKING_BUF occurs
read_page = MIN((pWorkBufEnd-(kal_uint8*)pDl_Package_Nand_Image_Header)/page_size, page_per_block);
status = bl_ReadXIMPage((kal_uint32)pDl_Package_Nand_Image_Header, 0, read_page, KAL_TRUE);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
pImage = (DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD*)(pDl_Package_Nand_Image_Header+1);
//NAND flash always has bootloader
pMAUIImage = pImage;
pImage++;
while( (kal_uint8*)(pImage+1) < pWorkBufEnd && memcmp(pImage, &emptyRecord, sizeof(emptyRecord))!=0 )
{
//Sanity test of the DL_PACKAGE_NAND_IMAGE_HEADER_IMG_RECORD record, to enrich
if(pMAUIImage == NULL && !bl_isFSImageRecord(pImage))
{
pMAUIImage = pImage;
}
else if(pFSImage == NULL && bl_isFSImageRecord(pImage))
{
pFSImage = pImage;
}
pImage++;
}
if( (kal_uint8*)(pImage+1) >= pWorkBufEnd )
{
//The image record region is large than expected, something wrong
return BL_CD_ERROR_INSUFFICIENT_WORKING_BUF;
}
//Calculate the number of image records
if(pMAUIImage)
image_count = pFSImage ? (pFSImage-pMAUIImage) : (pImage-pMAUIImage) ;
if(pFSImage)
fs_image_count = pImage-pFSImage;
//Sanity test: NFB with FDM v5 must has at least 3 images in XIM
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER(image_count > 3)
//Parse FDM5's mapping tbl and estimate the end of NAND image header
pFDM5MappingTbl = (DL_PACKAGE_NAND_IMAGE_HEADER_FDM5_MAP_TBL*)(pImage+1);
pFDM5MappingTblOffset = (kal_uint8*)pFDM5MappingTbl-(kal_uint8*)pDl_Package_Nand_Image_Header;
FDM5MappingTblLen = (pFDM5MappingTbl->size * sizeof(pFDM5MappingTbl->log2phy[0]) + sizeof(pFDM5MappingTbl->size));
FDM5MappingTblEntryNum = pFDM5MappingTbl->size;
nand_image_header_len = (kal_uint8*)pFDM5MappingTbl + FDM5MappingTblLen - (kal_uint8*)work_buf - pDl_Package_GFH->gfh_file_info.m_content_offset;
//Calculate the block count of nand image header
header_block_count = (nand_image_header_len+block_size-1)/block_size;
if((kal_uint8*)pFDM5MappingTbl+FDM5MappingTblLen > pWorkBufEnd-signatureLength)
{
//Sorry, we don't have enough space for complete mapping table. Have to perform lookup on the fly
pFDM5MappingTbl = NULL;
}
//Sanity test: the data block must be the first image
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER(pMAUIImage[0].start_block == header_block_count);
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER(pMAUIImage != NULL);
//Sanity test: MAUI image must exists
//Read and caculate the hash value of rest dummy data
while(read_page < header_block_count*page_per_block)
{
status = bl_ReadXIMPage((kal_uint32)page_buffer, read_page, 1, KAL_TRUE);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
read_page++;
bl_UpdateProgress(VERIFY_PHASE, (read_page*100/(im_file_size/page_size_with_spare)));
}
}
#endif /* _NAND_FLASH_BOOTING_ */
bl_Alg_Hash_Finish((kal_uint32)hash_value, sizeof(hash_value));
/* Read signature */
pSignatureBegin = pWorkBufEnd - signatureLength;
#ifdef _NAND_FLASH_BOOTING_
//Test if we still have space in the working buffer for the signature
if( pSignatureBegin < ((kal_uint8*)pDl_Package_Nand_Image_Header) + nand_image_header_len)
{
//GFH + header(with image record) + signature > predefined size of working buf (32K)
return BL_CD_ERROR_INSUFFICIENT_WORKING_BUF;
}
//"Signatures+GFH+ImageHeader" are bigger than the DL package? Impossible...
ASSERT_VALID_PARAM_IN_NAND_IMAEG_HEADER(dl_package_size >= signatureLength + nand_image_header_len + pDl_Package_GFH->gfh_file_info.m_content_offset);
#endif /* _NAND_FLASH_BOOTING_ */
//Seek and read
if(bl_DL_Seek(dl_package_size - signatureLength, 0) != 0)
{
status = BL_CD_ERROR_PACKAGE_SEEK_FAIL;
}
if(status == BL_CD_ERROR_NONE)
{
if(bl_DL_Read(pSignatureBegin, signatureLength) != signatureLength)
{
status = BL_CD_ERROR_PACKAGE_READ_FAIL;
}
}
if(custom_get_CDL_asymmetric_key_len() && status == BL_CD_ERROR_NONE)
{
if( bl_DL_SignatureVerify(hash_value, sizeof(hash_value), pSignatureBegin, signatureLength/2) == KAL_FALSE)
{
BL_PRINT(LOG_WARN, "Invalud header signature\n\r");
status = BL_CD_ERROR_INVALIDE_HEADER_SIG;
}
}
#ifdef __SV5_ENABLED__
//Calculate the xim image index for CBR and p-MAUI
#ifdef _NAND_FLASH_BOOTING_
xim_cbr_index = pDl_Package_Nand_Image_Header->Boot_Info_Count;
xim_maui_index = xim_cbr_index + 1;
#else /* _NAND_FLASH_BOOTING_ */
#ifdef __MTK_SECURE_PLATFORM__
/* under construction !*/
/* under construction !*/
#else /* __MTK_SECURE_PLATFORM__ */
xim_cbr_index = -2;
xim_maui_index = 3;
#endif /* __MTK_SECURE_PLATFORM__ */
#ifdef __DSP_FCORE4__
xim_cbr_index++;
xim_maui_index++;
#endif
#endif /* _NAND_FLASH_BOOTING_ */
#endif /* __SV5_ENABLED__ */
return status;
}
BL_CD_ERROR_CODE bl_VerifyPKGBody()
{
/* Check the integrity of body of the DL package, and version by the way */
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
kal_uint32 currentPage = 0;
kal_uint32 rominfo_addr = 0;
kal_uint32 pkgFC;
kal_uint32 pkgPairedVer, romPairedVer;
kal_uint8 pkgSWVer[64], romSWVer[64];
kal_uint8 pkgPlatformId[128], romPlatformId[128];
kal_uint32 romSearchBase = MAUI_ROM_START_ADDR;
const kal_uint32 updateFreq = (im_file_size/page_size_with_spare) * 0.05; //Update progress bar every 5%
if(bl_DL_Seek(currentPage*page_size_with_spare, 0) != 0)
{
return BL_CD_ERROR_PACKAGE_SEEK_FAIL;
}
if(custom_get_CDL_asymmetric_key_len())
{
bl_Alg_Hash_Init();
//Hash the header
bl_Alg_Hash_Append((kal_uint32)pDl_Package_GFH, sizeof(*pDl_Package_GFH));
while(currentPage < im_file_size/page_size_with_spare)
{
status = bl_ReadXIMPage((kal_uint32)page_buffer, currentPage, 1, KAL_TRUE);
if(status != BL_CD_ERROR_NONE)
{
return status;
}
//Update status
if((currentPage % updateFreq) == 0)
{
BL_PRINT(LOG_DEBUG, ".");
bl_UpdateProgress(VERIFY_PHASE, (currentPage*100/(im_file_size/page_size_with_spare)));
}
currentPage++;
}
bl_Alg_Hash_Finish((kal_uint32)hash_value, sizeof(hash_value));
if( bl_DL_SignatureVerify(hash_value, sizeof(hash_value), pSignatureBegin+signatureLength/2, signatureLength/2) == KAL_FALSE )
{
BL_PRINT(LOG_WARN, "Invalid body signature\n\r");
status = BL_CD_ERROR_INVALIDE_BODY_SIG;
}
BL_PRINT(LOG_DEBUG, "done\n\r");
}
//Loading version related info and let customer to do check
#ifndef __SV5_ENABLED__
//Load the info from T-flash (SV3)
if(status == BL_CD_ERROR_NONE)
{
#ifdef _NAND_FLASH_BOOTING_
status = bl_LoadILBInXIM();
rominfo_addr = (kal_uint32)rominfo_page;
#else /* _NAND_FLASHING_BOOTING_ */
status = bl_LoadRomInfoInXIM();
rominfo_addr = (kal_uint32)rominfo_buf;
#endif /* _NAND_FLASHING_BOOTING_ */
pkgPairedVer = SST_Get_MAUI_Paired_Version(rominfo_addr);
SST_Get_MAUI_Feature_Combination(rominfo_addr, &pkgFC);
SST_Get_SW_Version(rominfo_addr, pkgSWVer, sizeof(pkgSWVer));
SST_Get_Platform_ID(rominfo_addr, pkgPlatformId, sizeof(pkgPlatformId));
}
//Load the info from flash (rom)
if(status == BL_CD_ERROR_NONE)
{
romPairedVer = SST_Get_MAUI_Paired_Version(romSearchBase);
SST_Get_SW_Version(romSearchBase, romSWVer, sizeof(romSWVer));
SST_Get_Platform_ID(romSearchBase, romPlatformId, sizeof(romPlatformId));
}
#else /* #ifndef __SV5_ENABLED__ */
//Load the info from T-flash (SV5)
if(status == BL_CD_ERROR_NONE)
{
GFH_MAUI_INFO_v1 *pMauiInfo;
status = bl_LoadpMauiGFHInfoInXIM();
if(status != BL_CD_ERROR_NONE)
{
return status;
}
pMauiInfo = (GFH_MAUI_INFO_v1*)pMauiInfoInCard;
pkgPairedVer = pMauiInfo->m_bl_maui_paired_ver;
pkgFC = pMauiInfo->m_feature_combination;
memcpy(pkgSWVer, pMauiInfo->m_project_id, sizeof(pkgSWVer));
memcpy(pkgPlatformId, pMauiInfo->m_platform_id, sizeof(pkgPlatformId));
}
//Load the info from flash (rom)
if(status == BL_CD_ERROR_NONE)
{
GFH_MAUI_INFO_v1 *pMauiInfoInTarget;
#ifdef _NAND_FLASH_BOOTING_
extern GFH_MAUI_INFO_v1 *bl_GetMAUIInfo(void);
//If last CDL is failed. the GFH might be corrupted. Thus use the version in the MAUI info backuped in CBR.
if(last_cdl_fail_flag == KAL_TRUE)
{
//The UpdatingRecord should already be got successfully in bl_IsCardDownloadGoing()
pMauiInfoInTarget = &(UpdatingRecord.m_maui_info);
}
else
{ //There is no "Last update fail", thus get the maui info from GFH
pMauiInfoInTarget = bl_GetMAUIInfo();
//backup the maui info into global variable
memcpy(&(UpdatingRecord.m_maui_info), pMauiInfoInTarget, sizeof(GFH_MAUI_INFO_v1));
}
#else /* _NAND_FLASH_BOOTING_ */
kal_uint32 maui_addr = MAUI_ROM_START_ADDR;
//If last CDL is failed. the GFH might be corrupted.
//Thus use the version in the MAUI info backuped at 2nd page of 2nd block
if(last_cdl_fail_flag == KAL_TRUE)
{
kal_uint32 addr = maui_addr + g_ftlFuncTbl->FTL_GetBlockSize(bl_AddrToBlockIdx(maui_addr, NULL), NULL) + page_size;
if(GFH_Find((U32)addr, GFH_MAUI_INFO, (void **)&pMauiInfoInTarget) != B_OK)
{
return BL_CD_ERROR_NO_ROM_GFH_MAUI_INFO_FOUND;
}
}
else
{ //There is no "Last update fail", thus get the maui info from GFH
if(GFH_Find((U32)maui_addr, GFH_MAUI_INFO, (void **)&pMauiInfoInTarget) != B_OK)
{
return BL_CD_ERROR_NO_ROM_GFH_MAUI_INFO_FOUND;
}
//backup the flash pmaui gfh info into global variable
memcpy(flash_pmaui_gfh_buf, (void*)maui_addr, page_size);
}
#endif /* _NAND_FLASH_BOOTING_*/
romPairedVer = pMauiInfoInTarget->m_bl_maui_paired_ver;
memcpy(romSWVer, pMauiInfoInTarget->m_project_id, sizeof(romSWVer));
memcpy(romPlatformId, pMauiInfoInTarget->m_platform_id, sizeof(romPlatformId));
}
#endif /* #ifndef __SV5_ENABLED__*/
//Check the info
if(status == BL_CD_ERROR_NONE)
{
//Call customer's function to check
//Paired version of BL must be matched the one in XIM
if(romPairedVer == 0)
{
return BL_CD_ERROR_NO_ROM_INFO_FOUND;
}
if(pkgPairedVer == 0)
{
return BL_CD_ERROR_NO_PKG_ROM_INFO_FOUND;
}
if(pkgPairedVer != romPairedVer)
{
return BL_CD_ERROR_MISMATCHED_BL_PAIRED_VERION;
}
//Chech if the current BL can support the features in XIM
if(CheckFeatureCompatibility(pkgFC) == KAL_FALSE)
{
return BL_CD_ERROR_INCOMPATIBLE_FEATURES;
}
//Check if this upgrade/downgrade is allowed. Cusomter can choose the criteria
//Make sure they are all null-terminated
pDl_Package_GFH->gfh_dl_package_info.m_project_id[sizeof(pDl_Package_GFH->gfh_dl_package_info.m_project_id)-1] = 0;
pkgSWVer[sizeof(pkgSWVer)-1] = 0;
romSWVer[sizeof(romSWVer)-1] = 0;
if( custom_CDL_check_dl_package_version(romSWVer, pkgSWVer, pDl_Package_GFH->gfh_dl_package_info.m_project_id) == KAL_FALSE )
{
return BL_CD_ERROR_MISMATCH_DLPKG_SW_VER;
}
//Make sure they are all null-terminated
pDl_Package_GFH->gfh_dl_package_info.m_platform_id[sizeof(pDl_Package_GFH->gfh_dl_package_info.m_platform_id)-1] = 0;
pkgPlatformId[sizeof(pkgPlatformId)-1] = 0;
romPlatformId[sizeof(romPlatformId)-1] = 0;
if( custom_CDL_check_dl_platform_id(romPlatformId, pkgPlatformId, pDl_Package_GFH->gfh_dl_package_info.m_platform_id) == KAL_FALSE )
{
return BL_CD_ERROR_MISMATCH_DLPKG_PLATFORM_ID;
}
if( custom_CDL_customer_info_check((kal_uint32*)&pDl_Package_GFH->gfh_dl_package_info.m_customer_info, sizeof(pDl_Package_GFH->gfh_dl_package_info.m_customer_info)) != KAL_TRUE)
{
return BL_CD_ERROR_CUSTOM_CHECK_FAIL;
}
}
return status;
}
void bl_ShowCDLSuccess(void)
{
#ifdef __LCD_DRIVER_IN_BL__
BL_ShowUpdateFirmwareOK();
#endif /* __LCD_DRIVER_IN_BL__ */
}
void bl_ShowCDLError(BL_CD_ERROR_CODE status)
{
#ifdef __LCD_DRIVER_IN_BL__
kal_uint32 i;
typedef struct {
BL_CD_ERROR_CODE error_start;
BL_CD_ERROR_CODE error_end;
kal_uint16 rgb[3];
} ERROR_MAP;
const ERROR_MAP error_map[] =
{
{BL_CD_ERROR_INVALID_XIM_START, BL_CD_ERROR_INVALID_XIM_END, {255, 255, 0} }, //Yellow
{BL_CD_ERROR_VERSION_ERROR_START, BL_CD_ERROR_VERSION_ERROR_END, {255, 0, 255} }, //Purple
{BL_CD_ERROR_ILB_ERROR_START, BL_CD_ERROR_ILB_ERROR_END, { 0, 255, 255} }, //Light blue
{BL_CD_ERROR_UPDATE_ERROR_START, BL_CD_ERROR_UPDATE_ERROR_END, {255, 0, 0} }, //Red
{BL_CD_ERROR_PACKAGE_ACCESS_ERROR_START, BL_CD_ERROR_PACKAGE_ACCESS_ERROR_END, { 0, 0, 255} }, //Blue
{BL_CD_ERROR_FLASH_ERROR_START, BL_CD_ERROR_FLASH_ERROR_END, { 0, 255, 0} }, //Green
};
if(status == BL_CD_ERROR_NONE)
{
return;
}
bl_DL_InitLCD();
for(i=0; i<sizeof(error_map)/sizeof(*error_map); i++)
{
if(status <= error_map[i].error_start && status >= error_map[i].error_end)
{
BL_ShowUpdateFirmwareFail(error_map[i].rgb[0], error_map[i].rgb[1], error_map[i].rgb[2]);
return;
}
}
//Other erros
BL_ShowUpdateFirmwareFail(0, 0, 0);
#endif /* __LCD_DRIVER_IN_BL__ */
}
BL_CD_ERROR_CODE bl_UpdateMain(kal_bool forcedExec)
{
BL_CD_ERROR_CODE status = BL_CD_ERROR_NONE;
#if defined(__MTK_INTERNAL__)
/* under construction !*/
/* under construction !*/
#endif
BL_PRINT(LOG_DEBUG, "Acessing T-FLASH...\n\r");
//Step1. Open update file
status = bl_DL_Open(DUMMY_FILENAME);
if(status == BL_CD_ERROR_NONE)
{
//Step2. Check if valid PKG file exists
status = bl_LoadAndCheckPKGHeader();
}
else
{
BL_PRINT(LOG_DEBUG, "Open T-FLASH failed...%d\n\r", status);
}
if(status == BL_CD_ERROR_NO_CARD_FOUND || status == BL_CD_ERROR_NO_DL_PACKAGE_FOUND)
{
BL_PRINT(LOG_INFO, "No Card found or no update package found, %d\n\r", status);
if(forcedExec)
{
bl_ShowCDLError(status);
}
else
{
bl_ClearCardDownloadTrigger();
}
bl_DL_Close();
return status;
}
BL_PRINT(LOG_INFO, "\n\r");
//Step3. Integrity check
if(status == BL_CD_ERROR_NONE)
{
bl_UpdateProgress(VERIFY_PHASE, 0);
BL_PRINT(LOG_INFO, "[Check header]\n\r");
status = bl_ParseAndVerifyPKGHeader();
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_DEBUG, "Update Package Herader is bad...%d\n\r", status);
}
}
if(status == BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_INFO, "[Check body]\n\r");
status = bl_VerifyPKGBody();
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_DEBUG, "Update Package Body is bad...%d\n\r", status);
}
}
#ifdef __BAD_BLOCK_EMULATION__
if(status == BL_CD_ERROR_NONE)
{
extern flash_info_2 Flash_Info;
kal_uint32 totalBlock = Flash_Info.deviceInfo_CE[0].deviceSize*1024/(block_size/1024);
bl_MakeBadBlockTable(ilbStart, ilbEnd, ilbEnd+1, totalBlock-1);
}
#endif
//Step4.0 Init flash driver and check device status
if(status == BL_CD_ERROR_NONE)
{
if(g_ftlFuncTbl->FTL_Init(NULL) != FTL_SUCCESS)
{
status = BL_CD_ERROR_FLASH_INIT_FAIL;
}
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_DEBUG, "Init FTL is failed...%d\n\r", status);
}
}
if(status == BL_CD_ERROR_NONE)
{
status = bl_CheckFlashDeviceStatus();
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_DEBUG, "Check falsh device is failed..%d\n\r", status);
}
}
//Step4.1 Start to updating
if(status == BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_INFO, "[Initiate update]\n\r");
status = bl_InitialUpdate();
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_DEBUG, "Initial Update is failed...%d\n\r", status);
}
}
if(status == BL_CD_ERROR_NONE)
{
if(pDl_Package_GFH->gfh_dl_package_info.m_reserve1[0] & 0x02)
{
kal_uint32 i,j;
#ifdef _NAND_FLASH_BOOTING_
#ifndef __SV5_ENABLED__
i = bl_GetILBEnd()+1;
j = pDl_Package_Nand_Image_Header->plane_size*1024/pDl_Package_Nand_Image_Header->block_size-1;
#else /* __SV5_ENABLED__ */
i = (BL_Shared_info.m_bl_flash_layout.region[0].u.nandEmmc.startPage)/page_per_block;
j = pDl_Package_Nand_Image_Header->plane_size*1024/pDl_Package_Nand_Image_Header->block_size-1;
#endif /* __SV5_ENABLED__ */
BL_PRINT(LOG_INFO, "Erase all availalbe blocks, from %d to %d ", i, j);
for(; i<j; i++)
{
bl_EraseAndMarkBad(i, NULL);
BL_PRINT(LOG_INFO, ".");
}
#else /* _NAND_FLASH_BOOTING_ */
kal_uint32 start = MAUI_ROM_START_ADDR;
kal_uint32 length = custom_get_NORFLASH_Size();
kal_int32 endBlock = bl_AddrToBlockIdx(start+length-1, NULL);
BL_PRINT(LOG_INFO, "Erase all availalbe blocks, from 0x%x to 0x%x ", start, start+length);
for(i=bl_AddrToBlockIdx(start, NULL); i<endBlock; i++);
{
bl_EraseAndMarkBad(i, NULL);
BL_PRINT(LOG_INFO, ".");
}
#endif /* _NAND_FLASH_BOOTING_ */
BL_PRINT(LOG_INFO, "done\n\r");
}
bl_UpdateProgress(UPDATE_PHASE, 0);
BL_PRINT(LOG_INFO, "[Perform update]\n\r");
status = bl_DoUpdate();
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_DEBUG, "DoUpdate Body is failed...%d\n\r", status);
}
}
//Step5. Post-process and clean up
if(status == BL_CD_ERROR_NONE)
{
bl_UpdateProgress(FINISH_PHASE, 0);
#ifdef _NAND_FLASH_BOOTING_
BL_PRINT(LOG_WARN, "[Update image list block]\n\r");
#ifndef __SV5_ENABLED__
status = bl_UpdateImageList(bl_GetILBStart(), bl_GetILBEnd());
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_DEBUG, "Update ILB is failed...%d\n\r", status);
}
#else /* __SV5_ENABLED__ */
status = bl_UpdateImageInfo();
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_DEBUG, "Update Image Info is failed...%d\n\r", status);
}
#endif /* __SV5_ENABLED__ */
#endif /* _NAND_FLASH_BOOTING_ */
}
if(status == BL_CD_ERROR_NONE)
{
bl_UpdateProgress(FINISH_PHASE, 50);
BL_PRINT(LOG_INFO, "[Clean up]\n\r");
status = bl_FinishUpdate();
if(status != BL_CD_ERROR_NONE)
{
BL_PRINT(LOG_DEBUG, "Cannot finish...%d\n\r", status);
}
}
if(status == BL_CD_ERROR_NONE)
{
bl_UpdateProgress(FINISH_PHASE, 100);
bl_ShowCDLSuccess();
bl_DL_Close();
BL_PRINT(LOG_INFO, "[Update done]\n\r");
WacthDogDisable();
while(1)
{
bl_DetectPowerOff();
}
}
else
{
bl_ShowCDLError(status);
WacthDogDisable();
while(1)
{
BL_PRINT(LOG_ERROR, "***Something wrong during update, status=%d\n\r", status);
bl_DetectPowerOff();
}
}
return status;
}
#endif /* __EXT_BOOTLOADER__ */
#endif /* __CARD_DOWNLOAD__ */