This patch refactors low level NOR flash functions into a dedicated library to enable reuse, particularly for supporting the firmware upgrade feature. Signed-off-by: Tuan Phan <tphan@ventanamicro.com>
348 lines
11 KiB
C
348 lines
11 KiB
C
/** @file VirtNorFlashDxe.c
|
|
|
|
Copyright (c) 2011 - 2021, Arm Limited. All rights reserved.<BR>
|
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
|
|
|
**/
|
|
|
|
#include <Library/BaseMemoryLib.h>
|
|
#include <Library/DxeServicesTableLib.h>
|
|
#include <Library/HobLib.h>
|
|
#include <Library/MemoryAllocationLib.h>
|
|
#include <Library/PcdLib.h>
|
|
#include <Library/UefiBootServicesTableLib.h>
|
|
#include <Library/UefiLib.h>
|
|
|
|
#include "VirtNorFlashDxe.h"
|
|
|
|
STATIC EFI_EVENT mNorFlashVirtualAddrChangeEvent;
|
|
|
|
//
|
|
// Global variable declarations
|
|
//
|
|
NOR_FLASH_INSTANCE **mNorFlashInstances;
|
|
UINT32 mNorFlashDeviceCount;
|
|
UINTN mFlashNvStorageVariableBase;
|
|
EFI_EVENT mFvbVirtualAddrChangeEvent;
|
|
|
|
NOR_FLASH_INSTANCE mNorFlashInstanceTemplate = {
|
|
NOR_FLASH_SIGNATURE, // Signature
|
|
NULL, // Handle ... NEED TO BE FILLED
|
|
|
|
0, // DeviceBaseAddress ... NEED TO BE FILLED
|
|
0, // RegionBaseAddress ... NEED TO BE FILLED
|
|
0, // Size ... NEED TO BE FILLED
|
|
0, // StartLba
|
|
0, // LastBlock
|
|
0, // BlockSize
|
|
|
|
{
|
|
FvbGetAttributes, // GetAttributes
|
|
FvbSetAttributes, // SetAttributes
|
|
FvbGetPhysicalAddress, // GetPhysicalAddress
|
|
FvbGetBlockSize, // GetBlockSize
|
|
FvbRead, // Read
|
|
FvbWrite, // Write
|
|
FvbEraseBlocks, // EraseBlocks
|
|
NULL, // ParentHandle
|
|
}, // FvbProtoccol;
|
|
NULL, // ShadowBuffer
|
|
{
|
|
{
|
|
{
|
|
HARDWARE_DEVICE_PATH,
|
|
HW_VENDOR_DP,
|
|
{
|
|
(UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End)),
|
|
(UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End) >> 8)
|
|
}
|
|
},
|
|
{ 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
|
|
}, // GUID ... NEED TO BE FILLED
|
|
},
|
|
0, // Index
|
|
{
|
|
END_DEVICE_PATH_TYPE,
|
|
END_ENTIRE_DEVICE_PATH_SUBTYPE,
|
|
{ sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }
|
|
}
|
|
} // DevicePath
|
|
};
|
|
|
|
EFI_STATUS
|
|
NorFlashCreateInstance (
|
|
IN UINTN NorFlashDeviceBase,
|
|
IN UINTN NorFlashRegionBase,
|
|
IN UINTN NorFlashSize,
|
|
IN UINT32 Index,
|
|
IN UINT32 BlockSize,
|
|
IN BOOLEAN SupportFvb,
|
|
OUT NOR_FLASH_INSTANCE **NorFlashInstance
|
|
)
|
|
{
|
|
EFI_STATUS Status;
|
|
NOR_FLASH_INSTANCE *Instance;
|
|
|
|
ASSERT (NorFlashInstance != NULL);
|
|
|
|
Instance = AllocateRuntimeCopyPool (sizeof (NOR_FLASH_INSTANCE), &mNorFlashInstanceTemplate);
|
|
if (Instance == NULL) {
|
|
return EFI_OUT_OF_RESOURCES;
|
|
}
|
|
|
|
Instance->DeviceBaseAddress = NorFlashDeviceBase;
|
|
Instance->RegionBaseAddress = NorFlashRegionBase;
|
|
Instance->Size = NorFlashSize;
|
|
Instance->BlockSize = BlockSize;
|
|
Instance->LastBlock = (NorFlashSize / BlockSize) - 1;
|
|
|
|
CopyGuid (&Instance->DevicePath.Vendor.Guid, &gEfiCallerIdGuid);
|
|
Instance->DevicePath.Index = (UINT8)Index;
|
|
|
|
Instance->ShadowBuffer = AllocateRuntimePool (BlockSize);
|
|
if (Instance->ShadowBuffer == NULL) {
|
|
return EFI_OUT_OF_RESOURCES;
|
|
}
|
|
|
|
if (SupportFvb) {
|
|
NorFlashFvbInitialize (Instance);
|
|
|
|
Status = gBS->InstallMultipleProtocolInterfaces (
|
|
&Instance->Handle,
|
|
&gEfiDevicePathProtocolGuid,
|
|
&Instance->DevicePath,
|
|
&gEfiFirmwareVolumeBlockProtocolGuid,
|
|
&Instance->FvbProtocol,
|
|
NULL
|
|
);
|
|
if (EFI_ERROR (Status)) {
|
|
FreePool (Instance);
|
|
return Status;
|
|
}
|
|
} else {
|
|
Status = gBS->InstallMultipleProtocolInterfaces (
|
|
&Instance->Handle,
|
|
&gEfiDevicePathProtocolGuid,
|
|
&Instance->DevicePath,
|
|
NULL
|
|
);
|
|
if (EFI_ERROR (Status)) {
|
|
FreePool (Instance);
|
|
return Status;
|
|
}
|
|
}
|
|
|
|
*NorFlashInstance = Instance;
|
|
return Status;
|
|
}
|
|
|
|
/**
|
|
@param[in] Event The Event that is being processed
|
|
@param[in] Context Event Context
|
|
**/
|
|
VOID
|
|
EFIAPI
|
|
NorFlashVirtualNotifyEvent (
|
|
IN EFI_EVENT Event,
|
|
IN VOID *Context
|
|
)
|
|
{
|
|
UINTN Index;
|
|
|
|
for (Index = 0; Index < mNorFlashDeviceCount; Index++) {
|
|
EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->DeviceBaseAddress);
|
|
EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->RegionBaseAddress);
|
|
|
|
// Convert Fvb
|
|
EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtocol.EraseBlocks);
|
|
EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtocol.GetAttributes);
|
|
EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtocol.GetBlockSize);
|
|
EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtocol.GetPhysicalAddress);
|
|
EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtocol.Read);
|
|
EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtocol.SetAttributes);
|
|
EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->FvbProtocol.Write);
|
|
|
|
if (mNorFlashInstances[Index]->ShadowBuffer != NULL) {
|
|
EfiConvertPointer (0x0, (VOID **)&mNorFlashInstances[Index]->ShadowBuffer);
|
|
}
|
|
}
|
|
}
|
|
|
|
EFI_STATUS
|
|
EFIAPI
|
|
NorFlashInitialise (
|
|
IN EFI_HANDLE ImageHandle,
|
|
IN EFI_SYSTEM_TABLE *SystemTable
|
|
)
|
|
{
|
|
EFI_STATUS Status;
|
|
UINT32 Index;
|
|
VIRT_NOR_FLASH_DESCRIPTION *NorFlashDevices;
|
|
BOOLEAN ContainVariableStorage;
|
|
|
|
Status = VirtNorFlashPlatformInitialization ();
|
|
if (EFI_ERROR (Status)) {
|
|
DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to initialize Nor Flash devices\n"));
|
|
return Status;
|
|
}
|
|
|
|
Status = VirtNorFlashPlatformGetDevices (&NorFlashDevices, &mNorFlashDeviceCount);
|
|
if (EFI_ERROR (Status)) {
|
|
DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to get Nor Flash devices\n"));
|
|
return Status;
|
|
}
|
|
|
|
mNorFlashInstances = AllocateRuntimePool (sizeof (NOR_FLASH_INSTANCE *) * mNorFlashDeviceCount);
|
|
|
|
for (Index = 0; Index < mNorFlashDeviceCount; Index++) {
|
|
// Check if this NOR Flash device contain the variable storage region
|
|
|
|
if (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) {
|
|
ContainVariableStorage =
|
|
(NorFlashDevices[Index].RegionBaseAddress <= PcdGet64 (PcdFlashNvStorageVariableBase64)) &&
|
|
(PcdGet64 (PcdFlashNvStorageVariableBase64) + PcdGet32 (PcdFlashNvStorageVariableSize) <=
|
|
NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index].Size);
|
|
} else {
|
|
ContainVariableStorage =
|
|
(NorFlashDevices[Index].RegionBaseAddress <= PcdGet32 (PcdFlashNvStorageVariableBase)) &&
|
|
(PcdGet32 (PcdFlashNvStorageVariableBase) + PcdGet32 (PcdFlashNvStorageVariableSize) <=
|
|
NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index].Size);
|
|
}
|
|
|
|
Status = NorFlashCreateInstance (
|
|
NorFlashDevices[Index].DeviceBaseAddress,
|
|
NorFlashDevices[Index].RegionBaseAddress,
|
|
NorFlashDevices[Index].Size,
|
|
Index,
|
|
NorFlashDevices[Index].BlockSize,
|
|
ContainVariableStorage,
|
|
&mNorFlashInstances[Index]
|
|
);
|
|
if (EFI_ERROR (Status)) {
|
|
DEBUG ((DEBUG_ERROR, "NorFlashInitialise: Fail to create instance for NorFlash[%d]\n", Index));
|
|
}
|
|
}
|
|
|
|
//
|
|
// Register for the virtual address change event
|
|
//
|
|
Status = gBS->CreateEventEx (
|
|
EVT_NOTIFY_SIGNAL,
|
|
TPL_NOTIFY,
|
|
NorFlashVirtualNotifyEvent,
|
|
NULL,
|
|
&gEfiEventVirtualAddressChangeGuid,
|
|
&mNorFlashVirtualAddrChangeEvent
|
|
);
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
return Status;
|
|
}
|
|
|
|
EFI_STATUS
|
|
EFIAPI
|
|
NorFlashFvbInitialize (
|
|
IN NOR_FLASH_INSTANCE *Instance
|
|
)
|
|
{
|
|
EFI_STATUS Status;
|
|
UINT32 FvbNumLba;
|
|
EFI_BOOT_MODE BootMode;
|
|
UINTN RuntimeMmioRegionSize;
|
|
|
|
DEBUG ((DEBUG_BLKIO, "NorFlashFvbInitialize\n"));
|
|
ASSERT ((Instance != NULL));
|
|
|
|
//
|
|
// Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME
|
|
//
|
|
|
|
// Note: all the NOR Flash region needs to be reserved into the UEFI Runtime memory;
|
|
// even if we only use the small block region at the top of the NOR Flash.
|
|
// The reason is when the NOR Flash memory is set into program mode, the command
|
|
// is written as the base of the flash region (ie: Instance->DeviceBaseAddress)
|
|
RuntimeMmioRegionSize = (Instance->RegionBaseAddress - Instance->DeviceBaseAddress) + Instance->Size;
|
|
|
|
Status = gDS->AddMemorySpace (
|
|
EfiGcdMemoryTypeMemoryMappedIo,
|
|
Instance->DeviceBaseAddress,
|
|
RuntimeMmioRegionSize,
|
|
EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
|
|
);
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
Status = gDS->SetMemorySpaceAttributes (
|
|
Instance->DeviceBaseAddress,
|
|
RuntimeMmioRegionSize,
|
|
EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
|
|
);
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
mFlashNvStorageVariableBase = (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) ?
|
|
PcdGet64 (PcdFlashNvStorageVariableBase64) : PcdGet32 (PcdFlashNvStorageVariableBase);
|
|
|
|
// Set the index of the first LBA for the FVB
|
|
Instance->StartLba = (mFlashNvStorageVariableBase - Instance->RegionBaseAddress) / Instance->BlockSize;
|
|
|
|
BootMode = GetBootModeHob ();
|
|
if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
|
|
Status = EFI_INVALID_PARAMETER;
|
|
} else {
|
|
// Determine if there is a valid header at the beginning of the NorFlash
|
|
Status = ValidateFvHeader (Instance);
|
|
}
|
|
|
|
// Install the Default FVB header if required
|
|
if (EFI_ERROR (Status)) {
|
|
// There is no valid header, so time to install one.
|
|
DEBUG ((DEBUG_INFO, "%a: The FVB Header is not valid.\n", __func__));
|
|
DEBUG ((
|
|
DEBUG_INFO,
|
|
"%a: Installing a correct one for this volume.\n",
|
|
__func__
|
|
));
|
|
|
|
// Erase all the NorFlash that is reserved for variable storage
|
|
FvbNumLba = (PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) + PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / Instance->BlockSize;
|
|
|
|
Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);
|
|
if (EFI_ERROR (Status)) {
|
|
return Status;
|
|
}
|
|
|
|
// Install all appropriate headers
|
|
Status = InitializeFvAndVariableStoreHeaders (Instance);
|
|
if (EFI_ERROR (Status)) {
|
|
return Status;
|
|
}
|
|
}
|
|
|
|
//
|
|
// The driver implementing the variable read service can now be dispatched;
|
|
// the varstore headers are in place.
|
|
//
|
|
Status = gBS->InstallProtocolInterface (
|
|
&gImageHandle,
|
|
&gEdkiiNvVarStoreFormattedGuid,
|
|
EFI_NATIVE_INTERFACE,
|
|
NULL
|
|
);
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
//
|
|
// Register for the virtual address change event
|
|
//
|
|
Status = gBS->CreateEventEx (
|
|
EVT_NOTIFY_SIGNAL,
|
|
TPL_NOTIFY,
|
|
FvbVirtualNotifyEvent,
|
|
NULL,
|
|
&gEfiEventVirtualAddressChangeGuid,
|
|
&mFvbVirtualAddrChangeEvent
|
|
);
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
return Status;
|
|
}
|