xref: /netbsd-src/sys/kern/kern_subr.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: kern_subr.c,v 1.229 2020/11/21 08:10:27 mlelstv 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.229 2020/11/21 08:10:27 mlelstv 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 			kern_reboot(RB_HALT, NULL);
409 		else if (len == 6 && strcmp(buf, "reboot") == 0)
410 			kern_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 	switch (device_class(rootdv)) {
449 	case DV_IFNET:
450 	case DV_DISK:
451 		aprint_normal("root on %s", device_xname(rootdv));
452 		if (DEV_USES_PARTITIONS(rootdv))
453 			aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
454 		break;
455 	default:
456 		printf("can't determine root device\n");
457 		return;
458 	}
459 
460 	root_device = rootdv;
461 	setroot_dump(rootdv, dumpdv);
462 }
463 
464 /*
465  * configure
466  *   device_t root_device
467  *   dev_t rootdev (for disks)
468  *
469  */
470 static void
471 setroot_root(device_t bootdv, int bootpartition)
472 {
473 	device_t rootdv;
474 	int majdev;
475 	const char *rootdevname;
476 	char buf[128];
477 	dev_t nrootdev;
478 
479 	if (rootspec == NULL) {
480 
481 		/*
482 		 * Wildcarded root; use the boot device.
483 		 */
484 		rootdv = bootdv;
485 
486 		if (bootdv)
487 			majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
488 		else
489 			majdev = -1;
490 		if (majdev >= 0) {
491 			/*
492 			 * Root is on a disk.  `bootpartition' is root,
493 			 * unless the device does not use partitions.
494 			 */
495 			if (DEV_USES_PARTITIONS(bootdv))
496 				rootdev = MAKEDISKDEV(majdev,
497 						      device_unit(bootdv),
498 						      bootpartition);
499 			else
500 				rootdev = makedev(majdev, device_unit(bootdv));
501 		}
502 	} else {
503 
504 		/*
505 		 * `root on <dev> ...'
506 		 */
507 
508 		/*
509 		 * If rootspec can be parsed, just use it.
510 		 */
511 		rootdv = parsedisk(rootspec, strlen(rootspec), 0, &nrootdev);
512 		if (rootdv != NULL) {
513 			rootdev = nrootdev;
514 			goto haveroot;
515 		}
516 
517 		/*
518 		 * Fall back to rootdev, compute rootdv for it
519 		 */
520 		rootdevname = devsw_blk2name(major(rootdev));
521 		if (rootdevname == NULL) {
522 			printf("unknown device major 0x%llx\n",
523 			    (unsigned long long)rootdev);
524 			return;
525 		}
526 		memset(buf, 0, sizeof(buf));
527 		snprintf(buf, sizeof(buf), "%s%llu", rootdevname,
528 		    (unsigned long long)DISKUNIT(rootdev));
529 
530 		rootdv = finddevice(buf);
531 		if (rootdv == NULL) {
532 			printf("device %s (0x%llx) not configured\n",
533 			    buf, (unsigned long long)rootdev);
534 			return;
535 		}
536 	}
537 
538 haveroot:
539 	switch (device_class(rootdv)) {
540 	case DV_IFNET:
541 	case DV_DISK:
542 		aprint_normal("root on %s", device_xname(rootdv));
543 		if (DEV_USES_PARTITIONS(rootdv))
544 			aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
545 		break;
546 	default:
547 		printf("can't determine root device\n");
548 		return;
549 	}
550 
551 	root_device = rootdv;
552 	setroot_dump(rootdv, NULL);
553 }
554 
555 /*
556  * configure
557  *   dev_t dumpdev
558  *   dev_t dumpcdev
559  *
560  * set to NODEV if device not found
561  */
562 static void
563 setroot_dump(device_t rootdv, device_t dumpdv)
564 {
565 	device_t dv;
566 	deviter_t di;
567 	const char *dumpdevname;
568 	int majdev;
569 	char buf[128];
570 
571 	/*
572 	 * Now configure the dump device.
573 	 *
574 	 * If we haven't figured out the dump device, do so, with
575 	 * the following rules:
576 	 *
577 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
578 	 *
579 	 *	(b) If dumpspec is set, try to use it.  If the device
580 	 *	    is not available, punt.
581 	 *
582 	 *	(c) If dumpspec is not set, the dump device is
583 	 *	    wildcarded or unspecified.  If the root device
584 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
585 	 *	    of the root device.
586 	 */
587 
588 	if (boothowto & RB_ASKNAME) {		/* (a) */
589 		if (dumpdv == NULL)
590 			goto nodumpdev;
591 	} else if (dumpspec != NULL) {		/* (b) */
592 		if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
593 			/*
594 			 * Operator doesn't want a dump device.
595 			 * Or looks like they tried to pick a network
596 			 * device.  Oops.
597 			 */
598 			goto nodumpdev;
599 		}
600 
601 		dumpdevname = devsw_blk2name(major(dumpdev));
602 		if (dumpdevname == NULL)
603 			goto nodumpdev;
604 		memset(buf, 0, sizeof(buf));
605 		snprintf(buf, sizeof(buf), "%s%llu", dumpdevname,
606 		    (unsigned long long)DISKUNIT(dumpdev));
607 
608 		dumpdv = finddevice(buf);
609 		if (dumpdv == NULL) {
610 			/*
611 			 * Device not configured.
612 			 */
613 			goto nodumpdev;
614 		}
615 	} else {				/* (c) */
616 		if (DEV_USES_PARTITIONS(rootdv) == 0) {
617 			for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
618 			     dv != NULL;
619 			     dv = deviter_next(&di))
620 				if (isswap(dv))
621 					break;
622 			deviter_release(&di);
623 			if (dv == NULL)
624 				goto nodumpdev;
625 
626 			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
627 			if (majdev < 0)
628 				goto nodumpdev;
629 			dumpdv = dv;
630 			dumpdev = makedev(majdev, device_unit(dumpdv));
631 		} else {
632 			dumpdv = rootdv;
633 			dumpdev = MAKEDISKDEV(major(rootdev),
634 			    device_unit(dumpdv), 1);
635 		}
636 	}
637 
638 	dumpcdev = devsw_blk2chr(dumpdev);
639 	aprint_normal(" dumps on %s", device_xname(dumpdv));
640 	if (DEV_USES_PARTITIONS(dumpdv))
641 		aprint_normal("%c", (int)DISKPART(dumpdev) + 'a');
642 	aprint_normal("\n");
643 	return;
644 
645  nodumpdev:
646 	dumpdev = NODEV;
647 	dumpcdev = NODEV;
648 	aprint_normal("\n");
649 }
650 
651 static device_t
652 finddevice(const char *name)
653 {
654 	const char *wname;
655 
656 	if ((wname = getwedgename(name, strlen(name))) != NULL)
657 		return dkwedge_find_by_wname(wname);
658 
659 	return device_find_by_xname(name);
660 }
661 
662 static device_t
663 getdisk(const char *str, int len, int defpart, dev_t *devp, int isdump)
664 {
665 	device_t dv;
666 	deviter_t di;
667 
668 	if (len == 4 && strcmp(str, "halt") == 0)
669 		kern_reboot(RB_HALT, NULL);
670 	else if (len == 6 && strcmp(str, "reboot") == 0)
671 		kern_reboot(0, NULL);
672 #if defined(DDB)
673 	else if (len == 3 && strcmp(str, "ddb") == 0) {
674 		console_debugger();
675 		return NULL;
676 	}
677 #endif
678 
679 	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
680 		printf("use one of:");
681 		for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
682 		     dv = deviter_next(&di)) {
683 			if (DEV_USES_PARTITIONS(dv))
684 				printf(" %s[a-%c]", device_xname(dv),
685 				    'a' + MAXPARTITIONS - 1);
686 			else if (device_class(dv) == DV_DISK)
687 				printf(" %s", device_xname(dv));
688 			if (isdump == 0 && device_class(dv) == DV_IFNET)
689 				printf(" %s", device_xname(dv));
690 		}
691 		deviter_release(&di);
692 		dkwedge_print_wnames();
693 		if (isdump)
694 			printf(" none");
695 #if defined(DDB)
696 		printf(" ddb");
697 #endif
698 		printf(" halt reboot\n");
699 	}
700 	return dv;
701 }
702 
703 static const char *
704 getwedgename(const char *name, int namelen)
705 {
706 	const char *wpfx1 = "wedge:";
707 	const char *wpfx2 = "NAME=";
708 	const int wpfx1len = strlen(wpfx1);
709 	const int wpfx2len = strlen(wpfx2);
710 
711 	if (namelen > wpfx1len && strncmp(name, wpfx1, wpfx1len) == 0)
712 		return name + wpfx1len;
713 
714 	if (namelen > wpfx2len && strncasecmp(name, wpfx2, wpfx2len) == 0)
715 		return name + wpfx2len;
716 
717 	return NULL;
718 }
719 
720 static device_t
721 parsedisk(const char *str, int len, int defpart, dev_t *devp)
722 {
723 	device_t dv;
724 	const char *wname;
725 	char c;
726 	int majdev, part;
727 	char xname[16]; /* same size as dv_xname */
728 
729 	if (len == 0)
730 		return (NULL);
731 
732 	if ((wname = getwedgename(str, len)) != NULL) {
733 		if ((dv = dkwedge_find_by_wname(wname)) == NULL)
734 			return NULL;
735 		part = defpart;
736 		goto gotdisk;
737 	}
738 
739 	c = str[len-1];
740 	if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
741 		part = c - 'a';
742 		len--;
743 		if (len > sizeof(xname)-1)
744 			return NULL;
745 		memcpy(xname, str, len);
746 		xname[len] = '\0';
747 		str = xname;
748 	} else
749 		part = defpart;
750 
751 	dv = finddevice(str);
752 	if (dv != NULL) {
753 		if (device_class(dv) == DV_DISK) {
754  gotdisk:
755 			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
756 			if (majdev < 0)
757 				panic("parsedisk");
758 			if (DEV_USES_PARTITIONS(dv))
759 				*devp = MAKEDISKDEV(majdev, device_unit(dv),
760 						    part);
761 			else
762 				*devp = makedev(majdev, device_unit(dv));
763 		}
764 
765 		if (device_class(dv) == DV_IFNET)
766 			*devp = NODEV;
767 	}
768 
769 	return (dv);
770 }
771