2 Main file for DevTree shell Driver1 function.
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
10 #include "UefiShellDriver1CommandsLib.h"
12 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
19 Display a tree starting from this handle.
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.
28 @retval SHELL_SUCCESS The operation was successful.
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
39 SHELL_STATUS ShellStatus
;
43 EFI_HANDLE
*ChildHandleBuffer
;
48 ShellStatus
= SHELL_SUCCESS
;
50 ChildHandleBuffer
= NULL
;
53 ASSERT (TheHandle
!= NULL
);
54 ASSERT (HiiString
!= NULL
);
56 if (ShellGetExecutionBreakFlag ()) {
57 ShellStatus
= SHELL_ABORTED
;
62 // We want controller handles. they will not have LoadedImage or DriverBinding (or others...)
64 Status
= gBS
->OpenProtocol (
66 &gEfiDriverBindingProtocolGuid
,
70 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
72 if (!EFI_ERROR (Status
)) {
76 Status
= gBS
->OpenProtocol (
78 &gEfiLoadedImageProtocolGuid
,
82 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
84 if (!EFI_ERROR (Status
)) {
88 FormatString
= AllocateZeroPool (StrSize (HiiString
) + (10)*sizeof (FormatString
[0]));
89 if (FormatString
== NULL
) {
90 return SHELL_OUT_OF_RESOURCES
;
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.
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
);
102 // print out the information for ourselves
109 ConvertHandleToHandleIndex (TheHandle
),
110 Name
== NULL
? L
"Unknown" : Name
113 FreePool (FormatString
);
119 // recurse on each child handle with IndentCharCount + 2
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
) {
129 if (ChildHandleBuffer
!= NULL
) {
130 FreePool (ChildHandleBuffer
);
133 return (ShellStatus
);
137 Function for 'devtree' command.
139 @param[in] ImageHandle Handle to the Image (NULL if Internal).
140 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
144 ShellCommandRunDevTree (
145 IN EFI_HANDLE ImageHandle
,
146 IN EFI_SYSTEM_TABLE
*SystemTable
151 CHAR16
*ProblemParam
;
152 SHELL_STATUS ShellStatus
;
157 EFI_HANDLE TheHandle
;
160 UINTN ParentControllerHandleCount
;
161 EFI_HANDLE
*ParentControllerHandleBuffer
;
163 ShellStatus
= SHELL_SUCCESS
;
164 Status
= EFI_SUCCESS
;
168 // initialize the shell lib (we must be in non-auto-init...)
170 Status
= ShellInitialize ();
171 ASSERT_EFI_ERROR (Status
);
173 Status
= CommandInit ();
174 ASSERT_EFI_ERROR (Status
);
177 // parse the command line
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
;
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
);
195 Lang
= ShellCommandLineGetValue (Package
, L
"-l");
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");
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
);
210 FlagD
= ShellCommandLineGetFlag (Package
, L
"-d");
212 Lang
= ShellCommandLineGetRawValue (Package
, 1);
213 HiiString
= HiiGetString (gShellDriver1HiiHandle
, STRING_TOKEN (STR_DEV_TREE_OUTPUT
), Language
);
216 for (LoopVar
= 1; ; LoopVar
++) {
217 TheHandle
= ConvertHandleIndexToHandle (LoopVar
);
218 if (TheHandle
== NULL
) {
223 // Skip handles that do not have device path protocol
225 Status
= gBS
->OpenProtocol (
227 &gEfiDevicePathProtocolGuid
,
231 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
233 if (EFI_ERROR (Status
)) {
238 // Skip handles that do have parents
240 ParentControllerHandleBuffer
= NULL
;
241 Status
= PARSE_HANDLE_DATABASE_PARENTS (
243 &ParentControllerHandleCount
,
244 &ParentControllerHandleBuffer
246 SHELL_FREE_NON_NULL (ParentControllerHandleBuffer
);
247 if (ParentControllerHandleCount
> 0) {
252 // Start a devtree from TheHandle that has a device path and no parents
254 ShellStatus
= DoDevTreeForHandle (TheHandle
, Language
, FlagD
, 0, HiiString
);
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
;
262 ShellStatus
= DoDevTreeForHandle (ConvertHandleIndexToHandle ((UINTN
)Intermediate
), Language
, FlagD
, 0, HiiString
);
266 if (HiiString
!= NULL
) {
267 FreePool (HiiString
);
270 SHELL_FREE_NON_NULL (Language
);
271 ShellCommandLineFreeVarList (Package
);
274 return (ShellStatus
);