]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c
9bbfa95e2baeac80910ff4ee718e5e0113f3abdd
[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 <FrameworkDxe.h>
16
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>
23
24 #include <Guid/StatusCodeDataTypeId.h>
25 #include <Guid/StatusCodeDataTypeDebug.h>
26 #include <Protocol/SmmStatusCode.h>
27
28 EFI_SMM_REPORT_STATUS_CODE mReportStatusCode = NULL;
29 EFI_SMM_STATUS_CODE_PROTOCOL *mStatusCodeProtocol = NULL;
30
31
32 /**
33 Locate the report status code service.
34
35 @return Function pointer to the report status code service.
36 NULL is returned if no status code service is available.
37
38 **/
39 EFI_SMM_REPORT_STATUS_CODE
40 InternalGetReportStatusCode (
41 VOID
42 )
43 {
44 EFI_STATUS Status;
45
46 Status = gSmst->SmmLocateProtocol (&gEfiSmmStatusCodeProtocolGuid, NULL, (VOID**)&mStatusCodeProtocol);
47 if (!EFI_ERROR (Status) && mStatusCodeProtocol != NULL) {
48 return mStatusCodeProtocol->ReportStatusCode;
49 }
50 return NULL;
51 }
52
53 /**
54 Internal worker function that reports a status code through the status code service.
55
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.
60
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
66 NULL.
67 @param Data Pointer to the extended data buffer. This is an
68 optional parameter that may be NULL.
69
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.
73
74 **/
75 EFI_STATUS
76 InternalReportStatusCode (
77 IN EFI_STATUS_CODE_TYPE Type,
78 IN EFI_STATUS_CODE_VALUE Value,
79 IN UINT32 Instance,
80 IN CONST EFI_GUID *CallerId OPTIONAL,
81 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
82 )
83 {
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)) {
87 //
88 // If mReportStatusCode is NULL, then check if status code service is available in system.
89 //
90 if (mReportStatusCode == NULL) {
91 mReportStatusCode = InternalGetReportStatusCode ();
92 if (mReportStatusCode == NULL) {
93 return EFI_UNSUPPORTED;
94 }
95 }
96
97 //
98 // A status code service is present in system, so pass in all the parameters to the service.
99 //
100 return (*mReportStatusCode) (mStatusCodeProtocol, Type, Value, Instance, (EFI_GUID *)CallerId, Data);
101 }
102
103 return EFI_UNSUPPORTED;
104 }
105
106
107 /**
108 Converts a status code to an 8-bit POST code value.
109
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.
115
116 If PostCode is NULL, then ASSERT().
117
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.
121
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.
126
127 **/
128 BOOLEAN
129 EFIAPI
130 CodeTypeToPostCode (
131 IN EFI_STATUS_CODE_TYPE CodeType,
132 IN EFI_STATUS_CODE_VALUE Value,
133 OUT UINT8 *PostCode
134 )
135 {
136 //
137 // If PostCode is NULL, then ASSERT()
138 //
139 ASSERT (PostCode != NULL);
140
141 //
142 // Convert Value to an 8 bit post code
143 //
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));
148 return TRUE;
149 }
150 return FALSE;
151 }
152
153
154 /**
155 Extracts ASSERT() information from a status code structure.
156
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.
167
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().
172
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().
179
180 @retval TRUE The status code specified by CodeType, Value, and Data was
181 converted ASSERT() arguments specified by Filename, Description,
182 and LineNumber.
183 @retval FALSE The status code specified by CodeType, Value, and Data could
184 not be converted to ASSERT() arguments.
185
186 **/
187 BOOLEAN
188 EFIAPI
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
196 )
197 {
198 EFI_DEBUG_ASSERT_DATA *AssertData;
199
200 ASSERT (Data != NULL);
201 ASSERT (Filename != NULL);
202 ASSERT (Description != NULL);
203 ASSERT (LineNumber != NULL);
204
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;
212 return TRUE;
213 }
214 return FALSE;
215 }
216
217
218 /**
219 Extracts DEBUG() information from a status code structure.
220
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.
229
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().
234
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
239 debug message.
240
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
244 DEBUG() arguments.
245
246 **/
247 BOOLEAN
248 EFIAPI
249 ReportStatusCodeExtractDebugInfo (
250 IN CONST EFI_STATUS_CODE_DATA *Data,
251 OUT UINT32 *ErrorLevel,
252 OUT BASE_LIST *Marker,
253 OUT CHAR8 **Format
254 )
255 {
256 EFI_DEBUG_INFO *DebugInfo;
257
258 ASSERT (Data != NULL);
259 ASSERT (ErrorLevel != NULL);
260 ASSERT (Marker != NULL);
261 ASSERT (Format != NULL);
262
263 //
264 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
265 //
266 if (!CompareGuid (&Data->Type, &gEfiStatusCodeDataTypeDebugGuid)) {
267 return FALSE;
268 }
269
270 //
271 // Retrieve the debug information from the status code record
272 //
273 DebugInfo = (EFI_DEBUG_INFO *)(Data + 1);
274
275 *ErrorLevel = DebugInfo->ErrorLevel;
276
277 //
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.
285 //
286 *Marker = (BASE_LIST) (DebugInfo + 1);
287 *Format = (CHAR8 *)(((UINT64 *)*Marker) + 12);
288
289 return TRUE;
290 }
291
292
293 /**
294 Reports a status code.
295
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.
300
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.
304
305 @param Type Status code type.
306 @param Value Status code value.
307
308 @retval EFI_SUCCESS The status code was reported.
309 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
310 device error.
311 @retval EFI_UNSUPPORTED Report status code is not supported
312
313 **/
314 EFI_STATUS
315 EFIAPI
316 ReportStatusCode (
317 IN EFI_STATUS_CODE_TYPE Type,
318 IN EFI_STATUS_CODE_VALUE Value
319 )
320 {
321 return InternalReportStatusCode (Type, Value, 0, &gEfiCallerIdGuid, NULL);
322 }
323
324
325 /**
326 Reports a status code with an extended data buffer.
327
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.
336
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.
341
342 If ExtendedData is NULL, then ASSERT().
343 If ExtendedDataSize is 0, then ASSERT().
344
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
349 be reported.
350
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
356
357 **/
358 EFI_STATUS
359 EFIAPI
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
365 )
366 {
367 ASSERT (ExtendedData != NULL);
368 ASSERT (ExtendedDataSize != 0);
369 return ReportStatusCodeEx (
370 Type,
371 Value,
372 0,
373 NULL,
374 NULL,
375 ExtendedData,
376 ExtendedDataSize
377 );
378 }
379
380
381 /**
382 Reports a status code with full parameters.
383
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.
394
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.
399
400 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
401 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
402
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.
416
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
421
422 **/
423 EFI_STATUS
424 EFIAPI
425 ReportStatusCodeEx (
426 IN EFI_STATUS_CODE_TYPE Type,
427 IN EFI_STATUS_CODE_VALUE Value,
428 IN UINT32 Instance,
429 IN CONST EFI_GUID *CallerId OPTIONAL,
430 IN CONST EFI_GUID *ExtendedDataGuid OPTIONAL,
431 IN CONST VOID *ExtendedData OPTIONAL,
432 IN UINTN ExtendedDataSize
433 )
434 {
435 EFI_STATUS Status;
436 EFI_STATUS_CODE_DATA *StatusCodeData;
437
438 ASSERT (!((ExtendedData == NULL) && (ExtendedDataSize != 0)));
439 ASSERT (!((ExtendedData != NULL) && (ExtendedDataSize == 0)));
440
441 if (gSmst == NULL || gSmst->SmmAllocatePool == NULL || gSmst->SmmFreePool == NULL) {
442 return EFI_UNSUPPORTED;
443 }
444
445 //
446 // Allocate space for the Status Code Header and its buffer
447 //
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;
452 }
453
454 //
455 // Fill in the extended data header
456 //
457 StatusCodeData->HeaderSize = sizeof (EFI_STATUS_CODE_DATA);
458 StatusCodeData->Size = (UINT16)ExtendedDataSize;
459 if (ExtendedDataGuid == NULL) {
460 ExtendedDataGuid = &gEfiStatusCodeSpecificDataGuid;
461 }
462 CopyGuid (&StatusCodeData->Type, ExtendedDataGuid);
463
464 //
465 // Fill in the extended data buffer
466 //
467 if (ExtendedData != NULL) {
468 CopyMem (StatusCodeData + 1, ExtendedData, ExtendedDataSize);
469 }
470
471 //
472 // Report the status code
473 //
474 if (CallerId == NULL) {
475 CallerId = &gEfiCallerIdGuid;
476 }
477 Status = InternalReportStatusCode (Type, Value, Instance, CallerId, StatusCodeData);
478
479 //
480 // Free the allocated buffer
481 //
482 gSmst->SmmFreePool (StatusCodeData);
483
484 return Status;
485 }
486
487
488 /**
489 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
490
491 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
492 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
493
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.
498
499 **/
500 BOOLEAN
501 EFIAPI
502 ReportProgressCodeEnabled (
503 VOID
504 )
505 {
506 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED) != 0);
507 }
508
509
510 /**
511 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
512
513 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
514 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
515
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.
520
521 **/
522 BOOLEAN
523 EFIAPI
524 ReportErrorCodeEnabled (
525 VOID
526 )
527 {
528 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED) != 0);
529 }
530
531
532 /**
533 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
534
535 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
536 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
537
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.
542
543 **/
544 BOOLEAN
545 EFIAPI
546 ReportDebugCodeEnabled (
547 VOID
548 )
549 {
550 return (BOOLEAN) ((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED) != 0);
551 }