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
;
26 The function will go through the driver option link list, load and start
27 every driver the driver option device path point to.
29 @param BdsDriverLists The header of the current driver option link list
35 IN LIST_ENTRY
*BdsDriverLists
40 BDS_COMMON_OPTION
*Option
;
41 EFI_HANDLE ImageHandle
;
42 EFI_LOADED_IMAGE_PROTOCOL
*ImageInfo
;
50 // Process the driver option
52 for (Link
= BdsDriverLists
->ForwardLink
; Link
!= BdsDriverLists
; Link
= Link
->ForwardLink
) {
53 Option
= CR (Link
, BDS_COMMON_OPTION
, Link
, BDS_LOAD_OPTION_SIGNATURE
);
56 // If a load option is not marked as LOAD_OPTION_ACTIVE,
57 // the boot manager will not automatically load the option.
59 if (!IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_ACTIVE
)) {
64 // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
65 // then all of the EFI drivers in the system will be disconnected and
66 // reconnected after the last driver load option is processed.
68 if (IS_LOAD_OPTION_TYPE (Option
->Attribute
, LOAD_OPTION_FORCE_RECONNECT
)) {
73 // Make sure the driver path is connected.
75 BdsLibConnectDevicePath (Option
->DevicePath
);
78 // Load and start the image that Driver#### describes
80 Status
= gBS
->LoadImage (
89 if (!EFI_ERROR (Status
)) {
90 gBS
->HandleProtocol (ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**) &ImageInfo
);
93 // Verify whether this image is a driver, if not,
94 // exit it and continue to parse next load option
96 if (ImageInfo
->ImageCodeType
!= EfiBootServicesCode
&& ImageInfo
->ImageCodeType
!= EfiRuntimeServicesCode
) {
97 gBS
->Exit (ImageHandle
, EFI_INVALID_PARAMETER
, 0, NULL
);
101 if (Option
->LoadOptionsSize
!= 0) {
102 ImageInfo
->LoadOptionsSize
= Option
->LoadOptionsSize
;
103 ImageInfo
->LoadOptions
= Option
->LoadOptions
;
106 // Before calling the image, enable the Watchdog Timer for
107 // the 5 Minute period
109 gBS
->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL
);
111 Status
= gBS
->StartImage (ImageHandle
, &ExitDataSize
, &ExitData
);
112 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Driver Return Status = %r\n", Status
));
115 // Clear the Watchdog Timer after the image returns
117 gBS
->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL
);
122 // Process the LOAD_OPTION_FORCE_RECONNECT driver option
125 BdsLibDisconnectAllEfi ();
132 Get the Option Number that does not used.
133 Try to locate the specific option variable one by one utile find a free number.
135 @param VariableName Indicate if the boot#### or driver#### option
137 @return The Minimal Free Option Number
141 BdsLibGetFreeOptionNumber (
142 IN CHAR16
*VariableName
147 UINT16
*OptionBuffer
;
151 // Try to find the minimum free number from 0, 1, 2, 3....
155 if (*VariableName
== 'B') {
156 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Boot%04x", Index
);
158 UnicodeSPrint (StrTemp
, sizeof (StrTemp
), L
"Driver%04x", Index
);
161 // try if the option number is used
163 OptionBuffer
= BdsLibGetVariableAndSize (
165 &gEfiGlobalVariableGuid
,
168 if (OptionBuffer
== NULL
) {
174 return ((UINT16
) Index
);
179 This function will register the new boot#### or driver#### option base on
180 the VariableName. The new registered boot#### or driver#### will be linked
181 to BdsOptionList and also update to the VariableName. After the boot#### or
182 driver#### updated, the BootOrder or DriverOrder will also be updated.
184 @param BdsOptionList The header of the boot#### or driver#### link list
185 @param DevicePath The device path which the boot#### or driver####
187 @param String The description of the boot#### or driver####
188 @param VariableName Indicate if the boot#### or driver#### option
190 @retval EFI_SUCCESS The boot#### or driver#### have been success
192 @retval EFI_STATUS Return the status of gRT->SetVariable ().
197 BdsLibRegisterNewOption (
198 IN LIST_ENTRY
*BdsOptionList
,
199 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
201 IN CHAR16
*VariableName
206 UINT16 RegisterOptionNumber
;
207 UINT16
*TempOptionPtr
;
208 UINTN TempOptionSize
;
209 UINT16
*OptionOrderPtr
;
213 EFI_DEVICE_PATH_PROTOCOL
*OptionDevicePath
;
215 CHAR16 OptionName
[10];
216 BOOLEAN UpdateDescription
;
217 UINT16 BootOrderEntry
;
224 OptionDevicePath
= NULL
;
226 OptionOrderPtr
= NULL
;
227 UpdateDescription
= FALSE
;
228 Status
= EFI_SUCCESS
;
229 ZeroMem (OptionName
, sizeof (OptionName
));
232 TempOptionPtr
= BdsLibGetVariableAndSize (
234 &gEfiGlobalVariableGuid
,
238 // Compare with current option variable if the previous option is set in global variable.
240 for (Index
= 0; Index
< TempOptionSize
/ sizeof (UINT16
); Index
++) {
242 // TempOptionPtr must not be NULL if we have non-zero TempOptionSize.
244 ASSERT (TempOptionPtr
!= NULL
);
246 if (*VariableName
== 'B') {
247 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", TempOptionPtr
[Index
]);
249 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", TempOptionPtr
[Index
]);
252 OptionPtr
= BdsLibGetVariableAndSize (
254 &gEfiGlobalVariableGuid
,
257 if (OptionPtr
== NULL
) {
261 TempPtr
+= sizeof (UINT32
) + sizeof (UINT16
);
262 Description
= (CHAR16
*) TempPtr
;
263 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
264 OptionDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
267 // Notes: the description may will change base on the GetStringToken
269 if (CompareMem (OptionDevicePath
, DevicePath
, GetDevicePathSize (OptionDevicePath
)) == 0) {
270 if (CompareMem (Description
, String
, StrSize (Description
)) == 0) {
272 // Got the option, so just return
274 FreePool (OptionPtr
);
275 FreePool (TempOptionPtr
);
279 // Option description changed, need update.
281 UpdateDescription
= TRUE
;
282 FreePool (OptionPtr
);
287 FreePool (OptionPtr
);
290 OptionSize
= sizeof (UINT32
) + sizeof (UINT16
) + StrSize (String
);
291 OptionSize
+= GetDevicePathSize (DevicePath
);
292 OptionPtr
= AllocateZeroPool (OptionSize
);
293 ASSERT (OptionPtr
!= NULL
);
296 *(UINT32
*) TempPtr
= LOAD_OPTION_ACTIVE
;
297 TempPtr
+= sizeof (UINT32
);
298 *(UINT16
*) TempPtr
= (UINT16
) GetDevicePathSize (DevicePath
);
299 TempPtr
+= sizeof (UINT16
);
300 CopyMem (TempPtr
, String
, StrSize (String
));
301 TempPtr
+= StrSize (String
);
302 CopyMem (TempPtr
, DevicePath
, GetDevicePathSize (DevicePath
));
304 if (UpdateDescription
) {
306 // The number in option#### to be updated.
307 // In this case, we must have non-NULL TempOptionPtr.
309 ASSERT (TempOptionPtr
!= NULL
);
310 RegisterOptionNumber
= TempOptionPtr
[Index
];
313 // The new option#### number
315 RegisterOptionNumber
= BdsLibGetFreeOptionNumber(VariableName
);
318 if (*VariableName
== 'B') {
319 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", RegisterOptionNumber
);
321 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", RegisterOptionNumber
);
324 Status
= gRT
->SetVariable (
326 &gEfiGlobalVariableGuid
,
327 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
332 // Return if only need to update a changed description or fail to set option.
334 if (EFI_ERROR (Status
) || UpdateDescription
) {
335 FreePool (OptionPtr
);
336 if (TempOptionPtr
!= NULL
) {
337 FreePool (TempOptionPtr
);
342 FreePool (OptionPtr
);
345 // Update the option order variable
349 // If no option order
351 if (TempOptionSize
== 0) {
353 Status
= gRT
->SetVariable (
355 &gEfiGlobalVariableGuid
,
356 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
360 if (TempOptionPtr
!= NULL
) {
361 FreePool (TempOptionPtr
);
367 // TempOptionPtr must not be NULL if TempOptionSize is not zero.
369 ASSERT (TempOptionPtr
!= NULL
);
371 // Append the new option number to the original option order
373 OrderItemNum
= (TempOptionSize
/ sizeof (UINT16
)) + 1 ;
374 OptionOrderPtr
= AllocateZeroPool ( OrderItemNum
* sizeof (UINT16
));
375 ASSERT (OptionOrderPtr
!= NULL
);
376 CopyMem (OptionOrderPtr
, TempOptionPtr
, (OrderItemNum
- 1) * sizeof (UINT16
));
378 OptionOrderPtr
[Index
] = RegisterOptionNumber
;
380 Status
= gRT
->SetVariable (
382 &gEfiGlobalVariableGuid
,
383 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
384 OrderItemNum
* sizeof (UINT16
),
387 FreePool (TempOptionPtr
);
388 FreePool (OptionOrderPtr
);
395 Build the boot#### or driver#### option from the VariableName, the
396 build boot#### or driver#### will also be linked to BdsCommonOptionList.
398 @param BdsCommonOptionList The header of the boot#### or driver#### option
400 @param VariableName EFI Variable name indicate if it is boot#### or
403 @retval BDS_COMMON_OPTION Get the option just been created
404 @retval NULL Failed to get the new option
409 BdsLibVariableToOption (
410 IN OUT LIST_ENTRY
*BdsCommonOptionList
,
411 IN CHAR16
*VariableName
419 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
420 BDS_COMMON_OPTION
*Option
;
422 UINT32 LoadOptionsSize
;
426 // Read the variable. We will never free this data.
428 Variable
= BdsLibGetVariableAndSize (
430 &gEfiGlobalVariableGuid
,
433 if (Variable
== NULL
) {
437 // Notes: careful defined the variable of Boot#### or
438 // Driver####, consider use some macro to abstract the code
441 // Get the option attribute
444 Attribute
= *(UINT32
*) Variable
;
445 TempPtr
+= sizeof (UINT32
);
448 // Get the option's device path size
450 FilePathSize
= *(UINT16
*) TempPtr
;
451 TempPtr
+= sizeof (UINT16
);
454 // Get the option's description string
456 Description
= (CHAR16
*) TempPtr
;
459 // Get the option's description string size
461 TempPtr
+= StrSize ((CHAR16
*) TempPtr
);
464 // Get the option's device path
466 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) TempPtr
;
467 TempPtr
+= FilePathSize
;
469 LoadOptions
= TempPtr
;
470 LoadOptionsSize
= (UINT32
) (VariableSize
- (UINTN
) (TempPtr
- Variable
));
473 // The Console variables may have multiple device paths, so make
474 // an Entry for each one.
476 Option
= AllocateZeroPool (sizeof (BDS_COMMON_OPTION
));
477 if (Option
== NULL
) {
481 Option
->Signature
= BDS_LOAD_OPTION_SIGNATURE
;
482 Option
->DevicePath
= AllocateZeroPool (GetDevicePathSize (DevicePath
));
483 ASSERT(Option
->DevicePath
!= NULL
);
484 CopyMem (Option
->DevicePath
, DevicePath
, GetDevicePathSize (DevicePath
));
486 Option
->Attribute
= Attribute
;
487 Option
->Description
= AllocateZeroPool (StrSize (Description
));
488 ASSERT(Option
->Description
!= NULL
);
489 CopyMem (Option
->Description
, Description
, StrSize (Description
));
491 Option
->LoadOptions
= AllocateZeroPool (LoadOptionsSize
);
492 ASSERT(Option
->LoadOptions
!= NULL
);
493 CopyMem (Option
->LoadOptions
, LoadOptions
, LoadOptionsSize
);
494 Option
->LoadOptionsSize
= LoadOptionsSize
;
497 // Get the value from VariableName Unicode string
498 // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this
499 // Unicode stream to ASCII without any loss in meaning.
501 if (*VariableName
== 'B') {
502 NumOff
= sizeof (L
"Boot")/sizeof(CHAR16
) -1 ;
503 Option
->BootCurrent
= (UINT16
) ((VariableName
[NumOff
] -'0') * 0x1000);
504 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+1]-'0') * 0x100));
505 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+2]-'0') * 0x10));
506 Option
->BootCurrent
= (UINT16
) (Option
->BootCurrent
+ ((VariableName
[NumOff
+3]-'0')));
509 // Insert active entry to BdsDeviceList
511 if ((Option
->Attribute
& LOAD_OPTION_ACTIVE
) == LOAD_OPTION_ACTIVE
) {
512 InsertTailList (BdsCommonOptionList
, &Option
->Link
);
524 Process BootOrder, or DriverOrder variables, by calling
525 BdsLibVariableToOption () for each UINT16 in the variables.
527 @param BdsCommonOptionList The header of the option list base on variable
529 @param VariableName EFI Variable name indicate the BootOrder or
532 @retval EFI_SUCCESS Success create the boot option or driver option
534 @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list
539 BdsLibBuildOptionFromVar (
540 IN LIST_ENTRY
*BdsCommonOptionList
,
541 IN CHAR16
*VariableName
545 UINTN OptionOrderSize
;
547 BDS_COMMON_OPTION
*Option
;
548 CHAR16 OptionName
[20];
551 // Zero Buffer in order to get all BOOT#### variables
553 ZeroMem (OptionName
, sizeof (OptionName
));
556 // Read the BootOrder, or DriverOrder variable.
558 OptionOrder
= BdsLibGetVariableAndSize (
560 &gEfiGlobalVariableGuid
,
563 if (OptionOrder
== NULL
) {
564 return EFI_OUT_OF_RESOURCES
;
567 for (Index
= 0; Index
< OptionOrderSize
/ sizeof (UINT16
); Index
++) {
568 if (*VariableName
== 'B') {
569 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Boot%04x", OptionOrder
[Index
]);
571 UnicodeSPrint (OptionName
, sizeof (OptionName
), L
"Driver%04x", OptionOrder
[Index
]);
574 Option
= BdsLibVariableToOption (BdsCommonOptionList
, OptionName
);
575 ASSERT (Option
!= NULL
);
576 Option
->BootCurrent
= OptionOrder
[Index
];
580 FreePool (OptionOrder
);
586 Get boot mode by looking up configuration table and parsing HOB list
588 @param BootMode Boot mode from PEI handoff HOB.
590 @retval EFI_SUCCESS Successfully get boot mode
596 OUT EFI_BOOT_MODE
*BootMode
599 *BootMode
= GetBootModeHob ();
605 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
606 buffer, and the size of the buffer. If failure return NULL.
608 @param Name String part of EFI variable name
609 @param VendorGuid GUID part of EFI variable name
610 @param VariableSize Returns the size of the EFI variable that was read
612 @return Dynamically allocated memory that contains a copy of the EFI variable
613 Caller is responsible freeing the buffer.
614 @retval NULL Variable was not read
619 BdsLibGetVariableAndSize (
621 IN EFI_GUID
*VendorGuid
,
622 OUT UINTN
*VariableSize
632 // Pass in a zero size buffer to find the required buffer size.
635 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
636 if (Status
== EFI_BUFFER_TOO_SMALL
) {
638 // Allocate the buffer to return
640 Buffer
= AllocateZeroPool (BufferSize
);
641 if (Buffer
== NULL
) {
645 // Read variable into the allocated buffer.
647 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
648 if (EFI_ERROR (Status
)) {
653 *VariableSize
= BufferSize
;
658 Delete the instance in Multi which matches partly with Single instance
660 @param Multi A pointer to a multi-instance device path data
662 @param Single A pointer to a single-instance device path data
665 @return This function will remove the device path instances in Multi which partly
666 match with the Single, and return the result device path. If there is no
667 remaining device path as a result, this function will return NULL.
670 EFI_DEVICE_PATH_PROTOCOL
*
672 BdsLibDelPartMatchInstance (
673 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
674 IN EFI_DEVICE_PATH_PROTOCOL
*Single
677 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
678 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
679 EFI_DEVICE_PATH_PROTOCOL
*TempNewDevicePath
;
684 NewDevicePath
= NULL
;
685 TempNewDevicePath
= NULL
;
687 if (Multi
== NULL
|| Single
== NULL
) {
691 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
692 SingleDpSize
= GetDevicePathSize (Single
) - END_DEVICE_PATH_LENGTH
;
693 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
695 while (Instance
!= NULL
) {
697 Size
= (SingleDpSize
< InstanceSize
) ? SingleDpSize
: InstanceSize
;
699 if ((CompareMem (Instance
, Single
, Size
) != 0)) {
701 // Append the device path instance which does not match with Single
703 TempNewDevicePath
= NewDevicePath
;
704 NewDevicePath
= AppendDevicePathInstance (NewDevicePath
, Instance
);
705 if (TempNewDevicePath
!= NULL
) {
706 FreePool(TempNewDevicePath
);
710 Instance
= GetNextDevicePathInstance (&Multi
, &InstanceSize
);
711 InstanceSize
-= END_DEVICE_PATH_LENGTH
;
714 return NewDevicePath
;
718 Function compares a device path data structure to that of all the nodes of a
719 second device path 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 @retval TRUE If the Single device path is contained within Multi device path.
727 @retval FALSE The Single device path is not match within Multi device path.
732 BdsLibMatchDevicePaths (
733 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
734 IN EFI_DEVICE_PATH_PROTOCOL
*Single
737 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
738 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
741 if (Multi
== NULL
|| Single
== NULL
) {
746 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
749 // Search for the match of 'Single' in 'Multi'
751 while (DevicePathInst
!= NULL
) {
753 // If the single device path is found in multiple device paths,
756 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
757 FreePool (DevicePathInst
);
761 FreePool (DevicePathInst
);
762 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
769 This function prints a series of strings.
771 @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
772 @param ... A variable argument list containing series of
773 strings, the last string must be NULL.
775 @retval EFI_SUCCESS Success print out the string using ConOut.
776 @retval EFI_STATUS Return the status of the ConOut->OutputString ().
781 BdsLibOutputStrings (
782 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*ConOut
,
790 Status
= EFI_SUCCESS
;
791 VA_START (Args
, ConOut
);
793 while (!EFI_ERROR (Status
)) {
795 // If String is NULL, then it's the end of the list
797 String
= VA_ARG (Args
, CHAR16
*);
798 if (String
== NULL
) {
802 Status
= ConOut
->OutputString (ConOut
, String
);
804 if (EFI_ERROR (Status
)) {
814 // Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.
815 // Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if
816 // user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.
821 Enable the setup browser reset reminder feature.
822 This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.
827 EnableResetReminderFeature (
831 mFeaturerSwitch
= TRUE
;
836 Disable the setup browser reset reminder feature.
837 This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.
842 DisableResetReminderFeature (
846 mFeaturerSwitch
= FALSE
;
851 Record the info that a reset is required.
852 A module boolean variable is used to record whether a reset is required.
857 EnableResetRequired (
861 mResetRequired
= TRUE
;
866 Record the info that no reset is required.
867 A module boolean variable is used to record whether a reset is required.
872 DisableResetRequired (
876 mResetRequired
= FALSE
;
881 Check whether platform policy enable the reset reminder feature. The default is enabled.
886 IsResetReminderFeatureEnable (
890 return mFeaturerSwitch
;
895 Check if user changed any option setting which needs a system reset to be effective.
904 return mResetRequired
;
909 Check whether a reset is needed, and finish the reset reminder feature.
910 If a reset is needed, Popup a menu to notice user, and finish the feature
911 according to the user selection.
921 CHAR16
*StringBuffer1
;
922 CHAR16
*StringBuffer2
;
926 //check any reset required change is applied? if yes, reset system
928 if (IsResetReminderFeatureEnable ()) {
929 if (IsResetRequired ()) {
931 StringBuffer1
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
932 ASSERT (StringBuffer1
!= NULL
);
933 StringBuffer2
= AllocateZeroPool (MAX_STRING_LEN
* sizeof (CHAR16
));
934 ASSERT (StringBuffer2
!= NULL
);
935 StrCpy (StringBuffer1
, L
"Configuration changed. Reset to apply it Now ? ");
936 StrCpy (StringBuffer2
, L
"Enter (YES) / Esc (NO)");
938 // Popup a menu to notice user
941 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, StringBuffer1
, StringBuffer2
, NULL
);
942 } while ((Key
.ScanCode
!= SCAN_ESC
) && (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
));
944 FreePool (StringBuffer1
);
945 FreePool (StringBuffer2
);
947 // If the user hits the YES Response key, reset
949 if ((Key
.UnicodeChar
== CHAR_CARRIAGE_RETURN
)) {
950 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
952 gST
->ConOut
->ClearScreen (gST
->ConOut
);
958 Get the headers (dos, image, optional header) from an image
960 @param Device SimpleFileSystem device handle
961 @param FileName File name for the image
962 @param DosHeader Pointer to dos header
963 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
965 @retval EFI_SUCCESS Successfully get the machine type.
966 @retval EFI_NOT_FOUND The file is not found.
967 @retval EFI_LOAD_ERROR File is not a valid image file.
972 BdsLibGetImageHeader (
973 IN EFI_HANDLE Device
,
975 OUT EFI_IMAGE_DOS_HEADER
*DosHeader
,
976 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
980 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
981 EFI_FILE_HANDLE Root
;
982 EFI_FILE_HANDLE ThisFile
;
990 // Handle the file system interface to the device
992 Status
= gBS
->HandleProtocol (
994 &gEfiSimpleFileSystemProtocolGuid
,
997 if (EFI_ERROR (Status
)) {
1001 Status
= Volume
->OpenVolume (
1005 if (EFI_ERROR (Status
)) {
1010 Status
= Root
->Open (Root
, &ThisFile
, FileName
, EFI_FILE_MODE_READ
, 0);
1011 if (EFI_ERROR (Status
)) {
1018 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
1021 Status
= gBS
->AllocatePool (EfiBootServicesData
, BufferSize
, (VOID
**) &Info
);
1022 if (EFI_ERROR (Status
)) {
1025 Status
= ThisFile
->GetInfo (
1031 if (!EFI_ERROR (Status
)) {
1034 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1041 FileSize
= Info
->FileSize
;
1047 BufferSize
= sizeof (EFI_IMAGE_DOS_HEADER
);
1048 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, DosHeader
);
1049 if (EFI_ERROR (Status
) ||
1050 BufferSize
< sizeof (EFI_IMAGE_DOS_HEADER
) ||
1051 FileSize
<= DosHeader
->e_lfanew
||
1052 DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
1053 Status
= EFI_LOAD_ERROR
;
1058 // Move to PE signature
1060 Status
= ThisFile
->SetPosition (ThisFile
, DosHeader
->e_lfanew
);
1061 if (EFI_ERROR (Status
)) {
1062 Status
= EFI_LOAD_ERROR
;
1067 // Read and check PE signature
1069 BufferSize
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
1070 Status
= ThisFile
->Read (ThisFile
, &BufferSize
, Hdr
.Pe32
);
1071 if (EFI_ERROR (Status
) ||
1072 BufferSize
< sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
) ||
1073 Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1074 Status
= EFI_LOAD_ERROR
;
1079 // Check PE32 or PE32+ magic
1081 if (Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
&&
1082 Hdr
.Pe32
->OptionalHeader
.Magic
!= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
1083 Status
= EFI_LOAD_ERROR
;
1088 if (ThisFile
!= NULL
) {
1089 ThisFile
->Close (ThisFile
);
1099 This routine is a notification function for legayc boot or exit boot
1100 service event. It will adjust the memory information for different
1101 memory type and save them into the variables for next boot.
1104 @param Event The event that triggered this notification function.
1105 @param Context Pointer to the notification functions context.
1110 BdsSetMemoryTypeInformationVariable (
1116 EFI_MEMORY_TYPE_INFORMATION
*PreviousMemoryTypeInformation
;
1117 EFI_MEMORY_TYPE_INFORMATION
*CurrentMemoryTypeInformation
;
1119 BOOLEAN UpdateRequired
;
1125 EFI_HOB_GUID_TYPE
*GuidHob
;
1127 UpdateRequired
= FALSE
;
1130 // Retrieve the current memory usage statistics. If they are not found, then
1131 // no adjustments can be made to the Memory Type Information variable.
1133 Status
= EfiGetSystemConfigurationTable (
1134 &gEfiMemoryTypeInformationGuid
,
1135 (VOID
**) &CurrentMemoryTypeInformation
1137 if (EFI_ERROR (Status
) || CurrentMemoryTypeInformation
== NULL
) {
1142 // Get the Memory Type Information settings from Hob if they exist,
1143 // PEI is responsible for getting them from variable and build a Hob to save them.
1144 // If the previous Memory Type Information is not available, then set defaults
1146 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
1147 if (GuidHob
== NULL
) {
1149 // If Platform has not built Memory Type Info into the Hob, just return.
1153 PreviousMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
1154 VariableSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
1157 // Use a heuristic to adjust the Memory Type Information for the next boot
1159 for (Index
= 0; PreviousMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
1162 for (Index1
= 0; CurrentMemoryTypeInformation
[Index1
].Type
!= EfiMaxMemoryType
; Index1
++) {
1163 if (PreviousMemoryTypeInformation
[Index
].Type
== CurrentMemoryTypeInformation
[Index1
].Type
) {
1164 Current
= CurrentMemoryTypeInformation
[Index1
].NumberOfPages
;
1169 if (CurrentMemoryTypeInformation
[Index1
].Type
== EfiMaxMemoryType
) {
1173 Previous
= PreviousMemoryTypeInformation
[Index
].NumberOfPages
;
1176 // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail
1178 if (Current
> Previous
) {
1179 Next
= Current
+ (Current
>> 2);
1183 if (Next
> 0 && Next
< 4) {
1187 if (Next
!= Previous
) {
1188 PreviousMemoryTypeInformation
[Index
].NumberOfPages
= Next
;
1189 UpdateRequired
= TRUE
;
1195 // If any changes were made to the Memory Type Information settings, then set the new variable value
1197 if (UpdateRequired
) {
1198 Status
= gRT
->SetVariable (
1199 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
1200 &gEfiMemoryTypeInformationGuid
,
1201 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
1203 PreviousMemoryTypeInformation
1211 This routine register a function to adjust the different type memory page number
1212 just before booting and save the updated info into the variable for next boot to use.
1217 BdsLibSaveMemoryTypeInformation (
1222 EFI_EVENT ReadyToBootEvent
;
1224 Status
= EfiCreateEventReadyToBootEx (
1226 BdsSetMemoryTypeInformationVariable
,
1230 if (EFI_ERROR (Status
)) {
1231 DEBUG ((DEBUG_ERROR
,"Bds Set Memory Type Informationa Variable Fails\n"));