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