xref: /rk3399_rockchip-uboot/common/console.c (revision f05ce84792cbd2e5573a414010d421eb8fbb7689)
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 /* Don't allow drivers call tstc() to do some "exit" event(maybe enter hush) */
416 #if defined(CONFIG_ARCH_ROCKCHIP) && \
417     defined(CONFIG_AVB_VBMETA_PUBLIC_KEY_VALIDATE) && \
418     defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY <= 0)
419 	return 0;
420 #endif
421 
422 #ifdef CONFIG_DISABLE_CONSOLE
423 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
424 		return 0;
425 #endif
426 
427 	if (!gd->have_console)
428 		return 0;
429 #ifdef CONFIG_CONSOLE_RECORD
430 	if (gd->console_in.start) {
431 		if (membuff_peekbyte(&gd->console_in) != -1)
432 			return 1;
433 	}
434 #endif
435 	if (gd->flags & GD_FLG_DEVINIT) {
436 		/* Test the standard input */
437 		return ftstc(stdin);
438 	}
439 
440 	/* Send directly to the handler */
441 	return serial_tstc();
442 }
443 
444 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL			0
445 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL	1
446 
447 #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
448 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
449 
450 static void pre_console_putc(const char c)
451 {
452 	char *buffer;
453 
454 	buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
455 
456 	buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
457 
458 	unmap_sysmem(buffer);
459 }
460 
461 static void print_pre_console_buffer(int flushpoint)
462 {
463 	unsigned long in = 0, out = 0;
464 	char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
465 	char *buf_in;
466 
467 	buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
468 	if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
469 		in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
470 
471 	while (in < gd->precon_buf_idx)
472 		buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
473 	unmap_sysmem(buf_in);
474 
475 	buf_out[out] = 0;
476 
477 	switch (flushpoint) {
478 	case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
479 		puts(buf_out);
480 		break;
481 	case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
482 		console_puts_noserial(stdout, buf_out);
483 		break;
484 	}
485 }
486 #else
487 static inline void pre_console_putc(const char c) {}
488 static inline void print_pre_console_buffer(int flushpoint) {}
489 #endif
490 
491 void putc(const char c)
492 {
493 #ifdef CONFIG_DEBUG_UART
494 	/* if we don't have a console yet, use the debug UART */
495 	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
496 		printch(c);
497 		return;
498 	}
499 #endif
500 #ifdef CONFIG_CONSOLE_RECORD
501 	if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
502 		membuff_putbyte(&gd->console_out, c);
503 #endif
504 #ifdef CONFIG_SILENT_CONSOLE
505 	if (gd->flags & GD_FLG_SILENT)
506 		return;
507 #endif
508 
509 #ifdef CONFIG_DISABLE_CONSOLE
510 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
511 		return;
512 #endif
513 
514 	if (!gd->have_console)
515 		return pre_console_putc(c);
516 
517 	if (gd->flags & GD_FLG_DEVINIT) {
518 		/* Send to the standard output */
519 		fputc(stdout, c);
520 	} else {
521 		/* Send directly to the handler */
522 		pre_console_putc(c);
523 		serial_putc(c);
524 	}
525 }
526 
527 #if (!defined(CONFIG_SPL_BUILD) && defined(CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP))
528 static void vspfunc(char *buf, size_t size, char *format, ...)
529 {
530 	va_list ap;
531 
532 	va_start(ap, format);
533 	vsnprintf(buf, size, format, ap);
534 	va_end(ap);
535 }
536 
537 void puts(const char *s)
538 {
539 	unsigned long ts_sec, ts_msec, ticks;
540 	char pr_timestamp[32], *p;
541 
542 	while (*s) {
543 		if (*s == '\n') {
544 			gd->new_line = 1;
545 			putc(*s++);
546 			continue;
547 		}
548 
549 		if (gd->new_line) {
550 			gd->new_line = 0;
551 			ticks = (get_ticks() / 24ULL);
552 			ts_sec = ticks / 1000000;
553 			ts_msec = ticks % 1000000;
554 			vspfunc(pr_timestamp, sizeof(pr_timestamp),
555 				"[%5lu.%06lu] ", ts_sec, ts_msec);
556 			p = pr_timestamp;
557 			while (*p)
558 				putc(*p++);
559 		}
560 
561 		putc(*s++);
562 	}
563 }
564 #else
565 void puts(const char *s)
566 {
567 	while (*s)
568 		putc(*s++);
569 }
570 #endif
571 
572 
573 #ifdef CONFIG_CONSOLE_RECORD
574 int console_record_init(void)
575 {
576 	int ret;
577 
578 	ret = membuff_new(&gd->console_out, CONFIG_CONSOLE_RECORD_OUT_SIZE);
579 	if (ret)
580 		return ret;
581 	ret = membuff_new(&gd->console_in, CONFIG_CONSOLE_RECORD_IN_SIZE);
582 
583 	return ret;
584 }
585 
586 void console_record_reset(void)
587 {
588 	membuff_purge(&gd->console_out);
589 	membuff_purge(&gd->console_in);
590 }
591 
592 void console_record_reset_enable(void)
593 {
594 	console_record_reset();
595 	gd->flags |= GD_FLG_RECORD;
596 }
597 #endif
598 
599 /* test if ctrl-c was pressed */
600 static int ctrlc_disabled = 0;	/* see disable_ctrl() */
601 static int ctrlc_was_pressed = 0;
602 int ctrlc(void)
603 {
604 /* Don't allow drivers call ctrlc() to do some "exit" event(maybe enter hush) */
605 #if defined(CONFIG_ARCH_ROCKCHIP) && \
606     defined(CONFIG_AVB_VBMETA_PUBLIC_KEY_VALIDATE)
607 	return 0;
608 #endif
609 
610 #ifndef CONFIG_SANDBOX
611 	if (!ctrlc_disabled && gd->have_console) {
612 		if (tstc()) {
613 			switch (getc()) {
614 			case 0x03:		/* ^C - Control C */
615 				ctrlc_was_pressed = 1;
616 				return 1;
617 			default:
618 				break;
619 			}
620 		}
621 	}
622 #endif
623 
624 	return 0;
625 }
626 /* Reads user's confirmation.
627    Returns 1 if user's input is "y", "Y", "yes" or "YES"
628 */
629 int confirm_yesno(void)
630 {
631 	int i;
632 	char str_input[5];
633 
634 	/* Flush input */
635 	while (tstc())
636 		getc();
637 	i = 0;
638 	while (i < sizeof(str_input)) {
639 		str_input[i] = getc();
640 		putc(str_input[i]);
641 		if (str_input[i] == '\r')
642 			break;
643 		i++;
644 	}
645 	putc('\n');
646 	if (strncmp(str_input, "y\r", 2) == 0 ||
647 	    strncmp(str_input, "Y\r", 2) == 0 ||
648 	    strncmp(str_input, "yes\r", 4) == 0 ||
649 	    strncmp(str_input, "YES\r", 4) == 0)
650 		return 1;
651 	return 0;
652 }
653 /* pass 1 to disable ctrlc() checking, 0 to enable.
654  * returns previous state
655  */
656 int disable_ctrlc(int disable)
657 {
658 	int prev = ctrlc_disabled;	/* save previous state */
659 
660 	ctrlc_disabled = disable;
661 	return prev;
662 }
663 
664 int had_ctrlc (void)
665 {
666 	return ctrlc_was_pressed;
667 }
668 
669 void clear_ctrlc(void)
670 {
671 	ctrlc_was_pressed = 0;
672 }
673 
674 /** U-Boot INIT FUNCTIONS *************************************************/
675 
676 struct stdio_dev *search_device(int flags, const char *name)
677 {
678 	struct stdio_dev *dev;
679 
680 	dev = stdio_get_by_name(name);
681 #ifdef CONFIG_VIDCONSOLE_AS_LCD
682 	if (!dev && !strcmp(name, "lcd"))
683 		dev = stdio_get_by_name("vidconsole");
684 #endif
685 
686 	if (dev && (dev->flags & flags))
687 		return dev;
688 
689 	return NULL;
690 }
691 
692 int console_assign(int file, const char *devname)
693 {
694 	int flag;
695 	struct stdio_dev *dev;
696 
697 	/* Check for valid file */
698 	switch (file) {
699 	case stdin:
700 		flag = DEV_FLAGS_INPUT;
701 		break;
702 	case stdout:
703 	case stderr:
704 		flag = DEV_FLAGS_OUTPUT;
705 		break;
706 	default:
707 		return -1;
708 	}
709 
710 	/* Check for valid device name */
711 
712 	dev = search_device(flag, devname);
713 
714 	if (dev)
715 		return console_setfile(file, dev);
716 
717 	return -1;
718 }
719 
720 static void console_update_silent(void)
721 {
722 #ifdef CONFIG_SILENT_CONSOLE
723 	if (env_get("silent") != NULL) {
724 		printf("U-Boot: enable slient console\n");
725 		gd->flags |= GD_FLG_SILENT;
726 	} else {
727 		gd->flags &= ~GD_FLG_SILENT;
728 	}
729 #endif
730 }
731 
732 int console_announce_r(void)
733 {
734 #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
735 	char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
736 
737 	display_options_get_banner(false, buf, sizeof(buf));
738 
739 	console_puts_noserial(stdout, buf);
740 #endif
741 
742 	return 0;
743 }
744 
745 /* Called before relocation - use serial functions */
746 int console_init_f(void)
747 {
748 	gd->have_console = 1;
749 
750 	console_update_silent();
751 
752 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
753 
754 	return 0;
755 }
756 
757 void stdio_print_current_devices(void)
758 {
759 	/* Print information */
760 	puts("In:    ");
761 	if (stdio_devices[stdin] == NULL) {
762 		puts("No input devices available!\n");
763 	} else {
764 		printf ("%s\n", stdio_devices[stdin]->name);
765 	}
766 
767 	puts("Out:   ");
768 	if (stdio_devices[stdout] == NULL) {
769 		puts("No output devices available!\n");
770 	} else {
771 		printf ("%s\n", stdio_devices[stdout]->name);
772 	}
773 
774 	puts("Err:   ");
775 	if (stdio_devices[stderr] == NULL) {
776 		puts("No error devices available!\n");
777 	} else {
778 		printf ("%s\n", stdio_devices[stderr]->name);
779 	}
780 }
781 
782 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
783 /* Called after the relocation - use desired console functions */
784 int console_init_r(void)
785 {
786 	char *stdinname, *stdoutname, *stderrname;
787 	struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
788 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
789 	int i;
790 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
791 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
792 	int iomux_err = 0;
793 #endif
794 
795 	/* set default handlers at first */
796 	gd->jt->getc  = serial_getc;
797 	gd->jt->tstc  = serial_tstc;
798 	gd->jt->putc  = serial_putc;
799 	gd->jt->puts  = serial_puts;
800 	gd->jt->printf = serial_printf;
801 
802 	/* stdin stdout and stderr are in environment */
803 	/* scan for it */
804 	stdinname  = env_get("stdin");
805 	stdoutname = env_get("stdout");
806 	stderrname = env_get("stderr");
807 
808 	if (OVERWRITE_CONSOLE == 0) {	/* if not overwritten by config switch */
809 		inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
810 		outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
811 		errdev    = search_device(DEV_FLAGS_OUTPUT, stderrname);
812 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
813 		iomux_err = iomux_doenv(stdin, stdinname);
814 		iomux_err += iomux_doenv(stdout, stdoutname);
815 		iomux_err += iomux_doenv(stderr, stderrname);
816 		if (!iomux_err)
817 			/* Successful, so skip all the code below. */
818 			goto done;
819 #endif
820 	}
821 	/* if the devices are overwritten or not found, use default device */
822 	if (inputdev == NULL) {
823 		inputdev  = search_device(DEV_FLAGS_INPUT,  "serial");
824 	}
825 	if (outputdev == NULL) {
826 		outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
827 	}
828 	if (errdev == NULL) {
829 		errdev    = search_device(DEV_FLAGS_OUTPUT, "serial");
830 	}
831 	/* Initializes output console first */
832 	if (outputdev != NULL) {
833 		/* need to set a console if not done above. */
834 		console_doenv(stdout, outputdev);
835 	}
836 	if (errdev != NULL) {
837 		/* need to set a console if not done above. */
838 		console_doenv(stderr, errdev);
839 	}
840 	if (inputdev != NULL) {
841 		/* need to set a console if not done above. */
842 		console_doenv(stdin, inputdev);
843 	}
844 
845 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
846 done:
847 #endif
848 
849 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
850 	stdio_print_current_devices();
851 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
852 #ifdef CONFIG_VIDCONSOLE_AS_LCD
853 	if (strstr(stdoutname, "lcd"))
854 		printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
855 #endif
856 
857 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
858 	/* set the environment variables (will overwrite previous env settings) */
859 	for (i = 0; i < 3; i++) {
860 		env_set(stdio_names[i], stdio_devices[i]->name);
861 	}
862 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
863 
864 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
865 
866 #if 0
867 	/* If nothing usable installed, use only the initial console */
868 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
869 		return 0;
870 #endif
871 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
872 	return 0;
873 }
874 
875 #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
876 
877 /* Called after the relocation - use desired console functions */
878 int console_init_r(void)
879 {
880 	struct stdio_dev *inputdev = NULL, *outputdev = NULL;
881 	int i;
882 	struct list_head *list = stdio_get_list();
883 	struct list_head *pos;
884 	struct stdio_dev *dev;
885 
886 	console_update_silent();
887 
888 #ifdef CONFIG_SPLASH_SCREEN
889 	/*
890 	 * suppress all output if splash screen is enabled and we have
891 	 * a bmp to display. We redirect the output from frame buffer
892 	 * console to serial console in this case or suppress it if
893 	 * "silent" mode was requested.
894 	 */
895 	if (env_get("splashimage") != NULL) {
896 		if (!(gd->flags & GD_FLG_SILENT))
897 			outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
898 	}
899 #endif
900 
901 	/* Scan devices looking for input and output devices */
902 	list_for_each(pos, list) {
903 		dev = list_entry(pos, struct stdio_dev, list);
904 
905 		if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
906 			inputdev = dev;
907 		}
908 		if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
909 			outputdev = dev;
910 		}
911 		if(inputdev && outputdev)
912 			break;
913 	}
914 
915 	/* Initializes output console first */
916 	if (outputdev != NULL) {
917 		console_setfile(stdout, outputdev);
918 		console_setfile(stderr, outputdev);
919 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
920 		console_devices[stdout][0] = outputdev;
921 		console_devices[stderr][0] = outputdev;
922 #endif
923 	}
924 
925 	/* Initializes input console */
926 	if (inputdev != NULL) {
927 		console_setfile(stdin, inputdev);
928 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
929 		console_devices[stdin][0] = inputdev;
930 #endif
931 	}
932 
933 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
934 	stdio_print_current_devices();
935 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
936 
937 	/* Setting environment variables */
938 	for (i = 0; i < 3; i++) {
939 		env_set(stdio_names[i], stdio_devices[i]->name);
940 	}
941 
942 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
943 
944 #if 0
945 	/* If nothing usable installed, use only the initial console */
946 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
947 		return 0;
948 #endif
949 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
950 	return 0;
951 }
952 
953 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
954