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