summaryrefslogtreecommitdiffstats
path: root/src/phDnldNfc.c
blob: 3d851440d9a1393310e38e05a11cba692ef79082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
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
/*
 * Copyright (C) 2010 NXP Semiconductors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*!
* =========================================================================== *
*                                                                             *
*                                                                             *
* \file  phDnldNfc.c                                                          *
* \brief Download Mgmt Interface Source for the Firmware Download.                *
*                                                                             *
*                                                                             *
* Project: NFC-FRI-1.1                                                        *
*                                                                             *
* $Date: Tue Jun 28 14:25:44 2011 $                                           *
* $Author: ing04880 $                                                         *
* $Revision: 1.33 $                                                           *
* $Aliases:  $
*                                                                             *
* =========================================================================== *
*/


/*
################################################################################
***************************** Header File Inclusion ****************************
################################################################################
*/
#include <stdlib.h>
#include <unistd.h>
#include <phNfcConfig.h>
#include <phNfcCompId.h>
#include <phNfcIoctlCode.h>
#include <phDnldNfc.h>
#include <phOsalNfc.h>
#include <phOsalNfc_Timer.h>
#include <phDal4Nfc.h>
#include <utils/Log.h>
/*
################################################################################
****************************** Macro Definitions *******************************
################################################################################
*/

#ifndef STATIC
#define STATIC static
#endif

#if defined (DNLD_SUMMARY) && !defined (DNLD_TRACE)
#define DNLD_TRACE
#endif

/* #if defined(PHDBG_INFO) && defined (PHDBG_CRITICAL_ERROR) */
#if defined(DNLD_TRACE)
extern char phOsalNfc_DbgTraceBuffer[];

#define MAX_TRACE_BUFFER    0x0410
#define Trace_buffer    phOsalNfc_DbgTraceBuffer
/* #define DNLD_PRINT( str )  phOsalNfc_DbgTrace(str) */
#define DNLD_PRINT( str )  phOsalNfc_DbgString(str)
#define DNLD_DEBUG(str, arg) \
    {                                               \
        snprintf(Trace_buffer,MAX_TRACE_BUFFER,str,arg);    \
        phOsalNfc_DbgString(Trace_buffer);              \
    }
#define DNLD_PRINT_BUFFER(msg,buf,len)              \
    {                                               \
        snprintf(Trace_buffer,MAX_TRACE_BUFFER,"\n\t %s:",msg); \
        phOsalNfc_DbgString(Trace_buffer);              \
        phOsalNfc_DbgTrace(buf,len);                \
        phOsalNfc_DbgString("\r");                  \
    }
#else
#define DNLD_PRINT( str )
#define DNLD_DEBUG(str, arg)
#define DNLD_PRINT_BUFFER(msg,buf,len)
#endif

#define DO_DELAY(period)        usleep(period)

/* delay after SW reset cmd in ms, required on uart for XTAL stability */
#define PHDNLD_DNLD_DELAY        5000
//#define PHDNLD_MAX_PACKET        0x0200U  /* Max Total Packet Size is 512 */
#define PHDNLD_MAX_PACKET        32U  /* Max Total Packet Size is 512 */
#define PHDNLD_DATA_SIZE         ((PHDNLD_MAX_PACKET)- 8U) /* 0x01F8U */
                                                     /* Max Data Size is 504 */
#define PHDNLD_MIN_PACKET        0x03U    /* Minimum Packet Size is 3*/

#define DNLD_DEFAULT_RESPONSE_TIMEOUT   0x4000U

#define NXP_FW_MIN_TX_RX_LEN     0x0AU


#if defined( NXP_FW_MAX_TX_RX_LEN ) && \
     ( NXP_FW_MAX_TX_RX_LEN > NXP_FW_MIN_TX_RX_LEN )

#define PHDNLD_FW_TX_RX_LEN   NXP_FW_MAX_TX_RX_LEN

#elif !defined( NXP_FW_MAX_TX_RX_LEN )

/* To specify the Maximum TX/RX Len */
#define NXP_FW_MAX_TX_RX_LEN   0x200
#define PHDNLD_FW_TX_RX_LEN   NXP_FW_MAX_TX_RX_LEN

#else

#define PHDNLD_FW_TX_RX_LEN   NXP_FW_MIN_TX_RX_LEN

#endif

#define PHDNLD_FRAME_LEN_SIZE    0x02U
#define PHDNLD_ADDR_SIZE         0x03U
#define PHDNLD_DATA_LEN_SIZE     0x02U
#define PHDNLD_FRAME_DATA_OFFSET 0x03U

#define DNLD_SM_UNLOCK_MASK      0x01U
#define DNLD_TRIM_MASK           0x02U
#define DNLD_RESET_MASK          0x04U
#define DNLD_VERIFY_MASK         0x08U
#define DNLD_CRITICAL_MASK       0x10U


#define NXP_NFC_IMAG_FW_MAX      0x05U

#define PHDNLD_FW_PATCH_SEC      0x5FU

#define PHDNLD_PAGE_SIZE         0x80U    /* Page Size Configured for 64 Bytes */

#define FW_MAX_SECTION           0x15U    /* Max Number of Sections */

#define DNLD_CRC16_SIZE			 0x02U

#define DNLD_CRC32_SIZE			 0x04U

#define DNLD_CFG_PG_ADDR         0x00008000U
#define DNLD_FW_CODE_ADDR        0x00800000U
#define DNLD_PATCH_CODE_ADDR     0x00018800U
#define DNLD_PATCH_TABLE_ADDR    0x00008200U


/* Raw Command to pass the Data in Download Mode */
#define PHDNLD_CMD_RAW                  0x00U
/* Command to Reset the Device in Download Mode */
#define PHDNLD_CMD_RESET                0x01U
/* Command to Read from the Address specified in Download Mode */
#define PHDNLD_CMD_READ                 0x07U
#define PHDNLD_CMD_READ_LEN             0x0005U
/* Command to write to the Address specified in Download Mode */
#define PHDNLD_CMD_WRITE                0x08U
#define PHDNLD_CMD_SEC_WRITE            0x0CU
#define PHDNLD_CMD_WRITE_MIN_LEN        0x0005U
#define PHDNLD_CMD_WRITE_MAX_LEN        PHDNLD_DATA_SIZE
/* Command to verify the data written */
#define PHDNLD_CMD_CHECK                0x06U
#define PHDNLD_CMD_CHECK_LEN            0x0007U

/* Command to Lock the  */
#define PHDNLD_CMD_LOCK                 0x40U
#define PHDNLD_CMD_LOCK_LEN             0x0002U


/* Command to set the Host Interface properties */
#define PHDNLD_CMD_SET_HIF              0x09U

/* Command to Activate the Patches Updated  */
#define PHDNLD_CMD_ACTIVATE_PATCH       0x0AU

/* Command to verify the Integrity of the data written */
#define PHDNLD_CMD_CHECK_INTEGRITY      0x0BU

/* Command to verify the Integrity of the data written */
#define PHDNLD_CMD_ENCAPSULATE          0x0DU

#define CHECK_INTEGRITY_RESP_CRC16_LEN  0x03U
#define CHECK_INTEGRITY_RESP_CRC32_LEN  0x05U
#define CHECK_INTEGRITY_RESP_COMP_LEN   0x10U


/* Success Response to a Command Sent in the Download Mode */
#define PHDNLD_RESP_SUCCESS             0x00U
/* Timeout Response to a Command Sent in the Download Mode */
#define PHDNLD_RESP_TIMEOUT             0x01U
/* CRC Error Response to a Command Sent in the Download Mode */
#define PHDNLD_RESP_CRC_ERROR           0x02U
/* Access Denied Response to a Command Sent in the Download Mode */
#define PHDNLD_RESP_ACCESS_DENIED       0x08U
/* PROTOCOL Error Response to a Command Sent in the Download Mode */
#define PHDNLD_RESP_PROTOCOL_ERROR      0x0BU
/* Invalid parameter Response to a Command Sent in the Download Mode */
#define PHDNLD_RESP_INVALID_PARAMETER   0x11U
/* Command Not Supported Response to a Command Sent in the Download Mode */
#define PHDNLD_RESP_CMD_NOT_SUPPORTED   0x13U
/* Length parameter error Response to a Command Sent in the Download Mode */
#define PHDNLD_RESP_INVALID_LENGTH      0x18U
/* Checksum Error Response to a Command Sent in the Download Mode */
#define PHDNLD_RESP_CHKSUM_ERROR        0x19U
/* Version already uptodate Response to a Command Sent in the Download Mode */
#define PHDNLD_RESP_VERSION_UPTODATE    0x1DU
/*  Memory operation error during the processing of
                 the Command Frame in the Download Mode */
#define PHDNLD_RESP_MEMORY_UPDATE_ERROR 0x20U
/*  The Chaining of the Command Frame was Successful in the Download Mode */
#define PHDNLD_RESP_CHAINING_SUCCESS    0x21U
/*  The Command is not allowed anymore in the Download Mode */
#define PHDNLD_RESP_CMD_NOT_ALLOWED     0xE0U
/*  The Error during the Chaining the Command Frame in the Download Mode */
#define PHDNLD_RESP_CHAINING_ERROR      0xE6U
/* Write Error Response to a Command Sent in the Download Mode */
#define PHDNLD_RESP_WRITE_ERROR         0x74U

#define PNDNLD_WORD_LEN                 0x04U

#define NXP_MAX_DNLD_RETRY              0x02U

#define NXP_MAX_SECTION_WRITE           0x05U

#define NXP_PATCH_VER_INDEX             0x05U


/*
################################################################################
******************** Enumeration and Structure Definition **********************
################################################################################
*/

typedef enum phDnldNfc_eSeqType{
    DNLD_SEQ_RESET                              = 0x00U,
    DNLD_SEQ_INIT,
    DNLD_SEQ_RAW,
    DNLD_SEQ_LOCK,
    DNLD_SEQ_UNLOCK,
    DNLD_SEQ_UPDATE,
    DNLD_SEQ_ROLLBACK,
    DNLD_SEQ_COMPLETE
} phDnldNfc_eSeqType_t;

typedef enum phDnldNfc_eState
{
    phDnld_Reset_State        = 0x00,
    phDnld_Unlock_State,
    phDnld_Upgrade_State,
    phDnld_Verify_State,
    phDnld_Complete_State,
    phDnld_Invalid_State
}phDnldNfc_eState_t;


typedef enum phDnldNfc_eSeq
{
    phDnld_Reset_Seq        = 0x00,
    phDnld_Activate_Patch,
    phDnld_Deactivate_Patch,
    phDnld_Update_Patch,
    phDnld_Update_Patchtable,
    phDnld_Lock_System,
    phDnld_Unlock_System,
    phDnld_Upgrade_Section,
    phDnld_Verify_Integrity,
    phDnld_Verify_Section,
    phDnld_Complete_Seq,
    phDnld_Raw_Upgrade,
    phDnld_Invalid_Seq
}phDnldNfc_eSeq_t;

typedef enum phDnldNfc_eChkCrc{
    CHK_INTEGRITY_CONFIG_PAGE_CRC     = 0x00U,
    CHK_INTEGRITY_PATCH_TABLE_CRC     = 0x01U,
    CHK_INTEGRITY_FLASH_CODE_CRC      = 0x02U,
    CHK_INTEGRITY_PATCH_CODE_CRC      = 0x03U,
    CHK_INTEGRITY_COMPLETE_CRC        = 0xFFU
} phDnldNfc_eChkCrc_t;



typedef struct hw_comp_tbl
{
   uint8_t           hw_version[3];
   uint8_t           compatibility;
}hw_comp_tbl_t;


typedef struct img_data_hdr
{
  /* Image Identification */
  uint32_t          img_id;
  /* Offset of the Data from the header */
  uint8_t           img_data_offset;
  /* Number of fimware images available in the img_data */
  uint8_t           no_of_fw_img;
  /* Fimware image Padding in the img_data */
  uint8_t           fw_img_pad[2];
 /* HW Compatiblity table for the set of the Hardwares */
  hw_comp_tbl_t     comp_tbl;
  /* This data consists of the firmware images required to download */
}img_data_hdr_t;


typedef struct fw_data_hdr
{
 /* The data offset from the firmware header.
  * Just in case if in future we require to
  * add some more information.
  */
  uint8_t            fw_hdr_len;
  /* Total size of all the sections which needs to be updated */
  uint8_t            no_of_sections;
  uint8_t            hw_comp_no;
  uint8_t            fw_patch;
  uint32_t           fw_version;
}fw_data_hdr_t;



  /* This data consists all the sections that needs to be downloaded */
typedef struct section_hdr
{
  uint8_t            section_hdr_len;
  uint8_t            section_mem_type;
  uint8_t            section_checksum;
  uint8_t            section_conf;
  uint32_t           section_address;
  uint32_t           section_length;
}section_hdr_t;

typedef struct section_info
{
   section_hdr_t *p_sec_hdr;
   uint8_t       *p_trim_data;
  /* The section data consist of the Firmware binary required
   * to be loaded to the particular address.
   */
   uint8_t       *p_sec_data;
  /* The Section checksum to verify the integrity of the section
   * data.
   */
   uint8_t       *p_sec_chksum;
   /** \internal Index used to refer and process the
    *    Firmware Section Data */
   volatile uint32_t           section_offset;

   /** \internal Section Read Sequence */
   volatile uint8_t            section_read;

   /** \internal Section Write Sequence */
   volatile uint8_t            section_write;

   /** \internal TRIM Write Sequence */
   volatile uint8_t            trim_write;

   volatile uint8_t            sec_verify_retry;

}section_info_t;


typedef struct phDnldNfc_sParam
{
    uint8_t data_addr[PHDNLD_ADDR_SIZE];
    uint8_t data_len[PHDNLD_DATA_LEN_SIZE];
    uint8_t data_packet[PHDNLD_DATA_SIZE];
}phDnldNfc_sParam_t;

typedef struct phDnldNfc_sDataHdr
{
    uint8_t frame_type;
    uint8_t frame_length[PHDNLD_FRAME_LEN_SIZE];
}phDnldNfc_sData_Hdr_t;

typedef struct phDnldNfc_sRawHdr
{
    uint8_t frame_type;
    uint8_t frame_length[PHDNLD_FRAME_LEN_SIZE];
}phDnldNfc_sRawHdr_t;

typedef struct phDnldNfc_sRawDataHdr
{
    uint8_t data_addr[PHDNLD_ADDR_SIZE];
    uint8_t data_len[PHDNLD_DATA_LEN_SIZE];
}phDnldNfc_sRawDataHdr_t;

typedef struct phDnldNfc_sChkCrc16_Resp
{
    uint8_t Chk_status;
    uint8_t Chk_Crc16[2];

}phDnldNfc_sChkCrc16_Resp_t;

typedef struct phDnldNfc_sChkCrc32_Resp
{
    uint8_t Chk_status;
    uint8_t Chk_Crc32[4];

}phDnldNfc_sChkCrc32_Resp_t;


typedef struct phDnldNfc_sChkCrcComplete
{
    phDnldNfc_sChkCrc16_Resp_t config_page;
    phDnldNfc_sChkCrc16_Resp_t patch_table;
    phDnldNfc_sChkCrc32_Resp_t flash_code;
    phDnldNfc_sChkCrc32_Resp_t patch_code;
}phDnldNfc_sChkCrcComplete_t;

typedef struct phDnldNfc_sData
{
    uint8_t frame_type;
    uint8_t frame_length[PHDNLD_FRAME_LEN_SIZE];
    union param
    {
        phDnldNfc_sParam_t data_param;
        uint8_t            response_data[PHDNLD_MAX_PACKET];
        uint8_t            cmd_param;
    }param_info;
}phDnldNfc_sData_t;

#ifdef NXP_NFC_MULTIPLE_FW

typedef struct phDnldNfc_sFwImageInfo
{
    /** \internal Data Pointer to the Firmware header section of the Firmware */
    fw_data_hdr_t               *p_fw_hdr;
    /** \internal Buffer pointer to store the Firmware Section Data */
    section_info_t              *p_fw_sec;
    /** \internal Buffer pointer to store the Firmware Raw Data */
    uint8_t                     *p_fw_raw;
}phDnldNfc_sFwImageInfo_t;

#endif /* #ifdef NXP_NFC_MULTIPLE_FW */


typedef struct phDnldNfc_TxInfo
{
    uint8_t       *transmit_frame;

    uint16_t      tx_offset;

    /** \internal Remaining amount of data to be sent */
    uint16_t      tx_len;

    uint16_t      tx_total;

    /** \internal Chain information for the data to be sent */
    uint8_t       tx_chain;

}phDnldNfc_TxInfo_t;


typedef struct phDnldNfc_RxInfo
{
    /** \internal Total length of the received buffer */
    uint16_t      rx_total;
    /** \internal Chain information of the received buffer */
    uint16_t      rx_chain;
    /** \internal Remaining Data information to be read to complete the
      * Data Information.
      */
    uint16_t      rx_remain;

    /** \internal Buffer to Send the Raw Data Frame */
    uint8_t                     raw_buffer_data[PHDNLD_MAX_PACKET
                                                    + PHDNLD_PAGE_SIZE];
}phDnldNfc_RxInfo_t;


typedef struct phDnldNfc_sContext
{
    /** \internal Structure to store the lower interface operations */
    phNfc_sLowerIF_t            lower_interface;

    phNfc_sData_t               *p_fw_version;

    /** \internal Pointer to the Hardware Reference Sturcture */
    phHal_sHwReference_t        *p_hw_ref;

    /** \internal Pointer to the upper layer notification callback function */
    pphNfcIF_Notification_CB_t  p_upper_notify;
    /** \internal Pointer to the upper layer context */
    void                        *p_upper_context;

    /** \internal Timer ID for the Download Abort */
    uint32_t                    timer_id;
    /** \internal Internal Download for the Download Abort */
    uint32_t                    dnld_timeout;
    /** \internal Data Pointer to the Image header section of the Firmware */
    img_data_hdr_t              *p_img_hdr;

#ifdef NXP_NFC_MULTIPLE_FW
    /** \internal Data Pointer to the Firmware Image Information */
    phDnldNfc_sFwImageInfo_t    *p_img_info;
#endif /* #ifdef NXP_NFC_MULTIPLE_FW */

    /** \internal Data Pointer to the Firmware header section of the Firmware */
    fw_data_hdr_t               *p_fw_hdr;
    /** \internal Buffer pointer to store the Firmware Data */
    section_info_t              *p_fw_sec;
    /** \internal Buffer pointer to store the Firmware Raw Data */
    uint8_t                     *p_fw_raw;

    /** \internal Previous Download Size */
    uint32_t                    prev_dnld_size;

    /** \internal Single Data Block to download the Firmware */
    uint8_t                     dnld_data[PHDNLD_MAX_PACKET
                                                    + PHDNLD_PAGE_SIZE];
    /** \internal Index used to refer and process the Download Data */
    volatile uint32_t           dnld_index;

    /** \internal Response Data to process the response */
    phDnldNfc_sData_t           dnld_resp;

    /** \internal Previously downloaded data stored
	  * to compare the written data */
    phNfc_sData_t               dnld_store;

    /** \internal Previously downloaded trimmed data stored  
	  * to compare the written data */
    phNfc_sData_t               trim_store;

    uint8_t                    *p_resp_buffer;

    phDnldNfc_sChkCrcComplete_t chk_integrity_crc;

    phDnldNfc_eChkCrc_t         chk_integrity_param;

#define NXP_FW_SW_VMID_TRIM
#ifdef  NXP_FW_SW_VMID_TRIM

#define NXP_FW_VMID_TRIM_CHK_ADDR   0x0000813DU
#define NXP_FW_VMID_CARD_MODE_ADDR  0x00009931U
#define NXP_FW_VMID_RD_MODE_ADDR    0x00009981U

    uint8_t                     vmid_trim_update;
#endif /* #ifdef  NXP_FW_SW_VMID_TRIM */

    uint8_t						cur_frame_info;

    uint8_t						raw_mode_upgrade;

	uint8_t						*p_patch_table_crc;

	uint8_t						*p_flash_code_crc;

	uint8_t						*p_patch_code_crc;

    uint16_t                    resp_length;

    /** \internal Current FW Section in Process */
    volatile uint8_t            section_index;

    /** \internal Previous Command sent */
    volatile uint8_t            prev_cmd;

    uint8_t                     dnld_retry;

	/** \internal Current Download State */
    volatile uint8_t            cur_dnld_state;
    /** \internal Next Download State */
    volatile uint8_t            next_dnld_state;

    /** \internal Current step in Download Sequence */
    volatile uint8_t            cur_dnld_seq;
    /** \internal Next step in Download Sequence */
    volatile uint8_t            next_dnld_seq;

    /* \internal Data Transmit information */
    phDnldNfc_TxInfo_t          tx_info;

    /* \internal Data Receive information */
    phDnldNfc_RxInfo_t          rx_info;


}phDnldNfc_sContext_t;


/*
################################################################################
******************** Global and Static Variables Definition ********************
################################################################################
*/

#ifndef NFC_TIMER_CONTEXT
static phDnldNfc_sContext_t *gpphDnldContext = NULL;
#endif

#ifdef NXP_FW_DNLD_CHECK_PHASE

#define   NXP_FW_DNLD_COMPLETE_PHASE 0x00U
#define   NXP_FW_DNLD_SYSTEM_PHASE   0x01U
#define   NXP_FW_DNLD_CFG_PHASE      0x02U
#define   NXP_FW_DNLD_DATA_PHASE     0x03U
#define   NXP_FW_DNLD_RAW_PHASE      0x04U
#define   NXP_FW_DNLD_INVALID_PHASE  0xFFU

static uint8_t  gphDnldPhase = NXP_FW_DNLD_COMPLETE_PHASE;

#endif /* #ifdef NXP_FW_DNLD_CHECK_PHASE */

/**/

/*
*************************** Static Function Declaration **************************
*/

STATIC
NFCSTATUS
phDnldNfc_Send_Command(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        uint8_t                 cmd,
                        void                    *params,
                        uint16_t                param_length
                      );

static
NFCSTATUS
phDnldNfc_Process_FW(
                        phDnldNfc_sContext_t    *psDnldContext,
                        phHal_sHwReference_t    *pHwRef
#ifdef NXP_FW_PARAM
                        ,
                        uint8_t                 *nxp_nfc_fw,
                        uint32_t                fw_length
#endif
                     );

STATIC
void
phDnldNfc_Send_Complete (
                                void                    *psContext,
                                void                    *pHwRef,
                                phNfc_sTransactionInfo_t *pInfo
                       );

STATIC
void
phDnldNfc_Receive_Complete (
                                void                    *psContext,
                                void                    *pHwRef,
                                phNfc_sTransactionInfo_t *pInfo
                                );

STATIC
NFCSTATUS
phDnldNfc_Process_Response(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        void                    *pdata,
                        uint16_t                length
                     );


static
NFCSTATUS
phDnldNfc_Resume(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        void                    *pdata,
                        uint16_t                length
                     );

static
NFCSTATUS
phDnldNfc_Resume_Write(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef
                     );

static
NFCSTATUS
phDnldNfc_Process_Write(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        section_info_t          *p_sec_info,
                        uint32_t                *p_sec_offset
                     );

static
NFCSTATUS
phDnldNfc_Sequence(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        void                    *pdata,
                        uint16_t                length
                        );

static
NFCSTATUS
phDnldNfc_Upgrade_Sequence(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        void                    *pdata,
                        uint16_t                length
                        );

STATIC
NFCSTATUS
phDnldNfc_Receive(
                        void                *psContext,
                        void                *pHwRef,
                        uint8_t             *pdata,
                        uint16_t             length
                    );


STATIC
NFCSTATUS
phDnldNfc_Send (
                    void                    *psContext,
                    void                    *pHwRef,
                    uint8_t                 *pdata,
                    uint16_t                length
                    );

STATIC
NFCSTATUS
phDnldNfc_Set_Seq(
                                phDnldNfc_sContext_t    *psDnldContext,
                                phDnldNfc_eSeqType_t    seq_type
                        );

static
void
phDnldNfc_Notify(
                    pphNfcIF_Notification_CB_t  p_upper_notify,
                    void                        *p_upper_context,
                    void                        *pHwRef,
                    uint8_t                     type,
                    void                        *pInfo
               );

STATIC
NFCSTATUS
phDnldNfc_Allocate_Resource (
                                void                **ppBuffer,
                                uint16_t            size
                            );

STATIC
void
phDnldNfc_Release_Resources (
                                phDnldNfc_sContext_t    **ppsDnldContext
                            );

STATIC
void
phDnldNfc_Release_Lower(
                    phDnldNfc_sContext_t        *psDnldContext,
                    void                        *pHwRef
               );


static
NFCSTATUS
phDnldNfc_Read(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        section_info_t          *p_sec_info
                   );

STATIC
void
phDnldNfc_Abort (
                    uint32_t abort_id
#ifdef NFC_TIMER_CONTEXT
                    , void     *dnld_cntxt
#endif
                );


#ifdef DNLD_CRC_CALC

static
void
phDnldNfc_UpdateCrc16(
    uint8_t     crcByte,
    uint16_t    *pCrc
);

STATIC
uint16_t
phDnldNfc_ComputeCrc16(
    uint8_t     *pData,
    uint16_t    length
);


/*
*************************** Function Definitions **************************
*/
#define CRC32_POLYNOMIAL     0xEDB88320L

static uint32_t CRC32Table[0x100];
 
void BuildCRCTable()
{
    unsigned long crc;
    uint8_t i = 0, j = 0;
 
    for ( i = 0; i <= 0xFF ; i++ ) 
    {
        crc = i;
        for ( j = 8 ; j> 0; j-- ) 
        {
            if ( crc & 1 )
            {
                crc = ( crc>> 1 ) ^ CRC32_POLYNOMIAL;
            }
            else
            {
                crc>>= 1;
            }
        }
        CRC32Table[ i ] = crc;
    }
}

/*
* This routine calculates the CRC for a block of data using the
* table lookup method. It accepts an original value for the crc,
* and returns the updated value.
*/
 
uint32_t CalculateCRC32( void *buffer , uint32_t count, uint32_t crc )
{
    uint8_t *p;
    uint32_t temp1;
    uint32_t temp2;
 
    p = (uint8_t *) buffer;
    while ( count-- != 0 ) {
        temp1 = ( crc>> 8 ) & 0x00FFFFFFL;
        temp2 = CRC32Table[ ( (int) crc ^ *p++ ) & 0xff ];
        crc = temp1 ^ temp2;
    }
    return( crc );
}


static
void
phDnldNfc_UpdateCrc16(
    uint8_t     crcByte,
    uint16_t    *pCrc
)
{
    crcByte = (crcByte ^ (uint8_t)((*pCrc) & 0x00FF));
    crcByte = (crcByte ^ (uint8_t)(crcByte << 4));
    *pCrc = (*pCrc >> 8) ^ ((uint16_t)crcByte << 8) ^
                ((uint16_t)crcByte << 3) ^
                ((uint16_t)crcByte >> 4);
}


STATIC
uint16_t
phDnldNfc_ComputeCrc16(
    uint8_t     *pData,
    uint16_t    length
)
{
    uint8_t     crc_byte = 0;
    uint16_t    index = 0;
    uint16_t    crc = 0;

#ifdef CRC_A
        crc = 0x6363; /* ITU-V.41 */
#else
        crc = 0xFFFF; /* ISO/IEC 13239 (formerly ISO/IEC 3309) */
#endif /* #ifdef CRC_A */

    do
    {
        crc_byte = pData[index];
        phDnldNfc_UpdateCrc16(crc_byte, &crc);
        index++;
    } while (index < length);

#ifndef INVERT_CRC
    crc = ~crc; /* ISO/IEC 13239 (formerly ISO/IEC 3309) */
#endif /* #ifndef INVERT_CRC */

/*    *pCrc1 = (uint8_t) (crc & BYTE_MASK);
      *pCrc2 = (uint8_t) ((crc >> 8) & BYTE_MASK); */
    return crc ;
}

#endif /* #ifdef DNLD_CRC_CALC */


/*!
 * \brief Allocation of the Download Interface resources.
 *
 * This function releases and frees all the resources used by Download Mode
 * Feature.
 */

STATIC
NFCSTATUS
phDnldNfc_Allocate_Resource (
                                void                **ppBuffer,
                                uint16_t            size
                            )
{
    NFCSTATUS           status = NFCSTATUS_SUCCESS;

    *ppBuffer = (void *) phOsalNfc_GetMemory(size);
    if( *ppBuffer != NULL )
    {
        (void )memset(((void *)*ppBuffer), 0,
                                    size);
    }
    else
    {
        *ppBuffer = NULL;
        status = PHNFCSTVAL(CID_NFC_DNLD,
                        NFCSTATUS_INSUFFICIENT_RESOURCES);
    }
    return status;
}


/*!
 * \brief Release of the Download Interface resources.
 *
 * This function releases and frees all the resources used by Download layer.
 */

STATIC
void
phDnldNfc_Release_Resources (
                                phDnldNfc_sContext_t    **ppsDnldContext
                            )
{

    if(NULL != (*ppsDnldContext)->p_resp_buffer)
    {
        phOsalNfc_FreeMemory((*ppsDnldContext)->p_resp_buffer);
        (*ppsDnldContext)->p_resp_buffer = NULL;
    }
    if(NULL != (*ppsDnldContext)->dnld_store.buffer)
    {
        phOsalNfc_FreeMemory((*ppsDnldContext)->dnld_store.buffer);
        (*ppsDnldContext)->dnld_store.buffer = NULL;
        (*ppsDnldContext)->dnld_store.length = 0;
    }
    if(NULL != (*ppsDnldContext)->trim_store.buffer)
    {
        phOsalNfc_FreeMemory((*ppsDnldContext)->trim_store.buffer);
        (*ppsDnldContext)->trim_store.buffer = NULL;
        (*ppsDnldContext)->trim_store.length = 0;
    }
    if(NULL != (*ppsDnldContext)->p_fw_sec)
    {
        phOsalNfc_FreeMemory((*ppsDnldContext)->p_fw_sec);
        (*ppsDnldContext)->p_fw_sec = NULL;
    }
    if ( NXP_INVALID_TIMER_ID != (*ppsDnldContext)->timer_id )
    {
        phOsalNfc_Timer_Stop((*ppsDnldContext)->timer_id );
        phOsalNfc_Timer_Delete((*ppsDnldContext)->timer_id );
        (*ppsDnldContext)->timer_id = NXP_INVALID_TIMER_ID;
    }

    phOsalNfc_FreeMemory((*ppsDnldContext));
    (*ppsDnldContext) = NULL;

    return ;
}


STATIC
void
phDnldNfc_Release_Lower(
                    phDnldNfc_sContext_t        *psDnldContext,
                    void                        *pHwRef
               )
{
    phNfc_sLowerIF_t    *plower_if =
                            &(psDnldContext->lower_interface);
    NFCSTATUS            status = NFCSTATUS_SUCCESS;

    PHNFC_UNUSED_VARIABLE(status);

    if(NULL != plower_if->release)
    {
#ifdef DNLD_LOWER_RELEASE
        status = plower_if->release((void *)plower_if->pcontext,
                                        (void *)pHwRef);
#else
        PHNFC_UNUSED_VARIABLE(pHwRef);

#endif
        (void)memset((void *)plower_if,
                                                0, sizeof(phNfc_sLowerIF_t));
        DNLD_DEBUG(" FW_DNLD: Releasing the Lower Layer Resources: Status = %02X\n"
                                                                    ,status);
    }

    return;
}



static
void
phDnldNfc_Notify(
                    pphNfcIF_Notification_CB_t  p_upper_notify,
                    void                        *p_upper_context,
                    void                        *pHwRef,
                    uint8_t                     type,
                    void                        *pInfo
               )
{
    if( ( NULL != p_upper_notify) )
    {
        /* Notify the to the Upper Layer */
        (p_upper_notify)(p_upper_context, pHwRef, type, pInfo);
    }
}


STATIC
NFCSTATUS
phDnldNfc_Set_Seq(
                                phDnldNfc_sContext_t    *psDnldContext,
                                phDnldNfc_eSeqType_t    seq_type
                        )
{
    NFCSTATUS                       status = NFCSTATUS_SUCCESS;
    static  uint8_t                 prev_temp_state = 0;
    static  uint8_t                 prev_temp_seq =
                                (uint8_t) phDnld_Activate_Patch;

    switch(seq_type)
    {
        case DNLD_SEQ_RESET:
        case DNLD_SEQ_INIT:
        {
            psDnldContext->cur_dnld_state =
                                (uint8_t) phDnld_Reset_State;
            psDnldContext->next_dnld_state =
                            (uint8_t)phDnld_Upgrade_State;
            psDnldContext->cur_dnld_seq =
                            (uint8_t)phDnld_Upgrade_Section;
            psDnldContext->next_dnld_seq =
                                psDnldContext->cur_dnld_seq;
            break;
        }
        case DNLD_SEQ_RAW:
        {
            psDnldContext->cur_dnld_state =
                                (uint8_t) phDnld_Reset_State;
            psDnldContext->next_dnld_state =
                            (uint8_t)phDnld_Upgrade_State;
            psDnldContext->cur_dnld_seq =
                            (uint8_t)phDnld_Raw_Upgrade;
            psDnldContext->next_dnld_seq =
                                psDnldContext->cur_dnld_seq;
            break;
        }
        case DNLD_SEQ_UNLOCK:
        {
            psDnldContext->cur_dnld_state =
                                (uint8_t) phDnld_Reset_State;

#ifdef NXP_FW_DNLD_CHECK_PHASE
            if( NXP_FW_DNLD_SYSTEM_PHASE < gphDnldPhase )
            {
                psDnldContext->next_dnld_state =
                                (uint8_t)phDnld_Upgrade_State;
                psDnldContext->cur_dnld_seq =
                                (uint8_t)phDnld_Upgrade_Section;
            }
            else
#endif /* NXP_FW_DNLD_CHECK_PHASE */
            {
                psDnldContext->next_dnld_state =
                                    (uint8_t) phDnld_Unlock_State;
                psDnldContext->cur_dnld_seq =
                                    (uint8_t) phDnld_Activate_Patch;
            }
            psDnldContext->next_dnld_seq =
                                psDnldContext->cur_dnld_seq;
            break;
        }
        case DNLD_SEQ_LOCK:
        {
            psDnldContext->cur_dnld_state =
                                (uint8_t) phDnld_Reset_State;
            psDnldContext->next_dnld_state =
                                (uint8_t) phDnld_Reset_State;
            psDnldContext->cur_dnld_seq =
                                (uint8_t) phDnld_Lock_System;
            psDnldContext->next_dnld_seq =
                                psDnldContext->cur_dnld_seq;
            break;
        }
        case DNLD_SEQ_UPDATE:
        {
            prev_temp_state = (uint8_t) psDnldContext->cur_dnld_state;
            psDnldContext->cur_dnld_state =
                        psDnldContext->next_dnld_state;
            /* psDnldContext->next_dnld_state =
                        (uint8_t)phDnld_Invalid_State ; */
            prev_temp_seq = (uint8_t) psDnldContext->cur_dnld_seq;
            psDnldContext->cur_dnld_seq =
                        psDnldContext->next_dnld_seq;
            break;
        }
        case DNLD_SEQ_ROLLBACK:
        {
            psDnldContext->cur_dnld_seq = (uint8_t)  prev_temp_seq;
            psDnldContext->next_dnld_seq =
                        (uint8_t)phDnld_Invalid_Seq ;
            prev_temp_seq = 0;

            psDnldContext->cur_dnld_state = (uint8_t)  prev_temp_state;
            /* psDnldContext->next_dnld_state =
                        (uint8_t)phDnld_Invalid_State ; */
            prev_temp_state = 0;
            break;
        }
        case DNLD_SEQ_COMPLETE:
        {
            psDnldContext->cur_dnld_state =
                                (uint8_t) phDnld_Reset_State;
            psDnldContext->next_dnld_state =
                                (uint8_t) phDnld_Verify_State;
            psDnldContext->cur_dnld_seq =
                                (uint8_t) phDnld_Verify_Integrity;
            psDnldContext->next_dnld_seq =
                                psDnldContext->cur_dnld_seq ;
            break;
        }
        default:
        {
            break;
        }
    }

    return status;
}



/*!
 * \brief Sends the data the corresponding peripheral device.
 *
 * This function sends the Download data to the connected NFC Pheripheral device
 */


 STATIC
 NFCSTATUS
 phDnldNfc_Send (
                      void                  *psContext,
                      void                  *pHwRef,
                      uint8_t               *pdata,
                      uint16_t              length
                     )
{
    phDnldNfc_sContext_t    *psDnldContext= (phDnldNfc_sContext_t  *)psContext;
    NFCSTATUS               status = NFCSTATUS_SUCCESS;

    phNfc_sLowerIF_t        *plower_if = &(psDnldContext->lower_interface);

    if( (NULL != plower_if)
        && (NULL != plower_if->send)
      )
    {
#ifndef DNLD_SUMMARY
        DNLD_PRINT_BUFFER("Send Buffer",pdata,length);
#endif
        status = plower_if->send((void *)plower_if->pcontext,
                                (void *)pHwRef, pdata, length);

#if defined(FW_DOWNLOAD_TIMER) && \
                (FW_DOWNLOAD_TIMER == 2)
    if (
         (NFCSTATUS_PENDING == status)
        && ( NXP_INVALID_TIMER_ID != psDnldContext->timer_id )
       )
    {
        psDnldContext->dnld_timeout = NXP_DNLD_COMPLETE_TIMEOUT;

        if ( psDnldContext->dnld_timeout
                        <   DNLD_DEFAULT_RESPONSE_TIMEOUT)
        {
            psDnldContext->dnld_timeout
                            = DNLD_DEFAULT_RESPONSE_TIMEOUT;
        }
        /* Start the Download Timer */
        phOsalNfc_Timer_Start( psDnldContext->timer_id,
                psDnldContext->dnld_timeout,
                (ppCallBck_t) phDnldNfc_Abort
#ifdef NFC_TIMER_CONTEXT
                , (void *) psDnldContext
#endif
                );

        DNLD_DEBUG(" DNLD : Timer %X Started ", psDnldContext->timer_id);
        DNLD_DEBUG(" \t\t With %U Timeout \n", psDnldContext->dnld_timeout);
    }

#endif /* (NXP_NFC_DNLD_TIMER == 1) */
    }

    return status;
}


/*!
 * \brief Receives the Download Mode Response from the corresponding peripheral device.
 *
 * This function receives the Download Command Response to the connected NFC
 * Pheripheral device.
 */

STATIC
NFCSTATUS
phDnldNfc_Receive(
                        void                *psContext,
                        void                *pHwRef,
                        uint8_t             *pdata,
                        uint16_t             length
                    )
{
    phDnldNfc_sContext_t    *psDnldContext= (phDnldNfc_sContext_t  *)psContext;
    phNfc_sLowerIF_t *plower_if = NULL ;
    NFCSTATUS         status = NFCSTATUS_SUCCESS;

    if(NULL == psDnldContext )
    {
        status = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        plower_if = &(psDnldContext->lower_interface);

        if( (NULL != plower_if)
            && (NULL != plower_if->receive)
          )
        {
            status = plower_if->receive((void *)plower_if->pcontext,
                                    (void *)pHwRef, pdata, length);
        }
    }
    return status;
}


static
NFCSTATUS
phDnldNfc_Read(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        section_info_t          *p_sec_info
                   )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS;
    phDnldNfc_sData_t       *p_dnld_data =
                 (phDnldNfc_sData_t *)psDnldContext->dnld_data;
    phDnldNfc_sParam_t      *p_data_param =
                        &p_dnld_data->param_info.data_param;
    uint32_t        read_addr = (p_sec_info->p_sec_hdr->section_address
                                            + p_sec_info->section_offset);
    static unsigned sec_type = 0;
    uint8_t         i = 0;
    uint16_t        read_size = 0 ;

    sec_type = (unsigned int)p_sec_info->p_sec_hdr->section_mem_type;

    if( ( FALSE ==  p_sec_info->section_read )
    && ((sec_type & DNLD_TRIM_MASK))
    && (FALSE == p_sec_info->trim_write) )
    {
        read_size = (uint16_t) p_sec_info->p_sec_hdr->section_length;
        DNLD_DEBUG(" FW_DNLD: Section Read  = %X \n", read_size);
    }
    else
    {
        if (( FALSE ==  p_sec_info->section_read )
            && ((sec_type & DNLD_VERIFY_MASK))
            )
        {
            read_size = (uint16_t)(psDnldContext->prev_dnld_size );
            DNLD_DEBUG(" FW_DNLD: Section Read  = %X \n", read_size);
        }
        else if( ( TRUE ==  p_sec_info->section_read )
            && ( TRUE ==  p_sec_info->section_write )
            )
        {
            /*Already Read the Data Hence Ignore the Read */
           DNLD_DEBUG(" FW_DNLD: Already Read, Read Ignored, read_size = %X \n", read_size);
        }
        else
        {
            /* Ignore the Read */
           DNLD_DEBUG(" FW_DNLD: Section Read Status = %X \n", p_sec_info->section_read);
           DNLD_DEBUG(" FW_DNLD: Section Write Status = %X \n", p_sec_info->section_write);
           DNLD_DEBUG(" FW_DNLD: No Read Required, Read_size = %X \n", read_size);
        }
    }

    if (read_size != 0)
    {

        read_size = (uint16_t)((PHDNLD_DATA_SIZE >= read_size)?
                                            read_size: PHDNLD_DATA_SIZE);

        p_dnld_data->frame_length[i] = (uint8_t)0;
        /* Update the LSB of the Data and the Address Parameter*/
        p_data_param->data_addr[i] = (uint8_t)((read_addr  >>
                                 (BYTE_SIZE + BYTE_SIZE)) & BYTE_MASK);
        p_data_param->data_len[i] = (uint8_t)((read_size >>
                                    BYTE_SIZE) & BYTE_MASK);
        i++;

        p_dnld_data->frame_length[i] = (uint8_t)
                            ( PHDNLD_CMD_READ_LEN & BYTE_MASK);
        /* Update the 2nd byte of the Data and the Address Parameter*/
        p_data_param->data_addr[i] = (uint8_t)((read_addr  >>
                               BYTE_SIZE) & BYTE_MASK);
        p_data_param->data_len[i] = (uint8_t) (read_size & BYTE_MASK);
        i++;

        /* Update the 3rd byte of the the Address Parameter*/
        p_data_param->data_addr[i] = (uint8_t)(read_addr & BYTE_MASK);

        status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                    PHDNLD_CMD_READ, NULL , 0 );

        if ( NFCSTATUS_PENDING == status )
        {
            p_sec_info->section_read = TRUE ;
            psDnldContext->next_dnld_state =  phDnld_Upgrade_State;
            DNLD_DEBUG(" FW_DNLD: Memory Read at Address %X : ", read_addr);
            DNLD_DEBUG(" of Size %X \n", read_size);
        }

    }
    return status;
}



static
NFCSTATUS
phDnldNfc_Process_Write(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        section_info_t          *p_sec_info,
                        uint32_t                *p_sec_offset
                     )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS;
    phDnldNfc_sData_t       *p_dnld_data =
                 (phDnldNfc_sData_t *)psDnldContext->dnld_data;
    phDnldNfc_sParam_t      *dnld_data =
                             &p_dnld_data->param_info.data_param;
    uint8_t                 *p_sm_trim_data = (uint8_t *)psDnldContext->
                                        dnld_resp.param_info.response_data;
    uint32_t                dnld_addr = 0;
#ifdef  NXP_FW_SW_VMID_TRIM
    uint32_t                trim_addr = 0;
#endif /* #ifdef NXP_FW_SW_VMID_TRIM */
    static unsigned         sec_type = 0;
    uint8_t                 i = 0;
    uint16_t                dnld_size = 0;
    int cmp_val = 0x00;


    sec_type = (unsigned int)p_sec_info->p_sec_hdr->section_mem_type;

    status = phDnldNfc_Read(psDnldContext, pHwRef, p_sec_info);
    if( NFCSTATUS_PENDING != status )
    {
        if( (TRUE == p_sec_info->trim_write)
            && (TRUE == p_sec_info->section_read)
               && ((sec_type & DNLD_VERIFY_MASK))
          )
        {
            if(NULL != psDnldContext->trim_store.buffer)
            {
                uint32_t  trim_cmp_size = psDnldContext->prev_dnld_size;

                if( p_sec_info->p_sec_hdr->section_address
                              < (DNLD_CFG_PG_ADDR + PHDNLD_PAGE_SIZE) )
                {
                    trim_cmp_size = trim_cmp_size - 2;
                }

                /* Below Comparison fails due to the checksum */
                 cmp_val = phOsalNfc_MemCompare(
                                psDnldContext->trim_store.buffer,
                                  &psDnldContext->dnld_resp.
                                       param_info.response_data[0]
                                          ,trim_cmp_size);
                DNLD_DEBUG(" FW_DNLD: %X Bytes Trim Write Complete ",
                                    psDnldContext->prev_dnld_size);
                DNLD_DEBUG(" Comparison Status %X\n", cmp_val);
            }
            p_sec_info->trim_write = FALSE;
            DNLD_DEBUG(" FW_DNLD: TRIMMED %X Bytes Write Complete\n", psDnldContext->prev_dnld_size);
        }
        else
        {
            if((NULL != psDnldContext->dnld_store.buffer)
                && ((sec_type & DNLD_VERIFY_MASK))
                && (TRUE == p_sec_info->section_write)
                && (TRUE == p_sec_info->section_read)
                )
            {
                cmp_val = phOsalNfc_MemCompare(
                             psDnldContext->dnld_store.buffer,
                               &psDnldContext->dnld_resp.
                                 param_info.response_data[0]
                                 ,psDnldContext->dnld_store.length);
                p_sec_info->section_read = FALSE;
                p_sec_info->section_write = FALSE;
                DNLD_DEBUG(" FW_DNLD: %X Bytes Write Complete ", 
                                    psDnldContext->dnld_store.length);
                DNLD_DEBUG(" Comparison Status %X\n", cmp_val);
            }
            else
            {
                if(( TRUE == p_sec_info->section_write)
                     && ( FALSE == p_sec_info->section_read)
                   )
                {
                  p_sec_info->section_write = FALSE;
                }
            }
            /* p_sec_info->section_read = FALSE; */
        }

        if (( 0 == psDnldContext->dnld_retry )
            && (0 == cmp_val)
            )
        {
            p_sec_info->sec_verify_retry = 0;
            p_sec_info->section_offset = p_sec_info->section_offset +
                            psDnldContext->prev_dnld_size;
            psDnldContext->prev_dnld_size = 0;
            DNLD_DEBUG(" FW_DNLD: Memory Write Retry - %X \n",
                                    psDnldContext->dnld_retry);
        }
        else
        {
            p_sec_info->sec_verify_retry++;
            DNLD_DEBUG(" FW_DNLD: Memory Verification Failed, Retry =  %X \n",
                               p_sec_info->sec_verify_retry);
        }

        if( p_sec_info->sec_verify_retry < NXP_MAX_SECTION_WRITE )
        {

            dnld_addr =  (p_sec_info->p_sec_hdr->section_address + *p_sec_offset);
            dnld_size = (uint16_t)(p_sec_info->p_sec_hdr->section_length
                                                            - *p_sec_offset);
        }
        else
        {
            status = NFCSTATUS_FAILED;
            DNLD_DEBUG(" FW_DNLD: Memory Verification - Maximum Limit, Retry =  %X \n",
                               p_sec_info->sec_verify_retry);
        }
    }


    if (dnld_size != 0)
    {

        dnld_size = (uint16_t)((PHDNLD_DATA_SIZE >= dnld_size)?
                                        dnld_size: PHDNLD_DATA_SIZE);

        /* Update the LSB of the Data and the Address Parameter*/
        dnld_data->data_addr[i] = (uint8_t)((dnld_addr  >>
                                  (BYTE_SIZE + BYTE_SIZE)) & BYTE_MASK);
        dnld_data->data_len[i] = (uint8_t)((dnld_size >> BYTE_SIZE)
                                        & BYTE_MASK);
        p_dnld_data->frame_length[i] = (uint8_t)
                    (((dnld_size + PHDNLD_CMD_WRITE_MIN_LEN) >> BYTE_SIZE)
                                        & BYTE_MASK);
        i++;
        /* Update the 2nd byte of the Data and the Address Parameter*/
        dnld_data->data_addr[i] = (uint8_t)((dnld_addr  >> BYTE_SIZE)
                                        & BYTE_MASK);
        dnld_data->data_len[i] = (uint8_t) (dnld_size & BYTE_MASK);
        p_dnld_data->frame_length[i] = (uint8_t) ((dnld_size +
                            PHDNLD_CMD_WRITE_MIN_LEN) & BYTE_MASK);
        i++;
        /* Update the 3rd byte of the the Address Parameter*/
        dnld_data->data_addr[i] = (uint8_t)(dnld_addr & BYTE_MASK);

        (void)memcpy( dnld_data->data_packet,
                    (p_sec_info->p_sec_data + *p_sec_offset), dnld_size );

        if( ((sec_type & DNLD_TRIM_MASK))
            && (p_sec_info->sec_verify_retry != 0)
            && (NULL != psDnldContext->trim_store.buffer)
            )
        {
            (void)memcpy( dnld_data->data_packet,
                        psDnldContext->trim_store.buffer, dnld_size );
        }
        else if(((sec_type & DNLD_TRIM_MASK))
            && ( TRUE ==  p_sec_info->section_read )
            )
        {
            for(i = 0; i < *(p_sec_info->p_trim_data);i++)
            {

#ifdef  NXP_FW_SW_VMID_TRIM

/*
if(bit 0 of 0x813D is equal to 1) then
   
   Do not overwrite 0x9931 / 0x9981 during download

else

   @0x9931 = 0x79 // card Mode
   @0x9981 = 0x79 // Reader Mode
*/
                trim_addr = p_sec_info->p_sec_hdr->section_address 
                                    + p_sec_info->p_trim_data[i+1];
                if (NXP_FW_VMID_TRIM_CHK_ADDR == trim_addr)
                {
                    psDnldContext->vmid_trim_update = 
                            p_sm_trim_data[p_sec_info->p_trim_data[i+1]] ;
                }

                if((NXP_FW_VMID_CARD_MODE_ADDR == trim_addr) 
                        || (NXP_FW_VMID_RD_MODE_ADDR == trim_addr))
                {
                    if (TRUE == psDnldContext->vmid_trim_update) 
                    {
                        dnld_data->data_packet[p_sec_info->p_trim_data[i+1]] =
                                p_sm_trim_data[p_sec_info->p_trim_data[i+1]] ;
                    }
                }
                else
                     
#endif
                {
                    dnld_data->data_packet[p_sec_info->p_trim_data[i+1]] =
                            p_sm_trim_data[p_sec_info->p_trim_data[i+1]] ;
                }
            }
            if(NULL != psDnldContext->trim_store.buffer)
            {
                phOsalNfc_FreeMemory(psDnldContext->trim_store.buffer);
                psDnldContext->trim_store.buffer = NULL;
                psDnldContext->trim_store.length = 0;
            }
#if 1
            (void)
                phDnldNfc_Allocate_Resource((void **)
                              &(psDnldContext->trim_store.buffer),dnld_size);
#else
            psDnldContext->trim_store.buffer =
                                (uint8_t *) phOsalNfc_GetMemory(dnld_size);
#endif

            if(NULL != psDnldContext->trim_store.buffer)
            {
                (void )memset((void *)psDnldContext->trim_store.buffer,0,
                                            dnld_size);
                (void)memcpy( psDnldContext->trim_store.buffer,
                                dnld_data->data_packet,  dnld_size );
                psDnldContext->trim_store.length = dnld_size;
                DNLD_DEBUG(" FW_DNLD: Write with Trimming at Address %X ", dnld_addr );
                DNLD_DEBUG(" of Size %X and ", dnld_size );
                DNLD_DEBUG(" with %X Trimming Values \n", *(p_sec_info->p_trim_data) );

            }
        }
        else
        {
            if(NULL != psDnldContext->dnld_store.buffer)
            {
                phOsalNfc_FreeMemory(psDnldContext->dnld_store.buffer);
                psDnldContext->dnld_store.buffer = NULL;
                psDnldContext->dnld_store.length = 0;
            }
#if 1
            (void)
                phDnldNfc_Allocate_Resource((void **)
                              &(psDnldContext->dnld_store.buffer),dnld_size);
#else
            psDnldContext->dnld_store.buffer =
                                (uint8_t *) phOsalNfc_GetMemory(dnld_size);
#endif
            if(NULL != psDnldContext->dnld_store.buffer)
            {
                (void )memset((void *)psDnldContext->dnld_store.buffer,0,
                                            dnld_size);
                (void)memcpy( psDnldContext->dnld_store.buffer,
                                dnld_data->data_packet,  dnld_size );
                psDnldContext->dnld_store.length = dnld_size;
                DNLD_DEBUG(" FW_DNLD: Memory Write at Address %X ", dnld_addr );
                DNLD_DEBUG(" of Size %X ", dnld_size );
            }
        }

        if(PHDNLD_FW_PATCH_SEC !=  psDnldContext->p_fw_hdr->fw_patch)
        {
        status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                    PHDNLD_CMD_WRITE, NULL , 0 );
        }
        else
        {
            status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                        PHDNLD_CMD_SEC_WRITE, NULL , 0 );
        }

        DNLD_DEBUG(" FW_DNLD: Memory Write Status = %X \n", status);
        if ( NFCSTATUS_PENDING == status )
        {
            psDnldContext->prev_dnld_size = dnld_size;
            cmp_val = 0x00;
            if((sec_type & DNLD_TRIM_MASK))
            {
                p_sec_info->trim_write = TRUE;
                DNLD_DEBUG(" FW_DNLD: Bytes Downloaded (Trimming Values) = %X Bytes \n",
                                                        dnld_size);
            }
            else
            {
                p_sec_info->section_write = TRUE;
                DNLD_DEBUG(" FW_DNLD: Bytes Downloaded  = %X : ",
                                        (*p_sec_offset + dnld_size));
                DNLD_DEBUG(" Bytes Remaining  = %X \n",
                        (p_sec_info->p_sec_hdr->section_length -
                                        (*p_sec_offset + dnld_size)));
            }

            p_sec_info->section_read = FALSE;
        }
    }
    return status;
}



static
NFCSTATUS
phDnldNfc_Resume_Write(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef
                     )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS;
    uint8_t                 sec_index = psDnldContext->section_index;
    section_info_t         *p_sec_info = (psDnldContext->p_fw_sec + sec_index);

    while((sec_index < psDnldContext->p_fw_hdr->no_of_sections)
        && (NFCSTATUS_SUCCESS == status )
        )
    {

        status =  phDnldNfc_Process_Write(psDnldContext, pHwRef,
                p_sec_info, (uint32_t *)&(p_sec_info->section_offset));
        if (NFCSTATUS_SUCCESS == status)
        {
            unsigned sec_type = 0;
            sec_type = (unsigned int)p_sec_info->p_sec_hdr->section_mem_type;

            p_sec_info->section_offset = 0;
            p_sec_info->section_read = FALSE;
            p_sec_info->section_write = FALSE;
            p_sec_info->trim_write = FALSE;

            DNLD_DEBUG(" FW_DNLD: Section %02X Download Complete\n", sec_index);
            if((sec_type & DNLD_RESET_MASK))
            {
                DNLD_DEBUG(" FW_DNLD: Reset After Section %02X Download \n", sec_index);
                status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                            PHDNLD_CMD_RESET , NULL, 0 );
            }
            DNLD_PRINT("*******************************************\n\n");

            sec_index++;

#ifdef NXP_FW_DNLD_CHECK_PHASE
            if( p_sec_info->p_sec_hdr->section_address
                               < (DNLD_CFG_PG_ADDR + PHDNLD_PAGE_SIZE) )
            {
                gphDnldPhase = NXP_FW_DNLD_DATA_PHASE;

            }

            p_sec_info = (psDnldContext->p_fw_sec + sec_index);

            if( (sec_index < psDnldContext->p_fw_hdr->no_of_sections)
                && ( p_sec_info->p_sec_hdr->section_address
                               < (DNLD_CFG_PG_ADDR + PHDNLD_PAGE_SIZE) )
               )
            {
                 if( NXP_FW_DNLD_CFG_PHASE >= gphDnldPhase )
                 {
                    gphDnldPhase = NXP_FW_DNLD_CFG_PHASE;
                 }
                 else
                 {
                   sec_index++;
                   p_sec_info = (psDnldContext->p_fw_sec + sec_index);
                 }
            }
#else
            p_sec_info = (psDnldContext->p_fw_sec + sec_index);
#endif /* #ifdef NXP_FW_DNLD_CHECK_PHASE */

            psDnldContext->section_index = sec_index;
        /* psDnldContext->next_dnld_state = (uint8_t) phDnld_Upgrade_State; */
        }
    }
    if (NFCSTATUS_PENDING == status)
    {
        psDnldContext->next_dnld_state = (uint8_t) phDnld_Upgrade_State;
    }
    else if (NFCSTATUS_SUCCESS == status)
    {
        /* Reset the PN544 Device */
        psDnldContext->next_dnld_state = (uint8_t) phDnld_Complete_State;
    }
    else
    {

    }
    return status;
}


#define NXP_DNLD_SM_UNLOCK_ADDR         0x008002U

#if !defined (ES_HW_VER)
#define ES_HW_VER 32
#endif

#if (ES_HW_VER <= 30)
#define NXP_DNLD_PATCH_ADDR             0x01AFFFU
#else
#define NXP_DNLD_PATCH_ADDR             0x01A1E0U
#endif

#if  (ES_HW_VER <= 30)
#define NXP_DNLD_PATCH_TABLE_ADDR       0x008107U
#else
#define NXP_DNLD_PATCH_TABLE_ADDR       0x00825AU
#endif


static
NFCSTATUS
phDnldNfc_Sequence(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        void                    *pdata,
                        uint16_t                length
                        )
{
    NFCSTATUS           status = NFCSTATUS_SUCCESS;
    uint32_t            dnld_addr = 0;
    phDnldNfc_sData_t       *p_dnld_data =
                 (phDnldNfc_sData_t *)psDnldContext->dnld_data;
    phDnldNfc_sParam_t  *p_data_param =
                          & p_dnld_data->param_info.data_param;
    uint8_t             *p_data = NULL;
    static  uint32_t    patch_size = 0;

#if (ES_HW_VER == 32)

    static  uint8_t     patch_table[] = {0xA0, 0xA1, 0xE0, 0x80, 0xA9, 0x6C };
    static  uint8_t     patch_data[] = {0xA5, 0xD0, 0xFE, 0xA5, 0xD0, 0xFD, 0xA5,
                            0xD0, 0xFC, 0xA5, 0x02, 0x80, 0xA9, 0x75};

#elif (ES_HW_VER == 31)

    static  uint8_t     patch_table[] = {0xA0, 0xAF, 0xE0, 0x80, 0x78, 0x84 };
    static  uint8_t     patch_data[] = {0xA5, 0xD0, 0xFE, 0xA5, 0xD0, 0xFD, 0xA5,
                            0xD0, 0xFC, 0xD0, 0xE0, 0xA5, 0x02, 0x80, 0x78, 0x8D};

#elif (ES_HW_VER == 30)

    static  uint8_t     patch_table[] = {0x80, 0x91, 0x51, 0xA0, 0xAF,
                                                0xFF, 0x80, 0x91, 0x5A};
    static  uint8_t     patch_data[] = {0x22};

#endif

    static  uint8_t     unlock_data[] = {0x00, 0x00};
    static  uint8_t     lock_data[] = {0x0C, 0x00};

    uint8_t             i = 0;

    PHNFC_UNUSED_VARIABLE(pdata);
    PHNFC_UNUSED_VARIABLE(length);
    switch(psDnldContext->cur_dnld_seq)
    {
        case phDnld_Reset_Seq:
        {
            status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                        PHDNLD_CMD_RESET , NULL , 0 );
            /* status = (NFCSTATUS_PENDING == status)? NFCSTATUS_SUCCESS:
                                 status; */
            DNLD_DEBUG(" FW_DNLD: Reset Seq.. Status = %X \n", status);

            break;
        }
        case phDnld_Activate_Patch:
        {
            uint8_t     patch_activate = 0x01;
            psDnldContext->next_dnld_seq =
                            (uint8_t)phDnld_Update_Patch;
#ifdef NXP_FW_DNLD_CHECK_PHASE
            gphDnldPhase = NXP_FW_DNLD_SYSTEM_PHASE;
#endif /* NXP_FW_DNLD_CHECK_PHASE */

            status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                PHDNLD_CMD_ACTIVATE_PATCH , &patch_activate, sizeof(patch_activate) );
            DNLD_PRINT(" FW_DNLD: Activate the Patch Update .... \n");
            break;
        }
        case phDnld_Deactivate_Patch:
        {
            uint8_t     patch_activate = 0x00;

            psDnldContext->next_dnld_state =
                            (uint8_t)phDnld_Reset_State;

            status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                PHDNLD_CMD_ACTIVATE_PATCH , &patch_activate, sizeof(patch_activate) );
            DNLD_PRINT(" FW_DNLD: Deactivate the Patch Update .... \n");
            break;
        }
        case phDnld_Update_Patch:
        {
            dnld_addr = NXP_DNLD_PATCH_ADDR;
            patch_size = sizeof(patch_data) ;
            p_data = patch_data;
            psDnldContext->next_dnld_seq =
                            (uint8_t)phDnld_Update_Patchtable;
            DNLD_PRINT(" FW_DNLD: Patch Update Seq.... \n");
            break;
        }
        case phDnld_Update_Patchtable:
        {
            dnld_addr = NXP_DNLD_PATCH_TABLE_ADDR;
            patch_size = sizeof(patch_table) ;
            p_data = patch_table;

            psDnldContext->next_dnld_state =
                            (uint8_t)phDnld_Reset_State;

            DNLD_PRINT(" FW_DNLD: Patch Table Update Seq.... \n");
            break;
        }
        case phDnld_Unlock_System:
        {
            dnld_addr = NXP_DNLD_SM_UNLOCK_ADDR;
            patch_size = sizeof(unlock_data) ;
            p_data = unlock_data;
#define NXP_FW_PATCH_DISABLE
#ifdef NXP_FW_PATCH_DISABLE
            psDnldContext->next_dnld_seq =
                            (uint8_t)phDnld_Deactivate_Patch;
#else
            psDnldContext->next_dnld_state =
                            (uint8_t)phDnld_Reset_State;
#endif

            DNLD_PRINT(" FW_DNLD: System Memory Unlock Seq.... \n");
            break;
        }
        case phDnld_Lock_System:
        {
            dnld_addr = NXP_DNLD_SM_UNLOCK_ADDR;
            patch_size = sizeof(lock_data) ;
            p_data = lock_data;
            psDnldContext->next_dnld_state =
                            (uint8_t) phDnld_Reset_State;

            DNLD_PRINT(" FW_DNLD: System Memory Lock Seq.... \n");
            break;
        }
        case phDnld_Upgrade_Section:
        {
            status = phDnldNfc_Resume_Write(
                        psDnldContext, pHwRef );
            break;
        }
        case phDnld_Verify_Integrity:
        {
            psDnldContext->next_dnld_state =
                            (uint8_t) phDnld_Reset_State;

            status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                PHDNLD_CMD_CHECK_INTEGRITY , NULL, 0 );
            DNLD_PRINT(" FW_DNLD: System Memory Integrity Check Sequence.... \n");
            break;
        }
        case phDnld_Verify_Section:
        {
            break;
        }
        default:
        {
            break;
        }
    }

    if( NFCSTATUS_SUCCESS == status)
    {

        /* Update the LSB of the Data and the Address Parameter*/
        p_data_param->data_addr[i] = (uint8_t)((dnld_addr  >>
                                            (BYTE_SIZE + BYTE_SIZE))
                                                    & BYTE_MASK);
        p_data_param->data_len[i] = (uint8_t)((patch_size >> BYTE_SIZE)
                                                    & BYTE_MASK);
        p_dnld_data->frame_length[i] = (uint8_t)
                    (((patch_size + PHDNLD_CMD_WRITE_MIN_LEN) >> BYTE_SIZE)
                                                    & BYTE_MASK);
        i++;
        /* Update the 2nd byte of the Data and the Address Parameter*/
        p_data_param->data_addr[i] = (uint8_t)((dnld_addr  >> BYTE_SIZE)
                                                & BYTE_MASK);
        p_data_param->data_len[i] = (uint8_t) (patch_size & BYTE_MASK);
        p_dnld_data->frame_length[i] = (uint8_t)
                            ((patch_size + PHDNLD_CMD_WRITE_MIN_LEN)
                                                    & BYTE_MASK);
        i++;
        /* Update the 3rd byte of the the Address Parameter*/
        p_data_param->data_addr[i] = (uint8_t)(dnld_addr & BYTE_MASK);

        status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                    PHDNLD_CMD_WRITE,(void *)p_data , (uint8_t)patch_size );

        if (NFCSTATUS_PENDING != status)
        {
             status = phDnldNfc_Set_Seq(psDnldContext,
                                            DNLD_SEQ_ROLLBACK);
        }
    }
    return status;
}

#define FRAME_HEADER_LEN   0x03U


static
void
phDnldNfc_Tx_Reset(phDnldNfc_sContext_t    *psDnldContext)
{
    psDnldContext->tx_info.transmit_frame = NULL;
    psDnldContext->tx_info.tx_total = 0x00;
    psDnldContext->tx_info.tx_offset = 0x00;
    psDnldContext->tx_info.tx_len = 0x00;
    psDnldContext->tx_info.tx_chain = FALSE;
}

STATIC
bool_t
phDnldNfc_Extract_Chunks(
                       uint8_t  *frame_data,
                       uint16_t  frame_offset,
                       uint16_t  frame_length,
                       uint16_t  max_frame ,
                       uint16_t  *chunk_length
                       );


STATIC
bool_t
phDnldNfc_Extract_Chunks(
                       uint8_t  *frame_data,
                       uint16_t  frame_offset,
                       uint16_t  frame_length,
                       uint16_t  max_frame ,
                       uint16_t  *chunk_length
                       )
{
    bool_t  chunk_present = FALSE;

    if( 0 == frame_offset)
    {
        if( max_frame >= (frame_length
                - frame_offset))
        {
           *chunk_length = (frame_length - frame_offset);
        }
        else
        {
            *chunk_length = max_frame
                            - FRAME_HEADER_LEN;
            chunk_present = TRUE;
        }
    }
    else
    {
        if( max_frame >= (frame_length
                - frame_offset))
        {
           *chunk_length = (frame_length - frame_offset);
        }
        else
        {
            *chunk_length = max_frame
                            - FRAME_HEADER_LEN;
            chunk_present = TRUE;
        }
    }

    return chunk_present;
}


STATIC
NFCSTATUS
phDnldNfc_Send_Raw(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        uint8_t                 *raw_frame,
                        uint16_t                frame_offset,
                        uint16_t                frame_length
                      )
{
	NFCSTATUS status = NFCSTATUS_SUCCESS;
	phDnldNfc_sRawHdr_t *raw_frame_hdr = ( phDnldNfc_sRawHdr_t * ) raw_frame;

    switch(raw_frame_hdr->frame_type)
	{
        case PHDNLD_CMD_RESET:
        {
            break;
        }
        case PHDNLD_CMD_READ:
        {
            /* TODO: To Update the length and the buffer to receive data */
            break;
        }
        case PHDNLD_CMD_WRITE:
        {
			phDnldNfc_sRawDataHdr_t *raw_data_hdr =
				( phDnldNfc_sRawDataHdr_t * ) (raw_frame + FRAME_HEADER_LEN);

            psDnldContext->resp_length = PHDNLD_MIN_PACKET;

            break;
        }
        case PHDNLD_CMD_SEC_WRITE:
        {
            uint16_t    tx_length = 0x00;
            uint16_t    frame_offset =
                          psDnldContext->tx_info.tx_offset;
            uint16_t    chain =
                    psDnldContext->tx_info.tx_chain;

            chain =
            phDnldNfc_Extract_Chunks(
                         raw_frame,
                         frame_offset,
                         frame_length,
                         PHDNLD_FW_TX_RX_LEN,
                         &tx_length
                       );

            if( TRUE == chain )
            {
                status = phDnldNfc_Send_Command( psDnldContext,
                                    pHwRef, PHDNLD_CMD_ENCAPSULATE,
                                    (raw_frame + frame_offset),
                                    tx_length);
                if(NFCSTATUS_PENDING == status)
                {
                    psDnldContext->prev_cmd = raw_frame_hdr->frame_type;
                    /* TODO: Update for the Chaining */
                    psDnldContext->tx_info.tx_offset += tx_length;
                    psDnldContext->tx_info.tx_chain = chain;
                }
            }
            else if (0 != frame_offset)
            {
                status = phDnldNfc_Send_Command( psDnldContext,
                                    pHwRef, PHDNLD_CMD_ENCAPSULATE,
                                    (raw_frame + frame_offset),
                                    tx_length);
                if(NFCSTATUS_PENDING == status)
                {
                    psDnldContext->prev_cmd = raw_frame_hdr->frame_type;
                    /* TODO: Update for the Chaining */
                    psDnldContext->prev_dnld_size = frame_length;
                    phDnldNfc_Tx_Reset(psDnldContext);
                }
            }
            else
            {
			    phDnldNfc_sRawDataHdr_t *raw_data_hdr =
				    ( phDnldNfc_sRawDataHdr_t * ) (raw_frame + FRAME_HEADER_LEN);
                psDnldContext->resp_length = PHDNLD_MIN_PACKET;
            }

            break;
        }
        case PHDNLD_CMD_CHECK:
        {
            psDnldContext->resp_length = PHDNLD_MIN_PACKET;
            break;
        }
        case PHDNLD_CMD_SET_HIF:
        {
            psDnldContext->resp_length = PHDNLD_MIN_PACKET;
            break;
        }
        case PHDNLD_CMD_ACTIVATE_PATCH:
        {
            psDnldContext->resp_length = PHDNLD_MIN_PACKET;
            break;
        }
        case PHDNLD_CMD_CHECK_INTEGRITY:
        {
			uint8_t integrity_param =
				 *(raw_frame + FRAME_HEADER_LEN);
            switch(integrity_param)
            {
                case CHK_INTEGRITY_CONFIG_PAGE_CRC:
                case CHK_INTEGRITY_PATCH_TABLE_CRC:
                {
                    psDnldContext->resp_length = PHDNLD_MIN_PACKET
                                         + CHECK_INTEGRITY_RESP_CRC16_LEN;
                    break;
                }
                case CHK_INTEGRITY_FLASH_CODE_CRC:
                case CHK_INTEGRITY_PATCH_CODE_CRC:
                {
                    psDnldContext->resp_length = PHDNLD_MIN_PACKET
                                        +  CHECK_INTEGRITY_RESP_CRC32_LEN;
                    break;
                }
                case CHK_INTEGRITY_COMPLETE_CRC:
                default:
                {
                    psDnldContext->resp_length = PHDNLD_MIN_PACKET
                                        +  CHECK_INTEGRITY_RESP_COMP_LEN;
                    break;
                }
            }
            break;
        }
        default:
        {
            status = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_FEATURE_NOT_SUPPORTED);
            break;
        }
    }

    if (NFCSTATUS_SUCCESS == status)
    {
        status = phDnldNfc_Send( psDnldContext, pHwRef ,
                            raw_frame, frame_length);

        if(NFCSTATUS_PENDING == status)
        {
            psDnldContext->prev_cmd = raw_frame_hdr->frame_type;
            /* TODO: Update for the Chaining */
            psDnldContext->prev_dnld_size = frame_length;
        }
    }

    return status;
}


static
NFCSTATUS
phDnldNfc_Frame_Complete(phDnldNfc_sContext_t *psDnldContext)
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS;
    phDnldNfc_sData_Hdr_t   *p_dnld_raw = NULL;
    uint32_t                dnld_index = psDnldContext->dnld_index;
    uint8_t                 *p_raw_sec_hdr = NULL;
    uint16_t                tx_length = 0x00;

    dnld_index = dnld_index + psDnldContext->prev_dnld_size;
    p_raw_sec_hdr = psDnldContext->p_fw_raw + dnld_index;
    dnld_index = dnld_index + *p_raw_sec_hdr;

    p_dnld_raw = (phDnldNfc_sData_Hdr_t *) (psDnldContext->p_fw_raw +
                                              psDnldContext->dnld_index);

    tx_length = ((p_dnld_raw->frame_length[0] << BYTE_SIZE) |
                            p_dnld_raw->frame_length[1]);

    tx_length = tx_length + PHDNLD_MIN_PACKET;

    return status;
}


static
NFCSTATUS
phDnldNfc_Raw_Write(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef
                     )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS;
    uint32_t                dnld_index = psDnldContext->dnld_index;
    uint32_t                tx_length = 0;
    uint8_t                 *p_raw_sec_hdr = NULL;
    uint8_t                 dnld_flag = FALSE;
    uint8_t                 skip_frame = FALSE;

    if(NULL != psDnldContext->p_fw_raw)
    {

        if( (TRUE != psDnldContext->tx_info.tx_chain)
            && (0x00 == psDnldContext->dnld_retry)
          )
        {
            dnld_index = dnld_index + psDnldContext->prev_dnld_size;
            p_raw_sec_hdr = psDnldContext->p_fw_raw + dnld_index;
            dnld_index = dnld_index + *p_raw_sec_hdr;
        }
        else
        {
            phDnldNfc_sData_Hdr_t *p_dnld_raw = (phDnldNfc_sData_Hdr_t *)
									(psDnldContext->p_fw_raw +
                                              psDnldContext->dnld_index);

            tx_length = ((p_dnld_raw->frame_length[0] << BYTE_SIZE) |
                                    p_dnld_raw->frame_length[1]);

            tx_length = tx_length + PHDNLD_MIN_PACKET;

            status = phDnldNfc_Send_Raw( psDnldContext, pHwRef,
                            (uint8_t *)(p_dnld_raw),
                            psDnldContext->tx_info.tx_offset,
                                (uint16_t)tx_length);
        }


#define PHDNLD_MAJOR_OFFSET        0x04U
#define PHDNLD_MINOR_OFFSET        0x05U
#define PHDNLD_PHASE_OFFSET        0x06U
#define PHDNLD_FRAMETYPE_OFFSET    0x07U

#define PHDNLD_NO_OPERATION        0x00U
#define PHDNLD_NORMAL_OPERATION    0x10U
#define PHDNLD_ADVANCED_OPERATION  0x20U
#define PHDNLD_SETUP_OPERATION	   0x40U
#define PHDNLD_RECOVER_OPERATION   0x80U
#define PHDNLD_COMPLETE_OPERATION  0xF0U

#define PHDNLD_TERMINATE_TYPE      0x0EU

#define PHDNLD_MARKER_MASK         0x0FU

        while((NFCSTATUS_SUCCESS == status )
                && (FALSE == dnld_flag)
            )
       {
            phDnldNfc_sData_Hdr_t *p_dnld_raw = (phDnldNfc_sData_Hdr_t *)
												(psDnldContext->p_fw_raw + dnld_index);
            uint8_t               frame_type = *(p_raw_sec_hdr + PHDNLD_FRAMETYPE_OFFSET);

            tx_length = ((p_dnld_raw->frame_length[0] << BYTE_SIZE) |
                                    p_dnld_raw->frame_length[1]);

            tx_length = tx_length + PHDNLD_MIN_PACKET;

            skip_frame = FALSE;

            if(  (0x00 == *(p_raw_sec_hdr + PHDNLD_PHASE_OFFSET))
                    || (0xFF == *(p_raw_sec_hdr + PHDNLD_PHASE_OFFSET))
                    || !( psDnldContext->raw_mode_upgrade
                     & (frame_type & (~PHDNLD_MARKER_MASK)) )
                     )
            {
                dnld_index = dnld_index + tx_length;
                p_raw_sec_hdr = psDnldContext->p_fw_raw + dnld_index;
                dnld_index = dnld_index + *p_raw_sec_hdr;
                skip_frame = TRUE;
            }
            if (PHDNLD_TERMINATE_TYPE ==
                        (frame_type & PHDNLD_MARKER_MASK))
            {
                if(TRUE != skip_frame)
	        {
                   psDnldContext->raw_mode_upgrade = 
                       (psDnldContext->raw_mode_upgrade &
                              ~(frame_type & ~PHDNLD_MARKER_MASK));
		}

                if(PHDNLD_NO_OPERATION ==
                        psDnldContext->raw_mode_upgrade)
                {
                   dnld_flag = TRUE;
                }
            }
            else
            {

            }

            if((FALSE == skip_frame)
                && (FALSE == dnld_flag)
                )
            {
                status = phDnldNfc_Send_Raw( psDnldContext, pHwRef,
                               (uint8_t *)(p_dnld_raw),
                               psDnldContext->tx_info.tx_offset,
                                    (uint16_t)tx_length);
            }

            if( NFCSTATUS_PENDING == status )
            {
                psDnldContext->dnld_index = dnld_index;
				psDnldContext->cur_frame_info= frame_type;
            }
        }
    }

    return status;
}

static
NFCSTATUS
phDnldNfc_Upgrade_Sequence(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        void                    *pdata,
                        uint16_t                length
                        )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS;

    PHNFC_UNUSED_VARIABLE(pdata);
    PHNFC_UNUSED_VARIABLE(length);

    if(phDnld_Raw_Upgrade == psDnldContext->cur_dnld_seq)
    {
       status = phDnldNfc_Raw_Write( psDnldContext, pHwRef );
    }
    else
    {
       status = phDnldNfc_Resume_Write( psDnldContext, pHwRef );
    }

    return status;
}



static
NFCSTATUS
phDnldNfc_Resume(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        void                    *pdata,
                        uint16_t                length
                     )
{
    NFCSTATUS             status = NFCSTATUS_SUCCESS;
    phDnldNfc_eState_t    dnld_next_state = (phDnldNfc_eState_t)
                                    psDnldContext->cur_dnld_state;
    phNfc_sCompletionInfo_t comp_info = {0,0,0};

    switch( dnld_next_state )
    {
        case phDnld_Reset_State:
        {
            status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                        PHDNLD_CMD_RESET , NULL, 0 );
            switch( psDnldContext->cur_dnld_seq )
            {
                case phDnld_Update_Patchtable:
                {
                    psDnldContext->next_dnld_state =
                                    (uint8_t)phDnld_Unlock_State;
                    psDnldContext->next_dnld_seq =
                                    (uint8_t)phDnld_Unlock_System;
                    break;
                }
#ifdef NXP_FW_PATCH_DISABLE
                case phDnld_Deactivate_Patch:
#else
                case phDnld_Unlock_System:
#endif
                {
                    psDnldContext->next_dnld_state =
                                   (uint8_t)phDnld_Upgrade_State;
                    psDnldContext->next_dnld_seq =
                                    (uint8_t)phDnld_Upgrade_Section;
#ifdef NXP_FW_DNLD_CHECK_PHASE
                    gphDnldPhase = NXP_FW_DNLD_CFG_PHASE;
#endif /* NXP_FW_DNLD_CHECK_PHASE */
                    break;
                }
                case phDnld_Lock_System:
                {
#if  (NXP_FW_INTEGRITY_CHK >= 0x01)
                    psDnldContext->next_dnld_state =
                                    (uint8_t)phDnld_Verify_State;
                    psDnldContext->next_dnld_seq =
                                    (uint8_t)phDnld_Verify_Integrity;
#else
                    /* (void ) memset( (void *) &psDnldContext->chk_integrity_crc,
                                0, sizeof(psDnldContext->chk_integrity_crc)); */
                    psDnldContext->next_dnld_state =
                                            (uint8_t) phDnld_Complete_State;
#endif /* #if  (NXP_FW_INTEGRITY_CHK >= 0x01) */
                    break;
                }
                case phDnld_Verify_Integrity:
                {
                    psDnldContext->next_dnld_state =
                                            (uint8_t) phDnld_Complete_State;
                    break;
                }
                default:
                {
                    status = (NFCSTATUS_PENDING == status)?
                            NFCSTATUS_SUCCESS: status;
                    break;
                }
            }
            break;
        }
        case phDnld_Unlock_State:
        {

            status = phDnldNfc_Sequence( psDnldContext, pHwRef,
                                                            pdata, length);
            break;
        }
        case phDnld_Upgrade_State:
        {
            status = phDnldNfc_Upgrade_Sequence( psDnldContext, pHwRef,
                                                            pdata, length);
            if ((NFCSTATUS_SUCCESS == status )
                && (phDnld_Complete_State == psDnldContext->next_dnld_state))
            {
#if 0
                psDnldContext->cur_dnld_seq =
                                    (uint8_t)phDnld_Lock_System;
                psDnldContext->next_dnld_seq =
                                    psDnldContext->cur_dnld_seq;
#endif
#if  (NXP_FW_INTEGRITY_CHK >= 0x01)
                psDnldContext->next_dnld_state =
                                (uint8_t)phDnld_Verify_State;
                psDnldContext->next_dnld_seq =
                                (uint8_t)phDnld_Verify_Integrity;
                psDnldContext->cur_dnld_seq =
                                    psDnldContext->next_dnld_seq;
                status = phDnldNfc_Sequence( psDnldContext,
                                                        pHwRef, pdata, length);
#else
                /* (void ) memset( (void *) &psDnldContext->chk_integrity_crc,
                            0, sizeof(psDnldContext->chk_integrity_crc)); */
                psDnldContext->next_dnld_state =
                                        (uint8_t) phDnld_Complete_State;
#endif /* #if  (NXP_FW_INTEGRITY_CHK >= 0x01) */
            }
            break;
        }
        case phDnld_Verify_State:
        {
            status = phDnldNfc_Sequence( psDnldContext,
                                                 pHwRef, pdata, length);
            break;
        }
        case phDnld_Complete_State:
        {
            uint8_t integrity_chk = 0xA5;

#if  (NXP_FW_INTEGRITY_CHK >= 0x01)
            uint8_t verify_crc = 0x96;

            if ( (NULL != psDnldContext->p_flash_code_crc) 
                  && (NULL != psDnldContext->p_patch_code_crc) 
                   && (NULL != psDnldContext->p_patch_table_crc) 
                  )
            {
                uint8_t     crc_i = 0;
                uint16_t    patch_table_crc = 0;
                uint32_t    flash_code_crc = 0;
                uint32_t    patch_code_crc = 0;

                for (crc_i = 0; crc_i < DNLD_CRC32_SIZE; crc_i++ )
                {
                    if (crc_i < DNLD_CRC16_SIZE )
                    {
                        patch_table_crc = patch_table_crc
                            | psDnldContext->chk_integrity_crc.patch_table.Chk_Crc16[crc_i]
                                    << (crc_i * BYTE_SIZE)  ;
                    }
                    flash_code_crc  = flash_code_crc
                        | psDnldContext->chk_integrity_crc.flash_code.Chk_Crc32[crc_i]
                                << (crc_i * BYTE_SIZE)  ;
                    patch_code_crc  = patch_code_crc
                        | psDnldContext->chk_integrity_crc.patch_code.Chk_Crc32[crc_i]
                                << (crc_i * BYTE_SIZE)  ;
                }
                verify_crc =(uint8_t)( (*((uint32_t *) psDnldContext->p_flash_code_crc)) != 
                          flash_code_crc );
                verify_crc |=(uint8_t)( (*((uint32_t *) psDnldContext->p_patch_code_crc)) != 
                          patch_code_crc );
                verify_crc |=(uint8_t)( (*((uint16_t *) psDnldContext->p_patch_table_crc)) != 
                          patch_table_crc );
            }
            else
            {
                DNLD_PRINT(" FW_DNLD: Flash, Patch code and Patch Table CRC ");
                DNLD_PRINT(" Not Available in the Firmware \n");
            }

#endif /* #if  (NXP_FW_INTEGRITY_CHK >= 0x01) */

            integrity_chk = psDnldContext->chk_integrity_crc.config_page.Chk_status + 
                        psDnldContext->chk_integrity_crc.patch_table.Chk_status +
                        psDnldContext->chk_integrity_crc.flash_code.Chk_status +
                        psDnldContext->chk_integrity_crc.patch_code.Chk_status;

            if ( ( 0 != integrity_chk ) 
#if  (NXP_FW_INTEGRITY_CHK >= 0x01)
                || ( 0 != verify_crc )
#endif /* #if  (NXP_FW_INTEGRITY_CHK >= 0x01) */
                )
            {
                status = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_FAILED);
            }
            break;
        }
        default:
        {
            status = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_FAILED);
            break;
        }
    }

    if (NFCSTATUS_PENDING == status)
    {
        /* Write/Receive is still pending */
    }
    else
    {
        pphNfcIF_Notification_CB_t  p_upper_notify =
                        psDnldContext->p_upper_notify;
        void                        *p_upper_context =
                        psDnldContext->p_upper_context;

        DNLD_DEBUG(" FW_DNLD: Resume Termination Status = %X \n", status);

        comp_info.status = status;

        (void) phDal4Nfc_Unregister(
                            psDnldContext->lower_interface.pcontext, pHwRef);
        phDnldNfc_Release_Lower(psDnldContext, pHwRef);
        phDnldNfc_Release_Resources(&psDnldContext);
#ifndef NFC_TIMER_CONTEXT
        gpphDnldContext = psDnldContext;
#endif
        /* Notify the Error/Success Scenario to the upper layer */
        phDnldNfc_Notify( p_upper_notify, p_upper_context, pHwRef, (uint8_t)
            ((NFCSTATUS_SUCCESS == comp_info.status )? NFC_IO_SUCCESS: NFC_IO_ERROR),
                    &comp_info );
    }
    return status;
}

STATIC
NFCSTATUS
phDnldNfc_Process_Response(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        void                    *pdata,
                        uint16_t                length
                     )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS;
    phDnldNfc_sData_Hdr_t   *resp_data =
                        (phDnldNfc_sData_Hdr_t *) pdata;

    PHNFC_UNUSED_VARIABLE(pHwRef);
    DNLD_DEBUG(" FW_DNLD: Receive Length = %X \n", length );
    if(( psDnldContext->rx_info.rx_total == 0 )
        && (PHDNLD_MIN_PACKET <= length)
        )
    {
        psDnldContext->rx_info.rx_total =
            ((uint16_t)resp_data->frame_length[0] << BYTE_SIZE)|
                        resp_data->frame_length[1];
        if( psDnldContext->rx_info.rx_total + PHDNLD_MIN_PACKET == length )
        {

            DNLD_DEBUG(" FW_DNLD: Success Memory Read = %X \n",
                                                psDnldContext->rx_info.rx_total);
#ifndef DNLD_SUMMARY
            /* DNLD_PRINT_BUFFER("Receive Buffer",pdata,length); */
#endif

        }
        else
        {
           /* status = phDnldNfc_Receive( psDnldContext, pHwRef,
                psDnldContext->p_resp_buffer,
               (uint8_t)((psDnldContext->rx_info.rx_total <= PHDNLD_MAX_PACKET)?
                    psDnldContext->rx_info.rx_total: PHDNLD_MAX_PACKET) ); */
            DNLD_PRINT(" FW_DNLD: Invalid Receive length ");
            DNLD_DEBUG(": Length Expected = %X \n",
            (psDnldContext->rx_info.rx_total + PHDNLD_MIN_PACKET));
            status = PHNFCSTVAL( CID_NFC_DNLD,
                                    NFCSTATUS_INVALID_RECEIVE_LENGTH );
        }
    }
    else
    {
        /*TODO:*/
        psDnldContext->rx_info.rx_total = 0 ;
        status = PHNFCSTVAL( CID_NFC_DNLD,
                                NFCSTATUS_INVALID_RECEIVE_LENGTH );
    }

    return status;
}



STATIC
void
phDnldNfc_Receive_Complete (
                                void                    *psContext,
                                void                    *pHwRef,
                                phNfc_sTransactionInfo_t *pInfo
                                )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS ;
    void                    *pdata = NULL ;
    phDnldNfc_sData_Hdr_t   *resp_data = NULL;
    uint16_t                length = 0 ;
    phNfc_sCompletionInfo_t comp_info = {0,0,0};

    DNLD_PRINT("\n FW_DNLD: Receive Response .... ");
    if ( (NULL != psContext)
        && (NULL != pInfo)
        && (NULL != pHwRef)
        )
    {
        phDnldNfc_sContext_t *psDnldContext =
                                (phDnldNfc_sContext_t *)psContext;
        status = pInfo->status ;
        length = pInfo->length ;
        pdata = pInfo->buffer;

        if(status != NFCSTATUS_SUCCESS)
        {
            DNLD_DEBUG(" Failed. Status = %02X\n",status);
            /* Handle the Error Scenario */
        }
        else if (NULL == pdata)
        {
            DNLD_DEBUG(" Failed. No data received. pdata = %02X\n",pdata);
            /* Handle the Error Scenario */
            status = PHNFCSTVAL( CID_NFC_DNLD,  NFCSTATUS_FAILED );
        }
        else if ((0 == length) 
            || (PHDNLD_MIN_PACKET > length ))
        {
            DNLD_DEBUG(" Receive Response Length = %u .... \n",length);
            /* Handle the Error Scenario */
#ifndef HAL_SW_DNLD_RLEN
             status = PHNFCSTVAL( CID_NFC_DNLD,
                           NFCSTATUS_INVALID_RECEIVE_LENGTH );
#endif
        }
        else
        {

#if defined(FW_DOWNLOAD_TIMER) && \
                (FW_DOWNLOAD_TIMER == 2)
        if ( NXP_INVALID_TIMER_ID != psDnldContext->timer_id )
        {
            phOsalNfc_Timer_Stop( psDnldContext->timer_id );
        }

#endif

#ifndef DNLD_SUMMARY
            DNLD_PRINT_BUFFER("Receive Buffer",pdata,length);
#endif
            DNLD_DEBUG(" Receive Response Length = %X. \n", length);

            resp_data = (phDnldNfc_sData_Hdr_t *) pdata;

            switch(resp_data->frame_type)
            {
                case PHDNLD_RESP_SUCCESS:
                {
                    uint16_t resp_length =
                        ((uint16_t)resp_data->frame_length[0] << BYTE_SIZE)|
                                    resp_data->frame_length[1];
                    switch ( psDnldContext->prev_cmd )
                    {
                        case PHDNLD_CMD_READ :
                        {
							if( PHDNLD_NO_OPERATION
							       == psDnldContext->raw_mode_upgrade)
                            {
                            status = phDnldNfc_Process_Response(
                                    psDnldContext, pHwRef, pdata , length);

                            if (NFCSTATUS_SUCCESS != status)
                            {
                                /* psDnldContext->dnld_retry++; */
                                psDnldContext->dnld_retry = NXP_MAX_DNLD_RETRY;
                                /* psDnldContext->dnld_retry < NXP_MAX_DNLD_RETRY */
                            }
                            }
                            else
                            {

                            }
                            break;
                        }
                        case PHDNLD_CMD_CHECK_INTEGRITY :
                        {
							if( PHDNLD_NO_OPERATION
							       == psDnldContext->raw_mode_upgrade)
							{
#if  (NXP_FW_INTEGRITY_CHK >= 0x01)
                            phDnldNfc_sChkCrcComplete_t *p_dnld_crc_all = 
                                &psDnldContext->chk_integrity_crc;
                            switch(psDnldContext->chk_integrity_param)
                            {
                                case CHK_INTEGRITY_CONFIG_PAGE_CRC:
                                {
                                    (void)memcpy(&p_dnld_crc_all->config_page,  
                                                (((uint8_t *)pdata) + PHDNLD_MIN_PACKET), resp_length); 
                                    break;
                                }
                                case CHK_INTEGRITY_PATCH_TABLE_CRC:
                                {
                                    (void)memcpy(&p_dnld_crc_all->patch_table,  
                                                (((uint8_t *)pdata) + PHDNLD_MIN_PACKET), resp_length); 
                                    break;
                                }
                                case CHK_INTEGRITY_FLASH_CODE_CRC:
                                {
                                    (void)memcpy(&p_dnld_crc_all->flash_code,  
                                                (((uint8_t *)pdata) + PHDNLD_MIN_PACKET), resp_length); 
                                    break;
                                }
                                case CHK_INTEGRITY_PATCH_CODE_CRC:
                                {
                                    (void)memcpy(&p_dnld_crc_all->patch_code,  
                                                (((uint8_t *)pdata) + PHDNLD_MIN_PACKET), resp_length); 
                                    break;
                                }
                                case CHK_INTEGRITY_COMPLETE_CRC:
                                {
                                    (void)memcpy(p_dnld_crc_all,
                                            (((uint8_t *)pdata) + PHDNLD_MIN_PACKET), resp_length); 
                                    DNLD_DEBUG(" FW_DNLD: Check Integrity Complete Structure Size  = %X \n", 
                                                    sizeof(psDnldContext->chk_integrity_crc));
                                    break;
                                }
                                default:
                                {
                                    status = PHNFCSTVAL(CID_NFC_DNLD,
                                            NFCSTATUS_FEATURE_NOT_SUPPORTED);
                                    break;
                                }
                            }
#endif /* #if  (NXP_FW_INTEGRITY_CHK >= 0x01) */
							}
							else
							{
                                psDnldContext->raw_mode_upgrade =
                                     (PHDNLD_SETUP_OPERATION | PHDNLD_ADVANCED_OPERATION);
                                /* psDnldContext->raw_mode_upgrade =
                                    (psDnldContext->raw_mode_upgrade &
                                     ( psDnldContext->cur_frame_info & ~PHDNLD_MARKER_MASK )); */
							}
                            break;
                        }
                        case PHDNLD_CMD_WRITE:
                        {
                            psDnldContext->dnld_retry = 0;
                            break;
                        }
                        case PHDNLD_CMD_SEC_WRITE:
                        {
                            psDnldContext->dnld_retry = 0;
                            break;
                        }
                        case PHDNLD_CMD_ACTIVATE_PATCH:
                        case PHDNLD_CMD_CHECK:
                        default:
                        {
							if( PHDNLD_NO_OPERATION
							       == psDnldContext->raw_mode_upgrade)
							{
                            if( ( (PHDNLD_MIN_PACKET > length)
                                || ( 0 != resp_length) )
                                )
                            {
                                psDnldContext->dnld_retry = NXP_MAX_DNLD_RETRY;
                                status = PHNFCSTVAL( CID_NFC_DNLD,
                                                NFCSTATUS_INVALID_RECEIVE_LENGTH );
                            }
                            else
                            {
                                psDnldContext->dnld_retry = 0;
                            }
							}
							else
							{
                                psDnldContext->raw_mode_upgrade =
                                        (psDnldContext->raw_mode_upgrade & ~PHDNLD_RECOVER_OPERATION);
							}
                            break;
                        }
                    } /* End of the Previous Command Switch Case */
                    break;
                }/* Case PHDNLD_RESP_SUCCESS*/
                case PHDNLD_RESP_TIMEOUT:
                case PHDNLD_RESP_CRC_ERROR:
                case PHDNLD_RESP_WRITE_ERROR:
                {
                    if(psDnldContext->dnld_retry < NXP_MAX_DNLD_RETRY )
                    {
                        psDnldContext->dnld_retry++;
                    }
                    status = PHNFCSTVAL(CID_NFC_DNLD,
                                                resp_data->frame_type);
                    break;
                }
                /* fall through */
                case PHDNLD_RESP_ACCESS_DENIED:
                case PHDNLD_RESP_INVALID_PARAMETER:
                case PHDNLD_RESP_INVALID_LENGTH:
                    /*  Initial Frame Checksum */
                case PHDNLD_RESP_CHKSUM_ERROR:
                case PHDNLD_RESP_MEMORY_UPDATE_ERROR:
                {
                    psDnldContext->dnld_retry = NXP_MAX_DNLD_RETRY;
                    status = PHNFCSTVAL(CID_NFC_DNLD,
                                                resp_data->frame_type);
                    break;
                }
                case PHDNLD_RESP_PROTOCOL_ERROR:
                {
					if(( PHDNLD_NO_OPERATION
							== psDnldContext->raw_mode_upgrade)
                            || ( PHDNLD_ADVANCED_OPERATION
							== psDnldContext->raw_mode_upgrade)
                            )
                    {
                        psDnldContext->dnld_retry = NXP_MAX_DNLD_RETRY;
                        status = PHNFCSTVAL(CID_NFC_DNLD,
                                            NFCSTATUS_INVALID_FORMAT);
                    }
					else if( (PHDNLD_NORMAL_OPERATION
                                 & psDnldContext->raw_mode_upgrade)
                            )
					{
                        psDnldContext->raw_mode_upgrade =
                               (psDnldContext->raw_mode_upgrade & ~PHDNLD_NORMAL_OPERATION);
					}
                    else if ( PHDNLD_RECOVER_OPERATION
                                 & psDnldContext->raw_mode_upgrade )
                    {
                        psDnldContext->dnld_retry = NXP_MAX_DNLD_RETRY;
                        status = PHNFCSTVAL(CID_NFC_DNLD,
                                            NFCSTATUS_INVALID_FORMAT);
                    }
                    else
                    {
                       psDnldContext->raw_mode_upgrade =
                        (psDnldContext->raw_mode_upgrade &
                            ~( psDnldContext->cur_frame_info & ~PHDNLD_MARKER_MASK ));
                    }
                    break;
                }
                case PHDNLD_RESP_VERSION_UPTODATE:
                {
					/* TODO: to make sure that the Advance Frames are sent to get
					 *       the updated status */
					if ( PHDNLD_ADVANCED_OPERATION
                                 == psDnldContext->raw_mode_upgrade)
					{
						status = ( CID_NFC_DNLD << BYTE_SIZE ) ;
					}
                    else if ( PHDNLD_NO_OPERATION
                                != psDnldContext->raw_mode_upgrade)
                    {

                       psDnldContext->raw_mode_upgrade =
                        (psDnldContext->raw_mode_upgrade &
                            ~( psDnldContext->cur_frame_info & ~PHDNLD_MARKER_MASK ));
                    }
                    else
                    {
                    }
                    break;
                }
                case PHDNLD_RESP_CMD_NOT_SUPPORTED:
                {

                    if ( PHDNLD_NO_OPERATION
                                 == psDnldContext->raw_mode_upgrade)
                    {
                        status = PHNFCSTVAL(CID_NFC_DNLD,
                            NFCSTATUS_FEATURE_NOT_SUPPORTED);
                    }
                    else if ( PHDNLD_ADVANCED_OPERATION
                                 == psDnldContext->raw_mode_upgrade)
					{
						status = PHNFCSTVAL(CID_NFC_DNLD,
										 NFCSTATUS_FEATURE_NOT_SUPPORTED);
					}
#if 0
					else if( (PHDNLD_NORMAL_OPERATION
                                 & psDnldContext->raw_mode_upgrade)
                            )
					{
                        psDnldContext->raw_mode_upgrade =
                               (psDnldContext->raw_mode_upgrade & ~PHDNLD_NORMAL_OPERATION);
					}
                    else if ( PHDNLD_SETUP_OPERATION
                                 & psDnldContext->raw_mode_upgrade )
                    {
                        psDnldContext->raw_mode_upgrade =
                               (psDnldContext->raw_mode_upgrade & ~PHDNLD_SETUP_OPERATION);
                    }
#endif
                    else
                    {
                       psDnldContext->raw_mode_upgrade =
                        (psDnldContext->raw_mode_upgrade &
                            ~( psDnldContext->cur_frame_info & ~PHDNLD_MARKER_MASK ));
                    }
                    break;
                }
               /*  The Chaining of the Command Frame
                                  was Successful in the Download Mode */
                case PHDNLD_RESP_CHAINING_SUCCESS:
                {
					/* TODO: Handle the Corner Case Scenarios
					 *       the updated status */
                    psDnldContext->dnld_retry = 0x00;
                    break;
                }
/*  The Error during the Chaining the Command Frame in the Download Mode */
                case PHDNLD_RESP_CHAINING_ERROR:
                {
					/* TODO: Restart the Chunk in Corner Case
					 *       the updated status */
                    psDnldContext->dnld_retry++;
                    phDnldNfc_Tx_Reset(psDnldContext);
                    break;
                }
/*  The Command is not allowed anymore in the Download Mode */
                case PHDNLD_RESP_CMD_NOT_ALLOWED:
                default:
                {
                    psDnldContext->dnld_retry = NXP_MAX_DNLD_RETRY;
                    status = PHNFCSTVAL(CID_NFC_DNLD,
                                        NFCSTATUS_NOT_ALLOWED);
                    break;
                }

            } /* End of the Response Frame Type Switch */

            if (NFCSTATUS_PENDING != status)
            {
                if ((NFCSTATUS_SUCCESS != status) &&
                    (psDnldContext->dnld_retry >= NXP_MAX_DNLD_RETRY))
                {
                    pphNfcIF_Notification_CB_t  p_upper_notify =
                        psDnldContext->p_upper_notify;
                    void                        *p_upper_context =
                                        psDnldContext->p_upper_context;

                    comp_info.status = status;
                    DNLD_DEBUG(" FW_DNLD: Termination in Receive, Status = %X \n", status);
                    status = phDal4Nfc_Unregister(
                                psDnldContext->lower_interface.pcontext, pHwRef);
                    phDnldNfc_Release_Lower(psDnldContext, pHwRef);
                    phDnldNfc_Release_Resources(&psDnldContext);
#ifndef NFC_TIMER_CONTEXT
                    gpphDnldContext = psDnldContext;
#endif
                    /* Notify the Error/Success Scenario to the upper layer */
                    phDnldNfc_Notify( p_upper_notify, p_upper_context, pHwRef,
                                    (uint8_t) NFC_IO_ERROR, &comp_info );
                }
                else if ( (NFCSTATUS_SUCCESS != status) &&
                           (NFCSTATUS_SUCCESS == PHNFCSTATUS(status))
                         )
                {
                    pphNfcIF_Notification_CB_t  p_upper_notify =
                        psDnldContext->p_upper_notify;
                    void                        *p_upper_context =
                                        psDnldContext->p_upper_context;

                    comp_info.status = NFCSTATUS_SUCCESS;
                    DNLD_DEBUG(" FW_DNLD: Termination in Receive, Status = %X \n", status);
                    status = phDal4Nfc_Unregister(
                                psDnldContext->lower_interface.pcontext, pHwRef);
                    phDnldNfc_Release_Lower(psDnldContext, pHwRef);
                    phDnldNfc_Release_Resources(&psDnldContext);
#ifndef NFC_TIMER_CONTEXT
                    gpphDnldContext = psDnldContext;
#endif
                    /* Notify the Error/Success Scenario to the upper layer */
                    phDnldNfc_Notify( p_upper_notify, p_upper_context, pHwRef,
                        (uint8_t) NFC_IO_SUCCESS, &comp_info );

                }
                else if (NFCSTATUS_FEATURE_NOT_SUPPORTED == PHNFCSTATUS(status))
                {
                    pphNfcIF_Notification_CB_t  p_upper_notify =
                        psDnldContext->p_upper_notify;
                    void                        *p_upper_context =
                                        psDnldContext->p_upper_context;

                    comp_info.status = status;
                    DNLD_DEBUG(" FW_DNLD: Termination in Receive, Status = %X \n", status);
                    status = phDal4Nfc_Unregister(
                                psDnldContext->lower_interface.pcontext, pHwRef);
                    phDnldNfc_Release_Lower(psDnldContext, pHwRef);
                    phDnldNfc_Release_Resources(&psDnldContext);
#ifndef NFC_TIMER_CONTEXT
                    gpphDnldContext = psDnldContext;
#endif
                    /* Notify the Error/Success Scenario to the upper layer */
                    phDnldNfc_Notify( p_upper_notify, p_upper_context, pHwRef,
                        (uint8_t) NFC_IO_SUCCESS, &comp_info );

                }
                else
                {
                    /* DNLD_PRINT(" FW_DNLD: Successful.\n"); */
                    psDnldContext->resp_length = /* PHDNLD_MIN_PACKET */ 0 ;
                    status = phDnldNfc_Set_Seq(psDnldContext,
                                                    DNLD_SEQ_UPDATE);
                    status = phDnldNfc_Resume( psDnldContext,
                                            pHwRef, pdata, length );
                }
            }
        } /* End of status != Success */
    }
}


STATIC
void
phDnldNfc_Send_Complete (
                                void                    *psContext,
                                void                    *pHwRef,
                                phNfc_sTransactionInfo_t *pInfo
                       )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS ;
    uint16_t                    length = 0;

    DNLD_PRINT(" FW_DNLD: Send Data .... ");
    if ( (NULL != psContext)
        && (NULL != pInfo)
        && (NULL != pHwRef)
        )
    {
        phDnldNfc_sContext_t *psDnldContext =
                                    (phDnldNfc_sContext_t *)psContext;
        status = pInfo->status ;
        length = pInfo->length ;
        if(status != NFCSTATUS_SUCCESS)
        {
            DNLD_DEBUG(" Failed. Status = %02X\n",status);
            /* Handle the Error Scenario */
        }
        else
        {
            DNLD_PRINT(" Successful.\n");
            (void)memset((void *)&psDnldContext->dnld_data, 0,
                                sizeof(psDnldContext->dnld_data));
            if ((PHDNLD_CMD_SET_HIF != psDnldContext->prev_cmd)
                && (PHDNLD_CMD_RESET != psDnldContext->prev_cmd))
            {
                psDnldContext->rx_info.rx_total = 0;
                status = phDnldNfc_Receive( psDnldContext, pHwRef,
                            (uint8_t *)(&psDnldContext->dnld_resp),
                                           psDnldContext->resp_length);
            }
            else
            {
                psDnldContext->resp_length = 0;
                psDnldContext->dnld_retry = 0;
                /* clock unstable after SW reset command, especially on UART
                 * platform because of its sensitivity to clock. Experimentally
                 * we found clock unstable for 750us. Delay for 5ms to be sure.
                 */
                if( PHDNLD_CMD_RESET == psDnldContext->prev_cmd )
                {
                    DO_DELAY(PHDNLD_DNLD_DELAY);
                }
#if defined(FW_DOWNLOAD_TIMER) && \
                (FW_DOWNLOAD_TIMER == 2)

                if ( NXP_INVALID_TIMER_ID != psDnldContext->timer_id )
                {
                    phOsalNfc_Timer_Stop( psDnldContext->timer_id );
                }
#endif

                status = phDnldNfc_Set_Seq(psDnldContext,
                                                DNLD_SEQ_UPDATE);
            }

            if(NFCSTATUS_SUCCESS == status )
            {
                status = phDnldNfc_Resume( psDnldContext, pHwRef, NULL, length);
            }

        } /* End of status != Success */

    } /* End of Context != NULL  */
}



STATIC
NFCSTATUS
phDnldNfc_Send_Command(
                        phDnldNfc_sContext_t    *psDnldContext,
                        void                    *pHwRef,
                        uint8_t                 cmd,
                        void                    *params,
                        uint16_t                param_length
                      )
{
    NFCSTATUS   status = NFCSTATUS_SUCCESS;
    uint16_t    tx_length = 0;
    uint16_t    rx_length = 0;
    uint8_t     **pp_resp_data = &psDnldContext->p_resp_buffer;
    phDnldNfc_sData_t       *p_dnld_data =
                 (phDnldNfc_sData_t *)psDnldContext->dnld_data;

    switch(cmd)
    {
        case PHDNLD_CMD_RESET:
        {
            (void)memset((void *)&psDnldContext->dnld_data, 0,
                                sizeof(psDnldContext->dnld_data));
            break;
        }
        case PHDNLD_CMD_READ:
        {
            phDnldNfc_sData_t       *p_dnld_data =
                 (phDnldNfc_sData_t *)psDnldContext->dnld_data;
            phDnldNfc_sParam_t  *param_info = /* (phDnldNfc_sParam_t *)params */
                               &p_dnld_data->param_info.data_param;
            tx_length = PHDNLD_CMD_READ_LEN;
            if (NULL != *pp_resp_data)
            {
                phOsalNfc_FreeMemory(*pp_resp_data);
                *pp_resp_data = NULL;
            }
            rx_length = (uint16_t) (((uint16_t)param_info->data_len[0]
                                   << BYTE_SIZE) + param_info->data_len[1]);

            psDnldContext->resp_length =
                (( rx_length + PHDNLD_MIN_PACKET ));
            (void)phDnldNfc_Allocate_Resource( (void **) pp_resp_data,
                     rx_length);
            break;
        }
        case PHDNLD_CMD_WRITE:
        case PHDNLD_CMD_SEC_WRITE:
        {
            phDnldNfc_sData_t       *p_dnld_data =
                 (phDnldNfc_sData_t *)psDnldContext->dnld_data;
            phDnldNfc_sParam_t  *param_info = /* (phDnldNfc_sParam_t *)params */
                                &p_dnld_data->param_info.data_param;
            tx_length = (uint16_t) (((uint16_t)param_info->data_len[0]
                        << BYTE_SIZE) + param_info->data_len[1]
                                    + PHDNLD_CMD_WRITE_MIN_LEN );

            psDnldContext->resp_length = PHDNLD_MIN_PACKET;
            if ((0 != param_length) && (NULL != params))
            {
                (void)memcpy(param_info->data_packet,  params, param_length);
            }
            break;
        }
        case PHDNLD_CMD_CHECK:
        {
            tx_length = PHDNLD_CMD_CHECK_LEN;
            psDnldContext->resp_length = PHDNLD_MIN_PACKET;
            break;
        }
        case PHDNLD_CMD_ENCAPSULATE:
        {
            uint8_t  i = 0x00;
            if ((0 != param_length) && (NULL != params))
            {
                p_dnld_data->frame_type =
                            PHDNLD_CMD_ENCAPSULATE;
                (void)memcpy((void *)( ((uint8_t *)p_dnld_data)
                                           + PHDNLD_FRAME_DATA_OFFSET)
                                        , params, param_length);
                tx_length = param_length;

                p_dnld_data->frame_length[i++] =
                           (uint8_t)(tx_length >> BYTE_SIZE);
                p_dnld_data->frame_length[i]   =
                           (uint8_t)( tx_length & BYTE_MASK );
                tx_length += PHDNLD_FRAME_DATA_OFFSET;

                psDnldContext->resp_length = PHDNLD_MIN_PACKET;

                status = phDnldNfc_Send( psDnldContext, pHwRef ,
                                    (uint8_t *)p_dnld_data, tx_length);
            }
            else
            {
               status = PHNFCSTVAL(CID_NFC_DNLD,
                              NFCSTATUS_NOT_ALLOWED);
            }
            break;
        }
        case PHDNLD_CMD_SET_HIF:
        {
            tx_length++;
            psDnldContext->resp_length = PHDNLD_MIN_PACKET;
            break;
        }
        case PHDNLD_CMD_ACTIVATE_PATCH:
        {
            psDnldContext->resp_length = PHDNLD_MIN_PACKET;
            if ((NULL != params) && ( param_length > 0 ))
            {
                p_dnld_data->param_info.cmd_param =
                                            (*(uint8_t *)params);
                tx_length = param_length;
            }
            else
            {
                p_dnld_data->param_info.cmd_param = FALSE;
                tx_length++;
            }
            break;
        }
        case PHDNLD_CMD_CHECK_INTEGRITY:
        {
#if  (NXP_FW_INTEGRITY_CHK >= 0x01)
            if ((NULL != params) && ( param_length > 0 ))
            {
                psDnldContext->chk_integrity_param = 
                            (phDnldNfc_eChkCrc_t)(*(uint8_t *)params);
                tx_length = param_length;
            }
            else
            {
                psDnldContext->chk_integrity_param = CHK_INTEGRITY_COMPLETE_CRC;
                tx_length++;
            }
            p_dnld_data->param_info.cmd_param =
                                (uint8_t) psDnldContext->chk_integrity_param;
            switch(psDnldContext->chk_integrity_param)
            {
                case CHK_INTEGRITY_CONFIG_PAGE_CRC:
                case CHK_INTEGRITY_PATCH_TABLE_CRC:
                {
                    psDnldContext->resp_length = PHDNLD_MIN_PACKET 
                                         + CHECK_INTEGRITY_RESP_CRC16_LEN;
                    break;
                }
                case CHK_INTEGRITY_FLASH_CODE_CRC:
                case CHK_INTEGRITY_PATCH_CODE_CRC:
                {
                    psDnldContext->resp_length = PHDNLD_MIN_PACKET 
                                        +  CHECK_INTEGRITY_RESP_CRC32_LEN;
                    break;
                }
                case CHK_INTEGRITY_COMPLETE_CRC:
                default:
                {
                    psDnldContext->resp_length = PHDNLD_MIN_PACKET 
                                        +  CHECK_INTEGRITY_RESP_COMP_LEN;
                    break;
                }
            }
#else
            tx_length++;
            p_dnld_data->param_info.cmd_param =
                                (uint8_t) CHK_INTEGRITY_COMPLETE_CRC;

#endif /* #if  (NXP_FW_INTEGRITY_CHK >= 0x01) */
            break;
        }
        default:
        {
            status = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_FEATURE_NOT_SUPPORTED);
            break;
        }
    }
    if (NFCSTATUS_SUCCESS == status)
    {
        uint8_t     i = 0;

        p_dnld_data->frame_type = cmd;
        p_dnld_data->frame_length[i++] =
                                    (uint8_t)(tx_length >> BYTE_SIZE);
        p_dnld_data->frame_length[i]   =
                                    (uint8_t)( tx_length & BYTE_MASK );
        tx_length = tx_length + PHDNLD_MIN_PACKET;
        status = phDnldNfc_Send( psDnldContext, pHwRef ,
                            (uint8_t *)p_dnld_data, tx_length);
        if(NFCSTATUS_PENDING == status)
        {
            psDnldContext->prev_cmd = cmd;

            if( PHDNLD_CMD_RESET == cmd )
                DO_DELAY(PHDNLD_DNLD_DELAY);  //this seems like its on the wrong thread
        }
    }

    return status;
}

static
NFCSTATUS
phDnldNfc_Check_FW(
                    phHal_sHwReference_t    *pHwRef,
                    fw_data_hdr_t           *cur_fw_hdr
                     )
{
    NFCSTATUS               status = NFCSTATUS_FAILED;

        if ( !pHwRef->device_info.fw_version )
        {
            /* Override the Firmware Version Check and upgrade*/;
            DNLD_PRINT(" FW_DNLD_CHK: Forceful Upgrade of the Firmware .... Required \n");
            status = NFCSTATUS_SUCCESS;
        }
        else    if ( (pHwRef->device_info.fw_version >> (BYTE_SIZE * 2)) 
                != ( cur_fw_hdr->fw_version >> (BYTE_SIZE * 2) ))
        {
            /* Check for the Compatible Romlib Version for the Hardware */
            DNLD_PRINT(" FW_DNLD: IC Hardware Version Mismatch.. \n");
            status = PHNFCSTVAL( CID_NFC_DNLD, NFCSTATUS_NOT_ALLOWED );
        }
        else if (( pHwRef->device_info.fw_version < cur_fw_hdr->fw_version  )
            )
        {
            /* TODO: Firmware Version Check and upgrade*/
            DNLD_PRINT(" FW_DNLD: Older Firmware Upgrading to newerone.... \n");
            status = NFCSTATUS_SUCCESS;
        }
#ifdef NXP_FW_CHK_LATEST
        else if (( pHwRef->device_info.fw_version > cur_fw_hdr->fw_version  )
            )
        {
            DNLD_PRINT(" FW_DNLD: Newer than the Stored One .... \n");
            status = PHNFCSTVAL( CID_NFC_DNLD, NFCSTATUS_NOT_ALLOWED );
        }
#endif /* NXP_FW_CHK_LATEST */
        else
        {
            DNLD_PRINT(" FW_DNLD: Already Updated .... \n");
            status = ( CID_NFC_DNLD << BYTE_SIZE ) ;
        }

    return status;
    }


static
NFCSTATUS
phDnldNfc_Process_FW(
                        phDnldNfc_sContext_t    *psDnldContext,
                        phHal_sHwReference_t    *pHwRef
#ifdef NXP_FW_PARAM
                        ,uint8_t                 *nxp_nfc_fw
                        ,uint32_t                 nxp_fw_len
#endif
                     )
{
    NFCSTATUS               status = NFCSTATUS_FAILED;
    section_info_t          *p_cur_sec = NULL;
    static unsigned         sec_type;
    uint32_t                fw_index = 0;
#ifdef NXP_NFC_MULTIPLE_FW
    phDnldNfc_sFwImageInfo_t  *p_cur_fw = NULL;
#endif /* #ifdef NXP_NFC_MULTIPLE_FW */
    fw_data_hdr_t           *cur_fw_hdr = NULL;
    uint8_t                 sec_index = 0;
    uint8_t                 i = 0;

    psDnldContext->p_img_hdr = (img_data_hdr_t *) nxp_nfc_fw;

#ifdef NXP_NFC_MULTIPLE_FW

    /* TODO: Create a memory of pointers to store all the Firmwares */
    if( (NXP_NFC_IMAG_FW_MAX > psDnldContext->p_img_hdr->no_of_fw_img)
        && (0 != psDnldContext->p_img_hdr->no_of_fw_img)
        )
    {
        ( void )phDnldNfc_Allocate_Resource((void **)&psDnldContext->p_img_info,
            (psDnldContext->p_img_hdr->no_of_fw_img * sizeof(phDnldNfc_sFwImageInfo_t)));

        if(NULL != psDnldContext->p_img_info)
        {
            p_cur_fw = psDnldContext->p_img_info;
        }
    }
#endif /* #ifdef NXP_NFC_MULTIPLE_FW */

    fw_index = sizeof (img_data_hdr_t);

    for ( i=0; i < psDnldContext->p_img_hdr->no_of_fw_img; i++ )
    {

        psDnldContext->p_fw_hdr = (fw_data_hdr_t *) ( nxp_nfc_fw + fw_index );

#ifdef NXP_NFC_MULTIPLE_FW
        if(NULL != p_cur_fw)
        {
            ( p_cur_fw + i)->p_fw_hdr = psDnldContext->p_fw_hdr;
        }
#endif /* #ifdef NXP_NFC_MULTIPLE_FW */
        cur_fw_hdr = psDnldContext->p_fw_hdr;

        fw_index = fw_index + (cur_fw_hdr->fw_hdr_len * PNDNLD_WORD_LEN);

        status = phDnldNfc_Check_FW( pHwRef, cur_fw_hdr);

    }

    if ( ( NFCSTATUS_SUCCESS == status )
#if  defined (NXP_FW_INTEGRITY_VERIFY)
        || (NFCSTATUS_SUCCESS == PHNFCSTATUS(status) )
#endif /* !defined (NXP_FW_INTEGRITY_VERIFY) */
        )
    {
        if( (BYTE_MASK > cur_fw_hdr->no_of_sections)
           && (0 != cur_fw_hdr->no_of_sections)
          )
        {
        (void) phDnldNfc_Allocate_Resource((void **)&psDnldContext->p_fw_sec,
            (cur_fw_hdr->no_of_sections * sizeof(section_info_t)));

            if(NULL != psDnldContext->p_fw_sec)
        {
            DNLD_DEBUG(" FW_DNLD: FW Index : %x \n",
                                            fw_index );

            DNLD_DEBUG(" FW_DNLD: No of Sections : %x \n\n",
                                            cur_fw_hdr->no_of_sections);

            for(sec_index = 0; sec_index
                            < cur_fw_hdr->no_of_sections; sec_index++ )
            {
                p_cur_sec = ((section_info_t *)
                                (psDnldContext->p_fw_sec + sec_index ));

                p_cur_sec->p_sec_hdr = (section_hdr_t *)
                                        (nxp_nfc_fw + fw_index);

                DNLD_DEBUG(" FW_DNLD: Section %x \n",   sec_index);
                DNLD_DEBUG(" FW_DNLD: Section Header Len : %x   ",
                                        p_cur_sec->p_sec_hdr->section_hdr_len);
                DNLD_DEBUG(" Section Address : %x   ",
                                        p_cur_sec->p_sec_hdr->section_address);
                DNLD_DEBUG(" Section Length : %x   ",
                                        p_cur_sec->p_sec_hdr->section_length);
                DNLD_DEBUG(" Section Memory Type : %x   \n",
                                        p_cur_sec->p_sec_hdr->section_mem_type);

                sec_type = (unsigned int)p_cur_sec->p_sec_hdr->section_mem_type;

                if((sec_type & DNLD_TRIM_MASK))
                {
                    p_cur_sec->p_trim_data = (uint8_t *)
                               (nxp_nfc_fw + fw_index + sizeof(section_hdr_t));
                }
                else
                {
                    p_cur_sec->p_trim_data = NULL;
                }

                    if (0 == sec_index)
                    {
                        if ((sec_type & DNLD_SM_UNLOCK_MASK))
                        {
                            (void)phDnldNfc_Set_Seq(psDnldContext,
                                                            DNLD_SEQ_UNLOCK);
                        }
                        else
                        {
                            (void)phDnldNfc_Set_Seq(psDnldContext,
                                                            DNLD_SEQ_INIT);
                        }
                    }
                p_cur_sec->section_read = FALSE;

                p_cur_sec->section_offset = 0;

                p_cur_sec->p_sec_data = ((uint8_t *) nxp_nfc_fw) + fw_index +
                    (p_cur_sec->p_sec_hdr->section_hdr_len * PNDNLD_WORD_LEN);

                fw_index = fw_index +
                    (p_cur_sec->p_sec_hdr->section_hdr_len * PNDNLD_WORD_LEN)
                   + p_cur_sec->p_sec_hdr->section_length;


                    if( 0 != p_cur_sec->p_sec_hdr->section_checksum )
                    {
                            DNLD_DEBUG(" FW_DNLD: Section checksum : %x \n",
                                            p_cur_sec->p_sec_hdr->section_checksum );

                            p_cur_sec->p_sec_chksum = ( uint8_t *)(nxp_nfc_fw + fw_index);

                            fw_index = fw_index +
                                p_cur_sec->p_sec_hdr->section_checksum;
                    }

               DNLD_DEBUG(" FW_DNLD: FW Index : %x \n", fw_index );

#if  (NXP_FW_INTEGRITY_CHK >= 0x01)
              switch( p_cur_sec->p_sec_hdr->section_address )
               {
                   case DNLD_FW_CODE_ADDR:
                   {
                       psDnldContext->p_flash_code_crc = 
                           p_cur_sec->p_sec_data 
                             + p_cur_sec->p_sec_hdr->section_length
                                - DNLD_CRC32_SIZE;
                       break;
                   }
                   case DNLD_PATCH_CODE_ADDR:
                   {
                       psDnldContext->p_patch_code_crc = 
                           p_cur_sec->p_sec_data 
                             + p_cur_sec->p_sec_hdr->section_length
                                - DNLD_CRC32_SIZE;
                       break;
                   }
                   case DNLD_PATCH_TABLE_ADDR:
                   {
                       psDnldContext->p_patch_table_crc = 
                           p_cur_sec->p_sec_data 
                             + p_cur_sec->p_sec_hdr->section_length
                                - DNLD_CRC16_SIZE;
                       break;
                   }
                   default:
                   {
                       break;
                   }

                    } /* End of Address Switch */
#endif /* #if  (NXP_FW_INTEGRITY_CHK >= 0x01) */
                } /* End of For Loop */
            } /* End of the Null Check */
            else
            {
                status = PHNFCSTVAL(CID_NFC_DNLD,
                        NFCSTATUS_INSUFFICIENT_RESOURCES);
               }

            }
        else if (
                   (0 == cur_fw_hdr->no_of_sections)
                   && (PHDNLD_FW_PATCH_SEC == cur_fw_hdr->fw_patch)
                )
        {
            psDnldContext->p_fw_raw = (uint8_t *)(nxp_nfc_fw + fw_index);

			psDnldContext->raw_mode_upgrade = PHDNLD_COMPLETE_OPERATION;

            (void)phDnldNfc_Set_Seq(psDnldContext,
                                            DNLD_SEQ_RAW);
        }
        else
        {
          DNLD_PRINT("*********  Empty Section and Firmware ******************\n\n");
        }

            DNLD_PRINT("*******************************************\n\n");

    }
    return status;
}

#if  !defined (NXP_FW_INTEGRITY_VERIFY)

NFCSTATUS
phDnldNfc_Run_Check(
                        phHal_sHwReference_t    *pHwRef
#ifdef NXP_FW_PARAM
                        ,uint8_t                 *nxp_nfc_fw
                         uint32_t                  fw_length
#endif
                   )
{
    NFCSTATUS               status = NFCSTATUS_FAILED;
    uint32_t                fw_index = 0;
    img_data_hdr_t          *p_img_hdr = NULL;
    fw_data_hdr_t           *p_fw_hdr = NULL;
    fw_data_hdr_t           *cur_fw_hdr = NULL;
    uint8_t                  i = 0;

    p_img_hdr = (img_data_hdr_t *) nxp_nfc_fw;

    fw_index = sizeof (img_data_hdr_t);

    for ( i=0; i < p_img_hdr->no_of_fw_img; i++ )
    {
        p_fw_hdr = (fw_data_hdr_t *) ( nxp_nfc_fw + fw_index );
        /* TODO: Create a memory of pointers to store all the Firmwares */
        cur_fw_hdr = p_fw_hdr;

        fw_index = fw_index + (cur_fw_hdr->fw_hdr_len * PNDNLD_WORD_LEN);

        status = phDnldNfc_Check_FW( pHwRef, cur_fw_hdr);
    }
    return status;
}

#endif /* #if  !defined (NXP_FW_INTEGRITY_VERIFY) */


STATIC
void
phDnldNfc_Abort (
                    uint32_t    abort_id
#ifdef NFC_TIMER_CONTEXT
                    , void     *dnld_cntxt
#endif
                )
{

    phNfc_sCompletionInfo_t  comp_info = {0,0,0};

    phDnldNfc_sContext_t *p_dnld_context = NULL;

#ifdef NFC_TIMER_CONTEXT
    p_dnld_context = (phDnldNfc_sContext_t *)dnld_cntxt;
#else
    p_dnld_context = gpphDnldContext;
#endif

    if ( ( NULL != p_dnld_context)
            && (abort_id == p_dnld_context->timer_id ))
    {
        pphNfcIF_Notification_CB_t  p_upper_notify =
            p_dnld_context->p_upper_notify;
        void                        *p_upper_context =
                                p_dnld_context->p_upper_context;
        phHal_sHwReference_t        *pHwRef = p_dnld_context->p_hw_ref;

        (void)phDal4Nfc_Unregister(
                     p_dnld_context->lower_interface.pcontext, pHwRef );
        phDnldNfc_Release_Lower(p_dnld_context, pHwRef);
        phDnldNfc_Release_Resources(&p_dnld_context);
#ifndef NFC_TIMER_CONTEXT
        gpphDnldContext = p_dnld_context;
#endif

        /* Notify the Error/Success Scenario to the upper layer */
        DNLD_DEBUG(" FW_DNLD: FW_DNLD Aborted with %x Timer Timeout \n",
                                                                abort_id);
        comp_info.status = NFCSTATUS_FAILED ;
        phDnldNfc_Notify( p_upper_notify, p_upper_context,
                        pHwRef, (uint8_t) NFC_IO_ERROR, &comp_info );
    }

    return ;
}



NFCSTATUS
phDnldNfc_Upgrade (
                        phHal_sHwReference_t            *pHwRef,
#ifdef NXP_FW_PARAM
                        uint8_t                          type,
                        uint8_t                         *nxp_nfc_fw,
                        uint32_t                         fw_length,
#endif
                        pphNfcIF_Notification_CB_t      upgrade_complete,
                        void                            *context
                 )
 {
    phDnldNfc_sContext_t    *psDnldContext = NULL;
    phNfcIF_sReference_t    dnldReference = { NULL,0,0 };
    phNfcIF_sCallBack_t     if_callback = { NULL, NULL, NULL, NULL };
    phNfc_sLowerIF_t        *plower_if = NULL;
    NFCSTATUS                status = NFCSTATUS_SUCCESS;
    section_info_t          *p_cur_sec = NULL;
    unsigned                sec_type = 0;

    if( (NULL == pHwRef)
      )
    {
        status = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        DNLD_PRINT(" FW_DNLD: Starting the FW Upgrade Sequence .... \n");

        (void)
             phDnldNfc_Allocate_Resource((void **)
                              &psDnldContext,sizeof(phDnldNfc_sContext_t));
        if(psDnldContext != NULL)
        {
#ifndef NFC_TIMER_CONTEXT
            gpphDnldContext = psDnldContext;
#endif
            psDnldContext->p_hw_ref = pHwRef;
            psDnldContext->timer_id = NXP_INVALID_TIMER_ID;

            DNLD_PRINT(" FW_DNLD: Initialisation in Progress.... \n");

            if_callback.pif_ctxt = psDnldContext ;
            if_callback.send_complete = &phDnldNfc_Send_Complete;
            if_callback.receive_complete= &phDnldNfc_Receive_Complete;
            /* if_callback.notify = &phDnldNfc_Notify_Event; */
            plower_if = dnldReference.plower_if = &(psDnldContext->lower_interface);
            status = phDal4Nfc_Register(&dnldReference, if_callback,
                                    NULL);
            DNLD_DEBUG(" FW_DNLD: Lower Layer Register, Status = %02X\n",status);

            if(  (NFCSTATUS_SUCCESS == status) && (NULL != plower_if->init))
            {
                /* psDnldContext->p_config_params = pHwConfig ; */
                status = plower_if->init((void *)plower_if->pcontext,
                                        (void *)pHwRef);
                DNLD_DEBUG(" FW_DNLD: Lower Layer Initialisation, Status = %02X\n",status);
            }
            else
            {
                /* TODO: Handle Initialisation in the Invalid State */
            }
            /* The Lower layer Initialisation successful */
            if (NFCSTATUS_SUCCESS == status)
            {
                psDnldContext->p_upper_notify = upgrade_complete;
                psDnldContext->p_upper_context = context;

                status = phDnldNfc_Process_FW( psDnldContext, pHwRef
#ifdef NXP_FW_PARAM
                ,*nxp_nfc_fw , fw_length
#endif
                 );

                if (NFCSTATUS_SUCCESS == status)
                {
                    status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                            PHDNLD_CMD_RESET , NULL , 0 );
                    if (NFCSTATUS_PENDING == status)
                    {
                        DNLD_PRINT("\n FW_DNLD: Initial Reset .... \n");

#if defined(FW_DOWNLOAD_TIMER) 

                        psDnldContext->timer_id = phOsalNfc_Timer_Create( );

#if (FW_DOWNLOAD_TIMER < 2)
                        phOsalNfc_Timer_Start( psDnldContext->timer_id,
                                NXP_DNLD_COMPLETE_TIMEOUT,
                                (ppCallBck_t) phDnldNfc_Abort
#ifdef NFC_TIMER_CONTEXT
                                , (void *) psDnldContext
#endif
                                );

#endif  /* #if (FW_DOWNLOAD_TIMER < 2) */

#endif /* #if defined(FW_DOWNLOAD_TIMER)  */

                    }
                }
                else if (NFCSTATUS_SUCCESS == PHNFCSTATUS(status))
                {
#if  defined (NXP_FW_INTEGRITY_VERIFY)
                    /* 
                     * To check for the integrity if the firmware is already 
                     * Upgraded.
                     */
                    status = phDnldNfc_Send_Command( psDnldContext, pHwRef,
                            PHDNLD_CMD_RESET , NULL , 0 );
                    if (NFCSTATUS_PENDING == status)
                    {
                        DNLD_PRINT("\n FW_DNLD: Integrity Reset .... \n");
                        (void)phDnldNfc_Set_Seq(psDnldContext, DNLD_SEQ_COMPLETE);
                        status = PHNFCSTVAL( CID_NFC_DNLD, 
                                        NFCSTATUS_PENDING );
#if defined(FW_DOWNLOAD_TIMER) 
                        psDnldContext->timer_id = phOsalNfc_Timer_Create( );
#if (FW_DOWNLOAD_TIMER < 2)
                        phOsalNfc_Timer_Start( psDnldContext->timer_id,
                                NXP_DNLD_COMPLETE_TIMEOUT, 
                                (ppCallBck_t) phDnldNfc_Abort
#ifdef NFC_TIMER_CONTEXT
                                , (void *) psDnldContext
#endif
                                );

#endif  /* #if (FW_DOWNLOAD_TIMER < 2) */

#endif /* #if defined(FW_DOWNLOAD_TIMER)  */
                    }

#else
                    status = NFCSTATUS_SUCCESS;

#endif /* #if  defined (NXP_FW_INTEGRITY_VERIFY) */

                }
                else
                {
                    DNLD_PRINT(" FW_DNLD Initialisation in Failed \n");
                }
            }

            if (NFCSTATUS_PENDING != PHNFCSTATUS(status))
            {
                (void)phDal4Nfc_Unregister(
                            psDnldContext->lower_interface.pcontext, pHwRef);
                phDnldNfc_Release_Lower(psDnldContext, pHwRef);
                phDnldNfc_Release_Resources(&psDnldContext);
#ifndef NFC_TIMER_CONTEXT
                gpphDnldContext = psDnldContext;
#endif
            }
        } /* End of Status Check for Memory */
        else
        {
            status = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INSUFFICIENT_RESOURCES);

            DNLD_PRINT(" FW_DNLD: Memory Allocation of Context Failed\n");
        }
    }

    return status;
 }