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"
22 IN CONST EFI_HANDLE ControllerHandle OPTIONAL
,
23 IN CONST EFI_HANDLE DriverHandle OPTIONAL
,
24 IN CONST BOOLEAN Recursive
,
25 IN CONST BOOLEAN Output
,
26 IN CONST BOOLEAN AlwaysOutput
31 EFI_HANDLE
*ControllerHandleList
;
32 EFI_HANDLE
*DriverHandleList
;
33 EFI_HANDLE
*HandleWalker
;
35 ControllerHandleList
= NULL
;
36 Status
= EFI_NOT_FOUND
;
37 Status2
= EFI_NOT_FOUND
;
40 // If we have a single handle to connect make that a 'list'
42 if (DriverHandle
== NULL
) {
43 DriverHandleList
= NULL
;
45 DriverHandleList
= AllocatePool(2*sizeof(EFI_HANDLE
));
46 if (DriverHandleList
== NULL
) {
47 return (EFI_OUT_OF_RESOURCES
);
49 DriverHandleList
[0] = DriverHandle
;
50 DriverHandleList
[1] = NULL
;
54 // do we connect all controllers (with a loop) or a single one...
55 // This is where we call the gBS->ConnectController function.
57 if (ControllerHandle
== NULL
) {
58 ControllerHandleList
= GetHandleListByProtocol(&gEfiDevicePathProtocolGuid
);
59 for (HandleWalker
= ControllerHandleList
60 ; HandleWalker
!= NULL
&& *HandleWalker
!= NULL
63 Status
= gBS
->ConnectController(*HandleWalker
, DriverHandleList
, NULL
, Recursive
);
64 if (!EFI_ERROR(Status
)) {
65 Status2
= EFI_SUCCESS
;
67 if ((Output
&& !EFI_ERROR(Status
)) || AlwaysOutput
) {
68 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_HANDLE_RESULT
), gShellDriver1HiiHandle
, L
"Connect", ConvertHandleToHandleIndex(*HandleWalker
), Status
);
72 Status
= gBS
->ConnectController(ControllerHandle
, 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(ControllerHandle
), Status
);
82 // Free any memory we allocated.
84 if (ControllerHandleList
!= NULL
) {
85 FreePool(ControllerHandleList
);
87 if (DriverHandleList
!= NULL
) {
88 FreePool(DriverHandleList
);
99 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
100 EFI_DEVICE_PATH_PROTOCOL
*DevPathWalker
;
109 // Get the DevicePath buffer from the variable...
111 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
112 if (Status
== EFI_BUFFER_TOO_SMALL
) {
113 DevPath
= AllocatePool(Length
);
114 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
117 Status
= EFI_NOT_FOUND
;
119 // walk the list of devices and connect them
121 for (DevPathWalker
= DevPath
122 ; DevPathWalker
< (DevPath
+ Length
) && EFI_ERROR(Status
) && DevPath
!= NULL
123 ; DevPathWalker
+= GetDevicePathSize(DevPathWalker
)
126 // get the correct handle from a given device path
128 if ((StrCmp(Key
, L
"ConInDev") == 0)
129 ||(StrCmp(Key
, L
"ConIn") == 0)
131 Status
= gBS
->LocateDevicePath((EFI_GUID
*)&gEfiConsoleInDeviceGuid
, &DevPathWalker
, &Handle
);
132 if (!EFI_ERROR(Status
)) {
133 Status
= ConnectControllers(NULL
, Handle
, FALSE
, TRUE
, FALSE
);
135 } else if ((StrCmp(Key
, L
"ConOutDev") == 0)
136 || (StrCmp(Key
, L
"ConErrDev") == 0)
137 || (StrCmp(Key
, L
"ConOut") == 0)
138 || (StrCmp(Key
, L
"ConErr") == 0)
140 Status
= gBS
->LocateDevicePath((EFI_GUID
*)&gEfiConsoleOutDeviceGuid
, &DevPathWalker
, &Handle
);
141 if (!EFI_ERROR(Status
)) {
142 Status
= ConnectControllers(NULL
, Handle
, FALSE
, TRUE
, FALSE
);
147 if (DevPath
!= NULL
) {
155 ConvertAndConnectControllers (
156 IN CONST CHAR16
*StringHandle1 OPTIONAL
,
157 IN CONST CHAR16
*StringHandle2 OPTIONAL
,
158 IN CONST BOOLEAN Recursive
,
159 IN CONST BOOLEAN Output
166 // Convert the command line parameters to HANDLES. They must be in HEX according to spec.
168 if (StringHandle1
!= NULL
) {
169 Handle1
= ConvertHandleIndexToHandle(StrHexToUintn(StringHandle1
));
173 if (StringHandle2
!= NULL
) {
174 Handle2
= ConvertHandleIndexToHandle(StrHexToUintn(StringHandle2
));
180 // if only one is NULL verify it's the proper one...
182 if ( (Handle1
== NULL
&& Handle2
!= NULL
)
183 || (Handle1
!= NULL
&& Handle2
== NULL
)
186 // Figure out which one should be NULL and move the handle to the right place.
187 // If Handle1 is NULL then test Handle2 and vise versa.
188 // The one that DOES has driver binding must be Handle2
190 if (Handle1
== NULL
) {
191 if (EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
199 if (EFI_ERROR(gBS
->OpenProtocol(Handle1
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
209 return (ConnectControllers(Handle1
, Handle2
, Recursive
, Output
, FALSE
));
212 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
219 Function for 'connect' command.
221 @param[in] ImageHandle Handle to the Image (NULL if Internal).
222 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
226 ShellCommandRunConnect (
227 IN EFI_HANDLE ImageHandle
,
228 IN EFI_SYSTEM_TABLE
*SystemTable
233 CHAR16
*ProblemParam
;
234 SHELL_STATUS ShellStatus
;
235 CONST CHAR16
*Param1
;
236 CONST CHAR16
*Param2
;
239 ShellStatus
= SHELL_SUCCESS
;
242 // initialize the shell lib (we must be in non-auto-init...)
244 Status
= ShellInitialize();
245 ASSERT_EFI_ERROR(Status
);
247 Status
= CommandInit();
248 ASSERT_EFI_ERROR(Status
);
251 // parse the command line
253 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
254 if (EFI_ERROR(Status
)) {
255 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
256 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDriver1HiiHandle
, ProblemParam
);
257 FreePool(ProblemParam
);
258 ShellStatus
= SHELL_INVALID_PARAMETER
;
264 // if more than 2 'value' parameters (plus the name one) or either -r or -c with any value parameters we have too many parameters
266 if ((ShellCommandLineGetCount(Package
) > 3)
267 ||((ShellCommandLineGetFlag(Package
, L
"-r") || ShellCommandLineGetFlag(Package
, L
"-c")) && ShellCommandLineGetCount(Package
)>1)
268 ||(ShellCommandLineGetFlag(Package
, L
"-r") && ShellCommandLineGetFlag(Package
, L
"-c") )
271 // error for too many parameters
273 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDriver1HiiHandle
);
274 ShellStatus
= SHELL_INVALID_PARAMETER
;
275 } else if (ShellCommandLineGetFlag(Package
, L
"-c")) {
277 // do the conin and conout from EFI variables
278 // if the first fails dont 'loose' the error
280 Status
= ConnectFromDevPaths(L
"ConInDev");
281 if (EFI_ERROR(Status
)) {
282 ConnectFromDevPaths(L
"ConOutDev");
284 Status
= ConnectFromDevPaths(L
"ConOutDev");
286 if (EFI_ERROR(Status
)) {
287 ConnectFromDevPaths(L
"ConErrDev");
289 Status
= ConnectFromDevPaths(L
"ConErrDev");
291 if (EFI_ERROR(Status
)) {
292 ConnectFromDevPaths(L
"ConErr");
294 Status
= ConnectFromDevPaths(L
"ConErr");
296 if (EFI_ERROR(Status
)) {
297 ConnectFromDevPaths(L
"ConIn");
299 Status
= ConnectFromDevPaths(L
"ConIn");
301 if (EFI_ERROR(Status
)) {
302 ConnectFromDevPaths(L
"ConOut");
304 Status
= ConnectFromDevPaths(L
"ConOut");
306 if (EFI_ERROR(Status
)) {
307 ShellStatus
= SHELL_DEVICE_ERROR
;
311 // 0, 1, or 2 specific handles and possibly recursive
313 Param1
= ShellCommandLineGetRawValue(Package
, 1);
314 Param2
= ShellCommandLineGetRawValue(Package
, 2);
315 Count
= ShellCommandLineGetCount(Package
);
316 if (Param1
!= NULL
&& ConvertHandleIndexToHandle(StrHexToUintn(Param1
)) == NULL
){
317 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param1
);
318 ShellStatus
= SHELL_INVALID_PARAMETER
;
319 } else if (Param2
!= NULL
&& ConvertHandleIndexToHandle(StrHexToUintn(Param2
)) == NULL
) {
320 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
321 ShellStatus
= SHELL_INVALID_PARAMETER
;
323 Status
= ConvertAndConnectControllers(Param1
, Param2
, ShellCommandLineGetFlag(Package
, L
"-r"), (BOOLEAN
)(Count
!=0));
324 if (EFI_ERROR(Status
)) {
325 ShellStatus
= SHELL_DEVICE_ERROR
;
330 ShellCommandLineFreeVarList (Package
);
332 return (ShellStatus
);