]> 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 1c40e8395040e92ff4ceb5c3c6cd8957f3f0ff4e..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
+/**\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
@@ -165,12 +189,19 @@ GetShortPdbFileName (
 \r
 /** \r
   Get a human readable name for an image handle.\r
-  \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
+\r
   @post   The resulting Unicode name string is stored in the\r
           mGaugeString global array.\r
-  \r
+\r
 **/\r
 VOID\r
 GetNameFromHandle (\r
@@ -182,50 +213,154 @@ 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
 /** \r
@@ -286,9 +421,12 @@ 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