Blame view

common/console.c 21.3 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
47d1a6e1e   wdenk   Initial revision
2
3
4
  /*
   * (C) Copyright 2000
   * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
47d1a6e1e   wdenk   Initial revision
5
6
7
   */
  
  #include <common.h>
24b852a7a   Simon Glass   Move console defi...
8
  #include <console.h>
d6ea5307d   Simon Glass   dm: Allow debug U...
9
  #include <debug_uart.h>
7b3c4c3a5   Simon Glass   dm: console: Chec...
10
  #include <dm.h>
9fb625ce0   Simon Glass   env: Move env_set...
11
  #include <env.h>
47d1a6e1e   wdenk   Initial revision
12
  #include <stdarg.h>
482f4691a   Jeroen Hofstee   common:console: a...
13
  #include <iomux.h>
47d1a6e1e   wdenk   Initial revision
14
  #include <malloc.h>
4e6bafa56   Simon Glass   console: Use map_...
15
  #include <mapmem.h>
91b136c79   Simon Glass   sandbox: Allow th...
16
  #include <os.h>
849d5d9cd   Joe Hershberger   env: Add a consol...
17
  #include <serial.h>
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
18
  #include <stdio_dev.h>
27b207fd0   wdenk   * Implement new m...
19
  #include <exports.h>
f3998fdc4   Simon Glass   env: Rename envir...
20
  #include <env_internal.h>
644074671   Andreas J. Reichel   watchdog: Fix Wat...
21
  #include <watchdog.h>
47d1a6e1e   wdenk   Initial revision
22

d87080b72   Wolfgang Denk   GCC-4.x fixes: cl...
23
  DECLARE_GLOBAL_DATA_PTR;
849d5d9cd   Joe Hershberger   env: Add a consol...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  static int on_console(const char *name, const char *value, enum env_op op,
  	int flags)
  {
  	int console = -1;
  
  	/* Check for console redirection */
  	if (strcmp(name, "stdin") == 0)
  		console = stdin;
  	else if (strcmp(name, "stdout") == 0)
  		console = stdout;
  	else if (strcmp(name, "stderr") == 0)
  		console = stderr;
  
  	/* if not actually setting a console variable, we don't care */
  	if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
  		return 0;
  
  	switch (op) {
  	case env_op_create:
  	case env_op_overwrite:
b02654294   Simon Glass   console: Don't en...
44
  #if CONFIG_IS_ENABLED(CONSOLE_MUX)
849d5d9cd   Joe Hershberger   env: Add a consol...
45
46
47
48
49
50
  		if (iomux_doenv(console, value))
  			return 1;
  #else
  		/* Try assigning specified device */
  		if (console_assign(console, value) < 0)
  			return 1;
b02654294   Simon Glass   console: Don't en...
51
  #endif
849d5d9cd   Joe Hershberger   env: Add a consol...
52
53
54
55
56
57
58
59
60
61
62
63
64
  		return 0;
  
  	case env_op_delete:
  		if ((flags & H_FORCE) == 0)
  			printf("Can't delete \"%s\"
  ", name);
  		return 1;
  
  	default:
  		return 0;
  	}
  }
  U_BOOT_ENV_CALLBACK(console, on_console);
e080d545f   Joe Hershberger   env: Add a silent...
65
66
67
68
  #ifdef CONFIG_SILENT_CONSOLE
  static int on_silent(const char *name, const char *value, enum env_op op,
  	int flags)
  {
5daf6e56d   Wilson Lee   common: console: ...
69
  #if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_SET)
e080d545f   Joe Hershberger   env: Add a silent...
70
71
72
  	if (flags & H_INTERACTIVE)
  		return 0;
  #endif
5daf6e56d   Wilson Lee   common: console: ...
73
  #if !CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_RELOC)
e080d545f   Joe Hershberger   env: Add a silent...
74
75
76
77
78
79
80
81
82
83
84
85
86
  	if ((flags & H_INTERACTIVE) == 0)
  		return 0;
  #endif
  
  	if (value != NULL)
  		gd->flags |= GD_FLG_SILENT;
  	else
  		gd->flags &= ~GD_FLG_SILENT;
  
  	return 0;
  }
  U_BOOT_ENV_CALLBACK(silent, on_silent);
  #endif
b02654294   Simon Glass   console: Don't en...
87
  #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
47d1a6e1e   wdenk   Initial revision
88
89
90
91
92
  /*
   * if overwrite_console returns 1, the stdin, stderr and stdout
   * are switched to the serial port, else the settings in the
   * environment are used
   */
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
93
  #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
94
95
  extern int overwrite_console(void);
  #define OVERWRITE_CONSOLE overwrite_console()
47d1a6e1e   wdenk   Initial revision
96
  #else
83e40ba75   wdenk   * Patch by Detlev...
97
  #define OVERWRITE_CONSOLE 0
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
98
  #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
47d1a6e1e   wdenk   Initial revision
99

b02654294   Simon Glass   console: Don't en...
100
  #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
47d1a6e1e   wdenk   Initial revision
101

52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
102
  static int console_setfile(int file, struct stdio_dev * dev)
47d1a6e1e   wdenk   Initial revision
103
104
105
106
107
108
109
110
111
112
113
114
  {
  	int error = 0;
  
  	if (dev == NULL)
  		return -1;
  
  	switch (file) {
  	case stdin:
  	case stdout:
  	case stderr:
  		/* Start new device */
  		if (dev->start) {
709ea543b   Simon Glass   stdio: Pass devic...
115
  			error = dev->start(dev);
47d1a6e1e   wdenk   Initial revision
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  			/* If it's not started dont use it */
  			if (error < 0)
  				break;
  		}
  
  		/* Assign the new device (leaving the existing one started) */
  		stdio_devices[file] = dev;
  
  		/*
  		 * Update monitor functions
  		 * (to use the console stuff by other applications)
  		 */
  		switch (file) {
  		case stdin:
49cad5478   Martin Dorwig   Export redesign
130
131
  			gd->jt->getc = getc;
  			gd->jt->tstc = tstc;
47d1a6e1e   wdenk   Initial revision
132
133
  			break;
  		case stdout:
49cad5478   Martin Dorwig   Export redesign
134
135
136
  			gd->jt->putc  = putc;
  			gd->jt->puts  = puts;
  			gd->jt->printf = printf;
47d1a6e1e   wdenk   Initial revision
137
138
139
140
141
142
143
144
145
  			break;
  		}
  		break;
  
  	default:		/* Invalid file ID */
  		error = -1;
  	}
  	return error;
  }
42f9f915c   Simon Glass   console: Unify th...
146
147
148
149
  /**
   * console_dev_is_serial() - Check if a stdio device is a serial device
   *
   * @sdev: Device to check
7b3c4c3a5   Simon Glass   dm: console: Chec...
150
151
   * @return true if this device is in the serial uclass (or for pre-driver-model,
   * whether it is called "serial".
42f9f915c   Simon Glass   console: Unify th...
152
153
154
155
   */
  static bool console_dev_is_serial(struct stdio_dev *sdev)
  {
  	bool is_serial;
7b3c4c3a5   Simon Glass   dm: console: Chec...
156
157
158
159
160
161
162
  #ifdef CONFIG_DM_SERIAL
  	if (sdev->flags & DEV_FLAGS_DM) {
  		struct udevice *dev = sdev->priv;
  
  		is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL;
  	} else
  #endif
42f9f915c   Simon Glass   console: Unify th...
163
164
165
166
  	is_serial = !strcmp(sdev->name, "serial");
  
  	return is_serial;
  }
b02654294   Simon Glass   console: Don't en...
167
  #if CONFIG_IS_ENABLED(CONSOLE_MUX)
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
168
  /** Console I/O multiplexing *******************************************/
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
169
170
  static struct stdio_dev *tstcdev;
  struct stdio_dev **console_devices[MAX_FILES];
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
171
172
173
174
175
176
177
178
  int cd_count[MAX_FILES];
  
  /*
   * This depends on tstc() always being called before getc().
   * This is guaranteed to be true because this routine is called
   * only from fgetc() which assures it.
   * No attempt is made to demultiplex multiple input sources.
   */
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
179
  static int console_getc(int file)
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
180
181
182
183
  {
  	unsigned char ret;
  
  	/* This is never called with testcdev == NULL */
709ea543b   Simon Glass   stdio: Pass devic...
184
  	ret = tstcdev->getc(tstcdev);
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
185
186
187
  	tstcdev = NULL;
  	return ret;
  }
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
188
  static int console_tstc(int file)
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
189
190
  {
  	int i, ret;
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
191
  	struct stdio_dev *dev;
b2f58d8ee   Joe Hershberger   console: Remember...
192
  	int prev;
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
193

b2f58d8ee   Joe Hershberger   console: Remember...
194
  	prev = disable_ctrlc(1);
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
195
196
197
  	for (i = 0; i < cd_count[file]; i++) {
  		dev = console_devices[file][i];
  		if (dev->tstc != NULL) {
709ea543b   Simon Glass   stdio: Pass devic...
198
  			ret = dev->tstc(dev);
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
199
200
  			if (ret > 0) {
  				tstcdev = dev;
b2f58d8ee   Joe Hershberger   console: Remember...
201
  				disable_ctrlc(prev);
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
202
203
204
205
  				return ret;
  			}
  		}
  	}
b2f58d8ee   Joe Hershberger   console: Remember...
206
  	disable_ctrlc(prev);
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
207
208
209
  
  	return 0;
  }
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
210
  static void console_putc(int file, const char c)
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
211
212
  {
  	int i;
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
213
  	struct stdio_dev *dev;
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
214
215
216
217
  
  	for (i = 0; i < cd_count[file]; i++) {
  		dev = console_devices[file][i];
  		if (dev->putc != NULL)
709ea543b   Simon Glass   stdio: Pass devic...
218
  			dev->putc(dev, c);
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
219
220
  	}
  }
a8552c7c9   Hans de Goede   console: Fix pre-...
221
  static void console_puts_noserial(int file, const char *s)
276696675   Siarhei Siamashka   console: Use pre-...
222
223
224
225
226
227
  {
  	int i;
  	struct stdio_dev *dev;
  
  	for (i = 0; i < cd_count[file]; i++) {
  		dev = console_devices[file][i];
42f9f915c   Simon Glass   console: Unify th...
228
  		if (dev->puts != NULL && !console_dev_is_serial(dev))
a8552c7c9   Hans de Goede   console: Fix pre-...
229
  			dev->puts(dev, s);
276696675   Siarhei Siamashka   console: Use pre-...
230
231
  	}
  }
276696675   Siarhei Siamashka   console: Use pre-...
232

5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
233
  static void console_puts(int file, const char *s)
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
234
235
  {
  	int i;
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
236
  	struct stdio_dev *dev;
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
237
238
239
240
  
  	for (i = 0; i < cd_count[file]; i++) {
  		dev = console_devices[file][i];
  		if (dev->puts != NULL)
709ea543b   Simon Glass   stdio: Pass devic...
241
  			dev->puts(dev, s);
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
242
243
  	}
  }
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
244

5e63c96aa   Tom Rini   common/console.c:...
245
  #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
246
  static inline void console_doenv(int file, struct stdio_dev *dev)
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
247
248
249
  {
  	iomux_doenv(file, dev->name);
  }
5e63c96aa   Tom Rini   common/console.c:...
250
  #endif
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
251
252
253
  #else
  static inline int console_getc(int file)
  {
709ea543b   Simon Glass   stdio: Pass devic...
254
  	return stdio_devices[file]->getc(stdio_devices[file]);
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
255
256
257
258
  }
  
  static inline int console_tstc(int file)
  {
709ea543b   Simon Glass   stdio: Pass devic...
259
  	return stdio_devices[file]->tstc(stdio_devices[file]);
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
260
261
262
263
  }
  
  static inline void console_putc(int file, const char c)
  {
709ea543b   Simon Glass   stdio: Pass devic...
264
  	stdio_devices[file]->putc(stdio_devices[file], c);
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
265
  }
a8552c7c9   Hans de Goede   console: Fix pre-...
266
  static inline void console_puts_noserial(int file, const char *s)
276696675   Siarhei Siamashka   console: Use pre-...
267
  {
42f9f915c   Simon Glass   console: Unify th...
268
  	if (!console_dev_is_serial(stdio_devices[file]))
a8552c7c9   Hans de Goede   console: Fix pre-...
269
  		stdio_devices[file]->puts(stdio_devices[file], s);
276696675   Siarhei Siamashka   console: Use pre-...
270
  }
276696675   Siarhei Siamashka   console: Use pre-...
271

5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
272
273
  static inline void console_puts(int file, const char *s)
  {
709ea543b   Simon Glass   stdio: Pass devic...
274
  	stdio_devices[file]->puts(stdio_devices[file], s);
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
275
  }
5e63c96aa   Tom Rini   common/console.c:...
276
  #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
277
  static inline void console_doenv(int file, struct stdio_dev *dev)
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
278
279
280
  {
  	console_setfile(file, dev);
  }
5e63c96aa   Tom Rini   common/console.c:...
281
  #endif
b02654294   Simon Glass   console: Don't en...
282
  #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
283

47d1a6e1e   wdenk   Initial revision
284
  /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
d9c27253c   Wolfgang Denk   Make *printf() re...
285
  int serial_printf(const char *fmt, ...)
47d1a6e1e   wdenk   Initial revision
286
287
288
  {
  	va_list args;
  	uint i;
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
289
  	char printbuffer[CONFIG_SYS_PBSIZE];
47d1a6e1e   wdenk   Initial revision
290

ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
291
  	va_start(args, fmt);
47d1a6e1e   wdenk   Initial revision
292
293
294
295
  
  	/* For this to work, printbuffer must be larger than
  	 * anything we ever want to print.
  	 */
068af6f84   Sonny Rao   Make printf and v...
296
  	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
297
  	va_end(args);
47d1a6e1e   wdenk   Initial revision
298

ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
299
  	serial_puts(printbuffer);
d9c27253c   Wolfgang Denk   Make *printf() re...
300
  	return i;
47d1a6e1e   wdenk   Initial revision
301
  }
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
302
  int fgetc(int file)
47d1a6e1e   wdenk   Initial revision
303
  {
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
304
  	if (file < MAX_FILES) {
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
305
306
307
308
  		/*
  		 * Effectively poll for input wherever it may be available.
  		 */
  		for (;;) {
644074671   Andreas J. Reichel   watchdog: Fix Wat...
309
  			WATCHDOG_RESET();
273a12526   Patrick Delaunay   console: unify fg...
310
  #if CONFIG_IS_ENABLED(CONSOLE_MUX)
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
311
312
313
314
315
  			/*
  			 * Upper layer may have already called tstc() so
  			 * check for that first.
  			 */
  			if (tstcdev != NULL)
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
316
317
  				return console_getc(file);
  			console_tstc(file);
273a12526   Patrick Delaunay   console: unify fg...
318
319
320
321
  #else
  			if (console_tstc(file))
  				return console_getc(file);
  #endif
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
322
323
324
325
326
327
328
329
  #ifdef CONFIG_WATCHDOG
  			/*
  			 * If the watchdog must be rate-limited then it should
  			 * already be handled in board-specific code.
  			 */
  			 udelay(1);
  #endif
  		}
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
330
  	}
47d1a6e1e   wdenk   Initial revision
331
332
333
  
  	return -1;
  }
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
334
  int ftstc(int file)
47d1a6e1e   wdenk   Initial revision
335
336
  {
  	if (file < MAX_FILES)
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
337
  		return console_tstc(file);
47d1a6e1e   wdenk   Initial revision
338
339
340
  
  	return -1;
  }
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
341
  void fputc(int file, const char c)
47d1a6e1e   wdenk   Initial revision
342
343
  {
  	if (file < MAX_FILES)
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
344
  		console_putc(file, c);
47d1a6e1e   wdenk   Initial revision
345
  }
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
346
  void fputs(int file, const char *s)
47d1a6e1e   wdenk   Initial revision
347
348
  {
  	if (file < MAX_FILES)
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
349
  		console_puts(file, s);
47d1a6e1e   wdenk   Initial revision
350
  }
d9c27253c   Wolfgang Denk   Make *printf() re...
351
  int fprintf(int file, const char *fmt, ...)
47d1a6e1e   wdenk   Initial revision
352
353
354
  {
  	va_list args;
  	uint i;
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
355
  	char printbuffer[CONFIG_SYS_PBSIZE];
47d1a6e1e   wdenk   Initial revision
356

ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
357
  	va_start(args, fmt);
47d1a6e1e   wdenk   Initial revision
358
359
360
361
  
  	/* For this to work, printbuffer must be larger than
  	 * anything we ever want to print.
  	 */
068af6f84   Sonny Rao   Make printf and v...
362
  	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
363
  	va_end(args);
47d1a6e1e   wdenk   Initial revision
364
365
  
  	/* Send to desired file */
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
366
  	fputs(file, printbuffer);
d9c27253c   Wolfgang Denk   Make *printf() re...
367
  	return i;
47d1a6e1e   wdenk   Initial revision
368
369
370
  }
  
  /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
371
  int getc(void)
47d1a6e1e   wdenk   Initial revision
372
  {
f5c3ba797   Mark Jackson   Allow console inp...
373
374
375
376
  #ifdef CONFIG_DISABLE_CONSOLE
  	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  		return 0;
  #endif
e3e454cd7   Graeme Russ   console: Squelch ...
377
378
  	if (!gd->have_console)
  		return 0;
9854a8748   Simon Glass   console: Add a co...
379
380
381
  #ifdef CONFIG_CONSOLE_RECORD
  	if (gd->console_in.start) {
  		int ch;
a3a9e0465   Heinrich Schuchardt   common/console.c:...
382
  		ch = membuff_getbyte((struct membuff *)&gd->console_in);
9854a8748   Simon Glass   console: Add a co...
383
384
385
386
  		if (ch != -1)
  			return 1;
  	}
  #endif
47d1a6e1e   wdenk   Initial revision
387
388
  	if (gd->flags & GD_FLG_DEVINIT) {
  		/* Get from the standard input */
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
389
  		return fgetc(stdin);
47d1a6e1e   wdenk   Initial revision
390
391
392
  	}
  
  	/* Send directly to the handler */
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
393
  	return serial_getc();
47d1a6e1e   wdenk   Initial revision
394
  }
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
395
  int tstc(void)
47d1a6e1e   wdenk   Initial revision
396
  {
f5c3ba797   Mark Jackson   Allow console inp...
397
398
399
400
  #ifdef CONFIG_DISABLE_CONSOLE
  	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  		return 0;
  #endif
e3e454cd7   Graeme Russ   console: Squelch ...
401
402
  	if (!gd->have_console)
  		return 0;
9854a8748   Simon Glass   console: Add a co...
403
404
  #ifdef CONFIG_CONSOLE_RECORD
  	if (gd->console_in.start) {
a3a9e0465   Heinrich Schuchardt   common/console.c:...
405
  		if (membuff_peekbyte((struct membuff *)&gd->console_in) != -1)
9854a8748   Simon Glass   console: Add a co...
406
407
408
  			return 1;
  	}
  #endif
47d1a6e1e   wdenk   Initial revision
409
410
  	if (gd->flags & GD_FLG_DEVINIT) {
  		/* Test the standard input */
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
411
  		return ftstc(stdin);
47d1a6e1e   wdenk   Initial revision
412
413
414
  	}
  
  	/* Send directly to the handler */
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
415
  	return serial_tstc();
47d1a6e1e   wdenk   Initial revision
416
  }
276696675   Siarhei Siamashka   console: Use pre-...
417
418
  #define PRE_CONSOLE_FLUSHPOINT1_SERIAL			0
  #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL	1
8f9255841   Simon Glass   Convert CONSOLE_P...
419
  #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
9558b48af   Graeme Russ   console: Implemen...
420
421
422
423
  #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
  
  static void pre_console_putc(const char c)
  {
4e6bafa56   Simon Glass   console: Use map_...
424
425
426
  	char *buffer;
  
  	buffer = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
9558b48af   Graeme Russ   console: Implemen...
427
428
  
  	buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
4e6bafa56   Simon Glass   console: Use map_...
429
430
  
  	unmap_sysmem(buffer);
9558b48af   Graeme Russ   console: Implemen...
431
  }
be135cc5e   Soeren Moch   Revert "console: ...
432
433
434
435
436
  static void pre_console_puts(const char *s)
  {
  	while (*s)
  		pre_console_putc(*s++);
  }
276696675   Siarhei Siamashka   console: Use pre-...
437
  static void print_pre_console_buffer(int flushpoint)
9558b48af   Graeme Russ   console: Implemen...
438
  {
a8552c7c9   Hans de Goede   console: Fix pre-...
439
  	unsigned long in = 0, out = 0;
a8552c7c9   Hans de Goede   console: Fix pre-...
440
  	char buf_out[CONFIG_PRE_CON_BUF_SZ + 1];
4e6bafa56   Simon Glass   console: Use map_...
441
  	char *buf_in;
9558b48af   Graeme Russ   console: Implemen...
442

13551b911   Patrick Delaunay   console: execute ...
443
444
445
446
  #ifdef CONFIG_SILENT_CONSOLE
  	if (gd->flags & GD_FLG_SILENT)
  		return;
  #endif
4e6bafa56   Simon Glass   console: Use map_...
447
  	buf_in = map_sysmem(CONFIG_PRE_CON_BUF_ADDR, CONFIG_PRE_CON_BUF_SZ);
9558b48af   Graeme Russ   console: Implemen...
448
  	if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
a8552c7c9   Hans de Goede   console: Fix pre-...
449
  		in = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
9558b48af   Graeme Russ   console: Implemen...
450

a8552c7c9   Hans de Goede   console: Fix pre-...
451
452
  	while (in < gd->precon_buf_idx)
  		buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
4e6bafa56   Simon Glass   console: Use map_...
453
  	unmap_sysmem(buf_in);
a8552c7c9   Hans de Goede   console: Fix pre-...
454
455
456
457
458
459
460
461
462
463
464
  
  	buf_out[out] = 0;
  
  	switch (flushpoint) {
  	case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
  		puts(buf_out);
  		break;
  	case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
  		console_puts_noserial(stdout, buf_out);
  		break;
  	}
9558b48af   Graeme Russ   console: Implemen...
465
466
467
  }
  #else
  static inline void pre_console_putc(const char c) {}
be135cc5e   Soeren Moch   Revert "console: ...
468
  static inline void pre_console_puts(const char *s) {}
276696675   Siarhei Siamashka   console: Use pre-...
469
  static inline void print_pre_console_buffer(int flushpoint) {}
9558b48af   Graeme Russ   console: Implemen...
470
  #endif
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
471
  void putc(const char c)
47d1a6e1e   wdenk   Initial revision
472
  {
64e9b4f34   Simon Glass   Revert "sandbox: ...
473
474
475
476
477
478
479
  #ifdef CONFIG_SANDBOX
  	/* sandbox can send characters to stdout before it has a console */
  	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  		os_putc(c);
  		return;
  	}
  #endif
d6ea5307d   Simon Glass   dm: Allow debug U...
480
481
482
483
484
485
486
  #ifdef CONFIG_DEBUG_UART
  	/* if we don't have a console yet, use the debug UART */
  	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  		printch(c);
  		return;
  	}
  #endif
af880e247   Simon Glass   console: Fix hand...
487
488
  	if (!gd)
  		return;
9854a8748   Simon Glass   console: Add a co...
489
  #ifdef CONFIG_CONSOLE_RECORD
af880e247   Simon Glass   console: Fix hand...
490
  	if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
a3a9e0465   Heinrich Schuchardt   common/console.c:...
491
  		membuff_putbyte((struct membuff *)&gd->console_out, c);
9854a8748   Simon Glass   console: Add a co...
492
  #endif
a6cccaea5   wdenk   * Patch by Wolter...
493
  #ifdef CONFIG_SILENT_CONSOLE
13551b911   Patrick Delaunay   console: execute ...
494
495
496
  	if (gd->flags & GD_FLG_SILENT) {
  		if (!(gd->flags & GD_FLG_DEVINIT))
  			pre_console_putc(c);
f6e20fc6c   wdenk   Patch by Anders L...
497
  		return;
13551b911   Patrick Delaunay   console: execute ...
498
  	}
a6cccaea5   wdenk   * Patch by Wolter...
499
  #endif
f5c3ba797   Mark Jackson   Allow console inp...
500
501
502
503
  #ifdef CONFIG_DISABLE_CONSOLE
  	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  		return;
  #endif
e3e454cd7   Graeme Russ   console: Squelch ...
504
  	if (!gd->have_console)
9558b48af   Graeme Russ   console: Implemen...
505
  		return pre_console_putc(c);
e3e454cd7   Graeme Russ   console: Squelch ...
506

47d1a6e1e   wdenk   Initial revision
507
508
  	if (gd->flags & GD_FLG_DEVINIT) {
  		/* Send to the standard output */
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
509
  		fputc(stdout, c);
47d1a6e1e   wdenk   Initial revision
510
511
  	} else {
  		/* Send directly to the handler */
276696675   Siarhei Siamashka   console: Use pre-...
512
  		pre_console_putc(c);
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
513
  		serial_putc(c);
47d1a6e1e   wdenk   Initial revision
514
515
  	}
  }
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
516
  void puts(const char *s)
47d1a6e1e   wdenk   Initial revision
517
  {
36bcea62a   Simon Glass   sandbox: Allow pu...
518
519
520
521
522
523
524
  #ifdef CONFIG_SANDBOX
  	/* sandbox can send characters to stdout before it has a console */
  	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  		os_puts(s);
  		return;
  	}
  #endif
be135cc5e   Soeren Moch   Revert "console: ...
525
526
527
528
529
530
531
532
533
534
  #ifdef CONFIG_DEBUG_UART
  	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
  		while (*s) {
  			int ch = *s++;
  
  			printch(ch);
  		}
  		return;
  	}
  #endif
af880e247   Simon Glass   console: Fix hand...
535
536
  	if (!gd)
  		return;
be135cc5e   Soeren Moch   Revert "console: ...
537
  #ifdef CONFIG_CONSOLE_RECORD
af880e247   Simon Glass   console: Fix hand...
538
  	if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
a3a9e0465   Heinrich Schuchardt   common/console.c:...
539
  		membuff_put((struct membuff *)&gd->console_out, s, strlen(s));
be135cc5e   Soeren Moch   Revert "console: ...
540
541
  #endif
  #ifdef CONFIG_SILENT_CONSOLE
13551b911   Patrick Delaunay   console: execute ...
542
543
544
  	if (gd->flags & GD_FLG_SILENT) {
  		if (!(gd->flags & GD_FLG_DEVINIT))
  			pre_console_puts(s);
be135cc5e   Soeren Moch   Revert "console: ...
545
  		return;
13551b911   Patrick Delaunay   console: execute ...
546
  	}
be135cc5e   Soeren Moch   Revert "console: ...
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
  #endif
  
  #ifdef CONFIG_DISABLE_CONSOLE
  	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  		return;
  #endif
  
  	if (!gd->have_console)
  		return pre_console_puts(s);
  
  	if (gd->flags & GD_FLG_DEVINIT) {
  		/* Send to the standard output */
  		fputs(stdout, s);
  	} else {
  		/* Send directly to the handler */
  		pre_console_puts(s);
  		serial_puts(s);
  	}
47d1a6e1e   wdenk   Initial revision
565
  }
9854a8748   Simon Glass   console: Add a co...
566
567
568
569
  #ifdef CONFIG_CONSOLE_RECORD
  int console_record_init(void)
  {
  	int ret;
a3a9e0465   Heinrich Schuchardt   common/console.c:...
570
571
  	ret = membuff_new((struct membuff *)&gd->console_out,
  			  CONFIG_CONSOLE_RECORD_OUT_SIZE);
9854a8748   Simon Glass   console: Add a co...
572
573
  	if (ret)
  		return ret;
a3a9e0465   Heinrich Schuchardt   common/console.c:...
574
575
  	ret = membuff_new((struct membuff *)&gd->console_in,
  			  CONFIG_CONSOLE_RECORD_IN_SIZE);
9854a8748   Simon Glass   console: Add a co...
576
577
578
579
580
581
  
  	return ret;
  }
  
  void console_record_reset(void)
  {
a3a9e0465   Heinrich Schuchardt   common/console.c:...
582
583
  	membuff_purge((struct membuff *)&gd->console_out);
  	membuff_purge((struct membuff *)&gd->console_in);
9854a8748   Simon Glass   console: Add a co...
584
585
586
587
588
589
590
  }
  
  void console_record_reset_enable(void)
  {
  	console_record_reset();
  	gd->flags |= GD_FLG_RECORD;
  }
b61231281   Simon Glass   console: Add a fu...
591
592
593
  
  int console_record_readline(char *str, int maxlen)
  {
a3a9e0465   Heinrich Schuchardt   common/console.c:...
594
595
  	return membuff_readline((struct membuff *)&gd->console_out, str,
  				maxlen, ' ');
b61231281   Simon Glass   console: Add a fu...
596
597
598
599
  }
  
  int console_record_avail(void)
  {
a3a9e0465   Heinrich Schuchardt   common/console.c:...
600
  	return membuff_avail((struct membuff *)&gd->console_out);
b61231281   Simon Glass   console: Add a fu...
601
  }
9854a8748   Simon Glass   console: Add a co...
602
  #endif
47d1a6e1e   wdenk   Initial revision
603
604
605
  /* test if ctrl-c was pressed */
  static int ctrlc_disabled = 0;	/* see disable_ctrl() */
  static int ctrlc_was_pressed = 0;
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
606
  int ctrlc(void)
47d1a6e1e   wdenk   Initial revision
607
  {
47d1a6e1e   wdenk   Initial revision
608
  	if (!ctrlc_disabled && gd->have_console) {
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
609
610
  		if (tstc()) {
  			switch (getc()) {
47d1a6e1e   wdenk   Initial revision
611
612
613
614
615
616
617
618
  			case 0x03:		/* ^C - Control C */
  				ctrlc_was_pressed = 1;
  				return 1;
  			default:
  				break;
  			}
  		}
  	}
8969ea3e9   Simon Glass   sandbox: Disable ...
619

47d1a6e1e   wdenk   Initial revision
620
621
  	return 0;
  }
a5dffa4b6   Pierre Aubert   Add the function ...
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
  /* Reads user's confirmation.
     Returns 1 if user's input is "y", "Y", "yes" or "YES"
  */
  int confirm_yesno(void)
  {
  	int i;
  	char str_input[5];
  
  	/* Flush input */
  	while (tstc())
  		getc();
  	i = 0;
  	while (i < sizeof(str_input)) {
  		str_input[i] = getc();
  		putc(str_input[i]);
  		if (str_input[i] == '\r')
  			break;
  		i++;
  	}
  	putc('
  ');
  	if (strncmp(str_input, "y\r", 2) == 0 ||
  	    strncmp(str_input, "Y\r", 2) == 0 ||
  	    strncmp(str_input, "yes\r", 4) == 0 ||
  	    strncmp(str_input, "YES\r", 4) == 0)
  		return 1;
  	return 0;
  }
47d1a6e1e   wdenk   Initial revision
650
651
652
  /* pass 1 to disable ctrlc() checking, 0 to enable.
   * returns previous state
   */
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
653
  int disable_ctrlc(int disable)
47d1a6e1e   wdenk   Initial revision
654
655
656
657
658
659
660
661
662
663
664
  {
  	int prev = ctrlc_disabled;	/* save previous state */
  
  	ctrlc_disabled = disable;
  	return prev;
  }
  
  int had_ctrlc (void)
  {
  	return ctrlc_was_pressed;
  }
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
665
  void clear_ctrlc(void)
47d1a6e1e   wdenk   Initial revision
666
667
668
  {
  	ctrlc_was_pressed = 0;
  }
47d1a6e1e   wdenk   Initial revision
669
  /** U-Boot INIT FUNCTIONS *************************************************/
d7be3056d   Mike Frysinger   stdio: constify "...
670
  struct stdio_dev *search_device(int flags, const char *name)
c1de7a6da   Jean-Christophe PLAGNIOL-VILLARD   devices: merge to...
671
  {
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
672
  	struct stdio_dev *dev;
c1de7a6da   Jean-Christophe PLAGNIOL-VILLARD   devices: merge to...
673

52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
674
  	dev = stdio_get_by_name(name);
a2931b30d   Simon Glass   dm: video: Add a ...
675
676
677
678
  #ifdef CONFIG_VIDCONSOLE_AS_LCD
  	if (!dev && !strcmp(name, "lcd"))
  		dev = stdio_get_by_name("vidconsole");
  #endif
c1de7a6da   Jean-Christophe PLAGNIOL-VILLARD   devices: merge to...
679

ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
680
  	if (dev && (dev->flags & flags))
c1de7a6da   Jean-Christophe PLAGNIOL-VILLARD   devices: merge to...
681
682
683
684
  		return dev;
  
  	return NULL;
  }
d7be3056d   Mike Frysinger   stdio: constify "...
685
  int console_assign(int file, const char *devname)
47d1a6e1e   wdenk   Initial revision
686
  {
c1de7a6da   Jean-Christophe PLAGNIOL-VILLARD   devices: merge to...
687
  	int flag;
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
688
  	struct stdio_dev *dev;
47d1a6e1e   wdenk   Initial revision
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
  
  	/* Check for valid file */
  	switch (file) {
  	case stdin:
  		flag = DEV_FLAGS_INPUT;
  		break;
  	case stdout:
  	case stderr:
  		flag = DEV_FLAGS_OUTPUT;
  		break;
  	default:
  		return -1;
  	}
  
  	/* Check for valid device name */
c1de7a6da   Jean-Christophe PLAGNIOL-VILLARD   devices: merge to...
704
  	dev = search_device(flag, devname);
47d1a6e1e   wdenk   Initial revision
705

ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
706
707
  	if (dev)
  		return console_setfile(file, dev);
47d1a6e1e   wdenk   Initial revision
708
709
710
  
  	return -1;
  }
13551b911   Patrick Delaunay   console: execute ...
711
712
  /* return true if the 'silent' flag is removed */
  static bool console_update_silent(void)
47d1a6e1e   wdenk   Initial revision
713
  {
f72da3406   wdenk   Added config opti...
714
  #ifdef CONFIG_SILENT_CONSOLE
13551b911   Patrick Delaunay   console: execute ...
715
  	if (env_get("silent")) {
f72da3406   wdenk   Added config opti...
716
  		gd->flags |= GD_FLG_SILENT;
13551b911   Patrick Delaunay   console: execute ...
717
718
  	} else {
  		unsigned long flags = gd->flags;
43e0a3dec   Chris Packham   common/console.c:...
719
  		gd->flags &= ~GD_FLG_SILENT;
13551b911   Patrick Delaunay   console: execute ...
720
721
722
  
  		return !!(flags & GD_FLG_SILENT);
  	}
f72da3406   wdenk   Added config opti...
723
  #endif
13551b911   Patrick Delaunay   console: execute ...
724
725
  
  	return false;
43e0a3dec   Chris Packham   common/console.c:...
726
  }
b0895384b   Simon Glass   Allow displaying ...
727
728
729
730
731
732
733
734
735
736
737
738
  int console_announce_r(void)
  {
  #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
  	char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
  
  	display_options_get_banner(false, buf, sizeof(buf));
  
  	console_puts_noserial(stdout, buf);
  #endif
  
  	return 0;
  }
43e0a3dec   Chris Packham   common/console.c:...
739
740
741
742
743
744
  /* Called before relocation - use serial functions */
  int console_init_f(void)
  {
  	gd->have_console = 1;
  
  	console_update_silent();
f72da3406   wdenk   Added config opti...
745

276696675   Siarhei Siamashka   console: Use pre-...
746
  	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
9558b48af   Graeme Russ   console: Implemen...
747

ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
748
  	return 0;
47d1a6e1e   wdenk   Initial revision
749
  }
7e3be7cf3   Jean-Christophe PLAGNIOL-VILLARD   console: unify pr...
750
751
  void stdio_print_current_devices(void)
  {
7e3be7cf3   Jean-Christophe PLAGNIOL-VILLARD   console: unify pr...
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
  	/* Print information */
  	puts("In:    ");
  	if (stdio_devices[stdin] == NULL) {
  		puts("No input devices available!
  ");
  	} else {
  		printf ("%s
  ", stdio_devices[stdin]->name);
  	}
  
  	puts("Out:   ");
  	if (stdio_devices[stdout] == NULL) {
  		puts("No output devices available!
  ");
  	} else {
  		printf ("%s
  ", stdio_devices[stdout]->name);
  	}
  
  	puts("Err:   ");
  	if (stdio_devices[stderr] == NULL) {
  		puts("No error devices available!
  ");
  	} else {
  		printf ("%s
  ", stdio_devices[stderr]->name);
  	}
7e3be7cf3   Jean-Christophe PLAGNIOL-VILLARD   console: unify pr...
779
  }
b02654294   Simon Glass   console: Don't en...
780
  #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
47d1a6e1e   wdenk   Initial revision
781
  /* Called after the relocation - use desired console functions */
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
782
  int console_init_r(void)
47d1a6e1e   wdenk   Initial revision
783
784
  {
  	char *stdinname, *stdoutname, *stderrname;
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
785
  	struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
786
  #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
6e5923851   wdenk   * Cleanup, minor ...
787
  	int i;
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
788
  #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
b02654294   Simon Glass   console: Don't en...
789
  #if CONFIG_IS_ENABLED(CONSOLE_MUX)
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
790
791
  	int iomux_err = 0;
  #endif
13551b911   Patrick Delaunay   console: execute ...
792
  	int flushpoint;
47d1a6e1e   wdenk   Initial revision
793

bf46be721   Patrick Delaunay   console: update s...
794
  	/* update silent for env loaded from flash (initr_env) */
13551b911   Patrick Delaunay   console: execute ...
795
796
797
798
  	if (console_update_silent())
  		flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
  	else
  		flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
bf46be721   Patrick Delaunay   console: update s...
799

47d1a6e1e   wdenk   Initial revision
800
  	/* set default handlers at first */
49cad5478   Martin Dorwig   Export redesign
801
802
803
804
805
  	gd->jt->getc  = serial_getc;
  	gd->jt->tstc  = serial_tstc;
  	gd->jt->putc  = serial_putc;
  	gd->jt->puts  = serial_puts;
  	gd->jt->printf = serial_printf;
47d1a6e1e   wdenk   Initial revision
806
807
808
  
  	/* stdin stdout and stderr are in environment */
  	/* scan for it */
00caae6d4   Simon Glass   env: Rename geten...
809
810
811
  	stdinname  = env_get("stdin");
  	stdoutname = env_get("stdout");
  	stderrname = env_get("stderr");
47d1a6e1e   wdenk   Initial revision
812

53677ef18   Wolfgang Denk   Big white-space c...
813
  	if (OVERWRITE_CONSOLE == 0) {	/* if not overwritten by config switch */
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
814
815
816
  		inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
  		outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
  		errdev    = search_device(DEV_FLAGS_OUTPUT, stderrname);
b02654294   Simon Glass   console: Don't en...
817
  #if CONFIG_IS_ENABLED(CONSOLE_MUX)
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
818
819
820
821
822
823
824
  		iomux_err = iomux_doenv(stdin, stdinname);
  		iomux_err += iomux_doenv(stdout, stdoutname);
  		iomux_err += iomux_doenv(stderr, stderrname);
  		if (!iomux_err)
  			/* Successful, so skip all the code below. */
  			goto done;
  #endif
47d1a6e1e   wdenk   Initial revision
825
826
827
  	}
  	/* if the devices are overwritten or not found, use default device */
  	if (inputdev == NULL) {
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
828
  		inputdev  = search_device(DEV_FLAGS_INPUT,  "serial");
47d1a6e1e   wdenk   Initial revision
829
830
  	}
  	if (outputdev == NULL) {
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
831
  		outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
47d1a6e1e   wdenk   Initial revision
832
833
  	}
  	if (errdev == NULL) {
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
834
  		errdev    = search_device(DEV_FLAGS_OUTPUT, "serial");
47d1a6e1e   wdenk   Initial revision
835
836
837
  	}
  	/* Initializes output console first */
  	if (outputdev != NULL) {
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
838
  		/* need to set a console if not done above. */
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
839
  		console_doenv(stdout, outputdev);
47d1a6e1e   wdenk   Initial revision
840
841
  	}
  	if (errdev != NULL) {
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
842
  		/* need to set a console if not done above. */
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
843
  		console_doenv(stderr, errdev);
47d1a6e1e   wdenk   Initial revision
844
845
  	}
  	if (inputdev != NULL) {
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
846
  		/* need to set a console if not done above. */
5f0320108   Jean-Christophe PLAGNIOL-VILLARD   common/console: a...
847
  		console_doenv(stdin, inputdev);
47d1a6e1e   wdenk   Initial revision
848
  	}
b02654294   Simon Glass   console: Don't en...
849
  #if CONFIG_IS_ENABLED(CONSOLE_MUX)
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
850
851
  done:
  #endif
78c112c9f   Simon Glass   console: Enable f...
852
  #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
7e3be7cf3   Jean-Christophe PLAGNIOL-VILLARD   console: unify pr...
853
  	stdio_print_current_devices();
78c112c9f   Simon Glass   console: Enable f...
854
  #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
a2931b30d   Simon Glass   dm: video: Add a ...
855
856
857
858
859
  #ifdef CONFIG_VIDCONSOLE_AS_LCD
  	if (strstr(stdoutname, "lcd"))
  		printf("Warning: Please change 'lcd' to 'vidconsole' in stdout/stderr environment vars
  ");
  #endif
47d1a6e1e   wdenk   Initial revision
860

6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
861
  #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
47d1a6e1e   wdenk   Initial revision
862
  	/* set the environment variables (will overwrite previous env settings) */
27b4225b3   Tom Rini   stdio_names: Ensu...
863
  	for (i = 0; i < MAX_FILES; i++) {
382bee57f   Simon Glass   env: Rename seten...
864
  		env_set(stdio_names[i], stdio_devices[i]->name);
47d1a6e1e   wdenk   Initial revision
865
  	}
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
866
  #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
47d1a6e1e   wdenk   Initial revision
867

c4e0057fa   Joe Hershberger   env: Refactor do_...
868
  	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
47d1a6e1e   wdenk   Initial revision
869
870
871
  #if 0
  	/* If nothing usable installed, use only the initial console */
  	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
872
  		return 0;
47d1a6e1e   wdenk   Initial revision
873
  #endif
13551b911   Patrick Delaunay   console: execute ...
874
  	print_pre_console_buffer(flushpoint);
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
875
  	return 0;
47d1a6e1e   wdenk   Initial revision
876
  }
b02654294   Simon Glass   console: Don't en...
877
  #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
47d1a6e1e   wdenk   Initial revision
878
879
  
  /* Called after the relocation - use desired console functions */
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
880
  int console_init_r(void)
47d1a6e1e   wdenk   Initial revision
881
  {
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
882
  	struct stdio_dev *inputdev = NULL, *outputdev = NULL;
c1de7a6da   Jean-Christophe PLAGNIOL-VILLARD   devices: merge to...
883
  	int i;
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
884
  	struct list_head *list = stdio_get_list();
c1de7a6da   Jean-Christophe PLAGNIOL-VILLARD   devices: merge to...
885
  	struct list_head *pos;
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
886
  	struct stdio_dev *dev;
13551b911   Patrick Delaunay   console: execute ...
887
  	int flushpoint;
47d1a6e1e   wdenk   Initial revision
888

bf46be721   Patrick Delaunay   console: update s...
889
  	/* update silent for env loaded from flash (initr_env) */
13551b911   Patrick Delaunay   console: execute ...
890
891
892
893
  	if (console_update_silent())
  		flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
  	else
  		flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
43e0a3dec   Chris Packham   common/console.c:...
894

d791b1dc3   wdenk   * Make sure Block...
895
  #ifdef CONFIG_SPLASH_SCREEN
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
896
897
  	/*
  	 * suppress all output if splash screen is enabled and we have
a74908161   Anatolij Gustschin   console.c: fix pr...
898
899
900
  	 * a bmp to display. We redirect the output from frame buffer
  	 * console to serial console in this case or suppress it if
  	 * "silent" mode was requested.
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
901
  	 */
00caae6d4   Simon Glass   env: Rename geten...
902
  	if (env_get("splashimage") != NULL) {
a74908161   Anatolij Gustschin   console.c: fix pr...
903
904
905
  		if (!(gd->flags & GD_FLG_SILENT))
  			outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  	}
f72da3406   wdenk   Added config opti...
906
  #endif
47d1a6e1e   wdenk   Initial revision
907
  	/* Scan devices looking for input and output devices */
c1de7a6da   Jean-Christophe PLAGNIOL-VILLARD   devices: merge to...
908
  	list_for_each(pos, list) {
52cb4d4fb   Jean-Christophe PLAGNIOL-VILLARD   stdio/device: rew...
909
  		dev = list_entry(pos, struct stdio_dev, list);
47d1a6e1e   wdenk   Initial revision
910
911
912
913
914
915
916
  
  		if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  			inputdev = dev;
  		}
  		if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  			outputdev = dev;
  		}
c1de7a6da   Jean-Christophe PLAGNIOL-VILLARD   devices: merge to...
917
918
  		if(inputdev && outputdev)
  			break;
47d1a6e1e   wdenk   Initial revision
919
920
921
922
  	}
  
  	/* Initializes output console first */
  	if (outputdev != NULL) {
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
923
924
  		console_setfile(stdout, outputdev);
  		console_setfile(stderr, outputdev);
b02654294   Simon Glass   console: Don't en...
925
  #if CONFIG_IS_ENABLED(CONSOLE_MUX)
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
926
927
928
  		console_devices[stdout][0] = outputdev;
  		console_devices[stderr][0] = outputdev;
  #endif
47d1a6e1e   wdenk   Initial revision
929
930
931
932
  	}
  
  	/* Initializes input console */
  	if (inputdev != NULL) {
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
933
  		console_setfile(stdin, inputdev);
b02654294   Simon Glass   console: Don't en...
934
  #if CONFIG_IS_ENABLED(CONSOLE_MUX)
16a28ef21   Gary Jennejohn   IOMUX: Add consol...
935
936
  		console_devices[stdin][0] = inputdev;
  #endif
47d1a6e1e   wdenk   Initial revision
937
  	}
78c112c9f   Simon Glass   console: Enable f...
938
  #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
7e3be7cf3   Jean-Christophe PLAGNIOL-VILLARD   console: unify pr...
939
  	stdio_print_current_devices();
78c112c9f   Simon Glass   console: Enable f...
940
  #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
47d1a6e1e   wdenk   Initial revision
941
942
  
  	/* Setting environment variables */
27b4225b3   Tom Rini   stdio_names: Ensu...
943
  	for (i = 0; i < MAX_FILES; i++) {
382bee57f   Simon Glass   env: Rename seten...
944
  		env_set(stdio_names[i], stdio_devices[i]->name);
47d1a6e1e   wdenk   Initial revision
945
  	}
c4e0057fa   Joe Hershberger   env: Refactor do_...
946
  	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */
47d1a6e1e   wdenk   Initial revision
947
948
949
  #if 0
  	/* If nothing usable installed, use only the initial console */
  	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
950
  		return 0;
47d1a6e1e   wdenk   Initial revision
951
  #endif
13551b911   Patrick Delaunay   console: execute ...
952
  	print_pre_console_buffer(flushpoint);
ec6f14994   Jean-Christophe PLAGNIOL-VILLARD   common/console: c...
953
  	return 0;
47d1a6e1e   wdenk   Initial revision
954
  }
b02654294   Simon Glass   console: Don't en...
955
  #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */