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 <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>
32 // Define the maximum extended data size that is supported when a status code is
33 // reported at TPL_HIGH_LEVEL.
35 #define MAX_EXTENDED_DATA_SIZE 0x200
37 EFI_REPORT_STATUS_CODE mReportStatusCode
= NULL
;
40 Locate the report status code service.
42 @return Function pointer to the report status code service.
43 NULL is returned if no status code service is available.
46 EFI_REPORT_STATUS_CODE
47 InternalGetReportStatusCode (
51 EFI_STATUS_CODE_PROTOCOL
*StatusCodeProtocol
;
54 if (gRT
!= NULL
&& gRT
->Hdr
.Revision
< 0x20000) {
55 return ((FRAMEWORK_EFI_RUNTIME_SERVICES
*)gRT
)->ReportStatusCode
;
56 } else if (gBS
!= NULL
&& gBS
->LocateProtocol
!= NULL
) {
57 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**)&StatusCodeProtocol
);
58 if (!EFI_ERROR (Status
) && StatusCodeProtocol
!= NULL
) {
59 return StatusCodeProtocol
->ReportStatusCode
;
67 Internal worker function that reports a status code through the status code service.
69 If status code service is not cached, then this function checks if status code service is
70 available in system. If status code service is not available, then EFI_UNSUPPORTED is
71 returned. If status code service is present, then it is cached in mReportStatusCode.
72 Finally this function reports status code through the status code service.
74 @param Type Status code type.
75 @param Value Status code value.
76 @param Instance Status code instance number.
77 @param CallerId Pointer to a GUID that identifies the caller of this
78 function. This is an optional parameter that may be
80 @param Data Pointer to the extended data buffer. This is an
81 optional parameter that may be NULL.
83 @retval EFI_SUCCESS The status code was reported.
84 @retval EFI_UNSUPPORTED Status code service is not available.
85 @retval EFI_UNSUPPORTED Status code type is not supported.
89 InternalReportStatusCode (
90 IN EFI_STATUS_CODE_TYPE Type
,
91 IN EFI_STATUS_CODE_VALUE Value
,
93 IN CONST EFI_GUID
*CallerId OPTIONAL
,
94 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
97 if ((ReportProgressCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
98 (ReportErrorCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ||
99 (ReportDebugCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
)) {
101 // If mReportStatusCode is NULL, then check if status code service is available in system.
103 if (mReportStatusCode
== NULL
) {
104 mReportStatusCode
= InternalGetReportStatusCode ();
105 if (mReportStatusCode
== NULL
) {
106 return EFI_UNSUPPORTED
;
111 // A status code service is present in system, so pass in all the parameters to the service.
113 return (*mReportStatusCode
) (Type
, Value
, Instance
, (EFI_GUID
*)CallerId
, Data
);
116 return EFI_UNSUPPORTED
;
121 Converts a status code to an 8-bit POST code value.
123 Converts the status code specified by CodeType and Value to an 8-bit POST code
124 and returns the 8-bit POST code in PostCode. If CodeType is an
125 EFI_PROGRESS_CODE or CodeType is an EFI_ERROR_CODE, then bits 0..4 of PostCode
126 are set to bits 16..20 of Value, and bits 5..7 of PostCode are set to bits
127 24..26 of Value., and TRUE is returned. Otherwise, FALSE is returned.
129 If PostCode is NULL, then ASSERT().
131 @param CodeType The type of status code being converted.
132 @param Value The status code value being converted.
133 @param PostCode A pointer to the 8-bit POST code value to return.
135 @retval TRUE The status code specified by CodeType and Value was converted
136 to an 8-bit POST code and returned in PostCode.
137 @retval FALSE The status code specified by CodeType and Value could not be
138 converted to an 8-bit POST code value.
144 IN EFI_STATUS_CODE_TYPE CodeType
,
145 IN EFI_STATUS_CODE_VALUE Value
,
150 // If PostCode is NULL, then ASSERT()
152 ASSERT (PostCode
!= NULL
);
155 // Convert Value to an 8 bit post code
157 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
158 ((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ) {
159 *PostCode
= (UINT8
) ((((Value
& EFI_STATUS_CODE_CLASS_MASK
) >> 24) << 5) |
160 (((Value
& EFI_STATUS_CODE_SUBCLASS_MASK
) >> 16) & 0x1f));
168 Extracts ASSERT() information from a status code structure.
170 Converts the status code specified by CodeType, Value, and Data to the ASSERT()
171 arguments specified by Filename, Description, and LineNumber. If CodeType is
172 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
173 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
174 Filename, Description, and LineNumber from the optional data area of the
175 status code buffer specified by Data. The optional data area of Data contains
176 a Null-terminated ASCII string for the FileName, followed by a Null-terminated
177 ASCII string for the Description, followed by a 32-bit LineNumber. If the
178 ASSERT() information could be extracted from Data, then return TRUE.
179 Otherwise, FALSE is returned.
181 If Data is NULL, then ASSERT().
182 If Filename is NULL, then ASSERT().
183 If Description is NULL, then ASSERT().
184 If LineNumber is NULL, then ASSERT().
186 @param CodeType The type of status code being converted.
187 @param Value The status code value being converted.
188 @param Data Pointer to status code data buffer.
189 @param Filename Pointer to the source file name that generated the ASSERT().
190 @param Description Pointer to the description of the ASSERT().
191 @param LineNumber Pointer to source line number that generated the ASSERT().
193 @retval TRUE The status code specified by CodeType, Value, and Data was
194 converted ASSERT() arguments specified by Filename, Description,
196 @retval FALSE The status code specified by CodeType, Value, and Data could
197 not be converted to ASSERT() arguments.
202 ReportStatusCodeExtractAssertInfo (
203 IN EFI_STATUS_CODE_TYPE CodeType
,
204 IN EFI_STATUS_CODE_VALUE Value
,
205 IN CONST EFI_STATUS_CODE_DATA
*Data
,
206 OUT CHAR8
**Filename
,
207 OUT CHAR8
**Description
,
208 OUT UINT32
*LineNumber
211 EFI_DEBUG_ASSERT_DATA
*AssertData
;
213 ASSERT (Data
!= NULL
);
214 ASSERT (Filename
!= NULL
);
215 ASSERT (Description
!= NULL
);
216 ASSERT (LineNumber
!= NULL
);
218 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) &&
219 ((CodeType
& EFI_STATUS_CODE_SEVERITY_MASK
) == EFI_ERROR_UNRECOVERED
) &&
220 ((Value
& EFI_STATUS_CODE_OPERATION_MASK
) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)) {
221 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)(Data
+ 1);
222 *Filename
= (CHAR8
*)(AssertData
+ 1);
223 *Description
= *Filename
+ AsciiStrLen (*Filename
) + 1;
224 *LineNumber
= AssertData
->LineNumber
;
232 Extracts DEBUG() information from a status code structure.
234 Converts the status code specified by Data to the DEBUG() arguments specified
235 by ErrorLevel, Marker, and Format. If type GUID in Data is
236 EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
237 Format from the optional data area of the status code buffer specified by Data.
238 The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
239 which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
240 the Format. If the DEBUG() information could be extracted from Data, then
241 return TRUE. Otherwise, FALSE is returned.
243 If Data is NULL, then ASSERT().
244 If ErrorLevel is NULL, then ASSERT().
245 If Marker is NULL, then ASSERT().
246 If Format is NULL, then ASSERT().
248 @param Data Pointer to status code data buffer.
249 @param ErrorLevel Pointer to error level mask for a debug message.
250 @param Marker Pointer to the variable argument list associated with Format.
251 @param Format Pointer to a Null-terminated ASCII format string of a
254 @retval TRUE The status code specified by Data was converted DEBUG() arguments
255 specified by ErrorLevel, Marker, and Format.
256 @retval FALSE The status code specified by Data could not be converted to
262 ReportStatusCodeExtractDebugInfo (
263 IN CONST EFI_STATUS_CODE_DATA
*Data
,
264 OUT UINT32
*ErrorLevel
,
265 OUT BASE_LIST
*Marker
,
269 EFI_DEBUG_INFO
*DebugInfo
;
271 ASSERT (Data
!= NULL
);
272 ASSERT (ErrorLevel
!= NULL
);
273 ASSERT (Marker
!= NULL
);
274 ASSERT (Format
!= NULL
);
277 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
279 if (!CompareGuid (&Data
->Type
, &gEfiStatusCodeDataTypeDebugGuid
)) {
284 // Retrieve the debug information from the status code record
286 DebugInfo
= (EFI_DEBUG_INFO
*)(Data
+ 1);
288 *ErrorLevel
= DebugInfo
->ErrorLevel
;
291 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
292 // of format in DEBUG string. Its address is returned in Marker and has to be 64-bit aligned.
293 // It must be noticed that EFI_DEBUG_INFO follows EFI_STATUS_CODE_DATA, whose size is
294 // 20 bytes. The size of EFI_DEBUG_INFO is 4 bytes, so we can ensure that Marker
295 // returned is 64-bit aligned.
296 // 64-bit aligned is a must, otherwise retrieving 64-bit parameter from BASE_LIST will
297 // cause unalignment exception.
299 *Marker
= (BASE_LIST
) (DebugInfo
+ 1);
300 *Format
= (CHAR8
*)(((UINT64
*)*Marker
) + 12);
307 Reports a status code.
309 Reports the status code specified by the parameters Type and Value. Status
310 code also require an instance, caller ID, and extended data. This function
311 passed in a zero instance, NULL extended data, and a caller ID of
312 gEfiCallerIdGuid, which is the GUID for the module.
314 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
315 is called while processing another any other Report Status Code Library function,
316 then ReportStatusCode() must return immediately.
318 @param Type Status code type.
319 @param Value Status code value.
321 @retval EFI_SUCCESS The status code was reported.
322 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
324 @retval EFI_UNSUPPORTED Report status code is not supported
330 IN EFI_STATUS_CODE_TYPE Type
,
331 IN EFI_STATUS_CODE_VALUE Value
334 return InternalReportStatusCode (Type
, Value
, 0, &gEfiCallerIdGuid
, NULL
);
339 Reports a status code with a Device Path Protocol as the extended data.
341 Allocates and fills in the extended data section of a status code with the
342 Device Path Protocol specified by DevicePath. This function is responsible
343 for allocating a buffer large enough for the standard header and the device
344 path. The standard header is filled in with a GUID of
345 gEfiStatusCodeSpecificDataGuid. The status code is reported with a zero
346 instance and a caller ID of gEfiCallerIdGuid.
348 ReportStatusCodeWithDevicePath()must actively prevent recursion. If
349 ReportStatusCodeWithDevicePath() is called while processing another any other
350 Report Status Code Library function, then ReportStatusCodeWithDevicePath()
351 must return EFI_DEVICE_ERROR immediately.
353 If DevicePath is NULL, then ASSERT().
355 @param Type Status code type.
356 @param Value Status code value.
357 @param DevicePath Pointer to the Device Path Protocol to be reported.
359 @retval EFI_SUCCESS The status code was reported with the extended
360 data specified by DevicePath.
361 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
362 extended data section.
363 @retval EFI_UNSUPPORTED Report status code is not supported
368 ReportStatusCodeWithDevicePath (
369 IN EFI_STATUS_CODE_TYPE Type
,
370 IN EFI_STATUS_CODE_VALUE Value
,
371 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
374 ASSERT (DevicePath
!= NULL
);
375 return ReportStatusCodeWithExtendedData (
379 GetDevicePathSize (DevicePath
)
385 Reports a status code with an extended data buffer.
387 Allocates and fills in the extended data section of a status code with the
388 extended data specified by ExtendedData and ExtendedDataSize. ExtendedData
389 is assumed to be one of the data structures specified in Related Definitions.
390 These data structure do not have the standard header, so this function is
391 responsible for allocating a buffer large enough for the standard header and
392 the extended data passed into this function. The standard header is filled
393 in with a GUID of gEfiStatusCodeSpecificDataGuid. The status code is reported
394 with a zero instance and a caller ID of gEfiCallerIdGuid.
396 ReportStatusCodeWithExtendedData()must actively prevent recursion. If
397 ReportStatusCodeWithExtendedData() is called while processing another any other
398 Report Status Code Library function, then ReportStatusCodeWithExtendedData()
399 must return EFI_DEVICE_ERROR immediately.
401 If ExtendedData is NULL, then ASSERT().
402 If ExtendedDataSize is 0, then ASSERT().
404 @param Type Status code type.
405 @param Value Status code value.
406 @param ExtendedData Pointer to the extended data buffer to be reported.
407 @param ExtendedDataSize The size, in bytes, of the extended data buffer to
410 @retval EFI_SUCCESS The status code was reported with the extended
411 data specified by ExtendedData and ExtendedDataSize.
412 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
413 extended data section.
414 @retval EFI_UNSUPPORTED Report status code is not supported
419 ReportStatusCodeWithExtendedData (
420 IN EFI_STATUS_CODE_TYPE Type
,
421 IN EFI_STATUS_CODE_VALUE Value
,
422 IN CONST VOID
*ExtendedData
,
423 IN UINTN ExtendedDataSize
426 ASSERT (ExtendedData
!= NULL
);
427 ASSERT (ExtendedDataSize
!= 0);
428 return ReportStatusCodeEx (
441 Reports a status code with full parameters.
443 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
444 is 0, then an extended data buffer is not reported. If ExtendedData is not
445 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
446 ExtendedData is assumed not have the standard status code header, so this function
447 is responsible for allocating a buffer large enough for the standard header and
448 the extended data passed into this function. The standard header is filled in
449 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
450 GUID of gEfiStatusCodeSpecificDataGuid is used. The status code is reported with
451 an instance specified by Instance and a caller ID specified by CallerId. If
452 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
454 ReportStatusCodeEx()must actively prevent recursion. If
455 ReportStatusCodeEx() is called while processing another any
456 other Report Status Code Library function, then
457 ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
459 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
460 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
462 @param Type Status code type.
463 @param Value Status code value.
464 @param Instance Status code instance number.
465 @param CallerId Pointer to a GUID that identifies the caller of this
466 function. If this parameter is NULL, then a caller
467 ID of gEfiCallerIdGuid is used.
468 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
469 If this parameter is NULL, then a the status code
470 standard header is filled in with
471 gEfiStatusCodeSpecificDataGuid.
472 @param ExtendedData Pointer to the extended data buffer. This is an
473 optional parameter that may be NULL.
474 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
476 @retval EFI_SUCCESS The status code was reported.
477 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
478 the extended data section if it was specified.
479 @retval EFI_UNSUPPORTED Report status code is not supported
485 IN EFI_STATUS_CODE_TYPE Type
,
486 IN EFI_STATUS_CODE_VALUE Value
,
488 IN CONST EFI_GUID
*CallerId OPTIONAL
,
489 IN CONST EFI_GUID
*ExtendedDataGuid OPTIONAL
,
490 IN CONST VOID
*ExtendedData OPTIONAL
,
491 IN UINTN ExtendedDataSize
495 EFI_STATUS_CODE_DATA
*StatusCodeData
;
497 UINT64 Buffer
[(MAX_EXTENDED_DATA_SIZE
/ sizeof (UINT64
)) + 1];
499 ASSERT (!((ExtendedData
== NULL
) && (ExtendedDataSize
!= 0)));
500 ASSERT (!((ExtendedData
!= NULL
) && (ExtendedDataSize
== 0)));
502 if (gBS
== NULL
|| gBS
->AllocatePool
== NULL
|| gBS
->FreePool
== NULL
) {
503 return EFI_UNSUPPORTED
;
507 // Retrieve the current TPL
509 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
510 gBS
->RestoreTPL (Tpl
);
512 StatusCodeData
= NULL
;
513 if (Tpl
<= TPL_NOTIFY
) {
515 // Allocate space for the Status Code Header and its buffer
517 gBS
->AllocatePool (EfiBootServicesData
, sizeof (EFI_STATUS_CODE_DATA
) + ExtendedDataSize
, (VOID
**)&StatusCodeData
);
520 if (StatusCodeData
== NULL
) {
522 // If a buffer could not be allocated, then see if the local variable Buffer can be used
524 if (ExtendedDataSize
> (MAX_EXTENDED_DATA_SIZE
- sizeof (EFI_STATUS_CODE_DATA
))) {
526 // The local variable Buffer not large enough to hold the extended data associated
527 // with the status code being reported.
529 DEBUG ((EFI_D_ERROR
, "Status code extended data is too large to be reported!\n"));
530 return EFI_OUT_OF_RESOURCES
;
532 StatusCodeData
= (EFI_STATUS_CODE_DATA
*)Buffer
;
536 // Fill in the extended data header
538 StatusCodeData
->HeaderSize
= (UINT16
) sizeof (EFI_STATUS_CODE_DATA
);
539 StatusCodeData
->Size
= (UINT16
)ExtendedDataSize
;
540 if (ExtendedDataGuid
== NULL
) {
541 ExtendedDataGuid
= &gEfiStatusCodeSpecificDataGuid
;
543 CopyGuid (&StatusCodeData
->Type
, ExtendedDataGuid
);
546 // Fill in the extended data buffer
548 if (ExtendedData
!= NULL
) {
549 CopyMem (StatusCodeData
+ 1, ExtendedData
, ExtendedDataSize
);
553 // Report the status code
555 if (CallerId
== NULL
) {
556 CallerId
= &gEfiCallerIdGuid
;
558 Status
= InternalReportStatusCode (Type
, Value
, Instance
, CallerId
, StatusCodeData
);
561 // Free the allocated buffer
563 if (StatusCodeData
!= (EFI_STATUS_CODE_DATA
*)Buffer
) {
564 gBS
->FreePool (StatusCodeData
);
572 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
574 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
575 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
577 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
578 PcdReportStatusCodeProperyMask is set.
579 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
580 PcdReportStatusCodeProperyMask is clear.
585 ReportProgressCodeEnabled (
589 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
) != 0);
594 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
596 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
597 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
599 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
600 PcdReportStatusCodeProperyMask is set.
601 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
602 PcdReportStatusCodeProperyMask is clear.
607 ReportErrorCodeEnabled (
611 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
) != 0);
616 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
618 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
619 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
621 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
622 PcdReportStatusCodeProperyMask is set.
623 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
624 PcdReportStatusCodeProperyMask is clear.
629 ReportDebugCodeEnabled (
633 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
) != 0);