]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c
MdeModulePkg: SmmReportStatusCodeLib: ReportStatusCodeLib in StandaloneMm
[mirror_edk2.git] / MdeModulePkg / Library / SmmReportStatusCodeLib / ReportStatusCodeLib.c
1 /** @file
2 Report Status Code Library for MM 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/MmServicesTableLib.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/MmStatusCode.h>
20
21 #include "ReportStatusCodeLib.h"
22
23 EFI_MM_REPORT_STATUS_CODE mReportStatusCode = NULL;
24 EFI_MM_STATUS_CODE_PROTOCOL *mStatusCodeProtocol = NULL;
25
26
27 /**
28 Locate the report status code service.
29
30 @return Function pointer to the report status code service.
31 NULL is returned if no status code service is available.
32
33 **/
34 EFI_MM_REPORT_STATUS_CODE
35 InternalGetReportStatusCode (
36 VOID
37 )
38 {
39 EFI_STATUS Status;
40
41 Status = InternalLocateProtocol (&gEfiMmStatusCodeProtocolGuid, NULL, (VOID**)&mStatusCodeProtocol);
42 if (!EFI_ERROR (Status) && mStatusCodeProtocol != NULL) {
43 return mStatusCodeProtocol->ReportStatusCode;
44 }
45 return NULL;
46 }
47
48 /**
49 Internal worker function that reports a status code through the status code service.
50
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.
55
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
61 NULL.
62 @param Data Pointer to the extended data buffer. This is an
63 optional parameter that may be NULL.
64
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.
68
69 **/
70 EFI_STATUS
71 InternalReportStatusCode (
72 IN EFI_STATUS_CODE_TYPE Type,
73 IN EFI_STATUS_CODE_VALUE Value,
74 IN UINT32 Instance,
75 IN CONST EFI_GUID *CallerId OPTIONAL,
76 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
77 )
78 {
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)) {
82 //
83 // If mReportStatusCode is NULL, then check if status code service is available in system.
84 //
85 if (mReportStatusCode == NULL) {
86 mReportStatusCode = InternalGetReportStatusCode ();
87 if (mReportStatusCode == NULL) {
88 return EFI_UNSUPPORTED;
89 }
90 }
91
92 //
93 // A status code service is present in system, so pass in all the parameters to the service.
94 //
95 return (*mReportStatusCode) (mStatusCodeProtocol, Type, Value, Instance, (EFI_GUID *)CallerId, Data);
96 }
97
98 return EFI_UNSUPPORTED;
99 }
100
101
102 /**
103 Converts a status code to an 8-bit POST code value.
104
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.
110
111 If PostCode is NULL, then ASSERT().
112
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.
116
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.
121
122 **/
123 BOOLEAN
124 EFIAPI
125 CodeTypeToPostCode (
126 IN EFI_STATUS_CODE_TYPE CodeType,
127 IN EFI_STATUS_CODE_VALUE Value,
128 OUT UINT8 *PostCode
129 )
130 {
131 //
132 // If PostCode is NULL, then ASSERT()
133 //
134 ASSERT (PostCode != NULL);
135
136 //
137 // Convert Value to an 8 bit post code
138 //
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));
143 return TRUE;
144 }
145 return FALSE;
146 }
147
148
149 /**
150 Extracts ASSERT() information from a status code structure.
151
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.
162
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().
167
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().
174
175 @retval TRUE The status code specified by CodeType, Value, and Data was
176 converted ASSERT() arguments specified by Filename, Description,
177 and LineNumber.
178 @retval FALSE The status code specified by CodeType, Value, and Data could
179 not be converted to ASSERT() arguments.
180
181 **/
182 BOOLEAN
183 EFIAPI
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
191 )
192 {
193 EFI_DEBUG_ASSERT_DATA *AssertData;
194
195 ASSERT (Data != NULL);
196 ASSERT (Filename != NULL);
197 ASSERT (Description != NULL);
198 ASSERT (LineNumber != NULL);
199
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;
207 return TRUE;
208 }
209 return FALSE;
210 }
211
212
213 /**
214 Extracts DEBUG() information from a status code structure.
215
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.
224
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().
229
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
234 debug message.
235
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
239 DEBUG() arguments.
240
241 **/
242 BOOLEAN
243 EFIAPI
244 ReportStatusCodeExtractDebugInfo (
245 IN CONST EFI_STATUS_CODE_DATA *Data,
246 OUT UINT32 *ErrorLevel,
247 OUT BASE_LIST *Marker,
248 OUT CHAR8 **Format
249 )
250 {
251 EFI_DEBUG_INFO *DebugInfo;
252
253 ASSERT (Data != NULL);
254 ASSERT (ErrorLevel != NULL);
255 ASSERT (Marker != NULL);
256 ASSERT (Format != NULL);
257
258 //
259 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
260 //
261 if (!CompareGuid (&Data->Type, &gEfiStatusCodeDataTypeDebugGuid)) {
262 return FALSE;
263 }
264
265 //
266 // Retrieve the debug information from the status code record
267 //
268 DebugInfo = (EFI_DEBUG_INFO *)(Data + 1);
269
270 *ErrorLevel = DebugInfo->ErrorLevel;
271
272 //
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.
280 //
281 *Marker = (BASE_LIST) (DebugInfo + 1);
282 *Format = (CHAR8 *)(((UINT64 *)*Marker) + 12);
283
284 return TRUE;
285 }
286
287
288 /**
289 Reports a status code.
290
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.
295
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.
299
300 @param Type Status code type.
301 @param Value Status code value.
302
303 @retval EFI_SUCCESS The status code was reported.
304 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
305 device error.
306 @retval EFI_UNSUPPORTED Report status code is not supported
307
308 **/
309 EFI_STATUS
310 EFIAPI
311 ReportStatusCode (
312 IN EFI_STATUS_CODE_TYPE Type,
313 IN EFI_STATUS_CODE_VALUE Value
314 )
315 {
316 return InternalReportStatusCode (Type, Value, 0, &gEfiCallerIdGuid, NULL);
317 }
318
319
320 /**
321 Reports a status code with an extended data buffer.
322
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.
331
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.
336
337 If ExtendedData is NULL, then ASSERT().
338 If ExtendedDataSize is 0, then ASSERT().
339
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
344 be reported.
345
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
351
352 **/
353 EFI_STATUS
354 EFIAPI
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
360 )
361 {
362 ASSERT (ExtendedData != NULL);
363 ASSERT (ExtendedDataSize != 0);
364 return ReportStatusCodeEx (
365 Type,
366 Value,
367 0,
368 NULL,
369 NULL,
370 ExtendedData,
371 ExtendedDataSize
372 );
373 }
374
375
376 /**
377 Reports a status code with full parameters.
378
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.
389
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.
394
395 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
396 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
397
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.
411
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
416
417 **/
418 EFI_STATUS
419 EFIAPI
420 ReportStatusCodeEx (
421 IN EFI_STATUS_CODE_TYPE Type,
422 IN EFI_STATUS_CODE_VALUE Value,
423 IN UINT32 Instance,
424 IN CONST EFI_GUID *CallerId OPTIONAL,
425 IN CONST EFI_GUID *ExtendedDataGuid OPTIONAL,
426 IN CONST VOID *ExtendedData OPTIONAL,
427 IN UINTN ExtendedDataSize
428 )
429 {
430 EFI_STATUS Status;
431 EFI_STATUS_CODE_DATA *StatusCodeData;
432
433 ASSERT (!((ExtendedData == NULL) && (ExtendedDataSize != 0)));
434 ASSERT (!((ExtendedData != NULL) && (ExtendedDataSize == 0)));
435
436 //
437 // Allocate space for the Status Code Header and its buffer
438 //
439 StatusCodeData = AllocatePool (sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize);
440 if (StatusCodeData == NULL) {
441 return EFI_OUT_OF_RESOURCES;
442 }
443
444 //
445 // Fill in the extended data header
446 //
447 StatusCodeData->HeaderSize = (UINT16) sizeof (EFI_STATUS_CODE_DATA);
448 StatusCodeData->Size = (UINT16) ExtendedDataSize;
449 if (ExtendedDataGuid == NULL) {
450 ExtendedDataGuid = &gEfiStatusCodeSpecificDataGuid;
451 }
452 CopyGuid (&StatusCodeData->Type, ExtendedDataGuid);
453
454 //
455 // Fill in the extended data buffer
456 //
457 if (ExtendedData != NULL) {
458 CopyMem (StatusCodeData + 1, ExtendedData, ExtendedDataSize);
459 }
460
461 //
462 // Report the status code
463 //
464 if (CallerId == NULL) {
465 CallerId = &gEfiCallerIdGuid;
466 }
467 Status = InternalReportStatusCode (Type, Value, Instance, CallerId, StatusCodeData);
468
469 //
470 // Free the allocated buffer
471 //
472 FreePool (StatusCodeData);
473
474 return Status;
475 }
476
477
478 /**
479 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
480
481 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
482 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
483
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.
488
489 **/
490 BOOLEAN
491 EFIAPI
492 ReportProgressCodeEnabled (
493 VOID
494 )
495 {
496 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED) != 0);
497 }
498
499
500 /**
501 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
502
503 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
504 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
505
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.
510
511 **/
512 BOOLEAN
513 EFIAPI
514 ReportErrorCodeEnabled (
515 VOID
516 )
517 {
518 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED) != 0);
519 }
520
521
522 /**
523 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
524
525 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
526 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
527
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.
532
533 **/
534 BOOLEAN
535 EFIAPI
536 ReportDebugCodeEnabled (
537 VOID
538 )
539 {
540 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED) != 0);
541 }