DpUtilities.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /** @file
  2. Utility functions used by the Dp application.
  3. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
  4. (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Library/BaseLib.h>
  8. #include <Library/BaseMemoryLib.h>
  9. #include <Library/MemoryAllocationLib.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/UefiBootServicesTableLib.h>
  12. #include <Library/PeCoffGetEntryPointLib.h>
  13. #include <Library/PrintLib.h>
  14. #include <Library/HiiLib.h>
  15. #include <Library/PcdLib.h>
  16. #include <Library/UefiLib.h>
  17. #include <Library/DevicePathLib.h>
  18. #include <Library/HandleParsingLib.h>
  19. #include <Pi/PiFirmwareFile.h>
  20. #include <Library/DxeServicesLib.h>
  21. #include <Protocol/LoadedImage.h>
  22. #include <Protocol/DriverBinding.h>
  23. #include <Protocol/ComponentName2.h>
  24. #include <Protocol/DevicePath.h>
  25. #include <Guid/Performance.h>
  26. #include "Dp.h"
  27. #include "Literals.h"
  28. #include "DpInternal.h"
  29. /**
  30. Calculate an event's duration in timer ticks.
  31. Given the count direction and the event's start and end timer values,
  32. calculate the duration of the event in timer ticks. Information for
  33. the current measurement is pointed to by the parameter.
  34. If the measurement's start time is 1, it indicates that the developer
  35. is indicating that the measurement began at the release of reset.
  36. The start time is adjusted to the timer's starting count before performing
  37. the elapsed time calculation.
  38. The calculated duration, in ticks, is the absolute difference between
  39. the measurement's ending and starting counts.
  40. @param Measurement Pointer to a MEASUREMENT_RECORD structure containing
  41. data for the current measurement.
  42. @return The 64-bit duration of the event.
  43. **/
  44. UINT64
  45. GetDuration (
  46. IN OUT MEASUREMENT_RECORD *Measurement
  47. )
  48. {
  49. UINT64 Duration;
  50. BOOLEAN Error;
  51. if (Measurement->EndTimeStamp == 0) {
  52. return 0;
  53. }
  54. Duration = Measurement->EndTimeStamp - Measurement->StartTimeStamp;
  55. Error = (BOOLEAN)(Duration > Measurement->EndTimeStamp);
  56. if (Error) {
  57. DEBUG ((DEBUG_ERROR, ALit_TimerLibError));
  58. Duration = 0;
  59. }
  60. return Duration;
  61. }
  62. /**
  63. Determine whether the Measurement record is for an EFI Phase.
  64. The Token and Module members of the measurement record are checked.
  65. Module must be empty and Token must be one of SEC, PEI, DXE, BDS, or SHELL.
  66. @param[in] Measurement A pointer to the Measurement record to test.
  67. @retval TRUE The measurement record is for an EFI Phase.
  68. @retval FALSE The measurement record is NOT for an EFI Phase.
  69. **/
  70. BOOLEAN
  71. IsPhase (
  72. IN MEASUREMENT_RECORD *Measurement
  73. )
  74. {
  75. BOOLEAN RetVal;
  76. RetVal = (BOOLEAN)(
  77. ((AsciiStrCmp (Measurement->Token, ALit_SEC) == 0) ||
  78. (AsciiStrCmp (Measurement->Token, ALit_PEI) == 0) ||
  79. (AsciiStrCmp (Measurement->Token, ALit_DXE) == 0) ||
  80. (AsciiStrCmp (Measurement->Token, ALit_BDS) == 0))
  81. );
  82. return RetVal;
  83. }
  84. /**
  85. Determine whether the Measurement record is for core code.
  86. @param[in] Measurement A pointer to the Measurement record to test.
  87. @retval TRUE The measurement record is used for core.
  88. @retval FALSE The measurement record is NOT used for core.
  89. **/
  90. BOOLEAN
  91. IsCorePerf (
  92. IN MEASUREMENT_RECORD *Measurement
  93. )
  94. {
  95. BOOLEAN RetVal;
  96. RetVal = (BOOLEAN)(
  97. ((Measurement->Identifier == MODULE_START_ID) ||
  98. (Measurement->Identifier == MODULE_END_ID) ||
  99. (Measurement->Identifier == MODULE_LOADIMAGE_START_ID) ||
  100. (Measurement->Identifier == MODULE_LOADIMAGE_END_ID) ||
  101. (Measurement->Identifier == MODULE_DB_START_ID) ||
  102. (Measurement->Identifier == MODULE_DB_END_ID) ||
  103. (Measurement->Identifier == MODULE_DB_SUPPORT_START_ID) ||
  104. (Measurement->Identifier == MODULE_DB_SUPPORT_END_ID) ||
  105. (Measurement->Identifier == MODULE_DB_STOP_START_ID) ||
  106. (Measurement->Identifier == MODULE_DB_STOP_START_ID))
  107. );
  108. return RetVal;
  109. }
  110. /**
  111. Get the file name portion of the Pdb File Name.
  112. The portion of the Pdb File Name between the last backslash and
  113. either a following period or the end of the string is converted
  114. to Unicode and copied into UnicodeBuffer. The name is truncated,
  115. if necessary, to ensure that UnicodeBuffer is not overrun.
  116. @param[in] PdbFileName Pdb file name.
  117. @param[out] UnicodeBuffer The resultant Unicode File Name.
  118. **/
  119. VOID
  120. DpGetShortPdbFileName (
  121. IN CHAR8 *PdbFileName,
  122. OUT CHAR16 *UnicodeBuffer
  123. )
  124. {
  125. UINTN IndexA; // Current work location within an ASCII string.
  126. UINTN IndexU; // Current work location within a Unicode string.
  127. UINTN StartIndex;
  128. UINTN EndIndex;
  129. ZeroMem (UnicodeBuffer, (DP_GAUGE_STRING_LENGTH + 1) * sizeof (CHAR16));
  130. if (PdbFileName == NULL) {
  131. StrnCpyS (UnicodeBuffer, DP_GAUGE_STRING_LENGTH + 1, L" ", 1);
  132. } else {
  133. StartIndex = 0;
  134. for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++) {
  135. }
  136. for (IndexA = 0; PdbFileName[IndexA] != 0; IndexA++) {
  137. if ((PdbFileName[IndexA] == '\\') || (PdbFileName[IndexA] == '/')) {
  138. StartIndex = IndexA + 1;
  139. }
  140. if (PdbFileName[IndexA] == '.') {
  141. EndIndex = IndexA;
  142. }
  143. }
  144. IndexU = 0;
  145. for (IndexA = StartIndex; IndexA < EndIndex; IndexA++) {
  146. UnicodeBuffer[IndexU] = (CHAR16)PdbFileName[IndexA];
  147. IndexU++;
  148. if (IndexU >= DP_GAUGE_STRING_LENGTH) {
  149. UnicodeBuffer[DP_GAUGE_STRING_LENGTH] = 0;
  150. break;
  151. }
  152. }
  153. }
  154. }
  155. /**
  156. Get a human readable name for an image handle.
  157. The following methods will be tried orderly:
  158. 1. Image PDB
  159. 2. ComponentName2 protocol
  160. 3. FFS UI section
  161. 4. Image GUID
  162. 5. Image DevicePath
  163. 6. Unknown Driver Name
  164. @param[in] Handle
  165. @post The resulting Unicode name string is stored in the
  166. mGaugeString global array.
  167. **/
  168. VOID
  169. DpGetNameFromHandle (
  170. IN EFI_HANDLE Handle
  171. )
  172. {
  173. EFI_STATUS Status;
  174. EFI_LOADED_IMAGE_PROTOCOL *Image;
  175. CHAR8 *PdbFileName;
  176. EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
  177. EFI_STRING StringPtr;
  178. EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath;
  179. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  180. EFI_GUID *NameGuid;
  181. CHAR16 *NameString;
  182. UINTN StringSize;
  183. CHAR8 *PlatformLanguage;
  184. CHAR8 *BestLanguage;
  185. EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
  186. Image = NULL;
  187. LoadedImageDevicePath = NULL;
  188. DevicePath = NULL;
  189. //
  190. // Method 1: Get the name string from image PDB
  191. //
  192. Status = gBS->HandleProtocol (
  193. Handle,
  194. &gEfiLoadedImageProtocolGuid,
  195. (VOID **)&Image
  196. );
  197. if (EFI_ERROR (Status)) {
  198. Status = gBS->OpenProtocol (
  199. Handle,
  200. &gEfiDriverBindingProtocolGuid,
  201. (VOID **)&DriverBinding,
  202. NULL,
  203. NULL,
  204. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  205. );
  206. if (!EFI_ERROR (Status)) {
  207. Status = gBS->HandleProtocol (
  208. DriverBinding->ImageHandle,
  209. &gEfiLoadedImageProtocolGuid,
  210. (VOID **)&Image
  211. );
  212. }
  213. }
  214. if (!EFI_ERROR (Status)) {
  215. PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);
  216. if (PdbFileName != NULL) {
  217. DpGetShortPdbFileName (PdbFileName, mGaugeString);
  218. return;
  219. }
  220. }
  221. //
  222. // Method 2: Get the name string from ComponentName2 protocol
  223. //
  224. Status = gBS->HandleProtocol (
  225. Handle,
  226. &gEfiComponentName2ProtocolGuid,
  227. (VOID **)&ComponentName2
  228. );
  229. if (!EFI_ERROR (Status)) {
  230. //
  231. // Firstly use platform language setting, secondly use driver's first supported language.
  232. //
  233. GetVariable2 (L"PlatformLang", &gEfiGlobalVariableGuid, (VOID **)&PlatformLanguage, NULL);
  234. BestLanguage = GetBestLanguage (
  235. ComponentName2->SupportedLanguages,
  236. FALSE,
  237. (PlatformLanguage != NULL) ? PlatformLanguage : "",
  238. ComponentName2->SupportedLanguages,
  239. NULL
  240. );
  241. SHELL_FREE_NON_NULL (PlatformLanguage);
  242. Status = ComponentName2->GetDriverName (
  243. ComponentName2,
  244. BestLanguage != NULL ? BestLanguage : "en-US",
  245. &StringPtr
  246. );
  247. if (!EFI_ERROR (Status)) {
  248. SHELL_FREE_NON_NULL (BestLanguage);
  249. StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);
  250. mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;
  251. return;
  252. }
  253. }
  254. Status = gBS->HandleProtocol (
  255. Handle,
  256. &gEfiLoadedImageDevicePathProtocolGuid,
  257. (VOID **)&LoadedImageDevicePath
  258. );
  259. if (!EFI_ERROR (Status) && (LoadedImageDevicePath != NULL)) {
  260. DevicePath = LoadedImageDevicePath;
  261. } else if (Image != NULL) {
  262. DevicePath = Image->FilePath;
  263. }
  264. if (DevicePath != NULL) {
  265. //
  266. // Try to get image GUID from image DevicePath
  267. //
  268. NameGuid = NULL;
  269. while (!IsDevicePathEndType (DevicePath)) {
  270. NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)DevicePath);
  271. if (NameGuid != NULL) {
  272. break;
  273. }
  274. DevicePath = NextDevicePathNode (DevicePath);
  275. }
  276. if (NameGuid != NULL) {
  277. //
  278. // Try to get the image's FFS UI section by image GUID
  279. //
  280. NameString = NULL;
  281. StringSize = 0;
  282. Status = GetSectionFromAnyFv (
  283. NameGuid,
  284. EFI_SECTION_USER_INTERFACE,
  285. 0,
  286. (VOID **)&NameString,
  287. &StringSize
  288. );
  289. if (!EFI_ERROR (Status)) {
  290. //
  291. // Method 3. Get the name string from FFS UI section
  292. //
  293. StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);
  294. mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;
  295. FreePool (NameString);
  296. } else {
  297. //
  298. // Method 4: Get the name string from image GUID
  299. //
  300. UnicodeSPrint (mGaugeString, sizeof (mGaugeString), L"%g", NameGuid);
  301. }
  302. return;
  303. } else {
  304. //
  305. // Method 5: Get the name string from image DevicePath
  306. //
  307. NameString = ConvertDevicePathToText (DevicePath, TRUE, FALSE);
  308. if (NameString != NULL) {
  309. StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);
  310. mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;
  311. FreePool (NameString);
  312. return;
  313. }
  314. }
  315. }
  316. //
  317. // Method 6: Unknown Driver Name
  318. //
  319. StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);
  320. ASSERT (StringPtr != NULL);
  321. StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);
  322. FreePool (StringPtr);
  323. }
  324. /**
  325. Calculate the Duration in microseconds.
  326. Duration is multiplied by 1000, instead of Frequency being divided by 1000 or
  327. multiplying the result by 1000, in order to maintain precision. Since Duration is
  328. a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.
  329. The time is calculated as (Duration * 1000) / Timer_Frequency.
  330. @param[in] Duration The event duration in timer ticks.
  331. @return A 64-bit value which is the Elapsed time in microseconds.
  332. **/
  333. UINT64
  334. DurationInMicroSeconds (
  335. IN UINT64 Duration
  336. )
  337. {
  338. return DivU64x32 (Duration, 1000);
  339. }
  340. /**
  341. Get index of Measurement Record's match in the CumData array.
  342. If the Measurement's Token value matches a Token in one of the CumData
  343. records, the index of the matching record is returned. The returned
  344. index is a signed value so that negative values can indicate that
  345. the Measurement didn't match any entry in the CumData array.
  346. @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.
  347. @retval <0 Token is not in the CumData array.
  348. @retval >=0 Return value is the index into CumData where Token is found.
  349. **/
  350. INTN
  351. GetCumulativeItem (
  352. IN MEASUREMENT_RECORD *Measurement
  353. )
  354. {
  355. INTN Index;
  356. for ( Index = 0; Index < (INTN)NumCum; ++Index) {
  357. if (AsciiStrCmp (Measurement->Token, CumData[Index].Name) == 0) {
  358. return Index; // Exit, we found a match
  359. }
  360. }
  361. // If the for loop exits, Token was not found.
  362. return -1; // Indicate failure
  363. }