boot.c revision 1.11 1 /* $NetBSD: boot.c,v 1.11 2008/03/01 17:45:11 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/libsa/dev_net.h>
79 #include <lib/libkern/libkern.h>
80
81 #include <sys/param.h>
82 #include <sys/boot_flag.h>
83 #include <sys/exec.h>
84 #include <sys/exec_elf.h>
85
86 #include <cobalt/dev/gtreg.h>
87
88 #include "boot.h"
89 #include "cons.h"
90 #include "common.h"
91 #include "bootinfo.h"
92
93 char *kernelnames[] = {
94 "netbsd",
95 "netbsd.gz",
96 "onetbsd",
97 "onetbsd.gz",
98 "netbsd.bak",
99 "netbsd.bak.gz",
100 "netbsd.old",
101 "netbsd.old.gz",
102 "netbsd.cobalt",
103 "netbsd.cobalt.gz",
104 "netbsd.elf",
105 "netbsd.elf.gz",
106 NULL
107 };
108
109 u_int cobalt_id;
110 static const char * const cobalt_model[] =
111 {
112 [0] = "Unknown Cobalt",
113 [COBALT_ID_QUBE2700] = "Cobalt Qube 2700",
114 [COBALT_ID_RAQ] = "Cobalt RaQ",
115 [COBALT_ID_QUBE2] = "Cobalt Qube 2",
116 [COBALT_ID_RAQ2] = "Cobalt RaQ 2"
117 };
118 #define COBALT_MODELS __arraycount(cobalt_model)
119
120 extern u_long end; /* Boot loader code end address */
121 void start(void);
122
123 static char *bootstring;
124
125 static int patch_bootstring(char *bootspec);
126 static int get_bsdbootname(char **, char **, int *);
127 static int parse_bootname(char *, int, char **, char **);
128 static void prominit(unsigned int memsize);
129 static void print_banner(unsigned int memsize);
130 static u_int read_board_id(void);
131
132 void cpu_reboot(void);
133
134 int main(unsigned int memsize);
135
136 /*
137 * Perform CPU reboot.
138 */
139 void
140 cpu_reboot(void)
141 {
142
143 printf("rebooting...\n\n");
144
145 *(volatile uint8_t *)MIPS_PHYS_TO_KSEG1(LED_ADDR) = LED_RESET;
146 printf("WARNING: reboot failed!\n");
147
148 for (;;)
149 ;
150 }
151
152 /*
153 * Substitute root value with NetBSD root partition name.
154 */
155 int
156 patch_bootstring(char *bootspec)
157 {
158 char *sp = bootstring;
159 uint8_t unit, part;
160 int dev;
161 char *file;
162
163 DPRINTF(("patch_bootstring: %s\n", bootspec));
164
165 /* get boot parameters */
166 if (devparse(bootspec, &dev, &unit, &part, (const char **)&file) != 0)
167 unit = part = 0;
168
169 DPRINTF(("patch_bootstring: unit = %d, part = %d\n", unit, part));
170
171 /* take out the 'root=xxx' parameter */
172 if ((sp = strstr(bootstring, "root=")) != NULL) {
173 const char *end;
174
175 end = strchr(sp, ' ');
176
177 /* strip off leading spaces */
178 for (--sp; (sp > bootstring) && (*sp == ' '); --sp)
179 ;
180
181 if (end != NULL)
182 strcpy(++sp, end);
183 else
184 *++sp = '\0';
185 }
186
187 DPRINTF(("patch_bootstring: [%s]\n", bootstring));
188
189 #define DEVNAMESIZE (MAXDEVNAME + sizeof(" root=/dev/hd") + sizeof("0a"))
190 if (strcmp(devsw[dev].dv_name, "wd") == 0 &&
191 strlen(bootstring) <= (511 - DEVNAMESIZE)) {
192 int len;
193
194 /* omit "nfsroot=" arg on wd boot */
195 if ((sp = strstr(bootstring, "nfsroot=")) != NULL) {
196 const char *end;
197
198 end = strchr(sp, ' ');
199
200 /* strip off leading spaces */
201 for (--sp; (sp > bootstring) && (*sp == ' '); --sp)
202 ;
203
204 if (end != NULL)
205 strcpy(++sp, end);
206 else
207 *++sp = '\0';
208 }
209
210 /* bsd notation -> linux notation (wd0a -> hda1) */
211 strcat(bootstring, " root=/dev/hd");
212
213 len = strlen(bootstring);
214 bootstring[len++] = unit + 'a';
215 bootstring[len++] = part + '1';
216 bootstring[len++] = '\0';
217 }
218
219 DPRINTF(("patch_bootstring: -> %s\n", bootstring));
220 return 0;
221 }
222
223 /*
224 * Extract NetBSD boot specification
225 */
226 static int
227 get_bsdbootname(char **dev, char **kname, int *howtop)
228 {
229 int len;
230 int bootunit, bootpart;
231 char *bootstr_dev, *bootstr_kname;
232 char *prompt_dev, *prompt_kname;
233 char *ptr, *spec;
234 char c, namebuf[PATH_MAX];
235 static char bootdev[] = "wd0a";
236 static char nfsbootdev[] = "nfs";
237
238 bootstr_dev = prompt_dev = NULL;
239 bootstr_kname = prompt_kname = NULL;
240
241 /* first, get root device specified by the firmware */
242 spec = bootstring;
243
244 /* assume the last one is valid */
245 while ((spec = strstr(spec, "root=")) != NULL) {
246 spec += 5; /* skip 'root=' */
247 ptr = strchr(spec, ' ');
248 len = (ptr == NULL) ? strlen(spec) : ptr - spec;
249 /* decode unit and part from "/dev/hd[ab][1-4]" strings */
250 if (len == 9 && memcmp("/dev/hd", spec, 7) == 0) {
251 bootunit = spec[7] - 'a';
252 bootpart = spec[8] - '1';
253 if (bootunit >= 0 && bootunit < 2 &&
254 bootpart >= 0 && bootpart < 4) {
255 bootdev[sizeof(bootdev) - 3] = '0' + bootunit;
256 #if 0 /* bootpart is fdisk partition of Linux root */
257 bootdev[sizeof(bootdev) - 2] = 'a' + bootpart;
258 #endif
259 bootstr_dev = bootdev;
260 }
261 }
262 spec += len;
263 }
264
265 /* second, get bootname from bootstrings */
266 if ((spec = strstr(bootstring, "nbsd=")) != NULL) {
267 ptr = strchr(spec, ' ');
268 spec += 5; /* skip 'nbsd=' */
269 len = (ptr == NULL) ? strlen(spec) : ptr - spec;
270 if (len > 0) {
271 if (parse_bootname(spec, len,
272 &bootstr_dev, &bootstr_kname))
273 return 1;
274 }
275 }
276
277 /* third, check if netboot */
278 if (strstr(bootstring, "nfsroot=") != NULL) {
279 bootstr_dev = nfsbootdev;
280 }
281
282 DPRINTF(("bootstr_dev = %s, bootstr_kname = %s\n",
283 bootstr_dev ? bootstr_dev : "<NULL>",
284 bootstr_kname ? bootstr_kname : "<NULL>"));
285
286 spec = NULL;
287 len = 0;
288
289 memset(namebuf, 0, sizeof namebuf);
290 printf("Boot [%s:%s]: ",
291 bootstr_dev ? bootstr_dev : DEFBOOTDEV,
292 bootstr_kname ? bootstr_kname : DEFKERNELNAME);
293
294 if (tgets(namebuf) == -1)
295 printf("\n");
296
297 ptr = namebuf;
298 while ((c = *ptr) != '\0') {
299 while (c == ' ')
300 c = *++ptr;
301 if (c == '\0')
302 break;
303 if (c == '-') {
304 while ((c = *++ptr) && c != ' ')
305 BOOT_FLAG(c, *howtop);
306 } else {
307 spec = ptr;
308 while ((c = *++ptr) && c != ' ')
309 ;
310 if (c)
311 *ptr++ = '\0';
312 len = strlen(spec);
313 }
314 }
315
316 if (len > 0) {
317 if (parse_bootname(spec, len, &prompt_dev, &prompt_kname))
318 return 1;
319 }
320
321 DPRINTF(("prompt_dev = %s, prompt_kname = %s\n",
322 prompt_dev ? prompt_dev : "<NULL>",
323 prompt_kname ? prompt_kname : "<NULL>"));
324
325 if (prompt_dev)
326 *dev = prompt_dev;
327 else
328 *dev = bootstr_dev;
329
330 if (prompt_kname)
331 *kname = prompt_kname;
332 else
333 *kname = bootstr_kname;
334
335 DPRINTF(("dev = %s, kname = %s\n",
336 *dev ? *dev : "<NULL>",
337 *kname ? *kname : "<NULL>"));
338
339 return 0;
340 }
341
342 static int
343 parse_bootname(char *spec, int len, char **dev, char **kname)
344 {
345 char *bootname, *ptr;
346
347 bootname = alloc(len + 1);
348 if (bootname == NULL)
349 return 1;
350 memcpy(bootname, spec, len);
351 bootname[len] = '\0';
352
353 if ((ptr = memchr(bootname, ':', len)) != NULL) {
354 /* "wdXX:kernel" */
355 *ptr = '\0';
356 *dev = bootname;
357 if (*++ptr)
358 *kname = ptr;
359 } else
360 /* "kernel" */
361 *kname = bootname;
362 return 0;
363 }
364
365 /*
366 * Get the bootstring from PROM.
367 */
368 void
369 prominit(unsigned int memsize)
370 {
371
372 bootstring = (char *)(memsize - 512);
373 bootstring[511] = '\0';
374 }
375
376 /*
377 * Print boot message.
378 */
379 void
380 print_banner(unsigned int memsize)
381 {
382
383 printf("\n");
384 printf(">> %s " NETBSD_VERS " Bootloader, Revision %s [@%p]\n",
385 bootprog_name, bootprog_rev, (void*)&start);
386 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
387 printf(">> Model:\t\t%s\n", cobalt_model[cobalt_id]);
388 printf(">> Memory:\t\t%u k\n", (memsize - MIPS_KSEG0_START) / 1024);
389 printf(">> PROM boot string:\t%s\n", bootstring);
390 }
391
392 u_int
393 read_board_id(void)
394 {
395 volatile uint32_t *pcicfg_addr, *pcicfg_data;
396 uint32_t reg;
397 u_int id;
398
399 #define PCIB_PCI_BUS 0
400 #define PCIB_PCI_DEV 9
401 #define PCIB_PCI_FUNC 0
402 #define PCIB_BOARD_ID_REG 0x94
403 #define COBALT_BOARD_ID(reg) ((reg & 0x000000f0) >> 4)
404 #define GT_BASE 0x14000000
405
406 pcicfg_addr = (uint32_t *)MIPS_PHYS_TO_KSEG1(GT_BASE + GT_PCICFG_ADDR);
407 pcicfg_data = (uint32_t *)MIPS_PHYS_TO_KSEG1(GT_BASE + GT_PCICFG_DATA);
408
409 *pcicfg_addr = PCICFG_ENABLE |
410 (PCIB_PCI_BUS << 16) | (PCIB_PCI_DEV << 11) | (PCIB_PCI_FUNC << 8) |
411 PCIB_BOARD_ID_REG;
412 reg = *pcicfg_data;
413 *pcicfg_addr = 0;
414
415 return COBALT_BOARD_ID(reg);
416 }
417
418 /*
419 * Entry point.
420 * Parse PROM boot string, load the kernel and jump into it
421 */
422 int
423 main(unsigned int memsize)
424 {
425 char **namep, *dev, *kernel, *bi_addr;
426 char bootpath[PATH_MAX];
427 int win;
428 u_long marks[MARK_MAX];
429 void (*entry)(unsigned int, u_int, char *);
430
431 struct btinfo_flags bi_flags;
432 struct btinfo_symtab bi_syms;
433 struct btinfo_bootpath bi_bpath;
434 struct btinfo_howto bi_howto;
435 int addr, speed, howto;
436
437 try_bootp = 1;
438
439 /* Initialize boot info early */
440 howto = 0x0;
441 bi_flags.bi_flags = 0x0;
442 bi_addr = bi_init();
443
444 cobalt_id = read_board_id();
445 prominit(memsize);
446 if (cninit(&addr, &speed) != NULL)
447 bi_flags.bi_flags |= BI_SERIAL_CONSOLE;
448
449 print_banner(memsize);
450
451 memset(marks, 0, sizeof marks);
452 get_bsdbootname(&dev, &kernel, &howto);
453
454 if (kernel != NULL) {
455 DPRINTF(("kernel: %s\n", kernel));
456 kernelnames[0] = kernel;
457 kernelnames[1] = NULL;
458 } else {
459 DPRINTF(("kernel: NULL\n"));
460 }
461
462 win = 0;
463 DPRINTF(("Kernel names: %p\n", kernelnames));
464 for (namep = kernelnames, win = 0; (*namep != NULL) && !win; namep++) {
465 kernel = *namep;
466
467 bootpath[0] = '\0';
468
469 strcpy(bootpath, dev ? dev : DEFBOOTDEV);
470 strcat(bootpath, ":");
471 strcat(bootpath, kernel);
472
473 printf("Loading: %s", bootpath);
474 if (howto)
475 printf(" (howto 0x%x)", howto);
476 printf("\n");
477 patch_bootstring(bootpath);
478 win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
479 }
480
481 if (win) {
482 strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
483 bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
484
485 entry = (void *)marks[MARK_ENTRY];
486 bi_syms.nsym = marks[MARK_NSYM];
487 bi_syms.ssym = marks[MARK_SYM];
488 bi_syms.esym = marks[MARK_END];
489 bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
490
491 bi_add(&bi_flags, BTINFO_FLAGS, sizeof(bi_flags));
492
493 bi_howto.bi_howto = howto;
494 bi_add(&bi_howto, BTINFO_HOWTO, sizeof(bi_howto));
495
496 entry = (void *)marks[MARK_ENTRY];
497
498 DPRINTF(("Bootinfo @ 0x%x\n", bi_addr));
499 printf("Starting at 0x%x\n\n", (u_int)entry);
500 (*entry)(memsize, BOOTINFO_MAGIC, bi_addr);
501 }
502
503 (void)printf("Boot failed! Rebooting...\n");
504 return 0;
505 }
506