]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/GenericBdsLib/BdsConnect.c
Remove SafeFreePool from MemoryAllocationLib as this API's name is misleading. Its...
[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
897f0eee 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 CopyOfDevicePath = DevicePath;\r
107 if (DevicePath == NULL) {\r
108 return EFI_OUT_OF_RESOURCES;\r
109 }\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 Next = Instance;\r
120 while (!IsDevicePathEndType (Next)) {\r
121 Next = NextDevicePathNode (Next);\r
122 }\r
123\r
124 SetDevicePathEndNode (Next);\r
125\r
126 //\r
127 // Start the real work of connect with RemainingDevicePath\r
128 //\r
129 PreviousHandle = NULL;\r
130 do {\r
131 //\r
132 // Find the handle that best matches the Device Path. If it is only a\r
133 // partial match the remaining part of the device path is returned in\r
134 // RemainingDevicePath.\r
135 //\r
136 RemainingDevicePath = Instance;\r
137 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);\r
138\r
139 if (!EFI_ERROR (Status)) {\r
140 if (Handle == PreviousHandle) {\r
141 //\r
142 // If no forward progress is made try invoking the Dispatcher.\r
143 // A new FV may have been added to the system an new drivers\r
144 // may now be found.\r
145 // Status == EFI_SUCCESS means a driver was dispatched\r
146 // Status == EFI_NOT_FOUND means no new drivers were dispatched\r
147 //\r
148 Status = gDS->Dispatch ();\r
149 }\r
150\r
151 if (!EFI_ERROR (Status)) {\r
152 PreviousHandle = Handle;\r
153 //\r
154 // Connect all drivers that apply to Handle and RemainingDevicePath,\r
155 // the Recursive flag is FALSE so only one level will be expanded.\r
156 //\r
157 // Do not check the connect status here, if the connect controller fail,\r
158 // then still give the chance to do dispatch, because partial\r
159 // RemainingDevicepath may be in the new FV\r
160 //\r
161 // 1. If the connect fail, RemainingDevicepath and handle will not\r
162 // change, so next time will do the dispatch, then dispatch's status\r
163 // will take effect\r
164 // 2. If the connect success, the RemainingDevicepath and handle will\r
165 // change, then avoid the dispatch, we have chance to continue the\r
166 // next connection\r
167 //\r
168 gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);\r
169 }\r
170 }\r
171 //\r
172 // Loop until RemainingDevicePath is an empty device path\r
173 //\r
174 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));\r
175\r
176 } while (DevicePath != NULL);\r
177\r
178 if (CopyOfDevicePath != NULL) {\r
676df92c 179 FreePool (CopyOfDevicePath);\r
897f0eee 180 }\r
181 //\r
182 // All handle with DevicePath exists in the handle database\r
183 //\r
184 return Status;\r
185}\r
186\r
187\r
188/**\r
189 This function will connect all current system handles recursively. The\r
190 connection will finish until every handle's child handle created if it have.\r
191\r
192 @retval EFI_SUCCESS All handles and it's child handle have been\r
193 connected\r
194 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().\r
195\r
196**/\r
197EFI_STATUS\r
198EFIAPI\r
199BdsLibConnectAllEfi (\r
200 VOID\r
201 )\r
202{\r
203 EFI_STATUS Status;\r
204 UINTN HandleCount;\r
205 EFI_HANDLE *HandleBuffer;\r
206 UINTN Index;\r
207\r
208 Status = gBS->LocateHandleBuffer (\r
209 AllHandles,\r
210 NULL,\r
211 NULL,\r
212 &HandleCount,\r
213 &HandleBuffer\r
214 );\r
215 if (EFI_ERROR (Status)) {\r
216 return Status;\r
217 }\r
218\r
219 for (Index = 0; Index < HandleCount; Index++) {\r
220 Status = gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);\r
221 }\r
222\r
676df92c 223 if (HandleBuffer != NULL) {\r
224 FreePool (HandleBuffer);\r
225 }\r
897f0eee 226\r
227 return EFI_SUCCESS;\r
228}\r
229\r
230\r
231/**\r
232 This function will disconnect all current system handles. The disconnection\r
233 will finish until every handle have been disconnected.\r
234\r
235 @retval EFI_SUCCESS All handles have been disconnected\r
236 @retval EFI_STATUS Return the status of gBS->LocateHandleBuffer().\r
237\r
238**/\r
239EFI_STATUS\r
240EFIAPI\r
241BdsLibDisconnectAllEfi (\r
242 VOID\r
243 )\r
244{\r
245 EFI_STATUS Status;\r
246 UINTN HandleCount;\r
247 EFI_HANDLE *HandleBuffer;\r
248 UINTN Index;\r
249\r
250 //\r
251 // Disconnect all\r
252 //\r
253 Status = gBS->LocateHandleBuffer (\r
254 AllHandles,\r
255 NULL,\r
256 NULL,\r
257 &HandleCount,\r
258 &HandleBuffer\r
259 );\r
260 if (EFI_ERROR (Status)) {\r
261 return Status;\r
262 }\r
263\r
264 for (Index = 0; Index < HandleCount; Index++) {\r
265 Status = gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);\r
266 }\r
267\r
676df92c 268 if (HandleBuffer != NULL) {\r
269 FreePool (HandleBuffer);\r
270 }\r
897f0eee 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