]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/DevTree.c
ShellPkg: Add '-nc' flag support in 'disconnect' command
[mirror_edk2.git] / ShellPkg / Library / UefiShellDriver1CommandsLib / DevTree.c
1 /** @file
2 Main file for DevTree shell Driver1 function.
3
4 (C) Copyright 2014-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UefiShellDriver1CommandsLib.h"
17
18 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
19 {L"-d", TypeFlag},
20 {L"-l", TypeValue},
21 {NULL, TypeMax}
22 };
23
24 /**
25 Display a tree starting from this handle.
26
27 @param[in] TheHandle The handle to start with.
28 @param[in] Lang Optionally, a UEFI defined language code.
29 @param[in] UseDevPaths TRUE to display info from DevPath as identifiers.
30 FALSE will use component name protocol instead.
31 @param[in] IndentCharCount How many characters to indent (allows for recursion).
32 @param[in] HiiString The string from HII to use for output.
33
34 @retval SHELL_SUCCESS The operation was successful.
35 **/
36 SHELL_STATUS
37 EFIAPI
38 DoDevTreeForHandle(
39 IN CONST EFI_HANDLE TheHandle,
40 IN CONST CHAR8 *Lang OPTIONAL,
41 IN CONST BOOLEAN UseDevPaths,
42 IN CONST UINTN IndentCharCount,
43 IN CONST CHAR16 *HiiString
44 )
45 {
46 SHELL_STATUS ShellStatus;
47 EFI_STATUS Status;
48 CHAR16 *FormatString;
49 CHAR16 *Name;
50 EFI_HANDLE *ChildHandleBuffer;
51 UINTN ChildCount;
52 UINTN LoopVar;
53
54 Status = EFI_SUCCESS;
55 ShellStatus = SHELL_SUCCESS;
56 Name = NULL;
57 ChildHandleBuffer = NULL;
58 ChildCount = 0;
59
60 ASSERT (TheHandle != NULL);
61 ASSERT (HiiString != NULL);
62
63 if (ShellGetExecutionBreakFlag()) {
64 ShellStatus = SHELL_ABORTED;
65 return ShellStatus;
66 }
67
68 //
69 // We want controller handles. they will not have LoadedImage or DriverBinding (or others...)
70 //
71 Status = gBS->OpenProtocol (
72 TheHandle,
73 &gEfiDriverBindingProtocolGuid,
74 NULL,
75 NULL,
76 NULL,
77 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
78 );
79 if (!EFI_ERROR (Status)) {
80 return SHELL_SUCCESS;
81 }
82
83 Status = gBS->OpenProtocol (
84 TheHandle,
85 &gEfiLoadedImageProtocolGuid,
86 NULL,
87 NULL,
88 NULL,
89 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
90 );
91 if (!EFI_ERROR (Status)) {
92 return SHELL_SUCCESS;
93 }
94
95 FormatString = AllocateZeroPool(StrSize(HiiString) + (10)*sizeof(FormatString[0]));
96 if (FormatString == NULL) {
97 return SHELL_OUT_OF_RESOURCES;
98 }
99
100 //
101 // we generate the format string on the fly so that we can control the
102 // number of space characters that the first (empty) string has. this
103 // handles the indenting.
104 //
105
106 UnicodeSPrint(FormatString, StrSize(HiiString) + (10)*sizeof(FormatString[0]), L"%%%ds %s", IndentCharCount, HiiString);
107 gEfiShellProtocol->GetDeviceName((EFI_HANDLE)TheHandle, !UseDevPaths?EFI_DEVICE_NAME_USE_COMPONENT_NAME|EFI_DEVICE_NAME_USE_DEVICE_PATH:EFI_DEVICE_NAME_USE_DEVICE_PATH, (CHAR8*)Lang, &Name);
108 //
109 // print out the information for ourselves
110 //
111 ShellPrintEx(
112 -1,
113 -1,
114 FormatString,
115 L"",
116 ConvertHandleToHandleIndex(TheHandle),
117 Name==NULL?L"Unknown":Name);
118
119 FreePool(FormatString);
120 if (Name != NULL) {
121 FreePool(Name);
122 }
123
124 //
125 // recurse on each child handle with IndentCharCount + 2
126 //
127 ParseHandleDatabaseForChildControllers(TheHandle, &ChildCount, &ChildHandleBuffer);
128 for (LoopVar = 0 ; LoopVar < ChildCount && ShellStatus == SHELL_SUCCESS; LoopVar++){
129 ShellStatus = DoDevTreeForHandle(ChildHandleBuffer[LoopVar], Lang, UseDevPaths, IndentCharCount+2, HiiString);
130 if (ShellStatus == SHELL_ABORTED) {
131 break;
132 }
133 }
134
135 if (ChildHandleBuffer != NULL) {
136 FreePool(ChildHandleBuffer);
137 }
138
139 return (ShellStatus);
140 }
141
142 /**
143 Function for 'devtree' command.
144
145 @param[in] ImageHandle Handle to the Image (NULL if Internal).
146 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
147 **/
148 SHELL_STATUS
149 EFIAPI
150 ShellCommandRunDevTree (
151 IN EFI_HANDLE ImageHandle,
152 IN EFI_SYSTEM_TABLE *SystemTable
153 )
154 {
155 EFI_STATUS Status;
156 LIST_ENTRY *Package;
157 CHAR16 *ProblemParam;
158 SHELL_STATUS ShellStatus;
159 CHAR8 *Language;
160 CONST CHAR16 *Lang;
161 CHAR16 *HiiString;
162 UINTN LoopVar;
163 EFI_HANDLE TheHandle;
164 BOOLEAN FlagD;
165 UINT64 Intermediate;
166 UINTN ParentControllerHandleCount;
167 EFI_HANDLE *ParentControllerHandleBuffer;
168
169 ShellStatus = SHELL_SUCCESS;
170 Status = EFI_SUCCESS;
171 Language = NULL;
172
173 //
174 // initialize the shell lib (we must be in non-auto-init...)
175 //
176 Status = ShellInitialize();
177 ASSERT_EFI_ERROR(Status);
178
179 Status = CommandInit();
180 ASSERT_EFI_ERROR(Status);
181
182 //
183 // parse the command line
184 //
185 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
186 if (EFI_ERROR(Status)) {
187 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
188 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, L"devtree", ProblemParam);
189 FreePool(ProblemParam);
190 ShellStatus = SHELL_INVALID_PARAMETER;
191 } else {
192 ASSERT(FALSE);
193 }
194 } else {
195 if (ShellCommandLineGetCount(Package) > 2) {
196 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle, L"devtree");
197 ShellCommandLineFreeVarList (Package);
198 return (SHELL_INVALID_PARAMETER);
199 }
200 Lang = ShellCommandLineGetValue(Package, L"-l");
201 if (Lang != NULL) {
202 Language = AllocateZeroPool(StrSize(Lang));
203 AsciiSPrint(Language, StrSize(Lang), "%S", Lang);
204 } else if (!ShellCommandLineGetFlag(Package, L"-l")){
205 ASSERT(Language == NULL);
206 // Language = AllocateZeroPool(10);
207 // AsciiSPrint(Language, 10, "en-us");
208 } else {
209 ASSERT(Language == NULL);
210 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDriver1HiiHandle, L"devtree", L"-l");
211 ShellCommandLineFreeVarList (Package);
212 return (SHELL_INVALID_PARAMETER);
213 }
214 FlagD = ShellCommandLineGetFlag(Package, L"-d");
215
216 Lang = ShellCommandLineGetRawValue(Package, 1);
217 HiiString = HiiGetString(gShellDriver1HiiHandle, STRING_TOKEN (STR_DEV_TREE_OUTPUT), Language);
218
219 if (Lang == NULL) {
220 for (LoopVar = 1 ; ; LoopVar++){
221 TheHandle = ConvertHandleIndexToHandle(LoopVar);
222 if (TheHandle == NULL){
223 break;
224 }
225
226 //
227 // Skip handles that do not have device path protocol
228 //
229 Status = gBS->OpenProtocol (
230 TheHandle,
231 &gEfiDevicePathProtocolGuid,
232 NULL,
233 NULL,
234 NULL,
235 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
236 );
237 if (EFI_ERROR (Status)) {
238 continue;
239 }
240
241 //
242 // Skip handles that do have parents
243 //
244 ParentControllerHandleBuffer = NULL;
245 Status = PARSE_HANDLE_DATABASE_PARENTS (
246 TheHandle,
247 &ParentControllerHandleCount,
248 &ParentControllerHandleBuffer
249 );
250 SHELL_FREE_NON_NULL (ParentControllerHandleBuffer);
251 if (ParentControllerHandleCount > 0) {
252 continue;
253 }
254
255 //
256 // Start a devtree from TheHandle that has a device path and no parents
257 //
258 ShellStatus = DoDevTreeForHandle(TheHandle, Language, FlagD, 0, HiiString);
259 }
260 } else {
261 Status = ShellConvertStringToUint64(Lang, &Intermediate, TRUE, FALSE);
262 if (EFI_ERROR(Status) || ConvertHandleIndexToHandle((UINTN)Intermediate) == NULL) {
263 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, L"devtree", Lang);
264 ShellStatus = SHELL_INVALID_PARAMETER;
265 } else {
266 ShellStatus = DoDevTreeForHandle(ConvertHandleIndexToHandle((UINTN)Intermediate), Language, FlagD, 0, HiiString);
267 }
268 }
269
270 if (HiiString != NULL) {
271 FreePool(HiiString);
272 }
273 SHELL_FREE_NON_NULL(Language);
274 ShellCommandLineFreeVarList (Package);
275 }
276
277 return (ShellStatus);
278 }