xref: /rk3399_rockchip-uboot/common/console.c (revision 7c1937d6d1c7daf8e59b4760f8adc7ee42bd7bea)
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 #ifndef CONFIG_SANDBOX
598 	if (!ctrlc_disabled && gd->have_console) {
599 		if (tstc()) {
600 			switch (getc()) {
601 			case 0x03:		/* ^C - Control C */
602 				ctrlc_was_pressed = 1;
603 				return 1;
604 			default:
605 				break;
606 			}
607 		}
608 	}
609 #endif
610 
611 	return 0;
612 }
613 /* Reads user's confirmation.
614    Returns 1 if user's input is "y", "Y", "yes" or "YES"
615 */
616 int confirm_yesno(void)
617 {
618 	int i;
619 	char str_input[5];
620 
621 	/* Flush input */
622 	while (tstc())
623 		getc();
624 	i = 0;
625 	while (i < sizeof(str_input)) {
626 		str_input[i] = getc();
627 		putc(str_input[i]);
628 		if (str_input[i] == '\r')
629 			break;
630 		i++;
631 	}
632 	putc('\n');
633 	if (strncmp(str_input, "y\r", 2) == 0 ||
634 	    strncmp(str_input, "Y\r", 2) == 0 ||
635 	    strncmp(str_input, "yes\r", 4) == 0 ||
636 	    strncmp(str_input, "YES\r", 4) == 0)
637 		return 1;
638 	return 0;
639 }
640 /* pass 1 to disable ctrlc() checking, 0 to enable.
641  * returns previous state
642  */
643 int disable_ctrlc(int disable)
644 {
645 	int prev = ctrlc_disabled;	/* save previous state */
646 
647 	ctrlc_disabled = disable;
648 	return prev;
649 }
650 
651 int had_ctrlc (void)
652 {
653 	return ctrlc_was_pressed;
654 }
655 
656 void clear_ctrlc(void)
657 {
658 	ctrlc_was_pressed = 0;
659 }
660 
661 /** U-Boot INIT FUNCTIONS *************************************************/
662 
663 struct stdio_dev *search_device(int flags, const char *name)
664 {
665 	struct stdio_dev *dev;
666 
667 	dev = stdio_get_by_name(name);
668 #ifdef CONFIG_VIDCONSOLE_AS_LCD
669 	if (!dev && !strcmp(name, "lcd"))
670 		dev = stdio_get_by_name("vidconsole");
671 #endif
672 
673 	if (dev && (dev->flags & flags))
674 		return dev;
675 
676 	return NULL;
677 }
678 
679 int console_assign(int file, const char *devname)
680 {
681 	int flag;
682 	struct stdio_dev *dev;
683 
684 	/* Check for valid file */
685 	switch (file) {
686 	case stdin:
687 		flag = DEV_FLAGS_INPUT;
688 		break;
689 	case stdout:
690 	case stderr:
691 		flag = DEV_FLAGS_OUTPUT;
692 		break;
693 	default:
694 		return -1;
695 	}
696 
697 	/* Check for valid device name */
698 
699 	dev = search_device(flag, devname);
700 
701 	if (dev)
702 		return console_setfile(file, dev);
703 
704 	return -1;
705 }
706 
707 static void console_update_silent(void)
708 {
709 #ifdef CONFIG_SILENT_CONSOLE
710 	if (env_get("silent") != NULL) {
711 		printf("U-Boot: enable slient console\n");
712 		gd->flags |= GD_FLG_SILENT;
713 	} else {
714 		gd->flags &= ~GD_FLG_SILENT;
715 	}
716 #endif
717 }
718 
719 int console_announce_r(void)
720 {
721 #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
722 	char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
723 
724 	display_options_get_banner(false, buf, sizeof(buf));
725 
726 	console_puts_noserial(stdout, buf);
727 #endif
728 
729 	return 0;
730 }
731 
732 /* Called before relocation - use serial functions */
733 int console_init_f(void)
734 {
735 	gd->have_console = 1;
736 
737 	console_update_silent();
738 
739 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
740 
741 	return 0;
742 }
743 
744 void stdio_print_current_devices(void)
745 {
746 	/* Print information */
747 	puts("In:    ");
748 	if (stdio_devices[stdin] == NULL) {
749 		puts("No input devices available!\n");
750 	} else {
751 		printf ("%s\n", stdio_devices[stdin]->name);
752 	}
753 
754 	puts("Out:   ");
755 	if (stdio_devices[stdout] == NULL) {
756 		puts("No output devices available!\n");
757 	} else {
758 		printf ("%s\n", stdio_devices[stdout]->name);
759 	}
760 
761 	puts("Err:   ");
762 	if (stdio_devices[stderr] == NULL) {
763 		puts("No error devices available!\n");
764 	} else {
765 		printf ("%s\n", stdio_devices[stderr]->name);
766 	}
767 }
768 
769 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
770 /* Called after the relocation - use desired console functions */
771 int console_init_r(void)
772 {
773 	char *stdinname, *stdoutname, *stderrname;
774 	struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
775 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
776 	int i;
777 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
778 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
779 	int iomux_err = 0;
780 #endif
781 
782 	/* set default handlers at first */
783 	gd->jt->getc  = serial_getc;
784 	gd->jt->tstc  = serial_tstc;
785 	gd->jt->putc  = serial_putc;
786 	gd->jt->puts  = serial_puts;
787 	gd->jt->printf = serial_printf;
788 
789 	/* stdin stdout and stderr are in environment */
790 	/* scan for it */
791 	stdinname  = env_get("stdin");
792 	stdoutname = env_get("stdout");
793 	stderrname = env_get("stderr");
794 
795 	if (OVERWRITE_CONSOLE == 0) {	/* if not overwritten by config switch */
796 		inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
797 		outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
798 		errdev    = search_device(DEV_FLAGS_OUTPUT, stderrname);
799 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
800 		iomux_err = iomux_doenv(stdin, stdinname);
801 		iomux_err += iomux_doenv(stdout, stdoutname);
802 		iomux_err += iomux_doenv(stderr, stderrname);
803 		if (!iomux_err)
804 			/* Successful, so skip all the code below. */
805 			goto done;
806 #endif
807 	}
808 	/* if the devices are overwritten or not found, use default device */
809 	if (inputdev == NULL) {
810 		inputdev  = search_device(DEV_FLAGS_INPUT,  "serial");
811 	}
812 	if (outputdev == NULL) {
813 		outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
814 	}
815 	if (errdev == NULL) {
816 		errdev    = search_device(DEV_FLAGS_OUTPUT, "serial");
817 	}
818 	/* Initializes output console first */
819 	if (outputdev != NULL) {
820 		/* need to set a console if not done above. */
821 		console_doenv(stdout, outputdev);
822 	}
823 	if (errdev != NULL) {
824 		/* need to set a console if not done above. */
825 		console_doenv(stderr, errdev);
826 	}
827 	if (inputdev != NULL) {
828 		/* need to set a console if not done above. */
829 		console_doenv(stdin, inputdev);
830 	}
831 
832 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
833 done:
834 #endif
835 
836 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
837 	stdio_print_current_devices();
838 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
839 #ifdef CONFIG_VIDCONSOLE_AS_LCD
840 	if (strstr(stdoutname, "lcd"))
841 		printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
842 #endif
843 
844 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
845 	/* set the environment variables (will overwrite previous env settings) */
846 	for (i = 0; i < 3; i++) {
847 		env_set(stdio_names[i], stdio_devices[i]->name);
848 	}
849 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
850 
851 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
852 
853 #if 0
854 	/* If nothing usable installed, use only the initial console */
855 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
856 		return 0;
857 #endif
858 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
859 	return 0;
860 }
861 
862 #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
863 
864 /* Called after the relocation - use desired console functions */
865 int console_init_r(void)
866 {
867 	struct stdio_dev *inputdev = NULL, *outputdev = NULL;
868 	int i;
869 	struct list_head *list = stdio_get_list();
870 	struct list_head *pos;
871 	struct stdio_dev *dev;
872 
873 	console_update_silent();
874 
875 #ifdef CONFIG_SPLASH_SCREEN
876 	/*
877 	 * suppress all output if splash screen is enabled and we have
878 	 * a bmp to display. We redirect the output from frame buffer
879 	 * console to serial console in this case or suppress it if
880 	 * "silent" mode was requested.
881 	 */
882 	if (env_get("splashimage") != NULL) {
883 		if (!(gd->flags & GD_FLG_SILENT))
884 			outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
885 	}
886 #endif
887 
888 	/* Scan devices looking for input and output devices */
889 	list_for_each(pos, list) {
890 		dev = list_entry(pos, struct stdio_dev, list);
891 
892 		if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
893 			inputdev = dev;
894 		}
895 		if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
896 			outputdev = dev;
897 		}
898 		if(inputdev && outputdev)
899 			break;
900 	}
901 
902 	/* Initializes output console first */
903 	if (outputdev != NULL) {
904 		console_setfile(stdout, outputdev);
905 		console_setfile(stderr, outputdev);
906 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
907 		console_devices[stdout][0] = outputdev;
908 		console_devices[stderr][0] = outputdev;
909 #endif
910 	}
911 
912 	/* Initializes input console */
913 	if (inputdev != NULL) {
914 		console_setfile(stdin, inputdev);
915 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
916 		console_devices[stdin][0] = inputdev;
917 #endif
918 	}
919 
920 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
921 	stdio_print_current_devices();
922 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
923 
924 	/* Setting environment variables */
925 	for (i = 0; i < 3; i++) {
926 		env_set(stdio_names[i], stdio_devices[i]->name);
927 	}
928 
929 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
930 
931 #if 0
932 	/* If nothing usable installed, use only the initial console */
933 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
934 		return 0;
935 #endif
936 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
937 	return 0;
938 }
939 
940 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
941