-/** @file
-
-Copyright (c) 2004 - 2007, Intel Corporation
-All rights reserved. This program and the accompanying materials
-are licensed and made available under the terms and conditions of the BSD License
-which accompanies this distribution. The full text of the license may be found at
-http://opensource.org/licenses/bsd-license.php
-
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-
-Module Name:
-
- BdsMisc.c
-
-Abstract:
-
- Misc BDS library function
-
-
-**/
-
-#include "InternalBdsLib.h"
-
-
-#define MAX_STRING_LEN 200
-
-static BOOLEAN mFeaturerSwitch = TRUE;
-static BOOLEAN mResetRequired = FALSE;
-
-extern UINT16 gPlatformBootTimeOutDefault;
-
-
-/**
- Return the default value for system Timeout variable.
-
- None
-
- @return Timeout value.
-
-**/
-UINT16
-BdsLibGetTimeout (
- VOID
- )
-{
- UINT16 Timeout;
- UINTN Size;
- EFI_STATUS Status;
-
- //
- // Return Timeout variable or 0xffff if no valid
- // Timeout variable exists.
- //
- Size = sizeof (UINT16);
- Status = gRT->GetVariable (L"Timeout", &gEfiGlobalVariableGuid, NULL, &Size, &Timeout);
- if (!EFI_ERROR (Status)) {
- return Timeout;
- }
- //
- // To make the current EFI Automatic-Test activity possible, just add
- // following code to make AutoBoot enabled when this variable is not
- // present.
- // This code should be removed later.
- //
- Timeout = PcdGet16 (PcdPlatformBootTimeOutDefault);
-
- //
- // Notes: Platform should set default variable if non exists on all error cases!!!
- //
- Status = gRT->SetVariable (
- L"Timeout",
- &gEfiGlobalVariableGuid,
- EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
- sizeof (UINT16),
- &Timeout
- );
- return Timeout;
-}
-
-
-/**
- The function will go through the driver optoin link list, load and start
- every driver the driver optoin device path point to.
-
- @param BdsDriverLists The header of the current driver option link list
-
- @return None
-
-**/
-VOID
-BdsLibLoadDrivers (
- IN LIST_ENTRY *BdsDriverLists
- )
-{
- EFI_STATUS Status;
- LIST_ENTRY *Link;
- BDS_COMMON_OPTION *Option;
- EFI_HANDLE ImageHandle;
- EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;
- UINTN ExitDataSize;
- CHAR16 *ExitData;
- BOOLEAN ReconnectAll;
-
- ReconnectAll = FALSE;
-
- //
- // Process the driver option
- //
- for (Link = BdsDriverLists->ForwardLink; Link != BdsDriverLists; Link = Link->ForwardLink) {
- Option = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);
- //
- // If a load option is not marked as LOAD_OPTION_ACTIVE,
- // the boot manager will not automatically load the option.
- //
- if (!IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_ACTIVE)) {
- continue;
- }
- //
- // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,
- // then all of the EFI drivers in the system will be disconnected and
- // reconnected after the last driver load option is processed.
- //
- if (IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_FORCE_RECONNECT)) {
- ReconnectAll = TRUE;
- }
- //
- // Make sure the driver path is connected.
- //
- BdsLibConnectDevicePath (Option->DevicePath);
-
- //
- // Load and start the image that Driver#### describes
- //
- Status = gBS->LoadImage (
- FALSE,
- mBdsImageHandle,
- Option->DevicePath,
- NULL,
- 0,
- &ImageHandle
- );
-
- if (!EFI_ERROR (Status)) {
- gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);
-
- //
- // Verify whether this image is a driver, if not,
- // exit it and continue to parse next load option
- //
- if (ImageInfo->ImageCodeType != EfiBootServicesCode && ImageInfo->ImageCodeType != EfiRuntimeServicesCode) {
- gBS->Exit (ImageHandle, EFI_INVALID_PARAMETER, 0, NULL);
- continue;
- }
-
- if (Option->LoadOptionsSize != 0) {
- ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;
- ImageInfo->LoadOptions = Option->LoadOptions;
- }
- //
- // Before calling the image, enable the Watchdog Timer for
- // the 5 Minute period
- //
- gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);
-
- Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);
- DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Driver Return Status = %r\n", Status));
-
- //
- // Clear the Watchdog Timer after the image returns
- //
- gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);
- }
- }
- //
- // Process the LOAD_OPTION_FORCE_RECONNECT driver option
- //
- if (ReconnectAll) {
- BdsLibDisconnectAllEfi ();
- BdsLibConnectAll ();
- }
-
-}
-
-
-/**
- Get the Option Number that does not used
- Try to locate the specific option variable one by one untile find a free number
-
- @param VariableName Indicate if the boot#### or driver#### option
-
- @return The Minimal Free Option Number
-
-**/
-UINT16
-BdsLibGetFreeOptionNumber (
- IN CHAR16 *VariableName
- )
-{
- UINT16 Number;
- UINTN Index;
- CHAR16 StrTemp[10];
- UINT16 *OptionBuffer;
- UINTN OptionSize;
-
- //
- // Try to find the minimum free number from 0, 1, 2, 3....
- //
- Index = 0;
- do {
- if (*VariableName == 'B') {
- UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Boot%04x", Index);
- } else {
- UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Driver%04x", Index);
- }
- //
- // try if the option number is used
- //
- OptionBuffer = BdsLibGetVariableAndSize (
- StrTemp,
- &gEfiGlobalVariableGuid,
- &OptionSize
- );
- if (OptionBuffer == NULL) {
- break;
- }
- Index++;
- } while (1);
-
- Number = (UINT16) Index;
- return Number;
-}
-
-
-/**
- This function will register the new boot#### or driver#### option base on
- the VariableName. The new registered boot#### or driver#### will be linked
- to BdsOptionList and also update to the VariableName. After the boot#### or
- driver#### updated, the BootOrder or DriverOrder will also be updated.
-
- @param BdsOptionList The header of the boot#### or driver#### link list
- @param DevicePath The device path which the boot#### or driver####
- option present
- @param String The description of the boot#### or driver####
- @param VariableName Indicate if the boot#### or driver#### option
-
- @retval EFI_SUCCESS The boot#### or driver#### have been success
- registered
- @retval EFI_STATUS Return the status of gRT->SetVariable ().
-
-**/
-EFI_STATUS
-BdsLibRegisterNewOption (
- IN LIST_ENTRY *BdsOptionList,
- IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
- IN CHAR16 *String,
- IN CHAR16 *VariableName
- )
-{
- EFI_STATUS Status;
- UINTN Index;
- UINT16 RegisterOptionNumber;
- UINT16 *TempOptionPtr;
- UINTN TempOptionSize;
- UINT16 *OptionOrderPtr;
- VOID *OptionPtr;
- UINTN OptionSize;
- UINT8 *TempPtr;
- EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;
- CHAR16 *Description;
- CHAR16 OptionName[10];
- BOOLEAN UpdateDescription;
- UINT16 BootOrderEntry;
- UINTN OrderItemNum;
-
-
- OptionPtr = NULL;
- OptionSize = 0;
- TempPtr = NULL;
- OptionDevicePath = NULL;
- Description = NULL;
- OptionOrderPtr = NULL;
- UpdateDescription = FALSE;
- ZeroMem (OptionName, sizeof (OptionName));
-
- TempOptionSize = 0;
- TempOptionPtr = BdsLibGetVariableAndSize (
- VariableName,
- &gEfiGlobalVariableGuid,
- &TempOptionSize
- );
-
- //
- // Compare with current option variable
- //
- for (Index = 0; Index < TempOptionSize / sizeof (UINT16); Index++) {
-
- if (*VariableName == 'B') {
- UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", TempOptionPtr[Index]);
- } else {
- UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", TempOptionPtr[Index]);
- }
-
- OptionPtr = BdsLibGetVariableAndSize (
- OptionName,
- &gEfiGlobalVariableGuid,
- &OptionSize
- );
- if (OptionPtr == NULL) {
- continue;
- }
- TempPtr = OptionPtr;
- TempPtr += sizeof (UINT32) + sizeof (UINT16);
- Description = (CHAR16 *) TempPtr;
- TempPtr += StrSize ((CHAR16 *) TempPtr);
- OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
-
- //
- // Notes: the description may will change base on the GetStringToken
- //
- if (CompareMem (OptionDevicePath, DevicePath, GetDevicePathSize (OptionDevicePath)) == 0) {
- if (CompareMem (Description, String, StrSize (Description)) == 0) {
- //
- // Got the option, so just return
- //
- gBS->FreePool (OptionPtr);
- gBS->FreePool (TempOptionPtr);
- return EFI_SUCCESS;
- } else {
- //
- // Option description changed, need update.
- //
- UpdateDescription = TRUE;
- gBS->FreePool (OptionPtr);
- break;
- }
- }
-
- gBS->FreePool (OptionPtr);
- }
-
- OptionSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (String);
- OptionSize += GetDevicePathSize (DevicePath);
- OptionPtr = AllocateZeroPool (OptionSize);
- TempPtr = OptionPtr;
- *(UINT32 *) TempPtr = LOAD_OPTION_ACTIVE;
- TempPtr += sizeof (UINT32);
- *(UINT16 *) TempPtr = (UINT16) GetDevicePathSize (DevicePath);
- TempPtr += sizeof (UINT16);
- CopyMem (TempPtr, String, StrSize (String));
- TempPtr += StrSize (String);
- CopyMem (TempPtr, DevicePath, GetDevicePathSize (DevicePath));
-
- if (UpdateDescription) {
- //
- // The number in option#### to be updated
- //
- RegisterOptionNumber = TempOptionPtr[Index];
- } else {
- //
- // The new option#### number
- //
- RegisterOptionNumber = BdsLibGetFreeOptionNumber(VariableName);
- }
-
- if (*VariableName == 'B') {
- UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", RegisterOptionNumber);
- } else {
- UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", RegisterOptionNumber);
- }
-
- Status = gRT->SetVariable (
- OptionName,
- &gEfiGlobalVariableGuid,
- EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
- OptionSize,
- OptionPtr
- );
- //
- // Return if only need to update a changed description or fail to set option.
- //
- if (EFI_ERROR (Status) || UpdateDescription) {
- gBS->FreePool (OptionPtr);
- gBS->FreePool (TempOptionPtr);
- return Status;
- }
-
- gBS->FreePool (OptionPtr);
-
- //
- // Update the option order variable
- //
-
- //
- // If no option order
- //
- if (TempOptionSize == 0) {
- BootOrderEntry = 0;
- Status = gRT->SetVariable (
- VariableName,
- &gEfiGlobalVariableGuid,
- EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
- sizeof (UINT16),
- &BootOrderEntry
- );
- if (EFI_ERROR (Status)) {
- gBS->FreePool (TempOptionPtr);
- return Status;
- }
- return EFI_SUCCESS;
- }
-
- //
- // Append the new option number to the original option order
- //
- OrderItemNum = (TempOptionSize / sizeof (UINT16)) + 1 ;
- OptionOrderPtr = AllocateZeroPool ( OrderItemNum * sizeof (UINT16));
- CopyMem (OptionOrderPtr, TempOptionPtr, (OrderItemNum - 1) * sizeof (UINT16));
-
- OptionOrderPtr[Index] = RegisterOptionNumber;
-
- Status = gRT->SetVariable (
- VariableName,
- &gEfiGlobalVariableGuid,
- EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
- OrderItemNum * sizeof (UINT16),
- OptionOrderPtr
- );
- if (EFI_ERROR (Status)) {
- gBS->FreePool (TempOptionPtr);
- gBS->FreePool (OptionOrderPtr);
- return Status;
- }
-
- gBS->FreePool (TempOptionPtr);
- gBS->FreePool (OptionOrderPtr);
-
- return EFI_SUCCESS;
-}
-
-
-/**
- Build the boot#### or driver#### option from the VariableName, the
- build boot#### or driver#### will also be linked to BdsCommonOptionList
-
- @param BdsCommonOptionList The header of the boot#### or driver#### option
- link list
- @param VariableName EFI Variable name indicate if it is boot#### or
- driver####
-
- @retval BDS_COMMON_OPTION Get the option just been created
- @retval NULL Failed to get the new option
-
-**/
-BDS_COMMON_OPTION *
-BdsLibVariableToOption (
- IN OUT LIST_ENTRY *BdsCommonOptionList,
- IN CHAR16 *VariableName
- )
-{
- UINT32 Attribute;
- UINT16 FilePathSize;
- UINT8 *Variable;
- UINT8 *TempPtr;
- UINTN VariableSize;
- EFI_DEVICE_PATH_PROTOCOL *DevicePath;
- BDS_COMMON_OPTION *Option;
- VOID *LoadOptions;
- UINT32 LoadOptionsSize;
- CHAR16 *Description;
- UINT8 NumOff;
- //
- // Read the variable. We will never free this data.
- //
- Variable = BdsLibGetVariableAndSize (
- VariableName,
- &gEfiGlobalVariableGuid,
- &VariableSize
- );
- if (Variable == NULL) {
- return NULL;
- }
- //
- // Notes: careful defined the variable of Boot#### or
- // Driver####, consider use some macro to abstract the code
- //
- //
- // Get the option attribute
- //
- TempPtr = Variable;
- Attribute = *(UINT32 *) Variable;
- TempPtr += sizeof (UINT32);
-
- //
- // Get the option's device path size
- //
- FilePathSize = *(UINT16 *) TempPtr;
- TempPtr += sizeof (UINT16);
-
- //
- // Get the option's description string
- //
- Description = (CHAR16 *) TempPtr;
-
- //
- // Get the option's description string size
- //
- TempPtr += StrSize ((CHAR16 *) TempPtr);
-
- //
- // Get the option's device path
- //
- DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
- TempPtr += FilePathSize;
-
- LoadOptions = TempPtr;
- LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable));
-
- //
- // The Console variables may have multiple device paths, so make
- // an Entry for each one.
- //
- Option = AllocateZeroPool (sizeof (BDS_COMMON_OPTION));
- if (Option == NULL) {
- return NULL;
- }
-
- Option->Signature = BDS_LOAD_OPTION_SIGNATURE;
- Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));
- CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));
- Option->Attribute = Attribute;
- Option->Description = AllocateZeroPool (StrSize (Description));
- CopyMem (Option->Description, Description, StrSize (Description));
- Option->LoadOptions = AllocateZeroPool (LoadOptionsSize);
- CopyMem (Option->LoadOptions, LoadOptions, LoadOptionsSize);
- Option->LoadOptionsSize = LoadOptionsSize;
-
- //
- // Get the value from VariableName Unicode string
- // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this
- // Unicode stream to ASCII without any loss in meaning.
- //
- if (*VariableName == 'B') {
- NumOff = sizeof (L"Boot")/sizeof(CHAR16) -1 ;
- Option->BootCurrent = (UINT16) ((VariableName[NumOff] -'0') * 0x1000);
- Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+1]-'0') * 0x100));
- Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+2]-'0') * 0x10));
- Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+3]-'0')));
- }
- //
- // Insert active entry to BdsDeviceList
- //
- if ((Option->Attribute & LOAD_OPTION_ACTIVE) == LOAD_OPTION_ACTIVE) {
- InsertTailList (BdsCommonOptionList, &Option->Link);
- gBS->FreePool (Variable);
- return Option;
- }
-
- gBS->FreePool (Variable);
- gBS->FreePool (Option);
- return NULL;
-
-}
-
-
-/**
- Process BootOrder, or DriverOrder variables, by calling
- BdsLibVariableToOption () for each UINT16 in the variables.
-
- @param BdsCommonOptionList The header of the option list base on variable
- VariableName
- @param VariableName EFI Variable name indicate the BootOrder or
- DriverOrder
-
- @retval EFI_SUCCESS Success create the boot option or driver option
- list
- @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list
-
-**/
-EFI_STATUS
-BdsLibBuildOptionFromVar (
- IN LIST_ENTRY *BdsCommonOptionList,
- IN CHAR16 *VariableName
- )
-{
- UINT16 *OptionOrder;
- UINTN OptionOrderSize;
- UINTN Index;
- BDS_COMMON_OPTION *Option;
- CHAR16 OptionName[20];
-
- //
- // Zero Buffer in order to get all BOOT#### variables
- //
- ZeroMem (OptionName, sizeof (OptionName));
-
- //
- // Read the BootOrder, or DriverOrder variable.
- //
- OptionOrder = BdsLibGetVariableAndSize (
- VariableName,
- &gEfiGlobalVariableGuid,
- &OptionOrderSize
- );
- if (OptionOrder == NULL) {
- return EFI_OUT_OF_RESOURCES;
- }
-
- for (Index = 0; Index < OptionOrderSize / sizeof (UINT16); Index++) {
- if (*VariableName == 'B') {
- UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionOrder[Index]);
- } else {
- UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", OptionOrder[Index]);
- }
-
- Option = BdsLibVariableToOption (BdsCommonOptionList, OptionName);
- Option->BootCurrent = OptionOrder[Index];
-
- }
-
- gBS->FreePool (OptionOrder);
-
- return EFI_SUCCESS;
-}
-
-
-/**
- Get boot mode by looking up configuration table and parsing HOB list
-
- @param BootMode Boot mode from PEI handoff HOB.
-
- @retval EFI_SUCCESS Successfully get boot mode
- @retval EFI_NOT_FOUND Can not find the current system boot mode
-
-**/
-EFI_STATUS
-BdsLibGetBootMode (
- OUT EFI_BOOT_MODE *BootMode
- )
-{
- VOID *HobList;
- EFI_STATUS Status;
-
- //
- // Get Hob list
- //
- Status = EfiGetSystemConfigurationTable (&gEfiHobListGuid, &HobList);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Hob list not found\n"));
- *BootMode = 0;
- return EFI_NOT_FOUND;
- }
-
- Status = R8_GetHobBootMode (HobList, BootMode);
- if (EFI_ERROR (Status)) {
- return EFI_NOT_FOUND;
- }
-
- return EFI_SUCCESS;
-}
-
-
-/**
- Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
- buffer, and the size of the buffer. If failure return NULL.
-
- @param Name String part of EFI variable name
- @param VendorGuid GUID part of EFI variable name
- @param VariableSize Returns the size of the EFI variable that was read
-
- @return Dynamically allocated memory that contains a copy of the EFI variable.
- @return Caller is responsible freeing the buffer.
- @retval NULL Variable was not read
-
-**/
-VOID *
-BdsLibGetVariableAndSize (
- IN CHAR16 *Name,
- IN EFI_GUID *VendorGuid,
- OUT UINTN *VariableSize
- )
-{
- EFI_STATUS Status;
- UINTN BufferSize;
- VOID *Buffer;
-
- Buffer = NULL;
-
- //
- // Pass in a zero size buffer to find the required buffer size.
- //
- BufferSize = 0;
- Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
- if (Status == EFI_BUFFER_TOO_SMALL) {
- //
- // Allocate the buffer to return
- //
- Buffer = AllocateZeroPool (BufferSize);
- if (Buffer == NULL) {
- return NULL;
- }
- //
- // Read variable into the allocated buffer.
- //
- Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
- if (EFI_ERROR (Status)) {
- BufferSize = 0;
- }
- }
-
- *VariableSize = BufferSize;
- return Buffer;
-}
-
-
-/**
- Delete the instance in Multi which matches partly with Single instance
-
- @param Multi A pointer to a multi-instance device path data
- structure.
- @param Single A pointer to a single-instance device path data
- structure.
-
- @return This function will remove the device path instances in Multi which partly
- @return match with the Single, and return the result device path. If there is no
- @return remaining device path as a result, this function will return NULL.
-
-**/
-EFI_DEVICE_PATH_PROTOCOL *
-BdsLibDelPartMatchInstance (
- IN EFI_DEVICE_PATH_PROTOCOL *Multi,
- IN EFI_DEVICE_PATH_PROTOCOL *Single
- )
-{
- EFI_DEVICE_PATH_PROTOCOL *Instance;
- EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
- EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;
- UINTN InstanceSize;
- UINTN SingleDpSize;
- UINTN Size;
-
- NewDevicePath = NULL;
- TempNewDevicePath = NULL;
-
- if (Multi == NULL || Single == NULL) {
- return Multi;
- }
-
- Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);
- SingleDpSize = GetDevicePathSize (Single) - END_DEVICE_PATH_LENGTH;
- InstanceSize -= END_DEVICE_PATH_LENGTH;
-
- while (Instance != NULL) {
-
- Size = (SingleDpSize < InstanceSize) ? SingleDpSize : InstanceSize;
-
- if ((CompareMem (Instance, Single, Size) != 0)) {
- //
- // Append the device path instance which does not match with Single
- //
- TempNewDevicePath = NewDevicePath;
- NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);
- SafeFreePool(TempNewDevicePath);
- }
- SafeFreePool(Instance);
- Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);
- InstanceSize -= END_DEVICE_PATH_LENGTH;
- }
-
- return NewDevicePath;
-}
-
-
-/**
- Function compares a device path data structure to that of all the nodes of a
- second device path instance.
-
- @param Multi A pointer to a multi-instance device path data
- structure.
- @param Single A pointer to a single-instance device path data
- structure.
-
- @retval TRUE If the Single is contained within Multi
- @retval FALSE The Single is not match within Multi
-
-**/
-BOOLEAN
-BdsLibMatchDevicePaths (
- IN EFI_DEVICE_PATH_PROTOCOL *Multi,
- IN EFI_DEVICE_PATH_PROTOCOL *Single
- )
-{
- EFI_DEVICE_PATH_PROTOCOL *DevicePath;
- EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;
- UINTN Size;
-
- if (!Multi || !Single) {
- return FALSE;
- }
-
- DevicePath = Multi;
- DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
-
- //
- // Search for the match of 'Single' in 'Multi'
- //
- while (DevicePathInst != NULL) {
- //
- // If the single device path is found in multiple device paths,
- // return success
- //
- if (CompareMem (Single, DevicePathInst, Size) == 0) {
- gBS->FreePool (DevicePathInst);
- return TRUE;
- }
-
- gBS->FreePool (DevicePathInst);
- DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
- }
-
- return FALSE;
-}
-
-
-/**
- This function prints a series of strings.
-
- @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
- @param ... A variable argument list containing series of
- strings, the last string must be NULL.
-
- @retval EFI_SUCCESS Success print out the string using ConOut.
- @retval EFI_STATUS Return the status of the ConOut->OutputString ().
-
-**/
-EFI_STATUS
-BdsLibOutputStrings (
- IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut,
- ...
- )
-{
- VA_LIST args;
- EFI_STATUS Status;
- CHAR16 *String;
-
- Status = EFI_SUCCESS;
- VA_START (args, ConOut);
-
- while (!EFI_ERROR (Status)) {
- //
- // If String is NULL, then it's the end of the list
- //
- String = VA_ARG (args, CHAR16 *);
- if (!String) {
- break;
- }
-
- Status = ConOut->OutputString (ConOut, String);
-
- if (EFI_ERROR (Status)) {
- break;
- }
- }
-
- return Status;
-}
-
-//
-// Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.
-// Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if
-// user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.
-//
-
-
-/**
- Enable the setup browser reset reminder feature.
- This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.
-
- VOID
-
- @return VOID
-
-**/
-VOID
-EnableResetReminderFeature (
- VOID
- )
-{
- mFeaturerSwitch = TRUE;
-}
-
-
-/**
- Disable the setup browser reset reminder feature.
- This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.
-
- VOID
-
- @return VOID
-
-**/
-VOID
-DisableResetReminderFeature (
- VOID
- )
-{
- mFeaturerSwitch = FALSE;
-}
-
-
-/**
- Record the info that a reset is required.
- A module boolean variable is used to record whether a reset is required.
-
- VOID
-
- @return VOID
-
-**/
-VOID
-EnableResetRequired (
- VOID
- )
-{
- mResetRequired = TRUE;
-}
-
-
-/**
- Record the info that no reset is required.
- A module boolean variable is used to record whether a reset is required.
-
- VOID
-
- @return VOID
-
-**/
-VOID
-DisableResetRequired (
- VOID
- )
-{
- mResetRequired = FALSE;
-}
-
-
-/**
- Check whether platform policy enable the reset reminder feature. The default is enabled.
-
- VOID
-
- @return VOID
-
-**/
-BOOLEAN
-IsResetReminderFeatureEnable (
- VOID
- )
-{
- return mFeaturerSwitch;
-}
-
-
-/**
- Check if user changed any option setting which needs a system reset to be effective.
-
- VOID
-
- @return VOID
-
-**/
-BOOLEAN
-IsResetRequired (
- VOID
- )
-{
- return mResetRequired;
-}
-
-
-/**
- Check whether a reset is needed, and finish the reset reminder feature.
- If a reset is needed, Popup a menu to notice user, and finish the feature
- according to the user selection.
-
- VOID
-
- @return VOID
-
-**/
-VOID
-SetupResetReminder (
- VOID
- )
-{
- EFI_INPUT_KEY Key;
- CHAR16 *StringBuffer1;
- CHAR16 *StringBuffer2;
-
-
- //
- //check any reset required change is applied? if yes, reset system
- //
- if (IsResetReminderFeatureEnable ()) {
- if (IsResetRequired ()) {
-
- StringBuffer1 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));
- ASSERT (StringBuffer1 != NULL);
- StringBuffer2 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));
- ASSERT (StringBuffer2 != NULL);
- StrCpy (StringBuffer1, L"Configuration changed. Reset to apply it Now ? ");
- StrCpy (StringBuffer2, L"Enter (YES) / Esc (NO)");
- //
- // Popup a menu to notice user
- //
- do {
- IfrLibCreatePopUp (2, &Key, StringBuffer1, StringBuffer2);
- } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));
-
- gBS->FreePool (StringBuffer1);
- gBS->FreePool (StringBuffer2);
- //
- // If the user hits the YES Response key, reset
- //
- if ((Key.UnicodeChar == CHAR_CARRIAGE_RETURN)) {
- gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);
- }
- gST->ConOut->ClearScreen (gST->ConOut);
- }
- }
-}
-
-
-/**
- Get the headers (dos, image, optional header) from an image
-
- @param Device SimpleFileSystem device handle
- @param FileName File name for the image
- @param DosHeader Pointer to dos header
- @param ImageHeader Pointer to image header
- @param OptionalHeader Pointer to optional header
-
- @retval EFI_SUCCESS Successfully get the machine type.
- @retval EFI_NOT_FOUND The file is not found.
- @retval EFI_LOAD_ERROR File is not a valid image file.
-
-**/
-EFI_STATUS
-BdsLibGetImageHeader (
- IN EFI_HANDLE Device,
- IN CHAR16 *FileName,
- OUT EFI_IMAGE_DOS_HEADER *DosHeader,
- OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
- )
-{
- EFI_STATUS Status;
- EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;
- EFI_FILE_HANDLE Root;
- EFI_FILE_HANDLE ThisFile;
- UINTN BufferSize;
- UINT64 FileSize;
- EFI_FILE_INFO *Info;
-
- Root = NULL;
- ThisFile = NULL;
- //
- // Handle the file system interface to the device
- //
- Status = gBS->HandleProtocol (
- Device,
- &gEfiSimpleFileSystemProtocolGuid,
- (VOID *) &Volume
- );
- if (EFI_ERROR (Status)) {
- goto Done;
- }
-
- Status = Volume->OpenVolume (
- Volume,
- &Root
- );
- if (EFI_ERROR (Status)) {
- goto Done;
- }
-
- Status = Root->Open (Root, &ThisFile, FileName, EFI_FILE_MODE_READ, 0);
- if (EFI_ERROR (Status)) {
- goto Done;
- }
-
- //
- // Get file size
- //
- BufferSize = SIZE_OF_EFI_FILE_INFO + 200;
- do {
- Info = NULL;
- Status = gBS->AllocatePool (EfiBootServicesData, BufferSize, (VOID **) &Info);
- if (EFI_ERROR (Status)) {
- goto Done;
- }
- Status = ThisFile->GetInfo (
- ThisFile,
- &gEfiFileInfoGuid,
- &BufferSize,
- Info
- );
- if (!EFI_ERROR (Status)) {
- break;
- }
- if (Status != EFI_BUFFER_TOO_SMALL) {
- goto Done;
- }
- gBS->FreePool (Info);
- } while (TRUE);
-
- FileSize = Info->FileSize;
- gBS->FreePool (Info);
-
- //
- // Read dos header
- //
- BufferSize = sizeof (EFI_IMAGE_DOS_HEADER);
- Status = ThisFile->Read (ThisFile, &BufferSize, DosHeader);
- if (EFI_ERROR (Status) ||
- BufferSize < sizeof (EFI_IMAGE_DOS_HEADER) ||
- FileSize <= DosHeader->e_lfanew ||
- DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {
- Status = EFI_LOAD_ERROR;
- goto Done;
- }
-
- //
- // Move to PE signature
- //
- Status = ThisFile->SetPosition (ThisFile, DosHeader->e_lfanew);
- if (EFI_ERROR (Status)) {
- Status = EFI_LOAD_ERROR;
- goto Done;
- }
-
- //
- // Read and check PE signature
- //
- BufferSize = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
- Status = ThisFile->Read (ThisFile, &BufferSize, Hdr.Pe32);
- if (EFI_ERROR (Status) ||
- BufferSize < sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION) ||
- Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {
- Status = EFI_LOAD_ERROR;
- goto Done;
- }
-
- //
- // Check PE32 or PE32+ magic
- //
- if (Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC &&
- Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
- Status = EFI_LOAD_ERROR;
- goto Done;
- }
-
- Done:
- if (ThisFile != NULL) {
- ThisFile->Close (ThisFile);
- }
- if (Root != NULL) {
- Root->Close (Root);
- }
- return Status;
-}
-
-VOID
-EFIAPI
-BdsSetMemoryTypeInformationVariable (
- EFI_EVENT Event,
- VOID *Context
- )
-/*++
-
-Routine Description:
-
- This routine is a notification function for legayc boot or exit boot
- service event. It will adjust the memory information for different
- memory type and save them into the variables for next boot
-
-Arguments:
-
- Event - The event that triggered this notification function
- Context - Pointer to the notification functions context
-
-Returns:
-
- None.
-
---*/
-{
- EFI_STATUS Status;
- EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;
- EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;
- UINTN VariableSize;
- BOOLEAN UpdateRequired;
- UINTN Index;
- UINTN Index1;
- UINT32 Previous;
- UINT32 Current;
- UINT32 Next;
- VOID *HobList;
-
- UpdateRequired = FALSE;
-
- //
- // Retrieve the current memory usage statistics. If they are not found, then
- // no adjustments can be made to the Memory Type Information variable.
- //
- Status = EfiGetSystemConfigurationTable (
- &gEfiMemoryTypeInformationGuid,
- (VOID **) &CurrentMemoryTypeInformation
- );
- if (EFI_ERROR (Status)) {
- return;
- }
-
- //
- // Get the Memory Type Information settings from Hob if they exist,
- // PEI is responsible for getting them from variable and build a Hob to save them.
- // If the previous Memory Type Information is not available, then set defaults
- //
- EfiGetSystemConfigurationTable (&gEfiHobListGuid, &HobList);
- Status = R8_GetNextGuidHob (&HobList, &gEfiMemoryTypeInformationGuid, (VOID **) &PreviousMemoryTypeInformation, &VariableSize);
- if (EFI_ERROR (Status) || PreviousMemoryTypeInformation == NULL) {
- //
- // If Platform has not built Memory Type Info into the Hob, just return.
- //
- return;
- }
-
- //
- // Use a heuristic to adjust the Memory Type Information for the next boot
- //
- for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {
-
- Current = 0;
- for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {
- if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {
- Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;
- break;
- }
- }
-
- if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {
- continue;
- }
-
- Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;
-
- //
- // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail
- //
- if (Current > Previous) {
- Next = Current + (Current >> 2);
- } else {
- Next = Previous;
- }
- if (Next > 0 && Next < 4) {
- Next = 4;
- }
-
- if (Next != Previous) {
- PreviousMemoryTypeInformation[Index].NumberOfPages = Next;
- UpdateRequired = TRUE;
- }
-
- }
-
- //
- // If any changes were made to the Memory Type Information settings, then set the new variable value
- //
- if (UpdateRequired) {
- Status = gRT->SetVariable (
- EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
- &gEfiMemoryTypeInformationGuid,
- EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
- VariableSize,
- PreviousMemoryTypeInformation
- );
- }
-
- return;
-}
-
-
-/**
- This routine register a function to adjust the different type memory page number just before booting
- and save the updated info into the variable for next boot to use
-
- None
-
- @return None.
-
-**/
-VOID
-EFIAPI
-BdsLibSaveMemoryTypeInformation (
- VOID
- )
-{
- EFI_STATUS Status;
- EFI_EVENT ReadyToBootEvent;
-
- Status = EfiCreateEventReadyToBootEx (
- TPL_CALLBACK,
- BdsSetMemoryTypeInformationVariable,
- NULL,
- &ReadyToBootEvent
- );
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR,"Bds Set Memory Type Informationa Variable Fails\n"));
- }
-
-}
-
-
-/**
- return the current TPL, copied from the EDKII glue lib.
-
- VOID
-
- @return Current TPL
-
-**/
-EFI_TPL
-BdsLibGetCurrentTpl (
- VOID
- )
-{
- EFI_TPL Tpl;
-
- Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
- gBS->RestoreTPL (Tpl);
-
- return Tpl;
-}
+/** @file\r
+ Misc BDS library function\r
+\r
+Copyright (c) 2004 - 2008, Intel Corporation. <BR>\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "InternalBdsLib.h"\r
+\r
+\r
+#define MAX_STRING_LEN 200\r
+\r
+BOOLEAN mFeaturerSwitch = TRUE;\r
+BOOLEAN mResetRequired = FALSE;\r
+\r
+extern UINT16 gPlatformBootTimeOutDefault;\r
+\r
+\r
+/**\r
+ Return the default value for system Timeout variable.\r
+\r
+ @return Timeout value.\r
+\r
+**/\r
+UINT16\r
+EFIAPI\r
+BdsLibGetTimeout (\r
+ VOID\r
+ )\r
+{\r
+ UINT16 Timeout;\r
+ UINTN Size;\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Return Timeout variable or 0xffff if no valid\r
+ // Timeout variable exists.\r
+ //\r
+ Size = sizeof (UINT16);\r
+ Status = gRT->GetVariable (L"Timeout", &gEfiGlobalVariableGuid, NULL, &Size, &Timeout);\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // According to UEFI 2.0 spec, it should treat the Timeout value as 0xffff\r
+ // (default value PcdPlatformBootTimeOutDefault) when L"Timeout" variable is not present.\r
+ // To make the current EFI Automatic-Test activity possible, platform can choose other value\r
+ // for automatic boot when the variable is not present.\r
+ //\r
+ Timeout = PcdGet16 (PcdPlatformBootTimeOutDefault);\r
+ }\r
+\r
+ return Timeout;\r
+}\r
+\r
+/**\r
+ The function will go through the driver optoin link list, load and start\r
+ every driver the driver optoin device path point to.\r
+\r
+ @param BdsDriverLists The header of the current driver option link list\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+BdsLibLoadDrivers (\r
+ IN LIST_ENTRY *BdsDriverLists\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ LIST_ENTRY *Link;\r
+ BDS_COMMON_OPTION *Option;\r
+ EFI_HANDLE ImageHandle;\r
+ EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
+ UINTN ExitDataSize;\r
+ CHAR16 *ExitData;\r
+ BOOLEAN ReconnectAll;\r
+\r
+ ReconnectAll = FALSE;\r
+\r
+ //\r
+ // Process the driver option\r
+ //\r
+ for (Link = BdsDriverLists->ForwardLink; Link != BdsDriverLists; Link = Link->ForwardLink) {\r
+ Option = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);\r
+ \r
+ //\r
+ // If a load option is not marked as LOAD_OPTION_ACTIVE,\r
+ // the boot manager will not automatically load the option.\r
+ //\r
+ if (!IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_ACTIVE)) {\r
+ continue;\r
+ }\r
+ \r
+ //\r
+ // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,\r
+ // then all of the EFI drivers in the system will be disconnected and\r
+ // reconnected after the last driver load option is processed.\r
+ //\r
+ if (IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_FORCE_RECONNECT)) {\r
+ ReconnectAll = TRUE;\r
+ }\r
+ \r
+ //\r
+ // Make sure the driver path is connected.\r
+ //\r
+ BdsLibConnectDevicePath (Option->DevicePath);\r
+\r
+ //\r
+ // Load and start the image that Driver#### describes\r
+ //\r
+ Status = gBS->LoadImage (\r
+ FALSE,\r
+ mBdsImageHandle,\r
+ Option->DevicePath,\r
+ NULL,\r
+ 0,\r
+ &ImageHandle\r
+ );\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
+\r
+ //\r
+ // Verify whether this image is a driver, if not,\r
+ // exit it and continue to parse next load option\r
+ //\r
+ if (ImageInfo->ImageCodeType != EfiBootServicesCode && ImageInfo->ImageCodeType != EfiRuntimeServicesCode) {\r
+ gBS->Exit (ImageHandle, EFI_INVALID_PARAMETER, 0, NULL);\r
+ continue;\r
+ }\r
+\r
+ if (Option->LoadOptionsSize != 0) {\r
+ ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;\r
+ ImageInfo->LoadOptions = Option->LoadOptions;\r
+ }\r
+ //\r
+ // Before calling the image, enable the Watchdog Timer for\r
+ // the 5 Minute period\r
+ //\r
+ gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
+\r
+ Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);\r
+ DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Driver Return Status = %r\n", Status));\r
+\r
+ //\r
+ // Clear the Watchdog Timer after the image returns\r
+ //\r
+ gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
+ }\r
+ }\r
+ \r
+ //\r
+ // Process the LOAD_OPTION_FORCE_RECONNECT driver option\r
+ //\r
+ if (ReconnectAll) {\r
+ BdsLibDisconnectAllEfi ();\r
+ BdsLibConnectAll ();\r
+ }\r
+\r
+}\r
+\r
+/**\r
+ Get the Option Number that does not used.\r
+ Try to locate the specific option variable one by one untile find a free number.\r
+\r
+ @param VariableName Indicate if the boot#### or driver#### option\r
+\r
+ @return The Minimal Free Option Number\r
+\r
+**/\r
+UINT16\r
+BdsLibGetFreeOptionNumber (\r
+ IN CHAR16 *VariableName\r
+ )\r
+{\r
+ UINTN Index;\r
+ CHAR16 StrTemp[10];\r
+ UINT16 *OptionBuffer;\r
+ UINTN OptionSize;\r
+\r
+ //\r
+ // Try to find the minimum free number from 0, 1, 2, 3....\r
+ //\r
+ Index = 0;\r
+ do {\r
+ if (*VariableName == 'B') {\r
+ UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Boot%04x", Index);\r
+ } else {\r
+ UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Driver%04x", Index);\r
+ }\r
+ //\r
+ // try if the option number is used\r
+ //\r
+ OptionBuffer = BdsLibGetVariableAndSize (\r
+ StrTemp,\r
+ &gEfiGlobalVariableGuid,\r
+ &OptionSize\r
+ );\r
+ if (OptionBuffer == NULL) {\r
+ break;\r
+ }\r
+ Index ++;\r
+ } while (TRUE);\r
+\r
+ return ((UINT16) Index);\r
+}\r
+\r
+\r
+/**\r
+ This function will register the new boot#### or driver#### option base on\r
+ the VariableName. The new registered boot#### or driver#### will be linked\r
+ to BdsOptionList and also update to the VariableName. After the boot#### or\r
+ driver#### updated, the BootOrder or DriverOrder will also be updated.\r
+\r
+ @param BdsOptionList The header of the boot#### or driver#### link list\r
+ @param DevicePath The device path which the boot#### or driver####\r
+ option present\r
+ @param String The description of the boot#### or driver####\r
+ @param VariableName Indicate if the boot#### or driver#### option\r
+\r
+ @retval EFI_SUCCESS The boot#### or driver#### have been success\r
+ registered\r
+ @retval EFI_STATUS Return the status of gRT->SetVariable ().\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+BdsLibRegisterNewOption (\r
+ IN LIST_ENTRY *BdsOptionList,\r
+ IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
+ IN CHAR16 *String,\r
+ IN CHAR16 *VariableName\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN Index;\r
+ UINT16 RegisterOptionNumber;\r
+ UINT16 *TempOptionPtr;\r
+ UINTN TempOptionSize;\r
+ UINT16 *OptionOrderPtr;\r
+ VOID *OptionPtr;\r
+ UINTN OptionSize;\r
+ UINT8 *TempPtr;\r
+ EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
+ CHAR16 *Description;\r
+ CHAR16 OptionName[10];\r
+ BOOLEAN UpdateDescription;\r
+ UINT16 BootOrderEntry;\r
+ UINTN OrderItemNum;\r
+\r
+\r
+ OptionPtr = NULL;\r
+ OptionSize = 0;\r
+ TempPtr = NULL;\r
+ OptionDevicePath = NULL;\r
+ Description = NULL;\r
+ OptionOrderPtr = NULL;\r
+ UpdateDescription = FALSE;\r
+ Status = EFI_SUCCESS;\r
+ ZeroMem (OptionName, sizeof (OptionName));\r
+\r
+ TempOptionSize = 0;\r
+ TempOptionPtr = BdsLibGetVariableAndSize (\r
+ VariableName,\r
+ &gEfiGlobalVariableGuid,\r
+ &TempOptionSize\r
+ );\r
+\r
+ //\r
+ // Compare with current option variable\r
+ //\r
+ for (Index = 0; Index < TempOptionSize / sizeof (UINT16); Index++) {\r
+\r
+ if (*VariableName == 'B') {\r
+ UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", TempOptionPtr[Index]);\r
+ } else {\r
+ UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", TempOptionPtr[Index]);\r
+ }\r
+\r
+ OptionPtr = BdsLibGetVariableAndSize (\r
+ OptionName,\r
+ &gEfiGlobalVariableGuid,\r
+ &OptionSize\r
+ );\r
+ if (OptionPtr == NULL) {\r
+ continue;\r
+ }\r
+ TempPtr = OptionPtr;\r
+ TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
+ Description = (CHAR16 *) TempPtr;\r
+ TempPtr += StrSize ((CHAR16 *) TempPtr);\r
+ OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
+\r
+ //\r
+ // Notes: the description may will change base on the GetStringToken\r
+ //\r
+ if (CompareMem (OptionDevicePath, DevicePath, GetDevicePathSize (OptionDevicePath)) == 0) {\r
+ if (CompareMem (Description, String, StrSize (Description)) == 0) { \r
+ //\r
+ // Got the option, so just return\r
+ //\r
+ FreePool (OptionPtr);\r
+ FreePool (TempOptionPtr);\r
+ return EFI_SUCCESS;\r
+ } else {\r
+ //\r
+ // Option description changed, need update.\r
+ //\r
+ UpdateDescription = TRUE;\r
+ FreePool (OptionPtr);\r
+ break;\r
+ }\r
+ }\r
+\r
+ FreePool (OptionPtr);\r
+ }\r
+\r
+ OptionSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (String);\r
+ OptionSize += GetDevicePathSize (DevicePath);\r
+ OptionPtr = AllocateZeroPool (OptionSize);\r
+ ASSERT (OptionPtr != NULL);\r
+ \r
+ TempPtr = OptionPtr;\r
+ *(UINT32 *) TempPtr = LOAD_OPTION_ACTIVE;\r
+ TempPtr += sizeof (UINT32);\r
+ *(UINT16 *) TempPtr = (UINT16) GetDevicePathSize (DevicePath);\r
+ TempPtr += sizeof (UINT16);\r
+ CopyMem (TempPtr, String, StrSize (String));\r
+ TempPtr += StrSize (String);\r
+ CopyMem (TempPtr, DevicePath, GetDevicePathSize (DevicePath));\r
+\r
+ if (UpdateDescription) {\r
+ //\r
+ // The number in option#### to be updated\r
+ //\r
+ RegisterOptionNumber = TempOptionPtr[Index];\r
+ } else {\r
+ //\r
+ // The new option#### number\r
+ //\r
+ RegisterOptionNumber = BdsLibGetFreeOptionNumber(VariableName);\r
+ }\r
+\r
+ if (*VariableName == 'B') {\r
+ UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", RegisterOptionNumber);\r
+ } else {\r
+ UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", RegisterOptionNumber);\r
+ }\r
+\r
+ Status = gRT->SetVariable (\r
+ OptionName,\r
+ &gEfiGlobalVariableGuid,\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+ OptionSize,\r
+ OptionPtr\r
+ );\r
+ //\r
+ // Return if only need to update a changed description or fail to set option.\r
+ //\r
+ if (EFI_ERROR (Status) || UpdateDescription) {\r
+ FreePool (OptionPtr);\r
+ if (TempOptionPtr != NULL) {\r
+ FreePool (TempOptionPtr);\r
+ }\r
+ return Status;\r
+ }\r
+\r
+ FreePool (OptionPtr);\r
+\r
+ //\r
+ // Update the option order variable\r
+ //\r
+\r
+ //\r
+ // If no option order\r
+ //\r
+ if (TempOptionSize == 0) {\r
+ BootOrderEntry = 0;\r
+ Status = gRT->SetVariable (\r
+ VariableName,\r
+ &gEfiGlobalVariableGuid,\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+ sizeof (UINT16),\r
+ &BootOrderEntry\r
+ );\r
+ if (TempOptionPtr != NULL) {\r
+ FreePool (TempOptionPtr);\r
+ }\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Append the new option number to the original option order\r
+ //\r
+ OrderItemNum = (TempOptionSize / sizeof (UINT16)) + 1 ;\r
+ OptionOrderPtr = AllocateZeroPool ( OrderItemNum * sizeof (UINT16));\r
+ ASSERT (OptionOrderPtr!= NULL);\r
+ \r
+ CopyMem (OptionOrderPtr, TempOptionPtr, (OrderItemNum - 1) * sizeof (UINT16));\r
+\r
+ OptionOrderPtr[Index] = RegisterOptionNumber;\r
+\r
+ Status = gRT->SetVariable (\r
+ VariableName,\r
+ &gEfiGlobalVariableGuid,\r
+ EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+ OrderItemNum * sizeof (UINT16),\r
+ OptionOrderPtr\r
+ );\r
+ FreePool (TempOptionPtr);\r
+ FreePool (OptionOrderPtr);\r
+\r
+ return Status;\r
+}\r
+\r
+\r
+/**\r
+ Build the boot#### or driver#### option from the VariableName, the\r
+ build boot#### or driver#### will also be linked to BdsCommonOptionList.\r
+\r
+ @param BdsCommonOptionList The header of the boot#### or driver#### option\r
+ link list\r
+ @param VariableName EFI Variable name indicate if it is boot#### or\r
+ driver####\r
+\r
+ @retval BDS_COMMON_OPTION Get the option just been created\r
+ @retval NULL Failed to get the new option\r
+\r
+**/\r
+BDS_COMMON_OPTION *\r
+EFIAPI\r
+BdsLibVariableToOption (\r
+ IN OUT LIST_ENTRY *BdsCommonOptionList,\r
+ IN CHAR16 *VariableName\r
+ )\r
+{\r
+ UINT32 Attribute;\r
+ UINT16 FilePathSize;\r
+ UINT8 *Variable;\r
+ UINT8 *TempPtr;\r
+ UINTN VariableSize;\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ BDS_COMMON_OPTION *Option;\r
+ VOID *LoadOptions;\r
+ UINT32 LoadOptionsSize;\r
+ CHAR16 *Description;\r
+ UINT8 NumOff;\r
+ //\r
+ // Read the variable. We will never free this data.\r
+ //\r
+ Variable = BdsLibGetVariableAndSize (\r
+ VariableName,\r
+ &gEfiGlobalVariableGuid,\r
+ &VariableSize\r
+ );\r
+ if (Variable == NULL) {\r
+ return NULL;\r
+ }\r
+ //\r
+ // Notes: careful defined the variable of Boot#### or\r
+ // Driver####, consider use some macro to abstract the code\r
+ //\r
+ //\r
+ // Get the option attribute\r
+ //\r
+ TempPtr = Variable;\r
+ Attribute = *(UINT32 *) Variable;\r
+ TempPtr += sizeof (UINT32);\r
+\r
+ //\r
+ // Get the option's device path size\r
+ //\r
+ FilePathSize = *(UINT16 *) TempPtr;\r
+ TempPtr += sizeof (UINT16);\r
+\r
+ //\r
+ // Get the option's description string\r
+ //\r
+ Description = (CHAR16 *) TempPtr;\r
+\r
+ //\r
+ // Get the option's description string size\r
+ //\r
+ TempPtr += StrSize ((CHAR16 *) TempPtr);\r
+\r
+ //\r
+ // Get the option's device path\r
+ //\r
+ DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
+ TempPtr += FilePathSize;\r
+\r
+ LoadOptions = TempPtr;\r
+ LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable));\r
+\r
+ //\r
+ // The Console variables may have multiple device paths, so make\r
+ // an Entry for each one.\r
+ //\r
+ Option = AllocateZeroPool (sizeof (BDS_COMMON_OPTION));\r
+ if (Option == NULL) {\r
+ return NULL;\r
+ }\r
+\r
+ Option->Signature = BDS_LOAD_OPTION_SIGNATURE;\r
+ Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));\r
+ CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r
+ Option->Attribute = Attribute;\r
+ Option->Description = AllocateZeroPool (StrSize (Description));\r
+ CopyMem (Option->Description, Description, StrSize (Description));\r
+ Option->LoadOptions = AllocateZeroPool (LoadOptionsSize);\r
+ CopyMem (Option->LoadOptions, LoadOptions, LoadOptionsSize);\r
+ Option->LoadOptionsSize = LoadOptionsSize;\r
+\r
+ //\r
+ // Get the value from VariableName Unicode string\r
+ // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this\r
+ // Unicode stream to ASCII without any loss in meaning.\r
+ //\r
+ if (*VariableName == 'B') {\r
+ NumOff = sizeof (L"Boot")/sizeof(CHAR16) -1 ;\r
+ Option->BootCurrent = (UINT16) ((VariableName[NumOff] -'0') * 0x1000);\r
+ Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+1]-'0') * 0x100));\r
+ Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+2]-'0') * 0x10));\r
+ Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+3]-'0')));\r
+ }\r
+ //\r
+ // Insert active entry to BdsDeviceList\r
+ //\r
+ if ((Option->Attribute & LOAD_OPTION_ACTIVE) == LOAD_OPTION_ACTIVE) {\r
+ InsertTailList (BdsCommonOptionList, &Option->Link);\r
+ FreePool (Variable);\r
+ return Option;\r
+ }\r
+\r
+ FreePool (Variable);\r
+ FreePool (Option);\r
+ return NULL;\r
+\r
+}\r
+\r
+/**\r
+ Process BootOrder, or DriverOrder variables, by calling\r
+ BdsLibVariableToOption () for each UINT16 in the variables.\r
+\r
+ @param BdsCommonOptionList The header of the option list base on variable\r
+ VariableName\r
+ @param VariableName EFI Variable name indicate the BootOrder or\r
+ DriverOrder\r
+\r
+ @retval EFI_SUCCESS Success create the boot option or driver option\r
+ list\r
+ @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+BdsLibBuildOptionFromVar (\r
+ IN LIST_ENTRY *BdsCommonOptionList,\r
+ IN CHAR16 *VariableName\r
+ )\r
+{\r
+ UINT16 *OptionOrder;\r
+ UINTN OptionOrderSize;\r
+ UINTN Index;\r
+ BDS_COMMON_OPTION *Option;\r
+ CHAR16 OptionName[20];\r
+\r
+ //\r
+ // Zero Buffer in order to get all BOOT#### variables\r
+ //\r
+ ZeroMem (OptionName, sizeof (OptionName));\r
+\r
+ //\r
+ // Read the BootOrder, or DriverOrder variable.\r
+ //\r
+ OptionOrder = BdsLibGetVariableAndSize (\r
+ VariableName,\r
+ &gEfiGlobalVariableGuid,\r
+ &OptionOrderSize\r
+ );\r
+ if (OptionOrder == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ for (Index = 0; Index < OptionOrderSize / sizeof (UINT16); Index++) {\r
+ if (*VariableName == 'B') {\r
+ UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionOrder[Index]);\r
+ } else {\r
+ UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", OptionOrder[Index]);\r
+ }\r
+\r
+ Option = BdsLibVariableToOption (BdsCommonOptionList, OptionName);\r
+ Option->BootCurrent = OptionOrder[Index];\r
+\r
+ }\r
+\r
+ FreePool (OptionOrder);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Get boot mode by looking up configuration table and parsing HOB list\r
+\r
+ @param BootMode Boot mode from PEI handoff HOB.\r
+\r
+ @retval EFI_SUCCESS Successfully get boot mode\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+BdsLibGetBootMode (\r
+ OUT EFI_BOOT_MODE *BootMode\r
+ )\r
+{\r
+ *BootMode = GetBootModeHob ();\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Read the EFI variable (VendorGuid/Name) and return a dynamically allocated\r
+ buffer, and the size of the buffer. If failure return NULL.\r
+\r
+ @param Name String part of EFI variable name\r
+ @param VendorGuid GUID part of EFI variable name\r
+ @param VariableSize Returns the size of the EFI variable that was read\r
+\r
+ @return Dynamically allocated memory that contains a copy of the EFI variable.\r
+ @return Caller is responsible freeing the buffer.\r
+ @retval NULL Variable was not read\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+BdsLibGetVariableAndSize (\r
+ IN CHAR16 *Name,\r
+ IN EFI_GUID *VendorGuid,\r
+ OUT UINTN *VariableSize\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN BufferSize;\r
+ VOID *Buffer;\r
+\r
+ Buffer = NULL;\r
+\r
+ //\r
+ // Pass in a zero size buffer to find the required buffer size.\r
+ //\r
+ BufferSize = 0;\r
+ Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);\r
+ if (Status == EFI_BUFFER_TOO_SMALL) {\r
+ //\r
+ // Allocate the buffer to return\r
+ //\r
+ Buffer = AllocateZeroPool (BufferSize);\r
+ if (Buffer == NULL) {\r
+ return NULL;\r
+ }\r
+ //\r
+ // Read variable into the allocated buffer.\r
+ //\r
+ Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);\r
+ if (EFI_ERROR (Status)) {\r
+ BufferSize = 0;\r
+ }\r
+ }\r
+\r
+ *VariableSize = BufferSize;\r
+ return Buffer;\r
+}\r
+\r
+/**\r
+ Delete the instance in Multi which matches partly with Single instance\r
+\r
+ @param Multi A pointer to a multi-instance device path data\r
+ structure.\r
+ @param Single A pointer to a single-instance device path data\r
+ structure.\r
+\r
+ @return This function will remove the device path instances in Multi which partly\r
+ match with the Single, and return the result device path. If there is no\r
+ remaining device path as a result, this function will return NULL.\r
+\r
+**/\r
+EFI_DEVICE_PATH_PROTOCOL *\r
+EFIAPI\r
+BdsLibDelPartMatchInstance (\r
+ IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
+ IN EFI_DEVICE_PATH_PROTOCOL *Single\r
+ )\r
+{\r
+ EFI_DEVICE_PATH_PROTOCOL *Instance;\r
+ EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
+ EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r
+ UINTN InstanceSize;\r
+ UINTN SingleDpSize;\r
+ UINTN Size;\r
+\r
+ NewDevicePath = NULL;\r
+ TempNewDevicePath = NULL;\r
+\r
+ if (Multi == NULL || Single == NULL) {\r
+ return Multi;\r
+ }\r
+\r
+ Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
+ SingleDpSize = GetDevicePathSize (Single) - END_DEVICE_PATH_LENGTH;\r
+ InstanceSize -= END_DEVICE_PATH_LENGTH;\r
+\r
+ while (Instance != NULL) {\r
+\r
+ Size = (SingleDpSize < InstanceSize) ? SingleDpSize : InstanceSize;\r
+\r
+ if ((CompareMem (Instance, Single, Size) != 0)) {\r
+ //\r
+ // Append the device path instance which does not match with Single\r
+ //\r
+ TempNewDevicePath = NewDevicePath;\r
+ NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);\r
+ if (TempNewDevicePath != NULL) {\r
+ FreePool(TempNewDevicePath);\r
+ }\r
+ }\r
+ FreePool(Instance);\r
+ Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
+ InstanceSize -= END_DEVICE_PATH_LENGTH;\r
+ }\r
+\r
+ return NewDevicePath;\r
+}\r
+\r
+/**\r
+ Function compares a device path data structure to that of all the nodes of a\r
+ second device path instance.\r
+\r
+ @param Multi A pointer to a multi-instance device path data\r
+ structure.\r
+ @param Single A pointer to a single-instance device path data\r
+ structure.\r
+\r
+ @retval TRUE If the Single is contained within Multi\r
+ @retval FALSE The Single is not match within Multi\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+BdsLibMatchDevicePaths (\r
+ IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
+ IN EFI_DEVICE_PATH_PROTOCOL *Single\r
+ )\r
+{\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;\r
+ UINTN Size;\r
+\r
+ if (Multi == NULL || Single == NULL) {\r
+ return FALSE;\r
+ }\r
+\r
+ DevicePath = Multi;\r
+ DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
+\r
+ //\r
+ // Search for the match of 'Single' in 'Multi'\r
+ //\r
+ while (DevicePathInst != NULL) {\r
+ //\r
+ // If the single device path is found in multiple device paths,\r
+ // return success\r
+ //\r
+ if (CompareMem (Single, DevicePathInst, Size) == 0) {\r
+ FreePool (DevicePathInst);\r
+ return TRUE;\r
+ }\r
+\r
+ FreePool (DevicePathInst);\r
+ DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
+ }\r
+\r
+ return FALSE;\r
+}\r
+\r
+/**\r
+ This function prints a series of strings.\r
+\r
+ @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL\r
+ @param ... A variable argument list containing series of\r
+ strings, the last string must be NULL.\r
+\r
+ @retval EFI_SUCCESS Success print out the string using ConOut.\r
+ @retval EFI_STATUS Return the status of the ConOut->OutputString ().\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+BdsLibOutputStrings (\r
+ IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut,\r
+ ...\r
+ )\r
+{\r
+ VA_LIST Args;\r
+ EFI_STATUS Status;\r
+ CHAR16 *String;\r
+\r
+ Status = EFI_SUCCESS;\r
+ VA_START (Args, ConOut);\r
+\r
+ while (!EFI_ERROR (Status)) {\r
+ //\r
+ // If String is NULL, then it's the end of the list\r
+ //\r
+ String = VA_ARG (Args, CHAR16 *);\r
+ if (String != NULL) {\r
+ break;\r
+ }\r
+\r
+ Status = ConOut->OutputString (ConOut, String);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+//\r
+// Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.\r
+// Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if\r
+// user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.\r
+//\r
+\r
+\r
+/**\r
+ Enable the setup browser reset reminder feature.\r
+ This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+EnableResetReminderFeature (\r
+ VOID\r
+ )\r
+{\r
+ mFeaturerSwitch = TRUE;\r
+}\r
+\r
+\r
+/**\r
+ Disable the setup browser reset reminder feature.\r
+ This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+DisableResetReminderFeature (\r
+ VOID\r
+ )\r
+{\r
+ mFeaturerSwitch = FALSE;\r
+}\r
+\r
+\r
+/**\r
+ Record the info that a reset is required.\r
+ A module boolean variable is used to record whether a reset is required.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+EnableResetRequired (\r
+ VOID\r
+ )\r
+{\r
+ mResetRequired = TRUE;\r
+}\r
+\r
+\r
+/**\r
+ Record the info that no reset is required.\r
+ A module boolean variable is used to record whether a reset is required.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+DisableResetRequired (\r
+ VOID\r
+ )\r
+{\r
+ mResetRequired = FALSE;\r
+}\r
+\r
+\r
+/**\r
+ Check whether platform policy enable the reset reminder feature. The default is enabled.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsResetReminderFeatureEnable (\r
+ VOID\r
+ )\r
+{\r
+ return mFeaturerSwitch;\r
+}\r
+\r
+\r
+/**\r
+ Check if user changed any option setting which needs a system reset to be effective.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsResetRequired (\r
+ VOID\r
+ )\r
+{\r
+ return mResetRequired;\r
+}\r
+\r
+\r
+/**\r
+ Check whether a reset is needed, and finish the reset reminder feature.\r
+ If a reset is needed, Popup a menu to notice user, and finish the feature\r
+ according to the user selection.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+SetupResetReminder (\r
+ VOID\r
+ )\r
+{\r
+ EFI_INPUT_KEY Key;\r
+ CHAR16 *StringBuffer1;\r
+ CHAR16 *StringBuffer2;\r
+\r
+\r
+ //\r
+ //check any reset required change is applied? if yes, reset system\r
+ //\r
+ if (IsResetReminderFeatureEnable ()) {\r
+ if (IsResetRequired ()) {\r
+\r
+ StringBuffer1 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));\r
+ ASSERT (StringBuffer1 != NULL);\r
+ StringBuffer2 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));\r
+ ASSERT (StringBuffer2 != NULL);\r
+ StrCpy (StringBuffer1, L"Configuration changed. Reset to apply it Now ? ");\r
+ StrCpy (StringBuffer2, L"Enter (YES) / Esc (NO)");\r
+ //\r
+ // Popup a menu to notice user\r
+ //\r
+ do {\r
+ IfrLibCreatePopUp (2, &Key, StringBuffer1, StringBuffer2);\r
+ } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
+\r
+ FreePool (StringBuffer1);\r
+ FreePool (StringBuffer2);\r
+ //\r
+ // If the user hits the YES Response key, reset\r
+ //\r
+ if ((Key.UnicodeChar == CHAR_CARRIAGE_RETURN)) {\r
+ gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r
+ }\r
+ gST->ConOut->ClearScreen (gST->ConOut);\r
+ }\r
+ }\r
+}\r
+\r
+/**\r
+ Get the headers (dos, image, optional header) from an image.\r
+\r
+ @param Device SimpleFileSystem device handle\r
+ @param FileName File name for the image\r
+ @param DosHeader Pointer to dos header\r
+ @param Hdr Pointer to optional header\r
+\r
+ @retval EFI_SUCCESS Successfully get the machine type.\r
+ @retval EFI_NOT_FOUND The file is not found.\r
+ @retval EFI_LOAD_ERROR File is not a valid image file.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+BdsLibGetImageHeader (\r
+ IN EFI_HANDLE Device,\r
+ IN CHAR16 *FileName,\r
+ OUT EFI_IMAGE_DOS_HEADER *DosHeader,\r
+ OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
+ EFI_FILE_HANDLE Root;\r
+ EFI_FILE_HANDLE ThisFile;\r
+ UINTN BufferSize;\r
+ UINT64 FileSize;\r
+ EFI_FILE_INFO *Info;\r
+\r
+ Root = NULL;\r
+ ThisFile = NULL;\r
+ //\r
+ // Handle the file system interface to the device\r
+ //\r
+ Status = gBS->HandleProtocol (\r
+ Device,\r
+ &gEfiSimpleFileSystemProtocolGuid,\r
+ (VOID *) &Volume\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ Status = Volume->OpenVolume (\r
+ Volume,\r
+ &Root\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ Root = NULL;\r
+ goto Done;\r
+ }\r
+\r
+ Status = Root->Open (Root, &ThisFile, FileName, EFI_FILE_MODE_READ, 0);\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Get file size\r
+ //\r
+ BufferSize = SIZE_OF_EFI_FILE_INFO + 200;\r
+ do {\r
+ Info = NULL;\r
+ Status = gBS->AllocatePool (EfiBootServicesData, BufferSize, (VOID **) &Info);\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+ Status = ThisFile->GetInfo (\r
+ ThisFile,\r
+ &gEfiFileInfoGuid,\r
+ &BufferSize,\r
+ Info\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ break;\r
+ }\r
+ if (Status != EFI_BUFFER_TOO_SMALL) {\r
+ FreePool (Info);\r
+ goto Done;\r
+ }\r
+ FreePool (Info);\r
+ } while (TRUE);\r
+\r
+ FileSize = Info->FileSize;\r
+ FreePool (Info);\r
+\r
+ //\r
+ // Read dos header\r
+ //\r
+ BufferSize = sizeof (EFI_IMAGE_DOS_HEADER);\r
+ Status = ThisFile->Read (ThisFile, &BufferSize, DosHeader);\r
+ if (EFI_ERROR (Status) ||\r
+ BufferSize < sizeof (EFI_IMAGE_DOS_HEADER) ||\r
+ FileSize <= DosHeader->e_lfanew ||\r
+ DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {\r
+ Status = EFI_LOAD_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Move to PE signature\r
+ //\r
+ Status = ThisFile->SetPosition (ThisFile, DosHeader->e_lfanew);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_LOAD_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Read and check PE signature\r
+ //\r
+ BufferSize = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);\r
+ Status = ThisFile->Read (ThisFile, &BufferSize, Hdr.Pe32);\r
+ if (EFI_ERROR (Status) ||\r
+ BufferSize < sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION) ||\r
+ Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
+ Status = EFI_LOAD_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Check PE32 or PE32+ magic\r
+ //\r
+ if (Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC &&\r
+ Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
+ Status = EFI_LOAD_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ Done:\r
+ if (ThisFile != NULL) {\r
+ ThisFile->Close (ThisFile);\r
+ }\r
+ if (Root != NULL) {\r
+ Root->Close (Root);\r
+ }\r
+ return Status;\r
+}\r
+\r
+/**\r
+\r
+ This routine is a notification function for legayc boot or exit boot\r
+ service event. It will adjust the memory information for different\r
+ memory type and save them into the variables for next boot.\r
+\r
+\r
+ @param Event The event that triggered this notification function.\r
+ @param Context Pointer to the notification functions context.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+BdsSetMemoryTypeInformationVariable (\r
+ EFI_EVENT Event,\r
+ VOID *Context\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;\r
+ EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;\r
+ UINTN VariableSize;\r
+ BOOLEAN UpdateRequired;\r
+ UINTN Index;\r
+ UINTN Index1;\r
+ UINT32 Previous;\r
+ UINT32 Current;\r
+ UINT32 Next;\r
+ EFI_HOB_GUID_TYPE *GuidHob;\r
+\r
+ UpdateRequired = FALSE;\r
+\r
+ //\r
+ // Retrieve the current memory usage statistics. If they are not found, then\r
+ // no adjustments can be made to the Memory Type Information variable.\r
+ //\r
+ Status = EfiGetSystemConfigurationTable (\r
+ &gEfiMemoryTypeInformationGuid,\r
+ (VOID **) &CurrentMemoryTypeInformation\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return;\r
+ }\r
+\r
+ //\r
+ // Get the Memory Type Information settings from Hob if they exist,\r
+ // PEI is responsible for getting them from variable and build a Hob to save them.\r
+ // If the previous Memory Type Information is not available, then set defaults\r
+ //\r
+ GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);\r
+ if (GuidHob == NULL) {\r
+ //\r
+ // If Platform has not built Memory Type Info into the Hob, just return.\r
+ //\r
+ return;\r
+ }\r
+ PreviousMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);\r
+ VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);\r
+\r
+ //\r
+ // Use a heuristic to adjust the Memory Type Information for the next boot\r
+ //\r
+ for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
+\r
+ Current = 0;\r
+ for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {\r
+ if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {\r
+ Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {\r
+ continue;\r
+ }\r
+\r
+ Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;\r
+\r
+ //\r
+ // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail\r
+ //\r
+ if (Current > Previous) {\r
+ Next = Current + (Current >> 2);\r
+ } else {\r
+ Next = Previous;\r
+ }\r
+ if (Next > 0 && Next < 4) {\r
+ Next = 4;\r
+ }\r
+\r
+ if (Next != Previous) {\r
+ PreviousMemoryTypeInformation[Index].NumberOfPages = Next;\r
+ UpdateRequired = TRUE;\r
+ }\r
+\r
+ }\r
+\r
+ //\r
+ // If any changes were made to the Memory Type Information settings, then set the new variable value\r
+ //\r
+ if (UpdateRequired) {\r
+ Status = gRT->SetVariable (\r
+ EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
+ &gEfiMemoryTypeInformationGuid,\r
+ EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+ VariableSize,\r
+ PreviousMemoryTypeInformation\r
+ );\r
+ }\r
+\r
+ return;\r
+}\r
+\r
+/**\r
+ This routine register a function to adjust the different type memory page number just before booting\r
+ and save the updated info into the variable for next boot to use.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+BdsLibSaveMemoryTypeInformation (\r
+ VOID\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_EVENT ReadyToBootEvent;\r
+\r
+ Status = EfiCreateEventReadyToBootEx (\r
+ TPL_CALLBACK,\r
+ BdsSetMemoryTypeInformationVariable,\r
+ NULL,\r
+ &ReadyToBootEvent\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR,"Bds Set Memory Type Informationa Variable Fails\n"));\r
+ }\r
+\r
+}\r
+\r
+\r