3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <stdio_dev.h>
15 #include <environment.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 static int on_console(const char *name, const char *value, enum env_op op,
24 /* Check for console redirection */
25 if (strcmp(name, "stdin") == 0)
27 else if (strcmp(name, "stdout") == 0)
29 else if (strcmp(name, "stderr") == 0)
32 /* if not actually setting a console variable, we don't care */
33 if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
38 case env_op_overwrite:
40 #ifdef CONFIG_CONSOLE_MUX
41 if (iomux_doenv(console, value))
44 /* Try assigning specified device */
45 if (console_assign(console, value) < 0)
47 #endif /* CONFIG_CONSOLE_MUX */
51 if ((flags & H_FORCE) == 0)
52 printf("Can't delete \"%s\"\n", name);
59 U_BOOT_ENV_CALLBACK(console, on_console);
61 #ifdef CONFIG_SILENT_CONSOLE
62 static int on_silent(const char *name, const char *value, enum env_op op,
65 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
66 if (flags & H_INTERACTIVE)
69 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
70 if ((flags & H_INTERACTIVE) == 0)
75 gd->flags |= GD_FLG_SILENT;
77 gd->flags &= ~GD_FLG_SILENT;
81 U_BOOT_ENV_CALLBACK(silent, on_silent);
84 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
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
90 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
91 extern int overwrite_console(void);
92 #define OVERWRITE_CONSOLE overwrite_console()
94 #define OVERWRITE_CONSOLE 0
95 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
97 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
99 static int console_setfile(int file, struct stdio_dev * dev)
110 /* Start new device */
112 error = dev->start();
113 /* If it's not started dont use it */
118 /* Assign the new device (leaving the existing one started) */
119 stdio_devices[file] = dev;
122 * Update monitor functions
123 * (to use the console stuff by other applications)
127 gd->jt[XF_getc] = dev->getc;
128 gd->jt[XF_tstc] = dev->tstc;
131 gd->jt[XF_putc] = dev->putc;
132 gd->jt[XF_puts] = dev->puts;
133 gd->jt[XF_printf] = printf;
138 default: /* Invalid file ID */
144 #if defined(CONFIG_CONSOLE_MUX)
145 /** Console I/O multiplexing *******************************************/
147 static struct stdio_dev *tstcdev;
148 struct stdio_dev **console_devices[MAX_FILES];
149 int cd_count[MAX_FILES];
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.
157 static int console_getc(int file)
161 /* This is never called with testcdev == NULL */
162 ret = tstcdev->getc();
167 static int console_tstc(int file)
170 struct stdio_dev *dev;
173 for (i = 0; i < cd_count[file]; i++) {
174 dev = console_devices[file][i];
175 if (dev->tstc != NULL) {
189 static void console_putc(int file, const char c)
192 struct stdio_dev *dev;
194 for (i = 0; i < cd_count[file]; i++) {
195 dev = console_devices[file][i];
196 if (dev->putc != NULL)
201 static void console_puts(int file, const char *s)
204 struct stdio_dev *dev;
206 for (i = 0; i < cd_count[file]; i++) {
207 dev = console_devices[file][i];
208 if (dev->puts != NULL)
213 static inline void console_printdevs(int file)
215 iomux_printdevs(file);
218 static inline void console_doenv(int file, struct stdio_dev *dev)
220 iomux_doenv(file, dev->name);
223 static inline int console_getc(int file)
225 return stdio_devices[file]->getc();
228 static inline int console_tstc(int file)
230 return stdio_devices[file]->tstc();
233 static inline void console_putc(int file, const char c)
235 stdio_devices[file]->putc(c);
238 static inline void console_puts(int file, const char *s)
240 stdio_devices[file]->puts(s);
243 static inline void console_printdevs(int file)
245 printf("%s\n", stdio_devices[file]->name);
248 static inline void console_doenv(int file, struct stdio_dev *dev)
250 console_setfile(file, dev);
252 #endif /* defined(CONFIG_CONSOLE_MUX) */
254 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
256 int serial_printf(const char *fmt, ...)
260 char printbuffer[CONFIG_SYS_PBSIZE];
264 /* For this to work, printbuffer must be larger than
265 * anything we ever want to print.
267 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
270 serial_puts(printbuffer);
276 if (file < MAX_FILES) {
277 #if defined(CONFIG_CONSOLE_MUX)
279 * Effectively poll for input wherever it may be available.
283 * Upper layer may have already called tstc() so
284 * check for that first.
287 return console_getc(file);
289 #ifdef CONFIG_WATCHDOG
291 * If the watchdog must be rate-limited then it should
292 * already be handled in board-specific code.
298 return console_getc(file);
307 if (file < MAX_FILES)
308 return console_tstc(file);
313 void fputc(int file, const char c)
315 if (file < MAX_FILES)
316 console_putc(file, c);
319 void fputs(int file, const char *s)
321 if (file < MAX_FILES)
322 console_puts(file, s);
325 int fprintf(int file, const char *fmt, ...)
329 char printbuffer[CONFIG_SYS_PBSIZE];
333 /* For this to work, printbuffer must be larger than
334 * anything we ever want to print.
336 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
339 /* Send to desired file */
340 fputs(file, printbuffer);
344 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
348 #ifdef CONFIG_DISABLE_CONSOLE
349 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
353 if (!gd->have_console)
356 if (gd->flags & GD_FLG_DEVINIT) {
357 /* Get from the standard input */
361 /* Send directly to the handler */
362 return serial_getc();
367 #ifdef CONFIG_DISABLE_CONSOLE
368 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
372 if (!gd->have_console)
375 if (gd->flags & GD_FLG_DEVINIT) {
376 /* Test the standard input */
380 /* Send directly to the handler */
381 return serial_tstc();
384 #ifdef CONFIG_PRE_CONSOLE_BUFFER
385 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
387 static void pre_console_putc(const char c)
389 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
391 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
394 static void pre_console_puts(const char *s)
397 pre_console_putc(*s++);
400 static void print_pre_console_buffer(void)
403 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
405 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
406 i = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
408 while (i < gd->precon_buf_idx)
409 putc(buffer[CIRC_BUF_IDX(i++)]);
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) {}
417 void putc(const char c)
419 #ifdef CONFIG_SANDBOX
425 #ifdef CONFIG_SILENT_CONSOLE
426 if (gd->flags & GD_FLG_SILENT)
430 #ifdef CONFIG_DISABLE_CONSOLE
431 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
435 if (!gd->have_console)
436 return pre_console_putc(c);
438 if (gd->flags & GD_FLG_DEVINIT) {
439 /* Send to the standard output */
442 /* Send directly to the handler */
447 void puts(const char *s)
449 #ifdef CONFIG_SANDBOX
456 #ifdef CONFIG_SILENT_CONSOLE
457 if (gd->flags & GD_FLG_SILENT)
461 #ifdef CONFIG_DISABLE_CONSOLE
462 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
466 if (!gd->have_console)
467 return pre_console_puts(s);
469 if (gd->flags & GD_FLG_DEVINIT) {
470 /* Send to the standard output */
473 /* Send directly to the handler */
478 int printf(const char *fmt, ...)
482 char printbuffer[CONFIG_SYS_PBSIZE];
484 #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_PRE_CONSOLE_BUFFER)
485 if (!gd->have_console)
491 /* For this to work, printbuffer must be larger than
492 * anything we ever want to print.
494 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
497 /* Print the string */
502 int vprintf(const char *fmt, va_list args)
505 char printbuffer[CONFIG_SYS_PBSIZE];
507 #ifndef CONFIG_PRE_CONSOLE_BUFFER
508 if (!gd->have_console)
512 /* For this to work, printbuffer must be larger than
513 * anything we ever want to print.
515 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
517 /* Print the string */
522 /* test if ctrl-c was pressed */
523 static int ctrlc_disabled = 0; /* see disable_ctrl() */
524 static int ctrlc_was_pressed = 0;
527 if (!ctrlc_disabled && gd->have_console) {
530 case 0x03: /* ^C - Control C */
531 ctrlc_was_pressed = 1;
541 /* pass 1 to disable ctrlc() checking, 0 to enable.
542 * returns previous state
544 int disable_ctrlc(int disable)
546 int prev = ctrlc_disabled; /* save previous state */
548 ctrlc_disabled = disable;
554 return ctrlc_was_pressed;
557 void clear_ctrlc(void)
559 ctrlc_was_pressed = 0;
562 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
564 char *cursor = screen;
566 inline void dbg(const char *fmt, ...)
570 char printbuffer[CONFIG_SYS_PBSIZE];
573 memset(screen, 0, sizeof(screen));
579 /* For this to work, printbuffer must be larger than
580 * anything we ever want to print.
582 i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
585 if ((screen + sizeof(screen) - 1 - cursor)
586 < strlen(printbuffer) + 1) {
587 memset(screen, 0, sizeof(screen));
590 sprintf(cursor, printbuffer);
591 cursor += strlen(printbuffer);
595 inline void dbg(const char *fmt, ...)
600 /** U-Boot INIT FUNCTIONS *************************************************/
602 struct stdio_dev *search_device(int flags, const char *name)
604 struct stdio_dev *dev;
606 dev = stdio_get_by_name(name);
608 if (dev && (dev->flags & flags))
614 int console_assign(int file, const char *devname)
617 struct stdio_dev *dev;
619 /* Check for valid file */
622 flag = DEV_FLAGS_INPUT;
626 flag = DEV_FLAGS_OUTPUT;
632 /* Check for valid device name */
634 dev = search_device(flag, devname);
637 return console_setfile(file, dev);
642 /* Called before relocation - use serial functions */
643 int console_init_f(void)
645 gd->have_console = 1;
647 #ifdef CONFIG_SILENT_CONSOLE
648 if (getenv("silent") != NULL)
649 gd->flags |= GD_FLG_SILENT;
652 print_pre_console_buffer();
657 void stdio_print_current_devices(void)
659 /* Print information */
661 if (stdio_devices[stdin] == NULL) {
662 puts("No input devices available!\n");
664 printf ("%s\n", stdio_devices[stdin]->name);
668 if (stdio_devices[stdout] == NULL) {
669 puts("No output devices available!\n");
671 printf ("%s\n", stdio_devices[stdout]->name);
675 if (stdio_devices[stderr] == NULL) {
676 puts("No error devices available!\n");
678 printf ("%s\n", stdio_devices[stderr]->name);
682 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
683 /* Called after the relocation - use desired console functions */
684 int console_init_r(void)
686 char *stdinname, *stdoutname, *stderrname;
687 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
688 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
690 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
691 #ifdef CONFIG_CONSOLE_MUX
695 /* set default handlers at first */
696 gd->jt[XF_getc] = serial_getc;
697 gd->jt[XF_tstc] = serial_tstc;
698 gd->jt[XF_putc] = serial_putc;
699 gd->jt[XF_puts] = serial_puts;
700 gd->jt[XF_printf] = serial_printf;
702 /* stdin stdout and stderr are in environment */
704 stdinname = getenv("stdin");
705 stdoutname = getenv("stdout");
706 stderrname = getenv("stderr");
708 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
709 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
710 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
711 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
712 #ifdef CONFIG_CONSOLE_MUX
713 iomux_err = iomux_doenv(stdin, stdinname);
714 iomux_err += iomux_doenv(stdout, stdoutname);
715 iomux_err += iomux_doenv(stderr, stderrname);
717 /* Successful, so skip all the code below. */
721 /* if the devices are overwritten or not found, use default device */
722 if (inputdev == NULL) {
723 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
725 if (outputdev == NULL) {
726 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
728 if (errdev == NULL) {
729 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
731 /* Initializes output console first */
732 if (outputdev != NULL) {
733 /* need to set a console if not done above. */
734 console_doenv(stdout, outputdev);
736 if (errdev != NULL) {
737 /* need to set a console if not done above. */
738 console_doenv(stderr, errdev);
740 if (inputdev != NULL) {
741 /* need to set a console if not done above. */
742 console_doenv(stdin, inputdev);
745 #ifdef CONFIG_CONSOLE_MUX
749 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
750 stdio_print_current_devices();
751 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
753 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
754 /* set the environment variables (will overwrite previous env settings) */
755 for (i = 0; i < 3; i++) {
756 setenv(stdio_names[i], stdio_devices[i]->name);
758 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
760 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
763 /* If nothing usable installed, use only the initial console */
764 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
770 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
772 /* Called after the relocation - use desired console functions */
773 int console_init_r(void)
775 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
777 struct list_head *list = stdio_get_list();
778 struct list_head *pos;
779 struct stdio_dev *dev;
781 #ifdef CONFIG_SPLASH_SCREEN
783 * suppress all output if splash screen is enabled and we have
784 * a bmp to display. We redirect the output from frame buffer
785 * console to serial console in this case or suppress it if
786 * "silent" mode was requested.
788 if (getenv("splashimage") != NULL) {
789 if (!(gd->flags & GD_FLG_SILENT))
790 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
794 /* Scan devices looking for input and output devices */
795 list_for_each(pos, list) {
796 dev = list_entry(pos, struct stdio_dev, list);
798 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
801 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
804 if(inputdev && outputdev)
808 /* Initializes output console first */
809 if (outputdev != NULL) {
810 console_setfile(stdout, outputdev);
811 console_setfile(stderr, outputdev);
812 #ifdef CONFIG_CONSOLE_MUX
813 console_devices[stdout][0] = outputdev;
814 console_devices[stderr][0] = outputdev;
818 /* Initializes input console */
819 if (inputdev != NULL) {
820 console_setfile(stdin, inputdev);
821 #ifdef CONFIG_CONSOLE_MUX
822 console_devices[stdin][0] = inputdev;
826 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
827 stdio_print_current_devices();
828 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
830 /* Setting environment variables */
831 for (i = 0; i < 3; i++) {
832 setenv(stdio_names[i], stdio_devices[i]->name);
835 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
838 /* If nothing usable installed, use only the initial console */
839 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
846 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */