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