]> git.proxmox.com Git - mirror_edk2.git/blame - PerformancePkg/Dp_App/DpUtilities.c
Update all the code to consume the ConvertDevicePathToText, ConvertDevicePathNodeToTe...
[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
863986b3 4 Copyright (c) 2009 - 2013, 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
159 ZeroMem (UnicodeBuffer, DXE_PERFORMANCE_STRING_LENGTH * sizeof (CHAR16));\r
160\r
161 if (PdbFileName == NULL) {\r
162 StrCpy (UnicodeBuffer, L" ");\r
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
181 if (IndexU >= DXE_PERFORMANCE_STRING_LENGTH) {\r
182 UnicodeBuffer[DXE_PERFORMANCE_STRING_LENGTH] = 0;\r
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
221 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
c06ad33e 222\r
a2daf8db
SZ
223 //\r
224 // Method 1: Get the name string from image PDB\r
c06ad33e 225 //\r
226 Status = gBS->HandleProtocol (\r
a2daf8db
SZ
227 Handle,\r
228 &gEfiLoadedImageProtocolGuid,\r
229 (VOID **) &Image\r
230 );\r
c06ad33e 231\r
232 if (EFI_ERROR (Status)) {\r
233 Status = gBS->OpenProtocol (\r
a2daf8db
SZ
234 Handle,\r
235 &gEfiDriverBindingProtocolGuid,\r
236 (VOID **) &DriverBinding,\r
237 NULL,\r
238 NULL,\r
239 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
240 );\r
241 if (!EFI_ERROR (Status)) {\r
242 Status = gBS->HandleProtocol (\r
243 DriverBinding->ImageHandle,\r
244 &gEfiLoadedImageProtocolGuid,\r
245 (VOID **) &Image\r
246 );\r
247 }\r
248 }\r
249\r
250 if (!EFI_ERROR (Status)) {\r
251 PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);\r
252\r
253 if (PdbFileName != NULL) {\r
254 GetShortPdbFileName (PdbFileName, mGaugeString);\r
255 return;\r
256 }\r
257 }\r
258\r
259 //\r
260 // Method 2: Get the name string from ComponentName2 protocol\r
261 //\r
262 Status = gBS->HandleProtocol (\r
c06ad33e 263 Handle,\r
a2daf8db
SZ
264 &gEfiComponentName2ProtocolGuid,\r
265 (VOID **) &ComponentName2\r
c06ad33e 266 );\r
a2daf8db
SZ
267 if (!EFI_ERROR (Status)) {\r
268 //\r
269 // Get the current platform language setting\r
270 //\r
f01b91ae 271 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);\r
a2daf8db
SZ
272 Status = ComponentName2->GetDriverName (\r
273 ComponentName2,\r
274 PlatformLanguage != NULL ? PlatformLanguage : "en-US",\r
275 &StringPtr\r
276 );\r
277 if (!EFI_ERROR (Status)) {\r
278 SafeFreePool (PlatformLanguage);\r
279 StrnCpy (mGaugeString, StringPtr, DP_GAUGE_STRING_LENGTH);\r
280 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
281 return;\r
c06ad33e 282 }\r
a2daf8db 283 }\r
c06ad33e 284\r
a2daf8db
SZ
285 Status = gBS->HandleProtocol (\r
286 Handle,\r
287 &gEfiLoadedImageDevicePathProtocolGuid,\r
288 (VOID **) &LoadedImageDevicePath\r
c06ad33e 289 );\r
7ea7eee3 290 if (!EFI_ERROR (Status) && (LoadedImageDevicePath != NULL)) {\r
a2daf8db 291 DevicePath = LoadedImageDevicePath;\r
c06ad33e 292\r
a2daf8db
SZ
293 //\r
294 // Try to get image GUID from LoadedImageDevicePath protocol\r
295 //\r
296 NameGuid = NULL;\r
297 while (!IsDevicePathEndType (DevicePath)) {\r
298 NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath);\r
299 if (NameGuid != NULL) {\r
300 break;\r
301 }\r
302 DevicePath = NextDevicePathNode (DevicePath);\r
303 }\r
c06ad33e 304\r
a2daf8db
SZ
305 if (NameGuid != NULL) {\r
306 //\r
307 // Try to get the image's FFS UI section by image GUID\r
308 //\r
309 NameString = NULL;\r
310 StringSize = 0;\r
311 Status = GetSectionFromAnyFv (\r
312 NameGuid,\r
313 EFI_SECTION_USER_INTERFACE,\r
314 0,\r
f7fe94ab 315 (VOID **) &NameString,\r
a2daf8db
SZ
316 &StringSize\r
317 );\r
318\r
319 if (!EFI_ERROR (Status)) {\r
320 //\r
321 // Method 3. Get the name string from FFS UI section\r
322 //\r
323 StrnCpy (mGaugeString, NameString, DP_GAUGE_STRING_LENGTH);\r
324 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
325 FreePool (NameString);\r
326 } else {\r
327 //\r
328 // Method 4: Get the name string from image GUID\r
329 //\r
330 UnicodeSPrint (mGaugeString, sizeof (mGaugeString), L"%g", NameGuid);\r
331 }\r
332 return;\r
333 } else {\r
334 //\r
335 // Method 5: Get the name string from image DevicePath\r
336 //\r
863986b3
RN
337 NameString = ConvertDevicePathToText (LoadedImageDevicePath, TRUE, FALSE);\r
338 if (NameString != NULL) {\r
339 StrnCpy (mGaugeString, NameString, DP_GAUGE_STRING_LENGTH);\r
340 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
341 FreePool (NameString);\r
342 return;\r
a2daf8db
SZ
343 }\r
344 }\r
c06ad33e 345 }\r
a2daf8db
SZ
346\r
347 //\r
348 // Method 6: Unknown Driver Name\r
349 //\r
350 StringPtr = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);\r
351 ASSERT (StringPtr != NULL);\r
352 StrCpy (mGaugeString, StringPtr);\r
88359546 353 FreePool (StringPtr);\r
a2daf8db 354 return;\r
c06ad33e 355}\r
356\r
9dd74618
ED
357/** \r
358 Calculate the Duration in microseconds.\r
359 \r
360 Duration is multiplied by 1000, instead of Frequency being divided by 1000 or\r
361 multiplying the result by 1000, in order to maintain precision. Since Duration is\r
362 a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.\r
363 \r
364 The time is calculated as (Duration * 1000) / Timer_Frequency.\r
365 \r
366 @param[in] Duration The event duration in timer ticks.\r
367 \r
368 @return A 64-bit value which is the Elapsed time in microseconds.\r
c06ad33e 369**/\r
370UINT64\r
371DurationInMicroSeconds (\r
372 IN UINT64 Duration\r
373 )\r
374{\r
375 UINT64 Temp;\r
376\r
377 Temp = MultU64x32 (Duration, 1000);\r
378 return DivU64x32 (Temp, TimerInfo.Frequency);\r
379}\r
380\r
9dd74618
ED
381/** \r
382 Formatted Print using a Hii Token to reference the localized format string.\r
383 \r
384 @param[in] Token A HII token associated with a localized Unicode string.\r
385 @param[in] ... The variable argument list.\r
386 \r
387 @return The number of characters converted by UnicodeVSPrint().\r
388 \r
c06ad33e 389**/\r
390UINTN\r
391PrintToken (\r
392 IN UINT16 Token,\r
393 ...\r
394 )\r
395{\r
396 VA_LIST Marker;\r
397 EFI_STRING StringPtr;\r
398 UINTN Return;\r
399 UINTN BufferSize;\r
400\r
401 StringPtr = HiiGetString (gHiiHandle, Token, NULL);\r
402 ASSERT (StringPtr != NULL);\r
403\r
404 VA_START (Marker, Token);\r
405\r
406 BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);\r
407\r
408 if (mPrintTokenBuffer == NULL) {\r
409 mPrintTokenBuffer = AllocatePool (BufferSize);\r
410 ASSERT (mPrintTokenBuffer != NULL);\r
411 }\r
412 SetMem( mPrintTokenBuffer, BufferSize, 0);\r
413\r
414 Return = UnicodeVSPrint (mPrintTokenBuffer, BufferSize, StringPtr, Marker);\r
3bbe68a3 415 VA_END (Marker);\r
416 \r
c06ad33e 417 if (Return > 0 && gST->ConOut != NULL) {\r
418 gST->ConOut->OutputString (gST->ConOut, mPrintTokenBuffer);\r
419 }\r
88359546 420 FreePool (StringPtr);\r
c06ad33e 421 return Return;\r
422}\r
423\r
9dd74618
ED
424/** \r
425 Get index of Measurement Record's match in the CumData array.\r
426 \r
427 If the Measurement's Token value matches a Token in one of the CumData\r
428 records, the index of the matching record is returned. The returned\r
429 index is a signed value so that negative values can indicate that\r
430 the Measurement didn't match any entry in the CumData array.\r
431 \r
432 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.\r
433 \r
434 @retval <0 Token is not in the CumData array.\r
435 @retval >=0 Return value is the index into CumData where Token is found.\r
c06ad33e 436**/\r
437INTN\r
438GetCumulativeItem(\r
439 IN MEASUREMENT_RECORD *Measurement\r
440 )\r
441{\r
442 INTN Index;\r
443\r
444 for( Index = 0; Index < (INTN)NumCum; ++Index) {\r
445 if (AsciiStrnCmp (Measurement->Token, CumData[Index].Name, PERF_TOKEN_LENGTH) == 0) {\r
446 return Index; // Exit, we found a match\r
447 }\r
448 }\r
449 // If the for loop exits, Token was not found.\r
450 return -1; // Indicate failure\r
451}\r