]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
MdeModulePkg UefiBootManagerLib: Fix VS2012 build failure
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / BmMisc.c
CommitLineData
067ed98a
RN
1/** @file\r
2 Misc library functions.\r
3\r
4Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>\r
fb5848c5 5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
067ed98a
RN
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "InternalBm.h"\r
17\r
18/**\r
19 Delete the instance in Multi which matches partly with Single instance\r
20\r
21 @param Multi A pointer to a multi-instance device path data\r
22 structure.\r
23 @param Single A pointer to a single-instance device path data\r
24 structure.\r
25\r
26 @return This function will remove the device path instances in Multi which partly\r
27 match with the Single, and return the result device path. If there is no\r
28 remaining device path as a result, this function will return NULL.\r
29\r
30**/\r
31EFI_DEVICE_PATH_PROTOCOL *\r
32BmDelPartMatchInstance (\r
33 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
34 IN EFI_DEVICE_PATH_PROTOCOL *Single\r
35 )\r
36{\r
37 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
38 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
39 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r
40 UINTN InstanceSize;\r
41 UINTN SingleDpSize;\r
42\r
43 NewDevicePath = NULL;\r
44 TempNewDevicePath = NULL;\r
45\r
46 if (Multi == NULL || Single == NULL) {\r
47 return Multi;\r
48 }\r
49\r
50 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
51 SingleDpSize = GetDevicePathSize (Single) - END_DEVICE_PATH_LENGTH;\r
52 InstanceSize -= END_DEVICE_PATH_LENGTH;\r
53\r
54 while (Instance != NULL) {\r
55\r
56 if (CompareMem (Instance, Single, MIN (SingleDpSize, InstanceSize)) != 0) {\r
57 //\r
58 // Append the device path instance which does not match with Single\r
59 //\r
60 TempNewDevicePath = NewDevicePath;\r
61 NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);\r
62 if (TempNewDevicePath != NULL) {\r
63 FreePool(TempNewDevicePath);\r
64 }\r
65 }\r
66 FreePool(Instance);\r
67 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
68 InstanceSize -= END_DEVICE_PATH_LENGTH;\r
69 }\r
70\r
71 return NewDevicePath;\r
72}\r
73\r
74/**\r
75 Function compares a device path data structure to that of all the nodes of a\r
76 second device path instance.\r
77\r
78 @param Multi A pointer to a multi-instance device path data\r
79 structure.\r
80 @param Single A pointer to a single-instance device path data\r
81 structure.\r
82\r
83 @retval TRUE If the Single device path is contained within Multi device path.\r
84 @retval FALSE The Single device path is not match within Multi device path.\r
85\r
86**/\r
87BOOLEAN\r
88BmMatchDevicePaths (\r
89 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
90 IN EFI_DEVICE_PATH_PROTOCOL *Single\r
91 )\r
92{\r
93 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
94 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;\r
95 UINTN Size;\r
96\r
97 if (Multi == NULL || Single == NULL) {\r
98 return FALSE;\r
99 }\r
100\r
101 DevicePath = Multi;\r
102 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
103\r
104 //\r
105 // Search for the match of 'Single' in 'Multi'\r
106 //\r
107 while (DevicePathInst != NULL) {\r
108 //\r
109 // If the single device path is found in multiple device paths,\r
110 // return success\r
111 //\r
112 if (CompareMem (Single, DevicePathInst, Size) == 0) {\r
113 FreePool (DevicePathInst);\r
114 return TRUE;\r
115 }\r
116\r
117 FreePool (DevicePathInst);\r
118 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
119 }\r
120\r
121 return FALSE;\r
122}\r
123\r
124/**\r
125 This routine adjust the memory information for different memory type and \r
665b26ba
RN
126 save them into the variables for next boot. It resets the system when\r
127 memory information is updated and the current boot option belongs to\r
024bdafc
RN
128 boot category instead of application category. It doesn't count the\r
129 reserved memory occupied by RAM Disk.\r
665b26ba 130\r
024bdafc
RN
131 @param Boot TRUE if current boot option belongs to boot\r
132 category instead of application category.\r
067ed98a
RN
133**/\r
134VOID\r
135BmSetMemoryTypeInformationVariable (\r
3a986a35 136 IN BOOLEAN Boot\r
067ed98a
RN
137 )\r
138{\r
139 EFI_STATUS Status;\r
140 EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;\r
141 EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;\r
142 UINTN VariableSize;\r
143 UINTN Index;\r
144 UINTN Index1;\r
145 UINT32 Previous;\r
146 UINT32 Current;\r
147 UINT32 Next;\r
148 EFI_HOB_GUID_TYPE *GuidHob;\r
149 BOOLEAN MemoryTypeInformationModified;\r
150 BOOLEAN MemoryTypeInformationVariableExists;\r
151 EFI_BOOT_MODE BootMode;\r
152\r
153 MemoryTypeInformationModified = FALSE;\r
154 MemoryTypeInformationVariableExists = FALSE;\r
155\r
156\r
157 BootMode = GetBootModeHob ();\r
158 //\r
159 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.\r
160 //\r
161 if (BootMode == BOOT_IN_RECOVERY_MODE) {\r
162 return;\r
163 }\r
164\r
165 //\r
166 // Only check the the Memory Type Information variable in the boot mode \r
167 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type\r
168 // Information is not valid in this boot mode.\r
169 //\r
170 if (BootMode != BOOT_WITH_DEFAULT_SETTINGS) {\r
171 VariableSize = 0;\r
172 Status = gRT->GetVariable (\r
173 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
174 &gEfiMemoryTypeInformationGuid,\r
175 NULL, \r
176 &VariableSize, \r
177 NULL\r
178 );\r
179 if (Status == EFI_BUFFER_TOO_SMALL) {\r
180 MemoryTypeInformationVariableExists = TRUE;\r
181 }\r
182 }\r
183\r
184 //\r
185 // Retrieve the current memory usage statistics. If they are not found, then\r
186 // no adjustments can be made to the Memory Type Information variable.\r
187 //\r
188 Status = EfiGetSystemConfigurationTable (\r
189 &gEfiMemoryTypeInformationGuid,\r
190 (VOID **) &CurrentMemoryTypeInformation\r
191 );\r
192 if (EFI_ERROR (Status) || CurrentMemoryTypeInformation == NULL) {\r
193 return;\r
194 }\r
195\r
196 //\r
197 // Get the Memory Type Information settings from Hob if they exist,\r
198 // PEI is responsible for getting them from variable and build a Hob to save them.\r
199 // If the previous Memory Type Information is not available, then set defaults\r
200 //\r
201 GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);\r
202 if (GuidHob == NULL) {\r
203 //\r
204 // If Platform has not built Memory Type Info into the Hob, just return.\r
205 //\r
206 return;\r
207 }\r
208 PreviousMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);\r
209 VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);\r
210\r
211 //\r
212 // Use a heuristic to adjust the Memory Type Information for the next boot\r
213 //\r
214 DEBUG ((EFI_D_INFO, "Memory Previous Current Next \n"));\r
215 DEBUG ((EFI_D_INFO, " Type Pages Pages Pages \n"));\r
216 DEBUG ((EFI_D_INFO, "====== ======== ======== ========\n"));\r
217\r
218 for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
219\r
220 for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {\r
221 if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {\r
222 break;\r
223 }\r
224 }\r
225 if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {\r
226 continue;\r
227 }\r
228\r
229 //\r
230 // Previous is the number of pages pre-allocated\r
231 // Current is the number of pages actually needed\r
232 //\r
233 Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;\r
234 Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;\r
235 Next = Previous;\r
236\r
237 //\r
238 // Inconsistent Memory Reserved across bootings may lead to S4 fail\r
239 // Write next varible to 125% * current when the pre-allocated memory is:\r
240 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING\r
241 // 2. Less than the needed memory\r
242 //\r
243 if ((Current + (Current >> 1)) < Previous) {\r
244 if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {\r
245 Next = Current + (Current >> 2);\r
246 }\r
247 } else if (Current > Previous) {\r
248 Next = Current + (Current >> 2);\r
249 }\r
250 if (Next > 0 && Next < 4) {\r
251 Next = 4;\r
252 }\r
253\r
254 if (Next != Previous) {\r
255 PreviousMemoryTypeInformation[Index].NumberOfPages = Next;\r
256 MemoryTypeInformationModified = TRUE;\r
257 }\r
258\r
259 DEBUG ((EFI_D_INFO, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation[Index].Type, Previous, Current, Next));\r
260 }\r
261\r
262 //\r
263 // If any changes were made to the Memory Type Information settings, then set the new variable value;\r
264 // Or create the variable in first boot.\r
265 //\r
266 if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) {\r
267 Status = BmSetVariableAndReportStatusCodeOnError (\r
268 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
269 &gEfiMemoryTypeInformationGuid,\r
270 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
271 VariableSize,\r
272 PreviousMemoryTypeInformation\r
273 );\r
274\r
275 if (!EFI_ERROR (Status)) {\r
276 //\r
665b26ba
RN
277 // If the Memory Type Information settings have been modified and the boot option belongs to boot category,\r
278 // then reset the platform so the new Memory Type Information setting will be used to guarantee that an S4\r
067ed98a
RN
279 // entry/resume cycle will not fail.\r
280 //\r
665b26ba 281 if (MemoryTypeInformationModified && Boot && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
067ed98a
RN
282 DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));\r
283 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
284 }\r
285 } else {\r
286 DEBUG ((EFI_D_ERROR, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));\r
287 }\r
288 }\r
289}\r
290\r
291/**\r
292 Set the variable and report the error through status code upon failure.\r
293\r
294 @param VariableName A Null-terminated string that is the name of the vendor's variable.\r
295 Each VariableName is unique for each VendorGuid. VariableName must\r
296 contain 1 or more characters. If VariableName is an empty string,\r
297 then EFI_INVALID_PARAMETER is returned.\r
298 @param VendorGuid A unique identifier for the vendor.\r
299 @param Attributes Attributes bitmask to set for the variable.\r
300 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE, \r
301 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or \r
302 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero \r
303 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is \r
304 set, then a SetVariable() call with a DataSize of zero will not cause any change to \r
305 the variable value (the timestamp associated with the variable may be updated however \r
306 even if no new data value is provided,see the description of the \r
307 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not \r
308 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated). \r
309 @param Data The contents for the variable.\r
310\r
311 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as\r
312 defined by the Attributes.\r
313 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the\r
314 DataSize exceeds the maximum allowed.\r
315 @retval EFI_INVALID_PARAMETER VariableName is an empty string.\r
316 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
317 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.\r
318 @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
319 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
320 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS \r
321 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo \r
322 does NOT pass the validation check carried out by the firmware.\r
323\r
324 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
325**/\r
326EFI_STATUS\r
327BmSetVariableAndReportStatusCodeOnError (\r
328 IN CHAR16 *VariableName,\r
329 IN EFI_GUID *VendorGuid,\r
330 IN UINT32 Attributes,\r
331 IN UINTN DataSize,\r
332 IN VOID *Data\r
333 )\r
334{\r
335 EFI_STATUS Status;\r
336 EDKII_SET_VARIABLE_STATUS *SetVariableStatus;\r
337 UINTN NameSize;\r
338\r
339 Status = gRT->SetVariable (\r
340 VariableName,\r
341 VendorGuid,\r
342 Attributes,\r
343 DataSize,\r
344 Data\r
345 );\r
346 if (EFI_ERROR (Status)) {\r
347 NameSize = StrSize (VariableName);\r
348 SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);\r
349 if (SetVariableStatus != NULL) {\r
350 CopyGuid (&SetVariableStatus->Guid, VendorGuid);\r
351 SetVariableStatus->NameSize = NameSize;\r
352 SetVariableStatus->DataSize = DataSize;\r
353 SetVariableStatus->SetStatus = Status;\r
354 SetVariableStatus->Attributes = Attributes;\r
355 CopyMem (SetVariableStatus + 1, VariableName, NameSize);\r
356 CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data, DataSize);\r
357\r
358 REPORT_STATUS_CODE_EX (\r
359 EFI_ERROR_CODE,\r
360 PcdGet32 (PcdErrorCodeSetVariable),\r
361 0,\r
362 NULL,\r
363 &gEdkiiStatusCodeDataTypeVariableGuid,\r
364 SetVariableStatus,\r
365 sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize\r
366 );\r
367\r
368 FreePool (SetVariableStatus);\r
369 }\r
370 }\r
371\r
372 return Status;\r
373}\r
374\r
375\r
376/**\r
377 Print the device path info.\r
378\r
379 @param DevicePath The device path need to print.\r
380**/\r
381VOID\r
382BmPrintDp (\r
383 EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
384 )\r
385{\r
386 CHAR16 *Str;\r
387\r
388 Str = ConvertDevicePathToText (DevicePath, FALSE, FALSE);\r
389 DEBUG ((EFI_D_INFO, "%s", Str));\r
390 if (Str != NULL) {\r
391 FreePool (Str);\r
392 }\r
393}\r
418e8cd9
RN
394\r
395/**\r
396 Convert a single character to number.\r
397 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'\r
398\r
399 @param Char The input char which need to convert to int.\r
400\r
401 @return The converted 8-bit number or (UINTN) -1 if conversion failed.\r
402**/\r
403UINTN\r
404BmCharToUint (\r
405 IN CHAR16 Char\r
406 )\r
407{\r
408 if ((Char >= L'0') && (Char <= L'9')) {\r
409 return (UINTN) (Char - L'0');\r
410 }\r
411\r
412 if ((Char >= L'A') && (Char <= L'F')) {\r
413 return (UINTN) (Char - L'A' + 0xA);\r
414 }\r
415\r
416 ASSERT (FALSE);\r
417 return (UINTN) -1;\r
418}\r
419\r