]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c
Add check when get boot option variable to avoid system hang.
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / GenericBdsLib / BdsMisc.c
CommitLineData
5c08e117 1/** @file\r
2 Misc BDS library function\r
3\r
a97a8596 4Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>\r
180a5a35 5This program and the accompanying materials\r
5c08e117 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "InternalBdsLib.h"\r
16\r
17\r
18#define MAX_STRING_LEN 200\r
19\r
20BOOLEAN mFeaturerSwitch = TRUE;\r
21BOOLEAN mResetRequired = FALSE;\r
22\r
23extern UINT16 gPlatformBootTimeOutDefault;\r
24\r
5c08e117 25/**\r
26 The function will go through the driver option link list, load and start\r
27 every driver the driver option device path point to.\r
28\r
29 @param BdsDriverLists The header of the current driver option link list\r
30\r
31**/\r
32VOID\r
33EFIAPI\r
34BdsLibLoadDrivers (\r
35 IN LIST_ENTRY *BdsDriverLists\r
36 )\r
37{\r
38 EFI_STATUS Status;\r
39 LIST_ENTRY *Link;\r
40 BDS_COMMON_OPTION *Option;\r
41 EFI_HANDLE ImageHandle;\r
42 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
43 UINTN ExitDataSize;\r
44 CHAR16 *ExitData;\r
45 BOOLEAN ReconnectAll;\r
46\r
47 ReconnectAll = FALSE;\r
48\r
49 //\r
50 // Process the driver option\r
51 //\r
52 for (Link = BdsDriverLists->ForwardLink; Link != BdsDriverLists; Link = Link->ForwardLink) {\r
53 Option = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);\r
54 \r
55 //\r
56 // If a load option is not marked as LOAD_OPTION_ACTIVE,\r
57 // the boot manager will not automatically load the option.\r
58 //\r
59 if (!IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_ACTIVE)) {\r
60 continue;\r
61 }\r
62 \r
63 //\r
64 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,\r
65 // then all of the EFI drivers in the system will be disconnected and\r
66 // reconnected after the last driver load option is processed.\r
67 //\r
68 if (IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_FORCE_RECONNECT)) {\r
69 ReconnectAll = TRUE;\r
70 }\r
71 \r
72 //\r
73 // Make sure the driver path is connected.\r
74 //\r
75 BdsLibConnectDevicePath (Option->DevicePath);\r
76\r
77 //\r
78 // Load and start the image that Driver#### describes\r
79 //\r
80 Status = gBS->LoadImage (\r
81 FALSE,\r
fefefa4c 82 gImageHandle,\r
5c08e117 83 Option->DevicePath,\r
84 NULL,\r
85 0,\r
86 &ImageHandle\r
87 );\r
88\r
89 if (!EFI_ERROR (Status)) {\r
90 gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
91\r
92 //\r
93 // Verify whether this image is a driver, if not,\r
94 // exit it and continue to parse next load option\r
95 //\r
96 if (ImageInfo->ImageCodeType != EfiBootServicesCode && ImageInfo->ImageCodeType != EfiRuntimeServicesCode) {\r
97 gBS->Exit (ImageHandle, EFI_INVALID_PARAMETER, 0, NULL);\r
98 continue;\r
99 }\r
100\r
101 if (Option->LoadOptionsSize != 0) {\r
102 ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;\r
103 ImageInfo->LoadOptions = Option->LoadOptions;\r
104 }\r
105 //\r
106 // Before calling the image, enable the Watchdog Timer for\r
107 // the 5 Minute period\r
108 //\r
109 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
110\r
111 Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);\r
112 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Driver Return Status = %r\n", Status));\r
113\r
114 //\r
115 // Clear the Watchdog Timer after the image returns\r
116 //\r
117 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
118 }\r
119 }\r
120 \r
121 //\r
122 // Process the LOAD_OPTION_FORCE_RECONNECT driver option\r
123 //\r
124 if (ReconnectAll) {\r
125 BdsLibDisconnectAllEfi ();\r
126 BdsLibConnectAll ();\r
127 }\r
128\r
129}\r
130\r
131/**\r
132 Get the Option Number that does not used.\r
133 Try to locate the specific option variable one by one utile find a free number.\r
134\r
135 @param VariableName Indicate if the boot#### or driver#### option\r
136\r
137 @return The Minimal Free Option Number\r
138\r
139**/\r
140UINT16\r
141BdsLibGetFreeOptionNumber (\r
142 IN CHAR16 *VariableName\r
143 )\r
144{\r
145 UINTN Index;\r
146 CHAR16 StrTemp[10];\r
147 UINT16 *OptionBuffer;\r
148 UINTN OptionSize;\r
149\r
150 //\r
151 // Try to find the minimum free number from 0, 1, 2, 3....\r
152 //\r
153 Index = 0;\r
154 do {\r
155 if (*VariableName == 'B') {\r
156 UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Boot%04x", Index);\r
157 } else {\r
158 UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Driver%04x", Index);\r
159 }\r
160 //\r
161 // try if the option number is used\r
162 //\r
163 OptionBuffer = BdsLibGetVariableAndSize (\r
164 StrTemp,\r
165 &gEfiGlobalVariableGuid,\r
166 &OptionSize\r
167 );\r
168 if (OptionBuffer == NULL) {\r
169 break;\r
170 }\r
171 Index++;\r
172 } while (TRUE);\r
173\r
174 return ((UINT16) Index);\r
175}\r
176\r
177\r
178/**\r
179 This function will register the new boot#### or driver#### option base on\r
180 the VariableName. The new registered boot#### or driver#### will be linked\r
181 to BdsOptionList and also update to the VariableName. After the boot#### or\r
182 driver#### updated, the BootOrder or DriverOrder will also be updated.\r
183\r
184 @param BdsOptionList The header of the boot#### or driver#### link list\r
185 @param DevicePath The device path which the boot#### or driver####\r
186 option present\r
187 @param String The description of the boot#### or driver####\r
188 @param VariableName Indicate if the boot#### or driver#### option\r
189\r
190 @retval EFI_SUCCESS The boot#### or driver#### have been success\r
191 registered\r
192 @retval EFI_STATUS Return the status of gRT->SetVariable ().\r
193\r
194**/\r
195EFI_STATUS\r
196EFIAPI\r
197BdsLibRegisterNewOption (\r
198 IN LIST_ENTRY *BdsOptionList,\r
199 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
200 IN CHAR16 *String,\r
201 IN CHAR16 *VariableName\r
202 )\r
203{\r
204 EFI_STATUS Status;\r
205 UINTN Index;\r
206 UINT16 RegisterOptionNumber;\r
207 UINT16 *TempOptionPtr;\r
208 UINTN TempOptionSize;\r
209 UINT16 *OptionOrderPtr;\r
210 VOID *OptionPtr;\r
211 UINTN OptionSize;\r
212 UINT8 *TempPtr;\r
213 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
214 CHAR16 *Description;\r
215 CHAR16 OptionName[10];\r
216 BOOLEAN UpdateDescription;\r
217 UINT16 BootOrderEntry;\r
218 UINTN OrderItemNum;\r
219\r
220\r
221 OptionPtr = NULL;\r
222 OptionSize = 0;\r
223 TempPtr = NULL;\r
224 OptionDevicePath = NULL;\r
225 Description = NULL;\r
226 OptionOrderPtr = NULL;\r
227 UpdateDescription = FALSE;\r
228 Status = EFI_SUCCESS;\r
229 ZeroMem (OptionName, sizeof (OptionName));\r
230\r
231 TempOptionSize = 0;\r
232 TempOptionPtr = BdsLibGetVariableAndSize (\r
233 VariableName,\r
234 &gEfiGlobalVariableGuid,\r
235 &TempOptionSize\r
236 );\r
237 //\r
238 // Compare with current option variable if the previous option is set in global variable.\r
239 //\r
240 for (Index = 0; Index < TempOptionSize / sizeof (UINT16); Index++) {\r
241 //\r
242 // TempOptionPtr must not be NULL if we have non-zero TempOptionSize.\r
243 //\r
244 ASSERT (TempOptionPtr != NULL);\r
245\r
246 if (*VariableName == 'B') {\r
247 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", TempOptionPtr[Index]);\r
248 } else {\r
249 UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", TempOptionPtr[Index]);\r
250 }\r
251\r
252 OptionPtr = BdsLibGetVariableAndSize (\r
253 OptionName,\r
254 &gEfiGlobalVariableGuid,\r
255 &OptionSize\r
256 );\r
257 if (OptionPtr == NULL) {\r
258 continue;\r
259 }\r
260 TempPtr = OptionPtr;\r
261 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
262 Description = (CHAR16 *) TempPtr;\r
263 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
264 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
265\r
266 //\r
267 // Notes: the description may will change base on the GetStringToken\r
268 //\r
269 if (CompareMem (OptionDevicePath, DevicePath, GetDevicePathSize (OptionDevicePath)) == 0) {\r
270 if (CompareMem (Description, String, StrSize (Description)) == 0) { \r
271 //\r
272 // Got the option, so just return\r
273 //\r
274 FreePool (OptionPtr);\r
275 FreePool (TempOptionPtr);\r
276 return EFI_SUCCESS;\r
277 } else {\r
278 //\r
279 // Option description changed, need update.\r
280 //\r
281 UpdateDescription = TRUE;\r
282 FreePool (OptionPtr);\r
283 break;\r
284 }\r
285 }\r
286\r
287 FreePool (OptionPtr);\r
288 }\r
289\r
290 OptionSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (String);\r
291 OptionSize += GetDevicePathSize (DevicePath);\r
292 OptionPtr = AllocateZeroPool (OptionSize);\r
293 ASSERT (OptionPtr != NULL);\r
294 \r
295 TempPtr = OptionPtr;\r
296 *(UINT32 *) TempPtr = LOAD_OPTION_ACTIVE;\r
297 TempPtr += sizeof (UINT32);\r
298 *(UINT16 *) TempPtr = (UINT16) GetDevicePathSize (DevicePath);\r
299 TempPtr += sizeof (UINT16);\r
300 CopyMem (TempPtr, String, StrSize (String));\r
301 TempPtr += StrSize (String);\r
302 CopyMem (TempPtr, DevicePath, GetDevicePathSize (DevicePath));\r
303\r
304 if (UpdateDescription) {\r
305 //\r
306 // The number in option#### to be updated. \r
307 // In this case, we must have non-NULL TempOptionPtr.\r
308 //\r
309 ASSERT (TempOptionPtr != NULL);\r
310 RegisterOptionNumber = TempOptionPtr[Index];\r
311 } else {\r
312 //\r
313 // The new option#### number\r
314 //\r
315 RegisterOptionNumber = BdsLibGetFreeOptionNumber(VariableName);\r
316 }\r
317\r
318 if (*VariableName == 'B') {\r
319 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", RegisterOptionNumber);\r
320 } else {\r
321 UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", RegisterOptionNumber);\r
322 }\r
323\r
324 Status = gRT->SetVariable (\r
325 OptionName,\r
326 &gEfiGlobalVariableGuid,\r
327 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
328 OptionSize,\r
329 OptionPtr\r
330 );\r
331 //\r
332 // Return if only need to update a changed description or fail to set option.\r
333 //\r
334 if (EFI_ERROR (Status) || UpdateDescription) {\r
335 FreePool (OptionPtr);\r
336 if (TempOptionPtr != NULL) {\r
337 FreePool (TempOptionPtr);\r
338 }\r
339 return Status;\r
340 }\r
341\r
342 FreePool (OptionPtr);\r
343\r
344 //\r
345 // Update the option order variable\r
346 //\r
347\r
348 //\r
349 // If no option order\r
350 //\r
351 if (TempOptionSize == 0) {\r
352 BootOrderEntry = 0;\r
353 Status = gRT->SetVariable (\r
354 VariableName,\r
355 &gEfiGlobalVariableGuid,\r
356 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
357 sizeof (UINT16),\r
358 &BootOrderEntry\r
359 );\r
360 if (TempOptionPtr != NULL) {\r
361 FreePool (TempOptionPtr);\r
362 }\r
363 return Status;\r
364 }\r
365 \r
366 //\r
367 // TempOptionPtr must not be NULL if TempOptionSize is not zero.\r
368 //\r
369 ASSERT (TempOptionPtr != NULL);\r
370 //\r
371 // Append the new option number to the original option order\r
372 //\r
373 OrderItemNum = (TempOptionSize / sizeof (UINT16)) + 1 ;\r
374 OptionOrderPtr = AllocateZeroPool ( OrderItemNum * sizeof (UINT16));\r
375 ASSERT (OptionOrderPtr!= NULL);\r
376 CopyMem (OptionOrderPtr, TempOptionPtr, (OrderItemNum - 1) * sizeof (UINT16));\r
377\r
378 OptionOrderPtr[Index] = RegisterOptionNumber;\r
379\r
380 Status = gRT->SetVariable (\r
381 VariableName,\r
382 &gEfiGlobalVariableGuid,\r
383 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
384 OrderItemNum * sizeof (UINT16),\r
385 OptionOrderPtr\r
386 );\r
387 FreePool (TempOptionPtr);\r
388 FreePool (OptionOrderPtr);\r
389\r
390 return Status;\r
391}\r
392\r
b16cc38b
ED
393/**\r
394 Returns the size of a device path in bytes.\r
395\r
396 This function returns the size, in bytes, of the device path data structure \r
397 specified by DevicePath including the end of device path node. If DevicePath \r
398 is NULL, then 0 is returned. If the length of the device path is bigger than\r
399 MaxSize, also return 0 to indicate this is an invalidate device path.\r
400\r
401 @param DevicePath A pointer to a device path data structure.\r
402 @param MaxSize Max valid device path size. If big than this size, \r
403 return error.\r
404 \r
405 @retval 0 An invalid device path.\r
406 @retval Others The size of a device path in bytes.\r
407\r
408**/\r
409UINTN\r
410GetDevicePathSizeEx (\r
411 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
412 IN UINTN MaxSize\r
413 )\r
414{\r
415 UINTN Size;\r
416 UINTN NodeSize;\r
417\r
418 if (DevicePath == NULL) {\r
419 return 0;\r
420 }\r
421\r
422 //\r
423 // Search for the end of the device path structure\r
424 //\r
425 Size = 0;\r
426 while (!IsDevicePathEnd (DevicePath)) {\r
427 NodeSize = DevicePathNodeLength (DevicePath);\r
428 if (NodeSize == 0) {\r
429 return 0;\r
430 }\r
431 Size += NodeSize;\r
432 if (Size > MaxSize) {\r
433 return 0;\r
434 }\r
435 DevicePath = NextDevicePathNode (DevicePath);\r
436 }\r
437 Size += DevicePathNodeLength (DevicePath);\r
438 if (Size > MaxSize) {\r
439 return 0;\r
440 }\r
441\r
442 return Size;\r
443}\r
444\r
445/**\r
446 Returns the length of a Null-terminated Unicode string. If the length is \r
447 bigger than MaxStringLen, return length 0 to indicate that this is an \r
448 invalidate string.\r
449\r
450 This function returns the number of Unicode characters in the Null-terminated\r
451 Unicode string specified by String. \r
452\r
453 If String is NULL, then ASSERT().\r
454 If String is not aligned on a 16-bit boundary, then ASSERT().\r
455\r
456 @param String A pointer to a Null-terminated Unicode string.\r
457 @param MaxStringLen Max string len in this string.\r
458\r
459 @retval 0 An invalid string.\r
460 @retval Others The length of String.\r
461\r
462**/\r
463UINTN\r
464StrSizeEx (\r
465 IN CONST CHAR16 *String,\r
466 IN UINTN MaxStringLen\r
467 )\r
468{\r
469 UINTN Length;\r
470\r
471 ASSERT (String != NULL && MaxStringLen != 0);\r
472 ASSERT (((UINTN) String & BIT0) == 0);\r
473\r
474 for (Length = 0; *String != L'\0' && MaxStringLen != Length; String++, Length++);\r
475\r
476 if (*String != L'\0' && MaxStringLen == Length) {\r
477 return 0;\r
478 }\r
479\r
480 return (Length + 1) * sizeof (*String);\r
481}\r
5c08e117 482\r
483/**\r
484 Build the boot#### or driver#### option from the VariableName, the\r
485 build boot#### or driver#### will also be linked to BdsCommonOptionList.\r
486\r
487 @param BdsCommonOptionList The header of the boot#### or driver#### option\r
488 link list\r
489 @param VariableName EFI Variable name indicate if it is boot#### or\r
490 driver####\r
491\r
492 @retval BDS_COMMON_OPTION Get the option just been created\r
493 @retval NULL Failed to get the new option\r
494\r
495**/\r
496BDS_COMMON_OPTION *\r
497EFIAPI\r
498BdsLibVariableToOption (\r
499 IN OUT LIST_ENTRY *BdsCommonOptionList,\r
500 IN CHAR16 *VariableName\r
501 )\r
502{\r
503 UINT32 Attribute;\r
504 UINT16 FilePathSize;\r
505 UINT8 *Variable;\r
506 UINT8 *TempPtr;\r
507 UINTN VariableSize;\r
508 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
b16cc38b 509 EFI_DEVICE_PATH_PROTOCOL *TempPath;\r
5c08e117 510 BDS_COMMON_OPTION *Option;\r
511 VOID *LoadOptions;\r
512 UINT32 LoadOptionsSize;\r
513 CHAR16 *Description;\r
514 UINT8 NumOff;\r
b16cc38b 515 UINTN TempSize;\r
5c08e117 516 //\r
517 // Read the variable. We will never free this data.\r
518 //\r
519 Variable = BdsLibGetVariableAndSize (\r
520 VariableName,\r
521 &gEfiGlobalVariableGuid,\r
522 &VariableSize\r
523 );\r
524 if (Variable == NULL) {\r
525 return NULL;\r
526 }\r
527 //\r
528 // Notes: careful defined the variable of Boot#### or\r
529 // Driver####, consider use some macro to abstract the code\r
530 //\r
531 //\r
532 // Get the option attribute\r
533 //\r
534 TempPtr = Variable;\r
535 Attribute = *(UINT32 *) Variable;\r
536 TempPtr += sizeof (UINT32);\r
537\r
538 //\r
539 // Get the option's device path size\r
540 //\r
541 FilePathSize = *(UINT16 *) TempPtr;\r
542 TempPtr += sizeof (UINT16);\r
543\r
544 //\r
545 // Get the option's description string\r
546 //\r
547 Description = (CHAR16 *) TempPtr;\r
548\r
549 //\r
550 // Get the option's description string size\r
551 //\r
b16cc38b
ED
552 TempSize = StrSizeEx ((CHAR16 *) TempPtr, VariableSize);\r
553 if (TempSize == 0) {\r
554 return NULL;\r
555 }\r
556 TempPtr += TempSize;\r
5c08e117 557\r
558 //\r
559 // Get the option's device path\r
560 //\r
561 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
562 TempPtr += FilePathSize;\r
563\r
b16cc38b
ED
564 //\r
565 // Validation device path.\r
566 //\r
567 TempPath = DevicePath;\r
568 while (FilePathSize > 0) {\r
569 TempSize = GetDevicePathSizeEx (TempPath, FilePathSize);\r
570 if (TempSize == 0) {\r
571 return NULL;\r
572 }\r
573 FilePathSize = (UINT16) (FilePathSize - TempSize);\r
574 TempPath += TempSize;\r
575 }\r
576\r
577 //\r
578 // Get load opion data.\r
579 //\r
5c08e117 580 LoadOptions = TempPtr;\r
b16cc38b
ED
581 if (VariableSize < (UINTN)(TempPtr - Variable)) {\r
582 return NULL;\r
583 }\r
5c08e117 584 LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable));\r
585\r
586 //\r
587 // The Console variables may have multiple device paths, so make\r
588 // an Entry for each one.\r
589 //\r
590 Option = AllocateZeroPool (sizeof (BDS_COMMON_OPTION));\r
591 if (Option == NULL) {\r
592 return NULL;\r
593 }\r
594\r
595 Option->Signature = BDS_LOAD_OPTION_SIGNATURE;\r
596 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));\r
597 ASSERT(Option->DevicePath != NULL);\r
598 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r
599\r
600 Option->Attribute = Attribute;\r
601 Option->Description = AllocateZeroPool (StrSize (Description));\r
602 ASSERT(Option->Description != NULL);\r
603 CopyMem (Option->Description, Description, StrSize (Description));\r
604\r
605 Option->LoadOptions = AllocateZeroPool (LoadOptionsSize);\r
606 ASSERT(Option->LoadOptions != NULL);\r
607 CopyMem (Option->LoadOptions, LoadOptions, LoadOptionsSize);\r
608 Option->LoadOptionsSize = LoadOptionsSize;\r
609\r
610 //\r
611 // Get the value from VariableName Unicode string\r
612 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this\r
613 // Unicode stream to ASCII without any loss in meaning.\r
614 //\r
615 if (*VariableName == 'B') {\r
13314ba3 616 NumOff = (UINT8) (sizeof (L"Boot") / sizeof(CHAR16) - 1);\r
5c08e117 617 Option->BootCurrent = (UINT16) ((VariableName[NumOff] -'0') * 0x1000);\r
618 Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+1]-'0') * 0x100));\r
619 Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+2]-'0') * 0x10));\r
620 Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+3]-'0')));\r
621 }\r
622 //\r
623 // Insert active entry to BdsDeviceList\r
624 //\r
625 if ((Option->Attribute & LOAD_OPTION_ACTIVE) == LOAD_OPTION_ACTIVE) {\r
626 InsertTailList (BdsCommonOptionList, &Option->Link);\r
627 FreePool (Variable);\r
628 return Option;\r
629 }\r
630\r
631 FreePool (Variable);\r
632 FreePool (Option);\r
633 return NULL;\r
634\r
635}\r
636\r
637/**\r
638 Process BootOrder, or DriverOrder variables, by calling\r
639 BdsLibVariableToOption () for each UINT16 in the variables.\r
640\r
641 @param BdsCommonOptionList The header of the option list base on variable\r
642 VariableName\r
643 @param VariableName EFI Variable name indicate the BootOrder or\r
644 DriverOrder\r
645\r
646 @retval EFI_SUCCESS Success create the boot option or driver option\r
647 list\r
648 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list\r
649\r
650**/\r
651EFI_STATUS\r
652EFIAPI\r
653BdsLibBuildOptionFromVar (\r
654 IN LIST_ENTRY *BdsCommonOptionList,\r
655 IN CHAR16 *VariableName\r
656 )\r
657{\r
658 UINT16 *OptionOrder;\r
659 UINTN OptionOrderSize;\r
660 UINTN Index;\r
661 BDS_COMMON_OPTION *Option;\r
662 CHAR16 OptionName[20];\r
663\r
664 //\r
665 // Zero Buffer in order to get all BOOT#### variables\r
666 //\r
667 ZeroMem (OptionName, sizeof (OptionName));\r
668\r
669 //\r
670 // Read the BootOrder, or DriverOrder variable.\r
671 //\r
672 OptionOrder = BdsLibGetVariableAndSize (\r
673 VariableName,\r
674 &gEfiGlobalVariableGuid,\r
675 &OptionOrderSize\r
676 );\r
677 if (OptionOrder == NULL) {\r
678 return EFI_OUT_OF_RESOURCES;\r
679 }\r
680\r
681 for (Index = 0; Index < OptionOrderSize / sizeof (UINT16); Index++) {\r
682 if (*VariableName == 'B') {\r
683 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionOrder[Index]);\r
684 } else {\r
685 UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", OptionOrder[Index]);\r
686 }\r
687\r
688 Option = BdsLibVariableToOption (BdsCommonOptionList, OptionName);\r
7490e2be 689 if (Option != NULL) {\r
690 Option->BootCurrent = OptionOrder[Index];\r
691 }\r
5c08e117 692 }\r
693\r
694 FreePool (OptionOrder);\r
695\r
696 return EFI_SUCCESS;\r
697}\r
698\r
699/**\r
700 Get boot mode by looking up configuration table and parsing HOB list\r
701\r
702 @param BootMode Boot mode from PEI handoff HOB.\r
703\r
704 @retval EFI_SUCCESS Successfully get boot mode\r
705\r
706**/\r
707EFI_STATUS\r
708EFIAPI\r
709BdsLibGetBootMode (\r
710 OUT EFI_BOOT_MODE *BootMode\r
711 )\r
712{\r
713 *BootMode = GetBootModeHob ();\r
714\r
715 return EFI_SUCCESS;\r
716}\r
717\r
718/**\r
719 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated\r
720 buffer, and the size of the buffer. If failure return NULL.\r
721\r
722 @param Name String part of EFI variable name\r
723 @param VendorGuid GUID part of EFI variable name\r
724 @param VariableSize Returns the size of the EFI variable that was read\r
725\r
726 @return Dynamically allocated memory that contains a copy of the EFI variable\r
727 Caller is responsible freeing the buffer.\r
728 @retval NULL Variable was not read\r
729\r
730**/\r
731VOID *\r
732EFIAPI\r
733BdsLibGetVariableAndSize (\r
734 IN CHAR16 *Name,\r
735 IN EFI_GUID *VendorGuid,\r
736 OUT UINTN *VariableSize\r
737 )\r
738{\r
739 EFI_STATUS Status;\r
740 UINTN BufferSize;\r
741 VOID *Buffer;\r
742\r
743 Buffer = NULL;\r
744\r
745 //\r
746 // Pass in a zero size buffer to find the required buffer size.\r
747 //\r
748 BufferSize = 0;\r
749 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);\r
750 if (Status == EFI_BUFFER_TOO_SMALL) {\r
751 //\r
752 // Allocate the buffer to return\r
753 //\r
754 Buffer = AllocateZeroPool (BufferSize);\r
755 if (Buffer == NULL) {\r
756 return NULL;\r
757 }\r
758 //\r
759 // Read variable into the allocated buffer.\r
760 //\r
761 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);\r
762 if (EFI_ERROR (Status)) {\r
763 BufferSize = 0;\r
764 }\r
765 }\r
766\r
767 *VariableSize = BufferSize;\r
768 return Buffer;\r
769}\r
770\r
771/**\r
772 Delete the instance in Multi which matches partly with Single instance\r
773\r
774 @param Multi A pointer to a multi-instance device path data\r
775 structure.\r
776 @param Single A pointer to a single-instance device path data\r
777 structure.\r
778\r
779 @return This function will remove the device path instances in Multi which partly\r
780 match with the Single, and return the result device path. If there is no\r
781 remaining device path as a result, this function will return NULL.\r
782\r
783**/\r
784EFI_DEVICE_PATH_PROTOCOL *\r
785EFIAPI\r
786BdsLibDelPartMatchInstance (\r
787 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
788 IN EFI_DEVICE_PATH_PROTOCOL *Single\r
789 )\r
790{\r
791 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
792 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
793 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r
794 UINTN InstanceSize;\r
795 UINTN SingleDpSize;\r
796 UINTN Size;\r
797\r
798 NewDevicePath = NULL;\r
799 TempNewDevicePath = NULL;\r
800\r
801 if (Multi == NULL || Single == NULL) {\r
802 return Multi;\r
803 }\r
804\r
805 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
806 SingleDpSize = GetDevicePathSize (Single) - END_DEVICE_PATH_LENGTH;\r
807 InstanceSize -= END_DEVICE_PATH_LENGTH;\r
808\r
809 while (Instance != NULL) {\r
810\r
811 Size = (SingleDpSize < InstanceSize) ? SingleDpSize : InstanceSize;\r
812\r
813 if ((CompareMem (Instance, Single, Size) != 0)) {\r
814 //\r
815 // Append the device path instance which does not match with Single\r
816 //\r
817 TempNewDevicePath = NewDevicePath;\r
818 NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);\r
819 if (TempNewDevicePath != NULL) {\r
820 FreePool(TempNewDevicePath);\r
821 }\r
822 }\r
823 FreePool(Instance);\r
824 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
825 InstanceSize -= END_DEVICE_PATH_LENGTH;\r
826 }\r
827\r
828 return NewDevicePath;\r
829}\r
830\r
831/**\r
832 Function compares a device path data structure to that of all the nodes of a\r
833 second device path instance.\r
834\r
835 @param Multi A pointer to a multi-instance device path data\r
836 structure.\r
837 @param Single A pointer to a single-instance device path data\r
838 structure.\r
839\r
840 @retval TRUE If the Single device path is contained within Multi device path.\r
841 @retval FALSE The Single device path is not match within Multi device path.\r
842\r
843**/\r
844BOOLEAN\r
845EFIAPI\r
846BdsLibMatchDevicePaths (\r
847 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
848 IN EFI_DEVICE_PATH_PROTOCOL *Single\r
849 )\r
850{\r
851 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
852 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;\r
853 UINTN Size;\r
854\r
855 if (Multi == NULL || Single == NULL) {\r
856 return FALSE;\r
857 }\r
858\r
859 DevicePath = Multi;\r
860 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
861\r
862 //\r
863 // Search for the match of 'Single' in 'Multi'\r
864 //\r
865 while (DevicePathInst != NULL) {\r
866 //\r
867 // If the single device path is found in multiple device paths,\r
868 // return success\r
869 //\r
870 if (CompareMem (Single, DevicePathInst, Size) == 0) {\r
871 FreePool (DevicePathInst);\r
872 return TRUE;\r
873 }\r
874\r
875 FreePool (DevicePathInst);\r
876 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
877 }\r
878\r
879 return FALSE;\r
880}\r
881\r
882/**\r
883 This function prints a series of strings.\r
884\r
885 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL\r
886 @param ... A variable argument list containing series of\r
887 strings, the last string must be NULL.\r
888\r
889 @retval EFI_SUCCESS Success print out the string using ConOut.\r
890 @retval EFI_STATUS Return the status of the ConOut->OutputString ().\r
891\r
892**/\r
893EFI_STATUS\r
894EFIAPI\r
895BdsLibOutputStrings (\r
896 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut,\r
897 ...\r
898 )\r
899{\r
900 VA_LIST Args;\r
901 EFI_STATUS Status;\r
902 CHAR16 *String;\r
903\r
904 Status = EFI_SUCCESS;\r
905 VA_START (Args, ConOut);\r
906\r
907 while (!EFI_ERROR (Status)) {\r
908 //\r
909 // If String is NULL, then it's the end of the list\r
910 //\r
911 String = VA_ARG (Args, CHAR16 *);\r
31b440cf 912 if (String == NULL) {\r
5c08e117 913 break;\r
914 }\r
915\r
916 Status = ConOut->OutputString (ConOut, String);\r
917\r
918 if (EFI_ERROR (Status)) {\r
919 break;\r
920 }\r
921 }\r
922 \r
923 VA_END(Args);\r
924 return Status;\r
925}\r
926\r
927//\r
8d3b5aff 928// Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.\r
5c08e117 929// Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if\r
930// user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.\r
931//\r
932\r
933\r
934/**\r
935 Enable the setup browser reset reminder feature.\r
936 This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.\r
937\r
938**/\r
939VOID\r
940EFIAPI\r
941EnableResetReminderFeature (\r
942 VOID\r
943 )\r
944{\r
945 mFeaturerSwitch = TRUE;\r
946}\r
947\r
948\r
949/**\r
950 Disable the setup browser reset reminder feature.\r
951 This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.\r
952\r
953**/\r
954VOID\r
955EFIAPI\r
956DisableResetReminderFeature (\r
957 VOID\r
958 )\r
959{\r
960 mFeaturerSwitch = FALSE;\r
961}\r
962\r
963\r
964/**\r
965 Record the info that a reset is required.\r
966 A module boolean variable is used to record whether a reset is required.\r
967\r
968**/\r
969VOID\r
970EFIAPI\r
971EnableResetRequired (\r
972 VOID\r
973 )\r
974{\r
975 mResetRequired = TRUE;\r
976}\r
977\r
978\r
979/**\r
980 Record the info that no reset is required.\r
981 A module boolean variable is used to record whether a reset is required.\r
982\r
983**/\r
984VOID\r
985EFIAPI\r
986DisableResetRequired (\r
987 VOID\r
988 )\r
989{\r
990 mResetRequired = FALSE;\r
991}\r
992\r
993\r
994/**\r
995 Check whether platform policy enable the reset reminder feature. The default is enabled.\r
996\r
997**/\r
998BOOLEAN\r
999EFIAPI\r
1000IsResetReminderFeatureEnable (\r
1001 VOID\r
1002 )\r
1003{\r
1004 return mFeaturerSwitch;\r
1005}\r
1006\r
1007\r
1008/**\r
1009 Check if user changed any option setting which needs a system reset to be effective.\r
1010\r
1011**/\r
1012BOOLEAN\r
1013EFIAPI\r
1014IsResetRequired (\r
1015 VOID\r
1016 )\r
1017{\r
1018 return mResetRequired;\r
1019}\r
1020\r
1021\r
1022/**\r
1023 Check whether a reset is needed, and finish the reset reminder feature.\r
1024 If a reset is needed, Popup a menu to notice user, and finish the feature\r
1025 according to the user selection.\r
1026\r
1027**/\r
1028VOID\r
1029EFIAPI\r
1030SetupResetReminder (\r
1031 VOID\r
1032 )\r
1033{\r
1034 EFI_INPUT_KEY Key;\r
1035 CHAR16 *StringBuffer1;\r
1036 CHAR16 *StringBuffer2;\r
1037\r
1038\r
1039 //\r
1040 //check any reset required change is applied? if yes, reset system\r
1041 //\r
1042 if (IsResetReminderFeatureEnable ()) {\r
1043 if (IsResetRequired ()) {\r
1044\r
1045 StringBuffer1 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));\r
1046 ASSERT (StringBuffer1 != NULL);\r
1047 StringBuffer2 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));\r
1048 ASSERT (StringBuffer2 != NULL);\r
1049 StrCpy (StringBuffer1, L"Configuration changed. Reset to apply it Now ? ");\r
1050 StrCpy (StringBuffer2, L"Enter (YES) / Esc (NO)");\r
1051 //\r
1052 // Popup a menu to notice user\r
1053 //\r
1054 do {\r
e3b236c8 1055 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, StringBuffer1, StringBuffer2, NULL);\r
5c08e117 1056 } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
1057\r
1058 FreePool (StringBuffer1);\r
1059 FreePool (StringBuffer2);\r
1060 //\r
1061 // If the user hits the YES Response key, reset\r
1062 //\r
5817d508 1063 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
5c08e117 1064 gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r
1065 }\r
1066 gST->ConOut->ClearScreen (gST->ConOut);\r
1067 }\r
1068 }\r
1069}\r
1070\r
1071/**\r
1072 Get the headers (dos, image, optional header) from an image\r
1073\r
1074 @param Device SimpleFileSystem device handle\r
1075 @param FileName File name for the image\r
1076 @param DosHeader Pointer to dos header\r
1077 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.\r
1078\r
1079 @retval EFI_SUCCESS Successfully get the machine type.\r
1080 @retval EFI_NOT_FOUND The file is not found.\r
1081 @retval EFI_LOAD_ERROR File is not a valid image file.\r
1082\r
1083**/\r
1084EFI_STATUS\r
1085EFIAPI\r
1086BdsLibGetImageHeader (\r
1087 IN EFI_HANDLE Device,\r
1088 IN CHAR16 *FileName,\r
1089 OUT EFI_IMAGE_DOS_HEADER *DosHeader,\r
1090 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr\r
1091 )\r
1092{\r
1093 EFI_STATUS Status;\r
1094 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
1095 EFI_FILE_HANDLE Root;\r
1096 EFI_FILE_HANDLE ThisFile;\r
1097 UINTN BufferSize;\r
1098 UINT64 FileSize;\r
1099 EFI_FILE_INFO *Info;\r
1100\r
1101 Root = NULL;\r
1102 ThisFile = NULL;\r
1103 //\r
1104 // Handle the file system interface to the device\r
1105 //\r
1106 Status = gBS->HandleProtocol (\r
1107 Device,\r
1108 &gEfiSimpleFileSystemProtocolGuid,\r
1109 (VOID *) &Volume\r
1110 );\r
1111 if (EFI_ERROR (Status)) {\r
1112 goto Done;\r
1113 }\r
1114\r
1115 Status = Volume->OpenVolume (\r
1116 Volume,\r
1117 &Root\r
1118 );\r
1119 if (EFI_ERROR (Status)) {\r
1120 Root = NULL;\r
1121 goto Done;\r
1122 }\r
da166a5d 1123 ASSERT (Root != NULL);\r
5c08e117 1124 Status = Root->Open (Root, &ThisFile, FileName, EFI_FILE_MODE_READ, 0);\r
1125 if (EFI_ERROR (Status)) {\r
1126 goto Done;\r
1127 }\r
da166a5d 1128 ASSERT (ThisFile != NULL);\r
5c08e117 1129\r
1130 //\r
1131 // Get file size\r
1132 //\r
1133 BufferSize = SIZE_OF_EFI_FILE_INFO + 200;\r
1134 do {\r
1135 Info = NULL;\r
1136 Status = gBS->AllocatePool (EfiBootServicesData, BufferSize, (VOID **) &Info);\r
1137 if (EFI_ERROR (Status)) {\r
1138 goto Done;\r
1139 }\r
1140 Status = ThisFile->GetInfo (\r
1141 ThisFile,\r
1142 &gEfiFileInfoGuid,\r
1143 &BufferSize,\r
1144 Info\r
1145 );\r
1146 if (!EFI_ERROR (Status)) {\r
1147 break;\r
1148 }\r
1149 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1150 FreePool (Info);\r
1151 goto Done;\r
1152 }\r
1153 FreePool (Info);\r
1154 } while (TRUE);\r
1155\r
1156 FileSize = Info->FileSize;\r
1157 FreePool (Info);\r
1158\r
1159 //\r
1160 // Read dos header\r
1161 //\r
1162 BufferSize = sizeof (EFI_IMAGE_DOS_HEADER);\r
1163 Status = ThisFile->Read (ThisFile, &BufferSize, DosHeader);\r
1164 if (EFI_ERROR (Status) ||\r
1165 BufferSize < sizeof (EFI_IMAGE_DOS_HEADER) ||\r
1166 FileSize <= DosHeader->e_lfanew ||\r
1167 DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {\r
1168 Status = EFI_LOAD_ERROR;\r
1169 goto Done;\r
1170 }\r
1171\r
1172 //\r
1173 // Move to PE signature\r
1174 //\r
1175 Status = ThisFile->SetPosition (ThisFile, DosHeader->e_lfanew);\r
1176 if (EFI_ERROR (Status)) {\r
1177 Status = EFI_LOAD_ERROR;\r
1178 goto Done;\r
1179 }\r
1180\r
1181 //\r
1182 // Read and check PE signature\r
1183 //\r
1184 BufferSize = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);\r
1185 Status = ThisFile->Read (ThisFile, &BufferSize, Hdr.Pe32);\r
1186 if (EFI_ERROR (Status) ||\r
1187 BufferSize < sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION) ||\r
1188 Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
1189 Status = EFI_LOAD_ERROR;\r
1190 goto Done;\r
1191 }\r
1192\r
1193 //\r
1194 // Check PE32 or PE32+ magic\r
1195 //\r
1196 if (Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC &&\r
1197 Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
1198 Status = EFI_LOAD_ERROR;\r
1199 goto Done;\r
1200 }\r
1201\r
1202 Done:\r
1203 if (ThisFile != NULL) {\r
1204 ThisFile->Close (ThisFile);\r
1205 }\r
1206 if (Root != NULL) {\r
1207 Root->Close (Root);\r
1208 }\r
1209 return Status;\r
1210}\r
1211\r
1212/**\r
a97a8596
RN
1213 This routine adjusts the memory information for different memory type and \r
1214 saves them into the variables for next boot. It conditionally resets the\r
1215 system when the memory information changes. Platform can reserve memory \r
1216 large enough (125% of actual requirement) to avoid the reset in the first boot.\r
5c08e117 1217**/\r
1218VOID\r
5c08e117 1219BdsSetMemoryTypeInformationVariable (\r
7caf72a9 1220 VOID\r
5c08e117 1221 )\r
1222{\r
1223 EFI_STATUS Status;\r
1224 EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;\r
1225 EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;\r
1226 UINTN VariableSize;\r
5c08e117 1227 UINTN Index;\r
1228 UINTN Index1;\r
1229 UINT32 Previous;\r
1230 UINT32 Current;\r
1231 UINT32 Next;\r
1232 EFI_HOB_GUID_TYPE *GuidHob;\r
7bee5a76 1233 BOOLEAN MemoryTypeInformationModified;\r
a4e9b4f6 1234 BOOLEAN MemoryTypeInformationVariableExists;\r
8c716296 1235 EFI_BOOT_MODE BootMode;\r
a4e9b4f6 1236\r
7bee5a76 1237 MemoryTypeInformationModified = FALSE;\r
a4e9b4f6 1238 MemoryTypeInformationVariableExists = FALSE;\r
1239\r
8c716296 1240\r
1241 BootMode = GetBootModeHob ();\r
1242 //\r
1243 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.\r
1244 //\r
1245 if (BootMode == BOOT_IN_RECOVERY_MODE) {\r
1246 return;\r
1247 }\r
1248\r
7bee5a76 1249 //\r
a97a8596 1250 // Only check the the Memory Type Information variable in the boot mode \r
7bee5a76
RN
1251 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type\r
1252 // Information is not valid in this boot mode.\r
1253 //\r
8c716296 1254 if (BootMode != BOOT_WITH_DEFAULT_SETTINGS) {\r
a4e9b4f6 1255 VariableSize = 0;\r
1256 Status = gRT->GetVariable (\r
1257 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
1258 &gEfiMemoryTypeInformationGuid,\r
1259 NULL, \r
1260 &VariableSize, \r
1261 NULL\r
1262 );\r
1263 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1264 MemoryTypeInformationVariableExists = TRUE;\r
a4e9b4f6 1265 }\r
1266 }\r
5c08e117 1267\r
1268 //\r
1269 // Retrieve the current memory usage statistics. If they are not found, then\r
1270 // no adjustments can be made to the Memory Type Information variable.\r
1271 //\r
1272 Status = EfiGetSystemConfigurationTable (\r
1273 &gEfiMemoryTypeInformationGuid,\r
1274 (VOID **) &CurrentMemoryTypeInformation\r
1275 );\r
1276 if (EFI_ERROR (Status) || CurrentMemoryTypeInformation == NULL) {\r
1277 return;\r
1278 }\r
1279\r
1280 //\r
1281 // Get the Memory Type Information settings from Hob if they exist,\r
1282 // PEI is responsible for getting them from variable and build a Hob to save them.\r
1283 // If the previous Memory Type Information is not available, then set defaults\r
1284 //\r
1285 GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);\r
1286 if (GuidHob == NULL) {\r
1287 //\r
1288 // If Platform has not built Memory Type Info into the Hob, just return.\r
1289 //\r
1290 return;\r
1291 }\r
1292 PreviousMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);\r
1293 VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);\r
1294\r
1295 //\r
1296 // Use a heuristic to adjust the Memory Type Information for the next boot\r
1297 //\r
a4e9b4f6 1298 DEBUG ((EFI_D_INFO, "Memory Previous Current Next \n"));\r
1299 DEBUG ((EFI_D_INFO, " Type Pages Pages Pages \n"));\r
1300 DEBUG ((EFI_D_INFO, "====== ======== ======== ========\n"));\r
1301\r
5c08e117 1302 for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
1303\r
5c08e117 1304 for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {\r
1305 if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {\r
5c08e117 1306 break;\r
1307 }\r
1308 }\r
5c08e117 1309 if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {\r
1310 continue;\r
1311 }\r
1312\r
a97a8596
RN
1313 //\r
1314 // Previous is the number of pages pre-allocated\r
1315 // Current is the number of pages actually needed\r
1316 //\r
5c08e117 1317 Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;\r
a97a8596
RN
1318 Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;\r
1319 Next = Previous;\r
5c08e117 1320\r
1321 //\r
1322 // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail\r
1323 //\r
a97a8596
RN
1324 if (Current < Previous) {\r
1325 if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {\r
1326 Next = Current + (Current >> 2);\r
1327 } else if (!MemoryTypeInformationVariableExists) {\r
1328 Next = MAX (Current + (Current >> 2), Previous);\r
1329 }\r
a4e9b4f6 1330 } else if (Current > Previous) {\r
5c08e117 1331 Next = Current + (Current >> 2);\r
5c08e117 1332 }\r
1333 if (Next > 0 && Next < 4) {\r
1334 Next = 4;\r
1335 }\r
1336\r
1337 if (Next != Previous) {\r
1338 PreviousMemoryTypeInformation[Index].NumberOfPages = Next;\r
7bee5a76 1339 MemoryTypeInformationModified = TRUE;\r
5c08e117 1340 }\r
1341\r
a4e9b4f6 1342 DEBUG ((EFI_D_INFO, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation[Index].Type, Previous, Current, Next));\r
5c08e117 1343 }\r
1344\r
1345 //\r
7bee5a76
RN
1346 // If any changes were made to the Memory Type Information settings, then set the new variable value;\r
1347 // Or create the variable in first boot.\r
5c08e117 1348 //\r
7bee5a76 1349 if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) {\r
5c08e117 1350 Status = gRT->SetVariable (\r
7bee5a76
RN
1351 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
1352 &gEfiMemoryTypeInformationGuid,\r
1353 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
1354 VariableSize,\r
1355 PreviousMemoryTypeInformation\r
1356 );\r
1357\r
1358 //\r
1359 // If the Memory Type Information settings have been modified, then reset the platform\r
1360 // so the new Memory Type Information setting will be used to guarantee that an S4\r
1361 // entry/resume cycle will not fail.\r
1362 //\r
7caf72a9
RN
1363 if (MemoryTypeInformationModified && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
1364 DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));\r
7bee5a76
RN
1365 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
1366 }\r
5c08e117 1367 }\r
5c08e117 1368}\r
1369\r
1370/**\r
7caf72a9 1371 This routine is kept for backward compatibility.\r
5c08e117 1372**/\r
1373VOID\r
1374EFIAPI\r
1375BdsLibSaveMemoryTypeInformation (\r
1376 VOID\r
1377 )\r
1378{\r
5c08e117 1379}\r
1380\r
1381\r
337661bb 1382/**\r
1383 Identify a user and, if authenticated, returns the current user profile handle.\r
1384\r
1385 @param[out] User Point to user profile handle.\r
1386 \r
1387 @retval EFI_SUCCESS User is successfully identified, or user identification\r
1388 is not supported.\r
1389 @retval EFI_ACCESS_DENIED User is not successfully identified\r
1390\r
1391**/\r
1392EFI_STATUS\r
1393EFIAPI\r
1394BdsLibUserIdentify (\r
1395 OUT EFI_USER_PROFILE_HANDLE *User\r
1396 )\r
1397{\r
1398 EFI_STATUS Status;\r
1399 EFI_USER_MANAGER_PROTOCOL *Manager;\r
1400 \r
1401 Status = gBS->LocateProtocol (\r
1402 &gEfiUserManagerProtocolGuid,\r
1403 NULL,\r
1404 (VOID **) &Manager\r
1405 );\r
1406 if (EFI_ERROR (Status)) {\r
1407 return EFI_SUCCESS;\r
1408 }\r
1409\r
1410 return Manager->Identify (Manager, User);\r
1411}\r
1412\r