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