]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/Devices.c
clarify error message when listing files based on a metaname without a current workin...
[mirror_edk2.git] / ShellPkg / Library / UefiShellDriver1CommandsLib / Devices.c
1 /** @file
2 Main file for devices 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 /**
18 Get lots of info about a device from its handle.
19
20 @param[in] TheHandle The device handle to get info on.
21 @param[in,out] Type On successful return R, B, or D (root, bus, or
22 device) will be placed in this buffer.
23 @param[in,out] Cfg On successful return this buffer will be
24 TRUE if the handle has configuration, FALSE
25 otherwise.
26 @param[in,out] Diag On successful return this buffer will be
27 TRUE if the handle has disgnostics, FALSE
28 otherwise.
29 @param[in,out] Parents On successful return this buffer will be
30 contain the number of parent handles.
31 @param[in,out] Devices On successful return this buffer will be
32 contain the number of devices controlled.
33 @param[in,out] Children On successful return this buffer will be
34 contain the number of child handles.
35 @param[out] Name The pointer to a buffer that will be allocated
36 and contain the string name of the handle.
37 The caller must free this memory.
38 @param[in] Language The language code as defined by the UEFI spec.
39
40 @retval EFI_SUCCESS The info is there.
41 @retval EFI_INVALID_PARAMETER A parameter was invalid.
42 **/
43 EFI_STATUS
44 EFIAPI
45 GetDeviceHandleInfo (
46 IN EFI_HANDLE TheHandle,
47 IN OUT CHAR16 *Type,
48 IN OUT BOOLEAN *Cfg,
49 IN OUT BOOLEAN *Diag,
50 IN OUT UINTN *Parents,
51 IN OUT UINTN *Devices,
52 IN OUT UINTN *Children,
53 OUT CHAR16 **Name,
54 IN CONST CHAR8 *Language
55 )
56 {
57 EFI_STATUS Status;
58 EFI_HANDLE *HandleBuffer;
59 UINTN Count;
60
61 UINTN TestHandle = 0x43;
62
63 if (TheHandle == NULL
64 || Type == NULL
65 || Cfg == NULL
66 || Diag == NULL
67 || Parents == NULL
68 || Devices == NULL
69 || Children == NULL
70 || Name == NULL ) {
71 return (EFI_INVALID_PARAMETER);
72 }
73
74 if (ConvertHandleToHandleIndex(TheHandle) == TestHandle) {
75 TestHandle = TestHandle;
76 }
77
78 *Cfg = FALSE;
79 *Diag = FALSE;
80 *Children = 0;
81 *Parents = 0;
82 *Devices = 0;
83 *Type = L' ';
84 *Name = CHAR_NULL;
85 HandleBuffer = NULL;
86 Status = EFI_SUCCESS;
87
88 gEfiShellProtocol->GetDeviceName(TheHandle, EFI_DEVICE_NAME_USE_COMPONENT_NAME|EFI_DEVICE_NAME_USE_DEVICE_PATH, (CHAR8*)Language, Name);
89
90 Status = ParseHandleDatabaseForChildControllers(TheHandle, Children, NULL);
91 // if (!EFI_ERROR(Status)) {
92 Status = PARSE_HANDLE_DATABASE_PARENTS(TheHandle, Parents, NULL);
93 if (/*!EFI_ERROR(Status) && */Parents != NULL && Children != NULL) {
94 if (*Parents == 0) {
95 *Type = L'R';
96 } else if (*Children > 0) {
97 *Type = L'B';
98 } else {
99 *Type = L'D';
100 }
101 }
102 // }
103 Status = PARSE_HANDLE_DATABASE_UEFI_DRIVERS(TheHandle, Devices, &HandleBuffer);
104 if (!EFI_ERROR(Status) && Devices != NULL && HandleBuffer != NULL) {
105 for (Count = 0 ; Count < *Devices ; Count++) {
106 if (!EFI_ERROR(gBS->OpenProtocol(HandleBuffer[Count], &gEfiDriverConfigurationProtocolGuid, NULL, NULL, gImageHandle, EFI_OPEN_PROTOCOL_TEST_PROTOCOL))) {
107 *Cfg = TRUE;
108 }
109 if (!EFI_ERROR(gBS->OpenProtocol(HandleBuffer[Count], &gEfiDriverDiagnosticsProtocolGuid, NULL, NULL, gImageHandle, EFI_OPEN_PROTOCOL_TEST_PROTOCOL))) {
110 *Diag = TRUE;
111 }
112 if (!EFI_ERROR(gBS->OpenProtocol(HandleBuffer[Count], &gEfiDriverDiagnostics2ProtocolGuid, NULL, NULL, gImageHandle, EFI_OPEN_PROTOCOL_TEST_PROTOCOL))) {
113 *Diag = TRUE;
114 }
115 }
116 SHELL_FREE_NON_NULL(HandleBuffer);
117 }
118
119 return (Status);
120 }
121
122 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
123 {L"-l", TypeValue},
124 {NULL, TypeMax}
125 };
126
127 /**
128 Function for 'devices' command.
129
130 @param[in] ImageHandle Handle to the Image (NULL if Internal).
131 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
132 **/
133 SHELL_STATUS
134 EFIAPI
135 ShellCommandRunDevices (
136 IN EFI_HANDLE ImageHandle,
137 IN EFI_SYSTEM_TABLE *SystemTable
138 )
139 {
140 EFI_STATUS Status;
141 LIST_ENTRY *Package;
142 CHAR16 *ProblemParam;
143 SHELL_STATUS ShellStatus;
144 CHAR8 *Language;
145 EFI_HANDLE *HandleList;
146 EFI_HANDLE *HandleListWalker;
147 CHAR16 Type;
148 BOOLEAN Cfg;
149 BOOLEAN Diag;
150 UINTN Parents;
151 UINTN Devices;
152 UINTN Children;
153 CHAR16 *Name;
154 CONST CHAR16 *Lang;
155
156 ShellStatus = SHELL_SUCCESS;
157 Language = NULL;
158
159 //
160 // initialize the shell lib (we must be in non-auto-init...)
161 //
162 Status = ShellInitialize();
163 ASSERT_EFI_ERROR(Status);
164
165 Status = CommandInit();
166 ASSERT_EFI_ERROR(Status);
167
168 //
169 // parse the command line
170 //
171 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
172 if (EFI_ERROR(Status)) {
173 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
174 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, ProblemParam);
175 FreePool(ProblemParam);
176 ShellStatus = SHELL_INVALID_PARAMETER;
177 } else {
178 ASSERT(FALSE);
179 }
180 } else {
181 //
182 // if more than 0 'value' parameters we have too many parameters
183 //
184 if (ShellCommandLineGetRawValue(Package, 1) != NULL){
185 //
186 // error for too many parameters
187 //
188 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle);
189 ShellStatus = SHELL_INVALID_PARAMETER;
190 } else {
191 //
192 // get the language if necessary
193 //
194 Lang = ShellCommandLineGetValue(Package, L"-l");
195 if (Lang != NULL) {
196 Language = AllocateZeroPool(StrSize(Lang));
197 AsciiSPrint(Language, StrSize(Lang), "%S", Lang);
198 } else if (!ShellCommandLineGetFlag(Package, L"-l")){
199 ASSERT(Language == NULL);
200 // Language = AllocateZeroPool(10);
201 // AsciiSPrint(Language, 10, "en-us");
202 } else {
203 ASSERT(Language == NULL);
204 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDriver1HiiHandle, L"-l");
205 ShellCommandLineFreeVarList (Package);
206 return (SHELL_INVALID_PARAMETER);
207 }
208
209
210 //
211 // Print Header
212 //
213 ShellPrintHiiEx(-1, -1, Language, STRING_TOKEN (STR_DEVICES_HEADER_LINES), gShellDriver1HiiHandle);
214
215 //
216 // loop through each handle
217 //
218 HandleList = GetHandleListByProtocol(NULL);
219 ASSERT(HandleList != NULL);
220 for (HandleListWalker = HandleList
221 ; HandleListWalker != NULL && *HandleListWalker != NULL /*&& !EFI_ERROR(Status)*/
222 ; HandleListWalker++
223 ){
224
225 //
226 // get all the info on each handle
227 //
228 Name = NULL;
229 Status = GetDeviceHandleInfo(*HandleListWalker, &Type, &Cfg, &Diag, &Parents, &Devices, &Children, &Name, Language);
230 if (Name != NULL && (Parents != 0 || Devices != 0 || Children != 0)) {
231 ShellPrintHiiEx(
232 -1,
233 -1,
234 Language,
235 STRING_TOKEN (STR_DEVICES_ITEM_LINE),
236 gShellDriver1HiiHandle,
237 ConvertHandleToHandleIndex(*HandleListWalker),
238 Type,
239 Cfg?L'X':L'-',
240 Diag?L'X':L'-',
241 Parents,
242 Devices,
243 Children,
244 Name!=NULL?Name:L"<UNKNOWN>");
245 }
246 if (Name != NULL) {
247 FreePool(Name);
248 }
249 }
250
251 if (HandleList != NULL) {
252 FreePool(HandleList);
253 }
254
255 }
256 SHELL_FREE_NON_NULL(Language);
257 ShellCommandLineFreeVarList (Package);
258 }
259 return (ShellStatus);
260 }
261