2 Report Status Code Library for DXE Phase.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Library/ReportStatusCodeLib.h>
10 #include <Library/BaseLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/PcdLib.h>
15 #include <Library/DevicePathLib.h>
17 #include <Protocol/StatusCode.h>
19 #include <Guid/StatusCodeDataTypeId.h>
20 #include <Guid/StatusCodeDataTypeDebug.h>
23 // Define the maximum extended data size that is supported when a status code is
24 // reported at TPL_HIGH_LEVEL.
26 #define MAX_EXTENDED_DATA_SIZE 0x200
28 EFI_STATUS_CODE_PROTOCOL
*mReportStatusCodeLibStatusCodeProtocol
= NULL
;
31 Locate the report status code service.
33 Retrieve ReportStatusCode() API of Report Status Code Protocol.
37 InternalGetReportStatusCode (
43 if (mReportStatusCodeLibStatusCodeProtocol
!= NULL
) {
48 // Check gBS just in case ReportStatusCode is called before gBS is initialized.
50 if (gBS
!= NULL
&& gBS
->LocateProtocol
!= NULL
) {
51 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**) &mReportStatusCodeLibStatusCodeProtocol
);
52 if (EFI_ERROR (Status
)) {
53 mReportStatusCodeLibStatusCodeProtocol
= NULL
;
59 Internal worker function that reports a status code through the Report Status Code Protocol.
61 If status code service is not cached, then this function checks if Report Status Code
62 Protocol is available in system. If Report Status Code Protocol is not available, then
63 EFI_UNSUPPORTED is returned. If Report Status Code Protocol is present, then it is
64 cached in mReportStatusCodeLibStatusCodeProtocol. Finally this function reports status
65 code through the Report Status Code Protocol.
67 @param Type Status code type.
68 @param Value Status code value.
69 @param Instance Status code instance number.
70 @param CallerId Pointer to a GUID that identifies the caller of this
71 function. This is an optional parameter that may be
73 @param Data Pointer to the extended data buffer. This is an
74 optional parameter that may be NULL.
76 @retval EFI_SUCCESS The status code was reported.
77 @retval EFI_UNSUPPORTED Report Status Code Protocol is not available.
78 @retval EFI_UNSUPPORTED Status code type is not supported.
82 InternalReportStatusCode (
83 IN EFI_STATUS_CODE_TYPE Type
,
84 IN EFI_STATUS_CODE_VALUE Value
,
86 IN CONST EFI_GUID
*CallerId OPTIONAL
,
87 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
90 if ((ReportProgressCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
91 (ReportErrorCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ||
92 (ReportDebugCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
)) {
94 // If mReportStatusCodeLibStatusCodeProtocol is NULL, then check if Report Status Code Protocol is available in system.
96 InternalGetReportStatusCode ();
97 if (mReportStatusCodeLibStatusCodeProtocol
== NULL
) {
98 return EFI_UNSUPPORTED
;
102 // A Report Status Code Protocol is present in system, so pass in all the parameters to the service.
104 return mReportStatusCodeLibStatusCodeProtocol
->ReportStatusCode (Type
, Value
, Instance
, (EFI_GUID
*)CallerId
, Data
);
107 return EFI_UNSUPPORTED
;
112 Converts a status code to an 8-bit POST code value.
114 Converts the status code specified by CodeType and Value to an 8-bit POST code
115 and returns the 8-bit POST code in PostCode. If CodeType is an
116 EFI_PROGRESS_CODE or CodeType is an EFI_ERROR_CODE, then bits 0..4 of PostCode
117 are set to bits 16..20 of Value, and bits 5..7 of PostCode are set to bits
118 24..26 of Value., and TRUE is returned. Otherwise, FALSE is returned.
120 If PostCode is NULL, then ASSERT().
122 @param CodeType The type of status code being converted.
123 @param Value The status code value being converted.
124 @param PostCode A pointer to the 8-bit POST code value to return.
126 @retval TRUE The status code specified by CodeType and Value was converted
127 to an 8-bit POST code and returned in PostCode.
128 @retval FALSE The status code specified by CodeType and Value could not be
129 converted to an 8-bit POST code value.
135 IN EFI_STATUS_CODE_TYPE CodeType
,
136 IN EFI_STATUS_CODE_VALUE Value
,
141 // If PostCode is NULL, then ASSERT()
143 ASSERT (PostCode
!= NULL
);
146 // Convert Value to an 8 bit post code
148 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
149 ((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ) {
150 *PostCode
= (UINT8
) ((((Value
& EFI_STATUS_CODE_CLASS_MASK
) >> 24) << 5) |
151 (((Value
& EFI_STATUS_CODE_SUBCLASS_MASK
) >> 16) & 0x1f));
159 Extracts ASSERT() information from a status code structure.
161 Converts the status code specified by CodeType, Value, and Data to the ASSERT()
162 arguments specified by Filename, Description, and LineNumber. If CodeType is
163 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
164 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
165 Filename, Description, and LineNumber from the optional data area of the
166 status code buffer specified by Data. The optional data area of Data contains
167 a Null-terminated ASCII string for the FileName, followed by a Null-terminated
168 ASCII string for the Description, followed by a 32-bit LineNumber. If the
169 ASSERT() information could be extracted from Data, then return TRUE.
170 Otherwise, FALSE is returned.
172 If Data is NULL, then ASSERT().
173 If Filename is NULL, then ASSERT().
174 If Description is NULL, then ASSERT().
175 If LineNumber is NULL, then ASSERT().
177 @param CodeType The type of status code being converted.
178 @param Value The status code value being converted.
179 @param Data Pointer to status code data buffer.
180 @param Filename Pointer to the source file name that generated the ASSERT().
181 @param Description Pointer to the description of the ASSERT().
182 @param LineNumber Pointer to source line number that generated the ASSERT().
184 @retval TRUE The status code specified by CodeType, Value, and Data was
185 converted ASSERT() arguments specified by Filename, Description,
187 @retval FALSE The status code specified by CodeType, Value, and Data could
188 not be converted to ASSERT() arguments.
193 ReportStatusCodeExtractAssertInfo (
194 IN EFI_STATUS_CODE_TYPE CodeType
,
195 IN EFI_STATUS_CODE_VALUE Value
,
196 IN CONST EFI_STATUS_CODE_DATA
*Data
,
197 OUT CHAR8
**Filename
,
198 OUT CHAR8
**Description
,
199 OUT UINT32
*LineNumber
202 EFI_DEBUG_ASSERT_DATA
*AssertData
;
204 ASSERT (Data
!= NULL
);
205 ASSERT (Filename
!= NULL
);
206 ASSERT (Description
!= NULL
);
207 ASSERT (LineNumber
!= NULL
);
209 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) &&
210 ((CodeType
& EFI_STATUS_CODE_SEVERITY_MASK
) == EFI_ERROR_UNRECOVERED
) &&
211 ((Value
& EFI_STATUS_CODE_OPERATION_MASK
) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)) {
212 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)(Data
+ 1);
213 *Filename
= (CHAR8
*)(AssertData
+ 1);
214 *Description
= *Filename
+ AsciiStrLen (*Filename
) + 1;
215 *LineNumber
= AssertData
->LineNumber
;
223 Extracts DEBUG() information from a status code structure.
225 Converts the status code specified by Data to the DEBUG() arguments specified
226 by ErrorLevel, Marker, and Format. If type GUID in Data is
227 EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
228 Format from the optional data area of the status code buffer specified by Data.
229 The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
230 which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
231 the Format. If the DEBUG() information could be extracted from Data, then
232 return TRUE. Otherwise, FALSE is returned.
234 If Data is NULL, then ASSERT().
235 If ErrorLevel is NULL, then ASSERT().
236 If Marker is NULL, then ASSERT().
237 If Format is NULL, then ASSERT().
239 @param Data Pointer to status code data buffer.
240 @param ErrorLevel Pointer to error level mask for a debug message.
241 @param Marker Pointer to the variable argument list associated with Format.
242 @param Format Pointer to a Null-terminated ASCII format string of a
245 @retval TRUE The status code specified by Data was converted DEBUG() arguments
246 specified by ErrorLevel, Marker, and Format.
247 @retval FALSE The status code specified by Data could not be converted to
253 ReportStatusCodeExtractDebugInfo (
254 IN CONST EFI_STATUS_CODE_DATA
*Data
,
255 OUT UINT32
*ErrorLevel
,
256 OUT BASE_LIST
*Marker
,
260 EFI_DEBUG_INFO
*DebugInfo
;
262 ASSERT (Data
!= NULL
);
263 ASSERT (ErrorLevel
!= NULL
);
264 ASSERT (Marker
!= NULL
);
265 ASSERT (Format
!= NULL
);
268 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
270 if (!CompareGuid (&Data
->Type
, &gEfiStatusCodeDataTypeDebugGuid
)) {
275 // Retrieve the debug information from the status code record
277 DebugInfo
= (EFI_DEBUG_INFO
*)(Data
+ 1);
279 *ErrorLevel
= DebugInfo
->ErrorLevel
;
282 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
283 // of format in DEBUG string. Its address is returned in Marker and has to be 64-bit aligned.
284 // It must be noticed that EFI_DEBUG_INFO follows EFI_STATUS_CODE_DATA, whose size is
285 // 20 bytes. The size of EFI_DEBUG_INFO is 4 bytes, so we can ensure that Marker
286 // returned is 64-bit aligned.
287 // 64-bit aligned is a must, otherwise retrieving 64-bit parameter from BASE_LIST will
288 // cause unalignment exception.
290 *Marker
= (BASE_LIST
) (DebugInfo
+ 1);
291 *Format
= (CHAR8
*)(((UINT64
*)*Marker
) + 12);
298 Reports a status code.
300 Reports the status code specified by the parameters Type and Value. Status
301 code also require an instance, caller ID, and extended data. This function
302 passed in a zero instance, NULL extended data, and a caller ID of
303 gEfiCallerIdGuid, which is the GUID for the module.
305 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
306 is called while processing another any other Report Status Code Library function,
307 then ReportStatusCode() must return immediately.
309 @param Type Status code type.
310 @param Value Status code value.
312 @retval EFI_SUCCESS The status code was reported.
313 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
315 @retval EFI_UNSUPPORTED Report status code is not supported
321 IN EFI_STATUS_CODE_TYPE Type
,
322 IN EFI_STATUS_CODE_VALUE Value
325 return InternalReportStatusCode (Type
, Value
, 0, &gEfiCallerIdGuid
, NULL
);
330 Reports a status code with a Device Path Protocol as the extended data.
332 Allocates and fills in the extended data section of a status code with the
333 Device Path Protocol specified by DevicePath. This function is responsible
334 for allocating a buffer large enough for the standard header and the device
335 path. The standard header is filled in with a GUID of
336 gEfiStatusCodeSpecificDataGuid. The status code is reported with a zero
337 instance and a caller ID of gEfiCallerIdGuid.
339 ReportStatusCodeWithDevicePath()must actively prevent recursion. If
340 ReportStatusCodeWithDevicePath() is called while processing another any other
341 Report Status Code Library function, then ReportStatusCodeWithDevicePath()
342 must return EFI_DEVICE_ERROR immediately.
344 If DevicePath is NULL, then ASSERT().
346 @param Type Status code type.
347 @param Value Status code value.
348 @param DevicePath Pointer to the Device Path Protocol to be reported.
350 @retval EFI_SUCCESS The status code was reported with the extended
351 data specified by DevicePath.
352 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
353 extended data section.
354 @retval EFI_UNSUPPORTED Report status code is not supported
359 ReportStatusCodeWithDevicePath (
360 IN EFI_STATUS_CODE_TYPE Type
,
361 IN EFI_STATUS_CODE_VALUE Value
,
362 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
365 ASSERT (DevicePath
!= NULL
);
366 return ReportStatusCodeWithExtendedData (
370 GetDevicePathSize (DevicePath
)
376 Reports a status code with an extended data buffer.
378 Allocates and fills in the extended data section of a status code with the
379 extended data specified by ExtendedData and ExtendedDataSize. ExtendedData
380 is assumed to be one of the data structures specified in Related Definitions.
381 These data structure do not have the standard header, so this function is
382 responsible for allocating a buffer large enough for the standard header and
383 the extended data passed into this function. The standard header is filled
384 in with a GUID of gEfiStatusCodeSpecificDataGuid. The status code is reported
385 with a zero instance and a caller ID of gEfiCallerIdGuid.
387 ReportStatusCodeWithExtendedData()must actively prevent recursion. If
388 ReportStatusCodeWithExtendedData() is called while processing another any other
389 Report Status Code Library function, then ReportStatusCodeWithExtendedData()
390 must return EFI_DEVICE_ERROR immediately.
392 If ExtendedData is NULL, then ASSERT().
393 If ExtendedDataSize is 0, then ASSERT().
395 @param Type Status code type.
396 @param Value Status code value.
397 @param ExtendedData Pointer to the extended data buffer to be reported.
398 @param ExtendedDataSize The size, in bytes, of the extended data buffer to
401 @retval EFI_SUCCESS The status code was reported with the extended
402 data specified by ExtendedData and ExtendedDataSize.
403 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
404 extended data section.
405 @retval EFI_UNSUPPORTED Report status code is not supported
410 ReportStatusCodeWithExtendedData (
411 IN EFI_STATUS_CODE_TYPE Type
,
412 IN EFI_STATUS_CODE_VALUE Value
,
413 IN CONST VOID
*ExtendedData
,
414 IN UINTN ExtendedDataSize
417 ASSERT (ExtendedData
!= NULL
);
418 ASSERT (ExtendedDataSize
!= 0);
419 return ReportStatusCodeEx (
432 Reports a status code with full parameters.
434 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
435 is 0, then an extended data buffer is not reported. If ExtendedData is not
436 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
437 ExtendedData is assumed not have the standard status code header, so this function
438 is responsible for allocating a buffer large enough for the standard header and
439 the extended data passed into this function. The standard header is filled in
440 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
441 GUID of gEfiStatusCodeSpecificDataGuid is used. The status code is reported with
442 an instance specified by Instance and a caller ID specified by CallerId. If
443 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
445 ReportStatusCodeEx()must actively prevent recursion. If
446 ReportStatusCodeEx() is called while processing another any
447 other Report Status Code Library function, then
448 ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
450 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
451 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
453 @param Type Status code type.
454 @param Value Status code value.
455 @param Instance Status code instance number.
456 @param CallerId Pointer to a GUID that identifies the caller of this
457 function. If this parameter is NULL, then a caller
458 ID of gEfiCallerIdGuid is used.
459 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
460 If this parameter is NULL, then a the status code
461 standard header is filled in with
462 gEfiStatusCodeSpecificDataGuid.
463 @param ExtendedData Pointer to the extended data buffer. This is an
464 optional parameter that may be NULL.
465 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
467 @retval EFI_SUCCESS The status code was reported.
468 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
469 the extended data section if it was specified.
470 @retval EFI_UNSUPPORTED Report status code is not supported
476 IN EFI_STATUS_CODE_TYPE Type
,
477 IN EFI_STATUS_CODE_VALUE Value
,
479 IN CONST EFI_GUID
*CallerId OPTIONAL
,
480 IN CONST EFI_GUID
*ExtendedDataGuid OPTIONAL
,
481 IN CONST VOID
*ExtendedData OPTIONAL
,
482 IN UINTN ExtendedDataSize
486 EFI_STATUS_CODE_DATA
*StatusCodeData
;
488 UINT64 Buffer
[(MAX_EXTENDED_DATA_SIZE
/ sizeof (UINT64
)) + 1];
490 ASSERT (!((ExtendedData
== NULL
) && (ExtendedDataSize
!= 0)));
491 ASSERT (!((ExtendedData
!= NULL
) && (ExtendedDataSize
== 0)));
493 if (ExtendedDataSize
<= (MAX_EXTENDED_DATA_SIZE
- sizeof (EFI_STATUS_CODE_DATA
))) {
495 // Use Buffer instead of allocating if possible.
497 StatusCodeData
= (EFI_STATUS_CODE_DATA
*)Buffer
;
499 if (gBS
== NULL
|| gBS
->AllocatePool
== NULL
|| gBS
->FreePool
== NULL
) {
500 return EFI_UNSUPPORTED
;
504 // Retrieve the current TPL
506 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
507 gBS
->RestoreTPL (Tpl
);
509 if (Tpl
> TPL_NOTIFY
) {
510 return EFI_OUT_OF_RESOURCES
;
514 // Allocate space for the Status Code Header and its buffer
516 StatusCodeData
= NULL
;
517 gBS
->AllocatePool (EfiBootServicesData
, sizeof (EFI_STATUS_CODE_DATA
) + ExtendedDataSize
, (VOID
**)&StatusCodeData
);
518 if (StatusCodeData
== NULL
) {
519 return EFI_OUT_OF_RESOURCES
;
524 // Fill in the extended data header
526 StatusCodeData
->HeaderSize
= (UINT16
) sizeof (EFI_STATUS_CODE_DATA
);
527 StatusCodeData
->Size
= (UINT16
) ExtendedDataSize
;
528 if (ExtendedDataGuid
== NULL
) {
529 ExtendedDataGuid
= &gEfiStatusCodeSpecificDataGuid
;
531 CopyGuid (&StatusCodeData
->Type
, ExtendedDataGuid
);
534 // Fill in the extended data buffer
536 if (ExtendedData
!= NULL
) {
537 CopyMem (StatusCodeData
+ 1, ExtendedData
, ExtendedDataSize
);
541 // Report the status code
543 if (CallerId
== NULL
) {
544 CallerId
= &gEfiCallerIdGuid
;
546 Status
= InternalReportStatusCode (Type
, Value
, Instance
, CallerId
, StatusCodeData
);
549 // Free the allocated buffer
551 if (StatusCodeData
!= (EFI_STATUS_CODE_DATA
*)Buffer
) {
552 gBS
->FreePool (StatusCodeData
);
560 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
562 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
563 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
565 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
566 PcdReportStatusCodeProperyMask is set.
567 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
568 PcdReportStatusCodeProperyMask is clear.
573 ReportProgressCodeEnabled (
577 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
) != 0);
582 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
584 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
585 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
587 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
588 PcdReportStatusCodeProperyMask is set.
589 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
590 PcdReportStatusCodeProperyMask is clear.
595 ReportErrorCodeEnabled (
599 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
) != 0);
604 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
606 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
607 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
609 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
610 PcdReportStatusCodeProperyMask is set.
611 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
612 PcdReportStatusCodeProperyMask is clear.
617 ReportDebugCodeEnabled (
621 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
) != 0);