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