ExReportStatusCodeRouterPei.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /** @file
  2. Report Status Code Router PEIM which produces Report Stataus Code Handler PPI and Status Code PPI.
  3. @copyright
  4. Copyright (c) 2009 - 2021, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "ExReportStatusCodeRouterPei.h"
  8. EFI_PEI_RSC_HANDLER_PPI mRscHandlerPpi = {
  9. Register,
  10. Unregister
  11. };
  12. EFI_PEI_PROGRESS_CODE_PPI mStatusCodePpi = {
  13. ReportDispatcher
  14. };
  15. EFI_PEI_PPI_DESCRIPTOR mRscHandlerPpiList[] = {
  16. {
  17. EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
  18. &gEfiPeiRscHandlerPpiGuid,
  19. &mRscHandlerPpi
  20. }
  21. };
  22. EFI_PEI_PPI_DESCRIPTOR mStatusCodePpiList[] = {
  23. {
  24. EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
  25. &gEfiPeiStatusCodePpiGuid,
  26. &mStatusCodePpi
  27. }
  28. };
  29. /**
  30. Worker function to create one memory status code GUID'ed HOB,
  31. using PacketIndex to identify the packet.
  32. @param PacketIndex Index of records packet.
  33. @return Pointer to the memory status code packet.
  34. **/
  35. UINTN *
  36. CreateRscHandlerCallbackPacket (
  37. VOID
  38. )
  39. {
  40. UINTN *NumberOfEntries;
  41. EFI_PEI_RSC_HANDLER_CALLBACK *CallbackEntry;
  42. UINT16 Index;
  43. //
  44. // Build GUID'ed HOB with PCD defined size.
  45. //
  46. NumberOfEntries = BuildGuidHob (
  47. &gStatusCodeCallbackGuid,
  48. sizeof (EFI_PEI_RSC_HANDLER_CALLBACK) * 64 + sizeof (UINTN)
  49. );
  50. ASSERT (NumberOfEntries != NULL);
  51. if (NumberOfEntries == NULL) {
  52. return NumberOfEntries;
  53. }
  54. *NumberOfEntries = 0;
  55. CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (NumberOfEntries + 1);
  56. for (Index = 0; Index < 64; Index++) {
  57. CallbackEntry[Index] = NULL;
  58. }
  59. return NumberOfEntries;
  60. }
  61. /**
  62. Register the callback function for ReportStatusCode() notification.
  63. When this function is called the function pointer is added to an internal list and any future calls to
  64. ReportStatusCode() will be forwarded to the Callback function.
  65. @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is called
  66. when a call to ReportStatusCode() occurs.
  67. @retval EFI_SUCCESS Function was successfully registered.
  68. @retval EFI_INVALID_PARAMETER The callback function was NULL.
  69. @retval EFI_OUT_OF_RESOURCES The internal buffer ran out of space. No more functions can be
  70. registered.
  71. @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.
  72. **/
  73. EFI_STATUS
  74. EFIAPI
  75. Register (
  76. IN EFI_PEI_RSC_HANDLER_CALLBACK Callback
  77. )
  78. {
  79. EFI_PEI_HOB_POINTERS Hob;
  80. EFI_PEI_RSC_HANDLER_CALLBACK *CallbackEntry;
  81. UINTN *NumberOfEntries;
  82. UINTN Index;
  83. if (Callback == NULL) {
  84. return EFI_INVALID_PARAMETER;
  85. }
  86. Hob.Raw = GetFirstGuidHob (&gStatusCodeCallbackGuid);
  87. if (Hob.Raw != NULL) {
  88. NumberOfEntries = GET_GUID_HOB_DATA (Hob);
  89. CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (NumberOfEntries + 1);
  90. if (*NumberOfEntries >= 64) {
  91. //
  92. // If current total number of handlers does exceed 64, bail
  93. //
  94. return EFI_OUT_OF_RESOURCES;
  95. }
  96. for (Index = 0; Index <= *NumberOfEntries; Index++) {
  97. if (CallbackEntry[Index] == Callback) {
  98. //
  99. // If the function was already registered. It can't be registered again.
  100. //
  101. return EFI_ALREADY_STARTED;
  102. }
  103. if (CallbackEntry[Index] == NULL) {
  104. //
  105. // If the total number of handlers in current packet is max value 64,
  106. // search an entry with NULL pointer and fill new handler into this entry.
  107. //
  108. *NumberOfEntries += 1;
  109. CallbackEntry[Index] = Callback;
  110. return EFI_SUCCESS;
  111. }
  112. }
  113. }
  114. return EFI_NOT_FOUND;
  115. }
  116. /**
  117. Remove a previously registered callback function from the notification list.
  118. ReportStatusCode() messages will no longer be forwarded to the Callback function.
  119. @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is to be
  120. unregistered.
  121. @retval EFI_SUCCESS The function was successfully unregistered.
  122. @retval EFI_INVALID_PARAMETER The callback function was NULL.
  123. @retval EFI_NOT_FOUND The callback function was not found to be unregistered.
  124. **/
  125. EFI_STATUS
  126. EFIAPI
  127. Unregister (
  128. IN EFI_PEI_RSC_HANDLER_CALLBACK Callback
  129. )
  130. {
  131. EFI_PEI_HOB_POINTERS Hob;
  132. EFI_PEI_RSC_HANDLER_CALLBACK *CallbackEntry;
  133. UINTN *NumberOfEntries;
  134. UINTN Index;
  135. if (Callback == NULL) {
  136. return EFI_INVALID_PARAMETER;
  137. }
  138. Hob.Raw = GetFirstGuidHob (&gStatusCodeCallbackGuid);
  139. if (Hob.Raw != NULL) {
  140. NumberOfEntries = GET_GUID_HOB_DATA (Hob);
  141. CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (NumberOfEntries + 1);
  142. for (Index = 0; Index < *NumberOfEntries; Index++) {
  143. if (CallbackEntry[Index] == Callback) {
  144. //
  145. // Set removed entry as NULL.
  146. //
  147. CallbackEntry[Index] = NULL;
  148. return EFI_SUCCESS;
  149. }
  150. }
  151. }
  152. return EFI_NOT_FOUND;
  153. }
  154. /**
  155. Publishes an interface that allows PEIMs to report status codes.
  156. This function implements EFI_PEI_PROGRESS_CODE_PPI.ReportStatusCode().
  157. It publishes an interface that allows PEIMs to report status codes.
  158. @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
  159. @param CodeType Indicates the type of status code being reported.
  160. @param Value Describes the current status of a hardware or
  161. software entity. This includes information about the class and
  162. subclass that is used to classify the entity as well as an operation.
  163. For progress codes, the operation is the current activity.
  164. For error codes, it is the exception.For debug codes,it is not defined at this time.
  165. @param Instance The enumeration of a hardware or software entity within
  166. the system. A system may contain multiple entities that match a class/subclass
  167. pairing. The instance differentiates between them. An instance of 0 indicates
  168. that instance information is unavailable, not meaningful, or not relevant.
  169. Valid instance numbers start with 1.
  170. @param CallerId This optional parameter may be used to identify the caller.
  171. This parameter allows the status code driver to apply different rules to
  172. different callers.
  173. @param Data This optional parameter may be used to pass additional data.
  174. @retval EFI_SUCCESS The function completed successfully.
  175. **/
  176. EFI_STATUS
  177. EFIAPI
  178. ReportDispatcher (
  179. IN CONST EFI_PEI_SERVICES **PeiServices,
  180. IN EFI_STATUS_CODE_TYPE CodeType,
  181. IN EFI_STATUS_CODE_VALUE Value,
  182. IN UINT32 Instance,
  183. IN CONST EFI_GUID *CallerId OPTIONAL,
  184. IN CONST EFI_STATUS_CODE_DATA *Data OPTIONAL
  185. )
  186. {
  187. EFI_PEI_HOB_POINTERS Hob;
  188. EFI_PEI_RSC_HANDLER_CALLBACK *CallbackEntry;
  189. UINTN *NumberOfEntries;
  190. UINTN Index;
  191. Hob.Raw = GetFirstGuidHob (&gStatusCodeCallbackGuid);
  192. if (Hob.Raw != NULL) {
  193. NumberOfEntries = GET_GUID_HOB_DATA (Hob);
  194. CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (NumberOfEntries + 1);
  195. for (Index = 0; Index < *NumberOfEntries; Index++) {
  196. if (CallbackEntry[Index] != NULL) {
  197. CallbackEntry[Index](
  198. PeiServices,
  199. CodeType,
  200. Value,
  201. Instance,
  202. CallerId,
  203. Data
  204. );
  205. }
  206. }
  207. }
  208. return EFI_SUCCESS;
  209. }
  210. /**
  211. Entry point of Status Code PEIM.
  212. This function is the entry point of this Status Code Router PEIM.
  213. It produces Report Stataus Code Handler PPI and Status Code PPI.
  214. @param FileHandle Handle of the file being invoked.
  215. @param PeiServices Describes the list of possible PEI Services.
  216. @retval EFI_SUCESS The entry point of DXE IPL PEIM executes successfully.
  217. **/
  218. EFI_STATUS
  219. EFIAPI
  220. GenericStatusCodePeiEntry (
  221. IN EFI_PEI_FILE_HANDLE FileHandle,
  222. IN CONST EFI_PEI_SERVICES **PeiServices
  223. )
  224. {
  225. EFI_STATUS Status;
  226. EFI_PEI_PPI_DESCRIPTOR *OldDescriptor;
  227. EFI_PEI_PROGRESS_CODE_PPI *OldStatusCodePpi;
  228. CreateRscHandlerCallbackPacket ();
  229. //
  230. // Install Report Status Code Handler PPI
  231. //
  232. Status = PeiServicesInstallPpi (mRscHandlerPpiList);
  233. ASSERT_EFI_ERROR (Status);
  234. //
  235. // Install Status Code PPI. PI spec specifies that there can be only one instance
  236. // of this PPI in system. So first check if other instance already exists.
  237. // If no other instance exists, then just install the PPI.
  238. // If other instance already exists, then reinstall it.
  239. //
  240. Status = PeiServicesLocatePpi (
  241. &gEfiPeiStatusCodePpiGuid,
  242. 0,
  243. &OldDescriptor,
  244. (VOID **) &OldStatusCodePpi
  245. );
  246. if (!EFI_ERROR (Status)) {
  247. Status = PeiServicesReInstallPpi (OldDescriptor, mStatusCodePpiList);
  248. } else {
  249. Status = PeiServicesInstallPpi (mStatusCodePpiList);
  250. }
  251. ASSERT_EFI_ERROR (Status);
  252. return EFI_SUCCESS;
  253. }