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