xref: /rk3399_rockchip-uboot/common/console.c (revision 91cbfde1a2679846bd858761e06bcb81397abc19)
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 
260 static void console_clear(int file)
261 {
262 	int i;
263 	struct stdio_dev *dev;
264 
265 	for (i = 0; i < cd_count[file]; i++) {
266 		dev = console_devices[file][i];
267 		if (dev->clear != NULL)
268 			dev->clear(dev);
269 	}
270 }
271 #else
272 static inline int console_getc(int file)
273 {
274 	return stdio_devices[file]->getc(stdio_devices[file]);
275 }
276 
277 static inline int console_tstc(int file)
278 {
279 	return stdio_devices[file]->tstc(stdio_devices[file]);
280 }
281 
282 static inline void console_putc(int file, const char c)
283 {
284 	stdio_devices[file]->putc(stdio_devices[file], c);
285 }
286 
287 static inline void console_puts_noserial(int file, const char *s)
288 {
289 	if (!console_dev_is_serial(stdio_devices[file]))
290 		stdio_devices[file]->puts(stdio_devices[file], s);
291 }
292 
293 static inline void console_puts(int file, const char *s)
294 {
295 	stdio_devices[file]->puts(stdio_devices[file], s);
296 }
297 
298 static inline void console_clear(int file)
299 {
300 	if (stdio_devices[file]->clear)
301 		stdio_devices[file]->clear(stdio_devices[file]);
302 }
303 
304 static inline void console_doenv(int file, struct stdio_dev *dev)
305 {
306 	console_setfile(file, dev);
307 }
308 #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
309 
310 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
311 
312 int serial_printf(const char *fmt, ...)
313 {
314 	va_list args;
315 	uint i;
316 	char printbuffer[CONFIG_SYS_PBSIZE];
317 
318 	va_start(args, fmt);
319 
320 	/* For this to work, printbuffer must be larger than
321 	 * anything we ever want to print.
322 	 */
323 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
324 	va_end(args);
325 
326 	serial_puts(printbuffer);
327 	return i;
328 }
329 
330 int fgetc(int file)
331 {
332 	if (file < MAX_FILES) {
333 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
334 		/*
335 		 * Effectively poll for input wherever it may be available.
336 		 */
337 		for (;;) {
338 			WATCHDOG_RESET();
339 			/*
340 			 * Upper layer may have already called tstc() so
341 			 * check for that first.
342 			 */
343 			if (tstcdev != NULL)
344 				return console_getc(file);
345 			console_tstc(file);
346 #ifdef CONFIG_WATCHDOG
347 			/*
348 			 * If the watchdog must be rate-limited then it should
349 			 * already be handled in board-specific code.
350 			 */
351 			 udelay(1);
352 #endif
353 		}
354 #else
355 		return console_getc(file);
356 #endif
357 	}
358 
359 	return -1;
360 }
361 
362 int ftstc(int file)
363 {
364 	if (file < MAX_FILES)
365 		return console_tstc(file);
366 
367 	return -1;
368 }
369 
370 void fputc(int file, const char c)
371 {
372 	if (file < MAX_FILES)
373 		console_putc(file, c);
374 }
375 
376 void fputs(int file, const char *s)
377 {
378 	if (file < MAX_FILES)
379 		console_puts(file, s);
380 }
381 
382 void fclear(int file)
383 {
384 	if (file < MAX_FILES)
385 		console_clear(file);
386 }
387 
388 int fprintf(int file, const char *fmt, ...)
389 {
390 	va_list args;
391 	uint i;
392 	char printbuffer[CONFIG_SYS_PBSIZE];
393 
394 	va_start(args, fmt);
395 
396 	/* For this to work, printbuffer must be larger than
397 	 * anything we ever want to print.
398 	 */
399 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
400 	va_end(args);
401 
402 	/* Send to desired file */
403 	fputs(file, printbuffer);
404 	return i;
405 }
406 
407 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
408 
409 int getc(void)
410 {
411 #ifdef CONFIG_DISABLE_CONSOLE
412 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
413 		return 0;
414 #endif
415 
416 	if (!gd->have_console)
417 		return 0;
418 
419 #ifdef CONFIG_CONSOLE_RECORD
420 	if (gd->console_in.start) {
421 		int ch;
422 
423 		ch = membuff_getbyte(&gd->console_in);
424 		if (ch != -1)
425 			return 1;
426 	}
427 #endif
428 	if (gd->flags & GD_FLG_DEVINIT) {
429 		/* Get from the standard input */
430 		return fgetc(stdin);
431 	}
432 
433 	/* Send directly to the handler */
434 	return serial_getc();
435 }
436 
437 int tstc(void)
438 {
439 #ifdef CONFIG_DISABLE_CONSOLE
440 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
441 		return 0;
442 #endif
443 
444 	if (!gd->have_console)
445 		return 0;
446 #ifdef CONFIG_CONSOLE_RECORD
447 	if (gd->console_in.start) {
448 		if (membuff_peekbyte(&gd->console_in) != -1)
449 			return 1;
450 	}
451 #endif
452 	if (gd->flags & GD_FLG_DEVINIT) {
453 		/* Test the standard input */
454 		return ftstc(stdin);
455 	}
456 
457 	/* Send directly to the handler */
458 	return serial_tstc();
459 }
460 
461 void flushc(void)
462 {
463 #ifdef CONFIG_DISABLE_CONSOLE
464 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
465 		return;
466 #endif
467 
468 	if (gd->flags & GD_FLG_DEVINIT)
469 		fclear(stdout);
470 	else
471 		serial_clear();
472 }
473 
474 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL			0
475 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL	1
476 
477 #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
478 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
479 
480 static void pre_console_putc(const char c)
481 {
482 	char *buffer;
483 
484 	buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
485 
486 	buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
487 
488 	unmap_sysmem(buffer);
489 }
490 
491 static void print_pre_console_buffer(int flushpoint)
492 {
493 	unsigned long in = 0, out = 0;
494 	char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
495 	char *buf_in;
496 
497 	buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
498 	if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
499 		in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
500 
501 	while (in < gd->precon_buf_idx)
502 		buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
503 	unmap_sysmem(buf_in);
504 
505 	buf_out[out] = 0;
506 
507 	switch (flushpoint) {
508 	case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
509 		puts(buf_out);
510 		break;
511 	case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
512 		console_puts_noserial(stdout, buf_out);
513 		break;
514 	}
515 }
516 #else
517 static inline void pre_console_putc(const char c) {}
518 static inline void print_pre_console_buffer(int flushpoint) {}
519 #endif
520 
521 void putc(const char c)
522 {
523 #ifdef CONFIG_DEBUG_UART
524 	/* if we don't have a console yet, use the debug UART */
525 	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
526 		printch(c);
527 		return;
528 	}
529 #endif
530 #ifdef CONFIG_CONSOLE_RECORD
531 	if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
532 		membuff_putbyte(&gd->console_out, c);
533 #endif
534 #ifdef CONFIG_SILENT_CONSOLE
535 	if (gd->flags & GD_FLG_SILENT)
536 		return;
537 #endif
538 
539 #ifdef CONFIG_DISABLE_CONSOLE
540 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
541 		return;
542 #endif
543 
544 	if (!gd->have_console)
545 		return pre_console_putc(c);
546 
547 	if (gd->flags & GD_FLG_DEVINIT) {
548 		/* Send to the standard output */
549 		fputc(stdout, c);
550 	} else {
551 		/* Send directly to the handler */
552 		pre_console_putc(c);
553 		serial_putc(c);
554 	}
555 }
556 
557 #if (!defined(CONFIG_SPL_BUILD) && defined(CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP))
558 static void vspfunc(char *buf, size_t size, char *format, ...)
559 {
560 	va_list ap;
561 
562 	va_start(ap, format);
563 	vsnprintf(buf, size, format, ap);
564 	va_end(ap);
565 }
566 
567 void puts(const char *s)
568 {
569 	unsigned long ts_sec, ts_msec, ticks;
570 	char pr_timestamp[32], *p;
571 
572 	while (*s) {
573 		if (*s == '\n') {
574 			gd->new_line = 1;
575 			putc(*s++);
576 			continue;
577 		}
578 
579 		if (gd->new_line) {
580 			gd->new_line = 0;
581 			ticks = (get_ticks() / 24ULL);
582 			ts_sec = ticks / 1000000;
583 			ts_msec = ticks % 1000000;
584 			vspfunc(pr_timestamp, sizeof(pr_timestamp),
585 				"[%5lu.%06lu] ", ts_sec, ts_msec);
586 			p = pr_timestamp;
587 			while (*p)
588 				putc(*p++);
589 		}
590 
591 		putc(*s++);
592 	}
593 }
594 #else
595 void puts(const char *s)
596 {
597 	while (*s)
598 		putc(*s++);
599 }
600 #endif
601 
602 
603 #ifdef CONFIG_CONSOLE_RECORD
604 int console_record_init(void)
605 {
606 	int ret;
607 
608 	ret = membuff_new(&gd->console_out, CONFIG_CONSOLE_RECORD_OUT_SIZE);
609 	if (ret)
610 		return ret;
611 	ret = membuff_new(&gd->console_in, CONFIG_CONSOLE_RECORD_IN_SIZE);
612 
613 	return ret;
614 }
615 
616 void console_record_reset(void)
617 {
618 	membuff_purge(&gd->console_out);
619 	membuff_purge(&gd->console_in);
620 }
621 
622 void console_record_reset_enable(void)
623 {
624 	console_record_reset();
625 	gd->flags |= GD_FLG_RECORD;
626 }
627 #endif
628 
629 /* test if ctrl-c was pressed */
630 static int ctrlc_disabled = 0;	/* see disable_ctrl() */
631 static int ctrlc_was_pressed = 0;
632 int ctrlc(void)
633 {
634 #if defined(CONFIG_CONSOLE_DISABLE_CTRLC) && \
635     defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY <= 0)
636 	return 0;
637 #endif
638 
639 #ifndef CONFIG_SANDBOX
640 	if (!ctrlc_disabled && gd->have_console) {
641 		if (tstc()) {
642 			switch (getc()) {
643 			case 0x03:		/* ^C - Control C */
644 				ctrlc_was_pressed = 1;
645 				return 1;
646 			default:
647 				break;
648 			}
649 		}
650 	}
651 #endif
652 
653 	return 0;
654 }
655 /* Reads user's confirmation.
656    Returns 1 if user's input is "y", "Y", "yes" or "YES"
657 */
658 int confirm_yesno(void)
659 {
660 	int i;
661 	char str_input[5];
662 
663 	/* Flush input */
664 	while (tstc())
665 		getc();
666 	i = 0;
667 	while (i < sizeof(str_input)) {
668 		str_input[i] = getc();
669 		putc(str_input[i]);
670 		if (str_input[i] == '\r')
671 			break;
672 		i++;
673 	}
674 	putc('\n');
675 	if (strncmp(str_input, "y\r", 2) == 0 ||
676 	    strncmp(str_input, "Y\r", 2) == 0 ||
677 	    strncmp(str_input, "yes\r", 4) == 0 ||
678 	    strncmp(str_input, "YES\r", 4) == 0)
679 		return 1;
680 	return 0;
681 }
682 /* pass 1 to disable ctrlc() checking, 0 to enable.
683  * returns previous state
684  */
685 int disable_ctrlc(int disable)
686 {
687 	int prev = ctrlc_disabled;	/* save previous state */
688 
689 	ctrlc_disabled = disable;
690 	return prev;
691 }
692 
693 int had_ctrlc (void)
694 {
695 	return ctrlc_was_pressed;
696 }
697 
698 void clear_ctrlc(void)
699 {
700 	ctrlc_was_pressed = 0;
701 }
702 
703 /** U-Boot INIT FUNCTIONS *************************************************/
704 
705 struct stdio_dev *search_device(int flags, const char *name)
706 {
707 	struct stdio_dev *dev;
708 
709 	dev = stdio_get_by_name(name);
710 #ifdef CONFIG_VIDCONSOLE_AS_LCD
711 	if (!dev && !strcmp(name, "lcd"))
712 		dev = stdio_get_by_name("vidconsole");
713 #endif
714 
715 	if (dev && (dev->flags & flags))
716 		return dev;
717 
718 	return NULL;
719 }
720 
721 int console_assign(int file, const char *devname)
722 {
723 	int flag;
724 	struct stdio_dev *dev;
725 
726 	/* Check for valid file */
727 	switch (file) {
728 	case stdin:
729 		flag = DEV_FLAGS_INPUT;
730 		break;
731 	case stdout:
732 	case stderr:
733 		flag = DEV_FLAGS_OUTPUT;
734 		break;
735 	default:
736 		return -1;
737 	}
738 
739 	/* Check for valid device name */
740 
741 	dev = search_device(flag, devname);
742 
743 	if (dev)
744 		return console_setfile(file, dev);
745 
746 	return -1;
747 }
748 
749 static void console_update_silent(void)
750 {
751 #ifdef CONFIG_SILENT_CONSOLE
752 	if (env_get("silent") != NULL) {
753 		printf("U-Boot: enable slient console\n");
754 		gd->flags |= GD_FLG_SILENT;
755 	} else {
756 		gd->flags &= ~GD_FLG_SILENT;
757 	}
758 #endif
759 }
760 
761 int console_announce_r(void)
762 {
763 #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
764 	char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
765 
766 	display_options_get_banner(false, buf, sizeof(buf));
767 
768 	console_puts_noserial(stdout, buf);
769 #endif
770 
771 	return 0;
772 }
773 
774 /* Called before relocation - use serial functions */
775 int console_init_f(void)
776 {
777 	gd->have_console = 1;
778 
779 	console_update_silent();
780 
781 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
782 
783 	return 0;
784 }
785 
786 void stdio_print_current_devices(void)
787 {
788 	/* Print information */
789 	puts("In:    ");
790 	if (stdio_devices[stdin] == NULL) {
791 		puts("No input devices available!\n");
792 	} else {
793 		printf ("%s\n", stdio_devices[stdin]->name);
794 	}
795 
796 	puts("Out:   ");
797 	if (stdio_devices[stdout] == NULL) {
798 		puts("No output devices available!\n");
799 	} else {
800 		printf ("%s\n", stdio_devices[stdout]->name);
801 	}
802 
803 	puts("Err:   ");
804 	if (stdio_devices[stderr] == NULL) {
805 		puts("No error devices available!\n");
806 	} else {
807 		printf ("%s\n", stdio_devices[stderr]->name);
808 	}
809 }
810 
811 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
812 /* Called after the relocation - use desired console functions */
813 int console_init_r(void)
814 {
815 	char *stdinname, *stdoutname, *stderrname;
816 	struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
817 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
818 	int i;
819 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
820 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
821 	int iomux_err = 0;
822 #endif
823 
824 	/* set default handlers at first */
825 	gd->jt->getc  = serial_getc;
826 	gd->jt->tstc  = serial_tstc;
827 	gd->jt->putc  = serial_putc;
828 	gd->jt->puts  = serial_puts;
829 	gd->jt->printf = serial_printf;
830 
831 	/* stdin stdout and stderr are in environment */
832 	/* scan for it */
833 	stdinname  = env_get("stdin");
834 	stdoutname = env_get("stdout");
835 	stderrname = env_get("stderr");
836 
837 	if (OVERWRITE_CONSOLE == 0) {	/* if not overwritten by config switch */
838 		inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
839 		outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
840 		errdev    = search_device(DEV_FLAGS_OUTPUT, stderrname);
841 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
842 		iomux_err = iomux_doenv(stdin, stdinname);
843 		iomux_err += iomux_doenv(stdout, stdoutname);
844 		iomux_err += iomux_doenv(stderr, stderrname);
845 		if (!iomux_err)
846 			/* Successful, so skip all the code below. */
847 			goto done;
848 #endif
849 	}
850 	/* if the devices are overwritten or not found, use default device */
851 	if (inputdev == NULL) {
852 		inputdev  = search_device(DEV_FLAGS_INPUT,  "serial");
853 	}
854 	if (outputdev == NULL) {
855 		outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
856 	}
857 	if (errdev == NULL) {
858 		errdev    = search_device(DEV_FLAGS_OUTPUT, "serial");
859 	}
860 	/* Initializes output console first */
861 	if (outputdev != NULL) {
862 		/* need to set a console if not done above. */
863 		console_doenv(stdout, outputdev);
864 	}
865 	if (errdev != NULL) {
866 		/* need to set a console if not done above. */
867 		console_doenv(stderr, errdev);
868 	}
869 	if (inputdev != NULL) {
870 		/* need to set a console if not done above. */
871 		console_doenv(stdin, inputdev);
872 	}
873 
874 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
875 done:
876 #endif
877 
878 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
879 	stdio_print_current_devices();
880 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
881 #ifdef CONFIG_VIDCONSOLE_AS_LCD
882 	if (strstr(stdoutname, "lcd"))
883 		printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
884 #endif
885 
886 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
887 	/* set the environment variables (will overwrite previous env settings) */
888 	for (i = 0; i < 3; i++) {
889 		env_set(stdio_names[i], stdio_devices[i]->name);
890 	}
891 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
892 
893 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
894 
895 #if 0
896 	/* If nothing usable installed, use only the initial console */
897 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
898 		return 0;
899 #endif
900 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
901 	return 0;
902 }
903 
904 #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
905 
906 /* Called after the relocation - use desired console functions */
907 int console_init_r(void)
908 {
909 	struct stdio_dev *inputdev = NULL, *outputdev = NULL;
910 	int i;
911 	struct list_head *list = stdio_get_list();
912 	struct list_head *pos;
913 	struct stdio_dev *dev;
914 
915 	console_update_silent();
916 
917 #ifdef CONFIG_SPLASH_SCREEN
918 	/*
919 	 * suppress all output if splash screen is enabled and we have
920 	 * a bmp to display. We redirect the output from frame buffer
921 	 * console to serial console in this case or suppress it if
922 	 * "silent" mode was requested.
923 	 */
924 	if (env_get("splashimage") != NULL) {
925 		if (!(gd->flags & GD_FLG_SILENT))
926 			outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
927 	}
928 #endif
929 
930 	/* Scan devices looking for input and output devices */
931 	list_for_each(pos, list) {
932 		dev = list_entry(pos, struct stdio_dev, list);
933 
934 		if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
935 			inputdev = dev;
936 		}
937 		if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
938 			outputdev = dev;
939 		}
940 		if(inputdev && outputdev)
941 			break;
942 	}
943 
944 	/* Initializes output console first */
945 	if (outputdev != NULL) {
946 		console_setfile(stdout, outputdev);
947 		console_setfile(stderr, outputdev);
948 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
949 		console_devices[stdout][0] = outputdev;
950 		console_devices[stderr][0] = outputdev;
951 #endif
952 	}
953 
954 	/* Initializes input console */
955 	if (inputdev != NULL) {
956 		console_setfile(stdin, inputdev);
957 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
958 		console_devices[stdin][0] = inputdev;
959 #endif
960 	}
961 
962 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
963 	stdio_print_current_devices();
964 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
965 
966 	/* Setting environment variables */
967 	for (i = 0; i < 3; i++) {
968 		env_set(stdio_names[i], stdio_devices[i]->name);
969 	}
970 
971 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
972 
973 #if 0
974 	/* If nothing usable installed, use only the initial console */
975 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
976 		return 0;
977 #endif
978 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
979 	return 0;
980 }
981 
982 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
983