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