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