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. It resets the system when
126 memory information is updated and the current boot option belongs to
127 boot category instead of application category.
129 @param Boot TRUE if current boot option belongs to boot category instead of
130 application category.
133 BmSetMemoryTypeInformationVariable (
138 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
139 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
146 EFI_HOB_GUID_TYPE
*GuidHob
;
147 BOOLEAN MemoryTypeInformationModified
;
148 BOOLEAN MemoryTypeInformationVariableExists
;
149 EFI_BOOT_MODE BootMode
;
151 MemoryTypeInformationModified
= FALSE
;
152 MemoryTypeInformationVariableExists
= FALSE
;
155 BootMode
= GetBootModeHob ();
157 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.
159 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
164 // Only check the the Memory Type Information variable in the boot mode
165 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type
166 // Information is not valid in this boot mode.
168 if (BootMode
!= BOOT_WITH_DEFAULT_SETTINGS
) {
170 Status
= gRT
->GetVariable (
171 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
172 &gEfiMemoryTypeInformationGuid
,
177 if (Status
== EFI_BUFFER_TOO_SMALL
) {
178 MemoryTypeInformationVariableExists
= TRUE
;
183 // Retrieve the current memory usage statistics. If they are not found, then
184 // no adjustments can be made to the Memory Type Information variable.
186 Status
= EfiGetSystemConfigurationTable (
187 &gEfiMemoryTypeInformationGuid
,
188 (VOID
**) &CurrentMemoryTypeInformation
190 if (EFI_ERROR (Status
) || CurrentMemoryTypeInformation
== NULL
) {
195 // Get the Memory Type Information settings from Hob if they exist,
196 // PEI is responsible for getting them from variable and build a Hob to save them.
197 // If the previous Memory Type Information is not available, then set defaults
199 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
200 if (GuidHob
== NULL
) {
202 // If Platform has not built Memory Type Info into the Hob, just return.
206 PreviousMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
207 VariableSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
210 // Use a heuristic to adjust the Memory Type Information for the next boot
212 DEBUG ((EFI_D_INFO
, "Memory Previous Current Next \n"));
213 DEBUG ((EFI_D_INFO
, " Type Pages Pages Pages \n"));
214 DEBUG ((EFI_D_INFO
, "====== ======== ======== ========\n"));
216 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
218 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
219 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
223 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
228 // Previous is the number of pages pre-allocated
229 // Current is the number of pages actually needed
231 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
232 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
236 // Inconsistent Memory Reserved across bootings may lead to S4 fail
237 // Write next varible to 125% * current when the pre-allocated memory is:
238 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING
239 // 2. Less than the needed memory
241 if ((Current
+ (Current
>> 1)) < Previous
) {
242 if (BootMode
== BOOT_WITH_DEFAULT_SETTINGS
) {
243 Next
= Current
+ (Current
>> 2);
245 } else if (Current
> Previous
) {
246 Next
= Current
+ (Current
>> 2);
248 if (Next
> 0 && Next
< 4) {
252 if (Next
!= Previous
) {
253 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
254 MemoryTypeInformationModified
= TRUE
;
257 DEBUG ((EFI_D_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
&& Boot
&& PcdGetBool (PcdResetOnMemoryTypeInformationChange
)) {
280 DEBUG ((EFI_D_INFO
, "Memory Type Information settings change. Warm Reset!!!\n"));
281 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
284 DEBUG ((EFI_D_ERROR
, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));
290 Set the variable and report the error through status code upon failure.
292 @param VariableName A Null-terminated string that is the name of the vendor's variable.
293 Each VariableName is unique for each VendorGuid. VariableName must
294 contain 1 or more characters. If VariableName is an empty string,
295 then EFI_INVALID_PARAMETER is returned.
296 @param VendorGuid A unique identifier for the vendor.
297 @param Attributes Attributes bitmask to set for the variable.
298 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
299 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
300 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
301 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
302 set, then a SetVariable() call with a DataSize of zero will not cause any change to
303 the variable value (the timestamp associated with the variable may be updated however
304 even if no new data value is provided,see the description of the
305 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
306 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
307 @param Data The contents for the variable.
309 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
310 defined by the Attributes.
311 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
312 DataSize exceeds the maximum allowed.
313 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
314 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
315 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
316 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
317 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
318 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
319 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
320 does NOT pass the validation check carried out by the firmware.
322 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
325 BmSetVariableAndReportStatusCodeOnError (
326 IN CHAR16
*VariableName
,
327 IN EFI_GUID
*VendorGuid
,
328 IN UINT32 Attributes
,
334 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
337 Status
= gRT
->SetVariable (
344 if (EFI_ERROR (Status
)) {
345 NameSize
= StrSize (VariableName
);
346 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
347 if (SetVariableStatus
!= NULL
) {
348 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
349 SetVariableStatus
->NameSize
= NameSize
;
350 SetVariableStatus
->DataSize
= DataSize
;
351 SetVariableStatus
->SetStatus
= Status
;
352 SetVariableStatus
->Attributes
= Attributes
;
353 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
354 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
356 REPORT_STATUS_CODE_EX (
358 PcdGet32 (PcdErrorCodeSetVariable
),
361 &gEdkiiStatusCodeDataTypeVariableGuid
,
363 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
366 FreePool (SetVariableStatus
);
375 Print the device path info.
377 @param DevicePath The device path need to print.
381 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
386 Str
= ConvertDevicePathToText (DevicePath
, FALSE
, FALSE
);
387 DEBUG ((EFI_D_INFO
, "%s", Str
));
394 Convert a single character to number.
395 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'
397 @param Char The input char which need to convert to int.
399 @return The converted 8-bit number or (UINTN) -1 if conversion failed.
406 if ((Char
>= L
'0') && (Char
<= L
'9')) {
407 return (UINTN
) (Char
- L
'0');
410 if ((Char
>= L
'A') && (Char
<= L
'F')) {
411 return (UINTN
) (Char
- L
'A' + 0xA);