console: add function console_devices_set
[platform/kernel/u-boot.git] / common / console.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000
4  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
5  */
6
7 #include <common.h>
8 #include <console.h>
9 #include <debug_uart.h>
10 #include <dm.h>
11 #include <env.h>
12 #include <stdarg.h>
13 #include <iomux.h>
14 #include <malloc.h>
15 #include <mapmem.h>
16 #include <os.h>
17 #include <serial.h>
18 #include <stdio_dev.h>
19 #include <exports.h>
20 #include <env_internal.h>
21 #include <watchdog.h>
22 #include <linux/delay.h>
23
24 DECLARE_GLOBAL_DATA_PTR;
25
26 static int on_console(const char *name, const char *value, enum env_op op,
27         int flags)
28 {
29         int console = -1;
30
31         /* Check for console redirection */
32         if (strcmp(name, "stdin") == 0)
33                 console = stdin;
34         else if (strcmp(name, "stdout") == 0)
35                 console = stdout;
36         else if (strcmp(name, "stderr") == 0)
37                 console = stderr;
38
39         /* if not actually setting a console variable, we don't care */
40         if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
41                 return 0;
42
43         switch (op) {
44         case env_op_create:
45         case env_op_overwrite:
46
47                 if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
48                         if (iomux_doenv(console, value))
49                                 return 1;
50                 } else {
51                         /* Try assigning specified device */
52                         if (console_assign(console, value) < 0)
53                                 return 1;
54                 }
55
56                 return 0;
57
58         case env_op_delete:
59                 if ((flags & H_FORCE) == 0)
60                         printf("Can't delete \"%s\"\n", name);
61                 return 1;
62
63         default:
64                 return 0;
65         }
66 }
67 U_BOOT_ENV_CALLBACK(console, on_console);
68
69 #ifdef CONFIG_SILENT_CONSOLE
70 static int on_silent(const char *name, const char *value, enum env_op op,
71         int flags)
72 {
73         if (!CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_SET))
74                 if (flags & H_INTERACTIVE)
75                         return 0;
76
77         if (!CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_RELOC))
78                 if ((flags & H_INTERACTIVE) == 0)
79                         return 0;
80
81         if (value != NULL)
82                 gd->flags |= GD_FLG_SILENT;
83         else
84                 gd->flags &= ~GD_FLG_SILENT;
85
86         return 0;
87 }
88 U_BOOT_ENV_CALLBACK(silent, on_silent);
89 #endif
90
91 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
92 /*
93  * if overwrite_console returns 1, the stdin, stderr and stdout
94  * are switched to the serial port, else the settings in the
95  * environment are used
96  */
97 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
98 extern int overwrite_console(void);
99 #define OVERWRITE_CONSOLE overwrite_console()
100 #else
101 #define OVERWRITE_CONSOLE 0
102 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
103
104 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
105
106 static int console_setfile(int file, struct stdio_dev * dev)
107 {
108         int error = 0;
109
110         if (dev == NULL)
111                 return -1;
112
113         switch (file) {
114         case stdin:
115         case stdout:
116         case stderr:
117                 /* Start new device */
118                 if (dev->start) {
119                         error = dev->start(dev);
120                         /* If it's not started dont use it */
121                         if (error < 0)
122                                 break;
123                 }
124
125                 /* Assign the new device (leaving the existing one started) */
126                 stdio_devices[file] = dev;
127
128                 /*
129                  * Update monitor functions
130                  * (to use the console stuff by other applications)
131                  */
132                 switch (file) {
133                 case stdin:
134                         gd->jt->getc = getchar;
135                         gd->jt->tstc = tstc;
136                         break;
137                 case stdout:
138                         gd->jt->putc  = putc;
139                         gd->jt->puts  = puts;
140                         gd->jt->printf = printf;
141                         break;
142                 }
143                 break;
144
145         default:                /* Invalid file ID */
146                 error = -1;
147         }
148         return error;
149 }
150
151 /**
152  * console_dev_is_serial() - Check if a stdio device is a serial device
153  *
154  * @sdev: Device to check
155  * @return true if this device is in the serial uclass (or for pre-driver-model,
156  * whether it is called "serial".
157  */
158 static bool console_dev_is_serial(struct stdio_dev *sdev)
159 {
160         bool is_serial;
161
162         if (IS_ENABLED(CONFIG_DM_SERIAL) && (sdev->flags & DEV_FLAGS_DM)) {
163                 struct udevice *dev = sdev->priv;
164
165                 is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL;
166         } else {
167                 is_serial = !strcmp(sdev->name, "serial");
168         }
169
170         return is_serial;
171 }
172
173 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
174 /** Console I/O multiplexing *******************************************/
175
176 static struct stdio_dev *tstcdev;
177 struct stdio_dev **console_devices[MAX_FILES];
178 int cd_count[MAX_FILES];
179
180 static void __maybe_unused console_devices_set(int file, struct stdio_dev *dev)
181 {
182         console_devices[file][0] = dev;
183 }
184
185 /*
186  * This depends on tstc() always being called before getchar().
187  * This is guaranteed to be true because this routine is called
188  * only from fgetc() which assures it.
189  * No attempt is made to demultiplex multiple input sources.
190  */
191 static int console_getc(int file)
192 {
193         unsigned char ret;
194
195         /* This is never called with testcdev == NULL */
196         ret = tstcdev->getc(tstcdev);
197         tstcdev = NULL;
198         return ret;
199 }
200
201 static int console_tstc(int file)
202 {
203         int i, ret;
204         struct stdio_dev *dev;
205         int prev;
206
207         prev = disable_ctrlc(1);
208         for (i = 0; i < cd_count[file]; i++) {
209                 dev = console_devices[file][i];
210                 if (dev->tstc != NULL) {
211                         ret = dev->tstc(dev);
212                         if (ret > 0) {
213                                 tstcdev = dev;
214                                 disable_ctrlc(prev);
215                                 return ret;
216                         }
217                 }
218         }
219         disable_ctrlc(prev);
220
221         return 0;
222 }
223
224 static void console_putc(int file, const char c)
225 {
226         int i;
227         struct stdio_dev *dev;
228
229         for (i = 0; i < cd_count[file]; i++) {
230                 dev = console_devices[file][i];
231                 if (dev->putc != NULL)
232                         dev->putc(dev, c);
233         }
234 }
235
236 /**
237  * console_puts_select() - Output a string to all console devices
238  *
239  * @file: File number to output to (e,g, stdout, see stdio.h)
240  * @serial_only: true to output only to serial, false to output to everything
241  *      else
242  * @s: String to output
243  */
244 static void console_puts_select(int file, bool serial_only, const char *s)
245 {
246         int i;
247         struct stdio_dev *dev;
248
249         for (i = 0; i < cd_count[file]; i++) {
250                 bool is_serial;
251
252                 dev = console_devices[file][i];
253                 is_serial = console_dev_is_serial(dev);
254                 if (dev->puts && serial_only == is_serial)
255                         dev->puts(dev, s);
256         }
257 }
258
259 void console_puts_select_stderr(bool serial_only, const char *s)
260 {
261         console_puts_select(stderr, serial_only, s);
262 }
263
264 static void console_puts(int file, const char *s)
265 {
266         int i;
267         struct stdio_dev *dev;
268
269         for (i = 0; i < cd_count[file]; i++) {
270                 dev = console_devices[file][i];
271                 if (dev->puts != NULL)
272                         dev->puts(dev, s);
273         }
274 }
275
276 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
277 static inline void console_doenv(int file, struct stdio_dev *dev)
278 {
279         iomux_doenv(file, dev->name);
280 }
281 #endif
282 #else
283
284 static void __maybe_unused console_devices_set(int file, struct stdio_dev *dev)
285 {
286 }
287
288 static inline int console_getc(int file)
289 {
290         return stdio_devices[file]->getc(stdio_devices[file]);
291 }
292
293 static inline int console_tstc(int file)
294 {
295         return stdio_devices[file]->tstc(stdio_devices[file]);
296 }
297
298 static inline void console_putc(int file, const char c)
299 {
300         stdio_devices[file]->putc(stdio_devices[file], c);
301 }
302
303 void console_puts_select(int file, bool serial_only, const char *s)
304 {
305         if (serial_only == console_dev_is_serial(stdio_devices[file]))
306                 stdio_devices[file]->puts(stdio_devices[file], s);
307 }
308
309 static inline void console_puts(int file, const char *s)
310 {
311         stdio_devices[file]->puts(stdio_devices[file], s);
312 }
313
314 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
315 static inline void console_doenv(int file, struct stdio_dev *dev)
316 {
317         console_setfile(file, dev);
318 }
319 #endif
320 #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
321
322 /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
323
324 int serial_printf(const char *fmt, ...)
325 {
326         va_list args;
327         uint i;
328         char printbuffer[CONFIG_SYS_PBSIZE];
329
330         va_start(args, fmt);
331
332         /* For this to work, printbuffer must be larger than
333          * anything we ever want to print.
334          */
335         i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
336         va_end(args);
337
338         serial_puts(printbuffer);
339         return i;
340 }
341
342 int fgetc(int file)
343 {
344         if (file < MAX_FILES) {
345                 /*
346                  * Effectively poll for input wherever it may be available.
347                  */
348                 for (;;) {
349                         WATCHDOG_RESET();
350 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
351                         /*
352                          * Upper layer may have already called tstc() so
353                          * check for that first.
354                          */
355                         if (tstcdev != NULL)
356                                 return console_getc(file);
357                         console_tstc(file);
358 #else
359                         if (console_tstc(file))
360                                 return console_getc(file);
361 #endif
362                         /*
363                          * If the watchdog must be rate-limited then it should
364                          * already be handled in board-specific code.
365                          */
366                         if (IS_ENABLED(CONFIG_WATCHDOG))
367                                 udelay(1);
368                 }
369         }
370
371         return -1;
372 }
373
374 int ftstc(int file)
375 {
376         if (file < MAX_FILES)
377                 return console_tstc(file);
378
379         return -1;
380 }
381
382 void fputc(int file, const char c)
383 {
384         if (file < MAX_FILES)
385                 console_putc(file, c);
386 }
387
388 void fputs(int file, const char *s)
389 {
390         if (file < MAX_FILES)
391                 console_puts(file, s);
392 }
393
394 int fprintf(int file, const char *fmt, ...)
395 {
396         va_list args;
397         uint i;
398         char printbuffer[CONFIG_SYS_PBSIZE];
399
400         va_start(args, fmt);
401
402         /* For this to work, printbuffer must be larger than
403          * anything we ever want to print.
404          */
405         i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
406         va_end(args);
407
408         /* Send to desired file */
409         fputs(file, printbuffer);
410         return i;
411 }
412
413 /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
414
415 int getchar(void)
416 {
417         if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
418                 return 0;
419
420         if (!gd->have_console)
421                 return 0;
422
423 #ifdef CONFIG_CONSOLE_RECORD
424         if (gd->console_in.start) {
425                 int ch;
426
427                 ch = membuff_getbyte((struct membuff *)&gd->console_in);
428                 if (ch != -1)
429                         return 1;
430         }
431 #endif
432         if (gd->flags & GD_FLG_DEVINIT) {
433                 /* Get from the standard input */
434                 return fgetc(stdin);
435         }
436
437         /* Send directly to the handler */
438         return serial_getc();
439 }
440
441 int tstc(void)
442 {
443         if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
444                 return 0;
445
446         if (!gd->have_console)
447                 return 0;
448 #ifdef CONFIG_CONSOLE_RECORD
449         if (gd->console_in.start) {
450                 if (membuff_peekbyte((struct membuff *)&gd->console_in) != -1)
451                         return 1;
452         }
453 #endif
454         if (gd->flags & GD_FLG_DEVINIT) {
455                 /* Test the standard input */
456                 return ftstc(stdin);
457         }
458
459         /* Send directly to the handler */
460         return serial_tstc();
461 }
462
463 #define PRE_CONSOLE_FLUSHPOINT1_SERIAL                  0
464 #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL   1
465
466 #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
467 #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
468
469 static void pre_console_putc(const char c)
470 {
471         char *buffer;
472
473         buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
474
475         buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
476
477         unmap_sysmem(buffer);
478 }
479
480 static void pre_console_puts(const char *s)
481 {
482         while (*s)
483                 pre_console_putc(*s++);
484 }
485
486 static void print_pre_console_buffer(int flushpoint)
487 {
488         unsigned long in = 0, out = 0;
489         char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
490         char *buf_in;
491
492         if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT))
493                 return;
494
495         buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
496         if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
497                 in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
498
499         while (in < gd->precon_buf_idx)
500                 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
501         unmap_sysmem(buf_in);
502
503         buf_out[out] = 0;
504
505         switch (flushpoint) {
506         case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
507                 puts(buf_out);
508                 break;
509         case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
510                 console_puts_select(stdout, false, buf_out);
511                 break;
512         }
513 }
514 #else
515 static inline void pre_console_putc(const char c) {}
516 static inline void pre_console_puts(const char *s) {}
517 static inline void print_pre_console_buffer(int flushpoint) {}
518 #endif
519
520 void putc(const char c)
521 {
522         if (!gd)
523                 return;
524 #ifdef CONFIG_CONSOLE_RECORD
525         if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
526                 membuff_putbyte((struct membuff *)&gd->console_out, c);
527 #endif
528         /* sandbox can send characters to stdout before it has a console */
529         if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
530                 os_putc(c);
531                 return;
532         }
533
534         /* if we don't have a console yet, use the debug UART */
535         if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
536                 printch(c);
537                 return;
538         }
539
540         if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
541                 if (!(gd->flags & GD_FLG_DEVINIT))
542                         pre_console_putc(c);
543                 return;
544         }
545
546         if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
547                 return;
548
549         if (!gd->have_console)
550                 return pre_console_putc(c);
551
552         if (gd->flags & GD_FLG_DEVINIT) {
553                 /* Send to the standard output */
554                 fputc(stdout, c);
555         } else {
556                 /* Send directly to the handler */
557                 pre_console_putc(c);
558                 serial_putc(c);
559         }
560 }
561
562 void puts(const char *s)
563 {
564         if (!gd)
565                 return;
566 #ifdef CONFIG_CONSOLE_RECORD
567         if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
568                 membuff_put((struct membuff *)&gd->console_out, s, strlen(s));
569 #endif
570         /* sandbox can send characters to stdout before it has a console */
571         if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
572                 os_puts(s);
573                 return;
574         }
575
576         if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
577                 while (*s) {
578                         int ch = *s++;
579
580                         printch(ch);
581                 }
582                 return;
583         }
584
585         if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
586                 if (!(gd->flags & GD_FLG_DEVINIT))
587                         pre_console_puts(s);
588                 return;
589         }
590
591         if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
592                 return;
593
594         if (!gd->have_console)
595                 return pre_console_puts(s);
596
597         if (gd->flags & GD_FLG_DEVINIT) {
598                 /* Send to the standard output */
599                 fputs(stdout, s);
600         } else {
601                 /* Send directly to the handler */
602                 pre_console_puts(s);
603                 serial_puts(s);
604         }
605 }
606
607 #ifdef CONFIG_CONSOLE_RECORD
608 int console_record_init(void)
609 {
610         int ret;
611
612         ret = membuff_new((struct membuff *)&gd->console_out,
613                           CONFIG_CONSOLE_RECORD_OUT_SIZE);
614         if (ret)
615                 return ret;
616         ret = membuff_new((struct membuff *)&gd->console_in,
617                           CONFIG_CONSOLE_RECORD_IN_SIZE);
618
619         return ret;
620 }
621
622 void console_record_reset(void)
623 {
624         membuff_purge((struct membuff *)&gd->console_out);
625         membuff_purge((struct membuff *)&gd->console_in);
626 }
627
628 int console_record_reset_enable(void)
629 {
630         console_record_reset();
631         gd->flags |= GD_FLG_RECORD;
632
633         return 0;
634 }
635
636 int console_record_readline(char *str, int maxlen)
637 {
638         return membuff_readline((struct membuff *)&gd->console_out, str,
639                                 maxlen, ' ');
640 }
641
642 int console_record_avail(void)
643 {
644         return membuff_avail((struct membuff *)&gd->console_out);
645 }
646
647 #endif
648
649 /* test if ctrl-c was pressed */
650 static int ctrlc_disabled = 0;  /* see disable_ctrl() */
651 static int ctrlc_was_pressed = 0;
652 int ctrlc(void)
653 {
654         if (!ctrlc_disabled && gd->have_console) {
655                 if (tstc()) {
656                         switch (getchar()) {
657                         case 0x03:              /* ^C - Control C */
658                                 ctrlc_was_pressed = 1;
659                                 return 1;
660                         default:
661                                 break;
662                         }
663                 }
664         }
665
666         return 0;
667 }
668 /* Reads user's confirmation.
669    Returns 1 if user's input is "y", "Y", "yes" or "YES"
670 */
671 int confirm_yesno(void)
672 {
673         int i;
674         char str_input[5];
675
676         /* Flush input */
677         while (tstc())
678                 getchar();
679         i = 0;
680         while (i < sizeof(str_input)) {
681                 str_input[i] = getchar();
682                 putc(str_input[i]);
683                 if (str_input[i] == '\r')
684                         break;
685                 i++;
686         }
687         putc('\n');
688         if (strncmp(str_input, "y\r", 2) == 0 ||
689             strncmp(str_input, "Y\r", 2) == 0 ||
690             strncmp(str_input, "yes\r", 4) == 0 ||
691             strncmp(str_input, "YES\r", 4) == 0)
692                 return 1;
693         return 0;
694 }
695 /* pass 1 to disable ctrlc() checking, 0 to enable.
696  * returns previous state
697  */
698 int disable_ctrlc(int disable)
699 {
700         int prev = ctrlc_disabled;      /* save previous state */
701
702         ctrlc_disabled = disable;
703         return prev;
704 }
705
706 int had_ctrlc (void)
707 {
708         return ctrlc_was_pressed;
709 }
710
711 void clear_ctrlc(void)
712 {
713         ctrlc_was_pressed = 0;
714 }
715
716 /** U-Boot INIT FUNCTIONS *************************************************/
717
718 struct stdio_dev *search_device(int flags, const char *name)
719 {
720         struct stdio_dev *dev;
721
722         dev = stdio_get_by_name(name);
723 #ifdef CONFIG_VIDCONSOLE_AS_LCD
724         if (!dev && !strcmp(name, CONFIG_VIDCONSOLE_AS_NAME))
725                 dev = stdio_get_by_name("vidconsole");
726 #endif
727
728         if (dev && (dev->flags & flags))
729                 return dev;
730
731         return NULL;
732 }
733
734 int console_assign(int file, const char *devname)
735 {
736         int flag;
737         struct stdio_dev *dev;
738
739         /* Check for valid file */
740         switch (file) {
741         case stdin:
742                 flag = DEV_FLAGS_INPUT;
743                 break;
744         case stdout:
745         case stderr:
746                 flag = DEV_FLAGS_OUTPUT;
747                 break;
748         default:
749                 return -1;
750         }
751
752         /* Check for valid device name */
753
754         dev = search_device(flag, devname);
755
756         if (dev)
757                 return console_setfile(file, dev);
758
759         return -1;
760 }
761
762 /* return true if the 'silent' flag is removed */
763 static bool console_update_silent(void)
764 {
765         unsigned long flags = gd->flags;
766
767         if (!IS_ENABLED(CONFIG_SILENT_CONSOLE))
768                 return false;
769
770         if (env_get("silent")) {
771                 gd->flags |= GD_FLG_SILENT;
772                 return false;
773         }
774
775         gd->flags &= ~GD_FLG_SILENT;
776
777         return !!(flags & GD_FLG_SILENT);
778 }
779
780 int console_announce_r(void)
781 {
782 #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
783         char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
784
785         display_options_get_banner(false, buf, sizeof(buf));
786
787         console_puts_select(stdout, false, buf);
788 #endif
789
790         return 0;
791 }
792
793 /* Called before relocation - use serial functions */
794 int console_init_f(void)
795 {
796         gd->have_console = 1;
797
798         console_update_silent();
799
800         print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
801
802         return 0;
803 }
804
805 void stdio_print_current_devices(void)
806 {
807         /* Print information */
808         puts("In:    ");
809         if (stdio_devices[stdin] == NULL) {
810                 puts("No input devices available!\n");
811         } else {
812                 printf ("%s\n", stdio_devices[stdin]->name);
813         }
814
815         puts("Out:   ");
816         if (stdio_devices[stdout] == NULL) {
817                 puts("No output devices available!\n");
818         } else {
819                 printf ("%s\n", stdio_devices[stdout]->name);
820         }
821
822         puts("Err:   ");
823         if (stdio_devices[stderr] == NULL) {
824                 puts("No error devices available!\n");
825         } else {
826                 printf ("%s\n", stdio_devices[stderr]->name);
827         }
828 }
829
830 #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
831 /* Called after the relocation - use desired console functions */
832 int console_init_r(void)
833 {
834         char *stdinname, *stdoutname, *stderrname;
835         struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
836         int i;
837         int iomux_err = 0;
838         int flushpoint;
839
840         /* update silent for env loaded from flash (initr_env) */
841         if (console_update_silent())
842                 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
843         else
844                 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
845
846         /* set default handlers at first */
847         gd->jt->getc  = serial_getc;
848         gd->jt->tstc  = serial_tstc;
849         gd->jt->putc  = serial_putc;
850         gd->jt->puts  = serial_puts;
851         gd->jt->printf = serial_printf;
852
853         /* stdin stdout and stderr are in environment */
854         /* scan for it */
855         stdinname  = env_get("stdin");
856         stdoutname = env_get("stdout");
857         stderrname = env_get("stderr");
858
859         if (OVERWRITE_CONSOLE == 0) {   /* if not overwritten by config switch */
860                 inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
861                 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
862                 errdev    = search_device(DEV_FLAGS_OUTPUT, stderrname);
863                 if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
864                         iomux_err = iomux_doenv(stdin, stdinname);
865                         iomux_err += iomux_doenv(stdout, stdoutname);
866                         iomux_err += iomux_doenv(stderr, stderrname);
867                         if (!iomux_err)
868                                 /* Successful, so skip all the code below. */
869                                 goto done;
870                 }
871         }
872         /* if the devices are overwritten or not found, use default device */
873         if (inputdev == NULL) {
874                 inputdev  = search_device(DEV_FLAGS_INPUT,  "serial");
875         }
876         if (outputdev == NULL) {
877                 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
878         }
879         if (errdev == NULL) {
880                 errdev    = search_device(DEV_FLAGS_OUTPUT, "serial");
881         }
882         /* Initializes output console first */
883         if (outputdev != NULL) {
884                 /* need to set a console if not done above. */
885                 console_doenv(stdout, outputdev);
886         }
887         if (errdev != NULL) {
888                 /* need to set a console if not done above. */
889                 console_doenv(stderr, errdev);
890         }
891         if (inputdev != NULL) {
892                 /* need to set a console if not done above. */
893                 console_doenv(stdin, inputdev);
894         }
895
896 done:
897
898         if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
899                 stdio_print_current_devices();
900
901 #ifdef CONFIG_VIDCONSOLE_AS_LCD
902         if (strstr(stdoutname, CONFIG_VIDCONSOLE_AS_NAME))
903                 printf("Warning: Please change '%s' to 'vidconsole' in stdout/stderr environment vars\n",
904                        CONFIG_VIDCONSOLE_AS_NAME);
905 #endif
906
907         if (IS_ENABLED(CONFIG_SYS_CONSOLE_ENV_OVERWRITE)) {
908                 /* set the environment variables (will overwrite previous env settings) */
909                 for (i = 0; i < MAX_FILES; i++)
910                         env_set(stdio_names[i], stdio_devices[i]->name);
911         }
912
913         gd->flags |= GD_FLG_DEVINIT;    /* device initialization completed */
914
915 #if 0
916         /* If nothing usable installed, use only the initial console */
917         if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
918                 return 0;
919 #endif
920         print_pre_console_buffer(flushpoint);
921         return 0;
922 }
923
924 #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
925
926 /* Called after the relocation - use desired console functions */
927 int console_init_r(void)
928 {
929         struct stdio_dev *inputdev = NULL, *outputdev = NULL;
930         int i;
931         struct list_head *list = stdio_get_list();
932         struct list_head *pos;
933         struct stdio_dev *dev;
934         int flushpoint;
935
936         /* update silent for env loaded from flash (initr_env) */
937         if (console_update_silent())
938                 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
939         else
940                 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
941
942         /*
943          * suppress all output if splash screen is enabled and we have
944          * a bmp to display. We redirect the output from frame buffer
945          * console to serial console in this case or suppress it if
946          * "silent" mode was requested.
947          */
948         if (IS_ENABLED(CONFIG_SPLASH_SCREEN) && env_get("splashimage")) {
949                 if (!(gd->flags & GD_FLG_SILENT))
950                         outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
951         }
952
953         /* Scan devices looking for input and output devices */
954         list_for_each(pos, list) {
955                 dev = list_entry(pos, struct stdio_dev, list);
956
957                 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
958                         inputdev = dev;
959                 }
960                 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
961                         outputdev = dev;
962                 }
963                 if(inputdev && outputdev)
964                         break;
965         }
966
967         /* Initializes output console first */
968         if (outputdev != NULL) {
969                 console_setfile(stdout, outputdev);
970                 console_setfile(stderr, outputdev);
971                 console_devices_set(stdout, outputdev);
972                 console_devices_set(stderr, outputdev);
973         }
974
975         /* Initializes input console */
976         if (inputdev != NULL) {
977                 console_setfile(stdin, inputdev);
978                 console_devices_set(stdin, inputdev);
979         }
980
981         if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
982                 stdio_print_current_devices();
983
984         /* Setting environment variables */
985         for (i = 0; i < MAX_FILES; i++) {
986                 env_set(stdio_names[i], stdio_devices[i]->name);
987         }
988
989         gd->flags |= GD_FLG_DEVINIT;    /* device initialization completed */
990
991 #if 0
992         /* If nothing usable installed, use only the initial console */
993         if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
994                 return 0;
995 #endif
996         print_pre_console_buffer(flushpoint);
997         return 0;
998 }
999
1000 #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */