PlatformFlashAccessLibDxe.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /** @file
  2. Platform Flash Access library.
  3. Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/BaseMemoryLib.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/PcdLib.h>
  11. #include <Library/PlatformFlashAccessLib.h>
  12. #include <Library/UefiBootServicesTableLib.h>
  13. #include <Protocol/Spi.h>
  14. //
  15. // SPI default opcode slots
  16. //
  17. #define SPI_OPCODE_JEDEC_ID_INDEX 0
  18. #define SPI_OPCODE_READ_ID_INDEX 1
  19. #define SPI_OPCODE_WRITE_S_INDEX 2
  20. #define SPI_OPCODE_WRITE_INDEX 3
  21. #define SPI_OPCODE_READ_INDEX 4
  22. #define SPI_OPCODE_ERASE_INDEX 5
  23. #define SPI_OPCODE_READ_S_INDEX 6
  24. #define SPI_OPCODE_CHIP_ERASE_INDEX 7
  25. #define SPI_ERASE_SECTOR_SIZE SIZE_4KB //This is the chipset requirement
  26. STATIC EFI_PHYSICAL_ADDRESS mInternalFdAddress;
  27. EFI_SPI_PROTOCOL *mSpiProtocol;
  28. /**
  29. Writes specified number of bytes from the input buffer to the address
  30. @param[in] WriteAddress The flash address to be written.
  31. @param[in, out] NumBytes The number of bytes.
  32. @param[in] Buffer The data buffer to be written.
  33. @return The status of flash write.
  34. **/
  35. EFI_STATUS
  36. FlashFdWrite (
  37. IN UINTN WriteAddress,
  38. IN OUT UINTN *NumBytes,
  39. IN UINT8 *Buffer
  40. )
  41. {
  42. EFI_STATUS Status;
  43. Status = EFI_SUCCESS;
  44. Status = mSpiProtocol->Execute (
  45. mSpiProtocol,
  46. SPI_OPCODE_WRITE_INDEX, // OpcodeIndex
  47. 0, // PrefixOpcodeIndex
  48. TRUE, // DataCycle
  49. TRUE, // Atomic
  50. TRUE, // ShiftOut
  51. WriteAddress, // Address
  52. (UINT32) (*NumBytes), // Data Number
  53. Buffer,
  54. EnumSpiRegionBios
  55. );
  56. DEBUG((DEBUG_INFO, "FlashFdWrite - 0x%x - %r\n", (UINTN)WriteAddress, Status));
  57. AsmWbinvd ();
  58. return Status;
  59. }
  60. /**
  61. Erase a certain block from address LbaWriteAddress
  62. @param[in] WriteAddress The flash address to be erased.
  63. @return The status of flash erase.
  64. **/
  65. EFI_STATUS
  66. FlashFdErase (
  67. IN UINTN WriteAddress
  68. )
  69. {
  70. EFI_STATUS Status;
  71. Status = mSpiProtocol->Execute (
  72. mSpiProtocol,
  73. SPI_OPCODE_ERASE_INDEX, // OpcodeIndex
  74. 0, // PrefixOpcodeIndex
  75. FALSE, // DataCycle
  76. TRUE, // Atomic
  77. FALSE, // ShiftOut
  78. WriteAddress, // Address
  79. 0, // Data Number
  80. NULL,
  81. EnumSpiRegionBios // SPI_REGION_TYPE
  82. );
  83. DEBUG((DEBUG_INFO, "FlashFdErase - 0x%x - %r\n", (UINTN)WriteAddress, Status));
  84. AsmWbinvd ();
  85. return Status;
  86. }
  87. /**
  88. Perform flash write operation with progress indicator. The start and end
  89. completion percentage values are passed into this function. If the requested
  90. flash write operation is broken up, then completion percentage between the
  91. start and end values may be passed to the provided Progress function. The
  92. caller of this function is required to call the Progress function for the
  93. start and end completion percentage values. This allows the Progress,
  94. StartPercentage, and EndPercentage parameters to be ignored if the requested
  95. flash write operation can not be broken up
  96. @param[in] FirmwareType The type of firmware.
  97. @param[in] FlashAddress The address of flash device to be accessed.
  98. @param[in] FlashAddressType The type of flash device address.
  99. @param[in] Buffer The pointer to the data buffer.
  100. @param[in] Length The length of data buffer in bytes.
  101. @param[in] Progress A function used report the progress of the
  102. firmware update. This is an optional parameter
  103. that may be NULL.
  104. @param[in] StartPercentage The start completion percentage value that may
  105. be used to report progress during the flash
  106. write operation.
  107. @param[in] EndPercentage The end completion percentage value that may
  108. be used to report progress during the flash
  109. write operation.
  110. @retval EFI_SUCCESS The operation returns successfully.
  111. @retval EFI_WRITE_PROTECTED The flash device is read only.
  112. @retval EFI_UNSUPPORTED The flash device access is unsupported.
  113. @retval EFI_INVALID_PARAMETER The input parameter is not valid.
  114. **/
  115. EFI_STATUS
  116. EFIAPI
  117. PerformFlashWriteWithProgress (
  118. IN PLATFORM_FIRMWARE_TYPE FirmwareType,
  119. IN EFI_PHYSICAL_ADDRESS FlashAddress,
  120. IN FLASH_ADDRESS_TYPE FlashAddressType,
  121. IN VOID *Buffer,
  122. IN UINTN Length,
  123. IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress, OPTIONAL
  124. IN UINTN StartPercentage,
  125. IN UINTN EndPercentage
  126. )
  127. {
  128. EFI_STATUS Status;
  129. UINTN SectorNum;
  130. UINTN Index;
  131. UINTN NumBytes;
  132. DEBUG((DEBUG_INFO, "PerformFlashWrite - 0x%x(%x) - 0x%x\n", (UINTN)FlashAddress, (UINTN)FlashAddressType, Length));
  133. if (FlashAddressType == FlashAddressTypeAbsoluteAddress) {
  134. FlashAddress = FlashAddress - mInternalFdAddress;
  135. }
  136. //
  137. // Erase & Write
  138. //
  139. SectorNum = Length / SPI_ERASE_SECTOR_SIZE;
  140. for (Index = 0; Index < SectorNum; Index++){
  141. if (Progress != NULL) {
  142. Progress (StartPercentage + ((Index * (EndPercentage - StartPercentage)) / SectorNum));
  143. }
  144. if (CompareMem(
  145. (UINT8 *)(UINTN)(FlashAddress + mInternalFdAddress) + Index * SPI_ERASE_SECTOR_SIZE,
  146. (UINT8 *)Buffer + Index * SPI_ERASE_SECTOR_SIZE,
  147. SPI_ERASE_SECTOR_SIZE) == 0) {
  148. DEBUG((DEBUG_INFO, "Sector - 0x%x - skip\n", Index));
  149. continue;
  150. }
  151. DEBUG((DEBUG_INFO, "Sector - 0x%x - update...\n", Index));
  152. Status = FlashFdErase (
  153. (UINTN)FlashAddress + Index * SPI_ERASE_SECTOR_SIZE
  154. );
  155. if (Status != EFI_SUCCESS){
  156. break;
  157. }
  158. NumBytes = SPI_ERASE_SECTOR_SIZE;
  159. Status = FlashFdWrite (
  160. (UINTN)FlashAddress + Index * SPI_ERASE_SECTOR_SIZE,
  161. &NumBytes,
  162. (UINT8 *)Buffer + Index * SPI_ERASE_SECTOR_SIZE
  163. );
  164. if (Status != EFI_SUCCESS){
  165. break;
  166. }
  167. }
  168. if (Progress != NULL) {
  169. Progress (EndPercentage);
  170. }
  171. return EFI_SUCCESS;
  172. }
  173. /**
  174. Perform flash write operation.
  175. @param[in] FirmwareType The type of firmware.
  176. @param[in] FlashAddress The address of flash device to be accessed.
  177. @param[in] FlashAddressType The type of flash device address.
  178. @param[in] Buffer The pointer to the data buffer.
  179. @param[in] Length The length of data buffer in bytes.
  180. @retval EFI_SUCCESS The operation returns successfully.
  181. @retval EFI_WRITE_PROTECTED The flash device is read only.
  182. @retval EFI_UNSUPPORTED The flash device access is unsupported.
  183. @retval EFI_INVALID_PARAMETER The input parameter is not valid.
  184. **/
  185. EFI_STATUS
  186. EFIAPI
  187. PerformFlashWrite (
  188. IN PLATFORM_FIRMWARE_TYPE FirmwareType,
  189. IN EFI_PHYSICAL_ADDRESS FlashAddress,
  190. IN FLASH_ADDRESS_TYPE FlashAddressType,
  191. IN VOID *Buffer,
  192. IN UINTN Length
  193. )
  194. {
  195. return PerformFlashWriteWithProgress (
  196. FirmwareType,
  197. FlashAddress,
  198. FlashAddressType,
  199. Buffer,
  200. Length,
  201. NULL,
  202. 0,
  203. 0
  204. );
  205. }
  206. /**
  207. Platform Flash Access Lib Constructor.
  208. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  209. @param[in] SystemTable A pointer to the EFI System Table.
  210. @retval EFI_SUCCESS Constructor returns successfully.
  211. **/
  212. EFI_STATUS
  213. EFIAPI
  214. PerformFlashAccessLibConstructor (
  215. IN EFI_HANDLE ImageHandle,
  216. IN EFI_SYSTEM_TABLE *SystemTable
  217. )
  218. {
  219. EFI_STATUS Status;
  220. mInternalFdAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32(PcdFlashAreaBaseAddress);
  221. DEBUG((DEBUG_INFO, "PcdFlashAreaBaseAddress - 0x%x\n", mInternalFdAddress));
  222. Status = gBS->LocateProtocol(&gEfiSpiProtocolGuid, NULL, (VOID **)&mSpiProtocol);
  223. ASSERT_EFI_ERROR(Status);
  224. return EFI_SUCCESS;
  225. }