console.c 20 KB

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