prom.c revision 1.6 1 /* $NetBSD: prom.c,v 1.6 1999/03/31 03:10:00 cgd 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 (at) 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/types.h>
30
31 #include <machine/prom.h>
32 #include <machine/rpb.h>
33
34 #include "common.h"
35
36 int console;
37
38 static int test_getchar(prom_return_t *, int *);
39 static void putonechar(int c);
40
41 void
42 init_prom_calls()
43 {
44 extern struct prom_vec prom_dispatch_v;
45 struct rpb *r;
46 struct crb *c;
47 char buf[4];
48
49 r = (struct rpb *)HWRPB_ADDR;
50 c = (struct crb *)((u_int8_t *)r + r->rpb_crb_off);
51
52 prom_dispatch_v.routine_arg = c->crb_v_dispatch;
53 prom_dispatch_v.routine = c->crb_v_dispatch->entry_va;
54
55 /* Look for console tty. */
56 prom_getenv(PROM_E_TTY_DEV, buf, 4);
57 console = buf[0] - '0';
58 }
59
60 static int
61 test_getchar(xret, xc)
62 prom_return_t *xret;
63 int *xc;
64 {
65 xret->bits = prom_dispatch(PROM_R_GETC, console);
66 *xc = xret->u.retval;
67 return xret->u.status == 0 || xret->u.status == 1;
68 }
69
70 int
71 getchar()
72 {
73 int c;
74 prom_return_t ret;
75
76 for (;;) {
77 if (test_getchar(&ret, &c)) {
78 if (c == 3)
79 halt();
80 return c;
81 }
82 }
83 }
84
85 static void
86 putonechar(c)
87 int c;
88 {
89 prom_return_t ret;
90 char cbuf = c;
91
92 do {
93 ret.bits = prom_dispatch(PROM_R_PUTS, console, &cbuf, 1);
94 } while ((ret.u.retval & 1) == 0);
95 }
96
97 void
98 putchar(c)
99 int c;
100 {
101 prom_return_t ret;
102 int typed_c;
103
104 if (c == '\r' || c == '\n') {
105 putonechar('\r');
106 c = '\n';
107 }
108 putonechar(c);
109 ret.bits = prom_dispatch(PROM_R_GETC, console);
110 if (ret.u.status == 0 || ret.u.status == 1)
111 if (ret.u.retval == 3)
112 halt();
113 if (test_getchar(&ret, &typed_c))
114 if (typed_c == 3)
115 halt();
116 }
117
118 int
119 prom_getenv(id, buf, len)
120 int id, len;
121 char *buf;
122 {
123 prom_return_t ret;
124
125 ret.bits = prom_dispatch(PROM_R_GETENV, id, buf, len-1);
126 if (ret.u.status & 0x4)
127 ret.u.retval = 0;
128 buf[ret.u.retval] = '\0';
129
130 return (ret.u.retval);
131 }
132