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