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