]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/SmmRuntimeDxeReportStatusCodeLibFramework/ReportStatusCodeLib.c
fix Klocwork issues
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / SmmRuntimeDxeReportStatusCodeLibFramework / ReportStatusCodeLib.c
1 /** @file
2 Report Status Code Library for SMM Runtime driver.
3
4 Copyright (c) 2006 - 2007, 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 "ReportStatusCodeLibInternal.h"
16
17
18
19 EFI_REPORT_STATUS_CODE mReportStatusCode = NULL;
20
21 /**
22 Internal worker function that reports a status code through the Status Code Protocol
23
24 This function checks to see if a Status Code Protocol is present in the handle
25 database. If a Status Code Protocol is not present, then EFI_UNSUPPORTED is
26 returned. If a Status Code Protocol is present, then it is cached in gStatusCode,
27 and the ReportStatusCode() service of the Status Code Protocol is called passing in
28 Type, Value, Instance, CallerId, and Data. The result of this call is returned.
29
30 @param Type Status code type.
31 @param Value Status code value.
32 @param Instance Status code instance number.
33 @param CallerId Pointer to a GUID that identifies the caller of this
34 function. This is an optional parameter that may be
35 NULL.
36 @param Data Pointer to the extended data buffer. This is an
37 optional parameter that may be NULL.
38
39 @retval EFI_SUCCESS The status code was reported.
40 @retval EFI_OUT_OF_RESOURCES There were not enough resources to report the status code.
41 @retval EFI_UNSUPPORTED Status Code Protocol is not available.
42
43 **/
44 EFI_STATUS
45 InternalReportStatusCode (
46 IN EFI_STATUS_CODE_TYPE Type,
47 IN EFI_STATUS_CODE_VALUE Value,
48 IN UINT32 Instance,
49 IN CONST EFI_GUID *CallerId OPTIONAL,
50 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
51 )
52 {
53 //
54 // If gStatusCode is NULL, then see if a Status Code Protocol instance is present
55 // in the handle database.
56 //
57 if (mReportStatusCode == NULL) {
58 mReportStatusCode = InternalGetReportStatusCode ();
59 if (mReportStatusCode == NULL) {
60 return EFI_UNSUPPORTED;
61 }
62 }
63
64 //
65 // A Status Code Protocol is present in the handle database, so pass in all the
66 // parameters to the ReportStatusCode() service of the Status Code Protocol
67 //
68 return (*mReportStatusCode) (Type, Value, Instance, (EFI_GUID *)CallerId, Data);
69 }
70
71
72 /**
73 Computes and returns the size, in bytes, of a device path.
74
75 @param DevicePath A pointer to a device path.
76
77 @return The size, in bytes, of DevicePath.
78
79 **/
80 UINTN
81 InternalReportStatusCodeDevicePathSize (
82 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
83 )
84 {
85 CONST EFI_DEVICE_PATH_PROTOCOL *Start;
86
87 //
88 // Search for the end of the device path structure
89 //
90 Start = DevicePath;
91 while (!IsDevicePathEnd (DevicePath)) {
92 DevicePath = NextDevicePathNode (DevicePath);
93 }
94
95 //
96 // Subtract the start node from the end node and add in the size of the end node
97 //
98 return ((UINTN) DevicePath - (UINTN) Start) + DevicePathNodeLength (DevicePath);
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 VA_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 * UINTN bytes of the string are really an
274 // argument stack to support varargs on the Format string.
275 //
276 *Marker = (VA_LIST) (DebugInfo + 1);
277 *Format = (CHAR8 *)(((UINT64 *)*Marker) + 12);
278
279 return TRUE;
280 }
281
282
283 /**
284 Reports a status code.
285
286 Reports the status code specified by the parameters Type and Value. Status
287 code also require an instance, caller ID, and extended data. This function
288 passed in a zero instance, NULL extended data, and a caller ID of
289 gEfiCallerIdGuid, which is the GUID for the module.
290
291 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
292 is called while processing another any other Report Status Code Library function,
293 then ReportStatusCode() must return immediately.
294
295 @param Type Status code type.
296 @param Value Status code value.
297
298 @retval EFI_SUCCESS The status code was reported.
299 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
300 device error.
301 @retval EFI_UNSUPPORTED Report status code is not supported
302
303 **/
304 EFI_STATUS
305 EFIAPI
306 ReportStatusCode (
307 IN EFI_STATUS_CODE_TYPE Type,
308 IN EFI_STATUS_CODE_VALUE Value
309 )
310 {
311 return InternalReportStatusCode (Type, Value, 0, &gEfiCallerIdGuid, NULL);
312 }
313
314
315 /**
316 Reports a status code with a Device Path Protocol as the extended data.
317
318 Allocates and fills in the extended data section of a status code with the
319 Device Path Protocol specified by DevicePath. This function is responsible
320 for allocating a buffer large enough for the standard header and the device
321 path. The standard header is filled in with a GUID of
322 gEfiStatusCodeSpecificDataGuid. The status code is reported with a zero
323 instance and a caller ID of gEfiCallerIdGuid.
324
325 ReportStatusCodeWithDevicePath()must actively prevent recursion. If
326 ReportStatusCodeWithDevicePath() is called while processing another any other
327 Report Status Code Library function, then ReportStatusCodeWithDevicePath()
328 must return EFI_DEVICE_ERROR immediately.
329
330 If DevicePath is NULL, then ASSERT().
331
332 @param Type Status code type.
333 @param Value Status code value.
334 @param DevicePath Pointer to the Device Path Protocol to be reported.
335
336 @retval EFI_SUCCESS The status code was reported with the extended
337 data specified by DevicePath.
338 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
339 extended data section.
340 @retval EFI_UNSUPPORTED Report status code is not supported
341
342 **/
343 EFI_STATUS
344 EFIAPI
345 ReportStatusCodeWithDevicePath (
346 IN EFI_STATUS_CODE_TYPE Type,
347 IN EFI_STATUS_CODE_VALUE Value,
348 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
349 )
350 {
351 ASSERT (DevicePath != NULL);
352 return ReportStatusCodeWithExtendedData (
353 Type,
354 Value,
355 (VOID *)DevicePath,
356 InternalReportStatusCodeDevicePathSize (DevicePath)
357 );
358 }
359
360
361 /**
362 Reports a status code with an extended data buffer.
363
364 Allocates and fills in the extended data section of a status code with the
365 extended data specified by ExtendedData and ExtendedDataSize. ExtendedData
366 is assumed to be one of the data structures specified in Related Definitions.
367 These data structure do not have the standard header, so this function is
368 responsible for allocating a buffer large enough for the standard header and
369 the extended data passed into this function. The standard header is filled
370 in with a GUID of gEfiStatusCodeSpecificDataGuid. The status code is reported
371 with a zero instance and a caller ID of gEfiCallerIdGuid.
372
373 ReportStatusCodeWithExtendedData()must actively prevent recursion. If
374 ReportStatusCodeWithExtendedData() is called while processing another any other
375 Report Status Code Library function, then ReportStatusCodeWithExtendedData()
376 must return EFI_DEVICE_ERROR immediately.
377
378 If ExtendedData is NULL, then ASSERT().
379 If ExtendedDataSize is 0, then ASSERT().
380
381 @param Type Status code type.
382 @param Value Status code value.
383 @param ExtendedData Pointer to the extended data buffer to be reported.
384 @param ExtendedDataSize The size, in bytes, of the extended data buffer to
385 be reported.
386
387 @retval EFI_SUCCESS The status code was reported with the extended
388 data specified by ExtendedData and ExtendedDataSize.
389 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
390 extended data section.
391 @retval EFI_UNSUPPORTED Report status code is not supported
392
393 **/
394 EFI_STATUS
395 EFIAPI
396 ReportStatusCodeWithExtendedData (
397 IN EFI_STATUS_CODE_TYPE Type,
398 IN EFI_STATUS_CODE_VALUE Value,
399 IN CONST VOID *ExtendedData,
400 IN UINTN ExtendedDataSize
401 )
402 {
403 ASSERT (ExtendedData != NULL);
404 ASSERT (ExtendedDataSize != 0);
405 return ReportStatusCodeEx (
406 Type,
407 Value,
408 0,
409 NULL,
410 NULL,
411 ExtendedData,
412 ExtendedDataSize
413 );
414 }
415
416
417 /**
418 Reports a status code with full parameters.
419
420 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
421 is 0, then an extended data buffer is not reported. If ExtendedData is not
422 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
423 ExtendedData is assumed not have the standard status code header, so this function
424 is responsible for allocating a buffer large enough for the standard header and
425 the extended data passed into this function. The standard header is filled in
426 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
427 GUID of gEfiStatusCodeSpecificDatauid is used. The status code is reported with
428 an instance specified by Instance and a caller ID specified by CallerId. If
429 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
430
431 ReportStatusCodeEx()must actively prevent recursion. If
432 ReportStatusCodeEx() is called while processing another any
433 other Report Status Code Library function, then
434 ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
435
436 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
437 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
438
439 @param Type Status code type.
440 @param Value Status code value.
441 @param Instance Status code instance number.
442 @param CallerId Pointer to a GUID that identifies the caller of this
443 function. If this parameter is NULL, then a caller
444 ID of gEfiCallerIdGuid is used.
445 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
446 If this parameter is NULL, then a the status code
447 standard header is filled in with
448 gEfiStatusCodeSpecificDataGuid.
449 @param ExtendedData Pointer to the extended data buffer. This is an
450 optional parameter that may be NULL.
451 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
452
453 @retval EFI_SUCCESS The status code was reported.
454 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
455 the extended data section if it was specified.
456 @retval EFI_UNSUPPORTED Report status code is not supported
457
458 **/
459 EFI_STATUS
460 EFIAPI
461 ReportStatusCodeEx (
462 IN EFI_STATUS_CODE_TYPE Type,
463 IN EFI_STATUS_CODE_VALUE Value,
464 IN UINT32 Instance,
465 IN CONST EFI_GUID *CallerId OPTIONAL,
466 IN CONST EFI_GUID *ExtendedDataGuid OPTIONAL,
467 IN CONST VOID *ExtendedData OPTIONAL,
468 IN UINTN ExtendedDataSize
469 )
470 {
471 EFI_STATUS Status;
472
473 Status = InternalReportStatusCodeEx (
474 Type,
475 Value,
476 Instance,
477 CallerId,
478 ExtendedDataGuid,
479 ExtendedData,
480 ExtendedDataSize
481 );
482
483 return Status;
484 }
485
486
487 /**
488 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
489
490 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
491 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
492
493 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
494 PcdReportStatusCodeProperyMask is set.
495 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
496 PcdReportStatusCodeProperyMask is clear.
497
498 **/
499 BOOLEAN
500 EFIAPI
501 ReportProgressCodeEnabled (
502 VOID
503 )
504 {
505 return (BOOLEAN) ((PcdGet8(PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED) != 0);
506 }
507
508
509 /**
510 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
511
512 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
513 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
514
515 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
516 PcdReportStatusCodeProperyMask is set.
517 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
518 PcdReportStatusCodeProperyMask is clear.
519
520 **/
521 BOOLEAN
522 EFIAPI
523 ReportErrorCodeEnabled (
524 VOID
525 )
526 {
527 return (BOOLEAN) ((PcdGet8(PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED) != 0);
528 }
529
530
531 /**
532 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
533
534 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
535 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
536
537 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
538 PcdReportStatusCodeProperyMask is set.
539 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
540 PcdReportStatusCodeProperyMask is clear.
541
542 **/
543 BOOLEAN
544 EFIAPI
545 ReportDebugCodeEnabled (
546 VOID
547 )
548 {
549 return (BOOLEAN) ((PcdGet8(PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED) != 0);
550 }