]> code.delx.au - refind/blob - EfiLib/BdsConnect.c
TianoCore build support; new use_graphics_for refind.conf token
[refind] / EfiLib / BdsConnect.c
1 /** @file
2 BDS Lib functions which relate with connect the device
3
4 Copyright (c) 2004 - 2008, 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 "Platform.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 success, then still give one chance to do the dispatch,
73 which load the missing drivers if possible.
74
75 @param DevicePathToConnect The device path which will be connected, it can be
76 a multi-instance device path
77
78 @retval EFI_SUCCESS All handles associate with every device path node
79 have been created
80 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles
81 @retval EFI_NOT_FOUND Create the handle associate with one device path
82 node failed
83
84 **/
85 EFI_STATUS
86 EFIAPI
87 BdsLibConnectDevicePath (
88 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathToConnect
89 )
90 {
91 EFI_STATUS Status;
92 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
93 EFI_DEVICE_PATH_PROTOCOL *CopyOfDevicePath;
94 EFI_DEVICE_PATH_PROTOCOL *Instance;
95 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
96 EFI_DEVICE_PATH_PROTOCOL *Next;
97 EFI_HANDLE Handle;
98 EFI_HANDLE PreviousHandle;
99 UINTN Size;
100
101 if (DevicePathToConnect == NULL) {
102 return EFI_SUCCESS;
103 }
104
105 DevicePath = DuplicateDevicePath (DevicePathToConnect);
106 if (DevicePath == NULL) {
107 return EFI_OUT_OF_RESOURCES;
108 }
109 CopyOfDevicePath = DevicePath;
110
111 do {
112 //
113 // The outer loop handles multi instance device paths.
114 // Only console variables contain multiple instance device paths.
115 //
116 // After this call DevicePath points to the next Instance
117 //
118 Instance = GetNextDevicePathInstance (&DevicePath, &Size);
119 if (Instance == NULL) {
120 FreePool (CopyOfDevicePath);
121 return EFI_OUT_OF_RESOURCES;
122 }
123
124 Next = Instance;
125 while (!IsDevicePathEndType (Next)) {
126 Next = NextDevicePathNode (Next);
127 }
128
129 SetDevicePathEndNode (Next);
130
131 //
132 // Start the real work of connect with RemainingDevicePath
133 //
134 PreviousHandle = NULL;
135 do {
136 //
137 // Find the handle that best matches the Device Path. If it is only a
138 // partial match the remaining part of the device path is returned in
139 // RemainingDevicePath.
140 //
141 RemainingDevicePath = Instance;
142 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);
143
144 if (!EFI_ERROR (Status)) {
145 if (Handle == PreviousHandle) {
146 //
147 // If no forward progress is made try invoking the Dispatcher.
148 // A new FV may have been added to the system an new drivers
149 // may now be found.
150 // Status == EFI_SUCCESS means a driver was dispatched
151 // Status == EFI_NOT_FOUND means no new drivers were dispatched
152 //
153 Status = gDS->Dispatch ();
154 }
155
156 if (!EFI_ERROR (Status)) {
157 PreviousHandle = Handle;
158 //
159 // Connect all drivers that apply to Handle and RemainingDevicePath,
160 // the Recursive flag is FALSE so only one level will be expanded.
161 //
162 // Do not check the connect status here, if the connect controller fail,
163 // then still give the chance to do dispatch, because partial
164 // RemainingDevicepath may be in the new FV
165 //
166 // 1. If the connect fail, RemainingDevicepath and handle will not
167 // change, so next time will do the dispatch, then dispatch's status
168 // will take effect
169 // 2. If the connect success, the RemainingDevicepath and handle will
170 // change, then avoid the dispatch, we have chance to continue the
171 // next connection
172 //
173 gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);
174 }
175 }
176 //
177 // Loop until RemainingDevicePath is an empty device path
178 //
179 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));
180
181 } while (DevicePath != NULL);
182
183 if (CopyOfDevicePath != NULL) {
184 FreePool (CopyOfDevicePath);
185 }
186 //
187 // All handle with DevicePath exists in the handle database
188 //
189 return Status;
190 }
191
192
193 /**
194 This function will connect all current system handles recursively.
195
196 gBS->ConnectController() service is invoked for each handle exist in system handler buffer.
197 If the handle is bus type handler, all childrens also will be connected recursively
198 by gBS->ConnectController().
199
200 @retval EFI_SUCCESS All handles and it's child handle have been connected
201 @retval EFI_STATUS Error status returned by of gBS->LocateHandleBuffer().
202
203 **/
204 EFI_STATUS
205 EFIAPI
206 BdsLibConnectAllEfi (
207 VOID
208 )
209 {
210 EFI_STATUS Status;
211 UINTN HandleCount;
212 EFI_HANDLE *HandleBuffer;
213 UINTN Index;
214
215 Status = gBS->LocateHandleBuffer (
216 AllHandles,
217 NULL,
218 NULL,
219 &HandleCount,
220 &HandleBuffer
221 );
222 if (EFI_ERROR (Status)) {
223 return Status;
224 }
225
226 for (Index = 0; Index < HandleCount; Index++) {
227 Status = gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);
228 }
229
230 if (HandleBuffer != NULL) {
231 FreePool (HandleBuffer);
232 }
233
234 return EFI_SUCCESS;
235 }
236
237 /**
238 This function will disconnect all current system handles.
239
240 gBS->DisconnectController() is invoked for each handle exists in system handle buffer.
241 If handle is a bus type handle, all childrens also are disconnected recursively by
242 gBS->DisconnectController().
243
244 @retval EFI_SUCCESS All handles have been disconnected
245 @retval EFI_STATUS Error status returned by of gBS->LocateHandleBuffer().
246
247 **/
248 EFI_STATUS
249 EFIAPI
250 BdsLibDisconnectAllEfi (
251 VOID
252 )
253 {
254 EFI_STATUS Status;
255 UINTN HandleCount;
256 EFI_HANDLE *HandleBuffer;
257 UINTN Index;
258
259 //
260 // Disconnect all
261 //
262 Status = gBS->LocateHandleBuffer (
263 AllHandles,
264 NULL,
265 NULL,
266 &HandleCount,
267 &HandleBuffer
268 );
269 if (EFI_ERROR (Status)) {
270 return Status;
271 }
272
273 for (Index = 0; Index < HandleCount; Index++) {
274 Status = gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);
275 }
276
277 if (HandleBuffer != NULL) {
278 FreePool (HandleBuffer);
279 }
280
281 return EFI_SUCCESS;
282 }
283
284 EFI_STATUS ScanDeviceHandles(EFI_HANDLE ControllerHandle,
285 UINTN *HandleCount,
286 EFI_HANDLE **HandleBuffer,
287 UINT32 **HandleType)
288 {
289 EFI_STATUS Status;
290 UINTN HandleIndex;
291 EFI_GUID **ProtocolGuidArray;
292 UINTN ArrayCount;
293 UINTN ProtocolIndex;
294 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
295 UINTN OpenInfoCount;
296 UINTN OpenInfoIndex;
297 UINTN ChildIndex;
298
299 *HandleCount = 0;
300 *HandleBuffer = NULL;
301 *HandleType = NULL;
302
303 //
304 // Retrieve the list of all handles from the handle database
305 //
306 Status = gBS->LocateHandleBuffer (AllHandles, NULL, NULL, HandleCount, HandleBuffer);
307 if (EFI_ERROR (Status)) goto Error;
308
309 *HandleType = AllocatePool (*HandleCount * sizeof (UINT32));
310 if (*HandleType == NULL) goto Error;
311
312 for (HandleIndex = 0; HandleIndex < *HandleCount; HandleIndex++) {
313 (*HandleType)[HandleIndex] = EFI_HANDLE_TYPE_UNKNOWN;
314 //
315 // Retrieve the list of all the protocols on each handle
316 //
317 Status = gBS->ProtocolsPerHandle (
318 (*HandleBuffer)[HandleIndex],
319 &ProtocolGuidArray,
320 &ArrayCount
321 );
322 if (!EFI_ERROR (Status)) {
323 for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++)
324 {
325
326 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiLoadedImageProtocolGuid))
327 {
328 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_IMAGE_HANDLE;
329 }
330
331 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverBindingProtocolGuid))
332 {
333 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_BINDING_HANDLE;
334 }
335
336 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverConfigurationProtocolGuid))
337 {
338 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_CONFIGURATION_HANDLE;
339 }
340
341 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverDiagnosticsProtocolGuid))
342 {
343 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_DIAGNOSTICS_HANDLE;
344 }
345
346 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiComponentName2ProtocolGuid))
347 {
348 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_COMPONENT_NAME_HANDLE;
349 }
350
351 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiComponentNameProtocolGuid) )
352 {
353 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_COMPONENT_NAME_HANDLE;
354 }
355
356 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDevicePathProtocolGuid))
357 {
358 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DEVICE_HANDLE;
359 }
360
361 //
362 // Retrieve the list of agents that have opened each protocol
363 //
364 Status = gBS->OpenProtocolInformation (
365 (*HandleBuffer)[HandleIndex],
366 ProtocolGuidArray[ProtocolIndex],
367 &OpenInfo,
368 &OpenInfoCount
369 );
370 if (!EFI_ERROR (Status)) {
371
372 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
373
374 if (OpenInfo[OpenInfoIndex].ControllerHandle == ControllerHandle)
375 {
376 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER)
377 {
378 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++)
379 {
380 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle)
381 {
382 (*HandleType)[ChildIndex] |= EFI_HANDLE_TYPE_DEVICE_DRIVER;
383 }
384 }
385 }
386
387 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
388 {
389 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_PARENT_HANDLE;
390 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++)
391 {
392 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle)
393 {
394 (*HandleType)[ChildIndex] |= EFI_HANDLE_TYPE_BUS_DRIVER;
395 }
396 }
397 }
398 }
399 }
400
401 FreePool (OpenInfo);
402 }
403 }
404
405 FreePool (ProtocolGuidArray);
406 }
407 }
408
409 return EFI_SUCCESS;
410
411 Error:
412 if (*HandleType != NULL) {
413 FreePool (*HandleType);
414 }
415
416 if (*HandleBuffer != NULL) {
417 FreePool (*HandleBuffer);
418 }
419
420 *HandleCount = 0;
421 *HandleBuffer = NULL;
422 *HandleType = NULL;
423
424 return Status;
425 }
426
427
428
429 EFI_STATUS BdsLibConnectMostlyAllEfi()
430 {
431 EFI_STATUS Status;
432 UINTN AllHandleCount;
433 EFI_HANDLE *AllHandleBuffer;
434 UINTN Index;
435 UINTN HandleCount;
436 EFI_HANDLE *HandleBuffer;
437 UINT32 *HandleType;
438 UINTN HandleIndex;
439 BOOLEAN Parent;
440 BOOLEAN Device;
441 EFI_PCI_IO_PROTOCOL* PciIo;
442 PCI_TYPE00 Pci;
443
444
445 Status = gBS->LocateHandleBuffer (AllHandles, NULL, NULL, &AllHandleCount, &AllHandleBuffer);
446 if (CheckError(Status, L"locating handle buffer"))
447 return Status;
448
449 for (Index = 0; Index < AllHandleCount; Index++)
450 {
451 Status = ScanDeviceHandles(AllHandleBuffer[Index], &HandleCount, &HandleBuffer, &HandleType);
452
453 if (EFI_ERROR (Status))
454 goto Done;
455
456 Device = TRUE;
457
458 if (HandleType[Index] & EFI_HANDLE_TYPE_DRIVER_BINDING_HANDLE)
459 Device = FALSE;
460 if (HandleType[Index] & EFI_HANDLE_TYPE_IMAGE_HANDLE)
461 Device = FALSE;
462
463 if (Device)
464 {
465 Parent = FALSE;
466 for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++)
467 {
468 if (HandleType[HandleIndex] & EFI_HANDLE_TYPE_PARENT_HANDLE)
469 Parent = TRUE;
470 }
471
472 if (!Parent)
473 {
474 if (HandleType[Index] & EFI_HANDLE_TYPE_DEVICE_HANDLE)
475 {
476 Status = gBS->HandleProtocol (AllHandleBuffer[Index], &gEfiPciIoProtocolGuid, (VOID*)&PciIo);
477 if (!EFI_ERROR (Status))
478 {
479 Status = PciIo->Pci.Read (PciIo,EfiPciIoWidthUint32, 0, sizeof (Pci) / sizeof (UINT32), &Pci);
480 if (!EFI_ERROR (Status))
481 {
482 if(IS_PCI_VGA(&Pci)==TRUE)
483 {
484 gBS->DisconnectController(AllHandleBuffer[Index], NULL, NULL);
485 }
486 }
487 }
488 Status = gBS->ConnectController(AllHandleBuffer[Index], NULL, NULL, TRUE);
489 }
490 }
491 }
492
493 FreePool (HandleBuffer);
494 FreePool (HandleType);
495 }
496
497 Done:
498 FreePool (AllHandleBuffer);
499 return Status;
500 }
501
502
503
504 /**
505 Connects all drivers to all controllers.
506 This function make sure all the current system driver will manage
507 the correspoinding controllers if have. And at the same time, make
508 sure all the system controllers have driver to manage it if have.
509
510 **/
511 VOID
512 EFIAPI
513 BdsLibConnectAllDriversToAllControllers (
514 VOID
515 )
516 {
517 EFI_STATUS Status;
518
519 do {
520 //
521 // Connect All EFI 1.10 drivers following EFI 1.10 algorithm
522 //
523 //BdsLibConnectAllEfi ();
524 BdsLibConnectMostlyAllEfi ();
525
526 //
527 // Check to see if it's possible to dispatch an more DXE drivers.
528 // The BdsLibConnectAllEfi () may have made new DXE drivers show up.
529 // If anything is Dispatched Status == EFI_SUCCESS and we will try
530 // the connect again.
531 //
532 Status = gDS->Dispatch ();
533
534 } while (!EFI_ERROR (Status));
535
536 }
537
538
539 /**
540 Connect the specific Usb device which match the short form device path,
541 and whose bus is determined by Host Controller (Uhci or Ehci).
542
543 @param HostControllerPI Uhci (0x00) or Ehci (0x20) or Both uhci and ehci
544 (0xFF)
545 @param RemainingDevicePath a short-form device path that starts with the first
546 element being a USB WWID or a USB Class device
547 path
548
549 @return EFI_INVALID_PARAMETER RemainingDevicePath is NULL pointer.
550 RemainingDevicePath is not a USB device path.
551 Invalid HostControllerPI type.
552 @return EFI_SUCCESS Success to connect USB device
553 @return EFI_NOT_FOUND Fail to find handle for USB controller to connect.
554
555 **/
556 EFI_STATUS
557 EFIAPI
558 BdsLibConnectUsbDevByShortFormDP(
559 IN UINT8 HostControllerPI,
560 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
561 )
562 {
563 EFI_STATUS Status;
564 EFI_HANDLE *HandleArray;
565 UINTN HandleArrayCount;
566 UINTN Index;
567 EFI_PCI_IO_PROTOCOL *PciIo;
568 UINT8 Class[3];
569 BOOLEAN AtLeastOneConnected;
570
571 //
572 // Check the passed in parameters
573 //
574 if (RemainingDevicePath == NULL) {
575 return EFI_INVALID_PARAMETER;
576 }
577
578 if ((DevicePathType (RemainingDevicePath) != MESSAGING_DEVICE_PATH) ||
579 ((DevicePathSubType (RemainingDevicePath) != MSG_USB_CLASS_DP)
580 && (DevicePathSubType (RemainingDevicePath) != MSG_USB_WWID_DP)
581 )) {
582 return EFI_INVALID_PARAMETER;
583 }
584
585 if (HostControllerPI != 0xFF &&
586 HostControllerPI != 0x00 &&
587 HostControllerPI != 0x10 &&
588 HostControllerPI != 0x20 &&
589 HostControllerPI != 0x30) {
590 return EFI_INVALID_PARAMETER;
591 }
592
593 //
594 // Find the usb host controller firstly, then connect with the remaining device path
595 //
596 AtLeastOneConnected = FALSE;
597 Status = gBS->LocateHandleBuffer (
598 ByProtocol,
599 &gEfiPciIoProtocolGuid,
600 NULL,
601 &HandleArrayCount,
602 &HandleArray
603 );
604 if (!EFI_ERROR (Status)) {
605 for (Index = 0; Index < HandleArrayCount; Index++) {
606 Status = gBS->HandleProtocol (
607 HandleArray[Index],
608 &gEfiPciIoProtocolGuid,
609 (VOID **)&PciIo
610 );
611 if (!EFI_ERROR (Status)) {
612 //
613 // Check whether the Pci device is the wanted usb host controller
614 //
615 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, 0x09, 3, &Class);
616 if (!EFI_ERROR (Status)) {
617 if ((PCI_CLASS_SERIAL == Class[2]) &&
618 (PCI_CLASS_SERIAL_USB == Class[1])) {
619 if (HostControllerPI == Class[0] || HostControllerPI == 0xFF) {
620 Status = gBS->ConnectController (
621 HandleArray[Index],
622 NULL,
623 RemainingDevicePath,
624 FALSE
625 );
626 if (!EFI_ERROR(Status)) {
627 AtLeastOneConnected = TRUE;
628 }
629 }
630 }
631 }
632 }
633 }
634
635 if (AtLeastOneConnected) {
636 return EFI_SUCCESS;
637 }
638 }
639
640 return EFI_NOT_FOUND;
641 }