Home | History | Annotate | Line # | Download | only in common
prom.c revision 1.7
      1 /* $NetBSD: prom.c,v 1.7 1999/03/31 03:22:57 cgd Exp $ */
      2 
      3 /*
      4  * Mach Operating System
      5  * Copyright (c) 1992 Carnegie Mellon University
      6  * All Rights Reserved.
      7  *
      8  * Permission to use, copy, modify and distribute this software and its
      9  * documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie Mellon
     26  * the rights to redistribute these changes.
     27  */
     28 
     29 #include <sys/types.h>
     30 
     31 #include <machine/prom.h>
     32 #include <machine/rpb.h>
     33 
     34 #include "common.h"
     35 
     36 int console;
     37 
     38 static int test_getchar(int *);
     39 static void putonechar(int c);
     40 
     41 void
     42 init_prom_calls()
     43 {
     44 	extern struct prom_vec prom_dispatch_v;
     45 	struct rpb *r;
     46 	struct crb *c;
     47 	char buf[4];
     48 
     49 	r = (struct rpb *)HWRPB_ADDR;
     50 	c = (struct crb *)((u_int8_t *)r + r->rpb_crb_off);
     51 
     52 	prom_dispatch_v.routine_arg = c->crb_v_dispatch;
     53 	prom_dispatch_v.routine = c->crb_v_dispatch->entry_va;
     54 
     55 	/* Look for console tty. */
     56 	prom_getenv(PROM_E_TTY_DEV, buf, 4);
     57 	console = buf[0] - '0';
     58 }
     59 
     60 static int
     61 test_getchar(xc)
     62 	int *xc;
     63 {
     64 	prom_return_t ret;
     65 
     66 	ret.bits = prom_dispatch(PROM_R_GETC, console);
     67 	*xc = ret.u.retval;
     68 	return ret.u.status == 0 || ret.u.status == 1;
     69 }
     70 
     71 int
     72 getchar()
     73 {
     74 	int c;
     75 
     76 	for (;;) {
     77 		if (test_getchar(&c)) {
     78 			if (c == 3)
     79 				halt();
     80 			return c;
     81 		}
     82 	}
     83 }
     84 
     85 static void
     86 putonechar(c)
     87 	int c;
     88 {
     89 	prom_return_t ret;
     90 	char cbuf = c;
     91 
     92 	do {
     93 		ret.bits = prom_dispatch(PROM_R_PUTS, console, &cbuf, 1);
     94 	} while ((ret.u.retval & 1) == 0);
     95 }
     96 
     97 void
     98 putchar(c)
     99 	int c;
    100 {
    101 	int typed_c;
    102 
    103 	if (c == '\r' || c == '\n') {
    104 		putonechar('\r');
    105 		c = '\n';
    106 	}
    107 	putonechar(c);
    108 	if (test_getchar(&typed_c))
    109 		if (typed_c == 3)
    110 			halt();
    111 }
    112 
    113 int
    114 prom_getenv(id, buf, len)
    115 	int id, len;
    116 	char *buf;
    117 {
    118 	prom_return_t ret;
    119 
    120 	ret.bits = prom_dispatch(PROM_R_GETENV, id, buf, len-1);
    121 	if (ret.u.status & 0x4)
    122 		ret.u.retval = 0;
    123 	buf[ret.u.retval] = '\0';
    124 
    125 	return (ret.u.retval);
    126 }
    127