console.c 19 KB

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