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 *
CreateRscHandlerCallbackPacket(VOID)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
Register(IN EFI_PEI_RSC_HANDLER_CALLBACK Callback)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
Unregister(IN EFI_PEI_RSC_HANDLER_CALLBACK Callback)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
ReportDispatcher(IN CONST EFI_PEI_SERVICES ** PeiServices,IN EFI_STATUS_CODE_TYPE CodeType,IN EFI_STATUS_CODE_VALUE Value,IN UINT32 Instance,IN CONST EFI_GUID * CallerId OPTIONAL,IN CONST EFI_STATUS_CODE_DATA * Data OPTIONAL)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       if (CallbackEntry[Index] != NULL) {
253       CallbackEntry[Index](
254         PeiServices,
255         CodeType,
256         Value,
257         Instance,
258         CallerId,
259         Data
260         );
261       }
262     }
263     Hob.Raw = GET_NEXT_HOB (Hob);
264     Hob.Raw = GetNextGuidHob (&gStatusCodeCallbackGuid, Hob.Raw);
265   }
266 
267   return EFI_SUCCESS;
268 }
269 
270 /**
271   Entry point of Status Code PEIM.
272 
273   This function is the entry point of this Status Code Router PEIM.
274   It produces Report Stataus Code Handler PPI and Status Code PPI.
275 
276   @param  FileHandle  Handle of the file being invoked.
277   @param  PeiServices Describes the list of possible PEI Services.
278 
279   @retval EFI_SUCESS  The entry point of DXE IPL PEIM executes successfully.
280 
281 **/
282 EFI_STATUS
283 EFIAPI
GenericStatusCodePeiEntry(IN EFI_PEI_FILE_HANDLE FileHandle,IN CONST EFI_PEI_SERVICES ** PeiServices)284 GenericStatusCodePeiEntry (
285   IN       EFI_PEI_FILE_HANDLE  FileHandle,
286   IN CONST EFI_PEI_SERVICES     **PeiServices
287   )
288 {
289   EFI_STATUS                 Status;
290   EFI_PEI_PPI_DESCRIPTOR     *OldDescriptor;
291   EFI_PEI_PROGRESS_CODE_PPI  *OldStatusCodePpi;
292 
293   CreateRscHandlerCallbackPacket ();
294 
295   //
296   // Install Report Status Code Handler PPI
297   //
298   Status = PeiServicesInstallPpi (mRscHandlerPpiList);
299   ASSERT_EFI_ERROR (Status);
300 
301   //
302   // Install Status Code PPI. PI spec specifies that there can be only one instance
303   // of this PPI in system. So first check if other instance already exists.
304   // If no other instance exists, then just install the PPI.
305   // If other instance already exists, then reinstall it.
306   //
307   Status = PeiServicesLocatePpi (
308              &gEfiPeiStatusCodePpiGuid,
309              0,
310              &OldDescriptor,
311              (VOID **) &OldStatusCodePpi
312              );
313   if (!EFI_ERROR (Status)) {
314     Status = PeiServicesReInstallPpi (OldDescriptor, mStatusCodePpiList);
315   } else {
316     Status = PeiServicesInstallPpi (mStatusCodePpiList);
317   }
318   ASSERT_EFI_ERROR (Status);
319 
320   return EFI_SUCCESS;
321 }
322