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