Import Usb/UsbBusDxe and Usb/UsbMassStorageDxe into MdeModulePkg.
[mirror_edk2.git] / EdkNt32Pkg / Library / EdkGenericBdsLib / BdsConnect.c
1 /*++
2
3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 BdsConnect.c
15
16 Abstract:
17
18 BDS Lib functions which relate with connect the device
19
20 --*/
21
22 VOID
23 BdsLibConnectAll (
24 VOID
25 )
26 /*++
27
28 Routine Description:
29
30 This function will connect all the system driver to controller
31 first, and then special connect the default console, this make
32 sure all the system controller avialbe and the platform default
33 console connected.
34
35 Arguments:
36
37 None
38
39 Returns:
40
41 None
42
43 --*/
44 {
45 //
46 // Connect the platform console first
47 //
48 BdsLibConnectAllDefaultConsoles ();
49
50 //
51 // Generic way to connect all the drivers
52 //
53 BdsLibConnectAllDriversToAllControllers ();
54
55 //
56 // Here we have the assumption that we have already had
57 // platform default console
58 //
59 BdsLibConnectAllDefaultConsoles ();
60 }
61
62 VOID
63 BdsLibGenericConnectAll (
64 VOID
65 )
66 /*++
67
68 Routine Description:
69
70 This function will connect all the system drivers to all controllers
71 first, and then connect all the console devices the system current
72 have. After this we should get all the device work and console avariable
73 if the system have console device.
74
75 Arguments:
76
77 None
78
79 Returns:
80
81 None
82
83 --*/
84 {
85 //
86 // Most generic way to connect all the drivers
87 //
88 BdsLibConnectAllDriversToAllControllers ();
89 BdsLibConnectAllConsoles ();
90 }
91
92 EFI_STATUS
93 BdsLibConnectDevicePath (
94 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathToConnect
95 )
96 /*++
97
98 Routine Description:
99 This function will create all handles associate with every device
100 path node. If the handle associate with one device path node can not
101 be created success, then still give one chance to do the dispatch,
102 which load the missing drivers if possible.
103
104 Arguments:
105
106 DevicePathToConnect - The device path which will be connected, it can
107 be a multi-instance device path
108
109 Returns:
110
111 EFI_SUCCESS - All handles associate with every device path
112 node have been created
113
114 EFI_OUT_OF_RESOURCES - There is no resource to create new handles
115
116 EFI_NOT_FOUND - Create the handle associate with one device
117 path node failed
118
119 --*/
120 {
121 EFI_STATUS Status;
122 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
123 EFI_DEVICE_PATH_PROTOCOL *CopyOfDevicePath;
124 EFI_DEVICE_PATH_PROTOCOL *Instance;
125 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
126 EFI_DEVICE_PATH_PROTOCOL *Next;
127 EFI_HANDLE Handle;
128 EFI_HANDLE PreviousHandle;
129 UINTN Size;
130
131 if (DevicePathToConnect == NULL) {
132 return EFI_SUCCESS;
133 }
134
135 DevicePath = DuplicateDevicePath (DevicePathToConnect);
136 CopyOfDevicePath = DevicePath;
137 if (DevicePath == NULL) {
138 return EFI_OUT_OF_RESOURCES;
139 }
140
141 do {
142 //
143 // The outer loop handles multi instance device paths.
144 // Only console variables contain multiple instance device paths.
145 //
146 // After this call DevicePath points to the next Instance
147 //
148 Instance = GetNextDevicePathInstance (&DevicePath, &Size);
149 Next = Instance;
150 while (!IsDevicePathEndType (Next)) {
151 Next = NextDevicePathNode (Next);
152 }
153
154 SetDevicePathEndNode (Next);
155
156 //
157 // Start the real work of connect with RemainingDevicePath
158 //
159 PreviousHandle = NULL;
160 do {
161 //
162 // Find the handle that best matches the Device Path. If it is only a
163 // partial match the remaining part of the device path is returned in
164 // RemainingDevicePath.
165 //
166 RemainingDevicePath = Instance;
167 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);
168
169 if (!EFI_ERROR (Status)) {
170 if (Handle == PreviousHandle) {
171 //
172 // If no forward progress is made try invoking the Dispatcher.
173 // A new FV may have been added to the system an new drivers
174 // may now be found.
175 // Status == EFI_SUCCESS means a driver was dispatched
176 // Status == EFI_NOT_FOUND means no new drivers were dispatched
177 //
178 Status = gDS->Dispatch ();
179 }
180
181 if (!EFI_ERROR (Status)) {
182 PreviousHandle = Handle;
183 //
184 // Connect all drivers that apply to Handle and RemainingDevicePath,
185 // the Recursive flag is FALSE so only one level will be expanded.
186 //
187 // Do not check the connect status here, if the connect controller fail,
188 // then still give the chance to do dispatch, because partial
189 // RemainingDevicepath may be in the new FV
190 //
191 // 1. If the connect fail, RemainingDevicepath and handle will not
192 // change, so next time will do the dispatch, then dispatch's status
193 // will take effect
194 // 2. If the connect success, the RemainingDevicepath and handle will
195 // change, then avoid the dispatch, we have chance to continue the
196 // next connection
197 //
198 gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);
199 }
200 }
201 //
202 // Loop until RemainingDevicePath is an empty device path
203 //
204 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));
205
206 } while (DevicePath != NULL);
207
208 if (CopyOfDevicePath != NULL) {
209 FreePool (CopyOfDevicePath);
210 }
211 //
212 // All handle with DevicePath exists in the handle database
213 //
214 return Status;
215 }
216
217 EFI_STATUS
218 BdsLibConnectAllEfi (
219 VOID
220 )
221 /*++
222
223 Routine Description:
224
225 This function will connect all current system handles recursively. The
226 connection will finish until every handle's child handle created if it have.
227
228 Arguments:
229
230 None
231
232 Returns:
233
234 EFI_SUCCESS - All handles and it's child handle have been connected
235
236 EFI_STATUS - Return the status of gBS->LocateHandleBuffer().
237
238 --*/
239 {
240 EFI_STATUS Status;
241 UINTN HandleCount;
242 EFI_HANDLE *HandleBuffer;
243 UINTN Index;
244
245 Status = gBS->LocateHandleBuffer (
246 AllHandles,
247 NULL,
248 NULL,
249 &HandleCount,
250 &HandleBuffer
251 );
252 if (EFI_ERROR (Status)) {
253 return Status;
254 }
255
256 for (Index = 0; Index < HandleCount; Index++) {
257 Status = gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE);
258 }
259
260 FreePool (HandleBuffer);
261
262 return EFI_SUCCESS;
263 }
264
265 EFI_STATUS
266 BdsLibDisconnectAllEfi (
267 VOID
268 )
269 /*++
270
271 Routine Description:
272
273 This function will disconnect all current system handles. The disconnection
274 will finish until every handle have been disconnected.
275
276 Arguments:
277
278 None
279
280 Returns:
281
282 EFI_SUCCESS - All handles have been disconnected
283
284 EFI_STATUS - Return the status of gBS->LocateHandleBuffer().
285
286 --*/
287 {
288 EFI_STATUS Status;
289 UINTN HandleCount;
290 EFI_HANDLE *HandleBuffer;
291 UINTN Index;
292
293 //
294 // Disconnect all
295 //
296 Status = gBS->LocateHandleBuffer (
297 AllHandles,
298 NULL,
299 NULL,
300 &HandleCount,
301 &HandleBuffer
302 );
303 if (EFI_ERROR (Status)) {
304 return Status;
305 }
306
307 for (Index = 0; Index < HandleCount; Index++) {
308 Status = gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);
309 }
310
311 FreePool (HandleBuffer);
312
313 return EFI_SUCCESS;
314 }
315
316 VOID
317 BdsLibConnectAllDriversToAllControllers (
318 VOID
319 )
320 /*++
321
322 Routine Description:
323
324 Connects all drivers to all controllers.
325 This function make sure all the current system driver will manage
326 the correspoinding controllers if have. And at the same time, make
327 sure all the system controllers have driver to manage it if have.
328
329 Arguments:
330
331 None
332
333 Returns:
334
335 None
336
337 --*/
338 {
339 EFI_STATUS Status;
340
341 do {
342 //
343 // Connect All EFI 1.10 drivers following EFI 1.10 algorithm
344 //
345 BdsLibConnectAllEfi ();
346
347 //
348 // Check to see if it's possible to dispatch an more DXE drivers.
349 // The BdsLibConnectAllEfi () may have made new DXE drivers show up.
350 // If anything is Dispatched Status == EFI_SUCCESS and we will try
351 // the connect again.
352 //
353 Status = gDS->Dispatch ();
354
355 } while (!EFI_ERROR (Status));
356
357 }