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,
60 /* Do all console probes */
61 for (cons = 0; consoles[cons] != NULL; cons++) {
62 consoles[cons]->c_flags = 0;
63 consoles[cons]->c_probe(consoles[cons]);
65 /* Now find the first working one */
67 for (cons = 0; consoles[cons] != NULL && active == -1; cons++) {
68 consoles[cons]->c_flags = 0;
69 consoles[cons]->c_probe(consoles[cons]);
70 if (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT))
73 /* Force a console even if all probes failed */
77 /* Check to see if a console preference has already been registered */
78 prefconsole = getenv("console");
79 if (prefconsole != NULL)
80 prefconsole = strdup(prefconsole);
81 if (prefconsole != NULL) {
82 unsetenv("console"); /* we want to replace this */
83 cons_change(prefconsole);
85 consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
86 consoles[active]->c_init(0);
87 prefconsole = strdup(consoles[active]->c_name);
91 for (cons = 0; consoles[cons] != NULL; cons++)
92 if (consoles[cons]->c_flags & (C_ACTIVEIN | C_ACTIVEOUT))
93 printf("%s ", consoles[cons]->c_desc);
96 if (prefconsole != NULL) {
97 env_setenv("console", EV_VOLATILE, prefconsole, cons_set,
109 /* Loop forever polling all active consoles */
111 for (cons = 0; consoles[cons] != NULL; cons++) {
112 if ((consoles[cons]->c_flags &
113 (C_PRESENTIN | C_ACTIVEIN)) ==
114 (C_PRESENTIN | C_ACTIVEIN) &&
115 ((rv = consoles[cons]->c_in()) != -1))
126 for (cons = 0; consoles[cons] != NULL; cons++)
127 if ((consoles[cons]->c_flags & (C_PRESENTIN | C_ACTIVEIN)) ==
128 (C_PRESENTIN | C_ACTIVEIN) &&
129 (consoles[cons]->c_ready() != 0))
139 /* Expand newlines */
143 for (cons = 0; consoles[cons] != NULL; cons++) {
144 if ((consoles[cons]->c_flags & (C_PRESENTOUT | C_ACTIVEOUT)) ==
145 (C_PRESENTOUT | C_ACTIVEOUT))
146 consoles[cons]->c_out(c);
151 * Find the console with the specified name.
154 cons_find(const char *name)
158 for (cons = 0; consoles[cons] != NULL; cons++)
159 if (strcmp(consoles[cons]->c_name, name) == 0)
165 * Select one or more consoles.
168 cons_set(struct env_var *ev, int flags, const void *value)
172 if ((value == NULL) || (cons_check(value) == 0)) {
174 * Return CMD_OK instead of CMD_ERROR to prevent forth syntax
175 * error, which would prevent it processing any further
176 * loader.conf entries.
181 ret = cons_change(value);
185 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
190 * Check that at least one the consoles listed in *string is valid
193 cons_check(const char *string)
195 int cons, found, failed;
196 char *curpos, *dup, *next;
198 dup = next = strdup(string);
200 while (next != NULL) {
201 curpos = strsep(&next, " ,");
202 if (*curpos != '\0') {
203 cons = cons_find(curpos);
205 printf("console %s is invalid!\n", curpos);
216 printf("no valid consoles!\n");
218 if (found == 0 || failed != 0) {
219 printf("Available consoles:\n");
220 for (cons = 0; consoles[cons] != NULL; cons++)
221 printf(" %s\n", consoles[cons]->c_name);
228 * Activate all the valid consoles listed in *string and disable all others.
231 cons_change(const char *string)
234 char *curpos, *dup, *next;
236 /* Disable all consoles */
237 for (cons = 0; consoles[cons] != NULL; cons++) {
238 consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
241 /* Enable selected consoles */
242 dup = next = strdup(string);
244 while (next != NULL) {
245 curpos = strsep(&next, " ,");
248 cons = cons_find(curpos);
250 consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
251 consoles[cons]->c_init(0);
252 if ((consoles[cons]->c_flags &
253 (C_PRESENTIN | C_PRESENTOUT)) ==
254 (C_PRESENTIN | C_PRESENTOUT)) {
261 * If no consoles have initialised we
264 printf("console %s failed to initialize\n",
265 consoles[cons]->c_name);
274 * All requested consoles failed to initialise,
277 for (cons = 0; consoles[cons] != NULL; cons++) {
278 consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
279 consoles[cons]->c_init(0);
280 if ((consoles[cons]->c_flags &
281 (C_PRESENTIN | C_PRESENTOUT)) ==
282 (C_PRESENTIN | C_PRESENTOUT))
287 return (CMD_ERROR); /* Recovery failed. */
294 * Change the twiddle divisor.
296 * The user can set the twiddle_divisor variable to directly control how fast
297 * the progress twiddle spins, useful for folks with slow serial consoles. The
298 * code to monitor changes to the variable and propagate them to the twiddle
299 * routines has to live somewhere. Twiddling is console-related so it's here.
302 twiddle_set(struct env_var *ev, int flags, const void *value)
307 tdiv = strtoul(value, &eptr, 0);
308 if (*(const char *)value == 0 || *eptr != 0) {
309 printf("invalid twiddle_divisor '%s'\n", (const char *)value);
312 twiddle_divisor((u_int)tdiv);
313 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);