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