Commit | Line | Data |
---|---|---|
d425764e JY |
1 | /** @file\r |
2 | Instance of SMM memory check library.\r | |
3 | \r | |
4 | SMM memory check library library implementation. This library consumes SMM_ACCESS2_PROTOCOL\r | |
5 | to get SMRAM information. In order to use this library instance, the platform should produce\r | |
6 | all SMRAM range via SMM_ACCESS2_PROTOCOL, including the range for firmware (like SMM Core\r | |
7 | and SMM driver) and/or specific dedicated hardware.\r | |
8 | \r | |
9095d37b | 9 | Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r |
d425764e JY |
10 | This program and the accompanying materials\r |
11 | are licensed and made available under the terms and conditions of the BSD License\r | |
12 | which accompanies this distribution. The full text of the license may be found at\r | |
13 | http://opensource.org/licenses/bsd-license.php\r | |
14 | \r | |
15 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
16 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
17 | \r | |
18 | **/\r | |
19 | \r | |
20 | \r | |
21 | #include <PiSmm.h>\r | |
22 | \r | |
23 | #include <Library/BaseLib.h>\r | |
24 | #include <Library/BaseMemoryLib.h>\r | |
25 | #include <Library/DebugLib.h>\r | |
26 | #include <Library/MemoryAllocationLib.h>\r | |
27 | #include <Library/UefiBootServicesTableLib.h>\r | |
233ffa90 | 28 | #include <Library/DxeServicesTableLib.h>\r |
d425764e JY |
29 | #include <Library/SmmServicesTableLib.h>\r |
30 | #include <Library/HobLib.h>\r | |
31 | #include <Protocol/SmmAccess2.h>\r | |
91f51fcc JY |
32 | #include <Protocol/SmmReadyToLock.h>\r |
33 | #include <Protocol/SmmEndOfDxe.h>\r | |
34 | \r | |
233ffa90 JY |
35 | //\r |
36 | // attributes for reserved memory before it is promoted to system memory\r | |
37 | //\r | |
38 | #define EFI_MEMORY_PRESENT 0x0100000000000000ULL\r | |
39 | #define EFI_MEMORY_INITIALIZED 0x0200000000000000ULL\r | |
40 | #define EFI_MEMORY_TESTED 0x0400000000000000ULL\r | |
41 | \r | |
91f51fcc JY |
42 | #define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \\r |
43 | ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))\r | |
d425764e JY |
44 | \r |
45 | EFI_SMRAM_DESCRIPTOR *mSmmMemLibInternalSmramRanges;\r | |
46 | UINTN mSmmMemLibInternalSmramCount;\r | |
47 | \r | |
48 | //\r | |
49 | // Maximum support address used to check input buffer\r | |
50 | //\r | |
51 | EFI_PHYSICAL_ADDRESS mSmmMemLibInternalMaximumSupportAddress = 0;\r | |
52 | \r | |
91f51fcc JY |
53 | UINTN mMemoryMapEntryCount;\r |
54 | EFI_MEMORY_DESCRIPTOR *mMemoryMap;\r | |
55 | UINTN mDescriptorSize;\r | |
56 | \r | |
233ffa90 JY |
57 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmMemLibGcdMemSpace = NULL;\r |
58 | UINTN mSmmMemLibGcdMemNumberOfDesc = 0;\r | |
59 | \r | |
91f51fcc JY |
60 | VOID *mRegistrationEndOfDxe;\r |
61 | VOID *mRegistrationReadyToLock;\r | |
62 | \r | |
233ffa90 | 63 | BOOLEAN mSmmMemLibSmmReadyToLock = FALSE;\r |
91f51fcc | 64 | \r |
d425764e | 65 | /**\r |
2a93f2c3 | 66 | Calculate and save the maximum support address.\r |
d425764e JY |
67 | \r |
68 | **/\r | |
69 | VOID\r | |
2a93f2c3 | 70 | SmmMemLibInternalCalculateMaximumSupportAddress (\r |
d425764e JY |
71 | VOID\r |
72 | )\r | |
73 | {\r | |
74 | VOID *Hob;\r | |
75 | UINT32 RegEax;\r | |
76 | UINT8 PhysicalAddressBits;\r | |
77 | \r | |
78 | //\r | |
79 | // Get physical address bits supported.\r | |
80 | //\r | |
81 | Hob = GetFirstHob (EFI_HOB_TYPE_CPU);\r | |
82 | if (Hob != NULL) {\r | |
83 | PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;\r | |
84 | } else {\r | |
85 | AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r | |
86 | if (RegEax >= 0x80000008) {\r | |
87 | AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r | |
88 | PhysicalAddressBits = (UINT8) RegEax;\r | |
89 | } else {\r | |
90 | PhysicalAddressBits = 36;\r | |
91 | }\r | |
92 | }\r | |
93 | //\r | |
94 | // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.\r | |
95 | //\r | |
96 | ASSERT (PhysicalAddressBits <= 52);\r | |
97 | if (PhysicalAddressBits > 48) {\r | |
98 | PhysicalAddressBits = 48;\r | |
99 | }\r | |
9095d37b | 100 | \r |
d425764e | 101 | //\r |
9095d37b | 102 | // Save the maximum support address in one global variable\r |
d425764e JY |
103 | //\r |
104 | mSmmMemLibInternalMaximumSupportAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) - 1);\r | |
105 | DEBUG ((EFI_D_INFO, "mSmmMemLibInternalMaximumSupportAddress = 0x%lx\n", mSmmMemLibInternalMaximumSupportAddress));\r | |
106 | }\r | |
107 | \r | |
108 | /**\r | |
109 | This function check if the buffer is valid per processor architecture and not overlap with SMRAM.\r | |
110 | \r | |
111 | @param Buffer The buffer start address to be checked.\r | |
112 | @param Length The buffer length to be checked.\r | |
113 | \r | |
114 | @retval TRUE This buffer is valid per processor architecture and not overlap with SMRAM.\r | |
115 | @retval FALSE This buffer is not valid per processor architecture or overlap with SMRAM.\r | |
116 | **/\r | |
117 | BOOLEAN\r | |
118 | EFIAPI\r | |
119 | SmmIsBufferOutsideSmmValid (\r | |
120 | IN EFI_PHYSICAL_ADDRESS Buffer,\r | |
121 | IN UINT64 Length\r | |
122 | )\r | |
123 | {\r | |
124 | UINTN Index;\r | |
9095d37b | 125 | \r |
d425764e JY |
126 | //\r |
127 | // Check override.\r | |
128 | // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.\r | |
129 | //\r | |
130 | if ((Length > mSmmMemLibInternalMaximumSupportAddress) ||\r | |
131 | (Buffer > mSmmMemLibInternalMaximumSupportAddress) ||\r | |
132 | ((Length != 0) && (Buffer > (mSmmMemLibInternalMaximumSupportAddress - (Length - 1)))) ) {\r | |
133 | //\r | |
134 | // Overflow happen\r | |
135 | //\r | |
136 | DEBUG ((\r | |
137 | EFI_D_ERROR,\r | |
138 | "SmmIsBufferOutsideSmmValid: Overflow: Buffer (0x%lx) - Length (0x%lx), MaximumSupportAddress (0x%lx)\n",\r | |
139 | Buffer,\r | |
140 | Length,\r | |
141 | mSmmMemLibInternalMaximumSupportAddress\r | |
142 | ));\r | |
143 | return FALSE;\r | |
144 | }\r | |
9095d37b | 145 | \r |
d425764e JY |
146 | for (Index = 0; Index < mSmmMemLibInternalSmramCount; Index ++) {\r |
147 | if (((Buffer >= mSmmMemLibInternalSmramRanges[Index].CpuStart) && (Buffer < mSmmMemLibInternalSmramRanges[Index].CpuStart + mSmmMemLibInternalSmramRanges[Index].PhysicalSize)) ||\r | |
148 | ((mSmmMemLibInternalSmramRanges[Index].CpuStart >= Buffer) && (mSmmMemLibInternalSmramRanges[Index].CpuStart < Buffer + Length))) {\r | |
149 | DEBUG ((\r | |
150 | EFI_D_ERROR,\r | |
151 | "SmmIsBufferOutsideSmmValid: Overlap: Buffer (0x%lx) - Length (0x%lx), ",\r | |
152 | Buffer,\r | |
153 | Length\r | |
154 | ));\r | |
155 | DEBUG ((\r | |
156 | EFI_D_ERROR,\r | |
157 | "CpuStart (0x%lx) - PhysicalSize (0x%lx)\n",\r | |
158 | mSmmMemLibInternalSmramRanges[Index].CpuStart,\r | |
159 | mSmmMemLibInternalSmramRanges[Index].PhysicalSize\r | |
160 | ));\r | |
161 | return FALSE;\r | |
162 | }\r | |
163 | }\r | |
164 | \r | |
91f51fcc JY |
165 | //\r |
166 | // Check override for Valid Communication Region\r | |
167 | //\r | |
233ffa90 | 168 | if (mSmmMemLibSmmReadyToLock) {\r |
91f51fcc JY |
169 | EFI_MEMORY_DESCRIPTOR *MemoryMap;\r |
170 | BOOLEAN InValidCommunicationRegion;\r | |
9095d37b | 171 | \r |
91f51fcc JY |
172 | InValidCommunicationRegion = FALSE;\r |
173 | MemoryMap = mMemoryMap;\r | |
174 | for (Index = 0; Index < mMemoryMapEntryCount; Index++) {\r | |
175 | if ((Buffer >= MemoryMap->PhysicalStart) &&\r | |
176 | (Buffer + Length <= MemoryMap->PhysicalStart + LShiftU64 (MemoryMap->NumberOfPages, EFI_PAGE_SHIFT))) {\r | |
177 | InValidCommunicationRegion = TRUE;\r | |
178 | }\r | |
179 | MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, mDescriptorSize);\r | |
180 | }\r | |
181 | \r | |
182 | if (!InValidCommunicationRegion) {\r | |
183 | DEBUG ((\r | |
184 | EFI_D_ERROR,\r | |
233ffa90 | 185 | "SmmIsBufferOutsideSmmValid: Not in ValidCommunicationRegion: Buffer (0x%lx) - Length (0x%lx)\n",\r |
91f51fcc JY |
186 | Buffer,\r |
187 | Length\r | |
188 | ));\r | |
91f51fcc JY |
189 | return FALSE;\r |
190 | }\r | |
233ffa90 JY |
191 | \r |
192 | //\r | |
193 | // Check untested memory as invalid communication buffer.\r | |
194 | //\r | |
195 | for (Index = 0; Index < mSmmMemLibGcdMemNumberOfDesc; Index++) {\r | |
196 | if (((Buffer >= mSmmMemLibGcdMemSpace[Index].BaseAddress) && (Buffer < mSmmMemLibGcdMemSpace[Index].BaseAddress + mSmmMemLibGcdMemSpace[Index].Length)) ||\r | |
197 | ((mSmmMemLibGcdMemSpace[Index].BaseAddress >= Buffer) && (mSmmMemLibGcdMemSpace[Index].BaseAddress < Buffer + Length))) {\r | |
198 | DEBUG ((\r | |
199 | EFI_D_ERROR,\r | |
200 | "SmmIsBufferOutsideSmmValid: In Untested Memory Region: Buffer (0x%lx) - Length (0x%lx)\n",\r | |
201 | Buffer,\r | |
202 | Length\r | |
203 | ));\r | |
204 | return FALSE;\r | |
205 | }\r | |
206 | }\r | |
91f51fcc | 207 | }\r |
d425764e JY |
208 | return TRUE;\r |
209 | }\r | |
210 | \r | |
211 | /**\r | |
212 | Copies a source buffer (non-SMRAM) to a destination buffer (SMRAM).\r | |
213 | \r | |
214 | This function copies a source buffer (non-SMRAM) to a destination buffer (SMRAM).\r | |
215 | It checks if source buffer is valid per processor architecture and not overlap with SMRAM.\r | |
216 | If the check passes, it copies memory and returns EFI_SUCCESS.\r | |
217 | If the check fails, it return EFI_SECURITY_VIOLATION.\r | |
218 | The implementation must be reentrant.\r | |
219 | \r | |
220 | @param DestinationBuffer The pointer to the destination buffer of the memory copy.\r | |
221 | @param SourceBuffer The pointer to the source buffer of the memory copy.\r | |
222 | @param Length The number of bytes to copy from SourceBuffer to DestinationBuffer.\r | |
223 | \r | |
224 | @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with SMRAM.\r | |
225 | @retval EFI_SUCCESS Memory is copied.\r | |
226 | \r | |
227 | **/\r | |
228 | EFI_STATUS\r | |
229 | EFIAPI\r | |
230 | SmmCopyMemToSmram (\r | |
231 | OUT VOID *DestinationBuffer,\r | |
232 | IN CONST VOID *SourceBuffer,\r | |
233 | IN UINTN Length\r | |
234 | )\r | |
235 | {\r | |
236 | if (!SmmIsBufferOutsideSmmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {\r | |
237 | DEBUG ((EFI_D_ERROR, "SmmCopyMemToSmram: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));\r | |
238 | return EFI_SECURITY_VIOLATION;\r | |
239 | }\r | |
240 | CopyMem (DestinationBuffer, SourceBuffer, Length);\r | |
241 | return EFI_SUCCESS;\r | |
242 | }\r | |
243 | \r | |
244 | /**\r | |
245 | Copies a source buffer (SMRAM) to a destination buffer (NON-SMRAM).\r | |
246 | \r | |
247 | This function copies a source buffer (non-SMRAM) to a destination buffer (SMRAM).\r | |
248 | It checks if destination buffer is valid per processor architecture and not overlap with SMRAM.\r | |
249 | If the check passes, it copies memory and returns EFI_SUCCESS.\r | |
250 | If the check fails, it returns EFI_SECURITY_VIOLATION.\r | |
251 | The implementation must be reentrant.\r | |
9095d37b | 252 | \r |
d425764e JY |
253 | @param DestinationBuffer The pointer to the destination buffer of the memory copy.\r |
254 | @param SourceBuffer The pointer to the source buffer of the memory copy.\r | |
255 | @param Length The number of bytes to copy from SourceBuffer to DestinationBuffer.\r | |
256 | \r | |
257 | @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with SMRAM.\r | |
258 | @retval EFI_SUCCESS Memory is copied.\r | |
259 | \r | |
260 | **/\r | |
261 | EFI_STATUS\r | |
262 | EFIAPI\r | |
263 | SmmCopyMemFromSmram (\r | |
264 | OUT VOID *DestinationBuffer,\r | |
265 | IN CONST VOID *SourceBuffer,\r | |
266 | IN UINTN Length\r | |
267 | )\r | |
268 | {\r | |
269 | if (!SmmIsBufferOutsideSmmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {\r | |
270 | DEBUG ((EFI_D_ERROR, "SmmCopyMemFromSmram: Security Violation: Destination (0x%x), Length (0x%x)\n", DestinationBuffer, Length));\r | |
271 | return EFI_SECURITY_VIOLATION;\r | |
272 | }\r | |
273 | CopyMem (DestinationBuffer, SourceBuffer, Length);\r | |
274 | return EFI_SUCCESS;\r | |
275 | }\r | |
276 | \r | |
277 | /**\r | |
278 | Copies a source buffer (NON-SMRAM) to a destination buffer (NON-SMRAM).\r | |
279 | \r | |
280 | This function copies a source buffer (non-SMRAM) to a destination buffer (SMRAM).\r | |
281 | It checks if source buffer and destination buffer are valid per processor architecture and not overlap with SMRAM.\r | |
282 | If the check passes, it copies memory and returns EFI_SUCCESS.\r | |
283 | If the check fails, it returns EFI_SECURITY_VIOLATION.\r | |
284 | The implementation must be reentrant, and it must handle the case where source buffer overlaps destination buffer.\r | |
9095d37b | 285 | \r |
d425764e JY |
286 | @param DestinationBuffer The pointer to the destination buffer of the memory copy.\r |
287 | @param SourceBuffer The pointer to the source buffer of the memory copy.\r | |
288 | @param Length The number of bytes to copy from SourceBuffer to DestinationBuffer.\r | |
289 | \r | |
290 | @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with SMRAM.\r | |
291 | @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with SMRAM.\r | |
292 | @retval EFI_SUCCESS Memory is copied.\r | |
293 | \r | |
294 | **/\r | |
295 | EFI_STATUS\r | |
296 | EFIAPI\r | |
297 | SmmCopyMem (\r | |
298 | OUT VOID *DestinationBuffer,\r | |
299 | IN CONST VOID *SourceBuffer,\r | |
300 | IN UINTN Length\r | |
301 | )\r | |
302 | {\r | |
303 | if (!SmmIsBufferOutsideSmmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {\r | |
304 | DEBUG ((EFI_D_ERROR, "SmmCopyMem: Security Violation: Destination (0x%x), Length (0x%x)\n", DestinationBuffer, Length));\r | |
305 | return EFI_SECURITY_VIOLATION;\r | |
306 | }\r | |
307 | if (!SmmIsBufferOutsideSmmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {\r | |
308 | DEBUG ((EFI_D_ERROR, "SmmCopyMem: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));\r | |
309 | return EFI_SECURITY_VIOLATION;\r | |
310 | }\r | |
311 | CopyMem (DestinationBuffer, SourceBuffer, Length);\r | |
312 | return EFI_SUCCESS;\r | |
313 | }\r | |
314 | \r | |
315 | /**\r | |
316 | Fills a target buffer (NON-SMRAM) with a byte value.\r | |
317 | \r | |
318 | This function fills a target buffer (non-SMRAM) with a byte value.\r | |
319 | It checks if target buffer is valid per processor architecture and not overlap with SMRAM.\r | |
320 | If the check passes, it fills memory and returns EFI_SUCCESS.\r | |
321 | If the check fails, it returns EFI_SECURITY_VIOLATION.\r | |
9095d37b | 322 | \r |
d425764e JY |
323 | @param Buffer The memory to set.\r |
324 | @param Length The number of bytes to set.\r | |
325 | @param Value The value with which to fill Length bytes of Buffer.\r | |
9095d37b | 326 | \r |
d425764e JY |
327 | @retval EFI_SECURITY_VIOLATION The Buffer is invalid per processor architecture or overlap with SMRAM.\r |
328 | @retval EFI_SUCCESS Memory is set.\r | |
329 | \r | |
330 | **/\r | |
331 | EFI_STATUS\r | |
332 | EFIAPI\r | |
333 | SmmSetMem (\r | |
334 | OUT VOID *Buffer,\r | |
335 | IN UINTN Length,\r | |
336 | IN UINT8 Value\r | |
337 | )\r | |
338 | {\r | |
339 | if (!SmmIsBufferOutsideSmmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Length)) {\r | |
340 | DEBUG ((EFI_D_ERROR, "SmmSetMem: Security Violation: Source (0x%x), Length (0x%x)\n", Buffer, Length));\r | |
341 | return EFI_SECURITY_VIOLATION;\r | |
342 | }\r | |
343 | SetMem (Buffer, Length, Value);\r | |
344 | return EFI_SUCCESS;\r | |
345 | }\r | |
346 | \r | |
233ffa90 JY |
347 | /**\r |
348 | Get GCD memory map.\r | |
349 | Only record untested memory as invalid communication buffer.\r | |
350 | **/\r | |
351 | VOID\r | |
352 | SmmMemLibInternalGetGcdMemoryMap (\r | |
353 | VOID\r | |
354 | )\r | |
355 | {\r | |
356 | UINTN NumberOfDescriptors;\r | |
357 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;\r | |
358 | EFI_STATUS Status;\r | |
359 | UINTN Index;\r | |
360 | \r | |
361 | Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap);\r | |
362 | if (EFI_ERROR (Status)) {\r | |
363 | return ;\r | |
364 | }\r | |
365 | \r | |
366 | mSmmMemLibGcdMemNumberOfDesc = 0;\r | |
367 | for (Index = 0; Index < NumberOfDescriptors; Index++) {\r | |
368 | if (MemSpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeReserved &&\r | |
369 | (MemSpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r | |
370 | (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)\r | |
371 | ) {\r | |
372 | mSmmMemLibGcdMemNumberOfDesc++;\r | |
373 | }\r | |
374 | }\r | |
375 | \r | |
376 | mSmmMemLibGcdMemSpace = AllocateZeroPool (mSmmMemLibGcdMemNumberOfDesc * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR));\r | |
377 | ASSERT (mSmmMemLibGcdMemSpace != NULL);\r | |
378 | if (mSmmMemLibGcdMemSpace == NULL) {\r | |
379 | mSmmMemLibGcdMemNumberOfDesc = 0;\r | |
380 | gBS->FreePool (MemSpaceMap);\r | |
381 | return ;\r | |
382 | }\r | |
383 | \r | |
384 | mSmmMemLibGcdMemNumberOfDesc = 0;\r | |
385 | for (Index = 0; Index < NumberOfDescriptors; Index++) {\r | |
386 | if (MemSpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeReserved &&\r | |
387 | (MemSpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r | |
388 | (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)\r | |
389 | ) {\r | |
390 | CopyMem (\r | |
391 | &mSmmMemLibGcdMemSpace[mSmmMemLibGcdMemNumberOfDesc],\r | |
392 | &MemSpaceMap[Index],\r | |
393 | sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR)\r | |
394 | );\r | |
395 | mSmmMemLibGcdMemNumberOfDesc++;\r | |
396 | }\r | |
397 | }\r | |
398 | \r | |
399 | gBS->FreePool (MemSpaceMap);\r | |
400 | }\r | |
401 | \r | |
91f51fcc JY |
402 | /**\r |
403 | Notification for SMM EndOfDxe protocol.\r | |
404 | \r | |
405 | @param[in] Protocol Points to the protocol's unique identifier.\r | |
406 | @param[in] Interface Points to the interface instance.\r | |
407 | @param[in] Handle The handle on which the interface was installed.\r | |
408 | \r | |
409 | @retval EFI_SUCCESS Notification runs successfully.\r | |
410 | **/\r | |
411 | EFI_STATUS\r | |
412 | EFIAPI\r | |
413 | SmmLibInternalEndOfDxeNotify (\r | |
414 | IN CONST EFI_GUID *Protocol,\r | |
415 | IN VOID *Interface,\r | |
416 | IN EFI_HANDLE Handle\r | |
417 | )\r | |
418 | {\r | |
419 | EFI_STATUS Status;\r | |
420 | UINTN MapKey;\r | |
421 | UINTN MemoryMapSize;\r | |
422 | EFI_MEMORY_DESCRIPTOR *MemoryMap;\r | |
423 | EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r | |
424 | EFI_MEMORY_DESCRIPTOR *SmmMemoryMapStart;\r | |
425 | UINTN MemoryMapEntryCount;\r | |
426 | UINTN DescriptorSize;\r | |
427 | UINT32 DescriptorVersion;\r | |
428 | UINTN Index;\r | |
429 | \r | |
430 | MemoryMapSize = 0;\r | |
431 | MemoryMap = NULL;\r | |
432 | Status = gBS->GetMemoryMap (\r | |
433 | &MemoryMapSize,\r | |
434 | MemoryMap,\r | |
435 | &MapKey,\r | |
436 | &DescriptorSize,\r | |
437 | &DescriptorVersion\r | |
438 | );\r | |
439 | ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r | |
440 | \r | |
441 | do {\r | |
442 | Status = gBS->AllocatePool (EfiBootServicesData, MemoryMapSize, (VOID **)&MemoryMap);\r | |
443 | ASSERT (MemoryMap != NULL);\r | |
9095d37b | 444 | \r |
91f51fcc JY |
445 | Status = gBS->GetMemoryMap (\r |
446 | &MemoryMapSize,\r | |
447 | MemoryMap,\r | |
448 | &MapKey,\r | |
449 | &DescriptorSize,\r | |
450 | &DescriptorVersion\r | |
451 | );\r | |
452 | if (EFI_ERROR (Status)) {\r | |
453 | gBS->FreePool (MemoryMap);\r | |
454 | }\r | |
455 | } while (Status == EFI_BUFFER_TOO_SMALL);\r | |
456 | \r | |
457 | //\r | |
458 | // Get Count\r | |
459 | //\r | |
460 | mDescriptorSize = DescriptorSize;\r | |
461 | MemoryMapEntryCount = MemoryMapSize/DescriptorSize;\r | |
462 | MemoryMapStart = MemoryMap;\r | |
463 | mMemoryMapEntryCount = 0;\r | |
464 | for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r | |
465 | switch (MemoryMap->Type) {\r | |
466 | case EfiReservedMemoryType:\r | |
467 | case EfiRuntimeServicesCode:\r | |
468 | case EfiRuntimeServicesData:\r | |
469 | case EfiACPIMemoryNVS:\r | |
470 | mMemoryMapEntryCount++;\r | |
471 | break;\r | |
472 | }\r | |
473 | MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);\r | |
474 | }\r | |
475 | MemoryMap = MemoryMapStart;\r | |
9095d37b | 476 | \r |
91f51fcc JY |
477 | //\r |
478 | // Get Data\r | |
479 | //\r | |
480 | mMemoryMap = AllocatePool (mMemoryMapEntryCount*DescriptorSize);\r | |
481 | ASSERT (mMemoryMap != NULL);\r | |
482 | SmmMemoryMapStart = mMemoryMap;\r | |
483 | for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r | |
484 | switch (MemoryMap->Type) {\r | |
485 | case EfiReservedMemoryType:\r | |
486 | case EfiRuntimeServicesCode:\r | |
487 | case EfiRuntimeServicesData:\r | |
488 | case EfiACPIMemoryNVS:\r | |
489 | CopyMem (mMemoryMap, MemoryMap, DescriptorSize);\r | |
490 | mMemoryMap = NEXT_MEMORY_DESCRIPTOR(mMemoryMap, DescriptorSize);\r | |
491 | break;\r | |
492 | }\r | |
493 | MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);\r | |
494 | }\r | |
495 | mMemoryMap = SmmMemoryMapStart;\r | |
496 | MemoryMap = MemoryMapStart;\r | |
9095d37b | 497 | \r |
91f51fcc JY |
498 | gBS->FreePool (MemoryMap);\r |
499 | \r | |
233ffa90 JY |
500 | //\r |
501 | // Get additional information from GCD memory map.\r | |
502 | //\r | |
503 | SmmMemLibInternalGetGcdMemoryMap ();\r | |
504 | \r | |
91f51fcc JY |
505 | return EFI_SUCCESS;\r |
506 | }\r | |
507 | \r | |
91f51fcc JY |
508 | /**\r |
509 | Notification for SMM ReadyToLock protocol.\r | |
510 | \r | |
511 | @param[in] Protocol Points to the protocol's unique identifier.\r | |
512 | @param[in] Interface Points to the interface instance.\r | |
513 | @param[in] Handle The handle on which the interface was installed.\r | |
514 | \r | |
515 | @retval EFI_SUCCESS Notification runs successfully.\r | |
516 | **/\r | |
517 | EFI_STATUS\r | |
518 | EFIAPI\r | |
519 | SmmLibInternalReadyToLockNotify (\r | |
520 | IN CONST EFI_GUID *Protocol,\r | |
521 | IN VOID *Interface,\r | |
522 | IN EFI_HANDLE Handle\r | |
523 | )\r | |
524 | {\r | |
233ffa90 | 525 | mSmmMemLibSmmReadyToLock = TRUE;\r |
91f51fcc JY |
526 | return EFI_SUCCESS;\r |
527 | }\r | |
d425764e JY |
528 | /**\r |
529 | The constructor function initializes the Smm Mem library\r | |
530 | \r | |
531 | @param ImageHandle The firmware allocated handle for the EFI image.\r | |
532 | @param SystemTable A pointer to the EFI System Table.\r | |
533 | \r | |
534 | @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r | |
535 | \r | |
536 | **/\r | |
537 | EFI_STATUS\r | |
538 | EFIAPI\r | |
539 | SmmMemLibConstructor (\r | |
540 | IN EFI_HANDLE ImageHandle,\r | |
541 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
542 | )\r | |
543 | {\r | |
544 | EFI_STATUS Status;\r | |
545 | EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r | |
546 | UINTN Size;\r | |
9095d37b | 547 | \r |
d425764e JY |
548 | //\r |
549 | // Get SMRAM information\r | |
550 | //\r | |
551 | Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r | |
552 | ASSERT_EFI_ERROR (Status);\r | |
553 | \r | |
554 | Size = 0;\r | |
555 | Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r | |
556 | ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r | |
557 | \r | |
558 | mSmmMemLibInternalSmramRanges = AllocatePool (Size);\r | |
559 | ASSERT (mSmmMemLibInternalSmramRanges != NULL);\r | |
560 | \r | |
561 | Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmmMemLibInternalSmramRanges);\r | |
562 | ASSERT_EFI_ERROR (Status);\r | |
563 | \r | |
564 | mSmmMemLibInternalSmramCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r | |
565 | \r | |
566 | //\r | |
2a93f2c3 | 567 | // Calculate and save maximum support address\r |
d425764e | 568 | //\r |
2a93f2c3 | 569 | SmmMemLibInternalCalculateMaximumSupportAddress ();\r |
d425764e | 570 | \r |
91f51fcc JY |
571 | //\r |
572 | // Register EndOfDxe to get UEFI memory map\r | |
573 | //\r | |
574 | Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, SmmLibInternalEndOfDxeNotify, &mRegistrationEndOfDxe);\r | |
575 | ASSERT_EFI_ERROR (Status);\r | |
576 | \r | |
577 | //\r | |
578 | // Register ready to lock so that we can know when to check valid SMRAM region\r | |
579 | //\r | |
580 | Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, SmmLibInternalReadyToLockNotify, &mRegistrationReadyToLock);\r | |
581 | ASSERT_EFI_ERROR (Status);\r | |
582 | \r | |
d425764e JY |
583 | return EFI_SUCCESS;\r |
584 | }\r | |
585 | \r | |
586 | /**\r | |
587 | The destructor function frees resource used in the Smm Mem library\r | |
588 | \r | |
589 | @param[in] ImageHandle The firmware allocated handle for the EFI image.\r | |
590 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
591 | \r | |
592 | @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.\r | |
593 | **/\r | |
594 | EFI_STATUS\r | |
595 | EFIAPI\r | |
596 | SmmMemLibDestructor (\r | |
597 | IN EFI_HANDLE ImageHandle,\r | |
598 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
599 | )\r | |
600 | {\r | |
601 | FreePool (mSmmMemLibInternalSmramRanges);\r | |
602 | \r | |
91f51fcc JY |
603 | gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, NULL, &mRegistrationEndOfDxe);\r |
604 | gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, NULL, &mRegistrationReadyToLock);\r | |
d425764e JY |
605 | return EFI_SUCCESS;\r |
606 | }\r |