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