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 CFG_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 CFG_CONSOLE_OVERWRITE_ROUTINE
43 extern int overwrite_console (void);
44 #define OVERWRITE_CONSOLE overwrite_console ()
46 #define OVERWRITE_CONSOLE 0
47 #endif /* CFG_CONSOLE_OVERWRITE_ROUTINE */
49 #endif /* CFG_CONSOLE_IS_IN_ENV */
51 static int console_setfile (int file, device_t * dev)
62 /* Start new device */
64 error = dev->start ();
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 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
98 void serial_printf (const char *fmt, ...)
102 char printbuffer[CFG_PBSIZE];
104 va_start (args, fmt);
106 /* For this to work, printbuffer must be larger than
107 * anything we ever want to print.
109 i = vsprintf (printbuffer, fmt, args);
112 serial_puts (printbuffer);
117 if (file < MAX_FILES)
118 return stdio_devices[file]->getc ();
125 if (file < MAX_FILES)
126 return stdio_devices[file]->tstc ();
131 void fputc (int file, const char c)
133 if (file < MAX_FILES)
134 stdio_devices[file]->putc (c);
137 void fputs (int file, const char *s)
139 if (file < MAX_FILES)
140 stdio_devices[file]->puts (s);
143 void fprintf (int file, const char *fmt, ...)
147 char printbuffer[CFG_PBSIZE];
149 va_start (args, fmt);
151 /* For this to work, printbuffer must be larger than
152 * anything we ever want to print.
154 i = vsprintf (printbuffer, fmt, args);
157 /* Send to desired file */
158 fputs (file, printbuffer);
161 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
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 if (gd->flags & GD_FLG_DEVINIT) {
177 /* Test the standard input */
178 return ftstc (stdin);
181 /* Send directly to the handler */
182 return serial_tstc ();
185 void putc (const char c)
187 #ifdef CONFIG_SILENT_CONSOLE
188 if (gd->flags & GD_FLG_SILENT)
192 if (gd->flags & GD_FLG_DEVINIT) {
193 /* Send to the standard output */
196 /* Send directly to the handler */
201 void puts (const char *s)
203 #ifdef CONFIG_SILENT_CONSOLE
204 if (gd->flags & GD_FLG_SILENT)
208 if (gd->flags & GD_FLG_DEVINIT) {
209 /* Send to the standard output */
212 /* Send directly to the handler */
217 void printf (const char *fmt, ...)
221 char printbuffer[CFG_PBSIZE];
223 va_start (args, fmt);
225 /* For this to work, printbuffer must be larger than
226 * anything we ever want to print.
228 i = vsprintf (printbuffer, fmt, args);
231 /* Print the string */
235 void vprintf (const char *fmt, va_list args)
238 char printbuffer[CFG_PBSIZE];
240 /* For this to work, printbuffer must be larger than
241 * anything we ever want to print.
243 i = vsprintf (printbuffer, fmt, args);
245 /* Print the string */
249 /* test if ctrl-c was pressed */
250 static int ctrlc_disabled = 0; /* see disable_ctrl() */
251 static int ctrlc_was_pressed = 0;
254 if (!ctrlc_disabled && gd->have_console) {
257 case 0x03: /* ^C - Control C */
258 ctrlc_was_pressed = 1;
268 /* pass 1 to disable ctrlc() checking, 0 to enable.
269 * returns previous state
271 int disable_ctrlc (int disable)
273 int prev = ctrlc_disabled; /* save previous state */
275 ctrlc_disabled = disable;
281 return ctrlc_was_pressed;
284 void clear_ctrlc (void)
286 ctrlc_was_pressed = 0;
289 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
291 char *cursor = screen;
293 inline void dbg(const char *fmt, ...)
297 char printbuffer[CFG_PBSIZE];
300 memset(screen, 0, sizeof(screen));
306 /* For this to work, printbuffer must be larger than
307 * anything we ever want to print.
309 i = vsprintf(printbuffer, fmt, args);
312 if ((screen + sizeof(screen) - 1 - cursor) < strlen(printbuffer)+1) {
313 memset(screen, 0, sizeof(screen));
316 sprintf(cursor, printbuffer);
317 cursor += strlen(printbuffer);
321 inline void dbg(const char *fmt, ...)
326 /** U-Boot INIT FUNCTIONS *************************************************/
328 int console_assign (int file, char *devname)
332 /* Check for valid file */
335 flag = DEV_FLAGS_INPUT;
339 flag = DEV_FLAGS_OUTPUT;
345 /* Check for valid device name */
347 for (i = 1; i <= ListNumItems (devlist); i++) {
348 device_t *dev = ListGetPtrToItem (devlist, i);
350 if (strcmp (devname, dev->name) == 0) {
351 if (dev->flags & flag)
352 return console_setfile (file, dev);
361 /* Called before relocation - use serial functions */
362 int console_init_f (void)
364 gd->have_console = 1;
366 #ifdef CONFIG_SILENT_CONSOLE
367 if (getenv("silent") != NULL)
368 gd->flags |= GD_FLG_SILENT;
374 #if defined(CFG_CONSOLE_IS_IN_ENV) || defined(CONFIG_SPLASH_SCREEN) || defined(CONFIG_SILENT_CONSOLE)
375 /* search a device */
376 device_t *search_device (int flags, char *name)
379 device_t *dev = NULL;
381 items = ListNumItems (devlist);
385 for (i = 1; i <= items; i++) {
386 dev = ListGetPtrToItem (devlist, i);
387 if ((dev->flags & flags) && (strcmp (name, dev->name) == 0)) {
393 #endif /* CFG_CONSOLE_IS_IN_ENV || CONFIG_SPLASH_SCREEN */
395 #ifdef CFG_CONSOLE_IS_IN_ENV
396 /* Called after the relocation - use desired console functions */
397 int console_init_r (void)
399 char *stdinname, *stdoutname, *stderrname;
400 device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
401 #ifdef CFG_CONSOLE_ENV_OVERWRITE
403 #endif /* CFG_CONSOLE_ENV_OVERWRITE */
405 /* set default handlers at first */
406 gd->jt[XF_getc] = serial_getc;
407 gd->jt[XF_tstc] = serial_tstc;
408 gd->jt[XF_putc] = serial_putc;
409 gd->jt[XF_puts] = serial_puts;
410 gd->jt[XF_printf] = serial_printf;
412 /* stdin stdout and stderr are in environment */
414 stdinname = getenv ("stdin");
415 stdoutname = getenv ("stdout");
416 stderrname = getenv ("stderr");
418 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
419 inputdev = search_device (DEV_FLAGS_INPUT, stdinname);
420 outputdev = search_device (DEV_FLAGS_OUTPUT, stdoutname);
421 errdev = search_device (DEV_FLAGS_OUTPUT, stderrname);
423 /* if the devices are overwritten or not found, use default device */
424 if (inputdev == NULL) {
425 inputdev = search_device (DEV_FLAGS_INPUT, "serial");
427 if (outputdev == NULL) {
428 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
430 if (errdev == NULL) {
431 errdev = search_device (DEV_FLAGS_OUTPUT, "serial");
433 /* Initializes output console first */
434 if (outputdev != NULL) {
435 console_setfile (stdout, outputdev);
437 if (errdev != NULL) {
438 console_setfile (stderr, errdev);
440 if (inputdev != NULL) {
441 console_setfile (stdin, inputdev);
444 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
446 #ifndef CFG_CONSOLE_INFO_QUIET
447 /* Print information */
449 if (stdio_devices[stdin] == NULL) {
450 puts ("No input devices available!\n");
452 printf ("%s\n", stdio_devices[stdin]->name);
456 if (stdio_devices[stdout] == NULL) {
457 puts ("No output devices available!\n");
459 printf ("%s\n", stdio_devices[stdout]->name);
463 if (stdio_devices[stderr] == NULL) {
464 puts ("No error devices available!\n");
466 printf ("%s\n", stdio_devices[stderr]->name);
468 #endif /* CFG_CONSOLE_INFO_QUIET */
470 #ifdef CFG_CONSOLE_ENV_OVERWRITE
471 /* set the environment variables (will overwrite previous env settings) */
472 for (i = 0; i < 3; i++) {
473 setenv (stdio_names[i], stdio_devices[i]->name);
475 #endif /* CFG_CONSOLE_ENV_OVERWRITE */
478 /* If nothing usable installed, use only the initial console */
479 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
485 #else /* CFG_CONSOLE_IS_IN_ENV */
487 /* Called after the relocation - use desired console functions */
488 int console_init_r (void)
490 device_t *inputdev = NULL, *outputdev = NULL;
491 int i, items = ListNumItems (devlist);
493 #ifdef CONFIG_SPLASH_SCREEN
494 /* suppress all output if splash screen is enabled and we have
496 if (getenv("splashimage") != NULL)
497 gd->flags |= GD_FLG_SILENT;
500 /* Scan devices looking for input and output devices */
502 (i <= items) && ((inputdev == NULL) || (outputdev == NULL));
505 device_t *dev = ListGetPtrToItem (devlist, i);
507 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
510 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
515 /* Initializes output console first */
516 if (outputdev != NULL) {
517 console_setfile (stdout, outputdev);
518 console_setfile (stderr, outputdev);
521 /* Initializes input console */
522 if (inputdev != NULL) {
523 console_setfile (stdin, inputdev);
526 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
528 #ifndef CFG_CONSOLE_INFO_QUIET
529 /* Print information */
531 if (stdio_devices[stdin] == NULL) {
532 puts ("No input devices available!\n");
534 printf ("%s\n", stdio_devices[stdin]->name);
538 if (stdio_devices[stdout] == NULL) {
539 puts ("No output devices available!\n");
541 printf ("%s\n", stdio_devices[stdout]->name);
545 if (stdio_devices[stderr] == NULL) {
546 puts ("No error devices available!\n");
548 printf ("%s\n", stdio_devices[stderr]->name);
550 #endif /* CFG_CONSOLE_INFO_QUIET */
552 /* Setting environment variables */
553 for (i = 0; i < 3; i++) {
554 setenv (stdio_names[i], stdio_devices[i]->name);
558 /* If nothing usable installed, use only the initial console */
559 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
566 #endif /* CFG_CONSOLE_IS_IN_ENV */