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.
61 BdsLibGenericConnectAll (
66 // Most generic way to connect all the drivers
68 BdsLibConnectAllDriversToAllControllers ();
69 BdsLibConnectAllConsoles ();
74 This function will create all handles associate with every device
75 path node. If the handle associate with one device path node can not
76 be created success, then still give one chance to do the dispatch,
77 which load the missing drivers if possible.
79 @param DevicePathToConnect The device path which will be connected, it can be
80 a multi-instance device path
82 @retval EFI_SUCCESS All handles associate with every device path node
84 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles
85 @retval EFI_NOT_FOUND Create the handle associate with one device path
91 BdsLibConnectDevicePath (
92 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathToConnect
96 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
97 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevicePath
;
98 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
99 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
100 EFI_DEVICE_PATH_PROTOCOL
*Next
;
102 EFI_HANDLE PreviousHandle
;
105 if (DevicePathToConnect
== NULL
) {
109 DevicePath
= DuplicateDevicePath (DevicePathToConnect
);
110 CopyOfDevicePath
= DevicePath
;
111 if (DevicePath
== NULL
) {
112 return EFI_OUT_OF_RESOURCES
;
117 // The outer loop handles multi instance device paths.
118 // Only console variables contain multiple instance device paths.
120 // After this call DevicePath points to the next Instance
122 Instance
= GetNextDevicePathInstance (&DevicePath
, &Size
);
124 while (!IsDevicePathEndType (Next
)) {
125 Next
= NextDevicePathNode (Next
);
128 SetDevicePathEndNode (Next
);
131 // Start the real work of connect with RemainingDevicePath
133 PreviousHandle
= NULL
;
136 // Find the handle that best matches the Device Path. If it is only a
137 // partial match the remaining part of the device path is returned in
138 // RemainingDevicePath.
140 RemainingDevicePath
= Instance
;
141 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 Status
= gDS
->Dispatch ();
155 if (!EFI_ERROR (Status
)) {
156 PreviousHandle
= Handle
;
158 // Connect all drivers that apply to Handle and RemainingDevicePath,
159 // the Recursive flag is FALSE so only one level will be expanded.
161 // Do not check the connect status here, if the connect controller fail,
162 // then still give the chance to do dispatch, because partial
163 // RemainingDevicepath may be in the new FV
165 // 1. If the connect fail, RemainingDevicepath and handle will not
166 // change, so next time will do the dispatch, then dispatch's status
168 // 2. If the connect success, the RemainingDevicepath and handle will
169 // change, then avoid the dispatch, we have chance to continue the
172 gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
176 // Loop until RemainingDevicePath is an empty device path
178 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
));
180 } while (DevicePath
!= NULL
);
182 if (CopyOfDevicePath
!= NULL
) {
183 gBS
->FreePool (CopyOfDevicePath
);
186 // All handle with DevicePath exists in the handle database
193 This function will connect all current system handles recursively. The
194 connection will finish until every handle's child handle created if it have.
196 @retval EFI_SUCCESS All handles and it's child handle have been
198 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
203 BdsLibConnectAllEfi (
209 EFI_HANDLE
*HandleBuffer
;
212 Status
= gBS
->LocateHandleBuffer (
219 if (EFI_ERROR (Status
)) {
223 for (Index
= 0; Index
< HandleCount
; Index
++) {
224 Status
= gBS
->ConnectController (HandleBuffer
[Index
], NULL
, NULL
, TRUE
);
227 gBS
->FreePool (HandleBuffer
);
234 This function will disconnect all current system handles. The disconnection
235 will finish until every handle have been disconnected.
237 @retval EFI_SUCCESS All handles have been disconnected
238 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
243 BdsLibDisconnectAllEfi (
249 EFI_HANDLE
*HandleBuffer
;
255 Status
= gBS
->LocateHandleBuffer (
262 if (EFI_ERROR (Status
)) {
266 for (Index
= 0; Index
< HandleCount
; Index
++) {
267 Status
= gBS
->DisconnectController (HandleBuffer
[Index
], NULL
, NULL
);
270 gBS
->FreePool (HandleBuffer
);
277 Connects all drivers to all controllers.
278 This function make sure all the current system driver will manage
279 the correspoinding controllers if have. And at the same time, make
280 sure all the system controllers have driver to manage it if have.
285 BdsLibConnectAllDriversToAllControllers (
293 // Connect All EFI 1.10 drivers following EFI 1.10 algorithm
295 BdsLibConnectAllEfi ();
298 // Check to see if it's possible to dispatch an more DXE drivers.
299 // The BdsLibConnectAllEfi () may have made new DXE drivers show up.
300 // If anything is Dispatched Status == EFI_SUCCESS and we will try
301 // the connect again.
303 Status
= gDS
->Dispatch ();
305 } while (!EFI_ERROR (Status
));
311 Connect the specific Usb device which match the short form device path,
312 and whose bus is determined by Host Controller (Uhci or Ehci).
314 @param HostControllerPI Uhci (0x00) or Ehci (0x20) or Both uhci and ehci
316 @param RemainingDevicePath a short-form device path that starts with the first
317 element being a USB WWID or a USB Class device
320 @return EFI_INVALID_PARAMETER
322 @return EFI_NOT_FOUND
327 BdsLibConnectUsbDevByShortFormDP(
328 IN UINT8 HostControllerPI
,
329 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
333 EFI_HANDLE
*HandleArray
;
334 UINTN HandleArrayCount
;
336 EFI_PCI_IO_PROTOCOL
*PciIo
;
338 BOOLEAN AtLeastOneConnected
;
341 // Check the passed in parameters
343 if (RemainingDevicePath
== NULL
) {
344 return EFI_INVALID_PARAMETER
;
347 if ((DevicePathType (RemainingDevicePath
) != MESSAGING_DEVICE_PATH
) ||
348 ((DevicePathSubType (RemainingDevicePath
) != MSG_USB_CLASS_DP
)
349 && (DevicePathSubType (RemainingDevicePath
) != MSG_USB_WWID_DP
)
351 return EFI_INVALID_PARAMETER
;
354 if (HostControllerPI
!= 0xFF &&
355 HostControllerPI
!= 0x00 &&
356 HostControllerPI
!= 0x20) {
357 return EFI_INVALID_PARAMETER
;
361 // Find the usb host controller firstly, then connect with the remaining device path
363 AtLeastOneConnected
= FALSE
;
364 Status
= gBS
->LocateHandleBuffer (
366 &gEfiPciIoProtocolGuid
,
371 if (!EFI_ERROR (Status
)) {
372 for (Index
= 0; Index
< HandleArrayCount
; Index
++) {
373 Status
= gBS
->HandleProtocol (
375 &gEfiPciIoProtocolGuid
,
378 if (!EFI_ERROR (Status
)) {
380 // Check whether the Pci device is the wanted usb host controller
382 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x09, 3, &Class
);
383 if (!EFI_ERROR (Status
)) {
384 if ((PCI_CLASS_SERIAL
== Class
[2]) &&
385 (PCI_CLASS_SERIAL_USB
== Class
[1])) {
386 if (HostControllerPI
== Class
[0] || HostControllerPI
== 0xFF) {
387 Status
= gBS
->ConnectController (
393 if (!EFI_ERROR(Status
)) {
394 AtLeastOneConnected
= TRUE
;
402 if (AtLeastOneConnected
) {
407 return EFI_NOT_FOUND
;