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