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