]> git.proxmox.com Git - mirror_edk2.git/blob - Nt32Pkg/Library/EdkGenericBdsLib/BdsConnect.c
cb51488795efb3b997f48dfa828a73ce12a30a1a
[mirror_edk2.git] / Nt32Pkg / 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 //
23 // Include common header file for this module.
24 //
25 #include "CommonHeader.h"
26
27 VOID
28 BdsLibConnectAll (
29 VOID
30 )
31 /*++
32
33 Routine Description:
34
35 This function will connect all the system driver to controller
36 first, and then special connect the default console, this make
37 sure all the system controller avialbe and the platform default
38 console connected.
39
40 Arguments:
41
42 None
43
44 Returns:
45
46 None
47
48 --*/
49 {
50 //
51 // Connect the platform console first
52 //
53 BdsLibConnectAllDefaultConsoles ();
54
55 //
56 // Generic way to connect all the drivers
57 //
58 BdsLibConnectAllDriversToAllControllers ();
59
60 //
61 // Here we have the assumption that we have already had
62 // platform default console
63 //
64 BdsLibConnectAllDefaultConsoles ();
65 }
66
67 VOID
68 BdsLibGenericConnectAll (
69 VOID
70 )
71 /*++
72
73 Routine Description:
74
75 This function will connect all the system drivers to all controllers
76 first, and then connect all the console devices the system current
77 have. After this we should get all the device work and console avariable
78 if the system have console device.
79
80 Arguments:
81
82 None
83
84 Returns:
85
86 None
87
88 --*/
89 {
90 //
91 // Most generic way to connect all the drivers
92 //
93 BdsLibConnectAllDriversToAllControllers ();
94 BdsLibConnectAllConsoles ();
95 }
96
97 EFI_STATUS
98 BdsLibConnectDevicePath (
99 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathToConnect
100 )
101 /*++
102
103 Routine Description:
104 This function will create all handles associate with every device
105 path node. If the handle associate with one device path node can not
106 be created success, then still give one chance to do the dispatch,
107 which load the missing drivers if possible.
108
109 Arguments:
110
111 DevicePathToConnect - The device path which will be connected, it can
112 be a multi-instance device path
113
114 Returns:
115
116 EFI_SUCCESS - All handles associate with every device path
117 node have been created
118
119 EFI_OUT_OF_RESOURCES - There is no resource to create new handles
120
121 EFI_NOT_FOUND - Create the handle associate with one device
122 path node failed
123
124 --*/
125 {
126 EFI_STATUS Status;
127 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
128 EFI_DEVICE_PATH_PROTOCOL *CopyOfDevicePath;
129 EFI_DEVICE_PATH_PROTOCOL *Instance;
130 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
131 EFI_DEVICE_PATH_PROTOCOL *Next;
132 EFI_HANDLE Handle;
133 EFI_HANDLE PreviousHandle;
134 UINTN Size;
135
136 if (DevicePathToConnect == NULL) {
137 return EFI_SUCCESS;
138 }
139
140 DevicePath = DuplicateDevicePath (DevicePathToConnect);
141 CopyOfDevicePath = DevicePath;
142 if (DevicePath == NULL) {
143 return EFI_OUT_OF_RESOURCES;
144 }
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 Next = Instance;
155 while (!IsDevicePathEndType (Next)) {
156 Next = NextDevicePathNode (Next);
157 }
158
159 SetDevicePathEndNode (Next);
160
161 //
162 // Start the real work of connect with RemainingDevicePath
163 //
164 PreviousHandle = NULL;
165 do {
166 //
167 // Find the handle that best matches the Device Path. If it is only a
168 // partial match the remaining part of the device path is returned in
169 // RemainingDevicePath.
170 //
171 RemainingDevicePath = Instance;
172 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);
173
174 if (!EFI_ERROR (Status)) {
175 if (Handle == PreviousHandle) {
176 //
177 // If no forward progress is made try invoking the Dispatcher.
178 // A new FV may have been added to the system an new drivers
179 // may now be found.
180 // Status == EFI_SUCCESS means a driver was dispatched
181 // Status == EFI_NOT_FOUND means no new drivers were dispatched
182 //
183 Status = gDS->Dispatch ();
184 }
185
186 if (!EFI_ERROR (Status)) {
187 PreviousHandle = Handle;
188 //
189 // Connect all drivers that apply to Handle and RemainingDevicePath,
190 // the Recursive flag is FALSE so only one level will be expanded.
191 //
192 // Do not check the connect status here, if the connect controller fail,
193 // then still give the chance to do dispatch, because partial
194 // RemainingDevicepath may be in the new FV
195 //
196 // 1. If the connect fail, RemainingDevicepath and handle will not
197 // change, so next time will do the dispatch, then dispatch's status
198 // will take effect
199 // 2. If the connect success, the RemainingDevicepath and handle will
200 // change, then avoid the dispatch, we have chance to continue the
201 // next connection
202 //
203 gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);
204 }
205 }
206 //
207 // Loop until RemainingDevicePath is an empty device path
208 //
209 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));
210
211 } while (DevicePath != NULL);
212
213 if (CopyOfDevicePath != NULL) {
214 FreePool (CopyOfDevicePath);
215 }
216 //
217 // All handle with DevicePath exists in the handle database
218 //
219 return Status;
220 }
221
222 EFI_STATUS
223 BdsLibConnectAllEfi (
224 VOID
225 )
226 /*++
227
228 Routine Description:
229
230 This function will connect all current system handles recursively. The
231 connection will finish until every handle's child handle created if it have.
232
233 Arguments:
234
235 None
236
237 Returns:
238
239 EFI_SUCCESS - All handles and it's child handle have been connected
240
241 EFI_STATUS - Return the status of gBS->LocateHandleBuffer().
242
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 FreePool (HandleBuffer);
266
267 return EFI_SUCCESS;
268 }
269
270 EFI_STATUS
271 BdsLibDisconnectAllEfi (
272 VOID
273 )
274 /*++
275
276 Routine Description:
277
278 This function will disconnect all current system handles. The disconnection
279 will finish until every handle have been disconnected.
280
281 Arguments:
282
283 None
284
285 Returns:
286
287 EFI_SUCCESS - All handles have been disconnected
288
289 EFI_STATUS - Return the status of gBS->LocateHandleBuffer().
290
291 --*/
292 {
293 EFI_STATUS Status;
294 UINTN HandleCount;
295 EFI_HANDLE *HandleBuffer;
296 UINTN Index;
297
298 //
299 // Disconnect all
300 //
301 Status = gBS->LocateHandleBuffer (
302 AllHandles,
303 NULL,
304 NULL,
305 &HandleCount,
306 &HandleBuffer
307 );
308 if (EFI_ERROR (Status)) {
309 return Status;
310 }
311
312 for (Index = 0; Index < HandleCount; Index++) {
313 Status = gBS->DisconnectController (HandleBuffer[Index], NULL, NULL);
314 }
315
316 FreePool (HandleBuffer);
317
318 return EFI_SUCCESS;
319 }
320
321 VOID
322 BdsLibConnectAllDriversToAllControllers (
323 VOID
324 )
325 /*++
326
327 Routine Description:
328
329 Connects all drivers to all controllers.
330 This function make sure all the current system driver will manage
331 the correspoinding controllers if have. And at the same time, make
332 sure all the system controllers have driver to manage it if have.
333
334 Arguments:
335
336 None
337
338 Returns:
339
340 None
341
342 --*/
343 {
344 EFI_STATUS Status;
345
346 do {
347 //
348 // Connect All EFI 1.10 drivers following EFI 1.10 algorithm
349 //
350 BdsLibConnectAllEfi ();
351
352 //
353 // Check to see if it's possible to dispatch an more DXE drivers.
354 // The BdsLibConnectAllEfi () may have made new DXE drivers show up.
355 // If anything is Dispatched Status == EFI_SUCCESS and we will try
356 // the connect again.
357 //
358 Status = gDS->Dispatch ();
359
360 } while (!EFI_ERROR (Status));
361
362 }