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