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