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