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