--- /dev/null
+/** @file\r
+ Platform Flash Access library.\r
+\r
+ Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
+ This program and the accompanying materials\r
+ are licensed and made available under the terms and conditions of the BSD License\r
+ which accompanies this distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <Library/PlatformFlashAccessLib.h>\r
+#include <Library/FlashDeviceLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+#define SECTOR_SIZE_64KB 0x10000 // Common 64kBytes sector size\r
+#define ALINGED_SIZE SECTOR_SIZE_64KB\r
+\r
+STATIC EFI_PHYSICAL_ADDRESS mInternalFdAddress;\r
+\r
+/**\r
+ Perform flash write opreation.\r
+\r
+ @param[in] FirmwareType The type of firmware.\r
+ @param[in] FlashAddress The address of flash device to be accessed.\r
+ @param[in] FlashAddressType The type of flash device address.\r
+ @param[in] Buffer The pointer to the data buffer.\r
+ @param[in] Length The length of data buffer in bytes.\r
+\r
+ @retval EFI_SUCCESS The operation returns successfully.\r
+ @retval EFI_WRITE_PROTECTED The flash device is read only.\r
+ @retval EFI_UNSUPPORTED The flash device access is unsupported.\r
+ @retval EFI_INVALID_PARAMETER The input parameter is not valid.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PerformFlashWrite (\r
+ IN PLATFORM_FIRMWARE_TYPE FirmwareType,\r
+ IN EFI_PHYSICAL_ADDRESS FlashAddress,\r
+ IN FLASH_ADDRESS_TYPE FlashAddressType,\r
+ IN VOID *Buffer,\r
+ IN UINTN Length\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ DEBUG((DEBUG_INFO, "PerformFlashWrite - 0x%x(%x) - 0x%x\n", (UINTN)FlashAddress, (UINTN)FlashAddressType, Length));\r
+ if (FlashAddressType == FlashAddressTypeRelativeAddress) {\r
+ FlashAddress = FlashAddress + mInternalFdAddress;\r
+ }\r
+\r
+ DEBUG((DEBUG_INFO, " - 0x%x(%x) - 0x%x\n", (UINTN)FlashAddress, (UINTN)FlashAddressType, Length));\r
+ LibFvbFlashDeviceBlockLock(FlashAddress, Length, FALSE);\r
+\r
+ //\r
+ // Erase & Write\r
+ //\r
+ Status = LibFvbFlashDeviceBlockErase((UINTN)FlashAddress, Length);\r
+ ASSERT_EFI_ERROR(Status);\r
+ if (EFI_ERROR(Status)) {\r
+ LibFvbFlashDeviceBlockLock(FlashAddress, Length, TRUE);\r
+ DEBUG((DEBUG_ERROR, "Flash Erase error\n"));\r
+ return Status;\r
+ }\r
+\r
+ Status = LibFvbFlashDeviceWrite((UINTN)FlashAddress, &Length, Buffer);\r
+ ASSERT_EFI_ERROR(Status);\r
+ if (EFI_ERROR(Status)) {\r
+ LibFvbFlashDeviceBlockLock(FlashAddress, Length, TRUE);\r
+ DEBUG((DEBUG_ERROR, "Flash write error\n"));\r
+ return Status;\r
+ }\r
+\r
+ LibFvbFlashDeviceBlockLock(FlashAddress, Length, TRUE);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Perform microcode write opreation.\r
+\r
+ @param[in] FlashAddress The address of flash device to be accessed.\r
+ @param[in] Buffer The pointer to the data buffer.\r
+ @param[in] Length The length of data buffer in bytes.\r
+\r
+ @retval EFI_SUCCESS The operation returns successfully.\r
+ @retval EFI_WRITE_PROTECTED The flash device is read only.\r
+ @retval EFI_UNSUPPORTED The flash device access is unsupported.\r
+ @retval EFI_INVALID_PARAMETER The input parameter is not valid.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+MicrocodeFlashWrite (\r
+ IN EFI_PHYSICAL_ADDRESS FlashAddress,\r
+ IN VOID *Buffer,\r
+ IN UINTN Length\r
+ )\r
+{\r
+ EFI_PHYSICAL_ADDRESS AlignedFlashAddress;\r
+ VOID *AlignedBuffer;\r
+ UINTN AlignedLength;\r
+ UINTN OffsetHead;\r
+ UINTN OffsetTail;\r
+ EFI_STATUS Status;\r
+\r
+ DEBUG((DEBUG_INFO, "MicrocodeFlashWrite - 0x%x - 0x%x\n", (UINTN)FlashAddress, Length));\r
+\r
+ //\r
+ // Need make buffer 64K aligned to support ERASE\r
+ //\r
+ // [Aligned] FlashAddress [Aligned]\r
+ // | | |\r
+ // V V V\r
+ // +--------------+========+------------+\r
+ // | OffsetHeader | Length | OffsetTail |\r
+ // +--------------+========+------------+\r
+ // ^\r
+ // |<-----------AlignedLength----------->\r
+ // |\r
+ // AlignedFlashAddress\r
+ //\r
+ OffsetHead = FlashAddress & (ALINGED_SIZE - 1);\r
+ OffsetTail = (FlashAddress + Length) & (ALINGED_SIZE - 1);\r
+ if (OffsetTail != 0) {\r
+ OffsetTail = ALINGED_SIZE - OffsetTail;\r
+ }\r
+\r
+ if ((OffsetHead != 0) || (OffsetTail != 0)) {\r
+ AlignedFlashAddress = FlashAddress - OffsetHead;\r
+ AlignedLength = Length + OffsetHead + OffsetTail;\r
+\r
+ AlignedBuffer = AllocatePool(AlignedLength);\r
+ if (AlignedBuffer == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ //\r
+ // Save original buffer\r
+ //\r
+ if (OffsetHead != 0) {\r
+ CopyMem((UINT8 *)AlignedBuffer, (VOID *)AlignedFlashAddress, OffsetHead);\r
+ }\r
+ if (OffsetTail != 0) {\r
+ CopyMem((UINT8 *)AlignedBuffer + OffsetHead + Length, (VOID *)(AlignedFlashAddress + OffsetHead + Length), OffsetTail);\r
+ }\r
+ //\r
+ // Override new buffer\r
+ //\r
+ CopyMem((UINT8 *)AlignedBuffer + OffsetHead, Buffer, Length);\r
+ } else {\r
+ AlignedFlashAddress = FlashAddress;\r
+ AlignedBuffer = Buffer;\r
+ AlignedLength = Length;\r
+ }\r
+\r
+ Status = PerformFlashWrite(\r
+ PlatformFirmwareTypeSystemFirmware,\r
+ AlignedFlashAddress,\r
+ FlashAddressTypeAbsoluteAddress,\r
+ AlignedBuffer,\r
+ AlignedLength\r
+ );\r
+ if ((OffsetHead != 0) || (OffsetTail != 0)) {\r
+ FreePool (AlignedBuffer);\r
+ }\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Platform Flash Access Lib Constructor.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PerformFlashAccessLibConstructor (\r
+ VOID\r
+ )\r
+{\r
+ mInternalFdAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32(PcdFlashAreaBaseAddress);\r
+ DEBUG((DEBUG_INFO, "PcdFlashAreaBaseAddress - 0x%x\n", mInternalFdAddress));\r
+\r
+ return EFI_SUCCESS;\r
+}\r
--- /dev/null
+## @file\r
+# Platform Flash Access library.\r
+#\r
+# Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
+# This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution. The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+##\r
+\r
+[Defines]\r
+ INF_VERSION = 0x00010005\r
+ BASE_NAME = PlatformFlashAccessLib\r
+ FILE_GUID = 31CF9CEC-DA4E-4505-AA20-33364A291A95\r
+ MODULE_TYPE = BASE\r
+ VERSION_STRING = 1.0\r
+ LIBRARY_CLASS = PlatformFlashAccessLib\r
+ LIBRARY_CLASS = MicrocodeFlashAccessLib\r
+ CONSTRUCTOR = PerformFlashAccessLibConstructor\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+# VALID_ARCHITECTURES = IA32 X64 IPF EBC\r
+#\r
+\r
+[Sources]\r
+ PlatformFlashAccessLib.c\r
+\r
+[Packages]\r
+ MdePkg/MdePkg.dec\r
+ MdeModulePkg/MdeModulePkg.dec\r
+ SignedCapsulePkg/SignedCapsulePkg.dec\r
+ Vlv2TbltDevicePkg/PlatformPkg.dec\r
+\r
+[LibraryClasses]\r
+ BaseMemoryLib\r
+ PcdLib\r
+ DebugLib\r
+ FlashDeviceLib\r
+ MemoryAllocationLib\r
+\r
+[Pcd]\r
+ gPlatformModuleTokenSpaceGuid.PcdFlashAreaBaseAddress\r