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