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