2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 #include "bootstrap.h"
35 * Core console support
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);
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.
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);
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]);
64 /* Now find the first working one */
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))
72 /* Force a console even if all probes failed */
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);
84 consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
85 consoles[active]->c_init(0);
86 prefconsole = strdup(consoles[active]->c_name);
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);
95 if (prefconsole != NULL) {
96 env_setenv("console", EV_VOLATILE, prefconsole, cons_set,
108 /* Loop forever polling all active consoles */
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))
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))
135 /* Expand newlines */
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);
146 * Find the console with the specified name.
149 cons_find(const char *name)
153 for (cons = 0; consoles[cons] != NULL; cons++)
154 if (!strcmp(consoles[cons]->c_name, name))
160 * Select one or more consoles.
163 cons_set(struct env_var *ev, int flags, const void *value)
167 if ((value == NULL) || (cons_check(value) == 0)) {
169 * Return CMD_OK instead of CMD_ERROR to prevent forth syntax error,
170 * which would prevent it processing any further loader.conf entries.
175 ret = cons_change(value);
179 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
184 * Check that at least one the consoles listed in *string is valid
187 cons_check(const char *string)
189 int cons, found, failed;
190 char *curpos, *dup, *next;
192 dup = next = strdup(string);
194 while (next != NULL) {
195 curpos = strsep(&next, " ,");
196 if (*curpos != '\0') {
197 cons = cons_find(curpos);
199 printf("console %s is invalid!\n", curpos);
210 printf("no valid consoles!\n");
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);
222 * Activate all the valid consoles listed in *string and disable all others.
225 cons_change(const char *string)
228 char *curpos, *dup, *next;
230 /* Disable all consoles */
231 for (cons = 0; consoles[cons] != NULL; cons++) {
232 consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
235 /* Enable selected consoles */
236 dup = next = strdup(string);
238 while (next != NULL) {
239 curpos = strsep(&next, " ,");
242 cons = cons_find(curpos);
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)) {
253 /* If no consoles have initialised we wouldn't see this. */
254 printf("console %s failed to initialize\n", consoles[cons]->c_name);
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))
273 return (CMD_ERROR); /* Recovery failed. */
280 * Change the twiddle divisor.
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.
288 twiddle_set(struct env_var *ev, int flags, const void *value)
293 tdiv = strtoul(value, &eptr, 0);
294 if (*(const char *)value == 0 || *eptr != 0) {
295 printf("invalid twiddle_divisor '%s'\n", (const char *)value);
298 twiddle_divisor((u_int)tdiv);
299 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);