console.c 18.8 KB
Newer Older
wdenk's avatar
wdenk committed
1 2 3 4
/*
 * (C) Copyright 2000
 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
 *
5
 * SPDX-License-Identifier:	GPL-2.0+
wdenk's avatar
wdenk committed
6 7 8 9
 */

#include <common.h>
#include <stdarg.h>
10
#include <iomux.h>
wdenk's avatar
wdenk committed
11
#include <malloc.h>
12
#include <os.h>
13
#include <serial.h>
14
#include <stdio_dev.h>
15
#include <exports.h>
16
#include <environment.h>
wdenk's avatar
wdenk committed
17

18 19
DECLARE_GLOBAL_DATA_PTR;

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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:

#ifdef CONFIG_CONSOLE_MUX
		if (iomux_doenv(console, value))
			return 1;
#else
		/* Try assigning specified device */
		if (console_assign(console, value) < 0)
			return 1;
#endif /* CONFIG_CONSOLE_MUX */
		return 0;

	case env_op_delete:
		if ((flags & H_FORCE) == 0)
			printf("Can't delete \"%s\"\n", name);
		return 1;

	default:
		return 0;
	}
}
U_BOOT_ENV_CALLBACK(console, on_console);

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#ifdef CONFIG_SILENT_CONSOLE
static int on_silent(const char *name, const char *value, enum env_op op,
	int flags)
{
#ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
	if (flags & H_INTERACTIVE)
		return 0;
#endif
#ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
	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

85
#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk's avatar
wdenk committed
86 87 88 89 90
/*
 * if overwrite_console returns 1, the stdin, stderr and stdout
 * are switched to the serial port, else the settings in the
 * environment are used
 */
91
#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
92 93
extern int overwrite_console(void);
#define OVERWRITE_CONSOLE overwrite_console()
wdenk's avatar
wdenk committed
94
#else
95
#define OVERWRITE_CONSOLE 0
96
#endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
wdenk's avatar
wdenk committed
97

98
#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk's avatar
wdenk committed
99

100
static int console_setfile(int file, struct stdio_dev * dev)
wdenk's avatar
wdenk committed
101 102 103 104 105 106 107 108 109 110 111 112
{
	int error = 0;

	if (dev == NULL)
		return -1;

	switch (file) {
	case stdin:
	case stdout:
	case stderr:
		/* Start new device */
		if (dev->start) {
113
			error = dev->start(dev);
wdenk's avatar
wdenk committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127
			/* 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:
Martin Dorwig's avatar
Martin Dorwig committed
128 129
			gd->jt->getc = getc;
			gd->jt->tstc = tstc;
wdenk's avatar
wdenk committed
130 131
			break;
		case stdout:
Martin Dorwig's avatar
Martin Dorwig committed
132 133 134
			gd->jt->putc  = putc;
			gd->jt->puts  = puts;
			gd->jt->printf = printf;
wdenk's avatar
wdenk committed
135 136 137 138 139 140 141 142 143 144
			break;
		}
		break;

	default:		/* Invalid file ID */
		error = -1;
	}
	return error;
}

145 146 147
#if defined(CONFIG_CONSOLE_MUX)
/** Console I/O multiplexing *******************************************/

148 149
static struct stdio_dev *tstcdev;
struct stdio_dev **console_devices[MAX_FILES];
150 151 152 153 154 155 156 157
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.
 */
158
static int console_getc(int file)
159 160 161 162
{
	unsigned char ret;

	/* This is never called with testcdev == NULL */
163
	ret = tstcdev->getc(tstcdev);
164 165 166 167
	tstcdev = NULL;
	return ret;
}

168
static int console_tstc(int file)
169 170
{
	int i, ret;
171
	struct stdio_dev *dev;
172 173 174 175 176

	disable_ctrlc(1);
	for (i = 0; i < cd_count[file]; i++) {
		dev = console_devices[file][i];
		if (dev->tstc != NULL) {
177
			ret = dev->tstc(dev);
178 179 180 181 182 183 184 185 186 187 188 189
			if (ret > 0) {
				tstcdev = dev;
				disable_ctrlc(0);
				return ret;
			}
		}
	}
	disable_ctrlc(0);

	return 0;
}

190
static void console_putc(int file, const char c)
191 192
{
	int i;
193
	struct stdio_dev *dev;
194 195 196 197

	for (i = 0; i < cd_count[file]; i++) {
		dev = console_devices[file][i];
		if (dev->putc != NULL)
198
			dev->putc(dev, c);
199 200 201
	}
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215
#ifdef CONFIG_PRE_CONSOLE_BUFFER
static void console_putc_noserial(int file, const char c)
{
	int i;
	struct stdio_dev *dev;

	for (i = 0; i < cd_count[file]; i++) {
		dev = console_devices[file][i];
		if (dev->putc != NULL && strcmp(dev->name, "serial") != 0)
			dev->putc(dev, c);
	}
}
#endif

216
static void console_puts(int file, const char *s)
217 218
{
	int i;
219
	struct stdio_dev *dev;
220 221 222 223

	for (i = 0; i < cd_count[file]; i++) {
		dev = console_devices[file][i];
		if (dev->puts != NULL)
224
			dev->puts(dev, s);
225 226
	}
}
227 228 229 230 231 232

static inline void console_printdevs(int file)
{
	iomux_printdevs(file);
}

233
static inline void console_doenv(int file, struct stdio_dev *dev)
234 235 236 237 238 239
{
	iomux_doenv(file, dev->name);
}
#else
static inline int console_getc(int file)
{
240
	return stdio_devices[file]->getc(stdio_devices[file]);
241 242 243 244
}

static inline int console_tstc(int file)
{
245
	return stdio_devices[file]->tstc(stdio_devices[file]);
246 247 248 249
}

static inline void console_putc(int file, const char c)
{
250
	stdio_devices[file]->putc(stdio_devices[file], c);
251 252
}

253 254 255 256 257 258 259 260
#ifdef CONFIG_PRE_CONSOLE_BUFFER
static inline void console_putc_noserial(int file, const char c)
{
	if (strcmp(stdio_devices[file]->name, "serial") != 0)
		stdio_devices[file]->putc(stdio_devices[file], c);
}
#endif

261 262
static inline void console_puts(int file, const char *s)
{
263
	stdio_devices[file]->puts(stdio_devices[file], s);
264 265 266 267 268 269 270
}

static inline void console_printdevs(int file)
{
	printf("%s\n", stdio_devices[file]->name);
}

271
static inline void console_doenv(int file, struct stdio_dev *dev)
272 273 274
{
	console_setfile(file, dev);
}
275 276
#endif /* defined(CONFIG_CONSOLE_MUX) */

wdenk's avatar
wdenk committed
277 278
/** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/

279
int serial_printf(const char *fmt, ...)
wdenk's avatar
wdenk committed
280 281 282
{
	va_list args;
	uint i;
283
	char printbuffer[CONFIG_SYS_PBSIZE];
wdenk's avatar
wdenk committed
284

285
	va_start(args, fmt);
wdenk's avatar
wdenk committed
286 287 288 289

	/* For this to work, printbuffer must be larger than
	 * anything we ever want to print.
	 */
290
	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
291
	va_end(args);
wdenk's avatar
wdenk committed
292

293
	serial_puts(printbuffer);
294
	return i;
wdenk's avatar
wdenk committed
295 296
}

297
int fgetc(int file)
wdenk's avatar
wdenk committed
298
{
299 300 301 302 303 304 305 306 307 308 309
	if (file < MAX_FILES) {
#if defined(CONFIG_CONSOLE_MUX)
		/*
		 * Effectively poll for input wherever it may be available.
		 */
		for (;;) {
			/*
			 * Upper layer may have already called tstc() so
			 * check for that first.
			 */
			if (tstcdev != NULL)
310 311
				return console_getc(file);
			console_tstc(file);
312 313 314 315 316 317 318 319 320
#ifdef CONFIG_WATCHDOG
			/*
			 * If the watchdog must be rate-limited then it should
			 * already be handled in board-specific code.
			 */
			 udelay(1);
#endif
		}
#else
321
		return console_getc(file);
322 323
#endif
	}
wdenk's avatar
wdenk committed
324 325 326 327

	return -1;
}

328
int ftstc(int file)
wdenk's avatar
wdenk committed
329 330
{
	if (file < MAX_FILES)
331
		return console_tstc(file);
wdenk's avatar
wdenk committed
332 333 334 335

	return -1;
}

336
void fputc(int file, const char c)
wdenk's avatar
wdenk committed
337 338
{
	if (file < MAX_FILES)
339
		console_putc(file, c);
wdenk's avatar
wdenk committed
340 341
}

342
void fputs(int file, const char *s)
wdenk's avatar
wdenk committed
343 344
{
	if (file < MAX_FILES)
345
		console_puts(file, s);
wdenk's avatar
wdenk committed
346 347
}

348
int fprintf(int file, const char *fmt, ...)
wdenk's avatar
wdenk committed
349 350 351
{
	va_list args;
	uint i;
352
	char printbuffer[CONFIG_SYS_PBSIZE];
wdenk's avatar
wdenk committed
353

354
	va_start(args, fmt);
wdenk's avatar
wdenk committed
355 356 357 358

	/* For this to work, printbuffer must be larger than
	 * anything we ever want to print.
	 */
359
	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
360
	va_end(args);
wdenk's avatar
wdenk committed
361 362

	/* Send to desired file */
363
	fputs(file, printbuffer);
364
	return i;
wdenk's avatar
wdenk committed
365 366 367 368
}

/** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/

369
int getc(void)
wdenk's avatar
wdenk committed
370
{
371 372 373 374 375
#ifdef CONFIG_DISABLE_CONSOLE
	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
		return 0;
#endif

376 377 378
	if (!gd->have_console)
		return 0;

wdenk's avatar
wdenk committed
379 380
	if (gd->flags & GD_FLG_DEVINIT) {
		/* Get from the standard input */
381
		return fgetc(stdin);
wdenk's avatar
wdenk committed
382 383 384
	}

	/* Send directly to the handler */
385
	return serial_getc();
wdenk's avatar
wdenk committed
386 387
}

388
int tstc(void)
wdenk's avatar
wdenk committed
389
{
390 391 392 393 394
#ifdef CONFIG_DISABLE_CONSOLE
	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
		return 0;
#endif

395 396 397
	if (!gd->have_console)
		return 0;

wdenk's avatar
wdenk committed
398 399
	if (gd->flags & GD_FLG_DEVINIT) {
		/* Test the standard input */
400
		return ftstc(stdin);
wdenk's avatar
wdenk committed
401 402 403
	}

	/* Send directly to the handler */
404
	return serial_tstc();
wdenk's avatar
wdenk committed
405 406
}

407 408 409
#define PRE_CONSOLE_FLUSHPOINT1_SERIAL			0
#define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL	1

410
#ifdef CONFIG_PRE_CONSOLE_BUFFER
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)

static void pre_console_putc(const char c)
{
	char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;

	buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
}

static void pre_console_puts(const char *s)
{
	while (*s)
		pre_console_putc(*s++);
}

426
static void print_pre_console_buffer(int flushpoint)
427 428 429 430 431 432 433 434
{
	unsigned long i = 0;
	char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;

	if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
		i = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;

	while (i < gd->precon_buf_idx)
435 436 437 438 439 440 441 442 443
		switch (flushpoint) {
		case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
			putc(buffer[CIRC_BUF_IDX(i++)]);
			break;
		case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
			console_putc_noserial(stdout,
					      buffer[CIRC_BUF_IDX(i++)]);
			break;
		}
444 445 446 447
}
#else
static inline void pre_console_putc(const char c) {}
static inline void pre_console_puts(const char *s) {}
448
static inline void print_pre_console_buffer(int flushpoint) {}
449 450
#endif

451
void putc(const char c)
wdenk's avatar
wdenk committed
452
{
453
#ifdef CONFIG_SANDBOX
454
	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
455 456 457 458
		os_putc(c);
		return;
	}
#endif
459 460
#ifdef CONFIG_SILENT_CONSOLE
	if (gd->flags & GD_FLG_SILENT)
461
		return;
462 463
#endif

464 465 466 467 468
#ifdef CONFIG_DISABLE_CONSOLE
	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
		return;
#endif

469
	if (!gd->have_console)
470
		return pre_console_putc(c);
471

wdenk's avatar
wdenk committed
472 473
	if (gd->flags & GD_FLG_DEVINIT) {
		/* Send to the standard output */
474
		fputc(stdout, c);
wdenk's avatar
wdenk committed
475 476
	} else {
		/* Send directly to the handler */
477
		pre_console_putc(c);
478
		serial_putc(c);
wdenk's avatar
wdenk committed
479 480 481
	}
}

482
void puts(const char *s)
wdenk's avatar
wdenk committed
483
{
484
#ifdef CONFIG_SANDBOX
485
	if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
486 487 488 489 490
		os_puts(s);
		return;
	}
#endif

491 492 493 494 495
#ifdef CONFIG_SILENT_CONSOLE
	if (gd->flags & GD_FLG_SILENT)
		return;
#endif

496 497 498 499 500
#ifdef CONFIG_DISABLE_CONSOLE
	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
		return;
#endif

501
	if (!gd->have_console)
502
		return pre_console_puts(s);
503

wdenk's avatar
wdenk committed
504 505
	if (gd->flags & GD_FLG_DEVINIT) {
		/* Send to the standard output */
506
		fputs(stdout, s);
wdenk's avatar
wdenk committed
507 508
	} else {
		/* Send directly to the handler */
509
		pre_console_puts(s);
510
		serial_puts(s);
wdenk's avatar
wdenk committed
511 512 513
	}
}

514
int printf(const char *fmt, ...)
wdenk's avatar
wdenk committed
515 516 517
{
	va_list args;
	uint i;
518
	char printbuffer[CONFIG_SYS_PBSIZE];
wdenk's avatar
wdenk committed
519

520
#if !defined(CONFIG_SANDBOX) && !defined(CONFIG_PRE_CONSOLE_BUFFER)
521 522
	if (!gd->have_console)
		return 0;
523
#endif
524

525
	va_start(args, fmt);
wdenk's avatar
wdenk committed
526 527 528 529

	/* For this to work, printbuffer must be larger than
	 * anything we ever want to print.
	 */
530
	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
531
	va_end(args);
wdenk's avatar
wdenk committed
532 533

	/* Print the string */
534
	puts(printbuffer);
535
	return i;
wdenk's avatar
wdenk committed
536 537
}

538
int vprintf(const char *fmt, va_list args)
539 540
{
	uint i;
541
	char printbuffer[CONFIG_SYS_PBSIZE];
542

543
#if defined(CONFIG_PRE_CONSOLE_BUFFER) && !defined(CONFIG_SANDBOX)
544 545
	if (!gd->have_console)
		return 0;
546
#endif
547

548 549 550
	/* For this to work, printbuffer must be larger than
	 * anything we ever want to print.
	 */
551
	i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
552 553

	/* Print the string */
554
	puts(printbuffer);
555
	return i;
556 557
}

wdenk's avatar
wdenk committed
558 559 560
/* test if ctrl-c was pressed */
static int ctrlc_disabled = 0;	/* see disable_ctrl() */
static int ctrlc_was_pressed = 0;
561
int ctrlc(void)
wdenk's avatar
wdenk committed
562
{
Simon Glass's avatar
Simon Glass committed
563
#ifndef CONFIG_SANDBOX
wdenk's avatar
wdenk committed
564
	if (!ctrlc_disabled && gd->have_console) {
565 566
		if (tstc()) {
			switch (getc()) {
wdenk's avatar
wdenk committed
567 568 569 570 571 572 573 574
			case 0x03:		/* ^C - Control C */
				ctrlc_was_pressed = 1;
				return 1;
			default:
				break;
			}
		}
	}
Simon Glass's avatar
Simon Glass committed
575 576
#endif

wdenk's avatar
wdenk committed
577 578
	return 0;
}
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
/* 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('\n');
	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;
}
wdenk's avatar
wdenk committed
606 607 608
/* pass 1 to disable ctrlc() checking, 0 to enable.
 * returns previous state
 */
609
int disable_ctrlc(int disable)
wdenk's avatar
wdenk committed
610 611 612 613 614 615 616 617 618 619 620 621
{
	int prev = ctrlc_disabled;	/* save previous state */

	ctrlc_disabled = disable;
	return prev;
}

int had_ctrlc (void)
{
	return ctrlc_was_pressed;
}

622
void clear_ctrlc(void)
wdenk's avatar
wdenk committed
623 624 625 626 627 628 629 630 631 632 633 634
{
	ctrlc_was_pressed = 0;
}

#ifdef CONFIG_MODEM_SUPPORT_DEBUG
char	screen[1024];
char *cursor = screen;
int once = 0;
inline void dbg(const char *fmt, ...)
{
	va_list	args;
	uint	i;
635
	char	printbuffer[CONFIG_SYS_PBSIZE];
wdenk's avatar
wdenk committed
636 637 638 639 640 641 642 643 644 645 646

	if (!once) {
		memset(screen, 0, sizeof(screen));
		once++;
	}

	va_start(args, fmt);

	/* For this to work, printbuffer must be larger than
	 * anything we ever want to print.
	 */
647
	i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
wdenk's avatar
wdenk committed
648 649
	va_end(args);

650 651
	if ((screen + sizeof(screen) - 1 - cursor)
	    < strlen(printbuffer) + 1) {
wdenk's avatar
wdenk committed
652 653 654 655 656 657 658 659
		memset(screen, 0, sizeof(screen));
		cursor = screen;
	}
	sprintf(cursor, printbuffer);
	cursor += strlen(printbuffer);

}
#else
660
static inline void dbg(const char *fmt, ...)
wdenk's avatar
wdenk committed
661 662 663 664 665 666
{
}
#endif

/** U-Boot INIT FUNCTIONS *************************************************/

667
struct stdio_dev *search_device(int flags, const char *name)
668
{
669
	struct stdio_dev *dev;
670

671
	dev = stdio_get_by_name(name);
672

673
	if (dev && (dev->flags & flags))
674 675 676 677 678
		return dev;

	return NULL;
}

679
int console_assign(int file, const char *devname)
wdenk's avatar
wdenk committed
680
{
681
	int flag;
682
	struct stdio_dev *dev;
wdenk's avatar
wdenk committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

	/* 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 */

699
	dev = search_device(flag, devname);
wdenk's avatar
wdenk committed
700

701 702
	if (dev)
		return console_setfile(file, dev);
wdenk's avatar
wdenk committed
703 704 705 706 707

	return -1;
}

/* Called before relocation - use serial functions */
708
int console_init_f(void)
wdenk's avatar
wdenk committed
709 710
{
	gd->have_console = 1;
711 712 713 714 715 716

#ifdef CONFIG_SILENT_CONSOLE
	if (getenv("silent") != NULL)
		gd->flags |= GD_FLG_SILENT;
#endif

717
	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
718

719
	return 0;
wdenk's avatar
wdenk committed
720 721
}

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
void stdio_print_current_devices(void)
{
	/* Print information */
	puts("In:    ");
	if (stdio_devices[stdin] == NULL) {
		puts("No input devices available!\n");
	} else {
		printf ("%s\n", stdio_devices[stdin]->name);
	}

	puts("Out:   ");
	if (stdio_devices[stdout] == NULL) {
		puts("No output devices available!\n");
	} else {
		printf ("%s\n", stdio_devices[stdout]->name);
	}

	puts("Err:   ");
	if (stdio_devices[stderr] == NULL) {
		puts("No error devices available!\n");
	} else {
		printf ("%s\n", stdio_devices[stderr]->name);
	}
}

747
#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
wdenk's avatar
wdenk committed
748
/* Called after the relocation - use desired console functions */
749
int console_init_r(void)
wdenk's avatar
wdenk committed
750 751
{
	char *stdinname, *stdoutname, *stderrname;
752
	struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
753
#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk's avatar
wdenk committed
754
	int i;
755
#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
756 757 758
#ifdef CONFIG_CONSOLE_MUX
	int iomux_err = 0;
#endif
wdenk's avatar
wdenk committed
759 760

	/* set default handlers at first */
Martin Dorwig's avatar
Martin Dorwig committed
761 762 763 764 765
	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;
wdenk's avatar
wdenk committed
766 767 768

	/* stdin stdout and stderr are in environment */
	/* scan for it */
769 770 771
	stdinname  = getenv("stdin");
	stdoutname = getenv("stdout");
	stderrname = getenv("stderr");
wdenk's avatar
wdenk committed
772

Wolfgang Denk's avatar
Wolfgang Denk committed
773
	if (OVERWRITE_CONSOLE == 0) {	/* if not overwritten by config switch */
774 775 776
		inputdev  = search_device(DEV_FLAGS_INPUT,  stdinname);
		outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
		errdev    = search_device(DEV_FLAGS_OUTPUT, stderrname);
777 778 779 780 781 782 783 784
#ifdef CONFIG_CONSOLE_MUX
		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
wdenk's avatar
wdenk committed
785 786 787
	}
	/* if the devices are overwritten or not found, use default device */
	if (inputdev == NULL) {
788
		inputdev  = search_device(DEV_FLAGS_INPUT,  "serial");
wdenk's avatar
wdenk committed
789 790
	}
	if (outputdev == NULL) {
791
		outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk's avatar
wdenk committed
792 793
	}
	if (errdev == NULL) {
794
		errdev    = search_device(DEV_FLAGS_OUTPUT, "serial");
wdenk's avatar
wdenk committed
795 796 797
	}
	/* Initializes output console first */
	if (outputdev != NULL) {
798
		/* need to set a console if not done above. */
799
		console_doenv(stdout, outputdev);
wdenk's avatar
wdenk committed
800 801
	}
	if (errdev != NULL) {
802
		/* need to set a console if not done above. */
803
		console_doenv(stderr, errdev);
wdenk's avatar
wdenk committed
804 805
	}
	if (inputdev != NULL) {
806
		/* need to set a console if not done above. */
807
		console_doenv(stdin, inputdev);
wdenk's avatar
wdenk committed
808 809
	}

810 811 812 813
#ifdef CONFIG_CONSOLE_MUX
done:
#endif

814
#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
815
	stdio_print_current_devices();
816
#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk's avatar
wdenk committed
817

818
#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
wdenk's avatar
wdenk committed
819 820
	/* set the environment variables (will overwrite previous env settings) */
	for (i = 0; i < 3; i++) {
821
		setenv(stdio_names[i], stdio_devices[i]->name);
wdenk's avatar
wdenk committed
822
	}
823
#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
wdenk's avatar
wdenk committed
824

825 826
	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */

wdenk's avatar
wdenk committed
827 828 829
#if 0
	/* If nothing usable installed, use only the initial console */
	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
830
		return 0;
wdenk's avatar
wdenk committed
831
#endif
832
	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
833
	return 0;
wdenk's avatar
wdenk committed
834 835
}

836
#else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
wdenk's avatar
wdenk committed
837 838

/* Called after the relocation - use desired console functions */
839
int console_init_r(void)
wdenk's avatar
wdenk committed
840
{
841
	struct stdio_dev *inputdev = NULL, *outputdev = NULL;
842
	int i;
843
	struct list_head *list = stdio_get_list();
844
	struct list_head *pos;
845
	struct stdio_dev *dev;
wdenk's avatar
wdenk committed
846

847
#ifdef CONFIG_SPLASH_SCREEN
848 849
	/*
	 * suppress all output if splash screen is enabled and we have
850 851 852
	 * 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.
853
	 */
854 855 856 857
	if (getenv("splashimage") != NULL) {
		if (!(gd->flags & GD_FLG_SILENT))
			outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
	}
858 859
#endif

wdenk's avatar
wdenk committed
860
	/* Scan devices looking for input and output devices */
861
	list_for_each(pos, list) {
862
		dev = list_entry(pos, struct stdio_dev, list);
wdenk's avatar
wdenk committed
863 864 865 866 867 868 869

		if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
			inputdev = dev;
		}
		if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
			outputdev = dev;
		}
870 871
		if(inputdev && outputdev)
			break;
wdenk's avatar
wdenk committed
872 873 874 875
	}

	/* Initializes output console first */
	if (outputdev != NULL) {
876 877
		console_setfile(stdout, outputdev);
		console_setfile(stderr, outputdev);
878 879 880 881
#ifdef CONFIG_CONSOLE_MUX
		console_devices[stdout][0] = outputdev;
		console_devices[stderr][0] = outputdev;
#endif
wdenk's avatar
wdenk committed
882 883 884 885
	}

	/* Initializes input console */
	if (inputdev != NULL) {
886
		console_setfile(stdin, inputdev);
887 888 889
#ifdef CONFIG_CONSOLE_MUX
		console_devices[stdin][0] = inputdev;
#endif
wdenk's avatar
wdenk committed
890 891
	}

892
#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
893
	stdio_print_current_devices();
894
#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
wdenk's avatar
wdenk committed
895 896 897

	/* Setting environment variables */
	for (i = 0; i < 3; i++) {
898
		setenv(stdio_names[i], stdio_devices[i]->name);
wdenk's avatar
wdenk committed
899 900
	}

901 902
	gd->flags |= GD_FLG_DEVINIT;	/* device initialization completed */

wdenk's avatar
wdenk committed
903 904 905
#if 0
	/* If nothing usable installed, use only the initial console */
	if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
906
		return 0;
wdenk's avatar
wdenk committed
907
#endif
908
	print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL);
909
	return 0;
wdenk's avatar
wdenk committed
910 911
}

912
#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */