2 BDS Lib functions which relate with connect the device
4 Copyright (c) 2004 - 2008, Intel Corporation. <BR>
5 All rights reserved. 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 "InternalBdsLib.h"
19 This function will connect all the system driver to controller
20 first, and then special connect the default console, this make
21 sure all the system controller avialbe and the platform default
32 // Connect the platform console first
34 BdsLibConnectAllDefaultConsoles ();
37 // Generic way to connect all the drivers
39 BdsLibConnectAllDriversToAllControllers ();
42 // Here we have the assumption that we have already had
43 // platform default console
45 BdsLibConnectAllDefaultConsoles ();
50 This function will connect all the system drivers to all controllers
51 first, and then connect all the console devices the system current
52 have. After this we should get all the device work and console avariable
53 if the system have console device.
57 BdsLibGenericConnectAll (
62 // Most generic way to connect all the drivers
64 BdsLibConnectAllDriversToAllControllers ();
65 BdsLibConnectAllConsoles ();
70 This function will create all handles associate with every device
71 path node. If the handle associate with one device path node can not
72 be created success, then still give one chance to do the dispatch,
73 which load the missing drivers if possible.
75 @param DevicePathToConnect The device path which will be connected, it can be
76 a multi-instance device path
78 @retval EFI_SUCCESS All handles associate with every device path node
80 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles
81 @retval EFI_NOT_FOUND Create the handle associate with one device path
87 BdsLibConnectDevicePath (
88 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathToConnect
92 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
93 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevicePath
;
94 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
95 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
96 EFI_DEVICE_PATH_PROTOCOL
*Next
;
98 EFI_HANDLE PreviousHandle
;
101 if (DevicePathToConnect
== NULL
) {
105 DevicePath
= DuplicateDevicePath (DevicePathToConnect
);
106 CopyOfDevicePath
= DevicePath
;
107 if (DevicePath
== NULL
) {
108 return EFI_OUT_OF_RESOURCES
;
113 // The outer loop handles multi instance device paths.
114 // Only console variables contain multiple instance device paths.
116 // After this call DevicePath points to the next Instance
118 Instance
= GetNextDevicePathInstance (&DevicePath
, &Size
);
120 while (!IsDevicePathEndType (Next
)) {
121 Next
= NextDevicePathNode (Next
);
124 SetDevicePathEndNode (Next
);
127 // Start the real work of connect with RemainingDevicePath
129 PreviousHandle
= NULL
;
132 // Find the handle that best matches the Device Path. If it is only a
133 // partial match the remaining part of the device path is returned in
134 // RemainingDevicePath.
136 RemainingDevicePath
= Instance
;
137 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &Handle
);
139 if (!EFI_ERROR (Status
)) {
140 if (Handle
== PreviousHandle
) {
142 // If no forward progress is made try invoking the Dispatcher.
143 // A new FV may have been added to the system an new drivers
145 // Status == EFI_SUCCESS means a driver was dispatched
146 // Status == EFI_NOT_FOUND means no new drivers were dispatched
148 Status
= gDS
->Dispatch ();
151 if (!EFI_ERROR (Status
)) {
152 PreviousHandle
= Handle
;
154 // Connect all drivers that apply to Handle and RemainingDevicePath,
155 // the Recursive flag is FALSE so only one level will be expanded.
157 // Do not check the connect status here, if the connect controller fail,
158 // then still give the chance to do dispatch, because partial
159 // RemainingDevicepath may be in the new FV
161 // 1. If the connect fail, RemainingDevicepath and handle will not
162 // change, so next time will do the dispatch, then dispatch's status
164 // 2. If the connect success, the RemainingDevicepath and handle will
165 // change, then avoid the dispatch, we have chance to continue the
168 gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
172 // Loop until RemainingDevicePath is an empty device path
174 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
));
176 } while (DevicePath
!= NULL
);
178 if (CopyOfDevicePath
!= NULL
) {
179 SafeFreePool (CopyOfDevicePath
);
182 // All handle with DevicePath exists in the handle database
189 This function will connect all current system handles recursively. The
190 connection will finish until every handle's child handle created if it have.
192 @retval EFI_SUCCESS All handles and it's child handle have been
194 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
199 BdsLibConnectAllEfi (
205 EFI_HANDLE
*HandleBuffer
;
208 Status
= gBS
->LocateHandleBuffer (
215 if (EFI_ERROR (Status
)) {
219 for (Index
= 0; Index
< HandleCount
; Index
++) {
220 Status
= gBS
->ConnectController (HandleBuffer
[Index
], NULL
, NULL
, TRUE
);
223 SafeFreePool (HandleBuffer
);
230 This function will disconnect all current system handles. The disconnection
231 will finish until every handle have been disconnected.
233 @retval EFI_SUCCESS All handles have been disconnected
234 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
239 BdsLibDisconnectAllEfi (
245 EFI_HANDLE
*HandleBuffer
;
251 Status
= gBS
->LocateHandleBuffer (
258 if (EFI_ERROR (Status
)) {
262 for (Index
= 0; Index
< HandleCount
; Index
++) {
263 Status
= gBS
->DisconnectController (HandleBuffer
[Index
], NULL
, NULL
);
266 SafeFreePool (HandleBuffer
);
273 Connects all drivers to all controllers.
274 This function make sure all the current system driver will manage
275 the correspoinding controllers if have. And at the same time, make
276 sure all the system controllers have driver to manage it if have.
281 BdsLibConnectAllDriversToAllControllers (
289 // Connect All EFI 1.10 drivers following EFI 1.10 algorithm
291 BdsLibConnectAllEfi ();
294 // Check to see if it's possible to dispatch an more DXE drivers.
295 // The BdsLibConnectAllEfi () may have made new DXE drivers show up.
296 // If anything is Dispatched Status == EFI_SUCCESS and we will try
297 // the connect again.
299 Status
= gDS
->Dispatch ();
301 } while (!EFI_ERROR (Status
));
307 Connect the specific Usb device which match the short form device path,
308 and whose bus is determined by Host Controller (Uhci or Ehci).
310 @param HostControllerPI Uhci (0x00) or Ehci (0x20) or Both uhci and ehci
312 @param RemainingDevicePath a short-form device path that starts with the first
313 element being a USB WWID or a USB Class device
316 @return EFI_INVALID_PARAMETER
318 @return EFI_NOT_FOUND
323 BdsLibConnectUsbDevByShortFormDP(
324 IN UINT8 HostControllerPI
,
325 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
329 EFI_HANDLE
*HandleArray
;
330 UINTN HandleArrayCount
;
332 EFI_PCI_IO_PROTOCOL
*PciIo
;
334 BOOLEAN AtLeastOneConnected
;
337 // Check the passed in parameters
339 if (RemainingDevicePath
== NULL
) {
340 return EFI_INVALID_PARAMETER
;
343 if ((DevicePathType (RemainingDevicePath
) != MESSAGING_DEVICE_PATH
) ||
344 ((DevicePathSubType (RemainingDevicePath
) != MSG_USB_CLASS_DP
)
345 && (DevicePathSubType (RemainingDevicePath
) != MSG_USB_WWID_DP
)
347 return EFI_INVALID_PARAMETER
;
350 if (HostControllerPI
!= 0xFF &&
351 HostControllerPI
!= 0x00 &&
352 HostControllerPI
!= 0x20) {
353 return EFI_INVALID_PARAMETER
;
357 // Find the usb host controller firstly, then connect with the remaining device path
359 AtLeastOneConnected
= FALSE
;
360 Status
= gBS
->LocateHandleBuffer (
362 &gEfiPciIoProtocolGuid
,
367 if (!EFI_ERROR (Status
)) {
368 for (Index
= 0; Index
< HandleArrayCount
; Index
++) {
369 Status
= gBS
->HandleProtocol (
371 &gEfiPciIoProtocolGuid
,
374 if (!EFI_ERROR (Status
)) {
376 // Check whether the Pci device is the wanted usb host controller
378 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x09, 3, &Class
);
379 if (!EFI_ERROR (Status
)) {
380 if ((PCI_CLASS_SERIAL
== Class
[2]) &&
381 (PCI_CLASS_SERIAL_USB
== Class
[1])) {
382 if (HostControllerPI
== Class
[0] || HostControllerPI
== 0xFF) {
383 Status
= gBS
->ConnectController (
389 if (!EFI_ERROR(Status
)) {
390 AtLeastOneConnected
= TRUE
;
398 if (AtLeastOneConnected
) {
403 return EFI_NOT_FOUND
;