blob: ace206ca4ff5dd87651c32da857fae762916f41c [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 Glassd6ea5302015-06-23 15:38:33 -06009#include <debug_uart.h>
wdenk47d1a6e2002-11-03 00:01:44 +000010#include <stdarg.h>
Jeroen Hofstee482f4692014-10-08 22:57:48 +020011#include <iomux.h>
wdenk47d1a6e2002-11-03 00:01:44 +000012#include <malloc.h>
Simon Glass91b136c2013-11-10 10:27:01 -070013#include <os.h>
Joe Hershberger849d5d92012-12-11 22:16:29 -060014#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020015#include <stdio_dev.h>
wdenk27b207f2003-07-24 23:38:38 +000016#include <exports.h>
Joe Hershberger849d5d92012-12-11 22:16:29 -060017#include <environment.h>
wdenk47d1a6e2002-11-03 00:01:44 +000018
Wolfgang Denkd87080b2006-03-31 18:32:53 +020019DECLARE_GLOBAL_DATA_PTR;
20
Joe Hershberger849d5d92012-12-11 22:16:29 -060021static int on_console(const char *name, const char *value, enum env_op op,
22 int flags)
23{
24 int console = -1;
25
26 /* Check for console redirection */
27 if (strcmp(name, "stdin") == 0)
28 console = stdin;
29 else if (strcmp(name, "stdout") == 0)
30 console = stdout;
31 else if (strcmp(name, "stderr") == 0)
32 console = stderr;
33
34 /* if not actually setting a console variable, we don't care */
35 if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
36 return 0;
37
38 switch (op) {
39 case env_op_create:
40 case env_op_overwrite:
41
42#ifdef CONFIG_CONSOLE_MUX
43 if (iomux_doenv(console, value))
44 return 1;
45#else
46 /* Try assigning specified device */
47 if (console_assign(console, value) < 0)
48 return 1;
49#endif /* CONFIG_CONSOLE_MUX */
50 return 0;
51
52 case env_op_delete:
53 if ((flags & H_FORCE) == 0)
54 printf("Can't delete \"%s\"\n", name);
55 return 1;
56
57 default:
58 return 0;
59 }
60}
61U_BOOT_ENV_CALLBACK(console, on_console);
62
Joe Hershbergere080d542012-12-11 22:16:30 -060063#ifdef CONFIG_SILENT_CONSOLE
64static int on_silent(const char *name, const char *value, enum env_op op,
65 int flags)
66{
67#ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
68 if (flags & H_INTERACTIVE)
69 return 0;
70#endif
71#ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
72 if ((flags & H_INTERACTIVE) == 0)
73 return 0;
74#endif
75
76 if (value != NULL)
77 gd->flags |= GD_FLG_SILENT;
78 else
79 gd->flags &= ~GD_FLG_SILENT;
80
81 return 0;
82}
83U_BOOT_ENV_CALLBACK(silent, on_silent);
84#endif
85
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020086#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +000087/*
88 * if overwrite_console returns 1, the stdin, stderr and stdout
89 * are switched to the serial port, else the settings in the
90 * environment are used
91 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020092#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +010093extern int overwrite_console(void);
94#define OVERWRITE_CONSOLE overwrite_console()
wdenk47d1a6e2002-11-03 00:01:44 +000095#else
wdenk83e40ba2005-03-31 18:42:15 +000096#define OVERWRITE_CONSOLE 0
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020097#endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
wdenk47d1a6e2002-11-03 00:01:44 +000098
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020099#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +0000100
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200101static int console_setfile(int file, struct stdio_dev * dev)
wdenk47d1a6e2002-11-03 00:01:44 +0000102{
103 int error = 0;
104
105 if (dev == NULL)
106 return -1;
107
108 switch (file) {
109 case stdin:
110 case stdout:
111 case stderr:
112 /* Start new device */
113 if (dev->start) {
Simon Glass709ea542014-07-23 06:54:59 -0600114 error = dev->start(dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000115 /* If it's not started dont use it */
116 if (error < 0)
117 break;
118 }
119
120 /* Assign the new device (leaving the existing one started) */
121 stdio_devices[file] = dev;
122
123 /*
124 * Update monitor functions
125 * (to use the console stuff by other applications)
126 */
127 switch (file) {
128 case stdin:
Martin Dorwig49cad542015-01-26 15:22:54 -0700129 gd->jt->getc = getc;
130 gd->jt->tstc = tstc;
wdenk47d1a6e2002-11-03 00:01:44 +0000131 break;
132 case stdout:
Martin Dorwig49cad542015-01-26 15:22:54 -0700133 gd->jt->putc = putc;
134 gd->jt->puts = puts;
135 gd->jt->printf = printf;
wdenk47d1a6e2002-11-03 00:01:44 +0000136 break;
137 }
138 break;
139
140 default: /* Invalid file ID */
141 error = -1;
142 }
143 return error;
144}
145
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100146#if defined(CONFIG_CONSOLE_MUX)
147/** Console I/O multiplexing *******************************************/
148
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200149static struct stdio_dev *tstcdev;
150struct stdio_dev **console_devices[MAX_FILES];
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100151int cd_count[MAX_FILES];
152
153/*
154 * This depends on tstc() always being called before getc().
155 * This is guaranteed to be true because this routine is called
156 * only from fgetc() which assures it.
157 * No attempt is made to demultiplex multiple input sources.
158 */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100159static int console_getc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100160{
161 unsigned char ret;
162
163 /* This is never called with testcdev == NULL */
Simon Glass709ea542014-07-23 06:54:59 -0600164 ret = tstcdev->getc(tstcdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100165 tstcdev = NULL;
166 return ret;
167}
168
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100169static int console_tstc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100170{
171 int i, ret;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200172 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100173
174 disable_ctrlc(1);
175 for (i = 0; i < cd_count[file]; i++) {
176 dev = console_devices[file][i];
177 if (dev->tstc != NULL) {
Simon Glass709ea542014-07-23 06:54:59 -0600178 ret = dev->tstc(dev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100179 if (ret > 0) {
180 tstcdev = dev;
181 disable_ctrlc(0);
182 return ret;
183 }
184 }
185 }
186 disable_ctrlc(0);
187
188 return 0;
189}
190
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100191static void console_putc(int file, const char c)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100192{
193 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200194 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100195
196 for (i = 0; i < cd_count[file]; i++) {
197 dev = console_devices[file][i];
198 if (dev->putc != NULL)
Simon Glass709ea542014-07-23 06:54:59 -0600199 dev->putc(dev, c);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100200 }
201}
202
Siarhei Siamashka27669662015-01-08 09:02:31 +0200203#ifdef CONFIG_PRE_CONSOLE_BUFFER
Hans de Goedea8552c72015-05-05 13:13:36 +0200204static void console_puts_noserial(int file, const char *s)
Siarhei Siamashka27669662015-01-08 09:02:31 +0200205{
206 int i;
207 struct stdio_dev *dev;
208
209 for (i = 0; i < cd_count[file]; i++) {
210 dev = console_devices[file][i];
Hans de Goedea8552c72015-05-05 13:13:36 +0200211 if (dev->puts != NULL && strcmp(dev->name, "serial") != 0)
212 dev->puts(dev, s);
Siarhei Siamashka27669662015-01-08 09:02:31 +0200213 }
214}
215#endif
216
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100217static void console_puts(int file, const char *s)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100218{
219 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200220 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100221
222 for (i = 0; i < cd_count[file]; i++) {
223 dev = console_devices[file][i];
224 if (dev->puts != NULL)
Simon Glass709ea542014-07-23 06:54:59 -0600225 dev->puts(dev, s);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100226 }
227}
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100228
229static inline void console_printdevs(int file)
230{
231 iomux_printdevs(file);
232}
233
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200234static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100235{
236 iomux_doenv(file, dev->name);
237}
238#else
239static inline int console_getc(int file)
240{
Simon Glass709ea542014-07-23 06:54:59 -0600241 return stdio_devices[file]->getc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100242}
243
244static inline int console_tstc(int file)
245{
Simon Glass709ea542014-07-23 06:54:59 -0600246 return stdio_devices[file]->tstc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100247}
248
249static inline void console_putc(int file, const char c)
250{
Simon Glass709ea542014-07-23 06:54:59 -0600251 stdio_devices[file]->putc(stdio_devices[file], c);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100252}
253
Siarhei Siamashka27669662015-01-08 09:02:31 +0200254#ifdef CONFIG_PRE_CONSOLE_BUFFER
Hans de Goedea8552c72015-05-05 13:13:36 +0200255static inline void console_puts_noserial(int file, const char *s)
Siarhei Siamashka27669662015-01-08 09:02:31 +0200256{
257 if (strcmp(stdio_devices[file]->name, "serial") != 0)
Hans de Goedea8552c72015-05-05 13:13:36 +0200258 stdio_devices[file]->puts(stdio_devices[file], s);
Siarhei Siamashka27669662015-01-08 09:02:31 +0200259}
260#endif
261
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100262static inline void console_puts(int file, const char *s)
263{
Simon Glass709ea542014-07-23 06:54:59 -0600264 stdio_devices[file]->puts(stdio_devices[file], s);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100265}
266
267static inline void console_printdevs(int file)
268{
269 printf("%s\n", stdio_devices[file]->name);
270}
271
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200272static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100273{
274 console_setfile(file, dev);
275}
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100276#endif /* defined(CONFIG_CONSOLE_MUX) */
277
wdenk47d1a6e2002-11-03 00:01:44 +0000278/** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
279
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200280int serial_printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000281{
282 va_list args;
283 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200284 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000285
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100286 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000287
288 /* For this to work, printbuffer must be larger than
289 * anything we ever want to print.
290 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000291 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100292 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000293
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100294 serial_puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200295 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000296}
297
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100298int fgetc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000299{
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100300 if (file < MAX_FILES) {
301#if defined(CONFIG_CONSOLE_MUX)
302 /*
303 * Effectively poll for input wherever it may be available.
304 */
305 for (;;) {
306 /*
307 * Upper layer may have already called tstc() so
308 * check for that first.
309 */
310 if (tstcdev != NULL)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100311 return console_getc(file);
312 console_tstc(file);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100313#ifdef CONFIG_WATCHDOG
314 /*
315 * If the watchdog must be rate-limited then it should
316 * already be handled in board-specific code.
317 */
318 udelay(1);
319#endif
320 }
321#else
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100322 return console_getc(file);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100323#endif
324 }
wdenk47d1a6e2002-11-03 00:01:44 +0000325
326 return -1;
327}
328
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100329int ftstc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000330{
331 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100332 return console_tstc(file);
wdenk47d1a6e2002-11-03 00:01:44 +0000333
334 return -1;
335}
336
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100337void fputc(int file, const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000338{
339 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100340 console_putc(file, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000341}
342
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100343void fputs(int file, const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000344{
345 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100346 console_puts(file, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000347}
348
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200349int fprintf(int file, const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000350{
351 va_list args;
352 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200353 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000354
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100355 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000356
357 /* For this to work, printbuffer must be larger than
358 * anything we ever want to print.
359 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000360 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100361 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000362
363 /* Send to desired file */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100364 fputs(file, printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200365 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000366}
367
368/** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
369
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100370int getc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000371{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100372#ifdef CONFIG_DISABLE_CONSOLE
373 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
374 return 0;
375#endif
376
Graeme Russe3e454c2011-08-29 02:14:05 +0000377 if (!gd->have_console)
378 return 0;
379
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;
398
wdenk47d1a6e2002-11-03 00:01:44 +0000399 if (gd->flags & GD_FLG_DEVINIT) {
400 /* Test the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100401 return ftstc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000402 }
403
404 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100405 return serial_tstc();
wdenk47d1a6e2002-11-03 00:01:44 +0000406}
407
Siarhei Siamashka27669662015-01-08 09:02:31 +0200408#define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
409#define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
410
Simon Glass3fa49772012-03-19 21:59:14 -0700411#ifdef CONFIG_PRE_CONSOLE_BUFFER
Graeme Russ9558b482011-09-01 00:48:27 +0000412#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
413
414static void pre_console_putc(const char c)
415{
416 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
417
418 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
419}
420
421static void pre_console_puts(const char *s)
422{
423 while (*s)
424 pre_console_putc(*s++);
425}
426
Siarhei Siamashka27669662015-01-08 09:02:31 +0200427static void print_pre_console_buffer(int flushpoint)
Graeme Russ9558b482011-09-01 00:48:27 +0000428{
Hans de Goedea8552c72015-05-05 13:13:36 +0200429 unsigned long in = 0, out = 0;
430 char *buf_in = (char *)CONFIG_PRE_CON_BUF_ADDR;
431 char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
Graeme Russ9558b482011-09-01 00:48:27 +0000432
433 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
Hans de Goedea8552c72015-05-05 13:13:36 +0200434 in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
Graeme Russ9558b482011-09-01 00:48:27 +0000435
Hans de Goedea8552c72015-05-05 13:13:36 +0200436 while (in < gd->precon_buf_idx)
437 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
438
439 buf_out[out] = 0;
440
441 switch (flushpoint) {
442 case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
443 puts(buf_out);
444 break;
445 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
446 console_puts_noserial(stdout, buf_out);
447 break;
448 }
Graeme Russ9558b482011-09-01 00:48:27 +0000449}
450#else
451static inline void pre_console_putc(const char c) {}
452static inline void pre_console_puts(const char *s) {}
Siarhei Siamashka27669662015-01-08 09:02:31 +0200453static inline void print_pre_console_buffer(int flushpoint) {}
Graeme Russ9558b482011-09-01 00:48:27 +0000454#endif
455
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100456void putc(const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000457{
Simon Glass91b136c2013-11-10 10:27:01 -0700458#ifdef CONFIG_SANDBOX
Simon Glassda229e42015-06-23 15:38:34 -0600459 /* sandbox can send characters to stdout before it has a console */
Simon Glass093f79a2014-07-23 06:55:07 -0600460 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
Simon Glass91b136c2013-11-10 10:27:01 -0700461 os_putc(c);
462 return;
463 }
464#endif
Simon Glassd6ea5302015-06-23 15:38:33 -0600465#ifdef CONFIG_DEBUG_UART
466 /* if we don't have a console yet, use the debug UART */
467 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
468 printch(c);
469 return;
470 }
471#endif
wdenka6cccae2004-02-06 21:48:22 +0000472#ifdef CONFIG_SILENT_CONSOLE
473 if (gd->flags & GD_FLG_SILENT)
wdenkf6e20fc2004-02-08 19:38:38 +0000474 return;
wdenka6cccae2004-02-06 21:48:22 +0000475#endif
476
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100477#ifdef CONFIG_DISABLE_CONSOLE
478 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
479 return;
480#endif
481
Graeme Russe3e454c2011-08-29 02:14:05 +0000482 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000483 return pre_console_putc(c);
Graeme Russe3e454c2011-08-29 02:14:05 +0000484
wdenk47d1a6e2002-11-03 00:01:44 +0000485 if (gd->flags & GD_FLG_DEVINIT) {
486 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100487 fputc(stdout, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000488 } else {
489 /* Send directly to the handler */
Siarhei Siamashka27669662015-01-08 09:02:31 +0200490 pre_console_putc(c);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100491 serial_putc(c);
wdenk47d1a6e2002-11-03 00:01:44 +0000492 }
493}
494
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100495void puts(const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000496{
Simon Glass91b136c2013-11-10 10:27:01 -0700497#ifdef CONFIG_SANDBOX
Simon Glass093f79a2014-07-23 06:55:07 -0600498 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
Simon Glass91b136c2013-11-10 10:27:01 -0700499 os_puts(s);
500 return;
501 }
502#endif
Simon Glassd6ea5302015-06-23 15:38:33 -0600503#ifdef CONFIG_DEBUG_UART
504 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
505 while (*s) {
506 int ch = *s++;
Simon Glass91b136c2013-11-10 10:27:01 -0700507
Simon Glassd6ea5302015-06-23 15:38:33 -0600508 printch(ch);
509 if (ch == '\n')
510 printch('\r');
511 }
512 return;
513 }
514#endif
wdenka6cccae2004-02-06 21:48:22 +0000515#ifdef CONFIG_SILENT_CONSOLE
516 if (gd->flags & GD_FLG_SILENT)
517 return;
518#endif
519
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100520#ifdef CONFIG_DISABLE_CONSOLE
521 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
522 return;
523#endif
524
Graeme Russe3e454c2011-08-29 02:14:05 +0000525 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000526 return pre_console_puts(s);
Graeme Russe3e454c2011-08-29 02:14:05 +0000527
wdenk47d1a6e2002-11-03 00:01:44 +0000528 if (gd->flags & GD_FLG_DEVINIT) {
529 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100530 fputs(stdout, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000531 } else {
532 /* Send directly to the handler */
Siarhei Siamashka27669662015-01-08 09:02:31 +0200533 pre_console_puts(s);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100534 serial_puts(s);
wdenk47d1a6e2002-11-03 00:01:44 +0000535 }
536}
537
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200538int printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000539{
540 va_list args;
541 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200542 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000543
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100544 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000545
546 /* For this to work, printbuffer must be larger than
547 * anything we ever want to print.
548 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000549 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100550 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000551
552 /* Print the string */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100553 puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200554 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000555}
556
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200557int vprintf(const char *fmt, va_list args)
wdenk6dd652f2003-06-19 23:40:20 +0000558{
559 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200560 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk6dd652f2003-06-19 23:40:20 +0000561
Simon Glass7793ac92014-07-23 06:55:06 -0600562#if defined(CONFIG_PRE_CONSOLE_BUFFER) && !defined(CONFIG_SANDBOX)
Graeme Russe3e454c2011-08-29 02:14:05 +0000563 if (!gd->have_console)
564 return 0;
Graeme Russ9558b482011-09-01 00:48:27 +0000565#endif
Graeme Russe3e454c2011-08-29 02:14:05 +0000566
wdenk6dd652f2003-06-19 23:40:20 +0000567 /* For this to work, printbuffer must be larger than
568 * anything we ever want to print.
569 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000570 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
wdenk6dd652f2003-06-19 23:40:20 +0000571
572 /* Print the string */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100573 puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200574 return i;
wdenk6dd652f2003-06-19 23:40:20 +0000575}
576
wdenk47d1a6e2002-11-03 00:01:44 +0000577/* test if ctrl-c was pressed */
578static int ctrlc_disabled = 0; /* see disable_ctrl() */
579static int ctrlc_was_pressed = 0;
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100580int ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000581{
Simon Glass8969ea32014-09-14 12:40:15 -0600582#ifndef CONFIG_SANDBOX
wdenk47d1a6e2002-11-03 00:01:44 +0000583 if (!ctrlc_disabled && gd->have_console) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100584 if (tstc()) {
585 switch (getc()) {
wdenk47d1a6e2002-11-03 00:01:44 +0000586 case 0x03: /* ^C - Control C */
587 ctrlc_was_pressed = 1;
588 return 1;
589 default:
590 break;
591 }
592 }
593 }
Simon Glass8969ea32014-09-14 12:40:15 -0600594#endif
595
wdenk47d1a6e2002-11-03 00:01:44 +0000596 return 0;
597}
Pierre Auberta5dffa42014-04-24 10:30:07 +0200598/* Reads user's confirmation.
599 Returns 1 if user's input is "y", "Y", "yes" or "YES"
600*/
601int confirm_yesno(void)
602{
603 int i;
604 char str_input[5];
wdenk47d1a6e2002-11-03 00:01:44 +0000605
Pierre Auberta5dffa42014-04-24 10:30:07 +0200606 /* Flush input */
607 while (tstc())
608 getc();
609 i = 0;
610 while (i < sizeof(str_input)) {
611 str_input[i] = getc();
612 putc(str_input[i]);
613 if (str_input[i] == '\r')
614 break;
615 i++;
616 }
617 putc('\n');
618 if (strncmp(str_input, "y\r", 2) == 0 ||
619 strncmp(str_input, "Y\r", 2) == 0 ||
620 strncmp(str_input, "yes\r", 4) == 0 ||
621 strncmp(str_input, "YES\r", 4) == 0)
622 return 1;
623 return 0;
624}
wdenk47d1a6e2002-11-03 00:01:44 +0000625/* pass 1 to disable ctrlc() checking, 0 to enable.
626 * returns previous state
627 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100628int disable_ctrlc(int disable)
wdenk47d1a6e2002-11-03 00:01:44 +0000629{
630 int prev = ctrlc_disabled; /* save previous state */
631
632 ctrlc_disabled = disable;
633 return prev;
634}
635
636int had_ctrlc (void)
637{
638 return ctrlc_was_pressed;
639}
640
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100641void clear_ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000642{
643 ctrlc_was_pressed = 0;
644}
645
646#ifdef CONFIG_MODEM_SUPPORT_DEBUG
647char screen[1024];
648char *cursor = screen;
649int once = 0;
650inline void dbg(const char *fmt, ...)
651{
652 va_list args;
653 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200654 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000655
656 if (!once) {
657 memset(screen, 0, sizeof(screen));
658 once++;
659 }
660
661 va_start(args, fmt);
662
663 /* For this to work, printbuffer must be larger than
664 * anything we ever want to print.
665 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000666 i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
wdenk47d1a6e2002-11-03 00:01:44 +0000667 va_end(args);
668
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100669 if ((screen + sizeof(screen) - 1 - cursor)
670 < strlen(printbuffer) + 1) {
wdenk47d1a6e2002-11-03 00:01:44 +0000671 memset(screen, 0, sizeof(screen));
672 cursor = screen;
673 }
674 sprintf(cursor, printbuffer);
675 cursor += strlen(printbuffer);
676
677}
678#else
Jeroen Hofstee482f4692014-10-08 22:57:48 +0200679static inline void dbg(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000680{
681}
682#endif
683
684/** U-Boot INIT FUNCTIONS *************************************************/
685
Mike Frysingerd7be3052010-10-20 07:18:03 -0400686struct stdio_dev *search_device(int flags, const char *name)
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200687{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200688 struct stdio_dev *dev;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200689
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200690 dev = stdio_get_by_name(name);
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200691
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100692 if (dev && (dev->flags & flags))
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200693 return dev;
694
695 return NULL;
696}
697
Mike Frysingerd7be3052010-10-20 07:18:03 -0400698int console_assign(int file, const char *devname)
wdenk47d1a6e2002-11-03 00:01:44 +0000699{
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200700 int flag;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200701 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000702
703 /* Check for valid file */
704 switch (file) {
705 case stdin:
706 flag = DEV_FLAGS_INPUT;
707 break;
708 case stdout:
709 case stderr:
710 flag = DEV_FLAGS_OUTPUT;
711 break;
712 default:
713 return -1;
714 }
715
716 /* Check for valid device name */
717
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200718 dev = search_device(flag, devname);
wdenk47d1a6e2002-11-03 00:01:44 +0000719
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100720 if (dev)
721 return console_setfile(file, dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000722
723 return -1;
724}
725
726/* Called before relocation - use serial functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100727int console_init_f(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000728{
wdenk47d1a6e2002-11-03 00:01:44 +0000729 gd->have_console = 1;
wdenkf72da342003-10-10 10:05:42 +0000730
731#ifdef CONFIG_SILENT_CONSOLE
732 if (getenv("silent") != NULL)
733 gd->flags |= GD_FLG_SILENT;
734#endif
735
Siarhei Siamashka27669662015-01-08 09:02:31 +0200736 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
Graeme Russ9558b482011-09-01 00:48:27 +0000737
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100738 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000739}
740
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200741void stdio_print_current_devices(void)
742{
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200743 /* Print information */
744 puts("In: ");
745 if (stdio_devices[stdin] == NULL) {
746 puts("No input devices available!\n");
747 } else {
748 printf ("%s\n", stdio_devices[stdin]->name);
749 }
750
751 puts("Out: ");
752 if (stdio_devices[stdout] == NULL) {
753 puts("No output devices available!\n");
754 } else {
755 printf ("%s\n", stdio_devices[stdout]->name);
756 }
757
758 puts("Err: ");
759 if (stdio_devices[stderr] == NULL) {
760 puts("No error devices available!\n");
761 } else {
762 printf ("%s\n", stdio_devices[stderr]->name);
763 }
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200764}
765
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200766#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk47d1a6e2002-11-03 00:01:44 +0000767/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100768int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000769{
770 char *stdinname, *stdoutname, *stderrname;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200771 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200772#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk6e592382004-04-18 17:39:38 +0000773 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200774#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100775#ifdef CONFIG_CONSOLE_MUX
776 int iomux_err = 0;
777#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000778
779 /* set default handlers at first */
Martin Dorwig49cad542015-01-26 15:22:54 -0700780 gd->jt->getc = serial_getc;
781 gd->jt->tstc = serial_tstc;
782 gd->jt->putc = serial_putc;
783 gd->jt->puts = serial_puts;
784 gd->jt->printf = serial_printf;
wdenk47d1a6e2002-11-03 00:01:44 +0000785
786 /* stdin stdout and stderr are in environment */
787 /* scan for it */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100788 stdinname = getenv("stdin");
789 stdoutname = getenv("stdout");
790 stderrname = getenv("stderr");
wdenk47d1a6e2002-11-03 00:01:44 +0000791
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200792 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100793 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
794 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
795 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100796#ifdef CONFIG_CONSOLE_MUX
797 iomux_err = iomux_doenv(stdin, stdinname);
798 iomux_err += iomux_doenv(stdout, stdoutname);
799 iomux_err += iomux_doenv(stderr, stderrname);
800 if (!iomux_err)
801 /* Successful, so skip all the code below. */
802 goto done;
803#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000804 }
805 /* if the devices are overwritten or not found, use default device */
806 if (inputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100807 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000808 }
809 if (outputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100810 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000811 }
812 if (errdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100813 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000814 }
815 /* Initializes output console first */
816 if (outputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100817 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100818 console_doenv(stdout, outputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000819 }
820 if (errdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100821 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100822 console_doenv(stderr, errdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000823 }
824 if (inputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100825 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100826 console_doenv(stdin, inputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000827 }
828
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100829#ifdef CONFIG_CONSOLE_MUX
830done:
831#endif
832
Simon Glass78c112c2012-12-05 14:46:43 +0000833#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200834 stdio_print_current_devices();
Simon Glass78c112c2012-12-05 14:46:43 +0000835#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +0000836
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200837#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk47d1a6e2002-11-03 00:01:44 +0000838 /* set the environment variables (will overwrite previous env settings) */
839 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100840 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000841 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200842#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
wdenk47d1a6e2002-11-03 00:01:44 +0000843
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600844 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
845
wdenk47d1a6e2002-11-03 00:01:44 +0000846#if 0
847 /* If nothing usable installed, use only the initial console */
848 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100849 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000850#endif
Siarhei Siamashka27669662015-01-08 09:02:31 +0200851 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100852 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000853}
854
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200855#else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk47d1a6e2002-11-03 00:01:44 +0000856
857/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100858int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000859{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200860 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200861 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200862 struct list_head *list = stdio_get_list();
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200863 struct list_head *pos;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200864 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000865
wdenkd791b1d2003-04-20 14:04:18 +0000866#ifdef CONFIG_SPLASH_SCREEN
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100867 /*
868 * suppress all output if splash screen is enabled and we have
Anatolij Gustschina7490812010-03-16 15:29:33 +0100869 * a bmp to display. We redirect the output from frame buffer
870 * console to serial console in this case or suppress it if
871 * "silent" mode was requested.
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100872 */
Anatolij Gustschina7490812010-03-16 15:29:33 +0100873 if (getenv("splashimage") != NULL) {
874 if (!(gd->flags & GD_FLG_SILENT))
875 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
876 }
wdenkf72da342003-10-10 10:05:42 +0000877#endif
878
wdenk47d1a6e2002-11-03 00:01:44 +0000879 /* Scan devices looking for input and output devices */
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200880 list_for_each(pos, list) {
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200881 dev = list_entry(pos, struct stdio_dev, list);
wdenk47d1a6e2002-11-03 00:01:44 +0000882
883 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
884 inputdev = dev;
885 }
886 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
887 outputdev = dev;
888 }
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200889 if(inputdev && outputdev)
890 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000891 }
892
893 /* Initializes output console first */
894 if (outputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100895 console_setfile(stdout, outputdev);
896 console_setfile(stderr, outputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100897#ifdef CONFIG_CONSOLE_MUX
898 console_devices[stdout][0] = outputdev;
899 console_devices[stderr][0] = outputdev;
900#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000901 }
902
903 /* Initializes input console */
904 if (inputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100905 console_setfile(stdin, inputdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100906#ifdef CONFIG_CONSOLE_MUX
907 console_devices[stdin][0] = inputdev;
908#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000909 }
910
Simon Glass78c112c2012-12-05 14:46:43 +0000911#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200912 stdio_print_current_devices();
Simon Glass78c112c2012-12-05 14:46:43 +0000913#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +0000914
915 /* Setting environment variables */
916 for (i = 0; i < 3; i++) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100917 setenv(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000918 }
919
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600920 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
921
wdenk47d1a6e2002-11-03 00:01:44 +0000922#if 0
923 /* If nothing usable installed, use only the initial console */
924 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100925 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000926#endif
Siarhei Siamashka27669662015-01-08 09:02:31 +0200927 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100928 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000929}
930
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200931#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */