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_doenv(int file, struct stdio_dev *dev)
232 iomux_doenv(file, dev->name);
235 static inline int console_getc(int file)
237 return stdio_devices[file]->getc(stdio_devices[file]);
240 static inline int console_tstc(int file)
242 return stdio_devices[file]->tstc(stdio_devices[file]);
245 static inline void console_putc(int file, const char c)
247 stdio_devices[file]->putc(stdio_devices[file], c);
250 #ifdef CONFIG_PRE_CONSOLE_BUFFER
251 static inline void console_puts_noserial(int file, const char *s)
253 if (strcmp(stdio_devices[file]->name, "serial") != 0)
254 stdio_devices[file]->puts(stdio_devices[file], s);
258 static inline void console_puts(int file, const char *s)
260 stdio_devices[file]->puts(stdio_devices[file], s);
263 static inline void console_doenv(int file, struct stdio_dev *dev)
265 console_setfile(file, dev);
267 #endif /* defined(CONFIG_CONSOLE_MUX) */
269 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
271 int serial_printf(const char *fmt, ...)
275 char printbuffer[CONFIG_SYS_PBSIZE];
279 /* For this to work, printbuffer must be larger than
280 * anything we ever want to print.
282 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
285 serial_puts(printbuffer);
291 if (file < MAX_FILES) {
292 #if defined(CONFIG_CONSOLE_MUX)
294 * Effectively poll for input wherever it may be available.
298 * Upper layer may have already called tstc() so
299 * check for that first.
302 return console_getc(file);
304 #ifdef CONFIG_WATCHDOG
306 * If the watchdog must be rate-limited then it should
307 * already be handled in board-specific code.
313 return console_getc(file);
322 if (file < MAX_FILES)
323 return console_tstc(file);
328 void fputc(int file, const char c)
330 if (file < MAX_FILES)
331 console_putc(file, c);
334 void fputs(int file, const char *s)
336 if (file < MAX_FILES)
337 console_puts(file, s);
340 int fprintf(int file, const char *fmt, ...)
344 char printbuffer[CONFIG_SYS_PBSIZE];
348 /* For this to work, printbuffer must be larger than
349 * anything we ever want to print.
351 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
354 /* Send to desired file */
355 fputs(file, printbuffer);
359 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
363 #ifdef CONFIG_DISABLE_CONSOLE
364 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
368 if (!gd->have_console)
371 #ifdef CONFIG_CONSOLE_RECORD
372 if (gd->console_in.start) {
375 ch = membuff_getbyte(&gd->console_in);
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)
398 #ifdef CONFIG_CONSOLE_RECORD
399 if (gd->console_in.start) {
400 if (membuff_peekbyte(&gd->console_in) != -1)
404 if (gd->flags & GD_FLG_DEVINIT) {
405 /* Test the standard input */
409 /* Send directly to the handler */
410 return serial_tstc();
413 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
414 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
416 #ifdef CONFIG_PRE_CONSOLE_BUFFER
417 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
419 static void pre_console_putc(const char c)
421 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
423 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
426 static void pre_console_puts(const char *s)
429 pre_console_putc(*s++);
432 static void print_pre_console_buffer(int flushpoint)
434 unsigned long in = 0, out = 0;
435 char *buf_in = (char *)CONFIG_PRE_CON_BUF_ADDR;
436 char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
438 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
439 in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
441 while (in < gd->precon_buf_idx)
442 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
446 switch (flushpoint) {
447 case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
450 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
451 console_puts_noserial(stdout, buf_out);
456 static inline void pre_console_putc(const char c) {}
457 static inline void pre_console_puts(const char *s) {}
458 static inline void print_pre_console_buffer(int flushpoint) {}
461 void putc(const char c)
463 #ifdef CONFIG_SANDBOX
464 /* sandbox can send characters to stdout before it has a console */
465 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
470 #ifdef CONFIG_DEBUG_UART
471 /* if we don't have a console yet, use the debug UART */
472 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
477 #ifdef CONFIG_CONSOLE_RECORD
478 if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
479 membuff_putbyte(&gd->console_out, c);
481 #ifdef CONFIG_SILENT_CONSOLE
482 if (gd->flags & GD_FLG_SILENT)
486 #ifdef CONFIG_DISABLE_CONSOLE
487 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
491 if (!gd->have_console)
492 return pre_console_putc(c);
494 if (gd->flags & GD_FLG_DEVINIT) {
495 /* Send to the standard output */
498 /* Send directly to the handler */
504 void puts(const char *s)
506 #ifdef CONFIG_SANDBOX
507 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
512 #ifdef CONFIG_DEBUG_UART
513 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
522 #ifdef CONFIG_CONSOLE_RECORD
523 if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
524 membuff_put(&gd->console_out, s, strlen(s));
526 #ifdef CONFIG_SILENT_CONSOLE
527 if (gd->flags & GD_FLG_SILENT)
531 #ifdef CONFIG_DISABLE_CONSOLE
532 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
536 if (!gd->have_console)
537 return pre_console_puts(s);
539 if (gd->flags & GD_FLG_DEVINIT) {
540 /* Send to the standard output */
543 /* Send directly to the handler */
549 #ifdef CONFIG_CONSOLE_RECORD
550 int console_record_init(void)
554 ret = membuff_new(&gd->console_out, CONFIG_CONSOLE_RECORD_OUT_SIZE);
557 ret = membuff_new(&gd->console_in, CONFIG_CONSOLE_RECORD_IN_SIZE);
562 void console_record_reset(void)
564 membuff_purge(&gd->console_out);
565 membuff_purge(&gd->console_in);
568 void console_record_reset_enable(void)
570 console_record_reset();
571 gd->flags |= GD_FLG_RECORD;
575 /* test if ctrl-c was pressed */
576 static int ctrlc_disabled = 0; /* see disable_ctrl() */
577 static int ctrlc_was_pressed = 0;
580 #ifndef CONFIG_SANDBOX
581 if (!ctrlc_disabled && gd->have_console) {
584 case 0x03: /* ^C - Control C */
585 ctrlc_was_pressed = 1;
596 /* Reads user's confirmation.
597 Returns 1 if user's input is "y", "Y", "yes" or "YES"
599 int confirm_yesno(void)
608 while (i < sizeof(str_input)) {
609 str_input[i] = getc();
611 if (str_input[i] == '\r')
616 if (strncmp(str_input, "y\r", 2) == 0 ||
617 strncmp(str_input, "Y\r", 2) == 0 ||
618 strncmp(str_input, "yes\r", 4) == 0 ||
619 strncmp(str_input, "YES\r", 4) == 0)
623 /* pass 1 to disable ctrlc() checking, 0 to enable.
624 * returns previous state
626 int disable_ctrlc(int disable)
628 int prev = ctrlc_disabled; /* save previous state */
630 ctrlc_disabled = disable;
636 return ctrlc_was_pressed;
639 void clear_ctrlc(void)
641 ctrlc_was_pressed = 0;
644 /** U-Boot INIT FUNCTIONS *************************************************/
646 struct stdio_dev *search_device(int flags, const char *name)
648 struct stdio_dev *dev;
650 dev = stdio_get_by_name(name);
651 #ifdef CONFIG_VIDCONSOLE_AS_LCD
652 if (!dev && !strcmp(name, "lcd"))
653 dev = stdio_get_by_name("vidconsole");
656 if (dev && (dev->flags & flags))
662 int console_assign(int file, const char *devname)
665 struct stdio_dev *dev;
667 /* Check for valid file */
670 flag = DEV_FLAGS_INPUT;
674 flag = DEV_FLAGS_OUTPUT;
680 /* Check for valid device name */
682 dev = search_device(flag, devname);
685 return console_setfile(file, dev);
690 /* Called before relocation - use serial functions */
691 int console_init_f(void)
693 gd->have_console = 1;
695 #ifdef CONFIG_SILENT_CONSOLE
696 if (getenv("silent") != NULL)
697 gd->flags |= GD_FLG_SILENT;
700 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
705 void stdio_print_current_devices(void)
707 /* Print information */
709 if (stdio_devices[stdin] == NULL) {
710 puts("No input devices available!\n");
712 printf ("%s\n", stdio_devices[stdin]->name);
716 if (stdio_devices[stdout] == NULL) {
717 puts("No output devices available!\n");
719 printf ("%s\n", stdio_devices[stdout]->name);
723 if (stdio_devices[stderr] == NULL) {
724 puts("No error devices available!\n");
726 printf ("%s\n", stdio_devices[stderr]->name);
730 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
731 /* Called after the relocation - use desired console functions */
732 int console_init_r(void)
734 char *stdinname, *stdoutname, *stderrname;
735 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
736 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
738 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
739 #ifdef CONFIG_CONSOLE_MUX
743 /* set default handlers at first */
744 gd->jt->getc = serial_getc;
745 gd->jt->tstc = serial_tstc;
746 gd->jt->putc = serial_putc;
747 gd->jt->puts = serial_puts;
748 gd->jt->printf = serial_printf;
750 /* stdin stdout and stderr are in environment */
752 stdinname = getenv("stdin");
753 stdoutname = getenv("stdout");
754 stderrname = getenv("stderr");
756 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
757 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
758 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
759 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
760 #ifdef CONFIG_CONSOLE_MUX
761 iomux_err = iomux_doenv(stdin, stdinname);
762 iomux_err += iomux_doenv(stdout, stdoutname);
763 iomux_err += iomux_doenv(stderr, stderrname);
765 /* Successful, so skip all the code below. */
769 /* if the devices are overwritten or not found, use default device */
770 if (inputdev == NULL) {
771 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
773 if (outputdev == NULL) {
774 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
776 if (errdev == NULL) {
777 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
779 /* Initializes output console first */
780 if (outputdev != NULL) {
781 /* need to set a console if not done above. */
782 console_doenv(stdout, outputdev);
784 if (errdev != NULL) {
785 /* need to set a console if not done above. */
786 console_doenv(stderr, errdev);
788 if (inputdev != NULL) {
789 /* need to set a console if not done above. */
790 console_doenv(stdin, inputdev);
793 #ifdef CONFIG_CONSOLE_MUX
797 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
798 stdio_print_current_devices();
799 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
800 #ifdef CONFIG_VIDCONSOLE_AS_LCD
801 if (strstr(stdoutname, "lcd"))
802 printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
805 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
806 /* set the environment variables (will overwrite previous env settings) */
807 for (i = 0; i < 3; i++) {
808 setenv(stdio_names[i], stdio_devices[i]->name);
810 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
812 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
815 /* If nothing usable installed, use only the initial console */
816 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
819 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
823 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
825 /* Called after the relocation - use desired console functions */
826 int console_init_r(void)
828 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
830 struct list_head *list = stdio_get_list();
831 struct list_head *pos;
832 struct stdio_dev *dev;
834 #ifdef CONFIG_SPLASH_SCREEN
836 * suppress all output if splash screen is enabled and we have
837 * a bmp to display. We redirect the output from frame buffer
838 * console to serial console in this case or suppress it if
839 * "silent" mode was requested.
841 if (getenv("splashimage") != NULL) {
842 if (!(gd->flags & GD_FLG_SILENT))
843 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
847 /* Scan devices looking for input and output devices */
848 list_for_each(pos, list) {
849 dev = list_entry(pos, struct stdio_dev, list);
851 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
854 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
857 if(inputdev && outputdev)
861 /* Initializes output console first */
862 if (outputdev != NULL) {
863 console_setfile(stdout, outputdev);
864 console_setfile(stderr, outputdev);
865 #ifdef CONFIG_CONSOLE_MUX
866 console_devices[stdout][0] = outputdev;
867 console_devices[stderr][0] = outputdev;
871 /* Initializes input console */
872 if (inputdev != NULL) {
873 console_setfile(stdin, inputdev);
874 #ifdef CONFIG_CONSOLE_MUX
875 console_devices[stdin][0] = inputdev;
879 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
880 stdio_print_current_devices();
881 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
883 /* Setting environment variables */
884 for (i = 0; i < 3; i++) {
885 setenv(stdio_names[i], stdio_devices[i]->name);
888 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
891 /* If nothing usable installed, use only the initial console */
892 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
895 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
899 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */