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