]> git.proxmox.com Git - mirror_edk2.git/blobdiff - PerformancePkg/Dp_App/DpUtilities.c
Add Missing invocations to VA_END() for VA_START().
[mirror_edk2.git] / PerformancePkg / Dp_App / DpUtilities.c
index 1d48e1430a112e956a457e41bf1f8b0ca3a416c9..f60006125f3ce375a37b02fe7d791e678005c7da 100644 (file)
@@ -1,14 +1,14 @@
 /** @file\r
-  Utility functions used by the Dp application.\r
-  *\r
-  * Copyright (c) 2009 - 2010, 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
-  http://opensource.org/licenses/bsd-license.php\r
-  *\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  Utility functions used by the Dp application.\r
+\r
+  Copyright (c) 2009 - 2012, 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
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 **/\r
 \r
 #include <Library/BaseLib.h>\r
 #include <Library/PrintLib.h>\r
 #include <Library/HiiLib.h>\r
 #include <Library/PcdLib.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/DevicePathLib.h>\r
+\r
+#include <Pi/PiFirmwareFile.h>\r
+#include <Library/DxeServicesLib.h>\r
 \r
 #include <Protocol/LoadedImage.h>\r
 #include <Protocol/DriverBinding.h>\r
+#include <Protocol/ComponentName2.h>\r
+#include <Protocol/DevicePath.h>\r
+#include <Protocol/DevicePathToText.h>\r
 \r
 #include <Guid/Performance.h>\r
 \r
 #include "Literals.h"\r
 #include "DpInternal.h"\r
 \r
-/** Calculate an event's duration in timer ticks.\r
-  *\r
-  * Given the count direction and the event's start and end timer values,\r
-  * calculate the duration of the event in timer ticks.  Information for\r
-  * the current measurement is pointed to by the parameter.\r
-  *\r
-  * If the measurement's start time is 1, it indicates that the developer\r
-  * is indicating that the measurement began at the release of reset.\r
-  * The start time is adjusted to the timer's starting count before performing\r
-  * the elapsed time calculation.\r
-  *\r
-  * The calculated duration, in ticks, is the absolute difference between\r
-  * the measurement's ending and starting counts.\r
-  *\r
-  * @pre  The global TimerInfo structure must have already been initialized\r
-  *       before this function is called.\r
-  *\r
-  * @param[in,out]    Measurement   Pointer to a MEASUREMENT_RECORD structure containing\r
-  *                   data for the current measurement.\r
-  *\r
-  * @return       The 64-bit duration of the event.\r
+/**\r
+  Wrap original FreePool to check NULL pointer first.\r
+\r
+  @param[in]    Buffer      The pointer to the buffer to free.\r
+\r
+**/\r
+VOID\r
+SafeFreePool (\r
+  IN VOID   *Buffer\r
+  )\r
+{\r
+  if (Buffer != NULL) {\r
+    FreePool (Buffer);\r
+  }\r
+}\r
+\r
+/** \r
+  Calculate an event's duration in timer ticks.\r
+  \r
+  Given the count direction and the event's start and end timer values,\r
+  calculate the duration of the event in timer ticks.  Information for\r
+  the current measurement is pointed to by the parameter.\r
+  \r
+  If the measurement's start time is 1, it indicates that the developer\r
+  is indicating that the measurement began at the release of reset.\r
+  The start time is adjusted to the timer's starting count before performing\r
+  the elapsed time calculation.\r
+  \r
+  The calculated duration, in ticks, is the absolute difference between\r
+  the measurement's ending and starting counts.\r
+  \r
+  @param Measurement   Pointer to a MEASUREMENT_RECORD structure containing\r
+                       data for the current measurement.\r
+  \r
+  @return              The 64-bit duration of the event.\r
 **/\r
 UINT64\r
 GetDuration (\r
@@ -85,15 +107,16 @@ GetDuration (
   return Duration;\r
 }\r
 \r
-/** Determine whether the Measurement record is for an EFI Phase.\r
-  *\r
-  * The Token and Module members of the measurement record are checked.\r
-  * Module must be empty and Token must be one of SEC, PEI, DXE, BDS, or SHELL.\r
-  *\r
-  * @param[in]  Measurement A pointer to the Measurement record to test.\r
-  *\r
-  * @retval     TRUE        The measurement record is for an EFI Phase.\r
-  * @retval     FALSE       The measurement record is NOT for an EFI Phase.\r
+/** \r
+  Determine whether the Measurement record is for an EFI Phase.\r
+  \r
+  The Token and Module members of the measurement record are checked.\r
+  Module must be empty and Token must be one of SEC, PEI, DXE, BDS, or SHELL.\r
+  \r
+  @param[in]  Measurement A pointer to the Measurement record to test.\r
+  \r
+  @retval     TRUE        The measurement record is for an EFI Phase.\r
+  @retval     FALSE       The measurement record is NOT for an EFI Phase.\r
 **/\r
 BOOLEAN\r
 IsPhase(\r
@@ -111,16 +134,17 @@ IsPhase(
   return RetVal;\r
 }\r
 \r
-/** Get the file name portion of the Pdb File Name.\r
-  *\r
-  * The portion of the Pdb File Name between the last backslash and\r
-  * either a following period or the end of the string is converted\r
-  * to Unicode and copied into UnicodeBuffer.  The name is truncated,\r
-  * if necessary, to ensure that UnicodeBuffer is not overrun.\r
-  *\r
-  * @param[in]  PdbFileName     Pdb file name.\r
-  * @param[out] UnicodeBuffer   The resultant Unicode File Name.\r
-  *\r
+/** \r
+  Get the file name portion of the Pdb File Name.\r
+  \r
+  The portion of the Pdb File Name between the last backslash and\r
+  either a following period or the end of the string is converted\r
+  to Unicode and copied into UnicodeBuffer.  The name is truncated,\r
+  if necessary, to ensure that UnicodeBuffer is not overrun.\r
+  \r
+  @param[in]  PdbFileName     Pdb file name.\r
+  @param[out] UnicodeBuffer   The resultant Unicode File Name.\r
+  \r
 **/\r
 VOID\r
 GetShortPdbFileName (\r
@@ -163,13 +187,21 @@ GetShortPdbFileName (
   }\r
 }\r
 \r
-/** Get a human readable name for an image handle.\r
-  *\r
-  * @param[in]    Handle\r
-  *\r
-  * @post   The resulting Unicode name string is stored in the\r
-  *         mGaugeString global array.\r
-  *\r
+/** \r
+  Get a human readable name for an image handle.\r
+  The following methods will be tried orderly:\r
+    1. Image PDB\r
+    2. ComponentName2 protocol\r
+    3. FFS UI section\r
+    4. Image GUID\r
+    5. Image DevicePath\r
+    6. Unknown Driver Name\r
+\r
+  @param[in]    Handle\r
+\r
+  @post   The resulting Unicode name string is stored in the\r
+          mGaugeString global array.\r
+\r
 **/\r
 VOID\r
 GetNameFromHandle (\r
@@ -181,63 +213,168 @@ GetNameFromHandle (
   CHAR8                       *PdbFileName;\r
   EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;\r
   EFI_STRING                  StringPtr;\r
+  EFI_DEVICE_PATH_PROTOCOL    *LoadedImageDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL    *DevicePath;\r
+  EFI_GUID                    *NameGuid;\r
+  CHAR16                      *NameString;\r
+  UINTN                       StringSize;\r
+  CHAR8                       *PlatformLanguage;\r
+  EFI_COMPONENT_NAME2_PROTOCOL      *ComponentName2;\r
+  EFI_DEVICE_PATH_TO_TEXT_PROTOCOL  *DevicePathToText;\r
 \r
-  // Proactively get the error message so it will be ready if needed\r
-  StringPtr = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);\r
-  ASSERT (StringPtr != NULL);\r
-\r
-  // Get handle name from image protocol\r
+  //\r
+  // Method 1: Get the name string from image PDB\r
   //\r
   Status = gBS->HandleProtocol (\r
-                Handle,\r
-                &gEfiLoadedImageProtocolGuid,\r
-                (VOID**) &Image\r
-                );\r
+                  Handle,\r
+                  &gEfiLoadedImageProtocolGuid,\r
+                  (VOID **) &Image\r
+                  );\r
 \r
   if (EFI_ERROR (Status)) {\r
     Status = gBS->OpenProtocol (\r
+                    Handle,\r
+                    &gEfiDriverBindingProtocolGuid,\r
+                    (VOID **) &DriverBinding,\r
+                    NULL,\r
+                    NULL,\r
+                    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                    );\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = gBS->HandleProtocol (\r
+                      DriverBinding->ImageHandle,\r
+                      &gEfiLoadedImageProtocolGuid,\r
+                      (VOID **) &Image\r
+                      );\r
+    }\r
+  }\r
+\r
+  if (!EFI_ERROR (Status)) {\r
+    PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);\r
+\r
+    if (PdbFileName != NULL) {\r
+      GetShortPdbFileName (PdbFileName, mGaugeString);\r
+      return;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Method 2: Get the name string from ComponentName2 protocol\r
+  //\r
+  Status = gBS->HandleProtocol (\r
                   Handle,\r
-                  &gEfiDriverBindingProtocolGuid,\r
-                  (VOID **) &DriverBinding,\r
-                  NULL,\r
-                  NULL,\r
-                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  &gEfiComponentName2ProtocolGuid,\r
+                  (VOID **) &ComponentName2\r
                   );\r
-    if (EFI_ERROR (Status)) {\r
-      StrCpy (mGaugeString, StringPtr);\r
-      return ;\r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // Get the current platform language setting\r
+    //\r
+    PlatformLanguage = GetEfiGlobalVariable (L"PlatformLang");\r
+    Status = ComponentName2->GetDriverName (\r
+                               ComponentName2,\r
+                               PlatformLanguage != NULL ? PlatformLanguage : "en-US",\r
+                               &StringPtr\r
+                               );\r
+    if (!EFI_ERROR (Status)) {\r
+      SafeFreePool (PlatformLanguage);\r
+      StrnCpy (mGaugeString, StringPtr, DP_GAUGE_STRING_LENGTH);\r
+      mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
+      return;\r
     }\r
+  }\r
 \r
-    // Get handle name from image protocol\r
-    //\r
-    Status = gBS->HandleProtocol (\r
-                  DriverBinding->ImageHandle,\r
-                  &gEfiLoadedImageProtocolGuid,\r
-                  (VOID**) &Image\r
+  Status = gBS->HandleProtocol (\r
+                  Handle,\r
+                  &gEfiLoadedImageDevicePathProtocolGuid,\r
+                  (VOID **) &LoadedImageDevicePath\r
                   );\r
-  }\r
+  if (!EFI_ERROR (Status)) {\r
+    DevicePath = LoadedImageDevicePath;\r
+\r
+    //\r
+    // Try to get image GUID from LoadedImageDevicePath protocol\r
+    //\r
+    NameGuid = NULL;\r
+    while (!IsDevicePathEndType (DevicePath)) {\r
+      NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath);\r
+      if (NameGuid != NULL) {\r
+        break;\r
+      }\r
+      DevicePath = NextDevicePathNode (DevicePath);\r
+    }\r
 \r
-  PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);\r
+    if (NameGuid != NULL) {\r
+      //\r
+      // Try to get the image's FFS UI section by image GUID\r
+      //\r
+      NameString = NULL;\r
+      StringSize = 0;\r
+      Status = GetSectionFromAnyFv (\r
+                NameGuid,\r
+                EFI_SECTION_USER_INTERFACE,\r
+                0,\r
+                (VOID **) &NameString,\r
+                &StringSize\r
+                );\r
 \r
-  if (PdbFileName != NULL) {\r
-    GetShortPdbFileName (PdbFileName, mGaugeString);\r
-  } else {\r
-    StrCpy (mGaugeString, StringPtr);\r
+      if (!EFI_ERROR (Status)) {\r
+        //\r
+        // Method 3. Get the name string from FFS UI section\r
+        //\r
+        StrnCpy (mGaugeString, NameString, DP_GAUGE_STRING_LENGTH);\r
+        mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
+        FreePool (NameString);\r
+      } else {\r
+        //\r
+        // Method 4: Get the name string from image GUID\r
+        //\r
+        UnicodeSPrint (mGaugeString, sizeof (mGaugeString), L"%g", NameGuid);\r
+      }\r
+      return;\r
+    } else {\r
+      //\r
+      // Method 5: Get the name string from image DevicePath\r
+      //\r
+      Status = gBS->LocateProtocol (\r
+                      &gEfiDevicePathToTextProtocolGuid,\r
+                      NULL,\r
+                      (VOID **) &DevicePathToText\r
+                      );\r
+      if (!EFI_ERROR (Status)) {\r
+        NameString = DevicePathToText->ConvertDevicePathToText (LoadedImageDevicePath, TRUE, FALSE);\r
+        if (NameString != NULL) {\r
+          StrnCpy (mGaugeString, NameString, DP_GAUGE_STRING_LENGTH);\r
+          mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
+          FreePool (NameString);\r
+          return;\r
+        }\r
+      }\r
+    }\r
   }\r
-  return ;\r
+\r
+  //\r
+  // Method 6: Unknown Driver Name\r
+  //\r
+  StringPtr = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);\r
+  ASSERT (StringPtr != NULL);\r
+  StrCpy (mGaugeString, StringPtr);\r
+  FreePool (StringPtr);\r
+  return;\r
 }\r
 \r
-/** Calculate the Duration in microseconds.\r
-  *\r
-  * Duration is multiplied by 1000, instead of Frequency being divided by 1000 or\r
-  * multiplying the result by 1000, in order to maintain precision.  Since Duration is\r
-  * a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.\r
-  *\r
-  * The time is calculated as (Duration * 1000) / Timer_Frequency.\r
-  *\r
-  * @param[in]  Duration   The event duration in timer ticks.\r
-  *\r
-  * @return     A 64-bit value which is the Elapsed time in microseconds.\r
+/** \r
+  Calculate the Duration in microseconds.\r
+  \r
+  Duration is multiplied by 1000, instead of Frequency being divided by 1000 or\r
+  multiplying the result by 1000, in order to maintain precision.  Since Duration is\r
+  a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.\r
+  \r
+  The time is calculated as (Duration * 1000) / Timer_Frequency.\r
+  \r
+  @param[in]  Duration   The event duration in timer ticks.\r
+  \r
+  @return     A 64-bit value which is the Elapsed time in microseconds.\r
 **/\r
 UINT64\r
 DurationInMicroSeconds (\r
@@ -250,12 +387,14 @@ DurationInMicroSeconds (
   return DivU64x32 (Temp, TimerInfo.Frequency);\r
 }\r
 \r
-/** Formatted Print using a Hii Token to reference the localized format string.\r
-  *\r
-  * @param[in]  Token   A HII token associated with a localized Unicode string.\r
-  *\r
-  * @return             The number of characters converted by UnicodeVSPrint().\r
-  *\r
+/** \r
+  Formatted Print using a Hii Token to reference the localized format string.\r
+  \r
+  @param[in]  Token   A HII token associated with a localized Unicode string.\r
+  @param[in]  ...     The variable argument list.\r
+  \r
+  @return             The number of characters converted by UnicodeVSPrint().\r
+  \r
 **/\r
 UINTN\r
 PrintToken (\r
@@ -282,23 +421,27 @@ PrintToken (
   SetMem( mPrintTokenBuffer, BufferSize, 0);\r
 \r
   Return = UnicodeVSPrint (mPrintTokenBuffer, BufferSize, StringPtr, Marker);\r
+  VA_END (Marker);\r
+  \r
   if (Return > 0 && gST->ConOut != NULL) {\r
     gST->ConOut->OutputString (gST->ConOut, mPrintTokenBuffer);\r
   }\r
+  FreePool (StringPtr);\r
   return Return;\r
 }\r
 \r
-/** Get index of Measurement Record's match in the CumData array.\r
-  *\r
-  * If the Measurement's Token value matches a Token in one of the CumData\r
-  * records, the index of the matching record is returned.  The returned\r
-  * index is a signed value so that negative values can indicate that\r
-  * the Measurement didn't match any entry in the CumData array.\r
-  *\r
-  * @param[in]  Measurement A pointer to a Measurement Record to match against the CumData array.\r
-  *\r
-  * @retval     <0    Token is not in the CumData array.\r
-  * @retval     >=0   Return value is the index into CumData where Token is found.\r
+/** \r
+  Get index of Measurement Record's match in the CumData array.\r
+  \r
+  If the Measurement's Token value matches a Token in one of the CumData\r
+  records, the index of the matching record is returned.  The returned\r
+  index is a signed value so that negative values can indicate that\r
+  the Measurement didn't match any entry in the CumData array.\r
+  \r
+  @param[in]  Measurement A pointer to a Measurement Record to match against the CumData array.\r
+  \r
+  @retval     <0    Token is not in the CumData array.\r
+  @retval     >=0   Return value is the index into CumData where Token is found.\r
 **/\r
 INTN\r
 GetCumulativeItem(\r