]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
Add RELEASE target
[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
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials are licensed and made available \r
6 under the terms and conditions of the BSD License which accompanies this \r
7 distribution. The full text of the license may be found at \r
8 http://opensource.org/licenses/bsd-license.php \r
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
23#include <Protocol/FirmwareVolume2.h>\r
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
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
213 0, // BufferSize\r
214 NULL, // CommunicationBuffer\r
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
413 //\r
414 // If not already in SMM, then generate a Software SMI\r
415 //\r
416 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {\r
417 //\r
418 // Put arguments for Software SMI in gSmmCorePrivate\r
419 //\r
420 gSmmCorePrivate->CommunicationBuffer = CommBuffer;\r
421 gSmmCorePrivate->BufferSize = CommSize;\r
422\r
423 //\r
424 // Generate Software SMI\r
425 //\r
426 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);\r
427 if (EFI_ERROR (Status)) {\r
428 return EFI_UNSUPPORTED;\r
429 }\r
430\r
431 //\r
432 // Return status from software SMI \r
433 //\r
434 return gSmmCorePrivate->ReturnStatus;\r
435 }\r
436\r
437 //\r
438 // If we are in SMM, then the execution mode must be physical, which means that\r
439 // OS established virtual addresses can not be used. If SetVirtualAddressMap()\r
440 // has been called, then a direct invocation of the Software SMI is not \r
441 // not allowed so return EFI_INVALID_PARAMETER.\r
442 //\r
443 if (EfiGoneVirtual()) {\r
444 return EFI_INVALID_PARAMETER;\r
445 }\r
446\r
3c5963cf 447 //\r
448 // Don't allow call SmiManage() directly when SMRAM is closed or locked.\r
449 //\r
450 if (!mSmmAccess->OpenState || mSmmAccess->LockState) {\r
451 return EFI_INVALID_PARAMETER;\r
452 }\r
453 \r
e42e9404 454 //\r
455 // Save current InSmm state and set InSmm state to TRUE\r
456 //\r
457 OldInSmm = gSmmCorePrivate->InSmm;\r
458 gSmmCorePrivate->InSmm = TRUE;\r
459\r
460 //\r
461 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.\r
462 //\r
463 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;\r
464 *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
465 Status = gSmmCorePrivate->Smst->SmiManage (\r
466 &CommunicateHeader->HeaderGuid, \r
467 NULL, \r
468 CommunicateHeader->Data, \r
469 CommSize\r
470 );\r
471\r
472 //\r
473 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
474 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
475 //\r
476 *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
477\r
478 //\r
479 // Restore original InSmm state\r
480 //\r
481 gSmmCorePrivate->InSmm = OldInSmm;\r
482\r
483 return (Status == EFI_WARN_INTERRUPT_SOURCE_QUIESCED) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
484}\r
485\r
486/**\r
487 Event notification that is fired when DxeDispatch Event Group is signaled.\r
488\r
489 @param Event The Event that is being processed, not used.\r
490 @param Context Event Context, not used.\r
491\r
492**/\r
493VOID\r
494EFIAPI\r
495SmmIplGuidedEventNotify (\r
496 IN EFI_EVENT Event,\r
497 IN VOID *Context\r
498 )\r
499{\r
500 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader;\r
501 UINTN Size;\r
502\r
503 //\r
504 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure \r
505 //\r
506 CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
507 CommunicateHeader.MessageLength = 1;\r
508 CommunicateHeader.Data[0] = 0;\r
509\r
510 //\r
511 // Generate the Software SMI and return the result\r
512 //\r
513 Size = sizeof (CommunicateHeader);\r
514 SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);\r
515}\r
516\r
517/**\r
518 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r
519\r
520 @param Event The Event that is being processed, not used.\r
521 @param Context Event Context, not used.\r
522\r
523**/\r
524VOID\r
525EFIAPI\r
526SmmIplSmmConfigurationEventNotify (\r
527 IN EFI_EVENT Event,\r
528 IN VOID *Context\r
529 )\r
530{\r
531 EFI_STATUS Status;\r
532 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
533\r
534 //\r
535 // Make sure this notification is for this handler\r
536 //\r
537 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);\r
538 if (EFI_ERROR (Status)) {\r
539 return;\r
540 }\r
541\r
542 //\r
543 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol\r
544 //\r
545 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);\r
546 ASSERT_EFI_ERROR (Status);\r
547\r
548 //\r
549 // Set flag to indicate that the SM< Entry Point has been registered which \r
550 // means that SMIs are now fully operational.\r
551 //\r
552 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;\r
553\r
554 //\r
555 // Print debug message showing SMM Core entry point address.\r
556 //\r
557 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));\r
558\r
559 //\r
560 // Attempt to reset SMRAM cacheability to UC\r
561 //\r
562 Status = gDS->SetMemorySpaceAttributes(\r
40e8cca5 563 mSmramCacheBase, \r
564 mSmramCacheSize,\r
e42e9404 565 EFI_MEMORY_UC\r
566 );\r
567 if (EFI_ERROR (Status)) {\r
568 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
569 } \r
570\r
571 //\r
572 // Close all SMRAM ranges to protect SMRAM\r
573 //\r
574 Status = mSmmAccess->Close (mSmmAccess);\r
575 ASSERT_EFI_ERROR (Status);\r
576\r
577 //\r
578 // Print debug message that the SMRAM window is now closed.\r
579 //\r
580 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
581}\r
582\r
583/**\r
584 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
585 or if gEfiEventReadyToBootGuid is signalled.\r
586\r
587 @param Event The Event that is being processed, not used.\r
588 @param Context Event Context, not used.\r
589\r
590**/\r
591VOID\r
592EFIAPI\r
593SmmIplReadyToLockEventNotify (\r
594 IN EFI_EVENT Event,\r
595 IN VOID *Context\r
596 )\r
597{\r
598 EFI_STATUS Status;\r
599 VOID *Interface;\r
600 UINTN Index;\r
601\r
602 //\r
603 // See if we are already locked\r
604 //\r
605 if (mSmmLocked) {\r
606 return;\r
607 }\r
608 \r
609 //\r
610 // Make sure this notification is for this handler\r
611 //\r
612 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {\r
613 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);\r
614 if (EFI_ERROR (Status)) {\r
615 return;\r
616 }\r
617 } else {\r
618 //\r
619 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being \r
620 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.\r
621 // Print a warning on debug builds.\r
622 //\r
623 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));\r
624 }\r
625\r
626 //\r
627 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r
628 //\r
629 mSmmAccess->Lock (mSmmAccess);\r
630\r
631 //\r
632 // Close protocol and event notification events that do not apply after the \r
633 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot \r
634 // event has been signalled.\r
635 //\r
636 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
637 if (mSmmIplEvents[Index].CloseOnLock) {\r
638 gBS->CloseEvent (mSmmIplEvents[Index].Event);\r
639 }\r
640 }\r
641\r
642 //\r
643 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed\r
644 //\r
645 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);\r
646\r
647 //\r
648 // Print debug message that the SMRAM window is now locked.\r
649 //\r
650 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));\r
651 \r
652 //\r
653 // Set flag so this operation will not be performed again\r
654 //\r
655 mSmmLocked = TRUE;\r
656}\r
657\r
658/**\r
659 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
660\r
661 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
662 It convers pointer to new virtual address.\r
663\r
664 @param Event Event whose notification function is being invoked.\r
665 @param Context Pointer to the notification function's context.\r
666\r
667**/\r
668VOID\r
669EFIAPI\r
670SmmIplSetVirtualAddressNotify (\r
671 IN EFI_EVENT Event,\r
672 IN VOID *Context\r
673 )\r
674{\r
675 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);\r
676}\r
677\r
678/**\r
679 Searches all Firmware Volumes for the first file matching FileType and SectionType and returns the section data.\r
680\r
681 @param FileType FileType to search for within any of the firmware volumes in the platform.\r
682 @param SectionType SectionType to search for within any of the matching FileTypes in the firmware volumes in the platform.\r
683 @param SourceSize Return the size of the returned section data..\r
684\r
685 @retval != NULL Pointer to the allocated buffer containing the section data.\r
686 @retval NULL Section data was not found.\r
687\r
688**/\r
689VOID *\r
690GetSectionInAnyFv (\r
691 IN EFI_FV_FILETYPE FileType,\r
692 IN EFI_SECTION_TYPE SectionType,\r
693 OUT UINTN *SourceSize\r
694 )\r
695{\r
696 EFI_STATUS Status;\r
697 UINTN HandleCount;\r
698 EFI_HANDLE *HandleBuffer;\r
699 UINTN Index;\r
700 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
701 UINTN Key;\r
702 EFI_GUID NameGuid;\r
703 EFI_FV_FILE_ATTRIBUTES Attributes;\r
704 VOID *SourceBuffer;\r
705 UINT32 AuthenticationStatus;\r
706\r
707 HandleBuffer = NULL;\r
708 Status = gBS->LocateHandleBuffer (\r
709 ByProtocol,\r
710 &gEfiFirmwareVolume2ProtocolGuid,\r
711 NULL,\r
712 &HandleCount,\r
713 &HandleBuffer\r
714 );\r
715 if (EFI_ERROR (Status)) {\r
716 return NULL;\r
717 }\r
718\r
719 for (Index = 0; Index < HandleCount; Index++) {\r
720 Status = gBS->HandleProtocol (\r
721 HandleBuffer[Index],\r
722 &gEfiFirmwareVolume2ProtocolGuid,\r
723 (VOID **)&Fv\r
724 );\r
725 if (EFI_ERROR (Status)) {\r
726 continue;\r
727 }\r
728\r
729 //\r
730 // Use Firmware Volume 2 Protocol to search for a file of type FileType\r
731 //\r
732 Key = 0; \r
733 Status = Fv->GetNextFile (Fv, &Key, &FileType, &NameGuid, &Attributes, SourceSize);\r
734 if (EFI_ERROR (Status)) {\r
735 continue;\r
736 }\r
737\r
738 //\r
739 // Use Firmware Volume 2 Protocol to read a section of type SectionType\r
740 //\r
741 SourceBuffer = NULL;\r
742 Status = Fv->ReadSection (Fv, &NameGuid, SectionType, 0, &SourceBuffer, SourceSize, &AuthenticationStatus);\r
743 if (!EFI_ERROR (Status)) {\r
744 FreePool (HandleBuffer);\r
745 return SourceBuffer;\r
746 }\r
747 } \r
748\r
749 FreePool(HandleBuffer);\r
750 \r
751 return NULL;\r
752}\r
3c447c27 753/**\r
754 Get the fixed loadding address from image header assigned by build tool. This function only be called\r
755 when Loading module at Fixed address feature enabled.\r
e42e9404 756\r
3c447c27 757 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
758 image that needs to be examined by this function.\r
759 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r
760 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.\r
761**/\r
762EFI_STATUS\r
763GetPeCoffImageFixLoadingAssignedAddress(\r
764 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
765 )\r
766{\r
767 UINTN SectionHeaderOffset;\r
768 EFI_STATUS Status;\r
769 EFI_IMAGE_SECTION_HEADER SectionHeader;\r
770 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
771 EFI_PHYSICAL_ADDRESS FixLoaddingAddress;\r
772 UINT16 Index;\r
773 UINTN Size;\r
774 UINT16 NumberOfSections;\r
775 EFI_PHYSICAL_ADDRESS SmramBase;\r
776 UINT64 SmmCodeSize;\r
777 UINT64 ValueInSectionHeader;\r
778 //\r
779 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
780 //\r
781 SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));\r
782 \r
783 FixLoaddingAddress = 0;\r
784 Status = EFI_NOT_FOUND;\r
785 SmramBase = mCurrentSmramRange->CpuStart;\r
786 //\r
787 // Get PeHeader pointer\r
788 //\r
789 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
790 SectionHeaderOffset = (UINTN)(\r
791 ImageContext->PeCoffHeaderOffset +\r
792 sizeof (UINT32) +\r
793 sizeof (EFI_IMAGE_FILE_HEADER) +\r
794 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
795 );\r
796 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
797\r
798 //\r
799 // Get base address from the first section header that doesn't point to code section.\r
800 //\r
801 for (Index = 0; Index < NumberOfSections; Index++) {\r
802 //\r
803 // Read section header from file\r
804 //\r
805 Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
806 Status = ImageContext->ImageRead (\r
807 ImageContext->Handle,\r
808 SectionHeaderOffset,\r
809 &Size,\r
810 &SectionHeader\r
811 );\r
812 if (EFI_ERROR (Status)) {\r
813 return Status;\r
814 }\r
815 \r
816 Status = EFI_NOT_FOUND;\r
817 \r
818 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
819 //\r
820 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the\r
821 // first section header that doesn't point to code section in image header. And there is an assumption that when the\r
822 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers\r
823 // fields should NOT be Zero, or else, these 2 fileds should be set to Zero\r
824 //\r
825 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
826 if (ValueInSectionHeader != 0) {\r
827 //\r
828 // Found first section header that doesn't point to code section in which uild tool saves the\r
829 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
830 //\r
831 FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r
832\r
833 if (SmramBase + SmmCodeSize > FixLoaddingAddress && SmramBase <= FixLoaddingAddress) {\r
834 //\r
835 // The assigned address is valid. Return the specified loadding address\r
836 //\r
837 ImageContext->ImageAddress = FixLoaddingAddress;\r
838 Status = EFI_SUCCESS;\r
839 }\r
840 }\r
841 break;\r
842 }\r
843 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
844 }\r
845 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoaddingAddress, Status));\r
846 return Status;\r
847}\r
e42e9404 848/**\r
849 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r
850\r
851 @param[in] SmramRange Descriptor for the range of SMRAM to reload the \r
852 currently executing image.\r
853 @param[in] Context Context to pass into SMM Core\r
854\r
855 @return EFI_STATUS\r
856\r
857**/\r
858EFI_STATUS\r
859ExecuteSmmCoreFromSmram (\r
860 IN EFI_SMRAM_DESCRIPTOR *SmramRange,\r
861 IN VOID *Context\r
862 )\r
863{\r
864 EFI_STATUS Status;\r
865 VOID *SourceBuffer;\r
866 UINTN SourceSize;\r
867 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
868 UINTN PageCount;\r
869 EFI_PHYSICAL_ADDRESS DestinationBuffer;\r
870 EFI_IMAGE_ENTRY_POINT EntryPoint;\r
871\r
872 //\r
873 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE\r
874 // \r
875 SourceBuffer = GetSectionInAnyFv (EFI_FV_FILETYPE_SMM_CORE, EFI_SECTION_PE32, &SourceSize);\r
876 if (SourceBuffer == NULL) {\r
877 return EFI_NOT_FOUND;\r
878 }\r
879 \r
880 //\r
881 // Initilize ImageContext\r
882 //\r
883 ImageContext.Handle = SourceBuffer;\r
884 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
885\r
886 //\r
887 // Get information about the image being loaded\r
888 //\r
889 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
890 if (EFI_ERROR (Status)) {\r
891 return Status;\r
892 }\r
e42e9404 893 //\r
3c447c27 894 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to \r
895 // the address assigned by build tool.\r
e42e9404 896 //\r
3c447c27 897 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
898 //\r
899 // Get the fixed loading address assigned by Build tool\r
900 //\r
901 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);\r
902 if (!EFI_ERROR (Status)) {\r
903 //\r
904 // 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
905 //\r
906 PageCount = 0;\r
907 } else {\r
908 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));\r
909 //\r
910 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
911 // specified by SmramRange\r
912 //\r
913 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);\r
e42e9404 914\r
3c447c27 915 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
916 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
e42e9404 917\r
3c447c27 918 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
919 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
e42e9404 920\r
3c447c27 921 //\r
922 // Align buffer on section boundry\r
923 //\r
924 ImageContext.ImageAddress = DestinationBuffer;\r
925 }\r
926 } else {\r
927 //\r
928 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
929 // specified by SmramRange\r
930 //\r
931 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);\r
932\r
933 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
934 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
935\r
936 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
937 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
938\r
939 //\r
940 // Align buffer on section boundry\r
941 //\r
942 ImageContext.ImageAddress = DestinationBuffer;\r
943 }\r
944 \r
e42e9404 945 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
946 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
947\r
948 //\r
949 // Print debug message showing SMM Core load address.\r
950 //\r
951 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));\r
952\r
953 //\r
954 // Load the image to our new buffer\r
955 //\r
956 Status = PeCoffLoaderLoadImage (&ImageContext);\r
957 if (!EFI_ERROR (Status)) {\r
958 //\r
959 // Relocate the image in our new buffer\r
960 //\r
961 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
962 if (!EFI_ERROR (Status)) {\r
963 //\r
964 // Flush the instruction cache so the image data are written before we execute it\r
965 //\r
966 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
967\r
968 //\r
969 // Print debug message showing SMM Core entry point address.\r
970 //\r
971 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));\r
972\r
973 //\r
974 // Execute image\r
975 //\r
976 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;\r
977 Status = EntryPoint ((EFI_HANDLE)Context, gST);\r
978 }\r
979 }\r
980\r
981 //\r
982 // If the load operation, relocate operation, or the image execution return an\r
983 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by \r
984 // SmramRange\r
985 //\r
986 if (EFI_ERROR (Status)) {\r
987 SmramRange->PhysicalSize += EFI_PAGES_TO_SIZE (PageCount);\r
988 }\r
989\r
990 //\r
991 // Always free memory allocted by GetFileBufferByFilePath ()\r
992 //\r
993 FreePool (SourceBuffer);\r
994\r
995 return Status;\r
996}\r
997\r
998/**\r
999 The Entry Point for SMM IPL\r
1000\r
1001 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install \r
1002 SMM Base 2 Protocol and SMM Communication Protocol, and register for the \r
1003 critical events required to coordinate between DXE and SMM environments.\r
1004 \r
1005 @param ImageHandle The firmware allocated handle for the EFI image.\r
1006 @param SystemTable A pointer to the EFI System Table.\r
1007\r
1008 @retval EFI_SUCCESS The entry point is executed successfully.\r
1009 @retval Other Some error occurred when executing this entry point.\r
1010\r
1011**/\r
1012EFI_STATUS\r
1013EFIAPI\r
1014SmmIplEntry (\r
1015 IN EFI_HANDLE ImageHandle,\r
1016 IN EFI_SYSTEM_TABLE *SystemTable\r
1017 )\r
1018{\r
1019 EFI_STATUS Status;\r
1020 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
1021 UINTN Size;\r
1022 UINTN Index;\r
1023 EFI_SMM_RESERVED_SMRAM_REGION *SmramResRegion;\r
1024 UINT64 MaxSize;\r
1025 VOID *Registration;\r
3c447c27 1026 UINT64 SmmCodeSize;\r
1027 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *LMFAConfigurationTable;\r
e42e9404 1028\r
1029 //\r
1030 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the \r
1031 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded \r
1032 // by the SMM Core\r
1033 //\r
1034 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r
1035\r
1036 //\r
1037 // Get SMM Access Protocol\r
1038 //\r
1039 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);\r
1040 ASSERT_EFI_ERROR (Status);\r
1041\r
1042 //\r
1043 // Get SMM Control2 Protocol\r
1044 //\r
1045 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);\r
1046 ASSERT_EFI_ERROR (Status);\r
1047\r
1048 //\r
1049 // Get SMM Configuration Protocol if it is present\r
1050 //\r
1051 SmmConfiguration = NULL;\r
1052 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);\r
1053\r
1054 //\r
1055 // Get SMRAM information\r
1056 //\r
1057 Size = 0;\r
1058 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);\r
1059 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
1060\r
1061 gSmmCorePrivate->SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);\r
1062 ASSERT (gSmmCorePrivate->SmramRanges != NULL);\r
1063\r
1064 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, gSmmCorePrivate->SmramRanges);\r
1065 ASSERT_EFI_ERROR (Status);\r
1066\r
1067 gSmmCorePrivate->SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
1068\r
1069 //\r
1070 // Open all SMRAM ranges\r
1071 //\r
1072 Status = mSmmAccess->Open (mSmmAccess);\r
1073 ASSERT_EFI_ERROR (Status);\r
1074\r
1075 //\r
1076 // Print debug message that the SMRAM window is now open.\r
1077 //\r
1078 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r
1079\r
1080 //\r
1081 // Subtract SMRAM any reserved SMRAM regions.\r
1082 //\r
1083 if (SmmConfiguration != NULL) {\r
1084 SmramResRegion = SmmConfiguration->SmramReservedRegions;\r
1085 while (SmramResRegion->SmramReservedSize != 0) {\r
1086 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index ++) {\r
1087 if ((SmramResRegion->SmramReservedStart >= gSmmCorePrivate->SmramRanges[Index].CpuStart) && \\r
1088 ((SmramResRegion->SmramReservedStart + SmramResRegion->SmramReservedSize) <= \\r
1089 (gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize))) {\r
1090 //\r
1091 // This range has reserved area, calculate the left free size\r
1092 //\r
1093 gSmmCorePrivate->SmramRanges[Index].PhysicalSize = SmramResRegion->SmramReservedStart - gSmmCorePrivate->SmramRanges[Index].CpuStart;\r
1094 }\r
1095 }\r
1096 SmramResRegion++;\r
1097 }\r
1098 }\r
1099 \r
1100 //\r
06b07ce3 1101 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r
e42e9404 1102 //\r
1103 mCurrentSmramRange = NULL;\r
06b07ce3 1104 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
e42e9404 1105 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r
1106 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {\r
1107 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r
1108 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
1109 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r
1110 }\r
1111 }\r
1112 }\r
1113 }\r
1114\r
1115 if (mCurrentSmramRange != NULL) {\r
1116 //\r
1117 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r
1118 //\r
1119 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n", \r
1120 (VOID *)(UINTN)mCurrentSmramRange->CpuStart, \r
1121 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r
1122 ));\r
1123\r
40e8cca5 1124 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);\r
e42e9404 1125 //\r
1126 // Attempt to set SMRAM cacheability to WB\r
1127 //\r
1128 Status = gDS->SetMemorySpaceAttributes(\r
40e8cca5 1129 mSmramCacheBase, \r
1130 mSmramCacheSize,\r
e42e9404 1131 EFI_MEMORY_WB\r
1132 );\r
1133 if (EFI_ERROR (Status)) {\r
1134 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r
1135 } \r
3c447c27 1136 //\r
1137 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load\r
1138 // Modules At Fixed Address Configuration Table.\r
1139 //\r
1140 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
1141 //\r
1142 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
1143 //\r
1144 SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);\r
1145 //\r
1146 // The SMRAM available memory is assumed to be larger than SmmCodeSize\r
1147 //\r
1148 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);\r
1149 //\r
1150 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.\r
1151 //\r
1152 Status = EfiGetSystemConfigurationTable (\r
1153 &gLoadFixedAddressConfigurationTableGuid,\r
1154 (VOID **) &LMFAConfigurationTable\r
1155 );\r
1156 if (!EFI_ERROR (Status) && LMFAConfigurationTable != NULL) {\r
1157 LMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r
2d5ac154 1158 //\r
1159 // Print the SMRAM base\r
1160 //\r
1161 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable->SmramBase));\r
3c447c27 1162 }\r
3c447c27 1163 }\r
e42e9404 1164 //\r
1165 // Load SMM Core into SMRAM and execute it from SMRAM\r
1166 //\r
1167 Status = ExecuteSmmCoreFromSmram (mCurrentSmramRange, gSmmCorePrivate);\r
1168 if (EFI_ERROR (Status)) {\r
1169 //\r
1170 // Print error message that the SMM Core failed to be loaded and executed.\r
1171 //\r
1172 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));\r
1173\r
1174 //\r
1175 // Attempt to reset SMRAM cacheability to UC\r
1176 //\r
1177 Status = gDS->SetMemorySpaceAttributes(\r
40e8cca5 1178 mSmramCacheBase, \r
1179 mSmramCacheSize,\r
e42e9404 1180 EFI_MEMORY_UC\r
1181 );\r
1182 if (EFI_ERROR (Status)) {\r
1183 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
1184 } \r
1185 }\r
1186 } else {\r
1187 //\r
1188 // Print error message that there are not enough SMRAM resources to load the SMM Core.\r
1189 //\r
1190 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));\r
1191 }\r
1192\r
1193 //\r
1194 // If the SMM Core could not be loaded then close SMRAM window, free allocated \r
1195 // resources, and return an error so SMM IPL will be unloaded.\r
1196 //\r
1197 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {\r
1198 //\r
1199 // Close all SMRAM ranges\r
1200 //\r
1201 Status = mSmmAccess->Close (mSmmAccess);\r
1202 ASSERT_EFI_ERROR (Status);\r
1203\r
1204 //\r
1205 // Print debug message that the SMRAM window is now closed.\r
1206 //\r
1207 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
1208\r
1209 //\r
1210 // Free all allocated resources\r
1211 //\r
1212 FreePool (gSmmCorePrivate->SmramRanges);\r
1213 \r
1214 return EFI_UNSUPPORTED;\r
1215 }\r
1216 \r
1217 //\r
1218 // Install SMM Base2 Protocol and SMM Communication Protocol\r
1219 //\r
1220 Status = gBS->InstallMultipleProtocolInterfaces (\r
1221 &mSmmIplHandle,\r
1222 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,\r
1223 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,\r
1224 NULL\r
1225 );\r
1226 ASSERT_EFI_ERROR (Status);\r
1227\r
1228 //\r
1229 // Create the set of protocol and event notififcations that the SMM IPL requires\r
1230 //\r
1231 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
1232 if (mSmmIplEvents[Index].Protocol) {\r
1233 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (\r
1234 mSmmIplEvents[Index].Guid,\r
1235 TPL_CALLBACK,\r
1236 mSmmIplEvents[Index].NotifyFunction,\r
1237 mSmmIplEvents[Index].NotifyContext,\r
1238 &Registration\r
1239 );\r
1240 } else {\r
1241 Status = gBS->CreateEventEx (\r
1242 EVT_NOTIFY_SIGNAL,\r
1243 TPL_CALLBACK,\r
1244 mSmmIplEvents[Index].NotifyFunction,\r
1245 mSmmIplEvents[Index].NotifyContext,\r
1246 mSmmIplEvents[Index].Guid,\r
1247 &mSmmIplEvents[Index].Event\r
1248 );\r
1249 ASSERT_EFI_ERROR (Status);\r
1250 }\r
1251 }\r
1252\r
1253 return EFI_SUCCESS;\r
1254}\r