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>
27 #include <Guid/LoadModuleAtFixedAddress.h>
29 #include <Library/BaseLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/PeCoffLib.h>
32 #include <Library/CacheMaintenanceLib.h>
33 #include <Library/MemoryAllocationLib.h>
34 #include <Library/DebugLib.h>
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/DxeServicesTableLib.h>
37 #include <Library/UefiLib.h>
38 #include <Library/UefiRuntimeLib.h>
39 #include <Library/PcdLib.h>
41 #include "PiSmmCorePrivateData.h"
44 // Function prototypes from produced protocols
48 Indicate whether the driver is currently executing in the SMM Initialization phase.
50 @param This The EFI_SMM_BASE2_PROTOCOL instance.
51 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
52 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
54 @retval EFI_INVALID_PARAMETER InSmram was NULL.
55 @retval EFI_SUCCESS The call returned successfully.
61 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
66 Retrieves the location of the System Management System Table (SMST).
68 @param This The EFI_SMM_BASE2_PROTOCOL instance.
69 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
71 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
72 @retval EFI_SUCCESS The memory was returned to the system.
73 @retval EFI_UNSUPPORTED Not in SMM.
78 SmmBase2GetSmstLocation (
79 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
80 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
84 Communicates with a registered handler.
86 This function provides a service to send and receive messages from a registered
87 UEFI service. This function is part of the SMM Communication Protocol that may
88 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
89 after SetVirtualAddressMap().
91 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
92 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
93 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
94 being returned. Zero if the handler does not wish to reply with any data.
96 @retval EFI_SUCCESS The message was successfully posted.
97 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
101 SmmCommunicationCommunicate (
102 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
103 IN OUT VOID
*CommBuffer
,
104 IN OUT UINTN
*CommSize
108 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
110 @param Event The Event that is being processed, not used.
111 @param Context Event Context, not used.
116 SmmIplSmmConfigurationEventNotify (
122 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
123 or if gEfiEventReadyToBootGuid is signalled.
125 @param Event The Event that is being processed, not used.
126 @param Context Event Context, not used.
131 SmmIplReadyToLockEventNotify (
137 Event notification that is fired when DxeDispatch Event Group is signaled.
139 @param Event The Event that is being processed, not used.
140 @param Context Event Context, not used.
145 SmmIplGuidedEventNotify (
151 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
153 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
154 It convers pointer to new virtual address.
156 @param Event Event whose notification function is being invoked.
157 @param Context Pointer to the notification function's context.
162 SmmIplSetVirtualAddressNotify (
168 // Data structure used to declare a table of protocol notifications and event
169 // notifications required by the SMM IPL
175 EFI_EVENT_NOTIFY NotifyFunction
;
178 } SMM_IPL_EVENT_NOTIFICATION
;
181 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
183 EFI_HANDLE mSmmIplHandle
= NULL
;
186 // SMM Base 2 Protocol instance
188 EFI_SMM_BASE2_PROTOCOL mSmmBase2
= {
190 SmmBase2GetSmstLocation
194 // SMM Communication Protocol instance
196 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication
= {
197 SmmCommunicationCommunicate
201 // SMM Core Private Data structure that contains the data shared between
202 // the SMM IPL and the SMM Core.
204 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData
= {
205 SMM_CORE_PRIVATE_DATA_SIGNATURE
, // Signature
206 NULL
, // SmmIplImageHandle
207 0, // SmramRangeCount
209 NULL
, // SmmEntryPoint
210 FALSE
, // SmmEntryPointRegistered
214 NULL
, // CommunicationBuffer
215 EFI_SUCCESS
// ReturnStatus
219 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
221 SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
= &mSmmCorePrivateData
;
224 // SMM IPL global variables
226 EFI_SMM_CONTROL2_PROTOCOL
*mSmmControl2
;
227 EFI_SMM_ACCESS2_PROTOCOL
*mSmmAccess
;
228 EFI_SMRAM_DESCRIPTOR
*mCurrentSmramRange
;
229 BOOLEAN mSmmLocked
= FALSE
;
232 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
234 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents
[] = {
236 // Declare protocol notification on the SMM Configuration protocol. When this notification is etablished,
237 // the associated event is immediately signalled, so the notification function will be executed and the
238 // SMM Configuration Protocol will be found if it is already in the handle database.
240 { TRUE
, FALSE
, &gEfiSmmConfigurationProtocolGuid
, SmmIplSmmConfigurationEventNotify
, &gEfiSmmConfigurationProtocolGuid
, NULL
},
242 // Declare protocl notification on DxeSmmReadyToLock protocols. When this notification is etablished,
243 // the associated event is immediately signalled, so the notification function will be executed and the
244 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
246 { TRUE
, TRUE
, &gEfiDxeSmmReadyToLockProtocolGuid
, SmmIplReadyToLockEventNotify
, &gEfiDxeSmmReadyToLockProtocolGuid
, NULL
},
248 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
249 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
250 // if notified, so the SMM Core can dispatch SMM drivers.
252 { FALSE
, TRUE
, &gEfiEventDxeDispatchGuid
, SmmIplGuidedEventNotify
, &gEfiEventDxeDispatchGuid
, NULL
},
254 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
255 // used to make sure SMRAM is locked before any boot options are processed.
257 { FALSE
, TRUE
, &gEfiEventReadyToBootGuid
, SmmIplReadyToLockEventNotify
, &gEfiEventReadyToBootGuid
, NULL
},
259 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
260 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
261 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
263 { FALSE
, FALSE
, &gEfiEventLegacyBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventLegacyBootGuid
, NULL
},
265 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
266 // and mSmmControl2 from physical addresses to virtual addresses.
268 { FALSE
, FALSE
, &gEfiEventVirtualAddressChangeGuid
, SmmIplSetVirtualAddressNotify
, NULL
, NULL
},
270 // Terminate the table of event notifications
272 { FALSE
, FALSE
, NULL
, NULL
, NULL
, NULL
}
276 Indicate whether the driver is currently executing in the SMM Initialization phase.
278 @param This The EFI_SMM_BASE2_PROTOCOL instance.
279 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
280 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
282 @retval EFI_INVALID_PARAMETER InSmram was NULL.
283 @retval EFI_SUCCESS The call returned successfully.
289 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
293 if (InSmram
== NULL
) {
294 return EFI_INVALID_PARAMETER
;
297 *InSmram
= gSmmCorePrivate
->InSmm
;
303 Retrieves the location of the System Management System Table (SMST).
305 @param This The EFI_SMM_BASE2_PROTOCOL instance.
306 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
308 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
309 @retval EFI_SUCCESS The memory was returned to the system.
310 @retval EFI_UNSUPPORTED Not in SMM.
315 SmmBase2GetSmstLocation (
316 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
317 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
320 if ((This
== NULL
) ||(Smst
== NULL
)) {
321 return EFI_INVALID_PARAMETER
;
324 if (!gSmmCorePrivate
->InSmm
) {
325 return EFI_UNSUPPORTED
;
328 *Smst
= gSmmCorePrivate
->Smst
;
334 Communicates with a registered handler.
336 This function provides a service to send and receive messages from a registered
337 UEFI service. This function is part of the SMM Communication Protocol that may
338 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
339 after SetVirtualAddressMap().
341 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
342 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
343 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
344 being returned. Zero if the handler does not wish to reply with any data.
346 @retval EFI_SUCCESS The message was successfully posted.
347 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
351 SmmCommunicationCommunicate (
352 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
353 IN OUT VOID
*CommBuffer
,
354 IN OUT UINTN
*CommSize
358 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
364 if ((CommBuffer
== NULL
) || (CommSize
== NULL
)) {
365 return EFI_INVALID_PARAMETER
;
369 // If not already in SMM, then generate a Software SMI
371 if (!gSmmCorePrivate
->InSmm
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
373 // Put arguments for Software SMI in gSmmCorePrivate
375 gSmmCorePrivate
->CommunicationBuffer
= CommBuffer
;
376 gSmmCorePrivate
->BufferSize
= CommSize
;
379 // Generate Software SMI
381 Status
= mSmmControl2
->Trigger (mSmmControl2
, NULL
, NULL
, FALSE
, 0);
382 if (EFI_ERROR (Status
)) {
383 return EFI_UNSUPPORTED
;
387 // Return status from software SMI
389 return gSmmCorePrivate
->ReturnStatus
;
393 // If we are in SMM, then the execution mode must be physical, which means that
394 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
395 // has been called, then a direct invocation of the Software SMI is not
396 // not allowed so return EFI_INVALID_PARAMETER.
398 if (EfiGoneVirtual()) {
399 return EFI_INVALID_PARAMETER
;
403 // Don't allow call SmiManage() directly when SMRAM is closed or locked.
405 if (!mSmmAccess
->OpenState
|| mSmmAccess
->LockState
) {
406 return EFI_INVALID_PARAMETER
;
410 // Save current InSmm state and set InSmm state to TRUE
412 OldInSmm
= gSmmCorePrivate
->InSmm
;
413 gSmmCorePrivate
->InSmm
= TRUE
;
416 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.
418 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)CommBuffer
;
419 *CommSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
420 Status
= gSmmCorePrivate
->Smst
->SmiManage (
421 &CommunicateHeader
->HeaderGuid
,
423 CommunicateHeader
->Data
,
428 // Update CommunicationBuffer, BufferSize and ReturnStatus
429 // Communicate service finished, reset the pointer to CommBuffer to NULL
431 *CommSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
434 // Restore original InSmm state
436 gSmmCorePrivate
->InSmm
= OldInSmm
;
438 return (Status
== EFI_WARN_INTERRUPT_SOURCE_QUIESCED
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
442 Event notification that is fired when DxeDispatch Event Group is signaled.
444 @param Event The Event that is being processed, not used.
445 @param Context Event Context, not used.
450 SmmIplGuidedEventNotify (
455 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader
;
459 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
461 CopyGuid (&CommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
462 CommunicateHeader
.MessageLength
= 1;
463 CommunicateHeader
.Data
[0] = 0;
466 // Generate the Software SMI and return the result
468 Size
= sizeof (CommunicateHeader
);
469 SmmCommunicationCommunicate (&mSmmCommunication
, &CommunicateHeader
, &Size
);
473 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
475 @param Event The Event that is being processed, not used.
476 @param Context Event Context, not used.
481 SmmIplSmmConfigurationEventNotify (
487 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
490 // Make sure this notification is for this handler
492 Status
= gBS
->LocateProtocol (Context
, NULL
, (VOID
**)&SmmConfiguration
);
493 if (EFI_ERROR (Status
)) {
498 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
500 Status
= SmmConfiguration
->RegisterSmmEntry (SmmConfiguration
, gSmmCorePrivate
->SmmEntryPoint
);
501 ASSERT_EFI_ERROR (Status
);
504 // Set flag to indicate that the SM< Entry Point has been registered which
505 // means that SMIs are now fully operational.
507 gSmmCorePrivate
->SmmEntryPointRegistered
= TRUE
;
510 // Print debug message showing SMM Core entry point address.
512 DEBUG ((DEBUG_INFO
, "SMM IPL registered SMM Entry Point address %p\n", (VOID
*)(UINTN
)gSmmCorePrivate
->SmmEntryPoint
));
515 // Attempt to reset SMRAM cacheability to UC
517 Status
= gDS
->SetMemorySpaceAttributes(
518 mCurrentSmramRange
->CpuStart
,
519 mCurrentSmramRange
->PhysicalSize
,
522 if (EFI_ERROR (Status
)) {
523 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
527 // Close all SMRAM ranges to protect SMRAM
529 Status
= mSmmAccess
->Close (mSmmAccess
);
530 ASSERT_EFI_ERROR (Status
);
533 // Print debug message that the SMRAM window is now closed.
535 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
539 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
540 or if gEfiEventReadyToBootGuid is signalled.
542 @param Event The Event that is being processed, not used.
543 @param Context Event Context, not used.
548 SmmIplReadyToLockEventNotify (
558 // See if we are already locked
565 // Make sure this notification is for this handler
567 if (CompareGuid ((EFI_GUID
*)Context
, &gEfiDxeSmmReadyToLockProtocolGuid
)) {
568 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
569 if (EFI_ERROR (Status
)) {
574 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
575 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
576 // Print a warning on debug builds.
578 DEBUG ((DEBUG_WARN
, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
582 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
584 mSmmAccess
->Lock (mSmmAccess
);
587 // Close protocol and event notification events that do not apply after the
588 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
589 // event has been signalled.
591 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
592 if (mSmmIplEvents
[Index
].CloseOnLock
) {
593 gBS
->CloseEvent (mSmmIplEvents
[Index
].Event
);
598 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
600 SmmIplGuidedEventNotify (Event
, (VOID
*)&gEfiDxeSmmReadyToLockProtocolGuid
);
603 // Print debug message that the SMRAM window is now locked.
605 DEBUG ((DEBUG_INFO
, "SMM IPL locked SMRAM window\n"));
608 // Set flag so this operation will not be performed again
614 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
616 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
617 It convers pointer to new virtual address.
619 @param Event Event whose notification function is being invoked.
620 @param Context Pointer to the notification function's context.
625 SmmIplSetVirtualAddressNotify (
630 EfiConvertPointer (0x0, (VOID
**)&mSmmControl2
);
634 Searches all Firmware Volumes for the first file matching FileType and SectionType and returns the section data.
636 @param FileType FileType to search for within any of the firmware volumes in the platform.
637 @param SectionType SectionType to search for within any of the matching FileTypes in the firmware volumes in the platform.
638 @param SourceSize Return the size of the returned section data..
640 @retval != NULL Pointer to the allocated buffer containing the section data.
641 @retval NULL Section data was not found.
646 IN EFI_FV_FILETYPE FileType
,
647 IN EFI_SECTION_TYPE SectionType
,
648 OUT UINTN
*SourceSize
653 EFI_HANDLE
*HandleBuffer
;
655 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
658 EFI_FV_FILE_ATTRIBUTES Attributes
;
660 UINT32 AuthenticationStatus
;
663 Status
= gBS
->LocateHandleBuffer (
665 &gEfiFirmwareVolume2ProtocolGuid
,
670 if (EFI_ERROR (Status
)) {
674 for (Index
= 0; Index
< HandleCount
; Index
++) {
675 Status
= gBS
->HandleProtocol (
677 &gEfiFirmwareVolume2ProtocolGuid
,
680 if (EFI_ERROR (Status
)) {
685 // Use Firmware Volume 2 Protocol to search for a file of type FileType
688 Status
= Fv
->GetNextFile (Fv
, &Key
, &FileType
, &NameGuid
, &Attributes
, SourceSize
);
689 if (EFI_ERROR (Status
)) {
694 // Use Firmware Volume 2 Protocol to read a section of type SectionType
697 Status
= Fv
->ReadSection (Fv
, &NameGuid
, SectionType
, 0, &SourceBuffer
, SourceSize
, &AuthenticationStatus
);
698 if (!EFI_ERROR (Status
)) {
699 FreePool (HandleBuffer
);
704 FreePool(HandleBuffer
);
709 Get the fixed loadding address from image header assigned by build tool. This function only be called
710 when Loading module at Fixed address feature enabled.
712 @param ImageContext Pointer to the image context structure that describes the PE/COFF
713 image that needs to be examined by this function.
714 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
715 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
718 GetPeCoffImageFixLoadingAssignedAddress(
719 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
722 UINTN SectionHeaderOffset
;
724 EFI_IMAGE_SECTION_HEADER SectionHeader
;
725 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
726 EFI_PHYSICAL_ADDRESS FixLoaddingAddress
;
729 UINT16 NumberOfSections
;
730 EFI_PHYSICAL_ADDRESS SmramBase
;
732 UINT64 ValueInSectionHeader
;
734 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
736 SmmCodeSize
= EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
));
738 FixLoaddingAddress
= 0;
739 Status
= EFI_NOT_FOUND
;
740 SmramBase
= mCurrentSmramRange
->CpuStart
;
742 // Get PeHeader pointer
744 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
745 SectionHeaderOffset
= (UINTN
)(
746 ImageContext
->PeCoffHeaderOffset
+
748 sizeof (EFI_IMAGE_FILE_HEADER
) +
749 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
751 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
754 // Get base address from the first section header that doesn't point to code section.
756 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
758 // Read section header from file
760 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
761 Status
= ImageContext
->ImageRead (
762 ImageContext
->Handle
,
767 if (EFI_ERROR (Status
)) {
771 Status
= EFI_NOT_FOUND
;
773 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
775 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
776 // first section header that doesn't point to code section in image header. And there is an assumption that when the
777 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
778 // fields should NOT be Zero, or else, these 2 fileds should be set to Zero
780 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
781 if (ValueInSectionHeader
!= 0) {
783 // Found first section header that doesn't point to code section in which uild tool saves the
784 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
786 FixLoaddingAddress
= (EFI_PHYSICAL_ADDRESS
)(SmramBase
+ (INT64
)ValueInSectionHeader
);
788 if (SmramBase
+ SmmCodeSize
> FixLoaddingAddress
&& SmramBase
<= FixLoaddingAddress
) {
790 // The assigned address is valid. Return the specified loadding address
792 ImageContext
->ImageAddress
= FixLoaddingAddress
;
793 Status
= EFI_SUCCESS
;
798 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
800 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoaddingAddress
, Status
));
804 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
806 @param[in] SmramRange Descriptor for the range of SMRAM to reload the
807 currently executing image.
808 @param[in] Context Context to pass into SMM Core
814 ExecuteSmmCoreFromSmram (
815 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
822 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
824 EFI_PHYSICAL_ADDRESS DestinationBuffer
;
825 EFI_IMAGE_ENTRY_POINT EntryPoint
;
828 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
830 SourceBuffer
= GetSectionInAnyFv (EFI_FV_FILETYPE_SMM_CORE
, EFI_SECTION_PE32
, &SourceSize
);
831 if (SourceBuffer
== NULL
) {
832 return EFI_NOT_FOUND
;
836 // Initilize ImageContext
838 ImageContext
.Handle
= SourceBuffer
;
839 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
842 // Get information about the image being loaded
844 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
845 if (EFI_ERROR (Status
)) {
849 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
850 // the address assigned by build tool.
852 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
854 // Get the fixed loading address assigned by Build tool
856 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
857 if (!EFI_ERROR (Status
)) {
859 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
863 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
865 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
866 // specified by SmramRange
868 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
870 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
871 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
873 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
874 DestinationBuffer
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
877 // Align buffer on section boundry
879 ImageContext
.ImageAddress
= DestinationBuffer
;
883 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
884 // specified by SmramRange
886 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
888 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
889 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
891 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
892 DestinationBuffer
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
895 // Align buffer on section boundry
897 ImageContext
.ImageAddress
= DestinationBuffer
;
900 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
901 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
904 // Print debug message showing SMM Core load address.
906 DEBUG ((DEBUG_INFO
, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
));
909 // Load the image to our new buffer
911 Status
= PeCoffLoaderLoadImage (&ImageContext
);
912 if (!EFI_ERROR (Status
)) {
914 // Relocate the image in our new buffer
916 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
917 if (!EFI_ERROR (Status
)) {
919 // Flush the instruction cache so the image data are written before we execute it
921 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
924 // Print debug message showing SMM Core entry point address.
926 DEBUG ((DEBUG_INFO
, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.EntryPoint
));
931 EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)ImageContext
.EntryPoint
;
932 Status
= EntryPoint ((EFI_HANDLE
)Context
, gST
);
937 // If the load operation, relocate operation, or the image execution return an
938 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by
941 if (EFI_ERROR (Status
)) {
942 SmramRange
->PhysicalSize
+= EFI_PAGES_TO_SIZE (PageCount
);
946 // Always free memory allocted by GetFileBufferByFilePath ()
948 FreePool (SourceBuffer
);
954 The Entry Point for SMM IPL
956 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
957 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
958 critical events required to coordinate between DXE and SMM environments.
960 @param ImageHandle The firmware allocated handle for the EFI image.
961 @param SystemTable A pointer to the EFI System Table.
963 @retval EFI_SUCCESS The entry point is executed successfully.
964 @retval Other Some error occurred when executing this entry point.
970 IN EFI_HANDLE ImageHandle
,
971 IN EFI_SYSTEM_TABLE
*SystemTable
975 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
978 EFI_SMM_RESERVED_SMRAM_REGION
*SmramResRegion
;
982 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE
*LMFAConfigurationTable
;
985 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
986 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
989 mSmmCorePrivateData
.SmmIplImageHandle
= ImageHandle
;
992 // Get SMM Access Protocol
994 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&mSmmAccess
);
995 ASSERT_EFI_ERROR (Status
);
998 // Get SMM Control2 Protocol
1000 Status
= gBS
->LocateProtocol (&gEfiSmmControl2ProtocolGuid
, NULL
, (VOID
**)&mSmmControl2
);
1001 ASSERT_EFI_ERROR (Status
);
1004 // Get SMM Configuration Protocol if it is present
1006 SmmConfiguration
= NULL
;
1007 Status
= gBS
->LocateProtocol (&gEfiSmmConfigurationProtocolGuid
, NULL
, (VOID
**) &SmmConfiguration
);
1010 // Get SMRAM information
1013 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, NULL
);
1014 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
1016 gSmmCorePrivate
->SmramRanges
= (EFI_SMRAM_DESCRIPTOR
*)AllocatePool (Size
);
1017 ASSERT (gSmmCorePrivate
->SmramRanges
!= NULL
);
1019 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, gSmmCorePrivate
->SmramRanges
);
1020 ASSERT_EFI_ERROR (Status
);
1022 gSmmCorePrivate
->SmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
1025 // Open all SMRAM ranges
1027 Status
= mSmmAccess
->Open (mSmmAccess
);
1028 ASSERT_EFI_ERROR (Status
);
1031 // Print debug message that the SMRAM window is now open.
1033 DEBUG ((DEBUG_INFO
, "SMM IPL opened SMRAM window\n"));
1036 // Subtract SMRAM any reserved SMRAM regions.
1038 if (SmmConfiguration
!= NULL
) {
1039 SmramResRegion
= SmmConfiguration
->SmramReservedRegions
;
1040 while (SmramResRegion
->SmramReservedSize
!= 0) {
1041 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1042 if ((SmramResRegion
->SmramReservedStart
>= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
) && \
1043 ((SmramResRegion
->SmramReservedStart
+ SmramResRegion
->SmramReservedSize
) <= \
1044 (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
))) {
1046 // This range has reserved area, calculate the left free size
1048 gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
= SmramResRegion
->SmramReservedStart
- gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
1056 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1058 mCurrentSmramRange
= NULL
;
1059 for (Index
= 0, MaxSize
= SIZE_256KB
- EFI_PAGE_SIZE
; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1060 if (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
1061 if ((gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
) <= BASE_4GB
) {
1062 if (gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
1063 MaxSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
1064 mCurrentSmramRange
= &gSmmCorePrivate
->SmramRanges
[Index
];
1070 if (mCurrentSmramRange
!= NULL
) {
1072 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1074 DEBUG ((DEBUG_INFO
, "SMM IPL found SMRAM window %p - %p\n",
1075 (VOID
*)(UINTN
)mCurrentSmramRange
->CpuStart
,
1076 (VOID
*)(UINTN
)(mCurrentSmramRange
->CpuStart
+ mCurrentSmramRange
->PhysicalSize
- 1)
1080 // Attempt to set SMRAM cacheability to WB
1082 Status
= gDS
->SetMemorySpaceAttributes(
1083 mCurrentSmramRange
->CpuStart
,
1084 mCurrentSmramRange
->PhysicalSize
,
1087 if (EFI_ERROR (Status
)) {
1088 DEBUG ((DEBUG_WARN
, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1091 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1092 // Modules At Fixed Address Configuration Table.
1094 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1096 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1098 SmmCodeSize
= LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
), EFI_PAGE_SHIFT
);
1100 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1102 ASSERT (mCurrentSmramRange
->PhysicalSize
> SmmCodeSize
);
1104 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1106 Status
= EfiGetSystemConfigurationTable (
1107 &gLoadFixedAddressConfigurationTableGuid
,
1108 (VOID
**) &LMFAConfigurationTable
1110 if (!EFI_ERROR (Status
) && LMFAConfigurationTable
!= NULL
) {
1111 LMFAConfigurationTable
->SmramBase
= mCurrentSmramRange
->CpuStart
;
1113 // Print the SMRAM base
1115 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable
->SmramBase
));
1119 // Load SMM Core into SMRAM and execute it from SMRAM
1121 Status
= ExecuteSmmCoreFromSmram (mCurrentSmramRange
, gSmmCorePrivate
);
1122 if (EFI_ERROR (Status
)) {
1124 // Print error message that the SMM Core failed to be loaded and executed.
1126 DEBUG ((DEBUG_ERROR
, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1129 // Attempt to reset SMRAM cacheability to UC
1131 Status
= gDS
->SetMemorySpaceAttributes(
1132 mCurrentSmramRange
->CpuStart
,
1133 mCurrentSmramRange
->PhysicalSize
,
1136 if (EFI_ERROR (Status
)) {
1137 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1142 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1144 DEBUG ((DEBUG_ERROR
, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1148 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1149 // resources, and return an error so SMM IPL will be unloaded.
1151 if (mCurrentSmramRange
== NULL
|| EFI_ERROR (Status
)) {
1153 // Close all SMRAM ranges
1155 Status
= mSmmAccess
->Close (mSmmAccess
);
1156 ASSERT_EFI_ERROR (Status
);
1159 // Print debug message that the SMRAM window is now closed.
1161 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
1164 // Free all allocated resources
1166 FreePool (gSmmCorePrivate
->SmramRanges
);
1168 return EFI_UNSUPPORTED
;
1172 // Install SMM Base2 Protocol and SMM Communication Protocol
1174 Status
= gBS
->InstallMultipleProtocolInterfaces (
1176 &gEfiSmmBase2ProtocolGuid
, &mSmmBase2
,
1177 &gEfiSmmCommunicationProtocolGuid
, &mSmmCommunication
,
1180 ASSERT_EFI_ERROR (Status
);
1183 // Create the set of protocol and event notififcations that the SMM IPL requires
1185 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
1186 if (mSmmIplEvents
[Index
].Protocol
) {
1187 mSmmIplEvents
[Index
].Event
= EfiCreateProtocolNotifyEvent (
1188 mSmmIplEvents
[Index
].Guid
,
1190 mSmmIplEvents
[Index
].NotifyFunction
,
1191 mSmmIplEvents
[Index
].NotifyContext
,
1195 Status
= gBS
->CreateEventEx (
1198 mSmmIplEvents
[Index
].NotifyFunction
,
1199 mSmmIplEvents
[Index
].NotifyContext
,
1200 mSmmIplEvents
[Index
].Guid
,
1201 &mSmmIplEvents
[Index
].Event
1203 ASSERT_EFI_ERROR (Status
);