]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
MdeModulePkg: Refine casting expression result to bigger size
[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
4b5d69d3 4 Copyright (c) 2009 - 2017, 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
2048c585 272 // Declare protocol notification on the SMM Configuration protocol. When this notification is established,\r
e42e9404 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
2048c585 284 // Declare event notification on EndOfDxe event. When this notification is established,\r
46ece1ff
JY
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
4b5d69d3 524 // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.\r
e42e9404 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
2048c585 813 Get the fixed loading address from image header assigned by build tool. This function only be called\r
3c447c27 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
2048c585 819 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.\r
3c447c27 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
2048c585 830 EFI_PHYSICAL_ADDRESS FixLoadingAddress;\r
3c447c27 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
2048c585 842 FixLoadingAddress = 0;\r
3c447c27 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
16f69227
HW
849 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +\r
850 sizeof (UINT32) +\r
851 sizeof (EFI_IMAGE_FILE_HEADER) +\r
852 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r
3c447c27 853 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
854\r
855 //\r
856 // Get base address from the first section header that doesn't point to code section.\r
857 //\r
858 for (Index = 0; Index < NumberOfSections; Index++) {\r
859 //\r
860 // Read section header from file\r
861 //\r
862 Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
863 Status = ImageContext->ImageRead (\r
864 ImageContext->Handle,\r
865 SectionHeaderOffset,\r
866 &Size,\r
867 &SectionHeader\r
868 );\r
869 if (EFI_ERROR (Status)) {\r
870 return Status;\r
871 }\r
872 \r
873 Status = EFI_NOT_FOUND;\r
874 \r
875 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
876 //\r
877 // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the\r
878 // first section header that doesn't point to code section in image header. And there is an assumption that when the\r
879 // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers\r
2048c585 880 // fields should NOT be Zero, or else, these 2 fields should be set to Zero\r
3c447c27 881 //\r
882 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
883 if (ValueInSectionHeader != 0) {\r
884 //\r
2048c585 885 // Found first section header that doesn't point to code section in which build tool saves the\r
3c447c27 886 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
887 //\r
2048c585 888 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r
3c447c27 889\r
2048c585 890 if (SmramBase + SmmCodeSize > FixLoadingAddress && SmramBase <= FixLoadingAddress) {\r
3c447c27 891 //\r
2048c585 892 // The assigned address is valid. Return the specified loading address\r
3c447c27 893 //\r
2048c585 894 ImageContext->ImageAddress = FixLoadingAddress;\r
3c447c27 895 Status = EFI_SUCCESS;\r
896 }\r
897 }\r
898 break;\r
899 }\r
900 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
901 }\r
2048c585 902 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress, Status));\r
3c447c27 903 return Status;\r
904}\r
e42e9404 905/**\r
906 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r
907\r
c03beb76
SZ
908 @param[in, out] SmramRange Descriptor for the range of SMRAM to reload the \r
909 currently executing image, the rang of SMRAM to\r
910 hold SMM Core will be excluded.\r
911 @param[in, out] SmramRangeSmmCore Descriptor for the range of SMRAM to hold SMM Core.\r
912\r
913 @param[in] Context Context to pass into SMM Core\r
e42e9404 914\r
915 @return EFI_STATUS\r
916\r
917**/\r
918EFI_STATUS\r
919ExecuteSmmCoreFromSmram (\r
c03beb76
SZ
920 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRange,\r
921 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRangeSmmCore,\r
922 IN VOID *Context\r
e42e9404 923 )\r
924{\r
925 EFI_STATUS Status;\r
926 VOID *SourceBuffer;\r
927 UINTN SourceSize;\r
928 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
929 UINTN PageCount;\r
e42e9404 930 EFI_IMAGE_ENTRY_POINT EntryPoint;\r
931\r
932 //\r
933 // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE\r
934 // \r
d7aaf1dc
LG
935 Status = GetSectionFromAnyFvByFileType (\r
936 EFI_FV_FILETYPE_SMM_CORE, \r
937 0,\r
938 EFI_SECTION_PE32, \r
939 0,\r
940 &SourceBuffer, \r
941 &SourceSize\r
942 );\r
943 if (EFI_ERROR (Status)) {\r
944 return Status;\r
e42e9404 945 }\r
946 \r
947 //\r
948 // Initilize ImageContext\r
949 //\r
950 ImageContext.Handle = SourceBuffer;\r
951 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
952\r
953 //\r
954 // Get information about the image being loaded\r
955 //\r
956 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
957 if (EFI_ERROR (Status)) {\r
958 return Status;\r
959 }\r
e42e9404 960 //\r
3c447c27 961 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to \r
962 // the address assigned by build tool.\r
e42e9404 963 //\r
3c447c27 964 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
965 //\r
966 // Get the fixed loading address assigned by Build tool\r
967 //\r
968 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);\r
969 if (!EFI_ERROR (Status)) {\r
970 //\r
971 // 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
972 //\r
973 PageCount = 0;\r
a7d10bb0
LG
974 //\r
975 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.\r
976 //\r
977 gSmmCorePrivate->SmramRangeCount --;\r
c03beb76 978 } else {\r
3c447c27 979 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));\r
980 //\r
981 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
982 // specified by SmramRange\r
983 //\r
e0e7f80c 984 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
e42e9404 985\r
3c447c27 986 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
987 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
e42e9404 988\r
3c447c27 989 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
c03beb76
SZ
990 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
991 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;\r
992 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;\r
993 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);\r
e42e9404 994\r
3c447c27 995 //\r
c03beb76 996 // Align buffer on section boundary\r
3c447c27 997 //\r
c03beb76 998 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;\r
3c447c27 999 }\r
1000 } else {\r
1001 //\r
1002 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
1003 // specified by SmramRange\r
1004 //\r
e0e7f80c 1005 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
3c447c27 1006\r
1007 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
1008 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
1009\r
1010 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
c03beb76
SZ
1011 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
1012 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;\r
1013 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;\r
1014 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);\r
3c447c27 1015\r
1016 //\r
c03beb76 1017 // Align buffer on section boundary\r
3c447c27 1018 //\r
c03beb76 1019 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;\r
3c447c27 1020 }\r
1021 \r
e42e9404 1022 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
16f69227 1023 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r
e42e9404 1024\r
1025 //\r
1026 // Print debug message showing SMM Core load address.\r
1027 //\r
1028 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));\r
1029\r
1030 //\r
1031 // Load the image to our new buffer\r
1032 //\r
1033 Status = PeCoffLoaderLoadImage (&ImageContext);\r
1034 if (!EFI_ERROR (Status)) {\r
1035 //\r
1036 // Relocate the image in our new buffer\r
1037 //\r
1038 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
1039 if (!EFI_ERROR (Status)) {\r
1040 //\r
1041 // Flush the instruction cache so the image data are written before we execute it\r
1042 //\r
1043 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
1044\r
1045 //\r
1046 // Print debug message showing SMM Core entry point address.\r
1047 //\r
1048 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));\r
1049\r
84edd20b
SZ
1050 gSmmCorePrivate->PiSmmCoreImageBase = ImageContext.ImageAddress;\r
1051 gSmmCorePrivate->PiSmmCoreImageSize = ImageContext.ImageSize;\r
1052 DEBUG ((DEBUG_INFO, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageBase));\r
1053 DEBUG ((DEBUG_INFO, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageSize));\r
1054\r
1055 gSmmCorePrivate->PiSmmCoreEntryPoint = ImageContext.EntryPoint;\r
1056\r
e42e9404 1057 //\r
1058 // Execute image\r
1059 //\r
1060 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;\r
1061 Status = EntryPoint ((EFI_HANDLE)Context, gST);\r
1062 }\r
1063 }\r
1064\r
1065 //\r
c03beb76 1066 // Always free memory allocted by GetFileBufferByFilePath ()\r
e42e9404 1067 //\r
c03beb76
SZ
1068 FreePool (SourceBuffer);\r
1069\r
1070 return Status;\r
1071}\r
1072\r
1073/**\r
1074 SMM split SMRAM entry.\r
1075\r
1076 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.\r
1077 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.\r
1078 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.\r
1079 @param[in, out] RangeCount Pointer to range count.\r
1080 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.\r
1081 @param[in, out] ReservedRangeCount Pointer to reserved range count.\r
1082 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry\r
1083 that no need to be split anymore.\r
1084 @param[in, out] FinalRangeCount Pointer to final range count.\r
1085\r
1086**/\r
1087VOID\r
1088SmmSplitSmramEntry (\r
1089 IN OUT EFI_SMRAM_DESCRIPTOR *RangeToCompare,\r
1090 IN OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare,\r
1091 OUT EFI_SMRAM_DESCRIPTOR *Ranges,\r
1092 IN OUT UINTN *RangeCount,\r
1093 OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRanges,\r
1094 IN OUT UINTN *ReservedRangeCount,\r
1095 OUT EFI_SMRAM_DESCRIPTOR *FinalRanges,\r
1096 IN OUT UINTN *FinalRangeCount\r
1097 )\r
1098{\r
1099 UINT64 RangeToCompareEnd;\r
1100 UINT64 ReservedRangeToCompareEnd;\r
1101\r
1102 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;\r
1103 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;\r
1104\r
1105 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&\r
1106 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {\r
1107 if (RangeToCompareEnd < ReservedRangeToCompareEnd) {\r
1108 //\r
1109 // RangeToCompare ReservedRangeToCompare\r
1110 // ---- ---- --------------------------------------\r
1111 // | | | | -> 1. ReservedRangeToCompare\r
1112 // ---- | | |--| --------------------------------------\r
1113 // | | | | | |\r
1114 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1115 // | | | | | | RangeToCompare->PhysicalSize = 0\r
1116 // ---- | | |--| --------------------------------------\r
1117 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount\r
1118 // ---- ---- --------------------------------------\r
1119 //\r
1120\r
1121 //\r
1122 // 1. Update ReservedRangeToCompare.\r
1123 //\r
1124 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;\r
1125 //\r
1126 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1127 // Zero RangeToCompare->PhysicalSize.\r
1128 //\r
1129 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;\r
1130 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;\r
1131 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1132 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompare->PhysicalSize;\r
1133 *FinalRangeCount += 1;\r
1134 RangeToCompare->PhysicalSize = 0;\r
1135 //\r
1136 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.\r
1137 //\r
1138 ReservedRanges[*ReservedRangeCount].SmramReservedStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1139 ReservedRanges[*ReservedRangeCount].SmramReservedSize = ReservedRangeToCompareEnd - RangeToCompareEnd;\r
1140 *ReservedRangeCount += 1;\r
1141 } else {\r
1142 //\r
1143 // RangeToCompare ReservedRangeToCompare\r
1144 // ---- ---- --------------------------------------\r
1145 // | | | | -> 1. ReservedRangeToCompare\r
1146 // ---- | | |--| --------------------------------------\r
1147 // | | | | | |\r
1148 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1149 // | | | | | |\r
1150 // | | ---- |--| --------------------------------------\r
1151 // | | | | -> 3. RangeToCompare\r
1152 // ---- ---- --------------------------------------\r
1153 //\r
1154\r
1155 //\r
1156 // 1. Update ReservedRangeToCompare.\r
1157 //\r
1158 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;\r
1159 //\r
1160 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1161 //\r
1162 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;\r
1163 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;\r
1164 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1165 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompareEnd - RangeToCompare->CpuStart;\r
1166 *FinalRangeCount += 1;\r
1167 //\r
1168 // 3. Update RangeToCompare.\r
1169 //\r
1170 RangeToCompare->CpuStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1171 RangeToCompare->PhysicalStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1172 RangeToCompare->PhysicalSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1173 }\r
1174 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&\r
1175 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {\r
1176 if (ReservedRangeToCompareEnd < RangeToCompareEnd) {\r
1177 //\r
1178 // RangeToCompare ReservedRangeToCompare\r
1179 // ---- ---- --------------------------------------\r
1180 // | | | | -> 1. RangeToCompare\r
1181 // | | ---- |--| --------------------------------------\r
1182 // | | | | | |\r
1183 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1184 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0\r
1185 // | | ---- |--| --------------------------------------\r
1186 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount\r
1187 // ---- ---- --------------------------------------\r
1188 //\r
1189\r
1190 //\r
1191 // 1. Update RangeToCompare.\r
1192 //\r
1193 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;\r
1194 //\r
1195 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1196 // ReservedRangeToCompare->SmramReservedSize = 0\r
1197 //\r
1198 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;\r
1199 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;\r
1200 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1201 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompare->SmramReservedSize;\r
1202 *FinalRangeCount += 1;\r
1203 ReservedRangeToCompare->SmramReservedSize = 0;\r
1204 //\r
1205 // 3. Update Ranges[*RangeCount] and increment *RangeCount.\r
1206 //\r
1207 Ranges[*RangeCount].CpuStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1208 Ranges[*RangeCount].PhysicalStart = FinalRanges[*FinalRangeCount - 1].PhysicalStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1209 Ranges[*RangeCount].RegionState = RangeToCompare->RegionState;\r
1210 Ranges[*RangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompareEnd;\r
1211 *RangeCount += 1;\r
1212 } else {\r
1213 //\r
1214 // RangeToCompare ReservedRangeToCompare\r
1215 // ---- ---- --------------------------------------\r
1216 // | | | | -> 1. RangeToCompare\r
1217 // | | ---- |--| --------------------------------------\r
1218 // | | | | | |\r
1219 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1220 // | | | | | |\r
1221 // ---- | | |--| --------------------------------------\r
1222 // | | | | -> 3. ReservedRangeToCompare\r
1223 // ---- ---- --------------------------------------\r
1224 //\r
1225\r
1226 //\r
1227 // 1. Update RangeToCompare.\r
1228 //\r
1229 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;\r
1230 //\r
1231 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1232 // ReservedRangeToCompare->SmramReservedSize = 0\r
1233 //\r
1234 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;\r
1235 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;\r
1236 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1237 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompare->SmramReservedStart;\r
1238 *FinalRangeCount += 1;\r
1239 //\r
1240 // 3. Update ReservedRangeToCompare.\r
1241 //\r
1242 ReservedRangeToCompare->SmramReservedStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1243 ReservedRangeToCompare->SmramReservedSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1244 }\r
1245 }\r
1246}\r
1247\r
1248/**\r
1249 Returns if SMRAM range and SMRAM reserved range are overlapped.\r
1250\r
1251 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.\r
1252 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.\r
1253\r
1254 @retval TRUE There is overlap.\r
1255 @retval FALSE There is no overlap.\r
1256\r
1257**/\r
1258BOOLEAN\r
1259SmmIsSmramOverlap (\r
1260 IN EFI_SMRAM_DESCRIPTOR *RangeToCompare,\r
1261 IN EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare\r
1262 )\r
1263{\r
1264 UINT64 RangeToCompareEnd;\r
1265 UINT64 ReservedRangeToCompareEnd;\r
1266\r
1267 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;\r
1268 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;\r
1269\r
1270 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&\r
1271 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {\r
1272 return TRUE;\r
1273 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&\r
1274 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {\r
1275 return TRUE;\r
e42e9404 1276 }\r
c03beb76
SZ
1277 return FALSE;\r
1278}\r
1279\r
1280/**\r
1281 Get full SMRAM ranges.\r
e42e9404 1282\r
c03beb76
SZ
1283 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from\r
1284 SmmConfiguration protocol, split the entries if there is overlap between them.\r
1285 It will also reserve one entry for SMM core.\r
1286\r
1287 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.\r
1288\r
1289 @return Pointer to full SMRAM ranges.\r
1290\r
1291**/\r
1292EFI_SMRAM_DESCRIPTOR *\r
1293GetFullSmramRanges (\r
1294 OUT UINTN *FullSmramRangeCount\r
1295 )\r
1296{\r
1297 EFI_STATUS Status;\r
1298 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
1299 UINTN Size;\r
1300 UINTN Index;\r
1301 UINTN Index2;\r
1302 EFI_SMRAM_DESCRIPTOR *FullSmramRanges;\r
1303 UINTN TempSmramRangeCount;\r
a7d10bb0 1304 UINTN AdditionSmramRangeCount;\r
c03beb76
SZ
1305 EFI_SMRAM_DESCRIPTOR *TempSmramRanges;\r
1306 UINTN SmramRangeCount;\r
1307 EFI_SMRAM_DESCRIPTOR *SmramRanges;\r
1308 UINTN SmramReservedCount;\r
1309 EFI_SMM_RESERVED_SMRAM_REGION *SmramReservedRanges;\r
1310 UINTN MaxCount;\r
1311 BOOLEAN Rescan;\r
1312\r
1313 //\r
1314 // Get SMM Configuration Protocol if it is present.\r
e42e9404 1315 //\r
c03beb76
SZ
1316 SmmConfiguration = NULL;\r
1317 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);\r
1318\r
e42e9404 1319 //\r
c03beb76
SZ
1320 // Get SMRAM information.\r
1321 //\r
1322 Size = 0;\r
1323 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);\r
1324 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
e42e9404 1325\r
c03beb76
SZ
1326 SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
1327\r
1328 //\r
1329 // Get SMRAM reserved region count.\r
1330 //\r
1331 SmramReservedCount = 0;\r
1332 if (SmmConfiguration != NULL) {\r
1333 while (SmmConfiguration->SmramReservedRegions[SmramReservedCount].SmramReservedSize != 0) {\r
1334 SmramReservedCount++;\r
1335 }\r
1336 }\r
1337\r
a7d10bb0
LG
1338 //\r
1339 // Reserve one entry for SMM Core in the full SMRAM ranges.\r
1340 //\r
1341 AdditionSmramRangeCount = 1;\r
1342 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
1343 //\r
1344 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.\r
1345 //\r
1346 AdditionSmramRangeCount = 2;\r
1347 }\r
1348\r
c03beb76
SZ
1349 if (SmramReservedCount == 0) {\r
1350 //\r
1351 // No reserved SMRAM entry from SMM Configuration Protocol.\r
c03beb76 1352 //\r
a7d10bb0 1353 *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;\r
c03beb76 1354 Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);\r
08308fc9 1355 FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocateZeroPool (Size);\r
c03beb76
SZ
1356 ASSERT (FullSmramRanges != NULL);\r
1357\r
1358 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, FullSmramRanges);\r
1359 ASSERT_EFI_ERROR (Status);\r
1360\r
1361 return FullSmramRanges;\r
1362 }\r
1363\r
1364 //\r
1365 // Why MaxCount = X + 2 * Y?\r
1366 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.\r
1367 //\r
1368 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1\r
1369 // ---- ----\r
1370 // | | ---- |--|\r
1371 // | | | | -> | |\r
1372 // | | ---- |--|\r
1373 // ---- ----\r
1374 //\r
1375 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1\r
1376 // ---- ----\r
1377 // | | | |\r
1378 // | | ---- |--|\r
1379 // | | | | | |\r
1380 // |--| | | -> |--|\r
1381 // | | | | | |\r
1382 // | | ---- |--|\r
1383 // | | | |\r
1384 // ---- ----\r
1385 //\r
1386 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1\r
1387 // ---- ----\r
1388 // | | | |\r
1389 // | | ---- |--|\r
1390 // |--| | | |--|\r
1391 // | | | | -> | |\r
1392 // |--| | | |--|\r
1393 // | | ---- |--|\r
1394 // | | | |\r
1395 // ---- ----\r
1396 //\r
1397 // ......\r
1398 //\r
1399 MaxCount = SmramRangeCount + 2 * SmramReservedCount;\r
1400\r
1401 Size = MaxCount * sizeof (EFI_SMM_RESERVED_SMRAM_REGION);\r
1402 SmramReservedRanges = (EFI_SMM_RESERVED_SMRAM_REGION *) AllocatePool (Size);\r
1403 ASSERT (SmramReservedRanges != NULL);\r
1404 for (Index = 0; Index < SmramReservedCount; Index++) {\r
1405 CopyMem (&SmramReservedRanges[Index], &SmmConfiguration->SmramReservedRegions[Index], sizeof (EFI_SMM_RESERVED_SMRAM_REGION));\r
1406 }\r
1407\r
1408 Size = MaxCount * sizeof (EFI_SMRAM_DESCRIPTOR);\r
1409 TempSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
1410 ASSERT (TempSmramRanges != NULL);\r
1411 TempSmramRangeCount = 0;\r
1412\r
1413 SmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
1414 ASSERT (SmramRanges != NULL);\r
1415 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, SmramRanges);\r
1416 ASSERT_EFI_ERROR (Status);\r
1417\r
1418 do {\r
1419 Rescan = FALSE;\r
1420 for (Index = 0; (Index < SmramRangeCount) && !Rescan; Index++) {\r
1421 //\r
1422 // Skip zero size entry.\r
1423 //\r
1424 if (SmramRanges[Index].PhysicalSize != 0) {\r
1425 for (Index2 = 0; (Index2 < SmramReservedCount) && !Rescan; Index2++) {\r
1426 //\r
1427 // Skip zero size entry.\r
1428 //\r
1429 if (SmramReservedRanges[Index2].SmramReservedSize != 0) {\r
1430 if (SmmIsSmramOverlap (\r
1431 &SmramRanges[Index],\r
1432 &SmramReservedRanges[Index2]\r
1433 )) {\r
1434 //\r
1435 // There is overlap, need to split entry and then rescan.\r
1436 //\r
1437 SmmSplitSmramEntry (\r
1438 &SmramRanges[Index],\r
1439 &SmramReservedRanges[Index2],\r
1440 SmramRanges,\r
1441 &SmramRangeCount,\r
1442 SmramReservedRanges,\r
1443 &SmramReservedCount,\r
1444 TempSmramRanges,\r
1445 &TempSmramRangeCount\r
1446 );\r
1447 Rescan = TRUE;\r
1448 }\r
1449 }\r
1450 }\r
1451 if (!Rescan) {\r
1452 //\r
1453 // No any overlap, copy the entry to the temp SMRAM ranges.\r
1454 // Zero SmramRanges[Index].PhysicalSize = 0;\r
1455 //\r
1456 CopyMem (&TempSmramRanges[TempSmramRangeCount++], &SmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));\r
1457 SmramRanges[Index].PhysicalSize = 0;\r
1458 }\r
1459 }\r
1460 }\r
1461 } while (Rescan);\r
1462 ASSERT (TempSmramRangeCount <= MaxCount);\r
1463\r
1464 //\r
a7d10bb0 1465 // Sort the entries\r
c03beb76 1466 //\r
a7d10bb0 1467 FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));\r
c03beb76
SZ
1468 ASSERT (FullSmramRanges != NULL);\r
1469 *FullSmramRangeCount = 0;\r
1470 do {\r
1471 for (Index = 0; Index < TempSmramRangeCount; Index++) {\r
1472 if (TempSmramRanges[Index].PhysicalSize != 0) {\r
1473 break;\r
1474 }\r
1475 }\r
1476 ASSERT (Index < TempSmramRangeCount);\r
1477 for (Index2 = 0; Index2 < TempSmramRangeCount; Index2++) {\r
1478 if ((Index2 != Index) && (TempSmramRanges[Index2].PhysicalSize != 0) && (TempSmramRanges[Index2].CpuStart < TempSmramRanges[Index].CpuStart)) {\r
1479 Index = Index2;\r
1480 }\r
1481 }\r
1482 CopyMem (&FullSmramRanges[*FullSmramRangeCount], &TempSmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));\r
1483 *FullSmramRangeCount += 1;\r
1484 TempSmramRanges[Index].PhysicalSize = 0;\r
1485 } while (*FullSmramRangeCount < TempSmramRangeCount);\r
1486 ASSERT (*FullSmramRangeCount == TempSmramRangeCount);\r
a7d10bb0 1487 *FullSmramRangeCount += AdditionSmramRangeCount;\r
c03beb76
SZ
1488\r
1489 FreePool (SmramRanges);\r
1490 FreePool (SmramReservedRanges);\r
1491 FreePool (TempSmramRanges);\r
1492\r
1493 return FullSmramRanges;\r
e42e9404 1494}\r
1495\r
1496/**\r
1497 The Entry Point for SMM IPL\r
1498\r
1499 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install \r
1500 SMM Base 2 Protocol and SMM Communication Protocol, and register for the \r
1501 critical events required to coordinate between DXE and SMM environments.\r
1502 \r
1503 @param ImageHandle The firmware allocated handle for the EFI image.\r
1504 @param SystemTable A pointer to the EFI System Table.\r
1505\r
1506 @retval EFI_SUCCESS The entry point is executed successfully.\r
1507 @retval Other Some error occurred when executing this entry point.\r
1508\r
1509**/\r
1510EFI_STATUS\r
1511EFIAPI\r
1512SmmIplEntry (\r
1513 IN EFI_HANDLE ImageHandle,\r
1514 IN EFI_SYSTEM_TABLE *SystemTable\r
1515 )\r
1516{\r
1517 EFI_STATUS Status;\r
e42e9404 1518 UINTN Index;\r
e42e9404 1519 UINT64 MaxSize;\r
1520 VOID *Registration;\r
07d9dc83 1521 UINT64 SmmCodeSize;\r
3c447c27 1522 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *LMFAConfigurationTable;\r
07d9dc83 1523 EFI_CPU_ARCH_PROTOCOL *CpuArch;\r
b07ea4c1 1524 EFI_STATUS SetAttrStatus;\r
a7d10bb0 1525 EFI_SMRAM_DESCRIPTOR *SmramRangeSmmDriver;\r
e42e9404 1526\r
1527 //\r
1528 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the \r
1529 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded \r
1530 // by the SMM Core\r
1531 //\r
1532 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r
1533\r
1534 //\r
1535 // Get SMM Access Protocol\r
1536 //\r
1537 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);\r
1538 ASSERT_EFI_ERROR (Status);\r
1539\r
1540 //\r
1541 // Get SMM Control2 Protocol\r
1542 //\r
1543 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);\r
1544 ASSERT_EFI_ERROR (Status);\r
1545\r
c03beb76 1546 gSmmCorePrivate->SmramRanges = GetFullSmramRanges (&gSmmCorePrivate->SmramRangeCount);\r
84edd20b 1547\r
e42e9404 1548 //\r
1549 // Open all SMRAM ranges\r
1550 //\r
1551 Status = mSmmAccess->Open (mSmmAccess);\r
1552 ASSERT_EFI_ERROR (Status);\r
1553\r
1554 //\r
1555 // Print debug message that the SMRAM window is now open.\r
1556 //\r
1557 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r
e42e9404 1558 \r
1559 //\r
06b07ce3 1560 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r
e42e9404 1561 //\r
1562 mCurrentSmramRange = NULL;\r
06b07ce3 1563 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
2c0f06f0 1564 //\r
1565 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization\r
1566 //\r
1567 if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {\r
1568 continue;\r
1569 }\r
1570\r
e42e9404 1571 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r
10e4e4f6 1572 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {\r
e42e9404 1573 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r
1574 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
1575 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r
1576 }\r
1577 }\r
1578 }\r
1579 }\r
1580\r
1581 if (mCurrentSmramRange != NULL) {\r
1582 //\r
1583 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r
1584 //\r
1585 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n", \r
1586 (VOID *)(UINTN)mCurrentSmramRange->CpuStart, \r
1587 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r
1588 ));\r
1589\r
40e8cca5 1590 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);\r
e42e9404 1591 //\r
07d9dc83 1592 // If CPU AP is present, attempt to set SMRAM cacheability to WB\r
1593 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP\r
1594 // is not available here.\r
e42e9404 1595 //\r
0a6c0905 1596 CpuArch = NULL;\r
07d9dc83 1597 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);\r
1598 if (!EFI_ERROR (Status)) {\r
1599 Status = gDS->SetMemorySpaceAttributes(\r
1600 mSmramCacheBase, \r
1601 mSmramCacheSize,\r
1602 EFI_MEMORY_WB\r
1603 );\r
1604 if (EFI_ERROR (Status)) {\r
1605 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r
1606 } \r
1607 }\r
3c447c27 1608 //\r
1609 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load\r
1610 // Modules At Fixed Address Configuration Table.\r
1611 //\r
1612 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
1613 //\r
1614 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
1615 //\r
1616 SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);\r
1617 //\r
1618 // The SMRAM available memory is assumed to be larger than SmmCodeSize\r
1619 //\r
1620 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);\r
1621 //\r
1622 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.\r
1623 //\r
1624 Status = EfiGetSystemConfigurationTable (\r
1625 &gLoadFixedAddressConfigurationTableGuid,\r
1626 (VOID **) &LMFAConfigurationTable\r
1627 );\r
1628 if (!EFI_ERROR (Status) && LMFAConfigurationTable != NULL) {\r
1629 LMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r
2d5ac154 1630 //\r
1631 // Print the SMRAM base\r
1632 //\r
1633 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable->SmramBase));\r
3c447c27 1634 }\r
a7d10bb0
LG
1635\r
1636 //\r
1637 // Fill the Smram range for all SMM code\r
1638 //\r
1639 SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];\r
1640 SmramRangeSmmDriver->CpuStart = mCurrentSmramRange->CpuStart;\r
1641 SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;\r
1642 SmramRangeSmmDriver->RegionState = mCurrentSmramRange->RegionState | EFI_ALLOCATED;\r
1643 SmramRangeSmmDriver->PhysicalSize = SmmCodeSize;\r
1644\r
1645 mCurrentSmramRange->PhysicalSize -= SmmCodeSize;\r
1646 mCurrentSmramRange->CpuStart = mCurrentSmramRange->CpuStart + SmmCodeSize;\r
1647 mCurrentSmramRange->PhysicalStart = mCurrentSmramRange->PhysicalStart + SmmCodeSize;\r
3c447c27 1648 }\r
e42e9404 1649 //\r
1650 // Load SMM Core into SMRAM and execute it from SMRAM\r
1651 //\r
c03beb76
SZ
1652 Status = ExecuteSmmCoreFromSmram (\r
1653 mCurrentSmramRange,\r
1654 &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 1],\r
1655 gSmmCorePrivate\r
1656 );\r
e42e9404 1657 if (EFI_ERROR (Status)) {\r
1658 //\r
1659 // Print error message that the SMM Core failed to be loaded and executed.\r
1660 //\r
1661 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));\r
1662\r
1663 //\r
1664 // Attempt to reset SMRAM cacheability to UC\r
1665 //\r
0a6c0905 1666 if (CpuArch != NULL) {\r
b07ea4c1
LE
1667 SetAttrStatus = gDS->SetMemorySpaceAttributes(\r
1668 mSmramCacheBase, \r
1669 mSmramCacheSize,\r
1670 EFI_MEMORY_UC\r
1671 );\r
1672 if (EFI_ERROR (SetAttrStatus)) {\r
0a6c0905 1673 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
1674 } \r
1675 }\r
e42e9404 1676 }\r
1677 } else {\r
1678 //\r
1679 // Print error message that there are not enough SMRAM resources to load the SMM Core.\r
1680 //\r
1681 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));\r
1682 }\r
1683\r
1684 //\r
1685 // If the SMM Core could not be loaded then close SMRAM window, free allocated \r
1686 // resources, and return an error so SMM IPL will be unloaded.\r
1687 //\r
1688 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {\r
1689 //\r
1690 // Close all SMRAM ranges\r
1691 //\r
1692 Status = mSmmAccess->Close (mSmmAccess);\r
1693 ASSERT_EFI_ERROR (Status);\r
1694\r
1695 //\r
1696 // Print debug message that the SMRAM window is now closed.\r
1697 //\r
1698 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
1699\r
1700 //\r
1701 // Free all allocated resources\r
1702 //\r
1703 FreePool (gSmmCorePrivate->SmramRanges);\r
63aa86b0 1704\r
e42e9404 1705 return EFI_UNSUPPORTED;\r
1706 }\r
1707 \r
1708 //\r
1709 // Install SMM Base2 Protocol and SMM Communication Protocol\r
1710 //\r
1711 Status = gBS->InstallMultipleProtocolInterfaces (\r
1712 &mSmmIplHandle,\r
1713 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,\r
1714 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,\r
1715 NULL\r
1716 );\r
1717 ASSERT_EFI_ERROR (Status);\r
1718\r
1719 //\r
1720 // Create the set of protocol and event notififcations that the SMM IPL requires\r
1721 //\r
1722 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
1723 if (mSmmIplEvents[Index].Protocol) {\r
1724 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (\r
1725 mSmmIplEvents[Index].Guid,\r
5657b268 1726 mSmmIplEvents[Index].NotifyTpl,\r
e42e9404 1727 mSmmIplEvents[Index].NotifyFunction,\r
1728 mSmmIplEvents[Index].NotifyContext,\r
1729 &Registration\r
1730 );\r
1731 } else {\r
1732 Status = gBS->CreateEventEx (\r
1733 EVT_NOTIFY_SIGNAL,\r
5657b268 1734 mSmmIplEvents[Index].NotifyTpl,\r
e42e9404 1735 mSmmIplEvents[Index].NotifyFunction,\r
1736 mSmmIplEvents[Index].NotifyContext,\r
1737 mSmmIplEvents[Index].Guid,\r
1738 &mSmmIplEvents[Index].Event\r
1739 );\r
1740 ASSERT_EFI_ERROR (Status);\r
1741 }\r
1742 }\r
1743\r
1744 return EFI_SUCCESS;\r
1745}\r