RamFlash.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /** @file
  2. Copyright (c) 2019, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include <Library/BaseMemoryLib.h>
  6. #include <Library/DebugLib.h>
  7. #include <Library/IoLib.h>
  8. #include <Library/PcdLib.h>
  9. #include "RamFlash.h"
  10. VOID *mFlashBase;
  11. STATIC UINTN mFdBlockSize = 0;
  12. STATIC UINTN mFdBlockCount = 0;
  13. STATIC
  14. UINT8*
  15. RamFlashPtr (
  16. IN EFI_LBA Lba,
  17. IN UINTN Offset
  18. )
  19. {
  20. return mFlashBase + ((UINTN)Lba * mFdBlockSize) + Offset;
  21. }
  22. /**
  23. Read from Ram Flash
  24. @param[in] Lba The starting logical block index to read from.
  25. @param[in] Offset Offset into the block at which to begin reading.
  26. @param[in] NumBytes On input, indicates the requested read size. On
  27. output, indicates the actual number of bytes read
  28. @param[in] Buffer Pointer to the buffer to read into.
  29. **/
  30. EFI_STATUS
  31. RamFlashRead (
  32. IN EFI_LBA Lba,
  33. IN UINTN Offset,
  34. IN UINTN *NumBytes,
  35. IN UINT8 *Buffer
  36. )
  37. {
  38. UINT8 *Ptr;
  39. //
  40. // Only write to the first 64k. We don't bother saving the FTW Spare
  41. // block into the flash memory.
  42. //
  43. if (Lba >= mFdBlockCount) {
  44. return EFI_INVALID_PARAMETER;
  45. }
  46. //
  47. // Get flash address
  48. //
  49. Ptr = (UINT8*) RamFlashPtr (Lba, Offset);
  50. CopyMem (Buffer, Ptr, *NumBytes);
  51. return EFI_SUCCESS;
  52. }
  53. /**
  54. Write to Ram Flash
  55. @param[in] Lba The starting logical block index to write to.
  56. @param[in] Offset Offset into the block at which to begin writing.
  57. @param[in] NumBytes On input, indicates the requested write size. On
  58. output, indicates the actual number of bytes written
  59. @param[in] Buffer Pointer to the data to write.
  60. **/
  61. EFI_STATUS
  62. RamFlashWrite (
  63. IN EFI_LBA Lba,
  64. IN UINTN Offset,
  65. IN UINTN *NumBytes,
  66. IN UINT8 *Buffer
  67. )
  68. {
  69. UINT8 *Ptr;
  70. UINTN i;
  71. //
  72. // Only write to the first 64k. We don't bother saving the FTW Spare
  73. // block into the flash memory.
  74. //
  75. if (Lba >= mFdBlockCount) {
  76. return EFI_INVALID_PARAMETER;
  77. }
  78. //
  79. // Program flash
  80. //
  81. Ptr = RamFlashPtr (Lba, Offset);
  82. for (i = 0; i < *NumBytes; i++) {
  83. MmioWrite8((UINTN)Ptr, Buffer[i]);
  84. Ptr ++;
  85. }
  86. return EFI_SUCCESS;
  87. }
  88. /**
  89. Erase a Ram Flash block
  90. @param Lba The logical block index to erase.
  91. **/
  92. EFI_STATUS
  93. RamFlashEraseBlock (
  94. IN EFI_LBA Lba
  95. )
  96. {
  97. return EFI_SUCCESS;
  98. }
  99. /**
  100. Initializes Ram flash memory support
  101. @retval EFI_WRITE_PROTECTED The Ram flash device is not present.
  102. @retval EFI_SUCCESS The Ram flash device is supported.
  103. **/
  104. EFI_STATUS
  105. RamFlashInitialize (
  106. VOID
  107. )
  108. {
  109. mFlashBase = (UINT8*)(UINTN) PcdGet32 (PcdVariableFdBaseAddress);
  110. mFdBlockSize = PcdGet32 (PcdVariableFdBlockSize);
  111. ASSERT(PcdGet32 (PcdVariableFdSize) % mFdBlockSize == 0);
  112. mFdBlockCount = PcdGet32 (PcdVariableFdSize) / mFdBlockSize;
  113. return EFI_SUCCESS;
  114. }