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.
58 /* We want a callback to install the new value when this var changes. */
59 env_setenv("twiddle_divisor", EV_VOLATILE, "16", twiddle_set,
62 /* Do all console probes */
63 for (cons = 0; consoles[cons] != NULL; cons++) {
64 consoles[cons]->c_flags = 0;
65 consoles[cons]->c_probe(consoles[cons]);
67 /* Now find the first working one */
69 for (cons = 0; consoles[cons] != NULL && active == -1; cons++) {
70 consoles[cons]->c_flags = 0;
71 consoles[cons]->c_probe(consoles[cons]);
72 if (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT))
75 /* Force a console even if all probes failed */
79 /* Check to see if a console preference has already been registered */
80 prefconsole = getenv("console");
81 if (prefconsole != NULL)
82 prefconsole = strdup(prefconsole);
83 if (prefconsole != NULL) {
84 unsetenv("console"); /* we want to replace this */
85 cons_change(prefconsole);
87 consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
88 consoles[active]->c_init(0);
89 prefconsole = strdup(consoles[active]->c_name);
93 for (cons = 0; consoles[cons] != NULL; cons++)
94 if (consoles[cons]->c_flags & (C_ACTIVEIN | C_ACTIVEOUT))
95 printf("%s ", consoles[cons]->c_desc);
98 if (prefconsole != NULL) {
99 env_setenv("console", EV_VOLATILE, prefconsole, cons_set,
113 /* Loop forever polling all active consoles */
115 for (cons = 0; consoles[cons] != NULL; cons++) {
116 if ((consoles[cons]->c_flags &
117 (C_PRESENTIN | C_ACTIVEIN)) ==
118 (C_PRESENTIN | C_ACTIVEIN) &&
119 ((rv = consoles[cons]->c_in()) != -1))
130 for (cons = 0; consoles[cons] != NULL; cons++)
131 if ((consoles[cons]->c_flags & (C_PRESENTIN | C_ACTIVEIN)) ==
132 (C_PRESENTIN | C_ACTIVEIN) &&
133 (consoles[cons]->c_ready() != 0))
143 /* Expand newlines */
147 for (cons = 0; consoles[cons] != NULL; cons++) {
148 if ((consoles[cons]->c_flags & (C_PRESENTOUT | C_ACTIVEOUT)) ==
149 (C_PRESENTOUT | C_ACTIVEOUT))
150 consoles[cons]->c_out(c);
155 * Find the console with the specified name.
158 cons_find(const char *name)
162 for (cons = 0; consoles[cons] != NULL; cons++)
163 if (strcmp(consoles[cons]->c_name, name) == 0)
169 * Select one or more consoles.
172 cons_set(struct env_var *ev, int flags, const void *value)
176 if ((value == NULL) || (cons_check(value) == 0)) {
178 * Return CMD_OK instead of CMD_ERROR to prevent forth syntax
179 * error, which would prevent it processing any further
180 * loader.conf entries.
185 ret = cons_change(value);
189 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
194 * Check that at least one the consoles listed in *string is valid
197 cons_check(const char *string)
199 int cons, found, failed;
200 char *curpos, *dup, *next;
202 dup = next = strdup(string);
204 while (next != NULL) {
205 curpos = strsep(&next, " ,");
206 if (*curpos != '\0') {
207 cons = cons_find(curpos);
209 printf("console %s is invalid!\n", curpos);
220 printf("no valid consoles!\n");
222 if (found == 0 || failed != 0) {
223 printf("Available consoles:\n");
224 for (cons = 0; consoles[cons] != NULL; cons++)
225 printf(" %s\n", consoles[cons]->c_name);
232 * Activate all the valid consoles listed in *string and disable all others.
235 cons_change(const char *string)
238 char *curpos, *dup, *next;
240 /* Disable all consoles */
241 for (cons = 0; consoles[cons] != NULL; cons++) {
242 consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
245 /* Enable selected consoles */
246 dup = next = strdup(string);
248 while (next != NULL) {
249 curpos = strsep(&next, " ,");
252 cons = cons_find(curpos);
254 consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
255 consoles[cons]->c_init(0);
256 if ((consoles[cons]->c_flags &
257 (C_PRESENTIN | C_PRESENTOUT)) ==
258 (C_PRESENTIN | C_PRESENTOUT)) {
265 * If no consoles have initialised we
268 printf("console %s failed to initialize\n",
269 consoles[cons]->c_name);
278 * All requested consoles failed to initialise,
281 for (cons = 0; consoles[cons] != NULL; cons++) {
282 consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
283 consoles[cons]->c_init(0);
284 if ((consoles[cons]->c_flags &
285 (C_PRESENTIN | C_PRESENTOUT)) ==
286 (C_PRESENTIN | C_PRESENTOUT))
291 return (CMD_ERROR); /* Recovery failed. */
298 * Change the twiddle divisor.
300 * The user can set the twiddle_divisor variable to directly control how fast
301 * the progress twiddle spins, useful for folks with slow serial consoles. The
302 * code to monitor changes to the variable and propagate them to the twiddle
303 * routines has to live somewhere. Twiddling is console-related so it's here.
306 twiddle_set(struct env_var *ev, int flags, const void *value)
311 tdiv = strtoul(value, &eptr, 0);
312 if (*(const char *)value == 0 || *eptr != 0) {
313 printf("invalid twiddle_divisor '%s'\n", (const char *)value);
316 twiddle_divisor((u_int)tdiv);
317 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);