1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/** @file
Main file for Drivers shell Driver1 function.
(C) Copyright 2012-2015 Hewlett-Packard Development Company, L.P.<BR>
Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "UefiShellDriver1CommandsLib.h"
#define MAX_LEN_DRIVER_NAME 35
STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
{L"-sfo", TypeFlag},
{L"-l", TypeValue},
{NULL, TypeMax}
};
/**
Get a device path (in text format) for a given handle.
@param[in] TheHandle The handle to get the device path for.
@retval NULL An error occured.
@return A pointer to the driver path as a string. The callee must
free this memory.
**/
CHAR16*
GetDevicePathTextForHandle(
IN EFI_HANDLE TheHandle
)
{
EFI_STATUS Status;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;
EFI_DEVICE_PATH_PROTOCOL *FinalPath;
CHAR16 *RetVal;
FinalPath = NULL;
Status = gBS->OpenProtocol (
TheHandle,
&gEfiLoadedImageProtocolGuid,
(VOID**)&LoadedImage,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (!EFI_ERROR (Status)) {
Status = gBS->OpenProtocol (
LoadedImage->DeviceHandle,
&gEfiDevicePathProtocolGuid,
(VOID**)&ImageDevicePath,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (!EFI_ERROR (Status)) {
FinalPath = AppendDevicePath (ImageDevicePath, LoadedImage->FilePath);
gBS->CloseProtocol(
LoadedImage->DeviceHandle,
&gEfiDevicePathProtocolGuid,
gImageHandle,
NULL);
}
gBS->CloseProtocol(
TheHandle,
&gEfiLoadedImageProtocolGuid,
gImageHandle,
NULL);
}
if (FinalPath == NULL) {
return (NULL);
}
RetVal = gEfiShellProtocol->GetFilePathFromDevicePath(FinalPath);
if (RetVal == NULL) {
RetVal = ConvertDevicePathToText(FinalPath, TRUE, TRUE);
}
FreePool(FinalPath);
return (RetVal);
}
/**
Determine if the given handle has Driver Configuration protocol.
@param[in] TheHandle The handle to the driver to test.
@retval TRUE The driver does have Driver Configuration.
@retval FALSE The driver does not have Driver Configuration.
**/
BOOLEAN
ReturnDriverConfig(
IN CONST EFI_HANDLE TheHandle
)
{
EFI_STATUS Status;
Status = gBS->OpenProtocol((EFI_HANDLE)TheHandle, &gEfiDriverConfigurationProtocolGuid, NULL, gImageHandle, NULL, EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
if (EFI_ERROR(Status)) {
return (FALSE);
}
return (TRUE);
}
/**
Determine if the given handle has DriverDiagnostics protocol.
@param[in] TheHandle The handle to the driver to test.
@retval TRUE The driver does have Driver Diasgnostics.
@retval FALSE The driver does not have Driver Diagnostics.
**/
BOOLEAN
ReturnDriverDiag(
IN CONST EFI_HANDLE TheHandle
)
{
EFI_STATUS Status;
Status = gBS->OpenProtocol((EFI_HANDLE)TheHandle, &gEfiDriverDiagnostics2ProtocolGuid, NULL, gImageHandle, NULL, EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
if (EFI_ERROR(Status)) {
Status = gBS->OpenProtocol((EFI_HANDLE)TheHandle, &gEfiDriverDiagnosticsProtocolGuid, NULL, gImageHandle, NULL, EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
if (EFI_ERROR(Status)) {
return (FALSE);
}
}
return (TRUE);
}
/**
Finds and returns the version of the driver specified by TheHandle.
@param[in] TheHandle The driver handle to get the version of.
@return The version of the driver.
@retval 0xFFFFFFFF An error ocurred.
**/
UINT32
ReturnDriverVersion(
IN CONST EFI_HANDLE TheHandle
)
{
EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
EFI_STATUS Status;
UINT32 RetVal;
RetVal = (UINT32)-1;
Status = gBS->OpenProtocol((EFI_HANDLE)TheHandle, &gEfiDriverBindingProtocolGuid, (VOID**)&DriverBinding, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (!EFI_ERROR(Status)) {
RetVal = DriverBinding->Version;
gBS->CloseProtocol(TheHandle, &gEfiDriverBindingProtocolGuid, gImageHandle, NULL);
}
return (RetVal);
}
/**
Get image name from Image Handle.
@param[in] Handle Image Handle
@return A pointer to the image name as a string.
**/
CHAR16 *
GetImageNameFromHandle (
IN CONST EFI_HANDLE Handle
)
{
EFI_STATUS Status;
EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
EFI_DEVICE_PATH_PROTOCOL *DevPathNode;
EFI_GUID *NameGuid;
CHAR16 *ImageName;
UINTN BufferSize;
UINT32 AuthenticationStatus;
EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv2;
LoadedImage = NULL;
DriverBinding = NULL;
ImageName = NULL;
Status = gBS->OpenProtocol (
Handle,
&gEfiDriverBindingProtocolGuid,
(VOID **) &DriverBinding,
NULL,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
return NULL;
}
Status = gBS->OpenProtocol (
DriverBinding->ImageHandle,
&gEfiLoadedImageProtocolGuid,
(VOID**)&LoadedImage,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (!EFI_ERROR (Status)) {
DevPathNode = LoadedImage->FilePath;
if (DevPathNode == NULL) {
return NULL;
}
while (!IsDevicePathEnd (DevPathNode)) {
NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)DevPathNode);
if (NameGuid != NULL) {
Status = gBS->HandleProtocol (
LoadedImage->DeviceHandle,
&gEfiFirmwareVolume2ProtocolGuid,
(VOID **)&Fv2
);
if (!EFI_ERROR (Status)) {
Status = Fv2->ReadSection (
Fv2,
NameGuid,
EFI_SECTION_USER_INTERFACE,
0,
(VOID **)&ImageName,
&BufferSize,
&AuthenticationStatus
);
if (!EFI_ERROR (Status)) {
break;
}
ImageName = NULL;
}
}
//
// Next device path node
//
DevPathNode = NextDevicePathNode (DevPathNode);
}
if (ImageName == NULL) {
ImageName = ConvertDevicePathToText (LoadedImage->FilePath, TRUE, TRUE);
}
}
return ImageName;
}
/**
Function for 'drivers' command.
@param[in] ImageHandle Handle to the Image (NULL if Internal).
@param[in] SystemTable Pointer to the System Table (NULL if Internal).
**/
SHELL_STATUS
EFIAPI
ShellCommandRunDrivers (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
LIST_ENTRY *Package;
CHAR16 *ProblemParam;
SHELL_STATUS ShellStatus;
CHAR8 *Language;
CONST CHAR16 *Lang;
EFI_HANDLE *HandleList;
EFI_HANDLE *HandleWalker;
UINTN ChildCount;
UINTN DeviceCount;
CHAR16 ChildCountStr[3];
CHAR16 DeviceCountStr[3];
CHAR16 *Temp2;
CONST CHAR16 *FullDriverName;
CHAR16 *TruncatedDriverName;
CHAR16 *ImageName;
CHAR16 *FormatString;
UINT32 DriverVersion;
BOOLEAN DriverConfig;
BOOLEAN DriverDiag;
BOOLEAN SfoFlag;
ShellStatus = SHELL_SUCCESS;
Status = EFI_SUCCESS;
Language = NULL;
FormatString = NULL;
SfoFlag = FALSE;
//
// initialize the shell lib (we must be in non-auto-init...)
//
Status = ShellInitialize();
ASSERT_EFI_ERROR(Status);
Status = CommandInit();
ASSERT_EFI_ERROR(Status);
//
// parse the command line
//
Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
if (EFI_ERROR(Status)) {
if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, L"drivers", ProblemParam);
FreePool(ProblemParam);
ShellStatus = SHELL_INVALID_PARAMETER;
} else {
ASSERT(FALSE);
}
} else {
if (ShellCommandLineGetCount(Package) > 1) {
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle, L"drivers");
ShellStatus = SHELL_INVALID_PARAMETER;
} else {
if (ShellCommandLineGetFlag(Package, L"-l")){
Lang = ShellCommandLineGetValue(Package, L"-l");
if (Lang != NULL) {
Language = AllocateZeroPool(StrSize(Lang));
AsciiSPrint(Language, StrSize(Lang), "%S", Lang);
} else {
ASSERT(Language == NULL);
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDriver1HiiHandle, L"drivers", L"-l");
ShellCommandLineFreeVarList (Package);
return (SHELL_INVALID_PARAMETER);
}
}
if (ShellCommandLineGetFlag (Package, L"-sfo")) {
SfoFlag = TRUE;
FormatString = HiiGetString (gShellDriver1HiiHandle, STRING_TOKEN (STR_DRIVERS_ITEM_LINE_SFO), Language);
//
// print the SFO header
//
ShellPrintHiiEx (
-1,
-1,
Language,
STRING_TOKEN (STR_GEN_SFO_HEADER),
gShellDriver1HiiHandle,
L"drivers");
} else {
FormatString = HiiGetString (gShellDriver1HiiHandle, STRING_TOKEN (STR_DRIVERS_ITEM_LINE), Language);
//
// print the header row
//
ShellPrintHiiEx(
-1,
-1,
Language,
STRING_TOKEN (STR_DRIVERS_HEADER_LINES),
gShellDriver1HiiHandle);
}
HandleList = GetHandleListByProtocol(&gEfiDriverBindingProtocolGuid);
for (HandleWalker = HandleList ; HandleWalker != NULL && *HandleWalker != NULL ; HandleWalker++){
ChildCount = 0;
DeviceCount = 0;
Status = ParseHandleDatabaseForChildDevices (*HandleWalker, &ChildCount , NULL);
Status = PARSE_HANDLE_DATABASE_DEVICES (*HandleWalker, &DeviceCount, NULL);
Temp2 = GetDevicePathTextForHandle(*HandleWalker);
DriverVersion = ReturnDriverVersion(*HandleWalker);
DriverConfig = ReturnDriverConfig(*HandleWalker);
DriverDiag = ReturnDriverDiag (*HandleWalker);
FullDriverName = GetStringNameFromHandle(*HandleWalker, Language);
ImageName = GetImageNameFromHandle (*HandleWalker);
UnicodeValueToStringS (ChildCountStr, sizeof (ChildCountStr), 0, ChildCount, 0);
UnicodeValueToStringS (DeviceCountStr, sizeof (DeviceCountStr), 0, DeviceCount, 0);
TruncatedDriverName = NULL;
if (!SfoFlag && (FullDriverName != NULL)) {
TruncatedDriverName = AllocateZeroPool ((MAX_LEN_DRIVER_NAME + 1) * sizeof (CHAR16));
StrnCpyS (TruncatedDriverName, MAX_LEN_DRIVER_NAME + 1, FullDriverName, MAX_LEN_DRIVER_NAME);
}
if (!SfoFlag) {
ShellPrintEx (
-1,
-1,
FormatString,
ConvertHandleToHandleIndex (*HandleWalker),
DriverVersion,
ChildCount > 0 ? L'B' : (DeviceCount > 0 ? L'D' : L'?'),
DriverConfig ? L'X' : L'-',
DriverDiag ? L'X' : L'-',
DeviceCount > 0 ? DeviceCountStr : L"-",
ChildCount > 0 ? ChildCountStr : L"-",
TruncatedDriverName,
ImageName == NULL ? L"" : ImageName
);
} else {
ShellPrintEx (
-1,
-1,
FormatString,
ConvertHandleToHandleIndex (*HandleWalker),
DriverVersion,
ChildCount > 0 ? L'B' : (DeviceCount > 0 ? L'D' : L'?'),
DriverConfig ? L'Y' : L'N',
DriverDiag ? L'Y' : L'N',
DeviceCount,
ChildCount,
FullDriverName,
Temp2 == NULL ? L"" : Temp2
);
}
if (TruncatedDriverName != NULL) {
FreePool (TruncatedDriverName);
}
if (Temp2 != NULL) {
FreePool(Temp2);
}
if (ImageName != NULL) {
FreePool (ImageName);
}
if (ShellGetExecutionBreakFlag ()) {
ShellStatus = SHELL_ABORTED;
break;
}
}
}
SHELL_FREE_NON_NULL(Language);
ShellCommandLineFreeVarList (Package);
SHELL_FREE_NON_NULL(FormatString);
}
return (ShellStatus);
}
|