]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/ReportStatusCodeRouter/Pei/ReportStatusCodeRouterPei.c
Update the copyright notice format
[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 - 2010, 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 *FreePacket;
99
100 if (Callback == NULL) {
101 return EFI_INVALID_PARAMETER;
102 }
103
104 Hob.Raw = GetFirstGuidHob (&gStatusCodeCallbackGuid);
105 FreePacket = NULL;
106 while (Hob.Raw != NULL) {
107 NumberOfEntries = GET_GUID_HOB_DATA (Hob);
108 CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (NumberOfEntries + 1);
109 if (*NumberOfEntries < 64) {
110 FreePacket = NumberOfEntries;
111 }
112 for (Index = 0; Index < *NumberOfEntries; Index++) {
113 if (CallbackEntry[Index] == Callback) {
114 //
115 // If the function was already registered. It can't be registered again.
116 //
117 return EFI_ALREADY_STARTED;
118 }
119 }
120 Hob.Raw = GET_NEXT_HOB (Hob);
121 Hob.Raw = GetNextGuidHob (&gStatusCodeCallbackGuid, Hob.Raw);
122 }
123
124 if (FreePacket == NULL) {
125 FreePacket = CreateRscHandlerCallbackPacket();
126 }
127
128 CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (FreePacket + 1);
129 CallbackEntry[*FreePacket] = Callback;
130 *FreePacket += 1;
131
132 return EFI_SUCCESS;
133 }
134
135 /**
136 Remove a previously registered callback function from the notification list.
137
138 ReportStatusCode() messages will no longer be forwarded to the Callback function.
139
140 @param[in] Callback A pointer to a function of type EFI_PEI_RSC_HANDLER_CALLBACK that is to be
141 unregistered.
142
143 @retval EFI_SUCCESS The function was successfully unregistered.
144 @retval EFI_INVALID_PARAMETER The callback function was NULL.
145 @retval EFI_NOT_FOUND The callback function was not found to be unregistered.
146
147 **/
148 EFI_STATUS
149 EFIAPI
150 Unregister (
151 IN EFI_PEI_RSC_HANDLER_CALLBACK Callback
152 )
153 {
154 EFI_PEI_HOB_POINTERS Hob;
155 EFI_PEI_RSC_HANDLER_CALLBACK *CallbackEntry;
156 UINTN *NumberOfEntries;
157 UINTN Index;
158
159 if (Callback == NULL) {
160 return EFI_INVALID_PARAMETER;
161 }
162
163 Hob.Raw = GetFirstGuidHob (&gStatusCodeCallbackGuid);
164 while (Hob.Raw != NULL) {
165 NumberOfEntries = GET_GUID_HOB_DATA (Hob);
166 CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (NumberOfEntries + 1);
167 for (Index = 0; Index < *NumberOfEntries; Index++) {
168 if (CallbackEntry[Index] == Callback) {
169 CallbackEntry[Index] = CallbackEntry[*NumberOfEntries - 1];
170 *NumberOfEntries -= 1;
171 return EFI_SUCCESS;
172 }
173 }
174 Hob.Raw = GET_NEXT_HOB (Hob);
175 Hob.Raw = GetNextGuidHob (&gStatusCodeCallbackGuid, Hob.Raw);
176 }
177
178 return EFI_NOT_FOUND;
179 }
180
181 /**
182 Publishes an interface that allows PEIMs to report status codes.
183
184 This function implements EFI_PEI_PROGRESS_CODE_PPI.ReportStatusCode().
185 It publishes an interface that allows PEIMs to report status codes.
186
187 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
188 @param CodeType Indicates the type of status code being reported.
189 @param Value Describes the current status of a hardware or
190 software entity. This includes information about the class and
191 subclass that is used to classify the entity as well as an operation.
192 For progress codes, the operation is the current activity.
193 For error codes, it is the exception.For debug codes,it is not defined at this time.
194 @param Instance The enumeration of a hardware or software entity within
195 the system. A system may contain multiple entities that match a class/subclass
196 pairing. The instance differentiates between them. An instance of 0 indicates
197 that instance information is unavailable, not meaningful, or not relevant.
198 Valid instance numbers start with 1.
199 @param CallerId This optional parameter may be used to identify the caller.
200 This parameter allows the status code driver to apply different rules to
201 different callers.
202 @param Data This optional parameter may be used to pass additional data.
203
204 @retval EFI_SUCCESS The function completed successfully.
205
206 **/
207 EFI_STATUS
208 EFIAPI
209 ReportDispatcher (
210 IN CONST EFI_PEI_SERVICES **PeiServices,
211 IN EFI_STATUS_CODE_TYPE CodeType,
212 IN EFI_STATUS_CODE_VALUE Value,
213 IN UINT32 Instance,
214 IN CONST EFI_GUID *CallerId OPTIONAL,
215 IN CONST EFI_STATUS_CODE_DATA *Data OPTIONAL
216 )
217 {
218 EFI_PEI_HOB_POINTERS Hob;
219 EFI_PEI_RSC_HANDLER_CALLBACK *CallbackEntry;
220 UINTN *NumberOfEntries;
221 UINTN Index;
222
223 Hob.Raw = GetFirstGuidHob (&gStatusCodeCallbackGuid);
224 while (Hob.Raw != NULL) {
225 NumberOfEntries = GET_GUID_HOB_DATA (Hob);
226 CallbackEntry = (EFI_PEI_RSC_HANDLER_CALLBACK *) (NumberOfEntries + 1);
227 for (Index = 0; Index < *NumberOfEntries; Index++) {
228 CallbackEntry[Index](
229 PeiServices,
230 CodeType,
231 Value,
232 Instance,
233 CallerId,
234 Data
235 );
236 }
237 Hob.Raw = GET_NEXT_HOB (Hob);
238 Hob.Raw = GetNextGuidHob (&gStatusCodeCallbackGuid, Hob.Raw);
239 }
240
241 return EFI_SUCCESS;
242 }
243
244 /**
245 Entry point of Status Code PEIM.
246
247 This function is the entry point of this Status Code Router PEIM.
248 It produces Report Stataus Code Handler PPI and Status Code PPI.
249
250 @param FileHandle Handle of the file being invoked.
251 @param PeiServices Describes the list of possible PEI Services.
252
253 @retval EFI_SUCESS The entry point of DXE IPL PEIM executes successfully.
254
255 **/
256 EFI_STATUS
257 EFIAPI
258 GenericStatusCodePeiEntry (
259 IN EFI_PEI_FILE_HANDLE FileHandle,
260 IN CONST EFI_PEI_SERVICES **PeiServices
261 )
262 {
263 EFI_STATUS Status;
264 EFI_PEI_PPI_DESCRIPTOR *OldDescriptor;
265 EFI_PEI_PROGRESS_CODE_PPI *OldStatusCodePpi;
266
267 CreateRscHandlerCallbackPacket ();
268
269 //
270 // Install Report Status Code Handler PPI
271 //
272 Status = PeiServicesInstallPpi (mRscHandlerPpiList);
273 ASSERT_EFI_ERROR (Status);
274
275 //
276 // Install Status Code PPI. PI spec specifies that there can be only one instance
277 // of this PPI in system. So first check if other instance already exists.
278 // If no other instance exists, then just install the PPI.
279 // If other instance already exists, then reinstall it.
280 //
281 Status = PeiServicesLocatePpi (
282 &gEfiPeiStatusCodePpiGuid,
283 0,
284 &OldDescriptor,
285 (VOID **) &OldStatusCodePpi
286 );
287 if (!EFI_ERROR (Status)) {
288 Status = PeiServicesReInstallPpi (OldDescriptor, mStatusCodePpiList);
289 } else {
290 Status = PeiServicesInstallPpi (mStatusCodePpiList);
291 }
292 ASSERT_EFI_ERROR (Status);
293
294 return EFI_SUCCESS;
295 }