2 Misc BDS library function
4 Copyright (c) 2004 - 2015, 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 #define MAX_STRING_LEN 200
20 BOOLEAN mFeaturerSwitch
= TRUE
;
21 BOOLEAN mResetRequired
= FALSE
;
23 extern UINT16 gPlatformBootTimeOutDefault
;
26 The function will go through the driver option link list, load and start
27 every driver the driver option device path point to.
29 @param BdsDriverLists The header of the current driver option link list
35 IN LIST_ENTRY
*BdsDriverLists
40 BDS_COMMON_OPTION
*Option
;
41 EFI_HANDLE ImageHandle
;
42 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
50 // Process the driver option
52 for (Link
= BdsDriverLists
->ForwardLink
; Link
!= BdsDriverLists
; Link
= Link
->ForwardLink
) {
53 Option
= CR (Link
, BDS_COMMON_OPTION
, Link
, BDS_LOAD_OPTION_SIGNATURE
);
56 // If a load option is not marked as LOAD_OPTION_ACTIVE,
57 // the boot manager will not automatically load the option.
59 if (!IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_ACTIVE
)) {
64 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
65 // then all of the EFI drivers in the system will be disconnected and
66 // reconnected after the last driver load option is processed.
68 if (IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_FORCE_RECONNECT
)) {
73 // Make sure the driver path is connected.
75 BdsLibConnectDevicePath (Option
->DevicePath
);
78 // Load and start the image that Driver#### describes
80 Status
= gBS
->LoadImage (
89 if (!EFI_ERROR (Status
)) {
90 gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
93 // Verify whether this image is a driver, if not,
94 // exit it and continue to parse next load option
96 if (ImageInfo
->ImageCodeType
!= EfiBootServicesCode
&& ImageInfo
->ImageCodeType
!= EfiRuntimeServicesCode
) {
97 gBS
->Exit (ImageHandle
, EFI_INVALID_PARAMETER
, 0, NULL
);
101 if (Option
->LoadOptionsSize
!= 0) {
102 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
103 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
106 // Before calling the image, enable the Watchdog Timer for
107 // the 5 Minute period
109 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
111 Status
= gBS
->StartImage (ImageHandle
, &ExitDataSize
, &ExitData
);
112 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Driver Return Status = %r\n", Status
));
115 // Clear the Watchdog Timer after the image returns
117 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
122 // Process the LOAD_OPTION_FORCE_RECONNECT driver option
125 BdsLibDisconnectAllEfi ();
132 Get the Option Number that does not used.
133 Try to locate the specific option variable one by one utile find a free number.
135 @param VariableName Indicate if the boot#### or driver#### option
137 @return The Minimal Free Option Number
141 BdsLibGetFreeOptionNumber (
142 IN CHAR16
*VariableName
147 UINT16
*OptionBuffer
;
151 // Try to find the minimum free number from 0, 1, 2, 3....
155 if (*VariableName
== 'B') {
156 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Boot%04x", Index
);
158 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Driver%04x", Index
);
161 // try if the option number is used
163 OptionBuffer
= BdsLibGetVariableAndSize (
165 &gEfiGlobalVariableGuid
,
168 if (OptionBuffer
== NULL
) {
171 FreePool(OptionBuffer
);
175 return ((UINT16
) Index
);
180 This function will register the new boot#### or driver#### option base on
181 the VariableName. The new registered boot#### or driver#### will be linked
182 to BdsOptionList and also update to the VariableName. After the boot#### or
183 driver#### updated, the BootOrder or DriverOrder will also be updated.
185 @param BdsOptionList The header of the boot#### or driver#### link list
186 @param DevicePath The device path which the boot#### or driver####
188 @param String The description of the boot#### or driver####
189 @param VariableName Indicate if the boot#### or driver#### option
191 @retval EFI_SUCCESS The boot#### or driver#### have been success
193 @retval EFI_STATUS Return the status of gRT->SetVariable ().
198 BdsLibRegisterNewOption (
199 IN LIST_ENTRY
*BdsOptionList
,
200 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
202 IN CHAR16
*VariableName
207 UINT16 RegisterOptionNumber
;
208 UINT16
*TempOptionPtr
;
209 UINTN TempOptionSize
;
210 UINT16
*OptionOrderPtr
;
214 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
216 CHAR16 OptionName
[10];
217 BOOLEAN UpdateDescription
;
218 UINT16 BootOrderEntry
;
221 if (DevicePath
== NULL
) {
222 return EFI_INVALID_PARAMETER
;
228 OptionDevicePath
= NULL
;
230 OptionOrderPtr
= NULL
;
231 UpdateDescription
= FALSE
;
232 Status
= EFI_SUCCESS
;
233 ZeroMem (OptionName
, sizeof (OptionName
));
236 TempOptionPtr
= BdsLibGetVariableAndSize (
238 &gEfiGlobalVariableGuid
,
242 // Compare with current option variable if the previous option is set in global variable.
244 for (Index
= 0; Index
< TempOptionSize
/ sizeof (UINT16
); Index
++) {
246 // TempOptionPtr must not be NULL if we have non-zero TempOptionSize.
248 ASSERT (TempOptionPtr
!= NULL
);
250 if (*VariableName
== 'B') {
251 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", TempOptionPtr
[Index
]);
253 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", TempOptionPtr
[Index
]);
256 OptionPtr
= BdsLibGetVariableAndSize (
258 &gEfiGlobalVariableGuid
,
261 if (OptionPtr
== NULL
) {
266 // Validate the variable.
268 if (!ValidateOption(OptionPtr
, OptionSize
)) {
274 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
275 Description
= (CHAR16
*) TempPtr
;
276 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
277 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
280 // Notes: the description may will change base on the GetStringToken
282 if (CompareMem (OptionDevicePath
, DevicePath
, GetDevicePathSize (OptionDevicePath
)) == 0) {
283 if (CompareMem (Description
, String
, StrSize (Description
)) == 0) {
285 // Got the option, so just return
287 FreePool (OptionPtr
);
288 FreePool (TempOptionPtr
);
292 // Option description changed, need update.
294 UpdateDescription
= TRUE
;
295 FreePool (OptionPtr
);
300 FreePool (OptionPtr
);
303 OptionSize
= sizeof (UINT32
) + sizeof (UINT16
) + StrSize (String
);
304 OptionSize
+= GetDevicePathSize (DevicePath
);
305 OptionPtr
= AllocateZeroPool (OptionSize
);
306 ASSERT (OptionPtr
!= NULL
);
309 *(UINT32
*) TempPtr
= LOAD_OPTION_ACTIVE
;
310 TempPtr
+= sizeof (UINT32
);
311 *(UINT16
*) TempPtr
= (UINT16
) GetDevicePathSize (DevicePath
);
312 TempPtr
+= sizeof (UINT16
);
313 CopyMem (TempPtr
, String
, StrSize (String
));
314 TempPtr
+= StrSize (String
);
315 CopyMem (TempPtr
, DevicePath
, GetDevicePathSize (DevicePath
));
317 if (UpdateDescription
) {
319 // The number in option#### to be updated.
320 // In this case, we must have non-NULL TempOptionPtr.
322 ASSERT (TempOptionPtr
!= NULL
);
323 RegisterOptionNumber
= TempOptionPtr
[Index
];
326 // The new option#### number
328 RegisterOptionNumber
= BdsLibGetFreeOptionNumber(VariableName
);
331 if (*VariableName
== 'B') {
332 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", RegisterOptionNumber
);
334 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", RegisterOptionNumber
);
337 Status
= gRT
->SetVariable (
339 &gEfiGlobalVariableGuid
,
340 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
345 // Return if only need to update a changed description or fail to set option.
347 if (EFI_ERROR (Status
) || UpdateDescription
) {
348 FreePool (OptionPtr
);
349 if (TempOptionPtr
!= NULL
) {
350 FreePool (TempOptionPtr
);
355 FreePool (OptionPtr
);
358 // Update the option order variable
362 // If no option order
364 if (TempOptionSize
== 0) {
366 Status
= gRT
->SetVariable (
368 &gEfiGlobalVariableGuid
,
369 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
373 if (TempOptionPtr
!= NULL
) {
374 FreePool (TempOptionPtr
);
380 // TempOptionPtr must not be NULL if TempOptionSize is not zero.
382 ASSERT (TempOptionPtr
!= NULL
);
384 // Append the new option number to the original option order
386 OrderItemNum
= (TempOptionSize
/ sizeof (UINT16
)) + 1 ;
387 OptionOrderPtr
= AllocateZeroPool ( OrderItemNum
* sizeof (UINT16
));
388 ASSERT (OptionOrderPtr
!= NULL
);
389 CopyMem (OptionOrderPtr
, TempOptionPtr
, (OrderItemNum
- 1) * sizeof (UINT16
));
391 OptionOrderPtr
[Index
] = RegisterOptionNumber
;
393 Status
= gRT
->SetVariable (
395 &gEfiGlobalVariableGuid
,
396 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
397 OrderItemNum
* sizeof (UINT16
),
400 FreePool (TempOptionPtr
);
401 FreePool (OptionOrderPtr
);
407 Returns the size of a device path in bytes.
409 This function returns the size, in bytes, of the device path data structure
410 specified by DevicePath including the end of device path node. If DevicePath
411 is NULL, then 0 is returned. If the length of the device path is bigger than
412 MaxSize, also return 0 to indicate this is an invalidate device path.
414 @param DevicePath A pointer to a device path data structure.
415 @param MaxSize Max valid device path size. If big than this size,
418 @retval 0 An invalid device path.
419 @retval Others The size of a device path in bytes.
423 GetDevicePathSizeEx (
424 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
431 if (DevicePath
== NULL
) {
436 // Search for the end of the device path structure
439 while (!IsDevicePathEnd (DevicePath
)) {
440 NodeSize
= DevicePathNodeLength (DevicePath
);
441 if (NodeSize
< END_DEVICE_PATH_LENGTH
) {
445 if (Size
> MaxSize
) {
448 DevicePath
= NextDevicePathNode (DevicePath
);
450 Size
+= DevicePathNodeLength (DevicePath
);
451 if (Size
> MaxSize
) {
459 Returns the length of a Null-terminated Unicode string. If the length is
460 bigger than MaxStringLen, return length 0 to indicate that this is an
463 This function returns the byte length of Unicode characters in the Null-terminated
464 Unicode string specified by String.
466 If String is NULL, then ASSERT().
467 If String is not aligned on a 16-bit boundary, then ASSERT().
469 @param String A pointer to a Null-terminated Unicode string.
470 @param MaxStringLen Max string len in this string.
472 @retval 0 An invalid string.
473 @retval Others The length of String.
478 IN CONST CHAR16
*String
,
479 IN UINTN MaxStringLen
484 ASSERT (String
!= NULL
&& MaxStringLen
!= 0);
485 ASSERT (((UINTN
) String
& BIT0
) == 0);
487 for (Length
= 0; *String
!= L
'\0' && MaxStringLen
!= Length
; String
++, Length
+=2);
489 if (*String
!= L
'\0' && MaxStringLen
== Length
) {
497 Validate the EFI Boot#### variable (VendorGuid/Name)
499 @param Variable Boot#### variable data.
500 @param VariableSize Returns the size of the EFI variable that was read
502 @retval TRUE The variable data is correct.
503 @retval FALSE The variable data is corrupted.
514 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
517 if (VariableSize
<= sizeof (UINT16
) + sizeof (UINT32
)) {
522 // Skip the option attribute
525 TempPtr
+= sizeof (UINT32
);
528 // Get the option's device path size
530 FilePathSize
= *(UINT16
*) TempPtr
;
531 TempPtr
+= sizeof (UINT16
);
534 // Get the option's description string size
536 TempSize
= StrSizeEx ((CHAR16
*) TempPtr
, VariableSize
- sizeof (UINT16
) - sizeof (UINT32
));
540 // Get the option's device path
542 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
543 TempPtr
+= FilePathSize
;
546 // Validation boot option variable.
548 if ((FilePathSize
== 0) || (TempSize
== 0)) {
552 if (TempSize
+ FilePathSize
+ sizeof (UINT16
) + sizeof (UINT32
) > VariableSize
) {
556 return (BOOLEAN
) (GetDevicePathSizeEx (DevicePath
, FilePathSize
) != 0);
560 Convert a single character to number.
561 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'
563 @param Char The input char which need to change to a hex number.
571 if ((Char
>= L
'0') && (Char
<= L
'9')) {
572 return (UINTN
) (Char
- L
'0');
575 if ((Char
>= L
'A') && (Char
<= L
'F')) {
576 return (UINTN
) (Char
- L
'A' + 0xA);
584 Build the boot#### or driver#### option from the VariableName, the
585 build boot#### or driver#### will also be linked to BdsCommonOptionList.
587 @param BdsCommonOptionList The header of the boot#### or driver#### option
589 @param VariableName EFI Variable name indicate if it is boot#### or
592 @retval BDS_COMMON_OPTION Get the option just been created
593 @retval NULL Failed to get the new option
598 BdsLibVariableToOption (
599 IN OUT LIST_ENTRY
*BdsCommonOptionList
,
600 IN CHAR16
*VariableName
608 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
609 BDS_COMMON_OPTION
*Option
;
611 UINT32 LoadOptionsSize
;
616 // Read the variable. We will never free this data.
618 Variable
= BdsLibGetVariableAndSize (
620 &gEfiGlobalVariableGuid
,
623 if (Variable
== NULL
) {
628 // Validate Boot#### variable data.
630 if (!ValidateOption(Variable
, VariableSize
)) {
636 // Notes: careful defined the variable of Boot#### or
637 // Driver####, consider use some macro to abstract the code
640 // Get the option attribute
643 Attribute
= *(UINT32
*) Variable
;
644 TempPtr
+= sizeof (UINT32
);
647 // Get the option's device path size
649 FilePathSize
= *(UINT16
*) TempPtr
;
650 TempPtr
+= sizeof (UINT16
);
653 // Get the option's description string
655 Description
= (CHAR16
*) TempPtr
;
658 // Get the option's description string size
660 TempPtr
+= StrSize((CHAR16
*) TempPtr
);
663 // Get the option's device path
665 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
666 TempPtr
+= FilePathSize
;
669 // Get load opion data.
671 LoadOptions
= TempPtr
;
672 LoadOptionsSize
= (UINT32
) (VariableSize
- (UINTN
) (TempPtr
- Variable
));
675 // The Console variables may have multiple device paths, so make
676 // an Entry for each one.
678 Option
= AllocateZeroPool (sizeof (BDS_COMMON_OPTION
));
679 if (Option
== NULL
) {
684 Option
->Signature
= BDS_LOAD_OPTION_SIGNATURE
;
685 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
686 ASSERT(Option
->DevicePath
!= NULL
);
687 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
689 Option
->Attribute
= Attribute
;
690 Option
->Description
= AllocateZeroPool (StrSize (Description
));
691 ASSERT(Option
->Description
!= NULL
);
692 CopyMem (Option
->Description
, Description
, StrSize (Description
));
694 Option
->LoadOptions
= AllocateZeroPool (LoadOptionsSize
);
695 ASSERT(Option
->LoadOptions
!= NULL
);
696 CopyMem (Option
->LoadOptions
, LoadOptions
, LoadOptionsSize
);
697 Option
->LoadOptionsSize
= LoadOptionsSize
;
700 // Get the value from VariableName Unicode string
701 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this
702 // Unicode stream to ASCII without any loss in meaning.
704 if (*VariableName
== 'B') {
705 NumOff
= (UINT8
) (sizeof (L
"Boot") / sizeof (CHAR16
) - 1);
706 Option
->BootCurrent
= (UINT16
) (CharToUint (VariableName
[NumOff
+0]) * 0x1000)
707 + (UINT16
) (CharToUint (VariableName
[NumOff
+1]) * 0x100)
708 + (UINT16
) (CharToUint (VariableName
[NumOff
+2]) * 0x10)
709 + (UINT16
) (CharToUint (VariableName
[NumOff
+3]) * 0x1);
711 InsertTailList (BdsCommonOptionList
, &Option
->Link
);
717 Process BootOrder, or DriverOrder variables, by calling
718 BdsLibVariableToOption () for each UINT16 in the variables.
720 @param BdsCommonOptionList The header of the option list base on variable
722 @param VariableName EFI Variable name indicate the BootOrder or
725 @retval EFI_SUCCESS Success create the boot option or driver option
727 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list
732 BdsLibBuildOptionFromVar (
733 IN LIST_ENTRY
*BdsCommonOptionList
,
734 IN CHAR16
*VariableName
738 UINTN OptionOrderSize
;
740 BDS_COMMON_OPTION
*Option
;
741 CHAR16 OptionName
[20];
744 // Zero Buffer in order to get all BOOT#### variables
746 ZeroMem (OptionName
, sizeof (OptionName
));
749 // Read the BootOrder, or DriverOrder variable.
751 OptionOrder
= BdsLibGetVariableAndSize (
753 &gEfiGlobalVariableGuid
,
756 if (OptionOrder
== NULL
) {
757 return EFI_OUT_OF_RESOURCES
;
760 for (Index
= 0; Index
< OptionOrderSize
/ sizeof (UINT16
); Index
++) {
761 if (*VariableName
== 'B') {
762 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionOrder
[Index
]);
764 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", OptionOrder
[Index
]);
767 Option
= BdsLibVariableToOption (BdsCommonOptionList
, OptionName
);
768 if (Option
!= NULL
) {
769 Option
->BootCurrent
= OptionOrder
[Index
];
773 FreePool (OptionOrder
);
779 Get boot mode by looking up configuration table and parsing HOB list
781 @param BootMode Boot mode from PEI handoff HOB.
783 @retval EFI_SUCCESS Successfully get boot mode
789 OUT EFI_BOOT_MODE
*BootMode
792 *BootMode
= GetBootModeHob ();
798 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
799 buffer, and the size of the buffer. If failure return NULL.
801 @param Name String part of EFI variable name
802 @param VendorGuid GUID part of EFI variable name
803 @param VariableSize Returns the size of the EFI variable that was read
805 @return Dynamically allocated memory that contains a copy of the EFI variable
806 Caller is responsible freeing the buffer.
807 @retval NULL Variable was not read
812 BdsLibGetVariableAndSize (
814 IN EFI_GUID
*VendorGuid
,
815 OUT UINTN
*VariableSize
825 // Pass in a zero size buffer to find the required buffer size.
828 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
829 if (Status
== EFI_BUFFER_TOO_SMALL
) {
831 // Allocate the buffer to return
833 Buffer
= AllocateZeroPool (BufferSize
);
834 if (Buffer
== NULL
) {
839 // Read variable into the allocated buffer.
841 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
842 if (EFI_ERROR (Status
)) {
849 ASSERT (((Buffer
== NULL
) && (BufferSize
== 0)) ||
850 ((Buffer
!= NULL
) && (BufferSize
!= 0))
852 *VariableSize
= BufferSize
;
857 Delete the instance in Multi which matches partly with Single instance
859 @param Multi A pointer to a multi-instance device path data
861 @param Single A pointer to a single-instance device path data
864 @return This function will remove the device path instances in Multi which partly
865 match with the Single, and return the result device path. If there is no
866 remaining device path as a result, this function will return NULL.
869 EFI_DEVICE_PATH_PROTOCOL
*
871 BdsLibDelPartMatchInstance (
872 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
873 IN EFI_DEVICE_PATH_PROTOCOL
*Single
876 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
877 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
878 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
883 NewDevicePath
= NULL
;
884 TempNewDevicePath
= NULL
;
886 if (Multi
== NULL
|| Single
== NULL
) {
890 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
891 SingleDpSize
= GetDevicePathSize (Single
) - END_DEVICE_PATH_LENGTH
;
892 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
894 while (Instance
!= NULL
) {
896 Size
= (SingleDpSize
< InstanceSize
) ? SingleDpSize
: InstanceSize
;
898 if ((CompareMem (Instance
, Single
, Size
) != 0)) {
900 // Append the device path instance which does not match with Single
902 TempNewDevicePath
= NewDevicePath
;
903 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, Instance
);
904 if (TempNewDevicePath
!= NULL
) {
905 FreePool(TempNewDevicePath
);
909 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
910 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
913 return NewDevicePath
;
917 Function compares a device path data structure to that of all the nodes of a
918 second device path instance.
920 @param Multi A pointer to a multi-instance device path data
922 @param Single A pointer to a single-instance device path data
925 @retval TRUE If the Single device path is contained within Multi device path.
926 @retval FALSE The Single device path is not match within Multi device path.
931 BdsLibMatchDevicePaths (
932 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
933 IN EFI_DEVICE_PATH_PROTOCOL
*Single
936 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
937 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
940 if (Multi
== NULL
|| Single
== NULL
) {
945 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
948 // Search for the match of 'Single' in 'Multi'
950 while (DevicePathInst
!= NULL
) {
952 // If the single device path is found in multiple device paths,
955 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
956 FreePool (DevicePathInst
);
960 FreePool (DevicePathInst
);
961 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
968 This function prints a series of strings.
970 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
971 @param ... A variable argument list containing series of
972 strings, the last string must be NULL.
974 @retval EFI_SUCCESS Success print out the string using ConOut.
975 @retval EFI_STATUS Return the status of the ConOut->OutputString ().
980 BdsLibOutputStrings (
981 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*ConOut
,
989 Status
= EFI_SUCCESS
;
990 VA_START (Args
, ConOut
);
992 while (!EFI_ERROR (Status
)) {
994 // If String is NULL, then it's the end of the list
996 String
= VA_ARG (Args
, CHAR16
*);
997 if (String
== NULL
) {
1001 Status
= ConOut
->OutputString (ConOut
, String
);
1003 if (EFI_ERROR (Status
)) {
1013 // Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.
1014 // Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if
1015 // user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.
1020 Enable the setup browser reset reminder feature.
1021 This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.
1026 EnableResetReminderFeature (
1030 mFeaturerSwitch
= TRUE
;
1035 Disable the setup browser reset reminder feature.
1036 This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.
1041 DisableResetReminderFeature (
1045 mFeaturerSwitch
= FALSE
;
1050 Record the info that a reset is required.
1051 A module boolean variable is used to record whether a reset is required.
1056 EnableResetRequired (
1060 mResetRequired
= TRUE
;
1065 Record the info that no reset is required.
1066 A module boolean variable is used to record whether a reset is required.
1071 DisableResetRequired (
1075 mResetRequired
= FALSE
;
1080 Check whether platform policy enable the reset reminder feature. The default is enabled.
1085 IsResetReminderFeatureEnable (
1089 return mFeaturerSwitch
;
1094 Check if user changed any option setting which needs a system reset to be effective.
1103 return mResetRequired
;
1108 Check whether a reset is needed, and finish the reset reminder feature.
1109 If a reset is needed, Popup a menu to notice user, and finish the feature
1110 according to the user selection.
1115 SetupResetReminder (
1120 CHAR16
*StringBuffer1
;
1121 CHAR16
*StringBuffer2
;
1125 //check any reset required change is applied? if yes, reset system
1127 if (IsResetReminderFeatureEnable ()) {
1128 if (IsResetRequired ()) {
1130 StringBuffer1
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
1131 ASSERT (StringBuffer1
!= NULL
);
1132 StringBuffer2
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
1133 ASSERT (StringBuffer2
!= NULL
);
1137 L
"Configuration changed. Reset to apply it Now."
1142 L
"Press ENTER to reset"
1145 // Popup a menu to notice user
1148 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, StringBuffer1
, StringBuffer2
, NULL
);
1149 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
1151 FreePool (StringBuffer1
);
1152 FreePool (StringBuffer2
);
1154 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
1160 Get the headers (dos, image, optional header) from an image
1162 @param Device SimpleFileSystem device handle
1163 @param FileName File name for the image
1164 @param DosHeader Pointer to dos header
1165 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
1167 @retval EFI_SUCCESS Successfully get the machine type.
1168 @retval EFI_NOT_FOUND The file is not found.
1169 @retval EFI_LOAD_ERROR File is not a valid image file.
1174 BdsLibGetImageHeader (
1175 IN EFI_HANDLE Device
,
1176 IN CHAR16
*FileName
,
1177 OUT EFI_IMAGE_DOS_HEADER
*DosHeader
,
1178 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
1182 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
1183 EFI_FILE_HANDLE Root
;
1184 EFI_FILE_HANDLE ThisFile
;
1187 EFI_FILE_INFO
*Info
;
1192 // Handle the file system interface to the device
1194 Status
= gBS
->HandleProtocol (
1196 &gEfiSimpleFileSystemProtocolGuid
,
1199 if (EFI_ERROR (Status
)) {
1203 Status
= Volume
->OpenVolume (
1207 if (EFI_ERROR (Status
)) {
1211 ASSERT (Root
!= NULL
);
1212 Status
= Root
->Open (Root
, &ThisFile
, FileName
, EFI_FILE_MODE_READ
, 0);
1213 if (EFI_ERROR (Status
)) {
1216 ASSERT (ThisFile
!= NULL
);
1221 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
1224 Status
= gBS
->AllocatePool (EfiBootServicesData
, BufferSize
, (VOID
**) &Info
);
1225 if (EFI_ERROR (Status
)) {
1228 Status
= ThisFile
->GetInfo (
1234 if (!EFI_ERROR (Status
)) {
1237 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1244 FileSize
= Info
->FileSize
;
1250 BufferSize
= sizeof (EFI_IMAGE_DOS_HEADER
);
1251 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, DosHeader
);
1252 if (EFI_ERROR (Status
) ||
1253 BufferSize
< sizeof (EFI_IMAGE_DOS_HEADER
) ||
1254 FileSize
<= DosHeader
->e_lfanew
||
1255 DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
1256 Status
= EFI_LOAD_ERROR
;
1261 // Move to PE signature
1263 Status
= ThisFile
->SetPosition (ThisFile
, DosHeader
->e_lfanew
);
1264 if (EFI_ERROR (Status
)) {
1265 Status
= EFI_LOAD_ERROR
;
1270 // Read and check PE signature
1272 BufferSize
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
1273 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, Hdr
.Pe32
);
1274 if (EFI_ERROR (Status
) ||
1275 BufferSize
< sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
) ||
1276 Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1277 Status
= EFI_LOAD_ERROR
;
1282 // Check PE32 or PE32+ magic
1284 if (Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
&&
1285 Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
1286 Status
= EFI_LOAD_ERROR
;
1291 if (ThisFile
!= NULL
) {
1292 ThisFile
->Close (ThisFile
);
1301 This routine adjust the memory information for different memory type and
1302 save them into the variables for next boot.
1305 BdsSetMemoryTypeInformationVariable (
1310 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
1311 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
1318 EFI_HOB_GUID_TYPE
*GuidHob
;
1319 BOOLEAN MemoryTypeInformationModified
;
1320 BOOLEAN MemoryTypeInformationVariableExists
;
1321 EFI_BOOT_MODE BootMode
;
1323 MemoryTypeInformationModified
= FALSE
;
1324 MemoryTypeInformationVariableExists
= FALSE
;
1327 BootMode
= GetBootModeHob ();
1329 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.
1331 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
1336 // Only check the the Memory Type Information variable in the boot mode
1337 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type
1338 // Information is not valid in this boot mode.
1340 if (BootMode
!= BOOT_WITH_DEFAULT_SETTINGS
) {
1342 Status
= gRT
->GetVariable (
1343 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
1344 &gEfiMemoryTypeInformationGuid
,
1349 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1350 MemoryTypeInformationVariableExists
= TRUE
;
1355 // Retrieve the current memory usage statistics. If they are not found, then
1356 // no adjustments can be made to the Memory Type Information variable.
1358 Status
= EfiGetSystemConfigurationTable (
1359 &gEfiMemoryTypeInformationGuid
,
1360 (VOID
**) &CurrentMemoryTypeInformation
1362 if (EFI_ERROR (Status
) || CurrentMemoryTypeInformation
== NULL
) {
1367 // Get the Memory Type Information settings from Hob if they exist,
1368 // PEI is responsible for getting them from variable and build a Hob to save them.
1369 // If the previous Memory Type Information is not available, then set defaults
1371 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
1372 if (GuidHob
== NULL
) {
1374 // If Platform has not built Memory Type Info into the Hob, just return.
1378 PreviousMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
1379 VariableSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
1382 // Use a heuristic to adjust the Memory Type Information for the next boot
1384 DEBUG ((EFI_D_INFO
, "Memory Previous Current Next \n"));
1385 DEBUG ((EFI_D_INFO
, " Type Pages Pages Pages \n"));
1386 DEBUG ((EFI_D_INFO
, "====== ======== ======== ========\n"));
1388 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
1390 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
1391 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
1395 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
1400 // Previous is the number of pages pre-allocated
1401 // Current is the number of pages actually needed
1403 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
1404 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
1408 // Inconsistent Memory Reserved across bootings may lead to S4 fail
1409 // Write next varible to 125% * current when the pre-allocated memory is:
1410 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING
1411 // 2. Less than the needed memory
1413 if ((Current
+ (Current
>> 1)) < Previous
) {
1414 if (BootMode
== BOOT_WITH_DEFAULT_SETTINGS
) {
1415 Next
= Current
+ (Current
>> 2);
1417 } else if (Current
> Previous
) {
1418 Next
= Current
+ (Current
>> 2);
1420 if (Next
> 0 && Next
< 4) {
1424 if (Next
!= Previous
) {
1425 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
1426 MemoryTypeInformationModified
= TRUE
;
1429 DEBUG ((EFI_D_INFO
, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation
[Index
].Type
, Previous
, Current
, Next
));
1433 // If any changes were made to the Memory Type Information settings, then set the new variable value;
1434 // Or create the variable in first boot.
1436 if (MemoryTypeInformationModified
|| !MemoryTypeInformationVariableExists
) {
1437 Status
= SetVariableAndReportStatusCodeOnError (
1438 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
1439 &gEfiMemoryTypeInformationGuid
,
1440 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1442 PreviousMemoryTypeInformation
1445 if (!EFI_ERROR (Status
)) {
1447 // If the Memory Type Information settings have been modified, then reset the platform
1448 // so the new Memory Type Information setting will be used to guarantee that an S4
1449 // entry/resume cycle will not fail.
1451 if (MemoryTypeInformationModified
&& PcdGetBool (PcdResetOnMemoryTypeInformationChange
)) {
1452 DEBUG ((EFI_D_INFO
, "Memory Type Information settings change. Warm Reset!!!\n"));
1453 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
1456 DEBUG ((EFI_D_ERROR
, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));
1462 This routine is kept for backward compatibility.
1466 BdsLibSaveMemoryTypeInformation (
1474 Identify a user and, if authenticated, returns the current user profile handle.
1476 @param[out] User Point to user profile handle.
1478 @retval EFI_SUCCESS User is successfully identified, or user identification
1480 @retval EFI_ACCESS_DENIED User is not successfully identified
1485 BdsLibUserIdentify (
1486 OUT EFI_USER_PROFILE_HANDLE
*User
1490 EFI_USER_MANAGER_PROTOCOL
*Manager
;
1492 Status
= gBS
->LocateProtocol (
1493 &gEfiUserManagerProtocolGuid
,
1497 if (EFI_ERROR (Status
)) {
1501 return Manager
->Identify (Manager
, User
);
1505 Set the variable and report the error through status code upon failure.
1507 @param VariableName A Null-terminated string that is the name of the vendor's variable.
1508 Each VariableName is unique for each VendorGuid. VariableName must
1509 contain 1 or more characters. If VariableName is an empty string,
1510 then EFI_INVALID_PARAMETER is returned.
1511 @param VendorGuid A unique identifier for the vendor.
1512 @param Attributes Attributes bitmask to set for the variable.
1513 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
1514 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
1515 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
1516 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
1517 set, then a SetVariable() call with a DataSize of zero will not cause any change to
1518 the variable value (the timestamp associated with the variable may be updated however
1519 even if no new data value is provided,see the description of the
1520 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
1521 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
1522 @param Data The contents for the variable.
1524 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
1525 defined by the Attributes.
1526 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
1527 DataSize exceeds the maximum allowed.
1528 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
1529 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
1530 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
1531 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
1532 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
1533 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1534 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
1535 does NOT pass the validation check carried out by the firmware.
1537 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
1540 SetVariableAndReportStatusCodeOnError (
1541 IN CHAR16
*VariableName
,
1542 IN EFI_GUID
*VendorGuid
,
1543 IN UINT32 Attributes
,
1549 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
1552 Status
= gRT
->SetVariable (
1559 if (EFI_ERROR (Status
)) {
1560 NameSize
= StrSize (VariableName
);
1561 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
1562 if (SetVariableStatus
!= NULL
) {
1563 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
1564 SetVariableStatus
->NameSize
= NameSize
;
1565 SetVariableStatus
->DataSize
= DataSize
;
1566 SetVariableStatus
->SetStatus
= Status
;
1567 SetVariableStatus
->Attributes
= Attributes
;
1568 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
1569 if ((Data
!= NULL
) && (DataSize
!= 0)) {
1570 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
1573 REPORT_STATUS_CODE_EX (
1575 PcdGet32 (PcdErrorCodeSetVariable
),
1578 &gEdkiiStatusCodeDataTypeVariableGuid
,
1580 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
1583 FreePool (SetVariableStatus
);