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
|
/** @file
BDS Lib functions which relate with create or process the boot option.
Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "InternalBdsLib.h"
#include "String.h"
BOOLEAN mEnumBootDevice = FALSE;
EFI_HII_HANDLE gBdsLibStringPackHandle = NULL;
///
/// This GUID is used for an EFI Variable that stores the front device pathes
/// for a partial device path that starts with the HD node.
///
EFI_GUID mHdBootVariablePrivateGuid = { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };
///
/// This GUID is used for register UNI string.
///
EFI_GUID mBdsLibStringPackGuid = { 0x3b4d9b23, 0x95ac, 0x44f6, { 0x9f, 0xcd, 0xe, 0x95, 0x94, 0x58, 0x6c, 0x72 } };
///
/// This GUID is used for Set/Get platform language into/from variable at last time enumeration to ensure the enumeration will
/// only execute once.
///
EFI_GUID mBdsLibLastLangGuid = { 0xe8c545b, 0xa2ee, 0x470d, { 0x8e, 0x26, 0xbd, 0xa1, 0xa1, 0x3c, 0xa, 0xa3 } };
/**
The constructor function register UNI strings into imageHandle.
It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
@param ImageHandle The firmware allocated handle for the EFI image.
@param SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The constructor successfully added string package.
@retval Other value The constructor can't add string package.
**/
EFI_STATUS
EFIAPI
GenericBdsLibConstructor (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
gBdsLibStringPackHandle = HiiAddPackages (
&mBdsLibStringPackGuid,
&ImageHandle,
GenericBdsLibStrings,
NULL
);
ASSERT (gBdsLibStringPackHandle != NULL);
return EFI_SUCCESS;
}
/**
Boot the legacy system with the boot option
@param Option The legacy boot option which have BBS device path
@retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
legacy boot.
@retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
**/
EFI_STATUS
BdsLibDoLegacyBoot (
IN BDS_COMMON_OPTION *Option
)
{
EFI_STATUS Status;
EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);
if (EFI_ERROR (Status)) {
//
// If no LegacyBios protocol we do not support legacy boot
//
return EFI_UNSUPPORTED;
}
//
// Notes: if we separate the int 19, then we don't need to refresh BBS
//
BdsRefreshBbsTableForBoot (Option);
//
// Write boot to OS performance data for legacy boot.
//
PERF_CODE (
WriteBootToOsPerformanceData ();
);
DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Legacy Boot: %S\n", Option->Description));
return LegacyBios->LegacyBoot (
LegacyBios,
(BBS_BBS_DEVICE_PATH *) Option->DevicePath,
Option->LoadOptionsSize,
Option->LoadOptions
);
}
/**
Internal function to check if the input boot option is a valid EFI NV Boot####.
@param OptionToCheck Boot option to be checked.
@retval TRUE This boot option matches a valid EFI NV Boot####.
@retval FALSE If not.
**/
BOOLEAN
IsBootOptionValidNVVarialbe (
IN BDS_COMMON_OPTION *OptionToCheck
)
{
LIST_ENTRY TempList;
BDS_COMMON_OPTION *BootOption;
BOOLEAN Valid;
CHAR16 OptionName[20];
Valid = FALSE;
InitializeListHead (&TempList);
UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionToCheck->BootCurrent);
BootOption = BdsLibVariableToOption (&TempList, OptionName);
if (BootOption == NULL) {
return FALSE;
}
//
// If the Boot Option Number and Device Path matches, OptionToCheck matches a
// valid EFI NV Boot####.
//
if ((OptionToCheck->BootCurrent == BootOption->BootCurrent) &&
(CompareMem (OptionToCheck->DevicePath, BootOption->DevicePath, GetDevicePathSize (OptionToCheck->DevicePath)) == 0))
{
Valid = TRUE;
}
FreePool (BootOption);
return Valid;
}
/**
Process the boot option follow the UEFI specification and
special treat the legacy boot option with BBS_DEVICE_PATH.
@param Option The boot option need to be processed
@param DevicePath The device path which describe where to load the
boot image or the legacy BBS device path to boot
the legacy OS
@param ExitDataSize The size of exit data.
@param ExitData Data returned when Boot image failed.
@retval EFI_SUCCESS Boot from the input boot option successfully.
@retval EFI_NOT_FOUND If the Device Path is not found in the system
**/
EFI_STATUS
EFIAPI
BdsLibBootViaBootOption (
IN BDS_COMMON_OPTION *Option,
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
OUT UINTN *ExitDataSize,
OUT CHAR16 **ExitData OPTIONAL
)
{
EFI_STATUS Status;
EFI_HANDLE Handle;
EFI_HANDLE ImageHandle;
EFI_DEVICE_PATH_PROTOCOL *FilePath;
EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;
EFI_DEVICE_PATH_PROTOCOL *WorkingDevicePath;
EFI_ACPI_S3_SAVE_PROTOCOL *AcpiS3Save;
LIST_ENTRY TempBootLists;
//
// Record the performance data for End of BDS
//
PERF_END(NULL, "BDS", NULL, 0);
*ExitDataSize = 0;
*ExitData = NULL;
//
// Notes: this code can be remove after the s3 script table
// hook on the event EVT_SIGNAL_READY_TO_BOOT or
// EVT_SIGNAL_LEGACY_BOOT
//
Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);
if (!EFI_ERROR (Status)) {
AcpiS3Save->S3Save (AcpiS3Save, NULL);
}
//
// If it's Device Path that starts with a hard drive path, append it with the front part to compose a
// full device path
//
WorkingDevicePath = NULL;
if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&
(DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {
WorkingDevicePath = BdsExpandPartitionPartialDevicePathToFull (
(HARDDRIVE_DEVICE_PATH *)DevicePath
);
if (WorkingDevicePath != NULL) {
DevicePath = WorkingDevicePath;
}
}
//
// Signal the EVT_SIGNAL_READY_TO_BOOT event
//
EfiSignalEventReadyToBoot();
//
// Set Boot Current
//
if (IsBootOptionValidNVVarialbe (Option)) {
//
// For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
// In this case, "BootCurrent" is not created.
// Only create the BootCurrent variable when it points to a valid Boot#### variable.
//
gRT->SetVariable (
L"BootCurrent",
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
sizeof (UINT16),
&Option->BootCurrent
);
}
ASSERT (Option->DevicePath != NULL);
if ((DevicePathType (Option->DevicePath) == BBS_DEVICE_PATH) &&
(DevicePathSubType (Option->DevicePath) == BBS_BBS_DP)
) {
//
// Check to see if we should legacy BOOT. If yes then do the legacy boot
//
return BdsLibDoLegacyBoot (Option);
}
//
// If the boot option point to Internal FV shell, make sure it is valid
//
Status = BdsLibUpdateFvFileDevicePath (&DevicePath, PcdGetPtr(PcdShellFile));
if (!EFI_ERROR(Status)) {
if (Option->DevicePath != NULL) {
FreePool(Option->DevicePath);
}
Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));
ASSERT(Option->DevicePath != NULL);
CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));
//
// Update the shell boot option
//
InitializeListHead (&TempBootLists);
BdsLibRegisterNewOption (&TempBootLists, DevicePath, L"EFI Internal Shell", L"BootOrder");
//
// free the temporary device path created by BdsLibUpdateFvFileDevicePath()
//
FreePool (DevicePath);
DevicePath = Option->DevicePath;
}
DEBUG_CODE_BEGIN();
if (Option->Description == NULL) {
DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting from unknown device path\n"));
} else {
DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));
}
DEBUG_CODE_END();
Status = gBS->LoadImage (
TRUE,
mBdsImageHandle,
DevicePath,
NULL,
0,
&ImageHandle
);
//
// If we didn't find an image directly, we need to try as if it is a removable device boot option
// and load the image according to the default boot behavior for removable device.
//
if (EFI_ERROR (Status)) {
//
// check if there is a bootable removable media could be found in this device path ,
// and get the bootable media handle
//
Handle = BdsLibGetBootableHandle(DevicePath);
if (Handle == NULL) {
goto Done;
}
//
// Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
// machinename is ia32, ia64, x64, ...
//
FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);
if (FilePath != NULL) {
Status = gBS->LoadImage (
TRUE,
mBdsImageHandle,
FilePath,
NULL,
0,
&ImageHandle
);
if (EFI_ERROR (Status)) {
//
// The DevicePath failed, and it's not a valid
// removable media device.
//
goto Done;
}
}
}
if (EFI_ERROR (Status)) {
//
// It there is any error from the Boot attempt exit now.
//
goto Done;
}
//
// Provide the image with it's load options
//
Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);
ASSERT_EFI_ERROR (Status);
if (Option->LoadOptionsSize != 0) {
ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;
ImageInfo->LoadOptions = Option->LoadOptions;
}
//
// Before calling the image, enable the Watchdog Timer for
// the 5 Minute period
//
gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);
//
// Write boot to OS performance data for UEFI boot
//
PERF_CODE (
WriteBootToOsPerformanceData ();
);
Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);
DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));
//
// Clear the Watchdog Timer after the image returns
//
gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);
Done:
//
// Clear Boot Current
//
gRT->SetVariable (
L"BootCurrent",
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
0,
&Option->BootCurrent
);
return Status;
}
/**
Expand a device path that starts with a hard drive media device path node to be a
full device path that includes the full hardware path to the device. We need
to do this so it can be booted. As an optimization the front match (the part point
to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
so a connect all is not required on every boot. All successful history device path
which point to partition node (the front part) will be saved.
@param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
drive media device path.
@return A Pointer to the full device path or NULL if a valid Hard Drive devic path
cannot be found.
**/
EFI_DEVICE_PATH_PROTOCOL *
EFIAPI
BdsExpandPartitionPartialDevicePathToFull (
IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath
)
{
EFI_STATUS Status;
UINTN BlockIoHandleCount;
EFI_HANDLE *BlockIoBuffer;
EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;
EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
UINTN Index;
UINTN InstanceNum;
EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;
EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;
UINTN CachedDevicePathSize;
BOOLEAN DeviceExist;
BOOLEAN NeedAdjust;
EFI_DEVICE_PATH_PROTOCOL *Instance;
UINTN Size;
FullDevicePath = NULL;
//
// Check if there is prestore 'HDDP' variable.
// If exist, search the front path which point to partition node in the variable instants.
// If fail to find or 'HDDP' not exist, reconnect all and search in all system
//
CachedDevicePath = BdsLibGetVariableAndSize (
L"HDDP",
&mHdBootVariablePrivateGuid,
&CachedDevicePathSize
);
if (CachedDevicePath != NULL) {
TempNewDevicePath = CachedDevicePath;
DeviceExist = FALSE;
NeedAdjust = FALSE;
do {
//
// Check every instance of the variable
// First, check whether the instance contain the partition node, which is needed for distinguishing multi
// partial partition boot option. Second, check whether the instance could be connected.
//
Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);
if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {
//
// Connect the device path instance, the device path point to hard drive media device path node
// e.g. ACPI() /PCI()/ATA()/Partition()
//
Status = BdsLibConnectDevicePath (Instance);
if (!EFI_ERROR (Status)) {
DeviceExist = TRUE;
break;
}
}
//
// Come here means the first instance is not matched
//
NeedAdjust = TRUE;
FreePool(Instance);
} while (TempNewDevicePath != NULL);
if (DeviceExist) {
//
// Find the matched device path.
// Append the file path information from the boot option and return the fully expanded device path.
//
DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);
FullDevicePath = AppendDevicePath (Instance, DevicePath);
//
// Adjust the 'HDDP' instances sequence if the matched one is not first one.
//
if (NeedAdjust) {
//
// First delete the matched instance.
//
TempNewDevicePath = CachedDevicePath;
CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );
FreePool (TempNewDevicePath);
//
// Second, append the remaining path after the matched instance
//
TempNewDevicePath = CachedDevicePath;
CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );
FreePool (TempNewDevicePath);
//
// Save the matching Device Path so we don't need to do a connect all next time
//
Status = gRT->SetVariable (
L"HDDP",
&mHdBootVariablePrivateGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
GetDevicePathSize (CachedDevicePath),
CachedDevicePath
);
}
FreePool (Instance);
FreePool (CachedDevicePath);
return FullDevicePath;
}
}
//
// If we get here we fail to find or 'HDDP' not exist, and now we need
// to search all devices in the system for a matched partition
//
BdsLibConnectAllDriversToAllControllers ();
Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);
if (EFI_ERROR (Status) || BlockIoHandleCount == 0 || BlockIoBuffer == NULL) {
//
// If there was an error or there are no device handles that support
// the BLOCK_IO Protocol, then return.
//
return NULL;
}
//
// Loop through all the device handles that support the BLOCK_IO Protocol
//
for (Index = 0; Index < BlockIoHandleCount; Index++) {
Status = gBS->HandleProtocol (BlockIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &BlockIoDevicePath);
if (EFI_ERROR (Status) || BlockIoDevicePath == NULL) {
continue;
}
if (MatchPartitionDevicePathNode (BlockIoDevicePath, HardDriveDevicePath)) {
//
// Find the matched partition device path
//
DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);
FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);
//
// Save the matched partition device path in 'HDDP' variable
//
if (CachedDevicePath != NULL) {
//
// Save the matched partition device path as first instance of 'HDDP' variable
//
if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {
TempNewDevicePath = CachedDevicePath;
CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);
FreePool(TempNewDevicePath);
TempNewDevicePath = CachedDevicePath;
CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);
if (TempNewDevicePath != NULL) {
FreePool(TempNewDevicePath);
}
} else {
TempNewDevicePath = CachedDevicePath;
CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);
FreePool(TempNewDevicePath);
}
//
// Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
// If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
//
InstanceNum = 0;
ASSERT (CachedDevicePath != NULL);
TempNewDevicePath = CachedDevicePath;
while (!IsDevicePathEnd (TempNewDevicePath)) {
TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);
//
// Parse one instance
//
while (!IsDevicePathEndType (TempNewDevicePath)) {
TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);
}
InstanceNum++;
//
// If the CachedDevicePath variable contain too much instance, only remain 12 instances.
//
if (InstanceNum >= 12) {
SetDevicePathEndNode (TempNewDevicePath);
break;
}
}
} else {
CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);
}
//
// Save the matching Device Path so we don't need to do a connect all next time
//
Status = gRT->SetVariable (
L"HDDP",
&mHdBootVariablePrivateGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
GetDevicePathSize (CachedDevicePath),
CachedDevicePath
);
break;
}
}
if (CachedDevicePath != NULL) {
FreePool (CachedDevicePath);
}
if (BlockIoBuffer != NULL) {
FreePool (BlockIoBuffer);
}
return FullDevicePath;
}
/**
Check whether there is a instance in BlockIoDevicePath, which contain multi device path
instances, has the same partition node with HardDriveDevicePath device path
@param BlockIoDevicePath Multi device path instances which need to check
@param HardDriveDevicePath A device path which starts with a hard drive media
device path.
@retval TRUE There is a matched device path instance.
@retval FALSE There is no matched device path instance.
**/
BOOLEAN
EFIAPI
MatchPartitionDevicePathNode (
IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,
IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath
)
{
HARDDRIVE_DEVICE_PATH *TmpHdPath;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
BOOLEAN Match;
EFI_DEVICE_PATH_PROTOCOL *BlockIoHdDevicePathNode;
if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {
return FALSE;
}
//
// Make PreviousDevicePath == the device path node before the end node
//
DevicePath = BlockIoDevicePath;
BlockIoHdDevicePathNode = NULL;
//
// find the partition device path node
//
while (!IsDevicePathEnd (DevicePath)) {
if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&
(DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)
) {
BlockIoHdDevicePathNode = DevicePath;
break;
}
DevicePath = NextDevicePathNode (DevicePath);
}
if (BlockIoHdDevicePathNode == NULL) {
return FALSE;
}
//
// See if the harddrive device path in blockio matches the orig Hard Drive Node
//
TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;
Match = FALSE;
//
// Check for the match
//
if ((TmpHdPath->MBRType == HardDriveDevicePath->MBRType) &&
(TmpHdPath->SignatureType == HardDriveDevicePath->SignatureType)) {
switch (TmpHdPath->SignatureType) {
case SIGNATURE_TYPE_GUID:
Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)HardDriveDevicePath->Signature);
break;
case SIGNATURE_TYPE_MBR:
Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == ReadUnaligned32((UINT32 *)(&(HardDriveDevicePath->Signature[0]))));
break;
default:
Match = FALSE;
break;
}
}
return Match;
}
/**
Delete the boot option associated with the handle passed in.
@param Handle The handle which present the device path to create
boot option
@retval EFI_SUCCESS Delete the boot option success
@retval EFI_NOT_FOUND If the Device Path is not found in the system
@retval EFI_OUT_OF_RESOURCES Lack of memory resource
@retval Other Error return value from SetVariable()
**/
EFI_STATUS
BdsLibDeleteOptionFromHandle (
IN EFI_HANDLE Handle
)
{
UINT16 *BootOrder;
UINT8 *BootOptionVar;
UINTN BootOrderSize;
UINTN BootOptionSize;
EFI_STATUS Status;
UINTN Index;
UINT16 BootOption[BOOT_OPTION_MAX_CHAR];
UINTN DevicePathSize;
UINTN OptionDevicePathSize;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;
UINT8 *TempPtr;
Status = EFI_SUCCESS;
BootOrder = NULL;
BootOrderSize = 0;
//
// Check "BootOrder" variable, if no, means there is no any boot order.
//
BootOrder = BdsLibGetVariableAndSize (
L"BootOrder",
&gEfiGlobalVariableGuid,
&BootOrderSize
);
if (BootOrder == NULL) {
return EFI_NOT_FOUND;
}
//
// Convert device handle to device path protocol instance
//
DevicePath = DevicePathFromHandle (Handle);
if (DevicePath == NULL) {
return EFI_NOT_FOUND;
}
DevicePathSize = GetDevicePathSize (DevicePath);
//
// Loop all boot order variable and find the matching device path
//
Index = 0;
while (Index < BootOrderSize / sizeof (UINT16)) {
UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);
BootOptionVar = BdsLibGetVariableAndSize (
BootOption,
&gEfiGlobalVariableGuid,
&BootOptionSize
);
if (BootOptionVar == NULL) {
FreePool (BootOrder);
return EFI_OUT_OF_RESOURCES;
}
TempPtr = BootOptionVar;
TempPtr += sizeof (UINT32) + sizeof (UINT16);
TempPtr += StrSize ((CHAR16 *) TempPtr);
OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);
//
// Check whether the device path match
//
if ((OptionDevicePathSize == DevicePathSize) &&
(CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {
BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);
FreePool (BootOptionVar);
break;
}
FreePool (BootOptionVar);
Index++;
}
//
// Adjust number of boot option for "BootOrder" variable.
//
Status = gRT->SetVariable (
L"BootOrder",
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
BootOrderSize,
BootOrder
);
FreePool (BootOrder);
return Status;
}
/**
Delete all invalid EFI boot options.
@retval EFI_SUCCESS Delete all invalid boot option success
@retval EFI_NOT_FOUND Variable "BootOrder" is not found
@retval EFI_OUT_OF_RESOURCES Lack of memory resource
@retval Other Error return value from SetVariable()
**/
EFI_STATUS
BdsDeleteAllInvalidEfiBootOption (
VOID
)
{
UINT16 *BootOrder;
UINT8 *BootOptionVar;
UINTN BootOrderSize;
UINTN BootOptionSize;
EFI_STATUS Status;
UINTN Index;
UINTN Index2;
UINT16 BootOption[BOOT_OPTION_MAX_CHAR];
EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;
UINT8 *TempPtr;
CHAR16 *Description;
Status = EFI_SUCCESS;
BootOrder = NULL;
BootOrderSize = 0;
//
// Check "BootOrder" variable firstly, this variable hold the number of boot options
//
BootOrder = BdsLibGetVariableAndSize (
L"BootOrder",
&gEfiGlobalVariableGuid,
&BootOrderSize
);
if (NULL == BootOrder) {
return EFI_NOT_FOUND;
}
Index = 0;
while (Index < BootOrderSize / sizeof (UINT16)) {
UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);
BootOptionVar = BdsLibGetVariableAndSize (
BootOption,
&gEfiGlobalVariableGuid,
&BootOptionSize
);
if (NULL == BootOptionVar) {
FreePool (BootOrder);
return EFI_OUT_OF_RESOURCES;
}
TempPtr = BootOptionVar;
TempPtr += sizeof (UINT32) + sizeof (UINT16);
Description = (CHAR16 *) TempPtr;
TempPtr += StrSize ((CHAR16 *) TempPtr);
OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
//
// Skip legacy boot option (BBS boot device)
//
if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&
(DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {
FreePool (BootOptionVar);
Index++;
continue;
}
if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath, FALSE, Description)) {
//
// Delete this invalid boot option "Boot####"
//
Status = gRT->SetVariable (
BootOption,
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
0,
NULL
);
//
// Mark this boot option in boot order as deleted
//
BootOrder[Index] = 0xffff;
}
FreePool (BootOptionVar);
Index++;
}
//
// Adjust boot order array
//
Index2 = 0;
for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {
if (BootOrder[Index] != 0xffff) {
BootOrder[Index2] = BootOrder[Index];
Index2 ++;
}
}
Status = gRT->SetVariable (
L"BootOrder",
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
Index2 * sizeof (UINT16),
BootOrder
);
FreePool (BootOrder);
return Status;
}
/**
For EFI boot option, BDS separate them as six types:
1. Network - The boot option points to the SimpleNetworkProtocol device.
Bds will try to automatically create this type boot option when enumerate.
2. Shell - The boot option points to internal flash shell.
Bds will try to automatically create this type boot option when enumerate.
3. Removable BlockIo - The boot option only points to the removable media
device, like USB flash disk, DVD, Floppy etc.
These device should contain a *removable* blockIo
protocol in their device handle.
Bds will try to automatically create this type boot option
when enumerate.
4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
like HardDisk.
These device should contain a *fixed* blockIo
protocol in their device handle.
BDS will skip fixed blockIo devices, and NOT
automatically create boot option for them. But BDS
will help to delete those fixed blockIo boot option,
whose description rule conflict with other auto-created
boot options.
5. Non-BlockIo Simplefile - The boot option points to a device whose handle
has SimpleFileSystem Protocol, but has no blockio
protocol. These devices do not offer blockIo
protocol, but BDS still can get the
\EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
Protocol.
6. File - The boot option points to a file. These boot options are usually
created by user manually or OS loader. BDS will not delete or modify
these boot options.
This function will enumerate all possible boot device in the system, and
automatically create boot options for Network, Shell, Removable BlockIo,
and Non-BlockIo Simplefile devices.
It will only execute once of every boot.
@param BdsBootOptionList The header of the link list which indexed all
current boot options
@retval EFI_SUCCESS Finished all the boot device enumerate and create
the boot option base on that boot device
@retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
**/
EFI_STATUS
EFIAPI
BdsLibEnumerateAllBootOption (
IN OUT LIST_ENTRY *BdsBootOptionList
)
{
EFI_STATUS Status;
UINT16 FloppyNumber;
UINT16 CdromNumber;
UINT16 UsbNumber;
UINT16 MiscNumber;
UINT16 ScsiNumber;
UINT16 NonBlockNumber;
UINTN NumberBlockIoHandles;
EFI_HANDLE *BlockIoHandles;
EFI_BLOCK_IO_PROTOCOL *BlkIo;
UINTN Index;
UINTN NumOfLoadFileHandles;
EFI_HANDLE *LoadFileHandles;
UINTN FvHandleCount;
EFI_HANDLE *FvHandleBuffer;
EFI_FV_FILETYPE Type;
UINTN Size;
EFI_FV_FILE_ATTRIBUTES Attributes;
UINT32 AuthenticationStatus;
EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
UINTN DevicePathType;
CHAR16 Buffer[40];
EFI_HANDLE *FileSystemHandles;
UINTN NumberFileSystemHandles;
BOOLEAN NeedDelete;
EFI_IMAGE_DOS_HEADER DosHeader;
CHAR8 *PlatLang;
CHAR8 *LastLang;
EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;
EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
FloppyNumber = 0;
CdromNumber = 0;
UsbNumber = 0;
MiscNumber = 0;
ScsiNumber = 0;
PlatLang = NULL;
LastLang = NULL;
ZeroMem (Buffer, sizeof (Buffer));
//
// If the boot device enumerate happened, just get the boot
// device from the boot order variable
//
if (mEnumBootDevice) {
LastLang = GetVariable (L"LastEnumLang", &mBdsLibLastLangGuid);
PlatLang = GetEfiGlobalVariable (L"PlatformLang");
if (LastLang == PlatLang) {
Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");
return Status;
} else {
Status = gRT->SetVariable (
L"LastEnumLang",
&mBdsLibLastLangGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
sizeof (PlatLang),
PlatLang
);
ASSERT_EFI_ERROR (Status);
}
}
//
// Notes: this dirty code is to get the legacy boot option from the
// BBS table and create to variable as the EFI boot option, it should
// be removed after the CSM can provide legacy boot option directly
//
REFRESH_LEGACY_BOOT_OPTIONS;
//
// Delete invalid boot option
//
BdsDeleteAllInvalidEfiBootOption ();
//
// Parse removable media
//
gBS->LocateHandleBuffer (
ByProtocol,
&gEfiBlockIoProtocolGuid,
NULL,
&NumberBlockIoHandles,
&BlockIoHandles
);
for (Index = 0; Index < NumberBlockIoHandles; Index++) {
Status = gBS->HandleProtocol (
BlockIoHandles[Index],
&gEfiBlockIoProtocolGuid,
(VOID **) &BlkIo
);
if (!EFI_ERROR (Status)) {
if (!BlkIo->Media->RemovableMedia) {
//
// skip the non-removable block devices
//
continue;
}
}
DevicePath = DevicePathFromHandle (BlockIoHandles[Index]);
DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);
switch (DevicePathType) {
case BDS_EFI_ACPI_FLOPPY_BOOT:
if (FloppyNumber != 0) {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);
} else {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));
}
BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
FloppyNumber++;
break;
//
// Assume a removable SATA device should be the DVD/CD device
//
case BDS_EFI_MESSAGE_ATAPI_BOOT:
case BDS_EFI_MESSAGE_SATA_BOOT:
if (CdromNumber != 0) {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);
} else {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));
}
DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));
BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
CdromNumber++;
break;
case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:
if (UsbNumber != 0) {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);
} else {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));
}
BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
UsbNumber++;
break;
case BDS_EFI_MESSAGE_SCSI_BOOT:
if (ScsiNumber != 0) {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);
} else {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));
}
BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
ScsiNumber++;
break;
case BDS_EFI_MESSAGE_MISC_BOOT:
if (MiscNumber != 0) {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);
} else {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));
}
BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
MiscNumber++;
break;
default:
break;
}
}
if (NumberBlockIoHandles != 0) {
FreePool (BlockIoHandles);
}
//
// If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
//
NonBlockNumber = 0;
gBS->LocateHandleBuffer (
ByProtocol,
&gEfiSimpleFileSystemProtocolGuid,
NULL,
&NumberFileSystemHandles,
&FileSystemHandles
);
for (Index = 0; Index < NumberFileSystemHandles; Index++) {
Status = gBS->HandleProtocol (
FileSystemHandles[Index],
&gEfiBlockIoProtocolGuid,
(VOID **) &BlkIo
);
if (!EFI_ERROR (Status)) {
//
// Skip if the file system handle supports a BlkIo protocol,
//
continue;
}
//
// Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
// machinename is ia32, ia64, x64, ...
//
Hdr.Union = &HdrData;
NeedDelete = TRUE;
Status = BdsLibGetImageHeader (
FileSystemHandles[Index],
EFI_REMOVABLE_MEDIA_FILE_NAME,
&DosHeader,
Hdr
);
if (!EFI_ERROR (Status) &&
EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&
Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
NeedDelete = FALSE;
}
if (NeedDelete) {
//
// No such file or the file is not a EFI application, delete this boot option
//
BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);
} else {
if (NonBlockNumber != 0) {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);
} else {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));
}
BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);
NonBlockNumber++;
}
}
if (NumberFileSystemHandles != 0) {
FreePool (FileSystemHandles);
}
//
// Parse Network Boot Device
//
NumOfLoadFileHandles = 0;
//
// Search Load File protocol for PXE boot option.
//
gBS->LocateHandleBuffer (
ByProtocol,
&gEfiLoadFileProtocolGuid,
NULL,
&NumOfLoadFileHandles,
&LoadFileHandles
);
for (Index = 0; Index < NumOfLoadFileHandles; Index++) {
if (Index != 0) {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)), Index);
} else {
UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)));
}
BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);
}
if (NumOfLoadFileHandles != 0) {
FreePool (LoadFileHandles);
}
//
// Check if we have on flash shell
//
gBS->LocateHandleBuffer (
ByProtocol,
&gEfiFirmwareVolume2ProtocolGuid,
NULL,
&FvHandleCount,
&FvHandleBuffer
);
for (Index = 0; Index < FvHandleCount; Index++) {
gBS->HandleProtocol (
FvHandleBuffer[Index],
&gEfiFirmwareVolume2ProtocolGuid,
(VOID **) &Fv
);
Status = Fv->ReadFile (
Fv,
PcdGetPtr(PcdShellFile),
NULL,
&Size,
&Type,
&Attributes,
&AuthenticationStatus
);
if (EFI_ERROR (Status)) {
//
// Skip if no shell file in the FV
//
continue;
}
//
// Build the shell boot option
//
BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);
}
if (FvHandleCount != 0) {
FreePool (FvHandleBuffer);
}
//
// Make sure every boot only have one time
// boot device enumerate
//
Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");
mEnumBootDevice = TRUE;
return Status;
}
/**
Build the boot option with the handle parsed in
@param Handle The handle which present the device path to create
boot option
@param BdsBootOptionList The header of the link list which indexed all
current boot options
@param String The description of the boot option.
**/
VOID
EFIAPI
BdsLibBuildOptionFromHandle (
IN EFI_HANDLE Handle,
IN LIST_ENTRY *BdsBootOptionList,
IN CHAR16 *String
)
{
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
DevicePath = DevicePathFromHandle (Handle);
//
// Create and register new boot option
//
BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");
}
/**
Build the on flash shell boot option with the handle parsed in.
@param Handle The handle which present the device path to create
on flash shell boot option
@param BdsBootOptionList The header of the link list which indexed all
current boot options
**/
VOID
EFIAPI
BdsLibBuildOptionFromShell (
IN EFI_HANDLE Handle,
IN OUT LIST_ENTRY *BdsBootOptionList
)
{
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;
DevicePath = DevicePathFromHandle (Handle);
//
// Build the shell device path
//
EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));
DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);
//
// Create and register the shell boot option
//
BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");
}
/**
Boot from the UEFI spec defined "BootNext" variable.
**/
VOID
EFIAPI
BdsLibBootNext (
VOID
)
{
UINT16 *BootNext;
UINTN BootNextSize;
CHAR16 Buffer[20];
BDS_COMMON_OPTION *BootOption;
LIST_ENTRY TempList;
UINTN ExitDataSize;
CHAR16 *ExitData;
//
// Init the boot option name buffer and temp link list
//
InitializeListHead (&TempList);
ZeroMem (Buffer, sizeof (Buffer));
BootNext = BdsLibGetVariableAndSize (
L"BootNext",
&gEfiGlobalVariableGuid,
&BootNextSize
);
//
// Clear the boot next variable first
//
if (BootNext != NULL) {
gRT->SetVariable (
L"BootNext",
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
0,
BootNext
);
//
// Start to build the boot option and try to boot
//
UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);
BootOption = BdsLibVariableToOption (&TempList, Buffer);
ASSERT (BootOption != NULL);
BdsLibConnectDevicePath (BootOption->DevicePath);
BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);
}
}
/**
Return the bootable media handle.
First, check the device is connected
Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
Third, detect the the default boot file in the Media, and return the removable Media handle.
@param DevicePath Device Path to a bootable device
@return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
**/
EFI_HANDLE
EFIAPI
BdsLibGetBootableHandle (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;
EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;
EFI_HANDLE Handle;
EFI_BLOCK_IO_PROTOCOL *BlockIo;
VOID *Buffer;
EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
UINTN Size;
UINTN TempSize;
EFI_HANDLE ReturnHandle;
EFI_HANDLE *SimpleFileSystemHandles;
UINTN NumberSimpleFileSystemHandles;
UINTN Index;
EFI_IMAGE_DOS_HEADER DosHeader;
EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;
EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
UpdatedDevicePath = DevicePath;
//
// Check whether the device is connected
//
Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);
if (EFI_ERROR (Status)) {
//
// Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
//
Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);
if (EFI_ERROR (Status)) {
//
// Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
//
UpdatedDevicePath = DevicePath;
Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);
gBS->ConnectController (Handle, NULL, NULL, TRUE);
}
} else {
//
// For removable device boot option, its contained device path only point to the removable device handle,
// should make sure all its children handles (its child partion or media handles) are created and connected.
//
gBS->ConnectController (Handle, NULL, NULL, TRUE);
//
// Get BlockIo protocol and check removable attribute
//
Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
//
// 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.
//
Buffer = AllocatePool (BlockIo->Media->BlockSize);
if (Buffer != NULL) {
BlockIo->ReadBlocks (
BlockIo,
BlockIo->Media->MediaId,
0,
BlockIo->Media->BlockSize,
Buffer
);
FreePool(Buffer);
}
}
//
// Detect the the default boot file from removable Media
//
//
// If fail to get bootable handle specified by a USB boot option, the BDS should try to find other bootable device in the same USB bus
// Try to locate the USB node device path first, if fail then use its previous PCI node to search
//
DupDevicePath = DuplicateDevicePath (DevicePath);
ASSERT (DupDevicePath != NULL);
UpdatedDevicePath = DupDevicePath;
Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);
//
// if the resulting device path point to a usb node, and the usb node is a dummy node, should only let device path only point to the previous Pci node
// Acpi()/Pci()/Usb() --> Acpi()/Pci()
//
if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&
(DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {
//
// Remove the usb node, let the device path only point to PCI node
//
SetDevicePathEndNode (UpdatedDevicePath);
UpdatedDevicePath = DupDevicePath;
} else {
UpdatedDevicePath = DevicePath;
}
//
// Get the device path size of boot option
//
Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node
ReturnHandle = NULL;
gBS->LocateHandleBuffer (
ByProtocol,
&gEfiSimpleFileSystemProtocolGuid,
NULL,
&NumberSimpleFileSystemHandles,
&SimpleFileSystemHandles
);
for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {
//
// Get the device path size of SimpleFileSystem handle
//
TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);
TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node
//
// Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
//
if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {
//
// Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
// machinename is ia32, ia64, x64, ...
//
Hdr.Union = &HdrData;
Status = BdsLibGetImageHeader (
SimpleFileSystemHandles[Index],
EFI_REMOVABLE_MEDIA_FILE_NAME,
&DosHeader,
Hdr
);
if (!EFI_ERROR (Status) &&
EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&
Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
ReturnHandle = SimpleFileSystemHandles[Index];
break;
}
}
}
FreePool(DupDevicePath);
if (SimpleFileSystemHandles != NULL) {
FreePool(SimpleFileSystemHandles);
}
return ReturnHandle;
}
/**
Check to see if the network cable is plugged in. If the DevicePath is not
connected it will be connected.
@param DevicePath Device Path to check
@retval TRUE DevicePath points to an Network that is connected
@retval FALSE DevicePath does not point to a bootable network
**/
BOOLEAN
BdsLibNetworkBootWithMediaPresent (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;
EFI_HANDLE Handle;
EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
BOOLEAN MediaPresent;
UINT32 InterruptStatus;
MediaPresent = FALSE;
UpdatedDevicePath = DevicePath;
//
// Locate Load File Protocol for PXE boot option first
//
Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);
if (EFI_ERROR (Status)) {
//
// Device not present so see if we need to connect it
//
Status = BdsLibConnectDevicePath (DevicePath);
if (!EFI_ERROR (Status)) {
//
// This one should work after we did the connect
//
Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);
}
}
if (!EFI_ERROR (Status)) {
Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);
if (EFI_ERROR (Status)) {
//
// Failed to open SNP from this handle, try to get SNP from parent handle
//
UpdatedDevicePath = DevicePathFromHandle (Handle);
if (UpdatedDevicePath != NULL) {
Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);
if (!EFI_ERROR (Status)) {
//
// SNP handle found, get SNP from it
//
Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);
}
}
}
if (!EFI_ERROR (Status)) {
if (Snp->Mode->MediaPresentSupported) {
if (Snp->Mode->State == EfiSimpleNetworkInitialized) {
//
// Invoke Snp->GetStatus() to refresh the media status
//
Snp->GetStatus (Snp, &InterruptStatus, NULL);
//
// In case some one else is using the SNP check to see if it's connected
//
MediaPresent = Snp->Mode->MediaPresent;
} else {
//
// No one is using SNP so we need to Start and Initialize so
// MediaPresent will be valid.
//
Status = Snp->Start (Snp);
if (!EFI_ERROR (Status)) {
Status = Snp->Initialize (Snp, 0, 0);
if (!EFI_ERROR (Status)) {
MediaPresent = Snp->Mode->MediaPresent;
Snp->Shutdown (Snp);
}
Snp->Stop (Snp);
}
}
} else {
MediaPresent = TRUE;
}
}
}
return MediaPresent;
}
/**
For a bootable Device path, return its boot type.
@param DevicePath The bootable device Path to check
@retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
which subtype is MEDIA_HARDDRIVE_DP
@retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
which subtype is MEDIA_CDROM_DP
@retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
which HID is floppy device.
@retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
and its last device path node's subtype is MSG_ATAPI_DP.
@retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
and its last device path node's subtype is MSG_SCSI_DP.
@retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
and its last device path node's subtype is MSG_USB_DP.
@retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
its last device path node point to a message device path node.
@retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
@retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
**/
UINT32
EFIAPI
BdsGetBootTypeFromDevicePath (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
)
{
ACPI_HID_DEVICE_PATH *Acpi;
EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
UINT32 BootType;
if (NULL == DevicePath) {
return BDS_EFI_UNSUPPORT;
}
TempDevicePath = DevicePath;
while (!IsDevicePathEndType (TempDevicePath)) {
switch (DevicePathType (TempDevicePath)) {
case BBS_DEVICE_PATH:
return BDS_LEGACY_BBS_BOOT;
case MEDIA_DEVICE_PATH:
if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {
return BDS_EFI_MEDIA_HD_BOOT;
} else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {
return BDS_EFI_MEDIA_CDROM_BOOT;
}
break;
case ACPI_DEVICE_PATH:
Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;
if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {
return BDS_EFI_ACPI_FLOPPY_BOOT;
}
break;
case MESSAGING_DEVICE_PATH:
//
// Get the last device path node
//
LastDeviceNode = NextDevicePathNode (TempDevicePath);
if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {
//
// if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
// skip it
//
LastDeviceNode = NextDevicePathNode (LastDeviceNode);
}
//
// if the device path not only point to driver device, it is not a messaging device path,
//
if (!IsDevicePathEndType (LastDeviceNode)) {
break;
}
switch (DevicePathSubType (TempDevicePath)) {
case MSG_ATAPI_DP:
BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;
break;
case MSG_USB_DP:
BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;
break;
case MSG_SCSI_DP:
BootType = BDS_EFI_MESSAGE_SCSI_BOOT;
break;
case MSG_SATA_DP:
BootType = BDS_EFI_MESSAGE_SATA_BOOT;
break;
case MSG_MAC_ADDR_DP:
case MSG_VLAN_DP:
case MSG_IPv4_DP:
case MSG_IPv6_DP:
BootType = BDS_EFI_MESSAGE_MAC_BOOT;
break;
default:
BootType = BDS_EFI_MESSAGE_MISC_BOOT;
break;
}
return BootType;
default:
break;
}
TempDevicePath = NextDevicePathNode (TempDevicePath);
}
return BDS_EFI_UNSUPPORT;
}
/**
Check whether the Device path in a boot option point to a valid bootable device,
And if CheckMedia is true, check the device is ready to boot now.
@param DevPath the Device path in a boot option
@param CheckMedia if true, check the device is ready to boot now.
@retval TRUE the Device path is valid
@retval FALSE the Device path is invalid .
**/
BOOLEAN
EFIAPI
BdsLibIsValidEFIBootOptDevicePath (
IN EFI_DEVICE_PATH_PROTOCOL *DevPath,
IN BOOLEAN CheckMedia
)
{
return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);
}
/**
Check whether the Device path in a boot option point to a valid bootable device,
And if CheckMedia is true, check the device is ready to boot now.
If Description is not NULL and the device path point to a fixed BlockIo
device, check the description whether conflict with other auto-created
boot options.
@param DevPath the Device path in a boot option
@param CheckMedia if true, check the device is ready to boot now.
@param Description the description in a boot option
@retval TRUE the Device path is valid
@retval FALSE the Device path is invalid .
**/
BOOLEAN
EFIAPI
BdsLibIsValidEFIBootOptDevicePathExt (
IN EFI_DEVICE_PATH_PROTOCOL *DevPath,
IN BOOLEAN CheckMedia,
IN CHAR16 *Description
)
{
EFI_STATUS Status;
EFI_HANDLE Handle;
EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
EFI_BLOCK_IO_PROTOCOL *BlockIo;
TempDevicePath = DevPath;
LastDeviceNode = DevPath;
//
// Check if it's a valid boot option for network boot device.
// Check if there is EfiLoadFileProtocol installed.
// If yes, that means there is a boot option for network.
//
Status = gBS->LocateDevicePath (
&gEfiLoadFileProtocolGuid,
&TempDevicePath,
&Handle
);
if (EFI_ERROR (Status)) {
//
// Device not present so see if we need to connect it
//
TempDevicePath = DevPath;
BdsLibConnectDevicePath (TempDevicePath);
Status = gBS->LocateDevicePath (
&gEfiLoadFileProtocolGuid,
&TempDevicePath,
&Handle
);
}
if (!EFI_ERROR (Status)) {
if (!IsDevicePathEnd (TempDevicePath)) {
//
// LoadFile protocol is not installed on handle with exactly the same DevPath
//
return FALSE;
}
if (CheckMedia) {
//
// Test if it is ready to boot now
//
if (BdsLibNetworkBootWithMediaPresent(DevPath)) {
return TRUE;
}
} else {
return TRUE;
}
}
//
// If the boot option point to a file, it is a valid EFI boot option,
// and assume it is ready to boot now
//
while (!IsDevicePathEnd (TempDevicePath)) {
LastDeviceNode = TempDevicePath;
TempDevicePath = NextDevicePathNode (TempDevicePath);
}
if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&
(DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {
return TRUE;
}
//
// Check if it's a valid boot option for internal Shell
//
if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {
//
// If the boot option point to Internal FV shell, make sure it is valid
//
TempDevicePath = DevPath;
Status = BdsLibUpdateFvFileDevicePath (&TempDevicePath, PcdGetPtr(PcdShellFile));
if (Status == EFI_ALREADY_STARTED) {
return TRUE;
} else {
if (Status == EFI_SUCCESS) {
FreePool (TempDevicePath);
}
return FALSE;
}
}
//
// If the boot option point to a blockIO device:
// if it is a removable blockIo device, it is valid.
// if it is a fixed blockIo device, check its description confliction.
//
TempDevicePath = DevPath;
Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);
if (EFI_ERROR (Status)) {
//
// Device not present so see if we need to connect it
//
Status = BdsLibConnectDevicePath (DevPath);
if (!EFI_ERROR (Status)) {
//
// Try again to get the Block Io protocol after we did the connect
//
TempDevicePath = DevPath;
Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);
}
}
if (!EFI_ERROR (Status)) {
Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
if (!EFI_ERROR (Status)) {
if (CheckMedia) {
//
// Test if it is ready to boot now
//
if (BdsLibGetBootableHandle (DevPath) != NULL) {
return TRUE;
}
} else {
return TRUE;
}
}
} else {
//
// if the boot option point to a simple file protocol which does not consume block Io protocol, it is also a valid EFI boot option,
//
Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);
if (!EFI_ERROR (Status)) {
if (CheckMedia) {
//
// Test if it is ready to boot now
//
if (BdsLibGetBootableHandle (DevPath) != NULL) {
return TRUE;
}
} else {
return TRUE;
}
}
}
return FALSE;
}
/**
According to a file guild, check a Fv file device path is valid. If it is invalid,
try to return the valid device path.
FV address maybe changes for memory layout adjust from time to time, use this function
could promise the Fv file device path is right.
@param DevicePath on input, the Fv file device path need to check on
output, the updated valid Fv file device path
@param FileGuid the Fv file guild
@retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
parameter
@retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
guild at all
@retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
valid
@retval EFI_SUCCESS has successfully updated the invalid DevicePath,
and return the updated device path in DevicePath
**/
EFI_STATUS
EFIAPI
BdsLibUpdateFvFileDevicePath (
IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,
IN EFI_GUID *FileGuid
)
{
EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
EFI_STATUS Status;
EFI_GUID *GuidPoint;
UINTN Index;
UINTN FvHandleCount;
EFI_HANDLE *FvHandleBuffer;
EFI_FV_FILETYPE Type;
UINTN Size;
EFI_FV_FILE_ATTRIBUTES Attributes;
UINT32 AuthenticationStatus;
BOOLEAN FindFvFile;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;
EFI_HANDLE FoundFvHandle;
EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
if ((DevicePath == NULL) || (*DevicePath == NULL)) {
return EFI_INVALID_PARAMETER;
}
if (FileGuid == NULL) {
return EFI_INVALID_PARAMETER;
}
//
// Check whether the device path point to the default the input Fv file
//
TempDevicePath = *DevicePath;
LastDeviceNode = TempDevicePath;
while (!IsDevicePathEnd (TempDevicePath)) {
LastDeviceNode = TempDevicePath;
TempDevicePath = NextDevicePathNode (TempDevicePath);
}
GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (
(MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode
);
if (GuidPoint == NULL) {
//
// if this option does not points to a Fv file, just return EFI_UNSUPPORTED
//
return EFI_UNSUPPORTED;
}
if (!CompareGuid (GuidPoint, FileGuid)) {
//
// If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
//
return EFI_UNSUPPORTED;
}
//
// Check whether the input Fv file device path is valid
//
TempDevicePath = *DevicePath;
FoundFvHandle = NULL;
Status = gBS->LocateDevicePath (
&gEfiFirmwareVolume2ProtocolGuid,
&TempDevicePath,
&FoundFvHandle
);
if (!EFI_ERROR (Status)) {
Status = gBS->HandleProtocol (
FoundFvHandle,
&gEfiFirmwareVolume2ProtocolGuid,
(VOID **) &Fv
);
if (!EFI_ERROR (Status)) {
//
// Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
//
Status = Fv->ReadFile (
Fv,
FileGuid,
NULL,
&Size,
&Type,
&Attributes,
&AuthenticationStatus
);
if (!EFI_ERROR (Status)) {
return EFI_ALREADY_STARTED;
}
}
}
//
// Look for the input wanted FV file in current FV
// First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
//
FindFvFile = FALSE;
FoundFvHandle = NULL;
Status = gBS->HandleProtocol (
mBdsImageHandle,
&gEfiLoadedImageProtocolGuid,
(VOID **) &LoadedImage
);
if (!EFI_ERROR (Status)) {
Status = gBS->HandleProtocol (
LoadedImage->DeviceHandle,
&gEfiFirmwareVolume2ProtocolGuid,
(VOID **) &Fv
);
if (!EFI_ERROR (Status)) {
Status = Fv->ReadFile (
Fv,
FileGuid,
NULL,
&Size,
&Type,
&Attributes,
&AuthenticationStatus
);
if (!EFI_ERROR (Status)) {
FindFvFile = TRUE;
FoundFvHandle = LoadedImage->DeviceHandle;
}
}
}
//
// Second, if fail to find, try to enumerate all FV
//
if (!FindFvFile) {
FvHandleBuffer = NULL;
gBS->LocateHandleBuffer (
ByProtocol,
&gEfiFirmwareVolume2ProtocolGuid,
NULL,
&FvHandleCount,
&FvHandleBuffer
);
for (Index = 0; Index < FvHandleCount; Index++) {
gBS->HandleProtocol (
FvHandleBuffer[Index],
&gEfiFirmwareVolume2ProtocolGuid,
(VOID **) &Fv
);
Status = Fv->ReadFile (
Fv,
FileGuid,
NULL,
&Size,
&Type,
&Attributes,
&AuthenticationStatus
);
if (EFI_ERROR (Status)) {
//
// Skip if input Fv file not in the FV
//
continue;
}
FindFvFile = TRUE;
FoundFvHandle = FvHandleBuffer[Index];
break;
}
if (FvHandleBuffer != NULL) {
FreePool (FvHandleBuffer);
}
}
if (FindFvFile) {
//
// Build the shell device path
//
NewDevicePath = DevicePathFromHandle (FoundFvHandle);
EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);
NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);
*DevicePath = NewDevicePath;
return EFI_SUCCESS;
}
return EFI_NOT_FOUND;
}
|