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