]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
Fix non-boolean comparison expression.
[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
2292758d 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
e42e9404 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
2292758d 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
e42e9404 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
3c5963cf 400 //\r
401 // Don't allow call SmiManage() directly when SMRAM is closed or locked.\r
402 //\r
403 if (!mSmmAccess->OpenState || mSmmAccess->LockState) {\r
404 return EFI_INVALID_PARAMETER;\r
405 }\r
406 \r
e42e9404 407 //\r
408 // Save current InSmm state and set InSmm state to TRUE\r
409 //\r
410 OldInSmm = gSmmCorePrivate->InSmm;\r
411 gSmmCorePrivate->InSmm = TRUE;\r
412\r
413 //\r
414 // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.\r
415 //\r
416 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;\r
417 *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
418 Status = gSmmCorePrivate->Smst->SmiManage (\r
419 &CommunicateHeader->HeaderGuid, \r
420 NULL, \r
421 CommunicateHeader->Data, \r
422 CommSize\r
423 );\r
424\r
425 //\r
426 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
427 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
428 //\r
429 *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
430\r
431 //\r
432 // Restore original InSmm state\r
433 //\r
434 gSmmCorePrivate->InSmm = OldInSmm;\r
435\r
436 return (Status == EFI_WARN_INTERRUPT_SOURCE_QUIESCED) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
437}\r
438\r
439/**\r
440 Event notification that is fired when DxeDispatch Event Group is signaled.\r
441\r
442 @param Event The Event that is being processed, not used.\r
443 @param Context Event Context, not used.\r
444\r
445**/\r
446VOID\r
447EFIAPI\r
448SmmIplGuidedEventNotify (\r
449 IN EFI_EVENT Event,\r
450 IN VOID *Context\r
451 )\r
452{\r
453 EFI_SMM_COMMUNICATE_HEADER CommunicateHeader;\r
454 UINTN Size;\r
455\r
456 //\r
457 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure \r
458 //\r
459 CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
460 CommunicateHeader.MessageLength = 1;\r
461 CommunicateHeader.Data[0] = 0;\r
462\r
463 //\r
464 // Generate the Software SMI and return the result\r
465 //\r
466 Size = sizeof (CommunicateHeader);\r
467 SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);\r
468}\r
469\r
470/**\r
471 Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r
472\r
473 @param Event The Event that is being processed, not used.\r
474 @param Context Event Context, not used.\r
475\r
476**/\r
477VOID\r
478EFIAPI\r
479SmmIplSmmConfigurationEventNotify (\r
480 IN EFI_EVENT Event,\r
481 IN VOID *Context\r
482 )\r
483{\r
484 EFI_STATUS Status;\r
485 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
486\r
487 //\r
488 // Make sure this notification is for this handler\r
489 //\r
490 Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);\r
491 if (EFI_ERROR (Status)) {\r
492 return;\r
493 }\r
494\r
495 //\r
496 // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol\r
497 //\r
498 Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);\r
499 ASSERT_EFI_ERROR (Status);\r
500\r
501 //\r
502 // Set flag to indicate that the SM< Entry Point has been registered which \r
503 // means that SMIs are now fully operational.\r
504 //\r
505 gSmmCorePrivate->SmmEntryPointRegistered = TRUE;\r
506\r
507 //\r
508 // Print debug message showing SMM Core entry point address.\r
509 //\r
510 DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));\r
511\r
512 //\r
513 // Attempt to reset SMRAM cacheability to UC\r
514 //\r
515 Status = gDS->SetMemorySpaceAttributes(\r
516 mCurrentSmramRange->CpuStart, \r
517 mCurrentSmramRange->PhysicalSize,\r
518 EFI_MEMORY_UC\r
519 );\r
520 if (EFI_ERROR (Status)) {\r
521 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
522 } \r
523\r
524 //\r
525 // Close all SMRAM ranges to protect SMRAM\r
526 //\r
527 Status = mSmmAccess->Close (mSmmAccess);\r
528 ASSERT_EFI_ERROR (Status);\r
529\r
530 //\r
531 // Print debug message that the SMRAM window is now closed.\r
532 //\r
533 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
534}\r
535\r
536/**\r
537 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
538 or if gEfiEventReadyToBootGuid is signalled.\r
539\r
540 @param Event The Event that is being processed, not used.\r
541 @param Context Event Context, not used.\r
542\r
543**/\r
544VOID\r
545EFIAPI\r
546SmmIplReadyToLockEventNotify (\r
547 IN EFI_EVENT Event,\r
548 IN VOID *Context\r
549 )\r
550{\r
551 EFI_STATUS Status;\r
552 VOID *Interface;\r
553 UINTN Index;\r
554\r
555 //\r
556 // See if we are already locked\r
557 //\r
558 if (mSmmLocked) {\r
559 return;\r
560 }\r
561 \r
562 //\r
563 // Make sure this notification is for this handler\r
564 //\r
565 if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {\r
566 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);\r
567 if (EFI_ERROR (Status)) {\r
568 return;\r
569 }\r
570 } else {\r
571 //\r
572 // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being \r
573 // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.\r
574 // Print a warning on debug builds.\r
575 //\r
576 DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));\r
577 }\r
578\r
579 //\r
580 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r
581 //\r
582 mSmmAccess->Lock (mSmmAccess);\r
583\r
584 //\r
585 // Close protocol and event notification events that do not apply after the \r
586 // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot \r
587 // event has been signalled.\r
588 //\r
589 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
590 if (mSmmIplEvents[Index].CloseOnLock) {\r
591 gBS->CloseEvent (mSmmIplEvents[Index].Event);\r
592 }\r
593 }\r
594\r
595 //\r
596 // Inform SMM Core that the DxeSmmReadyToLock protocol was installed\r
597 //\r
598 SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);\r
599\r
600 //\r
601 // Print debug message that the SMRAM window is now locked.\r
602 //\r
603 DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));\r
604 \r
605 //\r
606 // Set flag so this operation will not be performed again\r
607 //\r
608 mSmmLocked = TRUE;\r
609}\r
610\r
611/**\r
612 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
613\r
614 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
615 It convers pointer to new virtual address.\r
616\r
617 @param Event Event whose notification function is being invoked.\r
618 @param Context Pointer to the notification function's context.\r
619\r
620**/\r
621VOID\r
622EFIAPI\r
623SmmIplSetVirtualAddressNotify (\r
624 IN EFI_EVENT Event,\r
625 IN VOID *Context\r
626 )\r
627{\r
628 EfiConvertPointer (0x0, (VOID **)&mSmmControl2);\r
629}\r
630\r
631/**\r
632 Searches all Firmware Volumes for the first file matching FileType and SectionType and returns the section data.\r
633\r
634 @param FileType FileType to search for within any of the firmware volumes in the platform.\r
635 @param SectionType SectionType to search for within any of the matching FileTypes in the firmware volumes in the platform.\r
636 @param SourceSize Return the size of the returned section data..\r
637\r
638 @retval != NULL Pointer to the allocated buffer containing the section data.\r
639 @retval NULL Section data was not found.\r
640\r
641**/\r
642VOID *\r
643GetSectionInAnyFv (\r
644 IN EFI_FV_FILETYPE FileType,\r
645 IN EFI_SECTION_TYPE SectionType,\r
646 OUT UINTN *SourceSize\r
647 )\r
648{\r
649 EFI_STATUS Status;\r
650 UINTN HandleCount;\r
651 EFI_HANDLE *HandleBuffer;\r
652 UINTN Index;\r
653 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
654 UINTN Key;\r
655 EFI_GUID NameGuid;\r
656 EFI_FV_FILE_ATTRIBUTES Attributes;\r
657 VOID *SourceBuffer;\r
658 UINT32 AuthenticationStatus;\r
659\r
660 HandleBuffer = NULL;\r
661 Status = gBS->LocateHandleBuffer (\r
662 ByProtocol,\r
663 &gEfiFirmwareVolume2ProtocolGuid,\r
664 NULL,\r
665 &HandleCount,\r
666 &HandleBuffer\r
667 );\r
668 if (EFI_ERROR (Status)) {\r
669 return NULL;\r
670 }\r
671\r
672 for (Index = 0; Index < HandleCount; Index++) {\r
673 Status = gBS->HandleProtocol (\r
674 HandleBuffer[Index],\r
675 &gEfiFirmwareVolume2ProtocolGuid,\r
676 (VOID **)&Fv\r
677 );\r
678 if (EFI_ERROR (Status)) {\r
679 continue;\r
680 }\r
681\r
682 //\r
683 // Use Firmware Volume 2 Protocol to search for a file of type FileType\r
684 //\r
685 Key = 0; \r
686 Status = Fv->GetNextFile (Fv, &Key, &FileType, &NameGuid, &Attributes, SourceSize);\r
687 if (EFI_ERROR (Status)) {\r
688 continue;\r
689 }\r
690\r
691 //\r
692 // Use Firmware Volume 2 Protocol to read a section of type SectionType\r
693 //\r
694 SourceBuffer = NULL;\r
695 Status = Fv->ReadSection (Fv, &NameGuid, SectionType, 0, &SourceBuffer, SourceSize, &AuthenticationStatus);\r
696 if (!EFI_ERROR (Status)) {\r
697 FreePool (HandleBuffer);\r
698 return SourceBuffer;\r
699 }\r
700 } \r
701\r
702 FreePool(HandleBuffer);\r
703 \r
704 return NULL;\r
705}\r
706\r
707/**\r
708 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r
709\r
710 @param[in] SmramRange Descriptor for the range of SMRAM to reload the \r
711 currently executing image.\r
712 @param[in] Context Context to pass into SMM Core\r
713\r
714 @return EFI_STATUS\r
715\r
716**/\r
717EFI_STATUS\r
718ExecuteSmmCoreFromSmram (\r
719 IN EFI_SMRAM_DESCRIPTOR *SmramRange,\r
720 IN VOID *Context\r
721 )\r
722{\r
723 EFI_STATUS Status;\r
724 VOID *SourceBuffer;\r
725 UINTN SourceSize;\r
726 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
727 UINTN PageCount;\r
728 EFI_PHYSICAL_ADDRESS DestinationBuffer;\r
729 EFI_IMAGE_ENTRY_POINT EntryPoint;\r
730\r
731 //\r
732 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE\r
733 // \r
734 SourceBuffer = GetSectionInAnyFv (EFI_FV_FILETYPE_SMM_CORE, EFI_SECTION_PE32, &SourceSize);\r
735 if (SourceBuffer == NULL) {\r
736 return EFI_NOT_FOUND;\r
737 }\r
738 \r
739 //\r
740 // Initilize ImageContext\r
741 //\r
742 ImageContext.Handle = SourceBuffer;\r
743 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
744\r
745 //\r
746 // Get information about the image being loaded\r
747 //\r
748 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
749 if (EFI_ERROR (Status)) {\r
750 return Status;\r
751 }\r
752\r
753 //\r
754 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
755 // specified by SmramRange\r
756 //\r
757 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);\r
758\r
759 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
760 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
761\r
762 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
763 DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
764\r
765 //\r
766 // Align buffer on section boundry\r
767 //\r
768 ImageContext.ImageAddress = DestinationBuffer;\r
769 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
770 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
771\r
772 //\r
773 // Print debug message showing SMM Core load address.\r
774 //\r
775 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));\r
776\r
777 //\r
778 // Load the image to our new buffer\r
779 //\r
780 Status = PeCoffLoaderLoadImage (&ImageContext);\r
781 if (!EFI_ERROR (Status)) {\r
782 //\r
783 // Relocate the image in our new buffer\r
784 //\r
785 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
786 if (!EFI_ERROR (Status)) {\r
787 //\r
788 // Flush the instruction cache so the image data are written before we execute it\r
789 //\r
790 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
791\r
792 //\r
793 // Print debug message showing SMM Core entry point address.\r
794 //\r
795 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));\r
796\r
797 //\r
798 // Execute image\r
799 //\r
800 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;\r
801 Status = EntryPoint ((EFI_HANDLE)Context, gST);\r
802 }\r
803 }\r
804\r
805 //\r
806 // If the load operation, relocate operation, or the image execution return an\r
807 // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by \r
808 // SmramRange\r
809 //\r
810 if (EFI_ERROR (Status)) {\r
811 SmramRange->PhysicalSize += EFI_PAGES_TO_SIZE (PageCount);\r
812 }\r
813\r
814 //\r
815 // Always free memory allocted by GetFileBufferByFilePath ()\r
816 //\r
817 FreePool (SourceBuffer);\r
818\r
819 return Status;\r
820}\r
821\r
822/**\r
823 The Entry Point for SMM IPL\r
824\r
825 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install \r
826 SMM Base 2 Protocol and SMM Communication Protocol, and register for the \r
827 critical events required to coordinate between DXE and SMM environments.\r
828 \r
829 @param ImageHandle The firmware allocated handle for the EFI image.\r
830 @param SystemTable A pointer to the EFI System Table.\r
831\r
832 @retval EFI_SUCCESS The entry point is executed successfully.\r
833 @retval Other Some error occurred when executing this entry point.\r
834\r
835**/\r
836EFI_STATUS\r
837EFIAPI\r
838SmmIplEntry (\r
839 IN EFI_HANDLE ImageHandle,\r
840 IN EFI_SYSTEM_TABLE *SystemTable\r
841 )\r
842{\r
843 EFI_STATUS Status;\r
844 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
845 UINTN Size;\r
846 UINTN Index;\r
847 EFI_SMM_RESERVED_SMRAM_REGION *SmramResRegion;\r
848 UINT64 MaxSize;\r
849 VOID *Registration;\r
850\r
851 //\r
852 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the \r
853 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded \r
854 // by the SMM Core\r
855 //\r
856 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r
857\r
858 //\r
859 // Get SMM Access Protocol\r
860 //\r
861 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);\r
862 ASSERT_EFI_ERROR (Status);\r
863\r
864 //\r
865 // Get SMM Control2 Protocol\r
866 //\r
867 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);\r
868 ASSERT_EFI_ERROR (Status);\r
869\r
870 //\r
871 // Get SMM Configuration Protocol if it is present\r
872 //\r
873 SmmConfiguration = NULL;\r
874 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);\r
875\r
876 //\r
877 // Get SMRAM information\r
878 //\r
879 Size = 0;\r
880 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);\r
881 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
882\r
883 gSmmCorePrivate->SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);\r
884 ASSERT (gSmmCorePrivate->SmramRanges != NULL);\r
885\r
886 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, gSmmCorePrivate->SmramRanges);\r
887 ASSERT_EFI_ERROR (Status);\r
888\r
889 gSmmCorePrivate->SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
890\r
891 //\r
892 // Open all SMRAM ranges\r
893 //\r
894 Status = mSmmAccess->Open (mSmmAccess);\r
895 ASSERT_EFI_ERROR (Status);\r
896\r
897 //\r
898 // Print debug message that the SMRAM window is now open.\r
899 //\r
900 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r
901\r
902 //\r
903 // Subtract SMRAM any reserved SMRAM regions.\r
904 //\r
905 if (SmmConfiguration != NULL) {\r
906 SmramResRegion = SmmConfiguration->SmramReservedRegions;\r
907 while (SmramResRegion->SmramReservedSize != 0) {\r
908 for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index ++) {\r
909 if ((SmramResRegion->SmramReservedStart >= gSmmCorePrivate->SmramRanges[Index].CpuStart) && \\r
910 ((SmramResRegion->SmramReservedStart + SmramResRegion->SmramReservedSize) <= \\r
911 (gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize))) {\r
912 //\r
913 // This range has reserved area, calculate the left free size\r
914 //\r
915 gSmmCorePrivate->SmramRanges[Index].PhysicalSize = SmramResRegion->SmramReservedStart - gSmmCorePrivate->SmramRanges[Index].CpuStart;\r
916 }\r
917 }\r
918 SmramResRegion++;\r
919 }\r
920 }\r
921 \r
922 //\r
06b07ce3 923 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r
e42e9404 924 //\r
925 mCurrentSmramRange = NULL;\r
06b07ce3 926 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
e42e9404 927 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r
928 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {\r
929 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r
930 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
931 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r
932 }\r
933 }\r
934 }\r
935 }\r
936\r
937 if (mCurrentSmramRange != NULL) {\r
938 //\r
939 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r
940 //\r
941 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n", \r
942 (VOID *)(UINTN)mCurrentSmramRange->CpuStart, \r
943 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r
944 ));\r
945\r
946 //\r
947 // Attempt to set SMRAM cacheability to WB\r
948 //\r
949 Status = gDS->SetMemorySpaceAttributes(\r
950 mCurrentSmramRange->CpuStart, \r
951 mCurrentSmramRange->PhysicalSize,\r
952 EFI_MEMORY_WB\r
953 );\r
954 if (EFI_ERROR (Status)) {\r
955 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r
956 } \r
957\r
958 //\r
959 // Load SMM Core into SMRAM and execute it from SMRAM\r
960 //\r
961 Status = ExecuteSmmCoreFromSmram (mCurrentSmramRange, gSmmCorePrivate);\r
962 if (EFI_ERROR (Status)) {\r
963 //\r
964 // Print error message that the SMM Core failed to be loaded and executed.\r
965 //\r
966 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));\r
967\r
968 //\r
969 // Attempt to reset SMRAM cacheability to UC\r
970 //\r
971 Status = gDS->SetMemorySpaceAttributes(\r
972 mCurrentSmramRange->CpuStart, \r
973 mCurrentSmramRange->PhysicalSize,\r
974 EFI_MEMORY_UC\r
975 );\r
976 if (EFI_ERROR (Status)) {\r
977 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
978 } \r
979 }\r
980 } else {\r
981 //\r
982 // Print error message that there are not enough SMRAM resources to load the SMM Core.\r
983 //\r
984 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));\r
985 }\r
986\r
987 //\r
988 // If the SMM Core could not be loaded then close SMRAM window, free allocated \r
989 // resources, and return an error so SMM IPL will be unloaded.\r
990 //\r
991 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {\r
992 //\r
993 // Close all SMRAM ranges\r
994 //\r
995 Status = mSmmAccess->Close (mSmmAccess);\r
996 ASSERT_EFI_ERROR (Status);\r
997\r
998 //\r
999 // Print debug message that the SMRAM window is now closed.\r
1000 //\r
1001 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
1002\r
1003 //\r
1004 // Free all allocated resources\r
1005 //\r
1006 FreePool (gSmmCorePrivate->SmramRanges);\r
1007 \r
1008 return EFI_UNSUPPORTED;\r
1009 }\r
1010 \r
1011 //\r
1012 // Install SMM Base2 Protocol and SMM Communication Protocol\r
1013 //\r
1014 Status = gBS->InstallMultipleProtocolInterfaces (\r
1015 &mSmmIplHandle,\r
1016 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,\r
1017 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,\r
1018 NULL\r
1019 );\r
1020 ASSERT_EFI_ERROR (Status);\r
1021\r
1022 //\r
1023 // Create the set of protocol and event notififcations that the SMM IPL requires\r
1024 //\r
1025 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
1026 if (mSmmIplEvents[Index].Protocol) {\r
1027 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (\r
1028 mSmmIplEvents[Index].Guid,\r
1029 TPL_CALLBACK,\r
1030 mSmmIplEvents[Index].NotifyFunction,\r
1031 mSmmIplEvents[Index].NotifyContext,\r
1032 &Registration\r
1033 );\r
1034 } else {\r
1035 Status = gBS->CreateEventEx (\r
1036 EVT_NOTIFY_SIGNAL,\r
1037 TPL_CALLBACK,\r
1038 mSmmIplEvents[Index].NotifyFunction,\r
1039 mSmmIplEvents[Index].NotifyContext,\r
1040 mSmmIplEvents[Index].Guid,\r
1041 &mSmmIplEvents[Index].Event\r
1042 );\r
1043 ASSERT_EFI_ERROR (Status);\r
1044 }\r
1045 }\r
1046\r
1047 return EFI_SUCCESS;\r
1048}\r