blob: 8e50af1f9d22afa3ad033ef832220131c55126ff [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk47d1a6e2002-11-03 00:01:44 +00002/*
3 * (C) Copyright 2000
4 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
wdenk47d1a6e2002-11-03 00:01:44 +00005 */
6
7#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -07008#include <console.h>
Simon Glassd6ea5302015-06-23 15:38:33 -06009#include <debug_uart.h>
Simon Glass7b3c4c32017-07-27 09:31:04 -060010#include <dm.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060011#include <env.h>
wdenk47d1a6e2002-11-03 00:01:44 +000012#include <stdarg.h>
Jeroen Hofstee482f4692014-10-08 22:57:48 +020013#include <iomux.h>
wdenk47d1a6e2002-11-03 00:01:44 +000014#include <malloc.h>
Simon Glass4e6bafa2017-06-15 21:37:52 -060015#include <mapmem.h>
Simon Glass91b136c2013-11-10 10:27:01 -070016#include <os.h>
Joe Hershberger849d5d92012-12-11 22:16:29 -060017#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020018#include <stdio_dev.h>
wdenk27b207f2003-07-24 23:38:38 +000019#include <exports.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060020#include <env_internal.h>
Andreas J. Reichel64407462016-07-13 12:56:51 +020021#include <watchdog.h>
Simon Glassc05ed002020-05-10 11:40:11 -060022#include <linux/delay.h>
wdenk47d1a6e2002-11-03 00:01:44 +000023
Wolfgang Denkd87080b2006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
25
Joe Hershberger849d5d92012-12-11 22:16:29 -060026static int on_console(const char *name, const char *value, enum env_op op,
27 int flags)
28{
29 int console = -1;
30
31 /* Check for console redirection */
32 if (strcmp(name, "stdin") == 0)
33 console = stdin;
34 else if (strcmp(name, "stdout") == 0)
35 console = stdout;
36 else if (strcmp(name, "stderr") == 0)
37 console = stderr;
38
39 /* if not actually setting a console variable, we don't care */
40 if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
41 return 0;
42
43 switch (op) {
44 case env_op_create:
45 case env_op_overwrite:
46
Simon Glassb0265422017-01-16 07:03:26 -070047#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Joe Hershberger849d5d92012-12-11 22:16:29 -060048 if (iomux_doenv(console, value))
49 return 1;
50#else
51 /* Try assigning specified device */
52 if (console_assign(console, value) < 0)
53 return 1;
Simon Glassb0265422017-01-16 07:03:26 -070054#endif
Joe Hershberger849d5d92012-12-11 22:16:29 -060055 return 0;
56
57 case env_op_delete:
58 if ((flags & H_FORCE) == 0)
59 printf("Can't delete \"%s\"\n", name);
60 return 1;
61
62 default:
63 return 0;
64 }
65}
66U_BOOT_ENV_CALLBACK(console, on_console);
67
Joe Hershbergere080d542012-12-11 22:16:30 -060068#ifdef CONFIG_SILENT_CONSOLE
69static int on_silent(const char *name, const char *value, enum env_op op,
70 int flags)
71{
Wilson Lee5daf6e52017-08-25 00:06:29 -070072#if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_SET)
Joe Hershbergere080d542012-12-11 22:16:30 -060073 if (flags & H_INTERACTIVE)
74 return 0;
75#endif
Wilson Lee5daf6e52017-08-25 00:06:29 -070076#if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_RELOC)
Joe Hershbergere080d542012-12-11 22:16:30 -060077 if ((flags & H_INTERACTIVE) == 0)
78 return 0;
79#endif
80
81 if (value != NULL)
82 gd->flags |= GD_FLG_SILENT;
83 else
84 gd->flags &= ~GD_FLG_SILENT;
85
86 return 0;
87}
88U_BOOT_ENV_CALLBACK(silent, on_silent);
89#endif
90
Simon Glassb0265422017-01-16 07:03:26 -070091#if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
wdenk47d1a6e2002-11-03 00:01:44 +000092/*
93 * if overwrite_console returns 1, the stdin, stderr and stdout
94 * are switched to the serial port, else the settings in the
95 * environment are used
96 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020097#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +010098extern int overwrite_console(void);
99#define OVERWRITE_CONSOLE overwrite_console()
wdenk47d1a6e2002-11-03 00:01:44 +0000100#else
wdenk83e40ba2005-03-31 18:42:15 +0000101#define OVERWRITE_CONSOLE 0
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200102#endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
wdenk47d1a6e2002-11-03 00:01:44 +0000103
Simon Glassb0265422017-01-16 07:03:26 -0700104#endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
wdenk47d1a6e2002-11-03 00:01:44 +0000105
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200106static int console_setfile(int file, struct stdio_dev * dev)
wdenk47d1a6e2002-11-03 00:01:44 +0000107{
108 int error = 0;
109
110 if (dev == NULL)
111 return -1;
112
113 switch (file) {
114 case stdin:
115 case stdout:
116 case stderr:
117 /* Start new device */
118 if (dev->start) {
Simon Glass709ea542014-07-23 06:54:59 -0600119 error = dev->start(dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000120 /* If it's not started dont use it */
121 if (error < 0)
122 break;
123 }
124
125 /* Assign the new device (leaving the existing one started) */
126 stdio_devices[file] = dev;
127
128 /*
129 * Update monitor functions
130 * (to use the console stuff by other applications)
131 */
132 switch (file) {
133 case stdin:
Martin Dorwig49cad542015-01-26 15:22:54 -0700134 gd->jt->getc = getc;
135 gd->jt->tstc = tstc;
wdenk47d1a6e2002-11-03 00:01:44 +0000136 break;
137 case stdout:
Martin Dorwig49cad542015-01-26 15:22:54 -0700138 gd->jt->putc = putc;
139 gd->jt->puts = puts;
140 gd->jt->printf = printf;
wdenk47d1a6e2002-11-03 00:01:44 +0000141 break;
142 }
143 break;
144
145 default: /* Invalid file ID */
146 error = -1;
147 }
148 return error;
149}
150
Simon Glass42f9f912017-07-27 09:31:03 -0600151/**
152 * console_dev_is_serial() - Check if a stdio device is a serial device
153 *
154 * @sdev: Device to check
Simon Glass7b3c4c32017-07-27 09:31:04 -0600155 * @return true if this device is in the serial uclass (or for pre-driver-model,
156 * whether it is called "serial".
Simon Glass42f9f912017-07-27 09:31:03 -0600157 */
158static bool console_dev_is_serial(struct stdio_dev *sdev)
159{
160 bool is_serial;
161
Simon Glass7b3c4c32017-07-27 09:31:04 -0600162#ifdef CONFIG_DM_SERIAL
163 if (sdev->flags & DEV_FLAGS_DM) {
164 struct udevice *dev = sdev->priv;
165
166 is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL;
167 } else
168#endif
Simon Glass42f9f912017-07-27 09:31:03 -0600169 is_serial = !strcmp(sdev->name, "serial");
170
171 return is_serial;
172}
173
Simon Glassb0265422017-01-16 07:03:26 -0700174#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100175/** Console I/O multiplexing *******************************************/
176
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200177static struct stdio_dev *tstcdev;
178struct stdio_dev **console_devices[MAX_FILES];
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100179int cd_count[MAX_FILES];
180
181/*
182 * This depends on tstc() always being called before getc().
183 * This is guaranteed to be true because this routine is called
184 * only from fgetc() which assures it.
185 * No attempt is made to demultiplex multiple input sources.
186 */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100187static int console_getc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100188{
189 unsigned char ret;
190
191 /* This is never called with testcdev == NULL */
Simon Glass709ea542014-07-23 06:54:59 -0600192 ret = tstcdev->getc(tstcdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100193 tstcdev = NULL;
194 return ret;
195}
196
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100197static int console_tstc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100198{
199 int i, ret;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200200 struct stdio_dev *dev;
Joe Hershbergerb2f58d82018-07-02 20:06:48 -0500201 int prev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100202
Joe Hershbergerb2f58d82018-07-02 20:06:48 -0500203 prev = disable_ctrlc(1);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100204 for (i = 0; i < cd_count[file]; i++) {
205 dev = console_devices[file][i];
206 if (dev->tstc != NULL) {
Simon Glass709ea542014-07-23 06:54:59 -0600207 ret = dev->tstc(dev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100208 if (ret > 0) {
209 tstcdev = dev;
Joe Hershbergerb2f58d82018-07-02 20:06:48 -0500210 disable_ctrlc(prev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100211 return ret;
212 }
213 }
214 }
Joe Hershbergerb2f58d82018-07-02 20:06:48 -0500215 disable_ctrlc(prev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100216
217 return 0;
218}
219
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100220static void console_putc(int file, const char c)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100221{
222 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200223 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100224
225 for (i = 0; i < cd_count[file]; i++) {
226 dev = console_devices[file][i];
227 if (dev->putc != NULL)
Simon Glass709ea542014-07-23 06:54:59 -0600228 dev->putc(dev, c);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100229 }
230}
231
Simon Glass493a4c82020-07-02 21:12:13 -0600232/**
233 * console_puts_select() - Output a string to all console devices
234 *
235 * @file: File number to output to (e,g, stdout, see stdio.h)
236 * @serial_only: true to output only to serial, false to output to everything
237 * else
238 * @s: String to output
239 */
240static void console_puts_select(int file, bool serial_only, const char *s)
Siarhei Siamashka27669662015-01-08 09:02:31 +0200241{
242 int i;
243 struct stdio_dev *dev;
244
245 for (i = 0; i < cd_count[file]; i++) {
Simon Glass493a4c82020-07-02 21:12:13 -0600246 bool is_serial;
247
Siarhei Siamashka27669662015-01-08 09:02:31 +0200248 dev = console_devices[file][i];
Simon Glass493a4c82020-07-02 21:12:13 -0600249 is_serial = console_dev_is_serial(dev);
250 if (dev->puts && serial_only == is_serial)
Hans de Goedea8552c72015-05-05 13:13:36 +0200251 dev->puts(dev, s);
Siarhei Siamashka27669662015-01-08 09:02:31 +0200252 }
253}
Siarhei Siamashka27669662015-01-08 09:02:31 +0200254
Simon Glass493a4c82020-07-02 21:12:13 -0600255void console_puts_select_stderr(bool serial_only, const char *s)
256{
257 console_puts_select(stderr, serial_only, s);
258}
259
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100260static void console_puts(int file, const char *s)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100261{
262 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200263 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100264
265 for (i = 0; i < cd_count[file]; i++) {
266 dev = console_devices[file][i];
267 if (dev->puts != NULL)
Simon Glass709ea542014-07-23 06:54:59 -0600268 dev->puts(dev, s);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100269 }
270}
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100271
Tom Rini5e63c962019-10-30 09:18:43 -0400272#if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
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 iomux_doenv(file, dev->name);
276}
Tom Rini5e63c962019-10-30 09:18:43 -0400277#endif
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100278#else
279static inline int console_getc(int file)
280{
Simon Glass709ea542014-07-23 06:54:59 -0600281 return stdio_devices[file]->getc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100282}
283
284static inline int console_tstc(int file)
285{
Simon Glass709ea542014-07-23 06:54:59 -0600286 return stdio_devices[file]->tstc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100287}
288
289static inline void console_putc(int file, const char c)
290{
Simon Glass709ea542014-07-23 06:54:59 -0600291 stdio_devices[file]->putc(stdio_devices[file], c);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100292}
293
Simon Glass493a4c82020-07-02 21:12:13 -0600294void console_puts_select(int file, bool serial_only, const char *s)
Siarhei Siamashka27669662015-01-08 09:02:31 +0200295{
Simon Glass493a4c82020-07-02 21:12:13 -0600296 if (serial_only == console_dev_is_serial(stdio_devices[file]))
Hans de Goedea8552c72015-05-05 13:13:36 +0200297 stdio_devices[file]->puts(stdio_devices[file], s);
Siarhei Siamashka27669662015-01-08 09:02:31 +0200298}
Siarhei Siamashka27669662015-01-08 09:02:31 +0200299
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100300static inline void console_puts(int file, const char *s)
301{
Simon Glass709ea542014-07-23 06:54:59 -0600302 stdio_devices[file]->puts(stdio_devices[file], s);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100303}
304
Tom Rini5e63c962019-10-30 09:18:43 -0400305#if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200306static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100307{
308 console_setfile(file, dev);
309}
Tom Rini5e63c962019-10-30 09:18:43 -0400310#endif
Simon Glassb0265422017-01-16 07:03:26 -0700311#endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100312
wdenk47d1a6e2002-11-03 00:01:44 +0000313/** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
314
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200315int serial_printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000316{
317 va_list args;
318 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200319 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000320
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100321 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000322
323 /* For this to work, printbuffer must be larger than
324 * anything we ever want to print.
325 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000326 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100327 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000328
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100329 serial_puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200330 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000331}
332
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100333int fgetc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000334{
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100335 if (file < MAX_FILES) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100336 /*
337 * Effectively poll for input wherever it may be available.
338 */
339 for (;;) {
Andreas J. Reichel64407462016-07-13 12:56:51 +0200340 WATCHDOG_RESET();
Patrick Delaunay273a1252018-08-03 13:38:44 +0200341#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100342 /*
343 * Upper layer may have already called tstc() so
344 * check for that first.
345 */
346 if (tstcdev != NULL)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100347 return console_getc(file);
348 console_tstc(file);
Patrick Delaunay273a1252018-08-03 13:38:44 +0200349#else
350 if (console_tstc(file))
351 return console_getc(file);
352#endif
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100353#ifdef CONFIG_WATCHDOG
354 /*
355 * If the watchdog must be rate-limited then it should
356 * already be handled in board-specific code.
357 */
358 udelay(1);
359#endif
360 }
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100361 }
wdenk47d1a6e2002-11-03 00:01:44 +0000362
363 return -1;
364}
365
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100366int ftstc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000367{
368 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100369 return console_tstc(file);
wdenk47d1a6e2002-11-03 00:01:44 +0000370
371 return -1;
372}
373
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100374void fputc(int file, const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000375{
376 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100377 console_putc(file, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000378}
379
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100380void fputs(int file, const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000381{
382 if (file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100383 console_puts(file, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000384}
385
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200386int fprintf(int file, const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000387{
388 va_list args;
389 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200390 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000391
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100392 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000393
394 /* For this to work, printbuffer must be larger than
395 * anything we ever want to print.
396 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000397 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100398 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000399
400 /* Send to desired file */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100401 fputs(file, printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200402 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000403}
404
405/** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
406
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100407int getc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000408{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100409#ifdef CONFIG_DISABLE_CONSOLE
410 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
411 return 0;
412#endif
413
Graeme Russe3e454c2011-08-29 02:14:05 +0000414 if (!gd->have_console)
415 return 0;
416
Simon Glass9854a872015-11-08 23:47:48 -0700417#ifdef CONFIG_CONSOLE_RECORD
418 if (gd->console_in.start) {
419 int ch;
420
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100421 ch = membuff_getbyte((struct membuff *)&gd->console_in);
Simon Glass9854a872015-11-08 23:47:48 -0700422 if (ch != -1)
423 return 1;
424 }
425#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000426 if (gd->flags & GD_FLG_DEVINIT) {
427 /* Get from the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100428 return fgetc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000429 }
430
431 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100432 return serial_getc();
wdenk47d1a6e2002-11-03 00:01:44 +0000433}
434
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100435int tstc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000436{
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100437#ifdef CONFIG_DISABLE_CONSOLE
438 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
439 return 0;
440#endif
441
Graeme Russe3e454c2011-08-29 02:14:05 +0000442 if (!gd->have_console)
443 return 0;
Simon Glass9854a872015-11-08 23:47:48 -0700444#ifdef CONFIG_CONSOLE_RECORD
445 if (gd->console_in.start) {
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100446 if (membuff_peekbyte((struct membuff *)&gd->console_in) != -1)
Simon Glass9854a872015-11-08 23:47:48 -0700447 return 1;
448 }
449#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000450 if (gd->flags & GD_FLG_DEVINIT) {
451 /* Test the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100452 return ftstc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000453 }
454
455 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100456 return serial_tstc();
wdenk47d1a6e2002-11-03 00:01:44 +0000457}
458
Siarhei Siamashka27669662015-01-08 09:02:31 +0200459#define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
460#define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
461
Simon Glass8f925582016-10-17 20:12:36 -0600462#if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
Graeme Russ9558b482011-09-01 00:48:27 +0000463#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
464
465static void pre_console_putc(const char c)
466{
Simon Glass4e6bafa2017-06-15 21:37:52 -0600467 char *buffer;
468
469 buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
Graeme Russ9558b482011-09-01 00:48:27 +0000470
471 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
Simon Glass4e6bafa2017-06-15 21:37:52 -0600472
473 unmap_sysmem(buffer);
Graeme Russ9558b482011-09-01 00:48:27 +0000474}
475
Soeren Mochbe135cc2017-11-04 16:14:09 +0100476static void pre_console_puts(const char *s)
477{
478 while (*s)
479 pre_console_putc(*s++);
480}
481
Siarhei Siamashka27669662015-01-08 09:02:31 +0200482static void print_pre_console_buffer(int flushpoint)
Graeme Russ9558b482011-09-01 00:48:27 +0000483{
Hans de Goedea8552c72015-05-05 13:13:36 +0200484 unsigned long in = 0, out = 0;
Hans de Goedea8552c72015-05-05 13:13:36 +0200485 char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
Simon Glass4e6bafa2017-06-15 21:37:52 -0600486 char *buf_in;
Graeme Russ9558b482011-09-01 00:48:27 +0000487
Patrick Delaunay13551b92019-08-02 14:58:10 +0200488#ifdef CONFIG_SILENT_CONSOLE
489 if (gd->flags & GD_FLG_SILENT)
490 return;
491#endif
492
Simon Glass4e6bafa2017-06-15 21:37:52 -0600493 buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
Graeme Russ9558b482011-09-01 00:48:27 +0000494 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
Hans de Goedea8552c72015-05-05 13:13:36 +0200495 in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
Graeme Russ9558b482011-09-01 00:48:27 +0000496
Hans de Goedea8552c72015-05-05 13:13:36 +0200497 while (in < gd->precon_buf_idx)
498 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
Simon Glass4e6bafa2017-06-15 21:37:52 -0600499 unmap_sysmem(buf_in);
Hans de Goedea8552c72015-05-05 13:13:36 +0200500
501 buf_out[out] = 0;
502
503 switch (flushpoint) {
504 case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
505 puts(buf_out);
506 break;
507 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
Simon Glass493a4c82020-07-02 21:12:13 -0600508 console_puts_select(stdout, false, buf_out);
Hans de Goedea8552c72015-05-05 13:13:36 +0200509 break;
510 }
Graeme Russ9558b482011-09-01 00:48:27 +0000511}
512#else
513static inline void pre_console_putc(const char c) {}
Soeren Mochbe135cc2017-11-04 16:14:09 +0100514static inline void pre_console_puts(const char *s) {}
Siarhei Siamashka27669662015-01-08 09:02:31 +0200515static inline void print_pre_console_buffer(int flushpoint) {}
Graeme Russ9558b482011-09-01 00:48:27 +0000516#endif
517
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100518void putc(const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000519{
Simon Glass64e9b4f2017-12-04 13:48:19 -0700520#ifdef CONFIG_SANDBOX
521 /* sandbox can send characters to stdout before it has a console */
522 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
523 os_putc(c);
524 return;
525 }
526#endif
Simon Glassd6ea5302015-06-23 15:38:33 -0600527#ifdef CONFIG_DEBUG_UART
528 /* if we don't have a console yet, use the debug UART */
529 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
530 printch(c);
531 return;
532 }
533#endif
Simon Glassaf880e22018-06-12 00:04:56 -0600534 if (!gd)
535 return;
Simon Glass9854a872015-11-08 23:47:48 -0700536#ifdef CONFIG_CONSOLE_RECORD
Simon Glassaf880e22018-06-12 00:04:56 -0600537 if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100538 membuff_putbyte((struct membuff *)&gd->console_out, c);
Simon Glass9854a872015-11-08 23:47:48 -0700539#endif
wdenka6cccae2004-02-06 21:48:22 +0000540#ifdef CONFIG_SILENT_CONSOLE
Patrick Delaunay13551b92019-08-02 14:58:10 +0200541 if (gd->flags & GD_FLG_SILENT) {
542 if (!(gd->flags & GD_FLG_DEVINIT))
543 pre_console_putc(c);
wdenkf6e20fc2004-02-08 19:38:38 +0000544 return;
Patrick Delaunay13551b92019-08-02 14:58:10 +0200545 }
wdenka6cccae2004-02-06 21:48:22 +0000546#endif
547
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100548#ifdef CONFIG_DISABLE_CONSOLE
549 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
550 return;
551#endif
552
Graeme Russe3e454c2011-08-29 02:14:05 +0000553 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000554 return pre_console_putc(c);
Graeme Russe3e454c2011-08-29 02:14:05 +0000555
wdenk47d1a6e2002-11-03 00:01:44 +0000556 if (gd->flags & GD_FLG_DEVINIT) {
557 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100558 fputc(stdout, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000559 } else {
560 /* Send directly to the handler */
Siarhei Siamashka27669662015-01-08 09:02:31 +0200561 pre_console_putc(c);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100562 serial_putc(c);
wdenk47d1a6e2002-11-03 00:01:44 +0000563 }
564}
565
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100566void puts(const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000567{
Simon Glass36bcea62018-11-15 18:44:04 -0700568#ifdef CONFIG_SANDBOX
569 /* sandbox can send characters to stdout before it has a console */
570 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
571 os_puts(s);
572 return;
573 }
574#endif
Soeren Mochbe135cc2017-11-04 16:14:09 +0100575#ifdef CONFIG_DEBUG_UART
576 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
577 while (*s) {
578 int ch = *s++;
579
580 printch(ch);
581 }
582 return;
583 }
584#endif
Simon Glassaf880e22018-06-12 00:04:56 -0600585 if (!gd)
586 return;
Soeren Mochbe135cc2017-11-04 16:14:09 +0100587#ifdef CONFIG_CONSOLE_RECORD
Simon Glassaf880e22018-06-12 00:04:56 -0600588 if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100589 membuff_put((struct membuff *)&gd->console_out, s, strlen(s));
Soeren Mochbe135cc2017-11-04 16:14:09 +0100590#endif
591#ifdef CONFIG_SILENT_CONSOLE
Patrick Delaunay13551b92019-08-02 14:58:10 +0200592 if (gd->flags & GD_FLG_SILENT) {
593 if (!(gd->flags & GD_FLG_DEVINIT))
594 pre_console_puts(s);
Soeren Mochbe135cc2017-11-04 16:14:09 +0100595 return;
Patrick Delaunay13551b92019-08-02 14:58:10 +0200596 }
Soeren Mochbe135cc2017-11-04 16:14:09 +0100597#endif
598
599#ifdef CONFIG_DISABLE_CONSOLE
600 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
601 return;
602#endif
603
604 if (!gd->have_console)
605 return pre_console_puts(s);
606
607 if (gd->flags & GD_FLG_DEVINIT) {
608 /* Send to the standard output */
609 fputs(stdout, s);
610 } else {
611 /* Send directly to the handler */
612 pre_console_puts(s);
613 serial_puts(s);
614 }
wdenk47d1a6e2002-11-03 00:01:44 +0000615}
616
Simon Glass9854a872015-11-08 23:47:48 -0700617#ifdef CONFIG_CONSOLE_RECORD
618int console_record_init(void)
619{
620 int ret;
621
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100622 ret = membuff_new((struct membuff *)&gd->console_out,
623 CONFIG_CONSOLE_RECORD_OUT_SIZE);
Simon Glass9854a872015-11-08 23:47:48 -0700624 if (ret)
625 return ret;
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100626 ret = membuff_new((struct membuff *)&gd->console_in,
627 CONFIG_CONSOLE_RECORD_IN_SIZE);
Simon Glass9854a872015-11-08 23:47:48 -0700628
629 return ret;
630}
631
632void console_record_reset(void)
633{
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100634 membuff_purge((struct membuff *)&gd->console_out);
635 membuff_purge((struct membuff *)&gd->console_in);
Simon Glass9854a872015-11-08 23:47:48 -0700636}
637
Simon Glassbd347152020-07-28 19:41:11 -0600638int console_record_reset_enable(void)
Simon Glass9854a872015-11-08 23:47:48 -0700639{
640 console_record_reset();
641 gd->flags |= GD_FLG_RECORD;
Simon Glassbd347152020-07-28 19:41:11 -0600642
643 return 0;
Simon Glass9854a872015-11-08 23:47:48 -0700644}
Simon Glassb6123122020-01-27 08:49:54 -0700645
646int console_record_readline(char *str, int maxlen)
647{
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100648 return membuff_readline((struct membuff *)&gd->console_out, str,
649 maxlen, ' ');
Simon Glassb6123122020-01-27 08:49:54 -0700650}
651
652int console_record_avail(void)
653{
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100654 return membuff_avail((struct membuff *)&gd->console_out);
Simon Glassb6123122020-01-27 08:49:54 -0700655}
656
Simon Glass9854a872015-11-08 23:47:48 -0700657#endif
658
wdenk47d1a6e2002-11-03 00:01:44 +0000659/* test if ctrl-c was pressed */
660static int ctrlc_disabled = 0; /* see disable_ctrl() */
661static int ctrlc_was_pressed = 0;
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100662int ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000663{
wdenk47d1a6e2002-11-03 00:01:44 +0000664 if (!ctrlc_disabled && gd->have_console) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100665 if (tstc()) {
666 switch (getc()) {
wdenk47d1a6e2002-11-03 00:01:44 +0000667 case 0x03: /* ^C - Control C */
668 ctrlc_was_pressed = 1;
669 return 1;
670 default:
671 break;
672 }
673 }
674 }
Simon Glass8969ea32014-09-14 12:40:15 -0600675
wdenk47d1a6e2002-11-03 00:01:44 +0000676 return 0;
677}
Pierre Auberta5dffa42014-04-24 10:30:07 +0200678/* Reads user's confirmation.
679 Returns 1 if user's input is "y", "Y", "yes" or "YES"
680*/
681int confirm_yesno(void)
682{
683 int i;
684 char str_input[5];
wdenk47d1a6e2002-11-03 00:01:44 +0000685
Pierre Auberta5dffa42014-04-24 10:30:07 +0200686 /* Flush input */
687 while (tstc())
688 getc();
689 i = 0;
690 while (i < sizeof(str_input)) {
691 str_input[i] = getc();
692 putc(str_input[i]);
693 if (str_input[i] == '\r')
694 break;
695 i++;
696 }
697 putc('\n');
698 if (strncmp(str_input, "y\r", 2) == 0 ||
699 strncmp(str_input, "Y\r", 2) == 0 ||
700 strncmp(str_input, "yes\r", 4) == 0 ||
701 strncmp(str_input, "YES\r", 4) == 0)
702 return 1;
703 return 0;
704}
wdenk47d1a6e2002-11-03 00:01:44 +0000705/* pass 1 to disable ctrlc() checking, 0 to enable.
706 * returns previous state
707 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100708int disable_ctrlc(int disable)
wdenk47d1a6e2002-11-03 00:01:44 +0000709{
710 int prev = ctrlc_disabled; /* save previous state */
711
712 ctrlc_disabled = disable;
713 return prev;
714}
715
716int had_ctrlc (void)
717{
718 return ctrlc_was_pressed;
719}
720
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100721void clear_ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000722{
723 ctrlc_was_pressed = 0;
724}
725
wdenk47d1a6e2002-11-03 00:01:44 +0000726/** U-Boot INIT FUNCTIONS *************************************************/
727
Mike Frysingerd7be3052010-10-20 07:18:03 -0400728struct stdio_dev *search_device(int flags, const char *name)
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200729{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200730 struct stdio_dev *dev;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200731
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200732 dev = stdio_get_by_name(name);
Simon Glassa2931b32016-02-06 14:31:37 -0700733#ifdef CONFIG_VIDCONSOLE_AS_LCD
Patrick Delaunay27b5b9e2020-07-01 14:56:10 +0200734 if (!dev && !strcmp(name, CONFIG_VIDCONSOLE_AS_NAME))
Simon Glassa2931b32016-02-06 14:31:37 -0700735 dev = stdio_get_by_name("vidconsole");
736#endif
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200737
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100738 if (dev && (dev->flags & flags))
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200739 return dev;
740
741 return NULL;
742}
743
Mike Frysingerd7be3052010-10-20 07:18:03 -0400744int console_assign(int file, const char *devname)
wdenk47d1a6e2002-11-03 00:01:44 +0000745{
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200746 int flag;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200747 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000748
749 /* Check for valid file */
750 switch (file) {
751 case stdin:
752 flag = DEV_FLAGS_INPUT;
753 break;
754 case stdout:
755 case stderr:
756 flag = DEV_FLAGS_OUTPUT;
757 break;
758 default:
759 return -1;
760 }
761
762 /* Check for valid device name */
763
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200764 dev = search_device(flag, devname);
wdenk47d1a6e2002-11-03 00:01:44 +0000765
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100766 if (dev)
767 return console_setfile(file, dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000768
769 return -1;
770}
771
Patrick Delaunay13551b92019-08-02 14:58:10 +0200772/* return true if the 'silent' flag is removed */
773static bool console_update_silent(void)
Chris Packham43e0a3d2016-09-23 15:59:43 +1200774{
775#ifdef CONFIG_SILENT_CONSOLE
Patrick Delaunay13551b92019-08-02 14:58:10 +0200776 if (env_get("silent")) {
Chris Packham43e0a3d2016-09-23 15:59:43 +1200777 gd->flags |= GD_FLG_SILENT;
Patrick Delaunay13551b92019-08-02 14:58:10 +0200778 } else {
779 unsigned long flags = gd->flags;
780
Chris Packham43e0a3d2016-09-23 15:59:43 +1200781 gd->flags &= ~GD_FLG_SILENT;
Patrick Delaunay13551b92019-08-02 14:58:10 +0200782
783 return !!(flags & GD_FLG_SILENT);
784 }
Chris Packham43e0a3d2016-09-23 15:59:43 +1200785#endif
Patrick Delaunay13551b92019-08-02 14:58:10 +0200786
787 return false;
Chris Packham43e0a3d2016-09-23 15:59:43 +1200788}
789
Simon Glassb0895382017-06-15 21:37:50 -0600790int console_announce_r(void)
791{
792#if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
793 char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
794
795 display_options_get_banner(false, buf, sizeof(buf));
796
Simon Glass493a4c82020-07-02 21:12:13 -0600797 console_puts_select(stdout, false, buf);
Simon Glassb0895382017-06-15 21:37:50 -0600798#endif
799
800 return 0;
801}
802
wdenk47d1a6e2002-11-03 00:01:44 +0000803/* Called before relocation - use serial functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100804int console_init_f(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000805{
wdenk47d1a6e2002-11-03 00:01:44 +0000806 gd->have_console = 1;
wdenkf72da342003-10-10 10:05:42 +0000807
Chris Packham43e0a3d2016-09-23 15:59:43 +1200808 console_update_silent();
wdenkf72da342003-10-10 10:05:42 +0000809
Siarhei Siamashka27669662015-01-08 09:02:31 +0200810 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
Graeme Russ9558b482011-09-01 00:48:27 +0000811
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100812 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000813}
814
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200815void stdio_print_current_devices(void)
816{
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200817 /* Print information */
818 puts("In: ");
819 if (stdio_devices[stdin] == NULL) {
820 puts("No input devices available!\n");
821 } else {
822 printf ("%s\n", stdio_devices[stdin]->name);
823 }
824
825 puts("Out: ");
826 if (stdio_devices[stdout] == NULL) {
827 puts("No output devices available!\n");
828 } else {
829 printf ("%s\n", stdio_devices[stdout]->name);
830 }
831
832 puts("Err: ");
833 if (stdio_devices[stderr] == NULL) {
834 puts("No error devices available!\n");
835 } else {
836 printf ("%s\n", stdio_devices[stderr]->name);
837 }
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200838}
839
Simon Glassb0265422017-01-16 07:03:26 -0700840#if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
wdenk47d1a6e2002-11-03 00:01:44 +0000841/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100842int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000843{
844 char *stdinname, *stdoutname, *stderrname;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200845 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200846#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk6e592382004-04-18 17:39:38 +0000847 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200848#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
Simon Glassb0265422017-01-16 07:03:26 -0700849#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100850 int iomux_err = 0;
851#endif
Patrick Delaunay13551b92019-08-02 14:58:10 +0200852 int flushpoint;
wdenk47d1a6e2002-11-03 00:01:44 +0000853
Patrick Delaunaybf46be72019-08-02 14:58:09 +0200854 /* update silent for env loaded from flash (initr_env) */
Patrick Delaunay13551b92019-08-02 14:58:10 +0200855 if (console_update_silent())
856 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
857 else
858 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
Patrick Delaunaybf46be72019-08-02 14:58:09 +0200859
wdenk47d1a6e2002-11-03 00:01:44 +0000860 /* set default handlers at first */
Martin Dorwig49cad542015-01-26 15:22:54 -0700861 gd->jt->getc = serial_getc;
862 gd->jt->tstc = serial_tstc;
863 gd->jt->putc = serial_putc;
864 gd->jt->puts = serial_puts;
865 gd->jt->printf = serial_printf;
wdenk47d1a6e2002-11-03 00:01:44 +0000866
867 /* stdin stdout and stderr are in environment */
868 /* scan for it */
Simon Glass00caae62017-08-03 12:22:12 -0600869 stdinname = env_get("stdin");
870 stdoutname = env_get("stdout");
871 stderrname = env_get("stderr");
wdenk47d1a6e2002-11-03 00:01:44 +0000872
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200873 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100874 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
875 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
876 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
Simon Glassb0265422017-01-16 07:03:26 -0700877#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100878 iomux_err = iomux_doenv(stdin, stdinname);
879 iomux_err += iomux_doenv(stdout, stdoutname);
880 iomux_err += iomux_doenv(stderr, stderrname);
881 if (!iomux_err)
882 /* Successful, so skip all the code below. */
883 goto done;
884#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000885 }
886 /* if the devices are overwritten or not found, use default device */
887 if (inputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100888 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000889 }
890 if (outputdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100891 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000892 }
893 if (errdev == NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100894 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +0000895 }
896 /* Initializes output console first */
897 if (outputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100898 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100899 console_doenv(stdout, outputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000900 }
901 if (errdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100902 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100903 console_doenv(stderr, errdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000904 }
905 if (inputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100906 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100907 console_doenv(stdin, inputdev);
wdenk47d1a6e2002-11-03 00:01:44 +0000908 }
909
Simon Glassb0265422017-01-16 07:03:26 -0700910#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100911done:
912#endif
913
Simon Glass78c112c2012-12-05 14:46:43 +0000914#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +0200915 stdio_print_current_devices();
Simon Glass78c112c2012-12-05 14:46:43 +0000916#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
Simon Glassa2931b32016-02-06 14:31:37 -0700917#ifdef CONFIG_VIDCONSOLE_AS_LCD
Patrick Delaunay27b5b9e2020-07-01 14:56:10 +0200918 if (strstr(stdoutname, CONFIG_VIDCONSOLE_AS_NAME))
Anatolij Gustschin22b897a2020-05-23 17:11:20 +0200919 printf("Warning: Please change '%s' to 'vidconsole' in stdout/stderr environment vars\n",
Patrick Delaunay27b5b9e2020-07-01 14:56:10 +0200920 CONFIG_VIDCONSOLE_AS_NAME);
Simon Glassa2931b32016-02-06 14:31:37 -0700921#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000922
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200923#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk47d1a6e2002-11-03 00:01:44 +0000924 /* set the environment variables (will overwrite previous env settings) */
Tom Rini27b42252018-05-03 09:12:26 -0400925 for (i = 0; i < MAX_FILES; i++) {
Simon Glass382bee52017-08-03 12:22:09 -0600926 env_set(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +0000927 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200928#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
wdenk47d1a6e2002-11-03 00:01:44 +0000929
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600930 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
931
wdenk47d1a6e2002-11-03 00:01:44 +0000932#if 0
933 /* If nothing usable installed, use only the initial console */
934 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100935 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000936#endif
Patrick Delaunay13551b92019-08-02 14:58:10 +0200937 print_pre_console_buffer(flushpoint);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100938 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000939}
940
Simon Glassb0265422017-01-16 07:03:26 -0700941#else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
wdenk47d1a6e2002-11-03 00:01:44 +0000942
943/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100944int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000945{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200946 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200947 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200948 struct list_head *list = stdio_get_list();
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200949 struct list_head *pos;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200950 struct stdio_dev *dev;
Patrick Delaunay13551b92019-08-02 14:58:10 +0200951 int flushpoint;
wdenk47d1a6e2002-11-03 00:01:44 +0000952
Patrick Delaunaybf46be72019-08-02 14:58:09 +0200953 /* update silent for env loaded from flash (initr_env) */
Patrick Delaunay13551b92019-08-02 14:58:10 +0200954 if (console_update_silent())
955 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
956 else
957 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
Chris Packham43e0a3d2016-09-23 15:59:43 +1200958
wdenkd791b1d2003-04-20 14:04:18 +0000959#ifdef CONFIG_SPLASH_SCREEN
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100960 /*
961 * suppress all output if splash screen is enabled and we have
Anatolij Gustschina7490812010-03-16 15:29:33 +0100962 * a bmp to display. We redirect the output from frame buffer
963 * console to serial console in this case or suppress it if
964 * "silent" mode was requested.
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100965 */
Simon Glass00caae62017-08-03 12:22:12 -0600966 if (env_get("splashimage") != NULL) {
Anatolij Gustschina7490812010-03-16 15:29:33 +0100967 if (!(gd->flags & GD_FLG_SILENT))
968 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
969 }
wdenkf72da342003-10-10 10:05:42 +0000970#endif
971
wdenk47d1a6e2002-11-03 00:01:44 +0000972 /* Scan devices looking for input and output devices */
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200973 list_for_each(pos, list) {
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200974 dev = list_entry(pos, struct stdio_dev, list);
wdenk47d1a6e2002-11-03 00:01:44 +0000975
976 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
977 inputdev = dev;
978 }
979 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
980 outputdev = dev;
981 }
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200982 if(inputdev && outputdev)
983 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000984 }
985
986 /* Initializes output console first */
987 if (outputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100988 console_setfile(stdout, outputdev);
989 console_setfile(stderr, outputdev);
Simon Glassb0265422017-01-16 07:03:26 -0700990#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100991 console_devices[stdout][0] = outputdev;
992 console_devices[stderr][0] = outputdev;
993#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000994 }
995
996 /* Initializes input console */
997 if (inputdev != NULL) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100998 console_setfile(stdin, inputdev);
Simon Glassb0265422017-01-16 07:03:26 -0700999#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +01001000 console_devices[stdin][0] = inputdev;
1001#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001002 }
1003
Simon Glass78c112c2012-12-05 14:46:43 +00001004#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +02001005 stdio_print_current_devices();
Simon Glass78c112c2012-12-05 14:46:43 +00001006#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk47d1a6e2002-11-03 00:01:44 +00001007
1008 /* Setting environment variables */
Tom Rini27b42252018-05-03 09:12:26 -04001009 for (i = 0; i < MAX_FILES; i++) {
Simon Glass382bee52017-08-03 12:22:09 -06001010 env_set(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +00001011 }
1012
Joe Hershbergerc4e00572012-12-11 22:16:19 -06001013 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
1014
wdenk47d1a6e2002-11-03 00:01:44 +00001015#if 0
1016 /* If nothing usable installed, use only the initial console */
1017 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +01001018 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +00001019#endif
Patrick Delaunay13551b92019-08-02 14:58:10 +02001020 print_pre_console_buffer(flushpoint);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +01001021 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +00001022}
1023
Simon Glassb0265422017-01-16 07:03:26 -07001024#endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */