]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Compatibility/SmmBaseOnSmmBase2Thunk/SmmBaseOnSmmBase2Thunk.c
Correct copyright year to 2010
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / SmmBaseOnSmmBase2Thunk / SmmBaseOnSmmBase2Thunk.c
CommitLineData
9e620719 1/** @file\r
2 SMM Base Protocol on SMM Base2 Protocol Thunk driver.\r
3\r
4 This driver co-operates with SMM Base Helper SMM driver to provide SMM Base Protocol\r
5 based on SMM Base2 Protocol.\r
6\r
7 This thunk driver is expected to be loaded before PI SMM IPL driver so that\r
8 SMM BASE Protocol can be published immediately after SMM Base2 Protocol is installed to\r
9 make SMM Base Protocol.InSmm() as early as possible.\r
10\r
11 Copyright (c) 2009 Intel Corporation\r
12 All rights reserved. This program and the accompanying materials\r
13 are licensed and made available under the terms and conditions of the BSD License\r
14 which accompanies this distribution. The full text of the license may be found at\r
15 http://opensource.org/licenses/bsd-license.php\r
16\r
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
19\r
20**/\r
21\r
22#include "SmmBaseOnSmmBase2Thunk.h"\r
23\r
24EFI_SMM_BASE_PROTOCOL gSmmBase = {\r
25 SmmBaseRegister,\r
26 SmmBaseUnregister,\r
27 SmmBaseCommunicate,\r
28 SmmBaseRegisterCallback,\r
29 SmmBaseInSmm,\r
30 SmmBaseSmmAllocatePool,\r
31 SmmBaseSmmFreePool,\r
32 SmmBaseGetSmstLocation\r
33};\r
34\r
35SMMBASETHUNK_COMMUNICATION_DATA gCommunicationData = {\r
36 EFI_SMM_BASE_THUNK_COMMUNICATION_GUID,\r
37 sizeof (SMMBASE_FUNCTION_DATA)\r
38};\r
39\r
40EFI_HANDLE mImageHandle;\r
41EFI_SMM_BASE2_PROTOCOL *mSmmBase2 = NULL;\r
42EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;\r
43EFI_SMM_BASE_HELPER_READY_PROTOCOL *mSmmBaseHelperReady = NULL;\r
44\r
45BOOLEAN\r
46IsInSmm (\r
47 VOID\r
48 )\r
49{\r
50 EFI_STATUS Status;\r
51 BOOLEAN InSmm;\r
52 \r
53 Status = mSmmBase2->InSmm (mSmmBase2, &InSmm);\r
54 ASSERT_EFI_ERROR (Status);\r
55 return InSmm;\r
56}\r
57\r
58/**\r
59 Invoke services provided by SMM Base Helper SMM driver.\r
60**/\r
61VOID\r
62SmmBaseHelperService (\r
63 VOID\r
64 )\r
65{\r
66 UINTN DataSize;\r
67\r
68 gCommunicationData.FunctionData.Status = EFI_UNSUPPORTED;\r
69\r
70 if (IsInSmm()) {\r
71 ///\r
72 /// If in SMM mode, directly call services in SMM Base Helper.\r
73 ///\r
74 if (mSmmBaseHelperReady == NULL) {\r
75 ASSERT (FALSE);\r
76 return;\r
77 }\r
78\r
79 DataSize = (UINTN)(sizeof (SMMBASE_FUNCTION_DATA));\r
80 mSmmBaseHelperReady->ServiceEntry (\r
81 NULL,\r
82 NULL,\r
83 &gCommunicationData.FunctionData,\r
84 &DataSize\r
85 );\r
86 } else {\r
87 ///\r
88 /// If in non-SMM mode, call services in SMM Base Helper via SMM Communication Protocol.\r
89 ///\r
90 if (mSmmCommunication == NULL) {\r
91 ASSERT (FALSE);\r
92 return;\r
93 }\r
94\r
95 DataSize = (UINTN)(sizeof (gCommunicationData));\r
96 mSmmCommunication->Communicate (\r
97 mSmmCommunication,\r
98 &gCommunicationData,\r
99 &DataSize\r
100 );\r
101 }\r
102}\r
103\r
104/**\r
105 Register a given driver into SMRAM. This is the equivalent of performing\r
106 the LoadImage/StartImage into System Management Mode.\r
107\r
108 @param[in] This Protocol instance pointer.\r
109 @param[in] FilePath Location of the image to be installed as the handler.\r
110 @param[in] SourceBuffer Optional source buffer in case the image file\r
111 is in memory.\r
112 @param[in] SourceSize Size of the source image file, if in memory.\r
113 @param[out] ImageHandle The handle that the base driver uses to decode \r
114 the handler. Unique among SMM handlers only, \r
115 not unique across DXE/EFI.\r
116 @param[in] LegacyIA32Binary An optional parameter specifying that the associated \r
117 file is a real-mode IA-32 binary.\r
118\r
119 @retval EFI_SUCCESS The operation was successful.\r
120 @retval EFI_OUT_OF_RESOURCES There were no additional SMRAM resources to load the handler\r
121 @retval EFI_UNSUPPORTED This platform does not support 16-bit handlers.\r
122 @retval EFI_UNSUPPORTED Platform is in runtime.\r
123 @retval EFI_INVALID_PARAMETER The handlers was not the correct image type\r
124**/\r
125EFI_STATUS\r
126EFIAPI\r
127SmmBaseRegister (\r
128 IN EFI_SMM_BASE_PROTOCOL *This,\r
129 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
130 IN VOID *SourceBuffer,\r
131 IN UINTN SourceSize,\r
132 OUT EFI_HANDLE *ImageHandle,\r
133 IN BOOLEAN LegacyIA32Binary\r
134 )\r
135{\r
136 if (LegacyIA32Binary) {\r
137 return EFI_UNSUPPORTED;\r
138 }\r
139\r
140 gCommunicationData.FunctionData.Function = SMMBASE_REGISTER;\r
141 gCommunicationData.FunctionData.Args.Register.FilePath = FilePath;\r
142 gCommunicationData.FunctionData.Args.Register.SourceBuffer = SourceBuffer;\r
143 gCommunicationData.FunctionData.Args.Register.SourceSize = SourceSize;\r
144 gCommunicationData.FunctionData.Args.Register.ImageHandle = ImageHandle;\r
145 gCommunicationData.FunctionData.Args.Register.LegacyIA32Binary = LegacyIA32Binary;\r
146\r
147 SmmBaseHelperService ();\r
148 return gCommunicationData.FunctionData.Status;\r
149}\r
150\r
151/**\r
152 Removes a handler from execution within SMRAM. This is the equivalent of performing\r
153 the UnloadImage in System Management Mode.\r
154\r
155 @param[in] This Protocol instance pointer.\r
156 @param[in] ImageHandle The handler to be removed.\r
157\r
158 @retval EFI_SUCCESS The operation was successful\r
159 @retval EFI_INVALID_PARAMETER The handler did not exist\r
160 @retval EFI_UNSUPPORTED Platform is in runtime.\r
161**/\r
162EFI_STATUS\r
163EFIAPI\r
164SmmBaseUnregister (\r
165 IN EFI_SMM_BASE_PROTOCOL *This,\r
166 IN EFI_HANDLE ImageHandle\r
167 )\r
168{\r
169 gCommunicationData.FunctionData.Function = SMMBASE_UNREGISTER;\r
170 gCommunicationData.FunctionData.Args.UnRegister.ImageHandle = ImageHandle;\r
171\r
172 SmmBaseHelperService ();\r
173 return gCommunicationData.FunctionData.Status;\r
174}\r
175\r
176/**\r
177 The SMM Inter-module Communicate Service Communicate() function\r
178 provides a service to send/receive messages from a registered\r
179 EFI service. The BASE protocol driver is responsible for doing\r
180 any of the copies such that the data lives in boot-service-accessible RAM.\r
181\r
182 @param[in] This Protocol instance pointer.\r
183 @param[in] ImageHandle The handle of the registered driver.\r
184 @param[in,out] CommunicationBuffer Pointer to the buffer to convey into SMRAM.\r
185 @param[in,out] BufferSize The size of the data buffer being passed in.\r
186 On exit, the size of data being returned.\r
187 Zero if the handler does not wish to reply with any data.\r
188\r
189 @retval EFI_SUCCESS The message was successfully posted\r
190 @retval EFI_INVALID_PARAMETER The buffer was NULL\r
191**/\r
192EFI_STATUS\r
193EFIAPI\r
194SmmBaseCommunicate (\r
195 IN EFI_SMM_BASE_PROTOCOL *This,\r
196 IN EFI_HANDLE ImageHandle,\r
197 IN OUT VOID *CommunicationBuffer,\r
198 IN OUT UINTN *BufferSize\r
199 )\r
200{\r
201 if (mSmmCommunication == NULL) {\r
202 ASSERT (FALSE);\r
203 return EFI_UNSUPPORTED;\r
204 }\r
205\r
206 return mSmmCommunication->Communicate (\r
207 mSmmCommunication,\r
208 CommunicationBuffer,\r
209 BufferSize\r
210 );\r
211}\r
212\r
213/**\r
214 Register a callback to execute within SMM.\r
215 This allows receipt of messages created with EFI_SMM_BASE_PROTOCOL.Communicate().\r
216\r
217 @param[in] This Protocol instance pointer.\r
218 @param[in] SmmImageHandle Handle of the callback service.\r
219 @param[in] CallbackAddress Address of the callback service.\r
220 @param[in] MakeLast If present, will stipulate that the handler is posted to \r
221 be executed last in the dispatch table.\r
222 @param[in] FloatingPointSave An optional parameter that informs the\r
223 EFI_SMM_ACCESS_PROTOCOL Driver core if it needs to save\r
224 the floating point register state. If any handler\r
225 require this, the state will be saved for all handlers.\r
226\r
227 @retval EFI_SUCCESS The operation was successful\r
228 @retval EFI_OUT_OF_RESOURCES Not enough space in the dispatch queue\r
229 @retval EFI_UNSUPPORTED Platform is in runtime.\r
230 @retval EFI_UNSUPPORTED The caller is not in SMM.\r
231**/\r
232EFI_STATUS\r
233EFIAPI\r
234SmmBaseRegisterCallback (\r
235 IN EFI_SMM_BASE_PROTOCOL *This,\r
236 IN EFI_HANDLE SmmImageHandle,\r
237 IN EFI_SMM_CALLBACK_ENTRY_POINT CallbackAddress,\r
238 IN BOOLEAN MakeLast,\r
239 IN BOOLEAN FloatingPointSave\r
240 )\r
241{\r
242 if (!IsInSmm()) {\r
243 return EFI_UNSUPPORTED;\r
244 }\r
245\r
246 gCommunicationData.FunctionData.Function = SMMBASE_REGISTER_CALLBACK;\r
247 gCommunicationData.FunctionData.Args.RegisterCallback.SmmImageHandle = SmmImageHandle;\r
248 gCommunicationData.FunctionData.Args.RegisterCallback.CallbackAddress = CallbackAddress;\r
249 gCommunicationData.FunctionData.Args.RegisterCallback.MakeLast = MakeLast;\r
250 gCommunicationData.FunctionData.Args.RegisterCallback.FloatingPointSave = FloatingPointSave;\r
251\r
252 SmmBaseHelperService();\r
253 return gCommunicationData.FunctionData.Status;\r
254}\r
255\r
256/**\r
257 This routine tells caller if execution context is SMM or not.\r
258\r
259 @param[in] This Protocol instance pointer.\r
260 @param[out] InSmm Whether the caller is inside SMM for IA-32\r
261 or servicing a PMI for the Itanium processor\r
262 family.\r
263\r
264 @retval EFI_SUCCESS The operation was successful\r
265 @retval EFI_INVALID_PARAMETER InSmm was NULL.\r
266**/\r
267EFI_STATUS\r
268EFIAPI\r
269SmmBaseInSmm (\r
270 IN EFI_SMM_BASE_PROTOCOL *This,\r
271 OUT BOOLEAN *InSmm\r
272 )\r
273{\r
274 return mSmmBase2->InSmm (mSmmBase2, InSmm);\r
275}\r
276\r
277/**\r
278 The SmmAllocatePool() function allocates a memory region of Size bytes from memory of\r
279 type PoolType and returns the address of the allocated memory in the location referenced\r
280 by Buffer. This function allocates pages from EFI SMRAM Memory as needed to grow the\r
281 requested pool type. All allocations are eight-byte aligned.\r
282\r
283 @param[in] This Protocol instance pointer.\r
284 @param[in] PoolType The type of pool to allocate.\r
285 The only supported type is EfiRuntimeServicesData;\r
286 the interface will internally map this runtime request to \r
287 SMRAM for IA-32 and leave as this type for the Itanium \r
288 processor family. Other types can be ignored.\r
289 @param[in] Size The number of bytes to allocate from the pool.\r
290 @param[out] Buffer A pointer to a pointer to the allocated buffer if the call\r
291 succeeds; undefined otherwise.\r
292\r
293 @retval EFI_SUCCESS The requested number of bytes was allocated.\r
294 @retval EFI_OUT_OF_RESOURCES The pool requested could not be allocated.\r
295 @retval EFI_UNSUPPORTED Platform is in runtime.\r
296**/\r
297EFI_STATUS\r
298EFIAPI\r
299SmmBaseSmmAllocatePool (\r
300 IN EFI_SMM_BASE_PROTOCOL *This,\r
301 IN EFI_MEMORY_TYPE PoolType,\r
302 IN UINTN Size,\r
303 OUT VOID **Buffer\r
304 )\r
305{\r
306 gCommunicationData.FunctionData.Function = SMMBASE_ALLOCATE_POOL;\r
307 gCommunicationData.FunctionData.Args.AllocatePool.PoolType = PoolType;\r
308 gCommunicationData.FunctionData.Args.AllocatePool.Size = Size;\r
309 gCommunicationData.FunctionData.Args.AllocatePool.Buffer = Buffer;\r
310\r
311 SmmBaseHelperService ();\r
312 return gCommunicationData.FunctionData.Status;\r
313}\r
314\r
315/**\r
316 The SmmFreePool() function returns the memory specified by Buffer to the system.\r
317 On return, the memory's type is EFI SMRAM Memory. The Buffer that is freed must\r
318 have been allocated by SmmAllocatePool().\r
319\r
320 @param[in] This Protocol instance pointer.\r
321 @param[in] Buffer Pointer to the buffer allocation.\r
322\r
323 @retval EFI_SUCCESS The memory was returned to the system.\r
324 @retval EFI_INVALID_PARAMETER Buffer was invalid.\r
325 @retval EFI_UNSUPPORTED Platform is in runtime.\r
326**/\r
327EFI_STATUS\r
328EFIAPI\r
329SmmBaseSmmFreePool (\r
330 IN EFI_SMM_BASE_PROTOCOL *This,\r
331 IN VOID *Buffer\r
332 )\r
333{\r
334 gCommunicationData.FunctionData.Function = SMMBASE_FREE_POOL;\r
335 gCommunicationData.FunctionData.Args.FreePool.Buffer = Buffer;\r
336\r
337 SmmBaseHelperService ();\r
338 return gCommunicationData.FunctionData.Status;\r
339}\r
340\r
341/**\r
342 The GetSmstLocation() function returns the location of the System Management\r
343 Service Table. The use of the API is such that a driver can discover the\r
344 location of the SMST in its entry point and then cache it in some driver\r
345 global variable so that the SMST can be invoked in subsequent callbacks.\r
346\r
347 @param[in] This Protocol instance pointer.\r
348 @param[in] Smst Pointer to the SMST.\r
349\r
350 @retval EFI_SUCCESS The operation was successful\r
351 @retval EFI_INVALID_PARAMETER Smst was invalid.\r
352 @retval EFI_UNSUPPORTED Not in SMM.\r
353**/\r
354EFI_STATUS\r
355EFIAPI\r
356SmmBaseGetSmstLocation (\r
357 IN EFI_SMM_BASE_PROTOCOL *This,\r
358 OUT EFI_SMM_SYSTEM_TABLE **Smst\r
359 )\r
360{\r
361 if (mSmmBaseHelperReady == NULL) {\r
362 ASSERT (FALSE);\r
363 return EFI_UNSUPPORTED;\r
364 }\r
365\r
366 if (!IsInSmm ()) {\r
367 return EFI_UNSUPPORTED;\r
368 }\r
369\r
370 if (Smst == NULL) {\r
371 return EFI_INVALID_PARAMETER;\r
372 }\r
373\r
374 *Smst = mSmmBaseHelperReady->FrameworkSmst;\r
375 return EFI_SUCCESS;\r
376}\r
377\r
378/**\r
379 SMM Base Protocol notification event handler.\r
380\r
381 @param[in] Event Event whose notification function is being invoked.\r
382 @param[in] Context Pointer to the notification function's context.\r
383**/\r
384VOID\r
385EFIAPI\r
386SmmBaseProtocolNotification (\r
387 IN EFI_EVENT Event,\r
388 IN VOID *Context\r
389 )\r
390{\r
391 EFI_STATUS Status;\r
392\r
393 ///\r
394 /// Assume only one instance of SMM Base2 Protocol in the system\r
395 /// Locate SMM Base2 Protocol\r
396 ///\r
397 Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID **) &mSmmBase2);\r
398 if (!EFI_ERROR (Status)) {\r
399 ///\r
400 /// Publish Framework SMM BASE Protocol immediately after SMM Base2 Protocol is installed to\r
401 /// make SMM Base Protocol.InSmm() available as early as possible.\r
402 ///\r
403 Status = gBS->InstallProtocolInterface (\r
404 &mImageHandle,\r
405 &gEfiSmmBaseProtocolGuid,\r
406 EFI_NATIVE_INTERFACE,\r
407 &gSmmBase\r
408 );\r
409 ASSERT_EFI_ERROR (Status);\r
410 }\r
411}\r
412\r
413/**\r
414 SMM Communication Protocol notification event handler.\r
415\r
416 @param[in] Event Event whose notification function is being invoked.\r
417 @param[in] Context Pointer to the notification function's context.\r
418**/\r
419VOID\r
420EFIAPI\r
421SmmCommunicationProtocolNotification (\r
422 IN EFI_EVENT Event,\r
423 IN VOID *Context\r
424 )\r
425{\r
426 ///\r
427 /// Assume only one instance of SMM Communication Protocol in the system\r
428 /// Locate SMM Communication Protocol\r
429 ///\r
430 gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
431}\r
432\r
433/**\r
434 SMM Base Helper Ready Protocol notification event handler.\r
435\r
436 @param[in] Event Event whose notification function is being invoked.\r
437 @param[in] Context Pointer to the notification function's context.\r
438**/\r
439VOID\r
440EFIAPI\r
441SmmBaseHelperReadyProtocolNotification (\r
442 IN EFI_EVENT Event,\r
443 IN VOID *Context\r
444 )\r
445{\r
446 ///\r
447 /// Assume only one instance of SMM Base Helper Ready Protocol in the system\r
448 /// Locate SMM Base Helper Ready Protocol\r
449 ///\r
450 gBS->LocateProtocol (&gEfiSmmBaseHelperReadyProtocolGuid, NULL, (VOID **) &mSmmBaseHelperReady);\r
451}\r
452\r
453/**\r
454 Entry Point for SMM Base Protocol on SMM Base2 Protocol Thunk driver.\r
455\r
456 @param[in] ImageHandle Image handle of this driver.\r
457 @param[in] SystemTable A Pointer to the EFI System Table.\r
458\r
459 @retval EFI_SUCCESS The entry point is executed successfully.\r
460**/\r
461EFI_STATUS\r
462EFIAPI\r
463SmmBaseThunkMain (\r
464 IN EFI_HANDLE ImageHandle,\r
465 IN EFI_SYSTEM_TABLE *SystemTable\r
466 )\r
467{\r
468 VOID *Registration;\r
469\r
470 mImageHandle = ImageHandle;\r
471\r
472 ///\r
473 /// Install notifications for required protocols\r
474 ///\r
475 /// Note we use protocol notifications here so as that this thunk driver can be\r
476 /// loaded before PI SMM IPL driver. Framework SMM BASE Protocol will be published \r
477 /// immediately after SMM Base2 Protocol is installed to make SMM Base Protocol.InSmm()\r
478 /// available as early as possible because some Framework code's behavior depends on\r
479 /// correct detection of SMM mode via SMM Base Protocol.InSmm().\r
480 ///\r
481 /// Also SMM Base Helper driver is expected to be dispatched\r
482 /// in the earliest round of SMM driver dispatch just after SMM IPL driver loads SMM Foundation.\r
483 /// So the full functionality of SMM Base Protocol is ready immediately after SMM IPL driver is\r
484 /// loaded. Since that point Framework SMM driver can be succesufully supported.\r
485 ///\r
486 EfiCreateProtocolNotifyEvent (\r
487 &gEfiSmmBase2ProtocolGuid,\r
488 TPL_CALLBACK,\r
489 SmmBaseProtocolNotification,\r
490 NULL,\r
491 &Registration\r
492 );\r
493\r
494 EfiCreateProtocolNotifyEvent (\r
495 &gEfiSmmCommunicationProtocolGuid,\r
496 TPL_CALLBACK,\r
497 SmmCommunicationProtocolNotification,\r
498 NULL,\r
499 &Registration\r
500 );\r
501\r
502 EfiCreateProtocolNotifyEvent (\r
503 &gEfiSmmBaseHelperReadyProtocolGuid,\r
504 TPL_CALLBACK,\r
505 SmmBaseHelperReadyProtocolNotification,\r
506 NULL,\r
507 &Registration\r
508 );\r
509\r
510 return EFI_SUCCESS;\r
511}\r
512\r