2 Report Status Code Library for SMM Phase.
4 Copyright (c) 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/SmmServicesTableLib.h>
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/PcdLib.h>
24 #include <Guid/StatusCodeDataTypeId.h>
25 #include <Guid/StatusCodeDataTypeDebug.h>
26 #include <Protocol/SmmStatusCode.h>
28 EFI_SMM_REPORT_STATUS_CODE mReportStatusCode
= NULL
;
29 EFI_SMM_STATUS_CODE_PROTOCOL
*mStatusCodeProtocol
= NULL
;
33 Locate the report status code service.
35 @return Function pointer to the report status code service.
36 NULL is returned if no status code service is available.
39 EFI_SMM_REPORT_STATUS_CODE
40 InternalGetReportStatusCode (
46 Status
= gSmst
->SmmLocateProtocol (&gEfiSmmStatusCodeProtocolGuid
, NULL
, (VOID
**)&mStatusCodeProtocol
);
47 if (!EFI_ERROR (Status
) && mStatusCodeProtocol
!= NULL
) {
48 return mStatusCodeProtocol
->ReportStatusCode
;
54 Internal worker function that reports a status code through the status code service.
56 If status code service is not cached, then this function checks if status code service is
57 available in system. If status code service is not available, then EFI_UNSUPPORTED is
58 returned. If status code service is present, then it is cached in mReportStatusCode.
59 Finally this function reports status code through the status code service.
61 @param Type Status code type.
62 @param Value Status code value.
63 @param Instance Status code instance number.
64 @param CallerId Pointer to a GUID that identifies the caller of this
65 function. This is an optional parameter that may be
67 @param Data Pointer to the extended data buffer. This is an
68 optional parameter that may be NULL.
70 @retval EFI_SUCCESS The status code was reported.
71 @retval EFI_UNSUPPORTED Status code service is not available.
72 @retval EFI_UNSUPPORTED Status code type is not supported.
76 InternalReportStatusCode (
77 IN EFI_STATUS_CODE_TYPE Type
,
78 IN EFI_STATUS_CODE_VALUE Value
,
80 IN CONST EFI_GUID
*CallerId OPTIONAL
,
81 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
84 if ((ReportProgressCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
85 (ReportErrorCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ||
86 (ReportDebugCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
)) {
88 // If mReportStatusCode is NULL, then check if status code service is available in system.
90 if (mReportStatusCode
== NULL
) {
91 mReportStatusCode
= InternalGetReportStatusCode ();
92 if (mReportStatusCode
== NULL
) {
93 return EFI_UNSUPPORTED
;
98 // A status code service is present in system, so pass in all the parameters to the service.
100 return (*mReportStatusCode
) (mStatusCodeProtocol
, Type
, Value
, Instance
, (EFI_GUID
*)CallerId
, Data
);
103 return EFI_UNSUPPORTED
;
108 Converts a status code to an 8-bit POST code value.
110 Converts the status code specified by CodeType and Value to an 8-bit POST code
111 and returns the 8-bit POST code in PostCode. If CodeType is an
112 EFI_PROGRESS_CODE or CodeType is an EFI_ERROR_CODE, then bits 0..4 of PostCode
113 are set to bits 16..20 of Value, and bits 5..7 of PostCode are set to bits
114 24..26 of Value., and TRUE is returned. Otherwise, FALSE is returned.
116 If PostCode is NULL, then ASSERT().
118 @param CodeType The type of status code being converted.
119 @param Value The status code value being converted.
120 @param PostCode A pointer to the 8-bit POST code value to return.
122 @retval TRUE The status code specified by CodeType and Value was converted
123 to an 8-bit POST code and returned in PostCode.
124 @retval FALSE The status code specified by CodeType and Value could not be
125 converted to an 8-bit POST code value.
131 IN EFI_STATUS_CODE_TYPE CodeType
,
132 IN EFI_STATUS_CODE_VALUE Value
,
137 // If PostCode is NULL, then ASSERT()
139 ASSERT (PostCode
!= NULL
);
142 // Convert Value to an 8 bit post code
144 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
145 ((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ) {
146 *PostCode
= (UINT8
) ((((Value
& EFI_STATUS_CODE_CLASS_MASK
) >> 24) << 5) |
147 (((Value
& EFI_STATUS_CODE_SUBCLASS_MASK
) >> 16) & 0x1f));
155 Extracts ASSERT() information from a status code structure.
157 Converts the status code specified by CodeType, Value, and Data to the ASSERT()
158 arguments specified by Filename, Description, and LineNumber. If CodeType is
159 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
160 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
161 Filename, Description, and LineNumber from the optional data area of the
162 status code buffer specified by Data. The optional data area of Data contains
163 a Null-terminated ASCII string for the FileName, followed by a Null-terminated
164 ASCII string for the Description, followed by a 32-bit LineNumber. If the
165 ASSERT() information could be extracted from Data, then return TRUE.
166 Otherwise, FALSE is returned.
168 If Data is NULL, then ASSERT().
169 If Filename is NULL, then ASSERT().
170 If Description is NULL, then ASSERT().
171 If LineNumber is NULL, then ASSERT().
173 @param CodeType The type of status code being converted.
174 @param Value The status code value being converted.
175 @param Data Pointer to status code data buffer.
176 @param Filename Pointer to the source file name that generated the ASSERT().
177 @param Description Pointer to the description of the ASSERT().
178 @param LineNumber Pointer to source line number that generated the ASSERT().
180 @retval TRUE The status code specified by CodeType, Value, and Data was
181 converted ASSERT() arguments specified by Filename, Description,
183 @retval FALSE The status code specified by CodeType, Value, and Data could
184 not be converted to ASSERT() arguments.
189 ReportStatusCodeExtractAssertInfo (
190 IN EFI_STATUS_CODE_TYPE CodeType
,
191 IN EFI_STATUS_CODE_VALUE Value
,
192 IN CONST EFI_STATUS_CODE_DATA
*Data
,
193 OUT CHAR8
**Filename
,
194 OUT CHAR8
**Description
,
195 OUT UINT32
*LineNumber
198 EFI_DEBUG_ASSERT_DATA
*AssertData
;
200 ASSERT (Data
!= NULL
);
201 ASSERT (Filename
!= NULL
);
202 ASSERT (Description
!= NULL
);
203 ASSERT (LineNumber
!= NULL
);
205 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) &&
206 ((CodeType
& EFI_STATUS_CODE_SEVERITY_MASK
) == EFI_ERROR_UNRECOVERED
) &&
207 ((Value
& EFI_STATUS_CODE_OPERATION_MASK
) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)) {
208 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)(Data
+ 1);
209 *Filename
= (CHAR8
*)(AssertData
+ 1);
210 *Description
= *Filename
+ AsciiStrLen (*Filename
) + 1;
211 *LineNumber
= AssertData
->LineNumber
;
219 Extracts DEBUG() information from a status code structure.
221 Converts the status code specified by Data to the DEBUG() arguments specified
222 by ErrorLevel, Marker, and Format. If type GUID in Data is
223 EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
224 Format from the optional data area of the status code buffer specified by Data.
225 The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
226 which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
227 the Format. If the DEBUG() information could be extracted from Data, then
228 return TRUE. Otherwise, FALSE is returned.
230 If Data is NULL, then ASSERT().
231 If ErrorLevel is NULL, then ASSERT().
232 If Marker is NULL, then ASSERT().
233 If Format is NULL, then ASSERT().
235 @param Data Pointer to status code data buffer.
236 @param ErrorLevel Pointer to error level mask for a debug message.
237 @param Marker Pointer to the variable argument list associated with Format.
238 @param Format Pointer to a Null-terminated ASCII format string of a
241 @retval TRUE The status code specified by Data was converted DEBUG() arguments
242 specified by ErrorLevel, Marker, and Format.
243 @retval FALSE The status code specified by Data could not be converted to
249 ReportStatusCodeExtractDebugInfo (
250 IN CONST EFI_STATUS_CODE_DATA
*Data
,
251 OUT UINT32
*ErrorLevel
,
252 OUT BASE_LIST
*Marker
,
256 EFI_DEBUG_INFO
*DebugInfo
;
258 ASSERT (Data
!= NULL
);
259 ASSERT (ErrorLevel
!= NULL
);
260 ASSERT (Marker
!= NULL
);
261 ASSERT (Format
!= NULL
);
264 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
266 if (!CompareGuid (&Data
->Type
, &gEfiStatusCodeDataTypeDebugGuid
)) {
271 // Retrieve the debug information from the status code record
273 DebugInfo
= (EFI_DEBUG_INFO
*)(Data
+ 1);
275 *ErrorLevel
= DebugInfo
->ErrorLevel
;
278 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
279 // of format in DEBUG string. Its address is returned in Marker and has to be 64-bit aligned.
280 // It must be noticed that EFI_DEBUG_INFO follows EFI_STATUS_CODE_DATA, whose size is
281 // 20 bytes. The size of EFI_DEBUG_INFO is 4 bytes, so we can ensure that Marker
282 // returned is 64-bit aligned.
283 // 64-bit aligned is a must, otherwise retrieving 64-bit parameter from BASE_LIST will
284 // cause unalignment exception.
286 *Marker
= (BASE_LIST
) (DebugInfo
+ 1);
287 *Format
= (CHAR8
*)(((UINT64
*)*Marker
) + 12);
294 Reports a status code.
296 Reports the status code specified by the parameters Type and Value. Status
297 code also require an instance, caller ID, and extended data. This function
298 passed in a zero instance, NULL extended data, and a caller ID of
299 gEfiCallerIdGuid, which is the GUID for the module.
301 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
302 is called while processing another any other Report Status Code Library function,
303 then ReportStatusCode() must return immediately.
305 @param Type Status code type.
306 @param Value Status code value.
308 @retval EFI_SUCCESS The status code was reported.
309 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
311 @retval EFI_UNSUPPORTED Report status code is not supported
317 IN EFI_STATUS_CODE_TYPE Type
,
318 IN EFI_STATUS_CODE_VALUE Value
321 return InternalReportStatusCode (Type
, Value
, 0, &gEfiCallerIdGuid
, NULL
);
326 Reports a status code with an extended data buffer.
328 Allocates and fills in the extended data section of a status code with the
329 extended data specified by ExtendedData and ExtendedDataSize. ExtendedData
330 is assumed to be one of the data structures specified in Related Definitions.
331 These data structure do not have the standard header, so this function is
332 responsible for allocating a buffer large enough for the standard header and
333 the extended data passed into this function. The standard header is filled
334 in with a GUID of gEfiStatusCodeSpecificDataGuid. The status code is reported
335 with a zero instance and a caller ID of gEfiCallerIdGuid.
337 ReportStatusCodeWithExtendedData()must actively prevent recursion. If
338 ReportStatusCodeWithExtendedData() is called while processing another any other
339 Report Status Code Library function, then ReportStatusCodeWithExtendedData()
340 must return EFI_DEVICE_ERROR immediately.
342 If ExtendedData is NULL, then ASSERT().
343 If ExtendedDataSize is 0, then ASSERT().
345 @param Type Status code type.
346 @param Value Status code value.
347 @param ExtendedData Pointer to the extended data buffer to be reported.
348 @param ExtendedDataSize The size, in bytes, of the extended data buffer to
351 @retval EFI_SUCCESS The status code was reported with the extended
352 data specified by ExtendedData and ExtendedDataSize.
353 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
354 extended data section.
355 @retval EFI_UNSUPPORTED Report status code is not supported
360 ReportStatusCodeWithExtendedData (
361 IN EFI_STATUS_CODE_TYPE Type
,
362 IN EFI_STATUS_CODE_VALUE Value
,
363 IN CONST VOID
*ExtendedData
,
364 IN UINTN ExtendedDataSize
367 ASSERT (ExtendedData
!= NULL
);
368 ASSERT (ExtendedDataSize
!= 0);
369 return ReportStatusCodeEx (
382 Reports a status code with full parameters.
384 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
385 is 0, then an extended data buffer is not reported. If ExtendedData is not
386 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
387 ExtendedData is assumed not have the standard status code header, so this function
388 is 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 in
390 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
391 GUID of gEfiStatusCodeSpecificDataGuid is used. The status code is reported with
392 an instance specified by Instance and a caller ID specified by CallerId. If
393 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
395 ReportStatusCodeEx()must actively prevent recursion. If
396 ReportStatusCodeEx() is called while processing another any
397 other Report Status Code Library function, then
398 ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
400 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
401 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
403 @param Type Status code type.
404 @param Value Status code value.
405 @param Instance Status code instance number.
406 @param CallerId Pointer to a GUID that identifies the caller of this
407 function. If this parameter is NULL, then a caller
408 ID of gEfiCallerIdGuid is used.
409 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
410 If this parameter is NULL, then a the status code
411 standard header is filled in with
412 gEfiStatusCodeSpecificDataGuid.
413 @param ExtendedData Pointer to the extended data buffer. This is an
414 optional parameter that may be NULL.
415 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
417 @retval EFI_SUCCESS The status code was reported.
418 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
419 the extended data section if it was specified.
420 @retval EFI_UNSUPPORTED Report status code is not supported
426 IN EFI_STATUS_CODE_TYPE Type
,
427 IN EFI_STATUS_CODE_VALUE Value
,
429 IN CONST EFI_GUID
*CallerId OPTIONAL
,
430 IN CONST EFI_GUID
*ExtendedDataGuid OPTIONAL
,
431 IN CONST VOID
*ExtendedData OPTIONAL
,
432 IN UINTN ExtendedDataSize
436 EFI_STATUS_CODE_DATA
*StatusCodeData
;
438 ASSERT (!((ExtendedData
== NULL
) && (ExtendedDataSize
!= 0)));
439 ASSERT (!((ExtendedData
!= NULL
) && (ExtendedDataSize
== 0)));
441 if (gSmst
== NULL
|| gSmst
->SmmAllocatePool
== NULL
|| gSmst
->SmmFreePool
== NULL
) {
442 return EFI_UNSUPPORTED
;
446 // Allocate space for the Status Code Header and its buffer
448 StatusCodeData
= NULL
;
449 gSmst
->SmmAllocatePool (EfiRuntimeServicesData
, sizeof (EFI_STATUS_CODE_DATA
) + ExtendedDataSize
, (VOID
**)&StatusCodeData
);
450 if (StatusCodeData
== NULL
) {
451 return EFI_OUT_OF_RESOURCES
;
455 // Fill in the extended data header
457 StatusCodeData
->HeaderSize
= sizeof (EFI_STATUS_CODE_DATA
);
458 StatusCodeData
->Size
= (UINT16
)ExtendedDataSize
;
459 if (ExtendedDataGuid
== NULL
) {
460 ExtendedDataGuid
= &gEfiStatusCodeSpecificDataGuid
;
462 CopyGuid (&StatusCodeData
->Type
, ExtendedDataGuid
);
465 // Fill in the extended data buffer
467 if (ExtendedData
!= NULL
) {
468 CopyMem (StatusCodeData
+ 1, ExtendedData
, ExtendedDataSize
);
472 // Report the status code
474 if (CallerId
== NULL
) {
475 CallerId
= &gEfiCallerIdGuid
;
477 Status
= InternalReportStatusCode (Type
, Value
, Instance
, CallerId
, StatusCodeData
);
480 // Free the allocated buffer
482 gSmst
->SmmFreePool (StatusCodeData
);
489 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
491 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
492 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
494 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
495 PcdReportStatusCodeProperyMask is set.
496 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
497 PcdReportStatusCodeProperyMask is clear.
502 ReportProgressCodeEnabled (
506 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
) != 0);
511 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
513 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
514 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
516 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
517 PcdReportStatusCodeProperyMask is set.
518 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
519 PcdReportStatusCodeProperyMask is clear.
524 ReportErrorCodeEnabled (
528 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
) != 0);
533 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
535 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
536 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
538 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
539 PcdReportStatusCodeProperyMask is set.
540 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
541 PcdReportStatusCodeProperyMask is clear.
546 ReportDebugCodeEnabled (
550 return (BOOLEAN
) ((PcdGet8 (PcdReportStatusCodePropertyMask
) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
) != 0);