blob: 8768b1c3ce240998a2c6a60ca704e4d6b6c7187a [file] [log] [blame]
Googlere00b8eb2019-07-08 16:37:07 -07001/*
2 * (C) Copyright 2000
3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
Googlera8fd56b2024-10-24 14:04:51 +08004 *
5 * SPDX-License-Identifier: GPL-2.0+
Googlere00b8eb2019-07-08 16:37:07 -07006 */
7
8#include <common.h>
Googlere00b8eb2019-07-08 16:37:07 -07009#include <stdarg.h>
10#include <iomux.h>
11#include <malloc.h>
Googlere00b8eb2019-07-08 16:37:07 -070012#include <os.h>
13#include <serial.h>
14#include <stdio_dev.h>
15#include <exports.h>
16#include <environment.h>
Googlere00b8eb2019-07-08 16:37:07 -070017
18DECLARE_GLOBAL_DATA_PTR;
19
20static int on_console(const char *name, const char *value, enum env_op op,
21 int flags)
22{
23 int console = -1;
24
25 /* Check for console redirection */
26 if (strcmp(name, "stdin") == 0)
27 console = stdin;
28 else if (strcmp(name, "stdout") == 0)
29 console = stdout;
30 else if (strcmp(name, "stderr") == 0)
31 console = stderr;
32
33 /* if not actually setting a console variable, we don't care */
34 if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
35 return 0;
36
37 switch (op) {
38 case env_op_create:
39 case env_op_overwrite:
40
Googlera8fd56b2024-10-24 14:04:51 +080041#ifdef CONFIG_CONSOLE_MUX
Googlere00b8eb2019-07-08 16:37:07 -070042 if (iomux_doenv(console, value))
43 return 1;
44#else
45 /* Try assigning specified device */
46 if (console_assign(console, value) < 0)
47 return 1;
Googlera8fd56b2024-10-24 14:04:51 +080048#endif /* CONFIG_CONSOLE_MUX */
Googlere00b8eb2019-07-08 16:37:07 -070049 return 0;
50
51 case env_op_delete:
52 if ((flags & H_FORCE) == 0)
53 printf("Can't delete \"%s\"\n", name);
54 return 1;
55
56 default:
57 return 0;
58 }
59}
60U_BOOT_ENV_CALLBACK(console, on_console);
61
62#ifdef CONFIG_SILENT_CONSOLE
63static int on_silent(const char *name, const char *value, enum env_op op,
64 int flags)
65{
Googlera8fd56b2024-10-24 14:04:51 +080066#ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_SET
Googlere00b8eb2019-07-08 16:37:07 -070067 if (flags & H_INTERACTIVE)
68 return 0;
69#endif
Googlera8fd56b2024-10-24 14:04:51 +080070#ifndef CONFIG_SILENT_CONSOLE_UPDATE_ON_RELOC
Googlere00b8eb2019-07-08 16:37:07 -070071 if ((flags & H_INTERACTIVE) == 0)
72 return 0;
73#endif
74
75 if (value != NULL)
Googlera8fd56b2024-10-24 14:04:51 +080076 if (!strcmp(value, "on"))
77 gd->flags |= GD_FLG_SILENT;
78 else
79 gd->flags &= ~GD_FLG_SILENT;
Googlere00b8eb2019-07-08 16:37:07 -070080 else
81 gd->flags &= ~GD_FLG_SILENT;
82
83 return 0;
84}
85U_BOOT_ENV_CALLBACK(silent, on_silent);
86#endif
87
Googlera8fd56b2024-10-24 14:04:51 +080088#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
Googlere00b8eb2019-07-08 16:37:07 -070089/*
90 * if overwrite_console returns 1, the stdin, stderr and stdout
91 * are switched to the serial port, else the settings in the
92 * environment are used
93 */
94#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
95extern int overwrite_console(void);
96#define OVERWRITE_CONSOLE overwrite_console()
97#else
98#define OVERWRITE_CONSOLE 0
99#endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
100
Googlera8fd56b2024-10-24 14:04:51 +0800101#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
Googlere00b8eb2019-07-08 16:37:07 -0700102
103static int console_setfile(int file, struct stdio_dev * dev)
104{
105 int error = 0;
106
107 if (dev == NULL)
108 return -1;
109
110 switch (file) {
111 case stdin:
112 case stdout:
113 case stderr:
114 /* Start new device */
115 if (dev->start) {
116 error = dev->start(dev);
117 /* If it's not started dont use it */
118 if (error < 0)
119 break;
120 }
121
122 /* Assign the new device (leaving the existing one started) */
123 stdio_devices[file] = dev;
124
125 /*
126 * Update monitor functions
127 * (to use the console stuff by other applications)
128 */
129 switch (file) {
130 case stdin:
Googlera8fd56b2024-10-24 14:04:51 +0800131 gd->jt[XF_getc] = getc;
132 gd->jt[XF_tstc] = tstc;
Googlere00b8eb2019-07-08 16:37:07 -0700133 break;
134 case stdout:
Googlera8fd56b2024-10-24 14:04:51 +0800135 gd->jt[XF_putc] = putc;
136 gd->jt[XF_puts] = puts;
137 gd->jt[XF_printf] = printf;
Googlere00b8eb2019-07-08 16:37:07 -0700138 break;
139 }
140 break;
141
142 default: /* Invalid file ID */
143 error = -1;
144 }
145 return error;
146}
147
Googlera8fd56b2024-10-24 14:04:51 +0800148#if defined(CONFIG_CONSOLE_MUX)
Googlere00b8eb2019-07-08 16:37:07 -0700149/** Console I/O multiplexing *******************************************/
150
151static struct stdio_dev *tstcdev;
152struct stdio_dev **console_devices[MAX_FILES];
153int cd_count[MAX_FILES];
154
155/*
156 * This depends on tstc() always being called before getc().
157 * This is guaranteed to be true because this routine is called
158 * only from fgetc() which assures it.
159 * No attempt is made to demultiplex multiple input sources.
160 */
161static int console_getc(int file)
162{
163 unsigned char ret;
164
165 /* This is never called with testcdev == NULL */
166 ret = tstcdev->getc(tstcdev);
167 tstcdev = NULL;
168 return ret;
169}
170
171static int console_tstc(int file)
172{
173 int i, ret;
174 struct stdio_dev *dev;
Googlere00b8eb2019-07-08 16:37:07 -0700175
Googlera8fd56b2024-10-24 14:04:51 +0800176 disable_ctrlc(1);
Googlere00b8eb2019-07-08 16:37:07 -0700177 for (i = 0; i < cd_count[file]; i++) {
178 dev = console_devices[file][i];
179 if (dev->tstc != NULL) {
180 ret = dev->tstc(dev);
181 if (ret > 0) {
182 tstcdev = dev;
Googlera8fd56b2024-10-24 14:04:51 +0800183 disable_ctrlc(0);
Googlere00b8eb2019-07-08 16:37:07 -0700184 return ret;
185 }
186 }
187 }
Googlera8fd56b2024-10-24 14:04:51 +0800188 disable_ctrlc(0);
Googlere00b8eb2019-07-08 16:37:07 -0700189
190 return 0;
191}
192
193static void console_putc(int file, const char c)
194{
195 int i;
196 struct stdio_dev *dev;
197
198 for (i = 0; i < cd_count[file]; i++) {
199 dev = console_devices[file][i];
200 if (dev->putc != NULL)
201 dev->putc(dev, c);
202 }
203}
204
Googlere00b8eb2019-07-08 16:37:07 -0700205static void console_puts(int file, const char *s)
206{
207 int i;
208 struct stdio_dev *dev;
209
210 for (i = 0; i < cd_count[file]; i++) {
211 dev = console_devices[file][i];
212 if (dev->puts != NULL)
213 dev->puts(dev, s);
214 }
215}
216
Googlera8fd56b2024-10-24 14:04:51 +0800217static inline void console_printdevs(int file)
218{
219 iomux_printdevs(file);
220}
221
Googlere00b8eb2019-07-08 16:37:07 -0700222static inline void console_doenv(int file, struct stdio_dev *dev)
223{
224 iomux_doenv(file, dev->name);
225}
226#else
227static inline int console_getc(int file)
228{
229 return stdio_devices[file]->getc(stdio_devices[file]);
230}
231
232static inline int console_tstc(int file)
233{
234 return stdio_devices[file]->tstc(stdio_devices[file]);
235}
236
237static inline void console_putc(int file, const char c)
238{
239 stdio_devices[file]->putc(stdio_devices[file], c);
240}
241
Googlere00b8eb2019-07-08 16:37:07 -0700242static inline void console_puts(int file, const char *s)
243{
244 stdio_devices[file]->puts(stdio_devices[file], s);
245}
246
Googlera8fd56b2024-10-24 14:04:51 +0800247static inline void console_printdevs(int file)
248{
249 printf("%s\n", stdio_devices[file]->name);
250}
251
Googlere00b8eb2019-07-08 16:37:07 -0700252static inline void console_doenv(int file, struct stdio_dev *dev)
253{
254 console_setfile(file, dev);
255}
Googlera8fd56b2024-10-24 14:04:51 +0800256#endif /* defined(CONFIG_CONSOLE_MUX) */
Googlere00b8eb2019-07-08 16:37:07 -0700257
258/** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
259
260int serial_printf(const char *fmt, ...)
261{
262 va_list args;
263 uint i;
264 char printbuffer[CONFIG_SYS_PBSIZE];
265
266 va_start(args, fmt);
267
268 /* For this to work, printbuffer must be larger than
269 * anything we ever want to print.
270 */
271 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
272 va_end(args);
273
274 serial_puts(printbuffer);
275 return i;
276}
277
278int fgetc(int file)
279{
280 if (file < MAX_FILES) {
Googlera8fd56b2024-10-24 14:04:51 +0800281#if defined(CONFIG_CONSOLE_MUX)
Googlere00b8eb2019-07-08 16:37:07 -0700282 /*
283 * Effectively poll for input wherever it may be available.
284 */
285 for (;;) {
Googlere00b8eb2019-07-08 16:37:07 -0700286 /*
287 * Upper layer may have already called tstc() so
288 * check for that first.
289 */
290 if (tstcdev != NULL)
291 return console_getc(file);
292 console_tstc(file);
Googlere00b8eb2019-07-08 16:37:07 -0700293#ifdef CONFIG_WATCHDOG
294 /*
295 * If the watchdog must be rate-limited then it should
296 * already be handled in board-specific code.
297 */
298 udelay(1);
299#endif
300 }
Googlera8fd56b2024-10-24 14:04:51 +0800301#else
302 return console_getc(file);
303#endif
Googlere00b8eb2019-07-08 16:37:07 -0700304 }
305
306 return -1;
307}
308
309int ftstc(int file)
310{
311 if (file < MAX_FILES)
312 return console_tstc(file);
313
314 return -1;
315}
316
317void fputc(int file, const char c)
318{
319 if (file < MAX_FILES)
320 console_putc(file, c);
321}
322
323void fputs(int file, const char *s)
324{
325 if (file < MAX_FILES)
326 console_puts(file, s);
327}
328
329int fprintf(int file, const char *fmt, ...)
330{
331 va_list args;
332 uint i;
333 char printbuffer[CONFIG_SYS_PBSIZE];
334
335 va_start(args, fmt);
336
337 /* For this to work, printbuffer must be larger than
338 * anything we ever want to print.
339 */
340 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
341 va_end(args);
342
343 /* Send to desired file */
344 fputs(file, printbuffer);
345 return i;
346}
347
348/** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
349
350int getc(void)
351{
352#ifdef CONFIG_DISABLE_CONSOLE
353 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
354 return 0;
355#endif
356
357 if (!gd->have_console)
358 return 0;
359
Googlere00b8eb2019-07-08 16:37:07 -0700360 if (gd->flags & GD_FLG_DEVINIT) {
361 /* Get from the standard input */
362 return fgetc(stdin);
363 }
364
365 /* Send directly to the handler */
366 return serial_getc();
367}
368
369int tstc(void)
370{
371#ifdef CONFIG_DISABLE_CONSOLE
372 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
373 return 0;
374#endif
375
376 if (!gd->have_console)
377 return 0;
Googlera8fd56b2024-10-24 14:04:51 +0800378
Googlere00b8eb2019-07-08 16:37:07 -0700379 if (gd->flags & GD_FLG_DEVINIT) {
380 /* Test the standard input */
381 return ftstc(stdin);
382 }
383
384 /* Send directly to the handler */
385 return serial_tstc();
386}
387
Googlera8fd56b2024-10-24 14:04:51 +0800388#ifdef CONFIG_PRE_CONSOLE_BUFFER
Googlere00b8eb2019-07-08 16:37:07 -0700389#define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_PRE_CON_BUF_SZ)
390
391static void pre_console_putc(const char c)
392{
Googlera8fd56b2024-10-24 14:04:51 +0800393 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
Googlere00b8eb2019-07-08 16:37:07 -0700394
395 buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
Googlere00b8eb2019-07-08 16:37:07 -0700396}
397
398static void pre_console_puts(const char *s)
399{
400 while (*s)
401 pre_console_putc(*s++);
402}
403
Googlera8fd56b2024-10-24 14:04:51 +0800404static void print_pre_console_buffer(void)
Googlere00b8eb2019-07-08 16:37:07 -0700405{
Googlera8fd56b2024-10-24 14:04:51 +0800406 unsigned long i = 0;
407 char *buffer = (char *)CONFIG_PRE_CON_BUF_ADDR;
Googlere00b8eb2019-07-08 16:37:07 -0700408
Googlere00b8eb2019-07-08 16:37:07 -0700409 if (gd->precon_buf_idx > CONFIG_PRE_CON_BUF_SZ)
Googlera8fd56b2024-10-24 14:04:51 +0800410 i = gd->precon_buf_idx - CONFIG_PRE_CON_BUF_SZ;
Googlere00b8eb2019-07-08 16:37:07 -0700411
Googlera8fd56b2024-10-24 14:04:51 +0800412 while (i < gd->precon_buf_idx)
413 putc(buffer[CIRC_BUF_IDX(i++)]);
Googlere00b8eb2019-07-08 16:37:07 -0700414}
415#else
416static inline void pre_console_putc(const char c) {}
417static inline void pre_console_puts(const char *s) {}
Googlera8fd56b2024-10-24 14:04:51 +0800418static inline void print_pre_console_buffer(void) {}
Googlere00b8eb2019-07-08 16:37:07 -0700419#endif
420
Googlera8fd56b2024-10-24 14:04:51 +0800421
422#ifdef CONFIG_SILENT_CONSOLE
423static void print_to_buf(const char *s);
424#endif
425
426
Googlere00b8eb2019-07-08 16:37:07 -0700427void putc(const char c)
428{
429#ifdef CONFIG_SANDBOX
Googlere00b8eb2019-07-08 16:37:07 -0700430 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
431 os_putc(c);
432 return;
433 }
434#endif
Googlera8fd56b2024-10-24 14:04:51 +0800435#ifdef CONFIG_SILENT_CONSOLE
436 char s[2];
437 s[0] = c;
438 s[1] = '\0';
439 if (gd->flags & GD_FLG_SILENT) {
440 print_to_buf(s);
Googlere00b8eb2019-07-08 16:37:07 -0700441 return;
442 }
443#endif
Googlere00b8eb2019-07-08 16:37:07 -0700444
445#ifdef CONFIG_DISABLE_CONSOLE
446 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
447 return;
448#endif
449
450 if (!gd->have_console)
451 return pre_console_putc(c);
452
453 if (gd->flags & GD_FLG_DEVINIT) {
454 /* Send to the standard output */
455 fputc(stdout, c);
456 } else {
457 /* Send directly to the handler */
Googlere00b8eb2019-07-08 16:37:07 -0700458 serial_putc(c);
459 }
460}
461
Googlera8fd56b2024-10-24 14:04:51 +0800462#ifdef CONFIG_SILENT_CONSOLE
463#define PRT_BUF_SIZE 65536
464#define PRT_BUF_END (PRT_BUF_SIZE - 8)
465static int buf_p = 0;
466static char* print_buf = NULL;
467
468static void print_to_buf(const char *s)
469{
470 int len, end;
471 int i = 0;
472 if (!print_buf)
473 return;
474 if (buf_p < PRT_BUF_END) {
475 len = strlen(s);
476 end = buf_p + len;
477 if (end < PRT_BUF_END) {
478 while (buf_p < end)
479 print_buf[buf_p++] = s[i++];
480 } else {
481 end = PRT_BUF_END;
482 while (buf_p < end)
483 print_buf[buf_p++] = s[i++];
484 while (buf_p < PRT_BUF_END + 6)
485 print_buf[buf_p++] = '.';
486 print_buf[buf_p++] = '\n';
487 }
488 }
489 return;
490}
491
492void flush_print_buf(void)
493{
494 char tmp_buf[CONFIG_SYS_PBSIZE + 1];
495 int out_p = 0;
496 int left_size = buf_p;
497 if (print_buf) {
498 memset(tmp_buf, 0, CONFIG_SYS_PBSIZE + 1);
499 while (left_size > CONFIG_SYS_PBSIZE) {
500 memcpy(tmp_buf, print_buf + out_p, CONFIG_SYS_PBSIZE);
501 printf("%s", tmp_buf);
502 left_size -= CONFIG_SYS_PBSIZE;
503 out_p += CONFIG_SYS_PBSIZE;
504 memset(tmp_buf, 0, CONFIG_SYS_PBSIZE + 1);
505 }
506 if (left_size) {
507 memcpy(tmp_buf, print_buf + out_p, left_size);
508 printf("%s", tmp_buf);
509 }
510 memset(print_buf, 0, PRT_BUF_SIZE);
511 buf_p = 0;
512 }
513 return;
514}
515
516void destroy_print_buf(void)
517{
518 if (print_buf)
519 free(print_buf);
520 print_buf = NULL;
521 buf_p = 0;
522}
523
524static int do_silent(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
525{
526 if (argc != 2)
527 return -1;
528 if (!strncmp(argv[1], "on", 6))
529 gd->flags |= GD_FLG_SILENT;
530 else if (!strncmp(argv[1], "off", 7))
531 gd->flags &= ~GD_FLG_SILENT;
532 else
533 return -1;
534 return 0;
535}
536
537U_BOOT_CMD(
538 silent, //command name
539 2, //maxargs
540 0, //repeatable
541 do_silent, //command function
542 "silent",
543 "make console silence on/off"
544);
545#endif /* CONFIG_MUTE_PRINT */
546
Googlere00b8eb2019-07-08 16:37:07 -0700547void puts(const char *s)
548{
549#ifdef CONFIG_SANDBOX
Googlere00b8eb2019-07-08 16:37:07 -0700550 if (!gd || !(gd->flags & GD_FLG_SERIAL_READY)) {
551 os_puts(s);
552 return;
553 }
554#endif
Googlere00b8eb2019-07-08 16:37:07 -0700555
Googlera8fd56b2024-10-24 14:04:51 +0800556#ifdef CONFIG_SILENT_CONSOLE
557 if (gd->flags & GD_FLG_SILENT) {
558 print_to_buf(s);
Googlere00b8eb2019-07-08 16:37:07 -0700559 return;
560 }
561#endif
Googlere00b8eb2019-07-08 16:37:07 -0700562
563#ifdef CONFIG_DISABLE_CONSOLE
564 if (gd->flags & GD_FLG_DISABLE_CONSOLE)
565 return;
566#endif
567
568 if (!gd->have_console)
569 return pre_console_puts(s);
570
571 if (gd->flags & GD_FLG_DEVINIT) {
572 /* Send to the standard output */
573 fputs(stdout, s);
574 } else {
575 /* Send directly to the handler */
Googlere00b8eb2019-07-08 16:37:07 -0700576 serial_puts(s);
577 }
578}
579
Googlera8fd56b2024-10-24 14:04:51 +0800580int printf(const char *fmt, ...)
Googlere00b8eb2019-07-08 16:37:07 -0700581{
Googlera8fd56b2024-10-24 14:04:51 +0800582 va_list args;
583 uint i;
584 char printbuffer[CONFIG_SYS_PBSIZE];
Googlere00b8eb2019-07-08 16:37:07 -0700585
Googlera8fd56b2024-10-24 14:04:51 +0800586#if !defined(CONFIG_SANDBOX) && !defined(CONFIG_PRE_CONSOLE_BUFFER)
587 if (!gd->have_console)
588 return 0;
Googler3cec7d72023-12-13 10:05:01 +0000589#endif
590
Googlera8fd56b2024-10-24 14:04:51 +0800591 va_start(args, fmt);
592
593 /* For this to work, printbuffer must be larger than
594 * anything we ever want to print.
595 */
596 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
597 va_end(args);
598
599 /* Print the string */
600 puts(printbuffer);
601 return i;
602}
603
604int vprintf(const char *fmt, va_list args)
605{
606 uint i;
607 char printbuffer[CONFIG_SYS_PBSIZE];
608
609#if defined(CONFIG_PRE_CONSOLE_BUFFER) && !defined(CONFIG_SANDBOX)
610 if (!gd->have_console)
611 return 0;
612#endif
613
614 /* For this to work, printbuffer must be larger than
615 * anything we ever want to print.
616 */
617 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
618
619 /* Print the string */
620 puts(printbuffer);
621 return i;
622}
623
Googlere00b8eb2019-07-08 16:37:07 -0700624/* test if ctrl-c was pressed */
625static int ctrlc_disabled = 0; /* see disable_ctrl() */
626static int ctrlc_was_pressed = 0;
627int ctrlc(void)
628{
Googlera8fd56b2024-10-24 14:04:51 +0800629#ifndef CONFIG_SANDBOX
Googlere00b8eb2019-07-08 16:37:07 -0700630 if (!ctrlc_disabled && gd->have_console) {
631 if (tstc()) {
632 switch (getc()) {
633 case 0x03: /* ^C - Control C */
634 ctrlc_was_pressed = 1;
635 return 1;
636 default:
637 break;
638 }
639 }
640 }
Googlera8fd56b2024-10-24 14:04:51 +0800641#endif
Googlere00b8eb2019-07-08 16:37:07 -0700642
643 return 0;
644}
645/* Reads user's confirmation.
646 Returns 1 if user's input is "y", "Y", "yes" or "YES"
647*/
648int confirm_yesno(void)
649{
650 int i;
651 char str_input[5];
652
653 /* Flush input */
654 while (tstc())
655 getc();
656 i = 0;
657 while (i < sizeof(str_input)) {
658 str_input[i] = getc();
659 putc(str_input[i]);
660 if (str_input[i] == '\r')
661 break;
662 i++;
663 }
664 putc('\n');
665 if (strncmp(str_input, "y\r", 2) == 0 ||
666 strncmp(str_input, "Y\r", 2) == 0 ||
667 strncmp(str_input, "yes\r", 4) == 0 ||
668 strncmp(str_input, "YES\r", 4) == 0)
669 return 1;
670 return 0;
671}
672/* pass 1 to disable ctrlc() checking, 0 to enable.
673 * returns previous state
674 */
675int disable_ctrlc(int disable)
676{
677 int prev = ctrlc_disabled; /* save previous state */
678
679 ctrlc_disabled = disable;
680 return prev;
681}
682
683int had_ctrlc (void)
684{
685 return ctrlc_was_pressed;
686}
687
688void clear_ctrlc(void)
689{
690 ctrlc_was_pressed = 0;
691}
692
Googlera8fd56b2024-10-24 14:04:51 +0800693#ifdef CONFIG_MODEM_SUPPORT_DEBUG
694char screen[1024];
695char *cursor = screen;
696int once = 0;
697inline void dbg(const char *fmt, ...)
698{
699 va_list args;
700 uint i;
701 char printbuffer[CONFIG_SYS_PBSIZE];
702
703 if (!once) {
704 memset(screen, 0, sizeof(screen));
705 once++;
706 }
707
708 va_start(args, fmt);
709
710 /* For this to work, printbuffer must be larger than
711 * anything we ever want to print.
712 */
713 i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
714 va_end(args);
715
716 if ((screen + sizeof(screen) - 1 - cursor)
717 < strlen(printbuffer) + 1) {
718 memset(screen, 0, sizeof(screen));
719 cursor = screen;
720 }
721 sprintf(cursor, printbuffer);
722 cursor += strlen(printbuffer);
723
724}
725#else
726static inline void dbg(const char *fmt, ...)
727{
728}
729#endif
730
Googlere00b8eb2019-07-08 16:37:07 -0700731/** U-Boot INIT FUNCTIONS *************************************************/
732
733struct stdio_dev *search_device(int flags, const char *name)
734{
735 struct stdio_dev *dev;
736
737 dev = stdio_get_by_name(name);
Googlere00b8eb2019-07-08 16:37:07 -0700738
739 if (dev && (dev->flags & flags))
740 return dev;
741
742 return NULL;
743}
744
745int console_assign(int file, const char *devname)
746{
747 int flag;
748 struct stdio_dev *dev;
749
750 /* Check for valid file */
751 switch (file) {
752 case stdin:
753 flag = DEV_FLAGS_INPUT;
754 break;
755 case stdout:
756 case stderr:
757 flag = DEV_FLAGS_OUTPUT;
758 break;
759 default:
760 return -1;
761 }
762
763 /* Check for valid device name */
764
765 dev = search_device(flag, devname);
766
767 if (dev)
768 return console_setfile(file, dev);
769
770 return -1;
771}
772
Googlere00b8eb2019-07-08 16:37:07 -0700773/* Called before relocation - use serial functions */
774int console_init_f(void)
775{
776 gd->have_console = 1;
777
Googlera8fd56b2024-10-24 14:04:51 +0800778 print_pre_console_buffer();
Googlere00b8eb2019-07-08 16:37:07 -0700779
Googlera8fd56b2024-10-24 14:04:51 +0800780 return 0;
781}
Googlere00b8eb2019-07-08 16:37:07 -0700782
Googlera8fd56b2024-10-24 14:04:51 +0800783int console_init_m(void)
784{
785#ifdef CONFIG_SILENT_CONSOLE
786 print_buf = (char*)malloc(PRT_BUF_SIZE);
787 if (!print_buf) {
788 puts("no memory for print_buf\n");
789 } else {
790 memset(print_buf, 0, PRT_BUF_SIZE);
791 buf_p = 0;
792 //gd->flags |= GD_FLG_SILENT;
793 }
794#endif
Googlere00b8eb2019-07-08 16:37:07 -0700795 return 0;
796}
797
798void stdio_print_current_devices(void)
799{
800 /* Print information */
Googler63dd2272024-10-23 19:29:29 +0800801 puts("In: ");
Googlere00b8eb2019-07-08 16:37:07 -0700802 if (stdio_devices[stdin] == NULL) {
803 puts("No input devices available!\n");
804 } else {
Googler63dd2272024-10-23 19:29:29 +0800805 printf ("%s\n", stdio_devices[stdin]->name);
Googlere00b8eb2019-07-08 16:37:07 -0700806 }
807
Googler63dd2272024-10-23 19:29:29 +0800808 puts("Out: ");
Googlere00b8eb2019-07-08 16:37:07 -0700809 if (stdio_devices[stdout] == NULL) {
810 puts("No output devices available!\n");
811 } else {
Googler63dd2272024-10-23 19:29:29 +0800812 printf ("%s\n", stdio_devices[stdout]->name);
Googlere00b8eb2019-07-08 16:37:07 -0700813 }
814
Googler63dd2272024-10-23 19:29:29 +0800815 puts("Err: ");
Googlere00b8eb2019-07-08 16:37:07 -0700816 if (stdio_devices[stderr] == NULL) {
817 puts("No error devices available!\n");
818 } else {
Googler63dd2272024-10-23 19:29:29 +0800819 printf ("%s\n", stdio_devices[stderr]->name);
Googlere00b8eb2019-07-08 16:37:07 -0700820 }
821}
822
Googlera8fd56b2024-10-24 14:04:51 +0800823#ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
Googlere00b8eb2019-07-08 16:37:07 -0700824/* Called after the relocation - use desired console functions */
825int console_init_r(void)
826{
827 char *stdinname, *stdoutname, *stderrname;
828 struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
829#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
830 int i;
831#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
Googlera8fd56b2024-10-24 14:04:51 +0800832#ifdef CONFIG_CONSOLE_MUX
Googlere00b8eb2019-07-08 16:37:07 -0700833 int iomux_err = 0;
834#endif
Googlere00b8eb2019-07-08 16:37:07 -0700835 /* set default handlers at first */
Googlera8fd56b2024-10-24 14:04:51 +0800836 gd->jt[XF_getc] = serial_getc;
837 gd->jt[XF_tstc] = serial_tstc;
838 gd->jt[XF_putc] = serial_putc;
839 gd->jt[XF_puts] = serial_puts;
840 gd->jt[XF_printf] = serial_printf;
Googlere00b8eb2019-07-08 16:37:07 -0700841
842 /* stdin stdout and stderr are in environment */
843 /* scan for it */
Googlera8fd56b2024-10-24 14:04:51 +0800844 stdinname = getenv("stdin");
845 stdoutname = getenv("stdout");
846 stderrname = getenv("stderr");
Googlere00b8eb2019-07-08 16:37:07 -0700847
848 if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
849 inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
850 outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
851 errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
Googlera8fd56b2024-10-24 14:04:51 +0800852#ifdef CONFIG_CONSOLE_MUX
Googlere00b8eb2019-07-08 16:37:07 -0700853 iomux_err = iomux_doenv(stdin, stdinname);
854 iomux_err += iomux_doenv(stdout, stdoutname);
855 iomux_err += iomux_doenv(stderr, stderrname);
856 if (!iomux_err)
857 /* Successful, so skip all the code below. */
858 goto done;
859#endif
860 }
861 /* if the devices are overwritten or not found, use default device */
862 if (inputdev == NULL) {
863 inputdev = search_device(DEV_FLAGS_INPUT, "serial");
864 }
865 if (outputdev == NULL) {
866 outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
867 }
868 if (errdev == NULL) {
869 errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
870 }
871 /* Initializes output console first */
872 if (outputdev != NULL) {
873 /* need to set a console if not done above. */
874 console_doenv(stdout, outputdev);
875 }
876 if (errdev != NULL) {
877 /* need to set a console if not done above. */
878 console_doenv(stderr, errdev);
879 }
880 if (inputdev != NULL) {
881 /* need to set a console if not done above. */
882 console_doenv(stdin, inputdev);
883 }
884
Googlera8fd56b2024-10-24 14:04:51 +0800885#ifdef CONFIG_CONSOLE_MUX
Googlere00b8eb2019-07-08 16:37:07 -0700886done:
887#endif
888
889#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
890 stdio_print_current_devices();
891#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
Googlere00b8eb2019-07-08 16:37:07 -0700892
893#ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
894 /* set the environment variables (will overwrite previous env settings) */
Googlera8fd56b2024-10-24 14:04:51 +0800895 for (i = 0; i < 3; i++) {
896 setenv(stdio_names[i], stdio_devices[i]->name);
Googlere00b8eb2019-07-08 16:37:07 -0700897 }
898#endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
899
900 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
901
902#if 0
903 /* If nothing usable installed, use only the initial console */
904 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
905 return 0;
906#endif
Googlere00b8eb2019-07-08 16:37:07 -0700907 return 0;
908}
909
Googlera8fd56b2024-10-24 14:04:51 +0800910#else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
Googlere00b8eb2019-07-08 16:37:07 -0700911
912/* Called after the relocation - use desired console functions */
913int console_init_r(void)
914{
915 struct stdio_dev *inputdev = NULL, *outputdev = NULL;
916 int i;
917 struct list_head *list = stdio_get_list();
918 struct list_head *pos;
919 struct stdio_dev *dev;
920
Googlere00b8eb2019-07-08 16:37:07 -0700921#ifdef CONFIG_SPLASH_SCREEN
922 /*
923 * suppress all output if splash screen is enabled and we have
924 * a bmp to display. We redirect the output from frame buffer
925 * console to serial console in this case or suppress it if
926 * "silent" mode was requested.
927 */
Googlera8fd56b2024-10-24 14:04:51 +0800928 if (getenv("splashimage") != NULL) {
Googlere00b8eb2019-07-08 16:37:07 -0700929 if (!(gd->flags & GD_FLG_SILENT))
930 outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
931 }
932#endif
933
934 /* Scan devices looking for input and output devices */
935 list_for_each(pos, list) {
936 dev = list_entry(pos, struct stdio_dev, list);
937
938 if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
939 inputdev = dev;
940 }
941 if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
942 outputdev = dev;
943 }
944 if(inputdev && outputdev)
945 break;
946 }
947
948 /* Initializes output console first */
949 if (outputdev != NULL) {
950 console_setfile(stdout, outputdev);
951 console_setfile(stderr, outputdev);
Googlera8fd56b2024-10-24 14:04:51 +0800952#ifdef CONFIG_CONSOLE_MUX
Googlere00b8eb2019-07-08 16:37:07 -0700953 console_devices[stdout][0] = outputdev;
954 console_devices[stderr][0] = outputdev;
955#endif
956 }
957
958 /* Initializes input console */
959 if (inputdev != NULL) {
960 console_setfile(stdin, inputdev);
Googlera8fd56b2024-10-24 14:04:51 +0800961#ifdef CONFIG_CONSOLE_MUX
Googlere00b8eb2019-07-08 16:37:07 -0700962 console_devices[stdin][0] = inputdev;
963#endif
964 }
965
966#ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
967 stdio_print_current_devices();
968#endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
969
970 /* Setting environment variables */
Googlera8fd56b2024-10-24 14:04:51 +0800971 for (i = 0; i < 3; i++) {
972 setenv(stdio_names[i], stdio_devices[i]->name);
Googlere00b8eb2019-07-08 16:37:07 -0700973 }
974
975 gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
976
977#if 0
978 /* If nothing usable installed, use only the initial console */
979 if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
980 return 0;
981#endif
Googlera8fd56b2024-10-24 14:04:51 +0800982
Googlere00b8eb2019-07-08 16:37:07 -0700983 return 0;
984}
985
Googlera8fd56b2024-10-24 14:04:51 +0800986#endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */