]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c
IntelFrameworkModulePkg GenericBdsLib: Use safe string functions
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / GenericBdsLib / BdsMisc.c
1 /** @file
2 Misc BDS library function
3
4 Copyright (c) 2004 - 2015, 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 (UINTN) (Char - L'0');
573 }
574
575 if ((Char >= L'A') && (Char <= L'F')) {
576 return (UINTN) (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 - 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 = AllocateCopyPool (
1131 MAX_STRING_LEN * sizeof (CHAR16),
1132 L"Configuration changed. Reset to apply it Now."
1133 );
1134 ASSERT (StringBuffer1 != NULL);
1135 StringBuffer2 = AllocateCopyPool (
1136 MAX_STRING_LEN * sizeof (CHAR16),
1137 L"Press ENTER to reset"
1138 );
1139 ASSERT (StringBuffer2 != NULL);
1140 //
1141 // Popup a menu to notice user
1142 //
1143 do {
1144 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, StringBuffer1, StringBuffer2, NULL);
1145 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);
1146
1147 FreePool (StringBuffer1);
1148 FreePool (StringBuffer2);
1149
1150 gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);
1151 }
1152 }
1153 }
1154
1155 /**
1156 Get the headers (dos, image, optional header) from an image
1157
1158 @param Device SimpleFileSystem device handle
1159 @param FileName File name for the image
1160 @param DosHeader Pointer to dos header
1161 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
1162
1163 @retval EFI_SUCCESS Successfully get the machine type.
1164 @retval EFI_NOT_FOUND The file is not found.
1165 @retval EFI_LOAD_ERROR File is not a valid image file.
1166
1167 **/
1168 EFI_STATUS
1169 EFIAPI
1170 BdsLibGetImageHeader (
1171 IN EFI_HANDLE Device,
1172 IN CHAR16 *FileName,
1173 OUT EFI_IMAGE_DOS_HEADER *DosHeader,
1174 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
1175 )
1176 {
1177 EFI_STATUS Status;
1178 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;
1179 EFI_FILE_HANDLE Root;
1180 EFI_FILE_HANDLE ThisFile;
1181 UINTN BufferSize;
1182 UINT64 FileSize;
1183 EFI_FILE_INFO *Info;
1184
1185 Root = NULL;
1186 ThisFile = NULL;
1187 //
1188 // Handle the file system interface to the device
1189 //
1190 Status = gBS->HandleProtocol (
1191 Device,
1192 &gEfiSimpleFileSystemProtocolGuid,
1193 (VOID *) &Volume
1194 );
1195 if (EFI_ERROR (Status)) {
1196 goto Done;
1197 }
1198
1199 Status = Volume->OpenVolume (
1200 Volume,
1201 &Root
1202 );
1203 if (EFI_ERROR (Status)) {
1204 Root = NULL;
1205 goto Done;
1206 }
1207 ASSERT (Root != NULL);
1208 Status = Root->Open (Root, &ThisFile, FileName, EFI_FILE_MODE_READ, 0);
1209 if (EFI_ERROR (Status)) {
1210 goto Done;
1211 }
1212 ASSERT (ThisFile != NULL);
1213
1214 //
1215 // Get file size
1216 //
1217 BufferSize = SIZE_OF_EFI_FILE_INFO + 200;
1218 do {
1219 Info = NULL;
1220 Status = gBS->AllocatePool (EfiBootServicesData, BufferSize, (VOID **) &Info);
1221 if (EFI_ERROR (Status)) {
1222 goto Done;
1223 }
1224 Status = ThisFile->GetInfo (
1225 ThisFile,
1226 &gEfiFileInfoGuid,
1227 &BufferSize,
1228 Info
1229 );
1230 if (!EFI_ERROR (Status)) {
1231 break;
1232 }
1233 if (Status != EFI_BUFFER_TOO_SMALL) {
1234 FreePool (Info);
1235 goto Done;
1236 }
1237 FreePool (Info);
1238 } while (TRUE);
1239
1240 FileSize = Info->FileSize;
1241 FreePool (Info);
1242
1243 //
1244 // Read dos header
1245 //
1246 BufferSize = sizeof (EFI_IMAGE_DOS_HEADER);
1247 Status = ThisFile->Read (ThisFile, &BufferSize, DosHeader);
1248 if (EFI_ERROR (Status) ||
1249 BufferSize < sizeof (EFI_IMAGE_DOS_HEADER) ||
1250 FileSize <= DosHeader->e_lfanew ||
1251 DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {
1252 Status = EFI_LOAD_ERROR;
1253 goto Done;
1254 }
1255
1256 //
1257 // Move to PE signature
1258 //
1259 Status = ThisFile->SetPosition (ThisFile, DosHeader->e_lfanew);
1260 if (EFI_ERROR (Status)) {
1261 Status = EFI_LOAD_ERROR;
1262 goto Done;
1263 }
1264
1265 //
1266 // Read and check PE signature
1267 //
1268 BufferSize = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
1269 Status = ThisFile->Read (ThisFile, &BufferSize, Hdr.Pe32);
1270 if (EFI_ERROR (Status) ||
1271 BufferSize < sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION) ||
1272 Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {
1273 Status = EFI_LOAD_ERROR;
1274 goto Done;
1275 }
1276
1277 //
1278 // Check PE32 or PE32+ magic
1279 //
1280 if (Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC &&
1281 Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
1282 Status = EFI_LOAD_ERROR;
1283 goto Done;
1284 }
1285
1286 Done:
1287 if (ThisFile != NULL) {
1288 ThisFile->Close (ThisFile);
1289 }
1290 if (Root != NULL) {
1291 Root->Close (Root);
1292 }
1293 return Status;
1294 }
1295
1296 /**
1297 This routine adjust the memory information for different memory type and
1298 save them into the variables for next boot.
1299 **/
1300 VOID
1301 BdsSetMemoryTypeInformationVariable (
1302 VOID
1303 )
1304 {
1305 EFI_STATUS Status;
1306 EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;
1307 EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;
1308 UINTN VariableSize;
1309 UINTN Index;
1310 UINTN Index1;
1311 UINT32 Previous;
1312 UINT32 Current;
1313 UINT32 Next;
1314 EFI_HOB_GUID_TYPE *GuidHob;
1315 BOOLEAN MemoryTypeInformationModified;
1316 BOOLEAN MemoryTypeInformationVariableExists;
1317 EFI_BOOT_MODE BootMode;
1318
1319 MemoryTypeInformationModified = FALSE;
1320 MemoryTypeInformationVariableExists = FALSE;
1321
1322
1323 BootMode = GetBootModeHob ();
1324 //
1325 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.
1326 //
1327 if (BootMode == BOOT_IN_RECOVERY_MODE) {
1328 return;
1329 }
1330
1331 //
1332 // Only check the the Memory Type Information variable in the boot mode
1333 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type
1334 // Information is not valid in this boot mode.
1335 //
1336 if (BootMode != BOOT_WITH_DEFAULT_SETTINGS) {
1337 VariableSize = 0;
1338 Status = gRT->GetVariable (
1339 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
1340 &gEfiMemoryTypeInformationGuid,
1341 NULL,
1342 &VariableSize,
1343 NULL
1344 );
1345 if (Status == EFI_BUFFER_TOO_SMALL) {
1346 MemoryTypeInformationVariableExists = TRUE;
1347 }
1348 }
1349
1350 //
1351 // Retrieve the current memory usage statistics. If they are not found, then
1352 // no adjustments can be made to the Memory Type Information variable.
1353 //
1354 Status = EfiGetSystemConfigurationTable (
1355 &gEfiMemoryTypeInformationGuid,
1356 (VOID **) &CurrentMemoryTypeInformation
1357 );
1358 if (EFI_ERROR (Status) || CurrentMemoryTypeInformation == NULL) {
1359 return;
1360 }
1361
1362 //
1363 // Get the Memory Type Information settings from Hob if they exist,
1364 // PEI is responsible for getting them from variable and build a Hob to save them.
1365 // If the previous Memory Type Information is not available, then set defaults
1366 //
1367 GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);
1368 if (GuidHob == NULL) {
1369 //
1370 // If Platform has not built Memory Type Info into the Hob, just return.
1371 //
1372 return;
1373 }
1374 PreviousMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);
1375 VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
1376
1377 //
1378 // Use a heuristic to adjust the Memory Type Information for the next boot
1379 //
1380 DEBUG ((EFI_D_INFO, "Memory Previous Current Next \n"));
1381 DEBUG ((EFI_D_INFO, " Type Pages Pages Pages \n"));
1382 DEBUG ((EFI_D_INFO, "====== ======== ======== ========\n"));
1383
1384 for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {
1385
1386 for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {
1387 if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {
1388 break;
1389 }
1390 }
1391 if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {
1392 continue;
1393 }
1394
1395 //
1396 // Previous is the number of pages pre-allocated
1397 // Current is the number of pages actually needed
1398 //
1399 Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;
1400 Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;
1401 Next = Previous;
1402
1403 //
1404 // Inconsistent Memory Reserved across bootings may lead to S4 fail
1405 // Write next varible to 125% * current when the pre-allocated memory is:
1406 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING
1407 // 2. Less than the needed memory
1408 //
1409 if ((Current + (Current >> 1)) < Previous) {
1410 if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
1411 Next = Current + (Current >> 2);
1412 }
1413 } else if (Current > Previous) {
1414 Next = Current + (Current >> 2);
1415 }
1416 if (Next > 0 && Next < 4) {
1417 Next = 4;
1418 }
1419
1420 if (Next != Previous) {
1421 PreviousMemoryTypeInformation[Index].NumberOfPages = Next;
1422 MemoryTypeInformationModified = TRUE;
1423 }
1424
1425 DEBUG ((EFI_D_INFO, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation[Index].Type, Previous, Current, Next));
1426 }
1427
1428 //
1429 // If any changes were made to the Memory Type Information settings, then set the new variable value;
1430 // Or create the variable in first boot.
1431 //
1432 if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) {
1433 Status = SetVariableAndReportStatusCodeOnError (
1434 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
1435 &gEfiMemoryTypeInformationGuid,
1436 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
1437 VariableSize,
1438 PreviousMemoryTypeInformation
1439 );
1440
1441 if (!EFI_ERROR (Status)) {
1442 //
1443 // If the Memory Type Information settings have been modified, then reset the platform
1444 // so the new Memory Type Information setting will be used to guarantee that an S4
1445 // entry/resume cycle will not fail.
1446 //
1447 if (MemoryTypeInformationModified && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {
1448 DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));
1449 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
1450 }
1451 } else {
1452 DEBUG ((EFI_D_ERROR, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));
1453 }
1454 }
1455 }
1456
1457 /**
1458 This routine is kept for backward compatibility.
1459 **/
1460 VOID
1461 EFIAPI
1462 BdsLibSaveMemoryTypeInformation (
1463 VOID
1464 )
1465 {
1466 }
1467
1468
1469 /**
1470 Identify a user and, if authenticated, returns the current user profile handle.
1471
1472 @param[out] User Point to user profile handle.
1473
1474 @retval EFI_SUCCESS User is successfully identified, or user identification
1475 is not supported.
1476 @retval EFI_ACCESS_DENIED User is not successfully identified
1477
1478 **/
1479 EFI_STATUS
1480 EFIAPI
1481 BdsLibUserIdentify (
1482 OUT EFI_USER_PROFILE_HANDLE *User
1483 )
1484 {
1485 EFI_STATUS Status;
1486 EFI_USER_MANAGER_PROTOCOL *Manager;
1487
1488 Status = gBS->LocateProtocol (
1489 &gEfiUserManagerProtocolGuid,
1490 NULL,
1491 (VOID **) &Manager
1492 );
1493 if (EFI_ERROR (Status)) {
1494 return EFI_SUCCESS;
1495 }
1496
1497 return Manager->Identify (Manager, User);
1498 }
1499
1500 /**
1501 Set the variable and report the error through status code upon failure.
1502
1503 @param VariableName A Null-terminated string that is the name of the vendor's variable.
1504 Each VariableName is unique for each VendorGuid. VariableName must
1505 contain 1 or more characters. If VariableName is an empty string,
1506 then EFI_INVALID_PARAMETER is returned.
1507 @param VendorGuid A unique identifier for the vendor.
1508 @param Attributes Attributes bitmask to set for the variable.
1509 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
1510 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
1511 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
1512 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
1513 set, then a SetVariable() call with a DataSize of zero will not cause any change to
1514 the variable value (the timestamp associated with the variable may be updated however
1515 even if no new data value is provided,see the description of the
1516 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
1517 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
1518 @param Data The contents for the variable.
1519
1520 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
1521 defined by the Attributes.
1522 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
1523 DataSize exceeds the maximum allowed.
1524 @retval EFI_INVALID_PARAMETER VariableName is an empty string.
1525 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
1526 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
1527 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
1528 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
1529 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
1530 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
1531 does NOT pass the validation check carried out by the firmware.
1532
1533 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
1534 **/
1535 EFI_STATUS
1536 SetVariableAndReportStatusCodeOnError (
1537 IN CHAR16 *VariableName,
1538 IN EFI_GUID *VendorGuid,
1539 IN UINT32 Attributes,
1540 IN UINTN DataSize,
1541 IN VOID *Data
1542 )
1543 {
1544 EFI_STATUS Status;
1545 EDKII_SET_VARIABLE_STATUS *SetVariableStatus;
1546 UINTN NameSize;
1547
1548 Status = gRT->SetVariable (
1549 VariableName,
1550 VendorGuid,
1551 Attributes,
1552 DataSize,
1553 Data
1554 );
1555 if (EFI_ERROR (Status)) {
1556 NameSize = StrSize (VariableName);
1557 SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);
1558 if (SetVariableStatus != NULL) {
1559 CopyGuid (&SetVariableStatus->Guid, VendorGuid);
1560 SetVariableStatus->NameSize = NameSize;
1561 SetVariableStatus->DataSize = DataSize;
1562 SetVariableStatus->SetStatus = Status;
1563 SetVariableStatus->Attributes = Attributes;
1564 CopyMem (SetVariableStatus + 1, VariableName, NameSize);
1565 if ((Data != NULL) && (DataSize != 0)) {
1566 CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data, DataSize);
1567 }
1568
1569 REPORT_STATUS_CODE_EX (
1570 EFI_ERROR_CODE,
1571 PcdGet32 (PcdErrorCodeSetVariable),
1572 0,
1573 NULL,
1574 &gEdkiiStatusCodeDataTypeVariableGuid,
1575 SetVariableStatus,
1576 sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize
1577 );
1578
1579 FreePool (SetVariableStatus);
1580 }
1581 }
1582
1583 return Status;
1584 }
1585