]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
MdeModulePkg PiSmmIpl: Handle CommSize OPTIONAL case
[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
d1632f69
SZ
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
d1632f69 446 This parameter is optional and may be NULL.\r
e42e9404 447\r
448 @retval EFI_SUCCESS The message was successfully posted.\r
449 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r
d1632f69
SZ
450 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.\r
451 If this error is returned, the MessageLength field\r
452 in the CommBuffer header or the integer pointed by\r
453 CommSize, are updated to reflect the maximum payload\r
454 size the implementation can accommodate.\r
455 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,\r
456 if not omitted, are in address range that cannot be\r
457 accessed by the MM environment.\r
458\r
e42e9404 459**/\r
460EFI_STATUS\r
461EFIAPI\r
462SmmCommunicationCommunicate (\r
463 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r
464 IN OUT VOID *CommBuffer,\r
d1632f69 465 IN OUT UINTN *CommSize OPTIONAL\r
e42e9404 466 )\r
467{\r
468 EFI_STATUS Status;\r
469 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r
470 BOOLEAN OldInSmm;\r
d1632f69 471 UINTN TempCommSize;\r
e42e9404 472\r
473 //\r
474 // Check parameters\r
475 //\r
d1632f69 476 if (CommBuffer == NULL) {\r
e42e9404 477 return EFI_INVALID_PARAMETER;\r
478 }\r
479\r
d1632f69
SZ
480 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;\r
481\r
482 if (CommSize == NULL) {\r
483 TempCommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + CommunicateHeader->MessageLength;\r
484 } else {\r
485 TempCommSize = *CommSize;\r
486 //\r
487 // CommSize must hold HeaderGuid and MessageLength\r
488 //\r
489 if (TempCommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {\r
490 return EFI_INVALID_PARAMETER;\r
491 }\r
ab780ebf
JY
492 }\r
493\r
e42e9404 494 //\r
495 // If not already in SMM, then generate a Software SMI\r
496 //\r
497 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {\r
498 //\r
499 // Put arguments for Software SMI in gSmmCorePrivate\r
500 //\r
501 gSmmCorePrivate->CommunicationBuffer = CommBuffer;\r
d1632f69 502 gSmmCorePrivate->BufferSize = TempCommSize;\r
e42e9404 503\r
504 //\r
505 // Generate Software SMI\r
506 //\r
507 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);\r
508 if (EFI_ERROR (Status)) {\r
509 return EFI_UNSUPPORTED;\r
510 }\r
511\r
512 //\r
513 // Return status from software SMI \r
514 //\r
d1632f69
SZ
515 if (CommSize != NULL) {\r
516 *CommSize = gSmmCorePrivate->BufferSize;\r
517 }\r
e42e9404 518 return gSmmCorePrivate->ReturnStatus;\r
519 }\r
520\r
521 //\r
522 // If we are in SMM, then the execution mode must be physical, which means that\r
523 // OS established virtual addresses can not be used. If SetVirtualAddressMap()\r
d1632f69
SZ
524 // has been called, then a direct invocation of the Software SMI is not allowed,\r
525 // so return EFI_INVALID_PARAMETER.\r
e42e9404 526 //\r
527 if (EfiGoneVirtual()) {\r
528 return EFI_INVALID_PARAMETER;\r
529 }\r
530\r
3c5963cf 531 //\r
96756716 532 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.\r
3c5963cf 533 //\r
96756716 534 if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) {\r
3c5963cf 535 return EFI_INVALID_PARAMETER;\r
536 }\r
537 \r
e42e9404 538 //\r
539 // Save current InSmm state and set InSmm state to TRUE\r
540 //\r
541 OldInSmm = gSmmCorePrivate->InSmm;\r
542 gSmmCorePrivate->InSmm = TRUE;\r
543\r
544 //\r
4b5d69d3 545 // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.\r
e42e9404 546 //\r
d1632f69 547 TempCommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
e42e9404 548 Status = gSmmCorePrivate->Smst->SmiManage (\r
549 &CommunicateHeader->HeaderGuid, \r
550 NULL, \r
551 CommunicateHeader->Data, \r
d1632f69 552 &TempCommSize\r
e42e9404 553 );\r
d1632f69
SZ
554 TempCommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
555 if (CommSize != NULL) {\r
556 *CommSize = TempCommSize;\r
557 }\r
e42e9404 558\r
559 //\r
560 // Restore original InSmm state\r
561 //\r
562 gSmmCorePrivate->InSmm = OldInSmm;\r
563\r
d5b339a9 564 return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
e42e9404 565}\r
566\r
567/**\r
5657b268 568 Event notification that is fired when GUIDed Event Group is signaled.\r
e42e9404 569\r
570 @param Event The Event that is being processed, not used.\r
571 @param Context Event Context, not used.\r
572\r
573**/\r
574VOID\r
575EFIAPI\r
576SmmIplGuidedEventNotify (\r
577 IN EFI_EVENT Event,\r
578 IN VOID *Context\r
579 )\r
580{\r
e42e9404 581 UINTN Size;\r
582\r
583 //\r
584 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure \r
585 //\r
670a563f
JY
586 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
587 mCommunicateHeader.MessageLength = 1;\r
588 mCommunicateHeader.Data[0] = 0;\r
e42e9404 589\r
590 //\r
591 // Generate the Software SMI and return the result\r
592 //\r
670a563f
JY
593 Size = sizeof (mCommunicateHeader);\r
594 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r
e42e9404 595}\r
596\r
265fff39
JF
597/**\r
598 Event notification that is fired when EndOfDxe 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
606SmmIplEndOfDxeEventNotify (\r
607 IN EFI_EVENT Event,\r
608 IN VOID *Context\r
609 )\r
610{\r
611 mEndOfDxe = TRUE;\r
612}\r
613\r
5657b268 614/**\r
615 Event notification that is fired when DxeDispatch Event Group is signaled.\r
616\r
617 @param Event The Event that is being processed, not used.\r
618 @param Context Event Context, not used.\r
619\r
620**/\r
621VOID\r
622EFIAPI\r
623SmmIplDxeDispatchEventNotify (\r
624 IN EFI_EVENT Event,\r
625 IN VOID *Context\r
626 )\r
627{\r
5657b268 628 UINTN Size;\r
629 EFI_STATUS Status;\r
630\r
631 //\r
632 // Keep calling the SMM Core Dispatcher until there is no request to restart it.\r
633 //\r
634 while (TRUE) {\r
635 //\r
636 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure\r
637 // Clear the buffer passed into the Software SMI. This buffer will return\r
638 // the status of the SMM Core Dispatcher.\r
639 //\r
670a563f
JY
640 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
641 mCommunicateHeader.MessageLength = 1;\r
642 mCommunicateHeader.Data[0] = 0;\r
5657b268 643\r
644 //\r
645 // Generate the Software SMI and return the result\r
646 //\r
670a563f
JY
647 Size = sizeof (mCommunicateHeader);\r
648 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r
5657b268 649 \r
650 //\r
651 // Return if there is no request to restart the SMM Core Dispatcher\r
652 //\r
670a563f 653 if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {\r
5657b268 654 return;\r
655 }\r
656 \r
657 //\r
658 // Attempt to reset SMRAM cacheability to UC\r
659 // Assume CPU AP is available at this time\r
660 //\r
661 Status = gDS->SetMemorySpaceAttributes(\r
662 mSmramCacheBase, \r
663 mSmramCacheSize,\r
664 EFI_MEMORY_UC\r
665 );\r
666 if (EFI_ERROR (Status)) {\r
667 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
668 } \r
669\r
670 //\r
671 // Close all SMRAM ranges to protect SMRAM\r
672 //\r
673 Status = mSmmAccess->Close (mSmmAccess);\r
674 ASSERT_EFI_ERROR (Status);\r
675\r
676 //\r
677 // Print debug message that the SMRAM window is now closed.\r
678 //\r
679 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
5657b268 680 }\r
681}\r
682\r
e42e9404 683/**\r
684 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r
685\r
686 @param Event The Event that is being processed, not used.\r
687 @param Context Event Context, not used.\r
688\r
689**/\r
690VOID\r
691EFIAPI\r
692SmmIplSmmConfigurationEventNotify (\r
693 IN EFI_EVENT Event,\r
694 IN VOID *Context\r
695 )\r
696{\r
697 EFI_STATUS Status;\r
698 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
699\r
700 //\r
701 // Make sure this notification is for this handler\r
702 //\r
703 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);\r
704 if (EFI_ERROR (Status)) {\r
705 return;\r
706 }\r
707\r
708 //\r
709 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol\r
710 //\r
711 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);\r
712 ASSERT_EFI_ERROR (Status);\r
713\r
714 //\r
5657b268 715 // Set flag to indicate that the SMM Entry Point has been registered which \r
e42e9404 716 // means that SMIs are now fully operational.\r
717 //\r
718 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;\r
719\r
720 //\r
721 // Print debug message showing SMM Core entry point address.\r
722 //\r
723 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));\r
e42e9404 724}\r
725\r
726/**\r
727 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
01331951 728 or if gEfiEventReadyToBootGuid is signaled.\r
e42e9404 729\r
730 @param Event The Event that is being processed, not used.\r
731 @param Context Event Context, not used.\r
732\r
733**/\r
734VOID\r
735EFIAPI\r
736SmmIplReadyToLockEventNotify (\r
737 IN EFI_EVENT Event,\r
738 IN VOID *Context\r
739 )\r
740{\r
741 EFI_STATUS Status;\r
742 VOID *Interface;\r
743 UINTN Index;\r
744\r
745 //\r
746 // See if we are already locked\r
747 //\r
748 if (mSmmLocked) {\r
749 return;\r
750 }\r
751 \r
752 //\r
753 // Make sure this notification is for this handler\r
754 //\r
755 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {\r
756 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);\r
757 if (EFI_ERROR (Status)) {\r
758 return;\r
759 }\r
760 } else {\r
761 //\r
762 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being \r
01331951 763 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.\r
e42e9404 764 // Print a warning on debug builds.\r
765 //\r
766 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));\r
767 }\r
768\r
265fff39
JF
769 if (!mEndOfDxe) {\r
770 DEBUG ((DEBUG_ERROR, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));\r
771 REPORT_STATUS_CODE (\r
772 EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,\r
773 (EFI_SOFTWARE_SMM_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)\r
774 );\r
775 ASSERT (FALSE);\r
776 }\r
777\r
e42e9404 778 //\r
779 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r
780 //\r
781 mSmmAccess->Lock (mSmmAccess);\r
f02dfb5a 782 \r
e42e9404 783 //\r
784 // Close protocol and event notification events that do not apply after the \r
785 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot \r
786 // event has been signalled.\r
787 //\r
788 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
789 if (mSmmIplEvents[Index].CloseOnLock) {\r
790 gBS->CloseEvent (mSmmIplEvents[Index].Event);\r
791 }\r
792 }\r
793\r
794 //\r
795 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed\r
796 //\r
797 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);\r
798\r
799 //\r
800 // Print debug message that the SMRAM window is now locked.\r
801 //\r
802 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));\r
803 \r
804 //\r
805 // Set flag so this operation will not be performed again\r
806 //\r
807 mSmmLocked = TRUE;\r
808}\r
809\r
810/**\r
811 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
812\r
813 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
814 It convers pointer to new virtual address.\r
815\r
816 @param Event Event whose notification function is being invoked.\r
817 @param Context Pointer to the notification function's context.\r
818\r
819**/\r
820VOID\r
821EFIAPI\r
822SmmIplSetVirtualAddressNotify (\r
823 IN EFI_EVENT Event,\r
824 IN VOID *Context\r
825 )\r
826{\r
827 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);\r
828}\r
829\r
3c447c27 830/**\r
2048c585 831 Get the fixed loading address from image header assigned by build tool. This function only be called\r
3c447c27 832 when Loading module at Fixed address feature enabled.\r
e42e9404 833\r
3c447c27 834 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
835 image that needs to be examined by this function.\r
836 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r
2048c585 837 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.\r
3c447c27 838**/\r
839EFI_STATUS\r
840GetPeCoffImageFixLoadingAssignedAddress(\r
841 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
842 )\r
843{\r
844 UINTN SectionHeaderOffset;\r
845 EFI_STATUS Status;\r
846 EFI_IMAGE_SECTION_HEADER SectionHeader;\r
847 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
2048c585 848 EFI_PHYSICAL_ADDRESS FixLoadingAddress;\r
3c447c27 849 UINT16 Index;\r
850 UINTN Size;\r
851 UINT16 NumberOfSections;\r
852 EFI_PHYSICAL_ADDRESS SmramBase;\r
853 UINT64 SmmCodeSize;\r
854 UINT64 ValueInSectionHeader;\r
855 //\r
856 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
857 //\r
858 SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));\r
859 \r
2048c585 860 FixLoadingAddress = 0;\r
3c447c27 861 Status = EFI_NOT_FOUND;\r
c2aeb66f 862 SmramBase = mLMFAConfigurationTable->SmramBase;\r
3c447c27 863 //\r
864 // Get PeHeader pointer\r
865 //\r
866 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
16f69227
HW
867 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +\r
868 sizeof (UINT32) +\r
869 sizeof (EFI_IMAGE_FILE_HEADER) +\r
870 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r
3c447c27 871 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
872\r
873 //\r
874 // Get base address from the first section header that doesn't point to code section.\r
875 //\r
876 for (Index = 0; Index < NumberOfSections; Index++) {\r
877 //\r
878 // Read section header from file\r
879 //\r
880 Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
881 Status = ImageContext->ImageRead (\r
882 ImageContext->Handle,\r
883 SectionHeaderOffset,\r
884 &Size,\r
885 &SectionHeader\r
886 );\r
887 if (EFI_ERROR (Status)) {\r
888 return Status;\r
889 }\r
890 \r
891 Status = EFI_NOT_FOUND;\r
892 \r
893 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
894 //\r
895 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the\r
896 // first section header that doesn't point to code section in image header. And there is an assumption that when the\r
897 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers\r
2048c585 898 // fields should NOT be Zero, or else, these 2 fields should be set to Zero\r
3c447c27 899 //\r
900 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
901 if (ValueInSectionHeader != 0) {\r
902 //\r
2048c585 903 // Found first section header that doesn't point to code section in which build tool saves the\r
3c447c27 904 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
905 //\r
2048c585 906 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r
3c447c27 907\r
2048c585 908 if (SmramBase + SmmCodeSize > FixLoadingAddress && SmramBase <= FixLoadingAddress) {\r
3c447c27 909 //\r
2048c585 910 // The assigned address is valid. Return the specified loading address\r
3c447c27 911 //\r
2048c585 912 ImageContext->ImageAddress = FixLoadingAddress;\r
3c447c27 913 Status = EFI_SUCCESS;\r
914 }\r
915 }\r
916 break;\r
917 }\r
918 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
919 }\r
2048c585 920 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress, Status));\r
3c447c27 921 return Status;\r
922}\r
e42e9404 923/**\r
924 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r
925\r
c03beb76
SZ
926 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the \r
927 currently executing image, the rang of SMRAM to\r
928 hold SMM Core will be excluded.\r
929 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.\r
930\r
931 @param[in] Context Context to pass into SMM Core\r
e42e9404 932\r
933 @return EFI_STATUS\r
934\r
935**/\r
936EFI_STATUS\r
937ExecuteSmmCoreFromSmram (\r
c03beb76
SZ
938 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRange,\r
939 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRangeSmmCore,\r
940 IN VOID *Context\r
e42e9404 941 )\r
942{\r
943 EFI_STATUS Status;\r
944 VOID *SourceBuffer;\r
945 UINTN SourceSize;\r
946 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
947 UINTN PageCount;\r
e42e9404 948 EFI_IMAGE_ENTRY_POINT EntryPoint;\r
949\r
950 //\r
951 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE\r
952 // \r
d7aaf1dc
LG
953 Status = GetSectionFromAnyFvByFileType (\r
954 EFI_FV_FILETYPE_SMM_CORE, \r
955 0,\r
956 EFI_SECTION_PE32, \r
957 0,\r
958 &SourceBuffer, \r
959 &SourceSize\r
960 );\r
961 if (EFI_ERROR (Status)) {\r
962 return Status;\r
e42e9404 963 }\r
964 \r
965 //\r
966 // Initilize ImageContext\r
967 //\r
968 ImageContext.Handle = SourceBuffer;\r
969 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
970\r
971 //\r
972 // Get information about the image being loaded\r
973 //\r
974 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
975 if (EFI_ERROR (Status)) {\r
976 return Status;\r
977 }\r
e42e9404 978 //\r
3c447c27 979 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to \r
980 // the address assigned by build tool.\r
e42e9404 981 //\r
3c447c27 982 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
983 //\r
984 // Get the fixed loading address assigned by Build tool\r
985 //\r
986 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);\r
987 if (!EFI_ERROR (Status)) {\r
988 //\r
989 // 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
990 //\r
991 PageCount = 0;\r
a7d10bb0
LG
992 //\r
993 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.\r
994 //\r
995 gSmmCorePrivate->SmramRangeCount --;\r
c03beb76 996 } else {\r
3c447c27 997 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));\r
998 //\r
999 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
1000 // specified by SmramRange\r
1001 //\r
e0e7f80c 1002 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
e42e9404 1003\r
3c447c27 1004 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
1005 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
e42e9404 1006\r
3c447c27 1007 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
c03beb76
SZ
1008 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
1009 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;\r
1010 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;\r
1011 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);\r
e42e9404 1012\r
3c447c27 1013 //\r
c03beb76 1014 // Align buffer on section boundary\r
3c447c27 1015 //\r
c03beb76 1016 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;\r
3c447c27 1017 }\r
1018 } else {\r
1019 //\r
1020 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
1021 // specified by SmramRange\r
1022 //\r
e0e7f80c 1023 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
3c447c27 1024\r
1025 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
1026 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
1027\r
1028 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
c03beb76
SZ
1029 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
1030 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;\r
1031 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;\r
1032 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);\r
3c447c27 1033\r
1034 //\r
c03beb76 1035 // Align buffer on section boundary\r
3c447c27 1036 //\r
c03beb76 1037 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;\r
3c447c27 1038 }\r
1039 \r
e42e9404 1040 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
16f69227 1041 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r
e42e9404 1042\r
1043 //\r
1044 // Print debug message showing SMM Core load address.\r
1045 //\r
1046 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));\r
1047\r
1048 //\r
1049 // Load the image to our new buffer\r
1050 //\r
1051 Status = PeCoffLoaderLoadImage (&ImageContext);\r
1052 if (!EFI_ERROR (Status)) {\r
1053 //\r
1054 // Relocate the image in our new buffer\r
1055 //\r
1056 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
1057 if (!EFI_ERROR (Status)) {\r
1058 //\r
1059 // Flush the instruction cache so the image data are written before we execute it\r
1060 //\r
1061 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
1062\r
1063 //\r
1064 // Print debug message showing SMM Core entry point address.\r
1065 //\r
1066 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));\r
1067\r
84edd20b
SZ
1068 gSmmCorePrivate->PiSmmCoreImageBase = ImageContext.ImageAddress;\r
1069 gSmmCorePrivate->PiSmmCoreImageSize = ImageContext.ImageSize;\r
1070 DEBUG ((DEBUG_INFO, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageBase));\r
1071 DEBUG ((DEBUG_INFO, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageSize));\r
1072\r
1073 gSmmCorePrivate->PiSmmCoreEntryPoint = ImageContext.EntryPoint;\r
1074\r
e42e9404 1075 //\r
1076 // Execute image\r
1077 //\r
1078 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;\r
1079 Status = EntryPoint ((EFI_HANDLE)Context, gST);\r
1080 }\r
1081 }\r
1082\r
1083 //\r
c03beb76 1084 // Always free memory allocted by GetFileBufferByFilePath ()\r
e42e9404 1085 //\r
c03beb76
SZ
1086 FreePool (SourceBuffer);\r
1087\r
1088 return Status;\r
1089}\r
1090\r
1091/**\r
1092 SMM split SMRAM entry.\r
1093\r
1094 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.\r
1095 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.\r
1096 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.\r
1097 @param[in, out] RangeCount Pointer to range count.\r
1098 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.\r
1099 @param[in, out] ReservedRangeCount Pointer to reserved range count.\r
1100 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry\r
1101 that no need to be split anymore.\r
1102 @param[in, out] FinalRangeCount Pointer to final range count.\r
1103\r
1104**/\r
1105VOID\r
1106SmmSplitSmramEntry (\r
1107 IN OUT EFI_SMRAM_DESCRIPTOR *RangeToCompare,\r
1108 IN OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare,\r
1109 OUT EFI_SMRAM_DESCRIPTOR *Ranges,\r
1110 IN OUT UINTN *RangeCount,\r
1111 OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRanges,\r
1112 IN OUT UINTN *ReservedRangeCount,\r
1113 OUT EFI_SMRAM_DESCRIPTOR *FinalRanges,\r
1114 IN OUT UINTN *FinalRangeCount\r
1115 )\r
1116{\r
1117 UINT64 RangeToCompareEnd;\r
1118 UINT64 ReservedRangeToCompareEnd;\r
1119\r
1120 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;\r
1121 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;\r
1122\r
1123 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&\r
1124 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {\r
1125 if (RangeToCompareEnd < ReservedRangeToCompareEnd) {\r
1126 //\r
1127 // RangeToCompare ReservedRangeToCompare\r
1128 // ---- ---- --------------------------------------\r
1129 // | | | | -> 1. ReservedRangeToCompare\r
1130 // ---- | | |--| --------------------------------------\r
1131 // | | | | | |\r
1132 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1133 // | | | | | | RangeToCompare->PhysicalSize = 0\r
1134 // ---- | | |--| --------------------------------------\r
1135 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount\r
1136 // ---- ---- --------------------------------------\r
1137 //\r
1138\r
1139 //\r
1140 // 1. Update ReservedRangeToCompare.\r
1141 //\r
1142 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;\r
1143 //\r
1144 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1145 // Zero RangeToCompare->PhysicalSize.\r
1146 //\r
1147 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;\r
1148 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;\r
1149 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1150 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompare->PhysicalSize;\r
1151 *FinalRangeCount += 1;\r
1152 RangeToCompare->PhysicalSize = 0;\r
1153 //\r
1154 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.\r
1155 //\r
1156 ReservedRanges[*ReservedRangeCount].SmramReservedStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1157 ReservedRanges[*ReservedRangeCount].SmramReservedSize = ReservedRangeToCompareEnd - RangeToCompareEnd;\r
1158 *ReservedRangeCount += 1;\r
1159 } else {\r
1160 //\r
1161 // RangeToCompare ReservedRangeToCompare\r
1162 // ---- ---- --------------------------------------\r
1163 // | | | | -> 1. ReservedRangeToCompare\r
1164 // ---- | | |--| --------------------------------------\r
1165 // | | | | | |\r
1166 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1167 // | | | | | |\r
1168 // | | ---- |--| --------------------------------------\r
1169 // | | | | -> 3. RangeToCompare\r
1170 // ---- ---- --------------------------------------\r
1171 //\r
1172\r
1173 //\r
1174 // 1. Update ReservedRangeToCompare.\r
1175 //\r
1176 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;\r
1177 //\r
1178 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1179 //\r
1180 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;\r
1181 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;\r
1182 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1183 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompareEnd - RangeToCompare->CpuStart;\r
1184 *FinalRangeCount += 1;\r
1185 //\r
1186 // 3. Update RangeToCompare.\r
1187 //\r
1188 RangeToCompare->CpuStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1189 RangeToCompare->PhysicalStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1190 RangeToCompare->PhysicalSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1191 }\r
1192 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&\r
1193 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {\r
1194 if (ReservedRangeToCompareEnd < RangeToCompareEnd) {\r
1195 //\r
1196 // RangeToCompare ReservedRangeToCompare\r
1197 // ---- ---- --------------------------------------\r
1198 // | | | | -> 1. RangeToCompare\r
1199 // | | ---- |--| --------------------------------------\r
1200 // | | | | | |\r
1201 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1202 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0\r
1203 // | | ---- |--| --------------------------------------\r
1204 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount\r
1205 // ---- ---- --------------------------------------\r
1206 //\r
1207\r
1208 //\r
1209 // 1. Update RangeToCompare.\r
1210 //\r
1211 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;\r
1212 //\r
1213 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1214 // ReservedRangeToCompare->SmramReservedSize = 0\r
1215 //\r
1216 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;\r
1217 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;\r
1218 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1219 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompare->SmramReservedSize;\r
1220 *FinalRangeCount += 1;\r
1221 ReservedRangeToCompare->SmramReservedSize = 0;\r
1222 //\r
1223 // 3. Update Ranges[*RangeCount] and increment *RangeCount.\r
1224 //\r
1225 Ranges[*RangeCount].CpuStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1226 Ranges[*RangeCount].PhysicalStart = FinalRanges[*FinalRangeCount - 1].PhysicalStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1227 Ranges[*RangeCount].RegionState = RangeToCompare->RegionState;\r
1228 Ranges[*RangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompareEnd;\r
1229 *RangeCount += 1;\r
1230 } else {\r
1231 //\r
1232 // RangeToCompare ReservedRangeToCompare\r
1233 // ---- ---- --------------------------------------\r
1234 // | | | | -> 1. RangeToCompare\r
1235 // | | ---- |--| --------------------------------------\r
1236 // | | | | | |\r
1237 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1238 // | | | | | |\r
1239 // ---- | | |--| --------------------------------------\r
1240 // | | | | -> 3. ReservedRangeToCompare\r
1241 // ---- ---- --------------------------------------\r
1242 //\r
1243\r
1244 //\r
1245 // 1. Update RangeToCompare.\r
1246 //\r
1247 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;\r
1248 //\r
1249 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1250 // ReservedRangeToCompare->SmramReservedSize = 0\r
1251 //\r
1252 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;\r
1253 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;\r
1254 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1255 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompare->SmramReservedStart;\r
1256 *FinalRangeCount += 1;\r
1257 //\r
1258 // 3. Update ReservedRangeToCompare.\r
1259 //\r
1260 ReservedRangeToCompare->SmramReservedStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1261 ReservedRangeToCompare->SmramReservedSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1262 }\r
1263 }\r
1264}\r
1265\r
1266/**\r
1267 Returns if SMRAM range and SMRAM reserved range are overlapped.\r
1268\r
1269 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.\r
1270 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.\r
1271\r
1272 @retval TRUE There is overlap.\r
1273 @retval FALSE There is no overlap.\r
1274\r
1275**/\r
1276BOOLEAN\r
1277SmmIsSmramOverlap (\r
1278 IN EFI_SMRAM_DESCRIPTOR *RangeToCompare,\r
1279 IN EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare\r
1280 )\r
1281{\r
1282 UINT64 RangeToCompareEnd;\r
1283 UINT64 ReservedRangeToCompareEnd;\r
1284\r
1285 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;\r
1286 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;\r
1287\r
1288 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&\r
1289 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {\r
1290 return TRUE;\r
1291 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&\r
1292 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {\r
1293 return TRUE;\r
e42e9404 1294 }\r
c03beb76
SZ
1295 return FALSE;\r
1296}\r
1297\r
1298/**\r
1299 Get full SMRAM ranges.\r
e42e9404 1300\r
c03beb76
SZ
1301 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from\r
1302 SmmConfiguration protocol, split the entries if there is overlap between them.\r
1303 It will also reserve one entry for SMM core.\r
1304\r
1305 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.\r
1306\r
1307 @return Pointer to full SMRAM ranges.\r
1308\r
1309**/\r
1310EFI_SMRAM_DESCRIPTOR *\r
1311GetFullSmramRanges (\r
1312 OUT UINTN *FullSmramRangeCount\r
1313 )\r
1314{\r
1315 EFI_STATUS Status;\r
1316 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
1317 UINTN Size;\r
1318 UINTN Index;\r
1319 UINTN Index2;\r
1320 EFI_SMRAM_DESCRIPTOR *FullSmramRanges;\r
1321 UINTN TempSmramRangeCount;\r
a7d10bb0 1322 UINTN AdditionSmramRangeCount;\r
c03beb76
SZ
1323 EFI_SMRAM_DESCRIPTOR *TempSmramRanges;\r
1324 UINTN SmramRangeCount;\r
1325 EFI_SMRAM_DESCRIPTOR *SmramRanges;\r
1326 UINTN SmramReservedCount;\r
1327 EFI_SMM_RESERVED_SMRAM_REGION *SmramReservedRanges;\r
1328 UINTN MaxCount;\r
1329 BOOLEAN Rescan;\r
1330\r
1331 //\r
1332 // Get SMM Configuration Protocol if it is present.\r
e42e9404 1333 //\r
c03beb76
SZ
1334 SmmConfiguration = NULL;\r
1335 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);\r
1336\r
e42e9404 1337 //\r
c03beb76
SZ
1338 // Get SMRAM information.\r
1339 //\r
1340 Size = 0;\r
1341 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);\r
1342 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
e42e9404 1343\r
c03beb76
SZ
1344 SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
1345\r
1346 //\r
1347 // Get SMRAM reserved region count.\r
1348 //\r
1349 SmramReservedCount = 0;\r
1350 if (SmmConfiguration != NULL) {\r
1351 while (SmmConfiguration->SmramReservedRegions[SmramReservedCount].SmramReservedSize != 0) {\r
1352 SmramReservedCount++;\r
1353 }\r
1354 }\r
1355\r
a7d10bb0
LG
1356 //\r
1357 // Reserve one entry for SMM Core in the full SMRAM ranges.\r
1358 //\r
1359 AdditionSmramRangeCount = 1;\r
1360 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
1361 //\r
1362 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.\r
1363 //\r
1364 AdditionSmramRangeCount = 2;\r
1365 }\r
1366\r
c03beb76
SZ
1367 if (SmramReservedCount == 0) {\r
1368 //\r
1369 // No reserved SMRAM entry from SMM Configuration Protocol.\r
c03beb76 1370 //\r
a7d10bb0 1371 *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;\r
c03beb76 1372 Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);\r
08308fc9 1373 FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocateZeroPool (Size);\r
c03beb76
SZ
1374 ASSERT (FullSmramRanges != NULL);\r
1375\r
1376 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, FullSmramRanges);\r
1377 ASSERT_EFI_ERROR (Status);\r
1378\r
1379 return FullSmramRanges;\r
1380 }\r
1381\r
1382 //\r
1383 // Why MaxCount = X + 2 * Y?\r
1384 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.\r
1385 //\r
1386 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1\r
1387 // ---- ----\r
1388 // | | ---- |--|\r
1389 // | | | | -> | |\r
1390 // | | ---- |--|\r
1391 // ---- ----\r
1392 //\r
1393 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1\r
1394 // ---- ----\r
1395 // | | | |\r
1396 // | | ---- |--|\r
1397 // | | | | | |\r
1398 // |--| | | -> |--|\r
1399 // | | | | | |\r
1400 // | | ---- |--|\r
1401 // | | | |\r
1402 // ---- ----\r
1403 //\r
1404 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1\r
1405 // ---- ----\r
1406 // | | | |\r
1407 // | | ---- |--|\r
1408 // |--| | | |--|\r
1409 // | | | | -> | |\r
1410 // |--| | | |--|\r
1411 // | | ---- |--|\r
1412 // | | | |\r
1413 // ---- ----\r
1414 //\r
1415 // ......\r
1416 //\r
1417 MaxCount = SmramRangeCount + 2 * SmramReservedCount;\r
1418\r
1419 Size = MaxCount * sizeof (EFI_SMM_RESERVED_SMRAM_REGION);\r
1420 SmramReservedRanges = (EFI_SMM_RESERVED_SMRAM_REGION *) AllocatePool (Size);\r
1421 ASSERT (SmramReservedRanges != NULL);\r
1422 for (Index = 0; Index < SmramReservedCount; Index++) {\r
1423 CopyMem (&SmramReservedRanges[Index], &SmmConfiguration->SmramReservedRegions[Index], sizeof (EFI_SMM_RESERVED_SMRAM_REGION));\r
1424 }\r
1425\r
1426 Size = MaxCount * sizeof (EFI_SMRAM_DESCRIPTOR);\r
1427 TempSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
1428 ASSERT (TempSmramRanges != NULL);\r
1429 TempSmramRangeCount = 0;\r
1430\r
1431 SmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
1432 ASSERT (SmramRanges != NULL);\r
1433 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, SmramRanges);\r
1434 ASSERT_EFI_ERROR (Status);\r
1435\r
1436 do {\r
1437 Rescan = FALSE;\r
1438 for (Index = 0; (Index < SmramRangeCount) && !Rescan; Index++) {\r
1439 //\r
1440 // Skip zero size entry.\r
1441 //\r
1442 if (SmramRanges[Index].PhysicalSize != 0) {\r
1443 for (Index2 = 0; (Index2 < SmramReservedCount) && !Rescan; Index2++) {\r
1444 //\r
1445 // Skip zero size entry.\r
1446 //\r
1447 if (SmramReservedRanges[Index2].SmramReservedSize != 0) {\r
1448 if (SmmIsSmramOverlap (\r
1449 &SmramRanges[Index],\r
1450 &SmramReservedRanges[Index2]\r
1451 )) {\r
1452 //\r
1453 // There is overlap, need to split entry and then rescan.\r
1454 //\r
1455 SmmSplitSmramEntry (\r
1456 &SmramRanges[Index],\r
1457 &SmramReservedRanges[Index2],\r
1458 SmramRanges,\r
1459 &SmramRangeCount,\r
1460 SmramReservedRanges,\r
1461 &SmramReservedCount,\r
1462 TempSmramRanges,\r
1463 &TempSmramRangeCount\r
1464 );\r
1465 Rescan = TRUE;\r
1466 }\r
1467 }\r
1468 }\r
1469 if (!Rescan) {\r
1470 //\r
1471 // No any overlap, copy the entry to the temp SMRAM ranges.\r
1472 // Zero SmramRanges[Index].PhysicalSize = 0;\r
1473 //\r
1474 CopyMem (&TempSmramRanges[TempSmramRangeCount++], &SmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));\r
1475 SmramRanges[Index].PhysicalSize = 0;\r
1476 }\r
1477 }\r
1478 }\r
1479 } while (Rescan);\r
1480 ASSERT (TempSmramRangeCount <= MaxCount);\r
1481\r
1482 //\r
a7d10bb0 1483 // Sort the entries\r
c03beb76 1484 //\r
a7d10bb0 1485 FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));\r
c03beb76
SZ
1486 ASSERT (FullSmramRanges != NULL);\r
1487 *FullSmramRangeCount = 0;\r
1488 do {\r
1489 for (Index = 0; Index < TempSmramRangeCount; Index++) {\r
1490 if (TempSmramRanges[Index].PhysicalSize != 0) {\r
1491 break;\r
1492 }\r
1493 }\r
1494 ASSERT (Index < TempSmramRangeCount);\r
1495 for (Index2 = 0; Index2 < TempSmramRangeCount; Index2++) {\r
1496 if ((Index2 != Index) && (TempSmramRanges[Index2].PhysicalSize != 0) && (TempSmramRanges[Index2].CpuStart < TempSmramRanges[Index].CpuStart)) {\r
1497 Index = Index2;\r
1498 }\r
1499 }\r
1500 CopyMem (&FullSmramRanges[*FullSmramRangeCount], &TempSmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));\r
1501 *FullSmramRangeCount += 1;\r
1502 TempSmramRanges[Index].PhysicalSize = 0;\r
1503 } while (*FullSmramRangeCount < TempSmramRangeCount);\r
1504 ASSERT (*FullSmramRangeCount == TempSmramRangeCount);\r
a7d10bb0 1505 *FullSmramRangeCount += AdditionSmramRangeCount;\r
c03beb76
SZ
1506\r
1507 FreePool (SmramRanges);\r
1508 FreePool (SmramReservedRanges);\r
1509 FreePool (TempSmramRanges);\r
1510\r
1511 return FullSmramRanges;\r
e42e9404 1512}\r
1513\r
1514/**\r
1515 The Entry Point for SMM IPL\r
1516\r
1517 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install \r
1518 SMM Base 2 Protocol and SMM Communication Protocol, and register for the \r
1519 critical events required to coordinate between DXE and SMM environments.\r
1520 \r
1521 @param ImageHandle The firmware allocated handle for the EFI image.\r
1522 @param SystemTable A pointer to the EFI System Table.\r
1523\r
1524 @retval EFI_SUCCESS The entry point is executed successfully.\r
1525 @retval Other Some error occurred when executing this entry point.\r
1526\r
1527**/\r
1528EFI_STATUS\r
1529EFIAPI\r
1530SmmIplEntry (\r
1531 IN EFI_HANDLE ImageHandle,\r
1532 IN EFI_SYSTEM_TABLE *SystemTable\r
1533 )\r
1534{\r
1535 EFI_STATUS Status;\r
e42e9404 1536 UINTN Index;\r
e42e9404 1537 UINT64 MaxSize;\r
1538 VOID *Registration;\r
07d9dc83 1539 UINT64 SmmCodeSize;\r
07d9dc83 1540 EFI_CPU_ARCH_PROTOCOL *CpuArch;\r
b07ea4c1 1541 EFI_STATUS SetAttrStatus;\r
a7d10bb0 1542 EFI_SMRAM_DESCRIPTOR *SmramRangeSmmDriver;\r
e42e9404 1543\r
1544 //\r
1545 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the \r
1546 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded \r
1547 // by the SMM Core\r
1548 //\r
1549 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r
1550\r
1551 //\r
1552 // Get SMM Access Protocol\r
1553 //\r
1554 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);\r
1555 ASSERT_EFI_ERROR (Status);\r
1556\r
1557 //\r
1558 // Get SMM Control2 Protocol\r
1559 //\r
1560 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);\r
1561 ASSERT_EFI_ERROR (Status);\r
1562\r
c03beb76 1563 gSmmCorePrivate->SmramRanges = GetFullSmramRanges (&gSmmCorePrivate->SmramRangeCount);\r
84edd20b 1564\r
e42e9404 1565 //\r
1566 // Open all SMRAM ranges\r
1567 //\r
1568 Status = mSmmAccess->Open (mSmmAccess);\r
1569 ASSERT_EFI_ERROR (Status);\r
1570\r
1571 //\r
1572 // Print debug message that the SMRAM window is now open.\r
1573 //\r
1574 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r
e42e9404 1575 \r
1576 //\r
06b07ce3 1577 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r
e42e9404 1578 //\r
1579 mCurrentSmramRange = NULL;\r
06b07ce3 1580 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
2c0f06f0 1581 //\r
1582 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization\r
1583 //\r
1584 if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {\r
1585 continue;\r
1586 }\r
1587\r
e42e9404 1588 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r
10e4e4f6 1589 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {\r
e42e9404 1590 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r
1591 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
1592 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r
1593 }\r
1594 }\r
1595 }\r
1596 }\r
1597\r
1598 if (mCurrentSmramRange != NULL) {\r
1599 //\r
1600 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r
1601 //\r
1602 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n", \r
1603 (VOID *)(UINTN)mCurrentSmramRange->CpuStart, \r
1604 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r
1605 ));\r
1606\r
40e8cca5 1607 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);\r
e42e9404 1608 //\r
07d9dc83 1609 // If CPU AP is present, attempt to set SMRAM cacheability to WB\r
1610 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP\r
1611 // is not available here.\r
e42e9404 1612 //\r
0a6c0905 1613 CpuArch = NULL;\r
07d9dc83 1614 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);\r
1615 if (!EFI_ERROR (Status)) {\r
1616 Status = gDS->SetMemorySpaceAttributes(\r
1617 mSmramCacheBase, \r
1618 mSmramCacheSize,\r
1619 EFI_MEMORY_WB\r
1620 );\r
1621 if (EFI_ERROR (Status)) {\r
1622 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r
1623 } \r
1624 }\r
3c447c27 1625 //\r
1626 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load\r
1627 // Modules At Fixed Address Configuration Table.\r
1628 //\r
1629 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
1630 //\r
1631 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
1632 //\r
1633 SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);\r
1634 //\r
1635 // The SMRAM available memory is assumed to be larger than SmmCodeSize\r
1636 //\r
1637 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);\r
1638 //\r
1639 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.\r
1640 //\r
1641 Status = EfiGetSystemConfigurationTable (\r
1642 &gLoadFixedAddressConfigurationTableGuid,\r
c2aeb66f 1643 (VOID **) &mLMFAConfigurationTable\r
3c447c27 1644 );\r
c2aeb66f
LG
1645 if (!EFI_ERROR (Status) && mLMFAConfigurationTable != NULL) {\r
1646 mLMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r
2d5ac154 1647 //\r
1648 // Print the SMRAM base\r
1649 //\r
c2aeb66f 1650 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable->SmramBase));\r
3c447c27 1651 }\r
a7d10bb0
LG
1652\r
1653 //\r
1654 // Fill the Smram range for all SMM code\r
1655 //\r
1656 SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];\r
1657 SmramRangeSmmDriver->CpuStart = mCurrentSmramRange->CpuStart;\r
1658 SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;\r
1659 SmramRangeSmmDriver->RegionState = mCurrentSmramRange->RegionState | EFI_ALLOCATED;\r
1660 SmramRangeSmmDriver->PhysicalSize = SmmCodeSize;\r
1661\r
1662 mCurrentSmramRange->PhysicalSize -= SmmCodeSize;\r
1663 mCurrentSmramRange->CpuStart = mCurrentSmramRange->CpuStart + SmmCodeSize;\r
1664 mCurrentSmramRange->PhysicalStart = mCurrentSmramRange->PhysicalStart + SmmCodeSize;\r
3c447c27 1665 }\r
e42e9404 1666 //\r
1667 // Load SMM Core into SMRAM and execute it from SMRAM\r
1668 //\r
c03beb76
SZ
1669 Status = ExecuteSmmCoreFromSmram (\r
1670 mCurrentSmramRange,\r
1671 &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 1],\r
1672 gSmmCorePrivate\r
1673 );\r
e42e9404 1674 if (EFI_ERROR (Status)) {\r
1675 //\r
1676 // Print error message that the SMM Core failed to be loaded and executed.\r
1677 //\r
1678 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));\r
1679\r
1680 //\r
1681 // Attempt to reset SMRAM cacheability to UC\r
1682 //\r
0a6c0905 1683 if (CpuArch != NULL) {\r
b07ea4c1
LE
1684 SetAttrStatus = gDS->SetMemorySpaceAttributes(\r
1685 mSmramCacheBase, \r
1686 mSmramCacheSize,\r
1687 EFI_MEMORY_UC\r
1688 );\r
1689 if (EFI_ERROR (SetAttrStatus)) {\r
0a6c0905 1690 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
1691 } \r
1692 }\r
e42e9404 1693 }\r
1694 } else {\r
1695 //\r
1696 // Print error message that there are not enough SMRAM resources to load the SMM Core.\r
1697 //\r
1698 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));\r
1699 }\r
1700\r
1701 //\r
1702 // If the SMM Core could not be loaded then close SMRAM window, free allocated \r
1703 // resources, and return an error so SMM IPL will be unloaded.\r
1704 //\r
1705 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {\r
1706 //\r
1707 // Close all SMRAM ranges\r
1708 //\r
1709 Status = mSmmAccess->Close (mSmmAccess);\r
1710 ASSERT_EFI_ERROR (Status);\r
1711\r
1712 //\r
1713 // Print debug message that the SMRAM window is now closed.\r
1714 //\r
1715 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
1716\r
1717 //\r
1718 // Free all allocated resources\r
1719 //\r
1720 FreePool (gSmmCorePrivate->SmramRanges);\r
63aa86b0 1721\r
e42e9404 1722 return EFI_UNSUPPORTED;\r
1723 }\r
1724 \r
1725 //\r
1726 // Install SMM Base2 Protocol and SMM Communication Protocol\r
1727 //\r
1728 Status = gBS->InstallMultipleProtocolInterfaces (\r
1729 &mSmmIplHandle,\r
1730 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,\r
1731 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,\r
1732 NULL\r
1733 );\r
1734 ASSERT_EFI_ERROR (Status);\r
1735\r
1736 //\r
1737 // Create the set of protocol and event notififcations that the SMM IPL requires\r
1738 //\r
1739 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
1740 if (mSmmIplEvents[Index].Protocol) {\r
1741 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (\r
1742 mSmmIplEvents[Index].Guid,\r
5657b268 1743 mSmmIplEvents[Index].NotifyTpl,\r
e42e9404 1744 mSmmIplEvents[Index].NotifyFunction,\r
1745 mSmmIplEvents[Index].NotifyContext,\r
1746 &Registration\r
1747 );\r
1748 } else {\r
1749 Status = gBS->CreateEventEx (\r
1750 EVT_NOTIFY_SIGNAL,\r
5657b268 1751 mSmmIplEvents[Index].NotifyTpl,\r
e42e9404 1752 mSmmIplEvents[Index].NotifyFunction,\r
1753 mSmmIplEvents[Index].NotifyContext,\r
1754 mSmmIplEvents[Index].Guid,\r
1755 &mSmmIplEvents[Index].Event\r
1756 );\r
1757 ASSERT_EFI_ERROR (Status);\r
1758 }\r
1759 }\r
1760\r
1761 return EFI_SUCCESS;\r
1762}\r