]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsConnect.c
7d0254d33c810d9ebc5be85edf16d01a5f379c6d
[mirror_edk2.git] / IntelFrameworkModulePkg / 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 available 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 available
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 if (DevicePath == NULL) {
107 return EFI_OUT_OF_RESOURCES;
108 }
109 CopyOfDevicePath = DevicePath;
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 if (Instance == NULL) {
120 FreePool (CopyOfDevicePath);
121 return EFI_OUT_OF_RESOURCES;
122 }
123
124 Next = Instance;
125 while (!IsDevicePathEndType (Next)) {
126 Next = NextDevicePathNode (Next);
127 }
128
129 SetDevicePathEndNode (Next);
130
131 //
132 // Start the real work of connect with RemainingDevicePath
133 //
134 PreviousHandle = NULL;
135 do {
136 //
137 // Find the handle that best matches the Device Path. If it is only a
138 // partial match the remaining part of the device path is returned in
139 // RemainingDevicePath.
140 //
141 RemainingDevicePath = Instance;
142 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);
143
144 if (!EFI_ERROR (Status)) {
145 if (Handle == PreviousHandle) {
146 //
147 // If no forward progress is made try invoking the Dispatcher.
148 // A new FV may have been added to the system an new drivers
149 // may now be found.
150 // Status == EFI_SUCCESS means a driver was dispatched
151 // Status == EFI_NOT_FOUND means no new drivers were dispatched
152 //
153 Status = gDS->Dispatch ();
154 }
155
156 if (!EFI_ERROR (Status)) {
157 PreviousHandle = Handle;
158 //
159 // Connect all drivers that apply to Handle and RemainingDevicePath,
160 // the Recursive flag is FALSE so only one level will be expanded.
161 //
162 // Do not check the connect status here, if the connect controller fail,
163 // then still give the chance to do dispatch, because partial
164 // RemainingDevicepath may be in the new FV
165 //
166 // 1. If the connect fail, RemainingDevicepath and handle will not
167 // change, so next time will do the dispatch, then dispatch's status
168 // will take effect
169 // 2. If the connect success, the RemainingDevicepath and handle will
170 // change, then avoid the dispatch, we have chance to continue the
171 // next connection
172 //
173 gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);
174 }
175 }
176 //
177 // Loop until RemainingDevicePath is an empty device path
178 //
179 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));
180
181 } while (DevicePath != NULL);
182
183 if (CopyOfDevicePath != NULL) {
184 FreePool (CopyOfDevicePath);
185 }
186 //
187 // All handle with DevicePath exists in the handle database
188 //
189 return Status;
190 }
191
192
193 /**
194 This function will connect all current system handles recursively. The
195 connection will finish until every handle's child handle created if it have.
196
197 @retval EFI_SUCCESS All handles and it's child handle have been
198 connected
199 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
200
201 **/
202 EFI_STATUS
203 EFIAPI
204 BdsLibConnectAllEfi (
205 VOID
206 )
207 {
208 EFI_STATUS Status;
209 UINTN HandleCount;
210 EFI_HANDLE *HandleBuffer;
211 UINTN Index;
212
213 Status = gBS->LocateHandleBuffer (
214 AllHandles,
215 NULL,
216 NULL,
217 &HandleCount,
218 &HandleBuffer
219 );
220 if (EFI_ERROR (Status)) {
221 return Status;
222 }
223
224 for (Index = 0; Index < HandleCount; Index++) {
225 Status = gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);
226 }
227
228 if (HandleBuffer != NULL) {
229 FreePool (HandleBuffer);
230 }
231
232 return EFI_SUCCESS;
233 }
234
235
236 /**
237 This function will disconnect all current system handles. The disconnection
238 will finish until every handle have been disconnected.
239
240 @retval EFI_SUCCESS All handles have been disconnected
241 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().
242
243 **/
244 EFI_STATUS
245 EFIAPI
246 BdsLibDisconnectAllEfi (
247 VOID
248 )
249 {
250 EFI_STATUS Status;
251 UINTN HandleCount;
252 EFI_HANDLE *HandleBuffer;
253 UINTN Index;
254
255 //
256 // Disconnect all
257 //
258 Status = gBS->LocateHandleBuffer (
259 AllHandles,
260 NULL,
261 NULL,
262 &HandleCount,
263 &HandleBuffer
264 );
265 if (EFI_ERROR (Status)) {
266 return Status;
267 }
268
269 for (Index = 0; Index < HandleCount; Index++) {
270 Status = gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);
271 }
272
273 if (HandleBuffer != NULL) {
274 FreePool (HandleBuffer);
275 }
276
277 return EFI_SUCCESS;
278 }
279
280
281 /**
282 Connects all drivers to all controllers.
283 This function make sure all the current system driver will manage
284 the correspoinding controllers if have. And at the same time, make
285 sure all the system controllers have driver to manage it if have.
286
287 **/
288 VOID
289 EFIAPI
290 BdsLibConnectAllDriversToAllControllers (
291 VOID
292 )
293 {
294 EFI_STATUS Status;
295
296 do {
297 //
298 // Connect All EFI 1.10 drivers following EFI 1.10 algorithm
299 //
300 BdsLibConnectAllEfi ();
301
302 //
303 // Check to see if it's possible to dispatch an more DXE drivers.
304 // The BdsLibConnectAllEfi () may have made new DXE drivers show up.
305 // If anything is Dispatched Status == EFI_SUCCESS and we will try
306 // the connect again.
307 //
308 Status = gDS->Dispatch ();
309
310 } while (!EFI_ERROR (Status));
311
312 }
313
314
315 /**
316 Connect the specific Usb device which match the short form device path,
317 and whose bus is determined by Host Controller (Uhci or Ehci).
318
319 @param HostControllerPI Uhci (0x00) or Ehci (0x20) or Both uhci and ehci
320 (0xFF)
321 @param RemainingDevicePath a short-form device path that starts with the first
322 element being a USB WWID or a USB Class device
323 path
324
325 @return EFI_INVALID_PARAMETER RemainingDevicePath is NULL pointer.
326 RemainingDevicePath is not a USB device path.
327 Invalid HostControllerPI type.
328 @return EFI_SUCCESS Success to connect USB device
329 @return EFI_NOT_FOUND Fail to find handle for USB controller to connect.
330
331 **/
332 EFI_STATUS
333 EFIAPI
334 BdsLibConnectUsbDevByShortFormDP(
335 IN UINT8 HostControllerPI,
336 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
337 )
338 {
339 EFI_STATUS Status;
340 EFI_HANDLE *HandleArray;
341 UINTN HandleArrayCount;
342 UINTN Index;
343 EFI_PCI_IO_PROTOCOL *PciIo;
344 UINT8 Class[3];
345 BOOLEAN AtLeastOneConnected;
346
347 //
348 // Check the passed in parameters
349 //
350 if (RemainingDevicePath == NULL) {
351 return EFI_INVALID_PARAMETER;
352 }
353
354 if ((DevicePathType (RemainingDevicePath) != MESSAGING_DEVICE_PATH) ||
355 ((DevicePathSubType (RemainingDevicePath) != MSG_USB_CLASS_DP)
356 && (DevicePathSubType (RemainingDevicePath) != MSG_USB_WWID_DP)
357 )) {
358 return EFI_INVALID_PARAMETER;
359 }
360
361 if (HostControllerPI != 0xFF &&
362 HostControllerPI != 0x00 &&
363 HostControllerPI != 0x20) {
364 return EFI_INVALID_PARAMETER;
365 }
366
367 //
368 // Find the usb host controller firstly, then connect with the remaining device path
369 //
370 AtLeastOneConnected = FALSE;
371 Status = gBS->LocateHandleBuffer (
372 ByProtocol,
373 &gEfiPciIoProtocolGuid,
374 NULL,
375 &HandleArrayCount,
376 &HandleArray
377 );
378 if (!EFI_ERROR (Status)) {
379 for (Index = 0; Index < HandleArrayCount; Index++) {
380 Status = gBS->HandleProtocol (
381 HandleArray[Index],
382 &gEfiPciIoProtocolGuid,
383 (VOID **)&PciIo
384 );
385 if (!EFI_ERROR (Status)) {
386 //
387 // Check whether the Pci device is the wanted usb host controller
388 //
389 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, 0x09, 3, &Class);
390 if (!EFI_ERROR (Status)) {
391 if ((PCI_CLASS_SERIAL == Class[2]) &&
392 (PCI_CLASS_SERIAL_USB == Class[1])) {
393 if (HostControllerPI == Class[0] || HostControllerPI == 0xFF) {
394 Status = gBS->ConnectController (
395 HandleArray[Index],
396 NULL,
397 RemainingDevicePath,
398 FALSE
399 );
400 if (!EFI_ERROR(Status)) {
401 AtLeastOneConnected = TRUE;
402 }
403 }
404 }
405 }
406 }
407 }
408
409 if (AtLeastOneConnected) {
410 return EFI_SUCCESS;
411 }
412 }
413
414 return EFI_NOT_FOUND;
415 }