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