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,
32 #ifdef CFG_CONSOLE_IS_IN_ENV
34 * if overwrite_console returns 1, the stdin, stderr and stdout
35 * are switched to the serial port, else the settings in the
36 * environment are used
38 #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
39 extern int overwrite_console (void);
41 int overwrite_console (void)
45 #endif /* CFG_CONSOLE_OVERWRITE_ROUTINE */
47 #endif /* CFG_CONSOLE_IS_IN_ENV */
49 static int console_setfile (int file, device_t * dev)
60 /* Start new device */
62 error = dev->start ();
63 /* If it's not started dont use it */
68 /* Assign the new device (leaving the existing one started) */
69 stdio_devices[file] = dev;
72 * Update monitor functions
73 * (to use the console stuff by other applications)
77 syscall_tbl[SYSCALL_GETC] = dev->getc;
78 syscall_tbl[SYSCALL_TSTC] = dev->tstc;
81 syscall_tbl[SYSCALL_PUTC] = dev->putc;
82 syscall_tbl[SYSCALL_PUTS] = dev->puts;
83 syscall_tbl[SYSCALL_PRINTF] = printf;
88 default: /* Invalid file ID */
94 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
96 void serial_printf (const char *fmt, ...)
100 char printbuffer[CFG_PBSIZE];
102 va_start (args, fmt);
104 /* For this to work, printbuffer must be larger than
105 * anything we ever want to print.
107 i = vsprintf (printbuffer, fmt, args);
110 serial_puts (printbuffer);
115 if (file < MAX_FILES)
116 return stdio_devices[file]->getc ();
123 if (file < MAX_FILES)
124 return stdio_devices[file]->tstc ();
129 void fputc (int file, const char c)
131 if (file < MAX_FILES)
132 stdio_devices[file]->putc (c);
135 void fputs (int file, const char *s)
137 if (file < MAX_FILES)
138 stdio_devices[file]->puts (s);
141 void fprintf (int file, const char *fmt, ...)
145 char printbuffer[CFG_PBSIZE];
147 va_start (args, fmt);
149 /* For this to work, printbuffer must be larger than
150 * anything we ever want to print.
152 i = vsprintf (printbuffer, fmt, args);
155 /* Send to desired file */
156 fputs (file, printbuffer);
159 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
163 DECLARE_GLOBAL_DATA_PTR;
165 if (gd->flags & GD_FLG_DEVINIT) {
166 /* Get from the standard input */
167 return fgetc (stdin);
170 /* Send directly to the handler */
171 return serial_getc ();
176 DECLARE_GLOBAL_DATA_PTR;
178 if (gd->flags & GD_FLG_DEVINIT) {
179 /* Test the standard input */
180 return ftstc (stdin);
183 /* Send directly to the handler */
184 return serial_tstc ();
187 void putc (const char c)
189 DECLARE_GLOBAL_DATA_PTR;
191 if (gd->flags & GD_FLG_DEVINIT) {
192 /* Send to the standard output */
195 /* Send directly to the handler */
200 void puts (const char *s)
202 DECLARE_GLOBAL_DATA_PTR;
204 if (gd->flags & GD_FLG_DEVINIT) {
205 /* Send to the standard output */
208 /* Send directly to the handler */
213 void printf (const char *fmt, ...)
217 char printbuffer[CFG_PBSIZE];
219 va_start (args, fmt);
221 /* For this to work, printbuffer must be larger than
222 * anything we ever want to print.
224 i = vsprintf (printbuffer, fmt, args);
227 /* Print the string */
231 /* test if ctrl-c was pressed */
232 static int ctrlc_disabled = 0; /* see disable_ctrl() */
233 static int ctrlc_was_pressed = 0;
236 DECLARE_GLOBAL_DATA_PTR;
238 if (!ctrlc_disabled && gd->have_console) {
241 case 0x03: /* ^C - Control C */
242 ctrlc_was_pressed = 1;
252 /* pass 1 to disable ctrlc() checking, 0 to enable.
253 * returns previous state
255 int disable_ctrlc (int disable)
257 int prev = ctrlc_disabled; /* save previous state */
259 ctrlc_disabled = disable;
265 return ctrlc_was_pressed;
268 void clear_ctrlc (void)
270 ctrlc_was_pressed = 0;
273 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
275 char *cursor = screen;
277 inline void dbg(const char *fmt, ...)
281 char printbuffer[CFG_PBSIZE];
284 memset(screen, 0, sizeof(screen));
290 /* For this to work, printbuffer must be larger than
291 * anything we ever want to print.
293 i = vsprintf(printbuffer, fmt, args);
296 if ((screen + sizeof(screen) - 1 - cursor) < strlen(printbuffer)+1) {
297 memset(screen, 0, sizeof(screen));
300 sprintf(cursor, printbuffer);
301 cursor += strlen(printbuffer);
305 inline void dbg(const char *fmt, ...)
310 /** U-Boot INIT FUNCTIONS *************************************************/
312 int console_assign (int file, char *devname)
316 /* Check for valid file */
319 flag = DEV_FLAGS_INPUT;
323 flag = DEV_FLAGS_OUTPUT;
329 /* Check for valid device name */
331 for (i = 1; i <= ListNumItems (devlist); i++) {
332 device_t *dev = ListGetPtrToItem (devlist, i);
334 if (strcmp (devname, dev->name) == 0) {
335 if (dev->flags & flag)
336 return console_setfile (file, dev);
345 /* Called before relocation - use serial functions */
346 int console_init_f (void)
348 DECLARE_GLOBAL_DATA_PTR;
350 gd->have_console = 1;
354 #ifdef CFG_CONSOLE_IS_IN_ENV
355 /* search a device */
356 device_t *search_device (int flags, char *name)
359 device_t *dev = NULL;
361 items = ListNumItems (devlist);
365 for (i = 1; i <= items; i++) {
366 dev = ListGetPtrToItem (devlist, i);
367 if ((dev->flags & flags) && (strcmp (name, dev->name) == 0)) {
373 #endif /* CFG_CONSOLE_IS_IN_ENV */
375 #ifdef CFG_CONSOLE_IS_IN_ENV
376 /* Called after the relocation - use desired console functions */
377 int console_init_r (void)
379 char *stdinname, *stdoutname, *stderrname;
380 device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
382 /* set default handlers at first */
383 syscall_tbl[SYSCALL_GETC] = serial_getc;
384 syscall_tbl[SYSCALL_TSTC] = serial_tstc;
385 syscall_tbl[SYSCALL_PUTC] = serial_putc;
386 syscall_tbl[SYSCALL_PUTS] = serial_puts;
387 syscall_tbl[SYSCALL_PRINTF] = serial_printf;
389 /* stdin stdout and stderr are in environment */
391 stdinname = getenv ("stdin");
392 stdoutname = getenv ("stdout");
393 stderrname = getenv ("stderr");
395 if (overwrite_console () == 0) { /* if not overwritten by config switch */
396 inputdev = search_device (DEV_FLAGS_INPUT, stdinname);
397 outputdev = search_device (DEV_FLAGS_OUTPUT, stdoutname);
398 errdev = search_device (DEV_FLAGS_OUTPUT, stderrname);
400 /* if the devices are overwritten or not found, use default device */
401 if (inputdev == NULL) {
402 inputdev = search_device (DEV_FLAGS_INPUT, "serial");
404 if (outputdev == NULL) {
405 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
407 if (errdev == NULL) {
408 errdev = search_device (DEV_FLAGS_OUTPUT, "serial");
410 /* Initializes output console first */
411 if (outputdev != NULL) {
412 console_setfile (stdout, outputdev);
414 if (errdev != NULL) {
415 console_setfile (stderr, errdev);
417 if (inputdev != NULL) {
418 console_setfile (stdin, inputdev);
421 #ifndef CFG_CONSOLE_INFO_QUIET
422 /* Print information */
424 if (stdio_devices[stdin] == NULL) {
425 printf ("No input devices available!\n");
427 printf ("%s\n", stdio_devices[stdin]->name);
431 if (stdio_devices[stdout] == NULL) {
432 printf ("No output devices available!\n");
434 printf ("%s\n", stdio_devices[stdout]->name);
438 if (stdio_devices[stderr] == NULL) {
439 printf ("No error devices available!\n");
441 printf ("%s\n", stdio_devices[stderr]->name);
443 #endif /* CFG_CONSOLE_INFO_QUIET */
445 #ifdef CFG_CONSOLE_ENV_OVERWRITE
446 /* set the environment variables (will overwrite previous env settings) */
447 for (i = 0; i < 3; i++) {
448 setenv (stdio_names[i], stdio_devices[i]->name);
450 #endif /* CFG_CONSOLE_ENV_OVERWRITE */
453 /* If nothing usable installed, use only the initial console */
454 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
460 #else /* CFG_CONSOLE_IS_IN_ENV */
462 /* Called after the relocation - use desired console functions */
463 int console_init_r (void)
465 device_t *inputdev = NULL, *outputdev = NULL;
466 int i, items = ListNumItems (devlist);
468 /* Scan devices looking for input and output devices */
470 (i <= items) && ((inputdev == NULL) || (outputdev == NULL));
473 device_t *dev = ListGetPtrToItem (devlist, i);
475 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
478 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
483 /* Initializes output console first */
484 if (outputdev != NULL) {
485 console_setfile (stdout, outputdev);
486 console_setfile (stderr, outputdev);
489 /* Initializes input console */
490 if (inputdev != NULL) {
491 console_setfile (stdin, inputdev);
494 #ifndef CFG_CONSOLE_INFO_QUIET
495 /* Print informations */
497 if (stdio_devices[stdin] == NULL) {
498 printf ("No input devices available!\n");
500 printf ("%s\n", stdio_devices[stdin]->name);
504 if (stdio_devices[stdout] == NULL) {
505 printf ("No output devices available!\n");
507 printf ("%s\n", stdio_devices[stdout]->name);
511 if (stdio_devices[stderr] == NULL) {
512 printf ("No error devices available!\n");
514 printf ("%s\n", stdio_devices[stderr]->name);
516 #endif /* CFG_CONSOLE_INFO_QUIET */
518 /* Setting environment variables */
519 for (i = 0; i < 3; i++) {
520 setenv (stdio_names[i], stdio_devices[i]->name);
524 /* If nothing usable installed, use only the initial console */
525 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
532 #endif /* CFG_CONSOLE_IS_IN_ENV */