wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <stdarg.h> |
Jeroen Hofstee | 482f469 | 2014-10-08 22:57:48 +0200 | [diff] [blame] | 10 | #include <iomux.h> |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 11 | #include <malloc.h> |
Simon Glass | 91b136c | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 12 | #include <os.h> |
Joe Hershberger | 849d5d9 | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 13 | #include <serial.h> |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 14 | #include <stdio_dev.h> |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 15 | #include <exports.h> |
Joe Hershberger | 849d5d9 | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 16 | #include <environment.h> |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 17 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Joe Hershberger | 849d5d9 | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 20 | static int on_console(const char *name, const char *value, enum env_op op, |
| 21 | int flags) |
| 22 | { |
| 23 | int console = -1; |
| 24 | |
| 25 | /* Check for console redirection */ |
| 26 | if (strcmp(name, "stdin") == 0) |
| 27 | console = stdin; |
| 28 | else if (strcmp(name, "stdout") == 0) |
| 29 | console = stdout; |
| 30 | else if (strcmp(name, "stderr") == 0) |
| 31 | console = stderr; |
| 32 | |
| 33 | /* if not actually setting a console variable, we don't care */ |
| 34 | if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0) |
| 35 | return 0; |
| 36 | |
| 37 | switch (op) { |
| 38 | case env_op_create: |
| 39 | case env_op_overwrite: |
| 40 | |
| 41 | #ifdef CONFIG_CONSOLE_MUX |
| 42 | if (iomux_doenv(console, value)) |
| 43 | return 1; |
| 44 | #else |
| 45 | /* Try assigning specified device */ |
| 46 | if (console_assign(console, value) < 0) |
| 47 | return 1; |
| 48 | #endif /* CONFIG_CONSOLE_MUX */ |
| 49 | return 0; |
| 50 | |
| 51 | case env_op_delete: |
| 52 | if ((flags & H_FORCE) == 0) |
| 53 | printf("Can't delete \"%s\"\n", name); |
| 54 | return 1; |
| 55 | |
| 56 | default: |
| 57 | return 0; |
| 58 | } |
| 59 | } |
| 60 | U_BOOT_ENV_CALLBACK(console, on_console); |
| 61 | |
Joe Hershberger | e080d54 | 2012-12-11 22:16:30 -0600 | [diff] [blame] | 62 | #ifdef CONFIG_SILENT_CONSOLE |
| 63 | static int on_silent(const char *name, const char *value, enum env_op op, |
| 64 | int flags) |
| 65 | { |
| 66 | #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET |
| 67 | if (flags & H_INTERACTIVE) |
| 68 | return 0; |
| 69 | #endif |
| 70 | #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC |
| 71 | if ((flags & H_INTERACTIVE) == 0) |
| 72 | return 0; |
| 73 | #endif |
| 74 | |
| 75 | if (value != NULL) |
| 76 | gd->flags |= GD_FLG_SILENT; |
| 77 | else |
| 78 | gd->flags &= ~GD_FLG_SILENT; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | U_BOOT_ENV_CALLBACK(silent, on_silent); |
| 83 | #endif |
| 84 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 85 | #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 86 | /* |
| 87 | * if overwrite_console returns 1, the stdin, stderr and stdout |
| 88 | * are switched to the serial port, else the settings in the |
| 89 | * environment are used |
| 90 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 91 | #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 92 | extern int overwrite_console(void); |
| 93 | #define OVERWRITE_CONSOLE overwrite_console() |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 94 | #else |
wdenk | 83e40ba | 2005-03-31 18:42:15 +0000 | [diff] [blame] | 95 | #define OVERWRITE_CONSOLE 0 |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 97 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 98 | #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 99 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 100 | static int console_setfile(int file, struct stdio_dev * dev) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 101 | { |
| 102 | int error = 0; |
| 103 | |
| 104 | if (dev == NULL) |
| 105 | return -1; |
| 106 | |
| 107 | switch (file) { |
| 108 | case stdin: |
| 109 | case stdout: |
| 110 | case stderr: |
| 111 | /* Start new device */ |
| 112 | if (dev->start) { |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 113 | error = dev->start(dev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 114 | /* If it's not started dont use it */ |
| 115 | if (error < 0) |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | /* Assign the new device (leaving the existing one started) */ |
| 120 | stdio_devices[file] = dev; |
| 121 | |
| 122 | /* |
| 123 | * Update monitor functions |
| 124 | * (to use the console stuff by other applications) |
| 125 | */ |
| 126 | switch (file) { |
| 127 | case stdin: |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 128 | gd->jt[XF_getc] = dev->getc; |
| 129 | gd->jt[XF_tstc] = dev->tstc; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 130 | break; |
| 131 | case stdout: |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 132 | gd->jt[XF_putc] = dev->putc; |
| 133 | gd->jt[XF_puts] = dev->puts; |
| 134 | gd->jt[XF_printf] = printf; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | break; |
| 138 | |
| 139 | default: /* Invalid file ID */ |
| 140 | error = -1; |
| 141 | } |
| 142 | return error; |
| 143 | } |
| 144 | |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 145 | #if defined(CONFIG_CONSOLE_MUX) |
| 146 | /** Console I/O multiplexing *******************************************/ |
| 147 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 148 | static struct stdio_dev *tstcdev; |
| 149 | struct stdio_dev **console_devices[MAX_FILES]; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 150 | int cd_count[MAX_FILES]; |
| 151 | |
| 152 | /* |
| 153 | * This depends on tstc() always being called before getc(). |
| 154 | * This is guaranteed to be true because this routine is called |
| 155 | * only from fgetc() which assures it. |
| 156 | * No attempt is made to demultiplex multiple input sources. |
| 157 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 158 | static int console_getc(int file) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 159 | { |
| 160 | unsigned char ret; |
| 161 | |
| 162 | /* This is never called with testcdev == NULL */ |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 163 | ret = tstcdev->getc(tstcdev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 164 | tstcdev = NULL; |
| 165 | return ret; |
| 166 | } |
| 167 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 168 | static int console_tstc(int file) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 169 | { |
| 170 | int i, ret; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 171 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 172 | |
| 173 | disable_ctrlc(1); |
| 174 | for (i = 0; i < cd_count[file]; i++) { |
| 175 | dev = console_devices[file][i]; |
| 176 | if (dev->tstc != NULL) { |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 177 | ret = dev->tstc(dev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 178 | if (ret > 0) { |
| 179 | tstcdev = dev; |
| 180 | disable_ctrlc(0); |
| 181 | return ret; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | disable_ctrlc(0); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 190 | static void console_putc(int file, const char c) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 191 | { |
| 192 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 193 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 194 | |
| 195 | for (i = 0; i < cd_count[file]; i++) { |
| 196 | dev = console_devices[file][i]; |
| 197 | if (dev->putc != NULL) |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 198 | dev->putc(dev, c); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 202 | static void console_puts(int file, const char *s) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 203 | { |
| 204 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 205 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 206 | |
| 207 | for (i = 0; i < cd_count[file]; i++) { |
| 208 | dev = console_devices[file][i]; |
| 209 | if (dev->puts != NULL) |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 210 | dev->puts(dev, s); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 211 | } |
| 212 | } |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 213 | |
| 214 | static inline void console_printdevs(int file) |
| 215 | { |
| 216 | iomux_printdevs(file); |
| 217 | } |
| 218 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 219 | static inline void console_doenv(int file, struct stdio_dev *dev) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 220 | { |
| 221 | iomux_doenv(file, dev->name); |
| 222 | } |
| 223 | #else |
| 224 | static inline int console_getc(int file) |
| 225 | { |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 226 | return stdio_devices[file]->getc(stdio_devices[file]); |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static inline int console_tstc(int file) |
| 230 | { |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 231 | return stdio_devices[file]->tstc(stdio_devices[file]); |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static inline void console_putc(int file, const char c) |
| 235 | { |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 236 | stdio_devices[file]->putc(stdio_devices[file], c); |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static inline void console_puts(int file, const char *s) |
| 240 | { |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 241 | stdio_devices[file]->puts(stdio_devices[file], s); |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static inline void console_printdevs(int file) |
| 245 | { |
| 246 | printf("%s\n", stdio_devices[file]->name); |
| 247 | } |
| 248 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 249 | static inline void console_doenv(int file, struct stdio_dev *dev) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 250 | { |
| 251 | console_setfile(file, dev); |
| 252 | } |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 253 | #endif /* defined(CONFIG_CONSOLE_MUX) */ |
| 254 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 255 | /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/ |
| 256 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 257 | int serial_printf(const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 258 | { |
| 259 | va_list args; |
| 260 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 261 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 262 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 263 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 264 | |
| 265 | /* For this to work, printbuffer must be larger than |
| 266 | * anything we ever want to print. |
| 267 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 268 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 269 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 270 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 271 | serial_puts(printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 272 | return i; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 275 | int fgetc(int file) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 276 | { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 277 | if (file < MAX_FILES) { |
| 278 | #if defined(CONFIG_CONSOLE_MUX) |
| 279 | /* |
| 280 | * Effectively poll for input wherever it may be available. |
| 281 | */ |
| 282 | for (;;) { |
| 283 | /* |
| 284 | * Upper layer may have already called tstc() so |
| 285 | * check for that first. |
| 286 | */ |
| 287 | if (tstcdev != NULL) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 288 | return console_getc(file); |
| 289 | console_tstc(file); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 290 | #ifdef CONFIG_WATCHDOG |
| 291 | /* |
| 292 | * If the watchdog must be rate-limited then it should |
| 293 | * already be handled in board-specific code. |
| 294 | */ |
| 295 | udelay(1); |
| 296 | #endif |
| 297 | } |
| 298 | #else |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 299 | return console_getc(file); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 300 | #endif |
| 301 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 302 | |
| 303 | return -1; |
| 304 | } |
| 305 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 306 | int ftstc(int file) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 307 | { |
| 308 | if (file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 309 | return console_tstc(file); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 310 | |
| 311 | return -1; |
| 312 | } |
| 313 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 314 | void fputc(int file, const char c) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 315 | { |
| 316 | if (file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 317 | console_putc(file, c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 320 | void fputs(int file, const char *s) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 321 | { |
| 322 | if (file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 323 | console_puts(file, s); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 326 | int fprintf(int file, const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 327 | { |
| 328 | va_list args; |
| 329 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 330 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 331 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 332 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 333 | |
| 334 | /* For this to work, printbuffer must be larger than |
| 335 | * anything we ever want to print. |
| 336 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 337 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 338 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 339 | |
| 340 | /* Send to desired file */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 341 | fputs(file, printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 342 | return i; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/ |
| 346 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 347 | int getc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 348 | { |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 349 | #ifdef CONFIG_DISABLE_CONSOLE |
| 350 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 351 | return 0; |
| 352 | #endif |
| 353 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 354 | if (!gd->have_console) |
| 355 | return 0; |
| 356 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 357 | if (gd->flags & GD_FLG_DEVINIT) { |
| 358 | /* Get from the standard input */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 359 | return fgetc(stdin); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 363 | return serial_getc(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 366 | int tstc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 367 | { |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 368 | #ifdef CONFIG_DISABLE_CONSOLE |
| 369 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 370 | return 0; |
| 371 | #endif |
| 372 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 373 | if (!gd->have_console) |
| 374 | return 0; |
| 375 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 376 | if (gd->flags & GD_FLG_DEVINIT) { |
| 377 | /* Test the standard input */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 378 | return ftstc(stdin); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 382 | return serial_tstc(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Simon Glass | 3fa4977 | 2012-03-19 21:59:14 -0700 | [diff] [blame] | 385 | #ifdef CONFIG_PRE_CONSOLE_BUFFER |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 386 | #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ) |
| 387 | |
| 388 | static void pre_console_putc(const char c) |
| 389 | { |
| 390 | char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR; |
| 391 | |
| 392 | buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c; |
| 393 | } |
| 394 | |
| 395 | static void pre_console_puts(const char *s) |
| 396 | { |
| 397 | while (*s) |
| 398 | pre_console_putc(*s++); |
| 399 | } |
| 400 | |
| 401 | static void print_pre_console_buffer(void) |
| 402 | { |
| 403 | unsigned long i = 0; |
| 404 | char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR; |
| 405 | |
| 406 | if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ) |
| 407 | i = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ; |
| 408 | |
| 409 | while (i < gd->precon_buf_idx) |
| 410 | putc(buffer[CIRC_BUF_IDX(i++)]); |
| 411 | } |
| 412 | #else |
| 413 | static inline void pre_console_putc(const char c) {} |
| 414 | static inline void pre_console_puts(const char *s) {} |
| 415 | static inline void print_pre_console_buffer(void) {} |
| 416 | #endif |
| 417 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 418 | void putc(const char c) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 419 | { |
Simon Glass | 91b136c | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 420 | #ifdef CONFIG_SANDBOX |
Simon Glass | 093f79a | 2014-07-23 06:55:07 -0600 | [diff] [blame] | 421 | if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) { |
Simon Glass | 91b136c | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 422 | os_putc(c); |
| 423 | return; |
| 424 | } |
| 425 | #endif |
wdenk | a6cccae | 2004-02-06 21:48:22 +0000 | [diff] [blame] | 426 | #ifdef CONFIG_SILENT_CONSOLE |
| 427 | if (gd->flags & GD_FLG_SILENT) |
wdenk | f6e20fc | 2004-02-08 19:38:38 +0000 | [diff] [blame] | 428 | return; |
wdenk | a6cccae | 2004-02-06 21:48:22 +0000 | [diff] [blame] | 429 | #endif |
| 430 | |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 431 | #ifdef CONFIG_DISABLE_CONSOLE |
| 432 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 433 | return; |
| 434 | #endif |
| 435 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 436 | if (!gd->have_console) |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 437 | return pre_console_putc(c); |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 438 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 439 | if (gd->flags & GD_FLG_DEVINIT) { |
| 440 | /* Send to the standard output */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 441 | fputc(stdout, c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 442 | } else { |
| 443 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 444 | serial_putc(c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 448 | void puts(const char *s) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 449 | { |
Simon Glass | 91b136c | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 450 | #ifdef CONFIG_SANDBOX |
Simon Glass | 093f79a | 2014-07-23 06:55:07 -0600 | [diff] [blame] | 451 | if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) { |
Simon Glass | 91b136c | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 452 | os_puts(s); |
| 453 | return; |
| 454 | } |
| 455 | #endif |
| 456 | |
wdenk | a6cccae | 2004-02-06 21:48:22 +0000 | [diff] [blame] | 457 | #ifdef CONFIG_SILENT_CONSOLE |
| 458 | if (gd->flags & GD_FLG_SILENT) |
| 459 | return; |
| 460 | #endif |
| 461 | |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 462 | #ifdef CONFIG_DISABLE_CONSOLE |
| 463 | if (gd->flags & GD_FLG_DISABLE_CONSOLE) |
| 464 | return; |
| 465 | #endif |
| 466 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 467 | if (!gd->have_console) |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 468 | return pre_console_puts(s); |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 469 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 470 | if (gd->flags & GD_FLG_DEVINIT) { |
| 471 | /* Send to the standard output */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 472 | fputs(stdout, s); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 473 | } else { |
| 474 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 475 | serial_puts(s); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 479 | int printf(const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 480 | { |
| 481 | va_list args; |
| 482 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 483 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 484 | |
Simon Glass | 91b136c | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 485 | #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_PRE_CONSOLE_BUFFER) |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 486 | if (!gd->have_console) |
| 487 | return 0; |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 488 | #endif |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 489 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 490 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 491 | |
| 492 | /* For this to work, printbuffer must be larger than |
| 493 | * anything we ever want to print. |
| 494 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 495 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 496 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 497 | |
| 498 | /* Print the string */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 499 | puts(printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 500 | return i; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 503 | int vprintf(const char *fmt, va_list args) |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 504 | { |
| 505 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 506 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 507 | |
Simon Glass | 7793ac9 | 2014-07-23 06:55:06 -0600 | [diff] [blame] | 508 | #if defined(CONFIG_PRE_CONSOLE_BUFFER) && !defined(CONFIG_SANDBOX) |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 509 | if (!gd->have_console) |
| 510 | return 0; |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 511 | #endif |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 512 | |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 513 | /* For this to work, printbuffer must be larger than |
| 514 | * anything we ever want to print. |
| 515 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 516 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 517 | |
| 518 | /* Print the string */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 519 | puts(printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 520 | return i; |
wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 521 | } |
| 522 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 523 | /* test if ctrl-c was pressed */ |
| 524 | static int ctrlc_disabled = 0; /* see disable_ctrl() */ |
| 525 | static int ctrlc_was_pressed = 0; |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 526 | int ctrlc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 527 | { |
Simon Glass | 8969ea3 | 2014-09-14 12:40:15 -0600 | [diff] [blame] | 528 | #ifndef CONFIG_SANDBOX |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 529 | if (!ctrlc_disabled && gd->have_console) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 530 | if (tstc()) { |
| 531 | switch (getc()) { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 532 | case 0x03: /* ^C - Control C */ |
| 533 | ctrlc_was_pressed = 1; |
| 534 | return 1; |
| 535 | default: |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | } |
Simon Glass | 8969ea3 | 2014-09-14 12:40:15 -0600 | [diff] [blame] | 540 | #endif |
| 541 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 542 | return 0; |
| 543 | } |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 544 | /* Reads user's confirmation. |
| 545 | Returns 1 if user's input is "y", "Y", "yes" or "YES" |
| 546 | */ |
| 547 | int confirm_yesno(void) |
| 548 | { |
| 549 | int i; |
| 550 | char str_input[5]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 551 | |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 552 | /* Flush input */ |
| 553 | while (tstc()) |
| 554 | getc(); |
| 555 | i = 0; |
| 556 | while (i < sizeof(str_input)) { |
| 557 | str_input[i] = getc(); |
| 558 | putc(str_input[i]); |
| 559 | if (str_input[i] == '\r') |
| 560 | break; |
| 561 | i++; |
| 562 | } |
| 563 | putc('\n'); |
| 564 | if (strncmp(str_input, "y\r", 2) == 0 || |
| 565 | strncmp(str_input, "Y\r", 2) == 0 || |
| 566 | strncmp(str_input, "yes\r", 4) == 0 || |
| 567 | strncmp(str_input, "YES\r", 4) == 0) |
| 568 | return 1; |
| 569 | return 0; |
| 570 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 571 | /* pass 1 to disable ctrlc() checking, 0 to enable. |
| 572 | * returns previous state |
| 573 | */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 574 | int disable_ctrlc(int disable) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 575 | { |
| 576 | int prev = ctrlc_disabled; /* save previous state */ |
| 577 | |
| 578 | ctrlc_disabled = disable; |
| 579 | return prev; |
| 580 | } |
| 581 | |
| 582 | int had_ctrlc (void) |
| 583 | { |
| 584 | return ctrlc_was_pressed; |
| 585 | } |
| 586 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 587 | void clear_ctrlc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 588 | { |
| 589 | ctrlc_was_pressed = 0; |
| 590 | } |
| 591 | |
| 592 | #ifdef CONFIG_MODEM_SUPPORT_DEBUG |
| 593 | char screen[1024]; |
| 594 | char *cursor = screen; |
| 595 | int once = 0; |
| 596 | inline void dbg(const char *fmt, ...) |
| 597 | { |
| 598 | va_list args; |
| 599 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 600 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 601 | |
| 602 | if (!once) { |
| 603 | memset(screen, 0, sizeof(screen)); |
| 604 | once++; |
| 605 | } |
| 606 | |
| 607 | va_start(args, fmt); |
| 608 | |
| 609 | /* For this to work, printbuffer must be larger than |
| 610 | * anything we ever want to print. |
| 611 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 612 | i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 613 | va_end(args); |
| 614 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 615 | if ((screen + sizeof(screen) - 1 - cursor) |
| 616 | < strlen(printbuffer) + 1) { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 617 | memset(screen, 0, sizeof(screen)); |
| 618 | cursor = screen; |
| 619 | } |
| 620 | sprintf(cursor, printbuffer); |
| 621 | cursor += strlen(printbuffer); |
| 622 | |
| 623 | } |
| 624 | #else |
Jeroen Hofstee | 482f469 | 2014-10-08 22:57:48 +0200 | [diff] [blame] | 625 | static inline void dbg(const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 626 | { |
| 627 | } |
| 628 | #endif |
| 629 | |
| 630 | /** U-Boot INIT FUNCTIONS *************************************************/ |
| 631 | |
Mike Frysinger | d7be305 | 2010-10-20 07:18:03 -0400 | [diff] [blame] | 632 | struct stdio_dev *search_device(int flags, const char *name) |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 633 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 634 | struct stdio_dev *dev; |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 635 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 636 | dev = stdio_get_by_name(name); |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 637 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 638 | if (dev && (dev->flags & flags)) |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 639 | return dev; |
| 640 | |
| 641 | return NULL; |
| 642 | } |
| 643 | |
Mike Frysinger | d7be305 | 2010-10-20 07:18:03 -0400 | [diff] [blame] | 644 | int console_assign(int file, const char *devname) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 645 | { |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 646 | int flag; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 647 | struct stdio_dev *dev; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 648 | |
| 649 | /* Check for valid file */ |
| 650 | switch (file) { |
| 651 | case stdin: |
| 652 | flag = DEV_FLAGS_INPUT; |
| 653 | break; |
| 654 | case stdout: |
| 655 | case stderr: |
| 656 | flag = DEV_FLAGS_OUTPUT; |
| 657 | break; |
| 658 | default: |
| 659 | return -1; |
| 660 | } |
| 661 | |
| 662 | /* Check for valid device name */ |
| 663 | |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 664 | dev = search_device(flag, devname); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 665 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 666 | if (dev) |
| 667 | return console_setfile(file, dev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 668 | |
| 669 | return -1; |
| 670 | } |
| 671 | |
| 672 | /* Called before relocation - use serial functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 673 | int console_init_f(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 674 | { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 675 | gd->have_console = 1; |
wdenk | f72da34 | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 676 | |
| 677 | #ifdef CONFIG_SILENT_CONSOLE |
| 678 | if (getenv("silent") != NULL) |
| 679 | gd->flags |= GD_FLG_SILENT; |
| 680 | #endif |
| 681 | |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 682 | print_pre_console_buffer(); |
| 683 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 684 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 687 | void stdio_print_current_devices(void) |
| 688 | { |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 689 | /* Print information */ |
| 690 | puts("In: "); |
| 691 | if (stdio_devices[stdin] == NULL) { |
| 692 | puts("No input devices available!\n"); |
| 693 | } else { |
| 694 | printf ("%s\n", stdio_devices[stdin]->name); |
| 695 | } |
| 696 | |
| 697 | puts("Out: "); |
| 698 | if (stdio_devices[stdout] == NULL) { |
| 699 | puts("No output devices available!\n"); |
| 700 | } else { |
| 701 | printf ("%s\n", stdio_devices[stdout]->name); |
| 702 | } |
| 703 | |
| 704 | puts("Err: "); |
| 705 | if (stdio_devices[stderr] == NULL) { |
| 706 | puts("No error devices available!\n"); |
| 707 | } else { |
| 708 | printf ("%s\n", stdio_devices[stderr]->name); |
| 709 | } |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 712 | #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 713 | /* Called after the relocation - use desired console functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 714 | int console_init_r(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 715 | { |
| 716 | char *stdinname, *stdoutname, *stderrname; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 717 | struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 718 | #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 719 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 720 | #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */ |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 721 | #ifdef CONFIG_CONSOLE_MUX |
| 722 | int iomux_err = 0; |
| 723 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 724 | |
| 725 | /* set default handlers at first */ |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 726 | gd->jt[XF_getc] = serial_getc; |
| 727 | gd->jt[XF_tstc] = serial_tstc; |
| 728 | gd->jt[XF_putc] = serial_putc; |
| 729 | gd->jt[XF_puts] = serial_puts; |
| 730 | gd->jt[XF_printf] = serial_printf; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 731 | |
| 732 | /* stdin stdout and stderr are in environment */ |
| 733 | /* scan for it */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 734 | stdinname = getenv("stdin"); |
| 735 | stdoutname = getenv("stdout"); |
| 736 | stderrname = getenv("stderr"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 737 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 738 | if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 739 | inputdev = search_device(DEV_FLAGS_INPUT, stdinname); |
| 740 | outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname); |
| 741 | errdev = search_device(DEV_FLAGS_OUTPUT, stderrname); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 742 | #ifdef CONFIG_CONSOLE_MUX |
| 743 | iomux_err = iomux_doenv(stdin, stdinname); |
| 744 | iomux_err += iomux_doenv(stdout, stdoutname); |
| 745 | iomux_err += iomux_doenv(stderr, stderrname); |
| 746 | if (!iomux_err) |
| 747 | /* Successful, so skip all the code below. */ |
| 748 | goto done; |
| 749 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 750 | } |
| 751 | /* if the devices are overwritten or not found, use default device */ |
| 752 | if (inputdev == NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 753 | inputdev = search_device(DEV_FLAGS_INPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 754 | } |
| 755 | if (outputdev == NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 756 | outputdev = search_device(DEV_FLAGS_OUTPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 757 | } |
| 758 | if (errdev == NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 759 | errdev = search_device(DEV_FLAGS_OUTPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 760 | } |
| 761 | /* Initializes output console first */ |
| 762 | if (outputdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 763 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 764 | console_doenv(stdout, outputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 765 | } |
| 766 | if (errdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 767 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 768 | console_doenv(stderr, errdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 769 | } |
| 770 | if (inputdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 771 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 772 | console_doenv(stdin, inputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 775 | #ifdef CONFIG_CONSOLE_MUX |
| 776 | done: |
| 777 | #endif |
| 778 | |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 779 | #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 780 | stdio_print_current_devices(); |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 781 | #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 782 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 783 | #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 784 | /* set the environment variables (will overwrite previous env settings) */ |
| 785 | for (i = 0; i < 3; i++) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 786 | setenv(stdio_names[i], stdio_devices[i]->name); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 787 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 788 | #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 789 | |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 790 | gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */ |
| 791 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 792 | #if 0 |
| 793 | /* If nothing usable installed, use only the initial console */ |
| 794 | if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL)) |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 795 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 796 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 797 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 800 | #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 801 | |
| 802 | /* Called after the relocation - use desired console functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 803 | int console_init_r(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 804 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 805 | struct stdio_dev *inputdev = NULL, *outputdev = NULL; |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 806 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 807 | struct list_head *list = stdio_get_list(); |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 808 | struct list_head *pos; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 809 | struct stdio_dev *dev; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 810 | |
wdenk | d791b1d | 2003-04-20 14:04:18 +0000 | [diff] [blame] | 811 | #ifdef CONFIG_SPLASH_SCREEN |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 812 | /* |
| 813 | * suppress all output if splash screen is enabled and we have |
Anatolij Gustschin | a749081 | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 814 | * a bmp to display. We redirect the output from frame buffer |
| 815 | * console to serial console in this case or suppress it if |
| 816 | * "silent" mode was requested. |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 817 | */ |
Anatolij Gustschin | a749081 | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 818 | if (getenv("splashimage") != NULL) { |
| 819 | if (!(gd->flags & GD_FLG_SILENT)) |
| 820 | outputdev = search_device (DEV_FLAGS_OUTPUT, "serial"); |
| 821 | } |
wdenk | f72da34 | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 822 | #endif |
| 823 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 824 | /* Scan devices looking for input and output devices */ |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 825 | list_for_each(pos, list) { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 826 | dev = list_entry(pos, struct stdio_dev, list); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 827 | |
| 828 | if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) { |
| 829 | inputdev = dev; |
| 830 | } |
| 831 | if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) { |
| 832 | outputdev = dev; |
| 833 | } |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 834 | if(inputdev && outputdev) |
| 835 | break; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | /* Initializes output console first */ |
| 839 | if (outputdev != NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 840 | console_setfile(stdout, outputdev); |
| 841 | console_setfile(stderr, outputdev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 842 | #ifdef CONFIG_CONSOLE_MUX |
| 843 | console_devices[stdout][0] = outputdev; |
| 844 | console_devices[stderr][0] = outputdev; |
| 845 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | /* Initializes input console */ |
| 849 | if (inputdev != NULL) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 850 | console_setfile(stdin, inputdev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 851 | #ifdef CONFIG_CONSOLE_MUX |
| 852 | console_devices[stdin][0] = inputdev; |
| 853 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 856 | #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 857 | stdio_print_current_devices(); |
Simon Glass | 78c112c | 2012-12-05 14:46:43 +0000 | [diff] [blame] | 858 | #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 859 | |
| 860 | /* Setting environment variables */ |
| 861 | for (i = 0; i < 3; i++) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 862 | setenv(stdio_names[i], stdio_devices[i]->name); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 865 | gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */ |
| 866 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 867 | #if 0 |
| 868 | /* If nothing usable installed, use only the initial console */ |
| 869 | if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL)) |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 870 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 871 | #endif |
| 872 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 873 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 876 | #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */ |