]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
Merge branch 'master' of https://github.com/tianocore/edk2
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmCore.c
CommitLineData
e42e9404 1/** @file\r
2 SMM Core Main Entry Point\r
3\r
e524f680 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 "PiSmmCore.h"\r
16\r
17//\r
18// Physical pointer to private structure shared between SMM IPL and the SMM Core\r
19//\r
20SMM_CORE_PRIVATE_DATA *gSmmCorePrivate;\r
21\r
22//\r
23// SMM Core global variable for SMM System Table. Only accessed as a physical structure in SMRAM.\r
24//\r
25EFI_SMM_SYSTEM_TABLE2 gSmmCoreSmst = {\r
26 {\r
27 SMM_SMST_SIGNATURE,\r
28 EFI_SMM_SYSTEM_TABLE2_REVISION,\r
29 sizeof (gSmmCoreSmst.Hdr)\r
30 },\r
31 NULL, // SmmFirmwareVendor\r
32 0, // SmmFirmwareRevision\r
33 SmmInstallConfigurationTable,\r
34 {\r
35 {\r
36 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5, // SmmMemRead\r
37 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5 // SmmMemWrite\r
38 },\r
39 {\r
40 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5, // SmmIoRead\r
41 (EFI_SMM_CPU_IO2) SmmEfiNotAvailableYetArg5 // SmmIoWrite\r
42 }\r
43 },\r
44 SmmAllocatePool,\r
45 SmmFreePool,\r
46 SmmAllocatePages,\r
47 SmmFreePages,\r
48 NULL, // SmmStartupThisAp\r
49 0, // CurrentlyExecutingCpu\r
50 0, // NumberOfCpus\r
51 NULL, // CpuSaveStateSize\r
52 NULL, // CpuSaveState\r
53 0, // NumberOfTableEntries\r
54 NULL, // SmmConfigurationTable\r
55 SmmInstallProtocolInterface,\r
56 SmmUninstallProtocolInterface,\r
57 SmmHandleProtocol,\r
58 SmmRegisterProtocolNotify,\r
59 SmmLocateHandle,\r
60 SmmLocateProtocol,\r
61 SmiManage,\r
62 SmiHandlerRegister,\r
63 SmiHandlerUnRegister\r
64};\r
65\r
66//\r
67// Flag to determine if the platform has performed a legacy boot.\r
68// If this flag is TRUE, then the runtime code and runtime data associated with the \r
53ec4d7f 69// SMM IPL are converted to free memory, so the SMM Core must guarantee that is\r
e42e9404 70// does not touch of the code/data associated with the SMM IPL if this flag is TRUE.\r
71//\r
72BOOLEAN mInLegacyBoot = FALSE;\r
73\r
74//\r
75// Table of SMI Handlers that are registered by the SMM Core when it is initialized\r
76//\r
77SMM_CORE_SMI_HANDLERS mSmmCoreSmiHandlers[] = {\r
53ec4d7f
SZ
78 { SmmDriverDispatchHandler, &gEfiEventDxeDispatchGuid, NULL, TRUE },\r
79 { SmmReadyToLockHandler, &gEfiDxeSmmReadyToLockProtocolGuid, NULL, TRUE }, \r
80 { SmmLegacyBootHandler, &gEfiEventLegacyBootGuid, NULL, FALSE },\r
81 { SmmExitBootServicesHandler, &gEfiEventExitBootServicesGuid, NULL, FALSE },\r
82 { SmmReadyToBootHandler, &gEfiEventReadyToBootGuid, NULL, FALSE },\r
dc157845 83 { SmmEndOfDxeHandler, &gEfiEndOfDxeEventGroupGuid, NULL, TRUE },\r
53ec4d7f 84 { NULL, NULL, NULL, FALSE }\r
e42e9404 85};\r
86\r
84edd20b
SZ
87UINTN mFullSmramRangeCount;\r
88EFI_SMRAM_DESCRIPTOR *mFullSmramRanges;\r
89\r
0b256fb1
JY
90EFI_LOADED_IMAGE_PROTOCOL *mSmmCoreLoadedImage;\r
91\r
e42e9404 92/**\r
93 Place holder function until all the SMM System Table Service are available.\r
94\r
95 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.\r
96\r
97 @param Arg1 Undefined\r
98 @param Arg2 Undefined\r
99 @param Arg3 Undefined\r
100 @param Arg4 Undefined\r
101 @param Arg5 Undefined\r
102\r
103 @return EFI_NOT_AVAILABLE_YET\r
104\r
105**/\r
106EFI_STATUS\r
107EFIAPI\r
108SmmEfiNotAvailableYetArg5 (\r
109 UINTN Arg1,\r
110 UINTN Arg2,\r
111 UINTN Arg3,\r
112 UINTN Arg4,\r
113 UINTN Arg5\r
114 )\r
115{\r
116 //\r
117 // This function should never be executed. If it does, then the architectural protocols\r
118 // have not been designed correctly.\r
119 //\r
120 return EFI_NOT_AVAILABLE_YET;\r
121}\r
122\r
123/**\r
124 Software SMI handler that is called when a Legacy Boot event is signalled. The SMM\r
125 Core uses this signal to know that a Legacy Boot has been performed and that \r
126 gSmmCorePrivate that is shared between the UEFI and SMM execution environments can\r
127 not be accessed from SMM anymore since that structure is considered free memory by\r
53ec4d7f
SZ
128 a legacy OS. Then the SMM Core also install SMM Legacy Boot protocol to notify SMM\r
129 driver that system enter legacy boot.\r
e42e9404 130\r
131 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
132 @param Context Points to an optional handler context which was specified when the handler was registered.\r
133 @param CommBuffer A pointer to a collection of data in memory that will\r
134 be conveyed from a non-SMM environment into an SMM environment.\r
135 @param CommBufferSize The size of the CommBuffer.\r
136\r
137 @return Status Code\r
138\r
139**/\r
140EFI_STATUS\r
141EFIAPI\r
142SmmLegacyBootHandler (\r
143 IN EFI_HANDLE DispatchHandle,\r
144 IN CONST VOID *Context, OPTIONAL\r
145 IN OUT VOID *CommBuffer, OPTIONAL\r
146 IN OUT UINTN *CommBufferSize OPTIONAL\r
147 )\r
148{\r
53ec4d7f
SZ
149 EFI_STATUS Status;\r
150 EFI_HANDLE SmmHandle;\r
151\r
152 //\r
153 // Install SMM Legacy Boot protocol.\r
154 //\r
155 SmmHandle = NULL;\r
156 Status = SmmInstallProtocolInterface (\r
157 &SmmHandle,\r
158 &gEdkiiSmmLegacyBootProtocolGuid,\r
159 EFI_NATIVE_INTERFACE,\r
160 NULL\r
161 );\r
162\r
e42e9404 163 mInLegacyBoot = TRUE;\r
53ec4d7f
SZ
164\r
165 SmiHandlerUnRegister (DispatchHandle);\r
166\r
167 return Status;\r
168}\r
169\r
170/**\r
171 Software SMI handler that is called when an Exit Boot Services event is signalled.\r
172 Then the SMM Core also install SMM Exit Boot Services protocol to notify SMM driver\r
173 that system enter exit boot services.\r
174\r
175 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
176 @param Context Points to an optional handler context which was specified when the handler was registered.\r
177 @param CommBuffer A pointer to a collection of data in memory that will\r
178 be conveyed from a non-SMM environment into an SMM environment.\r
179 @param CommBufferSize The size of the CommBuffer.\r
180\r
181 @return Status Code\r
182\r
183**/\r
184EFI_STATUS\r
185EFIAPI\r
186SmmExitBootServicesHandler (\r
187 IN EFI_HANDLE DispatchHandle,\r
188 IN CONST VOID *Context, OPTIONAL\r
189 IN OUT VOID *CommBuffer, OPTIONAL\r
190 IN OUT UINTN *CommBufferSize OPTIONAL\r
191 )\r
192{\r
193 EFI_STATUS Status;\r
194 EFI_HANDLE SmmHandle;\r
195\r
196 //\r
197 // Install SMM Exit Boot Services protocol.\r
198 //\r
199 SmmHandle = NULL;\r
200 Status = SmmInstallProtocolInterface (\r
201 &SmmHandle,\r
202 &gEdkiiSmmExitBootServicesProtocolGuid,\r
203 EFI_NATIVE_INTERFACE,\r
204 NULL\r
205 );\r
206\r
207 SmiHandlerUnRegister (DispatchHandle);\r
208\r
209 return Status;\r
210}\r
211\r
212/**\r
213 Software SMI handler that is called when an Ready To Boot event is signalled.\r
214 Then the SMM Core also install SMM Ready To Boot protocol to notify SMM driver\r
215 that system enter ready to boot.\r
216\r
217 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
218 @param Context Points to an optional handler context which was specified when the handler was registered.\r
219 @param CommBuffer A pointer to a collection of data in memory that will\r
220 be conveyed from a non-SMM environment into an SMM environment.\r
221 @param CommBufferSize The size of the CommBuffer.\r
222\r
223 @return Status Code\r
224\r
225**/\r
226EFI_STATUS\r
227EFIAPI\r
228SmmReadyToBootHandler (\r
229 IN EFI_HANDLE DispatchHandle,\r
230 IN CONST VOID *Context, OPTIONAL\r
231 IN OUT VOID *CommBuffer, OPTIONAL\r
232 IN OUT UINTN *CommBufferSize OPTIONAL\r
233 )\r
234{\r
235 EFI_STATUS Status;\r
236 EFI_HANDLE SmmHandle;\r
237\r
238 //\r
239 // Install SMM Ready To Boot protocol.\r
240 //\r
241 SmmHandle = NULL;\r
242 Status = SmmInstallProtocolInterface (\r
243 &SmmHandle,\r
244 &gEdkiiSmmReadyToBootProtocolGuid,\r
245 EFI_NATIVE_INTERFACE,\r
246 NULL\r
247 );\r
248\r
249 SmiHandlerUnRegister (DispatchHandle);\r
250\r
251 return Status;\r
e42e9404 252}\r
253\r
254/**\r
255 Software SMI handler that is called when the DxeSmmReadyToLock protocol is added\r
256 or if gEfiEventReadyToBootGuid is signalled. This function unregisters the \r
257 Software SMIs that are nor required after SMRAM is locked and installs the \r
258 SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about \r
259 to be locked. It also verifies the the SMM CPU I/O 2 Protocol has been installed\r
260 and NULLs gBS and gST because they can not longer be used after SMRAM is locked.\r
261\r
262 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
263 @param Context Points to an optional handler context which was specified when the handler was registered.\r
264 @param CommBuffer A pointer to a collection of data in memory that will\r
265 be conveyed from a non-SMM environment into an SMM environment.\r
266 @param CommBufferSize The size of the CommBuffer.\r
267\r
268 @return Status Code\r
269\r
270**/\r
271EFI_STATUS\r
272EFIAPI\r
273SmmReadyToLockHandler (\r
274 IN EFI_HANDLE DispatchHandle,\r
275 IN CONST VOID *Context, OPTIONAL\r
276 IN OUT VOID *CommBuffer, OPTIONAL\r
277 IN OUT UINTN *CommBufferSize OPTIONAL\r
278 )\r
279{\r
280 EFI_STATUS Status;\r
281 UINTN Index;\r
282 EFI_HANDLE SmmHandle;\r
283 VOID *Interface;\r
284\r
285 //\r
286 // Unregister SMI Handlers that are no required after the SMM driver dispatch is stopped\r
287 //\r
288 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
289 if (mSmmCoreSmiHandlers[Index].UnRegister) {\r
290 SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);\r
291 }\r
292 }\r
293\r
294 //\r
295 // Install SMM Ready to lock protocol\r
296 //\r
297 SmmHandle = NULL;\r
298 Status = SmmInstallProtocolInterface (\r
299 &SmmHandle,\r
300 &gEfiSmmReadyToLockProtocolGuid,\r
301 EFI_NATIVE_INTERFACE,\r
302 NULL\r
303 );\r
304\r
305 //\r
306 // Make sure SMM CPU I/O 2 Procol has been installed into the handle database\r
307 //\r
308 Status = SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid, NULL, &Interface);\r
309\r
310 //\r
311 // Print a message on a debug build if the SMM CPU I/O 2 Protocol is not installed\r
312 //\r
313 DEBUG_CODE_BEGIN ();\r
314 if (EFI_ERROR (Status)) {\r
315 DEBUG ((DEBUG_ERROR, "\nSMM: SmmCpuIo Arch Protocol not present!!\n"));\r
316 }\r
317 DEBUG_CODE_END ();\r
318\r
319 //\r
320 // Assert if the CPU I/O 2 Protocol is not installed\r
321 //\r
322 ASSERT_EFI_ERROR (Status);\r
323\r
324 //\r
325 // Display any drivers that were not dispatched because dependency expression\r
326 // evaluated to false if this is a debug build\r
327 //\r
328 DEBUG_CODE_BEGIN ();\r
329 SmmDisplayDiscoveredNotDispatched ();\r
330 DEBUG_CODE_END ();\r
331\r
332 //\r
333 // Not allowed to use gST or gBS after lock\r
334 //\r
335 gST = NULL;\r
336 gBS = NULL;\r
337\r
84edd20b
SZ
338 SmramProfileReadyToLock ();\r
339\r
e42e9404 340 return Status;\r
341}\r
342\r
46ece1ff
JY
343/**\r
344 Software SMI handler that is called when the EndOfDxe event is signalled.\r
345 This function installs the SMM EndOfDxe Protocol so SMM Drivers are informed that\r
346 platform code will invoke 3rd part code.\r
347\r
348 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
349 @param Context Points to an optional handler context which was specified when the handler was registered.\r
350 @param CommBuffer A pointer to a collection of data in memory that will\r
351 be conveyed from a non-SMM environment into an SMM environment.\r
352 @param CommBufferSize The size of the CommBuffer.\r
353\r
354 @return Status Code\r
355\r
356**/\r
357EFI_STATUS\r
358EFIAPI\r
359SmmEndOfDxeHandler (\r
360 IN EFI_HANDLE DispatchHandle,\r
361 IN CONST VOID *Context, OPTIONAL\r
362 IN OUT VOID *CommBuffer, OPTIONAL\r
363 IN OUT UINTN *CommBufferSize OPTIONAL\r
364 )\r
365{\r
366 EFI_STATUS Status;\r
367 EFI_HANDLE SmmHandle;\r
368\r
369 DEBUG ((EFI_D_INFO, "SmmEndOfDxeHandler\n"));\r
370 //\r
371 // Install SMM EndOfDxe protocol\r
372 //\r
373 SmmHandle = NULL;\r
374 Status = SmmInstallProtocolInterface (\r
375 &SmmHandle,\r
376 &gEfiSmmEndOfDxeProtocolGuid,\r
377 EFI_NATIVE_INTERFACE,\r
378 NULL\r
379 );\r
42e2ff2e 380 return Status;\r
46ece1ff
JY
381}\r
382\r
3b657538
SZ
383/**\r
384 Determine if two buffers overlap in memory.\r
385\r
386 @param[in] Buff1 Pointer to first buffer\r
387 @param[in] Size1 Size of Buff1\r
388 @param[in] Buff2 Pointer to second buffer\r
389 @param[in] Size2 Size of Buff2\r
390\r
391 @retval TRUE Buffers overlap in memory.\r
392 @retval FALSE Buffer doesn't overlap.\r
393\r
394**/\r
395BOOLEAN\r
396InternalIsBufferOverlapped (\r
397 IN UINT8 *Buff1,\r
398 IN UINTN Size1,\r
399 IN UINT8 *Buff2,\r
400 IN UINTN Size2\r
401 )\r
402{\r
403 //\r
404 // If buff1's end is less than the start of buff2, then it's ok.\r
405 // Also, if buff1's start is beyond buff2's end, then it's ok.\r
406 //\r
407 if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) {\r
408 return FALSE;\r
409 }\r
410\r
411 return TRUE;\r
412}\r
413\r
e42e9404 414/**\r
415 The main entry point to SMM Foundation.\r
416\r
417 Note: This function is only used by SMRAM invocation. It is never used by DXE invocation.\r
418\r
419 @param SmmEntryContext Processor information and functionality\r
420 needed by SMM Foundation.\r
421\r
422**/\r
423VOID\r
424EFIAPI\r
425SmmEntryPoint (\r
426 IN CONST EFI_SMM_ENTRY_CONTEXT *SmmEntryContext\r
427)\r
428{\r
429 EFI_STATUS Status;\r
430 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r
a25cb9f6 431 BOOLEAN InLegacyBoot;\r
3b657538 432 BOOLEAN IsOverlapped;\r
e42e9404 433\r
495797c5 434 PERF_START (NULL, "SMM", NULL, 0) ;\r
435\r
e42e9404 436 //\r
229fd9e7 437 // Update SMST with contents of the SmmEntryContext structure\r
e42e9404 438 //\r
229fd9e7
MK
439 gSmmCoreSmst.SmmStartupThisAp = SmmEntryContext->SmmStartupThisAp;\r
440 gSmmCoreSmst.CurrentlyExecutingCpu = SmmEntryContext->CurrentlyExecutingCpu;\r
441 gSmmCoreSmst.NumberOfCpus = SmmEntryContext->NumberOfCpus;\r
442 gSmmCoreSmst.CpuSaveStateSize = SmmEntryContext->CpuSaveStateSize;\r
443 gSmmCoreSmst.CpuSaveState = SmmEntryContext->CpuSaveState;\r
e42e9404 444\r
495797c5 445 //\r
446 // Call platform hook before Smm Dispatch\r
447 //\r
448 PlatformHookBeforeSmmDispatch ();\r
449\r
e42e9404 450 //\r
451 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
452 //\r
a25cb9f6 453 InLegacyBoot = mInLegacyBoot;\r
454 if (!InLegacyBoot) {\r
e42e9404 455 //\r
9fa90bb4 456 // Mark the InSmm flag as TRUE, it will be used by SmmBase2 protocol\r
e42e9404 457 //\r
9fa90bb4 458 gSmmCorePrivate->InSmm = TRUE;\r
e42e9404 459\r
460 //\r
9fa90bb4 461 // Check to see if this is a Synchronous SMI sent through the SMM Communication \r
462 // Protocol or an Asynchronous SMI\r
e42e9404 463 //\r
9fa90bb4 464 if (gSmmCorePrivate->CommunicationBuffer != NULL) {\r
465 //\r
466 // Synchronous SMI for SMM Core or request from Communicate protocol\r
467 //\r
3b657538
SZ
468 IsOverlapped = InternalIsBufferOverlapped (\r
469 (UINT8 *) gSmmCorePrivate->CommunicationBuffer,\r
470 gSmmCorePrivate->BufferSize,\r
471 (UINT8 *) gSmmCorePrivate,\r
472 sizeof (*gSmmCorePrivate)\r
473 );\r
474 if (!SmmIsBufferOutsideSmmValid ((UINTN)gSmmCorePrivate->CommunicationBuffer, gSmmCorePrivate->BufferSize) || IsOverlapped) {\r
3720ee6d 475 //\r
3b657538
SZ
476 // If CommunicationBuffer is not in valid address scope,\r
477 // or there is overlap between gSmmCorePrivate and CommunicationBuffer,\r
478 // return EFI_INVALID_PARAMETER\r
3720ee6d
JF
479 //\r
480 gSmmCorePrivate->CommunicationBuffer = NULL;\r
481 gSmmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;\r
482 } else {\r
483 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer;\r
484 gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
485 Status = SmiManage (\r
486 &CommunicateHeader->HeaderGuid, \r
487 NULL, \r
488 CommunicateHeader->Data, \r
489 &gSmmCorePrivate->BufferSize\r
490 );\r
491 //\r
492 // Update CommunicationBuffer, BufferSize and ReturnStatus\r
493 // Communicate service finished, reset the pointer to CommBuffer to NULL\r
494 //\r
495 gSmmCorePrivate->BufferSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
496 gSmmCorePrivate->CommunicationBuffer = NULL;\r
497 gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
498 }\r
9fa90bb4 499 }\r
e42e9404 500 }\r
9fa90bb4 501\r
502 //\r
503 // Process Asynchronous SMI sources\r
504 //\r
505 SmiManage (NULL, NULL, NULL, NULL);\r
495797c5 506 \r
507 //\r
508 // Call platform hook after Smm Dispatch\r
509 //\r
510 PlatformHookAfterSmmDispatch ();\r
e42e9404 511\r
512 //\r
9fa90bb4 513 // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
e42e9404 514 //\r
a25cb9f6 515 if (!InLegacyBoot) {\r
9fa90bb4 516 //\r
517 // Clear the InSmm flag as we are going to leave SMM\r
518 //\r
519 gSmmCorePrivate->InSmm = FALSE;\r
520 }\r
495797c5 521\r
522 PERF_END (NULL, "SMM", NULL, 0) ;\r
e42e9404 523}\r
524\r
0b256fb1
JY
525/**\r
526 Install LoadedImage protocol for SMM Core.\r
527**/\r
528VOID\r
529SmmCoreInstallLoadedImage (\r
530 VOID\r
531 )\r
532{\r
533 EFI_STATUS Status;\r
534 EFI_HANDLE Handle;\r
535\r
536 //\r
537 // Allocate a Loaded Image Protocol in EfiBootServicesData\r
538 //\r
539 Status = gBS->AllocatePool (EfiBootServicesData, sizeof(EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&mSmmCoreLoadedImage);\r
540 ASSERT_EFI_ERROR (Status);\r
541\r
542 ZeroMem (mSmmCoreLoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));\r
543 //\r
544 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
545 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.\r
546 //\r
547 mSmmCoreLoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
548 mSmmCoreLoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
549 mSmmCoreLoadedImage->SystemTable = gST;\r
550\r
551 mSmmCoreLoadedImage->ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;\r
552 mSmmCoreLoadedImage->ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;\r
553 mSmmCoreLoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
554 mSmmCoreLoadedImage->ImageDataType = EfiRuntimeServicesData;\r
555\r
556 //\r
557 // Create a new image handle in the UEFI handle database for the SMM Driver\r
558 //\r
559 Handle = NULL;\r
560 Status = gBS->InstallMultipleProtocolInterfaces (\r
561 &Handle,\r
562 &gEfiLoadedImageProtocolGuid, mSmmCoreLoadedImage,\r
563 NULL\r
564 );\r
565 ASSERT_EFI_ERROR (Status);\r
566\r
567 return ;\r
568}\r
569\r
e42e9404 570/**\r
571 The Entry Point for SMM Core\r
572\r
573 Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core \r
574 EntryPoint on the SMI vector.\r
575\r
576 Note: This function is called for both DXE invocation and SMRAM invocation.\r
577\r
578 @param ImageHandle The firmware allocated handle for the EFI image.\r
579 @param SystemTable A pointer to the EFI System Table.\r
580\r
581 @retval EFI_SUCCESS The entry point is executed successfully.\r
582 @retval Other Some error occurred when executing this entry point.\r
583\r
584**/\r
585EFI_STATUS\r
586EFIAPI\r
587SmmMain (\r
588 IN EFI_HANDLE ImageHandle,\r
589 IN EFI_SYSTEM_TABLE *SystemTable\r
590 )\r
591{\r
592 EFI_STATUS Status;\r
593 UINTN Index;\r
594\r
595 //\r
596 // Get SMM Core Private context passed in from SMM IPL in ImageHandle.\r
597 //\r
598 gSmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;\r
599\r
600 //\r
601 // Fill in SMRAM physical address for the SMM Services Table and the SMM Entry Point.\r
602 //\r
603 gSmmCorePrivate->Smst = &gSmmCoreSmst;\r
604 gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;\r
605 \r
606 //\r
842b1242
JY
607 // No need to initialize memory service.\r
608 // It is done in constructor of PiSmmCoreMemoryAllocationLib(),\r
609 // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.\r
e42e9404 610 //\r
e42e9404 611\r
84edd20b
SZ
612 SmramProfileInit ();\r
613\r
614 //\r
615 // Copy FullSmramRanges to SMRAM\r
616 //\r
c03beb76 617 mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;\r
84edd20b
SZ
618 mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
619 ASSERT (mFullSmramRanges != NULL);\r
c03beb76 620 CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
84edd20b 621\r
e42e9404 622 //\r
623 // Register all SMI Handlers required by the SMM Core\r
624 //\r
625 for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
626 Status = SmiHandlerRegister (\r
627 mSmmCoreSmiHandlers[Index].Handler,\r
628 mSmmCoreSmiHandlers[Index].HandlerType,\r
629 &mSmmCoreSmiHandlers[Index].DispatchHandle\r
630 );\r
631 ASSERT_EFI_ERROR (Status);\r
632 }\r
84edd20b
SZ
633\r
634 RegisterSmramProfileHandler ();\r
e524f680 635 SmramProfileInstallProtocol ();\r
84edd20b 636\r
0b256fb1
JY
637 SmmCoreInstallLoadedImage ();\r
638\r
e42e9404 639 return EFI_SUCCESS;\r
640}\r