]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/SmmBaseOnSmmBase2Thunk/SmmBaseOnSmmBase2Thunk.c
fe6024af529aff26e4cfe3b475356f0017396342
[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 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 "SmmBaseOnSmmBase2Thunk.h"
23
24 EFI_SMM_BASE_PROTOCOL gSmmBase = {
25 SmmBaseRegister,
26 SmmBaseUnregister,
27 SmmBaseCommunicate,
28 SmmBaseRegisterCallback,
29 SmmBaseInSmm,
30 SmmBaseSmmAllocatePool,
31 SmmBaseSmmFreePool,
32 SmmBaseGetSmstLocation
33 };
34
35 SMMBASETHUNK_COMMUNICATION_DATA gCommunicationData = {
36 EFI_SMM_BASE_THUNK_COMMUNICATION_GUID,
37 sizeof (SMMBASE_FUNCTION_DATA)
38 };
39
40 EFI_HANDLE mImageHandle;
41 EFI_SMM_BASE2_PROTOCOL *mSmmBase2 = NULL;
42 EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;
43 EFI_SMM_BASE_HELPER_READY_PROTOCOL *mSmmBaseHelperReady = NULL;
44
45 BOOLEAN
46 IsInSmm (
47 VOID
48 )
49 {
50 EFI_STATUS Status;
51 BOOLEAN InSmm;
52
53 Status = mSmmBase2->InSmm (mSmmBase2, &InSmm);
54 ASSERT_EFI_ERROR (Status);
55 return InSmm;
56 }
57
58 /**
59 Invoke services provided by SMM Base Helper SMM driver.
60 **/
61 VOID
62 SmmBaseHelperService (
63 VOID
64 )
65 {
66 UINTN DataSize;
67
68 gCommunicationData.FunctionData.Status = EFI_UNSUPPORTED;
69
70 if (IsInSmm()) {
71 ///
72 /// If in SMM mode, directly call services in SMM Base Helper.
73 ///
74 if (mSmmBaseHelperReady == NULL) {
75 ASSERT (FALSE);
76 return;
77 }
78
79 DataSize = (UINTN)(sizeof (SMMBASE_FUNCTION_DATA));
80 mSmmBaseHelperReady->ServiceEntry (
81 NULL,
82 NULL,
83 &gCommunicationData.FunctionData,
84 &DataSize
85 );
86 } else {
87 ///
88 /// If in non-SMM mode, call services in SMM Base Helper via SMM Communication Protocol.
89 ///
90 if (mSmmCommunication == NULL) {
91 ASSERT (FALSE);
92 return;
93 }
94
95 DataSize = (UINTN)(sizeof (gCommunicationData));
96 mSmmCommunication->Communicate (
97 mSmmCommunication,
98 &gCommunicationData,
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 gCommunicationData.FunctionData.Function = SMMBASE_REGISTER;
141 gCommunicationData.FunctionData.Args.Register.FilePath = FilePath;
142 gCommunicationData.FunctionData.Args.Register.SourceBuffer = SourceBuffer;
143 gCommunicationData.FunctionData.Args.Register.SourceSize = SourceSize;
144 gCommunicationData.FunctionData.Args.Register.ImageHandle = ImageHandle;
145 gCommunicationData.FunctionData.Args.Register.LegacyIA32Binary = LegacyIA32Binary;
146
147 SmmBaseHelperService ();
148 return gCommunicationData.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 gCommunicationData.FunctionData.Function = SMMBASE_UNREGISTER;
170 gCommunicationData.FunctionData.Args.UnRegister.ImageHandle = ImageHandle;
171
172 SmmBaseHelperService ();
173 return gCommunicationData.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 if (mSmmCommunication == NULL) {
202 ASSERT (FALSE);
203 return EFI_UNSUPPORTED;
204 }
205
206 return mSmmCommunication->Communicate (
207 mSmmCommunication,
208 CommunicationBuffer,
209 BufferSize
210 );
211 }
212
213 /**
214 Register a callback to execute within SMM.
215 This allows receipt of messages created with EFI_SMM_BASE_PROTOCOL.Communicate().
216
217 @param[in] This Protocol instance pointer.
218 @param[in] SmmImageHandle Handle of the callback service.
219 @param[in] CallbackAddress Address of the callback service.
220 @param[in] MakeLast If present, will stipulate that the handler is posted to
221 be executed last in the dispatch table.
222 @param[in] FloatingPointSave An optional parameter that informs the
223 EFI_SMM_ACCESS_PROTOCOL Driver core if it needs to save
224 the floating point register state. If any handler
225 require this, the state will be saved for all handlers.
226
227 @retval EFI_SUCCESS The operation was successful
228 @retval EFI_OUT_OF_RESOURCES Not enough space in the dispatch queue
229 @retval EFI_UNSUPPORTED Platform is in runtime.
230 @retval EFI_UNSUPPORTED The caller is not in SMM.
231 **/
232 EFI_STATUS
233 EFIAPI
234 SmmBaseRegisterCallback (
235 IN EFI_SMM_BASE_PROTOCOL *This,
236 IN EFI_HANDLE SmmImageHandle,
237 IN EFI_SMM_CALLBACK_ENTRY_POINT CallbackAddress,
238 IN BOOLEAN MakeLast,
239 IN BOOLEAN FloatingPointSave
240 )
241 {
242 if (!IsInSmm()) {
243 return EFI_UNSUPPORTED;
244 }
245
246 gCommunicationData.FunctionData.Function = SMMBASE_REGISTER_CALLBACK;
247 gCommunicationData.FunctionData.Args.RegisterCallback.SmmImageHandle = SmmImageHandle;
248 gCommunicationData.FunctionData.Args.RegisterCallback.CallbackAddress = CallbackAddress;
249 gCommunicationData.FunctionData.Args.RegisterCallback.MakeLast = MakeLast;
250 gCommunicationData.FunctionData.Args.RegisterCallback.FloatingPointSave = FloatingPointSave;
251
252 SmmBaseHelperService();
253 return gCommunicationData.FunctionData.Status;
254 }
255
256 /**
257 This routine tells caller if execution context is SMM or not.
258
259 @param[in] This Protocol instance pointer.
260 @param[out] InSmm Whether the caller is inside SMM for IA-32
261 or servicing a PMI for the Itanium processor
262 family.
263
264 @retval EFI_SUCCESS The operation was successful
265 @retval EFI_INVALID_PARAMETER InSmm was NULL.
266 **/
267 EFI_STATUS
268 EFIAPI
269 SmmBaseInSmm (
270 IN EFI_SMM_BASE_PROTOCOL *This,
271 OUT BOOLEAN *InSmm
272 )
273 {
274 return mSmmBase2->InSmm (mSmmBase2, InSmm);
275 }
276
277 /**
278 The SmmAllocatePool() function allocates a memory region of Size bytes from memory of
279 type PoolType and returns the address of the allocated memory in the location referenced
280 by Buffer. This function allocates pages from EFI SMRAM Memory as needed to grow the
281 requested pool type. All allocations are eight-byte aligned.
282
283 @param[in] This Protocol instance pointer.
284 @param[in] PoolType The type of pool to allocate.
285 The only supported type is EfiRuntimeServicesData;
286 the interface will internally map this runtime request to
287 SMRAM for IA-32 and leave as this type for the Itanium
288 processor family. Other types can be ignored.
289 @param[in] Size The number of bytes to allocate from the pool.
290 @param[out] Buffer A pointer to a pointer to the allocated buffer if the call
291 succeeds; undefined otherwise.
292
293 @retval EFI_SUCCESS The requested number of bytes was allocated.
294 @retval EFI_OUT_OF_RESOURCES The pool requested could not be allocated.
295 @retval EFI_UNSUPPORTED Platform is in runtime.
296 **/
297 EFI_STATUS
298 EFIAPI
299 SmmBaseSmmAllocatePool (
300 IN EFI_SMM_BASE_PROTOCOL *This,
301 IN EFI_MEMORY_TYPE PoolType,
302 IN UINTN Size,
303 OUT VOID **Buffer
304 )
305 {
306 gCommunicationData.FunctionData.Function = SMMBASE_ALLOCATE_POOL;
307 gCommunicationData.FunctionData.Args.AllocatePool.PoolType = PoolType;
308 gCommunicationData.FunctionData.Args.AllocatePool.Size = Size;
309 gCommunicationData.FunctionData.Args.AllocatePool.Buffer = Buffer;
310
311 SmmBaseHelperService ();
312 return gCommunicationData.FunctionData.Status;
313 }
314
315 /**
316 The SmmFreePool() function returns the memory specified by Buffer to the system.
317 On return, the memory's type is EFI SMRAM Memory. The Buffer that is freed must
318 have been allocated by SmmAllocatePool().
319
320 @param[in] This Protocol instance pointer.
321 @param[in] Buffer Pointer to the buffer allocation.
322
323 @retval EFI_SUCCESS The memory was returned to the system.
324 @retval EFI_INVALID_PARAMETER Buffer was invalid.
325 @retval EFI_UNSUPPORTED Platform is in runtime.
326 **/
327 EFI_STATUS
328 EFIAPI
329 SmmBaseSmmFreePool (
330 IN EFI_SMM_BASE_PROTOCOL *This,
331 IN VOID *Buffer
332 )
333 {
334 gCommunicationData.FunctionData.Function = SMMBASE_FREE_POOL;
335 gCommunicationData.FunctionData.Args.FreePool.Buffer = Buffer;
336
337 SmmBaseHelperService ();
338 return gCommunicationData.FunctionData.Status;
339 }
340
341 /**
342 The GetSmstLocation() function returns the location of the System Management
343 Service Table. The use of the API is such that a driver can discover the
344 location of the SMST in its entry point and then cache it in some driver
345 global variable so that the SMST can be invoked in subsequent callbacks.
346
347 @param[in] This Protocol instance pointer.
348 @param[in] Smst Pointer to the SMST.
349
350 @retval EFI_SUCCESS The operation was successful
351 @retval EFI_INVALID_PARAMETER Smst was invalid.
352 @retval EFI_UNSUPPORTED Not in SMM.
353 **/
354 EFI_STATUS
355 EFIAPI
356 SmmBaseGetSmstLocation (
357 IN EFI_SMM_BASE_PROTOCOL *This,
358 OUT EFI_SMM_SYSTEM_TABLE **Smst
359 )
360 {
361 if (mSmmBaseHelperReady == NULL) {
362 ASSERT (FALSE);
363 return EFI_UNSUPPORTED;
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 SMM Base Protocol notification event handler.
380
381 @param[in] Event Event whose notification function is being invoked.
382 @param[in] Context Pointer to the notification function's context.
383 **/
384 VOID
385 EFIAPI
386 SmmBaseProtocolNotification (
387 IN EFI_EVENT Event,
388 IN VOID *Context
389 )
390 {
391 EFI_STATUS Status;
392
393 ///
394 /// Assume only one instance of SMM Base2 Protocol in the system
395 /// Locate SMM Base2 Protocol
396 ///
397 Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID **) &mSmmBase2);
398 if (!EFI_ERROR (Status)) {
399 ///
400 /// Publish Framework SMM BASE Protocol immediately after SMM Base2 Protocol is installed to
401 /// make SMM Base Protocol.InSmm() available as early as possible.
402 ///
403 Status = gBS->InstallProtocolInterface (
404 &mImageHandle,
405 &gEfiSmmBaseProtocolGuid,
406 EFI_NATIVE_INTERFACE,
407 &gSmmBase
408 );
409 ASSERT_EFI_ERROR (Status);
410 }
411 }
412
413 /**
414 SMM Communication Protocol notification event handler.
415
416 @param[in] Event Event whose notification function is being invoked.
417 @param[in] Context Pointer to the notification function's context.
418 **/
419 VOID
420 EFIAPI
421 SmmCommunicationProtocolNotification (
422 IN EFI_EVENT Event,
423 IN VOID *Context
424 )
425 {
426 ///
427 /// Assume only one instance of SMM Communication Protocol in the system
428 /// Locate SMM Communication Protocol
429 ///
430 gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);
431 }
432
433 /**
434 SMM Base Helper Ready Protocol notification event handler.
435
436 @param[in] Event Event whose notification function is being invoked.
437 @param[in] Context Pointer to the notification function's context.
438 **/
439 VOID
440 EFIAPI
441 SmmBaseHelperReadyProtocolNotification (
442 IN EFI_EVENT Event,
443 IN VOID *Context
444 )
445 {
446 ///
447 /// Assume only one instance of SMM Base Helper Ready Protocol in the system
448 /// Locate SMM Base Helper Ready Protocol
449 ///
450 gBS->LocateProtocol (&gEfiSmmBaseHelperReadyProtocolGuid, NULL, (VOID **) &mSmmBaseHelperReady);
451 }
452
453 /**
454 Entry Point for SMM Base Protocol on SMM Base2 Protocol Thunk driver.
455
456 @param[in] ImageHandle Image handle of this driver.
457 @param[in] SystemTable A Pointer to the EFI System Table.
458
459 @retval EFI_SUCCESS The entry point is executed successfully.
460 **/
461 EFI_STATUS
462 EFIAPI
463 SmmBaseThunkMain (
464 IN EFI_HANDLE ImageHandle,
465 IN EFI_SYSTEM_TABLE *SystemTable
466 )
467 {
468 VOID *Registration;
469
470 mImageHandle = ImageHandle;
471
472 ///
473 /// Install notifications for required protocols
474 ///
475 /// Note we use protocol notifications here so as that this thunk driver can be
476 /// loaded before PI SMM IPL driver. Framework SMM BASE Protocol will be published
477 /// immediately after SMM Base2 Protocol is installed to make SMM Base Protocol.InSmm()
478 /// available as early as possible because some Framework code's behavior depends on
479 /// correct detection of SMM mode via SMM Base Protocol.InSmm().
480 ///
481 /// Also SMM Base Helper driver is expected to be dispatched
482 /// in the earliest round of SMM driver dispatch just after SMM IPL driver loads SMM Foundation.
483 /// So the full functionality of SMM Base Protocol is ready immediately after SMM IPL driver is
484 /// loaded. Since that point Framework SMM driver can be succesufully supported.
485 ///
486 EfiCreateProtocolNotifyEvent (
487 &gEfiSmmBase2ProtocolGuid,
488 TPL_CALLBACK,
489 SmmBaseProtocolNotification,
490 NULL,
491 &Registration
492 );
493
494 EfiCreateProtocolNotifyEvent (
495 &gEfiSmmCommunicationProtocolGuid,
496 TPL_CALLBACK,
497 SmmCommunicationProtocolNotification,
498 NULL,
499 &Registration
500 );
501
502 EfiCreateProtocolNotifyEvent (
503 &gEfiSmmBaseHelperReadyProtocolGuid,
504 TPL_CALLBACK,
505 SmmBaseHelperReadyProtocolNotification,
506 NULL,
507 &Registration
508 );
509
510 return EFI_SUCCESS;
511 }
512