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>
17 #include <stdio_dev.h>
19 #include <environment.h>
22 DECLARE_GLOBAL_DATA_PTR
;
24 static int on_console(const char *name
, const char *value
, enum env_op op
,
29 /* Check for console redirection */
30 if (strcmp(name
, "stdin") == 0)
32 else if (strcmp(name
, "stdout") == 0)
34 else if (strcmp(name
, "stderr") == 0)
37 /* if not actually setting a console variable, we don't care */
38 if (console
== -1 || (gd
->flags
& GD_FLG_DEVINIT
) == 0)
43 case env_op_overwrite
:
45 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
46 if (iomux_doenv(console
, value
))
49 /* Try assigning specified device */
50 if (console_assign(console
, value
) < 0)
56 if ((flags
& H_FORCE
) == 0)
57 printf("Can't delete \"%s\"\n", name
);
64 U_BOOT_ENV_CALLBACK(console
, on_console
);
66 #ifdef CONFIG_SILENT_CONSOLE
67 static int on_silent(const char *name
, const char *value
, enum env_op op
,
70 #if !CONFIG_IS_ENABLED(CONFIG_SILENT_CONSOLE_UPDATE_ON_SET)
71 if (flags
& H_INTERACTIVE
)
74 #if !CONFIG_IS_ENABLED(CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC)
75 if ((flags
& H_INTERACTIVE
) == 0)
80 gd
->flags
|= GD_FLG_SILENT
;
82 gd
->flags
&= ~GD_FLG_SILENT
;
86 U_BOOT_ENV_CALLBACK(silent
, on_silent
);
89 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
91 * if overwrite_console returns 1, the stdin, stderr and stdout
92 * are switched to the serial port, else the settings in the
93 * environment are used
95 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
96 extern int overwrite_console(void);
97 #define OVERWRITE_CONSOLE overwrite_console()
99 #define OVERWRITE_CONSOLE 0
100 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
102 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
104 static int console_setfile(int file
, struct stdio_dev
* dev
)
115 /* Start new device */
117 error
= dev
->start(dev
);
118 /* If it's not started dont use it */
123 /* Assign the new device (leaving the existing one started) */
124 stdio_devices
[file
] = dev
;
127 * Update monitor functions
128 * (to use the console stuff by other applications)
138 gd
->jt
->printf
= printf
;
143 default: /* Invalid file ID */
150 * console_dev_is_serial() - Check if a stdio device is a serial device
152 * @sdev: Device to check
153 * @return true if this device is a serial device
155 static bool console_dev_is_serial(struct stdio_dev
*sdev
)
159 is_serial
= !strcmp(sdev
->name
, "serial");
164 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
165 /** Console I/O multiplexing *******************************************/
167 static struct stdio_dev
*tstcdev
;
168 struct stdio_dev
**console_devices
[MAX_FILES
];
169 int cd_count
[MAX_FILES
];
172 * This depends on tstc() always being called before getc().
173 * This is guaranteed to be true because this routine is called
174 * only from fgetc() which assures it.
175 * No attempt is made to demultiplex multiple input sources.
177 static int console_getc(int file
)
181 /* This is never called with testcdev == NULL */
182 ret
= tstcdev
->getc(tstcdev
);
187 static int console_tstc(int file
)
190 struct stdio_dev
*dev
;
193 for (i
= 0; i
< cd_count
[file
]; i
++) {
194 dev
= console_devices
[file
][i
];
195 if (dev
->tstc
!= NULL
) {
196 ret
= dev
->tstc(dev
);
209 static void console_putc(int file
, const char c
)
212 struct stdio_dev
*dev
;
214 for (i
= 0; i
< cd_count
[file
]; i
++) {
215 dev
= console_devices
[file
][i
];
216 if (dev
->putc
!= NULL
)
221 static void console_puts_noserial(int file
, const char *s
)
224 struct stdio_dev
*dev
;
226 for (i
= 0; i
< cd_count
[file
]; i
++) {
227 dev
= console_devices
[file
][i
];
228 if (dev
->puts
!= NULL
&& !console_dev_is_serial(dev
))
233 static void console_puts(int file
, const char *s
)
236 struct stdio_dev
*dev
;
238 for (i
= 0; i
< cd_count
[file
]; i
++) {
239 dev
= console_devices
[file
][i
];
240 if (dev
->puts
!= NULL
)
245 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
247 iomux_doenv(file
, dev
->name
);
250 static inline int console_getc(int file
)
252 return stdio_devices
[file
]->getc(stdio_devices
[file
]);
255 static inline int console_tstc(int file
)
257 return stdio_devices
[file
]->tstc(stdio_devices
[file
]);
260 static inline void console_putc(int file
, const char c
)
262 stdio_devices
[file
]->putc(stdio_devices
[file
], c
);
265 static inline void console_puts_noserial(int file
, const char *s
)
267 if (!console_dev_is_serial(stdio_devices
[file
]))
268 stdio_devices
[file
]->puts(stdio_devices
[file
], s
);
271 static inline void console_puts(int file
, const char *s
)
273 stdio_devices
[file
]->puts(stdio_devices
[file
], s
);
276 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
278 console_setfile(file
, dev
);
280 #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
282 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
284 int serial_printf(const char *fmt
, ...)
288 char printbuffer
[CONFIG_SYS_PBSIZE
];
292 /* For this to work, printbuffer must be larger than
293 * anything we ever want to print.
295 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
298 serial_puts(printbuffer
);
304 if (file
< MAX_FILES
) {
305 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
307 * Effectively poll for input wherever it may be available.
312 * Upper layer may have already called tstc() so
313 * check for that first.
316 return console_getc(file
);
318 #ifdef CONFIG_WATCHDOG
320 * If the watchdog must be rate-limited then it should
321 * already be handled in board-specific code.
327 return console_getc(file
);
336 if (file
< MAX_FILES
)
337 return console_tstc(file
);
342 void fputc(int file
, const char c
)
344 if (file
< MAX_FILES
)
345 console_putc(file
, c
);
348 void fputs(int file
, const char *s
)
350 if (file
< MAX_FILES
)
351 console_puts(file
, s
);
354 int fprintf(int file
, const char *fmt
, ...)
358 char printbuffer
[CONFIG_SYS_PBSIZE
];
362 /* For this to work, printbuffer must be larger than
363 * anything we ever want to print.
365 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
368 /* Send to desired file */
369 fputs(file
, printbuffer
);
373 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
377 #ifdef CONFIG_DISABLE_CONSOLE
378 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
382 if (!gd
->have_console
)
385 #ifdef CONFIG_CONSOLE_RECORD
386 if (gd
->console_in
.start
) {
389 ch
= membuff_getbyte(&gd
->console_in
);
394 if (gd
->flags
& GD_FLG_DEVINIT
) {
395 /* Get from the standard input */
399 /* Send directly to the handler */
400 return serial_getc();
405 #ifdef CONFIG_DISABLE_CONSOLE
406 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
410 if (!gd
->have_console
)
412 #ifdef CONFIG_CONSOLE_RECORD
413 if (gd
->console_in
.start
) {
414 if (membuff_peekbyte(&gd
->console_in
) != -1)
418 if (gd
->flags
& GD_FLG_DEVINIT
) {
419 /* Test the standard input */
423 /* Send directly to the handler */
424 return serial_tstc();
427 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
428 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
430 #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
431 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
433 static void pre_console_putc(const char c
)
437 buffer
= map_sysmem(CONFIG_PRE_CON_BUF_ADDR
, CONFIG_PRE_CON_BUF_SZ
);
439 buffer
[CIRC_BUF_IDX(gd
->precon_buf_idx
++)] = c
;
441 unmap_sysmem(buffer
);
444 static void print_pre_console_buffer(int flushpoint
)
446 unsigned long in
= 0, out
= 0;
447 char buf_out
[CONFIG_PRE_CON_BUF_SZ
+ 1];
450 buf_in
= map_sysmem(CONFIG_PRE_CON_BUF_ADDR
, CONFIG_PRE_CON_BUF_SZ
);
451 if (gd
->precon_buf_idx
> CONFIG_PRE_CON_BUF_SZ
)
452 in
= gd
->precon_buf_idx
- CONFIG_PRE_CON_BUF_SZ
;
454 while (in
< gd
->precon_buf_idx
)
455 buf_out
[out
++] = buf_in
[CIRC_BUF_IDX(in
++)];
456 unmap_sysmem(buf_in
);
460 switch (flushpoint
) {
461 case PRE_CONSOLE_FLUSHPOINT1_SERIAL
:
464 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
:
465 console_puts_noserial(stdout
, buf_out
);
470 static inline void pre_console_putc(const char c
) {}
471 static inline void print_pre_console_buffer(int flushpoint
) {}
474 void putc(const char c
)
476 #ifdef CONFIG_DEBUG_UART
477 /* if we don't have a console yet, use the debug UART */
478 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
483 #ifdef CONFIG_CONSOLE_RECORD
484 if (gd
&& (gd
->flags
& GD_FLG_RECORD
) && gd
->console_out
.start
)
485 membuff_putbyte(&gd
->console_out
, c
);
487 #ifdef CONFIG_SILENT_CONSOLE
488 if (gd
->flags
& GD_FLG_SILENT
)
492 #ifdef CONFIG_DISABLE_CONSOLE
493 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
497 if (!gd
->have_console
)
498 return pre_console_putc(c
);
500 if (gd
->flags
& GD_FLG_DEVINIT
) {
501 /* Send to the standard output */
504 /* Send directly to the handler */
510 void puts(const char *s
)
516 #ifdef CONFIG_CONSOLE_RECORD
517 int console_record_init(void)
521 ret
= membuff_new(&gd
->console_out
, CONFIG_CONSOLE_RECORD_OUT_SIZE
);
524 ret
= membuff_new(&gd
->console_in
, CONFIG_CONSOLE_RECORD_IN_SIZE
);
529 void console_record_reset(void)
531 membuff_purge(&gd
->console_out
);
532 membuff_purge(&gd
->console_in
);
535 void console_record_reset_enable(void)
537 console_record_reset();
538 gd
->flags
|= GD_FLG_RECORD
;
542 /* test if ctrl-c was pressed */
543 static int ctrlc_disabled
= 0; /* see disable_ctrl() */
544 static int ctrlc_was_pressed
= 0;
547 #ifndef CONFIG_SANDBOX
548 if (!ctrlc_disabled
&& gd
->have_console
) {
551 case 0x03: /* ^C - Control C */
552 ctrlc_was_pressed
= 1;
563 /* Reads user's confirmation.
564 Returns 1 if user's input is "y", "Y", "yes" or "YES"
566 int confirm_yesno(void)
575 while (i
< sizeof(str_input
)) {
576 str_input
[i
] = getc();
578 if (str_input
[i
] == '\r')
583 if (strncmp(str_input
, "y\r", 2) == 0 ||
584 strncmp(str_input
, "Y\r", 2) == 0 ||
585 strncmp(str_input
, "yes\r", 4) == 0 ||
586 strncmp(str_input
, "YES\r", 4) == 0)
590 /* pass 1 to disable ctrlc() checking, 0 to enable.
591 * returns previous state
593 int disable_ctrlc(int disable
)
595 int prev
= ctrlc_disabled
; /* save previous state */
597 ctrlc_disabled
= disable
;
603 return ctrlc_was_pressed
;
606 void clear_ctrlc(void)
608 ctrlc_was_pressed
= 0;
611 /** U-Boot INIT FUNCTIONS *************************************************/
613 struct stdio_dev
*search_device(int flags
, const char *name
)
615 struct stdio_dev
*dev
;
617 dev
= stdio_get_by_name(name
);
618 #ifdef CONFIG_VIDCONSOLE_AS_LCD
619 if (!dev
&& !strcmp(name
, "lcd"))
620 dev
= stdio_get_by_name("vidconsole");
623 if (dev
&& (dev
->flags
& flags
))
629 int console_assign(int file
, const char *devname
)
632 struct stdio_dev
*dev
;
634 /* Check for valid file */
637 flag
= DEV_FLAGS_INPUT
;
641 flag
= DEV_FLAGS_OUTPUT
;
647 /* Check for valid device name */
649 dev
= search_device(flag
, devname
);
652 return console_setfile(file
, dev
);
657 static void console_update_silent(void)
659 #ifdef CONFIG_SILENT_CONSOLE
660 if (getenv("silent") != NULL
)
661 gd
->flags
|= GD_FLG_SILENT
;
663 gd
->flags
&= ~GD_FLG_SILENT
;
667 int console_announce_r(void)
669 #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
670 char buf
[DISPLAY_OPTIONS_BANNER_LENGTH
];
672 display_options_get_banner(false, buf
, sizeof(buf
));
674 console_puts_noserial(stdout
, buf
);
680 /* Called before relocation - use serial functions */
681 int console_init_f(void)
683 gd
->have_console
= 1;
685 console_update_silent();
687 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL
);
692 void stdio_print_current_devices(void)
694 /* Print information */
696 if (stdio_devices
[stdin
] == NULL
) {
697 puts("No input devices available!\n");
699 printf ("%s\n", stdio_devices
[stdin
]->name
);
703 if (stdio_devices
[stdout
] == NULL
) {
704 puts("No output devices available!\n");
706 printf ("%s\n", stdio_devices
[stdout
]->name
);
710 if (stdio_devices
[stderr
] == NULL
) {
711 puts("No error devices available!\n");
713 printf ("%s\n", stdio_devices
[stderr
]->name
);
717 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
718 /* Called after the relocation - use desired console functions */
719 int console_init_r(void)
721 char *stdinname
, *stdoutname
, *stderrname
;
722 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
, *errdev
= NULL
;
723 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
725 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
726 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
730 /* set default handlers at first */
731 gd
->jt
->getc
= serial_getc
;
732 gd
->jt
->tstc
= serial_tstc
;
733 gd
->jt
->putc
= serial_putc
;
734 gd
->jt
->puts
= serial_puts
;
735 gd
->jt
->printf
= serial_printf
;
737 /* stdin stdout and stderr are in environment */
739 stdinname
= getenv("stdin");
740 stdoutname
= getenv("stdout");
741 stderrname
= getenv("stderr");
743 if (OVERWRITE_CONSOLE
== 0) { /* if not overwritten by config switch */
744 inputdev
= search_device(DEV_FLAGS_INPUT
, stdinname
);
745 outputdev
= search_device(DEV_FLAGS_OUTPUT
, stdoutname
);
746 errdev
= search_device(DEV_FLAGS_OUTPUT
, stderrname
);
747 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
748 iomux_err
= iomux_doenv(stdin
, stdinname
);
749 iomux_err
+= iomux_doenv(stdout
, stdoutname
);
750 iomux_err
+= iomux_doenv(stderr
, stderrname
);
752 /* Successful, so skip all the code below. */
756 /* if the devices are overwritten or not found, use default device */
757 if (inputdev
== NULL
) {
758 inputdev
= search_device(DEV_FLAGS_INPUT
, "serial");
760 if (outputdev
== NULL
) {
761 outputdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
763 if (errdev
== NULL
) {
764 errdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
766 /* Initializes output console first */
767 if (outputdev
!= NULL
) {
768 /* need to set a console if not done above. */
769 console_doenv(stdout
, outputdev
);
771 if (errdev
!= NULL
) {
772 /* need to set a console if not done above. */
773 console_doenv(stderr
, errdev
);
775 if (inputdev
!= NULL
) {
776 /* need to set a console if not done above. */
777 console_doenv(stdin
, inputdev
);
780 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
784 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
785 stdio_print_current_devices();
786 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
787 #ifdef CONFIG_VIDCONSOLE_AS_LCD
788 if (strstr(stdoutname
, "lcd"))
789 printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
792 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
793 /* set the environment variables (will overwrite previous env settings) */
794 for (i
= 0; i
< 3; i
++) {
795 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
797 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
799 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
802 /* If nothing usable installed, use only the initial console */
803 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
806 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
);
810 #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
812 /* Called after the relocation - use desired console functions */
813 int console_init_r(void)
815 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
;
817 struct list_head
*list
= stdio_get_list();
818 struct list_head
*pos
;
819 struct stdio_dev
*dev
;
821 console_update_silent();
823 #ifdef CONFIG_SPLASH_SCREEN
825 * suppress all output if splash screen is enabled and we have
826 * a bmp to display. We redirect the output from frame buffer
827 * console to serial console in this case or suppress it if
828 * "silent" mode was requested.
830 if (getenv("splashimage") != NULL
) {
831 if (!(gd
->flags
& GD_FLG_SILENT
))
832 outputdev
= search_device (DEV_FLAGS_OUTPUT
, "serial");
836 /* Scan devices looking for input and output devices */
837 list_for_each(pos
, list
) {
838 dev
= list_entry(pos
, struct stdio_dev
, list
);
840 if ((dev
->flags
& DEV_FLAGS_INPUT
) && (inputdev
== NULL
)) {
843 if ((dev
->flags
& DEV_FLAGS_OUTPUT
) && (outputdev
== NULL
)) {
846 if(inputdev
&& outputdev
)
850 /* Initializes output console first */
851 if (outputdev
!= NULL
) {
852 console_setfile(stdout
, outputdev
);
853 console_setfile(stderr
, outputdev
);
854 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
855 console_devices
[stdout
][0] = outputdev
;
856 console_devices
[stderr
][0] = outputdev
;
860 /* Initializes input console */
861 if (inputdev
!= NULL
) {
862 console_setfile(stdin
, inputdev
);
863 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
864 console_devices
[stdin
][0] = inputdev
;
868 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
869 stdio_print_current_devices();
870 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
872 /* Setting environment variables */
873 for (i
= 0; i
< 3; i
++) {
874 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
877 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
880 /* If nothing usable installed, use only the initial console */
881 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
884 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL
);
888 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */