2 Misc library functions.
4 Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "InternalBm.h"
13 Delete the instance in Multi which matches partly with Single instance
15 @param Multi A pointer to a multi-instance device path data
17 @param Single A pointer to a single-instance device path data
20 @return This function will remove the device path instances in Multi which partly
21 match with the Single, and return the result device path. If there is no
22 remaining device path as a result, this function will return NULL.
25 EFI_DEVICE_PATH_PROTOCOL
*
26 BmDelPartMatchInstance (
27 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
28 IN EFI_DEVICE_PATH_PROTOCOL
*Single
31 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
32 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
33 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
38 TempNewDevicePath
= NULL
;
40 if ((Multi
== NULL
) || (Single
== NULL
)) {
44 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
45 SingleDpSize
= GetDevicePathSize (Single
) - END_DEVICE_PATH_LENGTH
;
46 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
48 while (Instance
!= NULL
) {
49 if (CompareMem (Instance
, Single
, MIN (SingleDpSize
, InstanceSize
)) != 0) {
51 // Append the device path instance which does not match with Single
53 TempNewDevicePath
= NewDevicePath
;
54 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, Instance
);
55 if (TempNewDevicePath
!= NULL
) {
56 FreePool (TempNewDevicePath
);
61 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
62 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
69 Function compares a device path data structure to that of all the nodes of a
70 second device path instance.
72 @param Multi A pointer to a multi-instance device path data
74 @param Single A pointer to a single-instance device path data
77 @retval TRUE If the Single device path is contained within Multi device path.
78 @retval FALSE The Single device path is not match within Multi device path.
83 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
84 IN EFI_DEVICE_PATH_PROTOCOL
*Single
87 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
88 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
91 if ((Multi
== NULL
) || (Single
== NULL
)) {
96 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
99 // Search for the match of 'Single' in 'Multi'
101 while (DevicePathInst
!= NULL
) {
103 // If the single device path is found in multiple device paths,
106 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
107 FreePool (DevicePathInst
);
111 FreePool (DevicePathInst
);
112 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
119 This routine adjust the memory information for different memory type and
120 save them into the variables for next boot. It resets the system when
121 memory information is updated and the current boot option belongs to
122 boot category instead of application category. It doesn't count the
123 reserved memory occupied by RAM Disk.
125 @param Boot TRUE if current boot option belongs to boot
126 category instead of application category.
129 BmSetMemoryTypeInformationVariable (
134 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
135 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
142 EFI_HOB_GUID_TYPE
*GuidHob
;
143 BOOLEAN MemoryTypeInformationModified
;
144 BOOLEAN MemoryTypeInformationVariableExists
;
145 EFI_BOOT_MODE BootMode
;
147 MemoryTypeInformationModified
= FALSE
;
148 MemoryTypeInformationVariableExists
= FALSE
;
150 BootMode
= GetBootModeHob ();
152 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.
154 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
159 // Only check the the Memory Type Information variable in the boot mode
160 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type
161 // Information is not valid in this boot mode.
163 if (BootMode
!= BOOT_WITH_DEFAULT_SETTINGS
) {
165 Status
= gRT
->GetVariable (
166 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
167 &gEfiMemoryTypeInformationGuid
,
172 if (Status
== EFI_BUFFER_TOO_SMALL
) {
173 MemoryTypeInformationVariableExists
= TRUE
;
178 // Retrieve the current memory usage statistics. If they are not found, then
179 // no adjustments can be made to the Memory Type Information variable.
181 Status
= EfiGetSystemConfigurationTable (
182 &gEfiMemoryTypeInformationGuid
,
183 (VOID
**)&CurrentMemoryTypeInformation
185 if (EFI_ERROR (Status
) || (CurrentMemoryTypeInformation
== NULL
)) {
190 // Get the Memory Type Information settings from Hob if they exist,
191 // PEI is responsible for getting them from variable and build a Hob to save them.
192 // If the previous Memory Type Information is not available, then set defaults
194 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
195 if (GuidHob
== NULL
) {
197 // If Platform has not built Memory Type Info into the Hob, just return.
202 VariableSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
203 PreviousMemoryTypeInformation
= AllocateCopyPool (VariableSize
, GET_GUID_HOB_DATA (GuidHob
));
204 if (PreviousMemoryTypeInformation
== NULL
) {
209 // Use a heuristic to adjust the Memory Type Information for the next boot
211 DEBUG ((DEBUG_INFO
, "Memory Previous Current Next \n"));
212 DEBUG ((DEBUG_INFO
, " Type Pages Pages Pages \n"));
213 DEBUG ((DEBUG_INFO
, "====== ======== ======== ========\n"));
215 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
216 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
217 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
222 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
227 // Previous is the number of pages pre-allocated
228 // Current is the number of pages actually needed
230 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
231 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
235 // Inconsistent Memory Reserved across bootings may lead to S4 fail
236 // Write next varible to 125% * current when the pre-allocated memory is:
237 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING
238 // 2. Less than the needed memory
240 if ((Current
+ (Current
>> 1)) < Previous
) {
241 if (BootMode
== BOOT_WITH_DEFAULT_SETTINGS
) {
242 Next
= Current
+ (Current
>> 2);
244 } else if (Current
> Previous
) {
245 Next
= Current
+ (Current
>> 2);
248 if ((Next
> 0) && (Next
< 4)) {
252 if (Next
!= Previous
) {
253 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
254 MemoryTypeInformationModified
= TRUE
;
257 DEBUG ((DEBUG_INFO
, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation
[Index
].Type
, Previous
, Current
, Next
));
261 // If any changes were made to the Memory Type Information settings, then set the new variable value;
262 // Or create the variable in first boot.
264 if (MemoryTypeInformationModified
|| !MemoryTypeInformationVariableExists
) {
265 Status
= BmSetVariableAndReportStatusCodeOnError (
266 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
267 &gEfiMemoryTypeInformationGuid
,
268 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
270 PreviousMemoryTypeInformation
273 if (!EFI_ERROR (Status
)) {
275 // If the Memory Type Information settings have been modified and the boot option belongs to boot category,
276 // then reset the platform so the new Memory Type Information setting will be used to guarantee that an S4
277 // entry/resume cycle will not fail.
279 if (MemoryTypeInformationModified
) {
280 DEBUG ((DEBUG_INFO
, "Memory Type Information settings change.\n"));
281 if (Boot
&& PcdGetBool (PcdResetOnMemoryTypeInformationChange
)) {
282 DEBUG ((DEBUG_INFO
, "...Warm Reset!!!\n"));
283 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
287 DEBUG ((DEBUG_ERROR
, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));
291 FreePool (PreviousMemoryTypeInformation
);
295 Set the variable and report the error through status code upon failure.
297 @param VariableName A Null-terminated string that is the name of the vendor's variable.
298 Each VariableName is unique for each VendorGuid. VariableName must
299 contain 1 or more characters. If VariableName is an empty string,
300 then EFI_INVALID_PARAMETER is returned.
301 @param VendorGuid A unique identifier for the vendor.
302 @param Attributes Attributes bitmask to set for the variable.
303 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
304 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
305 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
306 set, then a SetVariable() call with a DataSize of zero will not cause any change to
307 the variable value (the timestamp associated with the variable may be updated however
308 even if no new data value is provided,see the description of the
309 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
310 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
311 @param Data The contents for the variable.
313 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
314 defined by the Attributes.
315 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
316 DataSize exceeds the maximum allowed.
317 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
318 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
319 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
320 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
321 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
322 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS
323 being set, but the AuthInfo does NOT pass the validation check carried out by the firmware.
325 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
328 BmSetVariableAndReportStatusCodeOnError (
329 IN CHAR16
*VariableName
,
330 IN EFI_GUID
*VendorGuid
,
331 IN UINT32 Attributes
,
337 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
340 Status
= gRT
->SetVariable (
347 if (EFI_ERROR (Status
)) {
348 NameSize
= StrSize (VariableName
);
349 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
350 if (SetVariableStatus
!= NULL
) {
351 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
352 SetVariableStatus
->NameSize
= NameSize
;
353 SetVariableStatus
->DataSize
= DataSize
;
354 SetVariableStatus
->SetStatus
= Status
;
355 SetVariableStatus
->Attributes
= Attributes
;
356 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
357 CopyMem (((UINT8
*)(SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
359 REPORT_STATUS_CODE_EX (
361 PcdGet32 (PcdErrorCodeSetVariable
),
364 &gEdkiiStatusCodeDataTypeVariableGuid
,
366 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
369 FreePool (SetVariableStatus
);
377 Print the device path info.
379 @param DevicePath The device path need to print.
383 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
388 Str
= ConvertDevicePathToText (DevicePath
, FALSE
, FALSE
);
389 DEBUG ((DEBUG_INFO
, "%s", Str
));
396 Convert a single character to number.
397 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'
399 @param Char The input char which need to convert to int.
401 @return The converted 8-bit number or (UINTN) -1 if conversion failed.
408 if ((Char
>= L
'0') && (Char
<= L
'9')) {
409 return (Char
- L
'0');
412 if ((Char
>= L
'A') && (Char
<= L
'F')) {
413 return (Char
- L
'A' + 0xA);
420 Dispatch the deferred images that are returned from all DeferredImageLoad instances.
422 @retval EFI_SUCCESS At least one deferred image is loaded successfully and started.
423 @retval EFI_NOT_FOUND There is no deferred image.
424 @retval EFI_ACCESS_DENIED There are deferred images but all of them are failed to load.
428 EfiBootManagerDispatchDeferredImages (
433 EFI_DEFERRED_IMAGE_LOAD_PROTOCOL
*DeferredImage
;
438 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
442 EFI_HANDLE ImageHandle
;
447 // Find all the deferred image load protocols.
451 Status
= gBS
->LocateHandleBuffer (
453 &gEfiDeferredImageLoadProtocolGuid
,
458 if (EFI_ERROR (Status
)) {
459 return EFI_NOT_FOUND
;
464 for (Index
= 0; Index
< HandleCount
; Index
++) {
465 Status
= gBS
->HandleProtocol (Handles
[Index
], &gEfiDeferredImageLoadProtocolGuid
, (VOID
**)&DeferredImage
);
466 if (EFI_ERROR (Status
)) {
470 for (ImageIndex
= 0; ; ImageIndex
++) {
472 // Load all the deferred images in this protocol instance.
474 Status
= DeferredImage
->GetImageInfo (
482 if (EFI_ERROR (Status
)) {
488 // Load and start the image.
490 Status
= gBS
->LoadImage (
498 if (EFI_ERROR (Status
)) {
500 // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
501 // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
502 // If the caller doesn't have the option to defer the execution of an image, we should
503 // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
505 if (Status
== EFI_SECURITY_VIOLATION
) {
506 gBS
->UnloadImage (ImageHandle
);
511 // Before calling the image, enable the Watchdog Timer for
514 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
515 gBS
->StartImage (ImageHandle
, NULL
, NULL
);
518 // Clear the Watchdog Timer after the image returns.
520 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
;