]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/DevTree.c
Update return value.
[mirror_edk2.git] / ShellPkg / Library / UefiShellDriver1CommandsLib / DevTree.c
1 /** @file
2 Main file for DevTree shell Driver1 function.
3
4 Copyright (c) 2010, 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 SHELL_STATUS
24 EFIAPI
25 DoDevTreeForHandle(
26 IN CONST EFI_HANDLE TheHandle,
27 IN CONST CHAR8 *Lang OPTIONAL,
28 IN CONST BOOLEAN UseDevPaths,
29 IN CONST UINTN IndentCharCount,
30 IN CONST CHAR16 *HiiString
31 )
32 {
33 SHELL_STATUS ShellStatus;
34 EFI_STATUS Status;
35 CHAR16 *FormatString;
36 CHAR16 *Name;
37 EFI_HANDLE *ChildHandleBuffer;
38 UINTN ChildCount;
39 UINTN LoopVar;
40
41 Status = EFI_SUCCESS;
42 ShellStatus = SHELL_SUCCESS;
43 Name = NULL;
44 ChildHandleBuffer = NULL;
45 ChildCount = 0;
46
47 ASSERT(TheHandle != NULL);
48 //
49 // We want controller handles. they will not have LoadedImage or DriverBinding (or others...)
50 //
51 Status = gBS->OpenProtocol (
52 TheHandle,
53 &gEfiDriverBindingProtocolGuid,
54 NULL,
55 NULL,
56 NULL,
57 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
58 );
59 if (!EFI_ERROR (Status)) {
60 return SHELL_SUCCESS;
61 }
62
63 Status = gBS->OpenProtocol (
64 TheHandle,
65 &gEfiLoadedImageProtocolGuid,
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 //
76 // If we are at the begining then we want root handles they have no parents and do have device path.
77 //
78 if (IndentCharCount == 0) {
79 Status = gBS->OpenProtocol (
80 TheHandle,
81 &gEfiDevicePathProtocolGuid,
82 NULL,
83 NULL,
84 NULL,
85 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
86 );
87 if (EFI_ERROR (Status)) {
88 return SHELL_SUCCESS;
89 }
90 }
91
92 FormatString = AllocateZeroPool(StrSize(HiiString) + (10)*sizeof(FormatString[0]));
93
94 ASSERT(HiiString != NULL);
95 ASSERT(FormatString != NULL);
96
97 //
98 // we generate the format string on the fly so that we can control the
99 // number of space characters that the first (empty) string has. this
100 // handles the indenting.
101 //
102
103 UnicodeSPrint(FormatString, StrSize(HiiString) + (10)*sizeof(FormatString[0]), L"%%%ds %s", IndentCharCount, HiiString);
104 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);
105 //
106 // print out the information for ourselves
107 //
108 ShellPrintEx(
109 -1,
110 -1,
111 FormatString,
112 L"",
113 ConvertHandleToHandleIndex(TheHandle),
114 Name==NULL?L"Unknown":Name);
115
116 FreePool(FormatString);
117 if (Name != NULL) {
118 FreePool(Name);
119 }
120
121 //
122 // recurse on each child handle with IndentCharCount + 2
123 //
124 ParseHandleDatabaseForChildControllers(TheHandle, &ChildCount, &ChildHandleBuffer);
125 for (LoopVar = 0 ; LoopVar < ChildCount && ShellStatus == SHELL_SUCCESS; LoopVar++){
126 ShellStatus = DoDevTreeForHandle(ChildHandleBuffer[LoopVar], Lang, UseDevPaths, IndentCharCount+2, HiiString);
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
160 ShellStatus = SHELL_SUCCESS;
161 Status = EFI_SUCCESS;
162 Language = NULL;
163
164 //
165 // initialize the shell lib (we must be in non-auto-init...)
166 //
167 Status = ShellInitialize();
168 ASSERT_EFI_ERROR(Status);
169
170 Status = CommandInit();
171 ASSERT_EFI_ERROR(Status);
172
173 //
174 // parse the command line
175 //
176 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
177 if (EFI_ERROR(Status)) {
178 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
179 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, ProblemParam);
180 FreePool(ProblemParam);
181 ShellStatus = SHELL_INVALID_PARAMETER;
182 } else {
183 ASSERT(FALSE);
184 }
185 } else {
186 if (ShellCommandLineGetCount(Package) > 2) {
187 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle);
188 ShellCommandLineFreeVarList (Package);
189 return (SHELL_INVALID_PARAMETER);
190 }
191 Lang = ShellCommandLineGetValue(Package, L"-l");
192 if (Lang != NULL) {
193 Language = AllocateZeroPool(StrSize(Lang));
194 AsciiSPrint(Language, StrSize(Lang), "%S", Lang);
195 } else if (!ShellCommandLineGetFlag(Package, L"-l")){
196 ASSERT(Language == NULL);
197 // Language = AllocateZeroPool(10);
198 // AsciiSPrint(Language, 10, "en-us");
199 } else {
200 ASSERT(Language == NULL);
201 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDriver1HiiHandle, L"-l");
202 ShellCommandLineFreeVarList (Package);
203 return (SHELL_INVALID_PARAMETER);
204 }
205 FlagD = ShellCommandLineGetFlag(Package, L"-d");
206
207 Lang = ShellCommandLineGetRawValue(Package, 1);
208 HiiString = HiiGetString(gShellDriver1HiiHandle, STRING_TOKEN (STR_DEV_TREE_OUTPUT), Language);
209
210 if (Lang == NULL) {
211 for (LoopVar = 1 ; ; LoopVar++){
212 TheHandle = ConvertHandleIndexToHandle(LoopVar);
213 if (TheHandle == NULL){
214 break;
215 }
216 ShellStatus = DoDevTreeForHandle(TheHandle, Language, FlagD, 0, HiiString);
217 }
218 } else {
219 if (!ShellIsHexOrDecimalNumber(Lang, TRUE, FALSE) || ConvertHandleIndexToHandle(StrHexToUintn(Lang)) == NULL) {
220 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, Lang);
221 ShellStatus = SHELL_INVALID_PARAMETER;
222 } else {
223 ShellStatus = DoDevTreeForHandle(ConvertHandleIndexToHandle(StrHexToUintn(Lang)), Language, FlagD, 0, HiiString);
224 }
225 }
226
227 if (HiiString != NULL) {
228 FreePool(HiiString);
229 }
230 SHELL_FREE_NON_NULL(Language);
231 ShellCommandLineFreeVarList (Package);
232 }
233
234 return (ShellStatus);
235 }