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