]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/Drivers.c
1f7675b8b561d06224296f16be2d365b8a30108d
[mirror_edk2.git] / ShellPkg / Library / UefiShellDriver1CommandsLib / Drivers.c
1 /** @file
2 Main file for Drivers shell Driver1 function.
3
4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
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
9
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.
12
13 **/
14
15 #include "UefiShellDriver1CommandsLib.h"
16
17 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
18 {L"-sfo", TypeFlag},
19 {L"-l", TypeValue},
20 {NULL, TypeMax}
21 };
22
23 /**
24 Get a device path (in text format) for a given handle.
25
26 @param[in] TheHandle The handle to get the device path for.
27
28 @retval NULL An error occured.
29 @return A pointer to the driver path as a string. The callee must
30 free this memory.
31 **/
32 CHAR16*
33 EFIAPI
34 GetDevicePathTextForHandle(
35 IN EFI_HANDLE TheHandle
36 )
37 {
38 EFI_STATUS Status;
39 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
40 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;
41 EFI_DEVICE_PATH_PROTOCOL *FinalPath;
42 CHAR16 *RetVal;
43
44 FinalPath = NULL;
45
46 Status = gBS->OpenProtocol (
47 TheHandle,
48 &gEfiLoadedImageProtocolGuid,
49 (VOID**)&LoadedImage,
50 gImageHandle,
51 NULL,
52 EFI_OPEN_PROTOCOL_GET_PROTOCOL
53 );
54 if (!EFI_ERROR (Status)) {
55 Status = gBS->OpenProtocol (
56 LoadedImage->DeviceHandle,
57 &gEfiDevicePathProtocolGuid,
58 (VOID**)&ImageDevicePath,
59 gImageHandle,
60 NULL,
61 EFI_OPEN_PROTOCOL_GET_PROTOCOL
62 );
63 if (!EFI_ERROR (Status)) {
64 FinalPath = AppendDevicePath (ImageDevicePath, LoadedImage->FilePath);
65 gBS->CloseProtocol(
66 LoadedImage->DeviceHandle,
67 &gEfiDevicePathProtocolGuid,
68 gImageHandle,
69 NULL);
70 }
71 gBS->CloseProtocol(
72 TheHandle,
73 &gEfiLoadedImageProtocolGuid,
74 gImageHandle,
75 NULL);
76 }
77
78 if (FinalPath == NULL) {
79 return (NULL);
80 }
81 RetVal = gEfiShellProtocol->GetFilePathFromDevicePath(FinalPath);
82 if (RetVal == NULL) {
83 RetVal = gDevPathToText->ConvertDevicePathToText(FinalPath, TRUE, TRUE);
84 }
85 FreePool(FinalPath);
86 return (RetVal);
87 }
88
89 /**
90 Determine if the given handle has Driver Configuration protocol.
91
92 @param[in] TheHandle The handle to the driver to test.
93
94 @retval TRUE The driver does have Driver Configuration.
95 @retval FALSE The driver does not have Driver Configuration.
96 **/
97 BOOLEAN
98 EFIAPI
99 ReturnDriverConfig(
100 IN CONST EFI_HANDLE TheHandle
101 )
102 {
103 EFI_STATUS Status;
104 Status = gBS->OpenProtocol((EFI_HANDLE)TheHandle, &gEfiDriverConfigurationProtocolGuid, NULL, gImageHandle, NULL, EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
105 if (EFI_ERROR(Status)) {
106 return (FALSE);
107 }
108 return (TRUE);
109 }
110
111 /**
112 Determine if the given handle has DriverDiagnostics protocol.
113
114 @param[in] TheHandle The handle to the driver to test.
115
116 @retval TRUE The driver does have Driver Diasgnostics.
117 @retval FALSE The driver does not have Driver Diagnostics.
118 **/
119 BOOLEAN
120 EFIAPI
121 ReturnDriverDiag(
122 IN CONST EFI_HANDLE TheHandle
123 )
124 {
125 EFI_STATUS Status;
126 Status = gBS->OpenProtocol((EFI_HANDLE)TheHandle, &gEfiDriverDiagnostics2ProtocolGuid, NULL, gImageHandle, NULL, EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
127 if (EFI_ERROR(Status)) {
128 Status = gBS->OpenProtocol((EFI_HANDLE)TheHandle, &gEfiDriverDiagnosticsProtocolGuid, NULL, gImageHandle, NULL, EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
129 if (EFI_ERROR(Status)) {
130 return (FALSE);
131 }
132 }
133 return (TRUE);
134 }
135
136 /**
137 Finds and returns the version of the driver specified by TheHandle.
138
139 @param[in] TheHandle The driver handle to get the version of.
140
141 @return The version of the driver.
142 @retval 0xFFFFFFFF An error ocurred.
143 **/
144 UINT32
145 EFIAPI
146 ReturnDriverVersion(
147 IN CONST EFI_HANDLE TheHandle
148 )
149 {
150 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
151 EFI_STATUS Status;
152 UINT32 RetVal;
153
154 RetVal = (UINT32)-1;
155
156 Status = gBS->OpenProtocol((EFI_HANDLE)TheHandle, &gEfiDriverBindingProtocolGuid, (VOID**)&DriverBinding, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
157 if (!EFI_ERROR(Status)) {
158 RetVal = DriverBinding->Version;
159 gBS->CloseProtocol(TheHandle, &gEfiDriverBindingProtocolGuid, gImageHandle, NULL);
160 }
161 return (RetVal);
162 }
163
164 /**
165 Function for 'drivers' command.
166
167 @param[in] ImageHandle Handle to the Image (NULL if Internal).
168 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
169 **/
170 SHELL_STATUS
171 EFIAPI
172 ShellCommandRunDrivers (
173 IN EFI_HANDLE ImageHandle,
174 IN EFI_SYSTEM_TABLE *SystemTable
175 )
176 {
177 EFI_STATUS Status;
178 LIST_ENTRY *Package;
179 CHAR16 *ProblemParam;
180 SHELL_STATUS ShellStatus;
181 CHAR8 *Language;
182 CONST CHAR16 *Lang;
183 EFI_HANDLE *HandleList;
184 EFI_HANDLE *HandleWalker;
185 UINTN ChildCount;
186 UINTN DeviceCount;
187 CHAR16 *Temp2;
188 CHAR16 *FormatString;
189 UINT32 DriverVersion;
190 BOOLEAN DriverConfig;
191 BOOLEAN DriverDiag;
192
193 ShellStatus = SHELL_SUCCESS;
194 Status = EFI_SUCCESS;
195 Language = NULL;
196 FormatString = NULL;
197
198 //
199 // initialize the shell lib (we must be in non-auto-init...)
200 //
201 Status = ShellInitialize();
202 ASSERT_EFI_ERROR(Status);
203
204 Status = CommandInit();
205 ASSERT_EFI_ERROR(Status);
206
207 //
208 // parse the command line
209 //
210 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
211 if (EFI_ERROR(Status)) {
212 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
213 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, ProblemParam);
214 FreePool(ProblemParam);
215 ShellStatus = SHELL_INVALID_PARAMETER;
216 } else {
217 ASSERT(FALSE);
218 }
219 } else {
220 if (ShellCommandLineGetCount(Package) > 1) {
221 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle);
222 ShellStatus = SHELL_INVALID_PARAMETER;
223 } else {
224 if (ShellCommandLineGetFlag(Package, L"-l")){
225 Lang = ShellCommandLineGetValue(Package, L"-l");
226 if (Lang != NULL) {
227 Language = AllocateZeroPool(StrSize(Lang));
228 AsciiSPrint(Language, StrSize(Lang), "%S", Lang);
229 } else {
230 ASSERT(Language == NULL);
231 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDriver1HiiHandle, L"-l");
232 ShellCommandLineFreeVarList (Package);
233 return (SHELL_INVALID_PARAMETER);
234 }
235 }
236
237 if (ShellCommandLineGetFlag(Package, L"-sfo")) {
238 FormatString = HiiGetString(gShellDriver1HiiHandle, STRING_TOKEN(STR_DRIVERS_ITEM_LINE_SFO), Language);
239 } else {
240 FormatString = HiiGetString(gShellDriver1HiiHandle, STRING_TOKEN(STR_DRIVERS_ITEM_LINE), Language);
241 //
242 // print the header row
243 //
244 ShellPrintHiiEx(
245 -1,
246 -1,
247 Language,
248 STRING_TOKEN(STR_DRIVERS_HEADER_LINES),
249 gShellDriver1HiiHandle);
250 }
251
252 HandleList = GetHandleListByProtocol(&gEfiDriverBindingProtocolGuid);
253 for (HandleWalker = HandleList ; HandleWalker != NULL && *HandleWalker != NULL ; HandleWalker++){
254 ChildCount = 0;
255 DeviceCount = 0;
256 Status = ParseHandleDatabaseForChildDevices (*HandleWalker, &ChildCount , NULL);
257 Status = PARSE_HANDLE_DATABASE_DEVICES (*HandleWalker, &DeviceCount, NULL);
258 Temp2 = GetDevicePathTextForHandle(*HandleWalker);
259 DriverVersion = ReturnDriverVersion(*HandleWalker);
260 DriverConfig = ReturnDriverConfig(*HandleWalker);
261 DriverDiag = ReturnDriverDiag (*HandleWalker);
262 Lang = GetStringNameFromHandle(*HandleWalker, Language);
263
264 ShellPrintEx(
265 -1,
266 -1,
267 FormatString,
268 ConvertHandleToHandleIndex(*HandleWalker),
269 DriverVersion,
270 ChildCount > 0?L'B':(DeviceCount > 0?L'D':L'?'),
271 DriverConfig?L'Y':L'N',
272 DriverDiag?L'Y':L'N',
273 DeviceCount,
274 ChildCount,
275 Lang,
276 Temp2==NULL?L"":Temp2
277 );
278 if (Temp2 != NULL) {
279 FreePool(Temp2);
280 }
281 }
282 }
283 SHELL_FREE_NON_NULL(Language);
284 ShellCommandLineFreeVarList (Package);
285 SHELL_FREE_NON_NULL(FormatString);
286 }
287
288 return (ShellStatus);
289 }