3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <debug_uart.h>
16 #include <stdio_dev.h>
18 #include <environment.h>
20 DECLARE_GLOBAL_DATA_PTR
;
22 static int on_console(const char *name
, const char *value
, enum env_op op
,
27 /* Check for console redirection */
28 if (strcmp(name
, "stdin") == 0)
30 else if (strcmp(name
, "stdout") == 0)
32 else if (strcmp(name
, "stderr") == 0)
35 /* if not actually setting a console variable, we don't care */
36 if (console
== -1 || (gd
->flags
& GD_FLG_DEVINIT
) == 0)
41 case env_op_overwrite
:
43 #ifdef CONFIG_CONSOLE_MUX
44 if (iomux_doenv(console
, value
))
47 /* Try assigning specified device */
48 if (console_assign(console
, value
) < 0)
50 #endif /* CONFIG_CONSOLE_MUX */
54 if ((flags
& H_FORCE
) == 0)
55 printf("Can't delete \"%s\"\n", name
);
62 U_BOOT_ENV_CALLBACK(console
, on_console
);
64 #ifdef CONFIG_SILENT_CONSOLE
65 static int on_silent(const char *name
, const char *value
, enum env_op op
,
68 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
69 if (flags
& H_INTERACTIVE
)
72 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
73 if ((flags
& H_INTERACTIVE
) == 0)
78 gd
->flags
|= GD_FLG_SILENT
;
80 gd
->flags
&= ~GD_FLG_SILENT
;
84 U_BOOT_ENV_CALLBACK(silent
, on_silent
);
87 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
89 * if overwrite_console returns 1, the stdin, stderr and stdout
90 * are switched to the serial port, else the settings in the
91 * environment are used
93 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
94 extern int overwrite_console(void);
95 #define OVERWRITE_CONSOLE overwrite_console()
97 #define OVERWRITE_CONSOLE 0
98 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
100 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
102 static int console_setfile(int file
, struct stdio_dev
* dev
)
113 /* Start new device */
115 error
= dev
->start(dev
);
116 /* If it's not started dont use it */
121 /* Assign the new device (leaving the existing one started) */
122 stdio_devices
[file
] = dev
;
125 * Update monitor functions
126 * (to use the console stuff by other applications)
136 gd
->jt
->printf
= printf
;
141 default: /* Invalid file ID */
147 #if defined(CONFIG_CONSOLE_MUX)
148 /** Console I/O multiplexing *******************************************/
150 static struct stdio_dev
*tstcdev
;
151 struct stdio_dev
**console_devices
[MAX_FILES
];
152 int cd_count
[MAX_FILES
];
155 * This depends on tstc() always being called before getc().
156 * This is guaranteed to be true because this routine is called
157 * only from fgetc() which assures it.
158 * No attempt is made to demultiplex multiple input sources.
160 static int console_getc(int file
)
164 /* This is never called with testcdev == NULL */
165 ret
= tstcdev
->getc(tstcdev
);
170 static int console_tstc(int file
)
173 struct stdio_dev
*dev
;
176 for (i
= 0; i
< cd_count
[file
]; i
++) {
177 dev
= console_devices
[file
][i
];
178 if (dev
->tstc
!= NULL
) {
179 ret
= dev
->tstc(dev
);
192 static void console_putc(int file
, const char c
)
195 struct stdio_dev
*dev
;
197 for (i
= 0; i
< cd_count
[file
]; i
++) {
198 dev
= console_devices
[file
][i
];
199 if (dev
->putc
!= NULL
)
204 #ifdef CONFIG_PRE_CONSOLE_BUFFER
205 static void console_puts_noserial(int file
, const char *s
)
208 struct stdio_dev
*dev
;
210 for (i
= 0; i
< cd_count
[file
]; i
++) {
211 dev
= console_devices
[file
][i
];
212 if (dev
->puts
!= NULL
&& strcmp(dev
->name
, "serial") != 0)
218 static void console_puts(int file
, const char *s
)
221 struct stdio_dev
*dev
;
223 for (i
= 0; i
< cd_count
[file
]; i
++) {
224 dev
= console_devices
[file
][i
];
225 if (dev
->puts
!= NULL
)
230 static inline void console_printdevs(int file
)
232 iomux_printdevs(file
);
235 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
237 iomux_doenv(file
, dev
->name
);
240 static inline int console_getc(int file
)
242 return stdio_devices
[file
]->getc(stdio_devices
[file
]);
245 static inline int console_tstc(int file
)
247 return stdio_devices
[file
]->tstc(stdio_devices
[file
]);
250 static inline void console_putc(int file
, const char c
)
252 stdio_devices
[file
]->putc(stdio_devices
[file
], c
);
255 #ifdef CONFIG_PRE_CONSOLE_BUFFER
256 static inline void console_puts_noserial(int file
, const char *s
)
258 if (strcmp(stdio_devices
[file
]->name
, "serial") != 0)
259 stdio_devices
[file
]->puts(stdio_devices
[file
], s
);
263 static inline void console_puts(int file
, const char *s
)
265 stdio_devices
[file
]->puts(stdio_devices
[file
], s
);
268 static inline void console_printdevs(int file
)
270 printf("%s\n", stdio_devices
[file
]->name
);
273 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
275 console_setfile(file
, dev
);
277 #endif /* defined(CONFIG_CONSOLE_MUX) */
279 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
281 int serial_printf(const char *fmt
, ...)
285 char printbuffer
[CONFIG_SYS_PBSIZE
];
289 /* For this to work, printbuffer must be larger than
290 * anything we ever want to print.
292 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
295 serial_puts(printbuffer
);
301 if (file
< MAX_FILES
) {
302 #if defined(CONFIG_CONSOLE_MUX)
304 * Effectively poll for input wherever it may be available.
308 * Upper layer may have already called tstc() so
309 * check for that first.
312 return console_getc(file
);
314 #ifdef CONFIG_WATCHDOG
316 * If the watchdog must be rate-limited then it should
317 * already be handled in board-specific code.
323 return console_getc(file
);
332 if (file
< MAX_FILES
)
333 return console_tstc(file
);
338 void fputc(int file
, const char c
)
340 if (file
< MAX_FILES
)
341 console_putc(file
, c
);
344 void fputs(int file
, const char *s
)
346 if (file
< MAX_FILES
)
347 console_puts(file
, s
);
350 int fprintf(int file
, const char *fmt
, ...)
354 char printbuffer
[CONFIG_SYS_PBSIZE
];
358 /* For this to work, printbuffer must be larger than
359 * anything we ever want to print.
361 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
364 /* Send to desired file */
365 fputs(file
, printbuffer
);
369 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
373 #ifdef CONFIG_DISABLE_CONSOLE
374 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
378 if (!gd
->have_console
)
381 if (gd
->flags
& GD_FLG_DEVINIT
) {
382 /* Get from the standard input */
386 /* Send directly to the handler */
387 return serial_getc();
392 #ifdef CONFIG_DISABLE_CONSOLE
393 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
397 if (!gd
->have_console
)
400 if (gd
->flags
& GD_FLG_DEVINIT
) {
401 /* Test the standard input */
405 /* Send directly to the handler */
406 return serial_tstc();
409 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
410 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
412 #ifdef CONFIG_PRE_CONSOLE_BUFFER
413 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
415 static void pre_console_putc(const char c
)
417 char *buffer
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
419 buffer
[CIRC_BUF_IDX(gd
->precon_buf_idx
++)] = c
;
422 static void pre_console_puts(const char *s
)
425 pre_console_putc(*s
++);
428 static void print_pre_console_buffer(int flushpoint
)
430 unsigned long in
= 0, out
= 0;
431 char *buf_in
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
432 char buf_out
[CONFIG_PRE_CON_BUF_SZ
+ 1];
434 if (gd
->precon_buf_idx
> CONFIG_PRE_CON_BUF_SZ
)
435 in
= gd
->precon_buf_idx
- CONFIG_PRE_CON_BUF_SZ
;
437 while (in
< gd
->precon_buf_idx
)
438 buf_out
[out
++] = buf_in
[CIRC_BUF_IDX(in
++)];
442 switch (flushpoint
) {
443 case PRE_CONSOLE_FLUSHPOINT1_SERIAL
:
446 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
:
447 console_puts_noserial(stdout
, buf_out
);
452 static inline void pre_console_putc(const char c
) {}
453 static inline void pre_console_puts(const char *s
) {}
454 static inline void print_pre_console_buffer(int flushpoint
) {}
457 void putc(const char c
)
459 #ifdef CONFIG_SANDBOX
460 /* sandbox can send characters to stdout before it has a console */
461 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
466 #ifdef CONFIG_DEBUG_UART
467 /* if we don't have a console yet, use the debug UART */
468 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
473 #ifdef CONFIG_SILENT_CONSOLE
474 if (gd
->flags
& GD_FLG_SILENT
)
478 #ifdef CONFIG_DISABLE_CONSOLE
479 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
483 if (!gd
->have_console
)
484 return pre_console_putc(c
);
486 if (gd
->flags
& GD_FLG_DEVINIT
) {
487 /* Send to the standard output */
490 /* Send directly to the handler */
496 void puts(const char *s
)
498 #ifdef CONFIG_SANDBOX
499 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
504 #ifdef CONFIG_DEBUG_UART
505 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
516 #ifdef CONFIG_SILENT_CONSOLE
517 if (gd
->flags
& GD_FLG_SILENT
)
521 #ifdef CONFIG_DISABLE_CONSOLE
522 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
526 if (!gd
->have_console
)
527 return pre_console_puts(s
);
529 if (gd
->flags
& GD_FLG_DEVINIT
) {
530 /* Send to the standard output */
533 /* Send directly to the handler */
539 int printf(const char *fmt
, ...)
543 char printbuffer
[CONFIG_SYS_PBSIZE
];
547 /* For this to work, printbuffer must be larger than
548 * anything we ever want to print.
550 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
553 /* Print the string */
558 int vprintf(const char *fmt
, va_list args
)
561 char printbuffer
[CONFIG_SYS_PBSIZE
];
563 #if defined(CONFIG_PRE_CONSOLE_BUFFER) && !defined(CONFIG_SANDBOX)
564 if (!gd
->have_console
)
568 /* For this to work, printbuffer must be larger than
569 * anything we ever want to print.
571 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
573 /* Print the string */
578 /* test if ctrl-c was pressed */
579 static int ctrlc_disabled
= 0; /* see disable_ctrl() */
580 static int ctrlc_was_pressed
= 0;
583 #ifndef CONFIG_SANDBOX
584 if (!ctrlc_disabled
&& gd
->have_console
) {
587 case 0x03: /* ^C - Control C */
588 ctrlc_was_pressed
= 1;
599 /* Reads user's confirmation.
600 Returns 1 if user's input is "y", "Y", "yes" or "YES"
602 int confirm_yesno(void)
611 while (i
< sizeof(str_input
)) {
612 str_input
[i
] = getc();
614 if (str_input
[i
] == '\r')
619 if (strncmp(str_input
, "y\r", 2) == 0 ||
620 strncmp(str_input
, "Y\r", 2) == 0 ||
621 strncmp(str_input
, "yes\r", 4) == 0 ||
622 strncmp(str_input
, "YES\r", 4) == 0)
626 /* pass 1 to disable ctrlc() checking, 0 to enable.
627 * returns previous state
629 int disable_ctrlc(int disable
)
631 int prev
= ctrlc_disabled
; /* save previous state */
633 ctrlc_disabled
= disable
;
639 return ctrlc_was_pressed
;
642 void clear_ctrlc(void)
644 ctrlc_was_pressed
= 0;
647 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
649 char *cursor
= screen
;
651 inline void dbg(const char *fmt
, ...)
655 char printbuffer
[CONFIG_SYS_PBSIZE
];
658 memset(screen
, 0, sizeof(screen
));
664 /* For this to work, printbuffer must be larger than
665 * anything we ever want to print.
667 i
= vsnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
670 if ((screen
+ sizeof(screen
) - 1 - cursor
)
671 < strlen(printbuffer
) + 1) {
672 memset(screen
, 0, sizeof(screen
));
675 sprintf(cursor
, printbuffer
);
676 cursor
+= strlen(printbuffer
);
680 static inline void dbg(const char *fmt
, ...)
685 /** U-Boot INIT FUNCTIONS *************************************************/
687 struct stdio_dev
*search_device(int flags
, const char *name
)
689 struct stdio_dev
*dev
;
691 dev
= stdio_get_by_name(name
);
693 if (dev
&& (dev
->flags
& flags
))
699 int console_assign(int file
, const char *devname
)
702 struct stdio_dev
*dev
;
704 /* Check for valid file */
707 flag
= DEV_FLAGS_INPUT
;
711 flag
= DEV_FLAGS_OUTPUT
;
717 /* Check for valid device name */
719 dev
= search_device(flag
, devname
);
722 return console_setfile(file
, dev
);
727 /* Called before relocation - use serial functions */
728 int console_init_f(void)
730 gd
->have_console
= 1;
732 #ifdef CONFIG_SILENT_CONSOLE
733 if (getenv("silent") != NULL
)
734 gd
->flags
|= GD_FLG_SILENT
;
737 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL
);
742 void stdio_print_current_devices(void)
744 /* Print information */
746 if (stdio_devices
[stdin
] == NULL
) {
747 puts("No input devices available!\n");
749 printf ("%s\n", stdio_devices
[stdin
]->name
);
753 if (stdio_devices
[stdout
] == NULL
) {
754 puts("No output devices available!\n");
756 printf ("%s\n", stdio_devices
[stdout
]->name
);
760 if (stdio_devices
[stderr
] == NULL
) {
761 puts("No error devices available!\n");
763 printf ("%s\n", stdio_devices
[stderr
]->name
);
767 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
768 /* Called after the relocation - use desired console functions */
769 int console_init_r(void)
771 char *stdinname
, *stdoutname
, *stderrname
;
772 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
, *errdev
= NULL
;
773 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
775 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
776 #ifdef CONFIG_CONSOLE_MUX
780 /* set default handlers at first */
781 gd
->jt
->getc
= serial_getc
;
782 gd
->jt
->tstc
= serial_tstc
;
783 gd
->jt
->putc
= serial_putc
;
784 gd
->jt
->puts
= serial_puts
;
785 gd
->jt
->printf
= serial_printf
;
787 /* stdin stdout and stderr are in environment */
789 stdinname
= getenv("stdin");
790 stdoutname
= getenv("stdout");
791 stderrname
= getenv("stderr");
793 if (OVERWRITE_CONSOLE
== 0) { /* if not overwritten by config switch */
794 inputdev
= search_device(DEV_FLAGS_INPUT
, stdinname
);
795 outputdev
= search_device(DEV_FLAGS_OUTPUT
, stdoutname
);
796 errdev
= search_device(DEV_FLAGS_OUTPUT
, stderrname
);
797 #ifdef CONFIG_CONSOLE_MUX
798 iomux_err
= iomux_doenv(stdin
, stdinname
);
799 iomux_err
+= iomux_doenv(stdout
, stdoutname
);
800 iomux_err
+= iomux_doenv(stderr
, stderrname
);
802 /* Successful, so skip all the code below. */
806 /* if the devices are overwritten or not found, use default device */
807 if (inputdev
== NULL
) {
808 inputdev
= search_device(DEV_FLAGS_INPUT
, "serial");
810 if (outputdev
== NULL
) {
811 outputdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
813 if (errdev
== NULL
) {
814 errdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
816 /* Initializes output console first */
817 if (outputdev
!= NULL
) {
818 /* need to set a console if not done above. */
819 console_doenv(stdout
, outputdev
);
821 if (errdev
!= NULL
) {
822 /* need to set a console if not done above. */
823 console_doenv(stderr
, errdev
);
825 if (inputdev
!= NULL
) {
826 /* need to set a console if not done above. */
827 console_doenv(stdin
, inputdev
);
830 #ifdef CONFIG_CONSOLE_MUX
834 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
835 stdio_print_current_devices();
836 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
838 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
839 /* set the environment variables (will overwrite previous env settings) */
840 for (i
= 0; i
< 3; i
++) {
841 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
843 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
845 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
848 /* If nothing usable installed, use only the initial console */
849 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
852 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
);
856 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
858 /* Called after the relocation - use desired console functions */
859 int console_init_r(void)
861 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
;
863 struct list_head
*list
= stdio_get_list();
864 struct list_head
*pos
;
865 struct stdio_dev
*dev
;
867 #ifdef CONFIG_SPLASH_SCREEN
869 * suppress all output if splash screen is enabled and we have
870 * a bmp to display. We redirect the output from frame buffer
871 * console to serial console in this case or suppress it if
872 * "silent" mode was requested.
874 if (getenv("splashimage") != NULL
) {
875 if (!(gd
->flags
& GD_FLG_SILENT
))
876 outputdev
= search_device (DEV_FLAGS_OUTPUT
, "serial");
880 /* Scan devices looking for input and output devices */
881 list_for_each(pos
, list
) {
882 dev
= list_entry(pos
, struct stdio_dev
, list
);
884 if ((dev
->flags
& DEV_FLAGS_INPUT
) && (inputdev
== NULL
)) {
887 if ((dev
->flags
& DEV_FLAGS_OUTPUT
) && (outputdev
== NULL
)) {
890 if(inputdev
&& outputdev
)
894 /* Initializes output console first */
895 if (outputdev
!= NULL
) {
896 console_setfile(stdout
, outputdev
);
897 console_setfile(stderr
, outputdev
);
898 #ifdef CONFIG_CONSOLE_MUX
899 console_devices
[stdout
][0] = outputdev
;
900 console_devices
[stderr
][0] = outputdev
;
904 /* Initializes input console */
905 if (inputdev
!= NULL
) {
906 console_setfile(stdin
, inputdev
);
907 #ifdef CONFIG_CONSOLE_MUX
908 console_devices
[stdin
][0] = inputdev
;
912 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
913 stdio_print_current_devices();
914 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
916 /* Setting environment variables */
917 for (i
= 0; i
< 3; i
++) {
918 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
921 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
924 /* If nothing usable installed, use only the initial console */
925 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
928 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
);
932 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */