Fixed tools/env utilities
[u-boot-openmoko/mini2440.git] / common / console.c
blobd4086a15e92065d48121b5498d625022a54acf2c
1 /*
2 * (C) Copyright 2000
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
6 * project.
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,
21 * MA 02111-1307 USA
24 #include <common.h>
25 #include <stdarg.h>
26 #include <malloc.h>
27 #include <console.h>
28 #include <exports.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 #ifdef CONFIG_AMIGAONEG3SE
33 int console_changed = 0;
34 #endif
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 ()
45 #else
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)
53 int error = 0;
55 if (dev == NULL)
56 return -1;
58 switch (file) {
59 case stdin:
60 case stdout:
61 case stderr:
62 /* Start new device */
63 if (dev->start) {
64 error = dev->start ();
65 /* If it's not started dont use it */
66 if (error < 0)
67 break;
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)
77 switch (file) {
78 case stdin:
79 gd->jt[XF_getc] = dev->getc;
80 gd->jt[XF_tstc] = dev->tstc;
81 break;
82 case stdout:
83 gd->jt[XF_putc] = dev->putc;
84 gd->jt[XF_puts] = dev->puts;
85 gd->jt[XF_printf] = printf;
86 break;
88 break;
90 default: /* Invalid file ID */
91 error = -1;
93 return error;
96 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
98 void serial_printf (const char *fmt, ...)
100 va_list args;
101 uint i;
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);
110 va_end (args);
112 serial_puts (printbuffer);
115 int fgetc (int file)
117 if (file < MAX_FILES)
118 return stdio_devices[file]->getc ();
120 return -1;
123 int ftstc (int file)
125 if (file < MAX_FILES)
126 return stdio_devices[file]->tstc ();
128 return -1;
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, ...)
145 va_list args;
146 uint i;
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);
155 va_end (args);
157 /* Send to desired file */
158 fputs (file, printbuffer);
161 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
163 void (*console_poll_hook)(int activity);
165 int getc (void)
167 while (console_poll_hook && !tstc());
169 if (gd->flags & GD_FLG_DEVINIT) {
170 /* Get from the standard input */
171 return fgetc (stdin);
174 /* Send directly to the handler */
175 return serial_getc ();
178 static int do_tstc (void)
180 if (gd->flags & GD_FLG_DEVINIT) {
181 /* Test the standard input */
182 return ftstc (stdin);
185 /* Send directly to the handler */
186 return serial_tstc ();
189 int tstc (void)
191 int ret;
193 ret = do_tstc();
194 if (console_poll_hook)
195 console_poll_hook(ret);
196 return ret;
199 void putc (const char c)
201 #ifdef CONFIG_SILENT_CONSOLE
202 if (gd->flags & GD_FLG_SILENT)
203 return;
204 #endif
206 if (gd->flags & GD_FLG_DEVINIT) {
207 /* Send to the standard output */
208 fputc (stdout, c);
209 } else {
210 /* Send directly to the handler */
211 serial_putc (c);
215 void puts (const char *s)
217 #ifdef CONFIG_SILENT_CONSOLE
218 if (gd->flags & GD_FLG_SILENT)
219 return;
220 #endif
222 if (gd->flags & GD_FLG_DEVINIT) {
223 /* Send to the standard output */
224 fputs (stdout, s);
225 } else {
226 /* Send directly to the handler */
227 serial_puts (s);
231 void printf (const char *fmt, ...)
233 va_list args;
234 uint i;
235 char printbuffer[CFG_PBSIZE];
237 va_start (args, fmt);
239 /* For this to work, printbuffer must be larger than
240 * anything we ever want to print.
242 i = vsprintf (printbuffer, fmt, args);
243 va_end (args);
245 /* Print the string */
246 puts (printbuffer);
249 void vprintf (const char *fmt, va_list args)
251 uint i;
252 char printbuffer[CFG_PBSIZE];
254 /* For this to work, printbuffer must be larger than
255 * anything we ever want to print.
257 i = vsprintf (printbuffer, fmt, args);
259 /* Print the string */
260 puts (printbuffer);
263 /* test if ctrl-c was pressed */
264 static int ctrlc_disabled = 0; /* see disable_ctrl() */
265 static int ctrlc_was_pressed = 0;
266 int ctrlc (void)
268 if (!ctrlc_disabled && gd->have_console) {
269 if (tstc ()) {
270 switch (getc ()) {
271 case 0x03: /* ^C - Control C */
272 ctrlc_was_pressed = 1;
273 return 1;
274 default:
275 break;
279 return 0;
282 /* pass 1 to disable ctrlc() checking, 0 to enable.
283 * returns previous state
285 int disable_ctrlc (int disable)
287 int prev = ctrlc_disabled; /* save previous state */
289 ctrlc_disabled = disable;
290 return prev;
293 int had_ctrlc (void)
295 return ctrlc_was_pressed;
298 void clear_ctrlc (void)
300 ctrlc_was_pressed = 0;
303 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
304 char screen[1024];
305 char *cursor = screen;
306 int once = 0;
307 inline void dbg(const char *fmt, ...)
309 va_list args;
310 uint i;
311 char printbuffer[CFG_PBSIZE];
313 if (!once) {
314 memset(screen, 0, sizeof(screen));
315 once++;
318 va_start(args, fmt);
320 /* For this to work, printbuffer must be larger than
321 * anything we ever want to print.
323 i = vsprintf(printbuffer, fmt, args);
324 va_end(args);
326 if ((screen + sizeof(screen) - 1 - cursor) < strlen(printbuffer)+1) {
327 memset(screen, 0, sizeof(screen));
328 cursor = screen;
330 sprintf(cursor, printbuffer);
331 cursor += strlen(printbuffer);
334 #else
335 inline void dbg(const char *fmt, ...)
338 #endif
340 /** U-Boot INIT FUNCTIONS *************************************************/
342 int console_assign (int file, char *devname)
344 int flag, i;
346 /* Check for valid file */
347 switch (file) {
348 case stdin:
349 flag = DEV_FLAGS_INPUT;
350 break;
351 case stdout:
352 case stderr:
353 flag = DEV_FLAGS_OUTPUT;
354 break;
355 default:
356 return -1;
359 /* Check for valid device name */
361 for (i = 1; i <= ListNumItems (devlist); i++) {
362 device_t *dev = ListGetPtrToItem (devlist, i);
364 if (strcmp (devname, dev->name) == 0) {
365 if (dev->flags & flag)
366 return console_setfile (file, dev);
368 return -1;
372 return -1;
375 /* Called before relocation - use serial functions */
376 int console_init_f (void)
378 gd->have_console = 1;
380 #ifdef CONFIG_SILENT_CONSOLE
381 if (getenv("silent") != NULL)
382 gd->flags |= GD_FLG_SILENT;
383 #endif
385 return (0);
388 #if defined(CFG_CONSOLE_IS_IN_ENV) || defined(CONFIG_SPLASH_SCREEN) || defined(CONFIG_SILENT_CONSOLE)
389 /* search a device */
390 device_t *search_device (int flags, char *name)
392 int i, items;
393 device_t *dev = NULL;
395 items = ListNumItems (devlist);
396 if (name == NULL)
397 return dev;
399 for (i = 1; i <= items; i++) {
400 dev = ListGetPtrToItem (devlist, i);
401 if ((dev->flags & flags) && (strcmp (name, dev->name) == 0)) {
402 break;
405 return dev;
407 #endif /* CFG_CONSOLE_IS_IN_ENV || CONFIG_SPLASH_SCREEN */
409 #ifdef CFG_CONSOLE_IS_IN_ENV
410 /* Called after the relocation - use desired console functions */
411 int console_init_r (void)
413 char *stdinname, *stdoutname, *stderrname;
414 device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
415 #ifdef CFG_CONSOLE_ENV_OVERWRITE
416 int i;
417 #endif /* CFG_CONSOLE_ENV_OVERWRITE */
419 /* set default handlers at first */
420 gd->jt[XF_getc] = serial_getc;
421 gd->jt[XF_tstc] = serial_tstc;
422 gd->jt[XF_putc] = serial_putc;
423 gd->jt[XF_puts] = serial_puts;
424 gd->jt[XF_printf] = serial_printf;
426 /* stdin stdout and stderr are in environment */
427 /* scan for it */
428 stdinname = getenv ("stdin");
429 stdoutname = getenv ("stdout");
430 stderrname = getenv ("stderr");
432 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
433 inputdev = search_device (DEV_FLAGS_INPUT, stdinname);
434 outputdev = search_device (DEV_FLAGS_OUTPUT, stdoutname);
435 errdev = search_device (DEV_FLAGS_OUTPUT, stderrname);
437 /* if the devices are overwritten or not found, use default device */
438 if (inputdev == NULL) {
439 inputdev = search_device (DEV_FLAGS_INPUT, "serial");
441 if (outputdev == NULL) {
442 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
444 if (errdev == NULL) {
445 errdev = search_device (DEV_FLAGS_OUTPUT, "serial");
447 /* Initializes output console first */
448 if (outputdev != NULL) {
449 console_setfile (stdout, outputdev);
451 if (errdev != NULL) {
452 console_setfile (stderr, errdev);
454 if (inputdev != NULL) {
455 console_setfile (stdin, inputdev);
458 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
460 #ifndef CFG_CONSOLE_INFO_QUIET
461 /* Print information */
462 puts ("In: ");
463 if (stdio_devices[stdin] == NULL) {
464 puts ("No input devices available!\n");
465 } else {
466 printf ("%s\n", stdio_devices[stdin]->name);
469 puts ("Out: ");
470 if (stdio_devices[stdout] == NULL) {
471 puts ("No output devices available!\n");
472 } else {
473 printf ("%s\n", stdio_devices[stdout]->name);
476 puts ("Err: ");
477 if (stdio_devices[stderr] == NULL) {
478 puts ("No error devices available!\n");
479 } else {
480 printf ("%s\n", stdio_devices[stderr]->name);
482 #endif /* CFG_CONSOLE_INFO_QUIET */
484 #ifdef CFG_CONSOLE_ENV_OVERWRITE
485 /* set the environment variables (will overwrite previous env settings) */
486 for (i = 0; i < 3; i++) {
487 setenv (stdio_names[i], stdio_devices[i]->name);
489 #endif /* CFG_CONSOLE_ENV_OVERWRITE */
491 #if 0
492 /* If nothing usable installed, use only the initial console */
493 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
494 return (0);
495 #endif
496 return (0);
499 #else /* CFG_CONSOLE_IS_IN_ENV */
501 /* Called after the relocation - use desired console functions */
502 int console_init_r (void)
504 device_t *inputdev = NULL, *outputdev = NULL;
505 int i, items = ListNumItems (devlist);
507 #ifdef CONFIG_SPLASH_SCREEN
508 /* suppress all output if splash screen is enabled and we have
509 a bmp to display */
510 if (getenv("splashimage") != NULL)
511 gd->flags |= GD_FLG_SILENT;
512 #endif
514 /* Scan devices looking for input and output devices */
515 for (i = 1;
516 (i <= items) && ((inputdev == NULL) || (outputdev == NULL));
519 device_t *dev = ListGetPtrToItem (devlist, i);
521 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
522 inputdev = dev;
524 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
525 outputdev = dev;
529 /* Initializes output console first */
530 if (outputdev != NULL) {
531 console_setfile (stdout, outputdev);
532 console_setfile (stderr, outputdev);
535 /* Initializes input console */
536 if (inputdev != NULL) {
537 console_setfile (stdin, inputdev);
540 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
542 #ifndef CFG_CONSOLE_INFO_QUIET
543 /* Print information */
544 puts ("In: ");
545 if (stdio_devices[stdin] == NULL) {
546 puts ("No input devices available!\n");
547 } else {
548 printf ("%s\n", stdio_devices[stdin]->name);
551 puts ("Out: ");
552 if (stdio_devices[stdout] == NULL) {
553 puts ("No output devices available!\n");
554 } else {
555 printf ("%s\n", stdio_devices[stdout]->name);
558 puts ("Err: ");
559 if (stdio_devices[stderr] == NULL) {
560 puts ("No error devices available!\n");
561 } else {
562 printf ("%s\n", stdio_devices[stderr]->name);
564 #endif /* CFG_CONSOLE_INFO_QUIET */
566 /* Setting environment variables */
567 for (i = 0; i < 3; i++) {
568 setenv (stdio_names[i], stdio_devices[i]->name);
571 #if 0
572 /* If nothing usable installed, use only the initial console */
573 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
574 return (0);
575 #endif
577 return (0);
580 #endif /* CFG_CONSOLE_IS_IN_ENV */