2 Report Status Code Library for MM Phase.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Library/ReportStatusCodeLib.h>
10 #include <Library/DebugLib.h>
11 #include <Library/MmServicesTableLib.h>
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/PcdLib.h>
15 #include <Library/MemoryAllocationLib.h>
17 #include <Guid/StatusCodeDataTypeId.h>
18 #include <Guid/StatusCodeDataTypeDebug.h>
19 #include <Protocol/MmStatusCode.h>
21 #include "ReportStatusCodeLib.h"
23 EFI_MM_REPORT_STATUS_CODE mReportStatusCode
= NULL
;
24 EFI_MM_STATUS_CODE_PROTOCOL
*mStatusCodeProtocol
= NULL
;
28 Locate the report status code service.
30 @return Function pointer to the report status code service.
31 NULL is returned if no status code service is available.
34 EFI_MM_REPORT_STATUS_CODE
35 InternalGetReportStatusCode (
41 Status
= InternalLocateProtocol (&gEfiMmStatusCodeProtocolGuid
, NULL
, (VOID
**)&mStatusCodeProtocol
);
42 if (!EFI_ERROR (Status
) && mStatusCodeProtocol
!= NULL
) {
43 return mStatusCodeProtocol
->ReportStatusCode
;
49 Internal worker function that reports a status code through the status code service.
51 If status code service is not cached, then this function checks if status code service is
52 available in system. If status code service is not available, then EFI_UNSUPPORTED is
53 returned. If status code service is present, then it is cached in mReportStatusCode.
54 Finally this function reports status code through the status code service.
56 @param Type Status code type.
57 @param Value Status code value.
58 @param Instance Status code instance number.
59 @param CallerId Pointer to a GUID that identifies the caller of this
60 function. This is an optional parameter that may be
62 @param Data Pointer to the extended data buffer. This is an
63 optional parameter that may be NULL.
65 @retval EFI_SUCCESS The status code was reported.
66 @retval EFI_UNSUPPORTED Status code service is not available.
67 @retval EFI_UNSUPPORTED Status code type is not supported.
71 InternalReportStatusCode (
72 IN EFI_STATUS_CODE_TYPE Type
,
73 IN EFI_STATUS_CODE_VALUE Value
,
75 IN CONST EFI_GUID
*CallerId OPTIONAL
,
76 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
79 if ((ReportProgressCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
80 (ReportErrorCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ||
81 (ReportDebugCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
)) {
83 // If mReportStatusCode is NULL, then check if status code service is available in system.
85 if (mReportStatusCode
== NULL
) {
86 mReportStatusCode
= InternalGetReportStatusCode ();
87 if (mReportStatusCode
== NULL
) {
88 return EFI_UNSUPPORTED
;
93 // A status code service is present in system, so pass in all the parameters to the service.
95 return (*mReportStatusCode
) (mStatusCodeProtocol
, Type
, Value
, Instance
, (EFI_GUID
*)CallerId
, Data
);
98 return EFI_UNSUPPORTED
;
103 Converts a status code to an 8-bit POST code value.
105 Converts the status code specified by CodeType and Value to an 8-bit POST code
106 and returns the 8-bit POST code in PostCode. If CodeType is an
107 EFI_PROGRESS_CODE or CodeType is an EFI_ERROR_CODE, then bits 0..4 of PostCode
108 are set to bits 16..20 of Value, and bits 5..7 of PostCode are set to bits
109 24..26 of Value., and TRUE is returned. Otherwise, FALSE is returned.
111 If PostCode is NULL, then ASSERT().
113 @param CodeType The type of status code being converted.
114 @param Value The status code value being converted.
115 @param PostCode A pointer to the 8-bit POST code value to return.
117 @retval TRUE The status code specified by CodeType and Value was converted
118 to an 8-bit POST code and returned in PostCode.
119 @retval FALSE The status code specified by CodeType and Value could not be
120 converted to an 8-bit POST code value.
126 IN EFI_STATUS_CODE_TYPE CodeType
,
127 IN EFI_STATUS_CODE_VALUE Value
,
132 // If PostCode is NULL, then ASSERT()
134 ASSERT (PostCode
!= NULL
);
137 // Convert Value to an 8 bit post code
139 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
140 ((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ) {
141 *PostCode
= (UINT8
) ((((Value
& EFI_STATUS_CODE_CLASS_MASK
) >> 24) << 5) |
142 (((Value
& EFI_STATUS_CODE_SUBCLASS_MASK
) >> 16) & 0x1f));
150 Extracts ASSERT() information from a status code structure.
152 Converts the status code specified by CodeType, Value, and Data to the ASSERT()
153 arguments specified by Filename, Description, and LineNumber. If CodeType is
154 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
155 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
156 Filename, Description, and LineNumber from the optional data area of the
157 status code buffer specified by Data. The optional data area of Data contains
158 a Null-terminated ASCII string for the FileName, followed by a Null-terminated
159 ASCII string for the Description, followed by a 32-bit LineNumber. If the
160 ASSERT() information could be extracted from Data, then return TRUE.
161 Otherwise, FALSE is returned.
163 If Data is NULL, then ASSERT().
164 If Filename is NULL, then ASSERT().
165 If Description is NULL, then ASSERT().
166 If LineNumber is NULL, then ASSERT().
168 @param CodeType The type of status code being converted.
169 @param Value The status code value being converted.
170 @param Data Pointer to status code data buffer.
171 @param Filename Pointer to the source file name that generated the ASSERT().
172 @param Description Pointer to the description of the ASSERT().
173 @param LineNumber Pointer to source line number that generated the ASSERT().
175 @retval TRUE The status code specified by CodeType, Value, and Data was
176 converted ASSERT() arguments specified by Filename, Description,
178 @retval FALSE The status code specified by CodeType, Value, and Data could
179 not be converted to ASSERT() arguments.
184 ReportStatusCodeExtractAssertInfo (
185 IN EFI_STATUS_CODE_TYPE CodeType
,
186 IN EFI_STATUS_CODE_VALUE Value
,
187 IN CONST EFI_STATUS_CODE_DATA
*Data
,
188 OUT CHAR8
**Filename
,
189 OUT CHAR8
**Description
,
190 OUT UINT32
*LineNumber
193 EFI_DEBUG_ASSERT_DATA
*AssertData
;
195 ASSERT (Data
!= NULL
);
196 ASSERT (Filename
!= NULL
);
197 ASSERT (Description
!= NULL
);
198 ASSERT (LineNumber
!= NULL
);
200 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) &&
201 ((CodeType
& EFI_STATUS_CODE_SEVERITY_MASK
) == EFI_ERROR_UNRECOVERED
) &&
202 ((Value
& EFI_STATUS_CODE_OPERATION_MASK
) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)) {
203 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)(Data
+ 1);
204 *Filename
= (CHAR8
*)(AssertData
+ 1);
205 *Description
= *Filename
+ AsciiStrLen (*Filename
) + 1;
206 *LineNumber
= AssertData
->LineNumber
;
214 Extracts DEBUG() information from a status code structure.
216 Converts the status code specified by Data to the DEBUG() arguments specified
217 by ErrorLevel, Marker, and Format. If type GUID in Data is
218 EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
219 Format from the optional data area of the status code buffer specified by Data.
220 The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
221 which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
222 the Format. If the DEBUG() information could be extracted from Data, then
223 return TRUE. Otherwise, FALSE is returned.
225 If Data is NULL, then ASSERT().
226 If ErrorLevel is NULL, then ASSERT().
227 If Marker is NULL, then ASSERT().
228 If Format is NULL, then ASSERT().
230 @param Data Pointer to status code data buffer.
231 @param ErrorLevel Pointer to error level mask for a debug message.
232 @param Marker Pointer to the variable argument list associated with Format.
233 @param Format Pointer to a Null-terminated ASCII format string of a
236 @retval TRUE The status code specified by Data was converted DEBUG() arguments
237 specified by ErrorLevel, Marker, and Format.
238 @retval FALSE The status code specified by Data could not be converted to
244 ReportStatusCodeExtractDebugInfo (
245 IN CONST EFI_STATUS_CODE_DATA
*Data
,
246 OUT UINT32
*ErrorLevel
,
247 OUT BASE_LIST
*Marker
,
251 EFI_DEBUG_INFO
*DebugInfo
;
253 ASSERT (Data
!= NULL
);
254 ASSERT (ErrorLevel
!= NULL
);
255 ASSERT (Marker
!= NULL
);
256 ASSERT (Format
!= NULL
);
259 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
261 if (!CompareGuid (&Data
->Type
, &gEfiStatusCodeDataTypeDebugGuid
)) {
266 // Retrieve the debug information from the status code record
268 DebugInfo
= (EFI_DEBUG_INFO
*)(Data
+ 1);
270 *ErrorLevel
= DebugInfo
->ErrorLevel
;
273 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
274 // of format in DEBUG string. Its address is returned in Marker and has to be 64-bit aligned.
275 // It must be noticed that EFI_DEBUG_INFO follows EFI_STATUS_CODE_DATA, whose size is
276 // 20 bytes. The size of EFI_DEBUG_INFO is 4 bytes, so we can ensure that Marker
277 // returned is 64-bit aligned.
278 // 64-bit aligned is a must, otherwise retrieving 64-bit parameter from BASE_LIST will
279 // cause unalignment exception.
281 *Marker
= (BASE_LIST
) (DebugInfo
+ 1);
282 *Format
= (CHAR8
*)(((UINT64
*)*Marker
) + 12);
289 Reports a status code.
291 Reports the status code specified by the parameters Type and Value. Status
292 code also require an instance, caller ID, and extended data. This function
293 passed in a zero instance, NULL extended data, and a caller ID of
294 gEfiCallerIdGuid, which is the GUID for the module.
296 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
297 is called while processing another any other Report Status Code Library function,
298 then ReportStatusCode() must return immediately.
300 @param Type Status code type.
301 @param Value Status code value.
303 @retval EFI_SUCCESS The status code was reported.
304 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
306 @retval EFI_UNSUPPORTED Report status code is not supported
312 IN EFI_STATUS_CODE_TYPE Type
,
313 IN EFI_STATUS_CODE_VALUE Value
316 return InternalReportStatusCode (Type
, Value
, 0, &gEfiCallerIdGuid
, NULL
);
321 Reports a status code with an extended data buffer.
323 Allocates and fills in the extended data section of a status code with the
324 extended data specified by ExtendedData and ExtendedDataSize. ExtendedData
325 is assumed to be one of the data structures specified in Related Definitions.
326 These data structure do not have the standard header, so this function is
327 responsible for allocating a buffer large enough for the standard header and
328 the extended data passed into this function. The standard header is filled
329 in with a GUID of gEfiStatusCodeSpecificDataGuid. The status code is reported
330 with a zero instance and a caller ID of gEfiCallerIdGuid.
332 ReportStatusCodeWithExtendedData()must actively prevent recursion. If
333 ReportStatusCodeWithExtendedData() is called while processing another any other
334 Report Status Code Library function, then ReportStatusCodeWithExtendedData()
335 must return EFI_DEVICE_ERROR immediately.
337 If ExtendedData is NULL, then ASSERT().
338 If ExtendedDataSize is 0, then ASSERT().
340 @param Type Status code type.
341 @param Value Status code value.
342 @param ExtendedData Pointer to the extended data buffer to be reported.
343 @param ExtendedDataSize The size, in bytes, of the extended data buffer to
346 @retval EFI_SUCCESS The status code was reported with the extended
347 data specified by ExtendedData and ExtendedDataSize.
348 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
349 extended data section.
350 @retval EFI_UNSUPPORTED Report status code is not supported
355 ReportStatusCodeWithExtendedData (
356 IN EFI_STATUS_CODE_TYPE Type
,
357 IN EFI_STATUS_CODE_VALUE Value
,
358 IN CONST VOID
*ExtendedData
,
359 IN UINTN ExtendedDataSize
362 ASSERT (ExtendedData
!= NULL
);
363 ASSERT (ExtendedDataSize
!= 0);
364 return ReportStatusCodeEx (
377 Reports a status code with full parameters.
379 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
380 is 0, then an extended data buffer is not reported. If ExtendedData is not
381 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
382 ExtendedData is assumed not have the standard status code header, so this function
383 is responsible for allocating a buffer large enough for the standard header and
384 the extended data passed into this function. The standard header is filled in
385 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
386 GUID of gEfiStatusCodeSpecificDataGuid is used. The status code is reported with
387 an instance specified by Instance and a caller ID specified by CallerId. If
388 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
390 ReportStatusCodeEx()must actively prevent recursion. If
391 ReportStatusCodeEx() is called while processing another any
392 other Report Status Code Library function, then
393 ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
395 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
396 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
398 @param Type Status code type.
399 @param Value Status code value.
400 @param Instance Status code instance number.
401 @param CallerId Pointer to a GUID that identifies the caller of this
402 function. If this parameter is NULL, then a caller
403 ID of gEfiCallerIdGuid is used.
404 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
405 If this parameter is NULL, then a the status code
406 standard header is filled in with
407 gEfiStatusCodeSpecificDataGuid.
408 @param ExtendedData Pointer to the extended data buffer. This is an
409 optional parameter that may be NULL.
410 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
412 @retval EFI_SUCCESS The status code was reported.
413 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
414 the extended data section if it was specified.
415 @retval EFI_UNSUPPORTED Report status code is not supported
421 IN EFI_STATUS_CODE_TYPE Type
,
422 IN EFI_STATUS_CODE_VALUE Value
,
424 IN CONST EFI_GUID
*CallerId OPTIONAL
,
425 IN CONST EFI_GUID
*ExtendedDataGuid OPTIONAL
,
426 IN CONST VOID
*ExtendedData OPTIONAL
,
427 IN UINTN ExtendedDataSize
431 EFI_STATUS_CODE_DATA
*StatusCodeData
;
433 ASSERT (!((ExtendedData
== NULL
) && (ExtendedDataSize
!= 0)));
434 ASSERT (!((ExtendedData
!= NULL
) && (ExtendedDataSize
== 0)));
437 // Allocate space for the Status Code Header and its buffer
439 StatusCodeData
= AllocatePool (sizeof (EFI_STATUS_CODE_DATA
) + ExtendedDataSize
);
440 if (StatusCodeData
== NULL
) {
441 return EFI_OUT_OF_RESOURCES
;
445 // Fill in the extended data header
447 StatusCodeData
->HeaderSize
= (UINT16
) sizeof (EFI_STATUS_CODE_DATA
);
448 StatusCodeData
->Size
= (UINT16
) ExtendedDataSize
;
449 if (ExtendedDataGuid
== NULL
) {
450 ExtendedDataGuid
= &gEfiStatusCodeSpecificDataGuid
;
452 CopyGuid (&StatusCodeData
->Type
, ExtendedDataGuid
);
455 // Fill in the extended data buffer
457 if (ExtendedData
!= NULL
) {
458 CopyMem (StatusCodeData
+ 1, ExtendedData
, ExtendedDataSize
);
462 // Report the status code
464 if (CallerId
== NULL
) {
465 CallerId
= &gEfiCallerIdGuid
;
467 Status
= InternalReportStatusCode (Type
, Value
, Instance
, CallerId
, StatusCodeData
);
470 // Free the allocated buffer
472 FreePool (StatusCodeData
);
479 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
481 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
482 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
484 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
485 PcdReportStatusCodeProperyMask is set.
486 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
487 PcdReportStatusCodeProperyMask is clear.
492 ReportProgressCodeEnabled (
496 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
) != 0);
501 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
503 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
504 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
506 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
507 PcdReportStatusCodeProperyMask is set.
508 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
509 PcdReportStatusCodeProperyMask is clear.
514 ReportErrorCodeEnabled (
518 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
) != 0);
523 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
525 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
526 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
528 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
529 PcdReportStatusCodeProperyMask is set.
530 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
531 PcdReportStatusCodeProperyMask is clear.
536 ReportDebugCodeEnabled (
540 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
) != 0);