console.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. if (ch == '\n')
  440. printch('\r');
  441. }
  442. return;
  443. }
  444. #endif
  445. #ifdef CONFIG_CONSOLE_RECORD
  446. if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
  447. membuff_put(&gd->console_out, s, strlen(s));
  448. #endif
  449. #ifdef CONFIG_SILENT_CONSOLE
  450. if (gd->flags & GD_FLG_SILENT)
  451. return;
  452. #endif
  453. #ifdef CONFIG_DISABLE_CONSOLE
  454. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  455. return;
  456. #endif
  457. if (!gd->have_console)
  458. return pre_console_puts(s);
  459. if (gd->flags & GD_FLG_DEVINIT) {
  460. /* Send to the standard output */
  461. fputs(stdout, s);
  462. } else {
  463. /* Send directly to the handler */
  464. pre_console_puts(s);
  465. serial_puts(s);
  466. }
  467. }
  468. #ifdef CONFIG_CONSOLE_RECORD
  469. int console_record_init(void)
  470. {
  471. int ret;
  472. ret = membuff_new(&gd->console_out, CONFIG_CONSOLE_RECORD_OUT_SIZE);
  473. if (ret)
  474. return ret;
  475. ret = membuff_new(&gd->console_in, CONFIG_CONSOLE_RECORD_IN_SIZE);
  476. return ret;
  477. }
  478. void console_record_reset(void)
  479. {
  480. membuff_purge(&gd->console_out);
  481. membuff_purge(&gd->console_in);
  482. }
  483. void console_record_reset_enable(void)
  484. {
  485. console_record_reset();
  486. gd->flags |= GD_FLG_RECORD;
  487. }
  488. #endif
  489. /* test if ctrl-c was pressed */
  490. static int ctrlc_disabled = 0; /* see disable_ctrl() */
  491. static int ctrlc_was_pressed = 0;
  492. int ctrlc(void)
  493. {
  494. #ifndef CONFIG_SANDBOX
  495. if (!ctrlc_disabled && gd->have_console) {
  496. if (tstc()) {
  497. switch (getc()) {
  498. case 0x03: /* ^C - Control C */
  499. ctrlc_was_pressed = 1;
  500. return 1;
  501. default:
  502. break;
  503. }
  504. }
  505. }
  506. #endif
  507. return 0;
  508. }
  509. /* Reads user's confirmation.
  510. Returns 1 if user's input is "y", "Y", "yes" or "YES"
  511. */
  512. int confirm_yesno(void)
  513. {
  514. int i;
  515. char str_input[5];
  516. /* Flush input */
  517. while (tstc())
  518. getc();
  519. i = 0;
  520. while (i < sizeof(str_input)) {
  521. str_input[i] = getc();
  522. putc(str_input[i]);
  523. if (str_input[i] == '\r')
  524. break;
  525. i++;
  526. }
  527. putc('\n');
  528. if (strncmp(str_input, "y\r", 2) == 0 ||
  529. strncmp(str_input, "Y\r", 2) == 0 ||
  530. strncmp(str_input, "yes\r", 4) == 0 ||
  531. strncmp(str_input, "YES\r", 4) == 0)
  532. return 1;
  533. return 0;
  534. }
  535. /* pass 1 to disable ctrlc() checking, 0 to enable.
  536. * returns previous state
  537. */
  538. int disable_ctrlc(int disable)
  539. {
  540. int prev = ctrlc_disabled; /* save previous state */
  541. ctrlc_disabled = disable;
  542. return prev;
  543. }
  544. int had_ctrlc (void)
  545. {
  546. return ctrlc_was_pressed;
  547. }
  548. void clear_ctrlc(void)
  549. {
  550. ctrlc_was_pressed = 0;
  551. }
  552. /** U-Boot INIT FUNCTIONS *************************************************/
  553. struct stdio_dev *search_device(int flags, const char *name)
  554. {
  555. struct stdio_dev *dev;
  556. dev = stdio_get_by_name(name);
  557. #ifdef CONFIG_VIDCONSOLE_AS_LCD
  558. if (!dev && !strcmp(name, "lcd"))
  559. dev = stdio_get_by_name("vidconsole");
  560. #endif
  561. if (dev && (dev->flags & flags))
  562. return dev;
  563. return NULL;
  564. }
  565. int console_assign(int file, const char *devname)
  566. {
  567. int flag;
  568. struct stdio_dev *dev;
  569. /* Check for valid file */
  570. switch (file) {
  571. case stdin:
  572. flag = DEV_FLAGS_INPUT;
  573. break;
  574. case stdout:
  575. case stderr:
  576. flag = DEV_FLAGS_OUTPUT;
  577. break;
  578. default:
  579. return -1;
  580. }
  581. /* Check for valid device name */
  582. dev = search_device(flag, devname);
  583. if (dev)
  584. return console_setfile(file, dev);
  585. return -1;
  586. }
  587. /* Called before relocation - use serial functions */
  588. int console_init_f(void)
  589. {
  590. gd->have_console = 1;
  591. #ifdef CONFIG_SILENT_CONSOLE
  592. if (getenv("silent") != NULL)
  593. gd->flags |= GD_FLG_SILENT;
  594. #endif
  595. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
  596. return 0;
  597. }
  598. void stdio_print_current_devices(void)
  599. {
  600. /* Print information */
  601. puts("In: ");
  602. if (stdio_devices[stdin] == NULL) {
  603. puts("No input devices available!\n");
  604. } else {
  605. printf ("%s\n", stdio_devices[stdin]->name);
  606. }
  607. puts("Out: ");
  608. if (stdio_devices[stdout] == NULL) {
  609. puts("No output devices available!\n");
  610. } else {
  611. printf ("%s\n", stdio_devices[stdout]->name);
  612. }
  613. puts("Err: ");
  614. if (stdio_devices[stderr] == NULL) {
  615. puts("No error devices available!\n");
  616. } else {
  617. printf ("%s\n", stdio_devices[stderr]->name);
  618. }
  619. }
  620. #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
  621. /* Called after the relocation - use desired console functions */
  622. int console_init_r(void)
  623. {
  624. char *stdinname, *stdoutname, *stderrname;
  625. struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  626. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  627. int i;
  628. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  629. #ifdef CONFIG_CONSOLE_MUX
  630. int iomux_err = 0;
  631. #endif
  632. /* set default handlers at first */
  633. gd->jt->getc = serial_getc;
  634. gd->jt->tstc = serial_tstc;
  635. gd->jt->putc = serial_putc;
  636. gd->jt->puts = serial_puts;
  637. gd->jt->printf = serial_printf;
  638. /* stdin stdout and stderr are in environment */
  639. /* scan for it */
  640. stdinname = getenv("stdin");
  641. stdoutname = getenv("stdout");
  642. stderrname = getenv("stderr");
  643. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  644. inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
  645. outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
  646. errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
  647. #ifdef CONFIG_CONSOLE_MUX
  648. iomux_err = iomux_doenv(stdin, stdinname);
  649. iomux_err += iomux_doenv(stdout, stdoutname);
  650. iomux_err += iomux_doenv(stderr, stderrname);
  651. if (!iomux_err)
  652. /* Successful, so skip all the code below. */
  653. goto done;
  654. #endif
  655. }
  656. /* if the devices are overwritten or not found, use default device */
  657. if (inputdev == NULL) {
  658. inputdev = search_device(DEV_FLAGS_INPUT, "serial");
  659. }
  660. if (outputdev == NULL) {
  661. outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  662. }
  663. if (errdev == NULL) {
  664. errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  665. }
  666. /* Initializes output console first */
  667. if (outputdev != NULL) {
  668. /* need to set a console if not done above. */
  669. console_doenv(stdout, outputdev);
  670. }
  671. if (errdev != NULL) {
  672. /* need to set a console if not done above. */
  673. console_doenv(stderr, errdev);
  674. }
  675. if (inputdev != NULL) {
  676. /* need to set a console if not done above. */
  677. console_doenv(stdin, inputdev);
  678. }
  679. #ifdef CONFIG_CONSOLE_MUX
  680. done:
  681. #endif
  682. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  683. stdio_print_current_devices();
  684. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  685. #ifdef CONFIG_VIDCONSOLE_AS_LCD
  686. if (strstr(stdoutname, "lcd"))
  687. printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
  688. #endif
  689. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  690. /* set the environment variables (will overwrite previous env settings) */
  691. for (i = 0; i < 3; i++) {
  692. setenv(stdio_names[i], stdio_devices[i]->name);
  693. }
  694. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  695. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  696. #if 0
  697. /* If nothing usable installed, use only the initial console */
  698. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  699. return 0;
  700. #endif
  701. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
  702. return 0;
  703. }
  704. #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
  705. /* Called after the relocation - use desired console functions */
  706. int console_init_r(void)
  707. {
  708. struct stdio_dev *inputdev = NULL, *outputdev = NULL;
  709. int i;
  710. struct list_head *list = stdio_get_list();
  711. struct list_head *pos;
  712. struct stdio_dev *dev;
  713. #ifdef CONFIG_SPLASH_SCREEN
  714. /*
  715. * suppress all output if splash screen is enabled and we have
  716. * a bmp to display. We redirect the output from frame buffer
  717. * console to serial console in this case or suppress it if
  718. * "silent" mode was requested.
  719. */
  720. if (getenv("splashimage") != NULL) {
  721. if (!(gd->flags & GD_FLG_SILENT))
  722. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  723. }
  724. #endif
  725. /* Scan devices looking for input and output devices */
  726. list_for_each(pos, list) {
  727. dev = list_entry(pos, struct stdio_dev, list);
  728. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  729. inputdev = dev;
  730. }
  731. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  732. outputdev = dev;
  733. }
  734. if(inputdev && outputdev)
  735. break;
  736. }
  737. /* Initializes output console first */
  738. if (outputdev != NULL) {
  739. console_setfile(stdout, outputdev);
  740. console_setfile(stderr, outputdev);
  741. #ifdef CONFIG_CONSOLE_MUX
  742. console_devices[stdout][0] = outputdev;
  743. console_devices[stderr][0] = outputdev;
  744. #endif
  745. }
  746. /* Initializes input console */
  747. if (inputdev != NULL) {
  748. console_setfile(stdin, inputdev);
  749. #ifdef CONFIG_CONSOLE_MUX
  750. console_devices[stdin][0] = inputdev;
  751. #endif
  752. }
  753. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  754. stdio_print_current_devices();
  755. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  756. /* Setting environment variables */
  757. for (i = 0; i < 3; i++) {
  758. setenv(stdio_names[i], stdio_devices[i]->name);
  759. }
  760. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  761. #if 0
  762. /* If nothing usable installed, use only the initial console */
  763. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  764. return 0;
  765. #endif
  766. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
  767. return 0;
  768. }
  769. #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */