xref: /freebsd/stand/libofw/ofw_console.c (revision 815b7436a7c6302365b6514194d27d41cb736227)
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 #include <sys/types.h>
31 
32 #include "bootstrap.h"
33 #include "openfirm.h"
34 
35 static void ofw_cons_probe(struct console *cp);
36 static int ofw_cons_init(int);
37 void ofw_cons_putchar(int);
38 int ofw_cons_getchar(void);
39 int ofw_cons_poll(void);
40 
41 static ihandle_t stdin;
42 static ihandle_t stdout;
43 
44 struct console ofwconsole = {
45 	"ofw",
46 	"Open Firmware console",
47 	0,
48 	ofw_cons_probe,
49 	ofw_cons_init,
50 	ofw_cons_putchar,
51 	ofw_cons_getchar,
52 	ofw_cons_poll,
53 };
54 
55 static void
56 ofw_cons_probe(struct console *cp)
57 {
58 
59 	OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));
60 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
61 	cp->c_flags |= C_PRESENTIN|C_PRESENTOUT;
62 }
63 
64 static int
65 ofw_cons_init(int arg)
66 {
67 	return 0;
68 }
69 
70 void
71 ofw_cons_putchar(int c)
72 {
73 	char cbuf;
74 
75 	if (c == '\n') {
76 		cbuf = '\r';
77 		OF_write(stdout, &cbuf, 1);
78 	}
79 
80 	cbuf = c;
81 	OF_write(stdout, &cbuf, 1);
82 }
83 
84 static int saved_char = -1;
85 
86 int
87 ofw_cons_getchar()
88 {
89 	unsigned char ch = '\0';
90 	int l;
91 
92 	if (saved_char != -1) {
93 		l = saved_char;
94 		saved_char = -1;
95 		return l;
96 	}
97 
98 	/* At least since version 4.0.0, QEMU became bug-compatible
99 	 * with PowerVM's vty, by inserting a \0 after every \r.
100 	 * As this confuses loader's interpreter and as a \0 coming
101 	 * from the console doesn't seem reasonable, it's filtered here. */
102 	if (OF_read(stdin, &ch, 1) > 0 && ch != '\0')
103 		return (ch);
104 
105 	return (-1);
106 }
107 
108 int
109 ofw_cons_poll()
110 {
111 	unsigned char ch;
112 
113 	if (saved_char != -1)
114 		return 1;
115 
116 	if (OF_read(stdin, &ch, 1) > 0) {
117 		saved_char = ch;
118 		return 1;
119 	}
120 
121 	return 0;
122 }
123