2 Misc library functions.
4 Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "InternalBm.h"
19 Delete the instance in Multi which matches partly with Single instance
21 @param Multi A pointer to a multi-instance device path data
23 @param Single A pointer to a single-instance device path data
26 @return This function will remove the device path instances in Multi which partly
27 match with the Single, and return the result device path. If there is no
28 remaining device path as a result, this function will return NULL.
31 EFI_DEVICE_PATH_PROTOCOL
*
32 BmDelPartMatchInstance (
33 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
34 IN EFI_DEVICE_PATH_PROTOCOL
*Single
37 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
38 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
39 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
44 TempNewDevicePath
= NULL
;
46 if (Multi
== NULL
|| Single
== NULL
) {
50 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
51 SingleDpSize
= GetDevicePathSize (Single
) - END_DEVICE_PATH_LENGTH
;
52 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
54 while (Instance
!= NULL
) {
56 if (CompareMem (Instance
, Single
, MIN (SingleDpSize
, InstanceSize
)) != 0) {
58 // Append the device path instance which does not match with Single
60 TempNewDevicePath
= NewDevicePath
;
61 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, Instance
);
62 if (TempNewDevicePath
!= NULL
) {
63 FreePool(TempNewDevicePath
);
67 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
68 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
75 Function compares a device path data structure to that of all the nodes of a
76 second device path instance.
78 @param Multi A pointer to a multi-instance device path data
80 @param Single A pointer to a single-instance device path data
83 @retval TRUE If the Single device path is contained within Multi device path.
84 @retval FALSE The Single device path is not match within Multi device path.
89 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
90 IN EFI_DEVICE_PATH_PROTOCOL
*Single
93 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
94 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
97 if (Multi
== NULL
|| Single
== NULL
) {
102 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
105 // Search for the match of 'Single' in 'Multi'
107 while (DevicePathInst
!= NULL
) {
109 // If the single device path is found in multiple device paths,
112 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
113 FreePool (DevicePathInst
);
117 FreePool (DevicePathInst
);
118 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
125 This routine adjust the memory information for different memory type and
126 save them into the variables for next boot. It resets the system when
127 memory information is updated and the current boot option belongs to
128 boot category instead of application category. It doesn't count the
129 reserved memory occupied by RAM Disk.
131 @param Boot TRUE if current boot option belongs to boot
132 category instead of application category.
135 BmSetMemoryTypeInformationVariable (
140 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
141 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
148 EFI_HOB_GUID_TYPE
*GuidHob
;
149 BOOLEAN MemoryTypeInformationModified
;
150 BOOLEAN MemoryTypeInformationVariableExists
;
151 EFI_BOOT_MODE BootMode
;
153 MemoryTypeInformationModified
= FALSE
;
154 MemoryTypeInformationVariableExists
= FALSE
;
157 BootMode
= GetBootModeHob ();
159 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.
161 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
166 // Only check the the Memory Type Information variable in the boot mode
167 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type
168 // Information is not valid in this boot mode.
170 if (BootMode
!= BOOT_WITH_DEFAULT_SETTINGS
) {
172 Status
= gRT
->GetVariable (
173 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
174 &gEfiMemoryTypeInformationGuid
,
179 if (Status
== EFI_BUFFER_TOO_SMALL
) {
180 MemoryTypeInformationVariableExists
= TRUE
;
185 // Retrieve the current memory usage statistics. If they are not found, then
186 // no adjustments can be made to the Memory Type Information variable.
188 Status
= EfiGetSystemConfigurationTable (
189 &gEfiMemoryTypeInformationGuid
,
190 (VOID
**) &CurrentMemoryTypeInformation
192 if (EFI_ERROR (Status
) || CurrentMemoryTypeInformation
== NULL
) {
197 // Get the Memory Type Information settings from Hob if they exist,
198 // PEI is responsible for getting them from variable and build a Hob to save them.
199 // If the previous Memory Type Information is not available, then set defaults
201 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
202 if (GuidHob
== NULL
) {
204 // If Platform has not built Memory Type Info into the Hob, just return.
208 VariableSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
209 PreviousMemoryTypeInformation
= AllocateCopyPool (VariableSize
, GET_GUID_HOB_DATA (GuidHob
));
210 if (PreviousMemoryTypeInformation
== NULL
) {
215 // Use a heuristic to adjust the Memory Type Information for the next boot
217 DEBUG ((EFI_D_INFO
, "Memory Previous Current Next \n"));
218 DEBUG ((EFI_D_INFO
, " Type Pages Pages Pages \n"));
219 DEBUG ((EFI_D_INFO
, "====== ======== ======== ========\n"));
221 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
223 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
224 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
228 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
233 // Previous is the number of pages pre-allocated
234 // Current is the number of pages actually needed
236 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
237 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
241 // Inconsistent Memory Reserved across bootings may lead to S4 fail
242 // Write next varible to 125% * current when the pre-allocated memory is:
243 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING
244 // 2. Less than the needed memory
246 if ((Current
+ (Current
>> 1)) < Previous
) {
247 if (BootMode
== BOOT_WITH_DEFAULT_SETTINGS
) {
248 Next
= Current
+ (Current
>> 2);
250 } else if (Current
> Previous
) {
251 Next
= Current
+ (Current
>> 2);
253 if (Next
> 0 && Next
< 4) {
257 if (Next
!= Previous
) {
258 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
259 MemoryTypeInformationModified
= TRUE
;
262 DEBUG ((EFI_D_INFO
, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation
[Index
].Type
, Previous
, Current
, Next
));
266 // If any changes were made to the Memory Type Information settings, then set the new variable value;
267 // Or create the variable in first boot.
269 if (MemoryTypeInformationModified
|| !MemoryTypeInformationVariableExists
) {
270 Status
= BmSetVariableAndReportStatusCodeOnError (
271 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
272 &gEfiMemoryTypeInformationGuid
,
273 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
275 PreviousMemoryTypeInformation
278 if (!EFI_ERROR (Status
)) {
280 // If the Memory Type Information settings have been modified and the boot option belongs to boot category,
281 // then reset the platform so the new Memory Type Information setting will be used to guarantee that an S4
282 // entry/resume cycle will not fail.
284 if (MemoryTypeInformationModified
) {
285 DEBUG ((EFI_D_INFO
, "Memory Type Information settings change.\n"));
286 if (Boot
&& PcdGetBool (PcdResetOnMemoryTypeInformationChange
)) {
287 DEBUG ((EFI_D_INFO
, "...Warm Reset!!!\n"));
288 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
292 DEBUG ((EFI_D_ERROR
, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));
295 FreePool (PreviousMemoryTypeInformation
);
299 Set the variable and report the error through status code upon failure.
301 @param VariableName A Null-terminated string that is the name of the vendor's variable.
302 Each VariableName is unique for each VendorGuid. VariableName must
303 contain 1 or more characters. If VariableName is an empty string,
304 then EFI_INVALID_PARAMETER is returned.
305 @param VendorGuid A unique identifier for the vendor.
306 @param Attributes Attributes bitmask to set for the variable.
307 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
308 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
309 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
310 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
311 set, then a SetVariable() call with a DataSize of zero will not cause any change to
312 the variable value (the timestamp associated with the variable may be updated however
313 even if no new data value is provided,see the description of the
314 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
315 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
316 @param Data The contents for the variable.
318 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
319 defined by the Attributes.
320 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
321 DataSize exceeds the maximum allowed.
322 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
323 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
324 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
325 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
326 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
327 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
328 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
329 does NOT pass the validation check carried out by the firmware.
331 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
334 BmSetVariableAndReportStatusCodeOnError (
335 IN CHAR16
*VariableName
,
336 IN EFI_GUID
*VendorGuid
,
337 IN UINT32 Attributes
,
343 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
346 Status
= gRT
->SetVariable (
353 if (EFI_ERROR (Status
)) {
354 NameSize
= StrSize (VariableName
);
355 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
356 if (SetVariableStatus
!= NULL
) {
357 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
358 SetVariableStatus
->NameSize
= NameSize
;
359 SetVariableStatus
->DataSize
= DataSize
;
360 SetVariableStatus
->SetStatus
= Status
;
361 SetVariableStatus
->Attributes
= Attributes
;
362 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
363 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
365 REPORT_STATUS_CODE_EX (
367 PcdGet32 (PcdErrorCodeSetVariable
),
370 &gEdkiiStatusCodeDataTypeVariableGuid
,
372 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
375 FreePool (SetVariableStatus
);
384 Print the device path info.
386 @param DevicePath The device path need to print.
390 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
395 Str
= ConvertDevicePathToText (DevicePath
, FALSE
, FALSE
);
396 DEBUG ((EFI_D_INFO
, "%s", Str
));
403 Convert a single character to number.
404 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'
406 @param Char The input char which need to convert to int.
408 @return The converted 8-bit number or (UINTN) -1 if conversion failed.
415 if ((Char
>= L
'0') && (Char
<= L
'9')) {
416 return (Char
- L
'0');
419 if ((Char
>= L
'A') && (Char
<= L
'F')) {
420 return (Char
- L
'A' + 0xA);
427 Dispatch the deferred images that are returned from all DeferredImageLoad instances.
429 @retval EFI_SUCCESS At least one deferred image is loaded successfully and started.
430 @retval EFI_NOT_FOUND There is no deferred image.
431 @retval EFI_ACCESS_DENIED There are deferred images but all of them are failed to load.
435 EfiBootManagerDispatchDeferredImages (
440 EFI_DEFERRED_IMAGE_LOAD_PROTOCOL
*DeferredImage
;
445 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
449 EFI_HANDLE ImageHandle
;
456 // Find all the deferred image load protocols.
460 Status
= gBS
->LocateHandleBuffer (
462 &gEfiDeferredImageLoadProtocolGuid
,
467 if (EFI_ERROR (Status
)) {
468 return EFI_NOT_FOUND
;
473 for (Index
= 0; Index
< HandleCount
; Index
++) {
474 Status
= gBS
->HandleProtocol (Handles
[Index
], &gEfiDeferredImageLoadProtocolGuid
, (VOID
**) &DeferredImage
);
475 if (EFI_ERROR (Status
)) {
479 for (ImageIndex
= 0; ;ImageIndex
++) {
481 // Load all the deferred images in this protocol instance.
483 Status
= DeferredImage
->GetImageInfo (
491 if (EFI_ERROR (Status
)) {
496 // Load and start the image.
498 Status
= gBS
->LoadImage (
506 if (!EFI_ERROR (Status
)) {
509 // Before calling the image, enable the Watchdog Timer for
512 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
513 Status
= gBS
->StartImage (ImageHandle
, &ExitDataSize
, &ExitData
);
514 if (ExitData
!= NULL
) {
519 // Clear the Watchdog Timer after the image returns.
521 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
525 if (Handles
!= NULL
) {
529 if (ImageCount
== 0) {
530 return EFI_NOT_FOUND
;
532 if (LoadCount
== 0) {
533 return EFI_ACCESS_DENIED
;