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