2 Utility functions used by the Dp application.
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
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>
22 #include <Pi/PiFirmwareFile.h>
23 #include <Library/DxeServicesLib.h>
25 #include <Protocol/LoadedImage.h>
26 #include <Protocol/DriverBinding.h>
27 #include <Protocol/ComponentName2.h>
28 #include <Protocol/DevicePath.h>
30 #include <Guid/Performance.h>
34 #include "DpInternal.h"
37 Calculate an event's duration in timer ticks.
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.
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.
48 The calculated duration, in ticks, is the absolute difference between
49 the measurement's ending and starting counts.
51 @param Measurement Pointer to a MEASUREMENT_RECORD structure containing
52 data for the current measurement.
54 @return The 64-bit duration of the event.
58 IN OUT MEASUREMENT_RECORD
*Measurement
64 if (Measurement
->EndTimeStamp
== 0) {
68 Duration
= Measurement
->EndTimeStamp
- Measurement
->StartTimeStamp
;
69 Error
= (BOOLEAN
)(Duration
> Measurement
->EndTimeStamp
);
72 DEBUG ((EFI_D_ERROR
, ALit_TimerLibError
));
79 Determine whether the Measurement record is for an EFI Phase.
81 The Token and Module members of the measurement record are checked.
82 Module must be empty and Token must be one of SEC, PEI, DXE, BDS, or SHELL.
84 @param[in] Measurement A pointer to the Measurement record to test.
86 @retval TRUE The measurement record is for an EFI Phase.
87 @retval FALSE The measurement record is NOT for an EFI Phase.
91 IN MEASUREMENT_RECORD
*Measurement
97 ((AsciiStrCmp (Measurement
->Token
, ALit_SEC
) == 0) ||
98 (AsciiStrCmp (Measurement
->Token
, ALit_PEI
) == 0) ||
99 (AsciiStrCmp (Measurement
->Token
, ALit_DXE
) == 0) ||
100 (AsciiStrCmp (Measurement
->Token
, ALit_BDS
) == 0))
106 Determine whether the Measurement record is for core code.
108 @param[in] Measurement A pointer to the Measurement record to test.
110 @retval TRUE The measurement record is used for core.
111 @retval FALSE The measurement record is NOT used for core.
116 IN MEASUREMENT_RECORD
*Measurement
122 ((Measurement
->Identifier
== MODULE_START_ID
) ||
123 (Measurement
->Identifier
== MODULE_END_ID
) ||
124 (Measurement
->Identifier
== MODULE_LOADIMAGE_START_ID
) ||
125 (Measurement
->Identifier
== MODULE_LOADIMAGE_END_ID
) ||
126 (Measurement
->Identifier
== MODULE_DB_START_ID
) ||
127 (Measurement
->Identifier
== MODULE_DB_END_ID
) ||
128 (Measurement
->Identifier
== MODULE_DB_SUPPORT_START_ID
) ||
129 (Measurement
->Identifier
== MODULE_DB_SUPPORT_END_ID
) ||
130 (Measurement
->Identifier
== MODULE_DB_STOP_START_ID
) ||
131 (Measurement
->Identifier
== MODULE_DB_STOP_START_ID
))
137 Get the file name portion of the Pdb File Name.
139 The portion of the Pdb File Name between the last backslash and
140 either a following period or the end of the string is converted
141 to Unicode and copied into UnicodeBuffer. The name is truncated,
142 if necessary, to ensure that UnicodeBuffer is not overrun.
144 @param[in] PdbFileName Pdb file name.
145 @param[out] UnicodeBuffer The resultant Unicode File Name.
149 DpGetShortPdbFileName (
150 IN CHAR8
*PdbFileName
,
151 OUT CHAR16
*UnicodeBuffer
154 UINTN IndexA
; // Current work location within an ASCII string.
155 UINTN IndexU
; // Current work location within a Unicode string.
159 ZeroMem (UnicodeBuffer
, (DP_GAUGE_STRING_LENGTH
+ 1) * sizeof (CHAR16
));
161 if (PdbFileName
== NULL
) {
162 StrnCpyS (UnicodeBuffer
, DP_GAUGE_STRING_LENGTH
+ 1, L
" ", 1);
165 for (EndIndex
= 0; PdbFileName
[EndIndex
] != 0; EndIndex
++)
167 for (IndexA
= 0; PdbFileName
[IndexA
] != 0; IndexA
++) {
168 if ((PdbFileName
[IndexA
] == '\\') || (PdbFileName
[IndexA
] == '/')) {
169 StartIndex
= IndexA
+ 1;
172 if (PdbFileName
[IndexA
] == '.') {
178 for (IndexA
= StartIndex
; IndexA
< EndIndex
; IndexA
++) {
179 UnicodeBuffer
[IndexU
] = (CHAR16
) PdbFileName
[IndexA
];
181 if (IndexU
>= DP_GAUGE_STRING_LENGTH
) {
182 UnicodeBuffer
[DP_GAUGE_STRING_LENGTH
] = 0;
190 Get a human readable name for an image handle.
191 The following methods will be tried orderly:
193 2. ComponentName2 protocol
197 6. Unknown Driver Name
201 @post The resulting Unicode name string is stored in the
202 mGaugeString global array.
206 DpGetNameFromHandle (
211 EFI_LOADED_IMAGE_PROTOCOL
*Image
;
213 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
214 EFI_STRING StringPtr
;
215 EFI_DEVICE_PATH_PROTOCOL
*LoadedImageDevicePath
;
216 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
220 CHAR8
*PlatformLanguage
;
222 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
225 LoadedImageDevicePath
= NULL
;
229 // Method 1: Get the name string from image PDB
231 Status
= gBS
->HandleProtocol (
233 &gEfiLoadedImageProtocolGuid
,
237 if (EFI_ERROR (Status
)) {
238 Status
= gBS
->OpenProtocol (
240 &gEfiDriverBindingProtocolGuid
,
241 (VOID
**) &DriverBinding
,
244 EFI_OPEN_PROTOCOL_GET_PROTOCOL
246 if (!EFI_ERROR (Status
)) {
247 Status
= gBS
->HandleProtocol (
248 DriverBinding
->ImageHandle
,
249 &gEfiLoadedImageProtocolGuid
,
255 if (!EFI_ERROR (Status
)) {
256 PdbFileName
= PeCoffLoaderGetPdbPointer (Image
->ImageBase
);
258 if (PdbFileName
!= NULL
) {
259 DpGetShortPdbFileName (PdbFileName
, mGaugeString
);
265 // Method 2: Get the name string from ComponentName2 protocol
267 Status
= gBS
->HandleProtocol (
269 &gEfiComponentName2ProtocolGuid
,
270 (VOID
**) &ComponentName2
272 if (!EFI_ERROR (Status
)) {
274 // Firstly use platform language setting, secondly use driver's first supported language.
276 GetVariable2 (L
"PlatformLang", &gEfiGlobalVariableGuid
, (VOID
**)&PlatformLanguage
, NULL
);
277 BestLanguage
= GetBestLanguage(
278 ComponentName2
->SupportedLanguages
,
280 (PlatformLanguage
!= NULL
) ? PlatformLanguage
: "",
281 ComponentName2
->SupportedLanguages
,
284 SHELL_FREE_NON_NULL (PlatformLanguage
);
286 Status
= ComponentName2
->GetDriverName (
288 BestLanguage
!= NULL
? BestLanguage
: "en-US",
291 if (!EFI_ERROR (Status
)) {
292 SHELL_FREE_NON_NULL (BestLanguage
);
293 StrnCpyS (mGaugeString
, DP_GAUGE_STRING_LENGTH
+ 1, StringPtr
, DP_GAUGE_STRING_LENGTH
);
294 mGaugeString
[DP_GAUGE_STRING_LENGTH
] = 0;
299 Status
= gBS
->HandleProtocol (
301 &gEfiLoadedImageDevicePathProtocolGuid
,
302 (VOID
**) &LoadedImageDevicePath
304 if (!EFI_ERROR (Status
) && (LoadedImageDevicePath
!= NULL
)) {
305 DevicePath
= LoadedImageDevicePath
;
306 } else if (Image
!= NULL
) {
307 DevicePath
= Image
->FilePath
;
310 if (DevicePath
!= NULL
) {
312 // Try to get image GUID from image DevicePath
315 while (!IsDevicePathEndType (DevicePath
)) {
316 NameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) DevicePath
);
317 if (NameGuid
!= NULL
) {
320 DevicePath
= NextDevicePathNode (DevicePath
);
323 if (NameGuid
!= NULL
) {
325 // Try to get the image's FFS UI section by image GUID
329 Status
= GetSectionFromAnyFv (
331 EFI_SECTION_USER_INTERFACE
,
333 (VOID
**) &NameString
,
337 if (!EFI_ERROR (Status
)) {
339 // Method 3. Get the name string from FFS UI section
341 StrnCpyS (mGaugeString
, DP_GAUGE_STRING_LENGTH
+ 1, NameString
, DP_GAUGE_STRING_LENGTH
);
342 mGaugeString
[DP_GAUGE_STRING_LENGTH
] = 0;
343 FreePool (NameString
);
346 // Method 4: Get the name string from image GUID
348 UnicodeSPrint (mGaugeString
, sizeof (mGaugeString
), L
"%g", NameGuid
);
353 // Method 5: Get the name string from image DevicePath
355 NameString
= ConvertDevicePathToText (DevicePath
, TRUE
, FALSE
);
356 if (NameString
!= NULL
) {
357 StrnCpyS (mGaugeString
, DP_GAUGE_STRING_LENGTH
+ 1, NameString
, DP_GAUGE_STRING_LENGTH
);
358 mGaugeString
[DP_GAUGE_STRING_LENGTH
] = 0;
359 FreePool (NameString
);
366 // Method 6: Unknown Driver Name
368 StringPtr
= HiiGetString (mDpHiiHandle
, STRING_TOKEN (STR_DP_ERROR_NAME
), NULL
);
369 ASSERT (StringPtr
!= NULL
);
370 StrnCpyS (mGaugeString
, DP_GAUGE_STRING_LENGTH
+ 1, StringPtr
, DP_GAUGE_STRING_LENGTH
);
371 FreePool (StringPtr
);
375 Calculate the Duration in microseconds.
377 Duration is multiplied by 1000, instead of Frequency being divided by 1000 or
378 multiplying the result by 1000, in order to maintain precision. Since Duration is
379 a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.
381 The time is calculated as (Duration * 1000) / Timer_Frequency.
383 @param[in] Duration The event duration in timer ticks.
385 @return A 64-bit value which is the Elapsed time in microseconds.
388 DurationInMicroSeconds (
392 return DivU64x32 (Duration
, 1000);
396 Get index of Measurement Record's match in the CumData array.
398 If the Measurement's Token value matches a Token in one of the CumData
399 records, the index of the matching record is returned. The returned
400 index is a signed value so that negative values can indicate that
401 the Measurement didn't match any entry in the CumData array.
403 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.
405 @retval <0 Token is not in the CumData array.
406 @retval >=0 Return value is the index into CumData where Token is found.
410 IN MEASUREMENT_RECORD
*Measurement
415 for( Index
= 0; Index
< (INTN
)NumCum
; ++Index
) {
416 if (AsciiStrCmp (Measurement
->Token
, CumData
[Index
].Name
) == 0) {
417 return Index
; // Exit, we found a match
420 // If the for loop exits, Token was not found.
421 return -1; // Indicate failure