VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/VBoxPkg/Library/VBoxGenericBdsLib/BdsConnect.c@ 48674

Last change on this file since 48674 was 48674, checked in by vboxsync, 12 years ago

EFI: Export newly imported tinaocore UEFI sources to OSE.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.4 KB
Line 
1/* $Id: BdsConnect.c 48674 2013-09-25 08:26:15Z vboxsync $ */
2/** @file
3 * BdsConnect.c - BDS Lib functions which relate with connect the device
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*
28
29This code is based on:
30
31Copyright (c) 2004 - 2008, Intel Corporation. <BR>
32All rights reserved. This program and the accompanying materials
33are licensed and made available under the terms and conditions of the BSD License
34which accompanies this distribution. The full text of the license may be found at
35http://opensource.org/licenses/bsd-license.php
36
37THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
38WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
39
40*/
41
42#include "InternalBdsLib.h"
43#include <Protocol/DevicePathToText.h>
44
45/**
46 This function will connect all the system driver to controller
47 first, and then special connect the default console, this make
48 sure all the system controller available and the platform default
49 console connected.
50
51**/
52VOID
53EFIAPI
54BdsLibConnectAll (
55 VOID
56 )
57{
58 //
59 // Connect the platform console first
60 //
61 BdsLibConnectAllDefaultConsoles ();
62
63 //
64 // Generic way to connect all the drivers
65 //
66 BdsLibConnectAllDriversToAllControllers ();
67
68 //
69 // Here we have the assumption that we have already had
70 // platform default console
71 //
72 BdsLibConnectAllDefaultConsoles ();
73}
74
75
76/**
77 This function will connect all the system drivers to all controllers
78 first, and then connect all the console devices the system current
79 have. After this we should get all the device work and console available
80 if the system have console device.
81
82**/
83VOID
84BdsLibGenericConnectAll (
85 VOID
86 )
87{
88 //
89 // Most generic way to connect all the drivers
90 //
91 BdsLibConnectAllDriversToAllControllers ();
92 BdsLibConnectAllConsoles ();
93}
94
95
96/**
97 This function will create all handles associate with every device
98 path node. If the handle associate with one device path node can not
99 be created success, then still give one chance to do the dispatch,
100 which load the missing drivers if possible.
101
102 @param DevicePathToConnect The device path which will be connected, it can be
103 a multi-instance device path
104
105 @retval EFI_SUCCESS All handles associate with every device path node
106 have been created
107 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles
108 @retval EFI_NOT_FOUND Create the handle associate with one device path
109 node failed
110
111**/
112static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevPathToTxt;
113
114EFI_STATUS
115EFIAPI
116BdsLibConnectDevicePath (
117 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathToConnect
118 )
119{
120 EFI_STATUS Status;
121 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
122 EFI_DEVICE_PATH_PROTOCOL *CopyOfDevicePath;
123 EFI_DEVICE_PATH_PROTOCOL *Instance;
124 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
125 EFI_DEVICE_PATH_PROTOCOL *Next;
126 EFI_HANDLE Handle;
127 EFI_HANDLE PreviousHandle;
128 UINTN Size;
129
130 if (DevPathToTxt == NULL)
131 {
132 Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID **)&DevPathToTxt);
133 ASSERT((!EFI_ERROR(Status)));
134 }
135 if (DevicePathToConnect == NULL) {
136 return EFI_SUCCESS;
137 }
138
139 DEBUG((DEBUG_INFO, "%a:%d dev path '%s' to connect\n", __FILE__, __LINE__, DevPathToTxt->ConvertDevicePathToText(DevicePathToConnect, TRUE, FALSE)));
140 DevicePath = DuplicateDevicePath (DevicePathToConnect);
141 if (DevicePath == NULL) {
142 return EFI_OUT_OF_RESOURCES;
143 }
144 CopyOfDevicePath = DevicePath;
145
146 do {
147 //
148 // The outer loop handles multi instance device paths.
149 // Only console variables contain multiple instance device paths.
150 //
151 // After this call DevicePath points to the next Instance
152 //
153 Instance = GetNextDevicePathInstance (&DevicePath, &Size);
154 if (Instance == NULL) {
155 FreePool (CopyOfDevicePath);
156 return EFI_OUT_OF_RESOURCES;
157 }
158
159 Next = Instance;
160 while (!IsDevicePathEndType (Next)) {
161 Next = NextDevicePathNode (Next);
162 }
163
164 SetDevicePathEndNode (Next);
165
166 //
167 // Start the real work of connect with RemainingDevicePath
168 //
169 PreviousHandle = NULL;
170 do {
171 //
172 // Find the handle that best matches the Device Path. If it is only a
173 // partial match the remaining part of the device path is returned in
174 // RemainingDevicePath.
175 //
176 RemainingDevicePath = Instance;
177 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);
178
179 if (!EFI_ERROR (Status)) {
180 if (Handle == PreviousHandle) {
181 //
182 // If no forward progress is made try invoking the Dispatcher.
183 // A new FV may have been added to the system an new drivers
184 // may now be found.
185 // Status == EFI_SUCCESS means a driver was dispatched
186 // Status == EFI_NOT_FOUND means no new drivers were dispatched
187 //
188 Status = gDS->Dispatch ();
189 }
190
191 if (!EFI_ERROR (Status)) {
192 PreviousHandle = Handle;
193 //
194 // Connect all drivers that apply to Handle and RemainingDevicePath,
195 // the Recursive flag is FALSE so only one level will be expanded.
196 //
197 // Do not check the connect status here, if the connect controller fail,
198 // then still give the chance to do dispatch, because partial
199 // RemainingDevicepath may be in the new FV
200 //
201 // 1. If the connect fail, RemainingDevicepath and handle will not
202 // change, so next time will do the dispatch, then dispatch's status
203 // will take effect
204 // 2. If the connect success, the RemainingDevicepath and handle will
205 // change, then avoid the dispatch, we have chance to continue the
206 // next connection
207 //
208 gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);
209 }
210 }
211 //
212 // Loop until RemainingDevicePath is an empty device path
213 //
214 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));
215
216 } while (DevicePath != NULL);
217
218 if (CopyOfDevicePath != NULL) {
219 FreePool (CopyOfDevicePath);
220 }
221 //
222 // All handle with DevicePath exists in the handle database
223 //
224 return Status;
225}
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**/
239EFI_STATUS
240EFIAPI
241BdsLibConnectAllEfi (
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**/
283EFI_STATUS
284EFIAPI
285BdsLibDisconnectAllEfi (
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 corresponding controllers if have. And at the same time, make
324 sure all the system controllers have driver to manage it if have.
325
326**/
327VOID
328EFIAPI
329BdsLibConnectAllDriversToAllControllers (
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**/
371EFI_STATUS
372EFIAPI
373BdsLibConnectUsbDevByShortFormDP(
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 (AtLeastOneConnected) {
449 return EFI_SUCCESS;
450 }
451 }
452
453 return EFI_NOT_FOUND;
454}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette