Tom Rini | 83d290c | 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 | 90afded | 2024-09-01 16:26:18 -0600 | [diff] [blame^] | 7 | #define LOG_CATEGORY LOGC_CONSOLE |
| 8 | |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 9 | #include <console.h> |
Simon Glass | d6ea530 | 2015-06-23 15:38:33 -0600 | [diff] [blame] | 10 | #include <debug_uart.h> |
Simon Glass | 4e4bf94 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 11 | #include <display_options.h> |
Simon Glass | 7b3c4c3 | 2017-07-27 09:31:04 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 9fb625c | 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 | 482f469 | 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 | 4e6bafa | 2017-06-15 21:37:52 -0600 | [diff] [blame] | 17 | #include <mapmem.h> |
Simon Glass | 91b136c | 2013-11-10 10:27:01 -0700 | [diff] [blame] | 18 | #include <os.h> |
Joe Hershberger | 849d5d9 | 2012-12-11 22:16:29 -0600 | [diff] [blame] | 19 | #include <serial.h> |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 20 | #include <stdio_dev.h> |
wdenk | 27b207f | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 21 | #include <exports.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 22 | #include <env_internal.h> |
Simon Glass | cde03fa | 2023-10-01 19:15:23 -0600 | [diff] [blame] | 23 | #include <video_console.h> |
Andreas J. Reichel | 6440746 | 2016-07-13 12:56:51 +0200 | [diff] [blame] | 24 | #include <watchdog.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 25 | #include <asm/global_data.h> |
Simon Glass | c05ed00 | 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 | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Simon Glass | cde03fa | 2023-10-01 19:15:23 -0600 | [diff] [blame] | 30 | #define CSI "\x1b[" |
| 31 | |
Joe Hershberger | 849d5d9 | 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 | c04f856 | 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 | 849d5d9 | 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 | e080d54 | 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 | c04f856 | 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 | e080d54 | 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 | 1e99371 | 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 | c1a2bb4 | 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 | 1e99371 | 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 | c1a2bb4 | 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 | 1e99371 | 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 | b026542 | 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 | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 167 | #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 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 | 83e40ba | 2005-03-31 18:42:15 +0000 | [diff] [blame] | 171 | #define OVERWRITE_CONSOLE 0 |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 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 | b026542 | 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 | 52cb4d4 | 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 | 41f668b | 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 | |
| 194 | /* |
| 195 | * Update monitor functions |
| 196 | * (to use the console stuff by other applications) |
| 197 | */ |
| 198 | switch (file) { |
| 199 | case stdin: |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 200 | gd->jt->getc = getchar; |
Martin Dorwig | 49cad54 | 2015-01-26 15:22:54 -0700 | [diff] [blame] | 201 | gd->jt->tstc = tstc; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 202 | break; |
| 203 | case stdout: |
Martin Dorwig | 49cad54 | 2015-01-26 15:22:54 -0700 | [diff] [blame] | 204 | gd->jt->putc = putc; |
| 205 | gd->jt->puts = puts; |
Pali Rohár | 974f483 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 206 | STDIO_DEV_ASSIGN_FLUSH(gd->jt, flush); |
Martin Dorwig | 49cad54 | 2015-01-26 15:22:54 -0700 | [diff] [blame] | 207 | gd->jt->printf = printf; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 208 | break; |
| 209 | } |
| 210 | break; |
| 211 | |
| 212 | default: /* Invalid file ID */ |
| 213 | error = -1; |
| 214 | } |
| 215 | return error; |
| 216 | } |
| 217 | |
Simon Glass | 42f9f91 | 2017-07-27 09:31:03 -0600 | [diff] [blame] | 218 | /** |
| 219 | * console_dev_is_serial() - Check if a stdio device is a serial device |
| 220 | * |
| 221 | * @sdev: Device to check |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 222 | * Return: true if this device is in the serial uclass (or for pre-driver-model, |
Simon Glass | 7b3c4c3 | 2017-07-27 09:31:04 -0600 | [diff] [blame] | 223 | * whether it is called "serial". |
Simon Glass | 42f9f91 | 2017-07-27 09:31:03 -0600 | [diff] [blame] | 224 | */ |
| 225 | static bool console_dev_is_serial(struct stdio_dev *sdev) |
| 226 | { |
| 227 | bool is_serial; |
| 228 | |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 229 | if (IS_ENABLED(CONFIG_DM_SERIAL) && (sdev->flags & DEV_FLAGS_DM)) { |
Simon Glass | 7b3c4c3 | 2017-07-27 09:31:04 -0600 | [diff] [blame] | 230 | struct udevice *dev = sdev->priv; |
| 231 | |
| 232 | is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL; |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 233 | } else { |
| 234 | is_serial = !strcmp(sdev->name, "serial"); |
| 235 | } |
Simon Glass | 42f9f91 | 2017-07-27 09:31:03 -0600 | [diff] [blame] | 236 | |
| 237 | return is_serial; |
| 238 | } |
| 239 | |
Simon Glass | b026542 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 240 | #if CONFIG_IS_ENABLED(CONSOLE_MUX) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 241 | /** Console I/O multiplexing *******************************************/ |
| 242 | |
Patrick Delaunay | a17b38c | 2020-12-18 12:46:46 +0100 | [diff] [blame] | 243 | /* tstcdev: save the last stdio device with pending characters, with tstc != 0 */ |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 244 | static struct stdio_dev *tstcdev; |
| 245 | struct stdio_dev **console_devices[MAX_FILES]; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 246 | int cd_count[MAX_FILES]; |
| 247 | |
Andy Shevchenko | 09d8f07 | 2021-02-11 17:09:39 +0200 | [diff] [blame] | 248 | static void console_devices_set(int file, struct stdio_dev *dev) |
Patrick Delaunay | 45375ad | 2020-12-18 12:46:44 +0100 | [diff] [blame] | 249 | { |
| 250 | console_devices[file][0] = dev; |
Andy Shevchenko | 20a7d35 | 2021-02-11 17:09:38 +0200 | [diff] [blame] | 251 | cd_count[file] = 1; |
Patrick Delaunay | 45375ad | 2020-12-18 12:46:44 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Andy Shevchenko | 95aaf40 | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 254 | /** |
| 255 | * console_needs_start_stop() - check if we need to start or stop the STDIO device |
| 256 | * @file: STDIO file |
| 257 | * @sdev: STDIO device in question |
| 258 | * |
| 259 | * This function checks if we need to start or stop the stdio device used for |
| 260 | * a console. For IOMUX case it simply enforces one time start and one time |
| 261 | * stop of the device independently of how many STDIO files are using it. In |
| 262 | * other words, we start console once before first STDIO device wants it and |
| 263 | * stop after the last is gone. |
| 264 | */ |
| 265 | static bool console_needs_start_stop(int file, struct stdio_dev *sdev) |
| 266 | { |
Andy Shevchenko | b672c16 | 2021-02-11 17:09:41 +0200 | [diff] [blame] | 267 | int i; |
Andy Shevchenko | 95aaf40 | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 268 | |
| 269 | for (i = 0; i < ARRAY_SIZE(cd_count); i++) { |
| 270 | if (i == file) |
| 271 | continue; |
| 272 | |
Andy Shevchenko | b672c16 | 2021-02-11 17:09:41 +0200 | [diff] [blame] | 273 | if (iomux_match_device(console_devices[i], cd_count[i], sdev) >= 0) |
| 274 | return false; |
Andy Shevchenko | 95aaf40 | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 275 | } |
| 276 | return true; |
| 277 | } |
| 278 | |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 279 | /* |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 280 | * This depends on tstc() always being called before getchar(). |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 281 | * This is guaranteed to be true because this routine is called |
| 282 | * only from fgetc() which assures it. |
| 283 | * No attempt is made to demultiplex multiple input sources. |
| 284 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 285 | static int console_getc(int file) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 286 | { |
| 287 | unsigned char ret; |
| 288 | |
| 289 | /* This is never called with testcdev == NULL */ |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 290 | ret = tstcdev->getc(tstcdev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 291 | tstcdev = NULL; |
| 292 | return ret; |
| 293 | } |
| 294 | |
Patrick Delaunay | a17b38c | 2020-12-18 12:46:46 +0100 | [diff] [blame] | 295 | /* Upper layer may have already called tstc(): check the saved result */ |
| 296 | static bool console_has_tstc(void) |
| 297 | { |
| 298 | return !!tstcdev; |
| 299 | } |
| 300 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 301 | static int console_tstc(int file) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 302 | { |
| 303 | int i, ret; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 304 | struct stdio_dev *dev; |
Joe Hershberger | b2f58d8 | 2018-07-02 20:06:48 -0500 | [diff] [blame] | 305 | int prev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 306 | |
Joe Hershberger | b2f58d8 | 2018-07-02 20:06:48 -0500 | [diff] [blame] | 307 | prev = disable_ctrlc(1); |
Andy Shevchenko | 400797c | 2021-02-11 17:09:42 +0200 | [diff] [blame] | 308 | for_each_console_dev(i, file, dev) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 309 | if (dev->tstc != NULL) { |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 310 | ret = dev->tstc(dev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 311 | if (ret > 0) { |
| 312 | tstcdev = dev; |
Joe Hershberger | b2f58d8 | 2018-07-02 20:06:48 -0500 | [diff] [blame] | 313 | disable_ctrlc(prev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 314 | return ret; |
| 315 | } |
| 316 | } |
| 317 | } |
Joe Hershberger | b2f58d8 | 2018-07-02 20:06:48 -0500 | [diff] [blame] | 318 | disable_ctrlc(prev); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 323 | static void console_putc(int file, const char c) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 324 | { |
| 325 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 326 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 327 | |
Andy Shevchenko | 400797c | 2021-02-11 17:09:42 +0200 | [diff] [blame] | 328 | for_each_console_dev(i, file, dev) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 329 | if (dev->putc != NULL) |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 330 | dev->putc(dev, c); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Simon Glass | 493a4c8 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 334 | /** |
| 335 | * console_puts_select() - Output a string to all console devices |
| 336 | * |
| 337 | * @file: File number to output to (e,g, stdout, see stdio.h) |
| 338 | * @serial_only: true to output only to serial, false to output to everything |
| 339 | * else |
| 340 | * @s: String to output |
| 341 | */ |
| 342 | static void console_puts_select(int file, bool serial_only, const char *s) |
Siarhei Siamashka | 2766966 | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 343 | { |
| 344 | int i; |
| 345 | struct stdio_dev *dev; |
| 346 | |
Andy Shevchenko | 400797c | 2021-02-11 17:09:42 +0200 | [diff] [blame] | 347 | for_each_console_dev(i, file, dev) { |
| 348 | bool is_serial = console_dev_is_serial(dev); |
Simon Glass | 493a4c8 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 349 | |
Simon Glass | 493a4c8 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 350 | if (dev->puts && serial_only == is_serial) |
Hans de Goede | a8552c7 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 351 | dev->puts(dev, s); |
Siarhei Siamashka | 2766966 | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 352 | } |
| 353 | } |
Siarhei Siamashka | 2766966 | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 354 | |
Simon Glass | 493a4c8 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 355 | void console_puts_select_stderr(bool serial_only, const char *s) |
| 356 | { |
Simon Glass | 4057e27 | 2021-11-19 13:23:47 -0700 | [diff] [blame] | 357 | if (gd->flags & GD_FLG_DEVINIT) |
| 358 | console_puts_select(stderr, serial_only, s); |
Simon Glass | 493a4c8 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 359 | } |
| 360 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 361 | static void console_puts(int file, const char *s) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 362 | { |
| 363 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 364 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 365 | |
Andy Shevchenko | 400797c | 2021-02-11 17:09:42 +0200 | [diff] [blame] | 366 | for_each_console_dev(i, file, dev) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 367 | if (dev->puts != NULL) |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 368 | dev->puts(dev, s); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 369 | } |
| 370 | } |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 371 | |
Pali Rohár | 974f483 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 372 | #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT |
| 373 | static void console_flush(int file) |
| 374 | { |
| 375 | int i; |
| 376 | struct stdio_dev *dev; |
| 377 | |
| 378 | for_each_console_dev(i, file, dev) { |
| 379 | if (dev->flush != NULL) |
| 380 | dev->flush(dev); |
| 381 | } |
| 382 | } |
| 383 | #endif |
| 384 | |
Tom Rini | 5e63c96 | 2019-10-30 09:18:43 -0400 | [diff] [blame] | 385 | #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 386 | static inline void console_doenv(int file, struct stdio_dev *dev) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 387 | { |
| 388 | iomux_doenv(file, dev->name); |
| 389 | } |
Tom Rini | 5e63c96 | 2019-10-30 09:18:43 -0400 | [diff] [blame] | 390 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 391 | #else |
Patrick Delaunay | 45375ad | 2020-12-18 12:46:44 +0100 | [diff] [blame] | 392 | |
Andy Shevchenko | 09d8f07 | 2021-02-11 17:09:39 +0200 | [diff] [blame] | 393 | static void console_devices_set(int file, struct stdio_dev *dev) |
Patrick Delaunay | 45375ad | 2020-12-18 12:46:44 +0100 | [diff] [blame] | 394 | { |
| 395 | } |
| 396 | |
Andy Shevchenko | 95aaf40 | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 397 | static inline bool console_needs_start_stop(int file, struct stdio_dev *sdev) |
| 398 | { |
| 399 | return true; |
| 400 | } |
| 401 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 402 | static inline int console_getc(int file) |
| 403 | { |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 404 | return stdio_devices[file]->getc(stdio_devices[file]); |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Patrick Delaunay | a17b38c | 2020-12-18 12:46:46 +0100 | [diff] [blame] | 407 | static bool console_has_tstc(void) |
| 408 | { |
| 409 | return false; |
| 410 | } |
| 411 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 412 | static inline int console_tstc(int file) |
| 413 | { |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 414 | return stdio_devices[file]->tstc(stdio_devices[file]); |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static inline void console_putc(int file, const char c) |
| 418 | { |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 419 | stdio_devices[file]->putc(stdio_devices[file], c); |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Simon Glass | 493a4c8 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 422 | void console_puts_select(int file, bool serial_only, const char *s) |
Siarhei Siamashka | 2766966 | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 423 | { |
Simon Glass | 4057e27 | 2021-11-19 13:23:47 -0700 | [diff] [blame] | 424 | if ((gd->flags & GD_FLG_DEVINIT) && |
| 425 | serial_only == console_dev_is_serial(stdio_devices[file])) |
Hans de Goede | a8552c7 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 426 | stdio_devices[file]->puts(stdio_devices[file], s); |
Siarhei Siamashka | 2766966 | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 427 | } |
Siarhei Siamashka | 2766966 | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 428 | |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 429 | static inline void console_puts(int file, const char *s) |
| 430 | { |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 431 | stdio_devices[file]->puts(stdio_devices[file], s); |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Pali Rohár | 974f483 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 434 | #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT |
| 435 | static inline void console_flush(int file) |
| 436 | { |
| 437 | if (stdio_devices[file]->flush) |
| 438 | stdio_devices[file]->flush(stdio_devices[file]); |
| 439 | } |
| 440 | #endif |
| 441 | |
Tom Rini | 5e63c96 | 2019-10-30 09:18:43 -0400 | [diff] [blame] | 442 | #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 443 | static inline void console_doenv(int file, struct stdio_dev *dev) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 444 | { |
| 445 | console_setfile(file, dev); |
| 446 | } |
Tom Rini | 5e63c96 | 2019-10-30 09:18:43 -0400 | [diff] [blame] | 447 | #endif |
Simon Glass | b026542 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 448 | #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */ |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 449 | |
Andy Shevchenko | 09d8f07 | 2021-02-11 17:09:39 +0200 | [diff] [blame] | 450 | static void __maybe_unused console_setfile_and_devices(int file, struct stdio_dev *dev) |
| 451 | { |
| 452 | console_setfile(file, dev); |
| 453 | console_devices_set(file, dev); |
| 454 | } |
| 455 | |
Andy Shevchenko | 41f668b | 2020-12-21 14:30:00 +0200 | [diff] [blame] | 456 | int console_start(int file, struct stdio_dev *sdev) |
| 457 | { |
| 458 | int error; |
| 459 | |
Andy Shevchenko | 95aaf40 | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 460 | if (!console_needs_start_stop(file, sdev)) |
| 461 | return 0; |
| 462 | |
Andy Shevchenko | 41f668b | 2020-12-21 14:30:00 +0200 | [diff] [blame] | 463 | /* Start new device */ |
| 464 | if (sdev->start) { |
| 465 | error = sdev->start(sdev); |
| 466 | /* If it's not started don't use it */ |
| 467 | if (error < 0) |
| 468 | return error; |
| 469 | } |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | void console_stop(int file, struct stdio_dev *sdev) |
| 474 | { |
Andy Shevchenko | 95aaf40 | 2020-12-21 14:30:01 +0200 | [diff] [blame] | 475 | if (!console_needs_start_stop(file, sdev)) |
| 476 | return; |
| 477 | |
Andy Shevchenko | 41f668b | 2020-12-21 14:30:00 +0200 | [diff] [blame] | 478 | if (sdev->stop) |
| 479 | sdev->stop(sdev); |
| 480 | } |
| 481 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 482 | /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/ |
| 483 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 484 | int serial_printf(const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 485 | { |
| 486 | va_list args; |
| 487 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 488 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 489 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 490 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 491 | |
| 492 | /* For this to work, printbuffer must be larger than |
| 493 | * anything we ever want to print. |
| 494 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 495 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 496 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 497 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 498 | serial_puts(printbuffer); |
Wolfgang Denk | d9c2725 | 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 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 502 | int fgetc(int file) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 503 | { |
Heinrich Schuchardt | 27380d8 | 2022-10-22 11:32:34 +0200 | [diff] [blame] | 504 | if ((unsigned int)file < MAX_FILES) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 505 | /* |
| 506 | * Effectively poll for input wherever it may be available. |
| 507 | */ |
| 508 | for (;;) { |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 509 | schedule(); |
Patrick Delaunay | a17b38c | 2020-12-18 12:46:46 +0100 | [diff] [blame] | 510 | if (CONFIG_IS_ENABLED(CONSOLE_MUX)) { |
| 511 | /* |
| 512 | * Upper layer may have already called tstc() so |
| 513 | * check for that first. |
| 514 | */ |
| 515 | if (console_has_tstc()) |
| 516 | return console_getc(file); |
| 517 | console_tstc(file); |
| 518 | } else { |
| 519 | if (console_tstc(file)) |
| 520 | return console_getc(file); |
| 521 | } |
| 522 | |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 523 | /* |
| 524 | * If the watchdog must be rate-limited then it should |
| 525 | * already be handled in board-specific code. |
| 526 | */ |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 527 | if (IS_ENABLED(CONFIG_WATCHDOG)) |
| 528 | udelay(1); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 529 | } |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 530 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 531 | |
| 532 | return -1; |
| 533 | } |
| 534 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 535 | int ftstc(int file) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 536 | { |
Heinrich Schuchardt | 27380d8 | 2022-10-22 11:32:34 +0200 | [diff] [blame] | 537 | if ((unsigned int)file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 538 | return console_tstc(file); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 539 | |
| 540 | return -1; |
| 541 | } |
| 542 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 543 | void fputc(int file, const char c) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 544 | { |
Heinrich Schuchardt | 27380d8 | 2022-10-22 11:32:34 +0200 | [diff] [blame] | 545 | if ((unsigned int)file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 546 | console_putc(file, c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 549 | void fputs(int file, const char *s) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 550 | { |
Heinrich Schuchardt | 27380d8 | 2022-10-22 11:32:34 +0200 | [diff] [blame] | 551 | if ((unsigned int)file < MAX_FILES) |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 552 | console_puts(file, s); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Pali Rohár | 974f483 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 555 | #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT |
| 556 | void fflush(int file) |
| 557 | { |
Heinrich Schuchardt | 27380d8 | 2022-10-22 11:32:34 +0200 | [diff] [blame] | 558 | if ((unsigned int)file < MAX_FILES) |
Pali Rohár | 974f483 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 559 | console_flush(file); |
| 560 | } |
| 561 | #endif |
| 562 | |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 563 | int fprintf(int file, const char *fmt, ...) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 564 | { |
| 565 | va_list args; |
| 566 | uint i; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 567 | char printbuffer[CONFIG_SYS_PBSIZE]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 568 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 569 | va_start(args, fmt); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 570 | |
| 571 | /* For this to work, printbuffer must be larger than |
| 572 | * anything we ever want to print. |
| 573 | */ |
Sonny Rao | 068af6f | 2011-10-10 09:22:31 +0000 | [diff] [blame] | 574 | i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 575 | va_end(args); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 576 | |
| 577 | /* Send to desired file */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 578 | fputs(file, printbuffer); |
Wolfgang Denk | d9c2725 | 2010-06-20 17:14:14 +0200 | [diff] [blame] | 579 | return i; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/ |
| 583 | |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 584 | int getchar(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 585 | { |
Patrick Delaunay | 1e99371 | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 586 | int ch; |
| 587 | |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 588 | if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE)) |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 589 | return 0; |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 590 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 591 | if (!gd->have_console) |
| 592 | return 0; |
| 593 | |
Patrick Delaunay | 1e99371 | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 594 | ch = console_record_getc(); |
| 595 | if (ch != -1) |
| 596 | return ch; |
Simon Glass | 9854a87 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 597 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 598 | if (gd->flags & GD_FLG_DEVINIT) { |
| 599 | /* Get from the standard input */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 600 | return fgetc(stdin); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 604 | return serial_getc(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 607 | int tstc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 608 | { |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 609 | if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE)) |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 610 | return 0; |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 611 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 612 | if (!gd->have_console) |
| 613 | return 0; |
Patrick Delaunay | 1e99371 | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 614 | |
| 615 | if (console_record_tstc()) |
| 616 | return 1; |
| 617 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 618 | if (gd->flags & GD_FLG_DEVINIT) { |
| 619 | /* Test the standard input */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 620 | return ftstc(stdin); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /* Send directly to the handler */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 624 | return serial_tstc(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Siarhei Siamashka | 2766966 | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 627 | #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0 |
| 628 | #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1 |
| 629 | |
Simon Glass | 8f92558 | 2016-10-17 20:12:36 -0600 | [diff] [blame] | 630 | #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER) |
Rasmus Villemoes | cff2963 | 2022-05-03 14:37:39 +0200 | [diff] [blame] | 631 | #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_VAL(PRE_CON_BUF_SZ)) |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 632 | |
| 633 | static void pre_console_putc(const char c) |
| 634 | { |
Simon Glass | 4e6bafa | 2017-06-15 21:37:52 -0600 | [diff] [blame] | 635 | char *buffer; |
| 636 | |
Rasmus Villemoes | 04a20ca | 2022-05-03 15:13:27 +0200 | [diff] [blame] | 637 | if (gd->precon_buf_idx < 0) |
| 638 | return; |
| 639 | |
Rasmus Villemoes | cff2963 | 2022-05-03 14:37:39 +0200 | [diff] [blame] | 640 | buffer = map_sysmem(CONFIG_VAL(PRE_CON_BUF_ADDR), CONFIG_VAL(PRE_CON_BUF_SZ)); |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 641 | |
| 642 | buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c; |
Simon Glass | 4e6bafa | 2017-06-15 21:37:52 -0600 | [diff] [blame] | 643 | |
| 644 | unmap_sysmem(buffer); |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Soeren Moch | be135cc | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 647 | static void pre_console_puts(const char *s) |
| 648 | { |
Rasmus Villemoes | 04a20ca | 2022-05-03 15:13:27 +0200 | [diff] [blame] | 649 | if (gd->precon_buf_idx < 0) |
| 650 | return; |
| 651 | |
Soeren Moch | be135cc | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 652 | while (*s) |
| 653 | pre_console_putc(*s++); |
| 654 | } |
| 655 | |
Siarhei Siamashka | 2766966 | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 656 | static void print_pre_console_buffer(int flushpoint) |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 657 | { |
Rasmus Villemoes | 04a20ca | 2022-05-03 15:13:27 +0200 | [diff] [blame] | 658 | long in = 0, out = 0; |
Rasmus Villemoes | cff2963 | 2022-05-03 14:37:39 +0200 | [diff] [blame] | 659 | char buf_out[CONFIG_VAL(PRE_CON_BUF_SZ) + 1]; |
Simon Glass | 4e6bafa | 2017-06-15 21:37:52 -0600 | [diff] [blame] | 660 | char *buf_in; |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 661 | |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 662 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 663 | return; |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 664 | |
Rasmus Villemoes | cff2963 | 2022-05-03 14:37:39 +0200 | [diff] [blame] | 665 | buf_in = map_sysmem(CONFIG_VAL(PRE_CON_BUF_ADDR), CONFIG_VAL(PRE_CON_BUF_SZ)); |
| 666 | if (gd->precon_buf_idx > CONFIG_VAL(PRE_CON_BUF_SZ)) |
| 667 | in = gd->precon_buf_idx - CONFIG_VAL(PRE_CON_BUF_SZ); |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 668 | |
Hans de Goede | a8552c7 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 669 | while (in < gd->precon_buf_idx) |
| 670 | buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)]; |
Simon Glass | 4e6bafa | 2017-06-15 21:37:52 -0600 | [diff] [blame] | 671 | unmap_sysmem(buf_in); |
Hans de Goede | a8552c7 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 672 | |
| 673 | buf_out[out] = 0; |
| 674 | |
Rasmus Villemoes | 04a20ca | 2022-05-03 15:13:27 +0200 | [diff] [blame] | 675 | gd->precon_buf_idx = -1; |
Hans de Goede | a8552c7 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 676 | switch (flushpoint) { |
| 677 | case PRE_CONSOLE_FLUSHPOINT1_SERIAL: |
| 678 | puts(buf_out); |
| 679 | break; |
| 680 | case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL: |
Simon Glass | 493a4c8 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 681 | console_puts_select(stdout, false, buf_out); |
Hans de Goede | a8552c7 | 2015-05-05 13:13:36 +0200 | [diff] [blame] | 682 | break; |
| 683 | } |
Rasmus Villemoes | 04a20ca | 2022-05-03 15:13:27 +0200 | [diff] [blame] | 684 | gd->precon_buf_idx = in; |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 685 | } |
| 686 | #else |
| 687 | static inline void pre_console_putc(const char c) {} |
Soeren Moch | be135cc | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 688 | static inline void pre_console_puts(const char *s) {} |
Siarhei Siamashka | 2766966 | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 689 | static inline void print_pre_console_buffer(int flushpoint) {} |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 690 | #endif |
| 691 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 692 | void putc(const char c) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 693 | { |
Patrick Delaunay | 93cdb52 | 2020-11-27 11:20:56 +0100 | [diff] [blame] | 694 | if (!gd) |
| 695 | return; |
Patrick Delaunay | 1e99371 | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 696 | |
| 697 | console_record_putc(c); |
| 698 | |
Simon Glass | 64e9b4f | 2017-12-04 13:48:19 -0700 | [diff] [blame] | 699 | /* sandbox can send characters to stdout before it has a console */ |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 700 | if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) { |
Simon Glass | 64e9b4f | 2017-12-04 13:48:19 -0700 | [diff] [blame] | 701 | os_putc(c); |
| 702 | return; |
| 703 | } |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 704 | |
Simon Glass | d6ea530 | 2015-06-23 15:38:33 -0600 | [diff] [blame] | 705 | /* if we don't have a console yet, use the debug UART */ |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 706 | if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) { |
Simon Glass | d6ea530 | 2015-06-23 15:38:33 -0600 | [diff] [blame] | 707 | printch(c); |
| 708 | return; |
| 709 | } |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 710 | |
| 711 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) { |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 712 | if (!(gd->flags & GD_FLG_DEVINIT)) |
| 713 | pre_console_putc(c); |
wdenk | f6e20fc | 2004-02-08 19:38:38 +0000 | [diff] [blame] | 714 | return; |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 715 | } |
wdenk | a6cccae | 2004-02-06 21:48:22 +0000 | [diff] [blame] | 716 | |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 717 | if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE)) |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 718 | return; |
Mark Jackson | f5c3ba7 | 2008-08-25 19:21:30 +0100 | [diff] [blame] | 719 | |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 720 | if (!gd->have_console) |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 721 | return pre_console_putc(c); |
Graeme Russ | e3e454c | 2011-08-29 02:14:05 +0000 | [diff] [blame] | 722 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 723 | if (gd->flags & GD_FLG_DEVINIT) { |
| 724 | /* Send to the standard output */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 725 | fputc(stdout, c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 726 | } else { |
| 727 | /* Send directly to the handler */ |
Siarhei Siamashka | 2766966 | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 728 | pre_console_putc(c); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 729 | serial_putc(c); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 733 | void puts(const char *s) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 734 | { |
Patrick Delaunay | 93cdb52 | 2020-11-27 11:20:56 +0100 | [diff] [blame] | 735 | if (!gd) |
| 736 | return; |
Patrick Delaunay | 1e99371 | 2020-12-18 12:46:45 +0100 | [diff] [blame] | 737 | |
| 738 | console_record_puts(s); |
| 739 | |
Simon Glass | 36bcea6 | 2018-11-15 18:44:04 -0700 | [diff] [blame] | 740 | /* sandbox can send characters to stdout before it has a console */ |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 741 | if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) { |
Simon Glass | 36bcea6 | 2018-11-15 18:44:04 -0700 | [diff] [blame] | 742 | os_puts(s); |
| 743 | return; |
| 744 | } |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 745 | |
| 746 | if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) { |
Soeren Moch | be135cc | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 747 | while (*s) { |
| 748 | int ch = *s++; |
| 749 | |
| 750 | printch(ch); |
| 751 | } |
| 752 | return; |
| 753 | } |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 754 | |
| 755 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) { |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 756 | if (!(gd->flags & GD_FLG_DEVINIT)) |
| 757 | pre_console_puts(s); |
Soeren Moch | be135cc | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 758 | return; |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 759 | } |
Soeren Moch | be135cc | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 760 | |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 761 | if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE)) |
Soeren Moch | be135cc | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 762 | return; |
Soeren Moch | be135cc | 2017-11-04 16:14:09 +0100 | [diff] [blame] | 763 | |
| 764 | if (!gd->have_console) |
| 765 | return pre_console_puts(s); |
| 766 | |
| 767 | if (gd->flags & GD_FLG_DEVINIT) { |
| 768 | /* Send to the standard output */ |
| 769 | fputs(stdout, s); |
| 770 | } else { |
| 771 | /* Send directly to the handler */ |
| 772 | pre_console_puts(s); |
| 773 | serial_puts(s); |
| 774 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Pali Rohár | 974f483 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 777 | #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT |
| 778 | void flush(void) |
| 779 | { |
| 780 | if (!gd) |
| 781 | return; |
| 782 | |
| 783 | /* sandbox can send characters to stdout before it has a console */ |
| 784 | if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) { |
| 785 | os_flush(); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) |
| 790 | return; |
| 791 | |
| 792 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) |
| 793 | return; |
| 794 | |
| 795 | if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE)) |
| 796 | return; |
| 797 | |
| 798 | if (!gd->have_console) |
| 799 | return; |
| 800 | |
| 801 | if (gd->flags & GD_FLG_DEVINIT) { |
| 802 | /* Send to the standard output */ |
| 803 | fflush(stdout); |
Pali Rohár | 78b5243 | 2022-09-05 11:31:19 +0200 | [diff] [blame] | 804 | } else { |
| 805 | /* Send directly to the handler */ |
| 806 | serial_flush(); |
Pali Rohár | 974f483 | 2022-09-05 11:31:17 +0200 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | #endif |
| 810 | |
Simon Glass | 9854a87 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 811 | #ifdef CONFIG_CONSOLE_RECORD |
| 812 | int console_record_init(void) |
| 813 | { |
| 814 | int ret; |
| 815 | |
Heinrich Schuchardt | a3a9e04 | 2020-02-12 18:23:49 +0100 | [diff] [blame] | 816 | ret = membuff_new((struct membuff *)&gd->console_out, |
Simon Glass | 8ce465e | 2021-10-23 17:26:03 -0600 | [diff] [blame] | 817 | gd->flags & GD_FLG_RELOC ? |
| 818 | CONFIG_CONSOLE_RECORD_OUT_SIZE : |
| 819 | CONFIG_CONSOLE_RECORD_OUT_SIZE_F); |
Simon Glass | 9854a87 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 820 | if (ret) |
| 821 | return ret; |
Heinrich Schuchardt | a3a9e04 | 2020-02-12 18:23:49 +0100 | [diff] [blame] | 822 | ret = membuff_new((struct membuff *)&gd->console_in, |
| 823 | CONFIG_CONSOLE_RECORD_IN_SIZE); |
Simon Glass | 9854a87 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 824 | |
Ion Agorria | 90087dd | 2024-01-05 09:22:09 +0200 | [diff] [blame] | 825 | /* Start recording from the beginning */ |
| 826 | gd->flags |= GD_FLG_RECORD; |
| 827 | |
Simon Glass | 9854a87 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 828 | return ret; |
| 829 | } |
| 830 | |
| 831 | void console_record_reset(void) |
| 832 | { |
Heinrich Schuchardt | a3a9e04 | 2020-02-12 18:23:49 +0100 | [diff] [blame] | 833 | membuff_purge((struct membuff *)&gd->console_out); |
| 834 | membuff_purge((struct membuff *)&gd->console_in); |
Simon Glass | c1a2bb4 | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 835 | gd->flags &= ~GD_FLG_RECORD_OVF; |
Simon Glass | 9854a87 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Simon Glass | bd34715 | 2020-07-28 19:41:11 -0600 | [diff] [blame] | 838 | int console_record_reset_enable(void) |
Simon Glass | 9854a87 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 839 | { |
| 840 | console_record_reset(); |
| 841 | gd->flags |= GD_FLG_RECORD; |
Simon Glass | bd34715 | 2020-07-28 19:41:11 -0600 | [diff] [blame] | 842 | |
| 843 | return 0; |
Simon Glass | 9854a87 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 844 | } |
Simon Glass | b612312 | 2020-01-27 08:49:54 -0700 | [diff] [blame] | 845 | |
| 846 | int console_record_readline(char *str, int maxlen) |
| 847 | { |
Simon Glass | c1a2bb4 | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 848 | if (gd->flags & GD_FLG_RECORD_OVF) |
| 849 | return -ENOSPC; |
Simon Glass | 88ae69f | 2024-08-22 07:57:47 -0600 | [diff] [blame] | 850 | if (console_record_isempty()) |
| 851 | return -ENOENT; |
Simon Glass | c1a2bb4 | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 852 | |
Heinrich Schuchardt | a3a9e04 | 2020-02-12 18:23:49 +0100 | [diff] [blame] | 853 | return membuff_readline((struct membuff *)&gd->console_out, str, |
Ion Agorria | e58bafc | 2024-01-05 09:22:10 +0200 | [diff] [blame] | 854 | maxlen, '\0', false); |
Simon Glass | b612312 | 2020-01-27 08:49:54 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | int console_record_avail(void) |
| 858 | { |
Heinrich Schuchardt | a3a9e04 | 2020-02-12 18:23:49 +0100 | [diff] [blame] | 859 | return membuff_avail((struct membuff *)&gd->console_out); |
Simon Glass | b612312 | 2020-01-27 08:49:54 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Ion Agorria | 9ce75f4 | 2024-01-05 09:22:08 +0200 | [diff] [blame] | 862 | bool console_record_isempty(void) |
| 863 | { |
| 864 | return membuff_isempty((struct membuff *)&gd->console_out); |
| 865 | } |
| 866 | |
Steffen Jaeckel | 25c8b9f | 2021-07-08 15:57:40 +0200 | [diff] [blame] | 867 | int console_in_puts(const char *str) |
| 868 | { |
| 869 | return membuff_put((struct membuff *)&gd->console_in, str, strlen(str)); |
| 870 | } |
| 871 | |
Simon Glass | 9854a87 | 2015-11-08 23:47:48 -0700 | [diff] [blame] | 872 | #endif |
| 873 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 874 | /* test if ctrl-c was pressed */ |
| 875 | static int ctrlc_disabled = 0; /* see disable_ctrl() */ |
| 876 | static int ctrlc_was_pressed = 0; |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 877 | int ctrlc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 878 | { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 879 | if (!ctrlc_disabled && gd->have_console) { |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 880 | if (tstc()) { |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 881 | switch (getchar()) { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 882 | case 0x03: /* ^C - Control C */ |
| 883 | ctrlc_was_pressed = 1; |
| 884 | return 1; |
| 885 | default: |
| 886 | break; |
| 887 | } |
| 888 | } |
| 889 | } |
Simon Glass | 8969ea3 | 2014-09-14 12:40:15 -0600 | [diff] [blame] | 890 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 891 | return 0; |
| 892 | } |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 893 | /* Reads user's confirmation. |
| 894 | Returns 1 if user's input is "y", "Y", "yes" or "YES" |
| 895 | */ |
| 896 | int confirm_yesno(void) |
| 897 | { |
| 898 | int i; |
| 899 | char str_input[5]; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 900 | |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 901 | /* Flush input */ |
| 902 | while (tstc()) |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 903 | getchar(); |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 904 | i = 0; |
| 905 | while (i < sizeof(str_input)) { |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 906 | str_input[i] = getchar(); |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 907 | putc(str_input[i]); |
| 908 | if (str_input[i] == '\r') |
| 909 | break; |
| 910 | i++; |
| 911 | } |
| 912 | putc('\n'); |
| 913 | if (strncmp(str_input, "y\r", 2) == 0 || |
| 914 | strncmp(str_input, "Y\r", 2) == 0 || |
| 915 | strncmp(str_input, "yes\r", 4) == 0 || |
| 916 | strncmp(str_input, "YES\r", 4) == 0) |
| 917 | return 1; |
| 918 | return 0; |
| 919 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 920 | /* pass 1 to disable ctrlc() checking, 0 to enable. |
| 921 | * returns previous state |
| 922 | */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 923 | int disable_ctrlc(int disable) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 924 | { |
| 925 | int prev = ctrlc_disabled; /* save previous state */ |
| 926 | |
| 927 | ctrlc_disabled = disable; |
| 928 | return prev; |
| 929 | } |
| 930 | |
| 931 | int had_ctrlc (void) |
| 932 | { |
| 933 | return ctrlc_was_pressed; |
| 934 | } |
| 935 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 936 | void clear_ctrlc(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 937 | { |
| 938 | ctrlc_was_pressed = 0; |
| 939 | } |
| 940 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 941 | /** U-Boot INIT FUNCTIONS *************************************************/ |
| 942 | |
Andy Shevchenko | 3232487 | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 943 | struct stdio_dev *console_search_dev(int flags, const char *name) |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 944 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 945 | struct stdio_dev *dev; |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 946 | |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 947 | dev = stdio_get_by_name(name); |
Simon Glass | a2931b3 | 2016-02-06 14:31:37 -0700 | [diff] [blame] | 948 | #ifdef CONFIG_VIDCONSOLE_AS_LCD |
Patrick Delaunay | 27b5b9e | 2020-07-01 14:56:10 +0200 | [diff] [blame] | 949 | if (!dev && !strcmp(name, CONFIG_VIDCONSOLE_AS_NAME)) |
Simon Glass | a2931b3 | 2016-02-06 14:31:37 -0700 | [diff] [blame] | 950 | dev = stdio_get_by_name("vidconsole"); |
| 951 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 952 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 953 | if (dev && (dev->flags & flags)) |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 954 | return dev; |
| 955 | |
| 956 | return NULL; |
| 957 | } |
| 958 | |
Mike Frysinger | d7be305 | 2010-10-20 07:18:03 -0400 | [diff] [blame] | 959 | int console_assign(int file, const char *devname) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 960 | { |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 961 | int flag; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 962 | struct stdio_dev *dev; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 963 | |
| 964 | /* Check for valid file */ |
Andy Shevchenko | 7b9ca3f | 2021-02-11 17:09:37 +0200 | [diff] [blame] | 965 | flag = stdio_file_to_flags(file); |
| 966 | if (flag < 0) |
| 967 | return flag; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 968 | |
| 969 | /* Check for valid device name */ |
| 970 | |
Andy Shevchenko | 3232487 | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 971 | dev = console_search_dev(flag, devname); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 972 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 973 | if (dev) |
| 974 | return console_setfile(file, dev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 975 | |
| 976 | return -1; |
| 977 | } |
| 978 | |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 979 | /* return true if the 'silent' flag is removed */ |
| 980 | static bool console_update_silent(void) |
Chris Packham | 43e0a3d | 2016-09-23 15:59:43 +1200 | [diff] [blame] | 981 | { |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 982 | unsigned long flags = gd->flags; |
| 983 | |
| 984 | if (!IS_ENABLED(CONFIG_SILENT_CONSOLE)) |
| 985 | return false; |
| 986 | |
Harald Seiler | 33965c7 | 2022-07-06 13:19:10 +0200 | [diff] [blame] | 987 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE_UNTIL_ENV) && !(gd->flags & GD_FLG_ENV_READY)) { |
| 988 | gd->flags |= GD_FLG_SILENT; |
| 989 | return false; |
| 990 | } |
| 991 | |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 992 | if (env_get("silent")) { |
Chris Packham | 43e0a3d | 2016-09-23 15:59:43 +1200 | [diff] [blame] | 993 | gd->flags |= GD_FLG_SILENT; |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 994 | return false; |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 995 | } |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 996 | |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 997 | gd->flags &= ~GD_FLG_SILENT; |
| 998 | |
| 999 | return !!(flags & GD_FLG_SILENT); |
Chris Packham | 43e0a3d | 2016-09-23 15:59:43 +1200 | [diff] [blame] | 1000 | } |
| 1001 | |
Simon Glass | b089538 | 2017-06-15 21:37:50 -0600 | [diff] [blame] | 1002 | int console_announce_r(void) |
| 1003 | { |
| 1004 | #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER) |
| 1005 | char buf[DISPLAY_OPTIONS_BANNER_LENGTH]; |
| 1006 | |
| 1007 | display_options_get_banner(false, buf, sizeof(buf)); |
| 1008 | |
Simon Glass | 493a4c8 | 2020-07-02 21:12:13 -0600 | [diff] [blame] | 1009 | console_puts_select(stdout, false, buf); |
Simon Glass | b089538 | 2017-06-15 21:37:50 -0600 | [diff] [blame] | 1010 | #endif |
| 1011 | |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1015 | /* Called before relocation - use serial functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1016 | int console_init_f(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1017 | { |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1018 | gd->have_console = 1; |
wdenk | f72da34 | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 1019 | |
Chris Packham | 43e0a3d | 2016-09-23 15:59:43 +1200 | [diff] [blame] | 1020 | console_update_silent(); |
wdenk | f72da34 | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 1021 | |
Siarhei Siamashka | 2766966 | 2015-01-08 09:02:31 +0200 | [diff] [blame] | 1022 | print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL); |
Graeme Russ | 9558b48 | 2011-09-01 00:48:27 +0000 | [diff] [blame] | 1023 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1024 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Simon Glass | cde03fa | 2023-10-01 19:15:23 -0600 | [diff] [blame] | 1027 | int console_clear(void) |
| 1028 | { |
| 1029 | /* |
| 1030 | * Send clear screen and home |
| 1031 | * |
| 1032 | * FIXME(Heinrich Schuchardt <xypron.glpk@gmx.de>): This should go |
| 1033 | * through an API and only be written to serial terminals, not video |
| 1034 | * displays |
| 1035 | */ |
| 1036 | printf(CSI "2J" CSI "1;1H"); |
| 1037 | if (IS_ENABLED(CONFIG_VIDEO_ANSI)) |
| 1038 | return 0; |
| 1039 | |
| 1040 | if (IS_ENABLED(CONFIG_VIDEO)) { |
| 1041 | struct udevice *dev; |
| 1042 | int ret; |
| 1043 | |
| 1044 | ret = uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev); |
| 1045 | if (ret) |
| 1046 | return ret; |
| 1047 | ret = vidconsole_clear_and_reset(dev); |
| 1048 | if (ret) |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
Patrice Chotard | 9152a51 | 2024-01-17 13:37:13 +0100 | [diff] [blame] | 1055 | static char *get_stdio(const u8 std) |
| 1056 | { |
| 1057 | return stdio_devices[std] ? stdio_devices[std]->name : "No devices available!"; |
| 1058 | } |
| 1059 | |
Bin Meng | 75bfc6f | 2023-07-23 12:40:35 +0800 | [diff] [blame] | 1060 | static void stdio_print_current_devices(void) |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 1061 | { |
Patrice Chotard | 9152a51 | 2024-01-17 13:37:13 +0100 | [diff] [blame] | 1062 | char *stdinname = NULL; |
| 1063 | char *stdoutname = NULL; |
| 1064 | char *stderrname = NULL; |
Bin Meng | f30fd55 | 2023-07-23 12:40:36 +0800 | [diff] [blame] | 1065 | |
Bin Meng | 6b343ab | 2023-07-23 12:40:37 +0800 | [diff] [blame] | 1066 | if (CONFIG_IS_ENABLED(CONSOLE_MUX) && |
| 1067 | CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)) { |
| 1068 | /* stdin stdout and stderr are in environment */ |
| 1069 | stdinname = env_get("stdin"); |
| 1070 | stdoutname = env_get("stdout"); |
| 1071 | stderrname = env_get("stderr"); |
Bin Meng | 6b343ab | 2023-07-23 12:40:37 +0800 | [diff] [blame] | 1072 | } |
Bin Meng | f30fd55 | 2023-07-23 12:40:36 +0800 | [diff] [blame] | 1073 | |
Patrice Chotard | 9152a51 | 2024-01-17 13:37:13 +0100 | [diff] [blame] | 1074 | stdinname = stdinname ? : get_stdio(stdin); |
| 1075 | stdoutname = stdoutname ? : get_stdio(stdout); |
| 1076 | stderrname = stderrname ? : get_stdio(stderr); |
| 1077 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 1078 | /* Print information */ |
| 1079 | puts("In: "); |
Bin Meng | f30fd55 | 2023-07-23 12:40:36 +0800 | [diff] [blame] | 1080 | printf("%s\n", stdinname); |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 1081 | |
| 1082 | puts("Out: "); |
Bin Meng | f30fd55 | 2023-07-23 12:40:36 +0800 | [diff] [blame] | 1083 | printf("%s\n", stdoutname); |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 1084 | |
| 1085 | puts("Err: "); |
Bin Meng | f30fd55 | 2023-07-23 12:40:36 +0800 | [diff] [blame] | 1086 | printf("%s\n", stderrname); |
Jean-Christophe PLAGNIOL-VILLARD | 7e3be7c | 2009-05-16 12:14:55 +0200 | [diff] [blame] | 1087 | } |
| 1088 | |
Simon Glass | b026542 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 1089 | #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1090 | /* Called after the relocation - use desired console functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1091 | int console_init_r(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1092 | { |
| 1093 | char *stdinname, *stdoutname, *stderrname; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 1094 | struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1095 | int i; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1096 | int iomux_err = 0; |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1097 | int flushpoint; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1098 | |
Patrick Delaunay | bf46be7 | 2019-08-02 14:58:09 +0200 | [diff] [blame] | 1099 | /* update silent for env loaded from flash (initr_env) */ |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1100 | if (console_update_silent()) |
| 1101 | flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL; |
| 1102 | else |
| 1103 | flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL; |
Patrick Delaunay | bf46be7 | 2019-08-02 14:58:09 +0200 | [diff] [blame] | 1104 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1105 | /* set default handlers at first */ |
Martin Dorwig | 49cad54 | 2015-01-26 15:22:54 -0700 | [diff] [blame] | 1106 | gd->jt->getc = serial_getc; |
| 1107 | gd->jt->tstc = serial_tstc; |
| 1108 | gd->jt->putc = serial_putc; |
| 1109 | gd->jt->puts = serial_puts; |
| 1110 | gd->jt->printf = serial_printf; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1111 | |
| 1112 | /* stdin stdout and stderr are in environment */ |
| 1113 | /* scan for it */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1114 | stdinname = env_get("stdin"); |
| 1115 | stdoutname = env_get("stdout"); |
| 1116 | stderrname = env_get("stderr"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1117 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1118 | if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */ |
Andy Shevchenko | 3232487 | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 1119 | inputdev = console_search_dev(DEV_FLAGS_INPUT, stdinname); |
| 1120 | outputdev = console_search_dev(DEV_FLAGS_OUTPUT, stdoutname); |
| 1121 | errdev = console_search_dev(DEV_FLAGS_OUTPUT, stderrname); |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 1122 | if (CONFIG_IS_ENABLED(CONSOLE_MUX)) { |
| 1123 | iomux_err = iomux_doenv(stdin, stdinname); |
| 1124 | iomux_err += iomux_doenv(stdout, stdoutname); |
| 1125 | iomux_err += iomux_doenv(stderr, stderrname); |
| 1126 | if (!iomux_err) |
| 1127 | /* Successful, so skip all the code below. */ |
| 1128 | goto done; |
| 1129 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1130 | } |
| 1131 | /* if the devices are overwritten or not found, use default device */ |
| 1132 | if (inputdev == NULL) { |
Andy Shevchenko | 3232487 | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 1133 | inputdev = console_search_dev(DEV_FLAGS_INPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1134 | } |
| 1135 | if (outputdev == NULL) { |
Andy Shevchenko | 3232487 | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 1136 | outputdev = console_search_dev(DEV_FLAGS_OUTPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1137 | } |
| 1138 | if (errdev == NULL) { |
Andy Shevchenko | 3232487 | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 1139 | errdev = console_search_dev(DEV_FLAGS_OUTPUT, "serial"); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1140 | } |
| 1141 | /* Initializes output console first */ |
| 1142 | if (outputdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1143 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 1144 | console_doenv(stdout, outputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1145 | } |
| 1146 | if (errdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1147 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 1148 | console_doenv(stderr, errdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1149 | } |
| 1150 | if (inputdev != NULL) { |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1151 | /* need to set a console if not done above. */ |
Jean-Christophe PLAGNIOL-VILLARD | 5f03201 | 2009-02-01 17:07:52 +0100 | [diff] [blame] | 1152 | console_doenv(stdin, inputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1155 | done: |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 1156 | |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 1157 | if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET)) |
| 1158 | stdio_print_current_devices(); |
| 1159 | |
Simon Glass | a2931b3 | 2016-02-06 14:31:37 -0700 | [diff] [blame] | 1160 | #ifdef CONFIG_VIDCONSOLE_AS_LCD |
Patrick Delaunay | 27b5b9e | 2020-07-01 14:56:10 +0200 | [diff] [blame] | 1161 | if (strstr(stdoutname, CONFIG_VIDCONSOLE_AS_NAME)) |
Anatolij Gustschin | 22b897a | 2020-05-23 17:11:20 +0200 | [diff] [blame] | 1162 | printf("Warning: Please change '%s' to 'vidconsole' in stdout/stderr environment vars\n", |
Patrick Delaunay | 27b5b9e | 2020-07-01 14:56:10 +0200 | [diff] [blame] | 1163 | CONFIG_VIDCONSOLE_AS_NAME); |
Simon Glass | a2931b3 | 2016-02-06 14:31:37 -0700 | [diff] [blame] | 1164 | #endif |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1165 | |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 1166 | if (IS_ENABLED(CONFIG_SYS_CONSOLE_ENV_OVERWRITE)) { |
| 1167 | /* set the environment variables (will overwrite previous env settings) */ |
| 1168 | for (i = 0; i < MAX_FILES; i++) |
| 1169 | env_set(stdio_names[i], stdio_devices[i]->name); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1170 | } |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1171 | |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 1172 | gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */ |
| 1173 | |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1174 | print_pre_console_buffer(flushpoint); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1175 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Simon Glass | b026542 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 1178 | #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */ |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1179 | |
| 1180 | /* Called after the relocation - use desired console functions */ |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1181 | int console_init_r(void) |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1182 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 1183 | struct stdio_dev *inputdev = NULL, *outputdev = NULL; |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 1184 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 1185 | struct list_head *list = stdio_get_list(); |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 1186 | struct list_head *pos; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 1187 | struct stdio_dev *dev; |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1188 | int flushpoint; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1189 | |
Patrick Delaunay | bf46be7 | 2019-08-02 14:58:09 +0200 | [diff] [blame] | 1190 | /* update silent for env loaded from flash (initr_env) */ |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1191 | if (console_update_silent()) |
| 1192 | flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL; |
| 1193 | else |
| 1194 | flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL; |
Chris Packham | 43e0a3d | 2016-09-23 15:59:43 +1200 | [diff] [blame] | 1195 | |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1196 | /* |
| 1197 | * suppress all output if splash screen is enabled and we have |
Anatolij Gustschin | a749081 | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 1198 | * a bmp to display. We redirect the output from frame buffer |
| 1199 | * console to serial console in this case or suppress it if |
| 1200 | * "silent" mode was requested. |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1201 | */ |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 1202 | if (IS_ENABLED(CONFIG_SPLASH_SCREEN) && env_get("splashimage")) { |
Anatolij Gustschin | a749081 | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 1203 | if (!(gd->flags & GD_FLG_SILENT)) |
Andy Shevchenko | 3232487 | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 1204 | outputdev = console_search_dev (DEV_FLAGS_OUTPUT, "serial"); |
Anatolij Gustschin | a749081 | 2010-03-16 15:29:33 +0100 | [diff] [blame] | 1205 | } |
wdenk | f72da34 | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 1206 | |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1207 | /* Scan devices looking for input and output devices */ |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 1208 | list_for_each(pos, list) { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 1209 | dev = list_entry(pos, struct stdio_dev, list); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1210 | |
| 1211 | if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) { |
| 1212 | inputdev = dev; |
| 1213 | } |
| 1214 | if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) { |
| 1215 | outputdev = dev; |
| 1216 | } |
Jean-Christophe PLAGNIOL-VILLARD | c1de7a6 | 2008-08-31 04:24:55 +0200 | [diff] [blame] | 1217 | if(inputdev && outputdev) |
| 1218 | break; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | /* Initializes output console first */ |
| 1222 | if (outputdev != NULL) { |
Andy Shevchenko | 09d8f07 | 2021-02-11 17:09:39 +0200 | [diff] [blame] | 1223 | console_setfile_and_devices(stdout, outputdev); |
| 1224 | console_setfile_and_devices(stderr, outputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | /* Initializes input console */ |
Andy Shevchenko | 09d8f07 | 2021-02-11 17:09:39 +0200 | [diff] [blame] | 1228 | if (inputdev != NULL) |
| 1229 | console_setfile_and_devices(stdin, inputdev); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1230 | |
Patrick Delaunay | c04f856 | 2020-12-18 12:46:43 +0100 | [diff] [blame] | 1231 | if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET)) |
| 1232 | stdio_print_current_devices(); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1233 | |
| 1234 | /* Setting environment variables */ |
Tom Rini | 27b4225 | 2018-05-03 09:12:26 -0400 | [diff] [blame] | 1235 | for (i = 0; i < MAX_FILES; i++) { |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 1236 | env_set(stdio_names[i], stdio_devices[i]->name); |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 1239 | gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */ |
| 1240 | |
Patrick Delaunay | 13551b9 | 2019-08-02 14:58:10 +0200 | [diff] [blame] | 1241 | print_pre_console_buffer(flushpoint); |
Jean-Christophe PLAGNIOL-VILLARD | ec6f149 | 2009-02-01 17:07:51 +0100 | [diff] [blame] | 1242 | return 0; |
wdenk | 47d1a6e | 2002-11-03 00:01:44 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Simon Glass | b026542 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 1245 | #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */ |