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 #include <sys/param.h>
32 #include "bootstrap.h"
34 * Core console support
37 static int cons_set(struct env_var *ev, int flags, const void *value);
38 static int cons_find(const char *name);
39 static int cons_check(const char *string);
40 static int cons_change(const char *string);
41 static int twiddle_set(struct env_var *ev, int flags, const void *value);
43 #ifndef MODULE_VERBOSE
44 # define MODULE_VERBOSE MODULE_VERBOSE_TWIDDLE
46 int module_verbose = MODULE_VERBOSE;
49 module_verbose_set(struct env_var *ev, int flags, const void *value)
54 v = strtoul(value, &eptr, 0);
55 if (*(const char *)value == 0 || *eptr != 0) {
56 printf("invalid module_verbose '%s'\n", (const char *)value);
59 module_verbose = (int)v;
60 if (module_verbose < MODULE_VERBOSE_TWIDDLE) {
61 /* A hack for now; we do not want twiddling */
62 twiddle_divisor(UINT_MAX);
64 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
70 * Detect possible console(s) to use. If preferred console(s) have been
71 * specified, mark them as active. Else, mark the first probed console
72 * as active. Also create the console variable.
80 char module_verbose_buf[8];
84 /* We want a callback to install the new value when these vars change. */
85 snprintf(module_verbose_buf, sizeof(module_verbose_buf), "%d",
87 env_setenv("module_verbose", EV_VOLATILE, module_verbose_buf,
88 module_verbose_set, env_nounset);
89 env_setenv("twiddle_divisor", EV_VOLATILE, "16", twiddle_set,
92 /* Do all console probes */
93 for (cons = 0; consoles[cons] != NULL; cons++) {
94 consoles[cons]->c_flags = 0;
95 consoles[cons]->c_probe(consoles[cons]);
97 /* Now find the first working one */
99 for (cons = 0; consoles[cons] != NULL && active == -1; cons++) {
100 consoles[cons]->c_flags = 0;
101 consoles[cons]->c_probe(consoles[cons]);
102 if (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT))
105 /* Force a console even if all probes failed */
109 /* Check to see if a console preference has already been registered */
110 prefconsole = getenv("console");
111 if (prefconsole != NULL)
112 prefconsole = strdup(prefconsole);
113 if (prefconsole != NULL) {
114 unsetenv("console"); /* we want to replace this */
115 cons_change(prefconsole);
117 consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
118 consoles[active]->c_init(0);
119 prefconsole = strdup(consoles[active]->c_name);
122 printf("Consoles: ");
123 for (cons = 0; consoles[cons] != NULL; cons++)
124 if (consoles[cons]->c_flags & (C_ACTIVEIN | C_ACTIVEOUT))
125 printf("%s ", consoles[cons]->c_desc);
128 if (prefconsole != NULL) {
129 env_setenv("console", EV_VOLATILE, prefconsole, cons_set,
143 /* Loop forever polling all active consoles */
145 for (cons = 0; consoles[cons] != NULL; cons++) {
146 if ((consoles[cons]->c_flags &
147 (C_PRESENTIN | C_ACTIVEIN)) ==
148 (C_PRESENTIN | C_ACTIVEIN) &&
149 ((rv = consoles[cons]->c_in()) != -1))
160 for (cons = 0; consoles[cons] != NULL; cons++)
161 if ((consoles[cons]->c_flags & (C_PRESENTIN | C_ACTIVEIN)) ==
162 (C_PRESENTIN | C_ACTIVEIN) &&
163 (consoles[cons]->c_ready() != 0))
173 /* Expand newlines */
177 for (cons = 0; consoles[cons] != NULL; cons++) {
178 if ((consoles[cons]->c_flags & (C_PRESENTOUT | C_ACTIVEOUT)) ==
179 (C_PRESENTOUT | C_ACTIVEOUT))
180 consoles[cons]->c_out(c);
185 * Find the console with the specified name.
188 cons_find(const char *name)
192 for (cons = 0; consoles[cons] != NULL; cons++)
193 if (strcmp(consoles[cons]->c_name, name) == 0)
199 * Select one or more consoles.
202 cons_set(struct env_var *ev, int flags, const void *value)
206 if ((value == NULL) || (cons_check(value) == 0)) {
208 * Return CMD_OK instead of CMD_ERROR to prevent forth syntax
209 * error, which would prevent it processing any further
210 * loader.conf entries.
215 ret = cons_change(value);
219 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
224 * Check that at least one the consoles listed in *string is valid
227 cons_check(const char *string)
229 int cons, found, failed;
230 char *curpos, *dup, *next;
232 dup = next = strdup(string);
234 while (next != NULL) {
235 curpos = strsep(&next, " ,");
236 if (*curpos != '\0') {
237 cons = cons_find(curpos);
239 printf("console %s is unavailable\n", curpos);
250 printf("no valid consoles!\n");
252 if (found == 0 && failed != 0) {
253 printf("Available consoles:\n");
254 for (cons = 0; consoles[cons] != NULL; cons++)
255 printf(" %s\n", consoles[cons]->c_name);
262 * Activate all the valid consoles listed in *string and disable all others.
265 cons_change(const char *string)
268 char *curpos, *dup, *next;
270 /* Disable all consoles */
271 for (cons = 0; consoles[cons] != NULL; cons++) {
272 consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
275 /* Enable selected consoles */
276 dup = next = strdup(string);
278 while (next != NULL) {
279 curpos = strsep(&next, " ,");
282 cons = cons_find(curpos);
284 consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
285 consoles[cons]->c_init(0);
286 if ((consoles[cons]->c_flags &
287 (C_PRESENTIN | C_PRESENTOUT)) ==
288 (C_PRESENTIN | C_PRESENTOUT)) {
295 * If no consoles have initialised we
298 printf("console %s failed to initialize\n",
299 consoles[cons]->c_name);
308 * All requested consoles failed to initialise,
311 for (cons = 0; consoles[cons] != NULL; cons++) {
312 consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
313 consoles[cons]->c_init(0);
314 if ((consoles[cons]->c_flags &
315 (C_PRESENTIN | C_PRESENTOUT)) ==
316 (C_PRESENTIN | C_PRESENTOUT))
321 return (CMD_ERROR); /* Recovery failed. */
328 * Change the twiddle divisor.
330 * The user can set the twiddle_divisor variable to directly control how fast
331 * the progress twiddle spins, useful for folks with slow serial consoles. The
332 * code to monitor changes to the variable and propagate them to the twiddle
333 * routines has to live somewhere. Twiddling is console-related so it's here.
336 twiddle_set(struct env_var *ev, int flags, const void *value)
341 tdiv = strtoul(value, &eptr, 0);
342 if (*(const char *)value == 0 || *eptr != 0) {
343 printf("invalid twiddle_divisor '%s'\n", (const char *)value);
346 twiddle_divisor((u_int)tdiv);
347 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);