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
)) {
868 OptionNumber
= BootOrder
[BootOrderSize
/ sizeof (UINT16
) - 1];
872 ASSERT (BbsIndex
== Index
);
875 Status
= gRT
->SetVariable (
877 &gEfiGlobalVariableGuid
,
878 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
882 if (BootOrder
!= NULL
) {
883 FreePool (BootOrder
);
890 Fill the device order buffer.
892 @param BbsTable The BBS table.
893 @param BbsType The BBS Type.
894 @param BbsCount The BBS Count.
895 @param Buf device order buffer.
897 @return The device order buffer.
902 IN BBS_TABLE
*BbsTable
,
910 for (Index
= 0; Index
< BbsCount
; Index
++) {
911 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
915 if (BbsTable
[Index
].DeviceType
!= BbsType
) {
919 *Buf
= (UINT16
) (Index
& 0xFF);
927 Create the device order buffer.
929 @param BbsTable The BBS table.
930 @param BbsCount The BBS Count.
932 @retval EFI_SUCCES The buffer is created and the EFI variable named
933 VAR_LEGACY_DEV_ORDER and gEfiLegacyDevOrderVariableGuid is
935 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
936 @retval EFI_DEVICE_ERROR Fail to add the device order into EFI variable fail
937 because of hardware error.
941 IN BBS_TABLE
*BbsTable
,
953 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
954 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
963 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
965 Status
= EFI_SUCCESS
;
968 // Count all boot devices
970 for (Index
= 0; Index
< BbsCount
; Index
++) {
971 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
975 switch (BbsTable
[Index
].DeviceType
) {
988 case BBS_EMBED_NETWORK
:
1001 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * FDCount
);
1002 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * HDCount
);
1003 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * CDCount
);
1004 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * NETCount
);
1005 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * BEVCount
);
1008 // Create buffer to hold all boot device order
1010 DevOrder
= AllocateZeroPool (TotalSize
);
1011 if (NULL
== DevOrder
) {
1012 return EFI_OUT_OF_RESOURCES
;
1014 DevOrderPtr
= DevOrder
;
1016 DevOrderPtr
->BbsType
= BBS_FLOPPY
;
1017 DevOrderPtr
->Length
= (UINT16
) (sizeof (DevOrderPtr
->Length
) + FDCount
* sizeof (UINT16
));
1018 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_FLOPPY
, BbsCount
, DevOrderPtr
->Data
);
1020 DevOrderPtr
->BbsType
= BBS_HARDDISK
;
1021 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1022 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_HARDDISK
, BbsCount
, DevOrderPtr
->Data
);
1024 DevOrderPtr
->BbsType
= BBS_CDROM
;
1025 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1026 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_CDROM
, BbsCount
, DevOrderPtr
->Data
);
1028 DevOrderPtr
->BbsType
= BBS_EMBED_NETWORK
;
1029 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1030 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_EMBED_NETWORK
, BbsCount
, DevOrderPtr
->Data
);
1032 DevOrderPtr
->BbsType
= BBS_BEV_DEVICE
;
1033 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1034 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_BEV_DEVICE
, BbsCount
, DevOrderPtr
->Data
);
1036 ASSERT (TotalSize
== (UINTN
) ((UINT8
*) DevOrderPtr
- (UINT8
*) DevOrder
));
1039 // Save device order for legacy boot device to variable.
1041 Status
= gRT
->SetVariable (
1042 VAR_LEGACY_DEV_ORDER
,
1043 &gEfiLegacyDevOrderVariableGuid
,
1044 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1048 FreePool (DevOrder
);
1054 Add the legacy boot devices from BBS table into
1055 the legacy device boot order.
1057 @retval EFI_SUCCESS The boot devices are added successfully.
1058 @retval EFI_NOT_FOUND The legacy boot devices are not found.
1059 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
1060 @retval EFI_DEVICE_ERROR Fail to add the legacy device boot order into EFI variable
1061 because of hardware error.
1065 BdsUpdateLegacyDevOrder (
1069 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1070 LEGACY_DEV_ORDER_ENTRY
*NewDevOrder
;
1071 LEGACY_DEV_ORDER_ENTRY
*Ptr
;
1072 LEGACY_DEV_ORDER_ENTRY
*NewPtr
;
1074 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1078 HDD_INFO
*LocalHddInfo
;
1079 BBS_TABLE
*LocalBbsTable
;
1109 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
1117 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1118 if (EFI_ERROR (Status
)) {
1122 Status
= LegacyBios
->GetBbsInfo (
1129 if (EFI_ERROR (Status
)) {
1133 DevOrder
= BdsLibGetVariableAndSize (
1134 VAR_LEGACY_DEV_ORDER
,
1135 &gEfiLegacyDevOrderVariableGuid
,
1138 if (NULL
== DevOrder
) {
1139 return BdsCreateDevOrder (LocalBbsTable
, BbsCount
);
1142 // First we figure out how many boot devices with same device type respectively
1144 for (Index
= 0; Index
< BbsCount
; Index
++) {
1145 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1146 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1151 switch (LocalBbsTable
[Index
].DeviceType
) {
1164 case BBS_EMBED_NETWORK
:
1168 case BBS_BEV_DEVICE
:
1177 TotalSize
+= (HeaderSize
+ FDCount
* sizeof (UINT16
));
1178 TotalSize
+= (HeaderSize
+ HDCount
* sizeof (UINT16
));
1179 TotalSize
+= (HeaderSize
+ CDCount
* sizeof (UINT16
));
1180 TotalSize
+= (HeaderSize
+ NETCount
* sizeof (UINT16
));
1181 TotalSize
+= (HeaderSize
+ BEVCount
* sizeof (UINT16
));
1183 NewDevOrder
= AllocateZeroPool (TotalSize
);
1184 if (NULL
== NewDevOrder
) {
1185 return EFI_OUT_OF_RESOURCES
;
1194 NewPtr
= NewDevOrder
;
1195 NewPtr
->BbsType
= Ptr
->BbsType
;
1196 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + FDCount
* sizeof (UINT16
));
1197 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1198 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1199 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1200 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_FLOPPY
1205 NewPtr
->Data
[FDIndex
] = Ptr
->Data
[Index
];
1208 NewFDPtr
= NewPtr
->Data
;
1213 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1214 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1215 NewPtr
->BbsType
= Ptr
->BbsType
;
1216 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1217 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1218 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1219 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1220 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1221 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_HARDDISK
1226 NewPtr
->Data
[HDIndex
] = Ptr
->Data
[Index
];
1229 NewHDPtr
= NewPtr
->Data
;
1234 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1235 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1236 NewPtr
->BbsType
= Ptr
->BbsType
;
1237 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1238 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1239 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1240 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1241 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1242 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_CDROM
1247 NewPtr
->Data
[CDIndex
] = Ptr
->Data
[Index
];
1250 NewCDPtr
= NewPtr
->Data
;
1255 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1256 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1257 NewPtr
->BbsType
= Ptr
->BbsType
;
1258 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1259 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1260 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1261 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1262 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1263 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_EMBED_NETWORK
1268 NewPtr
->Data
[NETIndex
] = Ptr
->Data
[Index
];
1271 NewNETPtr
= NewPtr
->Data
;
1276 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1277 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1278 NewPtr
->BbsType
= Ptr
->BbsType
;
1279 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1280 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1281 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1282 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1283 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1284 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_BEV_DEVICE
1289 NewPtr
->Data
[BEVIndex
] = Ptr
->Data
[Index
];
1292 NewBEVPtr
= NewPtr
->Data
;
1294 for (Index
= 0; Index
< BbsCount
; Index
++) {
1295 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1296 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1301 switch (LocalBbsTable
[Index
].DeviceType
) {
1304 NewDevPtr
= NewFDPtr
;
1309 NewDevPtr
= NewHDPtr
;
1314 NewDevPtr
= NewCDPtr
;
1317 case BBS_EMBED_NETWORK
:
1319 NewDevPtr
= NewNETPtr
;
1322 case BBS_BEV_DEVICE
:
1324 NewDevPtr
= NewBEVPtr
;
1332 // at this point we have copied those valid indexes to new buffer
1333 // and we should check if there is any new appeared boot device
1336 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1337 if ((NewDevPtr
[Index2
] & 0xFF) == (UINT16
) Index
) {
1342 if (Index2
== *Idx
) {
1344 // Index2 == *Idx means we didn't find Index
1345 // so Index is a new appeared device's index in BBS table
1346 // insert it before disabled indexes.
1348 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1349 if ((NewDevPtr
[Index2
] & 0xFF00) == 0xFF00) {
1353 CopyMem (&NewDevPtr
[Index2
+ 1], &NewDevPtr
[Index2
], (*Idx
- Index2
) * sizeof (UINT16
));
1354 NewDevPtr
[Index2
] = (UINT16
) (Index
& 0xFF);
1360 FreePool (DevOrder
);
1362 Status
= gRT
->SetVariable (
1363 VAR_LEGACY_DEV_ORDER
,
1364 &gEfiLegacyDevOrderVariableGuid
,
1365 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1369 FreePool (NewDevOrder
);
1375 Set Boot Priority for specified device type.
1377 @param DeviceType The device type.
1378 @param BbsIndex The BBS index to set the highest priority. Ignore when -1.
1379 @param LocalBbsTable The BBS table.
1380 @param Priority The prority table.
1382 @retval EFI_SUCCESS The function completes successfully.
1383 @retval EFI_NOT_FOUND Failed to find device.
1384 @retval EFI_OUT_OF_RESOURCES Failed to get the efi variable of device order.
1388 BdsSetBootPriority4SameTypeDev (
1389 IN UINT16 DeviceType
,
1391 IN OUT BBS_TABLE
*LocalBbsTable
,
1392 IN OUT UINT16
*Priority
1395 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1396 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
1400 DevOrder
= BdsLibGetVariableAndSize (
1401 VAR_LEGACY_DEV_ORDER
,
1402 &gEfiLegacyDevOrderVariableGuid
,
1405 if (NULL
== DevOrder
) {
1406 return EFI_OUT_OF_RESOURCES
;
1409 DevOrderPtr
= DevOrder
;
1410 while ((UINT8
*) DevOrderPtr
< (UINT8
*) DevOrder
+ DevOrderSize
) {
1411 if (DevOrderPtr
->BbsType
== DeviceType
) {
1415 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) ((UINT8
*) DevOrderPtr
+ sizeof (BBS_TYPE
) + DevOrderPtr
->Length
);
1418 if ((UINT8
*) DevOrderPtr
>= (UINT8
*) DevOrder
+ DevOrderSize
) {
1419 FreePool (DevOrder
);
1420 return EFI_NOT_FOUND
;
1423 if (BbsIndex
!= (UINTN
) -1) {
1424 LocalBbsTable
[BbsIndex
].BootPriority
= *Priority
;
1428 // If the high byte of the DevIndex is 0xFF, it indicates that this device has been disabled.
1430 for (Index
= 0; Index
< DevOrderPtr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1431 if ((DevOrderPtr
->Data
[Index
] & 0xFF00) == 0xFF00) {
1433 // LocalBbsTable[DevIndex[Index] & 0xFF].BootPriority = BBS_DISABLED_ENTRY;
1435 } else if (DevOrderPtr
->Data
[Index
] != BbsIndex
) {
1436 LocalBbsTable
[DevOrderPtr
->Data
[Index
]].BootPriority
= *Priority
;
1441 FreePool (DevOrder
);
1446 Print the BBS Table.
1448 @param LocalBbsTable The BBS table.
1449 @param BbsCount The count of entry in BBS table.
1453 IN BBS_TABLE
*LocalBbsTable
,
1459 DEBUG ((DEBUG_ERROR
, "\n"));
1460 DEBUG ((DEBUG_ERROR
, " NO Prio bb/dd/ff cl/sc Type Stat segm:offs\n"));
1461 DEBUG ((DEBUG_ERROR
, "=============================================\n"));
1462 for (Idx
= 0; Idx
< BbsCount
; Idx
++) {
1463 if ((LocalBbsTable
[Idx
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1464 (LocalBbsTable
[Idx
].BootPriority
== BBS_DO_NOT_BOOT_FROM
) ||
1465 (LocalBbsTable
[Idx
].BootPriority
== BBS_LOWEST_PRIORITY
)
1472 " %02x: %04x %02x/%02x/%02x %02x/%02x %04x %04x %04x:%04x\n",
1474 (UINTN
) LocalBbsTable
[Idx
].BootPriority
,
1475 (UINTN
) LocalBbsTable
[Idx
].Bus
,
1476 (UINTN
) LocalBbsTable
[Idx
].Device
,
1477 (UINTN
) LocalBbsTable
[Idx
].Function
,
1478 (UINTN
) LocalBbsTable
[Idx
].Class
,
1479 (UINTN
) LocalBbsTable
[Idx
].SubClass
,
1480 (UINTN
) LocalBbsTable
[Idx
].DeviceType
,
1481 (UINTN
) * (UINT16
*) &LocalBbsTable
[Idx
].StatusFlags
,
1482 (UINTN
) LocalBbsTable
[Idx
].BootHandlerSegment
,
1483 (UINTN
) LocalBbsTable
[Idx
].BootHandlerOffset
,
1484 (UINTN
) ((LocalBbsTable
[Idx
].MfgStringSegment
<< 4) + LocalBbsTable
[Idx
].MfgStringOffset
),
1485 (UINTN
) ((LocalBbsTable
[Idx
].DescStringSegment
<< 4) + LocalBbsTable
[Idx
].DescStringOffset
))
1489 DEBUG ((DEBUG_ERROR
, "\n"));
1493 Set the boot priority for BBS entries based on boot option entry and boot order.
1495 @param Entry The boot option is to be checked for refresh BBS table.
1497 @retval EFI_SUCCESS The boot priority for BBS entries is refreshed successfully.
1498 @retval EFI_NOT_FOUND BBS entries can't be found.
1499 @retval EFI_OUT_OF_RESOURCES Failed to get the legacy device boot order.
1503 BdsRefreshBbsTableForBoot (
1504 IN BDS_COMMON_OPTION
*Entry
1511 HDD_INFO
*LocalHddInfo
;
1512 BBS_TABLE
*LocalBbsTable
;
1514 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1518 UINTN BootOrderSize
;
1519 UINT8
*BootOptionVar
;
1520 UINTN BootOptionSize
;
1521 CHAR16 BootOption
[9];
1524 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1526 UINTN DeviceTypeCount
;
1527 UINTN DeviceTypeIndex
;
1531 LocalHddInfo
= NULL
;
1532 LocalBbsTable
= NULL
;
1533 DevType
= BBS_UNKNOWN
;
1535 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1536 if (EFI_ERROR (Status
)) {
1540 LegacyBios
->GetBbsInfo (
1548 // First, set all the present devices' boot priority to BBS_UNPRIORITIZED_ENTRY
1549 // We will set them according to the settings setup by user
1551 for (Index
= 0; Index
< BbsCount
; Index
++) {
1552 if (!((BBS_IGNORE_ENTRY
== LocalBbsTable
[Index
].BootPriority
) ||
1553 (BBS_DO_NOT_BOOT_FROM
== LocalBbsTable
[Index
].BootPriority
) ||
1554 (BBS_LOWEST_PRIORITY
== LocalBbsTable
[Index
].BootPriority
))) {
1555 LocalBbsTable
[Index
].BootPriority
= BBS_UNPRIORITIZED_ENTRY
;
1559 // boot priority always starts at 0
1562 if (Entry
->LoadOptionsSize
== sizeof (BBS_TABLE
) + sizeof (UINT16
)) {
1564 // If Entry stands for a legacy boot option, we prioritize the devices with the same type first.
1566 DevType
= ((BBS_TABLE
*) Entry
->LoadOptions
)->DeviceType
;
1567 BbsIndex
= *(UINT16
*) ((BBS_TABLE
*) Entry
->LoadOptions
+ 1);
1568 Status
= BdsSetBootPriority4SameTypeDev (
1574 if (EFI_ERROR (Status
)) {
1579 // we have to set the boot priority for other BBS entries with different device types
1581 BootOrder
= BdsLibGetVariableAndSize (
1583 &gEfiGlobalVariableGuid
,
1586 DeviceType
= AllocatePool (BootOrderSize
+ sizeof (UINT16
));
1587 ASSERT (DeviceType
!= NULL
);
1589 DeviceType
[0] = DevType
;
1590 DeviceTypeCount
= 1;
1591 for (Index
= 0; ((BootOrder
!= NULL
) && (Index
< BootOrderSize
/ sizeof (UINT16
))); Index
++) {
1592 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
1593 BootOptionVar
= BdsLibGetVariableAndSize (
1595 &gEfiGlobalVariableGuid
,
1598 if (NULL
== BootOptionVar
) {
1602 Ptr
= BootOptionVar
;
1604 Ptr
+= sizeof (UINT32
);
1605 DevPathLen
= *(UINT16
*) Ptr
;
1606 Ptr
+= sizeof (UINT16
);
1607 Ptr
+= StrSize ((UINT16
*) Ptr
);
1608 DevPath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
1609 if (BBS_DEVICE_PATH
!= DevPath
->Type
|| BBS_BBS_DP
!= DevPath
->SubType
) {
1610 FreePool (BootOptionVar
);
1615 DevType
= ((BBS_TABLE
*) Ptr
)->DeviceType
;
1616 for (DeviceTypeIndex
= 0; DeviceTypeIndex
< DeviceTypeCount
; DeviceTypeIndex
++) {
1617 if (DeviceType
[DeviceTypeIndex
] == DevType
) {
1621 if (DeviceTypeIndex
< DeviceTypeCount
) {
1623 // We don't want to process twice for a device type
1625 FreePool (BootOptionVar
);
1629 DeviceType
[DeviceTypeCount
] = DevType
;
1632 Status
= BdsSetBootPriority4SameTypeDev (
1638 FreePool (BootOptionVar
);
1639 if (EFI_ERROR (Status
)) {
1644 if (BootOrder
!= NULL
) {
1645 FreePool (BootOrder
);
1649 PrintBbsTable (LocalBbsTable
, BbsCount
);
1656 Boot the legacy system with the boot option
1658 @param Option The legacy boot option which have BBS device path
1660 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
1662 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
1666 BdsLibDoLegacyBoot (
1667 IN BDS_COMMON_OPTION
*Option
1671 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1672 EFI_EVENT LegacyBootEvent
;
1674 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1675 if (EFI_ERROR (Status
)) {
1677 // If no LegacyBios protocol we do not support legacy boot
1679 return EFI_UNSUPPORTED
;
1682 // Notes: if we separate the int 19, then we don't need to refresh BBS
1684 BdsRefreshBbsTableForBoot (Option
);
1687 // Write boot to OS performance data for legacy boot.
1691 // Create an event to be signalled when Legacy Boot occurs to write performance data.
1693 Status
= EfiCreateEventLegacyBootEx(
1695 WriteBootToOsPerformanceData
,
1699 ASSERT_EFI_ERROR (Status
);
1702 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
1703 return LegacyBios
->LegacyBoot (
1705 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
1706 Option
->LoadOptionsSize
,
1712 Internal function to check if the input boot option is a valid EFI NV Boot####.
1714 @param OptionToCheck Boot option to be checked.
1716 @retval TRUE This boot option matches a valid EFI NV Boot####.
1717 @retval FALSE If not.
1721 IsBootOptionValidNVVarialbe (
1722 IN BDS_COMMON_OPTION
*OptionToCheck
1725 LIST_ENTRY TempList
;
1726 BDS_COMMON_OPTION
*BootOption
;
1728 CHAR16 OptionName
[20];
1732 InitializeListHead (&TempList
);
1733 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
1735 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
1736 if (BootOption
== NULL
) {
1741 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
1742 // valid EFI NV Boot####.
1744 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
1745 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
1750 FreePool (BootOption
);
1756 Check whether a USB device match the specified USB Class device path. This
1757 function follows "Load Option Processing" behavior in UEFI specification.
1759 @param UsbIo USB I/O protocol associated with the USB device.
1760 @param UsbClass The USB Class device path to match.
1762 @retval TRUE The USB device match the USB Class device path.
1763 @retval FALSE The USB device does not match the USB Class device path.
1768 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1769 IN USB_CLASS_DEVICE_PATH
*UsbClass
1773 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1774 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1776 UINT8 DeviceSubClass
;
1777 UINT8 DeviceProtocol
;
1779 if ((DevicePathType (UsbClass
) != MESSAGING_DEVICE_PATH
) ||
1780 (DevicePathSubType (UsbClass
) != MSG_USB_CLASS_DP
)){
1785 // Check Vendor Id and Product Id.
1787 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1788 if (EFI_ERROR (Status
)) {
1792 if ((UsbClass
->VendorId
!= 0xffff) &&
1793 (UsbClass
->VendorId
!= DevDesc
.IdVendor
)) {
1797 if ((UsbClass
->ProductId
!= 0xffff) &&
1798 (UsbClass
->ProductId
!= DevDesc
.IdProduct
)) {
1802 DeviceClass
= DevDesc
.DeviceClass
;
1803 DeviceSubClass
= DevDesc
.DeviceSubClass
;
1804 DeviceProtocol
= DevDesc
.DeviceProtocol
;
1805 if (DeviceClass
== 0) {
1807 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
1808 // Protocol in Interface Descriptor instead.
1810 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1811 if (EFI_ERROR (Status
)) {
1815 DeviceClass
= IfDesc
.InterfaceClass
;
1816 DeviceSubClass
= IfDesc
.InterfaceSubClass
;
1817 DeviceProtocol
= IfDesc
.InterfaceProtocol
;
1821 // Check Class, SubClass and Protocol.
1823 if ((UsbClass
->DeviceClass
!= 0xff) &&
1824 (UsbClass
->DeviceClass
!= DeviceClass
)) {
1828 if ((UsbClass
->DeviceSubClass
!= 0xff) &&
1829 (UsbClass
->DeviceSubClass
!= DeviceSubClass
)) {
1833 if ((UsbClass
->DeviceProtocol
!= 0xff) &&
1834 (UsbClass
->DeviceProtocol
!= DeviceProtocol
)) {
1842 Check whether a USB device match the specified USB WWID device path. This
1843 function follows "Load Option Processing" behavior in UEFI specification.
1845 @param UsbIo USB I/O protocol associated with the USB device.
1846 @param UsbWwid The USB WWID device path to match.
1848 @retval TRUE The USB device match the USB WWID device path.
1849 @retval FALSE The USB device does not match the USB WWID device path.
1854 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1855 IN USB_WWID_DEVICE_PATH
*UsbWwid
1859 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1860 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1861 UINT16
*LangIdTable
;
1866 CHAR16
*SerialNumberStr
;
1869 if ((DevicePathType (UsbWwid
) != MESSAGING_DEVICE_PATH
) ||
1870 (DevicePathSubType (UsbWwid
) != MSG_USB_WWID_DP
)){
1875 // Check Vendor Id and Product Id.
1877 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1878 if (EFI_ERROR (Status
)) {
1881 if ((DevDesc
.IdVendor
!= UsbWwid
->VendorId
) ||
1882 (DevDesc
.IdProduct
!= UsbWwid
->ProductId
)) {
1887 // Check Interface Number.
1889 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1890 if (EFI_ERROR (Status
)) {
1893 if (IfDesc
.InterfaceNumber
!= UsbWwid
->InterfaceNumber
) {
1898 // Check Serial Number.
1900 if (DevDesc
.StrSerialNumber
== 0) {
1905 // Get all supported languages.
1909 Status
= UsbIo
->UsbGetSupportedLanguages (UsbIo
, &LangIdTable
, &TableSize
);
1910 if (EFI_ERROR (Status
) || (TableSize
== 0) || (LangIdTable
== NULL
)) {
1915 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
1917 CompareStr
= (CHAR16
*) (UINTN
) (UsbWwid
+ 1);
1918 CompareLen
= (DevicePathNodeLength (UsbWwid
) - sizeof (USB_WWID_DEVICE_PATH
)) / sizeof (CHAR16
);
1919 if (CompareStr
[CompareLen
- 1] == L
'\0') {
1924 // Compare serial number in each supported language.
1926 for (Index
= 0; Index
< TableSize
/ sizeof (UINT16
); Index
++) {
1927 SerialNumberStr
= NULL
;
1928 Status
= UsbIo
->UsbGetStringDescriptor (
1931 DevDesc
.StrSerialNumber
,
1934 if (EFI_ERROR (Status
) || (SerialNumberStr
== NULL
)) {
1938 Length
= StrLen (SerialNumberStr
);
1939 if ((Length
>= CompareLen
) &&
1940 (CompareMem (SerialNumberStr
+ Length
- CompareLen
, CompareStr
, CompareLen
* sizeof (CHAR16
)) == 0)) {
1941 FreePool (SerialNumberStr
);
1945 FreePool (SerialNumberStr
);
1952 Find a USB device path which match the specified short-form device path start
1953 with USB Class or USB WWID device path and load the boot file then return the
1954 image handle. If ParentDevicePath is NULL, this function will search in all USB
1955 devices of the platform. If ParentDevicePath is not NULL,this function will only
1956 search in its child devices.
1958 @param ParentDevicePath The device path of the parent.
1959 @param ShortFormDevicePath The USB Class or USB WWID device path to match.
1961 @return The image Handle if find load file from specified short-form device path
1962 or NULL if not found.
1967 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
1968 IN EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
1972 UINTN UsbIoHandleCount
;
1973 EFI_HANDLE
*UsbIoHandleBuffer
;
1974 EFI_DEVICE_PATH_PROTOCOL
*UsbIoDevicePath
;
1975 EFI_USB_IO_PROTOCOL
*UsbIo
;
1979 EFI_HANDLE ImageHandle
;
1981 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
1982 EFI_DEVICE_PATH_PROTOCOL
*NextDevicePath
;
1984 FullDevicePath
= NULL
;
1988 // Get all UsbIo Handles.
1990 UsbIoHandleCount
= 0;
1991 UsbIoHandleBuffer
= NULL
;
1992 Status
= gBS
->LocateHandleBuffer (
1994 &gEfiUsbIoProtocolGuid
,
1999 if (EFI_ERROR (Status
) || (UsbIoHandleCount
== 0) || (UsbIoHandleBuffer
== NULL
)) {
2003 ParentSize
= (ParentDevicePath
== NULL
) ? 0 : GetDevicePathSize (ParentDevicePath
);
2004 for (Index
= 0; Index
< UsbIoHandleCount
; Index
++) {
2006 // Get the Usb IO interface.
2008 Status
= gBS
->HandleProtocol(
2009 UsbIoHandleBuffer
[Index
],
2010 &gEfiUsbIoProtocolGuid
,
2013 if (EFI_ERROR (Status
)) {
2017 UsbIoDevicePath
= DevicePathFromHandle (UsbIoHandleBuffer
[Index
]);
2018 if (UsbIoDevicePath
== NULL
) {
2022 if (ParentDevicePath
!= NULL
) {
2024 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
2026 Size
= GetDevicePathSize (UsbIoDevicePath
);
2027 if ((Size
< ParentSize
) ||
2028 (CompareMem (UsbIoDevicePath
, ParentDevicePath
, ParentSize
- END_DEVICE_PATH_LENGTH
) != 0)) {
2033 if (BdsMatchUsbClass (UsbIo
, (USB_CLASS_DEVICE_PATH
*) ShortFormDevicePath
) ||
2034 BdsMatchUsbWwid (UsbIo
, (USB_WWID_DEVICE_PATH
*) ShortFormDevicePath
)) {
2036 // Try to find if there is the boot file in this DevicePath
2038 NextDevicePath
= NextDevicePathNode (ShortFormDevicePath
);
2039 if (!IsDevicePathEnd (NextDevicePath
)) {
2040 FullDevicePath
= AppendDevicePath (UsbIoDevicePath
, NextDevicePath
);
2042 // Connect the full device path, so that Simple File System protocol
2043 // could be installed for this USB device.
2045 BdsLibConnectDevicePath (FullDevicePath
);
2046 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2047 Status
= gBS
->LoadImage (
2055 FreePool (FullDevicePath
);
2057 FullDevicePath
= UsbIoDevicePath
;
2058 Status
= EFI_NOT_FOUND
;
2062 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2063 // and load the image according to the default boot behavior for removable device.
2065 if (EFI_ERROR (Status
)) {
2067 // check if there is a bootable removable media could be found in this device path ,
2068 // and get the bootable media handle
2070 Handle
= BdsLibGetBootableHandle(UsbIoDevicePath
);
2071 if (Handle
== NULL
) {
2075 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2076 // machinename is ia32, ia64, x64, ...
2078 FullDevicePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2079 if (FullDevicePath
!= NULL
) {
2080 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2081 Status
= gBS
->LoadImage (
2089 if (EFI_ERROR (Status
)) {
2091 // The DevicePath failed, and it's not a valid
2092 // removable media device.
2104 FreePool (UsbIoHandleBuffer
);
2109 Expand USB Class or USB WWID device path node to be full device path of a USB
2110 device in platform then load the boot file on this full device path and return the
2113 This function support following 4 cases:
2114 1) Boot Option device path starts with a USB Class or USB WWID device path,
2115 and there is no Media FilePath device path in the end.
2116 In this case, it will follow Removable Media Boot Behavior.
2117 2) Boot Option device path starts with a USB Class or USB WWID device path,
2118 and ended with Media FilePath device path.
2119 3) Boot Option device path starts with a full device path to a USB Host Controller,
2120 contains a USB Class or USB WWID device path node, while not ended with Media
2121 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
2122 4) 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, and ended with Media
2124 FilePath device path.
2126 @param DevicePath The Boot Option device path.
2128 @return The image handle of boot file, or NULL if there is no boot file found in
2129 the specified USB Class or USB WWID device path.
2133 BdsExpandUsbShortFormDevicePath (
2134 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
2137 EFI_HANDLE
*ImageHandle
;
2138 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2139 EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
;
2142 // Search for USB Class or USB WWID device path node.
2144 ShortFormDevicePath
= NULL
;
2146 TempDevicePath
= DevicePath
;
2147 while (!IsDevicePathEnd (TempDevicePath
)) {
2148 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
2149 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
2150 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
2151 ShortFormDevicePath
= TempDevicePath
;
2154 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2157 if (ShortFormDevicePath
== NULL
) {
2159 // No USB Class or USB WWID device path node found, do nothing.
2164 if (ShortFormDevicePath
== DevicePath
) {
2166 // Boot Option device path starts with USB Class or USB WWID device path.
2168 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2169 if (ImageHandle
== NULL
) {
2171 // Failed to find a match in existing devices, connect the short form USB
2172 // device path and try again.
2174 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath
);
2175 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2179 // Boot Option device path contains USB Class or USB WWID device path node.
2183 // Prepare the parent device path for search.
2185 TempDevicePath
= DuplicateDevicePath (DevicePath
);
2186 ASSERT (TempDevicePath
!= NULL
);
2187 SetDevicePathEndNode (((UINT8
*) TempDevicePath
) + ((UINTN
) ShortFormDevicePath
- (UINTN
) DevicePath
));
2190 // The USB Host Controller device path is already in Boot Option device path
2191 // and USB Bus driver already support RemainingDevicePath starts with USB
2192 // Class or USB WWID device path, so just search in existing USB devices and
2193 // doesn't perform ConnectController here.
2195 ImageHandle
= BdsFindUsbDevice (TempDevicePath
, ShortFormDevicePath
);
2196 FreePool (TempDevicePath
);
2203 Process the boot option follow the UEFI specification and
2204 special treat the legacy boot option with BBS_DEVICE_PATH.
2206 @param Option The boot option need to be processed
2207 @param DevicePath The device path which describe where to load the
2208 boot image or the legacy BBS device path to boot
2210 @param ExitDataSize The size of exit data.
2211 @param ExitData Data returned when Boot image failed.
2213 @retval EFI_SUCCESS Boot from the input boot option successfully.
2214 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2219 BdsLibBootViaBootOption (
2220 IN BDS_COMMON_OPTION
*Option
,
2221 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2222 OUT UINTN
*ExitDataSize
,
2223 OUT CHAR16
**ExitData OPTIONAL
2227 EFI_STATUS StatusLogo
;
2229 EFI_HANDLE ImageHandle
;
2230 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
2231 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
2232 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
2233 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
2234 LIST_ENTRY TempBootLists
;
2235 EFI_BOOT_LOGO_PROTOCOL
*BootLogo
;
2241 // Notes: this code can be remove after the s3 script table
2242 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
2243 // EVT_SIGNAL_LEGACY_BOOT
2245 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
2246 if (!EFI_ERROR (Status
)) {
2247 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
2250 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
2253 WorkingDevicePath
= NULL
;
2254 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2255 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
2256 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
2257 (HARDDRIVE_DEVICE_PATH
*)DevicePath
2259 if (WorkingDevicePath
!= NULL
) {
2260 DevicePath
= WorkingDevicePath
;
2267 if (IsBootOptionValidNVVarialbe (Option
)) {
2269 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
2270 // In this case, "BootCurrent" is not created.
2271 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
2273 SetVariableAndReportStatusCodeOnError (
2275 &gEfiGlobalVariableGuid
,
2276 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2278 &Option
->BootCurrent
2283 // Report Status Code to indicate ReadyToBoot event will be signalled
2285 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT
));
2288 // Signal the EVT_SIGNAL_READY_TO_BOOT event
2290 EfiSignalEventReadyToBoot();
2293 // Expand USB Class or USB WWID device path node to be full device path of a USB
2294 // device in platform then load the boot file on this full device path and get the
2297 ImageHandle
= BdsExpandUsbShortFormDevicePath (DevicePath
);
2300 // Adjust the different type memory page number just before booting
2301 // and save the updated info into the variable for next boot to use
2303 BdsSetMemoryTypeInformationVariable ();
2306 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.
2307 // Here get the ImageHandle for the non USB class or WWID device path.
2309 if (ImageHandle
== NULL
) {
2310 ASSERT (Option
->DevicePath
!= NULL
);
2311 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
2312 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
2315 // Check to see if we should legacy BOOT. If yes then do the legacy boot
2317 return BdsLibDoLegacyBoot (Option
);
2321 // If the boot option point to Internal FV shell, make sure it is valid
2323 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
2324 if (!EFI_ERROR(Status
)) {
2325 if (Option
->DevicePath
!= NULL
) {
2326 FreePool(Option
->DevicePath
);
2328 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
2329 ASSERT(Option
->DevicePath
!= NULL
);
2330 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
2332 // Update the shell boot option
2334 InitializeListHead (&TempBootLists
);
2335 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
2338 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
2340 FreePool (DevicePath
);
2341 DevicePath
= Option
->DevicePath
;
2346 if (Option
->Description
== NULL
) {
2347 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
2349 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
2355 // Report status code for OS Loader LoadImage.
2357 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2358 Status
= gBS
->LoadImage (
2368 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2369 // and load the image according to the default boot behavior for removable device.
2371 if (EFI_ERROR (Status
)) {
2373 // check if there is a bootable removable media could be found in this device path ,
2374 // and get the bootable media handle
2376 Handle
= BdsLibGetBootableHandle(DevicePath
);
2377 if (Handle
!= NULL
) {
2379 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2380 // machinename is ia32, ia64, x64, ...
2382 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2383 if (FilePath
!= NULL
) {
2384 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2385 Status
= gBS
->LoadImage (
2398 // Provide the image with it's load options
2400 if ((ImageHandle
== NULL
) || (EFI_ERROR(Status
))) {
2402 // Report Status Code to indicate that the failure to load boot option
2404 REPORT_STATUS_CODE (
2405 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2406 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR
)
2411 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
2412 ASSERT_EFI_ERROR (Status
);
2414 if (Option
->LoadOptionsSize
!= 0) {
2415 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
2416 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
2420 // Clean to NULL because the image is loaded directly from the firmwares boot manager.
2422 ImageInfo
->ParentHandle
= NULL
;
2425 // Before calling the image, enable the Watchdog Timer for
2426 // the 5 Minute period
2428 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
2431 // Write boot to OS performance data for UEFI boot
2434 WriteBootToOsPerformanceData (NULL
, NULL
);
2438 // Report status code for OS Loader StartImage.
2440 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderStart
));
2442 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
2443 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
2444 if (EFI_ERROR (Status
)) {
2446 // Report Status Code to indicate that boot failure
2448 REPORT_STATUS_CODE (
2449 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2450 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED
)
2455 // Clear the Watchdog Timer after the image returns
2457 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
2461 // Set Logo status invalid after trying one boot option
2464 StatusLogo
= gBS
->LocateProtocol (&gEfiBootLogoProtocolGuid
, NULL
, (VOID
**) &BootLogo
);
2465 if (!EFI_ERROR (StatusLogo
) && (BootLogo
!= NULL
)) {
2466 BootLogo
->SetBootLogo (BootLogo
, NULL
, 0, 0, 0, 0);
2470 // Clear Boot Current
2471 // Deleting variable with current implementation shouldn't fail.
2475 &gEfiGlobalVariableGuid
,
2476 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2486 Expand a device path that starts with a hard drive media device path node to be a
2487 full device path that includes the full hardware path to the device. We need
2488 to do this so it can be booted. As an optimization the front match (the part point
2489 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
2490 so a connect all is not required on every boot. All successful history device path
2491 which point to partition node (the front part) will be saved.
2493 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
2494 drive media device path.
2495 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
2499 EFI_DEVICE_PATH_PROTOCOL
*
2501 BdsExpandPartitionPartialDevicePathToFull (
2502 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2506 UINTN BlockIoHandleCount
;
2507 EFI_HANDLE
*BlockIoBuffer
;
2508 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
2509 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
2510 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2513 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
2514 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
2515 UINTN CachedDevicePathSize
;
2516 BOOLEAN DeviceExist
;
2518 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
2521 FullDevicePath
= NULL
;
2523 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2524 // If exist, search the front path which point to partition node in the variable instants.
2525 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system
2527 CachedDevicePath
= BdsLibGetVariableAndSize (
2528 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2529 &gHdBootDevicePathVariablGuid
,
2530 &CachedDevicePathSize
2533 if (CachedDevicePath
!= NULL
) {
2534 TempNewDevicePath
= CachedDevicePath
;
2535 DeviceExist
= FALSE
;
2539 // Check every instance of the variable
2540 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
2541 // partial partition boot option. Second, check whether the instance could be connected.
2543 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
2544 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
2546 // Connect the device path instance, the device path point to hard drive media device path node
2547 // e.g. ACPI() /PCI()/ATA()/Partition()
2549 Status
= BdsLibConnectDevicePath (Instance
);
2550 if (!EFI_ERROR (Status
)) {
2556 // Come here means the first instance is not matched
2560 } while (TempNewDevicePath
!= NULL
);
2564 // Find the matched device path.
2565 // Append the file path information from the boot option and return the fully expanded device path.
2567 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2568 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
2571 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.
2575 // First delete the matched instance.
2577 TempNewDevicePath
= CachedDevicePath
;
2578 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
2579 FreePool (TempNewDevicePath
);
2582 // Second, append the remaining path after the matched instance
2584 TempNewDevicePath
= CachedDevicePath
;
2585 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
2586 FreePool (TempNewDevicePath
);
2588 // Save the matching Device Path so we don't need to do a connect all next time
2589 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2591 Status
= gRT
->SetVariable (
2592 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2593 &gHdBootDevicePathVariablGuid
,
2594 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2595 GetDevicePathSize (CachedDevicePath
),
2600 FreePool (Instance
);
2601 FreePool (CachedDevicePath
);
2602 return FullDevicePath
;
2607 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need
2608 // to search all devices in the system for a matched partition
2610 BdsLibConnectAllDriversToAllControllers ();
2611 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
2612 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
2614 // If there was an error or there are no device handles that support
2615 // the BLOCK_IO Protocol, then return.
2620 // Loop through all the device handles that support the BLOCK_IO Protocol
2622 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
2624 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
2625 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
2629 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
2631 // Find the matched partition device path
2633 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2634 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
2637 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2639 if (CachedDevicePath
!= NULL
) {
2641 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2643 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
2644 TempNewDevicePath
= CachedDevicePath
;
2645 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
2646 FreePool(TempNewDevicePath
);
2649 if (CachedDevicePath
!= NULL
) {
2650 TempNewDevicePath
= CachedDevicePath
;
2651 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
2652 FreePool(TempNewDevicePath
);
2654 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2658 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
2659 // If the user try to boot many OS in different HDs or partitions, in theory,
2660 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.
2663 ASSERT (CachedDevicePath
!= NULL
);
2664 TempNewDevicePath
= CachedDevicePath
;
2665 while (!IsDevicePathEnd (TempNewDevicePath
)) {
2666 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2668 // Parse one instance
2670 while (!IsDevicePathEndType (TempNewDevicePath
)) {
2671 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2675 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
2677 if (InstanceNum
>= 12) {
2678 SetDevicePathEndNode (TempNewDevicePath
);
2683 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2687 // Save the matching Device Path so we don't need to do a connect all next time
2688 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2690 Status
= gRT
->SetVariable (
2691 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2692 &gHdBootDevicePathVariablGuid
,
2693 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2694 GetDevicePathSize (CachedDevicePath
),
2702 if (CachedDevicePath
!= NULL
) {
2703 FreePool (CachedDevicePath
);
2705 if (BlockIoBuffer
!= NULL
) {
2706 FreePool (BlockIoBuffer
);
2708 return FullDevicePath
;
2712 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
2713 instances, has the same partition node with HardDriveDevicePath device path
2715 @param BlockIoDevicePath Multi device path instances which need to check
2716 @param HardDriveDevicePath A device path which starts with a hard drive media
2719 @retval TRUE There is a matched device path instance.
2720 @retval FALSE There is no matched device path instance.
2725 MatchPartitionDevicePathNode (
2726 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
2727 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2730 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
2731 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2733 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
2735 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
2740 // Make PreviousDevicePath == the device path node before the end node
2742 DevicePath
= BlockIoDevicePath
;
2743 BlockIoHdDevicePathNode
= NULL
;
2746 // find the partition device path node
2748 while (!IsDevicePathEnd (DevicePath
)) {
2749 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2750 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
2752 BlockIoHdDevicePathNode
= DevicePath
;
2756 DevicePath
= NextDevicePathNode (DevicePath
);
2759 if (BlockIoHdDevicePathNode
== NULL
) {
2763 // See if the harddrive device path in blockio matches the orig Hard Drive Node
2765 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
2769 // Check for the match
2771 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
2772 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
2773 switch (TmpHdPath
->SignatureType
) {
2774 case SIGNATURE_TYPE_GUID
:
2775 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
2777 case SIGNATURE_TYPE_MBR
:
2778 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
2790 Delete the boot option associated with the handle passed in.
2792 @param Handle The handle which present the device path to create
2795 @retval EFI_SUCCESS Delete the boot option success
2796 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2797 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2798 @retval Other Error return value from SetVariable()
2802 BdsLibDeleteOptionFromHandle (
2803 IN EFI_HANDLE Handle
2807 UINT8
*BootOptionVar
;
2808 UINTN BootOrderSize
;
2809 UINTN BootOptionSize
;
2812 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2813 UINTN DevicePathSize
;
2814 UINTN OptionDevicePathSize
;
2815 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2816 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2819 Status
= EFI_SUCCESS
;
2824 // Check "BootOrder" variable, if no, means there is no any boot order.
2826 BootOrder
= BdsLibGetVariableAndSize (
2828 &gEfiGlobalVariableGuid
,
2831 if (BootOrder
== NULL
) {
2832 return EFI_NOT_FOUND
;
2836 // Convert device handle to device path protocol instance
2838 DevicePath
= DevicePathFromHandle (Handle
);
2839 if (DevicePath
== NULL
) {
2840 return EFI_NOT_FOUND
;
2842 DevicePathSize
= GetDevicePathSize (DevicePath
);
2845 // Loop all boot order variable and find the matching device path
2848 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2849 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2850 BootOptionVar
= BdsLibGetVariableAndSize (
2852 &gEfiGlobalVariableGuid
,
2856 if (BootOptionVar
== NULL
) {
2857 FreePool (BootOrder
);
2858 return EFI_OUT_OF_RESOURCES
;
2861 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2862 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2863 FreePool (BootOptionVar
);
2868 TempPtr
= BootOptionVar
;
2869 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
2870 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
2871 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
2872 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
2875 // Check whether the device path match
2877 if ((OptionDevicePathSize
== DevicePathSize
) &&
2878 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
2879 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2880 FreePool (BootOptionVar
);
2884 FreePool (BootOptionVar
);
2889 // Adjust number of boot option for "BootOrder" variable.
2891 Status
= gRT
->SetVariable (
2893 &gEfiGlobalVariableGuid
,
2894 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2899 // Shrinking variable with existing variable implementation shouldn't fail.
2901 ASSERT_EFI_ERROR (Status
);
2903 FreePool (BootOrder
);
2910 Delete all invalid EFI boot options.
2912 @retval EFI_SUCCESS Delete all invalid boot option success
2913 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
2914 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2915 @retval Other Error return value from SetVariable()
2919 BdsDeleteAllInvalidEfiBootOption (
2924 UINT8
*BootOptionVar
;
2925 UINTN BootOrderSize
;
2926 UINTN BootOptionSize
;
2930 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2931 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2933 CHAR16
*Description
;
2936 Status
= EFI_SUCCESS
;
2939 OptionDevicePath
= NULL
;
2944 // Check "BootOrder" variable firstly, this variable hold the number of boot options
2946 BootOrder
= BdsLibGetVariableAndSize (
2948 &gEfiGlobalVariableGuid
,
2951 if (NULL
== BootOrder
) {
2952 return EFI_NOT_FOUND
;
2956 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2957 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2958 BootOptionVar
= BdsLibGetVariableAndSize (
2960 &gEfiGlobalVariableGuid
,
2963 if (NULL
== BootOptionVar
) {
2964 FreePool (BootOrder
);
2965 return EFI_OUT_OF_RESOURCES
;
2968 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2971 TempPtr
= BootOptionVar
;
2972 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
2973 Description
= (CHAR16
*) TempPtr
;
2974 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
2975 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
2978 // Skip legacy boot option (BBS boot device)
2980 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
2981 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
2982 FreePool (BootOptionVar
);
2988 if (Corrupted
|| !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
2990 // Delete this invalid boot option "Boot####"
2992 Status
= gRT
->SetVariable (
2994 &gEfiGlobalVariableGuid
,
2995 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3000 // Deleting variable with current variable implementation shouldn't fail.
3002 ASSERT_EFI_ERROR (Status
);
3004 // Mark this boot option in boot order as deleted
3006 BootOrder
[Index
] = 0xffff;
3010 FreePool (BootOptionVar
);
3015 // Adjust boot order array
3018 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
3019 if (BootOrder
[Index
] != 0xffff) {
3020 BootOrder
[Index2
] = BootOrder
[Index
];
3024 Status
= gRT
->SetVariable (
3026 &gEfiGlobalVariableGuid
,
3027 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3028 Index2
* sizeof (UINT16
),
3032 // Shrinking variable with current variable implementation shouldn't fail.
3034 ASSERT_EFI_ERROR (Status
);
3036 FreePool (BootOrder
);
3043 For EFI boot option, BDS separate them as six types:
3044 1. Network - The boot option points to the SimpleNetworkProtocol device.
3045 Bds will try to automatically create this type boot option when enumerate.
3046 2. Shell - The boot option points to internal flash shell.
3047 Bds will try to automatically create this type boot option when enumerate.
3048 3. Removable BlockIo - The boot option only points to the removable media
3049 device, like USB flash disk, DVD, Floppy etc.
3050 These device should contain a *removable* blockIo
3051 protocol in their device handle.
3052 Bds will try to automatically create this type boot option
3054 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
3056 These device should contain a *fixed* blockIo
3057 protocol in their device handle.
3058 BDS will skip fixed blockIo devices, and NOT
3059 automatically create boot option for them. But BDS
3060 will help to delete those fixed blockIo boot option,
3061 whose description rule conflict with other auto-created
3063 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
3064 has SimpleFileSystem Protocol, but has no blockio
3065 protocol. These devices do not offer blockIo
3066 protocol, but BDS still can get the
3067 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
3069 6. File - The boot option points to a file. These boot options are usually
3070 created by user manually or OS loader. BDS will not delete or modify
3073 This function will enumerate all possible boot device in the system, and
3074 automatically create boot options for Network, Shell, Removable BlockIo,
3075 and Non-BlockIo Simplefile devices.
3076 It will only execute once of every boot.
3078 @param BdsBootOptionList The header of the link list which indexed all
3079 current boot options
3081 @retval EFI_SUCCESS Finished all the boot device enumerate and create
3082 the boot option base on that boot device
3084 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
3088 BdsLibEnumerateAllBootOption (
3089 IN OUT LIST_ENTRY
*BdsBootOptionList
3093 UINT16 FloppyNumber
;
3094 UINT16 HarddriveNumber
;
3099 UINT16 NonBlockNumber
;
3100 UINTN NumberBlockIoHandles
;
3101 EFI_HANDLE
*BlockIoHandles
;
3102 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
3103 BOOLEAN Removable
[2];
3104 UINTN RemovableIndex
;
3106 UINTN NumOfLoadFileHandles
;
3107 EFI_HANDLE
*LoadFileHandles
;
3108 UINTN FvHandleCount
;
3109 EFI_HANDLE
*FvHandleBuffer
;
3110 EFI_FV_FILETYPE Type
;
3112 EFI_FV_FILE_ATTRIBUTES Attributes
;
3113 UINT32 AuthenticationStatus
;
3114 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
3115 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3116 UINTN DevicePathType
;
3118 EFI_HANDLE
*FileSystemHandles
;
3119 UINTN NumberFileSystemHandles
;
3121 EFI_IMAGE_DOS_HEADER DosHeader
;
3124 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3125 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3128 HarddriveNumber
= 0;
3135 ZeroMem (Buffer
, sizeof (Buffer
));
3138 // If the boot device enumerate happened, just get the boot
3139 // device from the boot order variable
3141 if (mEnumBootDevice
) {
3142 GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME
, &gLastEnumLangGuid
, (VOID
**)&LastLang
, NULL
);
3143 GetEfiGlobalVariable2 (L
"PlatformLang", (VOID
**)&PlatLang
, NULL
);
3144 ASSERT (PlatLang
!= NULL
);
3145 if ((LastLang
!= NULL
) && (AsciiStrCmp (LastLang
, PlatLang
) == 0)) {
3146 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3147 FreePool (LastLang
);
3148 FreePool (PlatLang
);
3151 Status
= gRT
->SetVariable (
3152 LAST_ENUM_LANGUAGE_VARIABLE_NAME
,
3154 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3155 AsciiStrSize (PlatLang
),
3159 // Failure to set the variable only impacts the performance next time enumerating the boot options.
3162 if (LastLang
!= NULL
) {
3163 FreePool (LastLang
);
3165 FreePool (PlatLang
);
3170 // Notes: this dirty code is to get the legacy boot option from the
3171 // BBS table and create to variable as the EFI boot option, it should
3172 // be removed after the CSM can provide legacy boot option directly
3174 REFRESH_LEGACY_BOOT_OPTIONS
;
3177 // Delete invalid boot option
3179 BdsDeleteAllInvalidEfiBootOption ();
3182 // Parse removable media followed by fixed media.
3183 // The Removable[] array is used by the for-loop below to create removable media boot options
3184 // at first, and then to create fixed media boot options.
3186 Removable
[0] = FALSE
;
3187 Removable
[1] = TRUE
;
3189 gBS
->LocateHandleBuffer (
3191 &gEfiBlockIoProtocolGuid
,
3193 &NumberBlockIoHandles
,
3197 for (RemovableIndex
= 0; RemovableIndex
< 2; RemovableIndex
++) {
3198 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
3199 Status
= gBS
->HandleProtocol (
3200 BlockIoHandles
[Index
],
3201 &gEfiBlockIoProtocolGuid
,
3205 // skip the fixed block io then the removable block io
3207 if (EFI_ERROR (Status
) || (BlkIo
->Media
->RemovableMedia
== Removable
[RemovableIndex
])) {
3210 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
3211 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
3213 switch (DevicePathType
) {
3214 case BDS_EFI_ACPI_FLOPPY_BOOT
:
3215 if (FloppyNumber
!= 0) {
3216 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
3218 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
3220 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3225 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.
3227 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
3228 case BDS_EFI_MESSAGE_SATA_BOOT
:
3229 if (BlkIo
->Media
->RemovableMedia
) {
3230 if (CdromNumber
!= 0) {
3231 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
3233 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
3237 if (HarddriveNumber
!= 0) {
3238 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)), HarddriveNumber
);
3240 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)));
3244 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
3245 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3248 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
3249 if (UsbNumber
!= 0) {
3250 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
3252 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
3254 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3258 case BDS_EFI_MESSAGE_SCSI_BOOT
:
3259 if (ScsiNumber
!= 0) {
3260 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
3262 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
3264 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3268 case BDS_EFI_MESSAGE_MISC_BOOT
:
3269 if (MiscNumber
!= 0) {
3270 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
3272 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
3274 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3284 if (NumberBlockIoHandles
!= 0) {
3285 FreePool (BlockIoHandles
);
3289 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
3292 gBS
->LocateHandleBuffer (
3294 &gEfiSimpleFileSystemProtocolGuid
,
3296 &NumberFileSystemHandles
,
3299 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
3300 Status
= gBS
->HandleProtocol (
3301 FileSystemHandles
[Index
],
3302 &gEfiBlockIoProtocolGuid
,
3305 if (!EFI_ERROR (Status
)) {
3307 // Skip if the file system handle supports a BlkIo protocol,
3313 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
3314 // machinename is ia32, ia64, x64, ...
3316 Hdr
.Union
= &HdrData
;
3318 Status
= BdsLibGetImageHeader (
3319 FileSystemHandles
[Index
],
3320 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3324 if (!EFI_ERROR (Status
) &&
3325 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3326 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3332 // No such file or the file is not a EFI application, delete this boot option
3334 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
3336 if (NonBlockNumber
!= 0) {
3337 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
3339 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
3341 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
3346 if (NumberFileSystemHandles
!= 0) {
3347 FreePool (FileSystemHandles
);
3351 // Parse Network Boot Device
3353 NumOfLoadFileHandles
= 0;
3355 // Search Load File protocol for PXE boot option.
3357 gBS
->LocateHandleBuffer (
3359 &gEfiLoadFileProtocolGuid
,
3361 &NumOfLoadFileHandles
,
3365 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
3367 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)), Index
);
3369 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)));
3371 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
3374 if (NumOfLoadFileHandles
!= 0) {
3375 FreePool (LoadFileHandles
);
3379 // Check if we have on flash shell
3381 gBS
->LocateHandleBuffer (
3383 &gEfiFirmwareVolume2ProtocolGuid
,
3388 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
3389 gBS
->HandleProtocol (
3390 FvHandleBuffer
[Index
],
3391 &gEfiFirmwareVolume2ProtocolGuid
,
3395 Status
= Fv
->ReadFile (
3397 PcdGetPtr(PcdShellFile
),
3402 &AuthenticationStatus
3404 if (EFI_ERROR (Status
)) {
3406 // Skip if no shell file in the FV
3411 // Build the shell boot option
3413 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
3416 if (FvHandleCount
!= 0) {
3417 FreePool (FvHandleBuffer
);
3420 // Make sure every boot only have one time
3421 // boot device enumerate
3423 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3424 mEnumBootDevice
= TRUE
;
3430 Build the boot option with the handle parsed in
3432 @param Handle The handle which present the device path to create
3434 @param BdsBootOptionList The header of the link list which indexed all
3435 current boot options
3436 @param String The description of the boot option.
3441 BdsLibBuildOptionFromHandle (
3442 IN EFI_HANDLE Handle
,
3443 IN LIST_ENTRY
*BdsBootOptionList
,
3447 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3449 DevicePath
= DevicePathFromHandle (Handle
);
3452 // Create and register new boot option
3454 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
3459 Build the on flash shell boot option with the handle parsed in.
3461 @param Handle The handle which present the device path to create
3462 on flash shell boot option
3463 @param BdsBootOptionList The header of the link list which indexed all
3464 current boot options
3469 BdsLibBuildOptionFromShell (
3470 IN EFI_HANDLE Handle
,
3471 IN OUT LIST_ENTRY
*BdsBootOptionList
3474 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3475 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
3477 DevicePath
= DevicePathFromHandle (Handle
);
3480 // Build the shell device path
3482 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
3484 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
3487 // Create and register the shell boot option
3489 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
3494 Boot from the UEFI spec defined "BootNext" variable.
3507 BDS_COMMON_OPTION
*BootOption
;
3508 LIST_ENTRY TempList
;
3513 // Init the boot option name buffer and temp link list
3515 InitializeListHead (&TempList
);
3516 ZeroMem (Buffer
, sizeof (Buffer
));
3518 BootNext
= BdsLibGetVariableAndSize (
3520 &gEfiGlobalVariableGuid
,
3525 // Clear the boot next variable first
3527 if (BootNext
!= NULL
) {
3528 Status
= gRT
->SetVariable (
3530 &gEfiGlobalVariableGuid
,
3531 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3536 // Deleting variable with current variable implementation shouldn't fail.
3538 ASSERT_EFI_ERROR (Status
);
3541 // Start to build the boot option and try to boot
3543 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
3544 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
3545 ASSERT (BootOption
!= NULL
);
3546 BdsLibConnectDevicePath (BootOption
->DevicePath
);
3547 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
3553 Return the bootable media handle.
3554 First, check the device is connected
3555 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
3556 Third, detect the the default boot file in the Media, and return the removable Media handle.
3558 @param DevicePath Device Path to a bootable device
3560 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
3565 BdsLibGetBootableHandle (
3566 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3571 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3572 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
3574 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
3576 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3579 EFI_HANDLE ReturnHandle
;
3580 EFI_HANDLE
*SimpleFileSystemHandles
;
3582 UINTN NumberSimpleFileSystemHandles
;
3584 EFI_IMAGE_DOS_HEADER DosHeader
;
3585 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3586 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3588 UpdatedDevicePath
= DevicePath
;
3591 // Enter to critical section to protect the acquired BlockIo instance
3592 // from getting released due to the USB mass storage hotplug event
3594 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
3597 // Check whether the device is connected
3599 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3600 if (EFI_ERROR (Status
)) {
3602 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
3604 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3605 if (EFI_ERROR (Status
)) {
3607 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
3609 UpdatedDevicePath
= DevicePath
;
3610 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3611 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3615 // For removable device boot option, its contained device path only point to the removable device handle,
3616 // should make sure all its children handles (its child partion or media handles) are created and connected.
3618 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3620 // Get BlockIo protocol and check removable attribute
3622 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
3623 ASSERT_EFI_ERROR (Status
);
3626 // Issue a dummy read to the device to check for media change.
3627 // When the removable media is changed, any Block IO read/write will
3628 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
3629 // returned. After the Block IO protocol is reinstalled, subsequent
3630 // Block IO read/write will success.
3632 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
3633 if (Buffer
!= NULL
) {
3634 BlockIo
->ReadBlocks (
3636 BlockIo
->Media
->MediaId
,
3638 BlockIo
->Media
->BlockSize
,
3646 // Detect the the default boot file from removable Media
3650 // 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
3651 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
3653 DupDevicePath
= DuplicateDevicePath (DevicePath
);
3654 ASSERT (DupDevicePath
!= NULL
);
3656 UpdatedDevicePath
= DupDevicePath
;
3657 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3659 // 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
3660 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
3662 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
3663 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
3665 // Remove the usb node, let the device path only point to PCI node
3667 SetDevicePathEndNode (UpdatedDevicePath
);
3668 UpdatedDevicePath
= DupDevicePath
;
3670 UpdatedDevicePath
= DevicePath
;
3674 // Get the device path size of boot option
3676 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3677 ReturnHandle
= NULL
;
3678 gBS
->LocateHandleBuffer (
3680 &gEfiSimpleFileSystemProtocolGuid
,
3682 &NumberSimpleFileSystemHandles
,
3683 &SimpleFileSystemHandles
3685 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
3687 // Get the device path size of SimpleFileSystem handle
3689 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
3690 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3692 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
3694 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
3696 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
3697 // machinename is ia32, ia64, x64, ...
3699 Hdr
.Union
= &HdrData
;
3700 Status
= BdsLibGetImageHeader (
3701 SimpleFileSystemHandles
[Index
],
3702 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3706 if (!EFI_ERROR (Status
) &&
3707 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3708 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3709 ReturnHandle
= SimpleFileSystemHandles
[Index
];
3715 FreePool(DupDevicePath
);
3717 if (SimpleFileSystemHandles
!= NULL
) {
3718 FreePool(SimpleFileSystemHandles
);
3721 gBS
->RestoreTPL (OldTpl
);
3723 return ReturnHandle
;
3727 Check to see if the network cable is plugged in. If the DevicePath is not
3728 connected it will be connected.
3730 @param DevicePath Device Path to check
3732 @retval TRUE DevicePath points to an Network that is connected
3733 @retval FALSE DevicePath does not point to a bootable network
3737 BdsLibNetworkBootWithMediaPresent (
3738 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3742 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3744 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
3745 BOOLEAN MediaPresent
;
3746 UINT32 InterruptStatus
;
3748 MediaPresent
= FALSE
;
3750 UpdatedDevicePath
= DevicePath
;
3752 // Locate Load File Protocol for PXE boot option first
3754 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3755 if (EFI_ERROR (Status
)) {
3757 // Device not present so see if we need to connect it
3759 Status
= BdsLibConnectDevicePath (DevicePath
);
3760 if (!EFI_ERROR (Status
)) {
3762 // This one should work after we did the connect
3764 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3768 if (!EFI_ERROR (Status
)) {
3769 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
3770 if (EFI_ERROR (Status
)) {
3772 // Failed to open SNP from this handle, try to get SNP from parent handle
3774 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
3775 if (UpdatedDevicePath
!= NULL
) {
3776 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3777 if (!EFI_ERROR (Status
)) {
3779 // SNP handle found, get SNP from it
3781 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
3786 if (!EFI_ERROR (Status
)) {
3787 if (Snp
->Mode
->MediaPresentSupported
) {
3788 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
3790 // Invoke Snp->GetStatus() to refresh the media status
3792 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
3795 // In case some one else is using the SNP check to see if it's connected
3797 MediaPresent
= Snp
->Mode
->MediaPresent
;
3800 // No one is using SNP so we need to Start and Initialize so
3801 // MediaPresent will be valid.
3803 Status
= Snp
->Start (Snp
);
3804 if (!EFI_ERROR (Status
)) {
3805 Status
= Snp
->Initialize (Snp
, 0, 0);
3806 if (!EFI_ERROR (Status
)) {
3807 MediaPresent
= Snp
->Mode
->MediaPresent
;
3808 Snp
->Shutdown (Snp
);
3814 MediaPresent
= TRUE
;
3819 return MediaPresent
;
3823 For a bootable Device path, return its boot type.
3825 @param DevicePath The bootable device Path to check
3827 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3828 which subtype is MEDIA_HARDDRIVE_DP
3829 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3830 which subtype is MEDIA_CDROM_DP
3831 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
3832 which HID is floppy device.
3833 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3834 and its last device path node's subtype is MSG_ATAPI_DP.
3835 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3836 and its last device path node's subtype is MSG_SCSI_DP.
3837 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3838 and its last device path node's subtype is MSG_USB_DP.
3839 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
3840 its last device path node point to a message device path node.
3841 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
3842 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
3847 BdsGetBootTypeFromDevicePath (
3848 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3851 ACPI_HID_DEVICE_PATH
*Acpi
;
3852 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3853 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
3856 if (NULL
== DevicePath
) {
3857 return BDS_EFI_UNSUPPORT
;
3860 TempDevicePath
= DevicePath
;
3862 while (!IsDevicePathEndType (TempDevicePath
)) {
3863 switch (DevicePathType (TempDevicePath
)) {
3864 case BBS_DEVICE_PATH
:
3865 return BDS_LEGACY_BBS_BOOT
;
3866 case MEDIA_DEVICE_PATH
:
3867 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
3868 return BDS_EFI_MEDIA_HD_BOOT
;
3869 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
3870 return BDS_EFI_MEDIA_CDROM_BOOT
;
3873 case ACPI_DEVICE_PATH
:
3874 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
3875 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
3876 return BDS_EFI_ACPI_FLOPPY_BOOT
;
3879 case MESSAGING_DEVICE_PATH
:
3881 // Get the last device path node
3883 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
3884 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
3886 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
3889 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
3892 // if the device path not only point to driver device, it is not a messaging device path,
3894 if (!IsDevicePathEndType (LastDeviceNode
)) {
3898 switch (DevicePathSubType (TempDevicePath
)) {
3900 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
3904 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
3908 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
3912 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
3915 case MSG_MAC_ADDR_DP
:
3919 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
3923 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
3931 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
3934 return BDS_EFI_UNSUPPORT
;
3938 Check whether the Device path in a boot option point to a valid bootable device,
3939 And if CheckMedia is true, check the device is ready to boot now.
3941 @param DevPath the Device path in a boot option
3942 @param CheckMedia if true, check the device is ready to boot now.
3944 @retval TRUE the Device path is valid
3945 @retval FALSE the Device path is invalid .
3950 BdsLibIsValidEFIBootOptDevicePath (
3951 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
3952 IN BOOLEAN CheckMedia
3955 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
3959 Check whether the Device path in a boot option point to a valid bootable device,
3960 And if CheckMedia is true, check the device is ready to boot now.
3961 If Description is not NULL and the device path point to a fixed BlockIo
3962 device, check the description whether conflict with other auto-created
3965 @param DevPath the Device path in a boot option
3966 @param CheckMedia if true, check the device is ready to boot now.
3967 @param Description the description in a boot option
3969 @retval TRUE the Device path is valid
3970 @retval FALSE the Device path is invalid .
3975 BdsLibIsValidEFIBootOptDevicePathExt (
3976 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
3977 IN BOOLEAN CheckMedia
,
3978 IN CHAR16
*Description
3983 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3984 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
3985 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
3987 TempDevicePath
= DevPath
;
3988 LastDeviceNode
= DevPath
;
3991 // Check if it's a valid boot option for network boot device.
3992 // Check if there is EfiLoadFileProtocol installed.
3993 // If yes, that means there is a boot option for network.
3995 Status
= gBS
->LocateDevicePath (
3996 &gEfiLoadFileProtocolGuid
,
4000 if (EFI_ERROR (Status
)) {
4002 // Device not present so see if we need to connect it
4004 TempDevicePath
= DevPath
;
4005 BdsLibConnectDevicePath (TempDevicePath
);
4006 Status
= gBS
->LocateDevicePath (
4007 &gEfiLoadFileProtocolGuid
,
4013 if (!EFI_ERROR (Status
)) {
4014 if (!IsDevicePathEnd (TempDevicePath
)) {
4016 // LoadFile protocol is not installed on handle with exactly the same DevPath
4023 // Test if it is ready to boot now
4025 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
4034 // If the boot option point to a file, it is a valid EFI boot option,
4035 // and assume it is ready to boot now
4037 while (!IsDevicePathEnd (TempDevicePath
)) {
4039 // If there is USB Class or USB WWID device path node, treat it as valid EFI
4040 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it
4041 // to full device path.
4043 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
4044 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
4045 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
4049 LastDeviceNode
= TempDevicePath
;
4050 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4052 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
4053 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
4058 // Check if it's a valid boot option for internal FV application
4060 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
4062 // If the boot option point to internal FV application, make sure it is valid
4064 TempDevicePath
= DevPath
;
4065 Status
= BdsLibUpdateFvFileDevicePath (
4067 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
)
4069 if (Status
== EFI_ALREADY_STARTED
) {
4072 if (Status
== EFI_SUCCESS
) {
4073 FreePool (TempDevicePath
);
4080 // If the boot option point to a blockIO device:
4081 // if it is a removable blockIo device, it is valid.
4082 // if it is a fixed blockIo device, check its description confliction.
4084 TempDevicePath
= DevPath
;
4085 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4086 if (EFI_ERROR (Status
)) {
4088 // Device not present so see if we need to connect it
4090 Status
= BdsLibConnectDevicePath (DevPath
);
4091 if (!EFI_ERROR (Status
)) {
4093 // Try again to get the Block Io protocol after we did the connect
4095 TempDevicePath
= DevPath
;
4096 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4100 if (!EFI_ERROR (Status
)) {
4101 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
4102 if (!EFI_ERROR (Status
)) {
4105 // Test if it is ready to boot now
4107 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4116 // 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,
4118 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
4119 if (!EFI_ERROR (Status
)) {
4122 // Test if it is ready to boot now
4124 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4138 According to a file guild, check a Fv file device path is valid. If it is invalid,
4139 try to return the valid device path.
4140 FV address maybe changes for memory layout adjust from time to time, use this function
4141 could promise the Fv file device path is right.
4143 @param DevicePath on input, the Fv file device path need to check on
4144 output, the updated valid Fv file device path
4145 @param FileGuid the Fv file guild
4147 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
4149 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
4151 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
4153 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
4154 and return the updated device path in DevicePath
4159 BdsLibUpdateFvFileDevicePath (
4160 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
4161 IN EFI_GUID
*FileGuid
4164 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4165 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4167 EFI_GUID
*GuidPoint
;
4169 UINTN FvHandleCount
;
4170 EFI_HANDLE
*FvHandleBuffer
;
4171 EFI_FV_FILETYPE Type
;
4173 EFI_FV_FILE_ATTRIBUTES Attributes
;
4174 UINT32 AuthenticationStatus
;
4176 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
4177 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
4178 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
4179 EFI_HANDLE FoundFvHandle
;
4180 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
4182 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
4183 return EFI_INVALID_PARAMETER
;
4185 if (FileGuid
== NULL
) {
4186 return EFI_INVALID_PARAMETER
;
4190 // Check whether the device path point to the default the input Fv file
4192 TempDevicePath
= *DevicePath
;
4193 LastDeviceNode
= TempDevicePath
;
4194 while (!IsDevicePathEnd (TempDevicePath
)) {
4195 LastDeviceNode
= TempDevicePath
;
4196 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4198 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
4199 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
4201 if (GuidPoint
== NULL
) {
4203 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
4205 return EFI_UNSUPPORTED
;
4207 if (!CompareGuid (GuidPoint
, FileGuid
)) {
4209 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
4211 return EFI_UNSUPPORTED
;
4215 // Check whether the input Fv file device path is valid
4217 TempDevicePath
= *DevicePath
;
4218 FoundFvHandle
= NULL
;
4219 Status
= gBS
->LocateDevicePath (
4220 &gEfiFirmwareVolume2ProtocolGuid
,
4224 if (!EFI_ERROR (Status
)) {
4225 Status
= gBS
->HandleProtocol (
4227 &gEfiFirmwareVolume2ProtocolGuid
,
4230 if (!EFI_ERROR (Status
)) {
4232 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
4234 Status
= Fv
->ReadFile (
4241 &AuthenticationStatus
4243 if (!EFI_ERROR (Status
)) {
4244 return EFI_ALREADY_STARTED
;
4250 // Look for the input wanted FV file in current FV
4251 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
4254 FoundFvHandle
= NULL
;
4255 Status
= gBS
->HandleProtocol (
4257 &gEfiLoadedImageProtocolGuid
,
4258 (VOID
**) &LoadedImage
4260 if (!EFI_ERROR (Status
)) {
4261 Status
= gBS
->HandleProtocol (
4262 LoadedImage
->DeviceHandle
,
4263 &gEfiFirmwareVolume2ProtocolGuid
,
4266 if (!EFI_ERROR (Status
)) {
4267 Status
= Fv
->ReadFile (
4274 &AuthenticationStatus
4276 if (!EFI_ERROR (Status
)) {
4278 FoundFvHandle
= LoadedImage
->DeviceHandle
;
4283 // Second, if fail to find, try to enumerate all FV
4286 FvHandleBuffer
= NULL
;
4287 gBS
->LocateHandleBuffer (
4289 &gEfiFirmwareVolume2ProtocolGuid
,
4294 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
4295 gBS
->HandleProtocol (
4296 FvHandleBuffer
[Index
],
4297 &gEfiFirmwareVolume2ProtocolGuid
,
4301 Status
= Fv
->ReadFile (
4308 &AuthenticationStatus
4310 if (EFI_ERROR (Status
)) {
4312 // Skip if input Fv file not in the FV
4317 FoundFvHandle
= FvHandleBuffer
[Index
];
4321 if (FvHandleBuffer
!= NULL
) {
4322 FreePool (FvHandleBuffer
);
4328 // Build the shell device path
4330 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
4331 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
4332 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
4333 *DevicePath
= NewDevicePath
;
4336 return EFI_NOT_FOUND
;