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