2 BDS Lib functions which relate with create or process the boot option.
4 Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalBdsLib.h"
18 BOOLEAN mEnumBootDevice
= FALSE
;
19 EFI_HII_HANDLE gBdsLibStringPackHandle
= NULL
;
22 The constructor function register UNI strings into imageHandle.
24 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
26 @param ImageHandle The firmware allocated handle for the EFI image.
27 @param SystemTable A pointer to the EFI System Table.
29 @retval EFI_SUCCESS The constructor successfully added string package.
30 @retval Other value The constructor can't add string package.
35 GenericBdsLibConstructor (
36 IN EFI_HANDLE ImageHandle
,
37 IN EFI_SYSTEM_TABLE
*SystemTable
41 gBdsLibStringPackHandle
= HiiAddPackages (
42 &gBdsLibStringPackageGuid
,
48 ASSERT (gBdsLibStringPackHandle
!= NULL
);
54 Deletete the Boot Option from EFI Variable. The Boot Order Arrray
57 @param OptionNumber The number of Boot option want to be deleted.
58 @param BootOrder The Boot Order array.
59 @param BootOrderSize The size of the Boot Order Array.
61 @retval EFI_SUCCESS The Boot Option Variable was found and removed
62 @retval EFI_UNSUPPORTED The Boot Option Variable store was inaccessible
63 @retval EFI_NOT_FOUND The Boot Option Variable was not found
68 IN UINTN OptionNumber
,
69 IN OUT UINT16
*BootOrder
,
70 IN OUT UINTN
*BootOrderSize
77 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", OptionNumber
);
78 Status
= gRT
->SetVariable (
80 &gEfiGlobalVariableGuid
,
87 // adjust boot order array
89 for (Index
= 0; Index
< *BootOrderSize
/ sizeof (UINT16
); Index
++) {
90 if (BootOrder
[Index
] == OptionNumber
) {
91 CopyMem (&BootOrder
[Index
], &BootOrder
[Index
+1], *BootOrderSize
- (Index
+1) * sizeof (UINT16
));
92 *BootOrderSize
-= sizeof (UINT16
);
101 Translate the first n characters of an Ascii string to
102 Unicode characters. The count n is indicated by parameter
103 Size. If Size is greater than the length of string, then
104 the entire string is translated.
107 @param AStr Pointer to input Ascii string.
108 @param Size The number of characters to translate.
109 @param UStr Pointer to output Unicode string buffer.
122 while (AStr
[Idx
] != 0) {
123 UStr
[Idx
] = (CHAR16
) AStr
[Idx
];
134 Build Legacy Device Name String according.
136 @param CurBBSEntry BBS Table.
138 @param BufSize The buffer size.
139 @param BootString The output string.
143 BdsBuildLegacyDevNameString (
144 IN BBS_TABLE
*CurBBSEntry
,
147 OUT CHAR16
*BootString
160 Fmt
= L
"Primary Master %s";
167 Fmt
= L
"Primary Slave %s";
174 Fmt
= L
"Secondary Master %s";
181 Fmt
= L
"Secondary Slave %s";
189 switch (CurBBSEntry
->DeviceType
) {
210 case BBS_EMBED_NETWORK
:
224 // If current BBS entry has its description then use it.
226 StringDesc
= (UINT8
*) (UINTN
) ((CurBBSEntry
->DescStringSegment
<< 4) + CurBBSEntry
->DescStringOffset
);
227 if (NULL
!= StringDesc
) {
229 // Only get fisrt 32 characters, this is suggested by BBS spec
231 AsciiToUnicodeSize (StringDesc
, 32, Temp
);
237 // BbsTable 16 entries are for onboard IDE.
238 // Set description string for SATA harddisks, Harddisk 0 ~ Harddisk 11
240 if (Index
>= 5 && Index
<= 16 && (CurBBSEntry
->DeviceType
== BBS_HARDDISK
|| CurBBSEntry
->DeviceType
== BBS_CDROM
)) {
242 UnicodeSPrint (BootString
, BufSize
, Fmt
, Type
, Index
- 5);
244 UnicodeSPrint (BootString
, BufSize
, Fmt
, Type
);
250 Create a legacy boot option for the specified entry of
251 BBS table, save it as variable, and append it to the boot
255 @param CurrentBbsEntry Pointer to current BBS table.
256 @param CurrentBbsDevPath Pointer to the Device Path Protocol instance of BBS
257 @param Index Index of the specified entry in BBS table.
258 @param BootOrderList On input, the original boot order list.
259 On output, the new boot order list attached with the
261 @param BootOrderListSize On input, the original size of boot order list.
262 On output, the size of new boot order list.
264 @retval EFI_SUCCESS Boot Option successfully created.
265 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
266 @retval Other Error occurs while setting variable.
270 BdsCreateLegacyBootOption (
271 IN BBS_TABLE
*CurrentBbsEntry
,
272 IN EFI_DEVICE_PATH_PROTOCOL
*CurrentBbsDevPath
,
274 IN OUT UINT16
**BootOrderList
,
275 IN OUT UINTN
*BootOrderListSize
279 UINT16 CurrentBootOptionNo
;
280 UINT16 BootString
[10];
281 CHAR16 BootDesc
[100];
282 CHAR8 HelpString
[100];
283 UINT16
*NewBootOrderList
;
288 UINT16 CurrentBbsDevPathSize
;
289 UINTN BootOrderIndex
;
290 UINTN BootOrderLastIndex
;
292 BOOLEAN IndexNotFound
;
293 BBS_BBS_DEVICE_PATH
*NewBbsDevPathNode
;
295 if ((*BootOrderList
) == NULL
) {
296 CurrentBootOptionNo
= 0;
298 for (ArrayIndex
= 0; ArrayIndex
< (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
)); ArrayIndex
++) {
299 IndexNotFound
= TRUE
;
300 for (BootOrderIndex
= 0; BootOrderIndex
< (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
)); BootOrderIndex
++) {
301 if ((*BootOrderList
)[BootOrderIndex
] == ArrayIndex
) {
302 IndexNotFound
= FALSE
;
307 if (!IndexNotFound
) {
314 CurrentBootOptionNo
= (UINT16
) ArrayIndex
;
324 BdsBuildLegacyDevNameString (CurrentBbsEntry
, Index
, sizeof (BootDesc
), BootDesc
);
327 // Create new BBS device path node with description string
329 UnicodeStrToAsciiStr (BootDesc
, HelpString
);
331 StringLen
= AsciiStrLen (HelpString
);
332 NewBbsDevPathNode
= AllocateZeroPool (sizeof (BBS_BBS_DEVICE_PATH
) + StringLen
);
333 if (NewBbsDevPathNode
== NULL
) {
334 return EFI_OUT_OF_RESOURCES
;
336 CopyMem (NewBbsDevPathNode
, CurrentBbsDevPath
, sizeof (BBS_BBS_DEVICE_PATH
));
337 CopyMem (NewBbsDevPathNode
->String
, HelpString
, StringLen
+ 1);
338 SetDevicePathNodeLength (&(NewBbsDevPathNode
->Header
), sizeof (BBS_BBS_DEVICE_PATH
) + StringLen
);
341 // Create entire new CurrentBbsDevPath with end node
343 CurrentBbsDevPath
= AppendDevicePathNode (
345 (EFI_DEVICE_PATH_PROTOCOL
*) NewBbsDevPathNode
347 if (CurrentBbsDevPath
== NULL
) {
348 FreePool (NewBbsDevPathNode
);
349 return EFI_OUT_OF_RESOURCES
;
352 CurrentBbsDevPathSize
= (UINT16
) (GetDevicePathSize (CurrentBbsDevPath
));
354 BufferSize
= sizeof (UINT32
) +
357 CurrentBbsDevPathSize
+
361 Buffer
= AllocateZeroPool (BufferSize
);
362 if (Buffer
== NULL
) {
363 FreePool (NewBbsDevPathNode
);
364 FreePool (CurrentBbsDevPath
);
365 return EFI_OUT_OF_RESOURCES
;
368 Ptr
= (UINT8
*) Buffer
;
370 *((UINT32
*) Ptr
) = LOAD_OPTION_ACTIVE
;
371 Ptr
+= sizeof (UINT32
);
373 *((UINT16
*) Ptr
) = CurrentBbsDevPathSize
;
374 Ptr
+= sizeof (UINT16
);
381 Ptr
+= StrSize (BootDesc
);
386 CurrentBbsDevPathSize
388 Ptr
+= CurrentBbsDevPathSize
;
396 Ptr
+= sizeof (BBS_TABLE
);
397 *((UINT16
*) Ptr
) = (UINT16
) Index
;
399 Status
= gRT
->SetVariable (
401 &gEfiGlobalVariableGuid
,
402 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
411 NewBootOrderList
= AllocateZeroPool (*BootOrderListSize
+ sizeof (UINT16
));
412 if (NULL
== NewBootOrderList
) {
413 FreePool (NewBbsDevPathNode
);
414 FreePool (CurrentBbsDevPath
);
415 return EFI_OUT_OF_RESOURCES
;
418 if (*BootOrderList
!= NULL
) {
419 CopyMem (NewBootOrderList
, *BootOrderList
, *BootOrderListSize
);
420 FreePool (*BootOrderList
);
423 BootOrderLastIndex
= (UINTN
) (*BootOrderListSize
/ sizeof (UINT16
));
424 NewBootOrderList
[BootOrderLastIndex
] = CurrentBootOptionNo
;
425 *BootOrderListSize
+= sizeof (UINT16
);
426 *BootOrderList
= NewBootOrderList
;
428 FreePool (NewBbsDevPathNode
);
429 FreePool (CurrentBbsDevPath
);
434 Check if the boot option is a legacy one.
436 @param BootOptionVar The boot option data payload.
437 @param BbsEntry The BBS Table.
438 @param BbsIndex The table index.
440 @retval TRUE It is a legacy boot option.
441 @retval FALSE It is not a legacy boot option.
445 BdsIsLegacyBootOption (
446 IN UINT8
*BootOptionVar
,
447 OUT BBS_TABLE
**BbsEntry
,
452 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
457 Ptr
+= sizeof (UINT32
);
458 DevPathLen
= *(UINT16
*) Ptr
;
459 Ptr
+= sizeof (UINT16
);
460 Ptr
+= StrSize ((UINT16
*) Ptr
);
461 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
462 if ((BBS_DEVICE_PATH
== DevicePath
->Type
) && (BBS_BBS_DP
== DevicePath
->SubType
)) {
464 *BbsEntry
= (BBS_TABLE
*) Ptr
;
465 Ptr
+= sizeof (BBS_TABLE
);
466 *BbsIndex
= *(UINT16
*) Ptr
;
477 Delete all the invalid legacy boot options.
479 @retval EFI_SUCCESS All invalide legacy boot options are deleted.
480 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
481 @retval EFI_NOT_FOUND Fail to retrive variable of boot order.
485 BdsDeleteAllInvalidLegacyBootOptions (
490 UINT8
*BootOptionVar
;
492 UINTN BootOptionSize
;
496 HDD_INFO
*LocalHddInfo
;
497 BBS_TABLE
*LocalBbsTable
;
500 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
502 UINT16 BootOption
[10];
503 UINT16 BootDesc
[100];
504 BOOLEAN DescStringMatch
;
506 Status
= EFI_SUCCESS
;
512 LocalBbsTable
= NULL
;
515 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
516 if (EFI_ERROR (Status
)) {
520 LegacyBios
->GetBbsInfo (
528 BootOrder
= BdsLibGetVariableAndSize (
530 &gEfiGlobalVariableGuid
,
533 if (BootOrder
== NULL
) {
538 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
539 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
540 BootOptionVar
= BdsLibGetVariableAndSize (
542 &gEfiGlobalVariableGuid
,
545 if (NULL
== BootOptionVar
) {
547 Status
= gRT
->GetVariable (
549 &gEfiGlobalVariableGuid
,
554 if (Status
== EFI_NOT_FOUND
) {
558 BdsDeleteBootOption (
565 FreePool (BootOrder
);
566 return EFI_OUT_OF_RESOURCES
;
571 // Skip Non-Legacy boot option
573 if (!BdsIsLegacyBootOption (BootOptionVar
, &BbsEntry
, &BbsIndex
)) {
574 if (BootOptionVar
!= NULL
) {
575 FreePool (BootOptionVar
);
581 if (BbsIndex
< BbsCount
) {
583 // Check if BBS Description String is changed
585 DescStringMatch
= FALSE
;
586 BdsBuildLegacyDevNameString (
587 &LocalBbsTable
[BbsIndex
],
593 if (StrCmp (BootDesc
, (UINT16
*)(BootOptionVar
+ sizeof (UINT32
) + sizeof (UINT16
))) == 0) {
594 DescStringMatch
= TRUE
;
597 if (!((LocalBbsTable
[BbsIndex
].BootPriority
== BBS_IGNORE_ENTRY
) ||
598 (LocalBbsTable
[BbsIndex
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)) &&
599 (LocalBbsTable
[BbsIndex
].DeviceType
== BbsEntry
->DeviceType
) &&
606 if (BootOptionVar
!= NULL
) {
607 FreePool (BootOptionVar
);
612 BdsDeleteBootOption (
620 // Adjust the number of boot options.
622 Status
= gRT
->SetVariable (
624 &gEfiGlobalVariableGuid
,
625 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
629 if (BootOrder
!= NULL
) {
630 FreePool (BootOrder
);
637 Find all legacy boot option by device type.
639 @param BootOrder The boot order array.
640 @param BootOptionNum The number of boot option.
641 @param DevType Device type.
642 @param DevName Device name.
643 @param Attribute The boot option attribute.
644 @param BbsIndex The BBS table index.
645 @param OptionNumber The boot option index.
647 @retval TRUE The Legacy boot option is found.
648 @retval FALSE The legacy boot option is not found.
652 BdsFindLegacyBootOptionByDevTypeAndName (
653 IN UINT16
*BootOrder
,
654 IN UINTN BootOptionNum
,
657 OUT UINT32
*Attribute
,
658 OUT UINT16
*BbsIndex
,
659 OUT UINT16
*OptionNumber
663 CHAR16 BootOption
[9];
664 UINTN BootOptionSize
;
665 UINT8
*BootOptionVar
;
672 if (NULL
== BootOrder
) {
677 // Loop all boot option from variable
679 for (Index
= 0; Index
< BootOptionNum
; Index
++) {
680 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", (UINTN
) BootOrder
[Index
]);
681 BootOptionVar
= BdsLibGetVariableAndSize (
683 &gEfiGlobalVariableGuid
,
686 if (NULL
== BootOptionVar
) {
691 // Skip Non-legacy boot option
693 if (!BdsIsLegacyBootOption (BootOptionVar
, &BbsEntry
, BbsIndex
)) {
694 FreePool (BootOptionVar
);
699 (BbsEntry
->DeviceType
!= DevType
) ||
700 (StrCmp (DevName
, (CHAR16
*)(BootOptionVar
+ sizeof (UINT32
) + sizeof (UINT16
))) != 0)
702 FreePool (BootOptionVar
);
706 *Attribute
= *(UINT32
*) BootOptionVar
;
707 *OptionNumber
= BootOrder
[Index
];
709 FreePool (BootOptionVar
);
717 Create a legacy boot option.
719 @param BbsItem The BBS Table entry.
720 @param Index Index of the specified entry in BBS table.
721 @param BootOrderList The boot order list.
722 @param BootOrderListSize The size of boot order list.
724 @retval EFI_OUT_OF_RESOURCE No enough memory.
725 @retval EFI_SUCCESS The function complete successfully.
726 @return Other value if the legacy boot option is not created.
730 BdsCreateOneLegacyBootOption (
731 IN BBS_TABLE
*BbsItem
,
733 IN OUT UINT16
**BootOrderList
,
734 IN OUT UINTN
*BootOrderListSize
737 BBS_BBS_DEVICE_PATH BbsDevPathNode
;
739 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
744 // Create device path node.
746 BbsDevPathNode
.Header
.Type
= BBS_DEVICE_PATH
;
747 BbsDevPathNode
.Header
.SubType
= BBS_BBS_DP
;
748 SetDevicePathNodeLength (&BbsDevPathNode
.Header
, sizeof (BBS_BBS_DEVICE_PATH
));
749 BbsDevPathNode
.DeviceType
= BbsItem
->DeviceType
;
750 CopyMem (&BbsDevPathNode
.StatusFlag
, &BbsItem
->StatusFlags
, sizeof (UINT16
));
752 DevPath
= AppendDevicePathNode (
754 (EFI_DEVICE_PATH_PROTOCOL
*) &BbsDevPathNode
756 if (NULL
== DevPath
) {
757 return EFI_OUT_OF_RESOURCES
;
760 Status
= BdsCreateLegacyBootOption (
767 BbsItem
->BootPriority
= 0x00;
775 Add the legacy boot options from BBS table if they do not exist.
777 @retval EFI_SUCCESS The boot options are added successfully
778 or they are already in boot options.
779 @retval EFI_NOT_FOUND No legacy boot options is found.
780 @retval EFI_OUT_OF_RESOURCE No enough memory.
781 @return Other value LegacyBoot options are not added.
785 BdsAddNonExistingLegacyBootOptions (
795 HDD_INFO
*LocalHddInfo
;
796 BBS_TABLE
*LocalBbsTable
;
798 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
807 LocalBbsTable
= NULL
;
809 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
810 if (EFI_ERROR (Status
)) {
814 LegacyBios
->GetBbsInfo (
822 BootOrder
= BdsLibGetVariableAndSize (
824 &gEfiGlobalVariableGuid
,
827 if (BootOrder
== NULL
) {
831 for (Index
= 0; Index
< BbsCount
; Index
++) {
832 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
833 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
838 BdsBuildLegacyDevNameString (&LocalBbsTable
[Index
], Index
, sizeof (Desc
), Desc
);
840 Exist
= BdsFindLegacyBootOptionByDevTypeAndName (
842 BootOrderSize
/ sizeof (UINT16
),
843 LocalBbsTable
[Index
].DeviceType
,
851 // Not found such type of legacy device in boot options or we found but it's disabled
852 // so we have to create one and put it to the tail of boot order list
854 Status
= BdsCreateOneLegacyBootOption (
855 &LocalBbsTable
[Index
],
860 if (EFI_ERROR (Status
)) {
864 OptionNumber
= BootOrder
[BootOrderSize
/ sizeof (UINT16
) - 1];
867 ASSERT (BbsIndex
== Index
);
870 Status
= gRT
->SetVariable (
872 &gEfiGlobalVariableGuid
,
873 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
877 if (BootOrder
!= NULL
) {
878 FreePool (BootOrder
);
885 Fill the device order buffer.
887 @param BbsTable The BBS table.
888 @param BbsType The BBS Type.
889 @param BbsCount The BBS Count.
890 @param Buf device order buffer.
892 @return The device order buffer.
897 IN BBS_TABLE
*BbsTable
,
905 for (Index
= 0; Index
< BbsCount
; Index
++) {
906 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
910 if (BbsTable
[Index
].DeviceType
!= BbsType
) {
914 *Buf
= (UINT16
) (Index
& 0xFF);
922 Create the device order buffer.
924 @param BbsTable The BBS table.
925 @param BbsCount The BBS Count.
927 @retval EFI_SUCCES The buffer is created and the EFI variable named
928 VAR_LEGACY_DEV_ORDER and gEfiLegacyDevOrderVariableGuid is
930 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
931 @retval EFI_DEVICE_ERROR Fail to add the device order into EFI variable fail
932 because of hardware error.
936 IN BBS_TABLE
*BbsTable
,
948 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
949 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
958 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
960 Status
= EFI_SUCCESS
;
963 // Count all boot devices
965 for (Index
= 0; Index
< BbsCount
; Index
++) {
966 if (BbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) {
970 switch (BbsTable
[Index
].DeviceType
) {
983 case BBS_EMBED_NETWORK
:
996 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * FDCount
);
997 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * HDCount
);
998 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * CDCount
);
999 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * NETCount
);
1000 TotalSize
+= (HeaderSize
+ sizeof (UINT16
) * BEVCount
);
1003 // Create buffer to hold all boot device order
1005 DevOrder
= AllocateZeroPool (TotalSize
);
1006 if (NULL
== DevOrder
) {
1007 return EFI_OUT_OF_RESOURCES
;
1009 DevOrderPtr
= DevOrder
;
1011 DevOrderPtr
->BbsType
= BBS_FLOPPY
;
1012 DevOrderPtr
->Length
= (UINT16
) (sizeof (DevOrderPtr
->Length
) + FDCount
* sizeof (UINT16
));
1013 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_FLOPPY
, BbsCount
, DevOrderPtr
->Data
);
1015 DevOrderPtr
->BbsType
= BBS_HARDDISK
;
1016 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1017 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_HARDDISK
, BbsCount
, DevOrderPtr
->Data
);
1019 DevOrderPtr
->BbsType
= BBS_CDROM
;
1020 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1021 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_CDROM
, BbsCount
, DevOrderPtr
->Data
);
1023 DevOrderPtr
->BbsType
= BBS_EMBED_NETWORK
;
1024 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1025 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_EMBED_NETWORK
, BbsCount
, DevOrderPtr
->Data
);
1027 DevOrderPtr
->BbsType
= BBS_BEV_DEVICE
;
1028 DevOrderPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1029 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) BdsFillDevOrderBuf (BbsTable
, BBS_BEV_DEVICE
, BbsCount
, DevOrderPtr
->Data
);
1031 ASSERT (TotalSize
== (UINTN
) ((UINT8
*) DevOrderPtr
- (UINT8
*) DevOrder
));
1034 // Save device order for legacy boot device to variable.
1036 Status
= gRT
->SetVariable (
1037 VAR_LEGACY_DEV_ORDER
,
1038 &gEfiLegacyDevOrderVariableGuid
,
1039 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1043 FreePool (DevOrder
);
1049 Add the legacy boot devices from BBS table into
1050 the legacy device boot order.
1052 @retval EFI_SUCCESS The boot devices are added successfully.
1053 @retval EFI_NOT_FOUND The legacy boot devices are not found.
1054 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
1055 @retval EFI_DEVICE_ERROR Fail to add the legacy device boot order into EFI variable
1056 because of hardware error.
1060 BdsUpdateLegacyDevOrder (
1064 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1065 LEGACY_DEV_ORDER_ENTRY
*NewDevOrder
;
1066 LEGACY_DEV_ORDER_ENTRY
*Ptr
;
1067 LEGACY_DEV_ORDER_ENTRY
*NewPtr
;
1069 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1073 HDD_INFO
*LocalHddInfo
;
1074 BBS_TABLE
*LocalBbsTable
;
1104 HeaderSize
= sizeof (BBS_TYPE
) + sizeof (UINT16
);
1112 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1113 if (EFI_ERROR (Status
)) {
1117 Status
= LegacyBios
->GetBbsInfo (
1124 if (EFI_ERROR (Status
)) {
1128 DevOrder
= BdsLibGetVariableAndSize (
1129 VAR_LEGACY_DEV_ORDER
,
1130 &gEfiLegacyDevOrderVariableGuid
,
1133 if (NULL
== DevOrder
) {
1134 return BdsCreateDevOrder (LocalBbsTable
, BbsCount
);
1137 // First we figure out how many boot devices with same device type respectively
1139 for (Index
= 0; Index
< BbsCount
; Index
++) {
1140 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1141 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1146 switch (LocalBbsTable
[Index
].DeviceType
) {
1159 case BBS_EMBED_NETWORK
:
1163 case BBS_BEV_DEVICE
:
1172 TotalSize
+= (HeaderSize
+ FDCount
* sizeof (UINT16
));
1173 TotalSize
+= (HeaderSize
+ HDCount
* sizeof (UINT16
));
1174 TotalSize
+= (HeaderSize
+ CDCount
* sizeof (UINT16
));
1175 TotalSize
+= (HeaderSize
+ NETCount
* sizeof (UINT16
));
1176 TotalSize
+= (HeaderSize
+ BEVCount
* sizeof (UINT16
));
1178 NewDevOrder
= AllocateZeroPool (TotalSize
);
1179 if (NULL
== NewDevOrder
) {
1180 return EFI_OUT_OF_RESOURCES
;
1189 NewPtr
= NewDevOrder
;
1190 NewPtr
->BbsType
= Ptr
->BbsType
;
1191 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + FDCount
* sizeof (UINT16
));
1192 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1193 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1194 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1195 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_FLOPPY
1200 NewPtr
->Data
[FDIndex
] = Ptr
->Data
[Index
];
1203 NewFDPtr
= NewPtr
->Data
;
1208 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1209 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1210 NewPtr
->BbsType
= Ptr
->BbsType
;
1211 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + HDCount
* sizeof (UINT16
));
1212 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1213 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1214 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1215 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1216 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_HARDDISK
1221 NewPtr
->Data
[HDIndex
] = Ptr
->Data
[Index
];
1224 NewHDPtr
= NewPtr
->Data
;
1229 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1230 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1231 NewPtr
->BbsType
= Ptr
->BbsType
;
1232 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + CDCount
* sizeof (UINT16
));
1233 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1234 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1235 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1236 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1237 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_CDROM
1242 NewPtr
->Data
[CDIndex
] = Ptr
->Data
[Index
];
1245 NewCDPtr
= NewPtr
->Data
;
1250 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1251 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1252 NewPtr
->BbsType
= Ptr
->BbsType
;
1253 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + NETCount
* sizeof (UINT16
));
1254 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1255 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1256 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1257 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1258 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_EMBED_NETWORK
1263 NewPtr
->Data
[NETIndex
] = Ptr
->Data
[Index
];
1266 NewNETPtr
= NewPtr
->Data
;
1271 Ptr
= (LEGACY_DEV_ORDER_ENTRY
*) (&Ptr
->Data
[Ptr
->Length
/ sizeof (UINT16
) - 1]);
1272 NewPtr
= (LEGACY_DEV_ORDER_ENTRY
*) (&NewPtr
->Data
[NewPtr
->Length
/ sizeof (UINT16
) -1]);
1273 NewPtr
->BbsType
= Ptr
->BbsType
;
1274 NewPtr
->Length
= (UINT16
) (sizeof (UINT16
) + BEVCount
* sizeof (UINT16
));
1275 for (Index
= 0; Index
< Ptr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1276 if (LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_IGNORE_ENTRY
||
1277 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_DO_NOT_BOOT_FROM
||
1278 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].BootPriority
== BBS_LOWEST_PRIORITY
||
1279 LocalBbsTable
[Ptr
->Data
[Index
] & 0xFF].DeviceType
!= BBS_BEV_DEVICE
1284 NewPtr
->Data
[BEVIndex
] = Ptr
->Data
[Index
];
1287 NewBEVPtr
= NewPtr
->Data
;
1289 for (Index
= 0; Index
< BbsCount
; Index
++) {
1290 if ((LocalBbsTable
[Index
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1291 (LocalBbsTable
[Index
].BootPriority
== BBS_DO_NOT_BOOT_FROM
)
1296 switch (LocalBbsTable
[Index
].DeviceType
) {
1299 NewDevPtr
= NewFDPtr
;
1304 NewDevPtr
= NewHDPtr
;
1309 NewDevPtr
= NewCDPtr
;
1312 case BBS_EMBED_NETWORK
:
1314 NewDevPtr
= NewNETPtr
;
1317 case BBS_BEV_DEVICE
:
1319 NewDevPtr
= NewBEVPtr
;
1327 // at this point we have copied those valid indexes to new buffer
1328 // and we should check if there is any new appeared boot device
1331 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1332 if ((NewDevPtr
[Index2
] & 0xFF) == (UINT16
) Index
) {
1337 if (Index2
== *Idx
) {
1339 // Index2 == *Idx means we didn't find Index
1340 // so Index is a new appeared device's index in BBS table
1341 // insert it before disabled indexes.
1343 for (Index2
= 0; Index2
< *Idx
; Index2
++) {
1344 if ((NewDevPtr
[Index2
] & 0xFF00) == 0xFF00) {
1348 CopyMem (&NewDevPtr
[Index2
+ 1], &NewDevPtr
[Index2
], (*Idx
- Index2
) * sizeof (UINT16
));
1349 NewDevPtr
[Index2
] = (UINT16
) (Index
& 0xFF);
1355 FreePool (DevOrder
);
1357 Status
= gRT
->SetVariable (
1358 VAR_LEGACY_DEV_ORDER
,
1359 &gEfiLegacyDevOrderVariableGuid
,
1360 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1364 FreePool (NewDevOrder
);
1370 Set Boot Priority for specified device type.
1372 @param DeviceType The device type.
1373 @param BbsIndex The BBS index to set the highest priority. Ignore when -1.
1374 @param LocalBbsTable The BBS table.
1375 @param Priority The prority table.
1377 @retval EFI_SUCCESS The function completes successfully.
1378 @retval EFI_NOT_FOUND Failed to find device.
1379 @retval EFI_OUT_OF_RESOURCES Failed to get the efi variable of device order.
1383 BdsSetBootPriority4SameTypeDev (
1384 IN UINT16 DeviceType
,
1386 IN OUT BBS_TABLE
*LocalBbsTable
,
1387 IN OUT UINT16
*Priority
1390 LEGACY_DEV_ORDER_ENTRY
*DevOrder
;
1391 LEGACY_DEV_ORDER_ENTRY
*DevOrderPtr
;
1395 DevOrder
= BdsLibGetVariableAndSize (
1396 VAR_LEGACY_DEV_ORDER
,
1397 &gEfiLegacyDevOrderVariableGuid
,
1400 if (NULL
== DevOrder
) {
1401 return EFI_OUT_OF_RESOURCES
;
1404 DevOrderPtr
= DevOrder
;
1405 while ((UINT8
*) DevOrderPtr
< (UINT8
*) DevOrder
+ DevOrderSize
) {
1406 if (DevOrderPtr
->BbsType
== DeviceType
) {
1410 DevOrderPtr
= (LEGACY_DEV_ORDER_ENTRY
*) ((UINT8
*) DevOrderPtr
+ sizeof (BBS_TYPE
) + DevOrderPtr
->Length
);
1413 if ((UINT8
*) DevOrderPtr
>= (UINT8
*) DevOrder
+ DevOrderSize
) {
1414 FreePool (DevOrder
);
1415 return EFI_NOT_FOUND
;
1418 if (BbsIndex
!= (UINTN
) -1) {
1419 LocalBbsTable
[BbsIndex
].BootPriority
= *Priority
;
1423 // If the high byte of the DevIndex is 0xFF, it indicates that this device has been disabled.
1425 for (Index
= 0; Index
< DevOrderPtr
->Length
/ sizeof (UINT16
) - 1; Index
++) {
1426 if ((DevOrderPtr
->Data
[Index
] & 0xFF00) == 0xFF00) {
1428 // LocalBbsTable[DevIndex[Index] & 0xFF].BootPriority = BBS_DISABLED_ENTRY;
1430 } else if (DevOrderPtr
->Data
[Index
] != BbsIndex
) {
1431 LocalBbsTable
[DevOrderPtr
->Data
[Index
]].BootPriority
= *Priority
;
1436 FreePool (DevOrder
);
1441 Print the BBS Table.
1443 @param LocalBbsTable The BBS table.
1444 @param BbsCount The count of entry in BBS table.
1448 IN BBS_TABLE
*LocalBbsTable
,
1454 DEBUG ((DEBUG_ERROR
, "\n"));
1455 DEBUG ((DEBUG_ERROR
, " NO Prio bb/dd/ff cl/sc Type Stat segm:offs\n"));
1456 DEBUG ((DEBUG_ERROR
, "=============================================\n"));
1457 for (Idx
= 0; Idx
< BbsCount
; Idx
++) {
1458 if ((LocalBbsTable
[Idx
].BootPriority
== BBS_IGNORE_ENTRY
) ||
1459 (LocalBbsTable
[Idx
].BootPriority
== BBS_DO_NOT_BOOT_FROM
) ||
1460 (LocalBbsTable
[Idx
].BootPriority
== BBS_LOWEST_PRIORITY
)
1467 " %02x: %04x %02x/%02x/%02x %02x/%02x %04x %04x %04x:%04x\n",
1469 (UINTN
) LocalBbsTable
[Idx
].BootPriority
,
1470 (UINTN
) LocalBbsTable
[Idx
].Bus
,
1471 (UINTN
) LocalBbsTable
[Idx
].Device
,
1472 (UINTN
) LocalBbsTable
[Idx
].Function
,
1473 (UINTN
) LocalBbsTable
[Idx
].Class
,
1474 (UINTN
) LocalBbsTable
[Idx
].SubClass
,
1475 (UINTN
) LocalBbsTable
[Idx
].DeviceType
,
1476 (UINTN
) * (UINT16
*) &LocalBbsTable
[Idx
].StatusFlags
,
1477 (UINTN
) LocalBbsTable
[Idx
].BootHandlerSegment
,
1478 (UINTN
) LocalBbsTable
[Idx
].BootHandlerOffset
,
1479 (UINTN
) ((LocalBbsTable
[Idx
].MfgStringSegment
<< 4) + LocalBbsTable
[Idx
].MfgStringOffset
),
1480 (UINTN
) ((LocalBbsTable
[Idx
].DescStringSegment
<< 4) + LocalBbsTable
[Idx
].DescStringOffset
))
1484 DEBUG ((DEBUG_ERROR
, "\n"));
1488 Set the boot priority for BBS entries based on boot option entry and boot order.
1490 @param Entry The boot option is to be checked for refresh BBS table.
1492 @retval EFI_SUCCESS The boot priority for BBS entries is refreshed successfully.
1493 @retval EFI_NOT_FOUND BBS entries can't be found.
1494 @retval EFI_OUT_OF_RESOURCES Failed to get the legacy device boot order.
1498 BdsRefreshBbsTableForBoot (
1499 IN BDS_COMMON_OPTION
*Entry
1506 HDD_INFO
*LocalHddInfo
;
1507 BBS_TABLE
*LocalBbsTable
;
1509 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1513 UINTN BootOrderSize
;
1514 UINT8
*BootOptionVar
;
1515 UINTN BootOptionSize
;
1516 CHAR16 BootOption
[9];
1519 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1521 UINTN DeviceTypeCount
;
1522 UINTN DeviceTypeIndex
;
1526 LocalHddInfo
= NULL
;
1527 LocalBbsTable
= NULL
;
1528 DevType
= BBS_UNKNOWN
;
1530 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1531 if (EFI_ERROR (Status
)) {
1535 LegacyBios
->GetBbsInfo (
1543 // First, set all the present devices' boot priority to BBS_UNPRIORITIZED_ENTRY
1544 // We will set them according to the settings setup by user
1546 for (Index
= 0; Index
< BbsCount
; Index
++) {
1547 if (!((BBS_IGNORE_ENTRY
== LocalBbsTable
[Index
].BootPriority
) ||
1548 (BBS_DO_NOT_BOOT_FROM
== LocalBbsTable
[Index
].BootPriority
) ||
1549 (BBS_LOWEST_PRIORITY
== LocalBbsTable
[Index
].BootPriority
))) {
1550 LocalBbsTable
[Index
].BootPriority
= BBS_UNPRIORITIZED_ENTRY
;
1554 // boot priority always starts at 0
1557 if (Entry
->LoadOptionsSize
== sizeof (BBS_TABLE
) + sizeof (UINT16
)) {
1559 // If Entry stands for a legacy boot option, we prioritize the devices with the same type first.
1561 DevType
= ((BBS_TABLE
*) Entry
->LoadOptions
)->DeviceType
;
1562 BbsIndex
= *(UINT16
*) ((BBS_TABLE
*) Entry
->LoadOptions
+ 1);
1563 Status
= BdsSetBootPriority4SameTypeDev (
1569 if (EFI_ERROR (Status
)) {
1574 // we have to set the boot priority for other BBS entries with different device types
1576 BootOrder
= BdsLibGetVariableAndSize (
1578 &gEfiGlobalVariableGuid
,
1581 DeviceType
= AllocatePool (BootOrderSize
+ sizeof (UINT16
));
1582 ASSERT (DeviceType
!= NULL
);
1584 DeviceType
[0] = DevType
;
1585 DeviceTypeCount
= 1;
1586 for (Index
= 0; ((BootOrder
!= NULL
) && (Index
< BootOrderSize
/ sizeof (UINT16
))); Index
++) {
1587 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
1588 BootOptionVar
= BdsLibGetVariableAndSize (
1590 &gEfiGlobalVariableGuid
,
1593 if (NULL
== BootOptionVar
) {
1597 Ptr
= BootOptionVar
;
1599 Ptr
+= sizeof (UINT32
);
1600 DevPathLen
= *(UINT16
*) Ptr
;
1601 Ptr
+= sizeof (UINT16
);
1602 Ptr
+= StrSize ((UINT16
*) Ptr
);
1603 DevPath
= (EFI_DEVICE_PATH_PROTOCOL
*) Ptr
;
1604 if (BBS_DEVICE_PATH
!= DevPath
->Type
|| BBS_BBS_DP
!= DevPath
->SubType
) {
1605 FreePool (BootOptionVar
);
1610 DevType
= ((BBS_TABLE
*) Ptr
)->DeviceType
;
1611 for (DeviceTypeIndex
= 0; DeviceTypeIndex
< DeviceTypeCount
; DeviceTypeIndex
++) {
1612 if (DeviceType
[DeviceTypeIndex
] == DevType
) {
1616 if (DeviceTypeIndex
< DeviceTypeCount
) {
1618 // We don't want to process twice for a device type
1620 FreePool (BootOptionVar
);
1624 DeviceType
[DeviceTypeCount
] = DevType
;
1627 Status
= BdsSetBootPriority4SameTypeDev (
1633 FreePool (BootOptionVar
);
1634 if (EFI_ERROR (Status
)) {
1639 if (BootOrder
!= NULL
) {
1640 FreePool (BootOrder
);
1644 PrintBbsTable (LocalBbsTable
, BbsCount
);
1651 Boot the legacy system with the boot option
1653 @param Option The legacy boot option which have BBS device path
1655 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
1657 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
1661 BdsLibDoLegacyBoot (
1662 IN BDS_COMMON_OPTION
*Option
1666 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
1667 EFI_EVENT LegacyBootEvent
;
1669 Status
= gBS
->LocateProtocol (&gEfiLegacyBiosProtocolGuid
, NULL
, (VOID
**) &LegacyBios
);
1670 if (EFI_ERROR (Status
)) {
1672 // If no LegacyBios protocol we do not support legacy boot
1674 return EFI_UNSUPPORTED
;
1677 // Notes: if we separate the int 19, then we don't need to refresh BBS
1679 BdsRefreshBbsTableForBoot (Option
);
1682 // Write boot to OS performance data for legacy boot.
1686 // Create an event to be signalled when Legacy Boot occurs to write performance data.
1688 Status
= EfiCreateEventLegacyBootEx(
1690 WriteBootToOsPerformanceData
,
1694 ASSERT_EFI_ERROR (Status
);
1697 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Legacy Boot: %S\n", Option
->Description
));
1698 return LegacyBios
->LegacyBoot (
1700 (BBS_BBS_DEVICE_PATH
*) Option
->DevicePath
,
1701 Option
->LoadOptionsSize
,
1707 Internal function to check if the input boot option is a valid EFI NV Boot####.
1709 @param OptionToCheck Boot option to be checked.
1711 @retval TRUE This boot option matches a valid EFI NV Boot####.
1712 @retval FALSE If not.
1716 IsBootOptionValidNVVarialbe (
1717 IN BDS_COMMON_OPTION
*OptionToCheck
1720 LIST_ENTRY TempList
;
1721 BDS_COMMON_OPTION
*BootOption
;
1723 CHAR16 OptionName
[20];
1727 InitializeListHead (&TempList
);
1728 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionToCheck
->BootCurrent
);
1730 BootOption
= BdsLibVariableToOption (&TempList
, OptionName
);
1731 if (BootOption
== NULL
) {
1736 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
1737 // valid EFI NV Boot####.
1739 if ((OptionToCheck
->BootCurrent
== BootOption
->BootCurrent
) &&
1740 (CompareMem (OptionToCheck
->DevicePath
, BootOption
->DevicePath
, GetDevicePathSize (OptionToCheck
->DevicePath
)) == 0))
1745 FreePool (BootOption
);
1751 Check whether a USB device match the specified USB Class device path. This
1752 function follows "Load Option Processing" behavior in UEFI specification.
1754 @param UsbIo USB I/O protocol associated with the USB device.
1755 @param UsbClass The USB Class device path to match.
1757 @retval TRUE The USB device match the USB Class device path.
1758 @retval FALSE The USB device does not match the USB Class device path.
1763 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1764 IN USB_CLASS_DEVICE_PATH
*UsbClass
1768 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1769 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1771 UINT8 DeviceSubClass
;
1772 UINT8 DeviceProtocol
;
1774 if ((DevicePathType (UsbClass
) != MESSAGING_DEVICE_PATH
) ||
1775 (DevicePathSubType (UsbClass
) != MSG_USB_CLASS_DP
)){
1780 // Check Vendor Id and Product Id.
1782 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1783 if (EFI_ERROR (Status
)) {
1787 if ((UsbClass
->VendorId
!= 0xffff) &&
1788 (UsbClass
->VendorId
!= DevDesc
.IdVendor
)) {
1792 if ((UsbClass
->ProductId
!= 0xffff) &&
1793 (UsbClass
->ProductId
!= DevDesc
.IdProduct
)) {
1797 DeviceClass
= DevDesc
.DeviceClass
;
1798 DeviceSubClass
= DevDesc
.DeviceSubClass
;
1799 DeviceProtocol
= DevDesc
.DeviceProtocol
;
1800 if (DeviceClass
== 0) {
1802 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
1803 // Protocol in Interface Descriptor instead.
1805 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1806 if (EFI_ERROR (Status
)) {
1810 DeviceClass
= IfDesc
.InterfaceClass
;
1811 DeviceSubClass
= IfDesc
.InterfaceSubClass
;
1812 DeviceProtocol
= IfDesc
.InterfaceProtocol
;
1816 // Check Class, SubClass and Protocol.
1818 if ((UsbClass
->DeviceClass
!= 0xff) &&
1819 (UsbClass
->DeviceClass
!= DeviceClass
)) {
1823 if ((UsbClass
->DeviceSubClass
!= 0xff) &&
1824 (UsbClass
->DeviceSubClass
!= DeviceSubClass
)) {
1828 if ((UsbClass
->DeviceProtocol
!= 0xff) &&
1829 (UsbClass
->DeviceProtocol
!= DeviceProtocol
)) {
1837 Check whether a USB device match the specified USB WWID device path. This
1838 function follows "Load Option Processing" behavior in UEFI specification.
1840 @param UsbIo USB I/O protocol associated with the USB device.
1841 @param UsbWwid The USB WWID device path to match.
1843 @retval TRUE The USB device match the USB WWID device path.
1844 @retval FALSE The USB device does not match the USB WWID device path.
1849 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1850 IN USB_WWID_DEVICE_PATH
*UsbWwid
1854 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
1855 EFI_USB_INTERFACE_DESCRIPTOR IfDesc
;
1856 UINT16
*LangIdTable
;
1861 CHAR16
*SerialNumberStr
;
1864 if ((DevicePathType (UsbWwid
) != MESSAGING_DEVICE_PATH
) ||
1865 (DevicePathSubType (UsbWwid
) != MSG_USB_WWID_DP
)){
1870 // Check Vendor Id and Product Id.
1872 Status
= UsbIo
->UsbGetDeviceDescriptor (UsbIo
, &DevDesc
);
1873 if (EFI_ERROR (Status
)) {
1876 if ((DevDesc
.IdVendor
!= UsbWwid
->VendorId
) ||
1877 (DevDesc
.IdProduct
!= UsbWwid
->ProductId
)) {
1882 // Check Interface Number.
1884 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &IfDesc
);
1885 if (EFI_ERROR (Status
)) {
1888 if (IfDesc
.InterfaceNumber
!= UsbWwid
->InterfaceNumber
) {
1893 // Check Serial Number.
1895 if (DevDesc
.StrSerialNumber
== 0) {
1900 // Get all supported languages.
1904 Status
= UsbIo
->UsbGetSupportedLanguages (UsbIo
, &LangIdTable
, &TableSize
);
1905 if (EFI_ERROR (Status
) || (TableSize
== 0) || (LangIdTable
== NULL
)) {
1910 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
1912 CompareStr
= (CHAR16
*) (UINTN
) (UsbWwid
+ 1);
1913 CompareLen
= (DevicePathNodeLength (UsbWwid
) - sizeof (USB_WWID_DEVICE_PATH
)) / sizeof (CHAR16
);
1914 if (CompareStr
[CompareLen
- 1] == L
'\0') {
1919 // Compare serial number in each supported language.
1921 for (Index
= 0; Index
< TableSize
/ sizeof (UINT16
); Index
++) {
1922 SerialNumberStr
= NULL
;
1923 Status
= UsbIo
->UsbGetStringDescriptor (
1926 DevDesc
.StrSerialNumber
,
1929 if (EFI_ERROR (Status
) || (SerialNumberStr
== NULL
)) {
1933 Length
= StrLen (SerialNumberStr
);
1934 if ((Length
>= CompareLen
) &&
1935 (CompareMem (SerialNumberStr
+ Length
- CompareLen
, CompareStr
, CompareLen
* sizeof (CHAR16
)) == 0)) {
1936 FreePool (SerialNumberStr
);
1940 FreePool (SerialNumberStr
);
1947 Find a USB device path which match the specified short-form device path start
1948 with USB Class or USB WWID device path and load the boot file then return the
1949 image handle. If ParentDevicePath is NULL, this function will search in all USB
1950 devices of the platform. If ParentDevicePath is not NULL,this function will only
1951 search in its child devices.
1953 @param ParentDevicePath The device path of the parent.
1954 @param ShortFormDevicePath The USB Class or USB WWID device path to match.
1956 @return The image Handle if find load file from specified short-form device path
1957 or NULL if not found.
1962 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
1963 IN EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
1967 UINTN UsbIoHandleCount
;
1968 EFI_HANDLE
*UsbIoHandleBuffer
;
1969 EFI_DEVICE_PATH_PROTOCOL
*UsbIoDevicePath
;
1970 EFI_USB_IO_PROTOCOL
*UsbIo
;
1974 EFI_HANDLE ImageHandle
;
1976 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
1977 EFI_DEVICE_PATH_PROTOCOL
*NextDevicePath
;
1979 FullDevicePath
= NULL
;
1983 // Get all UsbIo Handles.
1985 UsbIoHandleCount
= 0;
1986 UsbIoHandleBuffer
= NULL
;
1987 Status
= gBS
->LocateHandleBuffer (
1989 &gEfiUsbIoProtocolGuid
,
1994 if (EFI_ERROR (Status
) || (UsbIoHandleCount
== 0) || (UsbIoHandleBuffer
== NULL
)) {
1998 ParentSize
= (ParentDevicePath
== NULL
) ? 0 : GetDevicePathSize (ParentDevicePath
);
1999 for (Index
= 0; Index
< UsbIoHandleCount
; Index
++) {
2001 // Get the Usb IO interface.
2003 Status
= gBS
->HandleProtocol(
2004 UsbIoHandleBuffer
[Index
],
2005 &gEfiUsbIoProtocolGuid
,
2008 if (EFI_ERROR (Status
)) {
2012 UsbIoDevicePath
= DevicePathFromHandle (UsbIoHandleBuffer
[Index
]);
2013 if (UsbIoDevicePath
== NULL
) {
2017 if (ParentDevicePath
!= NULL
) {
2019 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
2021 Size
= GetDevicePathSize (UsbIoDevicePath
);
2022 if ((Size
< ParentSize
) ||
2023 (CompareMem (UsbIoDevicePath
, ParentDevicePath
, ParentSize
- END_DEVICE_PATH_LENGTH
) != 0)) {
2028 if (BdsMatchUsbClass (UsbIo
, (USB_CLASS_DEVICE_PATH
*) ShortFormDevicePath
) ||
2029 BdsMatchUsbWwid (UsbIo
, (USB_WWID_DEVICE_PATH
*) ShortFormDevicePath
)) {
2031 // Try to find if there is the boot file in this DevicePath
2033 NextDevicePath
= NextDevicePathNode (ShortFormDevicePath
);
2034 if (!IsDevicePathEnd (NextDevicePath
)) {
2035 FullDevicePath
= AppendDevicePath (UsbIoDevicePath
, NextDevicePath
);
2037 // Connect the full device path, so that Simple File System protocol
2038 // could be installed for this USB device.
2040 BdsLibConnectDevicePath (FullDevicePath
);
2041 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2042 Status
= gBS
->LoadImage (
2050 FreePool (FullDevicePath
);
2052 FullDevicePath
= UsbIoDevicePath
;
2053 Status
= EFI_NOT_FOUND
;
2057 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2058 // and load the image according to the default boot behavior for removable device.
2060 if (EFI_ERROR (Status
)) {
2062 // check if there is a bootable removable media could be found in this device path ,
2063 // and get the bootable media handle
2065 Handle
= BdsLibGetBootableHandle(UsbIoDevicePath
);
2066 if (Handle
== NULL
) {
2070 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2071 // machinename is ia32, ia64, x64, ...
2073 FullDevicePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2074 if (FullDevicePath
!= NULL
) {
2075 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2076 Status
= gBS
->LoadImage (
2084 if (EFI_ERROR (Status
)) {
2086 // The DevicePath failed, and it's not a valid
2087 // removable media device.
2099 FreePool (UsbIoHandleBuffer
);
2104 Expand USB Class or USB WWID device path node to be full device path of a USB
2105 device in platform then load the boot file on this full device path and return the
2108 This function support following 4 cases:
2109 1) Boot Option device path starts with a USB Class or USB WWID device path,
2110 and there is no Media FilePath device path in the end.
2111 In this case, it will follow Removable Media Boot Behavior.
2112 2) Boot Option device path starts with a USB Class or USB WWID device path,
2113 and ended with Media FilePath device path.
2114 3) Boot Option device path starts with a full device path to a USB Host Controller,
2115 contains a USB Class or USB WWID device path node, while not ended with Media
2116 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
2117 4) Boot Option device path starts with a full device path to a USB Host Controller,
2118 contains a USB Class or USB WWID device path node, and ended with Media
2119 FilePath device path.
2121 @param DevicePath The Boot Option device path.
2123 @return The image handle of boot file, or NULL if there is no boot file found in
2124 the specified USB Class or USB WWID device path.
2128 BdsExpandUsbShortFormDevicePath (
2129 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
2132 EFI_HANDLE
*ImageHandle
;
2133 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
2134 EFI_DEVICE_PATH_PROTOCOL
*ShortFormDevicePath
;
2137 // Search for USB Class or USB WWID device path node.
2139 ShortFormDevicePath
= NULL
;
2141 TempDevicePath
= DevicePath
;
2142 while (!IsDevicePathEnd (TempDevicePath
)) {
2143 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
2144 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
2145 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
2146 ShortFormDevicePath
= TempDevicePath
;
2149 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
2152 if (ShortFormDevicePath
== NULL
) {
2154 // No USB Class or USB WWID device path node found, do nothing.
2159 if (ShortFormDevicePath
== DevicePath
) {
2161 // Boot Option device path starts with USB Class or USB WWID device path.
2163 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2164 if (ImageHandle
== NULL
) {
2166 // Failed to find a match in existing devices, connect the short form USB
2167 // device path and try again.
2169 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath
);
2170 ImageHandle
= BdsFindUsbDevice (NULL
, ShortFormDevicePath
);
2174 // Boot Option device path contains USB Class or USB WWID device path node.
2178 // Prepare the parent device path for search.
2180 TempDevicePath
= DuplicateDevicePath (DevicePath
);
2181 ASSERT (TempDevicePath
!= NULL
);
2182 SetDevicePathEndNode (((UINT8
*) TempDevicePath
) + ((UINTN
) ShortFormDevicePath
- (UINTN
) DevicePath
));
2185 // The USB Host Controller device path is already in Boot Option device path
2186 // and USB Bus driver already support RemainingDevicePath starts with USB
2187 // Class or USB WWID device path, so just search in existing USB devices and
2188 // doesn't perform ConnectController here.
2190 ImageHandle
= BdsFindUsbDevice (TempDevicePath
, ShortFormDevicePath
);
2191 FreePool (TempDevicePath
);
2198 Process the boot option follow the UEFI specification and
2199 special treat the legacy boot option with BBS_DEVICE_PATH.
2201 @param Option The boot option need to be processed
2202 @param DevicePath The device path which describe where to load the
2203 boot image or the legacy BBS device path to boot
2205 @param ExitDataSize The size of exit data.
2206 @param ExitData Data returned when Boot image failed.
2208 @retval EFI_SUCCESS Boot from the input boot option successfully.
2209 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2214 BdsLibBootViaBootOption (
2215 IN BDS_COMMON_OPTION
*Option
,
2216 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
2217 OUT UINTN
*ExitDataSize
,
2218 OUT CHAR16
**ExitData OPTIONAL
2222 EFI_STATUS StatusLogo
;
2224 EFI_HANDLE ImageHandle
;
2225 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
2226 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
2227 EFI_DEVICE_PATH_PROTOCOL
*WorkingDevicePath
;
2228 EFI_ACPI_S3_SAVE_PROTOCOL
*AcpiS3Save
;
2229 LIST_ENTRY TempBootLists
;
2230 EFI_BOOT_LOGO_PROTOCOL
*BootLogo
;
2233 AllocateMemoryForPerformanceData ();
2240 // Notes: this code can be remove after the s3 script table
2241 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
2242 // EVT_SIGNAL_LEGACY_BOOT
2244 Status
= gBS
->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid
, NULL
, (VOID
**) &AcpiS3Save
);
2245 if (!EFI_ERROR (Status
)) {
2246 AcpiS3Save
->S3Save (AcpiS3Save
, NULL
);
2249 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
2252 WorkingDevicePath
= NULL
;
2253 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2254 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)) {
2255 WorkingDevicePath
= BdsExpandPartitionPartialDevicePathToFull (
2256 (HARDDRIVE_DEVICE_PATH
*)DevicePath
2258 if (WorkingDevicePath
!= NULL
) {
2259 DevicePath
= WorkingDevicePath
;
2266 if (IsBootOptionValidNVVarialbe (Option
)) {
2268 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
2269 // In this case, "BootCurrent" is not created.
2270 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
2274 &gEfiGlobalVariableGuid
,
2275 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2277 &Option
->BootCurrent
2282 // Report Status Code to indicate ReadyToBoot event will be signalled
2284 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT
));
2287 // Signal the EVT_SIGNAL_READY_TO_BOOT event
2289 EfiSignalEventReadyToBoot();
2292 // Expand USB Class or USB WWID device path node to be full device path of a USB
2293 // device in platform then load the boot file on this full device path and get the
2296 ImageHandle
= BdsExpandUsbShortFormDevicePath (DevicePath
);
2299 // Adjust the different type memory page number just before booting
2300 // and save the updated info into the variable for next boot to use
2302 BdsSetMemoryTypeInformationVariable ();
2305 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.
2306 // Here get the ImageHandle for the non USB class or WWID device path.
2308 if (ImageHandle
== NULL
) {
2309 ASSERT (Option
->DevicePath
!= NULL
);
2310 if ((DevicePathType (Option
->DevicePath
) == BBS_DEVICE_PATH
) &&
2311 (DevicePathSubType (Option
->DevicePath
) == BBS_BBS_DP
)
2314 // Check to see if we should legacy BOOT. If yes then do the legacy boot
2316 return BdsLibDoLegacyBoot (Option
);
2320 // If the boot option point to Internal FV shell, make sure it is valid
2322 Status
= BdsLibUpdateFvFileDevicePath (&DevicePath
, PcdGetPtr(PcdShellFile
));
2323 if (!EFI_ERROR(Status
)) {
2324 if (Option
->DevicePath
!= NULL
) {
2325 FreePool(Option
->DevicePath
);
2327 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
2328 ASSERT(Option
->DevicePath
!= NULL
);
2329 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
2331 // Update the shell boot option
2333 InitializeListHead (&TempBootLists
);
2334 BdsLibRegisterNewOption (&TempBootLists
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
2337 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
2339 FreePool (DevicePath
);
2340 DevicePath
= Option
->DevicePath
;
2345 if (Option
->Description
== NULL
) {
2346 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting from unknown device path\n"));
2348 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Booting %S\n", Option
->Description
));
2354 // Report status code for OS Loader LoadImage.
2356 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2357 Status
= gBS
->LoadImage (
2367 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2368 // and load the image according to the default boot behavior for removable device.
2370 if (EFI_ERROR (Status
)) {
2372 // check if there is a bootable removable media could be found in this device path ,
2373 // and get the bootable media handle
2375 Handle
= BdsLibGetBootableHandle(DevicePath
);
2376 if (Handle
!= NULL
) {
2378 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2379 // machinename is ia32, ia64, x64, ...
2381 FilePath
= FileDevicePath (Handle
, EFI_REMOVABLE_MEDIA_FILE_NAME
);
2382 if (FilePath
!= NULL
) {
2383 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderLoad
));
2384 Status
= gBS
->LoadImage (
2397 // Provide the image with it's load options
2399 if ((ImageHandle
== NULL
) || (EFI_ERROR(Status
))) {
2401 // Report Status Code to indicate that the failure to load boot option
2403 REPORT_STATUS_CODE (
2404 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2405 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR
)
2410 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
2411 ASSERT_EFI_ERROR (Status
);
2413 if (Option
->LoadOptionsSize
!= 0) {
2414 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
2415 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
2419 // Clean to NULL because the image is loaded directly from the firmwares boot manager.
2421 ImageInfo
->ParentHandle
= NULL
;
2424 // Before calling the image, enable the Watchdog Timer for
2425 // the 5 Minute period
2427 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
2430 // Write boot to OS performance data for UEFI boot
2433 WriteBootToOsPerformanceData (NULL
, NULL
);
2437 // Report status code for OS Loader StartImage.
2439 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdProgressCodeOsLoaderStart
));
2441 Status
= gBS
->StartImage (ImageHandle
, ExitDataSize
, ExitData
);
2442 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Image Return Status = %r\n", Status
));
2443 if (EFI_ERROR (Status
)) {
2445 // Report Status Code to indicate that boot failure
2447 REPORT_STATUS_CODE (
2448 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
2449 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED
)
2454 // Clear the Watchdog Timer after the image returns
2456 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
2460 // Set Logo status invalid after trying one boot option
2463 StatusLogo
= gBS
->LocateProtocol (&gEfiBootLogoProtocolGuid
, NULL
, (VOID
**) &BootLogo
);
2464 if (!EFI_ERROR (StatusLogo
) && (BootLogo
!= NULL
)) {
2465 BootLogo
->SetBootLogo (BootLogo
, NULL
, 0, 0, 0, 0);
2469 // Clear Boot Current
2473 &gEfiGlobalVariableGuid
,
2474 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
2476 &Option
->BootCurrent
2484 Expand a device path that starts with a hard drive media device path node to be a
2485 full device path that includes the full hardware path to the device. We need
2486 to do this so it can be booted. As an optimization the front match (the part point
2487 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
2488 so a connect all is not required on every boot. All successful history device path
2489 which point to partition node (the front part) will be saved.
2491 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
2492 drive media device path.
2493 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
2497 EFI_DEVICE_PATH_PROTOCOL
*
2499 BdsExpandPartitionPartialDevicePathToFull (
2500 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2504 UINTN BlockIoHandleCount
;
2505 EFI_HANDLE
*BlockIoBuffer
;
2506 EFI_DEVICE_PATH_PROTOCOL
*FullDevicePath
;
2507 EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
;
2508 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2511 EFI_DEVICE_PATH_PROTOCOL
*CachedDevicePath
;
2512 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
2513 UINTN CachedDevicePathSize
;
2514 BOOLEAN DeviceExist
;
2516 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
2519 FullDevicePath
= NULL
;
2521 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2522 // If exist, search the front path which point to partition node in the variable instants.
2523 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system
2525 CachedDevicePath
= BdsLibGetVariableAndSize (
2526 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2527 &gHdBootDevicePathVariablGuid
,
2528 &CachedDevicePathSize
2531 if (CachedDevicePath
!= NULL
) {
2532 TempNewDevicePath
= CachedDevicePath
;
2533 DeviceExist
= FALSE
;
2537 // Check every instance of the variable
2538 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
2539 // partial partition boot option. Second, check whether the instance could be connected.
2541 Instance
= GetNextDevicePathInstance (&TempNewDevicePath
, &Size
);
2542 if (MatchPartitionDevicePathNode (Instance
, HardDriveDevicePath
)) {
2544 // Connect the device path instance, the device path point to hard drive media device path node
2545 // e.g. ACPI() /PCI()/ATA()/Partition()
2547 Status
= BdsLibConnectDevicePath (Instance
);
2548 if (!EFI_ERROR (Status
)) {
2554 // Come here means the first instance is not matched
2558 } while (TempNewDevicePath
!= NULL
);
2562 // Find the matched device path.
2563 // Append the file path information from the boot option and return the fully expanded device path.
2565 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2566 FullDevicePath
= AppendDevicePath (Instance
, DevicePath
);
2569 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.
2573 // First delete the matched instance.
2575 TempNewDevicePath
= CachedDevicePath
;
2576 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, Instance
);
2577 FreePool (TempNewDevicePath
);
2580 // Second, append the remaining path after the matched instance
2582 TempNewDevicePath
= CachedDevicePath
;
2583 CachedDevicePath
= AppendDevicePathInstance (Instance
, CachedDevicePath
);
2584 FreePool (TempNewDevicePath
);
2586 // Save the matching Device Path so we don't need to do a connect all next time
2588 Status
= gRT
->SetVariable (
2589 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2590 &gHdBootDevicePathVariablGuid
,
2591 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2592 GetDevicePathSize (CachedDevicePath
),
2597 FreePool (Instance
);
2598 FreePool (CachedDevicePath
);
2599 return FullDevicePath
;
2604 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need
2605 // to search all devices in the system for a matched partition
2607 BdsLibConnectAllDriversToAllControllers ();
2608 Status
= gBS
->LocateHandleBuffer (ByProtocol
, &gEfiBlockIoProtocolGuid
, NULL
, &BlockIoHandleCount
, &BlockIoBuffer
);
2609 if (EFI_ERROR (Status
) || BlockIoHandleCount
== 0 || BlockIoBuffer
== NULL
) {
2611 // If there was an error or there are no device handles that support
2612 // the BLOCK_IO Protocol, then return.
2617 // Loop through all the device handles that support the BLOCK_IO Protocol
2619 for (Index
= 0; Index
< BlockIoHandleCount
; Index
++) {
2621 Status
= gBS
->HandleProtocol (BlockIoBuffer
[Index
], &gEfiDevicePathProtocolGuid
, (VOID
*) &BlockIoDevicePath
);
2622 if (EFI_ERROR (Status
) || BlockIoDevicePath
== NULL
) {
2626 if (MatchPartitionDevicePathNode (BlockIoDevicePath
, HardDriveDevicePath
)) {
2628 // Find the matched partition device path
2630 DevicePath
= NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL
*) HardDriveDevicePath
);
2631 FullDevicePath
= AppendDevicePath (BlockIoDevicePath
, DevicePath
);
2634 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2636 if (CachedDevicePath
!= NULL
) {
2638 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2640 if (BdsLibMatchDevicePaths (CachedDevicePath
, BlockIoDevicePath
)) {
2641 TempNewDevicePath
= CachedDevicePath
;
2642 CachedDevicePath
= BdsLibDelPartMatchInstance (CachedDevicePath
, BlockIoDevicePath
);
2643 FreePool(TempNewDevicePath
);
2645 TempNewDevicePath
= CachedDevicePath
;
2646 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
2647 if (TempNewDevicePath
!= NULL
) {
2648 FreePool(TempNewDevicePath
);
2651 TempNewDevicePath
= CachedDevicePath
;
2652 CachedDevicePath
= AppendDevicePathInstance (BlockIoDevicePath
, CachedDevicePath
);
2653 FreePool(TempNewDevicePath
);
2656 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
2657 // If the user try to boot many OS in different HDs or partitions, in theory,
2658 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.
2661 ASSERT (CachedDevicePath
!= NULL
);
2662 TempNewDevicePath
= CachedDevicePath
;
2663 while (!IsDevicePathEnd (TempNewDevicePath
)) {
2664 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2666 // Parse one instance
2668 while (!IsDevicePathEndType (TempNewDevicePath
)) {
2669 TempNewDevicePath
= NextDevicePathNode (TempNewDevicePath
);
2673 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
2675 if (InstanceNum
>= 12) {
2676 SetDevicePathEndNode (TempNewDevicePath
);
2681 CachedDevicePath
= DuplicateDevicePath (BlockIoDevicePath
);
2685 // Save the matching Device Path so we don't need to do a connect all next time
2687 Status
= gRT
->SetVariable (
2688 HD_BOOT_DEVICE_PATH_VARIABLE_NAME
,
2689 &gHdBootDevicePathVariablGuid
,
2690 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2691 GetDevicePathSize (CachedDevicePath
),
2699 if (CachedDevicePath
!= NULL
) {
2700 FreePool (CachedDevicePath
);
2702 if (BlockIoBuffer
!= NULL
) {
2703 FreePool (BlockIoBuffer
);
2705 return FullDevicePath
;
2709 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
2710 instances, has the same partition node with HardDriveDevicePath device path
2712 @param BlockIoDevicePath Multi device path instances which need to check
2713 @param HardDriveDevicePath A device path which starts with a hard drive media
2716 @retval TRUE There is a matched device path instance.
2717 @retval FALSE There is no matched device path instance.
2722 MatchPartitionDevicePathNode (
2723 IN EFI_DEVICE_PATH_PROTOCOL
*BlockIoDevicePath
,
2724 IN HARDDRIVE_DEVICE_PATH
*HardDriveDevicePath
2727 HARDDRIVE_DEVICE_PATH
*TmpHdPath
;
2728 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2730 EFI_DEVICE_PATH_PROTOCOL
*BlockIoHdDevicePathNode
;
2732 if ((BlockIoDevicePath
== NULL
) || (HardDriveDevicePath
== NULL
)) {
2737 // Make PreviousDevicePath == the device path node before the end node
2739 DevicePath
= BlockIoDevicePath
;
2740 BlockIoHdDevicePathNode
= NULL
;
2743 // find the partition device path node
2745 while (!IsDevicePathEnd (DevicePath
)) {
2746 if ((DevicePathType (DevicePath
) == MEDIA_DEVICE_PATH
) &&
2747 (DevicePathSubType (DevicePath
) == MEDIA_HARDDRIVE_DP
)
2749 BlockIoHdDevicePathNode
= DevicePath
;
2753 DevicePath
= NextDevicePathNode (DevicePath
);
2756 if (BlockIoHdDevicePathNode
== NULL
) {
2760 // See if the harddrive device path in blockio matches the orig Hard Drive Node
2762 TmpHdPath
= (HARDDRIVE_DEVICE_PATH
*) BlockIoHdDevicePathNode
;
2766 // Check for the match
2768 if ((TmpHdPath
->MBRType
== HardDriveDevicePath
->MBRType
) &&
2769 (TmpHdPath
->SignatureType
== HardDriveDevicePath
->SignatureType
)) {
2770 switch (TmpHdPath
->SignatureType
) {
2771 case SIGNATURE_TYPE_GUID
:
2772 Match
= CompareGuid ((EFI_GUID
*)TmpHdPath
->Signature
, (EFI_GUID
*)HardDriveDevicePath
->Signature
);
2774 case SIGNATURE_TYPE_MBR
:
2775 Match
= (BOOLEAN
)(*((UINT32
*)(&(TmpHdPath
->Signature
[0]))) == ReadUnaligned32((UINT32
*)(&(HardDriveDevicePath
->Signature
[0]))));
2787 Delete the boot option associated with the handle passed in.
2789 @param Handle The handle which present the device path to create
2792 @retval EFI_SUCCESS Delete the boot option success
2793 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2794 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2795 @retval Other Error return value from SetVariable()
2799 BdsLibDeleteOptionFromHandle (
2800 IN EFI_HANDLE Handle
2804 UINT8
*BootOptionVar
;
2805 UINTN BootOrderSize
;
2806 UINTN BootOptionSize
;
2809 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2810 UINTN DevicePathSize
;
2811 UINTN OptionDevicePathSize
;
2812 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
2813 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2816 Status
= EFI_SUCCESS
;
2821 // Check "BootOrder" variable, if no, means there is no any boot order.
2823 BootOrder
= BdsLibGetVariableAndSize (
2825 &gEfiGlobalVariableGuid
,
2828 if (BootOrder
== NULL
) {
2829 return EFI_NOT_FOUND
;
2833 // Convert device handle to device path protocol instance
2835 DevicePath
= DevicePathFromHandle (Handle
);
2836 if (DevicePath
== NULL
) {
2837 return EFI_NOT_FOUND
;
2839 DevicePathSize
= GetDevicePathSize (DevicePath
);
2842 // Loop all boot order variable and find the matching device path
2845 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2846 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2847 BootOptionVar
= BdsLibGetVariableAndSize (
2849 &gEfiGlobalVariableGuid
,
2853 if (BootOptionVar
== NULL
) {
2854 FreePool (BootOrder
);
2855 return EFI_OUT_OF_RESOURCES
;
2858 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2859 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2860 FreePool (BootOptionVar
);
2865 TempPtr
= BootOptionVar
;
2866 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
2867 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
2868 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
2869 OptionDevicePathSize
= GetDevicePathSize (OptionDevicePath
);
2872 // Check whether the device path match
2874 if ((OptionDevicePathSize
== DevicePathSize
) &&
2875 (CompareMem (DevicePath
, OptionDevicePath
, DevicePathSize
) == 0)) {
2876 BdsDeleteBootOption (BootOrder
[Index
], BootOrder
, &BootOrderSize
);
2877 FreePool (BootOptionVar
);
2881 FreePool (BootOptionVar
);
2886 // Adjust number of boot option for "BootOrder" variable.
2888 Status
= gRT
->SetVariable (
2890 &gEfiGlobalVariableGuid
,
2891 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2896 FreePool (BootOrder
);
2903 Delete all invalid EFI boot options.
2905 @retval EFI_SUCCESS Delete all invalid boot option success
2906 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
2907 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2908 @retval Other Error return value from SetVariable()
2912 BdsDeleteAllInvalidEfiBootOption (
2917 UINT8
*BootOptionVar
;
2918 UINTN BootOrderSize
;
2919 UINTN BootOptionSize
;
2923 UINT16 BootOption
[BOOT_OPTION_MAX_CHAR
];
2924 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
2926 CHAR16
*Description
;
2929 Status
= EFI_SUCCESS
;
2932 OptionDevicePath
= NULL
;
2937 // Check "BootOrder" variable firstly, this variable hold the number of boot options
2939 BootOrder
= BdsLibGetVariableAndSize (
2941 &gEfiGlobalVariableGuid
,
2944 if (NULL
== BootOrder
) {
2945 return EFI_NOT_FOUND
;
2949 while (Index
< BootOrderSize
/ sizeof (UINT16
)) {
2950 UnicodeSPrint (BootOption
, sizeof (BootOption
), L
"Boot%04x", BootOrder
[Index
]);
2951 BootOptionVar
= BdsLibGetVariableAndSize (
2953 &gEfiGlobalVariableGuid
,
2956 if (NULL
== BootOptionVar
) {
2957 FreePool (BootOrder
);
2958 return EFI_OUT_OF_RESOURCES
;
2961 if (!ValidateOption(BootOptionVar
, BootOptionSize
)) {
2964 TempPtr
= BootOptionVar
;
2965 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
2966 Description
= (CHAR16
*) TempPtr
;
2967 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
2968 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
2971 // Skip legacy boot option (BBS boot device)
2973 if ((DevicePathType (OptionDevicePath
) == BBS_DEVICE_PATH
) &&
2974 (DevicePathSubType (OptionDevicePath
) == BBS_BBS_DP
)) {
2975 FreePool (BootOptionVar
);
2981 if (Corrupted
|| !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath
, FALSE
, Description
)) {
2983 // Delete this invalid boot option "Boot####"
2985 Status
= gRT
->SetVariable (
2987 &gEfiGlobalVariableGuid
,
2988 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
2993 // Mark this boot option in boot order as deleted
2995 BootOrder
[Index
] = 0xffff;
2999 FreePool (BootOptionVar
);
3004 // Adjust boot order array
3007 for (Index
= 0; Index
< BootOrderSize
/ sizeof (UINT16
); Index
++) {
3008 if (BootOrder
[Index
] != 0xffff) {
3009 BootOrder
[Index2
] = BootOrder
[Index
];
3013 Status
= gRT
->SetVariable (
3015 &gEfiGlobalVariableGuid
,
3016 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3017 Index2
* sizeof (UINT16
),
3021 FreePool (BootOrder
);
3028 For EFI boot option, BDS separate them as six types:
3029 1. Network - The boot option points to the SimpleNetworkProtocol device.
3030 Bds will try to automatically create this type boot option when enumerate.
3031 2. Shell - The boot option points to internal flash shell.
3032 Bds will try to automatically create this type boot option when enumerate.
3033 3. Removable BlockIo - The boot option only points to the removable media
3034 device, like USB flash disk, DVD, Floppy etc.
3035 These device should contain a *removable* blockIo
3036 protocol in their device handle.
3037 Bds will try to automatically create this type boot option
3039 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
3041 These device should contain a *fixed* blockIo
3042 protocol in their device handle.
3043 BDS will skip fixed blockIo devices, and NOT
3044 automatically create boot option for them. But BDS
3045 will help to delete those fixed blockIo boot option,
3046 whose description rule conflict with other auto-created
3048 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
3049 has SimpleFileSystem Protocol, but has no blockio
3050 protocol. These devices do not offer blockIo
3051 protocol, but BDS still can get the
3052 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
3054 6. File - The boot option points to a file. These boot options are usually
3055 created by user manually or OS loader. BDS will not delete or modify
3058 This function will enumerate all possible boot device in the system, and
3059 automatically create boot options for Network, Shell, Removable BlockIo,
3060 and Non-BlockIo Simplefile devices.
3061 It will only execute once of every boot.
3063 @param BdsBootOptionList The header of the link list which indexed all
3064 current boot options
3066 @retval EFI_SUCCESS Finished all the boot device enumerate and create
3067 the boot option base on that boot device
3069 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
3073 BdsLibEnumerateAllBootOption (
3074 IN OUT LIST_ENTRY
*BdsBootOptionList
3078 UINT16 FloppyNumber
;
3079 UINT16 HarddriveNumber
;
3084 UINT16 NonBlockNumber
;
3085 UINTN NumberBlockIoHandles
;
3086 EFI_HANDLE
*BlockIoHandles
;
3087 EFI_BLOCK_IO_PROTOCOL
*BlkIo
;
3088 BOOLEAN Removable
[2];
3089 UINTN RemovableIndex
;
3091 UINTN NumOfLoadFileHandles
;
3092 EFI_HANDLE
*LoadFileHandles
;
3093 UINTN FvHandleCount
;
3094 EFI_HANDLE
*FvHandleBuffer
;
3095 EFI_FV_FILETYPE Type
;
3097 EFI_FV_FILE_ATTRIBUTES Attributes
;
3098 UINT32 AuthenticationStatus
;
3099 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
3100 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3101 UINTN DevicePathType
;
3103 EFI_HANDLE
*FileSystemHandles
;
3104 UINTN NumberFileSystemHandles
;
3106 EFI_IMAGE_DOS_HEADER DosHeader
;
3109 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3110 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3113 HarddriveNumber
= 0;
3120 ZeroMem (Buffer
, sizeof (Buffer
));
3123 // If the boot device enumerate happened, just get the boot
3124 // device from the boot order variable
3126 if (mEnumBootDevice
) {
3127 GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME
, &gLastEnumLangGuid
, (VOID
**)&LastLang
, NULL
);
3128 GetEfiGlobalVariable2 (L
"PlatformLang", (VOID
**)&PlatLang
, NULL
);
3129 ASSERT (PlatLang
!= NULL
);
3130 if ((LastLang
!= NULL
) && (AsciiStrCmp (LastLang
, PlatLang
) == 0)) {
3131 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3132 FreePool (LastLang
);
3133 FreePool (PlatLang
);
3136 Status
= gRT
->SetVariable (
3137 LAST_ENUM_LANGUAGE_VARIABLE_NAME
,
3139 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3140 AsciiStrSize (PlatLang
),
3143 ASSERT_EFI_ERROR (Status
);
3145 if (LastLang
!= NULL
) {
3146 FreePool (LastLang
);
3148 FreePool (PlatLang
);
3153 // Notes: this dirty code is to get the legacy boot option from the
3154 // BBS table and create to variable as the EFI boot option, it should
3155 // be removed after the CSM can provide legacy boot option directly
3157 REFRESH_LEGACY_BOOT_OPTIONS
;
3160 // Delete invalid boot option
3162 BdsDeleteAllInvalidEfiBootOption ();
3165 // Parse removable media followed by fixed media.
3166 // The Removable[] array is used by the for-loop below to create removable media boot options
3167 // at first, and then to create fixed media boot options.
3169 Removable
[0] = FALSE
;
3170 Removable
[1] = TRUE
;
3172 gBS
->LocateHandleBuffer (
3174 &gEfiBlockIoProtocolGuid
,
3176 &NumberBlockIoHandles
,
3180 for (RemovableIndex
= 0; RemovableIndex
< 2; RemovableIndex
++) {
3181 for (Index
= 0; Index
< NumberBlockIoHandles
; Index
++) {
3182 Status
= gBS
->HandleProtocol (
3183 BlockIoHandles
[Index
],
3184 &gEfiBlockIoProtocolGuid
,
3188 // skip the fixed block io then the removable block io
3190 if (EFI_ERROR (Status
) || (BlkIo
->Media
->RemovableMedia
== Removable
[RemovableIndex
])) {
3193 DevicePath
= DevicePathFromHandle (BlockIoHandles
[Index
]);
3194 DevicePathType
= BdsGetBootTypeFromDevicePath (DevicePath
);
3196 switch (DevicePathType
) {
3197 case BDS_EFI_ACPI_FLOPPY_BOOT
:
3198 if (FloppyNumber
!= 0) {
3199 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)), FloppyNumber
);
3201 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY
)));
3203 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3208 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.
3210 case BDS_EFI_MESSAGE_ATAPI_BOOT
:
3211 case BDS_EFI_MESSAGE_SATA_BOOT
:
3212 if (BlkIo
->Media
->RemovableMedia
) {
3213 if (CdromNumber
!= 0) {
3214 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)), CdromNumber
);
3216 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD
)));
3220 if (HarddriveNumber
!= 0) {
3221 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)), HarddriveNumber
);
3223 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE
)));
3227 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Buffer: %S\n", Buffer
));
3228 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3231 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT
:
3232 if (UsbNumber
!= 0) {
3233 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)), UsbNumber
);
3235 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB
)));
3237 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3241 case BDS_EFI_MESSAGE_SCSI_BOOT
:
3242 if (ScsiNumber
!= 0) {
3243 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)), ScsiNumber
);
3245 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI
)));
3247 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3251 case BDS_EFI_MESSAGE_MISC_BOOT
:
3252 if (MiscNumber
!= 0) {
3253 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)), MiscNumber
);
3255 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC
)));
3257 BdsLibBuildOptionFromHandle (BlockIoHandles
[Index
], BdsBootOptionList
, Buffer
);
3267 if (NumberBlockIoHandles
!= 0) {
3268 FreePool (BlockIoHandles
);
3272 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
3275 gBS
->LocateHandleBuffer (
3277 &gEfiSimpleFileSystemProtocolGuid
,
3279 &NumberFileSystemHandles
,
3282 for (Index
= 0; Index
< NumberFileSystemHandles
; Index
++) {
3283 Status
= gBS
->HandleProtocol (
3284 FileSystemHandles
[Index
],
3285 &gEfiBlockIoProtocolGuid
,
3288 if (!EFI_ERROR (Status
)) {
3290 // Skip if the file system handle supports a BlkIo protocol,
3296 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
3297 // machinename is ia32, ia64, x64, ...
3299 Hdr
.Union
= &HdrData
;
3301 Status
= BdsLibGetImageHeader (
3302 FileSystemHandles
[Index
],
3303 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3307 if (!EFI_ERROR (Status
) &&
3308 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3309 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3315 // No such file or the file is not a EFI application, delete this boot option
3317 BdsLibDeleteOptionFromHandle (FileSystemHandles
[Index
]);
3319 if (NonBlockNumber
!= 0) {
3320 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)), NonBlockNumber
);
3322 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK
)));
3324 BdsLibBuildOptionFromHandle (FileSystemHandles
[Index
], BdsBootOptionList
, Buffer
);
3329 if (NumberFileSystemHandles
!= 0) {
3330 FreePool (FileSystemHandles
);
3334 // Parse Network Boot Device
3336 NumOfLoadFileHandles
= 0;
3338 // Search Load File protocol for PXE boot option.
3340 gBS
->LocateHandleBuffer (
3342 &gEfiLoadFileProtocolGuid
,
3344 &NumOfLoadFileHandles
,
3348 for (Index
= 0; Index
< NumOfLoadFileHandles
; Index
++) {
3350 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)), Index
);
3352 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK
)));
3354 BdsLibBuildOptionFromHandle (LoadFileHandles
[Index
], BdsBootOptionList
, Buffer
);
3357 if (NumOfLoadFileHandles
!= 0) {
3358 FreePool (LoadFileHandles
);
3362 // Check if we have on flash shell
3364 gBS
->LocateHandleBuffer (
3366 &gEfiFirmwareVolume2ProtocolGuid
,
3371 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
3372 gBS
->HandleProtocol (
3373 FvHandleBuffer
[Index
],
3374 &gEfiFirmwareVolume2ProtocolGuid
,
3378 Status
= Fv
->ReadFile (
3380 PcdGetPtr(PcdShellFile
),
3385 &AuthenticationStatus
3387 if (EFI_ERROR (Status
)) {
3389 // Skip if no shell file in the FV
3394 // Build the shell boot option
3396 BdsLibBuildOptionFromShell (FvHandleBuffer
[Index
], BdsBootOptionList
);
3399 if (FvHandleCount
!= 0) {
3400 FreePool (FvHandleBuffer
);
3403 // Make sure every boot only have one time
3404 // boot device enumerate
3406 Status
= BdsLibBuildOptionFromVar (BdsBootOptionList
, L
"BootOrder");
3407 mEnumBootDevice
= TRUE
;
3413 Build the boot option with the handle parsed in
3415 @param Handle The handle which present the device path to create
3417 @param BdsBootOptionList The header of the link list which indexed all
3418 current boot options
3419 @param String The description of the boot option.
3424 BdsLibBuildOptionFromHandle (
3425 IN EFI_HANDLE Handle
,
3426 IN LIST_ENTRY
*BdsBootOptionList
,
3430 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3432 DevicePath
= DevicePathFromHandle (Handle
);
3435 // Create and register new boot option
3437 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, String
, L
"BootOrder");
3442 Build the on flash shell boot option with the handle parsed in.
3444 @param Handle The handle which present the device path to create
3445 on flash shell boot option
3446 @param BdsBootOptionList The header of the link list which indexed all
3447 current boot options
3452 BdsLibBuildOptionFromShell (
3453 IN EFI_HANDLE Handle
,
3454 IN OUT LIST_ENTRY
*BdsBootOptionList
3457 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
3458 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode
;
3460 DevicePath
= DevicePathFromHandle (Handle
);
3463 // Build the shell device path
3465 EfiInitializeFwVolDevicepathNode (&ShellNode
, PcdGetPtr(PcdShellFile
));
3467 DevicePath
= AppendDevicePathNode (DevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &ShellNode
);
3470 // Create and register the shell boot option
3472 BdsLibRegisterNewOption (BdsBootOptionList
, DevicePath
, L
"EFI Internal Shell", L
"BootOrder");
3477 Boot from the UEFI spec defined "BootNext" variable.
3489 BDS_COMMON_OPTION
*BootOption
;
3490 LIST_ENTRY TempList
;
3495 // Init the boot option name buffer and temp link list
3497 InitializeListHead (&TempList
);
3498 ZeroMem (Buffer
, sizeof (Buffer
));
3500 BootNext
= BdsLibGetVariableAndSize (
3502 &gEfiGlobalVariableGuid
,
3507 // Clear the boot next variable first
3509 if (BootNext
!= NULL
) {
3512 &gEfiGlobalVariableGuid
,
3513 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
3519 // Start to build the boot option and try to boot
3521 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", *BootNext
);
3522 BootOption
= BdsLibVariableToOption (&TempList
, Buffer
);
3523 ASSERT (BootOption
!= NULL
);
3524 BdsLibConnectDevicePath (BootOption
->DevicePath
);
3525 BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
3531 Return the bootable media handle.
3532 First, check the device is connected
3533 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
3534 Third, detect the the default boot file in the Media, and return the removable Media handle.
3536 @param DevicePath Device Path to a bootable device
3538 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
3543 BdsLibGetBootableHandle (
3544 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3549 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3550 EFI_DEVICE_PATH_PROTOCOL
*DupDevicePath
;
3552 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
3554 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3557 EFI_HANDLE ReturnHandle
;
3558 EFI_HANDLE
*SimpleFileSystemHandles
;
3560 UINTN NumberSimpleFileSystemHandles
;
3562 EFI_IMAGE_DOS_HEADER DosHeader
;
3563 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
3564 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
3566 UpdatedDevicePath
= DevicePath
;
3569 // Enter to critical section to protect the acquired BlockIo instance
3570 // from getting released due to the USB mass storage hotplug event
3572 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
3575 // Check whether the device is connected
3577 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3578 if (EFI_ERROR (Status
)) {
3580 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
3582 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3583 if (EFI_ERROR (Status
)) {
3585 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
3587 UpdatedDevicePath
= DevicePath
;
3588 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3589 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3593 // For removable device boot option, its contained device path only point to the removable device handle,
3594 // should make sure all its children handles (its child partion or media handles) are created and connected.
3596 gBS
->ConnectController (Handle
, NULL
, NULL
, TRUE
);
3598 // Get BlockIo protocol and check removable attribute
3600 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
3601 ASSERT_EFI_ERROR (Status
);
3604 // Issue a dummy read to the device to check for media change.
3605 // When the removable media is changed, any Block IO read/write will
3606 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
3607 // returned. After the Block IO protocol is reinstalled, subsequent
3608 // Block IO read/write will success.
3610 Buffer
= AllocatePool (BlockIo
->Media
->BlockSize
);
3611 if (Buffer
!= NULL
) {
3612 BlockIo
->ReadBlocks (
3614 BlockIo
->Media
->MediaId
,
3616 BlockIo
->Media
->BlockSize
,
3624 // Detect the the default boot file from removable Media
3628 // 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
3629 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
3631 DupDevicePath
= DuplicateDevicePath (DevicePath
);
3632 ASSERT (DupDevicePath
!= NULL
);
3634 UpdatedDevicePath
= DupDevicePath
;
3635 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3637 // 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
3638 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
3640 if ((DevicePathType (UpdatedDevicePath
) == MESSAGING_DEVICE_PATH
) &&
3641 (DevicePathSubType (UpdatedDevicePath
) == MSG_USB_DP
)) {
3643 // Remove the usb node, let the device path only point to PCI node
3645 SetDevicePathEndNode (UpdatedDevicePath
);
3646 UpdatedDevicePath
= DupDevicePath
;
3648 UpdatedDevicePath
= DevicePath
;
3652 // Get the device path size of boot option
3654 Size
= GetDevicePathSize(UpdatedDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3655 ReturnHandle
= NULL
;
3656 gBS
->LocateHandleBuffer (
3658 &gEfiSimpleFileSystemProtocolGuid
,
3660 &NumberSimpleFileSystemHandles
,
3661 &SimpleFileSystemHandles
3663 for (Index
= 0; Index
< NumberSimpleFileSystemHandles
; Index
++) {
3665 // Get the device path size of SimpleFileSystem handle
3667 TempDevicePath
= DevicePathFromHandle (SimpleFileSystemHandles
[Index
]);
3668 TempSize
= GetDevicePathSize (TempDevicePath
)- sizeof (EFI_DEVICE_PATH_PROTOCOL
); // minus the end node
3670 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
3672 if (Size
<= TempSize
&& CompareMem (TempDevicePath
, UpdatedDevicePath
, Size
)==0) {
3674 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
3675 // machinename is ia32, ia64, x64, ...
3677 Hdr
.Union
= &HdrData
;
3678 Status
= BdsLibGetImageHeader (
3679 SimpleFileSystemHandles
[Index
],
3680 EFI_REMOVABLE_MEDIA_FILE_NAME
,
3684 if (!EFI_ERROR (Status
) &&
3685 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr
.Pe32
->FileHeader
.Machine
) &&
3686 Hdr
.Pe32
->OptionalHeader
.Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) {
3687 ReturnHandle
= SimpleFileSystemHandles
[Index
];
3693 FreePool(DupDevicePath
);
3695 if (SimpleFileSystemHandles
!= NULL
) {
3696 FreePool(SimpleFileSystemHandles
);
3699 gBS
->RestoreTPL (OldTpl
);
3701 return ReturnHandle
;
3705 Check to see if the network cable is plugged in. If the DevicePath is not
3706 connected it will be connected.
3708 @param DevicePath Device Path to check
3710 @retval TRUE DevicePath points to an Network that is connected
3711 @retval FALSE DevicePath does not point to a bootable network
3715 BdsLibNetworkBootWithMediaPresent (
3716 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3720 EFI_DEVICE_PATH_PROTOCOL
*UpdatedDevicePath
;
3722 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
3723 BOOLEAN MediaPresent
;
3724 UINT32 InterruptStatus
;
3726 MediaPresent
= FALSE
;
3728 UpdatedDevicePath
= DevicePath
;
3730 // Locate Load File Protocol for PXE boot option first
3732 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3733 if (EFI_ERROR (Status
)) {
3735 // Device not present so see if we need to connect it
3737 Status
= BdsLibConnectDevicePath (DevicePath
);
3738 if (!EFI_ERROR (Status
)) {
3740 // This one should work after we did the connect
3742 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3746 if (!EFI_ERROR (Status
)) {
3747 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**)&Snp
);
3748 if (EFI_ERROR (Status
)) {
3750 // Failed to open SNP from this handle, try to get SNP from parent handle
3752 UpdatedDevicePath
= DevicePathFromHandle (Handle
);
3753 if (UpdatedDevicePath
!= NULL
) {
3754 Status
= gBS
->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid
, &UpdatedDevicePath
, &Handle
);
3755 if (!EFI_ERROR (Status
)) {
3757 // SNP handle found, get SNP from it
3759 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleNetworkProtocolGuid
, (VOID
**) &Snp
);
3764 if (!EFI_ERROR (Status
)) {
3765 if (Snp
->Mode
->MediaPresentSupported
) {
3766 if (Snp
->Mode
->State
== EfiSimpleNetworkInitialized
) {
3768 // Invoke Snp->GetStatus() to refresh the media status
3770 Snp
->GetStatus (Snp
, &InterruptStatus
, NULL
);
3773 // In case some one else is using the SNP check to see if it's connected
3775 MediaPresent
= Snp
->Mode
->MediaPresent
;
3778 // No one is using SNP so we need to Start and Initialize so
3779 // MediaPresent will be valid.
3781 Status
= Snp
->Start (Snp
);
3782 if (!EFI_ERROR (Status
)) {
3783 Status
= Snp
->Initialize (Snp
, 0, 0);
3784 if (!EFI_ERROR (Status
)) {
3785 MediaPresent
= Snp
->Mode
->MediaPresent
;
3786 Snp
->Shutdown (Snp
);
3792 MediaPresent
= TRUE
;
3797 return MediaPresent
;
3801 For a bootable Device path, return its boot type.
3803 @param DevicePath The bootable device Path to check
3805 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3806 which subtype is MEDIA_HARDDRIVE_DP
3807 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3808 which subtype is MEDIA_CDROM_DP
3809 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
3810 which HID is floppy device.
3811 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3812 and its last device path node's subtype is MSG_ATAPI_DP.
3813 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3814 and its last device path node's subtype is MSG_SCSI_DP.
3815 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3816 and its last device path node's subtype is MSG_USB_DP.
3817 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
3818 its last device path node point to a message device path node.
3819 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
3820 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
3825 BdsGetBootTypeFromDevicePath (
3826 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
3829 ACPI_HID_DEVICE_PATH
*Acpi
;
3830 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3831 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
3834 if (NULL
== DevicePath
) {
3835 return BDS_EFI_UNSUPPORT
;
3838 TempDevicePath
= DevicePath
;
3840 while (!IsDevicePathEndType (TempDevicePath
)) {
3841 switch (DevicePathType (TempDevicePath
)) {
3842 case BBS_DEVICE_PATH
:
3843 return BDS_LEGACY_BBS_BOOT
;
3844 case MEDIA_DEVICE_PATH
:
3845 if (DevicePathSubType (TempDevicePath
) == MEDIA_HARDDRIVE_DP
) {
3846 return BDS_EFI_MEDIA_HD_BOOT
;
3847 } else if (DevicePathSubType (TempDevicePath
) == MEDIA_CDROM_DP
) {
3848 return BDS_EFI_MEDIA_CDROM_BOOT
;
3851 case ACPI_DEVICE_PATH
:
3852 Acpi
= (ACPI_HID_DEVICE_PATH
*) TempDevicePath
;
3853 if (EISA_ID_TO_NUM (Acpi
->HID
) == 0x0604) {
3854 return BDS_EFI_ACPI_FLOPPY_BOOT
;
3857 case MESSAGING_DEVICE_PATH
:
3859 // Get the last device path node
3861 LastDeviceNode
= NextDevicePathNode (TempDevicePath
);
3862 if (DevicePathSubType(LastDeviceNode
) == MSG_DEVICE_LOGICAL_UNIT_DP
) {
3864 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
3867 LastDeviceNode
= NextDevicePathNode (LastDeviceNode
);
3870 // if the device path not only point to driver device, it is not a messaging device path,
3872 if (!IsDevicePathEndType (LastDeviceNode
)) {
3876 switch (DevicePathSubType (TempDevicePath
)) {
3878 BootType
= BDS_EFI_MESSAGE_ATAPI_BOOT
;
3882 BootType
= BDS_EFI_MESSAGE_USB_DEVICE_BOOT
;
3886 BootType
= BDS_EFI_MESSAGE_SCSI_BOOT
;
3890 BootType
= BDS_EFI_MESSAGE_SATA_BOOT
;
3893 case MSG_MAC_ADDR_DP
:
3897 BootType
= BDS_EFI_MESSAGE_MAC_BOOT
;
3901 BootType
= BDS_EFI_MESSAGE_MISC_BOOT
;
3909 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
3912 return BDS_EFI_UNSUPPORT
;
3916 Check whether the Device path in a boot option point to a valid bootable device,
3917 And if CheckMedia is true, check the device is ready to boot now.
3919 @param DevPath the Device path in a boot option
3920 @param CheckMedia if true, check the device is ready to boot now.
3922 @retval TRUE the Device path is valid
3923 @retval FALSE the Device path is invalid .
3928 BdsLibIsValidEFIBootOptDevicePath (
3929 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
3930 IN BOOLEAN CheckMedia
3933 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath
, CheckMedia
, NULL
);
3937 Check whether the Device path in a boot option point to a valid bootable device,
3938 And if CheckMedia is true, check the device is ready to boot now.
3939 If Description is not NULL and the device path point to a fixed BlockIo
3940 device, check the description whether conflict with other auto-created
3943 @param DevPath the Device path in a boot option
3944 @param CheckMedia if true, check the device is ready to boot now.
3945 @param Description the description in a boot option
3947 @retval TRUE the Device path is valid
3948 @retval FALSE the Device path is invalid .
3953 BdsLibIsValidEFIBootOptDevicePathExt (
3954 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
,
3955 IN BOOLEAN CheckMedia
,
3956 IN CHAR16
*Description
3961 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
3962 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
3963 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
3965 TempDevicePath
= DevPath
;
3966 LastDeviceNode
= DevPath
;
3969 // Check if it's a valid boot option for network boot device.
3970 // Check if there is EfiLoadFileProtocol installed.
3971 // If yes, that means there is a boot option for network.
3973 Status
= gBS
->LocateDevicePath (
3974 &gEfiLoadFileProtocolGuid
,
3978 if (EFI_ERROR (Status
)) {
3980 // Device not present so see if we need to connect it
3982 TempDevicePath
= DevPath
;
3983 BdsLibConnectDevicePath (TempDevicePath
);
3984 Status
= gBS
->LocateDevicePath (
3985 &gEfiLoadFileProtocolGuid
,
3991 if (!EFI_ERROR (Status
)) {
3992 if (!IsDevicePathEnd (TempDevicePath
)) {
3994 // LoadFile protocol is not installed on handle with exactly the same DevPath
4001 // Test if it is ready to boot now
4003 if (BdsLibNetworkBootWithMediaPresent(DevPath
)) {
4012 // If the boot option point to a file, it is a valid EFI boot option,
4013 // and assume it is ready to boot now
4015 while (!IsDevicePathEnd (TempDevicePath
)) {
4017 // If there is USB Class or USB WWID device path node, treat it as valid EFI
4018 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it
4019 // to full device path.
4021 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
4022 ((DevicePathSubType (TempDevicePath
) == MSG_USB_CLASS_DP
) ||
4023 (DevicePathSubType (TempDevicePath
) == MSG_USB_WWID_DP
))) {
4027 LastDeviceNode
= TempDevicePath
;
4028 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4030 if ((DevicePathType (LastDeviceNode
) == MEDIA_DEVICE_PATH
) &&
4031 (DevicePathSubType (LastDeviceNode
) == MEDIA_FILEPATH_DP
)) {
4036 // Check if it's a valid boot option for internal FV application
4038 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
) != NULL
) {
4040 // If the boot option point to internal FV application, make sure it is valid
4042 TempDevicePath
= DevPath
;
4043 Status
= BdsLibUpdateFvFileDevicePath (
4045 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
)
4047 if (Status
== EFI_ALREADY_STARTED
) {
4050 if (Status
== EFI_SUCCESS
) {
4051 FreePool (TempDevicePath
);
4058 // If the boot option point to a blockIO device:
4059 // if it is a removable blockIo device, it is valid.
4060 // if it is a fixed blockIo device, check its description confliction.
4062 TempDevicePath
= DevPath
;
4063 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4064 if (EFI_ERROR (Status
)) {
4066 // Device not present so see if we need to connect it
4068 Status
= BdsLibConnectDevicePath (DevPath
);
4069 if (!EFI_ERROR (Status
)) {
4071 // Try again to get the Block Io protocol after we did the connect
4073 TempDevicePath
= DevPath
;
4074 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &TempDevicePath
, &Handle
);
4078 if (!EFI_ERROR (Status
)) {
4079 Status
= gBS
->HandleProtocol (Handle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
4080 if (!EFI_ERROR (Status
)) {
4083 // Test if it is ready to boot now
4085 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4094 // 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,
4096 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &TempDevicePath
, &Handle
);
4097 if (!EFI_ERROR (Status
)) {
4100 // Test if it is ready to boot now
4102 if (BdsLibGetBootableHandle (DevPath
) != NULL
) {
4116 According to a file guild, check a Fv file device path is valid. If it is invalid,
4117 try to return the valid device path.
4118 FV address maybe changes for memory layout adjust from time to time, use this function
4119 could promise the Fv file device path is right.
4121 @param DevicePath on input, the Fv file device path need to check on
4122 output, the updated valid Fv file device path
4123 @param FileGuid the Fv file guild
4125 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
4127 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
4129 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
4131 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
4132 and return the updated device path in DevicePath
4137 BdsLibUpdateFvFileDevicePath (
4138 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
4139 IN EFI_GUID
*FileGuid
4142 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
4143 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
4145 EFI_GUID
*GuidPoint
;
4147 UINTN FvHandleCount
;
4148 EFI_HANDLE
*FvHandleBuffer
;
4149 EFI_FV_FILETYPE Type
;
4151 EFI_FV_FILE_ATTRIBUTES Attributes
;
4152 UINT32 AuthenticationStatus
;
4154 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
4155 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
4156 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
4157 EFI_HANDLE FoundFvHandle
;
4158 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
4160 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
4161 return EFI_INVALID_PARAMETER
;
4163 if (FileGuid
== NULL
) {
4164 return EFI_INVALID_PARAMETER
;
4168 // Check whether the device path point to the default the input Fv file
4170 TempDevicePath
= *DevicePath
;
4171 LastDeviceNode
= TempDevicePath
;
4172 while (!IsDevicePathEnd (TempDevicePath
)) {
4173 LastDeviceNode
= TempDevicePath
;
4174 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
4176 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
4177 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
4179 if (GuidPoint
== NULL
) {
4181 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
4183 return EFI_UNSUPPORTED
;
4185 if (!CompareGuid (GuidPoint
, FileGuid
)) {
4187 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
4189 return EFI_UNSUPPORTED
;
4193 // Check whether the input Fv file device path is valid
4195 TempDevicePath
= *DevicePath
;
4196 FoundFvHandle
= NULL
;
4197 Status
= gBS
->LocateDevicePath (
4198 &gEfiFirmwareVolume2ProtocolGuid
,
4202 if (!EFI_ERROR (Status
)) {
4203 Status
= gBS
->HandleProtocol (
4205 &gEfiFirmwareVolume2ProtocolGuid
,
4208 if (!EFI_ERROR (Status
)) {
4210 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
4212 Status
= Fv
->ReadFile (
4219 &AuthenticationStatus
4221 if (!EFI_ERROR (Status
)) {
4222 return EFI_ALREADY_STARTED
;
4228 // Look for the input wanted FV file in current FV
4229 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
4232 FoundFvHandle
= NULL
;
4233 Status
= gBS
->HandleProtocol (
4235 &gEfiLoadedImageProtocolGuid
,
4236 (VOID
**) &LoadedImage
4238 if (!EFI_ERROR (Status
)) {
4239 Status
= gBS
->HandleProtocol (
4240 LoadedImage
->DeviceHandle
,
4241 &gEfiFirmwareVolume2ProtocolGuid
,
4244 if (!EFI_ERROR (Status
)) {
4245 Status
= Fv
->ReadFile (
4252 &AuthenticationStatus
4254 if (!EFI_ERROR (Status
)) {
4256 FoundFvHandle
= LoadedImage
->DeviceHandle
;
4261 // Second, if fail to find, try to enumerate all FV
4264 FvHandleBuffer
= NULL
;
4265 gBS
->LocateHandleBuffer (
4267 &gEfiFirmwareVolume2ProtocolGuid
,
4272 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
4273 gBS
->HandleProtocol (
4274 FvHandleBuffer
[Index
],
4275 &gEfiFirmwareVolume2ProtocolGuid
,
4279 Status
= Fv
->ReadFile (
4286 &AuthenticationStatus
4288 if (EFI_ERROR (Status
)) {
4290 // Skip if input Fv file not in the FV
4295 FoundFvHandle
= FvHandleBuffer
[Index
];
4299 if (FvHandleBuffer
!= NULL
) {
4300 FreePool (FvHandleBuffer
);
4306 // Build the shell device path
4308 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
4309 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
4310 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
4311 *DevicePath
= NewDevicePath
;
4314 return EFI_NOT_FOUND
;