2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalBdsLib.h"
17 #include <Library/NetLib.h>
18 #include "Library/DebugLib.h"
20 BOOLEAN mEnumBootDevice
= FALSE
;
21 EFI_HII_HANDLE gBdsLibStringPackHandle
= NULL
;
27 @param Event The triggered event.
28 @param Context Context for this event.
39 // Record the performance data for End of BDS
41 PERF_END(NULL
, "BDS", NULL
, 0);
47 The constructor function register UNI strings into imageHandle.
49 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
51 @param ImageHandle The firmware allocated handle for the EFI image.
52 @param SystemTable A pointer to the EFI System Table.
54 @retval EFI_SUCCESS The constructor successfully added string package.
55 @retval Other value The constructor can't add string package.
60 GenericBdsLibConstructor (
61 IN EFI_HANDLE ImageHandle
,
62 IN EFI_SYSTEM_TABLE
*SystemTable
66 gBdsLibStringPackHandle
= HiiAddPackages (
67 &gBdsLibStringPackageGuid
,
73 ASSERT (gBdsLibStringPackHandle
!= NULL
);
79 Deletete the Boot Option from EFI Variable. The Boot Order Arrray
82 @param OptionNumber The number of Boot option want to be deleted.
83 @param BootOrder The Boot Order array.
84 @param BootOrderSize The size of the Boot Order Array.
86 @retval EFI_SUCCESS The Boot Option Variable was found and removed
87 @retval EFI_UNSUPPORTED The Boot Option Variable store was inaccessible
88 @retval EFI_NOT_FOUND The Boot Option Variable was not found
93 IN UINTN OptionNumber
,
94 IN OUT UINT16
*BootOrder
,
95 IN OUT UINTN
*BootOrderSize
102 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", OptionNumber
);
103 Status
= gRT
->SetVariable (
105 &gEfiGlobalVariableGuid
,
111 // Deleting variable with existing variable implementation shouldn't fail.
113 ASSERT_EFI_ERROR (Status
);
116 // adjust boot order array
118 for (Index
= 0; Index
< *BootOrderSize
/ sizeof (UINT16
); Index
++) {
119 if (BootOrder
[Index
] == OptionNumber
) {
120 CopyMem (&BootOrder
[Index
], &BootOrder
[Index
+1], *BootOrderSize
- (Index
+1) * sizeof (UINT16
));
121 *BootOrderSize
-= sizeof (UINT16
);
130 Translate the first n characters of an Ascii string to
131 Unicode characters. The count n is indicated by parameter
132 Size. If Size is greater than the length of string, then
133 the entire string is translated.
136 @param AStr Pointer to input Ascii string.
137 @param Size The number of characters to translate.
138 @param UStr Pointer to output Unicode string buffer.
151 while (AStr
[Idx
] != 0) {
152 UStr
[Idx
] = (CHAR16
) AStr
[Idx
];
163 Build Legacy Device Name String according.
165 @param CurBBSEntry BBS Table.
167 @param BufSize The buffer size.
168 @param BootString The output string.
172 BdsBuildLegacyDevNameString (
173 IN BBS_TABLE
*CurBBSEntry
,
176 OUT CHAR16
*BootString
189 Fmt
= L
"Primary Master %s";
196 Fmt
= L
"Primary Slave %s";
203 Fmt
= L
"Secondary Master %s";
210 Fmt
= L
"Secondary Slave %s";
218 switch (CurBBSEntry
->DeviceType
) {
239 case BBS_EMBED_NETWORK
:
253 // If current BBS entry has its description then use it.
255 StringDesc
= (UINT8
*) (UINTN
) ((CurBBSEntry
->DescStringSegment
<< 4) + CurBBSEntry
->DescStringOffset
);
256 if (NULL
!= StringDesc
) {
258 // Only get fisrt 32 characters, this is suggested by BBS spec
260 AsciiToUnicodeSize (StringDesc
, 32, Temp
);
266 // BbsTable 16 entries are for onboard IDE.
267 // Set description string for SATA harddisks, Harddisk 0 ~ Harddisk 11
269 if (Index
>= 5 && Index
<= 16 && (CurBBSEntry
->DeviceType
== BBS_HARDDISK
|| CurBBSEntry
->DeviceType
== BBS_CDROM
)) {
271 UnicodeSPrint (BootString
, BufSize
, Fmt
, Type
, Index
- 5);
273 UnicodeSPrint (BootString
, BufSize
, Fmt
, Type
);
279 Create a legacy boot option for the specified entry of
280 BBS table, save it as variable, and append it to the boot
284 @param CurrentBbsEntry Pointer to current BBS table.
285 @param CurrentBbsDevPath Pointer to the Device Path Protocol instance of BBS
286 @param Index Index of the specified entry in BBS table.
287 @param BootOrderList On input, the original boot order list.
288 On output, the new boot order list attached with the
290 @param BootOrderListSize On input, the original size of boot order list.
291 On output, the size of new boot order list.
293 @retval EFI_SUCCESS Boot Option successfully created.
294 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
295 @retval Other Error occurs while setting variable.
299 BdsCreateLegacyBootOption (
300 IN BBS_TABLE
*CurrentBbsEntry
,
301 IN EFI_DEVICE_PATH_PROTOCOL
*CurrentBbsDevPath
,
303 IN OUT UINT16
**BootOrderList
,
304 IN OUT UINTN
*BootOrderListSize
308 UINT16 CurrentBootOptionNo
;
309 UINT16 BootString
[10];
310 CHAR16 BootDesc
[100];
311 CHAR8 HelpString
[100];
312 UINT16
*NewBootOrderList
;
317 UINT16 CurrentBbsDevPathSize
;
318 UINTN BootOrderIndex
;
319 UINTN BootOrderLastIndex
;
321 BOOLEAN IndexNotFound
;
322 BBS_BBS_DEVICE_PATH
*NewBbsDevPathNode
;
324 if ((*BootOrderList
) == NULL
) {
325 CurrentBootOptionNo
= 0;
327 for (ArrayIndex
= 0; ArrayIndex
< (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
)); ArrayIndex
++) {
328 IndexNotFound
= TRUE
;
329 for (BootOrderIndex
= 0; BootOrderIndex
< (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
)); BootOrderIndex
++) {
330 if ((*BootOrderList
)[BootOrderIndex
] == ArrayIndex
) {
331 IndexNotFound
= FALSE
;
336 if (!IndexNotFound
) {
343 CurrentBootOptionNo
= (UINT16
) ArrayIndex
;
353 BdsBuildLegacyDevNameString (CurrentBbsEntry
, Index
, sizeof (BootDesc
), BootDesc
);
356 // Create new BBS device path node with description string
358 UnicodeStrToAsciiStr (BootDesc
, HelpString
);
360 StringLen
= AsciiStrLen (HelpString
);
361 NewBbsDevPathNode
= AllocateZeroPool (sizeof (BBS_BBS_DEVICE_PATH
) + StringLen
);
362 if (NewBbsDevPathNode
== NULL
) {
363 return EFI_OUT_OF_RESOURCES
;
365 CopyMem (NewBbsDevPathNode
, CurrentBbsDevPath
, sizeof (BBS_BBS_DEVICE_PATH
));
366 CopyMem (NewBbsDevPathNode
->String
, HelpString
, StringLen
+ 1);
367 SetDevicePathNodeLength (&(NewBbsDevPathNode
->Header
), sizeof (BBS_BBS_DEVICE_PATH
) + StringLen
);
370 // Create entire new CurrentBbsDevPath with end node
372 CurrentBbsDevPath
= AppendDevicePathNode (
374 (EFI_DEVICE_PATH_PROTOCOL
*) NewBbsDevPathNode
376 if (CurrentBbsDevPath
== NULL
) {
377 FreePool (NewBbsDevPathNode
);
378 return EFI_OUT_OF_RESOURCES
;
381 CurrentBbsDevPathSize
= (UINT16
) (GetDevicePathSize (CurrentBbsDevPath
));
383 BufferSize
= sizeof (UINT32
) +
386 CurrentBbsDevPathSize
+
390 Buffer
= AllocateZeroPool (BufferSize
);
391 if (Buffer
== NULL
) {
392 FreePool (NewBbsDevPathNode
);
393 FreePool (CurrentBbsDevPath
);
394 return EFI_OUT_OF_RESOURCES
;
397 Ptr
= (UINT8
*) Buffer
;
399 *((UINT32
*) Ptr
) = LOAD_OPTION_ACTIVE
;
400 Ptr
+= sizeof (UINT32
);
402 *((UINT16
*) Ptr
) = CurrentBbsDevPathSize
;
403 Ptr
+= sizeof (UINT16
);
410 Ptr
+= StrSize (BootDesc
);
415 CurrentBbsDevPathSize
417 Ptr
+= CurrentBbsDevPathSize
;
425 Ptr
+= sizeof (BBS_TABLE
);
426 *((UINT16
*) Ptr
) = (UINT16
) Index
;
428 Status
= gRT
->SetVariable (
430 &gEfiGlobalVariableGuid
,
431 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
440 NewBootOrderList
= AllocateZeroPool (*BootOrderListSize
+ sizeof (UINT16
));
441 if (NULL
== NewBootOrderList
) {
442 FreePool (NewBbsDevPathNode
);
443 FreePool (CurrentBbsDevPath
);
444 return EFI_OUT_OF_RESOURCES
;
447 if (*BootOrderList
!= NULL
) {
448 CopyMem (NewBootOrderList
, *BootOrderList
, *BootOrderListSize
);
449 FreePool (*BootOrderList
);
452 BootOrderLastIndex
= (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
));
453 NewBootOrderList
[BootOrderLastIndex
] = CurrentBootOptionNo
;
454 *BootOrderListSize
+= sizeof (UINT16
);
455 *BootOrderList
= NewBootOrderList
;
457 FreePool (NewBbsDevPathNode
);
458 FreePool (CurrentBbsDevPath
);
463 Check if the boot option is a legacy one.
465 @param BootOptionVar The boot option data payload.
466 @param BbsEntry The BBS Table.
467 @param BbsIndex The table index.
469 @retval TRUE It is a legacy boot option.
470 @retval FALSE It is not a legacy boot option.
474 BdsIsLegacyBootOption (
475 IN UINT8
*BootOptionVar
,
476 OUT BBS_TABLE
**BbsEntry
,
481 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
486 Ptr
+= sizeof (UINT32
);
487 DevPathLen
= *(UINT16
*) Ptr
;
488 Ptr
+= sizeof (UINT16
);
489 Ptr
+= StrSize ((UINT16
*) Ptr
);
490 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
491 if ((BBS_DEVICE_PATH
== DevicePath
->Type
) && (BBS_BBS_DP
== DevicePath
->SubType
)) {
493 *BbsEntry
= (BBS_TABLE
*) Ptr
;
494 Ptr
+= sizeof (BBS_TABLE
);
495 *BbsIndex
= *(UINT16
*) Ptr
;
506 Delete all the invalid legacy boot options.
508 @retval EFI_SUCCESS All invalide legacy boot options are deleted.
509 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
510 @retval EFI_NOT_FOUND Fail to retrive variable of boot order.
514 BdsDeleteAllInvalidLegacyBootOptions (
519 UINT8
*BootOptionVar
;
521 UINTN BootOptionSize
;
525 HDD_INFO
*LocalHddInfo
;
526 BBS_TABLE
*LocalBbsTable
;
529 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
531 UINT16 BootOption
[10];
532 UINT16 BootDesc
[100];
533 BOOLEAN DescStringMatch
;
535 Status
= EFI_SUCCESS
;
541 LocalBbsTable
= NULL
;
544 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
545 if (EFI_ERROR (Status
)) {
549 BootOrder
= BdsLibGetVariableAndSize (
551 &gEfiGlobalVariableGuid
,
554 if (BootOrder
== NULL
) {
555 return EFI_NOT_FOUND
;
558 LegacyBios
->GetBbsInfo (
567 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
568 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
569 BootOptionVar
= BdsLibGetVariableAndSize (
571 &gEfiGlobalVariableGuid
,
574 if (NULL
== BootOptionVar
) {
576 Status
= gRT
->GetVariable (
578 &gEfiGlobalVariableGuid
,
583 if (Status
== EFI_NOT_FOUND
) {
587 BdsDeleteBootOption (
594 FreePool (BootOrder
);
595 return EFI_OUT_OF_RESOURCES
;
600 // Skip Non-Legacy boot option
602 if (!BdsIsLegacyBootOption (BootOptionVar
, &BbsEntry
, &BbsIndex
)) {
603 if (BootOptionVar
!= NULL
) {
604 FreePool (BootOptionVar
);
610 if (BbsIndex
< BbsCount
) {
612 // Check if BBS Description String is changed
614 DescStringMatch
= FALSE
;
615 BdsBuildLegacyDevNameString (
616 &LocalBbsTable
[BbsIndex
],
622 if (StrCmp (BootDesc
, (UINT16
*)(BootOptionVar
+ sizeof (UINT32
) + sizeof (UINT16
))) == 0) {
623 DescStringMatch
= TRUE
;
626 if (!((LocalBbsTable
[BbsIndex
].BootPriority
== BBS_IGNORE_ENTRY
) ||
627 (LocalBbsTable
[BbsIndex
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)) &&
628 (LocalBbsTable
[BbsIndex
].DeviceType
== BbsEntry
->DeviceType
) &&
635 if (BootOptionVar
!= NULL
) {
636 FreePool (BootOptionVar
);
641 BdsDeleteBootOption (
649 // Adjust the number of boot options.
651 Status
= gRT
->SetVariable (
653 &gEfiGlobalVariableGuid
,
654 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
659 // Shrinking variable with existing variable implementation shouldn't fail.
661 ASSERT_EFI_ERROR (Status
);
662 FreePool (BootOrder
);
668 Find all legacy boot option by device type.
670 @param BootOrder The boot order array.
671 @param BootOptionNum The number of boot option.
672 @param DevType Device type.
673 @param DevName Device name.
674 @param Attribute The boot option attribute.
675 @param BbsIndex The BBS table index.
676 @param OptionNumber The boot option index.
678 @retval TRUE The Legacy boot option is found.
679 @retval FALSE The legacy boot option is not found.
683 BdsFindLegacyBootOptionByDevTypeAndName (
684 IN UINT16
*BootOrder
,
685 IN UINTN BootOptionNum
,
688 OUT UINT32
*Attribute
,
689 OUT UINT16
*BbsIndex
,
690 OUT UINT16
*OptionNumber
694 CHAR16 BootOption
[9];
695 UINTN BootOptionSize
;
696 UINT8
*BootOptionVar
;
703 if (NULL
== BootOrder
) {
708 // Loop all boot option from variable
710 for (Index
= 0; Index
< BootOptionNum
; Index
++) {
711 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", (UINTN
) BootOrder
[Index
]);
712 BootOptionVar
= BdsLibGetVariableAndSize (
714 &gEfiGlobalVariableGuid
,
717 if (NULL
== BootOptionVar
) {
722 // Skip Non-legacy boot option
724 if (!BdsIsLegacyBootOption (BootOptionVar
, &BbsEntry
, BbsIndex
)) {
725 FreePool (BootOptionVar
);
730 (BbsEntry
->DeviceType
!= DevType
) ||
731 (StrCmp (DevName
, (CHAR16
*)(BootOptionVar
+ sizeof (UINT32
) + sizeof (UINT16
))) != 0)
733 FreePool (BootOptionVar
);
737 *Attribute
= *(UINT32
*) BootOptionVar
;
738 *OptionNumber
= BootOrder
[Index
];
740 FreePool (BootOptionVar
);
748 Create a legacy boot option.
750 @param BbsItem The BBS Table entry.
751 @param Index Index of the specified entry in BBS table.
752 @param BootOrderList The boot order list.
753 @param BootOrderListSize The size of boot order list.
755 @retval EFI_OUT_OF_RESOURCE No enough memory.
756 @retval EFI_SUCCESS The function complete successfully.
757 @return Other value if the legacy boot option is not created.
761 BdsCreateOneLegacyBootOption (
762 IN BBS_TABLE
*BbsItem
,
764 IN OUT UINT16
**BootOrderList
,
765 IN OUT UINTN
*BootOrderListSize
768 BBS_BBS_DEVICE_PATH BbsDevPathNode
;
770 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
775 // Create device path node.
777 BbsDevPathNode
.Header
.Type
= BBS_DEVICE_PATH
;
778 BbsDevPathNode
.Header
.SubType
= BBS_BBS_DP
;
779 SetDevicePathNodeLength (&BbsDevPathNode
.Header
, sizeof (BBS_BBS_DEVICE_PATH
));
780 BbsDevPathNode
.DeviceType
= BbsItem
->DeviceType
;
781 CopyMem (&BbsDevPathNode
.StatusFlag
, &BbsItem
->StatusFlags
, sizeof (UINT16
));
783 DevPath
= AppendDevicePathNode (
785 (EFI_DEVICE_PATH_PROTOCOL
*) &BbsDevPathNode
787 if (NULL
== DevPath
) {
788 return EFI_OUT_OF_RESOURCES
;
791 Status
= BdsCreateLegacyBootOption (
798 BbsItem
->BootPriority
= 0x00;
806 Add the legacy boot options from BBS table if they do not exist.
808 @retval EFI_SUCCESS The boot options are added successfully
809 or they are already in boot options.
810 @retval EFI_NOT_FOUND No legacy boot options is found.
811 @retval EFI_OUT_OF_RESOURCE No enough memory.
812 @return Other value LegacyBoot options are not added.
816 BdsAddNonExistingLegacyBootOptions (
826 HDD_INFO
*LocalHddInfo
;
827 BBS_TABLE
*LocalBbsTable
;
829 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
838 LocalBbsTable
= NULL
;
840 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
841 if (EFI_ERROR (Status
)) {
845 LegacyBios
->GetBbsInfo (
853 BootOrder
= BdsLibGetVariableAndSize (
855 &gEfiGlobalVariableGuid
,
858 if (BootOrder
== NULL
) {
862 for (Index
= 0; Index
< BbsCount
; Index
++) {
863 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
864 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
869 BdsBuildLegacyDevNameString (&LocalBbsTable
[Index
], Index
, sizeof (Desc
), Desc
);
871 Exist
= BdsFindLegacyBootOptionByDevTypeAndName (
873 BootOrderSize
/ sizeof (UINT16
),
874 LocalBbsTable
[Index
].DeviceType
,
882 // Not found such type of legacy device in boot options or we found but it's disabled
883 // so we have to create one and put it to the tail of boot order list
885 Status
= BdsCreateOneLegacyBootOption (
886 &LocalBbsTable
[Index
],
891 if (!EFI_ERROR (Status
)) {
892 ASSERT (BootOrder
!= NULL
);
894 OptionNumber
= BootOrder
[BootOrderSize
/ sizeof (UINT16
) - 1];
898 ASSERT (BbsIndex
== Index
);
901 Status
= gRT
->SetVariable (
903 &gEfiGlobalVariableGuid
,
904 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
908 if (BootOrder
!= NULL
) {
909 FreePool (BootOrder
);
916 Fill the device order buffer.
918 @param BbsTable The BBS table.
919 @param BbsType The BBS Type.
920 @param BbsCount The BBS Count.
921 @param Buf device order buffer.
923 @return The device order buffer.
928 IN BBS_TABLE
*BbsTable
,
936 for (Index
= 0; Index
< BbsCount
; Index
++) {
937 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
941 if (BbsTable
[Index
].DeviceType
!= BbsType
) {
945 *Buf
= (UINT16
) (Index
& 0xFF);
953 Create the device order buffer.
955 @param BbsTable The BBS table.
956 @param BbsCount The BBS Count.
958 @retval EFI_SUCCES The buffer is created and the EFI variable named
959 VAR_LEGACY_DEV_ORDER and gEfiLegacyDevOrderVariableGuid is
961 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
962 @retval EFI_DEVICE_ERROR Fail to add the device order into EFI variable fail
963 because of hardware error.
967 IN BBS_TABLE
*BbsTable
,
979 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
980 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
989 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
991 Status
= EFI_SUCCESS
;
994 // Count all boot devices
996 for (Index
= 0; Index
< BbsCount
; Index
++) {
997 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
1001 switch (BbsTable
[Index
].DeviceType
) {
1014 case BBS_EMBED_NETWORK
:
1018 case BBS_BEV_DEVICE
:
1027 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * FDCount
);
1028 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * HDCount
);
1029 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * CDCount
);
1030 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * NETCount
);
1031 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * BEVCount
);
1034 // Create buffer to hold all boot device order
1036 DevOrder
= AllocateZeroPool (TotalSize
);
1037 if (NULL
== DevOrder
) {
1038 return EFI_OUT_OF_RESOURCES
;
1040 DevOrderPtr
= DevOrder
;
1042 DevOrderPtr
->BbsType
= BBS_FLOPPY
;
1043 DevOrderPtr
->Length
= (UINT16
) (sizeof (DevOrderPtr
->Length
) + FDCount
* sizeof (UINT16
));
1044 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_FLOPPY
, BbsCount
, DevOrderPtr
->Data
);
1046 DevOrderPtr
->BbsType
= BBS_HARDDISK
;
1047 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1048 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_HARDDISK
, BbsCount
, DevOrderPtr
->Data
);
1050 DevOrderPtr
->BbsType
= BBS_CDROM
;
1051 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1052 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_CDROM
, BbsCount
, DevOrderPtr
->Data
);
1054 DevOrderPtr
->BbsType
= BBS_EMBED_NETWORK
;
1055 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1056 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_EMBED_NETWORK
, BbsCount
, DevOrderPtr
->Data
);
1058 DevOrderPtr
->BbsType
= BBS_BEV_DEVICE
;
1059 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1060 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_BEV_DEVICE
, BbsCount
, DevOrderPtr
->Data
);
1062 ASSERT (TotalSize
== (UINTN
) ((UINT8
*) DevOrderPtr
- (UINT8
*) DevOrder
));
1065 // Save device order for legacy boot device to variable.
1067 Status
= gRT
->SetVariable (
1068 VAR_LEGACY_DEV_ORDER
,
1069 &gEfiLegacyDevOrderVariableGuid
,
1070 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1074 FreePool (DevOrder
);
1080 Add the legacy boot devices from BBS table into
1081 the legacy device boot order.
1083 @retval EFI_SUCCESS The boot devices are added successfully.
1084 @retval EFI_NOT_FOUND The legacy boot devices are not found.
1085 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
1086 @retval EFI_DEVICE_ERROR Fail to add the legacy device boot order into EFI variable
1087 because of hardware error.
1091 BdsUpdateLegacyDevOrder (
1095 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1096 LEGACY_DEV_ORDER_ENTRY
*NewDevOrder
;
1097 LEGACY_DEV_ORDER_ENTRY
*Ptr
;
1098 LEGACY_DEV_ORDER_ENTRY
*NewPtr
;
1100 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1104 HDD_INFO
*LocalHddInfo
;
1105 BBS_TABLE
*LocalBbsTable
;
1135 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
1143 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1144 if (EFI_ERROR (Status
)) {
1148 Status
= LegacyBios
->GetBbsInfo (
1155 if (EFI_ERROR (Status
)) {
1159 DevOrder
= BdsLibGetVariableAndSize (
1160 VAR_LEGACY_DEV_ORDER
,
1161 &gEfiLegacyDevOrderVariableGuid
,
1164 if (NULL
== DevOrder
) {
1165 return BdsCreateDevOrder (LocalBbsTable
, BbsCount
);
1168 // First we figure out how many boot devices with same device type respectively
1170 for (Index
= 0; Index
< BbsCount
; Index
++) {
1171 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1172 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1177 switch (LocalBbsTable
[Index
].DeviceType
) {
1190 case BBS_EMBED_NETWORK
:
1194 case BBS_BEV_DEVICE
:
1203 TotalSize
+= (HeaderSize
+ FDCount
* sizeof (UINT16
));
1204 TotalSize
+= (HeaderSize
+ HDCount
* sizeof (UINT16
));
1205 TotalSize
+= (HeaderSize
+ CDCount
* sizeof (UINT16
));
1206 TotalSize
+= (HeaderSize
+ NETCount
* sizeof (UINT16
));
1207 TotalSize
+= (HeaderSize
+ BEVCount
* sizeof (UINT16
));
1209 NewDevOrder
= AllocateZeroPool (TotalSize
);
1210 if (NULL
== NewDevOrder
) {
1211 return EFI_OUT_OF_RESOURCES
;
1220 NewPtr
= NewDevOrder
;
1221 NewPtr
->BbsType
= Ptr
->BbsType
;
1222 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + FDCount
* sizeof (UINT16
));
1223 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1224 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1225 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1226 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_FLOPPY
1231 NewPtr
->Data
[FDIndex
] = Ptr
->Data
[Index
];
1234 NewFDPtr
= NewPtr
->Data
;
1239 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1240 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1241 NewPtr
->BbsType
= Ptr
->BbsType
;
1242 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1243 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1244 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1245 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1246 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1247 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_HARDDISK
1252 NewPtr
->Data
[HDIndex
] = Ptr
->Data
[Index
];
1255 NewHDPtr
= NewPtr
->Data
;
1260 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1261 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1262 NewPtr
->BbsType
= Ptr
->BbsType
;
1263 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1264 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1265 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1266 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1267 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1268 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_CDROM
1273 NewPtr
->Data
[CDIndex
] = Ptr
->Data
[Index
];
1276 NewCDPtr
= NewPtr
->Data
;
1281 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1282 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1283 NewPtr
->BbsType
= Ptr
->BbsType
;
1284 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1285 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1286 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1287 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1288 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1289 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_EMBED_NETWORK
1294 NewPtr
->Data
[NETIndex
] = Ptr
->Data
[Index
];
1297 NewNETPtr
= NewPtr
->Data
;
1302 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1303 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1304 NewPtr
->BbsType
= Ptr
->BbsType
;
1305 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1306 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1307 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1308 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1309 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1310 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_BEV_DEVICE
1315 NewPtr
->Data
[BEVIndex
] = Ptr
->Data
[Index
];
1318 NewBEVPtr
= NewPtr
->Data
;
1320 for (Index
= 0; Index
< BbsCount
; Index
++) {
1321 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1322 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1327 switch (LocalBbsTable
[Index
].DeviceType
) {
1330 NewDevPtr
= NewFDPtr
;
1335 NewDevPtr
= NewHDPtr
;
1340 NewDevPtr
= NewCDPtr
;
1343 case BBS_EMBED_NETWORK
:
1345 NewDevPtr
= NewNETPtr
;
1348 case BBS_BEV_DEVICE
:
1350 NewDevPtr
= NewBEVPtr
;
1358 // at this point we have copied those valid indexes to new buffer
1359 // and we should check if there is any new appeared boot device
1362 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1363 if ((NewDevPtr
[Index2
] & 0xFF) == (UINT16
) Index
) {
1368 if (Index2
== *Idx
) {
1370 // Index2 == *Idx means we didn't find Index
1371 // so Index is a new appeared device's index in BBS table
1372 // insert it before disabled indexes.
1374 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1375 if ((NewDevPtr
[Index2
] & 0xFF00) == 0xFF00) {
1379 CopyMem (&NewDevPtr
[Index2
+ 1], &NewDevPtr
[Index2
], (*Idx
- Index2
) * sizeof (UINT16
));
1380 NewDevPtr
[Index2
] = (UINT16
) (Index
& 0xFF);
1386 FreePool (DevOrder
);
1388 Status
= gRT
->SetVariable (
1389 VAR_LEGACY_DEV_ORDER
,
1390 &gEfiLegacyDevOrderVariableGuid
,
1391 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1395 FreePool (NewDevOrder
);
1401 Set Boot Priority for specified device type.
1403 @param DeviceType The device type.
1404 @param BbsIndex The BBS index to set the highest priority. Ignore when -1.
1405 @param LocalBbsTable The BBS table.
1406 @param Priority The prority table.
1408 @retval EFI_SUCCESS The function completes successfully.
1409 @retval EFI_NOT_FOUND Failed to find device.
1410 @retval EFI_OUT_OF_RESOURCES Failed to get the efi variable of device order.
1414 BdsSetBootPriority4SameTypeDev (
1415 IN UINT16 DeviceType
,
1417 IN OUT BBS_TABLE
*LocalBbsTable
,
1418 IN OUT UINT16
*Priority
1421 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1422 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
1426 DevOrder
= BdsLibGetVariableAndSize (
1427 VAR_LEGACY_DEV_ORDER
,
1428 &gEfiLegacyDevOrderVariableGuid
,
1431 if (NULL
== DevOrder
) {
1432 return EFI_OUT_OF_RESOURCES
;
1435 DevOrderPtr
= DevOrder
;
1436 while ((UINT8
*) DevOrderPtr
< (UINT8
*) DevOrder
+ DevOrderSize
) {
1437 if (DevOrderPtr
->BbsType
== DeviceType
) {
1441 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) ((UINTN
) DevOrderPtr
+ sizeof (BBS_TYPE
) + DevOrderPtr
->Length
);
1444 if ((UINT8
*) DevOrderPtr
>= (UINT8
*) DevOrder
+ DevOrderSize
) {
1445 FreePool (DevOrder
);
1446 return EFI_NOT_FOUND
;
1449 if (BbsIndex
!= (UINTN
) -1) {
1450 LocalBbsTable
[BbsIndex
].BootPriority
= *Priority
;
1454 // If the high byte of the DevIndex is 0xFF, it indicates that this device has been disabled.
1456 for (Index
= 0; Index
< DevOrderPtr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1457 if ((DevOrderPtr
->Data
[Index
] & 0xFF00) == 0xFF00) {
1459 // LocalBbsTable[DevIndex[Index] & 0xFF].BootPriority = BBS_DISABLED_ENTRY;
1461 } else if (DevOrderPtr
->Data
[Index
] != BbsIndex
) {
1462 LocalBbsTable
[DevOrderPtr
->Data
[Index
]].BootPriority
= *Priority
;
1467 FreePool (DevOrder
);
1472 Print the BBS Table.
1474 @param LocalBbsTable The BBS table.
1475 @param BbsCount The count of entry in BBS table.
1479 IN BBS_TABLE
*LocalBbsTable
,
1485 DEBUG ((DEBUG_ERROR
, "\n"));
1486 DEBUG ((DEBUG_ERROR
, " NO Prio bb/dd/ff cl/sc Type Stat segm:offs\n"));
1487 DEBUG ((DEBUG_ERROR
, "=============================================\n"));
1488 for (Idx
= 0; Idx
< BbsCount
; Idx
++) {
1489 if ((LocalBbsTable
[Idx
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1490 (LocalBbsTable
[Idx
].BootPriority
== BBS_DO_NOT_BOOT_FROM
) ||
1491 (LocalBbsTable
[Idx
].BootPriority
== BBS_LOWEST_PRIORITY
)
1498 " %02x: %04x %02x/%02x/%02x %02x/%02x %04x %04x %04x:%04x\n",
1500 (UINTN
) LocalBbsTable
[Idx
].BootPriority
,
1501 (UINTN
) LocalBbsTable
[Idx
].Bus
,
1502 (UINTN
) LocalBbsTable
[Idx
].Device
,
1503 (UINTN
) LocalBbsTable
[Idx
].Function
,
1504 (UINTN
) LocalBbsTable
[Idx
].Class
,
1505 (UINTN
) LocalBbsTable
[Idx
].SubClass
,
1506 (UINTN
) LocalBbsTable
[Idx
].DeviceType
,
1507 (UINTN
) * (UINT16
*) &LocalBbsTable
[Idx
].StatusFlags
,
1508 (UINTN
) LocalBbsTable
[Idx
].BootHandlerSegment
,
1509 (UINTN
) LocalBbsTable
[Idx
].BootHandlerOffset
,
1510 (UINTN
) ((LocalBbsTable
[Idx
].MfgStringSegment
<< 4) + LocalBbsTable
[Idx
].MfgStringOffset
),
1511 (UINTN
) ((LocalBbsTable
[Idx
].DescStringSegment
<< 4) + LocalBbsTable
[Idx
].DescStringOffset
))
1515 DEBUG ((DEBUG_ERROR
, "\n"));
1519 Set the boot priority for BBS entries based on boot option entry and boot order.
1521 @param Entry The boot option is to be checked for refresh BBS table.
1523 @retval EFI_SUCCESS The boot priority for BBS entries is refreshed successfully.
1524 @retval EFI_NOT_FOUND BBS entries can't be found.
1525 @retval EFI_OUT_OF_RESOURCES Failed to get the legacy device boot order.
1529 BdsRefreshBbsTableForBoot (
1530 IN BDS_COMMON_OPTION
*Entry
1537 HDD_INFO
*LocalHddInfo
;
1538 BBS_TABLE
*LocalBbsTable
;
1540 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1544 UINTN BootOrderSize
;
1545 UINT8
*BootOptionVar
;
1546 UINTN BootOptionSize
;
1547 CHAR16 BootOption
[9];
1550 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1552 UINTN DeviceTypeCount
;
1553 UINTN DeviceTypeIndex
;
1557 LocalHddInfo
= NULL
;
1558 LocalBbsTable
= NULL
;
1559 DevType
= BBS_UNKNOWN
;
1561 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1562 if (EFI_ERROR (Status
)) {
1566 LegacyBios
->GetBbsInfo (
1574 // First, set all the present devices' boot priority to BBS_UNPRIORITIZED_ENTRY
1575 // We will set them according to the settings setup by user
1577 for (Index
= 0; Index
< BbsCount
; Index
++) {
1578 if (!((BBS_IGNORE_ENTRY
== LocalBbsTable
[Index
].BootPriority
) ||
1579 (BBS_DO_NOT_BOOT_FROM
== LocalBbsTable
[Index
].BootPriority
) ||
1580 (BBS_LOWEST_PRIORITY
== LocalBbsTable
[Index
].BootPriority
))) {
1581 LocalBbsTable
[Index
].BootPriority
= BBS_UNPRIORITIZED_ENTRY
;
1585 // boot priority always starts at 0
1588 if (Entry
->LoadOptionsSize
== sizeof (BBS_TABLE
) + sizeof (UINT16
)) {
1590 // If Entry stands for a legacy boot option, we prioritize the devices with the same type first.
1592 DevType
= ((BBS_TABLE
*) Entry
->LoadOptions
)->DeviceType
;
1593 BbsIndex
= *(UINT16
*) ((BBS_TABLE
*) Entry
->LoadOptions
+ 1);
1594 Status
= BdsSetBootPriority4SameTypeDev (
1600 if (EFI_ERROR (Status
)) {
1605 // we have to set the boot priority for other BBS entries with different device types
1607 BootOrder
= BdsLibGetVariableAndSize (
1609 &gEfiGlobalVariableGuid
,
1612 DeviceType
= AllocatePool (BootOrderSize
+ sizeof (UINT16
));
1613 ASSERT (DeviceType
!= NULL
);
1615 DeviceType
[0] = DevType
;
1616 DeviceTypeCount
= 1;
1617 for (Index
= 0; ((BootOrder
!= NULL
) && (Index
< BootOrderSize
/ sizeof (UINT16
))); Index
++) {
1618 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
1619 BootOptionVar
= BdsLibGetVariableAndSize (
1621 &gEfiGlobalVariableGuid
,
1624 if (NULL
== BootOptionVar
) {
1628 Ptr
= BootOptionVar
;
1630 Ptr
+= sizeof (UINT32
);
1631 DevPathLen
= *(UINT16
*) Ptr
;
1632 Ptr
+= sizeof (UINT16
);
1633 Ptr
+= StrSize ((UINT16
*) Ptr
);
1634 DevPath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
1635 if (BBS_DEVICE_PATH
!= DevPath
->Type
|| BBS_BBS_DP
!= DevPath
->SubType
) {
1636 FreePool (BootOptionVar
);
1641 DevType
= ((BBS_TABLE
*) Ptr
)->DeviceType
;
1642 for (DeviceTypeIndex
= 0; DeviceTypeIndex
< DeviceTypeCount
; DeviceTypeIndex
++) {
1643 if (DeviceType
[DeviceTypeIndex
] == DevType
) {
1647 if (DeviceTypeIndex
< DeviceTypeCount
) {
1649 // We don't want to process twice for a device type
1651 FreePool (BootOptionVar
);
1655 DeviceType
[DeviceTypeCount
] = DevType
;
1658 Status
= BdsSetBootPriority4SameTypeDev (
1664 FreePool (BootOptionVar
);
1665 if (EFI_ERROR (Status
)) {
1670 FreePool (DeviceType
);
1672 if (BootOrder
!= NULL
) {
1673 FreePool (BootOrder
);
1677 PrintBbsTable (LocalBbsTable
, BbsCount
);
1684 Boot the legacy system with the boot option
1686 @param Option The legacy boot option which have BBS device path
1688 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
1690 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
1694 BdsLibDoLegacyBoot (
1695 IN BDS_COMMON_OPTION
*Option
1699 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1700 EFI_EVENT LegacyBootEvent
;
1702 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1703 if (EFI_ERROR (Status
)) {
1705 // If no LegacyBios protocol we do not support legacy boot
1707 return EFI_UNSUPPORTED
;
1710 // Notes: if we separate the int 19, then we don't need to refresh BBS
1712 BdsRefreshBbsTableForBoot (Option
);
1715 // Write boot to OS performance data for legacy boot.
1719 // Create an event to be signalled when Legacy Boot occurs to write performance data.
1721 Status
= EfiCreateEventLegacyBootEx(
1727 ASSERT_EFI_ERROR (Status
);
1730 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
1731 return LegacyBios
->LegacyBoot (
1733 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
1734 Option
->LoadOptionsSize
,
1740 Internal function to check if the input boot option is a valid EFI NV Boot####.
1742 @param OptionToCheck Boot option to be checked.
1744 @retval TRUE This boot option matches a valid EFI NV Boot####.
1745 @retval FALSE If not.
1749 IsBootOptionValidNVVarialbe (
1750 IN BDS_COMMON_OPTION
*OptionToCheck
1753 LIST_ENTRY TempList
;
1754 BDS_COMMON_OPTION
*BootOption
;
1756 CHAR16 OptionName
[20];
1760 InitializeListHead (&TempList
);
1761 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
1763 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
1764 if (BootOption
== NULL
) {
1769 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
1770 // valid EFI NV Boot####.
1772 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
1773 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
1778 FreePool (BootOption
);
1784 Check whether a USB device match the specified USB Class device path. This
1785 function follows "Load Option Processing" behavior in UEFI specification.
1787 @param UsbIo USB I/O protocol associated with the USB device.
1788 @param UsbClass The USB Class device path to match.
1790 @retval TRUE The USB device match the USB Class device path.
1791 @retval FALSE The USB device does not match the USB Class device path.
1796 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1797 IN USB_CLASS_DEVICE_PATH
*UsbClass
1801 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1802 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1804 UINT8 DeviceSubClass
;
1805 UINT8 DeviceProtocol
;
1807 if ((DevicePathType (UsbClass
) != MESSAGING_DEVICE_PATH
) ||
1808 (DevicePathSubType (UsbClass
) != MSG_USB_CLASS_DP
)){
1813 // Check Vendor Id and Product Id.
1815 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1816 if (EFI_ERROR (Status
)) {
1820 if ((UsbClass
->VendorId
!= 0xffff) &&
1821 (UsbClass
->VendorId
!= DevDesc
.IdVendor
)) {
1825 if ((UsbClass
->ProductId
!= 0xffff) &&
1826 (UsbClass
->ProductId
!= DevDesc
.IdProduct
)) {
1830 DeviceClass
= DevDesc
.DeviceClass
;
1831 DeviceSubClass
= DevDesc
.DeviceSubClass
;
1832 DeviceProtocol
= DevDesc
.DeviceProtocol
;
1833 if (DeviceClass
== 0) {
1835 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
1836 // Protocol in Interface Descriptor instead.
1838 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1839 if (EFI_ERROR (Status
)) {
1843 DeviceClass
= IfDesc
.InterfaceClass
;
1844 DeviceSubClass
= IfDesc
.InterfaceSubClass
;
1845 DeviceProtocol
= IfDesc
.InterfaceProtocol
;
1849 // Check Class, SubClass and Protocol.
1851 if ((UsbClass
->DeviceClass
!= 0xff) &&
1852 (UsbClass
->DeviceClass
!= DeviceClass
)) {
1856 if ((UsbClass
->DeviceSubClass
!= 0xff) &&
1857 (UsbClass
->DeviceSubClass
!= DeviceSubClass
)) {
1861 if ((UsbClass
->DeviceProtocol
!= 0xff) &&
1862 (UsbClass
->DeviceProtocol
!= DeviceProtocol
)) {
1870 Check whether a USB device match the specified USB WWID device path. This
1871 function follows "Load Option Processing" behavior in UEFI specification.
1873 @param UsbIo USB I/O protocol associated with the USB device.
1874 @param UsbWwid The USB WWID device path to match.
1876 @retval TRUE The USB device match the USB WWID device path.
1877 @retval FALSE The USB device does not match the USB WWID device path.
1882 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1883 IN USB_WWID_DEVICE_PATH
*UsbWwid
1887 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1888 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1889 UINT16
*LangIdTable
;
1894 CHAR16
*SerialNumberStr
;
1897 if ((DevicePathType (UsbWwid
) != MESSAGING_DEVICE_PATH
) ||
1898 (DevicePathSubType (UsbWwid
) != MSG_USB_WWID_DP
)){
1903 // Check Vendor Id and Product Id.
1905 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1906 if (EFI_ERROR (Status
)) {
1909 if ((DevDesc
.IdVendor
!= UsbWwid
->VendorId
) ||
1910 (DevDesc
.IdProduct
!= UsbWwid
->ProductId
)) {
1915 // Check Interface Number.
1917 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1918 if (EFI_ERROR (Status
)) {
1921 if (IfDesc
.InterfaceNumber
!= UsbWwid
->InterfaceNumber
) {
1926 // Check Serial Number.
1928 if (DevDesc
.StrSerialNumber
== 0) {
1933 // Get all supported languages.
1937 Status
= UsbIo
->UsbGetSupportedLanguages (UsbIo
, &LangIdTable
, &TableSize
);
1938 if (EFI_ERROR (Status
) || (TableSize
== 0) || (LangIdTable
== NULL
)) {
1943 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
1945 CompareStr
= (CHAR16
*) (UINTN
) (UsbWwid
+ 1);
1946 CompareLen
= (DevicePathNodeLength (UsbWwid
) - sizeof (USB_WWID_DEVICE_PATH
)) / sizeof (CHAR16
);
1947 if (CompareStr
[CompareLen
- 1] == L
'\0') {
1952 // Compare serial number in each supported language.
1954 for (Index
= 0; Index
< TableSize
/ sizeof (UINT16
); Index
++) {
1955 SerialNumberStr
= NULL
;
1956 Status
= UsbIo
->UsbGetStringDescriptor (
1959 DevDesc
.StrSerialNumber
,
1962 if (EFI_ERROR (Status
) || (SerialNumberStr
== NULL
)) {
1966 Length
= StrLen (SerialNumberStr
);
1967 if ((Length
>= CompareLen
) &&
1968 (CompareMem (SerialNumberStr
+ Length
- CompareLen
, CompareStr
, CompareLen
* sizeof (CHAR16
)) == 0)) {
1969 FreePool (SerialNumberStr
);
1973 FreePool (SerialNumberStr
);
1980 Find a USB device path which match the specified short-form device path start
1981 with USB Class or USB WWID device path and load the boot file then return the
1982 image handle. If ParentDevicePath is NULL, this function will search in all USB
1983 devices of the platform. If ParentDevicePath is not NULL,this function will only
1984 search in its child devices.
1986 @param ParentDevicePath The device path of the parent.
1987 @param ShortFormDevicePath The USB Class or USB WWID device path to match.
1989 @return The image Handle if find load file from specified short-form device path
1990 or NULL if not found.
1995 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
1996 IN EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
2000 UINTN UsbIoHandleCount
;
2001 EFI_HANDLE
*UsbIoHandleBuffer
;
2002 EFI_DEVICE_PATH_PROTOCOL
*UsbIoDevicePath
;
2003 EFI_USB_IO_PROTOCOL
*UsbIo
;
2007 EFI_HANDLE ImageHandle
;
2009 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
2010 EFI_DEVICE_PATH_PROTOCOL
*NextDevicePath
;
2012 FullDevicePath
= NULL
;
2016 // Get all UsbIo Handles.
2018 UsbIoHandleCount
= 0;
2019 UsbIoHandleBuffer
= NULL
;
2020 Status
= gBS
->LocateHandleBuffer (
2022 &gEfiUsbIoProtocolGuid
,
2027 if (EFI_ERROR (Status
) || (UsbIoHandleCount
== 0) || (UsbIoHandleBuffer
== NULL
)) {
2031 ParentSize
= (ParentDevicePath
== NULL
) ? 0 : GetDevicePathSize (ParentDevicePath
);
2032 for (Index
= 0; Index
< UsbIoHandleCount
; Index
++) {
2034 // Get the Usb IO interface.
2036 Status
= gBS
->HandleProtocol(
2037 UsbIoHandleBuffer
[Index
],
2038 &gEfiUsbIoProtocolGuid
,
2041 if (EFI_ERROR (Status
)) {
2045 UsbIoDevicePath
= DevicePathFromHandle (UsbIoHandleBuffer
[Index
]);
2046 if (UsbIoDevicePath
== NULL
) {
2050 if (ParentDevicePath
!= NULL
) {
2052 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
2054 Size
= GetDevicePathSize (UsbIoDevicePath
);
2055 if ((Size
< ParentSize
) ||
2056 (CompareMem (UsbIoDevicePath
, ParentDevicePath
, ParentSize
- END_DEVICE_PATH_LENGTH
) != 0)) {
2061 if (BdsMatchUsbClass (UsbIo
, (USB_CLASS_DEVICE_PATH
*) ShortFormDevicePath
) ||
2062 BdsMatchUsbWwid (UsbIo
, (USB_WWID_DEVICE_PATH
*) ShortFormDevicePath
)) {
2064 // Try to find if there is the boot file in this DevicePath
2066 NextDevicePath
= NextDevicePathNode (ShortFormDevicePath
);
2067 if (!IsDevicePathEnd (NextDevicePath
)) {
2068 FullDevicePath
= AppendDevicePath (UsbIoDevicePath
, NextDevicePath
);
2070 // Connect the full device path, so that Simple File System protocol
2071 // could be installed for this USB device.
2073 BdsLibConnectDevicePath (FullDevicePath
);
2074 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2075 Status
= gBS
->LoadImage (
2083 FreePool (FullDevicePath
);
2085 FullDevicePath
= UsbIoDevicePath
;
2086 Status
= EFI_NOT_FOUND
;
2090 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2091 // and load the image according to the default boot behavior for removable device.
2093 if (EFI_ERROR (Status
)) {
2095 // check if there is a bootable removable media could be found in this device path ,
2096 // and get the bootable media handle
2098 Handle
= BdsLibGetBootableHandle(UsbIoDevicePath
);
2099 if (Handle
== NULL
) {
2103 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2104 // machinename is ia32, ia64, x64, ...
2106 FullDevicePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2107 if (FullDevicePath
!= NULL
) {
2108 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2109 Status
= gBS
->LoadImage (
2117 if (EFI_ERROR (Status
)) {
2119 // The DevicePath failed, and it's not a valid
2120 // removable media device.
2132 FreePool (UsbIoHandleBuffer
);
2137 Expand USB Class or USB WWID device path node to be full device path of a USB
2138 device in platform then load the boot file on this full device path and return the
2141 This function support following 4 cases:
2142 1) Boot Option device path starts with a USB Class or USB WWID device path,
2143 and there is no Media FilePath device path in the end.
2144 In this case, it will follow Removable Media Boot Behavior.
2145 2) Boot Option device path starts with a USB Class or USB WWID device path,
2146 and ended with Media FilePath device path.
2147 3) Boot Option device path starts with a full device path to a USB Host Controller,
2148 contains a USB Class or USB WWID device path node, while not ended with Media
2149 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
2150 4) Boot Option device path starts with a full device path to a USB Host Controller,
2151 contains a USB Class or USB WWID device path node, and ended with Media
2152 FilePath device path.
2154 @param DevicePath The Boot Option device path.
2156 @return The image handle of boot file, or NULL if there is no boot file found in
2157 the specified USB Class or USB WWID device path.
2161 BdsExpandUsbShortFormDevicePath (
2162 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
2165 EFI_HANDLE
*ImageHandle
;
2166 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2167 EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
;
2170 // Search for USB Class or USB WWID device path node.
2172 ShortFormDevicePath
= NULL
;
2174 TempDevicePath
= DevicePath
;
2175 while (!IsDevicePathEnd (TempDevicePath
)) {
2176 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
2177 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
2178 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
2179 ShortFormDevicePath
= TempDevicePath
;
2182 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2185 if (ShortFormDevicePath
== NULL
) {
2187 // No USB Class or USB WWID device path node found, do nothing.
2192 if (ShortFormDevicePath
== DevicePath
) {
2194 // Boot Option device path starts with USB Class or USB WWID device path.
2196 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2197 if (ImageHandle
== NULL
) {
2199 // Failed to find a match in existing devices, connect the short form USB
2200 // device path and try again.
2202 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath
);
2203 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2207 // Boot Option device path contains USB Class or USB WWID device path node.
2211 // Prepare the parent device path for search.
2213 TempDevicePath
= DuplicateDevicePath (DevicePath
);
2214 ASSERT (TempDevicePath
!= NULL
);
2215 SetDevicePathEndNode (((UINT8
*) TempDevicePath
) + ((UINTN
) ShortFormDevicePath
- (UINTN
) DevicePath
));
2218 // The USB Host Controller device path is already in Boot Option device path
2219 // and USB Bus driver already support RemainingDevicePath starts with USB
2220 // Class or USB WWID device path, so just search in existing USB devices and
2221 // doesn't perform ConnectController here.
2223 ImageHandle
= BdsFindUsbDevice (TempDevicePath
, ShortFormDevicePath
);
2224 FreePool (TempDevicePath
);
2231 Process the boot option follow the UEFI specification and
2232 special treat the legacy boot option with BBS_DEVICE_PATH.
2234 @param Option The boot option need to be processed
2235 @param DevicePath The device path which describe where to load the
2236 boot image or the legacy BBS device path to boot
2238 @param ExitDataSize The size of exit data.
2239 @param ExitData Data returned when Boot image failed.
2241 @retval EFI_SUCCESS Boot from the input boot option successfully.
2242 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2247 BdsLibBootViaBootOption (
2248 IN BDS_COMMON_OPTION
*Option
,
2249 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2250 OUT UINTN
*ExitDataSize
,
2251 OUT CHAR16
**ExitData OPTIONAL
2255 EFI_STATUS StatusLogo
;
2257 EFI_HANDLE ImageHandle
;
2258 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
2259 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
2260 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
2261 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
2262 LIST_ENTRY TempBootLists
;
2263 EFI_BOOT_LOGO_PROTOCOL
*BootLogo
;
2269 // Notes: this code can be remove after the s3 script table
2270 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
2271 // EVT_SIGNAL_LEGACY_BOOT
2273 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
2274 if (!EFI_ERROR (Status
)) {
2275 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
2278 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
2281 WorkingDevicePath
= NULL
;
2282 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2283 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
2284 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
2285 (HARDDRIVE_DEVICE_PATH
*)DevicePath
2287 if (WorkingDevicePath
!= NULL
) {
2288 DevicePath
= WorkingDevicePath
;
2295 if (IsBootOptionValidNVVarialbe (Option
)) {
2297 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
2298 // In this case, "BootCurrent" is not created.
2299 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
2301 SetVariableAndReportStatusCodeOnError (
2303 &gEfiGlobalVariableGuid
,
2304 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2306 &Option
->BootCurrent
2311 // Report Status Code to indicate ReadyToBoot event will be signalled
2313 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT
));
2316 // Signal the EVT_SIGNAL_READY_TO_BOOT event
2318 EfiSignalEventReadyToBoot();
2321 // Expand USB Class or USB WWID device path node to be full device path of a USB
2322 // device in platform then load the boot file on this full device path and get the
2325 ImageHandle
= BdsExpandUsbShortFormDevicePath (DevicePath
);
2328 // Adjust the different type memory page number just before booting
2329 // and save the updated info into the variable for next boot to use
2331 BdsSetMemoryTypeInformationVariable ();
2334 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.
2335 // Here get the ImageHandle for the non USB class or WWID device path.
2337 if (ImageHandle
== NULL
) {
2338 ASSERT (Option
->DevicePath
!= NULL
);
2339 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
2340 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
2343 // Check to see if we should legacy BOOT. If yes then do the legacy boot
2345 return BdsLibDoLegacyBoot (Option
);
2349 // If the boot option point to Internal FV shell, make sure it is valid
2351 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
2352 if (!EFI_ERROR(Status
)) {
2353 if (Option
->DevicePath
!= NULL
) {
2354 FreePool(Option
->DevicePath
);
2356 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
2357 ASSERT(Option
->DevicePath
!= NULL
);
2358 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
2360 // Update the shell boot option
2362 InitializeListHead (&TempBootLists
);
2363 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
2366 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
2368 FreePool (DevicePath
);
2369 DevicePath
= Option
->DevicePath
;
2374 if (Option
->Description
== NULL
) {
2375 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
2377 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
2383 // Report status code for OS Loader LoadImage.
2385 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2386 Status
= gBS
->LoadImage (
2396 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2397 // and load the image according to the default boot behavior for removable device.
2399 if (EFI_ERROR (Status
)) {
2401 // check if there is a bootable removable media could be found in this device path ,
2402 // and get the bootable media handle
2404 Handle
= BdsLibGetBootableHandle(DevicePath
);
2405 if (Handle
!= NULL
) {
2407 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2408 // machinename is ia32, ia64, x64, ...
2410 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2411 if (FilePath
!= NULL
) {
2412 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2413 Status
= gBS
->LoadImage (
2426 // Provide the image with it's load options
2428 if ((ImageHandle
== NULL
) || (EFI_ERROR(Status
))) {
2430 // Report Status Code to indicate that the failure to load boot option
2432 REPORT_STATUS_CODE (
2433 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2434 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR
)
2439 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
2440 ASSERT_EFI_ERROR (Status
);
2442 if (Option
->LoadOptionsSize
!= 0) {
2443 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
2444 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
2448 // Clean to NULL because the image is loaded directly from the firmwares boot manager.
2450 ImageInfo
->ParentHandle
= NULL
;
2453 // Before calling the image, enable the Watchdog Timer for
2454 // the 5 Minute period
2456 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
2459 // Write boot to OS performance data for UEFI boot
2462 BmEndOfBdsPerfCode (NULL
, NULL
);
2466 // Report status code for OS Loader StartImage.
2468 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderStart
));
2470 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
2471 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
2472 if (EFI_ERROR (Status
)) {
2474 // Report Status Code to indicate that boot failure
2476 REPORT_STATUS_CODE (
2477 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2478 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED
)
2483 // Clear the Watchdog Timer after the image returns
2485 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
2489 // Set Logo status invalid after trying one boot option
2492 StatusLogo
= gBS
->LocateProtocol (&gEfiBootLogoProtocolGuid
, NULL
, (VOID
**) &BootLogo
);
2493 if (!EFI_ERROR (StatusLogo
) && (BootLogo
!= NULL
)) {
2494 BootLogo
->SetBootLogo (BootLogo
, NULL
, 0, 0, 0, 0);
2498 // Clear Boot Current
2499 // Deleting variable with current implementation shouldn't fail.
2503 &gEfiGlobalVariableGuid
,
2504 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2514 Expand a device path that starts with a hard drive media device path node to be a
2515 full device path that includes the full hardware path to the device. We need
2516 to do this so it can be booted. As an optimization the front match (the part point
2517 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
2518 so a connect all is not required on every boot. All successful history device path
2519 which point to partition node (the front part) will be saved.
2521 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
2522 drive media device path.
2523 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
2527 EFI_DEVICE_PATH_PROTOCOL
*
2529 BdsExpandPartitionPartialDevicePathToFull (
2530 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2534 UINTN BlockIoHandleCount
;
2535 EFI_HANDLE
*BlockIoBuffer
;
2536 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
2537 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
2538 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2541 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
2542 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
2543 UINTN CachedDevicePathSize
;
2544 BOOLEAN DeviceExist
;
2546 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
2549 FullDevicePath
= NULL
;
2551 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2552 // If exist, search the front path which point to partition node in the variable instants.
2553 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system
2556 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2557 &gHdBootDevicePathVariablGuid
,
2558 (VOID
**) &CachedDevicePath
,
2559 &CachedDevicePathSize
2563 // Delete the invalid HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2565 if ((CachedDevicePath
!= NULL
) && !IsDevicePathValid (CachedDevicePath
, CachedDevicePathSize
)) {
2566 FreePool (CachedDevicePath
);
2567 CachedDevicePath
= NULL
;
2568 Status
= gRT
->SetVariable (
2569 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2570 &gHdBootDevicePathVariablGuid
,
2575 ASSERT_EFI_ERROR (Status
);
2578 if (CachedDevicePath
!= NULL
) {
2579 TempNewDevicePath
= CachedDevicePath
;
2580 DeviceExist
= FALSE
;
2584 // Check every instance of the variable
2585 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
2586 // partial partition boot option. Second, check whether the instance could be connected.
2588 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
2589 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
2591 // Connect the device path instance, the device path point to hard drive media device path node
2592 // e.g. ACPI() /PCI()/ATA()/Partition()
2594 Status
= BdsLibConnectDevicePath (Instance
);
2595 if (!EFI_ERROR (Status
)) {
2601 // Come here means the first instance is not matched
2605 } while (TempNewDevicePath
!= NULL
);
2609 // Find the matched device path.
2610 // Append the file path information from the boot option and return the fully expanded device path.
2612 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2613 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
2616 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.
2620 // First delete the matched instance.
2622 TempNewDevicePath
= CachedDevicePath
;
2623 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
2624 FreePool (TempNewDevicePath
);
2627 // Second, append the remaining path after the matched instance
2629 TempNewDevicePath
= CachedDevicePath
;
2630 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
2631 FreePool (TempNewDevicePath
);
2633 // Save the matching Device Path so we don't need to do a connect all next time
2634 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2636 Status
= gRT
->SetVariable (
2637 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2638 &gHdBootDevicePathVariablGuid
,
2639 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2640 GetDevicePathSize (CachedDevicePath
),
2645 FreePool (Instance
);
2646 FreePool (CachedDevicePath
);
2647 return FullDevicePath
;
2652 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need
2653 // to search all devices in the system for a matched partition
2655 BdsLibConnectAllDriversToAllControllers ();
2656 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
2657 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
2659 // If there was an error or there are no device handles that support
2660 // the BLOCK_IO Protocol, then return.
2665 // Loop through all the device handles that support the BLOCK_IO Protocol
2667 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
2669 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
2670 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
2674 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
2676 // Find the matched partition device path
2678 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2679 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
2682 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2684 if (CachedDevicePath
!= NULL
) {
2686 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2688 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
2689 TempNewDevicePath
= CachedDevicePath
;
2690 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
2691 FreePool(TempNewDevicePath
);
2694 if (CachedDevicePath
!= NULL
) {
2695 TempNewDevicePath
= CachedDevicePath
;
2696 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
2697 FreePool(TempNewDevicePath
);
2699 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2703 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
2704 // If the user try to boot many OS in different HDs or partitions, in theory,
2705 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.
2708 ASSERT (CachedDevicePath
!= NULL
);
2709 TempNewDevicePath
= CachedDevicePath
;
2710 while (!IsDevicePathEnd (TempNewDevicePath
)) {
2711 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2713 // Parse one instance
2715 while (!IsDevicePathEndType (TempNewDevicePath
)) {
2716 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2720 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
2722 if (InstanceNum
>= 12) {
2723 SetDevicePathEndNode (TempNewDevicePath
);
2728 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2732 // Save the matching Device Path so we don't need to do a connect all next time
2733 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2735 Status
= gRT
->SetVariable (
2736 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2737 &gHdBootDevicePathVariablGuid
,
2738 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2739 GetDevicePathSize (CachedDevicePath
),
2747 if (CachedDevicePath
!= NULL
) {
2748 FreePool (CachedDevicePath
);
2750 if (BlockIoBuffer
!= NULL
) {
2751 FreePool (BlockIoBuffer
);
2753 return FullDevicePath
;
2757 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
2758 instances, has the same partition node with HardDriveDevicePath device path
2760 @param BlockIoDevicePath Multi device path instances which need to check
2761 @param HardDriveDevicePath A device path which starts with a hard drive media
2764 @retval TRUE There is a matched device path instance.
2765 @retval FALSE There is no matched device path instance.
2770 MatchPartitionDevicePathNode (
2771 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
2772 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2775 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
2776 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2778 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
2780 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
2785 // Make PreviousDevicePath == the device path node before the end node
2787 DevicePath
= BlockIoDevicePath
;
2788 BlockIoHdDevicePathNode
= NULL
;
2791 // find the partition device path node
2793 while (!IsDevicePathEnd (DevicePath
)) {
2794 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2795 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
2797 BlockIoHdDevicePathNode
= DevicePath
;
2801 DevicePath
= NextDevicePathNode (DevicePath
);
2804 if (BlockIoHdDevicePathNode
== NULL
) {
2808 // See if the harddrive device path in blockio matches the orig Hard Drive Node
2810 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
2814 // Check for the match
2816 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
2817 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
2818 switch (TmpHdPath
->SignatureType
) {
2819 case SIGNATURE_TYPE_GUID
:
2820 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
2822 case SIGNATURE_TYPE_MBR
:
2823 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
2835 Delete the boot option associated with the handle passed in.
2837 @param Handle The handle which present the device path to create
2840 @retval EFI_SUCCESS Delete the boot option success
2841 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2842 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2843 @retval Other Error return value from SetVariable()
2847 BdsLibDeleteOptionFromHandle (
2848 IN EFI_HANDLE Handle
2852 UINT8
*BootOptionVar
;
2853 UINTN BootOrderSize
;
2854 UINTN BootOptionSize
;
2857 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2858 UINTN DevicePathSize
;
2859 UINTN OptionDevicePathSize
;
2860 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2861 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2864 Status
= EFI_SUCCESS
;
2869 // Check "BootOrder" variable, if no, means there is no any boot order.
2871 BootOrder
= BdsLibGetVariableAndSize (
2873 &gEfiGlobalVariableGuid
,
2876 if (BootOrder
== NULL
) {
2877 return EFI_NOT_FOUND
;
2881 // Convert device handle to device path protocol instance
2883 DevicePath
= DevicePathFromHandle (Handle
);
2884 if (DevicePath
== NULL
) {
2885 return EFI_NOT_FOUND
;
2887 DevicePathSize
= GetDevicePathSize (DevicePath
);
2890 // Loop all boot order variable and find the matching device path
2893 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2894 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2895 BootOptionVar
= BdsLibGetVariableAndSize (
2897 &gEfiGlobalVariableGuid
,
2901 if (BootOptionVar
== NULL
) {
2902 FreePool (BootOrder
);
2903 return EFI_OUT_OF_RESOURCES
;
2906 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2907 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2908 FreePool (BootOptionVar
);
2913 TempPtr
= BootOptionVar
;
2914 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
2915 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
2916 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
2917 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
2920 // Check whether the device path match
2922 if ((OptionDevicePathSize
== DevicePathSize
) &&
2923 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
2924 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2925 FreePool (BootOptionVar
);
2929 FreePool (BootOptionVar
);
2934 // Adjust number of boot option for "BootOrder" variable.
2936 Status
= gRT
->SetVariable (
2938 &gEfiGlobalVariableGuid
,
2939 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2944 // Shrinking variable with existing variable implementation shouldn't fail.
2946 ASSERT_EFI_ERROR (Status
);
2948 FreePool (BootOrder
);
2955 Delete all invalid EFI boot options.
2957 @retval EFI_SUCCESS Delete all invalid boot option success
2958 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
2959 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2960 @retval Other Error return value from SetVariable()
2964 BdsDeleteAllInvalidEfiBootOption (
2969 UINT8
*BootOptionVar
;
2970 UINTN BootOrderSize
;
2971 UINTN BootOptionSize
;
2975 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2976 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2978 CHAR16
*Description
;
2981 Status
= EFI_SUCCESS
;
2984 OptionDevicePath
= NULL
;
2989 // Check "BootOrder" variable firstly, this variable hold the number of boot options
2991 BootOrder
= BdsLibGetVariableAndSize (
2993 &gEfiGlobalVariableGuid
,
2996 if (NULL
== BootOrder
) {
2997 return EFI_NOT_FOUND
;
3001 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
3002 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
3003 BootOptionVar
= BdsLibGetVariableAndSize (
3005 &gEfiGlobalVariableGuid
,
3008 if (NULL
== BootOptionVar
) {
3009 FreePool (BootOrder
);
3010 return EFI_OUT_OF_RESOURCES
;
3013 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
3016 TempPtr
= BootOptionVar
;
3017 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
3018 Description
= (CHAR16
*) TempPtr
;
3019 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
3020 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
3023 // Skip legacy boot option (BBS boot device)
3025 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
3026 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
3027 FreePool (BootOptionVar
);
3033 if (Corrupted
|| !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
3035 // Delete this invalid boot option "Boot####"
3037 Status
= gRT
->SetVariable (
3039 &gEfiGlobalVariableGuid
,
3040 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3045 // Deleting variable with current variable implementation shouldn't fail.
3047 ASSERT_EFI_ERROR (Status
);
3049 // Mark this boot option in boot order as deleted
3051 BootOrder
[Index
] = 0xffff;
3055 FreePool (BootOptionVar
);
3060 // Adjust boot order array
3063 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
3064 if (BootOrder
[Index
] != 0xffff) {
3065 BootOrder
[Index2
] = BootOrder
[Index
];
3069 Status
= gRT
->SetVariable (
3071 &gEfiGlobalVariableGuid
,
3072 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3073 Index2
* sizeof (UINT16
),
3077 // Shrinking variable with current variable implementation shouldn't fail.
3079 ASSERT_EFI_ERROR (Status
);
3081 FreePool (BootOrder
);
3088 For EFI boot option, BDS separate them as six types:
3089 1. Network - The boot option points to the SimpleNetworkProtocol device.
3090 Bds will try to automatically create this type boot option when enumerate.
3091 2. Shell - The boot option points to internal flash shell.
3092 Bds will try to automatically create this type boot option when enumerate.
3093 3. Removable BlockIo - The boot option only points to the removable media
3094 device, like USB flash disk, DVD, Floppy etc.
3095 These device should contain a *removable* blockIo
3096 protocol in their device handle.
3097 Bds will try to automatically create this type boot option
3099 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
3101 These device should contain a *fixed* blockIo
3102 protocol in their device handle.
3103 BDS will skip fixed blockIo devices, and NOT
3104 automatically create boot option for them. But BDS
3105 will help to delete those fixed blockIo boot option,
3106 whose description rule conflict with other auto-created
3108 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
3109 has SimpleFileSystem Protocol, but has no blockio
3110 protocol. These devices do not offer blockIo
3111 protocol, but BDS still can get the
3112 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
3114 6. File - The boot option points to a file. These boot options are usually
3115 created by user manually or OS loader. BDS will not delete or modify
3118 This function will enumerate all possible boot device in the system, and
3119 automatically create boot options for Network, Shell, Removable BlockIo,
3120 and Non-BlockIo Simplefile devices.
3121 It will only execute once of every boot.
3123 @param BdsBootOptionList The header of the link list which indexed all
3124 current boot options
3126 @retval EFI_SUCCESS Finished all the boot device enumerate and create
3127 the boot option base on that boot device
3129 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
3133 BdsLibEnumerateAllBootOption (
3134 IN OUT LIST_ENTRY
*BdsBootOptionList
3138 UINT16 FloppyNumber
;
3139 UINT16 HarddriveNumber
;
3144 UINT16 NonBlockNumber
;
3145 UINTN NumberBlockIoHandles
;
3146 EFI_HANDLE
*BlockIoHandles
;
3147 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
3148 BOOLEAN Removable
[2];
3149 UINTN RemovableIndex
;
3151 UINTN NumOfLoadFileHandles
;
3152 EFI_HANDLE
*LoadFileHandles
;
3153 UINTN FvHandleCount
;
3154 EFI_HANDLE
*FvHandleBuffer
;
3155 EFI_FV_FILETYPE Type
;
3157 EFI_FV_FILE_ATTRIBUTES Attributes
;
3158 UINT32 AuthenticationStatus
;
3159 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
3160 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3161 UINTN DevicePathType
;
3163 EFI_HANDLE
*FileSystemHandles
;
3164 UINTN NumberFileSystemHandles
;
3166 EFI_IMAGE_DOS_HEADER DosHeader
;
3169 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3170 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3173 EFI_HANDLE
*NetworkHandles
;
3177 HarddriveNumber
= 0;
3184 ZeroMem (Buffer
, sizeof (Buffer
));
3187 // If the boot device enumerate happened, just get the boot
3188 // device from the boot order variable
3190 if (mEnumBootDevice
) {
3191 GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME
, &gLastEnumLangGuid
, (VOID
**)&LastLang
, NULL
);
3192 GetEfiGlobalVariable2 (L
"PlatformLang", (VOID
**)&PlatLang
, NULL
);
3193 ASSERT (PlatLang
!= NULL
);
3194 if ((LastLang
!= NULL
) && (AsciiStrCmp (LastLang
, PlatLang
) == 0)) {
3195 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3196 FreePool (LastLang
);
3197 FreePool (PlatLang
);
3200 Status
= gRT
->SetVariable (
3201 LAST_ENUM_LANGUAGE_VARIABLE_NAME
,
3203 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3204 AsciiStrSize (PlatLang
),
3208 // Failure to set the variable only impacts the performance next time enumerating the boot options.
3211 if (LastLang
!= NULL
) {
3212 FreePool (LastLang
);
3214 FreePool (PlatLang
);
3219 // Notes: this dirty code is to get the legacy boot option from the
3220 // BBS table and create to variable as the EFI boot option, it should
3221 // be removed after the CSM can provide legacy boot option directly
3223 REFRESH_LEGACY_BOOT_OPTIONS
;
3226 // Delete invalid boot option
3228 BdsDeleteAllInvalidEfiBootOption ();
3231 // Parse removable media followed by fixed media.
3232 // The Removable[] array is used by the for-loop below to create removable media boot options
3233 // at first, and then to create fixed media boot options.
3235 Removable
[0] = FALSE
;
3236 Removable
[1] = TRUE
;
3238 gBS
->LocateHandleBuffer (
3240 &gEfiBlockIoProtocolGuid
,
3242 &NumberBlockIoHandles
,
3246 for (RemovableIndex
= 0; RemovableIndex
< 2; RemovableIndex
++) {
3247 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
3248 Status
= gBS
->HandleProtocol (
3249 BlockIoHandles
[Index
],
3250 &gEfiBlockIoProtocolGuid
,
3254 // skip the logical partition
3256 if (EFI_ERROR (Status
) || BlkIo
->Media
->LogicalPartition
) {
3261 // firstly fixed block io then the removable block io
3263 if (BlkIo
->Media
->RemovableMedia
== Removable
[RemovableIndex
]) {
3266 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
3267 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
3269 switch (DevicePathType
) {
3270 case BDS_EFI_ACPI_FLOPPY_BOOT
:
3271 if (FloppyNumber
!= 0) {
3272 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
3274 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
3276 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3281 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.
3283 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
3284 case BDS_EFI_MESSAGE_SATA_BOOT
:
3285 if (BlkIo
->Media
->RemovableMedia
) {
3286 if (CdromNumber
!= 0) {
3287 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
3289 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
3293 if (HarddriveNumber
!= 0) {
3294 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)), HarddriveNumber
);
3296 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)));
3300 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
3301 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3304 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
3305 if (UsbNumber
!= 0) {
3306 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
3308 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
3310 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3314 case BDS_EFI_MESSAGE_SCSI_BOOT
:
3315 if (ScsiNumber
!= 0) {
3316 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
3318 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
3320 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3324 case BDS_EFI_MESSAGE_MISC_BOOT
:
3326 if (MiscNumber
!= 0) {
3327 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
3329 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
3331 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3338 if (NumberBlockIoHandles
!= 0) {
3339 FreePool (BlockIoHandles
);
3343 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
3346 gBS
->LocateHandleBuffer (
3348 &gEfiSimpleFileSystemProtocolGuid
,
3350 &NumberFileSystemHandles
,
3353 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
3354 Status
= gBS
->HandleProtocol (
3355 FileSystemHandles
[Index
],
3356 &gEfiBlockIoProtocolGuid
,
3359 if (!EFI_ERROR (Status
)) {
3361 // Skip if the file system handle supports a BlkIo protocol,
3367 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
3368 // machinename is ia32, ia64, x64, ...
3370 Hdr
.Union
= &HdrData
;
3372 Status
= BdsLibGetImageHeader (
3373 FileSystemHandles
[Index
],
3374 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3378 if (!EFI_ERROR (Status
) &&
3379 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3380 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3386 // No such file or the file is not a EFI application, delete this boot option
3388 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
3390 if (NonBlockNumber
!= 0) {
3391 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
3393 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
3395 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
3400 if (NumberFileSystemHandles
!= 0) {
3401 FreePool (FileSystemHandles
);
3405 // Parse Network Boot Device
3407 NumOfLoadFileHandles
= 0;
3409 // Search Load File protocol for PXE boot option.
3411 gBS
->LocateHandleBuffer (
3413 &gEfiLoadFileProtocolGuid
,
3415 &NumOfLoadFileHandles
,
3419 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
3422 //Locate EFI_DEVICE_PATH_PROTOCOL to dynamically get IPv4/IPv6 protocol information.
3425 Status
= gBS
->HandleProtocol (
3426 LoadFileHandles
[Index
],
3427 &gEfiDevicePathProtocolGuid
,
3428 (VOID
**) &DevicePath
3431 ASSERT_EFI_ERROR (Status
);
3433 while (!IsDevicePathEnd (DevicePath
)) {
3434 if ((DevicePath
->Type
== MESSAGING_DEVICE_PATH
) &&
3435 (DevicePath
->SubType
== MSG_IPv4_DP
)) {
3438 //Get handle infomation
3441 NetworkHandles
= NULL
;
3442 Status
= gBS
->LocateHandle (
3444 &gEfiSimpleNetworkProtocolGuid
,
3450 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3451 NetworkHandles
= AllocateZeroPool(BufferSize
);
3452 if (NetworkHandles
== NULL
) {
3453 return (EFI_OUT_OF_RESOURCES
);
3455 Status
= gBS
->LocateHandle(
3457 &gEfiSimpleNetworkProtocolGuid
,
3465 //Get the MAC string
3467 Status
= NetLibGetMacString (
3472 if (EFI_ERROR (Status
)) {
3475 IPverStr
= L
" IPv4";
3476 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s%s%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)),MacStr
,IPverStr
);
3479 if((DevicePath
->Type
== MESSAGING_DEVICE_PATH
) &&
3480 (DevicePath
->SubType
== MSG_IPv6_DP
)) {
3483 //Get handle infomation
3486 NetworkHandles
= NULL
;
3487 Status
= gBS
->LocateHandle (
3489 &gEfiSimpleNetworkProtocolGuid
,
3495 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3496 NetworkHandles
= AllocateZeroPool(BufferSize
);
3497 if (NetworkHandles
== NULL
) {
3498 return (EFI_OUT_OF_RESOURCES
);
3500 Status
= gBS
->LocateHandle(
3502 &gEfiSimpleNetworkProtocolGuid
,
3510 //Get the MAC string
3512 Status
= NetLibGetMacString (
3517 if (EFI_ERROR (Status
)) {
3520 IPverStr
= L
" IPv6";
3521 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s%s%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)),MacStr
,IPverStr
);
3524 DevicePath
= NextDevicePathNode (DevicePath
);
3527 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
3530 if (NumOfLoadFileHandles
!= 0) {
3531 FreePool (LoadFileHandles
);
3535 // Check if we have on flash shell
3537 gBS
->LocateHandleBuffer (
3539 &gEfiFirmwareVolume2ProtocolGuid
,
3544 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
3545 gBS
->HandleProtocol (
3546 FvHandleBuffer
[Index
],
3547 &gEfiFirmwareVolume2ProtocolGuid
,
3551 Status
= Fv
->ReadFile (
3553 PcdGetPtr(PcdShellFile
),
3558 &AuthenticationStatus
3560 if (EFI_ERROR (Status
)) {
3562 // Skip if no shell file in the FV
3567 // Build the shell boot option
3569 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
3572 if (FvHandleCount
!= 0) {
3573 FreePool (FvHandleBuffer
);
3576 // Make sure every boot only have one time
3577 // boot device enumerate
3579 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3580 mEnumBootDevice
= TRUE
;
3586 Build the boot option with the handle parsed in
3588 @param Handle The handle which present the device path to create
3590 @param BdsBootOptionList The header of the link list which indexed all
3591 current boot options
3592 @param String The description of the boot option.
3597 BdsLibBuildOptionFromHandle (
3598 IN EFI_HANDLE Handle
,
3599 IN LIST_ENTRY
*BdsBootOptionList
,
3603 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3605 DevicePath
= DevicePathFromHandle (Handle
);
3608 // Create and register new boot option
3610 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
3615 Build the on flash shell boot option with the handle parsed in.
3617 @param Handle The handle which present the device path to create
3618 on flash shell boot option
3619 @param BdsBootOptionList The header of the link list which indexed all
3620 current boot options
3625 BdsLibBuildOptionFromShell (
3626 IN EFI_HANDLE Handle
,
3627 IN OUT LIST_ENTRY
*BdsBootOptionList
3630 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3631 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
3633 DevicePath
= DevicePathFromHandle (Handle
);
3636 // Build the shell device path
3638 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
3640 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
3643 // Create and register the shell boot option
3645 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
3650 Boot from the UEFI spec defined "BootNext" variable.
3663 BDS_COMMON_OPTION
*BootOption
;
3664 LIST_ENTRY TempList
;
3669 // Init the boot option name buffer and temp link list
3671 InitializeListHead (&TempList
);
3672 ZeroMem (Buffer
, sizeof (Buffer
));
3674 BootNext
= BdsLibGetVariableAndSize (
3676 &gEfiGlobalVariableGuid
,
3681 // Clear the boot next variable first
3683 if (BootNext
!= NULL
) {
3684 Status
= gRT
->SetVariable (
3686 &gEfiGlobalVariableGuid
,
3687 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3692 // Deleting variable with current variable implementation shouldn't fail.
3694 ASSERT_EFI_ERROR (Status
);
3697 // Start to build the boot option and try to boot
3699 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
3700 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
3701 ASSERT (BootOption
!= NULL
);
3702 BdsLibConnectDevicePath (BootOption
->DevicePath
);
3703 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
3704 FreePool(BootOption
);
3711 Return the bootable media handle.
3712 First, check the device is connected
3713 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
3714 Third, detect the the default boot file in the Media, and return the removable Media handle.
3716 @param DevicePath Device Path to a bootable device
3718 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
3723 BdsLibGetBootableHandle (
3724 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3729 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3730 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
3732 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
3734 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3737 EFI_HANDLE ReturnHandle
;
3738 EFI_HANDLE
*SimpleFileSystemHandles
;
3740 UINTN NumberSimpleFileSystemHandles
;
3742 EFI_IMAGE_DOS_HEADER DosHeader
;
3743 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3744 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3746 UpdatedDevicePath
= DevicePath
;
3749 // Enter to critical section to protect the acquired BlockIo instance
3750 // from getting released due to the USB mass storage hotplug event
3752 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
3755 // Check whether the device is connected
3757 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3758 if (EFI_ERROR (Status
)) {
3760 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
3762 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3763 if (EFI_ERROR (Status
)) {
3765 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
3767 UpdatedDevicePath
= DevicePath
;
3768 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3769 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3773 // For removable device boot option, its contained device path only point to the removable device handle,
3774 // should make sure all its children handles (its child partion or media handles) are created and connected.
3776 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3778 // Get BlockIo protocol and check removable attribute
3780 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
3781 ASSERT_EFI_ERROR (Status
);
3784 // Issue a dummy read to the device to check for media change.
3785 // When the removable media is changed, any Block IO read/write will
3786 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
3787 // returned. After the Block IO protocol is reinstalled, subsequent
3788 // Block IO read/write will success.
3790 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
3791 if (Buffer
!= NULL
) {
3792 BlockIo
->ReadBlocks (
3794 BlockIo
->Media
->MediaId
,
3796 BlockIo
->Media
->BlockSize
,
3804 // Detect the the default boot file from removable Media
3808 // 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
3809 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
3811 DupDevicePath
= DuplicateDevicePath (DevicePath
);
3812 ASSERT (DupDevicePath
!= NULL
);
3814 UpdatedDevicePath
= DupDevicePath
;
3815 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3817 // 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
3818 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
3820 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
3821 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
3823 // Remove the usb node, let the device path only point to PCI node
3825 SetDevicePathEndNode (UpdatedDevicePath
);
3826 UpdatedDevicePath
= DupDevicePath
;
3828 UpdatedDevicePath
= DevicePath
;
3832 // Get the device path size of boot option
3834 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3835 ReturnHandle
= NULL
;
3836 gBS
->LocateHandleBuffer (
3838 &gEfiSimpleFileSystemProtocolGuid
,
3840 &NumberSimpleFileSystemHandles
,
3841 &SimpleFileSystemHandles
3843 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
3845 // Get the device path size of SimpleFileSystem handle
3847 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
3848 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3850 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
3852 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
3854 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
3855 // machinename is ia32, ia64, x64, ...
3857 Hdr
.Union
= &HdrData
;
3858 Status
= BdsLibGetImageHeader (
3859 SimpleFileSystemHandles
[Index
],
3860 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3864 if (!EFI_ERROR (Status
) &&
3865 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3866 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3867 ReturnHandle
= SimpleFileSystemHandles
[Index
];
3873 FreePool(DupDevicePath
);
3875 if (SimpleFileSystemHandles
!= NULL
) {
3876 FreePool(SimpleFileSystemHandles
);
3879 gBS
->RestoreTPL (OldTpl
);
3881 return ReturnHandle
;
3885 Check to see if the network cable is plugged in. If the DevicePath is not
3886 connected it will be connected.
3888 @param DevicePath Device Path to check
3890 @retval TRUE DevicePath points to an Network that is connected
3891 @retval FALSE DevicePath does not point to a bootable network
3895 BdsLibNetworkBootWithMediaPresent (
3896 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3900 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3902 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
3903 BOOLEAN MediaPresent
;
3904 UINT32 InterruptStatus
;
3906 MediaPresent
= FALSE
;
3908 UpdatedDevicePath
= DevicePath
;
3910 // Locate Load File Protocol for PXE boot option first
3912 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3913 if (EFI_ERROR (Status
)) {
3915 // Device not present so see if we need to connect it
3917 Status
= BdsLibConnectDevicePath (DevicePath
);
3918 if (!EFI_ERROR (Status
)) {
3920 // This one should work after we did the connect
3922 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3926 if (!EFI_ERROR (Status
)) {
3927 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
3928 if (EFI_ERROR (Status
)) {
3930 // Failed to open SNP from this handle, try to get SNP from parent handle
3932 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
3933 if (UpdatedDevicePath
!= NULL
) {
3934 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3935 if (!EFI_ERROR (Status
)) {
3937 // SNP handle found, get SNP from it
3939 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
3944 if (!EFI_ERROR (Status
)) {
3945 if (Snp
->Mode
->MediaPresentSupported
) {
3946 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
3948 // Invoke Snp->GetStatus() to refresh the media status
3950 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
3953 // In case some one else is using the SNP check to see if it's connected
3955 MediaPresent
= Snp
->Mode
->MediaPresent
;
3958 // No one is using SNP so we need to Start and Initialize so
3959 // MediaPresent will be valid.
3961 Status
= Snp
->Start (Snp
);
3962 if (!EFI_ERROR (Status
)) {
3963 Status
= Snp
->Initialize (Snp
, 0, 0);
3964 if (!EFI_ERROR (Status
)) {
3965 MediaPresent
= Snp
->Mode
->MediaPresent
;
3966 Snp
->Shutdown (Snp
);
3972 MediaPresent
= TRUE
;
3977 return MediaPresent
;
3981 For a bootable Device path, return its boot type.
3983 @param DevicePath The bootable device Path to check
3985 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3986 which subtype is MEDIA_HARDDRIVE_DP
3987 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3988 which subtype is MEDIA_CDROM_DP
3989 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
3990 which HID is floppy device.
3991 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3992 and its last device path node's subtype is MSG_ATAPI_DP.
3993 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3994 and its last device path node's subtype is MSG_SCSI_DP.
3995 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3996 and its last device path node's subtype is MSG_USB_DP.
3997 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
3998 its last device path node point to a message device path node.
3999 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
4000 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
4005 BdsGetBootTypeFromDevicePath (
4006 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
4009 ACPI_HID_DEVICE_PATH
*Acpi
;
4010 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4011 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4014 if (NULL
== DevicePath
) {
4015 return BDS_EFI_UNSUPPORT
;
4018 TempDevicePath
= DevicePath
;
4020 while (!IsDevicePathEndType (TempDevicePath
)) {
4021 switch (DevicePathType (TempDevicePath
)) {
4022 case BBS_DEVICE_PATH
:
4023 return BDS_LEGACY_BBS_BOOT
;
4024 case MEDIA_DEVICE_PATH
:
4025 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
4026 return BDS_EFI_MEDIA_HD_BOOT
;
4027 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
4028 return BDS_EFI_MEDIA_CDROM_BOOT
;
4031 case ACPI_DEVICE_PATH
:
4032 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
4033 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
4034 return BDS_EFI_ACPI_FLOPPY_BOOT
;
4037 case MESSAGING_DEVICE_PATH
:
4039 // Get the last device path node
4041 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
4042 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
4044 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
4047 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
4050 // if the device path not only point to driver device, it is not a messaging device path,
4052 if (!IsDevicePathEndType (LastDeviceNode
)) {
4056 switch (DevicePathSubType (TempDevicePath
)) {
4058 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
4062 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
4066 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
4070 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
4073 case MSG_MAC_ADDR_DP
:
4077 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
4081 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
4089 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4092 return BDS_EFI_UNSUPPORT
;
4096 Check whether the Device path in a boot option point to a valid bootable device,
4097 And if CheckMedia is true, check the device is ready to boot now.
4099 @param DevPath the Device path in a boot option
4100 @param CheckMedia if true, check the device is ready to boot now.
4102 @retval TRUE the Device path is valid
4103 @retval FALSE the Device path is invalid .
4108 BdsLibIsValidEFIBootOptDevicePath (
4109 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
4110 IN BOOLEAN CheckMedia
4113 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
4117 Check whether the Device path in a boot option point to a valid bootable device,
4118 And if CheckMedia is true, check the device is ready to boot now.
4119 If Description is not NULL and the device path point to a fixed BlockIo
4120 device, check the description whether conflict with other auto-created
4123 @param DevPath the Device path in a boot option
4124 @param CheckMedia if true, check the device is ready to boot now.
4125 @param Description the description in a boot option
4127 @retval TRUE the Device path is valid
4128 @retval FALSE the Device path is invalid .
4133 BdsLibIsValidEFIBootOptDevicePathExt (
4134 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
4135 IN BOOLEAN CheckMedia
,
4136 IN CHAR16
*Description
4141 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4142 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4143 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
4145 TempDevicePath
= DevPath
;
4146 LastDeviceNode
= DevPath
;
4149 // Check if it's a valid boot option for network boot device.
4150 // Check if there is EfiLoadFileProtocol installed.
4151 // If yes, that means there is a boot option for network.
4153 Status
= gBS
->LocateDevicePath (
4154 &gEfiLoadFileProtocolGuid
,
4158 if (EFI_ERROR (Status
)) {
4160 // Device not present so see if we need to connect it
4162 TempDevicePath
= DevPath
;
4163 BdsLibConnectDevicePath (TempDevicePath
);
4164 Status
= gBS
->LocateDevicePath (
4165 &gEfiLoadFileProtocolGuid
,
4171 if (!EFI_ERROR (Status
)) {
4172 if (!IsDevicePathEnd (TempDevicePath
)) {
4174 // LoadFile protocol is not installed on handle with exactly the same DevPath
4181 // Test if it is ready to boot now
4183 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
4192 // If the boot option point to a file, it is a valid EFI boot option,
4193 // and assume it is ready to boot now
4195 while (!IsDevicePathEnd (TempDevicePath
)) {
4197 // If there is USB Class or USB WWID device path node, treat it as valid EFI
4198 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it
4199 // to full device path.
4201 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
4202 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
4203 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
4207 LastDeviceNode
= TempDevicePath
;
4208 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4210 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
4211 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
4216 // Check if it's a valid boot option for internal FV application
4218 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
4220 // If the boot option point to internal FV application, make sure it is valid
4222 TempDevicePath
= DevPath
;
4223 Status
= BdsLibUpdateFvFileDevicePath (
4225 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
)
4227 if (Status
== EFI_ALREADY_STARTED
) {
4230 if (Status
== EFI_SUCCESS
) {
4231 FreePool (TempDevicePath
);
4238 // If the boot option point to a blockIO device:
4239 // if it is a removable blockIo device, it is valid.
4240 // if it is a fixed blockIo device, check its description confliction.
4242 TempDevicePath
= DevPath
;
4243 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4244 if (EFI_ERROR (Status
)) {
4246 // Device not present so see if we need to connect it
4248 Status
= BdsLibConnectDevicePath (DevPath
);
4249 if (!EFI_ERROR (Status
)) {
4251 // Try again to get the Block Io protocol after we did the connect
4253 TempDevicePath
= DevPath
;
4254 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4258 if (!EFI_ERROR (Status
)) {
4259 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
4260 if (!EFI_ERROR (Status
)) {
4263 // Test if it is ready to boot now
4265 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4274 // 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,
4276 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
4277 if (!EFI_ERROR (Status
)) {
4280 // Test if it is ready to boot now
4282 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4296 According to a file guild, check a Fv file device path is valid. If it is invalid,
4297 try to return the valid device path.
4298 FV address maybe changes for memory layout adjust from time to time, use this function
4299 could promise the Fv file device path is right.
4301 @param DevicePath on input, the Fv file device path need to check on
4302 output, the updated valid Fv file device path
4303 @param FileGuid the Fv file guild
4305 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
4307 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
4309 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
4311 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
4312 and return the updated device path in DevicePath
4317 BdsLibUpdateFvFileDevicePath (
4318 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
4319 IN EFI_GUID
*FileGuid
4322 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4323 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4325 EFI_GUID
*GuidPoint
;
4327 UINTN FvHandleCount
;
4328 EFI_HANDLE
*FvHandleBuffer
;
4329 EFI_FV_FILETYPE Type
;
4331 EFI_FV_FILE_ATTRIBUTES Attributes
;
4332 UINT32 AuthenticationStatus
;
4334 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
4335 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
4336 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
4337 EFI_HANDLE FoundFvHandle
;
4338 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
4340 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
4341 return EFI_INVALID_PARAMETER
;
4343 if (FileGuid
== NULL
) {
4344 return EFI_INVALID_PARAMETER
;
4348 // Check whether the device path point to the default the input Fv file
4350 TempDevicePath
= *DevicePath
;
4351 LastDeviceNode
= TempDevicePath
;
4352 while (!IsDevicePathEnd (TempDevicePath
)) {
4353 LastDeviceNode
= TempDevicePath
;
4354 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4356 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
4357 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
4359 if (GuidPoint
== NULL
) {
4361 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
4363 return EFI_UNSUPPORTED
;
4365 if (!CompareGuid (GuidPoint
, FileGuid
)) {
4367 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
4369 return EFI_UNSUPPORTED
;
4373 // Check whether the input Fv file device path is valid
4375 TempDevicePath
= *DevicePath
;
4376 FoundFvHandle
= NULL
;
4377 Status
= gBS
->LocateDevicePath (
4378 &gEfiFirmwareVolume2ProtocolGuid
,
4382 if (!EFI_ERROR (Status
)) {
4383 Status
= gBS
->HandleProtocol (
4385 &gEfiFirmwareVolume2ProtocolGuid
,
4388 if (!EFI_ERROR (Status
)) {
4390 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
4392 Status
= Fv
->ReadFile (
4399 &AuthenticationStatus
4401 if (!EFI_ERROR (Status
)) {
4402 return EFI_ALREADY_STARTED
;
4408 // Look for the input wanted FV file in current FV
4409 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
4412 FoundFvHandle
= NULL
;
4413 Status
= gBS
->HandleProtocol (
4415 &gEfiLoadedImageProtocolGuid
,
4416 (VOID
**) &LoadedImage
4418 if (!EFI_ERROR (Status
)) {
4419 Status
= gBS
->HandleProtocol (
4420 LoadedImage
->DeviceHandle
,
4421 &gEfiFirmwareVolume2ProtocolGuid
,
4424 if (!EFI_ERROR (Status
)) {
4425 Status
= Fv
->ReadFile (
4432 &AuthenticationStatus
4434 if (!EFI_ERROR (Status
)) {
4436 FoundFvHandle
= LoadedImage
->DeviceHandle
;
4441 // Second, if fail to find, try to enumerate all FV
4444 FvHandleBuffer
= NULL
;
4445 gBS
->LocateHandleBuffer (
4447 &gEfiFirmwareVolume2ProtocolGuid
,
4452 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
4453 gBS
->HandleProtocol (
4454 FvHandleBuffer
[Index
],
4455 &gEfiFirmwareVolume2ProtocolGuid
,
4459 Status
= Fv
->ReadFile (
4466 &AuthenticationStatus
4468 if (EFI_ERROR (Status
)) {
4470 // Skip if input Fv file not in the FV
4475 FoundFvHandle
= FvHandleBuffer
[Index
];
4479 if (FvHandleBuffer
!= NULL
) {
4480 FreePool (FvHandleBuffer
);
4486 // Build the shell device path
4488 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
4489 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
4490 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
4491 ASSERT (NewDevicePath
!= NULL
);
4492 *DevicePath
= NewDevicePath
;
4495 return EFI_NOT_FOUND
;