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