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