2 Utility functions used by the Dp application.
4 Copyright (c) 2009 - 2013, 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
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.
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>
27 #include <Pi/PiFirmwareFile.h>
28 #include <Library/DxeServicesLib.h>
30 #include <Protocol/LoadedImage.h>
31 #include <Protocol/DriverBinding.h>
32 #include <Protocol/ComponentName2.h>
33 #include <Protocol/DevicePath.h>
34 #include <Protocol/DevicePathToText.h>
36 #include <Guid/Performance.h>
40 #include "DpInternal.h"
43 Calculate an event's duration in timer ticks.
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.
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.
54 The calculated duration, in ticks, is the absolute difference between
55 the measurement's ending and starting counts.
57 @param Measurement Pointer to a MEASUREMENT_RECORD structure containing
58 data for the current measurement.
60 @return The 64-bit duration of the event.
64 IN OUT MEASUREMENT_RECORD
*Measurement
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
;
78 if (TimerInfo
.CountUp
) {
79 Duration
= Measurement
->EndTimeStamp
- Measurement
->StartTimeStamp
;
80 Error
= (BOOLEAN
)(Duration
> Measurement
->EndTimeStamp
);
83 Duration
= Measurement
->StartTimeStamp
- Measurement
->EndTimeStamp
;
84 Error
= (BOOLEAN
)(Duration
> Measurement
->StartTimeStamp
);
88 DEBUG ((EFI_D_ERROR
, ALit_TimerLibError
));
95 Determine whether the Measurement record is for an EFI Phase.
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.
100 @param[in] Measurement A pointer to the Measurement record to test.
102 @retval TRUE The measurement record is for an EFI Phase.
103 @retval FALSE The measurement record is NOT for an EFI Phase.
107 IN MEASUREMENT_RECORD
*Measurement
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))
122 Get the file name portion of the Pdb File Name.
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.
129 @param[in] PdbFileName Pdb file name.
130 @param[out] UnicodeBuffer The resultant Unicode File Name.
134 GetShortPdbFileName (
135 IN CHAR8
*PdbFileName
,
136 OUT CHAR16
*UnicodeBuffer
139 UINTN IndexA
; // Current work location within an ASCII string.
140 UINTN IndexU
; // Current work location within a Unicode string.
144 ZeroMem (UnicodeBuffer
, DXE_PERFORMANCE_STRING_LENGTH
* sizeof (CHAR16
));
146 if (PdbFileName
== NULL
) {
147 StrCpy (UnicodeBuffer
, L
" ");
150 for (EndIndex
= 0; PdbFileName
[EndIndex
] != 0; EndIndex
++)
152 for (IndexA
= 0; PdbFileName
[IndexA
] != 0; IndexA
++) {
153 if (PdbFileName
[IndexA
] == '\\') {
154 StartIndex
= IndexA
+ 1;
157 if (PdbFileName
[IndexA
] == '.') {
163 for (IndexA
= StartIndex
; IndexA
< EndIndex
; IndexA
++) {
164 UnicodeBuffer
[IndexU
] = (CHAR16
) PdbFileName
[IndexA
];
166 if (IndexU
>= DXE_PERFORMANCE_STRING_LENGTH
) {
167 UnicodeBuffer
[DXE_PERFORMANCE_STRING_LENGTH
] = 0;
175 Get a human readable name for an image handle.
176 The following methods will be tried orderly:
178 2. ComponentName2 protocol
182 6. Unknown Driver Name
186 @post The resulting Unicode name string is stored in the
187 mGaugeString global array.
196 EFI_LOADED_IMAGE_PROTOCOL
*Image
;
198 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
199 EFI_STRING StringPtr
;
200 EFI_DEVICE_PATH_PROTOCOL
*LoadedImageDevicePath
;
201 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
205 CHAR8
*PlatformLanguage
;
206 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
207 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*DevicePathToText
;
210 // Method 1: Get the name string from image PDB
212 Status
= gBS
->HandleProtocol (
214 &gEfiLoadedImageProtocolGuid
,
218 if (EFI_ERROR (Status
)) {
219 Status
= gBS
->OpenProtocol (
221 &gEfiDriverBindingProtocolGuid
,
222 (VOID
**) &DriverBinding
,
225 EFI_OPEN_PROTOCOL_GET_PROTOCOL
227 if (!EFI_ERROR (Status
)) {
228 Status
= gBS
->HandleProtocol (
229 DriverBinding
->ImageHandle
,
230 &gEfiLoadedImageProtocolGuid
,
236 if (!EFI_ERROR (Status
)) {
237 PdbFileName
= PeCoffLoaderGetPdbPointer (Image
->ImageBase
);
239 if (PdbFileName
!= NULL
) {
240 GetShortPdbFileName (PdbFileName
, mGaugeString
);
246 // Method 2: Get the name string from ComponentName2 protocol
248 Status
= gBS
->HandleProtocol (
250 &gEfiComponentName2ProtocolGuid
,
251 (VOID
**) &ComponentName2
253 if (!EFI_ERROR (Status
)) {
255 // Get the current platform language setting
257 GetEfiGlobalVariable2 (L
"PlatformLang", (VOID
**)&PlatformLanguage
, NULL
);
258 Status
= ComponentName2
->GetDriverName (
260 PlatformLanguage
!= NULL
? PlatformLanguage
: "en-US",
263 if (!EFI_ERROR (Status
)) {
264 SHELL_FREE_NON_NULL (PlatformLanguage
);
265 StrnCpy (mGaugeString
, StringPtr
, DP_GAUGE_STRING_LENGTH
);
266 mGaugeString
[DP_GAUGE_STRING_LENGTH
] = 0;
271 Status
= gBS
->HandleProtocol (
273 &gEfiLoadedImageDevicePathProtocolGuid
,
274 (VOID
**) &LoadedImageDevicePath
276 if (!EFI_ERROR (Status
) && (LoadedImageDevicePath
!= NULL
)) {
277 DevicePath
= LoadedImageDevicePath
;
280 // Try to get image GUID from LoadedImageDevicePath protocol
283 while (!IsDevicePathEndType (DevicePath
)) {
284 NameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) DevicePath
);
285 if (NameGuid
!= NULL
) {
288 DevicePath
= NextDevicePathNode (DevicePath
);
291 if (NameGuid
!= NULL
) {
293 // Try to get the image's FFS UI section by image GUID
297 Status
= GetSectionFromAnyFv (
299 EFI_SECTION_USER_INTERFACE
,
301 (VOID
**) &NameString
,
305 if (!EFI_ERROR (Status
)) {
307 // Method 3. Get the name string from FFS UI section
309 StrnCpy (mGaugeString
, NameString
, DP_GAUGE_STRING_LENGTH
);
310 mGaugeString
[DP_GAUGE_STRING_LENGTH
] = 0;
311 FreePool (NameString
);
314 // Method 4: Get the name string from image GUID
316 UnicodeSPrint (mGaugeString
, sizeof (mGaugeString
), L
"%g", NameGuid
);
321 // Method 5: Get the name string from image DevicePath
323 Status
= gBS
->LocateProtocol (
324 &gEfiDevicePathToTextProtocolGuid
,
326 (VOID
**) &DevicePathToText
328 if (!EFI_ERROR (Status
)) {
329 NameString
= DevicePathToText
->ConvertDevicePathToText (LoadedImageDevicePath
, TRUE
, FALSE
);
330 if (NameString
!= NULL
) {
331 StrnCpy (mGaugeString
, NameString
, DP_GAUGE_STRING_LENGTH
);
332 mGaugeString
[DP_GAUGE_STRING_LENGTH
] = 0;
333 FreePool (NameString
);
341 // Method 6: Unknown Driver Name
343 StringPtr
= HiiGetString (gDpHiiHandle
, STRING_TOKEN (STR_DP_ERROR_NAME
), NULL
);
344 ASSERT (StringPtr
!= NULL
);
345 StrCpy (mGaugeString
, StringPtr
);
346 FreePool (StringPtr
);
350 Calculate the Duration in microseconds.
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.
356 The time is calculated as (Duration * 1000) / Timer_Frequency.
358 @param[in] Duration The event duration in timer ticks.
360 @return A 64-bit value which is the Elapsed time in microseconds.
363 DurationInMicroSeconds (
369 Temp
= MultU64x32 (Duration
, 1000);
370 return DivU64x32 (Temp
, TimerInfo
.Frequency
);
374 Get index of Measurement Record's match in the CumData array.
376 If the Measurement's Token value matches a Token in one of the CumData
377 records, the index of the matching record is returned. The returned
378 index is a signed value so that negative values can indicate that
379 the Measurement didn't match any entry in the CumData array.
381 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.
383 @retval <0 Token is not in the CumData array.
384 @retval >=0 Return value is the index into CumData where Token is found.
388 IN MEASUREMENT_RECORD
*Measurement
393 for( Index
= 0; Index
< (INTN
)NumCum
; ++Index
) {
394 if (AsciiStrnCmp (Measurement
->Token
, CumData
[Index
].Name
, PERF_TOKEN_LENGTH
) == 0) {
395 return Index
; // Exit, we found a match
398 // If the for loop exits, Token was not found.
399 return -1; // Indicate failure