2 Misc BDS library function
4 Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalBdsLib.h"
12 #define MAX_STRING_LEN 200
14 BOOLEAN mFeaturerSwitch
= TRUE
;
15 BOOLEAN mResetRequired
= FALSE
;
17 extern UINT16 gPlatformBootTimeOutDefault
;
20 The function will go through the driver option link list, load and start
21 every driver the driver option device path point to.
23 @param BdsDriverLists The header of the current driver option link list
29 IN LIST_ENTRY
*BdsDriverLists
34 BDS_COMMON_OPTION
*Option
;
35 EFI_HANDLE ImageHandle
;
36 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
44 // Process the driver option
46 for (Link
= BdsDriverLists
->ForwardLink
; Link
!= BdsDriverLists
; Link
= Link
->ForwardLink
) {
47 Option
= CR (Link
, BDS_COMMON_OPTION
, Link
, BDS_LOAD_OPTION_SIGNATURE
);
50 // If a load option is not marked as LOAD_OPTION_ACTIVE,
51 // the boot manager will not automatically load the option.
53 if (!IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_ACTIVE
)) {
58 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
59 // then all of the EFI drivers in the system will be disconnected and
60 // reconnected after the last driver load option is processed.
62 if (IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_FORCE_RECONNECT
)) {
67 // Make sure the driver path is connected.
69 BdsLibConnectDevicePath (Option
->DevicePath
);
72 // Load and start the image that Driver#### describes
74 Status
= gBS
->LoadImage (
83 if (!EFI_ERROR (Status
)) {
84 gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
87 // Verify whether this image is a driver, if not,
88 // exit it and continue to parse next load option
90 if (ImageInfo
->ImageCodeType
!= EfiBootServicesCode
&& ImageInfo
->ImageCodeType
!= EfiRuntimeServicesCode
) {
91 gBS
->Exit (ImageHandle
, EFI_INVALID_PARAMETER
, 0, NULL
);
95 if (Option
->LoadOptionsSize
!= 0) {
96 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
97 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
100 // Before calling the image, enable the Watchdog Timer for
101 // the 5 Minute period
103 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
105 Status
= gBS
->StartImage (ImageHandle
, &ExitDataSize
, &ExitData
);
106 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Driver Return Status = %r\n", Status
));
109 // Clear the Watchdog Timer after the image returns
111 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
116 // Process the LOAD_OPTION_FORCE_RECONNECT driver option
119 BdsLibDisconnectAllEfi ();
126 Get the Option Number that does not used.
127 Try to locate the specific option variable one by one utile find a free number.
129 @param VariableName Indicate if the boot#### or driver#### option
131 @return The Minimal Free Option Number
135 BdsLibGetFreeOptionNumber (
136 IN CHAR16
*VariableName
141 UINT16
*OptionBuffer
;
145 // Try to find the minimum free number from 0, 1, 2, 3....
149 if (*VariableName
== 'B') {
150 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Boot%04x", Index
);
152 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Driver%04x", Index
);
155 // try if the option number is used
157 OptionBuffer
= BdsLibGetVariableAndSize (
159 &gEfiGlobalVariableGuid
,
162 if (OptionBuffer
== NULL
) {
165 FreePool(OptionBuffer
);
169 return ((UINT16
) Index
);
174 This function will register the new boot#### or driver#### option base on
175 the VariableName. The new registered boot#### or driver#### will be linked
176 to BdsOptionList and also update to the VariableName. After the boot#### or
177 driver#### updated, the BootOrder or DriverOrder will also be updated.
179 @param BdsOptionList The header of the boot#### or driver#### link list
180 @param DevicePath The device path which the boot#### or driver####
182 @param String The description of the boot#### or driver####
183 @param VariableName Indicate if the boot#### or driver#### option
185 @retval EFI_SUCCESS The boot#### or driver#### have been success
187 @retval EFI_STATUS Return the status of gRT->SetVariable ().
192 BdsLibRegisterNewOption (
193 IN LIST_ENTRY
*BdsOptionList
,
194 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
196 IN CHAR16
*VariableName
201 UINT16 RegisterOptionNumber
;
202 UINT16
*TempOptionPtr
;
203 UINTN TempOptionSize
;
204 UINT16
*OptionOrderPtr
;
208 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
210 CHAR16 OptionName
[10];
211 BOOLEAN UpdateDescription
;
212 UINT16 BootOrderEntry
;
215 if (DevicePath
== NULL
) {
216 return EFI_INVALID_PARAMETER
;
222 OptionDevicePath
= NULL
;
224 OptionOrderPtr
= NULL
;
225 UpdateDescription
= FALSE
;
226 Status
= EFI_SUCCESS
;
227 ZeroMem (OptionName
, sizeof (OptionName
));
230 TempOptionPtr
= BdsLibGetVariableAndSize (
232 &gEfiGlobalVariableGuid
,
236 // Compare with current option variable if the previous option is set in global variable.
238 for (Index
= 0; Index
< TempOptionSize
/ sizeof (UINT16
); Index
++) {
240 // TempOptionPtr must not be NULL if we have non-zero TempOptionSize.
242 ASSERT (TempOptionPtr
!= NULL
);
244 if (*VariableName
== 'B') {
245 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", TempOptionPtr
[Index
]);
247 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", TempOptionPtr
[Index
]);
250 OptionPtr
= BdsLibGetVariableAndSize (
252 &gEfiGlobalVariableGuid
,
255 if (OptionPtr
== NULL
) {
260 // Validate the variable.
262 if (!ValidateOption(OptionPtr
, OptionSize
)) {
268 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
269 Description
= (CHAR16
*) TempPtr
;
270 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
271 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
274 // Notes: the description may will change base on the GetStringToken
276 if (CompareMem (OptionDevicePath
, DevicePath
, GetDevicePathSize (OptionDevicePath
)) == 0) {
277 if (CompareMem (Description
, String
, StrSize (Description
)) == 0) {
279 // Got the option, so just return
281 FreePool (OptionPtr
);
282 FreePool (TempOptionPtr
);
286 // Option description changed, need update.
288 UpdateDescription
= TRUE
;
289 FreePool (OptionPtr
);
294 FreePool (OptionPtr
);
297 OptionSize
= sizeof (UINT32
) + sizeof (UINT16
) + StrSize (String
);
298 OptionSize
+= GetDevicePathSize (DevicePath
);
299 OptionPtr
= AllocateZeroPool (OptionSize
);
300 ASSERT (OptionPtr
!= NULL
);
303 *(UINT32
*) TempPtr
= LOAD_OPTION_ACTIVE
;
304 TempPtr
+= sizeof (UINT32
);
305 *(UINT16
*) TempPtr
= (UINT16
) GetDevicePathSize (DevicePath
);
306 TempPtr
+= sizeof (UINT16
);
307 CopyMem (TempPtr
, String
, StrSize (String
));
308 TempPtr
+= StrSize (String
);
309 CopyMem (TempPtr
, DevicePath
, GetDevicePathSize (DevicePath
));
311 if (UpdateDescription
) {
313 // The number in option#### to be updated.
314 // In this case, we must have non-NULL TempOptionPtr.
316 ASSERT (TempOptionPtr
!= NULL
);
317 RegisterOptionNumber
= TempOptionPtr
[Index
];
320 // The new option#### number
322 RegisterOptionNumber
= BdsLibGetFreeOptionNumber(VariableName
);
325 if (*VariableName
== 'B') {
326 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", RegisterOptionNumber
);
328 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", RegisterOptionNumber
);
331 Status
= gRT
->SetVariable (
333 &gEfiGlobalVariableGuid
,
334 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
339 // Return if only need to update a changed description or fail to set option.
341 if (EFI_ERROR (Status
) || UpdateDescription
) {
342 FreePool (OptionPtr
);
343 if (TempOptionPtr
!= NULL
) {
344 FreePool (TempOptionPtr
);
349 FreePool (OptionPtr
);
352 // Update the option order variable
356 // If no option order
358 if (TempOptionSize
== 0) {
360 Status
= gRT
->SetVariable (
362 &gEfiGlobalVariableGuid
,
363 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
367 if (TempOptionPtr
!= NULL
) {
368 FreePool (TempOptionPtr
);
374 // TempOptionPtr must not be NULL if TempOptionSize is not zero.
376 ASSERT (TempOptionPtr
!= NULL
);
378 // Append the new option number to the original option order
380 OrderItemNum
= (TempOptionSize
/ sizeof (UINT16
)) + 1 ;
381 OptionOrderPtr
= AllocateZeroPool ( OrderItemNum
* sizeof (UINT16
));
382 ASSERT (OptionOrderPtr
!= NULL
);
383 CopyMem (OptionOrderPtr
, TempOptionPtr
, (OrderItemNum
- 1) * sizeof (UINT16
));
385 OptionOrderPtr
[Index
] = RegisterOptionNumber
;
387 Status
= gRT
->SetVariable (
389 &gEfiGlobalVariableGuid
,
390 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
391 OrderItemNum
* sizeof (UINT16
),
394 FreePool (TempOptionPtr
);
395 FreePool (OptionOrderPtr
);
401 Returns the size of a device path in bytes.
403 This function returns the size, in bytes, of the device path data structure
404 specified by DevicePath including the end of device path node. If DevicePath
405 is NULL, then 0 is returned. If the length of the device path is bigger than
406 MaxSize, also return 0 to indicate this is an invalidate device path.
408 @param DevicePath A pointer to a device path data structure.
409 @param MaxSize Max valid device path size. If big than this size,
412 @retval 0 An invalid device path.
413 @retval Others The size of a device path in bytes.
417 GetDevicePathSizeEx (
418 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
425 if (DevicePath
== NULL
) {
430 // Search for the end of the device path structure
433 while (!IsDevicePathEnd (DevicePath
)) {
434 NodeSize
= DevicePathNodeLength (DevicePath
);
435 if (NodeSize
< END_DEVICE_PATH_LENGTH
) {
439 if (Size
> MaxSize
) {
442 DevicePath
= NextDevicePathNode (DevicePath
);
444 Size
+= DevicePathNodeLength (DevicePath
);
445 if (Size
> MaxSize
) {
453 Returns the length of a Null-terminated Unicode string. If the length is
454 bigger than MaxStringLen, return length 0 to indicate that this is an
457 This function returns the byte length of Unicode characters in the Null-terminated
458 Unicode string specified by String.
460 If String is NULL, then ASSERT().
461 If String is not aligned on a 16-bit boundary, then ASSERT().
463 @param String A pointer to a Null-terminated Unicode string.
464 @param MaxStringLen Max string len in this string.
466 @retval 0 An invalid string.
467 @retval Others The length of String.
472 IN CONST CHAR16
*String
,
473 IN UINTN MaxStringLen
478 ASSERT (String
!= NULL
&& MaxStringLen
!= 0);
479 ASSERT (((UINTN
) String
& BIT0
) == 0);
481 for (Length
= 0; *String
!= L
'\0' && MaxStringLen
!= Length
; String
++, Length
+=2);
483 if (*String
!= L
'\0' && MaxStringLen
== Length
) {
491 Validate the EFI Boot#### variable (VendorGuid/Name)
493 @param Variable Boot#### variable data.
494 @param VariableSize Returns the size of the EFI variable that was read
496 @retval TRUE The variable data is correct.
497 @retval FALSE The variable data is corrupted.
508 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
511 if (VariableSize
<= sizeof (UINT16
) + sizeof (UINT32
)) {
516 // Skip the option attribute
519 TempPtr
+= sizeof (UINT32
);
522 // Get the option's device path size
524 FilePathSize
= *(UINT16
*) TempPtr
;
525 TempPtr
+= sizeof (UINT16
);
528 // Get the option's description string size
530 TempSize
= StrSizeEx ((CHAR16
*) TempPtr
, VariableSize
- sizeof (UINT16
) - sizeof (UINT32
));
534 // Get the option's device path
536 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
537 TempPtr
+= FilePathSize
;
540 // Validation boot option variable.
542 if ((FilePathSize
== 0) || (TempSize
== 0)) {
546 if (TempSize
+ FilePathSize
+ sizeof (UINT16
) + sizeof (UINT32
) > VariableSize
) {
550 return (BOOLEAN
) (GetDevicePathSizeEx (DevicePath
, FilePathSize
) != 0);
554 Convert a single character to number.
555 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'
557 @param Char The input char which need to change to a hex number.
565 if ((Char
>= L
'0') && (Char
<= L
'9')) {
566 return (UINTN
) (Char
- L
'0');
569 if ((Char
>= L
'A') && (Char
<= L
'F')) {
570 return (UINTN
) (Char
- L
'A' + 0xA);
578 Build the boot#### or driver#### option from the VariableName, the
579 build boot#### or driver#### will also be linked to BdsCommonOptionList.
581 @param BdsCommonOptionList The header of the boot#### or driver#### option
583 @param VariableName EFI Variable name indicate if it is boot#### or
586 @retval BDS_COMMON_OPTION Get the option just been created
587 @retval NULL Failed to get the new option
592 BdsLibVariableToOption (
593 IN OUT LIST_ENTRY
*BdsCommonOptionList
,
594 IN CHAR16
*VariableName
602 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
603 BDS_COMMON_OPTION
*Option
;
605 UINT32 LoadOptionsSize
;
610 // Read the variable. We will never free this data.
612 Variable
= BdsLibGetVariableAndSize (
614 &gEfiGlobalVariableGuid
,
617 if (Variable
== NULL
) {
622 // Validate Boot#### variable data.
624 if (!ValidateOption(Variable
, VariableSize
)) {
630 // Notes: careful defined the variable of Boot#### or
631 // Driver####, consider use some macro to abstract the code
634 // Get the option attribute
637 Attribute
= *(UINT32
*) Variable
;
638 TempPtr
+= sizeof (UINT32
);
641 // Get the option's device path size
643 FilePathSize
= *(UINT16
*) TempPtr
;
644 TempPtr
+= sizeof (UINT16
);
647 // Get the option's description string
649 Description
= (CHAR16
*) TempPtr
;
652 // Get the option's description string size
654 TempPtr
+= StrSize((CHAR16
*) TempPtr
);
657 // Get the option's device path
659 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
660 TempPtr
+= FilePathSize
;
663 // Get load opion data.
665 LoadOptions
= TempPtr
;
666 LoadOptionsSize
= (UINT32
) (VariableSize
- (UINTN
) (TempPtr
- Variable
));
669 // The Console variables may have multiple device paths, so make
670 // an Entry for each one.
672 Option
= AllocateZeroPool (sizeof (BDS_COMMON_OPTION
));
673 if (Option
== NULL
) {
678 Option
->Signature
= BDS_LOAD_OPTION_SIGNATURE
;
679 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
680 ASSERT(Option
->DevicePath
!= NULL
);
681 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
683 Option
->Attribute
= Attribute
;
684 Option
->Description
= AllocateZeroPool (StrSize (Description
));
685 ASSERT(Option
->Description
!= NULL
);
686 CopyMem (Option
->Description
, Description
, StrSize (Description
));
688 Option
->LoadOptions
= AllocateZeroPool (LoadOptionsSize
);
689 ASSERT(Option
->LoadOptions
!= NULL
);
690 CopyMem (Option
->LoadOptions
, LoadOptions
, LoadOptionsSize
);
691 Option
->LoadOptionsSize
= LoadOptionsSize
;
694 // Get the value from VariableName Unicode string
695 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this
696 // Unicode stream to ASCII without any loss in meaning.
698 if (*VariableName
== 'B') {
699 NumOff
= (UINT8
) (sizeof (L
"Boot") / sizeof (CHAR16
) - 1);
700 Option
->BootCurrent
= (UINT16
) (CharToUint (VariableName
[NumOff
+0]) * 0x1000)
701 + (UINT16
) (CharToUint (VariableName
[NumOff
+1]) * 0x100)
702 + (UINT16
) (CharToUint (VariableName
[NumOff
+2]) * 0x10)
703 + (UINT16
) (CharToUint (VariableName
[NumOff
+3]) * 0x1);
705 InsertTailList (BdsCommonOptionList
, &Option
->Link
);
711 Process BootOrder, or DriverOrder variables, by calling
712 BdsLibVariableToOption () for each UINT16 in the variables.
714 @param BdsCommonOptionList The header of the option list base on variable
716 @param VariableName EFI Variable name indicate the BootOrder or
719 @retval EFI_SUCCESS Success create the boot option or driver option
721 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list
726 BdsLibBuildOptionFromVar (
727 IN LIST_ENTRY
*BdsCommonOptionList
,
728 IN CHAR16
*VariableName
732 UINTN OptionOrderSize
;
734 BDS_COMMON_OPTION
*Option
;
735 CHAR16 OptionName
[20];
738 // Zero Buffer in order to get all BOOT#### variables
740 ZeroMem (OptionName
, sizeof (OptionName
));
743 // Read the BootOrder, or DriverOrder variable.
745 OptionOrder
= BdsLibGetVariableAndSize (
747 &gEfiGlobalVariableGuid
,
750 if (OptionOrder
== NULL
) {
751 return EFI_OUT_OF_RESOURCES
;
754 for (Index
= 0; Index
< OptionOrderSize
/ sizeof (UINT16
); Index
++) {
755 if (*VariableName
== 'B') {
756 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionOrder
[Index
]);
758 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", OptionOrder
[Index
]);
761 Option
= BdsLibVariableToOption (BdsCommonOptionList
, OptionName
);
762 if (Option
!= NULL
) {
763 Option
->BootCurrent
= OptionOrder
[Index
];
767 FreePool (OptionOrder
);
773 Get boot mode by looking up configuration table and parsing HOB list
775 @param BootMode Boot mode from PEI handoff HOB.
777 @retval EFI_SUCCESS Successfully get boot mode
783 OUT EFI_BOOT_MODE
*BootMode
786 *BootMode
= GetBootModeHob ();
792 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
793 buffer, and the size of the buffer. If failure return NULL.
795 @param Name String part of EFI variable name
796 @param VendorGuid GUID part of EFI variable name
797 @param VariableSize Returns the size of the EFI variable that was read
799 @return Dynamically allocated memory that contains a copy of the EFI variable
800 Caller is responsible freeing the buffer.
801 @retval NULL Variable was not read
806 BdsLibGetVariableAndSize (
808 IN EFI_GUID
*VendorGuid
,
809 OUT UINTN
*VariableSize
819 // Pass in a zero size buffer to find the required buffer size.
822 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
823 if (Status
== EFI_BUFFER_TOO_SMALL
) {
825 // Allocate the buffer to return
827 Buffer
= AllocateZeroPool (BufferSize
);
828 if (Buffer
== NULL
) {
833 // Read variable into the allocated buffer.
835 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
836 if (EFI_ERROR (Status
)) {
843 ASSERT (((Buffer
== NULL
) && (BufferSize
== 0)) ||
844 ((Buffer
!= NULL
) && (BufferSize
!= 0))
846 *VariableSize
= BufferSize
;
851 Delete the instance in Multi which matches partly with Single instance
853 @param Multi A pointer to a multi-instance device path data
855 @param Single A pointer to a single-instance device path data
858 @return This function will remove the device path instances in Multi which partly
859 match with the Single, and return the result device path. If there is no
860 remaining device path as a result, this function will return NULL.
863 EFI_DEVICE_PATH_PROTOCOL
*
865 BdsLibDelPartMatchInstance (
866 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
867 IN EFI_DEVICE_PATH_PROTOCOL
*Single
870 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
871 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
872 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
877 NewDevicePath
= NULL
;
878 TempNewDevicePath
= NULL
;
880 if (Multi
== NULL
|| Single
== NULL
) {
884 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
885 SingleDpSize
= GetDevicePathSize (Single
) - END_DEVICE_PATH_LENGTH
;
886 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
888 while (Instance
!= NULL
) {
890 Size
= (SingleDpSize
< InstanceSize
) ? SingleDpSize
: InstanceSize
;
892 if ((CompareMem (Instance
, Single
, Size
) != 0)) {
894 // Append the device path instance which does not match with Single
896 TempNewDevicePath
= NewDevicePath
;
897 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, Instance
);
898 if (TempNewDevicePath
!= NULL
) {
899 FreePool(TempNewDevicePath
);
903 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
904 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
907 return NewDevicePath
;
911 Function compares a device path data structure to that of all the nodes of a
912 second device path instance.
914 @param Multi A pointer to a multi-instance device path data
916 @param Single A pointer to a single-instance device path data
919 @retval TRUE If the Single device path is contained within Multi device path.
920 @retval FALSE The Single device path is not match within Multi device path.
925 BdsLibMatchDevicePaths (
926 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
927 IN EFI_DEVICE_PATH_PROTOCOL
*Single
930 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
931 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
934 if (Multi
== NULL
|| Single
== NULL
) {
939 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
942 // Search for the match of 'Single' in 'Multi'
944 while (DevicePathInst
!= NULL
) {
946 // If the single device path is found in multiple device paths,
949 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
950 FreePool (DevicePathInst
);
954 FreePool (DevicePathInst
);
955 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
962 This function prints a series of strings.
964 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
965 @param ... A variable argument list containing series of
966 strings, the last string must be NULL.
968 @retval EFI_SUCCESS Success print out the string using ConOut.
969 @retval EFI_STATUS Return the status of the ConOut->OutputString ().
974 BdsLibOutputStrings (
975 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*ConOut
,
983 Status
= EFI_SUCCESS
;
984 VA_START (Args
, ConOut
);
986 while (!EFI_ERROR (Status
)) {
988 // If String is NULL, then it's the end of the list
990 String
= VA_ARG (Args
, CHAR16
*);
991 if (String
== NULL
) {
995 Status
= ConOut
->OutputString (ConOut
, String
);
997 if (EFI_ERROR (Status
)) {
1007 // Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.
1008 // Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if
1009 // user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.
1014 Enable the setup browser reset reminder feature.
1015 This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.
1020 EnableResetReminderFeature (
1024 mFeaturerSwitch
= TRUE
;
1029 Disable the setup browser reset reminder feature.
1030 This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.
1035 DisableResetReminderFeature (
1039 mFeaturerSwitch
= FALSE
;
1044 Record the info that a reset is required.
1045 A module boolean variable is used to record whether a reset is required.
1050 EnableResetRequired (
1054 mResetRequired
= TRUE
;
1059 Record the info that no reset is required.
1060 A module boolean variable is used to record whether a reset is required.
1065 DisableResetRequired (
1069 mResetRequired
= FALSE
;
1074 Check whether platform policy enable the reset reminder feature. The default is enabled.
1079 IsResetReminderFeatureEnable (
1083 return mFeaturerSwitch
;
1088 Check if user changed any option setting which needs a system reset to be effective.
1097 return mResetRequired
;
1102 Check whether a reset is needed, and finish the reset reminder feature.
1103 If a reset is needed, Popup a menu to notice user, and finish the feature
1104 according to the user selection.
1109 SetupResetReminder (
1114 CHAR16
*StringBuffer1
;
1115 CHAR16
*StringBuffer2
;
1119 //check any reset required change is applied? if yes, reset system
1121 if (IsResetReminderFeatureEnable ()) {
1122 if (IsResetRequired ()) {
1124 StringBuffer1
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
1125 ASSERT (StringBuffer1
!= NULL
);
1126 StringBuffer2
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
1127 ASSERT (StringBuffer2
!= NULL
);
1128 StrCpy (StringBuffer1
, L
"Configuration changed. Reset to apply it Now.");
1129 StrCpy (StringBuffer2
, L
"Press ENTER to reset");
1131 // Popup a menu to notice user
1134 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, StringBuffer1
, StringBuffer2
, NULL
);
1135 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
1137 FreePool (StringBuffer1
);
1138 FreePool (StringBuffer2
);
1140 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
1146 Get the headers (dos, image, optional header) from an image
1148 @param Device SimpleFileSystem device handle
1149 @param FileName File name for the image
1150 @param DosHeader Pointer to dos header
1151 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
1153 @retval EFI_SUCCESS Successfully get the machine type.
1154 @retval EFI_NOT_FOUND The file is not found.
1155 @retval EFI_LOAD_ERROR File is not a valid image file.
1160 BdsLibGetImageHeader (
1161 IN EFI_HANDLE Device
,
1162 IN CHAR16
*FileName
,
1163 OUT EFI_IMAGE_DOS_HEADER
*DosHeader
,
1164 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
1168 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
1169 EFI_FILE_HANDLE Root
;
1170 EFI_FILE_HANDLE ThisFile
;
1173 EFI_FILE_INFO
*Info
;
1178 // Handle the file system interface to the device
1180 Status
= gBS
->HandleProtocol (
1182 &gEfiSimpleFileSystemProtocolGuid
,
1185 if (EFI_ERROR (Status
)) {
1189 Status
= Volume
->OpenVolume (
1193 if (EFI_ERROR (Status
)) {
1197 ASSERT (Root
!= NULL
);
1198 Status
= Root
->Open (Root
, &ThisFile
, FileName
, EFI_FILE_MODE_READ
, 0);
1199 if (EFI_ERROR (Status
)) {
1202 ASSERT (ThisFile
!= NULL
);
1207 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
1210 Status
= gBS
->AllocatePool (EfiBootServicesData
, BufferSize
, (VOID
**) &Info
);
1211 if (EFI_ERROR (Status
)) {
1214 Status
= ThisFile
->GetInfo (
1220 if (!EFI_ERROR (Status
)) {
1223 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1230 FileSize
= Info
->FileSize
;
1236 BufferSize
= sizeof (EFI_IMAGE_DOS_HEADER
);
1237 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, DosHeader
);
1238 if (EFI_ERROR (Status
) ||
1239 BufferSize
< sizeof (EFI_IMAGE_DOS_HEADER
) ||
1240 FileSize
<= DosHeader
->e_lfanew
||
1241 DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
1242 Status
= EFI_LOAD_ERROR
;
1247 // Move to PE signature
1249 Status
= ThisFile
->SetPosition (ThisFile
, DosHeader
->e_lfanew
);
1250 if (EFI_ERROR (Status
)) {
1251 Status
= EFI_LOAD_ERROR
;
1256 // Read and check PE signature
1258 BufferSize
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
1259 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, Hdr
.Pe32
);
1260 if (EFI_ERROR (Status
) ||
1261 BufferSize
< sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
) ||
1262 Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1263 Status
= EFI_LOAD_ERROR
;
1268 // Check PE32 or PE32+ magic
1270 if (Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
&&
1271 Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
1272 Status
= EFI_LOAD_ERROR
;
1277 if (ThisFile
!= NULL
) {
1278 ThisFile
->Close (ThisFile
);
1287 This routine adjust the memory information for different memory type and
1288 save them into the variables for next boot.
1291 BdsSetMemoryTypeInformationVariable (
1296 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
1297 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
1304 EFI_HOB_GUID_TYPE
*GuidHob
;
1305 BOOLEAN MemoryTypeInformationModified
;
1306 BOOLEAN MemoryTypeInformationVariableExists
;
1307 EFI_BOOT_MODE BootMode
;
1309 MemoryTypeInformationModified
= FALSE
;
1310 MemoryTypeInformationVariableExists
= FALSE
;
1313 BootMode
= GetBootModeHob ();
1315 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.
1317 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
1322 // Only check the the Memory Type Information variable in the boot mode
1323 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type
1324 // Information is not valid in this boot mode.
1326 if (BootMode
!= BOOT_WITH_DEFAULT_SETTINGS
) {
1328 Status
= gRT
->GetVariable (
1329 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
1330 &gEfiMemoryTypeInformationGuid
,
1335 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1336 MemoryTypeInformationVariableExists
= TRUE
;
1341 // Retrieve the current memory usage statistics. If they are not found, then
1342 // no adjustments can be made to the Memory Type Information variable.
1344 Status
= EfiGetSystemConfigurationTable (
1345 &gEfiMemoryTypeInformationGuid
,
1346 (VOID
**) &CurrentMemoryTypeInformation
1348 if (EFI_ERROR (Status
) || CurrentMemoryTypeInformation
== NULL
) {
1353 // Get the Memory Type Information settings from Hob if they exist,
1354 // PEI is responsible for getting them from variable and build a Hob to save them.
1355 // If the previous Memory Type Information is not available, then set defaults
1357 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
1358 if (GuidHob
== NULL
) {
1360 // If Platform has not built Memory Type Info into the Hob, just return.
1364 PreviousMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
1365 VariableSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
1368 // Use a heuristic to adjust the Memory Type Information for the next boot
1370 DEBUG ((EFI_D_INFO
, "Memory Previous Current Next \n"));
1371 DEBUG ((EFI_D_INFO
, " Type Pages Pages Pages \n"));
1372 DEBUG ((EFI_D_INFO
, "====== ======== ======== ========\n"));
1374 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
1376 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
1377 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
1381 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
1386 // Previous is the number of pages pre-allocated
1387 // Current is the number of pages actually needed
1389 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
1390 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
1394 // Inconsistent Memory Reserved across bootings may lead to S4 fail
1395 // Write next varible to 125% * current when the pre-allocated memory is:
1396 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING
1397 // 2. Less than the needed memory
1399 if ((Current
+ (Current
>> 1)) < Previous
) {
1400 if (BootMode
== BOOT_WITH_DEFAULT_SETTINGS
) {
1401 Next
= Current
+ (Current
>> 2);
1403 } else if (Current
> Previous
) {
1404 Next
= Current
+ (Current
>> 2);
1406 if (Next
> 0 && Next
< 4) {
1410 if (Next
!= Previous
) {
1411 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
1412 MemoryTypeInformationModified
= TRUE
;
1415 DEBUG ((EFI_D_INFO
, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation
[Index
].Type
, Previous
, Current
, Next
));
1419 // If any changes were made to the Memory Type Information settings, then set the new variable value;
1420 // Or create the variable in first boot.
1422 if (MemoryTypeInformationModified
|| !MemoryTypeInformationVariableExists
) {
1423 Status
= SetVariableAndReportStatusCodeOnError (
1424 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
1425 &gEfiMemoryTypeInformationGuid
,
1426 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1428 PreviousMemoryTypeInformation
1431 if (!EFI_ERROR (Status
)) {
1433 // If the Memory Type Information settings have been modified, then reset the platform
1434 // so the new Memory Type Information setting will be used to guarantee that an S4
1435 // entry/resume cycle will not fail.
1437 if (MemoryTypeInformationModified
&& PcdGetBool (PcdResetOnMemoryTypeInformationChange
)) {
1438 DEBUG ((EFI_D_INFO
, "Memory Type Information settings change. Warm Reset!!!\n"));
1439 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
1442 DEBUG ((EFI_D_ERROR
, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));
1448 This routine is kept for backward compatibility.
1452 BdsLibSaveMemoryTypeInformation (
1460 Identify a user and, if authenticated, returns the current user profile handle.
1462 @param[out] User Point to user profile handle.
1464 @retval EFI_SUCCESS User is successfully identified, or user identification
1466 @retval EFI_ACCESS_DENIED User is not successfully identified
1471 BdsLibUserIdentify (
1472 OUT EFI_USER_PROFILE_HANDLE
*User
1476 EFI_USER_MANAGER_PROTOCOL
*Manager
;
1478 Status
= gBS
->LocateProtocol (
1479 &gEfiUserManagerProtocolGuid
,
1483 if (EFI_ERROR (Status
)) {
1487 return Manager
->Identify (Manager
, User
);
1491 Set the variable and report the error through status code upon failure.
1493 @param VariableName A Null-terminated string that is the name of the vendor's variable.
1494 Each VariableName is unique for each VendorGuid. VariableName must
1495 contain 1 or more characters. If VariableName is an empty string,
1496 then EFI_INVALID_PARAMETER is returned.
1497 @param VendorGuid A unique identifier for the vendor.
1498 @param Attributes Attributes bitmask to set for the variable.
1499 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
1500 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
1501 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
1502 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
1503 set, then a SetVariable() call with a DataSize of zero will not cause any change to
1504 the variable value (the timestamp associated with the variable may be updated however
1505 even if no new data value is provided,see the description of the
1506 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
1507 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
1508 @param Data The contents for the variable.
1510 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
1511 defined by the Attributes.
1512 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
1513 DataSize exceeds the maximum allowed.
1514 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
1515 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
1516 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
1517 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
1518 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
1519 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1520 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
1521 does NOT pass the validation check carried out by the firmware.
1523 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
1526 SetVariableAndReportStatusCodeOnError (
1527 IN CHAR16
*VariableName
,
1528 IN EFI_GUID
*VendorGuid
,
1529 IN UINT32 Attributes
,
1535 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
1538 Status
= gRT
->SetVariable (
1545 if (EFI_ERROR (Status
)) {
1546 NameSize
= StrSize (VariableName
);
1547 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
1548 if (SetVariableStatus
!= NULL
) {
1549 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
1550 SetVariableStatus
->NameSize
= NameSize
;
1551 SetVariableStatus
->DataSize
= DataSize
;
1552 SetVariableStatus
->SetStatus
= Status
;
1553 SetVariableStatus
->Attributes
= Attributes
;
1554 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
1555 if ((Data
!= NULL
) && (DataSize
!= 0)) {
1556 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
1559 REPORT_STATUS_CODE_EX (
1561 PcdGet32 (PcdErrorCodeSetVariable
),
1564 &gEdkiiStatusCodeDataTypeVariableGuid
,
1566 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
1569 FreePool (SetVariableStatus
);