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. It doesn't count the
128 reserved memory occupied by RAM Disk.
130 @param Boot TRUE if current boot option belongs to boot
131 category instead of application category.
132 @param RamDiskSizeInPages Reserved memory size in pages occupied by
136 BmSetMemoryTypeInformationVariable (
138 IN UINTN RamDiskSizeInPages
142 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
143 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
150 EFI_HOB_GUID_TYPE
*GuidHob
;
151 BOOLEAN MemoryTypeInformationModified
;
152 BOOLEAN MemoryTypeInformationVariableExists
;
153 EFI_BOOT_MODE BootMode
;
155 MemoryTypeInformationModified
= FALSE
;
156 MemoryTypeInformationVariableExists
= FALSE
;
159 BootMode
= GetBootModeHob ();
161 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.
163 if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
168 // Only check the the Memory Type Information variable in the boot mode
169 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type
170 // Information is not valid in this boot mode.
172 if (BootMode
!= BOOT_WITH_DEFAULT_SETTINGS
) {
174 Status
= gRT
->GetVariable (
175 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
176 &gEfiMemoryTypeInformationGuid
,
181 if (Status
== EFI_BUFFER_TOO_SMALL
) {
182 MemoryTypeInformationVariableExists
= TRUE
;
187 // Retrieve the current memory usage statistics. If they are not found, then
188 // no adjustments can be made to the Memory Type Information variable.
190 Status
= EfiGetSystemConfigurationTable (
191 &gEfiMemoryTypeInformationGuid
,
192 (VOID
**) &CurrentMemoryTypeInformation
194 if (EFI_ERROR (Status
) || CurrentMemoryTypeInformation
== NULL
) {
199 // Get the Memory Type Information settings from Hob if they exist,
200 // PEI is responsible for getting them from variable and build a Hob to save them.
201 // If the previous Memory Type Information is not available, then set defaults
203 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
204 if (GuidHob
== NULL
) {
206 // If Platform has not built Memory Type Info into the Hob, just return.
210 PreviousMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
211 VariableSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
214 // Use a heuristic to adjust the Memory Type Information for the next boot
216 DEBUG ((EFI_D_INFO
, "Memory Previous Current Next \n"));
217 DEBUG ((EFI_D_INFO
, " Type Pages Pages Pages \n"));
218 DEBUG ((EFI_D_INFO
, "====== ======== ======== ========\n"));
220 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
222 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
223 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
227 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
232 // Do not count the reserved memory occupied by RAM Disk.
234 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiReservedMemoryType
) {
235 CurrentMemoryTypeInformation
[Index1
].NumberOfPages
-= (UINT32
) RamDiskSizeInPages
;
239 // Previous is the number of pages pre-allocated
240 // Current is the number of pages actually needed
242 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
243 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
247 // Inconsistent Memory Reserved across bootings may lead to S4 fail
248 // Write next varible to 125% * current when the pre-allocated memory is:
249 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING
250 // 2. Less than the needed memory
252 if ((Current
+ (Current
>> 1)) < Previous
) {
253 if (BootMode
== BOOT_WITH_DEFAULT_SETTINGS
) {
254 Next
= Current
+ (Current
>> 2);
256 } else if (Current
> Previous
) {
257 Next
= Current
+ (Current
>> 2);
259 if (Next
> 0 && Next
< 4) {
263 if (Next
!= Previous
) {
264 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
265 MemoryTypeInformationModified
= TRUE
;
268 DEBUG ((EFI_D_INFO
, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation
[Index
].Type
, Previous
, Current
, Next
));
272 // If any changes were made to the Memory Type Information settings, then set the new variable value;
273 // Or create the variable in first boot.
275 if (MemoryTypeInformationModified
|| !MemoryTypeInformationVariableExists
) {
276 Status
= BmSetVariableAndReportStatusCodeOnError (
277 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
278 &gEfiMemoryTypeInformationGuid
,
279 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
281 PreviousMemoryTypeInformation
284 if (!EFI_ERROR (Status
)) {
286 // If the Memory Type Information settings have been modified and the boot option belongs to boot category,
287 // then reset the platform so the new Memory Type Information setting will be used to guarantee that an S4
288 // entry/resume cycle will not fail.
290 if (MemoryTypeInformationModified
&& Boot
&& PcdGetBool (PcdResetOnMemoryTypeInformationChange
)) {
291 DEBUG ((EFI_D_INFO
, "Memory Type Information settings change. Warm Reset!!!\n"));
292 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
295 DEBUG ((EFI_D_ERROR
, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));
301 Set the variable and report the error through status code upon failure.
303 @param VariableName A Null-terminated string that is the name of the vendor's variable.
304 Each VariableName is unique for each VendorGuid. VariableName must
305 contain 1 or more characters. If VariableName is an empty string,
306 then EFI_INVALID_PARAMETER is returned.
307 @param VendorGuid A unique identifier for the vendor.
308 @param Attributes Attributes bitmask to set for the variable.
309 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
310 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
311 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
312 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
313 set, then a SetVariable() call with a DataSize of zero will not cause any change to
314 the variable value (the timestamp associated with the variable may be updated however
315 even if no new data value is provided,see the description of the
316 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
317 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
318 @param Data The contents for the variable.
320 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
321 defined by the Attributes.
322 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
323 DataSize exceeds the maximum allowed.
324 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
325 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
326 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
327 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
328 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
329 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
330 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
331 does NOT pass the validation check carried out by the firmware.
333 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
336 BmSetVariableAndReportStatusCodeOnError (
337 IN CHAR16
*VariableName
,
338 IN EFI_GUID
*VendorGuid
,
339 IN UINT32 Attributes
,
345 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
348 Status
= gRT
->SetVariable (
355 if (EFI_ERROR (Status
)) {
356 NameSize
= StrSize (VariableName
);
357 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
);
358 if (SetVariableStatus
!= NULL
) {
359 CopyGuid (&SetVariableStatus
->Guid
, VendorGuid
);
360 SetVariableStatus
->NameSize
= NameSize
;
361 SetVariableStatus
->DataSize
= DataSize
;
362 SetVariableStatus
->SetStatus
= Status
;
363 SetVariableStatus
->Attributes
= Attributes
;
364 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
365 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Data
, DataSize
);
367 REPORT_STATUS_CODE_EX (
369 PcdGet32 (PcdErrorCodeSetVariable
),
372 &gEdkiiStatusCodeDataTypeVariableGuid
,
374 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ DataSize
377 FreePool (SetVariableStatus
);
386 Print the device path info.
388 @param DevicePath The device path need to print.
392 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
397 Str
= ConvertDevicePathToText (DevicePath
, FALSE
, FALSE
);
398 DEBUG ((EFI_D_INFO
, "%s", Str
));
405 Convert a single character to number.
406 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'
408 @param Char The input char which need to convert to int.
410 @return The converted 8-bit number or (UINTN) -1 if conversion failed.
417 if ((Char
>= L
'0') && (Char
<= L
'9')) {
418 return (UINTN
) (Char
- L
'0');
421 if ((Char
>= L
'A') && (Char
<= L
'F')) {
422 return (UINTN
) (Char
- L
'A' + 0xA);