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