From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web09.12625.1632611151977186302 for ; Sat, 25 Sep 2021 16:05:52 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.31, mailfrom: guo.dong@intel.com) X-IronPort-AV: E=McAfee;i="6200,9189,10118"; a="285311485" X-IronPort-AV: E=Sophos;i="5.85,322,1624345200"; d="scan'208";a="285311485" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2021 16:05:51 -0700 X-IronPort-AV: E=Sophos;i="5.85,322,1624345200"; d="scan'208";a="586418287" Received: from gdong1-mobl1.amr.corp.intel.com ([10.255.67.241]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2021 16:05:51 -0700 From: "Guo Dong" To: devel@edk2.groups.io Cc: ray.ni@intel.com, maurice.ma@intel.com, benjamin.you@intel.com, Guo Dong Subject: [`edk2-devel][PATCH 5/8] UefiPayloadPkg: Add FlashDeviceLib Date: Sat, 25 Sep 2021 16:05:27 -0700 Message-Id: <20210925230530.861-6-guo.dong@intel.com> X-Mailer: git-send-email 2.32.0.windows.2 In-Reply-To: <20210925230530.861-1-guo.dong@intel.com> References: <20210925230530.861-1-guo.dong@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Guo Dong This library provides FlashDeviceLib APIs based on SpiFlashLib and consumed by FVB driver. Signed-off-by: Guo Dong --- .../Include/Library/FlashDeviceLib.h | 108 ++++++++++++ .../Library/FlashDeviceLib/FlashDeviceLib.c | 165 ++++++++++++++++++ .../Library/FlashDeviceLib/FlashDeviceLib.inf | 38 ++++ 3 files changed, 311 insertions(+) create mode 100644 UefiPayloadPkg/Include/Library/FlashDeviceLib.h create mode 100644 UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.c create mode 100644 UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.inf diff --git a/UefiPayloadPkg/Include/Library/FlashDeviceLib.h b/UefiPayloadP= kg/Include/Library/FlashDeviceLib.h new file mode 100644 index 0000000000..32694b317e --- /dev/null +++ b/UefiPayloadPkg/Include/Library/FlashDeviceLib.h @@ -0,0 +1,108 @@ +/** @file=0D + Flash device library class header file.=0D +=0D + Copyright (c) 2017 - 2021, Intel Corporation. All rights reserved.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +=0D +#ifndef __FLASHDEVICE_LIB_H__=0D +#define __FLASHDEVICE_LIB_H__=0D +=0D +/**=0D + Read NumBytes bytes of data from the address specified by=0D + PAddress into Buffer.=0D +=0D + @param[in] PAddress The starting physical address of the read.=0D + @param[in,out] NumBytes On input, the number of bytes to read. On ou= tput, the number=0D + of bytes actually read.=0D + @param[out] Buffer The destination data buffer for the read.=0D +=0D + @retval EFI_SUCCESS. Opertion is successful.=0D + @retval EFI_DEVICE_ERROR If there is any device errors.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibFvbFlashDeviceRead (=0D + IN UINTN PAddress,=0D + IN OUT UINTN *NumBytes,=0D + OUT UINT8 *Buffer=0D + );=0D +=0D +=0D +/**=0D + Write NumBytes bytes of data from Buffer to the address specified by=0D + PAddresss.=0D +=0D + @param[in] PAddress The starting physical address of the write.=0D + @param[in,out] NumBytes On input, the number of bytes to write. On outp= ut,=0D + the actual number of bytes written.=0D + @param[in] Buffer The source data buffer for the write.=0D +=0D + @retval EFI_SUCCESS. Opertion is successful.=0D + @retval EFI_DEVICE_ERROR If there is any device errors.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibFvbFlashDeviceWrite (=0D + IN UINTN PAddress,=0D + IN OUT UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + );=0D +=0D +=0D +/**=0D + Erase the block staring at PAddress.=0D +=0D + @param[in] PAddress The starting physical address of the region to be e= rased.=0D + @param[in] LbaLength The length of the region to be erased. This para= meter is necessary=0D + as the physical block size on a flash device could = be different than=0D + the logical block size of Firmware Volume Block pro= tocol. Erase on=0D + flash chip is always performed block by block. Ther= efore, the ERASE=0D + operation to a logical block is converted a number = of ERASE operation=0D + (or a partial erase) on the hardware.=0D +=0D + @retval EFI_SUCCESS. Opertion is successful.=0D + @retval EFI_DEVICE_ERROR If there is any device errors.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibFvbFlashDeviceBlockErase (=0D + IN UINTN PAddress,=0D + IN UINTN LbaLength=0D +);=0D +=0D +=0D +/**=0D + Lock or unlock the block staring at PAddress.=0D +=0D + @param[in] PAddress The starting physical address of region to be (un)l= ocked.=0D + @param[in] LbaLength The length of the region to be (un)locked. This = parameter is necessary=0D + as the physical block size on a flash device could = be different than=0D + the logical block size of Firmware Volume Block pro= tocol. (Un)Lock on=0D + flash chip is always performed block by block. Ther= efore, the (Un)Lock=0D + operation to a logical block is converted a number = of (Un)Lock operation=0D + (or a partial erase) on the hardware.=0D + @param[in] Lock TRUE to lock. FALSE to unlock.=0D +=0D + @retval EFI_SUCCESS. Opertion is successful.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibFvbFlashDeviceBlockLock (=0D + IN UINTN PAddress,=0D + IN UINTN LbaLength,=0D + IN BOOLEAN Lock=0D +);=0D +=0D +PHYSICAL_ADDRESS=0D +EFIAPI=0D +LibFvbFlashDeviceMemoryMap (=0D +);=0D +=0D +#endif=0D diff --git a/UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.c b/UefiP= ayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.c new file mode 100644 index 0000000000..efefd53466 --- /dev/null +++ b/UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.c @@ -0,0 +1,165 @@ +/** @file=0D + Flash Device Library based on SPI Flash library.=0D +=0D +Copyright (c) 2018 - 2021, Intel Corporation. All rights reserved.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +/**=0D + Initialize spi flash device.=0D +=0D + @retval EFI_SUCCESS The tested spi flash device is supporte= d.=0D + @retval EFI_UNSUPPORTED The tested spi flash device is not supp= orted.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibFvbFlashDeviceInit (=0D + VOID=0D + )=0D +{=0D + return SpiConstructor ();=0D +}=0D +=0D +=0D +/**=0D + Read NumBytes bytes of data from the address specified by=0D + PAddress into Buffer.=0D +=0D + @param[in] PAddress The starting physical address of the read.= =0D + @param[in,out] NumBytes On input, the number of bytes to read. On = output, the number=0D + of bytes actually read.=0D + @param[out] Buffer The destination data buffer for the read.= =0D +=0D + @retval EFI_SUCCESS. Opertion is successful.=0D + @retval EFI_DEVICE_ERROR If there is any device errors.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibFvbFlashDeviceRead (=0D + IN UINTN PAddress,=0D + IN OUT UINTN *NumBytes,=0D + OUT UINT8 *Buffer=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINT32 ByteCount;=0D + UINT32 RgnSize;=0D + UINT32 AddrOffset;=0D +=0D + Status =3D SpiGetRegionAddress (FlashRegionBios, NULL, &RgnSize);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D +=0D + // BIOS region offset can be calculated by (PAddress - (0x100000000 - Rg= nSize))=0D + // which equal (PAddress + RgnSize) here.=0D + AddrOffset =3D (UINT32)((UINT32)PAddress + RgnSize);=0D + ByteCount =3D (UINT32)*NumBytes;=0D + return SpiFlashRead (FlashRegionBios, AddrOffset, ByteCount, Buffer);=0D +}=0D +=0D +=0D +/**=0D + Write NumBytes bytes of data from Buffer to the address specified by=0D + PAddresss.=0D +=0D + @param[in] PAddress The starting physical address of the wri= te.=0D + @param[in,out] NumBytes On input, the number of bytes to write. = On output,=0D + the actual number of bytes written.=0D + @param[in] Buffer The source data buffer for the write.=0D +=0D + @retval EFI_SUCCESS. Opertion is successful.=0D + @retval EFI_DEVICE_ERROR If there is any device errors.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibFvbFlashDeviceWrite (=0D + IN UINTN PAddress,=0D + IN OUT UINTN *NumBytes,=0D + IN UINT8 *Buffer=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINT32 ByteCount;=0D + UINT32 RgnSize;=0D + UINT32 AddrOffset;=0D +=0D + Status =3D SpiGetRegionAddress (FlashRegionBios, NULL, &RgnSize);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D +=0D + // BIOS region offset can be calculated by (PAddress - (0x100000000 - Rg= nSize))=0D + // which equal (PAddress + RgnSize) here.=0D + AddrOffset =3D (UINT32)((UINT32)PAddress + RgnSize);=0D + ByteCount =3D (UINT32)*NumBytes;=0D + return SpiFlashWrite (FlashRegionBios, AddrOffset, ByteCount, Buffer);=0D +}=0D +=0D +=0D +/**=0D + Erase the block staring at PAddress.=0D +=0D + @param[in] PAddress The starting physical address of the block t= o be erased.=0D + This library assume that caller garantee tha= t the PAddress=0D + is at the starting address of this block.=0D + @param[in] LbaLength The length of the logical block to be erased= .=0D +=0D + @retval EFI_SUCCESS. Opertion is successful.=0D + @retval EFI_DEVICE_ERROR If there is any device errors.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibFvbFlashDeviceBlockErase (=0D + IN UINTN PAddress,=0D + IN UINTN LbaLength=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINT32 RgnSize;=0D + UINT32 AddrOffset;=0D +=0D + Status =3D SpiGetRegionAddress (FlashRegionBios, NULL, &RgnSize);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D +=0D + // BIOS region offset can be calculated by (PAddress - (0x100000000 - Rg= nSize))=0D + // which equal (PAddress + RgnSize) here.=0D + AddrOffset =3D (UINT32)((UINT32)PAddress + RgnSize);=0D + return SpiFlashErase (FlashRegionBios, AddrOffset, (UINT32)LbaLength);=0D +}=0D +=0D +=0D +/**=0D + Lock or unlock the block staring at PAddress.=0D +=0D + @param[in] PAddress The starting physical address of region to b= e (un)locked.=0D + @param[in] LbaLength The length of the logical block to be erased= .=0D + @param[in] Lock TRUE to lock. FALSE to unlock.=0D +=0D + @retval EFI_SUCCESS. Opertion is successful.=0D + @retval EFI_DEVICE_ERROR If there is any device errors.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibFvbFlashDeviceBlockLock (=0D + IN UINTN PAddress,=0D + IN UINTN LbaLength,=0D + IN BOOLEAN Lock=0D + )=0D +{=0D + return EFI_SUCCESS;=0D +}=0D +=0D diff --git a/UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.inf b/Uef= iPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.inf new file mode 100644 index 0000000000..0ad22cffba --- /dev/null +++ b/UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.inf @@ -0,0 +1,38 @@ +## @file=0D +# Library instace of Flash Device Library Class=0D +#=0D +# This library implement the flash device library class for the lakeport p= latform.=0D +#@copyright=0D +# Copyright (c) 2014 - 2021 Intel Corporation. All rights reserved=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x00010005=0D + BASE_NAME =3D FlashDeviceLib=0D + FILE_GUID =3D BA7CA537-1C65-4a90-9379-622A24A08141= =0D + MODULE_TYPE =3D DXE_DRIVER=0D + VERSION_STRING =3D 1.0=0D + LIBRARY_CLASS =3D FlashDeviceLib | DXE_SMM_DRIVER DXE_R= UNTIME_DRIVER=0D +=0D +=0D +#=0D +# The following information is for reference only and not required by the = build tools.=0D +#=0D +# VALID_ARCHITECTURES =3D IA32 X64=0D +#=0D +=0D +[Sources]=0D + FlashDeviceLib.c=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D + MdeModulePkg/MdeModulePkg.dec=0D + UefiPayloadPkg/UefiPayloadPkg.dec=0D +=0D +[LibraryClasses]=0D + DebugLib=0D + BaseMemoryLib=0D + SpiFlashLib=0D +=0D --=20 2.32.0.windows.2