]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/DxeReportStatusCodeLib/ReportStatusCodeLib.c
BaseMemoryLib (BaseMemoryLibRepStr):
[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 CONST 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, (EFI_GUID *)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 CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
84 )
85 {
86 CONST EFI_DEVICE_PATH_PROTOCOL *Start;
87
88 //
89 // Search for the end of the device path structure
90 //
91 Start = DevicePath;
92 while (!EfiIsDevicePathEnd (DevicePath)) {
93 DevicePath = EfiNextDevicePathNode (DevicePath);
94 }
95
96 //
97 // Subtract the start node from the end node and add in the size of the end node
98 //
99 return ((UINTN) DevicePath - (UINTN) Start) + DevicePathNodeLength (DevicePath);
100 }
101
102
103 /**
104 Converts a status code to an 8-bit POST code value.
105
106 Converts the status code specified by CodeType and Value to an 8-bit POST code
107 and returns the 8-bit POST code in PostCode. If CodeType is an
108 EFI_PROGRESS_CODE or CodeType is an EFI_ERROR_CODE, then bits 0..4 of PostCode
109 are set to bits 16..20 of Value, and bits 5..7 of PostCode are set to bits
110 24..26 of Value., and TRUE is returned. Otherwise, FALSE is returned.
111
112 If PostCode is NULL, then ASSERT().
113
114 @param CodeType The type of status code being converted.
115 @param Value The status code value being converted.
116 @param PostCode A pointer to the 8-bit POST code value to return.
117
118 @retval TRUE The status code specified by CodeType and Value was converted
119 to an 8-bit POST code and returned in PostCode.
120 @retval FALSE The status code specified by CodeType and Value could not be
121 converted to an 8-bit POST code value.
122
123 **/
124 BOOLEAN
125 EFIAPI
126 CodeTypeToPostCode (
127 IN EFI_STATUS_CODE_TYPE CodeType,
128 IN EFI_STATUS_CODE_VALUE Value,
129 OUT UINT8 *PostCode
130 )
131 {
132 //
133 // If PostCode is NULL, then ASSERT()
134 //
135 ASSERT (PostCode != NULL);
136
137 //
138 // Convert Value to an 8 bit post code
139 //
140 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) ||
141 ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) ) {
142 *PostCode = (UINT8) (((Value & EFI_STATUS_CODE_CLASS_MASK) >> 24) << 5);
143 *PostCode |= (UINT8) (((Value & EFI_STATUS_CODE_SUBCLASS_MASK) >> 16) & 0x1f);
144 return TRUE;
145 }
146 return FALSE;
147 }
148
149
150 /**
151 Extracts ASSERT() information from a status code structure.
152
153 Converts the status code specified by CodeType, Value, and Data to the ASSERT()
154 arguments specified by Filename, Description, and LineNumber. If CodeType is
155 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
156 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
157 Filename, Description, and LineNumber from the optional data area of the
158 status code buffer specified by Data. The optional data area of Data contains
159 a Null-terminated ASCII string for the FileName, followed by a Null-terminated
160 ASCII string for the Description, followed by a 32-bit LineNumber. If the
161 ASSERT() information could be extracted from Data, then return TRUE.
162 Otherwise, FALSE is returned.
163
164 If Data is NULL, then ASSERT().
165 If Filename is NULL, then ASSERT().
166 If Description is NULL, then ASSERT().
167 If LineNumber is NULL, then ASSERT().
168
169 @param CodeType The type of status code being converted.
170 @param Value The status code value being converted.
171 @param Data Pointer to status code data buffer.
172 @param Filename Pointer to the source file name that generated the ASSERT().
173 @param Description Pointer to the description of the ASSERT().
174 @param LineNumber Pointer to source line number that generated the ASSERT().
175
176 @retval TRUE The status code specified by CodeType, Value, and Data was
177 converted ASSERT() arguments specified by Filename, Description,
178 and LineNumber.
179 @retval FALSE The status code specified by CodeType, Value, and Data could
180 not be converted to ASSERT() arguments.
181
182 **/
183 BOOLEAN
184 EFIAPI
185 ReportStatusCodeExtractAssertInfo (
186 IN EFI_STATUS_CODE_TYPE CodeType,
187 IN EFI_STATUS_CODE_VALUE Value,
188 IN CONST EFI_STATUS_CODE_DATA *Data,
189 OUT CHAR8 **Filename,
190 OUT CHAR8 **Description,
191 OUT UINT32 *LineNumber
192 )
193 {
194 EFI_DEBUG_ASSERT_DATA *AssertData;
195
196 ASSERT (Data != NULL);
197 ASSERT (Filename != NULL);
198 ASSERT (Description != NULL);
199 ASSERT (LineNumber != NULL);
200
201 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) &&
202 ((CodeType & EFI_STATUS_CODE_SEVERITY_MASK) == EFI_ERROR_UNRECOVERED) &&
203 ((Value & EFI_STATUS_CODE_OPERATION_MASK) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)) {
204 AssertData = (EFI_DEBUG_ASSERT_DATA *)(Data + 1);
205 *Filename = (CHAR8 *)(AssertData + 1);
206 *Description = *Filename + AsciiStrLen (*Filename) + 1;
207 *LineNumber = AssertData->LineNumber;
208 return TRUE;
209 }
210 return FALSE;
211 }
212
213
214 /**
215 Extracts DEBUG() information from a status code structure.
216
217 Converts the status code specified by Data to the DEBUG() arguments specified
218 by ErrorLevel, Marker, and Format. If type GUID in Data is
219 EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
220 Format from the optional data area of the status code buffer specified by Data.
221 The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
222 which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
223 the Format. If the DEBUG() information could be extracted from Data, then
224 return TRUE. Otherwise, FALSE is returned.
225
226 If Data is NULL, then ASSERT().
227 If ErrorLevel is NULL, then ASSERT().
228 If Marker is NULL, then ASSERT().
229 If Format is NULL, then ASSERT().
230
231 @param Data Pointer to status code data buffer.
232 @param ErrorLevel Pointer to error level mask for a debug message.
233 @param Marker Pointer to the variable argument list associated with Format.
234 @param Format Pointer to a Null-terminated ASCII format string of a
235 debug message.
236
237 @retval TRUE The status code specified by Data was converted DEBUG() arguments
238 specified by ErrorLevel, Marker, and Format.
239 @retval FALSE The status code specified by Data could not be converted to
240 DEBUG() arguments.
241
242 **/
243 BOOLEAN
244 EFIAPI
245 ReportStatusCodeExtractDebugInfo (
246 IN CONST EFI_STATUS_CODE_DATA *Data,
247 OUT UINT32 *ErrorLevel,
248 OUT VA_LIST *Marker,
249 OUT CHAR8 **Format
250 )
251 {
252 EFI_DEBUG_INFO *DebugInfo;
253
254 ASSERT (Data != NULL);
255 ASSERT (ErrorLevel != NULL);
256 ASSERT (Marker != NULL);
257 ASSERT (Format != NULL);
258
259 //
260 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
261 //
262 if (!CompareGuid (&Data->Type, &gEfiStatusCodeDataTypeDebugGuid)) {
263 return FALSE;
264 }
265
266 //
267 // Retrieve the debug information from the status code record
268 //
269 DebugInfo = (EFI_DEBUG_INFO *)(Data + 1);
270
271 *ErrorLevel = DebugInfo->ErrorLevel;
272
273 //
274 // The first 12 * UINTN bytes of the string are really an
275 // argument stack to support varargs on the Format string.
276 //
277 *Marker = (VA_LIST) (DebugInfo + 1);
278 *Format = (CHAR8 *)(((UINT64 *)*Marker) + 12);
279
280 return TRUE;
281 }
282
283
284 /**
285 Reports a status code.
286
287 Reports the status code specified by the parameters Type and Value. Status
288 code also require an instance, caller ID, and extended data. This function
289 passed in a zero instance, NULL extended data, and a caller ID of
290 gEfiCallerIdGuid, which is the GUID for the module.
291
292 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
293 is called while processing another any other Report Status Code Library function,
294 then ReportStatusCode() must return immediately.
295
296 @param Type Status code type.
297 @param Value Status code value.
298
299 @retval EFI_SUCCESS The status code was reported.
300 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
301 device error.
302 @retval EFI_UNSUPPORTED Report status code is not supported
303
304 **/
305 EFI_STATUS
306 EFIAPI
307 ReportStatusCode (
308 IN EFI_STATUS_CODE_TYPE Type,
309 IN EFI_STATUS_CODE_VALUE Value
310 )
311 {
312 return InternalReportStatusCode (Type, Value, 0, &gEfiCallerIdGuid, NULL);
313 }
314
315
316 /**
317 Reports a status code with a Device Path Protocol as the extended data.
318
319 Allocates and fills in the extended data section of a status code with the
320 Device Path Protocol specified by DevicePath. This function is responsible
321 for allocating a buffer large enough for the standard header and the device
322 path. The standard header is filled in with a GUID of
323 gEfiStatusCodeSpecificDataGuid. The status code is reported with a zero
324 instance and a caller ID of gEfiCallerIdGuid.
325
326 ReportStatusCodeWithDevicePath()must actively prevent recursion. If
327 ReportStatusCodeWithDevicePath() is called while processing another any other
328 Report Status Code Library function, then ReportStatusCodeWithDevicePath()
329 must return EFI_DEVICE_ERROR immediately.
330
331 If DevicePath is NULL, then ASSERT().
332
333 @param Type Status code type.
334 @param Value Status code value.
335 @param DevicePath Pointer to the Device Path Protocol to be reported.
336
337 @retval EFI_SUCCESS The status code was reported with the extended
338 data specified by DevicePath.
339 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
340 extended data section.
341 @retval EFI_UNSUPPORTED Report status code is not supported
342
343 **/
344 EFI_STATUS
345 EFIAPI
346 ReportStatusCodeWithDevicePath (
347 IN EFI_STATUS_CODE_TYPE Type,
348 IN EFI_STATUS_CODE_VALUE Value,
349 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
350 )
351 {
352 ASSERT (DevicePath != NULL);
353 return ReportStatusCodeWithExtendedData (
354 Type,
355 Value,
356 (VOID *)DevicePath,
357 InternalReportStatusCodeDevicePathSize (DevicePath)
358 );
359 }
360
361
362 /**
363 Reports a status code with an extended data buffer.
364
365 Allocates and fills in the extended data section of a status code with the
366 extended data specified by ExtendedData and ExtendedDataSize. ExtendedData
367 is assumed to be one of the data structures specified in Related Definitions.
368 These data structure do not have the standard header, so this function is
369 responsible for allocating a buffer large enough for the standard header and
370 the extended data passed into this function. The standard header is filled
371 in with a GUID of gEfiStatusCodeSpecificDataGuid. The status code is reported
372 with a zero instance and a caller ID of gEfiCallerIdGuid.
373
374 ReportStatusCodeWithExtendedData()must actively prevent recursion. If
375 ReportStatusCodeWithExtendedData() is called while processing another any other
376 Report Status Code Library function, then ReportStatusCodeWithExtendedData()
377 must return EFI_DEVICE_ERROR immediately.
378
379 If ExtendedData is NULL, then ASSERT().
380 If ExtendedDataSize is 0, then ASSERT().
381
382 @param Type Status code type.
383 @param Value Status code value.
384 @param ExtendedData Pointer to the extended data buffer to be reported.
385 @param ExtendedDataSize The size, in bytes, of the extended data buffer to
386 be reported.
387
388 @retval EFI_SUCCESS The status code was reported with the extended
389 data specified by ExtendedData and ExtendedDataSize.
390 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
391 extended data section.
392 @retval EFI_UNSUPPORTED Report status code is not supported
393
394 **/
395 EFI_STATUS
396 EFIAPI
397 ReportStatusCodeWithExtendedData (
398 IN EFI_STATUS_CODE_TYPE Type,
399 IN EFI_STATUS_CODE_VALUE Value,
400 IN CONST VOID *ExtendedData,
401 IN UINTN ExtendedDataSize
402 )
403 {
404 ASSERT (ExtendedData != NULL);
405 ASSERT (ExtendedDataSize != 0);
406 return ReportStatusCodeEx (
407 Type,
408 Value,
409 0,
410 NULL,
411 NULL,
412 ExtendedData,
413 ExtendedDataSize
414 );
415 }
416
417
418 /**
419 Reports a status code with full parameters.
420
421 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
422 is 0, then an extended data buffer is not reported. If ExtendedData is not
423 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
424 ExtendedData is assumed not have the standard status code header, so this function
425 is responsible for allocating a buffer large enough for the standard header and
426 the extended data passed into this function. The standard header is filled in
427 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
428 GUID of gEfiStatusCodeSpecificDatauid is used. The status code is reported with
429 an instance specified by Instance and a caller ID specified by CallerId. If
430 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
431
432 ReportStatusCodeEx()must actively prevent recursion. If ReportStatusCodeEx()
433 is called while processing another any other Report Status Code Library function,
434 then 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 EFI_STATUS_CODE_DATA *StatusCodeData;
473
474 ASSERT (!((ExtendedData == NULL) && (ExtendedDataSize != 0)));
475 ASSERT (!((ExtendedData != NULL) && (ExtendedDataSize == 0)));
476
477 //
478 // Allocate space for the Status Code Header and its buffer
479 //
480 StatusCodeData = NULL;
481 gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize, (VOID **)&StatusCodeData);
482 if (StatusCodeData == NULL) {
483 return EFI_OUT_OF_RESOURCES;
484 }
485
486 //
487 // Fill in the extended data header
488 //
489 StatusCodeData->HeaderSize = sizeof (EFI_STATUS_CODE_DATA);
490 StatusCodeData->Size = (UINT16)ExtendedDataSize;
491 if (ExtendedDataGuid == NULL) {
492 ExtendedDataGuid = &gEfiStatusCodeSpecificDataGuid;
493 }
494 CopyGuid (&StatusCodeData->Type, ExtendedDataGuid);
495
496 //
497 // Fill in the extended data buffer
498 //
499 CopyMem (StatusCodeData + 1, ExtendedData, ExtendedDataSize);
500
501 //
502 // Report the status code
503 //
504 if (CallerId == NULL) {
505 CallerId = &gEfiCallerIdGuid;
506 }
507 Status = InternalReportStatusCode (Type, Value, Instance, CallerId, StatusCodeData);
508
509 //
510 // Free the allocated buffer
511 //
512 gBS->FreePool (StatusCodeData);
513
514 return Status;
515 }
516
517
518 /**
519 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
520
521 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
522 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
523
524 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
525 PcdReportStatusCodeProperyMask is set.
526 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
527 PcdReportStatusCodeProperyMask is clear.
528
529 **/
530 BOOLEAN
531 EFIAPI
532 ReportProgressCodeEnabled (
533 VOID
534 )
535 {
536 return ((PcdGet8(PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED) != 0);
537 }
538
539
540 /**
541 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
542
543 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
544 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
545
546 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
547 PcdReportStatusCodeProperyMask is set.
548 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
549 PcdReportStatusCodeProperyMask is clear.
550
551 **/
552 BOOLEAN
553 EFIAPI
554 ReportErrorCodeEnabled (
555 VOID
556 )
557 {
558 return ((PcdGet8(PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED) != 0);
559 }
560
561
562 /**
563 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
564
565 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
566 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
567
568 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
569 PcdReportStatusCodeProperyMask is set.
570 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
571 PcdReportStatusCodeProperyMask is clear.
572
573 **/
574 BOOLEAN
575 EFIAPI
576 ReportDebugCodeEnabled (
577 VOID
578 )
579 {
580 return ((PcdGet8(PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED) != 0);
581 }