Home | History | Annotate | Line # | Download | only in altboot
main.c revision 1.12
      1 /* $NetBSD: main.c,v 1.12 2011/03/13 15:23:43 phx Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tohru Nishimura.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/reboot.h>
     34 
     35 #include <lib/libsa/stand.h>
     36 #include <lib/libsa/loadfile.h>
     37 #include <lib/libkern/libkern.h>
     38 
     39 #include <machine/bootinfo.h>
     40 
     41 #include "globals.h"
     42 
     43 static const struct bootarg {
     44 	const char *name;
     45 	int value;
     46 } bootargs[] = {
     47 	{ "multi",	RB_AUTOBOOT },
     48 	{ "auto",	RB_AUTOBOOT },
     49 	{ "ask",	RB_ASKNAME },
     50 	{ "single",	RB_SINGLE },
     51 	{ "ddb",	RB_KDB },
     52 	{ "userconf",	RB_USERCONF },
     53 	{ "norm",	AB_NORMAL },
     54 	{ "quiet",	AB_QUIET },
     55 	{ "verb",	AB_VERBOSE },
     56 	{ "silent",	AB_SILENT },
     57 	{ "debug",	AB_DEBUG },
     58 	{ "altboot",	-1 }
     59 };
     60 
     61 void *bootinfo; /* low memory reserved to pass bootinfo structures */
     62 int bi_size;	/* BOOTINFO_MAXSIZE */
     63 char *bi_next;
     64 
     65 void bi_init(void *);
     66 void bi_add(void *, int, int);
     67 
     68 struct btinfo_memory bi_mem;
     69 struct btinfo_console bi_cons;
     70 struct btinfo_clock bi_clk;
     71 struct btinfo_prodfamily bi_fam;
     72 struct btinfo_bootpath bi_path;
     73 struct btinfo_rootdevice bi_rdev;
     74 struct btinfo_net bi_net;
     75 struct btinfo_modulelist *btinfo_modulelist;
     76 size_t btinfo_modulelist_size;
     77 
     78 struct boot_module {
     79 	char *bm_kmod;
     80 	ssize_t bm_len;
     81 	struct boot_module *bm_next;
     82 };
     83 struct boot_module *boot_modules;
     84 char module_base[80];
     85 uint32_t kmodloadp;
     86 int modules_enabled = 0;
     87 
     88 void module_add(char *);
     89 void module_load(char *);
     90 int module_open(struct boot_module *);
     91 
     92 void main(int, char **, char *, char *);
     93 
     94 extern char bootprog_name[], bootprog_rev[];
     95 extern char newaltboot[], newaltboot_end[];
     96 
     97 struct pcidev lata[2];
     98 struct pcidev lnif[1];
     99 struct pcidev lusb[3];
    100 int nata, nnif, nusb;
    101 
    102 int brdtype;
    103 uint32_t busclock, cpuclock;
    104 
    105 static int check_bootname(char *);
    106 static int input_cmdline(char **, int);
    107 static int parse_cmdline(char **, int, char *, char *);
    108 static int is_space(char);
    109 
    110 #define	BNAME_DEFAULT "wd0:"
    111 #define MAX_ARGS 10
    112 
    113 void
    114 main(int argc, char *argv[], char *bootargs_start, char *bootargs_end)
    115 {
    116 	struct brdprop *brdprop;
    117 	unsigned long marks[MARK_MAX];
    118 	char *new_argv[MAX_ARGS];
    119 	ssize_t len;
    120 	int n, i, fd, howto;
    121 	char *bname;
    122 
    123 	printf("\n");
    124 	printf(">> %s altboot, revision %s\n", bootprog_name, bootprog_rev);
    125 
    126 	brdprop = brd_lookup(brdtype);
    127 	printf(">> %s, cpu %u MHz, bus %u MHz, %dMB SDRAM\n", brdprop->verbose,
    128 	    cpuclock / 1000000, busclock / 1000000, bi_mem.memsize >> 20);
    129 
    130 	nata = pcilookup(PCI_CLASS_IDE, lata, 2);
    131 	if (nata == 0)
    132 		nata = pcilookup(PCI_CLASS_MISCSTORAGE, lata, 2);
    133 	if (nata == 0)
    134 		nata = pcilookup(PCI_CLASS_SCSI, lata, 2);
    135 	nnif = pcilookup(PCI_CLASS_ETH, lnif, 1);
    136 	nusb = pcilookup(PCI_CLASS_USB, lusb, 3);
    137 
    138 #ifdef DEBUG
    139 	if (nata == 0)
    140 		printf("No IDE/SATA found\n");
    141 	else for (n = 0; n < nata; n++) {
    142 		int b, d, f, bdf, pvd;
    143 		bdf = lata[n].bdf;
    144 		pvd = lata[n].pvd;
    145 		pcidecomposetag(bdf, &b, &d, &f);
    146 		printf("%04x.%04x DSK %02d:%02d:%02d\n",
    147 		    PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
    148 	}
    149 	if (nnif == 0)
    150 		printf("no NET found\n");
    151 	else {
    152 		int b, d, f, bdf, pvd;
    153 		bdf = lnif[0].bdf;
    154 		pvd = lnif[0].pvd;
    155 		pcidecomposetag(bdf, &b, &d, &f);
    156 		printf("%04x.%04x NET %02d:%02d:%02d\n",
    157 		    PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
    158 	}
    159 	if (nusb == 0)
    160 		printf("no USB found\n");
    161 	else for (n = 0; n < nusb; n++) {
    162 		int b, d, f, bdf, pvd;
    163 		bdf = lusb[0].bdf;
    164 		pvd = lusb[0].pvd;
    165 		pcidecomposetag(bdf, &b, &d, &f);
    166 		printf("%04x.%04x USB %02d:%02d:%02d\n",
    167 		    PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
    168 	}
    169 #endif
    170 
    171 	pcisetup();
    172 	pcifixup();
    173 
    174 	if (dskdv_init(&lata[0]) == 0
    175 	    || (nata == 2 && dskdv_init(&lata[1]) == 0))
    176 		printf("IDE/SATA device driver was not found\n");
    177 
    178 	if (netif_init(&lnif[0]) == 0)
    179 		printf("no NET device driver was found\n");
    180 
    181 	/*
    182 	 * When argc is too big then it is probably a pointer, which could
    183 	 * indicate that we were launched as a Linux kernel module using
    184 	 * "bootm".
    185 	 */
    186 	if (argc > MAX_ARGS) {
    187 		/* parse Linux bootargs */
    188 		argv = new_argv;
    189 		argc = parse_cmdline(argv, MAX_ARGS, bootargs_start,
    190 		    bootargs_end);
    191 	}
    192 
    193 	/* wait 2s for user to enter interactive mode */
    194 	for (n = 200; n >= 0; n--) {
    195 		if (n % 100 == 0)
    196 			printf("Hit any key to enter interactive mode: %d\r",
    197 			    n / 100);
    198 		if (tstchar()) {
    199 			(void)getchar();
    200 			argv = new_argv;
    201 			argc = input_cmdline(argv, MAX_ARGS);
    202 			break;
    203 		}
    204 		delay(10000);
    205 	}
    206 	putchar('\n');
    207 
    208 	howto = RB_AUTOBOOT;		/* default is autoboot = 0 */
    209 
    210 	/* get boot options and determine bootname */
    211 	for (n = 1; n < argc; n++) {
    212 		for (i = 0; i < sizeof(bootargs) / sizeof(bootargs[0]); i++) {
    213 			if (strncasecmp(argv[n], bootargs[i].name,
    214 			    strlen(bootargs[i].name)) == 0) {
    215 				howto |= bootargs[i].value;
    216 				break;
    217 			}
    218 		}
    219 		if (i >= sizeof(bootargs) / sizeof(bootargs[0]))
    220 			break;	/* break on first unknown string */
    221 	}
    222 	if (n >= argc)
    223 		bname = BNAME_DEFAULT;
    224 	else {
    225 		bname = argv[n];
    226 		if (check_bootname(bname) == 0) {
    227 			printf("%s not a valid bootname\n", bname);
    228 			goto loadfail;
    229 		}
    230 	}
    231 
    232 	if ((fd = open(bname, 0)) < 0) {
    233 		if (errno == ENOENT)
    234 			printf("\"%s\" not found\n", bi_path.bootpath);
    235 		goto loadfail;
    236 	}
    237 	printf("loading \"%s\" ", bi_path.bootpath);
    238 	marks[MARK_START] = 0;
    239 
    240 	if (howto == -1) {
    241 		/* load another altboot binary and replace ourselves */
    242 		len = read(fd, (void *)0x100000, 0x1000000 - 0x100000);
    243 		if (len == -1)
    244 			goto loadfail;
    245 		close(fd);
    246 		netif_shutdown_all();
    247 
    248 		memcpy((void *)0xf0000, newaltboot,
    249 		    newaltboot_end - newaltboot);
    250 		__syncicache((void *)0xf0000, newaltboot_end - newaltboot);
    251 		printf("Restarting...\n");
    252 		run((void *)1, argv, (void *)0x100000, (void *)len,
    253 		    (void *)0xf0000);
    254 	} else if (fdloadfile(fd, marks, LOAD_KERNEL) < 0)
    255 		goto loadfail;
    256 	close(fd);
    257 
    258 	printf("entry=%p, ssym=%p, esym=%p\n",
    259 	    (void *)marks[MARK_ENTRY],
    260 	    (void *)marks[MARK_SYM],
    261 	    (void *)marks[MARK_END]);
    262 
    263 	bootinfo = (void *)0x4000;
    264 	bi_init(bootinfo);
    265 	bi_add(&bi_cons, BTINFO_CONSOLE, sizeof(bi_cons));
    266 	bi_add(&bi_mem, BTINFO_MEMORY, sizeof(bi_mem));
    267 	bi_add(&bi_clk, BTINFO_CLOCK, sizeof(bi_clk));
    268 	bi_add(&bi_path, BTINFO_BOOTPATH, sizeof(bi_path));
    269 	bi_add(&bi_rdev, BTINFO_ROOTDEVICE, sizeof(bi_rdev));
    270 	bi_add(&bi_fam, BTINFO_PRODFAMILY, sizeof(bi_fam));
    271 	if (brdtype == BRD_SYNOLOGY || brdtype == BRD_DLINKDSM) {
    272 		/* need to set this MAC address in kernel driver later */
    273 		bi_add(&bi_net, BTINFO_NET, sizeof(bi_net));
    274 	}
    275 
    276 	if (modules_enabled) {
    277 		module_add(fsmod);
    278 		if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
    279 			module_add(fsmod2);
    280 		kmodloadp = marks[MARK_END];
    281 		btinfo_modulelist = NULL;
    282 		module_load(bname);
    283 		if (btinfo_modulelist != NULL && btinfo_modulelist->num > 0)
    284 			bi_add(btinfo_modulelist, BTINFO_MODULELIST,
    285 			    btinfo_modulelist_size);
    286 	}
    287 
    288 	netif_shutdown_all();
    289 
    290 	__syncicache((void *)marks[MARK_ENTRY],
    291 	    (u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]);
    292 
    293 	run((void *)marks[MARK_SYM], (void *)marks[MARK_END],
    294 	    (void *)howto, bootinfo, (void *)marks[MARK_ENTRY]);
    295 
    296 	/* should never come here */
    297 	printf("exec returned. Restarting...\n");
    298 	_rtt();
    299 
    300   loadfail:
    301 	printf("load failed. Restarting...\n");
    302 	_rtt();
    303 }
    304 
    305 void
    306 bi_init(void *addr)
    307 {
    308 	struct btinfo_magic bi_magic;
    309 
    310 	memset(addr, 0, BOOTINFO_MAXSIZE);
    311 	bi_next = (char *)addr;
    312 	bi_size = 0;
    313 
    314 	bi_magic.magic = BOOTINFO_MAGIC;
    315 	bi_add(&bi_magic, BTINFO_MAGIC, sizeof(bi_magic));
    316 }
    317 
    318 void
    319 bi_add(void *new, int type, int size)
    320 {
    321 	struct btinfo_common *bi;
    322 
    323 	if (bi_size + size > BOOTINFO_MAXSIZE)
    324 		return;				/* XXX error? */
    325 
    326 	bi = new;
    327 	bi->next = size;
    328 	bi->type = type;
    329 	memcpy(bi_next, new, size);
    330 	bi_next += size;
    331 }
    332 
    333 void
    334 module_add(char *name)
    335 {
    336 	struct boot_module *bm, *bmp;
    337 
    338 	while (*name == ' ' || *name == '\t')
    339 		++name;
    340 
    341 	bm = alloc(sizeof(struct boot_module) + strlen(name) + 1);
    342 	if (bm == NULL) {
    343 		printf("couldn't allocate module %s\n", name);
    344 		return;
    345 	}
    346 
    347 	bm->bm_kmod = (char *)(bm + 1);
    348 	bm->bm_len = -1;
    349 	bm->bm_next = NULL;
    350 	strcpy(bm->bm_kmod, name);
    351 	if ((bmp = boot_modules) == NULL)
    352 		boot_modules = bm;
    353 	else {
    354 		while (bmp->bm_next != NULL)
    355 			bmp = bmp->bm_next;
    356 		bmp->bm_next = bm;
    357 	}
    358 }
    359 
    360 #define PAGE_SIZE	4096
    361 #define alignpg(x)	(((x)+PAGE_SIZE-1) & ~(PAGE_SIZE-1))
    362 
    363 void
    364 module_load(char *kernel_path)
    365 {
    366 	struct boot_module *bm;
    367 	struct bi_modulelist_entry *bi;
    368 	struct stat st;
    369 	char *p;
    370 	int size, fd;
    371 
    372 	strcpy(module_base, kernel_path);
    373 	if ((p = strchr(module_base, ':')) == NULL)
    374 		return; /* eeh?! */
    375 	p += 1;
    376 	size = sizeof(module_base) - (p - module_base);
    377 
    378 	if (netbsd_version / 1000000 % 100 == 99) {
    379 		/* -current */
    380 		snprintf(p, size,
    381 		    "/stand/sandpoint/%d.%d.%d/modules",
    382 		    netbsd_version / 100000000,
    383 		    netbsd_version / 1000000 % 100,
    384 		    netbsd_version / 100 % 100);
    385 	}
    386 	 else if (netbsd_version != 0) {
    387 		/* release */
    388 		snprintf(p, size,
    389 		    "/stand/sandpoint/%d.%d/modules",
    390 		    netbsd_version / 100000000,
    391 		    netbsd_version / 1000000 % 100);
    392 	}
    393 
    394 	/*
    395 	 * 1st pass; determine module existence
    396 	 */
    397 	size = 0;
    398 	for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
    399 		fd = module_open(bm);
    400 		if (fd == -1)
    401 			continue;
    402 		if (fstat(fd, &st) == -1 || st.st_size == -1) {
    403 			printf("WARNING: couldn't stat %s\n", bm->bm_kmod);
    404 			close(fd);
    405 			continue;
    406 		}
    407 		bm->bm_len = (int)st.st_size;
    408 		close(fd);
    409 		size += sizeof(struct bi_modulelist_entry);
    410 	}
    411 	if (size == 0)
    412 		return;
    413 
    414 	size += sizeof(struct btinfo_modulelist);
    415 	btinfo_modulelist = alloc(size);
    416 	if (btinfo_modulelist == NULL) {
    417 		printf("WARNING: couldn't allocate module list\n");
    418 		return;
    419 	}
    420 	btinfo_modulelist_size = size;
    421 	btinfo_modulelist->num = 0;
    422 
    423 	/*
    424 	 * 2nd pass; load modules into memory
    425 	 */
    426 	kmodloadp = alignpg(kmodloadp);
    427 	bi = (struct bi_modulelist_entry *)(btinfo_modulelist + 1);
    428 	for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
    429 		if (bm->bm_len == -1)
    430 			continue; /* already found unavailable */
    431 		fd = module_open(bm);
    432 		printf("module \"%s\" ", bm->bm_kmod);
    433 		size = read(fd, (char *)kmodloadp, SSIZE_MAX);
    434 		if (size < bm->bm_len)
    435 			printf("WARNING: couldn't load");
    436 		else {
    437 			snprintf(bi->kmod, sizeof(bi->kmod), bm->bm_kmod);
    438 			bi->type = BI_MODULE_ELF;
    439 			bi->len = size;
    440 			bi->base = kmodloadp;
    441 			btinfo_modulelist->num += 1;
    442 			printf("loaded at 0x%08x size 0x%x", kmodloadp, size);
    443 			kmodloadp += alignpg(size);
    444 			bi += 1;
    445 		}
    446 		printf("\n");
    447 		close(fd);
    448 	}
    449 	btinfo_modulelist->endpa = kmodloadp;
    450 }
    451 
    452 int
    453 module_open(struct boot_module *bm)
    454 {
    455 	char path[80];
    456 	int fd;
    457 
    458 	snprintf(path, sizeof(path),
    459 	    "%s/%s/%s.kmod", module_base, bm->bm_kmod, bm->bm_kmod);
    460 	fd = open(path, 0);
    461 	return fd;
    462 }
    463 
    464 #if 0
    465 static const char *cmdln[] = {
    466 	"console=ttyS0,115200 root=/dev/sda1 rw initrd=0x200000,2M",
    467 	"console=ttyS0,115200 root=/dev/nfs ip=dhcp"
    468 };
    469 
    470 void
    471 mkatagparams(unsigned addr, char *kcmd)
    472 {
    473 	struct tag {
    474 		unsigned siz;
    475 		unsigned tag;
    476 		unsigned val[1];
    477 	};
    478 	struct tag *p;
    479 #define ATAG_CORE 	0x54410001
    480 #define ATAG_MEM	0x54410002
    481 #define ATAG_INITRD	0x54410005
    482 #define ATAG_CMDLINE	0x54410009
    483 #define ATAG_NONE	0x00000000
    484 #define tagnext(p) (struct tag *)((unsigned *)(p) + (p)->siz)
    485 #define tagsize(n) (2 + (n))
    486 
    487 	p = (struct tag *)addr;
    488 	p->tag = ATAG_CORE;
    489 	p->siz = tagsize(3);
    490 	p->val[0] = 0;		/* flags */
    491 	p->val[1] = 0;		/* pagesize */
    492 	p->val[2] = 0;		/* rootdev */
    493 	p = tagnext(p);
    494 	p->tag = ATAG_MEM;
    495 	p->siz = tagsize(2);
    496 	p->val[0] = 64 * 1024 * 1024;
    497 	p->val[1] = 0;		/* start */
    498 	p = tagnext(p);
    499 	if (kcmd != NULL) {
    500 		p = tagnext(p);
    501 		p->tag = ATAG_CMDLINE;
    502 		p->siz = tagsize((strlen(kcmd) + 1 + 3) >> 2);
    503 		strcpy((void *)p->val, kcmd);
    504 	}
    505 	p = tagnext(p);
    506 	p->tag = ATAG_NONE;
    507 	p->siz = 0;
    508 }
    509 #endif
    510 
    511 void *
    512 allocaligned(size_t size, size_t align)
    513 {
    514 	uint32_t p;
    515 
    516 	if (align-- < 2)
    517 		return alloc(size);
    518 	p = (uint32_t)alloc(size + align);
    519 	return (void *)((p + align) & ~align);
    520 }
    521 
    522 static int hex2nibble(char c)
    523 {
    524 
    525 	if (c >= 'a')
    526 		c &= ~0x20;
    527 	if (c >= 'A' && c <= 'F')
    528 		c -= 'A' - ('9' + 1);
    529 	else if (c < '0' || c > '9')
    530 		return -1;
    531 
    532 	return c - '0';
    533 }
    534 
    535 uint32_t
    536 read_hex(const char *s)
    537 {
    538 	int n;
    539 	uint32_t val;
    540 
    541 	val = 0;
    542 	while ((n = hex2nibble(*s++)) >= 0)
    543 		val = (val << 4) | n;
    544 	return val;
    545 }
    546 
    547 static int
    548 check_bootname(char *s)
    549 {
    550 	/*
    551 	 * nfs:
    552 	 * nfs:<bootfile>
    553 	 * tftp:
    554 	 * tftp:<bootfile>
    555 	 * wd[N[P]]:<bootfile>
    556 	 * mem:<address>
    557 	 *
    558 	 * net is a synonym of nfs.
    559 	 */
    560 	if (strncmp(s, "nfs:", 4) == 0 || strncmp(s, "net:", 4) == 0 ||
    561 	    strncmp(s, "tftp:", 5) == 0 || strncmp(s, "mem:", 4) == 0)
    562 		return 1;
    563 	if (s[0] == 'w' && s[1] == 'd') {
    564 		s += 2;
    565 		if (*s != ':' && *s >= '0' && *s <= '3') {
    566 			++s;
    567 			if (*s != ':' && *s >= 'a' && *s <= 'p')
    568 				++s;
    569 		}
    570 		return *s == ':';
    571 	}
    572 	return 0;
    573 }
    574 
    575 static int input_cmdline(char **argv, int maxargc)
    576 {
    577 	char *cmdline;
    578 
    579 	printf("\nbootargs> ");
    580 	cmdline = alloc(256);
    581 	gets(cmdline);
    582 
    583 	return parse_cmdline(argv, maxargc, cmdline,
    584 	    cmdline + strlen(cmdline));
    585 }
    586 
    587 static int
    588 parse_cmdline(char **argv, int maxargc, char *p, char *end)
    589 {
    590 	int argc;
    591 
    592 	argv[0] = "";
    593 	for (argc = 1; argc < maxargc && p < end; argc++) {
    594 		while (is_space(*p))
    595 			p++;
    596 		if (p >= end)
    597 			break;
    598 		argv[argc] = p;
    599 		while (!is_space(*p) && p < end)
    600 			p++;
    601 		*p++ = '\0';
    602 	}
    603 
    604 	return argc;
    605 }
    606 
    607 static int
    608 is_space(char c)
    609 {
    610 	return c > '\0' && c <= ' ';
    611 }
    612