]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / BmMisc.c
CommitLineData
067ed98a
RN
1/** @file\r
2 Misc library functions.\r
3\r
d1102dba 4Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
fb5848c5 5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
9d510e61 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
067ed98a
RN
7\r
8**/\r
9\r
10#include "InternalBm.h"\r
11\r
12/**\r
13 Delete the instance in Multi which matches partly with Single instance\r
14\r
15 @param Multi A pointer to a multi-instance device path data\r
16 structure.\r
17 @param Single A pointer to a single-instance device path data\r
18 structure.\r
19\r
20 @return This function will remove the device path instances in Multi which partly\r
21 match with the Single, and return the result device path. If there is no\r
22 remaining device path as a result, this function will return NULL.\r
23\r
24**/\r
25EFI_DEVICE_PATH_PROTOCOL *\r
26BmDelPartMatchInstance (\r
27 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
28 IN EFI_DEVICE_PATH_PROTOCOL *Single\r
29 )\r
30{\r
31 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
32 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
33 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r
34 UINTN InstanceSize;\r
35 UINTN SingleDpSize;\r
36\r
37 NewDevicePath = NULL;\r
38 TempNewDevicePath = NULL;\r
39\r
40 if (Multi == NULL || Single == NULL) {\r
41 return Multi;\r
42 }\r
43\r
44 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
45 SingleDpSize = GetDevicePathSize (Single) - END_DEVICE_PATH_LENGTH;\r
46 InstanceSize -= END_DEVICE_PATH_LENGTH;\r
47\r
48 while (Instance != NULL) {\r
49\r
50 if (CompareMem (Instance, Single, MIN (SingleDpSize, InstanceSize)) != 0) {\r
51 //\r
52 // Append the device path instance which does not match with Single\r
53 //\r
54 TempNewDevicePath = NewDevicePath;\r
55 NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);\r
56 if (TempNewDevicePath != NULL) {\r
57 FreePool(TempNewDevicePath);\r
58 }\r
59 }\r
60 FreePool(Instance);\r
61 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
62 InstanceSize -= END_DEVICE_PATH_LENGTH;\r
63 }\r
64\r
65 return NewDevicePath;\r
66}\r
67\r
68/**\r
69 Function compares a device path data structure to that of all the nodes of a\r
70 second device path instance.\r
71\r
72 @param Multi A pointer to a multi-instance device path data\r
73 structure.\r
74 @param Single A pointer to a single-instance device path data\r
75 structure.\r
76\r
77 @retval TRUE If the Single device path is contained within Multi device path.\r
78 @retval FALSE The Single device path is not match within Multi device path.\r
79\r
80**/\r
81BOOLEAN\r
82BmMatchDevicePaths (\r
83 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
84 IN EFI_DEVICE_PATH_PROTOCOL *Single\r
85 )\r
86{\r
87 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
88 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;\r
89 UINTN Size;\r
90\r
91 if (Multi == NULL || Single == NULL) {\r
92 return FALSE;\r
93 }\r
94\r
95 DevicePath = Multi;\r
96 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
97\r
98 //\r
99 // Search for the match of 'Single' in 'Multi'\r
100 //\r
101 while (DevicePathInst != NULL) {\r
102 //\r
103 // If the single device path is found in multiple device paths,\r
104 // return success\r
105 //\r
106 if (CompareMem (Single, DevicePathInst, Size) == 0) {\r
107 FreePool (DevicePathInst);\r
108 return TRUE;\r
109 }\r
110\r
111 FreePool (DevicePathInst);\r
112 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
113 }\r
114\r
115 return FALSE;\r
116}\r
117\r
118/**\r
d1102dba 119 This routine adjust the memory information for different memory type and\r
665b26ba
RN
120 save them into the variables for next boot. It resets the system when\r
121 memory information is updated and the current boot option belongs to\r
024bdafc
RN
122 boot category instead of application category. It doesn't count the\r
123 reserved memory occupied by RAM Disk.\r
665b26ba 124\r
024bdafc
RN
125 @param Boot TRUE if current boot option belongs to boot\r
126 category instead of application category.\r
067ed98a
RN
127**/\r
128VOID\r
129BmSetMemoryTypeInformationVariable (\r
3a986a35 130 IN BOOLEAN Boot\r
067ed98a
RN
131 )\r
132{\r
133 EFI_STATUS Status;\r
134 EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;\r
135 EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;\r
136 UINTN VariableSize;\r
137 UINTN Index;\r
138 UINTN Index1;\r
139 UINT32 Previous;\r
140 UINT32 Current;\r
141 UINT32 Next;\r
142 EFI_HOB_GUID_TYPE *GuidHob;\r
143 BOOLEAN MemoryTypeInformationModified;\r
144 BOOLEAN MemoryTypeInformationVariableExists;\r
145 EFI_BOOT_MODE BootMode;\r
146\r
147 MemoryTypeInformationModified = FALSE;\r
148 MemoryTypeInformationVariableExists = FALSE;\r
149\r
150\r
151 BootMode = GetBootModeHob ();\r
152 //\r
153 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.\r
154 //\r
155 if (BootMode == BOOT_IN_RECOVERY_MODE) {\r
156 return;\r
157 }\r
158\r
159 //\r
d1102dba 160 // Only check the the Memory Type Information variable in the boot mode\r
067ed98a
RN
161 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type\r
162 // Information is not valid in this boot mode.\r
163 //\r
164 if (BootMode != BOOT_WITH_DEFAULT_SETTINGS) {\r
165 VariableSize = 0;\r
166 Status = gRT->GetVariable (\r
167 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
168 &gEfiMemoryTypeInformationGuid,\r
d1102dba
LG
169 NULL,\r
170 &VariableSize,\r
067ed98a
RN
171 NULL\r
172 );\r
173 if (Status == EFI_BUFFER_TOO_SMALL) {\r
174 MemoryTypeInformationVariableExists = TRUE;\r
175 }\r
176 }\r
177\r
178 //\r
179 // Retrieve the current memory usage statistics. If they are not found, then\r
180 // no adjustments can be made to the Memory Type Information variable.\r
181 //\r
182 Status = EfiGetSystemConfigurationTable (\r
183 &gEfiMemoryTypeInformationGuid,\r
184 (VOID **) &CurrentMemoryTypeInformation\r
185 );\r
186 if (EFI_ERROR (Status) || CurrentMemoryTypeInformation == NULL) {\r
187 return;\r
188 }\r
189\r
190 //\r
191 // Get the Memory Type Information settings from Hob if they exist,\r
192 // PEI is responsible for getting them from variable and build a Hob to save them.\r
193 // If the previous Memory Type Information is not available, then set defaults\r
194 //\r
195 GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);\r
196 if (GuidHob == NULL) {\r
197 //\r
198 // If Platform has not built Memory Type Info into the Hob, just return.\r
199 //\r
200 return;\r
201 }\r
0dc3359a
RN
202 VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);\r
203 PreviousMemoryTypeInformation = AllocateCopyPool (VariableSize, GET_GUID_HOB_DATA (GuidHob));\r
204 if (PreviousMemoryTypeInformation == NULL) {\r
205 return;\r
206 }\r
067ed98a
RN
207\r
208 //\r
209 // Use a heuristic to adjust the Memory Type Information for the next boot\r
210 //\r
211 DEBUG ((EFI_D_INFO, "Memory Previous Current Next \n"));\r
212 DEBUG ((EFI_D_INFO, " Type Pages Pages Pages \n"));\r
213 DEBUG ((EFI_D_INFO, "====== ======== ======== ========\n"));\r
214\r
215 for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
216\r
217 for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {\r
218 if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {\r
219 break;\r
220 }\r
221 }\r
222 if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {\r
223 continue;\r
224 }\r
225\r
226 //\r
227 // Previous is the number of pages pre-allocated\r
228 // Current is the number of pages actually needed\r
229 //\r
230 Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;\r
231 Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;\r
232 Next = Previous;\r
233\r
234 //\r
235 // Inconsistent Memory Reserved across bootings may lead to S4 fail\r
236 // Write next varible to 125% * current when the pre-allocated memory is:\r
237 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING\r
238 // 2. Less than the needed memory\r
239 //\r
240 if ((Current + (Current >> 1)) < Previous) {\r
241 if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {\r
242 Next = Current + (Current >> 2);\r
243 }\r
244 } else if (Current > Previous) {\r
245 Next = Current + (Current >> 2);\r
246 }\r
247 if (Next > 0 && Next < 4) {\r
248 Next = 4;\r
249 }\r
250\r
251 if (Next != Previous) {\r
252 PreviousMemoryTypeInformation[Index].NumberOfPages = Next;\r
253 MemoryTypeInformationModified = TRUE;\r
254 }\r
255\r
256 DEBUG ((EFI_D_INFO, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation[Index].Type, Previous, Current, Next));\r
257 }\r
258\r
259 //\r
260 // If any changes were made to the Memory Type Information settings, then set the new variable value;\r
261 // Or create the variable in first boot.\r
262 //\r
263 if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) {\r
264 Status = BmSetVariableAndReportStatusCodeOnError (\r
265 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
266 &gEfiMemoryTypeInformationGuid,\r
267 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
268 VariableSize,\r
269 PreviousMemoryTypeInformation\r
270 );\r
271\r
272 if (!EFI_ERROR (Status)) {\r
273 //\r
665b26ba
RN
274 // If the Memory Type Information settings have been modified and the boot option belongs to boot category,\r
275 // then reset the platform so the new Memory Type Information setting will be used to guarantee that an S4\r
067ed98a
RN
276 // entry/resume cycle will not fail.\r
277 //\r
0dc3359a
RN
278 if (MemoryTypeInformationModified) {\r
279 DEBUG ((EFI_D_INFO, "Memory Type Information settings change.\n"));\r
280 if (Boot && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
281 DEBUG ((EFI_D_INFO, "...Warm Reset!!!\n"));\r
282 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
283 }\r
067ed98a
RN
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
0dc3359a 289 FreePool (PreviousMemoryTypeInformation);\r
067ed98a
RN
290}\r
291\r
292/**\r
293 Set the variable and report the error through status code upon failure.\r
294\r
295 @param VariableName A Null-terminated string that is the name of the vendor's variable.\r
296 Each VariableName is unique for each VendorGuid. VariableName must\r
297 contain 1 or more characters. If VariableName is an empty string,\r
298 then EFI_INVALID_PARAMETER is returned.\r
299 @param VendorGuid A unique identifier for the vendor.\r
300 @param Attributes Attributes bitmask to set for the variable.\r
d1102dba 301 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,\r
4073f85d 302 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero\r
d1102dba
LG
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
067ed98a
RN
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
4073f85d
ZC
320 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS\r
321 being set, but the AuthInfo does NOT pass the validation check carried out by the firmware.\r
067ed98a
RN
322\r
323 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
324**/\r
325EFI_STATUS\r
326BmSetVariableAndReportStatusCodeOnError (\r
327 IN CHAR16 *VariableName,\r
328 IN EFI_GUID *VendorGuid,\r
329 IN UINT32 Attributes,\r
330 IN UINTN DataSize,\r
331 IN VOID *Data\r
332 )\r
333{\r
334 EFI_STATUS Status;\r
335 EDKII_SET_VARIABLE_STATUS *SetVariableStatus;\r
336 UINTN NameSize;\r
337\r
338 Status = gRT->SetVariable (\r
339 VariableName,\r
340 VendorGuid,\r
341 Attributes,\r
342 DataSize,\r
343 Data\r
344 );\r
345 if (EFI_ERROR (Status)) {\r
346 NameSize = StrSize (VariableName);\r
347 SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);\r
348 if (SetVariableStatus != NULL) {\r
349 CopyGuid (&SetVariableStatus->Guid, VendorGuid);\r
350 SetVariableStatus->NameSize = NameSize;\r
351 SetVariableStatus->DataSize = DataSize;\r
352 SetVariableStatus->SetStatus = Status;\r
353 SetVariableStatus->Attributes = Attributes;\r
354 CopyMem (SetVariableStatus + 1, VariableName, NameSize);\r
355 CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data, DataSize);\r
356\r
357 REPORT_STATUS_CODE_EX (\r
358 EFI_ERROR_CODE,\r
359 PcdGet32 (PcdErrorCodeSetVariable),\r
360 0,\r
361 NULL,\r
362 &gEdkiiStatusCodeDataTypeVariableGuid,\r
363 SetVariableStatus,\r
364 sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize\r
365 );\r
366\r
367 FreePool (SetVariableStatus);\r
368 }\r
369 }\r
370\r
371 return Status;\r
372}\r
373\r
374\r
375/**\r
376 Print the device path info.\r
377\r
378 @param DevicePath The device path need to print.\r
379**/\r
380VOID\r
381BmPrintDp (\r
382 EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
383 )\r
384{\r
385 CHAR16 *Str;\r
386\r
387 Str = ConvertDevicePathToText (DevicePath, FALSE, FALSE);\r
388 DEBUG ((EFI_D_INFO, "%s", Str));\r
389 if (Str != NULL) {\r
390 FreePool (Str);\r
391 }\r
392}\r
418e8cd9
RN
393\r
394/**\r
395 Convert a single character to number.\r
396 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'\r
397\r
398 @param Char The input char which need to convert to int.\r
399\r
400 @return The converted 8-bit number or (UINTN) -1 if conversion failed.\r
401**/\r
402UINTN\r
403BmCharToUint (\r
404 IN CHAR16 Char\r
405 )\r
406{\r
407 if ((Char >= L'0') && (Char <= L'9')) {\r
16f69227 408 return (Char - L'0');\r
418e8cd9
RN
409 }\r
410\r
411 if ((Char >= L'A') && (Char <= L'F')) {\r
16f69227 412 return (Char - L'A' + 0xA);\r
418e8cd9
RN
413 }\r
414\r
418e8cd9
RN
415 return (UINTN) -1;\r
416}\r
417\r
b33af221
RN
418/**\r
419 Dispatch the deferred images that are returned from all DeferredImageLoad instances.\r
420\r
421 @retval EFI_SUCCESS At least one deferred image is loaded successfully and started.\r
422 @retval EFI_NOT_FOUND There is no deferred image.\r
423 @retval EFI_ACCESS_DENIED There are deferred images but all of them are failed to load.\r
424**/\r
425EFI_STATUS\r
426EFIAPI\r
427EfiBootManagerDispatchDeferredImages (\r
428 VOID\r
429 )\r
430{\r
431 EFI_STATUS Status;\r
432 EFI_DEFERRED_IMAGE_LOAD_PROTOCOL *DeferredImage;\r
433 UINTN HandleCount;\r
434 EFI_HANDLE *Handles;\r
435 UINTN Index;\r
436 UINTN ImageIndex;\r
437 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;\r
438 VOID *Image;\r
439 UINTN ImageSize;\r
440 BOOLEAN BootOption;\r
441 EFI_HANDLE ImageHandle;\r
442 UINTN ExitDataSize;\r
443 CHAR16 *ExitData;\r
444 UINTN ImageCount;\r
445 UINTN LoadCount;\r
446\r
447 //\r
448 // Find all the deferred image load protocols.\r
449 //\r
450 HandleCount = 0;\r
451 Handles = NULL;\r
452 Status = gBS->LocateHandleBuffer (\r
453 ByProtocol,\r
454 &gEfiDeferredImageLoadProtocolGuid,\r
455 NULL,\r
456 &HandleCount,\r
457 &Handles\r
458 );\r
459 if (EFI_ERROR (Status)) {\r
460 return EFI_NOT_FOUND;\r
461 }\r
462\r
463 ImageCount = 0;\r
464 LoadCount = 0;\r
465 for (Index = 0; Index < HandleCount; Index++) {\r
466 Status = gBS->HandleProtocol (Handles[Index], &gEfiDeferredImageLoadProtocolGuid, (VOID **) &DeferredImage);\r
467 if (EFI_ERROR (Status)) {\r
468 continue;\r
469 }\r
470\r
471 for (ImageIndex = 0; ;ImageIndex++) {\r
472 //\r
473 // Load all the deferred images in this protocol instance.\r
474 //\r
475 Status = DeferredImage->GetImageInfo (\r
476 DeferredImage,\r
477 ImageIndex,\r
478 &ImageDevicePath,\r
479 (VOID **) &Image,\r
480 &ImageSize,\r
481 &BootOption\r
482 );\r
483 if (EFI_ERROR (Status)) {\r
484 break;\r
485 }\r
486 ImageCount++;\r
487 //\r
488 // Load and start the image.\r
489 //\r
490 Status = gBS->LoadImage (\r
491 BootOption,\r
492 gImageHandle,\r
493 ImageDevicePath,\r
494 NULL,\r
495 0,\r
496 &ImageHandle\r
497 );\r
498 if (!EFI_ERROR (Status)) {\r
499 LoadCount++;\r
500 //\r
501 // Before calling the image, enable the Watchdog Timer for\r
502 // a 5 Minute period\r
503 //\r
504 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
505 Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);\r
506 if (ExitData != NULL) {\r
507 FreePool (ExitData);\r
508 }\r
509\r
510 //\r
511 // Clear the Watchdog Timer after the image returns.\r
512 //\r
513 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
514 }\r
515 }\r
516 }\r
517 if (Handles != NULL) {\r
518 FreePool (Handles);\r
519 }\r
520\r
521 if (ImageCount == 0) {\r
522 return EFI_NOT_FOUND;\r
523 } else {\r
524 if (LoadCount == 0) {\r
525 return EFI_ACCESS_DENIED;\r
526 } else {\r
527 return EFI_SUCCESS;\r
528 }\r
529 }\r
530}\r