boot.c revision 1.7 1 /* $NetBSD: boot.c,v 1.7 2007/08/10 16:47:07 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 char *bootstr_dev, *bootstr_kname;
198 char *prompt_dev, *prompt_kname;
199 char *ptr, *spec;
200 char c, namebuf[PATH_MAX];
201
202 bootstr_dev = prompt_dev = NULL;
203 bootstr_kname = prompt_kname = NULL;
204
205 /* first, get bootname from bootstrings */
206 if ((spec = strstr(bootstring, "nbsd=")) != NULL) {
207 ptr = strchr(spec, ' ');
208 spec += 5; /* skip 'nbsd=' */
209 len = (ptr == NULL) ? strlen(spec) : ptr - spec;
210 if (len > 0) {
211 if (parse_bootname(spec, len,
212 &bootstr_dev, &bootstr_kname))
213 return 1;
214 }
215 }
216
217 DPRINTF(("bootstr_dev = %s, bootstr_kname = %s\n",
218 bootstr_dev ? bootstr_dev : "<NULL>",
219 bootstr_kname ? bootstr_kname : "<NULL>"));
220
221 spec = NULL;
222 len = 0;
223
224 memset(namebuf, 0, sizeof namebuf);
225 printf("Boot [%s:%s]: ",
226 bootstr_dev ? bootstr_dev : DEFBOOTDEV,
227 bootstr_kname ? bootstr_kname : DEFKERNELNAME);
228
229 if (tgets(namebuf) == -1)
230 printf("\n");
231
232 ptr = namebuf;
233 while ((c = *ptr) != '\0') {
234 while (c == ' ')
235 c = *++ptr;
236 if (c == '\0')
237 break;
238 if (c == '-') {
239 while ((c = *++ptr) && c != ' ')
240 BOOT_FLAG(c, *howtop);
241 } else {
242 spec = ptr;
243 while ((c = *++ptr) && c != ' ')
244 ;
245 if (c)
246 *ptr++ = '\0';
247 len = strlen(spec);
248 }
249 }
250
251 if (len > 0) {
252 if (parse_bootname(spec, len, &prompt_dev, &prompt_kname))
253 return 1;
254 }
255
256 DPRINTF(("prompt_dev = %s, prompt_kname = %s\n",
257 prompt_dev ? prompt_dev : "<NULL>",
258 prompt_kname ? prompt_kname : "<NULL>"));
259
260 if (prompt_dev)
261 *dev = prompt_dev;
262 else
263 *dev = bootstr_dev;
264
265 if (prompt_kname)
266 *kname = prompt_kname;
267 else
268 *kname = bootstr_kname;
269
270 DPRINTF(("dev = %s, kname = %s\n",
271 *dev ? *dev : "<NULL>",
272 *kname ? *kname : "<NULL>"));
273
274 return 0;
275 }
276
277 static int
278 parse_bootname(char *spec, int len, char **dev, char **kname)
279 {
280 char *bootname, *ptr;
281
282 bootname = alloc(len + 1);
283 if (bootname == NULL)
284 return 1;
285 memcpy(bootname, spec, len);
286 bootname[len] = '\0';
287
288 if ((ptr = memchr(bootname, ':', len)) != NULL) {
289 /* "wdXX:kernel" */
290 *ptr = '\0';
291 *dev = bootname;
292 if (*++ptr)
293 *kname = ptr;
294 } else
295 /* "kernel" */
296 *kname = bootname;
297 return 0;
298 }
299
300 /*
301 * Get the bootstring from PROM.
302 */
303 int
304 prominit(unsigned int memsize)
305 {
306
307 bootstring = (char *)(memsize - 512);
308 bootstring[511] = '\0';
309 }
310
311 /*
312 * Print boot message.
313 */
314 int
315 print_banner(unsigned int memsize)
316 {
317
318 printf("\n");
319 printf(">> %s " NETBSD_VERS " Bootloader, Revision %s [@%p]\n",
320 bootprog_name, bootprog_rev, (void*)&start);
321 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
322 printf(">> Memory:\t\t%u k\n", (memsize - MIPS_KSEG0_START) / 1024);
323 printf(">> PROM boot string:\t%s\n", bootstring);
324 }
325
326 /*
327 * Entry point.
328 * Parse PROM boot string, load the kernel and jump into it
329 */
330 int
331 main(unsigned int memsize)
332 {
333 char **namep, *dev, *kernel, *bi_addr;
334 char bootpath[PATH_MAX];
335 int win;
336 u_long marks[MARK_MAX];
337 void (*entry)(unsigned int, u_int, char *);
338
339 struct btinfo_flags bi_flags;
340 struct btinfo_symtab bi_syms;
341 struct btinfo_bootpath bi_bpath;
342 struct btinfo_howto bi_howto;
343
344 int addr, speed, howto;
345
346 /* Initialize boot info early */
347 howto = 0x0;
348 bi_flags.bi_flags = 0x0;
349 bi_addr = bi_init();
350
351 prominit(memsize);
352 if (cninit(&addr, &speed) != NULL)
353 bi_flags.bi_flags |= BI_SERIAL_CONSOLE;
354
355 print_banner(memsize);
356
357 memset(marks, 0, sizeof marks);
358 get_bsdbootname(&dev, &kernel, &howto);
359
360 if (kernel != NULL) {
361 DPRINTF(("kernel: %s\n", kernel));
362 kernelnames[0] = kernel;
363 kernelnames[1] = NULL;
364 } else {
365 DPRINTF(("kernel: NULL\n"));
366 }
367
368 win = 0;
369 DPRINTF(("Kernel names: %p\n", kernelnames));
370 for (namep = kernelnames, win = 0; (*namep != NULL) && !win; namep++) {
371 kernel = *namep;
372
373 bootpath[0] = '\0';
374
375 strcpy(bootpath, dev ? dev : DEFBOOTDEV);
376 strcat(bootpath, ":");
377 strcat(bootpath, kernel);
378
379 printf("Loading: %s", bootpath);
380 if (howto)
381 printf(" (howto 0x%x)", howto);
382 printf("\n");
383 patch_bootstring(bootpath);
384 win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
385 }
386
387 if (win) {
388 strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
389 bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
390
391 entry = (void *)marks[MARK_ENTRY];
392 bi_syms.nsym = marks[MARK_NSYM];
393 bi_syms.ssym = marks[MARK_SYM];
394 bi_syms.esym = marks[MARK_END];
395 bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
396
397 bi_add(&bi_flags, BTINFO_FLAGS, sizeof(bi_flags));
398
399 bi_howto.bi_howto = howto;
400 bi_add(&bi_howto, BTINFO_HOWTO, sizeof(bi_howto));
401
402 entry = (void *)marks[MARK_ENTRY];
403
404 DPRINTF(("Bootinfo @ 0x%x\n", bi_addr));
405 printf("Starting at 0x%x\n\n", (u_int)entry);
406 (*entry)(memsize, BOOTINFO_MAGIC, bi_addr);
407 }
408
409 (void)printf("Boot failed! Rebooting...\n");
410 return 0;
411 }
412