]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/DevTree.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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
113 FreePool (FormatString);
114 if (Name != NULL) {
115 FreePool (Name);
116 }
117
118 //
119 // recurse on each child handle with IndentCharCount + 2
120 //
121 ParseHandleDatabaseForChildControllers (TheHandle, &ChildCount, &ChildHandleBuffer);
122 for (LoopVar = 0; LoopVar < ChildCount && ShellStatus == SHELL_SUCCESS; LoopVar++) {
123 ShellStatus = DoDevTreeForHandle (ChildHandleBuffer[LoopVar], Lang, UseDevPaths, IndentCharCount+2, HiiString);
124 if (ShellStatus == SHELL_ABORTED) {
125 break;
126 }
127 }
128
129 if (ChildHandleBuffer != NULL) {
130 FreePool (ChildHandleBuffer);
131 }
132
133 return (ShellStatus);
134 }
135
136 /**
137 Function for 'devtree' command.
138
139 @param[in] ImageHandle Handle to the Image (NULL if Internal).
140 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
141 **/
142 SHELL_STATUS
143 EFIAPI
144 ShellCommandRunDevTree (
145 IN EFI_HANDLE ImageHandle,
146 IN EFI_SYSTEM_TABLE *SystemTable
147 )
148 {
149 EFI_STATUS Status;
150 LIST_ENTRY *Package;
151 CHAR16 *ProblemParam;
152 SHELL_STATUS ShellStatus;
153 CHAR8 *Language;
154 CONST CHAR16 *Lang;
155 CHAR16 *HiiString;
156 UINTN LoopVar;
157 EFI_HANDLE TheHandle;
158 BOOLEAN FlagD;
159 UINT64 Intermediate;
160 UINTN ParentControllerHandleCount;
161 EFI_HANDLE *ParentControllerHandleBuffer;
162
163 ShellStatus = SHELL_SUCCESS;
164 Status = EFI_SUCCESS;
165 Language = NULL;
166
167 //
168 // initialize the shell lib (we must be in non-auto-init...)
169 //
170 Status = ShellInitialize ();
171 ASSERT_EFI_ERROR (Status);
172
173 Status = CommandInit ();
174 ASSERT_EFI_ERROR (Status);
175
176 //
177 // parse the command line
178 //
179 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
180 if (EFI_ERROR (Status)) {
181 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
182 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, L"devtree", ProblemParam);
183 FreePool (ProblemParam);
184 ShellStatus = SHELL_INVALID_PARAMETER;
185 } else {
186 ASSERT (FALSE);
187 }
188 } else {
189 if (ShellCommandLineGetCount (Package) > 2) {
190 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle, L"devtree");
191 ShellCommandLineFreeVarList (Package);
192 return (SHELL_INVALID_PARAMETER);
193 }
194
195 Lang = ShellCommandLineGetValue (Package, L"-l");
196 if (Lang != NULL) {
197 Language = AllocateZeroPool (StrSize (Lang));
198 AsciiSPrint (Language, StrSize (Lang), "%S", Lang);
199 } else if (!ShellCommandLineGetFlag (Package, L"-l")) {
200 ASSERT (Language == NULL);
201 // Language = AllocateZeroPool(10);
202 // AsciiSPrint(Language, 10, "en-us");
203 } else {
204 ASSERT (Language == NULL);
205 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDriver1HiiHandle, L"devtree", L"-l");
206 ShellCommandLineFreeVarList (Package);
207 return (SHELL_INVALID_PARAMETER);
208 }
209
210 FlagD = ShellCommandLineGetFlag (Package, L"-d");
211
212 Lang = ShellCommandLineGetRawValue (Package, 1);
213 HiiString = HiiGetString (gShellDriver1HiiHandle, STRING_TOKEN (STR_DEV_TREE_OUTPUT), Language);
214
215 if (Lang == NULL) {
216 for (LoopVar = 1; ; LoopVar++) {
217 TheHandle = ConvertHandleIndexToHandle (LoopVar);
218 if (TheHandle == NULL) {
219 break;
220 }
221
222 //
223 // Skip handles that do not have device path protocol
224 //
225 Status = gBS->OpenProtocol (
226 TheHandle,
227 &gEfiDevicePathProtocolGuid,
228 NULL,
229 NULL,
230 NULL,
231 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
232 );
233 if (EFI_ERROR (Status)) {
234 continue;
235 }
236
237 //
238 // Skip handles that do have parents
239 //
240 ParentControllerHandleBuffer = NULL;
241 Status = PARSE_HANDLE_DATABASE_PARENTS (
242 TheHandle,
243 &ParentControllerHandleCount,
244 &ParentControllerHandleBuffer
245 );
246 SHELL_FREE_NON_NULL (ParentControllerHandleBuffer);
247 if (ParentControllerHandleCount > 0) {
248 continue;
249 }
250
251 //
252 // Start a devtree from TheHandle that has a device path and no parents
253 //
254 ShellStatus = DoDevTreeForHandle (TheHandle, Language, FlagD, 0, HiiString);
255 }
256 } else {
257 Status = ShellConvertStringToUint64 (Lang, &Intermediate, TRUE, FALSE);
258 if (EFI_ERROR (Status) || (ConvertHandleIndexToHandle ((UINTN)Intermediate) == NULL)) {
259 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, L"devtree", Lang);
260 ShellStatus = SHELL_INVALID_PARAMETER;
261 } else {
262 ShellStatus = DoDevTreeForHandle (ConvertHandleIndexToHandle ((UINTN)Intermediate), Language, FlagD, 0, HiiString);
263 }
264 }
265
266 if (HiiString != NULL) {
267 FreePool (HiiString);
268 }
269
270 SHELL_FREE_NON_NULL (Language);
271 ShellCommandLineFreeVarList (Package);
272 }
273
274 return (ShellStatus);
275 }