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)
58 if ((flags & H_FORCE) == 0)
59 printf("Can't delete \"%s\"\n", name);
66 U_BOOT_ENV_CALLBACK(console, on_console);
68 #ifdef CONFIG_SILENT_CONSOLE
69 static int on_silent(const char *name, const char *value, enum env_op op,
72 #if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_SET)
73 if (flags & H_INTERACTIVE)
76 #if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_RELOC)
77 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)
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 #ifdef CONFIG_DM_SERIAL
163 if (sdev->flags & DEV_FLAGS_DM) {
164 struct udevice *dev = sdev->priv;
166 is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL;
169 is_serial = !strcmp(sdev->name, "serial");
174 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
175 /** Console I/O multiplexing *******************************************/
177 static struct stdio_dev *tstcdev;
178 struct stdio_dev **console_devices[MAX_FILES];
179 int cd_count[MAX_FILES];
182 * This depends on tstc() always being called before getc().
183 * This is guaranteed to be true because this routine is called
184 * only from fgetc() which assures it.
185 * No attempt is made to demultiplex multiple input sources.
187 static int console_getc(int file)
191 /* This is never called with testcdev == NULL */
192 ret = tstcdev->getc(tstcdev);
197 static int console_tstc(int file)
200 struct stdio_dev *dev;
203 prev = disable_ctrlc(1);
204 for (i = 0; i < cd_count[file]; i++) {
205 dev = console_devices[file][i];
206 if (dev->tstc != NULL) {
207 ret = dev->tstc(dev);
220 static void console_putc(int file, const char c)
223 struct stdio_dev *dev;
225 for (i = 0; i < cd_count[file]; i++) {
226 dev = console_devices[file][i];
227 if (dev->putc != NULL)
233 * console_puts_select() - Output a string to all console devices
235 * @file: File number to output to (e,g, stdout, see stdio.h)
236 * @serial_only: true to output only to serial, false to output to everything
238 * @s: String to output
240 static void console_puts_select(int file, bool serial_only, const char *s)
243 struct stdio_dev *dev;
245 for (i = 0; i < cd_count[file]; i++) {
248 dev = console_devices[file][i];
249 is_serial = console_dev_is_serial(dev);
250 if (dev->puts && serial_only == is_serial)
255 void console_puts_select_stderr(bool serial_only, const char *s)
257 console_puts_select(stderr, serial_only, s);
260 static void console_puts(int file, const char *s)
263 struct stdio_dev *dev;
265 for (i = 0; i < cd_count[file]; i++) {
266 dev = console_devices[file][i];
267 if (dev->puts != NULL)
272 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
273 static inline void console_doenv(int file, struct stdio_dev *dev)
275 iomux_doenv(file, dev->name);
279 static inline int console_getc(int file)
281 return stdio_devices[file]->getc(stdio_devices[file]);
284 static inline int console_tstc(int file)
286 return stdio_devices[file]->tstc(stdio_devices[file]);
289 static inline void console_putc(int file, const char c)
291 stdio_devices[file]->putc(stdio_devices[file], c);
294 void console_puts_select(int file, bool serial_only, const char *s)
296 if (serial_only == console_dev_is_serial(stdio_devices[file]))
297 stdio_devices[file]->puts(stdio_devices[file], s);
300 static inline void console_puts(int file, const char *s)
302 stdio_devices[file]->puts(stdio_devices[file], s);
305 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
306 static inline void console_doenv(int file, struct stdio_dev *dev)
308 console_setfile(file, dev);
311 #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
313 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
315 int serial_printf(const char *fmt, ...)
319 char printbuffer[CONFIG_SYS_PBSIZE];
323 /* For this to work, printbuffer must be larger than
324 * anything we ever want to print.
326 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
329 serial_puts(printbuffer);
335 if (file < MAX_FILES) {
337 * Effectively poll for input wherever it may be available.
341 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
343 * Upper layer may have already called tstc() so
344 * check for that first.
347 return console_getc(file);
350 if (console_tstc(file))
351 return console_getc(file);
353 #ifdef CONFIG_WATCHDOG
355 * If the watchdog must be rate-limited then it should
356 * already be handled in board-specific code.
368 if (file < MAX_FILES)
369 return console_tstc(file);
374 void fputc(int file, const char c)
376 if (file < MAX_FILES)
377 console_putc(file, c);
380 void fputs(int file, const char *s)
382 if (file < MAX_FILES)
383 console_puts(file, s);
386 int fprintf(int file, const char *fmt, ...)
390 char printbuffer[CONFIG_SYS_PBSIZE];
394 /* For this to work, printbuffer must be larger than
395 * anything we ever want to print.
397 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
400 /* Send to desired file */
401 fputs(file, printbuffer);
405 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
409 #ifdef CONFIG_DISABLE_CONSOLE
410 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
414 if (!gd->have_console)
417 #ifdef CONFIG_CONSOLE_RECORD
418 if (gd->console_in.start) {
421 ch = membuff_getbyte((struct membuff *)&gd->console_in);
426 if (gd->flags & GD_FLG_DEVINIT) {
427 /* Get from the standard input */
431 /* Send directly to the handler */
432 return serial_getc();
437 #ifdef CONFIG_DISABLE_CONSOLE
438 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
442 if (!gd->have_console)
444 #ifdef CONFIG_CONSOLE_RECORD
445 if (gd->console_in.start) {
446 if (membuff_peekbyte((struct membuff *)&gd->console_in) != -1)
450 if (gd->flags & GD_FLG_DEVINIT) {
451 /* Test the standard input */
455 /* Send directly to the handler */
456 return serial_tstc();
459 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
460 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
462 #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
463 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
465 static void pre_console_putc(const char c)
469 buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
471 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
473 unmap_sysmem(buffer);
476 static void pre_console_puts(const char *s)
479 pre_console_putc(*s++);
482 static void print_pre_console_buffer(int flushpoint)
484 unsigned long in = 0, out = 0;
485 char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
488 #ifdef CONFIG_SILENT_CONSOLE
489 if (gd->flags & GD_FLG_SILENT)
493 buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
494 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
495 in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
497 while (in < gd->precon_buf_idx)
498 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
499 unmap_sysmem(buf_in);
503 switch (flushpoint) {
504 case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
507 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
508 console_puts_select(stdout, false, buf_out);
513 static inline void pre_console_putc(const char c) {}
514 static inline void pre_console_puts(const char *s) {}
515 static inline void print_pre_console_buffer(int flushpoint) {}
518 void putc(const char c)
520 #ifdef CONFIG_SANDBOX
521 /* sandbox can send characters to stdout before it has a console */
522 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
527 #ifdef CONFIG_DEBUG_UART
528 /* if we don't have a console yet, use the debug UART */
529 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
536 #ifdef CONFIG_CONSOLE_RECORD
537 if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
538 membuff_putbyte((struct membuff *)&gd->console_out, c);
540 #ifdef CONFIG_SILENT_CONSOLE
541 if (gd->flags & GD_FLG_SILENT) {
542 if (!(gd->flags & GD_FLG_DEVINIT))
548 #ifdef CONFIG_DISABLE_CONSOLE
549 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
553 if (!gd->have_console)
554 return pre_console_putc(c);
556 if (gd->flags & GD_FLG_DEVINIT) {
557 /* Send to the standard output */
560 /* Send directly to the handler */
566 void puts(const char *s)
568 #ifdef CONFIG_SANDBOX
569 /* sandbox can send characters to stdout before it has a console */
570 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
575 #ifdef CONFIG_DEBUG_UART
576 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
587 #ifdef CONFIG_CONSOLE_RECORD
588 if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
589 membuff_put((struct membuff *)&gd->console_out, s, strlen(s));
591 #ifdef CONFIG_SILENT_CONSOLE
592 if (gd->flags & GD_FLG_SILENT) {
593 if (!(gd->flags & GD_FLG_DEVINIT))
599 #ifdef CONFIG_DISABLE_CONSOLE
600 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
604 if (!gd->have_console)
605 return pre_console_puts(s);
607 if (gd->flags & GD_FLG_DEVINIT) {
608 /* Send to the standard output */
611 /* Send directly to the handler */
617 #ifdef CONFIG_CONSOLE_RECORD
618 int console_record_init(void)
622 ret = membuff_new((struct membuff *)&gd->console_out,
623 CONFIG_CONSOLE_RECORD_OUT_SIZE);
626 ret = membuff_new((struct membuff *)&gd->console_in,
627 CONFIG_CONSOLE_RECORD_IN_SIZE);
632 void console_record_reset(void)
634 membuff_purge((struct membuff *)&gd->console_out);
635 membuff_purge((struct membuff *)&gd->console_in);
638 void console_record_reset_enable(void)
640 console_record_reset();
641 gd->flags |= GD_FLG_RECORD;
644 int console_record_readline(char *str, int maxlen)
646 return membuff_readline((struct membuff *)&gd->console_out, str,
650 int console_record_avail(void)
652 return membuff_avail((struct membuff *)&gd->console_out);
657 /* test if ctrl-c was pressed */
658 static int ctrlc_disabled = 0; /* see disable_ctrl() */
659 static int ctrlc_was_pressed = 0;
662 if (!ctrlc_disabled && gd->have_console) {
665 case 0x03: /* ^C - Control C */
666 ctrlc_was_pressed = 1;
676 /* Reads user's confirmation.
677 Returns 1 if user's input is "y", "Y", "yes" or "YES"
679 int confirm_yesno(void)
688 while (i < sizeof(str_input)) {
689 str_input[i] = getc();
691 if (str_input[i] == '\r')
696 if (strncmp(str_input, "y\r", 2) == 0 ||
697 strncmp(str_input, "Y\r", 2) == 0 ||
698 strncmp(str_input, "yes\r", 4) == 0 ||
699 strncmp(str_input, "YES\r", 4) == 0)
703 /* pass 1 to disable ctrlc() checking, 0 to enable.
704 * returns previous state
706 int disable_ctrlc(int disable)
708 int prev = ctrlc_disabled; /* save previous state */
710 ctrlc_disabled = disable;
716 return ctrlc_was_pressed;
719 void clear_ctrlc(void)
721 ctrlc_was_pressed = 0;
724 /** U-Boot INIT FUNCTIONS *************************************************/
726 struct stdio_dev *search_device(int flags, const char *name)
728 struct stdio_dev *dev;
730 dev = stdio_get_by_name(name);
731 #ifdef CONFIG_VIDCONSOLE_AS_LCD
732 if (!dev && !strcmp(name, CONFIG_VIDCONSOLE_AS_NAME))
733 dev = stdio_get_by_name("vidconsole");
736 if (dev && (dev->flags & flags))
742 int console_assign(int file, const char *devname)
745 struct stdio_dev *dev;
747 /* Check for valid file */
750 flag = DEV_FLAGS_INPUT;
754 flag = DEV_FLAGS_OUTPUT;
760 /* Check for valid device name */
762 dev = search_device(flag, devname);
765 return console_setfile(file, dev);
770 /* return true if the 'silent' flag is removed */
771 static bool console_update_silent(void)
773 #ifdef CONFIG_SILENT_CONSOLE
774 if (env_get("silent")) {
775 gd->flags |= GD_FLG_SILENT;
777 unsigned long flags = gd->flags;
779 gd->flags &= ~GD_FLG_SILENT;
781 return !!(flags & GD_FLG_SILENT);
788 int console_announce_r(void)
790 #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
791 char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
793 display_options_get_banner(false, buf, sizeof(buf));
795 console_puts_select(stdout, false, buf);
801 /* Called before relocation - use serial functions */
802 int console_init_f(void)
804 gd->have_console = 1;
806 console_update_silent();
808 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
813 void stdio_print_current_devices(void)
815 /* Print information */
817 if (stdio_devices[stdin] == NULL) {
818 puts("No input devices available!\n");
820 printf ("%s\n", stdio_devices[stdin]->name);
824 if (stdio_devices[stdout] == NULL) {
825 puts("No output devices available!\n");
827 printf ("%s\n", stdio_devices[stdout]->name);
831 if (stdio_devices[stderr] == NULL) {
832 puts("No error devices available!\n");
834 printf ("%s\n", stdio_devices[stderr]->name);
838 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
839 /* Called after the relocation - use desired console functions */
840 int console_init_r(void)
842 char *stdinname, *stdoutname, *stderrname;
843 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
844 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
846 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
847 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
852 /* update silent for env loaded from flash (initr_env) */
853 if (console_update_silent())
854 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
856 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
858 /* set default handlers at first */
859 gd->jt->getc = serial_getc;
860 gd->jt->tstc = serial_tstc;
861 gd->jt->putc = serial_putc;
862 gd->jt->puts = serial_puts;
863 gd->jt->printf = serial_printf;
865 /* stdin stdout and stderr are in environment */
867 stdinname = env_get("stdin");
868 stdoutname = env_get("stdout");
869 stderrname = env_get("stderr");
871 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
872 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
873 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
874 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
875 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
876 iomux_err = iomux_doenv(stdin, stdinname);
877 iomux_err += iomux_doenv(stdout, stdoutname);
878 iomux_err += iomux_doenv(stderr, stderrname);
880 /* Successful, so skip all the code below. */
884 /* if the devices are overwritten or not found, use default device */
885 if (inputdev == NULL) {
886 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
888 if (outputdev == NULL) {
889 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
891 if (errdev == NULL) {
892 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
894 /* Initializes output console first */
895 if (outputdev != NULL) {
896 /* need to set a console if not done above. */
897 console_doenv(stdout, outputdev);
899 if (errdev != NULL) {
900 /* need to set a console if not done above. */
901 console_doenv(stderr, errdev);
903 if (inputdev != NULL) {
904 /* need to set a console if not done above. */
905 console_doenv(stdin, inputdev);
908 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
912 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
913 stdio_print_current_devices();
914 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
915 #ifdef CONFIG_VIDCONSOLE_AS_LCD
916 if (strstr(stdoutname, CONFIG_VIDCONSOLE_AS_NAME))
917 printf("Warning: Please change '%s' to 'vidconsole' in stdout/stderr environment vars\n",
918 CONFIG_VIDCONSOLE_AS_NAME);
921 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
922 /* set the environment variables (will overwrite previous env settings) */
923 for (i = 0; i < MAX_FILES; i++) {
924 env_set(stdio_names[i], stdio_devices[i]->name);
926 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
928 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
931 /* If nothing usable installed, use only the initial console */
932 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
935 print_pre_console_buffer(flushpoint);
939 #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
941 /* Called after the relocation - use desired console functions */
942 int console_init_r(void)
944 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
946 struct list_head *list = stdio_get_list();
947 struct list_head *pos;
948 struct stdio_dev *dev;
951 /* update silent for env loaded from flash (initr_env) */
952 if (console_update_silent())
953 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
955 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
957 #ifdef CONFIG_SPLASH_SCREEN
959 * suppress all output if splash screen is enabled and we have
960 * a bmp to display. We redirect the output from frame buffer
961 * console to serial console in this case or suppress it if
962 * "silent" mode was requested.
964 if (env_get("splashimage") != NULL) {
965 if (!(gd->flags & GD_FLG_SILENT))
966 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
970 /* Scan devices looking for input and output devices */
971 list_for_each(pos, list) {
972 dev = list_entry(pos, struct stdio_dev, list);
974 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
977 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
980 if(inputdev && outputdev)
984 /* Initializes output console first */
985 if (outputdev != NULL) {
986 console_setfile(stdout, outputdev);
987 console_setfile(stderr, outputdev);
988 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
989 console_devices[stdout][0] = outputdev;
990 console_devices[stderr][0] = outputdev;
994 /* Initializes input console */
995 if (inputdev != NULL) {
996 console_setfile(stdin, inputdev);
997 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
998 console_devices[stdin][0] = inputdev;
1002 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
1003 stdio_print_current_devices();
1004 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
1006 /* Setting environment variables */
1007 for (i = 0; i < MAX_FILES; i++) {
1008 env_set(stdio_names[i], stdio_devices[i]->name);
1011 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
1014 /* If nothing usable installed, use only the initial console */
1015 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
1018 print_pre_console_buffer(flushpoint);
1022 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */