]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
Add PI SMM IPL and PI SMM Core
[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
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
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
27\r
28#include <Library/BaseLib.h>\r
29#include <Library/BaseMemoryLib.h>\r
30#include <Library/PeCoffLib.h>\r
31#include <Library/CacheMaintenanceLib.h>\r
32#include <Library/MemoryAllocationLib.h>\r
33#include <Library/DebugLib.h>\r
34#include <Library/UefiBootServicesTableLib.h>\r
35#include <Library/DxeServicesTableLib.h>\r
36#include <Library/UefiLib.h>\r
37#include <Library/UefiRuntimeLib.h>\r
38\r
39#include "PiSmmCorePrivateData.h"\r
40\r
41//\r
42// Function prototypes from produced protocols\r
43//\r
44\r
45/**\r
46 Indicate whether the driver is currently executing in the SMM Initialization phase.\r
47\r
48 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
49 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r
50 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r
51\r
52 @retval EFI_INVALID_PARAMETER InSmram was NULL.\r
53 @retval EFI_SUCCESS The call returned successfully.\r
54\r
55**/\r
56EFI_STATUS\r
57EFIAPI\r
58SmmBase2InSmram (\r
59 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
60 OUT BOOLEAN *InSmram\r
61 );\r
62\r
63/**\r
64 Retrieves the location of the System Management System Table (SMST).\r
65\r
66 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
67 @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r
68\r
69 @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r
70 @retval EFI_SUCCESS The memory was returned to the system.\r
71 @retval EFI_UNSUPPORTED Not in SMM.\r
72\r
73**/\r
74EFI_STATUS\r
75EFIAPI\r
76SmmBase2GetSmstLocation (\r
77 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
78 OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r
79 );\r
80\r
81/**\r
82 Communicates with a registered handler.\r
83 \r
84 This function provides a service to send and receive messages from a registered \r
85 UEFI service. This function is part of the SMM Communication Protocol that may \r
86 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode \r
87 after SetVirtualAddressMap().\r
88\r
89 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
90 @param[in out] CommBuffer A pointer to the buffer to convey into SMRAM.\r
91 @param[in out] CommSize The size of the data buffer being passed in.On exit, the size of data\r
92 being returned. Zero if the handler does not wish to reply with any data.\r
93\r
94 @retval EFI_SUCCESS The message was successfully posted.\r
95 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r
96**/\r
97EFI_STATUS\r
98EFIAPI\r
99SmmCommunicationCommunicate (\r
100 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r
101 IN OUT VOID *CommBuffer,\r
102 IN OUT UINTN *CommSize\r
103 );\r
104\r
105/**\r
106 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r
107\r
108 @param Event The Event that is being processed, not used.\r
109 @param Context Event Context, not used.\r
110\r
111**/\r
112VOID\r
113EFIAPI\r
114SmmIplSmmConfigurationEventNotify (\r
115 IN EFI_EVENT Event,\r
116 IN VOID *Context\r
117 );\r
118\r
119/**\r
120 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
121 or if gEfiEventReadyToBootGuid is signalled.\r
122\r
123 @param Event The Event that is being processed, not used.\r
124 @param Context Event Context, not used.\r
125\r
126**/\r
127VOID\r
128EFIAPI\r
129SmmIplReadyToLockEventNotify (\r
130 IN EFI_EVENT Event,\r
131 IN VOID *Context\r
132 );\r
133\r
134/**\r
135 Event notification that is fired when DxeDispatch Event Group is signaled.\r
136\r
137 @param Event The Event that is being processed, not used.\r
138 @param Context Event Context, not used.\r
139\r
140**/\r
141VOID\r
142EFIAPI\r
143SmmIplGuidedEventNotify (\r
144 IN EFI_EVENT Event,\r
145 IN VOID *Context\r
146 );\r
147\r
148/**\r
149 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
150\r
151 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
152 It convers pointer to new virtual address.\r
153\r
154 @param Event Event whose notification function is being invoked.\r
155 @param Context Pointer to the notification function's context.\r
156\r
157**/\r
158VOID\r
159EFIAPI\r
160SmmIplSetVirtualAddressNotify (\r
161 IN EFI_EVENT Event,\r
162 IN VOID *Context\r
163 );\r
164\r
165//\r
166// Data structure used to declare a table of protocol notifications and event \r
167// notifications required by the SMM IPL\r
168//\r
169typedef struct {\r
170 BOOLEAN Protocol;\r
171 BOOLEAN CloseOnLock;\r
172 EFI_GUID *Guid;\r
173 EFI_EVENT_NOTIFY NotifyFunction;\r
174 VOID *NotifyContext;\r
175 EFI_EVENT Event;\r
176} SMM_IPL_EVENT_NOTIFICATION;\r
177\r
178//\r
179// Handle to install the SMM Base2 Protocol and the SMM Communication Protocol\r
180//\r
181EFI_HANDLE mSmmIplHandle = NULL;\r
182\r
183//\r
184// SMM Base 2 Protocol instance\r
185//\r
186EFI_SMM_BASE2_PROTOCOL mSmmBase2 = {\r
187 SmmBase2InSmram,\r
188 SmmBase2GetSmstLocation\r
189};\r
190\r
191//\r
192// SMM Communication Protocol instance\r
193//\r
194EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication = {\r
195 SmmCommunicationCommunicate\r
196};\r
197\r
198//\r
199// SMM Core Private Data structure that contains the data shared between\r
200// the SMM IPL and the SMM Core.\r
201//\r
202SMM_CORE_PRIVATE_DATA mSmmCorePrivateData = {\r
203 SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature\r
204 NULL, // SmmIplImageHandle\r
205 0, // SmramRangeCount\r
206 NULL, // SmramRanges\r
207 NULL, // SmmEntryPoint\r
208 FALSE, // SmmEntryPointRegistered\r
209 FALSE, // InSmm\r
210 NULL, // Smst\r
211 0, // BufferSize\r
212 NULL, // CommunicationBuffer\r
213 EFI_SUCCESS // ReturnStatus\r
214};\r
215\r
216//\r
217// Global pointer used to access mSmmCorePrivateData from outside and inside SMM\r
218//\r
219SMM_CORE_PRIVATE_DATA *gSmmCorePrivate = &mSmmCorePrivateData;\r
220\r
221//\r
222// SMM IPL global variables\r
223//\r
224EFI_SMM_CONTROL2_PROTOCOL *mSmmControl2;\r
225EFI_SMM_ACCESS2_PROTOCOL *mSmmAccess;\r
226EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange;\r
227BOOLEAN mSmmLocked = FALSE;\r
228\r
229//\r
230// Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires\r
231//\r
232SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents[] = {\r
233 //\r
234 // Declare protocol notification on the SMM Configuration protocol. When this notification is etablished, \r
235 // the associated event is immediately signalled, so the notification function will be executed and the \r
236 // SMM Configuration Protocol will be found if it is already in the handle database.\r
237 //\r
238 { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, NULL },\r
239 //\r
240 // Declare protocl notification on DxeSmmReadyToLock protocols. When this notification is etablished, \r
241 // the associated event is immediately signalled, so the notification function will be executed and the \r
242 // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.\r
243 //\r
244 { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, NULL },\r
245 //\r
246 // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core\r
247 // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core\r
248 // if notified, so the SMM Core can dispatch SMM drivers.\r
249 //\r
250 { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplGuidedEventNotify, &gEfiEventDxeDispatchGuid, NULL },\r
251 //\r
252 // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is\r
253 // used to make sure SMRAM is locked before any boot options are processed.\r
254 //\r
255 { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, NULL },\r
256 //\r
257 // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform \r
258 // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core \r
259 // must guarantee that it does not access any UEFI related structures outside of SMRAM.\r
260 //\r
261 { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, NULL },\r
262 //\r
263 // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate \r
264 // and mSmmControl2 from physical addresses to virtual addresses.\r
265 //\r
266 { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, NULL },\r
267 //\r
268 // Terminate the table of event notifications\r
269 //\r
270 { FALSE, FALSE, NULL, NULL, NULL, NULL }\r
271};\r
272\r
273/**\r
274 Indicate whether the driver is currently executing in the SMM Initialization phase.\r
275\r
276 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
277 @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r
278 inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r
279\r
280 @retval EFI_INVALID_PARAMETER InSmram was NULL.\r
281 @retval EFI_SUCCESS The call returned successfully.\r
282\r
283**/\r
284EFI_STATUS\r
285EFIAPI\r
286SmmBase2InSmram (\r
287 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
288 OUT BOOLEAN *InSmram\r
289 )\r
290{\r
291 if (InSmram == NULL) {\r
292 return EFI_INVALID_PARAMETER;\r
293 }\r
294\r
295 *InSmram = gSmmCorePrivate->InSmm;\r
296\r
297 return EFI_SUCCESS;\r
298}\r
299\r
300/**\r
301 Retrieves the location of the System Management System Table (SMST).\r
302\r
303 @param This The EFI_SMM_BASE2_PROTOCOL instance.\r
304 @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r
305\r
306 @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r
307 @retval EFI_SUCCESS The memory was returned to the system.\r
308 @retval EFI_UNSUPPORTED Not in SMM.\r
309\r
310**/\r
311EFI_STATUS\r
312EFIAPI\r
313SmmBase2GetSmstLocation (\r
314 IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r
315 OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r
316 )\r
317{\r
318 if ((This == NULL) ||(Smst == NULL)) {\r
319 return EFI_INVALID_PARAMETER;\r
320 }\r
321 \r
322 if (!gSmmCorePrivate->InSmm) {\r
323 return EFI_UNSUPPORTED;\r
324 }\r
325 \r
326 *Smst = gSmmCorePrivate->Smst;\r
327\r
328 return EFI_SUCCESS;\r
329}\r
330\r
331/**\r
332 Communicates with a registered handler.\r
333 \r
334 This function provides a service to send and receive messages from a registered \r
335 UEFI service. This function is part of the SMM Communication Protocol that may \r
336 be called in physical mode prior to SetVirtualAddressMap() and in virtual mode \r
337 after SetVirtualAddressMap().\r
338\r
339 @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
340 @param[in out] CommBuffer A pointer to the buffer to convey into SMRAM.\r
341 @param[in out] CommSize The size of the data buffer being passed in.On exit, the size of data\r
342 being returned. Zero if the handler does not wish to reply with any data.\r
343\r
344 @retval EFI_SUCCESS The message was successfully posted.\r
345 @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r
346**/\r
347EFI_STATUS\r
348EFIAPI\r
349SmmCommunicationCommunicate (\r
350 IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r
351 IN OUT VOID *CommBuffer,\r
352 IN OUT UINTN *CommSize\r
353 )\r
354{\r
355 EFI_STATUS Status;\r
356 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r
357 BOOLEAN OldInSmm;\r
358\r
359 //\r
360 // Check parameters\r
361 //\r
362 if ((CommBuffer == NULL) || (CommSize == NULL)) {\r
363 return EFI_INVALID_PARAMETER;\r
364 }\r
365\r
366 //\r
367 // If not already in SMM, then generate a Software SMI\r
368 //\r
369 if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {\r
370 //\r
371 // Put arguments for Software SMI in gSmmCorePrivate\r
372 //\r
373 gSmmCorePrivate->CommunicationBuffer = CommBuffer;\r
374 gSmmCorePrivate->BufferSize = CommSize;\r
375\r
376 //\r
377 // Generate Software SMI\r
378 //\r
379 Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);\r
380 if (EFI_ERROR (Status)) {\r
381 return EFI_UNSUPPORTED;\r
382 }\r
383\r
384 //\r
385 // Return status from software SMI \r
386 //\r
387 return gSmmCorePrivate->ReturnStatus;\r
388 }\r
389\r
390 //\r
391 // If we are in SMM, then the execution mode must be physical, which means that\r
392 // OS established virtual addresses can not be used. If SetVirtualAddressMap()\r
393 // has been called, then a direct invocation of the Software SMI is not \r
394 // not allowed so return EFI_INVALID_PARAMETER.\r
395 //\r
396 if (EfiGoneVirtual()) {\r
397 return EFI_INVALID_PARAMETER;\r
398 }\r
399\r
400 //\r
401 // Save current InSmm state and set InSmm state to TRUE\r
402 //\r
403 OldInSmm = gSmmCorePrivate->InSmm;\r
404 gSmmCorePrivate->InSmm = TRUE;\r
405\r
406 //\r
407 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.\r
408 //\r
409 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;\r
410 *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
411 Status = gSmmCorePrivate->Smst->SmiManage (\r
412 &CommunicateHeader->HeaderGuid, \r
413 NULL, \r
414 CommunicateHeader->Data, \r
415 CommSize\r
416 );\r
417\r
418 //\r
419 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
420 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
421 //\r
422 *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
423\r
424 //\r
425 // Restore original InSmm state\r
426 //\r
427 gSmmCorePrivate->InSmm = OldInSmm;\r
428\r
429 return (Status == EFI_WARN_INTERRUPT_SOURCE_QUIESCED) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
430}\r
431\r
432/**\r
433 Event notification that is fired when DxeDispatch Event Group is signaled.\r
434\r
435 @param Event The Event that is being processed, not used.\r
436 @param Context Event Context, not used.\r
437\r
438**/\r
439VOID\r
440EFIAPI\r
441SmmIplGuidedEventNotify (\r
442 IN EFI_EVENT Event,\r
443 IN VOID *Context\r
444 )\r
445{\r
446 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader;\r
447 UINTN Size;\r
448\r
449 //\r
450 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure \r
451 //\r
452 CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
453 CommunicateHeader.MessageLength = 1;\r
454 CommunicateHeader.Data[0] = 0;\r
455\r
456 //\r
457 // Generate the Software SMI and return the result\r
458 //\r
459 Size = sizeof (CommunicateHeader);\r
460 SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);\r
461}\r
462\r
463/**\r
464 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r
465\r
466 @param Event The Event that is being processed, not used.\r
467 @param Context Event Context, not used.\r
468\r
469**/\r
470VOID\r
471EFIAPI\r
472SmmIplSmmConfigurationEventNotify (\r
473 IN EFI_EVENT Event,\r
474 IN VOID *Context\r
475 )\r
476{\r
477 EFI_STATUS Status;\r
478 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
479\r
480 //\r
481 // Make sure this notification is for this handler\r
482 //\r
483 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);\r
484 if (EFI_ERROR (Status)) {\r
485 return;\r
486 }\r
487\r
488 //\r
489 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol\r
490 //\r
491 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);\r
492 ASSERT_EFI_ERROR (Status);\r
493\r
494 //\r
495 // Set flag to indicate that the SM< Entry Point has been registered which \r
496 // means that SMIs are now fully operational.\r
497 //\r
498 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;\r
499\r
500 //\r
501 // Print debug message showing SMM Core entry point address.\r
502 //\r
503 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));\r
504\r
505 //\r
506 // Attempt to reset SMRAM cacheability to UC\r
507 //\r
508 Status = gDS->SetMemorySpaceAttributes(\r
509 mCurrentSmramRange->CpuStart, \r
510 mCurrentSmramRange->PhysicalSize,\r
511 EFI_MEMORY_UC\r
512 );\r
513 if (EFI_ERROR (Status)) {\r
514 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
515 } \r
516\r
517 //\r
518 // Close all SMRAM ranges to protect SMRAM\r
519 //\r
520 Status = mSmmAccess->Close (mSmmAccess);\r
521 ASSERT_EFI_ERROR (Status);\r
522\r
523 //\r
524 // Print debug message that the SMRAM window is now closed.\r
525 //\r
526 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
527}\r
528\r
529/**\r
530 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
531 or if gEfiEventReadyToBootGuid is signalled.\r
532\r
533 @param Event The Event that is being processed, not used.\r
534 @param Context Event Context, not used.\r
535\r
536**/\r
537VOID\r
538EFIAPI\r
539SmmIplReadyToLockEventNotify (\r
540 IN EFI_EVENT Event,\r
541 IN VOID *Context\r
542 )\r
543{\r
544 EFI_STATUS Status;\r
545 VOID *Interface;\r
546 UINTN Index;\r
547\r
548 //\r
549 // See if we are already locked\r
550 //\r
551 if (mSmmLocked) {\r
552 return;\r
553 }\r
554 \r
555 //\r
556 // Make sure this notification is for this handler\r
557 //\r
558 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {\r
559 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);\r
560 if (EFI_ERROR (Status)) {\r
561 return;\r
562 }\r
563 } else {\r
564 //\r
565 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being \r
566 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.\r
567 // Print a warning on debug builds.\r
568 //\r
569 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));\r
570 }\r
571\r
572 //\r
573 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r
574 //\r
575 mSmmAccess->Lock (mSmmAccess);\r
576\r
577 //\r
578 // Close protocol and event notification events that do not apply after the \r
579 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot \r
580 // event has been signalled.\r
581 //\r
582 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
583 if (mSmmIplEvents[Index].CloseOnLock) {\r
584 gBS->CloseEvent (mSmmIplEvents[Index].Event);\r
585 }\r
586 }\r
587\r
588 //\r
589 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed\r
590 //\r
591 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);\r
592\r
593 //\r
594 // Print debug message that the SMRAM window is now locked.\r
595 //\r
596 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));\r
597 \r
598 //\r
599 // Set flag so this operation will not be performed again\r
600 //\r
601 mSmmLocked = TRUE;\r
602}\r
603\r
604/**\r
605 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
606\r
607 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
608 It convers pointer to new virtual address.\r
609\r
610 @param Event Event whose notification function is being invoked.\r
611 @param Context Pointer to the notification function's context.\r
612\r
613**/\r
614VOID\r
615EFIAPI\r
616SmmIplSetVirtualAddressNotify (\r
617 IN EFI_EVENT Event,\r
618 IN VOID *Context\r
619 )\r
620{\r
621 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);\r
622}\r
623\r
624/**\r
625 Searches all Firmware Volumes for the first file matching FileType and SectionType and returns the section data.\r
626\r
627 @param FileType FileType to search for within any of the firmware volumes in the platform.\r
628 @param SectionType SectionType to search for within any of the matching FileTypes in the firmware volumes in the platform.\r
629 @param SourceSize Return the size of the returned section data..\r
630\r
631 @retval != NULL Pointer to the allocated buffer containing the section data.\r
632 @retval NULL Section data was not found.\r
633\r
634**/\r
635VOID *\r
636GetSectionInAnyFv (\r
637 IN EFI_FV_FILETYPE FileType,\r
638 IN EFI_SECTION_TYPE SectionType,\r
639 OUT UINTN *SourceSize\r
640 )\r
641{\r
642 EFI_STATUS Status;\r
643 UINTN HandleCount;\r
644 EFI_HANDLE *HandleBuffer;\r
645 UINTN Index;\r
646 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
647 UINTN Key;\r
648 EFI_GUID NameGuid;\r
649 EFI_FV_FILE_ATTRIBUTES Attributes;\r
650 VOID *SourceBuffer;\r
651 UINT32 AuthenticationStatus;\r
652\r
653 HandleBuffer = NULL;\r
654 Status = gBS->LocateHandleBuffer (\r
655 ByProtocol,\r
656 &gEfiFirmwareVolume2ProtocolGuid,\r
657 NULL,\r
658 &HandleCount,\r
659 &HandleBuffer\r
660 );\r
661 if (EFI_ERROR (Status)) {\r
662 return NULL;\r
663 }\r
664\r
665 for (Index = 0; Index < HandleCount; Index++) {\r
666 Status = gBS->HandleProtocol (\r
667 HandleBuffer[Index],\r
668 &gEfiFirmwareVolume2ProtocolGuid,\r
669 (VOID **)&Fv\r
670 );\r
671 if (EFI_ERROR (Status)) {\r
672 continue;\r
673 }\r
674\r
675 //\r
676 // Use Firmware Volume 2 Protocol to search for a file of type FileType\r
677 //\r
678 Key = 0; \r
679 Status = Fv->GetNextFile (Fv, &Key, &FileType, &NameGuid, &Attributes, SourceSize);\r
680 if (EFI_ERROR (Status)) {\r
681 continue;\r
682 }\r
683\r
684 //\r
685 // Use Firmware Volume 2 Protocol to read a section of type SectionType\r
686 //\r
687 SourceBuffer = NULL;\r
688 Status = Fv->ReadSection (Fv, &NameGuid, SectionType, 0, &SourceBuffer, SourceSize, &AuthenticationStatus);\r
689 if (!EFI_ERROR (Status)) {\r
690 FreePool (HandleBuffer);\r
691 return SourceBuffer;\r
692 }\r
693 } \r
694\r
695 FreePool(HandleBuffer);\r
696 \r
697 return NULL;\r
698}\r
699\r
700/**\r
701 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r
702\r
703 @param[in] SmramRange Descriptor for the range of SMRAM to reload the \r
704 currently executing image.\r
705 @param[in] Context Context to pass into SMM Core\r
706\r
707 @return EFI_STATUS\r
708\r
709**/\r
710EFI_STATUS\r
711ExecuteSmmCoreFromSmram (\r
712 IN EFI_SMRAM_DESCRIPTOR *SmramRange,\r
713 IN VOID *Context\r
714 )\r
715{\r
716 EFI_STATUS Status;\r
717 VOID *SourceBuffer;\r
718 UINTN SourceSize;\r
719 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
720 UINTN PageCount;\r
721 EFI_PHYSICAL_ADDRESS DestinationBuffer;\r
722 EFI_IMAGE_ENTRY_POINT EntryPoint;\r
723\r
724 //\r
725 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE\r
726 // \r
727 SourceBuffer = GetSectionInAnyFv (EFI_FV_FILETYPE_SMM_CORE, EFI_SECTION_PE32, &SourceSize);\r
728 if (SourceBuffer == NULL) {\r
729 return EFI_NOT_FOUND;\r
730 }\r
731 \r
732 //\r
733 // Initilize ImageContext\r
734 //\r
735 ImageContext.Handle = SourceBuffer;\r
736 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
737\r
738 //\r
739 // Get information about the image being loaded\r
740 //\r
741 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
742 if (EFI_ERROR (Status)) {\r
743 return Status;\r
744 }\r
745\r
746 //\r
747 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
748 // specified by SmramRange\r
749 //\r
750 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);\r
751\r
752 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
753 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
754\r
755 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
756 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
757\r
758 //\r
759 // Align buffer on section boundry\r
760 //\r
761 ImageContext.ImageAddress = DestinationBuffer;\r
762 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
763 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
764\r
765 //\r
766 // Print debug message showing SMM Core load address.\r
767 //\r
768 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));\r
769\r
770 //\r
771 // Load the image to our new buffer\r
772 //\r
773 Status = PeCoffLoaderLoadImage (&ImageContext);\r
774 if (!EFI_ERROR (Status)) {\r
775 //\r
776 // Relocate the image in our new buffer\r
777 //\r
778 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
779 if (!EFI_ERROR (Status)) {\r
780 //\r
781 // Flush the instruction cache so the image data are written before we execute it\r
782 //\r
783 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
784\r
785 //\r
786 // Print debug message showing SMM Core entry point address.\r
787 //\r
788 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));\r
789\r
790 //\r
791 // Execute image\r
792 //\r
793 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;\r
794 Status = EntryPoint ((EFI_HANDLE)Context, gST);\r
795 }\r
796 }\r
797\r
798 //\r
799 // If the load operation, relocate operation, or the image execution return an\r
800 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by \r
801 // SmramRange\r
802 //\r
803 if (EFI_ERROR (Status)) {\r
804 SmramRange->PhysicalSize += EFI_PAGES_TO_SIZE (PageCount);\r
805 }\r
806\r
807 //\r
808 // Always free memory allocted by GetFileBufferByFilePath ()\r
809 //\r
810 FreePool (SourceBuffer);\r
811\r
812 return Status;\r
813}\r
814\r
815/**\r
816 The Entry Point for SMM IPL\r
817\r
818 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install \r
819 SMM Base 2 Protocol and SMM Communication Protocol, and register for the \r
820 critical events required to coordinate between DXE and SMM environments.\r
821 \r
822 @param ImageHandle The firmware allocated handle for the EFI image.\r
823 @param SystemTable A pointer to the EFI System Table.\r
824\r
825 @retval EFI_SUCCESS The entry point is executed successfully.\r
826 @retval Other Some error occurred when executing this entry point.\r
827\r
828**/\r
829EFI_STATUS\r
830EFIAPI\r
831SmmIplEntry (\r
832 IN EFI_HANDLE ImageHandle,\r
833 IN EFI_SYSTEM_TABLE *SystemTable\r
834 )\r
835{\r
836 EFI_STATUS Status;\r
837 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
838 UINTN Size;\r
839 UINTN Index;\r
840 EFI_SMM_RESERVED_SMRAM_REGION *SmramResRegion;\r
841 UINT64 MaxSize;\r
842 VOID *Registration;\r
843\r
844 //\r
845 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the \r
846 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded \r
847 // by the SMM Core\r
848 //\r
849 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r
850\r
851 //\r
852 // Get SMM Access Protocol\r
853 //\r
854 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);\r
855 ASSERT_EFI_ERROR (Status);\r
856\r
857 //\r
858 // Get SMM Control2 Protocol\r
859 //\r
860 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);\r
861 ASSERT_EFI_ERROR (Status);\r
862\r
863 //\r
864 // Get SMM Configuration Protocol if it is present\r
865 //\r
866 SmmConfiguration = NULL;\r
867 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);\r
868\r
869 //\r
870 // Get SMRAM information\r
871 //\r
872 Size = 0;\r
873 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);\r
874 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
875\r
876 gSmmCorePrivate->SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);\r
877 ASSERT (gSmmCorePrivate->SmramRanges != NULL);\r
878\r
879 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, gSmmCorePrivate->SmramRanges);\r
880 ASSERT_EFI_ERROR (Status);\r
881\r
882 gSmmCorePrivate->SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
883\r
884 //\r
885 // Open all SMRAM ranges\r
886 //\r
887 Status = mSmmAccess->Open (mSmmAccess);\r
888 ASSERT_EFI_ERROR (Status);\r
889\r
890 //\r
891 // Print debug message that the SMRAM window is now open.\r
892 //\r
893 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r
894\r
895 //\r
896 // Subtract SMRAM any reserved SMRAM regions.\r
897 //\r
898 if (SmmConfiguration != NULL) {\r
899 SmramResRegion = SmmConfiguration->SmramReservedRegions;\r
900 while (SmramResRegion->SmramReservedSize != 0) {\r
901 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index ++) {\r
902 if ((SmramResRegion->SmramReservedStart >= gSmmCorePrivate->SmramRanges[Index].CpuStart) && \\r
903 ((SmramResRegion->SmramReservedStart + SmramResRegion->SmramReservedSize) <= \\r
904 (gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize))) {\r
905 //\r
906 // This range has reserved area, calculate the left free size\r
907 //\r
908 gSmmCorePrivate->SmramRanges[Index].PhysicalSize = SmramResRegion->SmramReservedStart - gSmmCorePrivate->SmramRanges[Index].CpuStart;\r
909 }\r
910 }\r
911 SmramResRegion++;\r
912 }\r
913 }\r
914 \r
915 //\r
916 // Find the largest SMRAM range between 1MB and 4GB that is at least 1MB in size\r
917 //\r
918 mCurrentSmramRange = NULL;\r
919 for (Index = 0, MaxSize = SIZE_1MB; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
920 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r
921 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {\r
922 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r
923 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
924 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r
925 }\r
926 }\r
927 }\r
928 }\r
929\r
930 if (mCurrentSmramRange != NULL) {\r
931 //\r
932 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r
933 //\r
934 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n", \r
935 (VOID *)(UINTN)mCurrentSmramRange->CpuStart, \r
936 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r
937 ));\r
938\r
939 //\r
940 // Attempt to set SMRAM cacheability to WB\r
941 //\r
942 Status = gDS->SetMemorySpaceAttributes(\r
943 mCurrentSmramRange->CpuStart, \r
944 mCurrentSmramRange->PhysicalSize,\r
945 EFI_MEMORY_WB\r
946 );\r
947 if (EFI_ERROR (Status)) {\r
948 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r
949 } \r
950\r
951 //\r
952 // Load SMM Core into SMRAM and execute it from SMRAM\r
953 //\r
954 Status = ExecuteSmmCoreFromSmram (mCurrentSmramRange, gSmmCorePrivate);\r
955 if (EFI_ERROR (Status)) {\r
956 //\r
957 // Print error message that the SMM Core failed to be loaded and executed.\r
958 //\r
959 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));\r
960\r
961 //\r
962 // Attempt to reset SMRAM cacheability to UC\r
963 //\r
964 Status = gDS->SetMemorySpaceAttributes(\r
965 mCurrentSmramRange->CpuStart, \r
966 mCurrentSmramRange->PhysicalSize,\r
967 EFI_MEMORY_UC\r
968 );\r
969 if (EFI_ERROR (Status)) {\r
970 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
971 } \r
972 }\r
973 } else {\r
974 //\r
975 // Print error message that there are not enough SMRAM resources to load the SMM Core.\r
976 //\r
977 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));\r
978 }\r
979\r
980 //\r
981 // If the SMM Core could not be loaded then close SMRAM window, free allocated \r
982 // resources, and return an error so SMM IPL will be unloaded.\r
983 //\r
984 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {\r
985 //\r
986 // Close all SMRAM ranges\r
987 //\r
988 Status = mSmmAccess->Close (mSmmAccess);\r
989 ASSERT_EFI_ERROR (Status);\r
990\r
991 //\r
992 // Print debug message that the SMRAM window is now closed.\r
993 //\r
994 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
995\r
996 //\r
997 // Free all allocated resources\r
998 //\r
999 FreePool (gSmmCorePrivate->SmramRanges);\r
1000 \r
1001 return EFI_UNSUPPORTED;\r
1002 }\r
1003 \r
1004 //\r
1005 // Install SMM Base2 Protocol and SMM Communication Protocol\r
1006 //\r
1007 Status = gBS->InstallMultipleProtocolInterfaces (\r
1008 &mSmmIplHandle,\r
1009 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,\r
1010 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,\r
1011 NULL\r
1012 );\r
1013 ASSERT_EFI_ERROR (Status);\r
1014\r
1015 //\r
1016 // Create the set of protocol and event notififcations that the SMM IPL requires\r
1017 //\r
1018 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
1019 if (mSmmIplEvents[Index].Protocol) {\r
1020 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (\r
1021 mSmmIplEvents[Index].Guid,\r
1022 TPL_CALLBACK,\r
1023 mSmmIplEvents[Index].NotifyFunction,\r
1024 mSmmIplEvents[Index].NotifyContext,\r
1025 &Registration\r
1026 );\r
1027 } else {\r
1028 Status = gBS->CreateEventEx (\r
1029 EVT_NOTIFY_SIGNAL,\r
1030 TPL_CALLBACK,\r
1031 mSmmIplEvents[Index].NotifyFunction,\r
1032 mSmmIplEvents[Index].NotifyContext,\r
1033 mSmmIplEvents[Index].Guid,\r
1034 &mSmmIplEvents[Index].Event\r
1035 );\r
1036 ASSERT_EFI_ERROR (Status);\r
1037 }\r
1038 }\r
1039\r
1040 return EFI_SUCCESS;\r
1041}\r