4ff24294ba754c59b31e10caa00a27bece8887d7
[u-boot.git] / common / console.c
1 /*
2  * (C) Copyright 2000
3  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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
22  */
23
24 #include <common.h>
25 #include <stdarg.h>
26 #include <malloc.h>
27 #include <console.h>
28 #include <exports.h>
29
30 #ifdef CONFIG_AMIGAONEG3SE
31 int console_changed = 0;
32 #endif
33
34 #ifdef CFG_CONSOLE_IS_IN_ENV
35 /*
36  * if overwrite_console returns 1, the stdin, stderr and stdout
37  * are switched to the serial port, else the settings in the
38  * environment are used
39  */
40 #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
41 extern int overwrite_console (void);
42 #define OVERWRITE_CONSOLE overwrite_console ()
43 #else
44 #define OVERWRITE_CONSOLE 0
45 #endif /* CFG_CONSOLE_OVERWRITE_ROUTINE */
46
47 #endif /* CFG_CONSOLE_IS_IN_ENV */
48
49 int console_setfile (int file, device_t * dev)
50 {
51         DECLARE_GLOBAL_DATA_PTR;
52         int error = 0;
53
54         if (dev == NULL)
55                 return -1;
56
57         switch (file) {
58         case stdin:
59         case stdout:
60         case stderr:
61                 /* Start new device */
62                 if (dev->start) {
63                         error = dev->start ();
64                         /* If it's not started dont use it */
65                         if (error < 0)
66                                 break;
67                 }
68
69                 /* Assign the new device (leaving the existing one started) */
70                 stdio_devices[file] = dev;
71
72                 /*
73                  * Update monitor functions
74                  * (to use the console stuff by other applications)
75                  */
76                 switch (file) {
77                 case stdin:
78                         gd->jt[XF_getc] = dev->getc;
79                         gd->jt[XF_tstc] = dev->tstc;
80                         break;
81                 case stdout:
82                         gd->jt[XF_putc] = dev->putc;
83                         gd->jt[XF_puts] = dev->puts;
84                         gd->jt[XF_printf] = printf;
85                         break;
86                 }
87                 break;
88
89         default:                /* Invalid file ID */
90                 error = -1;
91         }
92         return error;
93 }
94
95 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
96
97 void serial_printf (const char *fmt, ...)
98 {
99         va_list args;
100         uint i;
101         char printbuffer[CFG_PBSIZE];
102
103         va_start (args, fmt);
104
105         /* For this to work, printbuffer must be larger than
106          * anything we ever want to print.
107          */
108         i = vsprintf (printbuffer, fmt, args);
109         va_end (args);
110
111         serial_puts (printbuffer);
112 }
113
114 int fgetc (int file)
115 {
116         if (file < MAX_FILES)
117                 return stdio_devices[file]->getc ();
118
119         return -1;
120 }
121
122 int ftstc (int file)
123 {
124         if (file < MAX_FILES)
125                 return stdio_devices[file]->tstc ();
126
127         return -1;
128 }
129
130 void fputc (int file, const char c)
131 {
132         if (file < MAX_FILES)
133                 stdio_devices[file]->putc (c);
134 }
135
136 void fputs (int file, const char *s)
137 {
138         if (file < MAX_FILES)
139                 stdio_devices[file]->puts (s);
140 }
141
142 void fprintf (int file, const char *fmt, ...)
143 {
144         va_list args;
145         uint i;
146         char printbuffer[CFG_PBSIZE];
147
148         va_start (args, fmt);
149
150         /* For this to work, printbuffer must be larger than
151          * anything we ever want to print.
152          */
153         i = vsprintf (printbuffer, fmt, args);
154         va_end (args);
155
156         /* Send to desired file */
157         fputs (file, printbuffer);
158 }
159
160 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
161
162 int getc (void)
163 {
164         DECLARE_GLOBAL_DATA_PTR;
165
166         if (gd->flags & GD_FLG_DEVINIT) {
167                 /* Get from the standard input */
168                 return fgetc (stdin);
169         }
170
171         /* Send directly to the handler */
172         return serial_getc ();
173 }
174
175 int tstc (void)
176 {
177         DECLARE_GLOBAL_DATA_PTR;
178
179         if (gd->flags & GD_FLG_DEVINIT) {
180                 /* Test the standard input */
181                 return ftstc (stdin);
182         }
183
184         /* Send directly to the handler */
185         return serial_tstc ();
186 }
187
188 void putc (const char c)
189 {
190         DECLARE_GLOBAL_DATA_PTR;
191
192 #ifdef CONFIG_SILENT_CONSOLE
193         if (gd->flags & GD_FLG_SILENT)
194                 return;
195 #endif
196
197         if (gd->flags & GD_FLG_DEVINIT) {
198                 /* Send to the standard output */
199                 fputc (stdout, c);
200         } else {
201                 /* Send directly to the handler */
202                 serial_putc (c);
203         }
204 }
205
206 void puts (const char *s)
207 {
208         DECLARE_GLOBAL_DATA_PTR;
209
210 #ifdef CONFIG_SILENT_CONSOLE
211         if (gd->flags & GD_FLG_SILENT)
212                 return;
213 #endif
214
215         if (gd->flags & GD_FLG_DEVINIT) {
216                 /* Send to the standard output */
217                 fputs (stdout, s);
218         } else {
219                 /* Send directly to the handler */
220                 serial_puts (s);
221         }
222 }
223
224 void printf (const char *fmt, ...)
225 {
226         va_list args;
227         uint i;
228         char printbuffer[CFG_PBSIZE];
229
230         va_start (args, fmt);
231
232         /* For this to work, printbuffer must be larger than
233          * anything we ever want to print.
234          */
235         i = vsprintf (printbuffer, fmt, args);
236         va_end (args);
237
238         /* Print the string */
239         puts (printbuffer);
240 }
241
242 void vprintf (const char *fmt, va_list args)
243 {
244         uint i;
245         char printbuffer[CFG_PBSIZE];
246
247         /* For this to work, printbuffer must be larger than
248          * anything we ever want to print.
249          */
250         i = vsprintf (printbuffer, fmt, args);
251
252         /* Print the string */
253         puts (printbuffer);
254 }
255
256 /* test if ctrl-c was pressed */
257 static int ctrlc_disabled = 0;  /* see disable_ctrl() */
258 static int ctrlc_was_pressed = 0;
259 int ctrlc (void)
260 {
261         DECLARE_GLOBAL_DATA_PTR;
262
263         if (!ctrlc_disabled && gd->have_console) {
264                 if (tstc ()) {
265                         switch (getc ()) {
266                         case 0x03:              /* ^C - Control C */
267                                 ctrlc_was_pressed = 1;
268                                 return 1;
269                         default:
270                                 break;
271                         }
272                 }
273         }
274         return 0;
275 }
276
277 /* pass 1 to disable ctrlc() checking, 0 to enable.
278  * returns previous state
279  */
280 int disable_ctrlc (int disable)
281 {
282         int prev = ctrlc_disabled;      /* save previous state */
283
284         ctrlc_disabled = disable;
285         return prev;
286 }
287
288 int had_ctrlc (void)
289 {
290         return ctrlc_was_pressed;
291 }
292
293 void clear_ctrlc (void)
294 {
295         ctrlc_was_pressed = 0;
296 }
297
298 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
299 char    screen[1024];
300 char *cursor = screen;
301 int once = 0;
302 inline void dbg(const char *fmt, ...)
303 {
304         va_list args;
305         uint    i;
306         char    printbuffer[CFG_PBSIZE];
307
308         if (!once) {
309                 memset(screen, 0, sizeof(screen));
310                 once++;
311         }
312
313         va_start(args, fmt);
314
315         /* For this to work, printbuffer must be larger than
316          * anything we ever want to print.
317          */
318         i = vsprintf(printbuffer, fmt, args);
319         va_end(args);
320
321         if ((screen + sizeof(screen) - 1 - cursor) < strlen(printbuffer)+1) {
322                 memset(screen, 0, sizeof(screen));
323                 cursor = screen;
324         }
325         sprintf(cursor, printbuffer);
326         cursor += strlen(printbuffer);
327
328 }
329 #else
330 inline void dbg(const char *fmt, ...)
331 {
332 }
333 #endif
334
335 /** U-Boot INIT FUNCTIONS *************************************************/
336
337 int console_assign (int file, char *devname)
338 {
339         int flag, i;
340
341         /* Check for valid file */
342         switch (file) {
343         case stdin:
344                 flag = DEV_FLAGS_INPUT;
345                 break;
346         case stdout:
347         case stderr:
348                 flag = DEV_FLAGS_OUTPUT;
349                 break;
350         default:
351                 return -1;
352         }
353
354         /* Check for valid device name */
355
356         for (i = 1; i <= ListNumItems (devlist); i++) {
357                 device_t *dev = ListGetPtrToItem (devlist, i);
358
359                 if (strcmp (devname, dev->name) == 0) {
360                         if (dev->flags & flag)
361                                 return console_setfile (file, dev);
362
363                         return -1;
364                 }
365         }
366
367         return -1;
368 }
369
370 /* Called before relocation - use serial functions */
371 int console_init_f (void)
372 {
373         DECLARE_GLOBAL_DATA_PTR;
374
375         gd->have_console = 1;
376
377 #ifdef CONFIG_SILENT_CONSOLE
378         if (getenv("silent") != NULL)
379                 gd->flags |= GD_FLG_SILENT;
380 #endif
381
382         return (0);
383 }
384
385 #if defined(CFG_CONSOLE_IS_IN_ENV) || defined(CONFIG_SPLASH_SCREEN) || defined(CONFIG_SILENT_CONSOLE)
386 /* search a device */
387 device_t *search_device (int flags, char *name)
388 {
389         int i, items;
390         device_t *dev = NULL;
391
392         items = ListNumItems (devlist);
393         if (name == NULL)
394                 return dev;
395
396         for (i = 1; i <= items; i++) {
397                 dev = ListGetPtrToItem (devlist, i);
398                 if ((dev->flags & flags) && (strcmp (name, dev->name) == 0)) {
399                         break;
400                 }
401         }
402         return dev;
403 }
404 #endif /* CFG_CONSOLE_IS_IN_ENV || CONFIG_SPLASH_SCREEN */
405
406 #ifdef CFG_CONSOLE_IS_IN_ENV
407 /* Called after the relocation - use desired console functions */
408 int console_init_r (void)
409 {
410         DECLARE_GLOBAL_DATA_PTR;
411         char *stdinname, *stdoutname, *stderrname;
412         device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
413 #ifdef CFG_CONSOLE_ENV_OVERWRITE
414         int i;
415 #endif /* CFG_CONSOLE_ENV_OVERWRITE */
416
417         /* set default handlers at first */
418         gd->jt[XF_getc] = serial_getc;
419         gd->jt[XF_tstc] = serial_tstc;
420         gd->jt[XF_putc] = serial_putc;
421         gd->jt[XF_puts] = serial_puts;
422         gd->jt[XF_printf] = serial_printf;
423
424         /* stdin stdout and stderr are in environment */
425         /* scan for it */
426         stdinname  = getenv ("stdin");
427         stdoutname = getenv ("stdout");
428         stderrname = getenv ("stderr");
429
430         if (OVERWRITE_CONSOLE == 0) {   /* if not overwritten by config switch */
431                 inputdev  = search_device (DEV_FLAGS_INPUT,  stdinname);
432                 outputdev = search_device (DEV_FLAGS_OUTPUT, stdoutname);
433                 errdev    = search_device (DEV_FLAGS_OUTPUT, stderrname);
434         }
435         /* if the devices are overwritten or not found, use default device */
436         if (inputdev == NULL) {
437                 inputdev  = search_device (DEV_FLAGS_INPUT,  "serial");
438         }
439         if (outputdev == NULL) {
440                 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
441         }
442         if (errdev == NULL) {
443                 errdev    = search_device (DEV_FLAGS_OUTPUT, "serial");
444         }
445         /* Initializes output console first */
446         if (outputdev != NULL) {
447                 console_setfile (stdout, outputdev);
448         }
449         if (errdev != NULL) {
450                 console_setfile (stderr, errdev);
451         }
452         if (inputdev != NULL) {
453                 console_setfile (stdin, inputdev);
454         }
455
456         gd->flags |= GD_FLG_DEVINIT;    /* device initialization completed */
457
458 #ifndef CFG_CONSOLE_INFO_QUIET
459         if (strcmp(stdio_devices[stdout]->name, "serial")) {
460                 extern char version_string[];
461                 printf ("\n%s\n", version_string);
462         }
463
464         /* Print information */
465         puts ("stdin :   ");
466         if (stdio_devices[stdin] == NULL) {
467                 puts ("No input devices available!\n");
468         } else {
469                 printf ("%s\n", stdio_devices[stdin]->name);
470         }
471
472         puts ("stdout:   ");
473         if (stdio_devices[stdout] == NULL) {
474                 puts ("No output devices available!\n");
475         } else {
476                 printf ("%s\n", stdio_devices[stdout]->name);
477         }
478
479         puts ("stderr:   ");
480         if (stdio_devices[stderr] == NULL) {
481                 puts ("No error devices available!\n");
482         } else {
483                 printf ("%s\n", stdio_devices[stderr]->name);
484         }
485 #endif /* CFG_CONSOLE_INFO_QUIET */
486
487 #ifdef CFG_CONSOLE_ENV_OVERWRITE
488         /* set the environment variables (will overwrite previous env settings) */
489         for (i = 0; i < 3; i++) {
490                 setenv (stdio_names[i], stdio_devices[i]->name);
491         }
492 #endif /* CFG_CONSOLE_ENV_OVERWRITE */
493
494 #if 0
495         /* If nothing usable installed, use only the initial console */
496         if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
497                 return (0);
498 #endif
499         return (0);
500 }
501
502 #else /* CFG_CONSOLE_IS_IN_ENV */
503
504 /* Called after the relocation - use desired console functions */
505 int console_init_r (void)
506 {
507         DECLARE_GLOBAL_DATA_PTR;
508
509         device_t *inputdev = NULL, *outputdev = NULL;
510         int i, items = ListNumItems (devlist);
511
512 #ifdef CONFIG_SPLASH_SCREEN
513         /* suppress all output if splash screen is enabled and we have
514            a bmp to display                                            */
515         if (getenv("splashimage") != NULL)
516                 outputdev = search_device (DEV_FLAGS_OUTPUT, "nulldev");
517 #endif
518
519 #ifdef CONFIG_SILENT_CONSOLE
520         /* Suppress all output if "silent" mode requested               */
521         if (gd->flags & GD_FLG_SILENT)
522                 outputdev = search_device (DEV_FLAGS_OUTPUT, "nulldev");
523 #endif
524
525         /* Scan devices looking for input and output devices */
526         for (i = 1;
527              (i <= items) && ((inputdev == NULL) || (outputdev == NULL));
528              i++
529             ) {
530                 device_t *dev = ListGetPtrToItem (devlist, i);
531
532                 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
533                         inputdev = dev;
534                 }
535                 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
536                         outputdev = dev;
537                 }
538         }
539
540         /* Initializes output console first */
541         if (outputdev != NULL) {
542                 console_setfile (stdout, outputdev);
543                 console_setfile (stderr, outputdev);
544         }
545
546         /* Initializes input console */
547         if (inputdev != NULL) {
548                 console_setfile (stdin, inputdev);
549         }
550
551         gd->flags |= GD_FLG_DEVINIT;    /* device initialization completed */
552
553 #ifndef CFG_CONSOLE_INFO_QUIET
554         /* Print information */
555         puts ("In:    ");
556         if (stdio_devices[stdin] == NULL) {
557                 puts ("No input devices available!\n");
558         } else {
559                 printf ("%s\n", stdio_devices[stdin]->name);
560         }
561
562         puts ("Out:   ");
563         if (stdio_devices[stdout] == NULL) {
564                 puts ("No output devices available!\n");
565         } else {
566                 printf ("%s\n", stdio_devices[stdout]->name);
567         }
568
569         puts ("Err:   ");
570         if (stdio_devices[stderr] == NULL) {
571                 puts ("No error devices available!\n");
572         } else {
573                 printf ("%s\n", stdio_devices[stderr]->name);
574         }
575 #endif /* CFG_CONSOLE_INFO_QUIET */
576
577         /* Setting environment variables */
578         for (i = 0; i < 3; i++) {
579                 setenv (stdio_names[i], stdio_devices[i]->name);
580         }
581
582 #if 0
583         /* If nothing usable installed, use only the initial console */
584         if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
585                 return (0);
586 #endif
587
588         return (0);
589 }
590
591 #endif /* CFG_CONSOLE_IS_IN_ENV */