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