blob: 12293f383624947cc716319a862df4340c44b1c2 [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 Denk1a459662013-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 Glass24b852a2015-11-08 23:47:45 -07009#include <console.h>
Simon Glassd6ea5302015-06-23 15:38:33 -060010#include <debug_uart.h>
wdenk47d1a6e2002-11-03 00:01:44 +000011#include <stdarg.h>
Jeroen Hofstee482f4692014-10-08 22:57:48 +020012#include <iomux.h>
wdenk47d1a6e2002-11-03 00:01:44 +000013#include <malloc.h>
Simon Glass91b136c2013-11-10 10:27:01 -070014#include <os.h>
Joe Hershberger849d5d92012-12-11 22:16:29 -060015#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020016#include <stdio_dev.h>
wdenk27b207f2003-07-24 23:38:38 +000017#include <exports.h>
Joe Hershberger849d5d92012-12-11 22:16:29 -060018#include <environment.h>
wdenk47d1a6e2002-11-03 00:01:44 +000019
Wolfgang Denkd87080b2006-03-31 18:32:53 +020020DECLARE_GLOBAL_DATA_PTR;
21
Joe Hershberger849d5d92012-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 Hershbergere080d542012-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-VILLARD6d0f6bc2008-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-VILLARD6d0f6bc2008-10-16 15:01:15 +020093#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +010094extern int overwrite_console(void);
95#define OVERWRITE_CONSOLE overwrite_console()
wdenk47d1a6e2002-11-03 00:01:44 +000096#else
wdenk83e40ba2005-03-31 18:42:15 +000097#define OVERWRITE_CONSOLE 0
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020098#endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
wdenk47d1a6e2002-11-03 00:01:44 +000099
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200100#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +0000101
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-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 Glass709ea542014-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 Dorwig49cad542015-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 Dorwig49cad542015-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 Jennejohn16a28ef2008-11-06 15:04:23 +0100147#if defined(CONFIG_CONSOLE_MUX)
148/** Console I/O multiplexing *******************************************/
149
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200150static struct stdio_dev *tstcdev;
151struct stdio_dev **console_devices[MAX_FILES];
Gary Jennejohn16a28ef2008-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-VILLARD5f032012009-02-01 17:07:52 +0100160static int console_getc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100161{
162 unsigned char ret;
163
164 /* This is never called with testcdev == NULL */
Simon Glass709ea542014-07-23 06:54:59 -0600165 ret = tstcdev->getc(tstcdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100166 tstcdev = NULL;
167 return ret;
168}
169
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100170static int console_tstc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100171{
172 int i, ret;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200173 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-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 Glass709ea542014-07-23 06:54:59 -0600179 ret = dev->tstc(dev);
Gary Jennejohn16a28ef2008-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-VILLARD5f032012009-02-01 17:07:52 +0100192static void console_putc(int file, const char c)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100193{
194 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200195 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-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 Glass709ea542014-07-23 06:54:59 -0600200 dev->putc(dev, c);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100201 }
202}
203
Siarhei Siamashka27669662015-01-08 09:02:31 +0200204#ifdef CONFIG_PRE_CONSOLE_BUFFER
Hans de Goedea8552c72015-05-05 13:13:36 +0200205static void console_puts_noserial(int file, const char *s)
Siarhei Siamashka27669662015-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 Goedea8552c72015-05-05 13:13:36 +0200212 if (dev->puts != NULL && strcmp(dev->name, "serial") != 0)
213 dev->puts(dev, s);
Siarhei Siamashka27669662015-01-08 09:02:31 +0200214 }
215}
216#endif
217
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100218static void console_puts(int file, const char *s)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100219{
220 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200221 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-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 Glass709ea542014-07-23 06:54:59 -0600226 dev->puts(dev, s);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100227 }
228}
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100229
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200230static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100231{
232 iomux_doenv(file, dev->name);
233}
234#else
235static inline int console_getc(int file)
236{
Simon Glass709ea542014-07-23 06:54:59 -0600237 return stdio_devices[file]->getc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100238}
239
240static inline int console_tstc(int file)
241{
Simon Glass709ea542014-07-23 06:54:59 -0600242 return stdio_devices[file]->tstc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100243}
244
245static inline void console_putc(int file, const char c)
246{
Simon Glass709ea542014-07-23 06:54:59 -0600247 stdio_devices[file]->putc(stdio_devices[file], c);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100248}
249
Siarhei Siamashka27669662015-01-08 09:02:31 +0200250#ifdef CONFIG_PRE_CONSOLE_BUFFER
Hans de Goedea8552c72015-05-05 13:13:36 +0200251static inline void console_puts_noserial(int file, const char *s)
Siarhei Siamashka27669662015-01-08 09:02:31 +0200252{
253 if (strcmp(stdio_devices[file]->name, "serial") != 0)
Hans de Goedea8552c72015-05-05 13:13:36 +0200254 stdio_devices[file]->puts(stdio_devices[file], s);
Siarhei Siamashka27669662015-01-08 09:02:31 +0200255}
256#endif
257
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100258static inline void console_puts(int file, const char *s)
259{
Simon Glass709ea542014-07-23 06:54:59 -0600260 stdio_devices[file]->puts(stdio_devices[file], s);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100261}
262
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200263static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100264{
265 console_setfile(file, dev);
266}
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100267#endif /* defined(CONFIG_CONSOLE_MUX) */
268
wdenk47d1a6e2002-11-03 00:01:44 +0000269/** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
270
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200271int serial_printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000272{
273 va_list args;
274 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200275 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000276
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100277 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000278
279 /* For this to work, printbuffer must be larger than
280 * anything we ever want to print.
281 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000282 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100283 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000284
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100285 serial_puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200286 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000287}
288
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100289int fgetc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000290{
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100291 if (file < MAX_FILES) {
292#if defined(CONFIG_CONSOLE_MUX)
293 /*
294 * Effectively poll for input wherever it may be available.
295 */
296 for (;;) {
297 /*
298 * Upper layer may have already called tstc() so
299 * check for that first.
300 */
301 if (tstcdev != NULL)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100302 return console_getc(file);
303 console_tstc(file);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100304#ifdef CONFIG_WATCHDOG
305 /*
306 * If the watchdog must be rate-limited then it should
307 * already be handled in board-specific code.
308 */
309 udelay(1);
310#endif
311 }
312#else
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100313 return console_getc(file);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100314#endif
315 }
wdenk47d1a6e2002-11-03 00:01:44 +0000316
317 return -1;
318}
319
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100320int ftstc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000321{
322 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100323 return console_tstc(file);
wdenk47d1a6e2002-11-03 00:01:44 +0000324
325 return -1;
326}
327
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100328void fputc(int file, const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000329{
330 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100331 console_putc(file, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000332}
333
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100334void fputs(int file, const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000335{
336 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100337 console_puts(file, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000338}
339
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200340int fprintf(int file, const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000341{
342 va_list args;
343 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200344 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000345
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100346 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000347
348 /* For this to work, printbuffer must be larger than
349 * anything we ever want to print.
350 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000351 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100352 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000353
354 /* Send to desired file */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100355 fputs(file, printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200356 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000357}
358
359/** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
360
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100361int getc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000362{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100363#ifdef CONFIG_DISABLE_CONSOLE
364 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
365 return 0;
366#endif
367
Graeme Russe3e454c2011-08-29 02:14:05 +0000368 if (!gd->have_console)
369 return 0;
370
Simon Glass9854a872015-11-08 23:47:48 -0700371#ifdef CONFIG_CONSOLE_RECORD
372 if (gd->console_in.start) {
373 int ch;
374
375 ch = membuff_getbyte(&gd->console_in);
376 if (ch != -1)
377 return 1;
378 }
379#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000380 if (gd->flags & GD_FLG_DEVINIT) {
381 /* Get from the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100382 return fgetc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000383 }
384
385 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100386 return serial_getc();
wdenk47d1a6e2002-11-03 00:01:44 +0000387}
388
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100389int tstc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000390{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100391#ifdef CONFIG_DISABLE_CONSOLE
392 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
393 return 0;
394#endif
395
Graeme Russe3e454c2011-08-29 02:14:05 +0000396 if (!gd->have_console)
397 return 0;
Simon Glass9854a872015-11-08 23:47:48 -0700398#ifdef CONFIG_CONSOLE_RECORD
399 if (gd->console_in.start) {
400 if (membuff_peekbyte(&gd->console_in) != -1)
401 return 1;
402 }
403#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000404 if (gd->flags & GD_FLG_DEVINIT) {
405 /* Test the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100406 return ftstc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000407 }
408
409 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100410 return serial_tstc();
wdenk47d1a6e2002-11-03 00:01:44 +0000411}
412
Siarhei Siamashka27669662015-01-08 09:02:31 +0200413#define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
414#define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
415
Simon Glass3fa49772012-03-19 21:59:14 -0700416#ifdef CONFIG_PRE_CONSOLE_BUFFER
Graeme Russ9558b482011-09-01 00:48:27 +0000417#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
418
419static void pre_console_putc(const char c)
420{
421 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
422
423 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
424}
425
426static void pre_console_puts(const char *s)
427{
428 while (*s)
429 pre_console_putc(*s++);
430}
431
Siarhei Siamashka27669662015-01-08 09:02:31 +0200432static void print_pre_console_buffer(int flushpoint)
Graeme Russ9558b482011-09-01 00:48:27 +0000433{
Hans de Goedea8552c72015-05-05 13:13:36 +0200434 unsigned long in = 0, out = 0;
435 char *buf_in = (char *)CONFIG_PRE_CON_BUF_ADDR;
436 char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
Graeme Russ9558b482011-09-01 00:48:27 +0000437
438 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
Hans de Goedea8552c72015-05-05 13:13:36 +0200439 in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
Graeme Russ9558b482011-09-01 00:48:27 +0000440
Hans de Goedea8552c72015-05-05 13:13:36 +0200441 while (in < gd->precon_buf_idx)
442 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
443
444 buf_out[out] = 0;
445
446 switch (flushpoint) {
447 case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
448 puts(buf_out);
449 break;
450 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
451 console_puts_noserial(stdout, buf_out);
452 break;
453 }
Graeme Russ9558b482011-09-01 00:48:27 +0000454}
455#else
456static inline void pre_console_putc(const char c) {}
457static inline void pre_console_puts(const char *s) {}
Siarhei Siamashka27669662015-01-08 09:02:31 +0200458static inline void print_pre_console_buffer(int flushpoint) {}
Graeme Russ9558b482011-09-01 00:48:27 +0000459#endif
460
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100461void putc(const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000462{
Simon Glass91b136c2013-11-10 10:27:01 -0700463#ifdef CONFIG_SANDBOX
Simon Glassda229e42015-06-23 15:38:34 -0600464 /* sandbox can send characters to stdout before it has a console */
Simon Glass093f79a2014-07-23 06:55:07 -0600465 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
Simon Glass91b136c2013-11-10 10:27:01 -0700466 os_putc(c);
467 return;
468 }
469#endif
Simon Glassd6ea5302015-06-23 15:38:33 -0600470#ifdef CONFIG_DEBUG_UART
471 /* if we don't have a console yet, use the debug UART */
472 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
473 printch(c);
474 return;
475 }
476#endif
Simon Glass9854a872015-11-08 23:47:48 -0700477#ifdef CONFIG_CONSOLE_RECORD
478 if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
479 membuff_putbyte(&gd->console_out, c);
480#endif
wdenka6cccae2004-02-06 21:48:22 +0000481#ifdef CONFIG_SILENT_CONSOLE
482 if (gd->flags & GD_FLG_SILENT)
wdenkf6e20fc2004-02-08 19:38:38 +0000483 return;
wdenka6cccae2004-02-06 21:48:22 +0000484#endif
485
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100486#ifdef CONFIG_DISABLE_CONSOLE
487 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
488 return;
489#endif
490
Graeme Russe3e454c2011-08-29 02:14:05 +0000491 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000492 return pre_console_putc(c);
Graeme Russe3e454c2011-08-29 02:14:05 +0000493
wdenk47d1a6e2002-11-03 00:01:44 +0000494 if (gd->flags & GD_FLG_DEVINIT) {
495 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100496 fputc(stdout, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000497 } else {
498 /* Send directly to the handler */
Siarhei Siamashka27669662015-01-08 09:02:31 +0200499 pre_console_putc(c);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100500 serial_putc(c);
wdenk47d1a6e2002-11-03 00:01:44 +0000501 }
502}
503
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100504void puts(const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000505{
Simon Glass91b136c2013-11-10 10:27:01 -0700506#ifdef CONFIG_SANDBOX
Simon Glass093f79a2014-07-23 06:55:07 -0600507 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
Simon Glass91b136c2013-11-10 10:27:01 -0700508 os_puts(s);
509 return;
510 }
511#endif
Simon Glassd6ea5302015-06-23 15:38:33 -0600512#ifdef CONFIG_DEBUG_UART
513 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
514 while (*s) {
515 int ch = *s++;
Simon Glass91b136c2013-11-10 10:27:01 -0700516
Simon Glassd6ea5302015-06-23 15:38:33 -0600517 printch(ch);
Simon Glassd6ea5302015-06-23 15:38:33 -0600518 }
519 return;
520 }
521#endif
Simon Glass9854a872015-11-08 23:47:48 -0700522#ifdef CONFIG_CONSOLE_RECORD
523 if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
524 membuff_put(&gd->console_out, s, strlen(s));
525#endif
wdenka6cccae2004-02-06 21:48:22 +0000526#ifdef CONFIG_SILENT_CONSOLE
527 if (gd->flags & GD_FLG_SILENT)
528 return;
529#endif
530
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100531#ifdef CONFIG_DISABLE_CONSOLE
532 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
533 return;
534#endif
535
Graeme Russe3e454c2011-08-29 02:14:05 +0000536 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000537 return pre_console_puts(s);
Graeme Russe3e454c2011-08-29 02:14:05 +0000538
wdenk47d1a6e2002-11-03 00:01:44 +0000539 if (gd->flags & GD_FLG_DEVINIT) {
540 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100541 fputs(stdout, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000542 } else {
543 /* Send directly to the handler */
Siarhei Siamashka27669662015-01-08 09:02:31 +0200544 pre_console_puts(s);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100545 serial_puts(s);
wdenk47d1a6e2002-11-03 00:01:44 +0000546 }
547}
548
Simon Glass9854a872015-11-08 23:47:48 -0700549#ifdef CONFIG_CONSOLE_RECORD
550int console_record_init(void)
551{
552 int ret;
553
554 ret = membuff_new(&gd->console_out, CONFIG_CONSOLE_RECORD_OUT_SIZE);
555 if (ret)
556 return ret;
557 ret = membuff_new(&gd->console_in, CONFIG_CONSOLE_RECORD_IN_SIZE);
558
559 return ret;
560}
561
562void console_record_reset(void)
563{
564 membuff_purge(&gd->console_out);
565 membuff_purge(&gd->console_in);
566}
567
568void console_record_reset_enable(void)
569{
570 console_record_reset();
571 gd->flags |= GD_FLG_RECORD;
572}
573#endif
574
wdenk47d1a6e2002-11-03 00:01:44 +0000575/* test if ctrl-c was pressed */
576static int ctrlc_disabled = 0; /* see disable_ctrl() */
577static int ctrlc_was_pressed = 0;
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100578int ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000579{
Simon Glass8969ea32014-09-14 12:40:15 -0600580#ifndef CONFIG_SANDBOX
wdenk47d1a6e2002-11-03 00:01:44 +0000581 if (!ctrlc_disabled && gd->have_console) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100582 if (tstc()) {
583 switch (getc()) {
wdenk47d1a6e2002-11-03 00:01:44 +0000584 case 0x03: /* ^C - Control C */
585 ctrlc_was_pressed = 1;
586 return 1;
587 default:
588 break;
589 }
590 }
591 }
Simon Glass8969ea32014-09-14 12:40:15 -0600592#endif
593
wdenk47d1a6e2002-11-03 00:01:44 +0000594 return 0;
595}
Pierre Auberta5dffa42014-04-24 10:30:07 +0200596/* Reads user's confirmation.
597 Returns 1 if user's input is "y", "Y", "yes" or "YES"
598*/
599int confirm_yesno(void)
600{
601 int i;
602 char str_input[5];
wdenk47d1a6e2002-11-03 00:01:44 +0000603
Pierre Auberta5dffa42014-04-24 10:30:07 +0200604 /* Flush input */
605 while (tstc())
606 getc();
607 i = 0;
608 while (i < sizeof(str_input)) {
609 str_input[i] = getc();
610 putc(str_input[i]);
611 if (str_input[i] == '\r')
612 break;
613 i++;
614 }
615 putc('\n');
616 if (strncmp(str_input, "y\r", 2) == 0 ||
617 strncmp(str_input, "Y\r", 2) == 0 ||
618 strncmp(str_input, "yes\r", 4) == 0 ||
619 strncmp(str_input, "YES\r", 4) == 0)
620 return 1;
621 return 0;
622}
wdenk47d1a6e2002-11-03 00:01:44 +0000623/* pass 1 to disable ctrlc() checking, 0 to enable.
624 * returns previous state
625 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100626int disable_ctrlc(int disable)
wdenk47d1a6e2002-11-03 00:01:44 +0000627{
628 int prev = ctrlc_disabled; /* save previous state */
629
630 ctrlc_disabled = disable;
631 return prev;
632}
633
634int had_ctrlc (void)
635{
636 return ctrlc_was_pressed;
637}
638
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100639void clear_ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000640{
641 ctrlc_was_pressed = 0;
642}
643
wdenk47d1a6e2002-11-03 00:01:44 +0000644/** U-Boot INIT FUNCTIONS *************************************************/
645
Mike Frysingerd7be3052010-10-20 07:18:03 -0400646struct stdio_dev *search_device(int flags, const char *name)
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200647{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200648 struct stdio_dev *dev;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200649
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200650 dev = stdio_get_by_name(name);
Simon Glassa2931b32016-02-06 14:31:37 -0700651#ifdef CONFIG_VIDCONSOLE_AS_LCD
652 if (!dev && !strcmp(name, "lcd"))
653 dev = stdio_get_by_name("vidconsole");
654#endif
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200655
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100656 if (dev && (dev->flags & flags))
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200657 return dev;
658
659 return NULL;
660}
661
Mike Frysingerd7be3052010-10-20 07:18:03 -0400662int console_assign(int file, const char *devname)
wdenk47d1a6e2002-11-03 00:01:44 +0000663{
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200664 int flag;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200665 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000666
667 /* Check for valid file */
668 switch (file) {
669 case stdin:
670 flag = DEV_FLAGS_INPUT;
671 break;
672 case stdout:
673 case stderr:
674 flag = DEV_FLAGS_OUTPUT;
675 break;
676 default:
677 return -1;
678 }
679
680 /* Check for valid device name */
681
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200682 dev = search_device(flag, devname);
wdenk47d1a6e2002-11-03 00:01:44 +0000683
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100684 if (dev)
685 return console_setfile(file, dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000686
687 return -1;
688}
689
690/* Called before relocation - use serial functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100691int console_init_f(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000692{
wdenk47d1a6e2002-11-03 00:01:44 +0000693 gd->have_console = 1;
wdenkf72da342003-10-10 10:05:42 +0000694
695#ifdef CONFIG_SILENT_CONSOLE
696 if (getenv("silent") != NULL)
697 gd->flags |= GD_FLG_SILENT;
698#endif
699
Siarhei Siamashka27669662015-01-08 09:02:31 +0200700 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
Graeme Russ9558b482011-09-01 00:48:27 +0000701
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100702 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000703}
704
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200705void stdio_print_current_devices(void)
706{
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200707 /* Print information */
708 puts("In: ");
709 if (stdio_devices[stdin] == NULL) {
710 puts("No input devices available!\n");
711 } else {
712 printf ("%s\n", stdio_devices[stdin]->name);
713 }
714
715 puts("Out: ");
716 if (stdio_devices[stdout] == NULL) {
717 puts("No output devices available!\n");
718 } else {
719 printf ("%s\n", stdio_devices[stdout]->name);
720 }
721
722 puts("Err: ");
723 if (stdio_devices[stderr] == NULL) {
724 puts("No error devices available!\n");
725 } else {
726 printf ("%s\n", stdio_devices[stderr]->name);
727 }
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200728}
729
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200730#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +0000731/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100732int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000733{
734 char *stdinname, *stdoutname, *stderrname;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200735 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200736#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk6e592382004-04-18 17:39:38 +0000737 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200738#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100739#ifdef CONFIG_CONSOLE_MUX
740 int iomux_err = 0;
741#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000742
743 /* set default handlers at first */
Martin Dorwig49cad542015-01-26 15:22:54 -0700744 gd->jt->getc = serial_getc;
745 gd->jt->tstc = serial_tstc;
746 gd->jt->putc = serial_putc;
747 gd->jt->puts = serial_puts;
748 gd->jt->printf = serial_printf;
wdenk47d1a6e2002-11-03 00:01:44 +0000749
750 /* stdin stdout and stderr are in environment */
751 /* scan for it */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100752 stdinname = getenv("stdin");
753 stdoutname = getenv("stdout");
754 stderrname = getenv("stderr");
wdenk47d1a6e2002-11-03 00:01:44 +0000755
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200756 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100757 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
758 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
759 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100760#ifdef CONFIG_CONSOLE_MUX
761 iomux_err = iomux_doenv(stdin, stdinname);
762 iomux_err += iomux_doenv(stdout, stdoutname);
763 iomux_err += iomux_doenv(stderr, stderrname);
764 if (!iomux_err)
765 /* Successful, so skip all the code below. */
766 goto done;
767#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000768 }
769 /* if the devices are overwritten or not found, use default device */
770 if (inputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100771 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000772 }
773 if (outputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100774 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000775 }
776 if (errdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100777 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000778 }
779 /* Initializes output console first */
780 if (outputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100781 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100782 console_doenv(stdout, outputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000783 }
784 if (errdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100785 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100786 console_doenv(stderr, errdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000787 }
788 if (inputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100789 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100790 console_doenv(stdin, inputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000791 }
792
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100793#ifdef CONFIG_CONSOLE_MUX
794done:
795#endif
796
Simon Glass78c112c2012-12-05 14:46:43 +0000797#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200798 stdio_print_current_devices();
Simon Glass78c112c2012-12-05 14:46:43 +0000799#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
Simon Glassa2931b32016-02-06 14:31:37 -0700800#ifdef CONFIG_VIDCONSOLE_AS_LCD
801 if (strstr(stdoutname, "lcd"))
802 printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars\n");
803#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000804
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200805#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk47d1a6e2002-11-03 00:01:44 +0000806 /* set the environment variables (will overwrite previous env settings) */
807 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100808 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000809 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200810#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
wdenk47d1a6e2002-11-03 00:01:44 +0000811
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600812 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
813
wdenk47d1a6e2002-11-03 00:01:44 +0000814#if 0
815 /* If nothing usable installed, use only the initial console */
816 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100817 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000818#endif
Siarhei Siamashka27669662015-01-08 09:02:31 +0200819 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100820 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000821}
822
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200823#else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +0000824
825/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100826int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000827{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200828 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200829 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200830 struct list_head *list = stdio_get_list();
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200831 struct list_head *pos;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200832 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000833
wdenkd791b1d2003-04-20 14:04:18 +0000834#ifdef CONFIG_SPLASH_SCREEN
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100835 /*
836 * suppress all output if splash screen is enabled and we have
Anatolij Gustschina7490812010-03-16 15:29:33 +0100837 * a bmp to display. We redirect the output from frame buffer
838 * console to serial console in this case or suppress it if
839 * "silent" mode was requested.
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100840 */
Anatolij Gustschina7490812010-03-16 15:29:33 +0100841 if (getenv("splashimage") != NULL) {
842 if (!(gd->flags & GD_FLG_SILENT))
843 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
844 }
wdenkf72da342003-10-10 10:05:42 +0000845#endif
846
wdenk47d1a6e2002-11-03 00:01:44 +0000847 /* Scan devices looking for input and output devices */
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200848 list_for_each(pos, list) {
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200849 dev = list_entry(pos, struct stdio_dev, list);
wdenk47d1a6e2002-11-03 00:01:44 +0000850
851 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
852 inputdev = dev;
853 }
854 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
855 outputdev = dev;
856 }
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200857 if(inputdev && outputdev)
858 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000859 }
860
861 /* Initializes output console first */
862 if (outputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100863 console_setfile(stdout, outputdev);
864 console_setfile(stderr, outputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100865#ifdef CONFIG_CONSOLE_MUX
866 console_devices[stdout][0] = outputdev;
867 console_devices[stderr][0] = outputdev;
868#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000869 }
870
871 /* Initializes input console */
872 if (inputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100873 console_setfile(stdin, inputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100874#ifdef CONFIG_CONSOLE_MUX
875 console_devices[stdin][0] = inputdev;
876#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000877 }
878
Simon Glass78c112c2012-12-05 14:46:43 +0000879#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200880 stdio_print_current_devices();
Simon Glass78c112c2012-12-05 14:46:43 +0000881#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +0000882
883 /* Setting environment variables */
884 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100885 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000886 }
887
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600888 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
889
wdenk47d1a6e2002-11-03 00:01:44 +0000890#if 0
891 /* If nothing usable installed, use only the initial console */
892 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100893 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000894#endif
Siarhei Siamashka27669662015-01-08 09:02:31 +0200895 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100896 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000897}
898
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200899#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */