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 LegacyBios
->GetBbsInfo (
532 BootOrder
= BdsLibGetVariableAndSize (
534 &gEfiGlobalVariableGuid
,
537 if (BootOrder
== NULL
) {
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 if (BootOrder
!= NULL
) {
638 FreePool (BootOrder
);
645 Find all legacy boot option by device type.
647 @param BootOrder The boot order array.
648 @param BootOptionNum The number of boot option.
649 @param DevType Device type.
650 @param DevName Device name.
651 @param Attribute The boot option attribute.
652 @param BbsIndex The BBS table index.
653 @param OptionNumber The boot option index.
655 @retval TRUE The Legacy boot option is found.
656 @retval FALSE The legacy boot option is not found.
660 BdsFindLegacyBootOptionByDevTypeAndName (
661 IN UINT16
*BootOrder
,
662 IN UINTN BootOptionNum
,
665 OUT UINT32
*Attribute
,
666 OUT UINT16
*BbsIndex
,
667 OUT UINT16
*OptionNumber
671 CHAR16 BootOption
[9];
672 UINTN BootOptionSize
;
673 UINT8
*BootOptionVar
;
680 if (NULL
== BootOrder
) {
685 // Loop all boot option from variable
687 for (Index
= 0; Index
< BootOptionNum
; Index
++) {
688 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", (UINTN
) BootOrder
[Index
]);
689 BootOptionVar
= BdsLibGetVariableAndSize (
691 &gEfiGlobalVariableGuid
,
694 if (NULL
== BootOptionVar
) {
699 // Skip Non-legacy boot option
701 if (!BdsIsLegacyBootOption (BootOptionVar
, &BbsEntry
, BbsIndex
)) {
702 FreePool (BootOptionVar
);
707 (BbsEntry
->DeviceType
!= DevType
) ||
708 (StrCmp (DevName
, (CHAR16
*)(BootOptionVar
+ sizeof (UINT32
) + sizeof (UINT16
))) != 0)
710 FreePool (BootOptionVar
);
714 *Attribute
= *(UINT32
*) BootOptionVar
;
715 *OptionNumber
= BootOrder
[Index
];
717 FreePool (BootOptionVar
);
725 Create a legacy boot option.
727 @param BbsItem The BBS Table entry.
728 @param Index Index of the specified entry in BBS table.
729 @param BootOrderList The boot order list.
730 @param BootOrderListSize The size of boot order list.
732 @retval EFI_OUT_OF_RESOURCE No enough memory.
733 @retval EFI_SUCCESS The function complete successfully.
734 @return Other value if the legacy boot option is not created.
738 BdsCreateOneLegacyBootOption (
739 IN BBS_TABLE
*BbsItem
,
741 IN OUT UINT16
**BootOrderList
,
742 IN OUT UINTN
*BootOrderListSize
745 BBS_BBS_DEVICE_PATH BbsDevPathNode
;
747 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
752 // Create device path node.
754 BbsDevPathNode
.Header
.Type
= BBS_DEVICE_PATH
;
755 BbsDevPathNode
.Header
.SubType
= BBS_BBS_DP
;
756 SetDevicePathNodeLength (&BbsDevPathNode
.Header
, sizeof (BBS_BBS_DEVICE_PATH
));
757 BbsDevPathNode
.DeviceType
= BbsItem
->DeviceType
;
758 CopyMem (&BbsDevPathNode
.StatusFlag
, &BbsItem
->StatusFlags
, sizeof (UINT16
));
760 DevPath
= AppendDevicePathNode (
762 (EFI_DEVICE_PATH_PROTOCOL
*) &BbsDevPathNode
764 if (NULL
== DevPath
) {
765 return EFI_OUT_OF_RESOURCES
;
768 Status
= BdsCreateLegacyBootOption (
775 BbsItem
->BootPriority
= 0x00;
783 Add the legacy boot options from BBS table if they do not exist.
785 @retval EFI_SUCCESS The boot options are added successfully
786 or they are already in boot options.
787 @retval EFI_NOT_FOUND No legacy boot options is found.
788 @retval EFI_OUT_OF_RESOURCE No enough memory.
789 @return Other value LegacyBoot options are not added.
793 BdsAddNonExistingLegacyBootOptions (
803 HDD_INFO
*LocalHddInfo
;
804 BBS_TABLE
*LocalBbsTable
;
806 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
815 LocalBbsTable
= NULL
;
817 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
818 if (EFI_ERROR (Status
)) {
822 LegacyBios
->GetBbsInfo (
830 BootOrder
= BdsLibGetVariableAndSize (
832 &gEfiGlobalVariableGuid
,
835 if (BootOrder
== NULL
) {
839 for (Index
= 0; Index
< BbsCount
; Index
++) {
840 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
841 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
846 BdsBuildLegacyDevNameString (&LocalBbsTable
[Index
], Index
, sizeof (Desc
), Desc
);
848 Exist
= BdsFindLegacyBootOptionByDevTypeAndName (
850 BootOrderSize
/ sizeof (UINT16
),
851 LocalBbsTable
[Index
].DeviceType
,
859 // Not found such type of legacy device in boot options or we found but it's disabled
860 // so we have to create one and put it to the tail of boot order list
862 Status
= BdsCreateOneLegacyBootOption (
863 &LocalBbsTable
[Index
],
868 if (!EFI_ERROR (Status
)) {
870 OptionNumber
= BootOrder
[BootOrderSize
/ sizeof (UINT16
) - 1];
874 ASSERT (BbsIndex
== Index
);
877 Status
= gRT
->SetVariable (
879 &gEfiGlobalVariableGuid
,
880 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
884 if (BootOrder
!= NULL
) {
885 FreePool (BootOrder
);
892 Fill the device order buffer.
894 @param BbsTable The BBS table.
895 @param BbsType The BBS Type.
896 @param BbsCount The BBS Count.
897 @param Buf device order buffer.
899 @return The device order buffer.
904 IN BBS_TABLE
*BbsTable
,
912 for (Index
= 0; Index
< BbsCount
; Index
++) {
913 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
917 if (BbsTable
[Index
].DeviceType
!= BbsType
) {
921 *Buf
= (UINT16
) (Index
& 0xFF);
929 Create the device order buffer.
931 @param BbsTable The BBS table.
932 @param BbsCount The BBS Count.
934 @retval EFI_SUCCES The buffer is created and the EFI variable named
935 VAR_LEGACY_DEV_ORDER and gEfiLegacyDevOrderVariableGuid is
937 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
938 @retval EFI_DEVICE_ERROR Fail to add the device order into EFI variable fail
939 because of hardware error.
943 IN BBS_TABLE
*BbsTable
,
955 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
956 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
965 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
967 Status
= EFI_SUCCESS
;
970 // Count all boot devices
972 for (Index
= 0; Index
< BbsCount
; Index
++) {
973 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
977 switch (BbsTable
[Index
].DeviceType
) {
990 case BBS_EMBED_NETWORK
:
1003 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * FDCount
);
1004 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * HDCount
);
1005 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * CDCount
);
1006 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * NETCount
);
1007 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * BEVCount
);
1010 // Create buffer to hold all boot device order
1012 DevOrder
= AllocateZeroPool (TotalSize
);
1013 if (NULL
== DevOrder
) {
1014 return EFI_OUT_OF_RESOURCES
;
1016 DevOrderPtr
= DevOrder
;
1018 DevOrderPtr
->BbsType
= BBS_FLOPPY
;
1019 DevOrderPtr
->Length
= (UINT16
) (sizeof (DevOrderPtr
->Length
) + FDCount
* sizeof (UINT16
));
1020 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_FLOPPY
, BbsCount
, DevOrderPtr
->Data
);
1022 DevOrderPtr
->BbsType
= BBS_HARDDISK
;
1023 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1024 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_HARDDISK
, BbsCount
, DevOrderPtr
->Data
);
1026 DevOrderPtr
->BbsType
= BBS_CDROM
;
1027 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1028 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_CDROM
, BbsCount
, DevOrderPtr
->Data
);
1030 DevOrderPtr
->BbsType
= BBS_EMBED_NETWORK
;
1031 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1032 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_EMBED_NETWORK
, BbsCount
, DevOrderPtr
->Data
);
1034 DevOrderPtr
->BbsType
= BBS_BEV_DEVICE
;
1035 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1036 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_BEV_DEVICE
, BbsCount
, DevOrderPtr
->Data
);
1038 ASSERT (TotalSize
== (UINTN
) ((UINT8
*) DevOrderPtr
- (UINT8
*) DevOrder
));
1041 // Save device order for legacy boot device to variable.
1043 Status
= gRT
->SetVariable (
1044 VAR_LEGACY_DEV_ORDER
,
1045 &gEfiLegacyDevOrderVariableGuid
,
1046 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1050 FreePool (DevOrder
);
1056 Add the legacy boot devices from BBS table into
1057 the legacy device boot order.
1059 @retval EFI_SUCCESS The boot devices are added successfully.
1060 @retval EFI_NOT_FOUND The legacy boot devices are not found.
1061 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
1062 @retval EFI_DEVICE_ERROR Fail to add the legacy device boot order into EFI variable
1063 because of hardware error.
1067 BdsUpdateLegacyDevOrder (
1071 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1072 LEGACY_DEV_ORDER_ENTRY
*NewDevOrder
;
1073 LEGACY_DEV_ORDER_ENTRY
*Ptr
;
1074 LEGACY_DEV_ORDER_ENTRY
*NewPtr
;
1076 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1080 HDD_INFO
*LocalHddInfo
;
1081 BBS_TABLE
*LocalBbsTable
;
1111 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
1119 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1120 if (EFI_ERROR (Status
)) {
1124 Status
= LegacyBios
->GetBbsInfo (
1131 if (EFI_ERROR (Status
)) {
1135 DevOrder
= BdsLibGetVariableAndSize (
1136 VAR_LEGACY_DEV_ORDER
,
1137 &gEfiLegacyDevOrderVariableGuid
,
1140 if (NULL
== DevOrder
) {
1141 return BdsCreateDevOrder (LocalBbsTable
, BbsCount
);
1144 // First we figure out how many boot devices with same device type respectively
1146 for (Index
= 0; Index
< BbsCount
; Index
++) {
1147 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1148 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1153 switch (LocalBbsTable
[Index
].DeviceType
) {
1166 case BBS_EMBED_NETWORK
:
1170 case BBS_BEV_DEVICE
:
1179 TotalSize
+= (HeaderSize
+ FDCount
* sizeof (UINT16
));
1180 TotalSize
+= (HeaderSize
+ HDCount
* sizeof (UINT16
));
1181 TotalSize
+= (HeaderSize
+ CDCount
* sizeof (UINT16
));
1182 TotalSize
+= (HeaderSize
+ NETCount
* sizeof (UINT16
));
1183 TotalSize
+= (HeaderSize
+ BEVCount
* sizeof (UINT16
));
1185 NewDevOrder
= AllocateZeroPool (TotalSize
);
1186 if (NULL
== NewDevOrder
) {
1187 return EFI_OUT_OF_RESOURCES
;
1196 NewPtr
= NewDevOrder
;
1197 NewPtr
->BbsType
= Ptr
->BbsType
;
1198 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + FDCount
* sizeof (UINT16
));
1199 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1200 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1201 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1202 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_FLOPPY
1207 NewPtr
->Data
[FDIndex
] = Ptr
->Data
[Index
];
1210 NewFDPtr
= NewPtr
->Data
;
1215 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1216 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1217 NewPtr
->BbsType
= Ptr
->BbsType
;
1218 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1219 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1220 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1221 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1222 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1223 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_HARDDISK
1228 NewPtr
->Data
[HDIndex
] = Ptr
->Data
[Index
];
1231 NewHDPtr
= NewPtr
->Data
;
1236 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1237 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1238 NewPtr
->BbsType
= Ptr
->BbsType
;
1239 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1240 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1241 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1242 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1243 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1244 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_CDROM
1249 NewPtr
->Data
[CDIndex
] = Ptr
->Data
[Index
];
1252 NewCDPtr
= NewPtr
->Data
;
1257 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1258 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1259 NewPtr
->BbsType
= Ptr
->BbsType
;
1260 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1261 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1262 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1263 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1264 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1265 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_EMBED_NETWORK
1270 NewPtr
->Data
[NETIndex
] = Ptr
->Data
[Index
];
1273 NewNETPtr
= NewPtr
->Data
;
1278 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1279 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1280 NewPtr
->BbsType
= Ptr
->BbsType
;
1281 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1282 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1283 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1284 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1285 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1286 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_BEV_DEVICE
1291 NewPtr
->Data
[BEVIndex
] = Ptr
->Data
[Index
];
1294 NewBEVPtr
= NewPtr
->Data
;
1296 for (Index
= 0; Index
< BbsCount
; Index
++) {
1297 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1298 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1303 switch (LocalBbsTable
[Index
].DeviceType
) {
1306 NewDevPtr
= NewFDPtr
;
1311 NewDevPtr
= NewHDPtr
;
1316 NewDevPtr
= NewCDPtr
;
1319 case BBS_EMBED_NETWORK
:
1321 NewDevPtr
= NewNETPtr
;
1324 case BBS_BEV_DEVICE
:
1326 NewDevPtr
= NewBEVPtr
;
1334 // at this point we have copied those valid indexes to new buffer
1335 // and we should check if there is any new appeared boot device
1338 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1339 if ((NewDevPtr
[Index2
] & 0xFF) == (UINT16
) Index
) {
1344 if (Index2
== *Idx
) {
1346 // Index2 == *Idx means we didn't find Index
1347 // so Index is a new appeared device's index in BBS table
1348 // insert it before disabled indexes.
1350 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1351 if ((NewDevPtr
[Index2
] & 0xFF00) == 0xFF00) {
1355 CopyMem (&NewDevPtr
[Index2
+ 1], &NewDevPtr
[Index2
], (*Idx
- Index2
) * sizeof (UINT16
));
1356 NewDevPtr
[Index2
] = (UINT16
) (Index
& 0xFF);
1362 FreePool (DevOrder
);
1364 Status
= gRT
->SetVariable (
1365 VAR_LEGACY_DEV_ORDER
,
1366 &gEfiLegacyDevOrderVariableGuid
,
1367 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1371 FreePool (NewDevOrder
);
1377 Set Boot Priority for specified device type.
1379 @param DeviceType The device type.
1380 @param BbsIndex The BBS index to set the highest priority. Ignore when -1.
1381 @param LocalBbsTable The BBS table.
1382 @param Priority The prority table.
1384 @retval EFI_SUCCESS The function completes successfully.
1385 @retval EFI_NOT_FOUND Failed to find device.
1386 @retval EFI_OUT_OF_RESOURCES Failed to get the efi variable of device order.
1390 BdsSetBootPriority4SameTypeDev (
1391 IN UINT16 DeviceType
,
1393 IN OUT BBS_TABLE
*LocalBbsTable
,
1394 IN OUT UINT16
*Priority
1397 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1398 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
1402 DevOrder
= BdsLibGetVariableAndSize (
1403 VAR_LEGACY_DEV_ORDER
,
1404 &gEfiLegacyDevOrderVariableGuid
,
1407 if (NULL
== DevOrder
) {
1408 return EFI_OUT_OF_RESOURCES
;
1411 DevOrderPtr
= DevOrder
;
1412 while ((UINT8
*) DevOrderPtr
< (UINT8
*) DevOrder
+ DevOrderSize
) {
1413 if (DevOrderPtr
->BbsType
== DeviceType
) {
1417 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) ((UINT8
*) DevOrderPtr
+ sizeof (BBS_TYPE
) + DevOrderPtr
->Length
);
1420 if ((UINT8
*) DevOrderPtr
>= (UINT8
*) DevOrder
+ DevOrderSize
) {
1421 FreePool (DevOrder
);
1422 return EFI_NOT_FOUND
;
1425 if (BbsIndex
!= (UINTN
) -1) {
1426 LocalBbsTable
[BbsIndex
].BootPriority
= *Priority
;
1430 // If the high byte of the DevIndex is 0xFF, it indicates that this device has been disabled.
1432 for (Index
= 0; Index
< DevOrderPtr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1433 if ((DevOrderPtr
->Data
[Index
] & 0xFF00) == 0xFF00) {
1435 // LocalBbsTable[DevIndex[Index] & 0xFF].BootPriority = BBS_DISABLED_ENTRY;
1437 } else if (DevOrderPtr
->Data
[Index
] != BbsIndex
) {
1438 LocalBbsTable
[DevOrderPtr
->Data
[Index
]].BootPriority
= *Priority
;
1443 FreePool (DevOrder
);
1448 Print the BBS Table.
1450 @param LocalBbsTable The BBS table.
1451 @param BbsCount The count of entry in BBS table.
1455 IN BBS_TABLE
*LocalBbsTable
,
1461 DEBUG ((DEBUG_ERROR
, "\n"));
1462 DEBUG ((DEBUG_ERROR
, " NO Prio bb/dd/ff cl/sc Type Stat segm:offs\n"));
1463 DEBUG ((DEBUG_ERROR
, "=============================================\n"));
1464 for (Idx
= 0; Idx
< BbsCount
; Idx
++) {
1465 if ((LocalBbsTable
[Idx
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1466 (LocalBbsTable
[Idx
].BootPriority
== BBS_DO_NOT_BOOT_FROM
) ||
1467 (LocalBbsTable
[Idx
].BootPriority
== BBS_LOWEST_PRIORITY
)
1474 " %02x: %04x %02x/%02x/%02x %02x/%02x %04x %04x %04x:%04x\n",
1476 (UINTN
) LocalBbsTable
[Idx
].BootPriority
,
1477 (UINTN
) LocalBbsTable
[Idx
].Bus
,
1478 (UINTN
) LocalBbsTable
[Idx
].Device
,
1479 (UINTN
) LocalBbsTable
[Idx
].Function
,
1480 (UINTN
) LocalBbsTable
[Idx
].Class
,
1481 (UINTN
) LocalBbsTable
[Idx
].SubClass
,
1482 (UINTN
) LocalBbsTable
[Idx
].DeviceType
,
1483 (UINTN
) * (UINT16
*) &LocalBbsTable
[Idx
].StatusFlags
,
1484 (UINTN
) LocalBbsTable
[Idx
].BootHandlerSegment
,
1485 (UINTN
) LocalBbsTable
[Idx
].BootHandlerOffset
,
1486 (UINTN
) ((LocalBbsTable
[Idx
].MfgStringSegment
<< 4) + LocalBbsTable
[Idx
].MfgStringOffset
),
1487 (UINTN
) ((LocalBbsTable
[Idx
].DescStringSegment
<< 4) + LocalBbsTable
[Idx
].DescStringOffset
))
1491 DEBUG ((DEBUG_ERROR
, "\n"));
1495 Set the boot priority for BBS entries based on boot option entry and boot order.
1497 @param Entry The boot option is to be checked for refresh BBS table.
1499 @retval EFI_SUCCESS The boot priority for BBS entries is refreshed successfully.
1500 @retval EFI_NOT_FOUND BBS entries can't be found.
1501 @retval EFI_OUT_OF_RESOURCES Failed to get the legacy device boot order.
1505 BdsRefreshBbsTableForBoot (
1506 IN BDS_COMMON_OPTION
*Entry
1513 HDD_INFO
*LocalHddInfo
;
1514 BBS_TABLE
*LocalBbsTable
;
1516 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1520 UINTN BootOrderSize
;
1521 UINT8
*BootOptionVar
;
1522 UINTN BootOptionSize
;
1523 CHAR16 BootOption
[9];
1526 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1528 UINTN DeviceTypeCount
;
1529 UINTN DeviceTypeIndex
;
1533 LocalHddInfo
= NULL
;
1534 LocalBbsTable
= NULL
;
1535 DevType
= BBS_UNKNOWN
;
1537 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1538 if (EFI_ERROR (Status
)) {
1542 LegacyBios
->GetBbsInfo (
1550 // First, set all the present devices' boot priority to BBS_UNPRIORITIZED_ENTRY
1551 // We will set them according to the settings setup by user
1553 for (Index
= 0; Index
< BbsCount
; Index
++) {
1554 if (!((BBS_IGNORE_ENTRY
== LocalBbsTable
[Index
].BootPriority
) ||
1555 (BBS_DO_NOT_BOOT_FROM
== LocalBbsTable
[Index
].BootPriority
) ||
1556 (BBS_LOWEST_PRIORITY
== LocalBbsTable
[Index
].BootPriority
))) {
1557 LocalBbsTable
[Index
].BootPriority
= BBS_UNPRIORITIZED_ENTRY
;
1561 // boot priority always starts at 0
1564 if (Entry
->LoadOptionsSize
== sizeof (BBS_TABLE
) + sizeof (UINT16
)) {
1566 // If Entry stands for a legacy boot option, we prioritize the devices with the same type first.
1568 DevType
= ((BBS_TABLE
*) Entry
->LoadOptions
)->DeviceType
;
1569 BbsIndex
= *(UINT16
*) ((BBS_TABLE
*) Entry
->LoadOptions
+ 1);
1570 Status
= BdsSetBootPriority4SameTypeDev (
1576 if (EFI_ERROR (Status
)) {
1581 // we have to set the boot priority for other BBS entries with different device types
1583 BootOrder
= BdsLibGetVariableAndSize (
1585 &gEfiGlobalVariableGuid
,
1588 DeviceType
= AllocatePool (BootOrderSize
+ sizeof (UINT16
));
1589 ASSERT (DeviceType
!= NULL
);
1591 DeviceType
[0] = DevType
;
1592 DeviceTypeCount
= 1;
1593 for (Index
= 0; ((BootOrder
!= NULL
) && (Index
< BootOrderSize
/ sizeof (UINT16
))); Index
++) {
1594 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
1595 BootOptionVar
= BdsLibGetVariableAndSize (
1597 &gEfiGlobalVariableGuid
,
1600 if (NULL
== BootOptionVar
) {
1604 Ptr
= BootOptionVar
;
1606 Ptr
+= sizeof (UINT32
);
1607 DevPathLen
= *(UINT16
*) Ptr
;
1608 Ptr
+= sizeof (UINT16
);
1609 Ptr
+= StrSize ((UINT16
*) Ptr
);
1610 DevPath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
1611 if (BBS_DEVICE_PATH
!= DevPath
->Type
|| BBS_BBS_DP
!= DevPath
->SubType
) {
1612 FreePool (BootOptionVar
);
1617 DevType
= ((BBS_TABLE
*) Ptr
)->DeviceType
;
1618 for (DeviceTypeIndex
= 0; DeviceTypeIndex
< DeviceTypeCount
; DeviceTypeIndex
++) {
1619 if (DeviceType
[DeviceTypeIndex
] == DevType
) {
1623 if (DeviceTypeIndex
< DeviceTypeCount
) {
1625 // We don't want to process twice for a device type
1627 FreePool (BootOptionVar
);
1631 DeviceType
[DeviceTypeCount
] = DevType
;
1634 Status
= BdsSetBootPriority4SameTypeDev (
1640 FreePool (BootOptionVar
);
1641 if (EFI_ERROR (Status
)) {
1646 if (BootOrder
!= NULL
) {
1647 FreePool (BootOrder
);
1651 PrintBbsTable (LocalBbsTable
, BbsCount
);
1658 Boot the legacy system with the boot option
1660 @param Option The legacy boot option which have BBS device path
1662 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
1664 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
1668 BdsLibDoLegacyBoot (
1669 IN BDS_COMMON_OPTION
*Option
1673 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1674 EFI_EVENT LegacyBootEvent
;
1676 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1677 if (EFI_ERROR (Status
)) {
1679 // If no LegacyBios protocol we do not support legacy boot
1681 return EFI_UNSUPPORTED
;
1684 // Notes: if we separate the int 19, then we don't need to refresh BBS
1686 BdsRefreshBbsTableForBoot (Option
);
1689 // Write boot to OS performance data for legacy boot.
1693 // Create an event to be signalled when Legacy Boot occurs to write performance data.
1695 Status
= EfiCreateEventLegacyBootEx(
1697 WriteBootToOsPerformanceData
,
1701 ASSERT_EFI_ERROR (Status
);
1704 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
1705 return LegacyBios
->LegacyBoot (
1707 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
1708 Option
->LoadOptionsSize
,
1714 Internal function to check if the input boot option is a valid EFI NV Boot####.
1716 @param OptionToCheck Boot option to be checked.
1718 @retval TRUE This boot option matches a valid EFI NV Boot####.
1719 @retval FALSE If not.
1723 IsBootOptionValidNVVarialbe (
1724 IN BDS_COMMON_OPTION
*OptionToCheck
1727 LIST_ENTRY TempList
;
1728 BDS_COMMON_OPTION
*BootOption
;
1730 CHAR16 OptionName
[20];
1734 InitializeListHead (&TempList
);
1735 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
1737 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
1738 if (BootOption
== NULL
) {
1743 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
1744 // valid EFI NV Boot####.
1746 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
1747 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
1752 FreePool (BootOption
);
1758 Check whether a USB device match the specified USB Class device path. This
1759 function follows "Load Option Processing" behavior in UEFI specification.
1761 @param UsbIo USB I/O protocol associated with the USB device.
1762 @param UsbClass The USB Class device path to match.
1764 @retval TRUE The USB device match the USB Class device path.
1765 @retval FALSE The USB device does not match the USB Class device path.
1770 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1771 IN USB_CLASS_DEVICE_PATH
*UsbClass
1775 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1776 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1778 UINT8 DeviceSubClass
;
1779 UINT8 DeviceProtocol
;
1781 if ((DevicePathType (UsbClass
) != MESSAGING_DEVICE_PATH
) ||
1782 (DevicePathSubType (UsbClass
) != MSG_USB_CLASS_DP
)){
1787 // Check Vendor Id and Product Id.
1789 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1790 if (EFI_ERROR (Status
)) {
1794 if ((UsbClass
->VendorId
!= 0xffff) &&
1795 (UsbClass
->VendorId
!= DevDesc
.IdVendor
)) {
1799 if ((UsbClass
->ProductId
!= 0xffff) &&
1800 (UsbClass
->ProductId
!= DevDesc
.IdProduct
)) {
1804 DeviceClass
= DevDesc
.DeviceClass
;
1805 DeviceSubClass
= DevDesc
.DeviceSubClass
;
1806 DeviceProtocol
= DevDesc
.DeviceProtocol
;
1807 if (DeviceClass
== 0) {
1809 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
1810 // Protocol in Interface Descriptor instead.
1812 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1813 if (EFI_ERROR (Status
)) {
1817 DeviceClass
= IfDesc
.InterfaceClass
;
1818 DeviceSubClass
= IfDesc
.InterfaceSubClass
;
1819 DeviceProtocol
= IfDesc
.InterfaceProtocol
;
1823 // Check Class, SubClass and Protocol.
1825 if ((UsbClass
->DeviceClass
!= 0xff) &&
1826 (UsbClass
->DeviceClass
!= DeviceClass
)) {
1830 if ((UsbClass
->DeviceSubClass
!= 0xff) &&
1831 (UsbClass
->DeviceSubClass
!= DeviceSubClass
)) {
1835 if ((UsbClass
->DeviceProtocol
!= 0xff) &&
1836 (UsbClass
->DeviceProtocol
!= DeviceProtocol
)) {
1844 Check whether a USB device match the specified USB WWID device path. This
1845 function follows "Load Option Processing" behavior in UEFI specification.
1847 @param UsbIo USB I/O protocol associated with the USB device.
1848 @param UsbWwid The USB WWID device path to match.
1850 @retval TRUE The USB device match the USB WWID device path.
1851 @retval FALSE The USB device does not match the USB WWID device path.
1856 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1857 IN USB_WWID_DEVICE_PATH
*UsbWwid
1861 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1862 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1863 UINT16
*LangIdTable
;
1868 CHAR16
*SerialNumberStr
;
1871 if ((DevicePathType (UsbWwid
) != MESSAGING_DEVICE_PATH
) ||
1872 (DevicePathSubType (UsbWwid
) != MSG_USB_WWID_DP
)){
1877 // Check Vendor Id and Product Id.
1879 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1880 if (EFI_ERROR (Status
)) {
1883 if ((DevDesc
.IdVendor
!= UsbWwid
->VendorId
) ||
1884 (DevDesc
.IdProduct
!= UsbWwid
->ProductId
)) {
1889 // Check Interface Number.
1891 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1892 if (EFI_ERROR (Status
)) {
1895 if (IfDesc
.InterfaceNumber
!= UsbWwid
->InterfaceNumber
) {
1900 // Check Serial Number.
1902 if (DevDesc
.StrSerialNumber
== 0) {
1907 // Get all supported languages.
1911 Status
= UsbIo
->UsbGetSupportedLanguages (UsbIo
, &LangIdTable
, &TableSize
);
1912 if (EFI_ERROR (Status
) || (TableSize
== 0) || (LangIdTable
== NULL
)) {
1917 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
1919 CompareStr
= (CHAR16
*) (UINTN
) (UsbWwid
+ 1);
1920 CompareLen
= (DevicePathNodeLength (UsbWwid
) - sizeof (USB_WWID_DEVICE_PATH
)) / sizeof (CHAR16
);
1921 if (CompareStr
[CompareLen
- 1] == L
'\0') {
1926 // Compare serial number in each supported language.
1928 for (Index
= 0; Index
< TableSize
/ sizeof (UINT16
); Index
++) {
1929 SerialNumberStr
= NULL
;
1930 Status
= UsbIo
->UsbGetStringDescriptor (
1933 DevDesc
.StrSerialNumber
,
1936 if (EFI_ERROR (Status
) || (SerialNumberStr
== NULL
)) {
1940 Length
= StrLen (SerialNumberStr
);
1941 if ((Length
>= CompareLen
) &&
1942 (CompareMem (SerialNumberStr
+ Length
- CompareLen
, CompareStr
, CompareLen
* sizeof (CHAR16
)) == 0)) {
1943 FreePool (SerialNumberStr
);
1947 FreePool (SerialNumberStr
);
1954 Find a USB device path which match the specified short-form device path start
1955 with USB Class or USB WWID device path and load the boot file then return the
1956 image handle. If ParentDevicePath is NULL, this function will search in all USB
1957 devices of the platform. If ParentDevicePath is not NULL,this function will only
1958 search in its child devices.
1960 @param ParentDevicePath The device path of the parent.
1961 @param ShortFormDevicePath The USB Class or USB WWID device path to match.
1963 @return The image Handle if find load file from specified short-form device path
1964 or NULL if not found.
1969 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
1970 IN EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
1974 UINTN UsbIoHandleCount
;
1975 EFI_HANDLE
*UsbIoHandleBuffer
;
1976 EFI_DEVICE_PATH_PROTOCOL
*UsbIoDevicePath
;
1977 EFI_USB_IO_PROTOCOL
*UsbIo
;
1981 EFI_HANDLE ImageHandle
;
1983 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
1984 EFI_DEVICE_PATH_PROTOCOL
*NextDevicePath
;
1986 FullDevicePath
= NULL
;
1990 // Get all UsbIo Handles.
1992 UsbIoHandleCount
= 0;
1993 UsbIoHandleBuffer
= NULL
;
1994 Status
= gBS
->LocateHandleBuffer (
1996 &gEfiUsbIoProtocolGuid
,
2001 if (EFI_ERROR (Status
) || (UsbIoHandleCount
== 0) || (UsbIoHandleBuffer
== NULL
)) {
2005 ParentSize
= (ParentDevicePath
== NULL
) ? 0 : GetDevicePathSize (ParentDevicePath
);
2006 for (Index
= 0; Index
< UsbIoHandleCount
; Index
++) {
2008 // Get the Usb IO interface.
2010 Status
= gBS
->HandleProtocol(
2011 UsbIoHandleBuffer
[Index
],
2012 &gEfiUsbIoProtocolGuid
,
2015 if (EFI_ERROR (Status
)) {
2019 UsbIoDevicePath
= DevicePathFromHandle (UsbIoHandleBuffer
[Index
]);
2020 if (UsbIoDevicePath
== NULL
) {
2024 if (ParentDevicePath
!= NULL
) {
2026 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
2028 Size
= GetDevicePathSize (UsbIoDevicePath
);
2029 if ((Size
< ParentSize
) ||
2030 (CompareMem (UsbIoDevicePath
, ParentDevicePath
, ParentSize
- END_DEVICE_PATH_LENGTH
) != 0)) {
2035 if (BdsMatchUsbClass (UsbIo
, (USB_CLASS_DEVICE_PATH
*) ShortFormDevicePath
) ||
2036 BdsMatchUsbWwid (UsbIo
, (USB_WWID_DEVICE_PATH
*) ShortFormDevicePath
)) {
2038 // Try to find if there is the boot file in this DevicePath
2040 NextDevicePath
= NextDevicePathNode (ShortFormDevicePath
);
2041 if (!IsDevicePathEnd (NextDevicePath
)) {
2042 FullDevicePath
= AppendDevicePath (UsbIoDevicePath
, NextDevicePath
);
2044 // Connect the full device path, so that Simple File System protocol
2045 // could be installed for this USB device.
2047 BdsLibConnectDevicePath (FullDevicePath
);
2048 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2049 Status
= gBS
->LoadImage (
2057 FreePool (FullDevicePath
);
2059 FullDevicePath
= UsbIoDevicePath
;
2060 Status
= EFI_NOT_FOUND
;
2064 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2065 // and load the image according to the default boot behavior for removable device.
2067 if (EFI_ERROR (Status
)) {
2069 // check if there is a bootable removable media could be found in this device path ,
2070 // and get the bootable media handle
2072 Handle
= BdsLibGetBootableHandle(UsbIoDevicePath
);
2073 if (Handle
== NULL
) {
2077 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2078 // machinename is ia32, ia64, x64, ...
2080 FullDevicePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2081 if (FullDevicePath
!= NULL
) {
2082 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2083 Status
= gBS
->LoadImage (
2091 if (EFI_ERROR (Status
)) {
2093 // The DevicePath failed, and it's not a valid
2094 // removable media device.
2106 FreePool (UsbIoHandleBuffer
);
2111 Expand USB Class or USB WWID device path node to be full device path of a USB
2112 device in platform then load the boot file on this full device path and return the
2115 This function support following 4 cases:
2116 1) Boot Option device path starts with a USB Class or USB WWID device path,
2117 and there is no Media FilePath device path in the end.
2118 In this case, it will follow Removable Media Boot Behavior.
2119 2) Boot Option device path starts with a USB Class or USB WWID device path,
2120 and ended with Media FilePath device path.
2121 3) Boot Option device path starts with a full device path to a USB Host Controller,
2122 contains a USB Class or USB WWID device path node, while not ended with Media
2123 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
2124 4) Boot Option device path starts with a full device path to a USB Host Controller,
2125 contains a USB Class or USB WWID device path node, and ended with Media
2126 FilePath device path.
2128 @param DevicePath The Boot Option device path.
2130 @return The image handle of boot file, or NULL if there is no boot file found in
2131 the specified USB Class or USB WWID device path.
2135 BdsExpandUsbShortFormDevicePath (
2136 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
2139 EFI_HANDLE
*ImageHandle
;
2140 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2141 EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
;
2144 // Search for USB Class or USB WWID device path node.
2146 ShortFormDevicePath
= NULL
;
2148 TempDevicePath
= DevicePath
;
2149 while (!IsDevicePathEnd (TempDevicePath
)) {
2150 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
2151 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
2152 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
2153 ShortFormDevicePath
= TempDevicePath
;
2156 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2159 if (ShortFormDevicePath
== NULL
) {
2161 // No USB Class or USB WWID device path node found, do nothing.
2166 if (ShortFormDevicePath
== DevicePath
) {
2168 // Boot Option device path starts with USB Class or USB WWID device path.
2170 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2171 if (ImageHandle
== NULL
) {
2173 // Failed to find a match in existing devices, connect the short form USB
2174 // device path and try again.
2176 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath
);
2177 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2181 // Boot Option device path contains USB Class or USB WWID device path node.
2185 // Prepare the parent device path for search.
2187 TempDevicePath
= DuplicateDevicePath (DevicePath
);
2188 ASSERT (TempDevicePath
!= NULL
);
2189 SetDevicePathEndNode (((UINT8
*) TempDevicePath
) + ((UINTN
) ShortFormDevicePath
- (UINTN
) DevicePath
));
2192 // The USB Host Controller device path is already in Boot Option device path
2193 // and USB Bus driver already support RemainingDevicePath starts with USB
2194 // Class or USB WWID device path, so just search in existing USB devices and
2195 // doesn't perform ConnectController here.
2197 ImageHandle
= BdsFindUsbDevice (TempDevicePath
, ShortFormDevicePath
);
2198 FreePool (TempDevicePath
);
2205 Process the boot option follow the UEFI specification and
2206 special treat the legacy boot option with BBS_DEVICE_PATH.
2208 @param Option The boot option need to be processed
2209 @param DevicePath The device path which describe where to load the
2210 boot image or the legacy BBS device path to boot
2212 @param ExitDataSize The size of exit data.
2213 @param ExitData Data returned when Boot image failed.
2215 @retval EFI_SUCCESS Boot from the input boot option successfully.
2216 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2221 BdsLibBootViaBootOption (
2222 IN BDS_COMMON_OPTION
*Option
,
2223 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2224 OUT UINTN
*ExitDataSize
,
2225 OUT CHAR16
**ExitData OPTIONAL
2229 EFI_STATUS StatusLogo
;
2231 EFI_HANDLE ImageHandle
;
2232 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
2233 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
2234 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
2235 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
2236 LIST_ENTRY TempBootLists
;
2237 EFI_BOOT_LOGO_PROTOCOL
*BootLogo
;
2243 // Notes: this code can be remove after the s3 script table
2244 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
2245 // EVT_SIGNAL_LEGACY_BOOT
2247 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
2248 if (!EFI_ERROR (Status
)) {
2249 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
2252 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
2255 WorkingDevicePath
= NULL
;
2256 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2257 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
2258 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
2259 (HARDDRIVE_DEVICE_PATH
*)DevicePath
2261 if (WorkingDevicePath
!= NULL
) {
2262 DevicePath
= WorkingDevicePath
;
2269 if (IsBootOptionValidNVVarialbe (Option
)) {
2271 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
2272 // In this case, "BootCurrent" is not created.
2273 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
2275 SetVariableAndReportStatusCodeOnError (
2277 &gEfiGlobalVariableGuid
,
2278 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2280 &Option
->BootCurrent
2285 // Report Status Code to indicate ReadyToBoot event will be signalled
2287 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT
));
2290 // Signal the EVT_SIGNAL_READY_TO_BOOT event
2292 EfiSignalEventReadyToBoot();
2295 // Expand USB Class or USB WWID device path node to be full device path of a USB
2296 // device in platform then load the boot file on this full device path and get the
2299 ImageHandle
= BdsExpandUsbShortFormDevicePath (DevicePath
);
2302 // Adjust the different type memory page number just before booting
2303 // and save the updated info into the variable for next boot to use
2305 BdsSetMemoryTypeInformationVariable ();
2308 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.
2309 // Here get the ImageHandle for the non USB class or WWID device path.
2311 if (ImageHandle
== NULL
) {
2312 ASSERT (Option
->DevicePath
!= NULL
);
2313 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
2314 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
2317 // Check to see if we should legacy BOOT. If yes then do the legacy boot
2319 return BdsLibDoLegacyBoot (Option
);
2323 // If the boot option point to Internal FV shell, make sure it is valid
2325 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
2326 if (!EFI_ERROR(Status
)) {
2327 if (Option
->DevicePath
!= NULL
) {
2328 FreePool(Option
->DevicePath
);
2330 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
2331 ASSERT(Option
->DevicePath
!= NULL
);
2332 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
2334 // Update the shell boot option
2336 InitializeListHead (&TempBootLists
);
2337 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
2340 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
2342 FreePool (DevicePath
);
2343 DevicePath
= Option
->DevicePath
;
2348 if (Option
->Description
== NULL
) {
2349 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
2351 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
2357 // Report status code for OS Loader LoadImage.
2359 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2360 Status
= gBS
->LoadImage (
2370 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2371 // and load the image according to the default boot behavior for removable device.
2373 if (EFI_ERROR (Status
)) {
2375 // check if there is a bootable removable media could be found in this device path ,
2376 // and get the bootable media handle
2378 Handle
= BdsLibGetBootableHandle(DevicePath
);
2379 if (Handle
!= NULL
) {
2381 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2382 // machinename is ia32, ia64, x64, ...
2384 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2385 if (FilePath
!= NULL
) {
2386 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2387 Status
= gBS
->LoadImage (
2400 // Provide the image with it's load options
2402 if ((ImageHandle
== NULL
) || (EFI_ERROR(Status
))) {
2404 // Report Status Code to indicate that the failure to load boot option
2406 REPORT_STATUS_CODE (
2407 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2408 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR
)
2413 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
2414 ASSERT_EFI_ERROR (Status
);
2416 if (Option
->LoadOptionsSize
!= 0) {
2417 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
2418 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
2422 // Clean to NULL because the image is loaded directly from the firmwares boot manager.
2424 ImageInfo
->ParentHandle
= NULL
;
2427 // Before calling the image, enable the Watchdog Timer for
2428 // the 5 Minute period
2430 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
2433 // Write boot to OS performance data for UEFI boot
2436 WriteBootToOsPerformanceData (NULL
, NULL
);
2440 // Report status code for OS Loader StartImage.
2442 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderStart
));
2444 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
2445 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
2446 if (EFI_ERROR (Status
)) {
2448 // Report Status Code to indicate that boot failure
2450 REPORT_STATUS_CODE (
2451 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2452 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED
)
2457 // Clear the Watchdog Timer after the image returns
2459 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
2463 // Set Logo status invalid after trying one boot option
2466 StatusLogo
= gBS
->LocateProtocol (&gEfiBootLogoProtocolGuid
, NULL
, (VOID
**) &BootLogo
);
2467 if (!EFI_ERROR (StatusLogo
) && (BootLogo
!= NULL
)) {
2468 BootLogo
->SetBootLogo (BootLogo
, NULL
, 0, 0, 0, 0);
2472 // Clear Boot Current
2473 // Deleting variable with current implementation shouldn't fail.
2477 &gEfiGlobalVariableGuid
,
2478 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2488 Expand a device path that starts with a hard drive media device path node to be a
2489 full device path that includes the full hardware path to the device. We need
2490 to do this so it can be booted. As an optimization the front match (the part point
2491 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
2492 so a connect all is not required on every boot. All successful history device path
2493 which point to partition node (the front part) will be saved.
2495 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
2496 drive media device path.
2497 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
2501 EFI_DEVICE_PATH_PROTOCOL
*
2503 BdsExpandPartitionPartialDevicePathToFull (
2504 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2508 UINTN BlockIoHandleCount
;
2509 EFI_HANDLE
*BlockIoBuffer
;
2510 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
2511 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
2512 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2515 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
2516 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
2517 UINTN CachedDevicePathSize
;
2518 BOOLEAN DeviceExist
;
2520 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
2523 FullDevicePath
= NULL
;
2525 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2526 // If exist, search the front path which point to partition node in the variable instants.
2527 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system
2529 CachedDevicePath
= BdsLibGetVariableAndSize (
2530 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2531 &gHdBootDevicePathVariablGuid
,
2532 &CachedDevicePathSize
2535 if (CachedDevicePath
!= NULL
) {
2536 TempNewDevicePath
= CachedDevicePath
;
2537 DeviceExist
= FALSE
;
2541 // Check every instance of the variable
2542 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
2543 // partial partition boot option. Second, check whether the instance could be connected.
2545 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
2546 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
2548 // Connect the device path instance, the device path point to hard drive media device path node
2549 // e.g. ACPI() /PCI()/ATA()/Partition()
2551 Status
= BdsLibConnectDevicePath (Instance
);
2552 if (!EFI_ERROR (Status
)) {
2558 // Come here means the first instance is not matched
2562 } while (TempNewDevicePath
!= NULL
);
2566 // Find the matched device path.
2567 // Append the file path information from the boot option and return the fully expanded device path.
2569 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2570 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
2573 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.
2577 // First delete the matched instance.
2579 TempNewDevicePath
= CachedDevicePath
;
2580 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
2581 FreePool (TempNewDevicePath
);
2584 // Second, append the remaining path after the matched instance
2586 TempNewDevicePath
= CachedDevicePath
;
2587 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
2588 FreePool (TempNewDevicePath
);
2590 // Save the matching Device Path so we don't need to do a connect all next time
2591 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2593 Status
= gRT
->SetVariable (
2594 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2595 &gHdBootDevicePathVariablGuid
,
2596 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2597 GetDevicePathSize (CachedDevicePath
),
2602 FreePool (Instance
);
2603 FreePool (CachedDevicePath
);
2604 return FullDevicePath
;
2609 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need
2610 // to search all devices in the system for a matched partition
2612 BdsLibConnectAllDriversToAllControllers ();
2613 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
2614 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
2616 // If there was an error or there are no device handles that support
2617 // the BLOCK_IO Protocol, then return.
2622 // Loop through all the device handles that support the BLOCK_IO Protocol
2624 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
2626 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
2627 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
2631 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
2633 // Find the matched partition device path
2635 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2636 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
2639 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2641 if (CachedDevicePath
!= NULL
) {
2643 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2645 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
2646 TempNewDevicePath
= CachedDevicePath
;
2647 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
2648 FreePool(TempNewDevicePath
);
2651 if (CachedDevicePath
!= NULL
) {
2652 TempNewDevicePath
= CachedDevicePath
;
2653 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
2654 FreePool(TempNewDevicePath
);
2656 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2660 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
2661 // If the user try to boot many OS in different HDs or partitions, in theory,
2662 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.
2665 ASSERT (CachedDevicePath
!= NULL
);
2666 TempNewDevicePath
= CachedDevicePath
;
2667 while (!IsDevicePathEnd (TempNewDevicePath
)) {
2668 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2670 // Parse one instance
2672 while (!IsDevicePathEndType (TempNewDevicePath
)) {
2673 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2677 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
2679 if (InstanceNum
>= 12) {
2680 SetDevicePathEndNode (TempNewDevicePath
);
2685 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2689 // Save the matching Device Path so we don't need to do a connect all next time
2690 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2692 Status
= gRT
->SetVariable (
2693 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2694 &gHdBootDevicePathVariablGuid
,
2695 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2696 GetDevicePathSize (CachedDevicePath
),
2704 if (CachedDevicePath
!= NULL
) {
2705 FreePool (CachedDevicePath
);
2707 if (BlockIoBuffer
!= NULL
) {
2708 FreePool (BlockIoBuffer
);
2710 return FullDevicePath
;
2714 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
2715 instances, has the same partition node with HardDriveDevicePath device path
2717 @param BlockIoDevicePath Multi device path instances which need to check
2718 @param HardDriveDevicePath A device path which starts with a hard drive media
2721 @retval TRUE There is a matched device path instance.
2722 @retval FALSE There is no matched device path instance.
2727 MatchPartitionDevicePathNode (
2728 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
2729 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2732 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
2733 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2735 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
2737 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
2742 // Make PreviousDevicePath == the device path node before the end node
2744 DevicePath
= BlockIoDevicePath
;
2745 BlockIoHdDevicePathNode
= NULL
;
2748 // find the partition device path node
2750 while (!IsDevicePathEnd (DevicePath
)) {
2751 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2752 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
2754 BlockIoHdDevicePathNode
= DevicePath
;
2758 DevicePath
= NextDevicePathNode (DevicePath
);
2761 if (BlockIoHdDevicePathNode
== NULL
) {
2765 // See if the harddrive device path in blockio matches the orig Hard Drive Node
2767 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
2771 // Check for the match
2773 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
2774 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
2775 switch (TmpHdPath
->SignatureType
) {
2776 case SIGNATURE_TYPE_GUID
:
2777 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
2779 case SIGNATURE_TYPE_MBR
:
2780 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
2792 Delete the boot option associated with the handle passed in.
2794 @param Handle The handle which present the device path to create
2797 @retval EFI_SUCCESS Delete the boot option success
2798 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2799 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2800 @retval Other Error return value from SetVariable()
2804 BdsLibDeleteOptionFromHandle (
2805 IN EFI_HANDLE Handle
2809 UINT8
*BootOptionVar
;
2810 UINTN BootOrderSize
;
2811 UINTN BootOptionSize
;
2814 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2815 UINTN DevicePathSize
;
2816 UINTN OptionDevicePathSize
;
2817 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2818 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2821 Status
= EFI_SUCCESS
;
2826 // Check "BootOrder" variable, if no, means there is no any boot order.
2828 BootOrder
= BdsLibGetVariableAndSize (
2830 &gEfiGlobalVariableGuid
,
2833 if (BootOrder
== NULL
) {
2834 return EFI_NOT_FOUND
;
2838 // Convert device handle to device path protocol instance
2840 DevicePath
= DevicePathFromHandle (Handle
);
2841 if (DevicePath
== NULL
) {
2842 return EFI_NOT_FOUND
;
2844 DevicePathSize
= GetDevicePathSize (DevicePath
);
2847 // Loop all boot order variable and find the matching device path
2850 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2851 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2852 BootOptionVar
= BdsLibGetVariableAndSize (
2854 &gEfiGlobalVariableGuid
,
2858 if (BootOptionVar
== NULL
) {
2859 FreePool (BootOrder
);
2860 return EFI_OUT_OF_RESOURCES
;
2863 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2864 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2865 FreePool (BootOptionVar
);
2870 TempPtr
= BootOptionVar
;
2871 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
2872 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
2873 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
2874 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
2877 // Check whether the device path match
2879 if ((OptionDevicePathSize
== DevicePathSize
) &&
2880 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
2881 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2882 FreePool (BootOptionVar
);
2886 FreePool (BootOptionVar
);
2891 // Adjust number of boot option for "BootOrder" variable.
2893 Status
= gRT
->SetVariable (
2895 &gEfiGlobalVariableGuid
,
2896 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2901 // Shrinking variable with existing variable implementation shouldn't fail.
2903 ASSERT_EFI_ERROR (Status
);
2905 FreePool (BootOrder
);
2912 Delete all invalid EFI boot options.
2914 @retval EFI_SUCCESS Delete all invalid boot option success
2915 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
2916 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2917 @retval Other Error return value from SetVariable()
2921 BdsDeleteAllInvalidEfiBootOption (
2926 UINT8
*BootOptionVar
;
2927 UINTN BootOrderSize
;
2928 UINTN BootOptionSize
;
2932 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2933 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2935 CHAR16
*Description
;
2938 Status
= EFI_SUCCESS
;
2941 OptionDevicePath
= NULL
;
2946 // Check "BootOrder" variable firstly, this variable hold the number of boot options
2948 BootOrder
= BdsLibGetVariableAndSize (
2950 &gEfiGlobalVariableGuid
,
2953 if (NULL
== BootOrder
) {
2954 return EFI_NOT_FOUND
;
2958 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2959 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2960 BootOptionVar
= BdsLibGetVariableAndSize (
2962 &gEfiGlobalVariableGuid
,
2965 if (NULL
== BootOptionVar
) {
2966 FreePool (BootOrder
);
2967 return EFI_OUT_OF_RESOURCES
;
2970 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2973 TempPtr
= BootOptionVar
;
2974 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
2975 Description
= (CHAR16
*) TempPtr
;
2976 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
2977 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
2980 // Skip legacy boot option (BBS boot device)
2982 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
2983 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
2984 FreePool (BootOptionVar
);
2990 if (Corrupted
|| !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
2992 // Delete this invalid boot option "Boot####"
2994 Status
= gRT
->SetVariable (
2996 &gEfiGlobalVariableGuid
,
2997 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3002 // Deleting variable with current variable implementation shouldn't fail.
3004 ASSERT_EFI_ERROR (Status
);
3006 // Mark this boot option in boot order as deleted
3008 BootOrder
[Index
] = 0xffff;
3012 FreePool (BootOptionVar
);
3017 // Adjust boot order array
3020 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
3021 if (BootOrder
[Index
] != 0xffff) {
3022 BootOrder
[Index2
] = BootOrder
[Index
];
3026 Status
= gRT
->SetVariable (
3028 &gEfiGlobalVariableGuid
,
3029 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3030 Index2
* sizeof (UINT16
),
3034 // Shrinking variable with current variable implementation shouldn't fail.
3036 ASSERT_EFI_ERROR (Status
);
3038 FreePool (BootOrder
);
3045 For EFI boot option, BDS separate them as six types:
3046 1. Network - The boot option points to the SimpleNetworkProtocol device.
3047 Bds will try to automatically create this type boot option when enumerate.
3048 2. Shell - The boot option points to internal flash shell.
3049 Bds will try to automatically create this type boot option when enumerate.
3050 3. Removable BlockIo - The boot option only points to the removable media
3051 device, like USB flash disk, DVD, Floppy etc.
3052 These device should contain a *removable* blockIo
3053 protocol in their device handle.
3054 Bds will try to automatically create this type boot option
3056 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
3058 These device should contain a *fixed* blockIo
3059 protocol in their device handle.
3060 BDS will skip fixed blockIo devices, and NOT
3061 automatically create boot option for them. But BDS
3062 will help to delete those fixed blockIo boot option,
3063 whose description rule conflict with other auto-created
3065 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
3066 has SimpleFileSystem Protocol, but has no blockio
3067 protocol. These devices do not offer blockIo
3068 protocol, but BDS still can get the
3069 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
3071 6. File - The boot option points to a file. These boot options are usually
3072 created by user manually or OS loader. BDS will not delete or modify
3075 This function will enumerate all possible boot device in the system, and
3076 automatically create boot options for Network, Shell, Removable BlockIo,
3077 and Non-BlockIo Simplefile devices.
3078 It will only execute once of every boot.
3080 @param BdsBootOptionList The header of the link list which indexed all
3081 current boot options
3083 @retval EFI_SUCCESS Finished all the boot device enumerate and create
3084 the boot option base on that boot device
3086 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
3090 BdsLibEnumerateAllBootOption (
3091 IN OUT LIST_ENTRY
*BdsBootOptionList
3095 UINT16 FloppyNumber
;
3096 UINT16 HarddriveNumber
;
3101 UINT16 NonBlockNumber
;
3102 UINTN NumberBlockIoHandles
;
3103 EFI_HANDLE
*BlockIoHandles
;
3104 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
3105 BOOLEAN Removable
[2];
3106 UINTN RemovableIndex
;
3108 UINTN NumOfLoadFileHandles
;
3109 EFI_HANDLE
*LoadFileHandles
;
3110 UINTN FvHandleCount
;
3111 EFI_HANDLE
*FvHandleBuffer
;
3112 EFI_FV_FILETYPE Type
;
3114 EFI_FV_FILE_ATTRIBUTES Attributes
;
3115 UINT32 AuthenticationStatus
;
3116 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
3117 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3118 UINTN DevicePathType
;
3120 EFI_HANDLE
*FileSystemHandles
;
3121 UINTN NumberFileSystemHandles
;
3123 EFI_IMAGE_DOS_HEADER DosHeader
;
3126 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3127 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3130 HarddriveNumber
= 0;
3137 ZeroMem (Buffer
, sizeof (Buffer
));
3140 // If the boot device enumerate happened, just get the boot
3141 // device from the boot order variable
3143 if (mEnumBootDevice
) {
3144 GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME
, &gLastEnumLangGuid
, (VOID
**)&LastLang
, NULL
);
3145 GetEfiGlobalVariable2 (L
"PlatformLang", (VOID
**)&PlatLang
, NULL
);
3146 ASSERT (PlatLang
!= NULL
);
3147 if ((LastLang
!= NULL
) && (AsciiStrCmp (LastLang
, PlatLang
) == 0)) {
3148 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3149 FreePool (LastLang
);
3150 FreePool (PlatLang
);
3153 Status
= gRT
->SetVariable (
3154 LAST_ENUM_LANGUAGE_VARIABLE_NAME
,
3156 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3157 AsciiStrSize (PlatLang
),
3161 // Failure to set the variable only impacts the performance next time enumerating the boot options.
3164 if (LastLang
!= NULL
) {
3165 FreePool (LastLang
);
3167 FreePool (PlatLang
);
3172 // Notes: this dirty code is to get the legacy boot option from the
3173 // BBS table and create to variable as the EFI boot option, it should
3174 // be removed after the CSM can provide legacy boot option directly
3176 REFRESH_LEGACY_BOOT_OPTIONS
;
3179 // Delete invalid boot option
3181 BdsDeleteAllInvalidEfiBootOption ();
3184 // Parse removable media followed by fixed media.
3185 // The Removable[] array is used by the for-loop below to create removable media boot options
3186 // at first, and then to create fixed media boot options.
3188 Removable
[0] = FALSE
;
3189 Removable
[1] = TRUE
;
3191 gBS
->LocateHandleBuffer (
3193 &gEfiBlockIoProtocolGuid
,
3195 &NumberBlockIoHandles
,
3199 for (RemovableIndex
= 0; RemovableIndex
< 2; RemovableIndex
++) {
3200 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
3201 Status
= gBS
->HandleProtocol (
3202 BlockIoHandles
[Index
],
3203 &gEfiBlockIoProtocolGuid
,
3207 // skip the fixed block io then the removable block io
3209 if (EFI_ERROR (Status
) || (BlkIo
->Media
->RemovableMedia
== Removable
[RemovableIndex
])) {
3212 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
3213 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
3215 switch (DevicePathType
) {
3216 case BDS_EFI_ACPI_FLOPPY_BOOT
:
3217 if (FloppyNumber
!= 0) {
3218 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
3220 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
3222 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3227 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.
3229 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
3230 case BDS_EFI_MESSAGE_SATA_BOOT
:
3231 if (BlkIo
->Media
->RemovableMedia
) {
3232 if (CdromNumber
!= 0) {
3233 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
3235 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
3239 if (HarddriveNumber
!= 0) {
3240 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)), HarddriveNumber
);
3242 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)));
3246 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
3247 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3250 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
3251 if (UsbNumber
!= 0) {
3252 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
3254 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
3256 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3260 case BDS_EFI_MESSAGE_SCSI_BOOT
:
3261 if (ScsiNumber
!= 0) {
3262 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
3264 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
3266 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3270 case BDS_EFI_MESSAGE_MISC_BOOT
:
3271 if (MiscNumber
!= 0) {
3272 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
3274 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
3276 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3286 if (NumberBlockIoHandles
!= 0) {
3287 FreePool (BlockIoHandles
);
3291 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
3294 gBS
->LocateHandleBuffer (
3296 &gEfiSimpleFileSystemProtocolGuid
,
3298 &NumberFileSystemHandles
,
3301 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
3302 Status
= gBS
->HandleProtocol (
3303 FileSystemHandles
[Index
],
3304 &gEfiBlockIoProtocolGuid
,
3307 if (!EFI_ERROR (Status
)) {
3309 // Skip if the file system handle supports a BlkIo protocol,
3315 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
3316 // machinename is ia32, ia64, x64, ...
3318 Hdr
.Union
= &HdrData
;
3320 Status
= BdsLibGetImageHeader (
3321 FileSystemHandles
[Index
],
3322 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3326 if (!EFI_ERROR (Status
) &&
3327 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3328 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3334 // No such file or the file is not a EFI application, delete this boot option
3336 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
3338 if (NonBlockNumber
!= 0) {
3339 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
3341 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
3343 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
3348 if (NumberFileSystemHandles
!= 0) {
3349 FreePool (FileSystemHandles
);
3353 // Parse Network Boot Device
3355 NumOfLoadFileHandles
= 0;
3357 // Search Load File protocol for PXE boot option.
3359 gBS
->LocateHandleBuffer (
3361 &gEfiLoadFileProtocolGuid
,
3363 &NumOfLoadFileHandles
,
3367 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
3369 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)), Index
);
3371 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)));
3373 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
3376 if (NumOfLoadFileHandles
!= 0) {
3377 FreePool (LoadFileHandles
);
3381 // Check if we have on flash shell
3383 gBS
->LocateHandleBuffer (
3385 &gEfiFirmwareVolume2ProtocolGuid
,
3390 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
3391 gBS
->HandleProtocol (
3392 FvHandleBuffer
[Index
],
3393 &gEfiFirmwareVolume2ProtocolGuid
,
3397 Status
= Fv
->ReadFile (
3399 PcdGetPtr(PcdShellFile
),
3404 &AuthenticationStatus
3406 if (EFI_ERROR (Status
)) {
3408 // Skip if no shell file in the FV
3413 // Build the shell boot option
3415 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
3418 if (FvHandleCount
!= 0) {
3419 FreePool (FvHandleBuffer
);
3422 // Make sure every boot only have one time
3423 // boot device enumerate
3425 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3426 mEnumBootDevice
= TRUE
;
3432 Build the boot option with the handle parsed in
3434 @param Handle The handle which present the device path to create
3436 @param BdsBootOptionList The header of the link list which indexed all
3437 current boot options
3438 @param String The description of the boot option.
3443 BdsLibBuildOptionFromHandle (
3444 IN EFI_HANDLE Handle
,
3445 IN LIST_ENTRY
*BdsBootOptionList
,
3449 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3451 DevicePath
= DevicePathFromHandle (Handle
);
3454 // Create and register new boot option
3456 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
3461 Build the on flash shell boot option with the handle parsed in.
3463 @param Handle The handle which present the device path to create
3464 on flash shell boot option
3465 @param BdsBootOptionList The header of the link list which indexed all
3466 current boot options
3471 BdsLibBuildOptionFromShell (
3472 IN EFI_HANDLE Handle
,
3473 IN OUT LIST_ENTRY
*BdsBootOptionList
3476 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3477 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
3479 DevicePath
= DevicePathFromHandle (Handle
);
3482 // Build the shell device path
3484 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
3486 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
3489 // Create and register the shell boot option
3491 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
3496 Boot from the UEFI spec defined "BootNext" variable.
3509 BDS_COMMON_OPTION
*BootOption
;
3510 LIST_ENTRY TempList
;
3515 // Init the boot option name buffer and temp link list
3517 InitializeListHead (&TempList
);
3518 ZeroMem (Buffer
, sizeof (Buffer
));
3520 BootNext
= BdsLibGetVariableAndSize (
3522 &gEfiGlobalVariableGuid
,
3527 // Clear the boot next variable first
3529 if (BootNext
!= NULL
) {
3530 Status
= gRT
->SetVariable (
3532 &gEfiGlobalVariableGuid
,
3533 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3538 // Deleting variable with current variable implementation shouldn't fail.
3540 ASSERT_EFI_ERROR (Status
);
3543 // Start to build the boot option and try to boot
3545 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
3546 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
3547 ASSERT (BootOption
!= NULL
);
3548 BdsLibConnectDevicePath (BootOption
->DevicePath
);
3549 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
3555 Return the bootable media handle.
3556 First, check the device is connected
3557 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
3558 Third, detect the the default boot file in the Media, and return the removable Media handle.
3560 @param DevicePath Device Path to a bootable device
3562 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
3567 BdsLibGetBootableHandle (
3568 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3573 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3574 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
3576 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
3578 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3581 EFI_HANDLE ReturnHandle
;
3582 EFI_HANDLE
*SimpleFileSystemHandles
;
3584 UINTN NumberSimpleFileSystemHandles
;
3586 EFI_IMAGE_DOS_HEADER DosHeader
;
3587 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3588 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3590 UpdatedDevicePath
= DevicePath
;
3593 // Enter to critical section to protect the acquired BlockIo instance
3594 // from getting released due to the USB mass storage hotplug event
3596 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
3599 // Check whether the device is connected
3601 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3602 if (EFI_ERROR (Status
)) {
3604 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
3606 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3607 if (EFI_ERROR (Status
)) {
3609 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
3611 UpdatedDevicePath
= DevicePath
;
3612 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3613 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3617 // For removable device boot option, its contained device path only point to the removable device handle,
3618 // should make sure all its children handles (its child partion or media handles) are created and connected.
3620 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3622 // Get BlockIo protocol and check removable attribute
3624 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
3625 ASSERT_EFI_ERROR (Status
);
3628 // Issue a dummy read to the device to check for media change.
3629 // When the removable media is changed, any Block IO read/write will
3630 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
3631 // returned. After the Block IO protocol is reinstalled, subsequent
3632 // Block IO read/write will success.
3634 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
3635 if (Buffer
!= NULL
) {
3636 BlockIo
->ReadBlocks (
3638 BlockIo
->Media
->MediaId
,
3640 BlockIo
->Media
->BlockSize
,
3648 // Detect the the default boot file from removable Media
3652 // 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
3653 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
3655 DupDevicePath
= DuplicateDevicePath (DevicePath
);
3656 ASSERT (DupDevicePath
!= NULL
);
3658 UpdatedDevicePath
= DupDevicePath
;
3659 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3661 // 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
3662 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
3664 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
3665 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
3667 // Remove the usb node, let the device path only point to PCI node
3669 SetDevicePathEndNode (UpdatedDevicePath
);
3670 UpdatedDevicePath
= DupDevicePath
;
3672 UpdatedDevicePath
= DevicePath
;
3676 // Get the device path size of boot option
3678 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3679 ReturnHandle
= NULL
;
3680 gBS
->LocateHandleBuffer (
3682 &gEfiSimpleFileSystemProtocolGuid
,
3684 &NumberSimpleFileSystemHandles
,
3685 &SimpleFileSystemHandles
3687 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
3689 // Get the device path size of SimpleFileSystem handle
3691 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
3692 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3694 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
3696 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
3698 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
3699 // machinename is ia32, ia64, x64, ...
3701 Hdr
.Union
= &HdrData
;
3702 Status
= BdsLibGetImageHeader (
3703 SimpleFileSystemHandles
[Index
],
3704 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3708 if (!EFI_ERROR (Status
) &&
3709 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3710 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3711 ReturnHandle
= SimpleFileSystemHandles
[Index
];
3717 FreePool(DupDevicePath
);
3719 if (SimpleFileSystemHandles
!= NULL
) {
3720 FreePool(SimpleFileSystemHandles
);
3723 gBS
->RestoreTPL (OldTpl
);
3725 return ReturnHandle
;
3729 Check to see if the network cable is plugged in. If the DevicePath is not
3730 connected it will be connected.
3732 @param DevicePath Device Path to check
3734 @retval TRUE DevicePath points to an Network that is connected
3735 @retval FALSE DevicePath does not point to a bootable network
3739 BdsLibNetworkBootWithMediaPresent (
3740 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3744 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3746 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
3747 BOOLEAN MediaPresent
;
3748 UINT32 InterruptStatus
;
3750 MediaPresent
= FALSE
;
3752 UpdatedDevicePath
= DevicePath
;
3754 // Locate Load File Protocol for PXE boot option first
3756 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3757 if (EFI_ERROR (Status
)) {
3759 // Device not present so see if we need to connect it
3761 Status
= BdsLibConnectDevicePath (DevicePath
);
3762 if (!EFI_ERROR (Status
)) {
3764 // This one should work after we did the connect
3766 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3770 if (!EFI_ERROR (Status
)) {
3771 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
3772 if (EFI_ERROR (Status
)) {
3774 // Failed to open SNP from this handle, try to get SNP from parent handle
3776 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
3777 if (UpdatedDevicePath
!= NULL
) {
3778 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3779 if (!EFI_ERROR (Status
)) {
3781 // SNP handle found, get SNP from it
3783 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
3788 if (!EFI_ERROR (Status
)) {
3789 if (Snp
->Mode
->MediaPresentSupported
) {
3790 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
3792 // Invoke Snp->GetStatus() to refresh the media status
3794 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
3797 // In case some one else is using the SNP check to see if it's connected
3799 MediaPresent
= Snp
->Mode
->MediaPresent
;
3802 // No one is using SNP so we need to Start and Initialize so
3803 // MediaPresent will be valid.
3805 Status
= Snp
->Start (Snp
);
3806 if (!EFI_ERROR (Status
)) {
3807 Status
= Snp
->Initialize (Snp
, 0, 0);
3808 if (!EFI_ERROR (Status
)) {
3809 MediaPresent
= Snp
->Mode
->MediaPresent
;
3810 Snp
->Shutdown (Snp
);
3816 MediaPresent
= TRUE
;
3821 return MediaPresent
;
3825 For a bootable Device path, return its boot type.
3827 @param DevicePath The bootable device Path to check
3829 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3830 which subtype is MEDIA_HARDDRIVE_DP
3831 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3832 which subtype is MEDIA_CDROM_DP
3833 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
3834 which HID is floppy device.
3835 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3836 and its last device path node's subtype is MSG_ATAPI_DP.
3837 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3838 and its last device path node's subtype is MSG_SCSI_DP.
3839 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3840 and its last device path node's subtype is MSG_USB_DP.
3841 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
3842 its last device path node point to a message device path node.
3843 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
3844 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
3849 BdsGetBootTypeFromDevicePath (
3850 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3853 ACPI_HID_DEVICE_PATH
*Acpi
;
3854 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3855 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
3858 if (NULL
== DevicePath
) {
3859 return BDS_EFI_UNSUPPORT
;
3862 TempDevicePath
= DevicePath
;
3864 while (!IsDevicePathEndType (TempDevicePath
)) {
3865 switch (DevicePathType (TempDevicePath
)) {
3866 case BBS_DEVICE_PATH
:
3867 return BDS_LEGACY_BBS_BOOT
;
3868 case MEDIA_DEVICE_PATH
:
3869 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
3870 return BDS_EFI_MEDIA_HD_BOOT
;
3871 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
3872 return BDS_EFI_MEDIA_CDROM_BOOT
;
3875 case ACPI_DEVICE_PATH
:
3876 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
3877 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
3878 return BDS_EFI_ACPI_FLOPPY_BOOT
;
3881 case MESSAGING_DEVICE_PATH
:
3883 // Get the last device path node
3885 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
3886 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
3888 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
3891 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
3894 // if the device path not only point to driver device, it is not a messaging device path,
3896 if (!IsDevicePathEndType (LastDeviceNode
)) {
3900 switch (DevicePathSubType (TempDevicePath
)) {
3902 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
3906 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
3910 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
3914 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
3917 case MSG_MAC_ADDR_DP
:
3921 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
3925 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
3933 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
3936 return BDS_EFI_UNSUPPORT
;
3940 Check whether the Device path in a boot option point to a valid bootable device,
3941 And if CheckMedia is true, check the device is ready to boot now.
3943 @param DevPath the Device path in a boot option
3944 @param CheckMedia if true, check the device is ready to boot now.
3946 @retval TRUE the Device path is valid
3947 @retval FALSE the Device path is invalid .
3952 BdsLibIsValidEFIBootOptDevicePath (
3953 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
3954 IN BOOLEAN CheckMedia
3957 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
3961 Check whether the Device path in a boot option point to a valid bootable device,
3962 And if CheckMedia is true, check the device is ready to boot now.
3963 If Description is not NULL and the device path point to a fixed BlockIo
3964 device, check the description whether conflict with other auto-created
3967 @param DevPath the Device path in a boot option
3968 @param CheckMedia if true, check the device is ready to boot now.
3969 @param Description the description in a boot option
3971 @retval TRUE the Device path is valid
3972 @retval FALSE the Device path is invalid .
3977 BdsLibIsValidEFIBootOptDevicePathExt (
3978 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
3979 IN BOOLEAN CheckMedia
,
3980 IN CHAR16
*Description
3985 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3986 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
3987 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
3989 TempDevicePath
= DevPath
;
3990 LastDeviceNode
= DevPath
;
3993 // Check if it's a valid boot option for network boot device.
3994 // Check if there is EfiLoadFileProtocol installed.
3995 // If yes, that means there is a boot option for network.
3997 Status
= gBS
->LocateDevicePath (
3998 &gEfiLoadFileProtocolGuid
,
4002 if (EFI_ERROR (Status
)) {
4004 // Device not present so see if we need to connect it
4006 TempDevicePath
= DevPath
;
4007 BdsLibConnectDevicePath (TempDevicePath
);
4008 Status
= gBS
->LocateDevicePath (
4009 &gEfiLoadFileProtocolGuid
,
4015 if (!EFI_ERROR (Status
)) {
4016 if (!IsDevicePathEnd (TempDevicePath
)) {
4018 // LoadFile protocol is not installed on handle with exactly the same DevPath
4025 // Test if it is ready to boot now
4027 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
4036 // If the boot option point to a file, it is a valid EFI boot option,
4037 // and assume it is ready to boot now
4039 while (!IsDevicePathEnd (TempDevicePath
)) {
4041 // If there is USB Class or USB WWID device path node, treat it as valid EFI
4042 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it
4043 // to full device path.
4045 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
4046 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
4047 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
4051 LastDeviceNode
= TempDevicePath
;
4052 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4054 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
4055 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
4060 // Check if it's a valid boot option for internal FV application
4062 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
4064 // If the boot option point to internal FV application, make sure it is valid
4066 TempDevicePath
= DevPath
;
4067 Status
= BdsLibUpdateFvFileDevicePath (
4069 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
)
4071 if (Status
== EFI_ALREADY_STARTED
) {
4074 if (Status
== EFI_SUCCESS
) {
4075 FreePool (TempDevicePath
);
4082 // If the boot option point to a blockIO device:
4083 // if it is a removable blockIo device, it is valid.
4084 // if it is a fixed blockIo device, check its description confliction.
4086 TempDevicePath
= DevPath
;
4087 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4088 if (EFI_ERROR (Status
)) {
4090 // Device not present so see if we need to connect it
4092 Status
= BdsLibConnectDevicePath (DevPath
);
4093 if (!EFI_ERROR (Status
)) {
4095 // Try again to get the Block Io protocol after we did the connect
4097 TempDevicePath
= DevPath
;
4098 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4102 if (!EFI_ERROR (Status
)) {
4103 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
4104 if (!EFI_ERROR (Status
)) {
4107 // Test if it is ready to boot now
4109 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4118 // 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,
4120 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
4121 if (!EFI_ERROR (Status
)) {
4124 // Test if it is ready to boot now
4126 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4140 According to a file guild, check a Fv file device path is valid. If it is invalid,
4141 try to return the valid device path.
4142 FV address maybe changes for memory layout adjust from time to time, use this function
4143 could promise the Fv file device path is right.
4145 @param DevicePath on input, the Fv file device path need to check on
4146 output, the updated valid Fv file device path
4147 @param FileGuid the Fv file guild
4149 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
4151 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
4153 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
4155 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
4156 and return the updated device path in DevicePath
4161 BdsLibUpdateFvFileDevicePath (
4162 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
4163 IN EFI_GUID
*FileGuid
4166 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4167 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4169 EFI_GUID
*GuidPoint
;
4171 UINTN FvHandleCount
;
4172 EFI_HANDLE
*FvHandleBuffer
;
4173 EFI_FV_FILETYPE Type
;
4175 EFI_FV_FILE_ATTRIBUTES Attributes
;
4176 UINT32 AuthenticationStatus
;
4178 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
4179 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
4180 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
4181 EFI_HANDLE FoundFvHandle
;
4182 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
4184 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
4185 return EFI_INVALID_PARAMETER
;
4187 if (FileGuid
== NULL
) {
4188 return EFI_INVALID_PARAMETER
;
4192 // Check whether the device path point to the default the input Fv file
4194 TempDevicePath
= *DevicePath
;
4195 LastDeviceNode
= TempDevicePath
;
4196 while (!IsDevicePathEnd (TempDevicePath
)) {
4197 LastDeviceNode
= TempDevicePath
;
4198 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4200 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
4201 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
4203 if (GuidPoint
== NULL
) {
4205 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
4207 return EFI_UNSUPPORTED
;
4209 if (!CompareGuid (GuidPoint
, FileGuid
)) {
4211 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
4213 return EFI_UNSUPPORTED
;
4217 // Check whether the input Fv file device path is valid
4219 TempDevicePath
= *DevicePath
;
4220 FoundFvHandle
= NULL
;
4221 Status
= gBS
->LocateDevicePath (
4222 &gEfiFirmwareVolume2ProtocolGuid
,
4226 if (!EFI_ERROR (Status
)) {
4227 Status
= gBS
->HandleProtocol (
4229 &gEfiFirmwareVolume2ProtocolGuid
,
4232 if (!EFI_ERROR (Status
)) {
4234 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
4236 Status
= Fv
->ReadFile (
4243 &AuthenticationStatus
4245 if (!EFI_ERROR (Status
)) {
4246 return EFI_ALREADY_STARTED
;
4252 // Look for the input wanted FV file in current FV
4253 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
4256 FoundFvHandle
= NULL
;
4257 Status
= gBS
->HandleProtocol (
4259 &gEfiLoadedImageProtocolGuid
,
4260 (VOID
**) &LoadedImage
4262 if (!EFI_ERROR (Status
)) {
4263 Status
= gBS
->HandleProtocol (
4264 LoadedImage
->DeviceHandle
,
4265 &gEfiFirmwareVolume2ProtocolGuid
,
4268 if (!EFI_ERROR (Status
)) {
4269 Status
= Fv
->ReadFile (
4276 &AuthenticationStatus
4278 if (!EFI_ERROR (Status
)) {
4280 FoundFvHandle
= LoadedImage
->DeviceHandle
;
4285 // Second, if fail to find, try to enumerate all FV
4288 FvHandleBuffer
= NULL
;
4289 gBS
->LocateHandleBuffer (
4291 &gEfiFirmwareVolume2ProtocolGuid
,
4296 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
4297 gBS
->HandleProtocol (
4298 FvHandleBuffer
[Index
],
4299 &gEfiFirmwareVolume2ProtocolGuid
,
4303 Status
= Fv
->ReadFile (
4310 &AuthenticationStatus
4312 if (EFI_ERROR (Status
)) {
4314 // Skip if input Fv file not in the FV
4319 FoundFvHandle
= FvHandleBuffer
[Index
];
4323 if (FvHandleBuffer
!= NULL
) {
4324 FreePool (FvHandleBuffer
);
4330 // Build the shell device path
4332 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
4333 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
4334 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
4335 *DevicePath
= NewDevicePath
;
4338 return EFI_NOT_FOUND
;