]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Library/PeiDxeDebugLibReportStatusCode/DebugLib.c
IntelFrameworkModulePkg: DebugAssert enhancement
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / PeiDxeDebugLibReportStatusCode / DebugLib.c
index f6ab54ed6a87d86697d7867ff17a40f195cad36f..7b07916bbdc955dc230f725debee7cd17abcf197 100644 (file)
@@ -4,7 +4,7 @@
   Note that if the debug message length is larger than the maximum allowable\r
   record length, then the debug message will be ignored directly.\r
 \r
-  Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -15,7 +15,7 @@
 \r
 **/\r
 \r
-#include <FrameworkPei.h>\r
+#include <PiPei.h>\r
 \r
 #include <Guid/StatusCodeDataTypeId.h>\r
 #include <Guid/StatusCodeDataTypeDebug.h>\r
@@ -56,6 +56,7 @@ DebugPrint (
   UINT64          Buffer[(EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof (UINT64)) + 1];\r
   EFI_DEBUG_INFO  *DebugInfo;\r
   UINTN           TotalSize;\r
+  UINTN           DestBufferSize;\r
   VA_LIST         VaListMarker;\r
   BASE_LIST       BaseListMarker;\r
   CHAR8           *FormatString;\r
@@ -115,7 +116,13 @@ DebugPrint (
   //\r
   // Copy the Format string into the record\r
   //\r
-  AsciiStrCpy (FormatString, Format);\r
+  // According to the content structure of Buffer shown above, the size of\r
+  // the FormatString buffer is the size of Buffer minus the Padding\r
+  // (4 bytes), minus the size of EFI_DEBUG_INFO, minus the size of\r
+  // variable arguments (12 * sizeof (UINT64)).\r
+  //\r
+  DestBufferSize = sizeof (Buffer) - 4 - sizeof (EFI_DEBUG_INFO) - 12 * sizeof (UINT64);\r
+  AsciiStrCpyS (FormatString, DestBufferSize / sizeof (CHAR8), Format);\r
 \r
   //\r
   // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments\r
@@ -182,7 +189,7 @@ DebugPrint (
     if ((*Format == 'p') && (sizeof (VOID *) > 4)) {\r
       Long = TRUE;\r
     }\r
-    if (*Format == 'p' || *Format == 'X' || *Format == 'x' || *Format == 'd') {\r
+    if (*Format == 'p' || *Format == 'X' || *Format == 'x' || *Format == 'd' || *Format == 'u') {\r
       if (Long) {\r
         BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64);\r
       } else {\r
@@ -258,45 +265,93 @@ DebugAssert (
 {\r
   UINT64                 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];\r
   EFI_DEBUG_ASSERT_DATA  *AssertData;\r
+  UINTN                  HeaderSize;\r
   UINTN                  TotalSize;\r
   CHAR8                  *Temp;\r
+  UINTN                  ModuleNameSize;\r
   UINTN                  FileNameSize;\r
   UINTN                  DescriptionSize;\r
 \r
   //\r
-  // Make sure it will all fit in the passed in buffer\r
+  // Get string size\r
   //\r
-  FileNameSize    = AsciiStrSize (FileName);\r
-  DescriptionSize = AsciiStrSize (Description);\r
-  TotalSize = sizeof (EFI_DEBUG_ASSERT_DATA) + FileNameSize + DescriptionSize;\r
-  if (TotalSize <= sizeof (Buffer)) {\r
-    //\r
-    // Fill in EFI_DEBUG_ASSERT_DATA\r
-    //\r
-    AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;\r
-    AssertData->LineNumber = (UINT32)LineNumber;\r
+  HeaderSize       = sizeof (EFI_DEBUG_ASSERT_DATA);\r
+  //\r
+  // Compute string size of module name enclosed by []\r
+  //\r
+  ModuleNameSize   = 2 + AsciiStrSize (gEfiCallerBaseName);\r
+  FileNameSize     = AsciiStrSize (FileName);\r
+  DescriptionSize  = AsciiStrSize (Description);\r
 \r
+  //\r
+  // Make sure it will all fit in the passed in buffer.\r
+  //\r
+  if (HeaderSize + ModuleNameSize + FileNameSize + DescriptionSize > sizeof (Buffer)) {\r
     //\r
-    // Copy Ascii FileName including NULL.\r
+    // remove module name if it's too long to be filled into buffer\r
     //\r
-    Temp = AsciiStrCpy ((CHAR8 *)(AssertData + 1), FileName);\r
+    ModuleNameSize = 0;\r
+    if (HeaderSize + FileNameSize + DescriptionSize > sizeof (Buffer)) {\r
+      //\r
+      // FileName + Description is too long to be filled into buffer.\r
+      //\r
+      if (HeaderSize + FileNameSize < sizeof (Buffer)) {\r
+        //\r
+        // Description has enough buffer to be truncated.\r
+        //\r
+        DescriptionSize = sizeof (Buffer) - HeaderSize - FileNameSize;\r
+      } else {\r
+        //\r
+        // FileName is too long to be filled into buffer.\r
+        // FileName will be truncated. Reserved one byte for Description NULL terminator.\r
+        //\r
+        DescriptionSize = 1;\r
+        FileNameSize    = sizeof (Buffer) - HeaderSize - DescriptionSize;\r
+      }\r
+    }\r
+  }\r
+  //\r
+  // Fill in EFI_DEBUG_ASSERT_DATA\r
+  //\r
+  AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;\r
+  AssertData->LineNumber = (UINT32)LineNumber;\r
+  TotalSize  = sizeof (EFI_DEBUG_ASSERT_DATA);\r
 \r
-    //\r
-    // Copy Ascii Description\r
-    //\r
-    AsciiStrCpy (Temp + FileNameSize, Description);\r
-\r
-    REPORT_STATUS_CODE_EX (\r
-      (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),\r
-      (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),\r
-      0,\r
-      NULL,\r
-      NULL,\r
-      AssertData,\r
-      TotalSize\r
-      );\r
+  Temp = (CHAR8 *)(AssertData + 1);\r
+\r
+  //\r
+  // Copy Ascii [ModuleName].\r
+  //\r
+  if (ModuleNameSize != 0) {\r
+    CopyMem(Temp, "[", 1);\r
+    CopyMem(Temp + 1, gEfiCallerBaseName, ModuleNameSize - 3);\r
+    CopyMem(Temp + ModuleNameSize - 2, "] ", 2);\r
   }\r
 \r
+  //\r
+  // Copy Ascii FileName including NULL terminator.\r
+  //\r
+  Temp = CopyMem (Temp + ModuleNameSize, FileName, FileNameSize);\r
+  Temp[FileNameSize - 1] = 0;\r
+  TotalSize += (ModuleNameSize + FileNameSize);\r
+\r
+  //\r
+  // Copy Ascii Description include NULL terminator.\r
+  //\r
+  Temp = CopyMem (Temp + FileNameSize, Description, DescriptionSize);\r
+  Temp[DescriptionSize - 1] = 0;\r
+  TotalSize += DescriptionSize;\r
+\r
+  REPORT_STATUS_CODE_EX (\r
+    (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),\r
+    (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),\r
+    0,\r
+    NULL,\r
+    NULL,\r
+    AssertData,\r
+    TotalSize\r
+    );\r
+\r
   //\r
   // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
   //\r
@@ -414,3 +469,21 @@ DebugClearMemoryEnabled (
 {\r
   return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
 }\r
+\r
+/**\r
+  Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
+\r
+  This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
+\r
+  @retval  TRUE    Current ErrorLevel is supported.\r
+  @retval  FALSE   Current ErrorLevel is not supported.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+DebugPrintLevelEnabled (\r
+  IN  CONST UINTN        ErrorLevel\r
+  )\r
+{\r
+  return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
+}\r