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