2 BDS Lib functions which relate with connect the device
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalBdsLib.h"
13 This function will connect all the system driver to controller
14 first, and then special connect the default console, this make
15 sure all the system controller available and the platform default
26 // Connect the platform console first
28 BdsLibConnectAllDefaultConsoles ();
31 // Generic way to connect all the drivers
33 BdsLibConnectAllDriversToAllControllers ();
36 // Here we have the assumption that we have already had
37 // platform default console
39 BdsLibConnectAllDefaultConsoles ();
44 This function will connect all the system drivers to all controllers
45 first, and then connect all the console devices the system current
46 have. After this we should get all the device work and console available
47 if the system have console device.
51 BdsLibGenericConnectAll (
56 // Most generic way to connect all the drivers
58 BdsLibConnectAllDriversToAllControllers ();
59 BdsLibConnectAllConsoles ();
63 This function will create all handles associate with every device
64 path node. If the handle associate with one device path node can not
65 be created successfully, then still give chance to do the dispatch,
66 which load the missing drivers if possible.
68 @param DevicePathToConnect The device path which will be connected, it can be
69 a multi-instance device path
71 @retval EFI_SUCCESS All handles associate with every device path node
73 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles
74 @retval EFI_NOT_FOUND Create the handle associate with one device path
80 BdsLibConnectDevicePath (
81 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathToConnect
85 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
86 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevicePath
;
87 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
88 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
89 EFI_DEVICE_PATH_PROTOCOL
*Next
;
91 EFI_HANDLE PreviousHandle
;
95 if (DevicePathToConnect
== NULL
) {
99 CurrentTpl
= EfiGetCurrentTpl ();
101 DevicePath
= DuplicateDevicePath (DevicePathToConnect
);
102 if (DevicePath
== NULL
) {
103 return EFI_OUT_OF_RESOURCES
;
105 CopyOfDevicePath
= DevicePath
;
109 // The outer loop handles multi instance device paths.
110 // Only console variables contain multiple instance device paths.
112 // After this call DevicePath points to the next Instance
114 Instance
= GetNextDevicePathInstance (&DevicePath
, &Size
);
115 if (Instance
== NULL
) {
116 FreePool (CopyOfDevicePath
);
117 return EFI_OUT_OF_RESOURCES
;
121 while (!IsDevicePathEndType (Next
)) {
122 Next
= NextDevicePathNode (Next
);
125 SetDevicePathEndNode (Next
);
128 // Start the real work of connect with RemainingDevicePath
130 PreviousHandle
= NULL
;
133 // Find the handle that best matches the Device Path. If it is only a
134 // partial match the remaining part of the device path is returned in
135 // RemainingDevicePath.
137 RemainingDevicePath
= Instance
;
138 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &Handle
);
140 if (!EFI_ERROR (Status
)) {
141 if (Handle
== PreviousHandle
) {
143 // If no forward progress is made try invoking the Dispatcher.
144 // A new FV may have been added to the system an new drivers
146 // Status == EFI_SUCCESS means a driver was dispatched
147 // Status == EFI_NOT_FOUND means no new drivers were dispatched
149 if (CurrentTpl
== TPL_APPLICATION
) {
151 // Dispatch calls LoadImage/StartImage which cannot run at TPL > TPL_APPLICATION
153 Status
= gDS
->Dispatch ();
156 // Always return EFI_NOT_FOUND here
157 // to prevent dead loop when control handle is found but connection failded case
159 Status
= EFI_NOT_FOUND
;
163 if (!EFI_ERROR (Status
)) {
164 PreviousHandle
= Handle
;
166 // Connect all drivers that apply to Handle and RemainingDevicePath,
167 // the Recursive flag is FALSE so only one level will be expanded.
169 // Do not check the connect status here, if the connect controller fail,
170 // then still give the chance to do dispatch, because partial
171 // RemainingDevicepath may be in the new FV
173 // 1. If the connect fail, RemainingDevicepath and handle will not
174 // change, so next time will do the dispatch, then dispatch's status
176 // 2. If the connect success, the RemainingDevicepath and handle will
177 // change, then avoid the dispatch, we have chance to continue the
180 gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
184 // Loop until RemainingDevicePath is an empty device path
186 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
));
188 } while (DevicePath
!= NULL
);
190 if (CopyOfDevicePath
!= NULL
) {
191 FreePool (CopyOfDevicePath
);
194 // All handle with DevicePath exists in the handle database
200 This function will connect all current system handles recursively.
202 gBS->ConnectController() service is invoked for each handle exist in system handler buffer.
203 If the handle is bus type handler, all childrens also will be connected recursively
204 by gBS->ConnectController().
206 @retval EFI_SUCCESS All handles and it's child handle have been connected
207 @retval EFI_STATUS Error status returned by of gBS->LocateHandleBuffer().
212 BdsLibConnectAllEfi (
218 EFI_HANDLE
*HandleBuffer
;
221 Status
= gBS
->LocateHandleBuffer (
228 if (EFI_ERROR (Status
)) {
232 for (Index
= 0; Index
< HandleCount
; Index
++) {
233 Status
= gBS
->ConnectController (HandleBuffer
[Index
], NULL
, NULL
, TRUE
);
236 if (HandleBuffer
!= NULL
) {
237 FreePool (HandleBuffer
);
244 This function will disconnect all current system handles.
246 gBS->DisconnectController() is invoked for each handle exists in system handle buffer.
247 If handle is a bus type handle, all childrens also are disconnected recursively by
248 gBS->DisconnectController().
250 @retval EFI_SUCCESS All handles have been disconnected
251 @retval EFI_STATUS Error status returned by of gBS->LocateHandleBuffer().
256 BdsLibDisconnectAllEfi (
262 EFI_HANDLE
*HandleBuffer
;
268 Status
= gBS
->LocateHandleBuffer (
275 if (EFI_ERROR (Status
)) {
279 for (Index
= 0; Index
< HandleCount
; Index
++) {
280 Status
= gBS
->DisconnectController (HandleBuffer
[Index
], NULL
, NULL
);
283 if (HandleBuffer
!= NULL
) {
284 FreePool (HandleBuffer
);
292 Connects all drivers to all controllers.
293 This function make sure all the current system driver will manage
294 the correspoinding controllers if have. And at the same time, make
295 sure all the system controllers have driver to manage it if have.
300 BdsLibConnectAllDriversToAllControllers (
308 // Connect All EFI 1.10 drivers following EFI 1.10 algorithm
310 BdsLibConnectAllEfi ();
313 // Check to see if it's possible to dispatch an more DXE drivers.
314 // The BdsLibConnectAllEfi () may have made new DXE drivers show up.
315 // If anything is Dispatched Status == EFI_SUCCESS and we will try
316 // the connect again.
318 Status
= gDS
->Dispatch ();
320 } while (!EFI_ERROR (Status
));
326 Connect the specific Usb device which match the short form device path,
327 and whose bus is determined by Host Controller (Uhci or Ehci).
329 @param HostControllerPI Uhci (0x00) or Ehci (0x20) or Both uhci and ehci
331 @param RemainingDevicePath a short-form device path that starts with the first
332 element being a USB WWID or a USB Class device
335 @return EFI_INVALID_PARAMETER RemainingDevicePath is NULL pointer.
336 RemainingDevicePath is not a USB device path.
337 Invalid HostControllerPI type.
338 @return EFI_SUCCESS Success to connect USB device
339 @return EFI_NOT_FOUND Fail to find handle for USB controller to connect.
344 BdsLibConnectUsbDevByShortFormDP(
345 IN UINT8 HostControllerPI
,
346 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
350 EFI_HANDLE
*HandleArray
;
351 UINTN HandleArrayCount
;
353 EFI_PCI_IO_PROTOCOL
*PciIo
;
355 BOOLEAN AtLeastOneConnected
;
358 // Check the passed in parameters
360 if (RemainingDevicePath
== NULL
) {
361 return EFI_INVALID_PARAMETER
;
364 if ((DevicePathType (RemainingDevicePath
) != MESSAGING_DEVICE_PATH
) ||
365 ((DevicePathSubType (RemainingDevicePath
) != MSG_USB_CLASS_DP
)
366 && (DevicePathSubType (RemainingDevicePath
) != MSG_USB_WWID_DP
)
368 return EFI_INVALID_PARAMETER
;
371 if (HostControllerPI
!= 0xFF &&
372 HostControllerPI
!= 0x00 &&
373 HostControllerPI
!= 0x20) {
374 return EFI_INVALID_PARAMETER
;
378 // Find the usb host controller firstly, then connect with the remaining device path
380 AtLeastOneConnected
= FALSE
;
381 Status
= gBS
->LocateHandleBuffer (
383 &gEfiPciIoProtocolGuid
,
388 if (!EFI_ERROR (Status
)) {
389 for (Index
= 0; Index
< HandleArrayCount
; Index
++) {
390 Status
= gBS
->HandleProtocol (
392 &gEfiPciIoProtocolGuid
,
395 if (!EFI_ERROR (Status
)) {
397 // Check whether the Pci device is the wanted usb host controller
399 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x09, 3, &Class
);
400 if (!EFI_ERROR (Status
)) {
401 if ((PCI_CLASS_SERIAL
== Class
[2]) &&
402 (PCI_CLASS_SERIAL_USB
== Class
[1])) {
403 if (HostControllerPI
== Class
[0] || HostControllerPI
== 0xFF) {
404 Status
= gBS
->ConnectController (
410 if (!EFI_ERROR(Status
)) {
411 AtLeastOneConnected
= TRUE
;
419 if (HandleArray
!= NULL
) {
420 FreePool (HandleArray
);
423 if (AtLeastOneConnected
) {
428 return EFI_NOT_FOUND
;