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