]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/GenericBdsLib/BdsConnect.c
Clean up GenericBdsLib library Instance.
[mirror_edk2.git] / MdeModulePkg / Library / GenericBdsLib / BdsConnect.c
CommitLineData
897f0eee 1/** @file\r
2 BDS Lib functions which relate with connect the device\r
3\r
4Copyright (c) 2004 - 2008, Intel Corporation. <BR>\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "InternalBdsLib.h"\r
16\r
17\r
18/**\r
19 This function will connect all the system driver to controller\r
20 first, and then special connect the default console, this make\r
21 sure all the system controller avialbe and the platform default\r
22 console connected.\r
23\r
24**/\r
25VOID\r
26EFIAPI\r
27BdsLibConnectAll (\r
28 VOID\r
29 )\r
30{\r
31 //\r
32 // Connect the platform console first\r
33 //\r
34 BdsLibConnectAllDefaultConsoles ();\r
35\r
36 //\r
37 // Generic way to connect all the drivers\r
38 //\r
39 BdsLibConnectAllDriversToAllControllers ();\r
40\r
41 //\r
42 // Here we have the assumption that we have already had\r
43 // platform default console\r
44 //\r
45 BdsLibConnectAllDefaultConsoles ();\r
46}\r
47\r
48\r
49/**\r
50 This function will connect all the system drivers to all controllers\r
51 first, and then connect all the console devices the system current\r
52 have. After this we should get all the device work and console avariable\r
53 if the system have console device.\r
54\r
55 None\r
56\r
57 @return None\r
58\r
59**/\r
60VOID\r
61BdsLibGenericConnectAll (\r
62 VOID\r
63 )\r
64{\r
65 //\r
66 // Most generic way to connect all the drivers\r
67 //\r
68 BdsLibConnectAllDriversToAllControllers ();\r
69 BdsLibConnectAllConsoles ();\r
70}\r
71\r
72\r
73/**\r
74 This function will create all handles associate with every device\r
75 path node. If the handle associate with one device path node can not\r
76 be created success, then still give one chance to do the dispatch,\r
77 which load the missing drivers if possible.\r
78\r
79 @param DevicePathToConnect The device path which will be connected, it can be\r
80 a multi-instance device path\r
81\r
82 @retval EFI_SUCCESS All handles associate with every device path node\r
83 have been created\r
84 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles\r
85 @retval EFI_NOT_FOUND Create the handle associate with one device path\r
86 node failed\r
87\r
88**/\r
89EFI_STATUS\r
90EFIAPI\r
91BdsLibConnectDevicePath (\r
92 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathToConnect\r
93 )\r
94{\r
95 EFI_STATUS Status;\r
96 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
97 EFI_DEVICE_PATH_PROTOCOL *CopyOfDevicePath;\r
98 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
99 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;\r
100 EFI_DEVICE_PATH_PROTOCOL *Next;\r
101 EFI_HANDLE Handle;\r
102 EFI_HANDLE PreviousHandle;\r
103 UINTN Size;\r
104\r
105 if (DevicePathToConnect == NULL) {\r
106 return EFI_SUCCESS;\r
107 }\r
108\r
109 DevicePath = DuplicateDevicePath (DevicePathToConnect);\r
110 CopyOfDevicePath = DevicePath;\r
111 if (DevicePath == NULL) {\r
112 return EFI_OUT_OF_RESOURCES;\r
113 }\r
114\r
115 do {\r
116 //\r
117 // The outer loop handles multi instance device paths.\r
118 // Only console variables contain multiple instance device paths.\r
119 //\r
120 // After this call DevicePath points to the next Instance\r
121 //\r
122 Instance = GetNextDevicePathInstance (&DevicePath, &Size);\r
123 Next = Instance;\r
124 while (!IsDevicePathEndType (Next)) {\r
125 Next = NextDevicePathNode (Next);\r
126 }\r
127\r
128 SetDevicePathEndNode (Next);\r
129\r
130 //\r
131 // Start the real work of connect with RemainingDevicePath\r
132 //\r
133 PreviousHandle = NULL;\r
134 do {\r
135 //\r
136 // Find the handle that best matches the Device Path. If it is only a\r
137 // partial match the remaining part of the device path is returned in\r
138 // RemainingDevicePath.\r
139 //\r
140 RemainingDevicePath = Instance;\r
141 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);\r
142\r
143 if (!EFI_ERROR (Status)) {\r
144 if (Handle == PreviousHandle) {\r
145 //\r
146 // If no forward progress is made try invoking the Dispatcher.\r
147 // A new FV may have been added to the system an new drivers\r
148 // may now be found.\r
149 // Status == EFI_SUCCESS means a driver was dispatched\r
150 // Status == EFI_NOT_FOUND means no new drivers were dispatched\r
151 //\r
152 Status = gDS->Dispatch ();\r
153 }\r
154\r
155 if (!EFI_ERROR (Status)) {\r
156 PreviousHandle = Handle;\r
157 //\r
158 // Connect all drivers that apply to Handle and RemainingDevicePath,\r
159 // the Recursive flag is FALSE so only one level will be expanded.\r
160 //\r
161 // Do not check the connect status here, if the connect controller fail,\r
162 // then still give the chance to do dispatch, because partial\r
163 // RemainingDevicepath may be in the new FV\r
164 //\r
165 // 1. If the connect fail, RemainingDevicepath and handle will not\r
166 // change, so next time will do the dispatch, then dispatch's status\r
167 // will take effect\r
168 // 2. If the connect success, the RemainingDevicepath and handle will\r
169 // change, then avoid the dispatch, we have chance to continue the\r
170 // next connection\r
171 //\r
172 gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);\r
173 }\r
174 }\r
175 //\r
176 // Loop until RemainingDevicePath is an empty device path\r
177 //\r
178 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));\r
179\r
180 } while (DevicePath != NULL);\r
181\r
182 if (CopyOfDevicePath != NULL) {\r
183 gBS->FreePool (CopyOfDevicePath);\r
184 }\r
185 //\r
186 // All handle with DevicePath exists in the handle database\r
187 //\r
188 return Status;\r
189}\r
190\r
191\r
192/**\r
193 This function will connect all current system handles recursively. The\r
194 connection will finish until every handle's child handle created if it have.\r
195\r
196 @retval EFI_SUCCESS All handles and it's child handle have been\r
197 connected\r
198 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().\r
199\r
200**/\r
201EFI_STATUS\r
202EFIAPI\r
203BdsLibConnectAllEfi (\r
204 VOID\r
205 )\r
206{\r
207 EFI_STATUS Status;\r
208 UINTN HandleCount;\r
209 EFI_HANDLE *HandleBuffer;\r
210 UINTN Index;\r
211\r
212 Status = gBS->LocateHandleBuffer (\r
213 AllHandles,\r
214 NULL,\r
215 NULL,\r
216 &HandleCount,\r
217 &HandleBuffer\r
218 );\r
219 if (EFI_ERROR (Status)) {\r
220 return Status;\r
221 }\r
222\r
223 for (Index = 0; Index < HandleCount; Index++) {\r
224 Status = gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);\r
225 }\r
226\r
227 gBS->FreePool (HandleBuffer);\r
228\r
229 return EFI_SUCCESS;\r
230}\r
231\r
232\r
233/**\r
234 This function will disconnect all current system handles. The disconnection\r
235 will finish until every handle have been disconnected.\r
236\r
237 @retval EFI_SUCCESS All handles have been disconnected\r
238 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().\r
239\r
240**/\r
241EFI_STATUS\r
242EFIAPI\r
243BdsLibDisconnectAllEfi (\r
244 VOID\r
245 )\r
246{\r
247 EFI_STATUS Status;\r
248 UINTN HandleCount;\r
249 EFI_HANDLE *HandleBuffer;\r
250 UINTN Index;\r
251\r
252 //\r
253 // Disconnect all\r
254 //\r
255 Status = gBS->LocateHandleBuffer (\r
256 AllHandles,\r
257 NULL,\r
258 NULL,\r
259 &HandleCount,\r
260 &HandleBuffer\r
261 );\r
262 if (EFI_ERROR (Status)) {\r
263 return Status;\r
264 }\r
265\r
266 for (Index = 0; Index < HandleCount; Index++) {\r
267 Status = gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);\r
268 }\r
269\r
270 gBS->FreePool (HandleBuffer);\r
271\r
272 return EFI_SUCCESS;\r
273}\r
274\r
275\r
276/**\r
277 Connects all drivers to all controllers.\r
278 This function make sure all the current system driver will manage\r
279 the correspoinding controllers if have. And at the same time, make\r
280 sure all the system controllers have driver to manage it if have.\r
281\r
282**/\r
283VOID\r
284EFIAPI\r
285BdsLibConnectAllDriversToAllControllers (\r
286 VOID\r
287 )\r
288{\r
289 EFI_STATUS Status;\r
290\r
291 do {\r
292 //\r
293 // Connect All EFI 1.10 drivers following EFI 1.10 algorithm\r
294 //\r
295 BdsLibConnectAllEfi ();\r
296\r
297 //\r
298 // Check to see if it's possible to dispatch an more DXE drivers.\r
299 // The BdsLibConnectAllEfi () may have made new DXE drivers show up.\r
300 // If anything is Dispatched Status == EFI_SUCCESS and we will try\r
301 // the connect again.\r
302 //\r
303 Status = gDS->Dispatch ();\r
304\r
305 } while (!EFI_ERROR (Status));\r
306\r
307}\r
308\r
309\r
310/**\r
311 Connect the specific Usb device which match the short form device path,\r
11ef23f9 312 and whose bus is determined by Host Controller (Uhci or Ehci).\r
897f0eee 313\r
314 @param HostControllerPI Uhci (0x00) or Ehci (0x20) or Both uhci and ehci\r
315 (0xFF)\r
316 @param RemainingDevicePath a short-form device path that starts with the first\r
317 element being a USB WWID or a USB Class device\r
318 path\r
319\r
320 @return EFI_INVALID_PARAMETER\r
321 @return EFI_SUCCESS\r
322 @return EFI_NOT_FOUND\r
323\r
324**/\r
325EFI_STATUS\r
326EFIAPI\r
327BdsLibConnectUsbDevByShortFormDP(\r
328 IN UINT8 HostControllerPI,\r
329 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
330 )\r
331{\r
332 EFI_STATUS Status;\r
333 EFI_HANDLE *HandleArray;\r
334 UINTN HandleArrayCount;\r
335 UINTN Index;\r
336 EFI_PCI_IO_PROTOCOL *PciIo;\r
337 UINT8 Class[3];\r
338 BOOLEAN AtLeastOneConnected;\r
339\r
340 //\r
341 // Check the passed in parameters\r
342 //\r
343 if (RemainingDevicePath == NULL) {\r
344 return EFI_INVALID_PARAMETER;\r
345 }\r
346\r
347 if ((DevicePathType (RemainingDevicePath) != MESSAGING_DEVICE_PATH) ||\r
348 ((DevicePathSubType (RemainingDevicePath) != MSG_USB_CLASS_DP)\r
349 && (DevicePathSubType (RemainingDevicePath) != MSG_USB_WWID_DP)\r
350 )) {\r
351 return EFI_INVALID_PARAMETER;\r
352 }\r
353\r
354 if (HostControllerPI != 0xFF &&\r
355 HostControllerPI != 0x00 &&\r
356 HostControllerPI != 0x20) {\r
357 return EFI_INVALID_PARAMETER;\r
358 }\r
359\r
360 //\r
361 // Find the usb host controller firstly, then connect with the remaining device path\r
362 //\r
363 AtLeastOneConnected = FALSE;\r
364 Status = gBS->LocateHandleBuffer (\r
365 ByProtocol,\r
366 &gEfiPciIoProtocolGuid,\r
367 NULL,\r
368 &HandleArrayCount,\r
369 &HandleArray\r
370 );\r
371 if (!EFI_ERROR (Status)) {\r
372 for (Index = 0; Index < HandleArrayCount; Index++) {\r
373 Status = gBS->HandleProtocol (\r
374 HandleArray[Index],\r
375 &gEfiPciIoProtocolGuid,\r
376 (VOID **)&PciIo\r
377 );\r
378 if (!EFI_ERROR (Status)) {\r
379 //\r
380 // Check whether the Pci device is the wanted usb host controller\r
381 //\r
382 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, 0x09, 3, &Class);\r
383 if (!EFI_ERROR (Status)) {\r
384 if ((PCI_CLASS_SERIAL == Class[2]) &&\r
385 (PCI_CLASS_SERIAL_USB == Class[1])) {\r
386 if (HostControllerPI == Class[0] || HostControllerPI == 0xFF) {\r
387 Status = gBS->ConnectController (\r
388 HandleArray[Index],\r
389 NULL,\r
390 RemainingDevicePath,\r
391 FALSE\r
392 );\r
393 if (!EFI_ERROR(Status)) {\r
394 AtLeastOneConnected = TRUE;\r
395 }\r
396 }\r
397 }\r
398 }\r
399 }\r
400 }\r
401\r
402 if (AtLeastOneConnected) {\r
403 return EFI_SUCCESS;\r
404 }\r
405 }\r
406\r
407 return EFI_NOT_FOUND;\r
408}\r