]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/ofw/common/main.c
Re-sync loader.mk and ficl.mk to where they should be
[FreeBSD/FreeBSD.git] / sys / boot / ofw / common / main.c
1 /*-
2  * Copyright (c) 2000 Benno Rice <benno@jeamland.net>
3  * Copyright (c) 2000 Stephane Potvin <sepotvin@videotron.ca>
4  * All rights reserved.
5  *
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <stand.h>
32 #include "openfirm.h"
33 #include "libofw.h"
34 #include "bootstrap.h"
35
36 struct arch_switch      archsw;         /* MI/MD interface boundary */
37
38 extern char end[];
39 extern char bootprog_info[];
40
41 u_int32_t       acells, scells;
42
43 static char bootargs[128];
44
45 #define HEAP_SIZE       0x100000
46
47 #define OF_puts(fd, text) OF_write(fd, text, strlen(text))
48
49 void
50 init_heap(void)
51 {
52         void    *base;
53         ihandle_t stdout;
54
55         if ((base = ofw_alloc_heap(HEAP_SIZE)) == (void *)0xffffffff) {
56                 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
57                 OF_puts(stdout, "Heap memory claim failed!\n");
58                 OF_enter();
59         }
60
61         setheap(base, (void *)((int)base + HEAP_SIZE));
62 }
63
64 uint64_t
65 memsize(void)
66 {
67         phandle_t       memoryp;
68         cell_t          reg[24];
69         int             i, sz;
70         u_int64_t       memsz;
71
72         memsz = 0;
73         memoryp = OF_instance_to_package(memory);
74
75         sz = OF_getprop(memoryp, "reg", &reg, sizeof(reg));
76         sz /= sizeof(reg[0]);
77
78         for (i = 0; i < sz; i += (acells + scells)) {
79                 if (scells > 1)
80                         memsz += (uint64_t)reg[i + acells] << 32;
81                 memsz += reg[i + acells + scells - 1];
82         }
83
84         return (memsz);
85 }
86
87 int
88 main(int (*openfirm)(void *))
89 {
90         phandle_t       root;
91         int             i;
92         char            bootpath[64];
93         char            *ch;
94         int             bargc;
95         char            **bargv;
96
97         /*
98          * Initialise the Open Firmware routines by giving them the entry point.
99          */
100         OF_init(openfirm);
101
102         root = OF_finddevice("/");
103
104         scells = acells = 1;
105         OF_getprop(root, "#address-cells", &acells, sizeof(acells));
106         OF_getprop(root, "#size-cells", &scells, sizeof(scells));
107
108         /*
109          * Initialise the heap as early as possible.  Once this is done,
110          * alloc() is usable. The stack is buried inside us, so this is
111          * safe.
112          */
113         init_heap();
114
115         /*
116          * Set up console.
117          */
118         cons_probe();
119
120         /*
121          * March through the device switch probing for things.
122          */
123         for (i = 0; devsw[i] != NULL; i++)
124                 if (devsw[i]->dv_init != NULL)
125                         (devsw[i]->dv_init)();
126
127         printf("\n%s", bootprog_info);
128         printf("Memory: %lldKB\n", memsize() / 1024);
129
130         OF_getprop(chosen, "bootpath", bootpath, 64);
131         ch = strchr(bootpath, ':');
132         *ch = '\0';
133         printf("Booted from: %s\n", bootpath);
134
135         printf("\n");
136
137         /*
138          * Only parse the first bootarg if present. It should
139          * be simple to handle extra arguments
140          */
141         OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
142         bargc = 0;
143         parse(&bargc, &bargv, bootargs);
144         if (bargc == 1)
145                 env_setenv("currdev", EV_VOLATILE, bargv[0], ofw_setcurrdev,
146                     env_nounset);
147         else
148                 env_setenv("currdev", EV_VOLATILE, bootpath,
149                            ofw_setcurrdev, env_nounset);
150         env_setenv("loaddev", EV_VOLATILE, bootpath, env_noset,
151             env_nounset);
152         setenv("LINES", "24", 1);               /* optional */
153
154         archsw.arch_getdev = ofw_getdev;
155         archsw.arch_copyin = ofw_copyin;
156         archsw.arch_copyout = ofw_copyout;
157         archsw.arch_readin = ofw_readin;
158         archsw.arch_autoload = ofw_autoload;
159
160         interact(NULL);                         /* doesn't return */
161
162         OF_exit();
163
164         return 0;
165 }
166
167 COMMAND_SET(halt, "halt", "halt the system", command_halt);
168
169 static int
170 command_halt(int argc, char *argv[])
171 {
172
173         OF_exit();
174         return (CMD_OK);
175 }
176
177 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
178
179 int
180 command_memmap(int argc, char **argv)
181 {
182
183         ofw_memmap(acells);
184         return (CMD_OK);
185 }