Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000 |
| 4 | * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 5e392f1 | 2024-09-01 16:26:18 -0600 | [diff] [blame] | 7 | #define LOG_CATEGORY LOGC_CONSOLE |
| 8 | |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 9 | #include <console.h> |
Simon Glass | 7dd27e0 | 2015-06-23 15:38:33 -0600 | [diff] [blame] | 10 | #include <debug_uart.h> |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 11 | #include <display_options.h> |
Simon Glass | 10a7fe9 | 2017-07-27 09:31:04 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 13 | #include <env.h> |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 14 | #include <stdarg.h> |
Jeroen Hofstee | 3b46b3b | 2014-10-08 22:57:48 +0200 | [diff] [blame] | 15 | #include <iomux.h> |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 16 | #include <malloc.h> |
Simon Glass | 46aaad0 | 2017-06-15 21:37:52 -0600 | [diff] [blame] | 17 | #include <mapmem.h> |
Simon Glass | 3e9fd24 | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 18 | #include <os.h> |
Joe Hershberger | 5c89d6d | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 19 | #include <serial.h> |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 20 | #include <stdio_dev.h> |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 21 | #include <exports.h> |
Simon Glass | 9d1f619 | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 22 | #include <env_internal.h> |
Simon Glass | ffafbe3 | 2023-10-01 19:15:23 -0600 | [diff] [blame] | 23 | #include <video_console.h> |
Andreas J. Reichel | 982ca46 | 2016-07-13 12:56:51 +0200 | [diff] [blame] | 24 | #include <watchdog.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 25 | #include <asm/global_data.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 26 | #include <linux/delay.h> |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 27 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Simon Glass | ffafbe3 | 2023-10-01 19:15:23 -0600 | [diff] [blame] | 30 | #define CSI "\x1b[" |
| 31 | |
Joe Hershberger | 5c89d6d | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 32 | static int on_console(const char *name, const char *value, enum env_op op, |
| 33 | int flags) |
| 34 | { |
| 35 | int console = -1; |
| 36 | |
| 37 | /* Check for console redirection */ |
| 38 | if (strcmp(name, "stdin") == 0) |
| 39 | console = stdin; |
| 40 | else if (strcmp(name, "stdout") == 0) |
| 41 | console = stdout; |
| 42 | else if (strcmp(name, "stderr") == 0) |
| 43 | console = stderr; |
| 44 | |
| 45 | /* if not actually setting a console variable, we don't care */ |
| 46 | if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0) |
| 47 | return 0; |
| 48 | |
| 49 | switch (op) { |
| 50 | case env_op_create: |
| 51 | case env_op_overwrite: |
| 52 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 53 | if (CONFIG_IS_ENABLED(CONSOLE_MUX)) { |
| 54 | if (iomux_doenv(console, value)) |
| 55 | return 1; |
| 56 | } else { |
| 57 | /* Try assigning specified device */ |
| 58 | if (console_assign(console, value) < 0) |
| 59 | return 1; |
| 60 | } |
| 61 | |
Joe Hershberger | 5c89d6d | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 62 | return 0; |
| 63 | |
| 64 | case env_op_delete: |
| 65 | if ((flags & H_FORCE) == 0) |
| 66 | printf("Can't delete \"%s\"\n", name); |
| 67 | return 1; |
| 68 | |
| 69 | default: |
| 70 | return 0; |
| 71 | } |
| 72 | } |
| 73 | U_BOOT_ENV_CALLBACK(console, on_console); |
| 74 | |
Joe Hershberger | 03ddaba | 2012-12-11 22:16:30 -0600 | [diff] [blame] | 75 | #ifdef CONFIG_SILENT_CONSOLE |
| 76 | static int on_silent(const char *name, const char *value, enum env_op op, |
| 77 | int flags) |
| 78 | { |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 79 | if (!CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_SET)) |
| 80 | if (flags & H_INTERACTIVE) |
| 81 | return 0; |
| 82 | |
| 83 | if (!CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_RELOC)) |
| 84 | if ((flags & H_INTERACTIVE) == 0) |
| 85 | return 0; |
Joe Hershberger | 03ddaba | 2012-12-11 22:16:30 -0600 | [diff] [blame] | 86 | |
| 87 | if (value != NULL) |
| 88 | gd->flags |= GD_FLG_SILENT; |
| 89 | else |
| 90 | gd->flags &= ~GD_FLG_SILENT; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | U_BOOT_ENV_CALLBACK(silent, on_silent); |
| 95 | #endif |
| 96 | |
Patrick Delaunay | c20847f | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 97 | #ifdef CONFIG_CONSOLE_RECORD |
| 98 | /* helper function: access to gd->console_out and gd->console_in */ |
| 99 | static void console_record_putc(const char c) |
| 100 | { |
| 101 | if (!(gd->flags & GD_FLG_RECORD)) |
| 102 | return; |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 103 | if (gd->console_out.start && |
| 104 | !membuff_putbyte((struct membuff *)&gd->console_out, c)) |
| 105 | gd->flags |= GD_FLG_RECORD_OVF; |
Patrick Delaunay | c20847f | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void console_record_puts(const char *s) |
| 109 | { |
| 110 | if (!(gd->flags & GD_FLG_RECORD)) |
| 111 | return; |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 112 | if (gd->console_out.start) { |
| 113 | int len = strlen(s); |
| 114 | |
| 115 | if (membuff_put((struct membuff *)&gd->console_out, s, len) != |
| 116 | len) |
| 117 | gd->flags |= GD_FLG_RECORD_OVF; |
| 118 | } |
Patrick Delaunay | c20847f | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static int console_record_getc(void) |
| 122 | { |
| 123 | if (!(gd->flags & GD_FLG_RECORD)) |
| 124 | return -1; |
| 125 | if (!gd->console_in.start) |
| 126 | return -1; |
| 127 | |
| 128 | return membuff_getbyte((struct membuff *)&gd->console_in); |
| 129 | } |
| 130 | |
| 131 | static int console_record_tstc(void) |
| 132 | { |
| 133 | if (!(gd->flags & GD_FLG_RECORD)) |
| 134 | return 0; |
| 135 | if (gd->console_in.start) { |
| 136 | if (membuff_peekbyte((struct membuff *)&gd->console_in) != -1) |
| 137 | return 1; |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | #else |
| 142 | static void console_record_putc(char c) |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | static void console_record_puts(const char *s) |
| 147 | { |
| 148 | } |
| 149 | |
| 150 | static int console_record_getc(void) |
| 151 | { |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | static int console_record_tstc(void) |
| 156 | { |
| 157 | return 0; |
| 158 | } |
| 159 | #endif |
| 160 | |
Simon Glass | dfc2535 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 161 | #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 162 | /* |
| 163 | * if overwrite_console returns 1, the stdin, stderr and stdout |
| 164 | * are switched to the serial port, else the settings in the |
| 165 | * environment are used |
| 166 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 167 | #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 168 | extern int overwrite_console(void); |
| 169 | #define OVERWRITE_CONSOLE overwrite_console() |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 170 | #else |
wdenk | 61c636e | 2005-03-31 18:42:15 +0000 | [diff] [blame] | 171 | #define OVERWRITE_CONSOLE 0 |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 172 | #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 173 | |
Simon Glass | dfc2535 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 174 | #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 175 | |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 176 | static int console_setfile(int file, struct stdio_dev * dev) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 177 | { |
| 178 | int error = 0; |
| 179 | |
| 180 | if (dev == NULL) |
| 181 | return -1; |
| 182 | |
| 183 | switch (file) { |
| 184 | case stdin: |
| 185 | case stdout: |
| 186 | case stderr: |
Andy Shevchenko | 2bce021 | 2020-12-21 14:30:00 +0200 | [diff] [blame] | 187 | error = console_start(file, dev); |
| 188 | if (error) |
| 189 | break; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 190 | |
| 191 | /* Assign the new device (leaving the existing one started) */ |
| 192 | stdio_devices[file] = dev; |
| 193 | |
Simon Glass | 0e84d96 | 2024-09-29 19:49:50 -0600 | [diff] [blame] | 194 | #ifndef CONFIG_XPL_BUILD |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 195 | /* |
| 196 | * Update monitor functions |
| 197 | * (to use the console stuff by other applications) |
| 198 | */ |
| 199 | switch (file) { |
| 200 | case stdin: |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 201 | gd->jt->getc = getchar; |
Martin Dorwig | cb2c286 | 2015-01-26 15:22:54 -0700 | [diff] [blame] | 202 | gd->jt->tstc = tstc; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 203 | break; |
| 204 | case stdout: |
Martin Dorwig | cb2c286 | 2015-01-26 15:22:54 -0700 | [diff] [blame] | 205 | gd->jt->putc = putc; |
| 206 | gd->jt->puts = puts; |
Pali Rohár | 5aceb26 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 207 | STDIO_DEV_ASSIGN_FLUSH(gd->jt, flush); |
Martin Dorwig | cb2c286 | 2015-01-26 15:22:54 -0700 | [diff] [blame] | 208 | gd->jt->printf = printf; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 209 | break; |
| 210 | } |
| 211 | break; |
Simon Glass | 8a0417f | 2024-08-21 10:19:24 -0600 | [diff] [blame] | 212 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 213 | default: /* Invalid file ID */ |
| 214 | error = -1; |
| 215 | } |
| 216 | return error; |
| 217 | } |
| 218 | |
Simon Glass | 045e4b5 | 2017-07-27 09:31:03 -0600 | [diff] [blame] | 219 | /** |
| 220 | * console_dev_is_serial() - Check if a stdio device is a serial device |
| 221 | * |
| 222 | * @sdev: Device to check |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 223 | * Return: true if this device is in the serial uclass (or for pre-driver-model, |
Simon Glass | 10a7fe9 | 2017-07-27 09:31:04 -0600 | [diff] [blame] | 224 | * whether it is called "serial". |
Simon Glass | 045e4b5 | 2017-07-27 09:31:03 -0600 | [diff] [blame] | 225 | */ |
| 226 | static bool console_dev_is_serial(struct stdio_dev *sdev) |
| 227 | { |
| 228 | bool is_serial; |
| 229 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 230 | if (IS_ENABLED(CONFIG_DM_SERIAL) && (sdev->flags & DEV_FLAGS_DM)) { |
Simon Glass | 10a7fe9 | 2017-07-27 09:31:04 -0600 | [diff] [blame] | 231 | struct udevice *dev = sdev->priv; |
| 232 | |
| 233 | is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL; |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 234 | } else { |
| 235 | is_serial = !strcmp(sdev->name, "serial"); |
| 236 | } |
Simon Glass | 045e4b5 | 2017-07-27 09:31:03 -0600 | [diff] [blame] | 237 | |
| 238 | return is_serial; |
| 239 | } |
| 240 | |
Simon Glass | dfc2535 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 241 | #if CONFIG_IS_ENABLED(CONSOLE_MUX) |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 242 | /** Console I/O multiplexing *******************************************/ |
| 243 | |
Patrick Delaunay | 4e8d61f | 2020-12-18 12:46:46 +0100 | [diff] [blame] | 244 | /* tstcdev: save the last stdio device with pending characters, with tstc != 0 */ |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 245 | static struct stdio_dev *tstcdev; |
| 246 | struct stdio_dev **console_devices[MAX_FILES]; |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 247 | int cd_count[MAX_FILES]; |
| 248 | |
Andy Shevchenko | ba69b2c | 2021-02-11 17:09:39 +0200 | [diff] [blame] | 249 | static void console_devices_set(int file, struct stdio_dev *dev) |
Patrick Delaunay | 7c5f6ed | 2020-12-18 12:46:44 +0100 | [diff] [blame] | 250 | { |
| 251 | console_devices[file][0] = dev; |
Andy Shevchenko | f1a852a | 2021-02-11 17:09:38 +0200 | [diff] [blame] | 252 | cd_count[file] = 1; |
Patrick Delaunay | 7c5f6ed | 2020-12-18 12:46:44 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Andy Shevchenko | bdb0d4c | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 255 | /** |
| 256 | * console_needs_start_stop() - check if we need to start or stop the STDIO device |
| 257 | * @file: STDIO file |
| 258 | * @sdev: STDIO device in question |
| 259 | * |
| 260 | * This function checks if we need to start or stop the stdio device used for |
| 261 | * a console. For IOMUX case it simply enforces one time start and one time |
| 262 | * stop of the device independently of how many STDIO files are using it. In |
| 263 | * other words, we start console once before first STDIO device wants it and |
| 264 | * stop after the last is gone. |
| 265 | */ |
| 266 | static bool console_needs_start_stop(int file, struct stdio_dev *sdev) |
| 267 | { |
Andy Shevchenko | 75039f5 | 2021-02-11 17:09:41 +0200 | [diff] [blame] | 268 | int i; |
Andy Shevchenko | bdb0d4c | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 269 | |
| 270 | for (i = 0; i < ARRAY_SIZE(cd_count); i++) { |
| 271 | if (i == file) |
| 272 | continue; |
| 273 | |
Andy Shevchenko | 75039f5 | 2021-02-11 17:09:41 +0200 | [diff] [blame] | 274 | if (iomux_match_device(console_devices[i], cd_count[i], sdev) >= 0) |
| 275 | return false; |
Andy Shevchenko | bdb0d4c | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 276 | } |
| 277 | return true; |
| 278 | } |
| 279 | |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 280 | /* |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 281 | * This depends on tstc() always being called before getchar(). |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 282 | * This is guaranteed to be true because this routine is called |
| 283 | * only from fgetc() which assures it. |
| 284 | * No attempt is made to demultiplex multiple input sources. |
| 285 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 286 | static int console_getc(int file) |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 287 | { |
| 288 | unsigned char ret; |
| 289 | |
| 290 | /* This is never called with testcdev == NULL */ |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 291 | ret = tstcdev->getc(tstcdev); |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 292 | tstcdev = NULL; |
| 293 | return ret; |
| 294 | } |
| 295 | |
Patrick Delaunay | 4e8d61f | 2020-12-18 12:46:46 +0100 | [diff] [blame] | 296 | /* Upper layer may have already called tstc(): check the saved result */ |
| 297 | static bool console_has_tstc(void) |
| 298 | { |
| 299 | return !!tstcdev; |
| 300 | } |
| 301 | |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 302 | static int console_tstc(int file) |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 303 | { |
| 304 | int i, ret; |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 305 | struct stdio_dev *dev; |
Joe Hershberger | c2f3954 | 2018-07-02 20:06:48 -0500 | [diff] [blame] | 306 | int prev; |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 307 | |
Joe Hershberger | c2f3954 | 2018-07-02 20:06:48 -0500 | [diff] [blame] | 308 | prev = disable_ctrlc(1); |
Andy Shevchenko | e29d019 | 2021-02-11 17:09:42 +0200 | [diff] [blame] | 309 | for_each_console_dev(i, file, dev) { |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 310 | if (dev->tstc != NULL) { |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 311 | ret = dev->tstc(dev); |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 312 | if (ret > 0) { |
| 313 | tstcdev = dev; |
Joe Hershberger | c2f3954 | 2018-07-02 20:06:48 -0500 | [diff] [blame] | 314 | disable_ctrlc(prev); |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 315 | return ret; |
| 316 | } |
| 317 | } |
| 318 | } |
Joe Hershberger | c2f3954 | 2018-07-02 20:06:48 -0500 | [diff] [blame] | 319 | disable_ctrlc(prev); |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 324 | static void console_putc(int file, const char c) |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 325 | { |
| 326 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 327 | struct stdio_dev *dev; |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 328 | |
Andy Shevchenko | e29d019 | 2021-02-11 17:09:42 +0200 | [diff] [blame] | 329 | for_each_console_dev(i, file, dev) { |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 330 | if (dev->putc != NULL) |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 331 | dev->putc(dev, c); |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Simon Glass | 66ee817 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 335 | /** |
| 336 | * console_puts_select() - Output a string to all console devices |
| 337 | * |
| 338 | * @file: File number to output to (e,g, stdout, see stdio.h) |
| 339 | * @serial_only: true to output only to serial, false to output to everything |
| 340 | * else |
| 341 | * @s: String to output |
| 342 | */ |
| 343 | static void console_puts_select(int file, bool serial_only, const char *s) |
Siarhei Siamashka | 616d03e | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 344 | { |
| 345 | int i; |
| 346 | struct stdio_dev *dev; |
| 347 | |
Andy Shevchenko | e29d019 | 2021-02-11 17:09:42 +0200 | [diff] [blame] | 348 | for_each_console_dev(i, file, dev) { |
| 349 | bool is_serial = console_dev_is_serial(dev); |
Simon Glass | 66ee817 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 350 | |
Simon Glass | 66ee817 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 351 | if (dev->puts && serial_only == is_serial) |
Hans de Goede | e355da0 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 352 | dev->puts(dev, s); |
Siarhei Siamashka | 616d03e | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 353 | } |
| 354 | } |
Siarhei Siamashka | 616d03e | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 355 | |
Simon Glass | 66ee817 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 356 | void console_puts_select_stderr(bool serial_only, const char *s) |
| 357 | { |
Simon Glass | c7ba6c8 | 2021-11-19 13:23:47 -0700 | [diff] [blame] | 358 | if (gd->flags & GD_FLG_DEVINIT) |
| 359 | console_puts_select(stderr, serial_only, s); |
Simon Glass | 66ee817 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 360 | } |
| 361 | |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 362 | static void console_puts(int file, const char *s) |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 363 | { |
| 364 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 365 | struct stdio_dev *dev; |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 366 | |
Andy Shevchenko | e29d019 | 2021-02-11 17:09:42 +0200 | [diff] [blame] | 367 | for_each_console_dev(i, file, dev) { |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 368 | if (dev->puts != NULL) |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 369 | dev->puts(dev, s); |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 370 | } |
| 371 | } |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 372 | |
Pali Rohár | 5aceb26 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 373 | #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT |
| 374 | static void console_flush(int file) |
| 375 | { |
| 376 | int i; |
| 377 | struct stdio_dev *dev; |
| 378 | |
| 379 | for_each_console_dev(i, file, dev) { |
| 380 | if (dev->flush != NULL) |
| 381 | dev->flush(dev); |
| 382 | } |
| 383 | } |
| 384 | #endif |
| 385 | |
Tom Rini | d30aeb1 | 2019-10-30 09:18:43 -0400 | [diff] [blame] | 386 | #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 387 | 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] | 388 | { |
| 389 | iomux_doenv(file, dev->name); |
| 390 | } |
Tom Rini | d30aeb1 | 2019-10-30 09:18:43 -0400 | [diff] [blame] | 391 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 392 | #else |
Patrick Delaunay | 7c5f6ed | 2020-12-18 12:46:44 +0100 | [diff] [blame] | 393 | |
Andy Shevchenko | ba69b2c | 2021-02-11 17:09:39 +0200 | [diff] [blame] | 394 | static void console_devices_set(int file, struct stdio_dev *dev) |
Patrick Delaunay | 7c5f6ed | 2020-12-18 12:46:44 +0100 | [diff] [blame] | 395 | { |
| 396 | } |
| 397 | |
Andy Shevchenko | bdb0d4c | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 398 | static inline bool console_needs_start_stop(int file, struct stdio_dev *sdev) |
| 399 | { |
| 400 | return true; |
| 401 | } |
| 402 | |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 403 | static inline int console_getc(int file) |
| 404 | { |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 405 | return stdio_devices[file]->getc(stdio_devices[file]); |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Patrick Delaunay | 4e8d61f | 2020-12-18 12:46:46 +0100 | [diff] [blame] | 408 | static bool console_has_tstc(void) |
| 409 | { |
| 410 | return false; |
| 411 | } |
| 412 | |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 413 | static inline int console_tstc(int file) |
| 414 | { |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 415 | return stdio_devices[file]->tstc(stdio_devices[file]); |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static inline void console_putc(int file, const char c) |
| 419 | { |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 420 | stdio_devices[file]->putc(stdio_devices[file], c); |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Simon Glass | 66ee817 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 423 | void console_puts_select(int file, bool serial_only, const char *s) |
Siarhei Siamashka | 616d03e | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 424 | { |
Simon Glass | c7ba6c8 | 2021-11-19 13:23:47 -0700 | [diff] [blame] | 425 | if ((gd->flags & GD_FLG_DEVINIT) && |
| 426 | serial_only == console_dev_is_serial(stdio_devices[file])) |
Hans de Goede | e355da0 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 427 | stdio_devices[file]->puts(stdio_devices[file], s); |
Siarhei Siamashka | 616d03e | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 428 | } |
Siarhei Siamashka | 616d03e | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 429 | |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 430 | static inline void console_puts(int file, const char *s) |
| 431 | { |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 432 | stdio_devices[file]->puts(stdio_devices[file], s); |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 433 | } |
Pali Rohár | 5aceb26 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 434 | |
| 435 | #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT |
| 436 | static inline void console_flush(int file) |
| 437 | { |
| 438 | if (stdio_devices[file]->flush) |
| 439 | stdio_devices[file]->flush(stdio_devices[file]); |
| 440 | } |
| 441 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 442 | |
Tom Rini | d30aeb1 | 2019-10-30 09:18:43 -0400 | [diff] [blame] | 443 | #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 444 | 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] | 445 | { |
| 446 | console_setfile(file, dev); |
| 447 | } |
Tom Rini | d30aeb1 | 2019-10-30 09:18:43 -0400 | [diff] [blame] | 448 | #endif |
Simon Glass | dfc2535 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 449 | #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */ |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 450 | |
Andy Shevchenko | ba69b2c | 2021-02-11 17:09:39 +0200 | [diff] [blame] | 451 | static void __maybe_unused console_setfile_and_devices(int file, struct stdio_dev *dev) |
| 452 | { |
| 453 | console_setfile(file, dev); |
| 454 | console_devices_set(file, dev); |
| 455 | } |
| 456 | |
Andy Shevchenko | 2bce021 | 2020-12-21 14:30:00 +0200 | [diff] [blame] | 457 | int console_start(int file, struct stdio_dev *sdev) |
| 458 | { |
| 459 | int error; |
| 460 | |
Andy Shevchenko | bdb0d4c | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 461 | if (!console_needs_start_stop(file, sdev)) |
| 462 | return 0; |
| 463 | |
Andy Shevchenko | 2bce021 | 2020-12-21 14:30:00 +0200 | [diff] [blame] | 464 | /* Start new device */ |
| 465 | if (sdev->start) { |
| 466 | error = sdev->start(sdev); |
| 467 | /* If it's not started don't use it */ |
| 468 | if (error < 0) |
| 469 | return error; |
| 470 | } |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | void console_stop(int file, struct stdio_dev *sdev) |
| 475 | { |
Andy Shevchenko | bdb0d4c | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 476 | if (!console_needs_start_stop(file, sdev)) |
| 477 | return; |
| 478 | |
Andy Shevchenko | 2bce021 | 2020-12-21 14:30:00 +0200 | [diff] [blame] | 479 | if (sdev->stop) |
| 480 | sdev->stop(sdev); |
| 481 | } |
| 482 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 483 | /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/ |
| 484 | |
Wolfgang Denk | 318ef5c | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 485 | int serial_printf(const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 486 | { |
| 487 | va_list args; |
| 488 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 489 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 490 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 491 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 492 | |
| 493 | /* For this to work, printbuffer must be larger than |
| 494 | * anything we ever want to print. |
| 495 | */ |
Sonny Rao | 2813a21 | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 496 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 497 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 498 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 499 | serial_puts(printbuffer); |
Wolfgang Denk | 318ef5c | 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 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 503 | int fgetc(int file) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 504 | { |
Heinrich Schuchardt | 1bc421b | 2022-10-22 11:32:34 +0200 | [diff] [blame] | 505 | if ((unsigned int)file < MAX_FILES) { |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 506 | /* |
| 507 | * Effectively poll for input wherever it may be available. |
| 508 | */ |
| 509 | for (;;) { |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 510 | schedule(); |
Patrick Delaunay | 4e8d61f | 2020-12-18 12:46:46 +0100 | [diff] [blame] | 511 | if (CONFIG_IS_ENABLED(CONSOLE_MUX)) { |
| 512 | /* |
| 513 | * Upper layer may have already called tstc() so |
| 514 | * check for that first. |
| 515 | */ |
| 516 | if (console_has_tstc()) |
| 517 | return console_getc(file); |
| 518 | console_tstc(file); |
| 519 | } else { |
| 520 | if (console_tstc(file)) |
| 521 | return console_getc(file); |
| 522 | } |
| 523 | |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 524 | /* |
| 525 | * If the watchdog must be rate-limited then it should |
| 526 | * already be handled in board-specific code. |
| 527 | */ |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 528 | if (IS_ENABLED(CONFIG_WATCHDOG)) |
| 529 | udelay(1); |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 530 | } |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 531 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 532 | |
| 533 | return -1; |
| 534 | } |
| 535 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 536 | int ftstc(int file) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 537 | { |
Heinrich Schuchardt | 1bc421b | 2022-10-22 11:32:34 +0200 | [diff] [blame] | 538 | if ((unsigned int)file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 539 | return console_tstc(file); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 540 | |
| 541 | return -1; |
| 542 | } |
| 543 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 544 | void fputc(int file, const char c) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 545 | { |
Heinrich Schuchardt | 1bc421b | 2022-10-22 11:32:34 +0200 | [diff] [blame] | 546 | if ((unsigned int)file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 547 | console_putc(file, c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 550 | void fputs(int file, const char *s) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 551 | { |
Heinrich Schuchardt | 1bc421b | 2022-10-22 11:32:34 +0200 | [diff] [blame] | 552 | if ((unsigned int)file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 553 | console_puts(file, s); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Pali Rohár | 5aceb26 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 556 | #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT |
| 557 | void fflush(int file) |
| 558 | { |
Heinrich Schuchardt | 1bc421b | 2022-10-22 11:32:34 +0200 | [diff] [blame] | 559 | if ((unsigned int)file < MAX_FILES) |
Pali Rohár | 5aceb26 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 560 | console_flush(file); |
| 561 | } |
| 562 | #endif |
| 563 | |
Wolfgang Denk | 318ef5c | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 564 | int fprintf(int file, const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 565 | { |
| 566 | va_list args; |
| 567 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 568 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 569 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 570 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 571 | |
| 572 | /* For this to work, printbuffer must be larger than |
| 573 | * anything we ever want to print. |
| 574 | */ |
Sonny Rao | 2813a21 | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 575 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 576 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 577 | |
| 578 | /* Send to desired file */ |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 579 | fputs(file, printbuffer); |
Wolfgang Denk | 318ef5c | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 580 | return i; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/ |
| 584 | |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 585 | int getchar(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 586 | { |
Patrick Delaunay | c20847f | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 587 | int ch; |
| 588 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 589 | if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE)) |
Mark Jackson | 5de5621 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 590 | return 0; |
Mark Jackson | 5de5621 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 591 | |
Simon Glass | d4b0fdb | 2024-08-21 10:19:04 -0600 | [diff] [blame] | 592 | if (!(gd->flags & GD_FLG_HAVE_CONSOLE)) |
Graeme Russ | 70600b0 | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 593 | return 0; |
| 594 | |
Patrick Delaunay | c20847f | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 595 | ch = console_record_getc(); |
| 596 | if (ch != -1) |
| 597 | return ch; |
Simon Glass | 1bb4923 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 598 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 599 | if (gd->flags & GD_FLG_DEVINIT) { |
| 600 | /* Get from the standard input */ |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 601 | return fgetc(stdin); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 605 | return serial_getc(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 608 | int tstc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 609 | { |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 610 | if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE)) |
Mark Jackson | 5de5621 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 611 | return 0; |
Mark Jackson | 5de5621 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 612 | |
Simon Glass | d4b0fdb | 2024-08-21 10:19:04 -0600 | [diff] [blame] | 613 | if (!(gd->flags & GD_FLG_HAVE_CONSOLE)) |
Graeme Russ | 70600b0 | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 614 | return 0; |
Patrick Delaunay | c20847f | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 615 | |
| 616 | if (console_record_tstc()) |
| 617 | return 1; |
| 618 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 619 | if (gd->flags & GD_FLG_DEVINIT) { |
| 620 | /* Test the standard input */ |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 621 | return ftstc(stdin); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 625 | return serial_tstc(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Siarhei Siamashka | 616d03e | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 628 | #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0 |
| 629 | #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1 |
| 630 | |
Simon Glass | e304a5e | 2016-10-17 20:12:36 -0600 | [diff] [blame] | 631 | #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER) |
Rasmus Villemoes | fb35fff | 2022-05-03 14:37:39 +0200 | [diff] [blame] | 632 | #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_VAL(PRE_CON_BUF_SZ)) |
Graeme Russ | 3c28f48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 633 | |
| 634 | static void pre_console_putc(const char c) |
| 635 | { |
Simon Glass | 46aaad0 | 2017-06-15 21:37:52 -0600 | [diff] [blame] | 636 | char *buffer; |
| 637 | |
Rasmus Villemoes | e406a61 | 2022-05-03 15:13:27 +0200 | [diff] [blame] | 638 | if (gd->precon_buf_idx < 0) |
| 639 | return; |
| 640 | |
Rasmus Villemoes | fb35fff | 2022-05-03 14:37:39 +0200 | [diff] [blame] | 641 | buffer = map_sysmem(CONFIG_VAL(PRE_CON_BUF_ADDR), CONFIG_VAL(PRE_CON_BUF_SZ)); |
Graeme Russ | 3c28f48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 642 | |
| 643 | buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c; |
Simon Glass | 46aaad0 | 2017-06-15 21:37:52 -0600 | [diff] [blame] | 644 | |
| 645 | unmap_sysmem(buffer); |
Graeme Russ | 3c28f48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Soeren Moch | 54619bb | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 648 | static void pre_console_puts(const char *s) |
| 649 | { |
Rasmus Villemoes | e406a61 | 2022-05-03 15:13:27 +0200 | [diff] [blame] | 650 | if (gd->precon_buf_idx < 0) |
| 651 | return; |
| 652 | |
Soeren Moch | 54619bb | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 653 | while (*s) |
| 654 | pre_console_putc(*s++); |
| 655 | } |
| 656 | |
Siarhei Siamashka | 616d03e | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 657 | static void print_pre_console_buffer(int flushpoint) |
Graeme Russ | 3c28f48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 658 | { |
Rasmus Villemoes | e406a61 | 2022-05-03 15:13:27 +0200 | [diff] [blame] | 659 | long in = 0, out = 0; |
Rasmus Villemoes | fb35fff | 2022-05-03 14:37:39 +0200 | [diff] [blame] | 660 | char buf_out[CONFIG_VAL(PRE_CON_BUF_SZ) + 1]; |
Simon Glass | 46aaad0 | 2017-06-15 21:37:52 -0600 | [diff] [blame] | 661 | char *buf_in; |
Graeme Russ | 3c28f48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 662 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 663 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 664 | return; |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 665 | |
Rasmus Villemoes | fb35fff | 2022-05-03 14:37:39 +0200 | [diff] [blame] | 666 | buf_in = map_sysmem(CONFIG_VAL(PRE_CON_BUF_ADDR), CONFIG_VAL(PRE_CON_BUF_SZ)); |
| 667 | if (gd->precon_buf_idx > CONFIG_VAL(PRE_CON_BUF_SZ)) |
| 668 | in = gd->precon_buf_idx - CONFIG_VAL(PRE_CON_BUF_SZ); |
Graeme Russ | 3c28f48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 669 | |
Hans de Goede | e355da0 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 670 | while (in < gd->precon_buf_idx) |
| 671 | buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)]; |
Simon Glass | 46aaad0 | 2017-06-15 21:37:52 -0600 | [diff] [blame] | 672 | unmap_sysmem(buf_in); |
Hans de Goede | e355da0 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 673 | |
| 674 | buf_out[out] = 0; |
| 675 | |
Rasmus Villemoes | e406a61 | 2022-05-03 15:13:27 +0200 | [diff] [blame] | 676 | gd->precon_buf_idx = -1; |
Hans de Goede | e355da0 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 677 | switch (flushpoint) { |
| 678 | case PRE_CONSOLE_FLUSHPOINT1_SERIAL: |
| 679 | puts(buf_out); |
| 680 | break; |
| 681 | case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL: |
Simon Glass | 66ee817 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 682 | console_puts_select(stdout, false, buf_out); |
Hans de Goede | e355da0 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 683 | break; |
| 684 | } |
Rasmus Villemoes | e406a61 | 2022-05-03 15:13:27 +0200 | [diff] [blame] | 685 | gd->precon_buf_idx = in; |
Graeme Russ | 3c28f48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 686 | } |
| 687 | #else |
| 688 | static inline void pre_console_putc(const char c) {} |
Soeren Moch | 54619bb | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 689 | static inline void pre_console_puts(const char *s) {} |
Siarhei Siamashka | 616d03e | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 690 | static inline void print_pre_console_buffer(int flushpoint) {} |
Graeme Russ | 3c28f48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 691 | #endif |
| 692 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 693 | void putc(const char c) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 694 | { |
Patrick Delaunay | 8f14b40 | 2020-11-27 11:20:56 +0100 | [diff] [blame] | 695 | if (!gd) |
| 696 | return; |
Patrick Delaunay | c20847f | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 697 | |
| 698 | console_record_putc(c); |
| 699 | |
Simon Glass | 16e3d7c | 2017-12-04 13:48:19 -0700 | [diff] [blame] | 700 | /* sandbox can send characters to stdout before it has a console */ |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 701 | if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) { |
Simon Glass | 16e3d7c | 2017-12-04 13:48:19 -0700 | [diff] [blame] | 702 | os_putc(c); |
| 703 | return; |
| 704 | } |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 705 | |
Simon Glass | 7dd27e0 | 2015-06-23 15:38:33 -0600 | [diff] [blame] | 706 | /* if we don't have a console yet, use the debug UART */ |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 707 | if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) { |
Simon Glass | 7dd27e0 | 2015-06-23 15:38:33 -0600 | [diff] [blame] | 708 | printch(c); |
| 709 | return; |
| 710 | } |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 711 | |
| 712 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) { |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 713 | if (!(gd->flags & GD_FLG_DEVINIT)) |
| 714 | pre_console_putc(c); |
wdenk | 927034e | 2004-02-08 19:38:38 +0000 | [diff] [blame] | 715 | return; |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 716 | } |
wdenk | 4b6e905 | 2004-02-06 21:48:22 +0000 | [diff] [blame] | 717 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 718 | if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE)) |
Mark Jackson | 5de5621 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 719 | return; |
Mark Jackson | 5de5621 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 720 | |
Simon Glass | d4b0fdb | 2024-08-21 10:19:04 -0600 | [diff] [blame] | 721 | if (!(gd->flags & GD_FLG_HAVE_CONSOLE)) |
Graeme Russ | 3c28f48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 722 | return pre_console_putc(c); |
Graeme Russ | 70600b0 | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 723 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 724 | if (gd->flags & GD_FLG_DEVINIT) { |
| 725 | /* Send to the standard output */ |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 726 | fputc(stdout, c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 727 | } else { |
| 728 | /* Send directly to the handler */ |
Siarhei Siamashka | 616d03e | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 729 | pre_console_putc(c); |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 730 | serial_putc(c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 734 | void puts(const char *s) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 735 | { |
Patrick Delaunay | 8f14b40 | 2020-11-27 11:20:56 +0100 | [diff] [blame] | 736 | if (!gd) |
| 737 | return; |
Patrick Delaunay | c20847f | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 738 | |
| 739 | console_record_puts(s); |
| 740 | |
Simon Glass | bf5c486 | 2018-11-15 18:44:04 -0700 | [diff] [blame] | 741 | /* sandbox can send characters to stdout before it has a console */ |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 742 | if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) { |
Simon Glass | bf5c486 | 2018-11-15 18:44:04 -0700 | [diff] [blame] | 743 | os_puts(s); |
| 744 | return; |
| 745 | } |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 746 | |
| 747 | if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) { |
Soeren Moch | 54619bb | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 748 | while (*s) { |
| 749 | int ch = *s++; |
| 750 | |
| 751 | printch(ch); |
| 752 | } |
| 753 | return; |
| 754 | } |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 755 | |
| 756 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) { |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 757 | if (!(gd->flags & GD_FLG_DEVINIT)) |
| 758 | pre_console_puts(s); |
Soeren Moch | 54619bb | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 759 | return; |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 760 | } |
Soeren Moch | 54619bb | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 761 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 762 | if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE)) |
Soeren Moch | 54619bb | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 763 | return; |
Soeren Moch | 54619bb | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 764 | |
Simon Glass | d4b0fdb | 2024-08-21 10:19:04 -0600 | [diff] [blame] | 765 | if (!(gd->flags & GD_FLG_HAVE_CONSOLE)) |
Soeren Moch | 54619bb | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 766 | return pre_console_puts(s); |
| 767 | |
| 768 | if (gd->flags & GD_FLG_DEVINIT) { |
| 769 | /* Send to the standard output */ |
| 770 | fputs(stdout, s); |
| 771 | } else { |
| 772 | /* Send directly to the handler */ |
| 773 | pre_console_puts(s); |
| 774 | serial_puts(s); |
| 775 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 776 | } |
Pali Rohár | 5aceb26 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 777 | |
| 778 | #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT |
| 779 | void flush(void) |
| 780 | { |
| 781 | if (!gd) |
| 782 | return; |
| 783 | |
| 784 | /* sandbox can send characters to stdout before it has a console */ |
| 785 | if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) { |
| 786 | os_flush(); |
| 787 | return; |
| 788 | } |
| 789 | |
| 790 | if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) |
| 791 | return; |
| 792 | |
| 793 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) |
| 794 | return; |
| 795 | |
| 796 | if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE)) |
| 797 | return; |
| 798 | |
Simon Glass | d4b0fdb | 2024-08-21 10:19:04 -0600 | [diff] [blame] | 799 | if (!(gd->flags & GD_FLG_HAVE_CONSOLE)) |
Pali Rohár | 5aceb26 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 800 | return; |
| 801 | |
| 802 | if (gd->flags & GD_FLG_DEVINIT) { |
| 803 | /* Send to the standard output */ |
| 804 | fflush(stdout); |
Pali Rohár | a48a24f | 2022-09-05 11:31:19 +0200 | [diff] [blame] | 805 | } else { |
| 806 | /* Send directly to the handler */ |
| 807 | serial_flush(); |
Pali Rohár | 5aceb26 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 811 | |
Simon Glass | 1bb4923 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 812 | #ifdef CONFIG_CONSOLE_RECORD |
| 813 | int console_record_init(void) |
| 814 | { |
| 815 | int ret; |
| 816 | |
Heinrich Schuchardt | 3b05a26 | 2020-02-12 18:23:49 +0100 | [diff] [blame] | 817 | ret = membuff_new((struct membuff *)&gd->console_out, |
Simon Glass | 9803ddf | 2021-10-23 17:26:03 -0600 | [diff] [blame] | 818 | gd->flags & GD_FLG_RELOC ? |
| 819 | CONFIG_CONSOLE_RECORD_OUT_SIZE : |
| 820 | CONFIG_CONSOLE_RECORD_OUT_SIZE_F); |
Simon Glass | 1bb4923 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 821 | if (ret) |
| 822 | return ret; |
Heinrich Schuchardt | 3b05a26 | 2020-02-12 18:23:49 +0100 | [diff] [blame] | 823 | ret = membuff_new((struct membuff *)&gd->console_in, |
| 824 | CONFIG_CONSOLE_RECORD_IN_SIZE); |
Simon Glass | 1bb4923 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 825 | |
Ion Agorria | c0b0de4 | 2024-01-05 09:22:09 +0200 | [diff] [blame] | 826 | /* Start recording from the beginning */ |
| 827 | gd->flags |= GD_FLG_RECORD; |
| 828 | |
Simon Glass | 1bb4923 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | void console_record_reset(void) |
| 833 | { |
Heinrich Schuchardt | 3b05a26 | 2020-02-12 18:23:49 +0100 | [diff] [blame] | 834 | membuff_purge((struct membuff *)&gd->console_out); |
| 835 | membuff_purge((struct membuff *)&gd->console_in); |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 836 | gd->flags &= ~GD_FLG_RECORD_OVF; |
Simon Glass | 1bb4923 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Simon Glass | dc357ef | 2020-07-28 19:41:11 -0600 | [diff] [blame] | 839 | int console_record_reset_enable(void) |
Simon Glass | 1bb4923 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 840 | { |
| 841 | console_record_reset(); |
| 842 | gd->flags |= GD_FLG_RECORD; |
Simon Glass | dc357ef | 2020-07-28 19:41:11 -0600 | [diff] [blame] | 843 | |
| 844 | return 0; |
Simon Glass | 1bb4923 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 845 | } |
Simon Glass | b17c158 | 2020-01-27 08:49:54 -0700 | [diff] [blame] | 846 | |
| 847 | int console_record_readline(char *str, int maxlen) |
| 848 | { |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 849 | if (gd->flags & GD_FLG_RECORD_OVF) |
| 850 | return -ENOSPC; |
Simon Glass | c2e59eb | 2024-08-22 07:57:47 -0600 | [diff] [blame] | 851 | if (console_record_isempty()) |
| 852 | return -ENOENT; |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 853 | |
Heinrich Schuchardt | 3b05a26 | 2020-02-12 18:23:49 +0100 | [diff] [blame] | 854 | return membuff_readline((struct membuff *)&gd->console_out, str, |
Ion Agorria | 7e06c1f | 2024-01-05 09:22:10 +0200 | [diff] [blame] | 855 | maxlen, '\0', false); |
Simon Glass | b17c158 | 2020-01-27 08:49:54 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | int console_record_avail(void) |
| 859 | { |
Heinrich Schuchardt | 3b05a26 | 2020-02-12 18:23:49 +0100 | [diff] [blame] | 860 | return membuff_avail((struct membuff *)&gd->console_out); |
Simon Glass | b17c158 | 2020-01-27 08:49:54 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Ion Agorria | 15f6d6a | 2024-01-05 09:22:08 +0200 | [diff] [blame] | 863 | bool console_record_isempty(void) |
| 864 | { |
| 865 | return membuff_isempty((struct membuff *)&gd->console_out); |
| 866 | } |
| 867 | |
Steffen Jaeckel | e1788f9 | 2021-07-08 15:57:40 +0200 | [diff] [blame] | 868 | int console_in_puts(const char *str) |
| 869 | { |
| 870 | return membuff_put((struct membuff *)&gd->console_in, str, strlen(str)); |
| 871 | } |
| 872 | |
Simon Glass | 1bb4923 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 873 | #endif |
| 874 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 875 | /* test if ctrl-c was pressed */ |
| 876 | static int ctrlc_disabled = 0; /* see disable_ctrl() */ |
| 877 | static int ctrlc_was_pressed = 0; |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 878 | int ctrlc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 879 | { |
Simon Glass | d4b0fdb | 2024-08-21 10:19:04 -0600 | [diff] [blame] | 880 | if (!ctrlc_disabled && (gd->flags & GD_FLG_HAVE_CONSOLE)) { |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 881 | if (tstc()) { |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 882 | switch (getchar()) { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 883 | case 0x03: /* ^C - Control C */ |
| 884 | ctrlc_was_pressed = 1; |
| 885 | return 1; |
| 886 | default: |
| 887 | break; |
| 888 | } |
| 889 | } |
| 890 | } |
Simon Glass | 28c5ce7 | 2014-09-14 12:40:15 -0600 | [diff] [blame] | 891 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 892 | return 0; |
| 893 | } |
Pierre Aubert | 5fef9d3 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 894 | /* Reads user's confirmation. |
| 895 | Returns 1 if user's input is "y", "Y", "yes" or "YES" |
| 896 | */ |
| 897 | int confirm_yesno(void) |
| 898 | { |
| 899 | int i; |
| 900 | char str_input[5]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 901 | |
Pierre Aubert | 5fef9d3 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 902 | /* Flush input */ |
| 903 | while (tstc()) |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 904 | getchar(); |
Pierre Aubert | 5fef9d3 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 905 | i = 0; |
| 906 | while (i < sizeof(str_input)) { |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 907 | str_input[i] = getchar(); |
Pierre Aubert | 5fef9d3 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 908 | putc(str_input[i]); |
| 909 | if (str_input[i] == '\r') |
| 910 | break; |
| 911 | i++; |
| 912 | } |
| 913 | putc('\n'); |
| 914 | if (strncmp(str_input, "y\r", 2) == 0 || |
| 915 | strncmp(str_input, "Y\r", 2) == 0 || |
| 916 | strncmp(str_input, "yes\r", 4) == 0 || |
| 917 | strncmp(str_input, "YES\r", 4) == 0) |
| 918 | return 1; |
| 919 | return 0; |
| 920 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 921 | /* pass 1 to disable ctrlc() checking, 0 to enable. |
| 922 | * returns previous state |
| 923 | */ |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 924 | int disable_ctrlc(int disable) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 925 | { |
| 926 | int prev = ctrlc_disabled; /* save previous state */ |
| 927 | |
| 928 | ctrlc_disabled = disable; |
| 929 | return prev; |
| 930 | } |
| 931 | |
| 932 | int had_ctrlc (void) |
| 933 | { |
| 934 | return ctrlc_was_pressed; |
| 935 | } |
| 936 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 937 | void clear_ctrlc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 938 | { |
| 939 | ctrlc_was_pressed = 0; |
| 940 | } |
| 941 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 942 | /** U-Boot INIT FUNCTIONS *************************************************/ |
| 943 | |
Andy Shevchenko | cfaed7f | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 944 | struct stdio_dev *console_search_dev(int flags, const char *name) |
Jean-Christophe PLAGNIOL-VILLARD | 8a4a784 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 945 | { |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 946 | struct stdio_dev *dev; |
Jean-Christophe PLAGNIOL-VILLARD | 8a4a784 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 947 | |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 948 | dev = stdio_get_by_name(name); |
Simon Glass | 86f0746 | 2016-02-06 14:31:37 -0700 | [diff] [blame] | 949 | #ifdef CONFIG_VIDCONSOLE_AS_LCD |
Patrick Delaunay | 60b9224 | 2020-07-01 14:56:10 +0200 | [diff] [blame] | 950 | if (!dev && !strcmp(name, CONFIG_VIDCONSOLE_AS_NAME)) |
Simon Glass | 86f0746 | 2016-02-06 14:31:37 -0700 | [diff] [blame] | 951 | dev = stdio_get_by_name("vidconsole"); |
| 952 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 8a4a784 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 953 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 954 | if (dev && (dev->flags & flags)) |
Jean-Christophe PLAGNIOL-VILLARD | 8a4a784 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 955 | return dev; |
| 956 | |
| 957 | return NULL; |
| 958 | } |
| 959 | |
Mike Frysinger | e3d5572 | 2010-10-20 07:18:03 -0400 | [diff] [blame] | 960 | int console_assign(int file, const char *devname) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 961 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8a4a784 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 962 | int flag; |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 963 | struct stdio_dev *dev; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 964 | |
| 965 | /* Check for valid file */ |
Andy Shevchenko | 59f324c | 2021-02-11 17:09:37 +0200 | [diff] [blame] | 966 | flag = stdio_file_to_flags(file); |
| 967 | if (flag < 0) |
| 968 | return flag; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 969 | |
| 970 | /* Check for valid device name */ |
| 971 | |
Andy Shevchenko | cfaed7f | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 972 | dev = console_search_dev(flag, devname); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 973 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 974 | if (dev) |
| 975 | return console_setfile(file, dev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 976 | |
| 977 | return -1; |
| 978 | } |
| 979 | |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 980 | /* return true if the 'silent' flag is removed */ |
| 981 | static bool console_update_silent(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 982 | { |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 983 | unsigned long flags = gd->flags; |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 984 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 985 | if (!IS_ENABLED(CONFIG_SILENT_CONSOLE)) |
| 986 | return false; |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 987 | |
Harald Seiler | 6d09df8 | 2022-07-06 13:19:10 +0200 | [diff] [blame] | 988 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE_UNTIL_ENV) && !(gd->flags & GD_FLG_ENV_READY)) { |
| 989 | gd->flags |= GD_FLG_SILENT; |
| 990 | return false; |
| 991 | } |
| 992 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 993 | if (env_get("silent")) { |
| 994 | gd->flags |= GD_FLG_SILENT; |
| 995 | return false; |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 996 | } |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 997 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 998 | gd->flags &= ~GD_FLG_SILENT; |
| 999 | |
| 1000 | return !!(flags & GD_FLG_SILENT); |
Simon Glass | c9c5c9f | 2017-06-15 21:37:50 -0600 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | int console_announce_r(void) |
| 1004 | { |
| 1005 | #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER) |
| 1006 | char buf[DISPLAY_OPTIONS_BANNER_LENGTH]; |
| 1007 | |
| 1008 | display_options_get_banner(false, buf, sizeof(buf)); |
| 1009 | |
Simon Glass | 66ee817 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 1010 | console_puts_select(stdout, false, buf); |
Simon Glass | c9c5c9f | 2017-06-15 21:37:50 -0600 | [diff] [blame] | 1011 | #endif |
| 1012 | |
| 1013 | return 0; |
Chris Packham | fab806d | 2016-09-23 15:59:43 +1200 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /* Called before relocation - use serial functions */ |
| 1017 | int console_init_f(void) |
| 1018 | { |
Simon Glass | d4b0fdb | 2024-08-21 10:19:04 -0600 | [diff] [blame] | 1019 | gd->flags |= GD_FLG_HAVE_CONSOLE; |
Chris Packham | fab806d | 2016-09-23 15:59:43 +1200 | [diff] [blame] | 1020 | |
| 1021 | console_update_silent(); |
wdenk | 808532a | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 1022 | |
Siarhei Siamashka | 616d03e | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 1023 | print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL); |
Graeme Russ | 3c28f48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 1024 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1025 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Simon Glass | ffafbe3 | 2023-10-01 19:15:23 -0600 | [diff] [blame] | 1028 | int console_clear(void) |
| 1029 | { |
| 1030 | /* |
| 1031 | * Send clear screen and home |
| 1032 | * |
| 1033 | * FIXME(Heinrich Schuchardt <xypron.glpk@gmx.de>): This should go |
| 1034 | * through an API and only be written to serial terminals, not video |
| 1035 | * displays |
| 1036 | */ |
| 1037 | printf(CSI "2J" CSI "1;1H"); |
| 1038 | if (IS_ENABLED(CONFIG_VIDEO_ANSI)) |
| 1039 | return 0; |
| 1040 | |
| 1041 | if (IS_ENABLED(CONFIG_VIDEO)) { |
| 1042 | struct udevice *dev; |
| 1043 | int ret; |
| 1044 | |
| 1045 | ret = uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev); |
| 1046 | if (ret) |
| 1047 | return ret; |
| 1048 | ret = vidconsole_clear_and_reset(dev); |
| 1049 | if (ret) |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
Patrice Chotard | cc82bae | 2024-01-17 13:37:13 +0100 | [diff] [blame] | 1056 | static char *get_stdio(const u8 std) |
| 1057 | { |
| 1058 | return stdio_devices[std] ? stdio_devices[std]->name : "No devices available!"; |
| 1059 | } |
| 1060 | |
Bin Meng | 1318689 | 2023-07-23 12:40:35 +0800 | [diff] [blame] | 1061 | static void stdio_print_current_devices(void) |
Jean-Christophe PLAGNIOL-VILLARD | 5a30936 | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 1062 | { |
Patrice Chotard | cc82bae | 2024-01-17 13:37:13 +0100 | [diff] [blame] | 1063 | char *stdinname = NULL; |
| 1064 | char *stdoutname = NULL; |
| 1065 | char *stderrname = NULL; |
Bin Meng | a6a8350 | 2023-07-23 12:40:36 +0800 | [diff] [blame] | 1066 | |
Bin Meng | d61be5b | 2023-07-23 12:40:37 +0800 | [diff] [blame] | 1067 | if (CONFIG_IS_ENABLED(CONSOLE_MUX) && |
| 1068 | CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)) { |
| 1069 | /* stdin stdout and stderr are in environment */ |
| 1070 | stdinname = env_get("stdin"); |
| 1071 | stdoutname = env_get("stdout"); |
| 1072 | stderrname = env_get("stderr"); |
Bin Meng | d61be5b | 2023-07-23 12:40:37 +0800 | [diff] [blame] | 1073 | } |
Bin Meng | a6a8350 | 2023-07-23 12:40:36 +0800 | [diff] [blame] | 1074 | |
Patrice Chotard | cc82bae | 2024-01-17 13:37:13 +0100 | [diff] [blame] | 1075 | stdinname = stdinname ? : get_stdio(stdin); |
| 1076 | stdoutname = stdoutname ? : get_stdio(stdout); |
| 1077 | stderrname = stderrname ? : get_stdio(stderr); |
| 1078 | |
Jean-Christophe PLAGNIOL-VILLARD | 5a30936 | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 1079 | /* Print information */ |
| 1080 | puts("In: "); |
Bin Meng | a6a8350 | 2023-07-23 12:40:36 +0800 | [diff] [blame] | 1081 | printf("%s\n", stdinname); |
Jean-Christophe PLAGNIOL-VILLARD | 5a30936 | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 1082 | |
| 1083 | puts("Out: "); |
Bin Meng | a6a8350 | 2023-07-23 12:40:36 +0800 | [diff] [blame] | 1084 | printf("%s\n", stdoutname); |
Jean-Christophe PLAGNIOL-VILLARD | 5a30936 | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 1085 | |
| 1086 | puts("Err: "); |
Bin Meng | a6a8350 | 2023-07-23 12:40:36 +0800 | [diff] [blame] | 1087 | printf("%s\n", stderrname); |
Jean-Christophe PLAGNIOL-VILLARD | 5a30936 | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
Simon Glass | dfc2535 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 1090 | #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1091 | /* Called after the relocation - use desired console functions */ |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1092 | int console_init_r(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1093 | { |
| 1094 | char *stdinname, *stdoutname, *stderrname; |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 1095 | struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1096 | int i; |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1097 | int iomux_err = 0; |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1098 | int flushpoint; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1099 | |
Patrick Delaunay | 46b610d | 2019-08-02 14:58:09 +0200 | [diff] [blame] | 1100 | /* update silent for env loaded from flash (initr_env) */ |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1101 | if (console_update_silent()) |
| 1102 | flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL; |
| 1103 | else |
| 1104 | flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL; |
Patrick Delaunay | 46b610d | 2019-08-02 14:58:09 +0200 | [diff] [blame] | 1105 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1106 | /* set default handlers at first */ |
Martin Dorwig | cb2c286 | 2015-01-26 15:22:54 -0700 | [diff] [blame] | 1107 | gd->jt->getc = serial_getc; |
| 1108 | gd->jt->tstc = serial_tstc; |
| 1109 | gd->jt->putc = serial_putc; |
| 1110 | gd->jt->puts = serial_puts; |
| 1111 | gd->jt->printf = serial_printf; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1112 | |
| 1113 | /* stdin stdout and stderr are in environment */ |
| 1114 | /* scan for it */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1115 | stdinname = env_get("stdin"); |
| 1116 | stdoutname = env_get("stdout"); |
| 1117 | stderrname = env_get("stderr"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1118 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1119 | if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */ |
Andy Shevchenko | cfaed7f | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 1120 | inputdev = console_search_dev(DEV_FLAGS_INPUT, stdinname); |
| 1121 | outputdev = console_search_dev(DEV_FLAGS_OUTPUT, stdoutname); |
| 1122 | errdev = console_search_dev(DEV_FLAGS_OUTPUT, stderrname); |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 1123 | if (CONFIG_IS_ENABLED(CONSOLE_MUX)) { |
| 1124 | iomux_err = iomux_doenv(stdin, stdinname); |
| 1125 | iomux_err += iomux_doenv(stdout, stdoutname); |
| 1126 | iomux_err += iomux_doenv(stderr, stderrname); |
| 1127 | if (!iomux_err) |
| 1128 | /* Successful, so skip all the code below. */ |
| 1129 | goto done; |
| 1130 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1131 | } |
| 1132 | /* if the devices are overwritten or not found, use default device */ |
| 1133 | if (inputdev == NULL) { |
Andy Shevchenko | cfaed7f | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 1134 | inputdev = console_search_dev(DEV_FLAGS_INPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1135 | } |
| 1136 | if (outputdev == NULL) { |
Andy Shevchenko | cfaed7f | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 1137 | outputdev = console_search_dev(DEV_FLAGS_OUTPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1138 | } |
| 1139 | if (errdev == NULL) { |
Andy Shevchenko | cfaed7f | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 1140 | errdev = console_search_dev(DEV_FLAGS_OUTPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1141 | } |
| 1142 | /* Initializes output console first */ |
| 1143 | if (outputdev != NULL) { |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1144 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 1145 | console_doenv(stdout, outputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1146 | } |
| 1147 | if (errdev != NULL) { |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1148 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 1149 | console_doenv(stderr, errdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1150 | } |
| 1151 | if (inputdev != NULL) { |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1152 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 63331a0 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 1153 | console_doenv(stdin, inputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1156 | done: |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1157 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 1158 | if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET)) |
| 1159 | stdio_print_current_devices(); |
| 1160 | |
Simon Glass | 86f0746 | 2016-02-06 14:31:37 -0700 | [diff] [blame] | 1161 | #ifdef CONFIG_VIDCONSOLE_AS_LCD |
Patrick Delaunay | 60b9224 | 2020-07-01 14:56:10 +0200 | [diff] [blame] | 1162 | if (strstr(stdoutname, CONFIG_VIDCONSOLE_AS_NAME)) |
Anatolij Gustschin | 4702064 | 2020-05-23 17:11:20 +0200 | [diff] [blame] | 1163 | printf("Warning: Please change '%s' to 'vidconsole' in stdout/stderr environment vars\n", |
Patrick Delaunay | 60b9224 | 2020-07-01 14:56:10 +0200 | [diff] [blame] | 1164 | CONFIG_VIDCONSOLE_AS_NAME); |
Simon Glass | 86f0746 | 2016-02-06 14:31:37 -0700 | [diff] [blame] | 1165 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1166 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 1167 | if (IS_ENABLED(CONFIG_SYS_CONSOLE_ENV_OVERWRITE)) { |
| 1168 | /* set the environment variables (will overwrite previous env settings) */ |
| 1169 | for (i = 0; i < MAX_FILES; i++) |
| 1170 | env_set(stdio_names[i], stdio_devices[i]->name); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1171 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1172 | |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 1173 | gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */ |
| 1174 | |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1175 | print_pre_console_buffer(flushpoint); |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1176 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Simon Glass | dfc2535 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 1179 | #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1180 | |
| 1181 | /* Called after the relocation - use desired console functions */ |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1182 | int console_init_r(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1183 | { |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 1184 | struct stdio_dev *inputdev = NULL, *outputdev = NULL; |
Jean-Christophe PLAGNIOL-VILLARD | 8a4a784 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 1185 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 1186 | struct list_head *list = stdio_get_list(); |
Jean-Christophe PLAGNIOL-VILLARD | 8a4a784 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 1187 | struct list_head *pos; |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 1188 | struct stdio_dev *dev; |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1189 | int flushpoint; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1190 | |
Patrick Delaunay | 46b610d | 2019-08-02 14:58:09 +0200 | [diff] [blame] | 1191 | /* update silent for env loaded from flash (initr_env) */ |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1192 | if (console_update_silent()) |
| 1193 | flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL; |
| 1194 | else |
| 1195 | flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL; |
Chris Packham | fab806d | 2016-09-23 15:59:43 +1200 | [diff] [blame] | 1196 | |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1197 | /* |
| 1198 | * suppress all output if splash screen is enabled and we have |
Anatolij Gustschin | ec8744a | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 1199 | * a bmp to display. We redirect the output from frame buffer |
| 1200 | * console to serial console in this case or suppress it if |
| 1201 | * "silent" mode was requested. |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1202 | */ |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 1203 | if (IS_ENABLED(CONFIG_SPLASH_SCREEN) && env_get("splashimage")) { |
Anatolij Gustschin | ec8744a | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 1204 | if (!(gd->flags & GD_FLG_SILENT)) |
Andy Shevchenko | cfaed7f | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 1205 | outputdev = console_search_dev (DEV_FLAGS_OUTPUT, "serial"); |
Anatolij Gustschin | ec8744a | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 1206 | } |
wdenk | 808532a | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 1207 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1208 | /* Scan devices looking for input and output devices */ |
Jean-Christophe PLAGNIOL-VILLARD | 8a4a784 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 1209 | list_for_each(pos, list) { |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 1210 | dev = list_entry(pos, struct stdio_dev, list); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1211 | |
| 1212 | if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) { |
| 1213 | inputdev = dev; |
| 1214 | } |
| 1215 | if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) { |
| 1216 | outputdev = dev; |
| 1217 | } |
Jean-Christophe PLAGNIOL-VILLARD | 8a4a784 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 1218 | if(inputdev && outputdev) |
| 1219 | break; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | /* Initializes output console first */ |
| 1223 | if (outputdev != NULL) { |
Andy Shevchenko | ba69b2c | 2021-02-11 17:09:39 +0200 | [diff] [blame] | 1224 | console_setfile_and_devices(stdout, outputdev); |
| 1225 | console_setfile_and_devices(stderr, outputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | /* Initializes input console */ |
Andy Shevchenko | ba69b2c | 2021-02-11 17:09:39 +0200 | [diff] [blame] | 1229 | if (inputdev != NULL) |
| 1230 | console_setfile_and_devices(stdin, inputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1231 | |
Patrick Delaunay | 4b0b56f | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 1232 | if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET)) |
| 1233 | stdio_print_current_devices(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1234 | |
| 1235 | /* Setting environment variables */ |
Tom Rini | 66c4d71 | 2018-05-03 09:12:26 -0400 | [diff] [blame] | 1236 | for (i = 0; i < MAX_FILES; i++) { |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 1237 | env_set(stdio_names[i], stdio_devices[i]->name); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 1240 | gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */ |
| 1241 | |
Patrick Delaunay | b39862f | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1242 | print_pre_console_buffer(flushpoint); |
Jean-Christophe PLAGNIOL-VILLARD | c7dc672 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1243 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Simon Glass | dfc2535 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 1246 | #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */ |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 1247 | |
| 1248 | int console_remove_by_name(const char *name) |
| 1249 | { |
| 1250 | int err = 0; |
| 1251 | |
| 1252 | #if CONFIG_IS_ENABLED(CONSOLE_MUX) |
| 1253 | int fnum; |
| 1254 | |
| 1255 | log_debug("removing console device %s\n", name); |
| 1256 | for (fnum = 0; fnum < MAX_FILES; fnum++) { |
| 1257 | struct stdio_dev **src, **dest; |
| 1258 | int i; |
| 1259 | |
| 1260 | log_debug("file %d: %d devices: ", fnum, cd_count[fnum]); |
| 1261 | src = console_devices[fnum]; |
| 1262 | dest = src; |
| 1263 | for (i = 0; i < cd_count[fnum]; i++, src++) { |
| 1264 | struct stdio_dev *sdev = *src; |
| 1265 | int ret = 0; |
| 1266 | |
| 1267 | if (!strcmp(sdev->name, name)) |
| 1268 | ret = stdio_deregister_dev(sdev, true); |
| 1269 | else |
| 1270 | *dest++ = *src; |
| 1271 | if (ret && !err) |
| 1272 | err = ret; |
| 1273 | } |
| 1274 | cd_count[fnum] = dest - console_devices[fnum]; |
| 1275 | log_debug("now %d\n", cd_count[fnum]); |
| 1276 | } |
| 1277 | #endif /* CONSOLE_MUX */ |
| 1278 | |
| 1279 | return err; |
| 1280 | } |