2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalBdsLib.h"
12 BOOLEAN mEnumBootDevice
= FALSE
;
13 EFI_HII_HANDLE gBdsLibStringPackHandle
= NULL
;
19 @param Event The triggered event.
20 @param Context Context for this event.
31 // Record the performance data for End of BDS
33 PERF_END(NULL
, "BDS", NULL
, 0);
39 The constructor function register UNI strings into imageHandle.
41 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
43 @param ImageHandle The firmware allocated handle for the EFI image.
44 @param SystemTable A pointer to the EFI System Table.
46 @retval EFI_SUCCESS The constructor successfully added string package.
47 @retval Other value The constructor can't add string package.
52 GenericBdsLibConstructor (
53 IN EFI_HANDLE ImageHandle
,
54 IN EFI_SYSTEM_TABLE
*SystemTable
58 gBdsLibStringPackHandle
= HiiAddPackages (
59 &gBdsLibStringPackageGuid
,
65 ASSERT (gBdsLibStringPackHandle
!= NULL
);
71 Deletete the Boot Option from EFI Variable. The Boot Order Arrray
74 @param OptionNumber The number of Boot option want to be deleted.
75 @param BootOrder The Boot Order array.
76 @param BootOrderSize The size of the Boot Order Array.
78 @retval EFI_SUCCESS The Boot Option Variable was found and removed
79 @retval EFI_UNSUPPORTED The Boot Option Variable store was inaccessible
80 @retval EFI_NOT_FOUND The Boot Option Variable was not found
85 IN UINTN OptionNumber
,
86 IN OUT UINT16
*BootOrder
,
87 IN OUT UINTN
*BootOrderSize
94 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", OptionNumber
);
95 Status
= gRT
->SetVariable (
97 &gEfiGlobalVariableGuid
,
103 // Deleting variable with existing variable implementation shouldn't fail.
105 ASSERT_EFI_ERROR (Status
);
108 // adjust boot order array
110 for (Index
= 0; Index
< *BootOrderSize
/ sizeof (UINT16
); Index
++) {
111 if (BootOrder
[Index
] == OptionNumber
) {
112 CopyMem (&BootOrder
[Index
], &BootOrder
[Index
+1], *BootOrderSize
- (Index
+1) * sizeof (UINT16
));
113 *BootOrderSize
-= sizeof (UINT16
);
122 Translate the first n characters of an Ascii string to
123 Unicode characters. The count n is indicated by parameter
124 Size. If Size is greater than the length of string, then
125 the entire string is translated.
128 @param AStr Pointer to input Ascii string.
129 @param Size The number of characters to translate.
130 @param UStr Pointer to output Unicode string buffer.
143 while (AStr
[Idx
] != 0) {
144 UStr
[Idx
] = (CHAR16
) AStr
[Idx
];
155 Build Legacy Device Name String according.
157 @param CurBBSEntry BBS Table.
159 @param BufSize The buffer size.
160 @param BootString The output string.
164 BdsBuildLegacyDevNameString (
165 IN BBS_TABLE
*CurBBSEntry
,
168 OUT CHAR16
*BootString
181 Fmt
= L
"Primary Master %s";
188 Fmt
= L
"Primary Slave %s";
195 Fmt
= L
"Secondary Master %s";
202 Fmt
= L
"Secondary Slave %s";
210 switch (CurBBSEntry
->DeviceType
) {
231 case BBS_EMBED_NETWORK
:
245 // If current BBS entry has its description then use it.
247 StringDesc
= (UINT8
*) (((UINTN
) CurBBSEntry
->DescStringSegment
<< 4) + CurBBSEntry
->DescStringOffset
);
248 if (NULL
!= StringDesc
) {
250 // Only get fisrt 32 characters, this is suggested by BBS spec
252 AsciiToUnicodeSize (StringDesc
, 32, Temp
);
258 // BbsTable 16 entries are for onboard IDE.
259 // Set description string for SATA harddisks, Harddisk 0 ~ Harddisk 11
261 if (Index
>= 5 && Index
<= 16 && (CurBBSEntry
->DeviceType
== BBS_HARDDISK
|| CurBBSEntry
->DeviceType
== BBS_CDROM
)) {
263 UnicodeSPrint (BootString
, BufSize
, Fmt
, Type
, Index
- 5);
265 UnicodeSPrint (BootString
, BufSize
, Fmt
, Type
);
271 Create a legacy boot option for the specified entry of
272 BBS table, save it as variable, and append it to the boot
276 @param CurrentBbsEntry Pointer to current BBS table.
277 @param CurrentBbsDevPath Pointer to the Device Path Protocol instance of BBS
278 @param Index Index of the specified entry in BBS table.
279 @param BootOrderList On input, the original boot order list.
280 On output, the new boot order list attached with the
282 @param BootOrderListSize On input, the original size of boot order list.
283 On output, the size of new boot order list.
285 @retval EFI_SUCCESS Boot Option successfully created.
286 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
287 @retval Other Error occurs while setting variable.
291 BdsCreateLegacyBootOption (
292 IN BBS_TABLE
*CurrentBbsEntry
,
293 IN EFI_DEVICE_PATH_PROTOCOL
*CurrentBbsDevPath
,
295 IN OUT UINT16
**BootOrderList
,
296 IN OUT UINTN
*BootOrderListSize
300 UINT16 CurrentBootOptionNo
;
301 UINT16 BootString
[10];
302 CHAR16 BootDesc
[100];
303 CHAR8 HelpString
[100];
304 UINT16
*NewBootOrderList
;
309 UINT16 CurrentBbsDevPathSize
;
310 UINTN BootOrderIndex
;
311 UINTN BootOrderLastIndex
;
313 BOOLEAN IndexNotFound
;
314 BBS_BBS_DEVICE_PATH
*NewBbsDevPathNode
;
316 if ((*BootOrderList
) == NULL
) {
317 CurrentBootOptionNo
= 0;
319 for (ArrayIndex
= 0; ArrayIndex
< (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
)); ArrayIndex
++) {
320 IndexNotFound
= TRUE
;
321 for (BootOrderIndex
= 0; BootOrderIndex
< (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
)); BootOrderIndex
++) {
322 if ((*BootOrderList
)[BootOrderIndex
] == ArrayIndex
) {
323 IndexNotFound
= FALSE
;
328 if (!IndexNotFound
) {
335 CurrentBootOptionNo
= (UINT16
) ArrayIndex
;
345 BdsBuildLegacyDevNameString (CurrentBbsEntry
, Index
, sizeof (BootDesc
), BootDesc
);
348 // Create new BBS device path node with description string
350 UnicodeStrToAsciiStrS (BootDesc
, HelpString
, sizeof (HelpString
));
352 StringLen
= AsciiStrLen (HelpString
);
353 NewBbsDevPathNode
= AllocateZeroPool (sizeof (BBS_BBS_DEVICE_PATH
) + StringLen
);
354 if (NewBbsDevPathNode
== NULL
) {
355 return EFI_OUT_OF_RESOURCES
;
357 CopyMem (NewBbsDevPathNode
, CurrentBbsDevPath
, sizeof (BBS_BBS_DEVICE_PATH
));
358 CopyMem (NewBbsDevPathNode
->String
, HelpString
, StringLen
+ 1);
359 SetDevicePathNodeLength (&(NewBbsDevPathNode
->Header
), sizeof (BBS_BBS_DEVICE_PATH
) + StringLen
);
362 // Create entire new CurrentBbsDevPath with end node
364 CurrentBbsDevPath
= AppendDevicePathNode (
366 (EFI_DEVICE_PATH_PROTOCOL
*) NewBbsDevPathNode
368 if (CurrentBbsDevPath
== NULL
) {
369 FreePool (NewBbsDevPathNode
);
370 return EFI_OUT_OF_RESOURCES
;
373 CurrentBbsDevPathSize
= (UINT16
) (GetDevicePathSize (CurrentBbsDevPath
));
375 BufferSize
= sizeof (UINT32
) +
378 CurrentBbsDevPathSize
+
382 Buffer
= AllocateZeroPool (BufferSize
);
383 if (Buffer
== NULL
) {
384 FreePool (NewBbsDevPathNode
);
385 FreePool (CurrentBbsDevPath
);
386 return EFI_OUT_OF_RESOURCES
;
389 Ptr
= (UINT8
*) Buffer
;
391 *((UINT32
*) Ptr
) = LOAD_OPTION_ACTIVE
;
392 Ptr
+= sizeof (UINT32
);
394 *((UINT16
*) Ptr
) = CurrentBbsDevPathSize
;
395 Ptr
+= sizeof (UINT16
);
402 Ptr
+= StrSize (BootDesc
);
407 CurrentBbsDevPathSize
409 Ptr
+= CurrentBbsDevPathSize
;
417 Ptr
+= sizeof (BBS_TABLE
);
418 *((UINT16
*) Ptr
) = (UINT16
) Index
;
420 Status
= gRT
->SetVariable (
422 &gEfiGlobalVariableGuid
,
423 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
432 NewBootOrderList
= AllocateZeroPool (*BootOrderListSize
+ sizeof (UINT16
));
433 if (NULL
== NewBootOrderList
) {
434 FreePool (NewBbsDevPathNode
);
435 FreePool (CurrentBbsDevPath
);
436 return EFI_OUT_OF_RESOURCES
;
439 if (*BootOrderList
!= NULL
) {
440 CopyMem (NewBootOrderList
, *BootOrderList
, *BootOrderListSize
);
441 FreePool (*BootOrderList
);
444 BootOrderLastIndex
= (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
));
445 NewBootOrderList
[BootOrderLastIndex
] = CurrentBootOptionNo
;
446 *BootOrderListSize
+= sizeof (UINT16
);
447 *BootOrderList
= NewBootOrderList
;
449 FreePool (NewBbsDevPathNode
);
450 FreePool (CurrentBbsDevPath
);
455 Check if the boot option is a legacy one.
457 @param BootOptionVar The boot option data payload.
458 @param BbsEntry The BBS Table.
459 @param BbsIndex The table index.
461 @retval TRUE It is a legacy boot option.
462 @retval FALSE It is not a legacy boot option.
466 BdsIsLegacyBootOption (
467 IN UINT8
*BootOptionVar
,
468 OUT BBS_TABLE
**BbsEntry
,
473 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
478 Ptr
+= sizeof (UINT32
);
479 DevPathLen
= *(UINT16
*) Ptr
;
480 Ptr
+= sizeof (UINT16
);
481 Ptr
+= StrSize ((UINT16
*) Ptr
);
482 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
483 if ((BBS_DEVICE_PATH
== DevicePath
->Type
) && (BBS_BBS_DP
== DevicePath
->SubType
)) {
485 *BbsEntry
= (BBS_TABLE
*) Ptr
;
486 Ptr
+= sizeof (BBS_TABLE
);
487 *BbsIndex
= *(UINT16
*) Ptr
;
498 Delete all the invalid legacy boot options.
500 @retval EFI_SUCCESS All invalide legacy boot options are deleted.
501 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
502 @retval EFI_NOT_FOUND Fail to retrive variable of boot order.
506 BdsDeleteAllInvalidLegacyBootOptions (
511 UINT8
*BootOptionVar
;
513 UINTN BootOptionSize
;
517 HDD_INFO
*LocalHddInfo
;
518 BBS_TABLE
*LocalBbsTable
;
521 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
523 UINT16 BootOption
[10];
524 UINT16 BootDesc
[100];
525 BOOLEAN DescStringMatch
;
527 Status
= EFI_SUCCESS
;
533 LocalBbsTable
= NULL
;
536 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
537 if (EFI_ERROR (Status
)) {
541 BootOrder
= BdsLibGetVariableAndSize (
543 &gEfiGlobalVariableGuid
,
546 if (BootOrder
== NULL
) {
547 return EFI_NOT_FOUND
;
550 LegacyBios
->GetBbsInfo (
559 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
560 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
561 BootOptionVar
= BdsLibGetVariableAndSize (
563 &gEfiGlobalVariableGuid
,
566 if (NULL
== BootOptionVar
) {
568 Status
= gRT
->GetVariable (
570 &gEfiGlobalVariableGuid
,
575 if (Status
== EFI_NOT_FOUND
) {
579 BdsDeleteBootOption (
586 FreePool (BootOrder
);
587 return EFI_OUT_OF_RESOURCES
;
592 // Skip Non-Legacy boot option
594 if (!BdsIsLegacyBootOption (BootOptionVar
, &BbsEntry
, &BbsIndex
)) {
595 if (BootOptionVar
!= NULL
) {
596 FreePool (BootOptionVar
);
602 if (BbsIndex
< BbsCount
) {
604 // Check if BBS Description String is changed
606 DescStringMatch
= FALSE
;
607 BdsBuildLegacyDevNameString (
608 &LocalBbsTable
[BbsIndex
],
614 if (StrCmp (BootDesc
, (UINT16
*)(BootOptionVar
+ sizeof (UINT32
) + sizeof (UINT16
))) == 0) {
615 DescStringMatch
= TRUE
;
618 if (!((LocalBbsTable
[BbsIndex
].BootPriority
== BBS_IGNORE_ENTRY
) ||
619 (LocalBbsTable
[BbsIndex
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)) &&
620 (LocalBbsTable
[BbsIndex
].DeviceType
== BbsEntry
->DeviceType
) &&
627 if (BootOptionVar
!= NULL
) {
628 FreePool (BootOptionVar
);
633 BdsDeleteBootOption (
641 // Adjust the number of boot options.
643 Status
= gRT
->SetVariable (
645 &gEfiGlobalVariableGuid
,
646 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
651 // Shrinking variable with existing variable implementation shouldn't fail.
653 ASSERT_EFI_ERROR (Status
);
654 FreePool (BootOrder
);
660 Find all legacy boot option by device type.
662 @param BootOrder The boot order array.
663 @param BootOptionNum The number of boot option.
664 @param DevType Device type.
665 @param DevName Device name.
666 @param Attribute The boot option attribute.
667 @param BbsIndex The BBS table index.
668 @param OptionNumber The boot option index.
670 @retval TRUE The Legacy boot option is found.
671 @retval FALSE The legacy boot option is not found.
675 BdsFindLegacyBootOptionByDevTypeAndName (
676 IN UINT16
*BootOrder
,
677 IN UINTN BootOptionNum
,
680 OUT UINT32
*Attribute
,
681 OUT UINT16
*BbsIndex
,
682 OUT UINT16
*OptionNumber
686 CHAR16 BootOption
[9];
687 UINTN BootOptionSize
;
688 UINT8
*BootOptionVar
;
695 if (NULL
== BootOrder
) {
700 // Loop all boot option from variable
702 for (Index
= 0; Index
< BootOptionNum
; Index
++) {
703 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", (UINTN
) BootOrder
[Index
]);
704 BootOptionVar
= BdsLibGetVariableAndSize (
706 &gEfiGlobalVariableGuid
,
709 if (NULL
== BootOptionVar
) {
714 // Skip Non-legacy boot option
716 if (!BdsIsLegacyBootOption (BootOptionVar
, &BbsEntry
, BbsIndex
)) {
717 FreePool (BootOptionVar
);
722 (BbsEntry
->DeviceType
!= DevType
) ||
723 (StrCmp (DevName
, (CHAR16
*)(BootOptionVar
+ sizeof (UINT32
) + sizeof (UINT16
))) != 0)
725 FreePool (BootOptionVar
);
729 *Attribute
= *(UINT32
*) BootOptionVar
;
730 *OptionNumber
= BootOrder
[Index
];
732 FreePool (BootOptionVar
);
740 Create a legacy boot option.
742 @param BbsItem The BBS Table entry.
743 @param Index Index of the specified entry in BBS table.
744 @param BootOrderList The boot order list.
745 @param BootOrderListSize The size of boot order list.
747 @retval EFI_OUT_OF_RESOURCE No enough memory.
748 @retval EFI_SUCCESS The function complete successfully.
749 @return Other value if the legacy boot option is not created.
753 BdsCreateOneLegacyBootOption (
754 IN BBS_TABLE
*BbsItem
,
756 IN OUT UINT16
**BootOrderList
,
757 IN OUT UINTN
*BootOrderListSize
760 BBS_BBS_DEVICE_PATH BbsDevPathNode
;
762 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
767 // Create device path node.
769 BbsDevPathNode
.Header
.Type
= BBS_DEVICE_PATH
;
770 BbsDevPathNode
.Header
.SubType
= BBS_BBS_DP
;
771 SetDevicePathNodeLength (&BbsDevPathNode
.Header
, sizeof (BBS_BBS_DEVICE_PATH
));
772 BbsDevPathNode
.DeviceType
= BbsItem
->DeviceType
;
773 CopyMem (&BbsDevPathNode
.StatusFlag
, &BbsItem
->StatusFlags
, sizeof (UINT16
));
775 DevPath
= AppendDevicePathNode (
777 (EFI_DEVICE_PATH_PROTOCOL
*) &BbsDevPathNode
779 if (NULL
== DevPath
) {
780 return EFI_OUT_OF_RESOURCES
;
783 Status
= BdsCreateLegacyBootOption (
790 BbsItem
->BootPriority
= 0x00;
798 Add the legacy boot options from BBS table if they do not exist.
800 @retval EFI_SUCCESS The boot options are added successfully
801 or they are already in boot options.
802 @retval EFI_NOT_FOUND No legacy boot options is found.
803 @retval EFI_OUT_OF_RESOURCE No enough memory.
804 @return Other value LegacyBoot options are not added.
808 BdsAddNonExistingLegacyBootOptions (
818 HDD_INFO
*LocalHddInfo
;
819 BBS_TABLE
*LocalBbsTable
;
821 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
830 LocalBbsTable
= NULL
;
832 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
833 if (EFI_ERROR (Status
)) {
837 LegacyBios
->GetBbsInfo (
845 BootOrder
= BdsLibGetVariableAndSize (
847 &gEfiGlobalVariableGuid
,
850 if (BootOrder
== NULL
) {
854 for (Index
= 0; Index
< BbsCount
; Index
++) {
855 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
856 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
861 BdsBuildLegacyDevNameString (&LocalBbsTable
[Index
], Index
, sizeof (Desc
), Desc
);
863 Exist
= BdsFindLegacyBootOptionByDevTypeAndName (
865 BootOrderSize
/ sizeof (UINT16
),
866 LocalBbsTable
[Index
].DeviceType
,
874 // Not found such type of legacy device in boot options or we found but it's disabled
875 // so we have to create one and put it to the tail of boot order list
877 Status
= BdsCreateOneLegacyBootOption (
878 &LocalBbsTable
[Index
],
883 if (!EFI_ERROR (Status
)) {
884 ASSERT (BootOrder
!= NULL
);
886 OptionNumber
= BootOrder
[BootOrderSize
/ sizeof (UINT16
) - 1];
890 ASSERT (BbsIndex
== Index
);
893 Status
= gRT
->SetVariable (
895 &gEfiGlobalVariableGuid
,
896 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
900 if (BootOrder
!= NULL
) {
901 FreePool (BootOrder
);
908 Fill the device order buffer.
910 @param BbsTable The BBS table.
911 @param BbsType The BBS Type.
912 @param BbsCount The BBS Count.
913 @param Buf device order buffer.
915 @return The device order buffer.
920 IN BBS_TABLE
*BbsTable
,
928 for (Index
= 0; Index
< BbsCount
; Index
++) {
929 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
933 if (BbsTable
[Index
].DeviceType
!= BbsType
) {
937 *Buf
= (UINT16
) (Index
& 0xFF);
945 Create the device order buffer.
947 @param BbsTable The BBS table.
948 @param BbsCount The BBS Count.
950 @retval EFI_SUCCES The buffer is created and the EFI variable named
951 VAR_LEGACY_DEV_ORDER and gEfiLegacyDevOrderVariableGuid is
953 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
954 @retval EFI_DEVICE_ERROR Fail to add the device order into EFI variable fail
955 because of hardware error.
959 IN BBS_TABLE
*BbsTable
,
971 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
972 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
981 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
983 Status
= EFI_SUCCESS
;
986 // Count all boot devices
988 for (Index
= 0; Index
< BbsCount
; Index
++) {
989 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
993 switch (BbsTable
[Index
].DeviceType
) {
1006 case BBS_EMBED_NETWORK
:
1010 case BBS_BEV_DEVICE
:
1019 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * FDCount
);
1020 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * HDCount
);
1021 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * CDCount
);
1022 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * NETCount
);
1023 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * BEVCount
);
1026 // Create buffer to hold all boot device order
1028 DevOrder
= AllocateZeroPool (TotalSize
);
1029 if (NULL
== DevOrder
) {
1030 return EFI_OUT_OF_RESOURCES
;
1032 DevOrderPtr
= DevOrder
;
1034 DevOrderPtr
->BbsType
= BBS_FLOPPY
;
1035 DevOrderPtr
->Length
= (UINT16
) (sizeof (DevOrderPtr
->Length
) + FDCount
* sizeof (UINT16
));
1036 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_FLOPPY
, BbsCount
, DevOrderPtr
->Data
);
1038 DevOrderPtr
->BbsType
= BBS_HARDDISK
;
1039 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1040 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_HARDDISK
, BbsCount
, DevOrderPtr
->Data
);
1042 DevOrderPtr
->BbsType
= BBS_CDROM
;
1043 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1044 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_CDROM
, BbsCount
, DevOrderPtr
->Data
);
1046 DevOrderPtr
->BbsType
= BBS_EMBED_NETWORK
;
1047 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1048 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_EMBED_NETWORK
, BbsCount
, DevOrderPtr
->Data
);
1050 DevOrderPtr
->BbsType
= BBS_BEV_DEVICE
;
1051 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1052 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_BEV_DEVICE
, BbsCount
, DevOrderPtr
->Data
);
1054 ASSERT (TotalSize
== ((UINTN
) DevOrderPtr
- (UINTN
) DevOrder
));
1057 // Save device order for legacy boot device to variable.
1059 Status
= gRT
->SetVariable (
1060 VAR_LEGACY_DEV_ORDER
,
1061 &gEfiLegacyDevOrderVariableGuid
,
1062 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1066 FreePool (DevOrder
);
1072 Add the legacy boot devices from BBS table into
1073 the legacy device boot order.
1075 @retval EFI_SUCCESS The boot devices are added successfully.
1076 @retval EFI_NOT_FOUND The legacy boot devices are not found.
1077 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
1078 @retval EFI_DEVICE_ERROR Fail to add the legacy device boot order into EFI variable
1079 because of hardware error.
1083 BdsUpdateLegacyDevOrder (
1087 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1088 LEGACY_DEV_ORDER_ENTRY
*NewDevOrder
;
1089 LEGACY_DEV_ORDER_ENTRY
*Ptr
;
1090 LEGACY_DEV_ORDER_ENTRY
*NewPtr
;
1092 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1096 HDD_INFO
*LocalHddInfo
;
1097 BBS_TABLE
*LocalBbsTable
;
1127 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
1135 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1136 if (EFI_ERROR (Status
)) {
1140 Status
= LegacyBios
->GetBbsInfo (
1147 if (EFI_ERROR (Status
)) {
1151 DevOrder
= BdsLibGetVariableAndSize (
1152 VAR_LEGACY_DEV_ORDER
,
1153 &gEfiLegacyDevOrderVariableGuid
,
1156 if (NULL
== DevOrder
) {
1157 return BdsCreateDevOrder (LocalBbsTable
, BbsCount
);
1160 // First we figure out how many boot devices with same device type respectively
1162 for (Index
= 0; Index
< BbsCount
; Index
++) {
1163 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1164 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1169 switch (LocalBbsTable
[Index
].DeviceType
) {
1182 case BBS_EMBED_NETWORK
:
1186 case BBS_BEV_DEVICE
:
1195 TotalSize
+= (HeaderSize
+ FDCount
* sizeof (UINT16
));
1196 TotalSize
+= (HeaderSize
+ HDCount
* sizeof (UINT16
));
1197 TotalSize
+= (HeaderSize
+ CDCount
* sizeof (UINT16
));
1198 TotalSize
+= (HeaderSize
+ NETCount
* sizeof (UINT16
));
1199 TotalSize
+= (HeaderSize
+ BEVCount
* sizeof (UINT16
));
1201 NewDevOrder
= AllocateZeroPool (TotalSize
);
1202 if (NULL
== NewDevOrder
) {
1203 return EFI_OUT_OF_RESOURCES
;
1212 NewPtr
= NewDevOrder
;
1213 NewPtr
->BbsType
= Ptr
->BbsType
;
1214 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + FDCount
* sizeof (UINT16
));
1215 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1216 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1217 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1218 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_FLOPPY
1223 NewPtr
->Data
[FDIndex
] = Ptr
->Data
[Index
];
1226 NewFDPtr
= NewPtr
->Data
;
1231 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1232 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1233 NewPtr
->BbsType
= Ptr
->BbsType
;
1234 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1235 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1236 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1237 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1238 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1239 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_HARDDISK
1244 NewPtr
->Data
[HDIndex
] = Ptr
->Data
[Index
];
1247 NewHDPtr
= NewPtr
->Data
;
1252 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1253 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1254 NewPtr
->BbsType
= Ptr
->BbsType
;
1255 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1256 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1257 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1258 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1259 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1260 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_CDROM
1265 NewPtr
->Data
[CDIndex
] = Ptr
->Data
[Index
];
1268 NewCDPtr
= NewPtr
->Data
;
1273 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1274 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1275 NewPtr
->BbsType
= Ptr
->BbsType
;
1276 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1277 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1278 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1279 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1280 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1281 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_EMBED_NETWORK
1286 NewPtr
->Data
[NETIndex
] = Ptr
->Data
[Index
];
1289 NewNETPtr
= NewPtr
->Data
;
1294 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1295 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1296 NewPtr
->BbsType
= Ptr
->BbsType
;
1297 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1298 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1299 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1300 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1301 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1302 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_BEV_DEVICE
1307 NewPtr
->Data
[BEVIndex
] = Ptr
->Data
[Index
];
1310 NewBEVPtr
= NewPtr
->Data
;
1312 for (Index
= 0; Index
< BbsCount
; Index
++) {
1313 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1314 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1319 switch (LocalBbsTable
[Index
].DeviceType
) {
1322 NewDevPtr
= NewFDPtr
;
1327 NewDevPtr
= NewHDPtr
;
1332 NewDevPtr
= NewCDPtr
;
1335 case BBS_EMBED_NETWORK
:
1337 NewDevPtr
= NewNETPtr
;
1340 case BBS_BEV_DEVICE
:
1342 NewDevPtr
= NewBEVPtr
;
1350 // at this point we have copied those valid indexes to new buffer
1351 // and we should check if there is any new appeared boot device
1354 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1355 if ((NewDevPtr
[Index2
] & 0xFF) == (UINT16
) Index
) {
1360 if (Index2
== *Idx
) {
1362 // Index2 == *Idx means we didn't find Index
1363 // so Index is a new appeared device's index in BBS table
1364 // insert it before disabled indexes.
1366 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1367 if ((NewDevPtr
[Index2
] & 0xFF00) == 0xFF00) {
1371 CopyMem (&NewDevPtr
[Index2
+ 1], &NewDevPtr
[Index2
], (*Idx
- Index2
) * sizeof (UINT16
));
1372 NewDevPtr
[Index2
] = (UINT16
) (Index
& 0xFF);
1378 FreePool (DevOrder
);
1380 Status
= gRT
->SetVariable (
1381 VAR_LEGACY_DEV_ORDER
,
1382 &gEfiLegacyDevOrderVariableGuid
,
1383 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1387 FreePool (NewDevOrder
);
1393 Set Boot Priority for specified device type.
1395 @param DeviceType The device type.
1396 @param BbsIndex The BBS index to set the highest priority. Ignore when -1.
1397 @param LocalBbsTable The BBS table.
1398 @param Priority The prority table.
1400 @retval EFI_SUCCESS The function completes successfully.
1401 @retval EFI_NOT_FOUND Failed to find device.
1402 @retval EFI_OUT_OF_RESOURCES Failed to get the efi variable of device order.
1406 BdsSetBootPriority4SameTypeDev (
1407 IN UINT16 DeviceType
,
1409 IN OUT BBS_TABLE
*LocalBbsTable
,
1410 IN OUT UINT16
*Priority
1413 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1414 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
1418 DevOrder
= BdsLibGetVariableAndSize (
1419 VAR_LEGACY_DEV_ORDER
,
1420 &gEfiLegacyDevOrderVariableGuid
,
1423 if (NULL
== DevOrder
) {
1424 return EFI_OUT_OF_RESOURCES
;
1427 DevOrderPtr
= DevOrder
;
1428 while ((UINT8
*) DevOrderPtr
< (UINT8
*) DevOrder
+ DevOrderSize
) {
1429 if (DevOrderPtr
->BbsType
== DeviceType
) {
1433 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) ((UINTN
) DevOrderPtr
+ sizeof (BBS_TYPE
) + DevOrderPtr
->Length
);
1436 if ((UINT8
*) DevOrderPtr
>= (UINT8
*) DevOrder
+ DevOrderSize
) {
1437 FreePool (DevOrder
);
1438 return EFI_NOT_FOUND
;
1441 if (BbsIndex
!= (UINTN
) -1) {
1442 LocalBbsTable
[BbsIndex
].BootPriority
= *Priority
;
1446 // If the high byte of the DevIndex is 0xFF, it indicates that this device has been disabled.
1448 for (Index
= 0; Index
< DevOrderPtr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1449 if ((DevOrderPtr
->Data
[Index
] & 0xFF00) == 0xFF00) {
1451 // LocalBbsTable[DevIndex[Index] & 0xFF].BootPriority = BBS_DISABLED_ENTRY;
1453 } else if (DevOrderPtr
->Data
[Index
] != BbsIndex
) {
1454 LocalBbsTable
[DevOrderPtr
->Data
[Index
]].BootPriority
= *Priority
;
1459 FreePool (DevOrder
);
1464 Print the BBS Table.
1466 @param LocalBbsTable The BBS table.
1467 @param BbsCount The count of entry in BBS table.
1471 IN BBS_TABLE
*LocalBbsTable
,
1477 DEBUG ((DEBUG_ERROR
, "\n"));
1478 DEBUG ((DEBUG_ERROR
, " NO Prio bb/dd/ff cl/sc Type Stat segm:offs\n"));
1479 DEBUG ((DEBUG_ERROR
, "=============================================\n"));
1480 for (Idx
= 0; Idx
< BbsCount
; Idx
++) {
1481 if ((LocalBbsTable
[Idx
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1482 (LocalBbsTable
[Idx
].BootPriority
== BBS_DO_NOT_BOOT_FROM
) ||
1483 (LocalBbsTable
[Idx
].BootPriority
== BBS_LOWEST_PRIORITY
)
1490 " %02x: %04x %02x/%02x/%02x %02x/%02x %04x %04x %04x:%04x\n",
1492 (UINTN
) LocalBbsTable
[Idx
].BootPriority
,
1493 (UINTN
) LocalBbsTable
[Idx
].Bus
,
1494 (UINTN
) LocalBbsTable
[Idx
].Device
,
1495 (UINTN
) LocalBbsTable
[Idx
].Function
,
1496 (UINTN
) LocalBbsTable
[Idx
].Class
,
1497 (UINTN
) LocalBbsTable
[Idx
].SubClass
,
1498 (UINTN
) LocalBbsTable
[Idx
].DeviceType
,
1499 (UINTN
) * (UINT16
*) &LocalBbsTable
[Idx
].StatusFlags
,
1500 (UINTN
) LocalBbsTable
[Idx
].BootHandlerSegment
,
1501 (UINTN
) LocalBbsTable
[Idx
].BootHandlerOffset
,
1502 (UINTN
) ((LocalBbsTable
[Idx
].MfgStringSegment
<< 4) + LocalBbsTable
[Idx
].MfgStringOffset
),
1503 (UINTN
) ((LocalBbsTable
[Idx
].DescStringSegment
<< 4) + LocalBbsTable
[Idx
].DescStringOffset
))
1507 DEBUG ((DEBUG_ERROR
, "\n"));
1511 Set the boot priority for BBS entries based on boot option entry and boot order.
1513 @param Entry The boot option is to be checked for refresh BBS table.
1515 @retval EFI_SUCCESS The boot priority for BBS entries is refreshed successfully.
1516 @retval EFI_NOT_FOUND BBS entries can't be found.
1517 @retval EFI_OUT_OF_RESOURCES Failed to get the legacy device boot order.
1521 BdsRefreshBbsTableForBoot (
1522 IN BDS_COMMON_OPTION
*Entry
1529 HDD_INFO
*LocalHddInfo
;
1530 BBS_TABLE
*LocalBbsTable
;
1532 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1536 UINTN BootOrderSize
;
1537 UINT8
*BootOptionVar
;
1538 UINTN BootOptionSize
;
1539 CHAR16 BootOption
[9];
1542 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1544 UINTN DeviceTypeCount
;
1545 UINTN DeviceTypeIndex
;
1549 LocalHddInfo
= NULL
;
1550 LocalBbsTable
= NULL
;
1551 DevType
= BBS_UNKNOWN
;
1553 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1554 if (EFI_ERROR (Status
)) {
1558 LegacyBios
->GetBbsInfo (
1566 // First, set all the present devices' boot priority to BBS_UNPRIORITIZED_ENTRY
1567 // We will set them according to the settings setup by user
1569 for (Index
= 0; Index
< BbsCount
; Index
++) {
1570 if (!((BBS_IGNORE_ENTRY
== LocalBbsTable
[Index
].BootPriority
) ||
1571 (BBS_DO_NOT_BOOT_FROM
== LocalBbsTable
[Index
].BootPriority
) ||
1572 (BBS_LOWEST_PRIORITY
== LocalBbsTable
[Index
].BootPriority
))) {
1573 LocalBbsTable
[Index
].BootPriority
= BBS_UNPRIORITIZED_ENTRY
;
1577 // boot priority always starts at 0
1580 if (Entry
->LoadOptionsSize
== sizeof (BBS_TABLE
) + sizeof (UINT16
)) {
1582 // If Entry stands for a legacy boot option, we prioritize the devices with the same type first.
1584 DevType
= ((BBS_TABLE
*) Entry
->LoadOptions
)->DeviceType
;
1585 BbsIndex
= *(UINT16
*) ((BBS_TABLE
*) Entry
->LoadOptions
+ 1);
1586 Status
= BdsSetBootPriority4SameTypeDev (
1592 if (EFI_ERROR (Status
)) {
1597 // we have to set the boot priority for other BBS entries with different device types
1599 BootOrder
= BdsLibGetVariableAndSize (
1601 &gEfiGlobalVariableGuid
,
1604 DeviceType
= AllocatePool (BootOrderSize
+ sizeof (UINT16
));
1605 ASSERT (DeviceType
!= NULL
);
1607 DeviceType
[0] = DevType
;
1608 DeviceTypeCount
= 1;
1609 for (Index
= 0; ((BootOrder
!= NULL
) && (Index
< BootOrderSize
/ sizeof (UINT16
))); Index
++) {
1610 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
1611 BootOptionVar
= BdsLibGetVariableAndSize (
1613 &gEfiGlobalVariableGuid
,
1616 if (NULL
== BootOptionVar
) {
1620 Ptr
= BootOptionVar
;
1622 Ptr
+= sizeof (UINT32
);
1623 DevPathLen
= *(UINT16
*) Ptr
;
1624 Ptr
+= sizeof (UINT16
);
1625 Ptr
+= StrSize ((UINT16
*) Ptr
);
1626 DevPath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
1627 if (BBS_DEVICE_PATH
!= DevPath
->Type
|| BBS_BBS_DP
!= DevPath
->SubType
) {
1628 FreePool (BootOptionVar
);
1633 DevType
= ((BBS_TABLE
*) Ptr
)->DeviceType
;
1634 for (DeviceTypeIndex
= 0; DeviceTypeIndex
< DeviceTypeCount
; DeviceTypeIndex
++) {
1635 if (DeviceType
[DeviceTypeIndex
] == DevType
) {
1639 if (DeviceTypeIndex
< DeviceTypeCount
) {
1641 // We don't want to process twice for a device type
1643 FreePool (BootOptionVar
);
1647 DeviceType
[DeviceTypeCount
] = DevType
;
1650 Status
= BdsSetBootPriority4SameTypeDev (
1656 FreePool (BootOptionVar
);
1657 if (EFI_ERROR (Status
)) {
1662 FreePool (DeviceType
);
1664 if (BootOrder
!= NULL
) {
1665 FreePool (BootOrder
);
1669 PrintBbsTable (LocalBbsTable
, BbsCount
);
1676 Boot the legacy system with the boot option
1678 @param Option The legacy boot option which have BBS device path
1680 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
1682 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
1686 BdsLibDoLegacyBoot (
1687 IN BDS_COMMON_OPTION
*Option
1691 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1692 EFI_EVENT LegacyBootEvent
;
1694 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1695 if (EFI_ERROR (Status
)) {
1697 // If no LegacyBios protocol we do not support legacy boot
1699 return EFI_UNSUPPORTED
;
1702 // Notes: if we separate the int 19, then we don't need to refresh BBS
1704 BdsRefreshBbsTableForBoot (Option
);
1707 // Write boot to OS performance data for legacy boot.
1711 // Create an event to be signalled when Legacy Boot occurs to write performance data.
1713 Status
= EfiCreateEventLegacyBootEx(
1719 ASSERT_EFI_ERROR (Status
);
1722 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
1723 return LegacyBios
->LegacyBoot (
1725 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
1726 Option
->LoadOptionsSize
,
1732 Internal function to check if the input boot option is a valid EFI NV Boot####.
1734 @param OptionToCheck Boot option to be checked.
1736 @retval TRUE This boot option matches a valid EFI NV Boot####.
1737 @retval FALSE If not.
1741 IsBootOptionValidNVVarialbe (
1742 IN BDS_COMMON_OPTION
*OptionToCheck
1745 LIST_ENTRY TempList
;
1746 BDS_COMMON_OPTION
*BootOption
;
1748 CHAR16 OptionName
[20];
1752 InitializeListHead (&TempList
);
1753 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
1755 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
1756 if (BootOption
== NULL
) {
1761 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
1762 // valid EFI NV Boot####.
1764 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
1765 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
1770 FreePool (BootOption
);
1776 Check whether a USB device match the specified USB Class device path. This
1777 function follows "Load Option Processing" behavior in UEFI specification.
1779 @param UsbIo USB I/O protocol associated with the USB device.
1780 @param UsbClass The USB Class device path to match.
1782 @retval TRUE The USB device match the USB Class device path.
1783 @retval FALSE The USB device does not match the USB Class device path.
1788 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1789 IN USB_CLASS_DEVICE_PATH
*UsbClass
1793 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1794 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1796 UINT8 DeviceSubClass
;
1797 UINT8 DeviceProtocol
;
1799 if ((DevicePathType (UsbClass
) != MESSAGING_DEVICE_PATH
) ||
1800 (DevicePathSubType (UsbClass
) != MSG_USB_CLASS_DP
)){
1805 // Check Vendor Id and Product Id.
1807 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1808 if (EFI_ERROR (Status
)) {
1812 if ((UsbClass
->VendorId
!= 0xffff) &&
1813 (UsbClass
->VendorId
!= DevDesc
.IdVendor
)) {
1817 if ((UsbClass
->ProductId
!= 0xffff) &&
1818 (UsbClass
->ProductId
!= DevDesc
.IdProduct
)) {
1822 DeviceClass
= DevDesc
.DeviceClass
;
1823 DeviceSubClass
= DevDesc
.DeviceSubClass
;
1824 DeviceProtocol
= DevDesc
.DeviceProtocol
;
1825 if (DeviceClass
== 0) {
1827 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
1828 // Protocol in Interface Descriptor instead.
1830 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1831 if (EFI_ERROR (Status
)) {
1835 DeviceClass
= IfDesc
.InterfaceClass
;
1836 DeviceSubClass
= IfDesc
.InterfaceSubClass
;
1837 DeviceProtocol
= IfDesc
.InterfaceProtocol
;
1841 // Check Class, SubClass and Protocol.
1843 if ((UsbClass
->DeviceClass
!= 0xff) &&
1844 (UsbClass
->DeviceClass
!= DeviceClass
)) {
1848 if ((UsbClass
->DeviceSubClass
!= 0xff) &&
1849 (UsbClass
->DeviceSubClass
!= DeviceSubClass
)) {
1853 if ((UsbClass
->DeviceProtocol
!= 0xff) &&
1854 (UsbClass
->DeviceProtocol
!= DeviceProtocol
)) {
1862 Check whether a USB device match the specified USB WWID device path. This
1863 function follows "Load Option Processing" behavior in UEFI specification.
1865 @param UsbIo USB I/O protocol associated with the USB device.
1866 @param UsbWwid The USB WWID device path to match.
1868 @retval TRUE The USB device match the USB WWID device path.
1869 @retval FALSE The USB device does not match the USB WWID device path.
1874 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1875 IN USB_WWID_DEVICE_PATH
*UsbWwid
1879 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1880 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1881 UINT16
*LangIdTable
;
1886 CHAR16
*SerialNumberStr
;
1889 if ((DevicePathType (UsbWwid
) != MESSAGING_DEVICE_PATH
) ||
1890 (DevicePathSubType (UsbWwid
) != MSG_USB_WWID_DP
)){
1895 // Check Vendor Id and Product Id.
1897 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1898 if (EFI_ERROR (Status
)) {
1901 if ((DevDesc
.IdVendor
!= UsbWwid
->VendorId
) ||
1902 (DevDesc
.IdProduct
!= UsbWwid
->ProductId
)) {
1907 // Check Interface Number.
1909 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1910 if (EFI_ERROR (Status
)) {
1913 if (IfDesc
.InterfaceNumber
!= UsbWwid
->InterfaceNumber
) {
1918 // Check Serial Number.
1920 if (DevDesc
.StrSerialNumber
== 0) {
1925 // Get all supported languages.
1929 Status
= UsbIo
->UsbGetSupportedLanguages (UsbIo
, &LangIdTable
, &TableSize
);
1930 if (EFI_ERROR (Status
) || (TableSize
== 0) || (LangIdTable
== NULL
)) {
1935 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
1937 CompareStr
= (CHAR16
*) (UINTN
) (UsbWwid
+ 1);
1938 CompareLen
= (DevicePathNodeLength (UsbWwid
) - sizeof (USB_WWID_DEVICE_PATH
)) / sizeof (CHAR16
);
1939 if (CompareStr
[CompareLen
- 1] == L
'\0') {
1944 // Compare serial number in each supported language.
1946 for (Index
= 0; Index
< TableSize
/ sizeof (UINT16
); Index
++) {
1947 SerialNumberStr
= NULL
;
1948 Status
= UsbIo
->UsbGetStringDescriptor (
1951 DevDesc
.StrSerialNumber
,
1954 if (EFI_ERROR (Status
) || (SerialNumberStr
== NULL
)) {
1958 Length
= StrLen (SerialNumberStr
);
1959 if ((Length
>= CompareLen
) &&
1960 (CompareMem (SerialNumberStr
+ Length
- CompareLen
, CompareStr
, CompareLen
* sizeof (CHAR16
)) == 0)) {
1961 FreePool (SerialNumberStr
);
1965 FreePool (SerialNumberStr
);
1972 Find a USB device path which match the specified short-form device path start
1973 with USB Class or USB WWID device path and load the boot file then return the
1974 image handle. If ParentDevicePath is NULL, this function will search in all USB
1975 devices of the platform. If ParentDevicePath is not NULL,this function will only
1976 search in its child devices.
1978 @param ParentDevicePath The device path of the parent.
1979 @param ShortFormDevicePath The USB Class or USB WWID device path to match.
1981 @return The image Handle if find load file from specified short-form device path
1982 or NULL if not found.
1987 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
1988 IN EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
1992 UINTN UsbIoHandleCount
;
1993 EFI_HANDLE
*UsbIoHandleBuffer
;
1994 EFI_DEVICE_PATH_PROTOCOL
*UsbIoDevicePath
;
1995 EFI_USB_IO_PROTOCOL
*UsbIo
;
1999 EFI_HANDLE ImageHandle
;
2001 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
2002 EFI_DEVICE_PATH_PROTOCOL
*NextDevicePath
;
2004 FullDevicePath
= NULL
;
2008 // Get all UsbIo Handles.
2010 UsbIoHandleCount
= 0;
2011 UsbIoHandleBuffer
= NULL
;
2012 Status
= gBS
->LocateHandleBuffer (
2014 &gEfiUsbIoProtocolGuid
,
2019 if (EFI_ERROR (Status
) || (UsbIoHandleCount
== 0) || (UsbIoHandleBuffer
== NULL
)) {
2023 ParentSize
= (ParentDevicePath
== NULL
) ? 0 : GetDevicePathSize (ParentDevicePath
);
2024 for (Index
= 0; Index
< UsbIoHandleCount
; Index
++) {
2026 // Get the Usb IO interface.
2028 Status
= gBS
->HandleProtocol(
2029 UsbIoHandleBuffer
[Index
],
2030 &gEfiUsbIoProtocolGuid
,
2033 if (EFI_ERROR (Status
)) {
2037 UsbIoDevicePath
= DevicePathFromHandle (UsbIoHandleBuffer
[Index
]);
2038 if (UsbIoDevicePath
== NULL
) {
2042 if (ParentDevicePath
!= NULL
) {
2044 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
2046 Size
= GetDevicePathSize (UsbIoDevicePath
);
2047 if ((Size
< ParentSize
) ||
2048 (CompareMem (UsbIoDevicePath
, ParentDevicePath
, ParentSize
- END_DEVICE_PATH_LENGTH
) != 0)) {
2053 if (BdsMatchUsbClass (UsbIo
, (USB_CLASS_DEVICE_PATH
*) ShortFormDevicePath
) ||
2054 BdsMatchUsbWwid (UsbIo
, (USB_WWID_DEVICE_PATH
*) ShortFormDevicePath
)) {
2056 // Try to find if there is the boot file in this DevicePath
2058 NextDevicePath
= NextDevicePathNode (ShortFormDevicePath
);
2059 if (!IsDevicePathEnd (NextDevicePath
)) {
2060 FullDevicePath
= AppendDevicePath (UsbIoDevicePath
, NextDevicePath
);
2062 // Connect the full device path, so that Simple File System protocol
2063 // could be installed for this USB device.
2065 BdsLibConnectDevicePath (FullDevicePath
);
2066 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2067 Status
= gBS
->LoadImage (
2075 FreePool (FullDevicePath
);
2077 FullDevicePath
= UsbIoDevicePath
;
2078 Status
= EFI_NOT_FOUND
;
2082 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2083 // and load the image according to the default boot behavior for removable device.
2085 if (EFI_ERROR (Status
)) {
2087 // check if there is a bootable removable media could be found in this device path ,
2088 // and get the bootable media handle
2090 Handle
= BdsLibGetBootableHandle(UsbIoDevicePath
);
2091 if (Handle
== NULL
) {
2095 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2096 // machinename is ia32, ia64, x64, ...
2098 FullDevicePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2099 if (FullDevicePath
!= NULL
) {
2100 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2101 Status
= gBS
->LoadImage (
2109 if (EFI_ERROR (Status
)) {
2111 // The DevicePath failed, and it's not a valid
2112 // removable media device.
2124 FreePool (UsbIoHandleBuffer
);
2129 Expand USB Class or USB WWID device path node to be full device path of a USB
2130 device in platform then load the boot file on this full device path and return the
2133 This function support following 4 cases:
2134 1) Boot Option device path starts with a USB Class or USB WWID device path,
2135 and there is no Media FilePath device path in the end.
2136 In this case, it will follow Removable Media Boot Behavior.
2137 2) Boot Option device path starts with a USB Class or USB WWID device path,
2138 and ended with Media FilePath device path.
2139 3) Boot Option device path starts with a full device path to a USB Host Controller,
2140 contains a USB Class or USB WWID device path node, while not ended with Media
2141 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
2142 4) Boot Option device path starts with a full device path to a USB Host Controller,
2143 contains a USB Class or USB WWID device path node, and ended with Media
2144 FilePath device path.
2146 @param DevicePath The Boot Option device path.
2148 @return The image handle of boot file, or NULL if there is no boot file found in
2149 the specified USB Class or USB WWID device path.
2153 BdsExpandUsbShortFormDevicePath (
2154 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
2157 EFI_HANDLE
*ImageHandle
;
2158 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2159 EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
;
2162 // Search for USB Class or USB WWID device path node.
2164 ShortFormDevicePath
= NULL
;
2166 TempDevicePath
= DevicePath
;
2167 while (!IsDevicePathEnd (TempDevicePath
)) {
2168 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
2169 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
2170 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
2171 ShortFormDevicePath
= TempDevicePath
;
2174 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2177 if (ShortFormDevicePath
== NULL
) {
2179 // No USB Class or USB WWID device path node found, do nothing.
2184 if (ShortFormDevicePath
== DevicePath
) {
2186 // Boot Option device path starts with USB Class or USB WWID device path.
2188 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2189 if (ImageHandle
== NULL
) {
2191 // Failed to find a match in existing devices, connect the short form USB
2192 // device path and try again.
2194 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath
);
2195 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2199 // Boot Option device path contains USB Class or USB WWID device path node.
2203 // Prepare the parent device path for search.
2205 TempDevicePath
= DuplicateDevicePath (DevicePath
);
2206 ASSERT (TempDevicePath
!= NULL
);
2207 SetDevicePathEndNode (((UINT8
*) TempDevicePath
) + ((UINTN
) ShortFormDevicePath
- (UINTN
) DevicePath
));
2210 // The USB Host Controller device path is already in Boot Option device path
2211 // and USB Bus driver already support RemainingDevicePath starts with USB
2212 // Class or USB WWID device path, so just search in existing USB devices and
2213 // doesn't perform ConnectController here.
2215 ImageHandle
= BdsFindUsbDevice (TempDevicePath
, ShortFormDevicePath
);
2216 FreePool (TempDevicePath
);
2223 Process the boot option follow the UEFI specification and
2224 special treat the legacy boot option with BBS_DEVICE_PATH.
2226 @param Option The boot option need to be processed
2227 @param DevicePath The device path which describe where to load the
2228 boot image or the legacy BBS device path to boot
2230 @param ExitDataSize The size of exit data.
2231 @param ExitData Data returned when Boot image failed.
2233 @retval EFI_SUCCESS Boot from the input boot option successfully.
2234 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2239 BdsLibBootViaBootOption (
2240 IN BDS_COMMON_OPTION
*Option
,
2241 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2242 OUT UINTN
*ExitDataSize
,
2243 OUT CHAR16
**ExitData OPTIONAL
2247 EFI_STATUS StatusLogo
;
2249 EFI_HANDLE ImageHandle
;
2250 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
2251 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
2252 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
2253 LIST_ENTRY TempBootLists
;
2254 EFI_BOOT_LOGO_PROTOCOL
*BootLogo
;
2256 Status
= EFI_SUCCESS
;
2261 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
2264 WorkingDevicePath
= NULL
;
2265 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2266 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
2267 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
2268 (HARDDRIVE_DEVICE_PATH
*)DevicePath
2270 if (WorkingDevicePath
!= NULL
) {
2271 DevicePath
= WorkingDevicePath
;
2278 if (IsBootOptionValidNVVarialbe (Option
)) {
2280 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
2281 // In this case, "BootCurrent" is not created.
2282 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
2284 SetVariableAndReportStatusCodeOnError (
2286 &gEfiGlobalVariableGuid
,
2287 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2289 &Option
->BootCurrent
2294 // Signal the EVT_SIGNAL_READY_TO_BOOT event
2296 EfiSignalEventReadyToBoot();
2299 // Report Status Code to indicate ReadyToBoot event was signalled
2301 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT
));
2304 // Expand USB Class or USB WWID device path node to be full device path of a USB
2305 // device in platform then load the boot file on this full device path and get the
2308 ImageHandle
= BdsExpandUsbShortFormDevicePath (DevicePath
);
2311 // Adjust the different type memory page number just before booting
2312 // and save the updated info into the variable for next boot to use
2314 BdsSetMemoryTypeInformationVariable ();
2317 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.
2318 // Here get the ImageHandle for the non USB class or WWID device path.
2320 if (ImageHandle
== NULL
) {
2321 ASSERT (Option
->DevicePath
!= NULL
);
2322 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
2323 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
2326 // Check to see if we should legacy BOOT. If yes then do the legacy boot
2328 return BdsLibDoLegacyBoot (Option
);
2332 // If the boot option point to Internal FV shell, make sure it is valid
2334 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
2335 if (!EFI_ERROR(Status
)) {
2336 if (Option
->DevicePath
!= NULL
) {
2337 FreePool(Option
->DevicePath
);
2339 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
2340 ASSERT(Option
->DevicePath
!= NULL
);
2341 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
2343 // Update the shell boot option
2345 InitializeListHead (&TempBootLists
);
2346 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
2349 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
2351 FreePool (DevicePath
);
2352 DevicePath
= Option
->DevicePath
;
2357 if (Option
->Description
== NULL
) {
2358 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
2360 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
2366 // Report status code for OS Loader LoadImage.
2368 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2369 Status
= gBS
->LoadImage (
2379 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2380 // and load the image according to the default boot behavior for removable device.
2382 if (EFI_ERROR (Status
)) {
2384 // check if there is a bootable removable media could be found in this device path ,
2385 // and get the bootable media handle
2387 Handle
= BdsLibGetBootableHandle(DevicePath
);
2388 if (Handle
!= NULL
) {
2390 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2391 // machinename is ia32, ia64, x64, ...
2393 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2394 if (FilePath
!= NULL
) {
2395 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2396 Status
= gBS
->LoadImage (
2409 // Provide the image with it's load options
2411 if ((ImageHandle
== NULL
) || (EFI_ERROR(Status
))) {
2413 // Report Status Code to indicate that the failure to load boot option
2415 REPORT_STATUS_CODE (
2416 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2417 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR
)
2422 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
2423 ASSERT_EFI_ERROR (Status
);
2425 if (Option
->LoadOptionsSize
!= 0) {
2426 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
2427 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
2431 // Clean to NULL because the image is loaded directly from the firmwares boot manager.
2433 ImageInfo
->ParentHandle
= NULL
;
2436 // Before calling the image, enable the Watchdog Timer for
2437 // the 5 Minute period
2439 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
2442 // Write boot to OS performance data for UEFI boot
2445 BmEndOfBdsPerfCode (NULL
, NULL
);
2449 // Report status code for OS Loader StartImage.
2451 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderStart
));
2453 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
2454 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
2455 if (EFI_ERROR (Status
)) {
2457 // Report Status Code to indicate that boot failure
2459 REPORT_STATUS_CODE (
2460 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2461 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED
)
2466 // Clear the Watchdog Timer after the image returns
2468 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
2472 // Set Logo status invalid after trying one boot option
2475 StatusLogo
= gBS
->LocateProtocol (&gEfiBootLogoProtocolGuid
, NULL
, (VOID
**) &BootLogo
);
2476 if (!EFI_ERROR (StatusLogo
) && (BootLogo
!= NULL
)) {
2477 BootLogo
->SetBootLogo (BootLogo
, NULL
, 0, 0, 0, 0);
2481 // Clear Boot Current
2482 // Deleting variable with current implementation shouldn't fail.
2486 &gEfiGlobalVariableGuid
,
2487 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2497 Expand a device path that starts with a hard drive media device path node to be a
2498 full device path that includes the full hardware path to the device. We need
2499 to do this so it can be booted. As an optimization the front match (the part point
2500 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
2501 so a connect all is not required on every boot. All successful history device path
2502 which point to partition node (the front part) will be saved.
2504 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
2505 drive media device path.
2506 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
2510 EFI_DEVICE_PATH_PROTOCOL
*
2512 BdsExpandPartitionPartialDevicePathToFull (
2513 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2517 UINTN BlockIoHandleCount
;
2518 EFI_HANDLE
*BlockIoBuffer
;
2519 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
2520 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
2521 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2524 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
2525 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
2526 UINTN CachedDevicePathSize
;
2527 BOOLEAN DeviceExist
;
2529 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
2532 FullDevicePath
= NULL
;
2534 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2535 // If exist, search the front path which point to partition node in the variable instants.
2536 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system
2539 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2540 &gHdBootDevicePathVariablGuid
,
2541 (VOID
**) &CachedDevicePath
,
2542 &CachedDevicePathSize
2546 // Delete the invalid HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2548 if ((CachedDevicePath
!= NULL
) && !IsDevicePathValid (CachedDevicePath
, CachedDevicePathSize
)) {
2549 FreePool (CachedDevicePath
);
2550 CachedDevicePath
= NULL
;
2551 Status
= gRT
->SetVariable (
2552 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2553 &gHdBootDevicePathVariablGuid
,
2558 ASSERT_EFI_ERROR (Status
);
2561 if (CachedDevicePath
!= NULL
) {
2562 TempNewDevicePath
= CachedDevicePath
;
2563 DeviceExist
= FALSE
;
2567 // Check every instance of the variable
2568 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
2569 // partial partition boot option. Second, check whether the instance could be connected.
2571 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
2572 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
2574 // Connect the device path instance, the device path point to hard drive media device path node
2575 // e.g. ACPI() /PCI()/ATA()/Partition()
2577 Status
= BdsLibConnectDevicePath (Instance
);
2578 if (!EFI_ERROR (Status
)) {
2584 // Come here means the first instance is not matched
2588 } while (TempNewDevicePath
!= NULL
);
2592 // Find the matched device path.
2593 // Append the file path information from the boot option and return the fully expanded device path.
2595 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2596 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
2599 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.
2603 // First delete the matched instance.
2605 TempNewDevicePath
= CachedDevicePath
;
2606 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
2607 FreePool (TempNewDevicePath
);
2610 // Second, append the remaining path after the matched instance
2612 TempNewDevicePath
= CachedDevicePath
;
2613 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
2614 FreePool (TempNewDevicePath
);
2616 // Save the matching Device Path so we don't need to do a connect all next time
2617 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2619 Status
= gRT
->SetVariable (
2620 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2621 &gHdBootDevicePathVariablGuid
,
2622 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2623 GetDevicePathSize (CachedDevicePath
),
2628 FreePool (Instance
);
2629 FreePool (CachedDevicePath
);
2630 return FullDevicePath
;
2635 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need
2636 // to search all devices in the system for a matched partition
2638 BdsLibConnectAllDriversToAllControllers ();
2639 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
2640 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
2642 // If there was an error or there are no device handles that support
2643 // the BLOCK_IO Protocol, then return.
2648 // Loop through all the device handles that support the BLOCK_IO Protocol
2650 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
2652 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
2653 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
2657 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
2659 // Find the matched partition device path
2661 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2662 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
2665 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2667 if (CachedDevicePath
!= NULL
) {
2669 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2671 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
2672 TempNewDevicePath
= CachedDevicePath
;
2673 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
2674 FreePool(TempNewDevicePath
);
2677 if (CachedDevicePath
!= NULL
) {
2678 TempNewDevicePath
= CachedDevicePath
;
2679 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
2680 FreePool(TempNewDevicePath
);
2682 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2686 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
2687 // If the user try to boot many OS in different HDs or partitions, in theory,
2688 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.
2691 ASSERT (CachedDevicePath
!= NULL
);
2692 TempNewDevicePath
= CachedDevicePath
;
2693 while (!IsDevicePathEnd (TempNewDevicePath
)) {
2694 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2696 // Parse one instance
2698 while (!IsDevicePathEndType (TempNewDevicePath
)) {
2699 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2703 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
2705 if (InstanceNum
>= 12) {
2706 SetDevicePathEndNode (TempNewDevicePath
);
2711 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2715 // Save the matching Device Path so we don't need to do a connect all next time
2716 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2718 Status
= gRT
->SetVariable (
2719 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2720 &gHdBootDevicePathVariablGuid
,
2721 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2722 GetDevicePathSize (CachedDevicePath
),
2730 if (CachedDevicePath
!= NULL
) {
2731 FreePool (CachedDevicePath
);
2733 if (BlockIoBuffer
!= NULL
) {
2734 FreePool (BlockIoBuffer
);
2736 return FullDevicePath
;
2740 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
2741 instances, has the same partition node with HardDriveDevicePath device path
2743 @param BlockIoDevicePath Multi device path instances which need to check
2744 @param HardDriveDevicePath A device path which starts with a hard drive media
2747 @retval TRUE There is a matched device path instance.
2748 @retval FALSE There is no matched device path instance.
2753 MatchPartitionDevicePathNode (
2754 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
2755 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2758 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
2759 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2761 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
2763 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
2768 // Make PreviousDevicePath == the device path node before the end node
2770 DevicePath
= BlockIoDevicePath
;
2771 BlockIoHdDevicePathNode
= NULL
;
2774 // find the partition device path node
2776 while (!IsDevicePathEnd (DevicePath
)) {
2777 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2778 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
2780 BlockIoHdDevicePathNode
= DevicePath
;
2784 DevicePath
= NextDevicePathNode (DevicePath
);
2787 if (BlockIoHdDevicePathNode
== NULL
) {
2791 // See if the harddrive device path in blockio matches the orig Hard Drive Node
2793 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
2797 // Check for the match
2799 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
2800 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
2801 switch (TmpHdPath
->SignatureType
) {
2802 case SIGNATURE_TYPE_GUID
:
2803 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
2805 case SIGNATURE_TYPE_MBR
:
2806 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
2818 Delete the boot option associated with the handle passed in.
2820 @param Handle The handle which present the device path to create
2823 @retval EFI_SUCCESS Delete the boot option success
2824 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2825 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2826 @retval Other Error return value from SetVariable()
2830 BdsLibDeleteOptionFromHandle (
2831 IN EFI_HANDLE Handle
2835 UINT8
*BootOptionVar
;
2836 UINTN BootOrderSize
;
2837 UINTN BootOptionSize
;
2840 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2841 UINTN DevicePathSize
;
2842 UINTN OptionDevicePathSize
;
2843 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2844 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2847 Status
= EFI_SUCCESS
;
2852 // Check "BootOrder" variable, if no, means there is no any boot order.
2854 BootOrder
= BdsLibGetVariableAndSize (
2856 &gEfiGlobalVariableGuid
,
2859 if (BootOrder
== NULL
) {
2860 return EFI_NOT_FOUND
;
2864 // Convert device handle to device path protocol instance
2866 DevicePath
= DevicePathFromHandle (Handle
);
2867 if (DevicePath
== NULL
) {
2868 return EFI_NOT_FOUND
;
2870 DevicePathSize
= GetDevicePathSize (DevicePath
);
2873 // Loop all boot order variable and find the matching device path
2876 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2877 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2878 BootOptionVar
= BdsLibGetVariableAndSize (
2880 &gEfiGlobalVariableGuid
,
2884 if (BootOptionVar
== NULL
) {
2885 FreePool (BootOrder
);
2886 return EFI_OUT_OF_RESOURCES
;
2889 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2890 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2891 FreePool (BootOptionVar
);
2896 TempPtr
= BootOptionVar
;
2897 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
2898 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
2899 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
2900 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
2903 // Check whether the device path match
2905 if ((OptionDevicePathSize
== DevicePathSize
) &&
2906 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
2907 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2908 FreePool (BootOptionVar
);
2912 FreePool (BootOptionVar
);
2917 // Adjust number of boot option for "BootOrder" variable.
2919 Status
= gRT
->SetVariable (
2921 &gEfiGlobalVariableGuid
,
2922 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2927 // Shrinking variable with existing variable implementation shouldn't fail.
2929 ASSERT_EFI_ERROR (Status
);
2931 FreePool (BootOrder
);
2938 Delete all invalid EFI boot options.
2940 @retval EFI_SUCCESS Delete all invalid boot option success
2941 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
2942 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2943 @retval Other Error return value from SetVariable()
2947 BdsDeleteAllInvalidEfiBootOption (
2952 UINT8
*BootOptionVar
;
2953 UINTN BootOrderSize
;
2954 UINTN BootOptionSize
;
2958 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2959 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2961 CHAR16
*Description
;
2964 Status
= EFI_SUCCESS
;
2967 OptionDevicePath
= NULL
;
2972 // Check "BootOrder" variable firstly, this variable hold the number of boot options
2974 BootOrder
= BdsLibGetVariableAndSize (
2976 &gEfiGlobalVariableGuid
,
2979 if (NULL
== BootOrder
) {
2980 return EFI_NOT_FOUND
;
2984 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2985 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2986 BootOptionVar
= BdsLibGetVariableAndSize (
2988 &gEfiGlobalVariableGuid
,
2991 if (NULL
== BootOptionVar
) {
2992 FreePool (BootOrder
);
2993 return EFI_OUT_OF_RESOURCES
;
2996 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2999 TempPtr
= BootOptionVar
;
3000 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
3001 Description
= (CHAR16
*) TempPtr
;
3002 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
3003 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
3006 // Skip legacy boot option (BBS boot device)
3008 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
3009 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
3010 FreePool (BootOptionVar
);
3016 if (Corrupted
|| !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
3018 // Delete this invalid boot option "Boot####"
3020 Status
= gRT
->SetVariable (
3022 &gEfiGlobalVariableGuid
,
3023 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3028 // Deleting variable with current variable implementation shouldn't fail.
3030 ASSERT_EFI_ERROR (Status
);
3032 // Mark this boot option in boot order as deleted
3034 BootOrder
[Index
] = 0xffff;
3038 FreePool (BootOptionVar
);
3043 // Adjust boot order array
3046 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
3047 if (BootOrder
[Index
] != 0xffff) {
3048 BootOrder
[Index2
] = BootOrder
[Index
];
3052 Status
= gRT
->SetVariable (
3054 &gEfiGlobalVariableGuid
,
3055 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3056 Index2
* sizeof (UINT16
),
3060 // Shrinking variable with current variable implementation shouldn't fail.
3062 ASSERT_EFI_ERROR (Status
);
3064 FreePool (BootOrder
);
3071 For EFI boot option, BDS separate them as six types:
3072 1. Network - The boot option points to the SimpleNetworkProtocol device.
3073 Bds will try to automatically create this type boot option when enumerate.
3074 2. Shell - The boot option points to internal flash shell.
3075 Bds will try to automatically create this type boot option when enumerate.
3076 3. Removable BlockIo - The boot option only points to the removable media
3077 device, like USB flash disk, DVD, Floppy etc.
3078 These device should contain a *removable* blockIo
3079 protocol in their device handle.
3080 Bds will try to automatically create this type boot option
3082 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
3084 These device should contain a *fixed* blockIo
3085 protocol in their device handle.
3086 BDS will skip fixed blockIo devices, and NOT
3087 automatically create boot option for them. But BDS
3088 will help to delete those fixed blockIo boot option,
3089 whose description rule conflict with other auto-created
3091 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
3092 has SimpleFileSystem Protocol, but has no blockio
3093 protocol. These devices do not offer blockIo
3094 protocol, but BDS still can get the
3095 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
3097 6. File - The boot option points to a file. These boot options are usually
3098 created by user manually or OS loader. BDS will not delete or modify
3101 This function will enumerate all possible boot device in the system, and
3102 automatically create boot options for Network, Shell, Removable BlockIo,
3103 and Non-BlockIo Simplefile devices.
3104 It will only execute once of every boot.
3106 @param BdsBootOptionList The header of the link list which indexed all
3107 current boot options
3109 @retval EFI_SUCCESS Finished all the boot device enumerate and create
3110 the boot option base on that boot device
3112 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
3116 BdsLibEnumerateAllBootOption (
3117 IN OUT LIST_ENTRY
*BdsBootOptionList
3121 UINT16 FloppyNumber
;
3122 UINT16 HarddriveNumber
;
3127 UINT16 NonBlockNumber
;
3128 UINTN NumberBlockIoHandles
;
3129 EFI_HANDLE
*BlockIoHandles
;
3130 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
3131 BOOLEAN Removable
[2];
3132 UINTN RemovableIndex
;
3134 UINTN NumOfLoadFileHandles
;
3135 EFI_HANDLE
*LoadFileHandles
;
3136 UINTN FvHandleCount
;
3137 EFI_HANDLE
*FvHandleBuffer
;
3138 EFI_FV_FILETYPE Type
;
3140 EFI_FV_FILE_ATTRIBUTES Attributes
;
3141 UINT32 AuthenticationStatus
;
3142 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
3143 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3144 UINTN DevicePathType
;
3146 EFI_HANDLE
*FileSystemHandles
;
3147 UINTN NumberFileSystemHandles
;
3149 EFI_IMAGE_DOS_HEADER DosHeader
;
3152 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3153 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3156 HarddriveNumber
= 0;
3163 ZeroMem (Buffer
, sizeof (Buffer
));
3166 // If the boot device enumerate happened, just get the boot
3167 // device from the boot order variable
3169 if (mEnumBootDevice
) {
3170 GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME
, &gLastEnumLangGuid
, (VOID
**)&LastLang
, NULL
);
3171 GetEfiGlobalVariable2 (L
"PlatformLang", (VOID
**)&PlatLang
, NULL
);
3172 ASSERT (PlatLang
!= NULL
);
3173 if ((LastLang
!= NULL
) && (AsciiStrCmp (LastLang
, PlatLang
) == 0)) {
3174 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3175 FreePool (LastLang
);
3176 FreePool (PlatLang
);
3179 Status
= gRT
->SetVariable (
3180 LAST_ENUM_LANGUAGE_VARIABLE_NAME
,
3182 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3183 AsciiStrSize (PlatLang
),
3187 // Failure to set the variable only impacts the performance next time enumerating the boot options.
3190 if (LastLang
!= NULL
) {
3191 FreePool (LastLang
);
3193 FreePool (PlatLang
);
3198 // Notes: this dirty code is to get the legacy boot option from the
3199 // BBS table and create to variable as the EFI boot option, it should
3200 // be removed after the CSM can provide legacy boot option directly
3202 REFRESH_LEGACY_BOOT_OPTIONS
;
3205 // Delete invalid boot option
3207 BdsDeleteAllInvalidEfiBootOption ();
3210 // Parse removable media followed by fixed media.
3211 // The Removable[] array is used by the for-loop below to create removable media boot options
3212 // at first, and then to create fixed media boot options.
3214 Removable
[0] = FALSE
;
3215 Removable
[1] = TRUE
;
3217 gBS
->LocateHandleBuffer (
3219 &gEfiBlockIoProtocolGuid
,
3221 &NumberBlockIoHandles
,
3225 for (RemovableIndex
= 0; RemovableIndex
< 2; RemovableIndex
++) {
3226 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
3227 Status
= gBS
->HandleProtocol (
3228 BlockIoHandles
[Index
],
3229 &gEfiBlockIoProtocolGuid
,
3233 // skip the logical partition
3235 if (EFI_ERROR (Status
) || BlkIo
->Media
->LogicalPartition
) {
3240 // firstly fixed block io then the removable block io
3242 if (BlkIo
->Media
->RemovableMedia
== Removable
[RemovableIndex
]) {
3245 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
3246 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
3248 switch (DevicePathType
) {
3249 case BDS_EFI_ACPI_FLOPPY_BOOT
:
3250 if (FloppyNumber
!= 0) {
3251 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
3253 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
3255 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3260 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.
3262 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
3263 case BDS_EFI_MESSAGE_SATA_BOOT
:
3264 if (BlkIo
->Media
->RemovableMedia
) {
3265 if (CdromNumber
!= 0) {
3266 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
3268 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
3272 if (HarddriveNumber
!= 0) {
3273 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)), HarddriveNumber
);
3275 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)));
3279 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
3280 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3283 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
3284 if (UsbNumber
!= 0) {
3285 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
3287 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
3289 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3293 case BDS_EFI_MESSAGE_SCSI_BOOT
:
3294 if (ScsiNumber
!= 0) {
3295 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
3297 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
3299 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3303 case BDS_EFI_MESSAGE_MISC_BOOT
:
3305 if (MiscNumber
!= 0) {
3306 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
3308 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
3310 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3317 if (NumberBlockIoHandles
!= 0) {
3318 FreePool (BlockIoHandles
);
3322 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
3325 gBS
->LocateHandleBuffer (
3327 &gEfiSimpleFileSystemProtocolGuid
,
3329 &NumberFileSystemHandles
,
3332 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
3333 Status
= gBS
->HandleProtocol (
3334 FileSystemHandles
[Index
],
3335 &gEfiBlockIoProtocolGuid
,
3338 if (!EFI_ERROR (Status
)) {
3340 // Skip if the file system handle supports a BlkIo protocol,
3346 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
3347 // machinename is ia32, ia64, x64, ...
3349 Hdr
.Union
= &HdrData
;
3351 Status
= BdsLibGetImageHeader (
3352 FileSystemHandles
[Index
],
3353 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3357 if (!EFI_ERROR (Status
) &&
3358 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3359 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3365 // No such file or the file is not a EFI application, delete this boot option
3367 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
3369 if (NonBlockNumber
!= 0) {
3370 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
3372 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
3374 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
3379 if (NumberFileSystemHandles
!= 0) {
3380 FreePool (FileSystemHandles
);
3384 // Parse Network Boot Device
3386 NumOfLoadFileHandles
= 0;
3388 // Search Load File protocol for PXE boot option.
3390 gBS
->LocateHandleBuffer (
3392 &gEfiLoadFileProtocolGuid
,
3394 &NumOfLoadFileHandles
,
3398 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
3400 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)), Index
);
3402 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)));
3404 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
3407 if (NumOfLoadFileHandles
!= 0) {
3408 FreePool (LoadFileHandles
);
3412 // Check if we have on flash shell
3414 gBS
->LocateHandleBuffer (
3416 &gEfiFirmwareVolume2ProtocolGuid
,
3421 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
3422 gBS
->HandleProtocol (
3423 FvHandleBuffer
[Index
],
3424 &gEfiFirmwareVolume2ProtocolGuid
,
3428 Status
= Fv
->ReadFile (
3430 PcdGetPtr(PcdShellFile
),
3435 &AuthenticationStatus
3437 if (EFI_ERROR (Status
)) {
3439 // Skip if no shell file in the FV
3444 // Build the shell boot option
3446 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
3449 if (FvHandleCount
!= 0) {
3450 FreePool (FvHandleBuffer
);
3453 // Make sure every boot only have one time
3454 // boot device enumerate
3456 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3457 mEnumBootDevice
= TRUE
;
3463 Build the boot option with the handle parsed in
3465 @param Handle The handle which present the device path to create
3467 @param BdsBootOptionList The header of the link list which indexed all
3468 current boot options
3469 @param String The description of the boot option.
3474 BdsLibBuildOptionFromHandle (
3475 IN EFI_HANDLE Handle
,
3476 IN LIST_ENTRY
*BdsBootOptionList
,
3480 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3482 DevicePath
= DevicePathFromHandle (Handle
);
3485 // Create and register new boot option
3487 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
3492 Build the on flash shell boot option with the handle parsed in.
3494 @param Handle The handle which present the device path to create
3495 on flash shell boot option
3496 @param BdsBootOptionList The header of the link list which indexed all
3497 current boot options
3502 BdsLibBuildOptionFromShell (
3503 IN EFI_HANDLE Handle
,
3504 IN OUT LIST_ENTRY
*BdsBootOptionList
3507 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3508 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
3510 DevicePath
= DevicePathFromHandle (Handle
);
3513 // Build the shell device path
3515 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
3517 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
3520 // Create and register the shell boot option
3522 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
3527 Boot from the UEFI spec defined "BootNext" variable.
3540 BDS_COMMON_OPTION
*BootOption
;
3541 LIST_ENTRY TempList
;
3546 // Init the boot option name buffer and temp link list
3548 InitializeListHead (&TempList
);
3549 ZeroMem (Buffer
, sizeof (Buffer
));
3551 BootNext
= BdsLibGetVariableAndSize (
3553 &gEfiGlobalVariableGuid
,
3558 // Clear the boot next variable first
3560 if (BootNext
!= NULL
) {
3561 Status
= gRT
->SetVariable (
3563 &gEfiGlobalVariableGuid
,
3564 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3569 // Deleting variable with current variable implementation shouldn't fail.
3571 ASSERT_EFI_ERROR (Status
);
3574 // Start to build the boot option and try to boot
3576 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
3577 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
3578 ASSERT (BootOption
!= NULL
);
3579 BdsLibConnectDevicePath (BootOption
->DevicePath
);
3580 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
3581 FreePool(BootOption
);
3588 Return the bootable media handle.
3589 First, check the device is connected
3590 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
3591 Third, detect the the default boot file in the Media, and return the removable Media handle.
3593 @param DevicePath Device Path to a bootable device
3595 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
3600 BdsLibGetBootableHandle (
3601 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3606 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3607 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
3609 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
3611 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3614 EFI_HANDLE ReturnHandle
;
3615 EFI_HANDLE
*SimpleFileSystemHandles
;
3617 UINTN NumberSimpleFileSystemHandles
;
3619 EFI_IMAGE_DOS_HEADER DosHeader
;
3620 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3621 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3623 UpdatedDevicePath
= DevicePath
;
3626 // Enter to critical section to protect the acquired BlockIo instance
3627 // from getting released due to the USB mass storage hotplug event
3629 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
3632 // Check whether the device is connected
3634 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3635 if (EFI_ERROR (Status
)) {
3637 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
3639 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3640 if (EFI_ERROR (Status
)) {
3642 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
3644 UpdatedDevicePath
= DevicePath
;
3645 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3646 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3650 // For removable device boot option, its contained device path only point to the removable device handle,
3651 // should make sure all its children handles (its child partion or media handles) are created and connected.
3653 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3655 // Get BlockIo protocol and check removable attribute
3657 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
3658 ASSERT_EFI_ERROR (Status
);
3661 // Issue a dummy read to the device to check for media change.
3662 // When the removable media is changed, any Block IO read/write will
3663 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
3664 // returned. After the Block IO protocol is reinstalled, subsequent
3665 // Block IO read/write will success.
3667 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
3668 if (Buffer
!= NULL
) {
3669 BlockIo
->ReadBlocks (
3671 BlockIo
->Media
->MediaId
,
3673 BlockIo
->Media
->BlockSize
,
3681 // Detect the the default boot file from removable Media
3685 // 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
3686 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
3688 DupDevicePath
= DuplicateDevicePath (DevicePath
);
3689 ASSERT (DupDevicePath
!= NULL
);
3691 UpdatedDevicePath
= DupDevicePath
;
3692 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3694 // 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
3695 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
3697 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
3698 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
3700 // Remove the usb node, let the device path only point to PCI node
3702 SetDevicePathEndNode (UpdatedDevicePath
);
3703 UpdatedDevicePath
= DupDevicePath
;
3705 UpdatedDevicePath
= DevicePath
;
3709 // Get the device path size of boot option
3711 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3712 ReturnHandle
= NULL
;
3713 gBS
->LocateHandleBuffer (
3715 &gEfiSimpleFileSystemProtocolGuid
,
3717 &NumberSimpleFileSystemHandles
,
3718 &SimpleFileSystemHandles
3720 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
3722 // Get the device path size of SimpleFileSystem handle
3724 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
3725 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3727 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
3729 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
3731 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
3732 // machinename is ia32, ia64, x64, ...
3734 Hdr
.Union
= &HdrData
;
3735 Status
= BdsLibGetImageHeader (
3736 SimpleFileSystemHandles
[Index
],
3737 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3741 if (!EFI_ERROR (Status
) &&
3742 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3743 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3744 ReturnHandle
= SimpleFileSystemHandles
[Index
];
3750 FreePool(DupDevicePath
);
3752 if (SimpleFileSystemHandles
!= NULL
) {
3753 FreePool(SimpleFileSystemHandles
);
3756 gBS
->RestoreTPL (OldTpl
);
3758 return ReturnHandle
;
3762 Check to see if the network cable is plugged in. If the DevicePath is not
3763 connected it will be connected.
3765 @param DevicePath Device Path to check
3767 @retval TRUE DevicePath points to an Network that is connected
3768 @retval FALSE DevicePath does not point to a bootable network
3772 BdsLibNetworkBootWithMediaPresent (
3773 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3777 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3779 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
3780 BOOLEAN MediaPresent
;
3781 UINT32 InterruptStatus
;
3783 MediaPresent
= FALSE
;
3785 UpdatedDevicePath
= DevicePath
;
3787 // Locate Load File Protocol for PXE boot option first
3789 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3790 if (EFI_ERROR (Status
)) {
3792 // Device not present so see if we need to connect it
3794 Status
= BdsLibConnectDevicePath (DevicePath
);
3795 if (!EFI_ERROR (Status
)) {
3797 // This one should work after we did the connect
3799 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3803 if (!EFI_ERROR (Status
)) {
3804 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
3805 if (EFI_ERROR (Status
)) {
3807 // Failed to open SNP from this handle, try to get SNP from parent handle
3809 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
3810 if (UpdatedDevicePath
!= NULL
) {
3811 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3812 if (!EFI_ERROR (Status
)) {
3814 // SNP handle found, get SNP from it
3816 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
3821 if (!EFI_ERROR (Status
)) {
3822 if (Snp
->Mode
->MediaPresentSupported
) {
3823 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
3825 // Invoke Snp->GetStatus() to refresh the media status
3827 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
3830 // In case some one else is using the SNP check to see if it's connected
3832 MediaPresent
= Snp
->Mode
->MediaPresent
;
3835 // No one is using SNP so we need to Start and Initialize so
3836 // MediaPresent will be valid.
3838 Status
= Snp
->Start (Snp
);
3839 if (!EFI_ERROR (Status
)) {
3840 Status
= Snp
->Initialize (Snp
, 0, 0);
3841 if (!EFI_ERROR (Status
)) {
3842 MediaPresent
= Snp
->Mode
->MediaPresent
;
3843 Snp
->Shutdown (Snp
);
3849 MediaPresent
= TRUE
;
3854 return MediaPresent
;
3858 For a bootable Device path, return its boot type.
3860 @param DevicePath The bootable device Path to check
3862 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3863 which subtype is MEDIA_HARDDRIVE_DP
3864 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3865 which subtype is MEDIA_CDROM_DP
3866 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
3867 which HID is floppy device.
3868 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3869 and its last device path node's subtype is MSG_ATAPI_DP.
3870 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3871 and its last device path node's subtype is MSG_SCSI_DP.
3872 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3873 and its last device path node's subtype is MSG_USB_DP.
3874 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
3875 its last device path node point to a message device path node.
3876 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
3877 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
3882 BdsGetBootTypeFromDevicePath (
3883 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3886 ACPI_HID_DEVICE_PATH
*Acpi
;
3887 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3888 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
3891 if (NULL
== DevicePath
) {
3892 return BDS_EFI_UNSUPPORT
;
3895 TempDevicePath
= DevicePath
;
3897 while (!IsDevicePathEndType (TempDevicePath
)) {
3898 switch (DevicePathType (TempDevicePath
)) {
3899 case BBS_DEVICE_PATH
:
3900 return BDS_LEGACY_BBS_BOOT
;
3901 case MEDIA_DEVICE_PATH
:
3902 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
3903 return BDS_EFI_MEDIA_HD_BOOT
;
3904 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
3905 return BDS_EFI_MEDIA_CDROM_BOOT
;
3908 case ACPI_DEVICE_PATH
:
3909 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
3910 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
3911 return BDS_EFI_ACPI_FLOPPY_BOOT
;
3914 case MESSAGING_DEVICE_PATH
:
3916 // Get the last device path node
3918 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
3919 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
3921 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
3924 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
3927 // if the device path not only point to driver device, it is not a messaging device path,
3929 if (!IsDevicePathEndType (LastDeviceNode
)) {
3933 switch (DevicePathSubType (TempDevicePath
)) {
3935 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
3939 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
3943 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
3947 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
3950 case MSG_MAC_ADDR_DP
:
3954 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
3958 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
3966 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
3969 return BDS_EFI_UNSUPPORT
;
3973 Check whether the Device path in a boot option point to a valid bootable device,
3974 And if CheckMedia is true, check the device is ready to boot now.
3976 @param DevPath the Device path in a boot option
3977 @param CheckMedia if true, check the device is ready to boot now.
3979 @retval TRUE the Device path is valid
3980 @retval FALSE the Device path is invalid .
3985 BdsLibIsValidEFIBootOptDevicePath (
3986 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
3987 IN BOOLEAN CheckMedia
3990 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
3994 Check whether the Device path in a boot option point to a valid bootable device,
3995 And if CheckMedia is true, check the device is ready to boot now.
3996 If Description is not NULL and the device path point to a fixed BlockIo
3997 device, check the description whether conflict with other auto-created
4000 @param DevPath the Device path in a boot option
4001 @param CheckMedia if true, check the device is ready to boot now.
4002 @param Description the description in a boot option
4004 @retval TRUE the Device path is valid
4005 @retval FALSE the Device path is invalid .
4010 BdsLibIsValidEFIBootOptDevicePathExt (
4011 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
4012 IN BOOLEAN CheckMedia
,
4013 IN CHAR16
*Description
4018 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4019 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4020 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
4022 TempDevicePath
= DevPath
;
4023 LastDeviceNode
= DevPath
;
4026 // Check if it's a valid boot option for network boot device.
4027 // Check if there is EfiLoadFileProtocol installed.
4028 // If yes, that means there is a boot option for network.
4030 Status
= gBS
->LocateDevicePath (
4031 &gEfiLoadFileProtocolGuid
,
4035 if (EFI_ERROR (Status
)) {
4037 // Device not present so see if we need to connect it
4039 TempDevicePath
= DevPath
;
4040 BdsLibConnectDevicePath (TempDevicePath
);
4041 Status
= gBS
->LocateDevicePath (
4042 &gEfiLoadFileProtocolGuid
,
4048 if (!EFI_ERROR (Status
)) {
4049 if (!IsDevicePathEnd (TempDevicePath
)) {
4051 // LoadFile protocol is not installed on handle with exactly the same DevPath
4058 // Test if it is ready to boot now
4060 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
4069 // If the boot option point to a file, it is a valid EFI boot option,
4070 // and assume it is ready to boot now
4072 while (!IsDevicePathEnd (TempDevicePath
)) {
4074 // If there is USB Class or USB WWID device path node, treat it as valid EFI
4075 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it
4076 // to full device path.
4078 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
4079 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
4080 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
4084 LastDeviceNode
= TempDevicePath
;
4085 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4087 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
4088 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
4093 // Check if it's a valid boot option for internal FV application
4095 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
4097 // If the boot option point to internal FV application, make sure it is valid
4099 TempDevicePath
= DevPath
;
4100 Status
= BdsLibUpdateFvFileDevicePath (
4102 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
)
4104 if (Status
== EFI_ALREADY_STARTED
) {
4107 if (Status
== EFI_SUCCESS
) {
4108 FreePool (TempDevicePath
);
4115 // If the boot option point to a blockIO device:
4116 // if it is a removable blockIo device, it is valid.
4117 // if it is a fixed blockIo device, check its description confliction.
4119 TempDevicePath
= DevPath
;
4120 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4121 if (EFI_ERROR (Status
)) {
4123 // Device not present so see if we need to connect it
4125 Status
= BdsLibConnectDevicePath (DevPath
);
4126 if (!EFI_ERROR (Status
)) {
4128 // Try again to get the Block Io protocol after we did the connect
4130 TempDevicePath
= DevPath
;
4131 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4135 if (!EFI_ERROR (Status
)) {
4136 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
4137 if (!EFI_ERROR (Status
)) {
4140 // Test if it is ready to boot now
4142 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4151 // 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,
4153 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
4154 if (!EFI_ERROR (Status
)) {
4157 // Test if it is ready to boot now
4159 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4173 According to a file guild, check a Fv file device path is valid. If it is invalid,
4174 try to return the valid device path.
4175 FV address maybe changes for memory layout adjust from time to time, use this function
4176 could promise the Fv file device path is right.
4178 @param DevicePath on input, the Fv file device path need to check on
4179 output, the updated valid Fv file device path
4180 @param FileGuid the Fv file guild
4182 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
4184 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
4186 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
4188 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
4189 and return the updated device path in DevicePath
4194 BdsLibUpdateFvFileDevicePath (
4195 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
4196 IN EFI_GUID
*FileGuid
4199 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4200 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4202 EFI_GUID
*GuidPoint
;
4204 UINTN FvHandleCount
;
4205 EFI_HANDLE
*FvHandleBuffer
;
4206 EFI_FV_FILETYPE Type
;
4208 EFI_FV_FILE_ATTRIBUTES Attributes
;
4209 UINT32 AuthenticationStatus
;
4211 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
4212 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
4213 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
4214 EFI_HANDLE FoundFvHandle
;
4215 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
4217 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
4218 return EFI_INVALID_PARAMETER
;
4220 if (FileGuid
== NULL
) {
4221 return EFI_INVALID_PARAMETER
;
4225 // Check whether the device path point to the default the input Fv file
4227 TempDevicePath
= *DevicePath
;
4228 LastDeviceNode
= TempDevicePath
;
4229 while (!IsDevicePathEnd (TempDevicePath
)) {
4230 LastDeviceNode
= TempDevicePath
;
4231 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4233 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
4234 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
4236 if (GuidPoint
== NULL
) {
4238 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
4240 return EFI_UNSUPPORTED
;
4242 if (!CompareGuid (GuidPoint
, FileGuid
)) {
4244 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
4246 return EFI_UNSUPPORTED
;
4250 // Check whether the input Fv file device path is valid
4252 TempDevicePath
= *DevicePath
;
4253 FoundFvHandle
= NULL
;
4254 Status
= gBS
->LocateDevicePath (
4255 &gEfiFirmwareVolume2ProtocolGuid
,
4259 if (!EFI_ERROR (Status
)) {
4260 Status
= gBS
->HandleProtocol (
4262 &gEfiFirmwareVolume2ProtocolGuid
,
4265 if (!EFI_ERROR (Status
)) {
4267 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
4269 Status
= Fv
->ReadFile (
4276 &AuthenticationStatus
4278 if (!EFI_ERROR (Status
)) {
4279 return EFI_ALREADY_STARTED
;
4285 // Look for the input wanted FV file in current FV
4286 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
4289 FoundFvHandle
= NULL
;
4290 Status
= gBS
->HandleProtocol (
4292 &gEfiLoadedImageProtocolGuid
,
4293 (VOID
**) &LoadedImage
4295 if (!EFI_ERROR (Status
)) {
4296 Status
= gBS
->HandleProtocol (
4297 LoadedImage
->DeviceHandle
,
4298 &gEfiFirmwareVolume2ProtocolGuid
,
4301 if (!EFI_ERROR (Status
)) {
4302 Status
= Fv
->ReadFile (
4309 &AuthenticationStatus
4311 if (!EFI_ERROR (Status
)) {
4313 FoundFvHandle
= LoadedImage
->DeviceHandle
;
4318 // Second, if fail to find, try to enumerate all FV
4321 FvHandleBuffer
= NULL
;
4322 gBS
->LocateHandleBuffer (
4324 &gEfiFirmwareVolume2ProtocolGuid
,
4329 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
4330 gBS
->HandleProtocol (
4331 FvHandleBuffer
[Index
],
4332 &gEfiFirmwareVolume2ProtocolGuid
,
4336 Status
= Fv
->ReadFile (
4343 &AuthenticationStatus
4345 if (EFI_ERROR (Status
)) {
4347 // Skip if input Fv file not in the FV
4352 FoundFvHandle
= FvHandleBuffer
[Index
];
4356 if (FvHandleBuffer
!= NULL
) {
4357 FreePool (FvHandleBuffer
);
4363 // Build the shell device path
4365 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
4366 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
4367 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
4368 ASSERT (NewDevicePath
!= NULL
);
4369 *DevicePath
= NewDevicePath
;
4372 return EFI_NOT_FOUND
;