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