3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <debug_uart.h>
15 #include <stdio_dev.h>
17 #include <environment.h>
19 DECLARE_GLOBAL_DATA_PTR
;
21 static int on_console(const char *name
, const char *value
, enum env_op op
,
26 /* Check for console redirection */
27 if (strcmp(name
, "stdin") == 0)
29 else if (strcmp(name
, "stdout") == 0)
31 else if (strcmp(name
, "stderr") == 0)
34 /* if not actually setting a console variable, we don't care */
35 if (console
== -1 || (gd
->flags
& GD_FLG_DEVINIT
) == 0)
40 case env_op_overwrite
:
42 #ifdef CONFIG_CONSOLE_MUX
43 if (iomux_doenv(console
, value
))
46 /* Try assigning specified device */
47 if (console_assign(console
, value
) < 0)
49 #endif /* CONFIG_CONSOLE_MUX */
53 if ((flags
& H_FORCE
) == 0)
54 printf("Can't delete \"%s\"\n", name
);
61 U_BOOT_ENV_CALLBACK(console
, on_console
);
63 #ifdef CONFIG_SILENT_CONSOLE
64 static int on_silent(const char *name
, const char *value
, enum env_op op
,
67 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
68 if (flags
& H_INTERACTIVE
)
71 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
72 if ((flags
& H_INTERACTIVE
) == 0)
77 gd
->flags
|= GD_FLG_SILENT
;
79 gd
->flags
&= ~GD_FLG_SILENT
;
83 U_BOOT_ENV_CALLBACK(silent
, on_silent
);
86 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
88 * if overwrite_console returns 1, the stdin, stderr and stdout
89 * are switched to the serial port, else the settings in the
90 * environment are used
92 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
93 extern int overwrite_console(void);
94 #define OVERWRITE_CONSOLE overwrite_console()
96 #define OVERWRITE_CONSOLE 0
97 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
99 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
101 static int console_setfile(int file
, struct stdio_dev
* dev
)
112 /* Start new device */
114 error
= dev
->start(dev
);
115 /* If it's not started dont use it */
120 /* Assign the new device (leaving the existing one started) */
121 stdio_devices
[file
] = dev
;
124 * Update monitor functions
125 * (to use the console stuff by other applications)
135 gd
->jt
->printf
= printf
;
140 default: /* Invalid file ID */
146 #if defined(CONFIG_CONSOLE_MUX)
147 /** Console I/O multiplexing *******************************************/
149 static struct stdio_dev
*tstcdev
;
150 struct stdio_dev
**console_devices
[MAX_FILES
];
151 int cd_count
[MAX_FILES
];
154 * This depends on tstc() always being called before getc().
155 * This is guaranteed to be true because this routine is called
156 * only from fgetc() which assures it.
157 * No attempt is made to demultiplex multiple input sources.
159 static int console_getc(int file
)
163 /* This is never called with testcdev == NULL */
164 ret
= tstcdev
->getc(tstcdev
);
169 static int console_tstc(int file
)
172 struct stdio_dev
*dev
;
175 for (i
= 0; i
< cd_count
[file
]; i
++) {
176 dev
= console_devices
[file
][i
];
177 if (dev
->tstc
!= NULL
) {
178 ret
= dev
->tstc(dev
);
191 static void console_putc(int file
, const char c
)
194 struct stdio_dev
*dev
;
196 for (i
= 0; i
< cd_count
[file
]; i
++) {
197 dev
= console_devices
[file
][i
];
198 if (dev
->putc
!= NULL
)
203 #ifdef CONFIG_PRE_CONSOLE_BUFFER
204 static void console_puts_noserial(int file
, const char *s
)
207 struct stdio_dev
*dev
;
209 for (i
= 0; i
< cd_count
[file
]; i
++) {
210 dev
= console_devices
[file
][i
];
211 if (dev
->puts
!= NULL
&& strcmp(dev
->name
, "serial") != 0)
217 static void console_puts(int file
, const char *s
)
220 struct stdio_dev
*dev
;
222 for (i
= 0; i
< cd_count
[file
]; i
++) {
223 dev
= console_devices
[file
][i
];
224 if (dev
->puts
!= NULL
)
229 static inline void console_printdevs(int file
)
231 iomux_printdevs(file
);
234 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
236 iomux_doenv(file
, dev
->name
);
239 static inline int console_getc(int file
)
241 return stdio_devices
[file
]->getc(stdio_devices
[file
]);
244 static inline int console_tstc(int file
)
246 return stdio_devices
[file
]->tstc(stdio_devices
[file
]);
249 static inline void console_putc(int file
, const char c
)
251 stdio_devices
[file
]->putc(stdio_devices
[file
], c
);
254 #ifdef CONFIG_PRE_CONSOLE_BUFFER
255 static inline void console_puts_noserial(int file
, const char *s
)
257 if (strcmp(stdio_devices
[file
]->name
, "serial") != 0)
258 stdio_devices
[file
]->puts(stdio_devices
[file
], s
);
262 static inline void console_puts(int file
, const char *s
)
264 stdio_devices
[file
]->puts(stdio_devices
[file
], s
);
267 static inline void console_printdevs(int file
)
269 printf("%s\n", stdio_devices
[file
]->name
);
272 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
274 console_setfile(file
, dev
);
276 #endif /* defined(CONFIG_CONSOLE_MUX) */
278 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
280 int serial_printf(const char *fmt
, ...)
284 char printbuffer
[CONFIG_SYS_PBSIZE
];
288 /* For this to work, printbuffer must be larger than
289 * anything we ever want to print.
291 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
294 serial_puts(printbuffer
);
300 if (file
< MAX_FILES
) {
301 #if defined(CONFIG_CONSOLE_MUX)
303 * Effectively poll for input wherever it may be available.
307 * Upper layer may have already called tstc() so
308 * check for that first.
311 return console_getc(file
);
313 #ifdef CONFIG_WATCHDOG
315 * If the watchdog must be rate-limited then it should
316 * already be handled in board-specific code.
322 return console_getc(file
);
331 if (file
< MAX_FILES
)
332 return console_tstc(file
);
337 void fputc(int file
, const char c
)
339 if (file
< MAX_FILES
)
340 console_putc(file
, c
);
343 void fputs(int file
, const char *s
)
345 if (file
< MAX_FILES
)
346 console_puts(file
, s
);
349 int fprintf(int file
, const char *fmt
, ...)
353 char printbuffer
[CONFIG_SYS_PBSIZE
];
357 /* For this to work, printbuffer must be larger than
358 * anything we ever want to print.
360 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
363 /* Send to desired file */
364 fputs(file
, printbuffer
);
368 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
372 #ifdef CONFIG_DISABLE_CONSOLE
373 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
377 if (!gd
->have_console
)
380 if (gd
->flags
& GD_FLG_DEVINIT
) {
381 /* Get from the standard input */
385 /* Send directly to the handler */
386 return serial_getc();
391 #ifdef CONFIG_DISABLE_CONSOLE
392 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
396 if (!gd
->have_console
)
399 if (gd
->flags
& GD_FLG_DEVINIT
) {
400 /* Test the standard input */
404 /* Send directly to the handler */
405 return serial_tstc();
408 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
409 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
411 #ifdef CONFIG_PRE_CONSOLE_BUFFER
412 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
414 static void pre_console_putc(const char c
)
416 char *buffer
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
418 buffer
[CIRC_BUF_IDX(gd
->precon_buf_idx
++)] = c
;
421 static void pre_console_puts(const char *s
)
424 pre_console_putc(*s
++);
427 static void print_pre_console_buffer(int flushpoint
)
429 unsigned long in
= 0, out
= 0;
430 char *buf_in
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
431 char buf_out
[CONFIG_PRE_CON_BUF_SZ
+ 1];
433 if (gd
->precon_buf_idx
> CONFIG_PRE_CON_BUF_SZ
)
434 in
= gd
->precon_buf_idx
- CONFIG_PRE_CON_BUF_SZ
;
436 while (in
< gd
->precon_buf_idx
)
437 buf_out
[out
++] = buf_in
[CIRC_BUF_IDX(in
++)];
441 switch (flushpoint
) {
442 case PRE_CONSOLE_FLUSHPOINT1_SERIAL
:
445 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
:
446 console_puts_noserial(stdout
, buf_out
);
451 static inline void pre_console_putc(const char c
) {}
452 static inline void pre_console_puts(const char *s
) {}
453 static inline void print_pre_console_buffer(int flushpoint
) {}
456 void putc(const char c
)
458 #ifdef CONFIG_SANDBOX
459 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
464 #ifdef CONFIG_DEBUG_UART
465 /* if we don't have a console yet, use the debug UART */
466 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
471 #ifdef CONFIG_SILENT_CONSOLE
472 if (gd
->flags
& GD_FLG_SILENT
)
476 #ifdef CONFIG_DISABLE_CONSOLE
477 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
481 if (!gd
->have_console
)
482 return pre_console_putc(c
);
484 if (gd
->flags
& GD_FLG_DEVINIT
) {
485 /* Send to the standard output */
488 /* Send directly to the handler */
494 void puts(const char *s
)
496 #ifdef CONFIG_SANDBOX
497 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
502 #ifdef CONFIG_DEBUG_UART
503 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
514 #ifdef CONFIG_SILENT_CONSOLE
515 if (gd
->flags
& GD_FLG_SILENT
)
519 #ifdef CONFIG_DISABLE_CONSOLE
520 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
524 if (!gd
->have_console
)
525 return pre_console_puts(s
);
527 if (gd
->flags
& GD_FLG_DEVINIT
) {
528 /* Send to the standard output */
531 /* Send directly to the handler */
537 int printf(const char *fmt
, ...)
541 char printbuffer
[CONFIG_SYS_PBSIZE
];
543 #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_PRE_CONSOLE_BUFFER)
544 if (!gd
->have_console
)
550 /* For this to work, printbuffer must be larger than
551 * anything we ever want to print.
553 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
556 /* Print the string */
561 int vprintf(const char *fmt
, va_list args
)
564 char printbuffer
[CONFIG_SYS_PBSIZE
];
566 #if defined(CONFIG_PRE_CONSOLE_BUFFER) && !defined(CONFIG_SANDBOX)
567 if (!gd
->have_console
)
571 /* For this to work, printbuffer must be larger than
572 * anything we ever want to print.
574 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
576 /* Print the string */
581 /* test if ctrl-c was pressed */
582 static int ctrlc_disabled
= 0; /* see disable_ctrl() */
583 static int ctrlc_was_pressed
= 0;
586 #ifndef CONFIG_SANDBOX
587 if (!ctrlc_disabled
&& gd
->have_console
) {
590 case 0x03: /* ^C - Control C */
591 ctrlc_was_pressed
= 1;
602 /* Reads user's confirmation.
603 Returns 1 if user's input is "y", "Y", "yes" or "YES"
605 int confirm_yesno(void)
614 while (i
< sizeof(str_input
)) {
615 str_input
[i
] = getc();
617 if (str_input
[i
] == '\r')
622 if (strncmp(str_input
, "y\r", 2) == 0 ||
623 strncmp(str_input
, "Y\r", 2) == 0 ||
624 strncmp(str_input
, "yes\r", 4) == 0 ||
625 strncmp(str_input
, "YES\r", 4) == 0)
629 /* pass 1 to disable ctrlc() checking, 0 to enable.
630 * returns previous state
632 int disable_ctrlc(int disable
)
634 int prev
= ctrlc_disabled
; /* save previous state */
636 ctrlc_disabled
= disable
;
642 return ctrlc_was_pressed
;
645 void clear_ctrlc(void)
647 ctrlc_was_pressed
= 0;
650 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
652 char *cursor
= screen
;
654 inline void dbg(const char *fmt
, ...)
658 char printbuffer
[CONFIG_SYS_PBSIZE
];
661 memset(screen
, 0, sizeof(screen
));
667 /* For this to work, printbuffer must be larger than
668 * anything we ever want to print.
670 i
= vsnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
673 if ((screen
+ sizeof(screen
) - 1 - cursor
)
674 < strlen(printbuffer
) + 1) {
675 memset(screen
, 0, sizeof(screen
));
678 sprintf(cursor
, printbuffer
);
679 cursor
+= strlen(printbuffer
);
683 static inline void dbg(const char *fmt
, ...)
688 /** U-Boot INIT FUNCTIONS *************************************************/
690 struct stdio_dev
*search_device(int flags
, const char *name
)
692 struct stdio_dev
*dev
;
694 dev
= stdio_get_by_name(name
);
696 if (dev
&& (dev
->flags
& flags
))
702 int console_assign(int file
, const char *devname
)
705 struct stdio_dev
*dev
;
707 /* Check for valid file */
710 flag
= DEV_FLAGS_INPUT
;
714 flag
= DEV_FLAGS_OUTPUT
;
720 /* Check for valid device name */
722 dev
= search_device(flag
, devname
);
725 return console_setfile(file
, dev
);
730 /* Called before relocation - use serial functions */
731 int console_init_f(void)
733 gd
->have_console
= 1;
735 #ifdef CONFIG_SILENT_CONSOLE
736 if (getenv("silent") != NULL
)
737 gd
->flags
|= GD_FLG_SILENT
;
740 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL
);
745 void stdio_print_current_devices(void)
747 /* Print information */
749 if (stdio_devices
[stdin
] == NULL
) {
750 puts("No input devices available!\n");
752 printf ("%s\n", stdio_devices
[stdin
]->name
);
756 if (stdio_devices
[stdout
] == NULL
) {
757 puts("No output devices available!\n");
759 printf ("%s\n", stdio_devices
[stdout
]->name
);
763 if (stdio_devices
[stderr
] == NULL
) {
764 puts("No error devices available!\n");
766 printf ("%s\n", stdio_devices
[stderr
]->name
);
770 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
771 /* Called after the relocation - use desired console functions */
772 int console_init_r(void)
774 char *stdinname
, *stdoutname
, *stderrname
;
775 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
, *errdev
= NULL
;
776 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
778 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
779 #ifdef CONFIG_CONSOLE_MUX
783 /* set default handlers at first */
784 gd
->jt
->getc
= serial_getc
;
785 gd
->jt
->tstc
= serial_tstc
;
786 gd
->jt
->putc
= serial_putc
;
787 gd
->jt
->puts
= serial_puts
;
788 gd
->jt
->printf
= serial_printf
;
790 /* stdin stdout and stderr are in environment */
792 stdinname
= getenv("stdin");
793 stdoutname
= getenv("stdout");
794 stderrname
= getenv("stderr");
796 if (OVERWRITE_CONSOLE
== 0) { /* if not overwritten by config switch */
797 inputdev
= search_device(DEV_FLAGS_INPUT
, stdinname
);
798 outputdev
= search_device(DEV_FLAGS_OUTPUT
, stdoutname
);
799 errdev
= search_device(DEV_FLAGS_OUTPUT
, stderrname
);
800 #ifdef CONFIG_CONSOLE_MUX
801 iomux_err
= iomux_doenv(stdin
, stdinname
);
802 iomux_err
+= iomux_doenv(stdout
, stdoutname
);
803 iomux_err
+= iomux_doenv(stderr
, stderrname
);
805 /* Successful, so skip all the code below. */
809 /* if the devices are overwritten or not found, use default device */
810 if (inputdev
== NULL
) {
811 inputdev
= search_device(DEV_FLAGS_INPUT
, "serial");
813 if (outputdev
== NULL
) {
814 outputdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
816 if (errdev
== NULL
) {
817 errdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
819 /* Initializes output console first */
820 if (outputdev
!= NULL
) {
821 /* need to set a console if not done above. */
822 console_doenv(stdout
, outputdev
);
824 if (errdev
!= NULL
) {
825 /* need to set a console if not done above. */
826 console_doenv(stderr
, errdev
);
828 if (inputdev
!= NULL
) {
829 /* need to set a console if not done above. */
830 console_doenv(stdin
, inputdev
);
833 #ifdef CONFIG_CONSOLE_MUX
837 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
838 stdio_print_current_devices();
839 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
841 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
842 /* set the environment variables (will overwrite previous env settings) */
843 for (i
= 0; i
< 3; i
++) {
844 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
846 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
848 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
851 /* If nothing usable installed, use only the initial console */
852 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
855 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
);
859 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
861 /* Called after the relocation - use desired console functions */
862 int console_init_r(void)
864 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
;
866 struct list_head
*list
= stdio_get_list();
867 struct list_head
*pos
;
868 struct stdio_dev
*dev
;
870 #ifdef CONFIG_SPLASH_SCREEN
872 * suppress all output if splash screen is enabled and we have
873 * a bmp to display. We redirect the output from frame buffer
874 * console to serial console in this case or suppress it if
875 * "silent" mode was requested.
877 if (getenv("splashimage") != NULL
) {
878 if (!(gd
->flags
& GD_FLG_SILENT
))
879 outputdev
= search_device (DEV_FLAGS_OUTPUT
, "serial");
883 /* Scan devices looking for input and output devices */
884 list_for_each(pos
, list
) {
885 dev
= list_entry(pos
, struct stdio_dev
, list
);
887 if ((dev
->flags
& DEV_FLAGS_INPUT
) && (inputdev
== NULL
)) {
890 if ((dev
->flags
& DEV_FLAGS_OUTPUT
) && (outputdev
== NULL
)) {
893 if(inputdev
&& outputdev
)
897 /* Initializes output console first */
898 if (outputdev
!= NULL
) {
899 console_setfile(stdout
, outputdev
);
900 console_setfile(stderr
, outputdev
);
901 #ifdef CONFIG_CONSOLE_MUX
902 console_devices
[stdout
][0] = outputdev
;
903 console_devices
[stderr
][0] = outputdev
;
907 /* Initializes input console */
908 if (inputdev
!= NULL
) {
909 console_setfile(stdin
, inputdev
);
910 #ifdef CONFIG_CONSOLE_MUX
911 console_devices
[stdin
][0] = inputdev
;
915 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
916 stdio_print_current_devices();
917 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
919 /* Setting environment variables */
920 for (i
= 0; i
< 3; i
++) {
921 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
924 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
927 /* If nothing usable installed, use only the initial console */
928 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
931 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
);
935 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */