Commit | Line | Data |
---|---|---|
e42e9404 | 1 | /** @file\r |
2 | SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM\r | |
3 | \r | |
d1102dba | 4 | Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r |
9d510e61 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
e42e9404 | 6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include <PiDxe.h>\r | |
10 | \r | |
11 | #include <Protocol/SmmBase2.h>\r | |
12 | #include <Protocol/SmmCommunication.h>\r | |
9ad2b981 | 13 | #include <Protocol/MmCommunication2.h>\r |
e42e9404 | 14 | #include <Protocol/SmmAccess2.h>\r |
15 | #include <Protocol/SmmConfiguration.h>\r | |
16 | #include <Protocol/SmmControl2.h>\r | |
17 | #include <Protocol/DxeSmmReadyToLock.h>\r | |
07d9dc83 | 18 | #include <Protocol/Cpu.h>\r |
e42e9404 | 19 | \r |
20 | #include <Guid/EventGroup.h>\r | |
21 | #include <Guid/EventLegacyBios.h>\r | |
3c447c27 | 22 | #include <Guid/LoadModuleAtFixedAddress.h>\r |
e42e9404 | 23 | \r |
24 | #include <Library/BaseLib.h>\r | |
25 | #include <Library/BaseMemoryLib.h>\r | |
26 | #include <Library/PeCoffLib.h>\r | |
27 | #include <Library/CacheMaintenanceLib.h>\r | |
28 | #include <Library/MemoryAllocationLib.h>\r | |
29 | #include <Library/DebugLib.h>\r | |
30 | #include <Library/UefiBootServicesTableLib.h>\r | |
31 | #include <Library/DxeServicesTableLib.h>\r | |
d7aaf1dc | 32 | #include <Library/DxeServicesLib.h>\r |
e42e9404 | 33 | #include <Library/UefiLib.h>\r |
34 | #include <Library/UefiRuntimeLib.h>\r | |
3c447c27 | 35 | #include <Library/PcdLib.h>\r |
265fff39 | 36 | #include <Library/ReportStatusCodeLib.h>\r |
e42e9404 | 37 | #include "PiSmmCorePrivateData.h"\r |
cab1f025 | 38 | #include <Library/SafeIntLib.h>\r |
e42e9404 | 39 | \r |
54ec85dd JW |
40 | #define SMRAM_CAPABILITIES (EFI_MEMORY_WB | EFI_MEMORY_UC)\r |
41 | \r | |
e42e9404 | 42 | //\r |
43 | // Function prototypes from produced protocols\r | |
44 | //\r | |
45 | \r | |
46 | /**\r | |
47 | Indicate whether the driver is currently executing in the SMM Initialization phase.\r | |
48 | \r | |
49 | @param This The EFI_SMM_BASE2_PROTOCOL instance.\r | |
50 | @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r | |
51 | inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r | |
52 | \r | |
53 | @retval EFI_INVALID_PARAMETER InSmram was NULL.\r | |
54 | @retval EFI_SUCCESS The call returned successfully.\r | |
55 | \r | |
56 | **/\r | |
57 | EFI_STATUS\r | |
58 | EFIAPI\r | |
59 | SmmBase2InSmram (\r | |
60 | IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r | |
61 | OUT BOOLEAN *InSmram\r | |
62 | );\r | |
63 | \r | |
64 | /**\r | |
65 | Retrieves the location of the System Management System Table (SMST).\r | |
66 | \r | |
67 | @param This The EFI_SMM_BASE2_PROTOCOL instance.\r | |
68 | @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r | |
69 | \r | |
70 | @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r | |
71 | @retval EFI_SUCCESS The memory was returned to the system.\r | |
72 | @retval EFI_UNSUPPORTED Not in SMM.\r | |
73 | \r | |
74 | **/\r | |
75 | EFI_STATUS\r | |
76 | EFIAPI\r | |
77 | SmmBase2GetSmstLocation (\r | |
78 | IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r | |
79 | OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r | |
80 | );\r | |
81 | \r | |
82 | /**\r | |
83 | Communicates with a registered handler.\r | |
d1102dba LG |
84 | \r |
85 | This function provides a service to send and receive messages from a registered\r | |
86 | UEFI service. This function is part of the SMM Communication Protocol that may\r | |
87 | be called in physical mode prior to SetVirtualAddressMap() and in virtual mode\r | |
e42e9404 | 88 | after SetVirtualAddressMap().\r |
89 | \r | |
8c721557 SZ |
90 | @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r |
91 | @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.\r | |
92 | @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data\r | |
93 | being returned. Zero if the handler does not wish to reply with any data.\r | |
94 | This parameter is optional and may be NULL.\r | |
95 | \r | |
96 | @retval EFI_SUCCESS The message was successfully posted.\r | |
97 | @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r | |
98 | @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.\r | |
99 | If this error is returned, the MessageLength field\r | |
100 | in the CommBuffer header or the integer pointed by\r | |
101 | CommSize, are updated to reflect the maximum payload\r | |
102 | size the implementation can accommodate.\r | |
103 | @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,\r | |
104 | if not omitted, are in address range that cannot be\r | |
105 | accessed by the MM environment.\r | |
e42e9404 | 106 | \r |
e42e9404 | 107 | **/\r |
108 | EFI_STATUS\r | |
109 | EFIAPI\r | |
110 | SmmCommunicationCommunicate (\r | |
111 | IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r | |
112 | IN OUT VOID *CommBuffer,\r | |
8c721557 | 113 | IN OUT UINTN *CommSize OPTIONAL\r |
e42e9404 | 114 | );\r |
115 | \r | |
9ad2b981 AB |
116 | /**\r |
117 | Communicates with a registered handler.\r | |
118 | \r | |
119 | This function provides a service to send and receive messages from a registered UEFI service.\r | |
120 | \r | |
121 | @param[in] This The EFI_MM_COMMUNICATION_PROTOCOL instance.\r | |
122 | @param[in] CommBufferPhysical Physical address of the MM communication buffer\r | |
123 | @param[in] CommBufferVirtual Virtual address of the MM communication buffer\r | |
124 | @param[in] CommSize The size of the data buffer being passed in. On exit, the size of data\r | |
125 | being returned. Zero if the handler does not wish to reply with any data.\r | |
126 | This parameter is optional and may be NULL.\r | |
127 | \r | |
128 | @retval EFI_SUCCESS The message was successfully posted.\r | |
129 | @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r | |
130 | @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.\r | |
131 | If this error is returned, the MessageLength field\r | |
132 | in the CommBuffer header or the integer pointed by\r | |
133 | CommSize, are updated to reflect the maximum payload\r | |
134 | size the implementation can accommodate.\r | |
135 | @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,\r | |
136 | if not omitted, are in address range that cannot be\r | |
137 | accessed by the MM environment.\r | |
138 | \r | |
139 | **/\r | |
140 | EFI_STATUS\r | |
141 | EFIAPI\r | |
142 | SmmCommunicationMmCommunicate2 (\r | |
1436aea4 MK |
143 | IN CONST EFI_MM_COMMUNICATION2_PROTOCOL *This,\r |
144 | IN OUT VOID *CommBufferPhysical,\r | |
145 | IN OUT VOID *CommBufferVirtual,\r | |
146 | IN OUT UINTN *CommSize OPTIONAL\r | |
9ad2b981 AB |
147 | );\r |
148 | \r | |
e42e9404 | 149 | /**\r |
150 | Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r | |
151 | \r | |
152 | @param Event The Event that is being processed, not used.\r | |
153 | @param Context Event Context, not used.\r | |
154 | \r | |
155 | **/\r | |
156 | VOID\r | |
157 | EFIAPI\r | |
158 | SmmIplSmmConfigurationEventNotify (\r | |
159 | IN EFI_EVENT Event,\r | |
160 | IN VOID *Context\r | |
161 | );\r | |
162 | \r | |
163 | /**\r | |
164 | Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r | |
165 | or if gEfiEventReadyToBootGuid is signalled.\r | |
166 | \r | |
167 | @param Event The Event that is being processed, not used.\r | |
168 | @param Context Event Context, not used.\r | |
169 | \r | |
170 | **/\r | |
171 | VOID\r | |
172 | EFIAPI\r | |
173 | SmmIplReadyToLockEventNotify (\r | |
174 | IN EFI_EVENT Event,\r | |
175 | IN VOID *Context\r | |
176 | );\r | |
177 | \r | |
178 | /**\r | |
179 | Event notification that is fired when DxeDispatch Event Group is signaled.\r | |
180 | \r | |
181 | @param Event The Event that is being processed, not used.\r | |
182 | @param Context Event Context, not used.\r | |
183 | \r | |
5657b268 | 184 | **/\r |
185 | VOID\r | |
186 | EFIAPI\r | |
187 | SmmIplDxeDispatchEventNotify (\r | |
188 | IN EFI_EVENT Event,\r | |
189 | IN VOID *Context\r | |
190 | );\r | |
191 | \r | |
192 | /**\r | |
193 | Event notification that is fired when a GUIDed Event Group is signaled.\r | |
194 | \r | |
195 | @param Event The Event that is being processed, not used.\r | |
196 | @param Context Event Context, not used.\r | |
197 | \r | |
e42e9404 | 198 | **/\r |
199 | VOID\r | |
200 | EFIAPI\r | |
201 | SmmIplGuidedEventNotify (\r | |
202 | IN EFI_EVENT Event,\r | |
203 | IN VOID *Context\r | |
204 | );\r | |
205 | \r | |
265fff39 JF |
206 | /**\r |
207 | Event notification that is fired when EndOfDxe Event Group is signaled.\r | |
208 | \r | |
209 | @param Event The Event that is being processed, not used.\r | |
210 | @param Context Event Context, not used.\r | |
211 | \r | |
212 | **/\r | |
213 | VOID\r | |
214 | EFIAPI\r | |
215 | SmmIplEndOfDxeEventNotify (\r | |
216 | IN EFI_EVENT Event,\r | |
217 | IN VOID *Context\r | |
218 | );\r | |
219 | \r | |
e42e9404 | 220 | /**\r |
221 | Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r | |
222 | \r | |
223 | This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r | |
224 | It convers pointer to new virtual address.\r | |
225 | \r | |
226 | @param Event Event whose notification function is being invoked.\r | |
227 | @param Context Pointer to the notification function's context.\r | |
228 | \r | |
229 | **/\r | |
230 | VOID\r | |
231 | EFIAPI\r | |
232 | SmmIplSetVirtualAddressNotify (\r | |
233 | IN EFI_EVENT Event,\r | |
234 | IN VOID *Context\r | |
235 | );\r | |
236 | \r | |
237 | //\r | |
d1102dba | 238 | // Data structure used to declare a table of protocol notifications and event\r |
e42e9404 | 239 | // notifications required by the SMM IPL\r |
240 | //\r | |
241 | typedef struct {\r | |
1436aea4 MK |
242 | BOOLEAN Protocol;\r |
243 | BOOLEAN CloseOnLock;\r | |
244 | EFI_GUID *Guid;\r | |
245 | EFI_EVENT_NOTIFY NotifyFunction;\r | |
246 | VOID *NotifyContext;\r | |
247 | EFI_TPL NotifyTpl;\r | |
248 | EFI_EVENT Event;\r | |
e42e9404 | 249 | } SMM_IPL_EVENT_NOTIFICATION;\r |
250 | \r | |
251 | //\r | |
252 | // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol\r | |
253 | //\r | |
254 | EFI_HANDLE mSmmIplHandle = NULL;\r | |
255 | \r | |
256 | //\r | |
257 | // SMM Base 2 Protocol instance\r | |
258 | //\r | |
259 | EFI_SMM_BASE2_PROTOCOL mSmmBase2 = {\r | |
260 | SmmBase2InSmram,\r | |
261 | SmmBase2GetSmstLocation\r | |
262 | };\r | |
263 | \r | |
264 | //\r | |
265 | // SMM Communication Protocol instance\r | |
266 | //\r | |
267 | EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication = {\r | |
268 | SmmCommunicationCommunicate\r | |
269 | };\r | |
270 | \r | |
9ad2b981 AB |
271 | //\r |
272 | // PI 1.7 MM Communication Protocol 2 instance\r | |
273 | //\r | |
274 | EFI_MM_COMMUNICATION2_PROTOCOL mMmCommunication2 = {\r | |
275 | SmmCommunicationMmCommunicate2\r | |
276 | };\r | |
277 | \r | |
e42e9404 | 278 | //\r |
279 | // SMM Core Private Data structure that contains the data shared between\r | |
280 | // the SMM IPL and the SMM Core.\r | |
281 | //\r | |
282 | SMM_CORE_PRIVATE_DATA mSmmCorePrivateData = {\r | |
283 | SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature\r | |
284 | NULL, // SmmIplImageHandle\r | |
285 | 0, // SmramRangeCount\r | |
286 | NULL, // SmramRanges\r | |
287 | NULL, // SmmEntryPoint\r | |
288 | FALSE, // SmmEntryPointRegistered\r | |
289 | FALSE, // InSmm\r | |
290 | NULL, // Smst\r | |
e42e9404 | 291 | NULL, // CommunicationBuffer\r |
ab780ebf | 292 | 0, // BufferSize\r |
e42e9404 | 293 | EFI_SUCCESS // ReturnStatus\r |
294 | };\r | |
295 | \r | |
296 | //\r | |
297 | // Global pointer used to access mSmmCorePrivateData from outside and inside SMM\r | |
298 | //\r | |
299 | SMM_CORE_PRIVATE_DATA *gSmmCorePrivate = &mSmmCorePrivateData;\r | |
300 | \r | |
301 | //\r | |
302 | // SMM IPL global variables\r | |
303 | //\r | |
304 | EFI_SMM_CONTROL2_PROTOCOL *mSmmControl2;\r | |
305 | EFI_SMM_ACCESS2_PROTOCOL *mSmmAccess;\r | |
306 | EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange;\r | |
307 | BOOLEAN mSmmLocked = FALSE;\r | |
265fff39 | 308 | BOOLEAN mEndOfDxe = FALSE;\r |
40e8cca5 | 309 | EFI_PHYSICAL_ADDRESS mSmramCacheBase;\r |
310 | UINT64 mSmramCacheSize;\r | |
e42e9404 | 311 | \r |
1436aea4 MK |
312 | EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader;\r |
313 | EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *mLMFAConfigurationTable = NULL;\r | |
670a563f | 314 | \r |
e42e9404 | 315 | //\r |
316 | // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires\r | |
317 | //\r | |
318 | SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents[] = {\r | |
319 | //\r | |
2048c585 | 320 | // Declare protocol notification on the SMM Configuration protocol. When this notification is established,\r |
d1102dba | 321 | // the associated event is immediately signalled, so the notification function will be executed and the\r |
e42e9404 | 322 | // SMM Configuration Protocol will be found if it is already in the handle database.\r |
323 | //\r | |
5657b268 | 324 | { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, TPL_NOTIFY, NULL },\r |
e42e9404 | 325 | //\r |
d1102dba LG |
326 | // Declare protocol notification on DxeSmmReadyToLock protocols. When this notification is established,\r |
327 | // the associated event is immediately signalled, so the notification function will be executed and the\r | |
e42e9404 | 328 | // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.\r |
329 | //\r | |
5657b268 | 330 | { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, TPL_CALLBACK, NULL },\r |
e42e9404 | 331 | //\r |
2048c585 | 332 | // Declare event notification on EndOfDxe event. When this notification is established,\r |
d1102dba | 333 | // the associated event is immediately signalled, so the notification function will be executed and the\r |
46ece1ff JY |
334 | // SMM End Of Dxe Protocol will be found if it is already in the handle database.\r |
335 | //\r | |
dc157845 | 336 | { FALSE, TRUE, &gEfiEndOfDxeEventGroupGuid, SmmIplGuidedEventNotify, &gEfiEndOfDxeEventGroupGuid, TPL_CALLBACK, NULL },\r |
46ece1ff | 337 | //\r |
265fff39 JF |
338 | // Declare event notification on EndOfDxe event. This is used to set EndOfDxe event signaled flag.\r |
339 | //\r | |
340 | { FALSE, TRUE, &gEfiEndOfDxeEventGroupGuid, SmmIplEndOfDxeEventNotify, &gEfiEndOfDxeEventGroupGuid, TPL_CALLBACK, NULL },\r | |
341 | //\r | |
e42e9404 | 342 | // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core\r |
343 | // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core\r | |
344 | // if notified, so the SMM Core can dispatch SMM drivers.\r | |
345 | //\r | |
5657b268 | 346 | { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplDxeDispatchEventNotify, &gEfiEventDxeDispatchGuid, TPL_CALLBACK, NULL },\r |
e42e9404 | 347 | //\r |
348 | // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is\r | |
349 | // used to make sure SMRAM is locked before any boot options are processed.\r | |
350 | //\r | |
5657b268 | 351 | { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },\r |
e42e9404 | 352 | //\r |
d1102dba LG |
353 | // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform\r |
354 | // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core\r | |
e42e9404 | 355 | // must guarantee that it does not access any UEFI related structures outside of SMRAM.\r |
53ec4d7f | 356 | // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.\r |
e42e9404 | 357 | //\r |
5657b268 | 358 | { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, TPL_CALLBACK, NULL },\r |
e42e9404 | 359 | //\r |
53ec4d7f SZ |
360 | // Declare event notification on Exit Boot Services Event Group. This is used to inform the SMM Core\r |
361 | // to notify SMM driver that system enter exit boot services.\r | |
362 | //\r | |
363 | { FALSE, FALSE, &gEfiEventExitBootServicesGuid, SmmIplGuidedEventNotify, &gEfiEventExitBootServicesGuid, TPL_CALLBACK, NULL },\r | |
364 | //\r | |
365 | // Declare event notification on Ready To Boot Event Group. This is used to inform the SMM Core\r | |
366 | // to notify SMM driver that system enter ready to boot.\r | |
367 | //\r | |
368 | { FALSE, FALSE, &gEfiEventReadyToBootGuid, SmmIplGuidedEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },\r | |
369 | //\r | |
d1102dba | 370 | // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate\r |
e42e9404 | 371 | // and mSmmControl2 from physical addresses to virtual addresses.\r |
372 | //\r | |
5657b268 | 373 | { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, TPL_CALLBACK, NULL },\r |
e42e9404 | 374 | //\r |
375 | // Terminate the table of event notifications\r | |
376 | //\r | |
5657b268 | 377 | { FALSE, FALSE, NULL, NULL, NULL, TPL_CALLBACK, NULL }\r |
e42e9404 | 378 | };\r |
379 | \r | |
40e8cca5 | 380 | /**\r |
381 | Find the maximum SMRAM cache range that covers the range specified by SmramRange.\r | |
d1102dba | 382 | \r |
40e8cca5 | 383 | This function searches and joins all adjacent ranges of SmramRange into a range to be cached.\r |
384 | \r | |
385 | @param SmramRange The SMRAM range to search from.\r | |
386 | @param SmramCacheBase The returned cache range base.\r | |
387 | @param SmramCacheSize The returned cache range size.\r | |
388 | \r | |
389 | **/\r | |
390 | VOID\r | |
391 | GetSmramCacheRange (\r | |
1436aea4 MK |
392 | IN EFI_SMRAM_DESCRIPTOR *SmramRange,\r |
393 | OUT EFI_PHYSICAL_ADDRESS *SmramCacheBase,\r | |
394 | OUT UINT64 *SmramCacheSize\r | |
40e8cca5 | 395 | )\r |
396 | {\r | |
1436aea4 MK |
397 | UINTN Index;\r |
398 | EFI_PHYSICAL_ADDRESS RangeCpuStart;\r | |
399 | UINT64 RangePhysicalSize;\r | |
400 | BOOLEAN FoundAjacentRange;\r | |
40e8cca5 | 401 | \r |
402 | *SmramCacheBase = SmramRange->CpuStart;\r | |
403 | *SmramCacheSize = SmramRange->PhysicalSize;\r | |
404 | \r | |
405 | do {\r | |
406 | FoundAjacentRange = FALSE;\r | |
407 | for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r | |
408 | RangeCpuStart = gSmmCorePrivate->SmramRanges[Index].CpuStart;\r | |
409 | RangePhysicalSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r | |
1436aea4 | 410 | if ((RangeCpuStart < *SmramCacheBase) && (*SmramCacheBase == (RangeCpuStart + RangePhysicalSize))) {\r |
40e8cca5 | 411 | *SmramCacheBase = RangeCpuStart;\r |
412 | *SmramCacheSize += RangePhysicalSize;\r | |
413 | FoundAjacentRange = TRUE;\r | |
1436aea4 | 414 | } else if (((*SmramCacheBase + *SmramCacheSize) == RangeCpuStart) && (RangePhysicalSize > 0)) {\r |
40e8cca5 | 415 | *SmramCacheSize += RangePhysicalSize;\r |
416 | FoundAjacentRange = TRUE;\r | |
417 | }\r | |
418 | }\r | |
419 | } while (FoundAjacentRange);\r | |
40e8cca5 | 420 | }\r |
421 | \r | |
e42e9404 | 422 | /**\r |
423 | Indicate whether the driver is currently executing in the SMM Initialization phase.\r | |
424 | \r | |
425 | @param This The EFI_SMM_BASE2_PROTOCOL instance.\r | |
426 | @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r | |
427 | inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r | |
428 | \r | |
429 | @retval EFI_INVALID_PARAMETER InSmram was NULL.\r | |
430 | @retval EFI_SUCCESS The call returned successfully.\r | |
431 | \r | |
432 | **/\r | |
433 | EFI_STATUS\r | |
434 | EFIAPI\r | |
435 | SmmBase2InSmram (\r | |
436 | IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r | |
437 | OUT BOOLEAN *InSmram\r | |
438 | )\r | |
439 | {\r | |
440 | if (InSmram == NULL) {\r | |
441 | return EFI_INVALID_PARAMETER;\r | |
442 | }\r | |
443 | \r | |
444 | *InSmram = gSmmCorePrivate->InSmm;\r | |
445 | \r | |
446 | return EFI_SUCCESS;\r | |
447 | }\r | |
448 | \r | |
449 | /**\r | |
450 | Retrieves the location of the System Management System Table (SMST).\r | |
451 | \r | |
452 | @param This The EFI_SMM_BASE2_PROTOCOL instance.\r | |
453 | @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r | |
454 | \r | |
455 | @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r | |
456 | @retval EFI_SUCCESS The memory was returned to the system.\r | |
457 | @retval EFI_UNSUPPORTED Not in SMM.\r | |
458 | \r | |
459 | **/\r | |
460 | EFI_STATUS\r | |
461 | EFIAPI\r | |
462 | SmmBase2GetSmstLocation (\r | |
463 | IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r | |
464 | OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r | |
465 | )\r | |
466 | {\r | |
1436aea4 | 467 | if ((This == NULL) || (Smst == NULL)) {\r |
e42e9404 | 468 | return EFI_INVALID_PARAMETER;\r |
469 | }\r | |
d1102dba | 470 | \r |
e42e9404 | 471 | if (!gSmmCorePrivate->InSmm) {\r |
472 | return EFI_UNSUPPORTED;\r | |
473 | }\r | |
d1102dba | 474 | \r |
e42e9404 | 475 | *Smst = gSmmCorePrivate->Smst;\r |
476 | \r | |
477 | return EFI_SUCCESS;\r | |
478 | }\r | |
479 | \r | |
480 | /**\r | |
481 | Communicates with a registered handler.\r | |
d1102dba LG |
482 | \r |
483 | This function provides a service to send and receive messages from a registered\r | |
484 | UEFI service. This function is part of the SMM Communication Protocol that may\r | |
485 | be called in physical mode prior to SetVirtualAddressMap() and in virtual mode\r | |
e42e9404 | 486 | after SetVirtualAddressMap().\r |
487 | \r | |
488 | @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r | |
d1632f69 SZ |
489 | @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.\r |
490 | @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data\r | |
e42e9404 | 491 | being returned. Zero if the handler does not wish to reply with any data.\r |
d1632f69 | 492 | This parameter is optional and may be NULL.\r |
e42e9404 | 493 | \r |
494 | @retval EFI_SUCCESS The message was successfully posted.\r | |
495 | @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r | |
d1632f69 SZ |
496 | @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.\r |
497 | If this error is returned, the MessageLength field\r | |
498 | in the CommBuffer header or the integer pointed by\r | |
499 | CommSize, are updated to reflect the maximum payload\r | |
500 | size the implementation can accommodate.\r | |
501 | @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,\r | |
502 | if not omitted, are in address range that cannot be\r | |
503 | accessed by the MM environment.\r | |
504 | \r | |
e42e9404 | 505 | **/\r |
506 | EFI_STATUS\r | |
507 | EFIAPI\r | |
508 | SmmCommunicationCommunicate (\r | |
509 | IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r | |
510 | IN OUT VOID *CommBuffer,\r | |
d1632f69 | 511 | IN OUT UINTN *CommSize OPTIONAL\r |
e42e9404 | 512 | )\r |
513 | {\r | |
514 | EFI_STATUS Status;\r | |
515 | EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r | |
516 | BOOLEAN OldInSmm;\r | |
d1632f69 | 517 | UINTN TempCommSize;\r |
e42e9404 | 518 | \r |
519 | //\r | |
520 | // Check parameters\r | |
521 | //\r | |
d1632f69 | 522 | if (CommBuffer == NULL) {\r |
e42e9404 | 523 | return EFI_INVALID_PARAMETER;\r |
524 | }\r | |
525 | \r | |
1436aea4 | 526 | CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;\r |
d1632f69 SZ |
527 | \r |
528 | if (CommSize == NULL) {\r | |
529 | TempCommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + CommunicateHeader->MessageLength;\r | |
530 | } else {\r | |
531 | TempCommSize = *CommSize;\r | |
532 | //\r | |
533 | // CommSize must hold HeaderGuid and MessageLength\r | |
534 | //\r | |
535 | if (TempCommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {\r | |
536 | return EFI_INVALID_PARAMETER;\r | |
537 | }\r | |
ab780ebf JY |
538 | }\r |
539 | \r | |
e42e9404 | 540 | //\r |
541 | // If not already in SMM, then generate a Software SMI\r | |
542 | //\r | |
543 | if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {\r | |
544 | //\r | |
545 | // Put arguments for Software SMI in gSmmCorePrivate\r | |
546 | //\r | |
547 | gSmmCorePrivate->CommunicationBuffer = CommBuffer;\r | |
d1632f69 | 548 | gSmmCorePrivate->BufferSize = TempCommSize;\r |
e42e9404 | 549 | \r |
550 | //\r | |
551 | // Generate Software SMI\r | |
552 | //\r | |
553 | Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);\r | |
554 | if (EFI_ERROR (Status)) {\r | |
555 | return EFI_UNSUPPORTED;\r | |
556 | }\r | |
557 | \r | |
558 | //\r | |
d1102dba | 559 | // Return status from software SMI\r |
e42e9404 | 560 | //\r |
d1632f69 SZ |
561 | if (CommSize != NULL) {\r |
562 | *CommSize = gSmmCorePrivate->BufferSize;\r | |
563 | }\r | |
1436aea4 | 564 | \r |
e42e9404 | 565 | return gSmmCorePrivate->ReturnStatus;\r |
566 | }\r | |
567 | \r | |
568 | //\r | |
569 | // If we are in SMM, then the execution mode must be physical, which means that\r | |
570 | // OS established virtual addresses can not be used. If SetVirtualAddressMap()\r | |
d1632f69 SZ |
571 | // has been called, then a direct invocation of the Software SMI is not allowed,\r |
572 | // so return EFI_INVALID_PARAMETER.\r | |
e42e9404 | 573 | //\r |
1436aea4 | 574 | if (EfiGoneVirtual ()) {\r |
e42e9404 | 575 | return EFI_INVALID_PARAMETER;\r |
576 | }\r | |
577 | \r | |
3c5963cf | 578 | //\r |
96756716 | 579 | // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.\r |
3c5963cf | 580 | //\r |
96756716 | 581 | if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) {\r |
3c5963cf | 582 | return EFI_INVALID_PARAMETER;\r |
583 | }\r | |
d1102dba | 584 | \r |
e42e9404 | 585 | //\r |
586 | // Save current InSmm state and set InSmm state to TRUE\r | |
587 | //\r | |
1436aea4 | 588 | OldInSmm = gSmmCorePrivate->InSmm;\r |
e42e9404 | 589 | gSmmCorePrivate->InSmm = TRUE;\r |
590 | \r | |
591 | //\r | |
4b5d69d3 | 592 | // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.\r |
e42e9404 | 593 | //\r |
d1632f69 | 594 | TempCommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r |
1436aea4 MK |
595 | Status = gSmmCorePrivate->Smst->SmiManage (\r |
596 | &CommunicateHeader->HeaderGuid,\r | |
597 | NULL,\r | |
598 | CommunicateHeader->Data,\r | |
599 | &TempCommSize\r | |
600 | );\r | |
d1632f69 SZ |
601 | TempCommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r |
602 | if (CommSize != NULL) {\r | |
603 | *CommSize = TempCommSize;\r | |
604 | }\r | |
e42e9404 | 605 | \r |
606 | //\r | |
607 | // Restore original InSmm state\r | |
608 | //\r | |
609 | gSmmCorePrivate->InSmm = OldInSmm;\r | |
610 | \r | |
d5b339a9 | 611 | return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r |
e42e9404 | 612 | }\r |
613 | \r | |
9ad2b981 AB |
614 | /**\r |
615 | Communicates with a registered handler.\r | |
616 | \r | |
617 | This function provides a service to send and receive messages from a registered UEFI service.\r | |
618 | \r | |
619 | @param[in] This The EFI_MM_COMMUNICATION_PROTOCOL instance.\r | |
620 | @param[in] CommBufferPhysical Physical address of the MM communication buffer\r | |
621 | @param[in] CommBufferVirtual Virtual address of the MM communication buffer\r | |
622 | @param[in] CommSize The size of the data buffer being passed in. On exit, the size of data\r | |
623 | being returned. Zero if the handler does not wish to reply with any data.\r | |
624 | This parameter is optional and may be NULL.\r | |
625 | \r | |
626 | @retval EFI_SUCCESS The message was successfully posted.\r | |
627 | @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r | |
628 | @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.\r | |
629 | If this error is returned, the MessageLength field\r | |
630 | in the CommBuffer header or the integer pointed by\r | |
631 | CommSize, are updated to reflect the maximum payload\r | |
632 | size the implementation can accommodate.\r | |
633 | @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,\r | |
634 | if not omitted, are in address range that cannot be\r | |
635 | accessed by the MM environment.\r | |
636 | \r | |
637 | **/\r | |
638 | EFI_STATUS\r | |
639 | EFIAPI\r | |
640 | SmmCommunicationMmCommunicate2 (\r | |
1436aea4 MK |
641 | IN CONST EFI_MM_COMMUNICATION2_PROTOCOL *This,\r |
642 | IN OUT VOID *CommBufferPhysical,\r | |
643 | IN OUT VOID *CommBufferVirtual,\r | |
644 | IN OUT UINTN *CommSize OPTIONAL\r | |
9ad2b981 AB |
645 | )\r |
646 | {\r | |
1436aea4 MK |
647 | return SmmCommunicationCommunicate (\r |
648 | &mSmmCommunication,\r | |
649 | CommBufferPhysical,\r | |
650 | CommSize\r | |
651 | );\r | |
9ad2b981 AB |
652 | }\r |
653 | \r | |
e42e9404 | 654 | /**\r |
5657b268 | 655 | Event notification that is fired when GUIDed Event Group is signaled.\r |
e42e9404 | 656 | \r |
657 | @param Event The Event that is being processed, not used.\r | |
658 | @param Context Event Context, not used.\r | |
659 | \r | |
660 | **/\r | |
661 | VOID\r | |
662 | EFIAPI\r | |
663 | SmmIplGuidedEventNotify (\r | |
664 | IN EFI_EVENT Event,\r | |
665 | IN VOID *Context\r | |
666 | )\r | |
667 | {\r | |
1436aea4 | 668 | UINTN Size;\r |
e42e9404 | 669 | \r |
670 | //\r | |
d1102dba | 671 | // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure\r |
e42e9404 | 672 | //\r |
670a563f JY |
673 | CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r |
674 | mCommunicateHeader.MessageLength = 1;\r | |
1436aea4 | 675 | mCommunicateHeader.Data[0] = 0;\r |
e42e9404 | 676 | \r |
677 | //\r | |
678 | // Generate the Software SMI and return the result\r | |
679 | //\r | |
670a563f JY |
680 | Size = sizeof (mCommunicateHeader);\r |
681 | SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r | |
e42e9404 | 682 | }\r |
683 | \r | |
265fff39 JF |
684 | /**\r |
685 | Event notification that is fired when EndOfDxe Event Group is signaled.\r | |
686 | \r | |
687 | @param Event The Event that is being processed, not used.\r | |
688 | @param Context Event Context, not used.\r | |
689 | \r | |
690 | **/\r | |
691 | VOID\r | |
692 | EFIAPI\r | |
693 | SmmIplEndOfDxeEventNotify (\r | |
694 | IN EFI_EVENT Event,\r | |
695 | IN VOID *Context\r | |
696 | )\r | |
697 | {\r | |
698 | mEndOfDxe = TRUE;\r | |
699 | }\r | |
700 | \r | |
5657b268 | 701 | /**\r |
702 | Event notification that is fired when DxeDispatch Event Group is signaled.\r | |
703 | \r | |
704 | @param Event The Event that is being processed, not used.\r | |
705 | @param Context Event Context, not used.\r | |
706 | \r | |
707 | **/\r | |
708 | VOID\r | |
709 | EFIAPI\r | |
710 | SmmIplDxeDispatchEventNotify (\r | |
711 | IN EFI_EVENT Event,\r | |
712 | IN VOID *Context\r | |
713 | )\r | |
714 | {\r | |
1436aea4 MK |
715 | UINTN Size;\r |
716 | EFI_STATUS Status;\r | |
5657b268 | 717 | \r |
718 | //\r | |
719 | // Keep calling the SMM Core Dispatcher until there is no request to restart it.\r | |
720 | //\r | |
721 | while (TRUE) {\r | |
722 | //\r | |
723 | // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure\r | |
724 | // Clear the buffer passed into the Software SMI. This buffer will return\r | |
725 | // the status of the SMM Core Dispatcher.\r | |
726 | //\r | |
670a563f JY |
727 | CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r |
728 | mCommunicateHeader.MessageLength = 1;\r | |
1436aea4 | 729 | mCommunicateHeader.Data[0] = 0;\r |
5657b268 | 730 | \r |
731 | //\r | |
732 | // Generate the Software SMI and return the result\r | |
733 | //\r | |
670a563f JY |
734 | Size = sizeof (mCommunicateHeader);\r |
735 | SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r | |
d1102dba | 736 | \r |
5657b268 | 737 | //\r |
738 | // Return if there is no request to restart the SMM Core Dispatcher\r | |
739 | //\r | |
670a563f | 740 | if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {\r |
5657b268 | 741 | return;\r |
742 | }\r | |
d1102dba | 743 | \r |
5657b268 | 744 | //\r |
745 | // Close all SMRAM ranges to protect SMRAM\r | |
bfc87aa7 RN |
746 | // NOTE: SMRR is enabled by CPU SMM driver by calling SmmCpuFeaturesInitializeProcessor() from SmmCpuFeaturesLib\r |
747 | // so no need to reset the SMRAM to UC in MTRR.\r | |
5657b268 | 748 | //\r |
749 | Status = mSmmAccess->Close (mSmmAccess);\r | |
750 | ASSERT_EFI_ERROR (Status);\r | |
751 | \r | |
752 | //\r | |
753 | // Print debug message that the SMRAM window is now closed.\r | |
754 | //\r | |
755 | DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r | |
5657b268 | 756 | }\r |
757 | }\r | |
758 | \r | |
e42e9404 | 759 | /**\r |
760 | Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r | |
761 | \r | |
762 | @param Event The Event that is being processed, not used.\r | |
763 | @param Context Event Context, not used.\r | |
764 | \r | |
765 | **/\r | |
766 | VOID\r | |
767 | EFIAPI\r | |
768 | SmmIplSmmConfigurationEventNotify (\r | |
769 | IN EFI_EVENT Event,\r | |
770 | IN VOID *Context\r | |
771 | )\r | |
772 | {\r | |
773 | EFI_STATUS Status;\r | |
774 | EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r | |
775 | \r | |
776 | //\r | |
777 | // Make sure this notification is for this handler\r | |
778 | //\r | |
779 | Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);\r | |
780 | if (EFI_ERROR (Status)) {\r | |
781 | return;\r | |
782 | }\r | |
783 | \r | |
784 | //\r | |
4be497df | 785 | // Register the SMM Entry Point provided by the SMM Core with the SMM Configuration protocol\r |
e42e9404 | 786 | //\r |
787 | Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);\r | |
788 | ASSERT_EFI_ERROR (Status);\r | |
789 | \r | |
790 | //\r | |
d1102dba | 791 | // Set flag to indicate that the SMM Entry Point has been registered which\r |
e42e9404 | 792 | // means that SMIs are now fully operational.\r |
793 | //\r | |
794 | gSmmCorePrivate->SmmEntryPointRegistered = TRUE;\r | |
795 | \r | |
796 | //\r | |
797 | // Print debug message showing SMM Core entry point address.\r | |
798 | //\r | |
799 | DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));\r | |
e42e9404 | 800 | }\r |
801 | \r | |
802 | /**\r | |
803 | Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r | |
01331951 | 804 | or if gEfiEventReadyToBootGuid is signaled.\r |
e42e9404 | 805 | \r |
806 | @param Event The Event that is being processed, not used.\r | |
807 | @param Context Event Context, not used.\r | |
808 | \r | |
809 | **/\r | |
810 | VOID\r | |
811 | EFIAPI\r | |
812 | SmmIplReadyToLockEventNotify (\r | |
813 | IN EFI_EVENT Event,\r | |
814 | IN VOID *Context\r | |
815 | )\r | |
816 | {\r | |
817 | EFI_STATUS Status;\r | |
818 | VOID *Interface;\r | |
819 | UINTN Index;\r | |
820 | \r | |
821 | //\r | |
822 | // See if we are already locked\r | |
823 | //\r | |
824 | if (mSmmLocked) {\r | |
825 | return;\r | |
826 | }\r | |
d1102dba | 827 | \r |
e42e9404 | 828 | //\r |
829 | // Make sure this notification is for this handler\r | |
830 | //\r | |
831 | if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {\r | |
832 | Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);\r | |
833 | if (EFI_ERROR (Status)) {\r | |
834 | return;\r | |
835 | }\r | |
836 | } else {\r | |
837 | //\r | |
d1102dba | 838 | // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being\r |
01331951 | 839 | // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.\r |
e42e9404 | 840 | // Print a warning on debug builds.\r |
841 | //\r | |
842 | DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));\r | |
843 | }\r | |
844 | \r | |
265fff39 JF |
845 | if (!mEndOfDxe) {\r |
846 | DEBUG ((DEBUG_ERROR, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));\r | |
847 | REPORT_STATUS_CODE (\r | |
848 | EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,\r | |
849 | (EFI_SOFTWARE_SMM_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)\r | |
850 | );\r | |
851 | ASSERT (FALSE);\r | |
852 | }\r | |
853 | \r | |
e42e9404 | 854 | //\r |
855 | // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r | |
856 | //\r | |
857 | mSmmAccess->Lock (mSmmAccess);\r | |
d1102dba | 858 | \r |
e42e9404 | 859 | //\r |
d1102dba LG |
860 | // Close protocol and event notification events that do not apply after the\r |
861 | // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot\r | |
e42e9404 | 862 | // event has been signalled.\r |
863 | //\r | |
864 | for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r | |
865 | if (mSmmIplEvents[Index].CloseOnLock) {\r | |
866 | gBS->CloseEvent (mSmmIplEvents[Index].Event);\r | |
867 | }\r | |
868 | }\r | |
869 | \r | |
870 | //\r | |
871 | // Inform SMM Core that the DxeSmmReadyToLock protocol was installed\r | |
872 | //\r | |
873 | SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);\r | |
874 | \r | |
875 | //\r | |
876 | // Print debug message that the SMRAM window is now locked.\r | |
877 | //\r | |
878 | DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));\r | |
d1102dba | 879 | \r |
e42e9404 | 880 | //\r |
881 | // Set flag so this operation will not be performed again\r | |
882 | //\r | |
883 | mSmmLocked = TRUE;\r | |
884 | }\r | |
885 | \r | |
886 | /**\r | |
887 | Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r | |
888 | \r | |
889 | This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r | |
890 | It convers pointer to new virtual address.\r | |
891 | \r | |
892 | @param Event Event whose notification function is being invoked.\r | |
893 | @param Context Pointer to the notification function's context.\r | |
894 | \r | |
895 | **/\r | |
896 | VOID\r | |
897 | EFIAPI\r | |
898 | SmmIplSetVirtualAddressNotify (\r | |
899 | IN EFI_EVENT Event,\r | |
900 | IN VOID *Context\r | |
901 | )\r | |
902 | {\r | |
903 | EfiConvertPointer (0x0, (VOID **)&mSmmControl2);\r | |
904 | }\r | |
905 | \r | |
3c447c27 | 906 | /**\r |
2048c585 | 907 | Get the fixed loading address from image header assigned by build tool. This function only be called\r |
3c447c27 | 908 | when Loading module at Fixed address feature enabled.\r |
e42e9404 | 909 | \r |
3c447c27 | 910 | @param ImageContext Pointer to the image context structure that describes the PE/COFF\r |
911 | image that needs to be examined by this function.\r | |
912 | @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r | |
2048c585 | 913 | @retval EFI_NOT_FOUND The image has no assigned fixed loading address.\r |
3c447c27 | 914 | **/\r |
915 | EFI_STATUS\r | |
1436aea4 | 916 | GetPeCoffImageFixLoadingAssignedAddress (\r |
3c447c27 | 917 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r |
918 | )\r | |
919 | {\r | |
1436aea4 MK |
920 | UINTN SectionHeaderOffset;\r |
921 | EFI_STATUS Status;\r | |
922 | EFI_IMAGE_SECTION_HEADER SectionHeader;\r | |
923 | EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r | |
924 | EFI_PHYSICAL_ADDRESS FixLoadingAddress;\r | |
925 | UINT16 Index;\r | |
926 | UINTN Size;\r | |
927 | UINT16 NumberOfSections;\r | |
928 | EFI_PHYSICAL_ADDRESS SmramBase;\r | |
929 | UINT64 SmmCodeSize;\r | |
930 | UINT64 ValueInSectionHeader;\r | |
931 | \r | |
932 | //\r | |
933 | // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r | |
934 | //\r | |
935 | SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32 (PcdLoadFixAddressSmmCodePageNumber));\r | |
936 | \r | |
937 | FixLoadingAddress = 0;\r | |
938 | Status = EFI_NOT_FOUND;\r | |
939 | SmramBase = mLMFAConfigurationTable->SmramBase;\r | |
940 | //\r | |
941 | // Get PeHeader pointer\r | |
942 | //\r | |
943 | ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8 *)ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r | |
944 | SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +\r | |
945 | sizeof (UINT32) +\r | |
946 | sizeof (EFI_IMAGE_FILE_HEADER) +\r | |
947 | ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r | |
948 | NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r | |
949 | \r | |
950 | //\r | |
951 | // Get base address from the first section header that doesn't point to code section.\r | |
952 | //\r | |
953 | for (Index = 0; Index < NumberOfSections; Index++) {\r | |
954 | //\r | |
955 | // Read section header from file\r | |
956 | //\r | |
957 | Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
958 | Status = ImageContext->ImageRead (\r | |
959 | ImageContext->Handle,\r | |
960 | SectionHeaderOffset,\r | |
961 | &Size,\r | |
962 | &SectionHeader\r | |
963 | );\r | |
964 | if (EFI_ERROR (Status)) {\r | |
965 | return Status;\r | |
966 | }\r | |
967 | \r | |
968 | Status = EFI_NOT_FOUND;\r | |
969 | \r | |
970 | if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r | |
971 | //\r | |
972 | // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the\r | |
973 | // first section header that doesn't point to code section in image header. And there is an assumption that when the\r | |
974 | // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers\r | |
975 | // fields should NOT be Zero, or else, these 2 fields should be set to Zero\r | |
976 | //\r | |
977 | ValueInSectionHeader = ReadUnaligned64 ((UINT64 *)&SectionHeader.PointerToRelocations);\r | |
978 | if (ValueInSectionHeader != 0) {\r | |
979 | //\r | |
980 | // Found first section header that doesn't point to code section in which build tool saves the\r | |
981 | // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r | |
982 | //\r | |
983 | FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r | |
984 | \r | |
985 | if ((SmramBase + SmmCodeSize > FixLoadingAddress) && (SmramBase <= FixLoadingAddress)) {\r | |
986 | //\r | |
987 | // The assigned address is valid. Return the specified loading address\r | |
988 | //\r | |
989 | ImageContext->ImageAddress = FixLoadingAddress;\r | |
990 | Status = EFI_SUCCESS;\r | |
991 | }\r | |
992 | }\r | |
993 | \r | |
994 | break;\r | |
995 | }\r | |
996 | \r | |
997 | SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
998 | }\r | |
999 | \r | |
1000 | DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress, Status));\r | |
1001 | return Status;\r | |
3c447c27 | 1002 | }\r |
1436aea4 | 1003 | \r |
e42e9404 | 1004 | /**\r |
1005 | Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r | |
1006 | \r | |
d1102dba | 1007 | @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the\r |
c03beb76 SZ |
1008 | currently executing image, the rang of SMRAM to\r |
1009 | hold SMM Core will be excluded.\r | |
1010 | @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.\r | |
1011 | \r | |
1012 | @param[in] Context Context to pass into SMM Core\r | |
e42e9404 | 1013 | \r |
1014 | @return EFI_STATUS\r | |
1015 | \r | |
1016 | **/\r | |
1017 | EFI_STATUS\r | |
1018 | ExecuteSmmCoreFromSmram (\r | |
1436aea4 MK |
1019 | IN OUT EFI_SMRAM_DESCRIPTOR *SmramRange,\r |
1020 | IN OUT EFI_SMRAM_DESCRIPTOR *SmramRangeSmmCore,\r | |
1021 | IN VOID *Context\r | |
e42e9404 | 1022 | )\r |
1023 | {\r | |
1024 | EFI_STATUS Status;\r | |
1025 | VOID *SourceBuffer;\r | |
1026 | UINTN SourceSize;\r | |
1027 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r | |
1028 | UINTN PageCount;\r | |
e42e9404 | 1029 | EFI_IMAGE_ENTRY_POINT EntryPoint;\r |
1030 | \r | |
1031 | //\r | |
1032 | // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE\r | |
d1102dba | 1033 | //\r |
d7aaf1dc | 1034 | Status = GetSectionFromAnyFvByFileType (\r |
d1102dba | 1035 | EFI_FV_FILETYPE_SMM_CORE,\r |
d7aaf1dc | 1036 | 0,\r |
d1102dba | 1037 | EFI_SECTION_PE32,\r |
d7aaf1dc | 1038 | 0,\r |
d1102dba | 1039 | &SourceBuffer,\r |
d7aaf1dc LG |
1040 | &SourceSize\r |
1041 | );\r | |
1042 | if (EFI_ERROR (Status)) {\r | |
1043 | return Status;\r | |
e42e9404 | 1044 | }\r |
d1102dba | 1045 | \r |
e42e9404 | 1046 | //\r |
4be497df | 1047 | // Initialize ImageContext\r |
e42e9404 | 1048 | //\r |
1049 | ImageContext.Handle = SourceBuffer;\r | |
1050 | ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r | |
1051 | \r | |
1052 | //\r | |
1053 | // Get information about the image being loaded\r | |
1054 | //\r | |
1055 | Status = PeCoffLoaderGetImageInfo (&ImageContext);\r | |
1056 | if (EFI_ERROR (Status)) {\r | |
1057 | return Status;\r | |
1058 | }\r | |
1436aea4 | 1059 | \r |
e42e9404 | 1060 | //\r |
d1102dba | 1061 | // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to\r |
3c447c27 | 1062 | // the address assigned by build tool.\r |
e42e9404 | 1063 | //\r |
1436aea4 | 1064 | if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0) {\r |
3c447c27 | 1065 | //\r |
1066 | // Get the fixed loading address assigned by Build tool\r | |
1067 | //\r | |
1068 | Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);\r | |
1069 | if (!EFI_ERROR (Status)) {\r | |
1070 | //\r | |
1071 | // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range\r | |
1072 | //\r | |
1073 | PageCount = 0;\r | |
a7d10bb0 LG |
1074 | //\r |
1075 | // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.\r | |
1076 | //\r | |
1436aea4 | 1077 | gSmmCorePrivate->SmramRangeCount--;\r |
c03beb76 | 1078 | } else {\r |
87000d77 | 1079 | DEBUG ((DEBUG_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));\r |
3c447c27 | 1080 | //\r |
d1102dba | 1081 | // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR\r |
3c447c27 | 1082 | // specified by SmramRange\r |
1083 | //\r | |
1436aea4 | 1084 | PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r |
e42e9404 | 1085 | \r |
3c447c27 | 1086 | ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r |
1087 | ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r | |
e42e9404 | 1088 | \r |
1436aea4 MK |
1089 | SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r |
1090 | SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;\r | |
c03beb76 | 1091 | SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;\r |
1436aea4 MK |
1092 | SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;\r |
1093 | SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);\r | |
e42e9404 | 1094 | \r |
3c447c27 | 1095 | //\r |
c03beb76 | 1096 | // Align buffer on section boundary\r |
3c447c27 | 1097 | //\r |
c03beb76 | 1098 | ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;\r |
3c447c27 | 1099 | }\r |
1100 | } else {\r | |
1101 | //\r | |
d1102dba | 1102 | // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR\r |
3c447c27 | 1103 | // specified by SmramRange\r |
1104 | //\r | |
1436aea4 | 1105 | PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r |
3c447c27 | 1106 | \r |
1107 | ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r | |
1108 | ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r | |
1109 | \r | |
1436aea4 MK |
1110 | SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r |
1111 | SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;\r | |
c03beb76 | 1112 | SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;\r |
1436aea4 MK |
1113 | SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;\r |
1114 | SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);\r | |
3c447c27 | 1115 | \r |
1116 | //\r | |
c03beb76 | 1117 | // Align buffer on section boundary\r |
3c447c27 | 1118 | //\r |
c03beb76 | 1119 | ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;\r |
3c447c27 | 1120 | }\r |
d1102dba | 1121 | \r |
e42e9404 | 1122 | ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r |
16f69227 | 1123 | ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r |
e42e9404 | 1124 | \r |
1125 | //\r | |
1126 | // Print debug message showing SMM Core load address.\r | |
1127 | //\r | |
1128 | DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));\r | |
1129 | \r | |
1130 | //\r | |
1131 | // Load the image to our new buffer\r | |
1132 | //\r | |
1133 | Status = PeCoffLoaderLoadImage (&ImageContext);\r | |
1134 | if (!EFI_ERROR (Status)) {\r | |
1135 | //\r | |
1136 | // Relocate the image in our new buffer\r | |
1137 | //\r | |
1138 | Status = PeCoffLoaderRelocateImage (&ImageContext);\r | |
1139 | if (!EFI_ERROR (Status)) {\r | |
1140 | //\r | |
1141 | // Flush the instruction cache so the image data are written before we execute it\r | |
1142 | //\r | |
1143 | InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r | |
1144 | \r | |
1145 | //\r | |
1146 | // Print debug message showing SMM Core entry point address.\r | |
1147 | //\r | |
1148 | DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));\r | |
1149 | \r | |
84edd20b SZ |
1150 | gSmmCorePrivate->PiSmmCoreImageBase = ImageContext.ImageAddress;\r |
1151 | gSmmCorePrivate->PiSmmCoreImageSize = ImageContext.ImageSize;\r | |
1152 | DEBUG ((DEBUG_INFO, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageBase));\r | |
1153 | DEBUG ((DEBUG_INFO, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageSize));\r | |
1154 | \r | |
1155 | gSmmCorePrivate->PiSmmCoreEntryPoint = ImageContext.EntryPoint;\r | |
1156 | \r | |
e42e9404 | 1157 | //\r |
1158 | // Execute image\r | |
1159 | //\r | |
1160 | EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;\r | |
1436aea4 | 1161 | Status = EntryPoint ((EFI_HANDLE)Context, gST);\r |
e42e9404 | 1162 | }\r |
1163 | }\r | |
1164 | \r | |
1165 | //\r | |
4be497df | 1166 | // Always free memory allocated by GetFileBufferByFilePath ()\r |
e42e9404 | 1167 | //\r |
c03beb76 SZ |
1168 | FreePool (SourceBuffer);\r |
1169 | \r | |
1170 | return Status;\r | |
1171 | }\r | |
1172 | \r | |
1173 | /**\r | |
1174 | SMM split SMRAM entry.\r | |
1175 | \r | |
1176 | @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.\r | |
1177 | @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.\r | |
1178 | @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.\r | |
1179 | @param[in, out] RangeCount Pointer to range count.\r | |
1180 | @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.\r | |
1181 | @param[in, out] ReservedRangeCount Pointer to reserved range count.\r | |
1182 | @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry\r | |
1183 | that no need to be split anymore.\r | |
1184 | @param[in, out] FinalRangeCount Pointer to final range count.\r | |
1185 | \r | |
1186 | **/\r | |
1187 | VOID\r | |
1188 | SmmSplitSmramEntry (\r | |
1189 | IN OUT EFI_SMRAM_DESCRIPTOR *RangeToCompare,\r | |
1190 | IN OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare,\r | |
1191 | OUT EFI_SMRAM_DESCRIPTOR *Ranges,\r | |
1192 | IN OUT UINTN *RangeCount,\r | |
1193 | OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRanges,\r | |
1194 | IN OUT UINTN *ReservedRangeCount,\r | |
1195 | OUT EFI_SMRAM_DESCRIPTOR *FinalRanges,\r | |
1196 | IN OUT UINTN *FinalRangeCount\r | |
1197 | )\r | |
1198 | {\r | |
1436aea4 MK |
1199 | UINT64 RangeToCompareEnd;\r |
1200 | UINT64 ReservedRangeToCompareEnd;\r | |
c03beb76 SZ |
1201 | \r |
1202 | RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;\r | |
1203 | ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;\r | |
1204 | \r | |
1205 | if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&\r | |
1436aea4 MK |
1206 | (RangeToCompare->CpuStart < ReservedRangeToCompareEnd))\r |
1207 | {\r | |
c03beb76 SZ |
1208 | if (RangeToCompareEnd < ReservedRangeToCompareEnd) {\r |
1209 | //\r | |
1210 | // RangeToCompare ReservedRangeToCompare\r | |
1211 | // ---- ---- --------------------------------------\r | |
1212 | // | | | | -> 1. ReservedRangeToCompare\r | |
1213 | // ---- | | |--| --------------------------------------\r | |
1214 | // | | | | | |\r | |
1215 | // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r | |
1216 | // | | | | | | RangeToCompare->PhysicalSize = 0\r | |
1217 | // ---- | | |--| --------------------------------------\r | |
1218 | // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount\r | |
1219 | // ---- ---- --------------------------------------\r | |
1220 | //\r | |
1221 | \r | |
1222 | //\r | |
1223 | // 1. Update ReservedRangeToCompare.\r | |
1224 | //\r | |
1225 | ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;\r | |
1226 | //\r | |
1227 | // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r | |
1228 | // Zero RangeToCompare->PhysicalSize.\r | |
1229 | //\r | |
1230 | FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;\r | |
1231 | FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;\r | |
1232 | FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r | |
1233 | FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompare->PhysicalSize;\r | |
1436aea4 MK |
1234 | *FinalRangeCount += 1;\r |
1235 | RangeToCompare->PhysicalSize = 0;\r | |
c03beb76 SZ |
1236 | //\r |
1237 | // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.\r | |
1238 | //\r | |
1239 | ReservedRanges[*ReservedRangeCount].SmramReservedStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r | |
1240 | ReservedRanges[*ReservedRangeCount].SmramReservedSize = ReservedRangeToCompareEnd - RangeToCompareEnd;\r | |
1436aea4 | 1241 | *ReservedRangeCount += 1;\r |
c03beb76 SZ |
1242 | } else {\r |
1243 | //\r | |
1244 | // RangeToCompare ReservedRangeToCompare\r | |
1245 | // ---- ---- --------------------------------------\r | |
1246 | // | | | | -> 1. ReservedRangeToCompare\r | |
1247 | // ---- | | |--| --------------------------------------\r | |
1248 | // | | | | | |\r | |
1249 | // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r | |
1250 | // | | | | | |\r | |
1251 | // | | ---- |--| --------------------------------------\r | |
1252 | // | | | | -> 3. RangeToCompare\r | |
1253 | // ---- ---- --------------------------------------\r | |
1254 | //\r | |
1255 | \r | |
1256 | //\r | |
1257 | // 1. Update ReservedRangeToCompare.\r | |
1258 | //\r | |
1259 | ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;\r | |
1260 | //\r | |
1261 | // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r | |
1262 | //\r | |
1263 | FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;\r | |
1264 | FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;\r | |
1265 | FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r | |
1266 | FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompareEnd - RangeToCompare->CpuStart;\r | |
1436aea4 | 1267 | *FinalRangeCount += 1;\r |
c03beb76 SZ |
1268 | //\r |
1269 | // 3. Update RangeToCompare.\r | |
1270 | //\r | |
1271 | RangeToCompare->CpuStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r | |
1272 | RangeToCompare->PhysicalStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r | |
1273 | RangeToCompare->PhysicalSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r | |
1274 | }\r | |
1275 | } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&\r | |
1436aea4 MK |
1276 | (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd))\r |
1277 | {\r | |
c03beb76 SZ |
1278 | if (ReservedRangeToCompareEnd < RangeToCompareEnd) {\r |
1279 | //\r | |
1280 | // RangeToCompare ReservedRangeToCompare\r | |
1281 | // ---- ---- --------------------------------------\r | |
1282 | // | | | | -> 1. RangeToCompare\r | |
1283 | // | | ---- |--| --------------------------------------\r | |
1284 | // | | | | | |\r | |
1285 | // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r | |
1286 | // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0\r | |
1287 | // | | ---- |--| --------------------------------------\r | |
1288 | // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount\r | |
1289 | // ---- ---- --------------------------------------\r | |
1290 | //\r | |
1291 | \r | |
1292 | //\r | |
1293 | // 1. Update RangeToCompare.\r | |
1294 | //\r | |
1295 | RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;\r | |
1296 | //\r | |
1297 | // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r | |
1298 | // ReservedRangeToCompare->SmramReservedSize = 0\r | |
1299 | //\r | |
1300 | FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;\r | |
1301 | FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;\r | |
1302 | FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r | |
1303 | FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompare->SmramReservedSize;\r | |
1436aea4 MK |
1304 | *FinalRangeCount += 1;\r |
1305 | ReservedRangeToCompare->SmramReservedSize = 0;\r | |
c03beb76 SZ |
1306 | //\r |
1307 | // 3. Update Ranges[*RangeCount] and increment *RangeCount.\r | |
1308 | //\r | |
1309 | Ranges[*RangeCount].CpuStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r | |
1310 | Ranges[*RangeCount].PhysicalStart = FinalRanges[*FinalRangeCount - 1].PhysicalStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r | |
1311 | Ranges[*RangeCount].RegionState = RangeToCompare->RegionState;\r | |
1312 | Ranges[*RangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompareEnd;\r | |
1436aea4 | 1313 | *RangeCount += 1;\r |
c03beb76 SZ |
1314 | } else {\r |
1315 | //\r | |
1316 | // RangeToCompare ReservedRangeToCompare\r | |
1317 | // ---- ---- --------------------------------------\r | |
1318 | // | | | | -> 1. RangeToCompare\r | |
1319 | // | | ---- |--| --------------------------------------\r | |
1320 | // | | | | | |\r | |
1321 | // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r | |
1322 | // | | | | | |\r | |
1323 | // ---- | | |--| --------------------------------------\r | |
1324 | // | | | | -> 3. ReservedRangeToCompare\r | |
1325 | // ---- ---- --------------------------------------\r | |
1326 | //\r | |
1327 | \r | |
1328 | //\r | |
1329 | // 1. Update RangeToCompare.\r | |
1330 | //\r | |
1331 | RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;\r | |
1332 | //\r | |
1333 | // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r | |
1334 | // ReservedRangeToCompare->SmramReservedSize = 0\r | |
1335 | //\r | |
1336 | FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;\r | |
1337 | FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;\r | |
1338 | FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r | |
1339 | FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompare->SmramReservedStart;\r | |
1436aea4 | 1340 | *FinalRangeCount += 1;\r |
c03beb76 SZ |
1341 | //\r |
1342 | // 3. Update ReservedRangeToCompare.\r | |
1343 | //\r | |
1344 | ReservedRangeToCompare->SmramReservedStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r | |
1345 | ReservedRangeToCompare->SmramReservedSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r | |
1346 | }\r | |
1347 | }\r | |
1348 | }\r | |
1349 | \r | |
1350 | /**\r | |
1351 | Returns if SMRAM range and SMRAM reserved range are overlapped.\r | |
1352 | \r | |
1353 | @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.\r | |
1354 | @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.\r | |
1355 | \r | |
1356 | @retval TRUE There is overlap.\r | |
cab1f025 | 1357 | @retval TRUE Math error.\r |
c03beb76 SZ |
1358 | @retval FALSE There is no overlap.\r |
1359 | \r | |
1360 | **/\r | |
1361 | BOOLEAN\r | |
1362 | SmmIsSmramOverlap (\r | |
1363 | IN EFI_SMRAM_DESCRIPTOR *RangeToCompare,\r | |
1364 | IN EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare\r | |
1365 | )\r | |
1366 | {\r | |
cab1f025 MD |
1367 | UINT64 RangeToCompareEnd;\r |
1368 | UINT64 ReservedRangeToCompareEnd;\r | |
1369 | BOOLEAN IsOverUnderflow1;\r | |
1370 | BOOLEAN IsOverUnderflow2;\r | |
1371 | \r | |
1372 | // Check for over or underflow.\r | |
1373 | IsOverUnderflow1 = EFI_ERROR (\r | |
1374 | SafeUint64Add (\r | |
1375 | (UINT64)RangeToCompare->CpuStart,\r | |
1376 | RangeToCompare->PhysicalSize,\r | |
1377 | &RangeToCompareEnd\r | |
1378 | )\r | |
1379 | );\r | |
1380 | IsOverUnderflow2 = EFI_ERROR (\r | |
1381 | SafeUint64Add (\r | |
1382 | (UINT64)ReservedRangeToCompare->SmramReservedStart,\r | |
1383 | ReservedRangeToCompare->SmramReservedSize,\r | |
1384 | &ReservedRangeToCompareEnd\r | |
1385 | )\r | |
1386 | );\r | |
1387 | if (IsOverUnderflow1 || IsOverUnderflow2) {\r | |
1388 | return TRUE;\r | |
1389 | }\r | |
c03beb76 SZ |
1390 | \r |
1391 | if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&\r | |
1436aea4 MK |
1392 | (RangeToCompare->CpuStart < ReservedRangeToCompareEnd))\r |
1393 | {\r | |
c03beb76 SZ |
1394 | return TRUE;\r |
1395 | } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&\r | |
1436aea4 MK |
1396 | (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd))\r |
1397 | {\r | |
c03beb76 | 1398 | return TRUE;\r |
e42e9404 | 1399 | }\r |
1436aea4 | 1400 | \r |
c03beb76 SZ |
1401 | return FALSE;\r |
1402 | }\r | |
1403 | \r | |
1404 | /**\r | |
1405 | Get full SMRAM ranges.\r | |
e42e9404 | 1406 | \r |
c03beb76 SZ |
1407 | It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from\r |
1408 | SmmConfiguration protocol, split the entries if there is overlap between them.\r | |
1409 | It will also reserve one entry for SMM core.\r | |
1410 | \r | |
1411 | @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.\r | |
1412 | \r | |
1413 | @return Pointer to full SMRAM ranges.\r | |
1414 | \r | |
1415 | **/\r | |
1416 | EFI_SMRAM_DESCRIPTOR *\r | |
1417 | GetFullSmramRanges (\r | |
1436aea4 | 1418 | OUT UINTN *FullSmramRangeCount\r |
c03beb76 SZ |
1419 | )\r |
1420 | {\r | |
1436aea4 MK |
1421 | EFI_STATUS Status;\r |
1422 | EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r | |
1423 | UINTN Size;\r | |
1424 | UINTN Index;\r | |
1425 | UINTN Index2;\r | |
1426 | EFI_SMRAM_DESCRIPTOR *FullSmramRanges;\r | |
1427 | UINTN TempSmramRangeCount;\r | |
1428 | UINTN AdditionSmramRangeCount;\r | |
1429 | EFI_SMRAM_DESCRIPTOR *TempSmramRanges;\r | |
1430 | UINTN SmramRangeCount;\r | |
1431 | EFI_SMRAM_DESCRIPTOR *SmramRanges;\r | |
1432 | UINTN SmramReservedCount;\r | |
1433 | EFI_SMM_RESERVED_SMRAM_REGION *SmramReservedRanges;\r | |
1434 | UINTN MaxCount;\r | |
1435 | BOOLEAN Rescan;\r | |
c03beb76 SZ |
1436 | \r |
1437 | //\r | |
1438 | // Get SMM Configuration Protocol if it is present.\r | |
e42e9404 | 1439 | //\r |
c03beb76 | 1440 | SmmConfiguration = NULL;\r |
1436aea4 | 1441 | Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **)&SmmConfiguration);\r |
c03beb76 | 1442 | \r |
e42e9404 | 1443 | //\r |
c03beb76 SZ |
1444 | // Get SMRAM information.\r |
1445 | //\r | |
1436aea4 | 1446 | Size = 0;\r |
c03beb76 SZ |
1447 | Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);\r |
1448 | ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r | |
e42e9404 | 1449 | \r |
c03beb76 SZ |
1450 | SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r |
1451 | \r | |
1452 | //\r | |
1453 | // Get SMRAM reserved region count.\r | |
1454 | //\r | |
1455 | SmramReservedCount = 0;\r | |
1456 | if (SmmConfiguration != NULL) {\r | |
1457 | while (SmmConfiguration->SmramReservedRegions[SmramReservedCount].SmramReservedSize != 0) {\r | |
1458 | SmramReservedCount++;\r | |
1459 | }\r | |
1460 | }\r | |
1461 | \r | |
a7d10bb0 LG |
1462 | //\r |
1463 | // Reserve one entry for SMM Core in the full SMRAM ranges.\r | |
1464 | //\r | |
1465 | AdditionSmramRangeCount = 1;\r | |
1436aea4 | 1466 | if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0) {\r |
a7d10bb0 LG |
1467 | //\r |
1468 | // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.\r | |
1469 | //\r | |
1470 | AdditionSmramRangeCount = 2;\r | |
1471 | }\r | |
1472 | \r | |
c03beb76 SZ |
1473 | if (SmramReservedCount == 0) {\r |
1474 | //\r | |
1475 | // No reserved SMRAM entry from SMM Configuration Protocol.\r | |
c03beb76 | 1476 | //\r |
a7d10bb0 | 1477 | *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;\r |
1436aea4 MK |
1478 | Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);\r |
1479 | FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocateZeroPool (Size);\r | |
c03beb76 SZ |
1480 | ASSERT (FullSmramRanges != NULL);\r |
1481 | \r | |
1482 | Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, FullSmramRanges);\r | |
1483 | ASSERT_EFI_ERROR (Status);\r | |
1484 | \r | |
1485 | return FullSmramRanges;\r | |
1486 | }\r | |
1487 | \r | |
1488 | //\r | |
1489 | // Why MaxCount = X + 2 * Y?\r | |
1490 | // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.\r | |
1491 | //\r | |
1492 | // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1\r | |
1493 | // ---- ----\r | |
1494 | // | | ---- |--|\r | |
1495 | // | | | | -> | |\r | |
1496 | // | | ---- |--|\r | |
1497 | // ---- ----\r | |
1498 | //\r | |
1499 | // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1\r | |
1500 | // ---- ----\r | |
1501 | // | | | |\r | |
1502 | // | | ---- |--|\r | |
1503 | // | | | | | |\r | |
1504 | // |--| | | -> |--|\r | |
1505 | // | | | | | |\r | |
1506 | // | | ---- |--|\r | |
1507 | // | | | |\r | |
1508 | // ---- ----\r | |
1509 | //\r | |
1510 | // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1\r | |
1511 | // ---- ----\r | |
1512 | // | | | |\r | |
1513 | // | | ---- |--|\r | |
1514 | // |--| | | |--|\r | |
1515 | // | | | | -> | |\r | |
1516 | // |--| | | |--|\r | |
1517 | // | | ---- |--|\r | |
1518 | // | | | |\r | |
1519 | // ---- ----\r | |
1520 | //\r | |
1521 | // ......\r | |
1522 | //\r | |
1523 | MaxCount = SmramRangeCount + 2 * SmramReservedCount;\r | |
1524 | \r | |
1436aea4 MK |
1525 | Size = MaxCount * sizeof (EFI_SMM_RESERVED_SMRAM_REGION);\r |
1526 | SmramReservedRanges = (EFI_SMM_RESERVED_SMRAM_REGION *)AllocatePool (Size);\r | |
c03beb76 SZ |
1527 | ASSERT (SmramReservedRanges != NULL);\r |
1528 | for (Index = 0; Index < SmramReservedCount; Index++) {\r | |
1529 | CopyMem (&SmramReservedRanges[Index], &SmmConfiguration->SmramReservedRegions[Index], sizeof (EFI_SMM_RESERVED_SMRAM_REGION));\r | |
1530 | }\r | |
1531 | \r | |
1436aea4 MK |
1532 | Size = MaxCount * sizeof (EFI_SMRAM_DESCRIPTOR);\r |
1533 | TempSmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);\r | |
c03beb76 SZ |
1534 | ASSERT (TempSmramRanges != NULL);\r |
1535 | TempSmramRangeCount = 0;\r | |
1536 | \r | |
1436aea4 | 1537 | SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);\r |
c03beb76 SZ |
1538 | ASSERT (SmramRanges != NULL);\r |
1539 | Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, SmramRanges);\r | |
1540 | ASSERT_EFI_ERROR (Status);\r | |
1541 | \r | |
1542 | do {\r | |
1543 | Rescan = FALSE;\r | |
1544 | for (Index = 0; (Index < SmramRangeCount) && !Rescan; Index++) {\r | |
1545 | //\r | |
1546 | // Skip zero size entry.\r | |
1547 | //\r | |
1548 | if (SmramRanges[Index].PhysicalSize != 0) {\r | |
1549 | for (Index2 = 0; (Index2 < SmramReservedCount) && !Rescan; Index2++) {\r | |
1550 | //\r | |
1551 | // Skip zero size entry.\r | |
1552 | //\r | |
1553 | if (SmramReservedRanges[Index2].SmramReservedSize != 0) {\r | |
1554 | if (SmmIsSmramOverlap (\r | |
1555 | &SmramRanges[Index],\r | |
1556 | &SmramReservedRanges[Index2]\r | |
1436aea4 MK |
1557 | ))\r |
1558 | {\r | |
c03beb76 SZ |
1559 | //\r |
1560 | // There is overlap, need to split entry and then rescan.\r | |
1561 | //\r | |
1562 | SmmSplitSmramEntry (\r | |
1563 | &SmramRanges[Index],\r | |
1564 | &SmramReservedRanges[Index2],\r | |
1565 | SmramRanges,\r | |
1566 | &SmramRangeCount,\r | |
1567 | SmramReservedRanges,\r | |
1568 | &SmramReservedCount,\r | |
1569 | TempSmramRanges,\r | |
1570 | &TempSmramRangeCount\r | |
1571 | );\r | |
1572 | Rescan = TRUE;\r | |
1573 | }\r | |
1574 | }\r | |
1575 | }\r | |
1436aea4 | 1576 | \r |
c03beb76 SZ |
1577 | if (!Rescan) {\r |
1578 | //\r | |
1579 | // No any overlap, copy the entry to the temp SMRAM ranges.\r | |
1580 | // Zero SmramRanges[Index].PhysicalSize = 0;\r | |
1581 | //\r | |
1582 | CopyMem (&TempSmramRanges[TempSmramRangeCount++], &SmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));\r | |
1583 | SmramRanges[Index].PhysicalSize = 0;\r | |
1584 | }\r | |
1585 | }\r | |
1586 | }\r | |
1587 | } while (Rescan);\r | |
1436aea4 | 1588 | \r |
c03beb76 SZ |
1589 | ASSERT (TempSmramRangeCount <= MaxCount);\r |
1590 | \r | |
1591 | //\r | |
a7d10bb0 | 1592 | // Sort the entries\r |
c03beb76 | 1593 | //\r |
a7d10bb0 | 1594 | FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));\r |
c03beb76 SZ |
1595 | ASSERT (FullSmramRanges != NULL);\r |
1596 | *FullSmramRangeCount = 0;\r | |
1597 | do {\r | |
1598 | for (Index = 0; Index < TempSmramRangeCount; Index++) {\r | |
1599 | if (TempSmramRanges[Index].PhysicalSize != 0) {\r | |
1600 | break;\r | |
1601 | }\r | |
1602 | }\r | |
1436aea4 | 1603 | \r |
c03beb76 SZ |
1604 | ASSERT (Index < TempSmramRangeCount);\r |
1605 | for (Index2 = 0; Index2 < TempSmramRangeCount; Index2++) {\r | |
1606 | if ((Index2 != Index) && (TempSmramRanges[Index2].PhysicalSize != 0) && (TempSmramRanges[Index2].CpuStart < TempSmramRanges[Index].CpuStart)) {\r | |
1607 | Index = Index2;\r | |
1608 | }\r | |
1609 | }\r | |
1436aea4 | 1610 | \r |
c03beb76 | 1611 | CopyMem (&FullSmramRanges[*FullSmramRangeCount], &TempSmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));\r |
1436aea4 | 1612 | *FullSmramRangeCount += 1;\r |
c03beb76 SZ |
1613 | TempSmramRanges[Index].PhysicalSize = 0;\r |
1614 | } while (*FullSmramRangeCount < TempSmramRangeCount);\r | |
1436aea4 | 1615 | \r |
c03beb76 | 1616 | ASSERT (*FullSmramRangeCount == TempSmramRangeCount);\r |
a7d10bb0 | 1617 | *FullSmramRangeCount += AdditionSmramRangeCount;\r |
c03beb76 SZ |
1618 | \r |
1619 | FreePool (SmramRanges);\r | |
1620 | FreePool (SmramReservedRanges);\r | |
1621 | FreePool (TempSmramRanges);\r | |
1622 | \r | |
1623 | return FullSmramRanges;\r | |
e42e9404 | 1624 | }\r |
1625 | \r | |
1626 | /**\r | |
1627 | The Entry Point for SMM IPL\r | |
1628 | \r | |
d1102dba LG |
1629 | Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install\r |
1630 | SMM Base 2 Protocol and SMM Communication Protocol, and register for the\r | |
e42e9404 | 1631 | critical events required to coordinate between DXE and SMM environments.\r |
d1102dba | 1632 | \r |
e42e9404 | 1633 | @param ImageHandle The firmware allocated handle for the EFI image.\r |
1634 | @param SystemTable A pointer to the EFI System Table.\r | |
1635 | \r | |
1636 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
1637 | @retval Other Some error occurred when executing this entry point.\r | |
1638 | \r | |
1639 | **/\r | |
1640 | EFI_STATUS\r | |
1641 | EFIAPI\r | |
1642 | SmmIplEntry (\r | |
1643 | IN EFI_HANDLE ImageHandle,\r | |
1644 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1645 | )\r | |
1646 | {\r | |
1436aea4 MK |
1647 | EFI_STATUS Status;\r |
1648 | UINTN Index;\r | |
1649 | UINT64 MaxSize;\r | |
1650 | VOID *Registration;\r | |
1651 | UINT64 SmmCodeSize;\r | |
1652 | EFI_CPU_ARCH_PROTOCOL *CpuArch;\r | |
1653 | EFI_STATUS SetAttrStatus;\r | |
1654 | EFI_SMRAM_DESCRIPTOR *SmramRangeSmmDriver;\r | |
1655 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;\r | |
e42e9404 | 1656 | \r |
1657 | //\r | |
d1102dba LG |
1658 | // Fill in the image handle of the SMM IPL so the SMM Core can use this as the\r |
1659 | // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded\r | |
e42e9404 | 1660 | // by the SMM Core\r |
1661 | //\r | |
1662 | mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r | |
1663 | \r | |
1664 | //\r | |
1665 | // Get SMM Access Protocol\r | |
1666 | //\r | |
1667 | Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);\r | |
1668 | ASSERT_EFI_ERROR (Status);\r | |
1669 | \r | |
1670 | //\r | |
1671 | // Get SMM Control2 Protocol\r | |
1672 | //\r | |
1673 | Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);\r | |
1674 | ASSERT_EFI_ERROR (Status);\r | |
1675 | \r | |
c03beb76 | 1676 | gSmmCorePrivate->SmramRanges = GetFullSmramRanges (&gSmmCorePrivate->SmramRangeCount);\r |
84edd20b | 1677 | \r |
e42e9404 | 1678 | //\r |
1679 | // Open all SMRAM ranges\r | |
1680 | //\r | |
1681 | Status = mSmmAccess->Open (mSmmAccess);\r | |
1682 | ASSERT_EFI_ERROR (Status);\r | |
1683 | \r | |
1684 | //\r | |
1685 | // Print debug message that the SMRAM window is now open.\r | |
1686 | //\r | |
1687 | DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r | |
d1102dba | 1688 | \r |
e42e9404 | 1689 | //\r |
06b07ce3 | 1690 | // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r |
e42e9404 | 1691 | //\r |
1692 | mCurrentSmramRange = NULL;\r | |
06b07ce3 | 1693 | for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r |
2c0f06f0 | 1694 | //\r |
1695 | // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization\r | |
1696 | //\r | |
1697 | if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {\r | |
1698 | continue;\r | |
1699 | }\r | |
1700 | \r | |
e42e9404 | 1701 | if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r |
10e4e4f6 | 1702 | if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {\r |
e42e9404 | 1703 | if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r |
1436aea4 | 1704 | MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r |
e42e9404 | 1705 | mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r |
1706 | }\r | |
1707 | }\r | |
1708 | }\r | |
1709 | }\r | |
1710 | \r | |
1711 | if (mCurrentSmramRange != NULL) {\r | |
1712 | //\r | |
1713 | // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r | |
1714 | //\r | |
1436aea4 MK |
1715 | DEBUG ((\r |
1716 | DEBUG_INFO,\r | |
1717 | "SMM IPL found SMRAM window %p - %p\n",\r | |
d1102dba | 1718 | (VOID *)(UINTN)mCurrentSmramRange->CpuStart,\r |
e42e9404 | 1719 | (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r |
1720 | ));\r | |
1721 | \r | |
40e8cca5 | 1722 | GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);\r |
e42e9404 | 1723 | //\r |
54ec85dd JW |
1724 | // Make sure we can change the desired memory attributes.\r |
1725 | //\r | |
1726 | Status = gDS->GetMemorySpaceDescriptor (\r | |
1727 | mSmramCacheBase,\r | |
1728 | &MemDesc\r | |
1729 | );\r | |
1730 | ASSERT_EFI_ERROR (Status);\r | |
1731 | if ((MemDesc.Capabilities & SMRAM_CAPABILITIES) != SMRAM_CAPABILITIES) {\r | |
1732 | gDS->SetMemorySpaceCapabilities (\r | |
1733 | mSmramCacheBase,\r | |
1734 | mSmramCacheSize,\r | |
1735 | MemDesc.Capabilities | SMRAM_CAPABILITIES\r | |
1736 | );\r | |
1737 | }\r | |
1436aea4 | 1738 | \r |
54ec85dd | 1739 | //\r |
94c0129d | 1740 | // If CPU AP is present, attempt to set SMRAM cacheability to WB and clear\r |
54ec85dd | 1741 | // all paging attributes.\r |
07d9dc83 | 1742 | // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP\r |
1743 | // is not available here.\r | |
e42e9404 | 1744 | //\r |
0a6c0905 | 1745 | CpuArch = NULL;\r |
1436aea4 | 1746 | Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);\r |
07d9dc83 | 1747 | if (!EFI_ERROR (Status)) {\r |
3bd5c994 | 1748 | MemDesc.Attributes &= ~(EFI_CACHE_ATTRIBUTE_MASK | EFI_MEMORY_ATTRIBUTE_MASK);\r |
54ec85dd | 1749 | MemDesc.Attributes |= EFI_MEMORY_WB;\r |
1436aea4 MK |
1750 | Status = gDS->SetMemorySpaceAttributes (\r |
1751 | mSmramCacheBase,\r | |
1752 | mSmramCacheSize,\r | |
1753 | MemDesc.Attributes\r | |
1754 | );\r | |
07d9dc83 | 1755 | if (EFI_ERROR (Status)) {\r |
1756 | DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r | |
94c0129d JW |
1757 | }\r |
1758 | \r | |
54ec85dd JW |
1759 | DEBUG_CODE (\r |
1760 | gDS->GetMemorySpaceDescriptor (\r | |
1761 | mSmramCacheBase,\r | |
1762 | &MemDesc\r | |
94c0129d | 1763 | );\r |
54ec85dd | 1764 | DEBUG ((DEBUG_INFO, "SMRAM attributes: %016lx\n", MemDesc.Attributes));\r |
3bd5c994 | 1765 | ASSERT ((MemDesc.Attributes & EFI_MEMORY_ATTRIBUTE_MASK) == 0);\r |
1436aea4 | 1766 | );\r |
07d9dc83 | 1767 | }\r |
1436aea4 | 1768 | \r |
3c447c27 | 1769 | //\r |
1770 | // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load\r | |
1771 | // Modules At Fixed Address Configuration Table.\r | |
1772 | //\r | |
1436aea4 | 1773 | if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0) {\r |
3c447c27 | 1774 | //\r |
1775 | // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r | |
1776 | //\r | |
1436aea4 | 1777 | SmmCodeSize = LShiftU64 (PcdGet32 (PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);\r |
3c447c27 | 1778 | //\r |
1779 | // The SMRAM available memory is assumed to be larger than SmmCodeSize\r | |
1780 | //\r | |
1781 | ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);\r | |
1782 | //\r | |
1783 | // Retrieve Load modules At fixed address configuration table and save the SMRAM base.\r | |
1784 | //\r | |
1785 | Status = EfiGetSystemConfigurationTable (\r | |
1436aea4 MK |
1786 | &gLoadFixedAddressConfigurationTableGuid,\r |
1787 | (VOID **)&mLMFAConfigurationTable\r | |
1788 | );\r | |
1789 | if (!EFI_ERROR (Status) && (mLMFAConfigurationTable != NULL)) {\r | |
c2aeb66f | 1790 | mLMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r |
2d5ac154 | 1791 | //\r |
1792 | // Print the SMRAM base\r | |
1793 | //\r | |
87000d77 | 1794 | DEBUG ((DEBUG_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable->SmramBase));\r |
3c447c27 | 1795 | }\r |
a7d10bb0 LG |
1796 | \r |
1797 | //\r | |
1798 | // Fill the Smram range for all SMM code\r | |
1799 | //\r | |
1436aea4 | 1800 | SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];\r |
a7d10bb0 LG |
1801 | SmramRangeSmmDriver->CpuStart = mCurrentSmramRange->CpuStart;\r |
1802 | SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;\r | |
1803 | SmramRangeSmmDriver->RegionState = mCurrentSmramRange->RegionState | EFI_ALLOCATED;\r | |
1804 | SmramRangeSmmDriver->PhysicalSize = SmmCodeSize;\r | |
1805 | \r | |
1436aea4 MK |
1806 | mCurrentSmramRange->PhysicalSize -= SmmCodeSize;\r |
1807 | mCurrentSmramRange->CpuStart = mCurrentSmramRange->CpuStart + SmmCodeSize;\r | |
1808 | mCurrentSmramRange->PhysicalStart = mCurrentSmramRange->PhysicalStart + SmmCodeSize;\r | |
3c447c27 | 1809 | }\r |
1436aea4 | 1810 | \r |
e42e9404 | 1811 | //\r |
1812 | // Load SMM Core into SMRAM and execute it from SMRAM\r | |
1813 | //\r | |
c03beb76 SZ |
1814 | Status = ExecuteSmmCoreFromSmram (\r |
1815 | mCurrentSmramRange,\r | |
1816 | &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 1],\r | |
1817 | gSmmCorePrivate\r | |
1818 | );\r | |
e42e9404 | 1819 | if (EFI_ERROR (Status)) {\r |
1820 | //\r | |
1821 | // Print error message that the SMM Core failed to be loaded and executed.\r | |
1822 | //\r | |
1823 | DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));\r | |
1824 | \r | |
1825 | //\r | |
1826 | // Attempt to reset SMRAM cacheability to UC\r | |
1827 | //\r | |
0a6c0905 | 1828 | if (CpuArch != NULL) {\r |
1436aea4 | 1829 | SetAttrStatus = gDS->SetMemorySpaceAttributes (\r |
d1102dba | 1830 | mSmramCacheBase,\r |
b07ea4c1 LE |
1831 | mSmramCacheSize,\r |
1832 | EFI_MEMORY_UC\r | |
1833 | );\r | |
1834 | if (EFI_ERROR (SetAttrStatus)) {\r | |
0a6c0905 | 1835 | DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r |
d1102dba | 1836 | }\r |
0a6c0905 | 1837 | }\r |
e42e9404 | 1838 | }\r |
1839 | } else {\r | |
1840 | //\r | |
1841 | // Print error message that there are not enough SMRAM resources to load the SMM Core.\r | |
1842 | //\r | |
1843 | DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));\r | |
1844 | }\r | |
1845 | \r | |
1846 | //\r | |
d1102dba | 1847 | // If the SMM Core could not be loaded then close SMRAM window, free allocated\r |
e42e9404 | 1848 | // resources, and return an error so SMM IPL will be unloaded.\r |
1849 | //\r | |
1436aea4 | 1850 | if ((mCurrentSmramRange == NULL) || EFI_ERROR (Status)) {\r |
e42e9404 | 1851 | //\r |
1852 | // Close all SMRAM ranges\r | |
1853 | //\r | |
1854 | Status = mSmmAccess->Close (mSmmAccess);\r | |
1855 | ASSERT_EFI_ERROR (Status);\r | |
1856 | \r | |
1857 | //\r | |
1858 | // Print debug message that the SMRAM window is now closed.\r | |
1859 | //\r | |
1860 | DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r | |
1861 | \r | |
1862 | //\r | |
1863 | // Free all allocated resources\r | |
1864 | //\r | |
1865 | FreePool (gSmmCorePrivate->SmramRanges);\r | |
63aa86b0 | 1866 | \r |
e42e9404 | 1867 | return EFI_UNSUPPORTED;\r |
1868 | }\r | |
d1102dba | 1869 | \r |
e42e9404 | 1870 | //\r |
1871 | // Install SMM Base2 Protocol and SMM Communication Protocol\r | |
1872 | //\r | |
1873 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
1874 | &mSmmIplHandle,\r | |
1436aea4 MK |
1875 | &gEfiSmmBase2ProtocolGuid,\r |
1876 | &mSmmBase2,\r | |
1877 | &gEfiSmmCommunicationProtocolGuid,\r | |
1878 | &mSmmCommunication,\r | |
1879 | &gEfiMmCommunication2ProtocolGuid,\r | |
1880 | &mMmCommunication2,\r | |
e42e9404 | 1881 | NULL\r |
1882 | );\r | |
1883 | ASSERT_EFI_ERROR (Status);\r | |
1884 | \r | |
1885 | //\r | |
4be497df | 1886 | // Create the set of protocol and event notifications that the SMM IPL requires\r |
e42e9404 | 1887 | //\r |
1888 | for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r | |
1889 | if (mSmmIplEvents[Index].Protocol) {\r | |
1890 | mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (\r | |
1891 | mSmmIplEvents[Index].Guid,\r | |
5657b268 | 1892 | mSmmIplEvents[Index].NotifyTpl,\r |
e42e9404 | 1893 | mSmmIplEvents[Index].NotifyFunction,\r |
1894 | mSmmIplEvents[Index].NotifyContext,\r | |
1436aea4 MK |
1895 | &Registration\r |
1896 | );\r | |
e42e9404 | 1897 | } else {\r |
1898 | Status = gBS->CreateEventEx (\r | |
1899 | EVT_NOTIFY_SIGNAL,\r | |
5657b268 | 1900 | mSmmIplEvents[Index].NotifyTpl,\r |
e42e9404 | 1901 | mSmmIplEvents[Index].NotifyFunction,\r |
1902 | mSmmIplEvents[Index].NotifyContext,\r | |
1903 | mSmmIplEvents[Index].Guid,\r | |
1904 | &mSmmIplEvents[Index].Event\r | |
1905 | );\r | |
1906 | ASSERT_EFI_ERROR (Status);\r | |
1907 | }\r | |
1908 | }\r | |
1909 | \r | |
1910 | return EFI_SUCCESS;\r | |
1911 | }\r |