]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/GenericBdsLib/BdsConnect.c
4c721e07db5e9c26fb01f0d24033b69af7145398
[mirror_edk2.git] / MdeModulePkg / Library / GenericBdsLib / BdsConnect.c
1 /** @file
2 BDS Lib functions which relate with connect the device
3
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
9
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.
12
13 **/
14
15 #include "InternalBdsLib.h"
16
17
18 /**
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
22 console connected.
23
24 **/
25 VOID
26 EFIAPI
27 BdsLibConnectAll (
28 VOID
29 )
30 {
31 //
32 // Connect the platform console first
33 //
34 BdsLibConnectAllDefaultConsoles ();
35
36 //
37 // Generic way to connect all the drivers
38 //
39 BdsLibConnectAllDriversToAllControllers ();
40
41 //
42 // Here we have the assumption that we have already had
43 // platform default console
44 //
45 BdsLibConnectAllDefaultConsoles ();
46 }
47
48
49 /**
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.
54
55 **/
56 VOID
57 BdsLibGenericConnectAll (
58 VOID
59 )
60 {
61 //
62 // Most generic way to connect all the drivers
63 //
64 BdsLibConnectAllDriversToAllControllers ();
65 BdsLibConnectAllConsoles ();
66 }
67
68
69 /**
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.
74
75 @param DevicePathToConnect The device path which will be connected, it can be
76 a multi-instance device path
77
78 @retval EFI_SUCCESS All handles associate with every device path node
79 have been created
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
82 node failed
83
84 **/
85 EFI_STATUS
86 EFIAPI
87 BdsLibConnectDevicePath (
88 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathToConnect
89 )
90 {
91 EFI_STATUS Status;
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;
97 EFI_HANDLE Handle;
98 EFI_HANDLE PreviousHandle;
99 UINTN Size;
100
101 if (DevicePathToConnect == NULL) {
102 return EFI_SUCCESS;
103 }
104
105 DevicePath = DuplicateDevicePath (DevicePathToConnect);
106 CopyOfDevicePath = DevicePath;
107 if (DevicePath == NULL) {
108 return EFI_OUT_OF_RESOURCES;
109 }
110
111 do {
112 //
113 // The outer loop handles multi instance device paths.
114 // Only console variables contain multiple instance device paths.
115 //
116 // After this call DevicePath points to the next Instance
117 //
118 Instance = GetNextDevicePathInstance (&DevicePath, &Size);
119 Next = Instance;
120 while (!IsDevicePathEndType (Next)) {
121 Next = NextDevicePathNode (Next);
122 }
123
124 SetDevicePathEndNode (Next);
125
126 //
127 // Start the real work of connect with RemainingDevicePath
128 //
129 PreviousHandle = NULL;
130 do {
131 //
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.
135 //
136 RemainingDevicePath = Instance;
137 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);
138
139 if (!EFI_ERROR (Status)) {
140 if (Handle == PreviousHandle) {
141 //
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
144 // may now be found.
145 // Status == EFI_SUCCESS means a driver was dispatched
146 // Status == EFI_NOT_FOUND means no new drivers were dispatched
147 //
148 Status = gDS->Dispatch ();
149 }
150
151 if (!EFI_ERROR (Status)) {
152 PreviousHandle = Handle;
153 //
154 // Connect all drivers that apply to Handle and RemainingDevicePath,
155 // the Recursive flag is FALSE so only one level will be expanded.
156 //
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
160 //
161 // 1. If the connect fail, RemainingDevicepath and handle will not
162 // change, so next time will do the dispatch, then dispatch's status
163 // will take effect
164 // 2. If the connect success, the RemainingDevicepath and handle will
165 // change, then avoid the dispatch, we have chance to continue the
166 // next connection
167 //
168 gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);
169 }
170 }
171 //
172 // Loop until RemainingDevicePath is an empty device path
173 //
174 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));
175
176 } while (DevicePath != NULL);
177
178 if (CopyOfDevicePath != NULL) {
179 FreePool (CopyOfDevicePath);
180 }
181 //
182 // All handle with DevicePath exists in the handle database
183 //
184 return Status;
185 }
186
187
188 /**
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.
191
192 @retval EFI_SUCCESS All handles and it's child handle have been
193 connected
194 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
195
196 **/
197 EFI_STATUS
198 EFIAPI
199 BdsLibConnectAllEfi (
200 VOID
201 )
202 {
203 EFI_STATUS Status;
204 UINTN HandleCount;
205 EFI_HANDLE *HandleBuffer;
206 UINTN Index;
207
208 Status = gBS->LocateHandleBuffer (
209 AllHandles,
210 NULL,
211 NULL,
212 &HandleCount,
213 &HandleBuffer
214 );
215 if (EFI_ERROR (Status)) {
216 return Status;
217 }
218
219 for (Index = 0; Index < HandleCount; Index++) {
220 Status = gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);
221 }
222
223 if (HandleBuffer != NULL) {
224 FreePool (HandleBuffer);
225 }
226
227 return EFI_SUCCESS;
228 }
229
230
231 /**
232 This function will disconnect all current system handles. The disconnection
233 will finish until every handle have been disconnected.
234
235 @retval EFI_SUCCESS All handles have been disconnected
236 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
237
238 **/
239 EFI_STATUS
240 EFIAPI
241 BdsLibDisconnectAllEfi (
242 VOID
243 )
244 {
245 EFI_STATUS Status;
246 UINTN HandleCount;
247 EFI_HANDLE *HandleBuffer;
248 UINTN Index;
249
250 //
251 // Disconnect all
252 //
253 Status = gBS->LocateHandleBuffer (
254 AllHandles,
255 NULL,
256 NULL,
257 &HandleCount,
258 &HandleBuffer
259 );
260 if (EFI_ERROR (Status)) {
261 return Status;
262 }
263
264 for (Index = 0; Index < HandleCount; Index++) {
265 Status = gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);
266 }
267
268 if (HandleBuffer != NULL) {
269 FreePool (HandleBuffer);
270 }
271
272 return EFI_SUCCESS;
273 }
274
275
276 /**
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.
281
282 **/
283 VOID
284 EFIAPI
285 BdsLibConnectAllDriversToAllControllers (
286 VOID
287 )
288 {
289 EFI_STATUS Status;
290
291 do {
292 //
293 // Connect All EFI 1.10 drivers following EFI 1.10 algorithm
294 //
295 BdsLibConnectAllEfi ();
296
297 //
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.
302 //
303 Status = gDS->Dispatch ();
304
305 } while (!EFI_ERROR (Status));
306
307 }
308
309
310 /**
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).
313
314 @param HostControllerPI Uhci (0x00) or Ehci (0x20) or Both uhci and ehci
315 (0xFF)
316 @param RemainingDevicePath a short-form device path that starts with the first
317 element being a USB WWID or a USB Class device
318 path
319
320 @return EFI_INVALID_PARAMETER RemainingDevicePath is NULL pointer.
321 RemainingDevicePath is not a USB device path.
322 Invalid HostControllerPI type.
323 @return EFI_SUCCESS Sucess to connect USB device
324 @return EFI_NOT_FOUND Fail to find hanlde for USB controller to connect.
325
326 **/
327 EFI_STATUS
328 EFIAPI
329 BdsLibConnectUsbDevByShortFormDP(
330 IN UINT8 HostControllerPI,
331 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
332 )
333 {
334 EFI_STATUS Status;
335 EFI_HANDLE *HandleArray;
336 UINTN HandleArrayCount;
337 UINTN Index;
338 EFI_PCI_IO_PROTOCOL *PciIo;
339 UINT8 Class[3];
340 BOOLEAN AtLeastOneConnected;
341
342 //
343 // Check the passed in parameters
344 //
345 if (RemainingDevicePath == NULL) {
346 return EFI_INVALID_PARAMETER;
347 }
348
349 if ((DevicePathType (RemainingDevicePath) != MESSAGING_DEVICE_PATH) ||
350 ((DevicePathSubType (RemainingDevicePath) != MSG_USB_CLASS_DP)
351 && (DevicePathSubType (RemainingDevicePath) != MSG_USB_WWID_DP)
352 )) {
353 return EFI_INVALID_PARAMETER;
354 }
355
356 if (HostControllerPI != 0xFF &&
357 HostControllerPI != 0x00 &&
358 HostControllerPI != 0x20) {
359 return EFI_INVALID_PARAMETER;
360 }
361
362 //
363 // Find the usb host controller firstly, then connect with the remaining device path
364 //
365 AtLeastOneConnected = FALSE;
366 Status = gBS->LocateHandleBuffer (
367 ByProtocol,
368 &gEfiPciIoProtocolGuid,
369 NULL,
370 &HandleArrayCount,
371 &HandleArray
372 );
373 if (!EFI_ERROR (Status)) {
374 for (Index = 0; Index < HandleArrayCount; Index++) {
375 Status = gBS->HandleProtocol (
376 HandleArray[Index],
377 &gEfiPciIoProtocolGuid,
378 (VOID **)&PciIo
379 );
380 if (!EFI_ERROR (Status)) {
381 //
382 // Check whether the Pci device is the wanted usb host controller
383 //
384 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, 0x09, 3, &Class);
385 if (!EFI_ERROR (Status)) {
386 if ((PCI_CLASS_SERIAL == Class[2]) &&
387 (PCI_CLASS_SERIAL_USB == Class[1])) {
388 if (HostControllerPI == Class[0] || HostControllerPI == 0xFF) {
389 Status = gBS->ConnectController (
390 HandleArray[Index],
391 NULL,
392 RemainingDevicePath,
393 FALSE
394 );
395 if (!EFI_ERROR(Status)) {
396 AtLeastOneConnected = TRUE;
397 }
398 }
399 }
400 }
401 }
402 }
403
404 if (AtLeastOneConnected) {
405 return EFI_SUCCESS;
406 }
407 }
408
409 return EFI_NOT_FOUND;
410 }