Home | History | Annotate | Download | only in PiSmmStatusCodeOnFrameworkSmmStatusCodeThunk
      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 EFIAPI
    217 SmmStatusCodeHandler (
    218   IN EFI_STATUS_CODE_TYPE     CodeType,
    219   IN EFI_STATUS_CODE_VALUE    Value,
    220   IN UINT32                   Instance,
    221   IN EFI_GUID                 *CallerId,
    222   IN EFI_STATUS_CODE_DATA     *Data OPTIONAL
    223   )
    224 {
    225   return mFrameworkSmmStatusCode->ReportStatusCode (
    226                                     mFrameworkSmmStatusCode,
    227                                     CodeType,
    228                                     Value,
    229                                     Instance,
    230                                     CallerId,
    231                                     Data
    232                                     );
    233 }
    234 
    235 /**
    236   Entry point of PI SMM Status Code Protocol on Framework SMM Status Code Protocol thunk driver.
    237 
    238   @param  ImageHandle       The firmware allocated handle for the EFI image.
    239   @param  SystemTable       A pointer to the EFI System Table.
    240 
    241   @retval EFI_SUCCESS       The entry point is executed successfully.
    242 
    243 **/
    244 EFI_STATUS
    245 EFIAPI
    246 SmmStatusCodeThunkMain (
    247   IN EFI_HANDLE         ImageHandle,
    248   IN EFI_SYSTEM_TABLE   *SystemTable
    249   )
    250 {
    251   EFI_STATUS     Status;
    252   EFI_HANDLE     Handle;
    253 
    254   //
    255   // Locate Framework SMM Status Code Protocol in UEFI handle database.
    256   //
    257   Status = SystemTable->BootServices->LocateProtocol (
    258                                         &gEfiSmmStatusCodeProtocolGuid,
    259                                         NULL,
    260                                         (VOID **)&mFrameworkSmmStatusCode
    261                                         );
    262   ASSERT_EFI_ERROR (Status);
    263 
    264   //
    265   // Registers status code handler to route status codes into Framework SMM Status Code Protocol.
    266   //
    267   Register (SmmStatusCodeHandler);
    268 
    269   Handle     = NULL;
    270 
    271   //
    272   // Install SmmRscHandler Protocol
    273   //
    274   Status = gSmst->SmmInstallProtocolInterface (
    275                     &Handle,
    276                     &gEfiSmmRscHandlerProtocolGuid,
    277                     EFI_NATIVE_INTERFACE,
    278                     &mSmmRscHandlerProtocol
    279                     );
    280   ASSERT_EFI_ERROR (Status);
    281 
    282   //
    283   // Install SmmStatusCode Protocol
    284   //
    285   Status = gSmst->SmmInstallProtocolInterface (
    286                     &Handle,
    287                     &gEfiSmmStatusCodeProtocolGuid,
    288                     EFI_NATIVE_INTERFACE,
    289                     &mSmmStatusCodeProtocol
    290                     );
    291   ASSERT_EFI_ERROR (Status);
    292 
    293   return EFI_SUCCESS;
    294 }
    295