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