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