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