1 /* $NetBSD: Locore.c,v 1.21 2008/02/12 04:27:46 garbled Exp $ */
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/param.h>
36 #include <lib/libsa/stand.h>
38 #include <machine/cpu.h>
40 static int (*openfirmware_entry
)(void *);
41 static int openfirmware(void *);
43 void startup(void *, int, int (*)(void *), char *, int)
44 __attribute__((__used__
));
45 static void setup(void);
47 /* this pad gets the rodata laignment right, don't EVER fiddle it */
48 char *pad
__attribute__((__aligned__ (8))) = "pad";
49 int stack
[8192/4 + 4] __attribute__((__aligned__ (4), __used__
));
50 char *heapspace
__attribute__((__aligned__ (4)));
51 char altheap
[0x20000] __attribute__((__aligned__ (4)));
54 openfirmware(void *arg
)
58 __asm
volatile ("sync; isync");
59 r
= openfirmware_entry(arg
);
60 __asm
volatile ("sync; isync");
66 startup(void *vpd
, int res
, int (*openfirm
)(void *), char *arg
, int argl
)
69 openfirmware_entry
= openfirm
;
89 for (;;); /* just in case */
93 OF_boot(char *bootspec
)
106 args
.bootspec
= bootspec
;
108 for (;;); /* just in case */
112 OF_finddevice(char *name
)
127 if (openfirmware(&args
) == -1)
133 OF_instance_to_package(int ihandle
)
142 "instance-to-package",
147 args
.ihandle
= ihandle
;
148 if (openfirmware(&args
) == -1)
154 OF_getprop(int handle
, char *prop
, void *buf
, int buflen
)
171 args
.phandle
= handle
;
174 args
.buflen
= buflen
;
175 if (openfirmware(&args
) == -1)
180 #ifdef __notyet__ /* Has a bug on FirePower */
182 OF_setprop(int handle
, char *prop
, void *buf
, int len
)
199 args
.phandle
= handle
;
203 if (openfirmware(&args
) == -1)
225 printf("OF_open(%s) -> ", dname
);
228 if (openfirmware(&args
) == -1 ||
236 printf("%d\n", args
.handle
);
256 printf("OF_close(%d)\n", handle
);
258 args
.handle
= handle
;
263 OF_write(int handle
, void *addr
, int len
)
281 printf("OF_write(%d, %p, %x) -> ", handle
, addr
, len
);
283 args
.ihandle
= handle
;
286 if (openfirmware(&args
) == -1) {
294 printf("%x\n", args
.actual
);
300 OF_read(int handle
, void *addr
, int len
)
318 printf("OF_read(%d, %p, %x) -> ", handle
, addr
, len
);
320 args
.ihandle
= handle
;
323 if (openfirmware(&args
) == -1) {
331 printf("%x\n", args
.actual
);
337 OF_seek(int handle
, u_quad_t pos
)
354 printf("OF_seek(%d, %x, %x) -> ", handle
, (int)(pos
>> 32), (int)pos
);
356 args
.handle
= handle
;
357 args
.poshi
= (int)(pos
>> 32);
358 args
.poslo
= (int)pos
;
359 if (openfirmware(&args
) == -1) {
366 printf("%d\n", args
.status
);
372 OF_alloc_mem(u_int size
)
386 printf("alloc-mem %x -> ", size
);
388 if (openfirmware(&args
) == -1) {
395 printf("%p\n", args
.baseaddr
);
397 return args
.baseaddr
;
401 OF_claim(void *virt
, u_int size
, u_int align
)
418 printf("OF_claim(%p, %x, %x) -> ", virt
, size
, align
);
423 if (openfirmware(&args
) == -1) {
430 printf("%p\n", args
.baseaddr
);
432 return args
.baseaddr
;
436 OF_release(void *virt
, u_int size
)
451 printf("OF_release(%p, %x)\n", virt
, size
);
459 OF_milliseconds(void)
478 OF_chain(void *virt
, u_int size
, void (*entry
)(), void *arg
, u_int len
)
504 OF_chain(void *virt
, u_int size
, boot_entry_t entry
, void *arg
, u_int len
)
507 * This is a REALLY dirty hack till the firmware gets this going
510 OF_release(virt
, size
);
512 entry(0, 0, openfirmware_entry
, arg
, len
);
524 if ((chosen
= OF_finddevice("/chosen")) == -1)
526 if (OF_getprop(chosen
, "stdin", &stdin
, sizeof(stdin
)) !=
528 OF_getprop(chosen
, "stdout", &stdout
, sizeof(stdout
)) !=
532 //printf("Allocating 0x20000 bytes of ram for boot\n");
533 heapspace
= OF_claim(0, 0x20000, NBPG
);
534 if (heapspace
== (char *)-1) {
535 printf("WARNING: Failed to alloc ram, using bss\n");
536 setheap(&altheap
, &altheap
[0x20000]);
538 setheap(heapspace
, heapspace
+0x20000);
548 OF_write(stdout
, &ch
, 1);
554 unsigned char ch
= '\0';
557 while ((l
= OF_read(stdin
, &ch
, 1)) != 1)
558 if (l
!= -2 && l
!= 0)
580 args
.phandle
= phandle
;
581 if (openfirmware(&args
) == -1)
587 OF_child(int phandle
)
601 args
.phandle
= phandle
;
602 if (openfirmware(&args
) == -1)
608 OF_nextprop(int handle
, const char *prop
, void *nextprop
)
624 args
.phandle
= handle
;
627 if (openfirmware(&args
) == -1)
633 OF_package_to_path(int phandle
, char *buf
, int buflen
)
651 args
.phandle
= phandle
;
653 args
.buflen
= buflen
;
654 if (openfirmware(&args
) < 0)
656 if (args
.length
> buflen
)
657 args
.length
= buflen
;
662 dump_ofwtree(int node
)
664 int peer
, child
, namelen
, dlen
, i
;
665 char namebuf
[33], newnamebuf
[33];
666 char path
[256], data
[256];
668 for (peer
= node
; peer
; peer
= OF_peer(peer
)) {
669 printf("\nnode: 0x%x ", peer
);
670 if (OF_package_to_path(peer
, path
, 512) >= 0)
671 printf("path=%s", path
);
674 namelen
= OF_nextprop(peer
, namebuf
, &newnamebuf
);
675 while (namelen
>= 0) {
676 /*printf("namelen == %d namebuf=%s new=%s\n", namelen,
677 namebuf, newnamebuf);*/
678 //newnamebuf[namelen] = '\0';
679 strcpy(namebuf
, newnamebuf
);
680 printf(" %s :", newnamebuf
);
681 dlen
= OF_getprop(peer
, newnamebuf
, data
, 256);
684 printf(" %s\n", data
);
688 for (i
=0; i
< dlen
&& i
< 256; i
++) {
691 printf("%x", data
[i
]);
699 namelen
= OF_nextprop(peer
, namebuf
, &newnamebuf
);
700 if (newnamebuf
[0] == '\0' ||
701 strcmp(namebuf
, newnamebuf
) == 0)
704 child
= OF_child(peer
);