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