]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
MdeModulePkg: Fix use-after-free error in InstallConfigurationTable()
[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 - 2017, 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 #include <Library/ReportStatusCodeLib.h>
42
43 #include "PiSmmCorePrivateData.h"
44
45 //
46 // Function prototypes from produced protocols
47 //
48
49 /**
50 Indicate whether the driver is currently executing in the SMM Initialization phase.
51
52 @param This The EFI_SMM_BASE2_PROTOCOL instance.
53 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
54 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
55
56 @retval EFI_INVALID_PARAMETER InSmram was NULL.
57 @retval EFI_SUCCESS The call returned successfully.
58
59 **/
60 EFI_STATUS
61 EFIAPI
62 SmmBase2InSmram (
63 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
64 OUT BOOLEAN *InSmram
65 );
66
67 /**
68 Retrieves the location of the System Management System Table (SMST).
69
70 @param This The EFI_SMM_BASE2_PROTOCOL instance.
71 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
72
73 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
74 @retval EFI_SUCCESS The memory was returned to the system.
75 @retval EFI_UNSUPPORTED Not in SMM.
76
77 **/
78 EFI_STATUS
79 EFIAPI
80 SmmBase2GetSmstLocation (
81 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
82 OUT EFI_SMM_SYSTEM_TABLE2 **Smst
83 );
84
85 /**
86 Communicates with a registered handler.
87
88 This function provides a service to send and receive messages from a registered
89 UEFI service. This function is part of the SMM Communication Protocol that may
90 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
91 after SetVirtualAddressMap().
92
93 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
94 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
95 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
96 being returned. Zero if the handler does not wish to reply with any data.
97
98 @retval EFI_SUCCESS The message was successfully posted.
99 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
100 **/
101 EFI_STATUS
102 EFIAPI
103 SmmCommunicationCommunicate (
104 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
105 IN OUT VOID *CommBuffer,
106 IN OUT UINTN *CommSize
107 );
108
109 /**
110 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
111
112 @param Event The Event that is being processed, not used.
113 @param Context Event Context, not used.
114
115 **/
116 VOID
117 EFIAPI
118 SmmIplSmmConfigurationEventNotify (
119 IN EFI_EVENT Event,
120 IN VOID *Context
121 );
122
123 /**
124 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
125 or if gEfiEventReadyToBootGuid is signalled.
126
127 @param Event The Event that is being processed, not used.
128 @param Context Event Context, not used.
129
130 **/
131 VOID
132 EFIAPI
133 SmmIplReadyToLockEventNotify (
134 IN EFI_EVENT Event,
135 IN VOID *Context
136 );
137
138 /**
139 Event notification that is fired when DxeDispatch Event Group is signaled.
140
141 @param Event The Event that is being processed, not used.
142 @param Context Event Context, not used.
143
144 **/
145 VOID
146 EFIAPI
147 SmmIplDxeDispatchEventNotify (
148 IN EFI_EVENT Event,
149 IN VOID *Context
150 );
151
152 /**
153 Event notification that is fired when a GUIDed Event Group is signaled.
154
155 @param Event The Event that is being processed, not used.
156 @param Context Event Context, not used.
157
158 **/
159 VOID
160 EFIAPI
161 SmmIplGuidedEventNotify (
162 IN EFI_EVENT Event,
163 IN VOID *Context
164 );
165
166 /**
167 Event notification that is fired when EndOfDxe Event Group is signaled.
168
169 @param Event The Event that is being processed, not used.
170 @param Context Event Context, not used.
171
172 **/
173 VOID
174 EFIAPI
175 SmmIplEndOfDxeEventNotify (
176 IN EFI_EVENT Event,
177 IN VOID *Context
178 );
179
180 /**
181 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
182
183 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
184 It convers pointer to new virtual address.
185
186 @param Event Event whose notification function is being invoked.
187 @param Context Pointer to the notification function's context.
188
189 **/
190 VOID
191 EFIAPI
192 SmmIplSetVirtualAddressNotify (
193 IN EFI_EVENT Event,
194 IN VOID *Context
195 );
196
197 //
198 // Data structure used to declare a table of protocol notifications and event
199 // notifications required by the SMM IPL
200 //
201 typedef struct {
202 BOOLEAN Protocol;
203 BOOLEAN CloseOnLock;
204 EFI_GUID *Guid;
205 EFI_EVENT_NOTIFY NotifyFunction;
206 VOID *NotifyContext;
207 EFI_TPL NotifyTpl;
208 EFI_EVENT Event;
209 } SMM_IPL_EVENT_NOTIFICATION;
210
211 //
212 // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol
213 //
214 EFI_HANDLE mSmmIplHandle = NULL;
215
216 //
217 // SMM Base 2 Protocol instance
218 //
219 EFI_SMM_BASE2_PROTOCOL mSmmBase2 = {
220 SmmBase2InSmram,
221 SmmBase2GetSmstLocation
222 };
223
224 //
225 // SMM Communication Protocol instance
226 //
227 EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication = {
228 SmmCommunicationCommunicate
229 };
230
231 //
232 // SMM Core Private Data structure that contains the data shared between
233 // the SMM IPL and the SMM Core.
234 //
235 SMM_CORE_PRIVATE_DATA mSmmCorePrivateData = {
236 SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature
237 NULL, // SmmIplImageHandle
238 0, // SmramRangeCount
239 NULL, // SmramRanges
240 NULL, // SmmEntryPoint
241 FALSE, // SmmEntryPointRegistered
242 FALSE, // InSmm
243 NULL, // Smst
244 NULL, // CommunicationBuffer
245 0, // BufferSize
246 EFI_SUCCESS // ReturnStatus
247 };
248
249 //
250 // Global pointer used to access mSmmCorePrivateData from outside and inside SMM
251 //
252 SMM_CORE_PRIVATE_DATA *gSmmCorePrivate = &mSmmCorePrivateData;
253
254 //
255 // SMM IPL global variables
256 //
257 EFI_SMM_CONTROL2_PROTOCOL *mSmmControl2;
258 EFI_SMM_ACCESS2_PROTOCOL *mSmmAccess;
259 EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange;
260 BOOLEAN mSmmLocked = FALSE;
261 BOOLEAN mEndOfDxe = FALSE;
262 EFI_PHYSICAL_ADDRESS mSmramCacheBase;
263 UINT64 mSmramCacheSize;
264
265 EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader;
266 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *mLMFAConfigurationTable = NULL;
267
268 //
269 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires
270 //
271 SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents[] = {
272 //
273 // Declare protocol notification on the SMM Configuration protocol. When this notification is established,
274 // the associated event is immediately signalled, so the notification function will be executed and the
275 // SMM Configuration Protocol will be found if it is already in the handle database.
276 //
277 { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, TPL_NOTIFY, NULL },
278 //
279 // Declare protocol notification on DxeSmmReadyToLock protocols. When this notification is established,
280 // the associated event is immediately signalled, so the notification function will be executed and the
281 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.
282 //
283 { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, TPL_CALLBACK, NULL },
284 //
285 // Declare event notification on EndOfDxe event. When this notification is established,
286 // the associated event is immediately signalled, so the notification function will be executed and the
287 // SMM End Of Dxe Protocol will be found if it is already in the handle database.
288 //
289 { FALSE, TRUE, &gEfiEndOfDxeEventGroupGuid, SmmIplGuidedEventNotify, &gEfiEndOfDxeEventGroupGuid, TPL_CALLBACK, NULL },
290 //
291 // Declare event notification on EndOfDxe event. This is used to set EndOfDxe event signaled flag.
292 //
293 { FALSE, TRUE, &gEfiEndOfDxeEventGroupGuid, SmmIplEndOfDxeEventNotify, &gEfiEndOfDxeEventGroupGuid, TPL_CALLBACK, NULL },
294 //
295 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core
296 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core
297 // if notified, so the SMM Core can dispatch SMM drivers.
298 //
299 { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplDxeDispatchEventNotify, &gEfiEventDxeDispatchGuid, TPL_CALLBACK, NULL },
300 //
301 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is
302 // used to make sure SMRAM is locked before any boot options are processed.
303 //
304 { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },
305 //
306 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform
307 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core
308 // must guarantee that it does not access any UEFI related structures outside of SMRAM.
309 // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.
310 //
311 { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, TPL_CALLBACK, NULL },
312 //
313 // Declare event notification on Exit Boot Services Event Group. This is used to inform the SMM Core
314 // to notify SMM driver that system enter exit boot services.
315 //
316 { FALSE, FALSE, &gEfiEventExitBootServicesGuid, SmmIplGuidedEventNotify, &gEfiEventExitBootServicesGuid, TPL_CALLBACK, NULL },
317 //
318 // Declare event notification on Ready To Boot Event Group. This is used to inform the SMM Core
319 // to notify SMM driver that system enter ready to boot.
320 //
321 { FALSE, FALSE, &gEfiEventReadyToBootGuid, SmmIplGuidedEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },
322 //
323 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate
324 // and mSmmControl2 from physical addresses to virtual addresses.
325 //
326 { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, TPL_CALLBACK, NULL },
327 //
328 // Terminate the table of event notifications
329 //
330 { FALSE, FALSE, NULL, NULL, NULL, TPL_CALLBACK, NULL }
331 };
332
333 /**
334 Find the maximum SMRAM cache range that covers the range specified by SmramRange.
335
336 This function searches and joins all adjacent ranges of SmramRange into a range to be cached.
337
338 @param SmramRange The SMRAM range to search from.
339 @param SmramCacheBase The returned cache range base.
340 @param SmramCacheSize The returned cache range size.
341
342 **/
343 VOID
344 GetSmramCacheRange (
345 IN EFI_SMRAM_DESCRIPTOR *SmramRange,
346 OUT EFI_PHYSICAL_ADDRESS *SmramCacheBase,
347 OUT UINT64 *SmramCacheSize
348 )
349 {
350 UINTN Index;
351 EFI_PHYSICAL_ADDRESS RangeCpuStart;
352 UINT64 RangePhysicalSize;
353 BOOLEAN FoundAjacentRange;
354
355 *SmramCacheBase = SmramRange->CpuStart;
356 *SmramCacheSize = SmramRange->PhysicalSize;
357
358 do {
359 FoundAjacentRange = FALSE;
360 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
361 RangeCpuStart = gSmmCorePrivate->SmramRanges[Index].CpuStart;
362 RangePhysicalSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
363 if (RangeCpuStart < *SmramCacheBase && *SmramCacheBase == (RangeCpuStart + RangePhysicalSize)) {
364 *SmramCacheBase = RangeCpuStart;
365 *SmramCacheSize += RangePhysicalSize;
366 FoundAjacentRange = TRUE;
367 } else if ((*SmramCacheBase + *SmramCacheSize) == RangeCpuStart && RangePhysicalSize > 0) {
368 *SmramCacheSize += RangePhysicalSize;
369 FoundAjacentRange = TRUE;
370 }
371 }
372 } while (FoundAjacentRange);
373
374 }
375
376 /**
377 Indicate whether the driver is currently executing in the SMM Initialization phase.
378
379 @param This The EFI_SMM_BASE2_PROTOCOL instance.
380 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing
381 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).
382
383 @retval EFI_INVALID_PARAMETER InSmram was NULL.
384 @retval EFI_SUCCESS The call returned successfully.
385
386 **/
387 EFI_STATUS
388 EFIAPI
389 SmmBase2InSmram (
390 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
391 OUT BOOLEAN *InSmram
392 )
393 {
394 if (InSmram == NULL) {
395 return EFI_INVALID_PARAMETER;
396 }
397
398 *InSmram = gSmmCorePrivate->InSmm;
399
400 return EFI_SUCCESS;
401 }
402
403 /**
404 Retrieves the location of the System Management System Table (SMST).
405
406 @param This The EFI_SMM_BASE2_PROTOCOL instance.
407 @param Smst On return, points to a pointer to the System Management Service Table (SMST).
408
409 @retval EFI_INVALID_PARAMETER Smst or This was invalid.
410 @retval EFI_SUCCESS The memory was returned to the system.
411 @retval EFI_UNSUPPORTED Not in SMM.
412
413 **/
414 EFI_STATUS
415 EFIAPI
416 SmmBase2GetSmstLocation (
417 IN CONST EFI_SMM_BASE2_PROTOCOL *This,
418 OUT EFI_SMM_SYSTEM_TABLE2 **Smst
419 )
420 {
421 if ((This == NULL) ||(Smst == NULL)) {
422 return EFI_INVALID_PARAMETER;
423 }
424
425 if (!gSmmCorePrivate->InSmm) {
426 return EFI_UNSUPPORTED;
427 }
428
429 *Smst = gSmmCorePrivate->Smst;
430
431 return EFI_SUCCESS;
432 }
433
434 /**
435 Communicates with a registered handler.
436
437 This function provides a service to send and receive messages from a registered
438 UEFI service. This function is part of the SMM Communication Protocol that may
439 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode
440 after SetVirtualAddressMap().
441
442 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.
443 @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.
444 @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data
445 being returned. Zero if the handler does not wish to reply with any data.
446
447 @retval EFI_SUCCESS The message was successfully posted.
448 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
449 **/
450 EFI_STATUS
451 EFIAPI
452 SmmCommunicationCommunicate (
453 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
454 IN OUT VOID *CommBuffer,
455 IN OUT UINTN *CommSize
456 )
457 {
458 EFI_STATUS Status;
459 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
460 BOOLEAN OldInSmm;
461
462 //
463 // Check parameters
464 //
465 if ((CommBuffer == NULL) || (CommSize == NULL)) {
466 return EFI_INVALID_PARAMETER;
467 }
468
469 //
470 // CommSize must hold HeaderGuid and MessageLength
471 //
472 if (*CommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {
473 return EFI_INVALID_PARAMETER;
474 }
475
476 //
477 // If not already in SMM, then generate a Software SMI
478 //
479 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {
480 //
481 // Put arguments for Software SMI in gSmmCorePrivate
482 //
483 gSmmCorePrivate->CommunicationBuffer = CommBuffer;
484 gSmmCorePrivate->BufferSize = *CommSize;
485
486 //
487 // Generate Software SMI
488 //
489 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);
490 if (EFI_ERROR (Status)) {
491 return EFI_UNSUPPORTED;
492 }
493
494 //
495 // Return status from software SMI
496 //
497 *CommSize = gSmmCorePrivate->BufferSize;
498 return gSmmCorePrivate->ReturnStatus;
499 }
500
501 //
502 // If we are in SMM, then the execution mode must be physical, which means that
503 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
504 // has been called, then a direct invocation of the Software SMI is not
505 // not allowed so return EFI_INVALID_PARAMETER.
506 //
507 if (EfiGoneVirtual()) {
508 return EFI_INVALID_PARAMETER;
509 }
510
511 //
512 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
513 //
514 if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) {
515 return EFI_INVALID_PARAMETER;
516 }
517
518 //
519 // Save current InSmm state and set InSmm state to TRUE
520 //
521 OldInSmm = gSmmCorePrivate->InSmm;
522 gSmmCorePrivate->InSmm = TRUE;
523
524 //
525 // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.
526 //
527 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;
528 *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
529 Status = gSmmCorePrivate->Smst->SmiManage (
530 &CommunicateHeader->HeaderGuid,
531 NULL,
532 CommunicateHeader->Data,
533 CommSize
534 );
535
536 //
537 // Update CommunicationBuffer, BufferSize and ReturnStatus
538 // Communicate service finished, reset the pointer to CommBuffer to NULL
539 //
540 *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
541
542 //
543 // Restore original InSmm state
544 //
545 gSmmCorePrivate->InSmm = OldInSmm;
546
547 return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
548 }
549
550 /**
551 Event notification that is fired when GUIDed Event Group is signaled.
552
553 @param Event The Event that is being processed, not used.
554 @param Context Event Context, not used.
555
556 **/
557 VOID
558 EFIAPI
559 SmmIplGuidedEventNotify (
560 IN EFI_EVENT Event,
561 IN VOID *Context
562 )
563 {
564 UINTN Size;
565
566 //
567 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
568 //
569 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
570 mCommunicateHeader.MessageLength = 1;
571 mCommunicateHeader.Data[0] = 0;
572
573 //
574 // Generate the Software SMI and return the result
575 //
576 Size = sizeof (mCommunicateHeader);
577 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);
578 }
579
580 /**
581 Event notification that is fired when EndOfDxe Event Group is signaled.
582
583 @param Event The Event that is being processed, not used.
584 @param Context Event Context, not used.
585
586 **/
587 VOID
588 EFIAPI
589 SmmIplEndOfDxeEventNotify (
590 IN EFI_EVENT Event,
591 IN VOID *Context
592 )
593 {
594 mEndOfDxe = TRUE;
595 }
596
597 /**
598 Event notification that is fired when DxeDispatch Event Group is signaled.
599
600 @param Event The Event that is being processed, not used.
601 @param Context Event Context, not used.
602
603 **/
604 VOID
605 EFIAPI
606 SmmIplDxeDispatchEventNotify (
607 IN EFI_EVENT Event,
608 IN VOID *Context
609 )
610 {
611 UINTN Size;
612 EFI_STATUS Status;
613
614 //
615 // Keep calling the SMM Core Dispatcher until there is no request to restart it.
616 //
617 while (TRUE) {
618 //
619 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
620 // Clear the buffer passed into the Software SMI. This buffer will return
621 // the status of the SMM Core Dispatcher.
622 //
623 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
624 mCommunicateHeader.MessageLength = 1;
625 mCommunicateHeader.Data[0] = 0;
626
627 //
628 // Generate the Software SMI and return the result
629 //
630 Size = sizeof (mCommunicateHeader);
631 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);
632
633 //
634 // Return if there is no request to restart the SMM Core Dispatcher
635 //
636 if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {
637 return;
638 }
639
640 //
641 // Attempt to reset SMRAM cacheability to UC
642 // Assume CPU AP is available at this time
643 //
644 Status = gDS->SetMemorySpaceAttributes(
645 mSmramCacheBase,
646 mSmramCacheSize,
647 EFI_MEMORY_UC
648 );
649 if (EFI_ERROR (Status)) {
650 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
651 }
652
653 //
654 // Close all SMRAM ranges to protect SMRAM
655 //
656 Status = mSmmAccess->Close (mSmmAccess);
657 ASSERT_EFI_ERROR (Status);
658
659 //
660 // Print debug message that the SMRAM window is now closed.
661 //
662 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
663 }
664 }
665
666 /**
667 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
668
669 @param Event The Event that is being processed, not used.
670 @param Context Event Context, not used.
671
672 **/
673 VOID
674 EFIAPI
675 SmmIplSmmConfigurationEventNotify (
676 IN EFI_EVENT Event,
677 IN VOID *Context
678 )
679 {
680 EFI_STATUS Status;
681 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
682
683 //
684 // Make sure this notification is for this handler
685 //
686 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);
687 if (EFI_ERROR (Status)) {
688 return;
689 }
690
691 //
692 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
693 //
694 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);
695 ASSERT_EFI_ERROR (Status);
696
697 //
698 // Set flag to indicate that the SMM Entry Point has been registered which
699 // means that SMIs are now fully operational.
700 //
701 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;
702
703 //
704 // Print debug message showing SMM Core entry point address.
705 //
706 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));
707 }
708
709 /**
710 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
711 or if gEfiEventReadyToBootGuid is signaled.
712
713 @param Event The Event that is being processed, not used.
714 @param Context Event Context, not used.
715
716 **/
717 VOID
718 EFIAPI
719 SmmIplReadyToLockEventNotify (
720 IN EFI_EVENT Event,
721 IN VOID *Context
722 )
723 {
724 EFI_STATUS Status;
725 VOID *Interface;
726 UINTN Index;
727
728 //
729 // See if we are already locked
730 //
731 if (mSmmLocked) {
732 return;
733 }
734
735 //
736 // Make sure this notification is for this handler
737 //
738 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {
739 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);
740 if (EFI_ERROR (Status)) {
741 return;
742 }
743 } else {
744 //
745 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
746 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
747 // Print a warning on debug builds.
748 //
749 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
750 }
751
752 if (!mEndOfDxe) {
753 DEBUG ((DEBUG_ERROR, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));
754 REPORT_STATUS_CODE (
755 EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,
756 (EFI_SOFTWARE_SMM_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)
757 );
758 ASSERT (FALSE);
759 }
760
761 //
762 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
763 //
764 mSmmAccess->Lock (mSmmAccess);
765
766 //
767 // Close protocol and event notification events that do not apply after the
768 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
769 // event has been signalled.
770 //
771 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
772 if (mSmmIplEvents[Index].CloseOnLock) {
773 gBS->CloseEvent (mSmmIplEvents[Index].Event);
774 }
775 }
776
777 //
778 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
779 //
780 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);
781
782 //
783 // Print debug message that the SMRAM window is now locked.
784 //
785 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));
786
787 //
788 // Set flag so this operation will not be performed again
789 //
790 mSmmLocked = TRUE;
791 }
792
793 /**
794 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
795
796 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
797 It convers pointer to new virtual address.
798
799 @param Event Event whose notification function is being invoked.
800 @param Context Pointer to the notification function's context.
801
802 **/
803 VOID
804 EFIAPI
805 SmmIplSetVirtualAddressNotify (
806 IN EFI_EVENT Event,
807 IN VOID *Context
808 )
809 {
810 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);
811 }
812
813 /**
814 Get the fixed loading address from image header assigned by build tool. This function only be called
815 when Loading module at Fixed address feature enabled.
816
817 @param ImageContext Pointer to the image context structure that describes the PE/COFF
818 image that needs to be examined by this function.
819 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
820 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
821 **/
822 EFI_STATUS
823 GetPeCoffImageFixLoadingAssignedAddress(
824 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
825 )
826 {
827 UINTN SectionHeaderOffset;
828 EFI_STATUS Status;
829 EFI_IMAGE_SECTION_HEADER SectionHeader;
830 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
831 EFI_PHYSICAL_ADDRESS FixLoadingAddress;
832 UINT16 Index;
833 UINTN Size;
834 UINT16 NumberOfSections;
835 EFI_PHYSICAL_ADDRESS SmramBase;
836 UINT64 SmmCodeSize;
837 UINT64 ValueInSectionHeader;
838 //
839 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
840 //
841 SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));
842
843 FixLoadingAddress = 0;
844 Status = EFI_NOT_FOUND;
845 SmramBase = mLMFAConfigurationTable->SmramBase;
846 //
847 // Get PeHeader pointer
848 //
849 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
850 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
851 sizeof (UINT32) +
852 sizeof (EFI_IMAGE_FILE_HEADER) +
853 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
854 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
855
856 //
857 // Get base address from the first section header that doesn't point to code section.
858 //
859 for (Index = 0; Index < NumberOfSections; Index++) {
860 //
861 // Read section header from file
862 //
863 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
864 Status = ImageContext->ImageRead (
865 ImageContext->Handle,
866 SectionHeaderOffset,
867 &Size,
868 &SectionHeader
869 );
870 if (EFI_ERROR (Status)) {
871 return Status;
872 }
873
874 Status = EFI_NOT_FOUND;
875
876 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
877 //
878 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
879 // first section header that doesn't point to code section in image header. And there is an assumption that when the
880 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
881 // fields should NOT be Zero, or else, these 2 fields should be set to Zero
882 //
883 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
884 if (ValueInSectionHeader != 0) {
885 //
886 // Found first section header that doesn't point to code section in which build tool saves the
887 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
888 //
889 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);
890
891 if (SmramBase + SmmCodeSize > FixLoadingAddress && SmramBase <= FixLoadingAddress) {
892 //
893 // The assigned address is valid. Return the specified loading address
894 //
895 ImageContext->ImageAddress = FixLoadingAddress;
896 Status = EFI_SUCCESS;
897 }
898 }
899 break;
900 }
901 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
902 }
903 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress, Status));
904 return Status;
905 }
906 /**
907 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
908
909 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the
910 currently executing image, the rang of SMRAM to
911 hold SMM Core will be excluded.
912 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.
913
914 @param[in] Context Context to pass into SMM Core
915
916 @return EFI_STATUS
917
918 **/
919 EFI_STATUS
920 ExecuteSmmCoreFromSmram (
921 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRange,
922 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRangeSmmCore,
923 IN VOID *Context
924 )
925 {
926 EFI_STATUS Status;
927 VOID *SourceBuffer;
928 UINTN SourceSize;
929 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
930 UINTN PageCount;
931 EFI_IMAGE_ENTRY_POINT EntryPoint;
932
933 //
934 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
935 //
936 Status = GetSectionFromAnyFvByFileType (
937 EFI_FV_FILETYPE_SMM_CORE,
938 0,
939 EFI_SECTION_PE32,
940 0,
941 &SourceBuffer,
942 &SourceSize
943 );
944 if (EFI_ERROR (Status)) {
945 return Status;
946 }
947
948 //
949 // Initilize ImageContext
950 //
951 ImageContext.Handle = SourceBuffer;
952 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
953
954 //
955 // Get information about the image being loaded
956 //
957 Status = PeCoffLoaderGetImageInfo (&ImageContext);
958 if (EFI_ERROR (Status)) {
959 return Status;
960 }
961 //
962 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
963 // the address assigned by build tool.
964 //
965 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
966 //
967 // Get the fixed loading address assigned by Build tool
968 //
969 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);
970 if (!EFI_ERROR (Status)) {
971 //
972 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
973 //
974 PageCount = 0;
975 //
976 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.
977 //
978 gSmmCorePrivate->SmramRangeCount --;
979 } else {
980 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
981 //
982 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
983 // specified by SmramRange
984 //
985 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
986
987 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
988 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
989
990 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
991 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;
992 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;
993 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;
994 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);
995
996 //
997 // Align buffer on section boundary
998 //
999 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;
1000 }
1001 } else {
1002 //
1003 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1004 // specified by SmramRange
1005 //
1006 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
1007
1008 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
1009 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
1010
1011 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
1012 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;
1013 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;
1014 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;
1015 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);
1016
1017 //
1018 // Align buffer on section boundary
1019 //
1020 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;
1021 }
1022
1023 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
1024 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);
1025
1026 //
1027 // Print debug message showing SMM Core load address.
1028 //
1029 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));
1030
1031 //
1032 // Load the image to our new buffer
1033 //
1034 Status = PeCoffLoaderLoadImage (&ImageContext);
1035 if (!EFI_ERROR (Status)) {
1036 //
1037 // Relocate the image in our new buffer
1038 //
1039 Status = PeCoffLoaderRelocateImage (&ImageContext);
1040 if (!EFI_ERROR (Status)) {
1041 //
1042 // Flush the instruction cache so the image data are written before we execute it
1043 //
1044 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
1045
1046 //
1047 // Print debug message showing SMM Core entry point address.
1048 //
1049 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));
1050
1051 gSmmCorePrivate->PiSmmCoreImageBase = ImageContext.ImageAddress;
1052 gSmmCorePrivate->PiSmmCoreImageSize = ImageContext.ImageSize;
1053 DEBUG ((DEBUG_INFO, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageBase));
1054 DEBUG ((DEBUG_INFO, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageSize));
1055
1056 gSmmCorePrivate->PiSmmCoreEntryPoint = ImageContext.EntryPoint;
1057
1058 //
1059 // Execute image
1060 //
1061 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;
1062 Status = EntryPoint ((EFI_HANDLE)Context, gST);
1063 }
1064 }
1065
1066 //
1067 // Always free memory allocted by GetFileBufferByFilePath ()
1068 //
1069 FreePool (SourceBuffer);
1070
1071 return Status;
1072 }
1073
1074 /**
1075 SMM split SMRAM entry.
1076
1077 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1078 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1079 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.
1080 @param[in, out] RangeCount Pointer to range count.
1081 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.
1082 @param[in, out] ReservedRangeCount Pointer to reserved range count.
1083 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry
1084 that no need to be split anymore.
1085 @param[in, out] FinalRangeCount Pointer to final range count.
1086
1087 **/
1088 VOID
1089 SmmSplitSmramEntry (
1090 IN OUT EFI_SMRAM_DESCRIPTOR *RangeToCompare,
1091 IN OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare,
1092 OUT EFI_SMRAM_DESCRIPTOR *Ranges,
1093 IN OUT UINTN *RangeCount,
1094 OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRanges,
1095 IN OUT UINTN *ReservedRangeCount,
1096 OUT EFI_SMRAM_DESCRIPTOR *FinalRanges,
1097 IN OUT UINTN *FinalRangeCount
1098 )
1099 {
1100 UINT64 RangeToCompareEnd;
1101 UINT64 ReservedRangeToCompareEnd;
1102
1103 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;
1104 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;
1105
1106 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&
1107 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {
1108 if (RangeToCompareEnd < ReservedRangeToCompareEnd) {
1109 //
1110 // RangeToCompare ReservedRangeToCompare
1111 // ---- ---- --------------------------------------
1112 // | | | | -> 1. ReservedRangeToCompare
1113 // ---- | | |--| --------------------------------------
1114 // | | | | | |
1115 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1116 // | | | | | | RangeToCompare->PhysicalSize = 0
1117 // ---- | | |--| --------------------------------------
1118 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount
1119 // ---- ---- --------------------------------------
1120 //
1121
1122 //
1123 // 1. Update ReservedRangeToCompare.
1124 //
1125 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;
1126 //
1127 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1128 // Zero RangeToCompare->PhysicalSize.
1129 //
1130 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;
1131 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;
1132 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1133 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompare->PhysicalSize;
1134 *FinalRangeCount += 1;
1135 RangeToCompare->PhysicalSize = 0;
1136 //
1137 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.
1138 //
1139 ReservedRanges[*ReservedRangeCount].SmramReservedStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1140 ReservedRanges[*ReservedRangeCount].SmramReservedSize = ReservedRangeToCompareEnd - RangeToCompareEnd;
1141 *ReservedRangeCount += 1;
1142 } else {
1143 //
1144 // RangeToCompare ReservedRangeToCompare
1145 // ---- ---- --------------------------------------
1146 // | | | | -> 1. ReservedRangeToCompare
1147 // ---- | | |--| --------------------------------------
1148 // | | | | | |
1149 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1150 // | | | | | |
1151 // | | ---- |--| --------------------------------------
1152 // | | | | -> 3. RangeToCompare
1153 // ---- ---- --------------------------------------
1154 //
1155
1156 //
1157 // 1. Update ReservedRangeToCompare.
1158 //
1159 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;
1160 //
1161 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1162 //
1163 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;
1164 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;
1165 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1166 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompareEnd - RangeToCompare->CpuStart;
1167 *FinalRangeCount += 1;
1168 //
1169 // 3. Update RangeToCompare.
1170 //
1171 RangeToCompare->CpuStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1172 RangeToCompare->PhysicalStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1173 RangeToCompare->PhysicalSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1174 }
1175 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&
1176 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {
1177 if (ReservedRangeToCompareEnd < RangeToCompareEnd) {
1178 //
1179 // RangeToCompare ReservedRangeToCompare
1180 // ---- ---- --------------------------------------
1181 // | | | | -> 1. RangeToCompare
1182 // | | ---- |--| --------------------------------------
1183 // | | | | | |
1184 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1185 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0
1186 // | | ---- |--| --------------------------------------
1187 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount
1188 // ---- ---- --------------------------------------
1189 //
1190
1191 //
1192 // 1. Update RangeToCompare.
1193 //
1194 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;
1195 //
1196 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1197 // ReservedRangeToCompare->SmramReservedSize = 0
1198 //
1199 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;
1200 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;
1201 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1202 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompare->SmramReservedSize;
1203 *FinalRangeCount += 1;
1204 ReservedRangeToCompare->SmramReservedSize = 0;
1205 //
1206 // 3. Update Ranges[*RangeCount] and increment *RangeCount.
1207 //
1208 Ranges[*RangeCount].CpuStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1209 Ranges[*RangeCount].PhysicalStart = FinalRanges[*FinalRangeCount - 1].PhysicalStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1210 Ranges[*RangeCount].RegionState = RangeToCompare->RegionState;
1211 Ranges[*RangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompareEnd;
1212 *RangeCount += 1;
1213 } else {
1214 //
1215 // RangeToCompare ReservedRangeToCompare
1216 // ---- ---- --------------------------------------
1217 // | | | | -> 1. RangeToCompare
1218 // | | ---- |--| --------------------------------------
1219 // | | | | | |
1220 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1221 // | | | | | |
1222 // ---- | | |--| --------------------------------------
1223 // | | | | -> 3. ReservedRangeToCompare
1224 // ---- ---- --------------------------------------
1225 //
1226
1227 //
1228 // 1. Update RangeToCompare.
1229 //
1230 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;
1231 //
1232 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1233 // ReservedRangeToCompare->SmramReservedSize = 0
1234 //
1235 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;
1236 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;
1237 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1238 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompare->SmramReservedStart;
1239 *FinalRangeCount += 1;
1240 //
1241 // 3. Update ReservedRangeToCompare.
1242 //
1243 ReservedRangeToCompare->SmramReservedStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1244 ReservedRangeToCompare->SmramReservedSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1245 }
1246 }
1247 }
1248
1249 /**
1250 Returns if SMRAM range and SMRAM reserved range are overlapped.
1251
1252 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1253 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1254
1255 @retval TRUE There is overlap.
1256 @retval FALSE There is no overlap.
1257
1258 **/
1259 BOOLEAN
1260 SmmIsSmramOverlap (
1261 IN EFI_SMRAM_DESCRIPTOR *RangeToCompare,
1262 IN EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare
1263 )
1264 {
1265 UINT64 RangeToCompareEnd;
1266 UINT64 ReservedRangeToCompareEnd;
1267
1268 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;
1269 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;
1270
1271 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&
1272 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {
1273 return TRUE;
1274 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&
1275 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {
1276 return TRUE;
1277 }
1278 return FALSE;
1279 }
1280
1281 /**
1282 Get full SMRAM ranges.
1283
1284 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from
1285 SmmConfiguration protocol, split the entries if there is overlap between them.
1286 It will also reserve one entry for SMM core.
1287
1288 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.
1289
1290 @return Pointer to full SMRAM ranges.
1291
1292 **/
1293 EFI_SMRAM_DESCRIPTOR *
1294 GetFullSmramRanges (
1295 OUT UINTN *FullSmramRangeCount
1296 )
1297 {
1298 EFI_STATUS Status;
1299 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
1300 UINTN Size;
1301 UINTN Index;
1302 UINTN Index2;
1303 EFI_SMRAM_DESCRIPTOR *FullSmramRanges;
1304 UINTN TempSmramRangeCount;
1305 UINTN AdditionSmramRangeCount;
1306 EFI_SMRAM_DESCRIPTOR *TempSmramRanges;
1307 UINTN SmramRangeCount;
1308 EFI_SMRAM_DESCRIPTOR *SmramRanges;
1309 UINTN SmramReservedCount;
1310 EFI_SMM_RESERVED_SMRAM_REGION *SmramReservedRanges;
1311 UINTN MaxCount;
1312 BOOLEAN Rescan;
1313
1314 //
1315 // Get SMM Configuration Protocol if it is present.
1316 //
1317 SmmConfiguration = NULL;
1318 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);
1319
1320 //
1321 // Get SMRAM information.
1322 //
1323 Size = 0;
1324 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);
1325 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
1326
1327 SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
1328
1329 //
1330 // Get SMRAM reserved region count.
1331 //
1332 SmramReservedCount = 0;
1333 if (SmmConfiguration != NULL) {
1334 while (SmmConfiguration->SmramReservedRegions[SmramReservedCount].SmramReservedSize != 0) {
1335 SmramReservedCount++;
1336 }
1337 }
1338
1339 //
1340 // Reserve one entry for SMM Core in the full SMRAM ranges.
1341 //
1342 AdditionSmramRangeCount = 1;
1343 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
1344 //
1345 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.
1346 //
1347 AdditionSmramRangeCount = 2;
1348 }
1349
1350 if (SmramReservedCount == 0) {
1351 //
1352 // No reserved SMRAM entry from SMM Configuration Protocol.
1353 //
1354 *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;
1355 Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);
1356 FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocateZeroPool (Size);
1357 ASSERT (FullSmramRanges != NULL);
1358
1359 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, FullSmramRanges);
1360 ASSERT_EFI_ERROR (Status);
1361
1362 return FullSmramRanges;
1363 }
1364
1365 //
1366 // Why MaxCount = X + 2 * Y?
1367 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.
1368 //
1369 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1
1370 // ---- ----
1371 // | | ---- |--|
1372 // | | | | -> | |
1373 // | | ---- |--|
1374 // ---- ----
1375 //
1376 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1
1377 // ---- ----
1378 // | | | |
1379 // | | ---- |--|
1380 // | | | | | |
1381 // |--| | | -> |--|
1382 // | | | | | |
1383 // | | ---- |--|
1384 // | | | |
1385 // ---- ----
1386 //
1387 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1
1388 // ---- ----
1389 // | | | |
1390 // | | ---- |--|
1391 // |--| | | |--|
1392 // | | | | -> | |
1393 // |--| | | |--|
1394 // | | ---- |--|
1395 // | | | |
1396 // ---- ----
1397 //
1398 // ......
1399 //
1400 MaxCount = SmramRangeCount + 2 * SmramReservedCount;
1401
1402 Size = MaxCount * sizeof (EFI_SMM_RESERVED_SMRAM_REGION);
1403 SmramReservedRanges = (EFI_SMM_RESERVED_SMRAM_REGION *) AllocatePool (Size);
1404 ASSERT (SmramReservedRanges != NULL);
1405 for (Index = 0; Index < SmramReservedCount; Index++) {
1406 CopyMem (&SmramReservedRanges[Index], &SmmConfiguration->SmramReservedRegions[Index], sizeof (EFI_SMM_RESERVED_SMRAM_REGION));
1407 }
1408
1409 Size = MaxCount * sizeof (EFI_SMRAM_DESCRIPTOR);
1410 TempSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);
1411 ASSERT (TempSmramRanges != NULL);
1412 TempSmramRangeCount = 0;
1413
1414 SmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);
1415 ASSERT (SmramRanges != NULL);
1416 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, SmramRanges);
1417 ASSERT_EFI_ERROR (Status);
1418
1419 do {
1420 Rescan = FALSE;
1421 for (Index = 0; (Index < SmramRangeCount) && !Rescan; Index++) {
1422 //
1423 // Skip zero size entry.
1424 //
1425 if (SmramRanges[Index].PhysicalSize != 0) {
1426 for (Index2 = 0; (Index2 < SmramReservedCount) && !Rescan; Index2++) {
1427 //
1428 // Skip zero size entry.
1429 //
1430 if (SmramReservedRanges[Index2].SmramReservedSize != 0) {
1431 if (SmmIsSmramOverlap (
1432 &SmramRanges[Index],
1433 &SmramReservedRanges[Index2]
1434 )) {
1435 //
1436 // There is overlap, need to split entry and then rescan.
1437 //
1438 SmmSplitSmramEntry (
1439 &SmramRanges[Index],
1440 &SmramReservedRanges[Index2],
1441 SmramRanges,
1442 &SmramRangeCount,
1443 SmramReservedRanges,
1444 &SmramReservedCount,
1445 TempSmramRanges,
1446 &TempSmramRangeCount
1447 );
1448 Rescan = TRUE;
1449 }
1450 }
1451 }
1452 if (!Rescan) {
1453 //
1454 // No any overlap, copy the entry to the temp SMRAM ranges.
1455 // Zero SmramRanges[Index].PhysicalSize = 0;
1456 //
1457 CopyMem (&TempSmramRanges[TempSmramRangeCount++], &SmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));
1458 SmramRanges[Index].PhysicalSize = 0;
1459 }
1460 }
1461 }
1462 } while (Rescan);
1463 ASSERT (TempSmramRangeCount <= MaxCount);
1464
1465 //
1466 // Sort the entries
1467 //
1468 FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));
1469 ASSERT (FullSmramRanges != NULL);
1470 *FullSmramRangeCount = 0;
1471 do {
1472 for (Index = 0; Index < TempSmramRangeCount; Index++) {
1473 if (TempSmramRanges[Index].PhysicalSize != 0) {
1474 break;
1475 }
1476 }
1477 ASSERT (Index < TempSmramRangeCount);
1478 for (Index2 = 0; Index2 < TempSmramRangeCount; Index2++) {
1479 if ((Index2 != Index) && (TempSmramRanges[Index2].PhysicalSize != 0) && (TempSmramRanges[Index2].CpuStart < TempSmramRanges[Index].CpuStart)) {
1480 Index = Index2;
1481 }
1482 }
1483 CopyMem (&FullSmramRanges[*FullSmramRangeCount], &TempSmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));
1484 *FullSmramRangeCount += 1;
1485 TempSmramRanges[Index].PhysicalSize = 0;
1486 } while (*FullSmramRangeCount < TempSmramRangeCount);
1487 ASSERT (*FullSmramRangeCount == TempSmramRangeCount);
1488 *FullSmramRangeCount += AdditionSmramRangeCount;
1489
1490 FreePool (SmramRanges);
1491 FreePool (SmramReservedRanges);
1492 FreePool (TempSmramRanges);
1493
1494 return FullSmramRanges;
1495 }
1496
1497 /**
1498 The Entry Point for SMM IPL
1499
1500 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
1501 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
1502 critical events required to coordinate between DXE and SMM environments.
1503
1504 @param ImageHandle The firmware allocated handle for the EFI image.
1505 @param SystemTable A pointer to the EFI System Table.
1506
1507 @retval EFI_SUCCESS The entry point is executed successfully.
1508 @retval Other Some error occurred when executing this entry point.
1509
1510 **/
1511 EFI_STATUS
1512 EFIAPI
1513 SmmIplEntry (
1514 IN EFI_HANDLE ImageHandle,
1515 IN EFI_SYSTEM_TABLE *SystemTable
1516 )
1517 {
1518 EFI_STATUS Status;
1519 UINTN Index;
1520 UINT64 MaxSize;
1521 VOID *Registration;
1522 UINT64 SmmCodeSize;
1523 EFI_CPU_ARCH_PROTOCOL *CpuArch;
1524 EFI_STATUS SetAttrStatus;
1525 EFI_SMRAM_DESCRIPTOR *SmramRangeSmmDriver;
1526
1527 //
1528 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
1529 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
1530 // by the SMM Core
1531 //
1532 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;
1533
1534 //
1535 // Get SMM Access Protocol
1536 //
1537 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);
1538 ASSERT_EFI_ERROR (Status);
1539
1540 //
1541 // Get SMM Control2 Protocol
1542 //
1543 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);
1544 ASSERT_EFI_ERROR (Status);
1545
1546 gSmmCorePrivate->SmramRanges = GetFullSmramRanges (&gSmmCorePrivate->SmramRangeCount);
1547
1548 //
1549 // Open all SMRAM ranges
1550 //
1551 Status = mSmmAccess->Open (mSmmAccess);
1552 ASSERT_EFI_ERROR (Status);
1553
1554 //
1555 // Print debug message that the SMRAM window is now open.
1556 //
1557 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));
1558
1559 //
1560 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1561 //
1562 mCurrentSmramRange = NULL;
1563 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
1564 //
1565 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1566 //
1567 if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
1568 continue;
1569 }
1570
1571 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {
1572 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {
1573 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {
1574 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
1575 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];
1576 }
1577 }
1578 }
1579 }
1580
1581 if (mCurrentSmramRange != NULL) {
1582 //
1583 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1584 //
1585 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n",
1586 (VOID *)(UINTN)mCurrentSmramRange->CpuStart,
1587 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)
1588 ));
1589
1590 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);
1591 //
1592 // If CPU AP is present, attempt to set SMRAM cacheability to WB
1593 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1594 // is not available here.
1595 //
1596 CpuArch = NULL;
1597 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);
1598 if (!EFI_ERROR (Status)) {
1599 Status = gDS->SetMemorySpaceAttributes(
1600 mSmramCacheBase,
1601 mSmramCacheSize,
1602 EFI_MEMORY_WB
1603 );
1604 if (EFI_ERROR (Status)) {
1605 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1606 }
1607 }
1608 //
1609 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1610 // Modules At Fixed Address Configuration Table.
1611 //
1612 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
1613 //
1614 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1615 //
1616 SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);
1617 //
1618 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1619 //
1620 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);
1621 //
1622 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1623 //
1624 Status = EfiGetSystemConfigurationTable (
1625 &gLoadFixedAddressConfigurationTableGuid,
1626 (VOID **) &mLMFAConfigurationTable
1627 );
1628 if (!EFI_ERROR (Status) && mLMFAConfigurationTable != NULL) {
1629 mLMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;
1630 //
1631 // Print the SMRAM base
1632 //
1633 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable->SmramBase));
1634 }
1635
1636 //
1637 // Fill the Smram range for all SMM code
1638 //
1639 SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];
1640 SmramRangeSmmDriver->CpuStart = mCurrentSmramRange->CpuStart;
1641 SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;
1642 SmramRangeSmmDriver->RegionState = mCurrentSmramRange->RegionState | EFI_ALLOCATED;
1643 SmramRangeSmmDriver->PhysicalSize = SmmCodeSize;
1644
1645 mCurrentSmramRange->PhysicalSize -= SmmCodeSize;
1646 mCurrentSmramRange->CpuStart = mCurrentSmramRange->CpuStart + SmmCodeSize;
1647 mCurrentSmramRange->PhysicalStart = mCurrentSmramRange->PhysicalStart + SmmCodeSize;
1648 }
1649 //
1650 // Load SMM Core into SMRAM and execute it from SMRAM
1651 //
1652 Status = ExecuteSmmCoreFromSmram (
1653 mCurrentSmramRange,
1654 &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 1],
1655 gSmmCorePrivate
1656 );
1657 if (EFI_ERROR (Status)) {
1658 //
1659 // Print error message that the SMM Core failed to be loaded and executed.
1660 //
1661 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1662
1663 //
1664 // Attempt to reset SMRAM cacheability to UC
1665 //
1666 if (CpuArch != NULL) {
1667 SetAttrStatus = gDS->SetMemorySpaceAttributes(
1668 mSmramCacheBase,
1669 mSmramCacheSize,
1670 EFI_MEMORY_UC
1671 );
1672 if (EFI_ERROR (SetAttrStatus)) {
1673 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1674 }
1675 }
1676 }
1677 } else {
1678 //
1679 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1680 //
1681 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1682 }
1683
1684 //
1685 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1686 // resources, and return an error so SMM IPL will be unloaded.
1687 //
1688 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {
1689 //
1690 // Close all SMRAM ranges
1691 //
1692 Status = mSmmAccess->Close (mSmmAccess);
1693 ASSERT_EFI_ERROR (Status);
1694
1695 //
1696 // Print debug message that the SMRAM window is now closed.
1697 //
1698 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
1699
1700 //
1701 // Free all allocated resources
1702 //
1703 FreePool (gSmmCorePrivate->SmramRanges);
1704
1705 return EFI_UNSUPPORTED;
1706 }
1707
1708 //
1709 // Install SMM Base2 Protocol and SMM Communication Protocol
1710 //
1711 Status = gBS->InstallMultipleProtocolInterfaces (
1712 &mSmmIplHandle,
1713 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,
1714 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,
1715 NULL
1716 );
1717 ASSERT_EFI_ERROR (Status);
1718
1719 //
1720 // Create the set of protocol and event notififcations that the SMM IPL requires
1721 //
1722 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
1723 if (mSmmIplEvents[Index].Protocol) {
1724 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (
1725 mSmmIplEvents[Index].Guid,
1726 mSmmIplEvents[Index].NotifyTpl,
1727 mSmmIplEvents[Index].NotifyFunction,
1728 mSmmIplEvents[Index].NotifyContext,
1729 &Registration
1730 );
1731 } else {
1732 Status = gBS->CreateEventEx (
1733 EVT_NOTIFY_SIGNAL,
1734 mSmmIplEvents[Index].NotifyTpl,
1735 mSmmIplEvents[Index].NotifyFunction,
1736 mSmmIplEvents[Index].NotifyContext,
1737 mSmmIplEvents[Index].Guid,
1738 &mSmmIplEvents[Index].Event
1739 );
1740 ASSERT_EFI_ERROR (Status);
1741 }
1742 }
1743
1744 return EFI_SUCCESS;
1745 }