]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/DynamicCommand/DpDynamicCommand/DpUtilities.c
ShellPkg/Dp: Add null pointer check
[mirror_edk2.git] / ShellPkg / DynamicCommand / DpDynamicCommand / DpUtilities.c
CommitLineData
d41bc92c 1/** @file\r
2 Utility functions used by the Dp application.\r
3\r
115eae65 4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.\r
69af8476 5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
d41bc92c 6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13**/\r
14\r
15#include <Library/BaseLib.h>\r
16#include <Library/BaseMemoryLib.h>\r
17#include <Library/MemoryAllocationLib.h>\r
18#include <Library/DebugLib.h>\r
19#include <Library/UefiBootServicesTableLib.h>\r
d41bc92c 20#include <Library/PeCoffGetEntryPointLib.h>\r
21#include <Library/PrintLib.h>\r
22#include <Library/HiiLib.h>\r
23#include <Library/PcdLib.h>\r
24#include <Library/UefiLib.h>\r
25#include <Library/DevicePathLib.h>\r
a71003f2 26#include <Library/HandleParsingLib.h>\r
d41bc92c 27\r
28#include <Pi/PiFirmwareFile.h>\r
29#include <Library/DxeServicesLib.h>\r
30\r
31#include <Protocol/LoadedImage.h>\r
32#include <Protocol/DriverBinding.h>\r
33#include <Protocol/ComponentName2.h>\r
34#include <Protocol/DevicePath.h>\r
d41bc92c 35\r
36#include <Guid/Performance.h>\r
37\r
38#include "Dp.h"\r
39#include "Literals.h"\r
40#include "DpInternal.h"\r
41\r
42/** \r
43 Calculate an event's duration in timer ticks.\r
44 \r
45 Given the count direction and the event's start and end timer values,\r
46 calculate the duration of the event in timer ticks. Information for\r
47 the current measurement is pointed to by the parameter.\r
48 \r
49 If the measurement's start time is 1, it indicates that the developer\r
50 is indicating that the measurement began at the release of reset.\r
51 The start time is adjusted to the timer's starting count before performing\r
52 the elapsed time calculation.\r
53 \r
54 The calculated duration, in ticks, is the absolute difference between\r
55 the measurement's ending and starting counts.\r
56 \r
57 @param Measurement Pointer to a MEASUREMENT_RECORD structure containing\r
58 data for the current measurement.\r
59 \r
60 @return The 64-bit duration of the event.\r
61**/\r
62UINT64\r
63GetDuration (\r
64 IN OUT MEASUREMENT_RECORD *Measurement\r
65 )\r
66{\r
67 UINT64 Duration;\r
68 BOOLEAN Error;\r
69\r
69af8476
CS
70 if (Measurement->EndTimeStamp == 0) {\r
71 return 0;\r
72 }\r
73\r
115eae65
DB
74 Duration = Measurement->EndTimeStamp - Measurement->StartTimeStamp;\r
75 Error = (BOOLEAN)(Duration > Measurement->EndTimeStamp);\r
d41bc92c 76\r
77 if (Error) {\r
78 DEBUG ((EFI_D_ERROR, ALit_TimerLibError));\r
79 Duration = 0;\r
80 }\r
81 return Duration;\r
82}\r
83\r
84/** \r
85 Determine whether the Measurement record is for an EFI Phase.\r
86 \r
87 The Token and Module members of the measurement record are checked.\r
88 Module must be empty and Token must be one of SEC, PEI, DXE, BDS, or SHELL.\r
89 \r
90 @param[in] Measurement A pointer to the Measurement record to test.\r
91 \r
92 @retval TRUE The measurement record is for an EFI Phase.\r
93 @retval FALSE The measurement record is NOT for an EFI Phase.\r
94**/\r
95BOOLEAN\r
96IsPhase(\r
97 IN MEASUREMENT_RECORD *Measurement\r
98 )\r
99{\r
100 BOOLEAN RetVal;\r
101\r
115eae65
DB
102 RetVal = (BOOLEAN)(\r
103 ((AsciiStrCmp (Measurement->Token, ALit_SEC) == 0) ||\r
104 (AsciiStrCmp (Measurement->Token, ALit_PEI) == 0) ||\r
105 (AsciiStrCmp (Measurement->Token, ALit_DXE) == 0) ||\r
106 (AsciiStrCmp (Measurement->Token, ALit_BDS) == 0))\r
d41bc92c 107 );\r
108 return RetVal;\r
109}\r
110\r
111/** \r
112 Get the file name portion of the Pdb File Name.\r
113 \r
114 The portion of the Pdb File Name between the last backslash and\r
115 either a following period or the end of the string is converted\r
116 to Unicode and copied into UnicodeBuffer. The name is truncated,\r
117 if necessary, to ensure that UnicodeBuffer is not overrun.\r
118 \r
119 @param[in] PdbFileName Pdb file name.\r
120 @param[out] UnicodeBuffer The resultant Unicode File Name.\r
121 \r
122**/\r
123VOID\r
2c55a81a 124DpGetShortPdbFileName (\r
d41bc92c 125 IN CHAR8 *PdbFileName,\r
126 OUT CHAR16 *UnicodeBuffer\r
127 )\r
128{\r
129 UINTN IndexA; // Current work location within an ASCII string.\r
130 UINTN IndexU; // Current work location within a Unicode string.\r
131 UINTN StartIndex;\r
132 UINTN EndIndex;\r
133\r
e75390f0 134 ZeroMem (UnicodeBuffer, (DP_GAUGE_STRING_LENGTH + 1) * sizeof (CHAR16));\r
d41bc92c 135\r
136 if (PdbFileName == NULL) {\r
e75390f0 137 StrnCpyS (UnicodeBuffer, DP_GAUGE_STRING_LENGTH + 1, L" ", 1);\r
d41bc92c 138 } else {\r
139 StartIndex = 0;\r
140 for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++)\r
141 ;\r
142 for (IndexA = 0; PdbFileName[IndexA] != 0; IndexA++) {\r
9248d551 143 if ((PdbFileName[IndexA] == '\\') || (PdbFileName[IndexA] == '/')) {\r
d41bc92c 144 StartIndex = IndexA + 1;\r
145 }\r
146\r
147 if (PdbFileName[IndexA] == '.') {\r
148 EndIndex = IndexA;\r
149 }\r
150 }\r
151\r
152 IndexU = 0;\r
153 for (IndexA = StartIndex; IndexA < EndIndex; IndexA++) {\r
154 UnicodeBuffer[IndexU] = (CHAR16) PdbFileName[IndexA];\r
155 IndexU++;\r
b9ffeab7
SZ
156 if (IndexU >= DP_GAUGE_STRING_LENGTH) {\r
157 UnicodeBuffer[DP_GAUGE_STRING_LENGTH] = 0;\r
d41bc92c 158 break;\r
159 }\r
160 }\r
161 }\r
162}\r
163\r
164/** \r
165 Get a human readable name for an image handle.\r
166 The following methods will be tried orderly:\r
167 1. Image PDB\r
168 2. ComponentName2 protocol\r
169 3. FFS UI section\r
170 4. Image GUID\r
171 5. Image DevicePath\r
172 6. Unknown Driver Name\r
173\r
174 @param[in] Handle\r
175\r
176 @post The resulting Unicode name string is stored in the\r
177 mGaugeString global array.\r
178\r
179**/\r
180VOID\r
2c55a81a 181DpGetNameFromHandle (\r
d41bc92c 182 IN EFI_HANDLE Handle\r
183 )\r
184{\r
92034c4c
RN
185 EFI_STATUS Status;\r
186 EFI_LOADED_IMAGE_PROTOCOL *Image;\r
187 CHAR8 *PdbFileName;\r
188 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;\r
189 EFI_STRING StringPtr;\r
190 EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath;\r
191 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
192 EFI_GUID *NameGuid;\r
193 CHAR16 *NameString;\r
194 UINTN StringSize;\r
195 CHAR8 *PlatformLanguage;\r
196 CHAR8 *BestLanguage;\r
197 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
d41bc92c 198\r
3b672240
SZ
199 Image = NULL;\r
200 LoadedImageDevicePath = NULL;\r
201 DevicePath = NULL;\r
202\r
d41bc92c 203 //\r
204 // Method 1: Get the name string from image PDB\r
205 //\r
206 Status = gBS->HandleProtocol (\r
207 Handle,\r
208 &gEfiLoadedImageProtocolGuid,\r
209 (VOID **) &Image\r
210 );\r
211\r
212 if (EFI_ERROR (Status)) {\r
213 Status = gBS->OpenProtocol (\r
214 Handle,\r
215 &gEfiDriverBindingProtocolGuid,\r
216 (VOID **) &DriverBinding,\r
217 NULL,\r
218 NULL,\r
219 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
220 );\r
221 if (!EFI_ERROR (Status)) {\r
222 Status = gBS->HandleProtocol (\r
223 DriverBinding->ImageHandle,\r
224 &gEfiLoadedImageProtocolGuid,\r
225 (VOID **) &Image\r
226 );\r
227 }\r
228 }\r
229\r
230 if (!EFI_ERROR (Status)) {\r
231 PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);\r
232\r
233 if (PdbFileName != NULL) {\r
2c55a81a 234 DpGetShortPdbFileName (PdbFileName, mGaugeString);\r
d41bc92c 235 return;\r
236 }\r
237 }\r
238\r
239 //\r
240 // Method 2: Get the name string from ComponentName2 protocol\r
241 //\r
242 Status = gBS->HandleProtocol (\r
243 Handle,\r
244 &gEfiComponentName2ProtocolGuid,\r
245 (VOID **) &ComponentName2\r
246 );\r
247 if (!EFI_ERROR (Status)) {\r
248 //\r
92034c4c 249 // Firstly use platform language setting, secondly use driver's first supported language.\r
d41bc92c 250 //\r
92034c4c
RN
251 GetVariable2 (L"PlatformLang", &gEfiGlobalVariableGuid, (VOID**)&PlatformLanguage, NULL);\r
252 BestLanguage = GetBestLanguage(\r
253 ComponentName2->SupportedLanguages,\r
254 FALSE,\r
255 (PlatformLanguage != NULL) ? PlatformLanguage : "",\r
256 ComponentName2->SupportedLanguages,\r
257 NULL\r
258 );\r
259 SHELL_FREE_NON_NULL (PlatformLanguage);\r
260\r
d41bc92c 261 Status = ComponentName2->GetDriverName (\r
262 ComponentName2,\r
92034c4c 263 BestLanguage != NULL ? BestLanguage : "en-US",\r
d41bc92c 264 &StringPtr\r
265 );\r
266 if (!EFI_ERROR (Status)) {\r
92034c4c 267 SHELL_FREE_NON_NULL (BestLanguage);\r
4dc0d578 268 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);\r
d41bc92c 269 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
270 return;\r
271 }\r
272 }\r
273\r
274 Status = gBS->HandleProtocol (\r
275 Handle,\r
276 &gEfiLoadedImageDevicePathProtocolGuid,\r
277 (VOID **) &LoadedImageDevicePath\r
278 );\r
279 if (!EFI_ERROR (Status) && (LoadedImageDevicePath != NULL)) {\r
280 DevicePath = LoadedImageDevicePath;\r
3b672240
SZ
281 } else if (Image != NULL) {\r
282 DevicePath = Image->FilePath;\r
283 }\r
d41bc92c 284\r
3b672240 285 if (DevicePath != NULL) {\r
d41bc92c 286 //\r
3b672240 287 // Try to get image GUID from image DevicePath\r
d41bc92c 288 //\r
289 NameGuid = NULL;\r
290 while (!IsDevicePathEndType (DevicePath)) {\r
291 NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath);\r
292 if (NameGuid != NULL) {\r
293 break;\r
294 }\r
295 DevicePath = NextDevicePathNode (DevicePath);\r
296 }\r
297\r
298 if (NameGuid != NULL) {\r
299 //\r
300 // Try to get the image's FFS UI section by image GUID\r
301 //\r
302 NameString = NULL;\r
303 StringSize = 0;\r
304 Status = GetSectionFromAnyFv (\r
305 NameGuid,\r
306 EFI_SECTION_USER_INTERFACE,\r
307 0,\r
308 (VOID **) &NameString,\r
309 &StringSize\r
310 );\r
311\r
312 if (!EFI_ERROR (Status)) {\r
313 //\r
314 // Method 3. Get the name string from FFS UI section\r
315 //\r
4dc0d578 316 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);\r
d41bc92c 317 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
318 FreePool (NameString);\r
319 } else {\r
320 //\r
321 // Method 4: Get the name string from image GUID\r
322 //\r
323 UnicodeSPrint (mGaugeString, sizeof (mGaugeString), L"%g", NameGuid);\r
324 }\r
325 return;\r
326 } else {\r
327 //\r
328 // Method 5: Get the name string from image DevicePath\r
329 //\r
3b672240 330 NameString = ConvertDevicePathToText (DevicePath, TRUE, FALSE);\r
863986b3 331 if (NameString != NULL) {\r
4dc0d578 332 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);\r
863986b3
RN
333 mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
334 FreePool (NameString);\r
335 return;\r
d41bc92c 336 }\r
337 }\r
338 }\r
339\r
340 //\r
341 // Method 6: Unknown Driver Name\r
342 //\r
92034c4c 343 StringPtr = HiiGetString (mDpHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);\r
d41bc92c 344 ASSERT (StringPtr != NULL);\r
4dc0d578 345 StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);\r
d41bc92c 346 FreePool (StringPtr);\r
347}\r
348\r
349/** \r
350 Calculate the Duration in microseconds.\r
351 \r
352 Duration is multiplied by 1000, instead of Frequency being divided by 1000 or\r
353 multiplying the result by 1000, in order to maintain precision. Since Duration is\r
354 a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.\r
355 \r
356 The time is calculated as (Duration * 1000) / Timer_Frequency.\r
357 \r
358 @param[in] Duration The event duration in timer ticks.\r
359 \r
360 @return A 64-bit value which is the Elapsed time in microseconds.\r
361**/\r
362UINT64\r
363DurationInMicroSeconds (\r
364 IN UINT64 Duration\r
365 )\r
366{\r
115eae65 367 return DivU64x32 (Duration, 1000);\r
d41bc92c 368}\r
369\r
370/** \r
371 Get index of Measurement Record's match in the CumData array.\r
372 \r
373 If the Measurement's Token value matches a Token in one of the CumData\r
374 records, the index of the matching record is returned. The returned\r
375 index is a signed value so that negative values can indicate that\r
376 the Measurement didn't match any entry in the CumData array.\r
377 \r
378 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.\r
379 \r
380 @retval <0 Token is not in the CumData array.\r
381 @retval >=0 Return value is the index into CumData where Token is found.\r
382**/\r
383INTN\r
384GetCumulativeItem(\r
385 IN MEASUREMENT_RECORD *Measurement\r
386 )\r
387{\r
388 INTN Index;\r
389\r
390 for( Index = 0; Index < (INTN)NumCum; ++Index) {\r
115eae65 391 if (AsciiStrCmp (Measurement->Token, CumData[Index].Name) == 0) {\r
d41bc92c 392 return Index; // Exit, we found a match\r
393 }\r
394 }\r
395 // If the for loop exits, Token was not found.\r
396 return -1; // Indicate failure\r
397}\r