video: Factor out vidconsole_put_string()
[oweals/u-boot.git] / common / console.c
index b7ed9a3b9debc312d5cbc8addb210489d7c253cf..0b0dd76256c7c865668add13a2ae89eb1530cb3f 100644 (file)
@@ -1,13 +1,13 @@
+// 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>
 #include <console.h>
 #include <debug_uart.h>
+#include <dm.h>
 #include <stdarg.h>
 #include <iomux.h>
 #include <malloc.h>
@@ -67,11 +67,11 @@ U_BOOT_ENV_CALLBACK(console, on_console);
 static int on_silent(const char *name, const char *value, enum env_op op,
        int flags)
 {
-#if !CONFIG_IS_ENABLED(CONFIG_SILENT_CONSOLE_UPDATE_ON_SET)
+#if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_SET)
        if (flags & H_INTERACTIVE)
                return 0;
 #endif
-#if !CONFIG_IS_ENABLED(CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC)
+#if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_RELOC)
        if ((flags & H_INTERACTIVE) == 0)
                return 0;
 #endif
@@ -150,12 +150,20 @@ static int console_setfile(int file, struct stdio_dev * dev)
  * console_dev_is_serial() - Check if a stdio device is a serial device
  *
  * @sdev: Device to check
- * @return true if this device is a serial device
+ * @return true if this device is in the serial uclass (or for pre-driver-model,
+ * whether it is called "serial".
  */
 static bool console_dev_is_serial(struct stdio_dev *sdev)
 {
        bool is_serial;
 
+#ifdef CONFIG_DM_SERIAL
+       if (sdev->flags & DEV_FLAGS_DM) {
+               struct udevice *dev = sdev->priv;
+
+               is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL;
+       } else
+#endif
        is_serial = !strcmp(sdev->name, "serial");
 
        return is_serial;
@@ -188,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;
 }
@@ -302,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.
@@ -315,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
@@ -323,9 +336,6 @@ int fgetc(int file)
                         udelay(1);
 #endif
                }
-#else
-               return console_getc(file);
-#endif
        }
 
        return -1;
@@ -441,6 +451,12 @@ static void pre_console_putc(const char c)
        unmap_sysmem(buffer);
 }
 
+static void pre_console_puts(const char *s)
+{
+       while (*s)
+               pre_console_putc(*s++);
+}
+
 static void print_pre_console_buffer(int flushpoint)
 {
        unsigned long in = 0, out = 0;
@@ -468,11 +484,19 @@ static void print_pre_console_buffer(int flushpoint)
 }
 #else
 static inline void pre_console_putc(const char c) {}
+static inline void pre_console_puts(const char *s) {}
 static inline void print_pre_console_buffer(int flushpoint) {}
 #endif
 
 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)) {
@@ -480,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
@@ -509,8 +535,50 @@ void putc(const char c)
 
 void puts(const char *s)
 {
-       while (*s)
-               putc(*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) {
+                       int ch = *s++;
+
+                       printch(ch);
+               }
+               return;
+       }
+#endif
+       if (!gd)
+               return;
+#ifdef CONFIG_CONSOLE_RECORD
+       if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
+               membuff_put(&gd->console_out, s, strlen(s));
+#endif
+#ifdef CONFIG_SILENT_CONSOLE
+       if (gd->flags & GD_FLG_SILENT)
+               return;
+#endif
+
+#ifdef CONFIG_DISABLE_CONSOLE
+       if (gd->flags & GD_FLG_DISABLE_CONSOLE)
+               return;
+#endif
+
+       if (!gd->have_console)
+               return pre_console_puts(s);
+
+       if (gd->flags & GD_FLG_DEVINIT) {
+               /* Send to the standard output */
+               fputs(stdout, s);
+       } else {
+               /* Send directly to the handler */
+               pre_console_puts(s);
+               serial_puts(s);
+       }
 }
 
 #ifdef CONFIG_CONSOLE_RECORD
@@ -544,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()) {
@@ -556,7 +623,6 @@ int ctrlc(void)
                        }
                }
        }
-#endif
 
        return 0;
 }
@@ -657,7 +723,7 @@ int console_assign(int file, const char *devname)
 static void console_update_silent(void)
 {
 #ifdef CONFIG_SILENT_CONSOLE
-       if (getenv("silent") != NULL)
+       if (env_get("silent") != NULL)
                gd->flags |= GD_FLG_SILENT;
        else
                gd->flags &= ~GD_FLG_SILENT;
@@ -736,9 +802,9 @@ int console_init_r(void)
 
        /* stdin stdout and stderr are in environment */
        /* scan for it */
-       stdinname  = getenv("stdin");
-       stdoutname = getenv("stdout");
-       stderrname = getenv("stderr");
+       stdinname  = env_get("stdin");
+       stdoutname = env_get("stdout");
+       stderrname = env_get("stderr");
 
        if (OVERWRITE_CONSOLE == 0) {   /* if not overwritten by config switch */
                inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
@@ -791,8 +857,8 @@ done:
 
 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
        /* set the environment variables (will overwrite previous env settings) */
-       for (i = 0; i < 3; i++) {
-               setenv(stdio_names[i], stdio_devices[i]->name);
+       for (i = 0; i < MAX_FILES; i++) {
+               env_set(stdio_names[i], stdio_devices[i]->name);
        }
 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
 
@@ -827,7 +893,7 @@ int console_init_r(void)
         * console to serial console in this case or suppress it if
         * "silent" mode was requested.
         */
-       if (getenv("splashimage") != NULL) {
+       if (env_get("splashimage") != NULL) {
                if (!(gd->flags & GD_FLG_SILENT))
                        outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
        }
@@ -870,8 +936,8 @@ int console_init_r(void)
 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
 
        /* Setting environment variables */
-       for (i = 0; i < 3; i++) {
-               setenv(stdio_names[i], stdio_devices[i]->name);
+       for (i = 0; i < MAX_FILES; i++) {
+               env_set(stdio_names[i], stdio_devices[i]->name);
        }
 
        gd->flags |= GD_FLG_DEVINIT;    /* device initialization completed */