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