/** @file
;******************************************************************************
;* Copyright (c) 2020, Insyde Software Corp. All Rights Reserved.
;*
;* You may not reproduce, distribute, publish, display, perform, modify, adapt,
;* transmit, broadcast, present, recite, release, license or otherwise exploit
;* any part of this publication in any form, by any means, without the prior
;* written permission of Insyde Software Corporation.
;*
;******************************************************************************
*/
//
// This file contains 'Framework Code' and is licensed as such
// under the terms of your license agreement with Intel or your
// vendor. This file may not be modified, except as allowed by
// additional terms of your license agreement.
//
/** @file
Misc BDS library function
Copyright (c) 2011 - 2020, Intel Corporation. All rights reserved.
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.
**/
#include "InternalBdsLib.h"
#include
#include
/**
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
Caller is responsible freeing the buffer.
@retval NULL Variable was not read
**/
VOID *
EFIAPI
EfiBootManagerGetVariableAndSize (
IN CHAR16 *Name,
IN EFI_GUID *VendorGuid,
OUT UINTN *VariableSize
)
{
EFI_STATUS Status;
UINTN BufferSize;
VOID *Buffer;
Buffer = NULL;
BufferSize = 0;
//
// Pass in a zero size buffer to find the required buffer size.
//
Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
if (Status == EFI_BUFFER_TOO_SMALL) {
//
// Allocate the buffer to return
//
Buffer = AllocatePool (BufferSize);
ASSERT (Buffer != NULL);
//
// ROYAL_PARK_OVERRIDE: RoyalParkOverrideBegin - RPCO-0031
//
if (Buffer == NULL) {
*VariableSize = 0;
return NULL;
}
//
// ROYAL_PARK_OVERRIDE: RoyalParkOverrideEnd
//
//
// Read variable into the allocated buffer.
//
Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
if (EFI_ERROR (Status)) {
FreePool (Buffer);
BufferSize = 0;
Buffer = NULL;
}
}
ASSERT (((Buffer == NULL) && (BufferSize == 0)) ||
((Buffer != NULL) && (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
match with the Single, and return the result device path. If there is no
remaining device path as a result, this function will return NULL.
**/
EFI_DEVICE_PATH_PROTOCOL *
EFIAPI
EfiBootManagerDelPartMatchInstance (
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;
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) {
if (CompareMem (Instance, Single, MIN (SingleDpSize, InstanceSize)) != 0) {
//
// Append the device path instance which does not match with Single
//
TempNewDevicePath = NewDevicePath;
NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);
if (TempNewDevicePath != NULL) {
FreePool(TempNewDevicePath);
}
}
FreePool(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 device path is contained within Multi device path.
@retval FALSE The Single device path is not match within Multi device path.
**/
BOOLEAN
EFIAPI
EfiBootManagerMatchDevicePaths (
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 == NULL || Single == NULL) {
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) {
FreePool (DevicePathInst);
return TRUE;
}
FreePool (DevicePathInst);
DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
}
return FALSE;
}
/**
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 Hdr The buffer in which to return the PE32, PE32+, or TE 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
GetImageHeader (
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)) {
Root = NULL;
goto Done;
}
ASSERT (Root != NULL);
//
// ROYAL_PARK_OVERRIDE: RoyalParkOverrideBegin - RPCO-0031
//
if (Root == NULL) {
goto Done;
}
//
// ROYAL_PARK_OVERRIDE: RoyalParkOverrideEnd
//
Status = Root->Open (Root, &ThisFile, FileName, EFI_FILE_MODE_READ, 0);
if (EFI_ERROR (Status)) {
goto Done;
}
ASSERT (ThisFile != NULL);
//
// ROYAL_PARK_OVERRIDE: RoyalParkOverrideBegin - RPCO-0031
//
if (ThisFile == NULL) {
goto Done;
}
//
// ROYAL_PARK_OVERRIDE: RoyalParkOverrideEnd
//
//
// 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) {
FreePool (Info);
goto Done;
}
FreePool (Info);
} while (TRUE);
FileSize = Info->FileSize;
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;
}
/**
This routine adjust the memory information for different memory type and
save them into the variables for next boot.
**/
VOID
SetMemoryTypeInformationVariable (
VOID
)
{
EFI_STATUS Status;
EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;
EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;
UINTN VariableSize;
UINTN Index;
UINTN Index1;
UINT32 Previous;
UINT32 Current;
UINT32 Next;
EFI_HOB_GUID_TYPE *GuidHob;
BOOLEAN MemoryTypeInformationModified;
BOOLEAN MemoryTypeInformationVariableExists;
EFI_BOOT_MODE BootMode;
MemoryTypeInformationModified = FALSE;
MemoryTypeInformationVariableExists = FALSE;
BootMode = GetBootModeHob ();
//
// In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.
//
if (BootMode == BOOT_IN_RECOVERY_MODE) {
return;
}
//
// Only check the the Memory Type Information variable in the boot mode
// other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type
// Information is not valid in this boot mode.
//
if (BootMode != BOOT_WITH_DEFAULT_SETTINGS) {
VariableSize = 0;
Status = gRT->GetVariable (
EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
&gEfiMemoryTypeInformationGuid,
NULL,
&VariableSize,
NULL
);
if (Status == EFI_BUFFER_TOO_SMALL) {
MemoryTypeInformationVariableExists = TRUE;
}
}
//
// 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) || CurrentMemoryTypeInformation == NULL) {
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
//
GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);
if (GuidHob == NULL) {
//
// If Platform has not built Memory Type Info into the Hob, just return.
//
return;
}
PreviousMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);
VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
//
// Use a heuristic to adjust the Memory Type Information for the next boot
//
DEBUG ((DEBUG_INFO, "Memory Previous Current Next \n"));
DEBUG ((DEBUG_INFO, " Type Pages Pages Pages \n"));
DEBUG ((DEBUG_INFO, "====== ======== ======== ========\n"));
for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {
for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {
if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {
break;
}
}
if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {
continue;
}
//
// Previous is the number of pages pre-allocated
// Current is the number of pages actually needed
//
Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;
Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;
Next = Previous;
//
// Inconsistent Memory Reserved across bootings may lead to S4 fail
// Write next varible to 125% * current when the pre-allocated memory is:
// 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING
// 2. Less than the needed memory
//
if ((Current + (Current >> 1)) < Previous) {
if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
Next = Current + (Current >> 2);
}
} else if (Current > Previous) {
Next = Current + (Current >> 2);
}
if (Next > 0 && Next < 4) {
Next = 4;
}
if (Next != Previous) {
PreviousMemoryTypeInformation[Index].NumberOfPages = Next;
MemoryTypeInformationModified = TRUE;
}
DEBUG ((DEBUG_INFO, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation[Index].Type, Previous, Current, Next));
}
//
// If any changes were made to the Memory Type Information settings, then set the new variable value;
// Or create the variable in first boot.
//
if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) {
Status = SetVariableAndReportStatusCodeOnError (
EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
&gEfiMemoryTypeInformationGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
VariableSize,
PreviousMemoryTypeInformation
);
if (!EFI_ERROR (Status)) {
//
// If the Memory Type Information settings have been modified, then reset the platform
// so the new Memory Type Information setting will be used to guarantee that an S4
// entry/resume cycle will not fail.
//
if (MemoryTypeInformationModified) {
DEBUG ((DEBUG_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));
gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
}
} else {
DEBUG ((DEBUG_ERROR, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));
}
}
}
/**
Set the variable and report the error through status code upon failure.
@param VariableName A Null-terminated string that is the name of the vendor's variable.
Each VariableName is unique for each VendorGuid. VariableName must
contain 1 or more characters. If VariableName is an empty string,
then EFI_INVALID_PARAMETER is returned.
@param VendorGuid A unique identifier for the vendor.
@param Attributes Attributes bitmask to set for the variable.
@param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE,
EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or
EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero
causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is
set, then a SetVariable() call with a DataSize of zero will not cause any change to
the variable value (the timestamp associated with the variable may be updated however
even if no new data value is provided,see the description of the
EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not
be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated).
@param Data The contents for the variable.
@retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
defined by the Attributes.
@retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the
DataSize exceeds the maximum allowed.
@retval EFI_INVALID_PARAMETER VariableName is an empty string.
@retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
@retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
@retval EFI_WRITE_PROTECTED The variable in question is read-only.
@retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
@retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo
does NOT pass the validation check carried out by the firmware.
@retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
**/
EFI_STATUS
SetVariableAndReportStatusCodeOnError (
IN CHAR16 *VariableName,
IN EFI_GUID *VendorGuid,
IN UINT32 Attributes,
IN UINTN DataSize,
IN VOID *Data
)
{
EFI_STATUS Status;
EDKII_SET_VARIABLE_STATUS *SetVariableStatus;
UINTN NameSize;
Status = gRT->SetVariable (
VariableName,
VendorGuid,
Attributes,
DataSize,
Data
);
if (EFI_ERROR (Status)) {
NameSize = StrSize (VariableName);
SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);
if (SetVariableStatus != NULL) {
CopyGuid (&SetVariableStatus->Guid, VendorGuid);
SetVariableStatus->NameSize = NameSize;
SetVariableStatus->DataSize = DataSize;
SetVariableStatus->SetStatus = Status;
SetVariableStatus->Attributes = Attributes;
CopyMem (SetVariableStatus + 1, VariableName, NameSize);
CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data, DataSize);
REPORT_STATUS_CODE_EX (
EFI_ERROR_CODE,
PcdGet32 (PcdErrorCodeSetVariable),
0,
NULL,
&gEdkiiStatusCodeDataTypeVariableGuid,
SetVariableStatus,
sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize
);
FreePool (SetVariableStatus);
}
}
return Status;
}
/**
Query all the children of VideoController and return the device paths of all the
children that support GraphicsOutput protocol.
@param VideoController PCI handle of video controller.
@return Device paths of all the children that support GraphicsOutput protocol.
**/
EFI_DEVICE_PATH_PROTOCOL *
EFIAPI
EfiBootManagerGetGopDevicePath (
IN EFI_HANDLE VideoController
)
{
UINTN Index;
EFI_STATUS Status;
EFI_GUID **ProtocolBuffer;
UINTN ProtocolBufferCount;
UINTN ProtocolIndex;
EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;
UINTN EntryCount;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
EFI_DEVICE_PATH_PROTOCOL *Next;
EFI_DEVICE_PATH_PROTOCOL *Previous;
EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
EFI_DEVICE_PATH_PROTOCOL *GopPool;
EFI_DEVICE_PATH_PROTOCOL *ReturnDevicePath;
Status = gBS->ProtocolsPerHandle (
VideoController,
&ProtocolBuffer,
&ProtocolBufferCount
);
if (EFI_ERROR (Status)) {
return NULL;
}
GopPool = NULL;
for (ProtocolIndex = 0; ProtocolIndex < ProtocolBufferCount; ProtocolIndex++) {
Status = gBS->OpenProtocolInformation (
VideoController,
ProtocolBuffer[ProtocolIndex],
&OpenInfoBuffer,
&EntryCount
);
if (EFI_ERROR (Status)) {
continue;
}
for (Index = 0; Index < EntryCount; Index++) {
//
// Query all the children
//
if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
Status = gBS->OpenProtocol (
OpenInfoBuffer[Index].ControllerHandle,
&gEfiDevicePathProtocolGuid,
(VOID **) &DevicePath,
NULL,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
continue;
}
Previous = NULL;
for (Next = DevicePath; !IsDevicePathEnd (Next); Next = NextDevicePathNode (Next)) {
Previous = Next;
}
ASSERT (Previous != NULL);
if (DevicePathType (Previous) == ACPI_DEVICE_PATH && DevicePathSubType (Previous) == ACPI_ADR_DP) {
Status = gBS->OpenProtocol (
OpenInfoBuffer[Index].ControllerHandle,
&gEfiGraphicsOutputProtocolGuid,
NULL,
NULL,
NULL,
EFI_OPEN_PROTOCOL_TEST_PROTOCOL
);
if (!EFI_ERROR (Status)) {
//
// Append the device path to GOP pool when there is GOP protocol installed.
//
TempDevicePath = GopPool;
GopPool = AppendDevicePathInstance (GopPool, DevicePath);
gBS->FreePool (TempDevicePath);
}
}
if (DevicePathType (Previous) == HARDWARE_DEVICE_PATH && DevicePathSubType (Previous) == HW_CONTROLLER_DP) {
//
// Recursively look for GOP child in this frame buffer handle
//
DEBUG ((DEBUG_INFO, "[Bds] Looking for GOP child deeper ... \n"));
TempDevicePath = GopPool;
ReturnDevicePath = EfiBootManagerGetGopDevicePath (OpenInfoBuffer[Index].ControllerHandle);
GopPool = AppendDevicePathInstance (GopPool, ReturnDevicePath);
gBS->FreePool (ReturnDevicePath);
gBS->FreePool (TempDevicePath);
}
}
}
FreePool (OpenInfoBuffer);
}
FreePool (ProtocolBuffer);
return GopPool;
}
/**
Dispatch the deferred images that are returned from all DeferredImageLoad instances.
@retval EFI_SUCCESS At least one deferred image is loaded successfully and started.
@retval EFI_NOT_FOUND There is no deferred image.
@retval EFI_ACCESS_DENIED There are deferred images but all of them are failed to load.
**/
EFI_STATUS
EFIAPI
EfiBootManagerDispatchDeferredImages (
VOID
)
{
EFI_STATUS Status;
EFI_DEFERRED_IMAGE_LOAD_PROTOCOL *DeferredImage;
UINTN HandleCount;
EFI_HANDLE *Handles;
UINTN Index;
UINTN ImageIndex;
EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;
EFI_DEVICE_PATH_PROTOCOL *DuplicatePath;
VOID *Image;
UINTN ImageSize;
BOOLEAN BootOption;
EFI_HANDLE ImageHandle;
UINTN ExitDataSize;
CHAR16 *ExitData;
UINTN ImageCount;
UINTN LoadCount;
EFI_BOOT_MODE BootMode;
UINTN Size;
UINT64 OsIndications;
EFI_HANDLE Handle;
PCI_TYPE00 PciType;
EFI_PCI_IO_PROTOCOL *PciIo;
//
// Find all the deferred image load protocols.
//
HandleCount = 0;
Handles = NULL;
Status = gBS->LocateHandleBuffer (
ByProtocol,
&gEfiDeferredImageLoadProtocolGuid,
NULL,
&HandleCount,
&Handles
);
if (EFI_ERROR (Status)) {
return EFI_NOT_FOUND;
}
ImageCount = 0;
LoadCount = 0;
//
// Check Fast Recovery and Capsule enable or not.
//
Size = sizeof(UINT64);
OsIndications = 0;
Status = gRT->GetVariable (
L"OsIndications",
&gEfiGlobalVariableGuid,
NULL,
&Size,
&OsIndications
);
BootMode = GetBootModeHob ();
for (Index = 0; Index < HandleCount; Index++) {
Status = gBS->HandleProtocol (Handles[Index], &gEfiDeferredImageLoadProtocolGuid, (VOID **) &DeferredImage);
if (EFI_ERROR (Status)) {
continue;
}
for (ImageIndex = 0; ;ImageIndex++) {
//
// Load all the deferred images in this protocol instance.
//
Status = DeferredImage->GetImageInfo (
DeferredImage,
ImageIndex,
&ImageDevicePath,
(VOID **) &Image,
&ImageSize,
&BootOption
);
if (EFI_ERROR (Status)) {
break;
}
if (((BootMode) == BOOT_ON_FLASH_UPDATE || (BootMode) == BOOT_IN_RECOVERY_MODE ) ||
(!EFI_ERROR (Status) && (OsIndications & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED))) {
//
// It only allowed VGA rom of 3rd party can be processed during rcovery mode or capsule enabled.
//
DuplicatePath = DuplicateDevicePath (ImageDevicePath);
Status = gBS->LocateDevicePath (
&gEfiPciIoProtocolGuid,
&DuplicatePath,
&Handle
);
if (EFI_ERROR (Status)) {
return Status;
}
Status = gBS->HandleProtocol (
Handle,
&gEfiPciIoProtocolGuid,
(VOID **) &PciIo
);
Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, 0, sizeof (PciType) / sizeof (UINT32), &PciType);
if (!IS_PCI_VGA (&PciType)) {
continue;
}
}
ImageCount++;
//
// Load and start the image.
//
Status = gBS->LoadImage (
BootOption,
gImageHandle,
ImageDevicePath,
NULL,
0,
&ImageHandle
);
if (!EFI_ERROR (Status)) {
LoadCount++;
//
// Before calling the image, enable the Watchdog Timer for
// a 5 Minute period
//
gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);
Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);
if (ExitData != NULL) {
FreePool (ExitData);
}
//
// Clear the Watchdog Timer after the image returns.
//
gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);
}
}
}
if (Handles != NULL) {
FreePool (Handles);
}
if (ImageCount == 0) {
return EFI_NOT_FOUND;
} else {
if (LoadCount == 0) {
return EFI_ACCESS_DENIED;
} else {
return EFI_SUCCESS;
}
}
}