2 Main file for connect shell Driver1 function.
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
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"
16 #include <Guid/GlobalVariable.h>
17 #include <Guid/ConsoleInDevice.h>
18 #include <Guid/ConsoleOutDevice.h>
23 IN CONST EFI_HANDLE ControllerHandle
,
24 IN CONST EFI_HANDLE DriverHandle
,
25 IN CONST BOOLEAN Recursive
,
26 IN CONST BOOLEAN Output
29 EFI_HANDLE
*ControllerHandleList
;
30 EFI_HANDLE
*DriverHandleList
;
31 EFI_HANDLE
*HandleWalker
;
33 ControllerHandleList
= NULL
;
34 Status
= EFI_NOT_FOUND
;
37 // If we have a single handle to connect make that a 'list'
39 if (DriverHandle
== NULL
) {
40 DriverHandleList
= NULL
;
42 DriverHandleList
= AllocatePool(2*sizeof(EFI_HANDLE
));
43 DriverHandleList
[0] = DriverHandle
;
44 DriverHandleList
[1] = NULL
;
48 // do we connect all controllers (with a loop) or a single one...
49 // This is where we call the gBS->ConnectController function.
51 if (ControllerHandle
== NULL
) {
52 ControllerHandleList
= GetHandleListByPotocol(&gEfiDevicePathProtocolGuid
);
53 for (HandleWalker
= ControllerHandleList
54 ; HandleWalker
!= NULL
&& *HandleWalker
!= NULL
57 Status
= gBS
->ConnectController(*HandleWalker
, DriverHandleList
, NULL
, Recursive
);
59 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_CON_RESULT
), gShellDriver1HiiHandle
, *HandleWalker
, Status
);
63 Status
= gBS
->ConnectController(ControllerHandle
, DriverHandleList
, NULL
, Recursive
);
64 ASSERT(Output
== FALSE
);
68 // Free any memory we allocated.
70 if (ControllerHandleList
!= NULL
) {
71 FreePool(ControllerHandleList
);
73 if (DriverHandleList
!= NULL
) {
74 FreePool(DriverHandleList
);
84 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
85 EFI_DEVICE_PATH_PROTOCOL
*DevPathWalker
;
94 // Get the DevicePath buffer from the variable...
96 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
97 if (Status
== EFI_BUFFER_TOO_SMALL
) {
98 DevPath
= AllocatePool(Length
);
99 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
103 // walk the list of devices and connect them
105 for (DevPathWalker
= DevPath
106 ; DevPathWalker
< (DevPath
+ Length
) && !EFI_ERROR(Status
) && DevPath
!= NULL
107 ; DevPathWalker
+= GetDevicePathSize(DevPathWalker
)
110 // get the correct handle from a given device path
112 if (StrCmp(Key
, L
"ConInDev") == 0) {
113 Status
= gBS
->LocateDevicePath((EFI_GUID
*)&gEfiConsoleInDeviceGuid
, &DevPathWalker
, &Handle
);
114 } else if (StrCmp(Key
, L
"ConOutDev") == 0) {
115 Status
= gBS
->LocateDevicePath((EFI_GUID
*)&gEfiConsoleOutDeviceGuid
, &DevPathWalker
, &Handle
);
118 Status
= EFI_INVALID_PARAMETER
;
121 if (!EFI_ERROR(Status
)) {
122 Status
= ConnectControllers(Handle
, NULL
, FALSE
, FALSE
);
126 if (DevPath
!= NULL
) {
134 ConvertAndConnectControllers (
135 IN CONST CHAR16
*StringHandle1
,
136 IN CONST CHAR16
*StringHandle2 OPTIONAL
,
137 IN CONST BOOLEAN Recursive
,
138 IN CONST BOOLEAN Output
144 // Convert the command line parameters to HANDLES. They must be in HEX according to spec.
146 if (StringHandle1
!= NULL
) {
147 Handle1
= (EFI_HANDLE
)StrHexToUintn(StringHandle1
);
151 if (StringHandle2
!= NULL
) {
152 Handle2
= (EFI_HANDLE
)StrHexToUintn(StringHandle2
);
158 // if only one is NULL verify it's the proper one...
160 if ( (Handle1
== NULL
&& Handle2
!= NULL
)
161 || (Handle1
!= NULL
&& Handle2
== NULL
)
164 // Figure out which one should be NULL and move the handle to the right place.
165 // If Handle1 is NULL then test Handle2 and vise versa.
166 // The one that DOES has driver binding must be Handle2
168 if (Handle1
== NULL
) {
169 if (EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
177 if (EFI_ERROR(gBS
->OpenProtocol(Handle1
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
187 return (ConnectControllers(Handle1
, Handle2
, Recursive
, Output
));
190 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
198 ShellCommandRunConnect (
203 CHAR16
*ProblemParam
;
204 SHELL_STATUS ShellStatus
;
206 ShellStatus
= SHELL_SUCCESS
;
209 // initialize the shell lib (we must be in non-auto-init...)
211 Status
= ShellInitialize();
212 ASSERT_EFI_ERROR(Status
);
214 Status
= CommandInit();
215 ASSERT_EFI_ERROR(Status
);
218 // parse the command line
220 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
221 if EFI_ERROR(Status
) {
222 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
223 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDriver1HiiHandle
, ProblemParam
);
224 FreePool(ProblemParam
);
225 ShellStatus
= SHELL_INVALID_PARAMETER
;
231 // if more than 2 'value' parameters (plus the name one) or either -r or -c with any value parameters we have too many parameters
233 if ((ShellCommandLineGetCount() > 3)
234 ||((ShellCommandLineGetFlag(Package
, L
"-r") != FALSE
|| ShellCommandLineGetFlag(Package
, L
"-c") != FALSE
) && ShellCommandLineGetCount()!=0)
237 // error for too many parameters
239 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDriver1HiiHandle
);
240 ShellStatus
= SHELL_INVALID_PARAMETER
;
241 } else if (ShellCommandLineGetFlag(Package
, L
"-c") != FALSE
) {
243 // do the conin and conout from EFI variables
244 // if the first fails dont 'loose' the error
246 Status
= ConnectFromDevPaths(L
"ConInDev");
247 if (EFI_ERROR(Status
)) {
248 ConnectFromDevPaths(L
"ConOutDev");
250 Status
= ConnectFromDevPaths(L
"ConOutDev");
252 ShellStatus
= Status
& (~MAX_BIT
);
255 // 0, 1, or 2 specific handles and possibly recursive
257 if (ShellCommandLineGetRawValue(Package
, 1) != NULL
&& CommandLibGetHandleValue(StrHexToUintn(ShellCommandLineGetRawValue(Package
, 1))) == NULL
){
258 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, ShellCommandLineGetRawValue(Package
, 1));
259 ShellStatus
= SHELL_INVALID_PARAMETER
;
260 } else if (ShellCommandLineGetRawValue(Package
, 2) != NULL
&& CommandLibGetHandleValue(StrHexToUintn(ShellCommandLineGetRawValue(Package
, 2))) == NULL
) {
261 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, ShellCommandLineGetRawValue(Package
, 2));
262 ShellStatus
= SHELL_INVALID_PARAMETER
;
264 Status
= ConvertAndConnectControllers(ShellCommandLineGetRawValue(Package
, 1), ShellCommandLineGetRawValue(Package
, 2), ShellCommandLineGetFlag(Package
, L
"-r"), (BOOLEAN
)(ShellCommandLineGetCount()!=0));
265 ShellStatus
= Status
& (~MAX_BIT
);
269 ShellCommandLineFreeVarList (Package
);
271 return (ShellStatus
);