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