]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Compatibility/SmmBaseOnSmmBase2Thunk/SmmBaseOnSmmBase2Thunk.c
Update code to match EDKII coding style.
[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
fb03ca1a 11 Copyright (c) 2009 - 2010, Intel Corporation\r
9e620719 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
f5501a65 22#include <PiDxe.h>\r
23#include <FrameworkSmm.h>\r
9e620719 24\r
f5501a65 25#include <Protocol/SmmBase2.h>\r
26#include <Protocol/SmmCommunication.h>\r
27#include <Protocol/SmmBaseHelperReady.h>\r
28\r
29#include <Guid/SmmBaseThunkCommunication.h>\r
30#include <Guid/EventGroup.h>\r
31\r
32#include <Library/DebugLib.h>\r
33#include <Library/UefiBootServicesTableLib.h>\r
34#include <Library/UefiDriverEntryPoint.h>\r
35#include <Library/UefiLib.h>\r
36#include <Library/UefiRuntimeLib.h>\r
37\r
f5501a65 38SMMBASETHUNK_COMMUNICATION_DATA mCommunicationData = {\r
9e620719 39 EFI_SMM_BASE_THUNK_COMMUNICATION_GUID,\r
40 sizeof (SMMBASE_FUNCTION_DATA)\r
41};\r
42\r
f5501a65 43EFI_HANDLE mSmmBaseHandle = NULL;\r
9e620719 44EFI_SMM_BASE2_PROTOCOL *mSmmBase2 = NULL;\r
45EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;\r
46EFI_SMM_BASE_HELPER_READY_PROTOCOL *mSmmBaseHelperReady = NULL;\r
47\r
17d2c9a3 48/**\r
49 Determine if in SMM mode.\r
50\r
51 @retval TRUE In SMM mode.\r
52 @retval FALSE Not in SMM mode.\r
53**/\r
9e620719 54BOOLEAN\r
55IsInSmm (\r
56 VOID\r
57 )\r
58{\r
59 EFI_STATUS Status;\r
60 BOOLEAN InSmm;\r
61 \r
62 Status = mSmmBase2->InSmm (mSmmBase2, &InSmm);\r
63 ASSERT_EFI_ERROR (Status);\r
64 return InSmm;\r
65}\r
66\r
67/**\r
68 Invoke services provided by SMM Base Helper SMM driver.\r
69**/\r
70VOID\r
71SmmBaseHelperService (\r
72 VOID\r
73 )\r
74{\r
75 UINTN DataSize;\r
76\r
f5501a65 77 mCommunicationData.FunctionData.Status = EFI_UNSUPPORTED;\r
9e620719 78\r
26a76fbc 79 if ((mCommunicationData.FunctionData.Function != SmmBaseFunctionCommunicate) && IsInSmm()) {\r
9e620719 80 ///\r
81 /// If in SMM mode, directly call services in SMM Base Helper.\r
82 ///\r
9e620719 83 DataSize = (UINTN)(sizeof (SMMBASE_FUNCTION_DATA));\r
84 mSmmBaseHelperReady->ServiceEntry (\r
85 NULL,\r
86 NULL,\r
f5501a65 87 &mCommunicationData.FunctionData,\r
9e620719 88 &DataSize\r
89 );\r
90 } else {\r
91 ///\r
bade9bf5 92 /// Call services in SMM Base Helper via SMM Communication Protocol.\r
9e620719 93 ///\r
f5501a65 94 DataSize = (UINTN)(sizeof (mCommunicationData));\r
9e620719 95 mSmmCommunication->Communicate (\r
96 mSmmCommunication,\r
f5501a65 97 &mCommunicationData,\r
9e620719 98 &DataSize\r
99 );\r
100 }\r
101}\r
102\r
103/**\r
104 Register a given driver into SMRAM. This is the equivalent of performing\r
105 the LoadImage/StartImage into System Management Mode.\r
106\r
107 @param[in] This Protocol instance pointer.\r
108 @param[in] FilePath Location of the image to be installed as the handler.\r
109 @param[in] SourceBuffer Optional source buffer in case the image file\r
110 is in memory.\r
111 @param[in] SourceSize Size of the source image file, if in memory.\r
112 @param[out] ImageHandle The handle that the base driver uses to decode \r
113 the handler. Unique among SMM handlers only, \r
114 not unique across DXE/EFI.\r
115 @param[in] LegacyIA32Binary An optional parameter specifying that the associated \r
116 file is a real-mode IA-32 binary.\r
117\r
118 @retval EFI_SUCCESS The operation was successful.\r
119 @retval EFI_OUT_OF_RESOURCES There were no additional SMRAM resources to load the handler\r
120 @retval EFI_UNSUPPORTED This platform does not support 16-bit handlers.\r
121 @retval EFI_UNSUPPORTED Platform is in runtime.\r
122 @retval EFI_INVALID_PARAMETER The handlers was not the correct image type\r
123**/\r
124EFI_STATUS\r
125EFIAPI\r
126SmmBaseRegister (\r
127 IN EFI_SMM_BASE_PROTOCOL *This,\r
128 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
129 IN VOID *SourceBuffer,\r
130 IN UINTN SourceSize,\r
131 OUT EFI_HANDLE *ImageHandle,\r
132 IN BOOLEAN LegacyIA32Binary\r
133 )\r
134{\r
135 if (LegacyIA32Binary) {\r
136 return EFI_UNSUPPORTED;\r
137 }\r
138\r
26a76fbc 139 mCommunicationData.FunctionData.Function = SmmBaseFunctionRegister;\r
f5501a65 140 mCommunicationData.FunctionData.Args.Register.FilePath = FilePath;\r
141 mCommunicationData.FunctionData.Args.Register.SourceBuffer = SourceBuffer;\r
142 mCommunicationData.FunctionData.Args.Register.SourceSize = SourceSize;\r
143 mCommunicationData.FunctionData.Args.Register.ImageHandle = ImageHandle;\r
144 mCommunicationData.FunctionData.Args.Register.LegacyIA32Binary = LegacyIA32Binary;\r
9e620719 145\r
146 SmmBaseHelperService ();\r
f5501a65 147 return mCommunicationData.FunctionData.Status;\r
9e620719 148}\r
149\r
150/**\r
151 Removes a handler from execution within SMRAM. This is the equivalent of performing\r
152 the UnloadImage in System Management Mode.\r
153\r
154 @param[in] This Protocol instance pointer.\r
155 @param[in] ImageHandle The handler to be removed.\r
156\r
157 @retval EFI_SUCCESS The operation was successful\r
158 @retval EFI_INVALID_PARAMETER The handler did not exist\r
159 @retval EFI_UNSUPPORTED Platform is in runtime.\r
160**/\r
161EFI_STATUS\r
162EFIAPI\r
163SmmBaseUnregister (\r
164 IN EFI_SMM_BASE_PROTOCOL *This,\r
165 IN EFI_HANDLE ImageHandle\r
166 )\r
167{\r
26a76fbc 168 mCommunicationData.FunctionData.Function = SmmBaseFunctionUnregister;\r
f5501a65 169 mCommunicationData.FunctionData.Args.UnRegister.ImageHandle = ImageHandle;\r
9e620719 170\r
171 SmmBaseHelperService ();\r
f5501a65 172 return mCommunicationData.FunctionData.Status;\r
9e620719 173}\r
174\r
175/**\r
176 The SMM Inter-module Communicate Service Communicate() function\r
177 provides a service to send/receive messages from a registered\r
178 EFI service. The BASE protocol driver is responsible for doing\r
179 any of the copies such that the data lives in boot-service-accessible RAM.\r
180\r
181 @param[in] This Protocol instance pointer.\r
182 @param[in] ImageHandle The handle of the registered driver.\r
26a76fbc
LG
183 @param[in, out] CommunicationBuffer Pointer to the buffer to convey into SMRAM.\r
184 @param[in, out] BufferSize The size of the data buffer being passed in.\r
9e620719 185 On exit, the size of data being returned.\r
186 Zero if the handler does not wish to reply with any data.\r
187\r
188 @retval EFI_SUCCESS The message was successfully posted\r
189 @retval EFI_INVALID_PARAMETER The buffer was NULL\r
190**/\r
191EFI_STATUS\r
192EFIAPI\r
193SmmBaseCommunicate (\r
194 IN EFI_SMM_BASE_PROTOCOL *This,\r
195 IN EFI_HANDLE ImageHandle,\r
196 IN OUT VOID *CommunicationBuffer,\r
197 IN OUT UINTN *BufferSize\r
198 )\r
199{\r
bade9bf5 200 ///\r
201 /// Note this is a runtime interface\r
202 ///\r
203\r
26a76fbc 204 mCommunicationData.FunctionData.Function = SmmBaseFunctionCommunicate;\r
bade9bf5 205 mCommunicationData.FunctionData.Args.Communicate.ImageHandle = ImageHandle;\r
206 mCommunicationData.FunctionData.Args.Communicate.CommunicationBuffer = CommunicationBuffer;\r
207 mCommunicationData.FunctionData.Args.Communicate.SourceSize = BufferSize;\r
9e620719 208\r
bade9bf5 209 SmmBaseHelperService ();\r
210 return mCommunicationData.FunctionData.Status;\r
9e620719 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
26a76fbc 246 mCommunicationData.FunctionData.Function = SmmBaseFunctionRegisterCallback;\r
f5501a65 247 mCommunicationData.FunctionData.Args.RegisterCallback.SmmImageHandle = SmmImageHandle;\r
248 mCommunicationData.FunctionData.Args.RegisterCallback.CallbackAddress = CallbackAddress;\r
249 mCommunicationData.FunctionData.Args.RegisterCallback.MakeLast = MakeLast;\r
250 mCommunicationData.FunctionData.Args.RegisterCallback.FloatingPointSave = FloatingPointSave;\r
9e620719 251\r
252 SmmBaseHelperService();\r
f5501a65 253 return mCommunicationData.FunctionData.Status;\r
9e620719 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
26a76fbc 306 mCommunicationData.FunctionData.Function = SmmBaseFunctionAllocatePool;\r
f5501a65 307 mCommunicationData.FunctionData.Args.AllocatePool.PoolType = PoolType;\r
308 mCommunicationData.FunctionData.Args.AllocatePool.Size = Size;\r
309 mCommunicationData.FunctionData.Args.AllocatePool.Buffer = Buffer;\r
9e620719 310\r
311 SmmBaseHelperService ();\r
f5501a65 312 return mCommunicationData.FunctionData.Status;\r
9e620719 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
26a76fbc 334 mCommunicationData.FunctionData.Function = SmmBaseFunctionFreePool;\r
f5501a65 335 mCommunicationData.FunctionData.Args.FreePool.Buffer = Buffer;\r
9e620719 336\r
337 SmmBaseHelperService ();\r
f5501a65 338 return mCommunicationData.FunctionData.Status;\r
9e620719 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
26a76fbc 348 @param[out] Smst Pointer to the SMST.\r
9e620719 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
9e620719 361 if (!IsInSmm ()) {\r
362 return EFI_UNSUPPORTED;\r
363 }\r
364\r
365 if (Smst == NULL) {\r
366 return EFI_INVALID_PARAMETER;\r
367 }\r
368\r
369 *Smst = mSmmBaseHelperReady->FrameworkSmst;\r
370 return EFI_SUCCESS;\r
371}\r
372\r
fb03ca1a 373/**\r
374 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE\r
375\r
376 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
377 It convers pointer to new virtual address.\r
378\r
379 @param Event Event whose notification function is being invoked\r
380 @param Context Pointer to the notification function's context\r
381**/\r
382VOID\r
383EFIAPI\r
384SmmBaseAddressChangeEvent (\r
385 IN EFI_EVENT Event,\r
386 IN VOID *Context\r
387 )\r
388{\r
bade9bf5 389 EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);\r
fb03ca1a 390}\r
391\r
26a76fbc
LG
392///\r
393/// SMM Base Protocol instance\r
394///\r
395EFI_SMM_BASE_PROTOCOL mSmmBase = {\r
396 SmmBaseRegister,\r
397 SmmBaseUnregister,\r
398 SmmBaseCommunicate,\r
399 SmmBaseRegisterCallback,\r
400 SmmBaseInSmm,\r
401 SmmBaseSmmAllocatePool,\r
402 SmmBaseSmmFreePool,\r
403 SmmBaseGetSmstLocation\r
404};\r
405\r
9e620719 406/**\r
407 Entry Point for SMM Base Protocol on SMM Base2 Protocol Thunk driver.\r
408\r
409 @param[in] ImageHandle Image handle of this driver.\r
410 @param[in] SystemTable A Pointer to the EFI System Table.\r
411\r
412 @retval EFI_SUCCESS The entry point is executed successfully.\r
413**/\r
414EFI_STATUS\r
415EFIAPI\r
416SmmBaseThunkMain (\r
417 IN EFI_HANDLE ImageHandle,\r
418 IN EFI_SYSTEM_TABLE *SystemTable\r
419 )\r
420{\r
f5501a65 421 EFI_STATUS Status;\r
422 EFI_EVENT Event;\r
9e620719 423\r
f5501a65 424 //\r
425 // Assume only one instance of SMM Base2 Protocol in the system\r
426 // Locate SMM Base2 Protocol\r
427 //\r
428 Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID **) &mSmmBase2);\r
429 ASSERT_EFI_ERROR (Status);\r
430\r
431 //\r
432 // Assume only one instance of SMM Communication Protocol in the system\r
433 // Locate SMM Communication Protocol\r
434 //\r
435 gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
436 ASSERT_EFI_ERROR (Status);\r
437\r
438 //\r
439 // Assume only one instance of SMM Base Helper Ready Protocol in the system\r
440 // Locate SMM Base Helper Ready Protocol\r
441 //\r
442 Status = gBS->LocateProtocol (&gEfiSmmBaseHelperReadyProtocolGuid, NULL, (VOID **) &mSmmBaseHelperReady);\r
443 ASSERT_EFI_ERROR (Status);\r
9e620719 444\r
f5501a65 445 //\r
446 // Create event on SetVirtualAddressMap() to convert mSmmCommunication from a physical address to a virtual address\r
447 //\r
fb03ca1a 448 Status = gBS->CreateEventEx (\r
449 EVT_NOTIFY_SIGNAL,\r
450 TPL_NOTIFY,\r
451 SmmBaseAddressChangeEvent,\r
452 NULL,\r
453 &gEfiEventVirtualAddressChangeGuid,\r
f5501a65 454 &Event\r
fb03ca1a 455 );\r
456 ASSERT_EFI_ERROR (Status);\r
f5501a65 457 \r
458 //\r
459 // Publish Framework SMM BASE Protocol immediately after SMM Base2 Protocol is installed to\r
460 // make SMM Base Protocol.InSmm() available as early as possible.\r
461 //\r
462 Status = gBS->InstallMultipleProtocolInterfaces (\r
463 &mSmmBaseHandle,\r
464 &gEfiSmmBaseProtocolGuid, &mSmmBase,\r
465 NULL\r
466 );\r
467 ASSERT_EFI_ERROR (Status);\r
468 \r
9e620719 469 return EFI_SUCCESS;\r
470}\r