Connect.c 17 KB

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