2 SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Protocol/SmmBase2.h>
18 #include <Protocol/SmmCommunication.h>
19 #include <Protocol/SmmAccess2.h>
20 #include <Protocol/SmmConfiguration.h>
21 #include <Protocol/SmmControl2.h>
22 #include <Protocol/DxeSmmReadyToLock.h>
23 #include <Protocol/FirmwareVolume2.h>
25 #include <Guid/EventGroup.h>
26 #include <Guid/EventLegacyBios.h>
28 #include <Library/BaseLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/PeCoffLib.h>
31 #include <Library/CacheMaintenanceLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/DebugLib.h>
34 #include <Library/UefiBootServicesTableLib.h>
35 #include <Library/DxeServicesTableLib.h>
36 #include <Library/UefiLib.h>
37 #include <Library/UefiRuntimeLib.h>
39 #include "PiSmmCorePrivateData.h"
42 // Function prototypes from produced protocols
46 Indicate whether the driver is currently executing in the SMM Initialization phase.
48 @param This The EFI_SMM_BASE2_PROTOCOL instance.
49 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
50 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
52 @retval EFI_INVALID_PARAMETER InSmram was NULL.
53 @retval EFI_SUCCESS The call returned successfully.
59 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
64 Retrieves the location of the System Management System Table (SMST).
66 @param This The EFI_SMM_BASE2_PROTOCOL instance.
67 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
69 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
70 @retval EFI_SUCCESS The memory was returned to the system.
71 @retval EFI_UNSUPPORTED Not in SMM.
76 SmmBase2GetSmstLocation (
77 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
78 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
82 Communicates with a registered handler.
84 This function provides a service to send and receive messages from a registered
85 UEFI service. This function is part of the SMM Communication Protocol that may
86 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
87 after SetVirtualAddressMap().
89 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
90 @param[in out] CommBuffer A pointer to the buffer to convey into SMRAM.
91 @param[in out] CommSize The size of the data buffer being passed in.On exit, the size of data
92 being returned. Zero if the handler does not wish to reply with any data.
94 @retval EFI_SUCCESS The message was successfully posted.
95 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
99 SmmCommunicationCommunicate (
100 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
101 IN OUT VOID
*CommBuffer
,
102 IN OUT UINTN
*CommSize
106 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
108 @param Event The Event that is being processed, not used.
109 @param Context Event Context, not used.
114 SmmIplSmmConfigurationEventNotify (
120 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
121 or if gEfiEventReadyToBootGuid is signalled.
123 @param Event The Event that is being processed, not used.
124 @param Context Event Context, not used.
129 SmmIplReadyToLockEventNotify (
135 Event notification that is fired when DxeDispatch Event Group is signaled.
137 @param Event The Event that is being processed, not used.
138 @param Context Event Context, not used.
143 SmmIplGuidedEventNotify (
149 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
151 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
152 It convers pointer to new virtual address.
154 @param Event Event whose notification function is being invoked.
155 @param Context Pointer to the notification function's context.
160 SmmIplSetVirtualAddressNotify (
166 // Data structure used to declare a table of protocol notifications and event
167 // notifications required by the SMM IPL
173 EFI_EVENT_NOTIFY NotifyFunction
;
176 } SMM_IPL_EVENT_NOTIFICATION
;
179 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
181 EFI_HANDLE mSmmIplHandle
= NULL
;
184 // SMM Base 2 Protocol instance
186 EFI_SMM_BASE2_PROTOCOL mSmmBase2
= {
188 SmmBase2GetSmstLocation
192 // SMM Communication Protocol instance
194 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication
= {
195 SmmCommunicationCommunicate
199 // SMM Core Private Data structure that contains the data shared between
200 // the SMM IPL and the SMM Core.
202 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData
= {
203 SMM_CORE_PRIVATE_DATA_SIGNATURE
, // Signature
204 NULL
, // SmmIplImageHandle
205 0, // SmramRangeCount
207 NULL
, // SmmEntryPoint
208 FALSE
, // SmmEntryPointRegistered
212 NULL
, // CommunicationBuffer
213 EFI_SUCCESS
// ReturnStatus
217 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
219 SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
= &mSmmCorePrivateData
;
222 // SMM IPL global variables
224 EFI_SMM_CONTROL2_PROTOCOL
*mSmmControl2
;
225 EFI_SMM_ACCESS2_PROTOCOL
*mSmmAccess
;
226 EFI_SMRAM_DESCRIPTOR
*mCurrentSmramRange
;
227 BOOLEAN mSmmLocked
= FALSE
;
230 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
232 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents
[] = {
234 // Declare protocol notification on the SMM Configuration protocol. When this notification is etablished,
235 // the associated event is immediately signalled, so the notification function will be executed and the
236 // SMM Configuration Protocol will be found if it is already in the handle database.
238 { TRUE
, FALSE
, &gEfiSmmConfigurationProtocolGuid
, SmmIplSmmConfigurationEventNotify
, &gEfiSmmConfigurationProtocolGuid
, NULL
},
240 // Declare protocl notification on DxeSmmReadyToLock protocols. When this notification is etablished,
241 // the associated event is immediately signalled, so the notification function will be executed and the
242 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
244 { TRUE
, TRUE
, &gEfiDxeSmmReadyToLockProtocolGuid
, SmmIplReadyToLockEventNotify
, &gEfiDxeSmmReadyToLockProtocolGuid
, NULL
},
246 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
247 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
248 // if notified, so the SMM Core can dispatch SMM drivers.
250 { FALSE
, TRUE
, &gEfiEventDxeDispatchGuid
, SmmIplGuidedEventNotify
, &gEfiEventDxeDispatchGuid
, NULL
},
252 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
253 // used to make sure SMRAM is locked before any boot options are processed.
255 { FALSE
, TRUE
, &gEfiEventReadyToBootGuid
, SmmIplReadyToLockEventNotify
, &gEfiEventReadyToBootGuid
, NULL
},
257 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
258 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
259 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
261 { FALSE
, FALSE
, &gEfiEventLegacyBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventLegacyBootGuid
, NULL
},
263 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
264 // and mSmmControl2 from physical addresses to virtual addresses.
266 { FALSE
, FALSE
, &gEfiEventVirtualAddressChangeGuid
, SmmIplSetVirtualAddressNotify
, NULL
, NULL
},
268 // Terminate the table of event notifications
270 { FALSE
, FALSE
, NULL
, NULL
, NULL
, NULL
}
274 Indicate whether the driver is currently executing in the SMM Initialization phase.
276 @param This The EFI_SMM_BASE2_PROTOCOL instance.
277 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
278 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
280 @retval EFI_INVALID_PARAMETER InSmram was NULL.
281 @retval EFI_SUCCESS The call returned successfully.
287 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
291 if (InSmram
== NULL
) {
292 return EFI_INVALID_PARAMETER
;
295 *InSmram
= gSmmCorePrivate
->InSmm
;
301 Retrieves the location of the System Management System Table (SMST).
303 @param This The EFI_SMM_BASE2_PROTOCOL instance.
304 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
306 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
307 @retval EFI_SUCCESS The memory was returned to the system.
308 @retval EFI_UNSUPPORTED Not in SMM.
313 SmmBase2GetSmstLocation (
314 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
315 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
318 if ((This
== NULL
) ||(Smst
== NULL
)) {
319 return EFI_INVALID_PARAMETER
;
322 if (!gSmmCorePrivate
->InSmm
) {
323 return EFI_UNSUPPORTED
;
326 *Smst
= gSmmCorePrivate
->Smst
;
332 Communicates with a registered handler.
334 This function provides a service to send and receive messages from a registered
335 UEFI service. This function is part of the SMM Communication Protocol that may
336 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
337 after SetVirtualAddressMap().
339 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
340 @param[in out] CommBuffer A pointer to the buffer to convey into SMRAM.
341 @param[in out] CommSize The size of the data buffer being passed in.On exit, the size of data
342 being returned. Zero if the handler does not wish to reply with any data.
344 @retval EFI_SUCCESS The message was successfully posted.
345 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
349 SmmCommunicationCommunicate (
350 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
351 IN OUT VOID
*CommBuffer
,
352 IN OUT UINTN
*CommSize
356 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
362 if ((CommBuffer
== NULL
) || (CommSize
== NULL
)) {
363 return EFI_INVALID_PARAMETER
;
367 // If not already in SMM, then generate a Software SMI
369 if (!gSmmCorePrivate
->InSmm
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
371 // Put arguments for Software SMI in gSmmCorePrivate
373 gSmmCorePrivate
->CommunicationBuffer
= CommBuffer
;
374 gSmmCorePrivate
->BufferSize
= CommSize
;
377 // Generate Software SMI
379 Status
= mSmmControl2
->Trigger (mSmmControl2
, NULL
, NULL
, FALSE
, 0);
380 if (EFI_ERROR (Status
)) {
381 return EFI_UNSUPPORTED
;
385 // Return status from software SMI
387 return gSmmCorePrivate
->ReturnStatus
;
391 // If we are in SMM, then the execution mode must be physical, which means that
392 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
393 // has been called, then a direct invocation of the Software SMI is not
394 // not allowed so return EFI_INVALID_PARAMETER.
396 if (EfiGoneVirtual()) {
397 return EFI_INVALID_PARAMETER
;
401 // Don't allow call SmiManage() directly when SMRAM is closed or locked.
403 if (!mSmmAccess
->OpenState
|| mSmmAccess
->LockState
) {
404 return EFI_INVALID_PARAMETER
;
408 // Save current InSmm state and set InSmm state to TRUE
410 OldInSmm
= gSmmCorePrivate
->InSmm
;
411 gSmmCorePrivate
->InSmm
= TRUE
;
414 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.
416 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)CommBuffer
;
417 *CommSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
418 Status
= gSmmCorePrivate
->Smst
->SmiManage (
419 &CommunicateHeader
->HeaderGuid
,
421 CommunicateHeader
->Data
,
426 // Update CommunicationBuffer, BufferSize and ReturnStatus
427 // Communicate service finished, reset the pointer to CommBuffer to NULL
429 *CommSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
432 // Restore original InSmm state
434 gSmmCorePrivate
->InSmm
= OldInSmm
;
436 return (Status
== EFI_WARN_INTERRUPT_SOURCE_QUIESCED
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
440 Event notification that is fired when DxeDispatch Event Group is signaled.
442 @param Event The Event that is being processed, not used.
443 @param Context Event Context, not used.
448 SmmIplGuidedEventNotify (
453 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader
;
457 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
459 CopyGuid (&CommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
460 CommunicateHeader
.MessageLength
= 1;
461 CommunicateHeader
.Data
[0] = 0;
464 // Generate the Software SMI and return the result
466 Size
= sizeof (CommunicateHeader
);
467 SmmCommunicationCommunicate (&mSmmCommunication
, &CommunicateHeader
, &Size
);
471 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
473 @param Event The Event that is being processed, not used.
474 @param Context Event Context, not used.
479 SmmIplSmmConfigurationEventNotify (
485 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
488 // Make sure this notification is for this handler
490 Status
= gBS
->LocateProtocol (Context
, NULL
, (VOID
**)&SmmConfiguration
);
491 if (EFI_ERROR (Status
)) {
496 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
498 Status
= SmmConfiguration
->RegisterSmmEntry (SmmConfiguration
, gSmmCorePrivate
->SmmEntryPoint
);
499 ASSERT_EFI_ERROR (Status
);
502 // Set flag to indicate that the SM< Entry Point has been registered which
503 // means that SMIs are now fully operational.
505 gSmmCorePrivate
->SmmEntryPointRegistered
= TRUE
;
508 // Print debug message showing SMM Core entry point address.
510 DEBUG ((DEBUG_INFO
, "SMM IPL registered SMM Entry Point address %p\n", (VOID
*)(UINTN
)gSmmCorePrivate
->SmmEntryPoint
));
513 // Attempt to reset SMRAM cacheability to UC
515 Status
= gDS
->SetMemorySpaceAttributes(
516 mCurrentSmramRange
->CpuStart
,
517 mCurrentSmramRange
->PhysicalSize
,
520 if (EFI_ERROR (Status
)) {
521 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
525 // Close all SMRAM ranges to protect SMRAM
527 Status
= mSmmAccess
->Close (mSmmAccess
);
528 ASSERT_EFI_ERROR (Status
);
531 // Print debug message that the SMRAM window is now closed.
533 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
537 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
538 or if gEfiEventReadyToBootGuid is signalled.
540 @param Event The Event that is being processed, not used.
541 @param Context Event Context, not used.
546 SmmIplReadyToLockEventNotify (
556 // See if we are already locked
563 // Make sure this notification is for this handler
565 if (CompareGuid ((EFI_GUID
*)Context
, &gEfiDxeSmmReadyToLockProtocolGuid
)) {
566 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
567 if (EFI_ERROR (Status
)) {
572 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
573 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
574 // Print a warning on debug builds.
576 DEBUG ((DEBUG_WARN
, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
580 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
582 mSmmAccess
->Lock (mSmmAccess
);
585 // Close protocol and event notification events that do not apply after the
586 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
587 // event has been signalled.
589 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
590 if (mSmmIplEvents
[Index
].CloseOnLock
) {
591 gBS
->CloseEvent (mSmmIplEvents
[Index
].Event
);
596 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
598 SmmIplGuidedEventNotify (Event
, (VOID
*)&gEfiDxeSmmReadyToLockProtocolGuid
);
601 // Print debug message that the SMRAM window is now locked.
603 DEBUG ((DEBUG_INFO
, "SMM IPL locked SMRAM window\n"));
606 // Set flag so this operation will not be performed again
612 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
614 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
615 It convers pointer to new virtual address.
617 @param Event Event whose notification function is being invoked.
618 @param Context Pointer to the notification function's context.
623 SmmIplSetVirtualAddressNotify (
628 EfiConvertPointer (0x0, (VOID
**)&mSmmControl2
);
632 Searches all Firmware Volumes for the first file matching FileType and SectionType and returns the section data.
634 @param FileType FileType to search for within any of the firmware volumes in the platform.
635 @param SectionType SectionType to search for within any of the matching FileTypes in the firmware volumes in the platform.
636 @param SourceSize Return the size of the returned section data..
638 @retval != NULL Pointer to the allocated buffer containing the section data.
639 @retval NULL Section data was not found.
644 IN EFI_FV_FILETYPE FileType
,
645 IN EFI_SECTION_TYPE SectionType
,
646 OUT UINTN
*SourceSize
651 EFI_HANDLE
*HandleBuffer
;
653 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
656 EFI_FV_FILE_ATTRIBUTES Attributes
;
658 UINT32 AuthenticationStatus
;
661 Status
= gBS
->LocateHandleBuffer (
663 &gEfiFirmwareVolume2ProtocolGuid
,
668 if (EFI_ERROR (Status
)) {
672 for (Index
= 0; Index
< HandleCount
; Index
++) {
673 Status
= gBS
->HandleProtocol (
675 &gEfiFirmwareVolume2ProtocolGuid
,
678 if (EFI_ERROR (Status
)) {
683 // Use Firmware Volume 2 Protocol to search for a file of type FileType
686 Status
= Fv
->GetNextFile (Fv
, &Key
, &FileType
, &NameGuid
, &Attributes
, SourceSize
);
687 if (EFI_ERROR (Status
)) {
692 // Use Firmware Volume 2 Protocol to read a section of type SectionType
695 Status
= Fv
->ReadSection (Fv
, &NameGuid
, SectionType
, 0, &SourceBuffer
, SourceSize
, &AuthenticationStatus
);
696 if (!EFI_ERROR (Status
)) {
697 FreePool (HandleBuffer
);
702 FreePool(HandleBuffer
);
708 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
710 @param[in] SmramRange Descriptor for the range of SMRAM to reload the
711 currently executing image.
712 @param[in] Context Context to pass into SMM Core
718 ExecuteSmmCoreFromSmram (
719 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
726 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
728 EFI_PHYSICAL_ADDRESS DestinationBuffer
;
729 EFI_IMAGE_ENTRY_POINT EntryPoint
;
732 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
734 SourceBuffer
= GetSectionInAnyFv (EFI_FV_FILETYPE_SMM_CORE
, EFI_SECTION_PE32
, &SourceSize
);
735 if (SourceBuffer
== NULL
) {
736 return EFI_NOT_FOUND
;
740 // Initilize ImageContext
742 ImageContext
.Handle
= SourceBuffer
;
743 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
746 // Get information about the image being loaded
748 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
749 if (EFI_ERROR (Status
)) {
754 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
755 // specified by SmramRange
757 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
759 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
760 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
762 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
763 DestinationBuffer
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
766 // Align buffer on section boundry
768 ImageContext
.ImageAddress
= DestinationBuffer
;
769 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
770 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
773 // Print debug message showing SMM Core load address.
775 DEBUG ((DEBUG_INFO
, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
));
778 // Load the image to our new buffer
780 Status
= PeCoffLoaderLoadImage (&ImageContext
);
781 if (!EFI_ERROR (Status
)) {
783 // Relocate the image in our new buffer
785 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
786 if (!EFI_ERROR (Status
)) {
788 // Flush the instruction cache so the image data are written before we execute it
790 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
793 // Print debug message showing SMM Core entry point address.
795 DEBUG ((DEBUG_INFO
, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.EntryPoint
));
800 EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)ImageContext
.EntryPoint
;
801 Status
= EntryPoint ((EFI_HANDLE
)Context
, gST
);
806 // If the load operation, relocate operation, or the image execution return an
807 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by
810 if (EFI_ERROR (Status
)) {
811 SmramRange
->PhysicalSize
+= EFI_PAGES_TO_SIZE (PageCount
);
815 // Always free memory allocted by GetFileBufferByFilePath ()
817 FreePool (SourceBuffer
);
823 The Entry Point for SMM IPL
825 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
826 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
827 critical events required to coordinate between DXE and SMM environments.
829 @param ImageHandle The firmware allocated handle for the EFI image.
830 @param SystemTable A pointer to the EFI System Table.
832 @retval EFI_SUCCESS The entry point is executed successfully.
833 @retval Other Some error occurred when executing this entry point.
839 IN EFI_HANDLE ImageHandle
,
840 IN EFI_SYSTEM_TABLE
*SystemTable
844 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
847 EFI_SMM_RESERVED_SMRAM_REGION
*SmramResRegion
;
852 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
853 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
856 mSmmCorePrivateData
.SmmIplImageHandle
= ImageHandle
;
859 // Get SMM Access Protocol
861 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&mSmmAccess
);
862 ASSERT_EFI_ERROR (Status
);
865 // Get SMM Control2 Protocol
867 Status
= gBS
->LocateProtocol (&gEfiSmmControl2ProtocolGuid
, NULL
, (VOID
**)&mSmmControl2
);
868 ASSERT_EFI_ERROR (Status
);
871 // Get SMM Configuration Protocol if it is present
873 SmmConfiguration
= NULL
;
874 Status
= gBS
->LocateProtocol (&gEfiSmmConfigurationProtocolGuid
, NULL
, (VOID
**) &SmmConfiguration
);
877 // Get SMRAM information
880 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, NULL
);
881 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
883 gSmmCorePrivate
->SmramRanges
= (EFI_SMRAM_DESCRIPTOR
*)AllocatePool (Size
);
884 ASSERT (gSmmCorePrivate
->SmramRanges
!= NULL
);
886 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, gSmmCorePrivate
->SmramRanges
);
887 ASSERT_EFI_ERROR (Status
);
889 gSmmCorePrivate
->SmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
892 // Open all SMRAM ranges
894 Status
= mSmmAccess
->Open (mSmmAccess
);
895 ASSERT_EFI_ERROR (Status
);
898 // Print debug message that the SMRAM window is now open.
900 DEBUG ((DEBUG_INFO
, "SMM IPL opened SMRAM window\n"));
903 // Subtract SMRAM any reserved SMRAM regions.
905 if (SmmConfiguration
!= NULL
) {
906 SmramResRegion
= SmmConfiguration
->SmramReservedRegions
;
907 while (SmramResRegion
->SmramReservedSize
!= 0) {
908 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
909 if ((SmramResRegion
->SmramReservedStart
>= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
) && \
910 ((SmramResRegion
->SmramReservedStart
+ SmramResRegion
->SmramReservedSize
) <= \
911 (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
))) {
913 // This range has reserved area, calculate the left free size
915 gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
= SmramResRegion
->SmramReservedStart
- gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
923 // Find the largest SMRAM range between 1MB and 4GB that is at least 1MB in size
925 mCurrentSmramRange
= NULL
;
926 for (Index
= 0, MaxSize
= SIZE_1MB
; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
927 if (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
928 if ((gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
) <= BASE_4GB
) {
929 if (gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
930 MaxSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
931 mCurrentSmramRange
= &gSmmCorePrivate
->SmramRanges
[Index
];
937 if (mCurrentSmramRange
!= NULL
) {
939 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
941 DEBUG ((DEBUG_INFO
, "SMM IPL found SMRAM window %p - %p\n",
942 (VOID
*)(UINTN
)mCurrentSmramRange
->CpuStart
,
943 (VOID
*)(UINTN
)(mCurrentSmramRange
->CpuStart
+ mCurrentSmramRange
->PhysicalSize
- 1)
947 // Attempt to set SMRAM cacheability to WB
949 Status
= gDS
->SetMemorySpaceAttributes(
950 mCurrentSmramRange
->CpuStart
,
951 mCurrentSmramRange
->PhysicalSize
,
954 if (EFI_ERROR (Status
)) {
955 DEBUG ((DEBUG_WARN
, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
959 // Load SMM Core into SMRAM and execute it from SMRAM
961 Status
= ExecuteSmmCoreFromSmram (mCurrentSmramRange
, gSmmCorePrivate
);
962 if (EFI_ERROR (Status
)) {
964 // Print error message that the SMM Core failed to be loaded and executed.
966 DEBUG ((DEBUG_ERROR
, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
969 // Attempt to reset SMRAM cacheability to UC
971 Status
= gDS
->SetMemorySpaceAttributes(
972 mCurrentSmramRange
->CpuStart
,
973 mCurrentSmramRange
->PhysicalSize
,
976 if (EFI_ERROR (Status
)) {
977 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
982 // Print error message that there are not enough SMRAM resources to load the SMM Core.
984 DEBUG ((DEBUG_ERROR
, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
988 // If the SMM Core could not be loaded then close SMRAM window, free allocated
989 // resources, and return an error so SMM IPL will be unloaded.
991 if (mCurrentSmramRange
== NULL
|| EFI_ERROR (Status
)) {
993 // Close all SMRAM ranges
995 Status
= mSmmAccess
->Close (mSmmAccess
);
996 ASSERT_EFI_ERROR (Status
);
999 // Print debug message that the SMRAM window is now closed.
1001 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
1004 // Free all allocated resources
1006 FreePool (gSmmCorePrivate
->SmramRanges
);
1008 return EFI_UNSUPPORTED
;
1012 // Install SMM Base2 Protocol and SMM Communication Protocol
1014 Status
= gBS
->InstallMultipleProtocolInterfaces (
1016 &gEfiSmmBase2ProtocolGuid
, &mSmmBase2
,
1017 &gEfiSmmCommunicationProtocolGuid
, &mSmmCommunication
,
1020 ASSERT_EFI_ERROR (Status
);
1023 // Create the set of protocol and event notififcations that the SMM IPL requires
1025 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
1026 if (mSmmIplEvents
[Index
].Protocol
) {
1027 mSmmIplEvents
[Index
].Event
= EfiCreateProtocolNotifyEvent (
1028 mSmmIplEvents
[Index
].Guid
,
1030 mSmmIplEvents
[Index
].NotifyFunction
,
1031 mSmmIplEvents
[Index
].NotifyContext
,
1035 Status
= gBS
->CreateEventEx (
1038 mSmmIplEvents
[Index
].NotifyFunction
,
1039 mSmmIplEvents
[Index
].NotifyContext
,
1040 mSmmIplEvents
[Index
].Guid
,
1041 &mSmmIplEvents
[Index
].Event
1043 ASSERT_EFI_ERROR (Status
);