video: Factor out vidconsole_put_string()
[oweals/u-boot.git] / common / console.c
index d763f2c68460df82aae6ce933823835b0e419200..0b0dd76256c7c865668add13a2ae89eb1530cb3f 100644 (file)
@@ -1,8 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2000
  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
@@ -197,20 +196,21 @@ static int console_tstc(int file)
 {
        int i, ret;
        struct stdio_dev *dev;
+       int prev;
 
-       disable_ctrlc(1);
+       prev = disable_ctrlc(1);
        for (i = 0; i < cd_count[file]; i++) {
                dev = console_devices[file][i];
                if (dev->tstc != NULL) {
                        ret = dev->tstc(dev);
                        if (ret > 0) {
                                tstcdev = dev;
-                               disable_ctrlc(0);
+                               disable_ctrlc(prev);
                                return ret;
                        }
                }
        }
-       disable_ctrlc(0);
+       disable_ctrlc(prev);
 
        return 0;
 }
@@ -311,12 +311,12 @@ int serial_printf(const char *fmt, ...)
 int fgetc(int file)
 {
        if (file < MAX_FILES) {
-#if CONFIG_IS_ENABLED(CONSOLE_MUX)
                /*
                 * Effectively poll for input wherever it may be available.
                 */
                for (;;) {
                        WATCHDOG_RESET();
+#if CONFIG_IS_ENABLED(CONSOLE_MUX)
                        /*
                         * Upper layer may have already called tstc() so
                         * check for that first.
@@ -324,6 +324,10 @@ int fgetc(int file)
                        if (tstcdev != NULL)
                                return console_getc(file);
                        console_tstc(file);
+#else
+                       if (console_tstc(file))
+                               return console_getc(file);
+#endif
 #ifdef CONFIG_WATCHDOG
                        /*
                         * If the watchdog must be rate-limited then it should
@@ -332,9 +336,6 @@ int fgetc(int file)
                         udelay(1);
 #endif
                }
-#else
-               return console_getc(file);
-#endif
        }
 
        return -1;
@@ -489,6 +490,13 @@ static inline void print_pre_console_buffer(int flushpoint) {}
 
 void putc(const char c)
 {
+#ifdef CONFIG_SANDBOX
+       /* sandbox can send characters to stdout before it has a console */
+       if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
+               os_putc(c);
+               return;
+       }
+#endif
 #ifdef CONFIG_DEBUG_UART
        /* if we don't have a console yet, use the debug UART */
        if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
@@ -496,8 +504,10 @@ void putc(const char c)
                return;
        }
 #endif
+       if (!gd)
+               return;
 #ifdef CONFIG_CONSOLE_RECORD
-       if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
+       if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
                membuff_putbyte(&gd->console_out, c);
 #endif
 #ifdef CONFIG_SILENT_CONSOLE
@@ -525,6 +535,13 @@ void putc(const char c)
 
 void puts(const char *s)
 {
+#ifdef CONFIG_SANDBOX
+       /* sandbox can send characters to stdout before it has a console */
+       if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
+               os_puts(s);
+               return;
+       }
+#endif
 #ifdef CONFIG_DEBUG_UART
        if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
                while (*s) {
@@ -535,8 +552,10 @@ void puts(const char *s)
                return;
        }
 #endif
+       if (!gd)
+               return;
 #ifdef CONFIG_CONSOLE_RECORD
-       if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
+       if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
                membuff_put(&gd->console_out, s, strlen(s));
 #endif
 #ifdef CONFIG_SILENT_CONSOLE
@@ -593,7 +612,6 @@ static int ctrlc_disabled = 0;      /* see disable_ctrl() */
 static int ctrlc_was_pressed = 0;
 int ctrlc(void)
 {
-#ifndef CONFIG_SANDBOX
        if (!ctrlc_disabled && gd->have_console) {
                if (tstc()) {
                        switch (getc()) {
@@ -605,7 +623,6 @@ int ctrlc(void)
                        }
                }
        }
-#endif
 
        return 0;
 }
@@ -840,7 +857,7 @@ done:
 
 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
        /* set the environment variables (will overwrite previous env settings) */
-       for (i = 0; i < 3; i++) {
+       for (i = 0; i < MAX_FILES; i++) {
                env_set(stdio_names[i], stdio_devices[i]->name);
        }
 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
@@ -919,7 +936,7 @@ int console_init_r(void)
 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
 
        /* Setting environment variables */
-       for (i = 0; i < 3; i++) {
+       for (i = 0; i < MAX_FILES; i++) {
                env_set(stdio_names[i], stdio_devices[i]->name);
        }