xref: /netbsd-src/sys/arch/ia64/stand/common/console.c (revision 93b81f4c81fbf76a2df731f1f7732195957c4734)
1*93b81f4cSkiyohara /*	$NetBSD: console.c,v 1.3 2009/07/20 04:59:03 kiyohara Exp $	*/
2ba7cbe76Scherry 
3ba7cbe76Scherry /*-
4ba7cbe76Scherry  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5ba7cbe76Scherry  * All rights reserved.
6ba7cbe76Scherry  *
7ba7cbe76Scherry  * Redistribution and use in source and binary forms, with or without
8ba7cbe76Scherry  * modification, are permitted provided that the following conditions
9ba7cbe76Scherry  * are met:
10ba7cbe76Scherry  * 1. Redistributions of source code must retain the above copyright
11ba7cbe76Scherry  *    notice, this list of conditions and the following disclaimer.
12ba7cbe76Scherry  * 2. Redistributions in binary form must reproduce the above copyright
13ba7cbe76Scherry  *    notice, this list of conditions and the following disclaimer in the
14ba7cbe76Scherry  *    documentation and/or other materials provided with the distribution.
15ba7cbe76Scherry  *
16ba7cbe76Scherry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ba7cbe76Scherry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ba7cbe76Scherry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ba7cbe76Scherry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20ba7cbe76Scherry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ba7cbe76Scherry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ba7cbe76Scherry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ba7cbe76Scherry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ba7cbe76Scherry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ba7cbe76Scherry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ba7cbe76Scherry  * SUCH DAMAGE.
27ba7cbe76Scherry  */
28ba7cbe76Scherry 
29ba7cbe76Scherry #include <sys/cdefs.h>
3040d4f67eScherry /* __FBSDID("$FreeBSD: src/sys/boot/common/console.c,v 1.6 2003/08/25 23:30:41 obrien Exp $"); */
31ba7cbe76Scherry 
32ba7cbe76Scherry #include <lib/libsa/stand.h>
33*93b81f4cSkiyohara #include <lib/libsa/loadfile.h>
34ba7cbe76Scherry #include <lib/libkern/libkern.h>
35ba7cbe76Scherry 
36ba7cbe76Scherry #include "bootstrap.h"
37ba7cbe76Scherry /*
38ba7cbe76Scherry  * Core console support
39ba7cbe76Scherry  */
40ba7cbe76Scherry 
41ba7cbe76Scherry static int	cons_set(struct env_var *ev, int flags, void *value);
42ba7cbe76Scherry static int	cons_find(char *name);
43ba7cbe76Scherry 
44ba7cbe76Scherry /*
45ba7cbe76Scherry  * Detect possible console(s) to use.  The first probed console
46ba7cbe76Scherry  * is marked active.  Also create the console variable.
47ba7cbe76Scherry  *
48ba7cbe76Scherry  * XXX Add logic for multiple console support.
49ba7cbe76Scherry  */
50ba7cbe76Scherry void
cons_probe(void)51ba7cbe76Scherry cons_probe(void)
52ba7cbe76Scherry {
53ba7cbe76Scherry     int			cons;
54ba7cbe76Scherry     int			active;
55ba7cbe76Scherry     char		*prefconsole;
56ba7cbe76Scherry 
57ba7cbe76Scherry     /* Do all console probes */
58ba7cbe76Scherry     for (cons = 0; consoles[cons] != NULL; cons++) {
59ba7cbe76Scherry 	consoles[cons]->c_flags = 0;
60ba7cbe76Scherry  	consoles[cons]->c_probe(consoles[cons]);
61ba7cbe76Scherry     }
62ba7cbe76Scherry     /* Now find the first working one */
63ba7cbe76Scherry     active = -1;
64ba7cbe76Scherry     for (cons = 0; consoles[cons] != NULL && active == -1; cons++) {
65ba7cbe76Scherry 	consoles[cons]->c_flags = 0;
66ba7cbe76Scherry  	consoles[cons]->c_probe(consoles[cons]);
67ba7cbe76Scherry 	if (consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT))
68ba7cbe76Scherry 	    active = cons;
69ba7cbe76Scherry     }
70ba7cbe76Scherry 
71ba7cbe76Scherry     /* Check to see if a console preference has already been registered */
72ba7cbe76Scherry     prefconsole = getenv("console");
73ba7cbe76Scherry     if (prefconsole != NULL)
74ba7cbe76Scherry 	prefconsole = strdup(prefconsole);
75ba7cbe76Scherry     if (prefconsole != NULL) {
76ba7cbe76Scherry 	unsetenv("console");		/* we want to replace this */
77ba7cbe76Scherry 	for (cons = 0; consoles[cons] != NULL; cons++)
78ba7cbe76Scherry 	    /* look for the nominated console, use it if it's functional */
79ba7cbe76Scherry 	    if (!strcmp(prefconsole, consoles[cons]->c_name) &&
80ba7cbe76Scherry 		(consoles[cons]->c_flags == (C_PRESENTIN | C_PRESENTOUT)))
81ba7cbe76Scherry 		active = cons;
82ba7cbe76Scherry 	free(prefconsole);
83ba7cbe76Scherry     }
84ba7cbe76Scherry     if (active == -1)
85ba7cbe76Scherry 	active = 0;
86ba7cbe76Scherry     consoles[active]->c_flags |= (C_ACTIVEIN | C_ACTIVEOUT);
87ba7cbe76Scherry     consoles[active]->c_init(0);
88ba7cbe76Scherry 
89ba7cbe76Scherry     printf("Console: %s\n", consoles[active]->c_desc);
90ba7cbe76Scherry     env_setenv("console", EV_VOLATILE, consoles[active]->c_name, (ev_sethook_t *) cons_set,
91ba7cbe76Scherry 	env_nounset);
92ba7cbe76Scherry }
93ba7cbe76Scherry 
94ba7cbe76Scherry int
getchar(void)95ba7cbe76Scherry getchar(void)
96ba7cbe76Scherry {
97ba7cbe76Scherry     int		cons;
98ba7cbe76Scherry     int		rv;
99ba7cbe76Scherry 
100ba7cbe76Scherry     /* Loop forever polling all active consoles */
101ba7cbe76Scherry     for(;;)
102ba7cbe76Scherry 	for (cons = 0; consoles[cons] != NULL; cons++)
103ba7cbe76Scherry 	    if ((consoles[cons]->c_flags & C_ACTIVEIN) &&
104ba7cbe76Scherry 		((rv = consoles[cons]->c_in()) != -1))
105ba7cbe76Scherry 		return(rv);
106ba7cbe76Scherry }
107ba7cbe76Scherry 
108ba7cbe76Scherry int
ischar(void)109ba7cbe76Scherry ischar(void)
110ba7cbe76Scherry {
111ba7cbe76Scherry     int		cons;
112ba7cbe76Scherry 
113ba7cbe76Scherry     for (cons = 0; consoles[cons] != NULL; cons++)
114ba7cbe76Scherry 	if ((consoles[cons]->c_flags & C_ACTIVEIN) &&
115ba7cbe76Scherry 	    (consoles[cons]->c_ready() != 0))
116ba7cbe76Scherry 		return(1);
117ba7cbe76Scherry     return(0);
118ba7cbe76Scherry }
119ba7cbe76Scherry 
120ba7cbe76Scherry void
putchar(int c)121ba7cbe76Scherry putchar(int c)
122ba7cbe76Scherry {
123ba7cbe76Scherry     int		cons;
124ba7cbe76Scherry 
125ba7cbe76Scherry     /* Expand newlines */
126ba7cbe76Scherry     if (c == '\n')
127ba7cbe76Scherry 	putchar('\r');
128ba7cbe76Scherry 
129ba7cbe76Scherry     for (cons = 0; consoles[cons] != NULL; cons++)
130ba7cbe76Scherry 	if (consoles[cons]->c_flags & C_ACTIVEOUT)
131ba7cbe76Scherry 	    consoles[cons]->c_out(c);
132ba7cbe76Scherry }
133ba7cbe76Scherry 
134ba7cbe76Scherry static int
cons_find(char * name)135ba7cbe76Scherry cons_find(char *name)
136ba7cbe76Scherry {
137ba7cbe76Scherry     int		cons;
138ba7cbe76Scherry 
139ba7cbe76Scherry     for (cons = 0; consoles[cons] != NULL; cons++)
140ba7cbe76Scherry 	if (!strcmp(consoles[cons]->c_name, name))
141ba7cbe76Scherry 	    return(cons);
142ba7cbe76Scherry     return(-1);
143ba7cbe76Scherry }
144ba7cbe76Scherry 
145ba7cbe76Scherry 
146ba7cbe76Scherry /*
147ba7cbe76Scherry  * Select a console.
148ba7cbe76Scherry  *
149ba7cbe76Scherry  * XXX Note that the console system design allows for some extension
150ba7cbe76Scherry  *     here (eg. multiple consoles, input/output only, etc.)
151ba7cbe76Scherry  */
152ba7cbe76Scherry static int
cons_set(struct env_var * ev,int flags,void * value)153ba7cbe76Scherry cons_set(struct env_var *ev, int flags, void *value)
154ba7cbe76Scherry {
155ba7cbe76Scherry     int		cons, active;
156ba7cbe76Scherry 
157ba7cbe76Scherry     if ((value == NULL) || ((active = cons_find(value)) == -1)) {
158ba7cbe76Scherry 	if (value != NULL)
159ba7cbe76Scherry 	    printf("no such console '%s'\n", (char *)value);
160ba7cbe76Scherry 	printf("Available consoles:\n");
161ba7cbe76Scherry 	for (cons = 0; consoles[cons] != NULL; cons++)
162ba7cbe76Scherry 	    printf("    %s\n", consoles[cons]->c_name);
163ba7cbe76Scherry 	return(CMD_ERROR);
164ba7cbe76Scherry     }
165ba7cbe76Scherry 
166ba7cbe76Scherry     /* disable all current consoles */
167ba7cbe76Scherry     for (cons = 0; consoles[cons] != NULL; cons++)
168ba7cbe76Scherry 	consoles[cons]->c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
169ba7cbe76Scherry 
170ba7cbe76Scherry     /* enable selected console */
171ba7cbe76Scherry     consoles[active]->c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
172ba7cbe76Scherry     consoles[active]->c_init(0);
173ba7cbe76Scherry 
174ba7cbe76Scherry     env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
175ba7cbe76Scherry     return(CMD_OK);
176ba7cbe76Scherry }
177