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