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