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
);
52 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
) );
59 Connect drivers for PCI root bridge.
61 @retval EFI_SUCCESS Connect drivers successfully.
62 @retval EFI_NOT_FOUND Cannot find PCI root bridge device.
66 ShellConnectPciRootBridge (
70 UINTN RootBridgeHandleCount
;
71 EFI_HANDLE
*RootBridgeHandleBuffer
;
72 UINTN RootBridgeIndex
;
75 RootBridgeHandleCount
= 0;
77 Status
= gBS
->LocateHandleBuffer (
79 &gEfiPciRootBridgeIoProtocolGuid
,
81 &RootBridgeHandleCount
,
82 &RootBridgeHandleBuffer
84 if (EFI_ERROR (Status
)) {
88 for (RootBridgeIndex
= 0; RootBridgeIndex
< RootBridgeHandleCount
; RootBridgeIndex
++) {
89 gBS
->ConnectController (RootBridgeHandleBuffer
[RootBridgeIndex
], NULL
, NULL
, FALSE
);
92 FreePool (RootBridgeHandleBuffer
);
99 Connect controller(s) and driver(s).
101 @param[in] ControllerHandle The handle to the controller. Should have driver binding on it.
102 @param[in] DriverHandle The handle to the driver. Should have driver binding.
103 @param[in] Recursive TRUE to connect recursively, FALSE otherwise.
104 @param[in] Output TRUE to have info on the screen, FALSE otherwise.
105 @param[in] AlwaysOutput Override Output for errors.
107 @retval EFI_SUCCESS The operation was successful.
111 IN CONST EFI_HANDLE ControllerHandle OPTIONAL
,
112 IN CONST EFI_HANDLE DriverHandle OPTIONAL
,
113 IN CONST BOOLEAN Recursive
,
114 IN CONST BOOLEAN Output
,
115 IN CONST BOOLEAN AlwaysOutput
120 EFI_HANDLE
*ControllerHandleList
;
121 EFI_HANDLE
*DriverHandleList
;
122 EFI_HANDLE
*HandleWalker
;
124 ControllerHandleList
= NULL
;
125 Status
= EFI_NOT_FOUND
;
126 Status2
= EFI_NOT_FOUND
;
129 // If we have a single handle to connect make that a 'list'
131 if (DriverHandle
== NULL
) {
132 DriverHandleList
= NULL
;
134 DriverHandleList
= AllocateZeroPool(2*sizeof(EFI_HANDLE
));
135 if (DriverHandleList
== NULL
) {
136 return (EFI_OUT_OF_RESOURCES
);
138 DriverHandleList
[0] = DriverHandle
;
139 DriverHandleList
[1] = NULL
;
143 // do we connect all controllers (with a loop) or a single one...
144 // This is where we call the gBS->ConnectController function.
146 if (ControllerHandle
== NULL
) {
147 ControllerHandleList
= GetHandleListByProtocol(&gEfiDevicePathProtocolGuid
);
148 for (HandleWalker
= ControllerHandleList
149 ; HandleWalker
!= NULL
&& *HandleWalker
!= NULL
152 Status
= gBS
->ConnectController(*HandleWalker
, DriverHandleList
, NULL
, Recursive
);
153 if (!EFI_ERROR(Status
)) {
154 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
;
165 if ((Output
&& !EFI_ERROR(Status
)) || AlwaysOutput
) {
166 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_HANDLE_RESULT
), gShellDriver1HiiHandle
, L
"Connect", ConvertHandleToHandleIndex(ControllerHandle
), Status
);
171 // Free any memory we allocated.
173 if (ControllerHandleList
!= NULL
) {
174 FreePool(ControllerHandleList
);
176 if (DriverHandleList
!= NULL
) {
177 FreePool(DriverHandleList
);
183 Do a connect from an EFI variable via it's key name.
185 @param[in] Key The name of the EFI Variable.
187 @retval EFI_SUCCESS The operation was successful.
190 ShellConnectFromDevPaths (
194 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
195 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevPath
;
196 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
197 EFI_DEVICE_PATH_PROTOCOL
*Next
;
200 UINTN HandleArrayCount
;
202 EFI_HANDLE
*HandleArray
;
204 BOOLEAN AtLeastOneConnected
;
205 EFI_PCI_IO_PROTOCOL
*PciIo
;
210 AtLeastOneConnected
= FALSE
;
213 // Get the DevicePath buffer from the variable...
215 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
216 if (Status
== EFI_BUFFER_TOO_SMALL
) {
217 DevPath
= AllocateZeroPool(Length
);
218 if (DevPath
== NULL
) {
219 return EFI_OUT_OF_RESOURCES
;
221 Status
= gRT
->GetVariable((CHAR16
*)Key
, (EFI_GUID
*)&gEfiGlobalVariableGuid
, NULL
, &Length
, DevPath
);
222 if (EFI_ERROR (Status
)) {
223 if (DevPath
!= NULL
) {
228 } else if (EFI_ERROR (Status
)) {
232 Status
= EFI_NOT_FOUND
;
234 CopyOfDevPath
= DevPath
;
236 // walk the list of devices and connect them
240 // Check every instance of the console variable
242 Instance
= GetNextDevicePathInstance (&CopyOfDevPath
, &Size
);
243 if (Instance
== NULL
) {
244 if (DevPath
!= NULL
) {
247 return EFI_UNSUPPORTED
;
251 while (!IsDevicePathEndType (Next
)) {
252 Next
= NextDevicePathNode (Next
);
255 SetDevicePathEndNode (Next
);
257 // connect short form device path
259 if ((DevicePathType (Instance
) == MESSAGING_DEVICE_PATH
) &&
260 ((DevicePathSubType (Instance
) == MSG_USB_CLASS_DP
)
261 || (DevicePathSubType (Instance
) == MSG_USB_WWID_DP
)
264 Status
= ShellConnectPciRootBridge ();
265 if (EFI_ERROR(Status
)) {
271 Status
= gBS
->LocateHandleBuffer (
273 &gEfiPciIoProtocolGuid
,
279 if (!EFI_ERROR (Status
)) {
280 for (Index
= 0; Index
< HandleArrayCount
; Index
++) {
281 Status
= gBS
->HandleProtocol (
283 &gEfiPciIoProtocolGuid
,
287 if (!EFI_ERROR (Status
)) {
288 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x09, 3, &Class
);
289 if (!EFI_ERROR (Status
)) {
290 if ((PCI_CLASS_SERIAL
== Class
[2]) &&
291 (PCI_CLASS_SERIAL_USB
== Class
[1])) {
292 Status
= gBS
->ConnectController (
298 if (!EFI_ERROR(Status
)) {
299 AtLeastOneConnected
= TRUE
;
307 if (HandleArray
!= NULL
) {
308 FreePool (HandleArray
);
312 // connect the entire device path
314 Status
= ShellConnectDevicePath (Instance
);
315 if (!EFI_ERROR (Status
)) {
316 AtLeastOneConnected
= TRUE
;
321 } while (CopyOfDevPath
!= NULL
);
323 if (DevPath
!= NULL
) {
327 if (AtLeastOneConnected
) {
330 return EFI_NOT_FOUND
;
336 Convert the handle identifiers from strings and then connect them.
338 One of them should have driver binding and either can be NULL.
340 @param[in] Handle1 The first handle.
341 @param[in] Handle2 The second handle.
342 @param[in] Recursive TRUE to do connect recursively. FALSE otherwise.
343 @param[in] Output TRUE to have output to screen. FALSE otherwise.
345 @retval EFI_SUCCESS The operation was successful.
348 ConvertAndConnectControllers (
349 IN EFI_HANDLE
*Handle1 OPTIONAL
,
350 IN EFI_HANDLE
*Handle2 OPTIONAL
,
351 IN CONST BOOLEAN Recursive
,
352 IN CONST BOOLEAN Output
356 // if only one is NULL verify it's the proper one...
358 if ( (Handle1
== NULL
&& Handle2
!= NULL
)
359 || (Handle1
!= NULL
&& Handle2
== NULL
)
362 // Figure out which one should be NULL and move the handle to the right place.
363 // If Handle1 is NULL then test Handle2 and vise versa.
364 // The one that DOES has driver binding must be Handle2
366 if (Handle1
== NULL
) {
367 if (EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
375 if (EFI_ERROR(gBS
->OpenProtocol(Handle1
, &gEfiDriverBindingProtocolGuid
, NULL
, NULL
, gImageHandle
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
385 return (ConnectControllers(Handle1
, Handle2
, Recursive
, Output
, (BOOLEAN
)(Handle2
!= NULL
&& Handle1
!= NULL
)));
388 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
395 Function for 'connect' command.
397 @param[in] ImageHandle Handle to the Image (NULL if Internal).
398 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
402 ShellCommandRunConnect (
403 IN EFI_HANDLE ImageHandle
,
404 IN EFI_SYSTEM_TABLE
*SystemTable
409 CHAR16
*ProblemParam
;
410 SHELL_STATUS ShellStatus
;
411 CONST CHAR16
*Param1
;
412 CONST CHAR16
*Param2
;
418 ShellStatus
= SHELL_SUCCESS
;
420 // initialize the shell lib (we must be in non-auto-init...)
422 Status
= ShellInitialize();
423 ASSERT_EFI_ERROR(Status
);
425 Status
= CommandInit();
426 ASSERT_EFI_ERROR(Status
);
429 // parse the command line
431 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
432 if (EFI_ERROR(Status
)) {
433 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
434 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDriver1HiiHandle
, L
"connect", ProblemParam
);
435 FreePool(ProblemParam
);
436 ShellStatus
= SHELL_INVALID_PARAMETER
;
442 // if more than 2 'value' parameters (plus the name one) or either -r or -c with any value parameters we have too many parameters
444 Count
= (gInReconnect
?0x4:0x3);
445 if ((ShellCommandLineGetCount(Package
) > Count
)
446 ||(ShellCommandLineGetFlag(Package
, L
"-c") && ShellCommandLineGetCount(Package
)>1)
447 ||(ShellCommandLineGetFlag(Package
, L
"-r") && ShellCommandLineGetCount(Package
)>2)
448 ||(ShellCommandLineGetFlag(Package
, L
"-r") && ShellCommandLineGetFlag(Package
, L
"-c") )
451 // error for too many parameters
453 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDriver1HiiHandle
, L
"connect");
454 ShellStatus
= SHELL_INVALID_PARAMETER
;
455 } else if (ShellCommandLineGetFlag(Package
, L
"-c")) {
457 // do the conin and conout from EFI variables
458 // if the first fails dont 'loose' the error
460 Status
= ShellConnectFromDevPaths(L
"ConInDev");
461 if (EFI_ERROR(Status
)) {
462 ShellConnectFromDevPaths(L
"ConOutDev");
464 Status
= ShellConnectFromDevPaths(L
"ConOutDev");
466 if (EFI_ERROR(Status
)) {
467 ShellConnectFromDevPaths(L
"ErrOutDev");
469 Status
= ShellConnectFromDevPaths(L
"ErrOutDev");
471 if (EFI_ERROR(Status
)) {
472 ShellConnectFromDevPaths(L
"ErrOut");
474 Status
= ShellConnectFromDevPaths(L
"ErrOut");
476 if (EFI_ERROR(Status
)) {
477 ShellConnectFromDevPaths(L
"ConIn");
479 Status
= ShellConnectFromDevPaths(L
"ConIn");
481 if (EFI_ERROR(Status
)) {
482 ShellConnectFromDevPaths(L
"ConOut");
484 Status
= ShellConnectFromDevPaths(L
"ConOut");
486 if (EFI_ERROR(Status
)) {
487 ShellStatus
= SHELL_DEVICE_ERROR
;
491 // 0, 1, or 2 specific handles and possibly recursive
493 Param1
= ShellCommandLineGetRawValue(Package
, 1);
494 Param2
= ShellCommandLineGetRawValue(Package
, 2);
495 Count
= ShellCommandLineGetCount(Package
);
497 if (Param1
!= NULL
) {
498 Status
= ShellConvertStringToUint64(Param1
, &Intermediate
, TRUE
, FALSE
);
499 Handle1
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
500 if (EFI_ERROR(Status
)) {
501 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, L
"connect", Param1
);
502 ShellStatus
= SHELL_INVALID_PARAMETER
;
508 if (Param2
!= NULL
) {
509 Status
= ShellConvertStringToUint64(Param2
, &Intermediate
, TRUE
, FALSE
);
510 Handle2
= ConvertHandleIndexToHandle((UINTN
)Intermediate
);
511 if (EFI_ERROR(Status
)) {
512 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, L
"connect", Param2
);
513 ShellStatus
= SHELL_INVALID_PARAMETER
;
519 if (ShellStatus
== SHELL_SUCCESS
) {
520 if (Param1
!= NULL
&& Handle1
== NULL
){
521 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, L
"connect", Param1
);
522 ShellStatus
= SHELL_INVALID_PARAMETER
;
523 } else if (Param2
!= NULL
&& Handle2
== NULL
) {
524 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, L
"connect", Param2
);
525 ShellStatus
= SHELL_INVALID_PARAMETER
;
526 } else if (Handle2
!= NULL
&& Handle1
!= NULL
&& EFI_ERROR(gBS
->OpenProtocol(Handle2
, &gEfiDriverBindingProtocolGuid
, NULL
, gImageHandle
, NULL
, EFI_OPEN_PROTOCOL_TEST_PROTOCOL
))) {
527 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_INV_HANDLE
), gShellDriver1HiiHandle
, L
"connect", Param2
);
528 ShellStatus
= SHELL_INVALID_PARAMETER
;
530 Status
= ConvertAndConnectControllers(Handle1
, Handle2
, ShellCommandLineGetFlag(Package
, L
"-r"), (BOOLEAN
)(Count
!=0));
531 if (EFI_ERROR(Status
)) {
532 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CONNECT_NONE
), gShellDriver1HiiHandle
);
533 ShellStatus
= SHELL_DEVICE_ERROR
;
539 ShellCommandLineFreeVarList (Package
);
541 return (ShellStatus
);