]>
git.ipfire.org Git - thirdparty/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+
12 #include <stdio_dev.h>
14 #include <environment.h>
16 DECLARE_GLOBAL_DATA_PTR
;
18 static int on_console(const char *name
, const char *value
, enum env_op op
,
23 /* Check for console redirection */
24 if (strcmp(name
, "stdin") == 0)
26 else if (strcmp(name
, "stdout") == 0)
28 else if (strcmp(name
, "stderr") == 0)
31 /* if not actually setting a console variable, we don't care */
32 if (console
== -1 || (gd
->flags
& GD_FLG_DEVINIT
) == 0)
37 case env_op_overwrite
:
39 #ifdef CONFIG_CONSOLE_MUX
40 if (iomux_doenv(console
, value
))
43 /* Try assigning specified device */
44 if (console_assign(console
, value
) < 0)
46 #endif /* CONFIG_CONSOLE_MUX */
50 if ((flags
& H_FORCE
) == 0)
51 printf("Can't delete \"%s\"\n", name
);
58 U_BOOT_ENV_CALLBACK(console
, on_console
);
60 #ifdef CONFIG_SILENT_CONSOLE
61 static int on_silent(const char *name
, const char *value
, enum env_op op
,
64 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
65 if (flags
& H_INTERACTIVE
)
68 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
69 if ((flags
& H_INTERACTIVE
) == 0)
74 gd
->flags
|= GD_FLG_SILENT
;
76 gd
->flags
&= ~GD_FLG_SILENT
;
80 U_BOOT_ENV_CALLBACK(silent
, on_silent
);
83 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
85 * if overwrite_console returns 1, the stdin, stderr and stdout
86 * are switched to the serial port, else the settings in the
87 * environment are used
89 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
90 extern int overwrite_console(void);
91 #define OVERWRITE_CONSOLE overwrite_console()
93 #define OVERWRITE_CONSOLE 0
94 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
96 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
98 static int console_setfile(int file
, struct stdio_dev
* dev
)
109 /* Start new device */
111 error
= dev
->start();
112 /* If it's not started dont use it */
117 /* Assign the new device (leaving the existing one started) */
118 stdio_devices
[file
] = dev
;
121 * Update monitor functions
122 * (to use the console stuff by other applications)
126 gd
->jt
[XF_getc
] = dev
->getc
;
127 gd
->jt
[XF_tstc
] = dev
->tstc
;
130 gd
->jt
[XF_putc
] = dev
->putc
;
131 gd
->jt
[XF_puts
] = dev
->puts
;
132 gd
->jt
[XF_printf
] = printf
;
137 default: /* Invalid file ID */
143 #if defined(CONFIG_CONSOLE_MUX)
144 /** Console I/O multiplexing *******************************************/
146 static struct stdio_dev
*tstcdev
;
147 struct stdio_dev
**console_devices
[MAX_FILES
];
148 int cd_count
[MAX_FILES
];
151 * This depends on tstc() always being called before getc().
152 * This is guaranteed to be true because this routine is called
153 * only from fgetc() which assures it.
154 * No attempt is made to demultiplex multiple input sources.
156 static int console_getc(int file
)
160 /* This is never called with testcdev == NULL */
161 ret
= tstcdev
->getc();
166 static int console_tstc(int file
)
169 struct stdio_dev
*dev
;
172 for (i
= 0; i
< cd_count
[file
]; i
++) {
173 dev
= console_devices
[file
][i
];
174 if (dev
->tstc
!= NULL
) {
188 static void console_putc(int file
, const char c
)
191 struct stdio_dev
*dev
;
193 for (i
= 0; i
< cd_count
[file
]; i
++) {
194 dev
= console_devices
[file
][i
];
195 if (dev
->putc
!= NULL
)
200 static void console_puts(int file
, const char *s
)
203 struct stdio_dev
*dev
;
205 for (i
= 0; i
< cd_count
[file
]; i
++) {
206 dev
= console_devices
[file
][i
];
207 if (dev
->puts
!= NULL
)
212 static inline void console_printdevs(int file
)
214 iomux_printdevs(file
);
217 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
219 iomux_doenv(file
, dev
->name
);
222 static inline int console_getc(int file
)
224 return stdio_devices
[file
]->getc();
227 static inline int console_tstc(int file
)
229 return stdio_devices
[file
]->tstc();
232 static inline void console_putc(int file
, const char c
)
234 stdio_devices
[file
]->putc(c
);
237 static inline void console_puts(int file
, const char *s
)
239 stdio_devices
[file
]->puts(s
);
242 static inline void console_printdevs(int file
)
244 printf("%s\n", stdio_devices
[file
]->name
);
247 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
249 console_setfile(file
, dev
);
251 #endif /* defined(CONFIG_CONSOLE_MUX) */
253 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
255 int serial_printf(const char *fmt
, ...)
259 char printbuffer
[CONFIG_SYS_PBSIZE
];
263 /* For this to work, printbuffer must be larger than
264 * anything we ever want to print.
266 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
269 serial_puts(printbuffer
);
275 if (file
< MAX_FILES
) {
276 #if defined(CONFIG_CONSOLE_MUX)
278 * Effectively poll for input wherever it may be available.
282 * Upper layer may have already called tstc() so
283 * check for that first.
286 return console_getc(file
);
288 #ifdef CONFIG_WATCHDOG
290 * If the watchdog must be rate-limited then it should
291 * already be handled in board-specific code.
297 return console_getc(file
);
306 if (file
< MAX_FILES
)
307 return console_tstc(file
);
312 void fputc(int file
, const char c
)
314 if (file
< MAX_FILES
)
315 console_putc(file
, c
);
318 void fputs(int file
, const char *s
)
320 if (file
< MAX_FILES
)
321 console_puts(file
, s
);
324 int fprintf(int file
, const char *fmt
, ...)
328 char printbuffer
[CONFIG_SYS_PBSIZE
];
332 /* For this to work, printbuffer must be larger than
333 * anything we ever want to print.
335 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
338 /* Send to desired file */
339 fputs(file
, printbuffer
);
343 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
347 #ifdef CONFIG_DISABLE_CONSOLE
348 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
352 if (!gd
->have_console
)
355 if (gd
->flags
& GD_FLG_DEVINIT
) {
356 /* Get from the standard input */
360 /* Send directly to the handler */
361 return serial_getc();
366 #ifdef CONFIG_DISABLE_CONSOLE
367 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
371 if (!gd
->have_console
)
374 if (gd
->flags
& GD_FLG_DEVINIT
) {
375 /* Test the standard input */
379 /* Send directly to the handler */
380 return serial_tstc();
383 #ifdef CONFIG_PRE_CONSOLE_BUFFER
384 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
386 static void pre_console_putc(const char c
)
388 char *buffer
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
390 buffer
[CIRC_BUF_IDX(gd
->precon_buf_idx
++)] = c
;
393 static void pre_console_puts(const char *s
)
396 pre_console_putc(*s
++);
399 static void print_pre_console_buffer(void)
402 char *buffer
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
404 if (gd
->precon_buf_idx
> CONFIG_PRE_CON_BUF_SZ
)
405 i
= gd
->precon_buf_idx
- CONFIG_PRE_CON_BUF_SZ
;
407 while (i
< gd
->precon_buf_idx
)
408 putc(buffer
[CIRC_BUF_IDX(i
++)]);
411 static inline void pre_console_putc(const char c
) {}
412 static inline void pre_console_puts(const char *s
) {}
413 static inline void print_pre_console_buffer(void) {}
416 void putc(const char c
)
418 #ifdef CONFIG_SILENT_CONSOLE
419 if (gd
->flags
& GD_FLG_SILENT
)
423 #ifdef CONFIG_DISABLE_CONSOLE
424 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
428 if (!gd
->have_console
)
429 return pre_console_putc(c
);
431 if (gd
->flags
& GD_FLG_DEVINIT
) {
432 /* Send to the standard output */
435 /* Send directly to the handler */
440 void puts(const char *s
)
442 #ifdef CONFIG_SILENT_CONSOLE
443 if (gd
->flags
& GD_FLG_SILENT
)
447 #ifdef CONFIG_DISABLE_CONSOLE
448 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
452 if (!gd
->have_console
)
453 return pre_console_puts(s
);
455 if (gd
->flags
& GD_FLG_DEVINIT
) {
456 /* Send to the standard output */
459 /* Send directly to the handler */
464 int printf(const char *fmt
, ...)
468 char printbuffer
[CONFIG_SYS_PBSIZE
];
470 #ifndef CONFIG_PRE_CONSOLE_BUFFER
471 if (!gd
->have_console
)
477 /* For this to work, printbuffer must be larger than
478 * anything we ever want to print.
480 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
483 /* Print the string */
488 int vprintf(const char *fmt
, va_list args
)
491 char printbuffer
[CONFIG_SYS_PBSIZE
];
493 #ifndef CONFIG_PRE_CONSOLE_BUFFER
494 if (!gd
->have_console
)
498 /* For this to work, printbuffer must be larger than
499 * anything we ever want to print.
501 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
503 /* Print the string */
508 /* test if ctrl-c was pressed */
509 static int ctrlc_disabled
= 0; /* see disable_ctrl() */
510 static int ctrlc_was_pressed
= 0;
513 if (!ctrlc_disabled
&& gd
->have_console
) {
516 case 0x03: /* ^C - Control C */
517 ctrlc_was_pressed
= 1;
527 /* pass 1 to disable ctrlc() checking, 0 to enable.
528 * returns previous state
530 int disable_ctrlc(int disable
)
532 int prev
= ctrlc_disabled
; /* save previous state */
534 ctrlc_disabled
= disable
;
540 return ctrlc_was_pressed
;
543 void clear_ctrlc(void)
545 ctrlc_was_pressed
= 0;
548 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
550 char *cursor
= screen
;
552 inline void dbg(const char *fmt
, ...)
556 char printbuffer
[CONFIG_SYS_PBSIZE
];
559 memset(screen
, 0, sizeof(screen
));
565 /* For this to work, printbuffer must be larger than
566 * anything we ever want to print.
568 i
= vsnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
571 if ((screen
+ sizeof(screen
) - 1 - cursor
)
572 < strlen(printbuffer
) + 1) {
573 memset(screen
, 0, sizeof(screen
));
576 sprintf(cursor
, printbuffer
);
577 cursor
+= strlen(printbuffer
);
581 inline void dbg(const char *fmt
, ...)
586 /** U-Boot INIT FUNCTIONS *************************************************/
588 struct stdio_dev
*search_device(int flags
, const char *name
)
590 struct stdio_dev
*dev
;
592 dev
= stdio_get_by_name(name
);
594 if (dev
&& (dev
->flags
& flags
))
600 int console_assign(int file
, const char *devname
)
603 struct stdio_dev
*dev
;
605 /* Check for valid file */
608 flag
= DEV_FLAGS_INPUT
;
612 flag
= DEV_FLAGS_OUTPUT
;
618 /* Check for valid device name */
620 dev
= search_device(flag
, devname
);
623 return console_setfile(file
, dev
);
628 /* Called before relocation - use serial functions */
629 int console_init_f(void)
631 gd
->have_console
= 1;
633 #ifdef CONFIG_SILENT_CONSOLE
634 if (getenv("silent") != NULL
)
635 gd
->flags
|= GD_FLG_SILENT
;
638 print_pre_console_buffer();
643 void stdio_print_current_devices(void)
645 /* Print information */
647 if (stdio_devices
[stdin
] == NULL
) {
648 puts("No input devices available!\n");
650 printf ("%s\n", stdio_devices
[stdin
]->name
);
654 if (stdio_devices
[stdout
] == NULL
) {
655 puts("No output devices available!\n");
657 printf ("%s\n", stdio_devices
[stdout
]->name
);
661 if (stdio_devices
[stderr
] == NULL
) {
662 puts("No error devices available!\n");
664 printf ("%s\n", stdio_devices
[stderr
]->name
);
668 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
669 /* Called after the relocation - use desired console functions */
670 int console_init_r(void)
672 char *stdinname
, *stdoutname
, *stderrname
;
673 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
, *errdev
= NULL
;
674 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
676 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
677 #ifdef CONFIG_CONSOLE_MUX
681 /* set default handlers at first */
682 gd
->jt
[XF_getc
] = serial_getc
;
683 gd
->jt
[XF_tstc
] = serial_tstc
;
684 gd
->jt
[XF_putc
] = serial_putc
;
685 gd
->jt
[XF_puts
] = serial_puts
;
686 gd
->jt
[XF_printf
] = serial_printf
;
688 /* stdin stdout and stderr are in environment */
690 stdinname
= getenv("stdin");
691 stdoutname
= getenv("stdout");
692 stderrname
= getenv("stderr");
694 if (OVERWRITE_CONSOLE
== 0) { /* if not overwritten by config switch */
695 inputdev
= search_device(DEV_FLAGS_INPUT
, stdinname
);
696 outputdev
= search_device(DEV_FLAGS_OUTPUT
, stdoutname
);
697 errdev
= search_device(DEV_FLAGS_OUTPUT
, stderrname
);
698 #ifdef CONFIG_CONSOLE_MUX
699 iomux_err
= iomux_doenv(stdin
, stdinname
);
700 iomux_err
+= iomux_doenv(stdout
, stdoutname
);
701 iomux_err
+= iomux_doenv(stderr
, stderrname
);
703 /* Successful, so skip all the code below. */
707 /* if the devices are overwritten or not found, use default device */
708 if (inputdev
== NULL
) {
709 inputdev
= search_device(DEV_FLAGS_INPUT
, "serial");
711 if (outputdev
== NULL
) {
712 outputdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
714 if (errdev
== NULL
) {
715 errdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
717 /* Initializes output console first */
718 if (outputdev
!= NULL
) {
719 /* need to set a console if not done above. */
720 console_doenv(stdout
, outputdev
);
722 if (errdev
!= NULL
) {
723 /* need to set a console if not done above. */
724 console_doenv(stderr
, errdev
);
726 if (inputdev
!= NULL
) {
727 /* need to set a console if not done above. */
728 console_doenv(stdin
, inputdev
);
731 #ifdef CONFIG_CONSOLE_MUX
735 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
736 stdio_print_current_devices();
737 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
739 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
740 /* set the environment variables (will overwrite previous env settings) */
741 for (i
= 0; i
< 3; i
++) {
742 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
744 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
746 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
749 /* If nothing usable installed, use only the initial console */
750 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
756 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
758 /* Called after the relocation - use desired console functions */
759 int console_init_r(void)
761 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
;
763 struct list_head
*list
= stdio_get_list();
764 struct list_head
*pos
;
765 struct stdio_dev
*dev
;
767 #ifdef CONFIG_SPLASH_SCREEN
769 * suppress all output if splash screen is enabled and we have
770 * a bmp to display. We redirect the output from frame buffer
771 * console to serial console in this case or suppress it if
772 * "silent" mode was requested.
774 if (getenv("splashimage") != NULL
) {
775 if (!(gd
->flags
& GD_FLG_SILENT
))
776 outputdev
= search_device (DEV_FLAGS_OUTPUT
, "serial");
780 /* Scan devices looking for input and output devices */
781 list_for_each(pos
, list
) {
782 dev
= list_entry(pos
, struct stdio_dev
, list
);
784 if ((dev
->flags
& DEV_FLAGS_INPUT
) && (inputdev
== NULL
)) {
787 if ((dev
->flags
& DEV_FLAGS_OUTPUT
) && (outputdev
== NULL
)) {
790 if(inputdev
&& outputdev
)
794 /* Initializes output console first */
795 if (outputdev
!= NULL
) {
796 console_setfile(stdout
, outputdev
);
797 console_setfile(stderr
, outputdev
);
798 #ifdef CONFIG_CONSOLE_MUX
799 console_devices
[stdout
][0] = outputdev
;
800 console_devices
[stderr
][0] = outputdev
;
804 /* Initializes input console */
805 if (inputdev
!= NULL
) {
806 console_setfile(stdin
, inputdev
);
807 #ifdef CONFIG_CONSOLE_MUX
808 console_devices
[stdin
][0] = inputdev
;
812 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
813 stdio_print_current_devices();
814 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
816 /* Setting environment variables */
817 for (i
= 0; i
< 3; i
++) {
818 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
821 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
824 /* If nothing usable installed, use only the initial console */
825 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
832 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */