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