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