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