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$");
30 #include <sys/param.h>
34 #include "bootstrap.h"
36 * Core console support
39 static int cons_set(struct env_var *ev, int flags, const void *value);
40 static int cons_find(const char *name);
41 static int cons_check(const char *string);
42 static int cons_change(const char *string);
43 static int twiddle_set(struct env_var *ev, int flags, const void *value);
45 #ifndef MODULE_VERBOSE
46 # define MODULE_VERBOSE MODULE_VERBOSE_TWIDDLE
48 int module_verbose = MODULE_VERBOSE;
51 module_verbose_set(struct env_var *ev, int flags, const void *value)
56 v = strtoul(value, &eptr, 0);
57 if (*(const char *)value == 0 || *eptr != 0) {
58 printf("invalid module_verbose '%s'\n", (const char *)value);
61 module_verbose = (int)v;
62 if (module_verbose < MODULE_VERBOSE_TWIDDLE) {
63 /* A hack for now; we do not want twiddling */
64 twiddle_divisor(UINT_MAX);
66 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
72 * Detect possible console(s) to use. If preferred console(s) have been
73 * specified, mark them as active. Else, mark the first probed console
74 * as active. Also create the console variable.
82 char module_verbose_buf[8];
86 /* We want a callback to install the new value when these vars change. */
87 snprintf(module_verbose_buf, sizeof(module_verbose_buf), "%d",
89 env_setenv("module_verbose", EV_VOLATILE, module_verbose_buf,
90 module_verbose_set, env_nounset);
91 env_setenv("twiddle_divisor", EV_VOLATILE, "16", twiddle_set,
94 /* Do all console probes */
95 for (cons = 0; consoles[cons] != NULL; cons++) {
96 consoles[cons]->c_flags = 0;
97 consoles[cons]->c_probe(consoles[cons]);
99 /* Now find the first working one */
101 for (cons = 0; consoles[cons] != NULL && active == -1; cons++) {
102 consoles[cons]->c_flags = 0;
103 consoles[cons]->c_probe(consoles[cons]);
104 if (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT))
107 /* Force a console even if all probes failed */
111 /* Check to see if a console preference has already been registered */
112 prefconsole = getenv("console");
113 if (prefconsole != NULL)
114 prefconsole = strdup(prefconsole);
115 if (prefconsole != NULL) {
116 unsetenv("console"); /* we want to replace this */
117 cons_change(prefconsole);
119 consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
120 consoles[active]->c_init(0);
121 prefconsole = strdup(consoles[active]->c_name);
124 printf("Consoles: ");
125 for (cons = 0; consoles[cons] != NULL; cons++)
126 if (consoles[cons]->c_flags & (C_ACTIVEIN | C_ACTIVEOUT))
127 printf("%s ", consoles[cons]->c_desc);
130 if (prefconsole != NULL) {
131 env_setenv("console", EV_VOLATILE, prefconsole, cons_set,
145 /* Loop forever polling all active consoles */
147 for (cons = 0; consoles[cons] != NULL; cons++) {
148 if ((consoles[cons]->c_flags &
149 (C_PRESENTIN | C_ACTIVEIN)) ==
150 (C_PRESENTIN | C_ACTIVEIN) &&
151 ((rv = consoles[cons]->c_in()) != -1))
162 for (cons = 0; consoles[cons] != NULL; cons++)
163 if ((consoles[cons]->c_flags & (C_PRESENTIN | C_ACTIVEIN)) ==
164 (C_PRESENTIN | C_ACTIVEIN) &&
165 (consoles[cons]->c_ready() != 0))
175 /* Expand newlines */
179 for (cons = 0; consoles[cons] != NULL; cons++) {
180 if ((consoles[cons]->c_flags & (C_PRESENTOUT | C_ACTIVEOUT)) ==
181 (C_PRESENTOUT | C_ACTIVEOUT))
182 consoles[cons]->c_out(c);
187 * Find the console with the specified name.
190 cons_find(const char *name)
194 for (cons = 0; consoles[cons] != NULL; cons++)
195 if (strcmp(consoles[cons]->c_name, name) == 0)
201 * Select one or more consoles.
204 cons_set(struct env_var *ev, int flags, const void *value)
208 if ((value == NULL) || (cons_check(value) == 0)) {
210 * Return CMD_OK instead of CMD_ERROR to prevent forth syntax
211 * error, which would prevent it processing any further
212 * loader.conf entries.
217 ret = cons_change(value);
221 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
226 * Check that at least one the consoles listed in *string is valid
229 cons_check(const char *string)
231 int cons, found, failed;
232 char *curpos, *dup, *next;
234 dup = next = strdup(string);
236 while (next != NULL) {
237 curpos = strsep(&next, " ,");
238 if (*curpos != '\0') {
239 cons = cons_find(curpos);
241 printf("console %s is invalid!\n", curpos);
252 printf("no valid consoles!\n");
254 if (found == 0 || failed != 0) {
255 printf("Available consoles:\n");
256 for (cons = 0; consoles[cons] != NULL; cons++)
257 printf(" %s\n", consoles[cons]->c_name);
264 * Activate all the valid consoles listed in *string and disable all others.
267 cons_change(const char *string)
270 char *curpos, *dup, *next;
272 /* Disable all consoles */
273 for (cons = 0; consoles[cons] != NULL; cons++) {
274 consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
277 /* Enable selected consoles */
278 dup = next = strdup(string);
280 while (next != NULL) {
281 curpos = strsep(&next, " ,");
284 cons = cons_find(curpos);
286 consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
287 consoles[cons]->c_init(0);
288 if ((consoles[cons]->c_flags &
289 (C_PRESENTIN | C_PRESENTOUT)) ==
290 (C_PRESENTIN | C_PRESENTOUT)) {
297 * If no consoles have initialised we
300 printf("console %s failed to initialize\n",
301 consoles[cons]->c_name);
310 * All requested consoles failed to initialise,
313 for (cons = 0; consoles[cons] != NULL; cons++) {
314 consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
315 consoles[cons]->c_init(0);
316 if ((consoles[cons]->c_flags &
317 (C_PRESENTIN | C_PRESENTOUT)) ==
318 (C_PRESENTIN | C_PRESENTOUT))
323 return (CMD_ERROR); /* Recovery failed. */
330 * Change the twiddle divisor.
332 * The user can set the twiddle_divisor variable to directly control how fast
333 * the progress twiddle spins, useful for folks with slow serial consoles. The
334 * code to monitor changes to the variable and propagate them to the twiddle
335 * routines has to live somewhere. Twiddling is console-related so it's here.
338 twiddle_set(struct env_var *ev, int flags, const void *value)
343 tdiv = strtoul(value, &eptr, 0);
344 if (*(const char *)value == 0 || *eptr != 0) {
345 printf("invalid twiddle_divisor '%s'\n", (const char *)value);
348 twiddle_divisor((u_int)tdiv);
349 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);