console.c 18 KB

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