2 Load option library functions which relate with creating and processing load options.
4 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "InternalBm.h"
18 GLOBAL_REMOVE_IF_UNREFERENCED
19 CHAR16
*mBmLoadOptionName
[] = {
26 GLOBAL_REMOVE_IF_UNREFERENCED
27 CHAR16
*mBmLoadOptionOrderName
[] = {
28 EFI_DRIVER_ORDER_VARIABLE_NAME
,
29 EFI_SYS_PREP_ORDER_VARIABLE_NAME
,
30 EFI_BOOT_ORDER_VARIABLE_NAME
,
31 NULL
// PlatformRecovery#### doesn't have associated *Order variable
35 Call Visitor function for each variable in variable storage.
37 @param Visitor Visitor function.
38 @param Context The context passed to Visitor function.
42 BM_VARIABLE_VISITOR Visitor
,
52 NameSize
= sizeof (CHAR16
);
53 Name
= AllocateZeroPool (NameSize
);
54 ASSERT (Name
!= NULL
);
56 NewNameSize
= NameSize
;
57 Status
= gRT
->GetNextVariableName (&NewNameSize
, Name
, &Guid
);
58 if (Status
== EFI_BUFFER_TOO_SMALL
) {
59 Name
= ReallocatePool (NameSize
, NewNameSize
, Name
);
60 ASSERT (Name
!= NULL
);
61 Status
= gRT
->GetNextVariableName (&NewNameSize
, Name
, &Guid
);
62 NameSize
= NewNameSize
;
65 if (Status
== EFI_NOT_FOUND
) {
68 ASSERT_EFI_ERROR (Status
);
70 Visitor (Name
, &Guid
, Context
);
77 Get the Option Number that wasn't used.
79 @param LoadOptionType The load option type.
80 @param FreeOptionNumber Return the minimal free option number.
82 @retval EFI_SUCCESS The option number is found and will be returned.
83 @retval EFI_OUT_OF_RESOURCES There is no free option number that can be used.
84 @retval EFI_INVALID_PARAMETER FreeOptionNumber is NULL
88 BmGetFreeOptionNumber (
89 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType
,
90 OUT UINT16
*FreeOptionNumber
97 UINTN OptionOrderSize
;
100 ASSERT (FreeOptionNumber
!= NULL
);
101 ASSERT (LoadOptionType
== LoadOptionTypeDriver
||
102 LoadOptionType
== LoadOptionTypeBoot
||
103 LoadOptionType
== LoadOptionTypeSysPrep
);
105 GetEfiGlobalVariable2 (mBmLoadOptionOrderName
[LoadOptionType
], (VOID
**) &OptionOrder
, &OptionOrderSize
);
106 ASSERT ((OptionOrder
!= NULL
&& OptionOrderSize
!= 0) || (OptionOrder
== NULL
&& OptionOrderSize
== 0));
109 if (LoadOptionType
== LoadOptionTypeBoot
) {
110 GetEfiGlobalVariable2 (L
"BootNext", (VOID
**) &BootNext
, NULL
);
113 for (OptionNumber
= 0;
114 OptionNumber
< OptionOrderSize
/ sizeof (UINT16
)
115 + ((BootNext
!= NULL
) ? 1 : 0);
119 // Search in OptionOrder whether the OptionNumber exists
121 for (Index
= 0; Index
< OptionOrderSize
/ sizeof (UINT16
); Index
++) {
122 if (OptionNumber
== OptionOrder
[Index
]) {
128 // We didn't find it in the ****Order array and it doesn't equal to BootNext
129 // Otherwise, OptionNumber equals to OptionOrderSize / sizeof (UINT16) + 1
131 if ((Index
== OptionOrderSize
/ sizeof (UINT16
)) &&
132 ((BootNext
== NULL
) || (OptionNumber
!= *BootNext
))
137 if (OptionOrder
!= NULL
) {
138 FreePool (OptionOrder
);
141 if (BootNext
!= NULL
) {
146 // When BootOrder & BootNext conver all numbers in the range [0 ... 0xffff],
147 // OptionNumber equals to 0x10000 which is not valid.
149 ASSERT (OptionNumber
<= 0x10000);
150 if (OptionNumber
== 0x10000) {
151 return EFI_OUT_OF_RESOURCES
;
153 *FreeOptionNumber
= (UINT16
) OptionNumber
;
159 Create the Boot####, Driver####, SysPrep####, PlatformRecovery#### variable
160 from the load option.
162 @param LoadOption Pointer to the load option.
164 @retval EFI_SUCCESS The variable was created.
165 @retval Others Error status returned by RT->SetVariable.
169 EfiBootManagerLoadOptionToVariable (
170 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION
*Option
177 CHAR16 OptionName
[BM_OPTION_NAME_LEN
];
180 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLock
;
181 UINT32 VariableAttributes
;
183 if ((Option
->OptionNumber
== LoadOptionNumberUnassigned
) ||
184 (Option
->FilePath
== NULL
) ||
185 ((UINT32
) Option
->OptionType
>= LoadOptionTypeMax
)
187 return EFI_INVALID_PARAMETER
;
191 // Convert NULL description to empty description
194 Description
= Option
->Description
;
195 if (Description
== NULL
) {
196 Description
= &NullChar
;
201 UINT16 FilePathListLength;
202 CHAR16 Description[];
203 EFI_DEVICE_PATH_PROTOCOL FilePathList[];
204 UINT8 OptionalData[];
205 TODO: FilePathList[] IS:
206 A packed array of UEFI device paths. The first element of the
207 array is a device path that describes the device and location of the
208 Image for this load option. The FilePathList[0] is specific
209 to the device type. Other device paths may optionally exist in the
210 FilePathList, but their usage is OSV specific. Each element
211 in the array is variable length, and ends at the device path end
214 VariableSize
= sizeof (Option
->Attributes
)
216 + StrSize (Description
)
217 + GetDevicePathSize (Option
->FilePath
)
218 + Option
->OptionalDataSize
;
220 Variable
= AllocatePool (VariableSize
);
221 ASSERT (Variable
!= NULL
);
224 WriteUnaligned32 ((UINT32
*) Ptr
, Option
->Attributes
);
225 Ptr
+= sizeof (Option
->Attributes
);
227 WriteUnaligned16 ((UINT16
*) Ptr
, (UINT16
) GetDevicePathSize (Option
->FilePath
));
228 Ptr
+= sizeof (UINT16
);
230 CopyMem (Ptr
, Description
, StrSize (Description
));
231 Ptr
+= StrSize (Description
);
233 CopyMem (Ptr
, Option
->FilePath
, GetDevicePathSize (Option
->FilePath
));
234 Ptr
+= GetDevicePathSize (Option
->FilePath
);
236 CopyMem (Ptr
, Option
->OptionalData
, Option
->OptionalDataSize
);
238 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"%s%04x", mBmLoadOptionName
[Option
->OptionType
], Option
->OptionNumber
);
240 VariableAttributes
= EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
;
241 if (Option
->OptionType
== LoadOptionTypePlatformRecovery
) {
243 // Lock the PlatformRecovery####
245 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**) &VariableLock
);
246 if (!EFI_ERROR (Status
)) {
247 Status
= VariableLock
->RequestToLock (VariableLock
, OptionName
, &gEfiGlobalVariableGuid
);
248 ASSERT_EFI_ERROR (Status
);
250 VariableAttributes
= EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
253 return gRT
->SetVariable (
255 &gEfiGlobalVariableGuid
,
263 Update order variable .
265 @param OptionOrderName Order variable name which need to be updated.
266 @param OptionNumber Option number for the new option.
267 @param Position Position of the new load option to put in the ****Order variable.
269 @retval EFI_SUCCESS The boot#### or driver#### have been successfully registered.
270 @retval EFI_ALREADY_STARTED The option number of Option is being used already.
271 @retval EFI_STATUS Return the status of gRT->SetVariable ().
275 BmAddOptionNumberToOrderVariable (
276 IN CHAR16
*OptionOrderName
,
277 IN UINT16 OptionNumber
,
284 UINT16
*NewOptionOrder
;
285 UINTN OptionOrderSize
;
287 // Update the option order variable
289 GetEfiGlobalVariable2 (OptionOrderName
, (VOID
**) &OptionOrder
, &OptionOrderSize
);
290 ASSERT ((OptionOrder
!= NULL
&& OptionOrderSize
!= 0) || (OptionOrder
== NULL
&& OptionOrderSize
== 0));
292 Status
= EFI_SUCCESS
;
293 for (Index
= 0; Index
< OptionOrderSize
/ sizeof (UINT16
); Index
++) {
294 if (OptionOrder
[Index
] == OptionNumber
) {
295 Status
= EFI_ALREADY_STARTED
;
300 if (!EFI_ERROR (Status
)) {
301 Position
= MIN (Position
, OptionOrderSize
/ sizeof (UINT16
));
303 NewOptionOrder
= AllocatePool (OptionOrderSize
+ sizeof (UINT16
));
304 ASSERT (NewOptionOrder
!= NULL
);
305 if (OptionOrderSize
!= 0) {
306 CopyMem (NewOptionOrder
, OptionOrder
, Position
* sizeof (UINT16
));
307 CopyMem (&NewOptionOrder
[Position
+ 1], &OptionOrder
[Position
], OptionOrderSize
- Position
* sizeof (UINT16
));
309 NewOptionOrder
[Position
] = OptionNumber
;
311 Status
= gRT
->SetVariable (
313 &gEfiGlobalVariableGuid
,
314 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
315 OptionOrderSize
+ sizeof (UINT16
),
318 FreePool (NewOptionOrder
);
321 if (OptionOrder
!= NULL
) {
322 FreePool (OptionOrder
);
329 This function will register the new Boot####, Driver#### or SysPrep#### option.
330 After the *#### is updated, the *Order will also be updated.
332 @param Option Pointer to load option to add. If on input
333 Option->OptionNumber is LoadOptionNumberUnassigned,
334 then on output Option->OptionNumber is updated to
335 the number of the new Boot####,
336 Driver#### or SysPrep#### option.
337 @param Position Position of the new load option to put in the ****Order variable.
339 @retval EFI_SUCCESS The *#### have been successfully registered.
340 @retval EFI_INVALID_PARAMETER The option number exceeds 0xFFFF.
341 @retval EFI_ALREADY_STARTED The option number of Option is being used already.
342 Note: this API only adds new load option, no replacement support.
343 @retval EFI_OUT_OF_RESOURCES There is no free option number that can be used when the
344 option number specified in the Option is LoadOptionNumberUnassigned.
345 @return Status codes of gRT->SetVariable ().
350 EfiBootManagerAddLoadOptionVariable (
351 IN OUT EFI_BOOT_MANAGER_LOAD_OPTION
*Option
,
358 if (Option
== NULL
) {
359 return EFI_INVALID_PARAMETER
;
362 if (Option
->OptionType
!= LoadOptionTypeDriver
&&
363 Option
->OptionType
!= LoadOptionTypeSysPrep
&&
364 Option
->OptionType
!= LoadOptionTypeBoot
366 return EFI_INVALID_PARAMETER
;
370 // Get the free option number if the option number is unassigned
372 if (Option
->OptionNumber
== LoadOptionNumberUnassigned
) {
373 Status
= BmGetFreeOptionNumber (Option
->OptionType
, &OptionNumber
);
374 if (EFI_ERROR (Status
)) {
377 Option
->OptionNumber
= OptionNumber
;
380 if (Option
->OptionNumber
>= LoadOptionNumberMax
) {
381 return EFI_INVALID_PARAMETER
;
384 Status
= BmAddOptionNumberToOrderVariable (mBmLoadOptionOrderName
[Option
->OptionType
], (UINT16
) Option
->OptionNumber
, Position
);
385 if (!EFI_ERROR (Status
)) {
387 // Save the Boot#### or Driver#### variable
389 Status
= EfiBootManagerLoadOptionToVariable (Option
);
390 if (EFI_ERROR (Status
)) {
392 // Remove the #### from *Order variable when the Driver####/SysPrep####/Boot#### cannot be saved.
394 EfiBootManagerDeleteLoadOptionVariable (Option
->OptionNumber
, Option
->OptionType
);
402 Sort the load option. The DriverOrder or BootOrder will be re-created to
403 reflect the new order.
405 @param OptionType Load option type
406 @param CompareFunction The comparator
410 EfiBootManagerSortLoadOptionVariable (
411 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType
,
412 SORT_COMPARE CompareFunction
416 EFI_BOOT_MANAGER_LOAD_OPTION
*LoadOption
;
417 UINTN LoadOptionCount
;
421 LoadOption
= EfiBootManagerGetLoadOptions (&LoadOptionCount
, OptionType
);
424 // Insertion sort algorithm
429 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
),
434 // Create new ****Order variable
436 OptionOrder
= AllocatePool (LoadOptionCount
* sizeof (UINT16
));
437 ASSERT (OptionOrder
!= NULL
);
438 for (Index
= 0; Index
< LoadOptionCount
; Index
++) {
439 OptionOrder
[Index
] = (UINT16
) LoadOption
[Index
].OptionNumber
;
442 Status
= gRT
->SetVariable (
443 mBmLoadOptionOrderName
[OptionType
],
444 &gEfiGlobalVariableGuid
,
445 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
446 LoadOptionCount
* sizeof (UINT16
),
450 // Changing the *Order content without increasing its size with current variable implementation shouldn't fail.
452 ASSERT_EFI_ERROR (Status
);
454 FreePool (OptionOrder
);
455 EfiBootManagerFreeLoadOptions (LoadOption
, LoadOptionCount
);
459 Initialize a load option.
461 @param Option Pointer to the load option to be initialized.
462 @param OptionNumber Option number of the load option.
463 @param OptionType Type of the load option.
464 @param Attributes Attributes of the load option.
465 @param Description Description of the load option.
466 @param FilePath Device path of the load option.
467 @param OptionalData Optional data of the load option.
468 @param OptionalDataSize Size of the optional data of the load option.
470 @retval EFI_SUCCESS The load option was initialized successfully.
471 @retval EFI_INVALID_PARAMETER Option, Description or FilePath is NULL.
475 EfiBootManagerInitializeLoadOption (
476 IN OUT EFI_BOOT_MANAGER_LOAD_OPTION
*Option
,
477 IN UINTN OptionNumber
,
478 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType
,
479 IN UINT32 Attributes
,
480 IN CHAR16
*Description
,
481 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
482 IN UINT8
*OptionalData
, OPTIONAL
483 IN UINT32 OptionalDataSize
486 if ((Option
== NULL
) || (Description
== NULL
) || (FilePath
== NULL
)) {
487 return EFI_INVALID_PARAMETER
;
490 if (((OptionalData
!= NULL
) && (OptionalDataSize
== 0)) ||
491 ((OptionalData
== NULL
) && (OptionalDataSize
!= 0))) {
492 return EFI_INVALID_PARAMETER
;
495 if ((UINT32
) OptionType
>= LoadOptionTypeMax
) {
496 return EFI_INVALID_PARAMETER
;
499 ZeroMem (Option
, sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
));
500 Option
->OptionNumber
= OptionNumber
;
501 Option
->OptionType
= OptionType
;
502 Option
->Attributes
= Attributes
;
503 Option
->Description
= AllocateCopyPool (StrSize (Description
), Description
);
504 Option
->FilePath
= DuplicateDevicePath (FilePath
);
505 if (OptionalData
!= NULL
) {
506 Option
->OptionalData
= AllocateCopyPool (OptionalDataSize
, OptionalData
);
507 Option
->OptionalDataSize
= OptionalDataSize
;
515 Return the index of the load option in the load option array.
517 The function consider two load options are equal when the
518 OptionType, Attributes, Description, FilePath and OptionalData are equal.
520 @param Key Pointer to the load option to be found.
521 @param Array Pointer to the array of load options to be found.
522 @param Count Number of entries in the Array.
524 @retval -1 Key wasn't found in the Array.
525 @retval 0 ~ Count-1 The index of the Key in the Array.
529 EfiBootManagerFindLoadOption (
530 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION
*Key
,
531 IN CONST EFI_BOOT_MANAGER_LOAD_OPTION
*Array
,
537 for (Index
= 0; Index
< Count
; Index
++) {
538 if ((Key
->OptionType
== Array
[Index
].OptionType
) &&
539 (Key
->Attributes
== Array
[Index
].Attributes
) &&
540 (StrCmp (Key
->Description
, Array
[Index
].Description
) == 0) &&
541 (CompareMem (Key
->FilePath
, Array
[Index
].FilePath
, GetDevicePathSize (Key
->FilePath
)) == 0) &&
542 (Key
->OptionalDataSize
== Array
[Index
].OptionalDataSize
) &&
543 (CompareMem (Key
->OptionalData
, Array
[Index
].OptionalData
, Key
->OptionalDataSize
) == 0)) {
552 Delete the load option.
554 @param OptionNumber Indicate the option number of load option
555 @param OptionType Indicate the type of load option
557 @retval EFI_INVALID_PARAMETER OptionType or OptionNumber is invalid.
558 @retval EFI_NOT_FOUND The load option cannot be found
559 @retval EFI_SUCCESS The load option was deleted
560 @retval others Status of RT->SetVariable()
564 EfiBootManagerDeleteLoadOptionVariable (
565 IN UINTN OptionNumber
,
566 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType
570 UINTN OptionOrderSize
;
572 CHAR16 OptionName
[BM_OPTION_NAME_LEN
];
574 if (((UINT32
) OptionType
>= LoadOptionTypeMax
) || (OptionNumber
>= LoadOptionNumberMax
)) {
575 return EFI_INVALID_PARAMETER
;
578 if (OptionType
== LoadOptionTypeDriver
|| OptionType
== LoadOptionTypeSysPrep
|| OptionType
== LoadOptionTypeBoot
) {
580 // If the associated *Order exists, firstly remove the reference in *Order for
581 // Driver####, SysPrep#### and Boot####.
583 GetEfiGlobalVariable2 (mBmLoadOptionOrderName
[OptionType
], (VOID
**) &OptionOrder
, &OptionOrderSize
);
584 ASSERT ((OptionOrder
!= NULL
&& OptionOrderSize
!= 0) || (OptionOrder
== NULL
&& OptionOrderSize
== 0));
586 for (Index
= 0; Index
< OptionOrderSize
/ sizeof (UINT16
); Index
++) {
587 if (OptionOrder
[Index
] == OptionNumber
) {
588 OptionOrderSize
-= sizeof (UINT16
);
589 CopyMem (&OptionOrder
[Index
], &OptionOrder
[Index
+ 1], OptionOrderSize
- Index
* sizeof (UINT16
));
591 mBmLoadOptionOrderName
[OptionType
],
592 &gEfiGlobalVariableGuid
,
593 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
600 if (OptionOrder
!= NULL
) {
601 FreePool (OptionOrder
);
606 // Remove the Driver####, SysPrep####, Boot#### or PlatformRecovery#### itself.
608 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"%s%04x", mBmLoadOptionName
[OptionType
], OptionNumber
);
609 return gRT
->SetVariable (
611 &gEfiGlobalVariableGuid
,
619 Returns the size of a device path in bytes.
621 This function returns the size, in bytes, of the device path data structure
622 specified by DevicePath including the end of device path node. If DevicePath
623 is NULL, then 0 is returned. If the length of the device path is bigger than
624 MaxSize, also return 0 to indicate this is an invalidate device path.
626 @param DevicePath A pointer to a device path data structure.
627 @param MaxSize Max valid device path size. If big than this size,
630 @retval 0 An invalid device path.
631 @retval Others The size of a device path in bytes.
635 BmGetDevicePathSizeEx (
636 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
643 if (DevicePath
== NULL
) {
648 // Search for the end of the device path structure
651 while (!IsDevicePathEnd (DevicePath
)) {
652 NodeSize
= DevicePathNodeLength (DevicePath
);
657 if (Size
> MaxSize
) {
660 DevicePath
= NextDevicePathNode (DevicePath
);
662 Size
+= DevicePathNodeLength (DevicePath
);
663 if (Size
> MaxSize
) {
671 Returns the length of a Null-terminated Unicode string. If the length is
672 bigger than MaxStringLen, return length 0 to indicate that this is an
675 This function returns the number of Unicode characters in the Null-terminated
676 Unicode string specified by String.
678 If String is NULL, then ASSERT().
679 If String is not aligned on a 16-bit boundary, then ASSERT().
681 @param String A pointer to a Null-terminated Unicode string.
682 @param MaxStringLen Max string len in this string.
684 @retval 0 An invalid string.
685 @retval Others The length of String.
690 IN CONST CHAR16
*String
,
691 IN UINTN MaxStringLen
696 ASSERT (String
!= NULL
&& MaxStringLen
!= 0);
697 ASSERT (((UINTN
) String
& BIT0
) == 0);
699 for (Length
= 0; *String
!= L
'\0' && MaxStringLen
!= Length
; String
++, Length
+=2);
701 if (*String
!= L
'\0' && MaxStringLen
== Length
) {
709 Validate the Boot####, Driver####, SysPrep#### and PlatformRecovery####
710 variable (VendorGuid/Name)
712 @param Variable The variable data.
713 @param VariableSize The variable size.
715 @retval TRUE The variable data is correct.
716 @retval FALSE The variable data is corrupted.
726 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
727 UINTN DescriptionSize
;
729 if (VariableSize
<= sizeof (UINT16
) + sizeof (UINT32
)) {
734 // Skip the option attribute
736 Variable
+= sizeof (UINT32
);
739 // Get the option's device path size
741 FilePathSize
= ReadUnaligned16 ((UINT16
*) Variable
);
742 Variable
+= sizeof (UINT16
);
745 // Get the option's description string size
747 DescriptionSize
= BmStrSizeEx ((CHAR16
*) Variable
, VariableSize
- sizeof (UINT16
) - sizeof (UINT32
));
748 Variable
+= DescriptionSize
;
751 // Get the option's device path
753 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) Variable
;
756 // Validation boot option variable.
758 if ((FilePathSize
== 0) || (DescriptionSize
== 0)) {
762 if (sizeof (UINT32
) + sizeof (UINT16
) + DescriptionSize
+ FilePathSize
> VariableSize
) {
766 return (BOOLEAN
) (BmGetDevicePathSizeEx (DevicePath
, FilePathSize
) != 0);
770 Check whether the VariableName is a valid load option variable name
771 and return the load option type and option number.
773 @param VariableName The name of the load option variable.
774 @param OptionType Return the load option type.
775 @param OptionNumber Return the load option number.
777 @retval TRUE The variable name is valid; The load option type and
778 load option number is returned.
779 @retval FALSE The variable name is NOT valid.
783 EfiBootManagerIsValidLoadOptionVariableName (
784 IN CHAR16
*VariableName
,
785 OUT EFI_BOOT_MANAGER_LOAD_OPTION_TYPE
*OptionType OPTIONAL
,
786 OUT UINT16
*OptionNumber OPTIONAL
789 UINTN VariableNameLen
;
792 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LocalOptionType
;
793 UINT16 LocalOptionNumber
;
795 if (VariableName
== NULL
) {
799 VariableNameLen
= StrLen (VariableName
);
802 // Return FALSE when the variable name length is too small.
804 if (VariableNameLen
<= 4) {
809 // Return FALSE when the variable name doesn't start with Driver/SysPrep/Boot/PlatformRecovery.
811 for (LocalOptionType
= 0; LocalOptionType
< ARRAY_SIZE (mBmLoadOptionName
); LocalOptionType
++) {
812 if ((VariableNameLen
- 4 == StrLen (mBmLoadOptionName
[LocalOptionType
])) &&
813 (StrnCmp (VariableName
, mBmLoadOptionName
[LocalOptionType
], VariableNameLen
- 4) == 0)
818 if (LocalOptionType
== ARRAY_SIZE (mBmLoadOptionName
)) {
823 // Return FALSE when the last four characters are not hex digits.
825 LocalOptionNumber
= 0;
826 for (Index
= VariableNameLen
- 4; Index
< VariableNameLen
; Index
++) {
827 Uint
= BmCharToUint (VariableName
[Index
]);
831 LocalOptionNumber
= (UINT16
) Uint
+ LocalOptionNumber
* 0x10;
834 if (Index
!= VariableNameLen
) {
838 if (OptionType
!= NULL
) {
839 *OptionType
= LocalOptionType
;
842 if (OptionNumber
!= NULL
) {
843 *OptionNumber
= LocalOptionNumber
;
850 Build the Boot#### or Driver#### option from the VariableName.
852 @param VariableName Variable name of the load option
853 @param VendorGuid Variable GUID of the load option
854 @param Option Return the load option.
856 @retval EFI_SUCCESS Get the option just been created
857 @retval EFI_NOT_FOUND Failed to get the new option
862 EfiBootManagerVariableToLoadOptionEx (
863 IN CHAR16
*VariableName
,
864 IN EFI_GUID
*VendorGuid
,
865 IN OUT EFI_BOOT_MANAGER_LOAD_OPTION
*Option
874 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
876 UINT32 OptionalDataSize
;
878 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType
;
881 if ((VariableName
== NULL
) || (Option
== NULL
)) {
882 return EFI_INVALID_PARAMETER
;
885 if (!EfiBootManagerIsValidLoadOptionVariableName (VariableName
, &OptionType
, &OptionNumber
)) {
886 return EFI_INVALID_PARAMETER
;
892 GetVariable2 (VariableName
, VendorGuid
, (VOID
**) &Variable
, &VariableSize
);
893 if (Variable
== NULL
) {
894 return EFI_NOT_FOUND
;
898 // Validate *#### variable data.
900 if (!BmValidateOption(Variable
, VariableSize
)) {
902 return EFI_INVALID_PARAMETER
;
906 // Get the option attribute
908 VariablePtr
= Variable
;
909 Attribute
= ReadUnaligned32 ((UINT32
*) VariablePtr
);
910 VariablePtr
+= sizeof (UINT32
);
913 // Get the option's device path size
915 FilePathSize
= ReadUnaligned16 ((UINT16
*) VariablePtr
);
916 VariablePtr
+= sizeof (UINT16
);
919 // Get the option's description string
921 Description
= (CHAR16
*) VariablePtr
;
924 // Get the option's description string size
926 VariablePtr
+= StrSize ((CHAR16
*) VariablePtr
);
929 // Get the option's device path
931 FilePath
= (EFI_DEVICE_PATH_PROTOCOL
*) VariablePtr
;
932 VariablePtr
+= FilePathSize
;
934 OptionalDataSize
= (UINT32
) (VariableSize
- ((UINTN
) VariablePtr
- (UINTN
) Variable
));
935 if (OptionalDataSize
== 0) {
938 OptionalData
= VariablePtr
;
941 Status
= EfiBootManagerInitializeLoadOption (
951 ASSERT_EFI_ERROR (Status
);
953 CopyGuid (&Option
->VendorGuid
, VendorGuid
);
960 Build the Boot#### or Driver#### option from the VariableName.
962 @param VariableName EFI Variable name indicate if it is Boot#### or Driver####
963 @param Option Return the Boot#### or Driver#### option.
965 @retval EFI_SUCCESS Get the option just been created
966 @retval EFI_NOT_FOUND Failed to get the new option
970 EfiBootManagerVariableToLoadOption (
971 IN CHAR16
*VariableName
,
972 IN OUT EFI_BOOT_MANAGER_LOAD_OPTION
*Option
975 return EfiBootManagerVariableToLoadOptionEx (VariableName
, &gEfiGlobalVariableGuid
, Option
);
979 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType
;
981 EFI_BOOT_MANAGER_LOAD_OPTION
*Options
;
983 } BM_COLLECT_LOAD_OPTIONS_PARAM
;
986 Visitor function to collect the Platform Recovery load options or OS Recovery
987 load options from NV storage.
989 @param Name Variable name.
990 @param Guid Variable GUID.
991 @param Context The same context passed to BmForEachVariable.
994 BmCollectLoadOptions (
1001 EFI_BOOT_MANAGER_LOAD_OPTION_TYPE OptionType
;
1002 UINT16 OptionNumber
;
1003 EFI_BOOT_MANAGER_LOAD_OPTION Option
;
1005 BM_COLLECT_LOAD_OPTIONS_PARAM
*Param
;
1007 Param
= (BM_COLLECT_LOAD_OPTIONS_PARAM
*) Context
;
1009 if (CompareGuid (Guid
, Param
->Guid
) && (
1010 Param
->OptionType
== LoadOptionTypePlatformRecovery
&&
1011 EfiBootManagerIsValidLoadOptionVariableName (Name
, &OptionType
, &OptionNumber
) &&
1012 OptionType
== LoadOptionTypePlatformRecovery
1014 Status
= EfiBootManagerVariableToLoadOptionEx (Name
, Guid
, &Option
);
1015 if (!EFI_ERROR (Status
)) {
1016 for (Index
= 0; Index
< Param
->OptionCount
; Index
++) {
1017 if (Param
->Options
[Index
].OptionNumber
> Option
.OptionNumber
) {
1021 Param
->Options
= ReallocatePool (
1022 Param
->OptionCount
* sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
),
1023 (Param
->OptionCount
+ 1) * sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
),
1026 ASSERT (Param
->Options
!= NULL
);
1027 CopyMem (&Param
->Options
[Index
+ 1], &Param
->Options
[Index
], (Param
->OptionCount
- Index
) * sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
));
1028 CopyMem (&Param
->Options
[Index
], &Option
, sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
));
1029 Param
->OptionCount
++;
1035 Returns an array of load options based on the EFI variable
1036 L"BootOrder"/L"DriverOrder" and the L"Boot####"/L"Driver####" variables impled by it.
1037 #### is the hex value of the UINT16 in each BootOrder/DriverOrder entry.
1039 @param LoadOptionCount Returns number of entries in the array.
1040 @param LoadOptionType The type of the load option.
1042 @retval NULL No load options exist.
1043 @retval !NULL Array of load option entries.
1046 EFI_BOOT_MANAGER_LOAD_OPTION
*
1048 EfiBootManagerGetLoadOptions (
1049 OUT UINTN
*OptionCount
,
1050 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE LoadOptionType
1054 UINT16
*OptionOrder
;
1055 UINTN OptionOrderSize
;
1058 EFI_BOOT_MANAGER_LOAD_OPTION
*Options
;
1059 CHAR16 OptionName
[BM_OPTION_NAME_LEN
];
1060 UINT16 OptionNumber
;
1061 BM_COLLECT_LOAD_OPTIONS_PARAM Param
;
1066 if (LoadOptionType
== LoadOptionTypeDriver
|| LoadOptionType
== LoadOptionTypeSysPrep
|| LoadOptionType
== LoadOptionTypeBoot
) {
1068 // Read the BootOrder, or DriverOrder variable.
1070 GetEfiGlobalVariable2 (mBmLoadOptionOrderName
[LoadOptionType
], (VOID
**) &OptionOrder
, &OptionOrderSize
);
1071 if (OptionOrder
== NULL
) {
1075 *OptionCount
= OptionOrderSize
/ sizeof (UINT16
);
1077 Options
= AllocatePool (*OptionCount
* sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
));
1078 ASSERT (Options
!= NULL
);
1081 for (Index
= 0; Index
< *OptionCount
; Index
++) {
1082 OptionNumber
= OptionOrder
[Index
];
1083 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"%s%04x", mBmLoadOptionName
[LoadOptionType
], OptionNumber
);
1085 Status
= EfiBootManagerVariableToLoadOption (OptionName
, &Options
[OptionIndex
]);
1086 if (EFI_ERROR (Status
)) {
1087 DEBUG ((EFI_D_INFO
, "[Bds] %s doesn't exist - Update ****Order variable to remove the reference!!", OptionName
));
1088 EfiBootManagerDeleteLoadOptionVariable (OptionNumber
, LoadOptionType
);
1090 ASSERT (Options
[OptionIndex
].OptionNumber
== OptionNumber
);
1095 if (OptionOrder
!= NULL
) {
1096 FreePool (OptionOrder
);
1099 if (OptionIndex
< *OptionCount
) {
1100 Options
= ReallocatePool (*OptionCount
* sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
), OptionIndex
* sizeof (EFI_BOOT_MANAGER_LOAD_OPTION
), Options
);
1101 ASSERT (Options
!= NULL
);
1102 *OptionCount
= OptionIndex
;
1105 } else if (LoadOptionType
== LoadOptionTypePlatformRecovery
) {
1106 Param
.OptionType
= LoadOptionTypePlatformRecovery
;
1107 Param
.Options
= NULL
;
1108 Param
.OptionCount
= 0;
1109 Param
.Guid
= &gEfiGlobalVariableGuid
;
1111 BmForEachVariable (BmCollectLoadOptions
, (VOID
*) &Param
);
1113 *OptionCount
= Param
.OptionCount
;
1114 Options
= Param
.Options
;
1121 Free an EFI_BOOT_MANGER_LOAD_OPTION entry that was allocate by the library.
1123 @param LoadOption Pointer to boot option to Free.
1125 @return EFI_SUCCESS BootOption was freed
1126 @return EFI_NOT_FOUND BootOption == NULL
1131 EfiBootManagerFreeLoadOption (
1132 IN EFI_BOOT_MANAGER_LOAD_OPTION
*LoadOption
1135 if (LoadOption
== NULL
) {
1136 return EFI_NOT_FOUND
;
1139 if (LoadOption
->Description
!= NULL
) {
1140 FreePool (LoadOption
->Description
);
1142 if (LoadOption
->FilePath
!= NULL
) {
1143 FreePool (LoadOption
->FilePath
);
1145 if (LoadOption
->OptionalData
!= NULL
) {
1146 FreePool (LoadOption
->OptionalData
);
1153 Free an EFI_BOOT_MANGER_LOAD_OPTION array that was allocated by
1154 EfiBootManagerGetLoadOptions().
1156 @param Option Pointer to boot option array to free.
1157 @param OptionCount Number of array entries in BootOption
1159 @return EFI_SUCCESS BootOption was freed
1160 @return EFI_NOT_FOUND BootOption == NULL
1165 EfiBootManagerFreeLoadOptions (
1166 IN EFI_BOOT_MANAGER_LOAD_OPTION
*Option
,
1167 IN UINTN OptionCount
1172 if (Option
== NULL
) {
1173 return EFI_NOT_FOUND
;
1176 for (Index
= 0;Index
< OptionCount
; Index
++) {
1177 EfiBootManagerFreeLoadOption (&Option
[Index
]);
1186 Return whether the PE header of the load option is valid or not.
1188 @param[in] Type The load option type.
1189 It's used to check whether the load option is valid.
1190 When it's LoadOptionTypeMax, the routine only guarantees
1191 the load option is a valid PE image but doesn't guarantee
1192 the PE's subsystem type is valid.
1193 @param[in] FileBuffer The PE file buffer of the load option.
1194 @param[in] FileSize The size of the load option file.
1196 @retval TRUE The PE header of the load option is valid.
1197 @retval FALSE The PE header of the load option is not valid.
1200 BmIsLoadOptionPeHeaderValid (
1201 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE Type
,
1202 IN VOID
*FileBuffer
,
1206 EFI_IMAGE_DOS_HEADER
*DosHeader
;
1207 EFI_IMAGE_OPTIONAL_HEADER_UNION
*PeHeader
;
1208 EFI_IMAGE_OPTIONAL_HEADER32
*OptionalHeader
;
1211 if (FileBuffer
== NULL
|| FileSize
== 0) {
1218 DosHeader
= (EFI_IMAGE_DOS_HEADER
*) FileBuffer
;
1219 if (FileSize
>= sizeof (EFI_IMAGE_DOS_HEADER
) &&
1220 FileSize
> DosHeader
->e_lfanew
&& DosHeader
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
1223 // Read and check PE signature
1225 PeHeader
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINT8
*) FileBuffer
+ DosHeader
->e_lfanew
);
1226 if (FileSize
>= DosHeader
->e_lfanew
+ sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
) &&
1227 PeHeader
->Pe32
.Signature
== EFI_IMAGE_NT_SIGNATURE
1230 // Check PE32 or PE32+ magic, and machine type
1232 OptionalHeader
= (EFI_IMAGE_OPTIONAL_HEADER32
*) &PeHeader
->Pe32
.OptionalHeader
;
1233 if ((OptionalHeader
->Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
||
1234 OptionalHeader
->Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) &&
1235 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeHeader
->Pe32
.FileHeader
.Machine
)
1238 // Check the Subsystem:
1239 // Driver#### must be of type BootServiceDriver or RuntimeDriver
1240 // SysPrep####, Boot####, OsRecovery####, PlatformRecovery#### must be of type Application
1242 Subsystem
= OptionalHeader
->Subsystem
;
1243 if ((Type
== LoadOptionTypeMax
) ||
1244 (Type
== LoadOptionTypeDriver
&& Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
) ||
1245 (Type
== LoadOptionTypeDriver
&& Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) ||
1246 (Type
== LoadOptionTypeSysPrep
&& Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) ||
1247 (Type
== LoadOptionTypeBoot
&& Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
) ||
1248 (Type
== LoadOptionTypePlatformRecovery
&& Subsystem
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
)
1260 Return the next matched load option buffer.
1261 The routine keeps calling BmGetNextLoadOptionDevicePath() until a valid
1262 load option is read.
1264 @param Type The load option type.
1265 It's used to check whether the load option is valid.
1266 When it's LoadOptionTypeMax, the routine only guarantees
1267 the load option is a valid PE image but doesn't guarantee
1268 the PE's subsystem type is valid.
1269 @param FilePath The device path pointing to a load option.
1270 It could be a short-form device path.
1271 @param FullPath Return the next full device path of the load option after
1272 short-form device path expanding.
1273 Caller is responsible to free it.
1274 NULL to return the first matched full device path.
1275 @param FileSize Return the load option size.
1277 @return The load option buffer. Caller is responsible to free the memory.
1280 BmGetNextLoadOptionBuffer (
1281 IN EFI_BOOT_MANAGER_LOAD_OPTION_TYPE Type
,
1282 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1283 OUT EFI_DEVICE_PATH_PROTOCOL
**FullPath
,
1288 EFI_DEVICE_PATH_PROTOCOL
*PreFullPath
;
1289 EFI_DEVICE_PATH_PROTOCOL
*CurFullPath
;
1290 UINTN LocalFileSize
;
1291 UINT32 AuthenticationStatus
;
1292 EFI_DEVICE_PATH_PROTOCOL
*RamDiskDevicePath
;
1296 CurFullPath
= *FullPath
;
1298 PreFullPath
= CurFullPath
;
1299 CurFullPath
= BmGetNextLoadOptionDevicePath (FilePath
, CurFullPath
);
1301 // Only free the full path created *inside* this routine
1303 if ((PreFullPath
!= NULL
) && (PreFullPath
!= *FullPath
)) {
1304 FreePool (PreFullPath
);
1306 if (CurFullPath
== NULL
) {
1309 FileBuffer
= GetFileBufferByFilePath (TRUE
, CurFullPath
, &LocalFileSize
, &AuthenticationStatus
);
1310 if ((FileBuffer
!= NULL
) && !BmIsLoadOptionPeHeaderValid (Type
, FileBuffer
, LocalFileSize
)) {
1312 // Free the RAM disk file system if the load option is invalid.
1314 RamDiskDevicePath
= BmGetRamDiskDevicePath (FilePath
);
1315 if (RamDiskDevicePath
!= NULL
) {
1316 BmDestroyRamDisk (RamDiskDevicePath
);
1317 FreePool (RamDiskDevicePath
);
1321 // Free the invalid load option buffer.
1323 FreePool (FileBuffer
);
1326 } while (FileBuffer
== NULL
);
1328 if (FileBuffer
== NULL
) {
1333 DEBUG ((DEBUG_INFO
, "[Bds] Expand "));
1334 BmPrintDp (FilePath
);
1335 DEBUG ((DEBUG_INFO
, " -> "));
1336 BmPrintDp (CurFullPath
);
1337 DEBUG ((DEBUG_INFO
, "\n"));
1339 *FullPath
= CurFullPath
;
1340 *FileSize
= LocalFileSize
;
1345 Process (load and execute) the load option.
1347 @param LoadOption Pointer to the load option.
1349 @retval EFI_INVALID_PARAMETER The load option type is invalid,
1350 or the load option file path doesn't point to a valid file.
1351 @retval EFI_UNSUPPORTED The load option type is of LoadOptionTypeBoot.
1352 @retval EFI_SUCCESS The load option is inactive, or successfully loaded and executed.
1356 EfiBootManagerProcessLoadOption (
1357 IN EFI_BOOT_MANAGER_LOAD_OPTION
*LoadOption
1361 EFI_DEVICE_PATH_PROTOCOL
*PreFullPath
;
1362 EFI_DEVICE_PATH_PROTOCOL
*CurFullPath
;
1363 EFI_HANDLE ImageHandle
;
1364 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
1368 if ((UINT32
) LoadOption
->OptionType
>= LoadOptionTypeMax
) {
1369 return EFI_INVALID_PARAMETER
;
1372 if (LoadOption
->OptionType
== LoadOptionTypeBoot
) {
1373 return EFI_UNSUPPORTED
;
1377 // If a load option is not marked as LOAD_OPTION_ACTIVE,
1378 // the boot manager will not automatically load the option.
1380 if ((LoadOption
->Attributes
& LOAD_OPTION_ACTIVE
) == 0) {
1385 // Load and start the load option.
1388 DEBUG_INFO
| DEBUG_LOAD
, "Process %s%04x (%s) ...\n",
1389 mBmLoadOptionName
[LoadOption
->OptionType
], LoadOption
->OptionNumber
,
1390 LoadOption
->Description
1394 EfiBootManagerConnectDevicePath (LoadOption
->FilePath
, NULL
);
1397 // while() loop is to keep starting next matched load option if the PlatformRecovery#### returns failure status.
1400 Status
= EFI_INVALID_PARAMETER
;
1401 PreFullPath
= CurFullPath
;
1402 FileBuffer
= BmGetNextLoadOptionBuffer (LoadOption
->OptionType
, LoadOption
->FilePath
, &CurFullPath
, &FileSize
);
1403 if (PreFullPath
!= NULL
) {
1404 FreePool (PreFullPath
);
1406 if (FileBuffer
== NULL
) {
1409 Status
= gBS
->LoadImage (
1417 FreePool (FileBuffer
);
1419 if (!EFI_ERROR (Status
)) {
1420 Status
= gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&ImageInfo
);
1421 ASSERT_EFI_ERROR (Status
);
1423 ImageInfo
->LoadOptionsSize
= LoadOption
->OptionalDataSize
;
1424 ImageInfo
->LoadOptions
= LoadOption
->OptionalData
;
1426 // Before calling the image, enable the Watchdog Timer for the 5-minute period
1428 gBS
->SetWatchdogTimer (5 * 60, 0, 0, NULL
);
1430 LoadOption
->Status
= gBS
->StartImage (ImageHandle
, &LoadOption
->ExitDataSize
, &LoadOption
->ExitData
);
1432 DEBUG_INFO
| DEBUG_LOAD
, "%s%04x Return Status = %r\n",
1433 mBmLoadOptionName
[LoadOption
->OptionType
], LoadOption
->OptionNumber
, LoadOption
->Status
1437 // Clear the Watchdog Timer after the image returns
1439 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
1441 if ((LoadOption
->OptionType
!= LoadOptionTypePlatformRecovery
) || (LoadOption
->Status
== EFI_SUCCESS
)) {
1447 if (CurFullPath
!= NULL
) {
1448 FreePool (CurFullPath
);