2 Misc BDS library function
4 Copyright (c) 2004 - 2014, 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
) {
174 return ((UINT16
) Index
);
179 This function will register the new boot#### or driver#### option base on
180 the VariableName. The new registered boot#### or driver#### will be linked
181 to BdsOptionList and also update to the VariableName. After the boot#### or
182 driver#### updated, the BootOrder or DriverOrder will also be updated.
184 @param BdsOptionList The header of the boot#### or driver#### link list
185 @param DevicePath The device path which the boot#### or driver####
187 @param String The description of the boot#### or driver####
188 @param VariableName Indicate if the boot#### or driver#### option
190 @retval EFI_SUCCESS The boot#### or driver#### have been success
192 @retval EFI_STATUS Return the status of gRT->SetVariable ().
197 BdsLibRegisterNewOption (
198 IN LIST_ENTRY
*BdsOptionList
,
199 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
201 IN CHAR16
*VariableName
206 UINT16 RegisterOptionNumber
;
207 UINT16
*TempOptionPtr
;
208 UINTN TempOptionSize
;
209 UINT16
*OptionOrderPtr
;
213 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
215 CHAR16 OptionName
[10];
216 BOOLEAN UpdateDescription
;
217 UINT16 BootOrderEntry
;
224 OptionDevicePath
= NULL
;
226 OptionOrderPtr
= NULL
;
227 UpdateDescription
= FALSE
;
228 Status
= EFI_SUCCESS
;
229 ZeroMem (OptionName
, sizeof (OptionName
));
232 TempOptionPtr
= BdsLibGetVariableAndSize (
234 &gEfiGlobalVariableGuid
,
238 // Compare with current option variable if the previous option is set in global variable.
240 for (Index
= 0; Index
< TempOptionSize
/ sizeof (UINT16
); Index
++) {
242 // TempOptionPtr must not be NULL if we have non-zero TempOptionSize.
244 ASSERT (TempOptionPtr
!= NULL
);
246 if (*VariableName
== 'B') {
247 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", TempOptionPtr
[Index
]);
249 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", TempOptionPtr
[Index
]);
252 OptionPtr
= BdsLibGetVariableAndSize (
254 &gEfiGlobalVariableGuid
,
257 if (OptionPtr
== NULL
) {
262 // Validate the variable.
264 if (!ValidateOption(OptionPtr
, OptionSize
)) {
269 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
270 Description
= (CHAR16
*) TempPtr
;
271 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
272 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
275 // Notes: the description may will change base on the GetStringToken
277 if (CompareMem (OptionDevicePath
, DevicePath
, GetDevicePathSize (OptionDevicePath
)) == 0) {
278 if (CompareMem (Description
, String
, StrSize (Description
)) == 0) {
280 // Got the option, so just return
282 FreePool (OptionPtr
);
283 FreePool (TempOptionPtr
);
287 // Option description changed, need update.
289 UpdateDescription
= TRUE
;
290 FreePool (OptionPtr
);
295 FreePool (OptionPtr
);
298 OptionSize
= sizeof (UINT32
) + sizeof (UINT16
) + StrSize (String
);
299 OptionSize
+= GetDevicePathSize (DevicePath
);
300 OptionPtr
= AllocateZeroPool (OptionSize
);
301 ASSERT (OptionPtr
!= NULL
);
304 *(UINT32
*) TempPtr
= LOAD_OPTION_ACTIVE
;
305 TempPtr
+= sizeof (UINT32
);
306 *(UINT16
*) TempPtr
= (UINT16
) GetDevicePathSize (DevicePath
);
307 TempPtr
+= sizeof (UINT16
);
308 CopyMem (TempPtr
, String
, StrSize (String
));
309 TempPtr
+= StrSize (String
);
310 CopyMem (TempPtr
, DevicePath
, GetDevicePathSize (DevicePath
));
312 if (UpdateDescription
) {
314 // The number in option#### to be updated.
315 // In this case, we must have non-NULL TempOptionPtr.
317 ASSERT (TempOptionPtr
!= NULL
);
318 RegisterOptionNumber
= TempOptionPtr
[Index
];
321 // The new option#### number
323 RegisterOptionNumber
= BdsLibGetFreeOptionNumber(VariableName
);
326 if (*VariableName
== 'B') {
327 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", RegisterOptionNumber
);
329 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", RegisterOptionNumber
);
332 Status
= gRT
->SetVariable (
334 &gEfiGlobalVariableGuid
,
335 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
340 // Return if only need to update a changed description or fail to set option.
342 if (EFI_ERROR (Status
) || UpdateDescription
) {
343 FreePool (OptionPtr
);
344 if (TempOptionPtr
!= NULL
) {
345 FreePool (TempOptionPtr
);
350 FreePool (OptionPtr
);
353 // Update the option order variable
357 // If no option order
359 if (TempOptionSize
== 0) {
361 Status
= gRT
->SetVariable (
363 &gEfiGlobalVariableGuid
,
364 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
368 if (TempOptionPtr
!= NULL
) {
369 FreePool (TempOptionPtr
);
375 // TempOptionPtr must not be NULL if TempOptionSize is not zero.
377 ASSERT (TempOptionPtr
!= NULL
);
379 // Append the new option number to the original option order
381 OrderItemNum
= (TempOptionSize
/ sizeof (UINT16
)) + 1 ;
382 OptionOrderPtr
= AllocateZeroPool ( OrderItemNum
* sizeof (UINT16
));
383 ASSERT (OptionOrderPtr
!= NULL
);
384 CopyMem (OptionOrderPtr
, TempOptionPtr
, (OrderItemNum
- 1) * sizeof (UINT16
));
386 OptionOrderPtr
[Index
] = RegisterOptionNumber
;
388 Status
= gRT
->SetVariable (
390 &gEfiGlobalVariableGuid
,
391 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
392 OrderItemNum
* sizeof (UINT16
),
395 FreePool (TempOptionPtr
);
396 FreePool (OptionOrderPtr
);
402 Returns the size of a device path in bytes.
404 This function returns the size, in bytes, of the device path data structure
405 specified by DevicePath including the end of device path node. If DevicePath
406 is NULL, then 0 is returned. If the length of the device path is bigger than
407 MaxSize, also return 0 to indicate this is an invalidate device path.
409 @param DevicePath A pointer to a device path data structure.
410 @param MaxSize Max valid device path size. If big than this size,
413 @retval 0 An invalid device path.
414 @retval Others The size of a device path in bytes.
418 GetDevicePathSizeEx (
419 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
426 if (DevicePath
== NULL
) {
431 // Search for the end of the device path structure
434 while (!IsDevicePathEnd (DevicePath
)) {
435 NodeSize
= DevicePathNodeLength (DevicePath
);
436 if (NodeSize
< END_DEVICE_PATH_LENGTH
) {
440 if (Size
> MaxSize
) {
443 DevicePath
= NextDevicePathNode (DevicePath
);
445 Size
+= DevicePathNodeLength (DevicePath
);
446 if (Size
> MaxSize
) {
454 Returns the length of a Null-terminated Unicode string. If the length is
455 bigger than MaxStringLen, return length 0 to indicate that this is an
458 This function returns the byte length of Unicode characters in the Null-terminated
459 Unicode string specified by String.
461 If String is NULL, then ASSERT().
462 If String is not aligned on a 16-bit boundary, then ASSERT().
464 @param String A pointer to a Null-terminated Unicode string.
465 @param MaxStringLen Max string len in this string.
467 @retval 0 An invalid string.
468 @retval Others The length of String.
473 IN CONST CHAR16
*String
,
474 IN UINTN MaxStringLen
479 ASSERT (String
!= NULL
&& MaxStringLen
!= 0);
480 ASSERT (((UINTN
) String
& BIT0
) == 0);
482 for (Length
= 0; *String
!= L
'\0' && MaxStringLen
!= Length
; String
++, Length
+=2);
484 if (*String
!= L
'\0' && MaxStringLen
== Length
) {
492 Validate the EFI Boot#### variable (VendorGuid/Name)
494 @param Variable Boot#### variable data.
495 @param VariableSize Returns the size of the EFI variable that was read
497 @retval TRUE The variable data is correct.
498 @retval FALSE The variable data is corrupted.
509 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
512 if (VariableSize
<= sizeof (UINT16
) + sizeof (UINT32
)) {
517 // Skip the option attribute
520 TempPtr
+= sizeof (UINT32
);
523 // Get the option's device path size
525 FilePathSize
= *(UINT16
*) TempPtr
;
526 TempPtr
+= sizeof (UINT16
);
529 // Get the option's description string size
531 TempSize
= StrSizeEx ((CHAR16
*) TempPtr
, VariableSize
- sizeof (UINT16
) - sizeof (UINT32
));
535 // Get the option's device path
537 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
538 TempPtr
+= FilePathSize
;
541 // Validation boot option variable.
543 if ((FilePathSize
== 0) || (TempSize
== 0)) {
547 if (TempSize
+ FilePathSize
+ sizeof (UINT16
) + sizeof (UINT32
) > VariableSize
) {
551 return (BOOLEAN
) (GetDevicePathSizeEx (DevicePath
, FilePathSize
) != 0);
555 Convert a single character to number.
556 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'
558 @param Char The input char which need to change to a hex number.
566 if ((Char
>= L
'0') && (Char
<= L
'9')) {
567 return (UINTN
) (Char
- L
'0');
570 if ((Char
>= L
'A') && (Char
<= L
'F')) {
571 return (UINTN
) (Char
- L
'A' + 0xA);
579 Build the boot#### or driver#### option from the VariableName, the
580 build boot#### or driver#### will also be linked to BdsCommonOptionList.
582 @param BdsCommonOptionList The header of the boot#### or driver#### option
584 @param VariableName EFI Variable name indicate if it is boot#### or
587 @retval BDS_COMMON_OPTION Get the option just been created
588 @retval NULL Failed to get the new option
593 BdsLibVariableToOption (
594 IN OUT LIST_ENTRY
*BdsCommonOptionList
,
595 IN CHAR16
*VariableName
603 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
604 BDS_COMMON_OPTION
*Option
;
606 UINT32 LoadOptionsSize
;
611 // Read the variable. We will never free this data.
613 Variable
= BdsLibGetVariableAndSize (
615 &gEfiGlobalVariableGuid
,
618 if (Variable
== NULL
) {
623 // Validate Boot#### variable data.
625 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
) {
677 Option
->Signature
= BDS_LOAD_OPTION_SIGNATURE
;
678 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
679 ASSERT(Option
->DevicePath
!= NULL
);
680 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
682 Option
->Attribute
= Attribute
;
683 Option
->Description
= AllocateZeroPool (StrSize (Description
));
684 ASSERT(Option
->Description
!= NULL
);
685 CopyMem (Option
->Description
, Description
, StrSize (Description
));
687 Option
->LoadOptions
= AllocateZeroPool (LoadOptionsSize
);
688 ASSERT(Option
->LoadOptions
!= NULL
);
689 CopyMem (Option
->LoadOptions
, LoadOptions
, LoadOptionsSize
);
690 Option
->LoadOptionsSize
= LoadOptionsSize
;
693 // Get the value from VariableName Unicode string
694 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this
695 // Unicode stream to ASCII without any loss in meaning.
697 if (*VariableName
== 'B') {
698 NumOff
= (UINT8
) (sizeof (L
"Boot") / sizeof (CHAR16
) - 1);
699 Option
->BootCurrent
= (UINT16
) (CharToUint (VariableName
[NumOff
+0]) * 0x1000)
700 + (UINT16
) (CharToUint (VariableName
[NumOff
+1]) * 0x100)
701 + (UINT16
) (CharToUint (VariableName
[NumOff
+2]) * 0x10)
702 + (UINT16
) (CharToUint (VariableName
[NumOff
+3]) * 0x1);
704 InsertTailList (BdsCommonOptionList
, &Option
->Link
);
710 Process BootOrder, or DriverOrder variables, by calling
711 BdsLibVariableToOption () for each UINT16 in the variables.
713 @param BdsCommonOptionList The header of the option list base on variable
715 @param VariableName EFI Variable name indicate the BootOrder or
718 @retval EFI_SUCCESS Success create the boot option or driver option
720 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list
725 BdsLibBuildOptionFromVar (
726 IN LIST_ENTRY
*BdsCommonOptionList
,
727 IN CHAR16
*VariableName
731 UINTN OptionOrderSize
;
733 BDS_COMMON_OPTION
*Option
;
734 CHAR16 OptionName
[20];
737 // Zero Buffer in order to get all BOOT#### variables
739 ZeroMem (OptionName
, sizeof (OptionName
));
742 // Read the BootOrder, or DriverOrder variable.
744 OptionOrder
= BdsLibGetVariableAndSize (
746 &gEfiGlobalVariableGuid
,
749 if (OptionOrder
== NULL
) {
750 return EFI_OUT_OF_RESOURCES
;
753 for (Index
= 0; Index
< OptionOrderSize
/ sizeof (UINT16
); Index
++) {
754 if (*VariableName
== 'B') {
755 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionOrder
[Index
]);
757 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", OptionOrder
[Index
]);
760 Option
= BdsLibVariableToOption (BdsCommonOptionList
, OptionName
);
761 if (Option
!= NULL
) {
762 Option
->BootCurrent
= OptionOrder
[Index
];
766 FreePool (OptionOrder
);
772 Get boot mode by looking up configuration table and parsing HOB list
774 @param BootMode Boot mode from PEI handoff HOB.
776 @retval EFI_SUCCESS Successfully get boot mode
782 OUT EFI_BOOT_MODE
*BootMode
785 *BootMode
= GetBootModeHob ();
791 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
792 buffer, and the size of the buffer. If failure return NULL.
794 @param Name String part of EFI variable name
795 @param VendorGuid GUID part of EFI variable name
796 @param VariableSize Returns the size of the EFI variable that was read
798 @return Dynamically allocated memory that contains a copy of the EFI variable
799 Caller is responsible freeing the buffer.
800 @retval NULL Variable was not read
805 BdsLibGetVariableAndSize (
807 IN EFI_GUID
*VendorGuid
,
808 OUT UINTN
*VariableSize
818 // Pass in a zero size buffer to find the required buffer size.
821 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
822 if (Status
== EFI_BUFFER_TOO_SMALL
) {
824 // Allocate the buffer to return
826 Buffer
= AllocateZeroPool (BufferSize
);
827 if (Buffer
== NULL
) {
832 // Read variable into the allocated buffer.
834 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
835 if (EFI_ERROR (Status
)) {
842 ASSERT (((Buffer
== NULL
) && (BufferSize
== 0)) ||
843 ((Buffer
!= NULL
) && (BufferSize
!= 0))
845 *VariableSize
= BufferSize
;
850 Delete the instance in Multi which matches partly with Single instance
852 @param Multi A pointer to a multi-instance device path data
854 @param Single A pointer to a single-instance device path data
857 @return This function will remove the device path instances in Multi which partly
858 match with the Single, and return the result device path. If there is no
859 remaining device path as a result, this function will return NULL.
862 EFI_DEVICE_PATH_PROTOCOL
*
864 BdsLibDelPartMatchInstance (
865 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
866 IN EFI_DEVICE_PATH_PROTOCOL
*Single
869 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
870 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
871 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
876 NewDevicePath
= NULL
;
877 TempNewDevicePath
= NULL
;
879 if (Multi
== NULL
|| Single
== NULL
) {
883 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
884 SingleDpSize
= GetDevicePathSize (Single
) - END_DEVICE_PATH_LENGTH
;
885 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
887 while (Instance
!= NULL
) {
889 Size
= (SingleDpSize
< InstanceSize
) ? SingleDpSize
: InstanceSize
;
891 if ((CompareMem (Instance
, Single
, Size
) != 0)) {
893 // Append the device path instance which does not match with Single
895 TempNewDevicePath
= NewDevicePath
;
896 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, Instance
);
897 if (TempNewDevicePath
!= NULL
) {
898 FreePool(TempNewDevicePath
);
902 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
903 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
906 return NewDevicePath
;
910 Function compares a device path data structure to that of all the nodes of a
911 second device path instance.
913 @param Multi A pointer to a multi-instance device path data
915 @param Single A pointer to a single-instance device path data
918 @retval TRUE If the Single device path is contained within Multi device path.
919 @retval FALSE The Single device path is not match within Multi device path.
924 BdsLibMatchDevicePaths (
925 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
926 IN EFI_DEVICE_PATH_PROTOCOL
*Single
929 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
930 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
933 if (Multi
== NULL
|| Single
== NULL
) {
938 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
941 // Search for the match of 'Single' in 'Multi'
943 while (DevicePathInst
!= NULL
) {
945 // If the single device path is found in multiple device paths,
948 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
949 FreePool (DevicePathInst
);
953 FreePool (DevicePathInst
);
954 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
961 This function prints a series of strings.
963 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
964 @param ... A variable argument list containing series of
965 strings, the last string must be NULL.
967 @retval EFI_SUCCESS Success print out the string using ConOut.
968 @retval EFI_STATUS Return the status of the ConOut->OutputString ().
973 BdsLibOutputStrings (
974 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*ConOut
,
982 Status
= EFI_SUCCESS
;
983 VA_START (Args
, ConOut
);
985 while (!EFI_ERROR (Status
)) {
987 // If String is NULL, then it's the end of the list
989 String
= VA_ARG (Args
, CHAR16
*);
990 if (String
== NULL
) {
994 Status
= ConOut
->OutputString (ConOut
, String
);
996 if (EFI_ERROR (Status
)) {
1006 // Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.
1007 // Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if
1008 // user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.
1013 Enable the setup browser reset reminder feature.
1014 This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.
1019 EnableResetReminderFeature (
1023 mFeaturerSwitch
= TRUE
;
1028 Disable the setup browser reset reminder feature.
1029 This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.
1034 DisableResetReminderFeature (
1038 mFeaturerSwitch
= FALSE
;
1043 Record the info that a reset is required.
1044 A module boolean variable is used to record whether a reset is required.
1049 EnableResetRequired (
1053 mResetRequired
= TRUE
;
1058 Record the info that no reset is required.
1059 A module boolean variable is used to record whether a reset is required.
1064 DisableResetRequired (
1068 mResetRequired
= FALSE
;
1073 Check whether platform policy enable the reset reminder feature. The default is enabled.
1078 IsResetReminderFeatureEnable (
1082 return mFeaturerSwitch
;
1087 Check if user changed any option setting which needs a system reset to be effective.
1096 return mResetRequired
;
1101 Check whether a reset is needed, and finish the reset reminder feature.
1102 If a reset is needed, Popup a menu to notice user, and finish the feature
1103 according to the user selection.
1108 SetupResetReminder (
1113 CHAR16
*StringBuffer1
;
1114 CHAR16
*StringBuffer2
;
1118 //check any reset required change is applied? if yes, reset system
1120 if (IsResetReminderFeatureEnable ()) {
1121 if (IsResetRequired ()) {
1123 StringBuffer1
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
1124 ASSERT (StringBuffer1
!= NULL
);
1125 StringBuffer2
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
1126 ASSERT (StringBuffer2
!= NULL
);
1127 StrCpy (StringBuffer1
, L
"Configuration changed. Reset to apply it Now.");
1128 StrCpy (StringBuffer2
, L
"Press ENTER to reset");
1130 // Popup a menu to notice user
1133 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, StringBuffer1
, StringBuffer2
, NULL
);
1134 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
1136 FreePool (StringBuffer1
);
1137 FreePool (StringBuffer2
);
1139 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
1145 Get the headers (dos, image, optional header) from an image
1147 @param Device SimpleFileSystem device handle
1148 @param FileName File name for the image
1149 @param DosHeader Pointer to dos header
1150 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
1152 @retval EFI_SUCCESS Successfully get the machine type.
1153 @retval EFI_NOT_FOUND The file is not found.
1154 @retval EFI_LOAD_ERROR File is not a valid image file.
1159 BdsLibGetImageHeader (
1160 IN EFI_HANDLE Device
,
1161 IN CHAR16
*FileName
,
1162 OUT EFI_IMAGE_DOS_HEADER
*DosHeader
,
1163 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
1167 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
1168 EFI_FILE_HANDLE Root
;
1169 EFI_FILE_HANDLE ThisFile
;
1172 EFI_FILE_INFO
*Info
;
1177 // Handle the file system interface to the device
1179 Status
= gBS
->HandleProtocol (
1181 &gEfiSimpleFileSystemProtocolGuid
,
1184 if (EFI_ERROR (Status
)) {
1188 Status
= Volume
->OpenVolume (
1192 if (EFI_ERROR (Status
)) {
1196 ASSERT (Root
!= NULL
);
1197 Status
= Root
->Open (Root
, &ThisFile
, FileName
, EFI_FILE_MODE_READ
, 0);
1198 if (EFI_ERROR (Status
)) {
1201 ASSERT (ThisFile
!= NULL
);
1206 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
1209 Status
= gBS
->AllocatePool (EfiBootServicesData
, BufferSize
, (VOID
**) &Info
);
1210 if (EFI_ERROR (Status
)) {
1213 Status
= ThisFile
->GetInfo (
1219 if (!EFI_ERROR (Status
)) {
1222 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1229 FileSize
= Info
->FileSize
;
1235 BufferSize
= sizeof (EFI_IMAGE_DOS_HEADER
);
1236 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, DosHeader
);
1237 if (EFI_ERROR (Status
) ||
1238 BufferSize
< sizeof (EFI_IMAGE_DOS_HEADER
) ||
1239 FileSize
<= DosHeader
->e_lfanew
||
1240 DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
1241 Status
= EFI_LOAD_ERROR
;
1246 // Move to PE signature
1248 Status
= ThisFile
->SetPosition (ThisFile
, DosHeader
->e_lfanew
);
1249 if (EFI_ERROR (Status
)) {
1250 Status
= EFI_LOAD_ERROR
;
1255 // Read and check PE signature
1257 BufferSize
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
1258 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, Hdr
.Pe32
);
1259 if (EFI_ERROR (Status
) ||
1260 BufferSize
< sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
) ||
1261 Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1262 Status
= EFI_LOAD_ERROR
;
1267 // Check PE32 or PE32+ magic
1269 if (Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
&&
1270 Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
1271 Status
= EFI_LOAD_ERROR
;
1276 if (ThisFile
!= NULL
) {
1277 ThisFile
->Close (ThisFile
);
1286 This routine adjust the memory information for different memory type and
1287 save them into the variables for next boot.
1290 BdsSetMemoryTypeInformationVariable (
1295 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
1296 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
1303 EFI_HOB_GUID_TYPE
*GuidHob
;
1304 BOOLEAN MemoryTypeInformationModified
;
1305 BOOLEAN MemoryTypeInformationVariableExists
;
1306 EFI_BOOT_MODE BootMode
;
1308 MemoryTypeInformationModified
= FALSE
;
1309 MemoryTypeInformationVariableExists
= FALSE
;
1312 BootMode
= GetBootModeHob ();
1314 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.
1316 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
1321 // Only check the the Memory Type Information variable in the boot mode
1322 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type
1323 // Information is not valid in this boot mode.
1325 if (BootMode
!= BOOT_WITH_DEFAULT_SETTINGS
) {
1327 Status
= gRT
->GetVariable (
1328 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
1329 &gEfiMemoryTypeInformationGuid
,
1334 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1335 MemoryTypeInformationVariableExists
= TRUE
;
1340 // Retrieve the current memory usage statistics. If they are not found, then
1341 // no adjustments can be made to the Memory Type Information variable.
1343 Status
= EfiGetSystemConfigurationTable (
1344 &gEfiMemoryTypeInformationGuid
,
1345 (VOID
**) &CurrentMemoryTypeInformation
1347 if (EFI_ERROR (Status
) || CurrentMemoryTypeInformation
== NULL
) {
1352 // Get the Memory Type Information settings from Hob if they exist,
1353 // PEI is responsible for getting them from variable and build a Hob to save them.
1354 // If the previous Memory Type Information is not available, then set defaults
1356 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
1357 if (GuidHob
== NULL
) {
1359 // If Platform has not built Memory Type Info into the Hob, just return.
1363 PreviousMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
1364 VariableSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
1367 // Use a heuristic to adjust the Memory Type Information for the next boot
1369 DEBUG ((EFI_D_INFO
, "Memory Previous Current Next \n"));
1370 DEBUG ((EFI_D_INFO
, " Type Pages Pages Pages \n"));
1371 DEBUG ((EFI_D_INFO
, "====== ======== ======== ========\n"));
1373 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
1375 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
1376 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
1380 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
1385 // Previous is the number of pages pre-allocated
1386 // Current is the number of pages actually needed
1388 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
1389 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
1393 // Inconsistent Memory Reserved across bootings may lead to S4 fail
1394 // Write next varible to 125% * current when the pre-allocated memory is:
1395 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING
1396 // 2. Less than the needed memory
1398 if ((Current
+ (Current
>> 1)) < Previous
) {
1399 if (BootMode
== BOOT_WITH_DEFAULT_SETTINGS
) {
1400 Next
= Current
+ (Current
>> 2);
1402 } else if (Current
> Previous
) {
1403 Next
= Current
+ (Current
>> 2);
1405 if (Next
> 0 && Next
< 4) {
1409 if (Next
!= Previous
) {
1410 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
1411 MemoryTypeInformationModified
= TRUE
;
1414 DEBUG ((EFI_D_INFO
, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation
[Index
].Type
, Previous
, Current
, Next
));
1418 // If any changes were made to the Memory Type Information settings, then set the new variable value;
1419 // Or create the variable in first boot.
1421 if (MemoryTypeInformationModified
|| !MemoryTypeInformationVariableExists
) {
1422 Status
= SetVariableAndReportStatusCodeOnError (
1423 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
1424 &gEfiMemoryTypeInformationGuid
,
1425 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1427 PreviousMemoryTypeInformation
1430 if (!EFI_ERROR (Status
)) {
1432 // If the Memory Type Information settings have been modified, then reset the platform
1433 // so the new Memory Type Information setting will be used to guarantee that an S4
1434 // entry/resume cycle will not fail.
1436 if (MemoryTypeInformationModified
&& PcdGetBool (PcdResetOnMemoryTypeInformationChange
)) {
1437 DEBUG ((EFI_D_INFO
, "Memory Type Information settings change. Warm Reset!!!\n"));
1438 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
1441 DEBUG ((EFI_D_ERROR
, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));
1447 This routine is kept for backward compatibility.
1451 BdsLibSaveMemoryTypeInformation (
1459 Identify a user and, if authenticated, returns the current user profile handle.
1461 @param[out] User Point to user profile handle.
1463 @retval EFI_SUCCESS User is successfully identified, or user identification
1465 @retval EFI_ACCESS_DENIED User is not successfully identified
1470 BdsLibUserIdentify (
1471 OUT EFI_USER_PROFILE_HANDLE
*User
1475 EFI_USER_MANAGER_PROTOCOL
*Manager
;
1477 Status
= gBS
->LocateProtocol (
1478 &gEfiUserManagerProtocolGuid
,
1482 if (EFI_ERROR (Status
)) {
1486 return Manager
->Identify (Manager
, User
);
1490 Set the variable and report the error through status code upon failure.
1492 @param VariableName A Null-terminated string that is the name of the vendor's variable.
1493 Each VariableName is unique for each VendorGuid. VariableName must
1494 contain 1 or more characters. If VariableName is an empty string,
1495 then EFI_INVALID_PARAMETER is returned.
1496 @param VendorGuid A unique identifier for the vendor.
1497 @param Attributes Attributes bitmask to set for the variable.
1498 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
1499 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
1500 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
1501 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
1502 set, then a SetVariable() call with a DataSize of zero will not cause any change to
1503 the variable value (the timestamp associated with the variable may be updated however
1504 even if no new data value is provided,see the description of the
1505 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
1506 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
1507 @param Data The contents for the variable.
1509 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
1510 defined by the Attributes.
1511 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
1512 DataSize exceeds the maximum allowed.
1513 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
1514 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
1515 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
1516 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
1517 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
1518 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1519 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
1520 does NOT pass the validation check carried out by the firmware.
1522 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
1525 SetVariableAndReportStatusCodeOnError (
1526 IN CHAR16
*VariableName
,
1527 IN EFI_GUID
*VendorGuid
,
1528 IN UINT32 Attributes
,
1534 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
1537 Status
= gRT
->SetVariable (
1544 if (EFI_ERROR (Status
)) {
1545 NameSize
= StrSize (VariableName
);
1546 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
1547 if (SetVariableStatus
!= NULL
) {
1548 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
1549 SetVariableStatus
->NameSize
= NameSize
;
1550 SetVariableStatus
->DataSize
= DataSize
;
1551 SetVariableStatus
->SetStatus
= Status
;
1552 SetVariableStatus
->Attributes
= Attributes
;
1553 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
1554 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
1556 REPORT_STATUS_CODE_EX (
1558 PcdGet32 (PcdErrorCodeSetVariable
),
1561 &gEdkiiStatusCodeDataTypeVariableGuid
,
1563 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
1566 FreePool (SetVariableStatus
);