Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*	$NetBSD: kern_subr.c,v 1.223.4.2 2021/03/05 13:48:27 martin Exp $	*/

/*-
 * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
 * NASA Ames Research Center, and by Luke Mewburn.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 1982, 1986, 1991, 1993
 *	The Regents of the University of California.  All rights reserved.
 * (c) UNIX System Laboratories, Inc.
 * All or some portions of this file are derived from material licensed
 * to the University of California by American Telephone and Telegraph
 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
 * the permission of UNIX System Laboratories, Inc.
 *
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This software was developed by the Computer Systems Engineering group
 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
 * contributed to Berkeley.
 *
 * All advertising materials mentioning features or use of this software
 * must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Lawrence Berkeley Laboratory.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)kern_subr.c	8.4 (Berkeley) 2/14/95
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.223.4.2 2021/03/05 13:48:27 martin Exp $");

#include "opt_ddb.h"
#include "opt_md.h"
#include "opt_tftproot.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/device.h>
#include <sys/reboot.h>
#include <sys/conf.h>
#include <sys/disk.h>
#include <sys/disklabel.h>
#include <sys/queue.h>
#include <sys/fcntl.h>
#include <sys/kauth.h>
#include <sys/stat.h>
#include <sys/vnode.h>
#include <sys/module.h>

#include <dev/cons.h>

#include <net/if.h>

/* XXX these should eventually move to subr_autoconf.c */
static device_t finddevice(const char *);
static device_t getdisk(char *, int, int, dev_t *, int);
static device_t parsedisk(char *, int, int, dev_t *);
static const char *getwedgename(const char *, int);

static void setroot_nfs(device_t);
static void setroot_md(device_t *);
static void setroot_ask(device_t, int);
static void setroot_root(device_t, int);
static void setroot_dump(device_t, device_t);


#ifdef TFTPROOT
int tftproot_dhcpboot(device_t);
#endif

dev_t	dumpcdev;	/* for savecore */

static int
isswap(device_t dv)
{
	struct dkwedge_info wi;
	struct vnode *vn;
	int error;

	if (device_class(dv) != DV_DISK || !device_is_a(dv, "dk"))
		return 0;

	if ((vn = opendisk(dv)) == NULL)
		return 0;

	error = VOP_IOCTL(vn, DIOCGWEDGEINFO, &wi, FREAD, NOCRED);
	VOP_CLOSE(vn, FREAD, NOCRED);
	vput(vn);
	if (error) {
#ifdef DEBUG_WEDGE
		printf("%s: Get wedge info returned %d\n", device_xname(dv), error);
#endif
		return 0;
	}
	return strcmp(wi.dkw_ptype, DKW_PTYPE_SWAP) == 0;
}

/*
 * Determine the root device and, if instructed to, the root file system.
 */

#ifdef MEMORY_DISK_IS_ROOT
int md_is_root = 1;
#else
int md_is_root = 0;
#endif

/*
 * The device and partition that we booted from.
 *
 * This data might be initialized by MD code, but is defined here.
 */
device_t booted_device;
const char *booted_method;
int booted_partition;
daddr_t booted_startblk;
uint64_t booted_nblks;
char *bootspec;

/*
 * Use partition letters if it's a disk class but not a wedge or flash.
 * XXX Check for wedge/flash is kinda gross.
 */
#define	DEV_USES_PARTITIONS(dv)						\
	(device_class((dv)) == DV_DISK &&				\
	 !device_is_a((dv), "dk") &&					\
	 !device_is_a((dv), "flash"))

void
setroot(device_t bootdv, int bootpartition)
{

	/*
	 * Let bootcode augment "rootspec".
	 */
	if (rootspec == NULL)
		rootspec = bootspec;

	/*
	 * force boot device to md0
	 */
	if (md_is_root)
		setroot_md(&bootdv);

#ifdef TFTPROOT
	/*
	 * XXX
	 * if rootspec specifies an interface
	 * sets root_device to that interface
	 * reuses NFS init code to set up network
	 * fetch image into ram disk
	 *
	 * if successful, we change boot device
	 */
	if (tftproot_dhcpboot(bootdv) == 0)
		setroot_md(&bootdv);
#endif
	
	/*
	 * quirk for
	 *  evbarm/mini2440
	 *  hpcarm
	 *  hpcmips
	 *  hpcsh
	 *
	 * if rootfstype is set to NFS and the
	 * kernel supports NFS and the boot device
	 * is unknown or not a network interface
 	 * -> chose the first network interface you find
	 *
	 * hp300 has similar MD code
	 */
	setroot_nfs(bootdv);

	/*
	 * If no bootdv was found by MD code and no
	 * root specified ask the user.
	 */
	if (rootspec == NULL && bootdv == NULL)
		boothowto |= RB_ASKNAME;

	/*
	 * loop until a root device is specified
	 */
	do {
		if (boothowto & RB_ASKNAME)
			setroot_ask(bootdv, bootpartition);
		else
			setroot_root(bootdv, bootpartition);

		if (root_device == NULL)
			boothowto |= RB_ASKNAME;
	} while (root_device == NULL);
}

/*
 * If NFS is specified as the file system, and we found
 * a DV_DISK boot device (or no boot device at all), then
 * find a reasonable network interface for "rootspec".
 */
static void
setroot_nfs(device_t dv)
{
	struct vfsops *vops;
	struct ifnet *ifp;

	vops = vfs_getopsbyname(MOUNT_NFS);
	if (vops != NULL && strcmp(rootfstype, MOUNT_NFS) == 0 &&
	    rootspec == NULL &&
	    (dv == NULL || device_class(dv) != DV_IFNET)) {
		int s = pserialize_read_enter();
		IFNET_READER_FOREACH(ifp) {
			if ((ifp->if_flags &
			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
				break;
		}
		if (ifp != NULL) {
			/*
			 * Have a suitable interface; behave as if
			 * the user specified this interface.
			 */
			rootspec = (const char *)ifp->if_xname;
		}
		pserialize_read_exit(s);
	}
	if (vops != NULL)
		vfs_delref(vops);
}

/*
 * Change boot device to md0
 *
 * md0 only exists when it is opened once.
 */
static void
setroot_md(device_t *dvp)
{
	int md_major;
	dev_t md_dev;

	md_major = devsw_name2blk("md", NULL, 0);
	if (md_major >= 0) {
		md_dev = MAKEDISKDEV(md_major, 0, RAW_PART);
		if (bdev_open(md_dev, FREAD, S_IFBLK, curlwp) == 0)
			*dvp = device_find_by_xname("md0");
	}
}

static void
setroot_ask(device_t bootdv, int bootpartition)
{
	device_t dv, defdumpdv, rootdv, dumpdv;
	dev_t nrootdev, ndumpdev;
	struct vfsops *vops;
	const char *deffsname;
	int len;
	char buf[128];

	for (;;) {
		printf("root device");
		if (bootdv != NULL) {
			printf(" (default %s", device_xname(bootdv));
			if (DEV_USES_PARTITIONS(bootdv))
				printf("%c", bootpartition + 'a');
			printf(")");
		}
		printf(": ");
		len = cngetsn(buf, sizeof(buf));
		if (len == 0 && bootdv != NULL) {
			strlcpy(buf, device_xname(bootdv), sizeof(buf));
			len = strlen(buf);
		}
		if (len > 0 && buf[len - 1] == '*') {
			buf[--len] = '\0';
			dv = getdisk(buf, len, 1, &nrootdev, 0);
			if (dv != NULL) {
				rootdv = dv;
				break;
			}
		}
		dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
		if (dv != NULL) {
			rootdv = dv;
			break;
		}
	}
	rootdev = nrootdev;

	/*
	 * Set up the default dump device.  If root is on
	 * a network device or a disk without partitions,
	 * there is no default dump device.
	 */
	if (DEV_USES_PARTITIONS(rootdv) == 0)
		defdumpdv = NULL;
	else
		defdumpdv = rootdv;

	ndumpdev = NODEV;
	for (;;) {
		printf("dump device");
		if (defdumpdv != NULL) {
			/*
			 * Note, we know it's a disk if we get here.
			 */
			printf(" (default %sb)", device_xname(defdumpdv));
		}
		printf(": ");
		len = cngetsn(buf, sizeof(buf));
		if (len == 0) {
			if (defdumpdv != NULL) {
				ndumpdev = MAKEDISKDEV(major(nrootdev),
				    DISKUNIT(nrootdev), 1);
			}
			dumpdv = defdumpdv;
			break;
		}
		if (len == 4 && strcmp(buf, "none") == 0) {
			dumpdv = NULL;
			break;
		}
		dv = getdisk(buf, len, 1, &ndumpdev, 1);
		if (dv != NULL) {
			dumpdv = dv;
			break;
		}
	}
	dumpdev = ndumpdev;

	for (vops = LIST_FIRST(&vfs_list); vops != NULL;
	     vops = LIST_NEXT(vops, vfs_list)) {
		if (vops->vfs_mountroot != NULL &&
		    strcmp(rootfstype, vops->vfs_name) == 0)
		break;
	}

	if (vops == NULL) {
		deffsname = "generic";
	} else
		deffsname = vops->vfs_name;

	for (;;) {
		printf("file system (default %s): ", deffsname);
		len = cngetsn(buf, sizeof(buf));
		if (len == 0) {
			if (strcmp(deffsname, "generic") == 0)
				rootfstype = ROOT_FSTYPE_ANY;
			break;
		}
		if (len == 4 && strcmp(buf, "halt") == 0)
			cpu_reboot(RB_HALT, NULL);
		else if (len == 6 && strcmp(buf, "reboot") == 0)
			cpu_reboot(0, NULL);
#if defined(DDB)
		else if (len == 3 && strcmp(buf, "ddb") == 0) {
			console_debugger();
		}
#endif
		else if (len == 7 && strcmp(buf, "generic") == 0) {
			rootfstype = ROOT_FSTYPE_ANY;
			break;
		}
		vops = vfs_getopsbyname(buf);
		if (vops == NULL || vops->vfs_mountroot == NULL) {
			printf("use one of: generic");
			for (vops = LIST_FIRST(&vfs_list);
			     vops != NULL;
			     vops = LIST_NEXT(vops, vfs_list)) {
				if (vops->vfs_mountroot != NULL)
					printf(" %s", vops->vfs_name);
			}
			if (vops != NULL)
				vfs_delref(vops);
#if defined(DDB)
			printf(" ddb");
#endif
			printf(" halt reboot\n");
		} else {
			/*
			 * XXX If *vops gets freed between here and
			 * the call to mountroot(), rootfstype will
			 * point to something unexpected.  But in
			 * this case the system will fail anyway.
			 */
			rootfstype = vops->vfs_name;
			vfs_delref(vops);
			break;
		}
	}

	switch (device_class(rootdv)) {
	case DV_IFNET:
	case DV_DISK:
		aprint_normal("root on %s", device_xname(rootdv));
		if (DEV_USES_PARTITIONS(rootdv))
			aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
		break;
	default:
		printf("can't determine root device\n");
		return;
	}

	root_device = rootdv;
	setroot_dump(rootdv, dumpdv);
}

/*
 * configure
 *   dev_t rootdev
 * 
 * return device_t or NULL if not found
 */
static void
setroot_root(device_t bootdv, int bootpartition)
{
	device_t rootdv;
	int majdev;
	const char *rootdevname;
	char buf[128];

	if (rootspec == NULL) {

		/*
		 * Wildcarded root; use the boot device.
		 */
		rootdv = bootdv;

		if (bootdv)
			majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
		else
			majdev = -1;
		if (majdev >= 0) {
			/*
			 * Root is on a disk.  `bootpartition' is root,
			 * unless the device does not use partitions.
			 */
			if (DEV_USES_PARTITIONS(bootdv))
				rootdev = MAKEDISKDEV(majdev,
						      device_unit(bootdv),
						      bootpartition);
			else
				rootdev = makedev(majdev, device_unit(bootdv));
		}
	} else {

		/*
		 * `root on <dev> ...'
		 */

		/*
		 * If it's a network interface, we can bail out
		 * early.
		 */
		rootdv = finddevice(rootspec);
		if (rootdv != NULL && device_class(rootdv) == DV_IFNET)
			goto haveroot;

		if (rootdv != NULL && device_class(rootdv) == DV_DISK &&
		    !DEV_USES_PARTITIONS(rootdv) &&
		    (majdev = devsw_name2blk(device_xname(rootdv), NULL, 0)) >= 0) {
			rootdev = makedev(majdev, device_unit(rootdv));
			goto haveroot;
		}

		rootdevname = devsw_blk2name(major(rootdev));
		if (rootdevname == NULL) {
			printf("unknown device major 0x%llx\n",
			    (unsigned long long)rootdev);
			return;
		}
		memset(buf, 0, sizeof(buf));
		snprintf(buf, sizeof(buf), "%s%llu", rootdevname,
		    (unsigned long long)DISKUNIT(rootdev));

		rootdv = finddevice(buf);
		if (rootdv == NULL) {
			printf("device %s (0x%llx) not configured\n",
			    buf, (unsigned long long)rootdev);
			return;
		}
	}

haveroot:
	switch (device_class(rootdv)) {
	case DV_IFNET:
	case DV_DISK:
		aprint_normal("root on %s", device_xname(rootdv));
		if (DEV_USES_PARTITIONS(rootdv))
			aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
		break;
	default:
		printf("can't determine root device\n");
		return;
	}

	root_device = rootdv;
	setroot_dump(rootdv, NULL);
}

/*
 * configure
 *   dev_t dumpdev
 *   dev_t dumpcdev
 *
 * set to NODEV if device not found
 */
static void
setroot_dump(device_t rootdv, device_t dumpdv)
{
	device_t dv;
	deviter_t di;
	const char *dumpdevname;
	int majdev;
	char buf[128];

	/*
	 * Now configure the dump device.
	 *
	 * If we haven't figured out the dump device, do so, with
	 * the following rules:
	 *
	 *	(a) We already know dumpdv in the RB_ASKNAME case.
	 *
	 *	(b) If dumpspec is set, try to use it.  If the device
	 *	    is not available, punt.
	 *
	 *	(c) If dumpspec is not set, the dump device is
	 *	    wildcarded or unspecified.  If the root device
	 *	    is DV_IFNET, punt.  Otherwise, use partition b
	 *	    of the root device.
	 */

	if (boothowto & RB_ASKNAME) {		/* (a) */
		if (dumpdv == NULL)
			goto nodumpdev;
	} else if (dumpspec != NULL) {		/* (b) */
		if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
			/*
			 * Operator doesn't want a dump device.
			 * Or looks like they tried to pick a network
			 * device.  Oops.
			 */
			goto nodumpdev;
		}

		dumpdevname = devsw_blk2name(major(dumpdev));
		if (dumpdevname == NULL)
			goto nodumpdev;
		memset(buf, 0, sizeof(buf));
		snprintf(buf, sizeof(buf), "%s%llu", dumpdevname,
		    (unsigned long long)DISKUNIT(dumpdev));

		dumpdv = finddevice(buf);
		if (dumpdv == NULL) {
			/*
			 * Device not configured.
			 */
			goto nodumpdev;
		}
	} else {				/* (c) */
		if (DEV_USES_PARTITIONS(rootdv) == 0) {
			for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
			     dv != NULL;
			     dv = deviter_next(&di))
				if (isswap(dv))
					break;
			deviter_release(&di);
			if (dv == NULL)
				goto nodumpdev;

			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
			if (majdev < 0)
				goto nodumpdev;
			dumpdv = dv;
			dumpdev = makedev(majdev, device_unit(dumpdv));
		} else {
			dumpdv = rootdv;
			dumpdev = MAKEDISKDEV(major(rootdev),
			    device_unit(dumpdv), 1);
		}
	}

	dumpcdev = devsw_blk2chr(dumpdev);
	aprint_normal(" dumps on %s", device_xname(dumpdv));
	if (DEV_USES_PARTITIONS(dumpdv))
		aprint_normal("%c", (int)DISKPART(dumpdev) + 'a');
	aprint_normal("\n");
	return;

 nodumpdev:
	dumpdev = NODEV;
	dumpcdev = NODEV;
	aprint_normal("\n");
}

static device_t
finddevice(const char *name)
{
	const char *wname;

	if ((wname = getwedgename(name, strlen(name))) != NULL)
		return dkwedge_find_by_wname(wname);

	return device_find_by_xname(name);
}

static device_t
getdisk(char *str, int len, int defpart, dev_t *devp, int isdump)
{
	device_t dv;
	deviter_t di;

	if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
		printf("use one of:");
		for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
		     dv = deviter_next(&di)) {
			if (DEV_USES_PARTITIONS(dv))
				printf(" %s[a-%c]", device_xname(dv),
				    'a' + MAXPARTITIONS - 1);
			else if (device_class(dv) == DV_DISK)
				printf(" %s", device_xname(dv));
			if (isdump == 0 && device_class(dv) == DV_IFNET)
				printf(" %s", device_xname(dv));
		}
		deviter_release(&di);
		dkwedge_print_wnames();
		if (isdump)
			printf(" none");
#if defined(DDB)
		printf(" ddb");
#endif
		printf(" halt reboot\n");
	}
	return dv;
}

static const char *
getwedgename(const char *name, int namelen)
{
	const char *wpfx1 = "wedge:";
	const char *wpfx2 = "NAME=";
	const int wpfx1len = strlen(wpfx1);
	const int wpfx2len = strlen(wpfx2);

	if (namelen > wpfx1len && strncmp(name, wpfx1, wpfx1len) == 0)
		return name + wpfx1len;

	if (namelen > wpfx2len && strncasecmp(name, wpfx2, wpfx2len) == 0)
		return name + wpfx2len;

	return NULL;
}

static device_t
parsedisk(char *str, int len, int defpart, dev_t *devp)
{
	device_t dv;
	const char *wname;
	char *cp, c;
	int majdev, part;
	if (len == 0)
		return (NULL);

	if (len == 4 && strcmp(str, "halt") == 0)
		cpu_reboot(RB_HALT, NULL);
	else if (len == 6 && strcmp(str, "reboot") == 0)
		cpu_reboot(0, NULL);
#if defined(DDB)
	else if (len == 3 && strcmp(str, "ddb") == 0)
		console_debugger();
#endif

	cp = str + len - 1;
	c = *cp;

	if ((wname = getwedgename(str, len)) != NULL) {
		if ((dv = dkwedge_find_by_wname(wname)) == NULL)
			return NULL;
		part = defpart;
		goto gotdisk;
	} else if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
		part = c - 'a';
		*cp = '\0';
	} else
		part = defpart;

	dv = finddevice(str);
	if (dv != NULL) {
		if (device_class(dv) == DV_DISK) {
 gotdisk:
			majdev = devsw_name2blk(device_xname(dv), NULL, 0);
			if (majdev < 0)
				panic("parsedisk");
			if (DEV_USES_PARTITIONS(dv))
				*devp = MAKEDISKDEV(majdev, device_unit(dv),
						    part);
			else
				*devp = makedev(majdev, device_unit(dv));
		}

		if (device_class(dv) == DV_IFNET)
			*devp = NODEV;
	}

	*cp = c;
	return (dv);
}