blob: bc37b6d962b0aa9eb2032a63b5d5c2044c329cb7 [file] [log] [blame]
wdenk47d1a6e2002-11-03 00:01:44 +00001/*
2 * (C) Copyright 2000
3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk47d1a6e2002-11-03 00:01:44 +00006 */
7
8#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -07009#include <console.h>
Simon Glass7dd27e02015-06-23 15:38:33 -060010#include <debug_uart.h>
wdenk47d1a6e2002-11-03 00:01:44 +000011#include <stdarg.h>
Jeroen Hofstee3b46b3b2014-10-08 22:57:48 +020012#include <iomux.h>
wdenk47d1a6e2002-11-03 00:01:44 +000013#include <malloc.h>
Simon Glass3e9fd242013-11-10 10:27:01 -070014#include <os.h>
Joe Hershberger5c89d6d2012-12-11 22:16:29 -060015#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020016#include <stdio_dev.h>
wdenk874ac262003-07-24 23:38:38 +000017#include <exports.h>
Joe Hershberger5c89d6d2012-12-11 22:16:29 -060018#include <environment.h>
wdenk47d1a6e2002-11-03 00:01:44 +000019
Wolfgang Denk6405a152006-03-31 18:32:53 +020020DECLARE_GLOBAL_DATA_PTR;
21
Joe Hershberger5c89d6d2012-12-11 22:16:29 -060022static int on_console(const char *name, const char *value, enum env_op op,
23 int flags)
24{
25 int console = -1;
26
27 /* Check for console redirection */
28 if (strcmp(name, "stdin") == 0)
29 console = stdin;
30 else if (strcmp(name, "stdout") == 0)
31 console = stdout;
32 else if (strcmp(name, "stderr") == 0)
33 console = stderr;
34
35 /* if not actually setting a console variable, we don't care */
36 if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
37 return 0;
38
39 switch (op) {
40 case env_op_create:
41 case env_op_overwrite:
42
43#ifdef CONFIG_CONSOLE_MUX
44 if (iomux_doenv(console, value))
45 return 1;
46#else
47 /* Try assigning specified device */
48 if (console_assign(console, value) < 0)
49 return 1;
50#endif /* CONFIG_CONSOLE_MUX */
51 return 0;
52
53 case env_op_delete:
54 if ((flags & H_FORCE) == 0)
55 printf("Can't delete \"%s\"\n", name);
56 return 1;
57
58 default:
59 return 0;
60 }
61}
62U_BOOT_ENV_CALLBACK(console, on_console);
63
Joe Hershberger03ddaba2012-12-11 22:16:30 -060064#ifdef CONFIG_SILENT_CONSOLE
65static int on_silent(const char *name, const char *value, enum env_op op,
66 int flags)
67{
68#ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
69 if (flags & H_INTERACTIVE)
70 return 0;
71#endif
72#ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
73 if ((flags & H_INTERACTIVE) == 0)
74 return 0;
75#endif
76
77 if (value != NULL)
78 gd->flags |= GD_FLG_SILENT;
79 else
80 gd->flags &= ~GD_FLG_SILENT;
81
82 return 0;
83}
84U_BOOT_ENV_CALLBACK(silent, on_silent);
85#endif
86
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020087#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +000088/*
89 * if overwrite_console returns 1, the stdin, stderr and stdout
90 * are switched to the serial port, else the settings in the
91 * environment are used
92 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020093#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +010094extern int overwrite_console(void);
95#define OVERWRITE_CONSOLE overwrite_console()
wdenk47d1a6e2002-11-03 00:01:44 +000096#else
wdenk61c636e2005-03-31 18:42:15 +000097#define OVERWRITE_CONSOLE 0
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020098#endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
wdenk47d1a6e2002-11-03 00:01:44 +000099
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200100#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +0000101
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200102static int console_setfile(int file, struct stdio_dev * dev)
wdenk47d1a6e2002-11-03 00:01:44 +0000103{
104 int error = 0;
105
106 if (dev == NULL)
107 return -1;
108
109 switch (file) {
110 case stdin:
111 case stdout:
112 case stderr:
113 /* Start new device */
114 if (dev->start) {
Simon Glass0d1e1f72014-07-23 06:54:59 -0600115 error = dev->start(dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000116 /* If it's not started dont use it */
117 if (error < 0)
118 break;
119 }
120
121 /* Assign the new device (leaving the existing one started) */
122 stdio_devices[file] = dev;
123
124 /*
125 * Update monitor functions
126 * (to use the console stuff by other applications)
127 */
128 switch (file) {
129 case stdin:
Martin Dorwigcb2c2862015-01-26 15:22:54 -0700130 gd->jt->getc = getc;
131 gd->jt->tstc = tstc;
wdenk47d1a6e2002-11-03 00:01:44 +0000132 break;
133 case stdout:
Martin Dorwigcb2c2862015-01-26 15:22:54 -0700134 gd->jt->putc = putc;
135 gd->jt->puts = puts;
136 gd->jt->printf = printf;
wdenk47d1a6e2002-11-03 00:01:44 +0000137 break;
138 }
139 break;
140
141 default: /* Invalid file ID */
142 error = -1;
143 }
144 return error;
145}
146
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100147#if defined(CONFIG_CONSOLE_MUX)
148/** Console I/O multiplexing *******************************************/
149
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200150static struct stdio_dev *tstcdev;
151struct stdio_dev **console_devices[MAX_FILES];
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100152int cd_count[MAX_FILES];
153
154/*
155 * This depends on tstc() always being called before getc().
156 * This is guaranteed to be true because this routine is called
157 * only from fgetc() which assures it.
158 * No attempt is made to demultiplex multiple input sources.
159 */
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100160static int console_getc(int file)
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100161{
162 unsigned char ret;
163
164 /* This is never called with testcdev == NULL */
Simon Glass0d1e1f72014-07-23 06:54:59 -0600165 ret = tstcdev->getc(tstcdev);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100166 tstcdev = NULL;
167 return ret;
168}
169
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100170static int console_tstc(int file)
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100171{
172 int i, ret;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200173 struct stdio_dev *dev;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100174
175 disable_ctrlc(1);
176 for (i = 0; i < cd_count[file]; i++) {
177 dev = console_devices[file][i];
178 if (dev->tstc != NULL) {
Simon Glass0d1e1f72014-07-23 06:54:59 -0600179 ret = dev->tstc(dev);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100180 if (ret > 0) {
181 tstcdev = dev;
182 disable_ctrlc(0);
183 return ret;
184 }
185 }
186 }
187 disable_ctrlc(0);
188
189 return 0;
190}
191
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100192static void console_putc(int file, const char c)
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100193{
194 int i;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200195 struct stdio_dev *dev;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100196
197 for (i = 0; i < cd_count[file]; i++) {
198 dev = console_devices[file][i];
199 if (dev->putc != NULL)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600200 dev->putc(dev, c);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100201 }
202}
203
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200204#ifdef CONFIG_PRE_CONSOLE_BUFFER
Hans de Goedee355da02015-05-05 13:13:36 +0200205static void console_puts_noserial(int file, const char *s)
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200206{
207 int i;
208 struct stdio_dev *dev;
209
210 for (i = 0; i < cd_count[file]; i++) {
211 dev = console_devices[file][i];
Hans de Goedee355da02015-05-05 13:13:36 +0200212 if (dev->puts != NULL && strcmp(dev->name, "serial") != 0)
213 dev->puts(dev, s);
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200214 }
215}
216#endif
217
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100218static void console_puts(int file, const char *s)
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100219{
220 int i;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200221 struct stdio_dev *dev;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100222
223 for (i = 0; i < cd_count[file]; i++) {
224 dev = console_devices[file][i];
225 if (dev->puts != NULL)
Simon Glass0d1e1f72014-07-23 06:54:59 -0600226 dev->puts(dev, s);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100227 }
228}
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100229
230static inline void console_printdevs(int file)
231{
232 iomux_printdevs(file);
233}
234
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200235static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100236{
237 iomux_doenv(file, dev->name);
238}
239#else
240static inline int console_getc(int file)
241{
Simon Glass0d1e1f72014-07-23 06:54:59 -0600242 return stdio_devices[file]->getc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100243}
244
245static inline int console_tstc(int file)
246{
Simon Glass0d1e1f72014-07-23 06:54:59 -0600247 return stdio_devices[file]->tstc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100248}
249
250static inline void console_putc(int file, const char c)
251{
Simon Glass0d1e1f72014-07-23 06:54:59 -0600252 stdio_devices[file]->putc(stdio_devices[file], c);
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100253}
254
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200255#ifdef CONFIG_PRE_CONSOLE_BUFFER
Hans de Goedee355da02015-05-05 13:13:36 +0200256static inline void console_puts_noserial(int file, const char *s)
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200257{
258 if (strcmp(stdio_devices[file]->name, "serial") != 0)
Hans de Goedee355da02015-05-05 13:13:36 +0200259 stdio_devices[file]->puts(stdio_devices[file], s);
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200260}
261#endif
262
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100263static inline void console_puts(int file, const char *s)
264{
Simon Glass0d1e1f72014-07-23 06:54:59 -0600265 stdio_devices[file]->puts(stdio_devices[file], s);
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100266}
267
268static inline void console_printdevs(int file)
269{
270 printf("%s\n", stdio_devices[file]->name);
271}
272
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200273static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100274{
275 console_setfile(file, dev);
276}
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100277#endif /* defined(CONFIG_CONSOLE_MUX) */
278
wdenk47d1a6e2002-11-03 00:01:44 +0000279/** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
280
Wolfgang Denk318ef5c2010-06-20 17:14:14 +0200281int serial_printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000282{
283 va_list args;
284 uint i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200285 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000286
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100287 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000288
289 /* For this to work, printbuffer must be larger than
290 * anything we ever want to print.
291 */
Sonny Rao2813a212011-10-10 09:22:31 +0000292 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100293 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000294
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100295 serial_puts(printbuffer);
Wolfgang Denk318ef5c2010-06-20 17:14:14 +0200296 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000297}
298
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100299int fgetc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000300{
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100301 if (file < MAX_FILES) {
302#if defined(CONFIG_CONSOLE_MUX)
303 /*
304 * Effectively poll for input wherever it may be available.
305 */
306 for (;;) {
307 /*
308 * Upper layer may have already called tstc() so
309 * check for that first.
310 */
311 if (tstcdev != NULL)
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100312 return console_getc(file);
313 console_tstc(file);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100314#ifdef CONFIG_WATCHDOG
315 /*
316 * If the watchdog must be rate-limited then it should
317 * already be handled in board-specific code.
318 */
319 udelay(1);
320#endif
321 }
322#else
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100323 return console_getc(file);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100324#endif
325 }
wdenk47d1a6e2002-11-03 00:01:44 +0000326
327 return -1;
328}
329
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100330int ftstc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000331{
332 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100333 return console_tstc(file);
wdenk47d1a6e2002-11-03 00:01:44 +0000334
335 return -1;
336}
337
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100338void fputc(int file, const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000339{
340 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100341 console_putc(file, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000342}
343
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100344void fputs(int file, const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000345{
346 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100347 console_puts(file, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000348}
349
Wolfgang Denk318ef5c2010-06-20 17:14:14 +0200350int fprintf(int file, const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000351{
352 va_list args;
353 uint i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200354 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000355
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100356 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000357
358 /* For this to work, printbuffer must be larger than
359 * anything we ever want to print.
360 */
Sonny Rao2813a212011-10-10 09:22:31 +0000361 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100362 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000363
364 /* Send to desired file */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100365 fputs(file, printbuffer);
Wolfgang Denk318ef5c2010-06-20 17:14:14 +0200366 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000367}
368
369/** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
370
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100371int getc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000372{
Mark Jackson5de56212008-08-25 19:21:30 +0100373#ifdef CONFIG_DISABLE_CONSOLE
374 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
375 return 0;
376#endif
377
Graeme Russ70600b02011-08-29 02:14:05 +0000378 if (!gd->have_console)
379 return 0;
380
Simon Glass1bb49232015-11-08 23:47:48 -0700381#ifdef CONFIG_CONSOLE_RECORD
382 if (gd->console_in.start) {
383 int ch;
384
385 ch = membuff_getbyte(&gd->console_in);
386 if (ch != -1)
387 return 1;
388 }
389#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000390 if (gd->flags & GD_FLG_DEVINIT) {
391 /* Get from the standard input */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100392 return fgetc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000393 }
394
395 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100396 return serial_getc();
wdenk47d1a6e2002-11-03 00:01:44 +0000397}
398
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100399int tstc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000400{
Mark Jackson5de56212008-08-25 19:21:30 +0100401#ifdef CONFIG_DISABLE_CONSOLE
402 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
403 return 0;
404#endif
405
Graeme Russ70600b02011-08-29 02:14:05 +0000406 if (!gd->have_console)
407 return 0;
Simon Glass1bb49232015-11-08 23:47:48 -0700408#ifdef CONFIG_CONSOLE_RECORD
409 if (gd->console_in.start) {
410 if (membuff_peekbyte(&gd->console_in) != -1)
411 return 1;
412 }
413#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000414 if (gd->flags & GD_FLG_DEVINIT) {
415 /* Test the standard input */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100416 return ftstc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000417 }
418
419 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100420 return serial_tstc();
wdenk47d1a6e2002-11-03 00:01:44 +0000421}
422
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200423#define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
424#define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
425
Simon Glass89aa5c92012-03-19 21:59:14 -0700426#ifdef CONFIG_PRE_CONSOLE_BUFFER
Graeme Russ3c28f482011-09-01 00:48:27 +0000427#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
428
429static void pre_console_putc(const char c)
430{
431 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
432
433 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
434}
435
436static void pre_console_puts(const char *s)
437{
438 while (*s)
439 pre_console_putc(*s++);
440}
441
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200442static void print_pre_console_buffer(int flushpoint)
Graeme Russ3c28f482011-09-01 00:48:27 +0000443{
Hans de Goedee355da02015-05-05 13:13:36 +0200444 unsigned long in = 0, out = 0;
445 char *buf_in = (char *)CONFIG_PRE_CON_BUF_ADDR;
446 char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
Graeme Russ3c28f482011-09-01 00:48:27 +0000447
448 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
Hans de Goedee355da02015-05-05 13:13:36 +0200449 in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
Graeme Russ3c28f482011-09-01 00:48:27 +0000450
Hans de Goedee355da02015-05-05 13:13:36 +0200451 while (in < gd->precon_buf_idx)
452 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
453
454 buf_out[out] = 0;
455
456 switch (flushpoint) {
457 case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
458 puts(buf_out);
459 break;
460 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
461 console_puts_noserial(stdout, buf_out);
462 break;
463 }
Graeme Russ3c28f482011-09-01 00:48:27 +0000464}
465#else
466static inline void pre_console_putc(const char c) {}
467static inline void pre_console_puts(const char *s) {}
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200468static inline void print_pre_console_buffer(int flushpoint) {}
Graeme Russ3c28f482011-09-01 00:48:27 +0000469#endif
470
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100471void putc(const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000472{
Simon Glass3e9fd242013-11-10 10:27:01 -0700473#ifdef CONFIG_SANDBOX
Simon Glass1fa4c712015-06-23 15:38:34 -0600474 /* sandbox can send characters to stdout before it has a console */
Simon Glassb9d5f6b2014-07-23 06:55:07 -0600475 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
Simon Glass3e9fd242013-11-10 10:27:01 -0700476 os_putc(c);
477 return;
478 }
479#endif
Simon Glass7dd27e02015-06-23 15:38:33 -0600480#ifdef CONFIG_DEBUG_UART
481 /* if we don't have a console yet, use the debug UART */
482 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
483 printch(c);
484 return;
485 }
486#endif
Simon Glass1bb49232015-11-08 23:47:48 -0700487#ifdef CONFIG_CONSOLE_RECORD
488 if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
489 membuff_putbyte(&gd->console_out, c);
490#endif
wdenk4b6e9052004-02-06 21:48:22 +0000491#ifdef CONFIG_SILENT_CONSOLE
492 if (gd->flags & GD_FLG_SILENT)
wdenk927034e2004-02-08 19:38:38 +0000493 return;
wdenk4b6e9052004-02-06 21:48:22 +0000494#endif
495
Mark Jackson5de56212008-08-25 19:21:30 +0100496#ifdef CONFIG_DISABLE_CONSOLE
497 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
498 return;
499#endif
500
Graeme Russ70600b02011-08-29 02:14:05 +0000501 if (!gd->have_console)
Graeme Russ3c28f482011-09-01 00:48:27 +0000502 return pre_console_putc(c);
Graeme Russ70600b02011-08-29 02:14:05 +0000503
wdenk47d1a6e2002-11-03 00:01:44 +0000504 if (gd->flags & GD_FLG_DEVINIT) {
505 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100506 fputc(stdout, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000507 } else {
508 /* Send directly to the handler */
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200509 pre_console_putc(c);
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100510 serial_putc(c);
wdenk47d1a6e2002-11-03 00:01:44 +0000511 }
512}
513
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100514void puts(const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000515{
Simon Glass3e9fd242013-11-10 10:27:01 -0700516#ifdef CONFIG_SANDBOX
Simon Glassb9d5f6b2014-07-23 06:55:07 -0600517 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
Simon Glass3e9fd242013-11-10 10:27:01 -0700518 os_puts(s);
519 return;
520 }
521#endif
Simon Glass7dd27e02015-06-23 15:38:33 -0600522#ifdef CONFIG_DEBUG_UART
523 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
524 while (*s) {
525 int ch = *s++;
Simon Glass3e9fd242013-11-10 10:27:01 -0700526
Simon Glass7dd27e02015-06-23 15:38:33 -0600527 printch(ch);
528 if (ch == '\n')
529 printch('\r');
530 }
531 return;
532 }
533#endif
Simon Glass1bb49232015-11-08 23:47:48 -0700534#ifdef CONFIG_CONSOLE_RECORD
535 if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
536 membuff_put(&gd->console_out, s, strlen(s));
537#endif
wdenk4b6e9052004-02-06 21:48:22 +0000538#ifdef CONFIG_SILENT_CONSOLE
539 if (gd->flags & GD_FLG_SILENT)
540 return;
541#endif
542
Mark Jackson5de56212008-08-25 19:21:30 +0100543#ifdef CONFIG_DISABLE_CONSOLE
544 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
545 return;
546#endif
547
Graeme Russ70600b02011-08-29 02:14:05 +0000548 if (!gd->have_console)
Graeme Russ3c28f482011-09-01 00:48:27 +0000549 return pre_console_puts(s);
Graeme Russ70600b02011-08-29 02:14:05 +0000550
wdenk47d1a6e2002-11-03 00:01:44 +0000551 if (gd->flags & GD_FLG_DEVINIT) {
552 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100553 fputs(stdout, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000554 } else {
555 /* Send directly to the handler */
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200556 pre_console_puts(s);
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100557 serial_puts(s);
wdenk47d1a6e2002-11-03 00:01:44 +0000558 }
559}
560
Simon Glass1bb49232015-11-08 23:47:48 -0700561#ifdef CONFIG_CONSOLE_RECORD
562int console_record_init(void)
563{
564 int ret;
565
566 ret = membuff_new(&gd->console_out, CONFIG_CONSOLE_RECORD_OUT_SIZE);
567 if (ret)
568 return ret;
569 ret = membuff_new(&gd->console_in, CONFIG_CONSOLE_RECORD_IN_SIZE);
570
571 return ret;
572}
573
574void console_record_reset(void)
575{
576 membuff_purge(&gd->console_out);
577 membuff_purge(&gd->console_in);
578}
579
580void console_record_reset_enable(void)
581{
582 console_record_reset();
583 gd->flags |= GD_FLG_RECORD;
584}
585#endif
586
wdenk47d1a6e2002-11-03 00:01:44 +0000587/* test if ctrl-c was pressed */
588static int ctrlc_disabled = 0; /* see disable_ctrl() */
589static int ctrlc_was_pressed = 0;
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100590int ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000591{
Simon Glass28c5ce72014-09-14 12:40:15 -0600592#ifndef CONFIG_SANDBOX
wdenk47d1a6e2002-11-03 00:01:44 +0000593 if (!ctrlc_disabled && gd->have_console) {
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100594 if (tstc()) {
595 switch (getc()) {
wdenk47d1a6e2002-11-03 00:01:44 +0000596 case 0x03: /* ^C - Control C */
597 ctrlc_was_pressed = 1;
598 return 1;
599 default:
600 break;
601 }
602 }
603 }
Simon Glass28c5ce72014-09-14 12:40:15 -0600604#endif
605
wdenk47d1a6e2002-11-03 00:01:44 +0000606 return 0;
607}
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200608/* Reads user's confirmation.
609 Returns 1 if user's input is "y", "Y", "yes" or "YES"
610*/
611int confirm_yesno(void)
612{
613 int i;
614 char str_input[5];
wdenk47d1a6e2002-11-03 00:01:44 +0000615
Pierre Aubert5fef9d32014-04-24 10:30:07 +0200616 /* Flush input */
617 while (tstc())
618 getc();
619 i = 0;
620 while (i < sizeof(str_input)) {
621 str_input[i] = getc();
622 putc(str_input[i]);
623 if (str_input[i] == '\r')
624 break;
625 i++;
626 }
627 putc('\n');
628 if (strncmp(str_input, "y\r", 2) == 0 ||
629 strncmp(str_input, "Y\r", 2) == 0 ||
630 strncmp(str_input, "yes\r", 4) == 0 ||
631 strncmp(str_input, "YES\r", 4) == 0)
632 return 1;
633 return 0;
634}
wdenk47d1a6e2002-11-03 00:01:44 +0000635/* pass 1 to disable ctrlc() checking, 0 to enable.
636 * returns previous state
637 */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100638int disable_ctrlc(int disable)
wdenk47d1a6e2002-11-03 00:01:44 +0000639{
640 int prev = ctrlc_disabled; /* save previous state */
641
642 ctrlc_disabled = disable;
643 return prev;
644}
645
646int had_ctrlc (void)
647{
648 return ctrlc_was_pressed;
649}
650
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100651void clear_ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000652{
653 ctrlc_was_pressed = 0;
654}
655
656#ifdef CONFIG_MODEM_SUPPORT_DEBUG
657char screen[1024];
658char *cursor = screen;
659int once = 0;
660inline void dbg(const char *fmt, ...)
661{
662 va_list args;
663 uint i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200664 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000665
666 if (!once) {
667 memset(screen, 0, sizeof(screen));
668 once++;
669 }
670
671 va_start(args, fmt);
672
673 /* For this to work, printbuffer must be larger than
674 * anything we ever want to print.
675 */
Sonny Rao2813a212011-10-10 09:22:31 +0000676 i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
wdenk47d1a6e2002-11-03 00:01:44 +0000677 va_end(args);
678
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100679 if ((screen + sizeof(screen) - 1 - cursor)
680 < strlen(printbuffer) + 1) {
wdenk47d1a6e2002-11-03 00:01:44 +0000681 memset(screen, 0, sizeof(screen));
682 cursor = screen;
683 }
684 sprintf(cursor, printbuffer);
685 cursor += strlen(printbuffer);
686
687}
688#else
Jeroen Hofstee3b46b3b2014-10-08 22:57:48 +0200689static inline void dbg(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000690{
691}
692#endif
693
694/** U-Boot INIT FUNCTIONS *************************************************/
695
Mike Frysingere3d55722010-10-20 07:18:03 -0400696struct stdio_dev *search_device(int flags, const char *name)
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200697{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200698 struct stdio_dev *dev;
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200699
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200700 dev = stdio_get_by_name(name);
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200701
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100702 if (dev && (dev->flags & flags))
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200703 return dev;
704
705 return NULL;
706}
707
Mike Frysingere3d55722010-10-20 07:18:03 -0400708int console_assign(int file, const char *devname)
wdenk47d1a6e2002-11-03 00:01:44 +0000709{
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200710 int flag;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200711 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000712
713 /* Check for valid file */
714 switch (file) {
715 case stdin:
716 flag = DEV_FLAGS_INPUT;
717 break;
718 case stdout:
719 case stderr:
720 flag = DEV_FLAGS_OUTPUT;
721 break;
722 default:
723 return -1;
724 }
725
726 /* Check for valid device name */
727
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200728 dev = search_device(flag, devname);
wdenk47d1a6e2002-11-03 00:01:44 +0000729
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100730 if (dev)
731 return console_setfile(file, dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000732
733 return -1;
734}
735
736/* Called before relocation - use serial functions */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100737int console_init_f(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000738{
wdenk47d1a6e2002-11-03 00:01:44 +0000739 gd->have_console = 1;
wdenk808532a2003-10-10 10:05:42 +0000740
741#ifdef CONFIG_SILENT_CONSOLE
742 if (getenv("silent") != NULL)
743 gd->flags |= GD_FLG_SILENT;
744#endif
745
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200746 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
Graeme Russ3c28f482011-09-01 00:48:27 +0000747
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100748 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000749}
750
Jean-Christophe PLAGNIOL-VILLARD5a309362009-05-16 12:14:55 +0200751void stdio_print_current_devices(void)
752{
Jean-Christophe PLAGNIOL-VILLARD5a309362009-05-16 12:14:55 +0200753 /* Print information */
754 puts("In: ");
755 if (stdio_devices[stdin] == NULL) {
756 puts("No input devices available!\n");
757 } else {
758 printf ("%s\n", stdio_devices[stdin]->name);
759 }
760
761 puts("Out: ");
762 if (stdio_devices[stdout] == NULL) {
763 puts("No output devices available!\n");
764 } else {
765 printf ("%s\n", stdio_devices[stdout]->name);
766 }
767
768 puts("Err: ");
769 if (stdio_devices[stderr] == NULL) {
770 puts("No error devices available!\n");
771 } else {
772 printf ("%s\n", stdio_devices[stderr]->name);
773 }
Jean-Christophe PLAGNIOL-VILLARD5a309362009-05-16 12:14:55 +0200774}
775
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200776#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +0000777/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100778int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000779{
780 char *stdinname, *stdoutname, *stderrname;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200781 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200782#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk05939202004-04-18 17:39:38 +0000783 int i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200784#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100785#ifdef CONFIG_CONSOLE_MUX
786 int iomux_err = 0;
787#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000788
789 /* set default handlers at first */
Martin Dorwigcb2c2862015-01-26 15:22:54 -0700790 gd->jt->getc = serial_getc;
791 gd->jt->tstc = serial_tstc;
792 gd->jt->putc = serial_putc;
793 gd->jt->puts = serial_puts;
794 gd->jt->printf = serial_printf;
wdenk47d1a6e2002-11-03 00:01:44 +0000795
796 /* stdin stdout and stderr are in environment */
797 /* scan for it */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100798 stdinname = getenv("stdin");
799 stdoutname = getenv("stdout");
800 stderrname = getenv("stderr");
wdenk47d1a6e2002-11-03 00:01:44 +0000801
Wolfgang Denka1be4762008-05-20 16:00:29 +0200802 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100803 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
804 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
805 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100806#ifdef CONFIG_CONSOLE_MUX
807 iomux_err = iomux_doenv(stdin, stdinname);
808 iomux_err += iomux_doenv(stdout, stdoutname);
809 iomux_err += iomux_doenv(stderr, stderrname);
810 if (!iomux_err)
811 /* Successful, so skip all the code below. */
812 goto done;
813#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000814 }
815 /* if the devices are overwritten or not found, use default device */
816 if (inputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100817 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000818 }
819 if (outputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100820 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000821 }
822 if (errdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100823 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000824 }
825 /* Initializes output console first */
826 if (outputdev != NULL) {
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100827 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100828 console_doenv(stdout, outputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000829 }
830 if (errdev != NULL) {
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100831 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100832 console_doenv(stderr, errdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000833 }
834 if (inputdev != NULL) {
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100835 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD63331a02009-02-01 17:07:52 +0100836 console_doenv(stdin, inputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000837 }
838
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100839#ifdef CONFIG_CONSOLE_MUX
840done:
841#endif
842
Simon Glassa2cc8552012-12-05 14:46:43 +0000843#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD5a309362009-05-16 12:14:55 +0200844 stdio_print_current_devices();
Simon Glassa2cc8552012-12-05 14:46:43 +0000845#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +0000846
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200847#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk47d1a6e2002-11-03 00:01:44 +0000848 /* set the environment variables (will overwrite previous env settings) */
849 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100850 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000851 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200852#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
wdenk47d1a6e2002-11-03 00:01:44 +0000853
Joe Hershbergera46f7702012-12-11 22:16:19 -0600854 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
855
wdenk47d1a6e2002-11-03 00:01:44 +0000856#if 0
857 /* If nothing usable installed, use only the initial console */
858 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100859 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000860#endif
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200861 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100862 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000863}
864
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200865#else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +0000866
867/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100868int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000869{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200870 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200871 int i;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200872 struct list_head *list = stdio_get_list();
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200873 struct list_head *pos;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200874 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000875
wdenk92bbe3f2003-04-20 14:04:18 +0000876#ifdef CONFIG_SPLASH_SCREEN
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100877 /*
878 * suppress all output if splash screen is enabled and we have
Anatolij Gustschinec8744a2010-03-16 15:29:33 +0100879 * a bmp to display. We redirect the output from frame buffer
880 * console to serial console in this case or suppress it if
881 * "silent" mode was requested.
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100882 */
Anatolij Gustschinec8744a2010-03-16 15:29:33 +0100883 if (getenv("splashimage") != NULL) {
884 if (!(gd->flags & GD_FLG_SILENT))
885 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
886 }
wdenk808532a2003-10-10 10:05:42 +0000887#endif
888
wdenk47d1a6e2002-11-03 00:01:44 +0000889 /* Scan devices looking for input and output devices */
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200890 list_for_each(pos, list) {
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200891 dev = list_entry(pos, struct stdio_dev, list);
wdenk47d1a6e2002-11-03 00:01:44 +0000892
893 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
894 inputdev = dev;
895 }
896 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
897 outputdev = dev;
898 }
Jean-Christophe PLAGNIOL-VILLARD8a4a7842008-08-31 04:24:55 +0200899 if(inputdev && outputdev)
900 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000901 }
902
903 /* Initializes output console first */
904 if (outputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100905 console_setfile(stdout, outputdev);
906 console_setfile(stderr, outputdev);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100907#ifdef CONFIG_CONSOLE_MUX
908 console_devices[stdout][0] = outputdev;
909 console_devices[stderr][0] = outputdev;
910#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000911 }
912
913 /* Initializes input console */
914 if (inputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100915 console_setfile(stdin, inputdev);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100916#ifdef CONFIG_CONSOLE_MUX
917 console_devices[stdin][0] = inputdev;
918#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000919 }
920
Simon Glassa2cc8552012-12-05 14:46:43 +0000921#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD5a309362009-05-16 12:14:55 +0200922 stdio_print_current_devices();
Simon Glassa2cc8552012-12-05 14:46:43 +0000923#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +0000924
925 /* Setting environment variables */
926 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100927 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000928 }
929
Joe Hershbergera46f7702012-12-11 22:16:19 -0600930 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
931
wdenk47d1a6e2002-11-03 00:01:44 +0000932#if 0
933 /* If nothing usable installed, use only the initial console */
934 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100935 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000936#endif
Siarhei Siamashka616d03e2015-01-08 09:02:31 +0200937 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
Jean-Christophe PLAGNIOL-VILLARDc7dc6722009-02-01 17:07:51 +0100938 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000939}
940
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200941#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */