]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/PeiReportStatusCodeLib/ReportStatusCodeLib.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Library / PeiReportStatusCodeLib / ReportStatusCodeLib.c
1 /** @file
2 Instance of Report Status Code Library for PEI Phase.
3
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Guid/StatusCodeDataTypeId.h>
10 #include <Guid/StatusCodeDataTypeDebug.h>
11
12 #include <Library/ReportStatusCodeLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/PeiServicesTablePointerLib.h>
17 #include <Library/OemHookStatusCodeLib.h>
18 #include <Library/PcdLib.h>
19
20 //
21 // Define the maximum extended data size that is supported in the PEI phase
22 //
23 #define MAX_EXTENDED_DATA_SIZE 0x200
24
25 /**
26 Internal worker function that reports a status code through the PEI Status Code Service or
27 OEM Hook Status Code Library.
28
29 This function first tries to report status code via PEI Status Code Service. If the service
30 is not available, it then tries calling OEM Hook Status Code Library.
31
32 @param Type Status code type.
33 @param Value Status code value.
34 @param Instance Status code instance number.
35 @param CallerId Pointer to a GUID that identifies the caller of this
36 function. This is an optional parameter that may be
37 NULL.
38 @param Data Pointer to the extended data buffer. This is an
39 optional parameter that may be NULL.
40
41 @retval EFI_SUCCESS The status code was reported.
42 @retval EFI_UNSUPPORTED Status code type is not supported.
43 @retval Others Failed to report status code.
44
45 **/
46 EFI_STATUS
47 InternalReportStatusCode (
48 IN EFI_STATUS_CODE_TYPE Type,
49 IN EFI_STATUS_CODE_VALUE Value,
50 IN UINT32 Instance,
51 IN CONST EFI_GUID *CallerId OPTIONAL,
52 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
53 )
54 {
55 CONST EFI_PEI_SERVICES **PeiServices;
56 EFI_STATUS Status;
57
58 if ((ReportProgressCodeEnabled() && ((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) ||
59 (ReportErrorCodeEnabled() && ((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) ||
60 (ReportDebugCodeEnabled() && ((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE)) {
61 PeiServices = GetPeiServicesTablePointer ();
62 Status = (*PeiServices)->ReportStatusCode (
63 PeiServices,
64 Type,
65 Value,
66 Instance,
67 (EFI_GUID *)CallerId,
68 Data
69 );
70 if (Status == EFI_NOT_AVAILABLE_YET) {
71 Status = OemHookStatusCodeInitialize ();
72 if (!EFI_ERROR (Status)) {
73 return OemHookStatusCodeReport (Type, Value, Instance, (EFI_GUID *) CallerId, Data);
74 }
75 }
76 return Status;
77 }
78
79 return EFI_UNSUPPORTED;
80 }
81
82
83 /**
84 Converts a status code to an 8-bit POST code value.
85
86 Converts the status code specified by CodeType and Value to an 8-bit POST code
87 and returns the 8-bit POST code in PostCode. If CodeType is an
88 EFI_PROGRESS_CODE or CodeType is an EFI_ERROR_CODE, then bits 0..4 of PostCode
89 are set to bits 16..20 of Value, and bits 5..7 of PostCode are set to bits
90 24..26 of Value., and TRUE is returned. Otherwise, FALSE is returned.
91
92 If PostCode is NULL, then ASSERT().
93
94 @param CodeType The type of status code being converted.
95 @param Value The status code value being converted.
96 @param PostCode A pointer to the 8-bit POST code value to return.
97
98 @retval TRUE The status code specified by CodeType and Value was converted
99 to an 8-bit POST code and returned in PostCode.
100 @retval FALSE The status code specified by CodeType and Value could not be
101 converted to an 8-bit POST code value.
102
103 **/
104 BOOLEAN
105 EFIAPI
106 CodeTypeToPostCode (
107 IN EFI_STATUS_CODE_TYPE CodeType,
108 IN EFI_STATUS_CODE_VALUE Value,
109 OUT UINT8 *PostCode
110 )
111 {
112 //
113 // If PostCode is NULL, then ASSERT()
114 //
115 ASSERT (PostCode != NULL);
116
117 //
118 // Convert Value to an 8 bit post code
119 //
120 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) ||
121 ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE)) {
122 *PostCode = (UINT8) ((((Value & EFI_STATUS_CODE_CLASS_MASK) >> 24) << 5) |
123 (((Value & EFI_STATUS_CODE_SUBCLASS_MASK) >> 16) & 0x1f));
124 return TRUE;
125 }
126 return FALSE;
127 }
128
129
130 /**
131 Extracts ASSERT() information from a status code structure.
132
133 Converts the status code specified by CodeType, Value, and Data to the ASSERT()
134 arguments specified by Filename, Description, and LineNumber. If CodeType is
135 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
136 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
137 Filename, Description, and LineNumber from the optional data area of the
138 status code buffer specified by Data. The optional data area of Data contains
139 a Null-terminated ASCII string for the FileName, followed by a Null-terminated
140 ASCII string for the Description, followed by a 32-bit LineNumber. If the
141 ASSERT() information could be extracted from Data, then return TRUE.
142 Otherwise, FALSE is returned.
143
144 If Data is NULL, then ASSERT().
145 If Filename is NULL, then ASSERT().
146 If Description is NULL, then ASSERT().
147 If LineNumber is NULL, then ASSERT().
148
149 @param CodeType The type of status code being converted.
150 @param Value The status code value being converted.
151 @param Data Pointer to status code data buffer.
152 @param Filename Pointer to the source file name that generated the ASSERT().
153 @param Description Pointer to the description of the ASSERT().
154 @param LineNumber Pointer to source line number that generated the ASSERT().
155
156 @retval TRUE The status code specified by CodeType, Value, and Data was
157 converted ASSERT() arguments specified by Filename, Description,
158 and LineNumber.
159 @retval FALSE The status code specified by CodeType, Value, and Data could
160 not be converted to ASSERT() arguments.
161
162 **/
163 BOOLEAN
164 EFIAPI
165 ReportStatusCodeExtractAssertInfo (
166 IN EFI_STATUS_CODE_TYPE CodeType,
167 IN EFI_STATUS_CODE_VALUE Value,
168 IN CONST EFI_STATUS_CODE_DATA *Data,
169 OUT CHAR8 **Filename,
170 OUT CHAR8 **Description,
171 OUT UINT32 *LineNumber
172 )
173 {
174 EFI_DEBUG_ASSERT_DATA *AssertData;
175
176 ASSERT (Data != NULL);
177 ASSERT (Filename != NULL);
178 ASSERT (Description != NULL);
179 ASSERT (LineNumber != NULL);
180
181 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) &&
182 ((CodeType & EFI_STATUS_CODE_SEVERITY_MASK) == EFI_ERROR_UNRECOVERED) &&
183 ((Value & EFI_STATUS_CODE_OPERATION_MASK) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)) {
184 AssertData = (EFI_DEBUG_ASSERT_DATA *)(Data + 1);
185 *Filename = (CHAR8 *)(AssertData + 1);
186 *Description = *Filename + AsciiStrLen (*Filename) + 1;
187 *LineNumber = AssertData->LineNumber;
188 return TRUE;
189 }
190 return FALSE;
191 }
192
193
194 /**
195 Extracts DEBUG() information from a status code structure.
196
197 Converts the status code specified by Data to the DEBUG() arguments specified
198 by ErrorLevel, Marker, and Format. If type GUID in Data is
199 EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
200 Format from the optional data area of the status code buffer specified by Data.
201 The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
202 which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
203 the Format. If the DEBUG() information could be extracted from Data, then
204 return TRUE. Otherwise, FALSE is returned.
205
206 If Data is NULL, then ASSERT().
207 If ErrorLevel is NULL, then ASSERT().
208 If Marker is NULL, then ASSERT().
209 If Format is NULL, then ASSERT().
210
211 @param Data Pointer to status code data buffer.
212 @param ErrorLevel Pointer to error level mask for a debug message.
213 @param Marker Pointer to the variable argument list associated with Format.
214 @param Format Pointer to a Null-terminated ASCII format string of a
215 debug message.
216
217 @retval TRUE The status code specified by Data was converted DEBUG() arguments
218 specified by ErrorLevel, Marker, and Format.
219 @retval FALSE The status code specified by Data could not be converted to
220 DEBUG() arguments.
221
222 **/
223 BOOLEAN
224 EFIAPI
225 ReportStatusCodeExtractDebugInfo (
226 IN CONST EFI_STATUS_CODE_DATA *Data,
227 OUT UINT32 *ErrorLevel,
228 OUT BASE_LIST *Marker,
229 OUT CHAR8 **Format
230 )
231 {
232 EFI_DEBUG_INFO *DebugInfo;
233
234 ASSERT (Data != NULL);
235 ASSERT (ErrorLevel != NULL);
236 ASSERT (Marker != NULL);
237 ASSERT (Format != NULL);
238
239 //
240 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
241 //
242 if (!CompareGuid (&Data->Type, &gEfiStatusCodeDataTypeDebugGuid)) {
243 return FALSE;
244 }
245
246 //
247 // Retrieve the debug information from the status code record
248 //
249 DebugInfo = (EFI_DEBUG_INFO *)(Data + 1);
250
251 *ErrorLevel = DebugInfo->ErrorLevel;
252
253 //
254 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
255 // of format in DEBUG string. Its address is returned in Marker and has to be 64-bit aligned.
256 // It must be noticed that EFI_DEBUG_INFO follows EFI_STATUS_CODE_DATA, whose size is
257 // 20 bytes. The size of EFI_DEBUG_INFO is 4 bytes, so we can ensure that Marker
258 // returned is 64-bit aligned.
259 // 64-bit aligned is a must, otherwise retrieving 64-bit parameter from BASE_LIST will
260 // cause unalignment exception.
261 //
262 *Marker = (BASE_LIST) (DebugInfo + 1);
263 *Format = (CHAR8 *)(((UINT64 *)*Marker) + 12);
264
265 return TRUE;
266 }
267
268
269 /**
270 Reports a status code.
271
272 Reports the status code specified by the parameters Type and Value. Status
273 code also require an instance, caller ID, and extended data. This function
274 passed in a zero instance, NULL extended data, and a caller ID of
275 gEfiCallerIdGuid, which is the GUID for the module.
276
277 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
278 is called while processing another any other Report Status Code Library function,
279 then ReportStatusCode() must return immediately.
280
281 @param Type Status code type.
282 @param Value Status code value.
283
284 @retval EFI_SUCCESS The status code was reported.
285 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
286 device error.
287 @retval EFI_UNSUPPORTED Report status code is not supported
288
289 **/
290 EFI_STATUS
291 EFIAPI
292 ReportStatusCode (
293 IN EFI_STATUS_CODE_TYPE Type,
294 IN EFI_STATUS_CODE_VALUE Value
295 )
296 {
297 return InternalReportStatusCode (Type, Value, 0, &gEfiCallerIdGuid, NULL);
298 }
299
300
301 /**
302 Reports a status code with a Device Path Protocol as the extended data.
303
304 Allocates and fills in the extended data section of a status code with the
305 Device Path Protocol specified by DevicePath. This function is responsible
306 for allocating a buffer large enough for the standard header and the device
307 path. The standard header is filled in with a GUID of
308 gEfiStatusCodeSpecificDataGuid. The status code is reported with a zero
309 instance and a caller ID of gEfiCallerIdGuid.
310
311 ReportStatusCodeWithDevicePath()must actively prevent recursion. If
312 ReportStatusCodeWithDevicePath() is called while processing another any other
313 Report Status Code Library function, then ReportStatusCodeWithDevicePath()
314 must return EFI_DEVICE_ERROR immediately.
315
316 If DevicePath is NULL, then ASSERT().
317
318 @param Type Status code type.
319 @param Value Status code value.
320 @param DevicePath Pointer to the Device Path Protocol to be reported.
321
322 @retval EFI_SUCCESS The status code was reported with the extended
323 data specified by DevicePath.
324 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
325 extended data section.
326 @retval EFI_UNSUPPORTED Report status code is not supported
327
328 **/
329 EFI_STATUS
330 EFIAPI
331 ReportStatusCodeWithDevicePath (
332 IN EFI_STATUS_CODE_TYPE Type,
333 IN EFI_STATUS_CODE_VALUE Value,
334 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
335 )
336 {
337 ASSERT (DevicePath != NULL);
338 //
339 // EFI_UNSUPPORTED is returned for device path is not supported in PEI phase.
340 //
341 return EFI_UNSUPPORTED;
342 }
343
344
345 /**
346 Reports a status code with an extended data buffer.
347
348 Allocates and fills in the extended data section of a status code with the
349 extended data specified by ExtendedData and ExtendedDataSize. ExtendedData
350 is assumed to be one of the data structures specified in Related Definitions.
351 These data structure do not have the standard header, so this function is
352 responsible for allocating a buffer large enough for the standard header and
353 the extended data passed into this function. The standard header is filled
354 in with a GUID of gEfiStatusCodeSpecificDataGuid. The status code is reported
355 with a zero instance and a caller ID of gEfiCallerIdGuid.
356
357 ReportStatusCodeWithExtendedData()must actively prevent recursion. If
358 ReportStatusCodeWithExtendedData() is called while processing another any other
359 Report Status Code Library function, then ReportStatusCodeWithExtendedData()
360 must return EFI_DEVICE_ERROR immediately.
361
362 If ExtendedData is NULL, then ASSERT().
363 If ExtendedDataSize is 0, then ASSERT().
364
365 @param Type Status code type.
366 @param Value Status code value.
367 @param ExtendedData Pointer to the extended data buffer to be reported.
368 @param ExtendedDataSize The size, in bytes, of the extended data buffer to
369 be reported.
370
371 @retval EFI_SUCCESS The status code was reported with the extended
372 data specified by ExtendedData and ExtendedDataSize.
373 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
374 extended data section.
375 @retval EFI_UNSUPPORTED Report status code is not supported
376
377 **/
378 EFI_STATUS
379 EFIAPI
380 ReportStatusCodeWithExtendedData (
381 IN EFI_STATUS_CODE_TYPE Type,
382 IN EFI_STATUS_CODE_VALUE Value,
383 IN CONST VOID *ExtendedData,
384 IN UINTN ExtendedDataSize
385 )
386 {
387 ASSERT (ExtendedData != NULL);
388 ASSERT (ExtendedDataSize != 0);
389 return ReportStatusCodeEx (
390 Type,
391 Value,
392 0,
393 NULL,
394 NULL,
395 ExtendedData,
396 ExtendedDataSize
397 );
398 }
399
400
401 /**
402 Reports a status code with full parameters.
403
404 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
405 is 0, then an extended data buffer is not reported. If ExtendedData is not
406 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
407 ExtendedData is assumed not have the standard status code header, so this function
408 is responsible for allocating a buffer large enough for the standard header and
409 the extended data passed into this function. The standard header is filled in
410 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
411 GUID of gEfiStatusCodeSpecificDatauid is used. The status code is reported with
412 an instance specified by Instance and a caller ID specified by CallerId. If
413 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
414
415 ReportStatusCodeEx()must actively prevent recursion. If ReportStatusCodeEx()
416 is called while processing another any other Report Status Code Library function,
417 then ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
418
419 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
420 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
421
422 @param Type Status code type.
423 @param Value Status code value.
424 @param Instance Status code instance number.
425 @param CallerId Pointer to a GUID that identifies the caller of this
426 function. If this parameter is NULL, then a caller
427 ID of gEfiCallerIdGuid is used.
428 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
429 If this parameter is NULL, then a the status code
430 standard header is filled in with
431 gEfiStatusCodeSpecificDataGuid.
432 @param ExtendedData Pointer to the extended data buffer. This is an
433 optional parameter that may be NULL.
434 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
435
436 @retval EFI_SUCCESS The status code was reported.
437 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
438 the extended data section if it was specified.
439 @retval EFI_UNSUPPORTED Report status code is not supported
440
441 **/
442 EFI_STATUS
443 EFIAPI
444 ReportStatusCodeEx (
445 IN EFI_STATUS_CODE_TYPE Type,
446 IN EFI_STATUS_CODE_VALUE Value,
447 IN UINT32 Instance,
448 IN CONST EFI_GUID *CallerId OPTIONAL,
449 IN CONST EFI_GUID *ExtendedDataGuid OPTIONAL,
450 IN CONST VOID *ExtendedData OPTIONAL,
451 IN UINTN ExtendedDataSize
452 )
453 {
454 EFI_STATUS_CODE_DATA *StatusCodeData;
455 UINT64 Buffer[(MAX_EXTENDED_DATA_SIZE / sizeof (UINT64)) + 1];
456
457 //
458 // If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
459 //
460 ASSERT (!((ExtendedData == NULL) && (ExtendedDataSize != 0)));
461 //
462 // If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
463 //
464 ASSERT (!((ExtendedData != NULL) && (ExtendedDataSize == 0)));
465
466 if (ExtendedDataSize > (MAX_EXTENDED_DATA_SIZE - sizeof (EFI_STATUS_CODE_DATA))) {
467 //
468 // The local variable Buffer not large enough to hold the extended data associated
469 // with the status code being reported.
470 //
471 DEBUG ((EFI_D_ERROR, "Status code extended data is too large to be reported!\n"));
472 return EFI_OUT_OF_RESOURCES;
473 }
474 StatusCodeData = (EFI_STATUS_CODE_DATA *) Buffer;
475 StatusCodeData->HeaderSize = (UINT16) sizeof (EFI_STATUS_CODE_DATA);
476 StatusCodeData->Size = (UINT16) ExtendedDataSize;
477 if (ExtendedDataGuid == NULL) {
478 ExtendedDataGuid = &gEfiStatusCodeSpecificDataGuid;
479 }
480 CopyGuid (&StatusCodeData->Type, ExtendedDataGuid);
481 if (ExtendedData != NULL) {
482 CopyMem (StatusCodeData + 1, ExtendedData, ExtendedDataSize);
483 }
484 if (CallerId == NULL) {
485 CallerId = &gEfiCallerIdGuid;
486 }
487 return InternalReportStatusCode (Type, Value, Instance, CallerId, StatusCodeData);
488 }
489
490
491 /**
492 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
493
494 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
495 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
496
497 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
498 PcdReportStatusCodeProperyMask is set.
499 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
500 PcdReportStatusCodeProperyMask is clear.
501
502 **/
503 BOOLEAN
504 EFIAPI
505 ReportProgressCodeEnabled (
506 VOID
507 )
508 {
509 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED) != 0);
510 }
511
512
513 /**
514 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
515
516 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
517 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
518
519 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
520 PcdReportStatusCodeProperyMask is set.
521 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
522 PcdReportStatusCodeProperyMask is clear.
523
524 **/
525 BOOLEAN
526 EFIAPI
527 ReportErrorCodeEnabled (
528 VOID
529 )
530 {
531 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED) != 0);
532 }
533
534
535 /**
536 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
537
538 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
539 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
540
541 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
542 PcdReportStatusCodeProperyMask is set.
543 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
544 PcdReportStatusCodeProperyMask is clear.
545
546 **/
547 BOOLEAN
548 EFIAPI
549 ReportDebugCodeEnabled (
550 VOID
551 )
552 {
553 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED) != 0);
554 }