Home | History | Annotate | Line # | Download | only in ofwboot
promlib.c revision 1.1
      1  1.1  cdi /*	$NetBSD: promlib.c,v 1.1 2006/01/27 18:31:12 cdi Exp $ */
      2  1.1  cdi 
      3  1.1  cdi /*-
      4  1.1  cdi  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  1.1  cdi  * All rights reserved.
      6  1.1  cdi  *
      7  1.1  cdi  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  cdi  * by Paul Kranenburg.
      9  1.1  cdi  *
     10  1.1  cdi  * Redistribution and use in source and binary forms, with or without
     11  1.1  cdi  * modification, are permitted provided that the following conditions
     12  1.1  cdi  * are met:
     13  1.1  cdi  * 1. Redistributions of source code must retain the above copyright
     14  1.1  cdi  *    notice, this list of conditions and the following disclaimer.
     15  1.1  cdi  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  cdi  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  cdi  *    documentation and/or other materials provided with the distribution.
     18  1.1  cdi  * 3. All advertising materials mentioning features or use of this software
     19  1.1  cdi  *    must display the following acknowledgement:
     20  1.1  cdi  *        This product includes software developed by the NetBSD
     21  1.1  cdi  *        Foundation, Inc. and its contributors.
     22  1.1  cdi  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  cdi  *    contributors may be used to endorse or promote products derived
     24  1.1  cdi  *    from this software without specific prior written permission.
     25  1.1  cdi  *
     26  1.1  cdi  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  cdi  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  cdi  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  cdi  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  cdi  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  cdi  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  cdi  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  cdi  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  cdi  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  cdi  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  cdi  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  cdi  */
     38  1.1  cdi 
     39  1.1  cdi /*
     40  1.1  cdi  * OPENPROM functions.  These are here mainly to hide the OPENPROM interface
     41  1.1  cdi  * from the rest of the kernel.
     42  1.1  cdi  */
     43  1.1  cdi 
     44  1.1  cdi #include <sys/types.h>
     45  1.1  cdi #include <machine/promlib.h>
     46  1.1  cdi 
     47  1.1  cdi #include "openfirm.h"
     48  1.1  cdi 
     49  1.1  cdi 
     50  1.1  cdi void *romp;
     51  1.1  cdi struct promops	promops;
     52  1.1  cdi 
     53  1.1  cdi 
     54  1.1  cdi static void
     55  1.1  cdi openfirmware_fatal(void)
     56  1.1  cdi {
     57  1.1  cdi 	printf("Invalid Openfirmware environment\n");
     58  1.1  cdi 	exit(0);
     59  1.1  cdi }
     60  1.1  cdi 
     61  1.1  cdi static int
     62  1.1  cdi openfirmware_chosen(void)
     63  1.1  cdi {
     64  1.1  cdi 	static int phandle = -1;
     65  1.1  cdi 
     66  1.1  cdi 	if (phandle == -1) {
     67  1.1  cdi 		if ( (phandle = OF_finddevice("/chosen")) == -1) {
     68  1.1  cdi 			exit(0);
     69  1.1  cdi 		}
     70  1.1  cdi 	}
     71  1.1  cdi 
     72  1.1  cdi 	return (phandle);
     73  1.1  cdi }
     74  1.1  cdi 
     75  1.1  cdi static const char*
     76  1.1  cdi openfirmware_bootpath(void)
     77  1.1  cdi {
     78  1.1  cdi 	static char bootpath[PROM_MAX_PATH];
     79  1.1  cdi 
     80  1.1  cdi 	if (_prom_getprop(openfirmware_chosen(), "bootpath", bootpath,
     81  1.1  cdi 				sizeof(bootpath)) < 0) {
     82  1.1  cdi 		openfirmware_fatal();
     83  1.1  cdi 	}
     84  1.1  cdi 
     85  1.1  cdi 	return bootpath;
     86  1.1  cdi }
     87  1.1  cdi 
     88  1.1  cdi static const char*
     89  1.1  cdi openfirmware_bootfile(void)
     90  1.1  cdi {
     91  1.1  cdi 	/* Default image name */
     92  1.1  cdi 	return "netbsd";
     93  1.1  cdi }
     94  1.1  cdi 
     95  1.1  cdi static const char*
     96  1.1  cdi openfirmware_bootargs(void)
     97  1.1  cdi {
     98  1.1  cdi 	static char bootargs[PROM_MAX_PATH * 2];
     99  1.1  cdi 
    100  1.1  cdi 	if (_prom_getprop(openfirmware_chosen(), "bootargs", bootargs,
    101  1.1  cdi 				sizeof(bootargs)) < 0) {
    102  1.1  cdi 		openfirmware_fatal();
    103  1.1  cdi 	}
    104  1.1  cdi 
    105  1.1  cdi 	return bootargs;
    106  1.1  cdi }
    107  1.1  cdi 
    108  1.1  cdi static int
    109  1.1  cdi openfirmware_getchar(void)
    110  1.1  cdi {
    111  1.1  cdi 	unsigned char ch = '\0';
    112  1.1  cdi 	int l;
    113  1.1  cdi 
    114  1.1  cdi 	while ((l = OF_read(prom_stdin(), &ch, 1)) != 1)
    115  1.1  cdi 		if (l != -2 && l != 0)
    116  1.1  cdi 			return -1;
    117  1.1  cdi 	return ch;
    118  1.1  cdi }
    119  1.1  cdi 
    120  1.1  cdi static void
    121  1.1  cdi openfirmware_putchar(int c)
    122  1.1  cdi {
    123  1.1  cdi 	char ch = c;
    124  1.1  cdi 
    125  1.1  cdi 	if (c == '\n')
    126  1.1  cdi 		putchar('\r');
    127  1.1  cdi 	OF_write(prom_stdout(), &ch, 1);
    128  1.1  cdi }
    129  1.1  cdi 
    130  1.1  cdi void
    131  1.1  cdi prom_halt(void)
    132  1.1  cdi {
    133  1.1  cdi 	_prom_halt();
    134  1.1  cdi }
    135  1.1  cdi 
    136  1.1  cdi int
    137  1.1  cdi prom_findroot(void)
    138  1.1  cdi {
    139  1.1  cdi 	return OF_peer(0);
    140  1.1  cdi }
    141  1.1  cdi 
    142  1.1  cdi void
    143  1.1  cdi prom_init(void)
    144  1.1  cdi {
    145  1.1  cdi 	int phandle, size;
    146  1.1  cdi 
    147  1.1  cdi 	OF_initialize();
    148  1.1  cdi 
    149  1.1  cdi 	memset(promops, 0, sizeof(promops));
    150  1.1  cdi 
    151  1.1  cdi 	/* Access to boot arguments */
    152  1.1  cdi 	promops.po_bootpath = openfirmware_bootpath;
    153  1.1  cdi 	promops.po_bootfile = openfirmware_bootfile;
    154  1.1  cdi 	promops.po_bootargs = openfirmware_bootargs;
    155  1.1  cdi 
    156  1.1  cdi 	/* I/O functions */
    157  1.1  cdi 	promops.po_getchar = openfirmware_getchar;
    158  1.1  cdi 	promops.po_putchar = openfirmware_putchar;
    159  1.1  cdi 	promops.po_open  = OF_open;
    160  1.1  cdi 	promops.po_close = OF_close;
    161  1.1  cdi 	promops.po_read  = OF_read;
    162  1.1  cdi 	promops.po_write = OF_write;
    163  1.1  cdi 	promops.po_seek  = OF_seek;
    164  1.1  cdi 
    165  1.1  cdi 	promops.po_instance_to_package = OF_instance_to_package;
    166  1.1  cdi 
    167  1.1  cdi 	/* Program termination control */
    168  1.1  cdi 	promops.po_halt  = OF_exit;
    169  1.1  cdi 	promops.po_abort = OF_enter;
    170  1.1  cdi 	promops.po_ticks = OF_milliseconds;
    171  1.1  cdi 
    172  1.1  cdi 	/* Device node traversal */
    173  1.1  cdi 	promops.po_firstchild  = OF_child;
    174  1.1  cdi 	promops.po_nextsibling = OF_peer;
    175  1.1  cdi 
    176  1.1  cdi 	/* Device node properties */
    177  1.1  cdi 	promops.po_getprop = OF_getprop;
    178  1.1  cdi 
    179  1.1  cdi 	/* Device discovery */
    180  1.1  cdi 	promops.po_finddevice = OF_finddevice;
    181  1.1  cdi 
    182  1.1  cdi 	/* Console I/O */
    183  1.1  cdi 	phandle = openfirmware_chosen();
    184  1.1  cdi 	size = _prom_getprop(phandle, "stdin", &promops.po_stdin,
    185  1.1  cdi 			sizeof(promops.po_stdin));
    186  1.1  cdi 	size += _prom_getprop(phandle, "stdout", &promops.po_stdout,
    187  1.1  cdi 			sizeof(promops.po_stdout));
    188  1.1  cdi 	if (size != (sizeof(promops.po_stdin) + sizeof(promops.po_stdout))) {
    189  1.1  cdi 		prom_halt();
    190  1.1  cdi 	}
    191  1.1  cdi }
    192