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