Home | History | Annotate | Download | only in PeiDxeDebugLibReportStatusCode
      1 /** @file
      2   Debug Library based on report status code library.
      3 
      4   Note that if the debug message length is larger than the maximum allowable
      5   record length, then the debug message will be ignored directly.
      6 
      7   Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
      8   This program and the accompanying materials
      9   are licensed and made available under the terms and conditions of the BSD License
     10   which accompanies this distribution.  The full text of the license may be found at
     11   http://opensource.org/licenses/bsd-license.php
     12 
     13   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     14   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     15 
     16 **/
     17 
     18 #include <PiPei.h>
     19 
     20 #include <Guid/StatusCodeDataTypeId.h>
     21 #include <Guid/StatusCodeDataTypeDebug.h>
     22 
     23 #include <Library/DebugLib.h>
     24 #include <Library/BaseLib.h>
     25 #include <Library/BaseMemoryLib.h>
     26 #include <Library/ReportStatusCodeLib.h>
     27 #include <Library/PcdLib.h>
     28 #include <Library/DebugPrintErrorLevelLib.h>
     29 
     30 /**
     31   Prints a debug message to the debug output device if the specified error level is enabled.
     32 
     33   If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
     34   GetDebugPrintErrorLevel (), then print the message specified by Format and the
     35   associated variable argument list to the debug output device.
     36 
     37   If Format is NULL, then ASSERT().
     38 
     39   If the length of the message string specificed by Format is larger than the maximum allowable
     40   record length, then directly return and not print it.
     41 
     42   @param  ErrorLevel  The error level of the debug message.
     43   @param  Format      Format string for the debug message to print.
     44   @param  ...         Variable argument list whose contents are accessed
     45                       based on the format string specified by Format.
     46 
     47 **/
     48 VOID
     49 EFIAPI
     50 DebugPrint (
     51   IN  UINTN        ErrorLevel,
     52   IN  CONST CHAR8  *Format,
     53   ...
     54   )
     55 {
     56   UINT64          Buffer[(EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof (UINT64)) + 1];
     57   EFI_DEBUG_INFO  *DebugInfo;
     58   UINTN           TotalSize;
     59   VA_LIST         VaListMarker;
     60   BASE_LIST       BaseListMarker;
     61   CHAR8           *FormatString;
     62   BOOLEAN         Long;
     63 
     64   //
     65   // If Format is NULL, then ASSERT().
     66   //
     67   ASSERT (Format != NULL);
     68 
     69   //
     70   // Check driver Debug Level value and global debug level
     71   //
     72   if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {
     73     return;
     74   }
     75 
     76   //
     77   // Compute the total size of the record.
     78   // Note that the passing-in format string and variable parameters will be constructed to
     79   // the following layout:
     80   //
     81   //         Buffer->|------------------------|
     82   //                 |         Padding        | 4 bytes
     83   //      DebugInfo->|------------------------|
     84   //                 |      EFI_DEBUG_INFO    | sizeof(EFI_DEBUG_INFO)
     85   // BaseListMarker->|------------------------|
     86   //                 |           ...          |
     87   //                 |   variable arguments   | 12 * sizeof (UINT64)
     88   //                 |           ...          |
     89   //                 |------------------------|
     90   //                 |       Format String    |
     91   //                 |------------------------|<- (UINT8 *)Buffer + sizeof(Buffer)
     92   //
     93   TotalSize = 4 + sizeof (EFI_DEBUG_INFO) + 12 * sizeof (UINT64) + AsciiStrSize (Format);
     94 
     95   //
     96   // If the TotalSize is larger than the maximum record size, then return
     97   //
     98   if (TotalSize > sizeof (Buffer)) {
     99     return;
    100   }
    101 
    102   //
    103   // Fill in EFI_DEBUG_INFO
    104   //
    105   // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarker is
    106   // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarker will cause
    107   // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))
    108   // just makes address of BaseListMarker, which follows DebugInfo, 64-bit aligned.
    109   //
    110   DebugInfo             = (EFI_DEBUG_INFO *)(Buffer) + 1;
    111   DebugInfo->ErrorLevel = (UINT32)ErrorLevel;
    112   BaseListMarker        = (BASE_LIST)(DebugInfo + 1);
    113   FormatString          = (CHAR8 *)((UINT64 *)(DebugInfo + 1) + 12);
    114 
    115   //
    116   // Copy the Format string into the record
    117   //
    118   AsciiStrCpyS (FormatString, sizeof(Buffer) - (4 + sizeof(EFI_DEBUG_INFO) + 12 * sizeof(UINT64)), Format);
    119 
    120   //
    121   // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
    122   // of format in DEBUG string, which is followed by the DEBUG format string.
    123   // Here we will process the variable arguments and pack them in this area.
    124   //
    125   VA_START (VaListMarker, Format);
    126   for (; *Format != '\0'; Format++) {
    127     //
    128     // Only format with prefix % is processed.
    129     //
    130     if (*Format != '%') {
    131       continue;
    132     }
    133     Long = FALSE;
    134     //
    135     // Parse Flags and Width
    136     //
    137     for (Format++; TRUE; Format++) {
    138       if (*Format == '.' || *Format == '-' || *Format == '+' || *Format == ' ') {
    139         //
    140         // These characters in format field are omitted.
    141         //
    142         continue;
    143       }
    144       if (*Format >= '0' && *Format <= '9') {
    145         //
    146         // These characters in format field are omitted.
    147         //
    148         continue;
    149       }
    150       if (*Format == 'L' || *Format == 'l') {
    151         //
    152         // 'L" or "l" in format field means the number being printed is a UINT64
    153         //
    154         Long = TRUE;
    155         continue;
    156       }
    157       if (*Format == '*') {
    158         //
    159         // '*' in format field means the precision of the field is specified by
    160         // a UINTN argument in the argument list.
    161         //
    162         BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);
    163         continue;
    164       }
    165       if (*Format == '\0') {
    166         //
    167         // Make no output if Format string terminates unexpectedly when
    168         // looking up for flag, width, precision and type.
    169         //
    170         Format--;
    171       }
    172       //
    173       // When valid argument type detected or format string terminates unexpectedly,
    174       // the inner loop is done.
    175       //
    176       break;
    177     }
    178 
    179     //
    180     // Pack variable arguments into the storage area following EFI_DEBUG_INFO.
    181     //
    182     if ((*Format == 'p') && (sizeof (VOID *) > 4)) {
    183       Long = TRUE;
    184     }
    185     if (*Format == 'p' || *Format == 'X' || *Format == 'x' || *Format == 'd' || *Format == 'u') {
    186       if (Long) {
    187         BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64);
    188       } else {
    189         BASE_ARG (BaseListMarker, int) = VA_ARG (VaListMarker, int);
    190       }
    191     } else if (*Format == 's' || *Format == 'S' || *Format == 'a' || *Format == 'g' || *Format == 't') {
    192       BASE_ARG (BaseListMarker, VOID *) = VA_ARG (VaListMarker, VOID *);
    193     } else if (*Format == 'c') {
    194       BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);
    195     } else if (*Format == 'r') {
    196       BASE_ARG (BaseListMarker, RETURN_STATUS) = VA_ARG (VaListMarker, RETURN_STATUS);
    197     }
    198 
    199     //
    200     // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()
    201     // This indicates that the DEBUG() macro is passing in more argument than can be handled by
    202     // the EFI_DEBUG_INFO record
    203     //
    204     ASSERT ((CHAR8 *)BaseListMarker <= FormatString);
    205 
    206     //
    207     // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return
    208     //
    209     if ((CHAR8 *)BaseListMarker > FormatString) {
    210       VA_END (VaListMarker);
    211       return;
    212     }
    213   }
    214   VA_END (VaListMarker);
    215 
    216   //
    217   // Send the DebugInfo record
    218   //
    219   REPORT_STATUS_CODE_EX (
    220     EFI_DEBUG_CODE,
    221     (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_DC_UNSPECIFIED),
    222     0,
    223     NULL,
    224     &gEfiStatusCodeDataTypeDebugGuid,
    225     DebugInfo,
    226     TotalSize
    227     );
    228 }
    229 
    230 /**
    231   Prints an assert message containing a filename, line number, and description.
    232   This may be followed by a breakpoint or a dead loop.
    233 
    234   Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
    235   to the debug output device.  If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
    236   PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
    237   DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
    238   CpuDeadLoop() is called.  If neither of these bits are set, then this function
    239   returns immediately after the message is printed to the debug output device.
    240   DebugAssert() must actively prevent recursion.  If DebugAssert() is called while
    241   processing another DebugAssert(), then DebugAssert() must return immediately.
    242 
    243   If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
    244   If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
    245 
    246   @param  FileName     Pointer to the name of the source file that generated the assert condition.
    247   @param  LineNumber   The line number in the source file that generated the assert condition
    248   @param  Description  Pointer to the description of the assert condition.
    249 
    250 **/
    251 VOID
    252 EFIAPI
    253 DebugAssert (
    254   IN CONST CHAR8  *FileName,
    255   IN UINTN        LineNumber,
    256   IN CONST CHAR8  *Description
    257   )
    258 {
    259   UINT64                 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];
    260   EFI_DEBUG_ASSERT_DATA  *AssertData;
    261   UINTN                  HeaderSize;
    262   UINTN                  TotalSize;
    263   CHAR8                  *Temp;
    264   UINTN                  ModuleNameSize;
    265   UINTN                  FileNameSize;
    266   UINTN                  DescriptionSize;
    267 
    268   //
    269   // Get string size
    270   //
    271   HeaderSize       = sizeof (EFI_DEBUG_ASSERT_DATA);
    272   //
    273   // Compute string size of module name enclosed by []
    274   //
    275   ModuleNameSize   = 2 + AsciiStrSize (gEfiCallerBaseName);
    276   FileNameSize     = AsciiStrSize (FileName);
    277   DescriptionSize  = AsciiStrSize (Description);
    278 
    279   //
    280   // Make sure it will all fit in the passed in buffer.
    281   //
    282   if (HeaderSize + ModuleNameSize + FileNameSize + DescriptionSize > sizeof (Buffer)) {
    283     //
    284     // remove module name if it's too long to be filled into buffer
    285     //
    286     ModuleNameSize = 0;
    287     if (HeaderSize + FileNameSize + DescriptionSize > sizeof (Buffer)) {
    288       //
    289       // FileName + Description is too long to be filled into buffer.
    290       //
    291       if (HeaderSize + FileNameSize < sizeof (Buffer)) {
    292         //
    293         // Description has enough buffer to be truncated.
    294         //
    295         DescriptionSize = sizeof (Buffer) - HeaderSize - FileNameSize;
    296       } else {
    297         //
    298         // FileName is too long to be filled into buffer.
    299         // FileName will be truncated. Reserved one byte for Description NULL terminator.
    300         //
    301         DescriptionSize = 1;
    302         FileNameSize    = sizeof (Buffer) - HeaderSize - DescriptionSize;
    303       }
    304     }
    305   }
    306   //
    307   // Fill in EFI_DEBUG_ASSERT_DATA
    308   //
    309   AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;
    310   AssertData->LineNumber = (UINT32)LineNumber;
    311   TotalSize  = sizeof (EFI_DEBUG_ASSERT_DATA);
    312 
    313   Temp = (CHAR8 *)(AssertData + 1);
    314 
    315   //
    316   // Copy Ascii [ModuleName].
    317   //
    318   if (ModuleNameSize != 0) {
    319     CopyMem(Temp, "[", 1);
    320     CopyMem(Temp + 1, gEfiCallerBaseName, ModuleNameSize - 3);
    321     CopyMem(Temp + ModuleNameSize - 2, "] ", 2);
    322   }
    323 
    324   //
    325   // Copy Ascii FileName including NULL terminator.
    326   //
    327   Temp = CopyMem (Temp + ModuleNameSize, FileName, FileNameSize);
    328   Temp[FileNameSize - 1] = 0;
    329   TotalSize += (ModuleNameSize + FileNameSize);
    330 
    331   //
    332   // Copy Ascii Description include NULL terminator.
    333   //
    334   Temp = CopyMem (Temp + FileNameSize, Description, DescriptionSize);
    335   Temp[DescriptionSize - 1] = 0;
    336   TotalSize += DescriptionSize;
    337 
    338   REPORT_STATUS_CODE_EX (
    339     (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
    340     (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),
    341     0,
    342     NULL,
    343     NULL,
    344     AssertData,
    345     TotalSize
    346     );
    347 
    348   //
    349   // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
    350   //
    351   if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
    352     CpuBreakpoint ();
    353   } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
    354     CpuDeadLoop ();
    355   }
    356 }
    357 
    358 
    359 /**
    360   Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
    361 
    362   This function fills Length bytes of Buffer with the value specified by
    363   PcdDebugClearMemoryValue, and returns Buffer.
    364 
    365   If Buffer is NULL, then ASSERT().
    366   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
    367 
    368   @param   Buffer  Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
    369   @param   Length  Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
    370 
    371   @return  Buffer  Pointer to the target buffer filled with PcdDebugClearMemoryValue.
    372 
    373 **/
    374 VOID *
    375 EFIAPI
    376 DebugClearMemory (
    377   OUT VOID  *Buffer,
    378   IN UINTN  Length
    379   )
    380 {
    381   ASSERT (Buffer != NULL);
    382 
    383   return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));
    384 }
    385 
    386 
    387 /**
    388   Returns TRUE if ASSERT() macros are enabled.
    389 
    390   This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
    391   PcdDebugProperyMask is set.  Otherwise FALSE is returned.
    392 
    393   @retval  TRUE    The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
    394   @retval  FALSE   The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
    395 
    396 **/
    397 BOOLEAN
    398 EFIAPI
    399 DebugAssertEnabled (
    400   VOID
    401   )
    402 {
    403   return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
    404 }
    405 
    406 
    407 /**
    408   Returns TRUE if DEBUG() macros are enabled.
    409 
    410   This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
    411   PcdDebugProperyMask is set.  Otherwise FALSE is returned.
    412 
    413   @retval  TRUE    The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
    414   @retval  FALSE   The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
    415 
    416 **/
    417 BOOLEAN
    418 EFIAPI
    419 DebugPrintEnabled (
    420   VOID
    421   )
    422 {
    423   return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
    424 }
    425 
    426 
    427 /**
    428   Returns TRUE if DEBUG_CODE() macros are enabled.
    429 
    430   This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
    431   PcdDebugProperyMask is set.  Otherwise FALSE is returned.
    432 
    433   @retval  TRUE    The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
    434   @retval  FALSE   The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
    435 
    436 **/
    437 BOOLEAN
    438 EFIAPI
    439 DebugCodeEnabled (
    440   VOID
    441   )
    442 {
    443   return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
    444 }
    445 
    446 
    447 /**
    448   Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
    449 
    450   This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
    451   PcdDebugProperyMask is set.  Otherwise FALSE is returned.
    452 
    453   @retval  TRUE    The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
    454   @retval  FALSE   The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
    455 
    456 **/
    457 BOOLEAN
    458 EFIAPI
    459 DebugClearMemoryEnabled (
    460   VOID
    461   )
    462 {
    463   return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
    464 }
    465 
    466 /**
    467   Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
    468 
    469   This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
    470 
    471   @retval  TRUE    Current ErrorLevel is supported.
    472   @retval  FALSE   Current ErrorLevel is not supported.
    473 
    474 **/
    475 BOOLEAN
    476 EFIAPI
    477 DebugPrintLevelEnabled (
    478   IN  CONST UINTN        ErrorLevel
    479   )
    480 {
    481   return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);
    482 }
    483