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