3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
5 * SPDX-License-Identifier: GPL-2.0+
14 #include <stdio_dev.h>
16 #include <environment.h>
18 DECLARE_GLOBAL_DATA_PTR
;
20 static int on_console(const char *name
, const char *value
, enum env_op op
,
25 /* Check for console redirection */
26 if (strcmp(name
, "stdin") == 0)
28 else if (strcmp(name
, "stdout") == 0)
30 else if (strcmp(name
, "stderr") == 0)
33 /* if not actually setting a console variable, we don't care */
34 if (console
== -1 || (gd
->flags
& GD_FLG_DEVINIT
) == 0)
39 case env_op_overwrite
:
41 #ifdef CONFIG_CONSOLE_MUX
42 if (iomux_doenv(console
, value
))
45 /* Try assigning specified device */
46 if (console_assign(console
, value
) < 0)
48 #endif /* CONFIG_CONSOLE_MUX */
52 if ((flags
& H_FORCE
) == 0)
53 printf("Can't delete \"%s\"\n", name
);
60 U_BOOT_ENV_CALLBACK(console
, on_console
);
62 #ifdef CONFIG_SILENT_CONSOLE
63 static int on_silent(const char *name
, const char *value
, enum env_op op
,
66 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
67 if (flags
& H_INTERACTIVE
)
70 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
71 if ((flags
& H_INTERACTIVE
) == 0)
76 gd
->flags
|= GD_FLG_SILENT
;
78 gd
->flags
&= ~GD_FLG_SILENT
;
82 U_BOOT_ENV_CALLBACK(silent
, on_silent
);
85 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
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
91 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
92 extern int overwrite_console(void);
93 #define OVERWRITE_CONSOLE overwrite_console()
95 #define OVERWRITE_CONSOLE 0
96 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
98 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
100 static int console_setfile(int file
, struct stdio_dev
* dev
)
111 /* Start new device */
113 error
= dev
->start(dev
);
114 /* If it's not started dont use it */
119 /* Assign the new device (leaving the existing one started) */
120 stdio_devices
[file
] = dev
;
123 * Update monitor functions
124 * (to use the console stuff by other applications)
134 gd
->jt
->printf
= printf
;
139 default: /* Invalid file ID */
145 #if defined(CONFIG_CONSOLE_MUX)
146 /** Console I/O multiplexing *******************************************/
148 static struct stdio_dev
*tstcdev
;
149 struct stdio_dev
**console_devices
[MAX_FILES
];
150 int cd_count
[MAX_FILES
];
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.
158 static int console_getc(int file
)
162 /* This is never called with testcdev == NULL */
163 ret
= tstcdev
->getc(tstcdev
);
168 static int console_tstc(int file
)
171 struct stdio_dev
*dev
;
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
);
190 static void console_putc(int file
, const char c
)
193 struct stdio_dev
*dev
;
195 for (i
= 0; i
< cd_count
[file
]; i
++) {
196 dev
= console_devices
[file
][i
];
197 if (dev
->putc
!= NULL
)
202 #ifdef CONFIG_PRE_CONSOLE_BUFFER
203 static void console_putc_noserial(int file
, const char c
)
206 struct stdio_dev
*dev
;
208 for (i
= 0; i
< cd_count
[file
]; i
++) {
209 dev
= console_devices
[file
][i
];
210 if (dev
->putc
!= NULL
&& strcmp(dev
->name
, "serial") != 0)
216 static void console_puts(int file
, const char *s
)
219 struct stdio_dev
*dev
;
221 for (i
= 0; i
< cd_count
[file
]; i
++) {
222 dev
= console_devices
[file
][i
];
223 if (dev
->puts
!= NULL
)
228 static inline void console_printdevs(int file
)
230 iomux_printdevs(file
);
233 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
235 iomux_doenv(file
, dev
->name
);
238 static inline int console_getc(int file
)
240 return stdio_devices
[file
]->getc(stdio_devices
[file
]);
243 static inline int console_tstc(int file
)
245 return stdio_devices
[file
]->tstc(stdio_devices
[file
]);
248 static inline void console_putc(int file
, const char c
)
250 stdio_devices
[file
]->putc(stdio_devices
[file
], c
);
253 #ifdef CONFIG_PRE_CONSOLE_BUFFER
254 static inline void console_putc_noserial(int file
, const char c
)
256 if (strcmp(stdio_devices
[file
]->name
, "serial") != 0)
257 stdio_devices
[file
]->putc(stdio_devices
[file
], c
);
261 static inline void console_puts(int file
, const char *s
)
263 stdio_devices
[file
]->puts(stdio_devices
[file
], s
);
266 static inline void console_printdevs(int file
)
268 printf("%s\n", stdio_devices
[file
]->name
);
271 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
273 console_setfile(file
, dev
);
275 #endif /* defined(CONFIG_CONSOLE_MUX) */
277 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
279 int serial_printf(const char *fmt
, ...)
283 char printbuffer
[CONFIG_SYS_PBSIZE
];
287 /* For this to work, printbuffer must be larger than
288 * anything we ever want to print.
290 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
293 serial_puts(printbuffer
);
299 if (file
< MAX_FILES
) {
300 #if defined(CONFIG_CONSOLE_MUX)
302 * Effectively poll for input wherever it may be available.
306 * Upper layer may have already called tstc() so
307 * check for that first.
310 return console_getc(file
);
312 #ifdef CONFIG_WATCHDOG
314 * If the watchdog must be rate-limited then it should
315 * already be handled in board-specific code.
321 return console_getc(file
);
330 if (file
< MAX_FILES
)
331 return console_tstc(file
);
336 void fputc(int file
, const char c
)
338 if (file
< MAX_FILES
)
339 console_putc(file
, c
);
342 void fputs(int file
, const char *s
)
344 if (file
< MAX_FILES
)
345 console_puts(file
, s
);
348 int fprintf(int file
, const char *fmt
, ...)
352 char printbuffer
[CONFIG_SYS_PBSIZE
];
356 /* For this to work, printbuffer must be larger than
357 * anything we ever want to print.
359 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
362 /* Send to desired file */
363 fputs(file
, printbuffer
);
367 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
371 #ifdef CONFIG_DISABLE_CONSOLE
372 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
376 if (!gd
->have_console
)
379 if (gd
->flags
& GD_FLG_DEVINIT
) {
380 /* Get from the standard input */
384 /* Send directly to the handler */
385 return serial_getc();
390 #ifdef CONFIG_DISABLE_CONSOLE
391 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
395 if (!gd
->have_console
)
398 if (gd
->flags
& GD_FLG_DEVINIT
) {
399 /* Test the standard input */
403 /* Send directly to the handler */
404 return serial_tstc();
407 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
408 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
410 #ifdef CONFIG_PRE_CONSOLE_BUFFER
411 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
413 static void pre_console_putc(const char c
)
415 char *buffer
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
417 buffer
[CIRC_BUF_IDX(gd
->precon_buf_idx
++)] = c
;
420 static void pre_console_puts(const char *s
)
423 pre_console_putc(*s
++);
426 static void print_pre_console_buffer(int flushpoint
)
429 char *buffer
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
431 if (gd
->precon_buf_idx
> CONFIG_PRE_CON_BUF_SZ
)
432 i
= gd
->precon_buf_idx
- CONFIG_PRE_CON_BUF_SZ
;
434 while (i
< gd
->precon_buf_idx
)
435 switch (flushpoint
) {
436 case PRE_CONSOLE_FLUSHPOINT1_SERIAL
:
437 putc(buffer
[CIRC_BUF_IDX(i
++)]);
439 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
:
440 console_putc_noserial(stdout
,
441 buffer
[CIRC_BUF_IDX(i
++)]);
446 static inline void pre_console_putc(const char c
) {}
447 static inline void pre_console_puts(const char *s
) {}
448 static inline void print_pre_console_buffer(int flushpoint
) {}
451 void putc(const char c
)
453 #ifdef CONFIG_SANDBOX
454 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
459 #ifdef CONFIG_SILENT_CONSOLE
460 if (gd
->flags
& GD_FLG_SILENT
)
464 #ifdef CONFIG_DISABLE_CONSOLE
465 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
469 if (!gd
->have_console
)
470 return pre_console_putc(c
);
472 if (gd
->flags
& GD_FLG_DEVINIT
) {
473 /* Send to the standard output */
476 /* Send directly to the handler */
482 void puts(const char *s
)
484 #ifdef CONFIG_SANDBOX
485 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
491 #ifdef CONFIG_SILENT_CONSOLE
492 if (gd
->flags
& GD_FLG_SILENT
)
496 #ifdef CONFIG_DISABLE_CONSOLE
497 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
501 if (!gd
->have_console
)
502 return pre_console_puts(s
);
504 if (gd
->flags
& GD_FLG_DEVINIT
) {
505 /* Send to the standard output */
508 /* Send directly to the handler */
514 int printf(const char *fmt
, ...)
518 char printbuffer
[CONFIG_SYS_PBSIZE
];
520 #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_PRE_CONSOLE_BUFFER)
521 if (!gd
->have_console
)
527 /* For this to work, printbuffer must be larger than
528 * anything we ever want to print.
530 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
533 /* Print the string */
538 int vprintf(const char *fmt
, va_list args
)
541 char printbuffer
[CONFIG_SYS_PBSIZE
];
543 #if defined(CONFIG_PRE_CONSOLE_BUFFER) && !defined(CONFIG_SANDBOX)
544 if (!gd
->have_console
)
548 /* For this to work, printbuffer must be larger than
549 * anything we ever want to print.
551 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
553 /* Print the string */
558 /* test if ctrl-c was pressed */
559 static int ctrlc_disabled
= 0; /* see disable_ctrl() */
560 static int ctrlc_was_pressed
= 0;
563 #ifndef CONFIG_SANDBOX
564 if (!ctrlc_disabled
&& gd
->have_console
) {
567 case 0x03: /* ^C - Control C */
568 ctrlc_was_pressed
= 1;
579 /* Reads user's confirmation.
580 Returns 1 if user's input is "y", "Y", "yes" or "YES"
582 int confirm_yesno(void)
591 while (i
< sizeof(str_input
)) {
592 str_input
[i
] = getc();
594 if (str_input
[i
] == '\r')
599 if (strncmp(str_input
, "y\r", 2) == 0 ||
600 strncmp(str_input
, "Y\r", 2) == 0 ||
601 strncmp(str_input
, "yes\r", 4) == 0 ||
602 strncmp(str_input
, "YES\r", 4) == 0)
606 /* pass 1 to disable ctrlc() checking, 0 to enable.
607 * returns previous state
609 int disable_ctrlc(int disable
)
611 int prev
= ctrlc_disabled
; /* save previous state */
613 ctrlc_disabled
= disable
;
619 return ctrlc_was_pressed
;
622 void clear_ctrlc(void)
624 ctrlc_was_pressed
= 0;
627 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
629 char *cursor
= screen
;
631 inline void dbg(const char *fmt
, ...)
635 char printbuffer
[CONFIG_SYS_PBSIZE
];
638 memset(screen
, 0, sizeof(screen
));
644 /* For this to work, printbuffer must be larger than
645 * anything we ever want to print.
647 i
= vsnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
650 if ((screen
+ sizeof(screen
) - 1 - cursor
)
651 < strlen(printbuffer
) + 1) {
652 memset(screen
, 0, sizeof(screen
));
655 sprintf(cursor
, printbuffer
);
656 cursor
+= strlen(printbuffer
);
660 static inline void dbg(const char *fmt
, ...)
665 /** U-Boot INIT FUNCTIONS *************************************************/
667 struct stdio_dev
*search_device(int flags
, const char *name
)
669 struct stdio_dev
*dev
;
671 dev
= stdio_get_by_name(name
);
673 if (dev
&& (dev
->flags
& flags
))
679 int console_assign(int file
, const char *devname
)
682 struct stdio_dev
*dev
;
684 /* Check for valid file */
687 flag
= DEV_FLAGS_INPUT
;
691 flag
= DEV_FLAGS_OUTPUT
;
697 /* Check for valid device name */
699 dev
= search_device(flag
, devname
);
702 return console_setfile(file
, dev
);
707 /* Called before relocation - use serial functions */
708 int console_init_f(void)
710 gd
->have_console
= 1;
712 #ifdef CONFIG_SILENT_CONSOLE
713 if (getenv("silent") != NULL
)
714 gd
->flags
|= GD_FLG_SILENT
;
717 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL
);
722 void stdio_print_current_devices(void)
724 /* Print information */
726 if (stdio_devices
[stdin
] == NULL
) {
727 puts("No input devices available!\n");
729 printf ("%s\n", stdio_devices
[stdin
]->name
);
733 if (stdio_devices
[stdout
] == NULL
) {
734 puts("No output devices available!\n");
736 printf ("%s\n", stdio_devices
[stdout
]->name
);
740 if (stdio_devices
[stderr
] == NULL
) {
741 puts("No error devices available!\n");
743 printf ("%s\n", stdio_devices
[stderr
]->name
);
747 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
748 /* Called after the relocation - use desired console functions */
749 int console_init_r(void)
751 char *stdinname
, *stdoutname
, *stderrname
;
752 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
, *errdev
= NULL
;
753 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
755 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
756 #ifdef CONFIG_CONSOLE_MUX
760 /* set default handlers at first */
761 gd
->jt
->getc
= serial_getc
;
762 gd
->jt
->tstc
= serial_tstc
;
763 gd
->jt
->putc
= serial_putc
;
764 gd
->jt
->puts
= serial_puts
;
765 gd
->jt
->printf
= serial_printf
;
767 /* stdin stdout and stderr are in environment */
769 stdinname
= getenv("stdin");
770 stdoutname
= getenv("stdout");
771 stderrname
= getenv("stderr");
773 if (OVERWRITE_CONSOLE
== 0) { /* if not overwritten by config switch */
774 inputdev
= search_device(DEV_FLAGS_INPUT
, stdinname
);
775 outputdev
= search_device(DEV_FLAGS_OUTPUT
, stdoutname
);
776 errdev
= search_device(DEV_FLAGS_OUTPUT
, stderrname
);
777 #ifdef CONFIG_CONSOLE_MUX
778 iomux_err
= iomux_doenv(stdin
, stdinname
);
779 iomux_err
+= iomux_doenv(stdout
, stdoutname
);
780 iomux_err
+= iomux_doenv(stderr
, stderrname
);
782 /* Successful, so skip all the code below. */
786 /* if the devices are overwritten or not found, use default device */
787 if (inputdev
== NULL
) {
788 inputdev
= search_device(DEV_FLAGS_INPUT
, "serial");
790 if (outputdev
== NULL
) {
791 outputdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
793 if (errdev
== NULL
) {
794 errdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
796 /* Initializes output console first */
797 if (outputdev
!= NULL
) {
798 /* need to set a console if not done above. */
799 console_doenv(stdout
, outputdev
);
801 if (errdev
!= NULL
) {
802 /* need to set a console if not done above. */
803 console_doenv(stderr
, errdev
);
805 if (inputdev
!= NULL
) {
806 /* need to set a console if not done above. */
807 console_doenv(stdin
, inputdev
);
810 #ifdef CONFIG_CONSOLE_MUX
814 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
815 stdio_print_current_devices();
816 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
818 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
819 /* set the environment variables (will overwrite previous env settings) */
820 for (i
= 0; i
< 3; i
++) {
821 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
823 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
825 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
828 /* If nothing usable installed, use only the initial console */
829 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
832 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
);
836 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
838 /* Called after the relocation - use desired console functions */
839 int console_init_r(void)
841 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
;
843 struct list_head
*list
= stdio_get_list();
844 struct list_head
*pos
;
845 struct stdio_dev
*dev
;
847 #ifdef CONFIG_SPLASH_SCREEN
849 * suppress all output if splash screen is enabled and we have
850 * a bmp to display. We redirect the output from frame buffer
851 * console to serial console in this case or suppress it if
852 * "silent" mode was requested.
854 if (getenv("splashimage") != NULL
) {
855 if (!(gd
->flags
& GD_FLG_SILENT
))
856 outputdev
= search_device (DEV_FLAGS_OUTPUT
, "serial");
860 /* Scan devices looking for input and output devices */
861 list_for_each(pos
, list
) {
862 dev
= list_entry(pos
, struct stdio_dev
, list
);
864 if ((dev
->flags
& DEV_FLAGS_INPUT
) && (inputdev
== NULL
)) {
867 if ((dev
->flags
& DEV_FLAGS_OUTPUT
) && (outputdev
== NULL
)) {
870 if(inputdev
&& outputdev
)
874 /* Initializes output console first */
875 if (outputdev
!= NULL
) {
876 console_setfile(stdout
, outputdev
);
877 console_setfile(stderr
, outputdev
);
878 #ifdef CONFIG_CONSOLE_MUX
879 console_devices
[stdout
][0] = outputdev
;
880 console_devices
[stderr
][0] = outputdev
;
884 /* Initializes input console */
885 if (inputdev
!= NULL
) {
886 console_setfile(stdin
, inputdev
);
887 #ifdef CONFIG_CONSOLE_MUX
888 console_devices
[stdin
][0] = inputdev
;
892 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
893 stdio_print_current_devices();
894 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
896 /* Setting environment variables */
897 for (i
= 0; i
< 3; i
++) {
898 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
901 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
904 /* If nothing usable installed, use only the initial console */
905 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
908 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
);
912 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */