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