Home | History | Annotate | Line # | Download | only in common
      1  1.9   msaitoh /*	$NetBSD: boot.c,v 1.9 2020/08/19 02:19:07 msaitoh Exp $	*/
      2  1.1    cherry 
      3  1.1    cherry /*-
      4  1.1    cherry  * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
      5  1.1    cherry  * All rights reserved.
      6  1.1    cherry  *
      7  1.1    cherry  * Redistribution and use in source and binary forms, with or without
      8  1.1    cherry  * modification, are permitted provided that the following conditions
      9  1.1    cherry  * are met:
     10  1.1    cherry  * 1. Redistributions of source code must retain the above copyright
     11  1.1    cherry  *    notice, this list of conditions and the following disclaimer.
     12  1.1    cherry  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1    cherry  *    notice, this list of conditions and the following disclaimer in the
     14  1.1    cherry  *    documentation and/or other materials provided with the distribution.
     15  1.1    cherry  *
     16  1.1    cherry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1    cherry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1    cherry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1    cherry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1    cherry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1    cherry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1    cherry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1    cherry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1    cherry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1    cherry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1    cherry  * SUCH DAMAGE.
     27  1.1    cherry  */
     28  1.1    cherry 
     29  1.1    cherry #include <sys/cdefs.h>
     30  1.2    cherry /* __FBSDID("$FreeBSD: src/sys/boot/common/boot.c,v 1.29 2003/08/25 23:30:41 obrien Exp $"); */
     31  1.1    cherry 
     32  1.1    cherry 
     33  1.1    cherry /*
     34  1.1    cherry  * Loading modules, booting the system
     35  1.1    cherry  */
     36  1.1    cherry 
     37  1.1    cherry #include <lib/libsa/stand.h>
     38  1.5  kiyohara #include <lib/libsa/loadfile.h>
     39  1.1    cherry #include <lib/libkern/libkern.h>
     40  1.1    cherry 
     41  1.1    cherry #include "bootstrap.h"
     42  1.1    cherry 
     43  1.1    cherry static char	*getbootfile(int try);
     44  1.1    cherry static int	loadakernel(int try, int argc, char* argv[]);
     45  1.1    cherry 
     46  1.3    cherry /* List of kernel names to try */
     47  1.1    cherry static const char *default_bootfiles = "netbsd";
     48  1.1    cherry 
     49  1.1    cherry static int autoboot_tried;
     50  1.1    cherry 
     51  1.1    cherry /*
     52  1.1    cherry  * The user wants us to boot.
     53  1.1    cherry  */
     54  1.1    cherry 
     55  1.1    cherry int
     56  1.1    cherry command_boot(int argc, char *argv[])
     57  1.1    cherry {
     58  1.1    cherry     struct preloaded_file	*fp;
     59  1.1    cherry 
     60  1.1    cherry     /*
     61  1.1    cherry      * See if the user has specified an explicit kernel to boot.
     62  1.1    cherry      */
     63  1.1    cherry     if ((argc > 1) && (argv[1][0] != '-')) {
     64  1.1    cherry 
     65  1.1    cherry 	/* XXX maybe we should discard everything and start again? */
     66  1.1    cherry 	if (file_findfile(NULL, NULL) != NULL) {
     67  1.6  christos 	    command_seterr("can't boot '%s', kernel module already loaded",
     68  1.6  christos 		argv[1]);
     69  1.1    cherry 	    return(CMD_ERROR);
     70  1.1    cherry 	}
     71  1.1    cherry 
     72  1.1    cherry 	/* find/load the kernel module */
     73  1.1    cherry 	if (file_loadkernel(argv[1], argc - 2, argv + 2) != 0)
     74  1.1    cherry 	    return(CMD_ERROR);
     75  1.1    cherry 
     76  1.1    cherry 	/* we have consumed all arguments */
     77  1.1    cherry 	argc = 1;
     78  1.1    cherry     }
     79  1.1    cherry 
     80  1.1    cherry     /*
     81  1.1    cherry      * See if there is a kernel module already loaded
     82  1.1    cherry      */
     83  1.1    cherry     if (file_findfile(NULL, NULL) == NULL)
     84  1.1    cherry 	if (loadakernel(0, argc - 1, argv + 1))
     85  1.1    cherry 	    /* we have consumed all arguments */
     86  1.1    cherry 	    argc = 1;
     87  1.1    cherry 
     88  1.1    cherry     /*
     89  1.1    cherry      * Loaded anything yet?
     90  1.1    cherry      */
     91  1.1    cherry     if ((fp = file_findfile(NULL, NULL)) == NULL) {
     92  1.6  christos 	command_seterr("no bootable kernel");
     93  1.1    cherry 	return(CMD_ERROR);
     94  1.1    cherry     }
     95  1.1    cherry 
     96  1.1    cherry     /*
     97  1.1    cherry      * If we were given arguments, discard any previous.
     98  1.1    cherry      * XXX should we merge arguments?  Hard to DWIM.
     99  1.1    cherry      */
    100  1.1    cherry     if (argc > 1) {
    101  1.1    cherry 	if (fp->f_args != NULL)
    102  1.1    cherry 	    free(fp->f_args);
    103  1.1    cherry 	fp->f_args = unargv(argc - 1, argv + 1);
    104  1.1    cherry     }
    105  1.1    cherry 
    106  1.1    cherry     /* Hook for platform-specific autoloading of modules */
    107  1.1    cherry     if (archsw.arch_autoload() != 0)
    108  1.1    cherry 	return(CMD_ERROR);
    109  1.1    cherry 
    110  1.1    cherry     /* Call the exec handler from the loader matching the kernel */
    111  1.7    martin     command_seterr("%s", strerror(file_formats[fp->f_loader]->l_exec(fp)));
    112  1.1    cherry     return(CMD_ERROR);
    113  1.1    cherry }
    114  1.1    cherry 
    115  1.1    cherry 
    116  1.1    cherry /*
    117  1.1    cherry  * Autoboot after a delay
    118  1.1    cherry  */
    119  1.1    cherry 
    120  1.1    cherry int
    121  1.1    cherry command_autoboot(int argc, char *argv[])
    122  1.1    cherry {
    123  1.1    cherry     int		howlong;
    124  1.1    cherry     char	*cp, *prompt;
    125  1.1    cherry 
    126  1.1    cherry     prompt = NULL;
    127  1.1    cherry     howlong = -1;
    128  1.1    cherry     switch(argc) {
    129  1.1    cherry     case 3:
    130  1.1    cherry 	prompt = argv[2];
    131  1.1    cherry 	/* FALLTHROUGH */
    132  1.1    cherry     case 2:
    133  1.1    cherry 	howlong = strtol(argv[1], &cp, 0);
    134  1.1    cherry 	if (*cp != 0) {
    135  1.6  christos 	    command_seterr("bad delay '%s'", argv[1]);
    136  1.1    cherry 	    return(CMD_ERROR);
    137  1.1    cherry 	}
    138  1.1    cherry 	/* FALLTHROUGH */
    139  1.1    cherry     case 1:
    140  1.1    cherry 	return(autoboot(howlong, prompt));
    141  1.1    cherry     }
    142  1.1    cherry 
    143  1.6  christos     command_seterr("too many arguments");
    144  1.1    cherry     return(CMD_ERROR);
    145  1.1    cherry }
    146  1.1    cherry 
    147  1.1    cherry /*
    148  1.1    cherry  * Called before we go interactive.  If we think we can autoboot, and
    149  1.1    cherry  * we haven't tried already, try now.
    150  1.1    cherry  */
    151  1.1    cherry void
    152  1.4    cegger autoboot_maybe(void)
    153  1.1    cherry {
    154  1.1    cherry     char	*cp;
    155  1.1    cherry 
    156  1.1    cherry     cp = getenv("autoboot_delay");
    157  1.1    cherry     if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
    158  1.1    cherry 	autoboot(-1, NULL);		/* try to boot automatically */
    159  1.1    cherry }
    160  1.1    cherry 
    161  1.1    cherry int
    162  1.1    cherry autoboot(int timeout, char *prompt)
    163  1.1    cherry {
    164  1.1    cherry     time_t	when, otime, ntime;
    165  1.1    cherry     int		c, yes;
    166  1.1    cherry     char	*argv[2], *cp, *ep;
    167  1.1    cherry     char	*kernelname;
    168  1.1    cherry 
    169  1.1    cherry     autoboot_tried = 1;
    170  1.1    cherry 
    171  1.1    cherry     if (timeout == -1) {
    172  1.1    cherry 	/* try to get a delay from the environment */
    173  1.1    cherry 	if ((cp = getenv("autoboot_delay"))) {
    174  1.1    cherry 	    timeout = strtol(cp, &ep, 0);
    175  1.1    cherry 	    if (cp == ep)
    176  1.1    cherry 		timeout = -1;
    177  1.1    cherry 	}
    178  1.1    cherry     }
    179  1.1    cherry     if (timeout == -1)		/* all else fails */
    180  1.1    cherry 	timeout = 10;
    181  1.1    cherry 
    182  1.1    cherry     kernelname = getenv("kernelname");
    183  1.1    cherry     if (kernelname == NULL) {
    184  1.1    cherry 	argv[0] = NULL;
    185  1.1    cherry 	loadakernel(0, 0, argv);
    186  1.1    cherry 	kernelname = getenv("kernelname");
    187  1.1    cherry 	if (kernelname == NULL) {
    188  1.6  christos 	    command_seterr("no valid kernel found");
    189  1.1    cherry 	    return(CMD_ERROR);
    190  1.1    cherry 	}
    191  1.1    cherry     }
    192  1.1    cherry 
    193  1.1    cherry     otime = time(NULL);
    194  1.1    cherry     when = otime + timeout;	/* when to boot */
    195  1.1    cherry     yes = 0;
    196  1.1    cherry 
    197  1.1    cherry     printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
    198  1.1    cherry 
    199  1.1    cherry     for (;;) {
    200  1.1    cherry 	if (ischar()) {
    201  1.1    cherry 	    c = getchar();
    202  1.1    cherry 	    if ((c == '\r') || (c == '\n'))
    203  1.1    cherry 		yes = 1;
    204  1.1    cherry 	    break;
    205  1.1    cherry 	}
    206  1.1    cherry 	ntime = time(NULL);
    207  1.1    cherry 	if (ntime >= when) {
    208  1.1    cherry 	    yes = 1;
    209  1.1    cherry 	    break;
    210  1.1    cherry 	}
    211  1.1    cherry 
    212  1.1    cherry 	if (ntime != otime) {
    213  1.1    cherry 	    printf("\rBooting [%s] in %d second%s... ",
    214  1.1    cherry 	    		kernelname, (int)(when - ntime),
    215  1.1    cherry 			(when-ntime)==1?"":"s");
    216  1.1    cherry 	    otime = ntime;
    217  1.1    cherry 	}
    218  1.1    cherry     }
    219  1.1    cherry     if (yes)
    220  1.1    cherry 	printf("\rBooting [%s]...               ", kernelname);
    221  1.1    cherry     putchar('\n');
    222  1.1    cherry     if (yes) {
    223  1.1    cherry 	argv[0] = "boot";
    224  1.1    cherry 	argv[1] = NULL;
    225  1.1    cherry 	return(command_boot(1, argv));
    226  1.1    cherry     }
    227  1.1    cherry     return(CMD_OK);
    228  1.1    cherry }
    229  1.1    cherry 
    230  1.1    cherry /*
    231  1.1    cherry  * Scrounge for the name of the (try)'th file we will try to boot.
    232  1.1    cherry  */
    233  1.1    cherry static char *
    234  1.1    cherry getbootfile(int try)
    235  1.1    cherry {
    236  1.1    cherry     static char *name = NULL;
    237  1.1    cherry     const char	*spec, *ep;
    238  1.1    cherry     size_t	len;
    239  1.1    cherry 
    240  1.1    cherry     /* we use dynamic storage */
    241  1.1    cherry     if (name != NULL) {
    242  1.1    cherry 	free(name);
    243  1.1    cherry 	name = NULL;
    244  1.1    cherry     }
    245  1.1    cherry 
    246  1.1    cherry     /*
    247  1.1    cherry      * Try $bootfile, then try our builtin default
    248  1.1    cherry      */
    249  1.1    cherry     if ((spec = getenv("bootfile")) == NULL)
    250  1.1    cherry 	spec = default_bootfiles;
    251  1.1    cherry 
    252  1.1    cherry     while ((try > 0) && (spec != NULL)) {
    253  1.1    cherry 	spec = strchr(spec, ';');
    254  1.1    cherry 	if (spec)
    255  1.1    cherry 	    spec++;	/* skip over the leading ';' */
    256  1.1    cherry 	try--;
    257  1.1    cherry     }
    258  1.1    cherry     if (spec != NULL) {
    259  1.1    cherry 	if ((ep = strchr(spec, ';')) != NULL) {
    260  1.1    cherry 	    len = ep - spec;
    261  1.1    cherry 	} else {
    262  1.1    cherry 	    len = strlen(spec);
    263  1.1    cherry 	}
    264  1.1    cherry 	name = alloc(len + 1);
    265  1.1    cherry 	strncpy(name, spec, len);
    266  1.1    cherry 	name[len] = 0;
    267  1.1    cherry     }
    268  1.1    cherry     if (name && name[0] == 0) {
    269  1.1    cherry 	free(name);
    270  1.1    cherry 	name = NULL;
    271  1.1    cherry     }
    272  1.1    cherry     else {
    273  1.1    cherry       setenv("kernelname", name, 1);
    274  1.1    cherry     }
    275  1.1    cherry 
    276  1.1    cherry     return(name);
    277  1.1    cherry }
    278  1.1    cherry 
    279  1.1    cherry /*
    280  1.1    cherry  * Try to find the /etc/fstab file on the filesystem (rootdev),
    281  1.9   msaitoh  * which should be the root filesystem, and parse it to find
    282  1.1    cherry  * out what the kernel ought to think the root filesystem is.
    283  1.1    cherry  *
    284  1.1    cherry  * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
    285  1.1    cherry  * so that the kernel can tell both which VFS and which node to use
    286  1.1    cherry  * to mount the device.  If this variable's already set, don't
    287  1.1    cherry  * overwrite it.
    288  1.1    cherry  */
    289  1.1    cherry int
    290  1.1    cherry getrootmount(char *rootdev)
    291  1.1    cherry {
    292  1.1    cherry     char	lbuf[128], *cp, *ep, *dev, *fstyp;
    293  1.1    cherry     int		fd, error;
    294  1.1    cherry 
    295  1.1    cherry     if (getenv("vfs.root.mountfrom") != NULL)
    296  1.1    cherry 	return(0);
    297  1.1    cherry 
    298  1.6  christos     snprintf(lbuf, sizeof(lbuf), "%s/etc/fstab", rootdev);
    299  1.1    cherry     if ((fd = open(lbuf, O_RDONLY)) < 0)
    300  1.1    cherry 	return(1);
    301  1.1    cherry 
    302  1.1    cherry     /* loop reading lines from /etc/fstab    What was that about sscanf again? */
    303  1.1    cherry     error = 1;
    304  1.1    cherry     while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
    305  1.1    cherry 	if ((lbuf[0] == 0) || (lbuf[0] == '#'))
    306  1.1    cherry 	    continue;
    307  1.1    cherry 
    308  1.1    cherry 	/* skip device name */
    309  1.1    cherry 	for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
    310  1.1    cherry 	    ;
    311  1.1    cherry 	if (*cp == 0)		/* misformatted */
    312  1.1    cherry 	    continue;
    313  1.1    cherry 	/* delimit and save */
    314  1.1    cherry 	*cp++ = 0;
    315  1.1    cherry 	dev = strdup(lbuf);
    316  1.1    cherry 
    317  1.1    cherry 	/* skip whitespace up to mountpoint */
    318  1.1    cherry 	while ((*cp != 0) && isspace(*cp))
    319  1.1    cherry 	    cp++;
    320  1.1    cherry 	/* must have /<space> to be root */
    321  1.8  dholland 	if ((*cp != '/') || !isspace(*(cp + 1)))
    322  1.1    cherry 	    continue;
    323  1.1    cherry 	/* skip whitespace up to fstype */
    324  1.1    cherry 	cp += 2;
    325  1.1    cherry 	while ((*cp != 0) && isspace(*cp))
    326  1.1    cherry 	    cp++;
    327  1.1    cherry 	if (*cp == 0)		/* misformatted */
    328  1.1    cherry 	    continue;
    329  1.1    cherry 	/* skip text to end of fstype and delimit */
    330  1.1    cherry 	ep = cp;
    331  1.1    cherry 	while ((*cp != 0) && !isspace(*cp))
    332  1.1    cherry 	    cp++;
    333  1.1    cherry 	*cp = 0;
    334  1.1    cherry 	fstyp = strdup(ep);
    335  1.1    cherry 
    336  1.1    cherry 	/* build the final result and save it */
    337  1.6  christos 	snprintf(lbuf, sizeof(lbuf), "%s:%s", fstyp, dev);
    338  1.1    cherry 	free(dev);
    339  1.1    cherry 	free(fstyp);
    340  1.1    cherry 	setenv("vfs.root.mountfrom", lbuf, 0);
    341  1.1    cherry 	error = 0;
    342  1.1    cherry 	break;
    343  1.1    cherry     }
    344  1.1    cherry     close(fd);
    345  1.1    cherry     return(error);
    346  1.1    cherry }
    347  1.1    cherry 
    348  1.1    cherry static int
    349  1.1    cherry loadakernel(int try, int argc, char* argv[])
    350  1.1    cherry {
    351  1.1    cherry 	char *cp;
    352  1.1    cherry 
    353  1.1    cherry 	for (try = 0; (cp = getbootfile(try)) != NULL; try++)
    354  1.1    cherry 		if (file_loadkernel(cp, argc - 1, argv + 1) != 0)
    355  1.1    cherry 			printf("can't load '%s'\n", cp);
    356  1.1    cherry 		else
    357  1.1    cherry 			return 1;
    358  1.1    cherry 	return 0;
    359  1.1    cherry }
    360  1.1    cherry 
    361