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