blob: 10972b04a6f6da4980f1ceaea93d1b8f37d0a38a [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
230static inline void console_printdevs(int file)
231{
232 iomux_printdevs(file);
233}
234
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200235static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100236{
237 iomux_doenv(file, dev->name);
238}
239#else
240static inline int console_getc(int file)
241{
Simon Glass709ea542014-07-23 06:54:59 -0600242 return stdio_devices[file]->getc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100243}
244
245static inline int console_tstc(int file)
246{
Simon Glass709ea542014-07-23 06:54:59 -0600247 return stdio_devices[file]->tstc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100248}
249
250static inline void console_putc(int file, const char c)
251{
Simon Glass709ea542014-07-23 06:54:59 -0600252 stdio_devices[file]->putc(stdio_devices[file], c);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100253}
254
Siarhei Siamashka27669662015-01-08 09:02:31 +0200255#ifdef CONFIG_PRE_CONSOLE_BUFFER
Hans de Goedea8552c72015-05-05 13:13:36 +0200256static inline void console_puts_noserial(int file, const char *s)
Siarhei Siamashka27669662015-01-08 09:02:31 +0200257{
258 if (strcmp(stdio_devices[file]->name, "serial") != 0)
Hans de Goedea8552c72015-05-05 13:13:36 +0200259 stdio_devices[file]->puts(stdio_devices[file], s);
Siarhei Siamashka27669662015-01-08 09:02:31 +0200260}
261#endif
262
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100263static inline void console_puts(int file, const char *s)
264{
Simon Glass709ea542014-07-23 06:54:59 -0600265 stdio_devices[file]->puts(stdio_devices[file], s);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-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-VILLARD52cb4d42009-05-16 12:14:54 +0200273static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100274{
275 console_setfile(file, dev);
276}
Gary Jennejohn16a28ef2008-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 Denkd9c27252010-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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200285 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000286
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-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 Rao068af6f2011-10-10 09:22:31 +0000292 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100293 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000294
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100295 serial_puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200296 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000297}
298
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100299int fgetc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000300{
Gary Jennejohn16a28ef2008-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-VILLARD5f032012009-02-01 17:07:52 +0100312 return console_getc(file);
313 console_tstc(file);
Gary Jennejohn16a28ef2008-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-VILLARD5f032012009-02-01 17:07:52 +0100323 return console_getc(file);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100324#endif
325 }
wdenk47d1a6e2002-11-03 00:01:44 +0000326
327 return -1;
328}
329
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100330int ftstc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000331{
332 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-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-VILLARDec6f1492009-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-VILLARD5f032012009-02-01 17:07:52 +0100341 console_putc(file, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000342}
343
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-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-VILLARD5f032012009-02-01 17:07:52 +0100347 console_puts(file, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000348}
349
Wolfgang Denkd9c27252010-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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200354 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000355
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-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 Rao068af6f2011-10-10 09:22:31 +0000361 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100362 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000363
364 /* Send to desired file */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100365 fputs(file, printbuffer);
Wolfgang Denkd9c27252010-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-VILLARDec6f1492009-02-01 17:07:51 +0100371int getc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000372{
Mark Jacksonf5c3ba72008-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 Russe3e454c2011-08-29 02:14:05 +0000378 if (!gd->have_console)
379 return 0;
380
wdenk47d1a6e2002-11-03 00:01:44 +0000381 if (gd->flags & GD_FLG_DEVINIT) {
382 /* Get from the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100383 return fgetc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000384 }
385
386 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100387 return serial_getc();
wdenk47d1a6e2002-11-03 00:01:44 +0000388}
389
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100390int tstc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000391{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100392#ifdef CONFIG_DISABLE_CONSOLE
393 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
394 return 0;
395#endif
396
Graeme Russe3e454c2011-08-29 02:14:05 +0000397 if (!gd->have_console)
398 return 0;
399
wdenk47d1a6e2002-11-03 00:01:44 +0000400 if (gd->flags & GD_FLG_DEVINIT) {
401 /* Test the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100402 return ftstc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000403 }
404
405 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100406 return serial_tstc();
wdenk47d1a6e2002-11-03 00:01:44 +0000407}
408
Siarhei Siamashka27669662015-01-08 09:02:31 +0200409#define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
410#define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
411
Simon Glass3fa49772012-03-19 21:59:14 -0700412#ifdef CONFIG_PRE_CONSOLE_BUFFER
Graeme Russ9558b482011-09-01 00:48:27 +0000413#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
414
415static void pre_console_putc(const char c)
416{
417 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
418
419 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
420}
421
422static void pre_console_puts(const char *s)
423{
424 while (*s)
425 pre_console_putc(*s++);
426}
427
Siarhei Siamashka27669662015-01-08 09:02:31 +0200428static void print_pre_console_buffer(int flushpoint)
Graeme Russ9558b482011-09-01 00:48:27 +0000429{
Hans de Goedea8552c72015-05-05 13:13:36 +0200430 unsigned long in = 0, out = 0;
431 char *buf_in = (char *)CONFIG_PRE_CON_BUF_ADDR;
432 char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
Graeme Russ9558b482011-09-01 00:48:27 +0000433
434 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
Hans de Goedea8552c72015-05-05 13:13:36 +0200435 in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
Graeme Russ9558b482011-09-01 00:48:27 +0000436
Hans de Goedea8552c72015-05-05 13:13:36 +0200437 while (in < gd->precon_buf_idx)
438 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
439
440 buf_out[out] = 0;
441
442 switch (flushpoint) {
443 case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
444 puts(buf_out);
445 break;
446 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
447 console_puts_noserial(stdout, buf_out);
448 break;
449 }
Graeme Russ9558b482011-09-01 00:48:27 +0000450}
451#else
452static inline void pre_console_putc(const char c) {}
453static inline void pre_console_puts(const char *s) {}
Siarhei Siamashka27669662015-01-08 09:02:31 +0200454static inline void print_pre_console_buffer(int flushpoint) {}
Graeme Russ9558b482011-09-01 00:48:27 +0000455#endif
456
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100457void putc(const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000458{
Simon Glass91b136c2013-11-10 10:27:01 -0700459#ifdef CONFIG_SANDBOX
Simon Glassda229e42015-06-23 15:38:34 -0600460 /* sandbox can send characters to stdout before it has a console */
Simon Glass093f79a2014-07-23 06:55:07 -0600461 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
Simon Glass91b136c2013-11-10 10:27:01 -0700462 os_putc(c);
463 return;
464 }
465#endif
Simon Glassd6ea5302015-06-23 15:38:33 -0600466#ifdef CONFIG_DEBUG_UART
467 /* if we don't have a console yet, use the debug UART */
468 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
469 printch(c);
470 return;
471 }
472#endif
wdenka6cccae2004-02-06 21:48:22 +0000473#ifdef CONFIG_SILENT_CONSOLE
474 if (gd->flags & GD_FLG_SILENT)
wdenkf6e20fc2004-02-08 19:38:38 +0000475 return;
wdenka6cccae2004-02-06 21:48:22 +0000476#endif
477
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100478#ifdef CONFIG_DISABLE_CONSOLE
479 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
480 return;
481#endif
482
Graeme Russe3e454c2011-08-29 02:14:05 +0000483 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000484 return pre_console_putc(c);
Graeme Russe3e454c2011-08-29 02:14:05 +0000485
wdenk47d1a6e2002-11-03 00:01:44 +0000486 if (gd->flags & GD_FLG_DEVINIT) {
487 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100488 fputc(stdout, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000489 } else {
490 /* Send directly to the handler */
Siarhei Siamashka27669662015-01-08 09:02:31 +0200491 pre_console_putc(c);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100492 serial_putc(c);
wdenk47d1a6e2002-11-03 00:01:44 +0000493 }
494}
495
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100496void puts(const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000497{
Simon Glass91b136c2013-11-10 10:27:01 -0700498#ifdef CONFIG_SANDBOX
Simon Glass093f79a2014-07-23 06:55:07 -0600499 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
Simon Glass91b136c2013-11-10 10:27:01 -0700500 os_puts(s);
501 return;
502 }
503#endif
Simon Glassd6ea5302015-06-23 15:38:33 -0600504#ifdef CONFIG_DEBUG_UART
505 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
506 while (*s) {
507 int ch = *s++;
Simon Glass91b136c2013-11-10 10:27:01 -0700508
Simon Glassd6ea5302015-06-23 15:38:33 -0600509 printch(ch);
510 if (ch == '\n')
511 printch('\r');
512 }
513 return;
514 }
515#endif
wdenka6cccae2004-02-06 21:48:22 +0000516#ifdef CONFIG_SILENT_CONSOLE
517 if (gd->flags & GD_FLG_SILENT)
518 return;
519#endif
520
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100521#ifdef CONFIG_DISABLE_CONSOLE
522 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
523 return;
524#endif
525
Graeme Russe3e454c2011-08-29 02:14:05 +0000526 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000527 return pre_console_puts(s);
Graeme Russe3e454c2011-08-29 02:14:05 +0000528
wdenk47d1a6e2002-11-03 00:01:44 +0000529 if (gd->flags & GD_FLG_DEVINIT) {
530 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100531 fputs(stdout, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000532 } else {
533 /* Send directly to the handler */
Siarhei Siamashka27669662015-01-08 09:02:31 +0200534 pre_console_puts(s);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100535 serial_puts(s);
wdenk47d1a6e2002-11-03 00:01:44 +0000536 }
537}
538
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200539int printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000540{
541 va_list args;
542 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200543 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000544
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100545 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000546
547 /* For this to work, printbuffer must be larger than
548 * anything we ever want to print.
549 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000550 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100551 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000552
553 /* Print the string */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100554 puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200555 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000556}
557
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200558int vprintf(const char *fmt, va_list args)
wdenk6dd652f2003-06-19 23:40:20 +0000559{
560 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200561 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk6dd652f2003-06-19 23:40:20 +0000562
Simon Glass7793ac92014-07-23 06:55:06 -0600563#if defined(CONFIG_PRE_CONSOLE_BUFFER) && !defined(CONFIG_SANDBOX)
Graeme Russe3e454c2011-08-29 02:14:05 +0000564 if (!gd->have_console)
565 return 0;
Graeme Russ9558b482011-09-01 00:48:27 +0000566#endif
Graeme Russe3e454c2011-08-29 02:14:05 +0000567
wdenk6dd652f2003-06-19 23:40:20 +0000568 /* For this to work, printbuffer must be larger than
569 * anything we ever want to print.
570 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000571 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
wdenk6dd652f2003-06-19 23:40:20 +0000572
573 /* Print the string */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100574 puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200575 return i;
wdenk6dd652f2003-06-19 23:40:20 +0000576}
577
wdenk47d1a6e2002-11-03 00:01:44 +0000578/* test if ctrl-c was pressed */
579static int ctrlc_disabled = 0; /* see disable_ctrl() */
580static int ctrlc_was_pressed = 0;
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100581int ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000582{
Simon Glass8969ea32014-09-14 12:40:15 -0600583#ifndef CONFIG_SANDBOX
wdenk47d1a6e2002-11-03 00:01:44 +0000584 if (!ctrlc_disabled && gd->have_console) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100585 if (tstc()) {
586 switch (getc()) {
wdenk47d1a6e2002-11-03 00:01:44 +0000587 case 0x03: /* ^C - Control C */
588 ctrlc_was_pressed = 1;
589 return 1;
590 default:
591 break;
592 }
593 }
594 }
Simon Glass8969ea32014-09-14 12:40:15 -0600595#endif
596
wdenk47d1a6e2002-11-03 00:01:44 +0000597 return 0;
598}
Pierre Auberta5dffa42014-04-24 10:30:07 +0200599/* Reads user's confirmation.
600 Returns 1 if user's input is "y", "Y", "yes" or "YES"
601*/
602int confirm_yesno(void)
603{
604 int i;
605 char str_input[5];
wdenk47d1a6e2002-11-03 00:01:44 +0000606
Pierre Auberta5dffa42014-04-24 10:30:07 +0200607 /* Flush input */
608 while (tstc())
609 getc();
610 i = 0;
611 while (i < sizeof(str_input)) {
612 str_input[i] = getc();
613 putc(str_input[i]);
614 if (str_input[i] == '\r')
615 break;
616 i++;
617 }
618 putc('\n');
619 if (strncmp(str_input, "y\r", 2) == 0 ||
620 strncmp(str_input, "Y\r", 2) == 0 ||
621 strncmp(str_input, "yes\r", 4) == 0 ||
622 strncmp(str_input, "YES\r", 4) == 0)
623 return 1;
624 return 0;
625}
wdenk47d1a6e2002-11-03 00:01:44 +0000626/* pass 1 to disable ctrlc() checking, 0 to enable.
627 * returns previous state
628 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100629int disable_ctrlc(int disable)
wdenk47d1a6e2002-11-03 00:01:44 +0000630{
631 int prev = ctrlc_disabled; /* save previous state */
632
633 ctrlc_disabled = disable;
634 return prev;
635}
636
637int had_ctrlc (void)
638{
639 return ctrlc_was_pressed;
640}
641
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100642void clear_ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000643{
644 ctrlc_was_pressed = 0;
645}
646
647#ifdef CONFIG_MODEM_SUPPORT_DEBUG
648char screen[1024];
649char *cursor = screen;
650int once = 0;
651inline void dbg(const char *fmt, ...)
652{
653 va_list args;
654 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200655 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000656
657 if (!once) {
658 memset(screen, 0, sizeof(screen));
659 once++;
660 }
661
662 va_start(args, fmt);
663
664 /* For this to work, printbuffer must be larger than
665 * anything we ever want to print.
666 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000667 i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
wdenk47d1a6e2002-11-03 00:01:44 +0000668 va_end(args);
669
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100670 if ((screen + sizeof(screen) - 1 - cursor)
671 < strlen(printbuffer) + 1) {
wdenk47d1a6e2002-11-03 00:01:44 +0000672 memset(screen, 0, sizeof(screen));
673 cursor = screen;
674 }
675 sprintf(cursor, printbuffer);
676 cursor += strlen(printbuffer);
677
678}
679#else
Jeroen Hofstee482f4692014-10-08 22:57:48 +0200680static inline void dbg(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000681{
682}
683#endif
684
685/** U-Boot INIT FUNCTIONS *************************************************/
686
Mike Frysingerd7be3052010-10-20 07:18:03 -0400687struct stdio_dev *search_device(int flags, const char *name)
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200688{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200689 struct stdio_dev *dev;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200690
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200691 dev = stdio_get_by_name(name);
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200692
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100693 if (dev && (dev->flags & flags))
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200694 return dev;
695
696 return NULL;
697}
698
Mike Frysingerd7be3052010-10-20 07:18:03 -0400699int console_assign(int file, const char *devname)
wdenk47d1a6e2002-11-03 00:01:44 +0000700{
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200701 int flag;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200702 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000703
704 /* Check for valid file */
705 switch (file) {
706 case stdin:
707 flag = DEV_FLAGS_INPUT;
708 break;
709 case stdout:
710 case stderr:
711 flag = DEV_FLAGS_OUTPUT;
712 break;
713 default:
714 return -1;
715 }
716
717 /* Check for valid device name */
718
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200719 dev = search_device(flag, devname);
wdenk47d1a6e2002-11-03 00:01:44 +0000720
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100721 if (dev)
722 return console_setfile(file, dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000723
724 return -1;
725}
726
727/* Called before relocation - use serial functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100728int console_init_f(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000729{
wdenk47d1a6e2002-11-03 00:01:44 +0000730 gd->have_console = 1;
wdenkf72da342003-10-10 10:05:42 +0000731
732#ifdef CONFIG_SILENT_CONSOLE
733 if (getenv("silent") != NULL)
734 gd->flags |= GD_FLG_SILENT;
735#endif
736
Siarhei Siamashka27669662015-01-08 09:02:31 +0200737 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
Graeme Russ9558b482011-09-01 00:48:27 +0000738
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100739 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000740}
741
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200742void stdio_print_current_devices(void)
743{
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200744 /* Print information */
745 puts("In: ");
746 if (stdio_devices[stdin] == NULL) {
747 puts("No input devices available!\n");
748 } else {
749 printf ("%s\n", stdio_devices[stdin]->name);
750 }
751
752 puts("Out: ");
753 if (stdio_devices[stdout] == NULL) {
754 puts("No output devices available!\n");
755 } else {
756 printf ("%s\n", stdio_devices[stdout]->name);
757 }
758
759 puts("Err: ");
760 if (stdio_devices[stderr] == NULL) {
761 puts("No error devices available!\n");
762 } else {
763 printf ("%s\n", stdio_devices[stderr]->name);
764 }
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200765}
766
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200767#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +0000768/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100769int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000770{
771 char *stdinname, *stdoutname, *stderrname;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200772 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200773#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk6e592382004-04-18 17:39:38 +0000774 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200775#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100776#ifdef CONFIG_CONSOLE_MUX
777 int iomux_err = 0;
778#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000779
780 /* set default handlers at first */
Martin Dorwig49cad542015-01-26 15:22:54 -0700781 gd->jt->getc = serial_getc;
782 gd->jt->tstc = serial_tstc;
783 gd->jt->putc = serial_putc;
784 gd->jt->puts = serial_puts;
785 gd->jt->printf = serial_printf;
wdenk47d1a6e2002-11-03 00:01:44 +0000786
787 /* stdin stdout and stderr are in environment */
788 /* scan for it */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100789 stdinname = getenv("stdin");
790 stdoutname = getenv("stdout");
791 stderrname = getenv("stderr");
wdenk47d1a6e2002-11-03 00:01:44 +0000792
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200793 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100794 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
795 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
796 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100797#ifdef CONFIG_CONSOLE_MUX
798 iomux_err = iomux_doenv(stdin, stdinname);
799 iomux_err += iomux_doenv(stdout, stdoutname);
800 iomux_err += iomux_doenv(stderr, stderrname);
801 if (!iomux_err)
802 /* Successful, so skip all the code below. */
803 goto done;
804#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000805 }
806 /* if the devices are overwritten or not found, use default device */
807 if (inputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100808 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000809 }
810 if (outputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100811 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000812 }
813 if (errdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100814 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000815 }
816 /* Initializes output console first */
817 if (outputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100818 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100819 console_doenv(stdout, outputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000820 }
821 if (errdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100822 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100823 console_doenv(stderr, errdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000824 }
825 if (inputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100826 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100827 console_doenv(stdin, inputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000828 }
829
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100830#ifdef CONFIG_CONSOLE_MUX
831done:
832#endif
833
Simon Glass78c112c2012-12-05 14:46:43 +0000834#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200835 stdio_print_current_devices();
Simon Glass78c112c2012-12-05 14:46:43 +0000836#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +0000837
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200838#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk47d1a6e2002-11-03 00:01:44 +0000839 /* set the environment variables (will overwrite previous env settings) */
840 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100841 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000842 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200843#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
wdenk47d1a6e2002-11-03 00:01:44 +0000844
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600845 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
846
wdenk47d1a6e2002-11-03 00:01:44 +0000847#if 0
848 /* If nothing usable installed, use only the initial console */
849 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100850 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000851#endif
Siarhei Siamashka27669662015-01-08 09:02:31 +0200852 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100853 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000854}
855
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200856#else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +0000857
858/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100859int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000860{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200861 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200862 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200863 struct list_head *list = stdio_get_list();
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200864 struct list_head *pos;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200865 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000866
wdenkd791b1d2003-04-20 14:04:18 +0000867#ifdef CONFIG_SPLASH_SCREEN
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100868 /*
869 * suppress all output if splash screen is enabled and we have
Anatolij Gustschina7490812010-03-16 15:29:33 +0100870 * a bmp to display. We redirect the output from frame buffer
871 * console to serial console in this case or suppress it if
872 * "silent" mode was requested.
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100873 */
Anatolij Gustschina7490812010-03-16 15:29:33 +0100874 if (getenv("splashimage") != NULL) {
875 if (!(gd->flags & GD_FLG_SILENT))
876 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
877 }
wdenkf72da342003-10-10 10:05:42 +0000878#endif
879
wdenk47d1a6e2002-11-03 00:01:44 +0000880 /* Scan devices looking for input and output devices */
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200881 list_for_each(pos, list) {
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200882 dev = list_entry(pos, struct stdio_dev, list);
wdenk47d1a6e2002-11-03 00:01:44 +0000883
884 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
885 inputdev = dev;
886 }
887 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
888 outputdev = dev;
889 }
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200890 if(inputdev && outputdev)
891 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000892 }
893
894 /* Initializes output console first */
895 if (outputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100896 console_setfile(stdout, outputdev);
897 console_setfile(stderr, outputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100898#ifdef CONFIG_CONSOLE_MUX
899 console_devices[stdout][0] = outputdev;
900 console_devices[stderr][0] = outputdev;
901#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000902 }
903
904 /* Initializes input console */
905 if (inputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100906 console_setfile(stdin, inputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100907#ifdef CONFIG_CONSOLE_MUX
908 console_devices[stdin][0] = inputdev;
909#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000910 }
911
Simon Glass78c112c2012-12-05 14:46:43 +0000912#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200913 stdio_print_current_devices();
Simon Glass78c112c2012-12-05 14:46:43 +0000914#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +0000915
916 /* Setting environment variables */
917 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100918 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000919 }
920
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600921 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
922
wdenk47d1a6e2002-11-03 00:01:44 +0000923#if 0
924 /* If nothing usable installed, use only the initial console */
925 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100926 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000927#endif
Siarhei Siamashka27669662015-01-08 09:02:31 +0200928 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100929 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000930}
931
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200932#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */