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];
181 * This depends on tstc() always being called before getchar().
182 * This is guaranteed to be true because this routine is called
183 * only from fgetc() which assures it.
184 * No attempt is made to demultiplex multiple input sources.
186 static int console_getc(int file)
190 /* This is never called with testcdev == NULL */
191 ret = tstcdev->getc(tstcdev);
196 static int console_tstc(int file)
199 struct stdio_dev *dev;
202 prev = disable_ctrlc(1);
203 for (i = 0; i < cd_count[file]; i++) {
204 dev = console_devices[file][i];
205 if (dev->tstc != NULL) {
206 ret = dev->tstc(dev);
219 static void console_putc(int file, const char c)
222 struct stdio_dev *dev;
224 for (i = 0; i < cd_count[file]; i++) {
225 dev = console_devices[file][i];
226 if (dev->putc != NULL)
232 * console_puts_select() - Output a string to all console devices
234 * @file: File number to output to (e,g, stdout, see stdio.h)
235 * @serial_only: true to output only to serial, false to output to everything
237 * @s: String to output
239 static void console_puts_select(int file, bool serial_only, const char *s)
242 struct stdio_dev *dev;
244 for (i = 0; i < cd_count[file]; i++) {
247 dev = console_devices[file][i];
248 is_serial = console_dev_is_serial(dev);
249 if (dev->puts && serial_only == is_serial)
254 void console_puts_select_stderr(bool serial_only, const char *s)
256 console_puts_select(stderr, serial_only, s);
259 static void console_puts(int file, const char *s)
262 struct stdio_dev *dev;
264 for (i = 0; i < cd_count[file]; i++) {
265 dev = console_devices[file][i];
266 if (dev->puts != NULL)
271 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
272 static inline void console_doenv(int file, struct stdio_dev *dev)
274 iomux_doenv(file, dev->name);
278 static inline int console_getc(int file)
280 return stdio_devices[file]->getc(stdio_devices[file]);
283 static inline int console_tstc(int file)
285 return stdio_devices[file]->tstc(stdio_devices[file]);
288 static inline void console_putc(int file, const char c)
290 stdio_devices[file]->putc(stdio_devices[file], c);
293 void console_puts_select(int file, bool serial_only, const char *s)
295 if (serial_only == console_dev_is_serial(stdio_devices[file]))
296 stdio_devices[file]->puts(stdio_devices[file], s);
299 static inline void console_puts(int file, const char *s)
301 stdio_devices[file]->puts(stdio_devices[file], s);
304 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
305 static inline void console_doenv(int file, struct stdio_dev *dev)
307 console_setfile(file, dev);
310 #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
312 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
314 int serial_printf(const char *fmt, ...)
318 char printbuffer[CONFIG_SYS_PBSIZE];
322 /* For this to work, printbuffer must be larger than
323 * anything we ever want to print.
325 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
328 serial_puts(printbuffer);
334 if (file < MAX_FILES) {
336 * Effectively poll for input wherever it may be available.
340 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
342 * Upper layer may have already called tstc() so
343 * check for that first.
346 return console_getc(file);
349 if (console_tstc(file))
350 return console_getc(file);
353 * If the watchdog must be rate-limited then it should
354 * already be handled in board-specific code.
356 if (IS_ENABLED(CONFIG_WATCHDOG))
366 if (file < MAX_FILES)
367 return console_tstc(file);
372 void fputc(int file, const char c)
374 if (file < MAX_FILES)
375 console_putc(file, c);
378 void fputs(int file, const char *s)
380 if (file < MAX_FILES)
381 console_puts(file, s);
384 int fprintf(int file, const char *fmt, ...)
388 char printbuffer[CONFIG_SYS_PBSIZE];
392 /* For this to work, printbuffer must be larger than
393 * anything we ever want to print.
395 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
398 /* Send to desired file */
399 fputs(file, printbuffer);
403 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
407 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
410 if (!gd->have_console)
413 #ifdef CONFIG_CONSOLE_RECORD
414 if (gd->console_in.start) {
417 ch = membuff_getbyte((struct membuff *)&gd->console_in);
422 if (gd->flags & GD_FLG_DEVINIT) {
423 /* Get from the standard input */
427 /* Send directly to the handler */
428 return serial_getc();
433 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
436 if (!gd->have_console)
438 #ifdef CONFIG_CONSOLE_RECORD
439 if (gd->console_in.start) {
440 if (membuff_peekbyte((struct membuff *)&gd->console_in) != -1)
444 if (gd->flags & GD_FLG_DEVINIT) {
445 /* Test the standard input */
449 /* Send directly to the handler */
450 return serial_tstc();
453 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
454 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
456 #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
457 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
459 static void pre_console_putc(const char c)
463 buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
465 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
467 unmap_sysmem(buffer);
470 static void pre_console_puts(const char *s)
473 pre_console_putc(*s++);
476 static void print_pre_console_buffer(int flushpoint)
478 unsigned long in = 0, out = 0;
479 char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
482 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT))
485 buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
486 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
487 in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
489 while (in < gd->precon_buf_idx)
490 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
491 unmap_sysmem(buf_in);
495 switch (flushpoint) {
496 case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
499 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
500 console_puts_select(stdout, false, buf_out);
505 static inline void pre_console_putc(const char c) {}
506 static inline void pre_console_puts(const char *s) {}
507 static inline void print_pre_console_buffer(int flushpoint) {}
510 void putc(const char c)
514 #ifdef CONFIG_CONSOLE_RECORD
515 if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
516 membuff_putbyte((struct membuff *)&gd->console_out, c);
518 /* sandbox can send characters to stdout before it has a console */
519 if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
524 /* if we don't have a console yet, use the debug UART */
525 if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
530 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
531 if (!(gd->flags & GD_FLG_DEVINIT))
536 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
539 if (!gd->have_console)
540 return pre_console_putc(c);
542 if (gd->flags & GD_FLG_DEVINIT) {
543 /* Send to the standard output */
546 /* Send directly to the handler */
552 void puts(const char *s)
556 #ifdef CONFIG_CONSOLE_RECORD
557 if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
558 membuff_put((struct membuff *)&gd->console_out, s, strlen(s));
560 /* sandbox can send characters to stdout before it has a console */
561 if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
566 if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
575 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
576 if (!(gd->flags & GD_FLG_DEVINIT))
581 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
584 if (!gd->have_console)
585 return pre_console_puts(s);
587 if (gd->flags & GD_FLG_DEVINIT) {
588 /* Send to the standard output */
591 /* Send directly to the handler */
597 #ifdef CONFIG_CONSOLE_RECORD
598 int console_record_init(void)
602 ret = membuff_new((struct membuff *)&gd->console_out,
603 CONFIG_CONSOLE_RECORD_OUT_SIZE);
606 ret = membuff_new((struct membuff *)&gd->console_in,
607 CONFIG_CONSOLE_RECORD_IN_SIZE);
612 void console_record_reset(void)
614 membuff_purge((struct membuff *)&gd->console_out);
615 membuff_purge((struct membuff *)&gd->console_in);
618 int console_record_reset_enable(void)
620 console_record_reset();
621 gd->flags |= GD_FLG_RECORD;
626 int console_record_readline(char *str, int maxlen)
628 return membuff_readline((struct membuff *)&gd->console_out, str,
632 int console_record_avail(void)
634 return membuff_avail((struct membuff *)&gd->console_out);
639 /* test if ctrl-c was pressed */
640 static int ctrlc_disabled = 0; /* see disable_ctrl() */
641 static int ctrlc_was_pressed = 0;
644 if (!ctrlc_disabled && gd->have_console) {
647 case 0x03: /* ^C - Control C */
648 ctrlc_was_pressed = 1;
658 /* Reads user's confirmation.
659 Returns 1 if user's input is "y", "Y", "yes" or "YES"
661 int confirm_yesno(void)
670 while (i < sizeof(str_input)) {
671 str_input[i] = getchar();
673 if (str_input[i] == '\r')
678 if (strncmp(str_input, "y\r", 2) == 0 ||
679 strncmp(str_input, "Y\r", 2) == 0 ||
680 strncmp(str_input, "yes\r", 4) == 0 ||
681 strncmp(str_input, "YES\r", 4) == 0)
685 /* pass 1 to disable ctrlc() checking, 0 to enable.
686 * returns previous state
688 int disable_ctrlc(int disable)
690 int prev = ctrlc_disabled; /* save previous state */
692 ctrlc_disabled = disable;
698 return ctrlc_was_pressed;
701 void clear_ctrlc(void)
703 ctrlc_was_pressed = 0;
706 /** U-Boot INIT FUNCTIONS *************************************************/
708 struct stdio_dev *search_device(int flags, const char *name)
710 struct stdio_dev *dev;
712 dev = stdio_get_by_name(name);
713 #ifdef CONFIG_VIDCONSOLE_AS_LCD
714 if (!dev && !strcmp(name, CONFIG_VIDCONSOLE_AS_NAME))
715 dev = stdio_get_by_name("vidconsole");
718 if (dev && (dev->flags & flags))
724 int console_assign(int file, const char *devname)
727 struct stdio_dev *dev;
729 /* Check for valid file */
732 flag = DEV_FLAGS_INPUT;
736 flag = DEV_FLAGS_OUTPUT;
742 /* Check for valid device name */
744 dev = search_device(flag, devname);
747 return console_setfile(file, dev);
752 /* return true if the 'silent' flag is removed */
753 static bool console_update_silent(void)
755 unsigned long flags = gd->flags;
757 if (!IS_ENABLED(CONFIG_SILENT_CONSOLE))
760 if (env_get("silent")) {
761 gd->flags |= GD_FLG_SILENT;
765 gd->flags &= ~GD_FLG_SILENT;
767 return !!(flags & GD_FLG_SILENT);
770 int console_announce_r(void)
772 #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
773 char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
775 display_options_get_banner(false, buf, sizeof(buf));
777 console_puts_select(stdout, false, buf);
783 /* Called before relocation - use serial functions */
784 int console_init_f(void)
786 gd->have_console = 1;
788 console_update_silent();
790 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
795 void stdio_print_current_devices(void)
797 /* Print information */
799 if (stdio_devices[stdin] == NULL) {
800 puts("No input devices available!\n");
802 printf ("%s\n", stdio_devices[stdin]->name);
806 if (stdio_devices[stdout] == NULL) {
807 puts("No output devices available!\n");
809 printf ("%s\n", stdio_devices[stdout]->name);
813 if (stdio_devices[stderr] == NULL) {
814 puts("No error devices available!\n");
816 printf ("%s\n", stdio_devices[stderr]->name);
820 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
821 /* Called after the relocation - use desired console functions */
822 int console_init_r(void)
824 char *stdinname, *stdoutname, *stderrname;
825 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
830 /* update silent for env loaded from flash (initr_env) */
831 if (console_update_silent())
832 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
834 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
836 /* set default handlers at first */
837 gd->jt->getc = serial_getc;
838 gd->jt->tstc = serial_tstc;
839 gd->jt->putc = serial_putc;
840 gd->jt->puts = serial_puts;
841 gd->jt->printf = serial_printf;
843 /* stdin stdout and stderr are in environment */
845 stdinname = env_get("stdin");
846 stdoutname = env_get("stdout");
847 stderrname = env_get("stderr");
849 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
850 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
851 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
852 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
853 if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
854 iomux_err = iomux_doenv(stdin, stdinname);
855 iomux_err += iomux_doenv(stdout, stdoutname);
856 iomux_err += iomux_doenv(stderr, stderrname);
858 /* Successful, so skip all the code below. */
862 /* if the devices are overwritten or not found, use default device */
863 if (inputdev == NULL) {
864 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
866 if (outputdev == NULL) {
867 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
869 if (errdev == NULL) {
870 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
872 /* Initializes output console first */
873 if (outputdev != NULL) {
874 /* need to set a console if not done above. */
875 console_doenv(stdout, outputdev);
877 if (errdev != NULL) {
878 /* need to set a console if not done above. */
879 console_doenv(stderr, errdev);
881 if (inputdev != NULL) {
882 /* need to set a console if not done above. */
883 console_doenv(stdin, inputdev);
888 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
889 stdio_print_current_devices();
891 #ifdef CONFIG_VIDCONSOLE_AS_LCD
892 if (strstr(stdoutname, CONFIG_VIDCONSOLE_AS_NAME))
893 printf("Warning: Please change '%s' to 'vidconsole' in stdout/stderr environment vars\n",
894 CONFIG_VIDCONSOLE_AS_NAME);
897 if (IS_ENABLED(CONFIG_SYS_CONSOLE_ENV_OVERWRITE)) {
898 /* set the environment variables (will overwrite previous env settings) */
899 for (i = 0; i < MAX_FILES; i++)
900 env_set(stdio_names[i], stdio_devices[i]->name);
903 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
906 /* If nothing usable installed, use only the initial console */
907 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
910 print_pre_console_buffer(flushpoint);
914 #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
916 /* Called after the relocation - use desired console functions */
917 int console_init_r(void)
919 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
921 struct list_head *list = stdio_get_list();
922 struct list_head *pos;
923 struct stdio_dev *dev;
926 /* update silent for env loaded from flash (initr_env) */
927 if (console_update_silent())
928 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
930 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
933 * suppress all output if splash screen is enabled and we have
934 * a bmp to display. We redirect the output from frame buffer
935 * console to serial console in this case or suppress it if
936 * "silent" mode was requested.
938 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) && env_get("splashimage")) {
939 if (!(gd->flags & GD_FLG_SILENT))
940 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
943 /* Scan devices looking for input and output devices */
944 list_for_each(pos, list) {
945 dev = list_entry(pos, struct stdio_dev, list);
947 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
950 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
953 if(inputdev && outputdev)
957 /* Initializes output console first */
958 if (outputdev != NULL) {
959 console_setfile(stdout, outputdev);
960 console_setfile(stderr, outputdev);
961 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
962 console_devices[stdout][0] = outputdev;
963 console_devices[stderr][0] = outputdev;
967 /* Initializes input console */
968 if (inputdev != NULL) {
969 console_setfile(stdin, inputdev);
970 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
971 console_devices[stdin][0] = inputdev;
975 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
976 stdio_print_current_devices();
978 /* Setting environment variables */
979 for (i = 0; i < MAX_FILES; i++) {
980 env_set(stdio_names[i], stdio_devices[i]->name);
983 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
986 /* If nothing usable installed, use only the initial console */
987 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
990 print_pre_console_buffer(flushpoint);
994 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */