]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/GenericBdsLib/BdsConnect.c
Clean up GenericBdsLib library Instance.
[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 None
56
57 @return None
58
59 **/
60 VOID
61 BdsLibGenericConnectAll (
62 VOID
63 )
64 {
65 //
66 // Most generic way to connect all the drivers
67 //
68 BdsLibConnectAllDriversToAllControllers ();
69 BdsLibConnectAllConsoles ();
70 }
71
72
73 /**
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.
78
79 @param DevicePathToConnect The device path which will be connected, it can be
80 a multi-instance device path
81
82 @retval EFI_SUCCESS All handles associate with every device path node
83 have been created
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
86 node failed
87
88 **/
89 EFI_STATUS
90 EFIAPI
91 BdsLibConnectDevicePath (
92 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathToConnect
93 )
94 {
95 EFI_STATUS Status;
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;
101 EFI_HANDLE Handle;
102 EFI_HANDLE PreviousHandle;
103 UINTN Size;
104
105 if (DevicePathToConnect == NULL) {
106 return EFI_SUCCESS;
107 }
108
109 DevicePath = DuplicateDevicePath (DevicePathToConnect);
110 CopyOfDevicePath = DevicePath;
111 if (DevicePath == NULL) {
112 return EFI_OUT_OF_RESOURCES;
113 }
114
115 do {
116 //
117 // The outer loop handles multi instance device paths.
118 // Only console variables contain multiple instance device paths.
119 //
120 // After this call DevicePath points to the next Instance
121 //
122 Instance = GetNextDevicePathInstance (&DevicePath, &Size);
123 Next = Instance;
124 while (!IsDevicePathEndType (Next)) {
125 Next = NextDevicePathNode (Next);
126 }
127
128 SetDevicePathEndNode (Next);
129
130 //
131 // Start the real work of connect with RemainingDevicePath
132 //
133 PreviousHandle = NULL;
134 do {
135 //
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.
139 //
140 RemainingDevicePath = Instance;
141 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);
142
143 if (!EFI_ERROR (Status)) {
144 if (Handle == PreviousHandle) {
145 //
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
148 // may now be found.
149 // Status == EFI_SUCCESS means a driver was dispatched
150 // Status == EFI_NOT_FOUND means no new drivers were dispatched
151 //
152 Status = gDS->Dispatch ();
153 }
154
155 if (!EFI_ERROR (Status)) {
156 PreviousHandle = Handle;
157 //
158 // Connect all drivers that apply to Handle and RemainingDevicePath,
159 // the Recursive flag is FALSE so only one level will be expanded.
160 //
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
164 //
165 // 1. If the connect fail, RemainingDevicepath and handle will not
166 // change, so next time will do the dispatch, then dispatch's status
167 // will take effect
168 // 2. If the connect success, the RemainingDevicepath and handle will
169 // change, then avoid the dispatch, we have chance to continue the
170 // next connection
171 //
172 gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);
173 }
174 }
175 //
176 // Loop until RemainingDevicePath is an empty device path
177 //
178 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));
179
180 } while (DevicePath != NULL);
181
182 if (CopyOfDevicePath != NULL) {
183 gBS->FreePool (CopyOfDevicePath);
184 }
185 //
186 // All handle with DevicePath exists in the handle database
187 //
188 return Status;
189 }
190
191
192 /**
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.
195
196 @retval EFI_SUCCESS All handles and it's child handle have been
197 connected
198 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
199
200 **/
201 EFI_STATUS
202 EFIAPI
203 BdsLibConnectAllEfi (
204 VOID
205 )
206 {
207 EFI_STATUS Status;
208 UINTN HandleCount;
209 EFI_HANDLE *HandleBuffer;
210 UINTN Index;
211
212 Status = gBS->LocateHandleBuffer (
213 AllHandles,
214 NULL,
215 NULL,
216 &HandleCount,
217 &HandleBuffer
218 );
219 if (EFI_ERROR (Status)) {
220 return Status;
221 }
222
223 for (Index = 0; Index < HandleCount; Index++) {
224 Status = gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);
225 }
226
227 gBS->FreePool (HandleBuffer);
228
229 return EFI_SUCCESS;
230 }
231
232
233 /**
234 This function will disconnect all current system handles. The disconnection
235 will finish until every handle have been disconnected.
236
237 @retval EFI_SUCCESS All handles have been disconnected
238 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
239
240 **/
241 EFI_STATUS
242 EFIAPI
243 BdsLibDisconnectAllEfi (
244 VOID
245 )
246 {
247 EFI_STATUS Status;
248 UINTN HandleCount;
249 EFI_HANDLE *HandleBuffer;
250 UINTN Index;
251
252 //
253 // Disconnect all
254 //
255 Status = gBS->LocateHandleBuffer (
256 AllHandles,
257 NULL,
258 NULL,
259 &HandleCount,
260 &HandleBuffer
261 );
262 if (EFI_ERROR (Status)) {
263 return Status;
264 }
265
266 for (Index = 0; Index < HandleCount; Index++) {
267 Status = gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);
268 }
269
270 gBS->FreePool (HandleBuffer);
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
321 @return EFI_SUCCESS
322 @return EFI_NOT_FOUND
323
324 **/
325 EFI_STATUS
326 EFIAPI
327 BdsLibConnectUsbDevByShortFormDP(
328 IN UINT8 HostControllerPI,
329 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
330 )
331 {
332 EFI_STATUS Status;
333 EFI_HANDLE *HandleArray;
334 UINTN HandleArrayCount;
335 UINTN Index;
336 EFI_PCI_IO_PROTOCOL *PciIo;
337 UINT8 Class[3];
338 BOOLEAN AtLeastOneConnected;
339
340 //
341 // Check the passed in parameters
342 //
343 if (RemainingDevicePath == NULL) {
344 return EFI_INVALID_PARAMETER;
345 }
346
347 if ((DevicePathType (RemainingDevicePath) != MESSAGING_DEVICE_PATH) ||
348 ((DevicePathSubType (RemainingDevicePath) != MSG_USB_CLASS_DP)
349 && (DevicePathSubType (RemainingDevicePath) != MSG_USB_WWID_DP)
350 )) {
351 return EFI_INVALID_PARAMETER;
352 }
353
354 if (HostControllerPI != 0xFF &&
355 HostControllerPI != 0x00 &&
356 HostControllerPI != 0x20) {
357 return EFI_INVALID_PARAMETER;
358 }
359
360 //
361 // Find the usb host controller firstly, then connect with the remaining device path
362 //
363 AtLeastOneConnected = FALSE;
364 Status = gBS->LocateHandleBuffer (
365 ByProtocol,
366 &gEfiPciIoProtocolGuid,
367 NULL,
368 &HandleArrayCount,
369 &HandleArray
370 );
371 if (!EFI_ERROR (Status)) {
372 for (Index = 0; Index < HandleArrayCount; Index++) {
373 Status = gBS->HandleProtocol (
374 HandleArray[Index],
375 &gEfiPciIoProtocolGuid,
376 (VOID **)&PciIo
377 );
378 if (!EFI_ERROR (Status)) {
379 //
380 // Check whether the Pci device is the wanted usb host controller
381 //
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 (
388 HandleArray[Index],
389 NULL,
390 RemainingDevicePath,
391 FALSE
392 );
393 if (!EFI_ERROR(Status)) {
394 AtLeastOneConnected = TRUE;
395 }
396 }
397 }
398 }
399 }
400 }
401
402 if (AtLeastOneConnected) {
403 return EFI_SUCCESS;
404 }
405 }
406
407 return EFI_NOT_FOUND;
408 }