]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
Add PI SMM IPL and PI SMM Core
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmIpl.c
1 /** @file
2 SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM
3
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
9
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.
12
13 **/
14
15 #include <PiDxe.h>
16
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>
24
25 #include <Guid/EventGroup.h>
26 #include <Guid/EventLegacyBios.h>
27
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>
38
39 #include "PiSmmCorePrivateData.h"
40
41 //
42 // Function prototypes from produced protocols
43 //
44
45 /**
46 Indicate whether the driver is currently executing in the SMM Initialization phase.
47
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).
51
52 @retval EFI_INVALID_PARAMETER InSmram was NULL.
53 @retval EFI_SUCCESS The call returned successfully.
54
55 **/
56 EFI_STATUS
57 EFIAPI
58 SmmBase2InSmram (
59 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
60 OUT BOOLEAN *InSmram
61 );
62
63 /**
64 Retrieves the location of the System Management System Table (SMST).
65
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).
68
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.
72
73 **/
74 EFI_STATUS
75 EFIAPI
76 SmmBase2GetSmstLocation (
77 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
78 OUT EFI_SMM_SYSTEM_TABLE2 **Smst
79 );
80
81 /**
82 Communicates with a registered handler.
83
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().
88
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.
93
94 @retval EFI_SUCCESS The message was successfully posted.
95 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
96 **/
97 EFI_STATUS
98 EFIAPI
99 SmmCommunicationCommunicate (
100 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
101 IN OUT VOID *CommBuffer,
102 IN OUT UINTN *CommSize
103 );
104
105 /**
106 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
107
108 @param Event The Event that is being processed, not used.
109 @param Context Event Context, not used.
110
111 **/
112 VOID
113 EFIAPI
114 SmmIplSmmConfigurationEventNotify (
115 IN EFI_EVENT Event,
116 IN VOID *Context
117 );
118
119 /**
120 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
121 or if gEfiEventReadyToBootGuid is signalled.
122
123 @param Event The Event that is being processed, not used.
124 @param Context Event Context, not used.
125
126 **/
127 VOID
128 EFIAPI
129 SmmIplReadyToLockEventNotify (
130 IN EFI_EVENT Event,
131 IN VOID *Context
132 );
133
134 /**
135 Event notification that is fired when DxeDispatch Event Group is signaled.
136
137 @param Event The Event that is being processed, not used.
138 @param Context Event Context, not used.
139
140 **/
141 VOID
142 EFIAPI
143 SmmIplGuidedEventNotify (
144 IN EFI_EVENT Event,
145 IN VOID *Context
146 );
147
148 /**
149 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
150
151 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
152 It convers pointer to new virtual address.
153
154 @param Event Event whose notification function is being invoked.
155 @param Context Pointer to the notification function's context.
156
157 **/
158 VOID
159 EFIAPI
160 SmmIplSetVirtualAddressNotify (
161 IN EFI_EVENT Event,
162 IN VOID *Context
163 );
164
165 //
166 // Data structure used to declare a table of protocol notifications and event
167 // notifications required by the SMM IPL
168 //
169 typedef struct {
170 BOOLEAN Protocol;
171 BOOLEAN CloseOnLock;
172 EFI_GUID *Guid;
173 EFI_EVENT_NOTIFY NotifyFunction;
174 VOID *NotifyContext;
175 EFI_EVENT Event;
176 } SMM_IPL_EVENT_NOTIFICATION;
177
178 //
179 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
180 //
181 EFI_HANDLE mSmmIplHandle = NULL;
182
183 //
184 // SMM Base 2 Protocol instance
185 //
186 EFI_SMM_BASE2_PROTOCOL mSmmBase2 = {
187 SmmBase2InSmram,
188 SmmBase2GetSmstLocation
189 };
190
191 //
192 // SMM Communication Protocol instance
193 //
194 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication = {
195 SmmCommunicationCommunicate
196 };
197
198 //
199 // SMM Core Private Data structure that contains the data shared between
200 // the SMM IPL and the SMM Core.
201 //
202 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData = {
203 SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature
204 NULL, // SmmIplImageHandle
205 0, // SmramRangeCount
206 NULL, // SmramRanges
207 NULL, // SmmEntryPoint
208 FALSE, // SmmEntryPointRegistered
209 FALSE, // InSmm
210 NULL, // Smst
211 0, // BufferSize
212 NULL, // CommunicationBuffer
213 EFI_SUCCESS // ReturnStatus
214 };
215
216 //
217 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
218 //
219 SMM_CORE_PRIVATE_DATA *gSmmCorePrivate = &mSmmCorePrivateData;
220
221 //
222 // SMM IPL global variables
223 //
224 EFI_SMM_CONTROL2_PROTOCOL *mSmmControl2;
225 EFI_SMM_ACCESS2_PROTOCOL *mSmmAccess;
226 EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange;
227 BOOLEAN mSmmLocked = FALSE;
228
229 //
230 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
231 //
232 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents[] = {
233 //
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.
237 //
238 { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, NULL },
239 //
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.
243 //
244 { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, NULL },
245 //
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.
249 //
250 { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplGuidedEventNotify, &gEfiEventDxeDispatchGuid, NULL },
251 //
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.
254 //
255 { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, NULL },
256 //
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.
260 //
261 { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, NULL },
262 //
263 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
264 // and mSmmControl2 from physical addresses to virtual addresses.
265 //
266 { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, NULL },
267 //
268 // Terminate the table of event notifications
269 //
270 { FALSE, FALSE, NULL, NULL, NULL, NULL }
271 };
272
273 /**
274 Indicate whether the driver is currently executing in the SMM Initialization phase.
275
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).
279
280 @retval EFI_INVALID_PARAMETER InSmram was NULL.
281 @retval EFI_SUCCESS The call returned successfully.
282
283 **/
284 EFI_STATUS
285 EFIAPI
286 SmmBase2InSmram (
287 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
288 OUT BOOLEAN *InSmram
289 )
290 {
291 if (InSmram == NULL) {
292 return EFI_INVALID_PARAMETER;
293 }
294
295 *InSmram = gSmmCorePrivate->InSmm;
296
297 return EFI_SUCCESS;
298 }
299
300 /**
301 Retrieves the location of the System Management System Table (SMST).
302
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).
305
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.
309
310 **/
311 EFI_STATUS
312 EFIAPI
313 SmmBase2GetSmstLocation (
314 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
315 OUT EFI_SMM_SYSTEM_TABLE2 **Smst
316 )
317 {
318 if ((This == NULL) ||(Smst == NULL)) {
319 return EFI_INVALID_PARAMETER;
320 }
321
322 if (!gSmmCorePrivate->InSmm) {
323 return EFI_UNSUPPORTED;
324 }
325
326 *Smst = gSmmCorePrivate->Smst;
327
328 return EFI_SUCCESS;
329 }
330
331 /**
332 Communicates with a registered handler.
333
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().
338
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.
343
344 @retval EFI_SUCCESS The message was successfully posted.
345 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
346 **/
347 EFI_STATUS
348 EFIAPI
349 SmmCommunicationCommunicate (
350 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
351 IN OUT VOID *CommBuffer,
352 IN OUT UINTN *CommSize
353 )
354 {
355 EFI_STATUS Status;
356 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
357 BOOLEAN OldInSmm;
358
359 //
360 // Check parameters
361 //
362 if ((CommBuffer == NULL) || (CommSize == NULL)) {
363 return EFI_INVALID_PARAMETER;
364 }
365
366 //
367 // If not already in SMM, then generate a Software SMI
368 //
369 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {
370 //
371 // Put arguments for Software SMI in gSmmCorePrivate
372 //
373 gSmmCorePrivate->CommunicationBuffer = CommBuffer;
374 gSmmCorePrivate->BufferSize = CommSize;
375
376 //
377 // Generate Software SMI
378 //
379 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);
380 if (EFI_ERROR (Status)) {
381 return EFI_UNSUPPORTED;
382 }
383
384 //
385 // Return status from software SMI
386 //
387 return gSmmCorePrivate->ReturnStatus;
388 }
389
390 //
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.
395 //
396 if (EfiGoneVirtual()) {
397 return EFI_INVALID_PARAMETER;
398 }
399
400 //
401 // Save current InSmm state and set InSmm state to TRUE
402 //
403 OldInSmm = gSmmCorePrivate->InSmm;
404 gSmmCorePrivate->InSmm = TRUE;
405
406 //
407 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.
408 //
409 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;
410 *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
411 Status = gSmmCorePrivate->Smst->SmiManage (
412 &CommunicateHeader->HeaderGuid,
413 NULL,
414 CommunicateHeader->Data,
415 CommSize
416 );
417
418 //
419 // Update CommunicationBuffer, BufferSize and ReturnStatus
420 // Communicate service finished, reset the pointer to CommBuffer to NULL
421 //
422 *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
423
424 //
425 // Restore original InSmm state
426 //
427 gSmmCorePrivate->InSmm = OldInSmm;
428
429 return (Status == EFI_WARN_INTERRUPT_SOURCE_QUIESCED) ? EFI_SUCCESS : EFI_NOT_FOUND;
430 }
431
432 /**
433 Event notification that is fired when DxeDispatch Event Group is signaled.
434
435 @param Event The Event that is being processed, not used.
436 @param Context Event Context, not used.
437
438 **/
439 VOID
440 EFIAPI
441 SmmIplGuidedEventNotify (
442 IN EFI_EVENT Event,
443 IN VOID *Context
444 )
445 {
446 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader;
447 UINTN Size;
448
449 //
450 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
451 //
452 CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
453 CommunicateHeader.MessageLength = 1;
454 CommunicateHeader.Data[0] = 0;
455
456 //
457 // Generate the Software SMI and return the result
458 //
459 Size = sizeof (CommunicateHeader);
460 SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);
461 }
462
463 /**
464 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
465
466 @param Event The Event that is being processed, not used.
467 @param Context Event Context, not used.
468
469 **/
470 VOID
471 EFIAPI
472 SmmIplSmmConfigurationEventNotify (
473 IN EFI_EVENT Event,
474 IN VOID *Context
475 )
476 {
477 EFI_STATUS Status;
478 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
479
480 //
481 // Make sure this notification is for this handler
482 //
483 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);
484 if (EFI_ERROR (Status)) {
485 return;
486 }
487
488 //
489 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
490 //
491 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);
492 ASSERT_EFI_ERROR (Status);
493
494 //
495 // Set flag to indicate that the SM< Entry Point has been registered which
496 // means that SMIs are now fully operational.
497 //
498 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;
499
500 //
501 // Print debug message showing SMM Core entry point address.
502 //
503 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));
504
505 //
506 // Attempt to reset SMRAM cacheability to UC
507 //
508 Status = gDS->SetMemorySpaceAttributes(
509 mCurrentSmramRange->CpuStart,
510 mCurrentSmramRange->PhysicalSize,
511 EFI_MEMORY_UC
512 );
513 if (EFI_ERROR (Status)) {
514 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
515 }
516
517 //
518 // Close all SMRAM ranges to protect SMRAM
519 //
520 Status = mSmmAccess->Close (mSmmAccess);
521 ASSERT_EFI_ERROR (Status);
522
523 //
524 // Print debug message that the SMRAM window is now closed.
525 //
526 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
527 }
528
529 /**
530 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
531 or if gEfiEventReadyToBootGuid is signalled.
532
533 @param Event The Event that is being processed, not used.
534 @param Context Event Context, not used.
535
536 **/
537 VOID
538 EFIAPI
539 SmmIplReadyToLockEventNotify (
540 IN EFI_EVENT Event,
541 IN VOID *Context
542 )
543 {
544 EFI_STATUS Status;
545 VOID *Interface;
546 UINTN Index;
547
548 //
549 // See if we are already locked
550 //
551 if (mSmmLocked) {
552 return;
553 }
554
555 //
556 // Make sure this notification is for this handler
557 //
558 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {
559 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);
560 if (EFI_ERROR (Status)) {
561 return;
562 }
563 } else {
564 //
565 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
566 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
567 // Print a warning on debug builds.
568 //
569 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
570 }
571
572 //
573 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
574 //
575 mSmmAccess->Lock (mSmmAccess);
576
577 //
578 // Close protocol and event notification events that do not apply after the
579 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
580 // event has been signalled.
581 //
582 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
583 if (mSmmIplEvents[Index].CloseOnLock) {
584 gBS->CloseEvent (mSmmIplEvents[Index].Event);
585 }
586 }
587
588 //
589 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
590 //
591 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);
592
593 //
594 // Print debug message that the SMRAM window is now locked.
595 //
596 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));
597
598 //
599 // Set flag so this operation will not be performed again
600 //
601 mSmmLocked = TRUE;
602 }
603
604 /**
605 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
606
607 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
608 It convers pointer to new virtual address.
609
610 @param Event Event whose notification function is being invoked.
611 @param Context Pointer to the notification function's context.
612
613 **/
614 VOID
615 EFIAPI
616 SmmIplSetVirtualAddressNotify (
617 IN EFI_EVENT Event,
618 IN VOID *Context
619 )
620 {
621 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);
622 }
623
624 /**
625 Searches all Firmware Volumes for the first file matching FileType and SectionType and returns the section data.
626
627 @param FileType FileType to search for within any of the firmware volumes in the platform.
628 @param SectionType SectionType to search for within any of the matching FileTypes in the firmware volumes in the platform.
629 @param SourceSize Return the size of the returned section data..
630
631 @retval != NULL Pointer to the allocated buffer containing the section data.
632 @retval NULL Section data was not found.
633
634 **/
635 VOID *
636 GetSectionInAnyFv (
637 IN EFI_FV_FILETYPE FileType,
638 IN EFI_SECTION_TYPE SectionType,
639 OUT UINTN *SourceSize
640 )
641 {
642 EFI_STATUS Status;
643 UINTN HandleCount;
644 EFI_HANDLE *HandleBuffer;
645 UINTN Index;
646 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
647 UINTN Key;
648 EFI_GUID NameGuid;
649 EFI_FV_FILE_ATTRIBUTES Attributes;
650 VOID *SourceBuffer;
651 UINT32 AuthenticationStatus;
652
653 HandleBuffer = NULL;
654 Status = gBS->LocateHandleBuffer (
655 ByProtocol,
656 &gEfiFirmwareVolume2ProtocolGuid,
657 NULL,
658 &HandleCount,
659 &HandleBuffer
660 );
661 if (EFI_ERROR (Status)) {
662 return NULL;
663 }
664
665 for (Index = 0; Index < HandleCount; Index++) {
666 Status = gBS->HandleProtocol (
667 HandleBuffer[Index],
668 &gEfiFirmwareVolume2ProtocolGuid,
669 (VOID **)&Fv
670 );
671 if (EFI_ERROR (Status)) {
672 continue;
673 }
674
675 //
676 // Use Firmware Volume 2 Protocol to search for a file of type FileType
677 //
678 Key = 0;
679 Status = Fv->GetNextFile (Fv, &Key, &FileType, &NameGuid, &Attributes, SourceSize);
680 if (EFI_ERROR (Status)) {
681 continue;
682 }
683
684 //
685 // Use Firmware Volume 2 Protocol to read a section of type SectionType
686 //
687 SourceBuffer = NULL;
688 Status = Fv->ReadSection (Fv, &NameGuid, SectionType, 0, &SourceBuffer, SourceSize, &AuthenticationStatus);
689 if (!EFI_ERROR (Status)) {
690 FreePool (HandleBuffer);
691 return SourceBuffer;
692 }
693 }
694
695 FreePool(HandleBuffer);
696
697 return NULL;
698 }
699
700 /**
701 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
702
703 @param[in] SmramRange Descriptor for the range of SMRAM to reload the
704 currently executing image.
705 @param[in] Context Context to pass into SMM Core
706
707 @return EFI_STATUS
708
709 **/
710 EFI_STATUS
711 ExecuteSmmCoreFromSmram (
712 IN EFI_SMRAM_DESCRIPTOR *SmramRange,
713 IN VOID *Context
714 )
715 {
716 EFI_STATUS Status;
717 VOID *SourceBuffer;
718 UINTN SourceSize;
719 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
720 UINTN PageCount;
721 EFI_PHYSICAL_ADDRESS DestinationBuffer;
722 EFI_IMAGE_ENTRY_POINT EntryPoint;
723
724 //
725 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
726 //
727 SourceBuffer = GetSectionInAnyFv (EFI_FV_FILETYPE_SMM_CORE, EFI_SECTION_PE32, &SourceSize);
728 if (SourceBuffer == NULL) {
729 return EFI_NOT_FOUND;
730 }
731
732 //
733 // Initilize ImageContext
734 //
735 ImageContext.Handle = SourceBuffer;
736 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
737
738 //
739 // Get information about the image being loaded
740 //
741 Status = PeCoffLoaderGetImageInfo (&ImageContext);
742 if (EFI_ERROR (Status)) {
743 return Status;
744 }
745
746 //
747 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
748 // specified by SmramRange
749 //
750 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);
751
752 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
753 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
754
755 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
756 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;
757
758 //
759 // Align buffer on section boundry
760 //
761 ImageContext.ImageAddress = DestinationBuffer;
762 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
763 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);
764
765 //
766 // Print debug message showing SMM Core load address.
767 //
768 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));
769
770 //
771 // Load the image to our new buffer
772 //
773 Status = PeCoffLoaderLoadImage (&ImageContext);
774 if (!EFI_ERROR (Status)) {
775 //
776 // Relocate the image in our new buffer
777 //
778 Status = PeCoffLoaderRelocateImage (&ImageContext);
779 if (!EFI_ERROR (Status)) {
780 //
781 // Flush the instruction cache so the image data are written before we execute it
782 //
783 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
784
785 //
786 // Print debug message showing SMM Core entry point address.
787 //
788 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));
789
790 //
791 // Execute image
792 //
793 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;
794 Status = EntryPoint ((EFI_HANDLE)Context, gST);
795 }
796 }
797
798 //
799 // If the load operation, relocate operation, or the image execution return an
800 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by
801 // SmramRange
802 //
803 if (EFI_ERROR (Status)) {
804 SmramRange->PhysicalSize += EFI_PAGES_TO_SIZE (PageCount);
805 }
806
807 //
808 // Always free memory allocted by GetFileBufferByFilePath ()
809 //
810 FreePool (SourceBuffer);
811
812 return Status;
813 }
814
815 /**
816 The Entry Point for SMM IPL
817
818 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
819 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
820 critical events required to coordinate between DXE and SMM environments.
821
822 @param ImageHandle The firmware allocated handle for the EFI image.
823 @param SystemTable A pointer to the EFI System Table.
824
825 @retval EFI_SUCCESS The entry point is executed successfully.
826 @retval Other Some error occurred when executing this entry point.
827
828 **/
829 EFI_STATUS
830 EFIAPI
831 SmmIplEntry (
832 IN EFI_HANDLE ImageHandle,
833 IN EFI_SYSTEM_TABLE *SystemTable
834 )
835 {
836 EFI_STATUS Status;
837 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
838 UINTN Size;
839 UINTN Index;
840 EFI_SMM_RESERVED_SMRAM_REGION *SmramResRegion;
841 UINT64 MaxSize;
842 VOID *Registration;
843
844 //
845 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
846 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
847 // by the SMM Core
848 //
849 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;
850
851 //
852 // Get SMM Access Protocol
853 //
854 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);
855 ASSERT_EFI_ERROR (Status);
856
857 //
858 // Get SMM Control2 Protocol
859 //
860 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);
861 ASSERT_EFI_ERROR (Status);
862
863 //
864 // Get SMM Configuration Protocol if it is present
865 //
866 SmmConfiguration = NULL;
867 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);
868
869 //
870 // Get SMRAM information
871 //
872 Size = 0;
873 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);
874 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
875
876 gSmmCorePrivate->SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);
877 ASSERT (gSmmCorePrivate->SmramRanges != NULL);
878
879 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, gSmmCorePrivate->SmramRanges);
880 ASSERT_EFI_ERROR (Status);
881
882 gSmmCorePrivate->SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
883
884 //
885 // Open all SMRAM ranges
886 //
887 Status = mSmmAccess->Open (mSmmAccess);
888 ASSERT_EFI_ERROR (Status);
889
890 //
891 // Print debug message that the SMRAM window is now open.
892 //
893 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));
894
895 //
896 // Subtract SMRAM any reserved SMRAM regions.
897 //
898 if (SmmConfiguration != NULL) {
899 SmramResRegion = SmmConfiguration->SmramReservedRegions;
900 while (SmramResRegion->SmramReservedSize != 0) {
901 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index ++) {
902 if ((SmramResRegion->SmramReservedStart >= gSmmCorePrivate->SmramRanges[Index].CpuStart) && \
903 ((SmramResRegion->SmramReservedStart + SmramResRegion->SmramReservedSize) <= \
904 (gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize))) {
905 //
906 // This range has reserved area, calculate the left free size
907 //
908 gSmmCorePrivate->SmramRanges[Index].PhysicalSize = SmramResRegion->SmramReservedStart - gSmmCorePrivate->SmramRanges[Index].CpuStart;
909 }
910 }
911 SmramResRegion++;
912 }
913 }
914
915 //
916 // Find the largest SMRAM range between 1MB and 4GB that is at least 1MB in size
917 //
918 mCurrentSmramRange = NULL;
919 for (Index = 0, MaxSize = SIZE_1MB; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
920 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {
921 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {
922 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {
923 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
924 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];
925 }
926 }
927 }
928 }
929
930 if (mCurrentSmramRange != NULL) {
931 //
932 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
933 //
934 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n",
935 (VOID *)(UINTN)mCurrentSmramRange->CpuStart,
936 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)
937 ));
938
939 //
940 // Attempt to set SMRAM cacheability to WB
941 //
942 Status = gDS->SetMemorySpaceAttributes(
943 mCurrentSmramRange->CpuStart,
944 mCurrentSmramRange->PhysicalSize,
945 EFI_MEMORY_WB
946 );
947 if (EFI_ERROR (Status)) {
948 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
949 }
950
951 //
952 // Load SMM Core into SMRAM and execute it from SMRAM
953 //
954 Status = ExecuteSmmCoreFromSmram (mCurrentSmramRange, gSmmCorePrivate);
955 if (EFI_ERROR (Status)) {
956 //
957 // Print error message that the SMM Core failed to be loaded and executed.
958 //
959 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
960
961 //
962 // Attempt to reset SMRAM cacheability to UC
963 //
964 Status = gDS->SetMemorySpaceAttributes(
965 mCurrentSmramRange->CpuStart,
966 mCurrentSmramRange->PhysicalSize,
967 EFI_MEMORY_UC
968 );
969 if (EFI_ERROR (Status)) {
970 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
971 }
972 }
973 } else {
974 //
975 // Print error message that there are not enough SMRAM resources to load the SMM Core.
976 //
977 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
978 }
979
980 //
981 // If the SMM Core could not be loaded then close SMRAM window, free allocated
982 // resources, and return an error so SMM IPL will be unloaded.
983 //
984 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {
985 //
986 // Close all SMRAM ranges
987 //
988 Status = mSmmAccess->Close (mSmmAccess);
989 ASSERT_EFI_ERROR (Status);
990
991 //
992 // Print debug message that the SMRAM window is now closed.
993 //
994 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
995
996 //
997 // Free all allocated resources
998 //
999 FreePool (gSmmCorePrivate->SmramRanges);
1000
1001 return EFI_UNSUPPORTED;
1002 }
1003
1004 //
1005 // Install SMM Base2 Protocol and SMM Communication Protocol
1006 //
1007 Status = gBS->InstallMultipleProtocolInterfaces (
1008 &mSmmIplHandle,
1009 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,
1010 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,
1011 NULL
1012 );
1013 ASSERT_EFI_ERROR (Status);
1014
1015 //
1016 // Create the set of protocol and event notififcations that the SMM IPL requires
1017 //
1018 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
1019 if (mSmmIplEvents[Index].Protocol) {
1020 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (
1021 mSmmIplEvents[Index].Guid,
1022 TPL_CALLBACK,
1023 mSmmIplEvents[Index].NotifyFunction,
1024 mSmmIplEvents[Index].NotifyContext,
1025 &Registration
1026 );
1027 } else {
1028 Status = gBS->CreateEventEx (
1029 EVT_NOTIFY_SIGNAL,
1030 TPL_CALLBACK,
1031 mSmmIplEvents[Index].NotifyFunction,
1032 mSmmIplEvents[Index].NotifyContext,
1033 mSmmIplEvents[Index].Guid,
1034 &mSmmIplEvents[Index].Event
1035 );
1036 ASSERT_EFI_ERROR (Status);
1037 }
1038 }
1039
1040 return EFI_SUCCESS;
1041 }