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