1 // SPDX-License-Identifier: GPL-2.0+
4 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
9 #include <debug_uart.h>
18 #include <stdio_dev.h>
20 #include <env_internal.h>
22 #include <linux/delay.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 static int on_console(const char *name, const char *value, enum env_op op,
31 /* Check for console redirection */
32 if (strcmp(name, "stdin") == 0)
34 else if (strcmp(name, "stdout") == 0)
36 else if (strcmp(name, "stderr") == 0)
39 /* if not actually setting a console variable, we don't care */
40 if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
45 case env_op_overwrite:
47 if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
48 if (iomux_doenv(console, value))
51 /* Try assigning specified device */
52 if (console_assign(console, value) < 0)
59 if ((flags & H_FORCE) == 0)
60 printf("Can't delete \"%s\"\n", name);
67 U_BOOT_ENV_CALLBACK(console, on_console);
69 #ifdef CONFIG_SILENT_CONSOLE
70 static int on_silent(const char *name, const char *value, enum env_op op,
73 if (!CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_SET))
74 if (flags & H_INTERACTIVE)
77 if (!CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_RELOC))
78 if ((flags & H_INTERACTIVE) == 0)
82 gd->flags |= GD_FLG_SILENT;
84 gd->flags &= ~GD_FLG_SILENT;
88 U_BOOT_ENV_CALLBACK(silent, on_silent);
91 #ifdef CONFIG_CONSOLE_RECORD
92 /* helper function: access to gd->console_out and gd->console_in */
93 static void console_record_putc(const char c)
95 if (!(gd->flags & GD_FLG_RECORD))
97 if (gd->console_out.start)
98 membuff_putbyte((struct membuff *)&gd->console_out, c);
101 static void console_record_puts(const char *s)
103 if (!(gd->flags & GD_FLG_RECORD))
105 if (gd->console_out.start)
106 membuff_put((struct membuff *)&gd->console_out, s, strlen(s));
109 static int console_record_getc(void)
111 if (!(gd->flags & GD_FLG_RECORD))
113 if (!gd->console_in.start)
116 return membuff_getbyte((struct membuff *)&gd->console_in);
119 static int console_record_tstc(void)
121 if (!(gd->flags & GD_FLG_RECORD))
123 if (gd->console_in.start) {
124 if (membuff_peekbyte((struct membuff *)&gd->console_in) != -1)
130 static void console_record_putc(char c)
134 static void console_record_puts(const char *s)
138 static int console_record_getc(void)
143 static int console_record_tstc(void)
149 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
151 * if overwrite_console returns 1, the stdin, stderr and stdout
152 * are switched to the serial port, else the settings in the
153 * environment are used
155 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
156 extern int overwrite_console(void);
157 #define OVERWRITE_CONSOLE overwrite_console()
159 #define OVERWRITE_CONSOLE 0
160 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
162 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
164 static int console_setfile(int file, struct stdio_dev * dev)
175 /* Start new device */
177 error = dev->start(dev);
178 /* If it's not started dont use it */
183 /* Assign the new device (leaving the existing one started) */
184 stdio_devices[file] = dev;
187 * Update monitor functions
188 * (to use the console stuff by other applications)
192 gd->jt->getc = getchar;
198 gd->jt->printf = printf;
203 default: /* Invalid file ID */
210 * console_dev_is_serial() - Check if a stdio device is a serial device
212 * @sdev: Device to check
213 * @return true if this device is in the serial uclass (or for pre-driver-model,
214 * whether it is called "serial".
216 static bool console_dev_is_serial(struct stdio_dev *sdev)
220 if (IS_ENABLED(CONFIG_DM_SERIAL) && (sdev->flags & DEV_FLAGS_DM)) {
221 struct udevice *dev = sdev->priv;
223 is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL;
225 is_serial = !strcmp(sdev->name, "serial");
231 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
232 /** Console I/O multiplexing *******************************************/
234 static struct stdio_dev *tstcdev;
235 struct stdio_dev **console_devices[MAX_FILES];
236 int cd_count[MAX_FILES];
238 static void __maybe_unused console_devices_set(int file, struct stdio_dev *dev)
240 console_devices[file][0] = dev;
244 * This depends on tstc() always being called before getchar().
245 * This is guaranteed to be true because this routine is called
246 * only from fgetc() which assures it.
247 * No attempt is made to demultiplex multiple input sources.
249 static int console_getc(int file)
253 /* This is never called with testcdev == NULL */
254 ret = tstcdev->getc(tstcdev);
259 static int console_tstc(int file)
262 struct stdio_dev *dev;
265 prev = disable_ctrlc(1);
266 for (i = 0; i < cd_count[file]; i++) {
267 dev = console_devices[file][i];
268 if (dev->tstc != NULL) {
269 ret = dev->tstc(dev);
282 static void console_putc(int file, const char c)
285 struct stdio_dev *dev;
287 for (i = 0; i < cd_count[file]; i++) {
288 dev = console_devices[file][i];
289 if (dev->putc != NULL)
295 * console_puts_select() - Output a string to all console devices
297 * @file: File number to output to (e,g, stdout, see stdio.h)
298 * @serial_only: true to output only to serial, false to output to everything
300 * @s: String to output
302 static void console_puts_select(int file, bool serial_only, const char *s)
305 struct stdio_dev *dev;
307 for (i = 0; i < cd_count[file]; i++) {
310 dev = console_devices[file][i];
311 is_serial = console_dev_is_serial(dev);
312 if (dev->puts && serial_only == is_serial)
317 void console_puts_select_stderr(bool serial_only, const char *s)
319 console_puts_select(stderr, serial_only, s);
322 static void console_puts(int file, const char *s)
325 struct stdio_dev *dev;
327 for (i = 0; i < cd_count[file]; i++) {
328 dev = console_devices[file][i];
329 if (dev->puts != NULL)
334 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
335 static inline void console_doenv(int file, struct stdio_dev *dev)
337 iomux_doenv(file, dev->name);
342 static void __maybe_unused console_devices_set(int file, struct stdio_dev *dev)
346 static inline int console_getc(int file)
348 return stdio_devices[file]->getc(stdio_devices[file]);
351 static inline int console_tstc(int file)
353 return stdio_devices[file]->tstc(stdio_devices[file]);
356 static inline void console_putc(int file, const char c)
358 stdio_devices[file]->putc(stdio_devices[file], c);
361 void console_puts_select(int file, bool serial_only, const char *s)
363 if (serial_only == console_dev_is_serial(stdio_devices[file]))
364 stdio_devices[file]->puts(stdio_devices[file], s);
367 static inline void console_puts(int file, const char *s)
369 stdio_devices[file]->puts(stdio_devices[file], s);
372 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
373 static inline void console_doenv(int file, struct stdio_dev *dev)
375 console_setfile(file, dev);
378 #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
380 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
382 int serial_printf(const char *fmt, ...)
386 char printbuffer[CONFIG_SYS_PBSIZE];
390 /* For this to work, printbuffer must be larger than
391 * anything we ever want to print.
393 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
396 serial_puts(printbuffer);
402 if (file < MAX_FILES) {
404 * Effectively poll for input wherever it may be available.
408 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
410 * Upper layer may have already called tstc() so
411 * check for that first.
414 return console_getc(file);
417 if (console_tstc(file))
418 return console_getc(file);
421 * If the watchdog must be rate-limited then it should
422 * already be handled in board-specific code.
424 if (IS_ENABLED(CONFIG_WATCHDOG))
434 if (file < MAX_FILES)
435 return console_tstc(file);
440 void fputc(int file, const char c)
442 if (file < MAX_FILES)
443 console_putc(file, c);
446 void fputs(int file, const char *s)
448 if (file < MAX_FILES)
449 console_puts(file, s);
452 int fprintf(int file, const char *fmt, ...)
456 char printbuffer[CONFIG_SYS_PBSIZE];
460 /* For this to work, printbuffer must be larger than
461 * anything we ever want to print.
463 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
466 /* Send to desired file */
467 fputs(file, printbuffer);
471 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
477 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
480 if (!gd->have_console)
483 ch = console_record_getc();
487 if (gd->flags & GD_FLG_DEVINIT) {
488 /* Get from the standard input */
492 /* Send directly to the handler */
493 return serial_getc();
498 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
501 if (!gd->have_console)
504 if (console_record_tstc())
507 if (gd->flags & GD_FLG_DEVINIT) {
508 /* Test the standard input */
512 /* Send directly to the handler */
513 return serial_tstc();
516 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
517 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
519 #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
520 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
522 static void pre_console_putc(const char c)
526 buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
528 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
530 unmap_sysmem(buffer);
533 static void pre_console_puts(const char *s)
536 pre_console_putc(*s++);
539 static void print_pre_console_buffer(int flushpoint)
541 unsigned long in = 0, out = 0;
542 char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
545 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT))
548 buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
549 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
550 in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
552 while (in < gd->precon_buf_idx)
553 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
554 unmap_sysmem(buf_in);
558 switch (flushpoint) {
559 case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
562 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
563 console_puts_select(stdout, false, buf_out);
568 static inline void pre_console_putc(const char c) {}
569 static inline void pre_console_puts(const char *s) {}
570 static inline void print_pre_console_buffer(int flushpoint) {}
573 void putc(const char c)
578 console_record_putc(c);
580 /* sandbox can send characters to stdout before it has a console */
581 if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
586 /* if we don't have a console yet, use the debug UART */
587 if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
592 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
593 if (!(gd->flags & GD_FLG_DEVINIT))
598 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
601 if (!gd->have_console)
602 return pre_console_putc(c);
604 if (gd->flags & GD_FLG_DEVINIT) {
605 /* Send to the standard output */
608 /* Send directly to the handler */
614 void puts(const char *s)
619 console_record_puts(s);
621 /* sandbox can send characters to stdout before it has a console */
622 if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
627 if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
636 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
637 if (!(gd->flags & GD_FLG_DEVINIT))
642 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
645 if (!gd->have_console)
646 return pre_console_puts(s);
648 if (gd->flags & GD_FLG_DEVINIT) {
649 /* Send to the standard output */
652 /* Send directly to the handler */
658 #ifdef CONFIG_CONSOLE_RECORD
659 int console_record_init(void)
663 ret = membuff_new((struct membuff *)&gd->console_out,
664 CONFIG_CONSOLE_RECORD_OUT_SIZE);
667 ret = membuff_new((struct membuff *)&gd->console_in,
668 CONFIG_CONSOLE_RECORD_IN_SIZE);
673 void console_record_reset(void)
675 membuff_purge((struct membuff *)&gd->console_out);
676 membuff_purge((struct membuff *)&gd->console_in);
679 int console_record_reset_enable(void)
681 console_record_reset();
682 gd->flags |= GD_FLG_RECORD;
687 int console_record_readline(char *str, int maxlen)
689 return membuff_readline((struct membuff *)&gd->console_out, str,
693 int console_record_avail(void)
695 return membuff_avail((struct membuff *)&gd->console_out);
700 /* test if ctrl-c was pressed */
701 static int ctrlc_disabled = 0; /* see disable_ctrl() */
702 static int ctrlc_was_pressed = 0;
705 if (!ctrlc_disabled && gd->have_console) {
708 case 0x03: /* ^C - Control C */
709 ctrlc_was_pressed = 1;
719 /* Reads user's confirmation.
720 Returns 1 if user's input is "y", "Y", "yes" or "YES"
722 int confirm_yesno(void)
731 while (i < sizeof(str_input)) {
732 str_input[i] = getchar();
734 if (str_input[i] == '\r')
739 if (strncmp(str_input, "y\r", 2) == 0 ||
740 strncmp(str_input, "Y\r", 2) == 0 ||
741 strncmp(str_input, "yes\r", 4) == 0 ||
742 strncmp(str_input, "YES\r", 4) == 0)
746 /* pass 1 to disable ctrlc() checking, 0 to enable.
747 * returns previous state
749 int disable_ctrlc(int disable)
751 int prev = ctrlc_disabled; /* save previous state */
753 ctrlc_disabled = disable;
759 return ctrlc_was_pressed;
762 void clear_ctrlc(void)
764 ctrlc_was_pressed = 0;
767 /** U-Boot INIT FUNCTIONS *************************************************/
769 struct stdio_dev *search_device(int flags, const char *name)
771 struct stdio_dev *dev;
773 dev = stdio_get_by_name(name);
774 #ifdef CONFIG_VIDCONSOLE_AS_LCD
775 if (!dev && !strcmp(name, CONFIG_VIDCONSOLE_AS_NAME))
776 dev = stdio_get_by_name("vidconsole");
779 if (dev && (dev->flags & flags))
785 int console_assign(int file, const char *devname)
788 struct stdio_dev *dev;
790 /* Check for valid file */
793 flag = DEV_FLAGS_INPUT;
797 flag = DEV_FLAGS_OUTPUT;
803 /* Check for valid device name */
805 dev = search_device(flag, devname);
808 return console_setfile(file, dev);
813 /* return true if the 'silent' flag is removed */
814 static bool console_update_silent(void)
816 unsigned long flags = gd->flags;
818 if (!IS_ENABLED(CONFIG_SILENT_CONSOLE))
821 if (env_get("silent")) {
822 gd->flags |= GD_FLG_SILENT;
826 gd->flags &= ~GD_FLG_SILENT;
828 return !!(flags & GD_FLG_SILENT);
831 int console_announce_r(void)
833 #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
834 char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
836 display_options_get_banner(false, buf, sizeof(buf));
838 console_puts_select(stdout, false, buf);
844 /* Called before relocation - use serial functions */
845 int console_init_f(void)
847 gd->have_console = 1;
849 console_update_silent();
851 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
856 void stdio_print_current_devices(void)
858 /* Print information */
860 if (stdio_devices[stdin] == NULL) {
861 puts("No input devices available!\n");
863 printf ("%s\n", stdio_devices[stdin]->name);
867 if (stdio_devices[stdout] == NULL) {
868 puts("No output devices available!\n");
870 printf ("%s\n", stdio_devices[stdout]->name);
874 if (stdio_devices[stderr] == NULL) {
875 puts("No error devices available!\n");
877 printf ("%s\n", stdio_devices[stderr]->name);
881 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
882 /* Called after the relocation - use desired console functions */
883 int console_init_r(void)
885 char *stdinname, *stdoutname, *stderrname;
886 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
891 /* update silent for env loaded from flash (initr_env) */
892 if (console_update_silent())
893 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
895 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
897 /* set default handlers at first */
898 gd->jt->getc = serial_getc;
899 gd->jt->tstc = serial_tstc;
900 gd->jt->putc = serial_putc;
901 gd->jt->puts = serial_puts;
902 gd->jt->printf = serial_printf;
904 /* stdin stdout and stderr are in environment */
906 stdinname = env_get("stdin");
907 stdoutname = env_get("stdout");
908 stderrname = env_get("stderr");
910 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
911 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
912 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
913 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
914 if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
915 iomux_err = iomux_doenv(stdin, stdinname);
916 iomux_err += iomux_doenv(stdout, stdoutname);
917 iomux_err += iomux_doenv(stderr, stderrname);
919 /* Successful, so skip all the code below. */
923 /* if the devices are overwritten or not found, use default device */
924 if (inputdev == NULL) {
925 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
927 if (outputdev == NULL) {
928 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
930 if (errdev == NULL) {
931 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
933 /* Initializes output console first */
934 if (outputdev != NULL) {
935 /* need to set a console if not done above. */
936 console_doenv(stdout, outputdev);
938 if (errdev != NULL) {
939 /* need to set a console if not done above. */
940 console_doenv(stderr, errdev);
942 if (inputdev != NULL) {
943 /* need to set a console if not done above. */
944 console_doenv(stdin, inputdev);
949 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
950 stdio_print_current_devices();
952 #ifdef CONFIG_VIDCONSOLE_AS_LCD
953 if (strstr(stdoutname, CONFIG_VIDCONSOLE_AS_NAME))
954 printf("Warning: Please change '%s' to 'vidconsole' in stdout/stderr environment vars\n",
955 CONFIG_VIDCONSOLE_AS_NAME);
958 if (IS_ENABLED(CONFIG_SYS_CONSOLE_ENV_OVERWRITE)) {
959 /* set the environment variables (will overwrite previous env settings) */
960 for (i = 0; i < MAX_FILES; i++)
961 env_set(stdio_names[i], stdio_devices[i]->name);
964 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
967 /* If nothing usable installed, use only the initial console */
968 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
971 print_pre_console_buffer(flushpoint);
975 #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
977 /* Called after the relocation - use desired console functions */
978 int console_init_r(void)
980 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
982 struct list_head *list = stdio_get_list();
983 struct list_head *pos;
984 struct stdio_dev *dev;
987 /* update silent for env loaded from flash (initr_env) */
988 if (console_update_silent())
989 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
991 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
994 * suppress all output if splash screen is enabled and we have
995 * a bmp to display. We redirect the output from frame buffer
996 * console to serial console in this case or suppress it if
997 * "silent" mode was requested.
999 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) && env_get("splashimage")) {
1000 if (!(gd->flags & GD_FLG_SILENT))
1001 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
1004 /* Scan devices looking for input and output devices */
1005 list_for_each(pos, list) {
1006 dev = list_entry(pos, struct stdio_dev, list);
1008 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
1011 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
1014 if(inputdev && outputdev)
1018 /* Initializes output console first */
1019 if (outputdev != NULL) {
1020 console_setfile(stdout, outputdev);
1021 console_setfile(stderr, outputdev);
1022 console_devices_set(stdout, outputdev);
1023 console_devices_set(stderr, outputdev);
1026 /* Initializes input console */
1027 if (inputdev != NULL) {
1028 console_setfile(stdin, inputdev);
1029 console_devices_set(stdin, inputdev);
1032 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
1033 stdio_print_current_devices();
1035 /* Setting environment variables */
1036 for (i = 0; i < MAX_FILES; i++) {
1037 env_set(stdio_names[i], stdio_devices[i]->name);
1040 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
1043 /* If nothing usable installed, use only the initial console */
1044 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
1047 print_pre_console_buffer(flushpoint);
1051 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */