boot.c revision 1.16 1 1.16 pk /* $NetBSD: boot.c,v 1.16 2003/07/10 09:06:52 pk Exp $ */
2 1.1 mrg
3 1.1 mrg /*-
4 1.1 mrg * Copyright (c) 1982, 1986, 1990, 1993
5 1.1 mrg * The Regents of the University of California. All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg * 3. All advertising materials mentioning features or use of this software
16 1.1 mrg * must display the following acknowledgement:
17 1.1 mrg * This product includes software developed by the University of
18 1.1 mrg * California, Berkeley and its contributors.
19 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
20 1.1 mrg * may be used to endorse or promote products derived from this software
21 1.1 mrg * without specific prior written permission.
22 1.1 mrg *
23 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 mrg * SUCH DAMAGE.
34 1.1 mrg *
35 1.1 mrg * @(#)boot.c 8.1 (Berkeley) 6/10/93
36 1.1 mrg */
37 1.1 mrg
38 1.1 mrg #include <sys/param.h>
39 1.1 mrg #include <sys/reboot.h>
40 1.12 uwe #include <sys/boot_flag.h>
41 1.9 pk #include <sys/exec.h>
42 1.1 mrg
43 1.1 mrg #include <lib/libsa/stand.h>
44 1.5 christos #include <lib/libsa/loadfile.h>
45 1.1 mrg
46 1.4 pk #include <machine/promlib.h>
47 1.1 mrg #include <sparc/stand/common/promdev.h>
48 1.1 mrg
49 1.6 christos #include "bootinfo.h"
50 1.6 christos
51 1.10 uwe extern void prom_patch __P((void)); /* prompatch.c */
52 1.10 uwe
53 1.4 pk static int bootoptions __P((char *));
54 1.4 pk
55 1.14 pk int boothowto;
56 1.14 pk int debug;
57 1.14 pk int netif_debug;
58 1.14 pk
59 1.14 pk char fbuf[80], dbuf[128];
60 1.15 pk paddr_t bstart, bend; /* physical start & end address of the boot program */
61 1.15 pk
62 1.15 pk int compatmode = 0; /* For loading older kernels */
63 1.15 pk u_long loadaddrmask = -1UL;
64 1.1 mrg
65 1.1 mrg extern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
66 1.1 mrg
67 1.4 pk int main __P((void));
68 1.1 mrg typedef void (*entry_t)__P((caddr_t, int, int, int, long, long));
69 1.1 mrg
70 1.3 mrg /*
71 1.3 mrg * Boot device is derived from ROM provided information, or if there is none,
72 1.3 mrg * this list is used in sequence, to find a kernel.
73 1.3 mrg */
74 1.3 mrg char *kernels[] = {
75 1.3 mrg "netbsd",
76 1.3 mrg "netbsd.gz",
77 1.3 mrg "netbsd.old",
78 1.3 mrg "netbsd.old.gz",
79 1.3 mrg "onetbsd",
80 1.3 mrg "onetbsd.gz",
81 1.3 mrg "vmunix",
82 1.3 mrg #ifdef notyet
83 1.3 mrg "netbsd.pl",
84 1.3 mrg "netbsd.pl.gz",
85 1.3 mrg "netbsd.el",
86 1.3 mrg "netbsd.el.gz",
87 1.3 mrg #endif
88 1.3 mrg NULL
89 1.3 mrg };
90 1.3 mrg
91 1.4 pk int
92 1.4 pk bootoptions(ap)
93 1.4 pk char *ap;
94 1.4 pk {
95 1.4 pk int v = 0;
96 1.4 pk if (ap == NULL || *ap++ != '-')
97 1.4 pk return (0);
98 1.4 pk
99 1.4 pk while (*ap != '\0' && *ap != ' ' && *ap != '\t' && *ap != '\n') {
100 1.12 uwe BOOT_FLAG(*ap, v);
101 1.16 pk if (*ap == 'C')
102 1.16 pk compatmode = 1;
103 1.4 pk ap++;
104 1.4 pk }
105 1.12 uwe
106 1.12 uwe if ((v & RB_KDB) != 0)
107 1.12 uwe debug = 1;
108 1.4 pk
109 1.4 pk return (v);
110 1.4 pk }
111 1.4 pk
112 1.15 pk static paddr_t getphysmem(u_long size)
113 1.15 pk {
114 1.15 pk struct memarr *pmemarr; /* physical memory regions */
115 1.15 pk int npmemarr; /* number of entries in pmemarr */
116 1.15 pk struct memarr *mp;
117 1.15 pk int i;
118 1.15 pk extern char start[]; /* top of stack (see srt0.S) */
119 1.15 pk
120 1.15 pk /*
121 1.15 pk * Find the physical memory area that's in use by the boot loader.
122 1.15 pk * Our stack grows down from label `start'; assume we need no more
123 1.15 pk * than 16K of stack space.
124 1.15 pk * The top of the boot loader is the next 4MB boundary.
125 1.15 pk */
126 1.15 pk if (pmap_extract((vaddr_t)start - (16*1024), &bstart) != 0)
127 1.15 pk return ((paddr_t)-1);
128 1.15 pk
129 1.15 pk bend = roundup(bstart, 0x400000);
130 1.15 pk
131 1.15 pk /*
132 1.15 pk * Get available physical memory from the prom.
133 1.15 pk */
134 1.15 pk npmemarr = prom_makememarr(NULL, 0, MEMARR_AVAILPHYS);
135 1.15 pk pmemarr = alloc(npmemarr*sizeof(struct memarr));
136 1.15 pk if (pmemarr == NULL)
137 1.15 pk return ((paddr_t)-1);
138 1.15 pk npmemarr = prom_makememarr(pmemarr, npmemarr, MEMARR_AVAILPHYS);
139 1.15 pk
140 1.15 pk /*
141 1.15 pk * Find a suitable loading address.
142 1.15 pk */
143 1.15 pk for (mp = pmemarr, i = npmemarr; --i >= 0; mp++) {
144 1.15 pk paddr_t pa = (paddr_t)pmemarr[i].addr;
145 1.15 pk u_long len = (u_long)pmemarr[i].len;
146 1.15 pk
147 1.15 pk /* Check whether it will fit in front of us */
148 1.15 pk if (pa < bstart && len >= size && (bstart - pa) >= size)
149 1.15 pk return (pa);
150 1.15 pk
151 1.15 pk /* Skip the boot program memory */
152 1.15 pk if (pa < bend) {
153 1.15 pk if (len < bend - pa)
154 1.15 pk /* Not large enough */
155 1.15 pk continue;
156 1.15 pk
157 1.15 pk /* Shrink this segment */
158 1.15 pk len -= bend - pa;
159 1.15 pk pa = bend;
160 1.15 pk }
161 1.15 pk
162 1.15 pk /* Does it fit in the remainder of this segment? */
163 1.15 pk if (len >= size)
164 1.15 pk return (pa);
165 1.15 pk }
166 1.15 pk return ((paddr_t)-1);
167 1.15 pk }
168 1.15 pk
169 1.14 pk static int
170 1.14 pk loadk(char *kernel, u_long *marks)
171 1.14 pk {
172 1.14 pk int fd, error;
173 1.15 pk vaddr_t va;
174 1.15 pk paddr_t pa;
175 1.14 pk u_long size;
176 1.14 pk
177 1.14 pk if ((fd = open(kernel, 0)) < 0)
178 1.14 pk return (errno ? errno : ENOENT);
179 1.14 pk
180 1.14 pk marks[MARK_START] = 0;
181 1.14 pk if ((error = fdloadfile(fd, marks, COUNT_KERNEL)) != 0)
182 1.14 pk goto out;
183 1.14 pk
184 1.14 pk size = marks[MARK_END] - marks[MARK_START];
185 1.15 pk
186 1.16 pk /* We want that leading 16K in front of the kernel image */
187 1.15 pk size += PROM_LOADADDR;
188 1.15 pk va = marks[MARK_START] - PROM_LOADADDR;
189 1.15 pk
190 1.16 pk /*
191 1.16 pk * Extra space for bootinfo and kernel bootstrap.
192 1.16 pk * In compat mode, we get to re-use the space occupied by the
193 1.16 pk * boot program. Traditionally, we've silently assumed that
194 1.16 pk * is enough for the kernel to work with.
195 1.16 pk */
196 1.16 pk size += BOOTINFO_SIZE;
197 1.16 pk if (!compatmode)
198 1.16 pk size += 512 * 1024;
199 1.15 pk
200 1.15 pk /* Get a physical load address */
201 1.15 pk pa = getphysmem(size);
202 1.15 pk if (pa == (paddr_t)-1) {
203 1.14 pk error = EFBIG;
204 1.14 pk goto out;
205 1.14 pk }
206 1.14 pk
207 1.15 pk if (boothowto & AB_VERBOSE)
208 1.15 pk printf("Loading at physical address %lx\n", pa);
209 1.15 pk if (pmap_map(va, pa, size) != 0) {
210 1.15 pk error = EFAULT;
211 1.15 pk goto out;
212 1.15 pk }
213 1.15 pk
214 1.15 pk /* XXX - to do: inspect kernel image and set compat mode */
215 1.15 pk if (compatmode) {
216 1.16 pk /* Double-map at VA 0 for compatibility */
217 1.16 pk if (pa + size >= bstart) {
218 1.16 pk printf("%s: too large for compat mode\n", kernel);
219 1.16 pk error = EFBIG;
220 1.16 pk goto out;
221 1.16 pk }
222 1.16 pk
223 1.16 pk if (pmap_map(0, pa, size) != 0) {
224 1.16 pk error = EFAULT;
225 1.16 pk goto out;
226 1.16 pk }
227 1.15 pk loadaddrmask = 0x07ffffffUL;
228 1.15 pk }
229 1.15 pk
230 1.14 pk marks[MARK_START] = 0;
231 1.14 pk error = fdloadfile(fd, marks, LOAD_KERNEL);
232 1.14 pk out:
233 1.14 pk close(fd);
234 1.14 pk return (error);
235 1.14 pk }
236 1.14 pk
237 1.4 pk int
238 1.1 mrg main()
239 1.1 mrg {
240 1.14 pk int error, i;
241 1.3 mrg char *kernel;
242 1.6 christos u_long marks[MARK_MAX], bootinfo;
243 1.6 christos struct btinfo_symtab bi_sym;
244 1.5 christos void *arg;
245 1.1 mrg
246 1.8 pk #ifdef HEAP_VARIABLE
247 1.8 pk {
248 1.8 pk extern char end[];
249 1.8 pk setheap((void *)ALIGN(end), (void *)0xffffffff);
250 1.8 pk }
251 1.8 pk #endif
252 1.1 mrg prom_init();
253 1.15 pk mmu_init();
254 1.1 mrg
255 1.1 mrg printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
256 1.1 mrg printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
257 1.10 uwe
258 1.10 uwe /* massage machine prom */
259 1.10 uwe prom_patch();
260 1.1 mrg
261 1.3 mrg /*
262 1.3 mrg * get default kernel.
263 1.3 mrg */
264 1.4 pk prom_bootdevice = prom_getbootpath();
265 1.4 pk kernel = prom_getbootfile();
266 1.14 pk boothowto = bootoptions(prom_getbootargs());
267 1.4 pk
268 1.4 pk if (kernel != NULL && *kernel != '\0') {
269 1.3 mrg i = -1; /* not using the kernels */
270 1.3 mrg } else {
271 1.3 mrg i = 0;
272 1.3 mrg kernel = kernels[i];
273 1.3 mrg }
274 1.1 mrg
275 1.1 mrg for (;;) {
276 1.3 mrg /*
277 1.3 mrg * ask for a kernel first ..
278 1.3 mrg */
279 1.14 pk if (boothowto & RB_ASKNAME) {
280 1.3 mrg printf("device[%s] (\"halt\" to halt): ",
281 1.4 pk prom_bootdevice);
282 1.1 mrg gets(dbuf);
283 1.3 mrg if (strcmp(dbuf, "halt") == 0)
284 1.3 mrg _rtt();
285 1.1 mrg if (dbuf[0])
286 1.1 mrg prom_bootdevice = dbuf;
287 1.3 mrg printf("boot (press RETURN to try default list): ");
288 1.1 mrg gets(fbuf);
289 1.1 mrg if (fbuf[0])
290 1.3 mrg kernel = fbuf;
291 1.3 mrg else {
292 1.14 pk boothowto &= ~RB_ASKNAME;
293 1.3 mrg i = 0;
294 1.3 mrg kernel = kernels[i];
295 1.3 mrg }
296 1.1 mrg }
297 1.3 mrg
298 1.5 christos printf("Booting %s\n", kernel);
299 1.14 pk if ((error = loadk(kernel, marks)) == 0)
300 1.14 pk break;
301 1.14 pk
302 1.14 pk if (error != ENOENT) {
303 1.14 pk printf("Cannot load %s: error=%d\n", kernel, error);
304 1.14 pk boothowto |= RB_ASKNAME;
305 1.13 pk }
306 1.13 pk
307 1.3 mrg /*
308 1.3 mrg * if we have are not in askname mode, and we aren't using the
309 1.3 mrg * prom bootfile, try the next one (if it exits). otherwise,
310 1.3 mrg * go into askname mode.
311 1.3 mrg */
312 1.14 pk if ((boothowto & RB_ASKNAME) == 0 &&
313 1.3 mrg i != -1 && kernels[++i]) {
314 1.3 mrg kernel = kernels[i];
315 1.3 mrg printf(": trying %s...\n", kernel);
316 1.3 mrg } else {
317 1.3 mrg printf("\n");
318 1.14 pk boothowto |= RB_ASKNAME;
319 1.3 mrg }
320 1.1 mrg }
321 1.1 mrg
322 1.15 pk marks[MARK_END] = (((u_long)marks[MARK_END] + sizeof(u_long) - 1)) &
323 1.15 pk (-sizeof(u_long));
324 1.7 christos arg = (prom_version() == PROM_OLDMON) ? (caddr_t)PROM_LOADADDR : romp;
325 1.14 pk
326 1.15 pk /* Setup boot info structure at the end of the kernel image */
327 1.15 pk bootinfo = bi_init(marks[MARK_END] & loadaddrmask);
328 1.11 pk
329 1.15 pk /* Add kernel symbols to bootinfo */
330 1.15 pk bi_sym.nsym = marks[MARK_NSYM] & loadaddrmask;
331 1.15 pk bi_sym.ssym = marks[MARK_SYM] & loadaddrmask;
332 1.15 pk bi_sym.esym = marks[MARK_END] & loadaddrmask;
333 1.6 christos bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym));
334 1.11 pk
335 1.15 pk /* Add kernel path to bootinfo */
336 1.11 pk i = sizeof(struct btinfo_common) + strlen(kernel) + 1;
337 1.11 pk /* Impose limit (somewhat arbitrary) */
338 1.11 pk if (i < BOOTINFO_SIZE / 2) {
339 1.11 pk union {
340 1.11 pk struct btinfo_kernelfile bi_file;
341 1.11 pk char x[i];
342 1.11 pk } U;
343 1.11 pk strcpy(U.bi_file.name, kernel);
344 1.11 pk bi_add(&U.bi_file, BTINFO_KERNELFILE, i);
345 1.11 pk }
346 1.11 pk
347 1.6 christos (*(entry_t)marks[MARK_ENTRY])(arg, 0, 0, 0, bootinfo, DDB_MAGIC2);
348 1.1 mrg _rtt();
349 1.1 mrg }
350