2 Report Status Code Library for DXE Phase.
4 Copyright (c) 2006 - 2009, Intel Corporation<BR>
5 All rights reserved. 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 <FrameworkDxe.h>
17 #include <Library/ReportStatusCodeLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/UefiRuntimeServicesTableLib.h>
25 #include <Library/DevicePathLib.h>
27 #include <Guid/StatusCodeDataTypeId.h>
28 #include <Guid/StatusCodeDataTypeDebug.h>
29 #include <Protocol/StatusCode.h>
31 EFI_REPORT_STATUS_CODE mReportStatusCode
= NULL
;
34 Locate the report status code service.
36 @return Function pointer to the report status code service.
37 NULL is returned if no status code service is available.
40 EFI_REPORT_STATUS_CODE
41 InternalGetReportStatusCode (
45 EFI_STATUS_CODE_PROTOCOL
*StatusCodeProtocol
;
48 if (gRT
!= NULL
&& gRT
->Hdr
.Revision
< 0x20000) {
49 return ((FRAMEWORK_EFI_RUNTIME_SERVICES
*)gRT
)->ReportStatusCode
;
50 } else if (gBS
!= NULL
&& gBS
->LocateProtocol
!= NULL
) {
51 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**)&StatusCodeProtocol
);
52 if (!EFI_ERROR (Status
) && StatusCodeProtocol
!= NULL
) {
53 return StatusCodeProtocol
->ReportStatusCode
;
61 Internal worker function that reports a status code through the status code service.
63 If status code service is not cached, then this function checks if status code service is
64 available in system. If status code service is not available, then EFI_UNSUPPORTED is
65 returned. If status code service is present, then it is cached in mReportStatusCode.
66 Finally this function reports status code through the status code service.
68 @param Type Status code type.
69 @param Value Status code value.
70 @param Instance Status code instance number.
71 @param CallerId Pointer to a GUID that identifies the caller of this
72 function. This is an optional parameter that may be
74 @param Data Pointer to the extended data buffer. This is an
75 optional parameter that may be NULL.
77 @retval EFI_SUCCESS The status code was reported.
78 @retval EFI_UNSUPPORTED Status code service is not available.
79 @retval EFI_UNSUPPORTED Status code type is not supported.
83 InternalReportStatusCode (
84 IN EFI_STATUS_CODE_TYPE Type
,
85 IN EFI_STATUS_CODE_VALUE Value
,
87 IN CONST EFI_GUID
*CallerId OPTIONAL
,
88 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
91 if ((ReportProgressCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
92 (ReportErrorCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ||
93 (ReportDebugCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
)) {
95 // If mReportStatusCode is NULL, then check if status code service is available in system.
97 if (mReportStatusCode
== NULL
) {
98 mReportStatusCode
= InternalGetReportStatusCode ();
99 if (mReportStatusCode
== NULL
) {
100 return EFI_UNSUPPORTED
;
105 // A status code service is present in system, so pass in all the parameters to the service.
107 return (*mReportStatusCode
) (Type
, Value
, Instance
, (EFI_GUID
*)CallerId
, Data
);
110 return EFI_UNSUPPORTED
;
115 Converts a status code to an 8-bit POST code value.
117 Converts the status code specified by CodeType and Value to an 8-bit POST code
118 and returns the 8-bit POST code in PostCode. If CodeType is an
119 EFI_PROGRESS_CODE or CodeType is an EFI_ERROR_CODE, then bits 0..4 of PostCode
120 are set to bits 16..20 of Value, and bits 5..7 of PostCode are set to bits
121 24..26 of Value., and TRUE is returned. Otherwise, FALSE is returned.
123 If PostCode is NULL, then ASSERT().
125 @param CodeType The type of status code being converted.
126 @param Value The status code value being converted.
127 @param PostCode A pointer to the 8-bit POST code value to return.
129 @retval TRUE The status code specified by CodeType and Value was converted
130 to an 8-bit POST code and returned in PostCode.
131 @retval FALSE The status code specified by CodeType and Value could not be
132 converted to an 8-bit POST code value.
138 IN EFI_STATUS_CODE_TYPE CodeType
,
139 IN EFI_STATUS_CODE_VALUE Value
,
144 // If PostCode is NULL, then ASSERT()
146 ASSERT (PostCode
!= NULL
);
149 // Convert Value to an 8 bit post code
151 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
152 ((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ) {
153 *PostCode
= (UINT8
) ((((Value
& EFI_STATUS_CODE_CLASS_MASK
) >> 24) << 5) |
154 (((Value
& EFI_STATUS_CODE_SUBCLASS_MASK
) >> 16) & 0x1f));
162 Extracts ASSERT() information from a status code structure.
164 Converts the status code specified by CodeType, Value, and Data to the ASSERT()
165 arguments specified by Filename, Description, and LineNumber. If CodeType is
166 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
167 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
168 Filename, Description, and LineNumber from the optional data area of the
169 status code buffer specified by Data. The optional data area of Data contains
170 a Null-terminated ASCII string for the FileName, followed by a Null-terminated
171 ASCII string for the Description, followed by a 32-bit LineNumber. If the
172 ASSERT() information could be extracted from Data, then return TRUE.
173 Otherwise, FALSE is returned.
175 If Data is NULL, then ASSERT().
176 If Filename is NULL, then ASSERT().
177 If Description is NULL, then ASSERT().
178 If LineNumber is NULL, then ASSERT().
180 @param CodeType The type of status code being converted.
181 @param Value The status code value being converted.
182 @param Data Pointer to status code data buffer.
183 @param Filename Pointer to the source file name that generated the ASSERT().
184 @param Description Pointer to the description of the ASSERT().
185 @param LineNumber Pointer to source line number that generated the ASSERT().
187 @retval TRUE The status code specified by CodeType, Value, and Data was
188 converted ASSERT() arguments specified by Filename, Description,
190 @retval FALSE The status code specified by CodeType, Value, and Data could
191 not be converted to ASSERT() arguments.
196 ReportStatusCodeExtractAssertInfo (
197 IN EFI_STATUS_CODE_TYPE CodeType
,
198 IN EFI_STATUS_CODE_VALUE Value
,
199 IN CONST EFI_STATUS_CODE_DATA
*Data
,
200 OUT CHAR8
**Filename
,
201 OUT CHAR8
**Description
,
202 OUT UINT32
*LineNumber
205 EFI_DEBUG_ASSERT_DATA
*AssertData
;
207 ASSERT (Data
!= NULL
);
208 ASSERT (Filename
!= NULL
);
209 ASSERT (Description
!= NULL
);
210 ASSERT (LineNumber
!= NULL
);
212 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) &&
213 ((CodeType
& EFI_STATUS_CODE_SEVERITY_MASK
) == EFI_ERROR_UNRECOVERED
) &&
214 ((Value
& EFI_STATUS_CODE_OPERATION_MASK
) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)) {
215 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)(Data
+ 1);
216 *Filename
= (CHAR8
*)(AssertData
+ 1);
217 *Description
= *Filename
+ AsciiStrLen (*Filename
) + 1;
218 *LineNumber
= AssertData
->LineNumber
;
226 Extracts DEBUG() information from a status code structure.
228 Converts the status code specified by Data to the DEBUG() arguments specified
229 by ErrorLevel, Marker, and Format. If type GUID in Data is
230 EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
231 Format from the optional data area of the status code buffer specified by Data.
232 The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
233 which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
234 the Format. If the DEBUG() information could be extracted from Data, then
235 return TRUE. Otherwise, FALSE is returned.
237 If Data is NULL, then ASSERT().
238 If ErrorLevel is NULL, then ASSERT().
239 If Marker is NULL, then ASSERT().
240 If Format is NULL, then ASSERT().
242 @param Data Pointer to status code data buffer.
243 @param ErrorLevel Pointer to error level mask for a debug message.
244 @param Marker Pointer to the variable argument list associated with Format.
245 @param Format Pointer to a Null-terminated ASCII format string of a
248 @retval TRUE The status code specified by Data was converted DEBUG() arguments
249 specified by ErrorLevel, Marker, and Format.
250 @retval FALSE The status code specified by Data could not be converted to
256 ReportStatusCodeExtractDebugInfo (
257 IN CONST EFI_STATUS_CODE_DATA
*Data
,
258 OUT UINT32
*ErrorLevel
,
259 OUT BASE_LIST
*Marker
,
263 EFI_DEBUG_INFO
*DebugInfo
;
265 ASSERT (Data
!= NULL
);
266 ASSERT (ErrorLevel
!= NULL
);
267 ASSERT (Marker
!= NULL
);
268 ASSERT (Format
!= NULL
);
271 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
273 if (!CompareGuid (&Data
->Type
, &gEfiStatusCodeDataTypeDebugGuid
)) {
278 // Retrieve the debug information from the status code record
280 DebugInfo
= (EFI_DEBUG_INFO
*)(Data
+ 1);
282 *ErrorLevel
= DebugInfo
->ErrorLevel
;
285 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
286 // of format in DEBUG string. Its address is returned in Marker and has to be 64-bit aligned.
287 // It must be noticed that EFI_DEBUG_INFO follows EFI_STATUS_CODE_DATA, whose size is
288 // 20 bytes. The size of EFI_DEBUG_INFO is 4 bytes, so we can ensure that Marker
289 // returned is 64-bit aligned.
290 // 64-bit aligned is a must, otherwise retrieving 64-bit parameter from BASE_LIST will
291 // cause unalignment exception.
293 *Marker
= (BASE_LIST
) (DebugInfo
+ 1);
294 *Format
= (CHAR8
*)(((UINT64
*)*Marker
) + 12);
301 Reports a status code.
303 Reports the status code specified by the parameters Type and Value. Status
304 code also require an instance, caller ID, and extended data. This function
305 passed in a zero instance, NULL extended data, and a caller ID of
306 gEfiCallerIdGuid, which is the GUID for the module.
308 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
309 is called while processing another any other Report Status Code Library function,
310 then ReportStatusCode() must return immediately.
312 @param Type Status code type.
313 @param Value Status code value.
315 @retval EFI_SUCCESS The status code was reported.
316 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
318 @retval EFI_UNSUPPORTED Report status code is not supported
324 IN EFI_STATUS_CODE_TYPE Type
,
325 IN EFI_STATUS_CODE_VALUE Value
328 return InternalReportStatusCode (Type
, Value
, 0, &gEfiCallerIdGuid
, NULL
);
333 Reports a status code with a Device Path Protocol as the extended data.
335 Allocates and fills in the extended data section of a status code with the
336 Device Path Protocol specified by DevicePath. This function is responsible
337 for allocating a buffer large enough for the standard header and the device
338 path. The standard header is filled in with a GUID of
339 gEfiStatusCodeSpecificDataGuid. The status code is reported with a zero
340 instance and a caller ID of gEfiCallerIdGuid.
342 ReportStatusCodeWithDevicePath()must actively prevent recursion. If
343 ReportStatusCodeWithDevicePath() is called while processing another any other
344 Report Status Code Library function, then ReportStatusCodeWithDevicePath()
345 must return EFI_DEVICE_ERROR immediately.
347 If DevicePath is NULL, then ASSERT().
349 @param Type Status code type.
350 @param Value Status code value.
351 @param DevicePath Pointer to the Device Path Protocol to be reported.
353 @retval EFI_SUCCESS The status code was reported with the extended
354 data specified by DevicePath.
355 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
356 extended data section.
357 @retval EFI_UNSUPPORTED Report status code is not supported
362 ReportStatusCodeWithDevicePath (
363 IN EFI_STATUS_CODE_TYPE Type
,
364 IN EFI_STATUS_CODE_VALUE Value
,
365 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
368 ASSERT (DevicePath
!= NULL
);
369 return ReportStatusCodeWithExtendedData (
373 GetDevicePathSize (DevicePath
)
379 Reports a status code with an extended data buffer.
381 Allocates and fills in the extended data section of a status code with the
382 extended data specified by ExtendedData and ExtendedDataSize. ExtendedData
383 is assumed to be one of the data structures specified in Related Definitions.
384 These data structure do not have the standard header, so this function is
385 responsible for allocating a buffer large enough for the standard header and
386 the extended data passed into this function. The standard header is filled
387 in with a GUID of gEfiStatusCodeSpecificDataGuid. The status code is reported
388 with a zero instance and a caller ID of gEfiCallerIdGuid.
390 ReportStatusCodeWithExtendedData()must actively prevent recursion. If
391 ReportStatusCodeWithExtendedData() is called while processing another any other
392 Report Status Code Library function, then ReportStatusCodeWithExtendedData()
393 must return EFI_DEVICE_ERROR immediately.
395 If ExtendedData is NULL, then ASSERT().
396 If ExtendedDataSize is 0, then ASSERT().
398 @param Type Status code type.
399 @param Value Status code value.
400 @param ExtendedData Pointer to the extended data buffer to be reported.
401 @param ExtendedDataSize The size, in bytes, of the extended data buffer to
404 @retval EFI_SUCCESS The status code was reported with the extended
405 data specified by ExtendedData and ExtendedDataSize.
406 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
407 extended data section.
408 @retval EFI_UNSUPPORTED Report status code is not supported
413 ReportStatusCodeWithExtendedData (
414 IN EFI_STATUS_CODE_TYPE Type
,
415 IN EFI_STATUS_CODE_VALUE Value
,
416 IN CONST VOID
*ExtendedData
,
417 IN UINTN ExtendedDataSize
420 ASSERT (ExtendedData
!= NULL
);
421 ASSERT (ExtendedDataSize
!= 0);
422 return ReportStatusCodeEx (
435 Reports a status code with full parameters.
437 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
438 is 0, then an extended data buffer is not reported. If ExtendedData is not
439 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
440 ExtendedData is assumed not have the standard status code header, so this function
441 is responsible for allocating a buffer large enough for the standard header and
442 the extended data passed into this function. The standard header is filled in
443 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
444 GUID of gEfiStatusCodeSpecificDataGuid is used. The status code is reported with
445 an instance specified by Instance and a caller ID specified by CallerId. If
446 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
448 ReportStatusCodeEx()must actively prevent recursion. If
449 ReportStatusCodeEx() is called while processing another any
450 other Report Status Code Library function, then
451 ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
453 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
454 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
456 @param Type Status code type.
457 @param Value Status code value.
458 @param Instance Status code instance number.
459 @param CallerId Pointer to a GUID that identifies the caller of this
460 function. If this parameter is NULL, then a caller
461 ID of gEfiCallerIdGuid is used.
462 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
463 If this parameter is NULL, then a the status code
464 standard header is filled in with
465 gEfiStatusCodeSpecificDataGuid.
466 @param ExtendedData Pointer to the extended data buffer. This is an
467 optional parameter that may be NULL.
468 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
470 @retval EFI_SUCCESS The status code was reported.
471 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
472 the extended data section if it was specified.
473 @retval EFI_UNSUPPORTED Report status code is not supported
479 IN EFI_STATUS_CODE_TYPE Type
,
480 IN EFI_STATUS_CODE_VALUE Value
,
482 IN CONST EFI_GUID
*CallerId OPTIONAL
,
483 IN CONST EFI_GUID
*ExtendedDataGuid OPTIONAL
,
484 IN CONST VOID
*ExtendedData OPTIONAL
,
485 IN UINTN ExtendedDataSize
489 EFI_STATUS_CODE_DATA
*StatusCodeData
;
491 ASSERT (!((ExtendedData
== NULL
) && (ExtendedDataSize
!= 0)));
492 ASSERT (!((ExtendedData
!= NULL
) && (ExtendedDataSize
== 0)));
495 // Allocate space for the Status Code Header and its buffer
497 StatusCodeData
= NULL
;
498 StatusCodeData
= AllocatePool (sizeof (EFI_STATUS_CODE_DATA
) + ExtendedDataSize
);
499 if (StatusCodeData
== NULL
) {
500 return EFI_OUT_OF_RESOURCES
;
504 // Fill in the extended data header
506 StatusCodeData
->HeaderSize
= sizeof (EFI_STATUS_CODE_DATA
);
507 StatusCodeData
->Size
= (UINT16
)ExtendedDataSize
;
508 if (ExtendedDataGuid
== NULL
) {
509 ExtendedDataGuid
= &gEfiStatusCodeSpecificDataGuid
;
511 CopyGuid (&StatusCodeData
->Type
, ExtendedDataGuid
);
514 // Fill in the extended data buffer
516 if (ExtendedData
!= NULL
) {
517 CopyMem (StatusCodeData
+ 1, ExtendedData
, ExtendedDataSize
);
521 // Report the status code
523 if (CallerId
== NULL
) {
524 CallerId
= &gEfiCallerIdGuid
;
526 Status
= InternalReportStatusCode (Type
, Value
, Instance
, CallerId
, StatusCodeData
);
529 // Free the allocated buffer
531 FreePool (StatusCodeData
);
538 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
540 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
541 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
543 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
544 PcdReportStatusCodeProperyMask is set.
545 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
546 PcdReportStatusCodeProperyMask is clear.
551 ReportProgressCodeEnabled (
555 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
) != 0);
560 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
562 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
563 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
565 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
566 PcdReportStatusCodeProperyMask is set.
567 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
568 PcdReportStatusCodeProperyMask is clear.
573 ReportErrorCodeEnabled (
577 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
) != 0);
582 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
584 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
585 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
587 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
588 PcdReportStatusCodeProperyMask is set.
589 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
590 PcdReportStatusCodeProperyMask is clear.
595 ReportDebugCodeEnabled (
599 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
) != 0);