console.c 19 KB

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