/** @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 - 2016, 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
\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
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
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
UINTN StartIndex;\r
UINTN EndIndex;\r
\r
- ZeroMem (UnicodeBuffer, DXE_PERFORMANCE_STRING_LENGTH * sizeof (CHAR16));\r
+ ZeroMem (UnicodeBuffer, (DP_GAUGE_STRING_LENGTH + 1) * sizeof (CHAR16));\r
\r
if (PdbFileName == NULL) {\r
- StrCpy (UnicodeBuffer, L" ");\r
+ StrCpyS (UnicodeBuffer, DP_GAUGE_STRING_LENGTH + 1, L" ");\r
} else {\r
StartIndex = 0;\r
for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++)\r
for (IndexA = StartIndex; IndexA < EndIndex; IndexA++) {\r
UnicodeBuffer[IndexU] = (CHAR16) PdbFileName[IndexA];\r
IndexU++;\r
- if (IndexU >= DXE_PERFORMANCE_STRING_LENGTH) {\r
- UnicodeBuffer[DXE_PERFORMANCE_STRING_LENGTH] = 0;\r
+ if (IndexU >= DP_GAUGE_STRING_LENGTH) {\r
+ UnicodeBuffer[DP_GAUGE_STRING_LENGTH] = 0;\r
break;\r
}\r
}\r
}\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
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
+ CHAR8 *BestLanguage;\r
+ EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\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
+ Image = NULL;\r
+ LoadedImageDevicePath = NULL;\r
+ DevicePath = NULL;\r
+ BestLanguage = NULL;\r
+ PlatformLanguage = 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
+ GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);\r
+\r
+ BestLanguage = GetBestLanguage(\r
+ ComponentName2->SupportedLanguages,\r
+ FALSE,\r
+ PlatformLanguage,\r
+ ComponentName2->SupportedLanguages,\r
+ NULL\r
+ );\r
+\r
+ SafeFreePool (PlatformLanguage);\r
+ Status = ComponentName2->GetDriverName (\r
+ ComponentName2,\r
+ BestLanguage,\r
+ &StringPtr\r
+ );\r
+ SafeFreePool (BestLanguage);\r
+ if (!EFI_ERROR (Status)) {\r
+ StrnCpyS (\r
+ mGaugeString,\r
+ DP_GAUGE_STRING_LENGTH + 1,\r
+ StringPtr,\r
+ DP_GAUGE_STRING_LENGTH\r
+ );\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
+ if (!EFI_ERROR (Status) && (LoadedImageDevicePath != NULL)) {\r
+ DevicePath = LoadedImageDevicePath;\r
+ } else if (Image != NULL) {\r
+ DevicePath = Image->FilePath;\r
}\r
\r
- PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);\r
+ if (DevicePath != NULL) {\r
+ //\r
+ // Try to get image GUID from image DevicePath\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
+ 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
+ StrnCpyS (\r
+ mGaugeString,\r
+ DP_GAUGE_STRING_LENGTH + 1,\r
+ NameString,\r
+ DP_GAUGE_STRING_LENGTH\r
+ );\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
+ NameString = ConvertDevicePathToText (DevicePath, TRUE, FALSE);\r
+ if (NameString != NULL) {\r
+ StrnCpyS (\r
+ mGaugeString,\r
+ DP_GAUGE_STRING_LENGTH + 1,\r
+ NameString,\r
+ DP_GAUGE_STRING_LENGTH\r
+ );\r
+ FreePool (NameString);\r
+ return;\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
+ StrCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, 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
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
+EFIAPI\r
PrintToken (\r
IN UINT16 Token,\r
...\r
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