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