From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=217.140.101.70; helo=foss.arm.com; envelope-from=jagadeesh.ujja@arm.com; receiver=edk2-devel@lists.01.org Received: from foss.arm.com (usa-sjc-mx-foss1.foss.arm.com [217.140.101.70]) by ml01.01.org (Postfix) with ESMTP id 1C699208F611A for ; Tue, 19 Feb 2019 02:32:27 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 57B9A1AC1; Tue, 19 Feb 2019 02:16:10 -0800 (PST) Received: from usa.arm.com (a075556-lin.blr.arm.com [10.162.2.185]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 888A03FB75; Tue, 19 Feb 2019 02:15:48 -0800 (PST) From: Jagadeesh Ujja To: edk2-devel@lists.01.org, liming.gao@intel.com, chao.b.zhang@intel.com, leif.lindholm@linaro.org, ard.biesheuvel@linaro.org Date: Tue, 19 Feb 2019 15:45:34 +0530 Message-Id: <1550571334-29663-3-git-send-email-jagadeesh.ujja@arm.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1550571334-29663-1-git-send-email-jagadeesh.ujja@arm.com> References: <1550571334-29663-1-git-send-email-jagadeesh.ujja@arm.com> Subject: [PATCH 2/2] ArmPlatformPkg/NorFlash: Allow reusability as a MM driver X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 X-List-Received-Date: Tue, 19 Feb 2019 10:32:27 -0000 Adapt the NorFlash driver to be used as a MM_STANDALONE driver to allow access to NOR flash for code executing in MM_STANDALONE mode. This allows storing of EFI variables on NOR flash which is accessible only via the MM STANDALONE mode software. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jagadeesh Ujja --- ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashStandaloneMm.c | 267 ++++++++++++++++++++ ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashStandaloneMm.inf | 77 ++++++ 2 files changed, 344 insertions(+) diff --git a/ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashStandaloneMm.c b/ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashStandaloneMm.c new file mode 100644 index 0000000..1e3603c --- /dev/null +++ b/ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashStandaloneMm.c @@ -0,0 +1,267 @@ +/*++ @file NorFlashStandaloneMm.c + + Copyright (c) 2019, ARM Ltd. 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. + + --*/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include "NorFlash.h" + +// +// Global variable declarations +// +NOR_FLASH_INSTANCE **mNorFlashInstances; +UINT32 mNorFlashDeviceCount; + +extern NOR_FLASH_INSTANCE mNorFlashInstanceTemplate; + +EFI_STATUS +EFIAPI +NorFlashFvbInitialize ( + IN NOR_FLASH_INSTANCE* Instance + ) +{ + EFI_STATUS Status; + UINT32 FvbNumLba; + EFI_BOOT_MODE BootMode; + + DEBUG((DEBUG_BLKIO,"NorFlashFvbInitialize\n")); + ASSERT((Instance != NULL)); + + mFlashNvStorageVariableBase = FixedPcdGet32 (PcdFlashNvStorageVariableBase); + + // Set the index of the first LBA for the FVB + Instance->StartLba = (PcdGet32 (PcdFlashNvStorageVariableBase) - Instance->RegionBaseAddress) / Instance->Media.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 ((EFI_D_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__)); + DEBUG ((EFI_D_INFO, "%a: Installing a correct one for this volume.\n", + __FUNCTION__)); + + // Erase all the NorFlash that is reserved for variable storage + FvbNumLba = (PcdGet32(PcdFlashNvStorageVariableSize) + PcdGet32(PcdFlashNvStorageFtwWorkingSize) + PcdGet32(PcdFlashNvStorageFtwSpareSize)) / Instance->Media.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; + } + } + + return Status; +} + +VOID +EFIAPI +NorFlashLock ( + NOR_FLASH_LOCK_CONTEXT *Context + ) +{ +} + +VOID +EFIAPI +NorFlashUnlock ( + NOR_FLASH_LOCK_CONTEXT *Context + ) +{ +} + +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->BlockIoProtocol.Media = &Instance->Media; + Instance->Media.MediaId = Index; + Instance->Media.BlockSize = BlockSize; + Instance->Media.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); + + //Install DevicePath Protocol + Status = gMmst->MmInstallProtocolInterface ( + &Instance->Handle, + &gEfiDevicePathProtocolGuid, + EFI_NATIVE_INTERFACE, + &Instance->DevicePath + ); + if (EFI_ERROR(Status)) { + FreePool (Instance); + return Status; + } + //Install BlockIo Protocol + Status = gMmst->MmInstallProtocolInterface ( + &Instance->Handle, + &gEfiBlockIoProtocolGuid, + EFI_NATIVE_INTERFACE, + &Instance->BlockIoProtocol + ); + if (EFI_ERROR(Status)) { + FreePool (Instance); + return Status; + } + //Install FirmwareVolumeBlock Protocol + Status = gMmst->MmInstallProtocolInterface ( + &Instance->Handle, + &gEfiSmmFirmwareVolumeBlockProtocolGuid, + EFI_NATIVE_INTERFACE, + &Instance->FvbProtocol + ); + if (EFI_ERROR(Status)) { + FreePool (Instance); + return Status; + } + } else { + //Install DevicePath Protocol + Status = gMmst->MmInstallProtocolInterface ( + &Instance->Handle, + &gEfiDevicePathProtocolGuid, + EFI_NATIVE_INTERFACE, + &Instance->DevicePath + ); + if (EFI_ERROR(Status)) { + FreePool (Instance); + return Status; + } + //Install BlockIo Protocol + Status = gMmst->MmInstallProtocolInterface ( + &Instance->Handle, + &gEfiBlockIoProtocolGuid, + EFI_NATIVE_INTERFACE, + &Instance->BlockIoProtocol + ); + if (EFI_ERROR(Status)) { + FreePool (Instance); + return Status; + } + //Install DiskIO Protocol + Status = gMmst->MmInstallProtocolInterface ( + &Instance->Handle, + &gEfiDiskIoProtocolGuid, + EFI_NATIVE_INTERFACE, + &Instance->DiskIoProtocol + ); + if (EFI_ERROR(Status)) { + FreePool (Instance); + return Status; + } + } + + *NorFlashInstance = Instance; + return Status; +} + +EFI_STATUS +EFIAPI +NorFlashInitialise ( + IN EFI_HANDLE ImageHandle, + IN EFI_MM_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + UINT32 Index; + NOR_FLASH_DESCRIPTION* NorFlashDevices; + BOOLEAN ContainVariableStorage; + + Status = NorFlashPlatformInitialization (); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_ERROR,"NorFlashInitialise: Fail to initialize Nor Flash devices\n")); + return Status; + } + + Status = NorFlashPlatformGetDevices (&NorFlashDevices, &mNorFlashDeviceCount); + if (EFI_ERROR(Status)) { + DEBUG((EFI_D_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 + 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((EFI_D_ERROR,"NorFlashInitialise: Fail to create instance for NorFlash[%d]\n",Index)); + } + } + return Status; +} diff --git a/ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashStandaloneMm.inf b/ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashStandaloneMm.inf new file mode 100644 index 0000000..b189220 --- /dev/null +++ b/ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashStandaloneMm.inf @@ -0,0 +1,77 @@ +#/** @file +# +# Component description file for NorFlashStandaloneMm module +# +# Copyright (c) 2019, ARM Ltd. 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. +# +#**/ + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = ArmVeNorFlashDxe + FILE_GUID = 166F677B-DAC9-4AE4-AD34-2FF2504B0637 + MODULE_TYPE = MM_STANDALONE + VERSION_STRING = 1.0 + PI_SPECIFICATION_VERSION = 0x00010032 + ENTRY_POINT = NorFlashInitialise + +[Sources.common] + NorFlash.c + NorFlashFvb.c + NorFlashBlockIo.c + NorFlashStandaloneMm.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + ArmPlatformPkg/ArmPlatformPkg.dec + EmbeddedPkg/EmbeddedPkg.dec + ArmPkg/ArmPkg.dec + StandaloneMmPkg/StandaloneMmPkg.dec + +[LibraryClasses] + StandaloneMmDriverEntryPoint + BaseMemoryLib + ArmSvcLib + ArmLib + IoLib + BaseLib + DebugLib + HobLib + MemoryAllocationLib + NorFlashPlatformLib + MmServicesTableLib + +[Guids] + gEfiSystemNvDataFvGuid + gEfiVariableGuid + gEfiAuthenticatedVariableGuid + gEfiEventVirtualAddressChangeGuid + gEdkiiNvVarStoreFormattedGuid ## PRODUCES ## PROTOCOL + +[Protocols] + gEfiBlockIoProtocolGuid + gEfiDevicePathProtocolGuid + gEfiSmmFirmwareVolumeBlockProtocolGuid + gEfiDiskIoProtocolGuid + +[Pcd.common] + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize + + gArmPlatformTokenSpaceGuid.PcdNorFlashCheckBlockLocked + +[Depex] + TRUE -- 2.7.4