xref: /openbmc/u-boot/common/console.c (revision dd5577703b99170e88fad3994c7865d1e9bc2874)
1 /*
2  * (C) Copyright 2000
3  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <console.h>
10 #include <debug_uart.h>
11 #include <stdarg.h>
12 #include <iomux.h>
13 #include <malloc.h>
14 #include <os.h>
15 #include <serial.h>
16 #include <stdio_dev.h>
17 #include <exports.h>
18 #include <environment.h>
19 
20 DECLARE_GLOBAL_DATA_PTR;
21 
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 
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 
87 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
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  */
93 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
94 extern int overwrite_console(void);
95 #define OVERWRITE_CONSOLE overwrite_console()
96 #else
97 #define OVERWRITE_CONSOLE 0
98 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
99 
100 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
101 
102 static int console_setfile(int file, struct stdio_dev * dev)
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) {
115 			error = dev->start(dev);
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:
130 			gd->jt->getc = getc;
131 			gd->jt->tstc = tstc;
132 			break;
133 		case stdout:
134 			gd->jt->putc  = putc;
135 			gd->jt->puts  = puts;
136 			gd->jt->printf = printf;
137 			break;
138 		}
139 		break;
140 
141 	default:		/* Invalid file ID */
142 		error = -1;
143 	}
144 	return error;
145 }
146 
147 #if defined(CONFIG_CONSOLE_MUX)
148 /** Console I/O multiplexing *******************************************/
149 
150 static struct stdio_dev *tstcdev;
151 struct stdio_dev **console_devices[MAX_FILES];
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  */
160 static int console_getc(int file)
161 {
162 	unsigned char ret;
163 
164 	/* This is never called with testcdev == NULL */
165 	ret = tstcdev->getc(tstcdev);
166 	tstcdev = NULL;
167 	return ret;
168 }
169 
170 static int console_tstc(int file)
171 {
172 	int i, ret;
173 	struct stdio_dev *dev;
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) {
179 			ret = dev->tstc(dev);
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 
192 static void console_putc(int file, const char c)
193 {
194 	int i;
195 	struct stdio_dev *dev;
196 
197 	for (i = 0; i < cd_count[file]; i++) {
198 		dev = console_devices[file][i];
199 		if (dev->putc != NULL)
200 			dev->putc(dev, c);
201 	}
202 }
203 
204 #ifdef CONFIG_PRE_CONSOLE_BUFFER
205 static void console_puts_noserial(int file, const char *s)
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];
212 		if (dev->puts != NULL && strcmp(dev->name, "serial") != 0)
213 			dev->puts(dev, s);
214 	}
215 }
216 #endif
217 
218 static void console_puts(int file, const char *s)
219 {
220 	int i;
221 	struct stdio_dev *dev;
222 
223 	for (i = 0; i < cd_count[file]; i++) {
224 		dev = console_devices[file][i];
225 		if (dev->puts != NULL)
226 			dev->puts(dev, s);
227 	}
228 }
229 
230 static inline void console_printdevs(int file)
231 {
232 	iomux_printdevs(file);
233 }
234 
235 static inline void console_doenv(int file, struct stdio_dev *dev)
236 {
237 	iomux_doenv(file, dev->name);
238 }
239 #else
240 static inline int console_getc(int file)
241 {
242 	return stdio_devices[file]->getc(stdio_devices[file]);
243 }
244 
245 static inline int console_tstc(int file)
246 {
247 	return stdio_devices[file]->tstc(stdio_devices[file]);
248 }
249 
250 static inline void console_putc(int file, const char c)
251 {
252 	stdio_devices[file]->putc(stdio_devices[file], c);
253 }
254 
255 #ifdef CONFIG_PRE_CONSOLE_BUFFER
256 static inline void console_puts_noserial(int file, const char *s)
257 {
258 	if (strcmp(stdio_devices[file]->name, "serial") != 0)
259 		stdio_devices[file]->puts(stdio_devices[file], s);
260 }
261 #endif
262 
263 static inline void console_puts(int file, const char *s)
264 {
265 	stdio_devices[file]->puts(stdio_devices[file], s);
266 }
267 
268 static inline void console_printdevs(int file)
269 {
270 	printf("%s\n", stdio_devices[file]->name);
271 }
272 
273 static inline void console_doenv(int file, struct stdio_dev *dev)
274 {
275 	console_setfile(file, dev);
276 }
277 #endif /* defined(CONFIG_CONSOLE_MUX) */
278 
279 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
280 
281 int serial_printf(const char *fmt, ...)
282 {
283 	va_list args;
284 	uint i;
285 	char printbuffer[CONFIG_SYS_PBSIZE];
286 
287 	va_start(args, fmt);
288 
289 	/* For this to work, printbuffer must be larger than
290 	 * anything we ever want to print.
291 	 */
292 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
293 	va_end(args);
294 
295 	serial_puts(printbuffer);
296 	return i;
297 }
298 
299 int fgetc(int file)
300 {
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)
312 				return console_getc(file);
313 			console_tstc(file);
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
323 		return console_getc(file);
324 #endif
325 	}
326 
327 	return -1;
328 }
329 
330 int ftstc(int file)
331 {
332 	if (file < MAX_FILES)
333 		return console_tstc(file);
334 
335 	return -1;
336 }
337 
338 void fputc(int file, const char c)
339 {
340 	if (file < MAX_FILES)
341 		console_putc(file, c);
342 }
343 
344 void fputs(int file, const char *s)
345 {
346 	if (file < MAX_FILES)
347 		console_puts(file, s);
348 }
349 
350 int fprintf(int file, const char *fmt, ...)
351 {
352 	va_list args;
353 	uint i;
354 	char printbuffer[CONFIG_SYS_PBSIZE];
355 
356 	va_start(args, fmt);
357 
358 	/* For this to work, printbuffer must be larger than
359 	 * anything we ever want to print.
360 	 */
361 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
362 	va_end(args);
363 
364 	/* Send to desired file */
365 	fputs(file, printbuffer);
366 	return i;
367 }
368 
369 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
370 
371 int getc(void)
372 {
373 #ifdef CONFIG_DISABLE_CONSOLE
374 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
375 		return 0;
376 #endif
377 
378 	if (!gd->have_console)
379 		return 0;
380 
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
390 	if (gd->flags & GD_FLG_DEVINIT) {
391 		/* Get from the standard input */
392 		return fgetc(stdin);
393 	}
394 
395 	/* Send directly to the handler */
396 	return serial_getc();
397 }
398 
399 int tstc(void)
400 {
401 #ifdef CONFIG_DISABLE_CONSOLE
402 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
403 		return 0;
404 #endif
405 
406 	if (!gd->have_console)
407 		return 0;
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
414 	if (gd->flags & GD_FLG_DEVINIT) {
415 		/* Test the standard input */
416 		return ftstc(stdin);
417 	}
418 
419 	/* Send directly to the handler */
420 	return serial_tstc();
421 }
422 
423 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL			0
424 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL	1
425 
426 #ifdef CONFIG_PRE_CONSOLE_BUFFER
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 
442 static void print_pre_console_buffer(int flushpoint)
443 {
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];
447 
448 	if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
449 		in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
450 
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 	}
464 }
465 #else
466 static inline void pre_console_putc(const char c) {}
467 static inline void pre_console_puts(const char *s) {}
468 static inline void print_pre_console_buffer(int flushpoint) {}
469 #endif
470 
471 void putc(const char c)
472 {
473 #ifdef CONFIG_SANDBOX
474 	/* sandbox can send characters to stdout before it has a console */
475 	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
476 		os_putc(c);
477 		return;
478 	}
479 #endif
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
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
491 #ifdef CONFIG_SILENT_CONSOLE
492 	if (gd->flags & GD_FLG_SILENT)
493 		return;
494 #endif
495 
496 #ifdef CONFIG_DISABLE_CONSOLE
497 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
498 		return;
499 #endif
500 
501 	if (!gd->have_console)
502 		return pre_console_putc(c);
503 
504 	if (gd->flags & GD_FLG_DEVINIT) {
505 		/* Send to the standard output */
506 		fputc(stdout, c);
507 	} else {
508 		/* Send directly to the handler */
509 		pre_console_putc(c);
510 		serial_putc(c);
511 	}
512 }
513 
514 void puts(const char *s)
515 {
516 #ifdef CONFIG_SANDBOX
517 	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
518 		os_puts(s);
519 		return;
520 	}
521 #endif
522 #ifdef CONFIG_DEBUG_UART
523 	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
524 		while (*s) {
525 			int ch = *s++;
526 
527 			printch(ch);
528 			if (ch == '\n')
529 				printch('\r');
530 		}
531 		return;
532 	}
533 #endif
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
538 #ifdef CONFIG_SILENT_CONSOLE
539 	if (gd->flags & GD_FLG_SILENT)
540 		return;
541 #endif
542 
543 #ifdef CONFIG_DISABLE_CONSOLE
544 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
545 		return;
546 #endif
547 
548 	if (!gd->have_console)
549 		return pre_console_puts(s);
550 
551 	if (gd->flags & GD_FLG_DEVINIT) {
552 		/* Send to the standard output */
553 		fputs(stdout, s);
554 	} else {
555 		/* Send directly to the handler */
556 		pre_console_puts(s);
557 		serial_puts(s);
558 	}
559 }
560 
561 int printf(const char *fmt, ...)
562 {
563 	va_list args;
564 	uint i;
565 	char printbuffer[CONFIG_SYS_PBSIZE];
566 
567 	va_start(args, fmt);
568 
569 	/* For this to work, printbuffer must be larger than
570 	 * anything we ever want to print.
571 	 */
572 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
573 	va_end(args);
574 
575 	/* Print the string */
576 	puts(printbuffer);
577 	return i;
578 }
579 
580 int vprintf(const char *fmt, va_list args)
581 {
582 	uint i;
583 	char printbuffer[CONFIG_SYS_PBSIZE];
584 
585 	/* For this to work, printbuffer must be larger than
586 	 * anything we ever want to print.
587 	 */
588 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
589 
590 	/* Print the string */
591 	puts(printbuffer);
592 	return i;
593 }
594 
595 #ifdef CONFIG_CONSOLE_RECORD
596 int console_record_init(void)
597 {
598 	int ret;
599 
600 	ret = membuff_new(&gd->console_out, CONFIG_CONSOLE_RECORD_OUT_SIZE);
601 	if (ret)
602 		return ret;
603 	ret = membuff_new(&gd->console_in, CONFIG_CONSOLE_RECORD_IN_SIZE);
604 
605 	return ret;
606 }
607 
608 void console_record_reset(void)
609 {
610 	membuff_purge(&gd->console_out);
611 	membuff_purge(&gd->console_in);
612 }
613 
614 void console_record_reset_enable(void)
615 {
616 	console_record_reset();
617 	gd->flags |= GD_FLG_RECORD;
618 }
619 #endif
620 
621 /* test if ctrl-c was pressed */
622 static int ctrlc_disabled = 0;	/* see disable_ctrl() */
623 static int ctrlc_was_pressed = 0;
624 int ctrlc(void)
625 {
626 #ifndef CONFIG_SANDBOX
627 	if (!ctrlc_disabled && gd->have_console) {
628 		if (tstc()) {
629 			switch (getc()) {
630 			case 0x03:		/* ^C - Control C */
631 				ctrlc_was_pressed = 1;
632 				return 1;
633 			default:
634 				break;
635 			}
636 		}
637 	}
638 #endif
639 
640 	return 0;
641 }
642 /* Reads user's confirmation.
643    Returns 1 if user's input is "y", "Y", "yes" or "YES"
644 */
645 int confirm_yesno(void)
646 {
647 	int i;
648 	char str_input[5];
649 
650 	/* Flush input */
651 	while (tstc())
652 		getc();
653 	i = 0;
654 	while (i < sizeof(str_input)) {
655 		str_input[i] = getc();
656 		putc(str_input[i]);
657 		if (str_input[i] == '\r')
658 			break;
659 		i++;
660 	}
661 	putc('\n');
662 	if (strncmp(str_input, "y\r", 2) == 0 ||
663 	    strncmp(str_input, "Y\r", 2) == 0 ||
664 	    strncmp(str_input, "yes\r", 4) == 0 ||
665 	    strncmp(str_input, "YES\r", 4) == 0)
666 		return 1;
667 	return 0;
668 }
669 /* pass 1 to disable ctrlc() checking, 0 to enable.
670  * returns previous state
671  */
672 int disable_ctrlc(int disable)
673 {
674 	int prev = ctrlc_disabled;	/* save previous state */
675 
676 	ctrlc_disabled = disable;
677 	return prev;
678 }
679 
680 int had_ctrlc (void)
681 {
682 	return ctrlc_was_pressed;
683 }
684 
685 void clear_ctrlc(void)
686 {
687 	ctrlc_was_pressed = 0;
688 }
689 
690 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
691 char	screen[1024];
692 char *cursor = screen;
693 int once = 0;
694 inline void dbg(const char *fmt, ...)
695 {
696 	va_list	args;
697 	uint	i;
698 	char	printbuffer[CONFIG_SYS_PBSIZE];
699 
700 	if (!once) {
701 		memset(screen, 0, sizeof(screen));
702 		once++;
703 	}
704 
705 	va_start(args, fmt);
706 
707 	/* For this to work, printbuffer must be larger than
708 	 * anything we ever want to print.
709 	 */
710 	i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
711 	va_end(args);
712 
713 	if ((screen + sizeof(screen) - 1 - cursor)
714 	    < strlen(printbuffer) + 1) {
715 		memset(screen, 0, sizeof(screen));
716 		cursor = screen;
717 	}
718 	sprintf(cursor, printbuffer);
719 	cursor += strlen(printbuffer);
720 
721 }
722 #else
723 static inline void dbg(const char *fmt, ...)
724 {
725 }
726 #endif
727 
728 /** U-Boot INIT FUNCTIONS *************************************************/
729 
730 struct stdio_dev *search_device(int flags, const char *name)
731 {
732 	struct stdio_dev *dev;
733 
734 	dev = stdio_get_by_name(name);
735 
736 	if (dev && (dev->flags & flags))
737 		return dev;
738 
739 	return NULL;
740 }
741 
742 int console_assign(int file, const char *devname)
743 {
744 	int flag;
745 	struct stdio_dev *dev;
746 
747 	/* Check for valid file */
748 	switch (file) {
749 	case stdin:
750 		flag = DEV_FLAGS_INPUT;
751 		break;
752 	case stdout:
753 	case stderr:
754 		flag = DEV_FLAGS_OUTPUT;
755 		break;
756 	default:
757 		return -1;
758 	}
759 
760 	/* Check for valid device name */
761 
762 	dev = search_device(flag, devname);
763 
764 	if (dev)
765 		return console_setfile(file, dev);
766 
767 	return -1;
768 }
769 
770 /* Called before relocation - use serial functions */
771 int console_init_f(void)
772 {
773 	gd->have_console = 1;
774 
775 #ifdef CONFIG_SILENT_CONSOLE
776 	if (getenv("silent") != NULL)
777 		gd->flags |= GD_FLG_SILENT;
778 #endif
779 
780 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
781 
782 	return 0;
783 }
784 
785 void stdio_print_current_devices(void)
786 {
787 	/* Print information */
788 	puts("In:    ");
789 	if (stdio_devices[stdin] == NULL) {
790 		puts("No input devices available!\n");
791 	} else {
792 		printf ("%s\n", stdio_devices[stdin]->name);
793 	}
794 
795 	puts("Out:   ");
796 	if (stdio_devices[stdout] == NULL) {
797 		puts("No output devices available!\n");
798 	} else {
799 		printf ("%s\n", stdio_devices[stdout]->name);
800 	}
801 
802 	puts("Err:   ");
803 	if (stdio_devices[stderr] == NULL) {
804 		puts("No error devices available!\n");
805 	} else {
806 		printf ("%s\n", stdio_devices[stderr]->name);
807 	}
808 }
809 
810 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
811 /* Called after the relocation - use desired console functions */
812 int console_init_r(void)
813 {
814 	char *stdinname, *stdoutname, *stderrname;
815 	struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
816 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
817 	int i;
818 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
819 #ifdef CONFIG_CONSOLE_MUX
820 	int iomux_err = 0;
821 #endif
822 
823 	/* set default handlers at first */
824 	gd->jt->getc  = serial_getc;
825 	gd->jt->tstc  = serial_tstc;
826 	gd->jt->putc  = serial_putc;
827 	gd->jt->puts  = serial_puts;
828 	gd->jt->printf = serial_printf;
829 
830 	/* stdin stdout and stderr are in environment */
831 	/* scan for it */
832 	stdinname  = getenv("stdin");
833 	stdoutname = getenv("stdout");
834 	stderrname = getenv("stderr");
835 
836 	if (OVERWRITE_CONSOLE == 0) {	/* if not overwritten by config switch */
837 		inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
838 		outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
839 		errdev    = search_device(DEV_FLAGS_OUTPUT, stderrname);
840 #ifdef CONFIG_CONSOLE_MUX
841 		iomux_err = iomux_doenv(stdin, stdinname);
842 		iomux_err += iomux_doenv(stdout, stdoutname);
843 		iomux_err += iomux_doenv(stderr, stderrname);
844 		if (!iomux_err)
845 			/* Successful, so skip all the code below. */
846 			goto done;
847 #endif
848 	}
849 	/* if the devices are overwritten or not found, use default device */
850 	if (inputdev == NULL) {
851 		inputdev  = search_device(DEV_FLAGS_INPUT,  "serial");
852 	}
853 	if (outputdev == NULL) {
854 		outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
855 	}
856 	if (errdev == NULL) {
857 		errdev    = search_device(DEV_FLAGS_OUTPUT, "serial");
858 	}
859 	/* Initializes output console first */
860 	if (outputdev != NULL) {
861 		/* need to set a console if not done above. */
862 		console_doenv(stdout, outputdev);
863 	}
864 	if (errdev != NULL) {
865 		/* need to set a console if not done above. */
866 		console_doenv(stderr, errdev);
867 	}
868 	if (inputdev != NULL) {
869 		/* need to set a console if not done above. */
870 		console_doenv(stdin, inputdev);
871 	}
872 
873 #ifdef CONFIG_CONSOLE_MUX
874 done:
875 #endif
876 
877 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
878 	stdio_print_current_devices();
879 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
880 
881 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
882 	/* set the environment variables (will overwrite previous env settings) */
883 	for (i = 0; i < 3; i++) {
884 		setenv(stdio_names[i], stdio_devices[i]->name);
885 	}
886 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
887 
888 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
889 
890 #if 0
891 	/* If nothing usable installed, use only the initial console */
892 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
893 		return 0;
894 #endif
895 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
896 	return 0;
897 }
898 
899 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
900 
901 /* Called after the relocation - use desired console functions */
902 int console_init_r(void)
903 {
904 	struct stdio_dev *inputdev = NULL, *outputdev = NULL;
905 	int i;
906 	struct list_head *list = stdio_get_list();
907 	struct list_head *pos;
908 	struct stdio_dev *dev;
909 
910 #ifdef CONFIG_SPLASH_SCREEN
911 	/*
912 	 * suppress all output if splash screen is enabled and we have
913 	 * a bmp to display. We redirect the output from frame buffer
914 	 * console to serial console in this case or suppress it if
915 	 * "silent" mode was requested.
916 	 */
917 	if (getenv("splashimage") != NULL) {
918 		if (!(gd->flags & GD_FLG_SILENT))
919 			outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
920 	}
921 #endif
922 
923 	/* Scan devices looking for input and output devices */
924 	list_for_each(pos, list) {
925 		dev = list_entry(pos, struct stdio_dev, list);
926 
927 		if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
928 			inputdev = dev;
929 		}
930 		if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
931 			outputdev = dev;
932 		}
933 		if(inputdev && outputdev)
934 			break;
935 	}
936 
937 	/* Initializes output console first */
938 	if (outputdev != NULL) {
939 		console_setfile(stdout, outputdev);
940 		console_setfile(stderr, outputdev);
941 #ifdef CONFIG_CONSOLE_MUX
942 		console_devices[stdout][0] = outputdev;
943 		console_devices[stderr][0] = outputdev;
944 #endif
945 	}
946 
947 	/* Initializes input console */
948 	if (inputdev != NULL) {
949 		console_setfile(stdin, inputdev);
950 #ifdef CONFIG_CONSOLE_MUX
951 		console_devices[stdin][0] = inputdev;
952 #endif
953 	}
954 
955 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
956 	stdio_print_current_devices();
957 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
958 
959 	/* Setting environment variables */
960 	for (i = 0; i < 3; i++) {
961 		setenv(stdio_names[i], stdio_devices[i]->name);
962 	}
963 
964 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
965 
966 #if 0
967 	/* If nothing usable installed, use only the initial console */
968 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
969 		return 0;
970 #endif
971 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
972 	return 0;
973 }
974 
975 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
976