Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.36
      1 /*	$NetBSD: kern_subr.c,v 1.36 1998/02/18 07:11:46 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 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.
     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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  * (c) UNIX System Laboratories, Inc.
     44  * All or some portions of this file are derived from material licensed
     45  * to the University of California by American Telephone and Telegraph
     46  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     47  * the permission of UNIX System Laboratories, Inc.
     48  *
     49  * Copyright (c) 1992, 1993
     50  *	The Regents of the University of California.  All rights reserved.
     51  *
     52  * This software was developed by the Computer Systems Engineering group
     53  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     54  * contributed to Berkeley.
     55  *
     56  * All advertising materials mentioning features or use of this software
     57  * must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Lawrence Berkeley Laboratory.
     60  *
     61  * Redistribution and use in source and binary forms, with or without
     62  * modification, are permitted provided that the following conditions
     63  * are met:
     64  * 1. Redistributions of source code must retain the above copyright
     65  *    notice, this list of conditions and the following disclaimer.
     66  * 2. Redistributions in binary form must reproduce the above copyright
     67  *    notice, this list of conditions and the following disclaimer in the
     68  *    documentation and/or other materials provided with the distribution.
     69  * 3. All advertising materials mentioning features or use of this software
     70  *    must display the following acknowledgement:
     71  *	This product includes software developed by the University of
     72  *	California, Berkeley and its contributors.
     73  * 4. Neither the name of the University nor the names of its contributors
     74  *    may be used to endorse or promote products derived from this software
     75  *    without specific prior written permission.
     76  *
     77  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     78  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     79  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     80  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     81  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     82  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     83  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     84  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     85  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     86  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     87  * SUCH DAMAGE.
     88  *
     89  *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
     90  */
     91 
     92 #include "opt_uvm.h"
     93 
     94 #include <sys/param.h>
     95 #include <sys/systm.h>
     96 #include <sys/proc.h>
     97 #include <sys/malloc.h>
     98 #include <sys/mount.h>
     99 #include <sys/device.h>
    100 #include <sys/reboot.h>
    101 #include <sys/conf.h>
    102 #include <sys/disklabel.h>
    103 #include <sys/queue.h>
    104 
    105 #include <dev/cons.h>
    106 
    107 #include <net/if.h>
    108 
    109 /* XXX these should eventually move to subr_autoconf.c */
    110 static int findblkmajor __P((const char *, struct devnametobdevmaj *));
    111 static const char *findblkname __P((int, struct devnametobdevmaj *));
    112 static struct device *getdisk __P((char *, int, int,
    113 	struct devnametobdevmaj *, dev_t *, int));
    114 static struct device *parsedisk __P((char *, int, int,
    115 	struct devnametobdevmaj *, dev_t *));
    116 static int getstr __P((char *, int));
    117 
    118 int
    119 uiomove(buf, n, uio)
    120 	register void *buf;
    121 	register int n;
    122 	register struct uio *uio;
    123 {
    124 	register struct iovec *iov;
    125 	u_int cnt;
    126 	int error = 0;
    127 	char *cp = buf;
    128 
    129 #ifdef DIAGNOSTIC
    130 	if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
    131 		panic("uiomove: mode");
    132 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    133 		panic("uiomove proc");
    134 #endif
    135 	while (n > 0 && uio->uio_resid) {
    136 		iov = uio->uio_iov;
    137 		cnt = iov->iov_len;
    138 		if (cnt == 0) {
    139 			uio->uio_iov++;
    140 			uio->uio_iovcnt--;
    141 			continue;
    142 		}
    143 		if (cnt > n)
    144 			cnt = n;
    145 		switch (uio->uio_segflg) {
    146 
    147 		case UIO_USERSPACE:
    148 			if (uio->uio_rw == UIO_READ)
    149 				error = copyout(cp, iov->iov_base, cnt);
    150 			else
    151 				error = copyin(iov->iov_base, cp, cnt);
    152 			if (error)
    153 				return (error);
    154 			break;
    155 
    156 		case UIO_SYSSPACE:
    157 #if defined(UVM)
    158 			if (uio->uio_rw == UIO_READ)
    159 				error = kcopy(cp, iov->iov_base, cnt);
    160 			else
    161 				error = kcopy(iov->iov_base, cp, cnt);
    162 			if (error)
    163 				return(error);
    164 #else
    165 			if (uio->uio_rw == UIO_READ)
    166 				bcopy(cp, iov->iov_base, cnt);
    167 			else
    168 				bcopy(iov->iov_base, cp, cnt);
    169 #endif
    170 			break;
    171 		}
    172 		iov->iov_base += cnt;
    173 		iov->iov_len -= cnt;
    174 		uio->uio_resid -= cnt;
    175 		uio->uio_offset += cnt;
    176 		cp += cnt;
    177 		n -= cnt;
    178 	}
    179 	return (error);
    180 }
    181 
    182 /*
    183  * Give next character to user as result of read.
    184  */
    185 int
    186 ureadc(c, uio)
    187 	register int c;
    188 	register struct uio *uio;
    189 {
    190 	register struct iovec *iov;
    191 
    192 	if (uio->uio_resid <= 0)
    193 		panic("ureadc: non-positive resid");
    194 again:
    195 	if (uio->uio_iovcnt <= 0)
    196 		panic("ureadc: non-positive iovcnt");
    197 	iov = uio->uio_iov;
    198 	if (iov->iov_len <= 0) {
    199 		uio->uio_iovcnt--;
    200 		uio->uio_iov++;
    201 		goto again;
    202 	}
    203 	switch (uio->uio_segflg) {
    204 
    205 	case UIO_USERSPACE:
    206 		if (subyte(iov->iov_base, c) < 0)
    207 			return (EFAULT);
    208 		break;
    209 
    210 	case UIO_SYSSPACE:
    211 		*iov->iov_base = c;
    212 		break;
    213 	}
    214 	iov->iov_base++;
    215 	iov->iov_len--;
    216 	uio->uio_resid--;
    217 	uio->uio_offset++;
    218 	return (0);
    219 }
    220 
    221 /*
    222  * General routine to allocate a hash table.
    223  */
    224 void *
    225 hashinit(elements, type, flags, hashmask)
    226 	int elements, type, flags;
    227 	u_long *hashmask;
    228 {
    229 	long hashsize;
    230 	LIST_HEAD(generic, generic) *hashtbl;
    231 	int i;
    232 
    233 	if (elements <= 0)
    234 		panic("hashinit: bad cnt");
    235 	for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
    236 		continue;
    237 	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, flags);
    238 	for (i = 0; i < hashsize; i++)
    239 		LIST_INIT(&hashtbl[i]);
    240 	*hashmask = hashsize - 1;
    241 	return (hashtbl);
    242 }
    243 
    244 /*
    245  * "Shutdown hook" types, functions, and variables.
    246  */
    247 
    248 struct shutdownhook_desc {
    249 	LIST_ENTRY(shutdownhook_desc) sfd_list;
    250 	void	(*sfd_fn) __P((void *));
    251 	void	*sfd_arg;
    252 };
    253 
    254 LIST_HEAD(, shutdownhook_desc) shutdownhook_list;
    255 
    256 void *
    257 shutdownhook_establish(fn, arg)
    258 	void (*fn) __P((void *));
    259 	void *arg;
    260 {
    261 	struct shutdownhook_desc *ndp;
    262 
    263 	ndp = (struct shutdownhook_desc *)
    264 	    malloc(sizeof (*ndp), M_DEVBUF, M_NOWAIT);
    265 	if (ndp == NULL)
    266 		return NULL;
    267 
    268 	ndp->sfd_fn = fn;
    269 	ndp->sfd_arg = arg;
    270 	LIST_INSERT_HEAD(&shutdownhook_list, ndp, sfd_list);
    271 
    272 	return (ndp);
    273 }
    274 
    275 void
    276 shutdownhook_disestablish(vhook)
    277 	void *vhook;
    278 {
    279 #ifdef DIAGNOSTIC
    280 	struct shutdownhook_desc *dp;
    281 
    282 	for (dp = shutdownhook_list.lh_first; dp != NULL;
    283 	    dp = dp->sfd_list.le_next)
    284                 if (dp == vhook)
    285 			break;
    286 	if (dp == NULL)
    287 		panic("shutdownhook_disestablish: hook not established");
    288 #endif
    289 
    290 	LIST_REMOVE((struct shutdownhook_desc *)vhook, sfd_list);
    291 	free(vhook, M_DEVBUF);
    292 }
    293 
    294 /*
    295  * Run shutdown hooks.  Should be invoked immediately before the
    296  * system is halted or rebooted, i.e. after file systems unmounted,
    297  * after crash dump done, etc.
    298  *
    299  * Each shutdown hook is removed from the list before it's run, so that
    300  * it won't be run again.
    301  */
    302 void
    303 doshutdownhooks()
    304 {
    305 	struct shutdownhook_desc *dp;
    306 
    307 	while ((dp = shutdownhook_list.lh_first) != NULL) {
    308 		LIST_REMOVE(dp, sfd_list);
    309 		(*dp->sfd_fn)(dp->sfd_arg);
    310 #if 0
    311 		/*
    312 		 * Don't bother freeing the hook structure,, since we may
    313 		 * be rebooting because of a memory corruption problem,
    314 		 * and this might only make things worse.  It doesn't
    315 		 * matter, anyway, since the system is just about to
    316 		 * reboot.
    317 		 */
    318 		free(dp, M_DEVBUF);
    319 #endif
    320 	}
    321 }
    322 
    323 /*
    324  * "Mountroot hook" types, functions, and variables.
    325  */
    326 
    327 struct mountroothook_desc {
    328 	LIST_ENTRY(mountroothook_desc) mrd_list;
    329 	struct	device *mrd_device;
    330 	void 	(*mrd_func) __P((struct device *));
    331 };
    332 
    333 LIST_HEAD(, mountroothook_desc) mountroothook_list;
    334 
    335 void *
    336 mountroothook_establish(func, dev)
    337 	void (*func) __P((struct device *));
    338 	struct device *dev;
    339 {
    340 	struct mountroothook_desc *mrd;
    341 
    342 	mrd = (struct mountroothook_desc *)
    343 	    malloc(sizeof(*mrd), M_DEVBUF, M_NOWAIT);
    344 	if (mrd == NULL)
    345 		return (NULL);
    346 
    347 	mrd->mrd_device = dev;
    348 	mrd->mrd_func = func;
    349 	LIST_INSERT_HEAD(&mountroothook_list, mrd, mrd_list);
    350 
    351 	return (mrd);
    352 }
    353 
    354 void
    355 mountroothook_disestablish(vhook)
    356 	void *vhook;
    357 {
    358 #ifdef DIAGNOSTIC
    359 	struct mountroothook_desc *mrd;
    360 
    361 	for (mrd = mountroothook_list.lh_first; mrd != NULL;
    362 	    mrd = mrd->mrd_list.le_next)
    363                 if (mrd == vhook)
    364 			break;
    365 	if (mrd == NULL)
    366 		panic("mountroothook_disestablish: hook not established");
    367 #endif
    368 
    369 	LIST_REMOVE((struct mountroothook_desc *)vhook, mrd_list);
    370 	free(vhook, M_DEVBUF);
    371 }
    372 
    373 void
    374 mountroothook_destroy()
    375 {
    376 	struct mountroothook_desc *mrd;
    377 
    378 	while ((mrd = mountroothook_list.lh_first) != NULL) {
    379 		LIST_REMOVE(mrd, mrd_list);
    380 		free(mrd, M_DEVBUF);
    381 	}
    382 }
    383 
    384 void
    385 domountroothook()
    386 {
    387 	struct mountroothook_desc *mrd;
    388 
    389 	for (mrd = mountroothook_list.lh_first; mrd != NULL;
    390 	    mrd = mrd->mrd_list.le_next) {
    391 		if (mrd->mrd_device == root_device) {
    392 			(*mrd->mrd_func)(root_device);
    393 			return;
    394 		}
    395 	}
    396 }
    397 
    398 /*
    399  * Determine the root device and, if instructed to, the root file system.
    400  */
    401 
    402 #include "md.h"
    403 #if NMD == 0
    404 #undef MEMORY_DISK_HOOKS
    405 #endif
    406 
    407 #ifdef MEMORY_DISK_HOOKS
    408 static struct device fakemdrootdev[NMD];
    409 #endif
    410 
    411 void
    412 setroot(bootdv, bootpartition, nam2blk)
    413 	struct device *bootdv;
    414 	int bootpartition;
    415 	struct devnametobdevmaj *nam2blk;
    416 {
    417 	struct device *dv;
    418 	int len, print_newline = 0;
    419 	dev_t nrootdev;
    420 	dev_t ndumpdev = NODEV;
    421 	char buf[128];
    422 	const char *rootdevname;
    423 	const char *dumpdevname;
    424 	struct device *rootdv = NULL;		/* XXX gcc -Wuninitialized */
    425 	struct device *dumpdv = NULL;
    426 	struct ifnet *ifp;
    427 	const char *deffsname;
    428 	struct vfsops *vops;
    429 	extern int (*mountroot) __P((void));
    430 	static struct devnametobdevmaj *last_nam2blk;
    431 
    432 	if (nam2blk == NULL) {
    433 		if (last_nam2blk == NULL)
    434 			panic("setroot: no name to bdev major map");
    435 		nam2blk = last_nam2blk;
    436 	}
    437 	last_nam2blk = nam2blk;
    438 
    439 #ifdef MEMORY_DISK_HOOKS
    440 	for (i = 0; i < NMD; i++) {
    441 		fakemdrootdev[i].dv_class  = DV_DISK;
    442 		fakemdrootdev[i].dv_cfdata = NULL;
    443 		fakemdrootdev[i].dv_unit   = i;
    444 		fakemdrootdev[i].dv_parent = NULL;
    445 		sprintf(fakemdrootdev[i].dv_xname, "md%d", i);
    446 	}
    447 #endif /* MEMORY_DISK_HOOKS */
    448 
    449 #ifdef MEMORY_DISK_IS_ROOT
    450 	bootdv = &fakemdrootdev[0];
    451 	bootpartition = 0;
    452 #endif
    453 
    454 	/*
    455 	 * If NFS is specified as the file system, and we found
    456 	 * a DV_DISK boot device (or no boot device at all), then
    457 	 * find a reasonable network interface for "rootspec".
    458 	 */
    459 	vops = vfs_getopsbyname("nfs");
    460 	if (vops != NULL && vops->vfs_mountroot == mountroot &&
    461 	    rootspec == NULL &&
    462 	    (bootdv == NULL || bootdv->dv_class != DV_IFNET)) {
    463 		for (ifp = ifnet.tqh_first; ifp != NULL;
    464 		    ifp = ifp->if_list.tqe_next)
    465 			if ((ifp->if_flags &
    466 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    467 				break;
    468 		if (ifp == NULL) {
    469 			/*
    470 			 * Can't find a suitable interface; ask the
    471 			 * user.
    472 			 */
    473 			boothowto |= RB_ASKNAME;
    474 		} else {
    475 			/*
    476 			 * Have a suitable interface; behave as if
    477 			 * the user specified this interface.
    478 			 */
    479 			rootspec = (const char *)ifp->if_xname;
    480 		}
    481 	}
    482 
    483 	/*
    484 	 * If wildcarded root and we the boot device wasn't determined,
    485 	 * ask the user.
    486 	 */
    487 	if (rootspec == NULL && bootdv == NULL)
    488 		boothowto |= RB_ASKNAME;
    489 
    490  top:
    491 	if (boothowto & RB_ASKNAME) {
    492 		struct device *defdumpdv;
    493 
    494 		for (;;) {
    495 			printf("root device");
    496 			if (bootdv != NULL) {
    497 				printf(" (default %s", bootdv->dv_xname);
    498 				if (bootdv->dv_class == DV_DISK)
    499 					printf("%c", bootpartition + 'a');
    500 				printf(")");
    501 			}
    502 			printf(": ");
    503 			len = getstr(buf, sizeof(buf));
    504 			if (len == 0 && bootdv != NULL) {
    505 				strcpy(buf, bootdv->dv_xname);
    506 				len = strlen(buf);
    507 			}
    508 			if (len > 0 && buf[len - 1] == '*') {
    509 				buf[--len] = '\0';
    510 				dv = getdisk(buf, len, 1, nam2blk,
    511 				    &nrootdev, 0);
    512 				if (dv != NULL) {
    513 					rootdv = dv;
    514 					break;
    515 				}
    516 			}
    517 			dv = getdisk(buf, len, bootpartition, nam2blk,
    518 			    &nrootdev, 0);
    519 			if (dv != NULL) {
    520 				rootdv = dv;
    521 				break;
    522 			}
    523 		}
    524 
    525 		/*
    526 		 * Set up the default dump device.  If root is on
    527 		 * a network device, there is no default dump
    528 		 * device, since we don't support dumps to the
    529 		 * network.
    530 		 */
    531 		if (rootdv->dv_class == DV_IFNET)
    532 			defdumpdv = NULL;
    533 		else
    534 			defdumpdv = rootdv;
    535 
    536 		for (;;) {
    537 			printf("dump device");
    538 			if (defdumpdv != NULL) {
    539 				/*
    540 				 * Note, we know it's a disk if we get here.
    541 				 */
    542 				printf(" (default %sb)", defdumpdv->dv_xname);
    543 			}
    544 			printf(": ");
    545 			len = getstr(buf, sizeof(buf));
    546 			if (len == 0) {
    547 				if (defdumpdv != NULL) {
    548 					ndumpdev = MAKEDISKDEV(major(nrootdev),
    549 					    DISKUNIT(nrootdev), 1);
    550 				}
    551 				if (rootdv->dv_class == DV_IFNET)
    552 					dumpdv = NULL;
    553 				else
    554 					dumpdv = rootdv;
    555 				break;
    556 			}
    557 			if (len == 4 && strcmp(buf, "none") == 0) {
    558 				dumpspec = "none";
    559 				goto havedump;
    560 			}
    561 			dv = getdisk(buf, len, 1, nam2blk, &ndumpdev, 1);
    562 			if (dv) {
    563 				dumpdv = dv;
    564 				break;
    565 			}
    566 		}
    567 
    568  havedump:
    569 		rootdev = nrootdev;
    570 		dumpdev = ndumpdev;
    571 
    572 		for (vops = LIST_FIRST(&vfs_list); vops != NULL;
    573 		     vops = LIST_NEXT(vops, vfs_list)) {
    574 			if (vops->vfs_mountroot != NULL &&
    575 			    vops->vfs_mountroot == mountroot)
    576 				break;
    577 		}
    578 
    579 		if (vops == NULL) {
    580 			mountroot = NULL;
    581 			deffsname = "generic";
    582 		} else
    583 			deffsname = vops->vfs_name;
    584 
    585 		for (;;) {
    586 			printf("file system (default %s): ", deffsname);
    587 			len = getstr(buf, sizeof(buf));
    588 			if (len == 0)
    589 				break;
    590 			if (len == 4 && strcmp(buf, "halt") == 0)
    591 				cpu_reboot(RB_HALT, NULL);
    592 			else if (len == 7 && strcmp(buf, "generic") == 0) {
    593 				mountroot = NULL;
    594 				break;
    595 			}
    596 			vops = vfs_getopsbyname(buf);
    597 			if (vops == NULL || vops->vfs_mountroot == NULL) {
    598 				printf("use one of: generic");
    599 				for (vops = LIST_FIRST(&vfs_list);
    600 				     vops != NULL;
    601 				     vops = LIST_NEXT(vops, vfs_list)) {
    602 					if (vops->vfs_mountroot != NULL)
    603 						printf(" %s", vops->vfs_name);
    604 				}
    605 				printf(" halt\n");
    606 			} else {
    607 				mountroot = vops->vfs_mountroot;
    608 				break;
    609 			}
    610 		}
    611 
    612 	} else if (rootspec == NULL) {
    613 		int majdev;
    614 
    615 		/*
    616 		 * Wildcarded root; use the boot device.
    617 		 */
    618 		rootdv = bootdv;
    619 
    620 		majdev = findblkmajor(bootdv->dv_xname, nam2blk);
    621 		if (majdev >= 0) {
    622 			/*
    623 			 * Root is on a disk.  `bootpartition' is root.
    624 			 */
    625 			rootdev = MAKEDISKDEV(majdev, bootdv->dv_unit,
    626 			    bootpartition);
    627 		}
    628 	} else {
    629 
    630 		/*
    631 		 * `root on <dev> ...'
    632 		 */
    633 
    634 		/*
    635 		 * If it's a network interface, we can bail out
    636 		 * early.
    637 		 */
    638 		for (dv = alldevs.tqh_first; dv != NULL;
    639 		    dv = dv->dv_list.tqe_next)
    640 			if (strcmp(dv->dv_xname, rootspec) == 0)
    641 				break;
    642 		if (dv != NULL && dv->dv_class == DV_IFNET) {
    643 			rootdv = dv;
    644 			goto haveroot;
    645 		}
    646 
    647 		rootdevname = findblkname(major(rootdev), nam2blk);
    648 		if (rootdevname == NULL) {
    649 			printf("unknown device major 0x%x\n", rootdev);
    650 			boothowto |= RB_ASKNAME;
    651 			goto top;
    652 		}
    653 		bzero(buf, sizeof(buf));
    654 		sprintf(buf, "%s%d", rootdevname, DISKUNIT(rootdev));
    655 
    656 		for (dv = alldevs.tqh_first; dv != NULL;
    657 		    dv = dv->dv_list.tqe_next) {
    658 			if (strcmp(buf, dv->dv_xname) == 0) {
    659 				rootdv = dv;
    660 				break;
    661 			}
    662 		}
    663 		if (rootdv == NULL) {
    664 			printf("device %s (0x%x) not configured\n",
    665 			    buf, rootdev);
    666 			boothowto |= RB_ASKNAME;
    667 			goto top;
    668 		}
    669 	}
    670 
    671  haveroot:
    672 
    673 	root_device = rootdv;
    674 
    675 	switch (rootdv->dv_class) {
    676 	case DV_IFNET:
    677 		/* Nothing. */
    678 		break;
    679 
    680 	case DV_DISK:
    681 		printf("root on %s%c", rootdv->dv_xname,
    682 		    DISKPART(rootdev) + 'a');
    683 		print_newline = 1;
    684 		break;
    685 
    686 	default:
    687 		printf("can't determine root device\n");
    688 		boothowto |= RB_ASKNAME;
    689 		goto top;
    690 	}
    691 
    692 	/*
    693 	 * Now configure the dump device.
    694 	 */
    695 
    696 	if (dumpspec != NULL && strcmp(dumpspec, "none") == 0) {
    697 		/*
    698 		 * Operator doesn't want a dump device.
    699 		 */
    700 		goto nodumpdev;
    701 	}
    702 
    703 	/*
    704 	 * If we haven't figured out the dump device, do so, with
    705 	 * the following rules:
    706 	 *
    707 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
    708 	 *
    709 	 *	(b) If dumpspec is set, try to use it.  If the device
    710 	 *	    is not available, punt.
    711 	 *
    712 	 *	(c) If dumpspec is not set, the dump device is
    713 	 *	    wildcarded or unspecified.  If the root device
    714 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
    715 	 *	    of the root device.
    716 	 */
    717 
    718 	if (boothowto & RB_ASKNAME) {
    719 		if (dumpdv == NULL) {
    720 			/*
    721 			 * Just return; dumpdev is already set to NODEV
    722 			 * and we don't want to print a newline in this
    723 			 * case.
    724 			 */
    725 			return;
    726 		}
    727 		goto out;
    728 	}
    729 
    730 	if (dumpspec != NULL) {
    731 		if (dumpdev == NODEV) {
    732 			/*
    733 			 * Looks like they tried to pick a network
    734 			 * device.  Oops.
    735 			 */
    736 			goto nodumpdev;
    737 		}
    738 
    739 		dumpdevname = findblkname(major(dumpdev), nam2blk);
    740 		if (dumpdevname == NULL)
    741 			goto nodumpdev;
    742 		bzero(buf, sizeof(buf));
    743 		sprintf(buf, "%s%d", dumpdevname, DISKUNIT(dumpdev));
    744 
    745 		for (dv = alldevs.tqh_first; dv != NULL;
    746 		    dv = dv->dv_list.tqe_next) {
    747 			if (strcmp(buf, dv->dv_xname) == 0) {
    748 				dumpdv = dv;
    749 				break;
    750 			}
    751 		}
    752 		if (dv == NULL) {
    753 			/*
    754 			 * Device not configured.
    755 			 */
    756 			goto nodumpdev;
    757 		}
    758 	} else if (rootdv->dv_class == DV_IFNET)
    759 		goto nodumpdev;
    760 	else {
    761 		dumpdv = rootdv;
    762 		dumpdev = MAKEDISKDEV(major(rootdev), dumpdv->dv_unit, 1);
    763 	}
    764 
    765  out:
    766 	printf(" dumps on %s%c\n", dumpdv->dv_xname, DISKPART(dumpdev) + 'a');
    767 	return;
    768 
    769  nodumpdev:
    770 	dumpdev = NODEV;
    771 	if (print_newline)
    772 		printf("\n");
    773 }
    774 
    775 static int
    776 findblkmajor(name, nam2blk)
    777 	const char *name;
    778 	struct devnametobdevmaj *nam2blk;
    779 {
    780 	int i;
    781 
    782 	if (nam2blk == NULL)
    783 		return (-1);
    784 
    785 	for (i = 0; nam2blk[i].d_name != NULL; i++)
    786 		if (strncmp(name, nam2blk[i].d_name,
    787 		    strlen(nam2blk[i].d_name)) == 0)
    788 			return (nam2blk[i].d_maj);
    789 	return (-1);
    790 }
    791 
    792 const char *
    793 findblkname(maj, nam2blk)
    794 	int maj;
    795 	struct devnametobdevmaj *nam2blk;
    796 {
    797 	int i;
    798 
    799 	if (nam2blk == NULL)
    800 		return (NULL);
    801 
    802 	for (i = 0; nam2blk[i].d_name != NULL; i++)
    803 		if (nam2blk[i].d_maj == maj)
    804 			return (nam2blk[i].d_name);
    805 	return (NULL);
    806 }
    807 
    808 static struct device *
    809 getdisk(str, len, defpart, nam2blk, devp, isdump)
    810 	char *str;
    811 	int len, defpart;
    812 	struct devnametobdevmaj *nam2blk;
    813 	dev_t *devp;
    814 	int isdump;
    815 {
    816 	struct device	*dv;
    817 #ifdef MEMORY_DISK_HOOKS
    818 	int		i;
    819 #endif
    820 
    821 	if ((dv = parsedisk(str, len, defpart, nam2blk, devp)) == NULL) {
    822 		printf("use one of:");
    823 #ifdef MEMORY_DISK_HOOKS
    824 		if (isdump == 0)
    825 			for (i = 0; i < NMD; i++)
    826 				printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
    827 				    'a' + MAXPARTITIONS - 1);
    828 #endif
    829 		for (dv = alldevs.tqh_first; dv != NULL;
    830 		    dv = dv->dv_list.tqe_next) {
    831 			if (dv->dv_class == DV_DISK)
    832 				printf(" %s[a-%c]", dv->dv_xname,
    833 				    'a' + MAXPARTITIONS - 1);
    834 			if (isdump == 0 && dv->dv_class == DV_IFNET)
    835 				printf(" %s", dv->dv_xname);
    836 		}
    837 		if (isdump)
    838 			printf(" none");
    839 		printf(" halt\n");
    840 	}
    841 	return (dv);
    842 }
    843 
    844 static struct device *
    845 parsedisk(str, len, defpart, nam2blk, devp)
    846 	char *str;
    847 	int len, defpart;
    848 	struct devnametobdevmaj *nam2blk;
    849 	dev_t *devp;
    850 {
    851 	struct device *dv;
    852 	char *cp, c;
    853 	int majdev, part;
    854 #ifdef MEMORY_DISK_HOOKS
    855 	int i;
    856 #endif
    857 
    858 	if (len == 0)
    859 		return (NULL);
    860 
    861 	if (len == 4 && strcmp(str, "halt") == 0)
    862 		cpu_reboot(RB_HALT, NULL);
    863 
    864 	cp = str + len - 1;
    865 	c = *cp;
    866 	if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
    867 		part = c - 'a';
    868 		*cp = '\0';
    869 	} else
    870 		part = defpart;
    871 
    872 #ifdef MEMORY_DISK_HOOKS
    873 	for (i = 0; i < NMD; i++)
    874 		if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) {
    875 			dv = &fakemdrootdev[i];
    876 			goto gotdisk;
    877 		}
    878 #endif
    879 
    880 	for (dv = alldevs.tqh_first; dv != NULL; dv = dv->dv_list.tqe_next) {
    881 		if (dv->dv_class == DV_DISK &&
    882 		    strcmp(str, dv->dv_xname) == 0) {
    883 #ifdef MEMORY_DISK_HOOKS
    884  gotdisk:
    885 #endif
    886 			majdev = findblkmajor(dv->dv_xname, nam2blk);
    887 			if (majdev < 0)
    888 				panic("parsedisk");
    889 			*devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
    890 			break;
    891 		}
    892 
    893 		if (dv->dv_class == DV_IFNET &&
    894 		    strcmp(str, dv->dv_xname) == 0) {
    895 			*devp = NODEV;
    896 			break;
    897 		}
    898 	}
    899 
    900 	*cp = c;
    901 	return (dv);
    902 }
    903 
    904 /*
    905  * XXX shouldn't this be a common function?
    906  */
    907 static int
    908 getstr(cp, size)
    909 	char *cp;
    910 	int size;
    911 {
    912 	char *lp;
    913 	int c, len;
    914 
    915 	cnpollc(1);
    916 
    917 	lp = cp;
    918 	len = 0;
    919 	for (;;) {
    920 		c = cngetc();
    921 		switch (c) {
    922 		case '\n':
    923 		case '\r':
    924 			printf("\n");
    925 			*lp++ = '\0';
    926 			cnpollc(0);
    927 			return (len);
    928 		case '\b':
    929 		case '\177':
    930 		case '#':
    931 			if (len) {
    932 				--len;
    933 				--lp;
    934 				printf("\b \b");
    935 			}
    936 			continue;
    937 		case '@':
    938 		case 'u'&037:
    939 			len = 0;
    940 			lp = cp;
    941 			printf("\n");
    942 			continue;
    943 		default:
    944 			if (len + 1 >= size || c < ' ') {
    945 				printf("\007");
    946 				continue;
    947 			}
    948 			printf("%c", c);
    949 			++len;
    950 			*lp++ = c;
    951 		}
    952 	}
    953 }
    954