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