]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/ReportStatusCodeRouter/Pei/ReportStatusCodeRouterPei.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / ReportStatusCodeRouter / Pei / ReportStatusCodeRouterPei.c
CommitLineData
25126a05 1/** @file\r
2 Report Status Code Router PEIM which produces Report Stataus Code Handler PPI and Status Code PPI.\r
3\r
d1102dba 4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 5 This program and the accompanying materials\r
25126a05 6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "ReportStatusCodeRouterPei.h"\r
16\r
17EFI_PEI_RSC_HANDLER_PPI mRscHandlerPpi = {\r
18 Register,\r
19 Unregister\r
20 };\r
21\r
22EFI_PEI_PROGRESS_CODE_PPI mStatusCodePpi = {\r
23 ReportDispatcher\r
24 };\r
25\r
e9867606 26EFI_PEI_PPI_DESCRIPTOR mRscHandlerPpiList[] = {\r
25126a05 27 {\r
e9867606 28 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
25126a05 29 &gEfiPeiRscHandlerPpiGuid,\r
30 &mRscHandlerPpi\r
e9867606 31 }\r
32};\r
33\r
34EFI_PEI_PPI_DESCRIPTOR mStatusCodePpiList[] = {\r
25126a05 35 {\r
36 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
37 &gEfiPeiStatusCodePpiGuid,\r
38 &mStatusCodePpi\r
39 }\r
40};\r
41\r
42/**\r
43 Worker function to create one memory status code GUID'ed HOB,\r
44 using PacketIndex to identify the packet.\r
45\r
46 @param PacketIndex Index of records packet.\r
47\r
48 @return Pointer to the memory status code packet.\r
49\r
50**/\r
51UINTN *\r
52CreateRscHandlerCallbackPacket (\r
53 VOID\r
54 )\r
55{\r
56 UINTN *NumberOfEntries;\r
57\r
58 //\r
59 // Build GUID'ed HOB with PCD defined size.\r
60 //\r
61 NumberOfEntries = BuildGuidHob (\r
62 &gStatusCodeCallbackGuid,\r
63 sizeof (EFI_PEI_RSC_HANDLER_CALLBACK) * 64 + sizeof (UINTN)\r
64 );\r
65 ASSERT (NumberOfEntries != NULL);\r
66\r
67 *NumberOfEntries = 0;\r
68\r
69 return NumberOfEntries;\r
70}\r
71\r
72/**\r
73 Register the callback function for ReportStatusCode() notification.\r
d1102dba 74\r
25126a05 75 When this function is called the function pointer is added to an internal list and any future calls to\r
76 ReportStatusCode() will be forwarded to the Callback function.\r
77\r
78 @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is called\r
79 when a call to ReportStatusCode() occurs.\r
d1102dba 80\r
25126a05 81 @retval EFI_SUCCESS Function was successfully registered.\r
82 @retval EFI_INVALID_PARAMETER The callback function was NULL.\r
83 @retval EFI_OUT_OF_RESOURCES The internal buffer ran out of space. No more functions can be\r
84 registered.\r
85 @retval EFI_ALREADY_STARTED The function was already registered. It can't be registered again.\r
d1102dba 86\r
25126a05 87**/\r
88EFI_STATUS\r
89EFIAPI\r
90Register (\r
91 IN EFI_PEI_RSC_HANDLER_CALLBACK Callback\r
92 )\r
93{\r
94 EFI_PEI_HOB_POINTERS Hob;\r
95 EFI_PEI_RSC_HANDLER_CALLBACK *CallbackEntry;\r
96 UINTN *NumberOfEntries;\r
97 UINTN Index;\r
04bbf1f3 98 UINTN FreeEntryIndex;\r
25126a05 99 UINTN *FreePacket;\r
100\r
101 if (Callback == NULL) {\r
102 return EFI_INVALID_PARAMETER;\r
103 }\r
104\r
04bbf1f3 105 Hob.Raw = GetFirstGuidHob (&gStatusCodeCallbackGuid);\r
106 FreePacket = NULL;\r
107 FreeEntryIndex = 0;\r
25126a05 108 while (Hob.Raw != NULL) {\r
109 NumberOfEntries = GET_GUID_HOB_DATA (Hob);\r
110 CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (NumberOfEntries + 1);\r
04bbf1f3 111 if (FreePacket == NULL && *NumberOfEntries < 64) {\r
112 //\r
113 // If current total number of handlers does not exceed 64, put new handler\r
114 // at the last of packet\r
115 //\r
25126a05 116 FreePacket = NumberOfEntries;\r
04bbf1f3 117 FreeEntryIndex = *NumberOfEntries;\r
25126a05 118 }\r
119 for (Index = 0; Index < *NumberOfEntries; Index++) {\r
120 if (CallbackEntry[Index] == Callback) {\r
121 //\r
122 // If the function was already registered. It can't be registered again.\r
123 //\r
124 return EFI_ALREADY_STARTED;\r
125 }\r
04bbf1f3 126 if (FreePacket == NULL && CallbackEntry[Index] == NULL) {\r
127 //\r
128 // If the total number of handlers in current packet is max value 64,\r
129 // search an entry with NULL pointer and fill new handler into this entry.\r
d1102dba 130 //\r
04bbf1f3 131 FreePacket = NumberOfEntries;\r
132 FreeEntryIndex = Index;\r
133 }\r
25126a05 134 }\r
135 Hob.Raw = GET_NEXT_HOB (Hob);\r
136 Hob.Raw = GetNextGuidHob (&gStatusCodeCallbackGuid, Hob.Raw);\r
137 }\r
138\r
139 if (FreePacket == NULL) {\r
140 FreePacket = CreateRscHandlerCallbackPacket();\r
141 }\r
142\r
143 CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (FreePacket + 1);\r
04bbf1f3 144 CallbackEntry[FreeEntryIndex] = Callback;\r
d1102dba 145\r
04bbf1f3 146 if (*FreePacket == FreeEntryIndex) {\r
147 //\r
148 // If new registered callback is added as a new entry in the packet,\r
149 // increase the total number of handlers in the packet.\r
150 //\r
151 *FreePacket += 1;\r
152 }\r
25126a05 153\r
154 return EFI_SUCCESS;\r
155}\r
156\r
157/**\r
158 Remove a previously registered callback function from the notification list.\r
d1102dba 159\r
25126a05 160 ReportStatusCode() messages will no longer be forwarded to the Callback function.\r
d1102dba 161\r
25126a05 162 @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is to be\r
163 unregistered.\r
164\r
165 @retval EFI_SUCCESS The function was successfully unregistered.\r
166 @retval EFI_INVALID_PARAMETER The callback function was NULL.\r
167 @retval EFI_NOT_FOUND The callback function was not found to be unregistered.\r
d1102dba 168\r
25126a05 169**/\r
170EFI_STATUS\r
171EFIAPI\r
172Unregister (\r
173 IN EFI_PEI_RSC_HANDLER_CALLBACK Callback\r
174 )\r
175{\r
176 EFI_PEI_HOB_POINTERS Hob;\r
177 EFI_PEI_RSC_HANDLER_CALLBACK *CallbackEntry;\r
178 UINTN *NumberOfEntries;\r
179 UINTN Index;\r
180\r
181 if (Callback == NULL) {\r
182 return EFI_INVALID_PARAMETER;\r
183 }\r
184\r
185 Hob.Raw = GetFirstGuidHob (&gStatusCodeCallbackGuid);\r
186 while (Hob.Raw != NULL) {\r
187 NumberOfEntries = GET_GUID_HOB_DATA (Hob);\r
188 CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (NumberOfEntries + 1);\r
189 for (Index = 0; Index < *NumberOfEntries; Index++) {\r
190 if (CallbackEntry[Index] == Callback) {\r
04bbf1f3 191 //\r
192 // Set removed entry as NULL.\r
193 //\r
194 CallbackEntry[Index] = NULL;\r
25126a05 195 return EFI_SUCCESS;\r
196 }\r
197 }\r
198 Hob.Raw = GET_NEXT_HOB (Hob);\r
199 Hob.Raw = GetNextGuidHob (&gStatusCodeCallbackGuid, Hob.Raw);\r
200 }\r
201\r
202 return EFI_NOT_FOUND;\r
203}\r
204\r
205/**\r
206 Publishes an interface that allows PEIMs to report status codes.\r
207\r
208 This function implements EFI_PEI_PROGRESS_CODE_PPI.ReportStatusCode().\r
209 It publishes an interface that allows PEIMs to report status codes.\r
210\r
211 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
212 @param CodeType Indicates the type of status code being reported.\r
213 @param Value Describes the current status of a hardware or\r
214 software entity. This includes information about the class and\r
215 subclass that is used to classify the entity as well as an operation.\r
216 For progress codes, the operation is the current activity.\r
217 For error codes, it is the exception.For debug codes,it is not defined at this time.\r
218 @param Instance The enumeration of a hardware or software entity within\r
219 the system. A system may contain multiple entities that match a class/subclass\r
220 pairing. The instance differentiates between them. An instance of 0 indicates\r
221 that instance information is unavailable, not meaningful, or not relevant.\r
222 Valid instance numbers start with 1.\r
223 @param CallerId This optional parameter may be used to identify the caller.\r
224 This parameter allows the status code driver to apply different rules to\r
225 different callers.\r
226 @param Data This optional parameter may be used to pass additional data.\r
227\r
228 @retval EFI_SUCCESS The function completed successfully.\r
229\r
230**/\r
231EFI_STATUS\r
232EFIAPI\r
233ReportDispatcher (\r
234 IN CONST EFI_PEI_SERVICES **PeiServices,\r
235 IN EFI_STATUS_CODE_TYPE CodeType,\r
236 IN EFI_STATUS_CODE_VALUE Value,\r
237 IN UINT32 Instance,\r
238 IN CONST EFI_GUID *CallerId OPTIONAL,\r
239 IN CONST EFI_STATUS_CODE_DATA *Data OPTIONAL\r
240 )\r
241{\r
242 EFI_PEI_HOB_POINTERS Hob;\r
243 EFI_PEI_RSC_HANDLER_CALLBACK *CallbackEntry;\r
244 UINTN *NumberOfEntries;\r
245 UINTN Index;\r
246\r
247 Hob.Raw = GetFirstGuidHob (&gStatusCodeCallbackGuid);\r
248 while (Hob.Raw != NULL) {\r
249 NumberOfEntries = GET_GUID_HOB_DATA (Hob);\r
250 CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (NumberOfEntries + 1);\r
251 for (Index = 0; Index < *NumberOfEntries; Index++) {\r
e846c180 252 if (CallbackEntry[Index] != NULL) {\r
25126a05 253 CallbackEntry[Index](\r
254 PeiServices,\r
255 CodeType,\r
256 Value,\r
257 Instance,\r
258 CallerId,\r
259 Data\r
260 );\r
e846c180 261 }\r
25126a05 262 }\r
263 Hob.Raw = GET_NEXT_HOB (Hob);\r
264 Hob.Raw = GetNextGuidHob (&gStatusCodeCallbackGuid, Hob.Raw);\r
265 }\r
266\r
267 return EFI_SUCCESS;\r
268}\r
269\r
270/**\r
271 Entry point of Status Code PEIM.\r
d1102dba 272\r
25126a05 273 This function is the entry point of this Status Code Router PEIM.\r
274 It produces Report Stataus Code Handler PPI and Status Code PPI.\r
275\r
276 @param FileHandle Handle of the file being invoked.\r
277 @param PeiServices Describes the list of possible PEI Services.\r
278\r
279 @retval EFI_SUCESS The entry point of DXE IPL PEIM executes successfully.\r
280\r
281**/\r
282EFI_STATUS\r
283EFIAPI\r
284GenericStatusCodePeiEntry (\r
285 IN EFI_PEI_FILE_HANDLE FileHandle,\r
286 IN CONST EFI_PEI_SERVICES **PeiServices\r
287 )\r
288{\r
e9867606 289 EFI_STATUS Status;\r
290 EFI_PEI_PPI_DESCRIPTOR *OldDescriptor;\r
291 EFI_PEI_PROGRESS_CODE_PPI *OldStatusCodePpi;\r
25126a05 292\r
293 CreateRscHandlerCallbackPacket ();\r
294\r
e9867606 295 //\r
296 // Install Report Status Code Handler PPI\r
297 //\r
298 Status = PeiServicesInstallPpi (mRscHandlerPpiList);\r
299 ASSERT_EFI_ERROR (Status);\r
300\r
301 //\r
302 // Install Status Code PPI. PI spec specifies that there can be only one instance\r
303 // of this PPI in system. So first check if other instance already exists.\r
304 // If no other instance exists, then just install the PPI.\r
305 // If other instance already exists, then reinstall it.\r
306 //\r
307 Status = PeiServicesLocatePpi (\r
308 &gEfiPeiStatusCodePpiGuid,\r
309 0,\r
310 &OldDescriptor,\r
311 (VOID **) &OldStatusCodePpi\r
312 );\r
313 if (!EFI_ERROR (Status)) {\r
314 Status = PeiServicesReInstallPpi (OldDescriptor, mStatusCodePpiList);\r
315 } else {\r
316 Status = PeiServicesInstallPpi (mStatusCodePpiList);\r
317 }\r
25126a05 318 ASSERT_EFI_ERROR (Status);\r
319\r
320 return EFI_SUCCESS;\r
321}\r