Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.12
      1 /*	$NetBSD: boot.c,v 1.12 2008/03/01 18:13:02 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 	uint32_t tag, reg;
    396 
    397 #define PCIB_PCI_BUS		0
    398 #define PCIB_PCI_DEV		9
    399 #define PCIB_PCI_FUNC		0
    400 #define PCIB_BOARD_ID_REG	0x94
    401 #define COBALT_BOARD_ID(reg)	((reg & 0x000000f0) >> 4)
    402 
    403 	tag = (PCIB_PCI_BUS << 16) | (PCIB_PCI_DEV << 11) |
    404 	    (PCIB_PCI_FUNC << 8);
    405 	reg = pcicfgread(tag, PCIB_BOARD_ID_REG);
    406 
    407 	return COBALT_BOARD_ID(reg);
    408 }
    409 
    410 /*
    411  * Entry point.
    412  * Parse PROM boot string, load the kernel and jump into it
    413  */
    414 int
    415 main(unsigned int memsize)
    416 {
    417 	char **namep, *dev, *kernel, *bi_addr;
    418 	char bootpath[PATH_MAX];
    419 	int win;
    420 	u_long marks[MARK_MAX];
    421 	void (*entry)(unsigned int, u_int, char *);
    422 
    423 	struct btinfo_flags bi_flags;
    424 	struct btinfo_symtab bi_syms;
    425 	struct btinfo_bootpath bi_bpath;
    426 	struct btinfo_howto bi_howto;
    427 	int addr, speed, howto;
    428 
    429 	try_bootp = 1;
    430 
    431 	/* Initialize boot info early */
    432 	howto = 0x0;
    433 	bi_flags.bi_flags = 0x0;
    434 	bi_addr = bi_init();
    435 
    436 	cobalt_id = read_board_id();
    437 	prominit(memsize);
    438 	if (cninit(&addr, &speed) != NULL)
    439 		bi_flags.bi_flags |= BI_SERIAL_CONSOLE;
    440 
    441 	print_banner(memsize);
    442 
    443 	memset(marks, 0, sizeof marks);
    444 	get_bsdbootname(&dev, &kernel, &howto);
    445 
    446 	if (kernel != NULL) {
    447 		DPRINTF(("kernel: %s\n", kernel));
    448 		kernelnames[0] = kernel;
    449 		kernelnames[1] = NULL;
    450 	} else {
    451 		DPRINTF(("kernel: NULL\n"));
    452 	}
    453 
    454 	win = 0;
    455 	DPRINTF(("Kernel names: %p\n", kernelnames));
    456 	for (namep = kernelnames, win = 0; (*namep != NULL) && !win; namep++) {
    457 		kernel = *namep;
    458 
    459 		bootpath[0] = '\0';
    460 
    461 		strcpy(bootpath, dev ? dev : DEFBOOTDEV);
    462 		strcat(bootpath, ":");
    463 		strcat(bootpath, kernel);
    464 
    465 		printf("Loading: %s", bootpath);
    466 		if (howto)
    467 			printf(" (howto 0x%x)", howto);
    468 		printf("\n");
    469 		patch_bootstring(bootpath);
    470 		win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
    471 	}
    472 
    473 	if (win) {
    474 		strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
    475 		bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
    476 
    477 		entry = (void *)marks[MARK_ENTRY];
    478 		bi_syms.nsym = marks[MARK_NSYM];
    479 		bi_syms.ssym = marks[MARK_SYM];
    480 		bi_syms.esym = marks[MARK_END];
    481 		bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
    482 
    483 		bi_add(&bi_flags, BTINFO_FLAGS, sizeof(bi_flags));
    484 
    485 		bi_howto.bi_howto = howto;
    486 		bi_add(&bi_howto, BTINFO_HOWTO, sizeof(bi_howto));
    487 
    488 		entry = (void *)marks[MARK_ENTRY];
    489 
    490 		DPRINTF(("Bootinfo @ 0x%x\n", bi_addr));
    491 		printf("Starting at 0x%x\n\n", (u_int)entry);
    492 		(*entry)(memsize, BOOTINFO_MAGIC, bi_addr);
    493 	}
    494 
    495 	(void)printf("Boot failed! Rebooting...\n");
    496 	return 0;
    497 }
    498