3 Copyright (c) 2004 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Misc BDS library function
23 #include "InternalBdsLib.h"
26 #define MAX_STRING_LEN 200
28 static BOOLEAN mFeaturerSwitch
= TRUE
;
29 static BOOLEAN mResetRequired
= FALSE
;
31 extern UINT16 gPlatformBootTimeOutDefault
;
35 Return the default value for system Timeout variable.
39 @return Timeout value.
52 // Return Timeout variable or 0xffff if no valid
53 // Timeout variable exists.
55 Size
= sizeof (UINT16
);
56 Status
= gRT
->GetVariable (L
"Timeout", &gEfiGlobalVariableGuid
, NULL
, &Size
, &Timeout
);
57 if (!EFI_ERROR (Status
)) {
61 // To make the current EFI Automatic-Test activity possible, just add
62 // following code to make AutoBoot enabled when this variable is not
64 // This code should be removed later.
66 Timeout
= PcdGet16 (PcdPlatformBootTimeOutDefault
);
69 // Notes: Platform should set default variable if non exists on all error cases!!!
71 Status
= gRT
->SetVariable (
73 &gEfiGlobalVariableGuid
,
74 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
83 The function will go through the driver optoin link list, load and start
84 every driver the driver optoin device path point to.
86 @param BdsDriverLists The header of the current driver option link list
93 IN LIST_ENTRY
*BdsDriverLists
98 BDS_COMMON_OPTION
*Option
;
99 EFI_HANDLE ImageHandle
;
100 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
103 BOOLEAN ReconnectAll
;
105 ReconnectAll
= FALSE
;
108 // Process the driver option
110 for (Link
= BdsDriverLists
->ForwardLink
; Link
!= BdsDriverLists
; Link
= Link
->ForwardLink
) {
111 Option
= CR (Link
, BDS_COMMON_OPTION
, Link
, BDS_LOAD_OPTION_SIGNATURE
);
113 // If a load option is not marked as LOAD_OPTION_ACTIVE,
114 // the boot manager will not automatically load the option.
116 if (!IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_ACTIVE
)) {
120 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
121 // then all of the EFI drivers in the system will be disconnected and
122 // reconnected after the last driver load option is processed.
124 if (IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_FORCE_RECONNECT
)) {
128 // Make sure the driver path is connected.
130 BdsLibConnectDevicePath (Option
->DevicePath
);
133 // Load and start the image that Driver#### describes
135 Status
= gBS
->LoadImage (
144 if (!EFI_ERROR (Status
)) {
145 gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
148 // Verify whether this image is a driver, if not,
149 // exit it and continue to parse next load option
151 if (ImageInfo
->ImageCodeType
!= EfiBootServicesCode
&& ImageInfo
->ImageCodeType
!= EfiRuntimeServicesCode
) {
152 gBS
->Exit (ImageHandle
, EFI_INVALID_PARAMETER
, 0, NULL
);
156 if (Option
->LoadOptionsSize
!= 0) {
157 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
158 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
161 // Before calling the image, enable the Watchdog Timer for
162 // the 5 Minute period
164 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
166 Status
= gBS
->StartImage (ImageHandle
, &ExitDataSize
, &ExitData
);
167 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Driver Return Status = %r\n", Status
));
170 // Clear the Watchdog Timer after the image returns
172 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
176 // Process the LOAD_OPTION_FORCE_RECONNECT driver option
179 BdsLibDisconnectAllEfi ();
187 Get the Option Number that does not used
188 Try to locate the specific option variable one by one untile find a free number
190 @param VariableName Indicate if the boot#### or driver#### option
192 @return The Minimal Free Option Number
196 BdsLibGetFreeOptionNumber (
197 IN CHAR16
*VariableName
203 UINT16
*OptionBuffer
;
207 // Try to find the minimum free number from 0, 1, 2, 3....
211 if (*VariableName
== 'B') {
212 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Boot%04x", Index
);
214 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Driver%04x", Index
);
217 // try if the option number is used
219 OptionBuffer
= BdsLibGetVariableAndSize (
221 &gEfiGlobalVariableGuid
,
224 if (OptionBuffer
== NULL
) {
230 Number
= (UINT16
) Index
;
236 This function will register the new boot#### or driver#### option base on
237 the VariableName. The new registered boot#### or driver#### will be linked
238 to BdsOptionList and also update to the VariableName. After the boot#### or
239 driver#### updated, the BootOrder or DriverOrder will also be updated.
241 @param BdsOptionList The header of the boot#### or driver#### link list
242 @param DevicePath The device path which the boot#### or driver####
244 @param String The description of the boot#### or driver####
245 @param VariableName Indicate if the boot#### or driver#### option
247 @retval EFI_SUCCESS The boot#### or driver#### have been success
249 @retval EFI_STATUS Return the status of gRT->SetVariable ().
253 BdsLibRegisterNewOption (
254 IN LIST_ENTRY
*BdsOptionList
,
255 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
257 IN CHAR16
*VariableName
262 UINT16 RegisterOptionNumber
;
263 UINT16
*TempOptionPtr
;
264 UINTN TempOptionSize
;
265 UINT16
*OptionOrderPtr
;
269 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
271 CHAR16 OptionName
[10];
272 BOOLEAN UpdateDescription
;
273 UINT16 BootOrderEntry
;
280 OptionDevicePath
= NULL
;
282 OptionOrderPtr
= NULL
;
283 UpdateDescription
= FALSE
;
284 ZeroMem (OptionName
, sizeof (OptionName
));
287 TempOptionPtr
= BdsLibGetVariableAndSize (
289 &gEfiGlobalVariableGuid
,
294 // Compare with current option variable
296 for (Index
= 0; Index
< TempOptionSize
/ sizeof (UINT16
); Index
++) {
298 if (*VariableName
== 'B') {
299 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", TempOptionPtr
[Index
]);
301 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", TempOptionPtr
[Index
]);
304 OptionPtr
= BdsLibGetVariableAndSize (
306 &gEfiGlobalVariableGuid
,
309 if (OptionPtr
== NULL
) {
313 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
314 Description
= (CHAR16
*) TempPtr
;
315 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
316 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
319 // Notes: the description may will change base on the GetStringToken
321 if (CompareMem (OptionDevicePath
, DevicePath
, GetDevicePathSize (OptionDevicePath
)) == 0) {
322 if (CompareMem (Description
, String
, StrSize (Description
)) == 0) {
324 // Got the option, so just return
326 gBS
->FreePool (OptionPtr
);
327 gBS
->FreePool (TempOptionPtr
);
331 // Option description changed, need update.
333 UpdateDescription
= TRUE
;
334 gBS
->FreePool (OptionPtr
);
339 gBS
->FreePool (OptionPtr
);
342 OptionSize
= sizeof (UINT32
) + sizeof (UINT16
) + StrSize (String
);
343 OptionSize
+= GetDevicePathSize (DevicePath
);
344 OptionPtr
= AllocateZeroPool (OptionSize
);
346 *(UINT32
*) TempPtr
= LOAD_OPTION_ACTIVE
;
347 TempPtr
+= sizeof (UINT32
);
348 *(UINT16
*) TempPtr
= (UINT16
) GetDevicePathSize (DevicePath
);
349 TempPtr
+= sizeof (UINT16
);
350 CopyMem (TempPtr
, String
, StrSize (String
));
351 TempPtr
+= StrSize (String
);
352 CopyMem (TempPtr
, DevicePath
, GetDevicePathSize (DevicePath
));
354 if (UpdateDescription
) {
356 // The number in option#### to be updated
358 RegisterOptionNumber
= TempOptionPtr
[Index
];
361 // The new option#### number
363 RegisterOptionNumber
= BdsLibGetFreeOptionNumber(VariableName
);
366 if (*VariableName
== 'B') {
367 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", RegisterOptionNumber
);
369 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", RegisterOptionNumber
);
372 Status
= gRT
->SetVariable (
374 &gEfiGlobalVariableGuid
,
375 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
380 // Return if only need to update a changed description or fail to set option.
382 if (EFI_ERROR (Status
) || UpdateDescription
) {
383 gBS
->FreePool (OptionPtr
);
384 gBS
->FreePool (TempOptionPtr
);
388 gBS
->FreePool (OptionPtr
);
391 // Update the option order variable
395 // If no option order
397 if (TempOptionSize
== 0) {
399 Status
= gRT
->SetVariable (
401 &gEfiGlobalVariableGuid
,
402 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
406 if (EFI_ERROR (Status
)) {
407 gBS
->FreePool (TempOptionPtr
);
414 // Append the new option number to the original option order
416 OrderItemNum
= (TempOptionSize
/ sizeof (UINT16
)) + 1 ;
417 OptionOrderPtr
= AllocateZeroPool ( OrderItemNum
* sizeof (UINT16
));
418 CopyMem (OptionOrderPtr
, TempOptionPtr
, (OrderItemNum
- 1) * sizeof (UINT16
));
420 OptionOrderPtr
[Index
] = RegisterOptionNumber
;
422 Status
= gRT
->SetVariable (
424 &gEfiGlobalVariableGuid
,
425 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
426 OrderItemNum
* sizeof (UINT16
),
429 if (EFI_ERROR (Status
)) {
430 gBS
->FreePool (TempOptionPtr
);
431 gBS
->FreePool (OptionOrderPtr
);
435 gBS
->FreePool (TempOptionPtr
);
436 gBS
->FreePool (OptionOrderPtr
);
443 Build the boot#### or driver#### option from the VariableName, the
444 build boot#### or driver#### will also be linked to BdsCommonOptionList
446 @param BdsCommonOptionList The header of the boot#### or driver#### option
448 @param VariableName EFI Variable name indicate if it is boot#### or
451 @retval BDS_COMMON_OPTION Get the option just been created
452 @retval NULL Failed to get the new option
456 BdsLibVariableToOption (
457 IN OUT LIST_ENTRY
*BdsCommonOptionList
,
458 IN CHAR16
*VariableName
466 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
467 BDS_COMMON_OPTION
*Option
;
469 UINT32 LoadOptionsSize
;
473 // Read the variable. We will never free this data.
475 Variable
= BdsLibGetVariableAndSize (
477 &gEfiGlobalVariableGuid
,
480 if (Variable
== NULL
) {
484 // Notes: careful defined the variable of Boot#### or
485 // Driver####, consider use some macro to abstract the code
488 // Get the option attribute
491 Attribute
= *(UINT32
*) Variable
;
492 TempPtr
+= sizeof (UINT32
);
495 // Get the option's device path size
497 FilePathSize
= *(UINT16
*) TempPtr
;
498 TempPtr
+= sizeof (UINT16
);
501 // Get the option's description string
503 Description
= (CHAR16
*) TempPtr
;
506 // Get the option's description string size
508 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
511 // Get the option's device path
513 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
514 TempPtr
+= FilePathSize
;
516 LoadOptions
= TempPtr
;
517 LoadOptionsSize
= (UINT32
) (VariableSize
- (UINTN
) (TempPtr
- Variable
));
520 // The Console variables may have multiple device paths, so make
521 // an Entry for each one.
523 Option
= AllocateZeroPool (sizeof (BDS_COMMON_OPTION
));
524 if (Option
== NULL
) {
528 Option
->Signature
= BDS_LOAD_OPTION_SIGNATURE
;
529 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
530 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
531 Option
->Attribute
= Attribute
;
532 Option
->Description
= AllocateZeroPool (StrSize (Description
));
533 CopyMem (Option
->Description
, Description
, StrSize (Description
));
534 Option
->LoadOptions
= AllocateZeroPool (LoadOptionsSize
);
535 CopyMem (Option
->LoadOptions
, LoadOptions
, LoadOptionsSize
);
536 Option
->LoadOptionsSize
= LoadOptionsSize
;
539 // Get the value from VariableName Unicode string
540 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this
541 // Unicode stream to ASCII without any loss in meaning.
543 if (*VariableName
== 'B') {
544 NumOff
= sizeof (L
"Boot")/sizeof(CHAR16
) -1 ;
545 Option
->BootCurrent
= (UINT16
) ((VariableName
[NumOff
] -'0') * 0x1000);
546 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+1]-'0') * 0x100));
547 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+2]-'0') * 0x10));
548 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+3]-'0')));
551 // Insert active entry to BdsDeviceList
553 if ((Option
->Attribute
& LOAD_OPTION_ACTIVE
) == LOAD_OPTION_ACTIVE
) {
554 InsertTailList (BdsCommonOptionList
, &Option
->Link
);
555 gBS
->FreePool (Variable
);
559 gBS
->FreePool (Variable
);
560 gBS
->FreePool (Option
);
567 Process BootOrder, or DriverOrder variables, by calling
568 BdsLibVariableToOption () for each UINT16 in the variables.
570 @param BdsCommonOptionList The header of the option list base on variable
572 @param VariableName EFI Variable name indicate the BootOrder or
575 @retval EFI_SUCCESS Success create the boot option or driver option
577 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list
581 BdsLibBuildOptionFromVar (
582 IN LIST_ENTRY
*BdsCommonOptionList
,
583 IN CHAR16
*VariableName
587 UINTN OptionOrderSize
;
589 BDS_COMMON_OPTION
*Option
;
590 CHAR16 OptionName
[20];
593 // Zero Buffer in order to get all BOOT#### variables
595 ZeroMem (OptionName
, sizeof (OptionName
));
598 // Read the BootOrder, or DriverOrder variable.
600 OptionOrder
= BdsLibGetVariableAndSize (
602 &gEfiGlobalVariableGuid
,
605 if (OptionOrder
== NULL
) {
606 return EFI_OUT_OF_RESOURCES
;
609 for (Index
= 0; Index
< OptionOrderSize
/ sizeof (UINT16
); Index
++) {
610 if (*VariableName
== 'B') {
611 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionOrder
[Index
]);
613 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", OptionOrder
[Index
]);
616 Option
= BdsLibVariableToOption (BdsCommonOptionList
, OptionName
);
617 Option
->BootCurrent
= OptionOrder
[Index
];
621 gBS
->FreePool (OptionOrder
);
628 Get boot mode by looking up configuration table and parsing HOB list
630 @param BootMode Boot mode from PEI handoff HOB.
632 @retval EFI_SUCCESS Successfully get boot mode
633 @retval EFI_NOT_FOUND Can not find the current system boot mode
638 OUT EFI_BOOT_MODE
*BootMode
647 Status
= EfiGetSystemConfigurationTable (&gEfiHobListGuid
, &HobList
);
648 if (EFI_ERROR (Status
)) {
649 DEBUG ((DEBUG_ERROR
, "Hob list not found\n"));
651 return EFI_NOT_FOUND
;
654 Status
= R8_GetHobBootMode (HobList
, BootMode
);
655 if (EFI_ERROR (Status
)) {
656 return EFI_NOT_FOUND
;
664 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
665 buffer, and the size of the buffer. If failure return NULL.
667 @param Name String part of EFI variable name
668 @param VendorGuid GUID part of EFI variable name
669 @param VariableSize Returns the size of the EFI variable that was read
671 @return Dynamically allocated memory that contains a copy of the EFI variable.
672 @return Caller is responsible freeing the buffer.
673 @retval NULL Variable was not read
677 BdsLibGetVariableAndSize (
679 IN EFI_GUID
*VendorGuid
,
680 OUT UINTN
*VariableSize
690 // Pass in a zero size buffer to find the required buffer size.
693 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
694 if (Status
== EFI_BUFFER_TOO_SMALL
) {
696 // Allocate the buffer to return
698 Buffer
= AllocateZeroPool (BufferSize
);
699 if (Buffer
== NULL
) {
703 // Read variable into the allocated buffer.
705 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
706 if (EFI_ERROR (Status
)) {
711 *VariableSize
= BufferSize
;
717 Delete the instance in Multi which matches partly with Single instance
719 @param Multi A pointer to a multi-instance device path data
721 @param Single A pointer to a single-instance device path data
724 @return This function will remove the device path instances in Multi which partly
725 @return match with the Single, and return the result device path. If there is no
726 @return remaining device path as a result, this function will return NULL.
729 EFI_DEVICE_PATH_PROTOCOL
*
730 BdsLibDelPartMatchInstance (
731 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
732 IN EFI_DEVICE_PATH_PROTOCOL
*Single
735 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
736 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
737 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
742 NewDevicePath
= NULL
;
743 TempNewDevicePath
= NULL
;
745 if (Multi
== NULL
|| Single
== NULL
) {
749 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
750 SingleDpSize
= GetDevicePathSize (Single
) - END_DEVICE_PATH_LENGTH
;
751 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
753 while (Instance
!= NULL
) {
755 Size
= (SingleDpSize
< InstanceSize
) ? SingleDpSize
: InstanceSize
;
757 if ((CompareMem (Instance
, Single
, Size
) != 0)) {
759 // Append the device path instance which does not match with Single
761 TempNewDevicePath
= NewDevicePath
;
762 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, Instance
);
763 SafeFreePool(TempNewDevicePath
);
765 SafeFreePool(Instance
);
766 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
767 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
770 return NewDevicePath
;
775 Function compares a device path data structure to that of all the nodes of a
776 second device path instance.
778 @param Multi A pointer to a multi-instance device path data
780 @param Single A pointer to a single-instance device path data
783 @retval TRUE If the Single is contained within Multi
784 @retval FALSE The Single is not match within Multi
788 BdsLibMatchDevicePaths (
789 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
790 IN EFI_DEVICE_PATH_PROTOCOL
*Single
793 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
794 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
797 if (!Multi
|| !Single
) {
802 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
805 // Search for the match of 'Single' in 'Multi'
807 while (DevicePathInst
!= NULL
) {
809 // If the single device path is found in multiple device paths,
812 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
813 gBS
->FreePool (DevicePathInst
);
817 gBS
->FreePool (DevicePathInst
);
818 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
826 This function prints a series of strings.
828 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
829 @param ... A variable argument list containing series of
830 strings, the last string must be NULL.
832 @retval EFI_SUCCESS Success print out the string using ConOut.
833 @retval EFI_STATUS Return the status of the ConOut->OutputString ().
837 BdsLibOutputStrings (
838 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*ConOut
,
846 Status
= EFI_SUCCESS
;
847 VA_START (args
, ConOut
);
849 while (!EFI_ERROR (Status
)) {
851 // If String is NULL, then it's the end of the list
853 String
= VA_ARG (args
, CHAR16
*);
858 Status
= ConOut
->OutputString (ConOut
, String
);
860 if (EFI_ERROR (Status
)) {
869 // Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.
870 // Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if
871 // user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.
876 Enable the setup browser reset reminder feature.
877 This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.
885 EnableResetReminderFeature (
889 mFeaturerSwitch
= TRUE
;
894 Disable the setup browser reset reminder feature.
895 This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.
903 DisableResetReminderFeature (
907 mFeaturerSwitch
= FALSE
;
912 Record the info that a reset is required.
913 A module boolean variable is used to record whether a reset is required.
921 EnableResetRequired (
925 mResetRequired
= TRUE
;
930 Record the info that no reset is required.
931 A module boolean variable is used to record whether a reset is required.
939 DisableResetRequired (
943 mResetRequired
= FALSE
;
948 Check whether platform policy enable the reset reminder feature. The default is enabled.
956 IsResetReminderFeatureEnable (
960 return mFeaturerSwitch
;
965 Check if user changed any option setting which needs a system reset to be effective.
977 return mResetRequired
;
982 Check whether a reset is needed, and finish the reset reminder feature.
983 If a reset is needed, Popup a menu to notice user, and finish the feature
984 according to the user selection.
997 CHAR16
*StringBuffer1
;
998 CHAR16
*StringBuffer2
;
1002 //check any reset required change is applied? if yes, reset system
1004 if (IsResetReminderFeatureEnable ()) {
1005 if (IsResetRequired ()) {
1007 StringBuffer1
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
1008 ASSERT (StringBuffer1
!= NULL
);
1009 StringBuffer2
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
1010 ASSERT (StringBuffer2
!= NULL
);
1011 StrCpy (StringBuffer1
, L
"Configuration changed. Reset to apply it Now ? ");
1012 StrCpy (StringBuffer2
, L
"Enter (YES) / Esc (NO)");
1014 // Popup a menu to notice user
1017 IfrLibCreatePopUp (2, &Key
, StringBuffer1
, StringBuffer2
);
1018 } while ((Key
.ScanCode
!= SCAN_ESC
) && (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
));
1020 gBS
->FreePool (StringBuffer1
);
1021 gBS
->FreePool (StringBuffer2
);
1023 // If the user hits the YES Response key, reset
1025 if ((Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
)) {
1026 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
1028 gST
->ConOut
->ClearScreen (gST
->ConOut
);
1035 Get the headers (dos, image, optional header) from an image
1037 @param Device SimpleFileSystem device handle
1038 @param FileName File name for the image
1039 @param DosHeader Pointer to dos header
1040 @param ImageHeader Pointer to image header
1041 @param OptionalHeader Pointer to optional header
1043 @retval EFI_SUCCESS Successfully get the machine type.
1044 @retval EFI_NOT_FOUND The file is not found.
1045 @retval EFI_LOAD_ERROR File is not a valid image file.
1049 BdsLibGetImageHeader (
1050 IN EFI_HANDLE Device
,
1051 IN CHAR16
*FileName
,
1052 OUT EFI_IMAGE_DOS_HEADER
*DosHeader
,
1053 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
1057 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
1058 EFI_FILE_HANDLE Root
;
1059 EFI_FILE_HANDLE ThisFile
;
1062 EFI_FILE_INFO
*Info
;
1067 // Handle the file system interface to the device
1069 Status
= gBS
->HandleProtocol (
1071 &gEfiSimpleFileSystemProtocolGuid
,
1074 if (EFI_ERROR (Status
)) {
1078 Status
= Volume
->OpenVolume (
1082 if (EFI_ERROR (Status
)) {
1086 Status
= Root
->Open (Root
, &ThisFile
, FileName
, EFI_FILE_MODE_READ
, 0);
1087 if (EFI_ERROR (Status
)) {
1094 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
1097 Status
= gBS
->AllocatePool (EfiBootServicesData
, BufferSize
, (VOID
**) &Info
);
1098 if (EFI_ERROR (Status
)) {
1101 Status
= ThisFile
->GetInfo (
1107 if (!EFI_ERROR (Status
)) {
1110 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1113 gBS
->FreePool (Info
);
1116 FileSize
= Info
->FileSize
;
1117 gBS
->FreePool (Info
);
1122 BufferSize
= sizeof (EFI_IMAGE_DOS_HEADER
);
1123 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, DosHeader
);
1124 if (EFI_ERROR (Status
) ||
1125 BufferSize
< sizeof (EFI_IMAGE_DOS_HEADER
) ||
1126 FileSize
<= DosHeader
->e_lfanew
||
1127 DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
1128 Status
= EFI_LOAD_ERROR
;
1133 // Move to PE signature
1135 Status
= ThisFile
->SetPosition (ThisFile
, DosHeader
->e_lfanew
);
1136 if (EFI_ERROR (Status
)) {
1137 Status
= EFI_LOAD_ERROR
;
1142 // Read and check PE signature
1144 BufferSize
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
1145 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, Hdr
.Pe32
);
1146 if (EFI_ERROR (Status
) ||
1147 BufferSize
< sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
) ||
1148 Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1149 Status
= EFI_LOAD_ERROR
;
1154 // Check PE32 or PE32+ magic
1156 if (Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
&&
1157 Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
1158 Status
= EFI_LOAD_ERROR
;
1163 if (ThisFile
!= NULL
) {
1164 ThisFile
->Close (ThisFile
);
1174 BdsSetMemoryTypeInformationVariable (
1180 Routine Description:
1182 This routine is a notification function for legayc boot or exit boot
1183 service event. It will adjust the memory information for different
1184 memory type and save them into the variables for next boot
1188 Event - The event that triggered this notification function
1189 Context - Pointer to the notification functions context
1198 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
1199 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
1201 BOOLEAN UpdateRequired
;
1209 UpdateRequired
= FALSE
;
1212 // Retrieve the current memory usage statistics. If they are not found, then
1213 // no adjustments can be made to the Memory Type Information variable.
1215 Status
= EfiGetSystemConfigurationTable (
1216 &gEfiMemoryTypeInformationGuid
,
1217 (VOID
**) &CurrentMemoryTypeInformation
1219 if (EFI_ERROR (Status
)) {
1224 // Get the Memory Type Information settings from Hob if they exist,
1225 // PEI is responsible for getting them from variable and build a Hob to save them.
1226 // If the previous Memory Type Information is not available, then set defaults
1228 EfiGetSystemConfigurationTable (&gEfiHobListGuid
, &HobList
);
1229 Status
= R8_GetNextGuidHob (&HobList
, &gEfiMemoryTypeInformationGuid
, (VOID
**) &PreviousMemoryTypeInformation
, &VariableSize
);
1230 if (EFI_ERROR (Status
) || PreviousMemoryTypeInformation
== NULL
) {
1232 // If Platform has not built Memory Type Info into the Hob, just return.
1238 // Use a heuristic to adjust the Memory Type Information for the next boot
1240 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
1243 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
1244 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
1245 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
1250 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
1254 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
1257 // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail
1259 if (Current
> Previous
) {
1260 Next
= Current
+ (Current
>> 2);
1264 if (Next
> 0 && Next
< 4) {
1268 if (Next
!= Previous
) {
1269 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
1270 UpdateRequired
= TRUE
;
1276 // If any changes were made to the Memory Type Information settings, then set the new variable value
1278 if (UpdateRequired
) {
1279 Status
= gRT
->SetVariable (
1280 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
1281 &gEfiMemoryTypeInformationGuid
,
1282 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
1284 PreviousMemoryTypeInformation
1293 This routine register a function to adjust the different type memory page number just before booting
1294 and save the updated info into the variable for next boot to use
1303 BdsLibSaveMemoryTypeInformation (
1308 EFI_EVENT ReadyToBootEvent
;
1310 Status
= EfiCreateEventReadyToBootEx (
1312 BdsSetMemoryTypeInformationVariable
,
1316 if (EFI_ERROR (Status
)) {
1317 DEBUG ((DEBUG_ERROR
,"Bds Set Memory Type Informationa Variable Fails\n"));
1324 return the current TPL, copied from the EDKII glue lib.
1332 BdsLibGetCurrentTpl (
1338 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
1339 gBS
->RestoreTPL (Tpl
);