]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/console.c
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)
128 gd
->jt
[XF_getc
] = getc
;
129 gd
->jt
[XF_tstc
] = tstc
;
132 gd
->jt
[XF_putc
] = putc
;
133 gd
->jt
[XF_puts
] = puts
;
134 gd
->jt
[XF_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 static void console_puts(int file
, const char *s
)
205 struct stdio_dev
*dev
;
207 for (i
= 0; i
< cd_count
[file
]; i
++) {
208 dev
= console_devices
[file
][i
];
209 if (dev
->puts
!= NULL
)
214 static inline void console_printdevs(int file
)
216 iomux_printdevs(file
);
219 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
221 iomux_doenv(file
, dev
->name
);
224 static inline int console_getc(int file
)
226 return stdio_devices
[file
]->getc(stdio_devices
[file
]);
229 static inline int console_tstc(int file
)
231 return stdio_devices
[file
]->tstc(stdio_devices
[file
]);
234 static inline void console_putc(int file
, const char c
)
236 stdio_devices
[file
]->putc(stdio_devices
[file
], c
);
239 static inline void console_puts(int file
, const char *s
)
241 stdio_devices
[file
]->puts(stdio_devices
[file
], s
);
244 static inline void console_printdevs(int file
)
246 printf("%s\n", stdio_devices
[file
]->name
);
249 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
251 console_setfile(file
, dev
);
253 #endif /* defined(CONFIG_CONSOLE_MUX) */
255 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
257 int serial_printf(const char *fmt
, ...)
261 char printbuffer
[CONFIG_SYS_PBSIZE
];
265 /* For this to work, printbuffer must be larger than
266 * anything we ever want to print.
268 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
271 serial_puts(printbuffer
);
277 if (file
< MAX_FILES
) {
278 #if defined(CONFIG_CONSOLE_MUX)
280 * Effectively poll for input wherever it may be available.
284 * Upper layer may have already called tstc() so
285 * check for that first.
288 return console_getc(file
);
290 #ifdef CONFIG_WATCHDOG
292 * If the watchdog must be rate-limited then it should
293 * already be handled in board-specific code.
299 return console_getc(file
);
308 if (file
< MAX_FILES
)
309 return console_tstc(file
);
314 void fputc(int file
, const char c
)
316 if (file
< MAX_FILES
)
317 console_putc(file
, c
);
320 void fputs(int file
, const char *s
)
322 if (file
< MAX_FILES
)
323 console_puts(file
, s
);
326 int fprintf(int file
, const char *fmt
, ...)
330 char printbuffer
[CONFIG_SYS_PBSIZE
];
334 /* For this to work, printbuffer must be larger than
335 * anything we ever want to print.
337 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
340 /* Send to desired file */
341 fputs(file
, printbuffer
);
345 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
349 #ifdef CONFIG_DISABLE_CONSOLE
350 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
354 if (!gd
->have_console
)
357 if (gd
->flags
& GD_FLG_DEVINIT
) {
358 /* Get from the standard input */
362 /* Send directly to the handler */
363 return serial_getc();
368 #ifdef CONFIG_DISABLE_CONSOLE
369 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
373 if (!gd
->have_console
)
376 if (gd
->flags
& GD_FLG_DEVINIT
) {
377 /* Test the standard input */
381 /* Send directly to the handler */
382 return serial_tstc();
385 #ifdef CONFIG_PRE_CONSOLE_BUFFER
386 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
388 static void pre_console_putc(const char c
)
390 char *buffer
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
392 buffer
[CIRC_BUF_IDX(gd
->precon_buf_idx
++)] = c
;
395 static void pre_console_puts(const char *s
)
398 pre_console_putc(*s
++);
401 static void print_pre_console_buffer(void)
404 char *buffer
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
406 if (gd
->precon_buf_idx
> CONFIG_PRE_CON_BUF_SZ
)
407 i
= gd
->precon_buf_idx
- CONFIG_PRE_CON_BUF_SZ
;
409 while (i
< gd
->precon_buf_idx
)
410 putc(buffer
[CIRC_BUF_IDX(i
++)]);
413 static inline void pre_console_putc(const char c
) {}
414 static inline void pre_console_puts(const char *s
) {}
415 static inline void print_pre_console_buffer(void) {}
418 void putc(const char c
)
420 #ifdef CONFIG_SANDBOX
421 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
426 #ifdef CONFIG_SILENT_CONSOLE
427 if (gd
->flags
& GD_FLG_SILENT
)
431 #ifdef CONFIG_DISABLE_CONSOLE
432 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
436 if (!gd
->have_console
)
437 return pre_console_putc(c
);
439 if (gd
->flags
& GD_FLG_DEVINIT
) {
440 /* Send to the standard output */
443 /* Send directly to the handler */
448 void puts(const char *s
)
450 #ifdef CONFIG_SANDBOX
451 if (!gd
|| !(gd
->flags
& GD_FLG_SERIAL_READY
)) {
457 #ifdef CONFIG_SILENT_CONSOLE
458 if (gd
->flags
& GD_FLG_SILENT
)
462 #ifdef CONFIG_DISABLE_CONSOLE
463 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
467 if (!gd
->have_console
)
468 return pre_console_puts(s
);
470 if (gd
->flags
& GD_FLG_DEVINIT
) {
471 /* Send to the standard output */
474 /* Send directly to the handler */
479 int printf(const char *fmt
, ...)
483 char printbuffer
[CONFIG_SYS_PBSIZE
];
485 #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_PRE_CONSOLE_BUFFER)
486 if (!gd
->have_console
)
492 /* For this to work, printbuffer must be larger than
493 * anything we ever want to print.
495 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
498 /* Print the string */
503 int vprintf(const char *fmt
, va_list args
)
506 char printbuffer
[CONFIG_SYS_PBSIZE
];
508 #if defined(CONFIG_PRE_CONSOLE_BUFFER) && !defined(CONFIG_SANDBOX)
509 if (!gd
->have_console
)
513 /* For this to work, printbuffer must be larger than
514 * anything we ever want to print.
516 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
518 /* Print the string */
523 /* test if ctrl-c was pressed */
524 static int ctrlc_disabled
= 0; /* see disable_ctrl() */
525 static int ctrlc_was_pressed
= 0;
528 #ifndef CONFIG_SANDBOX
529 if (!ctrlc_disabled
&& gd
->have_console
) {
532 case 0x03: /* ^C - Control C */
533 ctrlc_was_pressed
= 1;
544 /* Reads user's confirmation.
545 Returns 1 if user's input is "y", "Y", "yes" or "YES"
547 int confirm_yesno(void)
556 while (i
< sizeof(str_input
)) {
557 str_input
[i
] = getc();
559 if (str_input
[i
] == '\r')
564 if (strncmp(str_input
, "y\r", 2) == 0 ||
565 strncmp(str_input
, "Y\r", 2) == 0 ||
566 strncmp(str_input
, "yes\r", 4) == 0 ||
567 strncmp(str_input
, "YES\r", 4) == 0)
571 /* pass 1 to disable ctrlc() checking, 0 to enable.
572 * returns previous state
574 int disable_ctrlc(int disable
)
576 int prev
= ctrlc_disabled
; /* save previous state */
578 ctrlc_disabled
= disable
;
584 return ctrlc_was_pressed
;
587 void clear_ctrlc(void)
589 ctrlc_was_pressed
= 0;
592 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
594 char *cursor
= screen
;
596 inline void dbg(const char *fmt
, ...)
600 char printbuffer
[CONFIG_SYS_PBSIZE
];
603 memset(screen
, 0, sizeof(screen
));
609 /* For this to work, printbuffer must be larger than
610 * anything we ever want to print.
612 i
= vsnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
615 if ((screen
+ sizeof(screen
) - 1 - cursor
)
616 < strlen(printbuffer
) + 1) {
617 memset(screen
, 0, sizeof(screen
));
620 sprintf(cursor
, printbuffer
);
621 cursor
+= strlen(printbuffer
);
625 static inline void dbg(const char *fmt
, ...)
630 /** U-Boot INIT FUNCTIONS *************************************************/
632 struct stdio_dev
*search_device(int flags
, const char *name
)
634 struct stdio_dev
*dev
;
636 dev
= stdio_get_by_name(name
);
638 if (dev
&& (dev
->flags
& flags
))
644 int console_assign(int file
, const char *devname
)
647 struct stdio_dev
*dev
;
649 /* Check for valid file */
652 flag
= DEV_FLAGS_INPUT
;
656 flag
= DEV_FLAGS_OUTPUT
;
662 /* Check for valid device name */
664 dev
= search_device(flag
, devname
);
667 return console_setfile(file
, dev
);
672 /* Called before relocation - use serial functions */
673 int console_init_f(void)
675 gd
->have_console
= 1;
677 #ifdef CONFIG_SILENT_CONSOLE
678 if (getenv("silent") != NULL
)
679 gd
->flags
|= GD_FLG_SILENT
;
682 print_pre_console_buffer();
687 void stdio_print_current_devices(void)
689 /* Print information */
691 if (stdio_devices
[stdin
] == NULL
) {
692 puts("No input devices available!\n");
694 printf ("%s\n", stdio_devices
[stdin
]->name
);
698 if (stdio_devices
[stdout
] == NULL
) {
699 puts("No output devices available!\n");
701 printf ("%s\n", stdio_devices
[stdout
]->name
);
705 if (stdio_devices
[stderr
] == NULL
) {
706 puts("No error devices available!\n");
708 printf ("%s\n", stdio_devices
[stderr
]->name
);
712 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
713 /* Called after the relocation - use desired console functions */
714 int console_init_r(void)
716 char *stdinname
, *stdoutname
, *stderrname
;
717 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
, *errdev
= NULL
;
718 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
720 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
721 #ifdef CONFIG_CONSOLE_MUX
725 /* set default handlers at first */
726 gd
->jt
[XF_getc
] = serial_getc
;
727 gd
->jt
[XF_tstc
] = serial_tstc
;
728 gd
->jt
[XF_putc
] = serial_putc
;
729 gd
->jt
[XF_puts
] = serial_puts
;
730 gd
->jt
[XF_printf
] = serial_printf
;
732 /* stdin stdout and stderr are in environment */
734 stdinname
= getenv("stdin");
735 stdoutname
= getenv("stdout");
736 stderrname
= getenv("stderr");
738 if (OVERWRITE_CONSOLE
== 0) { /* if not overwritten by config switch */
739 inputdev
= search_device(DEV_FLAGS_INPUT
, stdinname
);
740 outputdev
= search_device(DEV_FLAGS_OUTPUT
, stdoutname
);
741 errdev
= search_device(DEV_FLAGS_OUTPUT
, stderrname
);
742 #ifdef CONFIG_CONSOLE_MUX
743 iomux_err
= iomux_doenv(stdin
, stdinname
);
744 iomux_err
+= iomux_doenv(stdout
, stdoutname
);
745 iomux_err
+= iomux_doenv(stderr
, stderrname
);
747 /* Successful, so skip all the code below. */
751 /* if the devices are overwritten or not found, use default device */
752 if (inputdev
== NULL
) {
753 inputdev
= search_device(DEV_FLAGS_INPUT
, "serial");
755 if (outputdev
== NULL
) {
756 outputdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
758 if (errdev
== NULL
) {
759 errdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
761 /* Initializes output console first */
762 if (outputdev
!= NULL
) {
763 /* need to set a console if not done above. */
764 console_doenv(stdout
, outputdev
);
766 if (errdev
!= NULL
) {
767 /* need to set a console if not done above. */
768 console_doenv(stderr
, errdev
);
770 if (inputdev
!= NULL
) {
771 /* need to set a console if not done above. */
772 console_doenv(stdin
, inputdev
);
775 #ifdef CONFIG_CONSOLE_MUX
779 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
780 stdio_print_current_devices();
781 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
783 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
784 /* set the environment variables (will overwrite previous env settings) */
785 for (i
= 0; i
< 3; i
++) {
786 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
788 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
790 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
793 /* If nothing usable installed, use only the initial console */
794 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
800 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
802 /* Called after the relocation - use desired console functions */
803 int console_init_r(void)
805 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
;
807 struct list_head
*list
= stdio_get_list();
808 struct list_head
*pos
;
809 struct stdio_dev
*dev
;
811 #ifdef CONFIG_SPLASH_SCREEN
813 * suppress all output if splash screen is enabled and we have
814 * a bmp to display. We redirect the output from frame buffer
815 * console to serial console in this case or suppress it if
816 * "silent" mode was requested.
818 if (getenv("splashimage") != NULL
) {
819 if (!(gd
->flags
& GD_FLG_SILENT
))
820 outputdev
= search_device (DEV_FLAGS_OUTPUT
, "serial");
824 /* Scan devices looking for input and output devices */
825 list_for_each(pos
, list
) {
826 dev
= list_entry(pos
, struct stdio_dev
, list
);
828 if ((dev
->flags
& DEV_FLAGS_INPUT
) && (inputdev
== NULL
)) {
831 if ((dev
->flags
& DEV_FLAGS_OUTPUT
) && (outputdev
== NULL
)) {
834 if(inputdev
&& outputdev
)
838 /* Initializes output console first */
839 if (outputdev
!= NULL
) {
840 console_setfile(stdout
, outputdev
);
841 console_setfile(stderr
, outputdev
);
842 #ifdef CONFIG_CONSOLE_MUX
843 console_devices
[stdout
][0] = outputdev
;
844 console_devices
[stderr
][0] = outputdev
;
848 /* Initializes input console */
849 if (inputdev
!= NULL
) {
850 console_setfile(stdin
, inputdev
);
851 #ifdef CONFIG_CONSOLE_MUX
852 console_devices
[stdin
][0] = inputdev
;
856 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
857 stdio_print_current_devices();
858 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
860 /* Setting environment variables */
861 for (i
= 0; i
< 3; i
++) {
862 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
865 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
868 /* If nothing usable installed, use only the initial console */
869 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
876 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */