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