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