2 Report Status Code Library for DXE Phase.
4 Copyright (c) 2006 - 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
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.
15 #include <Library/ReportStatusCodeLib.h>
16 #include <Library/BaseLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/DevicePathLib.h>
23 #include <Protocol/StatusCode.h>
25 #include <Guid/StatusCodeDataTypeId.h>
26 #include <Guid/StatusCodeDataTypeDebug.h>
29 // Define the maximum extended data size that is supported when a status code is
30 // reported at TPL_HIGH_LEVEL.
32 #define MAX_EXTENDED_DATA_SIZE 0x200
34 EFI_STATUS_CODE_PROTOCOL
*mReportStatusCodeLibStatusCodeProtocol
= NULL
;
37 Locate the report status code service.
39 Retrieve ReportStatusCode() API of Report Status Code Protocol.
43 InternalGetReportStatusCode (
49 if (mReportStatusCodeLibStatusCodeProtocol
!= NULL
) {
54 // Check gBS just in case ReportStatusCode is called before gBS is initialized.
56 if (gBS
!= NULL
&& gBS
->LocateProtocol
!= NULL
) {
57 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**) &mReportStatusCodeLibStatusCodeProtocol
);
58 if (EFI_ERROR (Status
)) {
59 mReportStatusCodeLibStatusCodeProtocol
= NULL
;
65 Internal worker function that reports a status code through the Report Status Code Protocol.
67 If status code service is not cached, then this function checks if Report Status Code
68 Protocol is available in system. If Report Status Code Protocol is not available, then
69 EFI_UNSUPPORTED is returned. If Report Status Code Protocol is present, then it is
70 cached in mReportStatusCodeLibStatusCodeProtocol. Finally this function reports status
71 code through the Report Status Code Protocol.
73 @param Type Status code type.
74 @param Value Status code value.
75 @param Instance Status code instance number.
76 @param CallerId Pointer to a GUID that identifies the caller of this
77 function. This is an optional parameter that may be
79 @param Data Pointer to the extended data buffer. This is an
80 optional parameter that may be NULL.
82 @retval EFI_SUCCESS The status code was reported.
83 @retval EFI_UNSUPPORTED Report Status Code Protocol is not available.
84 @retval EFI_UNSUPPORTED Status code type is not supported.
88 InternalReportStatusCode (
89 IN EFI_STATUS_CODE_TYPE Type
,
90 IN EFI_STATUS_CODE_VALUE Value
,
92 IN CONST EFI_GUID
*CallerId OPTIONAL
,
93 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
96 if ((ReportProgressCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
97 (ReportErrorCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ||
98 (ReportDebugCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
)) {
100 // If mReportStatusCodeLibStatusCodeProtocol is NULL, then check if Report Status Code Protocol is available in system.
102 InternalGetReportStatusCode ();
103 if (mReportStatusCodeLibStatusCodeProtocol
== NULL
) {
104 return EFI_UNSUPPORTED
;
108 // A Report Status Code Protocol is present in system, so pass in all the parameters to the service.
110 return mReportStatusCodeLibStatusCodeProtocol
->ReportStatusCode (Type
, Value
, Instance
, (EFI_GUID
*)CallerId
, Data
);
113 return EFI_UNSUPPORTED
;
118 Converts a status code to an 8-bit POST code value.
120 Converts the status code specified by CodeType and Value to an 8-bit POST code
121 and returns the 8-bit POST code in PostCode. If CodeType is an
122 EFI_PROGRESS_CODE or CodeType is an EFI_ERROR_CODE, then bits 0..4 of PostCode
123 are set to bits 16..20 of Value, and bits 5..7 of PostCode are set to bits
124 24..26 of Value., and TRUE is returned. Otherwise, FALSE is returned.
126 If PostCode is NULL, then ASSERT().
128 @param CodeType The type of status code being converted.
129 @param Value The status code value being converted.
130 @param PostCode A pointer to the 8-bit POST code value to return.
132 @retval TRUE The status code specified by CodeType and Value was converted
133 to an 8-bit POST code and returned in PostCode.
134 @retval FALSE The status code specified by CodeType and Value could not be
135 converted to an 8-bit POST code value.
141 IN EFI_STATUS_CODE_TYPE CodeType
,
142 IN EFI_STATUS_CODE_VALUE Value
,
147 // If PostCode is NULL, then ASSERT()
149 ASSERT (PostCode
!= NULL
);
152 // Convert Value to an 8 bit post code
154 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
155 ((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ) {
156 *PostCode
= (UINT8
) ((((Value
& EFI_STATUS_CODE_CLASS_MASK
) >> 24) << 5) |
157 (((Value
& EFI_STATUS_CODE_SUBCLASS_MASK
) >> 16) & 0x1f));
165 Extracts ASSERT() information from a status code structure.
167 Converts the status code specified by CodeType, Value, and Data to the ASSERT()
168 arguments specified by Filename, Description, and LineNumber. If CodeType is
169 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
170 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
171 Filename, Description, and LineNumber from the optional data area of the
172 status code buffer specified by Data. The optional data area of Data contains
173 a Null-terminated ASCII string for the FileName, followed by a Null-terminated
174 ASCII string for the Description, followed by a 32-bit LineNumber. If the
175 ASSERT() information could be extracted from Data, then return TRUE.
176 Otherwise, FALSE is returned.
178 If Data is NULL, then ASSERT().
179 If Filename is NULL, then ASSERT().
180 If Description is NULL, then ASSERT().
181 If LineNumber is NULL, then ASSERT().
183 @param CodeType The type of status code being converted.
184 @param Value The status code value being converted.
185 @param Data Pointer to status code data buffer.
186 @param Filename Pointer to the source file name that generated the ASSERT().
187 @param Description Pointer to the description of the ASSERT().
188 @param LineNumber Pointer to source line number that generated the ASSERT().
190 @retval TRUE The status code specified by CodeType, Value, and Data was
191 converted ASSERT() arguments specified by Filename, Description,
193 @retval FALSE The status code specified by CodeType, Value, and Data could
194 not be converted to ASSERT() arguments.
199 ReportStatusCodeExtractAssertInfo (
200 IN EFI_STATUS_CODE_TYPE CodeType
,
201 IN EFI_STATUS_CODE_VALUE Value
,
202 IN CONST EFI_STATUS_CODE_DATA
*Data
,
203 OUT CHAR8
**Filename
,
204 OUT CHAR8
**Description
,
205 OUT UINT32
*LineNumber
208 EFI_DEBUG_ASSERT_DATA
*AssertData
;
210 ASSERT (Data
!= NULL
);
211 ASSERT (Filename
!= NULL
);
212 ASSERT (Description
!= NULL
);
213 ASSERT (LineNumber
!= NULL
);
215 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) &&
216 ((CodeType
& EFI_STATUS_CODE_SEVERITY_MASK
) == EFI_ERROR_UNRECOVERED
) &&
217 ((Value
& EFI_STATUS_CODE_OPERATION_MASK
) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)) {
218 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)(Data
+ 1);
219 *Filename
= (CHAR8
*)(AssertData
+ 1);
220 *Description
= *Filename
+ AsciiStrLen (*Filename
) + 1;
221 *LineNumber
= AssertData
->LineNumber
;
229 Extracts DEBUG() information from a status code structure.
231 Converts the status code specified by Data to the DEBUG() arguments specified
232 by ErrorLevel, Marker, and Format. If type GUID in Data is
233 EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
234 Format from the optional data area of the status code buffer specified by Data.
235 The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
236 which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
237 the Format. If the DEBUG() information could be extracted from Data, then
238 return TRUE. Otherwise, FALSE is returned.
240 If Data is NULL, then ASSERT().
241 If ErrorLevel is NULL, then ASSERT().
242 If Marker is NULL, then ASSERT().
243 If Format is NULL, then ASSERT().
245 @param Data Pointer to status code data buffer.
246 @param ErrorLevel Pointer to error level mask for a debug message.
247 @param Marker Pointer to the variable argument list associated with Format.
248 @param Format Pointer to a Null-terminated ASCII format string of a
251 @retval TRUE The status code specified by Data was converted DEBUG() arguments
252 specified by ErrorLevel, Marker, and Format.
253 @retval FALSE The status code specified by Data could not be converted to
259 ReportStatusCodeExtractDebugInfo (
260 IN CONST EFI_STATUS_CODE_DATA
*Data
,
261 OUT UINT32
*ErrorLevel
,
262 OUT BASE_LIST
*Marker
,
266 EFI_DEBUG_INFO
*DebugInfo
;
268 ASSERT (Data
!= NULL
);
269 ASSERT (ErrorLevel
!= NULL
);
270 ASSERT (Marker
!= NULL
);
271 ASSERT (Format
!= NULL
);
274 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
276 if (!CompareGuid (&Data
->Type
, &gEfiStatusCodeDataTypeDebugGuid
)) {
281 // Retrieve the debug information from the status code record
283 DebugInfo
= (EFI_DEBUG_INFO
*)(Data
+ 1);
285 *ErrorLevel
= DebugInfo
->ErrorLevel
;
288 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
289 // of format in DEBUG string. Its address is returned in Marker and has to be 64-bit aligned.
290 // It must be noticed that EFI_DEBUG_INFO follows EFI_STATUS_CODE_DATA, whose size is
291 // 20 bytes. The size of EFI_DEBUG_INFO is 4 bytes, so we can ensure that Marker
292 // returned is 64-bit aligned.
293 // 64-bit aligned is a must, otherwise retrieving 64-bit parameter from BASE_LIST will
294 // cause unalignment exception.
296 *Marker
= (BASE_LIST
) (DebugInfo
+ 1);
297 *Format
= (CHAR8
*)(((UINT64
*)*Marker
) + 12);
304 Reports a status code.
306 Reports the status code specified by the parameters Type and Value. Status
307 code also require an instance, caller ID, and extended data. This function
308 passed in a zero instance, NULL extended data, and a caller ID of
309 gEfiCallerIdGuid, which is the GUID for the module.
311 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
312 is called while processing another any other Report Status Code Library function,
313 then ReportStatusCode() must return immediately.
315 @param Type Status code type.
316 @param Value Status code value.
318 @retval EFI_SUCCESS The status code was reported.
319 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
321 @retval EFI_UNSUPPORTED Report status code is not supported
327 IN EFI_STATUS_CODE_TYPE Type
,
328 IN EFI_STATUS_CODE_VALUE Value
331 return InternalReportStatusCode (Type
, Value
, 0, &gEfiCallerIdGuid
, NULL
);
336 Reports a status code with a Device Path Protocol as the extended data.
338 Allocates and fills in the extended data section of a status code with the
339 Device Path Protocol specified by DevicePath. This function is responsible
340 for allocating a buffer large enough for the standard header and the device
341 path. The standard header is filled in with a GUID of
342 gEfiStatusCodeSpecificDataGuid. The status code is reported with a zero
343 instance and a caller ID of gEfiCallerIdGuid.
345 ReportStatusCodeWithDevicePath()must actively prevent recursion. If
346 ReportStatusCodeWithDevicePath() is called while processing another any other
347 Report Status Code Library function, then ReportStatusCodeWithDevicePath()
348 must return EFI_DEVICE_ERROR immediately.
350 If DevicePath is NULL, then ASSERT().
352 @param Type Status code type.
353 @param Value Status code value.
354 @param DevicePath Pointer to the Device Path Protocol to be reported.
356 @retval EFI_SUCCESS The status code was reported with the extended
357 data specified by DevicePath.
358 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
359 extended data section.
360 @retval EFI_UNSUPPORTED Report status code is not supported
365 ReportStatusCodeWithDevicePath (
366 IN EFI_STATUS_CODE_TYPE Type
,
367 IN EFI_STATUS_CODE_VALUE Value
,
368 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
371 ASSERT (DevicePath
!= NULL
);
372 return ReportStatusCodeWithExtendedData (
376 GetDevicePathSize (DevicePath
)
382 Reports a status code with an extended data buffer.
384 Allocates and fills in the extended data section of a status code with the
385 extended data specified by ExtendedData and ExtendedDataSize. ExtendedData
386 is assumed to be one of the data structures specified in Related Definitions.
387 These data structure do not have the standard header, so this function is
388 responsible for allocating a buffer large enough for the standard header and
389 the extended data passed into this function. The standard header is filled
390 in with a GUID of gEfiStatusCodeSpecificDataGuid. The status code is reported
391 with a zero instance and a caller ID of gEfiCallerIdGuid.
393 ReportStatusCodeWithExtendedData()must actively prevent recursion. If
394 ReportStatusCodeWithExtendedData() is called while processing another any other
395 Report Status Code Library function, then ReportStatusCodeWithExtendedData()
396 must return EFI_DEVICE_ERROR immediately.
398 If ExtendedData is NULL, then ASSERT().
399 If ExtendedDataSize is 0, then ASSERT().
401 @param Type Status code type.
402 @param Value Status code value.
403 @param ExtendedData Pointer to the extended data buffer to be reported.
404 @param ExtendedDataSize The size, in bytes, of the extended data buffer to
407 @retval EFI_SUCCESS The status code was reported with the extended
408 data specified by ExtendedData and ExtendedDataSize.
409 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
410 extended data section.
411 @retval EFI_UNSUPPORTED Report status code is not supported
416 ReportStatusCodeWithExtendedData (
417 IN EFI_STATUS_CODE_TYPE Type
,
418 IN EFI_STATUS_CODE_VALUE Value
,
419 IN CONST VOID
*ExtendedData
,
420 IN UINTN ExtendedDataSize
423 ASSERT (ExtendedData
!= NULL
);
424 ASSERT (ExtendedDataSize
!= 0);
425 return ReportStatusCodeEx (
438 Reports a status code with full parameters.
440 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
441 is 0, then an extended data buffer is not reported. If ExtendedData is not
442 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
443 ExtendedData is assumed not have the standard status code header, so this function
444 is responsible for allocating a buffer large enough for the standard header and
445 the extended data passed into this function. The standard header is filled in
446 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
447 GUID of gEfiStatusCodeSpecificDataGuid is used. The status code is reported with
448 an instance specified by Instance and a caller ID specified by CallerId. If
449 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
451 ReportStatusCodeEx()must actively prevent recursion. If
452 ReportStatusCodeEx() is called while processing another any
453 other Report Status Code Library function, then
454 ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
456 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
457 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
459 @param Type Status code type.
460 @param Value Status code value.
461 @param Instance Status code instance number.
462 @param CallerId Pointer to a GUID that identifies the caller of this
463 function. If this parameter is NULL, then a caller
464 ID of gEfiCallerIdGuid is used.
465 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
466 If this parameter is NULL, then a the status code
467 standard header is filled in with
468 gEfiStatusCodeSpecificDataGuid.
469 @param ExtendedData Pointer to the extended data buffer. This is an
470 optional parameter that may be NULL.
471 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
473 @retval EFI_SUCCESS The status code was reported.
474 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
475 the extended data section if it was specified.
476 @retval EFI_UNSUPPORTED Report status code is not supported
482 IN EFI_STATUS_CODE_TYPE Type
,
483 IN EFI_STATUS_CODE_VALUE Value
,
485 IN CONST EFI_GUID
*CallerId OPTIONAL
,
486 IN CONST EFI_GUID
*ExtendedDataGuid OPTIONAL
,
487 IN CONST VOID
*ExtendedData OPTIONAL
,
488 IN UINTN ExtendedDataSize
492 EFI_STATUS_CODE_DATA
*StatusCodeData
;
494 UINT64 Buffer
[(MAX_EXTENDED_DATA_SIZE
/ sizeof (UINT64
)) + 1];
496 ASSERT (!((ExtendedData
== NULL
) && (ExtendedDataSize
!= 0)));
497 ASSERT (!((ExtendedData
!= NULL
) && (ExtendedDataSize
== 0)));
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 StatusCodeData
= NULL
;
510 if (Tpl
<= TPL_NOTIFY
) {
512 // Allocate space for the Status Code Header and its buffer
514 gBS
->AllocatePool (EfiBootServicesData
, sizeof (EFI_STATUS_CODE_DATA
) + ExtendedDataSize
, (VOID
**)&StatusCodeData
);
517 if (StatusCodeData
== NULL
) {
519 // If a buffer could not be allocated, then see if the local variable Buffer can be used
521 if (ExtendedDataSize
> (MAX_EXTENDED_DATA_SIZE
- sizeof (EFI_STATUS_CODE_DATA
))) {
523 // The local variable Buffer not large enough to hold the extended data associated
524 // with the status code being reported.
526 DEBUG ((EFI_D_ERROR
, "Status code extended data is too large to be reported!\n"));
527 return EFI_OUT_OF_RESOURCES
;
529 StatusCodeData
= (EFI_STATUS_CODE_DATA
*)Buffer
;
533 // Fill in the extended data header
535 StatusCodeData
->HeaderSize
= (UINT16
) sizeof (EFI_STATUS_CODE_DATA
);
536 StatusCodeData
->Size
= (UINT16
) ExtendedDataSize
;
537 if (ExtendedDataGuid
== NULL
) {
538 ExtendedDataGuid
= &gEfiStatusCodeSpecificDataGuid
;
540 CopyGuid (&StatusCodeData
->Type
, ExtendedDataGuid
);
543 // Fill in the extended data buffer
545 if (ExtendedData
!= NULL
) {
546 CopyMem (StatusCodeData
+ 1, ExtendedData
, ExtendedDataSize
);
550 // Report the status code
552 if (CallerId
== NULL
) {
553 CallerId
= &gEfiCallerIdGuid
;
555 Status
= InternalReportStatusCode (Type
, Value
, Instance
, CallerId
, StatusCodeData
);
558 // Free the allocated buffer
560 if (StatusCodeData
!= (EFI_STATUS_CODE_DATA
*)Buffer
) {
561 gBS
->FreePool (StatusCodeData
);
569 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
571 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
572 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
574 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
575 PcdReportStatusCodeProperyMask is set.
576 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
577 PcdReportStatusCodeProperyMask is clear.
582 ReportProgressCodeEnabled (
586 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
) != 0);
591 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
593 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
594 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
596 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
597 PcdReportStatusCodeProperyMask is set.
598 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
599 PcdReportStatusCodeProperyMask is clear.
604 ReportErrorCodeEnabled (
608 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
) != 0);
613 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
615 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
616 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
618 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
619 PcdReportStatusCodeProperyMask is set.
620 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
621 PcdReportStatusCodeProperyMask is clear.
626 ReportDebugCodeEnabled (
630 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
) != 0);