xref: /illumos-gate/usr/src/boot/common/console.c (revision ae676b12)
1ffedf5deSToomas Soome /*
2199767f8SToomas Soome  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3199767f8SToomas Soome  * All rights reserved.
4199767f8SToomas Soome  *
5199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome  * modification, are permitted provided that the following conditions
7199767f8SToomas Soome  * are met:
8199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
13199767f8SToomas Soome  *
14199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767f8SToomas Soome  * SUCH DAMAGE.
25199767f8SToomas Soome  */
26199767f8SToomas Soome 
27199767f8SToomas Soome #include <sys/cdefs.h>
28199767f8SToomas Soome 
29199767f8SToomas Soome #include <stand.h>
30199767f8SToomas Soome #include <string.h>
31199767f8SToomas Soome 
32199767f8SToomas Soome #include "bootstrap.h"
33199767f8SToomas Soome /*
34199767f8SToomas Soome  * Core console support
35199767f8SToomas Soome  */
36199767f8SToomas Soome 
37199767f8SToomas Soome static int	cons_set(struct env_var *ev, int flags, const void *value);
38199767f8SToomas Soome static int	cons_find(const char *name);
39199767f8SToomas Soome static int	cons_check(const char *string);
40f3ba9b4eSToomas Soome static int	cons_change(const char *string, char **);
41199767f8SToomas Soome static int	twiddle_set(struct env_var *ev, int flags, const void *value);
42199767f8SToomas Soome 
43b72c8d00SToomas Soome static int	last_input = -1;	/* input device index */
44b72c8d00SToomas Soome 
45b72c8d00SToomas Soome /*
46b72c8d00SToomas Soome  * With multiple active console devices, return index of last input
47b72c8d00SToomas Soome  * device, so we can set up os_console variable to denote console
48b72c8d00SToomas Soome  * device for kernel.
49b72c8d00SToomas Soome  *
50b72c8d00SToomas Soome  * Please note, this feature can not really work with UEFI, because
51b72c8d00SToomas Soome  * efi console input is returned from any device listed in ConIn,
52b72c8d00SToomas Soome  * and we have no way to check which device from ConIn actually was
53b72c8d00SToomas Soome  * generating input.
54b72c8d00SToomas Soome  */
55b72c8d00SToomas Soome int
cons_inputdev(void)56b72c8d00SToomas Soome cons_inputdev(void)
57b72c8d00SToomas Soome {
58b72c8d00SToomas Soome 	int	cons;
59b72c8d00SToomas Soome 	int	flags = C_PRESENTIN | C_ACTIVEIN;
60b72c8d00SToomas Soome 	int	active = 0;
61b72c8d00SToomas Soome 
62b72c8d00SToomas Soome 	for (cons = 0; consoles[cons] != NULL; cons++)
63b72c8d00SToomas Soome 		if ((consoles[cons]->c_flags & flags) == flags)
64b72c8d00SToomas Soome 			active++;
65b72c8d00SToomas Soome 
66b72c8d00SToomas Soome 	/* With just one active console, we will not set os_console */
67b72c8d00SToomas Soome 	if (active == 1)
68b72c8d00SToomas Soome 		return (-1);
69b72c8d00SToomas Soome 
70b72c8d00SToomas Soome 	return (last_input);
71b72c8d00SToomas Soome }
72b72c8d00SToomas Soome 
73d3bd5503SToomas Soome /*
74d3bd5503SToomas Soome  * Return number of array slots.
75d3bd5503SToomas Soome  */
76d3bd5503SToomas Soome uint_t
cons_array_size(void)77d3bd5503SToomas Soome cons_array_size(void)
78d3bd5503SToomas Soome {
79d3bd5503SToomas Soome 	uint_t n;
80d3bd5503SToomas Soome 
81d3bd5503SToomas Soome 	if (consoles == NULL)
82d3bd5503SToomas Soome 		return (0);
83d3bd5503SToomas Soome 
84d3bd5503SToomas Soome 	for (n = 0; consoles[n] != NULL; n++)
85d3bd5503SToomas Soome 		;
86d3bd5503SToomas Soome 	return (n + 1);
87d3bd5503SToomas Soome }
88d3bd5503SToomas Soome 
89d3bd5503SToomas Soome static void
cons_add_dev(struct console * dev)90d3bd5503SToomas Soome cons_add_dev(struct console *dev)
91d3bd5503SToomas Soome {
92d3bd5503SToomas Soome 	uint_t c = cons_array_size();
93d3bd5503SToomas Soome 	uint_t n = 1;
94d3bd5503SToomas Soome 	struct console **tmp;
95d3bd5503SToomas Soome 
96d3bd5503SToomas Soome 	if (c == 0)
97d3bd5503SToomas Soome 		n++;
98d3bd5503SToomas Soome 	tmp = realloc(consoles, (c + n) * sizeof (struct console *));
99d3bd5503SToomas Soome 	if (tmp == NULL)
100d3bd5503SToomas Soome 		return;
101d3bd5503SToomas Soome 	if (c > 0)
102d3bd5503SToomas Soome 		c--;
103d3bd5503SToomas Soome 	consoles = tmp;
104d3bd5503SToomas Soome 	consoles[c] = dev;
105d3bd5503SToomas Soome 	consoles[c + 1] = NULL;
106d3bd5503SToomas Soome }
107d3bd5503SToomas Soome 
108199767f8SToomas Soome /*
109199767f8SToomas Soome  * Detect possible console(s) to use.  If preferred console(s) have been
110199767f8SToomas Soome  * specified, mark them as active. Else, mark the first probed console
111199767f8SToomas Soome  * as active.  Also create the console variable.
112199767f8SToomas Soome  */
113199767f8SToomas Soome void
cons_probe(void)114199767f8SToomas Soome cons_probe(void)
115199767f8SToomas Soome {
116cb0ea096SToomas Soome 	int	cons;
117cb0ea096SToomas Soome 	int	active;
118f3ba9b4eSToomas Soome 	char	*prefconsole, *list, *console;
119199767f8SToomas Soome 
120d3bd5503SToomas Soome 	/* Build list of consoles */
121d3bd5503SToomas Soome 	consoles = NULL;
122d3bd5503SToomas Soome 	for (cons = 0;; cons++) {
123d3bd5503SToomas Soome 		if (ct_list[cons].ct_dev != NULL) {
124d3bd5503SToomas Soome 			cons_add_dev(ct_list[cons].ct_dev);
125d3bd5503SToomas Soome 			continue;
126d3bd5503SToomas Soome 		}
127d3bd5503SToomas Soome 		if (ct_list[cons].ct_init != NULL) {
128d3bd5503SToomas Soome 			ct_list[cons].ct_init();
129d3bd5503SToomas Soome 			continue;
130d3bd5503SToomas Soome 		}
131d3bd5503SToomas Soome 		break;
132d3bd5503SToomas Soome 	}
133d3bd5503SToomas Soome 
134cb0ea096SToomas Soome 	/* We want a callback to install the new value when this var changes. */
135*ae676b12SColin Percival 	(void) env_setenv("twiddle_divisor", EV_VOLATILE, "16", twiddle_set,
136199767f8SToomas Soome 	    env_nounset);
137cb0ea096SToomas Soome 
138cb0ea096SToomas Soome 	/* Do all console probes */
139cb0ea096SToomas Soome 	for (cons = 0; consoles[cons] != NULL; cons++) {
140cb0ea096SToomas Soome 		consoles[cons]->c_flags = 0;
141cb0ea096SToomas Soome 		consoles[cons]->c_probe(consoles[cons]);
142cb0ea096SToomas Soome 	}
143cb0ea096SToomas Soome 	/* Now find the first working one */
144cb0ea096SToomas Soome 	active = -1;
145f3ba9b4eSToomas Soome 	for (cons = 0; consoles[cons] != NULL; cons++) {
146f3ba9b4eSToomas Soome 		if (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT)) {
147cb0ea096SToomas Soome 			active = cons;
148f3ba9b4eSToomas Soome 			break;
149f3ba9b4eSToomas Soome 		}
150cb0ea096SToomas Soome 	}
151f3ba9b4eSToomas Soome 
152cb0ea096SToomas Soome 	/* Force a console even if all probes failed */
153cb0ea096SToomas Soome 	if (active == -1)
154cb0ea096SToomas Soome 		active = 0;
155cb0ea096SToomas Soome 
156cb0ea096SToomas Soome 	/* Check to see if a console preference has already been registered */
157f3ba9b4eSToomas Soome 	list = NULL;
158cb0ea096SToomas Soome 	prefconsole = getenv("console");
159cb0ea096SToomas Soome 	if (prefconsole != NULL)
160cb0ea096SToomas Soome 		prefconsole = strdup(prefconsole);
161f3ba9b4eSToomas Soome 	if (prefconsole == NULL)
162cb0ea096SToomas Soome 		prefconsole = strdup(consoles[active]->c_name);
163f3ba9b4eSToomas Soome 
164f3ba9b4eSToomas Soome 	/*
165f3ba9b4eSToomas Soome 	 * unset "console", we need to create one with callbacks.
166f3ba9b4eSToomas Soome 	 */
167f3ba9b4eSToomas Soome 	unsetenv("console");
168f3ba9b4eSToomas Soome 	cons_change(prefconsole, &list);
169cb0ea096SToomas Soome 
170cb0ea096SToomas Soome 	printf("Consoles: ");
171cb0ea096SToomas Soome 	for (cons = 0; consoles[cons] != NULL; cons++)
172cb0ea096SToomas Soome 		if (consoles[cons]->c_flags & (C_ACTIVEIN | C_ACTIVEOUT))
173cb0ea096SToomas Soome 			printf("%s  ", consoles[cons]->c_desc);
174cb0ea096SToomas Soome 	printf("\n");
175cb0ea096SToomas Soome 
176f3ba9b4eSToomas Soome 	if (list != NULL)
177f3ba9b4eSToomas Soome 		console = list;
178f3ba9b4eSToomas Soome 	else
179f3ba9b4eSToomas Soome 		console = prefconsole;
180f3ba9b4eSToomas Soome 
181*ae676b12SColin Percival 	(void) env_setenv("console", EV_VOLATILE, console, cons_set,
182f3ba9b4eSToomas Soome 	    env_nounset);
183f3ba9b4eSToomas Soome 
184f3ba9b4eSToomas Soome 	free(prefconsole);
185f3ba9b4eSToomas Soome 	free(list);
186199767f8SToomas Soome }
187199767f8SToomas Soome 
188199767f8SToomas Soome void
cons_mode(int raw)189199767f8SToomas Soome cons_mode(int raw)
190199767f8SToomas Soome {
191cb0ea096SToomas Soome 	int	cons;
192cb0ea096SToomas Soome 
193cb0ea096SToomas Soome 	for (cons = 0; consoles[cons] != NULL; cons++) {
194cb0ea096SToomas Soome 		if (raw == 0)
195cb0ea096SToomas Soome 			consoles[cons]->c_flags &= ~C_MODERAW;
196cb0ea096SToomas Soome 		else
197cb0ea096SToomas Soome 			consoles[cons]->c_flags |= C_MODERAW;
198cb0ea096SToomas Soome 	}
199199767f8SToomas Soome }
200199767f8SToomas Soome 
201199767f8SToomas Soome int
getchar(void)202199767f8SToomas Soome getchar(void)
203199767f8SToomas Soome {
204ffedf5deSToomas Soome 	int	cons;
205ffedf5deSToomas Soome 	int	flags = C_PRESENTIN | C_ACTIVEIN;
206ffedf5deSToomas Soome 	int	rv;
207ffedf5deSToomas Soome 
208ffedf5deSToomas Soome 	/*
209ffedf5deSToomas Soome 	 * Loop forever polling all active consoles.  Somewhat strangely,
210ffedf5deSToomas Soome 	 * this code expects all ->c_in() implementations to effectively do an
211ffedf5deSToomas Soome 	 * ischar() check first, returning -1 if there's not a char ready.
212ffedf5deSToomas Soome 	 */
213cb0ea096SToomas Soome 	for (;;) {
214ffedf5deSToomas Soome 		for (cons = 0; consoles[cons] != NULL; cons++) {
215b72c8d00SToomas Soome 			if ((consoles[cons]->c_flags & flags) == flags) {
216b72c8d00SToomas Soome 				rv = consoles[cons]->c_in(consoles[cons]);
217b72c8d00SToomas Soome 				if (rv != -1) {
218b72c8d00SToomas Soome #ifndef EFI
219b72c8d00SToomas Soome 					last_input = cons;
220b72c8d00SToomas Soome #endif
221b72c8d00SToomas Soome 					return (rv);
222b72c8d00SToomas Soome 				}
223b72c8d00SToomas Soome 			}
224ffedf5deSToomas Soome 		}
225ffedf5deSToomas Soome 		delay(30 * 1000);	/* delay 30ms */
226ffedf5deSToomas Soome 	}
227199767f8SToomas Soome }
228199767f8SToomas Soome 
229199767f8SToomas Soome int
ischar(void)230199767f8SToomas Soome ischar(void)
231199767f8SToomas Soome {
232cb0ea096SToomas Soome 	int	cons;
233cb0ea096SToomas Soome 
234cb0ea096SToomas Soome 	for (cons = 0; consoles[cons] != NULL; cons++)
235cb0ea096SToomas Soome 		if ((consoles[cons]->c_flags & (C_PRESENTIN | C_ACTIVEIN)) ==
236cb0ea096SToomas Soome 		    (C_PRESENTIN | C_ACTIVEIN) &&
237cb0ea096SToomas Soome 		    (consoles[cons]->c_ready(consoles[cons]) != 0))
238cb0ea096SToomas Soome 			return (1);
239cb0ea096SToomas Soome 	return (0);
240199767f8SToomas Soome }
241199767f8SToomas Soome 
242199767f8SToomas Soome void
putchar(int c)243199767f8SToomas Soome putchar(int c)
244199767f8SToomas Soome {
245cb0ea096SToomas Soome 	int	cons;
246cb0ea096SToomas Soome 
247cb0ea096SToomas Soome 	/* Expand newlines if not in raw mode */
248cb0ea096SToomas Soome 	for (cons = 0; consoles[cons] != NULL; cons++)
249cb0ea096SToomas Soome 		if ((consoles[cons]->c_flags & (C_PRESENTOUT | C_ACTIVEOUT)) ==
250cb0ea096SToomas Soome 		    (C_PRESENTOUT | C_ACTIVEOUT)) {
251cb0ea096SToomas Soome 			if (c == '\n' &&
252cb0ea096SToomas Soome 			    (consoles[cons]->c_flags & C_MODERAW) == 0)
253cb0ea096SToomas Soome 				consoles[cons]->c_out(consoles[cons], '\r');
254cb0ea096SToomas Soome 			consoles[cons]->c_out(consoles[cons], c);
255cb0ea096SToomas Soome 		}
256199767f8SToomas Soome }
257199767f8SToomas Soome 
258199767f8SToomas Soome /*
259199767f8SToomas Soome  * Find the console with the specified name.
260199767f8SToomas Soome  */
261199767f8SToomas Soome static int
cons_find(const char * name)262199767f8SToomas Soome cons_find(const char *name)
263199767f8SToomas Soome {
264cb0ea096SToomas Soome 	int	cons;
265199767f8SToomas Soome 
266cb0ea096SToomas Soome 	for (cons = 0; consoles[cons] != NULL; cons++)
267cb0ea096SToomas Soome 		if (strcmp(consoles[cons]->c_name, name) == 0)
268cb0ea096SToomas Soome 			return (cons);
269cb0ea096SToomas Soome 	return (-1);
270199767f8SToomas Soome }
271199767f8SToomas Soome 
272199767f8SToomas Soome /*
273199767f8SToomas Soome  * Select one or more consoles.
274199767f8SToomas Soome  */
275199767f8SToomas Soome static int
cons_set(struct env_var * ev,int flags,const void * value)276199767f8SToomas Soome cons_set(struct env_var *ev, int flags, const void *value)
277199767f8SToomas Soome {
278f3ba9b4eSToomas Soome 	int	ret;
279f3ba9b4eSToomas Soome 	char	*list;
280cb0ea096SToomas Soome 
281cb0ea096SToomas Soome 	if ((value == NULL) || (cons_check(value) == 0)) {
282cb0ea096SToomas Soome 		/*
283cb0ea096SToomas Soome 		 * Return CMD_OK instead of CMD_ERROR to prevent forth syntax
284cb0ea096SToomas Soome 		 * error, which would prevent it processing any further
285cb0ea096SToomas Soome 		 * loader.conf entries.
286cb0ea096SToomas Soome 		 */
287cb0ea096SToomas Soome 		return (CMD_OK);
288cb0ea096SToomas Soome 	}
289199767f8SToomas Soome 
290f3ba9b4eSToomas Soome 	list = NULL;
291f3ba9b4eSToomas Soome 	ret = cons_change(value, &list);
292cb0ea096SToomas Soome 	if (ret != CMD_OK)
293cb0ea096SToomas Soome 		return (ret);
294199767f8SToomas Soome 
295560dcad7SToomas Soome 	/*
296560dcad7SToomas Soome 	 * set console variable.
297560dcad7SToomas Soome 	 */
298560dcad7SToomas Soome 	if (list != NULL) {
299560dcad7SToomas Soome 		(void) env_setenv(ev->ev_name, flags | EV_NOHOOK, list,
300560dcad7SToomas Soome 		    NULL, NULL);
301560dcad7SToomas Soome 	} else {
302560dcad7SToomas Soome 		(void) env_setenv(ev->ev_name, flags | EV_NOHOOK, value,
303560dcad7SToomas Soome 		    NULL, NULL);
304560dcad7SToomas Soome 	}
305560dcad7SToomas Soome 	free(list);
306f3ba9b4eSToomas Soome 	return (ret);
307199767f8SToomas Soome }
308199767f8SToomas Soome 
309199767f8SToomas Soome /*
310199767f8SToomas Soome  * Check that at least one the consoles listed in *string is valid
311199767f8SToomas Soome  */
312199767f8SToomas Soome static int
cons_check(const char * string)313199767f8SToomas Soome cons_check(const char *string)
314199767f8SToomas Soome {
315cb0ea096SToomas Soome 	int	cons, found, failed;
316cb0ea096SToomas Soome 	char	*curpos, *dup, *next;
317cb0ea096SToomas Soome 
318cb0ea096SToomas Soome 	dup = next = strdup(string);
319cb0ea096SToomas Soome 	found = failed = 0;
320cb0ea096SToomas Soome 	while (next != NULL) {
321cb0ea096SToomas Soome 		curpos = strsep(&next, " ,");
322cb0ea096SToomas Soome 		if (*curpos != '\0') {
323cb0ea096SToomas Soome 			cons = cons_find(curpos);
324cb0ea096SToomas Soome 			if (cons == -1) {
325cb0ea096SToomas Soome 				printf("console %s is invalid!\n", curpos);
326cb0ea096SToomas Soome 				failed++;
327cb0ea096SToomas Soome 			} else {
328f6de26e2SToomas Soome 				if ((consoles[cons]->c_flags &
329f6de26e2SToomas Soome 				    (C_PRESENTIN | C_PRESENTOUT)) !=
330f6de26e2SToomas Soome 				    (C_PRESENTIN | C_PRESENTOUT)) {
331f6de26e2SToomas Soome 					failed++;
332f6de26e2SToomas Soome 				} else
333f6de26e2SToomas Soome 					found++;
334cb0ea096SToomas Soome 			}
335cb0ea096SToomas Soome 		}
336199767f8SToomas Soome 	}
337199767f8SToomas Soome 
338cb0ea096SToomas Soome 	free(dup);
339199767f8SToomas Soome 
340cb0ea096SToomas Soome 	if (found == 0)
341cb0ea096SToomas Soome 		printf("no valid consoles!\n");
342199767f8SToomas Soome 
343cb0ea096SToomas Soome 	if (found == 0 || failed != 0) {
344cb0ea096SToomas Soome 		printf("Available consoles:\n");
34580e47917SToomas Soome 		for (cons = 0; consoles[cons] != NULL; cons++) {
34680e47917SToomas Soome 			printf("    %s", consoles[cons]->c_name);
34780e47917SToomas Soome 			if (consoles[cons]->c_devinfo != NULL)
34880e47917SToomas Soome 				consoles[cons]->c_devinfo(consoles[cons]);
34980e47917SToomas Soome 			printf("\n");
35080e47917SToomas Soome 		}
351cb0ea096SToomas Soome 	}
352199767f8SToomas Soome 
353cb0ea096SToomas Soome 	return (found);
354199767f8SToomas Soome }
355199767f8SToomas Soome 
356f3ba9b4eSToomas Soome /*
357f3ba9b4eSToomas Soome  * Helper function to build string with list of console names.
358f3ba9b4eSToomas Soome  */
359f3ba9b4eSToomas Soome static char *
cons_add_list(char * list,const char * value)360f3ba9b4eSToomas Soome cons_add_list(char *list, const char *value)
361f3ba9b4eSToomas Soome {
362f3ba9b4eSToomas Soome 	char *tmp;
363f3ba9b4eSToomas Soome 
364f3ba9b4eSToomas Soome 	if (list == NULL)
365f3ba9b4eSToomas Soome 		return (strdup(value));
366f3ba9b4eSToomas Soome 
367f3ba9b4eSToomas Soome 	if (asprintf(&tmp, "%s,%s", list, value) > 0) {
368f3ba9b4eSToomas Soome 		free(list);
369f3ba9b4eSToomas Soome 		list = tmp;
370f3ba9b4eSToomas Soome 	}
371f3ba9b4eSToomas Soome 	return (list);
372f3ba9b4eSToomas Soome }
37380e47917SToomas Soome 
374199767f8SToomas Soome /*
375f3ba9b4eSToomas Soome  * Activate all the valid consoles listed in string and disable all others.
376f3ba9b4eSToomas Soome  * Return comma separated string with list of activated console names.
377199767f8SToomas Soome  */
378199767f8SToomas Soome static int
cons_change(const char * string,char ** list)379f3ba9b4eSToomas Soome cons_change(const char *string, char **list)
380199767f8SToomas Soome {
381f3ba9b4eSToomas Soome 	int	cons, active, rv;
382cb0ea096SToomas Soome 	char	*curpos, *dup, *next;
383cb0ea096SToomas Soome 
384cb0ea096SToomas Soome 	/* Disable all consoles */
385cb0ea096SToomas Soome 	for (cons = 0; consoles[cons] != NULL; cons++) {
386cb0ea096SToomas Soome 		consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
387199767f8SToomas Soome 	}
388199767f8SToomas Soome 
389cb0ea096SToomas Soome 	/* Enable selected consoles */
390cb0ea096SToomas Soome 	dup = next = strdup(string);
391cb0ea096SToomas Soome 	active = 0;
392f3ba9b4eSToomas Soome 	*list = NULL;
393f3ba9b4eSToomas Soome 	rv = CMD_OK;
394cb0ea096SToomas Soome 	while (next != NULL) {
395cb0ea096SToomas Soome 		curpos = strsep(&next, " ,");
396cb0ea096SToomas Soome 		if (*curpos == '\0')
397cb0ea096SToomas Soome 			continue;
398cb0ea096SToomas Soome 		cons = cons_find(curpos);
399cb0ea096SToomas Soome 		if (cons >= 0) {
400cb0ea096SToomas Soome 			consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
401cb0ea096SToomas Soome 			consoles[cons]->c_init(consoles[cons], 0);
402cb0ea096SToomas Soome 			if ((consoles[cons]->c_flags &
403f6de26e2SToomas Soome 			    (C_ACTIVEIN | C_ACTIVEOUT)) ==
404f6de26e2SToomas Soome 			    (C_ACTIVEIN | C_ACTIVEOUT)) {
405cb0ea096SToomas Soome 				active++;
406f3ba9b4eSToomas Soome 				*list = cons_add_list(*list, curpos);
407cb0ea096SToomas Soome 				continue;
408cb0ea096SToomas Soome 			}
409cb0ea096SToomas Soome 
410cb0ea096SToomas Soome 			if (active != 0) {
411cb0ea096SToomas Soome 				/*
412cb0ea096SToomas Soome 				 * If no consoles have initialised we wouldn't
413cb0ea096SToomas Soome 				 * see this.
414cb0ea096SToomas Soome 				 */
415cb0ea096SToomas Soome 				printf("console %s failed to initialize\n",
416cb0ea096SToomas Soome 				    consoles[cons]->c_name);
417cb0ea096SToomas Soome 			}
418cb0ea096SToomas Soome 		}
419cb0ea096SToomas Soome 	}
420199767f8SToomas Soome 
421cb0ea096SToomas Soome 	free(dup);
422cb0ea096SToomas Soome 
423cb0ea096SToomas Soome 	if (active == 0) {
424cb0ea096SToomas Soome 		/*
425cb0ea096SToomas Soome 		 * All requested consoles failed to initialise, try to recover.
426cb0ea096SToomas Soome 		 */
427cb0ea096SToomas Soome 		for (cons = 0; consoles[cons] != NULL; cons++) {
428cb0ea096SToomas Soome 			consoles[cons]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
429cb0ea096SToomas Soome 			consoles[cons]->c_init(consoles[cons], 0);
430cb0ea096SToomas Soome 			if ((consoles[cons]->c_flags &
431f6de26e2SToomas Soome 			    (C_ACTIVEIN | C_ACTIVEOUT)) ==
432f3ba9b4eSToomas Soome 			    (C_ACTIVEIN | C_ACTIVEOUT)) {
433cb0ea096SToomas Soome 				active++;
434f3ba9b4eSToomas Soome 				*list = cons_add_list(*list,
435f3ba9b4eSToomas Soome 				    consoles[cons]->c_name);
436f3ba9b4eSToomas Soome 			}
4379c3cf8b3SToomas Soome 		}
438199767f8SToomas Soome 
4399c3cf8b3SToomas Soome 		if (active == 0)
440f3ba9b4eSToomas Soome 			rv = CMD_ERROR; /* Recovery failed. */
441cb0ea096SToomas Soome 	}
442199767f8SToomas Soome 
443f3ba9b4eSToomas Soome 	return (rv);
444199767f8SToomas Soome }
445199767f8SToomas Soome 
446199767f8SToomas Soome /*
447199767f8SToomas Soome  * Change the twiddle divisor.
448199767f8SToomas Soome  *
449199767f8SToomas Soome  * The user can set the twiddle_divisor variable to directly control how fast
450199767f8SToomas Soome  * the progress twiddle spins, useful for folks with slow serial consoles.  The
451199767f8SToomas Soome  * code to monitor changes to the variable and propagate them to the twiddle
452199767f8SToomas Soome  * routines has to live somewhere.  Twiddling is console-related so it's here.
453199767f8SToomas Soome  */
454199767f8SToomas Soome static int
twiddle_set(struct env_var * ev,int flags,const void * value)455199767f8SToomas Soome twiddle_set(struct env_var *ev, int flags, const void *value)
456199767f8SToomas Soome {
457cb0ea096SToomas Soome 	ulong_t tdiv;
458cb0ea096SToomas Soome 	char *eptr;
459cb0ea096SToomas Soome 
460cb0ea096SToomas Soome 	tdiv = strtoul(value, &eptr, 0);
461cb0ea096SToomas Soome 	if (*(const char *)value == 0 || *eptr != 0) {
462cb0ea096SToomas Soome 		printf("invalid twiddle_divisor '%s'\n", (const char *)value);
463cb0ea096SToomas Soome 		return (CMD_ERROR);
464cb0ea096SToomas Soome 	}
465cb0ea096SToomas Soome 	twiddle_divisor((uint_t)tdiv);
466*ae676b12SColin Percival 	(void) env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
467cb0ea096SToomas Soome 
468cb0ea096SToomas Soome 	return (CMD_OK);
469199767f8SToomas Soome }
47080e47917SToomas Soome 
47180e47917SToomas Soome COMMAND_SET(console, "console", "console info", command_console);
47280e47917SToomas Soome 
47380e47917SToomas Soome static int
command_console(int argc,char * argv[])47480e47917SToomas Soome command_console(int argc, char *argv[])
47580e47917SToomas Soome {
47680e47917SToomas Soome 	if (argc > 1)
47780e47917SToomas Soome 		printf("%s: list info about available consoles\n", argv[0]);
47880e47917SToomas Soome 
47980e47917SToomas Soome 	printf("Current console: %s\n", getenv("console"));
48080e47917SToomas Soome 	printf("Available consoles:\n");
48180e47917SToomas Soome 	for (int cons = 0; consoles[cons] != NULL; cons++) {
48280e47917SToomas Soome 		printf("    %s", consoles[cons]->c_name);
48380e47917SToomas Soome 		if (consoles[cons]->c_devinfo != NULL)
48480e47917SToomas Soome 			consoles[cons]->c_devinfo(consoles[cons]);
48580e47917SToomas Soome 		printf("\n");
48680e47917SToomas Soome 	}
48780e47917SToomas Soome 
48880e47917SToomas Soome 	return (CMD_OK);
48980e47917SToomas Soome }
490