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