Home | History | Annotate | Line # | Download | only in ofwboot
      1  1.23      phx /*	$NetBSD: Locore.c,v 1.23 2014/09/20 23:10:46 phx Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*
      4   1.1  thorpej  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      5   1.1  thorpej  * Copyright (C) 1995, 1996 TooLs GmbH.
      6   1.1  thorpej  * All rights reserved.
      7   1.1  thorpej  *
      8   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      9   1.1  thorpej  * modification, are permitted provided that the following conditions
     10   1.1  thorpej  * are met:
     11   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     12   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     13   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     16   1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     17   1.1  thorpej  *    must display the following acknowledgement:
     18   1.1  thorpej  *	This product includes software developed by TooLs GmbH.
     19   1.1  thorpej  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20   1.1  thorpej  *    derived from this software without specific prior written permission.
     21   1.1  thorpej  *
     22   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23   1.1  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1  thorpej  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26   1.1  thorpej  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27   1.1  thorpej  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28   1.1  thorpej  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29   1.1  thorpej  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30   1.1  thorpej  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31   1.1  thorpej  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1  thorpej  */
     33   1.1  thorpej 
     34   1.9  aymeric #include "openfirm.h"
     35  1.18  garbled #include <sys/param.h>
     36   1.1  thorpej #include <lib/libsa/stand.h>
     37   1.1  thorpej 
     38   1.1  thorpej #include <machine/cpu.h>
     39   1.1  thorpej 
     40  1.10  aymeric static int (*openfirmware_entry)(void *);
     41  1.10  aymeric static int openfirmware(void *);
     42   1.4  mycroft 
     43  1.18  garbled void startup(void *, int, int (*)(void *), char *, int)
     44  1.18  garbled 	__attribute__((__used__));
     45  1.10  aymeric static void setup(void);
     46   1.1  thorpej 
     47  1.20  garbled /* this pad gets the rodata laignment right, don't EVER fiddle it */
     48  1.20  garbled char *pad __attribute__((__aligned__ (8))) = "pad";
     49  1.23      phx int stack[0x20000/4 + 4] __attribute__((__aligned__ (4), __used__));
     50  1.20  garbled char *heapspace __attribute__((__aligned__ (4)));
     51  1.20  garbled char altheap[0x20000] __attribute__((__aligned__ (4)));
     52   1.4  mycroft 
     53   1.4  mycroft static int
     54   1.9  aymeric openfirmware(void *arg)
     55   1.4  mycroft {
     56   1.9  aymeric 	int r;
     57   1.4  mycroft 
     58  1.13    perry 	__asm volatile ("sync; isync");
     59   1.9  aymeric 	r = openfirmware_entry(arg);
     60  1.13    perry 	__asm volatile ("sync; isync");
     61   1.9  aymeric 
     62   1.9  aymeric 	return r;
     63   1.4  mycroft }
     64   1.4  mycroft 
     65  1.18  garbled void
     66   1.9  aymeric startup(void *vpd, int res, int (*openfirm)(void *), char *arg, int argl)
     67   1.1  thorpej {
     68   1.1  thorpej 
     69   1.4  mycroft 	openfirmware_entry = openfirm;
     70   1.1  thorpej 	setup();
     71   1.4  mycroft 	main();
     72   1.4  mycroft 	OF_exit();
     73   1.1  thorpej }
     74   1.1  thorpej 
     75   1.1  thorpej __dead void
     76   1.9  aymeric OF_exit(void)
     77   1.1  thorpej {
     78   1.1  thorpej 	static struct {
     79   1.1  thorpej 		char *name;
     80   1.1  thorpej 		int nargs;
     81   1.1  thorpej 		int nreturns;
     82   1.1  thorpej 	} args = {
     83   1.1  thorpej 		"exit",
     84   1.1  thorpej 		0,
     85   1.1  thorpej 		0
     86   1.1  thorpej 	};
     87   1.1  thorpej 
     88   1.1  thorpej 	openfirmware(&args);
     89   1.4  mycroft 	for (;;);			/* just in case */
     90   1.7      chs }
     91   1.7      chs 
     92   1.7      chs __dead void
     93   1.9  aymeric OF_boot(char *bootspec)
     94   1.7      chs {
     95   1.7      chs 	static struct {
     96   1.7      chs 		char *name;
     97   1.7      chs 		int nargs;
     98   1.7      chs 		int nreturns;
     99   1.7      chs 		char *bootspec;
    100   1.7      chs 	} args = {
    101   1.7      chs 		"boot",
    102   1.7      chs 		1,
    103   1.7      chs 		0,
    104   1.7      chs 	};
    105   1.7      chs 
    106   1.7      chs 	args.bootspec = bootspec;
    107   1.7      chs 	openfirmware(&args);
    108  1.17     jmmv 	for (;;);			/* just in case */
    109   1.1  thorpej }
    110   1.1  thorpej 
    111   1.1  thorpej int
    112   1.9  aymeric OF_finddevice(char *name)
    113   1.1  thorpej {
    114   1.1  thorpej 	static struct {
    115   1.1  thorpej 		char *name;
    116   1.1  thorpej 		int nargs;
    117   1.1  thorpej 		int nreturns;
    118   1.1  thorpej 		char *device;
    119   1.1  thorpej 		int phandle;
    120   1.1  thorpej 	} args = {
    121   1.1  thorpej 		"finddevice",
    122   1.1  thorpej 		1,
    123   1.1  thorpej 		1,
    124  1.17     jmmv 	};
    125  1.17     jmmv 
    126   1.1  thorpej 	args.device = name;
    127   1.1  thorpej 	if (openfirmware(&args) == -1)
    128   1.1  thorpej 		return -1;
    129   1.1  thorpej 	return args.phandle;
    130   1.1  thorpej }
    131   1.1  thorpej 
    132   1.1  thorpej int
    133   1.9  aymeric OF_instance_to_package(int ihandle)
    134   1.1  thorpej {
    135   1.1  thorpej 	static struct {
    136   1.1  thorpej 		char *name;
    137   1.1  thorpej 		int nargs;
    138   1.1  thorpej 		int nreturns;
    139   1.1  thorpej 		int ihandle;
    140   1.1  thorpej 		int phandle;
    141   1.1  thorpej 	} args = {
    142   1.1  thorpej 		"instance-to-package",
    143   1.1  thorpej 		1,
    144   1.1  thorpej 		1,
    145   1.1  thorpej 	};
    146  1.17     jmmv 
    147   1.1  thorpej 	args.ihandle = ihandle;
    148   1.1  thorpej 	if (openfirmware(&args) == -1)
    149   1.1  thorpej 		return -1;
    150   1.1  thorpej 	return args.phandle;
    151   1.1  thorpej }
    152   1.1  thorpej 
    153   1.1  thorpej int
    154   1.9  aymeric OF_getprop(int handle, char *prop, void *buf, int buflen)
    155   1.1  thorpej {
    156   1.1  thorpej 	static struct {
    157   1.1  thorpej 		char *name;
    158   1.1  thorpej 		int nargs;
    159   1.1  thorpej 		int nreturns;
    160   1.1  thorpej 		int phandle;
    161   1.1  thorpej 		char *prop;
    162   1.1  thorpej 		void *buf;
    163   1.1  thorpej 		int buflen;
    164   1.1  thorpej 		int size;
    165   1.1  thorpej 	} args = {
    166   1.1  thorpej 		"getprop",
    167   1.1  thorpej 		4,
    168   1.1  thorpej 		1,
    169   1.1  thorpej 	};
    170  1.17     jmmv 
    171   1.1  thorpej 	args.phandle = handle;
    172   1.1  thorpej 	args.prop = prop;
    173   1.1  thorpej 	args.buf = buf;
    174   1.1  thorpej 	args.buflen = buflen;
    175   1.1  thorpej 	if (openfirmware(&args) == -1)
    176   1.1  thorpej 		return -1;
    177   1.1  thorpej 	return args.size;
    178   1.1  thorpej }
    179   1.1  thorpej 
    180   1.1  thorpej #ifdef	__notyet__	/* Has a bug on FirePower */
    181   1.1  thorpej int
    182   1.9  aymeric OF_setprop(int handle, char *prop, void *buf, int len)
    183   1.1  thorpej {
    184   1.1  thorpej 	static struct {
    185   1.1  thorpej 		char *name;
    186   1.1  thorpej 		int nargs;
    187   1.1  thorpej 		int nreturns;
    188   1.1  thorpej 		int phandle;
    189   1.1  thorpej 		char *prop;
    190   1.1  thorpej 		void *buf;
    191   1.1  thorpej 		int len;
    192   1.1  thorpej 		int size;
    193   1.1  thorpej 	} args = {
    194   1.1  thorpej 		"setprop",
    195   1.1  thorpej 		4,
    196   1.1  thorpej 		1,
    197   1.1  thorpej 	};
    198  1.17     jmmv 
    199   1.1  thorpej 	args.phandle = handle;
    200   1.1  thorpej 	args.prop = prop;
    201   1.1  thorpej 	args.buf = buf;
    202   1.1  thorpej 	args.len = len;
    203   1.1  thorpej 	if (openfirmware(&args) == -1)
    204   1.1  thorpej 		return -1;
    205   1.1  thorpej 	return args.size;
    206   1.1  thorpej }
    207   1.1  thorpej #endif
    208   1.1  thorpej 
    209   1.1  thorpej int
    210   1.9  aymeric OF_open(char *dname)
    211   1.1  thorpej {
    212   1.1  thorpej 	static struct {
    213   1.1  thorpej 		char *name;
    214   1.1  thorpej 		int nargs;
    215   1.1  thorpej 		int nreturns;
    216   1.1  thorpej 		char *dname;
    217   1.1  thorpej 		int handle;
    218   1.1  thorpej 	} args = {
    219   1.1  thorpej 		"open",
    220   1.1  thorpej 		1,
    221   1.1  thorpej 		1,
    222   1.1  thorpej 	};
    223  1.17     jmmv 
    224   1.4  mycroft #ifdef OFW_DEBUG
    225   1.4  mycroft 	printf("OF_open(%s) -> ", dname);
    226   1.4  mycroft #endif
    227   1.1  thorpej 	args.dname = dname;
    228   1.3  mycroft 	if (openfirmware(&args) == -1 ||
    229   1.4  mycroft 	    args.handle == 0) {
    230   1.4  mycroft #ifdef OFW_DEBUG
    231   1.4  mycroft 		printf("lose\n");
    232   1.4  mycroft #endif
    233   1.1  thorpej 		return -1;
    234   1.4  mycroft 	}
    235   1.4  mycroft #ifdef OFW_DEBUG
    236   1.4  mycroft 	printf("%d\n", args.handle);
    237   1.4  mycroft #endif
    238   1.1  thorpej 	return args.handle;
    239   1.1  thorpej }
    240   1.1  thorpej 
    241   1.1  thorpej void
    242   1.9  aymeric OF_close(int handle)
    243   1.1  thorpej {
    244   1.1  thorpej 	static struct {
    245   1.1  thorpej 		char *name;
    246   1.1  thorpej 		int nargs;
    247   1.1  thorpej 		int nreturns;
    248   1.1  thorpej 		int handle;
    249   1.1  thorpej 	} args = {
    250   1.1  thorpej 		"close",
    251   1.1  thorpej 		1,
    252   1.1  thorpej 		0,
    253   1.1  thorpej 	};
    254  1.17     jmmv 
    255   1.4  mycroft #ifdef OFW_DEBUG
    256   1.4  mycroft 	printf("OF_close(%d)\n", handle);
    257   1.4  mycroft #endif
    258   1.1  thorpej 	args.handle = handle;
    259   1.1  thorpej 	openfirmware(&args);
    260   1.1  thorpej }
    261   1.1  thorpej 
    262   1.1  thorpej int
    263   1.9  aymeric OF_write(int handle, void *addr, int len)
    264   1.1  thorpej {
    265   1.1  thorpej 	static struct {
    266   1.1  thorpej 		char *name;
    267   1.1  thorpej 		int nargs;
    268   1.1  thorpej 		int nreturns;
    269   1.1  thorpej 		int ihandle;
    270   1.1  thorpej 		void *addr;
    271   1.1  thorpej 		int len;
    272   1.1  thorpej 		int actual;
    273   1.1  thorpej 	} args = {
    274   1.1  thorpej 		"write",
    275   1.1  thorpej 		3,
    276   1.1  thorpej 		1,
    277   1.1  thorpej 	};
    278   1.1  thorpej 
    279   1.4  mycroft #ifdef OFW_DEBUG
    280   1.4  mycroft 	if (len != 1)
    281   1.9  aymeric 		printf("OF_write(%d, %p, %x) -> ", handle, addr, len);
    282   1.4  mycroft #endif
    283   1.1  thorpej 	args.ihandle = handle;
    284   1.1  thorpej 	args.addr = addr;
    285   1.1  thorpej 	args.len = len;
    286   1.4  mycroft 	if (openfirmware(&args) == -1) {
    287   1.4  mycroft #ifdef OFW_DEBUG
    288   1.4  mycroft 		printf("lose\n");
    289   1.4  mycroft #endif
    290   1.1  thorpej 		return -1;
    291   1.4  mycroft 	}
    292   1.4  mycroft #ifdef OFW_DEBUG
    293   1.4  mycroft 	if (len != 1)
    294   1.4  mycroft 		printf("%x\n", args.actual);
    295   1.4  mycroft #endif
    296   1.1  thorpej 	return args.actual;
    297   1.1  thorpej }
    298   1.1  thorpej 
    299   1.1  thorpej int
    300   1.9  aymeric OF_read(int handle, void *addr, int len)
    301   1.1  thorpej {
    302   1.1  thorpej 	static struct {
    303   1.1  thorpej 		char *name;
    304   1.1  thorpej 		int nargs;
    305   1.1  thorpej 		int nreturns;
    306   1.1  thorpej 		int ihandle;
    307   1.1  thorpej 		void *addr;
    308   1.1  thorpej 		int len;
    309   1.1  thorpej 		int actual;
    310   1.1  thorpej 	} args = {
    311   1.1  thorpej 		"read",
    312   1.1  thorpej 		3,
    313   1.1  thorpej 		1,
    314   1.1  thorpej 	};
    315   1.1  thorpej 
    316   1.4  mycroft #ifdef OFW_DEBUG
    317   1.4  mycroft 	if (len != 1)
    318   1.9  aymeric 		printf("OF_read(%d, %p, %x) -> ", handle, addr, len);
    319   1.4  mycroft #endif
    320   1.1  thorpej 	args.ihandle = handle;
    321   1.1  thorpej 	args.addr = addr;
    322   1.1  thorpej 	args.len = len;
    323   1.4  mycroft 	if (openfirmware(&args) == -1) {
    324   1.4  mycroft #ifdef OFW_DEBUG
    325   1.4  mycroft 		printf("lose\n");
    326   1.4  mycroft #endif
    327   1.1  thorpej 		return -1;
    328   1.4  mycroft 	}
    329   1.4  mycroft #ifdef OFW_DEBUG
    330   1.4  mycroft 	if (len != 1)
    331   1.4  mycroft 		printf("%x\n", args.actual);
    332   1.4  mycroft #endif
    333   1.1  thorpej 	return args.actual;
    334   1.1  thorpej }
    335   1.1  thorpej 
    336   1.1  thorpej int
    337   1.9  aymeric OF_seek(int handle, u_quad_t pos)
    338   1.1  thorpej {
    339   1.1  thorpej 	static struct {
    340   1.1  thorpej 		char *name;
    341   1.1  thorpej 		int nargs;
    342   1.1  thorpej 		int nreturns;
    343   1.1  thorpej 		int handle;
    344   1.1  thorpej 		int poshi;
    345   1.1  thorpej 		int poslo;
    346   1.1  thorpej 		int status;
    347   1.1  thorpej 	} args = {
    348   1.1  thorpej 		"seek",
    349   1.1  thorpej 		3,
    350   1.1  thorpej 		1,
    351   1.1  thorpej 	};
    352  1.17     jmmv 
    353   1.4  mycroft #ifdef OFW_DEBUG
    354   1.4  mycroft 	printf("OF_seek(%d, %x, %x) -> ", handle, (int)(pos >> 32), (int)pos);
    355   1.4  mycroft #endif
    356   1.1  thorpej 	args.handle = handle;
    357   1.1  thorpej 	args.poshi = (int)(pos >> 32);
    358   1.1  thorpej 	args.poslo = (int)pos;
    359   1.4  mycroft 	if (openfirmware(&args) == -1) {
    360   1.4  mycroft #ifdef OFW_DEBUG
    361   1.4  mycroft 		printf("lose\n");
    362   1.4  mycroft #endif
    363   1.1  thorpej 		return -1;
    364   1.4  mycroft 	}
    365   1.4  mycroft #ifdef OFW_DEBUG
    366   1.4  mycroft 	printf("%d\n", args.status);
    367   1.4  mycroft #endif
    368   1.1  thorpej 	return args.status;
    369   1.1  thorpej }
    370   1.1  thorpej 
    371   1.1  thorpej void *
    372  1.18  garbled OF_alloc_mem(u_int size)
    373  1.18  garbled {
    374  1.18  garbled 	static struct {
    375  1.18  garbled 		char *name;
    376  1.18  garbled 		int nargs;
    377  1.18  garbled 		int nreturns;
    378  1.18  garbled 		u_int size;
    379  1.18  garbled 		void *baseaddr;
    380  1.18  garbled 	} args = {
    381  1.18  garbled 		"alloc-mem",
    382  1.18  garbled 		1,
    383  1.18  garbled 		1,
    384  1.18  garbled 	};
    385  1.18  garbled #ifdef OFW_DEBUG
    386  1.18  garbled 	printf("alloc-mem %x -> ", size);
    387  1.18  garbled #endif
    388  1.18  garbled 	if (openfirmware(&args) == -1) {
    389  1.18  garbled #ifdef OFW_DEBUG
    390  1.18  garbled 		printf("lose\n");
    391  1.18  garbled #endif
    392  1.18  garbled 		return (void *)-1;
    393  1.18  garbled 	}
    394  1.18  garbled #ifdef OFW_DEBUG
    395  1.18  garbled 	printf("%p\n", args.baseaddr);
    396  1.18  garbled #endif
    397  1.18  garbled 	return args.baseaddr;
    398  1.18  garbled }
    399  1.18  garbled 
    400  1.18  garbled void *
    401   1.9  aymeric OF_claim(void *virt, u_int size, u_int align)
    402   1.1  thorpej {
    403   1.1  thorpej 	static struct {
    404   1.1  thorpej 		char *name;
    405   1.1  thorpej 		int nargs;
    406   1.1  thorpej 		int nreturns;
    407   1.1  thorpej 		void *virt;
    408   1.1  thorpej 		u_int size;
    409   1.1  thorpej 		u_int align;
    410   1.1  thorpej 		void *baseaddr;
    411   1.1  thorpej 	} args = {
    412   1.1  thorpej 		"claim",
    413   1.1  thorpej 		3,
    414   1.1  thorpej 		1,
    415   1.1  thorpej 	};
    416   1.1  thorpej 
    417   1.4  mycroft #ifdef OFW_DEBUG
    418   1.9  aymeric 	printf("OF_claim(%p, %x, %x) -> ", virt, size, align);
    419   1.1  thorpej #endif
    420   1.1  thorpej 	args.virt = virt;
    421   1.1  thorpej 	args.size = size;
    422   1.1  thorpej 	args.align = align;
    423   1.4  mycroft 	if (openfirmware(&args) == -1) {
    424   1.4  mycroft #ifdef OFW_DEBUG
    425   1.4  mycroft 		printf("lose\n");
    426   1.4  mycroft #endif
    427   1.1  thorpej 		return (void *)-1;
    428   1.4  mycroft 	}
    429   1.4  mycroft #ifdef OFW_DEBUG
    430   1.9  aymeric 	printf("%p\n", args.baseaddr);
    431   1.4  mycroft #endif
    432   1.1  thorpej 	return args.baseaddr;
    433   1.1  thorpej }
    434   1.1  thorpej 
    435   1.1  thorpej void
    436   1.9  aymeric OF_release(void *virt, u_int size)
    437   1.1  thorpej {
    438   1.1  thorpej 	static struct {
    439   1.1  thorpej 		char *name;
    440   1.1  thorpej 		int nargs;
    441   1.1  thorpej 		int nreturns;
    442   1.1  thorpej 		void *virt;
    443   1.1  thorpej 		u_int size;
    444   1.1  thorpej 	} args = {
    445   1.1  thorpej 		"release",
    446   1.1  thorpej 		2,
    447   1.1  thorpej 		0,
    448   1.1  thorpej 	};
    449  1.17     jmmv 
    450   1.4  mycroft #ifdef OFW_DEBUG
    451   1.9  aymeric 	printf("OF_release(%p, %x)\n", virt, size);
    452   1.4  mycroft #endif
    453   1.1  thorpej 	args.virt = virt;
    454   1.1  thorpej 	args.size = size;
    455   1.1  thorpej 	openfirmware(&args);
    456   1.1  thorpej }
    457   1.1  thorpej 
    458   1.1  thorpej int
    459   1.9  aymeric OF_milliseconds(void)
    460   1.1  thorpej {
    461   1.1  thorpej 	static struct {
    462   1.1  thorpej 		char *name;
    463   1.1  thorpej 		int nargs;
    464   1.1  thorpej 		int nreturns;
    465   1.1  thorpej 		int ms;
    466   1.1  thorpej 	} args = {
    467   1.1  thorpej 		"milliseconds",
    468   1.1  thorpej 		0,
    469   1.1  thorpej 		1,
    470   1.1  thorpej 	};
    471  1.17     jmmv 
    472   1.1  thorpej 	openfirmware(&args);
    473   1.1  thorpej 	return args.ms;
    474   1.1  thorpej }
    475   1.1  thorpej 
    476   1.1  thorpej #ifdef	__notyet__
    477   1.1  thorpej void
    478   1.9  aymeric OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
    479   1.1  thorpej {
    480   1.1  thorpej 	static struct {
    481   1.1  thorpej 		char *name;
    482   1.1  thorpej 		int nargs;
    483   1.1  thorpej 		int nreturns;
    484   1.1  thorpej 		void *virt;
    485   1.1  thorpej 		u_int size;
    486   1.1  thorpej 		void (*entry)();
    487   1.1  thorpej 		void *arg;
    488   1.1  thorpej 		u_int len;
    489   1.1  thorpej 	} args = {
    490   1.1  thorpej 		"chain",
    491   1.1  thorpej 		5,
    492   1.1  thorpej 		0,
    493   1.1  thorpej 	};
    494   1.1  thorpej 
    495   1.1  thorpej 	args.virt = virt;
    496   1.1  thorpej 	args.size = size;
    497   1.1  thorpej 	args.entry = entry;
    498   1.1  thorpej 	args.arg = arg;
    499   1.1  thorpej 	args.len = len;
    500   1.1  thorpej 	openfirmware(&args);
    501   1.1  thorpej }
    502   1.1  thorpej #else
    503   1.1  thorpej void
    504  1.10  aymeric OF_chain(void *virt, u_int size, boot_entry_t entry, void *arg, u_int len)
    505   1.1  thorpej {
    506   1.1  thorpej 	/*
    507   1.1  thorpej 	 * This is a REALLY dirty hack till the firmware gets this going
    508   1.1  thorpej 	 */
    509  1.18  garbled #if 0
    510   1.1  thorpej 	OF_release(virt, size);
    511   1.4  mycroft #endif
    512   1.4  mycroft 	entry(0, 0, openfirmware_entry, arg, len);
    513   1.1  thorpej }
    514   1.1  thorpej #endif
    515   1.1  thorpej 
    516   1.1  thorpej static int stdin;
    517   1.1  thorpej static int stdout;
    518   1.1  thorpej 
    519   1.1  thorpej static void
    520   1.9  aymeric setup(void)
    521   1.1  thorpej {
    522   1.1  thorpej 	int chosen;
    523  1.17     jmmv 
    524   1.1  thorpej 	if ((chosen = OF_finddevice("/chosen")) == -1)
    525   1.4  mycroft 		OF_exit();
    526   1.4  mycroft 	if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) !=
    527   1.4  mycroft 	    sizeof(stdin) ||
    528   1.4  mycroft 	    OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) !=
    529   1.1  thorpej 	    sizeof(stdout))
    530   1.4  mycroft 		OF_exit();
    531  1.18  garbled 
    532  1.20  garbled 	//printf("Allocating 0x20000 bytes of ram for boot\n");
    533  1.18  garbled 	heapspace = OF_claim(0, 0x20000, NBPG);
    534  1.18  garbled 	if (heapspace == (char *)-1) {
    535  1.18  garbled 		printf("WARNING: Failed to alloc ram, using bss\n");
    536  1.18  garbled 		setheap(&altheap, &altheap[0x20000]);
    537  1.18  garbled 	} else
    538  1.18  garbled 		setheap(heapspace, heapspace+0x20000);
    539   1.1  thorpej }
    540   1.1  thorpej 
    541   1.1  thorpej void
    542   1.9  aymeric putchar(int c)
    543   1.1  thorpej {
    544   1.1  thorpej 	char ch = c;
    545   1.1  thorpej 
    546   1.1  thorpej 	if (c == '\n')
    547   1.1  thorpej 		putchar('\r');
    548   1.1  thorpej 	OF_write(stdout, &ch, 1);
    549   1.1  thorpej }
    550   1.1  thorpej 
    551   1.1  thorpej int
    552   1.9  aymeric getchar(void)
    553   1.1  thorpej {
    554   1.2  mycroft 	unsigned char ch = '\0';
    555   1.1  thorpej 	int l;
    556   1.1  thorpej 
    557   1.1  thorpej 	while ((l = OF_read(stdin, &ch, 1)) != 1)
    558   1.2  mycroft 		if (l != -2 && l != 0)
    559   1.1  thorpej 			return -1;
    560   1.1  thorpej 	return ch;
    561   1.1  thorpej }
    562  1.21  garbled 
    563  1.21  garbled #ifdef OFWDUMP
    564  1.21  garbled 
    565  1.21  garbled static int
    566  1.21  garbled OF_peer(int phandle)
    567  1.21  garbled {
    568  1.21  garbled 	static struct {
    569  1.21  garbled 		const char *name;
    570  1.21  garbled 		int nargs;
    571  1.21  garbled 		int nreturns;
    572  1.21  garbled 		int phandle;
    573  1.21  garbled 		int sibling;
    574  1.21  garbled 	} args = {
    575  1.21  garbled 		"peer",
    576  1.21  garbled 			1,
    577  1.21  garbled 			1,
    578  1.21  garbled 	};
    579  1.21  garbled 
    580  1.21  garbled 	args.phandle = phandle;
    581  1.21  garbled 	if (openfirmware(&args) == -1)
    582  1.21  garbled 		return 0;
    583  1.21  garbled 	return args.sibling;
    584  1.21  garbled }
    585  1.21  garbled 
    586  1.21  garbled static int
    587  1.21  garbled OF_child(int phandle)
    588  1.21  garbled {
    589  1.21  garbled 	static struct {
    590  1.21  garbled 		const char *name;
    591  1.21  garbled 		int nargs;
    592  1.21  garbled 		int nreturns;
    593  1.21  garbled 		int phandle;
    594  1.21  garbled 		int child;
    595  1.21  garbled 	} args = {
    596  1.21  garbled 		"child",
    597  1.21  garbled 			1,
    598  1.21  garbled 			1,
    599  1.21  garbled 	};
    600  1.21  garbled 
    601  1.21  garbled 	args.phandle = phandle;
    602  1.21  garbled 	if (openfirmware(&args) == -1)
    603  1.21  garbled 		return 0;
    604  1.21  garbled 	return args.child;
    605  1.21  garbled }
    606  1.21  garbled 
    607  1.21  garbled int
    608  1.21  garbled OF_nextprop(int handle, const char *prop, void *nextprop)
    609  1.21  garbled {
    610  1.21  garbled 	static struct {
    611  1.21  garbled 		const char *name;
    612  1.21  garbled 		int nargs;
    613  1.21  garbled 		int nreturns;
    614  1.21  garbled 		int phandle;
    615  1.21  garbled 		const char *prop;
    616  1.21  garbled 		char *buf;
    617  1.21  garbled 		int flag;
    618  1.21  garbled 	} args = {
    619  1.21  garbled 		"nextprop",
    620  1.21  garbled 			3,
    621  1.21  garbled 			1,
    622  1.21  garbled 	};
    623  1.21  garbled 
    624  1.21  garbled 	args.phandle = handle;
    625  1.21  garbled 	args.prop = prop;
    626  1.21  garbled 	args.buf = nextprop;
    627  1.21  garbled 	if (openfirmware(&args) == -1)
    628  1.21  garbled 		return -1;
    629  1.21  garbled 	return args.flag;
    630  1.21  garbled }
    631  1.21  garbled 
    632  1.21  garbled static int
    633  1.21  garbled OF_package_to_path(int phandle, char *buf, int buflen)
    634  1.21  garbled {
    635  1.21  garbled 	static struct {
    636  1.21  garbled 		const char *name;
    637  1.21  garbled 		int nargs;
    638  1.21  garbled 		int nreturns;
    639  1.21  garbled 		int phandle;
    640  1.21  garbled 		char *buf;
    641  1.21  garbled 		int buflen;
    642  1.21  garbled 		int length;
    643  1.21  garbled 	} args = {
    644  1.21  garbled 		"package-to-path",
    645  1.21  garbled 			3,
    646  1.21  garbled 			1,
    647  1.21  garbled 	};
    648  1.21  garbled 
    649  1.21  garbled 	if (buflen > 4096)
    650  1.21  garbled 		return -1;
    651  1.21  garbled 	args.phandle = phandle;
    652  1.21  garbled 	args.buf = buf;
    653  1.21  garbled 	args.buflen = buflen;
    654  1.21  garbled 	if (openfirmware(&args) < 0)
    655  1.21  garbled 		return -1;
    656  1.21  garbled 	if (args.length > buflen)
    657  1.21  garbled 		args.length = buflen;
    658  1.21  garbled 	return args.length;
    659  1.21  garbled }
    660  1.21  garbled 
    661  1.21  garbled void
    662  1.21  garbled dump_ofwtree(int node)
    663  1.21  garbled {
    664  1.21  garbled 	int peer, child, namelen, dlen, i;
    665  1.21  garbled 	char namebuf[33], newnamebuf[33];
    666  1.21  garbled 	char path[256], data[256];
    667  1.21  garbled 
    668  1.21  garbled 	for (peer = node; peer; peer = OF_peer(peer)) {
    669  1.21  garbled 		printf("\nnode: 0x%x ", peer);
    670  1.21  garbled 		if (OF_package_to_path(peer, path, 512) >= 0)
    671  1.21  garbled 			printf("path=%s", path);
    672  1.21  garbled 		printf("\n");
    673  1.21  garbled 		namebuf[0] = '\0';
    674  1.21  garbled 		namelen = OF_nextprop(peer, namebuf, &newnamebuf);
    675  1.21  garbled 		while (namelen >= 0) {
    676  1.21  garbled 			/*printf("namelen == %d namebuf=%s new=%s\n", namelen,
    677  1.21  garbled 			  namebuf, newnamebuf);*/
    678  1.21  garbled 			//newnamebuf[namelen] = '\0';
    679  1.21  garbled 			strcpy(namebuf, newnamebuf);
    680  1.21  garbled 			printf("  %s :", newnamebuf);
    681  1.21  garbled 			dlen = OF_getprop(peer, newnamebuf, data, 256);
    682  1.21  garbled 			if (dlen > 0) {
    683  1.21  garbled 				if (data[0] < 0177)
    684  1.21  garbled 					printf(" %s\n", data);
    685  1.21  garbled 				else
    686  1.21  garbled 					printf("\n");
    687  1.21  garbled 				printf("    ");
    688  1.21  garbled 				for (i=0; i < dlen && i < 256; i++) {
    689  1.21  garbled 					if (data[i] < 0x10)
    690  1.21  garbled 						printf("0");
    691  1.21  garbled 					printf("%x", data[i]);
    692  1.21  garbled 					if ((i+1)%4 == 0)
    693  1.21  garbled 						printf(" ");
    694  1.21  garbled 					if ((i+1)%32 == 0)
    695  1.21  garbled 						printf("\n    ");
    696  1.21  garbled 				}
    697  1.21  garbled 			}
    698  1.21  garbled 			printf("\n");
    699  1.21  garbled 			namelen = OF_nextprop(peer, namebuf, &newnamebuf);
    700  1.21  garbled 			if (newnamebuf[0] == '\0' ||
    701  1.21  garbled 			    strcmp(namebuf, newnamebuf) == 0)
    702  1.21  garbled 				break;
    703  1.21  garbled 		}
    704  1.21  garbled 		child = OF_child(peer);
    705  1.21  garbled 		if (child > 0)
    706  1.21  garbled 			dump_ofwtree(child);
    707  1.21  garbled 	}
    708  1.21  garbled }
    709  1.21  garbled 
    710  1.21  garbled #endif /* OFWDUMP */
    711