Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.230
      1  1.230   hannken /*	$NetBSD: kern_subr.c,v 1.230 2022/03/19 13:51:35 hannken Exp $	*/
      2   1.31   thorpej 
      3   1.31   thorpej /*-
      4  1.185        ad  * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
      5   1.31   thorpej  * All rights reserved.
      6   1.31   thorpej  *
      7   1.31   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.31   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.48     lukem  * NASA Ames Research Center, and by Luke Mewburn.
     10   1.31   thorpej  *
     11   1.31   thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.31   thorpej  * modification, are permitted provided that the following conditions
     13   1.31   thorpej  * are met:
     14   1.31   thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.31   thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.31   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.31   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.31   thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.31   thorpej  *
     20   1.31   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.31   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.31   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.31   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.31   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.31   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.31   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.31   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.31   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.31   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.31   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.31   thorpej  */
     32   1.12       cgd 
     33    1.9       cgd /*
     34   1.10       cgd  * Copyright (c) 1982, 1986, 1991, 1993
     35   1.10       cgd  *	The Regents of the University of California.  All rights reserved.
     36    1.9       cgd  * (c) UNIX System Laboratories, Inc.
     37    1.9       cgd  * All or some portions of this file are derived from material licensed
     38    1.9       cgd  * to the University of California by American Telephone and Telegraph
     39    1.9       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40    1.9       cgd  * the permission of UNIX System Laboratories, Inc.
     41    1.9       cgd  *
     42   1.18   thorpej  * Copyright (c) 1992, 1993
     43   1.18   thorpej  *	The Regents of the University of California.  All rights reserved.
     44   1.18   thorpej  *
     45   1.18   thorpej  * This software was developed by the Computer Systems Engineering group
     46   1.18   thorpej  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     47   1.18   thorpej  * contributed to Berkeley.
     48   1.18   thorpej  *
     49   1.18   thorpej  * All advertising materials mentioning features or use of this software
     50   1.18   thorpej  * must display the following acknowledgement:
     51   1.18   thorpej  *	This product includes software developed by the University of
     52   1.18   thorpej  *	California, Lawrence Berkeley Laboratory.
     53   1.18   thorpej  *
     54    1.9       cgd  * Redistribution and use in source and binary forms, with or without
     55    1.9       cgd  * modification, are permitted provided that the following conditions
     56    1.9       cgd  * are met:
     57    1.9       cgd  * 1. Redistributions of source code must retain the above copyright
     58    1.9       cgd  *    notice, this list of conditions and the following disclaimer.
     59    1.9       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     60    1.9       cgd  *    notice, this list of conditions and the following disclaimer in the
     61    1.9       cgd  *    documentation and/or other materials provided with the distribution.
     62  1.103       agc  * 3. Neither the name of the University nor the names of its contributors
     63    1.9       cgd  *    may be used to endorse or promote products derived from this software
     64    1.9       cgd  *    without specific prior written permission.
     65    1.9       cgd  *
     66    1.9       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     67    1.9       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     68    1.9       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     69    1.9       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     70    1.9       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     71    1.9       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     72    1.9       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     73    1.9       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     74    1.9       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     75    1.9       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     76    1.9       cgd  * SUCH DAMAGE.
     77    1.9       cgd  *
     78   1.38      fvdl  *	@(#)kern_subr.c	8.4 (Berkeley) 2/14/95
     79    1.9       cgd  */
     80   1.77     lukem 
     81   1.77     lukem #include <sys/cdefs.h>
     82  1.230   hannken __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.230 2022/03/19 13:51:35 hannken Exp $");
     83   1.34       mrg 
     84   1.78   thorpej #include "opt_ddb.h"
     85   1.43  jonathan #include "opt_md.h"
     86  1.157      manu #include "opt_tftproot.h"
     87    1.9       cgd 
     88    1.9       cgd #include <sys/param.h>
     89    1.9       cgd #include <sys/systm.h>
     90    1.9       cgd #include <sys/proc.h>
     91   1.18   thorpej #include <sys/mount.h>
     92   1.18   thorpej #include <sys/device.h>
     93   1.18   thorpej #include <sys/reboot.h>
     94   1.18   thorpej #include <sys/conf.h>
     95  1.159    dyoung #include <sys/disk.h>
     96   1.18   thorpej #include <sys/disklabel.h>
     97   1.10       cgd #include <sys/queue.h>
     98  1.119   reinoud #include <sys/fcntl.h>
     99  1.160  christos #include <sys/kauth.h>
    100  1.208   hannken #include <sys/stat.h>
    101  1.160  christos #include <sys/vnode.h>
    102  1.194        ad #include <sys/module.h>
    103    1.9       cgd 
    104   1.18   thorpej #include <dev/cons.h>
    105   1.18   thorpej 
    106   1.18   thorpej #include <net/if.h>
    107   1.18   thorpej 
    108   1.18   thorpej /* XXX these should eventually move to subr_autoconf.c */
    109  1.200    dyoung static device_t finddevice(const char *);
    110  1.224   mlelstv static device_t getdisk(const char *, int, int, dev_t *, int);
    111  1.224   mlelstv static device_t parsedisk(const char *, int, int, dev_t *);
    112  1.159    dyoung static const char *getwedgename(const char *, int);
    113   1.18   thorpej 
    114  1.221   mlelstv static void setroot_nfs(device_t);
    115  1.222   mlelstv static void setroot_md(device_t *);
    116  1.221   mlelstv static void setroot_ask(device_t, int);
    117  1.221   mlelstv static void setroot_root(device_t, int);
    118  1.221   mlelstv static void setroot_dump(device_t, device_t);
    119  1.221   mlelstv 
    120  1.221   mlelstv 
    121  1.157      manu #ifdef TFTPROOT
    122  1.200    dyoung int tftproot_dhcpboot(device_t);
    123  1.157      manu #endif
    124  1.157      manu 
    125  1.175        ad dev_t	dumpcdev;	/* for savecore */
    126   1.18   thorpej 
    127  1.160  christos static int
    128  1.200    dyoung isswap(device_t dv)
    129  1.160  christos {
    130  1.160  christos 	struct dkwedge_info wi;
    131  1.160  christos 	struct vnode *vn;
    132  1.160  christos 	int error;
    133  1.160  christos 
    134  1.160  christos 	if (device_class(dv) != DV_DISK || !device_is_a(dv, "dk"))
    135  1.160  christos 		return 0;
    136  1.160  christos 
    137  1.160  christos 	if ((vn = opendisk(dv)) == NULL)
    138  1.160  christos 		return 0;
    139  1.160  christos 
    140  1.230   hannken 	VOP_UNLOCK(vn);
    141  1.166     pooka 	error = VOP_IOCTL(vn, DIOCGWEDGEINFO, &wi, FREAD, NOCRED);
    142  1.230   hannken 	vn_lock(vn, LK_EXCLUSIVE | LK_RETRY);
    143  1.166     pooka 	VOP_CLOSE(vn, FREAD, NOCRED);
    144  1.160  christos 	vput(vn);
    145  1.160  christos 	if (error) {
    146  1.160  christos #ifdef DEBUG_WEDGE
    147  1.184    cegger 		printf("%s: Get wedge info returned %d\n", device_xname(dv), error);
    148  1.160  christos #endif
    149  1.160  christos 		return 0;
    150  1.160  christos 	}
    151  1.160  christos 	return strcmp(wi.dkw_ptype, DKW_PTYPE_SWAP) == 0;
    152  1.160  christos }
    153  1.160  christos 
    154   1.18   thorpej /*
    155   1.18   thorpej  * Determine the root device and, if instructed to, the root file system.
    156   1.18   thorpej  */
    157   1.18   thorpej 
    158  1.108  christos #ifdef MEMORY_DISK_IS_ROOT
    159  1.187        ad int md_is_root = 1;
    160  1.187        ad #else
    161  1.187        ad int md_is_root = 0;
    162  1.108  christos #endif
    163  1.108  christos 
    164  1.113   thorpej /*
    165  1.213   mlelstv  * The device and partition that we booted from.
    166  1.221   mlelstv  *
    167  1.221   mlelstv  * This data might be initialized by MD code, but is defined here.
    168  1.113   thorpej  */
    169  1.200    dyoung device_t booted_device;
    170  1.218  christos const char *booted_method;
    171  1.113   thorpej int booted_partition;
    172  1.213   mlelstv daddr_t booted_startblk;
    173  1.213   mlelstv uint64_t booted_nblks;
    174  1.216   mlelstv char *bootspec;
    175  1.113   thorpej 
    176  1.113   thorpej /*
    177  1.219  jmcneill  * Use partition letters if it's a disk class but not a wedge or flash.
    178  1.219  jmcneill  * XXX Check for wedge/flash is kinda gross.
    179  1.113   thorpej  */
    180  1.113   thorpej #define	DEV_USES_PARTITIONS(dv)						\
    181  1.124   thorpej 	(device_class((dv)) == DV_DISK &&				\
    182  1.219  jmcneill 	 !device_is_a((dv), "dk") &&					\
    183  1.219  jmcneill 	 !device_is_a((dv), "flash"))
    184  1.113   thorpej 
    185   1.18   thorpej void
    186  1.200    dyoung setroot(device_t bootdv, int bootpartition)
    187   1.18   thorpej {
    188  1.221   mlelstv 
    189  1.221   mlelstv 	/*
    190  1.224   mlelstv 	 * Let bootcode augment "rootspec", ensure that
    191  1.224   mlelstv 	 * rootdev is invalid to avoid confusion.
    192  1.221   mlelstv 	 */
    193  1.224   mlelstv 	if (rootspec == NULL) {
    194  1.221   mlelstv 		rootspec = bootspec;
    195  1.224   mlelstv 		rootdev = NODEV;
    196  1.224   mlelstv 	}
    197  1.221   mlelstv 
    198  1.221   mlelstv 	/*
    199  1.221   mlelstv 	 * force boot device to md0
    200  1.221   mlelstv 	 */
    201  1.221   mlelstv 	if (md_is_root)
    202  1.222   mlelstv 		setroot_md(&bootdv);
    203   1.18   thorpej 
    204  1.157      manu #ifdef TFTPROOT
    205  1.221   mlelstv 	/*
    206  1.221   mlelstv 	 * XXX
    207  1.221   mlelstv 	 * if rootspec specifies an interface
    208  1.221   mlelstv 	 * sets root_device to that interface
    209  1.221   mlelstv 	 * reuses NFS init code to set up network
    210  1.221   mlelstv 	 * fetch image into ram disk
    211  1.221   mlelstv 	 *
    212  1.222   mlelstv 	 * if successful, we change boot device
    213  1.221   mlelstv 	 */
    214  1.221   mlelstv 	if (tftproot_dhcpboot(bootdv) == 0)
    215  1.222   mlelstv 		setroot_md(&bootdv);
    216  1.157      manu #endif
    217  1.221   mlelstv 
    218  1.208   hannken 	/*
    219  1.221   mlelstv 	 * quirk for
    220  1.221   mlelstv 	 *  evbarm/mini2440
    221  1.221   mlelstv 	 *  hpcarm
    222  1.221   mlelstv 	 *  hpcmips
    223  1.221   mlelstv 	 *  hpcsh
    224  1.221   mlelstv 	 *
    225  1.221   mlelstv 	 * if rootfstype is set to NFS and the
    226  1.221   mlelstv 	 * kernel supports NFS and the boot device
    227  1.221   mlelstv 	 * is unknown or not a network interface
    228  1.221   mlelstv  	 * -> chose the first network interface you find
    229  1.221   mlelstv 	 *
    230  1.221   mlelstv 	 * hp300 has similar MD code
    231  1.208   hannken 	 */
    232  1.221   mlelstv 	setroot_nfs(bootdv);
    233   1.18   thorpej 
    234   1.18   thorpej 	/*
    235  1.221   mlelstv 	 * If no bootdv was found by MD code and no
    236  1.221   mlelstv 	 * root specified ask the user.
    237  1.216   mlelstv 	 */
    238  1.221   mlelstv 	if (rootspec == NULL && bootdv == NULL)
    239  1.221   mlelstv 		boothowto |= RB_ASKNAME;
    240  1.216   mlelstv 
    241  1.216   mlelstv 	/*
    242  1.221   mlelstv 	 * loop until a root device is specified
    243   1.18   thorpej 	 */
    244  1.221   mlelstv 	do {
    245  1.221   mlelstv 		if (boothowto & RB_ASKNAME)
    246  1.221   mlelstv 			setroot_ask(bootdv, bootpartition);
    247  1.221   mlelstv 		else
    248  1.221   mlelstv 			setroot_root(bootdv, bootpartition);
    249  1.221   mlelstv 
    250  1.221   mlelstv 		if (root_device == NULL)
    251  1.221   mlelstv 			boothowto |= RB_ASKNAME;
    252  1.221   mlelstv 	} while (root_device == NULL);
    253  1.221   mlelstv }
    254  1.221   mlelstv 
    255  1.221   mlelstv /*
    256  1.221   mlelstv  * If NFS is specified as the file system, and we found
    257  1.221   mlelstv  * a DV_DISK boot device (or no boot device at all), then
    258  1.221   mlelstv  * find a reasonable network interface for "rootspec".
    259  1.221   mlelstv  */
    260  1.221   mlelstv static void
    261  1.221   mlelstv setroot_nfs(device_t dv)
    262  1.221   mlelstv {
    263  1.221   mlelstv 	struct vfsops *vops;
    264  1.221   mlelstv 	struct ifnet *ifp;
    265  1.221   mlelstv 
    266  1.197  pgoyette 	vops = vfs_getopsbyname(MOUNT_NFS);
    267  1.197  pgoyette 	if (vops != NULL && strcmp(rootfstype, MOUNT_NFS) == 0 &&
    268   1.18   thorpej 	    rootspec == NULL &&
    269  1.221   mlelstv 	    (dv == NULL || device_class(dv) != DV_IFNET)) {
    270  1.217     ozaki 		int s = pserialize_read_enter();
    271  1.217     ozaki 		IFNET_READER_FOREACH(ifp) {
    272   1.18   thorpej 			if ((ifp->if_flags &
    273   1.18   thorpej 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    274   1.18   thorpej 				break;
    275   1.83      matt 		}
    276  1.221   mlelstv 		if (ifp != NULL) {
    277   1.18   thorpej 			/*
    278   1.18   thorpej 			 * Have a suitable interface; behave as if
    279   1.18   thorpej 			 * the user specified this interface.
    280   1.18   thorpej 			 */
    281   1.18   thorpej 			rootspec = (const char *)ifp->if_xname;
    282   1.18   thorpej 		}
    283  1.217     ozaki 		pserialize_read_exit(s);
    284   1.18   thorpej 	}
    285  1.165        ad 	if (vops != NULL)
    286  1.165        ad 		vfs_delref(vops);
    287  1.221   mlelstv }
    288   1.24   thorpej 
    289  1.222   mlelstv /*
    290  1.222   mlelstv  * Change boot device to md0
    291  1.222   mlelstv  *
    292  1.222   mlelstv  * md0 only exists when it is opened once.
    293  1.222   mlelstv  */
    294  1.222   mlelstv static void
    295  1.222   mlelstv setroot_md(device_t *dvp)
    296  1.222   mlelstv {
    297  1.222   mlelstv 	int md_major;
    298  1.222   mlelstv 	dev_t md_dev;
    299  1.222   mlelstv 
    300  1.222   mlelstv 	md_major = devsw_name2blk("md", NULL, 0);
    301  1.222   mlelstv 	if (md_major >= 0) {
    302  1.222   mlelstv 		md_dev = MAKEDISKDEV(md_major, 0, RAW_PART);
    303  1.222   mlelstv 		if (bdev_open(md_dev, FREAD, S_IFBLK, curlwp) == 0)
    304  1.222   mlelstv 			*dvp = device_find_by_xname("md0");
    305  1.222   mlelstv 	}
    306  1.222   mlelstv }
    307  1.222   mlelstv 
    308  1.221   mlelstv static void
    309  1.221   mlelstv setroot_ask(device_t bootdv, int bootpartition)
    310  1.221   mlelstv {
    311  1.221   mlelstv 	device_t dv, defdumpdv, rootdv, dumpdv;
    312  1.221   mlelstv 	dev_t nrootdev, ndumpdev;
    313  1.221   mlelstv 	struct vfsops *vops;
    314  1.221   mlelstv 	const char *deffsname;
    315  1.221   mlelstv 	int len;
    316  1.221   mlelstv 	char buf[128];
    317   1.18   thorpej 
    318  1.221   mlelstv 	for (;;) {
    319  1.221   mlelstv 		printf("root device");
    320  1.221   mlelstv 		if (bootdv != NULL) {
    321  1.221   mlelstv 			printf(" (default %s", device_xname(bootdv));
    322  1.221   mlelstv 			if (DEV_USES_PARTITIONS(bootdv))
    323  1.221   mlelstv 				printf("%c", bootpartition + 'a');
    324  1.221   mlelstv 			printf(")");
    325  1.221   mlelstv 		}
    326  1.221   mlelstv 		printf(": ");
    327  1.221   mlelstv 		len = cngetsn(buf, sizeof(buf));
    328  1.221   mlelstv 		if (len == 0 && bootdv != NULL) {
    329  1.221   mlelstv 			strlcpy(buf, device_xname(bootdv), sizeof(buf));
    330  1.221   mlelstv 			len = strlen(buf);
    331  1.221   mlelstv 		}
    332  1.221   mlelstv 		if (len > 0 && buf[len - 1] == '*') {
    333  1.221   mlelstv 			buf[--len] = '\0';
    334  1.221   mlelstv 			dv = getdisk(buf, len, 1, &nrootdev, 0);
    335   1.18   thorpej 			if (dv != NULL) {
    336   1.18   thorpej 				rootdv = dv;
    337   1.18   thorpej 				break;
    338   1.18   thorpej 			}
    339   1.18   thorpej 		}
    340  1.221   mlelstv 		dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
    341  1.221   mlelstv 		if (dv != NULL) {
    342  1.221   mlelstv 			rootdv = dv;
    343  1.221   mlelstv 			break;
    344  1.221   mlelstv 		}
    345  1.221   mlelstv 	}
    346  1.221   mlelstv 	rootdev = nrootdev;
    347   1.18   thorpej 
    348  1.221   mlelstv 	/*
    349  1.221   mlelstv 	 * Set up the default dump device.  If root is on
    350  1.221   mlelstv 	 * a network device or a disk without partitions,
    351  1.221   mlelstv 	 * there is no default dump device.
    352  1.221   mlelstv 	 */
    353  1.221   mlelstv 	if (DEV_USES_PARTITIONS(rootdv) == 0)
    354  1.221   mlelstv 		defdumpdv = NULL;
    355  1.221   mlelstv 	else
    356  1.221   mlelstv 		defdumpdv = rootdv;
    357  1.221   mlelstv 
    358  1.221   mlelstv 	ndumpdev = NODEV;
    359  1.221   mlelstv 	for (;;) {
    360  1.221   mlelstv 		printf("dump device");
    361  1.221   mlelstv 		if (defdumpdv != NULL) {
    362  1.221   mlelstv 			/*
    363  1.221   mlelstv 			 * Note, we know it's a disk if we get here.
    364  1.221   mlelstv 			 */
    365  1.221   mlelstv 			printf(" (default %sb)", device_xname(defdumpdv));
    366  1.221   mlelstv 		}
    367  1.221   mlelstv 		printf(": ");
    368  1.221   mlelstv 		len = cngetsn(buf, sizeof(buf));
    369  1.221   mlelstv 		if (len == 0) {
    370   1.26   thorpej 			if (defdumpdv != NULL) {
    371  1.221   mlelstv 				ndumpdev = MAKEDISKDEV(major(nrootdev),
    372  1.221   mlelstv 				    DISKUNIT(nrootdev), 1);
    373   1.26   thorpej 			}
    374  1.221   mlelstv 			dumpdv = defdumpdv;
    375  1.221   mlelstv 			break;
    376  1.221   mlelstv 		}
    377  1.221   mlelstv 		if (len == 4 && strcmp(buf, "none") == 0) {
    378  1.221   mlelstv 			dumpdv = NULL;
    379  1.221   mlelstv 			break;
    380  1.221   mlelstv 		}
    381  1.221   mlelstv 		dv = getdisk(buf, len, 1, &ndumpdev, 1);
    382  1.221   mlelstv 		if (dv != NULL) {
    383  1.221   mlelstv 			dumpdv = dv;
    384  1.221   mlelstv 			break;
    385   1.26   thorpej 		}
    386  1.221   mlelstv 	}
    387  1.221   mlelstv 	dumpdev = ndumpdev;
    388   1.26   thorpej 
    389  1.221   mlelstv 	for (vops = LIST_FIRST(&vfs_list); vops != NULL;
    390  1.221   mlelstv 	     vops = LIST_NEXT(vops, vfs_list)) {
    391  1.221   mlelstv 		if (vops->vfs_mountroot != NULL &&
    392  1.221   mlelstv 		    strcmp(rootfstype, vops->vfs_name) == 0)
    393  1.221   mlelstv 		break;
    394  1.221   mlelstv 	}
    395  1.221   mlelstv 
    396  1.221   mlelstv 	if (vops == NULL) {
    397  1.221   mlelstv 		deffsname = "generic";
    398  1.221   mlelstv 	} else
    399  1.221   mlelstv 		deffsname = vops->vfs_name;
    400   1.18   thorpej 
    401  1.221   mlelstv 	for (;;) {
    402  1.221   mlelstv 		printf("file system (default %s): ", deffsname);
    403  1.221   mlelstv 		len = cngetsn(buf, sizeof(buf));
    404  1.221   mlelstv 		if (len == 0) {
    405  1.221   mlelstv 			if (strcmp(deffsname, "generic") == 0)
    406  1.221   mlelstv 				rootfstype = ROOT_FSTYPE_ANY;
    407   1.38      fvdl 			break;
    408   1.18   thorpej 		}
    409  1.221   mlelstv 		if (len == 4 && strcmp(buf, "halt") == 0)
    410  1.228   thorpej 			kern_reboot(RB_HALT, NULL);
    411  1.221   mlelstv 		else if (len == 6 && strcmp(buf, "reboot") == 0)
    412  1.228   thorpej 			kern_reboot(0, NULL);
    413   1.78   thorpej #if defined(DDB)
    414  1.221   mlelstv 		else if (len == 3 && strcmp(buf, "ddb") == 0) {
    415  1.221   mlelstv 			console_debugger();
    416  1.221   mlelstv 		}
    417   1.78   thorpej #endif
    418  1.221   mlelstv 		else if (len == 7 && strcmp(buf, "generic") == 0) {
    419  1.221   mlelstv 			rootfstype = ROOT_FSTYPE_ANY;
    420  1.221   mlelstv 			break;
    421  1.221   mlelstv 		}
    422  1.221   mlelstv 		vops = vfs_getopsbyname(buf);
    423  1.221   mlelstv 		if (vops == NULL || vops->vfs_mountroot == NULL) {
    424  1.221   mlelstv 			printf("use one of: generic");
    425  1.221   mlelstv 			for (vops = LIST_FIRST(&vfs_list);
    426  1.221   mlelstv 			     vops != NULL;
    427  1.221   mlelstv 			     vops = LIST_NEXT(vops, vfs_list)) {
    428  1.221   mlelstv 				if (vops->vfs_mountroot != NULL)
    429  1.221   mlelstv 					printf(" %s", vops->vfs_name);
    430   1.18   thorpej 			}
    431  1.221   mlelstv 			if (vops != NULL)
    432  1.221   mlelstv 				vfs_delref(vops);
    433   1.78   thorpej #if defined(DDB)
    434  1.221   mlelstv 			printf(" ddb");
    435   1.78   thorpej #endif
    436  1.221   mlelstv 			printf(" halt reboot\n");
    437  1.221   mlelstv 		} else {
    438  1.221   mlelstv 			/*
    439  1.221   mlelstv 			 * XXX If *vops gets freed between here and
    440  1.221   mlelstv 			 * the call to mountroot(), rootfstype will
    441  1.221   mlelstv 			 * point to something unexpected.  But in
    442  1.221   mlelstv 			 * this case the system will fail anyway.
    443  1.221   mlelstv 			 */
    444  1.221   mlelstv 			rootfstype = vops->vfs_name;
    445  1.221   mlelstv 			vfs_delref(vops);
    446  1.221   mlelstv 			break;
    447   1.18   thorpej 		}
    448  1.221   mlelstv 	}
    449  1.221   mlelstv 
    450  1.229   mlelstv 	switch (device_class(rootdv)) {
    451  1.229   mlelstv 	case DV_IFNET:
    452  1.229   mlelstv 	case DV_DISK:
    453  1.229   mlelstv 		aprint_normal("root on %s", device_xname(rootdv));
    454  1.229   mlelstv 		if (DEV_USES_PARTITIONS(rootdv))
    455  1.229   mlelstv 			aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
    456  1.229   mlelstv 		break;
    457  1.229   mlelstv 	default:
    458  1.229   mlelstv 		printf("can't determine root device\n");
    459  1.229   mlelstv 		return;
    460  1.229   mlelstv 	}
    461  1.229   mlelstv 
    462  1.221   mlelstv 	root_device = rootdv;
    463  1.229   mlelstv 	setroot_dump(rootdv, dumpdv);
    464  1.221   mlelstv }
    465  1.221   mlelstv 
    466  1.221   mlelstv /*
    467  1.221   mlelstv  * configure
    468  1.224   mlelstv  *   device_t root_device
    469  1.224   mlelstv  *   dev_t rootdev (for disks)
    470  1.221   mlelstv  *
    471  1.221   mlelstv  */
    472  1.221   mlelstv static void
    473  1.221   mlelstv setroot_root(device_t bootdv, int bootpartition)
    474  1.221   mlelstv {
    475  1.221   mlelstv 	device_t rootdv;
    476  1.221   mlelstv 	int majdev;
    477  1.221   mlelstv 	const char *rootdevname;
    478  1.221   mlelstv 	char buf[128];
    479  1.224   mlelstv 	dev_t nrootdev;
    480  1.221   mlelstv 
    481  1.221   mlelstv 	if (rootspec == NULL) {
    482   1.18   thorpej 
    483   1.18   thorpej 		/*
    484   1.18   thorpej 		 * Wildcarded root; use the boot device.
    485   1.18   thorpej 		 */
    486   1.26   thorpej 		rootdv = bootdv;
    487   1.26   thorpej 
    488  1.188  christos 		if (bootdv)
    489  1.188  christos 			majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
    490  1.188  christos 		else
    491  1.188  christos 			majdev = -1;
    492   1.18   thorpej 		if (majdev >= 0) {
    493   1.18   thorpej 			/*
    494  1.113   thorpej 			 * Root is on a disk.  `bootpartition' is root,
    495  1.113   thorpej 			 * unless the device does not use partitions.
    496   1.18   thorpej 			 */
    497  1.113   thorpej 			if (DEV_USES_PARTITIONS(bootdv))
    498  1.135   thorpej 				rootdev = MAKEDISKDEV(majdev,
    499  1.135   thorpej 						      device_unit(bootdv),
    500  1.135   thorpej 						      bootpartition);
    501  1.113   thorpej 			else
    502  1.135   thorpej 				rootdev = makedev(majdev, device_unit(bootdv));
    503   1.18   thorpej 		}
    504   1.18   thorpej 	} else {
    505   1.18   thorpej 
    506   1.18   thorpej 		/*
    507   1.25       mrg 		 * `root on <dev> ...'
    508   1.18   thorpej 		 */
    509   1.18   thorpej 
    510   1.18   thorpej 		/*
    511  1.224   mlelstv 		 * If rootspec can be parsed, just use it.
    512   1.18   thorpej 		 */
    513  1.224   mlelstv 		rootdv = parsedisk(rootspec, strlen(rootspec), 0, &nrootdev);
    514  1.224   mlelstv 		if (rootdv != NULL) {
    515  1.224   mlelstv 			rootdev = nrootdev;
    516  1.220   mlelstv 			goto haveroot;
    517  1.220   mlelstv 		}
    518  1.159    dyoung 
    519  1.224   mlelstv 		/*
    520  1.224   mlelstv 		 * Fall back to rootdev, compute rootdv for it
    521  1.224   mlelstv 		 */
    522   1.88   gehenna 		rootdevname = devsw_blk2name(major(rootdev));
    523   1.18   thorpej 		if (rootdevname == NULL) {
    524  1.198  christos 			printf("unknown device major 0x%llx\n",
    525  1.198  christos 			    (unsigned long long)rootdev);
    526  1.221   mlelstv 			return;
    527   1.18   thorpej 		}
    528   1.41     perry 		memset(buf, 0, sizeof(buf));
    529  1.198  christos 		snprintf(buf, sizeof(buf), "%s%llu", rootdevname,
    530  1.198  christos 		    (unsigned long long)DISKUNIT(rootdev));
    531   1.18   thorpej 
    532   1.56     enami 		rootdv = finddevice(buf);
    533   1.26   thorpej 		if (rootdv == NULL) {
    534  1.198  christos 			printf("device %s (0x%llx) not configured\n",
    535  1.198  christos 			    buf, (unsigned long long)rootdev);
    536  1.221   mlelstv 			return;
    537   1.18   thorpej 		}
    538   1.26   thorpej 	}
    539   1.18   thorpej 
    540  1.221   mlelstv haveroot:
    541  1.124   thorpej 	switch (device_class(rootdv)) {
    542   1.18   thorpej 	case DV_IFNET:
    543  1.139  christos 	case DV_DISK:
    544  1.184    cegger 		aprint_normal("root on %s", device_xname(rootdv));
    545  1.140  christos 		if (DEV_USES_PARTITIONS(rootdv))
    546  1.198  christos 			aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
    547   1.18   thorpej 		break;
    548   1.18   thorpej 	default:
    549   1.18   thorpej 		printf("can't determine root device\n");
    550  1.221   mlelstv 		return;
    551   1.18   thorpej 	}
    552   1.26   thorpej 
    553  1.221   mlelstv 	root_device = rootdv;
    554  1.221   mlelstv 	setroot_dump(rootdv, NULL);
    555  1.221   mlelstv }
    556  1.221   mlelstv 
    557  1.221   mlelstv /*
    558  1.221   mlelstv  * configure
    559  1.221   mlelstv  *   dev_t dumpdev
    560  1.221   mlelstv  *   dev_t dumpcdev
    561  1.221   mlelstv  *
    562  1.221   mlelstv  * set to NODEV if device not found
    563  1.221   mlelstv  */
    564  1.221   mlelstv static void
    565  1.221   mlelstv setroot_dump(device_t rootdv, device_t dumpdv)
    566  1.221   mlelstv {
    567  1.221   mlelstv 	device_t dv;
    568  1.221   mlelstv 	deviter_t di;
    569  1.221   mlelstv 	const char *dumpdevname;
    570  1.221   mlelstv 	int majdev;
    571  1.221   mlelstv 	char buf[128];
    572  1.221   mlelstv 
    573   1.26   thorpej 	/*
    574   1.26   thorpej 	 * Now configure the dump device.
    575   1.55     enami 	 *
    576   1.26   thorpej 	 * If we haven't figured out the dump device, do so, with
    577   1.26   thorpej 	 * the following rules:
    578   1.26   thorpej 	 *
    579   1.26   thorpej 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
    580   1.26   thorpej 	 *
    581   1.26   thorpej 	 *	(b) If dumpspec is set, try to use it.  If the device
    582   1.26   thorpej 	 *	    is not available, punt.
    583   1.26   thorpej 	 *
    584   1.26   thorpej 	 *	(c) If dumpspec is not set, the dump device is
    585   1.26   thorpej 	 *	    wildcarded or unspecified.  If the root device
    586   1.26   thorpej 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
    587   1.26   thorpej 	 *	    of the root device.
    588   1.26   thorpej 	 */
    589   1.26   thorpej 
    590   1.55     enami 	if (boothowto & RB_ASKNAME) {		/* (a) */
    591   1.55     enami 		if (dumpdv == NULL)
    592   1.55     enami 			goto nodumpdev;
    593   1.55     enami 	} else if (dumpspec != NULL) {		/* (b) */
    594   1.55     enami 		if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
    595   1.26   thorpej 			/*
    596   1.55     enami 			 * Operator doesn't want a dump device.
    597   1.55     enami 			 * Or looks like they tried to pick a network
    598   1.26   thorpej 			 * device.  Oops.
    599   1.26   thorpej 			 */
    600   1.26   thorpej 			goto nodumpdev;
    601   1.26   thorpej 		}
    602   1.26   thorpej 
    603   1.88   gehenna 		dumpdevname = devsw_blk2name(major(dumpdev));
    604   1.26   thorpej 		if (dumpdevname == NULL)
    605   1.26   thorpej 			goto nodumpdev;
    606   1.41     perry 		memset(buf, 0, sizeof(buf));
    607  1.198  christos 		snprintf(buf, sizeof(buf), "%s%llu", dumpdevname,
    608  1.198  christos 		    (unsigned long long)DISKUNIT(dumpdev));
    609   1.26   thorpej 
    610   1.56     enami 		dumpdv = finddevice(buf);
    611   1.56     enami 		if (dumpdv == NULL) {
    612   1.26   thorpej 			/*
    613   1.26   thorpej 			 * Device not configured.
    614   1.26   thorpej 			 */
    615   1.26   thorpej 			goto nodumpdev;
    616   1.26   thorpej 		}
    617   1.55     enami 	} else {				/* (c) */
    618  1.160  christos 		if (DEV_USES_PARTITIONS(rootdv) == 0) {
    619  1.203    dyoung 			for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    620  1.203    dyoung 			     dv != NULL;
    621  1.203    dyoung 			     dv = deviter_next(&di))
    622  1.160  christos 				if (isswap(dv))
    623  1.160  christos 					break;
    624  1.203    dyoung 			deviter_release(&di);
    625  1.160  christos 			if (dv == NULL)
    626  1.160  christos 				goto nodumpdev;
    627  1.160  christos 
    628  1.184    cegger 			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
    629  1.160  christos 			if (majdev < 0)
    630  1.160  christos 				goto nodumpdev;
    631  1.160  christos 			dumpdv = dv;
    632  1.160  christos 			dumpdev = makedev(majdev, device_unit(dumpdv));
    633  1.160  christos 		} else {
    634   1.55     enami 			dumpdv = rootdv;
    635   1.55     enami 			dumpdev = MAKEDISKDEV(major(rootdev),
    636  1.135   thorpej 			    device_unit(dumpdv), 1);
    637   1.55     enami 		}
    638   1.26   thorpej 	}
    639   1.26   thorpej 
    640  1.175        ad 	dumpcdev = devsw_blk2chr(dumpdev);
    641  1.184    cegger 	aprint_normal(" dumps on %s", device_xname(dumpdv));
    642  1.140  christos 	if (DEV_USES_PARTITIONS(dumpdv))
    643  1.198  christos 		aprint_normal("%c", (int)DISKPART(dumpdev) + 'a');
    644  1.140  christos 	aprint_normal("\n");
    645   1.26   thorpej 	return;
    646   1.26   thorpej 
    647   1.26   thorpej  nodumpdev:
    648   1.26   thorpej 	dumpdev = NODEV;
    649  1.175        ad 	dumpcdev = NODEV;
    650  1.100   thorpej 	aprint_normal("\n");
    651   1.18   thorpej }
    652   1.18   thorpej 
    653  1.200    dyoung static device_t
    654  1.117   thorpej finddevice(const char *name)
    655   1.56     enami {
    656  1.159    dyoung 	const char *wname;
    657  1.108  christos 
    658  1.159    dyoung 	if ((wname = getwedgename(name, strlen(name))) != NULL)
    659  1.159    dyoung 		return dkwedge_find_by_wname(wname);
    660  1.159    dyoung 
    661  1.179     joerg 	return device_find_by_xname(name);
    662   1.56     enami }
    663   1.56     enami 
    664  1.200    dyoung static device_t
    665  1.224   mlelstv getdisk(const char *str, int len, int defpart, dev_t *devp, int isdump)
    666   1.18   thorpej {
    667  1.200    dyoung 	device_t dv;
    668  1.203    dyoung 	deviter_t di;
    669   1.18   thorpej 
    670  1.224   mlelstv 	if (len == 4 && strcmp(str, "halt") == 0)
    671  1.228   thorpej 		kern_reboot(RB_HALT, NULL);
    672  1.224   mlelstv 	else if (len == 6 && strcmp(str, "reboot") == 0)
    673  1.228   thorpej 		kern_reboot(0, NULL);
    674  1.224   mlelstv #if defined(DDB)
    675  1.224   mlelstv 	else if (len == 3 && strcmp(str, "ddb") == 0) {
    676  1.224   mlelstv 		console_debugger();
    677  1.224   mlelstv 		return NULL;
    678  1.224   mlelstv 	}
    679  1.224   mlelstv #endif
    680  1.224   mlelstv 
    681   1.51   thorpej 	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
    682   1.18   thorpej 		printf("use one of:");
    683  1.203    dyoung 		for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
    684  1.203    dyoung 		     dv = deviter_next(&di)) {
    685  1.113   thorpej 			if (DEV_USES_PARTITIONS(dv))
    686  1.184    cegger 				printf(" %s[a-%c]", device_xname(dv),
    687   1.19       cgd 				    'a' + MAXPARTITIONS - 1);
    688  1.124   thorpej 			else if (device_class(dv) == DV_DISK)
    689  1.184    cegger 				printf(" %s", device_xname(dv));
    690  1.124   thorpej 			if (isdump == 0 && device_class(dv) == DV_IFNET)
    691  1.184    cegger 				printf(" %s", device_xname(dv));
    692   1.18   thorpej 		}
    693  1.203    dyoung 		deviter_release(&di);
    694  1.159    dyoung 		dkwedge_print_wnames();
    695   1.26   thorpej 		if (isdump)
    696   1.26   thorpej 			printf(" none");
    697   1.78   thorpej #if defined(DDB)
    698   1.78   thorpej 		printf(" ddb");
    699   1.78   thorpej #endif
    700   1.76   thorpej 		printf(" halt reboot\n");
    701   1.18   thorpej 	}
    702  1.159    dyoung 	return dv;
    703  1.159    dyoung }
    704  1.159    dyoung 
    705  1.159    dyoung static const char *
    706  1.159    dyoung getwedgename(const char *name, int namelen)
    707  1.159    dyoung {
    708  1.227      manu 	const char *wpfx1 = "wedge:";
    709  1.227      manu 	const char *wpfx2 = "NAME=";
    710  1.227      manu 	const int wpfx1len = strlen(wpfx1);
    711  1.227      manu 	const int wpfx2len = strlen(wpfx2);
    712  1.159    dyoung 
    713  1.227      manu 	if (namelen > wpfx1len && strncmp(name, wpfx1, wpfx1len) == 0)
    714  1.227      manu 		return name + wpfx1len;
    715  1.227      manu 
    716  1.227      manu 	if (namelen > wpfx2len && strncasecmp(name, wpfx2, wpfx2len) == 0)
    717  1.227      manu 		return name + wpfx2len;
    718  1.159    dyoung 
    719  1.227      manu 	return NULL;
    720   1.18   thorpej }
    721   1.18   thorpej 
    722  1.200    dyoung static device_t
    723  1.224   mlelstv parsedisk(const char *str, int len, int defpart, dev_t *devp)
    724   1.18   thorpej {
    725  1.200    dyoung 	device_t dv;
    726  1.159    dyoung 	const char *wname;
    727  1.224   mlelstv 	char c;
    728   1.29  drochner 	int majdev, part;
    729  1.224   mlelstv 	char xname[16]; /* same size as dv_xname */
    730  1.224   mlelstv 
    731   1.18   thorpej 	if (len == 0)
    732   1.18   thorpej 		return (NULL);
    733   1.18   thorpej 
    734  1.159    dyoung 	if ((wname = getwedgename(str, len)) != NULL) {
    735  1.159    dyoung 		if ((dv = dkwedge_find_by_wname(wname)) == NULL)
    736  1.159    dyoung 			return NULL;
    737  1.159    dyoung 		part = defpart;
    738  1.159    dyoung 		goto gotdisk;
    739  1.224   mlelstv 	}
    740  1.224   mlelstv 
    741  1.224   mlelstv 	c = str[len-1];
    742  1.224   mlelstv 	if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
    743   1.18   thorpej 		part = c - 'a';
    744  1.224   mlelstv 		len--;
    745  1.224   mlelstv 		if (len > sizeof(xname)-1)
    746  1.224   mlelstv 			return NULL;
    747  1.224   mlelstv 		memcpy(xname, str, len);
    748  1.224   mlelstv 		xname[len] = '\0';
    749  1.224   mlelstv 		str = xname;
    750   1.18   thorpej 	} else
    751   1.18   thorpej 		part = defpart;
    752   1.18   thorpej 
    753   1.56     enami 	dv = finddevice(str);
    754   1.56     enami 	if (dv != NULL) {
    755  1.124   thorpej 		if (device_class(dv) == DV_DISK) {
    756   1.18   thorpej  gotdisk:
    757  1.184    cegger 			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
    758   1.18   thorpej 			if (majdev < 0)
    759   1.18   thorpej 				panic("parsedisk");
    760  1.113   thorpej 			if (DEV_USES_PARTITIONS(dv))
    761  1.135   thorpej 				*devp = MAKEDISKDEV(majdev, device_unit(dv),
    762  1.135   thorpej 						    part);
    763  1.113   thorpej 			else
    764  1.135   thorpej 				*devp = makedev(majdev, device_unit(dv));
    765   1.18   thorpej 		}
    766   1.18   thorpej 
    767  1.124   thorpej 		if (device_class(dv) == DV_IFNET)
    768   1.18   thorpej 			*devp = NODEV;
    769   1.18   thorpej 	}
    770   1.18   thorpej 
    771   1.18   thorpej 	return (dv);
    772   1.48     lukem }
    773