3 Copyright (c) 2004 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 BDS Lib functions which relate with connect the device
23 #include "InternalBdsLib.h"
27 This function will connect all the system driver to controller
28 first, and then special connect the default console, this make
29 sure all the system controller avialbe and the platform default
43 // Connect the platform console first
45 BdsLibConnectAllDefaultConsoles ();
48 // Generic way to connect all the drivers
50 BdsLibConnectAllDriversToAllControllers ();
53 // Here we have the assumption that we have already had
54 // platform default console
56 BdsLibConnectAllDefaultConsoles ();
61 This function will connect all the system drivers to all controllers
62 first, and then connect all the console devices the system current
63 have. After this we should get all the device work and console avariable
64 if the system have console device.
72 BdsLibGenericConnectAll (
77 // Most generic way to connect all the drivers
79 BdsLibConnectAllDriversToAllControllers ();
80 BdsLibConnectAllConsoles ();
85 This function will create all handles associate with every device
86 path node. If the handle associate with one device path node can not
87 be created success, then still give one chance to do the dispatch,
88 which load the missing drivers if possible.
90 @param DevicePathToConnect The device path which will be connected, it can be
91 a multi-instance device path
93 @retval EFI_SUCCESS All handles associate with every device path node
95 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles
96 @retval EFI_NOT_FOUND Create the handle associate with one device path
101 BdsLibConnectDevicePath (
102 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathToConnect
106 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
107 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevicePath
;
108 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
109 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
110 EFI_DEVICE_PATH_PROTOCOL
*Next
;
112 EFI_HANDLE PreviousHandle
;
115 if (DevicePathToConnect
== NULL
) {
119 DevicePath
= DuplicateDevicePath (DevicePathToConnect
);
120 CopyOfDevicePath
= DevicePath
;
121 if (DevicePath
== NULL
) {
122 return EFI_OUT_OF_RESOURCES
;
127 // The outer loop handles multi instance device paths.
128 // Only console variables contain multiple instance device paths.
130 // After this call DevicePath points to the next Instance
132 Instance
= GetNextDevicePathInstance (&DevicePath
, &Size
);
134 while (!IsDevicePathEndType (Next
)) {
135 Next
= NextDevicePathNode (Next
);
138 SetDevicePathEndNode (Next
);
141 // Start the real work of connect with RemainingDevicePath
143 PreviousHandle
= NULL
;
146 // Find the handle that best matches the Device Path. If it is only a
147 // partial match the remaining part of the device path is returned in
148 // RemainingDevicePath.
150 RemainingDevicePath
= Instance
;
151 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &Handle
);
153 if (!EFI_ERROR (Status
)) {
154 if (Handle
== PreviousHandle
) {
156 // If no forward progress is made try invoking the Dispatcher.
157 // A new FV may have been added to the system an new drivers
159 // Status == EFI_SUCCESS means a driver was dispatched
160 // Status == EFI_NOT_FOUND means no new drivers were dispatched
162 Status
= gDS
->Dispatch ();
165 if (!EFI_ERROR (Status
)) {
166 PreviousHandle
= Handle
;
168 // Connect all drivers that apply to Handle and RemainingDevicePath,
169 // the Recursive flag is FALSE so only one level will be expanded.
171 // Do not check the connect status here, if the connect controller fail,
172 // then still give the chance to do dispatch, because partial
173 // RemainingDevicepath may be in the new FV
175 // 1. If the connect fail, RemainingDevicepath and handle will not
176 // change, so next time will do the dispatch, then dispatch's status
178 // 2. If the connect success, the RemainingDevicepath and handle will
179 // change, then avoid the dispatch, we have chance to continue the
182 gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
186 // Loop until RemainingDevicePath is an empty device path
188 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
));
190 } while (DevicePath
!= NULL
);
192 if (CopyOfDevicePath
!= NULL
) {
193 gBS
->FreePool (CopyOfDevicePath
);
196 // All handle with DevicePath exists in the handle database
203 This function will connect all current system handles recursively. The
204 connection will finish until every handle's child handle created if it have.
208 @retval EFI_SUCCESS All handles and it's child handle have been
210 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
214 BdsLibConnectAllEfi (
220 EFI_HANDLE
*HandleBuffer
;
223 Status
= gBS
->LocateHandleBuffer (
230 if (EFI_ERROR (Status
)) {
234 for (Index
= 0; Index
< HandleCount
; Index
++) {
235 Status
= gBS
->ConnectController (HandleBuffer
[Index
], NULL
, NULL
, TRUE
);
238 gBS
->FreePool (HandleBuffer
);
245 This function will disconnect all current system handles. The disconnection
246 will finish until every handle have been disconnected.
250 @retval EFI_SUCCESS All handles have been disconnected
251 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
255 BdsLibDisconnectAllEfi (
261 EFI_HANDLE
*HandleBuffer
;
267 Status
= gBS
->LocateHandleBuffer (
274 if (EFI_ERROR (Status
)) {
278 for (Index
= 0; Index
< HandleCount
; Index
++) {
279 Status
= gBS
->DisconnectController (HandleBuffer
[Index
], NULL
, NULL
);
282 gBS
->FreePool (HandleBuffer
);
289 Connects all drivers to all controllers.
290 This function make sure all the current system driver will manage
291 the correspoinding controllers if have. And at the same time, make
292 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
337 @return EFI_NOT_FOUND
341 BdsLibConnectUsbDevByShortFormDP(
342 IN CHAR8 HostControllerPI
,
343 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
347 EFI_HANDLE
*HandleArray
;
348 UINTN HandleArrayCount
;
350 EFI_PCI_IO_PROTOCOL
*PciIo
;
352 BOOLEAN AtLeastOneConnected
;
355 // Check the passed in parameters
357 if (RemainingDevicePath
== NULL
) {
358 return EFI_INVALID_PARAMETER
;
361 if ((DevicePathType (RemainingDevicePath
) != MESSAGING_DEVICE_PATH
) ||
362 ((DevicePathSubType (RemainingDevicePath
) != MSG_USB_CLASS_DP
)
363 && (DevicePathSubType (RemainingDevicePath
) != MSG_USB_WWID_DP
)
365 return EFI_INVALID_PARAMETER
;
368 if (HostControllerPI
!= 0xFF &&
369 HostControllerPI
!= 0x00 &&
370 HostControllerPI
!= 0x20) {
371 return EFI_INVALID_PARAMETER
;
375 // Find the usb host controller firstly, then connect with the remaining device path
377 AtLeastOneConnected
= FALSE
;
378 Status
= gBS
->LocateHandleBuffer (
380 &gEfiPciIoProtocolGuid
,
385 if (!EFI_ERROR (Status
)) {
386 for (Index
= 0; Index
< HandleArrayCount
; Index
++) {
387 Status
= gBS
->HandleProtocol (
389 &gEfiPciIoProtocolGuid
,
392 if (!EFI_ERROR (Status
)) {
394 // Check whether the Pci device is the wanted usb host controller
396 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x09, 3, &Class
);
397 if (!EFI_ERROR (Status
)) {
398 if ((PCI_CLASS_SERIAL
== Class
[2]) &&
399 (PCI_CLASS_SERIAL_USB
== Class
[1])) {
400 if (HostControllerPI
== Class
[0] || HostControllerPI
== 0xFF) {
401 Status
= gBS
->ConnectController (
407 if (!EFI_ERROR(Status
)) {
408 AtLeastOneConnected
= TRUE
;
416 if (AtLeastOneConnected
) {
421 return EFI_NOT_FOUND
;