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