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