2 Misc library functions.
4 Copyright (c) 2011 - 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 "InternalBm.h"
18 Delete the instance in Multi which matches partly with Single instance
20 @param Multi A pointer to a multi-instance device path data
22 @param Single A pointer to a single-instance device path data
25 @return This function will remove the device path instances in Multi which partly
26 match with the Single, and return the result device path. If there is no
27 remaining device path as a result, this function will return NULL.
30 EFI_DEVICE_PATH_PROTOCOL
*
31 BmDelPartMatchInstance (
32 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
33 IN EFI_DEVICE_PATH_PROTOCOL
*Single
36 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
37 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
38 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
43 TempNewDevicePath
= NULL
;
45 if (Multi
== NULL
|| Single
== NULL
) {
49 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
50 SingleDpSize
= GetDevicePathSize (Single
) - END_DEVICE_PATH_LENGTH
;
51 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
53 while (Instance
!= NULL
) {
55 if (CompareMem (Instance
, Single
, MIN (SingleDpSize
, InstanceSize
)) != 0) {
57 // Append the device path instance which does not match with Single
59 TempNewDevicePath
= NewDevicePath
;
60 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, Instance
);
61 if (TempNewDevicePath
!= NULL
) {
62 FreePool(TempNewDevicePath
);
66 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
67 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
74 Function compares a device path data structure to that of all the nodes of a
75 second device path instance.
77 @param Multi A pointer to a multi-instance device path data
79 @param Single A pointer to a single-instance device path data
82 @retval TRUE If the Single device path is contained within Multi device path.
83 @retval FALSE The Single device path is not match within Multi device path.
88 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
89 IN EFI_DEVICE_PATH_PROTOCOL
*Single
92 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
93 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
96 if (Multi
== NULL
|| Single
== NULL
) {
101 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
104 // Search for the match of 'Single' in 'Multi'
106 while (DevicePathInst
!= NULL
) {
108 // If the single device path is found in multiple device paths,
111 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
112 FreePool (DevicePathInst
);
116 FreePool (DevicePathInst
);
117 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
124 This routine adjust the memory information for different memory type and
125 save them into the variables for next boot.
128 BmSetMemoryTypeInformationVariable (
133 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
134 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
141 EFI_HOB_GUID_TYPE
*GuidHob
;
142 BOOLEAN MemoryTypeInformationModified
;
143 BOOLEAN MemoryTypeInformationVariableExists
;
144 EFI_BOOT_MODE BootMode
;
146 MemoryTypeInformationModified
= FALSE
;
147 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.
201 PreviousMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
202 VariableSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
205 // Use a heuristic to adjust the Memory Type Information for the next boot
207 DEBUG ((EFI_D_INFO
, "Memory Previous Current Next \n"));
208 DEBUG ((EFI_D_INFO
, " Type Pages Pages Pages \n"));
209 DEBUG ((EFI_D_INFO
, "====== ======== ======== ========\n"));
211 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
213 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
214 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
218 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
223 // Previous is the number of pages pre-allocated
224 // Current is the number of pages actually needed
226 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
227 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
231 // Inconsistent Memory Reserved across bootings may lead to S4 fail
232 // Write next varible to 125% * current when the pre-allocated memory is:
233 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING
234 // 2. Less than the needed memory
236 if ((Current
+ (Current
>> 1)) < Previous
) {
237 if (BootMode
== BOOT_WITH_DEFAULT_SETTINGS
) {
238 Next
= Current
+ (Current
>> 2);
240 } else if (Current
> Previous
) {
241 Next
= Current
+ (Current
>> 2);
243 if (Next
> 0 && Next
< 4) {
247 if (Next
!= Previous
) {
248 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
249 MemoryTypeInformationModified
= TRUE
;
252 DEBUG ((EFI_D_INFO
, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation
[Index
].Type
, Previous
, Current
, Next
));
256 // If any changes were made to the Memory Type Information settings, then set the new variable value;
257 // Or create the variable in first boot.
259 if (MemoryTypeInformationModified
|| !MemoryTypeInformationVariableExists
) {
260 Status
= BmSetVariableAndReportStatusCodeOnError (
261 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
262 &gEfiMemoryTypeInformationGuid
,
263 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
265 PreviousMemoryTypeInformation
268 if (!EFI_ERROR (Status
)) {
270 // If the Memory Type Information settings have been modified, then reset the platform
271 // so the new Memory Type Information setting will be used to guarantee that an S4
272 // entry/resume cycle will not fail.
274 if (MemoryTypeInformationModified
) {
275 DEBUG ((EFI_D_INFO
, "Memory Type Information settings change. Warm Reset!!!\n"));
276 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
279 DEBUG ((EFI_D_ERROR
, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));
285 Set the variable and report the error through status code upon failure.
287 @param VariableName A Null-terminated string that is the name of the vendor's variable.
288 Each VariableName is unique for each VendorGuid. VariableName must
289 contain 1 or more characters. If VariableName is an empty string,
290 then EFI_INVALID_PARAMETER is returned.
291 @param VendorGuid A unique identifier for the vendor.
292 @param Attributes Attributes bitmask to set for the variable.
293 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
294 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
295 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
296 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
297 set, then a SetVariable() call with a DataSize of zero will not cause any change to
298 the variable value (the timestamp associated with the variable may be updated however
299 even if no new data value is provided,see the description of the
300 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
301 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
302 @param Data The contents for the variable.
304 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
305 defined by the Attributes.
306 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
307 DataSize exceeds the maximum allowed.
308 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
309 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
310 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
311 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
312 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
313 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
314 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
315 does NOT pass the validation check carried out by the firmware.
317 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
320 BmSetVariableAndReportStatusCodeOnError (
321 IN CHAR16
*VariableName
,
322 IN EFI_GUID
*VendorGuid
,
323 IN UINT32 Attributes
,
329 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
332 Status
= gRT
->SetVariable (
339 if (EFI_ERROR (Status
)) {
340 NameSize
= StrSize (VariableName
);
341 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
342 if (SetVariableStatus
!= NULL
) {
343 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
344 SetVariableStatus
->NameSize
= NameSize
;
345 SetVariableStatus
->DataSize
= DataSize
;
346 SetVariableStatus
->SetStatus
= Status
;
347 SetVariableStatus
->Attributes
= Attributes
;
348 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
349 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
351 REPORT_STATUS_CODE_EX (
353 PcdGet32 (PcdErrorCodeSetVariable
),
356 &gEdkiiStatusCodeDataTypeVariableGuid
,
358 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
361 FreePool (SetVariableStatus
);
370 Print the device path info.
372 @param DevicePath The device path need to print.
376 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
381 Str
= ConvertDevicePathToText (DevicePath
, FALSE
, FALSE
);
382 DEBUG ((EFI_D_INFO
, "%s", Str
));
389 Convert a single character to number.
390 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'
392 @param Char The input char which need to convert to int.
394 @return The converted 8-bit number or (UINTN) -1 if conversion failed.
401 if ((Char
>= L
'0') && (Char
<= L
'9')) {
402 return (UINTN
) (Char
- L
'0');
405 if ((Char
>= L
'A') && (Char
<= L
'F')) {
406 return (UINTN
) (Char
- L
'A' + 0xA);