console.c 19 KB

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