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