]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
MdeModulePkg PiSmmCore: Retrieve Smram base address from system table
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmIpl.c
... / ...
CommitLineData
1/** @file\r
2 SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM\r
3\r
4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials are licensed and made available \r
6 under the terms and conditions of the BSD License which accompanies this \r
7 distribution. The full text of the license may be found at \r
8 http://opensource.org/licenses/bsd-license.php \r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16\r
17#include <Protocol/SmmBase2.h>\r
18#include <Protocol/SmmCommunication.h>\r
19#include <Protocol/SmmAccess2.h>\r
20#include <Protocol/SmmConfiguration.h>\r
21#include <Protocol/SmmControl2.h>\r
22#include <Protocol/DxeSmmReadyToLock.h>\r
23#include <Protocol/Cpu.h>\r
24\r
25#include <Guid/EventGroup.h>\r
26#include <Guid/EventLegacyBios.h>\r
27#include <Guid/LoadModuleAtFixedAddress.h>\r
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
37#include <Library/DxeServicesLib.h>\r
38#include <Library/UefiLib.h>\r
39#include <Library/UefiRuntimeLib.h>\r
40#include <Library/PcdLib.h>\r
41#include <Library/ReportStatusCodeLib.h>\r
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
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
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
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
158**/\r
159VOID\r
160EFIAPI\r
161SmmIplGuidedEventNotify (\r
162 IN EFI_EVENT Event,\r
163 IN VOID *Context\r
164 );\r
165\r
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
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
207 EFI_TPL NotifyTpl;\r
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
244 NULL, // CommunicationBuffer\r
245 0, // BufferSize\r
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
261BOOLEAN mEndOfDxe = FALSE;\r
262EFI_PHYSICAL_ADDRESS mSmramCacheBase;\r
263UINT64 mSmramCacheSize;\r
264\r
265EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader;\r
266\r
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 established,\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
276 { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, TPL_NOTIFY, NULL },\r
277 //\r
278 // Declare protocol notification on DxeSmmReadyToLock protocols. When this notification is established, \r
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
282 { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, TPL_CALLBACK, NULL },\r
283 //\r
284 // Declare event notification on EndOfDxe event. When this notification is established,\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
288 { FALSE, TRUE, &gEfiEndOfDxeEventGroupGuid, SmmIplGuidedEventNotify, &gEfiEndOfDxeEventGroupGuid, TPL_CALLBACK, NULL },\r
289 //\r
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
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
298 { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplDxeDispatchEventNotify, &gEfiEventDxeDispatchGuid, TPL_CALLBACK, NULL },\r
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
303 { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, TPL_CALLBACK, NULL },\r
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
308 // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.\r
309 //\r
310 { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, TPL_CALLBACK, NULL },\r
311 //\r
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
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
325 { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, TPL_CALLBACK, NULL },\r
326 //\r
327 // Terminate the table of event notifications\r
328 //\r
329 { FALSE, FALSE, NULL, NULL, NULL, TPL_CALLBACK, NULL }\r
330};\r
331\r
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
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
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
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
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
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
483 gSmmCorePrivate->BufferSize = *CommSize;\r
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
496 *CommSize = gSmmCorePrivate->BufferSize;\r
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
510 //\r
511 // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked.\r
512 //\r
513 if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) {\r
514 return EFI_INVALID_PARAMETER;\r
515 }\r
516 \r
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
546 return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
547}\r
548\r
549/**\r
550 Event notification that is fired when GUIDed Event Group is signaled.\r
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
563 UINTN Size;\r
564\r
565 //\r
566 // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure \r
567 //\r
568 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
569 mCommunicateHeader.MessageLength = 1;\r
570 mCommunicateHeader.Data[0] = 0;\r
571\r
572 //\r
573 // Generate the Software SMI and return the result\r
574 //\r
575 Size = sizeof (mCommunicateHeader);\r
576 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r
577}\r
578\r
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
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
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
622 CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
623 mCommunicateHeader.MessageLength = 1;\r
624 mCommunicateHeader.Data[0] = 0;\r
625\r
626 //\r
627 // Generate the Software SMI and return the result\r
628 //\r
629 Size = sizeof (mCommunicateHeader);\r
630 SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r
631 \r
632 //\r
633 // Return if there is no request to restart the SMM Core Dispatcher\r
634 //\r
635 if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {\r
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
662 }\r
663}\r
664\r
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
697 // Set flag to indicate that the SMM Entry Point has been registered which \r
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
706}\r
707\r
708/**\r
709 Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r
710 or if gEfiEventReadyToBootGuid is signaled.\r
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
745 // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.\r
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
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
760 //\r
761 // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r
762 //\r
763 mSmmAccess->Lock (mSmmAccess);\r
764 \r
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
812/**\r
813 Get the fixed loading address from image header assigned by build tool. This function only be called\r
814 when Loading module at Fixed address feature enabled.\r
815\r
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 loading 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 FixLoadingAddress;\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 FixLoadingAddress = 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 fields 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 build tool saves the\r
888 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
889 //\r
890 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r
891\r
892 if (SmramBase + SmmCodeSize > FixLoadingAddress && SmramBase <= FixLoadingAddress) {\r
893 //\r
894 // The assigned address is valid. Return the specified loading address\r
895 //\r
896 ImageContext->ImageAddress = FixLoadingAddress;\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", FixLoadingAddress, Status));\r
905 return Status;\r
906}\r
907/**\r
908 Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r
909\r
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
916\r
917 @return EFI_STATUS\r
918\r
919**/\r
920EFI_STATUS\r
921ExecuteSmmCoreFromSmram (\r
922 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRange,\r
923 IN OUT EFI_SMRAM_DESCRIPTOR *SmramRangeSmmCore,\r
924 IN VOID *Context\r
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
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
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
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
962 //\r
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
965 //\r
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
976 //\r
977 // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.\r
978 //\r
979 gSmmCorePrivate->SmramRangeCount --;\r
980 } else {\r
981 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));\r
982 //\r
983 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
984 // specified by SmramRange\r
985 //\r
986 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
987\r
988 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
989 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
990\r
991 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
992 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
993 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;\r
994 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;\r
995 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);\r
996\r
997 //\r
998 // Align buffer on section boundary\r
999 //\r
1000 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;\r
1001 }\r
1002 } else {\r
1003 //\r
1004 // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
1005 // specified by SmramRange\r
1006 //\r
1007 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
1008\r
1009 ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r
1010 ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r
1011\r
1012 SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r
1013 SmramRangeSmmCore->CpuStart = SmramRange->CpuStart + SmramRange->PhysicalSize;\r
1014 SmramRangeSmmCore->PhysicalStart = SmramRange->PhysicalStart + SmramRange->PhysicalSize;\r
1015 SmramRangeSmmCore->RegionState = SmramRange->RegionState | EFI_ALLOCATED;\r
1016 SmramRangeSmmCore->PhysicalSize = EFI_PAGES_TO_SIZE (PageCount);\r
1017\r
1018 //\r
1019 // Align buffer on section boundary\r
1020 //\r
1021 ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;\r
1022 }\r
1023 \r
1024 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
1025 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));\r
1026\r
1027 //\r
1028 // Print debug message showing SMM Core load address.\r
1029 //\r
1030 DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));\r
1031\r
1032 //\r
1033 // Load the image to our new buffer\r
1034 //\r
1035 Status = PeCoffLoaderLoadImage (&ImageContext);\r
1036 if (!EFI_ERROR (Status)) {\r
1037 //\r
1038 // Relocate the image in our new buffer\r
1039 //\r
1040 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
1041 if (!EFI_ERROR (Status)) {\r
1042 //\r
1043 // Flush the instruction cache so the image data are written before we execute it\r
1044 //\r
1045 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
1046\r
1047 //\r
1048 // Print debug message showing SMM Core entry point address.\r
1049 //\r
1050 DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));\r
1051\r
1052 gSmmCorePrivate->PiSmmCoreImageBase = ImageContext.ImageAddress;\r
1053 gSmmCorePrivate->PiSmmCoreImageSize = ImageContext.ImageSize;\r
1054 DEBUG ((DEBUG_INFO, "PiSmmCoreImageBase - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageBase));\r
1055 DEBUG ((DEBUG_INFO, "PiSmmCoreImageSize - 0x%016lx\n", gSmmCorePrivate->PiSmmCoreImageSize));\r
1056\r
1057 gSmmCorePrivate->PiSmmCoreEntryPoint = ImageContext.EntryPoint;\r
1058\r
1059 //\r
1060 // Execute image\r
1061 //\r
1062 EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;\r
1063 Status = EntryPoint ((EFI_HANDLE)Context, gST);\r
1064 }\r
1065 }\r
1066\r
1067 //\r
1068 // Always free memory allocted by GetFileBufferByFilePath ()\r
1069 //\r
1070 FreePool (SourceBuffer);\r
1071\r
1072 return Status;\r
1073}\r
1074\r
1075/**\r
1076 SMM split SMRAM entry.\r
1077\r
1078 @param[in, out] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.\r
1079 @param[in, out] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.\r
1080 @param[out] Ranges Output pointer to hold split EFI_SMRAM_DESCRIPTOR entry.\r
1081 @param[in, out] RangeCount Pointer to range count.\r
1082 @param[out] ReservedRanges Output pointer to hold split EFI_SMM_RESERVED_SMRAM_REGION entry.\r
1083 @param[in, out] ReservedRangeCount Pointer to reserved range count.\r
1084 @param[out] FinalRanges Output pointer to hold split final EFI_SMRAM_DESCRIPTOR entry\r
1085 that no need to be split anymore.\r
1086 @param[in, out] FinalRangeCount Pointer to final range count.\r
1087\r
1088**/\r
1089VOID\r
1090SmmSplitSmramEntry (\r
1091 IN OUT EFI_SMRAM_DESCRIPTOR *RangeToCompare,\r
1092 IN OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare,\r
1093 OUT EFI_SMRAM_DESCRIPTOR *Ranges,\r
1094 IN OUT UINTN *RangeCount,\r
1095 OUT EFI_SMM_RESERVED_SMRAM_REGION *ReservedRanges,\r
1096 IN OUT UINTN *ReservedRangeCount,\r
1097 OUT EFI_SMRAM_DESCRIPTOR *FinalRanges,\r
1098 IN OUT UINTN *FinalRangeCount\r
1099 )\r
1100{\r
1101 UINT64 RangeToCompareEnd;\r
1102 UINT64 ReservedRangeToCompareEnd;\r
1103\r
1104 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;\r
1105 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;\r
1106\r
1107 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&\r
1108 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {\r
1109 if (RangeToCompareEnd < ReservedRangeToCompareEnd) {\r
1110 //\r
1111 // RangeToCompare ReservedRangeToCompare\r
1112 // ---- ---- --------------------------------------\r
1113 // | | | | -> 1. ReservedRangeToCompare\r
1114 // ---- | | |--| --------------------------------------\r
1115 // | | | | | |\r
1116 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1117 // | | | | | | RangeToCompare->PhysicalSize = 0\r
1118 // ---- | | |--| --------------------------------------\r
1119 // | | | | -> 3. ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount\r
1120 // ---- ---- --------------------------------------\r
1121 //\r
1122\r
1123 //\r
1124 // 1. Update ReservedRangeToCompare.\r
1125 //\r
1126 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;\r
1127 //\r
1128 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1129 // Zero RangeToCompare->PhysicalSize.\r
1130 //\r
1131 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;\r
1132 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;\r
1133 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1134 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompare->PhysicalSize;\r
1135 *FinalRangeCount += 1;\r
1136 RangeToCompare->PhysicalSize = 0;\r
1137 //\r
1138 // 3. Update ReservedRanges[*ReservedRangeCount] and increment *ReservedRangeCount.\r
1139 //\r
1140 ReservedRanges[*ReservedRangeCount].SmramReservedStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1141 ReservedRanges[*ReservedRangeCount].SmramReservedSize = ReservedRangeToCompareEnd - RangeToCompareEnd;\r
1142 *ReservedRangeCount += 1;\r
1143 } else {\r
1144 //\r
1145 // RangeToCompare ReservedRangeToCompare\r
1146 // ---- ---- --------------------------------------\r
1147 // | | | | -> 1. ReservedRangeToCompare\r
1148 // ---- | | |--| --------------------------------------\r
1149 // | | | | | |\r
1150 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1151 // | | | | | |\r
1152 // | | ---- |--| --------------------------------------\r
1153 // | | | | -> 3. RangeToCompare\r
1154 // ---- ---- --------------------------------------\r
1155 //\r
1156\r
1157 //\r
1158 // 1. Update ReservedRangeToCompare.\r
1159 //\r
1160 ReservedRangeToCompare->SmramReservedSize = RangeToCompare->CpuStart - ReservedRangeToCompare->SmramReservedStart;\r
1161 //\r
1162 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1163 //\r
1164 FinalRanges[*FinalRangeCount].CpuStart = RangeToCompare->CpuStart;\r
1165 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart;\r
1166 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1167 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompareEnd - RangeToCompare->CpuStart;\r
1168 *FinalRangeCount += 1;\r
1169 //\r
1170 // 3. Update RangeToCompare.\r
1171 //\r
1172 RangeToCompare->CpuStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1173 RangeToCompare->PhysicalStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1174 RangeToCompare->PhysicalSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1175 }\r
1176 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&\r
1177 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {\r
1178 if (ReservedRangeToCompareEnd < RangeToCompareEnd) {\r
1179 //\r
1180 // RangeToCompare ReservedRangeToCompare\r
1181 // ---- ---- --------------------------------------\r
1182 // | | | | -> 1. RangeToCompare\r
1183 // | | ---- |--| --------------------------------------\r
1184 // | | | | | |\r
1185 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1186 // | | | | | | ReservedRangeToCompare->SmramReservedSize = 0\r
1187 // | | ---- |--| --------------------------------------\r
1188 // | | | | -> 3. Ranges[*RangeCount] and increment *RangeCount\r
1189 // ---- ---- --------------------------------------\r
1190 //\r
1191\r
1192 //\r
1193 // 1. Update RangeToCompare.\r
1194 //\r
1195 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;\r
1196 //\r
1197 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1198 // ReservedRangeToCompare->SmramReservedSize = 0\r
1199 //\r
1200 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;\r
1201 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;\r
1202 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1203 FinalRanges[*FinalRangeCount].PhysicalSize = ReservedRangeToCompare->SmramReservedSize;\r
1204 *FinalRangeCount += 1;\r
1205 ReservedRangeToCompare->SmramReservedSize = 0;\r
1206 //\r
1207 // 3. Update Ranges[*RangeCount] and increment *RangeCount.\r
1208 //\r
1209 Ranges[*RangeCount].CpuStart = FinalRanges[*FinalRangeCount - 1].CpuStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1210 Ranges[*RangeCount].PhysicalStart = FinalRanges[*FinalRangeCount - 1].PhysicalStart + FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1211 Ranges[*RangeCount].RegionState = RangeToCompare->RegionState;\r
1212 Ranges[*RangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompareEnd;\r
1213 *RangeCount += 1;\r
1214 } else {\r
1215 //\r
1216 // RangeToCompare ReservedRangeToCompare\r
1217 // ---- ---- --------------------------------------\r
1218 // | | | | -> 1. RangeToCompare\r
1219 // | | ---- |--| --------------------------------------\r
1220 // | | | | | |\r
1221 // | | | | | | -> 2. FinalRanges[*FinalRangeCount] and increment *FinalRangeCount\r
1222 // | | | | | |\r
1223 // ---- | | |--| --------------------------------------\r
1224 // | | | | -> 3. ReservedRangeToCompare\r
1225 // ---- ---- --------------------------------------\r
1226 //\r
1227\r
1228 //\r
1229 // 1. Update RangeToCompare.\r
1230 //\r
1231 RangeToCompare->PhysicalSize = ReservedRangeToCompare->SmramReservedStart - RangeToCompare->CpuStart;\r
1232 //\r
1233 // 2. Update FinalRanges[FinalRangeCount] and increment *FinalRangeCount.\r
1234 // ReservedRangeToCompare->SmramReservedSize = 0\r
1235 //\r
1236 FinalRanges[*FinalRangeCount].CpuStart = ReservedRangeToCompare->SmramReservedStart;\r
1237 FinalRanges[*FinalRangeCount].PhysicalStart = RangeToCompare->PhysicalStart + RangeToCompare->PhysicalSize;\r
1238 FinalRanges[*FinalRangeCount].RegionState = RangeToCompare->RegionState | EFI_ALLOCATED;\r
1239 FinalRanges[*FinalRangeCount].PhysicalSize = RangeToCompareEnd - ReservedRangeToCompare->SmramReservedStart;\r
1240 *FinalRangeCount += 1;\r
1241 //\r
1242 // 3. Update ReservedRangeToCompare.\r
1243 //\r
1244 ReservedRangeToCompare->SmramReservedStart += FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1245 ReservedRangeToCompare->SmramReservedSize -= FinalRanges[*FinalRangeCount - 1].PhysicalSize;\r
1246 }\r
1247 }\r
1248}\r
1249\r
1250/**\r
1251 Returns if SMRAM range and SMRAM reserved range are overlapped.\r
1252\r
1253 @param[in] RangeToCompare Pointer to EFI_SMRAM_DESCRIPTOR to compare.\r
1254 @param[in] ReservedRangeToCompare Pointer to EFI_SMM_RESERVED_SMRAM_REGION to compare.\r
1255\r
1256 @retval TRUE There is overlap.\r
1257 @retval FALSE There is no overlap.\r
1258\r
1259**/\r
1260BOOLEAN\r
1261SmmIsSmramOverlap (\r
1262 IN EFI_SMRAM_DESCRIPTOR *RangeToCompare,\r
1263 IN EFI_SMM_RESERVED_SMRAM_REGION *ReservedRangeToCompare\r
1264 )\r
1265{\r
1266 UINT64 RangeToCompareEnd;\r
1267 UINT64 ReservedRangeToCompareEnd;\r
1268\r
1269 RangeToCompareEnd = RangeToCompare->CpuStart + RangeToCompare->PhysicalSize;\r
1270 ReservedRangeToCompareEnd = ReservedRangeToCompare->SmramReservedStart + ReservedRangeToCompare->SmramReservedSize;\r
1271\r
1272 if ((RangeToCompare->CpuStart >= ReservedRangeToCompare->SmramReservedStart) &&\r
1273 (RangeToCompare->CpuStart < ReservedRangeToCompareEnd)) {\r
1274 return TRUE;\r
1275 } else if ((ReservedRangeToCompare->SmramReservedStart >= RangeToCompare->CpuStart) &&\r
1276 (ReservedRangeToCompare->SmramReservedStart < RangeToCompareEnd)) {\r
1277 return TRUE;\r
1278 }\r
1279 return FALSE;\r
1280}\r
1281\r
1282/**\r
1283 Get full SMRAM ranges.\r
1284\r
1285 It will get SMRAM ranges from SmmAccess protocol and SMRAM reserved ranges from\r
1286 SmmConfiguration protocol, split the entries if there is overlap between them.\r
1287 It will also reserve one entry for SMM core.\r
1288\r
1289 @param[out] FullSmramRangeCount Output pointer to full SMRAM range count.\r
1290\r
1291 @return Pointer to full SMRAM ranges.\r
1292\r
1293**/\r
1294EFI_SMRAM_DESCRIPTOR *\r
1295GetFullSmramRanges (\r
1296 OUT UINTN *FullSmramRangeCount\r
1297 )\r
1298{\r
1299 EFI_STATUS Status;\r
1300 EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r
1301 UINTN Size;\r
1302 UINTN Index;\r
1303 UINTN Index2;\r
1304 EFI_SMRAM_DESCRIPTOR *FullSmramRanges;\r
1305 UINTN TempSmramRangeCount;\r
1306 UINTN AdditionSmramRangeCount;\r
1307 EFI_SMRAM_DESCRIPTOR *TempSmramRanges;\r
1308 UINTN SmramRangeCount;\r
1309 EFI_SMRAM_DESCRIPTOR *SmramRanges;\r
1310 UINTN SmramReservedCount;\r
1311 EFI_SMM_RESERVED_SMRAM_REGION *SmramReservedRanges;\r
1312 UINTN MaxCount;\r
1313 BOOLEAN Rescan;\r
1314\r
1315 //\r
1316 // Get SMM Configuration Protocol if it is present.\r
1317 //\r
1318 SmmConfiguration = NULL;\r
1319 Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);\r
1320\r
1321 //\r
1322 // Get SMRAM information.\r
1323 //\r
1324 Size = 0;\r
1325 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);\r
1326 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
1327\r
1328 SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
1329\r
1330 //\r
1331 // Get SMRAM reserved region count.\r
1332 //\r
1333 SmramReservedCount = 0;\r
1334 if (SmmConfiguration != NULL) {\r
1335 while (SmmConfiguration->SmramReservedRegions[SmramReservedCount].SmramReservedSize != 0) {\r
1336 SmramReservedCount++;\r
1337 }\r
1338 }\r
1339\r
1340 //\r
1341 // Reserve one entry for SMM Core in the full SMRAM ranges.\r
1342 //\r
1343 AdditionSmramRangeCount = 1;\r
1344 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
1345 //\r
1346 // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.\r
1347 //\r
1348 AdditionSmramRangeCount = 2;\r
1349 }\r
1350\r
1351 if (SmramReservedCount == 0) {\r
1352 //\r
1353 // No reserved SMRAM entry from SMM Configuration Protocol.\r
1354 //\r
1355 *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;\r
1356 Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);\r
1357 FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocateZeroPool (Size);\r
1358 ASSERT (FullSmramRanges != NULL);\r
1359\r
1360 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, FullSmramRanges);\r
1361 ASSERT_EFI_ERROR (Status);\r
1362\r
1363 return FullSmramRanges;\r
1364 }\r
1365\r
1366 //\r
1367 // Why MaxCount = X + 2 * Y?\r
1368 // Take Y = 1 as example below, Y > 1 case is just the iteration of Y = 1.\r
1369 //\r
1370 // X = 1 Y = 1 MaxCount = 3 = 1 + 2 * 1\r
1371 // ---- ----\r
1372 // | | ---- |--|\r
1373 // | | | | -> | |\r
1374 // | | ---- |--|\r
1375 // ---- ----\r
1376 //\r
1377 // X = 2 Y = 1 MaxCount = 4 = 2 + 2 * 1\r
1378 // ---- ----\r
1379 // | | | |\r
1380 // | | ---- |--|\r
1381 // | | | | | |\r
1382 // |--| | | -> |--|\r
1383 // | | | | | |\r
1384 // | | ---- |--|\r
1385 // | | | |\r
1386 // ---- ----\r
1387 //\r
1388 // X = 3 Y = 1 MaxCount = 5 = 3 + 2 * 1\r
1389 // ---- ----\r
1390 // | | | |\r
1391 // | | ---- |--|\r
1392 // |--| | | |--|\r
1393 // | | | | -> | |\r
1394 // |--| | | |--|\r
1395 // | | ---- |--|\r
1396 // | | | |\r
1397 // ---- ----\r
1398 //\r
1399 // ......\r
1400 //\r
1401 MaxCount = SmramRangeCount + 2 * SmramReservedCount;\r
1402\r
1403 Size = MaxCount * sizeof (EFI_SMM_RESERVED_SMRAM_REGION);\r
1404 SmramReservedRanges = (EFI_SMM_RESERVED_SMRAM_REGION *) AllocatePool (Size);\r
1405 ASSERT (SmramReservedRanges != NULL);\r
1406 for (Index = 0; Index < SmramReservedCount; Index++) {\r
1407 CopyMem (&SmramReservedRanges[Index], &SmmConfiguration->SmramReservedRegions[Index], sizeof (EFI_SMM_RESERVED_SMRAM_REGION));\r
1408 }\r
1409\r
1410 Size = MaxCount * sizeof (EFI_SMRAM_DESCRIPTOR);\r
1411 TempSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
1412 ASSERT (TempSmramRanges != NULL);\r
1413 TempSmramRangeCount = 0;\r
1414\r
1415 SmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
1416 ASSERT (SmramRanges != NULL);\r
1417 Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, SmramRanges);\r
1418 ASSERT_EFI_ERROR (Status);\r
1419\r
1420 do {\r
1421 Rescan = FALSE;\r
1422 for (Index = 0; (Index < SmramRangeCount) && !Rescan; Index++) {\r
1423 //\r
1424 // Skip zero size entry.\r
1425 //\r
1426 if (SmramRanges[Index].PhysicalSize != 0) {\r
1427 for (Index2 = 0; (Index2 < SmramReservedCount) && !Rescan; Index2++) {\r
1428 //\r
1429 // Skip zero size entry.\r
1430 //\r
1431 if (SmramReservedRanges[Index2].SmramReservedSize != 0) {\r
1432 if (SmmIsSmramOverlap (\r
1433 &SmramRanges[Index],\r
1434 &SmramReservedRanges[Index2]\r
1435 )) {\r
1436 //\r
1437 // There is overlap, need to split entry and then rescan.\r
1438 //\r
1439 SmmSplitSmramEntry (\r
1440 &SmramRanges[Index],\r
1441 &SmramReservedRanges[Index2],\r
1442 SmramRanges,\r
1443 &SmramRangeCount,\r
1444 SmramReservedRanges,\r
1445 &SmramReservedCount,\r
1446 TempSmramRanges,\r
1447 &TempSmramRangeCount\r
1448 );\r
1449 Rescan = TRUE;\r
1450 }\r
1451 }\r
1452 }\r
1453 if (!Rescan) {\r
1454 //\r
1455 // No any overlap, copy the entry to the temp SMRAM ranges.\r
1456 // Zero SmramRanges[Index].PhysicalSize = 0;\r
1457 //\r
1458 CopyMem (&TempSmramRanges[TempSmramRangeCount++], &SmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));\r
1459 SmramRanges[Index].PhysicalSize = 0;\r
1460 }\r
1461 }\r
1462 }\r
1463 } while (Rescan);\r
1464 ASSERT (TempSmramRangeCount <= MaxCount);\r
1465\r
1466 //\r
1467 // Sort the entries\r
1468 //\r
1469 FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));\r
1470 ASSERT (FullSmramRanges != NULL);\r
1471 *FullSmramRangeCount = 0;\r
1472 do {\r
1473 for (Index = 0; Index < TempSmramRangeCount; Index++) {\r
1474 if (TempSmramRanges[Index].PhysicalSize != 0) {\r
1475 break;\r
1476 }\r
1477 }\r
1478 ASSERT (Index < TempSmramRangeCount);\r
1479 for (Index2 = 0; Index2 < TempSmramRangeCount; Index2++) {\r
1480 if ((Index2 != Index) && (TempSmramRanges[Index2].PhysicalSize != 0) && (TempSmramRanges[Index2].CpuStart < TempSmramRanges[Index].CpuStart)) {\r
1481 Index = Index2;\r
1482 }\r
1483 }\r
1484 CopyMem (&FullSmramRanges[*FullSmramRangeCount], &TempSmramRanges[Index], sizeof (EFI_SMRAM_DESCRIPTOR));\r
1485 *FullSmramRangeCount += 1;\r
1486 TempSmramRanges[Index].PhysicalSize = 0;\r
1487 } while (*FullSmramRangeCount < TempSmramRangeCount);\r
1488 ASSERT (*FullSmramRangeCount == TempSmramRangeCount);\r
1489 *FullSmramRangeCount += AdditionSmramRangeCount;\r
1490\r
1491 FreePool (SmramRanges);\r
1492 FreePool (SmramReservedRanges);\r
1493 FreePool (TempSmramRanges);\r
1494\r
1495 return FullSmramRanges;\r
1496}\r
1497\r
1498/**\r
1499 The Entry Point for SMM IPL\r
1500\r
1501 Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install \r
1502 SMM Base 2 Protocol and SMM Communication Protocol, and register for the \r
1503 critical events required to coordinate between DXE and SMM environments.\r
1504 \r
1505 @param ImageHandle The firmware allocated handle for the EFI image.\r
1506 @param SystemTable A pointer to the EFI System Table.\r
1507\r
1508 @retval EFI_SUCCESS The entry point is executed successfully.\r
1509 @retval Other Some error occurred when executing this entry point.\r
1510\r
1511**/\r
1512EFI_STATUS\r
1513EFIAPI\r
1514SmmIplEntry (\r
1515 IN EFI_HANDLE ImageHandle,\r
1516 IN EFI_SYSTEM_TABLE *SystemTable\r
1517 )\r
1518{\r
1519 EFI_STATUS Status;\r
1520 UINTN Index;\r
1521 UINT64 MaxSize;\r
1522 VOID *Registration;\r
1523 UINT64 SmmCodeSize;\r
1524 EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *LMFAConfigurationTable;\r
1525 EFI_CPU_ARCH_PROTOCOL *CpuArch;\r
1526 EFI_STATUS SetAttrStatus;\r
1527 EFI_SMRAM_DESCRIPTOR *SmramRangeSmmDriver;\r
1528\r
1529 //\r
1530 // Fill in the image handle of the SMM IPL so the SMM Core can use this as the \r
1531 // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded \r
1532 // by the SMM Core\r
1533 //\r
1534 mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r
1535\r
1536 //\r
1537 // Get SMM Access Protocol\r
1538 //\r
1539 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);\r
1540 ASSERT_EFI_ERROR (Status);\r
1541\r
1542 //\r
1543 // Get SMM Control2 Protocol\r
1544 //\r
1545 Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);\r
1546 ASSERT_EFI_ERROR (Status);\r
1547\r
1548 gSmmCorePrivate->SmramRanges = GetFullSmramRanges (&gSmmCorePrivate->SmramRangeCount);\r
1549\r
1550 //\r
1551 // Open all SMRAM ranges\r
1552 //\r
1553 Status = mSmmAccess->Open (mSmmAccess);\r
1554 ASSERT_EFI_ERROR (Status);\r
1555\r
1556 //\r
1557 // Print debug message that the SMRAM window is now open.\r
1558 //\r
1559 DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r
1560 \r
1561 //\r
1562 // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r
1563 //\r
1564 mCurrentSmramRange = NULL;\r
1565 for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r
1566 //\r
1567 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization\r
1568 //\r
1569 if ((gSmmCorePrivate->SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {\r
1570 continue;\r
1571 }\r
1572\r
1573 if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r
1574 if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {\r
1575 if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r
1576 MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
1577 mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r
1578 }\r
1579 }\r
1580 }\r
1581 }\r
1582\r
1583 if (mCurrentSmramRange != NULL) {\r
1584 //\r
1585 // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r
1586 //\r
1587 DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n", \r
1588 (VOID *)(UINTN)mCurrentSmramRange->CpuStart, \r
1589 (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r
1590 ));\r
1591\r
1592 GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);\r
1593 //\r
1594 // If CPU AP is present, attempt to set SMRAM cacheability to WB\r
1595 // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP\r
1596 // is not available here.\r
1597 //\r
1598 CpuArch = NULL;\r
1599 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);\r
1600 if (!EFI_ERROR (Status)) {\r
1601 Status = gDS->SetMemorySpaceAttributes(\r
1602 mSmramCacheBase, \r
1603 mSmramCacheSize,\r
1604 EFI_MEMORY_WB\r
1605 );\r
1606 if (EFI_ERROR (Status)) {\r
1607 DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r
1608 } \r
1609 }\r
1610 //\r
1611 // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load\r
1612 // Modules At Fixed Address Configuration Table.\r
1613 //\r
1614 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
1615 //\r
1616 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
1617 //\r
1618 SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);\r
1619 //\r
1620 // The SMRAM available memory is assumed to be larger than SmmCodeSize\r
1621 //\r
1622 ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);\r
1623 //\r
1624 // Retrieve Load modules At fixed address configuration table and save the SMRAM base.\r
1625 //\r
1626 Status = EfiGetSystemConfigurationTable (\r
1627 &gLoadFixedAddressConfigurationTableGuid,\r
1628 (VOID **) &LMFAConfigurationTable\r
1629 );\r
1630 if (!EFI_ERROR (Status) && LMFAConfigurationTable != NULL) {\r
1631 LMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r
1632 //\r
1633 // Print the SMRAM base\r
1634 //\r
1635 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable->SmramBase));\r
1636 }\r
1637\r
1638 //\r
1639 // Fill the Smram range for all SMM code\r
1640 //\r
1641 SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];\r
1642 SmramRangeSmmDriver->CpuStart = mCurrentSmramRange->CpuStart;\r
1643 SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;\r
1644 SmramRangeSmmDriver->RegionState = mCurrentSmramRange->RegionState | EFI_ALLOCATED;\r
1645 SmramRangeSmmDriver->PhysicalSize = SmmCodeSize;\r
1646\r
1647 mCurrentSmramRange->PhysicalSize -= SmmCodeSize;\r
1648 mCurrentSmramRange->CpuStart = mCurrentSmramRange->CpuStart + SmmCodeSize;\r
1649 mCurrentSmramRange->PhysicalStart = mCurrentSmramRange->PhysicalStart + SmmCodeSize;\r
1650 }\r
1651 //\r
1652 // Load SMM Core into SMRAM and execute it from SMRAM\r
1653 //\r
1654 Status = ExecuteSmmCoreFromSmram (\r
1655 mCurrentSmramRange,\r
1656 &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 1],\r
1657 gSmmCorePrivate\r
1658 );\r
1659 if (EFI_ERROR (Status)) {\r
1660 //\r
1661 // Print error message that the SMM Core failed to be loaded and executed.\r
1662 //\r
1663 DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));\r
1664\r
1665 //\r
1666 // Attempt to reset SMRAM cacheability to UC\r
1667 //\r
1668 if (CpuArch != NULL) {\r
1669 SetAttrStatus = gDS->SetMemorySpaceAttributes(\r
1670 mSmramCacheBase, \r
1671 mSmramCacheSize,\r
1672 EFI_MEMORY_UC\r
1673 );\r
1674 if (EFI_ERROR (SetAttrStatus)) {\r
1675 DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
1676 } \r
1677 }\r
1678 }\r
1679 } else {\r
1680 //\r
1681 // Print error message that there are not enough SMRAM resources to load the SMM Core.\r
1682 //\r
1683 DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));\r
1684 }\r
1685\r
1686 //\r
1687 // If the SMM Core could not be loaded then close SMRAM window, free allocated \r
1688 // resources, and return an error so SMM IPL will be unloaded.\r
1689 //\r
1690 if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {\r
1691 //\r
1692 // Close all SMRAM ranges\r
1693 //\r
1694 Status = mSmmAccess->Close (mSmmAccess);\r
1695 ASSERT_EFI_ERROR (Status);\r
1696\r
1697 //\r
1698 // Print debug message that the SMRAM window is now closed.\r
1699 //\r
1700 DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r
1701\r
1702 //\r
1703 // Free all allocated resources\r
1704 //\r
1705 FreePool (gSmmCorePrivate->SmramRanges);\r
1706\r
1707 return EFI_UNSUPPORTED;\r
1708 }\r
1709 \r
1710 //\r
1711 // Install SMM Base2 Protocol and SMM Communication Protocol\r
1712 //\r
1713 Status = gBS->InstallMultipleProtocolInterfaces (\r
1714 &mSmmIplHandle,\r
1715 &gEfiSmmBase2ProtocolGuid, &mSmmBase2,\r
1716 &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,\r
1717 NULL\r
1718 );\r
1719 ASSERT_EFI_ERROR (Status);\r
1720\r
1721 //\r
1722 // Create the set of protocol and event notififcations that the SMM IPL requires\r
1723 //\r
1724 for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
1725 if (mSmmIplEvents[Index].Protocol) {\r
1726 mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (\r
1727 mSmmIplEvents[Index].Guid,\r
1728 mSmmIplEvents[Index].NotifyTpl,\r
1729 mSmmIplEvents[Index].NotifyFunction,\r
1730 mSmmIplEvents[Index].NotifyContext,\r
1731 &Registration\r
1732 );\r
1733 } else {\r
1734 Status = gBS->CreateEventEx (\r
1735 EVT_NOTIFY_SIGNAL,\r
1736 mSmmIplEvents[Index].NotifyTpl,\r
1737 mSmmIplEvents[Index].NotifyFunction,\r
1738 mSmmIplEvents[Index].NotifyContext,\r
1739 mSmmIplEvents[Index].Guid,\r
1740 &mSmmIplEvents[Index].Event\r
1741 );\r
1742 ASSERT_EFI_ERROR (Status);\r
1743 }\r
1744 }\r
1745\r
1746 return EFI_SUCCESS;\r
1747}\r