]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDriver1CommandsLib/Connect.c
ShellPkg: Fixes and updates for the 'devices' command
[mirror_edk2.git] / ShellPkg / Library / UefiShellDriver1CommandsLib / Connect.c
1 /** @file
2 Main file for connect shell Driver1 function.
3
4 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "UefiShellDriver1CommandsLib.h"
16
17 /**
18 Create all handles associate with every device path node.
19
20 @param DevicePathToConnect The device path which will be connected.
21
22 @retval EFI_SUCCESS All handles associate with every device path node
23 have been created.
24 @retval EFI_INVALID_PARAMETER DevicePathToConnect is NULL.
25 @retval EFI_NOT_FOUND Create the handle associate with one device path
26 node failed
27
28 **/
29 EFI_STATUS
30 ShellConnectDevicePath (
31 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathToConnect
32 )
33 {
34 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
35 EFI_STATUS Status;
36 EFI_HANDLE Handle;
37 EFI_HANDLE PreviousHandle;
38
39 if (DevicePathToConnect == NULL) {
40 return EFI_INVALID_PARAMETER;
41 }
42
43 PreviousHandle = NULL;
44 do{
45 RemainingDevicePath = DevicePathToConnect;
46 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);
47
48 if (!EFI_ERROR (Status) && (Handle != NULL)) {
49 if (PreviousHandle == Handle) {
50 Status = EFI_NOT_FOUND;
51 } else {
52 PreviousHandle = Handle;
53 Status = gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);
54 }
55 }
56
57 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath) );
58
59 return Status;
60
61 }
62
63 /**
64 Connect drivers for PCI root bridge.
65
66 @retval EFI_SUCCESS Connect drivers successfully.
67 @retval EFI_NOT_FOUND Cannot find PCI root bridge device.
68
69 **/
70 EFI_STATUS
71 ShellConnectPciRootBridge (
72 VOID
73 )
74 {
75 UINTN RootBridgeHandleCount;
76 EFI_HANDLE *RootBridgeHandleBuffer;
77 UINTN RootBridgeIndex;
78 EFI_STATUS Status;
79
80 RootBridgeHandleCount = 0;
81
82 Status = gBS->LocateHandleBuffer (
83 ByProtocol,
84 &gEfiPciRootBridgeIoProtocolGuid,
85 NULL,
86 &RootBridgeHandleCount,
87 &RootBridgeHandleBuffer
88 );
89 if (EFI_ERROR (Status)) {
90 return Status;
91 }
92
93 for (RootBridgeIndex = 0; RootBridgeIndex < RootBridgeHandleCount; RootBridgeIndex++) {
94 gBS->ConnectController (RootBridgeHandleBuffer[RootBridgeIndex], NULL, NULL, FALSE);
95 }
96
97 return EFI_SUCCESS;
98 }
99
100
101 /**
102 Connect controller(s) and driver(s).
103
104 @param[in] ControllerHandle The handle to the controller. Should have driver binding on it.
105 @param[in] DriverHandle The handle to the driver. Should have driver binding.
106 @param[in] Recursive TRUE to connect recursively, FALSE otherwise.
107 @param[in] Output TRUE to have info on the screen, FALSE otherwise.
108 @param[in] AlwaysOutput Override Output for errors.
109
110 @retval EFI_SUCCESS The operation was successful.
111 **/
112 EFI_STATUS
113 EFIAPI
114 ConnectControllers (
115 IN CONST EFI_HANDLE ControllerHandle OPTIONAL,
116 IN CONST EFI_HANDLE DriverHandle OPTIONAL,
117 IN CONST BOOLEAN Recursive,
118 IN CONST BOOLEAN Output,
119 IN CONST BOOLEAN AlwaysOutput
120 )
121 {
122 EFI_STATUS Status;
123 EFI_STATUS Status2;
124 EFI_HANDLE *ControllerHandleList;
125 EFI_HANDLE *DriverHandleList;
126 EFI_HANDLE *HandleWalker;
127
128 ControllerHandleList = NULL;
129 Status = EFI_NOT_FOUND;
130 Status2 = EFI_NOT_FOUND;
131
132 //
133 // If we have a single handle to connect make that a 'list'
134 //
135 if (DriverHandle == NULL) {
136 DriverHandleList = NULL;
137 } else {
138 DriverHandleList = AllocateZeroPool(2*sizeof(EFI_HANDLE));
139 if (DriverHandleList == NULL) {
140 return (EFI_OUT_OF_RESOURCES);
141 }
142 DriverHandleList[0] = DriverHandle;
143 DriverHandleList[1] = NULL;
144 }
145
146 //
147 // do we connect all controllers (with a loop) or a single one...
148 // This is where we call the gBS->ConnectController function.
149 //
150 if (ControllerHandle == NULL) {
151 ControllerHandleList = GetHandleListByProtocol(&gEfiDevicePathProtocolGuid);
152 for (HandleWalker = ControllerHandleList
153 ; HandleWalker != NULL && *HandleWalker != NULL
154 ; HandleWalker++
155 ){
156 Status = gBS->ConnectController(*HandleWalker, DriverHandleList, NULL, Recursive);
157 if (!EFI_ERROR(Status)) {
158 Status2 = EFI_SUCCESS;
159 }
160 if ((Output && !EFI_ERROR(Status)) || AlwaysOutput) {
161 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_HANDLE_RESULT), gShellDriver1HiiHandle, L"Connect", ConvertHandleToHandleIndex(*HandleWalker), Status);
162 }
163 }
164 } else {
165 Status = gBS->ConnectController(ControllerHandle, DriverHandleList, NULL, Recursive);
166 if (!EFI_ERROR(Status)) {
167 Status2 = EFI_SUCCESS;
168 }
169 if ((Output && !EFI_ERROR(Status)) || AlwaysOutput) {
170 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_HANDLE_RESULT), gShellDriver1HiiHandle, L"Connect", ConvertHandleToHandleIndex(ControllerHandle), Status);
171 }
172 }
173
174 //
175 // Free any memory we allocated.
176 //
177 if (ControllerHandleList != NULL) {
178 FreePool(ControllerHandleList);
179 }
180 if (DriverHandleList != NULL) {
181 FreePool(DriverHandleList);
182 }
183 return (Status2);
184 }
185
186 /**
187 Do a connect from an EFI variable via it's key name.
188
189 @param[in] Key The name of the EFI Variable.
190
191 @retval EFI_SUCCESS The operation was successful.
192 **/
193 EFI_STATUS
194 EFIAPI
195 ConnectFromDevPaths (
196 IN CONST CHAR16 *Key
197 )
198 {
199 EFI_DEVICE_PATH_PROTOCOL *DevPath;
200 EFI_DEVICE_PATH_PROTOCOL *CopyOfDevPath;
201 EFI_DEVICE_PATH_PROTOCOL *Instance;
202 EFI_DEVICE_PATH_PROTOCOL *Next;
203 UINTN Length;
204 UINTN Index;
205 UINTN HandleArrayCount;
206 UINTN Size;
207 EFI_HANDLE *HandleArray;
208 EFI_STATUS Status;
209 BOOLEAN AtLeastOneConnected;
210 EFI_PCI_IO_PROTOCOL *PciIo;
211 UINT8 Class[3];
212
213 DevPath = NULL;
214 Length = 0;
215 AtLeastOneConnected = FALSE;
216
217 //
218 // Get the DevicePath buffer from the variable...
219 //
220 Status = gRT->GetVariable((CHAR16*)Key, (EFI_GUID*)&gEfiGlobalVariableGuid, NULL, &Length, DevPath);
221 if (Status == EFI_BUFFER_TOO_SMALL) {
222 DevPath = AllocateZeroPool(Length);
223 if (DevPath == NULL) {
224 return EFI_OUT_OF_RESOURCES;
225 }
226 Status = gRT->GetVariable((CHAR16*)Key, (EFI_GUID*)&gEfiGlobalVariableGuid, NULL, &Length, DevPath);
227 if (EFI_ERROR (Status)) {
228 if (DevPath != NULL) {
229 FreePool (DevPath);
230 }
231 return Status;
232 }
233 } else if (EFI_ERROR (Status)) {
234 return Status;
235 }
236
237 Status = EFI_NOT_FOUND;
238
239 CopyOfDevPath = DevPath;
240 //
241 // walk the list of devices and connect them
242 //
243 do {
244 //
245 // Check every instance of the console variable
246 //
247 Instance = GetNextDevicePathInstance (&CopyOfDevPath, &Size);
248 if (Instance == NULL) {
249 if (DevPath != NULL) {
250 FreePool (DevPath);
251 }
252 return EFI_UNSUPPORTED;
253 }
254
255 Next = Instance;
256 while (!IsDevicePathEndType (Next)) {
257 Next = NextDevicePathNode (Next);
258 }
259
260 SetDevicePathEndNode (Next);
261 //
262 // connect short form device path
263 //
264 if ((DevicePathType (Instance) == MESSAGING_DEVICE_PATH) &&
265 ((DevicePathSubType (Instance) == MSG_USB_CLASS_DP)
266 || (DevicePathSubType (Instance) == MSG_USB_WWID_DP)
267 )) {
268
269 Status = ShellConnectPciRootBridge ();
270 if (EFI_ERROR(Status)) {
271 FreePool(Instance);
272 FreePool(DevPath);
273 return Status;
274 }
275
276 Status = gBS->LocateHandleBuffer (
277 ByProtocol,
278 &gEfiPciIoProtocolGuid,
279 NULL,
280 &HandleArrayCount,
281 &HandleArray
282 );
283
284 if (!EFI_ERROR (Status)) {
285 for (Index = 0; Index < HandleArrayCount; Index++) {
286 Status = gBS->HandleProtocol (
287 HandleArray[Index],
288 &gEfiPciIoProtocolGuid,
289 (VOID **)&PciIo
290 );
291
292 if (!EFI_ERROR (Status)) {
293 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, 0x09, 3, &Class);
294 if (!EFI_ERROR (Status)) {
295 if ((PCI_CLASS_SERIAL == Class[2]) &&
296 (PCI_CLASS_SERIAL_USB == Class[1])) {
297 Status = gBS->ConnectController (
298 HandleArray[Index],
299 NULL,
300 Instance,
301 FALSE
302 );
303 if (!EFI_ERROR(Status)) {
304 AtLeastOneConnected = TRUE;
305 }
306 }
307 }
308 }
309 }
310 }
311
312 if (HandleArray != NULL) {
313 FreePool (HandleArray);
314 }
315 } else {
316 //
317 // connect the entire device path
318 //
319 Status = ShellConnectDevicePath (Instance);
320 if (!EFI_ERROR (Status)) {
321 AtLeastOneConnected = TRUE;
322 }
323 }
324 FreePool (Instance);
325
326 } while (CopyOfDevPath != NULL);
327
328 if (DevPath != NULL) {
329 FreePool(DevPath);
330 }
331
332 if (AtLeastOneConnected) {
333 return EFI_SUCCESS;
334 } else {
335 return EFI_NOT_FOUND;
336 }
337
338 }
339
340 /**
341 Convert the handle identifiers from strings and then connect them.
342
343 One of them should have driver binding and either can be NULL.
344
345 @param[in] Handle1 The first handle.
346 @param[in] Handle2 The second handle.
347 @param[in] Recursive TRUE to do connect recursively. FALSE otherwise.
348 @param[in] Output TRUE to have output to screen. FALSE otherwise.
349
350 @retval EFI_SUCCESS The operation was successful.
351 **/
352 EFI_STATUS
353 EFIAPI
354 ConvertAndConnectControllers (
355 IN EFI_HANDLE *Handle1 OPTIONAL,
356 IN EFI_HANDLE *Handle2 OPTIONAL,
357 IN CONST BOOLEAN Recursive,
358 IN CONST BOOLEAN Output
359 )
360 {
361 //
362 // if only one is NULL verify it's the proper one...
363 //
364 if ( (Handle1 == NULL && Handle2 != NULL)
365 || (Handle1 != NULL && Handle2 == NULL)
366 ){
367 //
368 // Figure out which one should be NULL and move the handle to the right place.
369 // If Handle1 is NULL then test Handle2 and vise versa.
370 // The one that DOES has driver binding must be Handle2
371 //
372 if (Handle1 == NULL) {
373 if (EFI_ERROR(gBS->OpenProtocol(Handle2, &gEfiDriverBindingProtocolGuid, NULL, NULL, gImageHandle, EFI_OPEN_PROTOCOL_TEST_PROTOCOL))) {
374 // swap
375 Handle1 = Handle2;
376 Handle2 = NULL;
377 } else {
378 // We're all good...
379 }
380 } else {
381 if (EFI_ERROR(gBS->OpenProtocol(Handle1, &gEfiDriverBindingProtocolGuid, NULL, NULL, gImageHandle, EFI_OPEN_PROTOCOL_TEST_PROTOCOL))) {
382 // We're all good...
383 } else {
384 // swap
385 Handle2 = Handle1;
386 Handle1 = NULL;
387 }
388 }
389 }
390
391 return (ConnectControllers(Handle1, Handle2, Recursive, Output, (BOOLEAN)(Handle2 != NULL && Handle1 != NULL)));
392 }
393
394 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
395 {L"-c", TypeFlag},
396 {L"-r", TypeFlag},
397 {NULL, TypeMax}
398 };
399
400 /**
401 Function for 'connect' command.
402
403 @param[in] ImageHandle Handle to the Image (NULL if Internal).
404 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
405 **/
406 SHELL_STATUS
407 EFIAPI
408 ShellCommandRunConnect (
409 IN EFI_HANDLE ImageHandle,
410 IN EFI_SYSTEM_TABLE *SystemTable
411 )
412 {
413 EFI_STATUS Status;
414 LIST_ENTRY *Package;
415 CHAR16 *ProblemParam;
416 SHELL_STATUS ShellStatus;
417 CONST CHAR16 *Param1;
418 CONST CHAR16 *Param2;
419 UINTN Count;
420 EFI_HANDLE Handle1;
421 EFI_HANDLE Handle2;
422 UINT64 Intermediate;
423
424 ShellStatus = SHELL_SUCCESS;
425 //
426 // initialize the shell lib (we must be in non-auto-init...)
427 //
428 Status = ShellInitialize();
429 ASSERT_EFI_ERROR(Status);
430
431 Status = CommandInit();
432 ASSERT_EFI_ERROR(Status);
433
434 //
435 // parse the command line
436 //
437 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
438 if (EFI_ERROR(Status)) {
439 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
440 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, ProblemParam);
441 FreePool(ProblemParam);
442 ShellStatus = SHELL_INVALID_PARAMETER;
443 } else {
444 ASSERT(FALSE);
445 }
446 } else {
447 //
448 // if more than 2 'value' parameters (plus the name one) or either -r or -c with any value parameters we have too many parameters
449 //
450 Count = (gInReconnect?0x4:0x3);
451 if ((ShellCommandLineGetCount(Package) > Count)
452 ||((ShellCommandLineGetFlag(Package, L"-r") || ShellCommandLineGetFlag(Package, L"-c")) && ShellCommandLineGetCount(Package)>1)
453 ||(ShellCommandLineGetFlag(Package, L"-r") && ShellCommandLineGetFlag(Package, L"-c") )
454 ){
455 //
456 // error for too many parameters
457 //
458 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle);
459 ShellStatus = SHELL_INVALID_PARAMETER;
460 } else if (ShellCommandLineGetFlag(Package, L"-c")) {
461 //
462 // do the conin and conout from EFI variables
463 // if the first fails dont 'loose' the error
464 //
465 Status = ConnectFromDevPaths(L"ConInDev");
466 if (EFI_ERROR(Status)) {
467 ConnectFromDevPaths(L"ConOutDev");
468 } else {
469 Status = ConnectFromDevPaths(L"ConOutDev");
470 }
471 if (EFI_ERROR(Status)) {
472 ConnectFromDevPaths(L"ErrOutDev");
473 } else {
474 Status = ConnectFromDevPaths(L"ErrOutDev");
475 }
476 if (EFI_ERROR(Status)) {
477 ConnectFromDevPaths(L"ErrOut");
478 } else {
479 Status = ConnectFromDevPaths(L"ErrOut");
480 }
481 if (EFI_ERROR(Status)) {
482 ConnectFromDevPaths(L"ConIn");
483 } else {
484 Status = ConnectFromDevPaths(L"ConIn");
485 }
486 if (EFI_ERROR(Status)) {
487 ConnectFromDevPaths(L"ConOut");
488 } else {
489 Status = ConnectFromDevPaths(L"ConOut");
490 }
491 if (EFI_ERROR(Status)) {
492 ShellStatus = SHELL_DEVICE_ERROR;
493 }
494 } else {
495 //
496 // 0, 1, or 2 specific handles and possibly recursive
497 //
498 Param1 = ShellCommandLineGetRawValue(Package, 1);
499 Param2 = ShellCommandLineGetRawValue(Package, 2);
500 Count = ShellCommandLineGetCount(Package);
501
502 if (Param1 != NULL) {
503 Status = ShellConvertStringToUint64(Param1, &Intermediate, TRUE, FALSE);
504 Handle1 = ConvertHandleIndexToHandle((UINTN)Intermediate);
505 if (EFI_ERROR(Status)) {
506 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, Param1);
507 ShellStatus = SHELL_INVALID_PARAMETER;
508 }
509 } else {
510 Handle1 = NULL;
511 }
512
513 if (Param2 != NULL) {
514 Status = ShellConvertStringToUint64(Param2, &Intermediate, TRUE, FALSE);
515 Handle2 = ConvertHandleIndexToHandle((UINTN)Intermediate);
516 if (EFI_ERROR(Status)) {
517 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, Param2);
518 ShellStatus = SHELL_INVALID_PARAMETER;
519 }
520 } else {
521 Handle2 = NULL;
522 }
523
524 if (ShellStatus == SHELL_SUCCESS) {
525 if (Param1 != NULL && Handle1 == NULL){
526 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, Param1);
527 ShellStatus = SHELL_INVALID_PARAMETER;
528 } else if (Param2 != NULL && Handle2 == NULL) {
529 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, Param2);
530 ShellStatus = SHELL_INVALID_PARAMETER;
531 } else if (Handle2 != NULL && Handle1 != NULL && EFI_ERROR(gBS->OpenProtocol(Handle2, &gEfiDriverBindingProtocolGuid, NULL, gImageHandle, NULL, EFI_OPEN_PROTOCOL_TEST_PROTOCOL))) {
532 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, Param2);
533 ShellStatus = SHELL_INVALID_PARAMETER;
534 } else {
535 Status = ConvertAndConnectControllers(Handle1, Handle2, ShellCommandLineGetFlag(Package, L"-r"), (BOOLEAN)(Count!=0));
536 if (EFI_ERROR(Status)) {
537 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CONNECT_NONE), gShellDriver1HiiHandle);
538 ShellStatus = SHELL_DEVICE_ERROR;
539 }
540 }
541 }
542 }
543
544 ShellCommandLineFreeVarList (Package);
545 }
546 return (ShellStatus);
547 }
548