console.c 20 KB

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