]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/OpenInfo.c
udk2010.up2.shell initial release.
[mirror_edk2.git] / ShellPkg / Library / UefiShellDriver1CommandsLib / OpenInfo.c
1 /** @file
2 Main file for OpenInfo 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 EFI_STATUS
18 EFIAPI
19 TraverseHandleDatabase (
20 IN CONST EFI_HANDLE DriverBindingHandle OPTIONAL,
21 IN CONST EFI_HANDLE ControllerHandle OPTIONAL,
22 IN UINTN *HandleCount,
23 OUT EFI_HANDLE **HandleBuffer,
24 OUT UINTN **HandleType
25 ){
26 EFI_STATUS Status;
27 UINTN HandleIndex;
28 EFI_GUID **ProtocolGuidArray;
29 UINTN ArrayCount;
30 UINTN ProtocolIndex;
31 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
32 UINTN OpenInfoCount;
33 UINTN OpenInfoIndex;
34 UINTN ChildIndex;
35
36 ASSERT(HandleCount != NULL);
37 ASSERT(HandleBuffer != NULL);
38 ASSERT(HandleType != NULL);
39 ASSERT(DriverBindingHandle != NULL || ControllerHandle != NULL);
40
41 *HandleCount = 0;
42 *HandleBuffer = NULL;
43 *HandleType = NULL;
44
45 //
46 // Retrieve the list of all handles from the handle database
47 //
48 Status = gBS->LocateHandleBuffer (
49 AllHandles,
50 NULL,
51 NULL,
52 HandleCount,
53 HandleBuffer
54 );
55 if (EFI_ERROR (Status)) {
56 return (Status);
57 }
58
59 *HandleType = AllocateZeroPool (*HandleCount * sizeof (UINTN));
60 ASSERT(*HandleType != NULL);
61
62 for (HandleIndex = 0; HandleIndex < *HandleCount; HandleIndex++) {
63 //
64 // Retrieve the list of all the protocols on each handle
65 //
66 Status = gBS->ProtocolsPerHandle (
67 (*HandleBuffer)[HandleIndex],
68 &ProtocolGuidArray,
69 &ArrayCount
70 );
71 if (!EFI_ERROR (Status)) {
72
73 for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {
74
75 //
76 // Set the bit describing what this handle has
77 //
78 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiLoadedImageProtocolGuid) != FALSE) {
79 (*HandleType)[HandleIndex] |= HANDLE_RELATIONSHIP_IMAGE_HANDLE;
80 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverBindingProtocolGuid) != FALSE) {
81 (*HandleType)[HandleIndex] |= HANDLE_RELATIONSHIP_DRIVER_BINDING_HANDLE;
82 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverConfiguration2ProtocolGuid) != FALSE) {
83 (*HandleType)[HandleIndex] |= HANDLE_RELATIONSHIP_DRIVER_CONFIGURATION_HANDLE;
84 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverConfigurationProtocolGuid) != FALSE) {
85 (*HandleType)[HandleIndex] |= HANDLE_RELATIONSHIP_DRIVER_CONFIGURATION_HANDLE;
86 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverDiagnostics2ProtocolGuid) != FALSE) {
87 (*HandleType)[HandleIndex] |= HANDLE_RELATIONSHIP_DRIVER_DIAGNOSTICS_HANDLE;
88 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverDiagnosticsProtocolGuid) != FALSE) {
89 (*HandleType)[HandleIndex] |= HANDLE_RELATIONSHIP_DRIVER_DIAGNOSTICS_HANDLE;
90 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiComponentName2ProtocolGuid) != FALSE) {
91 (*HandleType)[HandleIndex] |= HANDLE_RELATIONSHIP_COMPONENT_NAME_HANDLE;
92 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiComponentNameProtocolGuid) != FALSE) {
93 (*HandleType)[HandleIndex] |= HANDLE_RELATIONSHIP_COMPONENT_NAME_HANDLE;
94 } else if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDevicePathProtocolGuid) != FALSE) {
95 (*HandleType)[HandleIndex] |= HANDLE_RELATIONSHIP_DEVICE_HANDLE;
96 } else {
97 DEBUG_CODE_BEGIN();
98 ASSERT((*HandleType)[HandleIndex] == (*HandleType)[HandleIndex]);
99 DEBUG_CODE_END();
100 }
101 //
102 // Retrieve the list of agents that have opened each protocol
103 //
104 Status = gBS->OpenProtocolInformation (
105 (*HandleBuffer)[HandleIndex],
106 ProtocolGuidArray[ProtocolIndex],
107 &OpenInfo,
108 &OpenInfoCount
109 );
110 if (!EFI_ERROR (Status)) {
111 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
112 if (DriverBindingHandle != NULL && OpenInfo[OpenInfoIndex].AgentHandle == DriverBindingHandle) {
113 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {
114 //
115 // Mark the device handle as being managed by the driver specified by DriverBindingHandle
116 //
117 (*HandleType)[HandleIndex] |= (HANDLE_RELATIONSHIP_DEVICE_HANDLE | HANDLE_RELATIONSHIP_CONTROLLER_HANDLE);
118 }
119 if (ControllerHandle != NULL && (*HandleBuffer)[HandleIndex] == ControllerHandle) {
120 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
121 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
122 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].ControllerHandle) {
123 (*HandleType)[ChildIndex] |= (HANDLE_RELATIONSHIP_DEVICE_HANDLE | HANDLE_RELATIONSHIP_CHILD_HANDLE);
124 }
125 }
126 }
127 }
128 } else if (DriverBindingHandle == NULL && OpenInfo[OpenInfoIndex].ControllerHandle == ControllerHandle) {
129 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {
130 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
131 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle) {
132 //
133 // mark the handle who opened this as a device driver
134 //
135 (*HandleType)[ChildIndex] |= HANDLE_RELATIONSHIP_DEVICE_DRIVER;
136 }
137 }
138 }
139 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
140 //
141 // this handle has people opening by child so it must be a parent
142 //
143 (*HandleType)[HandleIndex] |= HANDLE_RELATIONSHIP_PARENT_HANDLE;
144 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
145 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle) {
146 (*HandleType)[ChildIndex] |= HANDLE_RELATIONSHIP_BUS_DRIVER;
147 }
148 }
149 }
150 }
151 }
152
153 FreePool (OpenInfo);
154 }
155 }
156
157 FreePool (ProtocolGuidArray);
158 }
159 }
160
161 if (EFI_ERROR(Status)) {
162 if (*HandleType != NULL) {
163 FreePool (*HandleType);
164 }
165 if (*HandleBuffer != NULL) {
166 FreePool (*HandleBuffer);
167 }
168
169 *HandleCount = 0;
170 *HandleBuffer = NULL;
171 *HandleType = NULL;
172 }
173
174 return Status;
175 }
176
177
178 SHELL_STATUS
179 EFIAPI
180 ShellCommandRunOpenInfo (
181 VOID *RESERVED
182 ) {
183 EFI_STATUS Status;
184 LIST_ENTRY *Package;
185 CHAR16 *ProblemParam;
186 SHELL_STATUS ShellStatus;
187 EFI_HANDLE theHandle;
188 EFI_HANDLE *HandleList;
189 UINTN Count;
190 UINTN *Type;
191
192 ShellStatus = SHELL_SUCCESS;
193
194 //
195 // initialize the shell lib (we must be in non-auto-init...)
196 //
197 Status = ShellInitialize();
198 ASSERT_EFI_ERROR(Status);
199
200 Status = CommandInit();
201 ASSERT_EFI_ERROR(Status);
202
203 //
204 // parse the command line
205 //
206 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
207 if EFI_ERROR(Status) {
208 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
209 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, ProblemParam);
210 FreePool(ProblemParam);
211 ShellStatus = SHELL_INVALID_PARAMETER;
212 } else {
213 ASSERT(FALSE);
214 }
215 } else {
216 if (ShellCommandLineGetCount() > 2){
217 //
218 // error for too many parameters
219 //
220 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle);
221 ShellStatus = SHELL_INVALID_PARAMETER;
222 } else if (ShellCommandLineGetCount() == 0) {
223 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDriver1HiiHandle);
224 ShellStatus = SHELL_INVALID_PARAMETER;
225 } else {
226 if (ShellCommandLineGetRawValue(Package, 1) != NULL && CommandLibGetHandleValue(StrHexToUintn(ShellCommandLineGetRawValue(Package, 1))) == NULL){
227 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, ShellCommandLineGetRawValue(Package, 1));
228 ShellStatus = SHELL_INVALID_PARAMETER;
229 } else {
230 theHandle = CommandLibGetHandleValue(StrHexToUintn(ShellCommandLineGetRawValue(Package, 1)));
231 ASSERT(theHandle != NULL);
232 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_OPENINFO_HEADER_LINE), gShellDriver1HiiHandle, StrHexToUintn(ShellCommandLineGetRawValue(Package, 1)), theHandle);
233 Status = TraverseHandleDatabase (NULL, theHandle, &Count, &HandleList, &Type);
234 if (EFI_ERROR(Status) == FALSE && Count > 0) {
235 } else {
236 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, ShellCommandLineGetRawValue(Package, 1));
237 ShellStatus = SHELL_NOT_FOUND;
238 }
239 }
240 }
241 }
242 return (ShellStatus);
243 }