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 ((DEBUG_ERROR
, ALit_TimerLibError
));
80 Determine whether the Measurement record is for an EFI Phase.
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.
85 @param[in] Measurement A pointer to the Measurement record to test.
87 @retval TRUE The measurement record is for an EFI Phase.
88 @retval FALSE The measurement record is NOT for an EFI Phase.
92 IN MEASUREMENT_RECORD
*Measurement
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))
107 Determine whether the Measurement record is for core code.
109 @param[in] Measurement A pointer to the Measurement record to test.
111 @retval TRUE The measurement record is used for core.
112 @retval FALSE The measurement record is NOT used for core.
117 IN MEASUREMENT_RECORD
*Measurement
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
))
138 Get the file name portion of the Pdb File Name.
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.
145 @param[in] PdbFileName Pdb file name.
146 @param[out] UnicodeBuffer The resultant Unicode File Name.
150 DpGetShortPdbFileName (
151 IN CHAR8
*PdbFileName
,
152 OUT CHAR16
*UnicodeBuffer
155 UINTN IndexA
; // Current work location within an ASCII string.
156 UINTN IndexU
; // Current work location within a Unicode string.
160 ZeroMem (UnicodeBuffer
, (DP_GAUGE_STRING_LENGTH
+ 1) * sizeof (CHAR16
));
162 if (PdbFileName
== NULL
) {
163 StrnCpyS (UnicodeBuffer
, DP_GAUGE_STRING_LENGTH
+ 1, L
" ", 1);
166 for (EndIndex
= 0; PdbFileName
[EndIndex
] != 0; EndIndex
++) {
169 for (IndexA
= 0; PdbFileName
[IndexA
] != 0; IndexA
++) {
170 if ((PdbFileName
[IndexA
] == '\\') || (PdbFileName
[IndexA
] == '/')) {
171 StartIndex
= IndexA
+ 1;
174 if (PdbFileName
[IndexA
] == '.') {
180 for (IndexA
= StartIndex
; IndexA
< EndIndex
; IndexA
++) {
181 UnicodeBuffer
[IndexU
] = (CHAR16
)PdbFileName
[IndexA
];
183 if (IndexU
>= DP_GAUGE_STRING_LENGTH
) {
184 UnicodeBuffer
[DP_GAUGE_STRING_LENGTH
] = 0;
192 Get a human readable name for an image handle.
193 The following methods will be tried orderly:
195 2. ComponentName2 protocol
199 6. Unknown Driver Name
203 @post The resulting Unicode name string is stored in the
204 mGaugeString global array.
208 DpGetNameFromHandle (
213 EFI_LOADED_IMAGE_PROTOCOL
*Image
;
215 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
216 EFI_STRING StringPtr
;
217 EFI_DEVICE_PATH_PROTOCOL
*LoadedImageDevicePath
;
218 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
222 CHAR8
*PlatformLanguage
;
224 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
227 LoadedImageDevicePath
= NULL
;
231 // Method 1: Get the name string from image PDB
233 Status
= gBS
->HandleProtocol (
235 &gEfiLoadedImageProtocolGuid
,
239 if (EFI_ERROR (Status
)) {
240 Status
= gBS
->OpenProtocol (
242 &gEfiDriverBindingProtocolGuid
,
243 (VOID
**)&DriverBinding
,
246 EFI_OPEN_PROTOCOL_GET_PROTOCOL
248 if (!EFI_ERROR (Status
)) {
249 Status
= gBS
->HandleProtocol (
250 DriverBinding
->ImageHandle
,
251 &gEfiLoadedImageProtocolGuid
,
257 if (!EFI_ERROR (Status
)) {
258 PdbFileName
= PeCoffLoaderGetPdbPointer (Image
->ImageBase
);
260 if (PdbFileName
!= NULL
) {
261 DpGetShortPdbFileName (PdbFileName
, mGaugeString
);
267 // Method 2: Get the name string from ComponentName2 protocol
269 Status
= gBS
->HandleProtocol (
271 &gEfiComponentName2ProtocolGuid
,
272 (VOID
**)&ComponentName2
274 if (!EFI_ERROR (Status
)) {
276 // Firstly use platform language setting, secondly use driver's first supported language.
278 GetVariable2 (L
"PlatformLang", &gEfiGlobalVariableGuid
, (VOID
**)&PlatformLanguage
, NULL
);
279 BestLanguage
= GetBestLanguage (
280 ComponentName2
->SupportedLanguages
,
282 (PlatformLanguage
!= NULL
) ? PlatformLanguage
: "",
283 ComponentName2
->SupportedLanguages
,
286 SHELL_FREE_NON_NULL (PlatformLanguage
);
288 Status
= ComponentName2
->GetDriverName (
290 BestLanguage
!= NULL
? BestLanguage
: "en-US",
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;
301 Status
= gBS
->HandleProtocol (
303 &gEfiLoadedImageDevicePathProtocolGuid
,
304 (VOID
**)&LoadedImageDevicePath
306 if (!EFI_ERROR (Status
) && (LoadedImageDevicePath
!= NULL
)) {
307 DevicePath
= LoadedImageDevicePath
;
308 } else if (Image
!= NULL
) {
309 DevicePath
= Image
->FilePath
;
312 if (DevicePath
!= NULL
) {
314 // Try to get image GUID from image DevicePath
317 while (!IsDevicePathEndType (DevicePath
)) {
318 NameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*)DevicePath
);
319 if (NameGuid
!= NULL
) {
323 DevicePath
= NextDevicePathNode (DevicePath
);
326 if (NameGuid
!= NULL
) {
328 // Try to get the image's FFS UI section by image GUID
332 Status
= GetSectionFromAnyFv (
334 EFI_SECTION_USER_INTERFACE
,
336 (VOID
**)&NameString
,
340 if (!EFI_ERROR (Status
)) {
342 // Method 3. Get the name string from FFS UI section
344 StrnCpyS (mGaugeString
, DP_GAUGE_STRING_LENGTH
+ 1, NameString
, DP_GAUGE_STRING_LENGTH
);
345 mGaugeString
[DP_GAUGE_STRING_LENGTH
] = 0;
346 FreePool (NameString
);
349 // Method 4: Get the name string from image GUID
351 UnicodeSPrint (mGaugeString
, sizeof (mGaugeString
), L
"%g", NameGuid
);
357 // Method 5: Get the name string from image DevicePath
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
);
370 // Method 6: Unknown Driver Name
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
);
379 Calculate the Duration in microseconds.
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.
385 The time is calculated as (Duration * 1000) / Timer_Frequency.
387 @param[in] Duration The event duration in timer ticks.
389 @return A 64-bit value which is the Elapsed time in microseconds.
392 DurationInMicroSeconds (
396 return DivU64x32 (Duration
, 1000);
400 Get index of Measurement Record's match in the CumData array.
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.
407 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.
409 @retval <0 Token is not in the CumData array.
410 @retval >=0 Return value is the index into CumData where Token is found.
414 IN MEASUREMENT_RECORD
*Measurement
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
425 // If the for loop exits, Token was not found.
426 return -1; // Indicate failure