2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2014, 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
;
22 The constructor function register UNI strings into imageHandle.
24 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
26 @param ImageHandle The firmware allocated handle for the EFI image.
27 @param SystemTable A pointer to the EFI System Table.
29 @retval EFI_SUCCESS The constructor successfully added string package.
30 @retval Other value The constructor can't add string package.
35 GenericBdsLibConstructor (
36 IN EFI_HANDLE ImageHandle
,
37 IN EFI_SYSTEM_TABLE
*SystemTable
41 gBdsLibStringPackHandle
= HiiAddPackages (
42 &gBdsLibStringPackageGuid
,
48 ASSERT (gBdsLibStringPackHandle
!= NULL
);
54 Deletete the Boot Option from EFI Variable. The Boot Order Arrray
57 @param OptionNumber The number of Boot option want to be deleted.
58 @param BootOrder The Boot Order array.
59 @param BootOrderSize The size of the Boot Order Array.
61 @retval EFI_SUCCESS The Boot Option Variable was found and removed
62 @retval EFI_UNSUPPORTED The Boot Option Variable store was inaccessible
63 @retval EFI_NOT_FOUND The Boot Option Variable was not found
68 IN UINTN OptionNumber
,
69 IN OUT UINT16
*BootOrder
,
70 IN OUT UINTN
*BootOrderSize
77 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", OptionNumber
);
78 Status
= gRT
->SetVariable (
80 &gEfiGlobalVariableGuid
,
86 // Deleting variable with existing variable implementation shouldn't fail.
88 ASSERT_EFI_ERROR (Status
);
91 // adjust boot order array
93 for (Index
= 0; Index
< *BootOrderSize
/ sizeof (UINT16
); Index
++) {
94 if (BootOrder
[Index
] == OptionNumber
) {
95 CopyMem (&BootOrder
[Index
], &BootOrder
[Index
+1], *BootOrderSize
- (Index
+1) * sizeof (UINT16
));
96 *BootOrderSize
-= sizeof (UINT16
);
105 Translate the first n characters of an Ascii string to
106 Unicode characters. The count n is indicated by parameter
107 Size. If Size is greater than the length of string, then
108 the entire string is translated.
111 @param AStr Pointer to input Ascii string.
112 @param Size The number of characters to translate.
113 @param UStr Pointer to output Unicode string buffer.
126 while (AStr
[Idx
] != 0) {
127 UStr
[Idx
] = (CHAR16
) AStr
[Idx
];
138 Build Legacy Device Name String according.
140 @param CurBBSEntry BBS Table.
142 @param BufSize The buffer size.
143 @param BootString The output string.
147 BdsBuildLegacyDevNameString (
148 IN BBS_TABLE
*CurBBSEntry
,
151 OUT CHAR16
*BootString
164 Fmt
= L
"Primary Master %s";
171 Fmt
= L
"Primary Slave %s";
178 Fmt
= L
"Secondary Master %s";
185 Fmt
= L
"Secondary Slave %s";
193 switch (CurBBSEntry
->DeviceType
) {
214 case BBS_EMBED_NETWORK
:
228 // If current BBS entry has its description then use it.
230 StringDesc
= (UINT8
*) (UINTN
) ((CurBBSEntry
->DescStringSegment
<< 4) + CurBBSEntry
->DescStringOffset
);
231 if (NULL
!= StringDesc
) {
233 // Only get fisrt 32 characters, this is suggested by BBS spec
235 AsciiToUnicodeSize (StringDesc
, 32, Temp
);
241 // BbsTable 16 entries are for onboard IDE.
242 // Set description string for SATA harddisks, Harddisk 0 ~ Harddisk 11
244 if (Index
>= 5 && Index
<= 16 && (CurBBSEntry
->DeviceType
== BBS_HARDDISK
|| CurBBSEntry
->DeviceType
== BBS_CDROM
)) {
246 UnicodeSPrint (BootString
, BufSize
, Fmt
, Type
, Index
- 5);
248 UnicodeSPrint (BootString
, BufSize
, Fmt
, Type
);
254 Create a legacy boot option for the specified entry of
255 BBS table, save it as variable, and append it to the boot
259 @param CurrentBbsEntry Pointer to current BBS table.
260 @param CurrentBbsDevPath Pointer to the Device Path Protocol instance of BBS
261 @param Index Index of the specified entry in BBS table.
262 @param BootOrderList On input, the original boot order list.
263 On output, the new boot order list attached with the
265 @param BootOrderListSize On input, the original size of boot order list.
266 On output, the size of new boot order list.
268 @retval EFI_SUCCESS Boot Option successfully created.
269 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
270 @retval Other Error occurs while setting variable.
274 BdsCreateLegacyBootOption (
275 IN BBS_TABLE
*CurrentBbsEntry
,
276 IN EFI_DEVICE_PATH_PROTOCOL
*CurrentBbsDevPath
,
278 IN OUT UINT16
**BootOrderList
,
279 IN OUT UINTN
*BootOrderListSize
283 UINT16 CurrentBootOptionNo
;
284 UINT16 BootString
[10];
285 CHAR16 BootDesc
[100];
286 CHAR8 HelpString
[100];
287 UINT16
*NewBootOrderList
;
292 UINT16 CurrentBbsDevPathSize
;
293 UINTN BootOrderIndex
;
294 UINTN BootOrderLastIndex
;
296 BOOLEAN IndexNotFound
;
297 BBS_BBS_DEVICE_PATH
*NewBbsDevPathNode
;
299 if ((*BootOrderList
) == NULL
) {
300 CurrentBootOptionNo
= 0;
302 for (ArrayIndex
= 0; ArrayIndex
< (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
)); ArrayIndex
++) {
303 IndexNotFound
= TRUE
;
304 for (BootOrderIndex
= 0; BootOrderIndex
< (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
)); BootOrderIndex
++) {
305 if ((*BootOrderList
)[BootOrderIndex
] == ArrayIndex
) {
306 IndexNotFound
= FALSE
;
311 if (!IndexNotFound
) {
318 CurrentBootOptionNo
= (UINT16
) ArrayIndex
;
328 BdsBuildLegacyDevNameString (CurrentBbsEntry
, Index
, sizeof (BootDesc
), BootDesc
);
331 // Create new BBS device path node with description string
333 UnicodeStrToAsciiStr (BootDesc
, HelpString
);
335 StringLen
= AsciiStrLen (HelpString
);
336 NewBbsDevPathNode
= AllocateZeroPool (sizeof (BBS_BBS_DEVICE_PATH
) + StringLen
);
337 if (NewBbsDevPathNode
== NULL
) {
338 return EFI_OUT_OF_RESOURCES
;
340 CopyMem (NewBbsDevPathNode
, CurrentBbsDevPath
, sizeof (BBS_BBS_DEVICE_PATH
));
341 CopyMem (NewBbsDevPathNode
->String
, HelpString
, StringLen
+ 1);
342 SetDevicePathNodeLength (&(NewBbsDevPathNode
->Header
), sizeof (BBS_BBS_DEVICE_PATH
) + StringLen
);
345 // Create entire new CurrentBbsDevPath with end node
347 CurrentBbsDevPath
= AppendDevicePathNode (
349 (EFI_DEVICE_PATH_PROTOCOL
*) NewBbsDevPathNode
351 if (CurrentBbsDevPath
== NULL
) {
352 FreePool (NewBbsDevPathNode
);
353 return EFI_OUT_OF_RESOURCES
;
356 CurrentBbsDevPathSize
= (UINT16
) (GetDevicePathSize (CurrentBbsDevPath
));
358 BufferSize
= sizeof (UINT32
) +
361 CurrentBbsDevPathSize
+
365 Buffer
= AllocateZeroPool (BufferSize
);
366 if (Buffer
== NULL
) {
367 FreePool (NewBbsDevPathNode
);
368 FreePool (CurrentBbsDevPath
);
369 return EFI_OUT_OF_RESOURCES
;
372 Ptr
= (UINT8
*) Buffer
;
374 *((UINT32
*) Ptr
) = LOAD_OPTION_ACTIVE
;
375 Ptr
+= sizeof (UINT32
);
377 *((UINT16
*) Ptr
) = CurrentBbsDevPathSize
;
378 Ptr
+= sizeof (UINT16
);
385 Ptr
+= StrSize (BootDesc
);
390 CurrentBbsDevPathSize
392 Ptr
+= CurrentBbsDevPathSize
;
400 Ptr
+= sizeof (BBS_TABLE
);
401 *((UINT16
*) Ptr
) = (UINT16
) Index
;
403 Status
= gRT
->SetVariable (
405 &gEfiGlobalVariableGuid
,
406 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
415 NewBootOrderList
= AllocateZeroPool (*BootOrderListSize
+ sizeof (UINT16
));
416 if (NULL
== NewBootOrderList
) {
417 FreePool (NewBbsDevPathNode
);
418 FreePool (CurrentBbsDevPath
);
419 return EFI_OUT_OF_RESOURCES
;
422 if (*BootOrderList
!= NULL
) {
423 CopyMem (NewBootOrderList
, *BootOrderList
, *BootOrderListSize
);
424 FreePool (*BootOrderList
);
427 BootOrderLastIndex
= (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
));
428 NewBootOrderList
[BootOrderLastIndex
] = CurrentBootOptionNo
;
429 *BootOrderListSize
+= sizeof (UINT16
);
430 *BootOrderList
= NewBootOrderList
;
432 FreePool (NewBbsDevPathNode
);
433 FreePool (CurrentBbsDevPath
);
438 Check if the boot option is a legacy one.
440 @param BootOptionVar The boot option data payload.
441 @param BbsEntry The BBS Table.
442 @param BbsIndex The table index.
444 @retval TRUE It is a legacy boot option.
445 @retval FALSE It is not a legacy boot option.
449 BdsIsLegacyBootOption (
450 IN UINT8
*BootOptionVar
,
451 OUT BBS_TABLE
**BbsEntry
,
456 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
461 Ptr
+= sizeof (UINT32
);
462 DevPathLen
= *(UINT16
*) Ptr
;
463 Ptr
+= sizeof (UINT16
);
464 Ptr
+= StrSize ((UINT16
*) Ptr
);
465 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
466 if ((BBS_DEVICE_PATH
== DevicePath
->Type
) && (BBS_BBS_DP
== DevicePath
->SubType
)) {
468 *BbsEntry
= (BBS_TABLE
*) Ptr
;
469 Ptr
+= sizeof (BBS_TABLE
);
470 *BbsIndex
= *(UINT16
*) Ptr
;
481 Delete all the invalid legacy boot options.
483 @retval EFI_SUCCESS All invalide legacy boot options are deleted.
484 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
485 @retval EFI_NOT_FOUND Fail to retrive variable of boot order.
489 BdsDeleteAllInvalidLegacyBootOptions (
494 UINT8
*BootOptionVar
;
496 UINTN BootOptionSize
;
500 HDD_INFO
*LocalHddInfo
;
501 BBS_TABLE
*LocalBbsTable
;
504 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
506 UINT16 BootOption
[10];
507 UINT16 BootDesc
[100];
508 BOOLEAN DescStringMatch
;
510 Status
= EFI_SUCCESS
;
516 LocalBbsTable
= NULL
;
519 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
520 if (EFI_ERROR (Status
)) {
524 BootOrder
= BdsLibGetVariableAndSize (
526 &gEfiGlobalVariableGuid
,
529 if (BootOrder
== NULL
) {
530 return EFI_NOT_FOUND
;
533 LegacyBios
->GetBbsInfo (
542 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
543 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
544 BootOptionVar
= BdsLibGetVariableAndSize (
546 &gEfiGlobalVariableGuid
,
549 if (NULL
== BootOptionVar
) {
551 Status
= gRT
->GetVariable (
553 &gEfiGlobalVariableGuid
,
558 if (Status
== EFI_NOT_FOUND
) {
562 BdsDeleteBootOption (
569 FreePool (BootOrder
);
570 return EFI_OUT_OF_RESOURCES
;
575 // Skip Non-Legacy boot option
577 if (!BdsIsLegacyBootOption (BootOptionVar
, &BbsEntry
, &BbsIndex
)) {
578 if (BootOptionVar
!= NULL
) {
579 FreePool (BootOptionVar
);
585 if (BbsIndex
< BbsCount
) {
587 // Check if BBS Description String is changed
589 DescStringMatch
= FALSE
;
590 BdsBuildLegacyDevNameString (
591 &LocalBbsTable
[BbsIndex
],
597 if (StrCmp (BootDesc
, (UINT16
*)(BootOptionVar
+ sizeof (UINT32
) + sizeof (UINT16
))) == 0) {
598 DescStringMatch
= TRUE
;
601 if (!((LocalBbsTable
[BbsIndex
].BootPriority
== BBS_IGNORE_ENTRY
) ||
602 (LocalBbsTable
[BbsIndex
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)) &&
603 (LocalBbsTable
[BbsIndex
].DeviceType
== BbsEntry
->DeviceType
) &&
610 if (BootOptionVar
!= NULL
) {
611 FreePool (BootOptionVar
);
616 BdsDeleteBootOption (
624 // Adjust the number of boot options.
626 Status
= gRT
->SetVariable (
628 &gEfiGlobalVariableGuid
,
629 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
634 // Shrinking variable with existing variable implementation shouldn't fail.
636 ASSERT_EFI_ERROR (Status
);
637 FreePool (BootOrder
);
643 Find all legacy boot option by device type.
645 @param BootOrder The boot order array.
646 @param BootOptionNum The number of boot option.
647 @param DevType Device type.
648 @param DevName Device name.
649 @param Attribute The boot option attribute.
650 @param BbsIndex The BBS table index.
651 @param OptionNumber The boot option index.
653 @retval TRUE The Legacy boot option is found.
654 @retval FALSE The legacy boot option is not found.
658 BdsFindLegacyBootOptionByDevTypeAndName (
659 IN UINT16
*BootOrder
,
660 IN UINTN BootOptionNum
,
663 OUT UINT32
*Attribute
,
664 OUT UINT16
*BbsIndex
,
665 OUT UINT16
*OptionNumber
669 CHAR16 BootOption
[9];
670 UINTN BootOptionSize
;
671 UINT8
*BootOptionVar
;
678 if (NULL
== BootOrder
) {
683 // Loop all boot option from variable
685 for (Index
= 0; Index
< BootOptionNum
; Index
++) {
686 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", (UINTN
) BootOrder
[Index
]);
687 BootOptionVar
= BdsLibGetVariableAndSize (
689 &gEfiGlobalVariableGuid
,
692 if (NULL
== BootOptionVar
) {
697 // Skip Non-legacy boot option
699 if (!BdsIsLegacyBootOption (BootOptionVar
, &BbsEntry
, BbsIndex
)) {
700 FreePool (BootOptionVar
);
705 (BbsEntry
->DeviceType
!= DevType
) ||
706 (StrCmp (DevName
, (CHAR16
*)(BootOptionVar
+ sizeof (UINT32
) + sizeof (UINT16
))) != 0)
708 FreePool (BootOptionVar
);
712 *Attribute
= *(UINT32
*) BootOptionVar
;
713 *OptionNumber
= BootOrder
[Index
];
715 FreePool (BootOptionVar
);
723 Create a legacy boot option.
725 @param BbsItem The BBS Table entry.
726 @param Index Index of the specified entry in BBS table.
727 @param BootOrderList The boot order list.
728 @param BootOrderListSize The size of boot order list.
730 @retval EFI_OUT_OF_RESOURCE No enough memory.
731 @retval EFI_SUCCESS The function complete successfully.
732 @return Other value if the legacy boot option is not created.
736 BdsCreateOneLegacyBootOption (
737 IN BBS_TABLE
*BbsItem
,
739 IN OUT UINT16
**BootOrderList
,
740 IN OUT UINTN
*BootOrderListSize
743 BBS_BBS_DEVICE_PATH BbsDevPathNode
;
745 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
750 // Create device path node.
752 BbsDevPathNode
.Header
.Type
= BBS_DEVICE_PATH
;
753 BbsDevPathNode
.Header
.SubType
= BBS_BBS_DP
;
754 SetDevicePathNodeLength (&BbsDevPathNode
.Header
, sizeof (BBS_BBS_DEVICE_PATH
));
755 BbsDevPathNode
.DeviceType
= BbsItem
->DeviceType
;
756 CopyMem (&BbsDevPathNode
.StatusFlag
, &BbsItem
->StatusFlags
, sizeof (UINT16
));
758 DevPath
= AppendDevicePathNode (
760 (EFI_DEVICE_PATH_PROTOCOL
*) &BbsDevPathNode
762 if (NULL
== DevPath
) {
763 return EFI_OUT_OF_RESOURCES
;
766 Status
= BdsCreateLegacyBootOption (
773 BbsItem
->BootPriority
= 0x00;
781 Add the legacy boot options from BBS table if they do not exist.
783 @retval EFI_SUCCESS The boot options are added successfully
784 or they are already in boot options.
785 @retval EFI_NOT_FOUND No legacy boot options is found.
786 @retval EFI_OUT_OF_RESOURCE No enough memory.
787 @return Other value LegacyBoot options are not added.
791 BdsAddNonExistingLegacyBootOptions (
801 HDD_INFO
*LocalHddInfo
;
802 BBS_TABLE
*LocalBbsTable
;
804 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
813 LocalBbsTable
= NULL
;
815 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
816 if (EFI_ERROR (Status
)) {
820 LegacyBios
->GetBbsInfo (
828 BootOrder
= BdsLibGetVariableAndSize (
830 &gEfiGlobalVariableGuid
,
833 if (BootOrder
== NULL
) {
837 for (Index
= 0; Index
< BbsCount
; Index
++) {
838 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
839 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
844 BdsBuildLegacyDevNameString (&LocalBbsTable
[Index
], Index
, sizeof (Desc
), Desc
);
846 Exist
= BdsFindLegacyBootOptionByDevTypeAndName (
848 BootOrderSize
/ sizeof (UINT16
),
849 LocalBbsTable
[Index
].DeviceType
,
857 // Not found such type of legacy device in boot options or we found but it's disabled
858 // so we have to create one and put it to the tail of boot order list
860 Status
= BdsCreateOneLegacyBootOption (
861 &LocalBbsTable
[Index
],
866 if (!EFI_ERROR (Status
)) {
867 ASSERT (BootOrder
!= NULL
);
869 OptionNumber
= BootOrder
[BootOrderSize
/ sizeof (UINT16
) - 1];
873 ASSERT (BbsIndex
== Index
);
876 Status
= gRT
->SetVariable (
878 &gEfiGlobalVariableGuid
,
879 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
883 if (BootOrder
!= NULL
) {
884 FreePool (BootOrder
);
891 Fill the device order buffer.
893 @param BbsTable The BBS table.
894 @param BbsType The BBS Type.
895 @param BbsCount The BBS Count.
896 @param Buf device order buffer.
898 @return The device order buffer.
903 IN BBS_TABLE
*BbsTable
,
911 for (Index
= 0; Index
< BbsCount
; Index
++) {
912 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
916 if (BbsTable
[Index
].DeviceType
!= BbsType
) {
920 *Buf
= (UINT16
) (Index
& 0xFF);
928 Create the device order buffer.
930 @param BbsTable The BBS table.
931 @param BbsCount The BBS Count.
933 @retval EFI_SUCCES The buffer is created and the EFI variable named
934 VAR_LEGACY_DEV_ORDER and gEfiLegacyDevOrderVariableGuid is
936 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
937 @retval EFI_DEVICE_ERROR Fail to add the device order into EFI variable fail
938 because of hardware error.
942 IN BBS_TABLE
*BbsTable
,
954 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
955 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
964 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
966 Status
= EFI_SUCCESS
;
969 // Count all boot devices
971 for (Index
= 0; Index
< BbsCount
; Index
++) {
972 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
976 switch (BbsTable
[Index
].DeviceType
) {
989 case BBS_EMBED_NETWORK
:
1002 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * FDCount
);
1003 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * HDCount
);
1004 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * CDCount
);
1005 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * NETCount
);
1006 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * BEVCount
);
1009 // Create buffer to hold all boot device order
1011 DevOrder
= AllocateZeroPool (TotalSize
);
1012 if (NULL
== DevOrder
) {
1013 return EFI_OUT_OF_RESOURCES
;
1015 DevOrderPtr
= DevOrder
;
1017 DevOrderPtr
->BbsType
= BBS_FLOPPY
;
1018 DevOrderPtr
->Length
= (UINT16
) (sizeof (DevOrderPtr
->Length
) + FDCount
* sizeof (UINT16
));
1019 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_FLOPPY
, BbsCount
, DevOrderPtr
->Data
);
1021 DevOrderPtr
->BbsType
= BBS_HARDDISK
;
1022 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1023 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_HARDDISK
, BbsCount
, DevOrderPtr
->Data
);
1025 DevOrderPtr
->BbsType
= BBS_CDROM
;
1026 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1027 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_CDROM
, BbsCount
, DevOrderPtr
->Data
);
1029 DevOrderPtr
->BbsType
= BBS_EMBED_NETWORK
;
1030 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1031 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_EMBED_NETWORK
, BbsCount
, DevOrderPtr
->Data
);
1033 DevOrderPtr
->BbsType
= BBS_BEV_DEVICE
;
1034 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1035 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_BEV_DEVICE
, BbsCount
, DevOrderPtr
->Data
);
1037 ASSERT (TotalSize
== (UINTN
) ((UINT8
*) DevOrderPtr
- (UINT8
*) DevOrder
));
1040 // Save device order for legacy boot device to variable.
1042 Status
= gRT
->SetVariable (
1043 VAR_LEGACY_DEV_ORDER
,
1044 &gEfiLegacyDevOrderVariableGuid
,
1045 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1049 FreePool (DevOrder
);
1055 Add the legacy boot devices from BBS table into
1056 the legacy device boot order.
1058 @retval EFI_SUCCESS The boot devices are added successfully.
1059 @retval EFI_NOT_FOUND The legacy boot devices are not found.
1060 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
1061 @retval EFI_DEVICE_ERROR Fail to add the legacy device boot order into EFI variable
1062 because of hardware error.
1066 BdsUpdateLegacyDevOrder (
1070 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1071 LEGACY_DEV_ORDER_ENTRY
*NewDevOrder
;
1072 LEGACY_DEV_ORDER_ENTRY
*Ptr
;
1073 LEGACY_DEV_ORDER_ENTRY
*NewPtr
;
1075 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1079 HDD_INFO
*LocalHddInfo
;
1080 BBS_TABLE
*LocalBbsTable
;
1110 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
1118 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1119 if (EFI_ERROR (Status
)) {
1123 Status
= LegacyBios
->GetBbsInfo (
1130 if (EFI_ERROR (Status
)) {
1134 DevOrder
= BdsLibGetVariableAndSize (
1135 VAR_LEGACY_DEV_ORDER
,
1136 &gEfiLegacyDevOrderVariableGuid
,
1139 if (NULL
== DevOrder
) {
1140 return BdsCreateDevOrder (LocalBbsTable
, BbsCount
);
1143 // First we figure out how many boot devices with same device type respectively
1145 for (Index
= 0; Index
< BbsCount
; Index
++) {
1146 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1147 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1152 switch (LocalBbsTable
[Index
].DeviceType
) {
1165 case BBS_EMBED_NETWORK
:
1169 case BBS_BEV_DEVICE
:
1178 TotalSize
+= (HeaderSize
+ FDCount
* sizeof (UINT16
));
1179 TotalSize
+= (HeaderSize
+ HDCount
* sizeof (UINT16
));
1180 TotalSize
+= (HeaderSize
+ CDCount
* sizeof (UINT16
));
1181 TotalSize
+= (HeaderSize
+ NETCount
* sizeof (UINT16
));
1182 TotalSize
+= (HeaderSize
+ BEVCount
* sizeof (UINT16
));
1184 NewDevOrder
= AllocateZeroPool (TotalSize
);
1185 if (NULL
== NewDevOrder
) {
1186 return EFI_OUT_OF_RESOURCES
;
1195 NewPtr
= NewDevOrder
;
1196 NewPtr
->BbsType
= Ptr
->BbsType
;
1197 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + FDCount
* sizeof (UINT16
));
1198 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1199 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1200 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1201 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_FLOPPY
1206 NewPtr
->Data
[FDIndex
] = Ptr
->Data
[Index
];
1209 NewFDPtr
= NewPtr
->Data
;
1214 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1215 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1216 NewPtr
->BbsType
= Ptr
->BbsType
;
1217 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1218 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1219 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1220 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1221 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1222 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_HARDDISK
1227 NewPtr
->Data
[HDIndex
] = Ptr
->Data
[Index
];
1230 NewHDPtr
= NewPtr
->Data
;
1235 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1236 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1237 NewPtr
->BbsType
= Ptr
->BbsType
;
1238 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1239 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1240 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1241 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1242 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1243 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_CDROM
1248 NewPtr
->Data
[CDIndex
] = Ptr
->Data
[Index
];
1251 NewCDPtr
= NewPtr
->Data
;
1256 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1257 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1258 NewPtr
->BbsType
= Ptr
->BbsType
;
1259 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1260 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1261 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1262 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1263 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1264 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_EMBED_NETWORK
1269 NewPtr
->Data
[NETIndex
] = Ptr
->Data
[Index
];
1272 NewNETPtr
= NewPtr
->Data
;
1277 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1278 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1279 NewPtr
->BbsType
= Ptr
->BbsType
;
1280 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1281 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1282 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1283 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1284 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1285 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_BEV_DEVICE
1290 NewPtr
->Data
[BEVIndex
] = Ptr
->Data
[Index
];
1293 NewBEVPtr
= NewPtr
->Data
;
1295 for (Index
= 0; Index
< BbsCount
; Index
++) {
1296 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1297 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1302 switch (LocalBbsTable
[Index
].DeviceType
) {
1305 NewDevPtr
= NewFDPtr
;
1310 NewDevPtr
= NewHDPtr
;
1315 NewDevPtr
= NewCDPtr
;
1318 case BBS_EMBED_NETWORK
:
1320 NewDevPtr
= NewNETPtr
;
1323 case BBS_BEV_DEVICE
:
1325 NewDevPtr
= NewBEVPtr
;
1333 // at this point we have copied those valid indexes to new buffer
1334 // and we should check if there is any new appeared boot device
1337 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1338 if ((NewDevPtr
[Index2
] & 0xFF) == (UINT16
) Index
) {
1343 if (Index2
== *Idx
) {
1345 // Index2 == *Idx means we didn't find Index
1346 // so Index is a new appeared device's index in BBS table
1347 // insert it before disabled indexes.
1349 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1350 if ((NewDevPtr
[Index2
] & 0xFF00) == 0xFF00) {
1354 CopyMem (&NewDevPtr
[Index2
+ 1], &NewDevPtr
[Index2
], (*Idx
- Index2
) * sizeof (UINT16
));
1355 NewDevPtr
[Index2
] = (UINT16
) (Index
& 0xFF);
1361 FreePool (DevOrder
);
1363 Status
= gRT
->SetVariable (
1364 VAR_LEGACY_DEV_ORDER
,
1365 &gEfiLegacyDevOrderVariableGuid
,
1366 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1370 FreePool (NewDevOrder
);
1376 Set Boot Priority for specified device type.
1378 @param DeviceType The device type.
1379 @param BbsIndex The BBS index to set the highest priority. Ignore when -1.
1380 @param LocalBbsTable The BBS table.
1381 @param Priority The prority table.
1383 @retval EFI_SUCCESS The function completes successfully.
1384 @retval EFI_NOT_FOUND Failed to find device.
1385 @retval EFI_OUT_OF_RESOURCES Failed to get the efi variable of device order.
1389 BdsSetBootPriority4SameTypeDev (
1390 IN UINT16 DeviceType
,
1392 IN OUT BBS_TABLE
*LocalBbsTable
,
1393 IN OUT UINT16
*Priority
1396 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1397 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
1401 DevOrder
= BdsLibGetVariableAndSize (
1402 VAR_LEGACY_DEV_ORDER
,
1403 &gEfiLegacyDevOrderVariableGuid
,
1406 if (NULL
== DevOrder
) {
1407 return EFI_OUT_OF_RESOURCES
;
1410 DevOrderPtr
= DevOrder
;
1411 while ((UINT8
*) DevOrderPtr
< (UINT8
*) DevOrder
+ DevOrderSize
) {
1412 if (DevOrderPtr
->BbsType
== DeviceType
) {
1416 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) ((UINTN
) DevOrderPtr
+ sizeof (BBS_TYPE
) + DevOrderPtr
->Length
);
1419 if ((UINT8
*) DevOrderPtr
>= (UINT8
*) DevOrder
+ DevOrderSize
) {
1420 FreePool (DevOrder
);
1421 return EFI_NOT_FOUND
;
1424 if (BbsIndex
!= (UINTN
) -1) {
1425 LocalBbsTable
[BbsIndex
].BootPriority
= *Priority
;
1429 // If the high byte of the DevIndex is 0xFF, it indicates that this device has been disabled.
1431 for (Index
= 0; Index
< DevOrderPtr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1432 if ((DevOrderPtr
->Data
[Index
] & 0xFF00) == 0xFF00) {
1434 // LocalBbsTable[DevIndex[Index] & 0xFF].BootPriority = BBS_DISABLED_ENTRY;
1436 } else if (DevOrderPtr
->Data
[Index
] != BbsIndex
) {
1437 LocalBbsTable
[DevOrderPtr
->Data
[Index
]].BootPriority
= *Priority
;
1442 FreePool (DevOrder
);
1447 Print the BBS Table.
1449 @param LocalBbsTable The BBS table.
1450 @param BbsCount The count of entry in BBS table.
1454 IN BBS_TABLE
*LocalBbsTable
,
1460 DEBUG ((DEBUG_ERROR
, "\n"));
1461 DEBUG ((DEBUG_ERROR
, " NO Prio bb/dd/ff cl/sc Type Stat segm:offs\n"));
1462 DEBUG ((DEBUG_ERROR
, "=============================================\n"));
1463 for (Idx
= 0; Idx
< BbsCount
; Idx
++) {
1464 if ((LocalBbsTable
[Idx
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1465 (LocalBbsTable
[Idx
].BootPriority
== BBS_DO_NOT_BOOT_FROM
) ||
1466 (LocalBbsTable
[Idx
].BootPriority
== BBS_LOWEST_PRIORITY
)
1473 " %02x: %04x %02x/%02x/%02x %02x/%02x %04x %04x %04x:%04x\n",
1475 (UINTN
) LocalBbsTable
[Idx
].BootPriority
,
1476 (UINTN
) LocalBbsTable
[Idx
].Bus
,
1477 (UINTN
) LocalBbsTable
[Idx
].Device
,
1478 (UINTN
) LocalBbsTable
[Idx
].Function
,
1479 (UINTN
) LocalBbsTable
[Idx
].Class
,
1480 (UINTN
) LocalBbsTable
[Idx
].SubClass
,
1481 (UINTN
) LocalBbsTable
[Idx
].DeviceType
,
1482 (UINTN
) * (UINT16
*) &LocalBbsTable
[Idx
].StatusFlags
,
1483 (UINTN
) LocalBbsTable
[Idx
].BootHandlerSegment
,
1484 (UINTN
) LocalBbsTable
[Idx
].BootHandlerOffset
,
1485 (UINTN
) ((LocalBbsTable
[Idx
].MfgStringSegment
<< 4) + LocalBbsTable
[Idx
].MfgStringOffset
),
1486 (UINTN
) ((LocalBbsTable
[Idx
].DescStringSegment
<< 4) + LocalBbsTable
[Idx
].DescStringOffset
))
1490 DEBUG ((DEBUG_ERROR
, "\n"));
1494 Set the boot priority for BBS entries based on boot option entry and boot order.
1496 @param Entry The boot option is to be checked for refresh BBS table.
1498 @retval EFI_SUCCESS The boot priority for BBS entries is refreshed successfully.
1499 @retval EFI_NOT_FOUND BBS entries can't be found.
1500 @retval EFI_OUT_OF_RESOURCES Failed to get the legacy device boot order.
1504 BdsRefreshBbsTableForBoot (
1505 IN BDS_COMMON_OPTION
*Entry
1512 HDD_INFO
*LocalHddInfo
;
1513 BBS_TABLE
*LocalBbsTable
;
1515 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1519 UINTN BootOrderSize
;
1520 UINT8
*BootOptionVar
;
1521 UINTN BootOptionSize
;
1522 CHAR16 BootOption
[9];
1525 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1527 UINTN DeviceTypeCount
;
1528 UINTN DeviceTypeIndex
;
1532 LocalHddInfo
= NULL
;
1533 LocalBbsTable
= NULL
;
1534 DevType
= BBS_UNKNOWN
;
1536 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1537 if (EFI_ERROR (Status
)) {
1541 LegacyBios
->GetBbsInfo (
1549 // First, set all the present devices' boot priority to BBS_UNPRIORITIZED_ENTRY
1550 // We will set them according to the settings setup by user
1552 for (Index
= 0; Index
< BbsCount
; Index
++) {
1553 if (!((BBS_IGNORE_ENTRY
== LocalBbsTable
[Index
].BootPriority
) ||
1554 (BBS_DO_NOT_BOOT_FROM
== LocalBbsTable
[Index
].BootPriority
) ||
1555 (BBS_LOWEST_PRIORITY
== LocalBbsTable
[Index
].BootPriority
))) {
1556 LocalBbsTable
[Index
].BootPriority
= BBS_UNPRIORITIZED_ENTRY
;
1560 // boot priority always starts at 0
1563 if (Entry
->LoadOptionsSize
== sizeof (BBS_TABLE
) + sizeof (UINT16
)) {
1565 // If Entry stands for a legacy boot option, we prioritize the devices with the same type first.
1567 DevType
= ((BBS_TABLE
*) Entry
->LoadOptions
)->DeviceType
;
1568 BbsIndex
= *(UINT16
*) ((BBS_TABLE
*) Entry
->LoadOptions
+ 1);
1569 Status
= BdsSetBootPriority4SameTypeDev (
1575 if (EFI_ERROR (Status
)) {
1580 // we have to set the boot priority for other BBS entries with different device types
1582 BootOrder
= BdsLibGetVariableAndSize (
1584 &gEfiGlobalVariableGuid
,
1587 DeviceType
= AllocatePool (BootOrderSize
+ sizeof (UINT16
));
1588 ASSERT (DeviceType
!= NULL
);
1590 DeviceType
[0] = DevType
;
1591 DeviceTypeCount
= 1;
1592 for (Index
= 0; ((BootOrder
!= NULL
) && (Index
< BootOrderSize
/ sizeof (UINT16
))); Index
++) {
1593 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
1594 BootOptionVar
= BdsLibGetVariableAndSize (
1596 &gEfiGlobalVariableGuid
,
1599 if (NULL
== BootOptionVar
) {
1603 Ptr
= BootOptionVar
;
1605 Ptr
+= sizeof (UINT32
);
1606 DevPathLen
= *(UINT16
*) Ptr
;
1607 Ptr
+= sizeof (UINT16
);
1608 Ptr
+= StrSize ((UINT16
*) Ptr
);
1609 DevPath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
1610 if (BBS_DEVICE_PATH
!= DevPath
->Type
|| BBS_BBS_DP
!= DevPath
->SubType
) {
1611 FreePool (BootOptionVar
);
1616 DevType
= ((BBS_TABLE
*) Ptr
)->DeviceType
;
1617 for (DeviceTypeIndex
= 0; DeviceTypeIndex
< DeviceTypeCount
; DeviceTypeIndex
++) {
1618 if (DeviceType
[DeviceTypeIndex
] == DevType
) {
1622 if (DeviceTypeIndex
< DeviceTypeCount
) {
1624 // We don't want to process twice for a device type
1626 FreePool (BootOptionVar
);
1630 DeviceType
[DeviceTypeCount
] = DevType
;
1633 Status
= BdsSetBootPriority4SameTypeDev (
1639 FreePool (BootOptionVar
);
1640 if (EFI_ERROR (Status
)) {
1645 FreePool (DeviceType
);
1647 if (BootOrder
!= NULL
) {
1648 FreePool (BootOrder
);
1652 PrintBbsTable (LocalBbsTable
, BbsCount
);
1659 Boot the legacy system with the boot option
1661 @param Option The legacy boot option which have BBS device path
1663 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
1665 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
1669 BdsLibDoLegacyBoot (
1670 IN BDS_COMMON_OPTION
*Option
1674 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1675 EFI_EVENT LegacyBootEvent
;
1677 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1678 if (EFI_ERROR (Status
)) {
1680 // If no LegacyBios protocol we do not support legacy boot
1682 return EFI_UNSUPPORTED
;
1685 // Notes: if we separate the int 19, then we don't need to refresh BBS
1687 BdsRefreshBbsTableForBoot (Option
);
1690 // Write boot to OS performance data for legacy boot.
1694 // Create an event to be signalled when Legacy Boot occurs to write performance data.
1696 Status
= EfiCreateEventLegacyBootEx(
1698 WriteBootToOsPerformanceData
,
1702 ASSERT_EFI_ERROR (Status
);
1705 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
1706 return LegacyBios
->LegacyBoot (
1708 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
1709 Option
->LoadOptionsSize
,
1715 Internal function to check if the input boot option is a valid EFI NV Boot####.
1717 @param OptionToCheck Boot option to be checked.
1719 @retval TRUE This boot option matches a valid EFI NV Boot####.
1720 @retval FALSE If not.
1724 IsBootOptionValidNVVarialbe (
1725 IN BDS_COMMON_OPTION
*OptionToCheck
1728 LIST_ENTRY TempList
;
1729 BDS_COMMON_OPTION
*BootOption
;
1731 CHAR16 OptionName
[20];
1735 InitializeListHead (&TempList
);
1736 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
1738 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
1739 if (BootOption
== NULL
) {
1744 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
1745 // valid EFI NV Boot####.
1747 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
1748 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
1753 FreePool (BootOption
);
1759 Check whether a USB device match the specified USB Class device path. This
1760 function follows "Load Option Processing" behavior in UEFI specification.
1762 @param UsbIo USB I/O protocol associated with the USB device.
1763 @param UsbClass The USB Class device path to match.
1765 @retval TRUE The USB device match the USB Class device path.
1766 @retval FALSE The USB device does not match the USB Class device path.
1771 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1772 IN USB_CLASS_DEVICE_PATH
*UsbClass
1776 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1777 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1779 UINT8 DeviceSubClass
;
1780 UINT8 DeviceProtocol
;
1782 if ((DevicePathType (UsbClass
) != MESSAGING_DEVICE_PATH
) ||
1783 (DevicePathSubType (UsbClass
) != MSG_USB_CLASS_DP
)){
1788 // Check Vendor Id and Product Id.
1790 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1791 if (EFI_ERROR (Status
)) {
1795 if ((UsbClass
->VendorId
!= 0xffff) &&
1796 (UsbClass
->VendorId
!= DevDesc
.IdVendor
)) {
1800 if ((UsbClass
->ProductId
!= 0xffff) &&
1801 (UsbClass
->ProductId
!= DevDesc
.IdProduct
)) {
1805 DeviceClass
= DevDesc
.DeviceClass
;
1806 DeviceSubClass
= DevDesc
.DeviceSubClass
;
1807 DeviceProtocol
= DevDesc
.DeviceProtocol
;
1808 if (DeviceClass
== 0) {
1810 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
1811 // Protocol in Interface Descriptor instead.
1813 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1814 if (EFI_ERROR (Status
)) {
1818 DeviceClass
= IfDesc
.InterfaceClass
;
1819 DeviceSubClass
= IfDesc
.InterfaceSubClass
;
1820 DeviceProtocol
= IfDesc
.InterfaceProtocol
;
1824 // Check Class, SubClass and Protocol.
1826 if ((UsbClass
->DeviceClass
!= 0xff) &&
1827 (UsbClass
->DeviceClass
!= DeviceClass
)) {
1831 if ((UsbClass
->DeviceSubClass
!= 0xff) &&
1832 (UsbClass
->DeviceSubClass
!= DeviceSubClass
)) {
1836 if ((UsbClass
->DeviceProtocol
!= 0xff) &&
1837 (UsbClass
->DeviceProtocol
!= DeviceProtocol
)) {
1845 Check whether a USB device match the specified USB WWID device path. This
1846 function follows "Load Option Processing" behavior in UEFI specification.
1848 @param UsbIo USB I/O protocol associated with the USB device.
1849 @param UsbWwid The USB WWID device path to match.
1851 @retval TRUE The USB device match the USB WWID device path.
1852 @retval FALSE The USB device does not match the USB WWID device path.
1857 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1858 IN USB_WWID_DEVICE_PATH
*UsbWwid
1862 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1863 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1864 UINT16
*LangIdTable
;
1869 CHAR16
*SerialNumberStr
;
1872 if ((DevicePathType (UsbWwid
) != MESSAGING_DEVICE_PATH
) ||
1873 (DevicePathSubType (UsbWwid
) != MSG_USB_WWID_DP
)){
1878 // Check Vendor Id and Product Id.
1880 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1881 if (EFI_ERROR (Status
)) {
1884 if ((DevDesc
.IdVendor
!= UsbWwid
->VendorId
) ||
1885 (DevDesc
.IdProduct
!= UsbWwid
->ProductId
)) {
1890 // Check Interface Number.
1892 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1893 if (EFI_ERROR (Status
)) {
1896 if (IfDesc
.InterfaceNumber
!= UsbWwid
->InterfaceNumber
) {
1901 // Check Serial Number.
1903 if (DevDesc
.StrSerialNumber
== 0) {
1908 // Get all supported languages.
1912 Status
= UsbIo
->UsbGetSupportedLanguages (UsbIo
, &LangIdTable
, &TableSize
);
1913 if (EFI_ERROR (Status
) || (TableSize
== 0) || (LangIdTable
== NULL
)) {
1918 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
1920 CompareStr
= (CHAR16
*) (UINTN
) (UsbWwid
+ 1);
1921 CompareLen
= (DevicePathNodeLength (UsbWwid
) - sizeof (USB_WWID_DEVICE_PATH
)) / sizeof (CHAR16
);
1922 if (CompareStr
[CompareLen
- 1] == L
'\0') {
1927 // Compare serial number in each supported language.
1929 for (Index
= 0; Index
< TableSize
/ sizeof (UINT16
); Index
++) {
1930 SerialNumberStr
= NULL
;
1931 Status
= UsbIo
->UsbGetStringDescriptor (
1934 DevDesc
.StrSerialNumber
,
1937 if (EFI_ERROR (Status
) || (SerialNumberStr
== NULL
)) {
1941 Length
= StrLen (SerialNumberStr
);
1942 if ((Length
>= CompareLen
) &&
1943 (CompareMem (SerialNumberStr
+ Length
- CompareLen
, CompareStr
, CompareLen
* sizeof (CHAR16
)) == 0)) {
1944 FreePool (SerialNumberStr
);
1948 FreePool (SerialNumberStr
);
1955 Find a USB device path which match the specified short-form device path start
1956 with USB Class or USB WWID device path and load the boot file then return the
1957 image handle. If ParentDevicePath is NULL, this function will search in all USB
1958 devices of the platform. If ParentDevicePath is not NULL,this function will only
1959 search in its child devices.
1961 @param ParentDevicePath The device path of the parent.
1962 @param ShortFormDevicePath The USB Class or USB WWID device path to match.
1964 @return The image Handle if find load file from specified short-form device path
1965 or NULL if not found.
1970 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
1971 IN EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
1975 UINTN UsbIoHandleCount
;
1976 EFI_HANDLE
*UsbIoHandleBuffer
;
1977 EFI_DEVICE_PATH_PROTOCOL
*UsbIoDevicePath
;
1978 EFI_USB_IO_PROTOCOL
*UsbIo
;
1982 EFI_HANDLE ImageHandle
;
1984 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
1985 EFI_DEVICE_PATH_PROTOCOL
*NextDevicePath
;
1987 FullDevicePath
= NULL
;
1991 // Get all UsbIo Handles.
1993 UsbIoHandleCount
= 0;
1994 UsbIoHandleBuffer
= NULL
;
1995 Status
= gBS
->LocateHandleBuffer (
1997 &gEfiUsbIoProtocolGuid
,
2002 if (EFI_ERROR (Status
) || (UsbIoHandleCount
== 0) || (UsbIoHandleBuffer
== NULL
)) {
2006 ParentSize
= (ParentDevicePath
== NULL
) ? 0 : GetDevicePathSize (ParentDevicePath
);
2007 for (Index
= 0; Index
< UsbIoHandleCount
; Index
++) {
2009 // Get the Usb IO interface.
2011 Status
= gBS
->HandleProtocol(
2012 UsbIoHandleBuffer
[Index
],
2013 &gEfiUsbIoProtocolGuid
,
2016 if (EFI_ERROR (Status
)) {
2020 UsbIoDevicePath
= DevicePathFromHandle (UsbIoHandleBuffer
[Index
]);
2021 if (UsbIoDevicePath
== NULL
) {
2025 if (ParentDevicePath
!= NULL
) {
2027 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
2029 Size
= GetDevicePathSize (UsbIoDevicePath
);
2030 if ((Size
< ParentSize
) ||
2031 (CompareMem (UsbIoDevicePath
, ParentDevicePath
, ParentSize
- END_DEVICE_PATH_LENGTH
) != 0)) {
2036 if (BdsMatchUsbClass (UsbIo
, (USB_CLASS_DEVICE_PATH
*) ShortFormDevicePath
) ||
2037 BdsMatchUsbWwid (UsbIo
, (USB_WWID_DEVICE_PATH
*) ShortFormDevicePath
)) {
2039 // Try to find if there is the boot file in this DevicePath
2041 NextDevicePath
= NextDevicePathNode (ShortFormDevicePath
);
2042 if (!IsDevicePathEnd (NextDevicePath
)) {
2043 FullDevicePath
= AppendDevicePath (UsbIoDevicePath
, NextDevicePath
);
2045 // Connect the full device path, so that Simple File System protocol
2046 // could be installed for this USB device.
2048 BdsLibConnectDevicePath (FullDevicePath
);
2049 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2050 Status
= gBS
->LoadImage (
2058 FreePool (FullDevicePath
);
2060 FullDevicePath
= UsbIoDevicePath
;
2061 Status
= EFI_NOT_FOUND
;
2065 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2066 // and load the image according to the default boot behavior for removable device.
2068 if (EFI_ERROR (Status
)) {
2070 // check if there is a bootable removable media could be found in this device path ,
2071 // and get the bootable media handle
2073 Handle
= BdsLibGetBootableHandle(UsbIoDevicePath
);
2074 if (Handle
== NULL
) {
2078 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2079 // machinename is ia32, ia64, x64, ...
2081 FullDevicePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2082 if (FullDevicePath
!= NULL
) {
2083 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2084 Status
= gBS
->LoadImage (
2092 if (EFI_ERROR (Status
)) {
2094 // The DevicePath failed, and it's not a valid
2095 // removable media device.
2107 FreePool (UsbIoHandleBuffer
);
2112 Expand USB Class or USB WWID device path node to be full device path of a USB
2113 device in platform then load the boot file on this full device path and return the
2116 This function support following 4 cases:
2117 1) Boot Option device path starts with a USB Class or USB WWID device path,
2118 and there is no Media FilePath device path in the end.
2119 In this case, it will follow Removable Media Boot Behavior.
2120 2) Boot Option device path starts with a USB Class or USB WWID device path,
2121 and ended with Media FilePath device path.
2122 3) Boot Option device path starts with a full device path to a USB Host Controller,
2123 contains a USB Class or USB WWID device path node, while not ended with Media
2124 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
2125 4) Boot Option device path starts with a full device path to a USB Host Controller,
2126 contains a USB Class or USB WWID device path node, and ended with Media
2127 FilePath device path.
2129 @param DevicePath The Boot Option device path.
2131 @return The image handle of boot file, or NULL if there is no boot file found in
2132 the specified USB Class or USB WWID device path.
2136 BdsExpandUsbShortFormDevicePath (
2137 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
2140 EFI_HANDLE
*ImageHandle
;
2141 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2142 EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
;
2145 // Search for USB Class or USB WWID device path node.
2147 ShortFormDevicePath
= NULL
;
2149 TempDevicePath
= DevicePath
;
2150 while (!IsDevicePathEnd (TempDevicePath
)) {
2151 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
2152 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
2153 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
2154 ShortFormDevicePath
= TempDevicePath
;
2157 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2160 if (ShortFormDevicePath
== NULL
) {
2162 // No USB Class or USB WWID device path node found, do nothing.
2167 if (ShortFormDevicePath
== DevicePath
) {
2169 // Boot Option device path starts with USB Class or USB WWID device path.
2171 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2172 if (ImageHandle
== NULL
) {
2174 // Failed to find a match in existing devices, connect the short form USB
2175 // device path and try again.
2177 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath
);
2178 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2182 // Boot Option device path contains USB Class or USB WWID device path node.
2186 // Prepare the parent device path for search.
2188 TempDevicePath
= DuplicateDevicePath (DevicePath
);
2189 ASSERT (TempDevicePath
!= NULL
);
2190 SetDevicePathEndNode (((UINT8
*) TempDevicePath
) + ((UINTN
) ShortFormDevicePath
- (UINTN
) DevicePath
));
2193 // The USB Host Controller device path is already in Boot Option device path
2194 // and USB Bus driver already support RemainingDevicePath starts with USB
2195 // Class or USB WWID device path, so just search in existing USB devices and
2196 // doesn't perform ConnectController here.
2198 ImageHandle
= BdsFindUsbDevice (TempDevicePath
, ShortFormDevicePath
);
2199 FreePool (TempDevicePath
);
2206 Process the boot option follow the UEFI specification and
2207 special treat the legacy boot option with BBS_DEVICE_PATH.
2209 @param Option The boot option need to be processed
2210 @param DevicePath The device path which describe where to load the
2211 boot image or the legacy BBS device path to boot
2213 @param ExitDataSize The size of exit data.
2214 @param ExitData Data returned when Boot image failed.
2216 @retval EFI_SUCCESS Boot from the input boot option successfully.
2217 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2222 BdsLibBootViaBootOption (
2223 IN BDS_COMMON_OPTION
*Option
,
2224 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2225 OUT UINTN
*ExitDataSize
,
2226 OUT CHAR16
**ExitData OPTIONAL
2230 EFI_STATUS StatusLogo
;
2232 EFI_HANDLE ImageHandle
;
2233 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
2234 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
2235 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
2236 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
2237 LIST_ENTRY TempBootLists
;
2238 EFI_BOOT_LOGO_PROTOCOL
*BootLogo
;
2244 // Notes: this code can be remove after the s3 script table
2245 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
2246 // EVT_SIGNAL_LEGACY_BOOT
2248 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
2249 if (!EFI_ERROR (Status
)) {
2250 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
2253 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
2256 WorkingDevicePath
= NULL
;
2257 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2258 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
2259 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
2260 (HARDDRIVE_DEVICE_PATH
*)DevicePath
2262 if (WorkingDevicePath
!= NULL
) {
2263 DevicePath
= WorkingDevicePath
;
2270 if (IsBootOptionValidNVVarialbe (Option
)) {
2272 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
2273 // In this case, "BootCurrent" is not created.
2274 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
2276 SetVariableAndReportStatusCodeOnError (
2278 &gEfiGlobalVariableGuid
,
2279 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2281 &Option
->BootCurrent
2286 // Report Status Code to indicate ReadyToBoot event will be signalled
2288 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT
));
2291 // Signal the EVT_SIGNAL_READY_TO_BOOT event
2293 EfiSignalEventReadyToBoot();
2296 // Expand USB Class or USB WWID device path node to be full device path of a USB
2297 // device in platform then load the boot file on this full device path and get the
2300 ImageHandle
= BdsExpandUsbShortFormDevicePath (DevicePath
);
2303 // Adjust the different type memory page number just before booting
2304 // and save the updated info into the variable for next boot to use
2306 BdsSetMemoryTypeInformationVariable ();
2309 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.
2310 // Here get the ImageHandle for the non USB class or WWID device path.
2312 if (ImageHandle
== NULL
) {
2313 ASSERT (Option
->DevicePath
!= NULL
);
2314 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
2315 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
2318 // Check to see if we should legacy BOOT. If yes then do the legacy boot
2320 return BdsLibDoLegacyBoot (Option
);
2324 // If the boot option point to Internal FV shell, make sure it is valid
2326 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
2327 if (!EFI_ERROR(Status
)) {
2328 if (Option
->DevicePath
!= NULL
) {
2329 FreePool(Option
->DevicePath
);
2331 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
2332 ASSERT(Option
->DevicePath
!= NULL
);
2333 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
2335 // Update the shell boot option
2337 InitializeListHead (&TempBootLists
);
2338 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
2341 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
2343 FreePool (DevicePath
);
2344 DevicePath
= Option
->DevicePath
;
2349 if (Option
->Description
== NULL
) {
2350 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
2352 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
2358 // Report status code for OS Loader LoadImage.
2360 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2361 Status
= gBS
->LoadImage (
2371 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2372 // and load the image according to the default boot behavior for removable device.
2374 if (EFI_ERROR (Status
)) {
2376 // check if there is a bootable removable media could be found in this device path ,
2377 // and get the bootable media handle
2379 Handle
= BdsLibGetBootableHandle(DevicePath
);
2380 if (Handle
!= NULL
) {
2382 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2383 // machinename is ia32, ia64, x64, ...
2385 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2386 if (FilePath
!= NULL
) {
2387 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2388 Status
= gBS
->LoadImage (
2401 // Provide the image with it's load options
2403 if ((ImageHandle
== NULL
) || (EFI_ERROR(Status
))) {
2405 // Report Status Code to indicate that the failure to load boot option
2407 REPORT_STATUS_CODE (
2408 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2409 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR
)
2414 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
2415 ASSERT_EFI_ERROR (Status
);
2417 if (Option
->LoadOptionsSize
!= 0) {
2418 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
2419 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
2423 // Clean to NULL because the image is loaded directly from the firmwares boot manager.
2425 ImageInfo
->ParentHandle
= NULL
;
2428 // Before calling the image, enable the Watchdog Timer for
2429 // the 5 Minute period
2431 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
2434 // Write boot to OS performance data for UEFI boot
2437 WriteBootToOsPerformanceData (NULL
, NULL
);
2441 // Report status code for OS Loader StartImage.
2443 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderStart
));
2445 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
2446 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
2447 if (EFI_ERROR (Status
)) {
2449 // Report Status Code to indicate that boot failure
2451 REPORT_STATUS_CODE (
2452 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2453 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED
)
2458 // Clear the Watchdog Timer after the image returns
2460 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
2464 // Set Logo status invalid after trying one boot option
2467 StatusLogo
= gBS
->LocateProtocol (&gEfiBootLogoProtocolGuid
, NULL
, (VOID
**) &BootLogo
);
2468 if (!EFI_ERROR (StatusLogo
) && (BootLogo
!= NULL
)) {
2469 BootLogo
->SetBootLogo (BootLogo
, NULL
, 0, 0, 0, 0);
2473 // Clear Boot Current
2474 // Deleting variable with current implementation shouldn't fail.
2478 &gEfiGlobalVariableGuid
,
2479 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2489 Expand a device path that starts with a hard drive media device path node to be a
2490 full device path that includes the full hardware path to the device. We need
2491 to do this so it can be booted. As an optimization the front match (the part point
2492 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
2493 so a connect all is not required on every boot. All successful history device path
2494 which point to partition node (the front part) will be saved.
2496 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
2497 drive media device path.
2498 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
2502 EFI_DEVICE_PATH_PROTOCOL
*
2504 BdsExpandPartitionPartialDevicePathToFull (
2505 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2509 UINTN BlockIoHandleCount
;
2510 EFI_HANDLE
*BlockIoBuffer
;
2511 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
2512 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
2513 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2516 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
2517 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
2518 UINTN CachedDevicePathSize
;
2519 BOOLEAN DeviceExist
;
2521 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
2524 FullDevicePath
= NULL
;
2526 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2527 // If exist, search the front path which point to partition node in the variable instants.
2528 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system
2531 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2532 &gHdBootDevicePathVariablGuid
,
2533 (VOID
**) &CachedDevicePath
,
2534 &CachedDevicePathSize
2538 // Delete the invalid HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2540 if ((CachedDevicePath
!= NULL
) && !IsDevicePathValid (CachedDevicePath
, CachedDevicePathSize
)) {
2541 FreePool (CachedDevicePath
);
2542 CachedDevicePath
= NULL
;
2543 Status
= gRT
->SetVariable (
2544 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2545 &gHdBootDevicePathVariablGuid
,
2550 ASSERT_EFI_ERROR (Status
);
2553 if (CachedDevicePath
!= NULL
) {
2554 TempNewDevicePath
= CachedDevicePath
;
2555 DeviceExist
= FALSE
;
2559 // Check every instance of the variable
2560 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
2561 // partial partition boot option. Second, check whether the instance could be connected.
2563 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
2564 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
2566 // Connect the device path instance, the device path point to hard drive media device path node
2567 // e.g. ACPI() /PCI()/ATA()/Partition()
2569 Status
= BdsLibConnectDevicePath (Instance
);
2570 if (!EFI_ERROR (Status
)) {
2576 // Come here means the first instance is not matched
2580 } while (TempNewDevicePath
!= NULL
);
2584 // Find the matched device path.
2585 // Append the file path information from the boot option and return the fully expanded device path.
2587 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2588 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
2591 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.
2595 // First delete the matched instance.
2597 TempNewDevicePath
= CachedDevicePath
;
2598 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
2599 FreePool (TempNewDevicePath
);
2602 // Second, append the remaining path after the matched instance
2604 TempNewDevicePath
= CachedDevicePath
;
2605 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
2606 FreePool (TempNewDevicePath
);
2608 // Save the matching Device Path so we don't need to do a connect all next time
2609 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2611 Status
= gRT
->SetVariable (
2612 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2613 &gHdBootDevicePathVariablGuid
,
2614 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2615 GetDevicePathSize (CachedDevicePath
),
2620 FreePool (Instance
);
2621 FreePool (CachedDevicePath
);
2622 return FullDevicePath
;
2627 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need
2628 // to search all devices in the system for a matched partition
2630 BdsLibConnectAllDriversToAllControllers ();
2631 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
2632 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
2634 // If there was an error or there are no device handles that support
2635 // the BLOCK_IO Protocol, then return.
2640 // Loop through all the device handles that support the BLOCK_IO Protocol
2642 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
2644 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
2645 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
2649 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
2651 // Find the matched partition device path
2653 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2654 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
2657 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2659 if (CachedDevicePath
!= NULL
) {
2661 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2663 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
2664 TempNewDevicePath
= CachedDevicePath
;
2665 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
2666 FreePool(TempNewDevicePath
);
2669 if (CachedDevicePath
!= NULL
) {
2670 TempNewDevicePath
= CachedDevicePath
;
2671 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
2672 FreePool(TempNewDevicePath
);
2674 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2678 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
2679 // If the user try to boot many OS in different HDs or partitions, in theory,
2680 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.
2683 ASSERT (CachedDevicePath
!= NULL
);
2684 TempNewDevicePath
= CachedDevicePath
;
2685 while (!IsDevicePathEnd (TempNewDevicePath
)) {
2686 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2688 // Parse one instance
2690 while (!IsDevicePathEndType (TempNewDevicePath
)) {
2691 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2695 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
2697 if (InstanceNum
>= 12) {
2698 SetDevicePathEndNode (TempNewDevicePath
);
2703 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2707 // Save the matching Device Path so we don't need to do a connect all next time
2708 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2710 Status
= gRT
->SetVariable (
2711 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2712 &gHdBootDevicePathVariablGuid
,
2713 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2714 GetDevicePathSize (CachedDevicePath
),
2722 if (CachedDevicePath
!= NULL
) {
2723 FreePool (CachedDevicePath
);
2725 if (BlockIoBuffer
!= NULL
) {
2726 FreePool (BlockIoBuffer
);
2728 return FullDevicePath
;
2732 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
2733 instances, has the same partition node with HardDriveDevicePath device path
2735 @param BlockIoDevicePath Multi device path instances which need to check
2736 @param HardDriveDevicePath A device path which starts with a hard drive media
2739 @retval TRUE There is a matched device path instance.
2740 @retval FALSE There is no matched device path instance.
2745 MatchPartitionDevicePathNode (
2746 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
2747 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2750 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
2751 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2753 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
2755 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
2760 // Make PreviousDevicePath == the device path node before the end node
2762 DevicePath
= BlockIoDevicePath
;
2763 BlockIoHdDevicePathNode
= NULL
;
2766 // find the partition device path node
2768 while (!IsDevicePathEnd (DevicePath
)) {
2769 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2770 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
2772 BlockIoHdDevicePathNode
= DevicePath
;
2776 DevicePath
= NextDevicePathNode (DevicePath
);
2779 if (BlockIoHdDevicePathNode
== NULL
) {
2783 // See if the harddrive device path in blockio matches the orig Hard Drive Node
2785 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
2789 // Check for the match
2791 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
2792 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
2793 switch (TmpHdPath
->SignatureType
) {
2794 case SIGNATURE_TYPE_GUID
:
2795 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
2797 case SIGNATURE_TYPE_MBR
:
2798 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
2810 Delete the boot option associated with the handle passed in.
2812 @param Handle The handle which present the device path to create
2815 @retval EFI_SUCCESS Delete the boot option success
2816 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2817 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2818 @retval Other Error return value from SetVariable()
2822 BdsLibDeleteOptionFromHandle (
2823 IN EFI_HANDLE Handle
2827 UINT8
*BootOptionVar
;
2828 UINTN BootOrderSize
;
2829 UINTN BootOptionSize
;
2832 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2833 UINTN DevicePathSize
;
2834 UINTN OptionDevicePathSize
;
2835 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2836 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2839 Status
= EFI_SUCCESS
;
2844 // Check "BootOrder" variable, if no, means there is no any boot order.
2846 BootOrder
= BdsLibGetVariableAndSize (
2848 &gEfiGlobalVariableGuid
,
2851 if (BootOrder
== NULL
) {
2852 return EFI_NOT_FOUND
;
2856 // Convert device handle to device path protocol instance
2858 DevicePath
= DevicePathFromHandle (Handle
);
2859 if (DevicePath
== NULL
) {
2860 return EFI_NOT_FOUND
;
2862 DevicePathSize
= GetDevicePathSize (DevicePath
);
2865 // Loop all boot order variable and find the matching device path
2868 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2869 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2870 BootOptionVar
= BdsLibGetVariableAndSize (
2872 &gEfiGlobalVariableGuid
,
2876 if (BootOptionVar
== NULL
) {
2877 FreePool (BootOrder
);
2878 return EFI_OUT_OF_RESOURCES
;
2881 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2882 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2883 FreePool (BootOptionVar
);
2888 TempPtr
= BootOptionVar
;
2889 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
2890 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
2891 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
2892 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
2895 // Check whether the device path match
2897 if ((OptionDevicePathSize
== DevicePathSize
) &&
2898 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
2899 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2900 FreePool (BootOptionVar
);
2904 FreePool (BootOptionVar
);
2909 // Adjust number of boot option for "BootOrder" variable.
2911 Status
= gRT
->SetVariable (
2913 &gEfiGlobalVariableGuid
,
2914 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2919 // Shrinking variable with existing variable implementation shouldn't fail.
2921 ASSERT_EFI_ERROR (Status
);
2923 FreePool (BootOrder
);
2930 Delete all invalid EFI boot options.
2932 @retval EFI_SUCCESS Delete all invalid boot option success
2933 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
2934 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2935 @retval Other Error return value from SetVariable()
2939 BdsDeleteAllInvalidEfiBootOption (
2944 UINT8
*BootOptionVar
;
2945 UINTN BootOrderSize
;
2946 UINTN BootOptionSize
;
2950 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2951 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2953 CHAR16
*Description
;
2956 Status
= EFI_SUCCESS
;
2959 OptionDevicePath
= NULL
;
2964 // Check "BootOrder" variable firstly, this variable hold the number of boot options
2966 BootOrder
= BdsLibGetVariableAndSize (
2968 &gEfiGlobalVariableGuid
,
2971 if (NULL
== BootOrder
) {
2972 return EFI_NOT_FOUND
;
2976 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2977 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2978 BootOptionVar
= BdsLibGetVariableAndSize (
2980 &gEfiGlobalVariableGuid
,
2983 if (NULL
== BootOptionVar
) {
2984 FreePool (BootOrder
);
2985 return EFI_OUT_OF_RESOURCES
;
2988 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2991 TempPtr
= BootOptionVar
;
2992 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
2993 Description
= (CHAR16
*) TempPtr
;
2994 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
2995 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
2998 // Skip legacy boot option (BBS boot device)
3000 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
3001 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
3002 FreePool (BootOptionVar
);
3008 if (Corrupted
|| !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
3010 // Delete this invalid boot option "Boot####"
3012 Status
= gRT
->SetVariable (
3014 &gEfiGlobalVariableGuid
,
3015 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3020 // Deleting variable with current variable implementation shouldn't fail.
3022 ASSERT_EFI_ERROR (Status
);
3024 // Mark this boot option in boot order as deleted
3026 BootOrder
[Index
] = 0xffff;
3030 FreePool (BootOptionVar
);
3035 // Adjust boot order array
3038 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
3039 if (BootOrder
[Index
] != 0xffff) {
3040 BootOrder
[Index2
] = BootOrder
[Index
];
3044 Status
= gRT
->SetVariable (
3046 &gEfiGlobalVariableGuid
,
3047 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3048 Index2
* sizeof (UINT16
),
3052 // Shrinking variable with current variable implementation shouldn't fail.
3054 ASSERT_EFI_ERROR (Status
);
3056 FreePool (BootOrder
);
3063 For EFI boot option, BDS separate them as six types:
3064 1. Network - The boot option points to the SimpleNetworkProtocol device.
3065 Bds will try to automatically create this type boot option when enumerate.
3066 2. Shell - The boot option points to internal flash shell.
3067 Bds will try to automatically create this type boot option when enumerate.
3068 3. Removable BlockIo - The boot option only points to the removable media
3069 device, like USB flash disk, DVD, Floppy etc.
3070 These device should contain a *removable* blockIo
3071 protocol in their device handle.
3072 Bds will try to automatically create this type boot option
3074 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
3076 These device should contain a *fixed* blockIo
3077 protocol in their device handle.
3078 BDS will skip fixed blockIo devices, and NOT
3079 automatically create boot option for them. But BDS
3080 will help to delete those fixed blockIo boot option,
3081 whose description rule conflict with other auto-created
3083 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
3084 has SimpleFileSystem Protocol, but has no blockio
3085 protocol. These devices do not offer blockIo
3086 protocol, but BDS still can get the
3087 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
3089 6. File - The boot option points to a file. These boot options are usually
3090 created by user manually or OS loader. BDS will not delete or modify
3093 This function will enumerate all possible boot device in the system, and
3094 automatically create boot options for Network, Shell, Removable BlockIo,
3095 and Non-BlockIo Simplefile devices.
3096 It will only execute once of every boot.
3098 @param BdsBootOptionList The header of the link list which indexed all
3099 current boot options
3101 @retval EFI_SUCCESS Finished all the boot device enumerate and create
3102 the boot option base on that boot device
3104 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
3108 BdsLibEnumerateAllBootOption (
3109 IN OUT LIST_ENTRY
*BdsBootOptionList
3113 UINT16 FloppyNumber
;
3114 UINT16 HarddriveNumber
;
3119 UINT16 NonBlockNumber
;
3120 UINTN NumberBlockIoHandles
;
3121 EFI_HANDLE
*BlockIoHandles
;
3122 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
3123 BOOLEAN Removable
[2];
3124 UINTN RemovableIndex
;
3126 UINTN NumOfLoadFileHandles
;
3127 EFI_HANDLE
*LoadFileHandles
;
3128 UINTN FvHandleCount
;
3129 EFI_HANDLE
*FvHandleBuffer
;
3130 EFI_FV_FILETYPE Type
;
3132 EFI_FV_FILE_ATTRIBUTES Attributes
;
3133 UINT32 AuthenticationStatus
;
3134 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
3135 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3136 UINTN DevicePathType
;
3138 EFI_HANDLE
*FileSystemHandles
;
3139 UINTN NumberFileSystemHandles
;
3141 EFI_IMAGE_DOS_HEADER DosHeader
;
3144 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3145 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3148 HarddriveNumber
= 0;
3155 ZeroMem (Buffer
, sizeof (Buffer
));
3158 // If the boot device enumerate happened, just get the boot
3159 // device from the boot order variable
3161 if (mEnumBootDevice
) {
3162 GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME
, &gLastEnumLangGuid
, (VOID
**)&LastLang
, NULL
);
3163 GetEfiGlobalVariable2 (L
"PlatformLang", (VOID
**)&PlatLang
, NULL
);
3164 ASSERT (PlatLang
!= NULL
);
3165 if ((LastLang
!= NULL
) && (AsciiStrCmp (LastLang
, PlatLang
) == 0)) {
3166 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3167 FreePool (LastLang
);
3168 FreePool (PlatLang
);
3171 Status
= gRT
->SetVariable (
3172 LAST_ENUM_LANGUAGE_VARIABLE_NAME
,
3174 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3175 AsciiStrSize (PlatLang
),
3179 // Failure to set the variable only impacts the performance next time enumerating the boot options.
3182 if (LastLang
!= NULL
) {
3183 FreePool (LastLang
);
3185 FreePool (PlatLang
);
3190 // Notes: this dirty code is to get the legacy boot option from the
3191 // BBS table and create to variable as the EFI boot option, it should
3192 // be removed after the CSM can provide legacy boot option directly
3194 REFRESH_LEGACY_BOOT_OPTIONS
;
3197 // Delete invalid boot option
3199 BdsDeleteAllInvalidEfiBootOption ();
3202 // Parse removable media followed by fixed media.
3203 // The Removable[] array is used by the for-loop below to create removable media boot options
3204 // at first, and then to create fixed media boot options.
3206 Removable
[0] = FALSE
;
3207 Removable
[1] = TRUE
;
3209 gBS
->LocateHandleBuffer (
3211 &gEfiBlockIoProtocolGuid
,
3213 &NumberBlockIoHandles
,
3217 for (RemovableIndex
= 0; RemovableIndex
< 2; RemovableIndex
++) {
3218 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
3219 Status
= gBS
->HandleProtocol (
3220 BlockIoHandles
[Index
],
3221 &gEfiBlockIoProtocolGuid
,
3225 // skip the logical partition
3227 if (EFI_ERROR (Status
) || BlkIo
->Media
->LogicalPartition
) {
3232 // firstly fixed block io then the removable block io
3234 if (BlkIo
->Media
->RemovableMedia
== Removable
[RemovableIndex
]) {
3237 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
3238 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
3240 switch (DevicePathType
) {
3241 case BDS_EFI_ACPI_FLOPPY_BOOT
:
3242 if (FloppyNumber
!= 0) {
3243 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
3245 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
3247 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3252 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.
3254 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
3255 case BDS_EFI_MESSAGE_SATA_BOOT
:
3256 if (BlkIo
->Media
->RemovableMedia
) {
3257 if (CdromNumber
!= 0) {
3258 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
3260 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
3264 if (HarddriveNumber
!= 0) {
3265 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)), HarddriveNumber
);
3267 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)));
3271 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
3272 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3275 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
3276 if (UsbNumber
!= 0) {
3277 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
3279 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
3281 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3285 case BDS_EFI_MESSAGE_SCSI_BOOT
:
3286 if (ScsiNumber
!= 0) {
3287 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
3289 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
3291 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3295 case BDS_EFI_MESSAGE_MISC_BOOT
:
3297 if (MiscNumber
!= 0) {
3298 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
3300 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
3302 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3309 if (NumberBlockIoHandles
!= 0) {
3310 FreePool (BlockIoHandles
);
3314 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
3317 gBS
->LocateHandleBuffer (
3319 &gEfiSimpleFileSystemProtocolGuid
,
3321 &NumberFileSystemHandles
,
3324 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
3325 Status
= gBS
->HandleProtocol (
3326 FileSystemHandles
[Index
],
3327 &gEfiBlockIoProtocolGuid
,
3330 if (!EFI_ERROR (Status
)) {
3332 // Skip if the file system handle supports a BlkIo protocol,
3338 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
3339 // machinename is ia32, ia64, x64, ...
3341 Hdr
.Union
= &HdrData
;
3343 Status
= BdsLibGetImageHeader (
3344 FileSystemHandles
[Index
],
3345 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3349 if (!EFI_ERROR (Status
) &&
3350 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3351 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3357 // No such file or the file is not a EFI application, delete this boot option
3359 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
3361 if (NonBlockNumber
!= 0) {
3362 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
3364 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
3366 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
3371 if (NumberFileSystemHandles
!= 0) {
3372 FreePool (FileSystemHandles
);
3376 // Parse Network Boot Device
3378 NumOfLoadFileHandles
= 0;
3380 // Search Load File protocol for PXE boot option.
3382 gBS
->LocateHandleBuffer (
3384 &gEfiLoadFileProtocolGuid
,
3386 &NumOfLoadFileHandles
,
3390 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
3392 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)), Index
);
3394 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)));
3396 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
3399 if (NumOfLoadFileHandles
!= 0) {
3400 FreePool (LoadFileHandles
);
3404 // Check if we have on flash shell
3406 gBS
->LocateHandleBuffer (
3408 &gEfiFirmwareVolume2ProtocolGuid
,
3413 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
3414 gBS
->HandleProtocol (
3415 FvHandleBuffer
[Index
],
3416 &gEfiFirmwareVolume2ProtocolGuid
,
3420 Status
= Fv
->ReadFile (
3422 PcdGetPtr(PcdShellFile
),
3427 &AuthenticationStatus
3429 if (EFI_ERROR (Status
)) {
3431 // Skip if no shell file in the FV
3436 // Build the shell boot option
3438 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
3441 if (FvHandleCount
!= 0) {
3442 FreePool (FvHandleBuffer
);
3445 // Make sure every boot only have one time
3446 // boot device enumerate
3448 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3449 mEnumBootDevice
= TRUE
;
3455 Build the boot option with the handle parsed in
3457 @param Handle The handle which present the device path to create
3459 @param BdsBootOptionList The header of the link list which indexed all
3460 current boot options
3461 @param String The description of the boot option.
3466 BdsLibBuildOptionFromHandle (
3467 IN EFI_HANDLE Handle
,
3468 IN LIST_ENTRY
*BdsBootOptionList
,
3472 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3474 DevicePath
= DevicePathFromHandle (Handle
);
3477 // Create and register new boot option
3479 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
3484 Build the on flash shell boot option with the handle parsed in.
3486 @param Handle The handle which present the device path to create
3487 on flash shell boot option
3488 @param BdsBootOptionList The header of the link list which indexed all
3489 current boot options
3494 BdsLibBuildOptionFromShell (
3495 IN EFI_HANDLE Handle
,
3496 IN OUT LIST_ENTRY
*BdsBootOptionList
3499 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3500 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
3502 DevicePath
= DevicePathFromHandle (Handle
);
3505 // Build the shell device path
3507 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
3509 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
3512 // Create and register the shell boot option
3514 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
3519 Boot from the UEFI spec defined "BootNext" variable.
3532 BDS_COMMON_OPTION
*BootOption
;
3533 LIST_ENTRY TempList
;
3538 // Init the boot option name buffer and temp link list
3540 InitializeListHead (&TempList
);
3541 ZeroMem (Buffer
, sizeof (Buffer
));
3543 BootNext
= BdsLibGetVariableAndSize (
3545 &gEfiGlobalVariableGuid
,
3550 // Clear the boot next variable first
3552 if (BootNext
!= NULL
) {
3553 Status
= gRT
->SetVariable (
3555 &gEfiGlobalVariableGuid
,
3556 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3561 // Deleting variable with current variable implementation shouldn't fail.
3563 ASSERT_EFI_ERROR (Status
);
3566 // Start to build the boot option and try to boot
3568 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
3569 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
3570 ASSERT (BootOption
!= NULL
);
3571 BdsLibConnectDevicePath (BootOption
->DevicePath
);
3572 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
3573 FreePool(BootOption
);
3580 Return the bootable media handle.
3581 First, check the device is connected
3582 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
3583 Third, detect the the default boot file in the Media, and return the removable Media handle.
3585 @param DevicePath Device Path to a bootable device
3587 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
3592 BdsLibGetBootableHandle (
3593 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3598 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3599 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
3601 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
3603 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3606 EFI_HANDLE ReturnHandle
;
3607 EFI_HANDLE
*SimpleFileSystemHandles
;
3609 UINTN NumberSimpleFileSystemHandles
;
3611 EFI_IMAGE_DOS_HEADER DosHeader
;
3612 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3613 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3615 UpdatedDevicePath
= DevicePath
;
3618 // Enter to critical section to protect the acquired BlockIo instance
3619 // from getting released due to the USB mass storage hotplug event
3621 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
3624 // Check whether the device is connected
3626 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3627 if (EFI_ERROR (Status
)) {
3629 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
3631 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3632 if (EFI_ERROR (Status
)) {
3634 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
3636 UpdatedDevicePath
= DevicePath
;
3637 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3638 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3642 // For removable device boot option, its contained device path only point to the removable device handle,
3643 // should make sure all its children handles (its child partion or media handles) are created and connected.
3645 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3647 // Get BlockIo protocol and check removable attribute
3649 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
3650 ASSERT_EFI_ERROR (Status
);
3653 // Issue a dummy read to the device to check for media change.
3654 // When the removable media is changed, any Block IO read/write will
3655 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
3656 // returned. After the Block IO protocol is reinstalled, subsequent
3657 // Block IO read/write will success.
3659 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
3660 if (Buffer
!= NULL
) {
3661 BlockIo
->ReadBlocks (
3663 BlockIo
->Media
->MediaId
,
3665 BlockIo
->Media
->BlockSize
,
3673 // Detect the the default boot file from removable Media
3677 // 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
3678 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
3680 DupDevicePath
= DuplicateDevicePath (DevicePath
);
3681 ASSERT (DupDevicePath
!= NULL
);
3683 UpdatedDevicePath
= DupDevicePath
;
3684 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3686 // 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
3687 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
3689 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
3690 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
3692 // Remove the usb node, let the device path only point to PCI node
3694 SetDevicePathEndNode (UpdatedDevicePath
);
3695 UpdatedDevicePath
= DupDevicePath
;
3697 UpdatedDevicePath
= DevicePath
;
3701 // Get the device path size of boot option
3703 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3704 ReturnHandle
= NULL
;
3705 gBS
->LocateHandleBuffer (
3707 &gEfiSimpleFileSystemProtocolGuid
,
3709 &NumberSimpleFileSystemHandles
,
3710 &SimpleFileSystemHandles
3712 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
3714 // Get the device path size of SimpleFileSystem handle
3716 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
3717 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3719 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
3721 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
3723 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
3724 // machinename is ia32, ia64, x64, ...
3726 Hdr
.Union
= &HdrData
;
3727 Status
= BdsLibGetImageHeader (
3728 SimpleFileSystemHandles
[Index
],
3729 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3733 if (!EFI_ERROR (Status
) &&
3734 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3735 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3736 ReturnHandle
= SimpleFileSystemHandles
[Index
];
3742 FreePool(DupDevicePath
);
3744 if (SimpleFileSystemHandles
!= NULL
) {
3745 FreePool(SimpleFileSystemHandles
);
3748 gBS
->RestoreTPL (OldTpl
);
3750 return ReturnHandle
;
3754 Check to see if the network cable is plugged in. If the DevicePath is not
3755 connected it will be connected.
3757 @param DevicePath Device Path to check
3759 @retval TRUE DevicePath points to an Network that is connected
3760 @retval FALSE DevicePath does not point to a bootable network
3764 BdsLibNetworkBootWithMediaPresent (
3765 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3769 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3771 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
3772 BOOLEAN MediaPresent
;
3773 UINT32 InterruptStatus
;
3775 MediaPresent
= FALSE
;
3777 UpdatedDevicePath
= DevicePath
;
3779 // Locate Load File Protocol for PXE boot option first
3781 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3782 if (EFI_ERROR (Status
)) {
3784 // Device not present so see if we need to connect it
3786 Status
= BdsLibConnectDevicePath (DevicePath
);
3787 if (!EFI_ERROR (Status
)) {
3789 // This one should work after we did the connect
3791 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3795 if (!EFI_ERROR (Status
)) {
3796 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
3797 if (EFI_ERROR (Status
)) {
3799 // Failed to open SNP from this handle, try to get SNP from parent handle
3801 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
3802 if (UpdatedDevicePath
!= NULL
) {
3803 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3804 if (!EFI_ERROR (Status
)) {
3806 // SNP handle found, get SNP from it
3808 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
3813 if (!EFI_ERROR (Status
)) {
3814 if (Snp
->Mode
->MediaPresentSupported
) {
3815 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
3817 // Invoke Snp->GetStatus() to refresh the media status
3819 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
3822 // In case some one else is using the SNP check to see if it's connected
3824 MediaPresent
= Snp
->Mode
->MediaPresent
;
3827 // No one is using SNP so we need to Start and Initialize so
3828 // MediaPresent will be valid.
3830 Status
= Snp
->Start (Snp
);
3831 if (!EFI_ERROR (Status
)) {
3832 Status
= Snp
->Initialize (Snp
, 0, 0);
3833 if (!EFI_ERROR (Status
)) {
3834 MediaPresent
= Snp
->Mode
->MediaPresent
;
3835 Snp
->Shutdown (Snp
);
3841 MediaPresent
= TRUE
;
3846 return MediaPresent
;
3850 For a bootable Device path, return its boot type.
3852 @param DevicePath The bootable device Path to check
3854 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3855 which subtype is MEDIA_HARDDRIVE_DP
3856 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3857 which subtype is MEDIA_CDROM_DP
3858 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
3859 which HID is floppy device.
3860 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3861 and its last device path node's subtype is MSG_ATAPI_DP.
3862 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3863 and its last device path node's subtype is MSG_SCSI_DP.
3864 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3865 and its last device path node's subtype is MSG_USB_DP.
3866 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
3867 its last device path node point to a message device path node.
3868 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
3869 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
3874 BdsGetBootTypeFromDevicePath (
3875 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3878 ACPI_HID_DEVICE_PATH
*Acpi
;
3879 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3880 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
3883 if (NULL
== DevicePath
) {
3884 return BDS_EFI_UNSUPPORT
;
3887 TempDevicePath
= DevicePath
;
3889 while (!IsDevicePathEndType (TempDevicePath
)) {
3890 switch (DevicePathType (TempDevicePath
)) {
3891 case BBS_DEVICE_PATH
:
3892 return BDS_LEGACY_BBS_BOOT
;
3893 case MEDIA_DEVICE_PATH
:
3894 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
3895 return BDS_EFI_MEDIA_HD_BOOT
;
3896 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
3897 return BDS_EFI_MEDIA_CDROM_BOOT
;
3900 case ACPI_DEVICE_PATH
:
3901 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
3902 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
3903 return BDS_EFI_ACPI_FLOPPY_BOOT
;
3906 case MESSAGING_DEVICE_PATH
:
3908 // Get the last device path node
3910 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
3911 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
3913 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
3916 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
3919 // if the device path not only point to driver device, it is not a messaging device path,
3921 if (!IsDevicePathEndType (LastDeviceNode
)) {
3925 switch (DevicePathSubType (TempDevicePath
)) {
3927 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
3931 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
3935 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
3939 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
3942 case MSG_MAC_ADDR_DP
:
3946 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
3950 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
3958 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
3961 return BDS_EFI_UNSUPPORT
;
3965 Check whether the Device path in a boot option point to a valid bootable device,
3966 And if CheckMedia is true, check the device is ready to boot now.
3968 @param DevPath the Device path in a boot option
3969 @param CheckMedia if true, check the device is ready to boot now.
3971 @retval TRUE the Device path is valid
3972 @retval FALSE the Device path is invalid .
3977 BdsLibIsValidEFIBootOptDevicePath (
3978 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
3979 IN BOOLEAN CheckMedia
3982 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
3986 Check whether the Device path in a boot option point to a valid bootable device,
3987 And if CheckMedia is true, check the device is ready to boot now.
3988 If Description is not NULL and the device path point to a fixed BlockIo
3989 device, check the description whether conflict with other auto-created
3992 @param DevPath the Device path in a boot option
3993 @param CheckMedia if true, check the device is ready to boot now.
3994 @param Description the description in a boot option
3996 @retval TRUE the Device path is valid
3997 @retval FALSE the Device path is invalid .
4002 BdsLibIsValidEFIBootOptDevicePathExt (
4003 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
4004 IN BOOLEAN CheckMedia
,
4005 IN CHAR16
*Description
4010 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4011 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4012 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
4014 TempDevicePath
= DevPath
;
4015 LastDeviceNode
= DevPath
;
4018 // Check if it's a valid boot option for network boot device.
4019 // Check if there is EfiLoadFileProtocol installed.
4020 // If yes, that means there is a boot option for network.
4022 Status
= gBS
->LocateDevicePath (
4023 &gEfiLoadFileProtocolGuid
,
4027 if (EFI_ERROR (Status
)) {
4029 // Device not present so see if we need to connect it
4031 TempDevicePath
= DevPath
;
4032 BdsLibConnectDevicePath (TempDevicePath
);
4033 Status
= gBS
->LocateDevicePath (
4034 &gEfiLoadFileProtocolGuid
,
4040 if (!EFI_ERROR (Status
)) {
4041 if (!IsDevicePathEnd (TempDevicePath
)) {
4043 // LoadFile protocol is not installed on handle with exactly the same DevPath
4050 // Test if it is ready to boot now
4052 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
4061 // If the boot option point to a file, it is a valid EFI boot option,
4062 // and assume it is ready to boot now
4064 while (!IsDevicePathEnd (TempDevicePath
)) {
4066 // If there is USB Class or USB WWID device path node, treat it as valid EFI
4067 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it
4068 // to full device path.
4070 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
4071 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
4072 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
4076 LastDeviceNode
= TempDevicePath
;
4077 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4079 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
4080 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
4085 // Check if it's a valid boot option for internal FV application
4087 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
4089 // If the boot option point to internal FV application, make sure it is valid
4091 TempDevicePath
= DevPath
;
4092 Status
= BdsLibUpdateFvFileDevicePath (
4094 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
)
4096 if (Status
== EFI_ALREADY_STARTED
) {
4099 if (Status
== EFI_SUCCESS
) {
4100 FreePool (TempDevicePath
);
4107 // If the boot option point to a blockIO device:
4108 // if it is a removable blockIo device, it is valid.
4109 // if it is a fixed blockIo device, check its description confliction.
4111 TempDevicePath
= DevPath
;
4112 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4113 if (EFI_ERROR (Status
)) {
4115 // Device not present so see if we need to connect it
4117 Status
= BdsLibConnectDevicePath (DevPath
);
4118 if (!EFI_ERROR (Status
)) {
4120 // Try again to get the Block Io protocol after we did the connect
4122 TempDevicePath
= DevPath
;
4123 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4127 if (!EFI_ERROR (Status
)) {
4128 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
4129 if (!EFI_ERROR (Status
)) {
4132 // Test if it is ready to boot now
4134 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4143 // 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,
4145 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
4146 if (!EFI_ERROR (Status
)) {
4149 // Test if it is ready to boot now
4151 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4165 According to a file guild, check a Fv file device path is valid. If it is invalid,
4166 try to return the valid device path.
4167 FV address maybe changes for memory layout adjust from time to time, use this function
4168 could promise the Fv file device path is right.
4170 @param DevicePath on input, the Fv file device path need to check on
4171 output, the updated valid Fv file device path
4172 @param FileGuid the Fv file guild
4174 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
4176 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
4178 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
4180 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
4181 and return the updated device path in DevicePath
4186 BdsLibUpdateFvFileDevicePath (
4187 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
4188 IN EFI_GUID
*FileGuid
4191 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4192 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4194 EFI_GUID
*GuidPoint
;
4196 UINTN FvHandleCount
;
4197 EFI_HANDLE
*FvHandleBuffer
;
4198 EFI_FV_FILETYPE Type
;
4200 EFI_FV_FILE_ATTRIBUTES Attributes
;
4201 UINT32 AuthenticationStatus
;
4203 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
4204 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
4205 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
4206 EFI_HANDLE FoundFvHandle
;
4207 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
4209 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
4210 return EFI_INVALID_PARAMETER
;
4212 if (FileGuid
== NULL
) {
4213 return EFI_INVALID_PARAMETER
;
4217 // Check whether the device path point to the default the input Fv file
4219 TempDevicePath
= *DevicePath
;
4220 LastDeviceNode
= TempDevicePath
;
4221 while (!IsDevicePathEnd (TempDevicePath
)) {
4222 LastDeviceNode
= TempDevicePath
;
4223 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4225 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
4226 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
4228 if (GuidPoint
== NULL
) {
4230 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
4232 return EFI_UNSUPPORTED
;
4234 if (!CompareGuid (GuidPoint
, FileGuid
)) {
4236 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
4238 return EFI_UNSUPPORTED
;
4242 // Check whether the input Fv file device path is valid
4244 TempDevicePath
= *DevicePath
;
4245 FoundFvHandle
= NULL
;
4246 Status
= gBS
->LocateDevicePath (
4247 &gEfiFirmwareVolume2ProtocolGuid
,
4251 if (!EFI_ERROR (Status
)) {
4252 Status
= gBS
->HandleProtocol (
4254 &gEfiFirmwareVolume2ProtocolGuid
,
4257 if (!EFI_ERROR (Status
)) {
4259 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
4261 Status
= Fv
->ReadFile (
4268 &AuthenticationStatus
4270 if (!EFI_ERROR (Status
)) {
4271 return EFI_ALREADY_STARTED
;
4277 // Look for the input wanted FV file in current FV
4278 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
4281 FoundFvHandle
= NULL
;
4282 Status
= gBS
->HandleProtocol (
4284 &gEfiLoadedImageProtocolGuid
,
4285 (VOID
**) &LoadedImage
4287 if (!EFI_ERROR (Status
)) {
4288 Status
= gBS
->HandleProtocol (
4289 LoadedImage
->DeviceHandle
,
4290 &gEfiFirmwareVolume2ProtocolGuid
,
4293 if (!EFI_ERROR (Status
)) {
4294 Status
= Fv
->ReadFile (
4301 &AuthenticationStatus
4303 if (!EFI_ERROR (Status
)) {
4305 FoundFvHandle
= LoadedImage
->DeviceHandle
;
4310 // Second, if fail to find, try to enumerate all FV
4313 FvHandleBuffer
= NULL
;
4314 gBS
->LocateHandleBuffer (
4316 &gEfiFirmwareVolume2ProtocolGuid
,
4321 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
4322 gBS
->HandleProtocol (
4323 FvHandleBuffer
[Index
],
4324 &gEfiFirmwareVolume2ProtocolGuid
,
4328 Status
= Fv
->ReadFile (
4335 &AuthenticationStatus
4337 if (EFI_ERROR (Status
)) {
4339 // Skip if input Fv file not in the FV
4344 FoundFvHandle
= FvHandleBuffer
[Index
];
4348 if (FvHandleBuffer
!= NULL
) {
4349 FreePool (FvHandleBuffer
);
4355 // Build the shell device path
4357 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
4358 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
4359 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
4360 ASSERT (NewDevicePath
!= NULL
);
4361 *DevicePath
= NewDevicePath
;
4364 return EFI_NOT_FOUND
;