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