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