console.c 19 KB

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