]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/Drivers.c
ShellPkg: Add 3 missing function declaration comments and change a function static...
[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 Lang = ShellCommandLineGetValue(Package, L"-l");
225 if (Lang != NULL) {
226 Language = AllocateZeroPool(StrSize(Lang));
227 AsciiSPrint(Language, StrSize(Lang), "%S", Lang);
228 } else if (!ShellCommandLineGetFlag(Package, L"-l")){
229 ASSERT(Language == NULL);
230 // Language = AllocateZeroPool(10);
231 // AsciiSPrint(Language, 10, "en-us");
232 } else {
233 ASSERT(Language == NULL);
234 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDriver1HiiHandle, L"-l");
235 ShellCommandLineFreeVarList (Package);
236 return (SHELL_INVALID_PARAMETER);
237 }
238
239 if (ShellCommandLineGetFlag(Package, L"-sfo")) {
240 FormatString = HiiGetString(gShellDriver1HiiHandle, STRING_TOKEN(STR_DRIVERS_ITEM_LINE_SFO), Language);
241 } else {
242 FormatString = HiiGetString(gShellDriver1HiiHandle, STRING_TOKEN(STR_DRIVERS_ITEM_LINE), Language);
243 //
244 // print the header row
245 //
246 ShellPrintHiiEx(
247 -1,
248 -1,
249 Language,
250 STRING_TOKEN(STR_DRIVERS_HEADER_LINES),
251 gShellDriver1HiiHandle);
252 }
253
254 HandleList = GetHandleListByProtocol(&gEfiDriverBindingProtocolGuid);
255 for (HandleWalker = HandleList ; HandleWalker != NULL && *HandleWalker != NULL ; HandleWalker++){
256 ChildCount = 0;
257 DeviceCount = 0;
258 Status = ParseHandleDatabaseForChildDevices (*HandleWalker, &ChildCount , NULL);
259 Status = PARSE_HANDLE_DATABASE_DEVICES (*HandleWalker, &DeviceCount, NULL);
260 Temp2 = GetDevicePathTextForHandle(*HandleWalker);
261 DriverVersion = ReturnDriverVersion(*HandleWalker);
262 DriverConfig = ReturnDriverConfig(*HandleWalker);
263 DriverDiag = ReturnDriverDiag (*HandleWalker);
264 Lang = GetStringNameFromHandle(*HandleWalker, Language==NULL?"en":Language);
265
266 ShellPrintEx(
267 -1,
268 -1,
269 FormatString,
270 ConvertHandleToHandleIndex(*HandleWalker),
271 DriverVersion,
272 ChildCount > 0?L'B':(DeviceCount > 0?L'D':L'?'),
273 DriverConfig?L'Y':L'N',
274 DriverDiag?L'Y':L'N',
275 DeviceCount,
276 ChildCount,
277 Lang,
278 Temp2==NULL?L"":Temp2
279 );
280 if (Temp2 != NULL) {
281 FreePool(Temp2);
282 }
283 }
284 }
285 SHELL_FREE_NON_NULL(Language);
286 ShellCommandLineFreeVarList (Package);
287 SHELL_FREE_NON_NULL(FormatString);
288 }
289
290 return (ShellStatus);
291 }