]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
Change the minimal SMM core size from 1MB to 256KB-4K.
[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 // Don't allow call SmiManage() directly when SMRAM is closed or locked.
402 //
403 if (!mSmmAccess->OpenState || mSmmAccess->LockState) {
404 return EFI_INVALID_PARAMETER;
405 }
406
407 //
408 // Save current InSmm state and set InSmm state to TRUE
409 //
410 OldInSmm = gSmmCorePrivate->InSmm;
411 gSmmCorePrivate->InSmm = TRUE;
412
413 //
414 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.
415 //
416 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;
417 *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
418 Status = gSmmCorePrivate->Smst->SmiManage (
419 &CommunicateHeader->HeaderGuid,
420 NULL,
421 CommunicateHeader->Data,
422 CommSize
423 );
424
425 //
426 // Update CommunicationBuffer, BufferSize and ReturnStatus
427 // Communicate service finished, reset the pointer to CommBuffer to NULL
428 //
429 *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
430
431 //
432 // Restore original InSmm state
433 //
434 gSmmCorePrivate->InSmm = OldInSmm;
435
436 return (Status == EFI_WARN_INTERRUPT_SOURCE_QUIESCED) ? EFI_SUCCESS : EFI_NOT_FOUND;
437 }
438
439 /**
440 Event notification that is fired when DxeDispatch Event Group is signaled.
441
442 @param Event The Event that is being processed, not used.
443 @param Context Event Context, not used.
444
445 **/
446 VOID
447 EFIAPI
448 SmmIplGuidedEventNotify (
449 IN EFI_EVENT Event,
450 IN VOID *Context
451 )
452 {
453 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader;
454 UINTN Size;
455
456 //
457 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
458 //
459 CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
460 CommunicateHeader.MessageLength = 1;
461 CommunicateHeader.Data[0] = 0;
462
463 //
464 // Generate the Software SMI and return the result
465 //
466 Size = sizeof (CommunicateHeader);
467 SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);
468 }
469
470 /**
471 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
472
473 @param Event The Event that is being processed, not used.
474 @param Context Event Context, not used.
475
476 **/
477 VOID
478 EFIAPI
479 SmmIplSmmConfigurationEventNotify (
480 IN EFI_EVENT Event,
481 IN VOID *Context
482 )
483 {
484 EFI_STATUS Status;
485 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
486
487 //
488 // Make sure this notification is for this handler
489 //
490 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);
491 if (EFI_ERROR (Status)) {
492 return;
493 }
494
495 //
496 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
497 //
498 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);
499 ASSERT_EFI_ERROR (Status);
500
501 //
502 // Set flag to indicate that the SM< Entry Point has been registered which
503 // means that SMIs are now fully operational.
504 //
505 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;
506
507 //
508 // Print debug message showing SMM Core entry point address.
509 //
510 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));
511
512 //
513 // Attempt to reset SMRAM cacheability to UC
514 //
515 Status = gDS->SetMemorySpaceAttributes(
516 mCurrentSmramRange->CpuStart,
517 mCurrentSmramRange->PhysicalSize,
518 EFI_MEMORY_UC
519 );
520 if (EFI_ERROR (Status)) {
521 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
522 }
523
524 //
525 // Close all SMRAM ranges to protect SMRAM
526 //
527 Status = mSmmAccess->Close (mSmmAccess);
528 ASSERT_EFI_ERROR (Status);
529
530 //
531 // Print debug message that the SMRAM window is now closed.
532 //
533 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
534 }
535
536 /**
537 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
538 or if gEfiEventReadyToBootGuid is signalled.
539
540 @param Event The Event that is being processed, not used.
541 @param Context Event Context, not used.
542
543 **/
544 VOID
545 EFIAPI
546 SmmIplReadyToLockEventNotify (
547 IN EFI_EVENT Event,
548 IN VOID *Context
549 )
550 {
551 EFI_STATUS Status;
552 VOID *Interface;
553 UINTN Index;
554
555 //
556 // See if we are already locked
557 //
558 if (mSmmLocked) {
559 return;
560 }
561
562 //
563 // Make sure this notification is for this handler
564 //
565 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {
566 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);
567 if (EFI_ERROR (Status)) {
568 return;
569 }
570 } else {
571 //
572 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
573 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
574 // Print a warning on debug builds.
575 //
576 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
577 }
578
579 //
580 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
581 //
582 mSmmAccess->Lock (mSmmAccess);
583
584 //
585 // Close protocol and event notification events that do not apply after the
586 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
587 // event has been signalled.
588 //
589 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
590 if (mSmmIplEvents[Index].CloseOnLock) {
591 gBS->CloseEvent (mSmmIplEvents[Index].Event);
592 }
593 }
594
595 //
596 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
597 //
598 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);
599
600 //
601 // Print debug message that the SMRAM window is now locked.
602 //
603 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));
604
605 //
606 // Set flag so this operation will not be performed again
607 //
608 mSmmLocked = TRUE;
609 }
610
611 /**
612 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
613
614 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
615 It convers pointer to new virtual address.
616
617 @param Event Event whose notification function is being invoked.
618 @param Context Pointer to the notification function's context.
619
620 **/
621 VOID
622 EFIAPI
623 SmmIplSetVirtualAddressNotify (
624 IN EFI_EVENT Event,
625 IN VOID *Context
626 )
627 {
628 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);
629 }
630
631 /**
632 Searches all Firmware Volumes for the first file matching FileType and SectionType and returns the section data.
633
634 @param FileType FileType to search for within any of the firmware volumes in the platform.
635 @param SectionType SectionType to search for within any of the matching FileTypes in the firmware volumes in the platform.
636 @param SourceSize Return the size of the returned section data..
637
638 @retval != NULL Pointer to the allocated buffer containing the section data.
639 @retval NULL Section data was not found.
640
641 **/
642 VOID *
643 GetSectionInAnyFv (
644 IN EFI_FV_FILETYPE FileType,
645 IN EFI_SECTION_TYPE SectionType,
646 OUT UINTN *SourceSize
647 )
648 {
649 EFI_STATUS Status;
650 UINTN HandleCount;
651 EFI_HANDLE *HandleBuffer;
652 UINTN Index;
653 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
654 UINTN Key;
655 EFI_GUID NameGuid;
656 EFI_FV_FILE_ATTRIBUTES Attributes;
657 VOID *SourceBuffer;
658 UINT32 AuthenticationStatus;
659
660 HandleBuffer = NULL;
661 Status = gBS->LocateHandleBuffer (
662 ByProtocol,
663 &gEfiFirmwareVolume2ProtocolGuid,
664 NULL,
665 &HandleCount,
666 &HandleBuffer
667 );
668 if (EFI_ERROR (Status)) {
669 return NULL;
670 }
671
672 for (Index = 0; Index < HandleCount; Index++) {
673 Status = gBS->HandleProtocol (
674 HandleBuffer[Index],
675 &gEfiFirmwareVolume2ProtocolGuid,
676 (VOID **)&Fv
677 );
678 if (EFI_ERROR (Status)) {
679 continue;
680 }
681
682 //
683 // Use Firmware Volume 2 Protocol to search for a file of type FileType
684 //
685 Key = 0;
686 Status = Fv->GetNextFile (Fv, &Key, &FileType, &NameGuid, &Attributes, SourceSize);
687 if (EFI_ERROR (Status)) {
688 continue;
689 }
690
691 //
692 // Use Firmware Volume 2 Protocol to read a section of type SectionType
693 //
694 SourceBuffer = NULL;
695 Status = Fv->ReadSection (Fv, &NameGuid, SectionType, 0, &SourceBuffer, SourceSize, &AuthenticationStatus);
696 if (!EFI_ERROR (Status)) {
697 FreePool (HandleBuffer);
698 return SourceBuffer;
699 }
700 }
701
702 FreePool(HandleBuffer);
703
704 return NULL;
705 }
706
707 /**
708 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
709
710 @param[in] SmramRange Descriptor for the range of SMRAM to reload the
711 currently executing image.
712 @param[in] Context Context to pass into SMM Core
713
714 @return EFI_STATUS
715
716 **/
717 EFI_STATUS
718 ExecuteSmmCoreFromSmram (
719 IN EFI_SMRAM_DESCRIPTOR *SmramRange,
720 IN VOID *Context
721 )
722 {
723 EFI_STATUS Status;
724 VOID *SourceBuffer;
725 UINTN SourceSize;
726 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
727 UINTN PageCount;
728 EFI_PHYSICAL_ADDRESS DestinationBuffer;
729 EFI_IMAGE_ENTRY_POINT EntryPoint;
730
731 //
732 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
733 //
734 SourceBuffer = GetSectionInAnyFv (EFI_FV_FILETYPE_SMM_CORE, EFI_SECTION_PE32, &SourceSize);
735 if (SourceBuffer == NULL) {
736 return EFI_NOT_FOUND;
737 }
738
739 //
740 // Initilize ImageContext
741 //
742 ImageContext.Handle = SourceBuffer;
743 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
744
745 //
746 // Get information about the image being loaded
747 //
748 Status = PeCoffLoaderGetImageInfo (&ImageContext);
749 if (EFI_ERROR (Status)) {
750 return Status;
751 }
752
753 //
754 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
755 // specified by SmramRange
756 //
757 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);
758
759 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
760 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
761
762 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
763 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;
764
765 //
766 // Align buffer on section boundry
767 //
768 ImageContext.ImageAddress = DestinationBuffer;
769 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
770 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);
771
772 //
773 // Print debug message showing SMM Core load address.
774 //
775 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));
776
777 //
778 // Load the image to our new buffer
779 //
780 Status = PeCoffLoaderLoadImage (&ImageContext);
781 if (!EFI_ERROR (Status)) {
782 //
783 // Relocate the image in our new buffer
784 //
785 Status = PeCoffLoaderRelocateImage (&ImageContext);
786 if (!EFI_ERROR (Status)) {
787 //
788 // Flush the instruction cache so the image data are written before we execute it
789 //
790 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
791
792 //
793 // Print debug message showing SMM Core entry point address.
794 //
795 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));
796
797 //
798 // Execute image
799 //
800 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;
801 Status = EntryPoint ((EFI_HANDLE)Context, gST);
802 }
803 }
804
805 //
806 // If the load operation, relocate operation, or the image execution return an
807 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by
808 // SmramRange
809 //
810 if (EFI_ERROR (Status)) {
811 SmramRange->PhysicalSize += EFI_PAGES_TO_SIZE (PageCount);
812 }
813
814 //
815 // Always free memory allocted by GetFileBufferByFilePath ()
816 //
817 FreePool (SourceBuffer);
818
819 return Status;
820 }
821
822 /**
823 The Entry Point for SMM IPL
824
825 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
826 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
827 critical events required to coordinate between DXE and SMM environments.
828
829 @param ImageHandle The firmware allocated handle for the EFI image.
830 @param SystemTable A pointer to the EFI System Table.
831
832 @retval EFI_SUCCESS The entry point is executed successfully.
833 @retval Other Some error occurred when executing this entry point.
834
835 **/
836 EFI_STATUS
837 EFIAPI
838 SmmIplEntry (
839 IN EFI_HANDLE ImageHandle,
840 IN EFI_SYSTEM_TABLE *SystemTable
841 )
842 {
843 EFI_STATUS Status;
844 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
845 UINTN Size;
846 UINTN Index;
847 EFI_SMM_RESERVED_SMRAM_REGION *SmramResRegion;
848 UINT64 MaxSize;
849 VOID *Registration;
850
851 //
852 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
853 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
854 // by the SMM Core
855 //
856 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;
857
858 //
859 // Get SMM Access Protocol
860 //
861 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);
862 ASSERT_EFI_ERROR (Status);
863
864 //
865 // Get SMM Control2 Protocol
866 //
867 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);
868 ASSERT_EFI_ERROR (Status);
869
870 //
871 // Get SMM Configuration Protocol if it is present
872 //
873 SmmConfiguration = NULL;
874 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);
875
876 //
877 // Get SMRAM information
878 //
879 Size = 0;
880 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);
881 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
882
883 gSmmCorePrivate->SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);
884 ASSERT (gSmmCorePrivate->SmramRanges != NULL);
885
886 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, gSmmCorePrivate->SmramRanges);
887 ASSERT_EFI_ERROR (Status);
888
889 gSmmCorePrivate->SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
890
891 //
892 // Open all SMRAM ranges
893 //
894 Status = mSmmAccess->Open (mSmmAccess);
895 ASSERT_EFI_ERROR (Status);
896
897 //
898 // Print debug message that the SMRAM window is now open.
899 //
900 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));
901
902 //
903 // Subtract SMRAM any reserved SMRAM regions.
904 //
905 if (SmmConfiguration != NULL) {
906 SmramResRegion = SmmConfiguration->SmramReservedRegions;
907 while (SmramResRegion->SmramReservedSize != 0) {
908 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index ++) {
909 if ((SmramResRegion->SmramReservedStart >= gSmmCorePrivate->SmramRanges[Index].CpuStart) && \
910 ((SmramResRegion->SmramReservedStart + SmramResRegion->SmramReservedSize) <= \
911 (gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize))) {
912 //
913 // This range has reserved area, calculate the left free size
914 //
915 gSmmCorePrivate->SmramRanges[Index].PhysicalSize = SmramResRegion->SmramReservedStart - gSmmCorePrivate->SmramRanges[Index].CpuStart;
916 }
917 }
918 SmramResRegion++;
919 }
920 }
921
922 //
923 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
924 //
925 mCurrentSmramRange = NULL;
926 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
927 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {
928 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {
929 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {
930 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
931 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];
932 }
933 }
934 }
935 }
936
937 if (mCurrentSmramRange != NULL) {
938 //
939 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
940 //
941 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n",
942 (VOID *)(UINTN)mCurrentSmramRange->CpuStart,
943 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)
944 ));
945
946 //
947 // Attempt to set SMRAM cacheability to WB
948 //
949 Status = gDS->SetMemorySpaceAttributes(
950 mCurrentSmramRange->CpuStart,
951 mCurrentSmramRange->PhysicalSize,
952 EFI_MEMORY_WB
953 );
954 if (EFI_ERROR (Status)) {
955 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
956 }
957
958 //
959 // Load SMM Core into SMRAM and execute it from SMRAM
960 //
961 Status = ExecuteSmmCoreFromSmram (mCurrentSmramRange, gSmmCorePrivate);
962 if (EFI_ERROR (Status)) {
963 //
964 // Print error message that the SMM Core failed to be loaded and executed.
965 //
966 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
967
968 //
969 // Attempt to reset SMRAM cacheability to UC
970 //
971 Status = gDS->SetMemorySpaceAttributes(
972 mCurrentSmramRange->CpuStart,
973 mCurrentSmramRange->PhysicalSize,
974 EFI_MEMORY_UC
975 );
976 if (EFI_ERROR (Status)) {
977 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
978 }
979 }
980 } else {
981 //
982 // Print error message that there are not enough SMRAM resources to load the SMM Core.
983 //
984 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
985 }
986
987 //
988 // If the SMM Core could not be loaded then close SMRAM window, free allocated
989 // resources, and return an error so SMM IPL will be unloaded.
990 //
991 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {
992 //
993 // Close all SMRAM ranges
994 //
995 Status = mSmmAccess->Close (mSmmAccess);
996 ASSERT_EFI_ERROR (Status);
997
998 //
999 // Print debug message that the SMRAM window is now closed.
1000 //
1001 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
1002
1003 //
1004 // Free all allocated resources
1005 //
1006 FreePool (gSmmCorePrivate->SmramRanges);
1007
1008 return EFI_UNSUPPORTED;
1009 }
1010
1011 //
1012 // Install SMM Base2 Protocol and SMM Communication Protocol
1013 //
1014 Status = gBS->InstallMultipleProtocolInterfaces (
1015 &mSmmIplHandle,
1016 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,
1017 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,
1018 NULL
1019 );
1020 ASSERT_EFI_ERROR (Status);
1021
1022 //
1023 // Create the set of protocol and event notififcations that the SMM IPL requires
1024 //
1025 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
1026 if (mSmmIplEvents[Index].Protocol) {
1027 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (
1028 mSmmIplEvents[Index].Guid,
1029 TPL_CALLBACK,
1030 mSmmIplEvents[Index].NotifyFunction,
1031 mSmmIplEvents[Index].NotifyContext,
1032 &Registration
1033 );
1034 } else {
1035 Status = gBS->CreateEventEx (
1036 EVT_NOTIFY_SIGNAL,
1037 TPL_CALLBACK,
1038 mSmmIplEvents[Index].NotifyFunction,
1039 mSmmIplEvents[Index].NotifyContext,
1040 mSmmIplEvents[Index].Guid,
1041 &mSmmIplEvents[Index].Event
1042 );
1043 ASSERT_EFI_ERROR (Status);
1044 }
1045 }
1046
1047 return EFI_SUCCESS;
1048 }