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