2 Main file for connect shell Driver1 function.
4 Copyright (c) 2010 - 2012, 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
"ErrOutDev") == 0)
153 || (StrCmp(Key
, L
"ConOut") == 0)
154 || (StrCmp(Key
, L
"ErrOut") == 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 Count
= (gInReconnect
?0x4:0x3);
281 if ((ShellCommandLineGetCount(Package
) > Count
)
282 ||((ShellCommandLineGetFlag(Package
, L
"-r") || ShellCommandLineGetFlag(Package
, L
"-c")) && ShellCommandLineGetCount(Package
)>1)
283 ||(ShellCommandLineGetFlag(Package
, L
"-r") && ShellCommandLineGetFlag(Package
, L
"-c") )
286 // error for too many parameters
288 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDriver1HiiHandle
);
289 ShellStatus
= SHELL_INVALID_PARAMETER
;
290 } else if (ShellCommandLineGetFlag(Package
, L
"-c")) {
292 // do the conin and conout from EFI variables
293 // if the first fails dont 'loose' the error
295 Status
= ConnectFromDevPaths(L
"ConInDev");
296 if (EFI_ERROR(Status
)) {
297 ConnectFromDevPaths(L
"ConOutDev");
299 Status
= ConnectFromDevPaths(L
"ConOutDev");
301 if (EFI_ERROR(Status
)) {
302 ConnectFromDevPaths(L
"ErrOutDev");
304 Status
= ConnectFromDevPaths(L
"ErrOutDev");
306 if (EFI_ERROR(Status
)) {
307 ConnectFromDevPaths(L
"ErrOut");
309 Status
= ConnectFromDevPaths(L
"ErrOut");
311 if (EFI_ERROR(Status
)) {
312 ConnectFromDevPaths(L
"ConIn");
314 Status
= ConnectFromDevPaths(L
"ConIn");
316 if (EFI_ERROR(Status
)) {
317 ConnectFromDevPaths(L
"ConOut");
319 Status
= ConnectFromDevPaths(L
"ConOut");
321 if (EFI_ERROR(Status
)) {
322 ShellStatus
= SHELL_DEVICE_ERROR
;
326 // 0, 1, or 2 specific handles and possibly recursive
328 Param1
= ShellCommandLineGetRawValue(Package
, 1);
329 Param2
= ShellCommandLineGetRawValue(Package
, 2);
330 Count
= ShellCommandLineGetCount(Package
);
332 if (Param1
!= NULL
) {
333 Status
= ShellConvertStringToUint64(Param1
, &Intermediate
, TRUE
, FALSE
);
334 Handle1
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
335 if (EFI_ERROR(Status
)) {
336 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param1
);
337 ShellStatus
= SHELL_INVALID_PARAMETER
;
343 if (Param2
!= NULL
) {
344 Status
= ShellConvertStringToUint64(Param2
, &Intermediate
, TRUE
, FALSE
);
345 Handle2
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
346 if (EFI_ERROR(Status
)) {
347 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
348 ShellStatus
= SHELL_INVALID_PARAMETER
;
354 if (ShellStatus
== SHELL_SUCCESS
) {
355 if (Param1
!= NULL
&& Handle1
== NULL
){
356 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param1
);
357 ShellStatus
= SHELL_INVALID_PARAMETER
;
358 } else if (Param2
!= NULL
&& Handle2
== NULL
) {
359 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
360 ShellStatus
= SHELL_INVALID_PARAMETER
;
361 } else if (Handle2
!= NULL
&& Handle1
!= NULL
&& EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
362 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
363 ShellStatus
= SHELL_INVALID_PARAMETER
;
365 Status
= ConvertAndConnectControllers(Handle1
, Handle2
, ShellCommandLineGetFlag(Package
, L
"-r"), (BOOLEAN
)(Count
!=0));
366 if (EFI_ERROR(Status
)) {
367 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CONNECT_NONE
), gShellDriver1HiiHandle
);
368 ShellStatus
= SHELL_DEVICE_ERROR
;
374 ShellCommandLineFreeVarList (Package
);
376 return (ShellStatus
);