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