Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.222
      1  1.222   mlelstv /*	$NetBSD: kern_subr.c,v 1.222 2019/01/05 18:03:41 mlelstv 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.222   mlelstv __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.222 2019/01/05 18:03:41 mlelstv 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.200    dyoung static device_t getdisk(char *, int, int, dev_t *, int);
    111  1.200    dyoung static device_t parsedisk(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.166     pooka 	error = VOP_IOCTL(vn, DIOCGWEDGEINFO, &wi, FREAD, NOCRED);
    141  1.166     pooka 	VOP_CLOSE(vn, FREAD, NOCRED);
    142  1.160  christos 	vput(vn);
    143  1.160  christos 	if (error) {
    144  1.160  christos #ifdef DEBUG_WEDGE
    145  1.184    cegger 		printf("%s: Get wedge info returned %d\n", device_xname(dv), error);
    146  1.160  christos #endif
    147  1.160  christos 		return 0;
    148  1.160  christos 	}
    149  1.160  christos 	return strcmp(wi.dkw_ptype, DKW_PTYPE_SWAP) == 0;
    150  1.160  christos }
    151  1.160  christos 
    152   1.18   thorpej /*
    153   1.18   thorpej  * Determine the root device and, if instructed to, the root file system.
    154   1.18   thorpej  */
    155   1.18   thorpej 
    156  1.108  christos #ifdef MEMORY_DISK_IS_ROOT
    157  1.187        ad int md_is_root = 1;
    158  1.187        ad #else
    159  1.187        ad int md_is_root = 0;
    160  1.108  christos #endif
    161  1.108  christos 
    162  1.113   thorpej /*
    163  1.213   mlelstv  * The device and partition that we booted from.
    164  1.221   mlelstv  *
    165  1.221   mlelstv  * This data might be initialized by MD code, but is defined here.
    166  1.113   thorpej  */
    167  1.200    dyoung device_t booted_device;
    168  1.218  christos const char *booted_method;
    169  1.113   thorpej int booted_partition;
    170  1.213   mlelstv daddr_t booted_startblk;
    171  1.213   mlelstv uint64_t booted_nblks;
    172  1.216   mlelstv char *bootspec;
    173  1.113   thorpej 
    174  1.113   thorpej /*
    175  1.219  jmcneill  * Use partition letters if it's a disk class but not a wedge or flash.
    176  1.219  jmcneill  * XXX Check for wedge/flash is kinda gross.
    177  1.113   thorpej  */
    178  1.113   thorpej #define	DEV_USES_PARTITIONS(dv)						\
    179  1.124   thorpej 	(device_class((dv)) == DV_DISK &&				\
    180  1.219  jmcneill 	 !device_is_a((dv), "dk") &&					\
    181  1.219  jmcneill 	 !device_is_a((dv), "flash"))
    182  1.113   thorpej 
    183   1.18   thorpej void
    184  1.200    dyoung setroot(device_t bootdv, int bootpartition)
    185   1.18   thorpej {
    186  1.221   mlelstv 
    187  1.221   mlelstv 	/*
    188  1.221   mlelstv 	 * Let bootcode augment "rootspec".
    189  1.221   mlelstv 	 */
    190  1.221   mlelstv 	if (rootspec == NULL)
    191  1.221   mlelstv 		rootspec = bootspec;
    192  1.221   mlelstv 
    193  1.221   mlelstv 	/*
    194  1.221   mlelstv 	 * force boot device to md0
    195  1.221   mlelstv 	 */
    196  1.221   mlelstv 	if (md_is_root)
    197  1.222   mlelstv 		setroot_md(&bootdv);
    198   1.18   thorpej 
    199  1.157      manu #ifdef TFTPROOT
    200  1.221   mlelstv 	/*
    201  1.221   mlelstv 	 * XXX
    202  1.221   mlelstv 	 * if rootspec specifies an interface
    203  1.221   mlelstv 	 * sets root_device to that interface
    204  1.221   mlelstv 	 * reuses NFS init code to set up network
    205  1.221   mlelstv 	 * fetch image into ram disk
    206  1.221   mlelstv 	 *
    207  1.222   mlelstv 	 * if successful, we change boot device
    208  1.221   mlelstv 	 */
    209  1.221   mlelstv 	if (tftproot_dhcpboot(bootdv) == 0)
    210  1.222   mlelstv 		setroot_md(&bootdv);
    211  1.157      manu #endif
    212  1.221   mlelstv 
    213  1.208   hannken 	/*
    214  1.221   mlelstv 	 * quirk for
    215  1.221   mlelstv 	 *  evbarm/mini2440
    216  1.221   mlelstv 	 *  hpcarm
    217  1.221   mlelstv 	 *  hpcmips
    218  1.221   mlelstv 	 *  hpcsh
    219  1.221   mlelstv 	 *
    220  1.221   mlelstv 	 * if rootfstype is set to NFS and the
    221  1.221   mlelstv 	 * kernel supports NFS and the boot device
    222  1.221   mlelstv 	 * is unknown or not a network interface
    223  1.221   mlelstv  	 * -> chose the first network interface you find
    224  1.221   mlelstv 	 *
    225  1.221   mlelstv 	 * hp300 has similar MD code
    226  1.208   hannken 	 */
    227  1.221   mlelstv 	setroot_nfs(bootdv);
    228   1.18   thorpej 
    229   1.18   thorpej 	/*
    230  1.221   mlelstv 	 * If no bootdv was found by MD code and no
    231  1.221   mlelstv 	 * root specified ask the user.
    232  1.216   mlelstv 	 */
    233  1.221   mlelstv 	if (rootspec == NULL && bootdv == NULL)
    234  1.221   mlelstv 		boothowto |= RB_ASKNAME;
    235  1.216   mlelstv 
    236  1.216   mlelstv 	/*
    237  1.221   mlelstv 	 * loop until a root device is specified
    238   1.18   thorpej 	 */
    239  1.221   mlelstv 	do {
    240  1.221   mlelstv 		if (boothowto & RB_ASKNAME)
    241  1.221   mlelstv 			setroot_ask(bootdv, bootpartition);
    242  1.221   mlelstv 		else
    243  1.221   mlelstv 			setroot_root(bootdv, bootpartition);
    244  1.221   mlelstv 
    245  1.221   mlelstv 		if (root_device == NULL)
    246  1.221   mlelstv 			boothowto |= RB_ASKNAME;
    247  1.221   mlelstv 	} while (root_device == NULL);
    248  1.221   mlelstv }
    249  1.221   mlelstv 
    250  1.221   mlelstv /*
    251  1.221   mlelstv  * If NFS is specified as the file system, and we found
    252  1.221   mlelstv  * a DV_DISK boot device (or no boot device at all), then
    253  1.221   mlelstv  * find a reasonable network interface for "rootspec".
    254  1.221   mlelstv  */
    255  1.221   mlelstv static void
    256  1.221   mlelstv setroot_nfs(device_t dv)
    257  1.221   mlelstv {
    258  1.221   mlelstv 	struct vfsops *vops;
    259  1.221   mlelstv 	struct ifnet *ifp;
    260  1.221   mlelstv 
    261  1.197  pgoyette 	vops = vfs_getopsbyname(MOUNT_NFS);
    262  1.197  pgoyette 	if (vops != NULL && strcmp(rootfstype, MOUNT_NFS) == 0 &&
    263   1.18   thorpej 	    rootspec == NULL &&
    264  1.221   mlelstv 	    (dv == NULL || device_class(dv) != DV_IFNET)) {
    265  1.217     ozaki 		int s = pserialize_read_enter();
    266  1.217     ozaki 		IFNET_READER_FOREACH(ifp) {
    267   1.18   thorpej 			if ((ifp->if_flags &
    268   1.18   thorpej 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    269   1.18   thorpej 				break;
    270   1.83      matt 		}
    271  1.221   mlelstv 		if (ifp != NULL) {
    272   1.18   thorpej 			/*
    273   1.18   thorpej 			 * Have a suitable interface; behave as if
    274   1.18   thorpej 			 * the user specified this interface.
    275   1.18   thorpej 			 */
    276   1.18   thorpej 			rootspec = (const char *)ifp->if_xname;
    277   1.18   thorpej 		}
    278  1.217     ozaki 		pserialize_read_exit(s);
    279   1.18   thorpej 	}
    280  1.165        ad 	if (vops != NULL)
    281  1.165        ad 		vfs_delref(vops);
    282  1.221   mlelstv }
    283   1.24   thorpej 
    284  1.222   mlelstv /*
    285  1.222   mlelstv  * Change boot device to md0
    286  1.222   mlelstv  *
    287  1.222   mlelstv  * md0 only exists when it is opened once.
    288  1.222   mlelstv  */
    289  1.222   mlelstv static void
    290  1.222   mlelstv setroot_md(device_t *dvp)
    291  1.222   mlelstv {
    292  1.222   mlelstv 	int md_major;
    293  1.222   mlelstv 	dev_t md_dev;
    294  1.222   mlelstv 
    295  1.222   mlelstv 	md_major = devsw_name2blk("md", NULL, 0);
    296  1.222   mlelstv 	if (md_major >= 0) {
    297  1.222   mlelstv 		md_dev = MAKEDISKDEV(md_major, 0, RAW_PART);
    298  1.222   mlelstv 		if (bdev_open(md_dev, FREAD, S_IFBLK, curlwp) == 0)
    299  1.222   mlelstv 			*dvp = device_find_by_xname("md0");
    300  1.222   mlelstv 	}
    301  1.222   mlelstv }
    302  1.222   mlelstv 
    303  1.221   mlelstv static void
    304  1.221   mlelstv setroot_ask(device_t bootdv, int bootpartition)
    305  1.221   mlelstv {
    306  1.221   mlelstv 	device_t dv, defdumpdv, rootdv, dumpdv;
    307  1.221   mlelstv 	dev_t nrootdev, ndumpdev;
    308  1.221   mlelstv 	struct vfsops *vops;
    309  1.221   mlelstv 	const char *deffsname;
    310  1.221   mlelstv 	int len;
    311  1.221   mlelstv 	char buf[128];
    312   1.18   thorpej 
    313  1.221   mlelstv 	for (;;) {
    314  1.221   mlelstv 		printf("root device");
    315  1.221   mlelstv 		if (bootdv != NULL) {
    316  1.221   mlelstv 			printf(" (default %s", device_xname(bootdv));
    317  1.221   mlelstv 			if (DEV_USES_PARTITIONS(bootdv))
    318  1.221   mlelstv 				printf("%c", bootpartition + 'a');
    319  1.221   mlelstv 			printf(")");
    320  1.221   mlelstv 		}
    321  1.221   mlelstv 		printf(": ");
    322  1.221   mlelstv 		len = cngetsn(buf, sizeof(buf));
    323  1.221   mlelstv 		if (len == 0 && bootdv != NULL) {
    324  1.221   mlelstv 			strlcpy(buf, device_xname(bootdv), sizeof(buf));
    325  1.221   mlelstv 			len = strlen(buf);
    326  1.221   mlelstv 		}
    327  1.221   mlelstv 		if (len > 0 && buf[len - 1] == '*') {
    328  1.221   mlelstv 			buf[--len] = '\0';
    329  1.221   mlelstv 			dv = getdisk(buf, len, 1, &nrootdev, 0);
    330   1.18   thorpej 			if (dv != NULL) {
    331   1.18   thorpej 				rootdv = dv;
    332   1.18   thorpej 				break;
    333   1.18   thorpej 			}
    334   1.18   thorpej 		}
    335  1.221   mlelstv 		dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
    336  1.221   mlelstv 		if (dv != NULL) {
    337  1.221   mlelstv 			rootdv = dv;
    338  1.221   mlelstv 			break;
    339  1.221   mlelstv 		}
    340  1.221   mlelstv 	}
    341  1.221   mlelstv 	rootdev = nrootdev;
    342   1.18   thorpej 
    343  1.221   mlelstv 	/*
    344  1.221   mlelstv 	 * Set up the default dump device.  If root is on
    345  1.221   mlelstv 	 * a network device or a disk without partitions,
    346  1.221   mlelstv 	 * there is no default dump device.
    347  1.221   mlelstv 	 */
    348  1.221   mlelstv 	if (DEV_USES_PARTITIONS(rootdv) == 0)
    349  1.221   mlelstv 		defdumpdv = NULL;
    350  1.221   mlelstv 	else
    351  1.221   mlelstv 		defdumpdv = rootdv;
    352  1.221   mlelstv 
    353  1.221   mlelstv 	ndumpdev = NODEV;
    354  1.221   mlelstv 	for (;;) {
    355  1.221   mlelstv 		printf("dump device");
    356  1.221   mlelstv 		if (defdumpdv != NULL) {
    357  1.221   mlelstv 			/*
    358  1.221   mlelstv 			 * Note, we know it's a disk if we get here.
    359  1.221   mlelstv 			 */
    360  1.221   mlelstv 			printf(" (default %sb)", device_xname(defdumpdv));
    361  1.221   mlelstv 		}
    362  1.221   mlelstv 		printf(": ");
    363  1.221   mlelstv 		len = cngetsn(buf, sizeof(buf));
    364  1.221   mlelstv 		if (len == 0) {
    365   1.26   thorpej 			if (defdumpdv != NULL) {
    366  1.221   mlelstv 				ndumpdev = MAKEDISKDEV(major(nrootdev),
    367  1.221   mlelstv 				    DISKUNIT(nrootdev), 1);
    368   1.26   thorpej 			}
    369  1.221   mlelstv 			dumpdv = defdumpdv;
    370  1.221   mlelstv 			break;
    371  1.221   mlelstv 		}
    372  1.221   mlelstv 		if (len == 4 && strcmp(buf, "none") == 0) {
    373  1.221   mlelstv 			dumpdv = NULL;
    374  1.221   mlelstv 			break;
    375  1.221   mlelstv 		}
    376  1.221   mlelstv 		dv = getdisk(buf, len, 1, &ndumpdev, 1);
    377  1.221   mlelstv 		if (dv != NULL) {
    378  1.221   mlelstv 			dumpdv = dv;
    379  1.221   mlelstv 			break;
    380   1.26   thorpej 		}
    381  1.221   mlelstv 	}
    382  1.221   mlelstv 	dumpdev = ndumpdev;
    383   1.26   thorpej 
    384  1.221   mlelstv 	for (vops = LIST_FIRST(&vfs_list); vops != NULL;
    385  1.221   mlelstv 	     vops = LIST_NEXT(vops, vfs_list)) {
    386  1.221   mlelstv 		if (vops->vfs_mountroot != NULL &&
    387  1.221   mlelstv 		    strcmp(rootfstype, vops->vfs_name) == 0)
    388  1.221   mlelstv 		break;
    389  1.221   mlelstv 	}
    390  1.221   mlelstv 
    391  1.221   mlelstv 	if (vops == NULL) {
    392  1.221   mlelstv 		deffsname = "generic";
    393  1.221   mlelstv 	} else
    394  1.221   mlelstv 		deffsname = vops->vfs_name;
    395   1.18   thorpej 
    396  1.221   mlelstv 	for (;;) {
    397  1.221   mlelstv 		printf("file system (default %s): ", deffsname);
    398  1.221   mlelstv 		len = cngetsn(buf, sizeof(buf));
    399  1.221   mlelstv 		if (len == 0) {
    400  1.221   mlelstv 			if (strcmp(deffsname, "generic") == 0)
    401  1.221   mlelstv 				rootfstype = ROOT_FSTYPE_ANY;
    402   1.38      fvdl 			break;
    403   1.18   thorpej 		}
    404  1.221   mlelstv 		if (len == 4 && strcmp(buf, "halt") == 0)
    405  1.221   mlelstv 			cpu_reboot(RB_HALT, NULL);
    406  1.221   mlelstv 		else if (len == 6 && strcmp(buf, "reboot") == 0)
    407  1.221   mlelstv 			cpu_reboot(0, NULL);
    408   1.78   thorpej #if defined(DDB)
    409  1.221   mlelstv 		else if (len == 3 && strcmp(buf, "ddb") == 0) {
    410  1.221   mlelstv 			console_debugger();
    411  1.221   mlelstv 		}
    412   1.78   thorpej #endif
    413  1.221   mlelstv 		else if (len == 7 && strcmp(buf, "generic") == 0) {
    414  1.221   mlelstv 			rootfstype = ROOT_FSTYPE_ANY;
    415  1.221   mlelstv 			break;
    416  1.221   mlelstv 		}
    417  1.221   mlelstv 		vops = vfs_getopsbyname(buf);
    418  1.221   mlelstv 		if (vops == NULL || vops->vfs_mountroot == NULL) {
    419  1.221   mlelstv 			printf("use one of: generic");
    420  1.221   mlelstv 			for (vops = LIST_FIRST(&vfs_list);
    421  1.221   mlelstv 			     vops != NULL;
    422  1.221   mlelstv 			     vops = LIST_NEXT(vops, vfs_list)) {
    423  1.221   mlelstv 				if (vops->vfs_mountroot != NULL)
    424  1.221   mlelstv 					printf(" %s", vops->vfs_name);
    425   1.18   thorpej 			}
    426  1.221   mlelstv 			if (vops != NULL)
    427  1.221   mlelstv 				vfs_delref(vops);
    428   1.78   thorpej #if defined(DDB)
    429  1.221   mlelstv 			printf(" ddb");
    430   1.78   thorpej #endif
    431  1.221   mlelstv 			printf(" halt reboot\n");
    432  1.221   mlelstv 		} else {
    433  1.221   mlelstv 			/*
    434  1.221   mlelstv 			 * XXX If *vops gets freed between here and
    435  1.221   mlelstv 			 * the call to mountroot(), rootfstype will
    436  1.221   mlelstv 			 * point to something unexpected.  But in
    437  1.221   mlelstv 			 * this case the system will fail anyway.
    438  1.221   mlelstv 			 */
    439  1.221   mlelstv 			rootfstype = vops->vfs_name;
    440  1.221   mlelstv 			vfs_delref(vops);
    441  1.221   mlelstv 			break;
    442   1.18   thorpej 		}
    443  1.221   mlelstv 	}
    444  1.221   mlelstv 
    445  1.221   mlelstv 	root_device = rootdv;
    446  1.221   mlelstv 	setroot_dump(root_device, dumpdv);
    447  1.221   mlelstv }
    448  1.221   mlelstv 
    449  1.221   mlelstv /*
    450  1.221   mlelstv  * configure
    451  1.221   mlelstv  *   dev_t rootdev
    452  1.221   mlelstv  *
    453  1.221   mlelstv  * return device_t or NULL if not found
    454  1.221   mlelstv  */
    455  1.221   mlelstv static void
    456  1.221   mlelstv setroot_root(device_t bootdv, int bootpartition)
    457  1.221   mlelstv {
    458  1.221   mlelstv 	device_t rootdv;
    459  1.221   mlelstv 	int majdev;
    460  1.221   mlelstv 	const char *rootdevname;
    461  1.221   mlelstv 	char buf[128];
    462  1.221   mlelstv 
    463  1.221   mlelstv 	if (rootspec == NULL) {
    464   1.18   thorpej 
    465   1.18   thorpej 		/*
    466   1.18   thorpej 		 * Wildcarded root; use the boot device.
    467   1.18   thorpej 		 */
    468   1.26   thorpej 		rootdv = bootdv;
    469   1.26   thorpej 
    470  1.188  christos 		if (bootdv)
    471  1.188  christos 			majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
    472  1.188  christos 		else
    473  1.188  christos 			majdev = -1;
    474   1.18   thorpej 		if (majdev >= 0) {
    475   1.18   thorpej 			/*
    476  1.113   thorpej 			 * Root is on a disk.  `bootpartition' is root,
    477  1.113   thorpej 			 * unless the device does not use partitions.
    478   1.18   thorpej 			 */
    479  1.113   thorpej 			if (DEV_USES_PARTITIONS(bootdv))
    480  1.135   thorpej 				rootdev = MAKEDISKDEV(majdev,
    481  1.135   thorpej 						      device_unit(bootdv),
    482  1.135   thorpej 						      bootpartition);
    483  1.113   thorpej 			else
    484  1.135   thorpej 				rootdev = makedev(majdev, device_unit(bootdv));
    485   1.18   thorpej 		}
    486   1.18   thorpej 	} else {
    487   1.18   thorpej 
    488   1.18   thorpej 		/*
    489   1.25       mrg 		 * `root on <dev> ...'
    490   1.18   thorpej 		 */
    491   1.18   thorpej 
    492   1.18   thorpej 		/*
    493   1.18   thorpej 		 * If it's a network interface, we can bail out
    494   1.18   thorpej 		 * early.
    495   1.18   thorpej 		 */
    496  1.221   mlelstv 		rootdv = finddevice(rootspec);
    497  1.221   mlelstv 		if (rootdv != NULL && device_class(rootdv) == DV_IFNET)
    498   1.26   thorpej 			goto haveroot;
    499   1.18   thorpej 
    500  1.221   mlelstv 		if (rootdv != NULL && device_class(rootdv) == DV_DISK &&
    501  1.221   mlelstv 		    !DEV_USES_PARTITIONS(rootdv) &&
    502  1.221   mlelstv 		    (majdev = devsw_name2blk(device_xname(rootdv), NULL, 0)) >= 0) {
    503  1.221   mlelstv 			rootdev = makedev(majdev, device_unit(rootdv));
    504  1.220   mlelstv 			goto haveroot;
    505  1.220   mlelstv 		}
    506  1.159    dyoung 
    507   1.88   gehenna 		rootdevname = devsw_blk2name(major(rootdev));
    508   1.18   thorpej 		if (rootdevname == NULL) {
    509  1.198  christos 			printf("unknown device major 0x%llx\n",
    510  1.198  christos 			    (unsigned long long)rootdev);
    511  1.221   mlelstv 			return;
    512   1.18   thorpej 		}
    513   1.41     perry 		memset(buf, 0, sizeof(buf));
    514  1.198  christos 		snprintf(buf, sizeof(buf), "%s%llu", rootdevname,
    515  1.198  christos 		    (unsigned long long)DISKUNIT(rootdev));
    516   1.18   thorpej 
    517   1.56     enami 		rootdv = finddevice(buf);
    518   1.26   thorpej 		if (rootdv == NULL) {
    519  1.198  christos 			printf("device %s (0x%llx) not configured\n",
    520  1.198  christos 			    buf, (unsigned long long)rootdev);
    521  1.221   mlelstv 			return;
    522   1.18   thorpej 		}
    523   1.26   thorpej 	}
    524   1.18   thorpej 
    525  1.221   mlelstv haveroot:
    526  1.124   thorpej 	switch (device_class(rootdv)) {
    527   1.18   thorpej 	case DV_IFNET:
    528  1.139  christos 	case DV_DISK:
    529  1.184    cegger 		aprint_normal("root on %s", device_xname(rootdv));
    530  1.140  christos 		if (DEV_USES_PARTITIONS(rootdv))
    531  1.198  christos 			aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
    532   1.18   thorpej 		break;
    533   1.18   thorpej 	default:
    534   1.18   thorpej 		printf("can't determine root device\n");
    535  1.221   mlelstv 		return;
    536   1.18   thorpej 	}
    537   1.26   thorpej 
    538  1.221   mlelstv 	root_device = rootdv;
    539  1.221   mlelstv 	setroot_dump(rootdv, NULL);
    540  1.221   mlelstv }
    541  1.221   mlelstv 
    542  1.221   mlelstv /*
    543  1.221   mlelstv  * configure
    544  1.221   mlelstv  *   dev_t dumpdev
    545  1.221   mlelstv  *   dev_t dumpcdev
    546  1.221   mlelstv  *
    547  1.221   mlelstv  * set to NODEV if device not found
    548  1.221   mlelstv  */
    549  1.221   mlelstv static void
    550  1.221   mlelstv setroot_dump(device_t rootdv, device_t dumpdv)
    551  1.221   mlelstv {
    552  1.221   mlelstv 	device_t dv;
    553  1.221   mlelstv 	deviter_t di;
    554  1.221   mlelstv 	const char *dumpdevname;
    555  1.221   mlelstv 	int majdev;
    556  1.221   mlelstv 	char buf[128];
    557  1.221   mlelstv 
    558   1.26   thorpej 	/*
    559   1.26   thorpej 	 * Now configure the dump device.
    560   1.55     enami 	 *
    561   1.26   thorpej 	 * If we haven't figured out the dump device, do so, with
    562   1.26   thorpej 	 * the following rules:
    563   1.26   thorpej 	 *
    564   1.26   thorpej 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
    565   1.26   thorpej 	 *
    566   1.26   thorpej 	 *	(b) If dumpspec is set, try to use it.  If the device
    567   1.26   thorpej 	 *	    is not available, punt.
    568   1.26   thorpej 	 *
    569   1.26   thorpej 	 *	(c) If dumpspec is not set, the dump device is
    570   1.26   thorpej 	 *	    wildcarded or unspecified.  If the root device
    571   1.26   thorpej 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
    572   1.26   thorpej 	 *	    of the root device.
    573   1.26   thorpej 	 */
    574   1.26   thorpej 
    575   1.55     enami 	if (boothowto & RB_ASKNAME) {		/* (a) */
    576   1.55     enami 		if (dumpdv == NULL)
    577   1.55     enami 			goto nodumpdev;
    578   1.55     enami 	} else if (dumpspec != NULL) {		/* (b) */
    579   1.55     enami 		if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
    580   1.26   thorpej 			/*
    581   1.55     enami 			 * Operator doesn't want a dump device.
    582   1.55     enami 			 * Or looks like they tried to pick a network
    583   1.26   thorpej 			 * device.  Oops.
    584   1.26   thorpej 			 */
    585   1.26   thorpej 			goto nodumpdev;
    586   1.26   thorpej 		}
    587   1.26   thorpej 
    588   1.88   gehenna 		dumpdevname = devsw_blk2name(major(dumpdev));
    589   1.26   thorpej 		if (dumpdevname == NULL)
    590   1.26   thorpej 			goto nodumpdev;
    591   1.41     perry 		memset(buf, 0, sizeof(buf));
    592  1.198  christos 		snprintf(buf, sizeof(buf), "%s%llu", dumpdevname,
    593  1.198  christos 		    (unsigned long long)DISKUNIT(dumpdev));
    594   1.26   thorpej 
    595   1.56     enami 		dumpdv = finddevice(buf);
    596   1.56     enami 		if (dumpdv == NULL) {
    597   1.26   thorpej 			/*
    598   1.26   thorpej 			 * Device not configured.
    599   1.26   thorpej 			 */
    600   1.26   thorpej 			goto nodumpdev;
    601   1.26   thorpej 		}
    602   1.55     enami 	} else {				/* (c) */
    603  1.160  christos 		if (DEV_USES_PARTITIONS(rootdv) == 0) {
    604  1.203    dyoung 			for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    605  1.203    dyoung 			     dv != NULL;
    606  1.203    dyoung 			     dv = deviter_next(&di))
    607  1.160  christos 				if (isswap(dv))
    608  1.160  christos 					break;
    609  1.203    dyoung 			deviter_release(&di);
    610  1.160  christos 			if (dv == NULL)
    611  1.160  christos 				goto nodumpdev;
    612  1.160  christos 
    613  1.184    cegger 			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
    614  1.160  christos 			if (majdev < 0)
    615  1.160  christos 				goto nodumpdev;
    616  1.160  christos 			dumpdv = dv;
    617  1.160  christos 			dumpdev = makedev(majdev, device_unit(dumpdv));
    618  1.160  christos 		} else {
    619   1.55     enami 			dumpdv = rootdv;
    620   1.55     enami 			dumpdev = MAKEDISKDEV(major(rootdev),
    621  1.135   thorpej 			    device_unit(dumpdv), 1);
    622   1.55     enami 		}
    623   1.26   thorpej 	}
    624   1.26   thorpej 
    625  1.175        ad 	dumpcdev = devsw_blk2chr(dumpdev);
    626  1.184    cegger 	aprint_normal(" dumps on %s", device_xname(dumpdv));
    627  1.140  christos 	if (DEV_USES_PARTITIONS(dumpdv))
    628  1.198  christos 		aprint_normal("%c", (int)DISKPART(dumpdev) + 'a');
    629  1.140  christos 	aprint_normal("\n");
    630   1.26   thorpej 	return;
    631   1.26   thorpej 
    632   1.26   thorpej  nodumpdev:
    633   1.26   thorpej 	dumpdev = NODEV;
    634  1.175        ad 	dumpcdev = NODEV;
    635  1.100   thorpej 	aprint_normal("\n");
    636   1.18   thorpej }
    637   1.18   thorpej 
    638  1.200    dyoung static device_t
    639  1.117   thorpej finddevice(const char *name)
    640   1.56     enami {
    641  1.159    dyoung 	const char *wname;
    642  1.108  christos 
    643  1.159    dyoung 	if ((wname = getwedgename(name, strlen(name))) != NULL)
    644  1.159    dyoung 		return dkwedge_find_by_wname(wname);
    645  1.159    dyoung 
    646  1.179     joerg 	return device_find_by_xname(name);
    647   1.56     enami }
    648   1.56     enami 
    649  1.200    dyoung static device_t
    650  1.117   thorpej getdisk(char *str, int len, int defpart, dev_t *devp, int isdump)
    651   1.18   thorpej {
    652  1.200    dyoung 	device_t dv;
    653  1.203    dyoung 	deviter_t di;
    654   1.18   thorpej 
    655   1.51   thorpej 	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
    656   1.18   thorpej 		printf("use one of:");
    657  1.203    dyoung 		for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
    658  1.203    dyoung 		     dv = deviter_next(&di)) {
    659  1.113   thorpej 			if (DEV_USES_PARTITIONS(dv))
    660  1.184    cegger 				printf(" %s[a-%c]", device_xname(dv),
    661   1.19       cgd 				    'a' + MAXPARTITIONS - 1);
    662  1.124   thorpej 			else if (device_class(dv) == DV_DISK)
    663  1.184    cegger 				printf(" %s", device_xname(dv));
    664  1.124   thorpej 			if (isdump == 0 && device_class(dv) == DV_IFNET)
    665  1.184    cegger 				printf(" %s", device_xname(dv));
    666   1.18   thorpej 		}
    667  1.203    dyoung 		deviter_release(&di);
    668  1.159    dyoung 		dkwedge_print_wnames();
    669   1.26   thorpej 		if (isdump)
    670   1.26   thorpej 			printf(" none");
    671   1.78   thorpej #if defined(DDB)
    672   1.78   thorpej 		printf(" ddb");
    673   1.78   thorpej #endif
    674   1.76   thorpej 		printf(" halt reboot\n");
    675   1.18   thorpej 	}
    676  1.159    dyoung 	return dv;
    677  1.159    dyoung }
    678  1.159    dyoung 
    679  1.159    dyoung static const char *
    680  1.159    dyoung getwedgename(const char *name, int namelen)
    681  1.159    dyoung {
    682  1.159    dyoung 	const char *wpfx = "wedge:";
    683  1.159    dyoung 	const int wpfxlen = strlen(wpfx);
    684  1.159    dyoung 
    685  1.159    dyoung 	if (namelen < wpfxlen || strncmp(name, wpfx, wpfxlen) != 0)
    686  1.159    dyoung 		return NULL;
    687  1.159    dyoung 
    688  1.159    dyoung 	return name + wpfxlen;
    689   1.18   thorpej }
    690   1.18   thorpej 
    691  1.200    dyoung static device_t
    692  1.117   thorpej parsedisk(char *str, int len, int defpart, dev_t *devp)
    693   1.18   thorpej {
    694  1.200    dyoung 	device_t dv;
    695  1.159    dyoung 	const char *wname;
    696   1.18   thorpej 	char *cp, c;
    697   1.29  drochner 	int majdev, part;
    698   1.18   thorpej 	if (len == 0)
    699   1.18   thorpej 		return (NULL);
    700   1.18   thorpej 
    701   1.18   thorpej 	if (len == 4 && strcmp(str, "halt") == 0)
    702   1.23       gwr 		cpu_reboot(RB_HALT, NULL);
    703   1.76   thorpej 	else if (len == 6 && strcmp(str, "reboot") == 0)
    704   1.76   thorpej 		cpu_reboot(0, NULL);
    705   1.78   thorpej #if defined(DDB)
    706   1.78   thorpej 	else if (len == 3 && strcmp(str, "ddb") == 0)
    707   1.78   thorpej 		console_debugger();
    708   1.78   thorpej #endif
    709   1.18   thorpej 
    710   1.18   thorpej 	cp = str + len - 1;
    711   1.18   thorpej 	c = *cp;
    712  1.159    dyoung 
    713  1.159    dyoung 	if ((wname = getwedgename(str, len)) != NULL) {
    714  1.159    dyoung 		if ((dv = dkwedge_find_by_wname(wname)) == NULL)
    715  1.159    dyoung 			return NULL;
    716  1.159    dyoung 		part = defpart;
    717  1.159    dyoung 		goto gotdisk;
    718  1.159    dyoung 	} else if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
    719   1.18   thorpej 		part = c - 'a';
    720   1.18   thorpej 		*cp = '\0';
    721   1.18   thorpej 	} else
    722   1.18   thorpej 		part = defpart;
    723   1.18   thorpej 
    724   1.56     enami 	dv = finddevice(str);
    725   1.56     enami 	if (dv != NULL) {
    726  1.124   thorpej 		if (device_class(dv) == DV_DISK) {
    727   1.18   thorpej  gotdisk:
    728  1.184    cegger 			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
    729   1.18   thorpej 			if (majdev < 0)
    730   1.18   thorpej 				panic("parsedisk");
    731  1.113   thorpej 			if (DEV_USES_PARTITIONS(dv))
    732  1.135   thorpej 				*devp = MAKEDISKDEV(majdev, device_unit(dv),
    733  1.135   thorpej 						    part);
    734  1.113   thorpej 			else
    735  1.135   thorpej 				*devp = makedev(majdev, device_unit(dv));
    736   1.18   thorpej 		}
    737   1.18   thorpej 
    738  1.124   thorpej 		if (device_class(dv) == DV_IFNET)
    739   1.18   thorpej 			*devp = NODEV;
    740   1.18   thorpej 	}
    741   1.18   thorpej 
    742   1.18   thorpej 	*cp = c;
    743   1.18   thorpej 	return (dv);
    744   1.48     lukem }
    745