xref: /freebsd/stand/libofw/ofw_console.c (revision c7581d76a1e46a925bba59d3e18394908f65c907)
1475008d6SBrandon Bergren /* $NetBSD: prom.c,v 1.3 1997/09/06 14:03:58 drochner Exp $ */
2475008d6SBrandon Bergren 
3475008d6SBrandon Bergren /*-
4475008d6SBrandon Bergren  * Mach Operating System
5475008d6SBrandon Bergren  * Copyright (c) 1992 Carnegie Mellon University
6475008d6SBrandon Bergren  * All Rights Reserved.
7475008d6SBrandon Bergren  *
8475008d6SBrandon Bergren  * Permission to use, copy, modify and distribute this software and its
9475008d6SBrandon Bergren  * documentation is hereby granted, provided that both the copyright
10475008d6SBrandon Bergren  * notice and this permission notice appear in all copies of the
11475008d6SBrandon Bergren  * software, derivative works or modified versions, and any portions
12475008d6SBrandon Bergren  * thereof, and that both notices appear in supporting documentation.
13475008d6SBrandon Bergren  *
14475008d6SBrandon Bergren  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15475008d6SBrandon Bergren  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16475008d6SBrandon Bergren  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17475008d6SBrandon Bergren  *
18475008d6SBrandon Bergren  * Carnegie Mellon requests users of this software to return to
19475008d6SBrandon Bergren  *
20475008d6SBrandon Bergren  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21475008d6SBrandon Bergren  *  School of Computer Science
22475008d6SBrandon Bergren  *  Carnegie Mellon University
23475008d6SBrandon Bergren  *  Pittsburgh PA 15213-3890
24475008d6SBrandon Bergren  *
25475008d6SBrandon Bergren  * any improvements or extensions that they make and grant Carnegie Mellon
26475008d6SBrandon Bergren  * the rights to redistribute these changes.
27475008d6SBrandon Bergren  */
28475008d6SBrandon Bergren 
29475008d6SBrandon Bergren #include <sys/types.h>
30475008d6SBrandon Bergren 
31475008d6SBrandon Bergren #include "bootstrap.h"
32475008d6SBrandon Bergren #include "openfirm.h"
33475008d6SBrandon Bergren 
34475008d6SBrandon Bergren static void ofw_cons_probe(struct console *cp);
35475008d6SBrandon Bergren static int ofw_cons_init(int);
36475008d6SBrandon Bergren void ofw_cons_putchar(int);
37475008d6SBrandon Bergren int ofw_cons_getchar(void);
38475008d6SBrandon Bergren int ofw_cons_poll(void);
39475008d6SBrandon Bergren 
40475008d6SBrandon Bergren static ihandle_t stdin;
41475008d6SBrandon Bergren static ihandle_t stdout;
42475008d6SBrandon Bergren 
43475008d6SBrandon Bergren struct console ofwconsole = {
44*c7581d76SWarner Losh 	.c_name = "ofw",
451f180d0aSWarner Losh 	.c_desc = "Open Firmware console",
461f180d0aSWarner Losh 	.c_probe = ofw_cons_probe,
471f180d0aSWarner Losh 	.c_init = ofw_cons_init,
481f180d0aSWarner Losh 	.c_out = ofw_cons_putchar,
491f180d0aSWarner Losh 	.c_in = ofw_cons_getchar,
501f180d0aSWarner Losh 	.c_ready = ofw_cons_poll,
51475008d6SBrandon Bergren };
52475008d6SBrandon Bergren 
53475008d6SBrandon Bergren static void
ofw_cons_probe(struct console * cp)54475008d6SBrandon Bergren ofw_cons_probe(struct console *cp)
55475008d6SBrandon Bergren {
56475008d6SBrandon Bergren 
57475008d6SBrandon Bergren 	OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));
58475008d6SBrandon Bergren 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
59475008d6SBrandon Bergren 	cp->c_flags |= C_PRESENTIN|C_PRESENTOUT;
60475008d6SBrandon Bergren }
61475008d6SBrandon Bergren 
62475008d6SBrandon Bergren static int
ofw_cons_init(int arg)63475008d6SBrandon Bergren ofw_cons_init(int arg)
64475008d6SBrandon Bergren {
654cddd20eSWarner Losh 	return (0);
66475008d6SBrandon Bergren }
67475008d6SBrandon Bergren 
68475008d6SBrandon Bergren void
ofw_cons_putchar(int c)69475008d6SBrandon Bergren ofw_cons_putchar(int c)
70475008d6SBrandon Bergren {
71475008d6SBrandon Bergren 	char cbuf;
72475008d6SBrandon Bergren 
73475008d6SBrandon Bergren 	if (c == '\n') {
74475008d6SBrandon Bergren 		cbuf = '\r';
75475008d6SBrandon Bergren 		OF_write(stdout, &cbuf, 1);
76475008d6SBrandon Bergren 	}
77475008d6SBrandon Bergren 
78475008d6SBrandon Bergren 	cbuf = c;
79475008d6SBrandon Bergren 	OF_write(stdout, &cbuf, 1);
80475008d6SBrandon Bergren }
81475008d6SBrandon Bergren 
82475008d6SBrandon Bergren static int saved_char = -1;
83475008d6SBrandon Bergren 
84475008d6SBrandon Bergren int
ofw_cons_getchar(void)85dcc20bceSWarner Losh ofw_cons_getchar(void)
86475008d6SBrandon Bergren {
87475008d6SBrandon Bergren 	unsigned char ch = '\0';
88475008d6SBrandon Bergren 	int l;
89475008d6SBrandon Bergren 
90475008d6SBrandon Bergren 	if (saved_char != -1) {
91475008d6SBrandon Bergren 		l = saved_char;
92475008d6SBrandon Bergren 		saved_char = -1;
934cddd20eSWarner Losh 		return (l);
94475008d6SBrandon Bergren 	}
95475008d6SBrandon Bergren 
96475008d6SBrandon Bergren 	/* At least since version 4.0.0, QEMU became bug-compatible
97475008d6SBrandon Bergren 	 * with PowerVM's vty, by inserting a \0 after every \r.
98475008d6SBrandon Bergren 	 * As this confuses loader's interpreter and as a \0 coming
99475008d6SBrandon Bergren 	 * from the console doesn't seem reasonable, it's filtered here. */
100475008d6SBrandon Bergren 	if (OF_read(stdin, &ch, 1) > 0 && ch != '\0')
101475008d6SBrandon Bergren 		return (ch);
102475008d6SBrandon Bergren 
103475008d6SBrandon Bergren 	return (-1);
104475008d6SBrandon Bergren }
105475008d6SBrandon Bergren 
106475008d6SBrandon Bergren int
ofw_cons_poll(void)107dcc20bceSWarner Losh ofw_cons_poll(void)
108475008d6SBrandon Bergren {
109475008d6SBrandon Bergren 	unsigned char ch;
110475008d6SBrandon Bergren 
111475008d6SBrandon Bergren 	if (saved_char != -1)
1124cddd20eSWarner Losh 		return (1);
113475008d6SBrandon Bergren 
114475008d6SBrandon Bergren 	if (OF_read(stdin, &ch, 1) > 0) {
115475008d6SBrandon Bergren 		saved_char = ch;
1164cddd20eSWarner Losh 		return (1);
117475008d6SBrandon Bergren 	}
118475008d6SBrandon Bergren 
1194cddd20eSWarner Losh 	return (0);
120475008d6SBrandon Bergren }
121