2 Misc BDS library function
4 Copyright (c) 2004 - 2008, Intel Corporation. <BR>
5 All rights reserved. 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
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.
15 #include "InternalBdsLib.h"
18 #define MAX_STRING_LEN 200
20 static BOOLEAN mFeaturerSwitch
= TRUE
;
21 static BOOLEAN mResetRequired
= FALSE
;
23 extern UINT16 gPlatformBootTimeOutDefault
;
27 Return the default value for system Timeout variable.
29 @return Timeout value.
43 // Return Timeout variable or 0xffff if no valid
44 // Timeout variable exists.
46 Size
= sizeof (UINT16
);
47 Status
= gRT
->GetVariable (L
"Timeout", &gEfiGlobalVariableGuid
, NULL
, &Size
, &Timeout
);
48 if (!EFI_ERROR (Status
)) {
52 // To make the current EFI Automatic-Test activity possible, just add
53 // following code to make AutoBoot enabled when this variable is not
55 // This code should be removed later.
57 Timeout
= PcdGet16 (PcdPlatformBootTimeOutDefault
);
60 // Notes: Platform should set default variable if non exists on all error cases!!!
62 Status
= gRT
->SetVariable (
64 &gEfiGlobalVariableGuid
,
65 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
74 The function will go through the driver optoin link list, load and start
75 every driver the driver optoin device path point to.
77 @param BdsDriverLists The header of the current driver option link list
83 IN LIST_ENTRY
*BdsDriverLists
88 BDS_COMMON_OPTION
*Option
;
89 EFI_HANDLE ImageHandle
;
90 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
98 // Process the driver option
100 for (Link
= BdsDriverLists
->ForwardLink
; Link
!= BdsDriverLists
; Link
= Link
->ForwardLink
) {
101 Option
= CR (Link
, BDS_COMMON_OPTION
, Link
, BDS_LOAD_OPTION_SIGNATURE
);
103 // If a load option is not marked as LOAD_OPTION_ACTIVE,
104 // the boot manager will not automatically load the option.
106 if (!IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_ACTIVE
)) {
110 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
111 // then all of the EFI drivers in the system will be disconnected and
112 // reconnected after the last driver load option is processed.
114 if (IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_FORCE_RECONNECT
)) {
118 // Make sure the driver path is connected.
120 BdsLibConnectDevicePath (Option
->DevicePath
);
123 // Load and start the image that Driver#### describes
125 Status
= gBS
->LoadImage (
134 if (!EFI_ERROR (Status
)) {
135 gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
138 // Verify whether this image is a driver, if not,
139 // exit it and continue to parse next load option
141 if (ImageInfo
->ImageCodeType
!= EfiBootServicesCode
&& ImageInfo
->ImageCodeType
!= EfiRuntimeServicesCode
) {
142 gBS
->Exit (ImageHandle
, EFI_INVALID_PARAMETER
, 0, NULL
);
146 if (Option
->LoadOptionsSize
!= 0) {
147 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
148 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
151 // Before calling the image, enable the Watchdog Timer for
152 // the 5 Minute period
154 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
156 Status
= gBS
->StartImage (ImageHandle
, &ExitDataSize
, &ExitData
);
157 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Driver Return Status = %r\n", Status
));
160 // Clear the Watchdog Timer after the image returns
162 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
166 // Process the LOAD_OPTION_FORCE_RECONNECT driver option
169 BdsLibDisconnectAllEfi ();
177 Get the Option Number that does not used
178 Try to locate the specific option variable one by one untile find a free number
180 @param VariableName Indicate if the boot#### or driver#### option
182 @return The Minimal Free Option Number
186 BdsLibGetFreeOptionNumber (
187 IN CHAR16
*VariableName
193 UINT16
*OptionBuffer
;
197 // Try to find the minimum free number from 0, 1, 2, 3....
201 if (*VariableName
== 'B') {
202 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Boot%04x", Index
);
204 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Driver%04x", Index
);
207 // try if the option number is used
209 OptionBuffer
= BdsLibGetVariableAndSize (
211 &gEfiGlobalVariableGuid
,
214 if (OptionBuffer
== NULL
) {
220 Number
= (UINT16
) Index
;
226 This function will register the new boot#### or driver#### option base on
227 the VariableName. The new registered boot#### or driver#### will be linked
228 to BdsOptionList and also update to the VariableName. After the boot#### or
229 driver#### updated, the BootOrder or DriverOrder will also be updated.
231 @param BdsOptionList The header of the boot#### or driver#### link list
232 @param DevicePath The device path which the boot#### or driver####
234 @param String The description of the boot#### or driver####
235 @param VariableName Indicate if the boot#### or driver#### option
237 @retval EFI_SUCCESS The boot#### or driver#### have been success
239 @retval EFI_STATUS Return the status of gRT->SetVariable ().
244 BdsLibRegisterNewOption (
245 IN LIST_ENTRY
*BdsOptionList
,
246 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
248 IN CHAR16
*VariableName
253 UINT16 RegisterOptionNumber
;
254 UINT16
*TempOptionPtr
;
255 UINTN TempOptionSize
;
256 UINT16
*OptionOrderPtr
;
260 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
262 CHAR16 OptionName
[10];
263 BOOLEAN UpdateDescription
;
264 UINT16 BootOrderEntry
;
271 OptionDevicePath
= NULL
;
273 OptionOrderPtr
= NULL
;
274 UpdateDescription
= FALSE
;
275 ZeroMem (OptionName
, sizeof (OptionName
));
278 TempOptionPtr
= BdsLibGetVariableAndSize (
280 &gEfiGlobalVariableGuid
,
285 // Compare with current option variable
287 for (Index
= 0; Index
< TempOptionSize
/ sizeof (UINT16
); Index
++) {
289 if (*VariableName
== 'B') {
290 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", TempOptionPtr
[Index
]);
292 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", TempOptionPtr
[Index
]);
295 OptionPtr
= BdsLibGetVariableAndSize (
297 &gEfiGlobalVariableGuid
,
300 if (OptionPtr
== NULL
) {
304 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
305 Description
= (CHAR16
*) TempPtr
;
306 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
307 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
310 // Notes: the description may will change base on the GetStringToken
312 if (CompareMem (OptionDevicePath
, DevicePath
, GetDevicePathSize (OptionDevicePath
)) == 0) {
313 if (CompareMem (Description
, String
, StrSize (Description
)) == 0) {
315 // Got the option, so just return
317 gBS
->FreePool (OptionPtr
);
318 gBS
->FreePool (TempOptionPtr
);
322 // Option description changed, need update.
324 UpdateDescription
= TRUE
;
325 gBS
->FreePool (OptionPtr
);
330 gBS
->FreePool (OptionPtr
);
333 OptionSize
= sizeof (UINT32
) + sizeof (UINT16
) + StrSize (String
);
334 OptionSize
+= GetDevicePathSize (DevicePath
);
335 OptionPtr
= AllocateZeroPool (OptionSize
);
337 *(UINT32
*) TempPtr
= LOAD_OPTION_ACTIVE
;
338 TempPtr
+= sizeof (UINT32
);
339 *(UINT16
*) TempPtr
= (UINT16
) GetDevicePathSize (DevicePath
);
340 TempPtr
+= sizeof (UINT16
);
341 CopyMem (TempPtr
, String
, StrSize (String
));
342 TempPtr
+= StrSize (String
);
343 CopyMem (TempPtr
, DevicePath
, GetDevicePathSize (DevicePath
));
345 if (UpdateDescription
) {
347 // The number in option#### to be updated
349 RegisterOptionNumber
= TempOptionPtr
[Index
];
352 // The new option#### number
354 RegisterOptionNumber
= BdsLibGetFreeOptionNumber(VariableName
);
357 if (*VariableName
== 'B') {
358 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", RegisterOptionNumber
);
360 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", RegisterOptionNumber
);
363 Status
= gRT
->SetVariable (
365 &gEfiGlobalVariableGuid
,
366 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
371 // Return if only need to update a changed description or fail to set option.
373 if (EFI_ERROR (Status
) || UpdateDescription
) {
374 gBS
->FreePool (OptionPtr
);
375 gBS
->FreePool (TempOptionPtr
);
379 gBS
->FreePool (OptionPtr
);
382 // Update the option order variable
386 // If no option order
388 if (TempOptionSize
== 0) {
390 Status
= gRT
->SetVariable (
392 &gEfiGlobalVariableGuid
,
393 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
397 if (EFI_ERROR (Status
)) {
398 gBS
->FreePool (TempOptionPtr
);
405 // Append the new option number to the original option order
407 OrderItemNum
= (TempOptionSize
/ sizeof (UINT16
)) + 1 ;
408 OptionOrderPtr
= AllocateZeroPool ( OrderItemNum
* sizeof (UINT16
));
409 CopyMem (OptionOrderPtr
, TempOptionPtr
, (OrderItemNum
- 1) * sizeof (UINT16
));
411 OptionOrderPtr
[Index
] = RegisterOptionNumber
;
413 Status
= gRT
->SetVariable (
415 &gEfiGlobalVariableGuid
,
416 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
417 OrderItemNum
* sizeof (UINT16
),
420 if (EFI_ERROR (Status
)) {
421 gBS
->FreePool (TempOptionPtr
);
422 gBS
->FreePool (OptionOrderPtr
);
426 gBS
->FreePool (TempOptionPtr
);
427 gBS
->FreePool (OptionOrderPtr
);
434 Build the boot#### or driver#### option from the VariableName, the
435 build boot#### or driver#### will also be linked to BdsCommonOptionList
437 @param BdsCommonOptionList The header of the boot#### or driver#### option
439 @param VariableName EFI Variable name indicate if it is boot#### or
442 @retval BDS_COMMON_OPTION Get the option just been created
443 @retval NULL Failed to get the new option
448 BdsLibVariableToOption (
449 IN OUT LIST_ENTRY
*BdsCommonOptionList
,
450 IN CHAR16
*VariableName
458 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
459 BDS_COMMON_OPTION
*Option
;
461 UINT32 LoadOptionsSize
;
465 // Read the variable. We will never free this data.
467 Variable
= BdsLibGetVariableAndSize (
469 &gEfiGlobalVariableGuid
,
472 if (Variable
== NULL
) {
476 // Notes: careful defined the variable of Boot#### or
477 // Driver####, consider use some macro to abstract the code
480 // Get the option attribute
483 Attribute
= *(UINT32
*) Variable
;
484 TempPtr
+= sizeof (UINT32
);
487 // Get the option's device path size
489 FilePathSize
= *(UINT16
*) TempPtr
;
490 TempPtr
+= sizeof (UINT16
);
493 // Get the option's description string
495 Description
= (CHAR16
*) TempPtr
;
498 // Get the option's description string size
500 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
503 // Get the option's device path
505 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
506 TempPtr
+= FilePathSize
;
508 LoadOptions
= TempPtr
;
509 LoadOptionsSize
= (UINT32
) (VariableSize
- (UINTN
) (TempPtr
- Variable
));
512 // The Console variables may have multiple device paths, so make
513 // an Entry for each one.
515 Option
= AllocateZeroPool (sizeof (BDS_COMMON_OPTION
));
516 if (Option
== NULL
) {
520 Option
->Signature
= BDS_LOAD_OPTION_SIGNATURE
;
521 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
522 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
523 Option
->Attribute
= Attribute
;
524 Option
->Description
= AllocateZeroPool (StrSize (Description
));
525 CopyMem (Option
->Description
, Description
, StrSize (Description
));
526 Option
->LoadOptions
= AllocateZeroPool (LoadOptionsSize
);
527 CopyMem (Option
->LoadOptions
, LoadOptions
, LoadOptionsSize
);
528 Option
->LoadOptionsSize
= LoadOptionsSize
;
531 // Get the value from VariableName Unicode string
532 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this
533 // Unicode stream to ASCII without any loss in meaning.
535 if (*VariableName
== 'B') {
536 NumOff
= sizeof (L
"Boot")/sizeof(CHAR16
) -1 ;
537 Option
->BootCurrent
= (UINT16
) ((VariableName
[NumOff
] -'0') * 0x1000);
538 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+1]-'0') * 0x100));
539 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+2]-'0') * 0x10));
540 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+3]-'0')));
543 // Insert active entry to BdsDeviceList
545 if ((Option
->Attribute
& LOAD_OPTION_ACTIVE
) == LOAD_OPTION_ACTIVE
) {
546 InsertTailList (BdsCommonOptionList
, &Option
->Link
);
547 gBS
->FreePool (Variable
);
551 gBS
->FreePool (Variable
);
552 gBS
->FreePool (Option
);
559 Process BootOrder, or DriverOrder variables, by calling
560 BdsLibVariableToOption () for each UINT16 in the variables.
562 @param BdsCommonOptionList The header of the option list base on variable
564 @param VariableName EFI Variable name indicate the BootOrder or
567 @retval EFI_SUCCESS Success create the boot option or driver option
569 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list
574 BdsLibBuildOptionFromVar (
575 IN LIST_ENTRY
*BdsCommonOptionList
,
576 IN CHAR16
*VariableName
580 UINTN OptionOrderSize
;
582 BDS_COMMON_OPTION
*Option
;
583 CHAR16 OptionName
[20];
586 // Zero Buffer in order to get all BOOT#### variables
588 ZeroMem (OptionName
, sizeof (OptionName
));
591 // Read the BootOrder, or DriverOrder variable.
593 OptionOrder
= BdsLibGetVariableAndSize (
595 &gEfiGlobalVariableGuid
,
598 if (OptionOrder
== NULL
) {
599 return EFI_OUT_OF_RESOURCES
;
602 for (Index
= 0; Index
< OptionOrderSize
/ sizeof (UINT16
); Index
++) {
603 if (*VariableName
== 'B') {
604 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionOrder
[Index
]);
606 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", OptionOrder
[Index
]);
609 Option
= BdsLibVariableToOption (BdsCommonOptionList
, OptionName
);
610 Option
->BootCurrent
= OptionOrder
[Index
];
614 gBS
->FreePool (OptionOrder
);
621 Get boot mode by looking up configuration table and parsing HOB list
623 @param BootMode Boot mode from PEI handoff HOB.
625 @retval EFI_SUCCESS Successfully get boot mode
631 OUT EFI_BOOT_MODE
*BootMode
634 *BootMode
= GetBootModeHob ();
641 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
642 buffer, and the size of the buffer. If failure return NULL.
644 @param Name String part of EFI variable name
645 @param VendorGuid GUID part of EFI variable name
646 @param VariableSize Returns the size of the EFI variable that was read
648 @return Dynamically allocated memory that contains a copy of the EFI variable.
649 @return Caller is responsible freeing the buffer.
650 @retval NULL Variable was not read
655 BdsLibGetVariableAndSize (
657 IN EFI_GUID
*VendorGuid
,
658 OUT UINTN
*VariableSize
668 // Pass in a zero size buffer to find the required buffer size.
671 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
672 if (Status
== EFI_BUFFER_TOO_SMALL
) {
674 // Allocate the buffer to return
676 Buffer
= AllocateZeroPool (BufferSize
);
677 if (Buffer
== NULL
) {
681 // Read variable into the allocated buffer.
683 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
684 if (EFI_ERROR (Status
)) {
689 *VariableSize
= BufferSize
;
695 Delete the instance in Multi which matches partly with Single instance
697 @param Multi A pointer to a multi-instance device path data
699 @param Single A pointer to a single-instance device path data
702 @return This function will remove the device path instances in Multi which partly
703 match with the Single, and return the result device path. If there is no
704 remaining device path as a result, this function will return NULL.
707 EFI_DEVICE_PATH_PROTOCOL
*
709 BdsLibDelPartMatchInstance (
710 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
711 IN EFI_DEVICE_PATH_PROTOCOL
*Single
714 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
715 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
716 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
721 NewDevicePath
= NULL
;
722 TempNewDevicePath
= NULL
;
724 if (Multi
== NULL
|| Single
== NULL
) {
728 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
729 SingleDpSize
= GetDevicePathSize (Single
) - END_DEVICE_PATH_LENGTH
;
730 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
732 while (Instance
!= NULL
) {
734 Size
= (SingleDpSize
< InstanceSize
) ? SingleDpSize
: InstanceSize
;
736 if ((CompareMem (Instance
, Single
, Size
) != 0)) {
738 // Append the device path instance which does not match with Single
740 TempNewDevicePath
= NewDevicePath
;
741 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, Instance
);
742 SafeFreePool(TempNewDevicePath
);
744 SafeFreePool(Instance
);
745 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
746 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
749 return NewDevicePath
;
754 Function compares a device path data structure to that of all the nodes of a
755 second device path instance.
757 @param Multi A pointer to a multi-instance device path data
759 @param Single A pointer to a single-instance device path data
762 @retval TRUE If the Single is contained within Multi
763 @retval FALSE The Single is not match within Multi
768 BdsLibMatchDevicePaths (
769 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
770 IN EFI_DEVICE_PATH_PROTOCOL
*Single
773 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
774 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
777 if (!Multi
|| !Single
) {
782 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
785 // Search for the match of 'Single' in 'Multi'
787 while (DevicePathInst
!= NULL
) {
789 // If the single device path is found in multiple device paths,
792 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
793 gBS
->FreePool (DevicePathInst
);
797 gBS
->FreePool (DevicePathInst
);
798 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
806 This function prints a series of strings.
808 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
809 @param ... A variable argument list containing series of
810 strings, the last string must be NULL.
812 @retval EFI_SUCCESS Success print out the string using ConOut.
813 @retval EFI_STATUS Return the status of the ConOut->OutputString ().
818 BdsLibOutputStrings (
819 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*ConOut
,
827 Status
= EFI_SUCCESS
;
828 VA_START (args
, ConOut
);
830 while (!EFI_ERROR (Status
)) {
832 // If String is NULL, then it's the end of the list
834 String
= VA_ARG (args
, CHAR16
*);
839 Status
= ConOut
->OutputString (ConOut
, String
);
841 if (EFI_ERROR (Status
)) {
850 // Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.
851 // Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if
852 // user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.
857 Enable the setup browser reset reminder feature.
858 This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.
863 EnableResetReminderFeature (
867 mFeaturerSwitch
= TRUE
;
872 Disable the setup browser reset reminder feature.
873 This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.
878 DisableResetReminderFeature (
882 mFeaturerSwitch
= FALSE
;
887 Record the info that a reset is required.
888 A module boolean variable is used to record whether a reset is required.
893 EnableResetRequired (
897 mResetRequired
= TRUE
;
902 Record the info that no reset is required.
903 A module boolean variable is used to record whether a reset is required.
908 DisableResetRequired (
912 mResetRequired
= FALSE
;
917 Check whether platform policy enable the reset reminder feature. The default is enabled.
922 IsResetReminderFeatureEnable (
926 return mFeaturerSwitch
;
931 Check if user changed any option setting which needs a system reset to be effective.
940 return mResetRequired
;
945 Check whether a reset is needed, and finish the reset reminder feature.
946 If a reset is needed, Popup a menu to notice user, and finish the feature
947 according to the user selection.
957 CHAR16
*StringBuffer1
;
958 CHAR16
*StringBuffer2
;
962 //check any reset required change is applied? if yes, reset system
964 if (IsResetReminderFeatureEnable ()) {
965 if (IsResetRequired ()) {
967 StringBuffer1
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
968 ASSERT (StringBuffer1
!= NULL
);
969 StringBuffer2
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
970 ASSERT (StringBuffer2
!= NULL
);
971 StrCpy (StringBuffer1
, L
"Configuration changed. Reset to apply it Now ? ");
972 StrCpy (StringBuffer2
, L
"Enter (YES) / Esc (NO)");
974 // Popup a menu to notice user
977 IfrLibCreatePopUp (2, &Key
, StringBuffer1
, StringBuffer2
);
978 } while ((Key
.ScanCode
!= SCAN_ESC
) && (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
));
980 gBS
->FreePool (StringBuffer1
);
981 gBS
->FreePool (StringBuffer2
);
983 // If the user hits the YES Response key, reset
985 if ((Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
)) {
986 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
988 gST
->ConOut
->ClearScreen (gST
->ConOut
);
995 Get the headers (dos, image, optional header) from an image
997 @param Device SimpleFileSystem device handle
998 @param FileName File name for the image
999 @param DosHeader Pointer to dos header
1000 @param ImageHeader Pointer to image header
1001 @param OptionalHeader Pointer to optional header
1003 @retval EFI_SUCCESS Successfully get the machine type.
1004 @retval EFI_NOT_FOUND The file is not found.
1005 @retval EFI_LOAD_ERROR File is not a valid image file.
1010 BdsLibGetImageHeader (
1011 IN EFI_HANDLE Device
,
1012 IN CHAR16
*FileName
,
1013 OUT EFI_IMAGE_DOS_HEADER
*DosHeader
,
1014 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
1018 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
1019 EFI_FILE_HANDLE Root
;
1020 EFI_FILE_HANDLE ThisFile
;
1023 EFI_FILE_INFO
*Info
;
1028 // Handle the file system interface to the device
1030 Status
= gBS
->HandleProtocol (
1032 &gEfiSimpleFileSystemProtocolGuid
,
1035 if (EFI_ERROR (Status
)) {
1039 Status
= Volume
->OpenVolume (
1043 if (EFI_ERROR (Status
)) {
1047 Status
= Root
->Open (Root
, &ThisFile
, FileName
, EFI_FILE_MODE_READ
, 0);
1048 if (EFI_ERROR (Status
)) {
1055 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
1058 Status
= gBS
->AllocatePool (EfiBootServicesData
, BufferSize
, (VOID
**) &Info
);
1059 if (EFI_ERROR (Status
)) {
1062 Status
= ThisFile
->GetInfo (
1068 if (!EFI_ERROR (Status
)) {
1071 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1074 gBS
->FreePool (Info
);
1077 FileSize
= Info
->FileSize
;
1078 gBS
->FreePool (Info
);
1083 BufferSize
= sizeof (EFI_IMAGE_DOS_HEADER
);
1084 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, DosHeader
);
1085 if (EFI_ERROR (Status
) ||
1086 BufferSize
< sizeof (EFI_IMAGE_DOS_HEADER
) ||
1087 FileSize
<= DosHeader
->e_lfanew
||
1088 DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
1089 Status
= EFI_LOAD_ERROR
;
1094 // Move to PE signature
1096 Status
= ThisFile
->SetPosition (ThisFile
, DosHeader
->e_lfanew
);
1097 if (EFI_ERROR (Status
)) {
1098 Status
= EFI_LOAD_ERROR
;
1103 // Read and check PE signature
1105 BufferSize
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
1106 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, Hdr
.Pe32
);
1107 if (EFI_ERROR (Status
) ||
1108 BufferSize
< sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
) ||
1109 Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1110 Status
= EFI_LOAD_ERROR
;
1115 // Check PE32 or PE32+ magic
1117 if (Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
&&
1118 Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
1119 Status
= EFI_LOAD_ERROR
;
1124 if (ThisFile
!= NULL
) {
1125 ThisFile
->Close (ThisFile
);
1135 BdsSetMemoryTypeInformationVariable (
1141 Routine Description:
1143 This routine is a notification function for legayc boot or exit boot
1144 service event. It will adjust the memory information for different
1145 memory type and save them into the variables for next boot
1149 Event - The event that triggered this notification function
1150 Context - Pointer to the notification functions context
1159 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
1160 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
1162 BOOLEAN UpdateRequired
;
1168 EFI_HOB_GUID_TYPE
*GuidHob
;
1170 UpdateRequired
= FALSE
;
1173 // Retrieve the current memory usage statistics. If they are not found, then
1174 // no adjustments can be made to the Memory Type Information variable.
1176 Status
= EfiGetSystemConfigurationTable (
1177 &gEfiMemoryTypeInformationGuid
,
1178 (VOID
**) &CurrentMemoryTypeInformation
1180 if (EFI_ERROR (Status
)) {
1185 // Get the Memory Type Information settings from Hob if they exist,
1186 // PEI is responsible for getting them from variable and build a Hob to save them.
1187 // If the previous Memory Type Information is not available, then set defaults
1189 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
1190 if (GuidHob
== NULL
) {
1192 // If Platform has not built Memory Type Info into the Hob, just return.
1196 PreviousMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
1197 VariableSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
1200 // Use a heuristic to adjust the Memory Type Information for the next boot
1202 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
1205 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
1206 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
1207 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
1212 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
1216 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
1219 // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail
1221 if (Current
> Previous
) {
1222 Next
= Current
+ (Current
>> 2);
1226 if (Next
> 0 && Next
< 4) {
1230 if (Next
!= Previous
) {
1231 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
1232 UpdateRequired
= TRUE
;
1238 // If any changes were made to the Memory Type Information settings, then set the new variable value
1240 if (UpdateRequired
) {
1241 Status
= gRT
->SetVariable (
1242 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
1243 &gEfiMemoryTypeInformationGuid
,
1244 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
1246 PreviousMemoryTypeInformation
1255 This routine register a function to adjust the different type memory page number just before booting
1256 and save the updated info into the variable for next boot to use
1261 BdsLibSaveMemoryTypeInformation (
1266 EFI_EVENT ReadyToBootEvent
;
1268 Status
= EfiCreateEventReadyToBootEx (
1270 BdsSetMemoryTypeInformationVariable
,
1274 if (EFI_ERROR (Status
)) {
1275 DEBUG ((DEBUG_ERROR
,"Bds Set Memory Type Informationa Variable Fails\n"));