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