console.c 19 KB

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