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