xref: /OK3568_Linux_fs/u-boot/common/console.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 #ifdef CONFIG_PSTORE
25 #include <asm/arch/pstore.h>
26 #endif
27 
28 DECLARE_GLOBAL_DATA_PTR;
29 
on_console(const char * name,const char * value,enum env_op op,int flags)30 static int on_console(const char *name, const char *value, enum env_op op,
31 	int flags)
32 {
33 	int console = -1;
34 
35 	/* Check for console redirection */
36 	if (strcmp(name, "stdin") == 0)
37 		console = stdin;
38 	else if (strcmp(name, "stdout") == 0)
39 		console = stdout;
40 	else if (strcmp(name, "stderr") == 0)
41 		console = stderr;
42 
43 	/* if not actually setting a console variable, we don't care */
44 	if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
45 		return 0;
46 
47 	switch (op) {
48 	case env_op_create:
49 	case env_op_overwrite:
50 
51 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
52 		if (iomux_doenv(console, value))
53 			return 1;
54 #else
55 		/* Try assigning specified device */
56 		if (console_assign(console, value) < 0)
57 			return 1;
58 #endif
59 		return 0;
60 
61 	case env_op_delete:
62 		if ((flags & H_FORCE) == 0)
63 			printf("Can't delete \"%s\"\n", name);
64 		return 1;
65 
66 	default:
67 		return 0;
68 	}
69 }
70 U_BOOT_ENV_CALLBACK(console, on_console);
71 
72 #ifdef CONFIG_SILENT_CONSOLE
on_silent(const char * name,const char * value,enum env_op op,int flags)73 static int on_silent(const char *name, const char *value, enum env_op op,
74 	int flags)
75 {
76 #if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_SET)
77 	if (flags & H_INTERACTIVE)
78 		return 0;
79 #endif
80 #if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_RELOC)
81 	if ((flags & H_INTERACTIVE) == 0)
82 		return 0;
83 #endif
84 
85 	if (value != NULL)
86 		gd->flags |= GD_FLG_SILENT;
87 	else
88 		gd->flags &= ~GD_FLG_SILENT;
89 
90 	return 0;
91 }
92 U_BOOT_ENV_CALLBACK(silent, on_silent);
93 #endif
94 
95 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
96 /*
97  * if overwrite_console returns 1, the stdin, stderr and stdout
98  * are switched to the serial port, else the settings in the
99  * environment are used
100  */
101 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
102 extern int overwrite_console(void);
103 #define OVERWRITE_CONSOLE overwrite_console()
104 #else
105 #define OVERWRITE_CONSOLE 0
106 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
107 
108 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
109 
console_setfile(int file,struct stdio_dev * dev)110 static int console_setfile(int file, struct stdio_dev * dev)
111 {
112 	int error = 0;
113 
114 	if (dev == NULL)
115 		return -1;
116 
117 	switch (file) {
118 	case stdin:
119 	case stdout:
120 	case stderr:
121 		/* Start new device */
122 		if (dev->start) {
123 			error = dev->start(dev);
124 			/* If it's not started dont use it */
125 			if (error < 0)
126 				break;
127 		}
128 
129 		/* Assign the new device (leaving the existing one started) */
130 		stdio_devices[file] = dev;
131 
132 		/*
133 		 * Update monitor functions
134 		 * (to use the console stuff by other applications)
135 		 */
136 		switch (file) {
137 		case stdin:
138 			gd->jt->getc = getc;
139 			gd->jt->tstc = tstc;
140 			break;
141 		case stdout:
142 			gd->jt->putc  = putc;
143 			gd->jt->puts  = puts;
144 			gd->jt->printf = printf;
145 			break;
146 		}
147 		break;
148 
149 	default:		/* Invalid file ID */
150 		error = -1;
151 	}
152 	return error;
153 }
154 
155 /**
156  * console_dev_is_serial() - Check if a stdio device is a serial device
157  *
158  * @sdev: Device to check
159  * @return true if this device is in the serial uclass (or for pre-driver-model,
160  * whether it is called "serial".
161  */
console_dev_is_serial(struct stdio_dev * sdev)162 static bool console_dev_is_serial(struct stdio_dev *sdev)
163 {
164 	bool is_serial;
165 
166 #ifdef CONFIG_DM_SERIAL
167 	if (sdev->flags & DEV_FLAGS_DM) {
168 		struct udevice *dev = sdev->priv;
169 
170 		is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL;
171 	} else
172 #endif
173 	is_serial = !strcmp(sdev->name, "serial");
174 
175 	return is_serial;
176 }
177 
178 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
179 /** Console I/O multiplexing *******************************************/
180 
181 static struct stdio_dev *tstcdev;
182 struct stdio_dev **console_devices[MAX_FILES];
183 int cd_count[MAX_FILES];
184 
185 /*
186  * This depends on tstc() always being called before getc().
187  * This is guaranteed to be true because this routine is called
188  * only from fgetc() which assures it.
189  * No attempt is made to demultiplex multiple input sources.
190  */
console_getc(int file)191 static int console_getc(int file)
192 {
193 	unsigned char ret;
194 
195 	/* This is never called with testcdev == NULL */
196 	ret = tstcdev->getc(tstcdev);
197 	tstcdev = NULL;
198 	return ret;
199 }
200 
console_tstc(int file)201 static int console_tstc(int file)
202 {
203 	int i, ret;
204 	struct stdio_dev *dev;
205 
206 	disable_ctrlc(1);
207 	for (i = 0; i < cd_count[file]; i++) {
208 		dev = console_devices[file][i];
209 		if (dev->tstc != NULL) {
210 			ret = dev->tstc(dev);
211 			if (ret > 0) {
212 				tstcdev = dev;
213 				disable_ctrlc(0);
214 				return ret;
215 			}
216 		}
217 	}
218 	disable_ctrlc(0);
219 
220 	return 0;
221 }
222 
console_putc(int file,const char c)223 static void console_putc(int file, const char c)
224 {
225 	int i;
226 	struct stdio_dev *dev;
227 
228 	for (i = 0; i < cd_count[file]; i++) {
229 		dev = console_devices[file][i];
230 		if (dev->putc != NULL)
231 			dev->putc(dev, c);
232 	}
233 }
234 
console_puts_noserial(int file,const char * s)235 static void console_puts_noserial(int file, const char *s)
236 {
237 	int i;
238 	struct stdio_dev *dev;
239 
240 	for (i = 0; i < cd_count[file]; i++) {
241 		dev = console_devices[file][i];
242 		if (dev->puts != NULL && !console_dev_is_serial(dev))
243 			dev->puts(dev, s);
244 	}
245 }
246 
console_puts(int file,const char * s)247 static void console_puts(int file, const char *s)
248 {
249 	int i;
250 	struct stdio_dev *dev;
251 
252 	for (i = 0; i < cd_count[file]; i++) {
253 		dev = console_devices[file][i];
254 		if (dev->puts != NULL)
255 			dev->puts(dev, s);
256 	}
257 }
258 
console_doenv(int file,struct stdio_dev * dev)259 static inline void console_doenv(int file, struct stdio_dev *dev)
260 {
261 	iomux_doenv(file, dev->name);
262 }
263 
console_clear(int file)264 static void console_clear(int file)
265 {
266 	int i;
267 	struct stdio_dev *dev;
268 
269 	for (i = 0; i < cd_count[file]; i++) {
270 		dev = console_devices[file][i];
271 		if (dev->clear != NULL)
272 			dev->clear(dev);
273 	}
274 }
275 #else
console_getc(int file)276 static inline int console_getc(int file)
277 {
278 	return stdio_devices[file]->getc(stdio_devices[file]);
279 }
280 
console_tstc(int file)281 static inline int console_tstc(int file)
282 {
283 	return stdio_devices[file]->tstc(stdio_devices[file]);
284 }
285 
console_putc(int file,const char c)286 static inline void console_putc(int file, const char c)
287 {
288 	stdio_devices[file]->putc(stdio_devices[file], c);
289 }
290 
console_puts_noserial(int file,const char * s)291 static inline void console_puts_noserial(int file, const char *s)
292 {
293 	if (!console_dev_is_serial(stdio_devices[file]))
294 		stdio_devices[file]->puts(stdio_devices[file], s);
295 }
296 
console_puts(int file,const char * s)297 static inline void console_puts(int file, const char *s)
298 {
299 	stdio_devices[file]->puts(stdio_devices[file], s);
300 }
301 
console_clear(int file)302 static inline void console_clear(int file)
303 {
304 	if (stdio_devices[file]->clear)
305 		stdio_devices[file]->clear(stdio_devices[file]);
306 }
307 
console_doenv(int file,struct stdio_dev * dev)308 static inline void console_doenv(int file, struct stdio_dev *dev)
309 {
310 	console_setfile(file, dev);
311 }
312 #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
313 
314 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
315 
serial_printf(const char * fmt,...)316 int serial_printf(const char *fmt, ...)
317 {
318 	va_list args;
319 	uint i;
320 	char printbuffer[CONFIG_SYS_PBSIZE];
321 
322 	va_start(args, fmt);
323 
324 	/* For this to work, printbuffer must be larger than
325 	 * anything we ever want to print.
326 	 */
327 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
328 	va_end(args);
329 
330 	serial_puts(printbuffer);
331 	return i;
332 }
333 
fgetc(int file)334 int fgetc(int file)
335 {
336 	if (file < MAX_FILES) {
337 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
338 		/*
339 		 * Effectively poll for input wherever it may be available.
340 		 */
341 		for (;;) {
342 			WATCHDOG_RESET();
343 			/*
344 			 * Upper layer may have already called tstc() so
345 			 * check for that first.
346 			 */
347 			if (tstcdev != NULL)
348 				return console_getc(file);
349 			console_tstc(file);
350 #ifdef CONFIG_WATCHDOG
351 			/*
352 			 * If the watchdog must be rate-limited then it should
353 			 * already be handled in board-specific code.
354 			 */
355 			 udelay(1);
356 #endif
357 		}
358 #else
359 		return console_getc(file);
360 #endif
361 	}
362 
363 	return -1;
364 }
365 
ftstc(int file)366 int ftstc(int file)
367 {
368 	if (file < MAX_FILES)
369 		return console_tstc(file);
370 
371 	return -1;
372 }
373 
fputc(int file,const char c)374 void fputc(int file, const char c)
375 {
376 	if (file < MAX_FILES)
377 		console_putc(file, c);
378 }
379 
fputs(int file,const char * s)380 void fputs(int file, const char *s)
381 {
382 	if (file < MAX_FILES)
383 		console_puts(file, s);
384 }
385 
fclear(int file)386 void fclear(int file)
387 {
388 	if (file < MAX_FILES)
389 		console_clear(file);
390 }
391 
fprintf(int file,const char * fmt,...)392 int fprintf(int file, const char *fmt, ...)
393 {
394 	va_list args;
395 	uint i;
396 	char printbuffer[CONFIG_SYS_PBSIZE];
397 
398 	va_start(args, fmt);
399 
400 	/* For this to work, printbuffer must be larger than
401 	 * anything we ever want to print.
402 	 */
403 	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
404 	va_end(args);
405 
406 	/* Send to desired file */
407 	fputs(file, printbuffer);
408 	return i;
409 }
410 
411 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
412 
getc(void)413 int getc(void)
414 {
415 	if (!gd || gd->flags & GD_FLG_DISABLE_CONSOLE)
416 		return 0;
417 
418 	if (!gd->have_console)
419 		return 0;
420 
421 #ifdef CONFIG_CONSOLE_RECORD
422 	if (gd->console_in.start) {
423 		int ch;
424 
425 		ch = membuff_getbyte((struct membuff *)&gd->console_in);
426 		if (ch != -1)
427 			return 1;
428 	}
429 #endif
430 	if (gd->flags & GD_FLG_DEVINIT) {
431 		/* Get from the standard input */
432 		return fgetc(stdin);
433 	}
434 
435 	/* Send directly to the handler */
436 	return serial_getc();
437 }
438 
tstc(void)439 int tstc(void)
440 {
441 	if (!gd || gd->flags & GD_FLG_DISABLE_CONSOLE)
442 		return 0;
443 
444 	if (!gd->have_console)
445 		return 0;
446 #ifdef CONFIG_CONSOLE_RECORD
447 	if (gd->console_in.start) {
448 		if (membuff_peekbyte((struct membuff *)&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 
flushc(void)461 void flushc(void)
462 {
463 	if (!gd || gd->flags & GD_FLG_DISABLE_CONSOLE)
464 		return;
465 
466 #ifdef CONFIG_DEBUG_UART_NS16550
467 	debug_uart_flushc();
468 #endif
469 }
470 
471 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL			0
472 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL	1
473 
474 #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
475 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
476 
pre_console_putc(const char c)477 static void pre_console_putc(const char c)
478 {
479 	char *buffer;
480 
481 	buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
482 
483 	buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
484 
485 	unmap_sysmem(buffer);
486 }
487 
print_pre_console_buffer(int flushpoint)488 static void print_pre_console_buffer(int flushpoint)
489 {
490 	unsigned long in = 0, out = 0;
491 	char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
492 	char *buf_in;
493 
494 	buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
495 	if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
496 		in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
497 
498 	while (in < gd->precon_buf_idx)
499 		buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
500 	unmap_sysmem(buf_in);
501 
502 	buf_out[out] = 0;
503 
504 	switch (flushpoint) {
505 	case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
506 		puts(buf_out);
507 		break;
508 	case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
509 		console_puts_noserial(stdout, buf_out);
510 		break;
511 	}
512 }
513 #else
pre_console_putc(const char c)514 static inline void pre_console_putc(const char c) {}
print_pre_console_buffer(int flushpoint)515 static inline void print_pre_console_buffer(int flushpoint) {}
516 #endif
517 
putc(const char c)518 void putc(const char c)
519 {
520 	if (!gd || gd->flags & GD_FLG_DISABLE_CONSOLE)
521 		return;
522 
523 #ifdef CONFIG_PSTORE
524 	putc_to_ram(c);
525 #endif
526 
527 #ifdef CONFIG_DEBUG_UART
528 	/* if we don't have a console yet, use the debug UART */
529 	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
530 		printch(c);
531 		return;
532 	}
533 #endif
534 #ifdef CONFIG_CONSOLE_RECORD
535 	if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
536 		membuff_putbyte((struct membuff *)&gd->console_out, c);
537 #endif
538 #ifdef CONFIG_SILENT_CONSOLE
539 	if (gd->flags & GD_FLG_SILENT)
540 		return;
541 #endif
542 
543 	if (!gd->have_console)
544 		return pre_console_putc(c);
545 
546 	if (gd->flags & GD_FLG_DEVINIT) {
547 		/* Send to the standard output */
548 		fputc(stdout, c);
549 	} else {
550 		/* Send directly to the handler */
551 		pre_console_putc(c);
552 		serial_putc(c);
553 	}
554 }
555 
556 #if ((!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)) && \
557 	defined(CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP))
vspfunc(char * buf,size_t size,char * format,...)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 
puts(const char * s)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
puts(const char * s)595 void puts(const char *s)
596 {
597 	while (*s)
598 		putc(*s++);
599 }
600 #endif
601 
602 
603 #ifdef CONFIG_CONSOLE_RECORD
console_record_init(void)604 int console_record_init(void)
605 {
606 	int ret;
607 
608 	ret = membuff_new((struct membuff *)&gd->console_out,
609 			  CONFIG_CONSOLE_RECORD_OUT_SIZE);
610 	if (ret)
611 		return ret;
612 	ret = membuff_new((struct membuff *)&gd->console_in,
613 			  CONFIG_CONSOLE_RECORD_IN_SIZE);
614 
615 	return ret;
616 }
617 
console_record_reset(void)618 void console_record_reset(void)
619 {
620 	membuff_purge((struct membuff *)&gd->console_out);
621 	membuff_purge((struct membuff *)&gd->console_in);
622 }
623 
console_record_reset_enable(void)624 void console_record_reset_enable(void)
625 {
626 	console_record_reset();
627 	gd->flags |= GD_FLG_RECORD;
628 }
629 
630 /* Print and remove data from buffer */
console_record_print_purge(void)631 void console_record_print_purge(void)
632 {
633 	unsigned long flags;
634 	char c;
635 
636 	if (!gd || !(gd->flags & GD_FLG_RECORD))
637 		return;
638 
639 	/* Remove some bits to avoid running unexpected branch in putc() */
640 	flags = gd->flags;
641 	gd->flags &= ~(GD_FLG_RECORD | GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE);
642 
643 	printf("\n\n## Console Record: \n");
644 	while (!membuff_isempty((struct membuff *)&gd->console_out)) {
645 		c = membuff_getbyte((struct membuff *)&gd->console_out);
646 		putc(c);
647 	}
648 
649 	gd->flags = flags;
650 }
651 #endif
652 
653 /* test if ctrl-c was pressed */
654 static int ctrlc_disabled = 0;	/* see disable_ctrl() */
655 static int ctrlc_was_pressed = 0;
ctrlc(void)656 int ctrlc(void)
657 {
658 #ifndef CONFIG_SANDBOX
659 	if (!ctrlc_disabled && gd->have_console) {
660 		if (tstc()) {
661 			switch (getc()) {
662 			case 0x03:		/* ^C - Control C */
663 				ctrlc_was_pressed = 1;
664 				return 1;
665 			default:
666 				break;
667 			}
668 		}
669 	}
670 #endif
671 
672 	return 0;
673 }
674 /* Reads user's confirmation.
675    Returns 1 if user's input is "y", "Y", "yes" or "YES"
676 */
confirm_yesno(void)677 int confirm_yesno(void)
678 {
679 	int i;
680 	char str_input[5];
681 
682 	/* Flush input */
683 	while (tstc())
684 		getc();
685 	i = 0;
686 	while (i < sizeof(str_input)) {
687 		str_input[i] = getc();
688 		putc(str_input[i]);
689 		if (str_input[i] == '\r')
690 			break;
691 		i++;
692 	}
693 	putc('\n');
694 	if (strncmp(str_input, "y\r", 2) == 0 ||
695 	    strncmp(str_input, "Y\r", 2) == 0 ||
696 	    strncmp(str_input, "yes\r", 4) == 0 ||
697 	    strncmp(str_input, "YES\r", 4) == 0)
698 		return 1;
699 	return 0;
700 }
701 /* pass 1 to disable ctrlc() checking, 0 to enable.
702  * returns previous state
703  */
disable_ctrlc(int disable)704 int disable_ctrlc(int disable)
705 {
706 	int prev = ctrlc_disabled;	/* save previous state */
707 
708 	ctrlc_disabled = disable;
709 	return prev;
710 }
711 
had_ctrlc(void)712 int had_ctrlc (void)
713 {
714 	return ctrlc_was_pressed;
715 }
716 
clear_ctrlc(void)717 void clear_ctrlc(void)
718 {
719 	ctrlc_was_pressed = 0;
720 }
721 
722 /** U-Boot INIT FUNCTIONS *************************************************/
723 
search_device(int flags,const char * name)724 struct stdio_dev *search_device(int flags, const char *name)
725 {
726 	struct stdio_dev *dev;
727 
728 	dev = stdio_get_by_name(name);
729 #ifdef CONFIG_VIDCONSOLE_AS_LCD
730 	if (!dev && !strcmp(name, "lcd"))
731 		dev = stdio_get_by_name("vidconsole");
732 #endif
733 
734 	if (dev && (dev->flags & flags))
735 		return dev;
736 
737 	return NULL;
738 }
739 
console_assign(int file,const char * devname)740 int console_assign(int file, const char *devname)
741 {
742 	int flag;
743 	struct stdio_dev *dev;
744 
745 	/* Check for valid file */
746 	switch (file) {
747 	case stdin:
748 		flag = DEV_FLAGS_INPUT;
749 		break;
750 	case stdout:
751 	case stderr:
752 		flag = DEV_FLAGS_OUTPUT;
753 		break;
754 	default:
755 		return -1;
756 	}
757 
758 	/* Check for valid device name */
759 
760 	dev = search_device(flag, devname);
761 
762 	if (dev)
763 		return console_setfile(file, dev);
764 
765 	return -1;
766 }
767 
console_update_silent(void)768 static void console_update_silent(void)
769 {
770 #ifdef CONFIG_SILENT_CONSOLE
771 	if (env_get("silent") != NULL) {
772 		printf("U-Boot: enable silent console\n");
773 		gd->flags |= GD_FLG_SILENT;
774 	} else {
775 		gd->flags &= ~GD_FLG_SILENT;
776 	}
777 #endif
778 }
779 
console_announce_r(void)780 int console_announce_r(void)
781 {
782 #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
783 	char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
784 
785 	display_options_get_banner(false, buf, sizeof(buf));
786 
787 	console_puts_noserial(stdout, buf);
788 #endif
789 
790 	return 0;
791 }
792 
793 /* Called before relocation - use serial functions */
console_init_f(void)794 int console_init_f(void)
795 {
796 	gd->have_console = 1;
797 
798 	console_update_silent();
799 
800 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
801 
802 	return 0;
803 }
804 
stdio_print_current_devices(void)805 void stdio_print_current_devices(void)
806 {
807 	/* Print information */
808 	puts("In:    ");
809 	if (stdio_devices[stdin] == NULL) {
810 		puts("No input devices available!\n");
811 	} else {
812 		printf ("%s\n", stdio_devices[stdin]->name);
813 	}
814 
815 	puts("Out:   ");
816 	if (stdio_devices[stdout] == NULL) {
817 		puts("No output devices available!\n");
818 	} else {
819 		printf ("%s\n", stdio_devices[stdout]->name);
820 	}
821 
822 	puts("Err:   ");
823 	if (stdio_devices[stderr] == NULL) {
824 		puts("No error devices available!\n");
825 	} else {
826 		printf ("%s\n", stdio_devices[stderr]->name);
827 	}
828 }
829 
830 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
831 /* Called after the relocation - use desired console functions */
console_init_r(void)832 int console_init_r(void)
833 {
834 	char *stdinname, *stdoutname, *stderrname;
835 	struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
836 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
837 	int i;
838 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
839 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
840 	int iomux_err = 0;
841 #endif
842 
843 	/* set default handlers at first */
844 	gd->jt->getc  = serial_getc;
845 	gd->jt->tstc  = serial_tstc;
846 	gd->jt->putc  = serial_putc;
847 	gd->jt->puts  = serial_puts;
848 	gd->jt->printf = serial_printf;
849 
850 	/* stdin stdout and stderr are in environment */
851 	/* scan for it */
852 	stdinname  = env_get("stdin");
853 	stdoutname = env_get("stdout");
854 	stderrname = env_get("stderr");
855 
856 	if (OVERWRITE_CONSOLE == 0) {	/* if not overwritten by config switch */
857 		inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
858 		outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
859 		errdev    = search_device(DEV_FLAGS_OUTPUT, stderrname);
860 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
861 		iomux_err = iomux_doenv(stdin, stdinname);
862 		iomux_err += iomux_doenv(stdout, stdoutname);
863 		iomux_err += iomux_doenv(stderr, stderrname);
864 		if (!iomux_err)
865 			/* Successful, so skip all the code below. */
866 			goto done;
867 #endif
868 	}
869 	/* if the devices are overwritten or not found, use default device */
870 	if (inputdev == NULL) {
871 		inputdev  = search_device(DEV_FLAGS_INPUT,  "serial");
872 	}
873 	if (outputdev == NULL) {
874 		outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
875 	}
876 	if (errdev == NULL) {
877 		errdev    = search_device(DEV_FLAGS_OUTPUT, "serial");
878 	}
879 	/* Initializes output console first */
880 	if (outputdev != NULL) {
881 		/* need to set a console if not done above. */
882 		console_doenv(stdout, outputdev);
883 	}
884 	if (errdev != NULL) {
885 		/* need to set a console if not done above. */
886 		console_doenv(stderr, errdev);
887 	}
888 	if (inputdev != NULL) {
889 		/* need to set a console if not done above. */
890 		console_doenv(stdin, inputdev);
891 	}
892 
893 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
894 done:
895 #endif
896 
897 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
898 	stdio_print_current_devices();
899 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
900 #ifdef CONFIG_VIDCONSOLE_AS_LCD
901 	if (strstr(stdoutname, "lcd"))
902 		printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
903 #endif
904 
905 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
906 	/* set the environment variables (will overwrite previous env settings) */
907 	for (i = 0; i < 3; i++) {
908 		env_set(stdio_names[i], stdio_devices[i]->name);
909 	}
910 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
911 
912 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
913 
914 #if 0
915 	/* If nothing usable installed, use only the initial console */
916 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
917 		return 0;
918 #endif
919 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
920 	return 0;
921 }
922 
923 #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
924 
925 /* Called after the relocation - use desired console functions */
console_init_r(void)926 int console_init_r(void)
927 {
928 	struct stdio_dev *inputdev = NULL, *outputdev = NULL;
929 	int i;
930 	struct list_head *list = stdio_get_list();
931 	struct list_head *pos;
932 	struct stdio_dev *dev;
933 
934 	console_update_silent();
935 
936 #ifdef CONFIG_SPLASH_SCREEN
937 	/*
938 	 * suppress all output if splash screen is enabled and we have
939 	 * a bmp to display. We redirect the output from frame buffer
940 	 * console to serial console in this case or suppress it if
941 	 * "silent" mode was requested.
942 	 */
943 	if (env_get("splashimage") != NULL) {
944 		if (!(gd->flags & GD_FLG_SILENT))
945 			outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
946 	}
947 #endif
948 
949 	/* Scan devices looking for input and output devices */
950 	list_for_each(pos, list) {
951 		dev = list_entry(pos, struct stdio_dev, list);
952 
953 		if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
954 			inputdev = dev;
955 		}
956 		if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
957 			outputdev = dev;
958 		}
959 		if(inputdev && outputdev)
960 			break;
961 	}
962 
963 	/* Initializes output console first */
964 	if (outputdev != NULL) {
965 		console_setfile(stdout, outputdev);
966 		console_setfile(stderr, outputdev);
967 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
968 		console_devices[stdout][0] = outputdev;
969 		console_devices[stderr][0] = outputdev;
970 #endif
971 	}
972 
973 	/* Initializes input console */
974 	if (inputdev != NULL) {
975 		console_setfile(stdin, inputdev);
976 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
977 		console_devices[stdin][0] = inputdev;
978 #endif
979 	}
980 
981 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
982 	stdio_print_current_devices();
983 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
984 
985 	/* Setting environment variables */
986 	for (i = 0; i < 3; i++) {
987 		env_set(stdio_names[i], stdio_devices[i]->name);
988 	}
989 
990 	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
991 
992 #if 0
993 	/* If nothing usable installed, use only the initial console */
994 	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
995 		return 0;
996 #endif
997 	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
998 	return 0;
999 }
1000 
1001 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
1002