console.c 19 KB

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