/edk2/EdkCompatibilityPkg/Compatibility/PiSmmStatusCodeOnFrameworkSmmStatusCodeThunk/PiSmmStatusCodeOnFrameworkSmmStatusCodeThunk.c

https://gitlab.com/envieidoc/Clover · C · 294 lines · 137 code · 33 blank · 124 comment · 14 complexity · a6d9529e0b2deaf73a85731026e6e16f MD5 · raw file

  1. /** @file
  2. PI SMM Status Code Protocol on Framework SMM Status Code Protocol Thunk.
  3. This thunk driver produces PI SMM Status Code Protocol and SMM Report Status Code Handler Protocol.
  4. And it registers a status code handler within itself to route status codes into Framework SMM Status
  5. Code Protocol.
  6. Note that Framework SMM Status Code Protocol and PI SMM Status Code Protocol have identical protocol
  7. GUID and interface structure, but they are in different handle databases.
  8. Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
  9. This program and the accompanying materials are licensed and made available under
  10. the terms and conditions of the BSD License that accompanies this distribution.
  11. The full text of the license may be found at
  12. http://opensource.org/licenses/bsd-license.php.
  13. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  15. **/
  16. #include "PiSmmStatusCodeOnFrameworkSmmStatusCodeThunk.h"
  17. LIST_ENTRY mCallbackListHead = INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead);
  18. //
  19. // Report operation nest status.
  20. // If it is set, then the report operation has nested.
  21. //
  22. UINT32 mStatusCodeNestStatus = 0;
  23. EFI_SMM_STATUS_CODE_PROTOCOL mSmmStatusCodeProtocol = {
  24. ReportDispatcher
  25. };
  26. EFI_SMM_RSC_HANDLER_PROTOCOL mSmmRscHandlerProtocol = {
  27. Register,
  28. Unregister
  29. };
  30. EFI_SMM_STATUS_CODE_PROTOCOL *mFrameworkSmmStatusCode;
  31. /**
  32. Register the callback function for ReportStatusCode() notification.
  33. When this function is called the function pointer is added to an internal list and any future calls to
  34. ReportStatusCode() will be forwarded to the Callback function.
  35. @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is called
  36. when a call to ReportStatusCode() occurs.
  37. @retval EFI_SUCCESS Function was successfully registered.
  38. @retval EFI_INVALID_PARAMETER The callback function was NULL.
  39. @retval EFI_OUT_OF_RESOURCES The internal buffer ran out of space. No more functions can be
  40. registered.
  41. @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.
  42. **/
  43. EFI_STATUS
  44. EFIAPI
  45. Register (
  46. IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
  47. )
  48. {
  49. LIST_ENTRY *Link;
  50. SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
  51. if (Callback == NULL) {
  52. return EFI_INVALID_PARAMETER;
  53. }
  54. for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
  55. CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
  56. if (CallbackEntry->RscHandlerCallback == Callback) {
  57. //
  58. // If the function was already registered. It can't be registered again.
  59. //
  60. return EFI_ALREADY_STARTED;
  61. }
  62. }
  63. CallbackEntry = (SMM_RSC_HANDLER_CALLBACK_ENTRY *)AllocatePool (sizeof (SMM_RSC_HANDLER_CALLBACK_ENTRY));
  64. ASSERT (CallbackEntry != NULL);
  65. CallbackEntry->Signature = SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
  66. CallbackEntry->RscHandlerCallback = Callback;
  67. InsertTailList (&mCallbackListHead, &CallbackEntry->Node);
  68. return EFI_SUCCESS;
  69. }
  70. /**
  71. Remove a previously registered callback function from the notification list.
  72. ReportStatusCode() messages will no longer be forwarded to the Callback function.
  73. @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is to be
  74. unregistered.
  75. @retval EFI_SUCCESS The function was successfully unregistered.
  76. @retval EFI_INVALID_PARAMETER The callback function was NULL.
  77. @retval EFI_NOT_FOUND The callback function was not found to be unregistered.
  78. **/
  79. EFI_STATUS
  80. EFIAPI
  81. Unregister (
  82. IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
  83. )
  84. {
  85. LIST_ENTRY *Link;
  86. SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
  87. if (Callback == NULL) {
  88. return EFI_INVALID_PARAMETER;
  89. }
  90. for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
  91. CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
  92. if (CallbackEntry->RscHandlerCallback == Callback) {
  93. //
  94. // If the function is found in list, delete it and return.
  95. //
  96. RemoveEntryList (&CallbackEntry->Node);
  97. FreePool (CallbackEntry);
  98. return EFI_SUCCESS;
  99. }
  100. }
  101. return EFI_NOT_FOUND;
  102. }
  103. /**
  104. Provides an interface that a software module can call to report a status code.
  105. @param This EFI_SMM_STATUS_CODE_PROTOCOL instance.
  106. @param Type Indicates the type of status code being reported.
  107. @param Value Describes the current status of a hardware or software entity.
  108. This included information about the class and subclass that is used to
  109. classify the entity as well as an operation.
  110. @param Instance The enumeration of a hardware or software entity within
  111. the system. Valid instance numbers start with 1.
  112. @param CallerId This optional parameter may be used to identify the caller.
  113. This parameter allows the status code driver to apply different rules to
  114. different callers.
  115. @param Data This optional parameter may be used to pass additional data.
  116. @retval EFI_SUCCESS The function completed successfully
  117. @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
  118. **/
  119. EFI_STATUS
  120. EFIAPI
  121. ReportDispatcher (
  122. IN CONST EFI_SMM_STATUS_CODE_PROTOCOL *This,
  123. IN EFI_STATUS_CODE_TYPE Type,
  124. IN EFI_STATUS_CODE_VALUE Value,
  125. IN UINT32 Instance,
  126. IN CONST EFI_GUID *CallerId OPTIONAL,
  127. IN EFI_STATUS_CODE_DATA *Data OPTIONAL
  128. )
  129. {
  130. LIST_ENTRY *Link;
  131. SMM_RSC_HANDLER_CALLBACK_ENTRY *CallbackEntry;
  132. //
  133. // Use atom operation to avoid the reentant of report.
  134. // If current status is not zero, then the function is reentrancy.
  135. //
  136. if (InterlockedCompareExchange32 (&mStatusCodeNestStatus, 0, 1) == 1) {
  137. return EFI_DEVICE_ERROR;
  138. }
  139. for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
  140. CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
  141. CallbackEntry->RscHandlerCallback (
  142. Type,
  143. Value,
  144. Instance,
  145. (EFI_GUID*)CallerId,
  146. Data
  147. );
  148. }
  149. //
  150. // Restore the nest status of report
  151. //
  152. InterlockedCompareExchange32 (&mStatusCodeNestStatus, 1, 0);
  153. return EFI_SUCCESS;
  154. }
  155. /**
  156. This SMM Status Code Handler routes status codes to Framework SMM Status Code Protocol.
  157. @param CodeType Indicates the type of status code being reported.
  158. @param Value Describes the current status of a hardware or software entity.
  159. This included information about the class and subclass that is used to
  160. classify the entity as well as an operation.
  161. @param Instance The enumeration of a hardware or software entity within
  162. the system. Valid instance numbers start with 1.
  163. @param CallerId This optional parameter may be used to identify the caller.
  164. This parameter allows the status code driver to apply different rules to
  165. different callers.
  166. @param Data This optional parameter may be used to pass additional data.
  167. @retval EFI_SUCCESS The function completed successfully.
  168. @retval EFI_DEVICE_ERROR The function should not be completed due to a device error.
  169. **/
  170. EFI_STATUS
  171. EFIAPI
  172. SmmStatusCodeHandler (
  173. IN EFI_STATUS_CODE_TYPE CodeType,
  174. IN EFI_STATUS_CODE_VALUE Value,
  175. IN UINT32 Instance,
  176. IN EFI_GUID *CallerId,
  177. IN EFI_STATUS_CODE_DATA *Data OPTIONAL
  178. )
  179. {
  180. return mFrameworkSmmStatusCode->ReportStatusCode (
  181. mFrameworkSmmStatusCode,
  182. CodeType,
  183. Value,
  184. Instance,
  185. CallerId,
  186. Data
  187. );
  188. }
  189. /**
  190. Entry point of PI SMM Status Code Protocol on Framework SMM Status Code Protocol thunk driver.
  191. @param ImageHandle The firmware allocated handle for the EFI image.
  192. @param SystemTable A pointer to the EFI System Table.
  193. @retval EFI_SUCCESS The entry point is executed successfully.
  194. **/
  195. EFI_STATUS
  196. EFIAPI
  197. SmmStatusCodeThunkMain (
  198. IN EFI_HANDLE ImageHandle,
  199. IN EFI_SYSTEM_TABLE *SystemTable
  200. )
  201. {
  202. EFI_STATUS Status;
  203. EFI_HANDLE Handle;
  204. //
  205. // Locate Framework SMM Status Code Protocol in UEFI handle database.
  206. //
  207. Status = SystemTable->BootServices->LocateProtocol (
  208. &gEfiSmmStatusCodeProtocolGuid,
  209. NULL,
  210. (VOID **)&mFrameworkSmmStatusCode
  211. );
  212. ASSERT_EFI_ERROR (Status);
  213. //
  214. // Registers status code handler to route status codes into Framework SMM Status Code Protocol.
  215. //
  216. Register (SmmStatusCodeHandler);
  217. Handle = NULL;
  218. //
  219. // Install SmmRscHandler Protocol
  220. //
  221. Status = gSmst->SmmInstallProtocolInterface (
  222. &Handle,
  223. &gEfiSmmRscHandlerProtocolGuid,
  224. EFI_NATIVE_INTERFACE,
  225. &mSmmRscHandlerProtocol
  226. );
  227. ASSERT_EFI_ERROR (Status);
  228. //
  229. // Install SmmStatusCode Protocol
  230. //
  231. Status = gSmst->SmmInstallProtocolInterface (
  232. &Handle,
  233. &gEfiSmmStatusCodeProtocolGuid,
  234. EFI_NATIVE_INTERFACE,
  235. &mSmmStatusCodeProtocol
  236. );
  237. ASSERT_EFI_ERROR (Status);
  238. return EFI_SUCCESS;
  239. }