Error out if no volumes are specified instead of core-dumping.
[dragonfly.git] / sys / boot / ofw / libofw / openfirm.c
bloba29720b0b5fd4c49226db321d8f3ae52c8988360
1 /*
2 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3 * Copyright (C) 1995, 1996 TooLs GmbH.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (C) 2000 Benno Rice.
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
49 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
52 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
53 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 * $NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $
56 * $FreeBSD: src/sys/boot/ofw/libofw/openfirm.c,v 1.7 2002/07/18 12:39:02 benno Exp $
57 * $DragonFly: src/sys/boot/ofw/libofw/openfirm.c,v 1.1 2003/11/10 06:08:37 dillon Exp $
60 #include <machine/stdarg.h>
62 #include <stand.h>
64 #include "openfirm.h"
66 int (*openfirmware)(void *);
68 static ihandle_t stdin;
69 static ihandle_t stdout;
71 ihandle_t mmu;
72 ihandle_t memory;
74 /* Initialiaser */
76 void
77 OF_init(int (*openfirm)(void *))
79 phandle_t chosen;
81 openfirmware = openfirm;
83 chosen = OF_finddevice("/chosen");
84 OF_getprop(chosen, "memory", &memory, sizeof(memory));
85 if (memory == 0)
86 panic("failed to get memory ihandle");
87 OF_getprop(chosen, "mmu", &mmu, sizeof(mmu));
88 if (mmu == 0)
89 panic("failed to get mmu ihandle");
93 * Generic functions
96 /* Test to see if a service exists. */
97 int
98 OF_test(char *name)
100 static struct {
101 cell_t name;
102 cell_t nargs;
103 cell_t nreturns;
104 cell_t service;
105 cell_t missing;
106 } args = {
107 (cell_t)"test",
114 args.service = (cell_t)name;
115 if (openfirmware(&args) == -1)
116 return -1;
117 return (int)args.missing;
120 /* Return firmware millisecond count. */
122 OF_milliseconds()
124 static struct {
125 cell_t name;
126 cell_t nargs;
127 cell_t nreturns;
128 cell_t ms;
129 } args = {
130 (cell_t)"milliseconds",
136 openfirmware(&args);
137 return (int)args.ms;
141 * Device tree functions
144 /* Return the next sibling of this node or 0. */
145 phandle_t
146 OF_peer(phandle_t node)
148 static struct {
149 cell_t name;
150 cell_t nargs;
151 cell_t nreturns;
152 cell_t node;
153 cell_t next;
154 } args = {
155 (cell_t)"peer",
162 args.node = (u_int)node;
163 if (openfirmware(&args) == -1)
164 return -1;
165 return (phandle_t)args.next;
168 /* Return the first child of this node or 0. */
169 phandle_t
170 OF_child(phandle_t node)
172 static struct {
173 cell_t name;
174 cell_t nargs;
175 cell_t nreturns;
176 cell_t node;
177 cell_t child;
178 } args = {
179 (cell_t)"child",
186 args.node = (u_int)node;
187 if (openfirmware(&args) == -1)
188 return -1;
189 return (phandle_t)args.child;
192 /* Return the parent of this node or 0. */
193 phandle_t
194 OF_parent(phandle_t node)
196 static struct {
197 cell_t name;
198 cell_t nargs;
199 cell_t nreturns;
200 cell_t node;
201 cell_t parent;
202 } args = {
203 (cell_t)"parent",
210 args.node = (u_int)node;
211 if (openfirmware(&args) == -1)
212 return -1;
213 return (phandle_t)args.parent;
216 /* Return the package handle that corresponds to an instance handle. */
217 phandle_t
218 OF_instance_to_package(ihandle_t instance)
220 static struct {
221 cell_t name;
222 cell_t nargs;
223 cell_t nreturns;
224 cell_t instance;
225 cell_t package;
226 } args = {
227 (cell_t)"instance-to-package",
234 args.instance = (u_int)instance;
235 if (openfirmware(&args) == -1)
236 return -1;
237 return (phandle_t)args.package;
240 /* Get the length of a property of a package. */
242 OF_getproplen(phandle_t package, char *propname)
244 static struct {
245 cell_t name;
246 cell_t nargs;
247 cell_t nreturns;
248 cell_t package;
249 cell_t propname;
250 cell_t proplen;
251 } args = {
252 (cell_t)"getproplen",
260 args.package = (u_int)package;
261 args.propname = (cell_t)propname;
262 if (openfirmware(&args) == -1)
263 return -1;
264 return (int)args.proplen;
267 /* Get the value of a property of a package. */
269 OF_getprop(phandle_t package, char *propname, void *buf, int buflen)
271 static struct {
272 cell_t name;
273 cell_t nargs;
274 cell_t nreturns;
275 cell_t package;
276 cell_t propname;
277 cell_t buf;
278 cell_t buflen;
279 cell_t size;
280 } args = {
281 (cell_t)"getprop",
291 args.package = (u_int)package;
292 args.propname = (cell_t)propname;
293 args.buf = (cell_t)buf;
294 args.buflen = (u_int)buflen;
295 if (openfirmware(&args) == -1)
296 return -1;
297 return (int)args.size;
300 /* Get the next property of a package. */
302 OF_nextprop(phandle_t package, char *previous, char *buf)
304 static struct {
305 cell_t name;
306 cell_t nargs;
307 cell_t nreturns;
308 cell_t package;
309 cell_t previous;
310 cell_t buf;
311 cell_t flag;
312 } args = {
313 (cell_t)"nextprop",
322 args.package = (u_int)package;
323 args.previous = (cell_t)previous;
324 args.buf = (cell_t)buf;
325 if (openfirmware(&args) == -1)
326 return -1;
327 return (int)args.flag;
330 /* Set the value of a property of a package. */
331 /* XXX Has a bug on FirePower */
333 OF_setprop(phandle_t package, char *propname, void *buf, int len)
335 static struct {
336 cell_t name;
337 cell_t nargs;
338 cell_t nreturns;
339 cell_t package;
340 cell_t propname;
341 cell_t buf;
342 cell_t len;
343 cell_t size;
344 } args = {
345 (cell_t)"setprop",
355 args.package = (u_int)package;
356 args.propname = (cell_t)propname;
357 args.buf = (cell_t)buf;
358 args.len = (u_int)len;
359 if (openfirmware(&args) == -1)
360 return -1;
361 return (int)args.size;
364 /* Convert a device specifier to a fully qualified pathname. */
366 OF_canon(const char *device, char *buf, int len)
368 static struct {
369 cell_t name;
370 cell_t nargs;
371 cell_t nreturns;
372 cell_t device;
373 cell_t buf;
374 cell_t len;
375 cell_t size;
376 } args = {
377 (cell_t)"canon",
386 args.device = (cell_t)device;
387 args.buf = (cell_t)buf;
388 args.len = (cell_t)len;
389 if (openfirmware(&args) == -1)
390 return -1;
391 return (int)args.size;
394 /* Return a package handle for the specified device. */
395 phandle_t
396 OF_finddevice(const char *device)
398 int i;
399 static struct {
400 cell_t name;
401 cell_t nargs;
402 cell_t nreturns;
403 cell_t device;
404 cell_t package;
405 } args = {
406 (cell_t)"finddevice",
413 args.device = (cell_t)device;
414 if (openfirmware(&args) == -1)
415 return -1;
417 return (phandle_t)args.package;
420 /* Return the fully qualified pathname corresponding to an instance. */
422 OF_instance_to_path(ihandle_t instance, char *buf, int len)
424 static struct {
425 cell_t name;
426 cell_t nargs;
427 cell_t nreturns;
428 cell_t instance;
429 cell_t buf;
430 cell_t len;
431 cell_t size;
432 } args = {
433 (cell_t)"instance-to-path",
442 args.instance = (u_int)instance;
443 args.buf = (cell_t)buf;
444 args.len = (u_int)len;
445 if (openfirmware(&args) == -1)
446 return -1;
447 return (int)args.size;
450 /* Return the fully qualified pathname corresponding to a package. */
452 OF_package_to_path(phandle_t package, char *buf, int len)
454 static struct {
455 cell_t name;
456 cell_t nargs;
457 cell_t nreturns;
458 cell_t package;
459 cell_t buf;
460 cell_t len;
461 cell_t size;
462 } args = {
463 (cell_t)"package-to-path",
472 args.package = (u_int)package;
473 args.buf = (cell_t)buf;
474 args.len = (u_int)len;
475 if (openfirmware(&args) == -1)
476 return -1;
477 return (int)args.size;
480 /* Call the method in the scope of a given instance. */
482 OF_call_method(char *method, ihandle_t instance, int nargs, int nreturns, ...)
484 va_list ap;
485 static struct {
486 cell_t name;
487 cell_t nargs;
488 cell_t nreturns;
489 cell_t method;
490 cell_t instance;
491 cell_t args_n_results[12];
492 } args = {
493 (cell_t)"call-method",
498 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
500 int *ip, n;
502 if (nargs > 6)
503 return -1;
504 args.nargs = nargs + 2;
505 args.nreturns = nreturns + 1;
506 args.method = (cell_t)method;
507 args.instance = (u_int)instance;
508 va_start(ap, nreturns);
509 for (ip = (int *)(args.args_n_results + (n = nargs)); --n >= 0;)
510 *--ip = va_arg(ap, int);
512 if (openfirmware(&args) == -1)
513 return -1;
514 if (args.args_n_results[nargs])
515 return (int)args.args_n_results[nargs];
516 for (ip = (int *)(args.args_n_results + nargs + (n = args.nreturns));
517 --n > 0;)
518 *va_arg(ap, int *) = *--ip;
519 va_end(ap);
520 return 0;
524 * Device I/O functions.
527 /* Open an instance for a device. */
528 ihandle_t
529 OF_open(char *device)
531 static struct {
532 cell_t name;
533 cell_t nargs;
534 cell_t nreturns;
535 cell_t device;
536 cell_t instance;
537 } args = {
538 (cell_t)"open",
545 args.device = (cell_t)device;
546 if (openfirmware(&args) == -1 || args.instance == 0) {
547 return -1;
549 return (ihandle_t)args.instance;
552 /* Close an instance. */
553 void
554 OF_close(ihandle_t instance)
556 static struct {
557 cell_t name;
558 cell_t nargs;
559 cell_t nreturns;
560 cell_t instance;
561 } args = {
562 (cell_t)"close",
568 args.instance = (u_int)instance;
569 openfirmware(&args);
572 /* Read from an instance. */
574 OF_read(ihandle_t instance, void *addr, int len)
576 static struct {
577 cell_t name;
578 cell_t nargs;
579 cell_t nreturns;
580 cell_t instance;
581 cell_t addr;
582 cell_t len;
583 cell_t actual;
584 } args = {
585 (cell_t)"read",
594 args.instance = (u_int)instance;
595 args.addr = (cell_t)addr;
596 args.len = (u_int)len;
598 #if defined(OPENFIRM_DEBUG)
599 printf("OF_read: called with instance=%08x, addr=%p, len=%d\n",
600 args.instance, args.addr, args.len);
601 #endif
603 if (openfirmware(&args) == -1)
604 return -1;
606 #if defined(OPENFIRM_DEBUG)
607 printf("OF_read: returning instance=%d, addr=%p, len=%d, actual=%d\n",
608 args.instance, args.addr, args.len, args.actual);
609 #endif
611 return (int)args.actual;
614 /* Write to an instance. */
616 OF_write(ihandle_t instance, void *addr, int len)
618 static struct {
619 cell_t name;
620 cell_t nargs;
621 cell_t nreturns;
622 cell_t instance;
623 cell_t addr;
624 cell_t len;
625 cell_t actual;
626 } args = {
627 (cell_t)"write",
636 args.instance = (u_int)instance;
637 args.addr = (cell_t)addr;
638 args.len = (u_int)len;
639 if (openfirmware(&args) == -1)
640 return -1;
641 return (int)args.actual;
644 /* Seek to a position. */
646 OF_seek(ihandle_t instance, u_int64_t pos)
648 static struct {
649 cell_t name;
650 cell_t nargs;
651 cell_t nreturns;
652 cell_t instance;
653 cell_t poshi;
654 cell_t poslo;
655 cell_t status;
656 } args = {
657 (cell_t)"seek",
666 args.instance = (u_int)instance;
667 args.poshi = pos >> 32;
668 args.poslo = pos;
669 if (openfirmware(&args) == -1)
670 return -1;
671 return (int)args.status;
675 * Memory functions.
678 /* Claim an area of memory. */
679 void *
680 OF_claim(void *virt, u_int size, u_int align)
682 static struct {
683 cell_t name;
684 cell_t nargs;
685 cell_t nreturns;
686 cell_t virt;
687 cell_t size;
688 cell_t align;
689 cell_t baseaddr;
690 } args = {
691 (cell_t)"claim",
700 args.virt = (cell_t)virt;
701 args.size = size;
702 args.align = align;
703 if (openfirmware(&args) == -1)
704 return (void *)-1;
705 return (void *)args.baseaddr;
708 /* Allocate an area of physical memory */
709 vm_offset_t
710 OF_claim_virt(vm_offset_t virt, size_t size, int align)
712 static struct {
713 cell_t name;
714 cell_t nargs;
715 cell_t nret;
716 cell_t method;
717 cell_t ihandle;
718 cell_t align;
719 cell_t size;
720 cell_t virt;
721 cell_t status;
722 cell_t ret;
723 } args = {
724 (cell_t)"call-method",
727 (cell_t)"claim",
732 0, /* ret */
736 args.ihandle = mmu;
737 args.align = align;
738 args.size = size;
739 args.virt = virt;
741 if (openfirmware(&args) == -1)
742 return (vm_offset_t)-1;
744 return (vm_offset_t)args.ret;
747 /* Allocate an area of physical memory */
748 void *
749 OF_alloc_phys(size_t size, int align)
751 static struct {
752 cell_t name;
753 cell_t nargs;
754 cell_t nret;
755 cell_t method;
756 cell_t ihandle;
757 cell_t align;
758 cell_t size;
759 cell_t status;
760 cell_t phys_hi;
761 cell_t phys_low;
762 } args = {
763 (cell_t)"call-method",
766 (cell_t)"claim",
770 0, /* ret */
775 args.ihandle = memory;
776 args.size = size;
777 args.align = align;
779 if (openfirmware(&args) == -1)
780 return (void *)-1;
782 return (void *)(args.phys_hi << 32 | args.phys_low);
785 /* Release an area of memory. */
786 void
787 OF_release(void *virt, u_int size)
789 static struct {
790 cell_t name;
791 cell_t nargs;
792 cell_t nreturns;
793 cell_t virt;
794 cell_t size;
795 } args = {
796 (cell_t)"release",
803 args.virt = (cell_t)virt;
804 args.size = size;
805 openfirmware(&args);
808 /* Release an area of physical memory. */
809 void
810 OF_release_phys(vm_offset_t phys, u_int size)
812 static struct {
813 cell_t name;
814 cell_t nargs;
815 cell_t nret;
816 cell_t method;
817 cell_t ihandle;
818 cell_t size;
819 cell_t phys_hi;
820 cell_t phys_lo;
821 } args = {
822 (cell_t)"call-method",
825 (cell_t)"release",
832 args.ihandle = memory;
833 args.phys_hi = (u_int32_t)(phys >> 32);
834 args.phys_lo = (u_int32_t)phys;
835 args.size = size;
836 openfirmware(&args);
840 * Control transfer functions.
843 /* Reset the system and call "boot <bootspec>". */
844 void
845 OF_boot(char *bootspec)
847 static struct {
848 cell_t name;
849 cell_t nargs;
850 cell_t nreturns;
851 cell_t bootspec;
852 } args = {
853 (cell_t)"boot",
859 args.bootspec = (cell_t)bootspec;
860 openfirmware(&args);
861 for (;;); /* just in case */
864 /* Suspend and drop back to the OpenFirmware interface. */
865 void
866 OF_enter()
868 static struct {
869 cell_t name;
870 cell_t nargs;
871 cell_t nreturns;
872 } args = {
873 (cell_t)"enter",
878 openfirmware(&args);
881 /* Shut down and drop back to the OpenFirmware interface. */
882 void
883 OF_exit()
885 static struct {
886 cell_t name;
887 cell_t nargs;
888 cell_t nreturns;
889 } args = {
890 (cell_t)"exit",
895 openfirmware(&args);
896 for (;;); /* just in case */
899 /* Free <size> bytes starting at <virt>, then call <entry> with <arg>. */
900 #ifdef __notyet__
901 void
902 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
904 static struct {
905 cell_t name;
906 cell_t nargs;
907 cell_t nreturns;
908 cell_t virt;
909 cell_t size;
910 cell_t entry;
911 cell_t arg;
912 cell_t len;
913 } args = {
914 (cell_t)"chain",
924 args.virt = (cell_t)virt;
925 args.size = size;
926 args.entry = (cell_t)entry;
927 args.arg = (cell_t)arg;
928 args.len = len;
929 openfirmware(&args);
931 #else
932 void
933 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
936 * This is a REALLY dirty hack till the firmware gets this going
938 if (size > 0)
939 OF_release(virt, size);
941 entry(0, 0, openfirmware, arg, len);
943 #endif