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 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalBdsLib.h"
18 BOOLEAN mEnumBootDevice
= FALSE
;
19 EFI_HII_HANDLE gBdsLibStringPackHandle
= NULL
;
25 @param Event The triggered event.
26 @param Context Context for this event.
37 // Record the performance data for End of BDS
39 PERF_END(NULL
, "BDS", NULL
, 0);
45 The constructor function register UNI strings into imageHandle.
47 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
49 @param ImageHandle The firmware allocated handle for the EFI image.
50 @param SystemTable A pointer to the EFI System Table.
52 @retval EFI_SUCCESS The constructor successfully added string package.
53 @retval Other value The constructor can't add string package.
58 GenericBdsLibConstructor (
59 IN EFI_HANDLE ImageHandle
,
60 IN EFI_SYSTEM_TABLE
*SystemTable
64 gBdsLibStringPackHandle
= HiiAddPackages (
65 &gBdsLibStringPackageGuid
,
71 ASSERT (gBdsLibStringPackHandle
!= NULL
);
77 Deletete the Boot Option from EFI Variable. The Boot Order Arrray
80 @param OptionNumber The number of Boot option want to be deleted.
81 @param BootOrder The Boot Order array.
82 @param BootOrderSize The size of the Boot Order Array.
84 @retval EFI_SUCCESS The Boot Option Variable was found and removed
85 @retval EFI_UNSUPPORTED The Boot Option Variable store was inaccessible
86 @retval EFI_NOT_FOUND The Boot Option Variable was not found
91 IN UINTN OptionNumber
,
92 IN OUT UINT16
*BootOrder
,
93 IN OUT UINTN
*BootOrderSize
100 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", OptionNumber
);
101 Status
= gRT
->SetVariable (
103 &gEfiGlobalVariableGuid
,
109 // Deleting variable with existing variable implementation shouldn't fail.
111 ASSERT_EFI_ERROR (Status
);
114 // adjust boot order array
116 for (Index
= 0; Index
< *BootOrderSize
/ sizeof (UINT16
); Index
++) {
117 if (BootOrder
[Index
] == OptionNumber
) {
118 CopyMem (&BootOrder
[Index
], &BootOrder
[Index
+1], *BootOrderSize
- (Index
+1) * sizeof (UINT16
));
119 *BootOrderSize
-= sizeof (UINT16
);
128 Translate the first n characters of an Ascii string to
129 Unicode characters. The count n is indicated by parameter
130 Size. If Size is greater than the length of string, then
131 the entire string is translated.
134 @param AStr Pointer to input Ascii string.
135 @param Size The number of characters to translate.
136 @param UStr Pointer to output Unicode string buffer.
149 while (AStr
[Idx
] != 0) {
150 UStr
[Idx
] = (CHAR16
) AStr
[Idx
];
161 Build Legacy Device Name String according.
163 @param CurBBSEntry BBS Table.
165 @param BufSize The buffer size.
166 @param BootString The output string.
170 BdsBuildLegacyDevNameString (
171 IN BBS_TABLE
*CurBBSEntry
,
174 OUT CHAR16
*BootString
187 Fmt
= L
"Primary Master %s";
194 Fmt
= L
"Primary Slave %s";
201 Fmt
= L
"Secondary Master %s";
208 Fmt
= L
"Secondary Slave %s";
216 switch (CurBBSEntry
->DeviceType
) {
237 case BBS_EMBED_NETWORK
:
251 // If current BBS entry has its description then use it.
253 StringDesc
= (UINT8
*) (((UINTN
) CurBBSEntry
->DescStringSegment
<< 4) + CurBBSEntry
->DescStringOffset
);
254 if (NULL
!= StringDesc
) {
256 // Only get fisrt 32 characters, this is suggested by BBS spec
258 AsciiToUnicodeSize (StringDesc
, 32, Temp
);
264 // BbsTable 16 entries are for onboard IDE.
265 // Set description string for SATA harddisks, Harddisk 0 ~ Harddisk 11
267 if (Index
>= 5 && Index
<= 16 && (CurBBSEntry
->DeviceType
== BBS_HARDDISK
|| CurBBSEntry
->DeviceType
== BBS_CDROM
)) {
269 UnicodeSPrint (BootString
, BufSize
, Fmt
, Type
, Index
- 5);
271 UnicodeSPrint (BootString
, BufSize
, Fmt
, Type
);
277 Create a legacy boot option for the specified entry of
278 BBS table, save it as variable, and append it to the boot
282 @param CurrentBbsEntry Pointer to current BBS table.
283 @param CurrentBbsDevPath Pointer to the Device Path Protocol instance of BBS
284 @param Index Index of the specified entry in BBS table.
285 @param BootOrderList On input, the original boot order list.
286 On output, the new boot order list attached with the
288 @param BootOrderListSize On input, the original size of boot order list.
289 On output, the size of new boot order list.
291 @retval EFI_SUCCESS Boot Option successfully created.
292 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
293 @retval Other Error occurs while setting variable.
297 BdsCreateLegacyBootOption (
298 IN BBS_TABLE
*CurrentBbsEntry
,
299 IN EFI_DEVICE_PATH_PROTOCOL
*CurrentBbsDevPath
,
301 IN OUT UINT16
**BootOrderList
,
302 IN OUT UINTN
*BootOrderListSize
306 UINT16 CurrentBootOptionNo
;
307 UINT16 BootString
[10];
308 CHAR16 BootDesc
[100];
309 CHAR8 HelpString
[100];
310 UINT16
*NewBootOrderList
;
315 UINT16 CurrentBbsDevPathSize
;
316 UINTN BootOrderIndex
;
317 UINTN BootOrderLastIndex
;
319 BOOLEAN IndexNotFound
;
320 BBS_BBS_DEVICE_PATH
*NewBbsDevPathNode
;
322 if ((*BootOrderList
) == NULL
) {
323 CurrentBootOptionNo
= 0;
325 for (ArrayIndex
= 0; ArrayIndex
< (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
)); ArrayIndex
++) {
326 IndexNotFound
= TRUE
;
327 for (BootOrderIndex
= 0; BootOrderIndex
< (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
)); BootOrderIndex
++) {
328 if ((*BootOrderList
)[BootOrderIndex
] == ArrayIndex
) {
329 IndexNotFound
= FALSE
;
334 if (!IndexNotFound
) {
341 CurrentBootOptionNo
= (UINT16
) ArrayIndex
;
351 BdsBuildLegacyDevNameString (CurrentBbsEntry
, Index
, sizeof (BootDesc
), BootDesc
);
354 // Create new BBS device path node with description string
356 UnicodeStrToAsciiStrS (BootDesc
, HelpString
, sizeof (HelpString
));
358 StringLen
= AsciiStrLen (HelpString
);
359 NewBbsDevPathNode
= AllocateZeroPool (sizeof (BBS_BBS_DEVICE_PATH
) + StringLen
);
360 if (NewBbsDevPathNode
== NULL
) {
361 return EFI_OUT_OF_RESOURCES
;
363 CopyMem (NewBbsDevPathNode
, CurrentBbsDevPath
, sizeof (BBS_BBS_DEVICE_PATH
));
364 CopyMem (NewBbsDevPathNode
->String
, HelpString
, StringLen
+ 1);
365 SetDevicePathNodeLength (&(NewBbsDevPathNode
->Header
), sizeof (BBS_BBS_DEVICE_PATH
) + StringLen
);
368 // Create entire new CurrentBbsDevPath with end node
370 CurrentBbsDevPath
= AppendDevicePathNode (
372 (EFI_DEVICE_PATH_PROTOCOL
*) NewBbsDevPathNode
374 if (CurrentBbsDevPath
== NULL
) {
375 FreePool (NewBbsDevPathNode
);
376 return EFI_OUT_OF_RESOURCES
;
379 CurrentBbsDevPathSize
= (UINT16
) (GetDevicePathSize (CurrentBbsDevPath
));
381 BufferSize
= sizeof (UINT32
) +
384 CurrentBbsDevPathSize
+
388 Buffer
= AllocateZeroPool (BufferSize
);
389 if (Buffer
== NULL
) {
390 FreePool (NewBbsDevPathNode
);
391 FreePool (CurrentBbsDevPath
);
392 return EFI_OUT_OF_RESOURCES
;
395 Ptr
= (UINT8
*) Buffer
;
397 *((UINT32
*) Ptr
) = LOAD_OPTION_ACTIVE
;
398 Ptr
+= sizeof (UINT32
);
400 *((UINT16
*) Ptr
) = CurrentBbsDevPathSize
;
401 Ptr
+= sizeof (UINT16
);
408 Ptr
+= StrSize (BootDesc
);
413 CurrentBbsDevPathSize
415 Ptr
+= CurrentBbsDevPathSize
;
423 Ptr
+= sizeof (BBS_TABLE
);
424 *((UINT16
*) Ptr
) = (UINT16
) Index
;
426 Status
= gRT
->SetVariable (
428 &gEfiGlobalVariableGuid
,
429 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
438 NewBootOrderList
= AllocateZeroPool (*BootOrderListSize
+ sizeof (UINT16
));
439 if (NULL
== NewBootOrderList
) {
440 FreePool (NewBbsDevPathNode
);
441 FreePool (CurrentBbsDevPath
);
442 return EFI_OUT_OF_RESOURCES
;
445 if (*BootOrderList
!= NULL
) {
446 CopyMem (NewBootOrderList
, *BootOrderList
, *BootOrderListSize
);
447 FreePool (*BootOrderList
);
450 BootOrderLastIndex
= (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
));
451 NewBootOrderList
[BootOrderLastIndex
] = CurrentBootOptionNo
;
452 *BootOrderListSize
+= sizeof (UINT16
);
453 *BootOrderList
= NewBootOrderList
;
455 FreePool (NewBbsDevPathNode
);
456 FreePool (CurrentBbsDevPath
);
461 Check if the boot option is a legacy one.
463 @param BootOptionVar The boot option data payload.
464 @param BbsEntry The BBS Table.
465 @param BbsIndex The table index.
467 @retval TRUE It is a legacy boot option.
468 @retval FALSE It is not a legacy boot option.
472 BdsIsLegacyBootOption (
473 IN UINT8
*BootOptionVar
,
474 OUT BBS_TABLE
**BbsEntry
,
479 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
484 Ptr
+= sizeof (UINT32
);
485 DevPathLen
= *(UINT16
*) Ptr
;
486 Ptr
+= sizeof (UINT16
);
487 Ptr
+= StrSize ((UINT16
*) Ptr
);
488 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
489 if ((BBS_DEVICE_PATH
== DevicePath
->Type
) && (BBS_BBS_DP
== DevicePath
->SubType
)) {
491 *BbsEntry
= (BBS_TABLE
*) Ptr
;
492 Ptr
+= sizeof (BBS_TABLE
);
493 *BbsIndex
= *(UINT16
*) Ptr
;
504 Delete all the invalid legacy boot options.
506 @retval EFI_SUCCESS All invalide legacy boot options are deleted.
507 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
508 @retval EFI_NOT_FOUND Fail to retrive variable of boot order.
512 BdsDeleteAllInvalidLegacyBootOptions (
517 UINT8
*BootOptionVar
;
519 UINTN BootOptionSize
;
523 HDD_INFO
*LocalHddInfo
;
524 BBS_TABLE
*LocalBbsTable
;
527 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
529 UINT16 BootOption
[10];
530 UINT16 BootDesc
[100];
531 BOOLEAN DescStringMatch
;
533 Status
= EFI_SUCCESS
;
539 LocalBbsTable
= NULL
;
542 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
543 if (EFI_ERROR (Status
)) {
547 BootOrder
= BdsLibGetVariableAndSize (
549 &gEfiGlobalVariableGuid
,
552 if (BootOrder
== NULL
) {
553 return EFI_NOT_FOUND
;
556 LegacyBios
->GetBbsInfo (
565 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
566 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
567 BootOptionVar
= BdsLibGetVariableAndSize (
569 &gEfiGlobalVariableGuid
,
572 if (NULL
== BootOptionVar
) {
574 Status
= gRT
->GetVariable (
576 &gEfiGlobalVariableGuid
,
581 if (Status
== EFI_NOT_FOUND
) {
585 BdsDeleteBootOption (
592 FreePool (BootOrder
);
593 return EFI_OUT_OF_RESOURCES
;
598 // Skip Non-Legacy boot option
600 if (!BdsIsLegacyBootOption (BootOptionVar
, &BbsEntry
, &BbsIndex
)) {
601 if (BootOptionVar
!= NULL
) {
602 FreePool (BootOptionVar
);
608 if (BbsIndex
< BbsCount
) {
610 // Check if BBS Description String is changed
612 DescStringMatch
= FALSE
;
613 BdsBuildLegacyDevNameString (
614 &LocalBbsTable
[BbsIndex
],
620 if (StrCmp (BootDesc
, (UINT16
*)(BootOptionVar
+ sizeof (UINT32
) + sizeof (UINT16
))) == 0) {
621 DescStringMatch
= TRUE
;
624 if (!((LocalBbsTable
[BbsIndex
].BootPriority
== BBS_IGNORE_ENTRY
) ||
625 (LocalBbsTable
[BbsIndex
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)) &&
626 (LocalBbsTable
[BbsIndex
].DeviceType
== BbsEntry
->DeviceType
) &&
633 if (BootOptionVar
!= NULL
) {
634 FreePool (BootOptionVar
);
639 BdsDeleteBootOption (
647 // Adjust the number of boot options.
649 Status
= gRT
->SetVariable (
651 &gEfiGlobalVariableGuid
,
652 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
657 // Shrinking variable with existing variable implementation shouldn't fail.
659 ASSERT_EFI_ERROR (Status
);
660 FreePool (BootOrder
);
666 Find all legacy boot option by device type.
668 @param BootOrder The boot order array.
669 @param BootOptionNum The number of boot option.
670 @param DevType Device type.
671 @param DevName Device name.
672 @param Attribute The boot option attribute.
673 @param BbsIndex The BBS table index.
674 @param OptionNumber The boot option index.
676 @retval TRUE The Legacy boot option is found.
677 @retval FALSE The legacy boot option is not found.
681 BdsFindLegacyBootOptionByDevTypeAndName (
682 IN UINT16
*BootOrder
,
683 IN UINTN BootOptionNum
,
686 OUT UINT32
*Attribute
,
687 OUT UINT16
*BbsIndex
,
688 OUT UINT16
*OptionNumber
692 CHAR16 BootOption
[9];
693 UINTN BootOptionSize
;
694 UINT8
*BootOptionVar
;
701 if (NULL
== BootOrder
) {
706 // Loop all boot option from variable
708 for (Index
= 0; Index
< BootOptionNum
; Index
++) {
709 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", (UINTN
) BootOrder
[Index
]);
710 BootOptionVar
= BdsLibGetVariableAndSize (
712 &gEfiGlobalVariableGuid
,
715 if (NULL
== BootOptionVar
) {
720 // Skip Non-legacy boot option
722 if (!BdsIsLegacyBootOption (BootOptionVar
, &BbsEntry
, BbsIndex
)) {
723 FreePool (BootOptionVar
);
728 (BbsEntry
->DeviceType
!= DevType
) ||
729 (StrCmp (DevName
, (CHAR16
*)(BootOptionVar
+ sizeof (UINT32
) + sizeof (UINT16
))) != 0)
731 FreePool (BootOptionVar
);
735 *Attribute
= *(UINT32
*) BootOptionVar
;
736 *OptionNumber
= BootOrder
[Index
];
738 FreePool (BootOptionVar
);
746 Create a legacy boot option.
748 @param BbsItem The BBS Table entry.
749 @param Index Index of the specified entry in BBS table.
750 @param BootOrderList The boot order list.
751 @param BootOrderListSize The size of boot order list.
753 @retval EFI_OUT_OF_RESOURCE No enough memory.
754 @retval EFI_SUCCESS The function complete successfully.
755 @return Other value if the legacy boot option is not created.
759 BdsCreateOneLegacyBootOption (
760 IN BBS_TABLE
*BbsItem
,
762 IN OUT UINT16
**BootOrderList
,
763 IN OUT UINTN
*BootOrderListSize
766 BBS_BBS_DEVICE_PATH BbsDevPathNode
;
768 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
773 // Create device path node.
775 BbsDevPathNode
.Header
.Type
= BBS_DEVICE_PATH
;
776 BbsDevPathNode
.Header
.SubType
= BBS_BBS_DP
;
777 SetDevicePathNodeLength (&BbsDevPathNode
.Header
, sizeof (BBS_BBS_DEVICE_PATH
));
778 BbsDevPathNode
.DeviceType
= BbsItem
->DeviceType
;
779 CopyMem (&BbsDevPathNode
.StatusFlag
, &BbsItem
->StatusFlags
, sizeof (UINT16
));
781 DevPath
= AppendDevicePathNode (
783 (EFI_DEVICE_PATH_PROTOCOL
*) &BbsDevPathNode
785 if (NULL
== DevPath
) {
786 return EFI_OUT_OF_RESOURCES
;
789 Status
= BdsCreateLegacyBootOption (
796 BbsItem
->BootPriority
= 0x00;
804 Add the legacy boot options from BBS table if they do not exist.
806 @retval EFI_SUCCESS The boot options are added successfully
807 or they are already in boot options.
808 @retval EFI_NOT_FOUND No legacy boot options is found.
809 @retval EFI_OUT_OF_RESOURCE No enough memory.
810 @return Other value LegacyBoot options are not added.
814 BdsAddNonExistingLegacyBootOptions (
824 HDD_INFO
*LocalHddInfo
;
825 BBS_TABLE
*LocalBbsTable
;
827 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
836 LocalBbsTable
= NULL
;
838 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
839 if (EFI_ERROR (Status
)) {
843 LegacyBios
->GetBbsInfo (
851 BootOrder
= BdsLibGetVariableAndSize (
853 &gEfiGlobalVariableGuid
,
856 if (BootOrder
== NULL
) {
860 for (Index
= 0; Index
< BbsCount
; Index
++) {
861 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
862 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
867 BdsBuildLegacyDevNameString (&LocalBbsTable
[Index
], Index
, sizeof (Desc
), Desc
);
869 Exist
= BdsFindLegacyBootOptionByDevTypeAndName (
871 BootOrderSize
/ sizeof (UINT16
),
872 LocalBbsTable
[Index
].DeviceType
,
880 // Not found such type of legacy device in boot options or we found but it's disabled
881 // so we have to create one and put it to the tail of boot order list
883 Status
= BdsCreateOneLegacyBootOption (
884 &LocalBbsTable
[Index
],
889 if (!EFI_ERROR (Status
)) {
890 ASSERT (BootOrder
!= NULL
);
892 OptionNumber
= BootOrder
[BootOrderSize
/ sizeof (UINT16
) - 1];
896 ASSERT (BbsIndex
== Index
);
899 Status
= gRT
->SetVariable (
901 &gEfiGlobalVariableGuid
,
902 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
906 if (BootOrder
!= NULL
) {
907 FreePool (BootOrder
);
914 Fill the device order buffer.
916 @param BbsTable The BBS table.
917 @param BbsType The BBS Type.
918 @param BbsCount The BBS Count.
919 @param Buf device order buffer.
921 @return The device order buffer.
926 IN BBS_TABLE
*BbsTable
,
934 for (Index
= 0; Index
< BbsCount
; Index
++) {
935 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
939 if (BbsTable
[Index
].DeviceType
!= BbsType
) {
943 *Buf
= (UINT16
) (Index
& 0xFF);
951 Create the device order buffer.
953 @param BbsTable The BBS table.
954 @param BbsCount The BBS Count.
956 @retval EFI_SUCCES The buffer is created and the EFI variable named
957 VAR_LEGACY_DEV_ORDER and gEfiLegacyDevOrderVariableGuid is
959 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
960 @retval EFI_DEVICE_ERROR Fail to add the device order into EFI variable fail
961 because of hardware error.
965 IN BBS_TABLE
*BbsTable
,
977 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
978 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
987 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
989 Status
= EFI_SUCCESS
;
992 // Count all boot devices
994 for (Index
= 0; Index
< BbsCount
; Index
++) {
995 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
999 switch (BbsTable
[Index
].DeviceType
) {
1012 case BBS_EMBED_NETWORK
:
1016 case BBS_BEV_DEVICE
:
1025 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * FDCount
);
1026 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * HDCount
);
1027 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * CDCount
);
1028 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * NETCount
);
1029 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * BEVCount
);
1032 // Create buffer to hold all boot device order
1034 DevOrder
= AllocateZeroPool (TotalSize
);
1035 if (NULL
== DevOrder
) {
1036 return EFI_OUT_OF_RESOURCES
;
1038 DevOrderPtr
= DevOrder
;
1040 DevOrderPtr
->BbsType
= BBS_FLOPPY
;
1041 DevOrderPtr
->Length
= (UINT16
) (sizeof (DevOrderPtr
->Length
) + FDCount
* sizeof (UINT16
));
1042 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_FLOPPY
, BbsCount
, DevOrderPtr
->Data
);
1044 DevOrderPtr
->BbsType
= BBS_HARDDISK
;
1045 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1046 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_HARDDISK
, BbsCount
, DevOrderPtr
->Data
);
1048 DevOrderPtr
->BbsType
= BBS_CDROM
;
1049 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1050 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_CDROM
, BbsCount
, DevOrderPtr
->Data
);
1052 DevOrderPtr
->BbsType
= BBS_EMBED_NETWORK
;
1053 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1054 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_EMBED_NETWORK
, BbsCount
, DevOrderPtr
->Data
);
1056 DevOrderPtr
->BbsType
= BBS_BEV_DEVICE
;
1057 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1058 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_BEV_DEVICE
, BbsCount
, DevOrderPtr
->Data
);
1060 ASSERT (TotalSize
== ((UINTN
) DevOrderPtr
- (UINTN
) DevOrder
));
1063 // Save device order for legacy boot device to variable.
1065 Status
= gRT
->SetVariable (
1066 VAR_LEGACY_DEV_ORDER
,
1067 &gEfiLegacyDevOrderVariableGuid
,
1068 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1072 FreePool (DevOrder
);
1078 Add the legacy boot devices from BBS table into
1079 the legacy device boot order.
1081 @retval EFI_SUCCESS The boot devices are added successfully.
1082 @retval EFI_NOT_FOUND The legacy boot devices are not found.
1083 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
1084 @retval EFI_DEVICE_ERROR Fail to add the legacy device boot order into EFI variable
1085 because of hardware error.
1089 BdsUpdateLegacyDevOrder (
1093 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1094 LEGACY_DEV_ORDER_ENTRY
*NewDevOrder
;
1095 LEGACY_DEV_ORDER_ENTRY
*Ptr
;
1096 LEGACY_DEV_ORDER_ENTRY
*NewPtr
;
1098 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1102 HDD_INFO
*LocalHddInfo
;
1103 BBS_TABLE
*LocalBbsTable
;
1133 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
1141 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1142 if (EFI_ERROR (Status
)) {
1146 Status
= LegacyBios
->GetBbsInfo (
1153 if (EFI_ERROR (Status
)) {
1157 DevOrder
= BdsLibGetVariableAndSize (
1158 VAR_LEGACY_DEV_ORDER
,
1159 &gEfiLegacyDevOrderVariableGuid
,
1162 if (NULL
== DevOrder
) {
1163 return BdsCreateDevOrder (LocalBbsTable
, BbsCount
);
1166 // First we figure out how many boot devices with same device type respectively
1168 for (Index
= 0; Index
< BbsCount
; Index
++) {
1169 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1170 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1175 switch (LocalBbsTable
[Index
].DeviceType
) {
1188 case BBS_EMBED_NETWORK
:
1192 case BBS_BEV_DEVICE
:
1201 TotalSize
+= (HeaderSize
+ FDCount
* sizeof (UINT16
));
1202 TotalSize
+= (HeaderSize
+ HDCount
* sizeof (UINT16
));
1203 TotalSize
+= (HeaderSize
+ CDCount
* sizeof (UINT16
));
1204 TotalSize
+= (HeaderSize
+ NETCount
* sizeof (UINT16
));
1205 TotalSize
+= (HeaderSize
+ BEVCount
* sizeof (UINT16
));
1207 NewDevOrder
= AllocateZeroPool (TotalSize
);
1208 if (NULL
== NewDevOrder
) {
1209 return EFI_OUT_OF_RESOURCES
;
1218 NewPtr
= NewDevOrder
;
1219 NewPtr
->BbsType
= Ptr
->BbsType
;
1220 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + FDCount
* sizeof (UINT16
));
1221 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1222 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1223 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1224 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_FLOPPY
1229 NewPtr
->Data
[FDIndex
] = Ptr
->Data
[Index
];
1232 NewFDPtr
= NewPtr
->Data
;
1237 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1238 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1239 NewPtr
->BbsType
= Ptr
->BbsType
;
1240 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1241 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1242 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1243 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1244 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1245 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_HARDDISK
1250 NewPtr
->Data
[HDIndex
] = Ptr
->Data
[Index
];
1253 NewHDPtr
= NewPtr
->Data
;
1258 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1259 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1260 NewPtr
->BbsType
= Ptr
->BbsType
;
1261 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1262 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1263 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1264 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1265 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1266 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_CDROM
1271 NewPtr
->Data
[CDIndex
] = Ptr
->Data
[Index
];
1274 NewCDPtr
= NewPtr
->Data
;
1279 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1280 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1281 NewPtr
->BbsType
= Ptr
->BbsType
;
1282 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1283 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1284 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1285 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1286 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1287 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_EMBED_NETWORK
1292 NewPtr
->Data
[NETIndex
] = Ptr
->Data
[Index
];
1295 NewNETPtr
= NewPtr
->Data
;
1300 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1301 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1302 NewPtr
->BbsType
= Ptr
->BbsType
;
1303 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1304 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1305 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1306 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1307 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1308 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_BEV_DEVICE
1313 NewPtr
->Data
[BEVIndex
] = Ptr
->Data
[Index
];
1316 NewBEVPtr
= NewPtr
->Data
;
1318 for (Index
= 0; Index
< BbsCount
; Index
++) {
1319 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1320 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1325 switch (LocalBbsTable
[Index
].DeviceType
) {
1328 NewDevPtr
= NewFDPtr
;
1333 NewDevPtr
= NewHDPtr
;
1338 NewDevPtr
= NewCDPtr
;
1341 case BBS_EMBED_NETWORK
:
1343 NewDevPtr
= NewNETPtr
;
1346 case BBS_BEV_DEVICE
:
1348 NewDevPtr
= NewBEVPtr
;
1356 // at this point we have copied those valid indexes to new buffer
1357 // and we should check if there is any new appeared boot device
1360 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1361 if ((NewDevPtr
[Index2
] & 0xFF) == (UINT16
) Index
) {
1366 if (Index2
== *Idx
) {
1368 // Index2 == *Idx means we didn't find Index
1369 // so Index is a new appeared device's index in BBS table
1370 // insert it before disabled indexes.
1372 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1373 if ((NewDevPtr
[Index2
] & 0xFF00) == 0xFF00) {
1377 CopyMem (&NewDevPtr
[Index2
+ 1], &NewDevPtr
[Index2
], (*Idx
- Index2
) * sizeof (UINT16
));
1378 NewDevPtr
[Index2
] = (UINT16
) (Index
& 0xFF);
1384 FreePool (DevOrder
);
1386 Status
= gRT
->SetVariable (
1387 VAR_LEGACY_DEV_ORDER
,
1388 &gEfiLegacyDevOrderVariableGuid
,
1389 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1393 FreePool (NewDevOrder
);
1399 Set Boot Priority for specified device type.
1401 @param DeviceType The device type.
1402 @param BbsIndex The BBS index to set the highest priority. Ignore when -1.
1403 @param LocalBbsTable The BBS table.
1404 @param Priority The prority table.
1406 @retval EFI_SUCCESS The function completes successfully.
1407 @retval EFI_NOT_FOUND Failed to find device.
1408 @retval EFI_OUT_OF_RESOURCES Failed to get the efi variable of device order.
1412 BdsSetBootPriority4SameTypeDev (
1413 IN UINT16 DeviceType
,
1415 IN OUT BBS_TABLE
*LocalBbsTable
,
1416 IN OUT UINT16
*Priority
1419 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1420 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
1424 DevOrder
= BdsLibGetVariableAndSize (
1425 VAR_LEGACY_DEV_ORDER
,
1426 &gEfiLegacyDevOrderVariableGuid
,
1429 if (NULL
== DevOrder
) {
1430 return EFI_OUT_OF_RESOURCES
;
1433 DevOrderPtr
= DevOrder
;
1434 while ((UINT8
*) DevOrderPtr
< (UINT8
*) DevOrder
+ DevOrderSize
) {
1435 if (DevOrderPtr
->BbsType
== DeviceType
) {
1439 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) ((UINTN
) DevOrderPtr
+ sizeof (BBS_TYPE
) + DevOrderPtr
->Length
);
1442 if ((UINT8
*) DevOrderPtr
>= (UINT8
*) DevOrder
+ DevOrderSize
) {
1443 FreePool (DevOrder
);
1444 return EFI_NOT_FOUND
;
1447 if (BbsIndex
!= (UINTN
) -1) {
1448 LocalBbsTable
[BbsIndex
].BootPriority
= *Priority
;
1452 // If the high byte of the DevIndex is 0xFF, it indicates that this device has been disabled.
1454 for (Index
= 0; Index
< DevOrderPtr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1455 if ((DevOrderPtr
->Data
[Index
] & 0xFF00) == 0xFF00) {
1457 // LocalBbsTable[DevIndex[Index] & 0xFF].BootPriority = BBS_DISABLED_ENTRY;
1459 } else if (DevOrderPtr
->Data
[Index
] != BbsIndex
) {
1460 LocalBbsTable
[DevOrderPtr
->Data
[Index
]].BootPriority
= *Priority
;
1465 FreePool (DevOrder
);
1470 Print the BBS Table.
1472 @param LocalBbsTable The BBS table.
1473 @param BbsCount The count of entry in BBS table.
1477 IN BBS_TABLE
*LocalBbsTable
,
1483 DEBUG ((DEBUG_ERROR
, "\n"));
1484 DEBUG ((DEBUG_ERROR
, " NO Prio bb/dd/ff cl/sc Type Stat segm:offs\n"));
1485 DEBUG ((DEBUG_ERROR
, "=============================================\n"));
1486 for (Idx
= 0; Idx
< BbsCount
; Idx
++) {
1487 if ((LocalBbsTable
[Idx
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1488 (LocalBbsTable
[Idx
].BootPriority
== BBS_DO_NOT_BOOT_FROM
) ||
1489 (LocalBbsTable
[Idx
].BootPriority
== BBS_LOWEST_PRIORITY
)
1496 " %02x: %04x %02x/%02x/%02x %02x/%02x %04x %04x %04x:%04x\n",
1498 (UINTN
) LocalBbsTable
[Idx
].BootPriority
,
1499 (UINTN
) LocalBbsTable
[Idx
].Bus
,
1500 (UINTN
) LocalBbsTable
[Idx
].Device
,
1501 (UINTN
) LocalBbsTable
[Idx
].Function
,
1502 (UINTN
) LocalBbsTable
[Idx
].Class
,
1503 (UINTN
) LocalBbsTable
[Idx
].SubClass
,
1504 (UINTN
) LocalBbsTable
[Idx
].DeviceType
,
1505 (UINTN
) * (UINT16
*) &LocalBbsTable
[Idx
].StatusFlags
,
1506 (UINTN
) LocalBbsTable
[Idx
].BootHandlerSegment
,
1507 (UINTN
) LocalBbsTable
[Idx
].BootHandlerOffset
,
1508 (UINTN
) ((LocalBbsTable
[Idx
].MfgStringSegment
<< 4) + LocalBbsTable
[Idx
].MfgStringOffset
),
1509 (UINTN
) ((LocalBbsTable
[Idx
].DescStringSegment
<< 4) + LocalBbsTable
[Idx
].DescStringOffset
))
1513 DEBUG ((DEBUG_ERROR
, "\n"));
1517 Set the boot priority for BBS entries based on boot option entry and boot order.
1519 @param Entry The boot option is to be checked for refresh BBS table.
1521 @retval EFI_SUCCESS The boot priority for BBS entries is refreshed successfully.
1522 @retval EFI_NOT_FOUND BBS entries can't be found.
1523 @retval EFI_OUT_OF_RESOURCES Failed to get the legacy device boot order.
1527 BdsRefreshBbsTableForBoot (
1528 IN BDS_COMMON_OPTION
*Entry
1535 HDD_INFO
*LocalHddInfo
;
1536 BBS_TABLE
*LocalBbsTable
;
1538 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1542 UINTN BootOrderSize
;
1543 UINT8
*BootOptionVar
;
1544 UINTN BootOptionSize
;
1545 CHAR16 BootOption
[9];
1548 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1550 UINTN DeviceTypeCount
;
1551 UINTN DeviceTypeIndex
;
1555 LocalHddInfo
= NULL
;
1556 LocalBbsTable
= NULL
;
1557 DevType
= BBS_UNKNOWN
;
1559 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1560 if (EFI_ERROR (Status
)) {
1564 LegacyBios
->GetBbsInfo (
1572 // First, set all the present devices' boot priority to BBS_UNPRIORITIZED_ENTRY
1573 // We will set them according to the settings setup by user
1575 for (Index
= 0; Index
< BbsCount
; Index
++) {
1576 if (!((BBS_IGNORE_ENTRY
== LocalBbsTable
[Index
].BootPriority
) ||
1577 (BBS_DO_NOT_BOOT_FROM
== LocalBbsTable
[Index
].BootPriority
) ||
1578 (BBS_LOWEST_PRIORITY
== LocalBbsTable
[Index
].BootPriority
))) {
1579 LocalBbsTable
[Index
].BootPriority
= BBS_UNPRIORITIZED_ENTRY
;
1583 // boot priority always starts at 0
1586 if (Entry
->LoadOptionsSize
== sizeof (BBS_TABLE
) + sizeof (UINT16
)) {
1588 // If Entry stands for a legacy boot option, we prioritize the devices with the same type first.
1590 DevType
= ((BBS_TABLE
*) Entry
->LoadOptions
)->DeviceType
;
1591 BbsIndex
= *(UINT16
*) ((BBS_TABLE
*) Entry
->LoadOptions
+ 1);
1592 Status
= BdsSetBootPriority4SameTypeDev (
1598 if (EFI_ERROR (Status
)) {
1603 // we have to set the boot priority for other BBS entries with different device types
1605 BootOrder
= BdsLibGetVariableAndSize (
1607 &gEfiGlobalVariableGuid
,
1610 DeviceType
= AllocatePool (BootOrderSize
+ sizeof (UINT16
));
1611 ASSERT (DeviceType
!= NULL
);
1613 DeviceType
[0] = DevType
;
1614 DeviceTypeCount
= 1;
1615 for (Index
= 0; ((BootOrder
!= NULL
) && (Index
< BootOrderSize
/ sizeof (UINT16
))); Index
++) {
1616 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
1617 BootOptionVar
= BdsLibGetVariableAndSize (
1619 &gEfiGlobalVariableGuid
,
1622 if (NULL
== BootOptionVar
) {
1626 Ptr
= BootOptionVar
;
1628 Ptr
+= sizeof (UINT32
);
1629 DevPathLen
= *(UINT16
*) Ptr
;
1630 Ptr
+= sizeof (UINT16
);
1631 Ptr
+= StrSize ((UINT16
*) Ptr
);
1632 DevPath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
1633 if (BBS_DEVICE_PATH
!= DevPath
->Type
|| BBS_BBS_DP
!= DevPath
->SubType
) {
1634 FreePool (BootOptionVar
);
1639 DevType
= ((BBS_TABLE
*) Ptr
)->DeviceType
;
1640 for (DeviceTypeIndex
= 0; DeviceTypeIndex
< DeviceTypeCount
; DeviceTypeIndex
++) {
1641 if (DeviceType
[DeviceTypeIndex
] == DevType
) {
1645 if (DeviceTypeIndex
< DeviceTypeCount
) {
1647 // We don't want to process twice for a device type
1649 FreePool (BootOptionVar
);
1653 DeviceType
[DeviceTypeCount
] = DevType
;
1656 Status
= BdsSetBootPriority4SameTypeDev (
1662 FreePool (BootOptionVar
);
1663 if (EFI_ERROR (Status
)) {
1668 FreePool (DeviceType
);
1670 if (BootOrder
!= NULL
) {
1671 FreePool (BootOrder
);
1675 PrintBbsTable (LocalBbsTable
, BbsCount
);
1682 Boot the legacy system with the boot option
1684 @param Option The legacy boot option which have BBS device path
1686 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
1688 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
1692 BdsLibDoLegacyBoot (
1693 IN BDS_COMMON_OPTION
*Option
1697 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1698 EFI_EVENT LegacyBootEvent
;
1700 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1701 if (EFI_ERROR (Status
)) {
1703 // If no LegacyBios protocol we do not support legacy boot
1705 return EFI_UNSUPPORTED
;
1708 // Notes: if we separate the int 19, then we don't need to refresh BBS
1710 BdsRefreshBbsTableForBoot (Option
);
1713 // Write boot to OS performance data for legacy boot.
1717 // Create an event to be signalled when Legacy Boot occurs to write performance data.
1719 Status
= EfiCreateEventLegacyBootEx(
1725 ASSERT_EFI_ERROR (Status
);
1728 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
1729 return LegacyBios
->LegacyBoot (
1731 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
1732 Option
->LoadOptionsSize
,
1738 Internal function to check if the input boot option is a valid EFI NV Boot####.
1740 @param OptionToCheck Boot option to be checked.
1742 @retval TRUE This boot option matches a valid EFI NV Boot####.
1743 @retval FALSE If not.
1747 IsBootOptionValidNVVarialbe (
1748 IN BDS_COMMON_OPTION
*OptionToCheck
1751 LIST_ENTRY TempList
;
1752 BDS_COMMON_OPTION
*BootOption
;
1754 CHAR16 OptionName
[20];
1758 InitializeListHead (&TempList
);
1759 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
1761 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
1762 if (BootOption
== NULL
) {
1767 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
1768 // valid EFI NV Boot####.
1770 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
1771 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
1776 FreePool (BootOption
);
1782 Check whether a USB device match the specified USB Class device path. This
1783 function follows "Load Option Processing" behavior in UEFI specification.
1785 @param UsbIo USB I/O protocol associated with the USB device.
1786 @param UsbClass The USB Class device path to match.
1788 @retval TRUE The USB device match the USB Class device path.
1789 @retval FALSE The USB device does not match the USB Class device path.
1794 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1795 IN USB_CLASS_DEVICE_PATH
*UsbClass
1799 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1800 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1802 UINT8 DeviceSubClass
;
1803 UINT8 DeviceProtocol
;
1805 if ((DevicePathType (UsbClass
) != MESSAGING_DEVICE_PATH
) ||
1806 (DevicePathSubType (UsbClass
) != MSG_USB_CLASS_DP
)){
1811 // Check Vendor Id and Product Id.
1813 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1814 if (EFI_ERROR (Status
)) {
1818 if ((UsbClass
->VendorId
!= 0xffff) &&
1819 (UsbClass
->VendorId
!= DevDesc
.IdVendor
)) {
1823 if ((UsbClass
->ProductId
!= 0xffff) &&
1824 (UsbClass
->ProductId
!= DevDesc
.IdProduct
)) {
1828 DeviceClass
= DevDesc
.DeviceClass
;
1829 DeviceSubClass
= DevDesc
.DeviceSubClass
;
1830 DeviceProtocol
= DevDesc
.DeviceProtocol
;
1831 if (DeviceClass
== 0) {
1833 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
1834 // Protocol in Interface Descriptor instead.
1836 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1837 if (EFI_ERROR (Status
)) {
1841 DeviceClass
= IfDesc
.InterfaceClass
;
1842 DeviceSubClass
= IfDesc
.InterfaceSubClass
;
1843 DeviceProtocol
= IfDesc
.InterfaceProtocol
;
1847 // Check Class, SubClass and Protocol.
1849 if ((UsbClass
->DeviceClass
!= 0xff) &&
1850 (UsbClass
->DeviceClass
!= DeviceClass
)) {
1854 if ((UsbClass
->DeviceSubClass
!= 0xff) &&
1855 (UsbClass
->DeviceSubClass
!= DeviceSubClass
)) {
1859 if ((UsbClass
->DeviceProtocol
!= 0xff) &&
1860 (UsbClass
->DeviceProtocol
!= DeviceProtocol
)) {
1868 Check whether a USB device match the specified USB WWID device path. This
1869 function follows "Load Option Processing" behavior in UEFI specification.
1871 @param UsbIo USB I/O protocol associated with the USB device.
1872 @param UsbWwid The USB WWID device path to match.
1874 @retval TRUE The USB device match the USB WWID device path.
1875 @retval FALSE The USB device does not match the USB WWID device path.
1880 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1881 IN USB_WWID_DEVICE_PATH
*UsbWwid
1885 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1886 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1887 UINT16
*LangIdTable
;
1892 CHAR16
*SerialNumberStr
;
1895 if ((DevicePathType (UsbWwid
) != MESSAGING_DEVICE_PATH
) ||
1896 (DevicePathSubType (UsbWwid
) != MSG_USB_WWID_DP
)){
1901 // Check Vendor Id and Product Id.
1903 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1904 if (EFI_ERROR (Status
)) {
1907 if ((DevDesc
.IdVendor
!= UsbWwid
->VendorId
) ||
1908 (DevDesc
.IdProduct
!= UsbWwid
->ProductId
)) {
1913 // Check Interface Number.
1915 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1916 if (EFI_ERROR (Status
)) {
1919 if (IfDesc
.InterfaceNumber
!= UsbWwid
->InterfaceNumber
) {
1924 // Check Serial Number.
1926 if (DevDesc
.StrSerialNumber
== 0) {
1931 // Get all supported languages.
1935 Status
= UsbIo
->UsbGetSupportedLanguages (UsbIo
, &LangIdTable
, &TableSize
);
1936 if (EFI_ERROR (Status
) || (TableSize
== 0) || (LangIdTable
== NULL
)) {
1941 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
1943 CompareStr
= (CHAR16
*) (UINTN
) (UsbWwid
+ 1);
1944 CompareLen
= (DevicePathNodeLength (UsbWwid
) - sizeof (USB_WWID_DEVICE_PATH
)) / sizeof (CHAR16
);
1945 if (CompareStr
[CompareLen
- 1] == L
'\0') {
1950 // Compare serial number in each supported language.
1952 for (Index
= 0; Index
< TableSize
/ sizeof (UINT16
); Index
++) {
1953 SerialNumberStr
= NULL
;
1954 Status
= UsbIo
->UsbGetStringDescriptor (
1957 DevDesc
.StrSerialNumber
,
1960 if (EFI_ERROR (Status
) || (SerialNumberStr
== NULL
)) {
1964 Length
= StrLen (SerialNumberStr
);
1965 if ((Length
>= CompareLen
) &&
1966 (CompareMem (SerialNumberStr
+ Length
- CompareLen
, CompareStr
, CompareLen
* sizeof (CHAR16
)) == 0)) {
1967 FreePool (SerialNumberStr
);
1971 FreePool (SerialNumberStr
);
1978 Find a USB device path which match the specified short-form device path start
1979 with USB Class or USB WWID device path and load the boot file then return the
1980 image handle. If ParentDevicePath is NULL, this function will search in all USB
1981 devices of the platform. If ParentDevicePath is not NULL,this function will only
1982 search in its child devices.
1984 @param ParentDevicePath The device path of the parent.
1985 @param ShortFormDevicePath The USB Class or USB WWID device path to match.
1987 @return The image Handle if find load file from specified short-form device path
1988 or NULL if not found.
1993 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
1994 IN EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
1998 UINTN UsbIoHandleCount
;
1999 EFI_HANDLE
*UsbIoHandleBuffer
;
2000 EFI_DEVICE_PATH_PROTOCOL
*UsbIoDevicePath
;
2001 EFI_USB_IO_PROTOCOL
*UsbIo
;
2005 EFI_HANDLE ImageHandle
;
2007 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
2008 EFI_DEVICE_PATH_PROTOCOL
*NextDevicePath
;
2010 FullDevicePath
= NULL
;
2014 // Get all UsbIo Handles.
2016 UsbIoHandleCount
= 0;
2017 UsbIoHandleBuffer
= NULL
;
2018 Status
= gBS
->LocateHandleBuffer (
2020 &gEfiUsbIoProtocolGuid
,
2025 if (EFI_ERROR (Status
) || (UsbIoHandleCount
== 0) || (UsbIoHandleBuffer
== NULL
)) {
2029 ParentSize
= (ParentDevicePath
== NULL
) ? 0 : GetDevicePathSize (ParentDevicePath
);
2030 for (Index
= 0; Index
< UsbIoHandleCount
; Index
++) {
2032 // Get the Usb IO interface.
2034 Status
= gBS
->HandleProtocol(
2035 UsbIoHandleBuffer
[Index
],
2036 &gEfiUsbIoProtocolGuid
,
2039 if (EFI_ERROR (Status
)) {
2043 UsbIoDevicePath
= DevicePathFromHandle (UsbIoHandleBuffer
[Index
]);
2044 if (UsbIoDevicePath
== NULL
) {
2048 if (ParentDevicePath
!= NULL
) {
2050 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
2052 Size
= GetDevicePathSize (UsbIoDevicePath
);
2053 if ((Size
< ParentSize
) ||
2054 (CompareMem (UsbIoDevicePath
, ParentDevicePath
, ParentSize
- END_DEVICE_PATH_LENGTH
) != 0)) {
2059 if (BdsMatchUsbClass (UsbIo
, (USB_CLASS_DEVICE_PATH
*) ShortFormDevicePath
) ||
2060 BdsMatchUsbWwid (UsbIo
, (USB_WWID_DEVICE_PATH
*) ShortFormDevicePath
)) {
2062 // Try to find if there is the boot file in this DevicePath
2064 NextDevicePath
= NextDevicePathNode (ShortFormDevicePath
);
2065 if (!IsDevicePathEnd (NextDevicePath
)) {
2066 FullDevicePath
= AppendDevicePath (UsbIoDevicePath
, NextDevicePath
);
2068 // Connect the full device path, so that Simple File System protocol
2069 // could be installed for this USB device.
2071 BdsLibConnectDevicePath (FullDevicePath
);
2072 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2073 Status
= gBS
->LoadImage (
2081 FreePool (FullDevicePath
);
2083 FullDevicePath
= UsbIoDevicePath
;
2084 Status
= EFI_NOT_FOUND
;
2088 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2089 // and load the image according to the default boot behavior for removable device.
2091 if (EFI_ERROR (Status
)) {
2093 // check if there is a bootable removable media could be found in this device path ,
2094 // and get the bootable media handle
2096 Handle
= BdsLibGetBootableHandle(UsbIoDevicePath
);
2097 if (Handle
== NULL
) {
2101 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2102 // machinename is ia32, ia64, x64, ...
2104 FullDevicePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2105 if (FullDevicePath
!= NULL
) {
2106 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2107 Status
= gBS
->LoadImage (
2115 if (EFI_ERROR (Status
)) {
2117 // The DevicePath failed, and it's not a valid
2118 // removable media device.
2130 FreePool (UsbIoHandleBuffer
);
2135 Expand USB Class or USB WWID device path node to be full device path of a USB
2136 device in platform then load the boot file on this full device path and return the
2139 This function support following 4 cases:
2140 1) Boot Option device path starts with a USB Class or USB WWID device path,
2141 and there is no Media FilePath device path in the end.
2142 In this case, it will follow Removable Media Boot Behavior.
2143 2) Boot Option device path starts with a USB Class or USB WWID device path,
2144 and ended with Media FilePath device path.
2145 3) Boot Option device path starts with a full device path to a USB Host Controller,
2146 contains a USB Class or USB WWID device path node, while not ended with Media
2147 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
2148 4) Boot Option device path starts with a full device path to a USB Host Controller,
2149 contains a USB Class or USB WWID device path node, and ended with Media
2150 FilePath device path.
2152 @param DevicePath The Boot Option device path.
2154 @return The image handle of boot file, or NULL if there is no boot file found in
2155 the specified USB Class or USB WWID device path.
2159 BdsExpandUsbShortFormDevicePath (
2160 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
2163 EFI_HANDLE
*ImageHandle
;
2164 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2165 EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
;
2168 // Search for USB Class or USB WWID device path node.
2170 ShortFormDevicePath
= NULL
;
2172 TempDevicePath
= DevicePath
;
2173 while (!IsDevicePathEnd (TempDevicePath
)) {
2174 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
2175 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
2176 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
2177 ShortFormDevicePath
= TempDevicePath
;
2180 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2183 if (ShortFormDevicePath
== NULL
) {
2185 // No USB Class or USB WWID device path node found, do nothing.
2190 if (ShortFormDevicePath
== DevicePath
) {
2192 // Boot Option device path starts with USB Class or USB WWID device path.
2194 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2195 if (ImageHandle
== NULL
) {
2197 // Failed to find a match in existing devices, connect the short form USB
2198 // device path and try again.
2200 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath
);
2201 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2205 // Boot Option device path contains USB Class or USB WWID device path node.
2209 // Prepare the parent device path for search.
2211 TempDevicePath
= DuplicateDevicePath (DevicePath
);
2212 ASSERT (TempDevicePath
!= NULL
);
2213 SetDevicePathEndNode (((UINT8
*) TempDevicePath
) + ((UINTN
) ShortFormDevicePath
- (UINTN
) DevicePath
));
2216 // The USB Host Controller device path is already in Boot Option device path
2217 // and USB Bus driver already support RemainingDevicePath starts with USB
2218 // Class or USB WWID device path, so just search in existing USB devices and
2219 // doesn't perform ConnectController here.
2221 ImageHandle
= BdsFindUsbDevice (TempDevicePath
, ShortFormDevicePath
);
2222 FreePool (TempDevicePath
);
2229 Process the boot option follow the UEFI specification and
2230 special treat the legacy boot option with BBS_DEVICE_PATH.
2232 @param Option The boot option need to be processed
2233 @param DevicePath The device path which describe where to load the
2234 boot image or the legacy BBS device path to boot
2236 @param ExitDataSize The size of exit data.
2237 @param ExitData Data returned when Boot image failed.
2239 @retval EFI_SUCCESS Boot from the input boot option successfully.
2240 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2245 BdsLibBootViaBootOption (
2246 IN BDS_COMMON_OPTION
*Option
,
2247 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2248 OUT UINTN
*ExitDataSize
,
2249 OUT CHAR16
**ExitData OPTIONAL
2253 EFI_STATUS StatusLogo
;
2255 EFI_HANDLE ImageHandle
;
2256 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
2257 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
2258 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
2259 LIST_ENTRY TempBootLists
;
2260 EFI_BOOT_LOGO_PROTOCOL
*BootLogo
;
2262 Status
= EFI_SUCCESS
;
2267 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
2270 WorkingDevicePath
= NULL
;
2271 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2272 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
2273 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
2274 (HARDDRIVE_DEVICE_PATH
*)DevicePath
2276 if (WorkingDevicePath
!= NULL
) {
2277 DevicePath
= WorkingDevicePath
;
2284 if (IsBootOptionValidNVVarialbe (Option
)) {
2286 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
2287 // In this case, "BootCurrent" is not created.
2288 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
2290 SetVariableAndReportStatusCodeOnError (
2292 &gEfiGlobalVariableGuid
,
2293 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2295 &Option
->BootCurrent
2300 // Signal the EVT_SIGNAL_READY_TO_BOOT event
2302 EfiSignalEventReadyToBoot();
2305 // Report Status Code to indicate ReadyToBoot event was signalled
2307 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT
));
2310 // Expand USB Class or USB WWID device path node to be full device path of a USB
2311 // device in platform then load the boot file on this full device path and get the
2314 ImageHandle
= BdsExpandUsbShortFormDevicePath (DevicePath
);
2317 // Adjust the different type memory page number just before booting
2318 // and save the updated info into the variable for next boot to use
2320 BdsSetMemoryTypeInformationVariable ();
2323 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.
2324 // Here get the ImageHandle for the non USB class or WWID device path.
2326 if (ImageHandle
== NULL
) {
2327 ASSERT (Option
->DevicePath
!= NULL
);
2328 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
2329 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
2332 // Check to see if we should legacy BOOT. If yes then do the legacy boot
2334 return BdsLibDoLegacyBoot (Option
);
2338 // If the boot option point to Internal FV shell, make sure it is valid
2340 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
2341 if (!EFI_ERROR(Status
)) {
2342 if (Option
->DevicePath
!= NULL
) {
2343 FreePool(Option
->DevicePath
);
2345 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
2346 ASSERT(Option
->DevicePath
!= NULL
);
2347 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
2349 // Update the shell boot option
2351 InitializeListHead (&TempBootLists
);
2352 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
2355 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
2357 FreePool (DevicePath
);
2358 DevicePath
= Option
->DevicePath
;
2363 if (Option
->Description
== NULL
) {
2364 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
2366 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
2372 // Report status code for OS Loader LoadImage.
2374 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2375 Status
= gBS
->LoadImage (
2385 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2386 // and load the image according to the default boot behavior for removable device.
2388 if (EFI_ERROR (Status
)) {
2390 // check if there is a bootable removable media could be found in this device path ,
2391 // and get the bootable media handle
2393 Handle
= BdsLibGetBootableHandle(DevicePath
);
2394 if (Handle
!= NULL
) {
2396 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2397 // machinename is ia32, ia64, x64, ...
2399 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2400 if (FilePath
!= NULL
) {
2401 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2402 Status
= gBS
->LoadImage (
2415 // Provide the image with it's load options
2417 if ((ImageHandle
== NULL
) || (EFI_ERROR(Status
))) {
2419 // Report Status Code to indicate that the failure to load boot option
2421 REPORT_STATUS_CODE (
2422 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2423 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR
)
2428 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
2429 ASSERT_EFI_ERROR (Status
);
2431 if (Option
->LoadOptionsSize
!= 0) {
2432 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
2433 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
2437 // Clean to NULL because the image is loaded directly from the firmwares boot manager.
2439 ImageInfo
->ParentHandle
= NULL
;
2442 // Before calling the image, enable the Watchdog Timer for
2443 // the 5 Minute period
2445 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
2448 // Write boot to OS performance data for UEFI boot
2451 BmEndOfBdsPerfCode (NULL
, NULL
);
2455 // Report status code for OS Loader StartImage.
2457 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderStart
));
2459 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
2460 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
2461 if (EFI_ERROR (Status
)) {
2463 // Report Status Code to indicate that boot failure
2465 REPORT_STATUS_CODE (
2466 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2467 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED
)
2472 // Clear the Watchdog Timer after the image returns
2474 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
2478 // Set Logo status invalid after trying one boot option
2481 StatusLogo
= gBS
->LocateProtocol (&gEfiBootLogoProtocolGuid
, NULL
, (VOID
**) &BootLogo
);
2482 if (!EFI_ERROR (StatusLogo
) && (BootLogo
!= NULL
)) {
2483 BootLogo
->SetBootLogo (BootLogo
, NULL
, 0, 0, 0, 0);
2487 // Clear Boot Current
2488 // Deleting variable with current implementation shouldn't fail.
2492 &gEfiGlobalVariableGuid
,
2493 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2503 Expand a device path that starts with a hard drive media device path node to be a
2504 full device path that includes the full hardware path to the device. We need
2505 to do this so it can be booted. As an optimization the front match (the part point
2506 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
2507 so a connect all is not required on every boot. All successful history device path
2508 which point to partition node (the front part) will be saved.
2510 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
2511 drive media device path.
2512 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
2516 EFI_DEVICE_PATH_PROTOCOL
*
2518 BdsExpandPartitionPartialDevicePathToFull (
2519 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2523 UINTN BlockIoHandleCount
;
2524 EFI_HANDLE
*BlockIoBuffer
;
2525 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
2526 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
2527 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2530 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
2531 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
2532 UINTN CachedDevicePathSize
;
2533 BOOLEAN DeviceExist
;
2535 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
2538 FullDevicePath
= NULL
;
2540 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2541 // If exist, search the front path which point to partition node in the variable instants.
2542 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system
2545 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2546 &gHdBootDevicePathVariablGuid
,
2547 (VOID
**) &CachedDevicePath
,
2548 &CachedDevicePathSize
2552 // Delete the invalid HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2554 if ((CachedDevicePath
!= NULL
) && !IsDevicePathValid (CachedDevicePath
, CachedDevicePathSize
)) {
2555 FreePool (CachedDevicePath
);
2556 CachedDevicePath
= NULL
;
2557 Status
= gRT
->SetVariable (
2558 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2559 &gHdBootDevicePathVariablGuid
,
2564 ASSERT_EFI_ERROR (Status
);
2567 if (CachedDevicePath
!= NULL
) {
2568 TempNewDevicePath
= CachedDevicePath
;
2569 DeviceExist
= FALSE
;
2573 // Check every instance of the variable
2574 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
2575 // partial partition boot option. Second, check whether the instance could be connected.
2577 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
2578 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
2580 // Connect the device path instance, the device path point to hard drive media device path node
2581 // e.g. ACPI() /PCI()/ATA()/Partition()
2583 Status
= BdsLibConnectDevicePath (Instance
);
2584 if (!EFI_ERROR (Status
)) {
2590 // Come here means the first instance is not matched
2594 } while (TempNewDevicePath
!= NULL
);
2598 // Find the matched device path.
2599 // Append the file path information from the boot option and return the fully expanded device path.
2601 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2602 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
2605 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.
2609 // First delete the matched instance.
2611 TempNewDevicePath
= CachedDevicePath
;
2612 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
2613 FreePool (TempNewDevicePath
);
2616 // Second, append the remaining path after the matched instance
2618 TempNewDevicePath
= CachedDevicePath
;
2619 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
2620 FreePool (TempNewDevicePath
);
2622 // Save the matching Device Path so we don't need to do a connect all next time
2623 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2625 Status
= gRT
->SetVariable (
2626 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2627 &gHdBootDevicePathVariablGuid
,
2628 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2629 GetDevicePathSize (CachedDevicePath
),
2634 FreePool (Instance
);
2635 FreePool (CachedDevicePath
);
2636 return FullDevicePath
;
2641 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need
2642 // to search all devices in the system for a matched partition
2644 BdsLibConnectAllDriversToAllControllers ();
2645 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
2646 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
2648 // If there was an error or there are no device handles that support
2649 // the BLOCK_IO Protocol, then return.
2654 // Loop through all the device handles that support the BLOCK_IO Protocol
2656 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
2658 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
2659 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
2663 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
2665 // Find the matched partition device path
2667 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2668 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
2671 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2673 if (CachedDevicePath
!= NULL
) {
2675 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2677 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
2678 TempNewDevicePath
= CachedDevicePath
;
2679 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
2680 FreePool(TempNewDevicePath
);
2683 if (CachedDevicePath
!= NULL
) {
2684 TempNewDevicePath
= CachedDevicePath
;
2685 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
2686 FreePool(TempNewDevicePath
);
2688 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2692 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
2693 // If the user try to boot many OS in different HDs or partitions, in theory,
2694 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.
2697 ASSERT (CachedDevicePath
!= NULL
);
2698 TempNewDevicePath
= CachedDevicePath
;
2699 while (!IsDevicePathEnd (TempNewDevicePath
)) {
2700 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2702 // Parse one instance
2704 while (!IsDevicePathEndType (TempNewDevicePath
)) {
2705 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2709 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
2711 if (InstanceNum
>= 12) {
2712 SetDevicePathEndNode (TempNewDevicePath
);
2717 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2721 // Save the matching Device Path so we don't need to do a connect all next time
2722 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2724 Status
= gRT
->SetVariable (
2725 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2726 &gHdBootDevicePathVariablGuid
,
2727 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2728 GetDevicePathSize (CachedDevicePath
),
2736 if (CachedDevicePath
!= NULL
) {
2737 FreePool (CachedDevicePath
);
2739 if (BlockIoBuffer
!= NULL
) {
2740 FreePool (BlockIoBuffer
);
2742 return FullDevicePath
;
2746 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
2747 instances, has the same partition node with HardDriveDevicePath device path
2749 @param BlockIoDevicePath Multi device path instances which need to check
2750 @param HardDriveDevicePath A device path which starts with a hard drive media
2753 @retval TRUE There is a matched device path instance.
2754 @retval FALSE There is no matched device path instance.
2759 MatchPartitionDevicePathNode (
2760 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
2761 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2764 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
2765 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2767 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
2769 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
2774 // Make PreviousDevicePath == the device path node before the end node
2776 DevicePath
= BlockIoDevicePath
;
2777 BlockIoHdDevicePathNode
= NULL
;
2780 // find the partition device path node
2782 while (!IsDevicePathEnd (DevicePath
)) {
2783 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2784 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
2786 BlockIoHdDevicePathNode
= DevicePath
;
2790 DevicePath
= NextDevicePathNode (DevicePath
);
2793 if (BlockIoHdDevicePathNode
== NULL
) {
2797 // See if the harddrive device path in blockio matches the orig Hard Drive Node
2799 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
2803 // Check for the match
2805 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
2806 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
2807 switch (TmpHdPath
->SignatureType
) {
2808 case SIGNATURE_TYPE_GUID
:
2809 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
2811 case SIGNATURE_TYPE_MBR
:
2812 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
2824 Delete the boot option associated with the handle passed in.
2826 @param Handle The handle which present the device path to create
2829 @retval EFI_SUCCESS Delete the boot option success
2830 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2831 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2832 @retval Other Error return value from SetVariable()
2836 BdsLibDeleteOptionFromHandle (
2837 IN EFI_HANDLE Handle
2841 UINT8
*BootOptionVar
;
2842 UINTN BootOrderSize
;
2843 UINTN BootOptionSize
;
2846 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2847 UINTN DevicePathSize
;
2848 UINTN OptionDevicePathSize
;
2849 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2850 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2853 Status
= EFI_SUCCESS
;
2858 // Check "BootOrder" variable, if no, means there is no any boot order.
2860 BootOrder
= BdsLibGetVariableAndSize (
2862 &gEfiGlobalVariableGuid
,
2865 if (BootOrder
== NULL
) {
2866 return EFI_NOT_FOUND
;
2870 // Convert device handle to device path protocol instance
2872 DevicePath
= DevicePathFromHandle (Handle
);
2873 if (DevicePath
== NULL
) {
2874 return EFI_NOT_FOUND
;
2876 DevicePathSize
= GetDevicePathSize (DevicePath
);
2879 // Loop all boot order variable and find the matching device path
2882 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2883 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2884 BootOptionVar
= BdsLibGetVariableAndSize (
2886 &gEfiGlobalVariableGuid
,
2890 if (BootOptionVar
== NULL
) {
2891 FreePool (BootOrder
);
2892 return EFI_OUT_OF_RESOURCES
;
2895 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2896 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2897 FreePool (BootOptionVar
);
2902 TempPtr
= BootOptionVar
;
2903 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
2904 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
2905 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
2906 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
2909 // Check whether the device path match
2911 if ((OptionDevicePathSize
== DevicePathSize
) &&
2912 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
2913 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2914 FreePool (BootOptionVar
);
2918 FreePool (BootOptionVar
);
2923 // Adjust number of boot option for "BootOrder" variable.
2925 Status
= gRT
->SetVariable (
2927 &gEfiGlobalVariableGuid
,
2928 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2933 // Shrinking variable with existing variable implementation shouldn't fail.
2935 ASSERT_EFI_ERROR (Status
);
2937 FreePool (BootOrder
);
2944 Delete all invalid EFI boot options.
2946 @retval EFI_SUCCESS Delete all invalid boot option success
2947 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
2948 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2949 @retval Other Error return value from SetVariable()
2953 BdsDeleteAllInvalidEfiBootOption (
2958 UINT8
*BootOptionVar
;
2959 UINTN BootOrderSize
;
2960 UINTN BootOptionSize
;
2964 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2965 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2967 CHAR16
*Description
;
2970 Status
= EFI_SUCCESS
;
2973 OptionDevicePath
= NULL
;
2978 // Check "BootOrder" variable firstly, this variable hold the number of boot options
2980 BootOrder
= BdsLibGetVariableAndSize (
2982 &gEfiGlobalVariableGuid
,
2985 if (NULL
== BootOrder
) {
2986 return EFI_NOT_FOUND
;
2990 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2991 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2992 BootOptionVar
= BdsLibGetVariableAndSize (
2994 &gEfiGlobalVariableGuid
,
2997 if (NULL
== BootOptionVar
) {
2998 FreePool (BootOrder
);
2999 return EFI_OUT_OF_RESOURCES
;
3002 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
3005 TempPtr
= BootOptionVar
;
3006 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
3007 Description
= (CHAR16
*) TempPtr
;
3008 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
3009 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
3012 // Skip legacy boot option (BBS boot device)
3014 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
3015 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
3016 FreePool (BootOptionVar
);
3022 if (Corrupted
|| !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
3024 // Delete this invalid boot option "Boot####"
3026 Status
= gRT
->SetVariable (
3028 &gEfiGlobalVariableGuid
,
3029 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3034 // Deleting variable with current variable implementation shouldn't fail.
3036 ASSERT_EFI_ERROR (Status
);
3038 // Mark this boot option in boot order as deleted
3040 BootOrder
[Index
] = 0xffff;
3044 FreePool (BootOptionVar
);
3049 // Adjust boot order array
3052 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
3053 if (BootOrder
[Index
] != 0xffff) {
3054 BootOrder
[Index2
] = BootOrder
[Index
];
3058 Status
= gRT
->SetVariable (
3060 &gEfiGlobalVariableGuid
,
3061 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3062 Index2
* sizeof (UINT16
),
3066 // Shrinking variable with current variable implementation shouldn't fail.
3068 ASSERT_EFI_ERROR (Status
);
3070 FreePool (BootOrder
);
3077 For EFI boot option, BDS separate them as six types:
3078 1. Network - The boot option points to the SimpleNetworkProtocol device.
3079 Bds will try to automatically create this type boot option when enumerate.
3080 2. Shell - The boot option points to internal flash shell.
3081 Bds will try to automatically create this type boot option when enumerate.
3082 3. Removable BlockIo - The boot option only points to the removable media
3083 device, like USB flash disk, DVD, Floppy etc.
3084 These device should contain a *removable* blockIo
3085 protocol in their device handle.
3086 Bds will try to automatically create this type boot option
3088 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
3090 These device should contain a *fixed* blockIo
3091 protocol in their device handle.
3092 BDS will skip fixed blockIo devices, and NOT
3093 automatically create boot option for them. But BDS
3094 will help to delete those fixed blockIo boot option,
3095 whose description rule conflict with other auto-created
3097 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
3098 has SimpleFileSystem Protocol, but has no blockio
3099 protocol. These devices do not offer blockIo
3100 protocol, but BDS still can get the
3101 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
3103 6. File - The boot option points to a file. These boot options are usually
3104 created by user manually or OS loader. BDS will not delete or modify
3107 This function will enumerate all possible boot device in the system, and
3108 automatically create boot options for Network, Shell, Removable BlockIo,
3109 and Non-BlockIo Simplefile devices.
3110 It will only execute once of every boot.
3112 @param BdsBootOptionList The header of the link list which indexed all
3113 current boot options
3115 @retval EFI_SUCCESS Finished all the boot device enumerate and create
3116 the boot option base on that boot device
3118 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
3122 BdsLibEnumerateAllBootOption (
3123 IN OUT LIST_ENTRY
*BdsBootOptionList
3127 UINT16 FloppyNumber
;
3128 UINT16 HarddriveNumber
;
3133 UINT16 NonBlockNumber
;
3134 UINTN NumberBlockIoHandles
;
3135 EFI_HANDLE
*BlockIoHandles
;
3136 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
3137 BOOLEAN Removable
[2];
3138 UINTN RemovableIndex
;
3140 UINTN NumOfLoadFileHandles
;
3141 EFI_HANDLE
*LoadFileHandles
;
3142 UINTN FvHandleCount
;
3143 EFI_HANDLE
*FvHandleBuffer
;
3144 EFI_FV_FILETYPE Type
;
3146 EFI_FV_FILE_ATTRIBUTES Attributes
;
3147 UINT32 AuthenticationStatus
;
3148 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
3149 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3150 UINTN DevicePathType
;
3152 EFI_HANDLE
*FileSystemHandles
;
3153 UINTN NumberFileSystemHandles
;
3155 EFI_IMAGE_DOS_HEADER DosHeader
;
3158 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3159 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3162 HarddriveNumber
= 0;
3169 ZeroMem (Buffer
, sizeof (Buffer
));
3172 // If the boot device enumerate happened, just get the boot
3173 // device from the boot order variable
3175 if (mEnumBootDevice
) {
3176 GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME
, &gLastEnumLangGuid
, (VOID
**)&LastLang
, NULL
);
3177 GetEfiGlobalVariable2 (L
"PlatformLang", (VOID
**)&PlatLang
, NULL
);
3178 ASSERT (PlatLang
!= NULL
);
3179 if ((LastLang
!= NULL
) && (AsciiStrCmp (LastLang
, PlatLang
) == 0)) {
3180 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3181 FreePool (LastLang
);
3182 FreePool (PlatLang
);
3185 Status
= gRT
->SetVariable (
3186 LAST_ENUM_LANGUAGE_VARIABLE_NAME
,
3188 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3189 AsciiStrSize (PlatLang
),
3193 // Failure to set the variable only impacts the performance next time enumerating the boot options.
3196 if (LastLang
!= NULL
) {
3197 FreePool (LastLang
);
3199 FreePool (PlatLang
);
3204 // Notes: this dirty code is to get the legacy boot option from the
3205 // BBS table and create to variable as the EFI boot option, it should
3206 // be removed after the CSM can provide legacy boot option directly
3208 REFRESH_LEGACY_BOOT_OPTIONS
;
3211 // Delete invalid boot option
3213 BdsDeleteAllInvalidEfiBootOption ();
3216 // Parse removable media followed by fixed media.
3217 // The Removable[] array is used by the for-loop below to create removable media boot options
3218 // at first, and then to create fixed media boot options.
3220 Removable
[0] = FALSE
;
3221 Removable
[1] = TRUE
;
3223 gBS
->LocateHandleBuffer (
3225 &gEfiBlockIoProtocolGuid
,
3227 &NumberBlockIoHandles
,
3231 for (RemovableIndex
= 0; RemovableIndex
< 2; RemovableIndex
++) {
3232 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
3233 Status
= gBS
->HandleProtocol (
3234 BlockIoHandles
[Index
],
3235 &gEfiBlockIoProtocolGuid
,
3239 // skip the logical partition
3241 if (EFI_ERROR (Status
) || BlkIo
->Media
->LogicalPartition
) {
3246 // firstly fixed block io then the removable block io
3248 if (BlkIo
->Media
->RemovableMedia
== Removable
[RemovableIndex
]) {
3251 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
3252 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
3254 switch (DevicePathType
) {
3255 case BDS_EFI_ACPI_FLOPPY_BOOT
:
3256 if (FloppyNumber
!= 0) {
3257 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
3259 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
3261 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3266 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.
3268 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
3269 case BDS_EFI_MESSAGE_SATA_BOOT
:
3270 if (BlkIo
->Media
->RemovableMedia
) {
3271 if (CdromNumber
!= 0) {
3272 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
3274 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
3278 if (HarddriveNumber
!= 0) {
3279 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)), HarddriveNumber
);
3281 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)));
3285 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
3286 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3289 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
3290 if (UsbNumber
!= 0) {
3291 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
3293 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
3295 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3299 case BDS_EFI_MESSAGE_SCSI_BOOT
:
3300 if (ScsiNumber
!= 0) {
3301 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
3303 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
3305 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3309 case BDS_EFI_MESSAGE_MISC_BOOT
:
3311 if (MiscNumber
!= 0) {
3312 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
3314 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
3316 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3323 if (NumberBlockIoHandles
!= 0) {
3324 FreePool (BlockIoHandles
);
3328 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
3331 gBS
->LocateHandleBuffer (
3333 &gEfiSimpleFileSystemProtocolGuid
,
3335 &NumberFileSystemHandles
,
3338 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
3339 Status
= gBS
->HandleProtocol (
3340 FileSystemHandles
[Index
],
3341 &gEfiBlockIoProtocolGuid
,
3344 if (!EFI_ERROR (Status
)) {
3346 // Skip if the file system handle supports a BlkIo protocol,
3352 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
3353 // machinename is ia32, ia64, x64, ...
3355 Hdr
.Union
= &HdrData
;
3357 Status
= BdsLibGetImageHeader (
3358 FileSystemHandles
[Index
],
3359 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3363 if (!EFI_ERROR (Status
) &&
3364 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3365 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3371 // No such file or the file is not a EFI application, delete this boot option
3373 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
3375 if (NonBlockNumber
!= 0) {
3376 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
3378 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
3380 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
3385 if (NumberFileSystemHandles
!= 0) {
3386 FreePool (FileSystemHandles
);
3390 // Parse Network Boot Device
3392 NumOfLoadFileHandles
= 0;
3394 // Search Load File protocol for PXE boot option.
3396 gBS
->LocateHandleBuffer (
3398 &gEfiLoadFileProtocolGuid
,
3400 &NumOfLoadFileHandles
,
3404 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
3406 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)), Index
);
3408 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)));
3410 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
3413 if (NumOfLoadFileHandles
!= 0) {
3414 FreePool (LoadFileHandles
);
3418 // Check if we have on flash shell
3420 gBS
->LocateHandleBuffer (
3422 &gEfiFirmwareVolume2ProtocolGuid
,
3427 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
3428 gBS
->HandleProtocol (
3429 FvHandleBuffer
[Index
],
3430 &gEfiFirmwareVolume2ProtocolGuid
,
3434 Status
= Fv
->ReadFile (
3436 PcdGetPtr(PcdShellFile
),
3441 &AuthenticationStatus
3443 if (EFI_ERROR (Status
)) {
3445 // Skip if no shell file in the FV
3450 // Build the shell boot option
3452 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
3455 if (FvHandleCount
!= 0) {
3456 FreePool (FvHandleBuffer
);
3459 // Make sure every boot only have one time
3460 // boot device enumerate
3462 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3463 mEnumBootDevice
= TRUE
;
3469 Build the boot option with the handle parsed in
3471 @param Handle The handle which present the device path to create
3473 @param BdsBootOptionList The header of the link list which indexed all
3474 current boot options
3475 @param String The description of the boot option.
3480 BdsLibBuildOptionFromHandle (
3481 IN EFI_HANDLE Handle
,
3482 IN LIST_ENTRY
*BdsBootOptionList
,
3486 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3488 DevicePath
= DevicePathFromHandle (Handle
);
3491 // Create and register new boot option
3493 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
3498 Build the on flash shell boot option with the handle parsed in.
3500 @param Handle The handle which present the device path to create
3501 on flash shell boot option
3502 @param BdsBootOptionList The header of the link list which indexed all
3503 current boot options
3508 BdsLibBuildOptionFromShell (
3509 IN EFI_HANDLE Handle
,
3510 IN OUT LIST_ENTRY
*BdsBootOptionList
3513 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3514 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
3516 DevicePath
= DevicePathFromHandle (Handle
);
3519 // Build the shell device path
3521 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
3523 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
3526 // Create and register the shell boot option
3528 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
3533 Boot from the UEFI spec defined "BootNext" variable.
3546 BDS_COMMON_OPTION
*BootOption
;
3547 LIST_ENTRY TempList
;
3552 // Init the boot option name buffer and temp link list
3554 InitializeListHead (&TempList
);
3555 ZeroMem (Buffer
, sizeof (Buffer
));
3557 BootNext
= BdsLibGetVariableAndSize (
3559 &gEfiGlobalVariableGuid
,
3564 // Clear the boot next variable first
3566 if (BootNext
!= NULL
) {
3567 Status
= gRT
->SetVariable (
3569 &gEfiGlobalVariableGuid
,
3570 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3575 // Deleting variable with current variable implementation shouldn't fail.
3577 ASSERT_EFI_ERROR (Status
);
3580 // Start to build the boot option and try to boot
3582 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
3583 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
3584 ASSERT (BootOption
!= NULL
);
3585 BdsLibConnectDevicePath (BootOption
->DevicePath
);
3586 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
3587 FreePool(BootOption
);
3594 Return the bootable media handle.
3595 First, check the device is connected
3596 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
3597 Third, detect the the default boot file in the Media, and return the removable Media handle.
3599 @param DevicePath Device Path to a bootable device
3601 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
3606 BdsLibGetBootableHandle (
3607 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3612 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3613 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
3615 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
3617 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3620 EFI_HANDLE ReturnHandle
;
3621 EFI_HANDLE
*SimpleFileSystemHandles
;
3623 UINTN NumberSimpleFileSystemHandles
;
3625 EFI_IMAGE_DOS_HEADER DosHeader
;
3626 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3627 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3629 UpdatedDevicePath
= DevicePath
;
3632 // Enter to critical section to protect the acquired BlockIo instance
3633 // from getting released due to the USB mass storage hotplug event
3635 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
3638 // Check whether the device is connected
3640 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3641 if (EFI_ERROR (Status
)) {
3643 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
3645 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3646 if (EFI_ERROR (Status
)) {
3648 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
3650 UpdatedDevicePath
= DevicePath
;
3651 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3652 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3656 // For removable device boot option, its contained device path only point to the removable device handle,
3657 // should make sure all its children handles (its child partion or media handles) are created and connected.
3659 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3661 // Get BlockIo protocol and check removable attribute
3663 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
3664 ASSERT_EFI_ERROR (Status
);
3667 // Issue a dummy read to the device to check for media change.
3668 // When the removable media is changed, any Block IO read/write will
3669 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
3670 // returned. After the Block IO protocol is reinstalled, subsequent
3671 // Block IO read/write will success.
3673 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
3674 if (Buffer
!= NULL
) {
3675 BlockIo
->ReadBlocks (
3677 BlockIo
->Media
->MediaId
,
3679 BlockIo
->Media
->BlockSize
,
3687 // Detect the the default boot file from removable Media
3691 // 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
3692 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
3694 DupDevicePath
= DuplicateDevicePath (DevicePath
);
3695 ASSERT (DupDevicePath
!= NULL
);
3697 UpdatedDevicePath
= DupDevicePath
;
3698 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3700 // 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
3701 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
3703 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
3704 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
3706 // Remove the usb node, let the device path only point to PCI node
3708 SetDevicePathEndNode (UpdatedDevicePath
);
3709 UpdatedDevicePath
= DupDevicePath
;
3711 UpdatedDevicePath
= DevicePath
;
3715 // Get the device path size of boot option
3717 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3718 ReturnHandle
= NULL
;
3719 gBS
->LocateHandleBuffer (
3721 &gEfiSimpleFileSystemProtocolGuid
,
3723 &NumberSimpleFileSystemHandles
,
3724 &SimpleFileSystemHandles
3726 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
3728 // Get the device path size of SimpleFileSystem handle
3730 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
3731 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3733 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
3735 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
3737 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
3738 // machinename is ia32, ia64, x64, ...
3740 Hdr
.Union
= &HdrData
;
3741 Status
= BdsLibGetImageHeader (
3742 SimpleFileSystemHandles
[Index
],
3743 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3747 if (!EFI_ERROR (Status
) &&
3748 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3749 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3750 ReturnHandle
= SimpleFileSystemHandles
[Index
];
3756 FreePool(DupDevicePath
);
3758 if (SimpleFileSystemHandles
!= NULL
) {
3759 FreePool(SimpleFileSystemHandles
);
3762 gBS
->RestoreTPL (OldTpl
);
3764 return ReturnHandle
;
3768 Check to see if the network cable is plugged in. If the DevicePath is not
3769 connected it will be connected.
3771 @param DevicePath Device Path to check
3773 @retval TRUE DevicePath points to an Network that is connected
3774 @retval FALSE DevicePath does not point to a bootable network
3778 BdsLibNetworkBootWithMediaPresent (
3779 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3783 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3785 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
3786 BOOLEAN MediaPresent
;
3787 UINT32 InterruptStatus
;
3789 MediaPresent
= FALSE
;
3791 UpdatedDevicePath
= DevicePath
;
3793 // Locate Load File Protocol for PXE boot option first
3795 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3796 if (EFI_ERROR (Status
)) {
3798 // Device not present so see if we need to connect it
3800 Status
= BdsLibConnectDevicePath (DevicePath
);
3801 if (!EFI_ERROR (Status
)) {
3803 // This one should work after we did the connect
3805 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3809 if (!EFI_ERROR (Status
)) {
3810 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
3811 if (EFI_ERROR (Status
)) {
3813 // Failed to open SNP from this handle, try to get SNP from parent handle
3815 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
3816 if (UpdatedDevicePath
!= NULL
) {
3817 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3818 if (!EFI_ERROR (Status
)) {
3820 // SNP handle found, get SNP from it
3822 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
3827 if (!EFI_ERROR (Status
)) {
3828 if (Snp
->Mode
->MediaPresentSupported
) {
3829 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
3831 // Invoke Snp->GetStatus() to refresh the media status
3833 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
3836 // In case some one else is using the SNP check to see if it's connected
3838 MediaPresent
= Snp
->Mode
->MediaPresent
;
3841 // No one is using SNP so we need to Start and Initialize so
3842 // MediaPresent will be valid.
3844 Status
= Snp
->Start (Snp
);
3845 if (!EFI_ERROR (Status
)) {
3846 Status
= Snp
->Initialize (Snp
, 0, 0);
3847 if (!EFI_ERROR (Status
)) {
3848 MediaPresent
= Snp
->Mode
->MediaPresent
;
3849 Snp
->Shutdown (Snp
);
3855 MediaPresent
= TRUE
;
3860 return MediaPresent
;
3864 For a bootable Device path, return its boot type.
3866 @param DevicePath The bootable device Path to check
3868 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3869 which subtype is MEDIA_HARDDRIVE_DP
3870 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3871 which subtype is MEDIA_CDROM_DP
3872 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
3873 which HID is floppy device.
3874 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3875 and its last device path node's subtype is MSG_ATAPI_DP.
3876 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3877 and its last device path node's subtype is MSG_SCSI_DP.
3878 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3879 and its last device path node's subtype is MSG_USB_DP.
3880 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
3881 its last device path node point to a message device path node.
3882 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
3883 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
3888 BdsGetBootTypeFromDevicePath (
3889 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3892 ACPI_HID_DEVICE_PATH
*Acpi
;
3893 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3894 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
3897 if (NULL
== DevicePath
) {
3898 return BDS_EFI_UNSUPPORT
;
3901 TempDevicePath
= DevicePath
;
3903 while (!IsDevicePathEndType (TempDevicePath
)) {
3904 switch (DevicePathType (TempDevicePath
)) {
3905 case BBS_DEVICE_PATH
:
3906 return BDS_LEGACY_BBS_BOOT
;
3907 case MEDIA_DEVICE_PATH
:
3908 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
3909 return BDS_EFI_MEDIA_HD_BOOT
;
3910 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
3911 return BDS_EFI_MEDIA_CDROM_BOOT
;
3914 case ACPI_DEVICE_PATH
:
3915 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
3916 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
3917 return BDS_EFI_ACPI_FLOPPY_BOOT
;
3920 case MESSAGING_DEVICE_PATH
:
3922 // Get the last device path node
3924 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
3925 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
3927 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
3930 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
3933 // if the device path not only point to driver device, it is not a messaging device path,
3935 if (!IsDevicePathEndType (LastDeviceNode
)) {
3939 switch (DevicePathSubType (TempDevicePath
)) {
3941 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
3945 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
3949 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
3953 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
3956 case MSG_MAC_ADDR_DP
:
3960 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
3964 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
3972 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
3975 return BDS_EFI_UNSUPPORT
;
3979 Check whether the Device path in a boot option point to a valid bootable device,
3980 And if CheckMedia is true, check the device is ready to boot now.
3982 @param DevPath the Device path in a boot option
3983 @param CheckMedia if true, check the device is ready to boot now.
3985 @retval TRUE the Device path is valid
3986 @retval FALSE the Device path is invalid .
3991 BdsLibIsValidEFIBootOptDevicePath (
3992 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
3993 IN BOOLEAN CheckMedia
3996 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
4000 Check whether the Device path in a boot option point to a valid bootable device,
4001 And if CheckMedia is true, check the device is ready to boot now.
4002 If Description is not NULL and the device path point to a fixed BlockIo
4003 device, check the description whether conflict with other auto-created
4006 @param DevPath the Device path in a boot option
4007 @param CheckMedia if true, check the device is ready to boot now.
4008 @param Description the description in a boot option
4010 @retval TRUE the Device path is valid
4011 @retval FALSE the Device path is invalid .
4016 BdsLibIsValidEFIBootOptDevicePathExt (
4017 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
4018 IN BOOLEAN CheckMedia
,
4019 IN CHAR16
*Description
4024 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4025 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4026 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
4028 TempDevicePath
= DevPath
;
4029 LastDeviceNode
= DevPath
;
4032 // Check if it's a valid boot option for network boot device.
4033 // Check if there is EfiLoadFileProtocol installed.
4034 // If yes, that means there is a boot option for network.
4036 Status
= gBS
->LocateDevicePath (
4037 &gEfiLoadFileProtocolGuid
,
4041 if (EFI_ERROR (Status
)) {
4043 // Device not present so see if we need to connect it
4045 TempDevicePath
= DevPath
;
4046 BdsLibConnectDevicePath (TempDevicePath
);
4047 Status
= gBS
->LocateDevicePath (
4048 &gEfiLoadFileProtocolGuid
,
4054 if (!EFI_ERROR (Status
)) {
4055 if (!IsDevicePathEnd (TempDevicePath
)) {
4057 // LoadFile protocol is not installed on handle with exactly the same DevPath
4064 // Test if it is ready to boot now
4066 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
4075 // If the boot option point to a file, it is a valid EFI boot option,
4076 // and assume it is ready to boot now
4078 while (!IsDevicePathEnd (TempDevicePath
)) {
4080 // If there is USB Class or USB WWID device path node, treat it as valid EFI
4081 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it
4082 // to full device path.
4084 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
4085 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
4086 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
4090 LastDeviceNode
= TempDevicePath
;
4091 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4093 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
4094 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
4099 // Check if it's a valid boot option for internal FV application
4101 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
4103 // If the boot option point to internal FV application, make sure it is valid
4105 TempDevicePath
= DevPath
;
4106 Status
= BdsLibUpdateFvFileDevicePath (
4108 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
)
4110 if (Status
== EFI_ALREADY_STARTED
) {
4113 if (Status
== EFI_SUCCESS
) {
4114 FreePool (TempDevicePath
);
4121 // If the boot option point to a blockIO device:
4122 // if it is a removable blockIo device, it is valid.
4123 // if it is a fixed blockIo device, check its description confliction.
4125 TempDevicePath
= DevPath
;
4126 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4127 if (EFI_ERROR (Status
)) {
4129 // Device not present so see if we need to connect it
4131 Status
= BdsLibConnectDevicePath (DevPath
);
4132 if (!EFI_ERROR (Status
)) {
4134 // Try again to get the Block Io protocol after we did the connect
4136 TempDevicePath
= DevPath
;
4137 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4141 if (!EFI_ERROR (Status
)) {
4142 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
4143 if (!EFI_ERROR (Status
)) {
4146 // Test if it is ready to boot now
4148 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4157 // 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,
4159 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
4160 if (!EFI_ERROR (Status
)) {
4163 // Test if it is ready to boot now
4165 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4179 According to a file guild, check a Fv file device path is valid. If it is invalid,
4180 try to return the valid device path.
4181 FV address maybe changes for memory layout adjust from time to time, use this function
4182 could promise the Fv file device path is right.
4184 @param DevicePath on input, the Fv file device path need to check on
4185 output, the updated valid Fv file device path
4186 @param FileGuid the Fv file guild
4188 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
4190 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
4192 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
4194 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
4195 and return the updated device path in DevicePath
4200 BdsLibUpdateFvFileDevicePath (
4201 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
4202 IN EFI_GUID
*FileGuid
4205 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4206 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4208 EFI_GUID
*GuidPoint
;
4210 UINTN FvHandleCount
;
4211 EFI_HANDLE
*FvHandleBuffer
;
4212 EFI_FV_FILETYPE Type
;
4214 EFI_FV_FILE_ATTRIBUTES Attributes
;
4215 UINT32 AuthenticationStatus
;
4217 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
4218 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
4219 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
4220 EFI_HANDLE FoundFvHandle
;
4221 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
4223 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
4224 return EFI_INVALID_PARAMETER
;
4226 if (FileGuid
== NULL
) {
4227 return EFI_INVALID_PARAMETER
;
4231 // Check whether the device path point to the default the input Fv file
4233 TempDevicePath
= *DevicePath
;
4234 LastDeviceNode
= TempDevicePath
;
4235 while (!IsDevicePathEnd (TempDevicePath
)) {
4236 LastDeviceNode
= TempDevicePath
;
4237 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4239 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
4240 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
4242 if (GuidPoint
== NULL
) {
4244 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
4246 return EFI_UNSUPPORTED
;
4248 if (!CompareGuid (GuidPoint
, FileGuid
)) {
4250 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
4252 return EFI_UNSUPPORTED
;
4256 // Check whether the input Fv file device path is valid
4258 TempDevicePath
= *DevicePath
;
4259 FoundFvHandle
= NULL
;
4260 Status
= gBS
->LocateDevicePath (
4261 &gEfiFirmwareVolume2ProtocolGuid
,
4265 if (!EFI_ERROR (Status
)) {
4266 Status
= gBS
->HandleProtocol (
4268 &gEfiFirmwareVolume2ProtocolGuid
,
4271 if (!EFI_ERROR (Status
)) {
4273 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
4275 Status
= Fv
->ReadFile (
4282 &AuthenticationStatus
4284 if (!EFI_ERROR (Status
)) {
4285 return EFI_ALREADY_STARTED
;
4291 // Look for the input wanted FV file in current FV
4292 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
4295 FoundFvHandle
= NULL
;
4296 Status
= gBS
->HandleProtocol (
4298 &gEfiLoadedImageProtocolGuid
,
4299 (VOID
**) &LoadedImage
4301 if (!EFI_ERROR (Status
)) {
4302 Status
= gBS
->HandleProtocol (
4303 LoadedImage
->DeviceHandle
,
4304 &gEfiFirmwareVolume2ProtocolGuid
,
4307 if (!EFI_ERROR (Status
)) {
4308 Status
= Fv
->ReadFile (
4315 &AuthenticationStatus
4317 if (!EFI_ERROR (Status
)) {
4319 FoundFvHandle
= LoadedImage
->DeviceHandle
;
4324 // Second, if fail to find, try to enumerate all FV
4327 FvHandleBuffer
= NULL
;
4328 gBS
->LocateHandleBuffer (
4330 &gEfiFirmwareVolume2ProtocolGuid
,
4335 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
4336 gBS
->HandleProtocol (
4337 FvHandleBuffer
[Index
],
4338 &gEfiFirmwareVolume2ProtocolGuid
,
4342 Status
= Fv
->ReadFile (
4349 &AuthenticationStatus
4351 if (EFI_ERROR (Status
)) {
4353 // Skip if input Fv file not in the FV
4358 FoundFvHandle
= FvHandleBuffer
[Index
];
4362 if (FvHandleBuffer
!= NULL
) {
4363 FreePool (FvHandleBuffer
);
4369 // Build the shell device path
4371 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
4372 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
4373 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
4374 ASSERT (NewDevicePath
!= NULL
);
4375 *DevicePath
= NewDevicePath
;
4378 return EFI_NOT_FOUND
;