common/memsize.c: Check for overflow in get_effective_memsize() only for mpc85xx
[platform/kernel/u-boot.git] / common / console.c
index 9064a6c..10ab361 100644 (file)
@@ -7,6 +7,7 @@
 #include <common.h>
 #include <console.h>
 #include <debug_uart.h>
+#include <display_options.h>
 #include <dm.h>
 #include <env.h>
 #include <stdarg.h>
@@ -95,16 +96,22 @@ static void console_record_putc(const char c)
 {
        if (!(gd->flags & GD_FLG_RECORD))
                return;
-       if  (gd->console_out.start)
-               membuff_putbyte((struct membuff *)&gd->console_out, c);
+       if  (gd->console_out.start &&
+            !membuff_putbyte((struct membuff *)&gd->console_out, c))
+               gd->flags |= GD_FLG_RECORD_OVF;
 }
 
 static void console_record_puts(const char *s)
 {
        if (!(gd->flags & GD_FLG_RECORD))
                return;
-       if  (gd->console_out.start)
-               membuff_put((struct membuff *)&gd->console_out, s, strlen(s));
+       if  (gd->console_out.start) {
+               int len = strlen(s);
+
+               if (membuff_put((struct membuff *)&gd->console_out, s, len) !=
+                   len)
+                       gd->flags |= GD_FLG_RECORD_OVF;
+       }
 }
 
 static int console_record_getc(void)
@@ -192,6 +199,7 @@ static int console_setfile(int file, struct stdio_dev * dev)
                case stdout:
                        gd->jt->putc  = putc;
                        gd->jt->puts  = puts;
+                       STDIO_DEV_ASSIGN_FLUSH(gd->jt, flush);
                        gd->jt->printf = printf;
                        break;
                }
@@ -207,7 +215,7 @@ 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 in the serial uclass (or for pre-driver-model,
+ * 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)
@@ -233,7 +241,7 @@ static struct stdio_dev *tstcdev;
 struct stdio_dev **console_devices[MAX_FILES];
 int cd_count[MAX_FILES];
 
-static void __maybe_unused console_devices_set(int file, struct stdio_dev *dev)
+static void console_devices_set(int file, struct stdio_dev *dev)
 {
        console_devices[file][0] = dev;
        cd_count[file] = 1;
@@ -252,15 +260,14 @@ static void __maybe_unused console_devices_set(int file, struct stdio_dev *dev)
  */
 static bool console_needs_start_stop(int file, struct stdio_dev *sdev)
 {
-       int i, j;
+       int i;
 
        for (i = 0; i < ARRAY_SIZE(cd_count); i++) {
                if (i == file)
                        continue;
 
-               for (j = 0; j < cd_count[i]; j++)
-                       if (console_devices[i][j] == sdev)
-                               return false;
+               if (iomux_match_device(console_devices[i], cd_count[i], sdev) >= 0)
+                       return false;
        }
        return true;
 }
@@ -294,8 +301,7 @@ static int console_tstc(int file)
        int prev;
 
        prev = disable_ctrlc(1);
-       for (i = 0; i < cd_count[file]; i++) {
-               dev = console_devices[file][i];
+       for_each_console_dev(i, file, dev) {
                if (dev->tstc != NULL) {
                        ret = dev->tstc(dev);
                        if (ret > 0) {
@@ -315,8 +321,7 @@ static void console_putc(int file, const char c)
        int i;
        struct stdio_dev *dev;
 
-       for (i = 0; i < cd_count[file]; i++) {
-               dev = console_devices[file][i];
+       for_each_console_dev(i, file, dev) {
                if (dev->putc != NULL)
                        dev->putc(dev, c);
        }
@@ -335,11 +340,9 @@ static void console_puts_select(int file, bool serial_only, const char *s)
        int i;
        struct stdio_dev *dev;
 
-       for (i = 0; i < cd_count[file]; i++) {
-               bool is_serial;
+       for_each_console_dev(i, file, dev) {
+               bool is_serial = console_dev_is_serial(dev);
 
-               dev = console_devices[file][i];
-               is_serial = console_dev_is_serial(dev);
                if (dev->puts && serial_only == is_serial)
                        dev->puts(dev, s);
        }
@@ -347,7 +350,8 @@ static void console_puts_select(int file, bool serial_only, const char *s)
 
 void console_puts_select_stderr(bool serial_only, const char *s)
 {
-       console_puts_select(stderr, serial_only, s);
+       if (gd->flags & GD_FLG_DEVINIT)
+               console_puts_select(stderr, serial_only, s);
 }
 
 static void console_puts(int file, const char *s)
@@ -355,13 +359,25 @@ static void console_puts(int file, const char *s)
        int i;
        struct stdio_dev *dev;
 
-       for (i = 0; i < cd_count[file]; i++) {
-               dev = console_devices[file][i];
+       for_each_console_dev(i, file, dev) {
                if (dev->puts != NULL)
                        dev->puts(dev, s);
        }
 }
 
+#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
+static void console_flush(int file)
+{
+       int i;
+       struct stdio_dev *dev;
+
+       for_each_console_dev(i, file, dev) {
+               if (dev->flush != NULL)
+                       dev->flush(dev);
+       }
+}
+#endif
+
 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
 static inline void console_doenv(int file, struct stdio_dev *dev)
 {
@@ -370,7 +386,7 @@ static inline void console_doenv(int file, struct stdio_dev *dev)
 #endif
 #else
 
-static void __maybe_unused console_devices_set(int file, struct stdio_dev *dev)
+static void console_devices_set(int file, struct stdio_dev *dev)
 {
 }
 
@@ -401,7 +417,8 @@ static inline void console_putc(int file, const char c)
 
 void console_puts_select(int file, bool serial_only, const char *s)
 {
-       if (serial_only == console_dev_is_serial(stdio_devices[file]))
+       if ((gd->flags & GD_FLG_DEVINIT) &&
+           serial_only == console_dev_is_serial(stdio_devices[file]))
                stdio_devices[file]->puts(stdio_devices[file], s);
 }
 
@@ -410,6 +427,14 @@ static inline void console_puts(int file, const char *s)
        stdio_devices[file]->puts(stdio_devices[file], s);
 }
 
+#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
+static inline void console_flush(int file)
+{
+       if (stdio_devices[file]->flush)
+               stdio_devices[file]->flush(stdio_devices[file]);
+}
+#endif
+
 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
 static inline void console_doenv(int file, struct stdio_dev *dev)
 {
@@ -418,6 +443,12 @@ static inline void console_doenv(int file, struct stdio_dev *dev)
 #endif
 #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
 
+static void __maybe_unused console_setfile_and_devices(int file, struct stdio_dev *dev)
+{
+       console_setfile(file, dev);
+       console_devices_set(file, dev);
+}
+
 int console_start(int file, struct stdio_dev *sdev)
 {
        int error;
@@ -466,12 +497,12 @@ int serial_printf(const char *fmt, ...)
 
 int fgetc(int file)
 {
-       if (file < MAX_FILES) {
+       if ((unsigned int)file < MAX_FILES) {
                /*
                 * Effectively poll for input wherever it may be available.
                 */
                for (;;) {
-                       WATCHDOG_RESET();
+                       schedule();
                        if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
                                /*
                                 * Upper layer may have already called tstc() so
@@ -499,7 +530,7 @@ int fgetc(int file)
 
 int ftstc(int file)
 {
-       if (file < MAX_FILES)
+       if ((unsigned int)file < MAX_FILES)
                return console_tstc(file);
 
        return -1;
@@ -507,16 +538,24 @@ int ftstc(int file)
 
 void fputc(int file, const char c)
 {
-       if (file < MAX_FILES)
+       if ((unsigned int)file < MAX_FILES)
                console_putc(file, c);
 }
 
 void fputs(int file, const char *s)
 {
-       if (file < MAX_FILES)
+       if ((unsigned int)file < MAX_FILES)
                console_puts(file, s);
 }
 
+#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
+void fflush(int file)
+{
+       if ((unsigned int)file < MAX_FILES)
+               console_flush(file);
+}
+#endif
+
 int fprintf(int file, const char *fmt, ...)
 {
        va_list args;
@@ -585,13 +624,16 @@ int tstc(void)
 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL  1
 
 #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
-#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
+#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_VAL(PRE_CON_BUF_SZ))
 
 static void pre_console_putc(const char c)
 {
        char *buffer;
 
-       buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
+       if (gd->precon_buf_idx < 0)
+               return;
+
+       buffer = map_sysmem(CONFIG_VAL(PRE_CON_BUF_ADDR), CONFIG_VAL(PRE_CON_BUF_SZ));
 
        buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
 
@@ -600,22 +642,25 @@ static void pre_console_putc(const char c)
 
 static void pre_console_puts(const char *s)
 {
+       if (gd->precon_buf_idx < 0)
+               return;
+
        while (*s)
                pre_console_putc(*s++);
 }
 
 static void print_pre_console_buffer(int flushpoint)
 {
-       unsigned long in = 0, out = 0;
-       char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
+       long in = 0, out = 0;
+       char buf_out[CONFIG_VAL(PRE_CON_BUF_SZ) + 1];
        char *buf_in;
 
        if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT))
                return;
 
-       buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
-       if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
-               in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
+       buf_in = map_sysmem(CONFIG_VAL(PRE_CON_BUF_ADDR), CONFIG_VAL(PRE_CON_BUF_SZ));
+       if (gd->precon_buf_idx > CONFIG_VAL(PRE_CON_BUF_SZ))
+               in = gd->precon_buf_idx - CONFIG_VAL(PRE_CON_BUF_SZ);
 
        while (in < gd->precon_buf_idx)
                buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
@@ -623,6 +668,7 @@ static void print_pre_console_buffer(int flushpoint)
 
        buf_out[out] = 0;
 
+       gd->precon_buf_idx = -1;
        switch (flushpoint) {
        case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
                puts(buf_out);
@@ -631,6 +677,7 @@ static void print_pre_console_buffer(int flushpoint)
                console_puts_select(stdout, false, buf_out);
                break;
        }
+       gd->precon_buf_idx = in;
 }
 #else
 static inline void pre_console_putc(const char c) {}
@@ -723,13 +770,49 @@ void puts(const char *s)
        }
 }
 
+#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
+void flush(void)
+{
+       if (!gd)
+               return;
+
+       /* sandbox can send characters to stdout before it has a console */
+       if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
+               os_flush();
+               return;
+       }
+
+       if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY))
+               return;
+
+       if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT))
+               return;
+
+       if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
+               return;
+
+       if (!gd->have_console)
+               return;
+
+       if (gd->flags & GD_FLG_DEVINIT) {
+               /* Send to the standard output */
+               fflush(stdout);
+       } else {
+               /* Send directly to the handler */
+               serial_flush();
+       }
+}
+#endif
+
 #ifdef CONFIG_CONSOLE_RECORD
 int console_record_init(void)
 {
        int ret;
 
        ret = membuff_new((struct membuff *)&gd->console_out,
-                         CONFIG_CONSOLE_RECORD_OUT_SIZE);
+                         gd->flags & GD_FLG_RELOC ?
+                                 CONFIG_CONSOLE_RECORD_OUT_SIZE :
+                                 CONFIG_CONSOLE_RECORD_OUT_SIZE_F);
        if (ret)
                return ret;
        ret = membuff_new((struct membuff *)&gd->console_in,
@@ -742,6 +825,7 @@ void console_record_reset(void)
 {
        membuff_purge((struct membuff *)&gd->console_out);
        membuff_purge((struct membuff *)&gd->console_in);
+       gd->flags &= ~GD_FLG_RECORD_OVF;
 }
 
 int console_record_reset_enable(void)
@@ -754,6 +838,9 @@ int console_record_reset_enable(void)
 
 int console_record_readline(char *str, int maxlen)
 {
+       if (gd->flags & GD_FLG_RECORD_OVF)
+               return -ENOSPC;
+
        return membuff_readline((struct membuff *)&gd->console_out, str,
                                maxlen, ' ');
 }
@@ -763,6 +850,11 @@ int console_record_avail(void)
        return membuff_avail((struct membuff *)&gd->console_out);
 }
 
+int console_in_puts(const char *str)
+{
+       return membuff_put((struct membuff *)&gd->console_in, str, strlen(str));
+}
+
 #endif
 
 /* test if ctrl-c was pressed */
@@ -1072,17 +1164,13 @@ int console_init_r(void)
 
        /* Initializes output console first */
        if (outputdev != NULL) {
-               console_setfile(stdout, outputdev);
-               console_setfile(stderr, outputdev);
-               console_devices_set(stdout, outputdev);
-               console_devices_set(stderr, outputdev);
+               console_setfile_and_devices(stdout, outputdev);
+               console_setfile_and_devices(stderr, outputdev);
        }
 
        /* Initializes input console */
-       if (inputdev != NULL) {
-               console_setfile(stdin, inputdev);
-               console_devices_set(stdin, inputdev);
-       }
+       if (inputdev != NULL)
+               console_setfile_and_devices(stdin, inputdev);
 
        if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
                stdio_print_current_devices();