console.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. #ifdef CONFIG_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 /* CONFIG_CONSOLE_MUX */
  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. #ifdef CONFIG_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_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 defined(CONFIG_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. #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
  176. static void console_puts_noserial(int file, const char *s)
  177. {
  178. int i;
  179. struct stdio_dev *dev;
  180. for (i = 0; i < cd_count[file]; i++) {
  181. dev = console_devices[file][i];
  182. if (dev->puts != NULL && strcmp(dev->name, "serial") != 0)
  183. dev->puts(dev, s);
  184. }
  185. }
  186. #endif
  187. static void console_puts(int file, const char *s)
  188. {
  189. int i;
  190. struct stdio_dev *dev;
  191. for (i = 0; i < cd_count[file]; i++) {
  192. dev = console_devices[file][i];
  193. if (dev->puts != NULL)
  194. dev->puts(dev, s);
  195. }
  196. }
  197. static inline void console_doenv(int file, struct stdio_dev *dev)
  198. {
  199. iomux_doenv(file, dev->name);
  200. }
  201. #else
  202. static inline int console_getc(int file)
  203. {
  204. return stdio_devices[file]->getc(stdio_devices[file]);
  205. }
  206. static inline int console_tstc(int file)
  207. {
  208. return stdio_devices[file]->tstc(stdio_devices[file]);
  209. }
  210. static inline void console_putc(int file, const char c)
  211. {
  212. stdio_devices[file]->putc(stdio_devices[file], c);
  213. }
  214. #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
  215. static inline void console_puts_noserial(int file, const char *s)
  216. {
  217. if (strcmp(stdio_devices[file]->name, "serial") != 0)
  218. stdio_devices[file]->puts(stdio_devices[file], s);
  219. }
  220. #endif
  221. static inline void console_puts(int file, const char *s)
  222. {
  223. stdio_devices[file]->puts(stdio_devices[file], s);
  224. }
  225. static inline void console_doenv(int file, struct stdio_dev *dev)
  226. {
  227. console_setfile(file, dev);
  228. }
  229. #endif /* defined(CONFIG_CONSOLE_MUX) */
  230. /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
  231. int serial_printf(const char *fmt, ...)
  232. {
  233. va_list args;
  234. uint i;
  235. char printbuffer[CONFIG_SYS_PBSIZE];
  236. va_start(args, fmt);
  237. /* For this to work, printbuffer must be larger than
  238. * anything we ever want to print.
  239. */
  240. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  241. va_end(args);
  242. serial_puts(printbuffer);
  243. return i;
  244. }
  245. int fgetc(int file)
  246. {
  247. if (file < MAX_FILES) {
  248. #if defined(CONFIG_CONSOLE_MUX)
  249. /*
  250. * Effectively poll for input wherever it may be available.
  251. */
  252. for (;;) {
  253. WATCHDOG_RESET();
  254. /*
  255. * Upper layer may have already called tstc() so
  256. * check for that first.
  257. */
  258. if (tstcdev != NULL)
  259. return console_getc(file);
  260. console_tstc(file);
  261. #ifdef CONFIG_WATCHDOG
  262. /*
  263. * If the watchdog must be rate-limited then it should
  264. * already be handled in board-specific code.
  265. */
  266. udelay(1);
  267. #endif
  268. }
  269. #else
  270. return console_getc(file);
  271. #endif
  272. }
  273. return -1;
  274. }
  275. int ftstc(int file)
  276. {
  277. if (file < MAX_FILES)
  278. return console_tstc(file);
  279. return -1;
  280. }
  281. void fputc(int file, const char c)
  282. {
  283. if (file < MAX_FILES)
  284. console_putc(file, c);
  285. }
  286. void fputs(int file, const char *s)
  287. {
  288. if (file < MAX_FILES)
  289. console_puts(file, s);
  290. }
  291. int fprintf(int file, const char *fmt, ...)
  292. {
  293. va_list args;
  294. uint i;
  295. char printbuffer[CONFIG_SYS_PBSIZE];
  296. va_start(args, fmt);
  297. /* For this to work, printbuffer must be larger than
  298. * anything we ever want to print.
  299. */
  300. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  301. va_end(args);
  302. /* Send to desired file */
  303. fputs(file, printbuffer);
  304. return i;
  305. }
  306. /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
  307. int getc(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. #ifdef CONFIG_CONSOLE_RECORD
  316. if (gd->console_in.start) {
  317. int ch;
  318. ch = membuff_getbyte(&gd->console_in);
  319. if (ch != -1)
  320. return 1;
  321. }
  322. #endif
  323. if (gd->flags & GD_FLG_DEVINIT) {
  324. /* Get from the standard input */
  325. return fgetc(stdin);
  326. }
  327. /* Send directly to the handler */
  328. return serial_getc();
  329. }
  330. int tstc(void)
  331. {
  332. #ifdef CONFIG_DISABLE_CONSOLE
  333. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  334. return 0;
  335. #endif
  336. if (!gd->have_console)
  337. return 0;
  338. #ifdef CONFIG_CONSOLE_RECORD
  339. if (gd->console_in.start) {
  340. if (membuff_peekbyte(&gd->console_in) != -1)
  341. return 1;
  342. }
  343. #endif
  344. if (gd->flags & GD_FLG_DEVINIT) {
  345. /* Test the standard input */
  346. return ftstc(stdin);
  347. }
  348. /* Send directly to the handler */
  349. return serial_tstc();
  350. }
  351. #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
  352. #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
  353. #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
  354. #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
  355. static void pre_console_putc(const char c)
  356. {
  357. char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
  358. buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
  359. }
  360. static void pre_console_puts(const char *s)
  361. {
  362. while (*s)
  363. pre_console_putc(*s++);
  364. }
  365. static void print_pre_console_buffer(int flushpoint)
  366. {
  367. unsigned long in = 0, out = 0;
  368. char *buf_in = (char *)CONFIG_PRE_CON_BUF_ADDR;
  369. char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
  370. if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
  371. in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
  372. while (in < gd->precon_buf_idx)
  373. buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
  374. buf_out[out] = 0;
  375. switch (flushpoint) {
  376. case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
  377. puts(buf_out);
  378. break;
  379. case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
  380. console_puts_noserial(stdout, buf_out);
  381. break;
  382. }
  383. }
  384. #else
  385. static inline void pre_console_putc(const char c) {}
  386. static inline void pre_console_puts(const char *s) {}
  387. static inline void print_pre_console_buffer(int flushpoint) {}
  388. #endif
  389. void putc(const char c)
  390. {
  391. #ifdef CONFIG_SANDBOX
  392. /* sandbox can send characters to stdout before it has a console */
  393. if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  394. os_putc(c);
  395. return;
  396. }
  397. #endif
  398. #ifdef CONFIG_DEBUG_UART
  399. /* if we don't have a console yet, use the debug UART */
  400. if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  401. printch(c);
  402. return;
  403. }
  404. #endif
  405. #ifdef CONFIG_CONSOLE_RECORD
  406. if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
  407. membuff_putbyte(&gd->console_out, c);
  408. #endif
  409. #ifdef CONFIG_SILENT_CONSOLE
  410. if (gd->flags & GD_FLG_SILENT)
  411. return;
  412. #endif
  413. #ifdef CONFIG_DISABLE_CONSOLE
  414. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  415. return;
  416. #endif
  417. if (!gd->have_console)
  418. return pre_console_putc(c);
  419. if (gd->flags & GD_FLG_DEVINIT) {
  420. /* Send to the standard output */
  421. fputc(stdout, c);
  422. } else {
  423. /* Send directly to the handler */
  424. pre_console_putc(c);
  425. serial_putc(c);
  426. }
  427. }
  428. void puts(const char *s)
  429. {
  430. #ifdef CONFIG_SANDBOX
  431. if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  432. os_puts(s);
  433. return;
  434. }
  435. #endif
  436. #ifdef CONFIG_DEBUG_UART
  437. if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  438. while (*s) {
  439. int ch = *s++;
  440. printch(ch);
  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. static void console_update_silent(void)
  588. {
  589. #ifdef CONFIG_SILENT_CONSOLE
  590. if (getenv("silent") != NULL)
  591. gd->flags |= GD_FLG_SILENT;
  592. else
  593. gd->flags &= ~GD_FLG_SILENT;
  594. #endif
  595. }
  596. /* Called before relocation - use serial functions */
  597. int console_init_f(void)
  598. {
  599. gd->have_console = 1;
  600. console_update_silent();
  601. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
  602. return 0;
  603. }
  604. void stdio_print_current_devices(void)
  605. {
  606. /* Print information */
  607. puts("In: ");
  608. if (stdio_devices[stdin] == NULL) {
  609. puts("No input devices available!\n");
  610. } else {
  611. printf ("%s\n", stdio_devices[stdin]->name);
  612. }
  613. puts("Out: ");
  614. if (stdio_devices[stdout] == NULL) {
  615. puts("No output devices available!\n");
  616. } else {
  617. printf ("%s\n", stdio_devices[stdout]->name);
  618. }
  619. puts("Err: ");
  620. if (stdio_devices[stderr] == NULL) {
  621. puts("No error devices available!\n");
  622. } else {
  623. printf ("%s\n", stdio_devices[stderr]->name);
  624. }
  625. }
  626. #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
  627. /* Called after the relocation - use desired console functions */
  628. int console_init_r(void)
  629. {
  630. char *stdinname, *stdoutname, *stderrname;
  631. struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  632. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  633. int i;
  634. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  635. #ifdef CONFIG_CONSOLE_MUX
  636. int iomux_err = 0;
  637. #endif
  638. /* set default handlers at first */
  639. gd->jt->getc = serial_getc;
  640. gd->jt->tstc = serial_tstc;
  641. gd->jt->putc = serial_putc;
  642. gd->jt->puts = serial_puts;
  643. gd->jt->printf = serial_printf;
  644. /* stdin stdout and stderr are in environment */
  645. /* scan for it */
  646. stdinname = getenv("stdin");
  647. stdoutname = getenv("stdout");
  648. stderrname = getenv("stderr");
  649. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  650. inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
  651. outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
  652. errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
  653. #ifdef CONFIG_CONSOLE_MUX
  654. iomux_err = iomux_doenv(stdin, stdinname);
  655. iomux_err += iomux_doenv(stdout, stdoutname);
  656. iomux_err += iomux_doenv(stderr, stderrname);
  657. if (!iomux_err)
  658. /* Successful, so skip all the code below. */
  659. goto done;
  660. #endif
  661. }
  662. /* if the devices are overwritten or not found, use default device */
  663. if (inputdev == NULL) {
  664. inputdev = search_device(DEV_FLAGS_INPUT, "serial");
  665. }
  666. if (outputdev == NULL) {
  667. outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  668. }
  669. if (errdev == NULL) {
  670. errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  671. }
  672. /* Initializes output console first */
  673. if (outputdev != NULL) {
  674. /* need to set a console if not done above. */
  675. console_doenv(stdout, outputdev);
  676. }
  677. if (errdev != NULL) {
  678. /* need to set a console if not done above. */
  679. console_doenv(stderr, errdev);
  680. }
  681. if (inputdev != NULL) {
  682. /* need to set a console if not done above. */
  683. console_doenv(stdin, inputdev);
  684. }
  685. #ifdef CONFIG_CONSOLE_MUX
  686. done:
  687. #endif
  688. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  689. stdio_print_current_devices();
  690. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  691. #ifdef CONFIG_VIDCONSOLE_AS_LCD
  692. if (strstr(stdoutname, "lcd"))
  693. printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
  694. #endif
  695. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  696. /* set the environment variables (will overwrite previous env settings) */
  697. for (i = 0; i < 3; i++) {
  698. setenv(stdio_names[i], stdio_devices[i]->name);
  699. }
  700. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  701. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  702. #if 0
  703. /* If nothing usable installed, use only the initial console */
  704. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  705. return 0;
  706. #endif
  707. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
  708. return 0;
  709. }
  710. #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
  711. /* Called after the relocation - use desired console functions */
  712. int console_init_r(void)
  713. {
  714. struct stdio_dev *inputdev = NULL, *outputdev = NULL;
  715. int i;
  716. struct list_head *list = stdio_get_list();
  717. struct list_head *pos;
  718. struct stdio_dev *dev;
  719. console_update_silent();
  720. #ifdef CONFIG_SPLASH_SCREEN
  721. /*
  722. * suppress all output if splash screen is enabled and we have
  723. * a bmp to display. We redirect the output from frame buffer
  724. * console to serial console in this case or suppress it if
  725. * "silent" mode was requested.
  726. */
  727. if (getenv("splashimage") != NULL) {
  728. if (!(gd->flags & GD_FLG_SILENT))
  729. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  730. }
  731. #endif
  732. /* Scan devices looking for input and output devices */
  733. list_for_each(pos, list) {
  734. dev = list_entry(pos, struct stdio_dev, list);
  735. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  736. inputdev = dev;
  737. }
  738. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  739. outputdev = dev;
  740. }
  741. if(inputdev && outputdev)
  742. break;
  743. }
  744. /* Initializes output console first */
  745. if (outputdev != NULL) {
  746. console_setfile(stdout, outputdev);
  747. console_setfile(stderr, outputdev);
  748. #ifdef CONFIG_CONSOLE_MUX
  749. console_devices[stdout][0] = outputdev;
  750. console_devices[stderr][0] = outputdev;
  751. #endif
  752. }
  753. /* Initializes input console */
  754. if (inputdev != NULL) {
  755. console_setfile(stdin, inputdev);
  756. #ifdef CONFIG_CONSOLE_MUX
  757. console_devices[stdin][0] = inputdev;
  758. #endif
  759. }
  760. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  761. stdio_print_current_devices();
  762. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  763. /* Setting environment variables */
  764. for (i = 0; i < 3; i++) {
  765. setenv(stdio_names[i], stdio_devices[i]->name);
  766. }
  767. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  768. #if 0
  769. /* If nothing usable installed, use only the initial console */
  770. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  771. return 0;
  772. #endif
  773. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
  774. return 0;
  775. }
  776. #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */