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
);
102 Connect controller(s) and driver(s).
104 @param[in] ControllerHandle The handle to the controller. Should have driver binding on it.
105 @param[in] DriverHandle The handle to the driver. Should have driver binding.
106 @param[in] Recursive TRUE to connect recursively, FALSE otherwise.
107 @param[in] Output TRUE to have info on the screen, FALSE otherwise.
108 @param[in] AlwaysOutput Override Output for errors.
110 @retval EFI_SUCCESS The operation was successful.
115 IN CONST EFI_HANDLE ControllerHandle OPTIONAL
,
116 IN CONST EFI_HANDLE DriverHandle OPTIONAL
,
117 IN CONST BOOLEAN Recursive
,
118 IN CONST BOOLEAN Output
,
119 IN CONST BOOLEAN AlwaysOutput
124 EFI_HANDLE
*ControllerHandleList
;
125 EFI_HANDLE
*DriverHandleList
;
126 EFI_HANDLE
*HandleWalker
;
128 ControllerHandleList
= NULL
;
129 Status
= EFI_NOT_FOUND
;
130 Status2
= EFI_NOT_FOUND
;
133 // If we have a single handle to connect make that a 'list'
135 if (DriverHandle
== NULL
) {
136 DriverHandleList
= NULL
;
138 DriverHandleList
= AllocateZeroPool(2*sizeof(EFI_HANDLE
));
139 if (DriverHandleList
== NULL
) {
140 return (EFI_OUT_OF_RESOURCES
);
142 DriverHandleList
[0] = DriverHandle
;
143 DriverHandleList
[1] = NULL
;
147 // do we connect all controllers (with a loop) or a single one...
148 // This is where we call the gBS->ConnectController function.
150 if (ControllerHandle
== NULL
) {
151 ControllerHandleList
= GetHandleListByProtocol(&gEfiDevicePathProtocolGuid
);
152 for (HandleWalker
= ControllerHandleList
153 ; HandleWalker
!= NULL
&& *HandleWalker
!= NULL
156 Status
= gBS
->ConnectController(*HandleWalker
, 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(*HandleWalker
), Status
);
165 Status
= gBS
->ConnectController(ControllerHandle
, DriverHandleList
, NULL
, Recursive
);
166 if (!EFI_ERROR(Status
)) {
167 Status2
= EFI_SUCCESS
;
169 if ((Output
&& !EFI_ERROR(Status
)) || AlwaysOutput
) {
170 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_HANDLE_RESULT
), gShellDriver1HiiHandle
, L
"Connect", ConvertHandleToHandleIndex(ControllerHandle
), Status
);
175 // Free any memory we allocated.
177 if (ControllerHandleList
!= NULL
) {
178 FreePool(ControllerHandleList
);
180 if (DriverHandleList
!= NULL
) {
181 FreePool(DriverHandleList
);
187 Do a connect from an EFI variable via it's key name.
189 @param[in] Key The name of the EFI Variable.
191 @retval EFI_SUCCESS The operation was successful.
195 ConnectFromDevPaths (
199 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
200 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevPath
;
201 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
202 EFI_DEVICE_PATH_PROTOCOL
*Next
;
205 UINTN HandleArrayCount
;
207 EFI_HANDLE
*HandleArray
;
209 BOOLEAN AtLeastOneConnected
;
210 EFI_PCI_IO_PROTOCOL
*PciIo
;
215 AtLeastOneConnected
= FALSE
;
218 // Get the DevicePath buffer from the variable...
220 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
221 if (Status
== EFI_BUFFER_TOO_SMALL
) {
222 DevPath
= AllocateZeroPool(Length
);
223 if (DevPath
== NULL
) {
224 return EFI_OUT_OF_RESOURCES
;
226 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
227 if (EFI_ERROR (Status
)) {
228 if (DevPath
!= NULL
) {
233 } else if (EFI_ERROR (Status
)) {
237 Status
= EFI_NOT_FOUND
;
239 CopyOfDevPath
= DevPath
;
241 // walk the list of devices and connect them
245 // Check every instance of the console variable
247 Instance
= GetNextDevicePathInstance (&CopyOfDevPath
, &Size
);
248 if (Instance
== NULL
) {
249 if (DevPath
!= NULL
) {
252 return EFI_UNSUPPORTED
;
256 while (!IsDevicePathEndType (Next
)) {
257 Next
= NextDevicePathNode (Next
);
260 SetDevicePathEndNode (Next
);
262 // connect short form device path
264 if ((DevicePathType (Instance
) == MESSAGING_DEVICE_PATH
) &&
265 ((DevicePathSubType (Instance
) == MSG_USB_CLASS_DP
)
266 || (DevicePathSubType (Instance
) == MSG_USB_WWID_DP
)
269 Status
= ShellConnectPciRootBridge ();
270 if (EFI_ERROR(Status
)) {
276 Status
= gBS
->LocateHandleBuffer (
278 &gEfiPciIoProtocolGuid
,
284 if (!EFI_ERROR (Status
)) {
285 for (Index
= 0; Index
< HandleArrayCount
; Index
++) {
286 Status
= gBS
->HandleProtocol (
288 &gEfiPciIoProtocolGuid
,
292 if (!EFI_ERROR (Status
)) {
293 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x09, 3, &Class
);
294 if (!EFI_ERROR (Status
)) {
295 if ((PCI_CLASS_SERIAL
== Class
[2]) &&
296 (PCI_CLASS_SERIAL_USB
== Class
[1])) {
297 Status
= gBS
->ConnectController (
303 if (!EFI_ERROR(Status
)) {
304 AtLeastOneConnected
= TRUE
;
312 if (HandleArray
!= NULL
) {
313 FreePool (HandleArray
);
317 // connect the entire device path
319 Status
= ShellConnectDevicePath (Instance
);
320 if (!EFI_ERROR (Status
)) {
321 AtLeastOneConnected
= TRUE
;
326 } while (CopyOfDevPath
!= NULL
);
328 if (DevPath
!= NULL
) {
332 if (AtLeastOneConnected
) {
335 return EFI_NOT_FOUND
;
341 Convert the handle identifiers from strings and then connect them.
343 One of them should have driver binding and either can be NULL.
345 @param[in] Handle1 The first handle.
346 @param[in] Handle2 The second handle.
347 @param[in] Recursive TRUE to do connect recursively. FALSE otherwise.
348 @param[in] Output TRUE to have output to screen. FALSE otherwise.
350 @retval EFI_SUCCESS The operation was successful.
354 ConvertAndConnectControllers (
355 IN EFI_HANDLE
*Handle1 OPTIONAL
,
356 IN EFI_HANDLE
*Handle2 OPTIONAL
,
357 IN CONST BOOLEAN Recursive
,
358 IN CONST BOOLEAN Output
362 // if only one is NULL verify it's the proper one...
364 if ( (Handle1
== NULL
&& Handle2
!= NULL
)
365 || (Handle1
!= NULL
&& Handle2
== NULL
)
368 // Figure out which one should be NULL and move the handle to the right place.
369 // If Handle1 is NULL then test Handle2 and vise versa.
370 // The one that DOES has driver binding must be Handle2
372 if (Handle1
== NULL
) {
373 if (EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
381 if (EFI_ERROR(gBS
->OpenProtocol(Handle1
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
391 return (ConnectControllers(Handle1
, Handle2
, Recursive
, Output
, (BOOLEAN
)(Handle2
!= NULL
&& Handle1
!= NULL
)));
394 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
401 Function for 'connect' command.
403 @param[in] ImageHandle Handle to the Image (NULL if Internal).
404 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
408 ShellCommandRunConnect (
409 IN EFI_HANDLE ImageHandle
,
410 IN EFI_SYSTEM_TABLE
*SystemTable
415 CHAR16
*ProblemParam
;
416 SHELL_STATUS ShellStatus
;
417 CONST CHAR16
*Param1
;
418 CONST CHAR16
*Param2
;
424 ShellStatus
= SHELL_SUCCESS
;
426 // initialize the shell lib (we must be in non-auto-init...)
428 Status
= ShellInitialize();
429 ASSERT_EFI_ERROR(Status
);
431 Status
= CommandInit();
432 ASSERT_EFI_ERROR(Status
);
435 // parse the command line
437 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
438 if (EFI_ERROR(Status
)) {
439 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
440 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDriver1HiiHandle
, ProblemParam
);
441 FreePool(ProblemParam
);
442 ShellStatus
= SHELL_INVALID_PARAMETER
;
448 // if more than 2 'value' parameters (plus the name one) or either -r or -c with any value parameters we have too many parameters
450 Count
= (gInReconnect
?0x4:0x3);
451 if ((ShellCommandLineGetCount(Package
) > Count
)
452 ||((ShellCommandLineGetFlag(Package
, L
"-r") || ShellCommandLineGetFlag(Package
, L
"-c")) && ShellCommandLineGetCount(Package
)>1)
453 ||(ShellCommandLineGetFlag(Package
, L
"-r") && ShellCommandLineGetFlag(Package
, L
"-c") )
456 // error for too many parameters
458 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDriver1HiiHandle
);
459 ShellStatus
= SHELL_INVALID_PARAMETER
;
460 } else if (ShellCommandLineGetFlag(Package
, L
"-c")) {
462 // do the conin and conout from EFI variables
463 // if the first fails dont 'loose' the error
465 Status
= ConnectFromDevPaths(L
"ConInDev");
466 if (EFI_ERROR(Status
)) {
467 ConnectFromDevPaths(L
"ConOutDev");
469 Status
= ConnectFromDevPaths(L
"ConOutDev");
471 if (EFI_ERROR(Status
)) {
472 ConnectFromDevPaths(L
"ErrOutDev");
474 Status
= ConnectFromDevPaths(L
"ErrOutDev");
476 if (EFI_ERROR(Status
)) {
477 ConnectFromDevPaths(L
"ErrOut");
479 Status
= ConnectFromDevPaths(L
"ErrOut");
481 if (EFI_ERROR(Status
)) {
482 ConnectFromDevPaths(L
"ConIn");
484 Status
= ConnectFromDevPaths(L
"ConIn");
486 if (EFI_ERROR(Status
)) {
487 ConnectFromDevPaths(L
"ConOut");
489 Status
= ConnectFromDevPaths(L
"ConOut");
491 if (EFI_ERROR(Status
)) {
492 ShellStatus
= SHELL_DEVICE_ERROR
;
496 // 0, 1, or 2 specific handles and possibly recursive
498 Param1
= ShellCommandLineGetRawValue(Package
, 1);
499 Param2
= ShellCommandLineGetRawValue(Package
, 2);
500 Count
= ShellCommandLineGetCount(Package
);
502 if (Param1
!= NULL
) {
503 Status
= ShellConvertStringToUint64(Param1
, &Intermediate
, TRUE
, FALSE
);
504 Handle1
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
505 if (EFI_ERROR(Status
)) {
506 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param1
);
507 ShellStatus
= SHELL_INVALID_PARAMETER
;
513 if (Param2
!= NULL
) {
514 Status
= ShellConvertStringToUint64(Param2
, &Intermediate
, TRUE
, FALSE
);
515 Handle2
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
516 if (EFI_ERROR(Status
)) {
517 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
518 ShellStatus
= SHELL_INVALID_PARAMETER
;
524 if (ShellStatus
== SHELL_SUCCESS
) {
525 if (Param1
!= NULL
&& Handle1
== NULL
){
526 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param1
);
527 ShellStatus
= SHELL_INVALID_PARAMETER
;
528 } else if (Param2
!= NULL
&& Handle2
== NULL
) {
529 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
530 ShellStatus
= SHELL_INVALID_PARAMETER
;
531 } else if (Handle2
!= NULL
&& Handle1
!= NULL
&& EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
532 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, Param2
);
533 ShellStatus
= SHELL_INVALID_PARAMETER
;
535 Status
= ConvertAndConnectControllers(Handle1
, Handle2
, ShellCommandLineGetFlag(Package
, L
"-r"), (BOOLEAN
)(Count
!=0));
536 if (EFI_ERROR(Status
)) {
537 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CONNECT_NONE
), gShellDriver1HiiHandle
);
538 ShellStatus
= SHELL_DEVICE_ERROR
;
544 ShellCommandLineFreeVarList (Package
);
546 return (ShellStatus
);