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 BOOLEAN mFeaturerSwitch
= TRUE
;
21 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
)) {
50 // According to UEFI 2.0 spec, it should treat the Timeout value as 0xffff
51 // (default value PcdPlatformBootTimeOutDefault) when L"Timeout" variable is not present.
52 // To make the current EFI Automatic-Test activity possible, platform can choose other value
53 // for automatic boot when the variable is not present.
55 Timeout
= PcdGet16 (PcdPlatformBootTimeOutDefault
);
62 The function will go through the driver optoin link list, load and start
63 every driver the driver optoin device path point to.
65 @param BdsDriverLists The header of the current driver option link list
71 IN LIST_ENTRY
*BdsDriverLists
76 BDS_COMMON_OPTION
*Option
;
77 EFI_HANDLE ImageHandle
;
78 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
86 // Process the driver option
88 for (Link
= BdsDriverLists
->ForwardLink
; Link
!= BdsDriverLists
; Link
= Link
->ForwardLink
) {
89 Option
= CR (Link
, BDS_COMMON_OPTION
, Link
, BDS_LOAD_OPTION_SIGNATURE
);
92 // If a load option is not marked as LOAD_OPTION_ACTIVE,
93 // the boot manager will not automatically load the option.
95 if (!IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_ACTIVE
)) {
100 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
101 // then all of the EFI drivers in the system will be disconnected and
102 // reconnected after the last driver load option is processed.
104 if (IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_FORCE_RECONNECT
)) {
109 // Make sure the driver path is connected.
111 BdsLibConnectDevicePath (Option
->DevicePath
);
114 // Load and start the image that Driver#### describes
116 Status
= gBS
->LoadImage (
125 if (!EFI_ERROR (Status
)) {
126 gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
129 // Verify whether this image is a driver, if not,
130 // exit it and continue to parse next load option
132 if (ImageInfo
->ImageCodeType
!= EfiBootServicesCode
&& ImageInfo
->ImageCodeType
!= EfiRuntimeServicesCode
) {
133 gBS
->Exit (ImageHandle
, EFI_INVALID_PARAMETER
, 0, NULL
);
137 if (Option
->LoadOptionsSize
!= 0) {
138 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
139 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
142 // Before calling the image, enable the Watchdog Timer for
143 // the 5 Minute period
145 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
147 Status
= gBS
->StartImage (ImageHandle
, &ExitDataSize
, &ExitData
);
148 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Driver Return Status = %r\n", Status
));
151 // Clear the Watchdog Timer after the image returns
153 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
158 // Process the LOAD_OPTION_FORCE_RECONNECT driver option
161 BdsLibDisconnectAllEfi ();
168 Get the Option Number that does not used.
169 Try to locate the specific option variable one by one untile find a free number.
171 @param VariableName Indicate if the boot#### or driver#### option
173 @return The Minimal Free Option Number
177 BdsLibGetFreeOptionNumber (
178 IN CHAR16
*VariableName
183 UINT16
*OptionBuffer
;
187 // Try to find the minimum free number from 0, 1, 2, 3....
191 if (*VariableName
== 'B') {
192 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Boot%04x", Index
);
194 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Driver%04x", Index
);
197 // try if the option number is used
199 OptionBuffer
= BdsLibGetVariableAndSize (
201 &gEfiGlobalVariableGuid
,
204 if (OptionBuffer
== NULL
) {
210 return ((UINT16
) Index
);
215 This function will register the new boot#### or driver#### option base on
216 the VariableName. The new registered boot#### or driver#### will be linked
217 to BdsOptionList and also update to the VariableName. After the boot#### or
218 driver#### updated, the BootOrder or DriverOrder will also be updated.
220 @param BdsOptionList The header of the boot#### or driver#### link list
221 @param DevicePath The device path which the boot#### or driver####
223 @param String The description of the boot#### or driver####
224 @param VariableName Indicate if the boot#### or driver#### option
226 @retval EFI_SUCCESS The boot#### or driver#### have been success
228 @retval EFI_STATUS Return the status of gRT->SetVariable ().
233 BdsLibRegisterNewOption (
234 IN LIST_ENTRY
*BdsOptionList
,
235 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
237 IN CHAR16
*VariableName
242 UINT16 RegisterOptionNumber
;
243 UINT16
*TempOptionPtr
;
244 UINTN TempOptionSize
;
245 UINT16
*OptionOrderPtr
;
249 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
251 CHAR16 OptionName
[10];
252 BOOLEAN UpdateDescription
;
253 UINT16 BootOrderEntry
;
260 OptionDevicePath
= NULL
;
262 OptionOrderPtr
= NULL
;
263 UpdateDescription
= FALSE
;
264 Status
= EFI_SUCCESS
;
265 ZeroMem (OptionName
, sizeof (OptionName
));
268 TempOptionPtr
= BdsLibGetVariableAndSize (
270 &gEfiGlobalVariableGuid
,
275 // Compare with current option variable
277 for (Index
= 0; Index
< TempOptionSize
/ sizeof (UINT16
); Index
++) {
279 if (*VariableName
== 'B') {
280 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", TempOptionPtr
[Index
]);
282 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", TempOptionPtr
[Index
]);
285 OptionPtr
= BdsLibGetVariableAndSize (
287 &gEfiGlobalVariableGuid
,
290 if (OptionPtr
== NULL
) {
294 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
295 Description
= (CHAR16
*) TempPtr
;
296 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
297 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
300 // Notes: the description may will change base on the GetStringToken
302 if (CompareMem (OptionDevicePath
, DevicePath
, GetDevicePathSize (OptionDevicePath
)) == 0) {
303 if (CompareMem (Description
, String
, StrSize (Description
)) == 0) {
305 // Got the option, so just return
307 FreePool (OptionPtr
);
308 FreePool (TempOptionPtr
);
312 // Option description changed, need update.
314 UpdateDescription
= TRUE
;
315 FreePool (OptionPtr
);
320 FreePool (OptionPtr
);
323 OptionSize
= sizeof (UINT32
) + sizeof (UINT16
) + StrSize (String
);
324 OptionSize
+= GetDevicePathSize (DevicePath
);
325 OptionPtr
= AllocateZeroPool (OptionSize
);
326 ASSERT (OptionPtr
!= NULL
);
329 *(UINT32
*) TempPtr
= LOAD_OPTION_ACTIVE
;
330 TempPtr
+= sizeof (UINT32
);
331 *(UINT16
*) TempPtr
= (UINT16
) GetDevicePathSize (DevicePath
);
332 TempPtr
+= sizeof (UINT16
);
333 CopyMem (TempPtr
, String
, StrSize (String
));
334 TempPtr
+= StrSize (String
);
335 CopyMem (TempPtr
, DevicePath
, GetDevicePathSize (DevicePath
));
337 if (UpdateDescription
) {
339 // The number in option#### to be updated
341 RegisterOptionNumber
= TempOptionPtr
[Index
];
344 // The new option#### number
346 RegisterOptionNumber
= BdsLibGetFreeOptionNumber(VariableName
);
349 if (*VariableName
== 'B') {
350 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", RegisterOptionNumber
);
352 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", RegisterOptionNumber
);
355 Status
= gRT
->SetVariable (
357 &gEfiGlobalVariableGuid
,
358 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
363 // Return if only need to update a changed description or fail to set option.
365 if (EFI_ERROR (Status
) || UpdateDescription
) {
366 FreePool (OptionPtr
);
367 if (TempOptionPtr
!= NULL
) {
368 FreePool (TempOptionPtr
);
373 FreePool (OptionPtr
);
376 // Update the option order variable
380 // If no option order
382 if (TempOptionSize
== 0) {
384 Status
= gRT
->SetVariable (
386 &gEfiGlobalVariableGuid
,
387 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
391 if (TempOptionPtr
!= NULL
) {
392 FreePool (TempOptionPtr
);
398 // Append the new option number to the original option order
400 OrderItemNum
= (TempOptionSize
/ sizeof (UINT16
)) + 1 ;
401 OptionOrderPtr
= AllocateZeroPool ( OrderItemNum
* sizeof (UINT16
));
402 ASSERT (OptionOrderPtr
!= NULL
);
404 CopyMem (OptionOrderPtr
, TempOptionPtr
, (OrderItemNum
- 1) * sizeof (UINT16
));
406 OptionOrderPtr
[Index
] = RegisterOptionNumber
;
408 Status
= gRT
->SetVariable (
410 &gEfiGlobalVariableGuid
,
411 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
412 OrderItemNum
* sizeof (UINT16
),
415 FreePool (TempOptionPtr
);
416 FreePool (OptionOrderPtr
);
423 Build the boot#### or driver#### option from the VariableName, the
424 build boot#### or driver#### will also be linked to BdsCommonOptionList.
426 @param BdsCommonOptionList The header of the boot#### or driver#### option
428 @param VariableName EFI Variable name indicate if it is boot#### or
431 @retval BDS_COMMON_OPTION Get the option just been created
432 @retval NULL Failed to get the new option
437 BdsLibVariableToOption (
438 IN OUT LIST_ENTRY
*BdsCommonOptionList
,
439 IN CHAR16
*VariableName
447 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
448 BDS_COMMON_OPTION
*Option
;
450 UINT32 LoadOptionsSize
;
454 // Read the variable. We will never free this data.
456 Variable
= BdsLibGetVariableAndSize (
458 &gEfiGlobalVariableGuid
,
461 if (Variable
== NULL
) {
465 // Notes: careful defined the variable of Boot#### or
466 // Driver####, consider use some macro to abstract the code
469 // Get the option attribute
472 Attribute
= *(UINT32
*) Variable
;
473 TempPtr
+= sizeof (UINT32
);
476 // Get the option's device path size
478 FilePathSize
= *(UINT16
*) TempPtr
;
479 TempPtr
+= sizeof (UINT16
);
482 // Get the option's description string
484 Description
= (CHAR16
*) TempPtr
;
487 // Get the option's description string size
489 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
492 // Get the option's device path
494 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
495 TempPtr
+= FilePathSize
;
497 LoadOptions
= TempPtr
;
498 LoadOptionsSize
= (UINT32
) (VariableSize
- (UINTN
) (TempPtr
- Variable
));
501 // The Console variables may have multiple device paths, so make
502 // an Entry for each one.
504 Option
= AllocateZeroPool (sizeof (BDS_COMMON_OPTION
));
505 if (Option
== NULL
) {
509 Option
->Signature
= BDS_LOAD_OPTION_SIGNATURE
;
510 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
511 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
512 Option
->Attribute
= Attribute
;
513 Option
->Description
= AllocateZeroPool (StrSize (Description
));
514 CopyMem (Option
->Description
, Description
, StrSize (Description
));
515 Option
->LoadOptions
= AllocateZeroPool (LoadOptionsSize
);
516 CopyMem (Option
->LoadOptions
, LoadOptions
, LoadOptionsSize
);
517 Option
->LoadOptionsSize
= LoadOptionsSize
;
520 // Get the value from VariableName Unicode string
521 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this
522 // Unicode stream to ASCII without any loss in meaning.
524 if (*VariableName
== 'B') {
525 NumOff
= sizeof (L
"Boot")/sizeof(CHAR16
) -1 ;
526 Option
->BootCurrent
= (UINT16
) ((VariableName
[NumOff
] -'0') * 0x1000);
527 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+1]-'0') * 0x100));
528 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+2]-'0') * 0x10));
529 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+3]-'0')));
532 // Insert active entry to BdsDeviceList
534 if ((Option
->Attribute
& LOAD_OPTION_ACTIVE
) == LOAD_OPTION_ACTIVE
) {
535 InsertTailList (BdsCommonOptionList
, &Option
->Link
);
547 Process BootOrder, or DriverOrder variables, by calling
548 BdsLibVariableToOption () for each UINT16 in the variables.
550 @param BdsCommonOptionList The header of the option list base on variable
552 @param VariableName EFI Variable name indicate the BootOrder or
555 @retval EFI_SUCCESS Success create the boot option or driver option
557 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list
562 BdsLibBuildOptionFromVar (
563 IN LIST_ENTRY
*BdsCommonOptionList
,
564 IN CHAR16
*VariableName
568 UINTN OptionOrderSize
;
570 BDS_COMMON_OPTION
*Option
;
571 CHAR16 OptionName
[20];
574 // Zero Buffer in order to get all BOOT#### variables
576 ZeroMem (OptionName
, sizeof (OptionName
));
579 // Read the BootOrder, or DriverOrder variable.
581 OptionOrder
= BdsLibGetVariableAndSize (
583 &gEfiGlobalVariableGuid
,
586 if (OptionOrder
== NULL
) {
587 return EFI_OUT_OF_RESOURCES
;
590 for (Index
= 0; Index
< OptionOrderSize
/ sizeof (UINT16
); Index
++) {
591 if (*VariableName
== 'B') {
592 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionOrder
[Index
]);
594 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", OptionOrder
[Index
]);
597 Option
= BdsLibVariableToOption (BdsCommonOptionList
, OptionName
);
598 Option
->BootCurrent
= OptionOrder
[Index
];
602 FreePool (OptionOrder
);
608 Get boot mode by looking up configuration table and parsing HOB list
610 @param BootMode Boot mode from PEI handoff HOB.
612 @retval EFI_SUCCESS Successfully get boot mode
618 OUT EFI_BOOT_MODE
*BootMode
621 *BootMode
= GetBootModeHob ();
627 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
628 buffer, and the size of the buffer. If failure return NULL.
630 @param Name String part of EFI variable name
631 @param VendorGuid GUID part of EFI variable name
632 @param VariableSize Returns the size of the EFI variable that was read
634 @return Dynamically allocated memory that contains a copy of the EFI variable.
635 @return Caller is responsible freeing the buffer.
636 @retval NULL Variable was not read
641 BdsLibGetVariableAndSize (
643 IN EFI_GUID
*VendorGuid
,
644 OUT UINTN
*VariableSize
654 // Pass in a zero size buffer to find the required buffer size.
657 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
658 if (Status
== EFI_BUFFER_TOO_SMALL
) {
660 // Allocate the buffer to return
662 Buffer
= AllocateZeroPool (BufferSize
);
663 if (Buffer
== NULL
) {
667 // Read variable into the allocated buffer.
669 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
670 if (EFI_ERROR (Status
)) {
675 *VariableSize
= BufferSize
;
680 Delete the instance in Multi which matches partly with Single instance
682 @param Multi A pointer to a multi-instance device path data
684 @param Single A pointer to a single-instance device path data
687 @return This function will remove the device path instances in Multi which partly
688 match with the Single, and return the result device path. If there is no
689 remaining device path as a result, this function will return NULL.
692 EFI_DEVICE_PATH_PROTOCOL
*
694 BdsLibDelPartMatchInstance (
695 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
696 IN EFI_DEVICE_PATH_PROTOCOL
*Single
699 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
700 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
701 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
706 NewDevicePath
= NULL
;
707 TempNewDevicePath
= NULL
;
709 if (Multi
== NULL
|| Single
== NULL
) {
713 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
714 SingleDpSize
= GetDevicePathSize (Single
) - END_DEVICE_PATH_LENGTH
;
715 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
717 while (Instance
!= NULL
) {
719 Size
= (SingleDpSize
< InstanceSize
) ? SingleDpSize
: InstanceSize
;
721 if ((CompareMem (Instance
, Single
, Size
) != 0)) {
723 // Append the device path instance which does not match with Single
725 TempNewDevicePath
= NewDevicePath
;
726 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, Instance
);
727 if (TempNewDevicePath
!= NULL
) {
728 FreePool(TempNewDevicePath
);
732 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
733 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
736 return NewDevicePath
;
740 Function compares a device path data structure to that of all the nodes of a
741 second device path instance.
743 @param Multi A pointer to a multi-instance device path data
745 @param Single A pointer to a single-instance device path data
748 @retval TRUE If the Single is contained within Multi
749 @retval FALSE The Single is not match within Multi
754 BdsLibMatchDevicePaths (
755 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
756 IN EFI_DEVICE_PATH_PROTOCOL
*Single
759 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
760 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
763 if (Multi
!= NULL
|| Single
!= NULL
) {
768 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
771 // Search for the match of 'Single' in 'Multi'
773 while (DevicePathInst
!= NULL
) {
775 // If the single device path is found in multiple device paths,
778 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
779 FreePool (DevicePathInst
);
783 FreePool (DevicePathInst
);
784 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
791 This function prints a series of strings.
793 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
794 @param ... A variable argument list containing series of
795 strings, the last string must be NULL.
797 @retval EFI_SUCCESS Success print out the string using ConOut.
798 @retval EFI_STATUS Return the status of the ConOut->OutputString ().
803 BdsLibOutputStrings (
804 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*ConOut
,
812 Status
= EFI_SUCCESS
;
813 VA_START (Args
, ConOut
);
815 while (!EFI_ERROR (Status
)) {
817 // If String is NULL, then it's the end of the list
819 String
= VA_ARG (Args
, CHAR16
*);
820 if (String
!= NULL
) {
824 Status
= ConOut
->OutputString (ConOut
, String
);
826 if (EFI_ERROR (Status
)) {
835 // Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.
836 // Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if
837 // user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.
842 Enable the setup browser reset reminder feature.
843 This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.
848 EnableResetReminderFeature (
852 mFeaturerSwitch
= TRUE
;
857 Disable the setup browser reset reminder feature.
858 This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.
863 DisableResetReminderFeature (
867 mFeaturerSwitch
= FALSE
;
872 Record the info that a reset is required.
873 A module boolean variable is used to record whether a reset is required.
878 EnableResetRequired (
882 mResetRequired
= TRUE
;
887 Record the info that no reset is required.
888 A module boolean variable is used to record whether a reset is required.
893 DisableResetRequired (
897 mResetRequired
= FALSE
;
902 Check whether platform policy enable the reset reminder feature. The default is enabled.
907 IsResetReminderFeatureEnable (
911 return mFeaturerSwitch
;
916 Check if user changed any option setting which needs a system reset to be effective.
925 return mResetRequired
;
930 Check whether a reset is needed, and finish the reset reminder feature.
931 If a reset is needed, Popup a menu to notice user, and finish the feature
932 according to the user selection.
942 CHAR16
*StringBuffer1
;
943 CHAR16
*StringBuffer2
;
947 //check any reset required change is applied? if yes, reset system
949 if (IsResetReminderFeatureEnable ()) {
950 if (IsResetRequired ()) {
952 StringBuffer1
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
953 ASSERT (StringBuffer1
!= NULL
);
954 StringBuffer2
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
955 ASSERT (StringBuffer2
!= NULL
);
956 StrCpy (StringBuffer1
, L
"Configuration changed. Reset to apply it Now ? ");
957 StrCpy (StringBuffer2
, L
"Enter (YES) / Esc (NO)");
959 // Popup a menu to notice user
962 IfrLibCreatePopUp (2, &Key
, StringBuffer1
, StringBuffer2
);
963 } while ((Key
.ScanCode
!= SCAN_ESC
) && (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
));
965 FreePool (StringBuffer1
);
966 FreePool (StringBuffer2
);
968 // If the user hits the YES Response key, reset
970 if ((Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
)) {
971 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
973 gST
->ConOut
->ClearScreen (gST
->ConOut
);
979 Get the headers (dos, image, optional header) from an image.
981 @param Device SimpleFileSystem device handle
982 @param FileName File name for the image
983 @param DosHeader Pointer to dos header
984 @param Hdr Pointer to optional header
986 @retval EFI_SUCCESS Successfully get the machine type.
987 @retval EFI_NOT_FOUND The file is not found.
988 @retval EFI_LOAD_ERROR File is not a valid image file.
993 BdsLibGetImageHeader (
994 IN EFI_HANDLE Device
,
996 OUT EFI_IMAGE_DOS_HEADER
*DosHeader
,
997 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
1001 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
1002 EFI_FILE_HANDLE Root
;
1003 EFI_FILE_HANDLE ThisFile
;
1006 EFI_FILE_INFO
*Info
;
1011 // Handle the file system interface to the device
1013 Status
= gBS
->HandleProtocol (
1015 &gEfiSimpleFileSystemProtocolGuid
,
1018 if (EFI_ERROR (Status
)) {
1022 Status
= Volume
->OpenVolume (
1026 if (EFI_ERROR (Status
)) {
1031 Status
= Root
->Open (Root
, &ThisFile
, FileName
, EFI_FILE_MODE_READ
, 0);
1032 if (EFI_ERROR (Status
)) {
1039 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
1042 Status
= gBS
->AllocatePool (EfiBootServicesData
, BufferSize
, (VOID
**) &Info
);
1043 if (EFI_ERROR (Status
)) {
1046 Status
= ThisFile
->GetInfo (
1052 if (!EFI_ERROR (Status
)) {
1055 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1062 FileSize
= Info
->FileSize
;
1068 BufferSize
= sizeof (EFI_IMAGE_DOS_HEADER
);
1069 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, DosHeader
);
1070 if (EFI_ERROR (Status
) ||
1071 BufferSize
< sizeof (EFI_IMAGE_DOS_HEADER
) ||
1072 FileSize
<= DosHeader
->e_lfanew
||
1073 DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
1074 Status
= EFI_LOAD_ERROR
;
1079 // Move to PE signature
1081 Status
= ThisFile
->SetPosition (ThisFile
, DosHeader
->e_lfanew
);
1082 if (EFI_ERROR (Status
)) {
1083 Status
= EFI_LOAD_ERROR
;
1088 // Read and check PE signature
1090 BufferSize
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
1091 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, Hdr
.Pe32
);
1092 if (EFI_ERROR (Status
) ||
1093 BufferSize
< sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
) ||
1094 Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1095 Status
= EFI_LOAD_ERROR
;
1100 // Check PE32 or PE32+ magic
1102 if (Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
&&
1103 Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
1104 Status
= EFI_LOAD_ERROR
;
1109 if (ThisFile
!= NULL
) {
1110 ThisFile
->Close (ThisFile
);
1120 This routine is a notification function for legayc boot or exit boot
1121 service event. It will adjust the memory information for different
1122 memory type and save them into the variables for next boot.
1125 @param Event The event that triggered this notification function.
1126 @param Context Pointer to the notification functions context.
1131 BdsSetMemoryTypeInformationVariable (
1137 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
1138 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
1140 BOOLEAN UpdateRequired
;
1146 EFI_HOB_GUID_TYPE
*GuidHob
;
1148 UpdateRequired
= FALSE
;
1151 // Retrieve the current memory usage statistics. If they are not found, then
1152 // no adjustments can be made to the Memory Type Information variable.
1154 Status
= EfiGetSystemConfigurationTable (
1155 &gEfiMemoryTypeInformationGuid
,
1156 (VOID
**) &CurrentMemoryTypeInformation
1158 if (EFI_ERROR (Status
)) {
1163 // Get the Memory Type Information settings from Hob if they exist,
1164 // PEI is responsible for getting them from variable and build a Hob to save them.
1165 // If the previous Memory Type Information is not available, then set defaults
1167 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
1168 if (GuidHob
== NULL
) {
1170 // If Platform has not built Memory Type Info into the Hob, just return.
1174 PreviousMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
1175 VariableSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
1178 // Use a heuristic to adjust the Memory Type Information for the next boot
1180 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
1183 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
1184 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
1185 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
1190 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
1194 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
1197 // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail
1199 if (Current
> Previous
) {
1200 Next
= Current
+ (Current
>> 2);
1204 if (Next
> 0 && Next
< 4) {
1208 if (Next
!= Previous
) {
1209 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
1210 UpdateRequired
= TRUE
;
1216 // If any changes were made to the Memory Type Information settings, then set the new variable value
1218 if (UpdateRequired
) {
1219 Status
= gRT
->SetVariable (
1220 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
1221 &gEfiMemoryTypeInformationGuid
,
1222 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
1224 PreviousMemoryTypeInformation
1232 This routine register a function to adjust the different type memory page number just before booting
1233 and save the updated info into the variable for next boot to use.
1238 BdsLibSaveMemoryTypeInformation (
1243 EFI_EVENT ReadyToBootEvent
;
1245 Status
= EfiCreateEventReadyToBootEx (
1247 BdsSetMemoryTypeInformationVariable
,
1251 if (EFI_ERROR (Status
)) {
1252 DEBUG ((DEBUG_ERROR
,"Bds Set Memory Type Informationa Variable Fails\n"));