console.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. int printf(const char *fmt, ...)
  477. {
  478. va_list args;
  479. uint i;
  480. char printbuffer[CONFIG_SYS_PBSIZE];
  481. va_start(args, fmt);
  482. /* For this to work, printbuffer must be larger than
  483. * anything we ever want to print.
  484. */
  485. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  486. va_end(args);
  487. /* Print the string */
  488. puts(printbuffer);
  489. return i;
  490. }
  491. int vprintf(const char *fmt, va_list args)
  492. {
  493. uint i;
  494. char printbuffer[CONFIG_SYS_PBSIZE];
  495. /* For this to work, printbuffer must be larger than
  496. * anything we ever want to print.
  497. */
  498. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  499. /* Print the string */
  500. puts(printbuffer);
  501. return i;
  502. }
  503. #ifdef CONFIG_CONSOLE_RECORD
  504. int console_record_init(void)
  505. {
  506. int ret;
  507. ret = membuff_new(&gd->console_out, CONFIG_CONSOLE_RECORD_OUT_SIZE);
  508. if (ret)
  509. return ret;
  510. ret = membuff_new(&gd->console_in, CONFIG_CONSOLE_RECORD_IN_SIZE);
  511. return ret;
  512. }
  513. void console_record_reset(void)
  514. {
  515. membuff_purge(&gd->console_out);
  516. membuff_purge(&gd->console_in);
  517. }
  518. void console_record_reset_enable(void)
  519. {
  520. console_record_reset();
  521. gd->flags |= GD_FLG_RECORD;
  522. }
  523. #endif
  524. /* test if ctrl-c was pressed */
  525. static int ctrlc_disabled = 0; /* see disable_ctrl() */
  526. static int ctrlc_was_pressed = 0;
  527. int ctrlc(void)
  528. {
  529. #ifndef CONFIG_SANDBOX
  530. if (!ctrlc_disabled && gd->have_console) {
  531. if (tstc()) {
  532. switch (getc()) {
  533. case 0x03: /* ^C - Control C */
  534. ctrlc_was_pressed = 1;
  535. return 1;
  536. default:
  537. break;
  538. }
  539. }
  540. }
  541. #endif
  542. return 0;
  543. }
  544. /* Reads user's confirmation.
  545. Returns 1 if user's input is "y", "Y", "yes" or "YES"
  546. */
  547. int confirm_yesno(void)
  548. {
  549. int i;
  550. char str_input[5];
  551. /* Flush input */
  552. while (tstc())
  553. getc();
  554. i = 0;
  555. while (i < sizeof(str_input)) {
  556. str_input[i] = getc();
  557. putc(str_input[i]);
  558. if (str_input[i] == '\r')
  559. break;
  560. i++;
  561. }
  562. putc('\n');
  563. if (strncmp(str_input, "y\r", 2) == 0 ||
  564. strncmp(str_input, "Y\r", 2) == 0 ||
  565. strncmp(str_input, "yes\r", 4) == 0 ||
  566. strncmp(str_input, "YES\r", 4) == 0)
  567. return 1;
  568. return 0;
  569. }
  570. /* pass 1 to disable ctrlc() checking, 0 to enable.
  571. * returns previous state
  572. */
  573. int disable_ctrlc(int disable)
  574. {
  575. int prev = ctrlc_disabled; /* save previous state */
  576. ctrlc_disabled = disable;
  577. return prev;
  578. }
  579. int had_ctrlc (void)
  580. {
  581. return ctrlc_was_pressed;
  582. }
  583. void clear_ctrlc(void)
  584. {
  585. ctrlc_was_pressed = 0;
  586. }
  587. #ifdef CONFIG_MODEM_SUPPORT_DEBUG
  588. char screen[1024];
  589. char *cursor = screen;
  590. int once = 0;
  591. inline void dbg(const char *fmt, ...)
  592. {
  593. va_list args;
  594. uint i;
  595. char printbuffer[CONFIG_SYS_PBSIZE];
  596. if (!once) {
  597. memset(screen, 0, sizeof(screen));
  598. once++;
  599. }
  600. va_start(args, fmt);
  601. /* For this to work, printbuffer must be larger than
  602. * anything we ever want to print.
  603. */
  604. i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  605. va_end(args);
  606. if ((screen + sizeof(screen) - 1 - cursor)
  607. < strlen(printbuffer) + 1) {
  608. memset(screen, 0, sizeof(screen));
  609. cursor = screen;
  610. }
  611. sprintf(cursor, printbuffer);
  612. cursor += strlen(printbuffer);
  613. }
  614. #else
  615. static inline void dbg(const char *fmt, ...)
  616. {
  617. }
  618. #endif
  619. /** U-Boot INIT FUNCTIONS *************************************************/
  620. struct stdio_dev *search_device(int flags, const char *name)
  621. {
  622. struct stdio_dev *dev;
  623. dev = stdio_get_by_name(name);
  624. if (dev && (dev->flags & flags))
  625. return dev;
  626. return NULL;
  627. }
  628. int console_assign(int file, const char *devname)
  629. {
  630. int flag;
  631. struct stdio_dev *dev;
  632. /* Check for valid file */
  633. switch (file) {
  634. case stdin:
  635. flag = DEV_FLAGS_INPUT;
  636. break;
  637. case stdout:
  638. case stderr:
  639. flag = DEV_FLAGS_OUTPUT;
  640. break;
  641. default:
  642. return -1;
  643. }
  644. /* Check for valid device name */
  645. dev = search_device(flag, devname);
  646. if (dev)
  647. return console_setfile(file, dev);
  648. return -1;
  649. }
  650. /* Called before relocation - use serial functions */
  651. int console_init_f(void)
  652. {
  653. gd->have_console = 1;
  654. #ifdef CONFIG_SILENT_CONSOLE
  655. if (getenv("silent") != NULL)
  656. gd->flags |= GD_FLG_SILENT;
  657. #endif
  658. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
  659. return 0;
  660. }
  661. void stdio_print_current_devices(void)
  662. {
  663. /* Print information */
  664. puts("In: ");
  665. if (stdio_devices[stdin] == NULL) {
  666. puts("No input devices available!\n");
  667. } else {
  668. printf ("%s\n", stdio_devices[stdin]->name);
  669. }
  670. puts("Out: ");
  671. if (stdio_devices[stdout] == NULL) {
  672. puts("No output devices available!\n");
  673. } else {
  674. printf ("%s\n", stdio_devices[stdout]->name);
  675. }
  676. puts("Err: ");
  677. if (stdio_devices[stderr] == NULL) {
  678. puts("No error devices available!\n");
  679. } else {
  680. printf ("%s\n", stdio_devices[stderr]->name);
  681. }
  682. }
  683. #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
  684. /* Called after the relocation - use desired console functions */
  685. int console_init_r(void)
  686. {
  687. char *stdinname, *stdoutname, *stderrname;
  688. struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  689. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  690. int i;
  691. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  692. #ifdef CONFIG_CONSOLE_MUX
  693. int iomux_err = 0;
  694. #endif
  695. /* set default handlers at first */
  696. gd->jt->getc = serial_getc;
  697. gd->jt->tstc = serial_tstc;
  698. gd->jt->putc = serial_putc;
  699. gd->jt->puts = serial_puts;
  700. gd->jt->printf = serial_printf;
  701. /* stdin stdout and stderr are in environment */
  702. /* scan for it */
  703. stdinname = getenv("stdin");
  704. stdoutname = getenv("stdout");
  705. stderrname = getenv("stderr");
  706. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  707. inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
  708. outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
  709. errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
  710. #ifdef CONFIG_CONSOLE_MUX
  711. iomux_err = iomux_doenv(stdin, stdinname);
  712. iomux_err += iomux_doenv(stdout, stdoutname);
  713. iomux_err += iomux_doenv(stderr, stderrname);
  714. if (!iomux_err)
  715. /* Successful, so skip all the code below. */
  716. goto done;
  717. #endif
  718. }
  719. /* if the devices are overwritten or not found, use default device */
  720. if (inputdev == NULL) {
  721. inputdev = search_device(DEV_FLAGS_INPUT, "serial");
  722. }
  723. if (outputdev == NULL) {
  724. outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  725. }
  726. if (errdev == NULL) {
  727. errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  728. }
  729. /* Initializes output console first */
  730. if (outputdev != NULL) {
  731. /* need to set a console if not done above. */
  732. console_doenv(stdout, outputdev);
  733. }
  734. if (errdev != NULL) {
  735. /* need to set a console if not done above. */
  736. console_doenv(stderr, errdev);
  737. }
  738. if (inputdev != NULL) {
  739. /* need to set a console if not done above. */
  740. console_doenv(stdin, inputdev);
  741. }
  742. #ifdef CONFIG_CONSOLE_MUX
  743. done:
  744. #endif
  745. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  746. stdio_print_current_devices();
  747. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  748. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  749. /* set the environment variables (will overwrite previous env settings) */
  750. for (i = 0; i < 3; i++) {
  751. setenv(stdio_names[i], stdio_devices[i]->name);
  752. }
  753. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  754. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  755. #if 0
  756. /* If nothing usable installed, use only the initial console */
  757. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  758. return 0;
  759. #endif
  760. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
  761. return 0;
  762. }
  763. #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
  764. /* Called after the relocation - use desired console functions */
  765. int console_init_r(void)
  766. {
  767. struct stdio_dev *inputdev = NULL, *outputdev = NULL;
  768. int i;
  769. struct list_head *list = stdio_get_list();
  770. struct list_head *pos;
  771. struct stdio_dev *dev;
  772. #ifdef CONFIG_SPLASH_SCREEN
  773. /*
  774. * suppress all output if splash screen is enabled and we have
  775. * a bmp to display. We redirect the output from frame buffer
  776. * console to serial console in this case or suppress it if
  777. * "silent" mode was requested.
  778. */
  779. if (getenv("splashimage") != NULL) {
  780. if (!(gd->flags & GD_FLG_SILENT))
  781. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  782. }
  783. #endif
  784. /* Scan devices looking for input and output devices */
  785. list_for_each(pos, list) {
  786. dev = list_entry(pos, struct stdio_dev, list);
  787. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  788. inputdev = dev;
  789. }
  790. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  791. outputdev = dev;
  792. }
  793. if(inputdev && outputdev)
  794. break;
  795. }
  796. /* Initializes output console first */
  797. if (outputdev != NULL) {
  798. console_setfile(stdout, outputdev);
  799. console_setfile(stderr, outputdev);
  800. #ifdef CONFIG_CONSOLE_MUX
  801. console_devices[stdout][0] = outputdev;
  802. console_devices[stderr][0] = outputdev;
  803. #endif
  804. }
  805. /* Initializes input console */
  806. if (inputdev != NULL) {
  807. console_setfile(stdin, inputdev);
  808. #ifdef CONFIG_CONSOLE_MUX
  809. console_devices[stdin][0] = inputdev;
  810. #endif
  811. }
  812. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  813. stdio_print_current_devices();
  814. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  815. /* Setting environment variables */
  816. for (i = 0; i < 3; i++) {
  817. setenv(stdio_names[i], stdio_devices[i]->name);
  818. }
  819. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  820. #if 0
  821. /* If nothing usable installed, use only the initial console */
  822. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  823. return 0;
  824. #endif
  825. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
  826. return 0;
  827. }
  828. #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */