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