]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/DynamicCommand/DpDynamicCommand/DpUtilities.c
ShellPkg: Clean up source files
[mirror_edk2.git] / ShellPkg / DynamicCommand / DpDynamicCommand / DpUtilities.c
CommitLineData
d41bc92c 1/** @file\r
2 Utility functions used by the Dp application.\r
3\r
115eae65 4 Copyright (c) 2009 - 2018, 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
ba0014b9 42/**\r
d41bc92c 43 Calculate an event's duration in timer ticks.\r
ba0014b9 44\r
d41bc92c 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
ba0014b9 48\r
d41bc92c 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
ba0014b9 53\r
d41bc92c 54 The calculated duration, in ticks, is the absolute difference between\r
55 the measurement's ending and starting counts.\r
ba0014b9 56\r
d41bc92c 57 @param Measurement Pointer to a MEASUREMENT_RECORD structure containing\r
58 data for the current measurement.\r
ba0014b9 59\r
d41bc92c 60 @return The 64-bit duration of the event.\r
61**/\r
62UINT64\r
63GetDuration (\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
115eae65
DB
74 Duration = Measurement->EndTimeStamp - Measurement->StartTimeStamp;\r
75 Error = (BOOLEAN)(Duration > Measurement->EndTimeStamp);\r
d41bc92c 76\r
77 if (Error) {\r
78 DEBUG ((EFI_D_ERROR, ALit_TimerLibError));\r
79 Duration = 0;\r
80 }\r
81 return Duration;\r
82}\r
83\r
ba0014b9 84/**\r
d41bc92c 85 Determine whether the Measurement record is for an EFI Phase.\r
ba0014b9 86\r
d41bc92c 87 The Token and Module members of the measurement record are checked.\r
88 Module must be empty and Token must be one of SEC, PEI, DXE, BDS, or SHELL.\r
ba0014b9 89\r
d41bc92c 90 @param[in] Measurement A pointer to the Measurement record to test.\r
ba0014b9 91\r
d41bc92c 92 @retval TRUE The measurement record is for an EFI Phase.\r
93 @retval FALSE The measurement record is NOT for an EFI Phase.\r
94**/\r
95BOOLEAN\r
96IsPhase(\r
97 IN MEASUREMENT_RECORD *Measurement\r
98 )\r
99{\r
100 BOOLEAN RetVal;\r
101\r
115eae65
DB
102 RetVal = (BOOLEAN)(\r
103 ((AsciiStrCmp (Measurement->Token, ALit_SEC) == 0) ||\r
104 (AsciiStrCmp (Measurement->Token, ALit_PEI) == 0) ||\r
105 (AsciiStrCmp (Measurement->Token, ALit_DXE) == 0) ||\r
106 (AsciiStrCmp (Measurement->Token, ALit_BDS) == 0))\r
d41bc92c 107 );\r
108 return RetVal;\r
109}\r
110\r
f45dd2dd
BD
111/**\r
112 Determine whether the Measurement record is for core code.\r
113\r
114 @param[in] Measurement A pointer to the Measurement record to test.\r
115\r
116 @retval TRUE The measurement record is used for core.\r
117 @retval FALSE The measurement record is NOT used for core.\r
118\r
119**/\r
120BOOLEAN\r
121IsCorePerf(\r
122 IN MEASUREMENT_RECORD *Measurement\r
123 )\r
124{\r
125 BOOLEAN RetVal;\r
126\r
127 RetVal = (BOOLEAN)(\r
128 ((Measurement->Identifier == MODULE_START_ID) ||\r
129 (Measurement->Identifier == MODULE_END_ID) ||\r
130 (Measurement->Identifier == MODULE_LOADIMAGE_START_ID) ||\r
131 (Measurement->Identifier == MODULE_LOADIMAGE_END_ID) ||\r
132 (Measurement->Identifier == MODULE_DB_START_ID) ||\r
133 (Measurement->Identifier == MODULE_DB_END_ID) ||\r
134 (Measurement->Identifier == MODULE_DB_SUPPORT_START_ID) ||\r
135 (Measurement->Identifier == MODULE_DB_SUPPORT_END_ID) ||\r
136 (Measurement->Identifier == MODULE_DB_STOP_START_ID) ||\r
137 (Measurement->Identifier == MODULE_DB_STOP_START_ID))\r
138 );\r
139 return RetVal;\r
140}\r
141\r
ba0014b9 142/**\r
d41bc92c 143 Get the file name portion of the Pdb File Name.\r
ba0014b9 144\r
d41bc92c 145 The portion of the Pdb File Name between the last backslash and\r
146 either a following period or the end of the string is converted\r
147 to Unicode and copied into UnicodeBuffer. The name is truncated,\r
148 if necessary, to ensure that UnicodeBuffer is not overrun.\r
ba0014b9 149\r
d41bc92c 150 @param[in] PdbFileName Pdb file name.\r
151 @param[out] UnicodeBuffer The resultant Unicode File Name.\r
ba0014b9 152\r
d41bc92c 153**/\r
154VOID\r
2c55a81a 155DpGetShortPdbFileName (\r
d41bc92c 156 IN CHAR8 *PdbFileName,\r
157 OUT CHAR16 *UnicodeBuffer\r
158 )\r
159{\r
160 UINTN IndexA; // Current work location within an ASCII string.\r
161 UINTN IndexU; // Current work location within a Unicode string.\r
162 UINTN StartIndex;\r
163 UINTN EndIndex;\r
164\r
e75390f0 165 ZeroMem (UnicodeBuffer, (DP_GAUGE_STRING_LENGTH + 1) * sizeof (CHAR16));\r
d41bc92c 166\r
167 if (PdbFileName == NULL) {\r
e75390f0 168 StrnCpyS (UnicodeBuffer, DP_GAUGE_STRING_LENGTH + 1, L" ", 1);\r
d41bc92c 169 } else {\r
170 StartIndex = 0;\r
171 for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++)\r
172 ;\r
173 for (IndexA = 0; PdbFileName[IndexA] != 0; IndexA++) {\r
9248d551 174 if ((PdbFileName[IndexA] == '\\') || (PdbFileName[IndexA] == '/')) {\r
d41bc92c 175 StartIndex = IndexA + 1;\r
176 }\r
177\r
178 if (PdbFileName[IndexA] == '.') {\r
179 EndIndex = IndexA;\r
180 }\r
181 }\r
182\r
183 IndexU = 0;\r
184 for (IndexA = StartIndex; IndexA < EndIndex; IndexA++) {\r
185 UnicodeBuffer[IndexU] = (CHAR16) PdbFileName[IndexA];\r
186 IndexU++;\r
b9ffeab7
SZ
187 if (IndexU >= DP_GAUGE_STRING_LENGTH) {\r
188 UnicodeBuffer[DP_GAUGE_STRING_LENGTH] = 0;\r
d41bc92c 189 break;\r
190 }\r
191 }\r
192 }\r
193}\r
194\r
ba0014b9 195/**\r
d41bc92c 196 Get a human readable name for an image handle.\r
197 The following methods will be tried orderly:\r
198 1. Image PDB\r
199 2. ComponentName2 protocol\r
200 3. FFS UI section\r
201 4. Image GUID\r
202 5. Image DevicePath\r
203 6. Unknown Driver Name\r
204\r
205 @param[in] Handle\r
206\r
207 @post The resulting Unicode name string is stored in the\r
208 mGaugeString global array.\r
209\r
210**/\r
211VOID\r
2c55a81a 212DpGetNameFromHandle (\r
d41bc92c 213 IN EFI_HANDLE Handle\r
214 )\r
215{\r
92034c4c
RN
216 EFI_STATUS Status;\r
217 EFI_LOADED_IMAGE_PROTOCOL *Image;\r
218 CHAR8 *PdbFileName;\r
219 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;\r
220 EFI_STRING StringPtr;\r
221 EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath;\r
222 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
223 EFI_GUID *NameGuid;\r
224 CHAR16 *NameString;\r
225 UINTN StringSize;\r
226 CHAR8 *PlatformLanguage;\r
227 CHAR8 *BestLanguage;\r
228 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
d41bc92c 229\r
3b672240
SZ
230 Image = NULL;\r
231 LoadedImageDevicePath = NULL;\r
232 DevicePath = NULL;\r
233\r
d41bc92c 234 //\r
235 // Method 1: Get the name string from image PDB\r
236 //\r
237 Status = gBS->HandleProtocol (\r
238 Handle,\r
239 &gEfiLoadedImageProtocolGuid,\r
240 (VOID **) &Image\r
241 );\r
242\r
243 if (EFI_ERROR (Status)) {\r
244 Status = gBS->OpenProtocol (\r
245 Handle,\r
246 &gEfiDriverBindingProtocolGuid,\r
247 (VOID **) &DriverBinding,\r
248 NULL,\r
249 NULL,\r
250 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
251 );\r
252 if (!EFI_ERROR (Status)) {\r
253 Status = gBS->HandleProtocol (\r
254 DriverBinding->ImageHandle,\r
255 &gEfiLoadedImageProtocolGuid,\r
256 (VOID **) &Image\r
257 );\r
258 }\r
259 }\r
260\r
261 if (!EFI_ERROR (Status)) {\r
262 PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);\r
263\r
264 if (PdbFileName != NULL) {\r
2c55a81a 265 DpGetShortPdbFileName (PdbFileName, mGaugeString);\r
d41bc92c 266 return;\r
267 }\r
268 }\r
269\r
270 //\r
271 // Method 2: Get the name string from ComponentName2 protocol\r
272 //\r
273 Status = gBS->HandleProtocol (\r
274 Handle,\r
275 &gEfiComponentName2ProtocolGuid,\r
276 (VOID **) &ComponentName2\r
277 );\r
278 if (!EFI_ERROR (Status)) {\r
279 //\r
92034c4c 280 // Firstly use platform language setting, secondly use driver's first supported language.\r
d41bc92c 281 //\r
92034c4c
RN
282 GetVariable2 (L"PlatformLang", &gEfiGlobalVariableGuid, (VOID**)&PlatformLanguage, NULL);\r
283 BestLanguage = GetBestLanguage(\r
284 ComponentName2->SupportedLanguages,\r
285 FALSE,\r
286 (PlatformLanguage != NULL) ? PlatformLanguage : "",\r
287 ComponentName2->SupportedLanguages,\r
288 NULL\r
289 );\r
290 SHELL_FREE_NON_NULL (PlatformLanguage);\r
291\r
d41bc92c 292 Status = ComponentName2->GetDriverName (\r
293 ComponentName2,\r
92034c4c 294 BestLanguage != NULL ? BestLanguage : "en-US",\r
d41bc92c 295 &StringPtr\r
296 );\r
297 if (!EFI_ERROR (Status)) {\r
92034c4c 298 SHELL_FREE_NON_NULL (BestLanguage);\r
4dc0d578 299 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);\r
d41bc92c 300 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
301 return;\r
302 }\r
303 }\r
304\r
305 Status = gBS->HandleProtocol (\r
306 Handle,\r
307 &gEfiLoadedImageDevicePathProtocolGuid,\r
308 (VOID **) &LoadedImageDevicePath\r
309 );\r
310 if (!EFI_ERROR (Status) && (LoadedImageDevicePath != NULL)) {\r
311 DevicePath = LoadedImageDevicePath;\r
3b672240
SZ
312 } else if (Image != NULL) {\r
313 DevicePath = Image->FilePath;\r
314 }\r
d41bc92c 315\r
3b672240 316 if (DevicePath != NULL) {\r
d41bc92c 317 //\r
3b672240 318 // Try to get image GUID from image DevicePath\r
d41bc92c 319 //\r
320 NameGuid = NULL;\r
321 while (!IsDevicePathEndType (DevicePath)) {\r
322 NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath);\r
323 if (NameGuid != NULL) {\r
324 break;\r
325 }\r
326 DevicePath = NextDevicePathNode (DevicePath);\r
327 }\r
328\r
329 if (NameGuid != NULL) {\r
330 //\r
331 // Try to get the image's FFS UI section by image GUID\r
332 //\r
333 NameString = NULL;\r
334 StringSize = 0;\r
335 Status = GetSectionFromAnyFv (\r
336 NameGuid,\r
337 EFI_SECTION_USER_INTERFACE,\r
338 0,\r
339 (VOID **) &NameString,\r
340 &StringSize\r
341 );\r
342\r
343 if (!EFI_ERROR (Status)) {\r
344 //\r
345 // Method 3. Get the name string from FFS UI section\r
346 //\r
4dc0d578 347 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);\r
d41bc92c 348 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
349 FreePool (NameString);\r
350 } else {\r
351 //\r
352 // Method 4: Get the name string from image GUID\r
353 //\r
354 UnicodeSPrint (mGaugeString, sizeof (mGaugeString), L"%g", NameGuid);\r
355 }\r
356 return;\r
357 } else {\r
358 //\r
359 // Method 5: Get the name string from image DevicePath\r
360 //\r
3b672240 361 NameString = ConvertDevicePathToText (DevicePath, TRUE, FALSE);\r
863986b3 362 if (NameString != NULL) {\r
4dc0d578 363 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);\r
863986b3
RN
364 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
365 FreePool (NameString);\r
366 return;\r
d41bc92c 367 }\r
368 }\r
369 }\r
370\r
371 //\r
372 // Method 6: Unknown Driver Name\r
373 //\r
92034c4c 374 StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);\r
d41bc92c 375 ASSERT (StringPtr != NULL);\r
4dc0d578 376 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);\r
d41bc92c 377 FreePool (StringPtr);\r
378}\r
379\r
ba0014b9 380/**\r
d41bc92c 381 Calculate the Duration in microseconds.\r
ba0014b9 382\r
d41bc92c 383 Duration is multiplied by 1000, instead of Frequency being divided by 1000 or\r
384 multiplying the result by 1000, in order to maintain precision. Since Duration is\r
385 a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.\r
ba0014b9 386\r
d41bc92c 387 The time is calculated as (Duration * 1000) / Timer_Frequency.\r
ba0014b9 388\r
d41bc92c 389 @param[in] Duration The event duration in timer ticks.\r
ba0014b9 390\r
d41bc92c 391 @return A 64-bit value which is the Elapsed time in microseconds.\r
392**/\r
393UINT64\r
394DurationInMicroSeconds (\r
395 IN UINT64 Duration\r
396 )\r
397{\r
115eae65 398 return DivU64x32 (Duration, 1000);\r
d41bc92c 399}\r
400\r
ba0014b9 401/**\r
d41bc92c 402 Get index of Measurement Record's match in the CumData array.\r
ba0014b9 403\r
d41bc92c 404 If the Measurement's Token value matches a Token in one of the CumData\r
405 records, the index of the matching record is returned. The returned\r
406 index is a signed value so that negative values can indicate that\r
407 the Measurement didn't match any entry in the CumData array.\r
ba0014b9 408\r
d41bc92c 409 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.\r
ba0014b9 410\r
d41bc92c 411 @retval <0 Token is not in the CumData array.\r
412 @retval >=0 Return value is the index into CumData where Token is found.\r
413**/\r
414INTN\r
415GetCumulativeItem(\r
416 IN MEASUREMENT_RECORD *Measurement\r
417 )\r
418{\r
419 INTN Index;\r
420\r
421 for( Index = 0; Index < (INTN)NumCum; ++Index) {\r
115eae65 422 if (AsciiStrCmp (Measurement->Token, CumData[Index].Name) == 0) {\r
d41bc92c 423 return Index; // Exit, we found a match\r
424 }\r
425 }\r
426 // If the for loop exits, Token was not found.\r
427 return -1; // Indicate failure\r
428}\r