2 Main file for connect shell Driver1 function.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "UefiShellDriver1CommandsLib.h"
13 Create all handles associate with every device path node.
15 @param DevicePathToConnect The device path which will be connected.
17 @retval EFI_SUCCESS All handles associate with every device path node
19 @retval EFI_INVALID_PARAMETER DevicePathToConnect is NULL.
20 @retval EFI_NOT_FOUND Create the handle associate with one device path
25 ShellConnectDevicePath (
26 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathToConnect
29 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
32 EFI_HANDLE PreviousHandle
;
34 if (DevicePathToConnect
== NULL
) {
35 return EFI_INVALID_PARAMETER
;
38 PreviousHandle
= NULL
;
40 RemainingDevicePath
= DevicePathToConnect
;
41 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &Handle
);
43 if (!EFI_ERROR (Status
) && (Handle
!= NULL
)) {
44 if (PreviousHandle
== Handle
) {
45 Status
= EFI_NOT_FOUND
;
47 PreviousHandle
= Handle
;
48 Status
= gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
51 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
));
57 Connect drivers for PCI root bridge.
59 @retval EFI_SUCCESS Connect drivers successfully.
60 @retval EFI_NOT_FOUND Cannot find PCI root bridge device.
64 ShellConnectPciRootBridge (
68 UINTN RootBridgeHandleCount
;
69 EFI_HANDLE
*RootBridgeHandleBuffer
;
70 UINTN RootBridgeIndex
;
73 RootBridgeHandleCount
= 0;
75 Status
= gBS
->LocateHandleBuffer (
77 &gEfiPciRootBridgeIoProtocolGuid
,
79 &RootBridgeHandleCount
,
80 &RootBridgeHandleBuffer
82 if (EFI_ERROR (Status
)) {
86 for (RootBridgeIndex
= 0; RootBridgeIndex
< RootBridgeHandleCount
; RootBridgeIndex
++) {
87 gBS
->ConnectController (RootBridgeHandleBuffer
[RootBridgeIndex
], NULL
, NULL
, FALSE
);
90 FreePool (RootBridgeHandleBuffer
);
96 Connect controller(s) and driver(s).
98 @param[in] ControllerHandle The handle to the controller. Should have driver binding on it.
99 @param[in] DriverHandle The handle to the driver. Should have driver binding.
100 @param[in] Recursive TRUE to connect recursively, FALSE otherwise.
101 @param[in] Output TRUE to have info on the screen, FALSE otherwise.
102 @param[in] AlwaysOutput Override Output for errors.
104 @retval EFI_SUCCESS The operation was successful.
108 IN CONST EFI_HANDLE ControllerHandle OPTIONAL
,
109 IN CONST EFI_HANDLE DriverHandle OPTIONAL
,
110 IN CONST BOOLEAN Recursive
,
111 IN CONST BOOLEAN Output
,
112 IN CONST BOOLEAN AlwaysOutput
117 EFI_HANDLE
*ControllerHandleList
;
118 EFI_HANDLE
*DriverHandleList
;
119 EFI_HANDLE
*HandleWalker
;
121 ControllerHandleList
= NULL
;
122 Status
= EFI_NOT_FOUND
;
123 Status2
= EFI_NOT_FOUND
;
126 // If we have a single handle to connect make that a 'list'
128 if (DriverHandle
== NULL
) {
129 DriverHandleList
= NULL
;
131 DriverHandleList
= AllocateZeroPool (2*sizeof (EFI_HANDLE
));
132 if (DriverHandleList
== NULL
) {
133 return (EFI_OUT_OF_RESOURCES
);
136 DriverHandleList
[0] = DriverHandle
;
137 DriverHandleList
[1] = NULL
;
141 // do we connect all controllers (with a loop) or a single one...
142 // This is where we call the gBS->ConnectController function.
144 if (ControllerHandle
== NULL
) {
145 ControllerHandleList
= GetHandleListByProtocol (&gEfiDevicePathProtocolGuid
);
146 for (HandleWalker
= ControllerHandleList
147 ; HandleWalker
!= NULL
&& *HandleWalker
!= NULL
151 Status
= gBS
->ConnectController (*HandleWalker
, DriverHandleList
, NULL
, Recursive
);
152 if (!EFI_ERROR (Status
)) {
153 Status2
= EFI_SUCCESS
;
156 if ((Output
&& !EFI_ERROR (Status
)) || AlwaysOutput
) {
157 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_HANDLE_RESULT
), gShellDriver1HiiHandle
, L
"Connect", ConvertHandleToHandleIndex (*HandleWalker
), Status
);
161 Status
= gBS
->ConnectController (ControllerHandle
, DriverHandleList
, NULL
, Recursive
);
162 if (!EFI_ERROR (Status
)) {
163 Status2
= EFI_SUCCESS
;
166 if ((Output
&& !EFI_ERROR (Status
)) || AlwaysOutput
) {
167 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_HANDLE_RESULT
), gShellDriver1HiiHandle
, L
"Connect", ConvertHandleToHandleIndex (ControllerHandle
), Status
);
172 // Free any memory we allocated.
174 if (ControllerHandleList
!= NULL
) {
175 FreePool (ControllerHandleList
);
178 if (DriverHandleList
!= NULL
) {
179 FreePool (DriverHandleList
);
186 Do a connect from an EFI variable via it's key name.
188 @param[in] Key The name of the EFI Variable.
190 @retval EFI_SUCCESS The operation was successful.
193 ShellConnectFromDevPaths (
197 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
198 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevPath
;
199 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
200 EFI_DEVICE_PATH_PROTOCOL
*Next
;
203 UINTN HandleArrayCount
;
205 EFI_HANDLE
*HandleArray
;
207 BOOLEAN AtLeastOneConnected
;
208 EFI_PCI_IO_PROTOCOL
*PciIo
;
213 AtLeastOneConnected
= FALSE
;
216 // Get the DevicePath buffer from the variable...
218 Status
= gRT
->GetVariable ((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
219 if (Status
== EFI_BUFFER_TOO_SMALL
) {
220 DevPath
= AllocateZeroPool (Length
);
221 if (DevPath
== NULL
) {
222 return EFI_OUT_OF_RESOURCES
;
225 Status
= gRT
->GetVariable ((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
226 if (EFI_ERROR (Status
)) {
227 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
) {
253 return EFI_UNSUPPORTED
;
257 while (!IsDevicePathEndType (Next
)) {
258 Next
= NextDevicePathNode (Next
);
261 SetDevicePathEndNode (Next
);
263 // connect short form device path
265 if ((DevicePathType (Instance
) == MESSAGING_DEVICE_PATH
) &&
266 ( (DevicePathSubType (Instance
) == MSG_USB_CLASS_DP
)
267 || (DevicePathSubType (Instance
) == MSG_USB_WWID_DP
)
270 Status
= ShellConnectPciRootBridge ();
271 if (EFI_ERROR (Status
)) {
277 Status
= gBS
->LocateHandleBuffer (
279 &gEfiPciIoProtocolGuid
,
285 if (!EFI_ERROR (Status
)) {
286 for (Index
= 0; Index
< HandleArrayCount
; Index
++) {
287 Status
= gBS
->HandleProtocol (
289 &gEfiPciIoProtocolGuid
,
293 if (!EFI_ERROR (Status
)) {
294 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x09, 3, &Class
);
295 if (!EFI_ERROR (Status
)) {
296 if ((PCI_CLASS_SERIAL
== Class
[2]) &&
297 (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
;
342 Convert the handle identifiers from strings and then connect them.
344 One of them should have driver binding and either can be NULL.
346 @param[in] Handle1 The first handle.
347 @param[in] Handle2 The second handle.
348 @param[in] Recursive TRUE to do connect recursively. FALSE otherwise.
349 @param[in] Output TRUE to have output to screen. FALSE otherwise.
351 @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
))
369 // Figure out which one should be NULL and move the handle to the right place.
370 // If Handle1 is NULL then test Handle2 and vise versa.
371 // The one that DOES has driver binding must be Handle2
373 if (Handle1
== NULL
) {
374 if (EFI_ERROR (gBS
->OpenProtocol (Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
382 if (EFI_ERROR (gBS
->OpenProtocol (Handle1
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
392 return (ConnectControllers (Handle1
, Handle2
, Recursive
, Output
, (BOOLEAN
)(Handle2
!= NULL
&& Handle1
!= NULL
)));
395 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
402 Function for 'connect' command.
404 @param[in] ImageHandle Handle to the Image (NULL if Internal).
405 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
409 ShellCommandRunConnect (
410 IN EFI_HANDLE ImageHandle
,
411 IN EFI_SYSTEM_TABLE
*SystemTable
416 CHAR16
*ProblemParam
;
417 SHELL_STATUS ShellStatus
;
418 CONST CHAR16
*Param1
;
419 CONST CHAR16
*Param2
;
425 ShellStatus
= SHELL_SUCCESS
;
427 // initialize the shell lib (we must be in non-auto-init...)
429 Status
= ShellInitialize ();
430 ASSERT_EFI_ERROR (Status
);
432 Status
= CommandInit ();
433 ASSERT_EFI_ERROR (Status
);
436 // parse the command line
438 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
439 if (EFI_ERROR (Status
)) {
440 if ((Status
== EFI_VOLUME_CORRUPTED
) && (ProblemParam
!= NULL
)) {
441 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDriver1HiiHandle
, L
"connect", ProblemParam
);
442 FreePool (ProblemParam
);
443 ShellStatus
= SHELL_INVALID_PARAMETER
;
449 // if more than 2 'value' parameters (plus the name one) or either -r or -c with any value parameters we have too many parameters
451 Count
= (gInReconnect
? 0x4 : 0x3);
452 if ( (ShellCommandLineGetCount (Package
) > Count
)
453 || (ShellCommandLineGetFlag (Package
, L
"-c") && (ShellCommandLineGetCount (Package
) > 1))
454 || (ShellCommandLineGetFlag (Package
, L
"-r") && (ShellCommandLineGetCount (Package
) > 2))
455 || (ShellCommandLineGetFlag (Package
, L
"-r") && ShellCommandLineGetFlag (Package
, L
"-c"))
459 // error for too many parameters
461 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDriver1HiiHandle
, L
"connect");
462 ShellStatus
= SHELL_INVALID_PARAMETER
;
463 } else if (ShellCommandLineGetFlag (Package
, L
"-c")) {
465 // do the conin and conout from EFI variables
466 // if the first fails dont 'loose' the error
468 Status
= ShellConnectFromDevPaths (L
"ConInDev");
469 if (EFI_ERROR (Status
)) {
470 ShellConnectFromDevPaths (L
"ConOutDev");
472 Status
= ShellConnectFromDevPaths (L
"ConOutDev");
475 if (EFI_ERROR (Status
)) {
476 ShellConnectFromDevPaths (L
"ErrOutDev");
478 Status
= ShellConnectFromDevPaths (L
"ErrOutDev");
481 if (EFI_ERROR (Status
)) {
482 ShellConnectFromDevPaths (L
"ErrOut");
484 Status
= ShellConnectFromDevPaths (L
"ErrOut");
487 if (EFI_ERROR (Status
)) {
488 ShellConnectFromDevPaths (L
"ConIn");
490 Status
= ShellConnectFromDevPaths (L
"ConIn");
493 if (EFI_ERROR (Status
)) {
494 ShellConnectFromDevPaths (L
"ConOut");
496 Status
= ShellConnectFromDevPaths (L
"ConOut");
499 if (EFI_ERROR (Status
)) {
500 ShellStatus
= SHELL_DEVICE_ERROR
;
504 // 0, 1, or 2 specific handles and possibly recursive
506 Param1
= ShellCommandLineGetRawValue (Package
, 1);
507 Param2
= ShellCommandLineGetRawValue (Package
, 2);
508 Count
= ShellCommandLineGetCount (Package
);
510 if (Param1
!= NULL
) {
511 Status
= ShellConvertStringToUint64 (Param1
, &Intermediate
, TRUE
, FALSE
);
512 Handle1
= ConvertHandleIndexToHandle ((UINTN
)Intermediate
);
513 if (EFI_ERROR (Status
)) {
514 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, L
"connect", Param1
);
515 ShellStatus
= SHELL_INVALID_PARAMETER
;
521 if (Param2
!= NULL
) {
522 Status
= ShellConvertStringToUint64 (Param2
, &Intermediate
, TRUE
, FALSE
);
523 Handle2
= ConvertHandleIndexToHandle ((UINTN
)Intermediate
);
524 if (EFI_ERROR (Status
)) {
525 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, L
"connect", Param2
);
526 ShellStatus
= SHELL_INVALID_PARAMETER
;
532 if (ShellStatus
== SHELL_SUCCESS
) {
533 if ((Param1
!= NULL
) && (Handle1
== NULL
)) {
534 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, L
"connect", Param1
);
535 ShellStatus
= SHELL_INVALID_PARAMETER
;
536 } else if ((Param2
!= NULL
) && (Handle2
== NULL
)) {
537 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, L
"connect", Param2
);
538 ShellStatus
= SHELL_INVALID_PARAMETER
;
539 } else if ((Handle2
!= NULL
) && (Handle1
!= NULL
) && EFI_ERROR (gBS
->OpenProtocol (Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
540 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, L
"connect", Param2
);
541 ShellStatus
= SHELL_INVALID_PARAMETER
;
543 Status
= ConvertAndConnectControllers (Handle1
, Handle2
, ShellCommandLineGetFlag (Package
, L
"-r"), (BOOLEAN
)(Count
!= 0));
544 if (EFI_ERROR (Status
)) {
545 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_CONNECT_NONE
), gShellDriver1HiiHandle
);
546 ShellStatus
= SHELL_DEVICE_ERROR
;
552 ShellCommandLineFreeVarList (Package
);
555 return (ShellStatus
);