]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/DevTree.c
connect - add comments and add input verification
[mirror_edk2.git] / ShellPkg / Library / UefiShellDriver1CommandsLib / DevTree.c
1 /** @file
2 Main file for DevTree shell Driver1 function.
3
4 Copyright (c) 2010 - 2011, 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 //
88 // If we are at the begining then we want root handles they have no parents and do have device path.
89 //
90 if (IndentCharCount == 0) {
91 Status = gBS->OpenProtocol (
92 TheHandle,
93 &gEfiDevicePathProtocolGuid,
94 NULL,
95 NULL,
96 NULL,
97 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
98 );
99 if (EFI_ERROR (Status)) {
100 return SHELL_SUCCESS;
101 }
102 }
103
104 FormatString = AllocateZeroPool(StrSize(HiiString) + (10)*sizeof(FormatString[0]));
105
106 ASSERT(HiiString != NULL);
107 ASSERT(FormatString != NULL);
108
109 //
110 // we generate the format string on the fly so that we can control the
111 // number of space characters that the first (empty) string has. this
112 // handles the indenting.
113 //
114
115 UnicodeSPrint(FormatString, StrSize(HiiString) + (10)*sizeof(FormatString[0]), L"%%%ds %s", IndentCharCount, HiiString);
116 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);
117 //
118 // print out the information for ourselves
119 //
120 ShellPrintEx(
121 -1,
122 -1,
123 FormatString,
124 L"",
125 ConvertHandleToHandleIndex(TheHandle),
126 Name==NULL?L"Unknown":Name);
127
128 FreePool(FormatString);
129 if (Name != NULL) {
130 FreePool(Name);
131 }
132
133 //
134 // recurse on each child handle with IndentCharCount + 2
135 //
136 ParseHandleDatabaseForChildControllers(TheHandle, &ChildCount, &ChildHandleBuffer);
137 for (LoopVar = 0 ; LoopVar < ChildCount && ShellStatus == SHELL_SUCCESS; LoopVar++){
138 ShellStatus = DoDevTreeForHandle(ChildHandleBuffer[LoopVar], Lang, UseDevPaths, IndentCharCount+2, HiiString);
139 }
140
141 if (ChildHandleBuffer != NULL) {
142 FreePool(ChildHandleBuffer);
143 }
144
145 return (ShellStatus);
146 }
147
148 /**
149 Function for 'devtree' command.
150
151 @param[in] ImageHandle Handle to the Image (NULL if Internal).
152 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
153 **/
154 SHELL_STATUS
155 EFIAPI
156 ShellCommandRunDevTree (
157 IN EFI_HANDLE ImageHandle,
158 IN EFI_SYSTEM_TABLE *SystemTable
159 )
160 {
161 EFI_STATUS Status;
162 LIST_ENTRY *Package;
163 CHAR16 *ProblemParam;
164 SHELL_STATUS ShellStatus;
165 CHAR8 *Language;
166 CONST CHAR16 *Lang;
167 CHAR16 *HiiString;
168 UINTN LoopVar;
169 EFI_HANDLE TheHandle;
170 BOOLEAN FlagD;
171 UINT64 Intermediate;
172
173 ShellStatus = SHELL_SUCCESS;
174 Status = EFI_SUCCESS;
175 Language = NULL;
176
177 //
178 // initialize the shell lib (we must be in non-auto-init...)
179 //
180 Status = ShellInitialize();
181 ASSERT_EFI_ERROR(Status);
182
183 Status = CommandInit();
184 ASSERT_EFI_ERROR(Status);
185
186 //
187 // parse the command line
188 //
189 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
190 if (EFI_ERROR(Status)) {
191 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
192 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, ProblemParam);
193 FreePool(ProblemParam);
194 ShellStatus = SHELL_INVALID_PARAMETER;
195 } else {
196 ASSERT(FALSE);
197 }
198 } else {
199 if (ShellCommandLineGetCount(Package) > 2) {
200 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle);
201 ShellCommandLineFreeVarList (Package);
202 return (SHELL_INVALID_PARAMETER);
203 }
204 Lang = ShellCommandLineGetValue(Package, L"-l");
205 if (Lang != NULL) {
206 Language = AllocateZeroPool(StrSize(Lang));
207 AsciiSPrint(Language, StrSize(Lang), "%S", Lang);
208 } else if (!ShellCommandLineGetFlag(Package, L"-l")){
209 ASSERT(Language == NULL);
210 // Language = AllocateZeroPool(10);
211 // AsciiSPrint(Language, 10, "en-us");
212 } else {
213 ASSERT(Language == NULL);
214 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDriver1HiiHandle, L"-l");
215 ShellCommandLineFreeVarList (Package);
216 return (SHELL_INVALID_PARAMETER);
217 }
218 FlagD = ShellCommandLineGetFlag(Package, L"-d");
219
220 Lang = ShellCommandLineGetRawValue(Package, 1);
221 HiiString = HiiGetString(gShellDriver1HiiHandle, STRING_TOKEN (STR_DEV_TREE_OUTPUT), Language);
222
223 if (Lang == NULL) {
224 for (LoopVar = 1 ; ; LoopVar++){
225 TheHandle = ConvertHandleIndexToHandle(LoopVar);
226 if (TheHandle == NULL){
227 break;
228 }
229 ShellStatus = DoDevTreeForHandle(TheHandle, Language, FlagD, 0, HiiString);
230 }
231 } else {
232 Status = ShellConvertStringToUint64(Lang, &Intermediate, TRUE, FALSE);
233 if (EFI_ERROR(Status) || ConvertHandleIndexToHandle((UINTN)Intermediate) == NULL) {
234 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, Lang);
235 ShellStatus = SHELL_INVALID_PARAMETER;
236 } else {
237 ShellStatus = DoDevTreeForHandle(ConvertHandleIndexToHandle((UINTN)Intermediate), Language, FlagD, 0, HiiString);
238 }
239 }
240
241 if (HiiString != NULL) {
242 FreePool(HiiString);
243 }
244 SHELL_FREE_NON_NULL(Language);
245 ShellCommandLineFreeVarList (Package);
246 }
247
248 return (ShellStatus);
249 }