console.c 19 KB

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