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