]>
Commit | Line | Data |
---|---|---|
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 - 2010, 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/FirmwareVolume2.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/UefiLib.h>\r | |
38 | #include <Library/UefiRuntimeLib.h>\r | |
39 | #include <Library/PcdLib.h>\r | |
40 | \r | |
41 | #include "PiSmmCorePrivateData.h"\r | |
42 | \r | |
43 | //\r | |
44 | // Function prototypes from produced protocols\r | |
45 | //\r | |
46 | \r | |
47 | /**\r | |
48 | Indicate whether the driver is currently executing in the SMM Initialization phase.\r | |
49 | \r | |
50 | @param This The EFI_SMM_BASE2_PROTOCOL instance.\r | |
51 | @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r | |
52 | inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r | |
53 | \r | |
54 | @retval EFI_INVALID_PARAMETER InSmram was NULL.\r | |
55 | @retval EFI_SUCCESS The call returned successfully.\r | |
56 | \r | |
57 | **/\r | |
58 | EFI_STATUS\r | |
59 | EFIAPI\r | |
60 | SmmBase2InSmram (\r | |
61 | IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r | |
62 | OUT BOOLEAN *InSmram\r | |
63 | );\r | |
64 | \r | |
65 | /**\r | |
66 | Retrieves the location of the System Management System Table (SMST).\r | |
67 | \r | |
68 | @param This The EFI_SMM_BASE2_PROTOCOL instance.\r | |
69 | @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r | |
70 | \r | |
71 | @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r | |
72 | @retval EFI_SUCCESS The memory was returned to the system.\r | |
73 | @retval EFI_UNSUPPORTED Not in SMM.\r | |
74 | \r | |
75 | **/\r | |
76 | EFI_STATUS\r | |
77 | EFIAPI\r | |
78 | SmmBase2GetSmstLocation (\r | |
79 | IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r | |
80 | OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r | |
81 | );\r | |
82 | \r | |
83 | /**\r | |
84 | Communicates with a registered handler.\r | |
85 | \r | |
86 | This function provides a service to send and receive messages from a registered \r | |
87 | UEFI service. This function is part of the SMM Communication Protocol that may \r | |
88 | be called in physical mode prior to SetVirtualAddressMap() and in virtual mode \r | |
89 | after SetVirtualAddressMap().\r | |
90 | \r | |
91 | @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r | |
92 | @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.\r | |
93 | @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data\r | |
94 | being returned. Zero if the handler does not wish to reply with any data.\r | |
95 | \r | |
96 | @retval EFI_SUCCESS The message was successfully posted.\r | |
97 | @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r | |
98 | **/\r | |
99 | EFI_STATUS\r | |
100 | EFIAPI\r | |
101 | SmmCommunicationCommunicate (\r | |
102 | IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r | |
103 | IN OUT VOID *CommBuffer,\r | |
104 | IN OUT UINTN *CommSize\r | |
105 | );\r | |
106 | \r | |
107 | /**\r | |
108 | Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r | |
109 | \r | |
110 | @param Event The Event that is being processed, not used.\r | |
111 | @param Context Event Context, not used.\r | |
112 | \r | |
113 | **/\r | |
114 | VOID\r | |
115 | EFIAPI\r | |
116 | SmmIplSmmConfigurationEventNotify (\r | |
117 | IN EFI_EVENT Event,\r | |
118 | IN VOID *Context\r | |
119 | );\r | |
120 | \r | |
121 | /**\r | |
122 | Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r | |
123 | or if gEfiEventReadyToBootGuid is signalled.\r | |
124 | \r | |
125 | @param Event The Event that is being processed, not used.\r | |
126 | @param Context Event Context, not used.\r | |
127 | \r | |
128 | **/\r | |
129 | VOID\r | |
130 | EFIAPI\r | |
131 | SmmIplReadyToLockEventNotify (\r | |
132 | IN EFI_EVENT Event,\r | |
133 | IN VOID *Context\r | |
134 | );\r | |
135 | \r | |
136 | /**\r | |
137 | Event notification that is fired when DxeDispatch Event Group is signaled.\r | |
138 | \r | |
139 | @param Event The Event that is being processed, not used.\r | |
140 | @param Context Event Context, not used.\r | |
141 | \r | |
142 | **/\r | |
143 | VOID\r | |
144 | EFIAPI\r | |
145 | SmmIplGuidedEventNotify (\r | |
146 | IN EFI_EVENT Event,\r | |
147 | IN VOID *Context\r | |
148 | );\r | |
149 | \r | |
150 | /**\r | |
151 | Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r | |
152 | \r | |
153 | This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r | |
154 | It convers pointer to new virtual address.\r | |
155 | \r | |
156 | @param Event Event whose notification function is being invoked.\r | |
157 | @param Context Pointer to the notification function's context.\r | |
158 | \r | |
159 | **/\r | |
160 | VOID\r | |
161 | EFIAPI\r | |
162 | SmmIplSetVirtualAddressNotify (\r | |
163 | IN EFI_EVENT Event,\r | |
164 | IN VOID *Context\r | |
165 | );\r | |
166 | \r | |
167 | //\r | |
168 | // Data structure used to declare a table of protocol notifications and event \r | |
169 | // notifications required by the SMM IPL\r | |
170 | //\r | |
171 | typedef struct {\r | |
172 | BOOLEAN Protocol;\r | |
173 | BOOLEAN CloseOnLock;\r | |
174 | EFI_GUID *Guid;\r | |
175 | EFI_EVENT_NOTIFY NotifyFunction;\r | |
176 | VOID *NotifyContext;\r | |
177 | EFI_EVENT Event;\r | |
178 | } SMM_IPL_EVENT_NOTIFICATION;\r | |
179 | \r | |
180 | //\r | |
181 | // Handle to install the SMM Base2 Protocol and the SMM Communication Protocol\r | |
182 | //\r | |
183 | EFI_HANDLE mSmmIplHandle = NULL;\r | |
184 | \r | |
185 | //\r | |
186 | // SMM Base 2 Protocol instance\r | |
187 | //\r | |
188 | EFI_SMM_BASE2_PROTOCOL mSmmBase2 = {\r | |
189 | SmmBase2InSmram,\r | |
190 | SmmBase2GetSmstLocation\r | |
191 | };\r | |
192 | \r | |
193 | //\r | |
194 | // SMM Communication Protocol instance\r | |
195 | //\r | |
196 | EFI_SMM_COMMUNICATION_PROTOCOL mSmmCommunication = {\r | |
197 | SmmCommunicationCommunicate\r | |
198 | };\r | |
199 | \r | |
200 | //\r | |
201 | // SMM Core Private Data structure that contains the data shared between\r | |
202 | // the SMM IPL and the SMM Core.\r | |
203 | //\r | |
204 | SMM_CORE_PRIVATE_DATA mSmmCorePrivateData = {\r | |
205 | SMM_CORE_PRIVATE_DATA_SIGNATURE, // Signature\r | |
206 | NULL, // SmmIplImageHandle\r | |
207 | 0, // SmramRangeCount\r | |
208 | NULL, // SmramRanges\r | |
209 | NULL, // SmmEntryPoint\r | |
210 | FALSE, // SmmEntryPointRegistered\r | |
211 | FALSE, // InSmm\r | |
212 | NULL, // Smst\r | |
213 | 0, // BufferSize\r | |
214 | NULL, // CommunicationBuffer\r | |
215 | EFI_SUCCESS // ReturnStatus\r | |
216 | };\r | |
217 | \r | |
218 | //\r | |
219 | // Global pointer used to access mSmmCorePrivateData from outside and inside SMM\r | |
220 | //\r | |
221 | SMM_CORE_PRIVATE_DATA *gSmmCorePrivate = &mSmmCorePrivateData;\r | |
222 | \r | |
223 | //\r | |
224 | // SMM IPL global variables\r | |
225 | //\r | |
226 | EFI_SMM_CONTROL2_PROTOCOL *mSmmControl2;\r | |
227 | EFI_SMM_ACCESS2_PROTOCOL *mSmmAccess;\r | |
228 | EFI_SMRAM_DESCRIPTOR *mCurrentSmramRange;\r | |
229 | BOOLEAN mSmmLocked = FALSE;\r | |
230 | \r | |
231 | //\r | |
232 | // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires\r | |
233 | //\r | |
234 | SMM_IPL_EVENT_NOTIFICATION mSmmIplEvents[] = {\r | |
235 | //\r | |
236 | // Declare protocol notification on the SMM Configuration protocol. When this notification is etablished, \r | |
237 | // the associated event is immediately signalled, so the notification function will be executed and the \r | |
238 | // SMM Configuration Protocol will be found if it is already in the handle database.\r | |
239 | //\r | |
240 | { TRUE, FALSE, &gEfiSmmConfigurationProtocolGuid, SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid, NULL },\r | |
241 | //\r | |
242 | // Declare protocl notification on DxeSmmReadyToLock protocols. When this notification is etablished, \r | |
243 | // the associated event is immediately signalled, so the notification function will be executed and the \r | |
244 | // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.\r | |
245 | //\r | |
246 | { TRUE, TRUE, &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify, &gEfiDxeSmmReadyToLockProtocolGuid, NULL },\r | |
247 | //\r | |
248 | // Declare event notification on the DXE Dispatch Event Group. This event is signaled by the DXE Core\r | |
249 | // each time the DXE Core dispatcher has completed its work. When this event is signalled, the SMM Core\r | |
250 | // if notified, so the SMM Core can dispatch SMM drivers.\r | |
251 | //\r | |
252 | { FALSE, TRUE, &gEfiEventDxeDispatchGuid, SmmIplGuidedEventNotify, &gEfiEventDxeDispatchGuid, NULL },\r | |
253 | //\r | |
254 | // Declare event notification on Ready To Boot Event Group. This is an extra event notification that is\r | |
255 | // used to make sure SMRAM is locked before any boot options are processed.\r | |
256 | //\r | |
257 | { FALSE, TRUE, &gEfiEventReadyToBootGuid, SmmIplReadyToLockEventNotify, &gEfiEventReadyToBootGuid, NULL },\r | |
258 | //\r | |
259 | // Declare event notification on Legacy Boot Event Group. This is used to inform the SMM Core that the platform \r | |
260 | // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core \r | |
261 | // must guarantee that it does not access any UEFI related structures outside of SMRAM.\r | |
262 | //\r | |
263 | { FALSE, FALSE, &gEfiEventLegacyBootGuid, SmmIplGuidedEventNotify, &gEfiEventLegacyBootGuid, NULL },\r | |
264 | //\r | |
265 | // Declare event notification on SetVirtualAddressMap() Event Group. This is used to convert gSmmCorePrivate \r | |
266 | // and mSmmControl2 from physical addresses to virtual addresses.\r | |
267 | //\r | |
268 | { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify, NULL, NULL },\r | |
269 | //\r | |
270 | // Terminate the table of event notifications\r | |
271 | //\r | |
272 | { FALSE, FALSE, NULL, NULL, NULL, NULL }\r | |
273 | };\r | |
274 | \r | |
275 | /**\r | |
276 | Indicate whether the driver is currently executing in the SMM Initialization phase.\r | |
277 | \r | |
278 | @param This The EFI_SMM_BASE2_PROTOCOL instance.\r | |
279 | @param InSmram Pointer to a Boolean which, on return, indicates that the driver is currently executing\r | |
280 | inside of SMRAM (TRUE) or outside of SMRAM (FALSE).\r | |
281 | \r | |
282 | @retval EFI_INVALID_PARAMETER InSmram was NULL.\r | |
283 | @retval EFI_SUCCESS The call returned successfully.\r | |
284 | \r | |
285 | **/\r | |
286 | EFI_STATUS\r | |
287 | EFIAPI\r | |
288 | SmmBase2InSmram (\r | |
289 | IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r | |
290 | OUT BOOLEAN *InSmram\r | |
291 | )\r | |
292 | {\r | |
293 | if (InSmram == NULL) {\r | |
294 | return EFI_INVALID_PARAMETER;\r | |
295 | }\r | |
296 | \r | |
297 | *InSmram = gSmmCorePrivate->InSmm;\r | |
298 | \r | |
299 | return EFI_SUCCESS;\r | |
300 | }\r | |
301 | \r | |
302 | /**\r | |
303 | Retrieves the location of the System Management System Table (SMST).\r | |
304 | \r | |
305 | @param This The EFI_SMM_BASE2_PROTOCOL instance.\r | |
306 | @param Smst On return, points to a pointer to the System Management Service Table (SMST).\r | |
307 | \r | |
308 | @retval EFI_INVALID_PARAMETER Smst or This was invalid.\r | |
309 | @retval EFI_SUCCESS The memory was returned to the system.\r | |
310 | @retval EFI_UNSUPPORTED Not in SMM.\r | |
311 | \r | |
312 | **/\r | |
313 | EFI_STATUS\r | |
314 | EFIAPI\r | |
315 | SmmBase2GetSmstLocation (\r | |
316 | IN CONST EFI_SMM_BASE2_PROTOCOL *This,\r | |
317 | OUT EFI_SMM_SYSTEM_TABLE2 **Smst\r | |
318 | )\r | |
319 | {\r | |
320 | if ((This == NULL) ||(Smst == NULL)) {\r | |
321 | return EFI_INVALID_PARAMETER;\r | |
322 | }\r | |
323 | \r | |
324 | if (!gSmmCorePrivate->InSmm) {\r | |
325 | return EFI_UNSUPPORTED;\r | |
326 | }\r | |
327 | \r | |
328 | *Smst = gSmmCorePrivate->Smst;\r | |
329 | \r | |
330 | return EFI_SUCCESS;\r | |
331 | }\r | |
332 | \r | |
333 | /**\r | |
334 | Communicates with a registered handler.\r | |
335 | \r | |
336 | This function provides a service to send and receive messages from a registered \r | |
337 | UEFI service. This function is part of the SMM Communication Protocol that may \r | |
338 | be called in physical mode prior to SetVirtualAddressMap() and in virtual mode \r | |
339 | after SetVirtualAddressMap().\r | |
340 | \r | |
341 | @param[in] This The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r | |
342 | @param[in, out] CommBuffer A pointer to the buffer to convey into SMRAM.\r | |
343 | @param[in, out] CommSize The size of the data buffer being passed in.On exit, the size of data\r | |
344 | being returned. Zero if the handler does not wish to reply with any data.\r | |
345 | \r | |
346 | @retval EFI_SUCCESS The message was successfully posted.\r | |
347 | @retval EFI_INVALID_PARAMETER The CommBuffer was NULL.\r | |
348 | **/\r | |
349 | EFI_STATUS\r | |
350 | EFIAPI\r | |
351 | SmmCommunicationCommunicate (\r | |
352 | IN CONST EFI_SMM_COMMUNICATION_PROTOCOL *This,\r | |
353 | IN OUT VOID *CommBuffer,\r | |
354 | IN OUT UINTN *CommSize\r | |
355 | )\r | |
356 | {\r | |
357 | EFI_STATUS Status;\r | |
358 | EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r | |
359 | BOOLEAN OldInSmm;\r | |
360 | \r | |
361 | //\r | |
362 | // Check parameters\r | |
363 | //\r | |
364 | if ((CommBuffer == NULL) || (CommSize == NULL)) {\r | |
365 | return EFI_INVALID_PARAMETER;\r | |
366 | }\r | |
367 | \r | |
368 | //\r | |
369 | // If not already in SMM, then generate a Software SMI\r | |
370 | //\r | |
371 | if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) {\r | |
372 | //\r | |
373 | // Put arguments for Software SMI in gSmmCorePrivate\r | |
374 | //\r | |
375 | gSmmCorePrivate->CommunicationBuffer = CommBuffer;\r | |
376 | gSmmCorePrivate->BufferSize = CommSize;\r | |
377 | \r | |
378 | //\r | |
379 | // Generate Software SMI\r | |
380 | //\r | |
381 | Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0);\r | |
382 | if (EFI_ERROR (Status)) {\r | |
383 | return EFI_UNSUPPORTED;\r | |
384 | }\r | |
385 | \r | |
386 | //\r | |
387 | // Return status from software SMI \r | |
388 | //\r | |
389 | return gSmmCorePrivate->ReturnStatus;\r | |
390 | }\r | |
391 | \r | |
392 | //\r | |
393 | // If we are in SMM, then the execution mode must be physical, which means that\r | |
394 | // OS established virtual addresses can not be used. If SetVirtualAddressMap()\r | |
395 | // has been called, then a direct invocation of the Software SMI is not \r | |
396 | // not allowed so return EFI_INVALID_PARAMETER.\r | |
397 | //\r | |
398 | if (EfiGoneVirtual()) {\r | |
399 | return EFI_INVALID_PARAMETER;\r | |
400 | }\r | |
401 | \r | |
402 | //\r | |
403 | // Don't allow call SmiManage() directly when SMRAM is closed or locked.\r | |
404 | //\r | |
405 | if (!mSmmAccess->OpenState || mSmmAccess->LockState) {\r | |
406 | return EFI_INVALID_PARAMETER;\r | |
407 | }\r | |
408 | \r | |
409 | //\r | |
410 | // Save current InSmm state and set InSmm state to TRUE\r | |
411 | //\r | |
412 | OldInSmm = gSmmCorePrivate->InSmm;\r | |
413 | gSmmCorePrivate->InSmm = TRUE;\r | |
414 | \r | |
415 | //\r | |
416 | // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.\r | |
417 | //\r | |
418 | CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;\r | |
419 | *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r | |
420 | Status = gSmmCorePrivate->Smst->SmiManage (\r | |
421 | &CommunicateHeader->HeaderGuid, \r | |
422 | NULL, \r | |
423 | CommunicateHeader->Data, \r | |
424 | CommSize\r | |
425 | );\r | |
426 | \r | |
427 | //\r | |
428 | // Update CommunicationBuffer, BufferSize and ReturnStatus\r | |
429 | // Communicate service finished, reset the pointer to CommBuffer to NULL\r | |
430 | //\r | |
431 | *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r | |
432 | \r | |
433 | //\r | |
434 | // Restore original InSmm state\r | |
435 | //\r | |
436 | gSmmCorePrivate->InSmm = OldInSmm;\r | |
437 | \r | |
438 | return (Status == EFI_WARN_INTERRUPT_SOURCE_QUIESCED) ? EFI_SUCCESS : EFI_NOT_FOUND;\r | |
439 | }\r | |
440 | \r | |
441 | /**\r | |
442 | Event notification that is fired when DxeDispatch Event Group is signaled.\r | |
443 | \r | |
444 | @param Event The Event that is being processed, not used.\r | |
445 | @param Context Event Context, not used.\r | |
446 | \r | |
447 | **/\r | |
448 | VOID\r | |
449 | EFIAPI\r | |
450 | SmmIplGuidedEventNotify (\r | |
451 | IN EFI_EVENT Event,\r | |
452 | IN VOID *Context\r | |
453 | )\r | |
454 | {\r | |
455 | EFI_SMM_COMMUNICATE_HEADER CommunicateHeader;\r | |
456 | UINTN Size;\r | |
457 | \r | |
458 | //\r | |
459 | // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure \r | |
460 | //\r | |
461 | CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r | |
462 | CommunicateHeader.MessageLength = 1;\r | |
463 | CommunicateHeader.Data[0] = 0;\r | |
464 | \r | |
465 | //\r | |
466 | // Generate the Software SMI and return the result\r | |
467 | //\r | |
468 | Size = sizeof (CommunicateHeader);\r | |
469 | SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);\r | |
470 | }\r | |
471 | \r | |
472 | /**\r | |
473 | Event notification that is fired every time a gEfiSmmConfigurationProtocol installs.\r | |
474 | \r | |
475 | @param Event The Event that is being processed, not used.\r | |
476 | @param Context Event Context, not used.\r | |
477 | \r | |
478 | **/\r | |
479 | VOID\r | |
480 | EFIAPI\r | |
481 | SmmIplSmmConfigurationEventNotify (\r | |
482 | IN EFI_EVENT Event,\r | |
483 | IN VOID *Context\r | |
484 | )\r | |
485 | {\r | |
486 | EFI_STATUS Status;\r | |
487 | EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r | |
488 | \r | |
489 | //\r | |
490 | // Make sure this notification is for this handler\r | |
491 | //\r | |
492 | Status = gBS->LocateProtocol (Context, NULL, (VOID **)&SmmConfiguration);\r | |
493 | if (EFI_ERROR (Status)) {\r | |
494 | return;\r | |
495 | }\r | |
496 | \r | |
497 | //\r | |
498 | // Register the SMM Entry Point provided by the SMM Core with the SMM COnfiguration protocol\r | |
499 | //\r | |
500 | Status = SmmConfiguration->RegisterSmmEntry (SmmConfiguration, gSmmCorePrivate->SmmEntryPoint);\r | |
501 | ASSERT_EFI_ERROR (Status);\r | |
502 | \r | |
503 | //\r | |
504 | // Set flag to indicate that the SM< Entry Point has been registered which \r | |
505 | // means that SMIs are now fully operational.\r | |
506 | //\r | |
507 | gSmmCorePrivate->SmmEntryPointRegistered = TRUE;\r | |
508 | \r | |
509 | //\r | |
510 | // Print debug message showing SMM Core entry point address.\r | |
511 | //\r | |
512 | DEBUG ((DEBUG_INFO, "SMM IPL registered SMM Entry Point address %p\n", (VOID *)(UINTN)gSmmCorePrivate->SmmEntryPoint));\r | |
513 | \r | |
514 | //\r | |
515 | // Attempt to reset SMRAM cacheability to UC\r | |
516 | //\r | |
517 | Status = gDS->SetMemorySpaceAttributes(\r | |
518 | mCurrentSmramRange->CpuStart, \r | |
519 | mCurrentSmramRange->PhysicalSize,\r | |
520 | EFI_MEMORY_UC\r | |
521 | );\r | |
522 | if (EFI_ERROR (Status)) {\r | |
523 | DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r | |
524 | } \r | |
525 | \r | |
526 | //\r | |
527 | // Close all SMRAM ranges to protect SMRAM\r | |
528 | //\r | |
529 | Status = mSmmAccess->Close (mSmmAccess);\r | |
530 | ASSERT_EFI_ERROR (Status);\r | |
531 | \r | |
532 | //\r | |
533 | // Print debug message that the SMRAM window is now closed.\r | |
534 | //\r | |
535 | DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r | |
536 | }\r | |
537 | \r | |
538 | /**\r | |
539 | Event notification that is fired every time a DxeSmmReadyToLock protocol is added\r | |
540 | or if gEfiEventReadyToBootGuid is signalled.\r | |
541 | \r | |
542 | @param Event The Event that is being processed, not used.\r | |
543 | @param Context Event Context, not used.\r | |
544 | \r | |
545 | **/\r | |
546 | VOID\r | |
547 | EFIAPI\r | |
548 | SmmIplReadyToLockEventNotify (\r | |
549 | IN EFI_EVENT Event,\r | |
550 | IN VOID *Context\r | |
551 | )\r | |
552 | {\r | |
553 | EFI_STATUS Status;\r | |
554 | VOID *Interface;\r | |
555 | UINTN Index;\r | |
556 | \r | |
557 | //\r | |
558 | // See if we are already locked\r | |
559 | //\r | |
560 | if (mSmmLocked) {\r | |
561 | return;\r | |
562 | }\r | |
563 | \r | |
564 | //\r | |
565 | // Make sure this notification is for this handler\r | |
566 | //\r | |
567 | if (CompareGuid ((EFI_GUID *)Context, &gEfiDxeSmmReadyToLockProtocolGuid)) {\r | |
568 | Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);\r | |
569 | if (EFI_ERROR (Status)) {\r | |
570 | return;\r | |
571 | }\r | |
572 | } else {\r | |
573 | //\r | |
574 | // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being \r | |
575 | // signalled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.\r | |
576 | // Print a warning on debug builds.\r | |
577 | //\r | |
578 | DEBUG ((DEBUG_WARN, "SMM IPL! DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));\r | |
579 | }\r | |
580 | \r | |
581 | //\r | |
582 | // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r | |
583 | //\r | |
584 | mSmmAccess->Lock (mSmmAccess);\r | |
585 | \r | |
586 | //\r | |
587 | // Close protocol and event notification events that do not apply after the \r | |
588 | // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot \r | |
589 | // event has been signalled.\r | |
590 | //\r | |
591 | for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r | |
592 | if (mSmmIplEvents[Index].CloseOnLock) {\r | |
593 | gBS->CloseEvent (mSmmIplEvents[Index].Event);\r | |
594 | }\r | |
595 | }\r | |
596 | \r | |
597 | //\r | |
598 | // Inform SMM Core that the DxeSmmReadyToLock protocol was installed\r | |
599 | //\r | |
600 | SmmIplGuidedEventNotify (Event, (VOID *)&gEfiDxeSmmReadyToLockProtocolGuid);\r | |
601 | \r | |
602 | //\r | |
603 | // Print debug message that the SMRAM window is now locked.\r | |
604 | //\r | |
605 | DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));\r | |
606 | \r | |
607 | //\r | |
608 | // Set flag so this operation will not be performed again\r | |
609 | //\r | |
610 | mSmmLocked = TRUE;\r | |
611 | }\r | |
612 | \r | |
613 | /**\r | |
614 | Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r | |
615 | \r | |
616 | This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r | |
617 | It convers pointer to new virtual address.\r | |
618 | \r | |
619 | @param Event Event whose notification function is being invoked.\r | |
620 | @param Context Pointer to the notification function's context.\r | |
621 | \r | |
622 | **/\r | |
623 | VOID\r | |
624 | EFIAPI\r | |
625 | SmmIplSetVirtualAddressNotify (\r | |
626 | IN EFI_EVENT Event,\r | |
627 | IN VOID *Context\r | |
628 | )\r | |
629 | {\r | |
630 | EfiConvertPointer (0x0, (VOID **)&mSmmControl2);\r | |
631 | }\r | |
632 | \r | |
633 | /**\r | |
634 | Searches all Firmware Volumes for the first file matching FileType and SectionType and returns the section data.\r | |
635 | \r | |
636 | @param FileType FileType to search for within any of the firmware volumes in the platform.\r | |
637 | @param SectionType SectionType to search for within any of the matching FileTypes in the firmware volumes in the platform.\r | |
638 | @param SourceSize Return the size of the returned section data..\r | |
639 | \r | |
640 | @retval != NULL Pointer to the allocated buffer containing the section data.\r | |
641 | @retval NULL Section data was not found.\r | |
642 | \r | |
643 | **/\r | |
644 | VOID *\r | |
645 | GetSectionInAnyFv (\r | |
646 | IN EFI_FV_FILETYPE FileType,\r | |
647 | IN EFI_SECTION_TYPE SectionType,\r | |
648 | OUT UINTN *SourceSize\r | |
649 | )\r | |
650 | {\r | |
651 | EFI_STATUS Status;\r | |
652 | UINTN HandleCount;\r | |
653 | EFI_HANDLE *HandleBuffer;\r | |
654 | UINTN Index;\r | |
655 | EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r | |
656 | UINTN Key;\r | |
657 | EFI_GUID NameGuid;\r | |
658 | EFI_FV_FILE_ATTRIBUTES Attributes;\r | |
659 | VOID *SourceBuffer;\r | |
660 | UINT32 AuthenticationStatus;\r | |
661 | \r | |
662 | HandleBuffer = NULL;\r | |
663 | Status = gBS->LocateHandleBuffer (\r | |
664 | ByProtocol,\r | |
665 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
666 | NULL,\r | |
667 | &HandleCount,\r | |
668 | &HandleBuffer\r | |
669 | );\r | |
670 | if (EFI_ERROR (Status)) {\r | |
671 | return NULL;\r | |
672 | }\r | |
673 | \r | |
674 | for (Index = 0; Index < HandleCount; Index++) {\r | |
675 | Status = gBS->HandleProtocol (\r | |
676 | HandleBuffer[Index],\r | |
677 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
678 | (VOID **)&Fv\r | |
679 | );\r | |
680 | if (EFI_ERROR (Status)) {\r | |
681 | continue;\r | |
682 | }\r | |
683 | \r | |
684 | //\r | |
685 | // Use Firmware Volume 2 Protocol to search for a file of type FileType\r | |
686 | //\r | |
687 | Key = 0; \r | |
688 | Status = Fv->GetNextFile (Fv, &Key, &FileType, &NameGuid, &Attributes, SourceSize);\r | |
689 | if (EFI_ERROR (Status)) {\r | |
690 | continue;\r | |
691 | }\r | |
692 | \r | |
693 | //\r | |
694 | // Use Firmware Volume 2 Protocol to read a section of type SectionType\r | |
695 | //\r | |
696 | SourceBuffer = NULL;\r | |
697 | Status = Fv->ReadSection (Fv, &NameGuid, SectionType, 0, &SourceBuffer, SourceSize, &AuthenticationStatus);\r | |
698 | if (!EFI_ERROR (Status)) {\r | |
699 | FreePool (HandleBuffer);\r | |
700 | return SourceBuffer;\r | |
701 | }\r | |
702 | } \r | |
703 | \r | |
704 | FreePool(HandleBuffer);\r | |
705 | \r | |
706 | return NULL;\r | |
707 | }\r | |
708 | /**\r | |
709 | Get the fixed loadding address from image header assigned by build tool. This function only be called\r | |
710 | when Loading module at Fixed address feature enabled.\r | |
711 | \r | |
712 | @param ImageContext Pointer to the image context structure that describes the PE/COFF\r | |
713 | image that needs to be examined by this function.\r | |
714 | @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r | |
715 | @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.\r | |
716 | **/\r | |
717 | EFI_STATUS\r | |
718 | GetPeCoffImageFixLoadingAssignedAddress(\r | |
719 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
720 | )\r | |
721 | {\r | |
722 | UINTN SectionHeaderOffset;\r | |
723 | EFI_STATUS Status;\r | |
724 | EFI_IMAGE_SECTION_HEADER SectionHeader;\r | |
725 | EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r | |
726 | EFI_PHYSICAL_ADDRESS FixLoaddingAddress;\r | |
727 | UINT16 Index;\r | |
728 | UINTN Size;\r | |
729 | UINT16 NumberOfSections;\r | |
730 | EFI_PHYSICAL_ADDRESS SmramBase;\r | |
731 | UINT64 SmmCodeSize;\r | |
732 | UINT64 ValueInSectionHeader;\r | |
733 | //\r | |
734 | // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r | |
735 | //\r | |
736 | SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));\r | |
737 | \r | |
738 | FixLoaddingAddress = 0;\r | |
739 | Status = EFI_NOT_FOUND;\r | |
740 | SmramBase = mCurrentSmramRange->CpuStart;\r | |
741 | //\r | |
742 | // Get PeHeader pointer\r | |
743 | //\r | |
744 | ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r | |
745 | SectionHeaderOffset = (UINTN)(\r | |
746 | ImageContext->PeCoffHeaderOffset +\r | |
747 | sizeof (UINT32) +\r | |
748 | sizeof (EFI_IMAGE_FILE_HEADER) +\r | |
749 | ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r | |
750 | );\r | |
751 | NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r | |
752 | \r | |
753 | //\r | |
754 | // Get base address from the first section header that doesn't point to code section.\r | |
755 | //\r | |
756 | for (Index = 0; Index < NumberOfSections; Index++) {\r | |
757 | //\r | |
758 | // Read section header from file\r | |
759 | //\r | |
760 | Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
761 | Status = ImageContext->ImageRead (\r | |
762 | ImageContext->Handle,\r | |
763 | SectionHeaderOffset,\r | |
764 | &Size,\r | |
765 | &SectionHeader\r | |
766 | );\r | |
767 | if (EFI_ERROR (Status)) {\r | |
768 | return Status;\r | |
769 | }\r | |
770 | \r | |
771 | Status = EFI_NOT_FOUND;\r | |
772 | \r | |
773 | if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r | |
774 | //\r | |
775 | // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the\r | |
776 | // first section header that doesn't point to code section in image header. And there is an assumption that when the\r | |
777 | // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers\r | |
778 | // fields should NOT be Zero, or else, these 2 fileds should be set to Zero\r | |
779 | //\r | |
780 | ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r | |
781 | if (ValueInSectionHeader != 0) {\r | |
782 | //\r | |
783 | // Found first section header that doesn't point to code section in which uild tool saves the\r | |
784 | // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r | |
785 | //\r | |
786 | FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r | |
787 | \r | |
788 | if (SmramBase + SmmCodeSize > FixLoaddingAddress && SmramBase <= FixLoaddingAddress) {\r | |
789 | //\r | |
790 | // The assigned address is valid. Return the specified loadding address\r | |
791 | //\r | |
792 | ImageContext->ImageAddress = FixLoaddingAddress;\r | |
793 | Status = EFI_SUCCESS;\r | |
794 | }\r | |
795 | }\r | |
796 | break;\r | |
797 | }\r | |
798 | SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
799 | }\r | |
800 | DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoaddingAddress, Status));\r | |
801 | return Status;\r | |
802 | }\r | |
803 | /**\r | |
804 | Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r | |
805 | \r | |
806 | @param[in] SmramRange Descriptor for the range of SMRAM to reload the \r | |
807 | currently executing image.\r | |
808 | @param[in] Context Context to pass into SMM Core\r | |
809 | \r | |
810 | @return EFI_STATUS\r | |
811 | \r | |
812 | **/\r | |
813 | EFI_STATUS\r | |
814 | ExecuteSmmCoreFromSmram (\r | |
815 | IN EFI_SMRAM_DESCRIPTOR *SmramRange,\r | |
816 | IN VOID *Context\r | |
817 | )\r | |
818 | {\r | |
819 | EFI_STATUS Status;\r | |
820 | VOID *SourceBuffer;\r | |
821 | UINTN SourceSize;\r | |
822 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r | |
823 | UINTN PageCount;\r | |
824 | EFI_PHYSICAL_ADDRESS DestinationBuffer;\r | |
825 | EFI_IMAGE_ENTRY_POINT EntryPoint;\r | |
826 | \r | |
827 | //\r | |
828 | // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE\r | |
829 | // \r | |
830 | SourceBuffer = GetSectionInAnyFv (EFI_FV_FILETYPE_SMM_CORE, EFI_SECTION_PE32, &SourceSize);\r | |
831 | if (SourceBuffer == NULL) {\r | |
832 | return EFI_NOT_FOUND;\r | |
833 | }\r | |
834 | \r | |
835 | //\r | |
836 | // Initilize ImageContext\r | |
837 | //\r | |
838 | ImageContext.Handle = SourceBuffer;\r | |
839 | ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r | |
840 | \r | |
841 | //\r | |
842 | // Get information about the image being loaded\r | |
843 | //\r | |
844 | Status = PeCoffLoaderGetImageInfo (&ImageContext);\r | |
845 | if (EFI_ERROR (Status)) {\r | |
846 | return Status;\r | |
847 | }\r | |
848 | //\r | |
849 | // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to \r | |
850 | // the address assigned by build tool.\r | |
851 | //\r | |
852 | if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r | |
853 | //\r | |
854 | // Get the fixed loading address assigned by Build tool\r | |
855 | //\r | |
856 | Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);\r | |
857 | if (!EFI_ERROR (Status)) {\r | |
858 | //\r | |
859 | // 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 | |
860 | //\r | |
861 | PageCount = 0;\r | |
862 | } else {\r | |
863 | DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));\r | |
864 | //\r | |
865 | // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r | |
866 | // specified by SmramRange\r | |
867 | //\r | |
868 | PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);\r | |
869 | \r | |
870 | ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r | |
871 | ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r | |
872 | \r | |
873 | SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r | |
874 | DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;\r | |
875 | \r | |
876 | //\r | |
877 | // Align buffer on section boundry\r | |
878 | //\r | |
879 | ImageContext.ImageAddress = DestinationBuffer;\r | |
880 | }\r | |
881 | } else {\r | |
882 | //\r | |
883 | // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r | |
884 | // specified by SmramRange\r | |
885 | //\r | |
886 | PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);\r | |
887 | \r | |
888 | ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0);\r | |
889 | ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));\r | |
890 | \r | |
891 | SmramRange->PhysicalSize -= EFI_PAGES_TO_SIZE (PageCount);\r | |
892 | DestinationBuffer = SmramRange->CpuStart + SmramRange->PhysicalSize;\r | |
893 | \r | |
894 | //\r | |
895 | // Align buffer on section boundry\r | |
896 | //\r | |
897 | ImageContext.ImageAddress = DestinationBuffer;\r | |
898 | }\r | |
899 | \r | |
900 | ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r | |
901 | ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r | |
902 | \r | |
903 | //\r | |
904 | // Print debug message showing SMM Core load address.\r | |
905 | //\r | |
906 | DEBUG ((DEBUG_INFO, "SMM IPL loading SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.ImageAddress));\r | |
907 | \r | |
908 | //\r | |
909 | // Load the image to our new buffer\r | |
910 | //\r | |
911 | Status = PeCoffLoaderLoadImage (&ImageContext);\r | |
912 | if (!EFI_ERROR (Status)) {\r | |
913 | //\r | |
914 | // Relocate the image in our new buffer\r | |
915 | //\r | |
916 | Status = PeCoffLoaderRelocateImage (&ImageContext);\r | |
917 | if (!EFI_ERROR (Status)) {\r | |
918 | //\r | |
919 | // Flush the instruction cache so the image data are written before we execute it\r | |
920 | //\r | |
921 | InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r | |
922 | \r | |
923 | //\r | |
924 | // Print debug message showing SMM Core entry point address.\r | |
925 | //\r | |
926 | DEBUG ((DEBUG_INFO, "SMM IPL calling SMM Core at SMRAM address %p\n", (VOID *)(UINTN)ImageContext.EntryPoint));\r | |
927 | \r | |
928 | //\r | |
929 | // Execute image\r | |
930 | //\r | |
931 | EntryPoint = (EFI_IMAGE_ENTRY_POINT)(UINTN)ImageContext.EntryPoint;\r | |
932 | Status = EntryPoint ((EFI_HANDLE)Context, gST);\r | |
933 | }\r | |
934 | }\r | |
935 | \r | |
936 | //\r | |
937 | // If the load operation, relocate operation, or the image execution return an\r | |
938 | // error, then free memory allocated from the EFI_SRAM_DESCRIPTOR specified by \r | |
939 | // SmramRange\r | |
940 | //\r | |
941 | if (EFI_ERROR (Status)) {\r | |
942 | SmramRange->PhysicalSize += EFI_PAGES_TO_SIZE (PageCount);\r | |
943 | }\r | |
944 | \r | |
945 | //\r | |
946 | // Always free memory allocted by GetFileBufferByFilePath ()\r | |
947 | //\r | |
948 | FreePool (SourceBuffer);\r | |
949 | \r | |
950 | return Status;\r | |
951 | }\r | |
952 | \r | |
953 | /**\r | |
954 | The Entry Point for SMM IPL\r | |
955 | \r | |
956 | Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install \r | |
957 | SMM Base 2 Protocol and SMM Communication Protocol, and register for the \r | |
958 | critical events required to coordinate between DXE and SMM environments.\r | |
959 | \r | |
960 | @param ImageHandle The firmware allocated handle for the EFI image.\r | |
961 | @param SystemTable A pointer to the EFI System Table.\r | |
962 | \r | |
963 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
964 | @retval Other Some error occurred when executing this entry point.\r | |
965 | \r | |
966 | **/\r | |
967 | EFI_STATUS\r | |
968 | EFIAPI\r | |
969 | SmmIplEntry (\r | |
970 | IN EFI_HANDLE ImageHandle,\r | |
971 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
972 | )\r | |
973 | {\r | |
974 | EFI_STATUS Status;\r | |
975 | EFI_SMM_CONFIGURATION_PROTOCOL *SmmConfiguration;\r | |
976 | UINTN Size;\r | |
977 | UINTN Index;\r | |
978 | EFI_SMM_RESERVED_SMRAM_REGION *SmramResRegion;\r | |
979 | UINT64 MaxSize;\r | |
980 | VOID *Registration;\r | |
981 | UINT64 SmmCodeSize;\r | |
982 | EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE *LMFAConfigurationTable;\r | |
983 | \r | |
984 | //\r | |
985 | // Fill in the image handle of the SMM IPL so the SMM Core can use this as the \r | |
986 | // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded \r | |
987 | // by the SMM Core\r | |
988 | //\r | |
989 | mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r | |
990 | \r | |
991 | //\r | |
992 | // Get SMM Access Protocol\r | |
993 | //\r | |
994 | Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&mSmmAccess);\r | |
995 | ASSERT_EFI_ERROR (Status);\r | |
996 | \r | |
997 | //\r | |
998 | // Get SMM Control2 Protocol\r | |
999 | //\r | |
1000 | Status = gBS->LocateProtocol (&gEfiSmmControl2ProtocolGuid, NULL, (VOID **)&mSmmControl2);\r | |
1001 | ASSERT_EFI_ERROR (Status);\r | |
1002 | \r | |
1003 | //\r | |
1004 | // Get SMM Configuration Protocol if it is present\r | |
1005 | //\r | |
1006 | SmmConfiguration = NULL;\r | |
1007 | Status = gBS->LocateProtocol (&gEfiSmmConfigurationProtocolGuid, NULL, (VOID **) &SmmConfiguration);\r | |
1008 | \r | |
1009 | //\r | |
1010 | // Get SMRAM information\r | |
1011 | //\r | |
1012 | Size = 0;\r | |
1013 | Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, NULL);\r | |
1014 | ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r | |
1015 | \r | |
1016 | gSmmCorePrivate->SmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size);\r | |
1017 | ASSERT (gSmmCorePrivate->SmramRanges != NULL);\r | |
1018 | \r | |
1019 | Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, gSmmCorePrivate->SmramRanges);\r | |
1020 | ASSERT_EFI_ERROR (Status);\r | |
1021 | \r | |
1022 | gSmmCorePrivate->SmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r | |
1023 | \r | |
1024 | //\r | |
1025 | // Open all SMRAM ranges\r | |
1026 | //\r | |
1027 | Status = mSmmAccess->Open (mSmmAccess);\r | |
1028 | ASSERT_EFI_ERROR (Status);\r | |
1029 | \r | |
1030 | //\r | |
1031 | // Print debug message that the SMRAM window is now open.\r | |
1032 | //\r | |
1033 | DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r | |
1034 | \r | |
1035 | //\r | |
1036 | // Subtract SMRAM any reserved SMRAM regions.\r | |
1037 | //\r | |
1038 | if (SmmConfiguration != NULL) {\r | |
1039 | SmramResRegion = SmmConfiguration->SmramReservedRegions;\r | |
1040 | while (SmramResRegion->SmramReservedSize != 0) {\r | |
1041 | for (Index = 0; Index < gSmmCorePrivate->SmramRangeCount; Index ++) {\r | |
1042 | if ((SmramResRegion->SmramReservedStart >= gSmmCorePrivate->SmramRanges[Index].CpuStart) && \\r | |
1043 | ((SmramResRegion->SmramReservedStart + SmramResRegion->SmramReservedSize) <= \\r | |
1044 | (gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize))) {\r | |
1045 | //\r | |
1046 | // This range has reserved area, calculate the left free size\r | |
1047 | //\r | |
1048 | gSmmCorePrivate->SmramRanges[Index].PhysicalSize = SmramResRegion->SmramReservedStart - gSmmCorePrivate->SmramRanges[Index].CpuStart;\r | |
1049 | }\r | |
1050 | }\r | |
1051 | SmramResRegion++;\r | |
1052 | }\r | |
1053 | }\r | |
1054 | \r | |
1055 | //\r | |
1056 | // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r | |
1057 | //\r | |
1058 | mCurrentSmramRange = NULL;\r | |
1059 | for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < gSmmCorePrivate->SmramRangeCount; Index++) {\r | |
1060 | if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r | |
1061 | if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {\r | |
1062 | if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r | |
1063 | MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r | |
1064 | mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r | |
1065 | }\r | |
1066 | }\r | |
1067 | }\r | |
1068 | }\r | |
1069 | \r | |
1070 | if (mCurrentSmramRange != NULL) {\r | |
1071 | //\r | |
1072 | // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r | |
1073 | //\r | |
1074 | DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n", \r | |
1075 | (VOID *)(UINTN)mCurrentSmramRange->CpuStart, \r | |
1076 | (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r | |
1077 | ));\r | |
1078 | \r | |
1079 | //\r | |
1080 | // Attempt to set SMRAM cacheability to WB\r | |
1081 | //\r | |
1082 | Status = gDS->SetMemorySpaceAttributes(\r | |
1083 | mCurrentSmramRange->CpuStart, \r | |
1084 | mCurrentSmramRange->PhysicalSize,\r | |
1085 | EFI_MEMORY_WB\r | |
1086 | );\r | |
1087 | if (EFI_ERROR (Status)) {\r | |
1088 | DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r | |
1089 | } \r | |
1090 | //\r | |
1091 | // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load\r | |
1092 | // Modules At Fixed Address Configuration Table.\r | |
1093 | //\r | |
1094 | if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r | |
1095 | //\r | |
1096 | // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r | |
1097 | //\r | |
1098 | SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);\r | |
1099 | //\r | |
1100 | // The SMRAM available memory is assumed to be larger than SmmCodeSize\r | |
1101 | //\r | |
1102 | ASSERT (mCurrentSmramRange->PhysicalSize > SmmCodeSize);\r | |
1103 | //\r | |
1104 | // Retrieve Load modules At fixed address configuration table and save the SMRAM base.\r | |
1105 | //\r | |
1106 | Status = EfiGetSystemConfigurationTable (\r | |
1107 | &gLoadFixedAddressConfigurationTableGuid,\r | |
1108 | (VOID **) &LMFAConfigurationTable\r | |
1109 | );\r | |
1110 | if (!EFI_ERROR (Status) && LMFAConfigurationTable != NULL) {\r | |
1111 | LMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r | |
1112 | //\r | |
1113 | // Print the SMRAM base\r | |
1114 | //\r | |
1115 | DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable->SmramBase));\r | |
1116 | }\r | |
1117 | }\r | |
1118 | //\r | |
1119 | // Load SMM Core into SMRAM and execute it from SMRAM\r | |
1120 | //\r | |
1121 | Status = ExecuteSmmCoreFromSmram (mCurrentSmramRange, gSmmCorePrivate);\r | |
1122 | if (EFI_ERROR (Status)) {\r | |
1123 | //\r | |
1124 | // Print error message that the SMM Core failed to be loaded and executed.\r | |
1125 | //\r | |
1126 | DEBUG ((DEBUG_ERROR, "SMM IPL could not load and execute SMM Core from SMRAM\n"));\r | |
1127 | \r | |
1128 | //\r | |
1129 | // Attempt to reset SMRAM cacheability to UC\r | |
1130 | //\r | |
1131 | Status = gDS->SetMemorySpaceAttributes(\r | |
1132 | mCurrentSmramRange->CpuStart, \r | |
1133 | mCurrentSmramRange->PhysicalSize,\r | |
1134 | EFI_MEMORY_UC\r | |
1135 | );\r | |
1136 | if (EFI_ERROR (Status)) {\r | |
1137 | DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r | |
1138 | } \r | |
1139 | }\r | |
1140 | } else {\r | |
1141 | //\r | |
1142 | // Print error message that there are not enough SMRAM resources to load the SMM Core.\r | |
1143 | //\r | |
1144 | DEBUG ((DEBUG_ERROR, "SMM IPL could not find a large enough SMRAM region to load SMM Core\n"));\r | |
1145 | }\r | |
1146 | \r | |
1147 | //\r | |
1148 | // If the SMM Core could not be loaded then close SMRAM window, free allocated \r | |
1149 | // resources, and return an error so SMM IPL will be unloaded.\r | |
1150 | //\r | |
1151 | if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {\r | |
1152 | //\r | |
1153 | // Close all SMRAM ranges\r | |
1154 | //\r | |
1155 | Status = mSmmAccess->Close (mSmmAccess);\r | |
1156 | ASSERT_EFI_ERROR (Status);\r | |
1157 | \r | |
1158 | //\r | |
1159 | // Print debug message that the SMRAM window is now closed.\r | |
1160 | //\r | |
1161 | DEBUG ((DEBUG_INFO, "SMM IPL closed SMRAM window\n"));\r | |
1162 | \r | |
1163 | //\r | |
1164 | // Free all allocated resources\r | |
1165 | //\r | |
1166 | FreePool (gSmmCorePrivate->SmramRanges);\r | |
1167 | \r | |
1168 | return EFI_UNSUPPORTED;\r | |
1169 | }\r | |
1170 | \r | |
1171 | //\r | |
1172 | // Install SMM Base2 Protocol and SMM Communication Protocol\r | |
1173 | //\r | |
1174 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
1175 | &mSmmIplHandle,\r | |
1176 | &gEfiSmmBase2ProtocolGuid, &mSmmBase2,\r | |
1177 | &gEfiSmmCommunicationProtocolGuid, &mSmmCommunication,\r | |
1178 | NULL\r | |
1179 | );\r | |
1180 | ASSERT_EFI_ERROR (Status);\r | |
1181 | \r | |
1182 | //\r | |
1183 | // Create the set of protocol and event notififcations that the SMM IPL requires\r | |
1184 | //\r | |
1185 | for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r | |
1186 | if (mSmmIplEvents[Index].Protocol) {\r | |
1187 | mSmmIplEvents[Index].Event = EfiCreateProtocolNotifyEvent (\r | |
1188 | mSmmIplEvents[Index].Guid,\r | |
1189 | TPL_CALLBACK,\r | |
1190 | mSmmIplEvents[Index].NotifyFunction,\r | |
1191 | mSmmIplEvents[Index].NotifyContext,\r | |
1192 | &Registration\r | |
1193 | );\r | |
1194 | } else {\r | |
1195 | Status = gBS->CreateEventEx (\r | |
1196 | EVT_NOTIFY_SIGNAL,\r | |
1197 | TPL_CALLBACK,\r | |
1198 | mSmmIplEvents[Index].NotifyFunction,\r | |
1199 | mSmmIplEvents[Index].NotifyContext,\r | |
1200 | mSmmIplEvents[Index].Guid,\r | |
1201 | &mSmmIplEvents[Index].Event\r | |
1202 | );\r | |
1203 | ASSERT_EFI_ERROR (Status);\r | |
1204 | }\r | |
1205 | }\r | |
1206 | \r | |
1207 | return EFI_SUCCESS;\r | |
1208 | }\r |