--- /dev/null
+/** @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
--- /dev/null
+/** @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