]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
31d2c9e45e1f56e7483892ab5e246a3f3e9b03cb
[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 This parameter is optional and may be NULL.
447
448 @retval EFI_SUCCESS The message was successfully posted.
449 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.
450 @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation.
451 If this error is returned, the MessageLength field
452 in the CommBuffer header or the integer pointed by
453 CommSize, are updated to reflect the maximum payload
454 size the implementation can accommodate.
455 @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter,
456 if not omitted, are in address range that cannot be
457 accessed by the MM environment.
458
459 **/
460 EFI_STATUS
461 EFIAPI
462 SmmCommunicationCommunicate (
463 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,
464 IN OUT VOID *CommBuffer,
465 IN OUT UINTN *CommSize OPTIONAL
466 )
467 {
468 EFI_STATUS Status;
469 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;
470 BOOLEAN OldInSmm;
471 UINTN TempCommSize;
472
473 //
474 // Check parameters
475 //
476 if (CommBuffer == NULL) {
477 return EFI_INVALID_PARAMETER;
478 }
479
480 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;
481
482 if (CommSize == NULL) {
483 TempCommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + CommunicateHeader->MessageLength;
484 } else {
485 TempCommSize = *CommSize;
486 //
487 // CommSize must hold HeaderGuid and MessageLength
488 //
489 if (TempCommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {
490 return EFI_INVALID_PARAMETER;
491 }
492 }
493
494 //
495 // If not already in SMM, then generate a Software SMI
496 //
497 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {
498 //
499 // Put arguments for Software SMI in gSmmCorePrivate
500 //
501 gSmmCorePrivate->CommunicationBuffer = CommBuffer;
502 gSmmCorePrivate->BufferSize = TempCommSize;
503
504 //
505 // Generate Software SMI
506 //
507 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);
508 if (EFI_ERROR (Status)) {
509 return EFI_UNSUPPORTED;
510 }
511
512 //
513 // Return status from software SMI
514 //
515 if (CommSize != NULL) {
516 *CommSize = gSmmCorePrivate->BufferSize;
517 }
518 return gSmmCorePrivate->ReturnStatus;
519 }
520
521 //
522 // If we are in SMM, then the execution mode must be physical, which means that
523 // OS established virtual addresses can not be used. If SetVirtualAddressMap()
524 // has been called, then a direct invocation of the Software SMI is not allowed,
525 // so return EFI_INVALID_PARAMETER.
526 //
527 if (EfiGoneVirtual()) {
528 return EFI_INVALID_PARAMETER;
529 }
530
531 //
532 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.
533 //
534 if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) {
535 return EFI_INVALID_PARAMETER;
536 }
537
538 //
539 // Save current InSmm state and set InSmm state to TRUE
540 //
541 OldInSmm = gSmmCorePrivate->InSmm;
542 gSmmCorePrivate->InSmm = TRUE;
543
544 //
545 // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.
546 //
547 TempCommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
548 Status = gSmmCorePrivate->Smst->SmiManage (
549 &CommunicateHeader->HeaderGuid,
550 NULL,
551 CommunicateHeader->Data,
552 &TempCommSize
553 );
554 TempCommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
555 if (CommSize != NULL) {
556 *CommSize = TempCommSize;
557 }
558
559 //
560 // Restore original InSmm state
561 //
562 gSmmCorePrivate->InSmm = OldInSmm;
563
564 return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
565 }
566
567 /**
568 Event notification that is fired when GUIDed Event Group is signaled.
569
570 @param Event The Event that is being processed, not used.
571 @param Context Event Context, not used.
572
573 **/
574 VOID
575 EFIAPI
576 SmmIplGuidedEventNotify (
577 IN EFI_EVENT Event,
578 IN VOID *Context
579 )
580 {
581 UINTN Size;
582
583 //
584 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
585 //
586 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
587 mCommunicateHeader.MessageLength = 1;
588 mCommunicateHeader.Data[0] = 0;
589
590 //
591 // Generate the Software SMI and return the result
592 //
593 Size = sizeof (mCommunicateHeader);
594 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);
595 }
596
597 /**
598 Event notification that is fired when EndOfDxe 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 SmmIplEndOfDxeEventNotify (
607 IN EFI_EVENT Event,
608 IN VOID *Context
609 )
610 {
611 mEndOfDxe = TRUE;
612 }
613
614 /**
615 Event notification that is fired when DxeDispatch Event Group is signaled.
616
617 @param Event The Event that is being processed, not used.
618 @param Context Event Context, not used.
619
620 **/
621 VOID
622 EFIAPI
623 SmmIplDxeDispatchEventNotify (
624 IN EFI_EVENT Event,
625 IN VOID *Context
626 )
627 {
628 UINTN Size;
629 EFI_STATUS Status;
630
631 //
632 // Keep calling the SMM Core Dispatcher until there is no request to restart it.
633 //
634 while (TRUE) {
635 //
636 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure
637 // Clear the buffer passed into the Software SMI. This buffer will return
638 // the status of the SMM Core Dispatcher.
639 //
640 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);
641 mCommunicateHeader.MessageLength = 1;
642 mCommunicateHeader.Data[0] = 0;
643
644 //
645 // Generate the Software SMI and return the result
646 //
647 Size = sizeof (mCommunicateHeader);
648 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);
649
650 //
651 // Return if there is no request to restart the SMM Core Dispatcher
652 //
653 if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {
654 return;
655 }
656
657 //
658 // Attempt to reset SMRAM cacheability to UC
659 // Assume CPU AP is available at this time
660 //
661 Status = gDS->SetMemorySpaceAttributes(
662 mSmramCacheBase,
663 mSmramCacheSize,
664 EFI_MEMORY_UC
665 );
666 if (EFI_ERROR (Status)) {
667 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
668 }
669
670 //
671 // Close all SMRAM ranges to protect SMRAM
672 //
673 Status = mSmmAccess->Close (mSmmAccess);
674 ASSERT_EFI_ERROR (Status);
675
676 //
677 // Print debug message that the SMRAM window is now closed.
678 //
679 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
680 }
681 }
682
683 /**
684 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.
685
686 @param Event The Event that is being processed, not used.
687 @param Context Event Context, not used.
688
689 **/
690 VOID
691 EFIAPI
692 SmmIplSmmConfigurationEventNotify (
693 IN EFI_EVENT Event,
694 IN VOID *Context
695 )
696 {
697 EFI_STATUS Status;
698 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
699
700 //
701 // Make sure this notification is for this handler
702 //
703 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);
704 if (EFI_ERROR (Status)) {
705 return;
706 }
707
708 //
709 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol
710 //
711 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);
712 ASSERT_EFI_ERROR (Status);
713
714 //
715 // Set flag to indicate that the SMM Entry Point has been registered which
716 // means that SMIs are now fully operational.
717 //
718 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;
719
720 //
721 // Print debug message showing SMM Core entry point address.
722 //
723 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));
724 }
725
726 /**
727 Event notification that is fired every time a DxeSmmReadyToLock protocol is added
728 or if gEfiEventReadyToBootGuid is signaled.
729
730 @param Event The Event that is being processed, not used.
731 @param Context Event Context, not used.
732
733 **/
734 VOID
735 EFIAPI
736 SmmIplReadyToLockEventNotify (
737 IN EFI_EVENT Event,
738 IN VOID *Context
739 )
740 {
741 EFI_STATUS Status;
742 VOID *Interface;
743 UINTN Index;
744
745 //
746 // See if we are already locked
747 //
748 if (mSmmLocked) {
749 return;
750 }
751
752 //
753 // Make sure this notification is for this handler
754 //
755 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {
756 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);
757 if (EFI_ERROR (Status)) {
758 return;
759 }
760 } else {
761 //
762 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being
763 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.
764 // Print a warning on debug builds.
765 //
766 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));
767 }
768
769 if (!mEndOfDxe) {
770 DEBUG ((DEBUG_ERROR, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));
771 REPORT_STATUS_CODE (
772 EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,
773 (EFI_SOFTWARE_SMM_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)
774 );
775 ASSERT (FALSE);
776 }
777
778 //
779 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)
780 //
781 mSmmAccess->Lock (mSmmAccess);
782
783 //
784 // Close protocol and event notification events that do not apply after the
785 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot
786 // event has been signalled.
787 //
788 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
789 if (mSmmIplEvents[Index].CloseOnLock) {
790 gBS->CloseEvent (mSmmIplEvents[Index].Event);
791 }
792 }
793
794 //
795 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed
796 //
797 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);
798
799 //
800 // Print debug message that the SMRAM window is now locked.
801 //
802 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));
803
804 //
805 // Set flag so this operation will not be performed again
806 //
807 mSmmLocked = TRUE;
808 }
809
810 /**
811 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
812
813 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
814 It convers pointer to new virtual address.
815
816 @param Event Event whose notification function is being invoked.
817 @param Context Pointer to the notification function's context.
818
819 **/
820 VOID
821 EFIAPI
822 SmmIplSetVirtualAddressNotify (
823 IN EFI_EVENT Event,
824 IN VOID *Context
825 )
826 {
827 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);
828 }
829
830 /**
831 Get the fixed loading address from image header assigned by build tool. This function only be called
832 when Loading module at Fixed address feature enabled.
833
834 @param ImageContext Pointer to the image context structure that describes the PE/COFF
835 image that needs to be examined by this function.
836 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
837 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.
838 **/
839 EFI_STATUS
840 GetPeCoffImageFixLoadingAssignedAddress(
841 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
842 )
843 {
844 UINTN SectionHeaderOffset;
845 EFI_STATUS Status;
846 EFI_IMAGE_SECTION_HEADER SectionHeader;
847 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
848 EFI_PHYSICAL_ADDRESS FixLoadingAddress;
849 UINT16 Index;
850 UINTN Size;
851 UINT16 NumberOfSections;
852 EFI_PHYSICAL_ADDRESS SmramBase;
853 UINT64 SmmCodeSize;
854 UINT64 ValueInSectionHeader;
855 //
856 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
857 //
858 SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));
859
860 FixLoadingAddress = 0;
861 Status = EFI_NOT_FOUND;
862 SmramBase = mLMFAConfigurationTable->SmramBase;
863 //
864 // Get PeHeader pointer
865 //
866 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
867 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
868 sizeof (UINT32) +
869 sizeof (EFI_IMAGE_FILE_HEADER) +
870 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;
871 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
872
873 //
874 // Get base address from the first section header that doesn't point to code section.
875 //
876 for (Index = 0; Index < NumberOfSections; Index++) {
877 //
878 // Read section header from file
879 //
880 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
881 Status = ImageContext->ImageRead (
882 ImageContext->Handle,
883 SectionHeaderOffset,
884 &Size,
885 &SectionHeader
886 );
887 if (EFI_ERROR (Status)) {
888 return Status;
889 }
890
891 Status = EFI_NOT_FOUND;
892
893 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
894 //
895 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the
896 // first section header that doesn't point to code section in image header. And there is an assumption that when the
897 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers
898 // fields should NOT be Zero, or else, these 2 fields should be set to Zero
899 //
900 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
901 if (ValueInSectionHeader != 0) {
902 //
903 // Found first section header that doesn't point to code section in which build tool saves the
904 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
905 //
906 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);
907
908 if (SmramBase + SmmCodeSize > FixLoadingAddress && SmramBase <= FixLoadingAddress) {
909 //
910 // The assigned address is valid. Return the specified loading address
911 //
912 ImageContext->ImageAddress = FixLoadingAddress;
913 Status = EFI_SUCCESS;
914 }
915 }
916 break;
917 }
918 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
919 }
920 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress, Status));
921 return Status;
922 }
923 /**
924 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.
925
926 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the
927 currently executing image, the rang of SMRAM to
928 hold SMM Core will be excluded.
929 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.
930
931 @param[in] Context Context to pass into SMM Core
932
933 @return EFI_STATUS
934
935 **/
936 EFI_STATUS
937 ExecuteSmmCoreFromSmram (
938 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRange,
939 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRangeSmmCore,
940 IN VOID *Context
941 )
942 {
943 EFI_STATUS Status;
944 VOID *SourceBuffer;
945 UINTN SourceSize;
946 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
947 UINTN PageCount;
948 EFI_IMAGE_ENTRY_POINT EntryPoint;
949
950 //
951 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE
952 //
953 Status = GetSectionFromAnyFvByFileType (
954 EFI_FV_FILETYPE_SMM_CORE,
955 0,
956 EFI_SECTION_PE32,
957 0,
958 &SourceBuffer,
959 &SourceSize
960 );
961 if (EFI_ERROR (Status)) {
962 return Status;
963 }
964
965 //
966 // Initilize ImageContext
967 //
968 ImageContext.Handle = SourceBuffer;
969 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
970
971 //
972 // Get information about the image being loaded
973 //
974 Status = PeCoffLoaderGetImageInfo (&ImageContext);
975 if (EFI_ERROR (Status)) {
976 return Status;
977 }
978 //
979 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
980 // the address assigned by build tool.
981 //
982 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
983 //
984 // Get the fixed loading address assigned by Build tool
985 //
986 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);
987 if (!EFI_ERROR (Status)) {
988 //
989 // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range
990 //
991 PageCount = 0;
992 //
993 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.
994 //
995 gSmmCorePrivate->SmramRangeCount --;
996 } else {
997 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));
998 //
999 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1000 // specified by SmramRange
1001 //
1002 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
1003
1004 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
1005 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
1006
1007 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
1008 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;
1009 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;
1010 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;
1011 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);
1012
1013 //
1014 // Align buffer on section boundary
1015 //
1016 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;
1017 }
1018 } else {
1019 //
1020 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR
1021 // specified by SmramRange
1022 //
1023 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
1024
1025 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);
1026 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
1027
1028 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);
1029 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;
1030 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;
1031 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;
1032 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);
1033
1034 //
1035 // Align buffer on section boundary
1036 //
1037 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;
1038 }
1039
1040 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
1041 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);
1042
1043 //
1044 // Print debug message showing SMM Core load address.
1045 //
1046 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));
1047
1048 //
1049 // Load the image to our new buffer
1050 //
1051 Status = PeCoffLoaderLoadImage (&ImageContext);
1052 if (!EFI_ERROR (Status)) {
1053 //
1054 // Relocate the image in our new buffer
1055 //
1056 Status = PeCoffLoaderRelocateImage (&ImageContext);
1057 if (!EFI_ERROR (Status)) {
1058 //
1059 // Flush the instruction cache so the image data are written before we execute it
1060 //
1061 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
1062
1063 //
1064 // Print debug message showing SMM Core entry point address.
1065 //
1066 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));
1067
1068 gSmmCorePrivate->PiSmmCoreImageBase = ImageContext.ImageAddress;
1069 gSmmCorePrivate->PiSmmCoreImageSize = ImageContext.ImageSize;
1070 DEBUG ((DEBUG_INFO, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageBase));
1071 DEBUG ((DEBUG_INFO, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageSize));
1072
1073 gSmmCorePrivate->PiSmmCoreEntryPoint = ImageContext.EntryPoint;
1074
1075 //
1076 // Execute image
1077 //
1078 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;
1079 Status = EntryPoint ((EFI_HANDLE)Context, gST);
1080 }
1081 }
1082
1083 //
1084 // Always free memory allocted by GetFileBufferByFilePath ()
1085 //
1086 FreePool (SourceBuffer);
1087
1088 return Status;
1089 }
1090
1091 /**
1092 SMM split SMRAM entry.
1093
1094 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1095 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1096 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.
1097 @param[in, out] RangeCount Pointer to range count.
1098 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.
1099 @param[in, out] ReservedRangeCount Pointer to reserved range count.
1100 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry
1101 that no need to be split anymore.
1102 @param[in, out] FinalRangeCount Pointer to final range count.
1103
1104 **/
1105 VOID
1106 SmmSplitSmramEntry (
1107 IN OUT EFI_SMRAM_DESCRIPTOR *RangeToCompare,
1108 IN OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare,
1109 OUT EFI_SMRAM_DESCRIPTOR *Ranges,
1110 IN OUT UINTN *RangeCount,
1111 OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRanges,
1112 IN OUT UINTN *ReservedRangeCount,
1113 OUT EFI_SMRAM_DESCRIPTOR *FinalRanges,
1114 IN OUT UINTN *FinalRangeCount
1115 )
1116 {
1117 UINT64 RangeToCompareEnd;
1118 UINT64 ReservedRangeToCompareEnd;
1119
1120 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;
1121 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;
1122
1123 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&
1124 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {
1125 if (RangeToCompareEnd < ReservedRangeToCompareEnd) {
1126 //
1127 // RangeToCompare ReservedRangeToCompare
1128 // ---- ---- --------------------------------------
1129 // | | | | -> 1. ReservedRangeToCompare
1130 // ---- | | |--| --------------------------------------
1131 // | | | | | |
1132 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1133 // | | | | | | RangeToCompare->PhysicalSize = 0
1134 // ---- | | |--| --------------------------------------
1135 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount
1136 // ---- ---- --------------------------------------
1137 //
1138
1139 //
1140 // 1. Update ReservedRangeToCompare.
1141 //
1142 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;
1143 //
1144 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1145 // Zero RangeToCompare->PhysicalSize.
1146 //
1147 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;
1148 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;
1149 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1150 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompare->PhysicalSize;
1151 *FinalRangeCount += 1;
1152 RangeToCompare->PhysicalSize = 0;
1153 //
1154 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.
1155 //
1156 ReservedRanges[*ReservedRangeCount].SmramReservedStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1157 ReservedRanges[*ReservedRangeCount].SmramReservedSize = ReservedRangeToCompareEnd - RangeToCompareEnd;
1158 *ReservedRangeCount += 1;
1159 } else {
1160 //
1161 // RangeToCompare ReservedRangeToCompare
1162 // ---- ---- --------------------------------------
1163 // | | | | -> 1. ReservedRangeToCompare
1164 // ---- | | |--| --------------------------------------
1165 // | | | | | |
1166 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1167 // | | | | | |
1168 // | | ---- |--| --------------------------------------
1169 // | | | | -> 3. RangeToCompare
1170 // ---- ---- --------------------------------------
1171 //
1172
1173 //
1174 // 1. Update ReservedRangeToCompare.
1175 //
1176 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;
1177 //
1178 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1179 //
1180 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;
1181 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;
1182 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1183 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompareEnd - RangeToCompare->CpuStart;
1184 *FinalRangeCount += 1;
1185 //
1186 // 3. Update RangeToCompare.
1187 //
1188 RangeToCompare->CpuStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1189 RangeToCompare->PhysicalStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1190 RangeToCompare->PhysicalSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1191 }
1192 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&
1193 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {
1194 if (ReservedRangeToCompareEnd < RangeToCompareEnd) {
1195 //
1196 // RangeToCompare ReservedRangeToCompare
1197 // ---- ---- --------------------------------------
1198 // | | | | -> 1. RangeToCompare
1199 // | | ---- |--| --------------------------------------
1200 // | | | | | |
1201 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1202 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0
1203 // | | ---- |--| --------------------------------------
1204 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount
1205 // ---- ---- --------------------------------------
1206 //
1207
1208 //
1209 // 1. Update RangeToCompare.
1210 //
1211 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;
1212 //
1213 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1214 // ReservedRangeToCompare->SmramReservedSize = 0
1215 //
1216 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;
1217 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;
1218 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1219 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompare->SmramReservedSize;
1220 *FinalRangeCount += 1;
1221 ReservedRangeToCompare->SmramReservedSize = 0;
1222 //
1223 // 3. Update Ranges[*RangeCount] and increment *RangeCount.
1224 //
1225 Ranges[*RangeCount].CpuStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1226 Ranges[*RangeCount].PhysicalStart = FinalRanges[*FinalRangeCount - 1].PhysicalStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1227 Ranges[*RangeCount].RegionState = RangeToCompare->RegionState;
1228 Ranges[*RangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompareEnd;
1229 *RangeCount += 1;
1230 } else {
1231 //
1232 // RangeToCompare ReservedRangeToCompare
1233 // ---- ---- --------------------------------------
1234 // | | | | -> 1. RangeToCompare
1235 // | | ---- |--| --------------------------------------
1236 // | | | | | |
1237 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount
1238 // | | | | | |
1239 // ---- | | |--| --------------------------------------
1240 // | | | | -> 3. ReservedRangeToCompare
1241 // ---- ---- --------------------------------------
1242 //
1243
1244 //
1245 // 1. Update RangeToCompare.
1246 //
1247 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;
1248 //
1249 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.
1250 // ReservedRangeToCompare->SmramReservedSize = 0
1251 //
1252 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;
1253 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;
1254 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;
1255 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompare->SmramReservedStart;
1256 *FinalRangeCount += 1;
1257 //
1258 // 3. Update ReservedRangeToCompare.
1259 //
1260 ReservedRangeToCompare->SmramReservedStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1261 ReservedRangeToCompare->SmramReservedSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;
1262 }
1263 }
1264 }
1265
1266 /**
1267 Returns if SMRAM range and SMRAM reserved range are overlapped.
1268
1269 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.
1270 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.
1271
1272 @retval TRUE There is overlap.
1273 @retval FALSE There is no overlap.
1274
1275 **/
1276 BOOLEAN
1277 SmmIsSmramOverlap (
1278 IN EFI_SMRAM_DESCRIPTOR *RangeToCompare,
1279 IN EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare
1280 )
1281 {
1282 UINT64 RangeToCompareEnd;
1283 UINT64 ReservedRangeToCompareEnd;
1284
1285 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;
1286 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;
1287
1288 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&
1289 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {
1290 return TRUE;
1291 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&
1292 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {
1293 return TRUE;
1294 }
1295 return FALSE;
1296 }
1297
1298 /**
1299 Get full SMRAM ranges.
1300
1301 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from
1302 SmmConfiguration protocol, split the entries if there is overlap between them.
1303 It will also reserve one entry for SMM core.
1304
1305 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.
1306
1307 @return Pointer to full SMRAM ranges.
1308
1309 **/
1310 EFI_SMRAM_DESCRIPTOR *
1311 GetFullSmramRanges (
1312 OUT UINTN *FullSmramRangeCount
1313 )
1314 {
1315 EFI_STATUS Status;
1316 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;
1317 UINTN Size;
1318 UINTN Index;
1319 UINTN Index2;
1320 EFI_SMRAM_DESCRIPTOR *FullSmramRanges;
1321 UINTN TempSmramRangeCount;
1322 UINTN AdditionSmramRangeCount;
1323 EFI_SMRAM_DESCRIPTOR *TempSmramRanges;
1324 UINTN SmramRangeCount;
1325 EFI_SMRAM_DESCRIPTOR *SmramRanges;
1326 UINTN SmramReservedCount;
1327 EFI_SMM_RESERVED_SMRAM_REGION *SmramReservedRanges;
1328 UINTN MaxCount;
1329 BOOLEAN Rescan;
1330
1331 //
1332 // Get SMM Configuration Protocol if it is present.
1333 //
1334 SmmConfiguration = NULL;
1335 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);
1336
1337 //
1338 // Get SMRAM information.
1339 //
1340 Size = 0;
1341 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);
1342 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
1343
1344 SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
1345
1346 //
1347 // Get SMRAM reserved region count.
1348 //
1349 SmramReservedCount = 0;
1350 if (SmmConfiguration != NULL) {
1351 while (SmmConfiguration->SmramReservedRegions[SmramReservedCount].SmramReservedSize != 0) {
1352 SmramReservedCount++;
1353 }
1354 }
1355
1356 //
1357 // Reserve one entry for SMM Core in the full SMRAM ranges.
1358 //
1359 AdditionSmramRangeCount = 1;
1360 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
1361 //
1362 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.
1363 //
1364 AdditionSmramRangeCount = 2;
1365 }
1366
1367 if (SmramReservedCount == 0) {
1368 //
1369 // No reserved SMRAM entry from SMM Configuration Protocol.
1370 //
1371 *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;
1372 Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);
1373 FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocateZeroPool (Size);
1374 ASSERT (FullSmramRanges != NULL);
1375
1376 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, FullSmramRanges);
1377 ASSERT_EFI_ERROR (Status);
1378
1379 return FullSmramRanges;
1380 }
1381
1382 //
1383 // Why MaxCount = X + 2 * Y?
1384 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.
1385 //
1386 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1
1387 // ---- ----
1388 // | | ---- |--|
1389 // | | | | -> | |
1390 // | | ---- |--|
1391 // ---- ----
1392 //
1393 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1
1394 // ---- ----
1395 // | | | |
1396 // | | ---- |--|
1397 // | | | | | |
1398 // |--| | | -> |--|
1399 // | | | | | |
1400 // | | ---- |--|
1401 // | | | |
1402 // ---- ----
1403 //
1404 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1
1405 // ---- ----
1406 // | | | |
1407 // | | ---- |--|
1408 // |--| | | |--|
1409 // | | | | -> | |
1410 // |--| | | |--|
1411 // | | ---- |--|
1412 // | | | |
1413 // ---- ----
1414 //
1415 // ......
1416 //
1417 MaxCount = SmramRangeCount + 2 * SmramReservedCount;
1418
1419 Size = MaxCount * sizeof (EFI_SMM_RESERVED_SMRAM_REGION);
1420 SmramReservedRanges = (EFI_SMM_RESERVED_SMRAM_REGION *) AllocatePool (Size);
1421 ASSERT (SmramReservedRanges != NULL);
1422 for (Index = 0; Index < SmramReservedCount; Index++) {
1423 CopyMem (&SmramReservedRanges[Index], &SmmConfiguration->SmramReservedRegions[Index], sizeof (EFI_SMM_RESERVED_SMRAM_REGION));
1424 }
1425
1426 Size = MaxCount * sizeof (EFI_SMRAM_DESCRIPTOR);
1427 TempSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);
1428 ASSERT (TempSmramRanges != NULL);
1429 TempSmramRangeCount = 0;
1430
1431 SmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);
1432 ASSERT (SmramRanges != NULL);
1433 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, SmramRanges);
1434 ASSERT_EFI_ERROR (Status);
1435
1436 do {
1437 Rescan = FALSE;
1438 for (Index = 0; (Index < SmramRangeCount) && !Rescan; Index++) {
1439 //
1440 // Skip zero size entry.
1441 //
1442 if (SmramRanges[Index].PhysicalSize != 0) {
1443 for (Index2 = 0; (Index2 < SmramReservedCount) && !Rescan; Index2++) {
1444 //
1445 // Skip zero size entry.
1446 //
1447 if (SmramReservedRanges[Index2].SmramReservedSize != 0) {
1448 if (SmmIsSmramOverlap (
1449 &SmramRanges[Index],
1450 &SmramReservedRanges[Index2]
1451 )) {
1452 //
1453 // There is overlap, need to split entry and then rescan.
1454 //
1455 SmmSplitSmramEntry (
1456 &SmramRanges[Index],
1457 &SmramReservedRanges[Index2],
1458 SmramRanges,
1459 &SmramRangeCount,
1460 SmramReservedRanges,
1461 &SmramReservedCount,
1462 TempSmramRanges,
1463 &TempSmramRangeCount
1464 );
1465 Rescan = TRUE;
1466 }
1467 }
1468 }
1469 if (!Rescan) {
1470 //
1471 // No any overlap, copy the entry to the temp SMRAM ranges.
1472 // Zero SmramRanges[Index].PhysicalSize = 0;
1473 //
1474 CopyMem (&TempSmramRanges[TempSmramRangeCount++], &SmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));
1475 SmramRanges[Index].PhysicalSize = 0;
1476 }
1477 }
1478 }
1479 } while (Rescan);
1480 ASSERT (TempSmramRangeCount <= MaxCount);
1481
1482 //
1483 // Sort the entries
1484 //
1485 FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));
1486 ASSERT (FullSmramRanges != NULL);
1487 *FullSmramRangeCount = 0;
1488 do {
1489 for (Index = 0; Index < TempSmramRangeCount; Index++) {
1490 if (TempSmramRanges[Index].PhysicalSize != 0) {
1491 break;
1492 }
1493 }
1494 ASSERT (Index < TempSmramRangeCount);
1495 for (Index2 = 0; Index2 < TempSmramRangeCount; Index2++) {
1496 if ((Index2 != Index) && (TempSmramRanges[Index2].PhysicalSize != 0) && (TempSmramRanges[Index2].CpuStart < TempSmramRanges[Index].CpuStart)) {
1497 Index = Index2;
1498 }
1499 }
1500 CopyMem (&FullSmramRanges[*FullSmramRangeCount], &TempSmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));
1501 *FullSmramRangeCount += 1;
1502 TempSmramRanges[Index].PhysicalSize = 0;
1503 } while (*FullSmramRangeCount < TempSmramRangeCount);
1504 ASSERT (*FullSmramRangeCount == TempSmramRangeCount);
1505 *FullSmramRangeCount += AdditionSmramRangeCount;
1506
1507 FreePool (SmramRanges);
1508 FreePool (SmramReservedRanges);
1509 FreePool (TempSmramRanges);
1510
1511 return FullSmramRanges;
1512 }
1513
1514 /**
1515 The Entry Point for SMM IPL
1516
1517 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install
1518 SMM Base 2 Protocol and SMM Communication Protocol, and register for the
1519 critical events required to coordinate between DXE and SMM environments.
1520
1521 @param ImageHandle The firmware allocated handle for the EFI image.
1522 @param SystemTable A pointer to the EFI System Table.
1523
1524 @retval EFI_SUCCESS The entry point is executed successfully.
1525 @retval Other Some error occurred when executing this entry point.
1526
1527 **/
1528 EFI_STATUS
1529 EFIAPI
1530 SmmIplEntry (
1531 IN EFI_HANDLE ImageHandle,
1532 IN EFI_SYSTEM_TABLE *SystemTable
1533 )
1534 {
1535 EFI_STATUS Status;
1536 UINTN Index;
1537 UINT64 MaxSize;
1538 VOID *Registration;
1539 UINT64 SmmCodeSize;
1540 EFI_CPU_ARCH_PROTOCOL *CpuArch;
1541 EFI_STATUS SetAttrStatus;
1542 EFI_SMRAM_DESCRIPTOR *SmramRangeSmmDriver;
1543
1544 //
1545 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the
1546 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded
1547 // by the SMM Core
1548 //
1549 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;
1550
1551 //
1552 // Get SMM Access Protocol
1553 //
1554 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);
1555 ASSERT_EFI_ERROR (Status);
1556
1557 //
1558 // Get SMM Control2 Protocol
1559 //
1560 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);
1561 ASSERT_EFI_ERROR (Status);
1562
1563 gSmmCorePrivate->SmramRanges = GetFullSmramRanges (&gSmmCorePrivate->SmramRangeCount);
1564
1565 //
1566 // Open all SMRAM ranges
1567 //
1568 Status = mSmmAccess->Open (mSmmAccess);
1569 ASSERT_EFI_ERROR (Status);
1570
1571 //
1572 // Print debug message that the SMRAM window is now open.
1573 //
1574 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));
1575
1576 //
1577 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size
1578 //
1579 mCurrentSmramRange = NULL;
1580 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {
1581 //
1582 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1583 //
1584 if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {
1585 continue;
1586 }
1587
1588 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {
1589 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {
1590 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {
1591 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;
1592 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];
1593 }
1594 }
1595 }
1596 }
1597
1598 if (mCurrentSmramRange != NULL) {
1599 //
1600 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core
1601 //
1602 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n",
1603 (VOID *)(UINTN)mCurrentSmramRange->CpuStart,
1604 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)
1605 ));
1606
1607 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);
1608 //
1609 // If CPU AP is present, attempt to set SMRAM cacheability to WB
1610 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP
1611 // is not available here.
1612 //
1613 CpuArch = NULL;
1614 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);
1615 if (!EFI_ERROR (Status)) {
1616 Status = gDS->SetMemorySpaceAttributes(
1617 mSmramCacheBase,
1618 mSmramCacheSize,
1619 EFI_MEMORY_WB
1620 );
1621 if (EFI_ERROR (Status)) {
1622 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));
1623 }
1624 }
1625 //
1626 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load
1627 // Modules At Fixed Address Configuration Table.
1628 //
1629 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
1630 //
1631 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
1632 //
1633 SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);
1634 //
1635 // The SMRAM available memory is assumed to be larger than SmmCodeSize
1636 //
1637 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);
1638 //
1639 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.
1640 //
1641 Status = EfiGetSystemConfigurationTable (
1642 &gLoadFixedAddressConfigurationTableGuid,
1643 (VOID **) &mLMFAConfigurationTable
1644 );
1645 if (!EFI_ERROR (Status) && mLMFAConfigurationTable != NULL) {
1646 mLMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;
1647 //
1648 // Print the SMRAM base
1649 //
1650 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable->SmramBase));
1651 }
1652
1653 //
1654 // Fill the Smram range for all SMM code
1655 //
1656 SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];
1657 SmramRangeSmmDriver->CpuStart = mCurrentSmramRange->CpuStart;
1658 SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;
1659 SmramRangeSmmDriver->RegionState = mCurrentSmramRange->RegionState | EFI_ALLOCATED;
1660 SmramRangeSmmDriver->PhysicalSize = SmmCodeSize;
1661
1662 mCurrentSmramRange->PhysicalSize -= SmmCodeSize;
1663 mCurrentSmramRange->CpuStart = mCurrentSmramRange->CpuStart + SmmCodeSize;
1664 mCurrentSmramRange->PhysicalStart = mCurrentSmramRange->PhysicalStart + SmmCodeSize;
1665 }
1666 //
1667 // Load SMM Core into SMRAM and execute it from SMRAM
1668 //
1669 Status = ExecuteSmmCoreFromSmram (
1670 mCurrentSmramRange,
1671 &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 1],
1672 gSmmCorePrivate
1673 );
1674 if (EFI_ERROR (Status)) {
1675 //
1676 // Print error message that the SMM Core failed to be loaded and executed.
1677 //
1678 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));
1679
1680 //
1681 // Attempt to reset SMRAM cacheability to UC
1682 //
1683 if (CpuArch != NULL) {
1684 SetAttrStatus = gDS->SetMemorySpaceAttributes(
1685 mSmramCacheBase,
1686 mSmramCacheSize,
1687 EFI_MEMORY_UC
1688 );
1689 if (EFI_ERROR (SetAttrStatus)) {
1690 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));
1691 }
1692 }
1693 }
1694 } else {
1695 //
1696 // Print error message that there are not enough SMRAM resources to load the SMM Core.
1697 //
1698 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));
1699 }
1700
1701 //
1702 // If the SMM Core could not be loaded then close SMRAM window, free allocated
1703 // resources, and return an error so SMM IPL will be unloaded.
1704 //
1705 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {
1706 //
1707 // Close all SMRAM ranges
1708 //
1709 Status = mSmmAccess->Close (mSmmAccess);
1710 ASSERT_EFI_ERROR (Status);
1711
1712 //
1713 // Print debug message that the SMRAM window is now closed.
1714 //
1715 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));
1716
1717 //
1718 // Free all allocated resources
1719 //
1720 FreePool (gSmmCorePrivate->SmramRanges);
1721
1722 return EFI_UNSUPPORTED;
1723 }
1724
1725 //
1726 // Install SMM Base2 Protocol and SMM Communication Protocol
1727 //
1728 Status = gBS->InstallMultipleProtocolInterfaces (
1729 &mSmmIplHandle,
1730 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,
1731 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,
1732 NULL
1733 );
1734 ASSERT_EFI_ERROR (Status);
1735
1736 //
1737 // Create the set of protocol and event notififcations that the SMM IPL requires
1738 //
1739 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {
1740 if (mSmmIplEvents[Index].Protocol) {
1741 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (
1742 mSmmIplEvents[Index].Guid,
1743 mSmmIplEvents[Index].NotifyTpl,
1744 mSmmIplEvents[Index].NotifyFunction,
1745 mSmmIplEvents[Index].NotifyContext,
1746 &Registration
1747 );
1748 } else {
1749 Status = gBS->CreateEventEx (
1750 EVT_NOTIFY_SIGNAL,
1751 mSmmIplEvents[Index].NotifyTpl,
1752 mSmmIplEvents[Index].NotifyFunction,
1753 mSmmIplEvents[Index].NotifyContext,
1754 mSmmIplEvents[Index].Guid,
1755 &mSmmIplEvents[Index].Event
1756 );
1757 ASSERT_EFI_ERROR (Status);
1758 }
1759 }
1760
1761 return EFI_SUCCESS;
1762 }