]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiDpLib/DpUtilities.c
Refine the select language logic.
[mirror_edk2.git] / ShellPkg / Library / UefiDpLib / DpUtilities.c
CommitLineData
d41bc92c 1/** @file\r
2 Utility functions used by the Dp application.\r
3\r
4 Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.\r
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
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
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
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
70 // PERF_START macros are called with a value of 1 to indicate\r
71 // the beginning of time. So, adjust the start ticker value\r
72 // to the real beginning of time.\r
73 // Assumes no wraparound. Even then, there is a very low probability\r
74 // of having a valid StartTicker value of 1.\r
75 if (Measurement->StartTimeStamp == 1) {\r
76 Measurement->StartTimeStamp = TimerInfo.StartCount;\r
77 }\r
78 if (TimerInfo.CountUp) {\r
79 Duration = Measurement->EndTimeStamp - Measurement->StartTimeStamp;\r
80 Error = (BOOLEAN)(Duration > Measurement->EndTimeStamp);\r
81 }\r
82 else {\r
83 Duration = Measurement->StartTimeStamp - Measurement->EndTimeStamp;\r
84 Error = (BOOLEAN)(Duration > Measurement->StartTimeStamp);\r
85 }\r
86\r
87 if (Error) {\r
88 DEBUG ((EFI_D_ERROR, ALit_TimerLibError));\r
89 Duration = 0;\r
90 }\r
91 return Duration;\r
92}\r
93\r
94/** \r
95 Determine whether the Measurement record is for an EFI Phase.\r
96 \r
97 The Token and Module members of the measurement record are checked.\r
98 Module must be empty and Token must be one of SEC, PEI, DXE, BDS, or SHELL.\r
99 \r
100 @param[in] Measurement A pointer to the Measurement record to test.\r
101 \r
102 @retval TRUE The measurement record is for an EFI Phase.\r
103 @retval FALSE The measurement record is NOT for an EFI Phase.\r
104**/\r
105BOOLEAN\r
106IsPhase(\r
107 IN MEASUREMENT_RECORD *Measurement\r
108 )\r
109{\r
110 BOOLEAN RetVal;\r
111\r
112 RetVal = (BOOLEAN)( ( *Measurement->Module == '\0') &&\r
113 ((AsciiStrnCmp (Measurement->Token, ALit_SEC, PERF_TOKEN_LENGTH) == 0) ||\r
114 (AsciiStrnCmp (Measurement->Token, ALit_PEI, PERF_TOKEN_LENGTH) == 0) ||\r
115 (AsciiStrnCmp (Measurement->Token, ALit_DXE, PERF_TOKEN_LENGTH) == 0) ||\r
116 (AsciiStrnCmp (Measurement->Token, ALit_BDS, PERF_TOKEN_LENGTH) == 0))\r
117 );\r
118 return RetVal;\r
119}\r
120\r
121/** \r
122 Get the file name portion of the Pdb File Name.\r
123 \r
124 The portion of the Pdb File Name between the last backslash and\r
125 either a following period or the end of the string is converted\r
126 to Unicode and copied into UnicodeBuffer. The name is truncated,\r
127 if necessary, to ensure that UnicodeBuffer is not overrun.\r
128 \r
129 @param[in] PdbFileName Pdb file name.\r
130 @param[out] UnicodeBuffer The resultant Unicode File Name.\r
131 \r
132**/\r
133VOID\r
134GetShortPdbFileName (\r
135 IN CHAR8 *PdbFileName,\r
136 OUT CHAR16 *UnicodeBuffer\r
137 )\r
138{\r
139 UINTN IndexA; // Current work location within an ASCII string.\r
140 UINTN IndexU; // Current work location within a Unicode string.\r
141 UINTN StartIndex;\r
142 UINTN EndIndex;\r
143\r
144 ZeroMem (UnicodeBuffer, DXE_PERFORMANCE_STRING_LENGTH * sizeof (CHAR16));\r
145\r
146 if (PdbFileName == NULL) {\r
147 StrCpy (UnicodeBuffer, L" ");\r
148 } else {\r
149 StartIndex = 0;\r
150 for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++)\r
151 ;\r
152 for (IndexA = 0; PdbFileName[IndexA] != 0; IndexA++) {\r
153 if (PdbFileName[IndexA] == '\\') {\r
154 StartIndex = IndexA + 1;\r
155 }\r
156\r
157 if (PdbFileName[IndexA] == '.') {\r
158 EndIndex = IndexA;\r
159 }\r
160 }\r
161\r
162 IndexU = 0;\r
163 for (IndexA = StartIndex; IndexA < EndIndex; IndexA++) {\r
164 UnicodeBuffer[IndexU] = (CHAR16) PdbFileName[IndexA];\r
165 IndexU++;\r
166 if (IndexU >= DXE_PERFORMANCE_STRING_LENGTH) {\r
167 UnicodeBuffer[DXE_PERFORMANCE_STRING_LENGTH] = 0;\r
168 break;\r
169 }\r
170 }\r
171 }\r
172}\r
173\r
174/** \r
175 Get a human readable name for an image handle.\r
176 The following methods will be tried orderly:\r
177 1. Image PDB\r
178 2. ComponentName2 protocol\r
179 3. FFS UI section\r
180 4. Image GUID\r
181 5. Image DevicePath\r
182 6. Unknown Driver Name\r
183\r
184 @param[in] Handle\r
185\r
186 @post The resulting Unicode name string is stored in the\r
187 mGaugeString global array.\r
188\r
189**/\r
190VOID\r
191GetNameFromHandle (\r
192 IN EFI_HANDLE Handle\r
193 )\r
194{\r
195 EFI_STATUS Status;\r
196 EFI_LOADED_IMAGE_PROTOCOL *Image;\r
197 CHAR8 *PdbFileName;\r
198 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;\r
199 EFI_STRING StringPtr;\r
200 EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath;\r
201 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
202 EFI_GUID *NameGuid;\r
203 CHAR16 *NameString;\r
204 UINTN StringSize;\r
205 CHAR8 *PlatformLanguage;\r
206 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
d41bc92c 207\r
208 //\r
209 // Method 1: Get the name string from image PDB\r
210 //\r
211 Status = gBS->HandleProtocol (\r
212 Handle,\r
213 &gEfiLoadedImageProtocolGuid,\r
214 (VOID **) &Image\r
215 );\r
216\r
217 if (EFI_ERROR (Status)) {\r
218 Status = gBS->OpenProtocol (\r
219 Handle,\r
220 &gEfiDriverBindingProtocolGuid,\r
221 (VOID **) &DriverBinding,\r
222 NULL,\r
223 NULL,\r
224 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
225 );\r
226 if (!EFI_ERROR (Status)) {\r
227 Status = gBS->HandleProtocol (\r
228 DriverBinding->ImageHandle,\r
229 &gEfiLoadedImageProtocolGuid,\r
230 (VOID **) &Image\r
231 );\r
232 }\r
233 }\r
234\r
235 if (!EFI_ERROR (Status)) {\r
236 PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);\r
237\r
238 if (PdbFileName != NULL) {\r
239 GetShortPdbFileName (PdbFileName, mGaugeString);\r
240 return;\r
241 }\r
242 }\r
243\r
244 //\r
245 // Method 2: Get the name string from ComponentName2 protocol\r
246 //\r
247 Status = gBS->HandleProtocol (\r
248 Handle,\r
249 &gEfiComponentName2ProtocolGuid,\r
250 (VOID **) &ComponentName2\r
251 );\r
252 if (!EFI_ERROR (Status)) {\r
253 //\r
254 // Get the current platform language setting\r
255 //\r
a71003f2 256 PlatformLanguage = GetBestLanguageForDriver(ComponentName2->SupportedLanguages, NULL, FALSE);\r
d41bc92c 257 Status = ComponentName2->GetDriverName (\r
258 ComponentName2,\r
259 PlatformLanguage != NULL ? PlatformLanguage : "en-US",\r
260 &StringPtr\r
261 );\r
262 if (!EFI_ERROR (Status)) {\r
263 SHELL_FREE_NON_NULL (PlatformLanguage);\r
264 StrnCpy (mGaugeString, StringPtr, DP_GAUGE_STRING_LENGTH);\r
265 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
266 return;\r
267 }\r
268 }\r
269\r
270 Status = gBS->HandleProtocol (\r
271 Handle,\r
272 &gEfiLoadedImageDevicePathProtocolGuid,\r
273 (VOID **) &LoadedImageDevicePath\r
274 );\r
275 if (!EFI_ERROR (Status) && (LoadedImageDevicePath != NULL)) {\r
276 DevicePath = LoadedImageDevicePath;\r
277\r
278 //\r
279 // Try to get image GUID from LoadedImageDevicePath protocol\r
280 //\r
281 NameGuid = NULL;\r
282 while (!IsDevicePathEndType (DevicePath)) {\r
283 NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath);\r
284 if (NameGuid != NULL) {\r
285 break;\r
286 }\r
287 DevicePath = NextDevicePathNode (DevicePath);\r
288 }\r
289\r
290 if (NameGuid != NULL) {\r
291 //\r
292 // Try to get the image's FFS UI section by image GUID\r
293 //\r
294 NameString = NULL;\r
295 StringSize = 0;\r
296 Status = GetSectionFromAnyFv (\r
297 NameGuid,\r
298 EFI_SECTION_USER_INTERFACE,\r
299 0,\r
300 (VOID **) &NameString,\r
301 &StringSize\r
302 );\r
303\r
304 if (!EFI_ERROR (Status)) {\r
305 //\r
306 // Method 3. Get the name string from FFS UI section\r
307 //\r
308 StrnCpy (mGaugeString, NameString, DP_GAUGE_STRING_LENGTH);\r
309 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
310 FreePool (NameString);\r
311 } else {\r
312 //\r
313 // Method 4: Get the name string from image GUID\r
314 //\r
315 UnicodeSPrint (mGaugeString, sizeof (mGaugeString), L"%g", NameGuid);\r
316 }\r
317 return;\r
318 } else {\r
319 //\r
320 // Method 5: Get the name string from image DevicePath\r
321 //\r
863986b3
RN
322 NameString = ConvertDevicePathToText (LoadedImageDevicePath, TRUE, FALSE);\r
323 if (NameString != NULL) {\r
324 StrnCpy (mGaugeString, NameString, DP_GAUGE_STRING_LENGTH);\r
325 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
326 FreePool (NameString);\r
327 return;\r
d41bc92c 328 }\r
329 }\r
330 }\r
331\r
332 //\r
333 // Method 6: Unknown Driver Name\r
334 //\r
335 StringPtr = HiiGetString (gDpHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);\r
336 ASSERT (StringPtr != NULL);\r
337 StrCpy (mGaugeString, StringPtr);\r
338 FreePool (StringPtr);\r
339}\r
340\r
341/** \r
342 Calculate the Duration in microseconds.\r
343 \r
344 Duration is multiplied by 1000, instead of Frequency being divided by 1000 or\r
345 multiplying the result by 1000, in order to maintain precision. Since Duration is\r
346 a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.\r
347 \r
348 The time is calculated as (Duration * 1000) / Timer_Frequency.\r
349 \r
350 @param[in] Duration The event duration in timer ticks.\r
351 \r
352 @return A 64-bit value which is the Elapsed time in microseconds.\r
353**/\r
354UINT64\r
355DurationInMicroSeconds (\r
356 IN UINT64 Duration\r
357 )\r
358{\r
359 UINT64 Temp;\r
360\r
361 Temp = MultU64x32 (Duration, 1000);\r
362 return DivU64x32 (Temp, TimerInfo.Frequency);\r
363}\r
364\r
365/** \r
366 Get index of Measurement Record's match in the CumData array.\r
367 \r
368 If the Measurement's Token value matches a Token in one of the CumData\r
369 records, the index of the matching record is returned. The returned\r
370 index is a signed value so that negative values can indicate that\r
371 the Measurement didn't match any entry in the CumData array.\r
372 \r
373 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.\r
374 \r
375 @retval <0 Token is not in the CumData array.\r
376 @retval >=0 Return value is the index into CumData where Token is found.\r
377**/\r
378INTN\r
379GetCumulativeItem(\r
380 IN MEASUREMENT_RECORD *Measurement\r
381 )\r
382{\r
383 INTN Index;\r
384\r
385 for( Index = 0; Index < (INTN)NumCum; ++Index) {\r
386 if (AsciiStrnCmp (Measurement->Token, CumData[Index].Name, PERF_TOKEN_LENGTH) == 0) {\r
387 return Index; // Exit, we found a match\r
388 }\r
389 }\r
390 // If the for loop exits, Token was not found.\r
391 return -1; // Indicate failure\r
392}\r