IntelSiliconPkg/SpiFvbService: Add support for VariableFlashInfoLib

REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3478

Adds support for getting the variable flash information from
VariableFlashInfoLib. This library abstracts the source of flash
information so platforms could expect the information to come
from a different source in the library implementation than the
PCDs previously used as the information source in this module.

In particular, the library allows Standalone MM platforms to
dynamically pass the information behind the library API.

Cc: Rangasai V Chaganty <rangasai.v.chaganty@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Isaac Oram <isaac.w.oram@intel.com>
Cc: Nate DeSimone <nathaniel.l.desimone@intel.com>
Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
Reviewed-by: Sai Chaganty <rangasai.v.chaganty@intel.com>
Reviewed-by: Isaac Oram <isaac.w.oram@intel.com>
This commit is contained in:
Michael Kubacki 2022-07-01 07:39:43 -07:00 коммит произвёл Ken Lautner
Родитель b7bd9c41f6
Коммит 81e0246c61
7 изменённых файлов: 243 добавлений и 64 удалений

Просмотреть файл

@ -3,6 +3,7 @@
These data is intent to decouple FVB driver with FV header.
Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
Copyright (c) Microsoft Corporation.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
@ -11,51 +12,92 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#define FIRMWARE_BLOCK_SIZE 0x10000
#define FVB_MEDIA_BLOCK_SIZE FIRMWARE_BLOCK_SIZE
#define NV_STORAGE_BASE_ADDRESS FixedPcdGet32(PcdFlashNvStorageVariableBase)
#define SYSTEM_NV_BLOCK_NUM ((FixedPcdGet32(PcdFlashNvStorageVariableSize)+ FixedPcdGet32(PcdFlashNvStorageFtwWorkingSize) + FixedPcdGet32(PcdFlashNvStorageFtwSpareSize))/ FVB_MEDIA_BLOCK_SIZE)
typedef struct {
EFI_PHYSICAL_ADDRESS BaseAddress;
EFI_FIRMWARE_VOLUME_HEADER FvbInfo;
EFI_FV_BLOCK_MAP_ENTRY End[1];
} EFI_FVB2_MEDIA_INFO;
//
// This data structure contains a template of all correct FV headers, which is used to restore
// Fv header if it's corrupted.
//
EFI_FVB2_MEDIA_INFO mPlatformFvbMediaInfo[] = {
//
// Systen NvStorage FVB
//
{
NV_STORAGE_BASE_ADDRESS,
{
{0,}, //ZeroVector[16]
EFI_SYSTEM_NV_DATA_FV_GUID,
FVB_MEDIA_BLOCK_SIZE * SYSTEM_NV_BLOCK_NUM,
EFI_FVH_SIGNATURE,
0x0004feff, // check MdePkg/Include/Pi/PiFirmwareVolume.h for details on EFI_FVB_ATTRIBUTES_2
sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY),
0, //CheckSum which will be calucated dynamically.
0, //ExtHeaderOffset
{0,}, //Reserved[1]
2, //Revision
{
{
SYSTEM_NV_BLOCK_NUM,
FVB_MEDIA_BLOCK_SIZE,
}
}
},
{
{
0,
0
}
}
/**
Returns FVB media information for NV variable storage.
@return FvbMediaInfo A pointer to an instance of FVB media info produced by this function.
The buffer is allocated internally to this function and it is the caller's
responsibility to free the memory
**/
typedef
EFI_STATUS
(*FVB_MEDIA_INFO_GENERATOR)(
OUT EFI_FVB2_MEDIA_INFO *FvbMediaInfo
);
/**
Returns FVB media information for NV variable storage.
@param[out] FvbMediaInfo A pointer to an instance of FVB media info produced by this function.
@retval EFI_SUCCESS A structure was successfully written to the FvbMediaInfo buffer.
@retval EFI_INVALID_PARAMETER The FvbMediaInfo parameter is NULL.
@retval EFI_UNSUPPORTED An error occurred retrieving variable FV information.
@retval EFI_BAD_BUFFER_SIZE An overflow or underflow of the FV buffer occurred with the information found.
**/
EFI_STATUS
GenerateNvStorageFvbMediaInfo (
OUT EFI_FVB2_MEDIA_INFO *FvbMediaInfo
)
{
EFI_STATUS Status;
UINT32 NvBlockNum;
UINT32 TotalNvVariableStorageSize;
EFI_PHYSICAL_ADDRESS NvStorageBaseAddress;
EFI_FIRMWARE_VOLUME_HEADER FvbInfo = {
{0,}, //ZeroVector[16]
EFI_SYSTEM_NV_DATA_FV_GUID, //FileSystemGuid
0, //FvLength
EFI_FVH_SIGNATURE, //Signature
0x0004feff, //Attributes
sizeof (EFI_FIRMWARE_VOLUME_HEADER) + //HeaderLength
sizeof (EFI_FV_BLOCK_MAP_ENTRY),
0, //Checksum
0, //ExtHeaderOffset
{0,}, //Reserved[1]
2, //Revision
{ //BlockMap[1]
{0,0}
}
};
if (FvbMediaInfo == NULL) {
return EFI_INVALID_PARAMETER;
}
ZeroMem (FvbMediaInfo, sizeof (*FvbMediaInfo));
GetVariableFvInfo (&NvStorageBaseAddress, &TotalNvVariableStorageSize);
if ((NvStorageBaseAddress == 0) || (TotalNvVariableStorageSize == 0)) {
return EFI_UNSUPPORTED;
}
NvBlockNum = TotalNvVariableStorageSize / FVB_MEDIA_BLOCK_SIZE;
Status = SafeUint64Mult ((UINT64)NvBlockNum, FVB_MEDIA_BLOCK_SIZE, &FvbInfo.FvLength);
if (EFI_ERROR (Status)) {
return EFI_BAD_BUFFER_SIZE;
}
FvbInfo.BlockMap[0].NumBlocks = NvBlockNum;
FvbInfo.BlockMap[0].Length = FVB_MEDIA_BLOCK_SIZE;
FvbMediaInfo->BaseAddress = NvStorageBaseAddress;
CopyMem (&FvbMediaInfo->FvbInfo, &FvbInfo, sizeof (FvbInfo));
return EFI_SUCCESS;
}
FVB_MEDIA_INFO_GENERATOR mFvbMediaInfoGenerators[] = {
GenerateNvStorageFvbMediaInfo
};
EFI_STATUS
@ -64,12 +106,16 @@ GetFvbInfo (
OUT EFI_FIRMWARE_VOLUME_HEADER **FvbInfo
)
{
EFI_STATUS Status;
EFI_FVB2_MEDIA_INFO FvbMediaInfo;
UINTN Index;
EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
for (Index = 0; Index < sizeof (mPlatformFvbMediaInfo) / sizeof (EFI_FVB2_MEDIA_INFO); Index++) {
if (mPlatformFvbMediaInfo[Index].BaseAddress == FvBaseAddress) {
FvHeader = &mPlatformFvbMediaInfo[Index].FvbInfo;
for (Index = 0; Index < ARRAY_SIZE (mFvbMediaInfoGenerators); Index++) {
Status = mFvbMediaInfoGenerators[Index](&FvbMediaInfo);
ASSERT_EFI_ERROR (Status);
if (!EFI_ERROR (Status) && (FvbMediaInfo.BaseAddress == FvBaseAddress)) {
FvHeader = AllocateCopyPool (sizeof (EFI_FIRMWARE_VOLUME_HEADER), &FvbMediaInfo.FvbInfo);
//
// Update the checksum value of FV header.

Просмотреть файл

@ -3,6 +3,7 @@
which are compliant with the Intel(R) Serial Flash Interface Compatibility Specification.
Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
Copyright (c) Microsoft Corporation.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
@ -25,12 +26,6 @@ FV_INFO mPlatformFvBaseAddress[] = {
{0, 0}
};
FV_INFO mPlatformDefaultBaseAddress[] = {
{0, 0}, // {FixedPcdGet32(PcdFlashNvStorageVariableBase), FixedPcdGet32(PcdFlashNvStorageVariableSize)},
{0, 0}, // {FixedPcdGet32(PcdFlashMicrocodeFvBase), FixedPcdGet32(PcdFlashMicrocodeFvSize)},
{0, 0}
};
FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate = {
{
{
@ -530,6 +525,85 @@ FvbSetVolumeAttributes (
return EFI_SUCCESS;
}
/**
Get the total size of the firmware volume on flash used for variable store operations.
@param[out] BaseAddress Base address of the variable store firmware volume.
@param[out] Length Length in bytes of the firmware volume used for variable store operations.
**/
VOID
GetVariableFvInfo (
OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
OUT UINT32 *Length
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS NvBaseAddress;
EFI_PHYSICAL_ADDRESS NvVariableBaseAddress;
UINT64 Length64;
UINT32 NvStoreLength;
UINT32 FtwSpareLength;
UINT32 FtwWorkingLength;
UINT32 TotalLength;
TotalLength = 0;
Status = EFI_SUCCESS;
if ((BaseAddress == NULL) || (Length == NULL)) {
ASSERT ((BaseAddress != NULL) && (Length != NULL));
return;
}
*BaseAddress = 0;
*Length = 0;
Status = GetVariableFlashNvStorageInfo (&NvBaseAddress, &Length64);
if (!EFI_ERROR (Status)) {
NvVariableBaseAddress = NvBaseAddress;
// Stay within the current UINT32 size assumptions in the variable stack.
Status = SafeUint64ToUint32 (Length64, &NvStoreLength);
}
if (EFI_ERROR (Status)) {
ASSERT_EFI_ERROR (Status);
return;
}
Status = GetVariableFlashFtwSpareInfo (&NvBaseAddress, &Length64);
if (!EFI_ERROR (Status)) {
// Stay within the current UINT32 size assumptions in the variable stack.
Status = SafeUint64ToUint32 (Length64, &FtwSpareLength);
}
if (EFI_ERROR (Status)) {
ASSERT_EFI_ERROR (Status);
return;
}
Status = GetVariableFlashFtwWorkingInfo (&NvBaseAddress, &Length64);
if (!EFI_ERROR (Status)) {
// Stay within the current UINT32 size assumptions in the variable stack.
Status = SafeUint64ToUint32 (Length64, &FtwWorkingLength);
}
if (EFI_ERROR (Status)) {
ASSERT_EFI_ERROR (Status);
return;
}
Status = SafeUint32Add (NvStoreLength, FtwSpareLength, &TotalLength);
if (EFI_ERROR (Status)) {
ASSERT_EFI_ERROR (Status);
return;
}
Status = SafeUint32Add (TotalLength, FtwWorkingLength, &TotalLength);
if (EFI_ERROR (Status)) {
ASSERT_EFI_ERROR (Status);
return;
}
*BaseAddress = NvVariableBaseAddress;
*Length = TotalLength;
}
/**
Check the integrity of firmware volume header
@ -545,7 +619,12 @@ IsFvHeaderValid (
IN CONST EFI_FIRMWARE_VOLUME_HEADER *FvHeader
)
{
if (FvBase == PcdGet32(PcdFlashNvStorageVariableBase)) {
EFI_PHYSICAL_ADDRESS NvStorageFvBaseAddress;
UINT32 NvStorageSize;
GetVariableFvInfo (&NvStorageFvBaseAddress, &NvStorageSize);
if (FvBase == NvStorageFvBaseAddress) {
if (CompareMem (&FvHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid, sizeof(EFI_GUID)) != 0 ) {
return FALSE;
}

Просмотреть файл

@ -2,6 +2,7 @@
Common source definitions used in serial flash drivers
Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
Copyright (c) Microsoft Corporation.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
@ -12,6 +13,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#include <Guid/EventGroup.h>
#include <Guid/FirmwareFileSystem2.h>
#include <Guid/SystemNvDataGuid.h>
#include <Pi/PiFirmwareVolume.h>
#include <Protocol/DevicePath.h>
#include <Protocol/FirmwareVolumeBlock.h>
@ -24,6 +26,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#include <Library/PcdLib.h>
#include <Library/DevicePathLib.h>
#include <Library/HobLib.h>
#include <Library/VariableFlashInfoLib.h>
#include <Library/SafeIntLib.h>
#include <Library/SpiFlashCommonLib.h>
@ -148,11 +152,23 @@ GetFvbInfo (
OUT EFI_FIRMWARE_VOLUME_HEADER **FvbInfo
);
/**
Get the total size of the firmware volume on flash used for variable store operations.
@param[out] BaseAddress Base address of the variable store firmware volume.
@param[out] Length Length in bytes of the firmware volume used for variable store operations.
**/
VOID
GetVariableFvInfo (
OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
OUT UINT32 *Length
);
extern FVB_GLOBAL mFvbModuleGlobal;
extern FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate;
extern FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate;
extern EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL mFvbProtocolTemplate;
extern FV_INFO mPlatformFvBaseAddress[];
extern FV_INFO mPlatformDefaultBaseAddress[];
#endif

Просмотреть файл

@ -113,15 +113,31 @@ FvbInitialize (
UINT32 MaxLbaSize;
UINT32 BytesWritten;
UINTN BytesErased;
UINT64 NvStorageFvSize;
Status = GetVariableFlashNvStorageInfo (&BaseAddress, &NvStorageFvSize);
if (EFI_ERROR (Status)) {
ASSERT_EFI_ERROR (Status);
DEBUG ((DEBUG_ERROR, "[%a] - An error ocurred getting variable info - %r.\n", __FUNCTION__, Status));
return;
}
// Stay within the current UINT32 size assumptions in the variable stack.
Status = SafeUint64ToUint32 (BaseAddress, &mPlatformFvBaseAddress[0].FvBase);
if (EFI_ERROR (Status)) {
ASSERT_EFI_ERROR (Status);
DEBUG ((DEBUG_ERROR, "[%a] - 64-bit variable storage base address not supported.\n", __FUNCTION__));
return;
}
Status = SafeUint64ToUint32 (NvStorageFvSize, &mPlatformFvBaseAddress[0].FvSize);
if (EFI_ERROR (Status)) {
ASSERT_EFI_ERROR (Status);
DEBUG ((DEBUG_ERROR, "[%a] - 64-bit variable storage size not supported.\n", __FUNCTION__));
return;
}
mPlatformFvBaseAddress[0].FvBase = PcdGet32(PcdFlashNvStorageVariableBase);
mPlatformFvBaseAddress[0].FvSize = PcdGet32(PcdFlashNvStorageVariableSize);
mPlatformFvBaseAddress[1].FvBase = PcdGet32(PcdFlashMicrocodeFvBase);
mPlatformFvBaseAddress[1].FvSize = PcdGet32(PcdFlashMicrocodeFvSize);
mPlatformDefaultBaseAddress[0].FvBase = PcdGet32(PcdFlashNvStorageVariableBase);
mPlatformDefaultBaseAddress[0].FvSize = PcdGet32(PcdFlashNvStorageVariableSize);
mPlatformDefaultBaseAddress[1].FvBase = PcdGet32(PcdFlashMicrocodeFvBase);
mPlatformDefaultBaseAddress[1].FvSize = PcdGet32(PcdFlashMicrocodeFvSize);
//
// We will only continue with FVB installation if the
@ -143,7 +159,8 @@ FvbInitialize (
BytesWritten = 0;
BytesErased = 0;
DEBUG ((DEBUG_ERROR, "ERROR - The FV in 0x%x is invalid!\n", FvHeader));
Status = GetFvbInfo (BaseAddress, &FvHeader);
FvHeader = NULL;
Status = GetFvbInfo (BaseAddress, &FvHeader);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "ERROR - Can't recovery FV header at 0x%x. GetFvbInfo Status %r\n", BaseAddress, Status));
continue;
@ -156,27 +173,42 @@ FvbInitialize (
Status = SpiFlashBlockErase( (UINTN) BaseAddress, &BytesErased);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "ERROR - SpiFlashBlockErase Error %r\n", Status));
if (FvHeader != NULL) {
FreePool (FvHeader);
}
continue;
}
if (BytesErased != FvHeader->BlockMap->Length) {
DEBUG ((DEBUG_WARN, "ERROR - BytesErased != FvHeader->BlockMap->Length\n"));
DEBUG ((DEBUG_INFO, " BytesErased = 0x%X\n Length = 0x%X\n", BytesErased, FvHeader->BlockMap->Length));
if (FvHeader != NULL) {
FreePool (FvHeader);
}
continue;
}
BytesWritten = FvHeader->HeaderLength;
Status = SpiFlashWrite ((UINTN)BaseAddress, &BytesWritten, (UINT8*)FvHeader);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "ERROR - SpiFlashWrite Error %r\n", Status));
if (FvHeader != NULL) {
FreePool (FvHeader);
}
continue;
}
if (BytesWritten != FvHeader->HeaderLength) {
DEBUG ((DEBUG_WARN, "ERROR - BytesWritten != HeaderLength\n"));
DEBUG ((DEBUG_INFO, " BytesWritten = 0x%X\n HeaderLength = 0x%X\n", BytesWritten, FvHeader->HeaderLength));
if (FvHeader != NULL) {
FreePool (FvHeader);
}
continue;
}
Status = SpiFlashLock ();
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "ERROR - SpiFlashLock Error %r\n", Status));
if (FvHeader != NULL) {
FreePool (FvHeader);
}
continue;
}
DEBUG ((DEBUG_INFO, "FV Header @ 0x%X restored with static data\n", BaseAddress));
@ -184,6 +216,9 @@ FvbInitialize (
// Clear cache for this range.
//
WriteBackInvalidateDataCacheRange ( (VOID *) (UINTN) BaseAddress, FvHeader->BlockMap->Length);
if (FvHeader != NULL) {
FreePool (FvHeader);
}
}
}

Просмотреть файл

@ -32,8 +32,10 @@
BaseLib
UefiBootServicesTableLib
UefiDriverEntryPoint
SafeIntLib
SpiFlashCommonLib
MmServicesTableLib
VariableFlashInfoLib
[Packages]
MdePkg/MdePkg.dec
@ -41,10 +43,6 @@
IntelSiliconPkg/IntelSiliconPkg.dec
[Pcd]
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize ## CONSUMES
gIntelSiliconPkgTokenSpaceGuid.PcdFlashMicrocodeFvBase ## CONSUMES
gIntelSiliconPkgTokenSpaceGuid.PcdFlashMicrocodeFvSize ## CONSUMES

Просмотреть файл

@ -31,8 +31,10 @@
MemoryAllocationLib
PcdLib
MmServicesTableLib
SafeIntLib
SpiFlashCommonLib
StandaloneMmDriverEntryPoint
VariableFlashInfoLib
[Packages]
MdePkg/MdePkg.dec
@ -40,10 +42,6 @@
IntelSiliconPkg/IntelSiliconPkg.dec
[Pcd]
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize ## CONSUMES
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize ## CONSUMES
gIntelSiliconPkgTokenSpaceGuid.PcdFlashMicrocodeFvBase ## CONSUMES
gIntelSiliconPkgTokenSpaceGuid.PcdFlashMicrocodeFvSize ## CONSUMES

Просмотреть файл

@ -40,9 +40,11 @@
PeiGetVtdPmrAlignmentLib|IntelSiliconPkg/Library/PeiGetVtdPmrAlignmentLib/PeiGetVtdPmrAlignmentLib.inf
TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf
MicrocodeLib|UefiCpuPkg/Library/MicrocodeLib/MicrocodeLib.inf
SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
SpiFlashCommonLib|IntelSiliconPkg/Library/SpiFlashCommonLibNull/SpiFlashCommonLibNull.inf
UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
VariableFlashInfoLib|MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
[LibraryClasses.common.PEIM]
PeimEntryPoint|MdePkg/Library/PeimEntryPoint/PeimEntryPoint.inf
@ -63,11 +65,16 @@
MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
[LibraryClasses.common.DXE_SMM_DRIVER]
DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
MemoryAllocationLib|MdePkg/Library/SmmMemoryAllocationLib/SmmMemoryAllocationLib.inf
MmServicesTableLib|MdePkg/Library/MmServicesTableLib/MmServicesTableLib.inf
SmmServicesTableLib|MdePkg/Library/SmmServicesTableLib/SmmServicesTableLib.inf
UefiLib|MdePkg/Library/UefiLib/UefiLib.inf
UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
[LibraryClasses.common.MM_STANDALONE]
HobLib|StandaloneMmPkg/Library/StandaloneMmHobLib/StandaloneMmHobLib.inf
MemoryAllocationLib|StandaloneMmPkg/Library/StandaloneMmMemoryAllocationLib/StandaloneMmMemoryAllocationLib.inf
MmServicesTableLib|MdePkg/Library/StandaloneMmServicesTableLib/StandaloneMmServicesTableLib.inf
StandaloneMmDriverEntryPoint|MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf