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