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