2 Main file for connect shell Driver1 function.
4 Copyright (c) 2010 - 2014, 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 Create all handles associate with every device path node.
20 @param DevicePathToConnect The device path which will be connected.
22 @retval EFI_SUCCESS All handles associate with every device path node
24 @retval EFI_INVALID_PARAMETER DevicePathToConnect is NULL.
25 @retval EFI_NOT_FOUND Create the handle associate with one device path
30 ShellConnectDevicePath (
31 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathToConnect
34 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
37 EFI_HANDLE PreviousHandle
;
39 if (DevicePathToConnect
== NULL
) {
40 return EFI_INVALID_PARAMETER
;
43 PreviousHandle
= NULL
;
45 RemainingDevicePath
= DevicePathToConnect
;
46 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &Handle
);
48 if (!EFI_ERROR (Status
) && (Handle
!= NULL
)) {
49 if (PreviousHandle
== Handle
) {
50 Status
= EFI_NOT_FOUND
;
52 PreviousHandle
= Handle
;
53 Status
= gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
57 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
) );
64 Connect drivers for PCI root bridge.
66 @retval EFI_SUCCESS Connect drivers successfully.
67 @retval EFI_NOT_FOUND Cannot find PCI root bridge device.
71 ShellConnectPciRootBridge (
75 UINTN RootBridgeHandleCount
;
76 EFI_HANDLE
*RootBridgeHandleBuffer
;
77 UINTN RootBridgeIndex
;
80 RootBridgeHandleCount
= 0;
82 Status
= gBS
->LocateHandleBuffer (
84 &gEfiPciRootBridgeIoProtocolGuid
,
86 &RootBridgeHandleCount
,
87 &RootBridgeHandleBuffer
89 if (EFI_ERROR (Status
)) {
93 for (RootBridgeIndex
= 0; RootBridgeIndex
< RootBridgeHandleCount
; RootBridgeIndex
++) {
94 gBS
->ConnectController (RootBridgeHandleBuffer
[RootBridgeIndex
], NULL
, NULL
, FALSE
);
97 FreePool (RootBridgeHandleBuffer
);
104 Connect controller(s) and driver(s).
106 @param[in] ControllerHandle The handle to the controller. Should have driver binding on it.
107 @param[in] DriverHandle The handle to the driver. Should have driver binding.
108 @param[in] Recursive TRUE to connect recursively, FALSE otherwise.
109 @param[in] Output TRUE to have info on the screen, FALSE otherwise.
110 @param[in] AlwaysOutput Override Output for errors.
112 @retval EFI_SUCCESS The operation was successful.
117 IN CONST EFI_HANDLE ControllerHandle OPTIONAL
,
118 IN CONST EFI_HANDLE DriverHandle OPTIONAL
,
119 IN CONST BOOLEAN Recursive
,
120 IN CONST BOOLEAN Output
,
121 IN CONST BOOLEAN AlwaysOutput
126 EFI_HANDLE
*ControllerHandleList
;
127 EFI_HANDLE
*DriverHandleList
;
128 EFI_HANDLE
*HandleWalker
;
130 ControllerHandleList
= NULL
;
131 Status
= EFI_NOT_FOUND
;
132 Status2
= EFI_NOT_FOUND
;
135 // If we have a single handle to connect make that a 'list'
137 if (DriverHandle
== NULL
) {
138 DriverHandleList
= NULL
;
140 DriverHandleList
= AllocateZeroPool(2*sizeof(EFI_HANDLE
));
141 if (DriverHandleList
== NULL
) {
142 return (EFI_OUT_OF_RESOURCES
);
144 DriverHandleList
[0] = DriverHandle
;
145 DriverHandleList
[1] = NULL
;
149 // do we connect all controllers (with a loop) or a single one...
150 // This is where we call the gBS->ConnectController function.
152 if (ControllerHandle
== NULL
) {
153 ControllerHandleList
= GetHandleListByProtocol(&gEfiDevicePathProtocolGuid
);
154 for (HandleWalker
= ControllerHandleList
155 ; HandleWalker
!= NULL
&& *HandleWalker
!= NULL
158 Status
= gBS
->ConnectController(*HandleWalker
, DriverHandleList
, NULL
, Recursive
);
159 if (!EFI_ERROR(Status
)) {
160 Status2
= EFI_SUCCESS
;
162 if ((Output
&& !EFI_ERROR(Status
)) || AlwaysOutput
) {
163 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_HANDLE_RESULT
), gShellDriver1HiiHandle
, L
"Connect", ConvertHandleToHandleIndex(*HandleWalker
), Status
);
167 Status
= gBS
->ConnectController(ControllerHandle
, DriverHandleList
, NULL
, Recursive
);
168 if (!EFI_ERROR(Status
)) {
169 Status2
= EFI_SUCCESS
;
171 if ((Output
&& !EFI_ERROR(Status
)) || AlwaysOutput
) {
172 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_HANDLE_RESULT
), gShellDriver1HiiHandle
, L
"Connect", ConvertHandleToHandleIndex(ControllerHandle
), Status
);
177 // Free any memory we allocated.
179 if (ControllerHandleList
!= NULL
) {
180 FreePool(ControllerHandleList
);
182 if (DriverHandleList
!= NULL
) {
183 FreePool(DriverHandleList
);
189 Do a connect from an EFI variable via it's key name.
191 @param[in] Key The name of the EFI Variable.
193 @retval EFI_SUCCESS The operation was successful.
197 ShellConnectFromDevPaths (
201 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
202 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevPath
;
203 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
204 EFI_DEVICE_PATH_PROTOCOL
*Next
;
207 UINTN HandleArrayCount
;
209 EFI_HANDLE
*HandleArray
;
211 BOOLEAN AtLeastOneConnected
;
212 EFI_PCI_IO_PROTOCOL
*PciIo
;
217 AtLeastOneConnected
= FALSE
;
220 // Get the DevicePath buffer from the variable...
222 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
223 if (Status
== EFI_BUFFER_TOO_SMALL
) {
224 DevPath
= AllocateZeroPool(Length
);
225 if (DevPath
== NULL
) {
226 return EFI_OUT_OF_RESOURCES
;
228 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
229 if (EFI_ERROR (Status
)) {
230 if (DevPath
!= NULL
) {
235 } else if (EFI_ERROR (Status
)) {
239 Status
= EFI_NOT_FOUND
;
241 CopyOfDevPath
= DevPath
;
243 // walk the list of devices and connect them
247 // Check every instance of the console variable
249 Instance
= GetNextDevicePathInstance (&CopyOfDevPath
, &Size
);
250 if (Instance
== NULL
) {
251 if (DevPath
!= NULL
) {
254 return EFI_UNSUPPORTED
;
258 while (!IsDevicePathEndType (Next
)) {
259 Next
= NextDevicePathNode (Next
);
262 SetDevicePathEndNode (Next
);
264 // connect short form device path
266 if ((DevicePathType (Instance
) == MESSAGING_DEVICE_PATH
) &&
267 ((DevicePathSubType (Instance
) == MSG_USB_CLASS_DP
)
268 || (DevicePathSubType (Instance
) == MSG_USB_WWID_DP
)
271 Status
= ShellConnectPciRootBridge ();
272 if (EFI_ERROR(Status
)) {
278 Status
= gBS
->LocateHandleBuffer (
280 &gEfiPciIoProtocolGuid
,
286 if (!EFI_ERROR (Status
)) {
287 for (Index
= 0; Index
< HandleArrayCount
; Index
++) {
288 Status
= gBS
->HandleProtocol (
290 &gEfiPciIoProtocolGuid
,
294 if (!EFI_ERROR (Status
)) {
295 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x09, 3, &Class
);
296 if (!EFI_ERROR (Status
)) {
297 if ((PCI_CLASS_SERIAL
== Class
[2]) &&
298 (PCI_CLASS_SERIAL_USB
== Class
[1])) {
299 Status
= gBS
->ConnectController (
305 if (!EFI_ERROR(Status
)) {
306 AtLeastOneConnected
= TRUE
;
314 if (HandleArray
!= NULL
) {
315 FreePool (HandleArray
);
319 // connect the entire device path
321 Status
= ShellConnectDevicePath (Instance
);
322 if (!EFI_ERROR (Status
)) {
323 AtLeastOneConnected
= TRUE
;
328 } while (CopyOfDevPath
!= NULL
);
330 if (DevPath
!= NULL
) {
334 if (AtLeastOneConnected
) {
337 return EFI_NOT_FOUND
;
343 Convert the handle identifiers from strings and then connect them.
345 One of them should have driver binding and either can be NULL.
347 @param[in] Handle1 The first handle.
348 @param[in] Handle2 The second handle.
349 @param[in] Recursive TRUE to do connect recursively. FALSE otherwise.
350 @param[in] Output TRUE to have output to screen. FALSE otherwise.
352 @retval EFI_SUCCESS The operation was successful.
356 ConvertAndConnectControllers (
357 IN EFI_HANDLE
*Handle1 OPTIONAL
,
358 IN EFI_HANDLE
*Handle2 OPTIONAL
,
359 IN CONST BOOLEAN Recursive
,
360 IN CONST BOOLEAN Output
364 // if only one is NULL verify it's the proper one...
366 if ( (Handle1
== NULL
&& Handle2
!= NULL
)
367 || (Handle1
!= NULL
&& Handle2
== NULL
)
370 // Figure out which one should be NULL and move the handle to the right place.
371 // If Handle1 is NULL then test Handle2 and vise versa.
372 // The one that DOES has driver binding must be Handle2
374 if (Handle1
== NULL
) {
375 if (EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
383 if (EFI_ERROR(gBS
->OpenProtocol(Handle1
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
393 return (ConnectControllers(Handle1
, Handle2
, Recursive
, Output
, (BOOLEAN
)(Handle2
!= NULL
&& Handle1
!= NULL
)));
396 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
403 Function for 'connect' command.
405 @param[in] ImageHandle Handle to the Image (NULL if Internal).
406 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
410 ShellCommandRunConnect (
411 IN EFI_HANDLE ImageHandle
,
412 IN EFI_SYSTEM_TABLE
*SystemTable
417 CHAR16
*ProblemParam
;
418 SHELL_STATUS ShellStatus
;
419 CONST CHAR16
*Param1
;
420 CONST CHAR16
*Param2
;
426 ShellStatus
= SHELL_SUCCESS
;
428 // initialize the shell lib (we must be in non-auto-init...)
430 Status
= ShellInitialize();
431 ASSERT_EFI_ERROR(Status
);
433 Status
= CommandInit();
434 ASSERT_EFI_ERROR(Status
);
437 // parse the command line
439 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
440 if (EFI_ERROR(Status
)) {
441 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
442 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDriver1HiiHandle
, ProblemParam
);
443 FreePool(ProblemParam
);
444 ShellStatus
= SHELL_INVALID_PARAMETER
;
450 // if more than 2 'value' parameters (plus the name one) or either -r or -c with any value parameters we have too many parameters
452 Count
= (gInReconnect
?0x4:0x3);
453 if ((ShellCommandLineGetCount(Package
) > Count
)
454 ||((ShellCommandLineGetFlag(Package
, L
"-r") || ShellCommandLineGetFlag(Package
, L
"-c")) && ShellCommandLineGetCount(Package
)>1)
455 ||(ShellCommandLineGetFlag(Package
, L
"-r") && ShellCommandLineGetFlag(Package
, L
"-c") )
458 // error for too many parameters
460 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDriver1HiiHandle
);
461 ShellStatus
= SHELL_INVALID_PARAMETER
;
462 } else if (ShellCommandLineGetFlag(Package
, L
"-c")) {
464 // do the conin and conout from EFI variables
465 // if the first fails dont 'loose' the error
467 Status
= ShellConnectFromDevPaths(L
"ConInDev");
468 if (EFI_ERROR(Status
)) {
469 ShellConnectFromDevPaths(L
"ConOutDev");
471 Status
= ShellConnectFromDevPaths(L
"ConOutDev");
473 if (EFI_ERROR(Status
)) {
474 ShellConnectFromDevPaths(L
"ErrOutDev");
476 Status
= ShellConnectFromDevPaths(L
"ErrOutDev");
478 if (EFI_ERROR(Status
)) {
479 ShellConnectFromDevPaths(L
"ErrOut");
481 Status
= ShellConnectFromDevPaths(L
"ErrOut");
483 if (EFI_ERROR(Status
)) {
484 ShellConnectFromDevPaths(L
"ConIn");
486 Status
= ShellConnectFromDevPaths(L
"ConIn");
488 if (EFI_ERROR(Status
)) {
489 ShellConnectFromDevPaths(L
"ConOut");
491 Status
= ShellConnectFromDevPaths(L
"ConOut");
493 if (EFI_ERROR(Status
)) {
494 ShellStatus
= SHELL_DEVICE_ERROR
;
498 // 0, 1, or 2 specific handles and possibly recursive
500 Param1
= ShellCommandLineGetRawValue(Package
, 1);
501 Param2
= ShellCommandLineGetRawValue(Package
, 2);
502 Count
= ShellCommandLineGetCount(Package
);
504 if (Param1
!= NULL
) {
505 Status
= ShellConvertStringToUint64(Param1
, &Intermediate
, TRUE
, FALSE
);
506 Handle1
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
507 if (EFI_ERROR(Status
)) {
508 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param1
);
509 ShellStatus
= SHELL_INVALID_PARAMETER
;
515 if (Param2
!= NULL
) {
516 Status
= ShellConvertStringToUint64(Param2
, &Intermediate
, TRUE
, FALSE
);
517 Handle2
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
518 if (EFI_ERROR(Status
)) {
519 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
520 ShellStatus
= SHELL_INVALID_PARAMETER
;
526 if (ShellStatus
== SHELL_SUCCESS
) {
527 if (Param1
!= NULL
&& Handle1
== NULL
){
528 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param1
);
529 ShellStatus
= SHELL_INVALID_PARAMETER
;
530 } else if (Param2
!= NULL
&& Handle2
== NULL
) {
531 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
532 ShellStatus
= SHELL_INVALID_PARAMETER
;
533 } else if (Handle2
!= NULL
&& Handle1
!= NULL
&& EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
534 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
535 ShellStatus
= SHELL_INVALID_PARAMETER
;
537 Status
= ConvertAndConnectControllers(Handle1
, Handle2
, ShellCommandLineGetFlag(Package
, L
"-r"), (BOOLEAN
)(Count
!=0));
538 if (EFI_ERROR(Status
)) {
539 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CONNECT_NONE
), gShellDriver1HiiHandle
);
540 ShellStatus
= SHELL_DEVICE_ERROR
;
546 ShellCommandLineFreeVarList (Package
);
548 return (ShellStatus
);