console.c 24 KB

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