]>
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 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include <stdio_dev.h>
30 #include <environment.h>
32 DECLARE_GLOBAL_DATA_PTR
;
34 static int on_console(const char *name
, const char *value
, enum env_op op
,
39 /* Check for console redirection */
40 if (strcmp(name
, "stdin") == 0)
42 else if (strcmp(name
, "stdout") == 0)
44 else if (strcmp(name
, "stderr") == 0)
47 /* if not actually setting a console variable, we don't care */
48 if (console
== -1 || (gd
->flags
& GD_FLG_DEVINIT
) == 0)
53 case env_op_overwrite
:
55 #ifdef CONFIG_CONSOLE_MUX
56 if (iomux_doenv(console
, value
))
59 /* Try assigning specified device */
60 if (console_assign(console
, value
) < 0)
62 #endif /* CONFIG_CONSOLE_MUX */
66 if ((flags
& H_FORCE
) == 0)
67 printf("Can't delete \"%s\"\n", name
);
74 U_BOOT_ENV_CALLBACK(console
, on_console
);
76 #ifdef CONFIG_SILENT_CONSOLE
77 static int on_silent(const char *name
, const char *value
, enum env_op op
,
80 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
81 if (flags
& H_INTERACTIVE
)
84 #ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
85 if ((flags
& H_INTERACTIVE
) == 0)
90 gd
->flags
|= GD_FLG_SILENT
;
92 gd
->flags
&= ~GD_FLG_SILENT
;
96 U_BOOT_ENV_CALLBACK(silent
, on_silent
);
99 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
101 * if overwrite_console returns 1, the stdin, stderr and stdout
102 * are switched to the serial port, else the settings in the
103 * environment are used
105 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
106 extern int overwrite_console(void);
107 #define OVERWRITE_CONSOLE overwrite_console()
109 #define OVERWRITE_CONSOLE 0
110 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
112 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
114 static int console_setfile(int file
, struct stdio_dev
* dev
)
125 /* Start new device */
127 error
= dev
->start();
128 /* If it's not started dont use it */
133 /* Assign the new device (leaving the existing one started) */
134 stdio_devices
[file
] = dev
;
137 * Update monitor functions
138 * (to use the console stuff by other applications)
142 gd
->jt
[XF_getc
] = dev
->getc
;
143 gd
->jt
[XF_tstc
] = dev
->tstc
;
146 gd
->jt
[XF_putc
] = dev
->putc
;
147 gd
->jt
[XF_puts
] = dev
->puts
;
148 gd
->jt
[XF_printf
] = printf
;
153 default: /* Invalid file ID */
159 #if defined(CONFIG_CONSOLE_MUX)
160 /** Console I/O multiplexing *******************************************/
162 static struct stdio_dev
*tstcdev
;
163 struct stdio_dev
**console_devices
[MAX_FILES
];
164 int cd_count
[MAX_FILES
];
167 * This depends on tstc() always being called before getc().
168 * This is guaranteed to be true because this routine is called
169 * only from fgetc() which assures it.
170 * No attempt is made to demultiplex multiple input sources.
172 static int console_getc(int file
)
176 /* This is never called with testcdev == NULL */
177 ret
= tstcdev
->getc();
182 static int console_tstc(int file
)
185 struct stdio_dev
*dev
;
188 for (i
= 0; i
< cd_count
[file
]; i
++) {
189 dev
= console_devices
[file
][i
];
190 if (dev
->tstc
!= NULL
) {
204 static void console_putc(int file
, const char c
)
207 struct stdio_dev
*dev
;
209 for (i
= 0; i
< cd_count
[file
]; i
++) {
210 dev
= console_devices
[file
][i
];
211 if (dev
->putc
!= NULL
)
216 static void console_puts(int file
, const char *s
)
219 struct stdio_dev
*dev
;
221 for (i
= 0; i
< cd_count
[file
]; i
++) {
222 dev
= console_devices
[file
][i
];
223 if (dev
->puts
!= NULL
)
228 static inline void console_printdevs(int file
)
230 iomux_printdevs(file
);
233 static inline void console_doenv(int file
, struct stdio_dev
*dev
)
235 iomux_doenv(file
, dev
->name
);
238 static inline int console_getc(int file
)
240 return stdio_devices
[file
]->getc();
243 static inline int console_tstc(int file
)
245 return stdio_devices
[file
]->tstc();
248 static inline void console_putc(int file
, const char c
)
250 stdio_devices
[file
]->putc(c
);
253 static inline void console_puts(int file
, const char *s
)
255 stdio_devices
[file
]->puts(s
);
258 static inline void console_printdevs(int file
)
260 printf("%s\n", stdio_devices
[file
]->name
);
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 if (gd
->flags
& GD_FLG_DEVINIT
) {
372 /* Get from the standard input */
376 /* Send directly to the handler */
377 return serial_getc();
382 #ifdef CONFIG_DISABLE_CONSOLE
383 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
387 if (!gd
->have_console
)
390 if (gd
->flags
& GD_FLG_DEVINIT
) {
391 /* Test the standard input */
395 /* Send directly to the handler */
396 return serial_tstc();
399 #ifdef CONFIG_PRE_CONSOLE_BUFFER
400 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
402 static void pre_console_putc(const char c
)
404 char *buffer
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
406 buffer
[CIRC_BUF_IDX(gd
->precon_buf_idx
++)] = c
;
409 static void pre_console_puts(const char *s
)
412 pre_console_putc(*s
++);
415 static void print_pre_console_buffer(void)
418 char *buffer
= (char *)CONFIG_PRE_CON_BUF_ADDR
;
420 if (gd
->precon_buf_idx
> CONFIG_PRE_CON_BUF_SZ
)
421 i
= gd
->precon_buf_idx
- CONFIG_PRE_CON_BUF_SZ
;
423 while (i
< gd
->precon_buf_idx
)
424 putc(buffer
[CIRC_BUF_IDX(i
++)]);
427 static inline void pre_console_putc(const char c
) {}
428 static inline void pre_console_puts(const char *s
) {}
429 static inline void print_pre_console_buffer(void) {}
432 void putc(const char c
)
434 #ifdef CONFIG_SILENT_CONSOLE
435 if (gd
->flags
& GD_FLG_SILENT
)
439 #ifdef CONFIG_DISABLE_CONSOLE
440 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
444 if (!gd
->have_console
)
445 return pre_console_putc(c
);
447 if (gd
->flags
& GD_FLG_DEVINIT
) {
448 /* Send to the standard output */
451 /* Send directly to the handler */
456 void puts(const char *s
)
458 #ifdef CONFIG_SILENT_CONSOLE
459 if (gd
->flags
& GD_FLG_SILENT
)
463 #ifdef CONFIG_DISABLE_CONSOLE
464 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
468 if (!gd
->have_console
)
469 return pre_console_puts(s
);
471 if (gd
->flags
& GD_FLG_DEVINIT
) {
472 /* Send to the standard output */
475 /* Send directly to the handler */
480 int printf(const char *fmt
, ...)
484 char printbuffer
[CONFIG_SYS_PBSIZE
];
486 #ifndef CONFIG_PRE_CONSOLE_BUFFER
487 if (!gd
->have_console
)
493 /* For this to work, printbuffer must be larger than
494 * anything we ever want to print.
496 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
499 /* Print the string */
504 int vprintf(const char *fmt
, va_list args
)
507 char printbuffer
[CONFIG_SYS_PBSIZE
];
509 #ifndef CONFIG_PRE_CONSOLE_BUFFER
510 if (!gd
->have_console
)
514 /* For this to work, printbuffer must be larger than
515 * anything we ever want to print.
517 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
519 /* Print the string */
524 /* test if ctrl-c was pressed */
525 static int ctrlc_disabled
= 0; /* see disable_ctrl() */
526 static int ctrlc_was_pressed
= 0;
529 if (!ctrlc_disabled
&& gd
->have_console
) {
532 case 0x03: /* ^C - Control C */
533 ctrlc_was_pressed
= 1;
543 /* pass 1 to disable ctrlc() checking, 0 to enable.
544 * returns previous state
546 int disable_ctrlc(int disable
)
548 int prev
= ctrlc_disabled
; /* save previous state */
550 ctrlc_disabled
= disable
;
556 return ctrlc_was_pressed
;
559 void clear_ctrlc(void)
561 ctrlc_was_pressed
= 0;
564 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
566 char *cursor
= screen
;
568 inline void dbg(const char *fmt
, ...)
572 char printbuffer
[CONFIG_SYS_PBSIZE
];
575 memset(screen
, 0, sizeof(screen
));
581 /* For this to work, printbuffer must be larger than
582 * anything we ever want to print.
584 i
= vsnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
587 if ((screen
+ sizeof(screen
) - 1 - cursor
)
588 < strlen(printbuffer
) + 1) {
589 memset(screen
, 0, sizeof(screen
));
592 sprintf(cursor
, printbuffer
);
593 cursor
+= strlen(printbuffer
);
597 inline void dbg(const char *fmt
, ...)
602 /** U-Boot INIT FUNCTIONS *************************************************/
604 struct stdio_dev
*search_device(int flags
, const char *name
)
606 struct stdio_dev
*dev
;
608 dev
= stdio_get_by_name(name
);
610 if (dev
&& (dev
->flags
& flags
))
616 int console_assign(int file
, const char *devname
)
619 struct stdio_dev
*dev
;
621 /* Check for valid file */
624 flag
= DEV_FLAGS_INPUT
;
628 flag
= DEV_FLAGS_OUTPUT
;
634 /* Check for valid device name */
636 dev
= search_device(flag
, devname
);
639 return console_setfile(file
, dev
);
644 /* Called before relocation - use serial functions */
645 int console_init_f(void)
647 gd
->have_console
= 1;
649 #ifdef CONFIG_SILENT_CONSOLE
650 if (getenv("silent") != NULL
)
651 gd
->flags
|= GD_FLG_SILENT
;
654 print_pre_console_buffer();
659 void stdio_print_current_devices(void)
661 /* Print information */
663 if (stdio_devices
[stdin
] == NULL
) {
664 puts("No input devices available!\n");
666 printf ("%s\n", stdio_devices
[stdin
]->name
);
670 if (stdio_devices
[stdout
] == NULL
) {
671 puts("No output devices available!\n");
673 printf ("%s\n", stdio_devices
[stdout
]->name
);
677 if (stdio_devices
[stderr
] == NULL
) {
678 puts("No error devices available!\n");
680 printf ("%s\n", stdio_devices
[stderr
]->name
);
684 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
685 /* Called after the relocation - use desired console functions */
686 int console_init_r(void)
688 char *stdinname
, *stdoutname
, *stderrname
;
689 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
, *errdev
= NULL
;
690 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
692 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
693 #ifdef CONFIG_CONSOLE_MUX
697 /* set default handlers at first */
698 gd
->jt
[XF_getc
] = serial_getc
;
699 gd
->jt
[XF_tstc
] = serial_tstc
;
700 gd
->jt
[XF_putc
] = serial_putc
;
701 gd
->jt
[XF_puts
] = serial_puts
;
702 gd
->jt
[XF_printf
] = serial_printf
;
704 /* stdin stdout and stderr are in environment */
706 stdinname
= getenv("stdin");
707 stdoutname
= getenv("stdout");
708 stderrname
= getenv("stderr");
710 if (OVERWRITE_CONSOLE
== 0) { /* if not overwritten by config switch */
711 inputdev
= search_device(DEV_FLAGS_INPUT
, stdinname
);
712 outputdev
= search_device(DEV_FLAGS_OUTPUT
, stdoutname
);
713 errdev
= search_device(DEV_FLAGS_OUTPUT
, stderrname
);
714 #ifdef CONFIG_CONSOLE_MUX
715 iomux_err
= iomux_doenv(stdin
, stdinname
);
716 iomux_err
+= iomux_doenv(stdout
, stdoutname
);
717 iomux_err
+= iomux_doenv(stderr
, stderrname
);
719 /* Successful, so skip all the code below. */
723 /* if the devices are overwritten or not found, use default device */
724 if (inputdev
== NULL
) {
725 inputdev
= search_device(DEV_FLAGS_INPUT
, "serial");
727 if (outputdev
== NULL
) {
728 outputdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
730 if (errdev
== NULL
) {
731 errdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
733 /* Initializes output console first */
734 if (outputdev
!= NULL
) {
735 /* need to set a console if not done above. */
736 console_doenv(stdout
, outputdev
);
738 if (errdev
!= NULL
) {
739 /* need to set a console if not done above. */
740 console_doenv(stderr
, errdev
);
742 if (inputdev
!= NULL
) {
743 /* need to set a console if not done above. */
744 console_doenv(stdin
, inputdev
);
747 #ifdef CONFIG_CONSOLE_MUX
751 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
752 stdio_print_current_devices();
753 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
755 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
756 /* set the environment variables (will overwrite previous env settings) */
757 for (i
= 0; i
< 3; i
++) {
758 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
760 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
762 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
765 /* If nothing usable installed, use only the initial console */
766 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
772 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
774 /* Called after the relocation - use desired console functions */
775 int console_init_r(void)
777 struct stdio_dev
*inputdev
= NULL
, *outputdev
= NULL
;
779 struct list_head
*list
= stdio_get_list();
780 struct list_head
*pos
;
781 struct stdio_dev
*dev
;
783 #ifdef CONFIG_SPLASH_SCREEN
785 * suppress all output if splash screen is enabled and we have
786 * a bmp to display. We redirect the output from frame buffer
787 * console to serial console in this case or suppress it if
788 * "silent" mode was requested.
790 if (getenv("splashimage") != NULL
) {
791 if (!(gd
->flags
& GD_FLG_SILENT
))
792 outputdev
= search_device (DEV_FLAGS_OUTPUT
, "serial");
796 /* Scan devices looking for input and output devices */
797 list_for_each(pos
, list
) {
798 dev
= list_entry(pos
, struct stdio_dev
, list
);
800 if ((dev
->flags
& DEV_FLAGS_INPUT
) && (inputdev
== NULL
)) {
803 if ((dev
->flags
& DEV_FLAGS_OUTPUT
) && (outputdev
== NULL
)) {
806 if(inputdev
&& outputdev
)
810 /* Initializes output console first */
811 if (outputdev
!= NULL
) {
812 console_setfile(stdout
, outputdev
);
813 console_setfile(stderr
, outputdev
);
814 #ifdef CONFIG_CONSOLE_MUX
815 console_devices
[stdout
][0] = outputdev
;
816 console_devices
[stderr
][0] = outputdev
;
820 /* Initializes input console */
821 if (inputdev
!= NULL
) {
822 console_setfile(stdin
, inputdev
);
823 #ifdef CONFIG_CONSOLE_MUX
824 console_devices
[stdin
][0] = inputdev
;
828 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
829 stdio_print_current_devices();
830 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
832 /* Setting environment variables */
833 for (i
= 0; i
< 3; i
++) {
834 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
837 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
840 /* If nothing usable installed, use only the initial console */
841 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
848 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */