console.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. int prev;
  171. prev = 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(prev);
  179. return ret;
  180. }
  181. }
  182. }
  183. disable_ctrlc(prev);
  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 pre_console_puts(const char *s)
  380. {
  381. while (*s)
  382. pre_console_putc(*s++);
  383. }
  384. static void print_pre_console_buffer(int flushpoint)
  385. {
  386. unsigned long in = 0, out = 0;
  387. char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
  388. char *buf_in;
  389. buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
  390. if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
  391. in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
  392. while (in < gd->precon_buf_idx)
  393. buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
  394. unmap_sysmem(buf_in);
  395. buf_out[out] = 0;
  396. switch (flushpoint) {
  397. case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
  398. puts(buf_out);
  399. break;
  400. case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
  401. console_puts_noserial(stdout, buf_out);
  402. break;
  403. }
  404. }
  405. #else
  406. static inline void pre_console_putc(const char c) {}
  407. static inline void pre_console_puts(const char *s) {}
  408. static inline void print_pre_console_buffer(int flushpoint) {}
  409. #endif
  410. void putc(const char c)
  411. {
  412. #ifdef CONFIG_SANDBOX
  413. /* sandbox can send characters to stdout before it has a console */
  414. if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  415. os_putc(c);
  416. return;
  417. }
  418. #endif
  419. #ifdef CONFIG_DEBUG_UART
  420. /* if we don't have a console yet, use the debug UART */
  421. if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  422. printch(c);
  423. return;
  424. }
  425. #endif
  426. if (!gd)
  427. return;
  428. #ifdef CONFIG_CONSOLE_RECORD
  429. if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
  430. membuff_putbyte(&gd->console_out, c);
  431. #endif
  432. #ifdef CONFIG_SILENT_CONSOLE
  433. if (gd->flags & GD_FLG_SILENT)
  434. return;
  435. #endif
  436. #ifdef CONFIG_DISABLE_CONSOLE
  437. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  438. return;
  439. #endif
  440. if (!gd->have_console)
  441. return pre_console_putc(c);
  442. if (gd->flags & GD_FLG_DEVINIT) {
  443. /* Send to the standard output */
  444. fputc(stdout, c);
  445. } else {
  446. /* Send directly to the handler */
  447. pre_console_putc(c);
  448. serial_putc(c);
  449. }
  450. }
  451. void puts(const char *s)
  452. {
  453. #ifdef CONFIG_DEBUG_UART
  454. if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  455. while (*s) {
  456. int ch = *s++;
  457. printch(ch);
  458. }
  459. return;
  460. }
  461. #endif
  462. if (!gd)
  463. return;
  464. #ifdef CONFIG_CONSOLE_RECORD
  465. if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
  466. membuff_put(&gd->console_out, s, strlen(s));
  467. #endif
  468. #ifdef CONFIG_SILENT_CONSOLE
  469. if (gd->flags & GD_FLG_SILENT)
  470. return;
  471. #endif
  472. #ifdef CONFIG_DISABLE_CONSOLE
  473. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  474. return;
  475. #endif
  476. if (!gd->have_console)
  477. return pre_console_puts(s);
  478. if (gd->flags & GD_FLG_DEVINIT) {
  479. /* Send to the standard output */
  480. fputs(stdout, s);
  481. } else {
  482. /* Send directly to the handler */
  483. pre_console_puts(s);
  484. serial_puts(s);
  485. }
  486. }
  487. #ifdef CONFIG_CONSOLE_RECORD
  488. int console_record_init(void)
  489. {
  490. int ret;
  491. ret = membuff_new(&gd->console_out, CONFIG_CONSOLE_RECORD_OUT_SIZE);
  492. if (ret)
  493. return ret;
  494. ret = membuff_new(&gd->console_in, CONFIG_CONSOLE_RECORD_IN_SIZE);
  495. return ret;
  496. }
  497. void console_record_reset(void)
  498. {
  499. membuff_purge(&gd->console_out);
  500. membuff_purge(&gd->console_in);
  501. }
  502. void console_record_reset_enable(void)
  503. {
  504. console_record_reset();
  505. gd->flags |= GD_FLG_RECORD;
  506. }
  507. #endif
  508. /* test if ctrl-c was pressed */
  509. static int ctrlc_disabled = 0; /* see disable_ctrl() */
  510. static int ctrlc_was_pressed = 0;
  511. int ctrlc(void)
  512. {
  513. #ifndef CONFIG_SANDBOX
  514. if (!ctrlc_disabled && gd->have_console) {
  515. if (tstc()) {
  516. switch (getc()) {
  517. case 0x03: /* ^C - Control C */
  518. ctrlc_was_pressed = 1;
  519. return 1;
  520. default:
  521. break;
  522. }
  523. }
  524. }
  525. #endif
  526. return 0;
  527. }
  528. /* Reads user's confirmation.
  529. Returns 1 if user's input is "y", "Y", "yes" or "YES"
  530. */
  531. int confirm_yesno(void)
  532. {
  533. int i;
  534. char str_input[5];
  535. /* Flush input */
  536. while (tstc())
  537. getc();
  538. i = 0;
  539. while (i < sizeof(str_input)) {
  540. str_input[i] = getc();
  541. putc(str_input[i]);
  542. if (str_input[i] == '\r')
  543. break;
  544. i++;
  545. }
  546. putc('\n');
  547. if (strncmp(str_input, "y\r", 2) == 0 ||
  548. strncmp(str_input, "Y\r", 2) == 0 ||
  549. strncmp(str_input, "yes\r", 4) == 0 ||
  550. strncmp(str_input, "YES\r", 4) == 0)
  551. return 1;
  552. return 0;
  553. }
  554. /* pass 1 to disable ctrlc() checking, 0 to enable.
  555. * returns previous state
  556. */
  557. int disable_ctrlc(int disable)
  558. {
  559. int prev = ctrlc_disabled; /* save previous state */
  560. ctrlc_disabled = disable;
  561. return prev;
  562. }
  563. int had_ctrlc (void)
  564. {
  565. return ctrlc_was_pressed;
  566. }
  567. void clear_ctrlc(void)
  568. {
  569. ctrlc_was_pressed = 0;
  570. }
  571. /** U-Boot INIT FUNCTIONS *************************************************/
  572. struct stdio_dev *search_device(int flags, const char *name)
  573. {
  574. struct stdio_dev *dev;
  575. dev = stdio_get_by_name(name);
  576. #ifdef CONFIG_VIDCONSOLE_AS_LCD
  577. if (!dev && !strcmp(name, "lcd"))
  578. dev = stdio_get_by_name("vidconsole");
  579. #endif
  580. if (dev && (dev->flags & flags))
  581. return dev;
  582. return NULL;
  583. }
  584. int console_assign(int file, const char *devname)
  585. {
  586. int flag;
  587. struct stdio_dev *dev;
  588. /* Check for valid file */
  589. switch (file) {
  590. case stdin:
  591. flag = DEV_FLAGS_INPUT;
  592. break;
  593. case stdout:
  594. case stderr:
  595. flag = DEV_FLAGS_OUTPUT;
  596. break;
  597. default:
  598. return -1;
  599. }
  600. /* Check for valid device name */
  601. dev = search_device(flag, devname);
  602. if (dev)
  603. return console_setfile(file, dev);
  604. return -1;
  605. }
  606. static void console_update_silent(void)
  607. {
  608. #ifdef CONFIG_SILENT_CONSOLE
  609. if (env_get("silent") != NULL)
  610. gd->flags |= GD_FLG_SILENT;
  611. else
  612. gd->flags &= ~GD_FLG_SILENT;
  613. #endif
  614. }
  615. int console_announce_r(void)
  616. {
  617. #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
  618. char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
  619. display_options_get_banner(false, buf, sizeof(buf));
  620. console_puts_noserial(stdout, buf);
  621. #endif
  622. return 0;
  623. }
  624. /* Called before relocation - use serial functions */
  625. int console_init_f(void)
  626. {
  627. gd->have_console = 1;
  628. console_update_silent();
  629. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
  630. return 0;
  631. }
  632. void stdio_print_current_devices(void)
  633. {
  634. /* Print information */
  635. puts("In: ");
  636. if (stdio_devices[stdin] == NULL) {
  637. puts("No input devices available!\n");
  638. } else {
  639. printf ("%s\n", stdio_devices[stdin]->name);
  640. }
  641. puts("Out: ");
  642. if (stdio_devices[stdout] == NULL) {
  643. puts("No output devices available!\n");
  644. } else {
  645. printf ("%s\n", stdio_devices[stdout]->name);
  646. }
  647. puts("Err: ");
  648. if (stdio_devices[stderr] == NULL) {
  649. puts("No error devices available!\n");
  650. } else {
  651. printf ("%s\n", stdio_devices[stderr]->name);
  652. }
  653. }
  654. #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
  655. /* Called after the relocation - use desired console functions */
  656. int console_init_r(void)
  657. {
  658. char *stdinname, *stdoutname, *stderrname;
  659. struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  660. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  661. int i;
  662. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  663. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  664. int iomux_err = 0;
  665. #endif
  666. /* set default handlers at first */
  667. gd->jt->getc = serial_getc;
  668. gd->jt->tstc = serial_tstc;
  669. gd->jt->putc = serial_putc;
  670. gd->jt->puts = serial_puts;
  671. gd->jt->printf = serial_printf;
  672. /* stdin stdout and stderr are in environment */
  673. /* scan for it */
  674. stdinname = env_get("stdin");
  675. stdoutname = env_get("stdout");
  676. stderrname = env_get("stderr");
  677. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  678. inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
  679. outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
  680. errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
  681. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  682. iomux_err = iomux_doenv(stdin, stdinname);
  683. iomux_err += iomux_doenv(stdout, stdoutname);
  684. iomux_err += iomux_doenv(stderr, stderrname);
  685. if (!iomux_err)
  686. /* Successful, so skip all the code below. */
  687. goto done;
  688. #endif
  689. }
  690. /* if the devices are overwritten or not found, use default device */
  691. if (inputdev == NULL) {
  692. inputdev = search_device(DEV_FLAGS_INPUT, "serial");
  693. }
  694. if (outputdev == NULL) {
  695. outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  696. }
  697. if (errdev == NULL) {
  698. errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  699. }
  700. /* Initializes output console first */
  701. if (outputdev != NULL) {
  702. /* need to set a console if not done above. */
  703. console_doenv(stdout, outputdev);
  704. }
  705. if (errdev != NULL) {
  706. /* need to set a console if not done above. */
  707. console_doenv(stderr, errdev);
  708. }
  709. if (inputdev != NULL) {
  710. /* need to set a console if not done above. */
  711. console_doenv(stdin, inputdev);
  712. }
  713. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  714. done:
  715. #endif
  716. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  717. stdio_print_current_devices();
  718. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  719. #ifdef CONFIG_VIDCONSOLE_AS_LCD
  720. if (strstr(stdoutname, "lcd"))
  721. printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
  722. #endif
  723. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  724. /* set the environment variables (will overwrite previous env settings) */
  725. for (i = 0; i < MAX_FILES; i++) {
  726. env_set(stdio_names[i], stdio_devices[i]->name);
  727. }
  728. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  729. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  730. #if 0
  731. /* If nothing usable installed, use only the initial console */
  732. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  733. return 0;
  734. #endif
  735. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
  736. return 0;
  737. }
  738. #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
  739. /* Called after the relocation - use desired console functions */
  740. int console_init_r(void)
  741. {
  742. struct stdio_dev *inputdev = NULL, *outputdev = NULL;
  743. int i;
  744. struct list_head *list = stdio_get_list();
  745. struct list_head *pos;
  746. struct stdio_dev *dev;
  747. console_update_silent();
  748. #ifdef CONFIG_SPLASH_SCREEN
  749. /*
  750. * suppress all output if splash screen is enabled and we have
  751. * a bmp to display. We redirect the output from frame buffer
  752. * console to serial console in this case or suppress it if
  753. * "silent" mode was requested.
  754. */
  755. if (env_get("splashimage") != NULL) {
  756. if (!(gd->flags & GD_FLG_SILENT))
  757. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  758. }
  759. #endif
  760. /* Scan devices looking for input and output devices */
  761. list_for_each(pos, list) {
  762. dev = list_entry(pos, struct stdio_dev, list);
  763. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  764. inputdev = dev;
  765. }
  766. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  767. outputdev = dev;
  768. }
  769. if(inputdev && outputdev)
  770. break;
  771. }
  772. /* Initializes output console first */
  773. if (outputdev != NULL) {
  774. console_setfile(stdout, outputdev);
  775. console_setfile(stderr, outputdev);
  776. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  777. console_devices[stdout][0] = outputdev;
  778. console_devices[stderr][0] = outputdev;
  779. #endif
  780. }
  781. /* Initializes input console */
  782. if (inputdev != NULL) {
  783. console_setfile(stdin, inputdev);
  784. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  785. console_devices[stdin][0] = inputdev;
  786. #endif
  787. }
  788. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  789. stdio_print_current_devices();
  790. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  791. /* Setting environment variables */
  792. for (i = 0; i < MAX_FILES; i++) {
  793. env_set(stdio_names[i], stdio_devices[i]->name);
  794. }
  795. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  796. #if 0
  797. /* If nothing usable installed, use only the initial console */
  798. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  799. return 0;
  800. #endif
  801. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
  802. return 0;
  803. }
  804. #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */