console.c revision 1.2.2.3 1 1.2.2.3 kardel /* $NetBSD: console.c,v 1.2.2.3 2006/06/01 22:34:54 kardel Exp $ */
2 1.2.2.2 simonb
3 1.2.2.2 simonb /*-
4 1.2.2.2 simonb * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
5 1.2.2.2 simonb * All rights reserved.
6 1.2.2.2 simonb *
7 1.2.2.2 simonb * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 simonb * modification, are permitted provided that the following conditions
9 1.2.2.2 simonb * are met:
10 1.2.2.2 simonb * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 simonb * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 simonb * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 simonb * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 simonb * documentation and/or other materials provided with the distribution.
15 1.2.2.2 simonb *
16 1.2.2.2 simonb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.2.2.2 simonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2.2.2 simonb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.2.2 simonb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.2.2.2 simonb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2.2.2 simonb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2.2.2 simonb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.2.2 simonb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2.2.2 simonb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.2.2 simonb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.2.2 simonb * SUCH DAMAGE.
27 1.2.2.2 simonb */
28 1.2.2.2 simonb
29 1.2.2.2 simonb #include <sys/cdefs.h>
30 1.2.2.3 kardel /* __FBSDID("$FreeBSD: src/sys/boot/common/console.c,v 1.6 2003/08/25 23:30:41 obrien Exp $"); */
31 1.2.2.2 simonb
32 1.2.2.2 simonb #include <lib/libsa/stand.h>
33 1.2.2.2 simonb #include <lib/libkern/libkern.h>
34 1.2.2.2 simonb
35 1.2.2.2 simonb #include "bootstrap.h"
36 1.2.2.2 simonb /*
37 1.2.2.2 simonb * Core console support
38 1.2.2.2 simonb */
39 1.2.2.2 simonb
40 1.2.2.2 simonb static int cons_set(struct env_var *ev, int flags, void *value);
41 1.2.2.2 simonb static int cons_find(char *name);
42 1.2.2.2 simonb
43 1.2.2.2 simonb /*
44 1.2.2.2 simonb * Detect possible console(s) to use. The first probed console
45 1.2.2.2 simonb * is marked active. Also create the console variable.
46 1.2.2.2 simonb *
47 1.2.2.2 simonb * XXX Add logic for multiple console support.
48 1.2.2.2 simonb */
49 1.2.2.2 simonb void
50 1.2.2.2 simonb cons_probe(void)
51 1.2.2.2 simonb {
52 1.2.2.2 simonb int cons;
53 1.2.2.2 simonb int active;
54 1.2.2.2 simonb char *prefconsole;
55 1.2.2.2 simonb
56 1.2.2.2 simonb /* Do all console probes */
57 1.2.2.2 simonb for (cons = 0; consoles[cons] != NULL; cons++) {
58 1.2.2.2 simonb consoles[cons]->c_flags = 0;
59 1.2.2.2 simonb consoles[cons]->c_probe(consoles[cons]);
60 1.2.2.2 simonb }
61 1.2.2.2 simonb /* Now find the first working one */
62 1.2.2.2 simonb active = -1;
63 1.2.2.2 simonb for (cons = 0; consoles[cons] != NULL && active == -1; cons++) {
64 1.2.2.2 simonb consoles[cons]->c_flags = 0;
65 1.2.2.2 simonb consoles[cons]->c_probe(consoles[cons]);
66 1.2.2.2 simonb if (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT))
67 1.2.2.2 simonb active = cons;
68 1.2.2.2 simonb }
69 1.2.2.2 simonb
70 1.2.2.2 simonb /* Check to see if a console preference has already been registered */
71 1.2.2.2 simonb prefconsole = getenv("console");
72 1.2.2.2 simonb if (prefconsole != NULL)
73 1.2.2.2 simonb prefconsole = strdup(prefconsole);
74 1.2.2.2 simonb if (prefconsole != NULL) {
75 1.2.2.2 simonb unsetenv("console"); /* we want to replace this */
76 1.2.2.2 simonb for (cons = 0; consoles[cons] != NULL; cons++)
77 1.2.2.2 simonb /* look for the nominated console, use it if it's functional */
78 1.2.2.2 simonb if (!strcmp(prefconsole, consoles[cons]->c_name) &&
79 1.2.2.2 simonb (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT)))
80 1.2.2.2 simonb active = cons;
81 1.2.2.2 simonb free(prefconsole);
82 1.2.2.2 simonb }
83 1.2.2.2 simonb if (active == -1)
84 1.2.2.2 simonb active = 0;
85 1.2.2.2 simonb consoles[active]->c_flags |= (C_ACTIVEIN | C_ACTIVEOUT);
86 1.2.2.2 simonb consoles[active]->c_init(0);
87 1.2.2.2 simonb
88 1.2.2.2 simonb printf("Console: %s\n", consoles[active]->c_desc);
89 1.2.2.2 simonb env_setenv("console", EV_VOLATILE, consoles[active]->c_name, (ev_sethook_t *) cons_set,
90 1.2.2.2 simonb env_nounset);
91 1.2.2.2 simonb }
92 1.2.2.2 simonb
93 1.2.2.2 simonb int
94 1.2.2.2 simonb getchar(void)
95 1.2.2.2 simonb {
96 1.2.2.2 simonb int cons;
97 1.2.2.2 simonb int rv;
98 1.2.2.2 simonb
99 1.2.2.2 simonb /* Loop forever polling all active consoles */
100 1.2.2.2 simonb for(;;)
101 1.2.2.2 simonb for (cons = 0; consoles[cons] != NULL; cons++)
102 1.2.2.2 simonb if ((consoles[cons]->c_flags & C_ACTIVEIN) &&
103 1.2.2.2 simonb ((rv = consoles[cons]->c_in()) != -1))
104 1.2.2.2 simonb return(rv);
105 1.2.2.2 simonb }
106 1.2.2.2 simonb
107 1.2.2.2 simonb int
108 1.2.2.2 simonb ischar(void)
109 1.2.2.2 simonb {
110 1.2.2.2 simonb int cons;
111 1.2.2.2 simonb
112 1.2.2.2 simonb for (cons = 0; consoles[cons] != NULL; cons++)
113 1.2.2.2 simonb if ((consoles[cons]->c_flags & C_ACTIVEIN) &&
114 1.2.2.2 simonb (consoles[cons]->c_ready() != 0))
115 1.2.2.2 simonb return(1);
116 1.2.2.2 simonb return(0);
117 1.2.2.2 simonb }
118 1.2.2.2 simonb
119 1.2.2.2 simonb void
120 1.2.2.2 simonb putchar(int c)
121 1.2.2.2 simonb {
122 1.2.2.2 simonb int cons;
123 1.2.2.2 simonb
124 1.2.2.2 simonb /* Expand newlines */
125 1.2.2.2 simonb if (c == '\n')
126 1.2.2.2 simonb putchar('\r');
127 1.2.2.2 simonb
128 1.2.2.2 simonb for (cons = 0; consoles[cons] != NULL; cons++)
129 1.2.2.2 simonb if (consoles[cons]->c_flags & C_ACTIVEOUT)
130 1.2.2.2 simonb consoles[cons]->c_out(c);
131 1.2.2.2 simonb }
132 1.2.2.2 simonb
133 1.2.2.2 simonb static int
134 1.2.2.2 simonb cons_find(char *name)
135 1.2.2.2 simonb {
136 1.2.2.2 simonb int cons;
137 1.2.2.2 simonb
138 1.2.2.2 simonb for (cons = 0; consoles[cons] != NULL; cons++)
139 1.2.2.2 simonb if (!strcmp(consoles[cons]->c_name, name))
140 1.2.2.2 simonb return(cons);
141 1.2.2.2 simonb return(-1);
142 1.2.2.2 simonb }
143 1.2.2.2 simonb
144 1.2.2.2 simonb
145 1.2.2.2 simonb /*
146 1.2.2.2 simonb * Select a console.
147 1.2.2.2 simonb *
148 1.2.2.2 simonb * XXX Note that the console system design allows for some extension
149 1.2.2.2 simonb * here (eg. multiple consoles, input/output only, etc.)
150 1.2.2.2 simonb */
151 1.2.2.2 simonb static int
152 1.2.2.2 simonb cons_set(struct env_var *ev, int flags, void *value)
153 1.2.2.2 simonb {
154 1.2.2.2 simonb int cons, active;
155 1.2.2.2 simonb
156 1.2.2.2 simonb if ((value == NULL) || ((active = cons_find(value)) == -1)) {
157 1.2.2.2 simonb if (value != NULL)
158 1.2.2.2 simonb printf("no such console '%s'\n", (char *)value);
159 1.2.2.2 simonb printf("Available consoles:\n");
160 1.2.2.2 simonb for (cons = 0; consoles[cons] != NULL; cons++)
161 1.2.2.2 simonb printf(" %s\n", consoles[cons]->c_name);
162 1.2.2.2 simonb return(CMD_ERROR);
163 1.2.2.2 simonb }
164 1.2.2.2 simonb
165 1.2.2.2 simonb /* disable all current consoles */
166 1.2.2.2 simonb for (cons = 0; consoles[cons] != NULL; cons++)
167 1.2.2.2 simonb consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
168 1.2.2.2 simonb
169 1.2.2.2 simonb /* enable selected console */
170 1.2.2.2 simonb consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
171 1.2.2.2 simonb consoles[active]->c_init(0);
172 1.2.2.2 simonb
173 1.2.2.2 simonb env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
174 1.2.2.2 simonb return(CMD_OK);
175 1.2.2.2 simonb }
176