]> git.proxmox.com Git - mirror_edk2.git/blob - StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c
f82cdb3ba816a5a36d10fcdf13fc6bd73f76820f
[mirror_edk2.git] / StandaloneMmPkg / Library / StandaloneMmMemLib / StandaloneMmMemLib.c
1 /** @file
2 Instance of MM memory check library.
3
4 MM memory check library library implementation. This library consumes MM_ACCESS_PROTOCOL
5 to get MMRAM information. In order to use this library instance, the platform should produce
6 all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
7 and MM driver) and/or specific dedicated hardware.
8
9 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
10 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
11
12 SPDX-License-Identifier: BSD-2-Clause-Patent
13
14 **/
15
16
17 #include <PiMm.h>
18
19 #include <Library/BaseLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22
23 EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
24 UINTN mMmMemLibInternalMmramCount;
25
26 //
27 // Maximum support address used to check input buffer
28 //
29 EFI_PHYSICAL_ADDRESS mMmMemLibInternalMaximumSupportAddress = 0;
30
31 /**
32 Calculate and save the maximum support address.
33
34 **/
35 VOID
36 MmMemLibInternalCalculateMaximumSupportAddress (
37 VOID
38 );
39
40 /**
41 This function check if the buffer is valid per processor architecture and not overlap with MMRAM.
42
43 @param Buffer The buffer start address to be checked.
44 @param Length The buffer length to be checked.
45
46 @retval TRUE This buffer is valid per processor architecture and not overlap with MMRAM.
47 @retval FALSE This buffer is not valid per processor architecture or overlap with MMRAM.
48 **/
49 BOOLEAN
50 EFIAPI
51 MmIsBufferOutsideMmValid (
52 IN EFI_PHYSICAL_ADDRESS Buffer,
53 IN UINT64 Length
54 )
55 {
56 UINTN Index;
57
58 //
59 // Check override.
60 // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.
61 //
62 if ((Length > mMmMemLibInternalMaximumSupportAddress) ||
63 (Buffer > mMmMemLibInternalMaximumSupportAddress) ||
64 ((Length != 0) && (Buffer > (mMmMemLibInternalMaximumSupportAddress - (Length - 1)))) ) {
65 //
66 // Overflow happen
67 //
68 DEBUG ((
69 DEBUG_ERROR,
70 "MmIsBufferOutsideMmValid: Overflow: Buffer (0x%lx) - Length (0x%lx), MaximumSupportAddress (0x%lx)\n",
71 Buffer,
72 Length,
73 mMmMemLibInternalMaximumSupportAddress
74 ));
75 return FALSE;
76 }
77
78 for (Index = 0; Index < mMmMemLibInternalMmramCount; Index ++) {
79 if (((Buffer >= mMmMemLibInternalMmramRanges[Index].CpuStart) &&
80 (Buffer < mMmMemLibInternalMmramRanges[Index].CpuStart + mMmMemLibInternalMmramRanges[Index].PhysicalSize)) ||
81 ((mMmMemLibInternalMmramRanges[Index].CpuStart >= Buffer) &&
82 (mMmMemLibInternalMmramRanges[Index].CpuStart < Buffer + Length))) {
83 DEBUG ((
84 DEBUG_ERROR,
85 "MmIsBufferOutsideMmValid: Overlap: Buffer (0x%lx) - Length (0x%lx), ",
86 Buffer,
87 Length
88 ));
89 DEBUG ((
90 DEBUG_ERROR,
91 "CpuStart (0x%lx) - PhysicalSize (0x%lx)\n",
92 mMmMemLibInternalMmramRanges[Index].CpuStart,
93 mMmMemLibInternalMmramRanges[Index].PhysicalSize
94 ));
95 return FALSE;
96 }
97 }
98
99 return TRUE;
100 }
101
102 /**
103 Copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
104
105 This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
106 It checks if source buffer is valid per processor architecture and not overlap with MMRAM.
107 If the check passes, it copies memory and returns EFI_SUCCESS.
108 If the check fails, it return EFI_SECURITY_VIOLATION.
109 The implementation must be reentrant.
110
111 @param DestinationBuffer The pointer to the destination buffer of the memory copy.
112 @param SourceBuffer The pointer to the source buffer of the memory copy.
113 @param Length The number of bytes to copy from SourceBuffer to DestinationBuffer.
114
115 @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
116 @retval EFI_SUCCESS Memory is copied.
117
118 **/
119 EFI_STATUS
120 EFIAPI
121 MmCopyMemToMmram (
122 OUT VOID *DestinationBuffer,
123 IN CONST VOID *SourceBuffer,
124 IN UINTN Length
125 )
126 {
127 if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
128 DEBUG ((DEBUG_ERROR, "MmCopyMemToMmram: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));
129 return EFI_SECURITY_VIOLATION;
130 }
131 CopyMem (DestinationBuffer, SourceBuffer, Length);
132 return EFI_SUCCESS;
133 }
134
135 /**
136 Copies a source buffer (MMRAM) to a destination buffer (NON-MMRAM).
137
138 This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
139 It checks if destination buffer is valid per processor architecture and not overlap with MMRAM.
140 If the check passes, it copies memory and returns EFI_SUCCESS.
141 If the check fails, it returns EFI_SECURITY_VIOLATION.
142 The implementation must be reentrant.
143
144 @param DestinationBuffer The pointer to the destination buffer of the memory copy.
145 @param SourceBuffer The pointer to the source buffer of the memory copy.
146 @param Length The number of bytes to copy from SourceBuffer to DestinationBuffer.
147
148 @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
149 @retval EFI_SUCCESS Memory is copied.
150
151 **/
152 EFI_STATUS
153 EFIAPI
154 MmCopyMemFromMmram (
155 OUT VOID *DestinationBuffer,
156 IN CONST VOID *SourceBuffer,
157 IN UINTN Length
158 )
159 {
160 if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
161 DEBUG ((DEBUG_ERROR, "MmCopyMemFromMmram: Security Violation: Destination (0x%x), Length (0x%x)\n",
162 DestinationBuffer, Length));
163 return EFI_SECURITY_VIOLATION;
164 }
165 CopyMem (DestinationBuffer, SourceBuffer, Length);
166 return EFI_SUCCESS;
167 }
168
169 /**
170 Copies a source buffer (NON-MMRAM) to a destination buffer (NON-MMRAM).
171
172 This function copies a source buffer (non-MMRAM) to a destination buffer (MMRAM).
173 It checks if source buffer and destination buffer are valid per processor architecture and not overlap with MMRAM.
174 If the check passes, it copies memory and returns EFI_SUCCESS.
175 If the check fails, it returns EFI_SECURITY_VIOLATION.
176 The implementation must be reentrant, and it must handle the case where source buffer overlaps destination buffer.
177
178 @param DestinationBuffer The pointer to the destination buffer of the memory copy.
179 @param SourceBuffer The pointer to the source buffer of the memory copy.
180 @param Length The number of bytes to copy from SourceBuffer to DestinationBuffer.
181
182 @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.
183 @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.
184 @retval EFI_SUCCESS Memory is copied.
185
186 **/
187 EFI_STATUS
188 EFIAPI
189 MmCopyMem (
190 OUT VOID *DestinationBuffer,
191 IN CONST VOID *SourceBuffer,
192 IN UINTN Length
193 )
194 {
195 if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {
196 DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Destination (0x%x), Length (0x%x)\n",
197 DestinationBuffer, Length));
198 return EFI_SECURITY_VIOLATION;
199 }
200 if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {
201 DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));
202 return EFI_SECURITY_VIOLATION;
203 }
204 CopyMem (DestinationBuffer, SourceBuffer, Length);
205 return EFI_SUCCESS;
206 }
207
208 /**
209 Fills a target buffer (NON-MMRAM) with a byte value.
210
211 This function fills a target buffer (non-MMRAM) with a byte value.
212 It checks if target buffer is valid per processor architecture and not overlap with MMRAM.
213 If the check passes, it fills memory and returns EFI_SUCCESS.
214 If the check fails, it returns EFI_SECURITY_VIOLATION.
215
216 @param Buffer The memory to set.
217 @param Length The number of bytes to set.
218 @param Value The value with which to fill Length bytes of Buffer.
219
220 @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor architecture or overlap with MMRAM.
221 @retval EFI_SUCCESS Memory is set.
222
223 **/
224 EFI_STATUS
225 EFIAPI
226 MmSetMem (
227 OUT VOID *Buffer,
228 IN UINTN Length,
229 IN UINT8 Value
230 )
231 {
232 if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Length)) {
233 DEBUG ((DEBUG_ERROR, "MmSetMem: Security Violation: Source (0x%x), Length (0x%x)\n", Buffer, Length));
234 return EFI_SECURITY_VIOLATION;
235 }
236 SetMem (Buffer, Length, Value);
237 return EFI_SUCCESS;
238 }
239
240 /**
241 The constructor function initializes the Mm Mem library
242
243 @param ImageHandle The firmware allocated handle for the EFI image.
244 @param SystemTable A pointer to the EFI System Table.
245
246 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
247
248 **/
249 EFI_STATUS
250 EFIAPI
251 MemLibConstructor (
252 IN EFI_HANDLE ImageHandle,
253 IN EFI_MM_SYSTEM_TABLE *MmSystemTable
254 )
255 {
256
257 //
258 // Calculate and save maximum support address
259 //
260 MmMemLibInternalCalculateMaximumSupportAddress ();
261
262 return EFI_SUCCESS;
263 }