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,
28 DECLARE_GLOBAL_DATA_PTR;
30 #ifdef CFG_CONSOLE_IS_IN_ENV
32 * if overwrite_console returns 1, the stdin, stderr and stdout
33 * are switched to the serial port, else the settings in the
34 * environment are used
36 #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
37 extern int overwrite_console(void);
38 #define OVERWRITE_CONSOLE overwrite_console()
40 #define OVERWRITE_CONSOLE 0
41 #endif /* CFG_CONSOLE_OVERWRITE_ROUTINE */
43 #endif /* CFG_CONSOLE_IS_IN_ENV */
45 static int console_setfile(int file, device_t * dev){
56 /* Start new device */
59 /* If it's not started dont use it */
65 /* Assign the new device (leaving the existing one started) */
66 stdio_devices[file] = dev;
69 * Update monitor functions
70 * (to use the console stuff by other applications)
74 gd->jt[XF_getc] = dev->getc;
75 gd->jt[XF_tstc] = dev->tstc;
78 gd->jt[XF_putc] = dev->putc;
79 gd->jt[XF_puts] = dev->puts;
80 gd->jt[XF_printf] = printf;
85 default: /* Invalid file ID */
91 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
93 void serial_printf(const char *fmt, ...){
96 char printbuffer[CFG_PBSIZE];
100 /* For this to work, printbuffer must be larger than
101 * anything we ever want to print.
103 //i = vsprintf(printbuffer, fmt, args);
104 vsprintf(printbuffer, fmt, args);
107 serial_puts(printbuffer);
111 if(file < MAX_FILES){
112 return(stdio_devices[file]->getc());
119 if(file < MAX_FILES){
120 return(stdio_devices[file]->tstc());
126 void fputc(int file, const char c){
127 if(file < MAX_FILES){
128 stdio_devices[file]->putc(c);
132 void fputs(int file, const char *s){
133 if(file < MAX_FILES){
134 stdio_devices[file]->puts(s);
138 void fprintf(int file, const char *fmt, ...){
141 char printbuffer[CFG_PBSIZE];
145 /* For this to work, printbuffer must be larger than
146 * anything we ever want to print.
148 //i = vsprintf(printbuffer, fmt, args);
149 vsprintf(printbuffer, fmt, args);
152 /* Send to desired file */
153 fputs(file, printbuffer);
156 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
159 if(gd->flags & GD_FLG_DEVINIT){
160 /* Get from the standard input */
161 return(fgetc(stdin));
164 /* Send directly to the handler */
165 return(serial_getc());
169 if(gd->flags & GD_FLG_DEVINIT){
170 /* Test the standard input */
171 return(ftstc(stdin));
174 /* Send directly to the handler */
175 return(serial_tstc());
178 void putc(const char c){
179 #ifdef CONFIG_SILENT_CONSOLE
180 if(gd->flags & GD_FLG_SILENT){
185 if(gd->flags & GD_FLG_DEVINIT){
186 /* Send to the standard output */
189 /* Send directly to the handler */
194 void puts(const char *s){
195 #ifdef CONFIG_SILENT_CONSOLE
196 if(gd->flags & GD_FLG_SILENT){
201 if(gd->flags & GD_FLG_DEVINIT){
202 /* Send to the standard output */
205 /* Send directly to the handler */
210 void printf(const char *fmt, ...){
213 char printbuffer[CFG_PBSIZE];
217 /* For this to work, printbuffer must be larger than
218 * anything we ever want to print.
220 //i = vsprintf(printbuffer, fmt, args);
221 vsprintf(printbuffer, fmt, args);
224 /* Print the string */
228 void vprintf(const char *fmt, va_list args){
230 char printbuffer[CFG_PBSIZE];
232 /* For this to work, printbuffer must be larger than
233 * anything we ever want to print.
235 //i = vsprintf(printbuffer, fmt, args);
236 vsprintf(printbuffer, fmt, args);
238 /* Print the string */
242 /* test if ctrl-c was pressed */
243 static int ctrlc_disabled = 0; /* see disable_ctrl() */
244 static int ctrlc_was_pressed = 0;
246 if(!ctrlc_disabled && gd->have_console){
249 case 0x03: /* ^C - Control C */
250 ctrlc_was_pressed = 1;
260 /* pass 1 to disable ctrlc() checking, 0 to enable.
261 * returns previous state
263 int disable_ctrlc(int disable){
264 int prev = ctrlc_disabled; /* save previous state */
266 ctrlc_disabled = disable;
271 return(ctrlc_was_pressed);
274 void clear_ctrlc(void){
275 ctrlc_was_pressed = 0;
279 inline void dbg(const char *fmt, ...){
283 /** U-Boot INIT FUNCTIONS *************************************************/
285 int console_assign(int file, char *devname){
288 /* Check for valid file */
291 flag = DEV_FLAGS_INPUT;
295 flag = DEV_FLAGS_OUTPUT;
301 /* Check for valid device name */
303 for(i = 1; i <= ListNumItems(devlist); i++){
304 device_t *dev = ListGetPtrToItem(devlist, i);
306 if(strcmp(devname, dev->name) == 0){
307 if(dev->flags & flag){
308 return(console_setfile(file, dev));
318 /* Called before relocation - use serial functions */
319 int console_init_f(void){
320 gd->have_console = 1;
322 #ifdef CONFIG_SILENT_CONSOLE
323 if(getenv("silent") != NULL){
324 gd->flags |= GD_FLG_SILENT;
331 #if defined(CFG_CONSOLE_IS_IN_ENV) || defined(CONFIG_SILENT_CONSOLE)
332 /* search a device */
333 device_t *search_device(int flags, char *name){
335 device_t *dev = NULL;
337 items = ListNumItems(devlist);
343 for(i = 1; i <= items; i++){
344 dev = ListGetPtrToItem(devlist, i);
345 if((dev->flags & flags) && (strcmp(name, dev->name) == 0)){
351 #endif /* CFG_CONSOLE_IS_IN_ENV || CONFIG_SILENT_CONSOLE */
353 #ifdef CFG_CONSOLE_IS_IN_ENV
354 /* Called after the relocation - use desired console functions */
355 int console_init_r(void){
356 char *stdinname, *stdoutname, *stderrname;
357 device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
358 #ifdef CFG_CONSOLE_ENV_OVERWRITE
360 #endif /* CFG_CONSOLE_ENV_OVERWRITE */
362 /* set default handlers at first */
363 gd->jt[XF_getc] = serial_getc;
364 gd->jt[XF_tstc] = serial_tstc;
365 gd->jt[XF_putc] = serial_putc;
366 gd->jt[XF_puts] = serial_puts;
367 gd->jt[XF_printf] = serial_printf;
369 /* stdin stdout and stderr are in environment */
371 stdinname = getenv("stdin");
372 stdoutname = getenv("stdout");
373 stderrname = getenv("stderr");
375 if(OVERWRITE_CONSOLE == 0){ /* if not overwritten by config switch */
376 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
377 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
378 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
380 /* if the devices are overwritten or not found, use default device */
381 if(inputdev == NULL){
382 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
384 if(outputdev == NULL){
385 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
388 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
390 /* Initializes output console first */
391 if(outputdev != NULL){
392 console_setfile(stdout, outputdev);
395 console_setfile(stderr, errdev);
397 if(inputdev != NULL){
398 console_setfile(stdin, inputdev);
401 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
403 #ifndef CFG_CONSOLE_INFO_QUIET
404 /* Print information */
406 if(stdio_devices[stdin] == NULL){
407 puts("No input devices available!\n");
409 printf("%s\n", stdio_devices[stdin]->name);
413 if(stdio_devices[stdout] == NULL){
414 puts("No output devices available!\n");
416 printf("%s\n", stdio_devices[stdout]->name);
420 if(stdio_devices[stderr] == NULL){
421 puts("No error devices available!\n");
423 printf("%s\n", stdio_devices[stderr]->name);
425 #endif /* CFG_CONSOLE_INFO_QUIET */
427 #ifdef CFG_CONSOLE_ENV_OVERWRITE
428 /* set the environment variables (will overwrite previous env settings) */
429 for(i = 0; i < 3; i++){
430 setenv(stdio_names[i], stdio_devices[i]->name);
432 #endif /* CFG_CONSOLE_ENV_OVERWRITE */
436 #else /* CFG_CONSOLE_IS_IN_ENV */
438 /* Called after the relocation - use desired console functions */
439 int console_init_r(void){
440 device_t *inputdev = NULL, *outputdev = NULL;
441 int i, items = ListNumItems(devlist);
443 #ifdef CONFIG_SILENT_CONSOLE
444 /* Suppress all output if "silent" mode requested */
445 if(gd->flags & GD_FLG_SILENT){
446 outputdev = search_device(DEV_FLAGS_OUTPUT, "nulldev");
450 /* Scan devices looking for input and output devices */
451 for(i = 1; (i <= items) && ((inputdev == NULL) || (outputdev == NULL)); i++){
452 device_t *dev = ListGetPtrToItem(devlist, i);
454 if((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)){
457 if((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)){
462 /* Initializes output console first */
463 if(outputdev != NULL){
464 console_setfile(stdout, outputdev);
465 console_setfile(stderr, outputdev);
468 /* Initializes input console */
469 if(inputdev != NULL){
470 console_setfile(stdin, inputdev);
473 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
475 #ifndef CFG_CONSOLE_INFO_QUIET
476 /* Print information */
478 if(stdio_devices[stdin] == NULL){
479 puts("No input devices available!\n");
481 printf("%s\n", stdio_devices[stdin]->name);
485 if(stdio_devices[stdout] == NULL){
486 puts("No output devices available!\n");
488 printf("%s\n", stdio_devices[stdout]->name);
492 if(stdio_devices[stderr] == NULL){
493 puts("No error devices available!\n");
495 printf("%s\n", stdio_devices[stderr]->name);
497 #endif /* CFG_CONSOLE_INFO_QUIET */
499 /* Setting environment variables */
500 for(i = 0; i < 3; i++){
501 setenv(stdio_names[i], stdio_devices[i]->name);
507 #endif /* CFG_CONSOLE_IS_IN_ENV */