xref: /openbmc/u-boot/common/console.c (revision d08957e6)
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_doenv(int file, struct stdio_dev *dev)
231 {
232 	iomux_doenv(file, dev->name);
233 }
234 #else
235 static inline int console_getc(int file)
236 {
237 	return stdio_devices[file]->getc(stdio_devices[file]);
238 }
239 
240 static inline int console_tstc(int file)
241 {
242 	return stdio_devices[file]->tstc(stdio_devices[file]);
243 }
244 
245 static inline void console_putc(int file, const char c)
246 {
247 	stdio_devices[file]->putc(stdio_devices[file], c);
248 }
249 
250 #ifdef CONFIG_PRE_CONSOLE_BUFFER
251 static inline void console_puts_noserial(int file, const char *s)
252 {
253 	if (strcmp(stdio_devices[file]->name, "serial") != 0)
254 		stdio_devices[file]->puts(stdio_devices[file], s);
255 }
256 #endif
257 
258 static inline void console_puts(int file, const char *s)
259 {
260 	stdio_devices[file]->puts(stdio_devices[file], s);
261 }
262 
263 static inline void console_doenv(int file, struct stdio_dev *dev)
264 {
265 	console_setfile(file, dev);
266 }
267 #endif /* defined(CONFIG_CONSOLE_MUX) */
268 
269 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
270 
271 int serial_printf(const char *fmt, ...)
272 {
273 	va_list args;
274 	uint i;
275 	char printbuffer[CONFIG_SYS_PBSIZE];
276 
277 	va_start(args, fmt);
278 
279 	/* For this to work, printbuffer must be larger than
280 	 * anything we ever want to print.
281 	 */
282 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
283 	va_end(args);
284 
285 	serial_puts(printbuffer);
286 	return i;
287 }
288 
289 int fgetc(int file)
290 {
291 	if (file < MAX_FILES) {
292 #if defined(CONFIG_CONSOLE_MUX)
293 		/*
294 		 * Effectively poll for input wherever it may be available.
295 		 */
296 		for (;;) {
297 			/*
298 			 * Upper layer may have already called tstc() so
299 			 * check for that first.
300 			 */
301 			if (tstcdev != NULL)
302 				return console_getc(file);
303 			console_tstc(file);
304 #ifdef CONFIG_WATCHDOG
305 			/*
306 			 * If the watchdog must be rate-limited then it should
307 			 * already be handled in board-specific code.
308 			 */
309 			 udelay(1);
310 #endif
311 		}
312 #else
313 		return console_getc(file);
314 #endif
315 	}
316 
317 	return -1;
318 }
319 
320 int ftstc(int file)
321 {
322 	if (file < MAX_FILES)
323 		return console_tstc(file);
324 
325 	return -1;
326 }
327 
328 void fputc(int file, const char c)
329 {
330 	if (file < MAX_FILES)
331 		console_putc(file, c);
332 }
333 
334 void fputs(int file, const char *s)
335 {
336 	if (file < MAX_FILES)
337 		console_puts(file, s);
338 }
339 
340 int fprintf(int file, const char *fmt, ...)
341 {
342 	va_list args;
343 	uint i;
344 	char printbuffer[CONFIG_SYS_PBSIZE];
345 
346 	va_start(args, fmt);
347 
348 	/* For this to work, printbuffer must be larger than
349 	 * anything we ever want to print.
350 	 */
351 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
352 	va_end(args);
353 
354 	/* Send to desired file */
355 	fputs(file, printbuffer);
356 	return i;
357 }
358 
359 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
360 
361 int getc(void)
362 {
363 #ifdef CONFIG_DISABLE_CONSOLE
364 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
365 		return 0;
366 #endif
367 
368 	if (!gd->have_console)
369 		return 0;
370 
371 #ifdef CONFIG_CONSOLE_RECORD
372 	if (gd->console_in.start) {
373 		int ch;
374 
375 		ch = membuff_getbyte(&gd->console_in);
376 		if (ch != -1)
377 			return 1;
378 	}
379 #endif
380 	if (gd->flags & GD_FLG_DEVINIT) {
381 		/* Get from the standard input */
382 		return fgetc(stdin);
383 	}
384 
385 	/* Send directly to the handler */
386 	return serial_getc();
387 }
388 
389 int tstc(void)
390 {
391 #ifdef CONFIG_DISABLE_CONSOLE
392 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
393 		return 0;
394 #endif
395 
396 	if (!gd->have_console)
397 		return 0;
398 #ifdef CONFIG_CONSOLE_RECORD
399 	if (gd->console_in.start) {
400 		if (membuff_peekbyte(&gd->console_in) != -1)
401 			return 1;
402 	}
403 #endif
404 	if (gd->flags & GD_FLG_DEVINIT) {
405 		/* Test the standard input */
406 		return ftstc(stdin);
407 	}
408 
409 	/* Send directly to the handler */
410 	return serial_tstc();
411 }
412 
413 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL			0
414 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL	1
415 
416 #ifdef CONFIG_PRE_CONSOLE_BUFFER
417 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
418 
419 static void pre_console_putc(const char c)
420 {
421 	char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
422 
423 	buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
424 }
425 
426 static void pre_console_puts(const char *s)
427 {
428 	while (*s)
429 		pre_console_putc(*s++);
430 }
431 
432 static void print_pre_console_buffer(int flushpoint)
433 {
434 	unsigned long in = 0, out = 0;
435 	char *buf_in = (char *)CONFIG_PRE_CON_BUF_ADDR;
436 	char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
437 
438 	if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
439 		in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
440 
441 	while (in < gd->precon_buf_idx)
442 		buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
443 
444 	buf_out[out] = 0;
445 
446 	switch (flushpoint) {
447 	case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
448 		puts(buf_out);
449 		break;
450 	case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
451 		console_puts_noserial(stdout, buf_out);
452 		break;
453 	}
454 }
455 #else
456 static inline void pre_console_putc(const char c) {}
457 static inline void pre_console_puts(const char *s) {}
458 static inline void print_pre_console_buffer(int flushpoint) {}
459 #endif
460 
461 void putc(const char c)
462 {
463 #ifdef CONFIG_SANDBOX
464 	/* sandbox can send characters to stdout before it has a console */
465 	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
466 		os_putc(c);
467 		return;
468 	}
469 #endif
470 #ifdef CONFIG_DEBUG_UART
471 	/* if we don't have a console yet, use the debug UART */
472 	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
473 		printch(c);
474 		return;
475 	}
476 #endif
477 #ifdef CONFIG_CONSOLE_RECORD
478 	if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
479 		membuff_putbyte(&gd->console_out, c);
480 #endif
481 #ifdef CONFIG_SILENT_CONSOLE
482 	if (gd->flags & GD_FLG_SILENT)
483 		return;
484 #endif
485 
486 #ifdef CONFIG_DISABLE_CONSOLE
487 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
488 		return;
489 #endif
490 
491 	if (!gd->have_console)
492 		return pre_console_putc(c);
493 
494 	if (gd->flags & GD_FLG_DEVINIT) {
495 		/* Send to the standard output */
496 		fputc(stdout, c);
497 	} else {
498 		/* Send directly to the handler */
499 		pre_console_putc(c);
500 		serial_putc(c);
501 	}
502 }
503 
504 void puts(const char *s)
505 {
506 #ifdef CONFIG_SANDBOX
507 	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
508 		os_puts(s);
509 		return;
510 	}
511 #endif
512 #ifdef CONFIG_DEBUG_UART
513 	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
514 		while (*s) {
515 			int ch = *s++;
516 
517 			printch(ch);
518 			if (ch == '\n')
519 				printch('\r');
520 		}
521 		return;
522 	}
523 #endif
524 #ifdef CONFIG_CONSOLE_RECORD
525 	if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
526 		membuff_put(&gd->console_out, s, strlen(s));
527 #endif
528 #ifdef CONFIG_SILENT_CONSOLE
529 	if (gd->flags & GD_FLG_SILENT)
530 		return;
531 #endif
532 
533 #ifdef CONFIG_DISABLE_CONSOLE
534 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
535 		return;
536 #endif
537 
538 	if (!gd->have_console)
539 		return pre_console_puts(s);
540 
541 	if (gd->flags & GD_FLG_DEVINIT) {
542 		/* Send to the standard output */
543 		fputs(stdout, s);
544 	} else {
545 		/* Send directly to the handler */
546 		pre_console_puts(s);
547 		serial_puts(s);
548 	}
549 }
550 
551 #ifdef CONFIG_CONSOLE_RECORD
552 int console_record_init(void)
553 {
554 	int ret;
555 
556 	ret = membuff_new(&gd->console_out, CONFIG_CONSOLE_RECORD_OUT_SIZE);
557 	if (ret)
558 		return ret;
559 	ret = membuff_new(&gd->console_in, CONFIG_CONSOLE_RECORD_IN_SIZE);
560 
561 	return ret;
562 }
563 
564 void console_record_reset(void)
565 {
566 	membuff_purge(&gd->console_out);
567 	membuff_purge(&gd->console_in);
568 }
569 
570 void console_record_reset_enable(void)
571 {
572 	console_record_reset();
573 	gd->flags |= GD_FLG_RECORD;
574 }
575 #endif
576 
577 /* test if ctrl-c was pressed */
578 static int ctrlc_disabled = 0;	/* see disable_ctrl() */
579 static int ctrlc_was_pressed = 0;
580 int ctrlc(void)
581 {
582 #ifndef CONFIG_SANDBOX
583 	if (!ctrlc_disabled && gd->have_console) {
584 		if (tstc()) {
585 			switch (getc()) {
586 			case 0x03:		/* ^C - Control C */
587 				ctrlc_was_pressed = 1;
588 				return 1;
589 			default:
590 				break;
591 			}
592 		}
593 	}
594 #endif
595 
596 	return 0;
597 }
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 }
625 /* pass 1 to disable ctrlc() checking, 0 to enable.
626  * returns previous state
627  */
628 int disable_ctrlc(int disable)
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 
641 void clear_ctrlc(void)
642 {
643 	ctrlc_was_pressed = 0;
644 }
645 
646 /** U-Boot INIT FUNCTIONS *************************************************/
647 
648 struct stdio_dev *search_device(int flags, const char *name)
649 {
650 	struct stdio_dev *dev;
651 
652 	dev = stdio_get_by_name(name);
653 #ifdef CONFIG_VIDCONSOLE_AS_LCD
654 	if (!dev && !strcmp(name, "lcd"))
655 		dev = stdio_get_by_name("vidconsole");
656 #endif
657 
658 	if (dev && (dev->flags & flags))
659 		return dev;
660 
661 	return NULL;
662 }
663 
664 int console_assign(int file, const char *devname)
665 {
666 	int flag;
667 	struct stdio_dev *dev;
668 
669 	/* Check for valid file */
670 	switch (file) {
671 	case stdin:
672 		flag = DEV_FLAGS_INPUT;
673 		break;
674 	case stdout:
675 	case stderr:
676 		flag = DEV_FLAGS_OUTPUT;
677 		break;
678 	default:
679 		return -1;
680 	}
681 
682 	/* Check for valid device name */
683 
684 	dev = search_device(flag, devname);
685 
686 	if (dev)
687 		return console_setfile(file, dev);
688 
689 	return -1;
690 }
691 
692 /* Called before relocation - use serial functions */
693 int console_init_f(void)
694 {
695 	gd->have_console = 1;
696 
697 #ifdef CONFIG_SILENT_CONSOLE
698 	if (getenv("silent") != NULL)
699 		gd->flags |= GD_FLG_SILENT;
700 #endif
701 
702 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
703 
704 	return 0;
705 }
706 
707 void stdio_print_current_devices(void)
708 {
709 	/* Print information */
710 	puts("In:    ");
711 	if (stdio_devices[stdin] == NULL) {
712 		puts("No input devices available!\n");
713 	} else {
714 		printf ("%s\n", stdio_devices[stdin]->name);
715 	}
716 
717 	puts("Out:   ");
718 	if (stdio_devices[stdout] == NULL) {
719 		puts("No output devices available!\n");
720 	} else {
721 		printf ("%s\n", stdio_devices[stdout]->name);
722 	}
723 
724 	puts("Err:   ");
725 	if (stdio_devices[stderr] == NULL) {
726 		puts("No error devices available!\n");
727 	} else {
728 		printf ("%s\n", stdio_devices[stderr]->name);
729 	}
730 }
731 
732 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
733 /* Called after the relocation - use desired console functions */
734 int console_init_r(void)
735 {
736 	char *stdinname, *stdoutname, *stderrname;
737 	struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
738 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
739 	int i;
740 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
741 #ifdef CONFIG_CONSOLE_MUX
742 	int iomux_err = 0;
743 #endif
744 
745 	/* set default handlers at first */
746 	gd->jt->getc  = serial_getc;
747 	gd->jt->tstc  = serial_tstc;
748 	gd->jt->putc  = serial_putc;
749 	gd->jt->puts  = serial_puts;
750 	gd->jt->printf = serial_printf;
751 
752 	/* stdin stdout and stderr are in environment */
753 	/* scan for it */
754 	stdinname  = getenv("stdin");
755 	stdoutname = getenv("stdout");
756 	stderrname = getenv("stderr");
757 
758 	if (OVERWRITE_CONSOLE == 0) {	/* if not overwritten by config switch */
759 		inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
760 		outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
761 		errdev    = search_device(DEV_FLAGS_OUTPUT, stderrname);
762 #ifdef CONFIG_CONSOLE_MUX
763 		iomux_err = iomux_doenv(stdin, stdinname);
764 		iomux_err += iomux_doenv(stdout, stdoutname);
765 		iomux_err += iomux_doenv(stderr, stderrname);
766 		if (!iomux_err)
767 			/* Successful, so skip all the code below. */
768 			goto done;
769 #endif
770 	}
771 	/* if the devices are overwritten or not found, use default device */
772 	if (inputdev == NULL) {
773 		inputdev  = search_device(DEV_FLAGS_INPUT,  "serial");
774 	}
775 	if (outputdev == NULL) {
776 		outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
777 	}
778 	if (errdev == NULL) {
779 		errdev    = search_device(DEV_FLAGS_OUTPUT, "serial");
780 	}
781 	/* Initializes output console first */
782 	if (outputdev != NULL) {
783 		/* need to set a console if not done above. */
784 		console_doenv(stdout, outputdev);
785 	}
786 	if (errdev != NULL) {
787 		/* need to set a console if not done above. */
788 		console_doenv(stderr, errdev);
789 	}
790 	if (inputdev != NULL) {
791 		/* need to set a console if not done above. */
792 		console_doenv(stdin, inputdev);
793 	}
794 
795 #ifdef CONFIG_CONSOLE_MUX
796 done:
797 #endif
798 
799 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
800 	stdio_print_current_devices();
801 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
802 #ifdef CONFIG_VIDCONSOLE_AS_LCD
803 	if (strstr(stdoutname, "lcd"))
804 		printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
805 #endif
806 
807 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
808 	/* set the environment variables (will overwrite previous env settings) */
809 	for (i = 0; i < 3; i++) {
810 		setenv(stdio_names[i], stdio_devices[i]->name);
811 	}
812 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
813 
814 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
815 
816 #if 0
817 	/* If nothing usable installed, use only the initial console */
818 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
819 		return 0;
820 #endif
821 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
822 	return 0;
823 }
824 
825 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
826 
827 /* Called after the relocation - use desired console functions */
828 int console_init_r(void)
829 {
830 	struct stdio_dev *inputdev = NULL, *outputdev = NULL;
831 	int i;
832 	struct list_head *list = stdio_get_list();
833 	struct list_head *pos;
834 	struct stdio_dev *dev;
835 
836 #ifdef CONFIG_SPLASH_SCREEN
837 	/*
838 	 * suppress all output if splash screen is enabled and we have
839 	 * a bmp to display. We redirect the output from frame buffer
840 	 * console to serial console in this case or suppress it if
841 	 * "silent" mode was requested.
842 	 */
843 	if (getenv("splashimage") != NULL) {
844 		if (!(gd->flags & GD_FLG_SILENT))
845 			outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
846 	}
847 #endif
848 
849 	/* Scan devices looking for input and output devices */
850 	list_for_each(pos, list) {
851 		dev = list_entry(pos, struct stdio_dev, list);
852 
853 		if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
854 			inputdev = dev;
855 		}
856 		if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
857 			outputdev = dev;
858 		}
859 		if(inputdev && outputdev)
860 			break;
861 	}
862 
863 	/* Initializes output console first */
864 	if (outputdev != NULL) {
865 		console_setfile(stdout, outputdev);
866 		console_setfile(stderr, outputdev);
867 #ifdef CONFIG_CONSOLE_MUX
868 		console_devices[stdout][0] = outputdev;
869 		console_devices[stderr][0] = outputdev;
870 #endif
871 	}
872 
873 	/* Initializes input console */
874 	if (inputdev != NULL) {
875 		console_setfile(stdin, inputdev);
876 #ifdef CONFIG_CONSOLE_MUX
877 		console_devices[stdin][0] = inputdev;
878 #endif
879 	}
880 
881 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
882 	stdio_print_current_devices();
883 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
884 
885 	/* Setting environment variables */
886 	for (i = 0; i < 3; i++) {
887 		setenv(stdio_names[i], stdio_devices[i]->name);
888 	}
889 
890 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
891 
892 #if 0
893 	/* If nothing usable installed, use only the initial console */
894 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
895 		return 0;
896 #endif
897 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
898 	return 0;
899 }
900 
901 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
902