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