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/Cpu.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/DxeServicesLib.h>
38 #include <Library/UefiLib.h>
39 #include <Library/UefiRuntimeLib.h>
40 #include <Library/PcdLib.h>
42 #include "PiSmmCorePrivateData.h"
45 // Function prototypes from produced protocols
49 Indicate whether the driver is currently executing in the SMM Initialization phase.
51 @param This The EFI_SMM_BASE2_PROTOCOL instance.
52 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
53 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
55 @retval EFI_INVALID_PARAMETER InSmram was NULL.
56 @retval EFI_SUCCESS The call returned successfully.
62 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
67 Retrieves the location of the System Management System Table (SMST).
69 @param This The EFI_SMM_BASE2_PROTOCOL instance.
70 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
72 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
73 @retval EFI_SUCCESS The memory was returned to the system.
74 @retval EFI_UNSUPPORTED Not in SMM.
79 SmmBase2GetSmstLocation (
80 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
81 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
85 Communicates with a registered handler.
87 This function provides a service to send and receive messages from a registered
88 UEFI service. This function is part of the SMM Communication Protocol that may
89 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
90 after SetVirtualAddressMap().
92 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
93 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
94 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
95 being returned. Zero if the handler does not wish to reply with any data.
97 @retval EFI_SUCCESS The message was successfully posted.
98 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
102 SmmCommunicationCommunicate (
103 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
104 IN OUT VOID
*CommBuffer
,
105 IN OUT UINTN
*CommSize
109 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
111 @param Event The Event that is being processed, not used.
112 @param Context Event Context, not used.
117 SmmIplSmmConfigurationEventNotify (
123 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
124 or if gEfiEventReadyToBootGuid is signalled.
126 @param Event The Event that is being processed, not used.
127 @param Context Event Context, not used.
132 SmmIplReadyToLockEventNotify (
138 Event notification that is fired when DxeDispatch Event Group is signaled.
140 @param Event The Event that is being processed, not used.
141 @param Context Event Context, not used.
146 SmmIplGuidedEventNotify (
152 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
154 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
155 It convers pointer to new virtual address.
157 @param Event Event whose notification function is being invoked.
158 @param Context Pointer to the notification function's context.
163 SmmIplSetVirtualAddressNotify (
169 // Data structure used to declare a table of protocol notifications and event
170 // notifications required by the SMM IPL
176 EFI_EVENT_NOTIFY NotifyFunction
;
179 } SMM_IPL_EVENT_NOTIFICATION
;
182 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
184 EFI_HANDLE mSmmIplHandle
= NULL
;
187 // SMM Base 2 Protocol instance
189 EFI_SMM_BASE2_PROTOCOL mSmmBase2
= {
191 SmmBase2GetSmstLocation
195 // SMM Communication Protocol instance
197 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication
= {
198 SmmCommunicationCommunicate
202 // SMM Core Private Data structure that contains the data shared between
203 // the SMM IPL and the SMM Core.
205 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData
= {
206 SMM_CORE_PRIVATE_DATA_SIGNATURE
, // Signature
207 NULL
, // SmmIplImageHandle
208 0, // SmramRangeCount
210 NULL
, // SmmEntryPoint
211 FALSE
, // SmmEntryPointRegistered
214 NULL
, // CommunicationBuffer
216 EFI_SUCCESS
// ReturnStatus
220 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
222 SMM_CORE_PRIVATE_DATA
*gSmmCorePrivate
= &mSmmCorePrivateData
;
225 // SMM IPL global variables
227 EFI_SMM_CONTROL2_PROTOCOL
*mSmmControl2
;
228 EFI_SMM_ACCESS2_PROTOCOL
*mSmmAccess
;
229 EFI_SMRAM_DESCRIPTOR
*mCurrentSmramRange
;
230 BOOLEAN mSmmLocked
= FALSE
;
231 EFI_PHYSICAL_ADDRESS mSmramCacheBase
;
232 UINT64 mSmramCacheSize
;
235 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
237 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents
[] = {
239 // Declare protocol notification on the SMM Configuration protocol. When this notification is etablished,
240 // the associated event is immediately signalled, so the notification function will be executed and the
241 // SMM Configuration Protocol will be found if it is already in the handle database.
243 { TRUE
, FALSE
, &gEfiSmmConfigurationProtocolGuid
, SmmIplSmmConfigurationEventNotify
, &gEfiSmmConfigurationProtocolGuid
, NULL
},
245 // Declare protocl notification on DxeSmmReadyToLock protocols. When this notification is etablished,
246 // the associated event is immediately signalled, so the notification function will be executed and the
247 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
249 { TRUE
, TRUE
, &gEfiDxeSmmReadyToLockProtocolGuid
, SmmIplReadyToLockEventNotify
, &gEfiDxeSmmReadyToLockProtocolGuid
, NULL
},
251 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
252 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
253 // if notified, so the SMM Core can dispatch SMM drivers.
255 { FALSE
, TRUE
, &gEfiEventDxeDispatchGuid
, SmmIplGuidedEventNotify
, &gEfiEventDxeDispatchGuid
, NULL
},
257 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
258 // used to make sure SMRAM is locked before any boot options are processed.
260 { FALSE
, TRUE
, &gEfiEventReadyToBootGuid
, SmmIplReadyToLockEventNotify
, &gEfiEventReadyToBootGuid
, NULL
},
262 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
263 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
264 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
266 { FALSE
, FALSE
, &gEfiEventLegacyBootGuid
, SmmIplGuidedEventNotify
, &gEfiEventLegacyBootGuid
, NULL
},
268 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
269 // and mSmmControl2 from physical addresses to virtual addresses.
271 { FALSE
, FALSE
, &gEfiEventVirtualAddressChangeGuid
, SmmIplSetVirtualAddressNotify
, NULL
, NULL
},
273 // Terminate the table of event notifications
275 { FALSE
, FALSE
, NULL
, NULL
, NULL
, NULL
}
279 Find the maximum SMRAM cache range that covers the range specified by SmramRange.
281 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
283 @param SmramRange The SMRAM range to search from.
284 @param SmramCacheBase The returned cache range base.
285 @param SmramCacheSize The returned cache range size.
290 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
291 OUT EFI_PHYSICAL_ADDRESS
*SmramCacheBase
,
292 OUT UINT64
*SmramCacheSize
296 EFI_PHYSICAL_ADDRESS RangeCpuStart
;
297 UINT64 RangePhysicalSize
;
298 BOOLEAN FoundAjacentRange
;
300 *SmramCacheBase
= SmramRange
->CpuStart
;
301 *SmramCacheSize
= SmramRange
->PhysicalSize
;
304 FoundAjacentRange
= FALSE
;
305 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
306 RangeCpuStart
= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
307 RangePhysicalSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
308 if (RangeCpuStart
< *SmramCacheBase
&& *SmramCacheBase
== (RangeCpuStart
+ RangePhysicalSize
)) {
309 *SmramCacheBase
= RangeCpuStart
;
310 *SmramCacheSize
+= RangePhysicalSize
;
311 FoundAjacentRange
= TRUE
;
312 } else if ((*SmramCacheBase
+ *SmramCacheSize
) == RangeCpuStart
&& RangePhysicalSize
> 0) {
313 *SmramCacheSize
+= RangePhysicalSize
;
314 FoundAjacentRange
= TRUE
;
317 } while (FoundAjacentRange
);
322 Indicate whether the driver is currently executing in the SMM Initialization phase.
324 @param This The EFI_SMM_BASE2_PROTOCOL instance.
325 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
326 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
328 @retval EFI_INVALID_PARAMETER InSmram was NULL.
329 @retval EFI_SUCCESS The call returned successfully.
335 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
339 if (InSmram
== NULL
) {
340 return EFI_INVALID_PARAMETER
;
343 *InSmram
= gSmmCorePrivate
->InSmm
;
349 Retrieves the location of the System Management System Table (SMST).
351 @param This The EFI_SMM_BASE2_PROTOCOL instance.
352 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
354 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
355 @retval EFI_SUCCESS The memory was returned to the system.
356 @retval EFI_UNSUPPORTED Not in SMM.
361 SmmBase2GetSmstLocation (
362 IN CONST EFI_SMM_BASE2_PROTOCOL
*This
,
363 OUT EFI_SMM_SYSTEM_TABLE2
**Smst
366 if ((This
== NULL
) ||(Smst
== NULL
)) {
367 return EFI_INVALID_PARAMETER
;
370 if (!gSmmCorePrivate
->InSmm
) {
371 return EFI_UNSUPPORTED
;
374 *Smst
= gSmmCorePrivate
->Smst
;
380 Communicates with a registered handler.
382 This function provides a service to send and receive messages from a registered
383 UEFI service. This function is part of the SMM Communication Protocol that may
384 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
385 after SetVirtualAddressMap().
387 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
388 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
389 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
390 being returned. Zero if the handler does not wish to reply with any data.
392 @retval EFI_SUCCESS The message was successfully posted.
393 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
397 SmmCommunicationCommunicate (
398 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL
*This
,
399 IN OUT VOID
*CommBuffer
,
400 IN OUT UINTN
*CommSize
404 EFI_SMM_COMMUNICATE_HEADER
*CommunicateHeader
;
410 if ((CommBuffer
== NULL
) || (CommSize
== NULL
)) {
411 return EFI_INVALID_PARAMETER
;
415 // CommSize must hold HeaderGuid and MessageLength
417 if (*CommSize
< OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
)) {
418 return EFI_INVALID_PARAMETER
;
422 // If not already in SMM, then generate a Software SMI
424 if (!gSmmCorePrivate
->InSmm
&& gSmmCorePrivate
->SmmEntryPointRegistered
) {
426 // Put arguments for Software SMI in gSmmCorePrivate
428 gSmmCorePrivate
->CommunicationBuffer
= CommBuffer
;
429 gSmmCorePrivate
->BufferSize
= *CommSize
;
432 // Generate Software SMI
434 Status
= mSmmControl2
->Trigger (mSmmControl2
, NULL
, NULL
, FALSE
, 0);
435 if (EFI_ERROR (Status
)) {
436 return EFI_UNSUPPORTED
;
440 // Return status from software SMI
442 *CommSize
= gSmmCorePrivate
->BufferSize
;
443 return gSmmCorePrivate
->ReturnStatus
;
447 // If we are in SMM, then the execution mode must be physical, which means that
448 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
449 // has been called, then a direct invocation of the Software SMI is not
450 // not allowed so return EFI_INVALID_PARAMETER.
452 if (EfiGoneVirtual()) {
453 return EFI_INVALID_PARAMETER
;
457 // Don't allow call SmiManage() directly when SMRAM is closed or locked.
459 if (!mSmmAccess
->OpenState
|| mSmmAccess
->LockState
) {
460 return EFI_INVALID_PARAMETER
;
464 // Save current InSmm state and set InSmm state to TRUE
466 OldInSmm
= gSmmCorePrivate
->InSmm
;
467 gSmmCorePrivate
->InSmm
= TRUE
;
470 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.
472 CommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*)CommBuffer
;
473 *CommSize
-= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
474 Status
= gSmmCorePrivate
->Smst
->SmiManage (
475 &CommunicateHeader
->HeaderGuid
,
477 CommunicateHeader
->Data
,
482 // Update CommunicationBuffer, BufferSize and ReturnStatus
483 // Communicate service finished, reset the pointer to CommBuffer to NULL
485 *CommSize
+= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER
, Data
);
488 // Restore original InSmm state
490 gSmmCorePrivate
->InSmm
= OldInSmm
;
492 return (Status
== EFI_WARN_INTERRUPT_SOURCE_QUIESCED
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
496 Event notification that is fired when DxeDispatch Event Group is signaled.
498 @param Event The Event that is being processed, not used.
499 @param Context Event Context, not used.
504 SmmIplGuidedEventNotify (
509 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader
;
513 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
515 CopyGuid (&CommunicateHeader
.HeaderGuid
, (EFI_GUID
*)Context
);
516 CommunicateHeader
.MessageLength
= 1;
517 CommunicateHeader
.Data
[0] = 0;
520 // Generate the Software SMI and return the result
522 Size
= sizeof (CommunicateHeader
);
523 SmmCommunicationCommunicate (&mSmmCommunication
, &CommunicateHeader
, &Size
);
527 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
529 @param Event The Event that is being processed, not used.
530 @param Context Event Context, not used.
535 SmmIplSmmConfigurationEventNotify (
541 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
544 // Make sure this notification is for this handler
546 Status
= gBS
->LocateProtocol (Context
, NULL
, (VOID
**)&SmmConfiguration
);
547 if (EFI_ERROR (Status
)) {
552 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
554 Status
= SmmConfiguration
->RegisterSmmEntry (SmmConfiguration
, gSmmCorePrivate
->SmmEntryPoint
);
555 ASSERT_EFI_ERROR (Status
);
558 // Set flag to indicate that the SM< Entry Point has been registered which
559 // means that SMIs are now fully operational.
561 gSmmCorePrivate
->SmmEntryPointRegistered
= TRUE
;
564 // Print debug message showing SMM Core entry point address.
566 DEBUG ((DEBUG_INFO
, "SMM IPL registered SMM Entry Point address %p\n", (VOID
*)(UINTN
)gSmmCorePrivate
->SmmEntryPoint
));
569 // Attempt to reset SMRAM cacheability to UC
571 Status
= gDS
->SetMemorySpaceAttributes(
576 if (EFI_ERROR (Status
)) {
577 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
581 // Close all SMRAM ranges to protect SMRAM
583 Status
= mSmmAccess
->Close (mSmmAccess
);
584 ASSERT_EFI_ERROR (Status
);
587 // Print debug message that the SMRAM window is now closed.
589 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
593 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
594 or if gEfiEventReadyToBootGuid is signalled.
596 @param Event The Event that is being processed, not used.
597 @param Context Event Context, not used.
602 SmmIplReadyToLockEventNotify (
612 // See if we are already locked
619 // Make sure this notification is for this handler
621 if (CompareGuid ((EFI_GUID
*)Context
, &gEfiDxeSmmReadyToLockProtocolGuid
)) {
622 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
623 if (EFI_ERROR (Status
)) {
628 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
629 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
630 // Print a warning on debug builds.
632 DEBUG ((DEBUG_WARN
, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
636 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
638 mSmmAccess
->Lock (mSmmAccess
);
641 // Close protocol and event notification events that do not apply after the
642 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
643 // event has been signalled.
645 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
646 if (mSmmIplEvents
[Index
].CloseOnLock
) {
647 gBS
->CloseEvent (mSmmIplEvents
[Index
].Event
);
652 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
654 SmmIplGuidedEventNotify (Event
, (VOID
*)&gEfiDxeSmmReadyToLockProtocolGuid
);
657 // Print debug message that the SMRAM window is now locked.
659 DEBUG ((DEBUG_INFO
, "SMM IPL locked SMRAM window\n"));
662 // Set flag so this operation will not be performed again
668 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
670 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
671 It convers pointer to new virtual address.
673 @param Event Event whose notification function is being invoked.
674 @param Context Pointer to the notification function's context.
679 SmmIplSetVirtualAddressNotify (
684 EfiConvertPointer (0x0, (VOID
**)&mSmmControl2
);
688 Get the fixed loadding address from image header assigned by build tool. This function only be called
689 when Loading module at Fixed address feature enabled.
691 @param ImageContext Pointer to the image context structure that describes the PE/COFF
692 image that needs to be examined by this function.
693 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
694 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
697 GetPeCoffImageFixLoadingAssignedAddress(
698 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
701 UINTN SectionHeaderOffset
;
703 EFI_IMAGE_SECTION_HEADER SectionHeader
;
704 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
705 EFI_PHYSICAL_ADDRESS FixLoaddingAddress
;
708 UINT16 NumberOfSections
;
709 EFI_PHYSICAL_ADDRESS SmramBase
;
711 UINT64 ValueInSectionHeader
;
713 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
715 SmmCodeSize
= EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
));
717 FixLoaddingAddress
= 0;
718 Status
= EFI_NOT_FOUND
;
719 SmramBase
= mCurrentSmramRange
->CpuStart
;
721 // Get PeHeader pointer
723 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((CHAR8
* )ImageContext
->Handle
+ ImageContext
->PeCoffHeaderOffset
);
724 SectionHeaderOffset
= (UINTN
)(
725 ImageContext
->PeCoffHeaderOffset
+
727 sizeof (EFI_IMAGE_FILE_HEADER
) +
728 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
730 NumberOfSections
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
733 // Get base address from the first section header that doesn't point to code section.
735 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
737 // Read section header from file
739 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
740 Status
= ImageContext
->ImageRead (
741 ImageContext
->Handle
,
746 if (EFI_ERROR (Status
)) {
750 Status
= EFI_NOT_FOUND
;
752 if ((SectionHeader
.Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
754 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
755 // first section header that doesn't point to code section in image header. And there is an assumption that when the
756 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
757 // fields should NOT be Zero, or else, these 2 fileds should be set to Zero
759 ValueInSectionHeader
= ReadUnaligned64((UINT64
*)&SectionHeader
.PointerToRelocations
);
760 if (ValueInSectionHeader
!= 0) {
762 // Found first section header that doesn't point to code section in which uild tool saves the
763 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
765 FixLoaddingAddress
= (EFI_PHYSICAL_ADDRESS
)(SmramBase
+ (INT64
)ValueInSectionHeader
);
767 if (SmramBase
+ SmmCodeSize
> FixLoaddingAddress
&& SmramBase
<= FixLoaddingAddress
) {
769 // The assigned address is valid. Return the specified loadding address
771 ImageContext
->ImageAddress
= FixLoaddingAddress
;
772 Status
= EFI_SUCCESS
;
777 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
779 DEBUG ((EFI_D_INFO
|EFI_D_LOAD
, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoaddingAddress
, Status
));
783 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
785 @param[in] SmramRange Descriptor for the range of SMRAM to reload the
786 currently executing image.
787 @param[in] Context Context to pass into SMM Core
793 ExecuteSmmCoreFromSmram (
794 IN EFI_SMRAM_DESCRIPTOR
*SmramRange
,
801 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
803 EFI_PHYSICAL_ADDRESS DestinationBuffer
;
804 EFI_IMAGE_ENTRY_POINT EntryPoint
;
807 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
809 Status
= GetSectionFromAnyFvByFileType (
810 EFI_FV_FILETYPE_SMM_CORE
,
817 if (EFI_ERROR (Status
)) {
822 // Initilize ImageContext
824 ImageContext
.Handle
= SourceBuffer
;
825 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
828 // Get information about the image being loaded
830 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
831 if (EFI_ERROR (Status
)) {
835 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
836 // the address assigned by build tool.
838 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
840 // Get the fixed loading address assigned by Build tool
842 Status
= GetPeCoffImageFixLoadingAssignedAddress (&ImageContext
);
843 if (!EFI_ERROR (Status
)) {
845 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
849 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
851 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
852 // specified by SmramRange
854 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
856 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
857 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
859 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
860 DestinationBuffer
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
863 // Align buffer on section boundry
865 ImageContext
.ImageAddress
= DestinationBuffer
;
869 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
870 // specified by SmramRange
872 PageCount
= (UINTN
)EFI_SIZE_TO_PAGES(ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
874 ASSERT ((SmramRange
->PhysicalSize
& EFI_PAGE_MASK
) == 0);
875 ASSERT (SmramRange
->PhysicalSize
> EFI_PAGES_TO_SIZE (PageCount
));
877 SmramRange
->PhysicalSize
-= EFI_PAGES_TO_SIZE (PageCount
);
878 DestinationBuffer
= SmramRange
->CpuStart
+ SmramRange
->PhysicalSize
;
881 // Align buffer on section boundry
883 ImageContext
.ImageAddress
= DestinationBuffer
;
886 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
887 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
890 // Print debug message showing SMM Core load address.
892 DEBUG ((DEBUG_INFO
, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.ImageAddress
));
895 // Load the image to our new buffer
897 Status
= PeCoffLoaderLoadImage (&ImageContext
);
898 if (!EFI_ERROR (Status
)) {
900 // Relocate the image in our new buffer
902 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
903 if (!EFI_ERROR (Status
)) {
905 // Flush the instruction cache so the image data are written before we execute it
907 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
910 // Print debug message showing SMM Core entry point address.
912 DEBUG ((DEBUG_INFO
, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID
*)(UINTN
)ImageContext
.EntryPoint
));
917 EntryPoint
= (EFI_IMAGE_ENTRY_POINT
)(UINTN
)ImageContext
.EntryPoint
;
918 Status
= EntryPoint ((EFI_HANDLE
)Context
, gST
);
923 // If the load operation, relocate operation, or the image execution return an
924 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by
927 if (EFI_ERROR (Status
)) {
928 SmramRange
->PhysicalSize
+= EFI_PAGES_TO_SIZE (PageCount
);
932 // Always free memory allocted by GetFileBufferByFilePath ()
934 FreePool (SourceBuffer
);
940 The Entry Point for SMM IPL
942 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
943 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
944 critical events required to coordinate between DXE and SMM environments.
946 @param ImageHandle The firmware allocated handle for the EFI image.
947 @param SystemTable A pointer to the EFI System Table.
949 @retval EFI_SUCCESS The entry point is executed successfully.
950 @retval Other Some error occurred when executing this entry point.
956 IN EFI_HANDLE ImageHandle
,
957 IN EFI_SYSTEM_TABLE
*SystemTable
961 EFI_SMM_CONFIGURATION_PROTOCOL
*SmmConfiguration
;
964 EFI_SMM_RESERVED_SMRAM_REGION
*SmramResRegion
;
968 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE
*LMFAConfigurationTable
;
969 EFI_CPU_ARCH_PROTOCOL
*CpuArch
;
972 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
973 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
976 mSmmCorePrivateData
.SmmIplImageHandle
= ImageHandle
;
979 // Get SMM Access Protocol
981 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&mSmmAccess
);
982 ASSERT_EFI_ERROR (Status
);
985 // Get SMM Control2 Protocol
987 Status
= gBS
->LocateProtocol (&gEfiSmmControl2ProtocolGuid
, NULL
, (VOID
**)&mSmmControl2
);
988 ASSERT_EFI_ERROR (Status
);
991 // Get SMM Configuration Protocol if it is present
993 SmmConfiguration
= NULL
;
994 Status
= gBS
->LocateProtocol (&gEfiSmmConfigurationProtocolGuid
, NULL
, (VOID
**) &SmmConfiguration
);
997 // Get SMRAM information
1000 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, NULL
);
1001 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
1003 gSmmCorePrivate
->SmramRanges
= (EFI_SMRAM_DESCRIPTOR
*)AllocatePool (Size
);
1004 ASSERT (gSmmCorePrivate
->SmramRanges
!= NULL
);
1006 Status
= mSmmAccess
->GetCapabilities (mSmmAccess
, &Size
, gSmmCorePrivate
->SmramRanges
);
1007 ASSERT_EFI_ERROR (Status
);
1009 gSmmCorePrivate
->SmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
1012 // Open all SMRAM ranges
1014 Status
= mSmmAccess
->Open (mSmmAccess
);
1015 ASSERT_EFI_ERROR (Status
);
1018 // Print debug message that the SMRAM window is now open.
1020 DEBUG ((DEBUG_INFO
, "SMM IPL opened SMRAM window\n"));
1023 // Subtract SMRAM any reserved SMRAM regions.
1025 if (SmmConfiguration
!= NULL
) {
1026 SmramResRegion
= SmmConfiguration
->SmramReservedRegions
;
1027 while (SmramResRegion
->SmramReservedSize
!= 0) {
1028 for (Index
= 0; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1029 if ((SmramResRegion
->SmramReservedStart
>= gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
) && \
1030 ((SmramResRegion
->SmramReservedStart
+ SmramResRegion
->SmramReservedSize
) <= \
1031 (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
))) {
1033 // This range has reserved area, calculate the left free size
1035 gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
= SmramResRegion
->SmramReservedStart
- gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
;
1043 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1045 mCurrentSmramRange
= NULL
;
1046 for (Index
= 0, MaxSize
= SIZE_256KB
- EFI_PAGE_SIZE
; Index
< gSmmCorePrivate
->SmramRangeCount
; Index
++) {
1047 if (gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
1048 if ((gSmmCorePrivate
->SmramRanges
[Index
].CpuStart
+ gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
) <= BASE_4GB
) {
1049 if (gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
1050 MaxSize
= gSmmCorePrivate
->SmramRanges
[Index
].PhysicalSize
;
1051 mCurrentSmramRange
= &gSmmCorePrivate
->SmramRanges
[Index
];
1057 if (mCurrentSmramRange
!= NULL
) {
1059 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1061 DEBUG ((DEBUG_INFO
, "SMM IPL found SMRAM window %p - %p\n",
1062 (VOID
*)(UINTN
)mCurrentSmramRange
->CpuStart
,
1063 (VOID
*)(UINTN
)(mCurrentSmramRange
->CpuStart
+ mCurrentSmramRange
->PhysicalSize
- 1)
1066 GetSmramCacheRange (mCurrentSmramRange
, &mSmramCacheBase
, &mSmramCacheSize
);
1068 // If CPU AP is present, attempt to set SMRAM cacheability to WB
1069 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1070 // is not available here.
1072 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&CpuArch
);
1073 if (!EFI_ERROR (Status
)) {
1074 Status
= gDS
->SetMemorySpaceAttributes(
1079 if (EFI_ERROR (Status
)) {
1080 DEBUG ((DEBUG_WARN
, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1084 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1085 // Modules At Fixed Address Configuration Table.
1087 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
1089 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1091 SmmCodeSize
= LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber
), EFI_PAGE_SHIFT
);
1093 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1095 ASSERT (mCurrentSmramRange
->PhysicalSize
> SmmCodeSize
);
1097 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1099 Status
= EfiGetSystemConfigurationTable (
1100 &gLoadFixedAddressConfigurationTableGuid
,
1101 (VOID
**) &LMFAConfigurationTable
1103 if (!EFI_ERROR (Status
) && LMFAConfigurationTable
!= NULL
) {
1104 LMFAConfigurationTable
->SmramBase
= mCurrentSmramRange
->CpuStart
;
1106 // Print the SMRAM base
1108 DEBUG ((EFI_D_INFO
, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable
->SmramBase
));
1112 // Load SMM Core into SMRAM and execute it from SMRAM
1114 Status
= ExecuteSmmCoreFromSmram (mCurrentSmramRange
, gSmmCorePrivate
);
1115 if (EFI_ERROR (Status
)) {
1117 // Print error message that the SMM Core failed to be loaded and executed.
1119 DEBUG ((DEBUG_ERROR
, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1122 // Attempt to reset SMRAM cacheability to UC
1124 Status
= gDS
->SetMemorySpaceAttributes(
1129 if (EFI_ERROR (Status
)) {
1130 DEBUG ((DEBUG_WARN
, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1135 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1137 DEBUG ((DEBUG_ERROR
, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1141 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1142 // resources, and return an error so SMM IPL will be unloaded.
1144 if (mCurrentSmramRange
== NULL
|| EFI_ERROR (Status
)) {
1146 // Close all SMRAM ranges
1148 Status
= mSmmAccess
->Close (mSmmAccess
);
1149 ASSERT_EFI_ERROR (Status
);
1152 // Print debug message that the SMRAM window is now closed.
1154 DEBUG ((DEBUG_INFO
, "SMM IPL closed SMRAM window\n"));
1157 // Free all allocated resources
1159 FreePool (gSmmCorePrivate
->SmramRanges
);
1161 return EFI_UNSUPPORTED
;
1165 // Install SMM Base2 Protocol and SMM Communication Protocol
1167 Status
= gBS
->InstallMultipleProtocolInterfaces (
1169 &gEfiSmmBase2ProtocolGuid
, &mSmmBase2
,
1170 &gEfiSmmCommunicationProtocolGuid
, &mSmmCommunication
,
1173 ASSERT_EFI_ERROR (Status
);
1176 // Create the set of protocol and event notififcations that the SMM IPL requires
1178 for (Index
= 0; mSmmIplEvents
[Index
].NotifyFunction
!= NULL
; Index
++) {
1179 if (mSmmIplEvents
[Index
].Protocol
) {
1180 mSmmIplEvents
[Index
].Event
= EfiCreateProtocolNotifyEvent (
1181 mSmmIplEvents
[Index
].Guid
,
1183 mSmmIplEvents
[Index
].NotifyFunction
,
1184 mSmmIplEvents
[Index
].NotifyContext
,
1188 Status
= gBS
->CreateEventEx (
1191 mSmmIplEvents
[Index
].NotifyFunction
,
1192 mSmmIplEvents
[Index
].NotifyContext
,
1193 mSmmIplEvents
[Index
].Guid
,
1194 &mSmmIplEvents
[Index
].Event
1196 ASSERT_EFI_ERROR (Status
);