]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
MdeModulePkg/UefiBootManagerLib: Generate boot description for NVME
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / BmMisc.c
CommitLineData
067ed98a
RN
1/** @file\r
2 Misc library functions.\r
3\r
0dc3359a 4Copyright (c) 2011 - 2017, 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
0dc3359a
RN
208 VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);\r
209 PreviousMemoryTypeInformation = AllocateCopyPool (VariableSize, GET_GUID_HOB_DATA (GuidHob));\r
210 if (PreviousMemoryTypeInformation == NULL) {\r
211 return;\r
212 }\r
067ed98a
RN
213\r
214 //\r
215 // Use a heuristic to adjust the Memory Type Information for the next boot\r
216 //\r
217 DEBUG ((EFI_D_INFO, "Memory Previous Current Next \n"));\r
218 DEBUG ((EFI_D_INFO, " Type Pages Pages Pages \n"));\r
219 DEBUG ((EFI_D_INFO, "====== ======== ======== ========\n"));\r
220\r
221 for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
222\r
223 for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {\r
224 if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {\r
225 break;\r
226 }\r
227 }\r
228 if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {\r
229 continue;\r
230 }\r
231\r
232 //\r
233 // Previous is the number of pages pre-allocated\r
234 // Current is the number of pages actually needed\r
235 //\r
236 Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;\r
237 Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;\r
238 Next = Previous;\r
239\r
240 //\r
241 // Inconsistent Memory Reserved across bootings may lead to S4 fail\r
242 // Write next varible to 125% * current when the pre-allocated memory is:\r
243 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING\r
244 // 2. Less than the needed memory\r
245 //\r
246 if ((Current + (Current >> 1)) < Previous) {\r
247 if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {\r
248 Next = Current + (Current >> 2);\r
249 }\r
250 } else if (Current > Previous) {\r
251 Next = Current + (Current >> 2);\r
252 }\r
253 if (Next > 0 && Next < 4) {\r
254 Next = 4;\r
255 }\r
256\r
257 if (Next != Previous) {\r
258 PreviousMemoryTypeInformation[Index].NumberOfPages = Next;\r
259 MemoryTypeInformationModified = TRUE;\r
260 }\r
261\r
262 DEBUG ((EFI_D_INFO, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation[Index].Type, Previous, Current, Next));\r
263 }\r
264\r
265 //\r
266 // If any changes were made to the Memory Type Information settings, then set the new variable value;\r
267 // Or create the variable in first boot.\r
268 //\r
269 if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) {\r
270 Status = BmSetVariableAndReportStatusCodeOnError (\r
271 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
272 &gEfiMemoryTypeInformationGuid,\r
273 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
274 VariableSize,\r
275 PreviousMemoryTypeInformation\r
276 );\r
277\r
278 if (!EFI_ERROR (Status)) {\r
279 //\r
665b26ba
RN
280 // If the Memory Type Information settings have been modified and the boot option belongs to boot category,\r
281 // then reset the platform so the new Memory Type Information setting will be used to guarantee that an S4\r
067ed98a
RN
282 // entry/resume cycle will not fail.\r
283 //\r
0dc3359a
RN
284 if (MemoryTypeInformationModified) {\r
285 DEBUG ((EFI_D_INFO, "Memory Type Information settings change.\n"));\r
286 if (Boot && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
287 DEBUG ((EFI_D_INFO, "...Warm Reset!!!\n"));\r
288 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
289 }\r
067ed98a
RN
290 }\r
291 } else {\r
292 DEBUG ((EFI_D_ERROR, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));\r
293 }\r
294 }\r
0dc3359a 295 FreePool (PreviousMemoryTypeInformation);\r
067ed98a
RN
296}\r
297\r
298/**\r
299 Set the variable and report the error through status code upon failure.\r
300\r
301 @param VariableName A Null-terminated string that is the name of the vendor's variable.\r
302 Each VariableName is unique for each VendorGuid. VariableName must\r
303 contain 1 or more characters. If VariableName is an empty string,\r
304 then EFI_INVALID_PARAMETER is returned.\r
305 @param VendorGuid A unique identifier for the vendor.\r
306 @param Attributes Attributes bitmask to set for the variable.\r
307 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE, \r
308 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or \r
309 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero \r
310 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is \r
311 set, then a SetVariable() call with a DataSize of zero will not cause any change to \r
312 the variable value (the timestamp associated with the variable may be updated however \r
313 even if no new data value is provided,see the description of the \r
314 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not \r
315 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated). \r
316 @param Data The contents for the variable.\r
317\r
318 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as\r
319 defined by the Attributes.\r
320 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the\r
321 DataSize exceeds the maximum allowed.\r
322 @retval EFI_INVALID_PARAMETER VariableName is an empty string.\r
323 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
324 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.\r
325 @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
326 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
327 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS \r
328 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo \r
329 does NOT pass the validation check carried out by the firmware.\r
330\r
331 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
332**/\r
333EFI_STATUS\r
334BmSetVariableAndReportStatusCodeOnError (\r
335 IN CHAR16 *VariableName,\r
336 IN EFI_GUID *VendorGuid,\r
337 IN UINT32 Attributes,\r
338 IN UINTN DataSize,\r
339 IN VOID *Data\r
340 )\r
341{\r
342 EFI_STATUS Status;\r
343 EDKII_SET_VARIABLE_STATUS *SetVariableStatus;\r
344 UINTN NameSize;\r
345\r
346 Status = gRT->SetVariable (\r
347 VariableName,\r
348 VendorGuid,\r
349 Attributes,\r
350 DataSize,\r
351 Data\r
352 );\r
353 if (EFI_ERROR (Status)) {\r
354 NameSize = StrSize (VariableName);\r
355 SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);\r
356 if (SetVariableStatus != NULL) {\r
357 CopyGuid (&SetVariableStatus->Guid, VendorGuid);\r
358 SetVariableStatus->NameSize = NameSize;\r
359 SetVariableStatus->DataSize = DataSize;\r
360 SetVariableStatus->SetStatus = Status;\r
361 SetVariableStatus->Attributes = Attributes;\r
362 CopyMem (SetVariableStatus + 1, VariableName, NameSize);\r
363 CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data, DataSize);\r
364\r
365 REPORT_STATUS_CODE_EX (\r
366 EFI_ERROR_CODE,\r
367 PcdGet32 (PcdErrorCodeSetVariable),\r
368 0,\r
369 NULL,\r
370 &gEdkiiStatusCodeDataTypeVariableGuid,\r
371 SetVariableStatus,\r
372 sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize\r
373 );\r
374\r
375 FreePool (SetVariableStatus);\r
376 }\r
377 }\r
378\r
379 return Status;\r
380}\r
381\r
382\r
383/**\r
384 Print the device path info.\r
385\r
386 @param DevicePath The device path need to print.\r
387**/\r
388VOID\r
389BmPrintDp (\r
390 EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
391 )\r
392{\r
393 CHAR16 *Str;\r
394\r
395 Str = ConvertDevicePathToText (DevicePath, FALSE, FALSE);\r
396 DEBUG ((EFI_D_INFO, "%s", Str));\r
397 if (Str != NULL) {\r
398 FreePool (Str);\r
399 }\r
400}\r
418e8cd9
RN
401\r
402/**\r
403 Convert a single character to number.\r
404 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'\r
405\r
406 @param Char The input char which need to convert to int.\r
407\r
408 @return The converted 8-bit number or (UINTN) -1 if conversion failed.\r
409**/\r
410UINTN\r
411BmCharToUint (\r
412 IN CHAR16 Char\r
413 )\r
414{\r
415 if ((Char >= L'0') && (Char <= L'9')) {\r
16f69227 416 return (Char - L'0');\r
418e8cd9
RN
417 }\r
418\r
419 if ((Char >= L'A') && (Char <= L'F')) {\r
16f69227 420 return (Char - L'A' + 0xA);\r
418e8cd9
RN
421 }\r
422\r
423 ASSERT (FALSE);\r
424 return (UINTN) -1;\r
425}\r
426\r
b33af221
RN
427/**\r
428 Dispatch the deferred images that are returned from all DeferredImageLoad instances.\r
429\r
430 @retval EFI_SUCCESS At least one deferred image is loaded successfully and started.\r
431 @retval EFI_NOT_FOUND There is no deferred image.\r
432 @retval EFI_ACCESS_DENIED There are deferred images but all of them are failed to load.\r
433**/\r
434EFI_STATUS\r
435EFIAPI\r
436EfiBootManagerDispatchDeferredImages (\r
437 VOID\r
438 )\r
439{\r
440 EFI_STATUS Status;\r
441 EFI_DEFERRED_IMAGE_LOAD_PROTOCOL *DeferredImage;\r
442 UINTN HandleCount;\r
443 EFI_HANDLE *Handles;\r
444 UINTN Index;\r
445 UINTN ImageIndex;\r
446 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;\r
447 VOID *Image;\r
448 UINTN ImageSize;\r
449 BOOLEAN BootOption;\r
450 EFI_HANDLE ImageHandle;\r
451 UINTN ExitDataSize;\r
452 CHAR16 *ExitData;\r
453 UINTN ImageCount;\r
454 UINTN LoadCount;\r
455\r
456 //\r
457 // Find all the deferred image load protocols.\r
458 //\r
459 HandleCount = 0;\r
460 Handles = NULL;\r
461 Status = gBS->LocateHandleBuffer (\r
462 ByProtocol,\r
463 &gEfiDeferredImageLoadProtocolGuid,\r
464 NULL,\r
465 &HandleCount,\r
466 &Handles\r
467 );\r
468 if (EFI_ERROR (Status)) {\r
469 return EFI_NOT_FOUND;\r
470 }\r
471\r
472 ImageCount = 0;\r
473 LoadCount = 0;\r
474 for (Index = 0; Index < HandleCount; Index++) {\r
475 Status = gBS->HandleProtocol (Handles[Index], &gEfiDeferredImageLoadProtocolGuid, (VOID **) &DeferredImage);\r
476 if (EFI_ERROR (Status)) {\r
477 continue;\r
478 }\r
479\r
480 for (ImageIndex = 0; ;ImageIndex++) {\r
481 //\r
482 // Load all the deferred images in this protocol instance.\r
483 //\r
484 Status = DeferredImage->GetImageInfo (\r
485 DeferredImage,\r
486 ImageIndex,\r
487 &ImageDevicePath,\r
488 (VOID **) &Image,\r
489 &ImageSize,\r
490 &BootOption\r
491 );\r
492 if (EFI_ERROR (Status)) {\r
493 break;\r
494 }\r
495 ImageCount++;\r
496 //\r
497 // Load and start the image.\r
498 //\r
499 Status = gBS->LoadImage (\r
500 BootOption,\r
501 gImageHandle,\r
502 ImageDevicePath,\r
503 NULL,\r
504 0,\r
505 &ImageHandle\r
506 );\r
507 if (!EFI_ERROR (Status)) {\r
508 LoadCount++;\r
509 //\r
510 // Before calling the image, enable the Watchdog Timer for\r
511 // a 5 Minute period\r
512 //\r
513 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
514 Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);\r
515 if (ExitData != NULL) {\r
516 FreePool (ExitData);\r
517 }\r
518\r
519 //\r
520 // Clear the Watchdog Timer after the image returns.\r
521 //\r
522 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
523 }\r
524 }\r
525 }\r
526 if (Handles != NULL) {\r
527 FreePool (Handles);\r
528 }\r
529\r
530 if (ImageCount == 0) {\r
531 return EFI_NOT_FOUND;\r
532 } else {\r
533 if (LoadCount == 0) {\r
534 return EFI_ACCESS_DENIED;\r
535 } else {\r
536 return EFI_SUCCESS;\r
537 }\r
538 }\r
539}\r