]>
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,
30 DECLARE_GLOBAL_DATA_PTR
;
32 #ifdef CONFIG_AMIGAONEG3SE
33 int console_changed
= 0;
36 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
38 * if overwrite_console returns 1, the stdin, stderr and stdout
39 * are switched to the serial port, else the settings in the
40 * environment are used
42 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
43 extern int overwrite_console(void);
44 #define OVERWRITE_CONSOLE overwrite_console()
46 #define OVERWRITE_CONSOLE 0
47 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
49 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
51 static int console_setfile(int file
, device_t
* dev
)
62 /* Start new device */
65 /* If it's not started dont use it */
70 /* Assign the new device (leaving the existing one started) */
71 stdio_devices
[file
] = dev
;
74 * Update monitor functions
75 * (to use the console stuff by other applications)
79 gd
->jt
[XF_getc
] = dev
->getc
;
80 gd
->jt
[XF_tstc
] = dev
->tstc
;
83 gd
->jt
[XF_putc
] = dev
->putc
;
84 gd
->jt
[XF_puts
] = dev
->puts
;
85 gd
->jt
[XF_printf
] = printf
;
90 default: /* Invalid file ID */
96 #if defined(CONFIG_CONSOLE_MUX)
97 /** Console I/O multiplexing *******************************************/
99 static device_t
*tstcdev
;
100 device_t
**console_devices
[MAX_FILES
];
101 int cd_count
[MAX_FILES
];
104 * This depends on tstc() always being called before getc().
105 * This is guaranteed to be true because this routine is called
106 * only from fgetc() which assures it.
107 * No attempt is made to demultiplex multiple input sources.
109 static int iomux_getc(void)
113 /* This is never called with testcdev == NULL */
114 ret
= tstcdev
->getc();
119 static int iomux_tstc(int file
)
125 for (i
= 0; i
< cd_count
[file
]; i
++) {
126 dev
= console_devices
[file
][i
];
127 if (dev
->tstc
!= NULL
) {
141 static void iomux_putc(int file
, const char c
)
146 for (i
= 0; i
< cd_count
[file
]; i
++) {
147 dev
= console_devices
[file
][i
];
148 if (dev
->putc
!= NULL
)
153 static void iomux_puts(int file
, const char *s
)
158 for (i
= 0; i
< cd_count
[file
]; i
++) {
159 dev
= console_devices
[file
][i
];
160 if (dev
->puts
!= NULL
)
164 #endif /* defined(CONFIG_CONSOLE_MUX) */
166 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
168 void serial_printf(const char *fmt
, ...)
172 char printbuffer
[CONFIG_SYS_PBSIZE
];
176 /* For this to work, printbuffer must be larger than
177 * anything we ever want to print.
179 i
= vsprintf(printbuffer
, fmt
, args
);
182 serial_puts(printbuffer
);
187 if (file
< MAX_FILES
) {
188 #if defined(CONFIG_CONSOLE_MUX)
190 * Effectively poll for input wherever it may be available.
194 * Upper layer may have already called tstc() so
195 * check for that first.
200 #ifdef CONFIG_WATCHDOG
202 * If the watchdog must be rate-limited then it should
203 * already be handled in board-specific code.
209 return stdio_devices
[file
]->getc();
218 if (file
< MAX_FILES
)
219 #if defined(CONFIG_CONSOLE_MUX)
220 return iomux_tstc(file
);
222 return stdio_devices
[file
]->tstc();
228 void fputc(int file
, const char c
)
230 if (file
< MAX_FILES
)
231 #if defined(CONFIG_CONSOLE_MUX)
234 stdio_devices
[file
]->putc(c
);
238 void fputs(int file
, const char *s
)
240 if (file
< MAX_FILES
)
241 #if defined(CONFIG_CONSOLE_MUX)
244 stdio_devices
[file
]->puts(s
);
248 void fprintf(int file
, const char *fmt
, ...)
252 char printbuffer
[CONFIG_SYS_PBSIZE
];
256 /* For this to work, printbuffer must be larger than
257 * anything we ever want to print.
259 i
= vsprintf(printbuffer
, fmt
, args
);
262 /* Send to desired file */
263 fputs(file
, printbuffer
);
266 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
270 #ifdef CONFIG_DISABLE_CONSOLE
271 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
275 if (gd
->flags
& GD_FLG_DEVINIT
) {
276 /* Get from the standard input */
280 /* Send directly to the handler */
281 return serial_getc();
286 #ifdef CONFIG_DISABLE_CONSOLE
287 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
291 if (gd
->flags
& GD_FLG_DEVINIT
) {
292 /* Test the standard input */
296 /* Send directly to the handler */
297 return serial_tstc();
300 void putc(const char c
)
302 #ifdef CONFIG_SILENT_CONSOLE
303 if (gd
->flags
& GD_FLG_SILENT
)
307 #ifdef CONFIG_DISABLE_CONSOLE
308 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
312 if (gd
->flags
& GD_FLG_DEVINIT
) {
313 /* Send to the standard output */
316 /* Send directly to the handler */
321 void puts(const char *s
)
323 #ifdef CONFIG_SILENT_CONSOLE
324 if (gd
->flags
& GD_FLG_SILENT
)
328 #ifdef CONFIG_DISABLE_CONSOLE
329 if (gd
->flags
& GD_FLG_DISABLE_CONSOLE
)
333 if (gd
->flags
& GD_FLG_DEVINIT
) {
334 /* Send to the standard output */
337 /* Send directly to the handler */
342 void printf(const char *fmt
, ...)
346 char printbuffer
[CONFIG_SYS_PBSIZE
];
350 /* For this to work, printbuffer must be larger than
351 * anything we ever want to print.
353 i
= vsprintf(printbuffer
, fmt
, args
);
356 /* Print the string */
360 void vprintf(const char *fmt
, va_list args
)
363 char printbuffer
[CONFIG_SYS_PBSIZE
];
365 /* For this to work, printbuffer must be larger than
366 * anything we ever want to print.
368 i
= vsprintf(printbuffer
, fmt
, args
);
370 /* Print the string */
374 /* test if ctrl-c was pressed */
375 static int ctrlc_disabled
= 0; /* see disable_ctrl() */
376 static int ctrlc_was_pressed
= 0;
379 if (!ctrlc_disabled
&& gd
->have_console
) {
382 case 0x03: /* ^C - Control C */
383 ctrlc_was_pressed
= 1;
393 /* pass 1 to disable ctrlc() checking, 0 to enable.
394 * returns previous state
396 int disable_ctrlc(int disable
)
398 int prev
= ctrlc_disabled
; /* save previous state */
400 ctrlc_disabled
= disable
;
406 return ctrlc_was_pressed
;
409 void clear_ctrlc(void)
411 ctrlc_was_pressed
= 0;
414 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
416 char *cursor
= screen
;
418 inline void dbg(const char *fmt
, ...)
422 char printbuffer
[CONFIG_SYS_PBSIZE
];
425 memset(screen
, 0, sizeof(screen
));
431 /* For this to work, printbuffer must be larger than
432 * anything we ever want to print.
434 i
= vsprintf(printbuffer
, fmt
, args
);
437 if ((screen
+ sizeof(screen
) - 1 - cursor
)
438 < strlen(printbuffer
) + 1) {
439 memset(screen
, 0, sizeof(screen
));
442 sprintf(cursor
, printbuffer
);
443 cursor
+= strlen(printbuffer
);
447 inline void dbg(const char *fmt
, ...)
452 /** U-Boot INIT FUNCTIONS *************************************************/
454 device_t
*search_device(int flags
, char *name
)
458 dev
= device_get_by_name(name
);
460 if (dev
&& (dev
->flags
& flags
))
466 int console_assign(int file
, char *devname
)
471 /* Check for valid file */
474 flag
= DEV_FLAGS_INPUT
;
478 flag
= DEV_FLAGS_OUTPUT
;
484 /* Check for valid device name */
486 dev
= search_device(flag
, devname
);
489 return console_setfile(file
, dev
);
494 /* Called before relocation - use serial functions */
495 int console_init_f(void)
497 gd
->have_console
= 1;
499 #ifdef CONFIG_SILENT_CONSOLE
500 if (getenv("silent") != NULL
)
501 gd
->flags
|= GD_FLG_SILENT
;
507 #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
508 /* Called after the relocation - use desired console functions */
509 int console_init_r(void)
511 char *stdinname
, *stdoutname
, *stderrname
;
512 device_t
*inputdev
= NULL
, *outputdev
= NULL
, *errdev
= NULL
;
513 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
515 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
516 #ifdef CONFIG_CONSOLE_MUX
520 /* set default handlers at first */
521 gd
->jt
[XF_getc
] = serial_getc
;
522 gd
->jt
[XF_tstc
] = serial_tstc
;
523 gd
->jt
[XF_putc
] = serial_putc
;
524 gd
->jt
[XF_puts
] = serial_puts
;
525 gd
->jt
[XF_printf
] = serial_printf
;
527 /* stdin stdout and stderr are in environment */
529 stdinname
= getenv("stdin");
530 stdoutname
= getenv("stdout");
531 stderrname
= getenv("stderr");
533 if (OVERWRITE_CONSOLE
== 0) { /* if not overwritten by config switch */
534 inputdev
= search_device(DEV_FLAGS_INPUT
, stdinname
);
535 outputdev
= search_device(DEV_FLAGS_OUTPUT
, stdoutname
);
536 errdev
= search_device(DEV_FLAGS_OUTPUT
, stderrname
);
537 #ifdef CONFIG_CONSOLE_MUX
538 iomux_err
= iomux_doenv(stdin
, stdinname
);
539 iomux_err
+= iomux_doenv(stdout
, stdoutname
);
540 iomux_err
+= iomux_doenv(stderr
, stderrname
);
542 /* Successful, so skip all the code below. */
546 /* if the devices are overwritten or not found, use default device */
547 if (inputdev
== NULL
) {
548 inputdev
= search_device(DEV_FLAGS_INPUT
, "serial");
550 if (outputdev
== NULL
) {
551 outputdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
553 if (errdev
== NULL
) {
554 errdev
= search_device(DEV_FLAGS_OUTPUT
, "serial");
556 /* Initializes output console first */
557 if (outputdev
!= NULL
) {
558 #ifdef CONFIG_CONSOLE_MUX
559 /* need to set a console if not done above. */
560 iomux_doenv(stdout
, outputdev
->name
);
562 console_setfile(stdout
, outputdev
);
565 if (errdev
!= NULL
) {
566 #ifdef CONFIG_CONSOLE_MUX
567 /* need to set a console if not done above. */
568 iomux_doenv(stderr
, errdev
->name
);
570 console_setfile(stderr
, errdev
);
573 if (inputdev
!= NULL
) {
574 #ifdef CONFIG_CONSOLE_MUX
575 /* need to set a console if not done above. */
576 iomux_doenv(stdin
, inputdev
->name
);
578 console_setfile(stdin
, inputdev
);
582 #ifdef CONFIG_CONSOLE_MUX
586 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
588 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
589 /* Print information */
591 if (stdio_devices
[stdin
] == NULL
) {
592 puts("No input devices available!\n");
594 #ifdef CONFIG_CONSOLE_MUX
595 iomux_printdevs(stdin
);
597 printf("%s\n", stdio_devices
[stdin
]->name
);
602 if (stdio_devices
[stdout
] == NULL
) {
603 puts("No output devices available!\n");
605 #ifdef CONFIG_CONSOLE_MUX
606 iomux_printdevs(stdout
);
608 printf("%s\n", stdio_devices
[stdout
]->name
);
613 if (stdio_devices
[stderr
] == NULL
) {
614 puts("No error devices available!\n");
616 #ifdef CONFIG_CONSOLE_MUX
617 iomux_printdevs(stderr
);
619 printf("%s\n", stdio_devices
[stderr
]->name
);
622 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
624 #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
625 /* set the environment variables (will overwrite previous env settings) */
626 for (i
= 0; i
< 3; i
++) {
627 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
629 #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
632 /* If nothing usable installed, use only the initial console */
633 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
639 #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
641 /* Called after the relocation - use desired console functions */
642 int console_init_r(void)
644 device_t
*inputdev
= NULL
, *outputdev
= NULL
;
646 struct list_head
*list
= device_get_list();
647 struct list_head
*pos
;
650 #ifdef CONFIG_SPLASH_SCREEN
652 * suppress all output if splash screen is enabled and we have
655 if (getenv("splashimage") != NULL
)
656 gd
->flags
|= GD_FLG_SILENT
;
659 /* Scan devices looking for input and output devices */
660 list_for_each(pos
, list
) {
661 dev
= list_entry(pos
, device_t
, list
);
663 if ((dev
->flags
& DEV_FLAGS_INPUT
) && (inputdev
== NULL
)) {
666 if ((dev
->flags
& DEV_FLAGS_OUTPUT
) && (outputdev
== NULL
)) {
669 if(inputdev
&& outputdev
)
673 /* Initializes output console first */
674 if (outputdev
!= NULL
) {
675 console_setfile(stdout
, outputdev
);
676 console_setfile(stderr
, outputdev
);
677 #ifdef CONFIG_CONSOLE_MUX
678 console_devices
[stdout
][0] = outputdev
;
679 console_devices
[stderr
][0] = outputdev
;
683 /* Initializes input console */
684 if (inputdev
!= NULL
) {
685 console_setfile(stdin
, inputdev
);
686 #ifdef CONFIG_CONSOLE_MUX
687 console_devices
[stdin
][0] = inputdev
;
691 gd
->flags
|= GD_FLG_DEVINIT
; /* device initialization completed */
693 #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
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
);
715 #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
717 /* Setting environment variables */
718 for (i
= 0; i
< 3; i
++) {
719 setenv(stdio_names
[i
], stdio_devices
[i
]->name
);
723 /* If nothing usable installed, use only the initial console */
724 if ((stdio_devices
[stdin
] == NULL
) && (stdio_devices
[stdout
] == NULL
))
731 #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */