console.c 21 KB

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