]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - sys/boot/common/console.c
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / sys / boot / common / console.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <stand.h>
31 #include <string.h>
32
33 #include "bootstrap.h"
34 /*
35  * Core console support
36  */
37
38 static int      cons_set(struct env_var *ev, int flags, const void *value);
39 static int      cons_find(const char *name);
40 static int      cons_check(const char *string);
41 static int      cons_change(const char *string);
42 static int      twiddle_set(struct env_var *ev, int flags, const void *value);
43
44 /*
45  * Detect possible console(s) to use.  If preferred console(s) have been
46  * specified, mark them as active. Else, mark the first probed console
47  * as active.  Also create the console variable.
48  */
49 void
50 cons_probe(void)
51 {
52     int                 cons;
53     int                 active;
54     char                *prefconsole;
55
56     /* We want a callback to install the new value when this var changes. */
57     env_setenv("twiddle_divisor", EV_VOLATILE, "1", twiddle_set, env_nounset);
58
59     /* Do all console probes */
60     for (cons = 0; consoles[cons] != NULL; cons++) {
61         consoles[cons]->c_flags = 0;
62         consoles[cons]->c_probe(consoles[cons]);
63     }
64     /* Now find the first working one */
65     active = -1;
66     for (cons = 0; consoles[cons] != NULL && active == -1; cons++) {
67         consoles[cons]->c_flags = 0;
68         consoles[cons]->c_probe(consoles[cons]);
69         if (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT))
70             active = cons;
71     }
72     /* Force a console even if all probes failed */
73     if (active == -1)
74         active = 0;
75
76     /* Check to see if a console preference has already been registered */
77     prefconsole = getenv("console");
78     if (prefconsole != NULL)
79         prefconsole = strdup(prefconsole);
80     if (prefconsole != NULL) {
81         unsetenv("console");            /* we want to replace this */
82         cons_change(prefconsole);
83     } else {
84         consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
85         consoles[active]->c_init(0);
86         prefconsole = strdup(consoles[active]->c_name);
87     }
88
89     printf("Consoles: ");
90     for (cons = 0; consoles[cons] != NULL; cons++)
91         if (consoles[cons]->c_flags & (C_ACTIVEIN | C_ACTIVEOUT))
92             printf("%s  ", consoles[cons]->c_desc);
93     printf("\n");
94
95     if (prefconsole != NULL) {
96         env_setenv("console", EV_VOLATILE, prefconsole, cons_set,
97             env_nounset);
98         free(prefconsole);
99     }
100 }
101
102 int
103 getchar(void)
104 {
105     int         cons;
106     int         rv;
107
108     /* Loop forever polling all active consoles */
109     for(;;)
110         for (cons = 0; consoles[cons] != NULL; cons++)
111             if ((consoles[cons]->c_flags & (C_PRESENTIN | C_ACTIVEIN)) ==
112                 (C_PRESENTIN | C_ACTIVEIN) &&
113                 ((rv = consoles[cons]->c_in()) != -1))
114                 return(rv);
115 }
116
117 int
118 ischar(void)
119 {
120     int         cons;
121
122     for (cons = 0; consoles[cons] != NULL; cons++)
123         if ((consoles[cons]->c_flags & (C_PRESENTIN | C_ACTIVEIN)) ==
124             (C_PRESENTIN | C_ACTIVEIN) &&
125             (consoles[cons]->c_ready() != 0))
126                 return(1);
127     return(0);
128 }
129
130 void
131 putchar(int c)
132 {
133     int         cons;
134
135     /* Expand newlines */
136     if (c == '\n')
137         putchar('\r');
138
139     for (cons = 0; consoles[cons] != NULL; cons++)
140         if ((consoles[cons]->c_flags & (C_PRESENTOUT | C_ACTIVEOUT)) ==
141             (C_PRESENTOUT | C_ACTIVEOUT))
142             consoles[cons]->c_out(c);
143 }
144
145 /*
146  * Find the console with the specified name.
147  */
148 static int
149 cons_find(const char *name)
150 {
151     int         cons;
152
153     for (cons = 0; consoles[cons] != NULL; cons++)
154         if (!strcmp(consoles[cons]->c_name, name))
155             return (cons);
156     return (-1);
157 }
158
159 /*
160  * Select one or more consoles.
161  */
162 static int
163 cons_set(struct env_var *ev, int flags, const void *value)
164 {
165     int         ret;
166
167     if ((value == NULL) || (cons_check(value) == 0)) {
168         /*
169          * Return CMD_OK instead of CMD_ERROR to prevent forth syntax error,
170          * which would prevent it processing any further loader.conf entries.
171          */
172         return (CMD_OK);
173     }
174
175     ret = cons_change(value);
176     if (ret != CMD_OK)
177         return (ret);
178
179     env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
180     return (CMD_OK);
181 }
182
183 /*
184  * Check that at least one the consoles listed in *string is valid
185  */
186 static int
187 cons_check(const char *string)
188 {
189     int         cons, found, failed;
190     char        *curpos, *dup, *next;
191
192     dup = next = strdup(string);
193     found = failed = 0;
194     while (next != NULL) {
195         curpos = strsep(&next, " ,");
196         if (*curpos != '\0') {
197             cons = cons_find(curpos);
198             if (cons == -1) {
199                 printf("console %s is invalid!\n", curpos);
200                 failed++;
201             } else {
202                 found++;
203             }
204         }
205     }
206
207     free(dup);
208
209     if (found == 0)
210         printf("no valid consoles!\n");
211
212     if (found == 0 || failed != 0) {
213         printf("Available consoles:\n");
214         for (cons = 0; consoles[cons] != NULL; cons++)
215             printf("    %s\n", consoles[cons]->c_name);
216     }
217
218     return (found);
219 }
220
221 /*
222  * Activate all the valid consoles listed in *string and disable all others.
223  */
224 static int
225 cons_change(const char *string)
226 {
227     int         cons, active;
228     char        *curpos, *dup, *next;
229
230     /* Disable all consoles */
231     for (cons = 0; consoles[cons] != NULL; cons++) {
232         consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
233     }
234
235     /* Enable selected consoles */
236     dup = next = strdup(string);
237     active = 0;
238     while (next != NULL) {
239         curpos = strsep(&next, " ,");
240         if (*curpos == '\0')
241                 continue;
242         cons = cons_find(curpos);
243         if (cons >= 0) {
244             consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
245             consoles[cons]->c_init(0);
246             if ((consoles[cons]->c_flags & (C_PRESENTIN | C_PRESENTOUT)) ==
247                 (C_PRESENTIN | C_PRESENTOUT)) {
248                 active++;
249                 continue;
250             }
251
252             if (active != 0) {
253                 /* If no consoles have initialised we wouldn't see this. */
254                 printf("console %s failed to initialize\n", consoles[cons]->c_name);
255             }
256         }
257     }
258
259     free(dup);
260
261     if (active == 0) {
262         /* All requested consoles failed to initialise, try to recover. */
263         for (cons = 0; consoles[cons] != NULL; cons++) {
264             consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
265             consoles[cons]->c_init(0);
266             if ((consoles[cons]->c_flags &
267                 (C_PRESENTIN | C_PRESENTOUT)) ==
268                 (C_PRESENTIN | C_PRESENTOUT))
269                 active++;
270         }
271
272         if (active == 0)
273             return (CMD_ERROR); /* Recovery failed. */
274     }
275
276     return (CMD_OK);
277 }
278
279 /*
280  * Change the twiddle divisor.
281  *
282  * The user can set the twiddle_divisor variable to directly control how fast
283  * the progress twiddle spins, useful for folks with slow serial consoles.  The
284  * code to monitor changes to the variable and propagate them to the twiddle
285  * routines has to live somewhere.  Twiddling is console-related so it's here.
286  */
287 static int
288 twiddle_set(struct env_var *ev, int flags, const void *value)
289 {
290     u_long tdiv;
291     char * eptr;
292
293     tdiv = strtoul(value, &eptr, 0);
294     if (*(const char *)value == 0 || *eptr != 0) {
295         printf("invalid twiddle_divisor '%s'\n", (const char *)value);
296         return (CMD_ERROR);
297     }
298     twiddle_divisor((u_int)tdiv);
299     env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
300
301     return(CMD_OK);
302 }