]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Library / SmmReportStatusCodeLib / ReportStatusCodeLib.c
1 /** @file
2 Report Status Code Library for SMM Phase.
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Library/ReportStatusCodeLib.h>
10 #include <Library/DebugLib.h>
11 #include <Library/SmmServicesTableLib.h>
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/PcdLib.h>
15 #include <Library/MemoryAllocationLib.h>
16
17 #include <Guid/StatusCodeDataTypeId.h>
18 #include <Guid/StatusCodeDataTypeDebug.h>
19 #include <Protocol/SmmStatusCode.h>
20
21 EFI_SMM_REPORT_STATUS_CODE mReportStatusCode = NULL;
22 EFI_SMM_STATUS_CODE_PROTOCOL *mStatusCodeProtocol = NULL;
23
24
25 /**
26 Locate the report status code service.
27
28 @return Function pointer to the report status code service.
29 NULL is returned if no status code service is available.
30
31 **/
32 EFI_SMM_REPORT_STATUS_CODE
33 InternalGetReportStatusCode (
34 VOID
35 )
36 {
37 EFI_STATUS Status;
38
39 Status = gSmst->SmmLocateProtocol (&gEfiSmmStatusCodeProtocolGuid, NULL, (VOID**)&mStatusCodeProtocol);
40 if (!EFI_ERROR (Status) && mStatusCodeProtocol != NULL) {
41 return mStatusCodeProtocol->ReportStatusCode;
42 }
43 return NULL;
44 }
45
46 /**
47 Internal worker function that reports a status code through the status code service.
48
49 If status code service is not cached, then this function checks if status code service is
50 available in system. If status code service is not available, then EFI_UNSUPPORTED is
51 returned. If status code service is present, then it is cached in mReportStatusCode.
52 Finally this function reports status code through the status code service.
53
54 @param Type Status code type.
55 @param Value Status code value.
56 @param Instance Status code instance number.
57 @param CallerId Pointer to a GUID that identifies the caller of this
58 function. This is an optional parameter that may be
59 NULL.
60 @param Data Pointer to the extended data buffer. This is an
61 optional parameter that may be NULL.
62
63 @retval EFI_SUCCESS The status code was reported.
64 @retval EFI_UNSUPPORTED Status code service is not available.
65 @retval EFI_UNSUPPORTED Status code type is not supported.
66
67 **/
68 EFI_STATUS
69 InternalReportStatusCode (
70 IN EFI_STATUS_CODE_TYPE Type,
71 IN EFI_STATUS_CODE_VALUE Value,
72 IN UINT32 Instance,
73 IN CONST EFI_GUID *CallerId OPTIONAL,
74 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
75 )
76 {
77 if ((ReportProgressCodeEnabled() && ((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) ||
78 (ReportErrorCodeEnabled() && ((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) ||
79 (ReportDebugCodeEnabled() && ((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE)) {
80 //
81 // If mReportStatusCode is NULL, then check if status code service is available in system.
82 //
83 if (mReportStatusCode == NULL) {
84 mReportStatusCode = InternalGetReportStatusCode ();
85 if (mReportStatusCode == NULL) {
86 return EFI_UNSUPPORTED;
87 }
88 }
89
90 //
91 // A status code service is present in system, so pass in all the parameters to the service.
92 //
93 return (*mReportStatusCode) (mStatusCodeProtocol, Type, Value, Instance, (EFI_GUID *)CallerId, Data);
94 }
95
96 return EFI_UNSUPPORTED;
97 }
98
99
100 /**
101 Converts a status code to an 8-bit POST code value.
102
103 Converts the status code specified by CodeType and Value to an 8-bit POST code
104 and returns the 8-bit POST code in PostCode. If CodeType is an
105 EFI_PROGRESS_CODE or CodeType is an EFI_ERROR_CODE, then bits 0..4 of PostCode
106 are set to bits 16..20 of Value, and bits 5..7 of PostCode are set to bits
107 24..26 of Value., and TRUE is returned. Otherwise, FALSE is returned.
108
109 If PostCode is NULL, then ASSERT().
110
111 @param CodeType The type of status code being converted.
112 @param Value The status code value being converted.
113 @param PostCode A pointer to the 8-bit POST code value to return.
114
115 @retval TRUE The status code specified by CodeType and Value was converted
116 to an 8-bit POST code and returned in PostCode.
117 @retval FALSE The status code specified by CodeType and Value could not be
118 converted to an 8-bit POST code value.
119
120 **/
121 BOOLEAN
122 EFIAPI
123 CodeTypeToPostCode (
124 IN EFI_STATUS_CODE_TYPE CodeType,
125 IN EFI_STATUS_CODE_VALUE Value,
126 OUT UINT8 *PostCode
127 )
128 {
129 //
130 // If PostCode is NULL, then ASSERT()
131 //
132 ASSERT (PostCode != NULL);
133
134 //
135 // Convert Value to an 8 bit post code
136 //
137 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) ||
138 ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) ) {
139 *PostCode = (UINT8) ((((Value & EFI_STATUS_CODE_CLASS_MASK) >> 24) << 5) |
140 (((Value & EFI_STATUS_CODE_SUBCLASS_MASK) >> 16) & 0x1f));
141 return TRUE;
142 }
143 return FALSE;
144 }
145
146
147 /**
148 Extracts ASSERT() information from a status code structure.
149
150 Converts the status code specified by CodeType, Value, and Data to the ASSERT()
151 arguments specified by Filename, Description, and LineNumber. If CodeType is
152 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
153 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
154 Filename, Description, and LineNumber from the optional data area of the
155 status code buffer specified by Data. The optional data area of Data contains
156 a Null-terminated ASCII string for the FileName, followed by a Null-terminated
157 ASCII string for the Description, followed by a 32-bit LineNumber. If the
158 ASSERT() information could be extracted from Data, then return TRUE.
159 Otherwise, FALSE is returned.
160
161 If Data is NULL, then ASSERT().
162 If Filename is NULL, then ASSERT().
163 If Description is NULL, then ASSERT().
164 If LineNumber is NULL, then ASSERT().
165
166 @param CodeType The type of status code being converted.
167 @param Value The status code value being converted.
168 @param Data Pointer to status code data buffer.
169 @param Filename Pointer to the source file name that generated the ASSERT().
170 @param Description Pointer to the description of the ASSERT().
171 @param LineNumber Pointer to source line number that generated the ASSERT().
172
173 @retval TRUE The status code specified by CodeType, Value, and Data was
174 converted ASSERT() arguments specified by Filename, Description,
175 and LineNumber.
176 @retval FALSE The status code specified by CodeType, Value, and Data could
177 not be converted to ASSERT() arguments.
178
179 **/
180 BOOLEAN
181 EFIAPI
182 ReportStatusCodeExtractAssertInfo (
183 IN EFI_STATUS_CODE_TYPE CodeType,
184 IN EFI_STATUS_CODE_VALUE Value,
185 IN CONST EFI_STATUS_CODE_DATA *Data,
186 OUT CHAR8 **Filename,
187 OUT CHAR8 **Description,
188 OUT UINT32 *LineNumber
189 )
190 {
191 EFI_DEBUG_ASSERT_DATA *AssertData;
192
193 ASSERT (Data != NULL);
194 ASSERT (Filename != NULL);
195 ASSERT (Description != NULL);
196 ASSERT (LineNumber != NULL);
197
198 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) &&
199 ((CodeType & EFI_STATUS_CODE_SEVERITY_MASK) == EFI_ERROR_UNRECOVERED) &&
200 ((Value & EFI_STATUS_CODE_OPERATION_MASK) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)) {
201 AssertData = (EFI_DEBUG_ASSERT_DATA *)(Data + 1);
202 *Filename = (CHAR8 *)(AssertData + 1);
203 *Description = *Filename + AsciiStrLen (*Filename) + 1;
204 *LineNumber = AssertData->LineNumber;
205 return TRUE;
206 }
207 return FALSE;
208 }
209
210
211 /**
212 Extracts DEBUG() information from a status code structure.
213
214 Converts the status code specified by Data to the DEBUG() arguments specified
215 by ErrorLevel, Marker, and Format. If type GUID in Data is
216 EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
217 Format from the optional data area of the status code buffer specified by Data.
218 The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
219 which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
220 the Format. If the DEBUG() information could be extracted from Data, then
221 return TRUE. Otherwise, FALSE is returned.
222
223 If Data is NULL, then ASSERT().
224 If ErrorLevel is NULL, then ASSERT().
225 If Marker is NULL, then ASSERT().
226 If Format is NULL, then ASSERT().
227
228 @param Data Pointer to status code data buffer.
229 @param ErrorLevel Pointer to error level mask for a debug message.
230 @param Marker Pointer to the variable argument list associated with Format.
231 @param Format Pointer to a Null-terminated ASCII format string of a
232 debug message.
233
234 @retval TRUE The status code specified by Data was converted DEBUG() arguments
235 specified by ErrorLevel, Marker, and Format.
236 @retval FALSE The status code specified by Data could not be converted to
237 DEBUG() arguments.
238
239 **/
240 BOOLEAN
241 EFIAPI
242 ReportStatusCodeExtractDebugInfo (
243 IN CONST EFI_STATUS_CODE_DATA *Data,
244 OUT UINT32 *ErrorLevel,
245 OUT BASE_LIST *Marker,
246 OUT CHAR8 **Format
247 )
248 {
249 EFI_DEBUG_INFO *DebugInfo;
250
251 ASSERT (Data != NULL);
252 ASSERT (ErrorLevel != NULL);
253 ASSERT (Marker != NULL);
254 ASSERT (Format != NULL);
255
256 //
257 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
258 //
259 if (!CompareGuid (&Data->Type, &gEfiStatusCodeDataTypeDebugGuid)) {
260 return FALSE;
261 }
262
263 //
264 // Retrieve the debug information from the status code record
265 //
266 DebugInfo = (EFI_DEBUG_INFO *)(Data + 1);
267
268 *ErrorLevel = DebugInfo->ErrorLevel;
269
270 //
271 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
272 // of format in DEBUG string. Its address is returned in Marker and has to be 64-bit aligned.
273 // It must be noticed that EFI_DEBUG_INFO follows EFI_STATUS_CODE_DATA, whose size is
274 // 20 bytes. The size of EFI_DEBUG_INFO is 4 bytes, so we can ensure that Marker
275 // returned is 64-bit aligned.
276 // 64-bit aligned is a must, otherwise retrieving 64-bit parameter from BASE_LIST will
277 // cause unalignment exception.
278 //
279 *Marker = (BASE_LIST) (DebugInfo + 1);
280 *Format = (CHAR8 *)(((UINT64 *)*Marker) + 12);
281
282 return TRUE;
283 }
284
285
286 /**
287 Reports a status code.
288
289 Reports the status code specified by the parameters Type and Value. Status
290 code also require an instance, caller ID, and extended data. This function
291 passed in a zero instance, NULL extended data, and a caller ID of
292 gEfiCallerIdGuid, which is the GUID for the module.
293
294 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
295 is called while processing another any other Report Status Code Library function,
296 then ReportStatusCode() must return immediately.
297
298 @param Type Status code type.
299 @param Value Status code value.
300
301 @retval EFI_SUCCESS The status code was reported.
302 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
303 device error.
304 @retval EFI_UNSUPPORTED Report status code is not supported
305
306 **/
307 EFI_STATUS
308 EFIAPI
309 ReportStatusCode (
310 IN EFI_STATUS_CODE_TYPE Type,
311 IN EFI_STATUS_CODE_VALUE Value
312 )
313 {
314 return InternalReportStatusCode (Type, Value, 0, &gEfiCallerIdGuid, NULL);
315 }
316
317
318 /**
319 Reports a status code with an extended data buffer.
320
321 Allocates and fills in the extended data section of a status code with the
322 extended data specified by ExtendedData and ExtendedDataSize. ExtendedData
323 is assumed to be one of the data structures specified in Related Definitions.
324 These data structure do not have the standard header, so this function is
325 responsible for allocating a buffer large enough for the standard header and
326 the extended data passed into this function. The standard header is filled
327 in with a GUID of gEfiStatusCodeSpecificDataGuid. The status code is reported
328 with a zero instance and a caller ID of gEfiCallerIdGuid.
329
330 ReportStatusCodeWithExtendedData()must actively prevent recursion. If
331 ReportStatusCodeWithExtendedData() is called while processing another any other
332 Report Status Code Library function, then ReportStatusCodeWithExtendedData()
333 must return EFI_DEVICE_ERROR immediately.
334
335 If ExtendedData is NULL, then ASSERT().
336 If ExtendedDataSize is 0, then ASSERT().
337
338 @param Type Status code type.
339 @param Value Status code value.
340 @param ExtendedData Pointer to the extended data buffer to be reported.
341 @param ExtendedDataSize The size, in bytes, of the extended data buffer to
342 be reported.
343
344 @retval EFI_SUCCESS The status code was reported with the extended
345 data specified by ExtendedData and ExtendedDataSize.
346 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
347 extended data section.
348 @retval EFI_UNSUPPORTED Report status code is not supported
349
350 **/
351 EFI_STATUS
352 EFIAPI
353 ReportStatusCodeWithExtendedData (
354 IN EFI_STATUS_CODE_TYPE Type,
355 IN EFI_STATUS_CODE_VALUE Value,
356 IN CONST VOID *ExtendedData,
357 IN UINTN ExtendedDataSize
358 )
359 {
360 ASSERT (ExtendedData != NULL);
361 ASSERT (ExtendedDataSize != 0);
362 return ReportStatusCodeEx (
363 Type,
364 Value,
365 0,
366 NULL,
367 NULL,
368 ExtendedData,
369 ExtendedDataSize
370 );
371 }
372
373
374 /**
375 Reports a status code with full parameters.
376
377 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
378 is 0, then an extended data buffer is not reported. If ExtendedData is not
379 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
380 ExtendedData is assumed not have the standard status code header, so this function
381 is responsible for allocating a buffer large enough for the standard header and
382 the extended data passed into this function. The standard header is filled in
383 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
384 GUID of gEfiStatusCodeSpecificDataGuid is used. The status code is reported with
385 an instance specified by Instance and a caller ID specified by CallerId. If
386 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
387
388 ReportStatusCodeEx()must actively prevent recursion. If
389 ReportStatusCodeEx() is called while processing another any
390 other Report Status Code Library function, then
391 ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
392
393 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
394 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
395
396 @param Type Status code type.
397 @param Value Status code value.
398 @param Instance Status code instance number.
399 @param CallerId Pointer to a GUID that identifies the caller of this
400 function. If this parameter is NULL, then a caller
401 ID of gEfiCallerIdGuid is used.
402 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
403 If this parameter is NULL, then a the status code
404 standard header is filled in with
405 gEfiStatusCodeSpecificDataGuid.
406 @param ExtendedData Pointer to the extended data buffer. This is an
407 optional parameter that may be NULL.
408 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
409
410 @retval EFI_SUCCESS The status code was reported.
411 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
412 the extended data section if it was specified.
413 @retval EFI_UNSUPPORTED Report status code is not supported
414
415 **/
416 EFI_STATUS
417 EFIAPI
418 ReportStatusCodeEx (
419 IN EFI_STATUS_CODE_TYPE Type,
420 IN EFI_STATUS_CODE_VALUE Value,
421 IN UINT32 Instance,
422 IN CONST EFI_GUID *CallerId OPTIONAL,
423 IN CONST EFI_GUID *ExtendedDataGuid OPTIONAL,
424 IN CONST VOID *ExtendedData OPTIONAL,
425 IN UINTN ExtendedDataSize
426 )
427 {
428 EFI_STATUS Status;
429 EFI_STATUS_CODE_DATA *StatusCodeData;
430
431 ASSERT (!((ExtendedData == NULL) && (ExtendedDataSize != 0)));
432 ASSERT (!((ExtendedData != NULL) && (ExtendedDataSize == 0)));
433
434 //
435 // Allocate space for the Status Code Header and its buffer
436 //
437 StatusCodeData = AllocatePool (sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize);
438 if (StatusCodeData == NULL) {
439 return EFI_OUT_OF_RESOURCES;
440 }
441
442 //
443 // Fill in the extended data header
444 //
445 StatusCodeData->HeaderSize = (UINT16) sizeof (EFI_STATUS_CODE_DATA);
446 StatusCodeData->Size = (UINT16) ExtendedDataSize;
447 if (ExtendedDataGuid == NULL) {
448 ExtendedDataGuid = &gEfiStatusCodeSpecificDataGuid;
449 }
450 CopyGuid (&StatusCodeData->Type, ExtendedDataGuid);
451
452 //
453 // Fill in the extended data buffer
454 //
455 if (ExtendedData != NULL) {
456 CopyMem (StatusCodeData + 1, ExtendedData, ExtendedDataSize);
457 }
458
459 //
460 // Report the status code
461 //
462 if (CallerId == NULL) {
463 CallerId = &gEfiCallerIdGuid;
464 }
465 Status = InternalReportStatusCode (Type, Value, Instance, CallerId, StatusCodeData);
466
467 //
468 // Free the allocated buffer
469 //
470 FreePool (StatusCodeData);
471
472 return Status;
473 }
474
475
476 /**
477 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
478
479 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
480 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
481
482 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
483 PcdReportStatusCodeProperyMask is set.
484 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
485 PcdReportStatusCodeProperyMask is clear.
486
487 **/
488 BOOLEAN
489 EFIAPI
490 ReportProgressCodeEnabled (
491 VOID
492 )
493 {
494 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED) != 0);
495 }
496
497
498 /**
499 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
500
501 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
502 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
503
504 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
505 PcdReportStatusCodeProperyMask is set.
506 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
507 PcdReportStatusCodeProperyMask is clear.
508
509 **/
510 BOOLEAN
511 EFIAPI
512 ReportErrorCodeEnabled (
513 VOID
514 )
515 {
516 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED) != 0);
517 }
518
519
520 /**
521 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
522
523 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
524 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
525
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.
530
531 **/
532 BOOLEAN
533 EFIAPI
534 ReportDebugCodeEnabled (
535 VOID
536 )
537 {
538 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED) != 0);
539 }