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