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