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