]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
Minor comment update.
[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
cd5ebaa0 4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
e42e9404 5 This program and the accompanying materials are licensed and made available \r
6 under the terms and conditions of the BSD License which accompanies this \r
7 distribution. The full text of the license may be found at \r
8 http://opensource.org/licenses/bsd-license.php \r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16\r
17#include <Protocol/SmmBase2.h>\r
18#include <Protocol/SmmCommunication.h>\r
19#include <Protocol/SmmAccess2.h>\r
20#include <Protocol/SmmConfiguration.h>\r
21#include <Protocol/SmmControl2.h>\r
22#include <Protocol/DxeSmmReadyToLock.h>\r
e42e9404 23\r
24#include <Guid/EventGroup.h>\r
25#include <Guid/EventLegacyBios.h>\r
3c447c27 26#include <Guid/LoadModuleAtFixedAddress.h>\r
e42e9404 27\r
28#include <Library/BaseLib.h>\r
29#include <Library/BaseMemoryLib.h>\r
30#include <Library/PeCoffLib.h>\r
31#include <Library/CacheMaintenanceLib.h>\r
32#include <Library/MemoryAllocationLib.h>\r
33#include <Library/DebugLib.h>\r
34#include <Library/UefiBootServicesTableLib.h>\r
35#include <Library/DxeServicesTableLib.h>\r
d7aaf1dc 36#include <Library/DxeServicesLib.h>\r
e42e9404 37#include <Library/UefiLib.h>\r
38#include <Library/UefiRuntimeLib.h>\r
3c447c27 39#include <Library/PcdLib.h>\r
e42e9404 40\r
41#include "PiSmmCorePrivateData.h"\r
42\r
43//\r
44// Function prototypes from produced protocols\r
45//\r
46\r
47/**\r
48 Indicate whether the driver is currently executing in the SMM Initialization phase.\r
49\r
50 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
51 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r
52 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r
53\r
54 @retval EFI_INVALID_PARAMETER InSmram was NULL.\r
55 @retval EFI_SUCCESS The call returned successfully.\r
56\r
57**/\r
58EFI_STATUS\r
59EFIAPI\r
60SmmBase2InSmram (\r
61 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
62 OUT BOOLEAN *InSmram\r
63 );\r
64\r
65/**\r
66 Retrieves the location of the System Management System Table (SMST).\r
67\r
68 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
69 @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r
70\r
71 @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r
72 @retval EFI_SUCCESS The memory was returned to the system.\r
73 @retval EFI_UNSUPPORTED Not in SMM.\r
74\r
75**/\r
76EFI_STATUS\r
77EFIAPI\r
78SmmBase2GetSmstLocation (\r
79 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
80 OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r
81 );\r
82\r
83/**\r
84 Communicates with a registered handler.\r
85 \r
86 This function provides a service to send and receive messages from a registered \r
87 UEFI service. This function is part of the SMM Communication Protocol that may \r
88 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode \r
89 after SetVirtualAddressMap().\r
90\r
91 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
2292758d 92 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.\r
93 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data\r
e42e9404 94 being returned. Zero if the handler does not wish to reply with any data.\r
95\r
96 @retval EFI_SUCCESS The message was successfully posted.\r
97 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r
98**/\r
99EFI_STATUS\r
100EFIAPI\r
101SmmCommunicationCommunicate (\r
102 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r
103 IN OUT VOID *CommBuffer,\r
104 IN OUT UINTN *CommSize\r
105 );\r
106\r
107/**\r
108 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r
109\r
110 @param Event The Event that is being processed, not used.\r
111 @param Context Event Context, not used.\r
112\r
113**/\r
114VOID\r
115EFIAPI\r
116SmmIplSmmConfigurationEventNotify (\r
117 IN EFI_EVENT Event,\r
118 IN VOID *Context\r
119 );\r
120\r
121/**\r
122 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
123 or if gEfiEventReadyToBootGuid is signalled.\r
124\r
125 @param Event The Event that is being processed, not used.\r
126 @param Context Event Context, not used.\r
127\r
128**/\r
129VOID\r
130EFIAPI\r
131SmmIplReadyToLockEventNotify (\r
132 IN EFI_EVENT Event,\r
133 IN VOID *Context\r
134 );\r
135\r
136/**\r
137 Event notification that is fired when DxeDispatch Event Group is signaled.\r
138\r
139 @param Event The Event that is being processed, not used.\r
140 @param Context Event Context, not used.\r
141\r
142**/\r
143VOID\r
144EFIAPI\r
145SmmIplGuidedEventNotify (\r
146 IN EFI_EVENT Event,\r
147 IN VOID *Context\r
148 );\r
149\r
150/**\r
151 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
152\r
153 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
154 It convers pointer to new virtual address.\r
155\r
156 @param Event Event whose notification function is being invoked.\r
157 @param Context Pointer to the notification function's context.\r
158\r
159**/\r
160VOID\r
161EFIAPI\r
162SmmIplSetVirtualAddressNotify (\r
163 IN EFI_EVENT Event,\r
164 IN VOID *Context\r
165 );\r
166\r
167//\r
168// Data structure used to declare a table of protocol notifications and event \r
169// notifications required by the SMM IPL\r
170//\r
171typedef struct {\r
172 BOOLEAN Protocol;\r
173 BOOLEAN CloseOnLock;\r
174 EFI_GUID *Guid;\r
175 EFI_EVENT_NOTIFY NotifyFunction;\r
176 VOID *NotifyContext;\r
177 EFI_EVENT Event;\r
178} SMM_IPL_EVENT_NOTIFICATION;\r
179\r
180//\r
181// Handle to install the SMM Base2 Protocol and the SMM Communication Protocol\r
182//\r
183EFI_HANDLE mSmmIplHandle = NULL;\r
184\r
185//\r
186// SMM Base 2 Protocol instance\r
187//\r
188EFI_SMM_BASE2_PROTOCOL mSmmBase2 = {\r
189 SmmBase2InSmram,\r
190 SmmBase2GetSmstLocation\r
191};\r
192\r
193//\r
194// SMM Communication Protocol instance\r
195//\r
196EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication = {\r
197 SmmCommunicationCommunicate\r
198};\r
199\r
200//\r
201// SMM Core Private Data structure that contains the data shared between\r
202// the SMM IPL and the SMM Core.\r
203//\r
204SMM_CORE_PRIVATE_DATA mSmmCorePrivateData = {\r
205 SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature\r
206 NULL, // SmmIplImageHandle\r
207 0, // SmramRangeCount\r
208 NULL, // SmramRanges\r
209 NULL, // SmmEntryPoint\r
210 FALSE, // SmmEntryPointRegistered\r
211 FALSE, // InSmm\r
212 NULL, // Smst\r
e42e9404 213 NULL, // CommunicationBuffer\r
ab780ebf 214 0, // BufferSize\r
e42e9404 215 EFI_SUCCESS // ReturnStatus\r
216};\r
217\r
218//\r
219// Global pointer used to access mSmmCorePrivateData from outside and inside SMM\r
220//\r
221SMM_CORE_PRIVATE_DATA *gSmmCorePrivate = &mSmmCorePrivateData;\r
222\r
223//\r
224// SMM IPL global variables\r
225//\r
226EFI_SMM_CONTROL2_PROTOCOL *mSmmControl2;\r
227EFI_SMM_ACCESS2_PROTOCOL *mSmmAccess;\r
228EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange;\r
229BOOLEAN mSmmLocked = FALSE;\r
40e8cca5 230EFI_PHYSICAL_ADDRESS mSmramCacheBase;\r
231UINT64 mSmramCacheSize;\r
e42e9404 232\r
233//\r
234// Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires\r
235//\r
236SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents[] = {\r
237 //\r
238 // Declare protocol notification on the SMM Configuration protocol. When this notification is etablished, \r
239 // the associated event is immediately signalled, so the notification function will be executed and the \r
240 // SMM Configuration Protocol will be found if it is already in the handle database.\r
241 //\r
242 { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, NULL },\r
243 //\r
244 // Declare protocl notification on DxeSmmReadyToLock protocols. When this notification is etablished, \r
245 // the associated event is immediately signalled, so the notification function will be executed and the \r
246 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.\r
247 //\r
248 { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, NULL },\r
249 //\r
250 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core\r
251 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core\r
252 // if notified, so the SMM Core can dispatch SMM drivers.\r
253 //\r
254 { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplGuidedEventNotify, &gEfiEventDxeDispatchGuid, NULL },\r
255 //\r
256 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is\r
257 // used to make sure SMRAM is locked before any boot options are processed.\r
258 //\r
259 { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, NULL },\r
260 //\r
261 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform \r
262 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core \r
263 // must guarantee that it does not access any UEFI related structures outside of SMRAM.\r
264 //\r
265 { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, NULL },\r
266 //\r
267 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate \r
268 // and mSmmControl2 from physical addresses to virtual addresses.\r
269 //\r
270 { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, NULL },\r
271 //\r
272 // Terminate the table of event notifications\r
273 //\r
274 { FALSE, FALSE, NULL, NULL, NULL, NULL }\r
275};\r
276\r
40e8cca5 277/**\r
278 Find the maximum SMRAM cache range that covers the range specified by SmramRange.\r
279 \r
280 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.\r
281\r
282 @param SmramRange The SMRAM range to search from.\r
283 @param SmramCacheBase The returned cache range base.\r
284 @param SmramCacheSize The returned cache range size.\r
285\r
286**/\r
287VOID\r
288GetSmramCacheRange (\r
289 IN EFI_SMRAM_DESCRIPTOR *SmramRange,\r
290 OUT EFI_PHYSICAL_ADDRESS *SmramCacheBase,\r
291 OUT UINT64 *SmramCacheSize\r
292 )\r
293{\r
294 UINTN Index;\r
295 EFI_PHYSICAL_ADDRESS RangeCpuStart;\r
296 UINT64 RangePhysicalSize;\r
297 BOOLEAN FoundAjacentRange;\r
298\r
299 *SmramCacheBase = SmramRange->CpuStart;\r
300 *SmramCacheSize = SmramRange->PhysicalSize;\r
301\r
302 do {\r
303 FoundAjacentRange = FALSE;\r
304 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
305 RangeCpuStart = gSmmCorePrivate->SmramRanges[Index].CpuStart;\r
306 RangePhysicalSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
307 if (RangeCpuStart < *SmramCacheBase && *SmramCacheBase == (RangeCpuStart + RangePhysicalSize)) {\r
308 *SmramCacheBase = RangeCpuStart;\r
309 *SmramCacheSize += RangePhysicalSize;\r
310 FoundAjacentRange = TRUE;\r
311 } else if ((*SmramCacheBase + *SmramCacheSize) == RangeCpuStart && RangePhysicalSize > 0) {\r
312 *SmramCacheSize += RangePhysicalSize;\r
313 FoundAjacentRange = TRUE;\r
314 }\r
315 }\r
316 } while (FoundAjacentRange);\r
317 \r
318}\r
319\r
e42e9404 320/**\r
321 Indicate whether the driver is currently executing in the SMM Initialization phase.\r
322\r
323 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
324 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r
325 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r
326\r
327 @retval EFI_INVALID_PARAMETER InSmram was NULL.\r
328 @retval EFI_SUCCESS The call returned successfully.\r
329\r
330**/\r
331EFI_STATUS\r
332EFIAPI\r
333SmmBase2InSmram (\r
334 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
335 OUT BOOLEAN *InSmram\r
336 )\r
337{\r
338 if (InSmram == NULL) {\r
339 return EFI_INVALID_PARAMETER;\r
340 }\r
341\r
342 *InSmram = gSmmCorePrivate->InSmm;\r
343\r
344 return EFI_SUCCESS;\r
345}\r
346\r
347/**\r
348 Retrieves the location of the System Management System Table (SMST).\r
349\r
350 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
351 @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r
352\r
353 @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r
354 @retval EFI_SUCCESS The memory was returned to the system.\r
355 @retval EFI_UNSUPPORTED Not in SMM.\r
356\r
357**/\r
358EFI_STATUS\r
359EFIAPI\r
360SmmBase2GetSmstLocation (\r
361 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
362 OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r
363 )\r
364{\r
365 if ((This == NULL) ||(Smst == NULL)) {\r
366 return EFI_INVALID_PARAMETER;\r
367 }\r
368 \r
369 if (!gSmmCorePrivate->InSmm) {\r
370 return EFI_UNSUPPORTED;\r
371 }\r
372 \r
373 *Smst = gSmmCorePrivate->Smst;\r
374\r
375 return EFI_SUCCESS;\r
376}\r
377\r
378/**\r
379 Communicates with a registered handler.\r
380 \r
381 This function provides a service to send and receive messages from a registered \r
382 UEFI service. This function is part of the SMM Communication Protocol that may \r
383 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode \r
384 after SetVirtualAddressMap().\r
385\r
386 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
2292758d 387 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.\r
388 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data\r
e42e9404 389 being returned. Zero if the handler does not wish to reply with any data.\r
390\r
391 @retval EFI_SUCCESS The message was successfully posted.\r
392 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r
393**/\r
394EFI_STATUS\r
395EFIAPI\r
396SmmCommunicationCommunicate (\r
397 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r
398 IN OUT VOID *CommBuffer,\r
399 IN OUT UINTN *CommSize\r
400 )\r
401{\r
402 EFI_STATUS Status;\r
403 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r
404 BOOLEAN OldInSmm;\r
405\r
406 //\r
407 // Check parameters\r
408 //\r
409 if ((CommBuffer == NULL) || (CommSize == NULL)) {\r
410 return EFI_INVALID_PARAMETER;\r
411 }\r
412\r
ab780ebf
JY
413 //\r
414 // CommSize must hold HeaderGuid and MessageLength\r
415 //\r
416 if (*CommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {\r
417 return EFI_INVALID_PARAMETER;\r
418 }\r
419\r
e42e9404 420 //\r
421 // If not already in SMM, then generate a Software SMI\r
422 //\r
423 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {\r
424 //\r
425 // Put arguments for Software SMI in gSmmCorePrivate\r
426 //\r
427 gSmmCorePrivate->CommunicationBuffer = CommBuffer;\r
ab780ebf 428 gSmmCorePrivate->BufferSize = *CommSize;\r
e42e9404 429\r
430 //\r
431 // Generate Software SMI\r
432 //\r
433 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);\r
434 if (EFI_ERROR (Status)) {\r
435 return EFI_UNSUPPORTED;\r
436 }\r
437\r
438 //\r
439 // Return status from software SMI \r
440 //\r
ab780ebf 441 *CommSize = gSmmCorePrivate->BufferSize;\r
e42e9404 442 return gSmmCorePrivate->ReturnStatus;\r
443 }\r
444\r
445 //\r
446 // If we are in SMM, then the execution mode must be physical, which means that\r
447 // OS established virtual addresses can not be used. If SetVirtualAddressMap()\r
448 // has been called, then a direct invocation of the Software SMI is not \r
449 // not allowed so return EFI_INVALID_PARAMETER.\r
450 //\r
451 if (EfiGoneVirtual()) {\r
452 return EFI_INVALID_PARAMETER;\r
453 }\r
454\r
3c5963cf 455 //\r
456 // Don't allow call SmiManage() directly when SMRAM is closed or locked.\r
457 //\r
458 if (!mSmmAccess->OpenState || mSmmAccess->LockState) {\r
459 return EFI_INVALID_PARAMETER;\r
460 }\r
461 \r
e42e9404 462 //\r
463 // Save current InSmm state and set InSmm state to TRUE\r
464 //\r
465 OldInSmm = gSmmCorePrivate->InSmm;\r
466 gSmmCorePrivate->InSmm = TRUE;\r
467\r
468 //\r
469 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.\r
470 //\r
471 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;\r
472 *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
473 Status = gSmmCorePrivate->Smst->SmiManage (\r
474 &CommunicateHeader->HeaderGuid, \r
475 NULL, \r
476 CommunicateHeader->Data, \r
477 CommSize\r
478 );\r
479\r
480 //\r
481 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
482 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
483 //\r
484 *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
485\r
486 //\r
487 // Restore original InSmm state\r
488 //\r
489 gSmmCorePrivate->InSmm = OldInSmm;\r
490\r
491 return (Status == EFI_WARN_INTERRUPT_SOURCE_QUIESCED) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
492}\r
493\r
494/**\r
495 Event notification that is fired when DxeDispatch Event Group is signaled.\r
496\r
497 @param Event The Event that is being processed, not used.\r
498 @param Context Event Context, not used.\r
499\r
500**/\r
501VOID\r
502EFIAPI\r
503SmmIplGuidedEventNotify (\r
504 IN EFI_EVENT Event,\r
505 IN VOID *Context\r
506 )\r
507{\r
508 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader;\r
509 UINTN Size;\r
510\r
511 //\r
512 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure \r
513 //\r
514 CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
515 CommunicateHeader.MessageLength = 1;\r
516 CommunicateHeader.Data[0] = 0;\r
517\r
518 //\r
519 // Generate the Software SMI and return the result\r
520 //\r
521 Size = sizeof (CommunicateHeader);\r
522 SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);\r
523}\r
524\r
525/**\r
526 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r
527\r
528 @param Event The Event that is being processed, not used.\r
529 @param Context Event Context, not used.\r
530\r
531**/\r
532VOID\r
533EFIAPI\r
534SmmIplSmmConfigurationEventNotify (\r
535 IN EFI_EVENT Event,\r
536 IN VOID *Context\r
537 )\r
538{\r
539 EFI_STATUS Status;\r
540 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
541\r
542 //\r
543 // Make sure this notification is for this handler\r
544 //\r
545 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);\r
546 if (EFI_ERROR (Status)) {\r
547 return;\r
548 }\r
549\r
550 //\r
551 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol\r
552 //\r
553 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);\r
554 ASSERT_EFI_ERROR (Status);\r
555\r
556 //\r
557 // Set flag to indicate that the SM< Entry Point has been registered which \r
558 // means that SMIs are now fully operational.\r
559 //\r
560 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;\r
561\r
562 //\r
563 // Print debug message showing SMM Core entry point address.\r
564 //\r
565 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));\r
566\r
567 //\r
568 // Attempt to reset SMRAM cacheability to UC\r
569 //\r
570 Status = gDS->SetMemorySpaceAttributes(\r
40e8cca5 571 mSmramCacheBase, \r
572 mSmramCacheSize,\r
e42e9404 573 EFI_MEMORY_UC\r
574 );\r
575 if (EFI_ERROR (Status)) {\r
576 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
577 } \r
578\r
579 //\r
580 // Close all SMRAM ranges to protect SMRAM\r
581 //\r
582 Status = mSmmAccess->Close (mSmmAccess);\r
583 ASSERT_EFI_ERROR (Status);\r
584\r
585 //\r
586 // Print debug message that the SMRAM window is now closed.\r
587 //\r
588 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
589}\r
590\r
591/**\r
592 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
593 or if gEfiEventReadyToBootGuid is signalled.\r
594\r
595 @param Event The Event that is being processed, not used.\r
596 @param Context Event Context, not used.\r
597\r
598**/\r
599VOID\r
600EFIAPI\r
601SmmIplReadyToLockEventNotify (\r
602 IN EFI_EVENT Event,\r
603 IN VOID *Context\r
604 )\r
605{\r
606 EFI_STATUS Status;\r
607 VOID *Interface;\r
608 UINTN Index;\r
609\r
610 //\r
611 // See if we are already locked\r
612 //\r
613 if (mSmmLocked) {\r
614 return;\r
615 }\r
616 \r
617 //\r
618 // Make sure this notification is for this handler\r
619 //\r
620 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {\r
621 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);\r
622 if (EFI_ERROR (Status)) {\r
623 return;\r
624 }\r
625 } else {\r
626 //\r
627 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being \r
628 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.\r
629 // Print a warning on debug builds.\r
630 //\r
631 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));\r
632 }\r
633\r
634 //\r
635 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r
636 //\r
637 mSmmAccess->Lock (mSmmAccess);\r
638\r
639 //\r
640 // Close protocol and event notification events that do not apply after the \r
641 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot \r
642 // event has been signalled.\r
643 //\r
644 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
645 if (mSmmIplEvents[Index].CloseOnLock) {\r
646 gBS->CloseEvent (mSmmIplEvents[Index].Event);\r
647 }\r
648 }\r
649\r
650 //\r
651 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed\r
652 //\r
653 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);\r
654\r
655 //\r
656 // Print debug message that the SMRAM window is now locked.\r
657 //\r
658 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));\r
659 \r
660 //\r
661 // Set flag so this operation will not be performed again\r
662 //\r
663 mSmmLocked = TRUE;\r
664}\r
665\r
666/**\r
667 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
668\r
669 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
670 It convers pointer to new virtual address.\r
671\r
672 @param Event Event whose notification function is being invoked.\r
673 @param Context Pointer to the notification function's context.\r
674\r
675**/\r
676VOID\r
677EFIAPI\r
678SmmIplSetVirtualAddressNotify (\r
679 IN EFI_EVENT Event,\r
680 IN VOID *Context\r
681 )\r
682{\r
683 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);\r
684}\r
685\r
3c447c27 686/**\r
687 Get the fixed loadding address from image header assigned by build tool. This function only be called\r
688 when Loading module at Fixed address feature enabled.\r
e42e9404 689\r
3c447c27 690 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
691 image that needs to be examined by this function.\r
692 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r
693 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.\r
694**/\r
695EFI_STATUS\r
696GetPeCoffImageFixLoadingAssignedAddress(\r
697 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
698 )\r
699{\r
700 UINTN SectionHeaderOffset;\r
701 EFI_STATUS Status;\r
702 EFI_IMAGE_SECTION_HEADER SectionHeader;\r
703 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
704 EFI_PHYSICAL_ADDRESS FixLoaddingAddress;\r
705 UINT16 Index;\r
706 UINTN Size;\r
707 UINT16 NumberOfSections;\r
708 EFI_PHYSICAL_ADDRESS SmramBase;\r
709 UINT64 SmmCodeSize;\r
710 UINT64 ValueInSectionHeader;\r
711 //\r
712 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
713 //\r
714 SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));\r
715 \r
716 FixLoaddingAddress = 0;\r
717 Status = EFI_NOT_FOUND;\r
718 SmramBase = mCurrentSmramRange->CpuStart;\r
719 //\r
720 // Get PeHeader pointer\r
721 //\r
722 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
723 SectionHeaderOffset = (UINTN)(\r
724 ImageContext->PeCoffHeaderOffset +\r
725 sizeof (UINT32) +\r
726 sizeof (EFI_IMAGE_FILE_HEADER) +\r
727 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
728 );\r
729 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
730\r
731 //\r
732 // Get base address from the first section header that doesn't point to code section.\r
733 //\r
734 for (Index = 0; Index < NumberOfSections; Index++) {\r
735 //\r
736 // Read section header from file\r
737 //\r
738 Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
739 Status = ImageContext->ImageRead (\r
740 ImageContext->Handle,\r
741 SectionHeaderOffset,\r
742 &Size,\r
743 &SectionHeader\r
744 );\r
745 if (EFI_ERROR (Status)) {\r
746 return Status;\r
747 }\r
748 \r
749 Status = EFI_NOT_FOUND;\r
750 \r
751 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
752 //\r
753 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the\r
754 // first section header that doesn't point to code section in image header. And there is an assumption that when the\r
755 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers\r
756 // fields should NOT be Zero, or else, these 2 fileds should be set to Zero\r
757 //\r
758 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
759 if (ValueInSectionHeader != 0) {\r
760 //\r
761 // Found first section header that doesn't point to code section in which uild tool saves the\r
762 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
763 //\r
764 FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r
765\r
766 if (SmramBase + SmmCodeSize > FixLoaddingAddress && SmramBase <= FixLoaddingAddress) {\r
767 //\r
768 // The assigned address is valid. Return the specified loadding address\r
769 //\r
770 ImageContext->ImageAddress = FixLoaddingAddress;\r
771 Status = EFI_SUCCESS;\r
772 }\r
773 }\r
774 break;\r
775 }\r
776 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
777 }\r
778 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoaddingAddress, Status));\r
779 return Status;\r
780}\r
e42e9404 781/**\r
782 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r
783\r
784 @param[in] SmramRange Descriptor for the range of SMRAM to reload the \r
785 currently executing image.\r
786 @param[in] Context Context to pass into SMM Core\r
787\r
788 @return EFI_STATUS\r
789\r
790**/\r
791EFI_STATUS\r
792ExecuteSmmCoreFromSmram (\r
793 IN EFI_SMRAM_DESCRIPTOR *SmramRange,\r
794 IN VOID *Context\r
795 )\r
796{\r
797 EFI_STATUS Status;\r
798 VOID *SourceBuffer;\r
799 UINTN SourceSize;\r
800 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
801 UINTN PageCount;\r
802 EFI_PHYSICAL_ADDRESS DestinationBuffer;\r
803 EFI_IMAGE_ENTRY_POINT EntryPoint;\r
804\r
805 //\r
806 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE\r
807 // \r
d7aaf1dc
LG
808 Status = GetSectionFromAnyFvByFileType (\r
809 EFI_FV_FILETYPE_SMM_CORE, \r
810 0,\r
811 EFI_SECTION_PE32, \r
812 0,\r
813 &SourceBuffer, \r
814 &SourceSize\r
815 );\r
816 if (EFI_ERROR (Status)) {\r
817 return Status;\r
e42e9404 818 }\r
819 \r
820 //\r
821 // Initilize ImageContext\r
822 //\r
823 ImageContext.Handle = SourceBuffer;\r
824 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
825\r
826 //\r
827 // Get information about the image being loaded\r
828 //\r
829 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
830 if (EFI_ERROR (Status)) {\r
831 return Status;\r
832 }\r
e42e9404 833 //\r
3c447c27 834 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to \r
835 // the address assigned by build tool.\r
e42e9404 836 //\r
3c447c27 837 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
838 //\r
839 // Get the fixed loading address assigned by Build tool\r
840 //\r
841 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);\r
842 if (!EFI_ERROR (Status)) {\r
843 //\r
844 // 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
845 //\r
846 PageCount = 0;\r
847 } else {\r
848 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));\r
849 //\r
850 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
851 // specified by SmramRange\r
852 //\r
853 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);\r
e42e9404 854\r
3c447c27 855 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
856 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
e42e9404 857\r
3c447c27 858 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
859 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
e42e9404 860\r
3c447c27 861 //\r
862 // Align buffer on section boundry\r
863 //\r
864 ImageContext.ImageAddress = DestinationBuffer;\r
865 }\r
866 } else {\r
867 //\r
868 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
869 // specified by SmramRange\r
870 //\r
871 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);\r
872\r
873 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
874 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
875\r
876 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
877 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
878\r
879 //\r
880 // Align buffer on section boundry\r
881 //\r
882 ImageContext.ImageAddress = DestinationBuffer;\r
883 }\r
884 \r
e42e9404 885 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
886 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
887\r
888 //\r
889 // Print debug message showing SMM Core load address.\r
890 //\r
891 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));\r
892\r
893 //\r
894 // Load the image to our new buffer\r
895 //\r
896 Status = PeCoffLoaderLoadImage (&ImageContext);\r
897 if (!EFI_ERROR (Status)) {\r
898 //\r
899 // Relocate the image in our new buffer\r
900 //\r
901 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
902 if (!EFI_ERROR (Status)) {\r
903 //\r
904 // Flush the instruction cache so the image data are written before we execute it\r
905 //\r
906 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
907\r
908 //\r
909 // Print debug message showing SMM Core entry point address.\r
910 //\r
911 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));\r
912\r
913 //\r
914 // Execute image\r
915 //\r
916 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;\r
917 Status = EntryPoint ((EFI_HANDLE)Context, gST);\r
918 }\r
919 }\r
920\r
921 //\r
922 // If the load operation, relocate operation, or the image execution return an\r
923 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by \r
924 // SmramRange\r
925 //\r
926 if (EFI_ERROR (Status)) {\r
927 SmramRange->PhysicalSize += EFI_PAGES_TO_SIZE (PageCount);\r
928 }\r
929\r
930 //\r
931 // Always free memory allocted by GetFileBufferByFilePath ()\r
932 //\r
933 FreePool (SourceBuffer);\r
934\r
935 return Status;\r
936}\r
937\r
938/**\r
939 The Entry Point for SMM IPL\r
940\r
941 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install \r
942 SMM Base 2 Protocol and SMM Communication Protocol, and register for the \r
943 critical events required to coordinate between DXE and SMM environments.\r
944 \r
945 @param ImageHandle The firmware allocated handle for the EFI image.\r
946 @param SystemTable A pointer to the EFI System Table.\r
947\r
948 @retval EFI_SUCCESS The entry point is executed successfully.\r
949 @retval Other Some error occurred when executing this entry point.\r
950\r
951**/\r
952EFI_STATUS\r
953EFIAPI\r
954SmmIplEntry (\r
955 IN EFI_HANDLE ImageHandle,\r
956 IN EFI_SYSTEM_TABLE *SystemTable\r
957 )\r
958{\r
959 EFI_STATUS Status;\r
960 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
961 UINTN Size;\r
962 UINTN Index;\r
963 EFI_SMM_RESERVED_SMRAM_REGION *SmramResRegion;\r
964 UINT64 MaxSize;\r
965 VOID *Registration;\r
3c447c27 966 UINT64 SmmCodeSize;\r
967 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *LMFAConfigurationTable;\r
e42e9404 968\r
969 //\r
970 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the \r
971 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded \r
972 // by the SMM Core\r
973 //\r
974 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r
975\r
976 //\r
977 // Get SMM Access Protocol\r
978 //\r
979 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);\r
980 ASSERT_EFI_ERROR (Status);\r
981\r
982 //\r
983 // Get SMM Control2 Protocol\r
984 //\r
985 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);\r
986 ASSERT_EFI_ERROR (Status);\r
987\r
988 //\r
989 // Get SMM Configuration Protocol if it is present\r
990 //\r
991 SmmConfiguration = NULL;\r
992 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);\r
993\r
994 //\r
995 // Get SMRAM information\r
996 //\r
997 Size = 0;\r
998 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);\r
999 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
1000\r
1001 gSmmCorePrivate->SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);\r
1002 ASSERT (gSmmCorePrivate->SmramRanges != NULL);\r
1003\r
1004 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, gSmmCorePrivate->SmramRanges);\r
1005 ASSERT_EFI_ERROR (Status);\r
1006\r
1007 gSmmCorePrivate->SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
1008\r
1009 //\r
1010 // Open all SMRAM ranges\r
1011 //\r
1012 Status = mSmmAccess->Open (mSmmAccess);\r
1013 ASSERT_EFI_ERROR (Status);\r
1014\r
1015 //\r
1016 // Print debug message that the SMRAM window is now open.\r
1017 //\r
1018 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r
1019\r
1020 //\r
1021 // Subtract SMRAM any reserved SMRAM regions.\r
1022 //\r
1023 if (SmmConfiguration != NULL) {\r
1024 SmramResRegion = SmmConfiguration->SmramReservedRegions;\r
1025 while (SmramResRegion->SmramReservedSize != 0) {\r
1026 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index ++) {\r
1027 if ((SmramResRegion->SmramReservedStart >= gSmmCorePrivate->SmramRanges[Index].CpuStart) && \\r
1028 ((SmramResRegion->SmramReservedStart + SmramResRegion->SmramReservedSize) <= \\r
1029 (gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize))) {\r
1030 //\r
1031 // This range has reserved area, calculate the left free size\r
1032 //\r
1033 gSmmCorePrivate->SmramRanges[Index].PhysicalSize = SmramResRegion->SmramReservedStart - gSmmCorePrivate->SmramRanges[Index].CpuStart;\r
1034 }\r
1035 }\r
1036 SmramResRegion++;\r
1037 }\r
1038 }\r
1039 \r
1040 //\r
06b07ce3 1041 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r
e42e9404 1042 //\r
1043 mCurrentSmramRange = NULL;\r
06b07ce3 1044 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
e42e9404 1045 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r
1046 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {\r
1047 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r
1048 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
1049 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r
1050 }\r
1051 }\r
1052 }\r
1053 }\r
1054\r
1055 if (mCurrentSmramRange != NULL) {\r
1056 //\r
1057 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r
1058 //\r
1059 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n", \r
1060 (VOID *)(UINTN)mCurrentSmramRange->CpuStart, \r
1061 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r
1062 ));\r
1063\r
40e8cca5 1064 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);\r
e42e9404 1065 //\r
1066 // Attempt to set SMRAM cacheability to WB\r
1067 //\r
1068 Status = gDS->SetMemorySpaceAttributes(\r
40e8cca5 1069 mSmramCacheBase, \r
1070 mSmramCacheSize,\r
e42e9404 1071 EFI_MEMORY_WB\r
1072 );\r
1073 if (EFI_ERROR (Status)) {\r
1074 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r
1075 } \r
3c447c27 1076 //\r
1077 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load\r
1078 // Modules At Fixed Address Configuration Table.\r
1079 //\r
1080 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
1081 //\r
1082 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
1083 //\r
1084 SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);\r
1085 //\r
1086 // The SMRAM available memory is assumed to be larger than SmmCodeSize\r
1087 //\r
1088 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);\r
1089 //\r
1090 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.\r
1091 //\r
1092 Status = EfiGetSystemConfigurationTable (\r
1093 &gLoadFixedAddressConfigurationTableGuid,\r
1094 (VOID **) &LMFAConfigurationTable\r
1095 );\r
1096 if (!EFI_ERROR (Status) && LMFAConfigurationTable != NULL) {\r
1097 LMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r
2d5ac154 1098 //\r
1099 // Print the SMRAM base\r
1100 //\r
1101 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable->SmramBase));\r
3c447c27 1102 }\r
3c447c27 1103 }\r
e42e9404 1104 //\r
1105 // Load SMM Core into SMRAM and execute it from SMRAM\r
1106 //\r
1107 Status = ExecuteSmmCoreFromSmram (mCurrentSmramRange, gSmmCorePrivate);\r
1108 if (EFI_ERROR (Status)) {\r
1109 //\r
1110 // Print error message that the SMM Core failed to be loaded and executed.\r
1111 //\r
1112 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));\r
1113\r
1114 //\r
1115 // Attempt to reset SMRAM cacheability to UC\r
1116 //\r
1117 Status = gDS->SetMemorySpaceAttributes(\r
40e8cca5 1118 mSmramCacheBase, \r
1119 mSmramCacheSize,\r
e42e9404 1120 EFI_MEMORY_UC\r
1121 );\r
1122 if (EFI_ERROR (Status)) {\r
1123 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
1124 } \r
1125 }\r
1126 } else {\r
1127 //\r
1128 // Print error message that there are not enough SMRAM resources to load the SMM Core.\r
1129 //\r
1130 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));\r
1131 }\r
1132\r
1133 //\r
1134 // If the SMM Core could not be loaded then close SMRAM window, free allocated \r
1135 // resources, and return an error so SMM IPL will be unloaded.\r
1136 //\r
1137 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {\r
1138 //\r
1139 // Close all SMRAM ranges\r
1140 //\r
1141 Status = mSmmAccess->Close (mSmmAccess);\r
1142 ASSERT_EFI_ERROR (Status);\r
1143\r
1144 //\r
1145 // Print debug message that the SMRAM window is now closed.\r
1146 //\r
1147 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
1148\r
1149 //\r
1150 // Free all allocated resources\r
1151 //\r
1152 FreePool (gSmmCorePrivate->SmramRanges);\r
1153 \r
1154 return EFI_UNSUPPORTED;\r
1155 }\r
1156 \r
1157 //\r
1158 // Install SMM Base2 Protocol and SMM Communication Protocol\r
1159 //\r
1160 Status = gBS->InstallMultipleProtocolInterfaces (\r
1161 &mSmmIplHandle,\r
1162 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,\r
1163 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,\r
1164 NULL\r
1165 );\r
1166 ASSERT_EFI_ERROR (Status);\r
1167\r
1168 //\r
1169 // Create the set of protocol and event notififcations that the SMM IPL requires\r
1170 //\r
1171 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
1172 if (mSmmIplEvents[Index].Protocol) {\r
1173 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (\r
1174 mSmmIplEvents[Index].Guid,\r
1175 TPL_CALLBACK,\r
1176 mSmmIplEvents[Index].NotifyFunction,\r
1177 mSmmIplEvents[Index].NotifyContext,\r
1178 &Registration\r
1179 );\r
1180 } else {\r
1181 Status = gBS->CreateEventEx (\r
1182 EVT_NOTIFY_SIGNAL,\r
1183 TPL_CALLBACK,\r
1184 mSmmIplEvents[Index].NotifyFunction,\r
1185 mSmmIplEvents[Index].NotifyContext,\r
1186 mSmmIplEvents[Index].Guid,\r
1187 &mSmmIplEvents[Index].Event\r
1188 );\r
1189 ASSERT_EFI_ERROR (Status);\r
1190 }\r
1191 }\r
1192\r
1193 return EFI_SUCCESS;\r
1194}\r