PlatformFlashAccessLibDxe.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /** @file
  2. Platform Flash Access library.
  3. Copyright (c) 2018, Hisilicon Limited. All rights reserved.
  4. Copyright (c) 2018, Linaro Limited. All rights reserved.
  5. Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
  6. This program and the accompanying materials
  7. are licensed and made available under the terms and conditions of the BSD License
  8. which accompanies this distribution. The full text of the license may be found at
  9. http://opensource.org/licenses/bsd-license.php
  10. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  12. **/
  13. #include <PiDxe.h>
  14. #include <Library/BaseLib.h>
  15. #include <Library/BaseMemoryLib.h>
  16. #include <Library/DebugLib.h>
  17. #include <Library/PcdLib.h>
  18. #include <Library/PlatformFlashAccessLib.h>
  19. #include <Library/UefiBootServicesTableLib.h>
  20. #include <Protocol/HisiSpiFlashProtocol.h>
  21. STATIC EFI_PHYSICAL_ADDRESS mInternalFdAddress;
  22. STATIC EFI_PHYSICAL_ADDRESS mSFCMEM0BaseAddress;
  23. STATIC HISI_SPI_FLASH_PROTOCOL *mSpiProtocol;
  24. /**
  25. Perform flash write opreation.
  26. @param[in] FirmwareType The type of firmware.
  27. @param[in] FlashAddress The address of flash device to be accessed.
  28. @param[in] FlashAddressType The type of flash device address.
  29. @param[in] Buffer The pointer to the data buffer.
  30. @param[in] Length The length of data buffer in bytes.
  31. @retval EFI_SUCCESS The operation returns successfully.
  32. @retval EFI_WRITE_PROTECTED The flash device is read only.
  33. @retval EFI_UNSUPPORTED The flash device access is unsupported.
  34. @retval EFI_INVALID_PARAMETER The input parameter is not valid.
  35. **/
  36. EFI_STATUS
  37. EFIAPI
  38. PerformFlashWrite (
  39. IN PLATFORM_FIRMWARE_TYPE FirmwareType,
  40. IN EFI_PHYSICAL_ADDRESS FlashAddress,
  41. IN FLASH_ADDRESS_TYPE FlashAddressType,
  42. IN VOID *Buffer,
  43. IN UINTN Length
  44. )
  45. {
  46. UINT32 RomAddress;
  47. EFI_STATUS Status;
  48. DEBUG ((DEBUG_INFO,
  49. "PerformFlashWrite - 0x%x(%x) - 0x%x\n",
  50. (UINTN)FlashAddress,
  51. (UINTN)FlashAddressType,
  52. Length));
  53. if (FlashAddressType == FlashAddressTypeAbsoluteAddress) {
  54. FlashAddress = FlashAddress - mInternalFdAddress;
  55. }
  56. RomAddress = (UINT32)FlashAddress + (mInternalFdAddress - mSFCMEM0BaseAddress);
  57. DEBUG ((DEBUG_INFO, "Erase and Write Flash Start\n"));
  58. Status = mSpiProtocol->EraseWrite (
  59. mSpiProtocol,
  60. (UINT32) RomAddress,
  61. (UINT8 *)Buffer,
  62. (UINT32) Length
  63. );
  64. if (EFI_ERROR (Status)) {
  65. DEBUG ((DEBUG_ERROR, "Erase and Write Status = %r \n", Status));
  66. }
  67. return Status;
  68. }
  69. /**
  70. Platform Flash Access Lib Constructor.
  71. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  72. @param[in] SystemTable A pointer to the EFI System Table.
  73. @retval EFI_SUCCESS Constructor returns successfully.
  74. **/
  75. EFI_STATUS
  76. EFIAPI
  77. PerformFlashAccessLibConstructor (
  78. IN EFI_HANDLE ImageHandle,
  79. IN EFI_SYSTEM_TABLE *SystemTable
  80. )
  81. {
  82. EFI_STATUS Status;
  83. mInternalFdAddress = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFdBaseAddress);
  84. mSFCMEM0BaseAddress = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdSFCMEM0BaseAddress);
  85. DEBUG ((DEBUG_INFO,
  86. "PcdFlashAreaBaseAddress - 0x%x, PcdSFCMEM0BaseAddress - 0x%x \n",
  87. mInternalFdAddress,
  88. mSFCMEM0BaseAddress));
  89. Status = gBS->LocateProtocol (
  90. &gHisiSpiFlashProtocolGuid,
  91. NULL,
  92. (VOID **)&mSpiProtocol);
  93. if (EFI_ERROR (Status)) {
  94. DEBUG ((DEBUG_ERROR,
  95. "LocateProtocol gHisiSpiFlashProtocolGuid Status = %r \n",
  96. Status));
  97. }
  98. return Status;
  99. }