]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/PiSmmStatusCodeOnFrameworkSmmStatusCodeThunk/PiSmmStatusCodeOnFrameworkSmmStatusCodeThunk.c
92eed5a0d02b02880d891055c41c5ad82c512e60
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / PiSmmStatusCodeOnFrameworkSmmStatusCodeThunk / PiSmmStatusCodeOnFrameworkSmmStatusCodeThunk.c
1 /** @file
2 PI SMM Status Code Protocol on Framework SMM Status Code Protocol Thunk.
3
4 This thunk driver produces PI SMM Status Code Protocol and SMM Report Status Code Handler Protocol.
5 And it registers a status code handler within itself to route status codes into Framework SMM Status
6 Code Protocol.
7
8 Note that Framework SMM Status Code Protocol and PI SMM Status Code Protocol have identical protocol
9 GUID and interface structure, but they are in different handle databases.
10
11 Copyright (c) 2010, Intel Corporation. All rights reserved<BR>
12 This program and the accompanying materials are licensed and made available under
13 the terms and conditions of the BSD License that accompanies this distribution.
14 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 "PiSmmStatusCodeOnFrameworkSmmStatusCodeThunk.h"
23
24 LIST_ENTRY mCallbackListHead = INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead);
25
26 //
27 // Report operation nest status.
28 // If it is set, then the report operation has nested.
29 //
30 UINT32 mStatusCodeNestStatus = 0;
31
32 EFI_SMM_STATUS_CODE_PROTOCOL mSmmStatusCodeProtocol = {
33 ReportDispatcher
34 };
35
36 EFI_SMM_RSC_HANDLER_PROTOCOL mSmmRscHandlerProtocol = {
37 Register,
38 Unregister
39 };
40
41 EFI_SMM_STATUS_CODE_PROTOCOL *mFrameworkSmmStatusCode;
42
43 /**
44 Register the callback function for ReportStatusCode() notification.
45
46 When this function is called the function pointer is added to an internal list and any future calls to
47 ReportStatusCode() will be forwarded to the Callback function.
48
49 @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is called
50 when a call to ReportStatusCode() occurs.
51
52 @retval EFI_SUCCESS Function was successfully registered.
53 @retval EFI_INVALID_PARAMETER The callback function was NULL.
54 @retval EFI_OUT_OF_RESOURCES The internal buffer ran out of space. No more functions can be
55 registered.
56 @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.
57
58 **/
59 EFI_STATUS
60 EFIAPI
61 Register (
62 IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
63 )
64 {
65 LIST_ENTRY *Link;
66 SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
67
68 if (Callback == NULL) {
69 return EFI_INVALID_PARAMETER;
70 }
71
72 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
73 CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
74 if (CallbackEntry->RscHandlerCallback == Callback) {
75 //
76 // If the function was already registered. It can't be registered again.
77 //
78 return EFI_ALREADY_STARTED;
79 }
80 }
81
82 CallbackEntry = (SMM_RSC_HANDLER_CALLBACK_ENTRY *)AllocatePool (sizeof (SMM_RSC_HANDLER_CALLBACK_ENTRY));
83 ASSERT (CallbackEntry != NULL);
84
85 CallbackEntry->Signature = SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
86 CallbackEntry->RscHandlerCallback = Callback;
87
88 InsertTailList (&mCallbackListHead, &CallbackEntry->Node);
89
90 return EFI_SUCCESS;
91 }
92
93 /**
94 Remove a previously registered callback function from the notification list.
95
96 ReportStatusCode() messages will no longer be forwarded to the Callback function.
97
98 @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is to be
99 unregistered.
100
101 @retval EFI_SUCCESS The function was successfully unregistered.
102 @retval EFI_INVALID_PARAMETER The callback function was NULL.
103 @retval EFI_NOT_FOUND The callback function was not found to be unregistered.
104
105 **/
106 EFI_STATUS
107 EFIAPI
108 Unregister (
109 IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
110 )
111 {
112 LIST_ENTRY *Link;
113 SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
114
115 if (Callback == NULL) {
116 return EFI_INVALID_PARAMETER;
117 }
118
119 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
120 CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
121 if (CallbackEntry->RscHandlerCallback == Callback) {
122 //
123 // If the function is found in list, delete it and return.
124 //
125 RemoveEntryList (&CallbackEntry->Node);
126 FreePool (CallbackEntry);
127 return EFI_SUCCESS;
128 }
129 }
130
131 return EFI_NOT_FOUND;
132 }
133
134
135 /**
136 Provides an interface that a software module can call to report a status code.
137
138 @param This EFI_SMM_STATUS_CODE_PROTOCOL instance.
139 @param Type Indicates the type of status code being reported.
140 @param Value Describes the current status of a hardware or software entity.
141 This included information about the class and subclass that is used to
142 classify the entity as well as an operation.
143 @param Instance The enumeration of a hardware or software entity within
144 the system. Valid instance numbers start with 1.
145 @param CallerId This optional parameter may be used to identify the caller.
146 This parameter allows the status code driver to apply different rules to
147 different callers.
148 @param Data This optional parameter may be used to pass additional data.
149
150 @retval EFI_SUCCESS The function completed successfully
151 @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
152
153 **/
154 EFI_STATUS
155 EFIAPI
156 ReportDispatcher (
157 IN CONST EFI_SMM_STATUS_CODE_PROTOCOL *This,
158 IN EFI_STATUS_CODE_TYPE Type,
159 IN EFI_STATUS_CODE_VALUE Value,
160 IN UINT32 Instance,
161 IN CONST EFI_GUID *CallerId OPTIONAL,
162 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
163 )
164 {
165 LIST_ENTRY *Link;
166 SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
167
168 //
169 // Use atom operation to avoid the reentant of report.
170 // If current status is not zero, then the function is reentrancy.
171 //
172 if (InterlockedCompareExchange32 (&mStatusCodeNestStatus, 0, 1) == 1) {
173 return EFI_DEVICE_ERROR;
174 }
175
176 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
177 CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
178
179 CallbackEntry->RscHandlerCallback (
180 Type,
181 Value,
182 Instance,
183 (EFI_GUID*)CallerId,
184 Data
185 );
186
187 }
188
189 //
190 // Restore the nest status of report
191 //
192 InterlockedCompareExchange32 (&mStatusCodeNestStatus, 1, 0);
193
194 return EFI_SUCCESS;
195 }
196
197 /**
198 This SMM Status Code Handler routes status codes to Framework SMM Status Code Protocol.
199
200 @param CodeType Indicates the type of status code being reported.
201 @param Value Describes the current status of a hardware or software entity.
202 This included information about the class and subclass that is used to
203 classify the entity as well as an operation.
204 @param Instance The enumeration of a hardware or software entity within
205 the system. Valid instance numbers start with 1.
206 @param CallerId This optional parameter may be used to identify the caller.
207 This parameter allows the status code driver to apply different rules to
208 different callers.
209 @param Data This optional parameter may be used to pass additional data.
210
211 @retval EFI_SUCCESS The function completed successfully.
212 @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
213
214 **/
215 EFI_STATUS
216 SmmStatusCodeHandler (
217 IN EFI_STATUS_CODE_TYPE CodeType,
218 IN EFI_STATUS_CODE_VALUE Value,
219 IN UINT32 Instance,
220 IN EFI_GUID *CallerId,
221 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
222 )
223 {
224 return mFrameworkSmmStatusCode->ReportStatusCode (
225 mFrameworkSmmStatusCode,
226 CodeType,
227 Value,
228 Instance,
229 CallerId,
230 Data
231 );
232 }
233
234 /**
235 Entry point of PI SMM Status Code Protocol on Framework SMM Status Code Protocol thunk driver.
236
237 @param ImageHandle The firmware allocated handle for the EFI image.
238 @param SystemTable A pointer to the EFI System Table.
239
240 @retval EFI_SUCCESS The entry point is executed successfully.
241
242 **/
243 EFI_STATUS
244 EFIAPI
245 SmmStatusCodeThunkMain (
246 IN EFI_HANDLE ImageHandle,
247 IN EFI_SYSTEM_TABLE *SystemTable
248 )
249 {
250 EFI_STATUS Status;
251 EFI_HANDLE Handle;
252
253 //
254 // Locate Framework SMM Status Code Protocol in UEFI handle database.
255 //
256 Status = SystemTable->BootServices->LocateProtocol (
257 &gEfiSmmStatusCodeProtocolGuid,
258 NULL,
259 (VOID **)&mFrameworkSmmStatusCode
260 );
261 ASSERT_EFI_ERROR (Status);
262
263 //
264 // Registers status code handler to route status codes into Framework SMM Status Code Protocol.
265 //
266 Register (SmmStatusCodeHandler);
267
268 Handle = NULL;
269
270 //
271 // Install SmmRscHandler Protocol
272 //
273 Status = gSmst->SmmInstallProtocolInterface (
274 &Handle,
275 &gEfiSmmRscHandlerProtocolGuid,
276 EFI_NATIVE_INTERFACE,
277 &mSmmRscHandlerProtocol
278 );
279 ASSERT_EFI_ERROR (Status);
280
281 //
282 // Install SmmStatusCode Protocol
283 //
284 Status = gSmst->SmmInstallProtocolInterface (
285 &Handle,
286 &gEfiSmmStatusCodeProtocolGuid,
287 EFI_NATIVE_INTERFACE,
288 &mSmmStatusCodeProtocol
289 );
290 ASSERT_EFI_ERROR (Status);
291
292 return EFI_SUCCESS;
293 }
294 /** @file
295 PI SMM Status Code Protocol on Framework SMM Status Code Protocol Thunk.
296
297 This thunk driver produces PI SMM Status Code Protocol and SMM Report Status Code Handler Protocol.
298 And it registers a status code handler within itself to route status codes into Framework SMM Status
299 Code Protocol.
300
301 Note that Framework SMM Status Code Protocol and PI SMM Status Code Protocol have identical protocol
302 GUID and interface structure, but they are in different handle databases.
303
304 Copyright (c) 2010, Intel Corporation. All rights reserved<BR>
305 This program and the accompanying materials are licensed and made available under
306 the terms and conditions of the BSD License that accompanies this distribution.
307 The full text of the license may be found at
308 http://opensource.org/licenses/bsd-license.php.
309
310 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
311 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
312
313 **/
314
315 #include "PiSmmStatusCodeOnFrameworkSmmStatusCodeThunk.h"
316
317 LIST_ENTRY mCallbackListHead = INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead);
318
319 //
320 // Report operation nest status.
321 // If it is set, then the report operation has nested.
322 //
323 UINT32 mStatusCodeNestStatus = 0;
324
325 EFI_SMM_STATUS_CODE_PROTOCOL mSmmStatusCodeProtocol = {
326 ReportDispatcher
327 };
328
329 EFI_SMM_RSC_HANDLER_PROTOCOL mSmmRscHandlerProtocol = {
330 Register,
331 Unregister
332 };
333
334 EFI_SMM_STATUS_CODE_PROTOCOL *mFrameworkSmmStatusCode;
335
336 /**
337 Register the callback function for ReportStatusCode() notification.
338
339 When this function is called the function pointer is added to an internal list and any future calls to
340 ReportStatusCode() will be forwarded to the Callback function.
341
342 @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is called
343 when a call to ReportStatusCode() occurs.
344
345 @retval EFI_SUCCESS Function was successfully registered.
346 @retval EFI_INVALID_PARAMETER The callback function was NULL.
347 @retval EFI_OUT_OF_RESOURCES The internal buffer ran out of space. No more functions can be
348 registered.
349 @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.
350
351 **/
352 EFI_STATUS
353 EFIAPI
354 Register (
355 IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
356 )
357 {
358 LIST_ENTRY *Link;
359 SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
360
361 if (Callback == NULL) {
362 return EFI_INVALID_PARAMETER;
363 }
364
365 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
366 CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
367 if (CallbackEntry->RscHandlerCallback == Callback) {
368 //
369 // If the function was already registered. It can't be registered again.
370 //
371 return EFI_ALREADY_STARTED;
372 }
373 }
374
375 CallbackEntry = (SMM_RSC_HANDLER_CALLBACK_ENTRY *)AllocatePool (sizeof (SMM_RSC_HANDLER_CALLBACK_ENTRY));
376 ASSERT (CallbackEntry != NULL);
377
378 CallbackEntry->Signature = SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
379 CallbackEntry->RscHandlerCallback = Callback;
380
381 InsertTailList (&mCallbackListHead, &CallbackEntry->Node);
382
383 return EFI_SUCCESS;
384 }
385
386 /**
387 Remove a previously registered callback function from the notification list.
388
389 ReportStatusCode() messages will no longer be forwarded to the Callback function.
390
391 @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is to be
392 unregistered.
393
394 @retval EFI_SUCCESS The function was successfully unregistered.
395 @retval EFI_INVALID_PARAMETER The callback function was NULL.
396 @retval EFI_NOT_FOUND The callback function was not found to be unregistered.
397
398 **/
399 EFI_STATUS
400 EFIAPI
401 Unregister (
402 IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
403 )
404 {
405 LIST_ENTRY *Link;
406 SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
407
408 if (Callback == NULL) {
409 return EFI_INVALID_PARAMETER;
410 }
411
412 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
413 CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
414 if (CallbackEntry->RscHandlerCallback == Callback) {
415 //
416 // If the function is found in list, delete it and return.
417 //
418 RemoveEntryList (&CallbackEntry->Node);
419 FreePool (CallbackEntry);
420 return EFI_SUCCESS;
421 }
422 }
423
424 return EFI_NOT_FOUND;
425 }
426
427
428 /**
429 Provides an interface that a software module can call to report a status code.
430
431 @param This EFI_SMM_STATUS_CODE_PROTOCOL instance.
432 @param Type Indicates the type of status code being reported.
433 @param Value Describes the current status of a hardware or software entity.
434 This included information about the class and subclass that is used to
435 classify the entity as well as an operation.
436 @param Instance The enumeration of a hardware or software entity within
437 the system. Valid instance numbers start with 1.
438 @param CallerId This optional parameter may be used to identify the caller.
439 This parameter allows the status code driver to apply different rules to
440 different callers.
441 @param Data This optional parameter may be used to pass additional data.
442
443 @retval EFI_SUCCESS The function completed successfully
444 @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
445
446 **/
447 EFI_STATUS
448 EFIAPI
449 ReportDispatcher (
450 IN CONST EFI_SMM_STATUS_CODE_PROTOCOL *This,
451 IN EFI_STATUS_CODE_TYPE Type,
452 IN EFI_STATUS_CODE_VALUE Value,
453 IN UINT32 Instance,
454 IN CONST EFI_GUID *CallerId OPTIONAL,
455 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
456 )
457 {
458 LIST_ENTRY *Link;
459 SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
460
461 //
462 // Use atom operation to avoid the reentant of report.
463 // If current status is not zero, then the function is reentrancy.
464 //
465 if (InterlockedCompareExchange32 (&mStatusCodeNestStatus, 0, 1) == 1) {
466 return EFI_DEVICE_ERROR;
467 }
468
469 for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
470 CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
471
472 CallbackEntry->RscHandlerCallback (
473 Type,
474 Value,
475 Instance,
476 (EFI_GUID*)CallerId,
477 Data
478 );
479
480 }
481
482 //
483 // Restore the nest status of report
484 //
485 InterlockedCompareExchange32 (&mStatusCodeNestStatus, 1, 0);
486
487 return EFI_SUCCESS;
488 }
489
490 /**
491 This SMM Status Code Handler routes status codes to Framework SMM Status Code Protocol.
492
493 @param CodeType Indicates the type of status code being reported.
494 @param Value Describes the current status of a hardware or software entity.
495 This included information about the class and subclass that is used to
496 classify the entity as well as an operation.
497 @param Instance The enumeration of a hardware or software entity within
498 the system. Valid instance numbers start with 1.
499 @param CallerId This optional parameter may be used to identify the caller.
500 This parameter allows the status code driver to apply different rules to
501 different callers.
502 @param Data This optional parameter may be used to pass additional data.
503
504 @retval EFI_SUCCESS The function completed successfully.
505 @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
506
507 **/
508 EFI_STATUS
509 SmmStatusCodeHandler (
510 IN EFI_STATUS_CODE_TYPE CodeType,
511 IN EFI_STATUS_CODE_VALUE Value,
512 IN UINT32 Instance,
513 IN EFI_GUID *CallerId,
514 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
515 )
516 {
517 return mFrameworkSmmStatusCode->ReportStatusCode (
518 mFrameworkSmmStatusCode,
519 CodeType,
520 Value,
521 Instance,
522 CallerId,
523 Data
524 );
525 }
526
527 /**
528 Entry point of PI SMM Status Code Protocol on Framework SMM Status Code Protocol thunk driver.
529
530 @param ImageHandle The firmware allocated handle for the EFI image.
531 @param SystemTable A pointer to the EFI System Table.
532
533 @retval EFI_SUCCESS The entry point is executed successfully.
534
535 **/
536 EFI_STATUS
537 EFIAPI
538 SmmStatusCodeThunkMain (
539 IN EFI_HANDLE ImageHandle,
540 IN EFI_SYSTEM_TABLE *SystemTable
541 )
542 {
543 EFI_STATUS Status;
544 EFI_HANDLE Handle;
545
546 //
547 // Locate Framework SMM Status Code Protocol in UEFI handle database.
548 //
549 Status = SystemTable->BootServices->LocateProtocol (
550 &gEfiSmmStatusCodeProtocolGuid,
551 NULL,
552 (VOID **)&mFrameworkSmmStatusCode
553 );
554 ASSERT_EFI_ERROR (Status);
555
556 //
557 // Registers status code handler to route status codes into Framework SMM Status Code Protocol.
558 //
559 Register (SmmStatusCodeHandler);
560
561 Handle = NULL;
562
563 //
564 // Install SmmRscHandler Protocol
565 //
566 Status = gSmst->SmmInstallProtocolInterface (
567 &Handle,
568 &gEfiSmmRscHandlerProtocolGuid,
569 EFI_NATIVE_INTERFACE,
570 &mSmmRscHandlerProtocol
571 );
572 ASSERT_EFI_ERROR (Status);
573
574 //
575 // Install SmmStatusCode Protocol
576 //
577 Status = gSmst->SmmInstallProtocolInterface (
578 &Handle,
579 &gEfiSmmStatusCodeProtocolGuid,
580 EFI_NATIVE_INTERFACE,
581 &mSmmStatusCodeProtocol
582 );
583 ASSERT_EFI_ERROR (Status);
584
585 return EFI_SUCCESS;
586 }