2 Library functions which relate with connecting the device.
4 Copyright (c) 2011 - 2015, 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 "InternalBm.h"
18 Connect all the drivers to all the controllers.
20 This function makes sure all the current system drivers manage the correspoinding
21 controllers if have. And at the same time, makes sure all the system controllers
22 have driver to manage it if have.
25 BmConnectAllDriversToAllControllers (
31 EFI_HANDLE
*HandleBuffer
;
36 // Connect All EFI 1.10 drivers following EFI 1.10 algorithm
38 gBS
->LocateHandleBuffer (
46 for (Index
= 0; Index
< HandleCount
; Index
++) {
47 gBS
->ConnectController (HandleBuffer
[Index
], NULL
, NULL
, TRUE
);
50 if (HandleBuffer
!= NULL
) {
51 FreePool (HandleBuffer
);
55 // Check to see if it's possible to dispatch an more DXE drivers.
56 // The above code may have made new DXE drivers show up.
57 // If any new driver is dispatched (Status == EFI_SUCCESS) and we will try
60 Status
= gDS
->Dispatch ();
62 } while (!EFI_ERROR (Status
));
66 This function will connect all the system driver to controller
67 first, and then special connect the default console, this make
68 sure all the system controller available and the platform default
74 EfiBootManagerConnectAll (
79 // Connect the platform console first
81 EfiBootManagerConnectAllDefaultConsoles ();
84 // Generic way to connect all the drivers
86 BmConnectAllDriversToAllControllers ();
89 // Here we have the assumption that we have already had
90 // platform default console
92 EfiBootManagerConnectAllDefaultConsoles ();
96 This function will create all handles associate with every device
97 path node. If the handle associate with one device path node can not
98 be created successfully, then still give chance to do the dispatch,
99 which load the missing drivers if possible.
101 @param DevicePathToConnect The device path which will be connected, it can be
102 a multi-instance device path
103 @param MatchingHandle Return the controller handle closest to the DevicePathToConnect
105 @retval EFI_SUCCESS All handles associate with every device path node
107 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles.
108 @retval EFI_NOT_FOUND Create the handle associate with one device path
110 @retval EFI_SECURITY_VIOLATION The user has no permission to start UEFI device
111 drivers on the DevicePath.
115 EfiBootManagerConnectDevicePath (
116 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathToConnect
,
117 OUT EFI_HANDLE
*MatchingHandle OPTIONAL
121 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
123 EFI_HANDLE PreviousHandle
;
126 if (DevicePathToConnect
== NULL
) {
127 return EFI_INVALID_PARAMETER
;
130 CurrentTpl
= EfiGetCurrentTpl ();
132 // Start the real work of connect with RemainingDevicePath
134 PreviousHandle
= NULL
;
137 // Find the handle that best matches the Device Path. If it is only a
138 // partial match the remaining part of the device path is returned in
139 // RemainingDevicePath.
141 RemainingDevicePath
= DevicePathToConnect
;
142 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &Handle
);
143 if (!EFI_ERROR (Status
)) {
144 if (Handle
== PreviousHandle
) {
146 // If no forward progress is made try invoking the Dispatcher.
147 // A new FV may have been added to the system an new drivers
149 // Status == EFI_SUCCESS means a driver was dispatched
150 // Status == EFI_NOT_FOUND means no new drivers were dispatched
152 if (CurrentTpl
== 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
;
164 if (!EFI_ERROR (Status
)) {
165 PreviousHandle
= Handle
;
167 // Connect all drivers that apply to Handle and RemainingDevicePath,
168 // the Recursive flag is FALSE so only one level will be expanded.
170 // If ConnectController fails to find a driver, then still give the chance to
171 // do dispatch, because partial 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 Status
= gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
181 if (Status
== EFI_NOT_FOUND
) {
182 Status
= EFI_SUCCESS
;
184 if (MatchingHandle
!= NULL
) {
185 *MatchingHandle
= Handle
;
190 // Loop until RemainingDevicePath is an empty device path
192 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
));
194 ASSERT (EFI_ERROR (Status
) || IsDevicePathEnd (RemainingDevicePath
));
200 This function will disconnect all current system handles.
202 gBS->DisconnectController() is invoked for each handle exists in system handle buffer.
203 If handle is a bus type handle, all childrens also are disconnected recursively by
204 gBS->DisconnectController().
208 EfiBootManagerDisconnectAll (
213 EFI_HANDLE
*HandleBuffer
;
219 gBS
->LocateHandleBuffer (
226 for (Index
= 0; Index
< HandleCount
; Index
++) {
227 gBS
->DisconnectController (HandleBuffer
[Index
], NULL
, NULL
);
230 if (HandleBuffer
!= NULL
) {
231 FreePool (HandleBuffer
);
236 Connect the specific Usb device which match the short form device path,
237 and whose bus is determined by Host Controller (Uhci or Ehci).
239 @param DevicePath A short-form device path that starts with the first
240 element being a USB WWID or a USB Class device
243 @return EFI_INVALID_PARAMETER DevicePath is NULL pointer.
244 DevicePath is not a USB device path.
246 @return EFI_SUCCESS Success to connect USB device
247 @return EFI_NOT_FOUND Fail to find handle for USB controller to connect.
251 BmConnectUsbShortFormDevicePath (
252 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
259 EFI_PCI_IO_PROTOCOL
*PciIo
;
261 BOOLEAN AtLeastOneConnected
;
264 // Check the passed in parameters
266 if (DevicePath
== NULL
) {
267 return EFI_INVALID_PARAMETER
;
270 if ((DevicePathType (DevicePath
) != MESSAGING_DEVICE_PATH
) ||
271 ((DevicePathSubType (DevicePath
) != MSG_USB_CLASS_DP
) && (DevicePathSubType (DevicePath
) != MSG_USB_WWID_DP
))
273 return EFI_INVALID_PARAMETER
;
277 // Find the usb host controller firstly, then connect with the remaining device path
279 AtLeastOneConnected
= FALSE
;
280 Status
= gBS
->LocateHandleBuffer (
282 &gEfiPciIoProtocolGuid
,
287 if (!EFI_ERROR (Status
)) {
288 for (Index
= 0; Index
< HandleCount
; Index
++) {
289 Status
= gBS
->HandleProtocol (
291 &gEfiPciIoProtocolGuid
,
294 if (!EFI_ERROR (Status
)) {
296 // Check whether the Pci device is the wanted usb host controller
298 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x09, 3, &Class
);
299 if (!EFI_ERROR (Status
) &&
300 ((PCI_CLASS_SERIAL
== Class
[2]) && (PCI_CLASS_SERIAL_USB
== Class
[1]))
302 Status
= gBS
->ConnectController (
308 if (!EFI_ERROR(Status
)) {
309 AtLeastOneConnected
= TRUE
;
315 if (Handles
!= NULL
) {
320 return AtLeastOneConnected
? EFI_SUCCESS
: EFI_NOT_FOUND
;