]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiPayloadPkg: Add FlashDeviceLib
authorGuo Dong <guo.dong@intel.com>
Wed, 22 Sep 2021 21:32:30 +0000 (14:32 -0700)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 25 Oct 2021 17:28:21 +0000 (17:28 +0000)
This library provides FlashDeviceLib APIs based on
SpiFlashLib and consumed by FVB driver.

Signed-off-by: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Reviewed-by: Benjamin You <benjamin.you@intel.com>
UefiPayloadPkg/Include/Library/FlashDeviceLib.h [new file with mode: 0644]
UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.c [new file with mode: 0644]
UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.inf [new file with mode: 0644]

diff --git a/UefiPayloadPkg/Include/Library/FlashDeviceLib.h b/UefiPayloadPkg/Include/Library/FlashDeviceLib.h
new file mode 100644 (file)
index 0000000..d71481c
--- /dev/null
@@ -0,0 +1,108 @@
+/** @file\r
+  Flash device library class header file.\r
+\r
+  Copyright (c) 2017 - 2021, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+\r
+#ifndef FLASHDEVICE_LIB_H_\r
+#define FLASHDEVICE_LIB_H_\r
+\r
+/**\r
+  Read NumBytes bytes of data from the address specified by\r
+  PAddress into Buffer.\r
+\r
+  @param[in]      PAddress    The starting physical address of the read.\r
+  @param[in,out]  NumBytes    On input, the number of bytes to read. On output, the number\r
+                              of bytes actually read.\r
+  @param[out]     Buffer      The destination data buffer for the read.\r
+\r
+  @retval EFI_SUCCESS.        Opertion is successful.\r
+  @retval EFI_DEVICE_ERROR    If there is any device errors.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+LibFvbFlashDeviceRead (\r
+  IN      UINTN                           PAddress,\r
+  IN  OUT UINTN                           *NumBytes,\r
+      OUT UINT8                           *Buffer\r
+  );\r
+\r
+\r
+/**\r
+  Write NumBytes bytes of data from Buffer to the address specified by\r
+  PAddresss.\r
+\r
+  @param[in]      PAddress The starting physical address of the write.\r
+  @param[in,out]  NumBytes On input, the number of bytes to write. On output,\r
+                           the actual number of bytes written.\r
+  @param[in]      Buffer   The source data buffer for the write.\r
+\r
+  @retval EFI_SUCCESS.            Opertion is successful.\r
+  @retval EFI_DEVICE_ERROR        If there is any device errors.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+LibFvbFlashDeviceWrite (\r
+  IN        UINTN                           PAddress,\r
+  IN OUT    UINTN                           *NumBytes,\r
+  IN        UINT8                           *Buffer\r
+  );\r
+\r
+\r
+/**\r
+  Erase the block starting at PAddress.\r
+\r
+  @param[in]  PAddress The starting physical address of the region to be erased.\r
+  @param[in]  LbaLength   The length of the region to be erased. This parameter is necessary\r
+                       as the physical block size on a flash device could be different than\r
+                       the logical block size of Firmware Volume Block protocol. Erase on\r
+                       flash chip is always performed block by block. Therefore, the ERASE\r
+                       operation to a logical block is converted a number of ERASE operation\r
+                       (or a partial erase) on the hardware.\r
+\r
+  @retval EFI_SUCCESS.            Opertion is successful.\r
+  @retval EFI_DEVICE_ERROR        If there is any device errors.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+LibFvbFlashDeviceBlockErase (\r
+  IN    UINTN                      PAddress,\r
+  IN    UINTN                      LbaLength\r
+);\r
+\r
+\r
+/**\r
+  Lock or unlock the block starting at PAddress.\r
+\r
+  @param[in]  PAddress The starting physical address of region to be (un)locked.\r
+  @param[in]  LbaLength   The length of the region to be (un)locked. This parameter is necessary\r
+                       as the physical block size on a flash device could be different than\r
+                       the logical block size of Firmware Volume Block protocol. (Un)Lock on\r
+                       flash chip is always performed block by block. Therefore, the (Un)Lock\r
+                       operation to a logical block is converted a number of (Un)Lock operation\r
+                       (or a partial erase) on the hardware.\r
+  @param[in]  Lock     TRUE to lock. FALSE to unlock.\r
+\r
+  @retval EFI_SUCCESS. Opertion is successful.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+LibFvbFlashDeviceBlockLock (\r
+  IN    UINTN                          PAddress,\r
+  IN    UINTN                          LbaLength,\r
+  IN    BOOLEAN                        Lock\r
+);\r
+\r
+PHYSICAL_ADDRESS\r
+EFIAPI\r
+LibFvbFlashDeviceMemoryMap (\r
+);\r
+\r
+#endif\r
diff --git a/UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.c b/UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.c
new file mode 100644 (file)
index 0000000..065841b
--- /dev/null
@@ -0,0 +1,165 @@
+/** @file\r
+  Flash Device Library based on SPI Flash library.\r
+\r
+Copyright (c) 2018 - 2021, Intel Corporation. All rights reserved. <BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/SpiFlashLib.h>\r
+\r
+/**\r
+  Initialize spi flash device.\r
+\r
+  @retval EFI_SUCCESS              The tested spi flash device is supported.\r
+  @retval EFI_UNSUPPORTED          The tested spi flash device is not supported.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+LibFvbFlashDeviceInit (\r
+  VOID\r
+  )\r
+{\r
+  return SpiConstructor ();\r
+}\r
+\r
+\r
+/**\r
+  Read NumBytes bytes of data from the address specified by\r
+  PAddress into Buffer.\r
+\r
+  @param[in]      PAddress      The starting physical address of the read.\r
+  @param[in,out]  NumBytes      On input, the number of bytes to read. On output, the number\r
+                                of bytes actually read.\r
+  @param[out]     Buffer        The destination data buffer for the read.\r
+\r
+  @retval         EFI_SUCCESS.      Opertion is successful.\r
+  @retval         EFI_DEVICE_ERROR  If there is any device errors.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+LibFvbFlashDeviceRead (\r
+  IN      UINTN                           PAddress,\r
+  IN  OUT UINTN                           *NumBytes,\r
+      OUT UINT8                           *Buffer\r
+  )\r
+{\r
+  EFI_STATUS                              Status;\r
+  UINT32                                  ByteCount;\r
+  UINT32                                  RgnSize;\r
+  UINT32                                  AddrOffset;\r
+\r
+  Status = SpiGetRegionAddress (FlashRegionBios, NULL, &RgnSize);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  // BIOS region offset can be calculated by (PAddress - (0x100000000 - RgnSize))\r
+  // which equal (PAddress + RgnSize) here.\r
+  AddrOffset = (UINT32)((UINT32)PAddress + RgnSize);\r
+  ByteCount  = (UINT32)*NumBytes;\r
+  return SpiFlashRead (FlashRegionBios, AddrOffset, ByteCount, Buffer);\r
+}\r
+\r
+\r
+/**\r
+  Write NumBytes bytes of data from Buffer to the address specified by\r
+  PAddresss.\r
+\r
+  @param[in]      PAddress        The starting physical address of the write.\r
+  @param[in,out]  NumBytes        On input, the number of bytes to write. On output,\r
+                                  the actual number of bytes written.\r
+  @param[in]      Buffer          The source data buffer for the write.\r
+\r
+  @retval         EFI_SUCCESS.      Opertion is successful.\r
+  @retval         EFI_DEVICE_ERROR  If there is any device errors.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+LibFvbFlashDeviceWrite (\r
+  IN        UINTN                           PAddress,\r
+  IN OUT    UINTN                           *NumBytes,\r
+  IN        UINT8                           *Buffer\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  UINT32                                    ByteCount;\r
+  UINT32                                    RgnSize;\r
+  UINT32                                    AddrOffset;\r
+\r
+  Status = SpiGetRegionAddress (FlashRegionBios, NULL, &RgnSize);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  // BIOS region offset can be calculated by (PAddress - (0x100000000 - RgnSize))\r
+  // which equal (PAddress + RgnSize) here.\r
+  AddrOffset = (UINT32)((UINT32)PAddress + RgnSize);\r
+  ByteCount  = (UINT32)*NumBytes;\r
+  return SpiFlashWrite (FlashRegionBios, AddrOffset, ByteCount, Buffer);\r
+}\r
+\r
+\r
+/**\r
+  Erase the block starting at PAddress.\r
+\r
+  @param[in]  PAddress        The starting physical address of the block to be erased.\r
+                              This library assume that caller garantee that the PAddress\r
+                              is at the starting address of this block.\r
+  @param[in]  LbaLength       The length of the logical block to be erased.\r
+\r
+  @retval     EFI_SUCCESS.      Opertion is successful.\r
+  @retval     EFI_DEVICE_ERROR  If there is any device errors.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+LibFvbFlashDeviceBlockErase (\r
+  IN    UINTN                     PAddress,\r
+  IN    UINTN                     LbaLength\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  UINT32                          RgnSize;\r
+  UINT32                          AddrOffset;\r
+\r
+  Status = SpiGetRegionAddress (FlashRegionBios, NULL, &RgnSize);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  // BIOS region offset can be calculated by (PAddress - (0x100000000 - RgnSize))\r
+  // which equal (PAddress + RgnSize) here.\r
+  AddrOffset = (UINT32)((UINT32)PAddress + RgnSize);\r
+  return SpiFlashErase (FlashRegionBios, AddrOffset, (UINT32)LbaLength);\r
+}\r
+\r
+\r
+/**\r
+  Lock or unlock the block starting at PAddress.\r
+\r
+  @param[in]  PAddress        The starting physical address of region to be (un)locked.\r
+  @param[in]  LbaLength       The length of the logical block to be erased.\r
+  @param[in]  Lock            TRUE to lock. FALSE to unlock.\r
+\r
+  @retval     EFI_SUCCESS.      Opertion is successful.\r
+  @retval     EFI_DEVICE_ERROR  If there is any device errors.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+LibFvbFlashDeviceBlockLock (\r
+  IN    UINTN                          PAddress,\r
+  IN    UINTN                          LbaLength,\r
+  IN    BOOLEAN                        Lock\r
+  )\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
+\r
diff --git a/UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.inf b/UefiPayloadPkg/Library/FlashDeviceLib/FlashDeviceLib.inf
new file mode 100644 (file)
index 0000000..0ad22cf
--- /dev/null
@@ -0,0 +1,38 @@
+## @file\r
+# Library instace of Flash Device Library Class\r
+#\r
+# This library implement the flash device library class for the lakeport platform.\r
+#@copyright\r
+# Copyright (c) 2014 - 2021 Intel Corporation. All rights reserved\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = FlashDeviceLib\r
+  FILE_GUID                      = BA7CA537-1C65-4a90-9379-622A24A08141\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = FlashDeviceLib | DXE_SMM_DRIVER DXE_RUNTIME_DRIVER\r
+\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  FlashDeviceLib.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  UefiPayloadPkg/UefiPayloadPkg.dec\r
+\r
+[LibraryClasses]\r
+  DebugLib\r
+  BaseMemoryLib\r
+  SpiFlashLib\r
+\r