]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
MdeModulePkg UsbBusPei: Fix wrong buffer length used to read hub desc
[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
4073f85d 308 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero\r
067ed98a
RN
309 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is \r
310 set, then a SetVariable() call with a DataSize of zero will not cause any change to \r
311 the variable value (the timestamp associated with the variable may be updated however \r
312 even if no new data value is provided,see the description of the \r
313 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not \r
314 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated). \r
315 @param Data The contents for the variable.\r
316\r
317 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as\r
318 defined by the Attributes.\r
319 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the\r
320 DataSize exceeds the maximum allowed.\r
321 @retval EFI_INVALID_PARAMETER VariableName is an empty string.\r
322 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
323 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.\r
324 @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
325 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
4073f85d
ZC
326 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS\r
327 being set, but the AuthInfo does NOT pass the validation check carried out by the firmware.\r
067ed98a
RN
328\r
329 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
330**/\r
331EFI_STATUS\r
332BmSetVariableAndReportStatusCodeOnError (\r
333 IN CHAR16 *VariableName,\r
334 IN EFI_GUID *VendorGuid,\r
335 IN UINT32 Attributes,\r
336 IN UINTN DataSize,\r
337 IN VOID *Data\r
338 )\r
339{\r
340 EFI_STATUS Status;\r
341 EDKII_SET_VARIABLE_STATUS *SetVariableStatus;\r
342 UINTN NameSize;\r
343\r
344 Status = gRT->SetVariable (\r
345 VariableName,\r
346 VendorGuid,\r
347 Attributes,\r
348 DataSize,\r
349 Data\r
350 );\r
351 if (EFI_ERROR (Status)) {\r
352 NameSize = StrSize (VariableName);\r
353 SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);\r
354 if (SetVariableStatus != NULL) {\r
355 CopyGuid (&SetVariableStatus->Guid, VendorGuid);\r
356 SetVariableStatus->NameSize = NameSize;\r
357 SetVariableStatus->DataSize = DataSize;\r
358 SetVariableStatus->SetStatus = Status;\r
359 SetVariableStatus->Attributes = Attributes;\r
360 CopyMem (SetVariableStatus + 1, VariableName, NameSize);\r
361 CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data, DataSize);\r
362\r
363 REPORT_STATUS_CODE_EX (\r
364 EFI_ERROR_CODE,\r
365 PcdGet32 (PcdErrorCodeSetVariable),\r
366 0,\r
367 NULL,\r
368 &gEdkiiStatusCodeDataTypeVariableGuid,\r
369 SetVariableStatus,\r
370 sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize\r
371 );\r
372\r
373 FreePool (SetVariableStatus);\r
374 }\r
375 }\r
376\r
377 return Status;\r
378}\r
379\r
380\r
381/**\r
382 Print the device path info.\r
383\r
384 @param DevicePath The device path need to print.\r
385**/\r
386VOID\r
387BmPrintDp (\r
388 EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
389 )\r
390{\r
391 CHAR16 *Str;\r
392\r
393 Str = ConvertDevicePathToText (DevicePath, FALSE, FALSE);\r
394 DEBUG ((EFI_D_INFO, "%s", Str));\r
395 if (Str != NULL) {\r
396 FreePool (Str);\r
397 }\r
398}\r
418e8cd9
RN
399\r
400/**\r
401 Convert a single character to number.\r
402 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'\r
403\r
404 @param Char The input char which need to convert to int.\r
405\r
406 @return The converted 8-bit number or (UINTN) -1 if conversion failed.\r
407**/\r
408UINTN\r
409BmCharToUint (\r
410 IN CHAR16 Char\r
411 )\r
412{\r
413 if ((Char >= L'0') && (Char <= L'9')) {\r
16f69227 414 return (Char - L'0');\r
418e8cd9
RN
415 }\r
416\r
417 if ((Char >= L'A') && (Char <= L'F')) {\r
16f69227 418 return (Char - L'A' + 0xA);\r
418e8cd9
RN
419 }\r
420\r
418e8cd9
RN
421 return (UINTN) -1;\r
422}\r
423\r
b33af221
RN
424/**\r
425 Dispatch the deferred images that are returned from all DeferredImageLoad instances.\r
426\r
427 @retval EFI_SUCCESS At least one deferred image is loaded successfully and started.\r
428 @retval EFI_NOT_FOUND There is no deferred image.\r
429 @retval EFI_ACCESS_DENIED There are deferred images but all of them are failed to load.\r
430**/\r
431EFI_STATUS\r
432EFIAPI\r
433EfiBootManagerDispatchDeferredImages (\r
434 VOID\r
435 )\r
436{\r
437 EFI_STATUS Status;\r
438 EFI_DEFERRED_IMAGE_LOAD_PROTOCOL *DeferredImage;\r
439 UINTN HandleCount;\r
440 EFI_HANDLE *Handles;\r
441 UINTN Index;\r
442 UINTN ImageIndex;\r
443 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;\r
444 VOID *Image;\r
445 UINTN ImageSize;\r
446 BOOLEAN BootOption;\r
447 EFI_HANDLE ImageHandle;\r
448 UINTN ExitDataSize;\r
449 CHAR16 *ExitData;\r
450 UINTN ImageCount;\r
451 UINTN LoadCount;\r
452\r
453 //\r
454 // Find all the deferred image load protocols.\r
455 //\r
456 HandleCount = 0;\r
457 Handles = NULL;\r
458 Status = gBS->LocateHandleBuffer (\r
459 ByProtocol,\r
460 &gEfiDeferredImageLoadProtocolGuid,\r
461 NULL,\r
462 &HandleCount,\r
463 &Handles\r
464 );\r
465 if (EFI_ERROR (Status)) {\r
466 return EFI_NOT_FOUND;\r
467 }\r
468\r
469 ImageCount = 0;\r
470 LoadCount = 0;\r
471 for (Index = 0; Index < HandleCount; Index++) {\r
472 Status = gBS->HandleProtocol (Handles[Index], &gEfiDeferredImageLoadProtocolGuid, (VOID **) &DeferredImage);\r
473 if (EFI_ERROR (Status)) {\r
474 continue;\r
475 }\r
476\r
477 for (ImageIndex = 0; ;ImageIndex++) {\r
478 //\r
479 // Load all the deferred images in this protocol instance.\r
480 //\r
481 Status = DeferredImage->GetImageInfo (\r
482 DeferredImage,\r
483 ImageIndex,\r
484 &ImageDevicePath,\r
485 (VOID **) &Image,\r
486 &ImageSize,\r
487 &BootOption\r
488 );\r
489 if (EFI_ERROR (Status)) {\r
490 break;\r
491 }\r
492 ImageCount++;\r
493 //\r
494 // Load and start the image.\r
495 //\r
496 Status = gBS->LoadImage (\r
497 BootOption,\r
498 gImageHandle,\r
499 ImageDevicePath,\r
500 NULL,\r
501 0,\r
502 &ImageHandle\r
503 );\r
504 if (!EFI_ERROR (Status)) {\r
505 LoadCount++;\r
506 //\r
507 // Before calling the image, enable the Watchdog Timer for\r
508 // a 5 Minute period\r
509 //\r
510 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
511 Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);\r
512 if (ExitData != NULL) {\r
513 FreePool (ExitData);\r
514 }\r
515\r
516 //\r
517 // Clear the Watchdog Timer after the image returns.\r
518 //\r
519 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
520 }\r
521 }\r
522 }\r
523 if (Handles != NULL) {\r
524 FreePool (Handles);\r
525 }\r
526\r
527 if (ImageCount == 0) {\r
528 return EFI_NOT_FOUND;\r
529 } else {\r
530 if (LoadCount == 0) {\r
531 return EFI_ACCESS_DENIED;\r
532 } else {\r
533 return EFI_SUCCESS;\r
534 }\r
535 }\r
536}\r