xref: /freebsd/stand/libofw/ofw_console.c (revision a7623790fb345e6dc986dfd31df0ace115e6f2e4)
1 /* $NetBSD: prom.c,v 1.3 1997/09/06 14:03:58 drochner Exp $ */
2 
3 /*-
4  * Mach Operating System
5  * Copyright (c) 1992 Carnegie Mellon University
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie Mellon
26  * the rights to redistribute these changes.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 
34 #include "bootstrap.h"
35 #include "openfirm.h"
36 
37 static void ofw_cons_probe(struct console *cp);
38 static int ofw_cons_init(int);
39 void ofw_cons_putchar(int);
40 int ofw_cons_getchar(void);
41 int ofw_cons_poll(void);
42 
43 static ihandle_t stdin;
44 static ihandle_t stdout;
45 
46 struct console ofwconsole = {
47 	"ofw",
48 	"Open Firmware console",
49 	0,
50 	ofw_cons_probe,
51 	ofw_cons_init,
52 	ofw_cons_putchar,
53 	ofw_cons_getchar,
54 	ofw_cons_poll,
55 };
56 
57 static void
58 ofw_cons_probe(struct console *cp)
59 {
60 
61 	OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));
62 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
63 	cp->c_flags |= C_PRESENTIN|C_PRESENTOUT;
64 }
65 
66 static int
67 ofw_cons_init(int arg)
68 {
69 	return 0;
70 }
71 
72 void
73 ofw_cons_putchar(int c)
74 {
75 	char cbuf;
76 
77 	if (c == '\n') {
78 		cbuf = '\r';
79 		OF_write(stdout, &cbuf, 1);
80 	}
81 
82 	cbuf = c;
83 	OF_write(stdout, &cbuf, 1);
84 }
85 
86 static int saved_char = -1;
87 
88 int
89 ofw_cons_getchar()
90 {
91 	unsigned char ch = '\0';
92 	int l;
93 
94 	if (saved_char != -1) {
95 		l = saved_char;
96 		saved_char = -1;
97 		return l;
98 	}
99 
100 	/* At least since version 4.0.0, QEMU became bug-compatible
101 	 * with PowerVM's vty, by inserting a \0 after every \r.
102 	 * As this confuses loader's interpreter and as a \0 coming
103 	 * from the console doesn't seem reasonable, it's filtered here. */
104 	if (OF_read(stdin, &ch, 1) > 0 && ch != '\0')
105 		return (ch);
106 
107 	return (-1);
108 }
109 
110 int
111 ofw_cons_poll()
112 {
113 	unsigned char ch;
114 
115 	if (saved_char != -1)
116 		return 1;
117 
118 	if (OF_read(stdin, &ch, 1) > 0) {
119 		saved_char = ch;
120 		return 1;
121 	}
122 
123 	return 0;
124 }
125