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 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
93 * if overwrite_console returns 1, the stdin, stderr and stdout
94 * are switched to the serial port, else the settings in the
95 * environment are used
97 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
98 extern int overwrite_console(void);
99 #define OVERWRITE_CONSOLE overwrite_console()
101 #define OVERWRITE_CONSOLE 0
102 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
104 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
106 static int console_setfile(int file, struct stdio_dev * dev)
117 /* Start new device */
119 error = dev->start(dev);
120 /* If it's not started dont use it */
125 /* Assign the new device (leaving the existing one started) */
126 stdio_devices[file] = dev;
129 * Update monitor functions
130 * (to use the console stuff by other applications)
134 gd->jt->getc = getchar;
140 gd->jt->printf = printf;
145 default: /* Invalid file ID */
152 * console_dev_is_serial() - Check if a stdio device is a serial device
154 * @sdev: Device to check
155 * @return true if this device is in the serial uclass (or for pre-driver-model,
156 * whether it is called "serial".
158 static bool console_dev_is_serial(struct stdio_dev *sdev)
162 if (IS_ENABLED(CONFIG_DM_SERIAL) && (sdev->flags & DEV_FLAGS_DM)) {
163 struct udevice *dev = sdev->priv;
165 is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL;
167 is_serial = !strcmp(sdev->name, "serial");
173 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
174 /** Console I/O multiplexing *******************************************/
176 static struct stdio_dev *tstcdev;
177 struct stdio_dev **console_devices[MAX_FILES];
178 int cd_count[MAX_FILES];
180 static void __maybe_unused console_devices_set(int file, struct stdio_dev *dev)
182 console_devices[file][0] = dev;
186 * This depends on tstc() always being called before getchar().
187 * This is guaranteed to be true because this routine is called
188 * only from fgetc() which assures it.
189 * No attempt is made to demultiplex multiple input sources.
191 static int console_getc(int file)
195 /* This is never called with testcdev == NULL */
196 ret = tstcdev->getc(tstcdev);
201 static int console_tstc(int file)
204 struct stdio_dev *dev;
207 prev = disable_ctrlc(1);
208 for (i = 0; i < cd_count[file]; i++) {
209 dev = console_devices[file][i];
210 if (dev->tstc != NULL) {
211 ret = dev->tstc(dev);
224 static void console_putc(int file, const char c)
227 struct stdio_dev *dev;
229 for (i = 0; i < cd_count[file]; i++) {
230 dev = console_devices[file][i];
231 if (dev->putc != NULL)
237 * console_puts_select() - Output a string to all console devices
239 * @file: File number to output to (e,g, stdout, see stdio.h)
240 * @serial_only: true to output only to serial, false to output to everything
242 * @s: String to output
244 static void console_puts_select(int file, bool serial_only, const char *s)
247 struct stdio_dev *dev;
249 for (i = 0; i < cd_count[file]; i++) {
252 dev = console_devices[file][i];
253 is_serial = console_dev_is_serial(dev);
254 if (dev->puts && serial_only == is_serial)
259 void console_puts_select_stderr(bool serial_only, const char *s)
261 console_puts_select(stderr, serial_only, s);
264 static void console_puts(int file, const char *s)
267 struct stdio_dev *dev;
269 for (i = 0; i < cd_count[file]; i++) {
270 dev = console_devices[file][i];
271 if (dev->puts != NULL)
276 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
277 static inline void console_doenv(int file, struct stdio_dev *dev)
279 iomux_doenv(file, dev->name);
284 static void __maybe_unused console_devices_set(int file, struct stdio_dev *dev)
288 static inline int console_getc(int file)
290 return stdio_devices[file]->getc(stdio_devices[file]);
293 static inline int console_tstc(int file)
295 return stdio_devices[file]->tstc(stdio_devices[file]);
298 static inline void console_putc(int file, const char c)
300 stdio_devices[file]->putc(stdio_devices[file], c);
303 void console_puts_select(int file, bool serial_only, const char *s)
305 if (serial_only == console_dev_is_serial(stdio_devices[file]))
306 stdio_devices[file]->puts(stdio_devices[file], s);
309 static inline void console_puts(int file, const char *s)
311 stdio_devices[file]->puts(stdio_devices[file], s);
314 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
315 static inline void console_doenv(int file, struct stdio_dev *dev)
317 console_setfile(file, dev);
320 #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
322 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
324 int serial_printf(const char *fmt, ...)
328 char printbuffer[CONFIG_SYS_PBSIZE];
332 /* For this to work, printbuffer must be larger than
333 * anything we ever want to print.
335 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
338 serial_puts(printbuffer);
344 if (file < MAX_FILES) {
346 * Effectively poll for input wherever it may be available.
350 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
352 * Upper layer may have already called tstc() so
353 * check for that first.
356 return console_getc(file);
359 if (console_tstc(file))
360 return console_getc(file);
363 * If the watchdog must be rate-limited then it should
364 * already be handled in board-specific code.
366 if (IS_ENABLED(CONFIG_WATCHDOG))
376 if (file < MAX_FILES)
377 return console_tstc(file);
382 void fputc(int file, const char c)
384 if (file < MAX_FILES)
385 console_putc(file, c);
388 void fputs(int file, const char *s)
390 if (file < MAX_FILES)
391 console_puts(file, s);
394 int fprintf(int file, const char *fmt, ...)
398 char printbuffer[CONFIG_SYS_PBSIZE];
402 /* For this to work, printbuffer must be larger than
403 * anything we ever want to print.
405 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
408 /* Send to desired file */
409 fputs(file, printbuffer);
413 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
417 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
420 if (!gd->have_console)
423 #ifdef CONFIG_CONSOLE_RECORD
424 if (gd->console_in.start) {
427 ch = membuff_getbyte((struct membuff *)&gd->console_in);
432 if (gd->flags & GD_FLG_DEVINIT) {
433 /* Get from the standard input */
437 /* Send directly to the handler */
438 return serial_getc();
443 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
446 if (!gd->have_console)
448 #ifdef CONFIG_CONSOLE_RECORD
449 if (gd->console_in.start) {
450 if (membuff_peekbyte((struct membuff *)&gd->console_in) != -1)
454 if (gd->flags & GD_FLG_DEVINIT) {
455 /* Test the standard input */
459 /* Send directly to the handler */
460 return serial_tstc();
463 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
464 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
466 #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
467 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
469 static void pre_console_putc(const char c)
473 buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
475 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
477 unmap_sysmem(buffer);
480 static void pre_console_puts(const char *s)
483 pre_console_putc(*s++);
486 static void print_pre_console_buffer(int flushpoint)
488 unsigned long in = 0, out = 0;
489 char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
492 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT))
495 buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
496 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
497 in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
499 while (in < gd->precon_buf_idx)
500 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
501 unmap_sysmem(buf_in);
505 switch (flushpoint) {
506 case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
509 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
510 console_puts_select(stdout, false, buf_out);
515 static inline void pre_console_putc(const char c) {}
516 static inline void pre_console_puts(const char *s) {}
517 static inline void print_pre_console_buffer(int flushpoint) {}
520 void putc(const char c)
524 #ifdef CONFIG_CONSOLE_RECORD
525 if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
526 membuff_putbyte((struct membuff *)&gd->console_out, c);
528 /* sandbox can send characters to stdout before it has a console */
529 if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
534 /* if we don't have a console yet, use the debug UART */
535 if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
540 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
541 if (!(gd->flags & GD_FLG_DEVINIT))
546 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
549 if (!gd->have_console)
550 return pre_console_putc(c);
552 if (gd->flags & GD_FLG_DEVINIT) {
553 /* Send to the standard output */
556 /* Send directly to the handler */
562 void puts(const char *s)
566 #ifdef CONFIG_CONSOLE_RECORD
567 if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
568 membuff_put((struct membuff *)&gd->console_out, s, strlen(s));
570 /* sandbox can send characters to stdout before it has a console */
571 if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
576 if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
585 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
586 if (!(gd->flags & GD_FLG_DEVINIT))
591 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
594 if (!gd->have_console)
595 return pre_console_puts(s);
597 if (gd->flags & GD_FLG_DEVINIT) {
598 /* Send to the standard output */
601 /* Send directly to the handler */
607 #ifdef CONFIG_CONSOLE_RECORD
608 int console_record_init(void)
612 ret = membuff_new((struct membuff *)&gd->console_out,
613 CONFIG_CONSOLE_RECORD_OUT_SIZE);
616 ret = membuff_new((struct membuff *)&gd->console_in,
617 CONFIG_CONSOLE_RECORD_IN_SIZE);
622 void console_record_reset(void)
624 membuff_purge((struct membuff *)&gd->console_out);
625 membuff_purge((struct membuff *)&gd->console_in);
628 int console_record_reset_enable(void)
630 console_record_reset();
631 gd->flags |= GD_FLG_RECORD;
636 int console_record_readline(char *str, int maxlen)
638 return membuff_readline((struct membuff *)&gd->console_out, str,
642 int console_record_avail(void)
644 return membuff_avail((struct membuff *)&gd->console_out);
649 /* test if ctrl-c was pressed */
650 static int ctrlc_disabled = 0; /* see disable_ctrl() */
651 static int ctrlc_was_pressed = 0;
654 if (!ctrlc_disabled && gd->have_console) {
657 case 0x03: /* ^C - Control C */
658 ctrlc_was_pressed = 1;
668 /* Reads user's confirmation.
669 Returns 1 if user's input is "y", "Y", "yes" or "YES"
671 int confirm_yesno(void)
680 while (i < sizeof(str_input)) {
681 str_input[i] = getchar();
683 if (str_input[i] == '\r')
688 if (strncmp(str_input, "y\r", 2) == 0 ||
689 strncmp(str_input, "Y\r", 2) == 0 ||
690 strncmp(str_input, "yes\r", 4) == 0 ||
691 strncmp(str_input, "YES\r", 4) == 0)
695 /* pass 1 to disable ctrlc() checking, 0 to enable.
696 * returns previous state
698 int disable_ctrlc(int disable)
700 int prev = ctrlc_disabled; /* save previous state */
702 ctrlc_disabled = disable;
708 return ctrlc_was_pressed;
711 void clear_ctrlc(void)
713 ctrlc_was_pressed = 0;
716 /** U-Boot INIT FUNCTIONS *************************************************/
718 struct stdio_dev *search_device(int flags, const char *name)
720 struct stdio_dev *dev;
722 dev = stdio_get_by_name(name);
723 #ifdef CONFIG_VIDCONSOLE_AS_LCD
724 if (!dev && !strcmp(name, CONFIG_VIDCONSOLE_AS_NAME))
725 dev = stdio_get_by_name("vidconsole");
728 if (dev && (dev->flags & flags))
734 int console_assign(int file, const char *devname)
737 struct stdio_dev *dev;
739 /* Check for valid file */
742 flag = DEV_FLAGS_INPUT;
746 flag = DEV_FLAGS_OUTPUT;
752 /* Check for valid device name */
754 dev = search_device(flag, devname);
757 return console_setfile(file, dev);
762 /* return true if the 'silent' flag is removed */
763 static bool console_update_silent(void)
765 unsigned long flags = gd->flags;
767 if (!IS_ENABLED(CONFIG_SILENT_CONSOLE))
770 if (env_get("silent")) {
771 gd->flags |= GD_FLG_SILENT;
775 gd->flags &= ~GD_FLG_SILENT;
777 return !!(flags & GD_FLG_SILENT);
780 int console_announce_r(void)
782 #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
783 char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
785 display_options_get_banner(false, buf, sizeof(buf));
787 console_puts_select(stdout, false, buf);
793 /* Called before relocation - use serial functions */
794 int console_init_f(void)
796 gd->have_console = 1;
798 console_update_silent();
800 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
805 void stdio_print_current_devices(void)
807 /* Print information */
809 if (stdio_devices[stdin] == NULL) {
810 puts("No input devices available!\n");
812 printf ("%s\n", stdio_devices[stdin]->name);
816 if (stdio_devices[stdout] == NULL) {
817 puts("No output devices available!\n");
819 printf ("%s\n", stdio_devices[stdout]->name);
823 if (stdio_devices[stderr] == NULL) {
824 puts("No error devices available!\n");
826 printf ("%s\n", stdio_devices[stderr]->name);
830 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
831 /* Called after the relocation - use desired console functions */
832 int console_init_r(void)
834 char *stdinname, *stdoutname, *stderrname;
835 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
840 /* update silent for env loaded from flash (initr_env) */
841 if (console_update_silent())
842 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
844 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
846 /* set default handlers at first */
847 gd->jt->getc = serial_getc;
848 gd->jt->tstc = serial_tstc;
849 gd->jt->putc = serial_putc;
850 gd->jt->puts = serial_puts;
851 gd->jt->printf = serial_printf;
853 /* stdin stdout and stderr are in environment */
855 stdinname = env_get("stdin");
856 stdoutname = env_get("stdout");
857 stderrname = env_get("stderr");
859 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
860 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
861 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
862 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
863 if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
864 iomux_err = iomux_doenv(stdin, stdinname);
865 iomux_err += iomux_doenv(stdout, stdoutname);
866 iomux_err += iomux_doenv(stderr, stderrname);
868 /* Successful, so skip all the code below. */
872 /* if the devices are overwritten or not found, use default device */
873 if (inputdev == NULL) {
874 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
876 if (outputdev == NULL) {
877 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
879 if (errdev == NULL) {
880 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
882 /* Initializes output console first */
883 if (outputdev != NULL) {
884 /* need to set a console if not done above. */
885 console_doenv(stdout, outputdev);
887 if (errdev != NULL) {
888 /* need to set a console if not done above. */
889 console_doenv(stderr, errdev);
891 if (inputdev != NULL) {
892 /* need to set a console if not done above. */
893 console_doenv(stdin, inputdev);
898 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
899 stdio_print_current_devices();
901 #ifdef CONFIG_VIDCONSOLE_AS_LCD
902 if (strstr(stdoutname, CONFIG_VIDCONSOLE_AS_NAME))
903 printf("Warning: Please change '%s' to 'vidconsole' in stdout/stderr environment vars\n",
904 CONFIG_VIDCONSOLE_AS_NAME);
907 if (IS_ENABLED(CONFIG_SYS_CONSOLE_ENV_OVERWRITE)) {
908 /* set the environment variables (will overwrite previous env settings) */
909 for (i = 0; i < MAX_FILES; i++)
910 env_set(stdio_names[i], stdio_devices[i]->name);
913 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
916 /* If nothing usable installed, use only the initial console */
917 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
920 print_pre_console_buffer(flushpoint);
924 #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
926 /* Called after the relocation - use desired console functions */
927 int console_init_r(void)
929 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
931 struct list_head *list = stdio_get_list();
932 struct list_head *pos;
933 struct stdio_dev *dev;
936 /* update silent for env loaded from flash (initr_env) */
937 if (console_update_silent())
938 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
940 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
943 * suppress all output if splash screen is enabled and we have
944 * a bmp to display. We redirect the output from frame buffer
945 * console to serial console in this case or suppress it if
946 * "silent" mode was requested.
948 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) && env_get("splashimage")) {
949 if (!(gd->flags & GD_FLG_SILENT))
950 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
953 /* Scan devices looking for input and output devices */
954 list_for_each(pos, list) {
955 dev = list_entry(pos, struct stdio_dev, list);
957 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
960 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
963 if(inputdev && outputdev)
967 /* Initializes output console first */
968 if (outputdev != NULL) {
969 console_setfile(stdout, outputdev);
970 console_setfile(stderr, outputdev);
971 console_devices_set(stdout, outputdev);
972 console_devices_set(stderr, outputdev);
975 /* Initializes input console */
976 if (inputdev != NULL) {
977 console_setfile(stdin, inputdev);
978 console_devices_set(stdin, inputdev);
981 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
982 stdio_print_current_devices();
984 /* Setting environment variables */
985 for (i = 0; i < MAX_FILES; i++) {
986 env_set(stdio_names[i], stdio_devices[i]->name);
989 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
992 /* If nothing usable installed, use only the initial console */
993 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
996 print_pre_console_buffer(flushpoint);
1000 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */