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