TianoCore EDK2 master
Loading...
Searching...
No Matches
Drivers.c
Go to the documentation of this file.
1
11
12#define MAX_LEN_DRIVER_NAME 35
13
14STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
15 { L"-sfo", TypeFlag },
16 { L"-l", TypeValue },
17 { NULL, TypeMax }
18};
19
29CHAR16 *
31 IN EFI_HANDLE TheHandle
32 )
33{
34 EFI_STATUS Status;
35 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
36 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;
37 EFI_DEVICE_PATH_PROTOCOL *FinalPath;
38 CHAR16 *RetVal;
39
40 FinalPath = NULL;
41
42 Status = gBS->OpenProtocol (
43 TheHandle,
44 &gEfiLoadedImageProtocolGuid,
45 (VOID **)&LoadedImage,
47 NULL,
48 EFI_OPEN_PROTOCOL_GET_PROTOCOL
49 );
50 if (!EFI_ERROR (Status)) {
51 Status = gBS->OpenProtocol (
52 LoadedImage->DeviceHandle,
53 &gEfiDevicePathProtocolGuid,
54 (VOID **)&ImageDevicePath,
56 NULL,
57 EFI_OPEN_PROTOCOL_GET_PROTOCOL
58 );
59 if (!EFI_ERROR (Status)) {
60 FinalPath = AppendDevicePath (ImageDevicePath, LoadedImage->FilePath);
61 gBS->CloseProtocol (
62 LoadedImage->DeviceHandle,
63 &gEfiDevicePathProtocolGuid,
65 NULL
66 );
67 }
68
69 gBS->CloseProtocol (
70 TheHandle,
71 &gEfiLoadedImageProtocolGuid,
73 NULL
74 );
75 }
76
77 if (FinalPath == NULL) {
78 return (NULL);
79 }
80
81 RetVal = gEfiShellProtocol->GetFilePathFromDevicePath (FinalPath);
82 if (RetVal == NULL) {
83 RetVal = ConvertDevicePathToText (FinalPath, TRUE, TRUE);
84 }
85
86 FreePool (FinalPath);
87 return (RetVal);
88}
89
98BOOLEAN
100 IN CONST EFI_HANDLE TheHandle
101 )
102{
103 EFI_STATUS Status;
104
105 Status = gBS->OpenProtocol ((EFI_HANDLE)TheHandle, &gEfiDriverConfigurationProtocolGuid, NULL, gImageHandle, NULL, EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
106 if (EFI_ERROR (Status)) {
107 return (FALSE);
108 }
109
110 return (TRUE);
111}
112
121BOOLEAN
123 IN CONST EFI_HANDLE TheHandle
124 )
125{
126 EFI_STATUS Status;
127
128 Status = gBS->OpenProtocol ((EFI_HANDLE)TheHandle, &gEfiDriverDiagnostics2ProtocolGuid, NULL, gImageHandle, NULL, EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
129 if (EFI_ERROR (Status)) {
130 Status = gBS->OpenProtocol ((EFI_HANDLE)TheHandle, &gEfiDriverDiagnosticsProtocolGuid, NULL, gImageHandle, NULL, EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
131 if (EFI_ERROR (Status)) {
132 return (FALSE);
133 }
134 }
135
136 return (TRUE);
137}
138
147UINT32
149 IN CONST EFI_HANDLE TheHandle
150 )
151{
152 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
153 EFI_STATUS Status;
154 UINT32 RetVal;
155
156 RetVal = (UINT32)-1;
157
158 Status = gBS->OpenProtocol ((EFI_HANDLE)TheHandle, &gEfiDriverBindingProtocolGuid, (VOID **)&DriverBinding, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
159 if (!EFI_ERROR (Status)) {
160 RetVal = DriverBinding->Version;
161 gBS->CloseProtocol (TheHandle, &gEfiDriverBindingProtocolGuid, gImageHandle, NULL);
162 }
163
164 return (RetVal);
165}
166
174CHAR16 *
176 IN CONST EFI_HANDLE Handle
177 )
178{
179 EFI_STATUS Status;
180 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
181 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
182 EFI_DEVICE_PATH_PROTOCOL *DevPathNode;
183 EFI_GUID *NameGuid;
184 CHAR16 *ImageName;
185 UINTN BufferSize;
186 UINT32 AuthenticationStatus;
188
189 LoadedImage = NULL;
190 DriverBinding = NULL;
191 ImageName = NULL;
192
193 Status = gBS->OpenProtocol (
194 Handle,
195 &gEfiDriverBindingProtocolGuid,
196 (VOID **)&DriverBinding,
197 NULL,
198 NULL,
199 EFI_OPEN_PROTOCOL_GET_PROTOCOL
200 );
201 if (EFI_ERROR (Status)) {
202 return NULL;
203 }
204
205 Status = gBS->OpenProtocol (
206 DriverBinding->ImageHandle,
207 &gEfiLoadedImageProtocolGuid,
208 (VOID **)&LoadedImage,
210 NULL,
211 EFI_OPEN_PROTOCOL_GET_PROTOCOL
212 );
213 if (!EFI_ERROR (Status)) {
214 DevPathNode = LoadedImage->FilePath;
215 if (DevPathNode == NULL) {
216 return NULL;
217 }
218
219 while (!IsDevicePathEnd (DevPathNode)) {
221 if (NameGuid != NULL) {
222 Status = gBS->HandleProtocol (
223 LoadedImage->DeviceHandle,
224 &gEfiFirmwareVolume2ProtocolGuid,
225 (VOID **)&Fv2
226 );
227 if (!EFI_ERROR (Status)) {
228 Status = Fv2->ReadSection (
229 Fv2,
230 NameGuid,
231 EFI_SECTION_USER_INTERFACE,
232 0,
233 (VOID **)&ImageName,
234 &BufferSize,
235 &AuthenticationStatus
236 );
237 if (!EFI_ERROR (Status)) {
238 break;
239 }
240
241 ImageName = NULL;
242 }
243 }
244
245 //
246 // Next device path node
247 //
248 DevPathNode = NextDevicePathNode (DevPathNode);
249 }
250
251 if (ImageName == NULL) {
252 ImageName = ConvertDevicePathToText (LoadedImage->FilePath, TRUE, TRUE);
253 }
254 }
255
256 return ImageName;
257}
258
266EFIAPI
268 IN EFI_HANDLE ImageHandle,
269 IN EFI_SYSTEM_TABLE *SystemTable
270 )
271{
272 EFI_STATUS Status;
273 LIST_ENTRY *Package;
274 CHAR16 *ProblemParam;
275 SHELL_STATUS ShellStatus;
276 CHAR8 *Language;
277 CONST CHAR16 *Lang;
278 EFI_HANDLE *HandleList;
279 EFI_HANDLE *HandleWalker;
280 UINTN ChildCount;
281 UINTN DeviceCount;
282 CHAR16 ChildCountStr[21];
283 CHAR16 DeviceCountStr[21];
284 CHAR16 *Temp2;
285 CONST CHAR16 *FullDriverName;
286 CHAR16 *TruncatedDriverName;
287 CHAR16 *ImageName;
288 CHAR16 *FormatString;
289 UINT32 DriverVersion;
290 BOOLEAN DriverConfig;
291 BOOLEAN DriverDiag;
292 BOOLEAN SfoFlag;
293
294 ShellStatus = SHELL_SUCCESS;
295 Status = EFI_SUCCESS;
296 Language = NULL;
297 FormatString = NULL;
298 SfoFlag = FALSE;
299
300 //
301 // initialize the shell lib (we must be in non-auto-init...)
302 //
303 Status = ShellInitialize ();
304 ASSERT_EFI_ERROR (Status);
305
306 Status = CommandInit ();
307 ASSERT_EFI_ERROR (Status);
308
309 //
310 // parse the command line
311 //
312 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
313 if (EFI_ERROR (Status)) {
314 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
315 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, L"drivers", ProblemParam);
316 FreePool (ProblemParam);
317 ShellStatus = SHELL_INVALID_PARAMETER;
318 } else {
319 ASSERT (FALSE);
320 }
321 } else {
322 if (ShellCommandLineGetCount (Package) > 1) {
323 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle, L"drivers");
324 ShellStatus = SHELL_INVALID_PARAMETER;
325 } else {
326 if (ShellCommandLineGetFlag (Package, L"-l")) {
327 Lang = ShellCommandLineGetValue (Package, L"-l");
328 if (Lang != NULL) {
329 Language = AllocateZeroPool (StrSize (Lang));
330 if (Language == NULL) {
331 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDriver1HiiHandle, L"drivers");
333 return (SHELL_OUT_OF_RESOURCES);
334 }
335
336 AsciiSPrint (Language, StrSize (Lang), "%S", Lang);
337 } else {
338 ASSERT (Language == NULL);
339 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDriver1HiiHandle, L"drivers", L"-l");
342 }
343 }
344
345 if (ShellCommandLineGetFlag (Package, L"-sfo")) {
346 SfoFlag = TRUE;
347 FormatString = HiiGetString (gShellDriver1HiiHandle, STRING_TOKEN (STR_DRIVERS_ITEM_LINE_SFO), Language);
348 //
349 // print the SFO header
350 //
352 -1,
353 -1,
354 Language,
355 STRING_TOKEN (STR_GEN_SFO_HEADER),
356 gShellDriver1HiiHandle,
357 L"drivers"
358 );
359 } else {
360 FormatString = HiiGetString (gShellDriver1HiiHandle, STRING_TOKEN (STR_DRIVERS_ITEM_LINE), Language);
361 //
362 // print the header row
363 //
365 -1,
366 -1,
367 Language,
368 STRING_TOKEN (STR_DRIVERS_HEADER_LINES),
369 gShellDriver1HiiHandle
370 );
371 }
372
373 if (FormatString == NULL) {
374 // Assume the string is present because it is hard-coded and report out of memory
375 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDriver1HiiHandle, L"drivers");
377 return (SHELL_OUT_OF_RESOURCES);
378 }
379
380 HandleList = GetHandleListByProtocol (&gEfiDriverBindingProtocolGuid);
381 for (HandleWalker = HandleList; HandleWalker != NULL && *HandleWalker != NULL; HandleWalker++) {
382 ChildCount = 0;
383 DeviceCount = 0;
384 Status = ParseHandleDatabaseForChildDevices (*HandleWalker, &ChildCount, NULL);
385 Status = PARSE_HANDLE_DATABASE_DEVICES (*HandleWalker, &DeviceCount, NULL);
386 Temp2 = GetDevicePathTextForHandle (*HandleWalker);
387 DriverVersion = ReturnDriverVersion (*HandleWalker);
388 DriverConfig = ReturnDriverConfig (*HandleWalker);
389 DriverDiag = ReturnDriverDiag (*HandleWalker);
390 FullDriverName = GetStringNameFromHandle (*HandleWalker, Language);
391 ImageName = GetImageNameFromHandle (*HandleWalker);
392
393 UnicodeValueToStringS (ChildCountStr, sizeof (ChildCountStr), 0, ChildCount, 0);
394 UnicodeValueToStringS (DeviceCountStr, sizeof (DeviceCountStr), 0, DeviceCount, 0);
395 TruncatedDriverName = NULL;
396 if (!SfoFlag && (FullDriverName != NULL)) {
397 TruncatedDriverName = AllocateZeroPool ((MAX_LEN_DRIVER_NAME + 1) * sizeof (CHAR16));
398 if (TruncatedDriverName == NULL) {
399 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDriver1HiiHandle, L"drivers");
401 return (SHELL_OUT_OF_RESOURCES);
402 }
403
404 StrnCpyS (TruncatedDriverName, MAX_LEN_DRIVER_NAME + 1, FullDriverName, MAX_LEN_DRIVER_NAME);
405 }
406
407 if (!SfoFlag) {
409 -1,
410 -1,
411 FormatString,
412 ConvertHandleToHandleIndex (*HandleWalker),
413 DriverVersion,
414 ChildCount > 0 ? L'B' : (DeviceCount > 0 ? L'D' : L'?'),
415 DriverConfig ? L'X' : L'-',
416 DriverDiag ? L'X' : L'-',
417 DeviceCount > 0 ? DeviceCountStr : L"-",
418 ChildCount > 0 ? ChildCountStr : L"-",
419 TruncatedDriverName,
420 ImageName == NULL ? L"" : ImageName
421 );
422 } else {
424 -1,
425 -1,
426 FormatString,
427 ConvertHandleToHandleIndex (*HandleWalker),
428 DriverVersion,
429 ChildCount > 0 ? L'B' : (DeviceCount > 0 ? L'D' : L'?'),
430 DriverConfig ? L'Y' : L'N',
431 DriverDiag ? L'Y' : L'N',
432 DeviceCount,
433 ChildCount,
434 FullDriverName,
435 Temp2 == NULL ? L"" : Temp2
436 );
437 }
438
439 if (TruncatedDriverName != NULL) {
440 FreePool (TruncatedDriverName);
441 }
442
443 if (Temp2 != NULL) {
444 FreePool (Temp2);
445 }
446
447 if (ImageName != NULL) {
448 FreePool (ImageName);
449 }
450
452 ShellStatus = SHELL_ABORTED;
453 break;
454 }
455 }
456 }
457
458 SHELL_FREE_NON_NULL (Language);
460 SHELL_FREE_NON_NULL (FormatString);
461 }
462
463 return (ShellStatus);
464}
UINT64 UINTN
UINTN EFIAPI StrSize(IN CONST CHAR16 *String)
Definition: String.c:72
RETURN_STATUS EFIAPI StrnCpyS(OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source, IN UINTN Length)
Definition: SafeString.c:310
BOOLEAN EFIAPI IsDevicePathEnd(IN CONST VOID *Node)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI NextDevicePathNode(IN CONST VOID *Node)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI AppendDevicePath(IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath OPTIONAL, IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL)
CHAR16 *EFIAPI ConvertDevicePathToText(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN BOOLEAN DisplayOnly, IN BOOLEAN AllowShortcuts)
UINT32 ReturnDriverVersion(IN CONST EFI_HANDLE TheHandle)
Definition: Drivers.c:148
BOOLEAN ReturnDriverDiag(IN CONST EFI_HANDLE TheHandle)
Definition: Drivers.c:122
CHAR16 * GetImageNameFromHandle(IN CONST EFI_HANDLE Handle)
Definition: Drivers.c:175
CHAR16 * GetDevicePathTextForHandle(IN EFI_HANDLE TheHandle)
Definition: Drivers.c:30
SHELL_STATUS EFIAPI ShellCommandRunDrivers(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: Drivers.c:267
BOOLEAN ReturnDriverConfig(IN CONST EFI_HANDLE TheHandle)
Definition: Drivers.c:99
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
CONST CHAR16 *EFIAPI GetStringNameFromHandle(IN CONST EFI_HANDLE TheHandle, IN CONST CHAR8 *Language)
EFI_STATUS EFIAPI ParseHandleDatabaseForChildDevices(IN CONST EFI_HANDLE DriverHandle, IN UINTN *MatchingHandleCount, OUT EFI_HANDLE **MatchingHandleBuffer OPTIONAL)
#define PARSE_HANDLE_DATABASE_DEVICES(DriverHandle, Count, Buffer)
UINTN EFIAPI ConvertHandleToHandleIndex(IN CONST EFI_HANDLE TheHandle)
EFI_HANDLE *EFIAPI GetHandleListByProtocol(IN CONST EFI_GUID *ProtocolGuid OPTIONAL)
EFI_STRING EFIAPI HiiGetString(IN EFI_HII_HANDLE HiiHandle, IN EFI_STRING_ID StringId, IN CONST CHAR8 *Language OPTIONAL)
Definition: HiiString.c:211
RETURN_STATUS EFIAPI UnicodeValueToStringS(IN OUT CHAR16 *Buffer, IN UINTN BufferSize, IN UINTN Flags, IN INT64 Value, IN UINTN Width)
Definition: PrintLib.c:652
UINTN EFIAPI AsciiSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString,...)
Definition: PrintLib.c:813
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_OUT_OF_RESOURCES
Definition: Shell.h:73
@ SHELL_ABORTED
Definition: Shell.h:128
@ SHELL_SUCCESS
Definition: Shell.h:25
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
EFI_STATUS EFIAPI CommandInit(VOID)
CONST CHAR16 *EFIAPI ShellCommandLineGetValue(IN CONST LIST_ENTRY *CheckPackage, IN CHAR16 *KeyString)
BOOLEAN EFIAPI ShellGetExecutionBreakFlag(VOID)
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
BOOLEAN EFIAPI ShellCommandLineGetFlag(IN CONST LIST_ENTRY *CONST CheckPackage, IN CONST CHAR16 *CONST KeyString)
@ TypeValue
A flag that has some data following it with a space (IE "-a 1").
Definition: ShellLib.h:700
@ TypeFlag
A flag that is present or not present only (IE "-a").
Definition: ShellLib.h:699
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellInitialize(VOID)
Definition: UefiShellLib.c:532
EFI_STATUS EFIAPI ShellPrintEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR16 *Format,...)
UINTN EFIAPI ShellCommandLineGetCount(IN CONST LIST_ENTRY *CheckPackage)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_HANDLE gImageHandle
EFI_BOOT_SERVICES * gBS
#define STRING_TOKEN(t)
EFI_GUID *EFIAPI EfiGetNameGuidFromFwVolDevicePathNode(IN CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvDevicePathNode)
Definition: UefiNotTiano.c:292
EFI_DEVICE_PATH_PROTOCOL * FilePath
Definition: LoadedImage.h:54
EFI_HANDLE DeviceHandle
The device handle that the EFI Image was loaded from.
Definition: LoadedImage.h:53
Definition: Base.h:213