summaryrefslogtreecommitdiff
path: root/EDK/MiniSetup/BootOnly/boot.c
blob: 5b5036ce2bd4d4c10a57f4e80bc80c82a7ab938c (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
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
//**                                                             **//
//**         (C)Copyright 2013, American Megatrends, Inc.        **//
//**                                                             **//
//**                     All Rights Reserved.                    **//
//**                                                             **//
//**   5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093  **//
//**                                                             **//
//**                     Phone (770)-246-8600                    **//
//**                                                             **//
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
// $Archive: /Alaska/SOURCE/Modules/AMITSE2_0/AMITSE/BootOnly/boot.c $
//
// $Author: Arunsb $
//
// $Revision: 70 $
//
// $Date: 5/02/14 9:26p $
//
//*****************************************************************//
//*****************************************************************//
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/AMITSE2_0/AMITSE/BootOnly/boot.c $
// 
// 70    5/02/14 9:26p Arunsb
// semicolon added to PERF_END
// 
// 69    5/02/14 5:13p Premkumara
// [TAG]  	                EIP162197
// [Category]  	Improvement
// [Description]  	Password encode feature modify to encode using Hashing
// based on token.
// [[Files]  		Password.c, PasswordEncodeBin.cif, AMITSE.cif,
// TSESource.cif, PasswordEncode.c, PasswordEncode.h, AMITSE.sdl,
// PasswordEncodeBin.mak, PasswordEncodeBin.sdl
// AMIVfr.h, Boot.c
// 
// 68    5/02/14 2:24a Arunsb
// [TAG]  		EIP158989
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	BGRT status bit not cleared with Shell v2.0
// [RootCause]  	Shell v2.0 protocol not registered
// [Solution]  	Shell v2.0 protocol registered and cleared the BGRT status
// bit
// [Files]  		AMIVfr.h and boot.c
// 
// 67    2/11/14 7:52p Arunsb
// [TAG]	EIP125719
// [Category]	Improvement
// [Description]	The EfiCreateEventReadyToBoot is conflicted under
// UefiLib.h and EDKhelper.h.
// [Files]	boot.c
// 
// 66    12/04/13 5:20a Premkumara
// [TAG]	EIP123535
// [Category]	Improvement
// [Description]	Moved saving quietboot resolution to DrawQuietBootLogo()
// to avoid setting resolution of initPostScreenthis while booting.
// [Files]	commonoem.c, Boot.c, Logo.c, MinisetupExt.c
// 
// 65    11/06/13 12:16p Arunsb
// [TAG]	EIP136592, 141863
// [Category]	Improvement
// [Description]	Resolution restored before signalling
// AMITSE_EVENT_BEFORE_BOOT_GUID
// [Files]	boot.c
// 
// 64    9/13/13 1:55p Premkumara
// Uploaded back EIP-126807 after TSEBootOnly1240 release.
// 
// 63    9/13/13 8:33a Premkumara
// Reverted EIP-126807 for TSEBootOnly1240 release.
// 
// 61    7/02/13 10:06a Premkumara
// [TAG]  		EIP120011
// [Category]  	Improvement
// [Description]  	Variable BootOrder and LegacyDevOrder need
// fool-proofing function when TSE load variabled
// [Files]  		Bbs.c, Boot.c, Callback.c, TseLiteHelper.c, Variable.c
// 
// 60    6/10/13 1:17p Arunsb
// BS->CalculateCrc32 used to calculate CRC
// 
// 59    5/21/13 5:52a Rajashakerg
// [TAG]  		EIP121881 
// [Category]  	Improvement
// [Description]  	Change resolution before boot in FastBoot path
// [Files]  		boot.c
// 
// 58    4/18/13 2:18a Arunsb
// [TAG]  		EIP113266
// [Category]  	Improvement
// [Description]  	gBrowserCallbackEnabled variable moved to boot only
// module
// [Files]  		boot.c and FormBrowser2.c
// 
// 57    3/18/13 12:23a Arunsb
// [TAG]  		EIP117338
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	TSE BootGetBBSOptionStatus asserts
// [RootCause]  	Pointer incremented from allocated initial memory and
// freed so asserts
// [Solution]  	Freed the initial allocated memory
// [Files]  		boot.c
// 
// 56    3/14/13 10:50a Premkumara
// [TAG]  		EIP112796
// [Category]  	New Feature
// [Description]  	AMITSE not signalling AMITSE_EVENT_BEFORE_BOOT_GUID in
// fast boot path and legacy boot path.
// [Files]  		Boot.c
// 
// 55    2/19/13 4:18a Premkumara
// [TAG]  		EIP103540 
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	 Currently _BootBuildFVDevicePath function using FV
// protocol alone not FV2 protocol. So applications loading from ffs are
// not loading properly.
// [RootCause]  	_BootBuildFVDevicePath() function used
// gEfiFirmwareVolumeProtocolGuid only not gEfiFirmwareVolume2ProtocolGuid
// [Solution]  	Used gEfiFirmwareVolume2ProtocolGuid based on
// PISpecVersion
// [Files]  		CommonHelper.c, Boot.c
// 
// 54    12/05/12 5:30a Arunsb
// [TAG]  		EIP105864
// [Category]  	Improvement
// [Description]  	Screen not cleared properly for successive boots
// [Files]  		boot.c
// 
// 53    10/18/12 11:20a Arunsb
// [TAG]  		EIP102710
// [Category]  	Improvement
// [Description]  	Include function "_RegisterShellGuid()" in fast boot
// path
// [Files]  		boot.c
// 
// 52    10/18/12 5:58a Arunsb
// Updated for 2.16.1235 QA submission
// 
// 19    10/10/12 12:36p Arunsb
// Synched the source for v2.16.1232, backup with Aptio
// 
// 50    9/24/12 9:13a Premkumara
// [TAG]  		EIP 93797
// [Category]  	Improvement
// [Description]  	 	Add support to check for enabled device in BBS
// priority order before launching legacy boot.
// [Files]  		bbs.c, Boot.c, Protocol.c
// 
// 49    9/21/12 9:38a Premkumara
//  [TAG]  		EIP 97704
//  [Category]  	Improvement
//  [Description]  	Support BootFFFF variable
//  [Files]  		Boot.h, Boot.c, Special.c
// 
// 48    9/18/12 12:50a Rajashakerg
// [TAG]  		EIP95518
// [Category]  	Improvement
// [Description]  	Validate the Gop before usage in all the possible cases
// and also get instance of Gop through notification
// [Files]  		boot.c, notify.c, logo.c
// 
// 47    9/17/12 6:10a Rajashakerg
// Updated EIP changes for 2.16 release.
// 
// 45    9/12/12 5:23a Rajashakerg
// [TAG]  		EIP94205
// [Category]  	Improvement
// [Description]  	OEM needs Token to assign the display mode of SHELL
// [Files]  		AMITSE.sdl, boot.c, CommonHelper.c
// 
// 44    8/29/12 4:19p Arunsb
// [TAG]  			EIP94702
// [Description]  	Support for Native Resolution in POST/BOOT
// [Files]  		amitse.sdl, commonhelper.c, commonoem.c, boot.c, logo.c,
// notify.c, postmgmt.c, tselite\minisetupext.c, ezport/stylecommon.c,
// ezportplus/stylecommon.c andlegacy/stylecommon.c
// 
// 43    6/25/12 5:37p Arunsb
// [TAG]  		EIP93524
// [Category]  	Improvement
// [Description]  	Invalidate the BGRT table when boot device launched
// from BBS popup or from shell or launched as second boot device
// [Files]  		boot.c and minisetup.c
// 
// 42    5/29/12 3:53a Arunsb
// [TAG]  		EIP91109
// [Category]  	Improvement
// [Description]  	Sync the Aptio IV source for AptioV
// 
// 41    5/28/12 6:09a Rajashakerg
// [TAG]  		EIP89377
// [Category]  	Improvement
// [Description]  	Support to LegacyBootFailHook() in TSE.
// [Files]  		AMITSE.sdl, CommonHelper.c, boot.c
// 
// 40    5/25/12 1:38a Premkumara
// [TAG]  		EIP88430
// [Category]  	Improvement
// [Description]  	Resolution set after logo displayed should retain when
// boot to device.
// [Files]  		boot.c, logo.c
// 
// 39    5/08/12 2:31a Rajashakerg
// [TAG]  		EIP89483
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	Handle hidden boot options properly in BBS Pop up menu
// [RootCause]  	Check for LOAD_OPTION_HIDDEN is not present when
// TSE_BOOT_NOW_IN_BOOT_ORDER and POPUP_MENU_SHOW_ALL_BBS_DEVICES are
// Disabled
// [Solution]  	Provided the check for the loda option hidden.
// [Files]  		boot.c
// 
// 38    2/02/12 1:05p Premkumara
// [TAG]  		EIP75351
// [Category]  	Improvement
// [Description]  	Suppress the warnings from static code analyzer
// [Files]  		boot.c
// 
// 37    1/31/12 6:42a Arunsb
// [TAG]  		EIP81830
// [Category]  	Improvement
// [Description]  	Support to uninstall the BGRT on legacy boot.
// [Files]  		Commonhelper.c, postmgmt.c and boot.c
// 
// 36    1/13/12 1:40a Arunsb
// For shell the cursor is enabled. Default the cursor is disabled to
// avoid cursor visibility in win8 seamless boot
// 
// 35    1/10/12 2:36a Arunsb
// [TAG]  		EIP77400
// [Category]  	Improvement
// [Description]  	Need to do "Clear Screen" whenever boot to EFI Shell
// [Files]  		Boot.c and minisetup.h
// 
// 34    12/29/11 12:00p Arunsb
// [TAG]  		EIP74871
// [Category]  	Improvement
// [Description]  	When UEFI booting to windows, a cursor is displayed on
// the lower right hand corner of the screen
// [Files]  		Screen attribute changed to EFI_BACKGROUND_BLACK | EFI_WHITE
// 
// 33    12/14/11 12:14p Premkumara
// [TAG]  		EIP77400
// [Category]  	Improvement
// [Description]  	Need to do "Clear Screen" whenever boot to EFI Shell
// [Files]  		Boot.c
// 
// 32    12/10/11 4:43a Arunsb
// Corrected the header
// 
// 31    12/08/11 12:29p Arunsb
// UpdateDriverVariables function used properly
// 
// 30    11/30/11 1:20p Premkumara
// [TAG]  		EIP75352
// [Category]  	Improvement
// [Description]  	Suppress the warnings from static code analyzer
// [Files]  		Boot.c, bbs.c, TseAdvanced.c, Special.c, Variable.c,
// TseLiteHelper.c, PopupSel.c, AddBootOption.c, Hii.c, FormBrowser2.c
// 
// 29    11/24/11 5:26a Arunsb
// [TAG]  		EIP75351
// [Category]  	Improvement
// [Description]  	Suppress the warnings from static code analyzer
// [Files]  		boot.c
// 
// 28    11/16/11 11:17a Madhans
// [TAG]  		EIP75736
// [Category]  	Improvement
// [Description]  	To avoid flicks when booting UEFI OS for seamless boot.
// [Files]  		Boot.c 
// Tseadvanced.c
// 
// 27    11/13/11 12:30p Arunsb
// [TAG]  		EIP70421
// [Category]  	New Feature
// [Description]  	Support for driver order in TSE
// [Files]  		AMITSE.SDL, CommonHelper.c, setup.ini, uefisetup.ini,
// boot.c,
// minisetup.h, bbs.c, special.c, special.h, tseadvanced.c,
// addbootoption.c,
// callback.c, minisetupext.c, minisetupext.h, popupsel.c, popupsel.h,
// TseLitehelper.c, variable.c, Uefi21Wapper.c, AMIVfr.h, boot.h,
// TseElink.h, variable.h, 
// setup.h, Boot.vfr and Setup.uni
// 
// 26    11/02/11 10:43a Arunsb
// [TAG]  		EIP69884
// [Category]  	New Feature
// [Description]  	Support for UEFI Load Options - USB WWID
// [Files]  		boot.c
// 
// 25    11/01/11 3:53p Rajashakerg
// [TAG]  		EIP71223
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	TSE label 4.6.2_TSE_2_14_1219 has issues in Sandybridge
// platform
// [Solution]  	Fixed the issue by handling the return status when there
// is no valid boot option to boot.
// [Files]  		BootOnly\boot.c
// 
// 24    10/31/11 9:47a Rajashakerg
// [TAG]  		EIP71120,71512 
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	BIOS gets stuck in infinite loop On enabling
// TSE_LOAD_OPTION_HIDDEN token,In first boot incorrect devices is
// disabled in Setup & BBS table but in second boot the correct device is
// disabled in both places.
// [Solution]  	Hidden option handled properly.
// [Files]  		TseLite\variable.c, TseLite\minisetupext.c, TseAdvanced.c,
// special.c, BootOnly\minisetup.h,BootOnly\boot.c, BootOnly\bbs.c
// 
// 23    9/24/11 4:41a Arunsb
// [TAG]  		EIP70096
// [Category]  	Improvement
// [Description]  	Exposing the Handle of the image that's being launched
// for boot.
// This will help other elinks to do the processing. Needed for CsmOptOut
// Feature.
// [Files]  		BootOnly\boot.c
// 
// 22    7/19/11 11:53a Arunsb
// [TAG]  		EIP65320
// [Category]  	Improvement
// [Description]  	Board module hook for LoadImage failure
// [Files]  		boot.c and commonhelper.c
// 
// 21    7/11/11 4:50p Arunsb
// [TAG]  		EIP64295 
// [Category]  	New Feature
// [Description]  	Support for software created boot entry for USB.
// CheckDevSupShortFormPath function added to check whether the boot
// option supports short-form device path for USB class(Table 60) device
// path.
// Goto statement removed in CheckDevSupShortFormPath function.
// [Files]  		boot.c
// 
// 20    7/11/11 3:24p Arunsb
// [TAG]  		EIP64295 
// [Category]  	New Feature
// [Description]  	Support for software created boot entry for USB.
// CheckDevSupShortFormPath function added to check whether the boot
// option supports short-form device path for USB class(Table 60) device
// path.
// [Files]  		boot.c
//
// 19    6/30/11 4:13a Arunsb
// [TAG]           EIP57661
// [Category]      New Feature
// [Description]   Boot manager algorithm for interaction with Driver
// Health protocol.
//                 Boot device repair operation performed.
// [Files]         amitse.cif, amitse.sdl, faketokens.c, amitsestr.uni,
//                 commonhelper.c, uefisetup.ini, tsedrvhealth.h,
// amivfr.h, minisetupbin.mak,
//                 hiistring21.c, hiistring20.c, tseadvanced.c, special.c,
// special.h, boot.h, minisetup.h,
//                 uefi20wapper.c, formbrowser2.c, hii.c, parse.c and
// uefi21wapper.c.
//
// 18    6/29/11 6:12a Arunsb
// [TAG]           EIP 62631, 60128
// [Category]      New Feature
// [Description]   Hot key boot option in TSE as per UEFI spec. section
// 3.1.6.
// [Files]         AMITSE.sdl, CommonHelper.c, commonoem.c, commonoem.h,
// boot.c, hiistring20.c and hiistring21.c.
//
// 17    6/22/11 2:40p Arunsb
// [TAG]           EIP 62631
// [Category]      New Feature
// [Description]   Support for Hot key boot option in TSE as per UEFI
// spec. section 3.1.6.
// [Files]         AMITSE.sdl, CommonHelper.c, commonoem.c, commonoem.h,
// boot.c, hiistring20.c and hiistring21.c.
//
// 16    6/20/11 3:55p Arunsb
// [TAG]           EIP57660
// [Category]    New Feature
// [Description] Non-removable media boot behavior as described in UEFI
// specification v 2.3.1, p. 3.4.1.
// [Files]           amitse.sdl, bootflow.c, bootflow.h, commonoem.c,
// boot.c and protocol.c
//
// 15    6/20/11 11:45a Rajashakerg
// [TAG]  		EIP59417
// [Category]  	New Feature
// [Description]  	Spport LOAD_OPTION_HIDDEN option in TSE
// [Files]  		boot.h, AMITSE.sdl, CommonHelper.c, bbs.c, boot.c,
// minisetup.h, special.c, callback.c
//
// 14    6/10/11 2:35p Arunsb
// [TAG]  		EIP57660
// [Category]  	New Feature
// [Description]  	Non-removable media boot behavior as described in UEFI
// specification v 2.3.1, p. 3.4.1.2
// [Files]  		boot.c, bootflow.c and bootflow.h
//
// 13    6/04/11 1:49p Arunsb
// [TAG]             EIP58954
// [Category]      New Feature
// [Description]  Wrapper function added for InvalidateStatusInBgrt
// function.
// [Files]            Postmgmt.c, boot.c, logo.c commonoem.c and
// commonhelper.c
//
// 12    5/29/11 12:04p Arunsb
// [TAG]           EIP58954
// [Category]      New Feature
// [Description]   Quiet boot logo's only added for BGRT. BGRT status
// field cleared if any changes happened in screen other than displaying
// the image.
// [Files]         Postmgmt.c, boot.c, logo.c commonoem.c and
// commonhelper.c
//
// 11    3/15/11 5:15a Rajashakerg
// [TAG]  		EIP51671
// [Category]  	New Feature
// [Description]  	 Boot overide menu devices are not disable
// [Files]  		boot.c, minisetup.h, special.c, minisetupext.c, AMITSE.sdl,
// boot.h, CommonHelper.c
//
// 10    2/04/11 4:36p Mallikarjunanv
// UpdateBootVariables() function added to update boot and BBS Order
// varaibles.
//
// 9     1/14/11 6:05p Madhans
// [TAG]  		EIP52153
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	If new device added once we boot after reboot, Booting
// using BBS popup might Crash
// [RootCause]  	The BootOrder Varaible is read when TSE entry and And it
// is not synced with Cache value.
// [Solution]  	Update the BootOrder,BBSOrder variable cache, when we
// handle boot Variables in TSE
// [Files]  		boot.c
//
// 8     12/02/10 2:33p Madhans
// [TAG] - EIP 48169
// [Category]- Enhancement
// [Severity]- Mordarate
// [Symptom]- Code Cleanup and Compiler Warning need to resolved.
// [Rootcause] Warnings reported when we build AMI higher Warning level.
// [Solution]- 1. Fix the warnings and do the code cleanup.
//             2. Introduce proper checks.
//             3. change the popupSel.c to not change the Option/variable
// cache to default or first option
//                when the variable cache is not matching with any of
// option.
// [Files] - commonoem.c bbs.c boot.c hiistring.c resource.c
// popuppassword.c popupsel.c
//           expression.c hii.c parse.c
//
// 7     9/16/10 8:38p Madhans
// Update for TSE 2.10. Refer Changelog.log for more details.
//
// 13    9/13/10 3:10p Madhans
// [TAG]    	EIP41137
// [Category]	Improvement
// [Symptom]	Boot errors status reported for TSE loaded also
// [RootCause]	Fix done in Boot.c. To report the error status only for
// boot options.
//
// 12    9/08/10 6:53a Mallikarjunanv
// EIP-42080: TSE updates with respect to Fast Boot Support
//
// 11    8/23/10 4:00p Blaines
// In the function _BootSetBootManagerVariables, the assert that was
// ensuring that all disabled boot options were forced to the end of the
// list, was left outside of the code block (it was causing a hang  after
// inserting a new boot device, then re-booting).
//
// Only process the assert if the preserve disabled boot option order is
// disabled.
//
// 10    7/28/10 4:46a Mallikarjunanv
// EIP-29951: TSE Device Path Name support updated
//
// 9     4/13/10 6:01p Madhans
// Eip: 33100 To create new hook for BootNowLaunching. (From bbsPopoup and
// Boot override menu).
//
// 8     3/23/10 5:08p Blaines
// Preseve the order of disabled BBS boot devices.
//
// 7     2/19/10 8:14a Mallikarjunanv
// updated year in copyright message
//
// 6     2/15/10 10:10p Madhans
// To avoid Compilation issues
//
// 5     1/29/10 4:37p Madhans
// To not to depend on ConsoleControl and ConOut.
//
// 4     1/29/10 4:34p Madhans
// To avoid compiler warnings.
//
// 3     6/23/09 6:56p Blaines
// Coding standard update,
// Remove spaces from file header to allow proper chm function list
// creation.
//
// 2     6/12/09 7:41p Presannar
// Initial implementation of coding standards
//
// 1     6/04/09 8:05p Madhans
//
// 2     5/19/09 6:36p Madhans
// updated the code getting language codes
//
// 1     4/28/09 11:11p Madhans
// Tse 2.0 Code complete Checkin.
//
// 6     4/28/09 9:39p Madhans
// Tse 2.0 Code complete Checkin.
//
// 5     3/31/09 3:33p Madhans
// UnicodeCollection2 protocol support.
//
// 4     2/05/09 5:19p Madhans
// PostMgrStatus interface added.
//
// 3     2/05/09 10:15a Madhans
// Style Module created.
//
// 2     1/30/09 6:06p Madhans
// Function headers added.
//
// 1     12/18/08 7:58p Madhans
// Intial version of TSE Lite sources
//
//*****************************************************************//
//*****************************************************************//

//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name:		BOOT.C
//
// Description:	This file contains code for Boot management
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>

#include "minisetup.h"
#include EFI_PROTOCOL_DEFINITION (BlockIo)
#include EFI_PROTOCOL_DEFINITION (UsbIo)
#include EFI_PROTOCOL_DEFINITION (DiskIo)
#define BOOT_OPTION_ALLOC_UNIT  10
//////////////////////////////////////
//Extern Variables
//////////////////////////////////////
extern UINTN        	gDelOptionCount;
extern BOOT_DATA    	*gDelBootData;
extern UINT32       	gBootFlow;
extern UINTN 			CurrentScreenresolutionX, CurrentScreenresolutionY; //EIP-88430
//////////////////////////////////////
//Internal Variable Declarations
//////////////////////////////////////
UINTN		        		gBootOptionCount;
EFI_GUID	        		gBootNowCountGuid 	= 	BOOT_NOW_COUNT_GUID;
UINT16		        *gBBSDisabled 			= 	L"Disabled in BBS Order";
UINTN		        		gLangCount;
BOOT_DATA		    	*gBootData;
LANGUAGE_DATA	    	*gLanguages;
EFI_HANDLE          	gCurrentBootHandle 	= 	NULL;
EFI_EVENT				gShellLaunchEvent 	= NULL;		//EIP 77400 Clearing the screen for shell boot
EFI_EVENT				gShell20LaunchEvent 	= NULL;	//EIP158989 For shell2.0
UINT16 					DISABLED_BOOT_OPTION; //EIP-97704
UINT16 					DISABLED_DRIVER_OPTION; //EIP-97704
UINTN						gDriverOptionCount;//EIP70421 & 70422 Support for driver order
BOOT_DATA				*gDriverData;//EIP70421 & 70422 Support for driver order
BOOLEAN              gBrowserCallbackEnabled = FALSE; //EIP113266 Allow external drivers to change ASL cache only if it's TRUE; ignore browser callback otherwise
//////////////////////////////////////
//Internal Functions Declarations
//////////////////////////////////////
EFI_DEVICE_PATH_PROTOCOL *_BootBuildFVDevicePath( UINT32 *index, EFI_GUID *guidPtr );
EFI_DEVICE_PATH_PROTOCOL *_BootBuildFileDevicePath( UINT32 *index, CHAR16 *fileName );
EFI_DEVICE_PATH_PROTOCOL* _DiscoverPartition(EFI_DEVICE_PATH_PROTOCOL *DevicePath);
VOID        _BootSetBootManagerVariables(VOID);
VOID        _BootInstallLoadOptions( EFI_HANDLE handle, VOID *Options, UINTN OptionSize );
EFI_STATUS  _BootLaunchDevicePath( EFI_DEVICE_PATH_PROTOCOL *DevicePath, VOID *Options, UINTN OptionSize, BOOLEAN ValidBootOption );
UINT16      _BootSetBootNowCount(VOID);
CHAR16      *TseGetUefiDevPathString(EFI_DEVICE_PATH_PROTOCOL *DevPath);
VOID        InvalidateStatusInBgrtWrapper (VOID);
VOID        UninstallBgrtWrapper(VOID);//EIP81830 Support to uninstall the BGRT on legacy boot
VOID        FormHotBootKeys (CHAR16 *VarName);
VOID        RefreshBootKeysDetails (VOID);
VOID        FreeExtraKeyMemories (VOID);
EFI_STATUS	CheckForDeviceNeedRepair (EFI_DEVICE_PATH_PROTOCOL *);
EFI_HANDLE	CheckDevSupShortFormPath (EFI_DEVICE_PATH_PROTOCOL *);
VOID 			LegacyBootFailHook(EFI_STATUS);
VOID			UefiBootFailHook (EFI_STATUS);
VOID			UpdateBootVariables ();
VOID			FixHiddenOptions (BOOLEAN, UINT16 **, UINTN);
VOID 			_SetDriverManagerVariables (VOID);//EIP70421 & 70422 Support for driver order
VOID 			UpdateDriverVariables ();//EIP70421 & 70422 Support for driver order
UINT32  		ShellTextMode (VOID);//EIP 94205 : OEM needs Token to assign the display mode of SHELL.
UINTN   		PISpecVersion(VOID);//EIP-103540

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootGetBootOptions
//
// Description:	This function collects all the boot options, both efi
//              and legacy boot options, that are present in the
//              system and sets various boot manager variables that
//              are used to expand boot manager questions.
//
// Input:		VOID
//
// Output:		Return Status based on errors that occurred in library
//              functions.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS BootGetBootOptions( VOID )
{
    EFI_STATUS	Status;
    UINTN		AllocatedCount;

#ifndef STANDALONE_APPLICATION
    CHAR16		Pattern[]=L"boot[0-9a-f][0-9a-f][0-9a-f][0-9a-f]";
    CHAR16		KeyPattern[]=L"Key[0-9a-f][0-9a-f][0-9a-f][0-9a-f]";
    UINTN		OldVarNameSize;
    UINTN		VarNameSize;
    CHAR16		*VarName;
    EFI_GUID	VarGuid;
	VOID *		UnicodeInterface;
    UINTN		size=0, Length = 2;
    UINT8		*bufPtr;
    BOOT_OPTION *buffer;
    BOOT_DATA   *dataPtr;
	UINT32      *conditionPtr;
	UINT16      *BootOrder;
    CHAR16      *String=NULL;
//    UINTN       KeyVarNameSize = 0;
#endif //STANDALONE_APPLICATION
	BOOLEAN IsFastBoot=FALSE;
//EIP70421 & 70422 Support for driver order starts
	UINTN		DriverAllocatedCount;
    CHAR16		DriverPattern[] = L"Driver[0-9a-f][0-9a-f][0-9a-f][0-9a-f]";
	BOOLEAN 	BootOption = FALSE;
	BOOLEAN 	DriverOption = FALSE;
//EIP-97704 Starts
	UINT16 		jIndex 		= 	0;
	DISABLED_BOOT_OPTION 	= 	0xFFFF;
	DISABLED_DRIVER_OPTION 	= 	0xFFFF;
//EIP-97704 Ends
	DriverAllocatedCount = gDriverOptionCount = 0;
//EIP70421 & 70422 Support for driver order ends
    AllocatedCount = gBootOptionCount = 0;
#ifndef STANDALONE_APPLICATION
	Status = InitUnicodeCollectionProtocol(&UnicodeInterface);
    if(EFI_ERROR(Status))
        return Status;

    RefreshBootKeysDetails ();
    //start with a size of 80 bytes
    VarNameSize = 80;
    OldVarNameSize = VarNameSize;
    VarName = EfiLibAllocateZeroPool(VarNameSize);
    conditionPtr = VarGetNvramName( L"BootFlow", &_gBootFlowGuid, NULL, &size );
    if ( (conditionPtr != NULL) && (BOOT_FLOW_CONDITION_FAST_BOOT == *conditionPtr) )
	{
		IsFastBoot = TRUE;
		BootOrder = HelperGetVariable(VARIABLE_ID_BOOT_ORDER, L"BootOrder", &gEfiGlobalVariableGuid, NULL, &size );
		VarNameSize = ((EfiStrLen(L"BOOTXXXX")+1)*2);
		VarName = EfiLibAllocateZeroPool(VarNameSize);
		SPrint (VarName, VarNameSize, L"Boot%04X",BootOrder[0] );
	    MemFreePointer((VOID **) &BootOrder);
	    MemFreePointer((VOID **) &conditionPtr);
	}
    do
    {
		if (!IsFastBoot)
		{
			Status = gRT->GetNextVariableName (&VarNameSize,
											VarName,
											&VarGuid);
			if (Status == EFI_BUFFER_TOO_SMALL)
			{
				//Allocate correct size
				VarName = MemReallocateZeroPool (VarName,
												OldVarNameSize,
												VarNameSize);
				OldVarNameSize = VarNameSize;
				continue;
			}
		}
//        KeyVarNameSize = VarNameSize;                   													//EIP: 62631 For hot key boot
        if ( (VarNameSize == ((EfiStrLen (L"BOOTXXXX")+1)*2)) || (VarNameSize == ((EfiStrLen (L"DRIVERXXXX")+1)*2)) )	        //Find if this variable is one of L"BOOTXXXX" or L"DRIVERXXXX"
        {
			if (MetaiMatch (UnicodeInterface, VarName, Pattern))											//The buffer is definatly a boot#### option
			{
                size = 0;
                buffer = VarGetNvramName (VarName, &gEfiGlobalVariableGuid, NULL, &size);
                if (buffer)																					//Variable exists. Store details in global array.
                {
                    if (gBootOptionCount >= AllocatedCount)													//Check if enough size has been already allocated
                    {
                        gBootData = MemReallocateZeroPool (gBootData, AllocatedCount * sizeof(BOOT_DATA), (AllocatedCount + BOOT_OPTION_ALLOC_UNIT) * sizeof(BOOT_DATA));
                        if (NULL == gBootData)
						{
                            return EFI_UNSUPPORTED;
						}
                        AllocatedCount += BOOT_OPTION_ALLOC_UNIT;
                    }
					dataPtr = &(gBootData [gBootOptionCount]);
					HexStringToBuf(
						(UINT8 *)(&(dataPtr->Option)),
						&Length,
						&(VarName[4]),
						NULL);
					BootOption = TRUE;
					gBootOptionCount ++;
				}
			}
			else if (MetaiMatch (UnicodeInterface, VarName, DriverPattern))	//EIP70421 Support for Driver Order
			{
                size = 0;
                buffer = VarGetNvramName (VarName, &gEfiGlobalVariableGuid, NULL, &size);
                if (buffer)		//Variable exists. Store details in global array.
                {
                    if (gDriverOptionCount >= DriverAllocatedCount)		//Check if enough size has been already allocated
                    {
                        gDriverData = MemReallocateZeroPool (gDriverData, DriverAllocatedCount * sizeof (BOOT_DATA), (DriverAllocatedCount + BOOT_OPTION_ALLOC_UNIT) * sizeof(BOOT_DATA));
                        if (NULL == gDriverData)
						{
                            return EFI_UNSUPPORTED;
						}	
                        DriverAllocatedCount += BOOT_OPTION_ALLOC_UNIT;
                    }
					dataPtr = &(gDriverData [gDriverOptionCount]);
					HexStringToBuf(
						(UINT8 *)(&(dataPtr->Option)),
						&Length,
						&(VarName[6]),
						NULL);
					DriverOption = TRUE;
					gDriverOptionCount ++;
				}
			}
			else
			{
				goto _NextVariable;					//Jumping for next variable iteration
			}
			if (DriverOption || BootOption)			//Common code for both driver and boot options
			{
				dataPtr->Active = buffer->Active;
				dataPtr->Name = StrDup (buffer->Name);
				bufPtr = (UINT8 *)((CHAR16 *)buffer->Name + EfiStrLen( buffer->Name ) + 1);
				dataPtr->DevicePath = EfiLibAllocateZeroPool( buffer->PathLength );
				if (NULL != dataPtr->DevicePath)
				{
					MemCopy (dataPtr->DevicePath, bufPtr, buffer->PathLength);
				}
				bufPtr += buffer->PathLength;
				size -= (UINTN)bufPtr - (UINTN)buffer;
				if (size != 0)
				{
					dataPtr->LoadOptions = EfiLibAllocatePool( size );
					if ( dataPtr->LoadOptions != NULL )
					{
						dataPtr->LoadOptionSize = size;
						MemCopy( dataPtr->LoadOptions, bufPtr, size );
					}
				}
				if (BootOption)
				{
	                //In case of DEVIE PATH support, to reset the name using device path
	                String = TseGetUefiDevPathString(dataPtr->DevicePath);
	                if (NULL != String)
					{
	                    // For UEFI boot options, Prefix the string with "UEFI: "
	                    MemFreePointer( (VOID **)&(dataPtr->Name) );
	                    dataPtr->Name = EfiLibAllocateZeroPool( EfiStrLen( String ) + EfiStrLen(L"UEFI: ")+ 1 );
	                    EfiStrCpy(dataPtr->Name, L"UEFI: ");
	                    EfiStrCat(dataPtr->Name, String);
	                }
	            }
				MemFreePointer( (VOID **)&buffer );
			}
_NextVariable:
			BootOption = DriverOption = FALSE;		//Getting ready for next iteration
        }
        else if (VarNameSize == ((EfiStrLen(L"keyXXXX")+1)*2))		//Boot, Driver and Key are different length so handling in else if cases
        {
            if (MetaiMatch (UnicodeInterface, VarName, KeyPattern))
            {
                FormHotBootKeys (VarName);
            }
        }
		// Only one Boot options is valid in FastBoot case
		if(IsFastBoot)
			break;
        //reset the size of this buffer to what has been allocated for it
        VarNameSize = OldVarNameSize;

    }while(Status != EFI_NOT_FOUND);

#endif //STANDALONE_APPLICATION

//EIP-97704 Starts
	//For Boot#### variable
	for ( jIndex = 0; jIndex < gBootOptionCount; jIndex++ )
	{
		if ( gBootData[jIndex].Option == DISABLED_BOOT_OPTION )
		{
			DISABLED_BOOT_OPTION--;//Changing the value based on the available of BOOT#### variable
			jIndex	=	-1;
			continue;
		}
	}
	//For Driver#### variable
	for ( jIndex = 0; jIndex < gDriverOptionCount; jIndex++ )
	{
		if ( gDriverData[jIndex].Option == DISABLED_DRIVER_OPTION )	
		{
			DISABLED_DRIVER_OPTION--;//Changing the value based on the available of DRIVER#### variable
			jIndex	=	-1;
			continue;
		}
	}
//EIP-97704 Ends
    //Free unused memory
    if (gBootOptionCount < AllocatedCount)
        gBootData = MemReallocateZeroPool(gBootData, AllocatedCount * sizeof (BOOT_DATA), gBootOptionCount * sizeof (BOOT_DATA));

    if (gDriverOptionCount < DriverAllocatedCount)
        gDriverData = MemReallocateZeroPool(gDriverData, DriverAllocatedCount * sizeof (BOOT_DATA), gDriverOptionCount * sizeof (BOOT_DATA));

    FreeExtraKeyMemories ();
    //Set boot manager variables
	if(!IsFastBoot)
	{
	    _BootSetBootManagerVariables ();
		_SetDriverManagerVariables ();
	}
    Status = EFI_SUCCESS;
    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootLaunchBootOption
//
// Description:	This function launches the boot option supplied
//
// Input:		u16Option: BootOption to be launched
//              pOrder: The order in which to set BBS priorities
//              u16OrderCount: No of options in pOrder
//
// Output:		Return Status based on errors that occurred in library
//              functions.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS BootLaunchBootOption(UINT16 u16Option, UINT16 *pOrder, UINTN u16OrderCount)
{
    EFI_STATUS	status = EFI_NOT_FOUND;
    UINT16		u16BootCurrent;

    BOOT_DATA	*pBootData = NULL;

    pBootData = BootGetBootData(u16Option);
    if (pBootData)
    {
        //Set BootCurrent
        u16BootCurrent = pBootData->Option;
        VarSetNvramName(L"BootCurrent",
                &gEfiGlobalVariableGuid,
                EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
                &(u16BootCurrent),
                sizeof(u16BootCurrent));
        if( gBootFlow != BOOT_FLOW_CONDITION_FAST_BOOT) {
        	CsmBBSSetBootPriorities(pBootData, pOrder, u16OrderCount);
        }
        status = _BootLaunchDevicePath(pBootData->DevicePath, pBootData->LoadOptions, pBootData->LoadOptionSize,TRUE);

        //clear BootCurrent
        VarSetNvramName(L"BootCurrent",
                &gEfiGlobalVariableGuid,
                EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
                &(u16BootCurrent),
                0);
    }

    return status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootGetBootData
//
// Description:	Finds and returns internal data structure BOOT_DATA
//              for a given boot option number.
//
// Input:		Option: Option number for which BOOT_DATA is needed
//
// Output:		Returns pointer to BOOT_DATA if found. Returns NULL
//              if BOOT_DATA not found.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOT_DATA *BootGetBootData( UINT16 Option )
{
    UINTN i;

    for ( i = 0; i < gBootOptionCount; i++ )
    {
        if ( gBootData[i].Option == Option )
            return &gBootData[i];
    }

    return NULL;
}

//EIP70421 & 70422  Support for driver order
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	DriverGetDriverData
//
// Description:	Finds and returns internal data structure BOOT_DATA
//              for a given Driver option number.
//
// Input:		Option: Option number for which BOOT_DATA is needed
//
// Output:		Returns pointer to BOOT_DATA if found. Returns NULL
//              if BOOT_DATA not found.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOT_DATA *DriverGetDriverData (UINT16 Option)
{
    UINTN i;

    for ( i = 0; i < gDriverOptionCount; i++ )
    {
        if ( gDriverData[i].Option == Option )
            return &gDriverData[i];
    }
    return NULL;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootGetOptionName
//
// Description:	Provides the unicode name for the given boot option
//              in the form of internal data structure BOOT_DATA.
//
// Input:		bootData: BOOT_DATA struct for which unicode name is
//              needed.
//
// Output:		Returns unicode string corresponding to the provided
//              BOOT_DATA. If the BOOT_DATA provided is for legacy
//              option then the name for the first legacy option in
//              that category is returned.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
CHAR16 *BootGetOptionName( BOOT_DATA *bootData)
{
    if ( BBSValidDevicePath(bootData->DevicePath) ) {
		return CsmBBSBootOptionName(bootData);
    }
    return bootData->Name;
}

//EIP70421 & 70422  Support for driver order
//<AMI_PHDR_START>
//------------------------------------------------------------------------------------
// Procedure:	DriverGetOptionName
//
// Description:	Provides the unicode name for the given driver option
//              in the form of internal data structure BOOT_DATA.
//
// Input:		DriverData: BOOT_DATA struct for which unicode name is
//              needed.
//
// Output:		Returns unicode string corresponding to the provided BOOT_DATA
//
//------------------------------------------------------------------------------------
//<AMI_PHDR_END>
CHAR16 *DriverGetOptionName (BOOT_DATA *DriverData)
{
    return DriverData->Name;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootGetBootNowName
//
// Description:	Provides the unicode name for the given boot option.
//
// Input:		value: boot option for which unicode name is needed
//
// Output:		Returns unicode string corresponding to the provided
//              boot option.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
CHAR16 *BootGetBootNowName( UINT16 value, BOOLEAN ShowAllBBSDev, BOOLEAN TseBootNowInBootOrde)
{
    UINT16	*BootOrder=NULL;
    UINTN	size = 0;
    UINTN	i,j,k;
    UINTN	count;

    BOOT_DATA *bootData;

	if((!ShowAllBBSDev) && (!TseBootNowInBootOrde))
		if(gBootData == NULL)
			return NULL;
		else
		{
			if(gLoadOptionHidden && (gBootData[value].Active & LOAD_OPTION_HIDDEN))
				return NULL;//EIP 89483 : Cheking for the load option hidden presence in the Boot option

			return BootGetOptionName(&(gBootData[value]));
		}
	if(TseBootNowInBootOrde)
	{
		BootOrder = HelperGetVariable(VARIABLE_ID_BOOT_ORDER, L"BootOrder", &gEfiGlobalVariableGuid, NULL, &size );
  		//EIP-75352 Suppress the warnings from static code analyzer
		if(NULL == BootOrder){
			ASSERT (0);
			return NULL;
		}

	    //Find the first disabled option
	    for ( i = 0; i < gBootOptionCount; i++ )
	    {
	        if ( DISABLED_BOOT_OPTION == BootOrder[i] )
	            break;
	    }

	    if(i<gBootOptionCount)
	    {
	        //There are disabled options replace them with valid options
	        for(j=0;j<gBootOptionCount;j++)
	        {
	            for(k=0;k<gBootOptionCount;k++)
	            {
	                if(BootOrder[k] == gBootData[j].Option)
	                    break;
	            }

	            if(k >= gBootOptionCount)
	            {
	                //gBootData[j].Option is not present in BootOrder; fill it
	                BootOrder[i] = gBootData[j].Option;
	                i++;
	            }
	        }
	    }
	}

	if(ShowAllBBSDev)
	{
	    count = 0;
	    for(i=0;i<gBootOptionCount;i++)
	    {
			if(TseBootNowInBootOrde) {
		        bootData = BootGetBootData(BootOrder[i]);
				//EIP-75352 Suppress the warnings from static code analyzer
				if(NULL == bootData){
					return NULL;
				}
			}
			else {
	     		bootData = gBootData + i;
			}

	        if ( BBSValidDevicePath(bootData->DevicePath) )
	        {
	            if((value >= count) && (value < (count+bootData->LegacyDevCount)))
	            {
					if(TseBootNowInBootOrde)
		                MemFreePointer((VOID **) &BootOrder);
				if (gLoadOptionHidden && ((bootData->Active & LOAD_OPTION_HIDDEN)!= 0))
 					return NULL;
				
	                return bootData->OrderList[value-count].Name;
	            }

	            count+=bootData->LegacyDevCount;
	        }
	        else {
	            if(value == count)
	            {
					if(TseBootNowInBootOrde)
		                MemFreePointer((VOID **) &BootOrder);
					if (gLoadOptionHidden && ((bootData->Active & LOAD_OPTION_HIDDEN)!= 0))
 					 return NULL;
	                return bootData->Name;
	            }
	            count++;
	        }
	    }
 		return NULL;
	}
	else {
	    bootData = BootGetBootData(BootOrder[value]);
	    MemFreePointer((VOID **) &BootOrder);
		if (gLoadOptionHidden && ((bootData->Active & LOAD_OPTION_HIDDEN)!= 0))
				return NULL;//EIP 89483 : Cheking for the load option hidden presence in the Boot option

	    return BootGetOptionName(bootData);
	}

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootGetLanguages
//
// Description:	Finds the languages that the firmware supports.
//
// Input:		VOID
//
// Output:		VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID GetBootLanguages( VOID )
{
    CHAR16	*langString;
    CHAR8	*langCodes, *langPtr;
    UINTN	i, count, size = 0;

    langCodes = VarGetNvramName( L"LangCodes", &gEfiGlobalVariableGuid, NULL, &size );
    count = size /3;

    if ( size == 0 )
    {
	    langCodes = VarGetNvramName( L"Lang", &gEfiGlobalVariableGuid, NULL, &size );
		if ( size != 0) {
		    count = 1;
		}
		else {
	        gLanguages = EfiLibAllocateZeroPool( sizeof(LANGUAGE_DATA) );
	        if ( gLanguages == NULL )
	            return;

			gLanguages[0].LangCode = StrDup8("eng");
			gLanguages[0].Unicode = StrDup(L"eng");
	        gLangCount = 1;
	        langString = HiiGetStringLanguage( (VOID*)(UINTN)INVALID_HANDLE, 1, gLanguages[0].Unicode );
	        gLanguages[0].Token = HiiAddString( gHiiHandle, langString );
	        gLanguages[0].LangString = langString;
	        return;
		}
    }

    gLanguages = EfiLibAllocateZeroPool( count * sizeof(LANGUAGE_DATA) );
    if ( gLanguages == NULL )
        return;

    for ( langPtr = langCodes, i = 0; i < count; i++, langPtr += 3 )
    {
		gLanguages[i].LangCode = EfiLibAllocateZeroPool( 4 * sizeof(CHAR8));
        MemCopy( gLanguages[i].LangCode, langPtr, 3 * sizeof(CHAR8) );
		gLanguages[i].Unicode = StrDup8to16(gLanguages[i].LangCode);
    }

    gLangCount = count;

    for ( langPtr = langCodes, i = 0; i < count; i++, langPtr += 3 )
    {
        langString = HiiGetStringLanguage( (VOID*)(UINTN)INVALID_HANDLE, 1, gLanguages[i].Unicode );
        gLanguages[i].Token = HiiAddString( gHiiHandle, langString );
        gLanguages[i].LangString = langString;
    }

    MemFreePointer( (VOID **)&langCodes );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootGetLanguages
//
// Description:	Finds the languages that the firmware supports.
//
// Input:		VOID
//
// Output:		VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID GetPlatformBootLanguages( VOID )
{
    CHAR16	*langString;
    CHAR8	*langCodes, *langPtr;
    UINTN	i = 0, count=0, size = 0, pos = 0;

    langCodes = VarGetNvramName( L"PlatformLangCodes", &gEfiGlobalVariableGuid, NULL, &size );
    if(langCodes != NULL)
		count = GetTokenCount(langCodes);

    if ((0 == size) || (NULL == langCodes))
    {
	    langCodes = VarGetNvramName( L"PlatformLang", &gEfiGlobalVariableGuid, NULL, &size );
	    if ( size  != 0 ) {
			count=1;
		}
		else {
		        gLanguages = (LANGUAGE_DATA *)EfiLibAllocateZeroPool( sizeof(LANGUAGE_DATA) );
	        if ( gLanguages == NULL )
	            return;

	        gLanguages[0].LangCode = StrDup8("en-US");
	        gLanguages[0].Unicode = StrDup(L"en-US");

	        gLangCount = 1;
	        langString = HiiGetStringLanguage( (VOID*)(UINTN)INVALID_HANDLE, 1, gLanguages[0].Unicode );
	        gLanguages[0].Token = HiiAddString( gHiiHandle, langString );
	        gLanguages[0].LangString = langString;
	        return;
	    }
	}

    gLanguages = EfiLibAllocateZeroPool( count * sizeof(LANGUAGE_DATA) );
    if ( gLanguages == NULL )
        return;

    for ( langPtr = langCodes, i = 0; i < count; i++ )
    {
        gLanguages[i].LangCode = GetTokenString(langPtr, &pos);
        gLanguages[i].Unicode  = StrDup8to16(gLanguages[i].LangCode);
        langString = HiiGetStringLanguage( INVALID_HANDLE, 1, gLanguages[i].Unicode );
        gLanguages[i].Token = HiiAddString( gHiiHandle, langString );
        gLanguages[i].LangString = langString;

        langPtr += pos + 1;
    }

    gLangCount = count;
    MemFreePointer( (VOID **)&langCodes );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootLaunchBootNow
//
// Description:	Launches the boot option provided.
//
// Input:		index: The nth option in the boot now menu.
//				ShowAllBbsDev - SETUP_SHOW_ALL_BBS_DEVICES
//
// Output:		Return Status based on errors that occurred in library
//              functions.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS BootLaunchBootNow( UINT16 index, BOOLEAN ShowAllBbsDev )
{

    EFI_STATUS	Status = EFI_OUT_OF_RESOURCES;
    BOOLEAN		bLegacyBoot = FALSE;
    UINT16		count = 0, BootCurrent;
    UINT16		*BootOrder;
    UINTN		i = 0;

    BOOT_DATA *bootData=NULL;

	ProcessProceedToBootNowHook();

    BootOrder = BootNowinBootOrderInit();

	if(ShowAllBbsDev)
	{
	    for(i=0;i<gBootOptionCount;i++)
	    {
			bootData = BootGetBootNowBootData(gBootData,BootOrder,i);
			//EIP-75352 Suppress the warnings from static code analyzer
			if(NULL == bootData){
				continue;
			}
	        if ( BBSValidDevicePath(bootData->DevicePath) )
	        {
	            if((index >= count) && (index < (count+bootData->LegacyDevCount)))
	            {
	                bLegacyBoot = TRUE;
	                break;
	            }
	            else
	                count=count+(UINT16)(bootData->LegacyDevCount);
	        }
	        else
	        {
	            if(index == count)
	                break;
	            count++;
	        }
	    }
	}
	else
	{
		if(0 == gBootOptionCount)//EIP 71223: If there is no valid boot option present, returning EFI_NOT_FOUND. 
	        return EFI_NOT_FOUND;
		bootData = BootGetBootNowBootData(gBootData,BootOrder,index);
	}

	if(BootOrder!=NULL)
		MemFreePointer((VOID **) &BootOrder);

	//EIP-75352 Suppress the warnings from static code analyzer
	if(NULL == bootData){
		return EFI_NOT_FOUND;
	}

	if(ShowAllBbsDev)
	{
	    if(i == gBootOptionCount)
	        return Status;

	    //Boot bootData and device index-count
	    //set bbs priorities
	    if(bLegacyBoot)
		{
			Status = CsmBBSSetBootNowPriority(bootData,index-count,ShowAllBbsDev);
			if(EFI_ERROR( Status )) //EIP-93797
				return EFI_UNSUPPORTED;
		}
	}

    //Set BootCurrent
    BootCurrent = bootData->Option;
    VarSetNvramName(L"BootCurrent",
            &gEfiGlobalVariableGuid,
            EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
            &(BootCurrent),
            sizeof(BootCurrent));
	if(!ShowAllBbsDev)
	{
	    if(BBSValidDevicePath(bootData->DevicePath))
		{
	        Status = CsmBBSSetBootNowPriority(bootData,0,ShowAllBbsDev);
			if(EFI_ERROR( Status )) //EIP-93797
				return EFI_UNSUPPORTED;
		}	
	}
    Status = _BootLaunchDevicePath( bootData->DevicePath, bootData->LoadOptions, bootData->LoadOptionSize,TRUE );
    //clear BootCurrent
    VarSetNvramName(L"BootCurrent",
            &gEfiGlobalVariableGuid,
            EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
            &(BootCurrent),
            0);

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootLaunchGuid
//
// Description:	Launches the guided file from FV.
//
// Input:		guid: Guid of the file to be launched.
//
// Output:		Return Status based on errors that occurred in library
//              functions.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS BootLaunchGuid( EFI_GUID *guid )
{
    EFI_STATUS	Status = EFI_UNSUPPORTED;
    UINT32		index = 0;

    EFI_DEVICE_PATH_PROTOCOL *devicePath;

    do
    {
        devicePath = _BootBuildFVDevicePath( &index, guid );
        if ( index != (UINT32)-1 )
            Status = _BootLaunchDevicePath( devicePath, NULL, 0,FALSE );

        MemFreePointer( (VOID **)&devicePath );
    }
    while ( ( EFI_ERROR( Status ) ) && ( index != (UINT32)-1 ) );

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootLaunchFilename
//
// Description:	Launches the file mentioned in file path from the
//              available FS.
//
// Input:		fileName: Path of the file to be launched.
//
// Output:		Return Status based on errors that occurred in library
//              functions.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS BootLaunchFilename( CHAR16 *fileName )
{
    EFI_STATUS Status = EFI_UNSUPPORTED;
    UINT32     index = 0;

    EFI_DEVICE_PATH_PROTOCOL *devicePath;

    do
    {
        devicePath = _BootBuildFileDevicePath( &index, fileName );
        if ( index != (UINT32)-1 )
            Status = _BootLaunchDevicePath( devicePath, NULL, 0,FALSE );

        MemFreePointer( (VOID **)&devicePath );
    }
    while ( ( EFI_ERROR( Status ) ) && ( index != (UINT32)-1 ) );

    return Status;
}

//<AMI_PHDR_START>
//-------------------------------------------------------------------------------
// Procedure:	FixHiddenOptions
//
// Description:	Function to move the hidden option at last of the boot order
//
// Input:		UINT16 ** => Boot order to be reformed
//
// Output:		VOID
//
//--------------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID FixHiddenOptions (BOOLEAN Option, UINT16 **Order, UINTN OptionCount)
{
	UINTN iIndex = 0;
	UINTN jIndex = 0;
	UINT16 TempBootOption = 0;
	BOOT_DATA   *pBootData;

	if (0 == OptionCount)
	{
		return;
	}
	for (iIndex = 0; iIndex < OptionCount-1; iIndex ++)
	{
		if (BOOT_ORDER_OPTION == Option)
		{
			pBootData = BootGetBootData ((*Order) [iIndex]);
		}
		else
		{
			pBootData = DriverGetDriverData ((*Order) [iIndex]);
		}
		if (pBootData)
		{
			if (pBootData->Active & LOAD_OPTION_HIDDEN)
			{
				for (jIndex = iIndex+1; jIndex < OptionCount; jIndex ++)
				{
					if (BOOT_ORDER_OPTION == Option)
					{
						pBootData = BootGetBootData ((*Order) [jIndex]);
					}
					else
					{
						pBootData = DriverGetDriverData ((*Order) [jIndex]);
					}
					if (!(pBootData->Active & LOAD_OPTION_HIDDEN))
					{
						TempBootOption = (*Order) [iIndex];
						(*Order) [iIndex] = (*Order) [jIndex];
						(*Order) [jIndex] = TempBootOption;
						break;
					}
				}
			}
		}
	}
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	_BootSetBootManagerVariables
//
// Description:	function to set the variables for the boot manager
//
// Input:		void
//
// Output:		void
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID _BootSetBootManagerVariables(VOID)
{
    UINT16	*NewBootOrder, *BootOrder, count;
    UINTN	size=0, i=0, j=0, k=0, BootOrderCount;

    EFI_GUID 	BootManGuid = BOOT_MANAGER_GUID;
    BOOT_DATA   *pBootData;

    //Correct BootOrder variable if necessary
    NewBootOrder = NULL;
    if(gBootOptionCount)
        NewBootOrder = (UINT16 *)EfiLibAllocateZeroPool(gBootOptionCount * sizeof(UINT16));

    BootOrder = (UINT16 *)VarGetNvramName( L"BootOrder", &gEfiGlobalVariableGuid, NULL, &size );
    BootOrderCount = size/sizeof(UINT16);

    //Get all the enabled boot options in the boot order
    for(i = 0; i < BootOrderCount; i++)
    {
        pBootData = BootGetBootData(BootOrder[i]);
        if(pBootData)
        {
	  	    if(IsPreservedDisabledBootOptionOrder())
		    {	 //EIP:59417 - Checking the  LOAD_OPTION_HIDDEN for the boot option
				/*if(gLoadOptionHidden && (pBootData->Active & LOAD_OPTION_HIDDEN))
				{
					j++;
					continue;
				}*/
				NewBootOrder[j] = BootOrder[i];
	   			j++;
		    }
		    else if(pBootData->Active & LOAD_OPTION_ACTIVE)
	        {	 //EIP:59417 - Checking the  LOAD_OPTION_HIDDEN for the boot option
				/*if(gLoadOptionHidden && (pBootData->Active & LOAD_OPTION_HIDDEN))
				{
					j++;
					continue;
				}*/
	            NewBootOrder[j] = BootOrder[i];
	            j++;
	        }
        }
    }

    //Append all options that are enabled but not included in
    //BootOrder. FCFS used.
    for(i=0; i < gBootOptionCount; i++)
    {
        if(gBootData[i].Active & LOAD_OPTION_ACTIVE)
        {
            //Check presence in boot order
            for(k=0;k<BootOrderCount;k++)
            {
                if(BootOrder[k] == gBootData[i].Option)
                    break;
            }
            if(k >= BootOrderCount)
            {
                //Not present in boot order! Add option
                NewBootOrder[j] = gBootData[i].Option;
                j++;
            }
        }
    }

    //Free Boot order
    MemFreePointer((VOID **)&BootOrder);

    if(!IsPreservedDisabledBootOptionOrder())
    {
        //Put disabled options at the end of NewBootOrder
        for(i=0; i < gBootOptionCount; i++)
        {
            if(!(gBootData[i].Active & LOAD_OPTION_ACTIVE))
            {	 //EIP:59417 - Checking the  LOAD_OPTION_HIDDEN for the boot option
/*				if(gLoadOptionHidden && (gBootData[i].Active & LOAD_OPTION_HIDDEN))
				{
					j++;
					continue;
				}*/
    			NewBootOrder[j] = gBootData[i].Option;
    			j++;
	        }
        }
        ASSERT(j==gBootOptionCount);
    }

    //Set BootOrder Variable with corrected order
	if (gLoadOptionHidden)
	{
		FixHiddenOptions (BOOT_ORDER_OPTION, &NewBootOrder, gBootOptionCount);
	}
    VarSetNvramName(L"BootOrder",
        &gEfiGlobalVariableGuid,
        EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
        NewBootOrder,
        gBootOptionCount * sizeof(UINT16));

    //Free NewBootOrder
    MemFreePointer((VOID **)&NewBootOrder);

    //Get BBS devices
    CsmBBSGetDeviceList();

    // Update the BootOrder,BBSOrder Cache From the NVRAM.
    UpdateBootVariables();

    //Set Boot manager variable
    count = (UINT16)gBootOptionCount;
    VarSetNvramName( L"BootManager", &BootManGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS, &count, sizeof(count) );

    //Set Boot now count
	if(gShowAllBbsDev)
  		count = _BootSetBootNowCount();

    VarSetNvramName( L"BootNowCount", &gBootNowCountGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS, &count, sizeof(count) );
	UpdateAddDeleteBootVar ();
}

//EIP70421 & 70422  Support for driver order Starts
//<AMI_PHDR_START>
//--------------------------------------------------------------------------------------
// Procedure:	_SetDriverManagerVariables
//
// Description:  Sets DriverManager variable for drivers count and sets DriverOrder
//
// Input:   void
//
// Output:  void
//
//--------------------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID _SetDriverManagerVariables (VOID)
{
	UINT16	*DriverOrder = NULL;
	UINT16	*NewDriverOrder = NULL;
	UINT16	DriverOrderCount = 0;
    UINTN	DriverOrderSize = 0;
	UINTN 	iIndex = 0;
	UINTN 	jIndex = 0;
	UINT16 	Count = 0;
    BOOT_DATA   *pDrvData;			//BOOT_DATA using same structure for driver options too
    EFI_GUID 	DrvMgrGuid = DRIVER_MANAGER_GUID;

	if (gDriverOptionCount)
	{	
	    NewDriverOrder = (UINT16 *)EfiLibAllocateZeroPool (gDriverOptionCount * sizeof(UINT16));
		if (NULL == NewDriverOrder)
		{
			return;
		}
	}
	DriverOrder = (UINT16 *)VarGetNvramName (L"DriverOrder", &gEfiGlobalVariableGuid, NULL, &DriverOrderSize);
	DriverOrderCount = (UINT16)(DriverOrderSize/sizeof(UINT16));
	if ((NULL == DriverOrder) || (0 == DriverOrderSize))
	{
		for (iIndex = 0; iIndex < gDriverOptionCount; iIndex++)			//Forming DriverOrder variable freshly
        {
			if (gDriverData [iIndex].Active & LOAD_OPTION_ACTIVE)
			{
				NewDriverOrder [Count] = gDriverData [iIndex].Option;
				Count ++;
			}
		}
		goto _FormDisableDevices;
	}
	else											//Forming DriverOrder variable with first active and non hidden drivers
	{
	    for (iIndex = 0; iIndex < DriverOrderCount; iIndex ++)
	    {
	        pDrvData = DriverGetDriverData (DriverOrder [iIndex]);
	        if (pDrvData)
	        {													
		    	if ( (pDrvData->Active & LOAD_OPTION_ACTIVE) )				
				{													//Checking for active drivers
	                NewDriverOrder [Count] = DriverOrder [iIndex];
	                Count ++;
	            }
	        }
	    }
	}
	//Append all options that are enabled but not included in
    //DriverOrder. Having same logic as of boot order.
    for(iIndex = 0; iIndex < gDriverOptionCount; iIndex ++)
    {
		if ( (gDriverData [iIndex].Active & LOAD_OPTION_ACTIVE) )
        {
            //Check presence in DriverOrder
            for (jIndex = 0; jIndex < DriverOrderCount; jIndex ++)
            {
                if (DriverOrder [jIndex] == gDriverData [iIndex].Option)
                    break;
            }
            if (jIndex >= DriverOrderCount)
            {
                //Not present in Driver order! Add option
                NewDriverOrder [Count] = gBootData[iIndex].Option;
                Count ++;
            }
        }
    }
_FormDisableDevices:			//Forming disabled drivers last in the newly formed DriverOrder variable.
	for (iIndex = 0; iIndex < gDriverOptionCount; iIndex ++)
	{
		if (!(gDriverData [iIndex].Active & LOAD_OPTION_ACTIVE))
		{	 
			NewDriverOrder [Count] = gDriverData [iIndex].Option;
			Count ++;
		}
	}
	if (gLoadOptionHidden)
	{
		FixHiddenOptions (DRIVER_ORDER_OPTION, &NewDriverOrder, gDriverOptionCount);
	}
	VarSetNvramName (
		L"DriverOrder",
		&gEfiGlobalVariableGuid,
		EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
		NewDriverOrder,
		gDriverOptionCount * sizeof(UINT16)
		);
    //Set Driver manager variable
    Count = (UINT16)gDriverOptionCount;
    VarSetNvramName (L"DriverManager", &DrvMgrGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS, &Count, sizeof(Count));
    UpdateDriverVariables ();
	if (DriverOrder)
	{
		MemFreePointer ((VOID **)&DriverOrder);
	}
	if (DriverOrder)
	{
		MemFreePointer ((VOID **)&NewDriverOrder);
	}
	UpdateAddDeleteDriverVar ();
}
//EIP70421 & 70422  Support for driver order Ends

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	_BootSetBootNowCount
//
// Description:	function to set boot new devices count
//
// Input:		void
//
// Output:		void
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT16 _BootSetBootNowCount(VOID)
{
    UINT16 count = 0;
    UINTN i;

    BOOT_DATA *bootData;

    for( i=0; i<gBootOptionCount; i++)
    {
        bootData = &(gBootData[i]);

		if(BBSValidDevicePath(bootData->DevicePath))
        {
            count = count + bootData->LegacyDevCount;
        }
        else
            count++;
    }

    return count;
}

//EIP 77400 Starts
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	_ShellClearScreen
//
// Description:	Clears the screen for shell boot
//
// Input:		EFI_EVENT , VOID *
//
// Output:		VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void ClearGrphxScreen (void);
VOID _ShellClearScreen (EFI_EVENT Event, VOID *Context)
{
	UINT32 shelltextmodetype;
	InvalidateStatusInBgrtWrapper();				//EIP93524 : When booting to shell clearing the BGRT status bit.
	ClearGrphxScreen ();
	gST->ConOut->ClearScreen (gST->ConOut);
	gBS->CloseEvent (Event);
	gST->ConOut->EnableCursor (gST->ConOut, TRUE);
	if (Event != gShellLaunchEvent)				//One event will be closed in incoming fnc argument other we have to close it
	{
		gBS->CloseEvent (gShellLaunchEvent);
	}
	if (Event != gShell20LaunchEvent)				//EIP158989 For shell2.0
	{
		gBS->CloseEvent (gShell20LaunchEvent);
	}
	gShellLaunchEvent = NULL;					//Make NULL otherwise we will try to close it after startimage
	gShell20LaunchEvent = NULL;

	shelltextmodetype = ShellTextMode();//EIP 94205 : OEM needs Token to assign the display mode of SHELL.
	
	if ( 0xFF != shelltextmodetype )
		gST->ConOut->SetMode( gST->ConOut, shelltextmodetype );	
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	_RegisterShellGuid
//
// Description:	Registers the shell guid
//
// Input:		VOID
//
// Output:		VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID _RegisterShellGuid (VOID)
{
	EFI_STATUS 	Status = EFI_UNSUPPORTED;
	VOID 		*Registration = NULL;
	EFI_GUID 	EfiShellInterfaceGuid = EFI_SHELL_PROTOCOL_GUID;
	EFI_GUID 	EfiShell20InterfaceGuid = EFI_SHELL2_0_FILE_GUID;

	Status = gBS->CreateEvent (
				EFI_EVENT_NOTIFY_SIGNAL, 
				EFI_TPL_CALLBACK,
				_ShellClearScreen,
				NULL,
				&gShellLaunchEvent);
	if (!EFI_ERROR (Status))
	{
		Status = gBS->RegisterProtocolNotify(
				&EfiShellInterfaceGuid,
				gShellLaunchEvent,
				&Registration
				);
	}
	Status = gBS->CreateEvent (
				EFI_EVENT_NOTIFY_SIGNAL, 
				EFI_TPL_CALLBACK,
				_ShellClearScreen,
				NULL,
				&gShell20LaunchEvent);
	if (!EFI_ERROR (Status))
	{
		Status = gBS->RegisterProtocolNotify(
				&EfiShell20InterfaceGuid,
				gShell20LaunchEvent,
				&Registration
				);
	}
}
//EIP 77400 Ends

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	_BootLaunchDevicePath
//
// Description:	function to launch the boot operation
//
// Input:		EFI_DEVICE_PATH_PROTOCOL *DevicePath, VOID *Options, UINTN OptionSize, BOOLEAN ValidBootOption
//
// Output:		status
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS _BootLaunchDevicePath( EFI_DEVICE_PATH_PROTOCOL *DevicePath, VOID *Options, UINTN OptionSize, BOOLEAN ValidBootOption )
{
    EFI_STATUS	Status;
    EFI_HANDLE	handle;
    BOOLEAN		FreeDevicePath = FALSE;
    EFI_EVENT	ReadyToBootEvent;
	EFI_GUID	Bootguid = AMITSE_EVENT_BEFORE_BOOT_GUID;
	EFI_GUID	AfterBootGuid = AMITSE_EVENT_AFTER_BOOT_GUID; //EIP-162197

    EFI_TPL CurrentTpl;
	TSE_POST_STATUS	BackupPostStatus;
#ifndef STANDALONE_APPLICATION
    EFI_CONSOLE_CONTROL_SCREEN_MODE ScreenMode = EfiConsoleControlScreenText;
#endif //STANDALONE_APPLICATION

	BackupPostStatus = gPostStatus; // Back it up and of boot fail restore it back
	gPostStatus = TSE_POST_STATUS_PROCEED_TO_BOOT;

#if defined(EFI_EVENT_SIGNAL_READY_TO_BOOT) && EFI_SPECIFICATION_VERSION<0x20000
    Status = gBS->CreateEvent(
                EFI_EVENT_SIGNAL_READY_TO_BOOT | EFI_EVENT_NOTIFY_SIGNAL_ALL,
                EFI_TPL_CALLBACK,
                NULL,
                NULL,
                &ReadyToBootEvent
                );
#else
    Status = TseEfiCreateEventReadyToBoot(
                EFI_TPL_CALLBACK,
                NULL,
                NULL,
                &ReadyToBootEvent
                );
#endif //EFI_EVENT_SIGNAL_READY_TO_BOOT

    if (EFI_ERROR(Status))
        return Status;

    gBS->SignalEvent( ReadyToBootEvent );
    gBS->CloseEvent( ReadyToBootEvent );
	EfiLibReportStatusCode(EFI_PROGRESS_CODE, DXE_READY_TO_BOOT,0,NULL,NULL);

    TSEIDEPasswordFreezeDevices();

    if( gBootFlow == BOOT_FLOW_CONDITION_FAST_BOOT)
    {
		// Call the Hook and do the FastBoot
		if ( BBSValidDevicePath( DevicePath ) )
		{
			BeforeLegacyBootLaunchHook();
			UninstallBgrtWrapper(); 							//EIP81830 Support to uninstall the BGRT on legacy boot
		}
		else
		{
			BeforeEfiBootLaunchHook();							//Clearing the logo for shell in fast boot also EIP102710
			if (NULL != gShellLaunchEvent)				
			{
				gBS->CloseEvent (gShellLaunchEvent);
				gShellLaunchEvent = NULL;
			}
			if (NULL != gShell20LaunchEvent)		//EIP158989			
			{
				gBS->CloseEvent (gShell20LaunchEvent);
				gShell20LaunchEvent = NULL;
			}
			//EIP 95518 : Validate the Gop before usage in all the possible cases and also get instance of Gop through notification
			//EIP136592, 141863, 123535 
			if ( ( NULL != gGOP ) && (CurrentScreenresolutionX && CurrentScreenresolutionY) && //If it has valid gGOP and resolution
				 ((CurrentScreenresolutionX != gGOP->Mode->Info->HorizontalResolution) || (CurrentScreenresolutionY != gGOP->Mode->Info->VerticalResolution)) //If current and quietboot resolution is different
				)//EIP-88430 
			{
				GOPSetScreenResolution(&CurrentScreenresolutionX, &CurrentScreenresolutionY);
				//gGOP->SetMode (gGOP, CurrentResolutionIndex); // To maintain graphics resolution 
			}
			EfiLibNamedEventSignal (&Bootguid);//EIP-112796 Signal  AMITSE_EVENT_BEFORE_BOOT_GUID for EfiBoot during Fastboot
			_RegisterShellGuid ();
		}
		// Performance measurement Pause
		PERF_END (0, AMITSE_TEXT("Boot"), NULL, 0);
		Status = FastBootLaunch();
		
		EfiLibNamedEventSignal (&AfterBootGuid);
		return Status;
    }
#if APTIO_4_00						//EIP94702 Useful for secure boot violation message box
	gMaxRows = MAX_ROWS;
	gMaxCols = MAX_COLS;
#endif
    if ( BBSValidDevicePath( DevicePath ) )			//EIP58954 Changing the mode only for csm
    {
#ifndef STANDALONE_APPLICATION
	// Fast Boot May want to boot without Console Control
		if (gConsoleControl != NULL)
		{
		    gConsoleControl->GetMode (gConsoleControl, &ScreenMode, NULL, NULL);
		    if (ScreenMode != EfiConsoleControlScreenText)
			{
		        gConsoleControl->SetMode (gConsoleControl, EfiConsoleControlScreenText);
				InvalidateStatusInBgrtWrapper ();					//Since mode changed invalidating status field in BGRT table. EIP 58954
			}
		}
#endif //STANDALONE_APPLICATION
        BeforeLegacyBootLaunchHook();
        UninstallBgrtWrapper(); 										//EIP81830 Support to uninstall the BGRT on legacy boot
        Status = CsmBBSLaunchDevicePath( DevicePath );
		if (EFI_ERROR (Status))
			LegacyBootFailHook(Status);//EIP 89377 : Support to LegacyBootFailHook() in TSE.
        AfterLegacyBootLaunchHook();
		gPostStatus = BackupPostStatus;
		
		EfiLibNamedEventSignal (&AfterBootGuid);
        return Status;
    }

    CurrentTpl = gBS->RaiseTPL( EFI_TPL_HIGH_LEVEL );
    gBS->RestoreTPL( EFI_TPL_APPLICATION );
    if (DevicePath->Type==MEDIA_DEVICE_PATH && DevicePath->SubType==MEDIA_HARDDRIVE_DP)
    {
        DevicePath = _DiscoverPartition(DevicePath);
        FreeDevicePath = TRUE;
    }
    Status = gBS->LoadImage (TRUE, gImageHandle, DevicePath, NULL, 0, &handle);
    if (EFI_ERROR (Status))
    {
        EFI_DEVICE_PATH_PROTOCOL    *TempDevicePath = DevicePath;
        EFI_STATUS RepairStatus = FALSE;
		UefiBootFailHook (Status);
        RepairStatus = CheckForDeviceNeedRepair (TempDevicePath);           //EIP 57661 support for UEFI specification v 2.3.1, p. 10.10.1 Driver health protocol
        if (!(EFI_ERROR (RepairStatus)))
        {
            Status = gBS->LoadImage (TRUE, gImageHandle, DevicePath, NULL, 0, &handle);
			if (EFI_ERROR (Status))
			{
				UefiBootFailHook (Status);
			}
        }
    }
    if (EFI_ERROR (Status))
    {
        //Try default behaviour
        EFI_DEVICE_PATH_PROTOCOL    *TempDevicePath = NULL;
        EFI_HANDLE                  DevHandle;
        EFI_HANDLE                  UsbDevHandle = NULL;
        EFI_BLOCK_IO_PROTOCOL       *BlkIo = NULL;
        VOID                        *Buffer = NULL;
        EFI_DEVICE_PATH_PROTOCOL    *FilePath = NULL;

        // Find a Simple File System protocol on the device path.
        TempDevicePath = DevicePath;

        UsbDevHandle = CheckDevSupShortFormPath (DevicePath);       ////EIP 64295 Support for section 3.1.2. booting from a short-form device path
        if (NULL != UsbDevHandle)
        {
            DevHandle = UsbDevHandle;
        }
        else
        {
            Status = gBS->LocateDevicePath (
                        &gEfiSimpleFileSystemProtocolGuid,
                        &TempDevicePath,
                        &DevHandle
                        );
        }
        if ((!EFI_ERROR (Status) && IsDevicePathEnd (TempDevicePath)) || (NULL != UsbDevHandle))
        {
            // Files are specified in the device path so try to
            // load the default removable media file name.

            FilePath = EfiFileDevicePath (DevHandle, gBootFileName);

			if (FilePath)
			{
                // Issue a dummy read to the device to check for media
                // change. When the removable media is changed, any Block
                // IO read/write will cause the BlockIo protocol be
                // reinstalled and EFI_MEDIA_CHANGED is returned. After
                // the Block IO protocol is reinstalled, subsequent Block
                // IO read/write will success.
                Status = gBS->HandleProtocol (
                                    DevHandle,
                                    &gEfiBlockIoProtocolGuid,
                                    (VOID **) &BlkIo
                                    );
                if (!EFI_ERROR (Status))
                {
                    Buffer = EfiLibAllocatePool (BlkIo->Media->BlockSize);
                    if (Buffer != NULL)
                    {
                        BlkIo->ReadBlocks (
                            BlkIo,
                            BlkIo->Media->MediaId,
                            0,
                            BlkIo->Media->BlockSize,
                            Buffer
                            );
                        MemFreePointer((VOID **)&Buffer);
                    }
                }

                Status = gBS->LoadImage (
                    TRUE,
                    gImageHandle,
                    FilePath,
                    NULL,
                    0,
                    &handle
                    );
				if (EFI_ERROR (Status))
				{
					UefiBootFailHook (Status);
				}
                MemFreePointer((VOID **) &FilePath);
            }
            else
            {
                Status = EFI_NOT_FOUND;
            }
        }
        else
        {
            Status = EFI_NOT_FOUND;
        }
    }


    if(ValidBootOption==TRUE)
        if (EFI_ERROR (Status)) // Report only if it Boot Option launch - Boot of loading Error!!!
    		EfiLibReportStatusCode(EFI_ERROR_CODE| EFI_ERROR_MAJOR, DXE_BOOT_OPTION_LOAD_ERROR,0,NULL,NULL);

    if (FreeDevicePath) gBS->FreePool(DevicePath);

    if ( ! EFI_ERROR( Status ) )
    {
		if (gST->ConOut != NULL)
        {
            gST->ConOut->EnableCursor (gST->ConOut, FALSE);         //Disabling bcoz Cursor appears on the WIN8 boot
        }
        _BootInstallLoadOptions( handle, Options, OptionSize );

        BeforeEfiBootLaunchHook();

		// Performance measurement Pause
		PERF_END (0, AMITSE_TEXT("Boot"), NULL, 0);

		//EIP 95518 : Validate the Gop before usage in all the possible cases and also get instance of Gop through notification
		//EIP136592, 141863
		if ( ( NULL != gGOP ) && (CurrentScreenresolutionX && CurrentScreenresolutionY) && //If it has valid gGOP and resolution
			 ((CurrentScreenresolutionX != gGOP->Mode->Info->HorizontalResolution) || (CurrentScreenresolutionY != gGOP->Mode->Info->VerticalResolution)) //If current and quietboot resolution is different
			)//EIP-88430 
		{
			GOPSetScreenResolution(&CurrentScreenresolutionX, &CurrentScreenresolutionY);
			//gGOP->SetMode (gGOP, CurrentResolutionIndex); // To maintain graphics resolution 
		}
		
		// Signal AMITSE_EVENT_BEFORE_BOOT_GUID Event;
		EfiLibNamedEventSignal (&Bootguid);
		_RegisterShellGuid ();						//EIP 77400 clearing the screen if its shell boot

		Status = gBS->StartImage( handle, NULL, NULL );
		if (NULL != gShellLaunchEvent)				//EIP 77400 Close the event if it is not the shell boot
		{
			gBS->CloseEvent (gShellLaunchEvent);
			gShellLaunchEvent = NULL;
		}
		if (NULL != gShell20LaunchEvent)			//EIP158989	
		{
			gBS->CloseEvent (gShell20LaunchEvent);
			gShell20LaunchEvent = NULL;
		}
		// Performance measurement continue
		PERF_START (0, AMITSE_TEXT("Boot"), NULL, 0);

        AfterEfiBootLaunchHook();
		EfiLibNamedEventSignal (&AfterBootGuid);
/*#if APTIO_4_00
		gMaxRows = MAX_ROWS;
		gMaxCols = MAX_COLS;
#endif //APTIO_4_00

		SetDesiredTextMode();			//EIP94702 - StyleInit will set the desired text mode*/
      ClearGrphxScreen ();
        if(EFI_ERROR( Status ))
        {
            // Report only if it is Boot Option launch - Starting the Boot option failed.!!!!
            if(ValidBootOption==TRUE)
    			EfiLibReportStatusCode(EFI_ERROR_CODE| EFI_ERROR_MAJOR, DXE_BOOT_OPTION_FAILED,0,NULL,NULL);

    		//Clear the flush buffer so that flush lines will actually
            //draw the complete screen again
        	MemSet(gFlushBuffer, sizeof(SCREEN_BUFFER), 0);

			if (gST->ConOut != NULL)
			{
	            //Disable cursor, set desired attributes and clear screen
	            gST->ConOut->EnableCursor( gST->ConOut, FALSE );
	            gST->ConOut->SetAttribute( gST->ConOut, EFI_BACKGROUND_BLACK | EFI_WHITE);
	            gST->ConOut->ClearScreen( gST->ConOut);
			}

            //Call flush lines to draw the whole screen again
            FlushLines( 0, gMaxRows - 1 );
//			DoRealFlushLines();			//EIP94702
        }
		else
		{
			InvalidateStatusInBgrtWrapper ();			//EIP93524 When win8 launched after successfull boot then BGRT table should be invalidated
		}
    }
	 gPostStatus = BackupPostStatus; /// restore the pre. Post status.

    if ( CurrentTpl > EFI_TPL_APPLICATION )
        gBS->RaiseTPL( CurrentTpl );

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	_DiscoverPartition
//
// Description:	function to launch the boot operation
//
// Input:		EFI_DEVICE_PATH_PROTOCOL *DevicePath
//
// Output:		device path
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_DEVICE_PATH_PROTOCOL* _DiscoverPartition(EFI_DEVICE_PATH_PROTOCOL *DevicePath)
{
    EFI_STATUS	Status;
    EFI_HANDLE	*Handle;
    UINTN		Count, i;

    EFI_DEVICE_PATH_PROTOCOL *FullDevicePath=NULL;
    HARDDRIVE_DEVICE_PATH* BootParitionDevicePath  = (HARDDRIVE_DEVICE_PATH*)DevicePath;

    //get list of available Block I/O devices
    Status = gBS->LocateHandleBuffer(ByProtocol,&gEfiBlockIoProtocolGuid,NULL,&Count,&Handle);
    if (EFI_ERROR(Status)) return NULL;

    for( i=0; i<Count; i++ )
    {
        EFI_BLOCK_IO_PROTOCOL		*BlockIo;
        EFI_DEVICE_PATH_PROTOCOL	*PartitionDevicePath, *TmpDevicePath;
        HARDDRIVE_DEVICE_PATH*		PartitionNode;

        Status = gBS->HandleProtocol(Handle[i],&gEfiBlockIoProtocolGuid,&BlockIo);
        if (EFI_ERROR(Status))
			continue;

        // if this is not partition, continue
        if (!BlockIo->Media->LogicalPartition)
			continue;

		Status = gBS->HandleProtocol(Handle[i],&gEfiDevicePathProtocolGuid,&PartitionDevicePath);
        if (EFI_ERROR(Status))
			continue;

        // Get last node of the device path. It should be partition node
        PartitionNode = (HARDDRIVE_DEVICE_PATH*)PartitionDevicePath;

        for( TmpDevicePath = PartitionDevicePath;
             !IsDevicePathEndType(TmpDevicePath);
             TmpDevicePath=NextDevicePathNode(TmpDevicePath) )
		{
			PartitionNode = (HARDDRIVE_DEVICE_PATH*)TmpDevicePath;
		}

        //Check if our partition matches Boot partition
        if (PartitionNode->Header.Type!=MEDIA_DEVICE_PATH || PartitionNode->Header.SubType!=MEDIA_HARDDRIVE_DP)
			continue;

        if ( PartitionNode->PartitionNumber==BootParitionDevicePath->PartitionNumber &&
             PartitionNode->SignatureType==BootParitionDevicePath->SignatureType &&
             !MemCmp(PartitionNode->Signature,BootParitionDevicePath->Signature,16) )
        {
            //Match found
            FullDevicePath = EfiAppendDevicePath(PartitionDevicePath,NextDevicePathNode(DevicePath));
            break;
        }
    }

    gBS->FreePool(Handle);
    return FullDevicePath;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	_BootInstallLoadOptions
//
// Description:	function to install the load options
//
// Input:		EFI_HANDLE handle, VOID *Options, UINTN OptionSize
//
// Output:		void
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID _BootInstallLoadOptions( EFI_HANDLE handle, VOID *Options, UINTN OptionSize )
{
    EFI_STATUS Status;
    EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;

    Status = gBS->HandleProtocol( handle, &gEfiLoadedImageProtocolGuid, &LoadedImage );
    if ( EFI_ERROR( Status ) )
        return;

    LoadedImage->LoadOptions = Options;
    LoadedImage->LoadOptionsSize = (UINT32)OptionSize;

    gCurrentBootHandle = handle;		//EIP70096 Exposing the Handle of the image that's being launched for boot, This will help other elinks to do the processing.

}

#if EFI_SPECIFICATION_VERSION<0x20000
static MEDIA_FW_VOL_FILEPATH_DEVICE_PATH  gFvFile =
{
  { MEDIA_DEVICE_PATH, MEDIA_FV_FILEPATH_DP, { sizeof(MEDIA_FW_VOL_FILEPATH_DEVICE_PATH), 0 } },
  MINI_SETUP_DATA_GUID
};
#endif //EFI_SPECIFICATION_VERSION

static EFI_DEVICE_PATH_PROTOCOL gEndDevicePath =
{
    END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { sizeof(EFI_DEVICE_PATH_PROTOCOL), 0 }
};

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	_BootBuildFVDevicePath
//
// Description:	function to build firmware volume device path protocol.
//
// Input:		UINT32 *index, EFI_GUID *guidPtr
//
// Output:		device path
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_DEVICE_PATH_PROTOCOL *_BootBuildFVDevicePath( UINT32 *index, EFI_GUID *guidPtr )
{
	EFI_STATUS  Status;
	EFI_HANDLE  *HandleBuffer;
	UINTN       Count;
	UINT32      i;
	EFI_GUID tempEfiFirmwareVolumeProtocolGuid;

    EFI_DEVICE_PATH_PROTOCOL *DevicePath, *FilePath = NULL;
#if EFI_SPECIFICATION_VERSION>=0x20000
    MEDIA_FW_VOL_FILEPATH_DEVICE_PATH gFvFile;
#endif //EFI_SPECIFICATION_VERSION

	if ( PISpecVersion() < 0x00010000 )//EIP-103540
		tempEfiFirmwareVolumeProtocolGuid = gEfiFirmwareVolumeProtocolGuid;
	else
		tempEfiFirmwareVolumeProtocolGuid = gEfiFirmwareVolume2ProtocolGuid;

    Status = gBS->LocateHandleBuffer(
            ByProtocol,
            &tempEfiFirmwareVolumeProtocolGuid,
            NULL,
            &Count,
            &HandleBuffer
            );

    if ( EFI_ERROR( Status ) )
    {
        *index = (UINT32)-1;
        return FilePath;
    }

#if EFI_SPECIFICATION_VERSION<0x20000
    MemCopy( (UINT8 *)&gFvFile + sizeof(EFI_DEVICE_PATH_PROTOCOL), guidPtr, sizeof(EFI_GUID) );
#endif //EFI_SPECIFICATION_VERSION

    for ( i = *index; i < (UINT32)Count; i++ )
    {
        Status = gBS->HandleProtocol( HandleBuffer[i], &gEfiDevicePathProtocolGuid, &DevicePath );
        if ( EFI_ERROR( Status ) )
            continue;

#if EFI_SPECIFICATION_VERSION>=0x20000
        EfiInitializeFwVolDevicepathNode (&gFvFile, guidPtr);
#endif //EFI_SPECIFICATION_VERSION

        FilePath = EfiAppendDevicePathNode( DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gFvFile );
        if ( FilePath != NULL )
        {
            *index = i + 1;
            break;
        }
    }

    if ( i == (UINT32)Count )
        *index = (UINT32)-1;

    MemFreePointer( (VOID **)&HandleBuffer );

    return FilePath;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	_BootBuildFileDevicePath
//
// Description:	function to build File device path protocol.
//
// Input:		UINT32 *index, CHAR16 *fileName
//
// Output:		device path
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_DEVICE_PATH_PROTOCOL *_BootBuildFileDevicePath( UINT32 *index, CHAR16 *fileName )
{
    EFI_STATUS  Status;
    EFI_HANDLE  *HandleBuffer;
    UINTN       Count;
    UINT32      i;

    EFI_DEVICE_PATH_PROTOCOL *FilePath = NULL;

    Status = gBS->LocateHandleBuffer(
            ByProtocol,
            &gEfiSimpleFileSystemProtocolGuid,
            NULL,
            &Count,
            &HandleBuffer
            );

    if ( EFI_ERROR( Status ) )
    {
        *index = (UINT32)-1;
        return FilePath;
    }

    for ( i = *index; i < (UINT32)Count; i++ )
    {
        FilePath = EfiFileDevicePath( HandleBuffer[i], fileName );
        if ( FilePath != NULL )
        {
            *index = i + 1;
            break;
        }
    }

    if ( i == (UINT32)Count )
        *index = (UINT32)-1;

    MemFreePointer( (VOID **)&HandleBuffer );
    return FilePath;
}

//EIP: 51671 START
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	IsBootDeviceEnabled
//
// Description:	Function to check the boot option status
//
// Input:		UINT16 value, BOOLEAN ShowAllBBSDev, BOOLEAN TseBootNowInBootOrde, BOOLEAN FromSetup
//
// Output:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN IsBootDeviceEnabled( UINT16 value, BOOLEAN ShowAllBBSDev, BOOLEAN TseBootNowInBootOrde, BOOLEAN FromSetup)
{

	UINT16	*BootOrder=NULL;
    UINTN	size = 0;
    UINTN	i,j,k;
    UINT16	count;

    BOOT_DATA *bootData;

	if((!ShowAllBBSDev) && (!TseBootNowInBootOrde))
		if(gBootData == NULL)
			return FALSE;
		else
			{
				bootData = &gBootData[value];
				if(BBSValidDevicePath(bootData->DevicePath) )
					return BootGetBBSOptionStatus(bootData, 0, FromSetup,ShowAllBBSDev);
				else
	      		return BootGetOptionStatus(bootData, FromSetup);//EIP: 51671 Getting the Boot option status when TSE_BOOT_NOW_IN_BOOT_ORDER and SETUP_SHOW_ALL_BBS_DEVICES is OFF.
			}
	if(TseBootNowInBootOrde)
	{
		BootOrder = HelperGetVariable(VARIABLE_ID_BOOT_ORDER, L"BootOrder", &gEfiGlobalVariableGuid, NULL, &size );
		if (NULL == BootOrder)		//Check for boot order else the system will hang
		{
			return TRUE;
		}
	    //Find the first disabled option
	    for ( i = 0; i < gBootOptionCount; i++ )
	    {
	        if ( DISABLED_BOOT_OPTION == BootOrder[i] )
	            break;
	    }

	    if(i<gBootOptionCount)
	    {
	        //There are disabled options replace them with valid options
	        for(j=0;j<gBootOptionCount;j++)
	        {
	            for(k=0;k<gBootOptionCount;k++)
	            {
	                if(BootOrder[k] == gBootData[j].Option)
	                    break;
	            }

	            if(k >= gBootOptionCount)
	            {
	                //gBootData[j].Option is not present in BootOrder; fill it
	                BootOrder[i] = gBootData[j].Option;
	                i++;
	            }
	        }
	    }
	}

	if(ShowAllBBSDev)
	{
	    count = 0;
	    for(i=0;i<gBootOptionCount;i++)
	    {
			if(TseBootNowInBootOrde) {
		        bootData = BootGetBootData(BootOrder[i]);
			}
			else {
	     		bootData = gBootData + i;
			}

	        if ( BBSValidDevicePath(bootData->DevicePath) )
	        {
	            if((value >= count) && (value < (count+bootData->LegacyDevCount)))
	            {
					if(TseBootNowInBootOrde)
		                MemFreePointer((VOID **) &BootOrder);
	                return BootGetBBSOptionStatus(bootData, value-count, FromSetup, ShowAllBBSDev);//EIP: 51671 Getting the Legacy Boot option status when SETUP_SHOW_ALL_BBS_DEVICES is ON.
	            }

	            count+=bootData->LegacyDevCount;
	        }
	        else {
	            if(value == count)
	            {
					if(TseBootNowInBootOrde)
		                MemFreePointer((VOID **) &BootOrder);
	                return BootGetOptionStatus(bootData, FromSetup);//EIP: 51671 Getting the Boot option status when SETUP_SHOW_ALL_BBS_DEVICES is ON.
	            }
	            count++;
	        }
	    }
 		return FALSE;
	}
	else {
	    bootData = BootGetBootData(BootOrder[value]);
	    MemFreePointer((VOID **) &BootOrder);
		if(BBSValidDevicePath(bootData->DevicePath) )
			return BootGetBBSOptionStatus(bootData, 0, FromSetup,ShowAllBBSDev);
		else
	      return BootGetOptionStatus(bootData, FromSetup);
	}
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootGetOptionStatus
//
// Description:	Function to check the boot option status in Boot Order
//
// Input:		BOOT_DATA *bootData, BOOLEAN FromSetup
//
// Output:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN BootGetOptionStatus(BOOT_DATA *bootData, BOOLEAN FromSetup)
{
	UINTN size = 0;
	UINTN i=0;
	UINT16 *buffer = NULL;

			if(0 == FromSetup)
			{
					if(bootData->Active & LOAD_OPTION_ACTIVE)
						return FALSE;
					else
						return TRUE;

			}

			buffer = EfiLibAllocateZeroPool(  gBootOptionCount * sizeof(UINT16));
			buffer = HelperGetVariable(VARIABLE_ID_BOOT_ORDER, L"BootOrder", &gEfiGlobalVariableGuid, NULL, &size );
         if (NULL == buffer)
         {
            return TRUE;
         }

			for(i=0 ; i<gBootOptionCount ; i++)
			{
				if(buffer[i] == bootData->Option)
				   break;
			}
			if(i == gBootOptionCount )
			{
				MemFreePointer((VOID **) &buffer);
					return TRUE;
			}
			else
			{
				MemFreePointer((VOID **) &buffer);
					return FALSE;
			}

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BootGetBBSOptionStatus
//
// Description:	Function to check the Legacy boot option status
//
// Input:		BOOT_DATA *bootData, BOOLEAN FromSetup, BOOLEAN ShowAllBBSDev
//
// Output:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN BootGetBBSOptionStatus(BOOT_DATA *bootData, UINT16 value, BOOLEAN FromSetup, BOOLEAN ShowAllBBSDev)
{
	   UINT32 offset=0;
		UINT32 i;
		UINT8 *pDevOrder;
		BBS_ORDER_TABLE	*pDev;
		UINTN size = 0;
		UINT16 *buf = NULL, *Tempbuf = NULL;

		if(0 == FromSetup)
		{
		   if(!BootGetOptionStatus(bootData, FromSetup))
		   {
	            offset = (UINT16)bootData->LegacyEntryOffset;

			  		pDevOrder = HelperGetVariable(VARIABLE_ID_BBS_ORDER,L"LegacyDevOrder", &gLegacyDevGuid, NULL, &size);

	         	pDev = (BBS_ORDER_TABLE *)(pDevOrder + offset);

					if ( (pDev->Length >= size) || (0 == pDev->Length) ) //EIP-120011
						return TRUE;

              if(DISABLED_BOOT_OPTION == pDev->Data[value])
					{
						 MemFreePointer((VOID **) &pDevOrder);
  					    return  TRUE;
					}
    		  		else
					{
						 MemFreePointer((VOID **) &pDevOrder);
                   return  FALSE;
					}
		   }
		   else
		       return TRUE;
        }
		if(!BootGetOptionStatus(bootData, FromSetup))
		{

			size=0;
		 	offset = (UINT16)bootData->LegacyEntryOffset;
			pDevOrder = HelperGetVariable(VARIABLE_ID_BBS_ORDER,L"LegacyDevOrder", &gLegacyDevGuid, NULL, &size);
            if (NULL == pDevOrder)
            {
              return TRUE;
            }

			pDev = (BBS_ORDER_TABLE *)(pDevOrder + offset);

			if ( (pDev->Length >= size) || (0 == pDev->Length) ) //EIP-120011
				return TRUE;

			if(!ShowAllBBSDev)
			{
					if(DISABLED_BOOT_OPTION == pDev->Data[value])
					{
						MemFreePointer((VOID **) &pDevOrder);
  					   	return  TRUE;
    				}
					else
					{
						MemFreePointer((VOID **) &pDevOrder);
                  	return  FALSE;
					}
			}
			
	      buf = EfiLibAllocateZeroPool(  pDev->Length - sizeof(UINT16));
			
         if (NULL == buf)		//EIP117338
         {
            return FALSE;
         }
			
         Tempbuf = buf;
			MemCopy( buf, &pDev->Data, pDev->Length - sizeof(UINT16) );
			
			for(i=0; i < bootData->LegacyDevCount ;i++)
			{
            if(*buf == bootData->OrderList[value].Index)
               break;
            buf++;
			}
			
         MemFreePointer((VOID **) &pDevOrder);		//EIP117338
         MemFreePointer((VOID **) &Tempbuf);
			
			if(i ==  bootData->LegacyDevCount)
			{
            return  TRUE;
			}
			else
			{
            return  FALSE;
			}
		}
		else
			return TRUE;
}
//EIP: 51671 END
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	LoadOptionhidden
//
// Description:	Function to check the Boot option status if gLoadOptionhidden token is Enabled
//
// Input:		UINT16 value
//
// Output:		BOOLEAN
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN LoadOptionhidden (UINT16 value, BOOLEAN Option)
{
	if (BOOT_ORDER_OPTION == Option)
	{
    	if (gBootData [value].Active & LOAD_OPTION_HIDDEN)
			return TRUE;
	}
	else if (DRIVER_ORDER_OPTION == Option)				//EIP70421 & 70422
	{
		if (gDriverData [value].Active & LOAD_OPTION_HIDDEN)
			return TRUE;
	}	
	return FALSE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	CheckHiddenforBootDriverOption
//
// Description:	Check whether the Boot/Driver option has hidden property
//
// Input:		UINT16, BOOLEAN
//
// Output:		BOOLEAN
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN CheckHiddenforBootDriverOption (UINT16 Option, BOOLEAN HiddenOption)
{
	UINTN i = 0;
	if (gLoadOptionHidden)
	{	
		if (BOOT_ORDER_OPTION == HiddenOption)
		{
			for (i = 0 ;i < gBootOptionCount ;i++)
			{
				if (gBootData [i].Option == Option)
				{
					if (gBootData [i].Active & LOAD_OPTION_HIDDEN)
					{
						return TRUE;
					}
					break;
				}
			}
		}
		else if (DRIVER_ORDER_OPTION == HiddenOption)
		{
			for (i = 0 ;i < gDriverOptionCount ;i++)
			{
				if (gDriverData [i].Option == Option)
				{
					if (gDriverData [i].Active & LOAD_OPTION_HIDDEN)
					{
						return TRUE;
					}
					break;
				}
			}
		}
	}
	return FALSE;
}

//EIP: 62631 Start
//<AMI_PHDR_START>
//-----------------------------------------------------------------------------------------------
// Procedure:	CheckBootOptionMatch
//
// Description:	Checks the input boot option matches with any of the boot option in the system
//
// Input:		UINT16 = Boot option to which match to be find
//
// Output:		UINT32 = Returns 0 if no match found
//                     = 32 bit CRC value of boot option if match found
//
//------------------------------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT32 CheckBootOptionMatch (UINT16 BootOption)
{
    UINTN	iIndex = 0;
    CHAR16  BootOptionName [9];             //Bootxxxx + 1 NULL char
    UINT32	*LoadOptions = NULL;        //Using 32 bit ptr bcoz to find CRC32
    UINTN	LoadOptionSize = 0;
    UINT32  CRC32 = 0;

    SPrint (BootOptionName, sizeof (BootOptionName), L"Boot%04X", BootOption);
    LoadOptions = VarGetNvramName (BootOptionName, &gEfiGlobalVariableGuid, NULL, &LoadOptionSize);     //Getting boot options
    if ((NULL == LoadOptions) || (0 == LoadOptionSize))
    {
        return CRC32;           //returning 0
    }
   gBS->CalculateCrc32 ((UINT8 *)LoadOptions, LoadOptionSize, &CRC32);
   return CRC32;
}

//<AMI_PHDR_START>
//--------------------------------------------------------------------------------------
// Procedure:	SetBootOptionSupportVariable
//
// Description:	Function to set the BootOptionSupport variable
//
// Input:		UINT32 = Capabilities for the BootOptionSupport variable
//
// Output:		VOID
//
//--------------------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID SetBootOptionSupportVariable (UINT32 BootManCapabilities)
{
    CHAR16 VariableName [] = L"BootOptionSupport";
    UINT32 Attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
    UINTN DataSize = 0;
    VOID *Data;
    UINT32 SetData = 0;

    Data = VarGetNvramName (VariableName, &gEfiGlobalVariableGuid, &Attributes, &DataSize);     //If the variable exists use its attribute to set it
    if (NULL != Data)
    {
        SetData = *((UINT32 *)Data);
    }
    SetData |= BootManCapabilities;
    DataSize = sizeof (UINT32);         //Sizeof BootOptionSupport variable is UINT32
    VarSetNvramName (VariableName, &gEfiGlobalVariableGuid, Attributes, (VOID *)&SetData, DataSize);
}
//EIP: 62631 End

//EIP 64295 Start
//<AMI_PHDR_START>
//--------------------------------------------------------------------------------------
// Procedure:	CheckDevSupShortFormPath
//
// Description:  Matches the device path with USB class device path (Table 60) and returns
//                  the corresponding USB's file system handle
//
// Input:   EFI_DEVICE_PATH_PROTOCOL * -> Device path for the boot option
//
// Output:  EFI_HANDLE -> Handle for the file system
//
//--------------------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_HANDLE CheckDevSupShortFormPath (EFI_DEVICE_PATH_PROTOCOL *DevicePath)
{
	UINTN       NumHandles = 0;
	UINTN       iIndex = 0;
	CHAR16		*USBString = NULL;
	CHAR16		*USBDevPathString = NULL;
	UINT16     	*LangIDTable;
	UINT16		TableSize = 0;
	EFI_STATUS  Status;
	EFI_HANDLE  *UsbIoHandles = NULL;
    EFI_USB_IO_PROTOCOL         *UsbIoProtocolInstance = NULL;
    EFI_USB_DEVICE_DESCRIPTOR   DeviceDescriptor;
    USB_CLASS_DEVICE_PATH       *UsbClassDevPath = NULL;
	USB_WWID_DEVICE_PATH		*UsbWwidDevPath = NULL;
    EFI_DEVICE_PATH_PROTOCOL    *TempDevPath = DevicePath;
	
    if ((MESSAGING_DEVICE_PATH == TempDevPath->Type) && (MSG_USB_CLASS_DP == TempDevPath->SubType))     //Check for USB Device Path Class. type = 3 and subtype = 0xf
    {
		UsbClassDevPath = (USB_CLASS_DEVICE_PATH *)TempDevPath;
    }
    else if ((MESSAGING_DEVICE_PATH == TempDevPath->Type) && (MSG_USB_WWID_CLASS_DP == TempDevPath->SubType))
    {
		UsbWwidDevPath = (USB_WWID_DEVICE_PATH *)TempDevPath;
		USBDevPathString = (CHAR16 *)((UINT8 *)UsbWwidDevPath + sizeof (USB_WWID_DEVICE_PATH));		//String will be present at the end of the WWID device path
    }
	else
	{
		return NULL;
	}
	Status = gBS->LocateHandleBuffer (                  //To match with USB Device Path Class
				ByProtocol,
				&gEfiUsbIoProtocolGuid,
				NULL,
				&NumHandles,
				&UsbIoHandles
				);
	if (EFI_ERROR (Status))
	{
        return NULL;
	}
	for (iIndex = 0; iIndex < NumHandles; iIndex ++)
	{
		Status = gBS->HandleProtocol (UsbIoHandles [iIndex], &gEfiUsbIoProtocolGuid, &UsbIoProtocolInstance);
		if (EFI_ERROR (Status))
    	{
            continue;
        }
        Status = UsbIoProtocolInstance->UsbGetDeviceDescriptor (UsbIoProtocolInstance, &DeviceDescriptor);
        if (EFI_ERROR (Status))
    	{
            continue;
        }
		if (UsbWwidDevPath)
		{
			Status = UsbIoProtocolInstance->UsbGetSupportedLanguages (UsbIoProtocolInstance, &LangIDTable, &TableSize);
			if (!EFI_ERROR (Status) && TableSize)
			{
				Status = UsbIoProtocolInstance->UsbGetStringDescriptor (UsbIoProtocolInstance, LangIDTable [0], DeviceDescriptor.StrSerialNumber, &USBString);		//LangIDTable [0], getting default as English
				if (EFI_ERROR (Status))
				{
					USBString = NULL;  		//Explicitly making as NULL
				}
			}
			if ((NULL != USBString) ^ (0 != EfiStrLen (USBDevPathString)))		//If serial number string present in device path and not in descriptor then try for other device and vice versa too
			{																			//If device path and descriptor not has the string then proceed
				continue;
			}
		}
		if (UsbClassDevPath?
		(			//Check for USB Class device path
        ((UsbClassDevPath->VendorId == DeviceDescriptor.IdVendor) || (0xFFFF == UsbClassDevPath->VendorId)) &&      //If values are 0xF's then dont consider that option
        ((UsbClassDevPath->ProductId == DeviceDescriptor.IdProduct) || (0xFFFF == UsbClassDevPath->ProductId)) &&
        ((UsbClassDevPath->DeviceClass == DeviceDescriptor.DeviceClass) || (0xFF == UsbClassDevPath->DeviceClass)) &&
        ((UsbClassDevPath->DeviceSubClass == DeviceDescriptor.DeviceSubClass) || (0xFF == UsbClassDevPath->DeviceSubClass)) &&
        ((UsbClassDevPath->DeviceProtocol == DeviceDescriptor.DeviceProtocol) || (0xFF == UsbClassDevPath->DeviceProtocol))
        ):
		(			//Check for USB WWID device path
		(UsbWwidDevPath->VendorId == DeviceDescriptor.IdVendor) &&
		(UsbWwidDevPath->ProductId == DeviceDescriptor.IdProduct) &&
		(USBString ? (!(EfiStrCmp (USBDevPathString, USBString))):1)			//String number of USB might not be filled in some case in such conditions will take it as TRUE
		)
		)
        {
            UINTN       Count;
        	EFI_GUID    **ppGuid;
            UINTN       jIndex = 0;
            UINTN       kIndex = 0;

            Status = gBS->ProtocolsPerHandle (UsbIoHandles [iIndex], &ppGuid, &Count);
            if (EFI_ERROR (Status))
            {
                continue;
            }
            for (jIndex = 0; jIndex < Count; jIndex ++)
	        {
                if (!guidcmp (ppGuid [jIndex], &gEfiDiskIoProtocolGuid))
                {
                    EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *pInfo;
                    UINTN InfoCount = 0;
                    VOID  *FilsSystemInstance = NULL;
                    EFI_HANDLE handle = NULL;
                    EFI_DEVICE_PATH_PROTOCOL    *FilePath = NULL;

                    Status = gBS->OpenProtocolInformation (UsbIoHandles [iIndex], ppGuid [jIndex], &pInfo, &InfoCount);
                    if (EFI_ERROR (Status))
                    {
                        continue;
                    }
                    for (kIndex = 0; kIndex < InfoCount; kIndex ++)
                    {
                        Status = gBS->HandleProtocol (pInfo [kIndex].ControllerHandle, &gEfiSimpleFileSystemProtocolGuid, &FilsSystemInstance);
                        if (EFI_ERROR (Status))
                        {
                            continue;
                        }
                        FilePath = EfiFileDevicePath (pInfo [kIndex].ControllerHandle, gBootFileName);
                        if (FilePath)
                        {
                            Status = gBS->LoadImage (                       //Ensuring the image can load
                                                       TRUE,
                                                        gImageHandle,
                                                        FilePath,
                                                        NULL,
                                                        0,
                                                        &handle
                                                    );
                            MemFreePointer((VOID **) &FilePath);
                            if (!EFI_ERROR (Status))
                            {
								if (USBString)
								{
									MemFreePointer ((VOID **)&USBString);
								}
                                MemFreePointer ((VOID **)&UsbIoHandles);
                                return pInfo [kIndex].ControllerHandle;
                            }

                        }
                    }
                    if (InfoCount)
					{
						MemFreePointer ((VOID **)&pInfo);
                	}
                }
            }
        }
		if (USBString)
		{
			MemFreePointer ((VOID **)&USBString);
		}
	}
	MemFreePointer ((VOID **)&UsbIoHandles);
    return NULL;
}
//EIP 64295 End

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2013, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**     5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093     **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************