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
;
38 if (DevicePathToConnect
== NULL
) {
39 return EFI_INVALID_PARAMETER
;
43 RemainingDevicePath
= DevicePathToConnect
;
44 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &Handle
);
45 if (!EFI_ERROR (Status
) && (Handle
!= NULL
)) {
46 Status
= gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
48 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
));
55 Connect drivers for PCI root bridge.
57 @retval EFI_SUCCESS Connect drivers successfully.
58 @retval EFI_NOT_FOUND Cannot find PCI root bridge device.
62 ShellConnectPciRootBridge (
66 UINTN RootBridgeHandleCount
;
67 EFI_HANDLE
*RootBridgeHandleBuffer
;
68 UINTN RootBridgeIndex
;
71 RootBridgeHandleCount
= 0;
73 Status
= gBS
->LocateHandleBuffer (
75 &gEfiPciRootBridgeIoProtocolGuid
,
77 &RootBridgeHandleCount
,
78 &RootBridgeHandleBuffer
80 if (EFI_ERROR (Status
)) {
84 for (RootBridgeIndex
= 0; RootBridgeIndex
< RootBridgeHandleCount
; RootBridgeIndex
++) {
85 gBS
->ConnectController (RootBridgeHandleBuffer
[RootBridgeIndex
], NULL
, NULL
, FALSE
);
93 Connect controller(s) and driver(s).
95 @param[in] ControllerHandle The handle to the controller. Should have driver binding on it.
96 @param[in] DriverHandle The handle to the driver. Should have driver binding.
97 @param[in] Recursive TRUE to connect recursively, FALSE otherwise.
98 @param[in] Output TRUE to have info on the screen, FALSE otherwise.
99 @param[in] AlwaysOutput Override Output for errors.
101 @retval EFI_SUCCESS The operation was successful.
106 IN CONST EFI_HANDLE ControllerHandle OPTIONAL
,
107 IN CONST EFI_HANDLE DriverHandle OPTIONAL
,
108 IN CONST BOOLEAN Recursive
,
109 IN CONST BOOLEAN Output
,
110 IN CONST BOOLEAN AlwaysOutput
115 EFI_HANDLE
*ControllerHandleList
;
116 EFI_HANDLE
*DriverHandleList
;
117 EFI_HANDLE
*HandleWalker
;
119 ControllerHandleList
= NULL
;
120 Status
= EFI_NOT_FOUND
;
121 Status2
= EFI_NOT_FOUND
;
124 // If we have a single handle to connect make that a 'list'
126 if (DriverHandle
== NULL
) {
127 DriverHandleList
= NULL
;
129 DriverHandleList
= AllocateZeroPool(2*sizeof(EFI_HANDLE
));
130 if (DriverHandleList
== NULL
) {
131 return (EFI_OUT_OF_RESOURCES
);
133 DriverHandleList
[0] = DriverHandle
;
134 DriverHandleList
[1] = NULL
;
138 // do we connect all controllers (with a loop) or a single one...
139 // This is where we call the gBS->ConnectController function.
141 if (ControllerHandle
== NULL
) {
142 ControllerHandleList
= GetHandleListByProtocol(&gEfiDevicePathProtocolGuid
);
143 for (HandleWalker
= ControllerHandleList
144 ; HandleWalker
!= NULL
&& *HandleWalker
!= NULL
147 Status
= gBS
->ConnectController(*HandleWalker
, DriverHandleList
, NULL
, Recursive
);
148 if (!EFI_ERROR(Status
)) {
149 Status2
= EFI_SUCCESS
;
151 if ((Output
&& !EFI_ERROR(Status
)) || AlwaysOutput
) {
152 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_HANDLE_RESULT
), gShellDriver1HiiHandle
, L
"Connect", ConvertHandleToHandleIndex(*HandleWalker
), Status
);
156 Status
= gBS
->ConnectController(ControllerHandle
, DriverHandleList
, NULL
, Recursive
);
157 if (!EFI_ERROR(Status
)) {
158 Status2
= EFI_SUCCESS
;
160 if ((Output
&& !EFI_ERROR(Status
)) || AlwaysOutput
) {
161 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_HANDLE_RESULT
), gShellDriver1HiiHandle
, L
"Connect", ConvertHandleToHandleIndex(ControllerHandle
), Status
);
166 // Free any memory we allocated.
168 if (ControllerHandleList
!= NULL
) {
169 FreePool(ControllerHandleList
);
171 if (DriverHandleList
!= NULL
) {
172 FreePool(DriverHandleList
);
178 Do a connect from an EFI variable via it's key name.
180 @param[in] Key The name of the EFI Variable.
182 @retval EFI_SUCCESS The operation was successful.
186 ConnectFromDevPaths (
190 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
191 EFI_DEVICE_PATH_PROTOCOL
*DevPathWalker
;
192 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
195 UINTN HandleArrayCount
;
196 EFI_HANDLE
*HandleArray
;
198 BOOLEAN AtLeastOneConnected
;
199 EFI_PCI_IO_PROTOCOL
*PciIo
;
204 AtLeastOneConnected
= FALSE
;
207 // Get the DevicePath buffer from the variable...
209 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
210 if (Status
== EFI_BUFFER_TOO_SMALL
) {
211 DevPath
= AllocateZeroPool(Length
);
212 if (DevPath
== NULL
) {
213 return EFI_OUT_OF_RESOURCES
;
215 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
218 Status
= EFI_NOT_FOUND
;
220 // walk the list of devices and connect them
222 for (DevPathWalker
= DevPath
223 ; DevPathWalker
< (DevPath
+ Length
) && EFI_ERROR(Status
) && DevPath
!= NULL
224 ; DevPathWalker
+= GetDevicePathSize(DevPathWalker
)
227 // connect short form device path
229 if ((DevicePathType (DevPathWalker
) == MESSAGING_DEVICE_PATH
) &&
230 ((DevicePathSubType (DevPathWalker
) == MSG_USB_CLASS_DP
)
231 || (DevicePathSubType (DevPathWalker
) == MSG_USB_WWID_DP
)
233 Instance
= DuplicateDevicePath (DevPathWalker
);
234 if (Instance
== NULL
) {
236 return EFI_OUT_OF_RESOURCES
;
239 Status
= ShellConnectPciRootBridge ();
240 if (EFI_ERROR(Status
)) {
245 Status
= gBS
->LocateHandleBuffer (
247 &gEfiPciIoProtocolGuid
,
253 if (!EFI_ERROR (Status
)) {
254 for (Index
= 0; Index
< HandleArrayCount
; Index
++) {
255 Status
= gBS
->HandleProtocol (
257 &gEfiPciIoProtocolGuid
,
261 if (!EFI_ERROR (Status
)) {
262 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x09, 3, &Class
);
263 if (!EFI_ERROR (Status
)) {
264 if ((PCI_CLASS_SERIAL
== Class
[2]) &&
265 (PCI_CLASS_SERIAL_USB
== Class
[1])) {
266 Status
= gBS
->ConnectController (
272 if (!EFI_ERROR(Status
)) {
273 AtLeastOneConnected
= TRUE
;
281 if (HandleArray
!= NULL
) {
282 FreePool (HandleArray
);
287 // connect the entire device path
289 Instance
= DuplicateDevicePath (DevPathWalker
);
290 if (Instance
== NULL
) {
292 return EFI_OUT_OF_RESOURCES
;
295 Status
= ShellConnectDevicePath (Instance
);
296 if (!EFI_ERROR (Status
)) {
297 AtLeastOneConnected
= TRUE
;
305 if (DevPath
!= NULL
) {
309 if (AtLeastOneConnected
) {
312 return EFI_NOT_FOUND
;
318 Convert the handle identifiers from strings and then connect them.
320 One of them should have driver binding and either can be NULL.
322 @param[in] Handle1 The first handle.
323 @param[in] Handle2 The second handle.
324 @param[in] Recursive TRUE to do connect recursively. FALSE otherwise.
325 @param[in] Output TRUE to have output to screen. FALSE otherwise.
327 @retval EFI_SUCCESS The operation was successful.
331 ConvertAndConnectControllers (
332 IN EFI_HANDLE
*Handle1 OPTIONAL
,
333 IN EFI_HANDLE
*Handle2 OPTIONAL
,
334 IN CONST BOOLEAN Recursive
,
335 IN CONST BOOLEAN Output
339 // if only one is NULL verify it's the proper one...
341 if ( (Handle1
== NULL
&& Handle2
!= NULL
)
342 || (Handle1
!= NULL
&& Handle2
== NULL
)
345 // Figure out which one should be NULL and move the handle to the right place.
346 // If Handle1 is NULL then test Handle2 and vise versa.
347 // The one that DOES has driver binding must be Handle2
349 if (Handle1
== NULL
) {
350 if (EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
358 if (EFI_ERROR(gBS
->OpenProtocol(Handle1
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
368 return (ConnectControllers(Handle1
, Handle2
, Recursive
, Output
, (BOOLEAN
)(Handle2
!= NULL
&& Handle1
!= NULL
)));
371 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
378 Function for 'connect' command.
380 @param[in] ImageHandle Handle to the Image (NULL if Internal).
381 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
385 ShellCommandRunConnect (
386 IN EFI_HANDLE ImageHandle
,
387 IN EFI_SYSTEM_TABLE
*SystemTable
392 CHAR16
*ProblemParam
;
393 SHELL_STATUS ShellStatus
;
394 CONST CHAR16
*Param1
;
395 CONST CHAR16
*Param2
;
401 ShellStatus
= SHELL_SUCCESS
;
404 // initialize the shell lib (we must be in non-auto-init...)
406 Status
= ShellInitialize();
407 ASSERT_EFI_ERROR(Status
);
409 Status
= CommandInit();
410 ASSERT_EFI_ERROR(Status
);
413 // parse the command line
415 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
416 if (EFI_ERROR(Status
)) {
417 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
418 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDriver1HiiHandle
, ProblemParam
);
419 FreePool(ProblemParam
);
420 ShellStatus
= SHELL_INVALID_PARAMETER
;
426 // if more than 2 'value' parameters (plus the name one) or either -r or -c with any value parameters we have too many parameters
428 Count
= (gInReconnect
?0x4:0x3);
429 if ((ShellCommandLineGetCount(Package
) > Count
)
430 ||((ShellCommandLineGetFlag(Package
, L
"-r") || ShellCommandLineGetFlag(Package
, L
"-c")) && ShellCommandLineGetCount(Package
)>1)
431 ||(ShellCommandLineGetFlag(Package
, L
"-r") && ShellCommandLineGetFlag(Package
, L
"-c") )
434 // error for too many parameters
436 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDriver1HiiHandle
);
437 ShellStatus
= SHELL_INVALID_PARAMETER
;
438 } else if (ShellCommandLineGetFlag(Package
, L
"-c")) {
440 // do the conin and conout from EFI variables
441 // if the first fails dont 'loose' the error
443 Status
= ConnectFromDevPaths(L
"ConInDev");
444 if (EFI_ERROR(Status
)) {
445 ConnectFromDevPaths(L
"ConOutDev");
447 Status
= ConnectFromDevPaths(L
"ConOutDev");
449 if (EFI_ERROR(Status
)) {
450 ConnectFromDevPaths(L
"ErrOutDev");
452 Status
= ConnectFromDevPaths(L
"ErrOutDev");
454 if (EFI_ERROR(Status
)) {
455 ConnectFromDevPaths(L
"ErrOut");
457 Status
= ConnectFromDevPaths(L
"ErrOut");
459 if (EFI_ERROR(Status
)) {
460 ConnectFromDevPaths(L
"ConIn");
462 Status
= ConnectFromDevPaths(L
"ConIn");
464 if (EFI_ERROR(Status
)) {
465 ConnectFromDevPaths(L
"ConOut");
467 Status
= ConnectFromDevPaths(L
"ConOut");
469 if (EFI_ERROR(Status
)) {
470 ShellStatus
= SHELL_DEVICE_ERROR
;
474 // 0, 1, or 2 specific handles and possibly recursive
476 Param1
= ShellCommandLineGetRawValue(Package
, 1);
477 Param2
= ShellCommandLineGetRawValue(Package
, 2);
478 Count
= ShellCommandLineGetCount(Package
);
480 if (Param1
!= NULL
) {
481 Status
= ShellConvertStringToUint64(Param1
, &Intermediate
, TRUE
, FALSE
);
482 Handle1
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
483 if (EFI_ERROR(Status
)) {
484 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param1
);
485 ShellStatus
= SHELL_INVALID_PARAMETER
;
491 if (Param2
!= NULL
) {
492 Status
= ShellConvertStringToUint64(Param2
, &Intermediate
, TRUE
, FALSE
);
493 Handle2
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
494 if (EFI_ERROR(Status
)) {
495 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
496 ShellStatus
= SHELL_INVALID_PARAMETER
;
502 if (ShellStatus
== SHELL_SUCCESS
) {
503 if (Param1
!= NULL
&& Handle1
== NULL
){
504 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param1
);
505 ShellStatus
= SHELL_INVALID_PARAMETER
;
506 } else if (Param2
!= NULL
&& Handle2
== NULL
) {
507 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
508 ShellStatus
= SHELL_INVALID_PARAMETER
;
509 } else if (Handle2
!= NULL
&& Handle1
!= NULL
&& EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
510 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
511 ShellStatus
= SHELL_INVALID_PARAMETER
;
513 Status
= ConvertAndConnectControllers(Handle1
, Handle2
, ShellCommandLineGetFlag(Package
, L
"-r"), (BOOLEAN
)(Count
!=0));
514 if (EFI_ERROR(Status
)) {
515 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CONNECT_NONE
), gShellDriver1HiiHandle
);
516 ShellStatus
= SHELL_DEVICE_ERROR
;
522 ShellCommandLineFreeVarList (Package
);
524 return (ShellStatus
);