boot.c revision 1.3 1 /* $NetBSD: boot.c,v 1.3 2004/01/07 12:43:44 cdi Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jonathan Stone, Michael Hitch, Simon Burge and Wayne Knowles.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1992, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * Ralph Campbell.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)boot.c 8.1 (Berkeley) 6/10/93
71 */
72
73 #include <machine/cpu.h>
74 #include <machine/leds.h>
75 #include <machine/vmparam.h>
76
77 #include <lib/libsa/stand.h>
78 #include <lib/libsa/loadfile.h>
79 #include <lib/libkern/libkern.h>
80
81 #include <sys/param.h>
82 #include <sys/exec.h>
83 #include <sys/exec_elf.h>
84
85 #include "boot.h"
86 #include "cons.h"
87 #include "common.h"
88 #include "bootinfo.h"
89
90 char *kernelnames[] = {
91 "netbsd",
92 "netbsd.gz",
93 "onetbsd",
94 "onetbsd.gz",
95 "netbsd.bak",
96 "netbsd.bak.gz",
97 "netbsd.old",
98 "netbsd.old.gz",
99 "netbsd.cobalt",
100 "netbsd.cobalt.gz",
101 "netbsd.elf",
102 "netbsd.elf.gz",
103 NULL
104 };
105
106 extern u_long end; /* Boot loader code end address */
107 void start(void);
108
109 static char *bootstring;
110
111 static int patch_bootstring (char *bootspec);
112 static int get_bsdbootname (char **dev, char **name, char **kname);
113 static int prominit (unsigned int memsize);
114 static int print_banner (unsigned int memsize);
115
116 int cpu_reboot(void);
117
118 int main(unsigned int memsize);
119
120 /*
121 * Perform CPU reboot.
122 */
123 int
124 cpu_reboot()
125 {
126 printf("rebooting...\n\n");
127
128 *(volatile char *)MIPS_PHYS_TO_KSEG1(LED_ADDR) = LED_RESET;
129 printf("WARNING: reboot failed!\n");
130
131 for (;;);
132 }
133
134 /*
135 * Substitute root value with NetBSD root partition name.
136 */
137 int
138 patch_bootstring(bootspec)
139 char *bootspec;
140 {
141 char *sp = bootstring;
142 u_int8_t unit, part;
143 int dev, error;
144 char *file;
145
146 DPRINTF(("patch_bootstring: %s\n", bootspec));
147
148 /* get boot parameters */
149 if (devparse(bootspec, &dev, &unit, &part, (const char **)&file) != 0)
150 unit = part = 0;
151
152 DPRINTF(("patch_bootstring: %d, %d\n", unit, part));
153
154 /* take out the 'root=xxx' parameter */
155 if ( (sp = strstr(bootstring, "root=")) != NULL) {
156 const char *end;
157
158 end = strchr(sp, ' ');
159
160 /* strip off leading spaces */
161 for (--sp; (sp > bootstring) && (*sp == ' '); --sp)
162 ;
163
164 if (end != NULL)
165 strcpy(++sp, end);
166 else
167 *++sp = '\0';
168 }
169
170 DPRINTF(("patch_bootstring: [%s]\n", bootstring));
171
172 #define DEVNAMESIZE (MAXDEVNAME + sizeof(" root=/dev/hd") + sizeof("0a"))
173 /* bsd notation -> linux notation (wd0a -> hda1) */
174 if (strlen(bootstring) <= (511 - DEVNAMESIZE)) {
175 int len;
176
177 strcat(bootstring, " root=/dev/hd");
178
179 len = strlen(bootstring);
180 bootstring[len++] = unit + 'a';
181 bootstring[len++] = part + '1';
182 bootstring[len++] = '\0';
183 }
184
185 DPRINTF(("patch_bootstring: -> %s\n", bootstring));
186 return (0);
187 }
188
189 /*
190 * Extract NetBSD boot specification
191 */
192 int
193 get_bsdbootname(dev, name, kname)
194 char **dev;
195 char **name;
196 char **kname;
197 {
198 char *spec;
199
200 *dev = NULL;
201 *name = NULL;
202 *kname = NULL;
203
204 if ( (spec = strstr(bootstring, "nbsd=")) != NULL) {
205 int len;
206 char *ptr = strchr(spec, ' ');
207
208 spec += 5; /* skip 'nbsd=' */
209 len = (ptr == NULL) ? strlen(spec) : ptr - spec;
210
211 if (len > 0) {
212 char *devname = alloc(len + 1);
213 if (devname != NULL) {
214 memcpy(devname, spec, len);
215 devname[len] = '\0';
216
217 if ( (ptr = memchr(devname,':',len)) != NULL) {
218 /* wdXX:kernel */
219 *ptr = '\0';
220 *dev = devname;
221
222 if (*++ptr)
223 *kname = ptr;
224
225 devname = alloc(len + 1);
226 if (devname != NULL) {
227 memcpy(devname, spec, len);
228 devname[len] = '\0';
229 }
230 }
231
232 *name = devname;
233 return (0);
234 }
235 }
236 }
237
238 return (1);
239 }
240
241 /*
242 * Get the bootstring from PROM.
243 */
244 int
245 prominit(memsize)
246 unsigned int memsize;
247 {
248 bootstring = (char *)(memsize - 512);
249 bootstring[511] = '\0';
250 }
251
252 /*
253 * Print boot message.
254 */
255 int
256 print_banner(memsize)
257 unsigned int memsize;
258 {
259 printf("\n");
260 printf(">> %s " NETBSD_VERS " Bootloader, Revision %s [@%p]\n",
261 bootprog_name, bootprog_rev, (void*)&start);
262 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
263 printf(">> Memory:\t\t%u k\n", (memsize - MIPS_KSEG0_START) / 1024);
264 printf(">> PROM boot string:\t%s\n", bootstring);
265 }
266
267 /*
268 * Entry point.
269 * Parse PROM boot string, load the kernel and jump into it
270 */
271 int
272 main(memsize)
273 unsigned int memsize;
274 {
275 char *name, **namep, *dev, *kernel, *spec, *bi_addr;
276 char bootpath[PATH_MAX];
277 int win;
278 u_long marks[MARK_MAX];
279 void (*entry) __P((unsigned int, u_int, char*));
280
281 struct btinfo_flags bi_flags;
282 struct btinfo_symtab bi_syms;
283 struct btinfo_bootpath bi_bpath;
284
285 int addr, speed;
286
287 /* Initialize boot info early */
288 bi_flags.bi_flags = 0x0;
289 bi_addr = bi_init();
290
291 prominit(memsize);
292 if (cninit(&addr, &speed) != NULL)
293 bi_flags.bi_flags |= BI_SERIAL_CONSOLE;
294
295 print_banner(memsize);
296
297 memset(marks, 0, sizeof marks);
298 get_bsdbootname(&dev, &name, &kernel);
299
300 if (kernel != NULL) {
301 DPRINTF(("kernel: %s\n", kernel));
302 patch_bootstring(name);
303 win = (loadfile(name, marks, LOAD_KERNEL) == 0);
304 } else {
305 win = 0;
306 DPRINTF(("kernel: NULL\n"));
307 DPRINTF(("Kernel names: %p\n", kernelnames));
308 for (namep = kernelnames, win = 0;
309 (*namep != NULL) && !win;
310 namep++) {
311 kernel = *namep;
312
313 bootpath[0] = '\0';
314
315 strcpy(bootpath, (dev != NULL) ? dev : "wd0a");
316 strcat(bootpath, ":");
317 strcat(bootpath, kernel);
318
319 printf("Loading: %s\n", bootpath);
320 patch_bootstring(bootpath);
321 win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
322 }
323 }
324
325 if (win) {
326 strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
327 bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
328
329 entry = (void *) marks[MARK_ENTRY];
330 bi_syms.nsym = marks[MARK_NSYM];
331 bi_syms.ssym = marks[MARK_SYM];
332 bi_syms.esym = marks[MARK_END];
333 bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
334
335 bi_add(&bi_flags, BTINFO_FLAGS, sizeof(bi_flags));
336
337 entry = (void *) marks[MARK_ENTRY];
338
339 DPRINTF(("Bootinfo @ 0x%x\n", bi_addr));
340 printf("Starting at 0x%x\n\n", (u_int)entry);
341 (*entry)(memsize, BOOTINFO_MAGIC, bi_addr);
342 }
343
344 (void)printf("Boot failed! Rebooting...\n");
345 return (0);
346 }
347