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
;
27 Locate the report status code service.
29 @return Function pointer to the report status code service.
30 NULL is returned if no status code service is available.
33 EFI_MM_REPORT_STATUS_CODE
34 InternalGetReportStatusCode (
40 Status
= InternalLocateProtocol (&gEfiMmStatusCodeProtocolGuid
, NULL
, (VOID
**)&mStatusCodeProtocol
);
41 if (!EFI_ERROR (Status
) && (mStatusCodeProtocol
!= NULL
)) {
42 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
)))
84 // If mReportStatusCode is NULL, then check if status code service is available in system.
86 if (mReportStatusCode
== NULL
) {
87 mReportStatusCode
= InternalGetReportStatusCode ();
88 if (mReportStatusCode
== NULL
) {
89 return EFI_UNSUPPORTED
;
94 // A status code service is present in system, so pass in all the parameters to the service.
96 return (*mReportStatusCode
)(mStatusCodeProtocol
, Type
, Value
, Instance
, (EFI_GUID
*)CallerId
, Data
);
99 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
))
142 *PostCode
= (UINT8
)((((Value
& EFI_STATUS_CODE_CLASS_MASK
) >> 24) << 5) |
143 (((Value
& EFI_STATUS_CODE_SUBCLASS_MASK
) >> 16) & 0x1f));
151 Extracts ASSERT() information from a status code structure.
153 Converts the status code specified by CodeType, Value, and Data to the ASSERT()
154 arguments specified by Filename, Description, and LineNumber. If CodeType is
155 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
156 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
157 Filename, Description, and LineNumber from the optional data area of the
158 status code buffer specified by Data. The optional data area of Data contains
159 a Null-terminated ASCII string for the FileName, followed by a Null-terminated
160 ASCII string for the Description, followed by a 32-bit LineNumber. If the
161 ASSERT() information could be extracted from Data, then return TRUE.
162 Otherwise, FALSE is returned.
164 If Data is NULL, then ASSERT().
165 If Filename is NULL, then ASSERT().
166 If Description is NULL, then ASSERT().
167 If LineNumber is NULL, then ASSERT().
169 @param CodeType The type of status code being converted.
170 @param Value The status code value being converted.
171 @param Data Pointer to status code data buffer.
172 @param Filename Pointer to the source file name that generated the ASSERT().
173 @param Description Pointer to the description of the ASSERT().
174 @param LineNumber Pointer to source line number that generated the ASSERT().
176 @retval TRUE The status code specified by CodeType, Value, and Data was
177 converted ASSERT() arguments specified by Filename, Description,
179 @retval FALSE The status code specified by CodeType, Value, and Data could
180 not be converted to ASSERT() arguments.
185 ReportStatusCodeExtractAssertInfo (
186 IN EFI_STATUS_CODE_TYPE CodeType
,
187 IN EFI_STATUS_CODE_VALUE Value
,
188 IN CONST EFI_STATUS_CODE_DATA
*Data
,
189 OUT CHAR8
**Filename
,
190 OUT CHAR8
**Description
,
191 OUT UINT32
*LineNumber
194 EFI_DEBUG_ASSERT_DATA
*AssertData
;
196 ASSERT (Data
!= NULL
);
197 ASSERT (Filename
!= NULL
);
198 ASSERT (Description
!= NULL
);
199 ASSERT (LineNumber
!= NULL
);
201 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) &&
202 ((CodeType
& EFI_STATUS_CODE_SEVERITY_MASK
) == EFI_ERROR_UNRECOVERED
) &&
203 ((Value
& EFI_STATUS_CODE_OPERATION_MASK
) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
))
205 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)(Data
+ 1);
206 *Filename
= (CHAR8
*)(AssertData
+ 1);
207 *Description
= *Filename
+ AsciiStrLen (*Filename
) + 1;
208 *LineNumber
= AssertData
->LineNumber
;
216 Extracts DEBUG() information from a status code structure.
218 Converts the status code specified by Data to the DEBUG() arguments specified
219 by ErrorLevel, Marker, and Format. If type GUID in Data is
220 EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
221 Format from the optional data area of the status code buffer specified by Data.
222 The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
223 which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
224 the Format. If the DEBUG() information could be extracted from Data, then
225 return TRUE. Otherwise, FALSE is returned.
227 If Data is NULL, then ASSERT().
228 If ErrorLevel is NULL, then ASSERT().
229 If Marker is NULL, then ASSERT().
230 If Format is NULL, then ASSERT().
232 @param Data Pointer to status code data buffer.
233 @param ErrorLevel Pointer to error level mask for a debug message.
234 @param Marker Pointer to the variable argument list associated with Format.
235 @param Format Pointer to a Null-terminated ASCII format string of a
238 @retval TRUE The status code specified by Data was converted DEBUG() arguments
239 specified by ErrorLevel, Marker, and Format.
240 @retval FALSE The status code specified by Data could not be converted to
246 ReportStatusCodeExtractDebugInfo (
247 IN CONST EFI_STATUS_CODE_DATA
*Data
,
248 OUT UINT32
*ErrorLevel
,
249 OUT BASE_LIST
*Marker
,
253 EFI_DEBUG_INFO
*DebugInfo
;
255 ASSERT (Data
!= NULL
);
256 ASSERT (ErrorLevel
!= NULL
);
257 ASSERT (Marker
!= NULL
);
258 ASSERT (Format
!= NULL
);
261 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
263 if (!CompareGuid (&Data
->Type
, &gEfiStatusCodeDataTypeDebugGuid
)) {
268 // Retrieve the debug information from the status code record
270 DebugInfo
= (EFI_DEBUG_INFO
*)(Data
+ 1);
272 *ErrorLevel
= DebugInfo
->ErrorLevel
;
275 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
276 // of format in DEBUG string. Its address is returned in Marker and has to be 64-bit aligned.
277 // It must be noticed that EFI_DEBUG_INFO follows EFI_STATUS_CODE_DATA, whose size is
278 // 20 bytes. The size of EFI_DEBUG_INFO is 4 bytes, so we can ensure that Marker
279 // returned is 64-bit aligned.
280 // 64-bit aligned is a must, otherwise retrieving 64-bit parameter from BASE_LIST will
281 // cause unalignment exception.
283 *Marker
= (BASE_LIST
)(DebugInfo
+ 1);
284 *Format
= (CHAR8
*)(((UINT64
*)*Marker
) + 12);
290 Reports a status code.
292 Reports the status code specified by the parameters Type and Value. Status
293 code also require an instance, caller ID, and extended data. This function
294 passed in a zero instance, NULL extended data, and a caller ID of
295 gEfiCallerIdGuid, which is the GUID for the module.
297 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
298 is called while processing another any other Report Status Code Library function,
299 then ReportStatusCode() must return immediately.
301 @param Type Status code type.
302 @param Value Status code value.
304 @retval EFI_SUCCESS The status code was reported.
305 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
307 @retval EFI_UNSUPPORTED Report status code is not supported
313 IN EFI_STATUS_CODE_TYPE Type
,
314 IN EFI_STATUS_CODE_VALUE Value
317 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 (
376 Reports a status code with full parameters.
378 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
379 is 0, then an extended data buffer is not reported. If ExtendedData is not
380 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
381 ExtendedData is assumed not have the standard status code header, so this function
382 is 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 in
384 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
385 GUID of gEfiStatusCodeSpecificDataGuid is used. The status code is reported with
386 an instance specified by Instance and a caller ID specified by CallerId. If
387 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
389 ReportStatusCodeEx()must actively prevent recursion. If
390 ReportStatusCodeEx() is called while processing another any
391 other Report Status Code Library function, then
392 ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
394 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
395 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
397 @param Type Status code type.
398 @param Value Status code value.
399 @param Instance Status code instance number.
400 @param CallerId Pointer to a GUID that identifies the caller of this
401 function. If this parameter is NULL, then a caller
402 ID of gEfiCallerIdGuid is used.
403 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
404 If this parameter is NULL, then a the status code
405 standard header is filled in with
406 gEfiStatusCodeSpecificDataGuid.
407 @param ExtendedData Pointer to the extended data buffer. This is an
408 optional parameter that may be NULL.
409 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
411 @retval EFI_SUCCESS The status code was reported.
412 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
413 the extended data section if it was specified.
414 @retval EFI_UNSUPPORTED Report status code is not supported
420 IN EFI_STATUS_CODE_TYPE Type
,
421 IN EFI_STATUS_CODE_VALUE Value
,
423 IN CONST EFI_GUID
*CallerId OPTIONAL
,
424 IN CONST EFI_GUID
*ExtendedDataGuid OPTIONAL
,
425 IN CONST VOID
*ExtendedData OPTIONAL
,
426 IN UINTN ExtendedDataSize
430 EFI_STATUS_CODE_DATA
*StatusCodeData
;
432 ASSERT (!((ExtendedData
== NULL
) && (ExtendedDataSize
!= 0)));
433 ASSERT (!((ExtendedData
!= NULL
) && (ExtendedDataSize
== 0)));
436 // Allocate space for the Status Code Header and its buffer
438 StatusCodeData
= AllocatePool (sizeof (EFI_STATUS_CODE_DATA
) + ExtendedDataSize
);
439 if (StatusCodeData
== NULL
) {
440 return EFI_OUT_OF_RESOURCES
;
444 // Fill in the extended data header
446 StatusCodeData
->HeaderSize
= (UINT16
)sizeof (EFI_STATUS_CODE_DATA
);
447 StatusCodeData
->Size
= (UINT16
)ExtendedDataSize
;
448 if (ExtendedDataGuid
== NULL
) {
449 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
;
468 Status
= InternalReportStatusCode (Type
, Value
, Instance
, CallerId
, StatusCodeData
);
471 // Free the allocated buffer
473 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);
500 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
502 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
503 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
505 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
506 PcdReportStatusCodeProperyMask is set.
507 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
508 PcdReportStatusCodeProperyMask is clear.
513 ReportErrorCodeEnabled (
517 return (BOOLEAN
)((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
) != 0);
521 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
523 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
524 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
526 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
527 PcdReportStatusCodeProperyMask is set.
528 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
529 PcdReportStatusCodeProperyMask is clear.
534 ReportDebugCodeEnabled (
538 return (BOOLEAN
)((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
) != 0);