2 Main file for connect shell Driver1 function.
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
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.
15 #include "UefiShellDriver1CommandsLib.h"
18 Connect controller(s) and driver(s).
20 @param[in] ControllerHandle The handle to the controller. Should have driver binding on it.
21 @param[in] DriverHandle The handle to the driver. Should have driver binding.
22 @param[in] Recursive TRUE to connect recursively, FALSE otherwise.
23 @param[in] Output TRUE to have info on the screen, FALSE otherwise.
24 @param[in] AlwaysOutput Override Output for errors.
26 @retval EFI_SUCCESS The operation was successful.
31 IN CONST EFI_HANDLE ControllerHandle OPTIONAL
,
32 IN CONST EFI_HANDLE DriverHandle OPTIONAL
,
33 IN CONST BOOLEAN Recursive
,
34 IN CONST BOOLEAN Output
,
35 IN CONST BOOLEAN AlwaysOutput
40 EFI_HANDLE
*ControllerHandleList
;
41 EFI_HANDLE
*DriverHandleList
;
42 EFI_HANDLE
*HandleWalker
;
44 ControllerHandleList
= NULL
;
45 Status
= EFI_NOT_FOUND
;
46 Status2
= EFI_NOT_FOUND
;
49 // If we have a single handle to connect make that a 'list'
51 if (DriverHandle
== NULL
) {
52 DriverHandleList
= NULL
;
54 DriverHandleList
= AllocateZeroPool(2*sizeof(EFI_HANDLE
));
55 if (DriverHandleList
== NULL
) {
56 return (EFI_OUT_OF_RESOURCES
);
58 DriverHandleList
[0] = DriverHandle
;
59 DriverHandleList
[1] = NULL
;
63 // do we connect all controllers (with a loop) or a single one...
64 // This is where we call the gBS->ConnectController function.
66 if (ControllerHandle
== NULL
) {
67 ControllerHandleList
= GetHandleListByProtocol(&gEfiDevicePathProtocolGuid
);
68 for (HandleWalker
= ControllerHandleList
69 ; HandleWalker
!= NULL
&& *HandleWalker
!= NULL
72 Status
= gBS
->ConnectController(*HandleWalker
, DriverHandleList
, NULL
, Recursive
);
73 if (!EFI_ERROR(Status
)) {
74 Status2
= EFI_SUCCESS
;
76 if ((Output
&& !EFI_ERROR(Status
)) || AlwaysOutput
) {
77 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_HANDLE_RESULT
), gShellDriver1HiiHandle
, L
"Connect", ConvertHandleToHandleIndex(*HandleWalker
), Status
);
81 Status
= gBS
->ConnectController(ControllerHandle
, DriverHandleList
, NULL
, Recursive
);
82 if (!EFI_ERROR(Status
)) {
83 Status2
= EFI_SUCCESS
;
85 if ((Output
&& !EFI_ERROR(Status
)) || AlwaysOutput
) {
86 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_HANDLE_RESULT
), gShellDriver1HiiHandle
, L
"Connect", ConvertHandleToHandleIndex(ControllerHandle
), Status
);
91 // Free any memory we allocated.
93 if (ControllerHandleList
!= NULL
) {
94 FreePool(ControllerHandleList
);
96 if (DriverHandleList
!= NULL
) {
97 FreePool(DriverHandleList
);
103 Do a connect from an EFI variable via it's key name.
105 @param[in] Key The name of the EFI Variable.
107 @retval EFI_SUCCESS The operation was successful.
111 ConnectFromDevPaths (
115 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
116 EFI_DEVICE_PATH_PROTOCOL
*DevPathWalker
;
125 // Get the DevicePath buffer from the variable...
127 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
128 if (Status
== EFI_BUFFER_TOO_SMALL
) {
129 DevPath
= AllocateZeroPool(Length
);
130 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
133 Status
= EFI_NOT_FOUND
;
135 // walk the list of devices and connect them
137 for (DevPathWalker
= DevPath
138 ; DevPathWalker
< (DevPath
+ Length
) && EFI_ERROR(Status
) && DevPath
!= NULL
139 ; DevPathWalker
+= GetDevicePathSize(DevPathWalker
)
142 // get the correct handle from a given device path
144 if ((StrCmp(Key
, L
"ConInDev") == 0)
145 ||(StrCmp(Key
, L
"ConIn") == 0)
147 Status
= gBS
->LocateDevicePath((EFI_GUID
*)&gEfiConsoleInDeviceGuid
, &DevPathWalker
, &Handle
);
148 if (!EFI_ERROR(Status
)) {
149 Status
= ConnectControllers(NULL
, Handle
, FALSE
, TRUE
, FALSE
);
151 } else if ((StrCmp(Key
, L
"ConOutDev") == 0)
152 || (StrCmp(Key
, L
"ConErrDev") == 0)
153 || (StrCmp(Key
, L
"ConOut") == 0)
154 || (StrCmp(Key
, L
"ConErr") == 0)
156 Status
= gBS
->LocateDevicePath((EFI_GUID
*)&gEfiConsoleOutDeviceGuid
, &DevPathWalker
, &Handle
);
157 if (!EFI_ERROR(Status
)) {
158 Status
= ConnectControllers(NULL
, Handle
, FALSE
, TRUE
, FALSE
);
163 if (DevPath
!= NULL
) {
170 Convert the handle identifiers from strings and then connect them.
172 One of them should have driver binding and either can be NULL.
174 @param[in] Handle1 The first handle.
175 @param[in] Handle2 The second handle.
176 @param[in] Recursive TRUE to do connect recursively. FALSE otherwise.
177 @param[in] Output TRUE to have output to screen. FALSE otherwise.
179 @retval EFI_SUCCESS The operation was successful.
183 ConvertAndConnectControllers (
184 IN EFI_HANDLE
*Handle1 OPTIONAL
,
185 IN EFI_HANDLE
*Handle2 OPTIONAL
,
186 IN CONST BOOLEAN Recursive
,
187 IN CONST BOOLEAN Output
191 // if only one is NULL verify it's the proper one...
193 if ( (Handle1
== NULL
&& Handle2
!= NULL
)
194 || (Handle1
!= NULL
&& Handle2
== NULL
)
197 // Figure out which one should be NULL and move the handle to the right place.
198 // If Handle1 is NULL then test Handle2 and vise versa.
199 // The one that DOES has driver binding must be Handle2
201 if (Handle1
== NULL
) {
202 if (EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
210 if (EFI_ERROR(gBS
->OpenProtocol(Handle1
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
220 return (ConnectControllers(Handle1
, Handle2
, Recursive
, Output
, (BOOLEAN
)(Handle2
!= NULL
&& Handle1
!= NULL
)));
223 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
230 Function for 'connect' command.
232 @param[in] ImageHandle Handle to the Image (NULL if Internal).
233 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
237 ShellCommandRunConnect (
238 IN EFI_HANDLE ImageHandle
,
239 IN EFI_SYSTEM_TABLE
*SystemTable
244 CHAR16
*ProblemParam
;
245 SHELL_STATUS ShellStatus
;
246 CONST CHAR16
*Param1
;
247 CONST CHAR16
*Param2
;
253 ShellStatus
= SHELL_SUCCESS
;
256 // initialize the shell lib (we must be in non-auto-init...)
258 Status
= ShellInitialize();
259 ASSERT_EFI_ERROR(Status
);
261 Status
= CommandInit();
262 ASSERT_EFI_ERROR(Status
);
265 // parse the command line
267 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
268 if (EFI_ERROR(Status
)) {
269 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
270 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDriver1HiiHandle
, ProblemParam
);
271 FreePool(ProblemParam
);
272 ShellStatus
= SHELL_INVALID_PARAMETER
;
278 // if more than 2 'value' parameters (plus the name one) or either -r or -c with any value parameters we have too many parameters
280 if ((ShellCommandLineGetCount(Package
) > 3)
281 ||((ShellCommandLineGetFlag(Package
, L
"-r") || ShellCommandLineGetFlag(Package
, L
"-c")) && ShellCommandLineGetCount(Package
)>1)
282 ||(ShellCommandLineGetFlag(Package
, L
"-r") && ShellCommandLineGetFlag(Package
, L
"-c") )
285 // error for too many parameters
287 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDriver1HiiHandle
);
288 ShellStatus
= SHELL_INVALID_PARAMETER
;
289 } else if (ShellCommandLineGetFlag(Package
, L
"-c")) {
291 // do the conin and conout from EFI variables
292 // if the first fails dont 'loose' the error
294 Status
= ConnectFromDevPaths(L
"ConInDev");
295 if (EFI_ERROR(Status
)) {
296 ConnectFromDevPaths(L
"ConOutDev");
298 Status
= ConnectFromDevPaths(L
"ConOutDev");
300 if (EFI_ERROR(Status
)) {
301 ConnectFromDevPaths(L
"ConErrDev");
303 Status
= ConnectFromDevPaths(L
"ConErrDev");
305 if (EFI_ERROR(Status
)) {
306 ConnectFromDevPaths(L
"ConErr");
308 Status
= ConnectFromDevPaths(L
"ConErr");
310 if (EFI_ERROR(Status
)) {
311 ConnectFromDevPaths(L
"ConIn");
313 Status
= ConnectFromDevPaths(L
"ConIn");
315 if (EFI_ERROR(Status
)) {
316 ConnectFromDevPaths(L
"ConOut");
318 Status
= ConnectFromDevPaths(L
"ConOut");
320 if (EFI_ERROR(Status
)) {
321 ShellStatus
= SHELL_DEVICE_ERROR
;
325 // 0, 1, or 2 specific handles and possibly recursive
327 Param1
= ShellCommandLineGetRawValue(Package
, 1);
328 Param2
= ShellCommandLineGetRawValue(Package
, 2);
329 Count
= ShellCommandLineGetCount(Package
);
331 Status
= ShellConvertStringToUint64(Param1
, &Intermediate
, TRUE
, FALSE
);
332 Handle1
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
333 if (EFI_ERROR(Status
)) {
334 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param1
);
335 ShellStatus
= SHELL_INVALID_PARAMETER
;
337 Status
= ShellConvertStringToUint64(Param2
, &Intermediate
, TRUE
, FALSE
);
338 Handle2
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
339 if (EFI_ERROR(Status
)) {
340 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
341 ShellStatus
= SHELL_INVALID_PARAMETER
;
344 if (ShellStatus
== SHELL_SUCCESS
) {
345 if (Param1
!= NULL
&& Handle1
== NULL
){
346 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param1
);
347 ShellStatus
= SHELL_INVALID_PARAMETER
;
348 } else if (Param2
!= NULL
&& Handle2
== NULL
) {
349 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
350 ShellStatus
= SHELL_INVALID_PARAMETER
;
351 } else if (Handle2
!= NULL
&& Handle1
!= NULL
&& EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
352 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
353 ShellStatus
= SHELL_INVALID_PARAMETER
;
355 Status
= ConvertAndConnectControllers(Handle1
, Handle2
, ShellCommandLineGetFlag(Package
, L
"-r"), (BOOLEAN
)(Count
!=0));
356 if (EFI_ERROR(Status
)) {
357 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CONNECT_NONE
), gShellDriver1HiiHandle
);
358 ShellStatus
= SHELL_DEVICE_ERROR
;
364 ShellCommandLineFreeVarList (Package
);
366 return (ShellStatus
);