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