]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/UefiBootManagerLib/BmConnect.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / BmConnect.c
1 /** @file
2 Library functions which relate with connecting the device.
3
4 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "InternalBm.h"
10
11 /**
12 Connect all the drivers to all the controllers.
13
14 This function makes sure all the current system drivers manage the correspoinding
15 controllers if have. And at the same time, makes sure all the system controllers
16 have driver to manage it if have.
17 **/
18 VOID
19 BmConnectAllDriversToAllControllers (
20 VOID
21 )
22 {
23 EFI_STATUS Status;
24 UINTN HandleCount;
25 EFI_HANDLE *HandleBuffer;
26 UINTN Index;
27
28 do {
29 //
30 // Connect All EFI 1.10 drivers following EFI 1.10 algorithm
31 //
32 gBS->LocateHandleBuffer (
33 AllHandles,
34 NULL,
35 NULL,
36 &HandleCount,
37 &HandleBuffer
38 );
39
40 for (Index = 0; Index < HandleCount; Index++) {
41 gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);
42 }
43
44 if (HandleBuffer != NULL) {
45 FreePool (HandleBuffer);
46 }
47
48 //
49 // Check to see if it's possible to dispatch an more DXE drivers.
50 // The above code may have made new DXE drivers show up.
51 // If any new driver is dispatched (Status == EFI_SUCCESS) and we will try
52 // the connect again.
53 //
54 Status = gDS->Dispatch ();
55
56 } while (!EFI_ERROR (Status));
57 }
58
59 /**
60 This function will connect all the system driver to controller
61 first, and then special connect the default console, this make
62 sure all the system controller available and the platform default
63 console connected.
64
65 **/
66 VOID
67 EFIAPI
68 EfiBootManagerConnectAll (
69 VOID
70 )
71 {
72 //
73 // Connect the platform console first
74 //
75 EfiBootManagerConnectAllDefaultConsoles ();
76
77 //
78 // Generic way to connect all the drivers
79 //
80 BmConnectAllDriversToAllControllers ();
81
82 //
83 // Here we have the assumption that we have already had
84 // platform default console
85 //
86 EfiBootManagerConnectAllDefaultConsoles ();
87 }
88
89 /**
90 This function will create all handles associate with every device
91 path node. If the handle associate with one device path node can not
92 be created successfully, then still give chance to do the dispatch,
93 which load the missing drivers if possible.
94
95 @param DevicePathToConnect The device path which will be connected, it can be
96 a multi-instance device path
97 @param MatchingHandle Return the controller handle closest to the DevicePathToConnect
98
99 @retval EFI_SUCCESS All handles associate with every device path node
100 have been created.
101 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles.
102 @retval EFI_NOT_FOUND Create the handle associate with one device path
103 node failed.
104 @retval EFI_SECURITY_VIOLATION The user has no permission to start UEFI device
105 drivers on the DevicePath.
106 **/
107 EFI_STATUS
108 EFIAPI
109 EfiBootManagerConnectDevicePath (
110 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathToConnect,
111 OUT EFI_HANDLE *MatchingHandle OPTIONAL
112 )
113 {
114 EFI_STATUS Status;
115 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
116 EFI_HANDLE Handle;
117 EFI_HANDLE PreviousHandle;
118 EFI_TPL CurrentTpl;
119
120 if (DevicePathToConnect == NULL) {
121 return EFI_INVALID_PARAMETER;
122 }
123
124 CurrentTpl = EfiGetCurrentTpl ();
125 //
126 // Start the real work of connect with RemainingDevicePath
127 //
128 PreviousHandle = NULL;
129 do {
130 //
131 // Find the handle that best matches the Device Path. If it is only a
132 // partial match the remaining part of the device path is returned in
133 // RemainingDevicePath.
134 //
135 RemainingDevicePath = DevicePathToConnect;
136 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);
137 if (!EFI_ERROR (Status)) {
138 if (Handle == PreviousHandle) {
139 //
140 // If no forward progress is made try invoking the Dispatcher.
141 // A new FV may have been added to the system an new drivers
142 // may now be found.
143 // Status == EFI_SUCCESS means a driver was dispatched
144 // Status == EFI_NOT_FOUND means no new drivers were dispatched
145 //
146 if (CurrentTpl == TPL_APPLICATION) {
147 Status = gDS->Dispatch ();
148 } else {
149 //
150 // Always return EFI_NOT_FOUND here
151 // to prevent dead loop when control handle is found but connection failded case
152 //
153 Status = EFI_NOT_FOUND;
154 }
155 }
156
157
158 if (!EFI_ERROR (Status)) {
159 PreviousHandle = Handle;
160 //
161 // Connect all drivers that apply to Handle and RemainingDevicePath,
162 // the Recursive flag is FALSE so only one level will be expanded.
163 //
164 // If ConnectController fails to find a driver, then still give the chance to
165 // do dispatch, because partial RemainingDevicePath may be in the new FV
166 //
167 // 1. If the connect fail, RemainingDevicepath and handle will not
168 // change, so next time will do the dispatch, then dispatch's status
169 // will take effect
170 // 2. If the connect success, the RemainingDevicepath and handle will
171 // change, then avoid the dispatch, we have chance to continue the
172 // next connection
173 //
174 Status = gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);
175 if (Status == EFI_NOT_FOUND) {
176 Status = EFI_SUCCESS;
177 }
178 if (MatchingHandle != NULL) {
179 *MatchingHandle = Handle;
180 }
181 }
182 }
183 //
184 // Loop until RemainingDevicePath is an empty device path
185 //
186 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));
187
188 ASSERT (EFI_ERROR (Status) || IsDevicePathEnd (RemainingDevicePath));
189
190 return Status;
191 }
192
193 /**
194 This function will disconnect all current system handles.
195
196 gBS->DisconnectController() is invoked for each handle exists in system handle buffer.
197 If handle is a bus type handle, all childrens also are disconnected recursively by
198 gBS->DisconnectController().
199 **/
200 VOID
201 EFIAPI
202 EfiBootManagerDisconnectAll (
203 VOID
204 )
205 {
206 UINTN HandleCount;
207 EFI_HANDLE *HandleBuffer;
208 UINTN Index;
209
210 //
211 // Disconnect all
212 //
213 gBS->LocateHandleBuffer (
214 AllHandles,
215 NULL,
216 NULL,
217 &HandleCount,
218 &HandleBuffer
219 );
220 for (Index = 0; Index < HandleCount; Index++) {
221 gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);
222 }
223
224 if (HandleBuffer != NULL) {
225 FreePool (HandleBuffer);
226 }
227 }
228
229 /**
230 Connect the specific Usb device which match the short form device path,
231 and whose bus is determined by Host Controller (Uhci or Ehci).
232
233 @param DevicePath A short-form device path that starts with the first
234 element being a USB WWID or a USB Class device
235 path
236
237 @return EFI_INVALID_PARAMETER DevicePath is NULL pointer.
238 DevicePath is not a USB device path.
239
240 @return EFI_SUCCESS Success to connect USB device
241 @return EFI_NOT_FOUND Fail to find handle for USB controller to connect.
242
243 **/
244 EFI_STATUS
245 BmConnectUsbShortFormDevicePath (
246 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
247 )
248 {
249 EFI_STATUS Status;
250 EFI_HANDLE *Handles;
251 UINTN HandleCount;
252 UINTN Index;
253 EFI_PCI_IO_PROTOCOL *PciIo;
254 UINT8 Class[3];
255 BOOLEAN AtLeastOneConnected;
256
257 //
258 // Check the passed in parameters
259 //
260 if (DevicePath == NULL) {
261 return EFI_INVALID_PARAMETER;
262 }
263
264 if ((DevicePathType (DevicePath) != MESSAGING_DEVICE_PATH) ||
265 ((DevicePathSubType (DevicePath) != MSG_USB_CLASS_DP) && (DevicePathSubType (DevicePath) != MSG_USB_WWID_DP))
266 ) {
267 return EFI_INVALID_PARAMETER;
268 }
269
270 //
271 // Find the usb host controller firstly, then connect with the remaining device path
272 //
273 AtLeastOneConnected = FALSE;
274 Status = gBS->LocateHandleBuffer (
275 ByProtocol,
276 &gEfiPciIoProtocolGuid,
277 NULL,
278 &HandleCount,
279 &Handles
280 );
281 if (!EFI_ERROR (Status)) {
282 for (Index = 0; Index < HandleCount; Index++) {
283 Status = gBS->HandleProtocol (
284 Handles[Index],
285 &gEfiPciIoProtocolGuid,
286 (VOID **) &PciIo
287 );
288 if (!EFI_ERROR (Status)) {
289 //
290 // Check whether the Pci device is the wanted usb host controller
291 //
292 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, 0x09, 3, &Class);
293 if (!EFI_ERROR (Status) &&
294 ((PCI_CLASS_SERIAL == Class[2]) && (PCI_CLASS_SERIAL_USB == Class[1]))
295 ) {
296 Status = gBS->ConnectController (
297 Handles[Index],
298 NULL,
299 DevicePath,
300 FALSE
301 );
302 if (!EFI_ERROR(Status)) {
303 AtLeastOneConnected = TRUE;
304 }
305 }
306 }
307 }
308
309 if (Handles != NULL) {
310 FreePool (Handles);
311 }
312 }
313
314 return AtLeastOneConnected ? EFI_SUCCESS : EFI_NOT_FOUND;
315 }