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