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