]> git.proxmox.com Git - mirror_edk2.git/blame - PerformancePkg/Dp_App/DpUtilities.c
PerformancePkg Dp_App: Fix ASSERT in GetNameFromHandle
[mirror_edk2.git] / PerformancePkg / Dp_App / DpUtilities.c
CommitLineData
c06ad33e 1/** @file\r
86da563d
ED
2 Utility functions used by the Dp application.\r
3\r
ecd58a25 4 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
86da563d
ED
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
c06ad33e 12**/\r
13\r
14#include <Library/BaseLib.h>\r
15#include <Library/BaseMemoryLib.h>\r
16#include <Library/MemoryAllocationLib.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/TimerLib.h>\r
20#include <Library/PeCoffGetEntryPointLib.h>\r
21#include <Library/PrintLib.h>\r
22#include <Library/HiiLib.h>\r
23#include <Library/PcdLib.h>\r
a2daf8db
SZ
24#include <Library/UefiLib.h>\r
25#include <Library/DevicePathLib.h>\r
26\r
27#include <Pi/PiFirmwareFile.h>\r
28#include <Library/DxeServicesLib.h>\r
c06ad33e 29\r
30#include <Protocol/LoadedImage.h>\r
16d5d168 31#include <Protocol/DriverBinding.h>\r
a2daf8db
SZ
32#include <Protocol/ComponentName2.h>\r
33#include <Protocol/DevicePath.h>\r
c06ad33e 34\r
35#include <Guid/Performance.h>\r
36\r
37#include "Dp.h"\r
38#include "Literals.h"\r
39#include "DpInternal.h"\r
40\r
a2daf8db
SZ
41/**\r
42 Wrap original FreePool to check NULL pointer first.\r
43\r
44 @param[in] Buffer The pointer to the buffer to free.\r
45\r
46**/\r
47VOID\r
48SafeFreePool (\r
49 IN VOID *Buffer\r
50 )\r
51{\r
52 if (Buffer != NULL) {\r
53 FreePool (Buffer);\r
54 }\r
55}\r
56\r
9dd74618
ED
57/** \r
58 Calculate an event's duration in timer ticks.\r
59 \r
60 Given the count direction and the event's start and end timer values,\r
61 calculate the duration of the event in timer ticks. Information for\r
62 the current measurement is pointed to by the parameter.\r
63 \r
64 If the measurement's start time is 1, it indicates that the developer\r
65 is indicating that the measurement began at the release of reset.\r
66 The start time is adjusted to the timer's starting count before performing\r
67 the elapsed time calculation.\r
68 \r
69 The calculated duration, in ticks, is the absolute difference between\r
70 the measurement's ending and starting counts.\r
71 \r
72 @param Measurement Pointer to a MEASUREMENT_RECORD structure containing\r
73 data for the current measurement.\r
74 \r
75 @return The 64-bit duration of the event.\r
c06ad33e 76**/\r
77UINT64\r
78GetDuration (\r
79 IN OUT MEASUREMENT_RECORD *Measurement\r
80 )\r
81{\r
82 UINT64 Duration;\r
83 BOOLEAN Error;\r
84\r
85 // PERF_START macros are called with a value of 1 to indicate\r
86 // the beginning of time. So, adjust the start ticker value\r
87 // to the real beginning of time.\r
88 // Assumes no wraparound. Even then, there is a very low probability\r
89 // of having a valid StartTicker value of 1.\r
90 if (Measurement->StartTimeStamp == 1) {\r
91 Measurement->StartTimeStamp = TimerInfo.StartCount;\r
92 }\r
93 if (TimerInfo.CountUp) {\r
94 Duration = Measurement->EndTimeStamp - Measurement->StartTimeStamp;\r
95 Error = (BOOLEAN)(Duration > Measurement->EndTimeStamp);\r
96 }\r
97 else {\r
98 Duration = Measurement->StartTimeStamp - Measurement->EndTimeStamp;\r
99 Error = (BOOLEAN)(Duration > Measurement->StartTimeStamp);\r
100 }\r
101\r
102 if (Error) {\r
103 DEBUG ((EFI_D_ERROR, ALit_TimerLibError));\r
104 Duration = 0;\r
105 }\r
106 return Duration;\r
107}\r
108\r
9dd74618
ED
109/** \r
110 Determine whether the Measurement record is for an EFI Phase.\r
111 \r
112 The Token and Module members of the measurement record are checked.\r
113 Module must be empty and Token must be one of SEC, PEI, DXE, BDS, or SHELL.\r
114 \r
115 @param[in] Measurement A pointer to the Measurement record to test.\r
116 \r
117 @retval TRUE The measurement record is for an EFI Phase.\r
118 @retval FALSE The measurement record is NOT for an EFI Phase.\r
c06ad33e 119**/\r
120BOOLEAN\r
121IsPhase(\r
122 IN MEASUREMENT_RECORD *Measurement\r
123 )\r
124{\r
125 BOOLEAN RetVal;\r
126\r
127 RetVal = (BOOLEAN)( ( *Measurement->Module == '\0') &&\r
128 ((AsciiStrnCmp (Measurement->Token, ALit_SEC, PERF_TOKEN_LENGTH) == 0) ||\r
129 (AsciiStrnCmp (Measurement->Token, ALit_PEI, PERF_TOKEN_LENGTH) == 0) ||\r
130 (AsciiStrnCmp (Measurement->Token, ALit_DXE, PERF_TOKEN_LENGTH) == 0) ||\r
131 (AsciiStrnCmp (Measurement->Token, ALit_BDS, PERF_TOKEN_LENGTH) == 0))\r
132 );\r
133 return RetVal;\r
134}\r
135\r
9dd74618
ED
136/** \r
137 Get the file name portion of the Pdb File Name.\r
138 \r
139 The portion of the Pdb File Name between the last backslash and\r
140 either a following period or the end of the string is converted\r
141 to Unicode and copied into UnicodeBuffer. The name is truncated,\r
142 if necessary, to ensure that UnicodeBuffer is not overrun.\r
143 \r
144 @param[in] PdbFileName Pdb file name.\r
145 @param[out] UnicodeBuffer The resultant Unicode File Name.\r
146 \r
c06ad33e 147**/\r
148VOID\r
149GetShortPdbFileName (\r
150 IN CHAR8 *PdbFileName,\r
151 OUT CHAR16 *UnicodeBuffer\r
152 )\r
153{\r
154 UINTN IndexA; // Current work location within an ASCII string.\r
155 UINTN IndexU; // Current work location within a Unicode string.\r
156 UINTN StartIndex;\r
157 UINTN EndIndex;\r
158\r
269e0aeb 159 ZeroMem (UnicodeBuffer, (DP_GAUGE_STRING_LENGTH + 1) * sizeof (CHAR16));\r
c06ad33e 160\r
161 if (PdbFileName == NULL) {\r
269e0aeb 162 StrCpyS (UnicodeBuffer, DP_GAUGE_STRING_LENGTH + 1, L" ");\r
c06ad33e 163 } else {\r
164 StartIndex = 0;\r
165 for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++)\r
166 ;\r
167 for (IndexA = 0; PdbFileName[IndexA] != 0; IndexA++) {\r
168 if (PdbFileName[IndexA] == '\\') {\r
169 StartIndex = IndexA + 1;\r
170 }\r
171\r
172 if (PdbFileName[IndexA] == '.') {\r
173 EndIndex = IndexA;\r
174 }\r
175 }\r
176\r
177 IndexU = 0;\r
178 for (IndexA = StartIndex; IndexA < EndIndex; IndexA++) {\r
179 UnicodeBuffer[IndexU] = (CHAR16) PdbFileName[IndexA];\r
180 IndexU++;\r
269e0aeb
HW
181 if (IndexU >= DP_GAUGE_STRING_LENGTH) {\r
182 UnicodeBuffer[DP_GAUGE_STRING_LENGTH] = 0;\r
c06ad33e 183 break;\r
184 }\r
185 }\r
186 }\r
187}\r
188\r
9dd74618
ED
189/** \r
190 Get a human readable name for an image handle.\r
a2daf8db
SZ
191 The following methods will be tried orderly:\r
192 1. Image PDB\r
193 2. ComponentName2 protocol\r
194 3. FFS UI section\r
195 4. Image GUID\r
196 5. Image DevicePath\r
197 6. Unknown Driver Name\r
198\r
9dd74618 199 @param[in] Handle\r
a2daf8db 200\r
9dd74618
ED
201 @post The resulting Unicode name string is stored in the\r
202 mGaugeString global array.\r
a2daf8db 203\r
c06ad33e 204**/\r
205VOID\r
206GetNameFromHandle (\r
207 IN EFI_HANDLE Handle\r
208 )\r
209{\r
210 EFI_STATUS Status;\r
211 EFI_LOADED_IMAGE_PROTOCOL *Image;\r
212 CHAR8 *PdbFileName;\r
213 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;\r
214 EFI_STRING StringPtr;\r
a2daf8db
SZ
215 EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath;\r
216 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
217 EFI_GUID *NameGuid;\r
218 CHAR16 *NameString;\r
219 UINTN StringSize;\r
220 CHAR8 *PlatformLanguage;\r
10516d29 221 CHAR8 *BestLanguage;\r
a2daf8db 222 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
c06ad33e 223\r
10516d29
ED
224 BestLanguage = NULL;\r
225 PlatformLanguage = NULL;\r
226\r
a2daf8db
SZ
227 //\r
228 // Method 1: Get the name string from image PDB\r
c06ad33e 229 //\r
230 Status = gBS->HandleProtocol (\r
a2daf8db
SZ
231 Handle,\r
232 &gEfiLoadedImageProtocolGuid,\r
233 (VOID **) &Image\r
234 );\r
c06ad33e 235\r
236 if (EFI_ERROR (Status)) {\r
237 Status = gBS->OpenProtocol (\r
a2daf8db
SZ
238 Handle,\r
239 &gEfiDriverBindingProtocolGuid,\r
240 (VOID **) &DriverBinding,\r
241 NULL,\r
242 NULL,\r
243 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
244 );\r
245 if (!EFI_ERROR (Status)) {\r
246 Status = gBS->HandleProtocol (\r
247 DriverBinding->ImageHandle,\r
248 &gEfiLoadedImageProtocolGuid,\r
249 (VOID **) &Image\r
250 );\r
251 }\r
252 }\r
253\r
254 if (!EFI_ERROR (Status)) {\r
255 PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);\r
256\r
257 if (PdbFileName != NULL) {\r
258 GetShortPdbFileName (PdbFileName, mGaugeString);\r
259 return;\r
260 }\r
261 }\r
262\r
263 //\r
264 // Method 2: Get the name string from ComponentName2 protocol\r
265 //\r
266 Status = gBS->HandleProtocol (\r
c06ad33e 267 Handle,\r
a2daf8db
SZ
268 &gEfiComponentName2ProtocolGuid,\r
269 (VOID **) &ComponentName2\r
c06ad33e 270 );\r
a2daf8db
SZ
271 if (!EFI_ERROR (Status)) {\r
272 //\r
273 // Get the current platform language setting\r
274 //\r
f01b91ae 275 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);\r
10516d29
ED
276\r
277 BestLanguage = GetBestLanguage(\r
278 ComponentName2->SupportedLanguages,\r
279 FALSE,\r
280 PlatformLanguage,\r
281 ComponentName2->SupportedLanguages,\r
282 NULL\r
283 );\r
284\r
285 SafeFreePool (PlatformLanguage);\r
a2daf8db
SZ
286 Status = ComponentName2->GetDriverName (\r
287 ComponentName2,\r
10516d29 288 BestLanguage,\r
a2daf8db
SZ
289 &StringPtr\r
290 );\r
10516d29 291 SafeFreePool (BestLanguage);\r
a2daf8db 292 if (!EFI_ERROR (Status)) {\r
6bc4e42f 293 StrnCpyS (\r
ecd58a25
HW
294 mGaugeString,\r
295 DP_GAUGE_STRING_LENGTH + 1,\r
6bc4e42f
HW
296 StringPtr,\r
297 DP_GAUGE_STRING_LENGTH\r
ecd58a25 298 );\r
a2daf8db 299 return;\r
c06ad33e 300 }\r
a2daf8db 301 }\r
c06ad33e 302\r
a2daf8db
SZ
303 Status = gBS->HandleProtocol (\r
304 Handle,\r
305 &gEfiLoadedImageDevicePathProtocolGuid,\r
306 (VOID **) &LoadedImageDevicePath\r
c06ad33e 307 );\r
7ea7eee3 308 if (!EFI_ERROR (Status) && (LoadedImageDevicePath != NULL)) {\r
a2daf8db 309 DevicePath = LoadedImageDevicePath;\r
c06ad33e 310\r
a2daf8db
SZ
311 //\r
312 // Try to get image GUID from LoadedImageDevicePath protocol\r
313 //\r
314 NameGuid = NULL;\r
315 while (!IsDevicePathEndType (DevicePath)) {\r
316 NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath);\r
317 if (NameGuid != NULL) {\r
318 break;\r
319 }\r
320 DevicePath = NextDevicePathNode (DevicePath);\r
321 }\r
c06ad33e 322\r
a2daf8db
SZ
323 if (NameGuid != NULL) {\r
324 //\r
325 // Try to get the image's FFS UI section by image GUID\r
326 //\r
327 NameString = NULL;\r
328 StringSize = 0;\r
329 Status = GetSectionFromAnyFv (\r
330 NameGuid,\r
331 EFI_SECTION_USER_INTERFACE,\r
332 0,\r
f7fe94ab 333 (VOID **) &NameString,\r
a2daf8db
SZ
334 &StringSize\r
335 );\r
336\r
337 if (!EFI_ERROR (Status)) {\r
338 //\r
339 // Method 3. Get the name string from FFS UI section\r
340 //\r
6bc4e42f 341 StrnCpyS (\r
ecd58a25
HW
342 mGaugeString,\r
343 DP_GAUGE_STRING_LENGTH + 1,\r
6bc4e42f
HW
344 NameString,\r
345 DP_GAUGE_STRING_LENGTH\r
ecd58a25 346 );\r
a2daf8db
SZ
347 FreePool (NameString);\r
348 } else {\r
349 //\r
350 // Method 4: Get the name string from image GUID\r
351 //\r
352 UnicodeSPrint (mGaugeString, sizeof (mGaugeString), L"%g", NameGuid);\r
353 }\r
354 return;\r
355 } else {\r
356 //\r
357 // Method 5: Get the name string from image DevicePath\r
358 //\r
863986b3
RN
359 NameString = ConvertDevicePathToText (LoadedImageDevicePath, TRUE, FALSE);\r
360 if (NameString != NULL) {\r
6bc4e42f 361 StrnCpyS (\r
ecd58a25
HW
362 mGaugeString,\r
363 DP_GAUGE_STRING_LENGTH + 1,\r
6bc4e42f
HW
364 NameString,\r
365 DP_GAUGE_STRING_LENGTH\r
ecd58a25 366 );\r
863986b3
RN
367 FreePool (NameString);\r
368 return;\r
a2daf8db
SZ
369 }\r
370 }\r
c06ad33e 371 }\r
a2daf8db
SZ
372\r
373 //\r
374 // Method 6: Unknown Driver Name\r
375 //\r
376 StringPtr = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);\r
377 ASSERT (StringPtr != NULL);\r
ecd58a25 378 StrCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr);\r
88359546 379 FreePool (StringPtr);\r
a2daf8db 380 return;\r
c06ad33e 381}\r
382\r
9dd74618
ED
383/** \r
384 Calculate the Duration in microseconds.\r
385 \r
386 Duration is multiplied by 1000, instead of Frequency being divided by 1000 or\r
387 multiplying the result by 1000, in order to maintain precision. Since Duration is\r
388 a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.\r
389 \r
390 The time is calculated as (Duration * 1000) / Timer_Frequency.\r
391 \r
392 @param[in] Duration The event duration in timer ticks.\r
393 \r
394 @return A 64-bit value which is the Elapsed time in microseconds.\r
c06ad33e 395**/\r
396UINT64\r
397DurationInMicroSeconds (\r
398 IN UINT64 Duration\r
399 )\r
400{\r
401 UINT64 Temp;\r
402\r
403 Temp = MultU64x32 (Duration, 1000);\r
404 return DivU64x32 (Temp, TimerInfo.Frequency);\r
405}\r
406\r
9dd74618
ED
407/** \r
408 Formatted Print using a Hii Token to reference the localized format string.\r
409 \r
410 @param[in] Token A HII token associated with a localized Unicode string.\r
411 @param[in] ... The variable argument list.\r
412 \r
413 @return The number of characters converted by UnicodeVSPrint().\r
414 \r
c06ad33e 415**/\r
416UINTN\r
f85acc22 417EFIAPI\r
c06ad33e 418PrintToken (\r
419 IN UINT16 Token,\r
420 ...\r
421 )\r
422{\r
423 VA_LIST Marker;\r
424 EFI_STRING StringPtr;\r
425 UINTN Return;\r
426 UINTN BufferSize;\r
427\r
428 StringPtr = HiiGetString (gHiiHandle, Token, NULL);\r
429 ASSERT (StringPtr != NULL);\r
430\r
431 VA_START (Marker, Token);\r
432\r
433 BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);\r
434\r
435 if (mPrintTokenBuffer == NULL) {\r
436 mPrintTokenBuffer = AllocatePool (BufferSize);\r
437 ASSERT (mPrintTokenBuffer != NULL);\r
438 }\r
439 SetMem( mPrintTokenBuffer, BufferSize, 0);\r
440\r
441 Return = UnicodeVSPrint (mPrintTokenBuffer, BufferSize, StringPtr, Marker);\r
3bbe68a3 442 VA_END (Marker);\r
443 \r
c06ad33e 444 if (Return > 0 && gST->ConOut != NULL) {\r
445 gST->ConOut->OutputString (gST->ConOut, mPrintTokenBuffer);\r
446 }\r
88359546 447 FreePool (StringPtr);\r
c06ad33e 448 return Return;\r
449}\r
450\r
9dd74618
ED
451/** \r
452 Get index of Measurement Record's match in the CumData array.\r
453 \r
454 If the Measurement's Token value matches a Token in one of the CumData\r
455 records, the index of the matching record is returned. The returned\r
456 index is a signed value so that negative values can indicate that\r
457 the Measurement didn't match any entry in the CumData array.\r
458 \r
459 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.\r
460 \r
461 @retval <0 Token is not in the CumData array.\r
462 @retval >=0 Return value is the index into CumData where Token is found.\r
c06ad33e 463**/\r
464INTN\r
465GetCumulativeItem(\r
466 IN MEASUREMENT_RECORD *Measurement\r
467 )\r
468{\r
469 INTN Index;\r
470\r
471 for( Index = 0; Index < (INTN)NumCum; ++Index) {\r
472 if (AsciiStrnCmp (Measurement->Token, CumData[Index].Name, PERF_TOKEN_LENGTH) == 0) {\r
473 return Index; // Exit, we found a match\r
474 }\r
475 }\r
476 // If the for loop exits, Token was not found.\r
477 return -1; // Indicate failure\r
478}\r