]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
Update SMM Core to use SMM Mode as soon as SMM Mode is available
[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 - 2011, 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/Cpu.h>
24
25 #include <Guid/EventGroup.h>
26 #include <Guid/EventLegacyBios.h>
27 #include <Guid/LoadModuleAtFixedAddress.h>
28
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>
41
42 #include "PiSmmCorePrivateData.h"
43
44 //
45 // Function prototypes from produced protocols
46 //
47
48 /**
49 Indicate whether the driver is currently executing in the SMM Initialization phase.
50
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).
54
55 @retval EFI_INVALID_PARAMETER InSmram was NULL.
56 @retval EFI_SUCCESS The call returned successfully.
57
58 **/
59 EFI_STATUS
60 EFIAPI
61 SmmBase2InSmram (
62 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
63 OUT BOOLEAN *InSmram
64 );
65
66 /**
67 Retrieves the location of the System Management System Table (SMST).
68
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).
71
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.
75
76 **/
77 EFI_STATUS
78 EFIAPI
79 SmmBase2GetSmstLocation (
80 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
81 OUT EFI_SMM_SYSTEM_TABLE2 **Smst
82 );
83
84 /**
85 Communicates with a registered handler.
86
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().
91
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.
96
97 @retval EFI_SUCCESS The message was successfully posted.
98 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
99 **/
100 EFI_STATUS
101 EFIAPI
102 SmmCommunicationCommunicate (
103 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
104 IN OUT VOID *CommBuffer,
105 IN OUT UINTN *CommSize
106 );
107
108 /**
109 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
110
111 @param Event The Event that is being processed, not used.
112 @param Context Event Context, not used.
113
114 **/
115 VOID
116 EFIAPI
117 SmmIplSmmConfigurationEventNotify (
118 IN EFI_EVENT Event,
119 IN VOID *Context
120 );
121
122 /**
123 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
124 or if gEfiEventReadyToBootGuid is signalled.
125
126 @param Event The Event that is being processed, not used.
127 @param Context Event Context, not used.
128
129 **/
130 VOID
131 EFIAPI
132 SmmIplReadyToLockEventNotify (
133 IN EFI_EVENT Event,
134 IN VOID *Context
135 );
136
137 /**
138 Event notification that is fired when DxeDispatch Event Group is signaled.
139
140 @param Event The Event that is being processed, not used.
141 @param Context Event Context, not used.
142
143 **/
144 VOID
145 EFIAPI
146 SmmIplDxeDispatchEventNotify (
147 IN EFI_EVENT Event,
148 IN VOID *Context
149 );
150
151 /**
152 Event notification that is fired when a GUIDed Event Group is signaled.
153
154 @param Event The Event that is being processed, not used.
155 @param Context Event Context, not used.
156
157 **/
158 VOID
159 EFIAPI
160 SmmIplGuidedEventNotify (
161 IN EFI_EVENT Event,
162 IN VOID *Context
163 );
164
165 /**
166 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
167
168 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
169 It convers pointer to new virtual address.
170
171 @param Event Event whose notification function is being invoked.
172 @param Context Pointer to the notification function's context.
173
174 **/
175 VOID
176 EFIAPI
177 SmmIplSetVirtualAddressNotify (
178 IN EFI_EVENT Event,
179 IN VOID *Context
180 );
181
182 //
183 // Data structure used to declare a table of protocol notifications and event
184 // notifications required by the SMM IPL
185 //
186 typedef struct {
187 BOOLEAN Protocol;
188 BOOLEAN CloseOnLock;
189 EFI_GUID *Guid;
190 EFI_EVENT_NOTIFY NotifyFunction;
191 VOID *NotifyContext;
192 EFI_TPL NotifyTpl;
193 EFI_EVENT Event;
194 } SMM_IPL_EVENT_NOTIFICATION;
195
196 //
197 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
198 //
199 EFI_HANDLE mSmmIplHandle = NULL;
200
201 //
202 // SMM Base 2 Protocol instance
203 //
204 EFI_SMM_BASE2_PROTOCOL mSmmBase2 = {
205 SmmBase2InSmram,
206 SmmBase2GetSmstLocation
207 };
208
209 //
210 // SMM Communication Protocol instance
211 //
212 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication = {
213 SmmCommunicationCommunicate
214 };
215
216 //
217 // SMM Core Private Data structure that contains the data shared between
218 // the SMM IPL and the SMM Core.
219 //
220 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData = {
221 SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature
222 NULL, // SmmIplImageHandle
223 0, // SmramRangeCount
224 NULL, // SmramRanges
225 NULL, // SmmEntryPoint
226 FALSE, // SmmEntryPointRegistered
227 FALSE, // InSmm
228 NULL, // Smst
229 NULL, // CommunicationBuffer
230 0, // BufferSize
231 EFI_SUCCESS // ReturnStatus
232 };
233
234 //
235 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
236 //
237 SMM_CORE_PRIVATE_DATA *gSmmCorePrivate = &mSmmCorePrivateData;
238
239 //
240 // SMM IPL global variables
241 //
242 EFI_SMM_CONTROL2_PROTOCOL *mSmmControl2;
243 EFI_SMM_ACCESS2_PROTOCOL *mSmmAccess;
244 EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange;
245 BOOLEAN mSmmLocked = FALSE;
246 EFI_PHYSICAL_ADDRESS mSmramCacheBase;
247 UINT64 mSmramCacheSize;
248
249 //
250 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
251 //
252 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents[] = {
253 //
254 // Declare protocol notification on the SMM Configuration protocol. When this notification is etablished,
255 // the associated event is immediately signalled, so the notification function will be executed and the
256 // SMM Configuration Protocol will be found if it is already in the handle database.
257 //
258 { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, TPL_NOTIFY, NULL },
259 //
260 // Declare protocl notification on DxeSmmReadyToLock protocols. When this notification is etablished,
261 // the associated event is immediately signalled, so the notification function will be executed and the
262 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
263 //
264 { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, TPL_CALLBACK, NULL },
265 //
266 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
267 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
268 // if notified, so the SMM Core can dispatch SMM drivers.
269 //
270 { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplDxeDispatchEventNotify, &gEfiEventDxeDispatchGuid, TPL_CALLBACK, NULL },
271 //
272 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
273 // used to make sure SMRAM is locked before any boot options are processed.
274 //
275 { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },
276 //
277 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
278 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
279 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
280 //
281 { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, TPL_CALLBACK, NULL },
282 //
283 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
284 // and mSmmControl2 from physical addresses to virtual addresses.
285 //
286 { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, TPL_CALLBACK, NULL },
287 //
288 // Terminate the table of event notifications
289 //
290 { FALSE, FALSE, NULL, NULL, NULL, TPL_CALLBACK, NULL }
291 };
292
293 /**
294 Find the maximum SMRAM cache range that covers the range specified by SmramRange.
295
296 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
297
298 @param SmramRange The SMRAM range to search from.
299 @param SmramCacheBase The returned cache range base.
300 @param SmramCacheSize The returned cache range size.
301
302 **/
303 VOID
304 GetSmramCacheRange (
305 IN EFI_SMRAM_DESCRIPTOR *SmramRange,
306 OUT EFI_PHYSICAL_ADDRESS *SmramCacheBase,
307 OUT UINT64 *SmramCacheSize
308 )
309 {
310 UINTN Index;
311 EFI_PHYSICAL_ADDRESS RangeCpuStart;
312 UINT64 RangePhysicalSize;
313 BOOLEAN FoundAjacentRange;
314
315 *SmramCacheBase = SmramRange->CpuStart;
316 *SmramCacheSize = SmramRange->PhysicalSize;
317
318 do {
319 FoundAjacentRange = FALSE;
320 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
321 RangeCpuStart = gSmmCorePrivate->SmramRanges[Index].CpuStart;
322 RangePhysicalSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
323 if (RangeCpuStart < *SmramCacheBase && *SmramCacheBase == (RangeCpuStart + RangePhysicalSize)) {
324 *SmramCacheBase = RangeCpuStart;
325 *SmramCacheSize += RangePhysicalSize;
326 FoundAjacentRange = TRUE;
327 } else if ((*SmramCacheBase + *SmramCacheSize) == RangeCpuStart && RangePhysicalSize > 0) {
328 *SmramCacheSize += RangePhysicalSize;
329 FoundAjacentRange = TRUE;
330 }
331 }
332 } while (FoundAjacentRange);
333
334 }
335
336 /**
337 Indicate whether the driver is currently executing in the SMM Initialization phase.
338
339 @param This The EFI_SMM_BASE2_PROTOCOL instance.
340 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
341 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
342
343 @retval EFI_INVALID_PARAMETER InSmram was NULL.
344 @retval EFI_SUCCESS The call returned successfully.
345
346 **/
347 EFI_STATUS
348 EFIAPI
349 SmmBase2InSmram (
350 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
351 OUT BOOLEAN *InSmram
352 )
353 {
354 if (InSmram == NULL) {
355 return EFI_INVALID_PARAMETER;
356 }
357
358 *InSmram = gSmmCorePrivate->InSmm;
359
360 return EFI_SUCCESS;
361 }
362
363 /**
364 Retrieves the location of the System Management System Table (SMST).
365
366 @param This The EFI_SMM_BASE2_PROTOCOL instance.
367 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
368
369 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
370 @retval EFI_SUCCESS The memory was returned to the system.
371 @retval EFI_UNSUPPORTED Not in SMM.
372
373 **/
374 EFI_STATUS
375 EFIAPI
376 SmmBase2GetSmstLocation (
377 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
378 OUT EFI_SMM_SYSTEM_TABLE2 **Smst
379 )
380 {
381 if ((This == NULL) ||(Smst == NULL)) {
382 return EFI_INVALID_PARAMETER;
383 }
384
385 if (!gSmmCorePrivate->InSmm) {
386 return EFI_UNSUPPORTED;
387 }
388
389 *Smst = gSmmCorePrivate->Smst;
390
391 return EFI_SUCCESS;
392 }
393
394 /**
395 Communicates with a registered handler.
396
397 This function provides a service to send and receive messages from a registered
398 UEFI service. This function is part of the SMM Communication Protocol that may
399 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
400 after SetVirtualAddressMap().
401
402 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
403 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
404 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
405 being returned. Zero if the handler does not wish to reply with any data.
406
407 @retval EFI_SUCCESS The message was successfully posted.
408 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
409 **/
410 EFI_STATUS
411 EFIAPI
412 SmmCommunicationCommunicate (
413 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
414 IN OUT VOID *CommBuffer,
415 IN OUT UINTN *CommSize
416 )
417 {
418 EFI_STATUS Status;
419 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
420 BOOLEAN OldInSmm;
421
422 //
423 // Check parameters
424 //
425 if ((CommBuffer == NULL) || (CommSize == NULL)) {
426 return EFI_INVALID_PARAMETER;
427 }
428
429 //
430 // CommSize must hold HeaderGuid and MessageLength
431 //
432 if (*CommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {
433 return EFI_INVALID_PARAMETER;
434 }
435
436 //
437 // If not already in SMM, then generate a Software SMI
438 //
439 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {
440 //
441 // Put arguments for Software SMI in gSmmCorePrivate
442 //
443 gSmmCorePrivate->CommunicationBuffer = CommBuffer;
444 gSmmCorePrivate->BufferSize = *CommSize;
445
446 //
447 // Generate Software SMI
448 //
449 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);
450 if (EFI_ERROR (Status)) {
451 return EFI_UNSUPPORTED;
452 }
453
454 //
455 // Return status from software SMI
456 //
457 *CommSize = gSmmCorePrivate->BufferSize;
458 return gSmmCorePrivate->ReturnStatus;
459 }
460
461 //
462 // If we are in SMM, then the execution mode must be physical, which means that
463 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
464 // has been called, then a direct invocation of the Software SMI is not
465 // not allowed so return EFI_INVALID_PARAMETER.
466 //
467 if (EfiGoneVirtual()) {
468 return EFI_INVALID_PARAMETER;
469 }
470
471 //
472 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
473 //
474 if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) {
475 return EFI_INVALID_PARAMETER;
476 }
477
478 //
479 // Save current InSmm state and set InSmm state to TRUE
480 //
481 OldInSmm = gSmmCorePrivate->InSmm;
482 gSmmCorePrivate->InSmm = TRUE;
483
484 //
485 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.
486 //
487 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;
488 *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
489 Status = gSmmCorePrivate->Smst->SmiManage (
490 &CommunicateHeader->HeaderGuid,
491 NULL,
492 CommunicateHeader->Data,
493 CommSize
494 );
495
496 //
497 // Update CommunicationBuffer, BufferSize and ReturnStatus
498 // Communicate service finished, reset the pointer to CommBuffer to NULL
499 //
500 *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
501
502 //
503 // Restore original InSmm state
504 //
505 gSmmCorePrivate->InSmm = OldInSmm;
506
507 return (Status == EFI_WARN_INTERRUPT_SOURCE_QUIESCED) ? EFI_SUCCESS : EFI_NOT_FOUND;
508 }
509
510 /**
511 Event notification that is fired when GUIDed Event Group is signaled.
512
513 @param Event The Event that is being processed, not used.
514 @param Context Event Context, not used.
515
516 **/
517 VOID
518 EFIAPI
519 SmmIplGuidedEventNotify (
520 IN EFI_EVENT Event,
521 IN VOID *Context
522 )
523 {
524 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader;
525 UINTN Size;
526
527 //
528 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
529 //
530 CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
531 CommunicateHeader.MessageLength = 1;
532 CommunicateHeader.Data[0] = 0;
533
534 //
535 // Generate the Software SMI and return the result
536 //
537 Size = sizeof (CommunicateHeader);
538 SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);
539 }
540
541 /**
542 Event notification that is fired when DxeDispatch Event Group is signaled.
543
544 @param Event The Event that is being processed, not used.
545 @param Context Event Context, not used.
546
547 **/
548 VOID
549 EFIAPI
550 SmmIplDxeDispatchEventNotify (
551 IN EFI_EVENT Event,
552 IN VOID *Context
553 )
554 {
555 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader;
556 UINTN Size;
557 EFI_STATUS Status;
558
559 //
560 // Keep calling the SMM Core Dispatcher until there is no request to restart it.
561 //
562 while (TRUE) {
563 //
564 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
565 // Clear the buffer passed into the Software SMI. This buffer will return
566 // the status of the SMM Core Dispatcher.
567 //
568 CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
569 CommunicateHeader.MessageLength = 1;
570 CommunicateHeader.Data[0] = 0;
571
572 //
573 // Generate the Software SMI and return the result
574 //
575 Size = sizeof (CommunicateHeader);
576 SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);
577
578 //
579 // Return if there is no request to restart the SMM Core Dispatcher
580 //
581 if (CommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {
582 return;
583 }
584
585 //
586 // Attempt to reset SMRAM cacheability to UC
587 // Assume CPU AP is available at this time
588 //
589 Status = gDS->SetMemorySpaceAttributes(
590 mSmramCacheBase,
591 mSmramCacheSize,
592 EFI_MEMORY_UC
593 );
594 if (EFI_ERROR (Status)) {
595 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
596 }
597
598 //
599 // Close all SMRAM ranges to protect SMRAM
600 //
601 Status = mSmmAccess->Close (mSmmAccess);
602 ASSERT_EFI_ERROR (Status);
603
604 //
605 // Print debug message that the SMRAM window is now closed.
606 //
607 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
608
609 //
610 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
611 //
612 mSmmAccess->Lock (mSmmAccess);
613
614 //
615 // Print debug message that the SMRAM window is now locked
616 //
617 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));
618 }
619 }
620
621 /**
622 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
623
624 @param Event The Event that is being processed, not used.
625 @param Context Event Context, not used.
626
627 **/
628 VOID
629 EFIAPI
630 SmmIplSmmConfigurationEventNotify (
631 IN EFI_EVENT Event,
632 IN VOID *Context
633 )
634 {
635 EFI_STATUS Status;
636 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
637
638 //
639 // Make sure this notification is for this handler
640 //
641 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);
642 if (EFI_ERROR (Status)) {
643 return;
644 }
645
646 //
647 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
648 //
649 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);
650 ASSERT_EFI_ERROR (Status);
651
652 //
653 // Set flag to indicate that the SMM Entry Point has been registered which
654 // means that SMIs are now fully operational.
655 //
656 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;
657
658 //
659 // Print debug message showing SMM Core entry point address.
660 //
661 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));
662 }
663
664 /**
665 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
666 or if gEfiEventReadyToBootGuid is signalled.
667
668 @param Event The Event that is being processed, not used.
669 @param Context Event Context, not used.
670
671 **/
672 VOID
673 EFIAPI
674 SmmIplReadyToLockEventNotify (
675 IN EFI_EVENT Event,
676 IN VOID *Context
677 )
678 {
679 EFI_STATUS Status;
680 VOID *Interface;
681 UINTN Index;
682
683 //
684 // See if we are already locked
685 //
686 if (mSmmLocked) {
687 return;
688 }
689
690 //
691 // Make sure this notification is for this handler
692 //
693 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {
694 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);
695 if (EFI_ERROR (Status)) {
696 return;
697 }
698 } else {
699 //
700 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
701 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
702 // Print a warning on debug builds.
703 //
704 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
705 }
706
707 //
708 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
709 //
710 mSmmAccess->Lock (mSmmAccess);
711
712 //
713 // Close protocol and event notification events that do not apply after the
714 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
715 // event has been signalled.
716 //
717 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
718 if (mSmmIplEvents[Index].CloseOnLock) {
719 gBS->CloseEvent (mSmmIplEvents[Index].Event);
720 }
721 }
722
723 //
724 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
725 //
726 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);
727
728 //
729 // Print debug message that the SMRAM window is now locked.
730 //
731 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));
732
733 //
734 // Set flag so this operation will not be performed again
735 //
736 mSmmLocked = TRUE;
737 }
738
739 /**
740 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
741
742 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
743 It convers pointer to new virtual address.
744
745 @param Event Event whose notification function is being invoked.
746 @param Context Pointer to the notification function's context.
747
748 **/
749 VOID
750 EFIAPI
751 SmmIplSetVirtualAddressNotify (
752 IN EFI_EVENT Event,
753 IN VOID *Context
754 )
755 {
756 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);
757 }
758
759 /**
760 Get the fixed loadding address from image header assigned by build tool. This function only be called
761 when Loading module at Fixed address feature enabled.
762
763 @param ImageContext Pointer to the image context structure that describes the PE/COFF
764 image that needs to be examined by this function.
765 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
766 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
767 **/
768 EFI_STATUS
769 GetPeCoffImageFixLoadingAssignedAddress(
770 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
771 )
772 {
773 UINTN SectionHeaderOffset;
774 EFI_STATUS Status;
775 EFI_IMAGE_SECTION_HEADER SectionHeader;
776 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
777 EFI_PHYSICAL_ADDRESS FixLoaddingAddress;
778 UINT16 Index;
779 UINTN Size;
780 UINT16 NumberOfSections;
781 EFI_PHYSICAL_ADDRESS SmramBase;
782 UINT64 SmmCodeSize;
783 UINT64 ValueInSectionHeader;
784 //
785 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
786 //
787 SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));
788
789 FixLoaddingAddress = 0;
790 Status = EFI_NOT_FOUND;
791 SmramBase = mCurrentSmramRange->CpuStart;
792 //
793 // Get PeHeader pointer
794 //
795 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
796 SectionHeaderOffset = (UINTN)(
797 ImageContext->PeCoffHeaderOffset +
798 sizeof (UINT32) +
799 sizeof (EFI_IMAGE_FILE_HEADER) +
800 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader
801 );
802 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
803
804 //
805 // Get base address from the first section header that doesn't point to code section.
806 //
807 for (Index = 0; Index < NumberOfSections; Index++) {
808 //
809 // Read section header from file
810 //
811 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
812 Status = ImageContext->ImageRead (
813 ImageContext->Handle,
814 SectionHeaderOffset,
815 &Size,
816 &SectionHeader
817 );
818 if (EFI_ERROR (Status)) {
819 return Status;
820 }
821
822 Status = EFI_NOT_FOUND;
823
824 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
825 //
826 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
827 // first section header that doesn't point to code section in image header. And there is an assumption that when the
828 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
829 // fields should NOT be Zero, or else, these 2 fileds should be set to Zero
830 //
831 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
832 if (ValueInSectionHeader != 0) {
833 //
834 // Found first section header that doesn't point to code section in which uild tool saves the
835 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
836 //
837 FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);
838
839 if (SmramBase + SmmCodeSize > FixLoaddingAddress && SmramBase <= FixLoaddingAddress) {
840 //
841 // The assigned address is valid. Return the specified loadding address
842 //
843 ImageContext->ImageAddress = FixLoaddingAddress;
844 Status = EFI_SUCCESS;
845 }
846 }
847 break;
848 }
849 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
850 }
851 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoaddingAddress, Status));
852 return Status;
853 }
854 /**
855 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
856
857 @param[in] SmramRange Descriptor for the range of SMRAM to reload the
858 currently executing image.
859 @param[in] Context Context to pass into SMM Core
860
861 @return EFI_STATUS
862
863 **/
864 EFI_STATUS
865 ExecuteSmmCoreFromSmram (
866 IN EFI_SMRAM_DESCRIPTOR *SmramRange,
867 IN VOID *Context
868 )
869 {
870 EFI_STATUS Status;
871 VOID *SourceBuffer;
872 UINTN SourceSize;
873 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
874 UINTN PageCount;
875 EFI_PHYSICAL_ADDRESS DestinationBuffer;
876 EFI_IMAGE_ENTRY_POINT EntryPoint;
877
878 //
879 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
880 //
881 Status = GetSectionFromAnyFvByFileType (
882 EFI_FV_FILETYPE_SMM_CORE,
883 0,
884 EFI_SECTION_PE32,
885 0,
886 &SourceBuffer,
887 &SourceSize
888 );
889 if (EFI_ERROR (Status)) {
890 return Status;
891 }
892
893 //
894 // Initilize ImageContext
895 //
896 ImageContext.Handle = SourceBuffer;
897 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
898
899 //
900 // Get information about the image being loaded
901 //
902 Status = PeCoffLoaderGetImageInfo (&ImageContext);
903 if (EFI_ERROR (Status)) {
904 return Status;
905 }
906 //
907 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
908 // the address assigned by build tool.
909 //
910 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
911 //
912 // Get the fixed loading address assigned by Build tool
913 //
914 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);
915 if (!EFI_ERROR (Status)) {
916 //
917 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
918 //
919 PageCount = 0;
920 } else {
921 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
922 //
923 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
924 // specified by SmramRange
925 //
926 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);
927
928 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
929 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
930
931 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
932 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;
933
934 //
935 // Align buffer on section boundry
936 //
937 ImageContext.ImageAddress = DestinationBuffer;
938 }
939 } else {
940 //
941 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
942 // specified by SmramRange
943 //
944 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);
945
946 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
947 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
948
949 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
950 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;
951
952 //
953 // Align buffer on section boundry
954 //
955 ImageContext.ImageAddress = DestinationBuffer;
956 }
957
958 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
959 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);
960
961 //
962 // Print debug message showing SMM Core load address.
963 //
964 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));
965
966 //
967 // Load the image to our new buffer
968 //
969 Status = PeCoffLoaderLoadImage (&ImageContext);
970 if (!EFI_ERROR (Status)) {
971 //
972 // Relocate the image in our new buffer
973 //
974 Status = PeCoffLoaderRelocateImage (&ImageContext);
975 if (!EFI_ERROR (Status)) {
976 //
977 // Flush the instruction cache so the image data are written before we execute it
978 //
979 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
980
981 //
982 // Print debug message showing SMM Core entry point address.
983 //
984 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));
985
986 //
987 // Execute image
988 //
989 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;
990 Status = EntryPoint ((EFI_HANDLE)Context, gST);
991 }
992 }
993
994 //
995 // If the load operation, relocate operation, or the image execution return an
996 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by
997 // SmramRange
998 //
999 if (EFI_ERROR (Status)) {
1000 SmramRange->PhysicalSize += EFI_PAGES_TO_SIZE (PageCount);
1001 }
1002
1003 //
1004 // Always free memory allocted by GetFileBufferByFilePath ()
1005 //
1006 FreePool (SourceBuffer);
1007
1008 return Status;
1009 }
1010
1011 /**
1012 The Entry Point for SMM IPL
1013
1014 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
1015 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
1016 critical events required to coordinate between DXE and SMM environments.
1017
1018 @param ImageHandle The firmware allocated handle for the EFI image.
1019 @param SystemTable A pointer to the EFI System Table.
1020
1021 @retval EFI_SUCCESS The entry point is executed successfully.
1022 @retval Other Some error occurred when executing this entry point.
1023
1024 **/
1025 EFI_STATUS
1026 EFIAPI
1027 SmmIplEntry (
1028 IN EFI_HANDLE ImageHandle,
1029 IN EFI_SYSTEM_TABLE *SystemTable
1030 )
1031 {
1032 EFI_STATUS Status;
1033 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
1034 UINTN Size;
1035 UINTN Index;
1036 EFI_SMM_RESERVED_SMRAM_REGION *SmramResRegion;
1037 UINT64 MaxSize;
1038 VOID *Registration;
1039 UINT64 SmmCodeSize;
1040 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *LMFAConfigurationTable;
1041 EFI_CPU_ARCH_PROTOCOL *CpuArch;
1042
1043 //
1044 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
1045 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
1046 // by the SMM Core
1047 //
1048 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;
1049
1050 //
1051 // Get SMM Access Protocol
1052 //
1053 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);
1054 ASSERT_EFI_ERROR (Status);
1055
1056 //
1057 // Get SMM Control2 Protocol
1058 //
1059 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);
1060 ASSERT_EFI_ERROR (Status);
1061
1062 //
1063 // Get SMM Configuration Protocol if it is present
1064 //
1065 SmmConfiguration = NULL;
1066 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);
1067
1068 //
1069 // Get SMRAM information
1070 //
1071 Size = 0;
1072 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);
1073 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
1074
1075 gSmmCorePrivate->SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);
1076 ASSERT (gSmmCorePrivate->SmramRanges != NULL);
1077
1078 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, gSmmCorePrivate->SmramRanges);
1079 ASSERT_EFI_ERROR (Status);
1080
1081 gSmmCorePrivate->SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
1082
1083 //
1084 // Open all SMRAM ranges
1085 //
1086 Status = mSmmAccess->Open (mSmmAccess);
1087 ASSERT_EFI_ERROR (Status);
1088
1089 //
1090 // Print debug message that the SMRAM window is now open.
1091 //
1092 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));
1093
1094 //
1095 // Subtract SMRAM any reserved SMRAM regions.
1096 //
1097 if (SmmConfiguration != NULL) {
1098 SmramResRegion = SmmConfiguration->SmramReservedRegions;
1099 while (SmramResRegion->SmramReservedSize != 0) {
1100 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index ++) {
1101 if ((SmramResRegion->SmramReservedStart >= gSmmCorePrivate->SmramRanges[Index].CpuStart) && \
1102 ((SmramResRegion->SmramReservedStart + SmramResRegion->SmramReservedSize) <= \
1103 (gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize))) {
1104 //
1105 // This range has reserved area, calculate the left free size
1106 //
1107 gSmmCorePrivate->SmramRanges[Index].PhysicalSize = SmramResRegion->SmramReservedStart - gSmmCorePrivate->SmramRanges[Index].CpuStart;
1108 }
1109 }
1110 SmramResRegion++;
1111 }
1112 }
1113
1114 //
1115 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1116 //
1117 mCurrentSmramRange = NULL;
1118 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
1119 //
1120 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1121 //
1122 if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
1123 continue;
1124 }
1125
1126 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {
1127 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {
1128 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {
1129 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
1130 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];
1131 }
1132 }
1133 }
1134 }
1135
1136 if (mCurrentSmramRange != NULL) {
1137 //
1138 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1139 //
1140 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n",
1141 (VOID *)(UINTN)mCurrentSmramRange->CpuStart,
1142 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)
1143 ));
1144
1145 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);
1146 //
1147 // If CPU AP is present, attempt to set SMRAM cacheability to WB
1148 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1149 // is not available here.
1150 //
1151 CpuArch = NULL;
1152 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);
1153 if (!EFI_ERROR (Status)) {
1154 Status = gDS->SetMemorySpaceAttributes(
1155 mSmramCacheBase,
1156 mSmramCacheSize,
1157 EFI_MEMORY_WB
1158 );
1159 if (EFI_ERROR (Status)) {
1160 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1161 }
1162 }
1163 //
1164 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1165 // Modules At Fixed Address Configuration Table.
1166 //
1167 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
1168 //
1169 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1170 //
1171 SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);
1172 //
1173 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1174 //
1175 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);
1176 //
1177 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1178 //
1179 Status = EfiGetSystemConfigurationTable (
1180 &gLoadFixedAddressConfigurationTableGuid,
1181 (VOID **) &LMFAConfigurationTable
1182 );
1183 if (!EFI_ERROR (Status) && LMFAConfigurationTable != NULL) {
1184 LMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;
1185 //
1186 // Print the SMRAM base
1187 //
1188 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable->SmramBase));
1189 }
1190 }
1191 //
1192 // Load SMM Core into SMRAM and execute it from SMRAM
1193 //
1194 Status = ExecuteSmmCoreFromSmram (mCurrentSmramRange, gSmmCorePrivate);
1195 if (EFI_ERROR (Status)) {
1196 //
1197 // Print error message that the SMM Core failed to be loaded and executed.
1198 //
1199 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1200
1201 //
1202 // Attempt to reset SMRAM cacheability to UC
1203 //
1204 if (CpuArch != NULL) {
1205 Status = gDS->SetMemorySpaceAttributes(
1206 mSmramCacheBase,
1207 mSmramCacheSize,
1208 EFI_MEMORY_UC
1209 );
1210 if (EFI_ERROR (Status)) {
1211 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1212 }
1213 }
1214 }
1215 } else {
1216 //
1217 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1218 //
1219 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1220 }
1221
1222 //
1223 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1224 // resources, and return an error so SMM IPL will be unloaded.
1225 //
1226 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {
1227 //
1228 // Close all SMRAM ranges
1229 //
1230 Status = mSmmAccess->Close (mSmmAccess);
1231 ASSERT_EFI_ERROR (Status);
1232
1233 //
1234 // Print debug message that the SMRAM window is now closed.
1235 //
1236 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
1237
1238 //
1239 // Free all allocated resources
1240 //
1241 FreePool (gSmmCorePrivate->SmramRanges);
1242
1243 return EFI_UNSUPPORTED;
1244 }
1245
1246 //
1247 // Install SMM Base2 Protocol and SMM Communication Protocol
1248 //
1249 Status = gBS->InstallMultipleProtocolInterfaces (
1250 &mSmmIplHandle,
1251 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,
1252 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,
1253 NULL
1254 );
1255 ASSERT_EFI_ERROR (Status);
1256
1257 //
1258 // Create the set of protocol and event notififcations that the SMM IPL requires
1259 //
1260 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
1261 if (mSmmIplEvents[Index].Protocol) {
1262 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (
1263 mSmmIplEvents[Index].Guid,
1264 mSmmIplEvents[Index].NotifyTpl,
1265 mSmmIplEvents[Index].NotifyFunction,
1266 mSmmIplEvents[Index].NotifyContext,
1267 &Registration
1268 );
1269 } else {
1270 Status = gBS->CreateEventEx (
1271 EVT_NOTIFY_SIGNAL,
1272 mSmmIplEvents[Index].NotifyTpl,
1273 mSmmIplEvents[Index].NotifyFunction,
1274 mSmmIplEvents[Index].NotifyContext,
1275 mSmmIplEvents[Index].Guid,
1276 &mSmmIplEvents[Index].Event
1277 );
1278 ASSERT_EFI_ERROR (Status);
1279 }
1280 }
1281
1282 return EFI_SUCCESS;
1283 }