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