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