console.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. static inline void console_doenv(int file, struct stdio_dev *dev)
  218. {
  219. iomux_doenv(file, dev->name);
  220. }
  221. #else
  222. static inline int console_getc(int file)
  223. {
  224. return stdio_devices[file]->getc(stdio_devices[file]);
  225. }
  226. static inline int console_tstc(int file)
  227. {
  228. return stdio_devices[file]->tstc(stdio_devices[file]);
  229. }
  230. static inline void console_putc(int file, const char c)
  231. {
  232. stdio_devices[file]->putc(stdio_devices[file], c);
  233. }
  234. static inline void console_puts_noserial(int file, const char *s)
  235. {
  236. if (!console_dev_is_serial(stdio_devices[file]))
  237. stdio_devices[file]->puts(stdio_devices[file], s);
  238. }
  239. static inline void console_puts(int file, const char *s)
  240. {
  241. stdio_devices[file]->puts(stdio_devices[file], s);
  242. }
  243. static inline void console_doenv(int file, struct stdio_dev *dev)
  244. {
  245. console_setfile(file, dev);
  246. }
  247. #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
  248. /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
  249. int serial_printf(const char *fmt, ...)
  250. {
  251. va_list args;
  252. uint i;
  253. char printbuffer[CONFIG_SYS_PBSIZE];
  254. va_start(args, fmt);
  255. /* For this to work, printbuffer must be larger than
  256. * anything we ever want to print.
  257. */
  258. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  259. va_end(args);
  260. serial_puts(printbuffer);
  261. return i;
  262. }
  263. int fgetc(int file)
  264. {
  265. if (file < MAX_FILES) {
  266. /*
  267. * Effectively poll for input wherever it may be available.
  268. */
  269. for (;;) {
  270. WATCHDOG_RESET();
  271. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  272. /*
  273. * Upper layer may have already called tstc() so
  274. * check for that first.
  275. */
  276. if (tstcdev != NULL)
  277. return console_getc(file);
  278. console_tstc(file);
  279. #else
  280. if (console_tstc(file))
  281. return console_getc(file);
  282. #endif
  283. #ifdef CONFIG_WATCHDOG
  284. /*
  285. * If the watchdog must be rate-limited then it should
  286. * already be handled in board-specific code.
  287. */
  288. udelay(1);
  289. #endif
  290. }
  291. }
  292. return -1;
  293. }
  294. int ftstc(int file)
  295. {
  296. if (file < MAX_FILES)
  297. return console_tstc(file);
  298. return -1;
  299. }
  300. void fputc(int file, const char c)
  301. {
  302. if (file < MAX_FILES)
  303. console_putc(file, c);
  304. }
  305. void fputs(int file, const char *s)
  306. {
  307. if (file < MAX_FILES)
  308. console_puts(file, s);
  309. }
  310. int fprintf(int file, const char *fmt, ...)
  311. {
  312. va_list args;
  313. uint i;
  314. char printbuffer[CONFIG_SYS_PBSIZE];
  315. va_start(args, fmt);
  316. /* For this to work, printbuffer must be larger than
  317. * anything we ever want to print.
  318. */
  319. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  320. va_end(args);
  321. /* Send to desired file */
  322. fputs(file, printbuffer);
  323. return i;
  324. }
  325. /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
  326. int getc(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. #ifdef CONFIG_CONSOLE_RECORD
  335. if (gd->console_in.start) {
  336. int ch;
  337. ch = membuff_getbyte(&gd->console_in);
  338. if (ch != -1)
  339. return 1;
  340. }
  341. #endif
  342. if (gd->flags & GD_FLG_DEVINIT) {
  343. /* Get from the standard input */
  344. return fgetc(stdin);
  345. }
  346. /* Send directly to the handler */
  347. return serial_getc();
  348. }
  349. int tstc(void)
  350. {
  351. #ifdef CONFIG_DISABLE_CONSOLE
  352. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  353. return 0;
  354. #endif
  355. if (!gd->have_console)
  356. return 0;
  357. #ifdef CONFIG_CONSOLE_RECORD
  358. if (gd->console_in.start) {
  359. if (membuff_peekbyte(&gd->console_in) != -1)
  360. return 1;
  361. }
  362. #endif
  363. if (gd->flags & GD_FLG_DEVINIT) {
  364. /* Test the standard input */
  365. return ftstc(stdin);
  366. }
  367. /* Send directly to the handler */
  368. return serial_tstc();
  369. }
  370. #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
  371. #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
  372. #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
  373. #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
  374. static void pre_console_putc(const char c)
  375. {
  376. char *buffer;
  377. buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
  378. buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
  379. unmap_sysmem(buffer);
  380. }
  381. static void pre_console_puts(const char *s)
  382. {
  383. while (*s)
  384. pre_console_putc(*s++);
  385. }
  386. static void print_pre_console_buffer(int flushpoint)
  387. {
  388. unsigned long in = 0, out = 0;
  389. char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
  390. char *buf_in;
  391. #ifdef CONFIG_SILENT_CONSOLE
  392. if (gd->flags & GD_FLG_SILENT)
  393. return;
  394. #endif
  395. buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
  396. if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
  397. in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
  398. while (in < gd->precon_buf_idx)
  399. buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
  400. unmap_sysmem(buf_in);
  401. buf_out[out] = 0;
  402. switch (flushpoint) {
  403. case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
  404. puts(buf_out);
  405. break;
  406. case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
  407. console_puts_noserial(stdout, buf_out);
  408. break;
  409. }
  410. }
  411. #else
  412. static inline void pre_console_putc(const char c) {}
  413. static inline void pre_console_puts(const char *s) {}
  414. static inline void print_pre_console_buffer(int flushpoint) {}
  415. #endif
  416. void putc(const char c)
  417. {
  418. #ifdef CONFIG_SANDBOX
  419. /* sandbox can send characters to stdout before it has a console */
  420. if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  421. os_putc(c);
  422. return;
  423. }
  424. #endif
  425. #ifdef CONFIG_DEBUG_UART
  426. /* if we don't have a console yet, use the debug UART */
  427. if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  428. printch(c);
  429. return;
  430. }
  431. #endif
  432. if (!gd)
  433. return;
  434. #ifdef CONFIG_CONSOLE_RECORD
  435. if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
  436. membuff_putbyte(&gd->console_out, c);
  437. #endif
  438. #ifdef CONFIG_SILENT_CONSOLE
  439. if (gd->flags & GD_FLG_SILENT) {
  440. if (!(gd->flags & GD_FLG_DEVINIT))
  441. pre_console_putc(c);
  442. return;
  443. }
  444. #endif
  445. #ifdef CONFIG_DISABLE_CONSOLE
  446. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  447. return;
  448. #endif
  449. if (!gd->have_console)
  450. return pre_console_putc(c);
  451. if (gd->flags & GD_FLG_DEVINIT) {
  452. /* Send to the standard output */
  453. fputc(stdout, c);
  454. } else {
  455. /* Send directly to the handler */
  456. pre_console_putc(c);
  457. serial_putc(c);
  458. }
  459. }
  460. void puts(const char *s)
  461. {
  462. #ifdef CONFIG_SANDBOX
  463. /* sandbox can send characters to stdout before it has a console */
  464. if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  465. os_puts(s);
  466. return;
  467. }
  468. #endif
  469. #ifdef CONFIG_DEBUG_UART
  470. if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  471. while (*s) {
  472. int ch = *s++;
  473. printch(ch);
  474. }
  475. return;
  476. }
  477. #endif
  478. if (!gd)
  479. return;
  480. #ifdef CONFIG_CONSOLE_RECORD
  481. if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
  482. membuff_put(&gd->console_out, s, strlen(s));
  483. #endif
  484. #ifdef CONFIG_SILENT_CONSOLE
  485. if (gd->flags & GD_FLG_SILENT) {
  486. if (!(gd->flags & GD_FLG_DEVINIT))
  487. pre_console_puts(s);
  488. return;
  489. }
  490. #endif
  491. #ifdef CONFIG_DISABLE_CONSOLE
  492. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  493. return;
  494. #endif
  495. if (!gd->have_console)
  496. return pre_console_puts(s);
  497. if (gd->flags & GD_FLG_DEVINIT) {
  498. /* Send to the standard output */
  499. fputs(stdout, s);
  500. } else {
  501. /* Send directly to the handler */
  502. pre_console_puts(s);
  503. serial_puts(s);
  504. }
  505. }
  506. #ifdef CONFIG_CONSOLE_RECORD
  507. int console_record_init(void)
  508. {
  509. int ret;
  510. ret = membuff_new(&gd->console_out, CONFIG_CONSOLE_RECORD_OUT_SIZE);
  511. if (ret)
  512. return ret;
  513. ret = membuff_new(&gd->console_in, CONFIG_CONSOLE_RECORD_IN_SIZE);
  514. return ret;
  515. }
  516. void console_record_reset(void)
  517. {
  518. membuff_purge(&gd->console_out);
  519. membuff_purge(&gd->console_in);
  520. }
  521. void console_record_reset_enable(void)
  522. {
  523. console_record_reset();
  524. gd->flags |= GD_FLG_RECORD;
  525. }
  526. #endif
  527. /* test if ctrl-c was pressed */
  528. static int ctrlc_disabled = 0; /* see disable_ctrl() */
  529. static int ctrlc_was_pressed = 0;
  530. int ctrlc(void)
  531. {
  532. if (!ctrlc_disabled && gd->have_console) {
  533. if (tstc()) {
  534. switch (getc()) {
  535. case 0x03: /* ^C - Control C */
  536. ctrlc_was_pressed = 1;
  537. return 1;
  538. default:
  539. break;
  540. }
  541. }
  542. }
  543. return 0;
  544. }
  545. /* Reads user's confirmation.
  546. Returns 1 if user's input is "y", "Y", "yes" or "YES"
  547. */
  548. int confirm_yesno(void)
  549. {
  550. int i;
  551. char str_input[5];
  552. /* Flush input */
  553. while (tstc())
  554. getc();
  555. i = 0;
  556. while (i < sizeof(str_input)) {
  557. str_input[i] = getc();
  558. putc(str_input[i]);
  559. if (str_input[i] == '\r')
  560. break;
  561. i++;
  562. }
  563. putc('\n');
  564. if (strncmp(str_input, "y\r", 2) == 0 ||
  565. strncmp(str_input, "Y\r", 2) == 0 ||
  566. strncmp(str_input, "yes\r", 4) == 0 ||
  567. strncmp(str_input, "YES\r", 4) == 0)
  568. return 1;
  569. return 0;
  570. }
  571. /* pass 1 to disable ctrlc() checking, 0 to enable.
  572. * returns previous state
  573. */
  574. int disable_ctrlc(int disable)
  575. {
  576. int prev = ctrlc_disabled; /* save previous state */
  577. ctrlc_disabled = disable;
  578. return prev;
  579. }
  580. int had_ctrlc (void)
  581. {
  582. return ctrlc_was_pressed;
  583. }
  584. void clear_ctrlc(void)
  585. {
  586. ctrlc_was_pressed = 0;
  587. }
  588. /** U-Boot INIT FUNCTIONS *************************************************/
  589. struct stdio_dev *search_device(int flags, const char *name)
  590. {
  591. struct stdio_dev *dev;
  592. dev = stdio_get_by_name(name);
  593. #ifdef CONFIG_VIDCONSOLE_AS_LCD
  594. if (!dev && !strcmp(name, "lcd"))
  595. dev = stdio_get_by_name("vidconsole");
  596. #endif
  597. if (dev && (dev->flags & flags))
  598. return dev;
  599. return NULL;
  600. }
  601. int console_assign(int file, const char *devname)
  602. {
  603. int flag;
  604. struct stdio_dev *dev;
  605. /* Check for valid file */
  606. switch (file) {
  607. case stdin:
  608. flag = DEV_FLAGS_INPUT;
  609. break;
  610. case stdout:
  611. case stderr:
  612. flag = DEV_FLAGS_OUTPUT;
  613. break;
  614. default:
  615. return -1;
  616. }
  617. /* Check for valid device name */
  618. dev = search_device(flag, devname);
  619. if (dev)
  620. return console_setfile(file, dev);
  621. return -1;
  622. }
  623. /* return true if the 'silent' flag is removed */
  624. static bool console_update_silent(void)
  625. {
  626. #ifdef CONFIG_SILENT_CONSOLE
  627. if (env_get("silent")) {
  628. gd->flags |= GD_FLG_SILENT;
  629. } else {
  630. unsigned long flags = gd->flags;
  631. gd->flags &= ~GD_FLG_SILENT;
  632. return !!(flags & GD_FLG_SILENT);
  633. }
  634. #endif
  635. return false;
  636. }
  637. int console_announce_r(void)
  638. {
  639. #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
  640. char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
  641. display_options_get_banner(false, buf, sizeof(buf));
  642. console_puts_noserial(stdout, buf);
  643. #endif
  644. return 0;
  645. }
  646. /* Called before relocation - use serial functions */
  647. int console_init_f(void)
  648. {
  649. gd->have_console = 1;
  650. console_update_silent();
  651. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
  652. return 0;
  653. }
  654. void stdio_print_current_devices(void)
  655. {
  656. /* Print information */
  657. puts("In: ");
  658. if (stdio_devices[stdin] == NULL) {
  659. puts("No input devices available!\n");
  660. } else {
  661. printf ("%s\n", stdio_devices[stdin]->name);
  662. }
  663. puts("Out: ");
  664. if (stdio_devices[stdout] == NULL) {
  665. puts("No output devices available!\n");
  666. } else {
  667. printf ("%s\n", stdio_devices[stdout]->name);
  668. }
  669. puts("Err: ");
  670. if (stdio_devices[stderr] == NULL) {
  671. puts("No error devices available!\n");
  672. } else {
  673. printf ("%s\n", stdio_devices[stderr]->name);
  674. }
  675. }
  676. #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
  677. /* Called after the relocation - use desired console functions */
  678. int console_init_r(void)
  679. {
  680. char *stdinname, *stdoutname, *stderrname;
  681. struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  682. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  683. int i;
  684. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  685. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  686. int iomux_err = 0;
  687. #endif
  688. int flushpoint;
  689. /* update silent for env loaded from flash (initr_env) */
  690. if (console_update_silent())
  691. flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
  692. else
  693. flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
  694. /* set default handlers at first */
  695. gd->jt->getc = serial_getc;
  696. gd->jt->tstc = serial_tstc;
  697. gd->jt->putc = serial_putc;
  698. gd->jt->puts = serial_puts;
  699. gd->jt->printf = serial_printf;
  700. /* stdin stdout and stderr are in environment */
  701. /* scan for it */
  702. stdinname = env_get("stdin");
  703. stdoutname = env_get("stdout");
  704. stderrname = env_get("stderr");
  705. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  706. inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
  707. outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
  708. errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
  709. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  710. iomux_err = iomux_doenv(stdin, stdinname);
  711. iomux_err += iomux_doenv(stdout, stdoutname);
  712. iomux_err += iomux_doenv(stderr, stderrname);
  713. if (!iomux_err)
  714. /* Successful, so skip all the code below. */
  715. goto done;
  716. #endif
  717. }
  718. /* if the devices are overwritten or not found, use default device */
  719. if (inputdev == NULL) {
  720. inputdev = search_device(DEV_FLAGS_INPUT, "serial");
  721. }
  722. if (outputdev == NULL) {
  723. outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  724. }
  725. if (errdev == NULL) {
  726. errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  727. }
  728. /* Initializes output console first */
  729. if (outputdev != NULL) {
  730. /* need to set a console if not done above. */
  731. console_doenv(stdout, outputdev);
  732. }
  733. if (errdev != NULL) {
  734. /* need to set a console if not done above. */
  735. console_doenv(stderr, errdev);
  736. }
  737. if (inputdev != NULL) {
  738. /* need to set a console if not done above. */
  739. console_doenv(stdin, inputdev);
  740. }
  741. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  742. done:
  743. #endif
  744. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  745. stdio_print_current_devices();
  746. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  747. #ifdef CONFIG_VIDCONSOLE_AS_LCD
  748. if (strstr(stdoutname, "lcd"))
  749. printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
  750. #endif
  751. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  752. /* set the environment variables (will overwrite previous env settings) */
  753. for (i = 0; i < MAX_FILES; i++) {
  754. env_set(stdio_names[i], stdio_devices[i]->name);
  755. }
  756. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  757. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  758. #if 0
  759. /* If nothing usable installed, use only the initial console */
  760. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  761. return 0;
  762. #endif
  763. print_pre_console_buffer(flushpoint);
  764. return 0;
  765. }
  766. #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
  767. /* Called after the relocation - use desired console functions */
  768. int console_init_r(void)
  769. {
  770. struct stdio_dev *inputdev = NULL, *outputdev = NULL;
  771. int i;
  772. struct list_head *list = stdio_get_list();
  773. struct list_head *pos;
  774. struct stdio_dev *dev;
  775. int flushpoint;
  776. /* update silent for env loaded from flash (initr_env) */
  777. if (console_update_silent())
  778. flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
  779. else
  780. flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
  781. #ifdef CONFIG_SPLASH_SCREEN
  782. /*
  783. * suppress all output if splash screen is enabled and we have
  784. * a bmp to display. We redirect the output from frame buffer
  785. * console to serial console in this case or suppress it if
  786. * "silent" mode was requested.
  787. */
  788. if (env_get("splashimage") != NULL) {
  789. if (!(gd->flags & GD_FLG_SILENT))
  790. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  791. }
  792. #endif
  793. /* Scan devices looking for input and output devices */
  794. list_for_each(pos, list) {
  795. dev = list_entry(pos, struct stdio_dev, list);
  796. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  797. inputdev = dev;
  798. }
  799. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  800. outputdev = dev;
  801. }
  802. if(inputdev && outputdev)
  803. break;
  804. }
  805. /* Initializes output console first */
  806. if (outputdev != NULL) {
  807. console_setfile(stdout, outputdev);
  808. console_setfile(stderr, outputdev);
  809. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  810. console_devices[stdout][0] = outputdev;
  811. console_devices[stderr][0] = outputdev;
  812. #endif
  813. }
  814. /* Initializes input console */
  815. if (inputdev != NULL) {
  816. console_setfile(stdin, inputdev);
  817. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  818. console_devices[stdin][0] = inputdev;
  819. #endif
  820. }
  821. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  822. stdio_print_current_devices();
  823. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  824. /* Setting environment variables */
  825. for (i = 0; i < MAX_FILES; i++) {
  826. env_set(stdio_names[i], stdio_devices[i]->name);
  827. }
  828. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  829. #if 0
  830. /* If nothing usable installed, use only the initial console */
  831. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  832. return 0;
  833. #endif
  834. print_pre_console_buffer(flushpoint);
  835. return 0;
  836. }
  837. #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */