console.c 21 KB

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