console.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /*
  2. * (C) Copyright 2000
  3. * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <stdarg.h>
  9. #include <malloc.h>
  10. #include <os.h>
  11. #include <serial.h>
  12. #include <stdio_dev.h>
  13. #include <exports.h>
  14. #include <environment.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. static int on_console(const char *name, const char *value, enum env_op op,
  17. int flags)
  18. {
  19. int console = -1;
  20. /* Check for console redirection */
  21. if (strcmp(name, "stdin") == 0)
  22. console = stdin;
  23. else if (strcmp(name, "stdout") == 0)
  24. console = stdout;
  25. else if (strcmp(name, "stderr") == 0)
  26. console = stderr;
  27. /* if not actually setting a console variable, we don't care */
  28. if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
  29. return 0;
  30. switch (op) {
  31. case env_op_create:
  32. case env_op_overwrite:
  33. #ifdef CONFIG_CONSOLE_MUX
  34. if (iomux_doenv(console, value))
  35. return 1;
  36. #else
  37. /* Try assigning specified device */
  38. if (console_assign(console, value) < 0)
  39. return 1;
  40. #endif /* CONFIG_CONSOLE_MUX */
  41. return 0;
  42. case env_op_delete:
  43. if ((flags & H_FORCE) == 0)
  44. printf("Can't delete \"%s\"\n", name);
  45. return 1;
  46. default:
  47. return 0;
  48. }
  49. }
  50. U_BOOT_ENV_CALLBACK(console, on_console);
  51. #ifdef CONFIG_SILENT_CONSOLE
  52. static int on_silent(const char *name, const char *value, enum env_op op,
  53. int flags)
  54. {
  55. #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
  56. if (flags & H_INTERACTIVE)
  57. return 0;
  58. #endif
  59. #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
  60. if ((flags & H_INTERACTIVE) == 0)
  61. return 0;
  62. #endif
  63. if (value != NULL)
  64. gd->flags |= GD_FLG_SILENT;
  65. else
  66. gd->flags &= ~GD_FLG_SILENT;
  67. return 0;
  68. }
  69. U_BOOT_ENV_CALLBACK(silent, on_silent);
  70. #endif
  71. #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
  72. /*
  73. * if overwrite_console returns 1, the stdin, stderr and stdout
  74. * are switched to the serial port, else the settings in the
  75. * environment are used
  76. */
  77. #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
  78. extern int overwrite_console(void);
  79. #define OVERWRITE_CONSOLE overwrite_console()
  80. #else
  81. #define OVERWRITE_CONSOLE 0
  82. #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
  83. #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
  84. static int console_setfile(int file, struct stdio_dev * dev)
  85. {
  86. int error = 0;
  87. if (dev == NULL)
  88. return -1;
  89. switch (file) {
  90. case stdin:
  91. case stdout:
  92. case stderr:
  93. /* Start new device */
  94. if (dev->start) {
  95. error = dev->start(dev);
  96. /* If it's not started dont use it */
  97. if (error < 0)
  98. break;
  99. }
  100. /* Assign the new device (leaving the existing one started) */
  101. stdio_devices[file] = dev;
  102. /*
  103. * Update monitor functions
  104. * (to use the console stuff by other applications)
  105. */
  106. switch (file) {
  107. case stdin:
  108. gd->jt[XF_getc] = dev->getc;
  109. gd->jt[XF_tstc] = dev->tstc;
  110. break;
  111. case stdout:
  112. gd->jt[XF_putc] = dev->putc;
  113. gd->jt[XF_puts] = dev->puts;
  114. gd->jt[XF_printf] = printf;
  115. break;
  116. }
  117. break;
  118. default: /* Invalid file ID */
  119. error = -1;
  120. }
  121. return error;
  122. }
  123. #if defined(CONFIG_CONSOLE_MUX)
  124. /** Console I/O multiplexing *******************************************/
  125. static struct stdio_dev *tstcdev;
  126. struct stdio_dev **console_devices[MAX_FILES];
  127. int cd_count[MAX_FILES];
  128. /*
  129. * This depends on tstc() always being called before getc().
  130. * This is guaranteed to be true because this routine is called
  131. * only from fgetc() which assures it.
  132. * No attempt is made to demultiplex multiple input sources.
  133. */
  134. static int console_getc(int file)
  135. {
  136. unsigned char ret;
  137. /* This is never called with testcdev == NULL */
  138. ret = tstcdev->getc(tstcdev);
  139. tstcdev = NULL;
  140. return ret;
  141. }
  142. static int console_tstc(int file)
  143. {
  144. int i, ret;
  145. struct stdio_dev *dev;
  146. disable_ctrlc(1);
  147. for (i = 0; i < cd_count[file]; i++) {
  148. dev = console_devices[file][i];
  149. if (dev->tstc != NULL) {
  150. ret = dev->tstc(dev);
  151. if (ret > 0) {
  152. tstcdev = dev;
  153. disable_ctrlc(0);
  154. return ret;
  155. }
  156. }
  157. }
  158. disable_ctrlc(0);
  159. return 0;
  160. }
  161. static void console_putc(int file, const char c)
  162. {
  163. int i;
  164. struct stdio_dev *dev;
  165. for (i = 0; i < cd_count[file]; i++) {
  166. dev = console_devices[file][i];
  167. if (dev->putc != NULL)
  168. dev->putc(dev, c);
  169. }
  170. }
  171. static void console_puts(int file, const char *s)
  172. {
  173. int i;
  174. struct stdio_dev *dev;
  175. for (i = 0; i < cd_count[file]; i++) {
  176. dev = console_devices[file][i];
  177. if (dev->puts != NULL)
  178. dev->puts(dev, s);
  179. }
  180. }
  181. static inline void console_printdevs(int file)
  182. {
  183. iomux_printdevs(file);
  184. }
  185. static inline void console_doenv(int file, struct stdio_dev *dev)
  186. {
  187. iomux_doenv(file, dev->name);
  188. }
  189. #else
  190. static inline int console_getc(int file)
  191. {
  192. return stdio_devices[file]->getc(stdio_devices[file]);
  193. }
  194. static inline int console_tstc(int file)
  195. {
  196. return stdio_devices[file]->tstc(stdio_devices[file]);
  197. }
  198. static inline void console_putc(int file, const char c)
  199. {
  200. stdio_devices[file]->putc(stdio_devices[file], c);
  201. }
  202. static inline void console_puts(int file, const char *s)
  203. {
  204. stdio_devices[file]->puts(stdio_devices[file], s);
  205. }
  206. static inline void console_printdevs(int file)
  207. {
  208. printf("%s\n", stdio_devices[file]->name);
  209. }
  210. static inline void console_doenv(int file, struct stdio_dev *dev)
  211. {
  212. console_setfile(file, dev);
  213. }
  214. #endif /* defined(CONFIG_CONSOLE_MUX) */
  215. /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
  216. int serial_printf(const char *fmt, ...)
  217. {
  218. va_list args;
  219. uint i;
  220. char printbuffer[CONFIG_SYS_PBSIZE];
  221. va_start(args, fmt);
  222. /* For this to work, printbuffer must be larger than
  223. * anything we ever want to print.
  224. */
  225. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  226. va_end(args);
  227. serial_puts(printbuffer);
  228. return i;
  229. }
  230. int fgetc(int file)
  231. {
  232. if (file < MAX_FILES) {
  233. #if defined(CONFIG_CONSOLE_MUX)
  234. /*
  235. * Effectively poll for input wherever it may be available.
  236. */
  237. for (;;) {
  238. /*
  239. * Upper layer may have already called tstc() so
  240. * check for that first.
  241. */
  242. if (tstcdev != NULL)
  243. return console_getc(file);
  244. console_tstc(file);
  245. #ifdef CONFIG_WATCHDOG
  246. /*
  247. * If the watchdog must be rate-limited then it should
  248. * already be handled in board-specific code.
  249. */
  250. udelay(1);
  251. #endif
  252. }
  253. #else
  254. return console_getc(file);
  255. #endif
  256. }
  257. return -1;
  258. }
  259. int ftstc(int file)
  260. {
  261. if (file < MAX_FILES)
  262. return console_tstc(file);
  263. return -1;
  264. }
  265. void fputc(int file, const char c)
  266. {
  267. if (file < MAX_FILES)
  268. console_putc(file, c);
  269. }
  270. void fputs(int file, const char *s)
  271. {
  272. if (file < MAX_FILES)
  273. console_puts(file, s);
  274. }
  275. int fprintf(int file, const char *fmt, ...)
  276. {
  277. va_list args;
  278. uint i;
  279. char printbuffer[CONFIG_SYS_PBSIZE];
  280. va_start(args, fmt);
  281. /* For this to work, printbuffer must be larger than
  282. * anything we ever want to print.
  283. */
  284. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  285. va_end(args);
  286. /* Send to desired file */
  287. fputs(file, printbuffer);
  288. return i;
  289. }
  290. /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
  291. int getc(void)
  292. {
  293. #ifdef CONFIG_DISABLE_CONSOLE
  294. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  295. return 0;
  296. #endif
  297. if (!gd->have_console)
  298. return 0;
  299. if (gd->flags & GD_FLG_DEVINIT) {
  300. /* Get from the standard input */
  301. return fgetc(stdin);
  302. }
  303. /* Send directly to the handler */
  304. return serial_getc();
  305. }
  306. int tstc(void)
  307. {
  308. #ifdef CONFIG_DISABLE_CONSOLE
  309. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  310. return 0;
  311. #endif
  312. if (!gd->have_console)
  313. return 0;
  314. if (gd->flags & GD_FLG_DEVINIT) {
  315. /* Test the standard input */
  316. return ftstc(stdin);
  317. }
  318. /* Send directly to the handler */
  319. return serial_tstc();
  320. }
  321. #ifdef CONFIG_PRE_CONSOLE_BUFFER
  322. #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
  323. static void pre_console_putc(const char c)
  324. {
  325. char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
  326. buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
  327. }
  328. static void pre_console_puts(const char *s)
  329. {
  330. while (*s)
  331. pre_console_putc(*s++);
  332. }
  333. static void print_pre_console_buffer(void)
  334. {
  335. unsigned long i = 0;
  336. char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
  337. if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
  338. i = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
  339. while (i < gd->precon_buf_idx)
  340. putc(buffer[CIRC_BUF_IDX(i++)]);
  341. }
  342. #else
  343. static inline void pre_console_putc(const char c) {}
  344. static inline void pre_console_puts(const char *s) {}
  345. static inline void print_pre_console_buffer(void) {}
  346. #endif
  347. void putc(const char c)
  348. {
  349. #ifdef CONFIG_SANDBOX
  350. if (!gd) {
  351. os_putc(c);
  352. return;
  353. }
  354. #endif
  355. #ifdef CONFIG_SILENT_CONSOLE
  356. if (gd->flags & GD_FLG_SILENT)
  357. return;
  358. #endif
  359. #ifdef CONFIG_DISABLE_CONSOLE
  360. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  361. return;
  362. #endif
  363. if (!gd->have_console)
  364. return pre_console_putc(c);
  365. if (gd->flags & GD_FLG_DEVINIT) {
  366. /* Send to the standard output */
  367. fputc(stdout, c);
  368. } else {
  369. /* Send directly to the handler */
  370. serial_putc(c);
  371. }
  372. }
  373. void puts(const char *s)
  374. {
  375. #ifdef CONFIG_SANDBOX
  376. if (!gd) {
  377. os_puts(s);
  378. return;
  379. }
  380. #endif
  381. #ifdef CONFIG_SILENT_CONSOLE
  382. if (gd->flags & GD_FLG_SILENT)
  383. return;
  384. #endif
  385. #ifdef CONFIG_DISABLE_CONSOLE
  386. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  387. return;
  388. #endif
  389. if (!gd->have_console)
  390. return pre_console_puts(s);
  391. if (gd->flags & GD_FLG_DEVINIT) {
  392. /* Send to the standard output */
  393. fputs(stdout, s);
  394. } else {
  395. /* Send directly to the handler */
  396. serial_puts(s);
  397. }
  398. }
  399. int printf(const char *fmt, ...)
  400. {
  401. va_list args;
  402. uint i;
  403. char printbuffer[CONFIG_SYS_PBSIZE];
  404. #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_PRE_CONSOLE_BUFFER)
  405. if (!gd->have_console)
  406. return 0;
  407. #endif
  408. va_start(args, fmt);
  409. /* For this to work, printbuffer must be larger than
  410. * anything we ever want to print.
  411. */
  412. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  413. va_end(args);
  414. /* Print the string */
  415. puts(printbuffer);
  416. return i;
  417. }
  418. int vprintf(const char *fmt, va_list args)
  419. {
  420. uint i;
  421. char printbuffer[CONFIG_SYS_PBSIZE];
  422. #ifndef CONFIG_PRE_CONSOLE_BUFFER
  423. if (!gd->have_console)
  424. return 0;
  425. #endif
  426. /* For this to work, printbuffer must be larger than
  427. * anything we ever want to print.
  428. */
  429. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  430. /* Print the string */
  431. puts(printbuffer);
  432. return i;
  433. }
  434. /* test if ctrl-c was pressed */
  435. static int ctrlc_disabled = 0; /* see disable_ctrl() */
  436. static int ctrlc_was_pressed = 0;
  437. int ctrlc(void)
  438. {
  439. if (!ctrlc_disabled && gd->have_console) {
  440. if (tstc()) {
  441. switch (getc()) {
  442. case 0x03: /* ^C - Control C */
  443. ctrlc_was_pressed = 1;
  444. return 1;
  445. default:
  446. break;
  447. }
  448. }
  449. }
  450. return 0;
  451. }
  452. /* Reads user's confirmation.
  453. Returns 1 if user's input is "y", "Y", "yes" or "YES"
  454. */
  455. int confirm_yesno(void)
  456. {
  457. int i;
  458. char str_input[5];
  459. /* Flush input */
  460. while (tstc())
  461. getc();
  462. i = 0;
  463. while (i < sizeof(str_input)) {
  464. str_input[i] = getc();
  465. putc(str_input[i]);
  466. if (str_input[i] == '\r')
  467. break;
  468. i++;
  469. }
  470. putc('\n');
  471. if (strncmp(str_input, "y\r", 2) == 0 ||
  472. strncmp(str_input, "Y\r", 2) == 0 ||
  473. strncmp(str_input, "yes\r", 4) == 0 ||
  474. strncmp(str_input, "YES\r", 4) == 0)
  475. return 1;
  476. return 0;
  477. }
  478. /* pass 1 to disable ctrlc() checking, 0 to enable.
  479. * returns previous state
  480. */
  481. int disable_ctrlc(int disable)
  482. {
  483. int prev = ctrlc_disabled; /* save previous state */
  484. ctrlc_disabled = disable;
  485. return prev;
  486. }
  487. int had_ctrlc (void)
  488. {
  489. return ctrlc_was_pressed;
  490. }
  491. void clear_ctrlc(void)
  492. {
  493. ctrlc_was_pressed = 0;
  494. }
  495. #ifdef CONFIG_MODEM_SUPPORT_DEBUG
  496. char screen[1024];
  497. char *cursor = screen;
  498. int once = 0;
  499. inline void dbg(const char *fmt, ...)
  500. {
  501. va_list args;
  502. uint i;
  503. char printbuffer[CONFIG_SYS_PBSIZE];
  504. if (!once) {
  505. memset(screen, 0, sizeof(screen));
  506. once++;
  507. }
  508. va_start(args, fmt);
  509. /* For this to work, printbuffer must be larger than
  510. * anything we ever want to print.
  511. */
  512. i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  513. va_end(args);
  514. if ((screen + sizeof(screen) - 1 - cursor)
  515. < strlen(printbuffer) + 1) {
  516. memset(screen, 0, sizeof(screen));
  517. cursor = screen;
  518. }
  519. sprintf(cursor, printbuffer);
  520. cursor += strlen(printbuffer);
  521. }
  522. #else
  523. inline void dbg(const char *fmt, ...)
  524. {
  525. }
  526. #endif
  527. /** U-Boot INIT FUNCTIONS *************************************************/
  528. struct stdio_dev *search_device(int flags, const char *name)
  529. {
  530. struct stdio_dev *dev;
  531. dev = stdio_get_by_name(name);
  532. if (dev && (dev->flags & flags))
  533. return dev;
  534. return NULL;
  535. }
  536. int console_assign(int file, const char *devname)
  537. {
  538. int flag;
  539. struct stdio_dev *dev;
  540. /* Check for valid file */
  541. switch (file) {
  542. case stdin:
  543. flag = DEV_FLAGS_INPUT;
  544. break;
  545. case stdout:
  546. case stderr:
  547. flag = DEV_FLAGS_OUTPUT;
  548. break;
  549. default:
  550. return -1;
  551. }
  552. /* Check for valid device name */
  553. dev = search_device(flag, devname);
  554. if (dev)
  555. return console_setfile(file, dev);
  556. return -1;
  557. }
  558. /* Called before relocation - use serial functions */
  559. int console_init_f(void)
  560. {
  561. gd->have_console = 1;
  562. #ifdef CONFIG_SILENT_CONSOLE
  563. if (getenv("silent") != NULL)
  564. gd->flags |= GD_FLG_SILENT;
  565. #endif
  566. print_pre_console_buffer();
  567. return 0;
  568. }
  569. void stdio_print_current_devices(void)
  570. {
  571. /* Print information */
  572. puts("In: ");
  573. if (stdio_devices[stdin] == NULL) {
  574. puts("No input devices available!\n");
  575. } else {
  576. printf ("%s\n", stdio_devices[stdin]->name);
  577. }
  578. puts("Out: ");
  579. if (stdio_devices[stdout] == NULL) {
  580. puts("No output devices available!\n");
  581. } else {
  582. printf ("%s\n", stdio_devices[stdout]->name);
  583. }
  584. puts("Err: ");
  585. if (stdio_devices[stderr] == NULL) {
  586. puts("No error devices available!\n");
  587. } else {
  588. printf ("%s\n", stdio_devices[stderr]->name);
  589. }
  590. }
  591. #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
  592. /* Called after the relocation - use desired console functions */
  593. int console_init_r(void)
  594. {
  595. char *stdinname, *stdoutname, *stderrname;
  596. struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  597. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  598. int i;
  599. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  600. #ifdef CONFIG_CONSOLE_MUX
  601. int iomux_err = 0;
  602. #endif
  603. /* set default handlers at first */
  604. gd->jt[XF_getc] = serial_getc;
  605. gd->jt[XF_tstc] = serial_tstc;
  606. gd->jt[XF_putc] = serial_putc;
  607. gd->jt[XF_puts] = serial_puts;
  608. gd->jt[XF_printf] = serial_printf;
  609. /* stdin stdout and stderr are in environment */
  610. /* scan for it */
  611. stdinname = getenv("stdin");
  612. stdoutname = getenv("stdout");
  613. stderrname = getenv("stderr");
  614. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  615. inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
  616. outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
  617. errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
  618. #ifdef CONFIG_CONSOLE_MUX
  619. iomux_err = iomux_doenv(stdin, stdinname);
  620. iomux_err += iomux_doenv(stdout, stdoutname);
  621. iomux_err += iomux_doenv(stderr, stderrname);
  622. if (!iomux_err)
  623. /* Successful, so skip all the code below. */
  624. goto done;
  625. #endif
  626. }
  627. /* if the devices are overwritten or not found, use default device */
  628. if (inputdev == NULL) {
  629. inputdev = search_device(DEV_FLAGS_INPUT, "serial");
  630. }
  631. if (outputdev == NULL) {
  632. outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  633. }
  634. if (errdev == NULL) {
  635. errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  636. }
  637. /* Initializes output console first */
  638. if (outputdev != NULL) {
  639. /* need to set a console if not done above. */
  640. console_doenv(stdout, outputdev);
  641. }
  642. if (errdev != NULL) {
  643. /* need to set a console if not done above. */
  644. console_doenv(stderr, errdev);
  645. }
  646. if (inputdev != NULL) {
  647. /* need to set a console if not done above. */
  648. console_doenv(stdin, inputdev);
  649. }
  650. #ifdef CONFIG_CONSOLE_MUX
  651. done:
  652. #endif
  653. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  654. stdio_print_current_devices();
  655. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  656. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  657. /* set the environment variables (will overwrite previous env settings) */
  658. for (i = 0; i < 3; i++) {
  659. setenv(stdio_names[i], stdio_devices[i]->name);
  660. }
  661. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  662. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  663. #if 0
  664. /* If nothing usable installed, use only the initial console */
  665. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  666. return 0;
  667. #endif
  668. return 0;
  669. }
  670. #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
  671. /* Called after the relocation - use desired console functions */
  672. int console_init_r(void)
  673. {
  674. struct stdio_dev *inputdev = NULL, *outputdev = NULL;
  675. int i;
  676. struct list_head *list = stdio_get_list();
  677. struct list_head *pos;
  678. struct stdio_dev *dev;
  679. #ifdef CONFIG_SPLASH_SCREEN
  680. /*
  681. * suppress all output if splash screen is enabled and we have
  682. * a bmp to display. We redirect the output from frame buffer
  683. * console to serial console in this case or suppress it if
  684. * "silent" mode was requested.
  685. */
  686. if (getenv("splashimage") != NULL) {
  687. if (!(gd->flags & GD_FLG_SILENT))
  688. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  689. }
  690. #endif
  691. /* Scan devices looking for input and output devices */
  692. list_for_each(pos, list) {
  693. dev = list_entry(pos, struct stdio_dev, list);
  694. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  695. inputdev = dev;
  696. }
  697. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  698. outputdev = dev;
  699. }
  700. if(inputdev && outputdev)
  701. break;
  702. }
  703. /* Initializes output console first */
  704. if (outputdev != NULL) {
  705. console_setfile(stdout, outputdev);
  706. console_setfile(stderr, outputdev);
  707. #ifdef CONFIG_CONSOLE_MUX
  708. console_devices[stdout][0] = outputdev;
  709. console_devices[stderr][0] = outputdev;
  710. #endif
  711. }
  712. /* Initializes input console */
  713. if (inputdev != NULL) {
  714. console_setfile(stdin, inputdev);
  715. #ifdef CONFIG_CONSOLE_MUX
  716. console_devices[stdin][0] = inputdev;
  717. #endif
  718. }
  719. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  720. stdio_print_current_devices();
  721. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  722. /* Setting environment variables */
  723. for (i = 0; i < 3; i++) {
  724. setenv(stdio_names[i], stdio_devices[i]->name);
  725. }
  726. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  727. #if 0
  728. /* If nothing usable installed, use only the initial console */
  729. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  730. return 0;
  731. #endif
  732. return 0;
  733. }
  734. #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */