--- /dev/null
+/** @file\r
+ Utility functions used by the Dp application.\r
+\r
+ Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.\r
+ (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<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/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/PeCoffGetEntryPointLib.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
+#include <Library/HandleParsingLib.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 "Dp.h"\r
+#include "Literals.h"\r
+#include "DpInternal.h"\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
+ IN OUT MEASUREMENT_RECORD *Measurement\r
+ )\r
+{\r
+ UINT64 Duration;\r
+ BOOLEAN Error;\r
+\r
+ if (Measurement->EndTimeStamp == 0) {\r
+ return 0;\r
+ }\r
+\r
+ // PERF_START macros are called with a value of 1 to indicate\r
+ // the beginning of time. So, adjust the start ticker value\r
+ // to the real beginning of time.\r
+ // Assumes no wraparound. Even then, there is a very low probability\r
+ // of having a valid StartTicker value of 1.\r
+ if (Measurement->StartTimeStamp == 1) {\r
+ Measurement->StartTimeStamp = TimerInfo.StartCount;\r
+ }\r
+ if (TimerInfo.CountUp) {\r
+ Duration = Measurement->EndTimeStamp - Measurement->StartTimeStamp;\r
+ Error = (BOOLEAN)(Duration > Measurement->EndTimeStamp);\r
+ }\r
+ else {\r
+ Duration = Measurement->StartTimeStamp - Measurement->EndTimeStamp;\r
+ Error = (BOOLEAN)(Duration > Measurement->StartTimeStamp);\r
+ }\r
+\r
+ if (Error) {\r
+ DEBUG ((EFI_D_ERROR, ALit_TimerLibError));\r
+ Duration = 0;\r
+ }\r
+ return Duration;\r
+}\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
+BOOLEAN\r
+IsPhase(\r
+ IN MEASUREMENT_RECORD *Measurement\r
+ )\r
+{\r
+ BOOLEAN RetVal;\r
+\r
+ RetVal = (BOOLEAN)( ( *Measurement->Module == '\0') &&\r
+ ((AsciiStrnCmp (Measurement->Token, ALit_SEC, PERF_TOKEN_LENGTH) == 0) ||\r
+ (AsciiStrnCmp (Measurement->Token, ALit_PEI, PERF_TOKEN_LENGTH) == 0) ||\r
+ (AsciiStrnCmp (Measurement->Token, ALit_DXE, PERF_TOKEN_LENGTH) == 0) ||\r
+ (AsciiStrnCmp (Measurement->Token, ALit_BDS, PERF_TOKEN_LENGTH) == 0))\r
+ );\r
+ return RetVal;\r
+}\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
+DpGetShortPdbFileName (\r
+ IN CHAR8 *PdbFileName,\r
+ OUT CHAR16 *UnicodeBuffer\r
+ )\r
+{\r
+ UINTN IndexA; // Current work location within an ASCII string.\r
+ UINTN IndexU; // Current work location within a Unicode string.\r
+ UINTN StartIndex;\r
+ UINTN EndIndex;\r
+\r
+ ZeroMem (UnicodeBuffer, (DP_GAUGE_STRING_LENGTH + 1) * sizeof (CHAR16));\r
+\r
+ if (PdbFileName == NULL) {\r
+ StrnCpyS (UnicodeBuffer, DP_GAUGE_STRING_LENGTH + 1, L" ", 1);\r
+ } else {\r
+ StartIndex = 0;\r
+ for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++)\r
+ ;\r
+ for (IndexA = 0; PdbFileName[IndexA] != 0; IndexA++) {\r
+ if ((PdbFileName[IndexA] == '\\') || (PdbFileName[IndexA] == '/')) {\r
+ StartIndex = IndexA + 1;\r
+ }\r
+\r
+ if (PdbFileName[IndexA] == '.') {\r
+ EndIndex = IndexA;\r
+ }\r
+ }\r
+\r
+ IndexU = 0;\r
+ for (IndexA = StartIndex; IndexA < EndIndex; IndexA++) {\r
+ UnicodeBuffer[IndexU] = (CHAR16) PdbFileName[IndexA];\r
+ IndexU++;\r
+ if (IndexU >= DP_GAUGE_STRING_LENGTH) {\r
+ UnicodeBuffer[DP_GAUGE_STRING_LENGTH] = 0;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+}\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
+DpGetNameFromHandle (\r
+ IN EFI_HANDLE Handle\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_LOADED_IMAGE_PROTOCOL *Image;\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
+ Image = NULL;\r
+ LoadedImageDevicePath = NULL;\r
+ DevicePath = NULL;\r
+\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
+\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
+ DpGetShortPdbFileName (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
+ &gEfiComponentName2ProtocolGuid,\r
+ (VOID **) &ComponentName2\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ //\r
+ // Firstly use platform language setting, secondly use driver's first supported language.\r
+ //\r
+ GetVariable2 (L"PlatformLang", &gEfiGlobalVariableGuid, (VOID**)&PlatformLanguage, NULL);\r
+ BestLanguage = GetBestLanguage(\r
+ ComponentName2->SupportedLanguages,\r
+ FALSE,\r
+ (PlatformLanguage != NULL) ? PlatformLanguage : "",\r
+ ComponentName2->SupportedLanguages,\r
+ NULL\r
+ );\r
+ SHELL_FREE_NON_NULL (PlatformLanguage);\r
+\r
+ Status = ComponentName2->GetDriverName (\r
+ ComponentName2,\r
+ BestLanguage != NULL ? BestLanguage : "en-US",\r
+ &StringPtr\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ SHELL_FREE_NON_NULL (BestLanguage);\r
+ StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);\r
+ mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
+ return;\r
+ }\r
+ }\r
+\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
+ 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 (!EFI_ERROR (Status)) {\r
+ //\r
+ // Method 3. Get the name string from FFS UI section\r
+ //\r
+ StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, 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
+ NameString = ConvertDevicePathToText (DevicePath, TRUE, FALSE);\r
+ if (NameString != NULL) {\r
+ StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);\r
+ mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
+ FreePool (NameString);\r
+ return;\r
+ }\r
+ }\r
+ }\r
+\r
+ //\r
+ // Method 6: Unknown Driver Name\r
+ //\r
+ StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);\r
+ ASSERT (StringPtr != NULL);\r
+ StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);\r
+ FreePool (StringPtr);\r
+}\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
+UINT64\r
+DurationInMicroSeconds (\r
+ IN UINT64 Duration\r
+ )\r
+{\r
+ UINT64 Temp;\r
+\r
+ Temp = MultU64x32 (Duration, 1000);\r
+ return DivU64x32 (Temp, TimerInfo.Frequency);\r
+}\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
+INTN\r
+GetCumulativeItem(\r
+ IN MEASUREMENT_RECORD *Measurement\r
+ )\r
+{\r
+ INTN Index;\r
+\r
+ for( Index = 0; Index < (INTN)NumCum; ++Index) {\r
+ if (AsciiStrnCmp (Measurement->Token, CumData[Index].Name, PERF_TOKEN_LENGTH) == 0) {\r
+ return Index; // Exit, we found a match\r
+ }\r
+ }\r
+ // If the for loop exits, Token was not found.\r
+ return -1; // Indicate failure\r
+}\r