blob: 09d4b51f675e286a47535440f370d90ccbb19e2f [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
Simon Glass90afded2024-09-01 16:26:18 -06007#define LOG_CATEGORY LOGC_CONSOLE
8
Simon Glass24b852a2015-11-08 23:47:45 -07009#include <console.h>
Simon Glassd6ea5302015-06-23 15:38:33 -060010#include <debug_uart.h>
Simon Glass4e4bf942022-07-31 12:28:48 -060011#include <display_options.h>
Simon Glass7b3c4c32017-07-27 09:31:04 -060012#include <dm.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060013#include <env.h>
wdenk47d1a6e2002-11-03 00:01:44 +000014#include <stdarg.h>
Jeroen Hofstee482f4692014-10-08 22:57:48 +020015#include <iomux.h>
wdenk47d1a6e2002-11-03 00:01:44 +000016#include <malloc.h>
Simon Glass4e6bafa2017-06-15 21:37:52 -060017#include <mapmem.h>
Simon Glass91b136c2013-11-10 10:27:01 -070018#include <os.h>
Joe Hershberger849d5d92012-12-11 22:16:29 -060019#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020020#include <stdio_dev.h>
wdenk27b207f2003-07-24 23:38:38 +000021#include <exports.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060022#include <env_internal.h>
Simon Glasscde03fa2023-10-01 19:15:23 -060023#include <video_console.h>
Andreas J. Reichel64407462016-07-13 12:56:51 +020024#include <watchdog.h>
Simon Glass401d1c42020-10-30 21:38:53 -060025#include <asm/global_data.h>
Simon Glassc05ed002020-05-10 11:40:11 -060026#include <linux/delay.h>
wdenk47d1a6e2002-11-03 00:01:44 +000027
Wolfgang Denkd87080b2006-03-31 18:32:53 +020028DECLARE_GLOBAL_DATA_PTR;
29
Simon Glasscde03fa2023-10-01 19:15:23 -060030#define CSI "\x1b["
31
Joe Hershberger849d5d92012-12-11 22:16:29 -060032static int on_console(const char *name, const char *value, enum env_op op,
33 int flags)
34{
35 int console = -1;
36
37 /* Check for console redirection */
38 if (strcmp(name, "stdin") == 0)
39 console = stdin;
40 else if (strcmp(name, "stdout") == 0)
41 console = stdout;
42 else if (strcmp(name, "stderr") == 0)
43 console = stderr;
44
45 /* if not actually setting a console variable, we don't care */
46 if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
47 return 0;
48
49 switch (op) {
50 case env_op_create:
51 case env_op_overwrite:
52
Patrick Delaunayc04f8562020-12-18 12:46:43 +010053 if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
54 if (iomux_doenv(console, value))
55 return 1;
56 } else {
57 /* Try assigning specified device */
58 if (console_assign(console, value) < 0)
59 return 1;
60 }
61
Joe Hershberger849d5d92012-12-11 22:16:29 -060062 return 0;
63
64 case env_op_delete:
65 if ((flags & H_FORCE) == 0)
66 printf("Can't delete \"%s\"\n", name);
67 return 1;
68
69 default:
70 return 0;
71 }
72}
73U_BOOT_ENV_CALLBACK(console, on_console);
74
Joe Hershbergere080d542012-12-11 22:16:30 -060075#ifdef CONFIG_SILENT_CONSOLE
76static int on_silent(const char *name, const char *value, enum env_op op,
77 int flags)
78{
Patrick Delaunayc04f8562020-12-18 12:46:43 +010079 if (!CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_SET))
80 if (flags & H_INTERACTIVE)
81 return 0;
82
83 if (!CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_RELOC))
84 if ((flags & H_INTERACTIVE) == 0)
85 return 0;
Joe Hershbergere080d542012-12-11 22:16:30 -060086
87 if (value != NULL)
88 gd->flags |= GD_FLG_SILENT;
89 else
90 gd->flags &= ~GD_FLG_SILENT;
91
92 return 0;
93}
94U_BOOT_ENV_CALLBACK(silent, on_silent);
95#endif
96
Patrick Delaunay1e993712020-12-18 12:46:45 +010097#ifdef CONFIG_CONSOLE_RECORD
98/* helper function: access to gd->console_out and gd->console_in */
99static void console_record_putc(const char c)
100{
101 if (!(gd->flags & GD_FLG_RECORD))
102 return;
Simon Glassc1a2bb42021-05-08 06:59:56 -0600103 if (gd->console_out.start &&
104 !membuff_putbyte((struct membuff *)&gd->console_out, c))
105 gd->flags |= GD_FLG_RECORD_OVF;
Patrick Delaunay1e993712020-12-18 12:46:45 +0100106}
107
108static void console_record_puts(const char *s)
109{
110 if (!(gd->flags & GD_FLG_RECORD))
111 return;
Simon Glassc1a2bb42021-05-08 06:59:56 -0600112 if (gd->console_out.start) {
113 int len = strlen(s);
114
115 if (membuff_put((struct membuff *)&gd->console_out, s, len) !=
116 len)
117 gd->flags |= GD_FLG_RECORD_OVF;
118 }
Patrick Delaunay1e993712020-12-18 12:46:45 +0100119}
120
121static int console_record_getc(void)
122{
123 if (!(gd->flags & GD_FLG_RECORD))
124 return -1;
125 if (!gd->console_in.start)
126 return -1;
127
128 return membuff_getbyte((struct membuff *)&gd->console_in);
129}
130
131static int console_record_tstc(void)
132{
133 if (!(gd->flags & GD_FLG_RECORD))
134 return 0;
135 if (gd->console_in.start) {
136 if (membuff_peekbyte((struct membuff *)&gd->console_in) != -1)
137 return 1;
138 }
139 return 0;
140}
141#else
142static void console_record_putc(char c)
143{
144}
145
146static void console_record_puts(const char *s)
147{
148}
149
150static int console_record_getc(void)
151{
152 return -1;
153}
154
155static int console_record_tstc(void)
156{
157 return 0;
158}
159#endif
160
Simon Glassb0265422017-01-16 07:03:26 -0700161#if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
wdenk47d1a6e2002-11-03 00:01:44 +0000162/*
163 * if overwrite_console returns 1, the stdin, stderr and stdout
164 * are switched to the serial port, else the settings in the
165 * environment are used
166 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200167#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100168extern int overwrite_console(void);
169#define OVERWRITE_CONSOLE overwrite_console()
wdenk47d1a6e2002-11-03 00:01:44 +0000170#else
wdenk83e40ba2005-03-31 18:42:15 +0000171#define OVERWRITE_CONSOLE 0
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200172#endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
wdenk47d1a6e2002-11-03 00:01:44 +0000173
Simon Glassb0265422017-01-16 07:03:26 -0700174#endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
wdenk47d1a6e2002-11-03 00:01:44 +0000175
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200176static int console_setfile(int file, struct stdio_dev * dev)
wdenk47d1a6e2002-11-03 00:01:44 +0000177{
178 int error = 0;
179
180 if (dev == NULL)
181 return -1;
182
183 switch (file) {
184 case stdin:
185 case stdout:
186 case stderr:
Andy Shevchenko41f668b2020-12-21 14:30:00 +0200187 error = console_start(file, dev);
188 if (error)
189 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000190
191 /* Assign the new device (leaving the existing one started) */
192 stdio_devices[file] = dev;
193
194 /*
195 * Update monitor functions
196 * (to use the console stuff by other applications)
197 */
198 switch (file) {
199 case stdin:
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200200 gd->jt->getc = getchar;
Martin Dorwig49cad542015-01-26 15:22:54 -0700201 gd->jt->tstc = tstc;
wdenk47d1a6e2002-11-03 00:01:44 +0000202 break;
203 case stdout:
Martin Dorwig49cad542015-01-26 15:22:54 -0700204 gd->jt->putc = putc;
205 gd->jt->puts = puts;
Pali Rohár974f4832022-09-05 11:31:17 +0200206 STDIO_DEV_ASSIGN_FLUSH(gd->jt, flush);
Martin Dorwig49cad542015-01-26 15:22:54 -0700207 gd->jt->printf = printf;
wdenk47d1a6e2002-11-03 00:01:44 +0000208 break;
209 }
210 break;
211
212 default: /* Invalid file ID */
213 error = -1;
214 }
215 return error;
216}
217
Simon Glass42f9f912017-07-27 09:31:03 -0600218/**
219 * console_dev_is_serial() - Check if a stdio device is a serial device
220 *
221 * @sdev: Device to check
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100222 * Return: true if this device is in the serial uclass (or for pre-driver-model,
Simon Glass7b3c4c32017-07-27 09:31:04 -0600223 * whether it is called "serial".
Simon Glass42f9f912017-07-27 09:31:03 -0600224 */
225static bool console_dev_is_serial(struct stdio_dev *sdev)
226{
227 bool is_serial;
228
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100229 if (IS_ENABLED(CONFIG_DM_SERIAL) && (sdev->flags & DEV_FLAGS_DM)) {
Simon Glass7b3c4c32017-07-27 09:31:04 -0600230 struct udevice *dev = sdev->priv;
231
232 is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL;
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100233 } else {
234 is_serial = !strcmp(sdev->name, "serial");
235 }
Simon Glass42f9f912017-07-27 09:31:03 -0600236
237 return is_serial;
238}
239
Simon Glassb0265422017-01-16 07:03:26 -0700240#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100241/** Console I/O multiplexing *******************************************/
242
Patrick Delaunaya17b38c2020-12-18 12:46:46 +0100243/* tstcdev: save the last stdio device with pending characters, with tstc != 0 */
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200244static struct stdio_dev *tstcdev;
245struct stdio_dev **console_devices[MAX_FILES];
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100246int cd_count[MAX_FILES];
247
Andy Shevchenko09d8f072021-02-11 17:09:39 +0200248static void console_devices_set(int file, struct stdio_dev *dev)
Patrick Delaunay45375ad2020-12-18 12:46:44 +0100249{
250 console_devices[file][0] = dev;
Andy Shevchenko20a7d352021-02-11 17:09:38 +0200251 cd_count[file] = 1;
Patrick Delaunay45375ad2020-12-18 12:46:44 +0100252}
253
Andy Shevchenko95aaf402020-12-21 14:30:01 +0200254/**
255 * console_needs_start_stop() - check if we need to start or stop the STDIO device
256 * @file: STDIO file
257 * @sdev: STDIO device in question
258 *
259 * This function checks if we need to start or stop the stdio device used for
260 * a console. For IOMUX case it simply enforces one time start and one time
261 * stop of the device independently of how many STDIO files are using it. In
262 * other words, we start console once before first STDIO device wants it and
263 * stop after the last is gone.
264 */
265static bool console_needs_start_stop(int file, struct stdio_dev *sdev)
266{
Andy Shevchenkob672c162021-02-11 17:09:41 +0200267 int i;
Andy Shevchenko95aaf402020-12-21 14:30:01 +0200268
269 for (i = 0; i < ARRAY_SIZE(cd_count); i++) {
270 if (i == file)
271 continue;
272
Andy Shevchenkob672c162021-02-11 17:09:41 +0200273 if (iomux_match_device(console_devices[i], cd_count[i], sdev) >= 0)
274 return false;
Andy Shevchenko95aaf402020-12-21 14:30:01 +0200275 }
276 return true;
277}
278
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100279/*
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200280 * This depends on tstc() always being called before getchar().
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100281 * This is guaranteed to be true because this routine is called
282 * only from fgetc() which assures it.
283 * No attempt is made to demultiplex multiple input sources.
284 */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100285static int console_getc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100286{
287 unsigned char ret;
288
289 /* This is never called with testcdev == NULL */
Simon Glass709ea542014-07-23 06:54:59 -0600290 ret = tstcdev->getc(tstcdev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100291 tstcdev = NULL;
292 return ret;
293}
294
Patrick Delaunaya17b38c2020-12-18 12:46:46 +0100295/* Upper layer may have already called tstc(): check the saved result */
296static bool console_has_tstc(void)
297{
298 return !!tstcdev;
299}
300
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100301static int console_tstc(int file)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100302{
303 int i, ret;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200304 struct stdio_dev *dev;
Joe Hershbergerb2f58d82018-07-02 20:06:48 -0500305 int prev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100306
Joe Hershbergerb2f58d82018-07-02 20:06:48 -0500307 prev = disable_ctrlc(1);
Andy Shevchenko400797c2021-02-11 17:09:42 +0200308 for_each_console_dev(i, file, dev) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100309 if (dev->tstc != NULL) {
Simon Glass709ea542014-07-23 06:54:59 -0600310 ret = dev->tstc(dev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100311 if (ret > 0) {
312 tstcdev = dev;
Joe Hershbergerb2f58d82018-07-02 20:06:48 -0500313 disable_ctrlc(prev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100314 return ret;
315 }
316 }
317 }
Joe Hershbergerb2f58d82018-07-02 20:06:48 -0500318 disable_ctrlc(prev);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100319
320 return 0;
321}
322
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100323static void console_putc(int file, const char c)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100324{
325 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200326 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100327
Andy Shevchenko400797c2021-02-11 17:09:42 +0200328 for_each_console_dev(i, file, dev) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100329 if (dev->putc != NULL)
Simon Glass709ea542014-07-23 06:54:59 -0600330 dev->putc(dev, c);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100331 }
332}
333
Simon Glass493a4c82020-07-02 21:12:13 -0600334/**
335 * console_puts_select() - Output a string to all console devices
336 *
337 * @file: File number to output to (e,g, stdout, see stdio.h)
338 * @serial_only: true to output only to serial, false to output to everything
339 * else
340 * @s: String to output
341 */
342static void console_puts_select(int file, bool serial_only, const char *s)
Siarhei Siamashka27669662015-01-08 09:02:31 +0200343{
344 int i;
345 struct stdio_dev *dev;
346
Andy Shevchenko400797c2021-02-11 17:09:42 +0200347 for_each_console_dev(i, file, dev) {
348 bool is_serial = console_dev_is_serial(dev);
Simon Glass493a4c82020-07-02 21:12:13 -0600349
Simon Glass493a4c82020-07-02 21:12:13 -0600350 if (dev->puts && serial_only == is_serial)
Hans de Goedea8552c72015-05-05 13:13:36 +0200351 dev->puts(dev, s);
Siarhei Siamashka27669662015-01-08 09:02:31 +0200352 }
353}
Siarhei Siamashka27669662015-01-08 09:02:31 +0200354
Simon Glass493a4c82020-07-02 21:12:13 -0600355void console_puts_select_stderr(bool serial_only, const char *s)
356{
Simon Glass4057e272021-11-19 13:23:47 -0700357 if (gd->flags & GD_FLG_DEVINIT)
358 console_puts_select(stderr, serial_only, s);
Simon Glass493a4c82020-07-02 21:12:13 -0600359}
360
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100361static void console_puts(int file, const char *s)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100362{
363 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200364 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100365
Andy Shevchenko400797c2021-02-11 17:09:42 +0200366 for_each_console_dev(i, file, dev) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100367 if (dev->puts != NULL)
Simon Glass709ea542014-07-23 06:54:59 -0600368 dev->puts(dev, s);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100369 }
370}
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100371
Pali Rohár974f4832022-09-05 11:31:17 +0200372#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
373static void console_flush(int file)
374{
375 int i;
376 struct stdio_dev *dev;
377
378 for_each_console_dev(i, file, dev) {
379 if (dev->flush != NULL)
380 dev->flush(dev);
381 }
382}
383#endif
384
Tom Rini5e63c962019-10-30 09:18:43 -0400385#if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200386static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100387{
388 iomux_doenv(file, dev->name);
389}
Tom Rini5e63c962019-10-30 09:18:43 -0400390#endif
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100391#else
Patrick Delaunay45375ad2020-12-18 12:46:44 +0100392
Andy Shevchenko09d8f072021-02-11 17:09:39 +0200393static void console_devices_set(int file, struct stdio_dev *dev)
Patrick Delaunay45375ad2020-12-18 12:46:44 +0100394{
395}
396
Andy Shevchenko95aaf402020-12-21 14:30:01 +0200397static inline bool console_needs_start_stop(int file, struct stdio_dev *sdev)
398{
399 return true;
400}
401
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100402static inline int console_getc(int file)
403{
Simon Glass709ea542014-07-23 06:54:59 -0600404 return stdio_devices[file]->getc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100405}
406
Patrick Delaunaya17b38c2020-12-18 12:46:46 +0100407static bool console_has_tstc(void)
408{
409 return false;
410}
411
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100412static inline int console_tstc(int file)
413{
Simon Glass709ea542014-07-23 06:54:59 -0600414 return stdio_devices[file]->tstc(stdio_devices[file]);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100415}
416
417static inline void console_putc(int file, const char c)
418{
Simon Glass709ea542014-07-23 06:54:59 -0600419 stdio_devices[file]->putc(stdio_devices[file], c);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100420}
421
Simon Glass493a4c82020-07-02 21:12:13 -0600422void console_puts_select(int file, bool serial_only, const char *s)
Siarhei Siamashka27669662015-01-08 09:02:31 +0200423{
Simon Glass4057e272021-11-19 13:23:47 -0700424 if ((gd->flags & GD_FLG_DEVINIT) &&
425 serial_only == console_dev_is_serial(stdio_devices[file]))
Hans de Goedea8552c72015-05-05 13:13:36 +0200426 stdio_devices[file]->puts(stdio_devices[file], s);
Siarhei Siamashka27669662015-01-08 09:02:31 +0200427}
Siarhei Siamashka27669662015-01-08 09:02:31 +0200428
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100429static inline void console_puts(int file, const char *s)
430{
Simon Glass709ea542014-07-23 06:54:59 -0600431 stdio_devices[file]->puts(stdio_devices[file], s);
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100432}
433
Pali Rohár974f4832022-09-05 11:31:17 +0200434#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
435static inline void console_flush(int file)
436{
437 if (stdio_devices[file]->flush)
438 stdio_devices[file]->flush(stdio_devices[file]);
439}
440#endif
441
Tom Rini5e63c962019-10-30 09:18:43 -0400442#if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200443static inline void console_doenv(int file, struct stdio_dev *dev)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100444{
445 console_setfile(file, dev);
446}
Tom Rini5e63c962019-10-30 09:18:43 -0400447#endif
Simon Glassb0265422017-01-16 07:03:26 -0700448#endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100449
Andy Shevchenko09d8f072021-02-11 17:09:39 +0200450static void __maybe_unused console_setfile_and_devices(int file, struct stdio_dev *dev)
451{
452 console_setfile(file, dev);
453 console_devices_set(file, dev);
454}
455
Andy Shevchenko41f668b2020-12-21 14:30:00 +0200456int console_start(int file, struct stdio_dev *sdev)
457{
458 int error;
459
Andy Shevchenko95aaf402020-12-21 14:30:01 +0200460 if (!console_needs_start_stop(file, sdev))
461 return 0;
462
Andy Shevchenko41f668b2020-12-21 14:30:00 +0200463 /* Start new device */
464 if (sdev->start) {
465 error = sdev->start(sdev);
466 /* If it's not started don't use it */
467 if (error < 0)
468 return error;
469 }
470 return 0;
471}
472
473void console_stop(int file, struct stdio_dev *sdev)
474{
Andy Shevchenko95aaf402020-12-21 14:30:01 +0200475 if (!console_needs_start_stop(file, sdev))
476 return;
477
Andy Shevchenko41f668b2020-12-21 14:30:00 +0200478 if (sdev->stop)
479 sdev->stop(sdev);
480}
481
wdenk47d1a6e2002-11-03 00:01:44 +0000482/** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
483
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200484int serial_printf(const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000485{
486 va_list args;
487 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200488 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000489
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100490 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000491
492 /* For this to work, printbuffer must be larger than
493 * anything we ever want to print.
494 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000495 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100496 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000497
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100498 serial_puts(printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200499 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000500}
501
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100502int fgetc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000503{
Heinrich Schuchardt27380d82022-10-22 11:32:34 +0200504 if ((unsigned int)file < MAX_FILES) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100505 /*
506 * Effectively poll for input wherever it may be available.
507 */
508 for (;;) {
Stefan Roese29caf932022-09-02 14:10:46 +0200509 schedule();
Patrick Delaunaya17b38c2020-12-18 12:46:46 +0100510 if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
511 /*
512 * Upper layer may have already called tstc() so
513 * check for that first.
514 */
515 if (console_has_tstc())
516 return console_getc(file);
517 console_tstc(file);
518 } else {
519 if (console_tstc(file))
520 return console_getc(file);
521 }
522
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100523 /*
524 * If the watchdog must be rate-limited then it should
525 * already be handled in board-specific code.
526 */
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100527 if (IS_ENABLED(CONFIG_WATCHDOG))
528 udelay(1);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100529 }
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100530 }
wdenk47d1a6e2002-11-03 00:01:44 +0000531
532 return -1;
533}
534
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100535int ftstc(int file)
wdenk47d1a6e2002-11-03 00:01:44 +0000536{
Heinrich Schuchardt27380d82022-10-22 11:32:34 +0200537 if ((unsigned int)file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100538 return console_tstc(file);
wdenk47d1a6e2002-11-03 00:01:44 +0000539
540 return -1;
541}
542
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100543void fputc(int file, const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000544{
Heinrich Schuchardt27380d82022-10-22 11:32:34 +0200545 if ((unsigned int)file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100546 console_putc(file, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000547}
548
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100549void fputs(int file, const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000550{
Heinrich Schuchardt27380d82022-10-22 11:32:34 +0200551 if ((unsigned int)file < MAX_FILES)
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +0100552 console_puts(file, s);
wdenk47d1a6e2002-11-03 00:01:44 +0000553}
554
Pali Rohár974f4832022-09-05 11:31:17 +0200555#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
556void fflush(int file)
557{
Heinrich Schuchardt27380d82022-10-22 11:32:34 +0200558 if ((unsigned int)file < MAX_FILES)
Pali Rohár974f4832022-09-05 11:31:17 +0200559 console_flush(file);
560}
561#endif
562
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200563int fprintf(int file, const char *fmt, ...)
wdenk47d1a6e2002-11-03 00:01:44 +0000564{
565 va_list args;
566 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200567 char printbuffer[CONFIG_SYS_PBSIZE];
wdenk47d1a6e2002-11-03 00:01:44 +0000568
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100569 va_start(args, fmt);
wdenk47d1a6e2002-11-03 00:01:44 +0000570
571 /* For this to work, printbuffer must be larger than
572 * anything we ever want to print.
573 */
Sonny Rao068af6f2011-10-10 09:22:31 +0000574 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100575 va_end(args);
wdenk47d1a6e2002-11-03 00:01:44 +0000576
577 /* Send to desired file */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100578 fputs(file, printbuffer);
Wolfgang Denkd9c27252010-06-20 17:14:14 +0200579 return i;
wdenk47d1a6e2002-11-03 00:01:44 +0000580}
581
582/** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
583
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200584int getchar(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000585{
Patrick Delaunay1e993712020-12-18 12:46:45 +0100586 int ch;
587
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100588 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100589 return 0;
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100590
Graeme Russe3e454c2011-08-29 02:14:05 +0000591 if (!gd->have_console)
592 return 0;
593
Patrick Delaunay1e993712020-12-18 12:46:45 +0100594 ch = console_record_getc();
595 if (ch != -1)
596 return ch;
Simon Glass9854a872015-11-08 23:47:48 -0700597
wdenk47d1a6e2002-11-03 00:01:44 +0000598 if (gd->flags & GD_FLG_DEVINIT) {
599 /* Get from the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100600 return fgetc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000601 }
602
603 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100604 return serial_getc();
wdenk47d1a6e2002-11-03 00:01:44 +0000605}
606
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100607int tstc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000608{
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100609 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100610 return 0;
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100611
Graeme Russe3e454c2011-08-29 02:14:05 +0000612 if (!gd->have_console)
613 return 0;
Patrick Delaunay1e993712020-12-18 12:46:45 +0100614
615 if (console_record_tstc())
616 return 1;
617
wdenk47d1a6e2002-11-03 00:01:44 +0000618 if (gd->flags & GD_FLG_DEVINIT) {
619 /* Test the standard input */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100620 return ftstc(stdin);
wdenk47d1a6e2002-11-03 00:01:44 +0000621 }
622
623 /* Send directly to the handler */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100624 return serial_tstc();
wdenk47d1a6e2002-11-03 00:01:44 +0000625}
626
Siarhei Siamashka27669662015-01-08 09:02:31 +0200627#define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
628#define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
629
Simon Glass8f925582016-10-17 20:12:36 -0600630#if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
Rasmus Villemoescff29632022-05-03 14:37:39 +0200631#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_VAL(PRE_CON_BUF_SZ))
Graeme Russ9558b482011-09-01 00:48:27 +0000632
633static void pre_console_putc(const char c)
634{
Simon Glass4e6bafa2017-06-15 21:37:52 -0600635 char *buffer;
636
Rasmus Villemoes04a20ca2022-05-03 15:13:27 +0200637 if (gd->precon_buf_idx < 0)
638 return;
639
Rasmus Villemoescff29632022-05-03 14:37:39 +0200640 buffer = map_sysmem(CONFIG_VAL(PRE_CON_BUF_ADDR), CONFIG_VAL(PRE_CON_BUF_SZ));
Graeme Russ9558b482011-09-01 00:48:27 +0000641
642 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
Simon Glass4e6bafa2017-06-15 21:37:52 -0600643
644 unmap_sysmem(buffer);
Graeme Russ9558b482011-09-01 00:48:27 +0000645}
646
Soeren Mochbe135cc2017-11-04 16:14:09 +0100647static void pre_console_puts(const char *s)
648{
Rasmus Villemoes04a20ca2022-05-03 15:13:27 +0200649 if (gd->precon_buf_idx < 0)
650 return;
651
Soeren Mochbe135cc2017-11-04 16:14:09 +0100652 while (*s)
653 pre_console_putc(*s++);
654}
655
Siarhei Siamashka27669662015-01-08 09:02:31 +0200656static void print_pre_console_buffer(int flushpoint)
Graeme Russ9558b482011-09-01 00:48:27 +0000657{
Rasmus Villemoes04a20ca2022-05-03 15:13:27 +0200658 long in = 0, out = 0;
Rasmus Villemoescff29632022-05-03 14:37:39 +0200659 char buf_out[CONFIG_VAL(PRE_CON_BUF_SZ) + 1];
Simon Glass4e6bafa2017-06-15 21:37:52 -0600660 char *buf_in;
Graeme Russ9558b482011-09-01 00:48:27 +0000661
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100662 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT))
Patrick Delaunay13551b92019-08-02 14:58:10 +0200663 return;
Patrick Delaunay13551b92019-08-02 14:58:10 +0200664
Rasmus Villemoescff29632022-05-03 14:37:39 +0200665 buf_in = map_sysmem(CONFIG_VAL(PRE_CON_BUF_ADDR), CONFIG_VAL(PRE_CON_BUF_SZ));
666 if (gd->precon_buf_idx > CONFIG_VAL(PRE_CON_BUF_SZ))
667 in = gd->precon_buf_idx - CONFIG_VAL(PRE_CON_BUF_SZ);
Graeme Russ9558b482011-09-01 00:48:27 +0000668
Hans de Goedea8552c72015-05-05 13:13:36 +0200669 while (in < gd->precon_buf_idx)
670 buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
Simon Glass4e6bafa2017-06-15 21:37:52 -0600671 unmap_sysmem(buf_in);
Hans de Goedea8552c72015-05-05 13:13:36 +0200672
673 buf_out[out] = 0;
674
Rasmus Villemoes04a20ca2022-05-03 15:13:27 +0200675 gd->precon_buf_idx = -1;
Hans de Goedea8552c72015-05-05 13:13:36 +0200676 switch (flushpoint) {
677 case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
678 puts(buf_out);
679 break;
680 case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
Simon Glass493a4c82020-07-02 21:12:13 -0600681 console_puts_select(stdout, false, buf_out);
Hans de Goedea8552c72015-05-05 13:13:36 +0200682 break;
683 }
Rasmus Villemoes04a20ca2022-05-03 15:13:27 +0200684 gd->precon_buf_idx = in;
Graeme Russ9558b482011-09-01 00:48:27 +0000685}
686#else
687static inline void pre_console_putc(const char c) {}
Soeren Mochbe135cc2017-11-04 16:14:09 +0100688static inline void pre_console_puts(const char *s) {}
Siarhei Siamashka27669662015-01-08 09:02:31 +0200689static inline void print_pre_console_buffer(int flushpoint) {}
Graeme Russ9558b482011-09-01 00:48:27 +0000690#endif
691
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100692void putc(const char c)
wdenk47d1a6e2002-11-03 00:01:44 +0000693{
Patrick Delaunay93cdb522020-11-27 11:20:56 +0100694 if (!gd)
695 return;
Patrick Delaunay1e993712020-12-18 12:46:45 +0100696
697 console_record_putc(c);
698
Simon Glass64e9b4f2017-12-04 13:48:19 -0700699 /* sandbox can send characters to stdout before it has a console */
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100700 if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
Simon Glass64e9b4f2017-12-04 13:48:19 -0700701 os_putc(c);
702 return;
703 }
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100704
Simon Glassd6ea5302015-06-23 15:38:33 -0600705 /* if we don't have a console yet, use the debug UART */
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100706 if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
Simon Glassd6ea5302015-06-23 15:38:33 -0600707 printch(c);
708 return;
709 }
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100710
711 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
Patrick Delaunay13551b92019-08-02 14:58:10 +0200712 if (!(gd->flags & GD_FLG_DEVINIT))
713 pre_console_putc(c);
wdenkf6e20fc2004-02-08 19:38:38 +0000714 return;
Patrick Delaunay13551b92019-08-02 14:58:10 +0200715 }
wdenka6cccae2004-02-06 21:48:22 +0000716
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100717 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100718 return;
Mark Jacksonf5c3ba72008-08-25 19:21:30 +0100719
Graeme Russe3e454c2011-08-29 02:14:05 +0000720 if (!gd->have_console)
Graeme Russ9558b482011-09-01 00:48:27 +0000721 return pre_console_putc(c);
Graeme Russe3e454c2011-08-29 02:14:05 +0000722
wdenk47d1a6e2002-11-03 00:01:44 +0000723 if (gd->flags & GD_FLG_DEVINIT) {
724 /* Send to the standard output */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100725 fputc(stdout, c);
wdenk47d1a6e2002-11-03 00:01:44 +0000726 } else {
727 /* Send directly to the handler */
Siarhei Siamashka27669662015-01-08 09:02:31 +0200728 pre_console_putc(c);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100729 serial_putc(c);
wdenk47d1a6e2002-11-03 00:01:44 +0000730 }
731}
732
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100733void puts(const char *s)
wdenk47d1a6e2002-11-03 00:01:44 +0000734{
Patrick Delaunay93cdb522020-11-27 11:20:56 +0100735 if (!gd)
736 return;
Patrick Delaunay1e993712020-12-18 12:46:45 +0100737
738 console_record_puts(s);
739
Simon Glass36bcea62018-11-15 18:44:04 -0700740 /* sandbox can send characters to stdout before it has a console */
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100741 if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
Simon Glass36bcea62018-11-15 18:44:04 -0700742 os_puts(s);
743 return;
744 }
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100745
746 if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
Soeren Mochbe135cc2017-11-04 16:14:09 +0100747 while (*s) {
748 int ch = *s++;
749
750 printch(ch);
751 }
752 return;
753 }
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100754
755 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
Patrick Delaunay13551b92019-08-02 14:58:10 +0200756 if (!(gd->flags & GD_FLG_DEVINIT))
757 pre_console_puts(s);
Soeren Mochbe135cc2017-11-04 16:14:09 +0100758 return;
Patrick Delaunay13551b92019-08-02 14:58:10 +0200759 }
Soeren Mochbe135cc2017-11-04 16:14:09 +0100760
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100761 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
Soeren Mochbe135cc2017-11-04 16:14:09 +0100762 return;
Soeren Mochbe135cc2017-11-04 16:14:09 +0100763
764 if (!gd->have_console)
765 return pre_console_puts(s);
766
767 if (gd->flags & GD_FLG_DEVINIT) {
768 /* Send to the standard output */
769 fputs(stdout, s);
770 } else {
771 /* Send directly to the handler */
772 pre_console_puts(s);
773 serial_puts(s);
774 }
wdenk47d1a6e2002-11-03 00:01:44 +0000775}
776
Pali Rohár974f4832022-09-05 11:31:17 +0200777#ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
778void flush(void)
779{
780 if (!gd)
781 return;
782
783 /* sandbox can send characters to stdout before it has a console */
784 if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
785 os_flush();
786 return;
787 }
788
789 if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY))
790 return;
791
792 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT))
793 return;
794
795 if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
796 return;
797
798 if (!gd->have_console)
799 return;
800
801 if (gd->flags & GD_FLG_DEVINIT) {
802 /* Send to the standard output */
803 fflush(stdout);
Pali Rohár78b52432022-09-05 11:31:19 +0200804 } else {
805 /* Send directly to the handler */
806 serial_flush();
Pali Rohár974f4832022-09-05 11:31:17 +0200807 }
808}
809#endif
810
Simon Glass9854a872015-11-08 23:47:48 -0700811#ifdef CONFIG_CONSOLE_RECORD
812int console_record_init(void)
813{
814 int ret;
815
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100816 ret = membuff_new((struct membuff *)&gd->console_out,
Simon Glass8ce465e2021-10-23 17:26:03 -0600817 gd->flags & GD_FLG_RELOC ?
818 CONFIG_CONSOLE_RECORD_OUT_SIZE :
819 CONFIG_CONSOLE_RECORD_OUT_SIZE_F);
Simon Glass9854a872015-11-08 23:47:48 -0700820 if (ret)
821 return ret;
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100822 ret = membuff_new((struct membuff *)&gd->console_in,
823 CONFIG_CONSOLE_RECORD_IN_SIZE);
Simon Glass9854a872015-11-08 23:47:48 -0700824
Ion Agorria90087dd2024-01-05 09:22:09 +0200825 /* Start recording from the beginning */
826 gd->flags |= GD_FLG_RECORD;
827
Simon Glass9854a872015-11-08 23:47:48 -0700828 return ret;
829}
830
831void console_record_reset(void)
832{
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100833 membuff_purge((struct membuff *)&gd->console_out);
834 membuff_purge((struct membuff *)&gd->console_in);
Simon Glassc1a2bb42021-05-08 06:59:56 -0600835 gd->flags &= ~GD_FLG_RECORD_OVF;
Simon Glass9854a872015-11-08 23:47:48 -0700836}
837
Simon Glassbd347152020-07-28 19:41:11 -0600838int console_record_reset_enable(void)
Simon Glass9854a872015-11-08 23:47:48 -0700839{
840 console_record_reset();
841 gd->flags |= GD_FLG_RECORD;
Simon Glassbd347152020-07-28 19:41:11 -0600842
843 return 0;
Simon Glass9854a872015-11-08 23:47:48 -0700844}
Simon Glassb6123122020-01-27 08:49:54 -0700845
846int console_record_readline(char *str, int maxlen)
847{
Simon Glassc1a2bb42021-05-08 06:59:56 -0600848 if (gd->flags & GD_FLG_RECORD_OVF)
849 return -ENOSPC;
Simon Glass88ae69f2024-08-22 07:57:47 -0600850 if (console_record_isempty())
851 return -ENOENT;
Simon Glassc1a2bb42021-05-08 06:59:56 -0600852
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100853 return membuff_readline((struct membuff *)&gd->console_out, str,
Ion Agorriae58bafc2024-01-05 09:22:10 +0200854 maxlen, '\0', false);
Simon Glassb6123122020-01-27 08:49:54 -0700855}
856
857int console_record_avail(void)
858{
Heinrich Schuchardta3a9e042020-02-12 18:23:49 +0100859 return membuff_avail((struct membuff *)&gd->console_out);
Simon Glassb6123122020-01-27 08:49:54 -0700860}
861
Ion Agorria9ce75f42024-01-05 09:22:08 +0200862bool console_record_isempty(void)
863{
864 return membuff_isempty((struct membuff *)&gd->console_out);
865}
866
Steffen Jaeckel25c8b9f2021-07-08 15:57:40 +0200867int console_in_puts(const char *str)
868{
869 return membuff_put((struct membuff *)&gd->console_in, str, strlen(str));
870}
871
Simon Glass9854a872015-11-08 23:47:48 -0700872#endif
873
wdenk47d1a6e2002-11-03 00:01:44 +0000874/* test if ctrl-c was pressed */
875static int ctrlc_disabled = 0; /* see disable_ctrl() */
876static int ctrlc_was_pressed = 0;
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100877int ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000878{
wdenk47d1a6e2002-11-03 00:01:44 +0000879 if (!ctrlc_disabled && gd->have_console) {
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100880 if (tstc()) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200881 switch (getchar()) {
wdenk47d1a6e2002-11-03 00:01:44 +0000882 case 0x03: /* ^C - Control C */
883 ctrlc_was_pressed = 1;
884 return 1;
885 default:
886 break;
887 }
888 }
889 }
Simon Glass8969ea32014-09-14 12:40:15 -0600890
wdenk47d1a6e2002-11-03 00:01:44 +0000891 return 0;
892}
Pierre Auberta5dffa42014-04-24 10:30:07 +0200893/* Reads user's confirmation.
894 Returns 1 if user's input is "y", "Y", "yes" or "YES"
895*/
896int confirm_yesno(void)
897{
898 int i;
899 char str_input[5];
wdenk47d1a6e2002-11-03 00:01:44 +0000900
Pierre Auberta5dffa42014-04-24 10:30:07 +0200901 /* Flush input */
902 while (tstc())
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200903 getchar();
Pierre Auberta5dffa42014-04-24 10:30:07 +0200904 i = 0;
905 while (i < sizeof(str_input)) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200906 str_input[i] = getchar();
Pierre Auberta5dffa42014-04-24 10:30:07 +0200907 putc(str_input[i]);
908 if (str_input[i] == '\r')
909 break;
910 i++;
911 }
912 putc('\n');
913 if (strncmp(str_input, "y\r", 2) == 0 ||
914 strncmp(str_input, "Y\r", 2) == 0 ||
915 strncmp(str_input, "yes\r", 4) == 0 ||
916 strncmp(str_input, "YES\r", 4) == 0)
917 return 1;
918 return 0;
919}
wdenk47d1a6e2002-11-03 00:01:44 +0000920/* pass 1 to disable ctrlc() checking, 0 to enable.
921 * returns previous state
922 */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100923int disable_ctrlc(int disable)
wdenk47d1a6e2002-11-03 00:01:44 +0000924{
925 int prev = ctrlc_disabled; /* save previous state */
926
927 ctrlc_disabled = disable;
928 return prev;
929}
930
931int had_ctrlc (void)
932{
933 return ctrlc_was_pressed;
934}
935
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100936void clear_ctrlc(void)
wdenk47d1a6e2002-11-03 00:01:44 +0000937{
938 ctrlc_was_pressed = 0;
939}
940
wdenk47d1a6e2002-11-03 00:01:44 +0000941/** U-Boot INIT FUNCTIONS *************************************************/
942
Andy Shevchenko32324872020-12-21 14:30:03 +0200943struct stdio_dev *console_search_dev(int flags, const char *name)
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200944{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200945 struct stdio_dev *dev;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200946
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200947 dev = stdio_get_by_name(name);
Simon Glassa2931b32016-02-06 14:31:37 -0700948#ifdef CONFIG_VIDCONSOLE_AS_LCD
Patrick Delaunay27b5b9e2020-07-01 14:56:10 +0200949 if (!dev && !strcmp(name, CONFIG_VIDCONSOLE_AS_NAME))
Simon Glassa2931b32016-02-06 14:31:37 -0700950 dev = stdio_get_by_name("vidconsole");
951#endif
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200952
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100953 if (dev && (dev->flags & flags))
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200954 return dev;
955
956 return NULL;
957}
958
Mike Frysingerd7be3052010-10-20 07:18:03 -0400959int console_assign(int file, const char *devname)
wdenk47d1a6e2002-11-03 00:01:44 +0000960{
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +0200961 int flag;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200962 struct stdio_dev *dev;
wdenk47d1a6e2002-11-03 00:01:44 +0000963
964 /* Check for valid file */
Andy Shevchenko7b9ca3f2021-02-11 17:09:37 +0200965 flag = stdio_file_to_flags(file);
966 if (flag < 0)
967 return flag;
wdenk47d1a6e2002-11-03 00:01:44 +0000968
969 /* Check for valid device name */
970
Andy Shevchenko32324872020-12-21 14:30:03 +0200971 dev = console_search_dev(flag, devname);
wdenk47d1a6e2002-11-03 00:01:44 +0000972
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +0100973 if (dev)
974 return console_setfile(file, dev);
wdenk47d1a6e2002-11-03 00:01:44 +0000975
976 return -1;
977}
978
Patrick Delaunay13551b92019-08-02 14:58:10 +0200979/* return true if the 'silent' flag is removed */
980static bool console_update_silent(void)
Chris Packham43e0a3d2016-09-23 15:59:43 +1200981{
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100982 unsigned long flags = gd->flags;
983
984 if (!IS_ENABLED(CONFIG_SILENT_CONSOLE))
985 return false;
986
Harald Seiler33965c72022-07-06 13:19:10 +0200987 if (IS_ENABLED(CONFIG_SILENT_CONSOLE_UNTIL_ENV) && !(gd->flags & GD_FLG_ENV_READY)) {
988 gd->flags |= GD_FLG_SILENT;
989 return false;
990 }
991
Patrick Delaunay13551b92019-08-02 14:58:10 +0200992 if (env_get("silent")) {
Chris Packham43e0a3d2016-09-23 15:59:43 +1200993 gd->flags |= GD_FLG_SILENT;
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100994 return false;
Patrick Delaunay13551b92019-08-02 14:58:10 +0200995 }
Patrick Delaunay13551b92019-08-02 14:58:10 +0200996
Patrick Delaunayc04f8562020-12-18 12:46:43 +0100997 gd->flags &= ~GD_FLG_SILENT;
998
999 return !!(flags & GD_FLG_SILENT);
Chris Packham43e0a3d2016-09-23 15:59:43 +12001000}
1001
Simon Glassb0895382017-06-15 21:37:50 -06001002int console_announce_r(void)
1003{
1004#if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
1005 char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
1006
1007 display_options_get_banner(false, buf, sizeof(buf));
1008
Simon Glass493a4c82020-07-02 21:12:13 -06001009 console_puts_select(stdout, false, buf);
Simon Glassb0895382017-06-15 21:37:50 -06001010#endif
1011
1012 return 0;
1013}
1014
wdenk47d1a6e2002-11-03 00:01:44 +00001015/* Called before relocation - use serial functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +01001016int console_init_f(void)
wdenk47d1a6e2002-11-03 00:01:44 +00001017{
wdenk47d1a6e2002-11-03 00:01:44 +00001018 gd->have_console = 1;
wdenkf72da342003-10-10 10:05:42 +00001019
Chris Packham43e0a3d2016-09-23 15:59:43 +12001020 console_update_silent();
wdenkf72da342003-10-10 10:05:42 +00001021
Siarhei Siamashka27669662015-01-08 09:02:31 +02001022 print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
Graeme Russ9558b482011-09-01 00:48:27 +00001023
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +01001024 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +00001025}
1026
Simon Glasscde03fa2023-10-01 19:15:23 -06001027int console_clear(void)
1028{
1029 /*
1030 * Send clear screen and home
1031 *
1032 * FIXME(Heinrich Schuchardt <xypron.glpk@gmx.de>): This should go
1033 * through an API and only be written to serial terminals, not video
1034 * displays
1035 */
1036 printf(CSI "2J" CSI "1;1H");
1037 if (IS_ENABLED(CONFIG_VIDEO_ANSI))
1038 return 0;
1039
1040 if (IS_ENABLED(CONFIG_VIDEO)) {
1041 struct udevice *dev;
1042 int ret;
1043
1044 ret = uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev);
1045 if (ret)
1046 return ret;
1047 ret = vidconsole_clear_and_reset(dev);
1048 if (ret)
1049 return ret;
1050 }
1051
1052 return 0;
1053}
1054
Patrice Chotard9152a512024-01-17 13:37:13 +01001055static char *get_stdio(const u8 std)
1056{
1057 return stdio_devices[std] ? stdio_devices[std]->name : "No devices available!";
1058}
1059
Bin Meng75bfc6f2023-07-23 12:40:35 +08001060static void stdio_print_current_devices(void)
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +02001061{
Patrice Chotard9152a512024-01-17 13:37:13 +01001062 char *stdinname = NULL;
1063 char *stdoutname = NULL;
1064 char *stderrname = NULL;
Bin Mengf30fd552023-07-23 12:40:36 +08001065
Bin Meng6b343ab2023-07-23 12:40:37 +08001066 if (CONFIG_IS_ENABLED(CONSOLE_MUX) &&
1067 CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)) {
1068 /* stdin stdout and stderr are in environment */
1069 stdinname = env_get("stdin");
1070 stdoutname = env_get("stdout");
1071 stderrname = env_get("stderr");
Bin Meng6b343ab2023-07-23 12:40:37 +08001072 }
Bin Mengf30fd552023-07-23 12:40:36 +08001073
Patrice Chotard9152a512024-01-17 13:37:13 +01001074 stdinname = stdinname ? : get_stdio(stdin);
1075 stdoutname = stdoutname ? : get_stdio(stdout);
1076 stderrname = stderrname ? : get_stdio(stderr);
1077
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +02001078 /* Print information */
1079 puts("In: ");
Bin Mengf30fd552023-07-23 12:40:36 +08001080 printf("%s\n", stdinname);
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +02001081
1082 puts("Out: ");
Bin Mengf30fd552023-07-23 12:40:36 +08001083 printf("%s\n", stdoutname);
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +02001084
1085 puts("Err: ");
Bin Mengf30fd552023-07-23 12:40:36 +08001086 printf("%s\n", stderrname);
Jean-Christophe PLAGNIOL-VILLARD7e3be7c2009-05-16 12:14:55 +02001087}
1088
Simon Glassb0265422017-01-16 07:03:26 -07001089#if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
wdenk47d1a6e2002-11-03 00:01:44 +00001090/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +01001091int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +00001092{
1093 char *stdinname, *stdoutname, *stderrname;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +02001094 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
wdenk6e592382004-04-18 17:39:38 +00001095 int i;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +01001096 int iomux_err = 0;
Patrick Delaunay13551b92019-08-02 14:58:10 +02001097 int flushpoint;
wdenk47d1a6e2002-11-03 00:01:44 +00001098
Patrick Delaunaybf46be72019-08-02 14:58:09 +02001099 /* update silent for env loaded from flash (initr_env) */
Patrick Delaunay13551b92019-08-02 14:58:10 +02001100 if (console_update_silent())
1101 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
1102 else
1103 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
Patrick Delaunaybf46be72019-08-02 14:58:09 +02001104
wdenk47d1a6e2002-11-03 00:01:44 +00001105 /* set default handlers at first */
Martin Dorwig49cad542015-01-26 15:22:54 -07001106 gd->jt->getc = serial_getc;
1107 gd->jt->tstc = serial_tstc;
1108 gd->jt->putc = serial_putc;
1109 gd->jt->puts = serial_puts;
1110 gd->jt->printf = serial_printf;
wdenk47d1a6e2002-11-03 00:01:44 +00001111
1112 /* stdin stdout and stderr are in environment */
1113 /* scan for it */
Simon Glass00caae62017-08-03 12:22:12 -06001114 stdinname = env_get("stdin");
1115 stdoutname = env_get("stdout");
1116 stderrname = env_get("stderr");
wdenk47d1a6e2002-11-03 00:01:44 +00001117
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001118 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
Andy Shevchenko32324872020-12-21 14:30:03 +02001119 inputdev = console_search_dev(DEV_FLAGS_INPUT, stdinname);
1120 outputdev = console_search_dev(DEV_FLAGS_OUTPUT, stdoutname);
1121 errdev = console_search_dev(DEV_FLAGS_OUTPUT, stderrname);
Patrick Delaunayc04f8562020-12-18 12:46:43 +01001122 if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
1123 iomux_err = iomux_doenv(stdin, stdinname);
1124 iomux_err += iomux_doenv(stdout, stdoutname);
1125 iomux_err += iomux_doenv(stderr, stderrname);
1126 if (!iomux_err)
1127 /* Successful, so skip all the code below. */
1128 goto done;
1129 }
wdenk47d1a6e2002-11-03 00:01:44 +00001130 }
1131 /* if the devices are overwritten or not found, use default device */
1132 if (inputdev == NULL) {
Andy Shevchenko32324872020-12-21 14:30:03 +02001133 inputdev = console_search_dev(DEV_FLAGS_INPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +00001134 }
1135 if (outputdev == NULL) {
Andy Shevchenko32324872020-12-21 14:30:03 +02001136 outputdev = console_search_dev(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +00001137 }
1138 if (errdev == NULL) {
Andy Shevchenko32324872020-12-21 14:30:03 +02001139 errdev = console_search_dev(DEV_FLAGS_OUTPUT, "serial");
wdenk47d1a6e2002-11-03 00:01:44 +00001140 }
1141 /* Initializes output console first */
1142 if (outputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +01001143 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +01001144 console_doenv(stdout, outputdev);
wdenk47d1a6e2002-11-03 00:01:44 +00001145 }
1146 if (errdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +01001147 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +01001148 console_doenv(stderr, errdev);
wdenk47d1a6e2002-11-03 00:01:44 +00001149 }
1150 if (inputdev != NULL) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +01001151 /* need to set a console if not done above. */
Jean-Christophe PLAGNIOL-VILLARD5f032012009-02-01 17:07:52 +01001152 console_doenv(stdin, inputdev);
wdenk47d1a6e2002-11-03 00:01:44 +00001153 }
1154
Gary Jennejohn16a28ef2008-11-06 15:04:23 +01001155done:
Gary Jennejohn16a28ef2008-11-06 15:04:23 +01001156
Patrick Delaunayc04f8562020-12-18 12:46:43 +01001157 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
1158 stdio_print_current_devices();
1159
Simon Glassa2931b32016-02-06 14:31:37 -07001160#ifdef CONFIG_VIDCONSOLE_AS_LCD
Patrick Delaunay27b5b9e2020-07-01 14:56:10 +02001161 if (strstr(stdoutname, CONFIG_VIDCONSOLE_AS_NAME))
Anatolij Gustschin22b897a2020-05-23 17:11:20 +02001162 printf("Warning: Please change '%s' to 'vidconsole' in stdout/stderr environment vars\n",
Patrick Delaunay27b5b9e2020-07-01 14:56:10 +02001163 CONFIG_VIDCONSOLE_AS_NAME);
Simon Glassa2931b32016-02-06 14:31:37 -07001164#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001165
Patrick Delaunayc04f8562020-12-18 12:46:43 +01001166 if (IS_ENABLED(CONFIG_SYS_CONSOLE_ENV_OVERWRITE)) {
1167 /* set the environment variables (will overwrite previous env settings) */
1168 for (i = 0; i < MAX_FILES; i++)
1169 env_set(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +00001170 }
wdenk47d1a6e2002-11-03 00:01:44 +00001171
Joe Hershbergerc4e00572012-12-11 22:16:19 -06001172 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
1173
Patrick Delaunay13551b92019-08-02 14:58:10 +02001174 print_pre_console_buffer(flushpoint);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +01001175 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +00001176}
1177
Simon Glassb0265422017-01-16 07:03:26 -07001178#else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
wdenk47d1a6e2002-11-03 00:01:44 +00001179
1180/* Called after the relocation - use desired console functions */
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +01001181int console_init_r(void)
wdenk47d1a6e2002-11-03 00:01:44 +00001182{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +02001183 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +02001184 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +02001185 struct list_head *list = stdio_get_list();
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +02001186 struct list_head *pos;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +02001187 struct stdio_dev *dev;
Patrick Delaunay13551b92019-08-02 14:58:10 +02001188 int flushpoint;
wdenk47d1a6e2002-11-03 00:01:44 +00001189
Patrick Delaunaybf46be72019-08-02 14:58:09 +02001190 /* update silent for env loaded from flash (initr_env) */
Patrick Delaunay13551b92019-08-02 14:58:10 +02001191 if (console_update_silent())
1192 flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
1193 else
1194 flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
Chris Packham43e0a3d2016-09-23 15:59:43 +12001195
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +01001196 /*
1197 * suppress all output if splash screen is enabled and we have
Anatolij Gustschina7490812010-03-16 15:29:33 +01001198 * a bmp to display. We redirect the output from frame buffer
1199 * console to serial console in this case or suppress it if
1200 * "silent" mode was requested.
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +01001201 */
Patrick Delaunayc04f8562020-12-18 12:46:43 +01001202 if (IS_ENABLED(CONFIG_SPLASH_SCREEN) && env_get("splashimage")) {
Anatolij Gustschina7490812010-03-16 15:29:33 +01001203 if (!(gd->flags & GD_FLG_SILENT))
Andy Shevchenko32324872020-12-21 14:30:03 +02001204 outputdev = console_search_dev (DEV_FLAGS_OUTPUT, "serial");
Anatolij Gustschina7490812010-03-16 15:29:33 +01001205 }
wdenkf72da342003-10-10 10:05:42 +00001206
wdenk47d1a6e2002-11-03 00:01:44 +00001207 /* Scan devices looking for input and output devices */
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +02001208 list_for_each(pos, list) {
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +02001209 dev = list_entry(pos, struct stdio_dev, list);
wdenk47d1a6e2002-11-03 00:01:44 +00001210
1211 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
1212 inputdev = dev;
1213 }
1214 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
1215 outputdev = dev;
1216 }
Jean-Christophe PLAGNIOL-VILLARDc1de7a62008-08-31 04:24:55 +02001217 if(inputdev && outputdev)
1218 break;
wdenk47d1a6e2002-11-03 00:01:44 +00001219 }
1220
1221 /* Initializes output console first */
1222 if (outputdev != NULL) {
Andy Shevchenko09d8f072021-02-11 17:09:39 +02001223 console_setfile_and_devices(stdout, outputdev);
1224 console_setfile_and_devices(stderr, outputdev);
wdenk47d1a6e2002-11-03 00:01:44 +00001225 }
1226
1227 /* Initializes input console */
Andy Shevchenko09d8f072021-02-11 17:09:39 +02001228 if (inputdev != NULL)
1229 console_setfile_and_devices(stdin, inputdev);
wdenk47d1a6e2002-11-03 00:01:44 +00001230
Patrick Delaunayc04f8562020-12-18 12:46:43 +01001231 if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
1232 stdio_print_current_devices();
wdenk47d1a6e2002-11-03 00:01:44 +00001233
1234 /* Setting environment variables */
Tom Rini27b42252018-05-03 09:12:26 -04001235 for (i = 0; i < MAX_FILES; i++) {
Simon Glass382bee52017-08-03 12:22:09 -06001236 env_set(stdio_names[i], stdio_devices[i]->name);
wdenk47d1a6e2002-11-03 00:01:44 +00001237 }
1238
Joe Hershbergerc4e00572012-12-11 22:16:19 -06001239 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
1240
Patrick Delaunay13551b92019-08-02 14:58:10 +02001241 print_pre_console_buffer(flushpoint);
Jean-Christophe PLAGNIOL-VILLARDec6f1492009-02-01 17:07:51 +01001242 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +00001243}
1244
Simon Glassb0265422017-01-16 07:03:26 -07001245#endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */