boot.c revision 1.8 1 /* $NetBSD: boot.c,v 1.8 2007/08/13 02:09:01 tsutsui 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
76 #include <lib/libsa/stand.h>
77 #include <lib/libsa/loadfile.h>
78 #include <lib/libkern/libkern.h>
79
80 #include <sys/param.h>
81 #include <sys/boot_flag.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 **, char **, int *);
113 static int parse_bootname(char *, int, char **, char **);
114 static int prominit (unsigned int memsize);
115 static int print_banner (unsigned int memsize);
116
117 int cpu_reboot(void);
118
119 int main(unsigned int memsize);
120
121 /*
122 * Perform CPU reboot.
123 */
124 int
125 cpu_reboot(void)
126 {
127 printf("rebooting...\n\n");
128
129 *(volatile uint8_t *)MIPS_PHYS_TO_KSEG1(LED_ADDR) = LED_RESET;
130 printf("WARNING: reboot failed!\n");
131
132 for (;;)
133 ;
134 }
135
136 /*
137 * Substitute root value with NetBSD root partition name.
138 */
139 int
140 patch_bootstring(char *bootspec)
141 {
142 char *sp = bootstring;
143 uint8_t unit, part;
144 int dev, error;
145 char *file;
146
147 DPRINTF(("patch_bootstring: %s\n", bootspec));
148
149 /* get boot parameters */
150 if (devparse(bootspec, &dev, &unit, &part, (const char **)&file) != 0)
151 unit = part = 0;
152
153 DPRINTF(("patch_bootstring: %d, %d\n", unit, part));
154
155 /* take out the 'root=xxx' parameter */
156 if ((sp = strstr(bootstring, "root=")) != NULL) {
157 const char *end;
158
159 end = strchr(sp, ' ');
160
161 /* strip off leading spaces */
162 for (--sp; (sp > bootstring) && (*sp == ' '); --sp)
163 ;
164
165 if (end != NULL)
166 strcpy(++sp, end);
167 else
168 *++sp = '\0';
169 }
170
171 DPRINTF(("patch_bootstring: [%s]\n", bootstring));
172
173 #define DEVNAMESIZE (MAXDEVNAME + sizeof(" root=/dev/hd") + sizeof("0a"))
174 /* bsd notation -> linux notation (wd0a -> hda1) */
175 if (strlen(bootstring) <= (511 - DEVNAMESIZE)) {
176 int len;
177
178 strcat(bootstring, " root=/dev/hd");
179
180 len = strlen(bootstring);
181 bootstring[len++] = unit + 'a';
182 bootstring[len++] = part + '1';
183 bootstring[len++] = '\0';
184 }
185
186 DPRINTF(("patch_bootstring: -> %s\n", bootstring));
187 return 0;
188 }
189
190 /*
191 * Extract NetBSD boot specification
192 */
193 static int
194 get_bsdbootname(char **dev, char **kname, int *howtop)
195 {
196 int len, error;
197 int bootunit, bootpart;
198 char *bootstr_dev, *bootstr_kname;
199 char *prompt_dev, *prompt_kname;
200 char *ptr, *spec;
201 char c, namebuf[PATH_MAX];
202 static char bootdev[] = "wd0a";
203
204 bootstr_dev = prompt_dev = NULL;
205 bootstr_kname = prompt_kname = NULL;
206
207 /* first, get root device specified by the firmware */
208 spec = bootstring;
209 /* assume the last one is valid */
210 while ((spec = strstr(spec, "root=")) != NULL) {
211 spec += 5; /* skip 'root=' */
212 ptr = strchr(spec, ' ');
213 len = (ptr == NULL) ? strlen(spec) : ptr - spec;
214 /* decode unit and part from "/dev/hd[ab][1-4]" strings */
215 if (len == 9 && memcmp("/dev/hd", spec, 7) == 0) {
216 bootunit = spec[7] - 'a';
217 bootpart = spec[8] - '1';
218 if (bootunit >= 0 && bootunit < 2 &&
219 bootpart >= 0 && bootpart < 4) {
220 bootdev[sizeof(bootdev) - 3] = '0' + bootunit;
221 #if 0 /* bootpart is fdisk partition of Linux root */
222 bootdev[sizeof(bootdev) - 2] = 'a' + bootpart;
223 #endif
224 bootstr_dev = bootdev;
225 }
226 }
227 spec += len;
228 }
229
230 /* second, get bootname from bootstrings */
231 if ((spec = strstr(bootstring, "nbsd=")) != NULL) {
232 ptr = strchr(spec, ' ');
233 spec += 5; /* skip 'nbsd=' */
234 len = (ptr == NULL) ? strlen(spec) : ptr - spec;
235 if (len > 0) {
236 if (parse_bootname(spec, len,
237 &bootstr_dev, &bootstr_kname))
238 return 1;
239 }
240 }
241
242 DPRINTF(("bootstr_dev = %s, bootstr_kname = %s\n",
243 bootstr_dev ? bootstr_dev : "<NULL>",
244 bootstr_kname ? bootstr_kname : "<NULL>"));
245
246 spec = NULL;
247 len = 0;
248
249 memset(namebuf, 0, sizeof namebuf);
250 printf("Boot [%s:%s]: ",
251 bootstr_dev ? bootstr_dev : DEFBOOTDEV,
252 bootstr_kname ? bootstr_kname : DEFKERNELNAME);
253
254 if (tgets(namebuf) == -1)
255 printf("\n");
256
257 ptr = namebuf;
258 while ((c = *ptr) != '\0') {
259 while (c == ' ')
260 c = *++ptr;
261 if (c == '\0')
262 break;
263 if (c == '-') {
264 while ((c = *++ptr) && c != ' ')
265 BOOT_FLAG(c, *howtop);
266 } else {
267 spec = ptr;
268 while ((c = *++ptr) && c != ' ')
269 ;
270 if (c)
271 *ptr++ = '\0';
272 len = strlen(spec);
273 }
274 }
275
276 if (len > 0) {
277 if (parse_bootname(spec, len, &prompt_dev, &prompt_kname))
278 return 1;
279 }
280
281 DPRINTF(("prompt_dev = %s, prompt_kname = %s\n",
282 prompt_dev ? prompt_dev : "<NULL>",
283 prompt_kname ? prompt_kname : "<NULL>"));
284
285 if (prompt_dev)
286 *dev = prompt_dev;
287 else
288 *dev = bootstr_dev;
289
290 if (prompt_kname)
291 *kname = prompt_kname;
292 else
293 *kname = bootstr_kname;
294
295 DPRINTF(("dev = %s, kname = %s\n",
296 *dev ? *dev : "<NULL>",
297 *kname ? *kname : "<NULL>"));
298
299 return 0;
300 }
301
302 static int
303 parse_bootname(char *spec, int len, char **dev, char **kname)
304 {
305 char *bootname, *ptr;
306
307 bootname = alloc(len + 1);
308 if (bootname == NULL)
309 return 1;
310 memcpy(bootname, spec, len);
311 bootname[len] = '\0';
312
313 if ((ptr = memchr(bootname, ':', len)) != NULL) {
314 /* "wdXX:kernel" */
315 *ptr = '\0';
316 *dev = bootname;
317 if (*++ptr)
318 *kname = ptr;
319 } else
320 /* "kernel" */
321 *kname = bootname;
322 return 0;
323 }
324
325 /*
326 * Get the bootstring from PROM.
327 */
328 int
329 prominit(unsigned int memsize)
330 {
331
332 bootstring = (char *)(memsize - 512);
333 bootstring[511] = '\0';
334 }
335
336 /*
337 * Print boot message.
338 */
339 int
340 print_banner(unsigned int memsize)
341 {
342
343 printf("\n");
344 printf(">> %s " NETBSD_VERS " Bootloader, Revision %s [@%p]\n",
345 bootprog_name, bootprog_rev, (void*)&start);
346 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
347 printf(">> Memory:\t\t%u k\n", (memsize - MIPS_KSEG0_START) / 1024);
348 printf(">> PROM boot string:\t%s\n", bootstring);
349 }
350
351 /*
352 * Entry point.
353 * Parse PROM boot string, load the kernel and jump into it
354 */
355 int
356 main(unsigned int memsize)
357 {
358 char **namep, *dev, *kernel, *bi_addr;
359 char bootpath[PATH_MAX];
360 int win;
361 u_long marks[MARK_MAX];
362 void (*entry)(unsigned int, u_int, char *);
363
364 struct btinfo_flags bi_flags;
365 struct btinfo_symtab bi_syms;
366 struct btinfo_bootpath bi_bpath;
367 struct btinfo_howto bi_howto;
368
369 int addr, speed, howto;
370
371 /* Initialize boot info early */
372 howto = 0x0;
373 bi_flags.bi_flags = 0x0;
374 bi_addr = bi_init();
375
376 prominit(memsize);
377 if (cninit(&addr, &speed) != NULL)
378 bi_flags.bi_flags |= BI_SERIAL_CONSOLE;
379
380 print_banner(memsize);
381
382 memset(marks, 0, sizeof marks);
383 get_bsdbootname(&dev, &kernel, &howto);
384
385 if (kernel != NULL) {
386 DPRINTF(("kernel: %s\n", kernel));
387 kernelnames[0] = kernel;
388 kernelnames[1] = NULL;
389 } else {
390 DPRINTF(("kernel: NULL\n"));
391 }
392
393 win = 0;
394 DPRINTF(("Kernel names: %p\n", kernelnames));
395 for (namep = kernelnames, win = 0; (*namep != NULL) && !win; namep++) {
396 kernel = *namep;
397
398 bootpath[0] = '\0';
399
400 strcpy(bootpath, dev ? dev : DEFBOOTDEV);
401 strcat(bootpath, ":");
402 strcat(bootpath, kernel);
403
404 printf("Loading: %s", bootpath);
405 if (howto)
406 printf(" (howto 0x%x)", howto);
407 printf("\n");
408 patch_bootstring(bootpath);
409 win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
410 }
411
412 if (win) {
413 strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
414 bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
415
416 entry = (void *)marks[MARK_ENTRY];
417 bi_syms.nsym = marks[MARK_NSYM];
418 bi_syms.ssym = marks[MARK_SYM];
419 bi_syms.esym = marks[MARK_END];
420 bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
421
422 bi_add(&bi_flags, BTINFO_FLAGS, sizeof(bi_flags));
423
424 bi_howto.bi_howto = howto;
425 bi_add(&bi_howto, BTINFO_HOWTO, sizeof(bi_howto));
426
427 entry = (void *)marks[MARK_ENTRY];
428
429 DPRINTF(("Bootinfo @ 0x%x\n", bi_addr));
430 printf("Starting at 0x%x\n\n", (u_int)entry);
431 (*entry)(memsize, BOOTINFO_MAGIC, bi_addr);
432 }
433
434 (void)printf("Boot failed! Rebooting...\n");
435 return 0;
436 }
437