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 UpdateBootDevicePath
;
273 UINT16 BootOrderEntry
;
280 OptionDevicePath
= NULL
;
282 OptionOrderPtr
= NULL
;
283 UpdateBootDevicePath
= 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 (Description
, String
, StrSize (Description
)) == 0) {
322 if (CompareMem (OptionDevicePath
, DevicePath
, GetDevicePathSize (OptionDevicePath
)) == 0) {
324 // Got the option, so just return
326 gBS
->FreePool (OptionPtr
);
327 gBS
->FreePool (TempOptionPtr
);
331 // Boot device path changed, need update.
333 UpdateBootDevicePath
= TRUE
;
338 gBS
->FreePool (OptionPtr
);
341 OptionSize
= sizeof (UINT32
) + sizeof (UINT16
) + StrSize (String
);
342 OptionSize
+= GetDevicePathSize (DevicePath
);
343 OptionPtr
= AllocateZeroPool (OptionSize
);
345 *(UINT32
*) TempPtr
= LOAD_OPTION_ACTIVE
;
346 TempPtr
+= sizeof (UINT32
);
347 *(UINT16
*) TempPtr
= (UINT16
) GetDevicePathSize (DevicePath
);
348 TempPtr
+= sizeof (UINT16
);
349 CopyMem (TempPtr
, String
, StrSize (String
));
350 TempPtr
+= StrSize (String
);
351 CopyMem (TempPtr
, DevicePath
, GetDevicePathSize (DevicePath
));
353 if (UpdateBootDevicePath
) {
355 // The number in option#### to be updated
357 RegisterOptionNumber
= TempOptionPtr
[Index
];
360 // The new option#### number
362 RegisterOptionNumber
= BdsLibGetFreeOptionNumber(VariableName
);
365 if (*VariableName
== 'B') {
366 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", RegisterOptionNumber
);
368 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", RegisterOptionNumber
);
371 Status
= gRT
->SetVariable (
373 &gEfiGlobalVariableGuid
,
374 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
378 if (EFI_ERROR (Status
) || UpdateBootDevicePath
) {
379 gBS
->FreePool (OptionPtr
);
380 gBS
->FreePool (TempOptionPtr
);
384 gBS
->FreePool (OptionPtr
);
387 // Update the option order variable
393 if (TempOptionSize
== 0) {
395 Status
= gRT
->SetVariable (
397 &gEfiGlobalVariableGuid
,
398 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
402 if (EFI_ERROR (Status
)) {
403 gBS
->FreePool (TempOptionPtr
);
409 if (UpdateBootDevicePath
) {
411 // If just update a old option, the new optionorder size not change
413 OrderItemNum
= (TempOptionSize
/ sizeof (UINT16
)) ;
414 OptionOrderPtr
= AllocateZeroPool ( OrderItemNum
* sizeof (UINT16
));
415 CopyMem (OptionOrderPtr
, TempOptionPtr
, OrderItemNum
* sizeof (UINT16
));
417 OrderItemNum
= (TempOptionSize
/ sizeof (UINT16
)) + 1 ;
418 OptionOrderPtr
= AllocateZeroPool ( OrderItemNum
* sizeof (UINT16
));
419 CopyMem (OptionOrderPtr
, TempOptionPtr
, (OrderItemNum
- 1) * sizeof (UINT16
));
422 OptionOrderPtr
[Index
] = RegisterOptionNumber
;
424 Status
= gRT
->SetVariable (
426 &gEfiGlobalVariableGuid
,
427 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
428 OrderItemNum
* sizeof (UINT16
),
431 if (EFI_ERROR (Status
)) {
432 gBS
->FreePool (TempOptionPtr
);
433 gBS
->FreePool (OptionOrderPtr
);
437 gBS
->FreePool (TempOptionPtr
);
438 gBS
->FreePool (OptionOrderPtr
);
445 Build the boot#### or driver#### option from the VariableName, the
446 build boot#### or driver#### will also be linked to BdsCommonOptionList
448 @param BdsCommonOptionList The header of the boot#### or driver#### option
450 @param VariableName EFI Variable name indicate if it is boot#### or
453 @retval BDS_COMMON_OPTION Get the option just been created
454 @retval NULL Failed to get the new option
458 BdsLibVariableToOption (
459 IN OUT LIST_ENTRY
*BdsCommonOptionList
,
460 IN CHAR16
*VariableName
468 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
469 BDS_COMMON_OPTION
*Option
;
471 UINT32 LoadOptionsSize
;
475 // Read the variable. We will never free this data.
477 Variable
= BdsLibGetVariableAndSize (
479 &gEfiGlobalVariableGuid
,
482 if (Variable
== NULL
) {
486 // Notes: careful defined the variable of Boot#### or
487 // Driver####, consider use some macro to abstract the code
490 // Get the option attribute
493 Attribute
= *(UINT32
*) Variable
;
494 TempPtr
+= sizeof (UINT32
);
497 // Get the option's device path size
499 FilePathSize
= *(UINT16
*) TempPtr
;
500 TempPtr
+= sizeof (UINT16
);
503 // Get the option's description string
505 Description
= (CHAR16
*) TempPtr
;
508 // Get the option's description string size
510 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
513 // Get the option's device path
515 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
516 TempPtr
+= FilePathSize
;
518 LoadOptions
= TempPtr
;
519 LoadOptionsSize
= (UINT32
) (VariableSize
- (UINTN
) (TempPtr
- Variable
));
522 // The Console variables may have multiple device paths, so make
523 // an Entry for each one.
525 Option
= AllocateZeroPool (sizeof (BDS_COMMON_OPTION
));
526 if (Option
== NULL
) {
530 Option
->Signature
= BDS_LOAD_OPTION_SIGNATURE
;
531 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
532 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
533 Option
->Attribute
= Attribute
;
534 Option
->Description
= AllocateZeroPool (StrSize (Description
));
535 CopyMem (Option
->Description
, Description
, StrSize (Description
));
536 Option
->LoadOptions
= AllocateZeroPool (LoadOptionsSize
);
537 CopyMem (Option
->LoadOptions
, LoadOptions
, LoadOptionsSize
);
538 Option
->LoadOptionsSize
= LoadOptionsSize
;
541 // Get the value from VariableName Unicode string
542 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this
543 // Unicode stream to ASCII without any loss in meaning.
545 if (*VariableName
== 'B') {
546 NumOff
= sizeof (L
"Boot")/sizeof(CHAR16
) -1 ;
547 Option
->BootCurrent
= (UINT16
) ((VariableName
[NumOff
] -'0') * 0x1000);
548 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+1]-'0') * 0x100));
549 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+2]-'0') * 0x10));
550 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+3]-'0')));
553 // Insert active entry to BdsDeviceList
555 if ((Option
->Attribute
& LOAD_OPTION_ACTIVE
) == LOAD_OPTION_ACTIVE
) {
556 InsertTailList (BdsCommonOptionList
, &Option
->Link
);
557 gBS
->FreePool (Variable
);
561 gBS
->FreePool (Variable
);
562 gBS
->FreePool (Option
);
569 Process BootOrder, or DriverOrder variables, by calling
570 BdsLibVariableToOption () for each UINT16 in the variables.
572 @param BdsCommonOptionList The header of the option list base on variable
574 @param VariableName EFI Variable name indicate the BootOrder or
577 @retval EFI_SUCCESS Success create the boot option or driver option
579 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list
583 BdsLibBuildOptionFromVar (
584 IN LIST_ENTRY
*BdsCommonOptionList
,
585 IN CHAR16
*VariableName
589 UINTN OptionOrderSize
;
591 BDS_COMMON_OPTION
*Option
;
592 CHAR16 OptionName
[20];
595 // Zero Buffer in order to get all BOOT#### variables
597 ZeroMem (OptionName
, sizeof (OptionName
));
600 // Read the BootOrder, or DriverOrder variable.
602 OptionOrder
= BdsLibGetVariableAndSize (
604 &gEfiGlobalVariableGuid
,
607 if (OptionOrder
== NULL
) {
608 return EFI_OUT_OF_RESOURCES
;
611 for (Index
= 0; Index
< OptionOrderSize
/ sizeof (UINT16
); Index
++) {
612 if (*VariableName
== 'B') {
613 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionOrder
[Index
]);
615 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", OptionOrder
[Index
]);
618 Option
= BdsLibVariableToOption (BdsCommonOptionList
, OptionName
);
619 Option
->BootCurrent
= OptionOrder
[Index
];
623 gBS
->FreePool (OptionOrder
);
630 Get boot mode by looking up configuration table and parsing HOB list
632 @param BootMode Boot mode from PEI handoff HOB.
634 @retval EFI_SUCCESS Successfully get boot mode
635 @retval EFI_NOT_FOUND Can not find the current system boot mode
640 OUT EFI_BOOT_MODE
*BootMode
649 Status
= EfiGetSystemConfigurationTable (&gEfiHobListGuid
, &HobList
);
650 if (EFI_ERROR (Status
)) {
651 DEBUG ((DEBUG_ERROR
, "Hob list not found\n"));
653 return EFI_NOT_FOUND
;
656 Status
= R8_GetHobBootMode (HobList
, BootMode
);
657 if (EFI_ERROR (Status
)) {
658 return EFI_NOT_FOUND
;
666 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
667 buffer, and the size of the buffer. If failure return NULL.
669 @param Name String part of EFI variable name
670 @param VendorGuid GUID part of EFI variable name
671 @param VariableSize Returns the size of the EFI variable that was read
673 @return Dynamically allocated memory that contains a copy of the EFI variable.
674 @return Caller is responsible freeing the buffer.
675 @retval NULL Variable was not read
679 BdsLibGetVariableAndSize (
681 IN EFI_GUID
*VendorGuid
,
682 OUT UINTN
*VariableSize
692 // Pass in a zero size buffer to find the required buffer size.
695 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
696 if (Status
== EFI_BUFFER_TOO_SMALL
) {
698 // Allocate the buffer to return
700 Buffer
= AllocateZeroPool (BufferSize
);
701 if (Buffer
== NULL
) {
705 // Read variable into the allocated buffer.
707 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
708 if (EFI_ERROR (Status
)) {
713 *VariableSize
= BufferSize
;
719 Delete the instance in Multi which matches partly with Single instance
721 @param Multi A pointer to a multi-instance device path data
723 @param Single A pointer to a single-instance device path data
726 @return This function will remove the device path instances in Multi which partly
727 @return match with the Single, and return the result device path. If there is no
728 @return remaining device path as a result, this function will return NULL.
731 EFI_DEVICE_PATH_PROTOCOL
*
732 BdsLibDelPartMatchInstance (
733 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
734 IN EFI_DEVICE_PATH_PROTOCOL
*Single
737 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
738 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
739 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
744 NewDevicePath
= NULL
;
745 TempNewDevicePath
= NULL
;
747 if (Multi
== NULL
|| Single
== NULL
) {
751 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
752 SingleDpSize
= GetDevicePathSize (Single
) - END_DEVICE_PATH_LENGTH
;
753 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
755 while (Instance
!= NULL
) {
757 Size
= (SingleDpSize
< InstanceSize
) ? SingleDpSize
: InstanceSize
;
759 if ((CompareMem (Instance
, Single
, Size
) != 0)) {
761 // Append the device path instance which does not match with Single
763 TempNewDevicePath
= NewDevicePath
;
764 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, Instance
);
765 SafeFreePool(TempNewDevicePath
);
767 SafeFreePool(Instance
);
768 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
769 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
772 return NewDevicePath
;
777 Function compares a device path data structure to that of all the nodes of a
778 second device path instance.
780 @param Multi A pointer to a multi-instance device path data
782 @param Single A pointer to a single-instance device path data
785 @retval TRUE If the Single is contained within Multi
786 @retval FALSE The Single is not match within Multi
790 BdsLibMatchDevicePaths (
791 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
792 IN EFI_DEVICE_PATH_PROTOCOL
*Single
795 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
796 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
799 if (!Multi
|| !Single
) {
804 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
807 // Search for the match of 'Single' in 'Multi'
809 while (DevicePathInst
!= NULL
) {
811 // If the single device path is found in multiple device paths,
814 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
815 gBS
->FreePool (DevicePathInst
);
819 gBS
->FreePool (DevicePathInst
);
820 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
828 This function prints a series of strings.
830 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
831 @param ... A variable argument list containing series of
832 strings, the last string must be NULL.
834 @retval EFI_SUCCESS Success print out the string using ConOut.
835 @retval EFI_STATUS Return the status of the ConOut->OutputString ().
839 BdsLibOutputStrings (
840 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*ConOut
,
848 Status
= EFI_SUCCESS
;
849 VA_START (args
, ConOut
);
851 while (!EFI_ERROR (Status
)) {
853 // If String is NULL, then it's the end of the list
855 String
= VA_ARG (args
, CHAR16
*);
860 Status
= ConOut
->OutputString (ConOut
, String
);
862 if (EFI_ERROR (Status
)) {
871 // Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.
872 // Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if
873 // user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.
878 Enable the setup browser reset reminder feature.
879 This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.
887 EnableResetReminderFeature (
891 mFeaturerSwitch
= TRUE
;
896 Disable the setup browser reset reminder feature.
897 This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.
905 DisableResetReminderFeature (
909 mFeaturerSwitch
= FALSE
;
914 Record the info that a reset is required.
915 A module boolean variable is used to record whether a reset is required.
923 EnableResetRequired (
927 mResetRequired
= TRUE
;
932 Record the info that no reset is required.
933 A module boolean variable is used to record whether a reset is required.
941 DisableResetRequired (
945 mResetRequired
= FALSE
;
950 Check whether platform policy enable the reset reminder feature. The default is enabled.
958 IsResetReminderFeatureEnable (
962 return mFeaturerSwitch
;
967 Check if user changed any option setting which needs a system reset to be effective.
979 return mResetRequired
;
984 Check whether a reset is needed, and finish the reset reminder feature.
985 If a reset is needed, Popup a menu to notice user, and finish the feature
986 according to the user selection.
998 #if (EFI_SPECIFICATION_VERSION < 0x0002000A)
1000 EFI_FORM_BROWSER_PROTOCOL
*Browser
;
1003 CHAR16
*StringBuffer1
;
1004 CHAR16
*StringBuffer2
;
1008 //check any reset required change is applied? if yes, reset system
1010 if (IsResetReminderFeatureEnable ()) {
1011 if (IsResetRequired ()) {
1013 #if (EFI_SPECIFICATION_VERSION < 0x0002000A)
1014 Status
= gBS
->LocateProtocol (
1015 &gEfiFormBrowserProtocolGuid
,
1021 StringBuffer1
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
1022 ASSERT (StringBuffer1
!= NULL
);
1023 StringBuffer2
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
1024 ASSERT (StringBuffer2
!= NULL
);
1025 StrCpy (StringBuffer1
, L
"Configuration changed. Reset to apply it Now ? ");
1026 StrCpy (StringBuffer2
, L
"Enter (YES) / Esc (NO)");
1028 // Popup a menu to notice user
1031 #if (EFI_SPECIFICATION_VERSION < 0x0002000A)
1032 Browser
->CreatePopUp (2, TRUE
, 0, NULL
, &Key
, StringBuffer1
, StringBuffer2
);
1034 IfrLibCreatePopUp (2, &Key
, StringBuffer1
, StringBuffer2
);
1036 } while ((Key
.ScanCode
!= SCAN_ESC
) && (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
));
1038 gBS
->FreePool (StringBuffer1
);
1039 gBS
->FreePool (StringBuffer2
);
1041 // If the user hits the YES Response key, reset
1043 if ((Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
)) {
1044 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
1046 gST
->ConOut
->ClearScreen (gST
->ConOut
);
1053 Get the headers (dos, image, optional header) from an image
1055 @param Device SimpleFileSystem device handle
1056 @param FileName File name for the image
1057 @param DosHeader Pointer to dos header
1058 @param ImageHeader Pointer to image header
1059 @param OptionalHeader Pointer to optional header
1061 @retval EFI_SUCCESS Successfully get the machine type.
1062 @retval EFI_NOT_FOUND The file is not found.
1063 @retval EFI_LOAD_ERROR File is not a valid image file.
1067 BdsLibGetImageHeader (
1068 IN EFI_HANDLE Device
,
1069 IN CHAR16
*FileName
,
1070 OUT EFI_IMAGE_DOS_HEADER
*DosHeader
,
1071 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
1075 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
1076 EFI_FILE_HANDLE Root
;
1077 EFI_FILE_HANDLE ThisFile
;
1080 EFI_FILE_INFO
*Info
;
1085 // Handle the file system interface to the device
1087 Status
= gBS
->HandleProtocol (
1089 &gEfiSimpleFileSystemProtocolGuid
,
1092 if (EFI_ERROR (Status
)) {
1096 Status
= Volume
->OpenVolume (
1100 if (EFI_ERROR (Status
)) {
1104 Status
= Root
->Open (Root
, &ThisFile
, FileName
, EFI_FILE_MODE_READ
, 0);
1105 if (EFI_ERROR (Status
)) {
1112 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
1115 Status
= gBS
->AllocatePool (EfiBootServicesData
, BufferSize
, (VOID
**) &Info
);
1116 if (EFI_ERROR (Status
)) {
1119 Status
= ThisFile
->GetInfo (
1125 if (!EFI_ERROR (Status
)) {
1128 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1131 gBS
->FreePool (Info
);
1134 FileSize
= Info
->FileSize
;
1135 gBS
->FreePool (Info
);
1140 BufferSize
= sizeof (EFI_IMAGE_DOS_HEADER
);
1141 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, DosHeader
);
1142 if (EFI_ERROR (Status
) ||
1143 BufferSize
< sizeof (EFI_IMAGE_DOS_HEADER
) ||
1144 FileSize
<= DosHeader
->e_lfanew
||
1145 DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
1146 Status
= EFI_LOAD_ERROR
;
1151 // Move to PE signature
1153 Status
= ThisFile
->SetPosition (ThisFile
, DosHeader
->e_lfanew
);
1154 if (EFI_ERROR (Status
)) {
1155 Status
= EFI_LOAD_ERROR
;
1160 // Read and check PE signature
1162 BufferSize
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
1163 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, Hdr
.Pe32
);
1164 if (EFI_ERROR (Status
) ||
1165 BufferSize
< sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
) ||
1166 Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1167 Status
= EFI_LOAD_ERROR
;
1172 // Check PE32 or PE32+ magic
1174 if (Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
&&
1175 Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
1176 Status
= EFI_LOAD_ERROR
;
1181 if (ThisFile
!= NULL
) {
1182 ThisFile
->Close (ThisFile
);
1190 #if (EFI_SPECIFICATION_VERSION < 0x0002000A)
1192 BdsLibGetHiiHandles (
1193 IN EFI_HII_PROTOCOL
*Hii
,
1194 IN OUT UINT16
*HandleBufferLength
,
1195 OUT EFI_HII_HANDLE
**HiiHandleBuffer
1199 Routine Description:
1201 Determines the handles that are currently active in the database.
1202 It's the caller's responsibility to free handle buffer.
1206 This - A pointer to the EFI_HII_PROTOCOL instance.
1207 HandleBufferLength - On input, a pointer to the length of the handle buffer. On output,
1208 the length of the handle buffer that is required for the handles found.
1209 HiiHandleBuffer - Pointer to an array of EFI_HII_PROTOCOL instances returned.
1213 EFI_SUCCESS - Get an array of EFI_HII_PROTOCOL instances successfully.
1214 EFI_INVALID_PARAMETER - Hii is NULL.
1215 EFI_NOT_FOUND - Database not found.
1219 UINT16 TempBufferLength
;
1222 TempBufferLength
= 0;
1225 // Try to find the actual buffer size for HiiHandle Buffer.
1227 Status
= Hii
->FindHandles (Hii
, &TempBufferLength
, *HiiHandleBuffer
);
1229 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1230 *HiiHandleBuffer
= AllocateZeroPool (TempBufferLength
);
1231 Status
= Hii
->FindHandles (Hii
, &TempBufferLength
, *HiiHandleBuffer
);
1233 // we should not fail here.
1235 ASSERT_EFI_ERROR (Status
);
1238 *HandleBufferLength
= TempBufferLength
;
1247 BdsSetMemoryTypeInformationVariable (
1253 Routine Description:
1255 This routine is a notification function for legayc boot or exit boot
1256 service event. It will adjust the memory information for different
1257 memory type and save them into the variables for next boot
1261 Event - The event that triggered this notification function
1262 Context - Pointer to the notification functions context
1271 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
1272 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
1274 BOOLEAN UpdateRequired
;
1282 UpdateRequired
= FALSE
;
1285 // Retrieve the current memory usage statistics. If they are not found, then
1286 // no adjustments can be made to the Memory Type Information variable.
1288 Status
= EfiGetSystemConfigurationTable (
1289 &gEfiMemoryTypeInformationGuid
,
1290 (VOID
**) &CurrentMemoryTypeInformation
1292 if (EFI_ERROR (Status
)) {
1297 // Get the Memory Type Information settings from Hob if they exist,
1298 // PEI is responsible for getting them from variable and build a Hob to save them.
1299 // If the previous Memory Type Information is not available, then set defaults
1301 EfiGetSystemConfigurationTable (&gEfiHobListGuid
, &HobList
);
1302 Status
= R8_GetNextGuidHob (&HobList
, &gEfiMemoryTypeInformationGuid
, (VOID
**) &PreviousMemoryTypeInformation
, &VariableSize
);
1303 if (EFI_ERROR (Status
) || PreviousMemoryTypeInformation
== NULL
) {
1305 // If Platform has not built Memory Type Info into the Hob, just return.
1311 // Use a heuristic to adjust the Memory Type Information for the next boot
1313 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
1316 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
1317 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
1318 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
1323 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
1327 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
1330 // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail
1332 if (Current
> Previous
) {
1333 Next
= Current
+ (Current
>> 2);
1337 if (Next
> 0 && Next
< 4) {
1341 if (Next
!= Previous
) {
1342 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
1343 UpdateRequired
= TRUE
;
1349 // If any changes were made to the Memory Type Information settings, then set the new variable value
1351 if (UpdateRequired
) {
1352 Status
= gRT
->SetVariable (
1353 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
1354 &gEfiMemoryTypeInformationGuid
,
1355 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
1357 PreviousMemoryTypeInformation
1366 This routine register a function to adjust the different type memory page number just before booting
1367 and save the updated info into the variable for next boot to use
1376 BdsLibSaveMemoryTypeInformation (
1381 EFI_EVENT ReadyToBootEvent
;
1383 Status
= EfiCreateEventReadyToBootEx (
1385 BdsSetMemoryTypeInformationVariable
,
1389 if (EFI_ERROR (Status
)) {
1390 DEBUG ((DEBUG_ERROR
,"Bds Set Memory Type Informationa Variable Fails\n"));
1397 return the current TPL, copied from the EDKII glue lib.
1405 BdsLibGetCurrentTpl (
1411 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
1412 gBS
->RestoreTPL (Tpl
);