]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c
Refine logic about validate boot option.
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / GenericBdsLib / BdsMisc.c
CommitLineData
5c08e117 1/** @file\r
2 Misc BDS library function\r
3\r
5f595f14 4Copyright (c) 2004 - 2012, 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
8c08a567
ED
260\r
261 //\r
262 // Validate the variable.\r
263 //\r
264 if (!ValidateOption(OptionPtr, OptionSize)) {\r
265 continue;\r
266 }\r
267\r
5c08e117 268 TempPtr = OptionPtr;\r
269 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
270 Description = (CHAR16 *) TempPtr;\r
271 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
272 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
273\r
274 //\r
275 // Notes: the description may will change base on the GetStringToken\r
276 //\r
277 if (CompareMem (OptionDevicePath, DevicePath, GetDevicePathSize (OptionDevicePath)) == 0) {\r
278 if (CompareMem (Description, String, StrSize (Description)) == 0) { \r
279 //\r
280 // Got the option, so just return\r
281 //\r
282 FreePool (OptionPtr);\r
283 FreePool (TempOptionPtr);\r
284 return EFI_SUCCESS;\r
285 } else {\r
286 //\r
287 // Option description changed, need update.\r
288 //\r
289 UpdateDescription = TRUE;\r
290 FreePool (OptionPtr);\r
291 break;\r
292 }\r
293 }\r
294\r
295 FreePool (OptionPtr);\r
296 }\r
297\r
298 OptionSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (String);\r
299 OptionSize += GetDevicePathSize (DevicePath);\r
300 OptionPtr = AllocateZeroPool (OptionSize);\r
301 ASSERT (OptionPtr != NULL);\r
302 \r
303 TempPtr = OptionPtr;\r
304 *(UINT32 *) TempPtr = LOAD_OPTION_ACTIVE;\r
305 TempPtr += sizeof (UINT32);\r
306 *(UINT16 *) TempPtr = (UINT16) GetDevicePathSize (DevicePath);\r
307 TempPtr += sizeof (UINT16);\r
308 CopyMem (TempPtr, String, StrSize (String));\r
309 TempPtr += StrSize (String);\r
310 CopyMem (TempPtr, DevicePath, GetDevicePathSize (DevicePath));\r
311\r
312 if (UpdateDescription) {\r
313 //\r
314 // The number in option#### to be updated. \r
315 // In this case, we must have non-NULL TempOptionPtr.\r
316 //\r
317 ASSERT (TempOptionPtr != NULL);\r
318 RegisterOptionNumber = TempOptionPtr[Index];\r
319 } else {\r
320 //\r
321 // The new option#### number\r
322 //\r
323 RegisterOptionNumber = BdsLibGetFreeOptionNumber(VariableName);\r
324 }\r
325\r
326 if (*VariableName == 'B') {\r
327 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", RegisterOptionNumber);\r
328 } else {\r
329 UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", RegisterOptionNumber);\r
330 }\r
331\r
332 Status = gRT->SetVariable (\r
333 OptionName,\r
334 &gEfiGlobalVariableGuid,\r
335 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
336 OptionSize,\r
337 OptionPtr\r
338 );\r
339 //\r
340 // Return if only need to update a changed description or fail to set option.\r
341 //\r
342 if (EFI_ERROR (Status) || UpdateDescription) {\r
343 FreePool (OptionPtr);\r
344 if (TempOptionPtr != NULL) {\r
345 FreePool (TempOptionPtr);\r
346 }\r
347 return Status;\r
348 }\r
349\r
350 FreePool (OptionPtr);\r
351\r
352 //\r
353 // Update the option order variable\r
354 //\r
355\r
356 //\r
357 // If no option order\r
358 //\r
359 if (TempOptionSize == 0) {\r
360 BootOrderEntry = 0;\r
361 Status = gRT->SetVariable (\r
362 VariableName,\r
363 &gEfiGlobalVariableGuid,\r
364 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
365 sizeof (UINT16),\r
366 &BootOrderEntry\r
367 );\r
368 if (TempOptionPtr != NULL) {\r
369 FreePool (TempOptionPtr);\r
370 }\r
371 return Status;\r
372 }\r
373 \r
374 //\r
375 // TempOptionPtr must not be NULL if TempOptionSize is not zero.\r
376 //\r
377 ASSERT (TempOptionPtr != NULL);\r
378 //\r
379 // Append the new option number to the original option order\r
380 //\r
381 OrderItemNum = (TempOptionSize / sizeof (UINT16)) + 1 ;\r
382 OptionOrderPtr = AllocateZeroPool ( OrderItemNum * sizeof (UINT16));\r
383 ASSERT (OptionOrderPtr!= NULL);\r
384 CopyMem (OptionOrderPtr, TempOptionPtr, (OrderItemNum - 1) * sizeof (UINT16));\r
385\r
386 OptionOrderPtr[Index] = RegisterOptionNumber;\r
387\r
388 Status = gRT->SetVariable (\r
389 VariableName,\r
390 &gEfiGlobalVariableGuid,\r
391 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
392 OrderItemNum * sizeof (UINT16),\r
393 OptionOrderPtr\r
394 );\r
395 FreePool (TempOptionPtr);\r
396 FreePool (OptionOrderPtr);\r
397\r
398 return Status;\r
399}\r
400\r
b16cc38b
ED
401/**\r
402 Returns the size of a device path in bytes.\r
403\r
404 This function returns the size, in bytes, of the device path data structure \r
405 specified by DevicePath including the end of device path node. If DevicePath \r
406 is NULL, then 0 is returned. If the length of the device path is bigger than\r
407 MaxSize, also return 0 to indicate this is an invalidate device path.\r
408\r
409 @param DevicePath A pointer to a device path data structure.\r
410 @param MaxSize Max valid device path size. If big than this size, \r
411 return error.\r
412 \r
413 @retval 0 An invalid device path.\r
414 @retval Others The size of a device path in bytes.\r
415\r
416**/\r
417UINTN\r
418GetDevicePathSizeEx (\r
419 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
420 IN UINTN MaxSize\r
421 )\r
422{\r
423 UINTN Size;\r
424 UINTN NodeSize;\r
425\r
426 if (DevicePath == NULL) {\r
427 return 0;\r
428 }\r
429\r
430 //\r
431 // Search for the end of the device path structure\r
432 //\r
433 Size = 0;\r
434 while (!IsDevicePathEnd (DevicePath)) {\r
435 NodeSize = DevicePathNodeLength (DevicePath);\r
8c08a567 436 if (NodeSize < END_DEVICE_PATH_LENGTH) {\r
b16cc38b
ED
437 return 0;\r
438 }\r
439 Size += NodeSize;\r
440 if (Size > MaxSize) {\r
441 return 0;\r
442 }\r
443 DevicePath = NextDevicePathNode (DevicePath);\r
444 }\r
445 Size += DevicePathNodeLength (DevicePath);\r
446 if (Size > MaxSize) {\r
447 return 0;\r
448 }\r
449\r
450 return Size;\r
451}\r
452\r
453/**\r
454 Returns the length of a Null-terminated Unicode string. If the length is \r
455 bigger than MaxStringLen, return length 0 to indicate that this is an \r
456 invalidate string.\r
457\r
3e867071 458 This function returns the byte length of Unicode characters in the Null-terminated\r
b16cc38b
ED
459 Unicode string specified by String. \r
460\r
461 If String is NULL, then ASSERT().\r
462 If String is not aligned on a 16-bit boundary, then ASSERT().\r
463\r
464 @param String A pointer to a Null-terminated Unicode string.\r
465 @param MaxStringLen Max string len in this string.\r
466\r
467 @retval 0 An invalid string.\r
468 @retval Others The length of String.\r
469\r
470**/\r
471UINTN\r
472StrSizeEx (\r
473 IN CONST CHAR16 *String,\r
474 IN UINTN MaxStringLen\r
475 )\r
476{\r
477 UINTN Length;\r
478\r
479 ASSERT (String != NULL && MaxStringLen != 0);\r
480 ASSERT (((UINTN) String & BIT0) == 0);\r
481\r
3e867071 482 for (Length = 0; *String != L'\0' && MaxStringLen != Length; String++, Length+=2);\r
b16cc38b
ED
483\r
484 if (*String != L'\0' && MaxStringLen == Length) {\r
485 return 0;\r
486 }\r
487\r
3e867071 488 return Length + 2;\r
b16cc38b 489}\r
5c08e117 490\r
8c08a567
ED
491/**\r
492 Validate the EFI Boot#### variable (VendorGuid/Name)\r
493\r
494 @param Variable Boot#### variable data.\r
495 @param VariableSize Returns the size of the EFI variable that was read\r
496\r
497 @retval TRUE The variable data is correct.\r
498 @retval FALSE The variable data is corrupted.\r
499\r
500**/\r
501BOOLEAN \r
502ValidateOption (\r
503 UINT8 *Variable,\r
504 UINTN VariableSize\r
505 )\r
506{\r
507 UINT16 FilePathSize;\r
508 UINT8 *TempPtr;\r
509 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
8c08a567
ED
510 UINTN TempSize;\r
511\r
3e867071
ED
512 if (VariableSize <= sizeof (UINT16) + sizeof (UINT32)) {\r
513 return FALSE;\r
514 }\r
515\r
8c08a567
ED
516 //\r
517 // Skip the option attribute\r
518 //\r
519 TempPtr = Variable;\r
520 TempPtr += sizeof (UINT32);\r
521\r
522 //\r
523 // Get the option's device path size\r
524 //\r
525 FilePathSize = *(UINT16 *) TempPtr;\r
526 TempPtr += sizeof (UINT16);\r
527\r
528 //\r
529 // Get the option's description string size\r
530 //\r
3e867071 531 TempSize = StrSizeEx ((CHAR16 *) TempPtr, VariableSize - sizeof (UINT16) - sizeof (UINT32));\r
8c08a567
ED
532 TempPtr += TempSize;\r
533\r
534 //\r
535 // Get the option's device path\r
536 //\r
537 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
3e867071 538 TempPtr += FilePathSize;\r
8c08a567
ED
539\r
540 //\r
541 // Validation boot option variable.\r
542 //\r
543 if ((FilePathSize == 0) || (TempSize == 0)) {\r
544 return FALSE;\r
545 }\r
546\r
3e867071 547 if (TempSize + FilePathSize + sizeof (UINT16) + sizeof (UINT32) > VariableSize) {\r
8c08a567
ED
548 return FALSE;\r
549 }\r
550\r
3e867071 551 return GetDevicePathSizeEx (DevicePath, FilePathSize) != 0;\r
8c08a567
ED
552}\r
553\r
c1e2752c
ED
554/**\r
555 Convert a single character to number.\r
556 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'\r
557 \r
558 @param Char The input char which need to change to a hex number.\r
559 \r
560**/\r
561UINTN\r
562CharToUint (\r
563 IN CHAR16 Char\r
564 )\r
565{\r
566 if ((Char >= L'0') && (Char <= L'9')) {\r
567 return (UINTN) (Char - L'0');\r
568 }\r
569\r
570 if ((Char >= L'A') && (Char <= L'F')) {\r
571 return (UINTN) (Char - L'A' + 0xA);\r
572 }\r
573\r
574 ASSERT (FALSE);\r
575 return 0;\r
576}\r
577\r
5c08e117 578/**\r
579 Build the boot#### or driver#### option from the VariableName, the\r
580 build boot#### or driver#### will also be linked to BdsCommonOptionList.\r
581\r
582 @param BdsCommonOptionList The header of the boot#### or driver#### option\r
583 link list\r
584 @param VariableName EFI Variable name indicate if it is boot#### or\r
585 driver####\r
586\r
587 @retval BDS_COMMON_OPTION Get the option just been created\r
588 @retval NULL Failed to get the new option\r
589\r
590**/\r
591BDS_COMMON_OPTION *\r
592EFIAPI\r
593BdsLibVariableToOption (\r
594 IN OUT LIST_ENTRY *BdsCommonOptionList,\r
595 IN CHAR16 *VariableName\r
596 )\r
597{\r
598 UINT32 Attribute;\r
599 UINT16 FilePathSize;\r
600 UINT8 *Variable;\r
601 UINT8 *TempPtr;\r
602 UINTN VariableSize;\r
603 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
604 BDS_COMMON_OPTION *Option;\r
605 VOID *LoadOptions;\r
606 UINT32 LoadOptionsSize;\r
607 CHAR16 *Description;\r
608 UINT8 NumOff;\r
3e867071 609\r
5c08e117 610 //\r
611 // Read the variable. We will never free this data.\r
612 //\r
613 Variable = BdsLibGetVariableAndSize (\r
614 VariableName,\r
615 &gEfiGlobalVariableGuid,\r
616 &VariableSize\r
617 );\r
618 if (Variable == NULL) {\r
619 return NULL;\r
620 }\r
8c08a567
ED
621\r
622 //\r
623 // Validate Boot#### variable data.\r
624 //\r
625 if (!ValidateOption(Variable, VariableSize)) {\r
626 return NULL;\r
627 }\r
628\r
5c08e117 629 //\r
630 // Notes: careful defined the variable of Boot#### or\r
631 // Driver####, consider use some macro to abstract the code\r
632 //\r
633 //\r
634 // Get the option attribute\r
635 //\r
636 TempPtr = Variable;\r
637 Attribute = *(UINT32 *) Variable;\r
638 TempPtr += sizeof (UINT32);\r
639\r
640 //\r
641 // Get the option's device path size\r
642 //\r
643 FilePathSize = *(UINT16 *) TempPtr;\r
644 TempPtr += sizeof (UINT16);\r
645\r
646 //\r
647 // Get the option's description string\r
648 //\r
649 Description = (CHAR16 *) TempPtr;\r
650\r
651 //\r
652 // Get the option's description string size\r
653 //\r
3e867071 654 TempPtr += StrSize((CHAR16 *) TempPtr);\r
5c08e117 655\r
656 //\r
657 // Get the option's device path\r
658 //\r
659 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
660 TempPtr += FilePathSize;\r
661\r
b16cc38b
ED
662 //\r
663 // Get load opion data.\r
664 //\r
5c08e117 665 LoadOptions = TempPtr;\r
666 LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable));\r
667\r
668 //\r
669 // The Console variables may have multiple device paths, so make\r
670 // an Entry for each one.\r
671 //\r
672 Option = AllocateZeroPool (sizeof (BDS_COMMON_OPTION));\r
673 if (Option == NULL) {\r
674 return NULL;\r
675 }\r
676\r
677 Option->Signature = BDS_LOAD_OPTION_SIGNATURE;\r
678 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));\r
679 ASSERT(Option->DevicePath != NULL);\r
680 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r
681\r
682 Option->Attribute = Attribute;\r
683 Option->Description = AllocateZeroPool (StrSize (Description));\r
684 ASSERT(Option->Description != NULL);\r
685 CopyMem (Option->Description, Description, StrSize (Description));\r
686\r
687 Option->LoadOptions = AllocateZeroPool (LoadOptionsSize);\r
688 ASSERT(Option->LoadOptions != NULL);\r
689 CopyMem (Option->LoadOptions, LoadOptions, LoadOptionsSize);\r
690 Option->LoadOptionsSize = LoadOptionsSize;\r
691\r
692 //\r
693 // Get the value from VariableName Unicode string\r
694 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this\r
695 // Unicode stream to ASCII without any loss in meaning.\r
696 //\r
697 if (*VariableName == 'B') {\r
c1e2752c
ED
698 NumOff = (UINT8) (sizeof (L"Boot") / sizeof (CHAR16) - 1);\r
699 Option->BootCurrent = (UINT16) (CharToUint (VariableName[NumOff+0]) * 0x1000) \r
700 + (UINT16) (CharToUint (VariableName[NumOff+1]) * 0x100)\r
701 + (UINT16) (CharToUint (VariableName[NumOff+2]) * 0x10)\r
702 + (UINT16) (CharToUint (VariableName[NumOff+3]) * 0x1);\r
5c08e117 703 }\r
704 //\r
705 // Insert active entry to BdsDeviceList\r
706 //\r
707 if ((Option->Attribute & LOAD_OPTION_ACTIVE) == LOAD_OPTION_ACTIVE) {\r
708 InsertTailList (BdsCommonOptionList, &Option->Link);\r
709 FreePool (Variable);\r
710 return Option;\r
711 }\r
712\r
713 FreePool (Variable);\r
7c9fbd79
ED
714 FreePool (Option->Description);\r
715 FreePool (Option->DevicePath);\r
716 FreePool (Option->LoadOptions);\r
5c08e117 717 FreePool (Option);\r
718 return NULL;\r
5c08e117 719}\r
720\r
721/**\r
722 Process BootOrder, or DriverOrder variables, by calling\r
723 BdsLibVariableToOption () for each UINT16 in the variables.\r
724\r
725 @param BdsCommonOptionList The header of the option list base on variable\r
726 VariableName\r
727 @param VariableName EFI Variable name indicate the BootOrder or\r
728 DriverOrder\r
729\r
730 @retval EFI_SUCCESS Success create the boot option or driver option\r
731 list\r
732 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list\r
733\r
734**/\r
735EFI_STATUS\r
736EFIAPI\r
737BdsLibBuildOptionFromVar (\r
738 IN LIST_ENTRY *BdsCommonOptionList,\r
739 IN CHAR16 *VariableName\r
740 )\r
741{\r
742 UINT16 *OptionOrder;\r
743 UINTN OptionOrderSize;\r
744 UINTN Index;\r
745 BDS_COMMON_OPTION *Option;\r
746 CHAR16 OptionName[20];\r
747\r
748 //\r
749 // Zero Buffer in order to get all BOOT#### variables\r
750 //\r
751 ZeroMem (OptionName, sizeof (OptionName));\r
752\r
753 //\r
754 // Read the BootOrder, or DriverOrder variable.\r
755 //\r
756 OptionOrder = BdsLibGetVariableAndSize (\r
757 VariableName,\r
758 &gEfiGlobalVariableGuid,\r
759 &OptionOrderSize\r
760 );\r
761 if (OptionOrder == NULL) {\r
762 return EFI_OUT_OF_RESOURCES;\r
763 }\r
764\r
765 for (Index = 0; Index < OptionOrderSize / sizeof (UINT16); Index++) {\r
766 if (*VariableName == 'B') {\r
767 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionOrder[Index]);\r
768 } else {\r
769 UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", OptionOrder[Index]);\r
770 }\r
771\r
772 Option = BdsLibVariableToOption (BdsCommonOptionList, OptionName);\r
7490e2be 773 if (Option != NULL) {\r
774 Option->BootCurrent = OptionOrder[Index];\r
775 }\r
5c08e117 776 }\r
777\r
778 FreePool (OptionOrder);\r
779\r
780 return EFI_SUCCESS;\r
781}\r
782\r
783/**\r
784 Get boot mode by looking up configuration table and parsing HOB list\r
785\r
786 @param BootMode Boot mode from PEI handoff HOB.\r
787\r
788 @retval EFI_SUCCESS Successfully get boot mode\r
789\r
790**/\r
791EFI_STATUS\r
792EFIAPI\r
793BdsLibGetBootMode (\r
794 OUT EFI_BOOT_MODE *BootMode\r
795 )\r
796{\r
797 *BootMode = GetBootModeHob ();\r
798\r
799 return EFI_SUCCESS;\r
800}\r
801\r
802/**\r
803 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated\r
804 buffer, and the size of the buffer. If failure return NULL.\r
805\r
806 @param Name String part of EFI variable name\r
807 @param VendorGuid GUID part of EFI variable name\r
808 @param VariableSize Returns the size of the EFI variable that was read\r
809\r
810 @return Dynamically allocated memory that contains a copy of the EFI variable\r
811 Caller is responsible freeing the buffer.\r
812 @retval NULL Variable was not read\r
813\r
814**/\r
815VOID *\r
816EFIAPI\r
817BdsLibGetVariableAndSize (\r
818 IN CHAR16 *Name,\r
819 IN EFI_GUID *VendorGuid,\r
820 OUT UINTN *VariableSize\r
821 )\r
822{\r
823 EFI_STATUS Status;\r
824 UINTN BufferSize;\r
825 VOID *Buffer;\r
826\r
827 Buffer = NULL;\r
828\r
829 //\r
830 // Pass in a zero size buffer to find the required buffer size.\r
831 //\r
832 BufferSize = 0;\r
833 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);\r
834 if (Status == EFI_BUFFER_TOO_SMALL) {\r
835 //\r
836 // Allocate the buffer to return\r
837 //\r
838 Buffer = AllocateZeroPool (BufferSize);\r
839 if (Buffer == NULL) {\r
840 return NULL;\r
841 }\r
842 //\r
843 // Read variable into the allocated buffer.\r
844 //\r
845 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);\r
846 if (EFI_ERROR (Status)) {\r
847 BufferSize = 0;\r
848 }\r
849 }\r
850\r
851 *VariableSize = BufferSize;\r
852 return Buffer;\r
853}\r
854\r
855/**\r
856 Delete the instance in Multi which matches partly with Single instance\r
857\r
858 @param Multi A pointer to a multi-instance device path data\r
859 structure.\r
860 @param Single A pointer to a single-instance device path data\r
861 structure.\r
862\r
863 @return This function will remove the device path instances in Multi which partly\r
864 match with the Single, and return the result device path. If there is no\r
865 remaining device path as a result, this function will return NULL.\r
866\r
867**/\r
868EFI_DEVICE_PATH_PROTOCOL *\r
869EFIAPI\r
870BdsLibDelPartMatchInstance (\r
871 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
872 IN EFI_DEVICE_PATH_PROTOCOL *Single\r
873 )\r
874{\r
875 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
876 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
877 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r
878 UINTN InstanceSize;\r
879 UINTN SingleDpSize;\r
880 UINTN Size;\r
881\r
882 NewDevicePath = NULL;\r
883 TempNewDevicePath = NULL;\r
884\r
885 if (Multi == NULL || Single == NULL) {\r
886 return Multi;\r
887 }\r
888\r
889 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
890 SingleDpSize = GetDevicePathSize (Single) - END_DEVICE_PATH_LENGTH;\r
891 InstanceSize -= END_DEVICE_PATH_LENGTH;\r
892\r
893 while (Instance != NULL) {\r
894\r
895 Size = (SingleDpSize < InstanceSize) ? SingleDpSize : InstanceSize;\r
896\r
897 if ((CompareMem (Instance, Single, Size) != 0)) {\r
898 //\r
899 // Append the device path instance which does not match with Single\r
900 //\r
901 TempNewDevicePath = NewDevicePath;\r
902 NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);\r
903 if (TempNewDevicePath != NULL) {\r
904 FreePool(TempNewDevicePath);\r
905 }\r
906 }\r
907 FreePool(Instance);\r
908 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
909 InstanceSize -= END_DEVICE_PATH_LENGTH;\r
910 }\r
911\r
912 return NewDevicePath;\r
913}\r
914\r
915/**\r
916 Function compares a device path data structure to that of all the nodes of a\r
917 second device path instance.\r
918\r
919 @param Multi A pointer to a multi-instance device path data\r
920 structure.\r
921 @param Single A pointer to a single-instance device path data\r
922 structure.\r
923\r
924 @retval TRUE If the Single device path is contained within Multi device path.\r
925 @retval FALSE The Single device path is not match within Multi device path.\r
926\r
927**/\r
928BOOLEAN\r
929EFIAPI\r
930BdsLibMatchDevicePaths (\r
931 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
932 IN EFI_DEVICE_PATH_PROTOCOL *Single\r
933 )\r
934{\r
935 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
936 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;\r
937 UINTN Size;\r
938\r
939 if (Multi == NULL || Single == NULL) {\r
940 return FALSE;\r
941 }\r
942\r
943 DevicePath = Multi;\r
944 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
945\r
946 //\r
947 // Search for the match of 'Single' in 'Multi'\r
948 //\r
949 while (DevicePathInst != NULL) {\r
950 //\r
951 // If the single device path is found in multiple device paths,\r
952 // return success\r
953 //\r
954 if (CompareMem (Single, DevicePathInst, Size) == 0) {\r
955 FreePool (DevicePathInst);\r
956 return TRUE;\r
957 }\r
958\r
959 FreePool (DevicePathInst);\r
960 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
961 }\r
962\r
963 return FALSE;\r
964}\r
965\r
966/**\r
967 This function prints a series of strings.\r
968\r
969 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL\r
970 @param ... A variable argument list containing series of\r
971 strings, the last string must be NULL.\r
972\r
973 @retval EFI_SUCCESS Success print out the string using ConOut.\r
974 @retval EFI_STATUS Return the status of the ConOut->OutputString ().\r
975\r
976**/\r
977EFI_STATUS\r
978EFIAPI\r
979BdsLibOutputStrings (\r
980 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut,\r
981 ...\r
982 )\r
983{\r
984 VA_LIST Args;\r
985 EFI_STATUS Status;\r
986 CHAR16 *String;\r
987\r
988 Status = EFI_SUCCESS;\r
989 VA_START (Args, ConOut);\r
990\r
991 while (!EFI_ERROR (Status)) {\r
992 //\r
993 // If String is NULL, then it's the end of the list\r
994 //\r
995 String = VA_ARG (Args, CHAR16 *);\r
31b440cf 996 if (String == NULL) {\r
5c08e117 997 break;\r
998 }\r
999\r
1000 Status = ConOut->OutputString (ConOut, String);\r
1001\r
1002 if (EFI_ERROR (Status)) {\r
1003 break;\r
1004 }\r
1005 }\r
1006 \r
1007 VA_END(Args);\r
1008 return Status;\r
1009}\r
1010\r
1011//\r
8d3b5aff 1012// Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.\r
5c08e117 1013// Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if\r
1014// user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.\r
1015//\r
1016\r
1017\r
1018/**\r
1019 Enable the setup browser reset reminder feature.\r
1020 This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.\r
1021\r
1022**/\r
1023VOID\r
1024EFIAPI\r
1025EnableResetReminderFeature (\r
1026 VOID\r
1027 )\r
1028{\r
1029 mFeaturerSwitch = TRUE;\r
1030}\r
1031\r
1032\r
1033/**\r
1034 Disable the setup browser reset reminder feature.\r
1035 This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.\r
1036\r
1037**/\r
1038VOID\r
1039EFIAPI\r
1040DisableResetReminderFeature (\r
1041 VOID\r
1042 )\r
1043{\r
1044 mFeaturerSwitch = FALSE;\r
1045}\r
1046\r
1047\r
1048/**\r
1049 Record the info that a reset is required.\r
1050 A module boolean variable is used to record whether a reset is required.\r
1051\r
1052**/\r
1053VOID\r
1054EFIAPI\r
1055EnableResetRequired (\r
1056 VOID\r
1057 )\r
1058{\r
1059 mResetRequired = TRUE;\r
1060}\r
1061\r
1062\r
1063/**\r
1064 Record the info that no reset is required.\r
1065 A module boolean variable is used to record whether a reset is required.\r
1066\r
1067**/\r
1068VOID\r
1069EFIAPI\r
1070DisableResetRequired (\r
1071 VOID\r
1072 )\r
1073{\r
1074 mResetRequired = FALSE;\r
1075}\r
1076\r
1077\r
1078/**\r
1079 Check whether platform policy enable the reset reminder feature. The default is enabled.\r
1080\r
1081**/\r
1082BOOLEAN\r
1083EFIAPI\r
1084IsResetReminderFeatureEnable (\r
1085 VOID\r
1086 )\r
1087{\r
1088 return mFeaturerSwitch;\r
1089}\r
1090\r
1091\r
1092/**\r
1093 Check if user changed any option setting which needs a system reset to be effective.\r
1094\r
1095**/\r
1096BOOLEAN\r
1097EFIAPI\r
1098IsResetRequired (\r
1099 VOID\r
1100 )\r
1101{\r
1102 return mResetRequired;\r
1103}\r
1104\r
1105\r
1106/**\r
1107 Check whether a reset is needed, and finish the reset reminder feature.\r
1108 If a reset is needed, Popup a menu to notice user, and finish the feature\r
1109 according to the user selection.\r
1110\r
1111**/\r
1112VOID\r
1113EFIAPI\r
1114SetupResetReminder (\r
1115 VOID\r
1116 )\r
1117{\r
1118 EFI_INPUT_KEY Key;\r
1119 CHAR16 *StringBuffer1;\r
1120 CHAR16 *StringBuffer2;\r
1121\r
1122\r
1123 //\r
1124 //check any reset required change is applied? if yes, reset system\r
1125 //\r
1126 if (IsResetReminderFeatureEnable ()) {\r
1127 if (IsResetRequired ()) {\r
1128\r
1129 StringBuffer1 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));\r
1130 ASSERT (StringBuffer1 != NULL);\r
1131 StringBuffer2 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));\r
1132 ASSERT (StringBuffer2 != NULL);\r
1133 StrCpy (StringBuffer1, L"Configuration changed. Reset to apply it Now ? ");\r
1134 StrCpy (StringBuffer2, L"Enter (YES) / Esc (NO)");\r
1135 //\r
1136 // Popup a menu to notice user\r
1137 //\r
1138 do {\r
e3b236c8 1139 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, StringBuffer1, StringBuffer2, NULL);\r
5c08e117 1140 } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
1141\r
1142 FreePool (StringBuffer1);\r
1143 FreePool (StringBuffer2);\r
1144 //\r
1145 // If the user hits the YES Response key, reset\r
1146 //\r
5817d508 1147 if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
5c08e117 1148 gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r
1149 }\r
1150 gST->ConOut->ClearScreen (gST->ConOut);\r
1151 }\r
1152 }\r
1153}\r
1154\r
1155/**\r
1156 Get the headers (dos, image, optional header) from an image\r
1157\r
1158 @param Device SimpleFileSystem device handle\r
1159 @param FileName File name for the image\r
1160 @param DosHeader Pointer to dos header\r
1161 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.\r
1162\r
1163 @retval EFI_SUCCESS Successfully get the machine type.\r
1164 @retval EFI_NOT_FOUND The file is not found.\r
1165 @retval EFI_LOAD_ERROR File is not a valid image file.\r
1166\r
1167**/\r
1168EFI_STATUS\r
1169EFIAPI\r
1170BdsLibGetImageHeader (\r
1171 IN EFI_HANDLE Device,\r
1172 IN CHAR16 *FileName,\r
1173 OUT EFI_IMAGE_DOS_HEADER *DosHeader,\r
1174 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr\r
1175 )\r
1176{\r
1177 EFI_STATUS Status;\r
1178 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
1179 EFI_FILE_HANDLE Root;\r
1180 EFI_FILE_HANDLE ThisFile;\r
1181 UINTN BufferSize;\r
1182 UINT64 FileSize;\r
1183 EFI_FILE_INFO *Info;\r
1184\r
1185 Root = NULL;\r
1186 ThisFile = NULL;\r
1187 //\r
1188 // Handle the file system interface to the device\r
1189 //\r
1190 Status = gBS->HandleProtocol (\r
1191 Device,\r
1192 &gEfiSimpleFileSystemProtocolGuid,\r
1193 (VOID *) &Volume\r
1194 );\r
1195 if (EFI_ERROR (Status)) {\r
1196 goto Done;\r
1197 }\r
1198\r
1199 Status = Volume->OpenVolume (\r
1200 Volume,\r
1201 &Root\r
1202 );\r
1203 if (EFI_ERROR (Status)) {\r
1204 Root = NULL;\r
1205 goto Done;\r
1206 }\r
da166a5d 1207 ASSERT (Root != NULL);\r
5c08e117 1208 Status = Root->Open (Root, &ThisFile, FileName, EFI_FILE_MODE_READ, 0);\r
1209 if (EFI_ERROR (Status)) {\r
1210 goto Done;\r
1211 }\r
da166a5d 1212 ASSERT (ThisFile != NULL);\r
5c08e117 1213\r
1214 //\r
1215 // Get file size\r
1216 //\r
1217 BufferSize = SIZE_OF_EFI_FILE_INFO + 200;\r
1218 do {\r
1219 Info = NULL;\r
1220 Status = gBS->AllocatePool (EfiBootServicesData, BufferSize, (VOID **) &Info);\r
1221 if (EFI_ERROR (Status)) {\r
1222 goto Done;\r
1223 }\r
1224 Status = ThisFile->GetInfo (\r
1225 ThisFile,\r
1226 &gEfiFileInfoGuid,\r
1227 &BufferSize,\r
1228 Info\r
1229 );\r
1230 if (!EFI_ERROR (Status)) {\r
1231 break;\r
1232 }\r
1233 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1234 FreePool (Info);\r
1235 goto Done;\r
1236 }\r
1237 FreePool (Info);\r
1238 } while (TRUE);\r
1239\r
1240 FileSize = Info->FileSize;\r
1241 FreePool (Info);\r
1242\r
1243 //\r
1244 // Read dos header\r
1245 //\r
1246 BufferSize = sizeof (EFI_IMAGE_DOS_HEADER);\r
1247 Status = ThisFile->Read (ThisFile, &BufferSize, DosHeader);\r
1248 if (EFI_ERROR (Status) ||\r
1249 BufferSize < sizeof (EFI_IMAGE_DOS_HEADER) ||\r
1250 FileSize <= DosHeader->e_lfanew ||\r
1251 DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {\r
1252 Status = EFI_LOAD_ERROR;\r
1253 goto Done;\r
1254 }\r
1255\r
1256 //\r
1257 // Move to PE signature\r
1258 //\r
1259 Status = ThisFile->SetPosition (ThisFile, DosHeader->e_lfanew);\r
1260 if (EFI_ERROR (Status)) {\r
1261 Status = EFI_LOAD_ERROR;\r
1262 goto Done;\r
1263 }\r
1264\r
1265 //\r
1266 // Read and check PE signature\r
1267 //\r
1268 BufferSize = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);\r
1269 Status = ThisFile->Read (ThisFile, &BufferSize, Hdr.Pe32);\r
1270 if (EFI_ERROR (Status) ||\r
1271 BufferSize < sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION) ||\r
1272 Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
1273 Status = EFI_LOAD_ERROR;\r
1274 goto Done;\r
1275 }\r
1276\r
1277 //\r
1278 // Check PE32 or PE32+ magic\r
1279 //\r
1280 if (Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC &&\r
1281 Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
1282 Status = EFI_LOAD_ERROR;\r
1283 goto Done;\r
1284 }\r
1285\r
1286 Done:\r
1287 if (ThisFile != NULL) {\r
1288 ThisFile->Close (ThisFile);\r
1289 }\r
1290 if (Root != NULL) {\r
1291 Root->Close (Root);\r
1292 }\r
1293 return Status;\r
1294}\r
1295\r
1296/**\r
a97a8596
RN
1297 This routine adjusts the memory information for different memory type and \r
1298 saves them into the variables for next boot. It conditionally resets the\r
1299 system when the memory information changes. Platform can reserve memory \r
1300 large enough (125% of actual requirement) to avoid the reset in the first boot.\r
5c08e117 1301**/\r
1302VOID\r
5c08e117 1303BdsSetMemoryTypeInformationVariable (\r
7caf72a9 1304 VOID\r
5c08e117 1305 )\r
1306{\r
1307 EFI_STATUS Status;\r
1308 EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;\r
1309 EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;\r
1310 UINTN VariableSize;\r
5c08e117 1311 UINTN Index;\r
1312 UINTN Index1;\r
1313 UINT32 Previous;\r
1314 UINT32 Current;\r
1315 UINT32 Next;\r
1316 EFI_HOB_GUID_TYPE *GuidHob;\r
7bee5a76 1317 BOOLEAN MemoryTypeInformationModified;\r
a4e9b4f6 1318 BOOLEAN MemoryTypeInformationVariableExists;\r
8c716296 1319 EFI_BOOT_MODE BootMode;\r
a4e9b4f6 1320\r
7bee5a76 1321 MemoryTypeInformationModified = FALSE;\r
a4e9b4f6 1322 MemoryTypeInformationVariableExists = FALSE;\r
1323\r
8c716296 1324\r
1325 BootMode = GetBootModeHob ();\r
1326 //\r
1327 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.\r
1328 //\r
1329 if (BootMode == BOOT_IN_RECOVERY_MODE) {\r
1330 return;\r
1331 }\r
1332\r
7bee5a76 1333 //\r
a97a8596 1334 // Only check the the Memory Type Information variable in the boot mode \r
7bee5a76
RN
1335 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type\r
1336 // Information is not valid in this boot mode.\r
1337 //\r
8c716296 1338 if (BootMode != BOOT_WITH_DEFAULT_SETTINGS) {\r
a4e9b4f6 1339 VariableSize = 0;\r
1340 Status = gRT->GetVariable (\r
1341 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
1342 &gEfiMemoryTypeInformationGuid,\r
1343 NULL, \r
1344 &VariableSize, \r
1345 NULL\r
1346 );\r
1347 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1348 MemoryTypeInformationVariableExists = TRUE;\r
a4e9b4f6 1349 }\r
1350 }\r
5c08e117 1351\r
1352 //\r
1353 // Retrieve the current memory usage statistics. If they are not found, then\r
1354 // no adjustments can be made to the Memory Type Information variable.\r
1355 //\r
1356 Status = EfiGetSystemConfigurationTable (\r
1357 &gEfiMemoryTypeInformationGuid,\r
1358 (VOID **) &CurrentMemoryTypeInformation\r
1359 );\r
1360 if (EFI_ERROR (Status) || CurrentMemoryTypeInformation == NULL) {\r
1361 return;\r
1362 }\r
1363\r
1364 //\r
1365 // Get the Memory Type Information settings from Hob if they exist,\r
1366 // PEI is responsible for getting them from variable and build a Hob to save them.\r
1367 // If the previous Memory Type Information is not available, then set defaults\r
1368 //\r
1369 GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);\r
1370 if (GuidHob == NULL) {\r
1371 //\r
1372 // If Platform has not built Memory Type Info into the Hob, just return.\r
1373 //\r
1374 return;\r
1375 }\r
1376 PreviousMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);\r
1377 VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);\r
1378\r
1379 //\r
1380 // Use a heuristic to adjust the Memory Type Information for the next boot\r
1381 //\r
a4e9b4f6 1382 DEBUG ((EFI_D_INFO, "Memory Previous Current Next \n"));\r
1383 DEBUG ((EFI_D_INFO, " Type Pages Pages Pages \n"));\r
1384 DEBUG ((EFI_D_INFO, "====== ======== ======== ========\n"));\r
1385\r
5c08e117 1386 for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
1387\r
5c08e117 1388 for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {\r
1389 if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {\r
5c08e117 1390 break;\r
1391 }\r
1392 }\r
5c08e117 1393 if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {\r
1394 continue;\r
1395 }\r
1396\r
a97a8596
RN
1397 //\r
1398 // Previous is the number of pages pre-allocated\r
1399 // Current is the number of pages actually needed\r
1400 //\r
5c08e117 1401 Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;\r
a97a8596
RN
1402 Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;\r
1403 Next = Previous;\r
5c08e117 1404\r
1405 //\r
1406 // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail\r
1407 //\r
a97a8596
RN
1408 if (Current < Previous) {\r
1409 if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {\r
1410 Next = Current + (Current >> 2);\r
1411 } else if (!MemoryTypeInformationVariableExists) {\r
1412 Next = MAX (Current + (Current >> 2), Previous);\r
1413 }\r
a4e9b4f6 1414 } else if (Current > Previous) {\r
5c08e117 1415 Next = Current + (Current >> 2);\r
5c08e117 1416 }\r
1417 if (Next > 0 && Next < 4) {\r
1418 Next = 4;\r
1419 }\r
1420\r
1421 if (Next != Previous) {\r
1422 PreviousMemoryTypeInformation[Index].NumberOfPages = Next;\r
7bee5a76 1423 MemoryTypeInformationModified = TRUE;\r
5c08e117 1424 }\r
1425\r
a4e9b4f6 1426 DEBUG ((EFI_D_INFO, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation[Index].Type, Previous, Current, Next));\r
5c08e117 1427 }\r
1428\r
1429 //\r
7bee5a76
RN
1430 // If any changes were made to the Memory Type Information settings, then set the new variable value;\r
1431 // Or create the variable in first boot.\r
5c08e117 1432 //\r
7bee5a76 1433 if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) {\r
5c08e117 1434 Status = gRT->SetVariable (\r
7bee5a76
RN
1435 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
1436 &gEfiMemoryTypeInformationGuid,\r
1437 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
1438 VariableSize,\r
1439 PreviousMemoryTypeInformation\r
1440 );\r
1441\r
1442 //\r
1443 // If the Memory Type Information settings have been modified, then reset the platform\r
1444 // so the new Memory Type Information setting will be used to guarantee that an S4\r
1445 // entry/resume cycle will not fail.\r
1446 //\r
7caf72a9
RN
1447 if (MemoryTypeInformationModified && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
1448 DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));\r
7bee5a76
RN
1449 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
1450 }\r
5c08e117 1451 }\r
5c08e117 1452}\r
1453\r
1454/**\r
7caf72a9 1455 This routine is kept for backward compatibility.\r
5c08e117 1456**/\r
1457VOID\r
1458EFIAPI\r
1459BdsLibSaveMemoryTypeInformation (\r
1460 VOID\r
1461 )\r
1462{\r
5c08e117 1463}\r
1464\r
1465\r
337661bb 1466/**\r
1467 Identify a user and, if authenticated, returns the current user profile handle.\r
1468\r
1469 @param[out] User Point to user profile handle.\r
1470 \r
1471 @retval EFI_SUCCESS User is successfully identified, or user identification\r
1472 is not supported.\r
1473 @retval EFI_ACCESS_DENIED User is not successfully identified\r
1474\r
1475**/\r
1476EFI_STATUS\r
1477EFIAPI\r
1478BdsLibUserIdentify (\r
1479 OUT EFI_USER_PROFILE_HANDLE *User\r
1480 )\r
1481{\r
1482 EFI_STATUS Status;\r
1483 EFI_USER_MANAGER_PROTOCOL *Manager;\r
1484 \r
1485 Status = gBS->LocateProtocol (\r
1486 &gEfiUserManagerProtocolGuid,\r
1487 NULL,\r
1488 (VOID **) &Manager\r
1489 );\r
1490 if (EFI_ERROR (Status)) {\r
1491 return EFI_SUCCESS;\r
1492 }\r
1493\r
1494 return Manager->Identify (Manager, User);\r
1495}\r
1496\r