Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.42
      1 /*	$NetBSD: kern_subr.c,v 1.42 1999/01/22 07:57:13 chs 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.4 (Berkeley) 2/14/95
     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 				memcpy(iov->iov_base, cp, cnt);
    167 			else
    168 				memcpy(cp, iov->iov_base, cnt);
    169 #endif
    170 			break;
    171 		}
    172 		(caddr_t)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 		*(char *)iov->iov_base = c;
    212 		break;
    213 	}
    214 	((caddr_t)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  * Allocate enough memory to hold at least `elements' list-head pointers.
    224  * Return a pointer to the allocated space and set *hashmask to a pattern
    225  * suitable for masking a value to use as an index into the returned array.
    226  */
    227 void *
    228 hashinit(elements, type, flags, hashmask)
    229 	int elements, type, flags;
    230 	u_long *hashmask;
    231 {
    232 	long hashsize;
    233 	LIST_HEAD(generic, generic) *hashtbl;
    234 	int i;
    235 
    236 	if (elements <= 0)
    237 		panic("hashinit: bad cnt");
    238 	for (hashsize = 1; hashsize < elements; hashsize <<= 1)
    239 		continue;
    240 	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, flags);
    241 	for (i = 0; i < hashsize; i++)
    242 		LIST_INIT(&hashtbl[i]);
    243 	*hashmask = hashsize - 1;
    244 	return (hashtbl);
    245 }
    246 
    247 /*
    248  * "Shutdown hook" types, functions, and variables.
    249  */
    250 
    251 struct shutdownhook_desc {
    252 	LIST_ENTRY(shutdownhook_desc) sfd_list;
    253 	void	(*sfd_fn) __P((void *));
    254 	void	*sfd_arg;
    255 };
    256 
    257 LIST_HEAD(, shutdownhook_desc) shutdownhook_list;
    258 
    259 void *
    260 shutdownhook_establish(fn, arg)
    261 	void (*fn) __P((void *));
    262 	void *arg;
    263 {
    264 	struct shutdownhook_desc *ndp;
    265 
    266 	ndp = (struct shutdownhook_desc *)
    267 	    malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
    268 	if (ndp == NULL)
    269 		return NULL;
    270 
    271 	ndp->sfd_fn = fn;
    272 	ndp->sfd_arg = arg;
    273 	LIST_INSERT_HEAD(&shutdownhook_list, ndp, sfd_list);
    274 
    275 	return (ndp);
    276 }
    277 
    278 void
    279 shutdownhook_disestablish(vhook)
    280 	void *vhook;
    281 {
    282 #ifdef DIAGNOSTIC
    283 	struct shutdownhook_desc *dp;
    284 
    285 	for (dp = shutdownhook_list.lh_first; dp != NULL;
    286 	    dp = dp->sfd_list.le_next)
    287                 if (dp == vhook)
    288 			break;
    289 	if (dp == NULL)
    290 		panic("shutdownhook_disestablish: hook not established");
    291 #endif
    292 
    293 	LIST_REMOVE((struct shutdownhook_desc *)vhook, sfd_list);
    294 	free(vhook, M_DEVBUF);
    295 }
    296 
    297 /*
    298  * Run shutdown hooks.  Should be invoked immediately before the
    299  * system is halted or rebooted, i.e. after file systems unmounted,
    300  * after crash dump done, etc.
    301  *
    302  * Each shutdown hook is removed from the list before it's run, so that
    303  * it won't be run again.
    304  */
    305 void
    306 doshutdownhooks()
    307 {
    308 	struct shutdownhook_desc *dp;
    309 
    310 	while ((dp = shutdownhook_list.lh_first) != NULL) {
    311 		LIST_REMOVE(dp, sfd_list);
    312 		(*dp->sfd_fn)(dp->sfd_arg);
    313 #if 0
    314 		/*
    315 		 * Don't bother freeing the hook structure,, since we may
    316 		 * be rebooting because of a memory corruption problem,
    317 		 * and this might only make things worse.  It doesn't
    318 		 * matter, anyway, since the system is just about to
    319 		 * reboot.
    320 		 */
    321 		free(dp, M_DEVBUF);
    322 #endif
    323 	}
    324 }
    325 
    326 /*
    327  * "Mountroot hook" types, functions, and variables.
    328  */
    329 
    330 struct mountroothook_desc {
    331 	LIST_ENTRY(mountroothook_desc) mrd_list;
    332 	struct	device *mrd_device;
    333 	void 	(*mrd_func) __P((struct device *));
    334 };
    335 
    336 LIST_HEAD(, mountroothook_desc) mountroothook_list;
    337 
    338 void *
    339 mountroothook_establish(func, dev)
    340 	void (*func) __P((struct device *));
    341 	struct device *dev;
    342 {
    343 	struct mountroothook_desc *mrd;
    344 
    345 	mrd = (struct mountroothook_desc *)
    346 	    malloc(sizeof(*mrd), M_DEVBUF, M_NOWAIT);
    347 	if (mrd == NULL)
    348 		return (NULL);
    349 
    350 	mrd->mrd_device = dev;
    351 	mrd->mrd_func = func;
    352 	LIST_INSERT_HEAD(&mountroothook_list, mrd, mrd_list);
    353 
    354 	return (mrd);
    355 }
    356 
    357 void
    358 mountroothook_disestablish(vhook)
    359 	void *vhook;
    360 {
    361 #ifdef DIAGNOSTIC
    362 	struct mountroothook_desc *mrd;
    363 
    364 	for (mrd = mountroothook_list.lh_first; mrd != NULL;
    365 	    mrd = mrd->mrd_list.le_next)
    366                 if (mrd == vhook)
    367 			break;
    368 	if (mrd == NULL)
    369 		panic("mountroothook_disestablish: hook not established");
    370 #endif
    371 
    372 	LIST_REMOVE((struct mountroothook_desc *)vhook, mrd_list);
    373 	free(vhook, M_DEVBUF);
    374 }
    375 
    376 void
    377 mountroothook_destroy()
    378 {
    379 	struct mountroothook_desc *mrd;
    380 
    381 	while ((mrd = mountroothook_list.lh_first) != NULL) {
    382 		LIST_REMOVE(mrd, mrd_list);
    383 		free(mrd, M_DEVBUF);
    384 	}
    385 }
    386 
    387 void
    388 domountroothook()
    389 {
    390 	struct mountroothook_desc *mrd;
    391 
    392 	for (mrd = mountroothook_list.lh_first; mrd != NULL;
    393 	    mrd = mrd->mrd_list.le_next) {
    394 		if (mrd->mrd_device == root_device) {
    395 			(*mrd->mrd_func)(root_device);
    396 			return;
    397 		}
    398 	}
    399 }
    400 
    401 /*
    402  * Determine the root device and, if instructed to, the root file system.
    403  */
    404 
    405 #include "md.h"
    406 #if NMD == 0
    407 #undef MEMORY_DISK_HOOKS
    408 #endif
    409 
    410 #ifdef MEMORY_DISK_HOOKS
    411 static struct device fakemdrootdev[NMD];
    412 #endif
    413 
    414 void
    415 setroot(bootdv, bootpartition, nam2blk)
    416 	struct device *bootdv;
    417 	int bootpartition;
    418 	struct devnametobdevmaj *nam2blk;
    419 {
    420 	struct device *dv;
    421 	int len, print_newline = 0;
    422 #ifdef MEMORY_DISK_HOOKS
    423 	int i;
    424 #endif
    425 	dev_t nrootdev;
    426 	dev_t ndumpdev = NODEV;
    427 	char buf[128];
    428 	const char *rootdevname;
    429 	const char *dumpdevname;
    430 	struct device *rootdv = NULL;		/* XXX gcc -Wuninitialized */
    431 	struct device *dumpdv = NULL;
    432 	struct ifnet *ifp;
    433 	const char *deffsname;
    434 	struct vfsops *vops;
    435 	extern int (*mountroot) __P((void));
    436 	static struct devnametobdevmaj *last_nam2blk;
    437 
    438 	if (nam2blk == NULL) {
    439 		if (last_nam2blk == NULL)
    440 			panic("setroot: no name to bdev major map");
    441 		nam2blk = last_nam2blk;
    442 	}
    443 	last_nam2blk = nam2blk;
    444 
    445 #ifdef MEMORY_DISK_HOOKS
    446 	for (i = 0; i < NMD; i++) {
    447 		fakemdrootdev[i].dv_class  = DV_DISK;
    448 		fakemdrootdev[i].dv_cfdata = NULL;
    449 		fakemdrootdev[i].dv_unit   = i;
    450 		fakemdrootdev[i].dv_parent = NULL;
    451 		sprintf(fakemdrootdev[i].dv_xname, "md%d", i);
    452 	}
    453 #endif /* MEMORY_DISK_HOOKS */
    454 
    455 #ifdef MEMORY_DISK_IS_ROOT
    456 	bootdv = &fakemdrootdev[0];
    457 	bootpartition = 0;
    458 #endif
    459 
    460 	/*
    461 	 * If NFS is specified as the file system, and we found
    462 	 * a DV_DISK boot device (or no boot device at all), then
    463 	 * find a reasonable network interface for "rootspec".
    464 	 */
    465 	vops = vfs_getopsbyname("nfs");
    466 	if (vops != NULL && vops->vfs_mountroot == mountroot &&
    467 	    rootspec == NULL &&
    468 	    (bootdv == NULL || bootdv->dv_class != DV_IFNET)) {
    469 		for (ifp = ifnet.tqh_first; ifp != NULL;
    470 		    ifp = ifp->if_list.tqe_next)
    471 			if ((ifp->if_flags &
    472 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    473 				break;
    474 		if (ifp == NULL) {
    475 			/*
    476 			 * Can't find a suitable interface; ask the
    477 			 * user.
    478 			 */
    479 			boothowto |= RB_ASKNAME;
    480 		} else {
    481 			/*
    482 			 * Have a suitable interface; behave as if
    483 			 * the user specified this interface.
    484 			 */
    485 			rootspec = (const char *)ifp->if_xname;
    486 		}
    487 	}
    488 
    489 	/*
    490 	 * If wildcarded root and we the boot device wasn't determined,
    491 	 * ask the user.
    492 	 */
    493 	if (rootspec == NULL && bootdv == NULL)
    494 		boothowto |= RB_ASKNAME;
    495 
    496  top:
    497 	if (boothowto & RB_ASKNAME) {
    498 		struct device *defdumpdv;
    499 
    500 		for (;;) {
    501 			printf("root device");
    502 			if (bootdv != NULL) {
    503 				printf(" (default %s", bootdv->dv_xname);
    504 				if (bootdv->dv_class == DV_DISK)
    505 					printf("%c", bootpartition + 'a');
    506 				printf(")");
    507 			}
    508 			printf(": ");
    509 			len = getstr(buf, sizeof(buf));
    510 			if (len == 0 && bootdv != NULL) {
    511 				strcpy(buf, bootdv->dv_xname);
    512 				len = strlen(buf);
    513 			}
    514 			if (len > 0 && buf[len - 1] == '*') {
    515 				buf[--len] = '\0';
    516 				dv = getdisk(buf, len, 1, nam2blk,
    517 				    &nrootdev, 0);
    518 				if (dv != NULL) {
    519 					rootdv = dv;
    520 					break;
    521 				}
    522 			}
    523 			dv = getdisk(buf, len, bootpartition, nam2blk,
    524 			    &nrootdev, 0);
    525 			if (dv != NULL) {
    526 				rootdv = dv;
    527 				break;
    528 			}
    529 		}
    530 
    531 		/*
    532 		 * Set up the default dump device.  If root is on
    533 		 * a network device, there is no default dump
    534 		 * device, since we don't support dumps to the
    535 		 * network.
    536 		 */
    537 		if (rootdv->dv_class == DV_IFNET)
    538 			defdumpdv = NULL;
    539 		else
    540 			defdumpdv = rootdv;
    541 
    542 		for (;;) {
    543 			printf("dump device");
    544 			if (defdumpdv != NULL) {
    545 				/*
    546 				 * Note, we know it's a disk if we get here.
    547 				 */
    548 				printf(" (default %sb)", defdumpdv->dv_xname);
    549 			}
    550 			printf(": ");
    551 			len = getstr(buf, sizeof(buf));
    552 			if (len == 0) {
    553 				if (defdumpdv != NULL) {
    554 					ndumpdev = MAKEDISKDEV(major(nrootdev),
    555 					    DISKUNIT(nrootdev), 1);
    556 				}
    557 				if (rootdv->dv_class == DV_IFNET)
    558 					dumpdv = NULL;
    559 				else
    560 					dumpdv = rootdv;
    561 				break;
    562 			}
    563 			if (len == 4 && strcmp(buf, "none") == 0) {
    564 				dumpspec = "none";
    565 				goto havedump;
    566 			}
    567 			dv = getdisk(buf, len, 1, nam2blk, &ndumpdev, 1);
    568 			if (dv) {
    569 				dumpdv = dv;
    570 				break;
    571 			}
    572 		}
    573 
    574  havedump:
    575 		rootdev = nrootdev;
    576 		dumpdev = ndumpdev;
    577 
    578 		for (vops = LIST_FIRST(&vfs_list); vops != NULL;
    579 		     vops = LIST_NEXT(vops, vfs_list)) {
    580 			if (vops->vfs_mountroot != NULL &&
    581 			    vops->vfs_mountroot == mountroot)
    582 			break;
    583 		}
    584 
    585 		if (vops == NULL) {
    586 			mountroot = NULL;
    587 			deffsname = "generic";
    588 		} else
    589 			deffsname = vops->vfs_name;
    590 
    591 		for (;;) {
    592 			printf("file system (default %s): ", deffsname);
    593 			len = getstr(buf, sizeof(buf));
    594 			if (len == 0)
    595 				break;
    596 			if (len == 4 && strcmp(buf, "halt") == 0)
    597 				cpu_reboot(RB_HALT, NULL);
    598 			else if (len == 7 && strcmp(buf, "generic") == 0) {
    599 				mountroot = NULL;
    600 				break;
    601 			}
    602 			vops = vfs_getopsbyname(buf);
    603 			if (vops == NULL || vops->vfs_mountroot == NULL) {
    604 				printf("use one of: generic");
    605 				for (vops = LIST_FIRST(&vfs_list);
    606 				     vops != NULL;
    607 				     vops = LIST_NEXT(vops, vfs_list)) {
    608 					if (vops->vfs_mountroot != NULL)
    609 						printf(" %s", vops->vfs_name);
    610 				}
    611 				printf(" halt\n");
    612 			} else {
    613 				mountroot = vops->vfs_mountroot;
    614 				break;
    615 			}
    616 		}
    617 
    618 	} else if (rootspec == NULL) {
    619 		int majdev;
    620 
    621 		/*
    622 		 * Wildcarded root; use the boot device.
    623 		 */
    624 		rootdv = bootdv;
    625 
    626 		majdev = findblkmajor(bootdv->dv_xname, nam2blk);
    627 		if (majdev >= 0) {
    628 			/*
    629 			 * Root is on a disk.  `bootpartition' is root.
    630 			 */
    631 			rootdev = MAKEDISKDEV(majdev, bootdv->dv_unit,
    632 			    bootpartition);
    633 		}
    634 	} else {
    635 
    636 		/*
    637 		 * `root on <dev> ...'
    638 		 */
    639 
    640 		/*
    641 		 * If it's a network interface, we can bail out
    642 		 * early.
    643 		 */
    644 		for (dv = alldevs.tqh_first; dv != NULL;
    645 		    dv = dv->dv_list.tqe_next)
    646 			if (strcmp(dv->dv_xname, rootspec) == 0)
    647 				break;
    648 		if (dv != NULL && dv->dv_class == DV_IFNET) {
    649 			rootdv = dv;
    650 			goto haveroot;
    651 		}
    652 
    653 		rootdevname = findblkname(major(rootdev), nam2blk);
    654 		if (rootdevname == NULL) {
    655 			printf("unknown device major 0x%x\n", rootdev);
    656 			boothowto |= RB_ASKNAME;
    657 			goto top;
    658 		}
    659 		memset(buf, 0, sizeof(buf));
    660 		sprintf(buf, "%s%d", rootdevname, DISKUNIT(rootdev));
    661 
    662 		for (dv = alldevs.tqh_first; dv != NULL;
    663 		    dv = dv->dv_list.tqe_next) {
    664 			if (strcmp(buf, dv->dv_xname) == 0) {
    665 				rootdv = dv;
    666 				break;
    667 			}
    668 		}
    669 		if (rootdv == NULL) {
    670 			printf("device %s (0x%x) not configured\n",
    671 			    buf, rootdev);
    672 			boothowto |= RB_ASKNAME;
    673 			goto top;
    674 		}
    675 	}
    676 
    677  haveroot:
    678 
    679 	root_device = rootdv;
    680 
    681 	switch (rootdv->dv_class) {
    682 	case DV_IFNET:
    683 		/* Nothing. */
    684 		break;
    685 
    686 	case DV_DISK:
    687 		printf("root on %s%c", rootdv->dv_xname,
    688 		    DISKPART(rootdev) + 'a');
    689 		print_newline = 1;
    690 		break;
    691 
    692 	default:
    693 		printf("can't determine root device\n");
    694 		boothowto |= RB_ASKNAME;
    695 		goto top;
    696 	}
    697 
    698 	/*
    699 	 * Now configure the dump device.
    700 	 */
    701 
    702 	if (dumpspec != NULL && strcmp(dumpspec, "none") == 0) {
    703 		/*
    704 		 * Operator doesn't want a dump device.
    705 		 */
    706 		goto nodumpdev;
    707 	}
    708 
    709 	/*
    710 	 * If we haven't figured out the dump device, do so, with
    711 	 * the following rules:
    712 	 *
    713 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
    714 	 *
    715 	 *	(b) If dumpspec is set, try to use it.  If the device
    716 	 *	    is not available, punt.
    717 	 *
    718 	 *	(c) If dumpspec is not set, the dump device is
    719 	 *	    wildcarded or unspecified.  If the root device
    720 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
    721 	 *	    of the root device.
    722 	 */
    723 
    724 	if (boothowto & RB_ASKNAME) {
    725 		if (dumpdv == NULL) {
    726 			/*
    727 			 * Just return; dumpdev is already set to NODEV
    728 			 * and we don't want to print a newline in this
    729 			 * case.
    730 			 */
    731 			return;
    732 		}
    733 		goto out;
    734 	}
    735 
    736 	if (dumpspec != NULL) {
    737 		if (dumpdev == NODEV) {
    738 			/*
    739 			 * Looks like they tried to pick a network
    740 			 * device.  Oops.
    741 			 */
    742 			goto nodumpdev;
    743 		}
    744 
    745 		dumpdevname = findblkname(major(dumpdev), nam2blk);
    746 		if (dumpdevname == NULL)
    747 			goto nodumpdev;
    748 		memset(buf, 0, sizeof(buf));
    749 		sprintf(buf, "%s%d", dumpdevname, DISKUNIT(dumpdev));
    750 
    751 		for (dv = alldevs.tqh_first; dv != NULL;
    752 		    dv = dv->dv_list.tqe_next) {
    753 			if (strcmp(buf, dv->dv_xname) == 0) {
    754 				dumpdv = dv;
    755 				break;
    756 			}
    757 		}
    758 		if (dv == NULL) {
    759 			/*
    760 			 * Device not configured.
    761 			 */
    762 			goto nodumpdev;
    763 		}
    764 	} else if (rootdv->dv_class == DV_IFNET)
    765 		goto nodumpdev;
    766 	else {
    767 		dumpdv = rootdv;
    768 		dumpdev = MAKEDISKDEV(major(rootdev), dumpdv->dv_unit, 1);
    769 	}
    770 
    771  out:
    772 	printf(" dumps on %s%c\n", dumpdv->dv_xname, DISKPART(dumpdev) + 'a');
    773 	return;
    774 
    775  nodumpdev:
    776 	dumpdev = NODEV;
    777 	if (print_newline)
    778 		printf("\n");
    779 }
    780 
    781 static int
    782 findblkmajor(name, nam2blk)
    783 	const char *name;
    784 	struct devnametobdevmaj *nam2blk;
    785 {
    786 	int i;
    787 
    788 	if (nam2blk == NULL)
    789 		return (-1);
    790 
    791 	for (i = 0; nam2blk[i].d_name != NULL; i++)
    792 		if (strncmp(name, nam2blk[i].d_name,
    793 		    strlen(nam2blk[i].d_name)) == 0)
    794 			return (nam2blk[i].d_maj);
    795 	return (-1);
    796 }
    797 
    798 const char *
    799 findblkname(maj, nam2blk)
    800 	int maj;
    801 	struct devnametobdevmaj *nam2blk;
    802 {
    803 	int i;
    804 
    805 	if (nam2blk == NULL)
    806 		return (NULL);
    807 
    808 	for (i = 0; nam2blk[i].d_name != NULL; i++)
    809 		if (nam2blk[i].d_maj == maj)
    810 			return (nam2blk[i].d_name);
    811 	return (NULL);
    812 }
    813 
    814 static struct device *
    815 getdisk(str, len, defpart, nam2blk, devp, isdump)
    816 	char *str;
    817 	int len, defpart;
    818 	struct devnametobdevmaj *nam2blk;
    819 	dev_t *devp;
    820 	int isdump;
    821 {
    822 	struct device	*dv;
    823 #ifdef MEMORY_DISK_HOOKS
    824 	int		i;
    825 #endif
    826 
    827 	if ((dv = parsedisk(str, len, defpart, nam2blk, devp)) == NULL) {
    828 		printf("use one of:");
    829 #ifdef MEMORY_DISK_HOOKS
    830 		if (isdump == 0)
    831 			for (i = 0; i < NMD; i++)
    832 				printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
    833 				    'a' + MAXPARTITIONS - 1);
    834 #endif
    835 		for (dv = alldevs.tqh_first; dv != NULL;
    836 		    dv = dv->dv_list.tqe_next) {
    837 			if (dv->dv_class == DV_DISK)
    838 				printf(" %s[a-%c]", dv->dv_xname,
    839 				    'a' + MAXPARTITIONS - 1);
    840 			if (isdump == 0 && dv->dv_class == DV_IFNET)
    841 				printf(" %s", dv->dv_xname);
    842 		}
    843 		if (isdump)
    844 			printf(" none");
    845 		printf(" halt\n");
    846 	}
    847 	return (dv);
    848 }
    849 
    850 static struct device *
    851 parsedisk(str, len, defpart, nam2blk, devp)
    852 	char *str;
    853 	int len, defpart;
    854 	struct devnametobdevmaj *nam2blk;
    855 	dev_t *devp;
    856 {
    857 	struct device *dv;
    858 	char *cp, c;
    859 	int majdev, part;
    860 #ifdef MEMORY_DISK_HOOKS
    861 	int i;
    862 #endif
    863 
    864 	if (len == 0)
    865 		return (NULL);
    866 
    867 	if (len == 4 && strcmp(str, "halt") == 0)
    868 		cpu_reboot(RB_HALT, NULL);
    869 
    870 	cp = str + len - 1;
    871 	c = *cp;
    872 	if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
    873 		part = c - 'a';
    874 		*cp = '\0';
    875 	} else
    876 		part = defpart;
    877 
    878 #ifdef MEMORY_DISK_HOOKS
    879 	for (i = 0; i < NMD; i++)
    880 		if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) {
    881 			dv = &fakemdrootdev[i];
    882 			goto gotdisk;
    883 		}
    884 #endif
    885 
    886 	for (dv = alldevs.tqh_first; dv != NULL; dv = dv->dv_list.tqe_next) {
    887 		if (dv->dv_class == DV_DISK &&
    888 		    strcmp(str, dv->dv_xname) == 0) {
    889 #ifdef MEMORY_DISK_HOOKS
    890  gotdisk:
    891 #endif
    892 			majdev = findblkmajor(dv->dv_xname, nam2blk);
    893 			if (majdev < 0)
    894 				panic("parsedisk");
    895 			*devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
    896 			break;
    897 		}
    898 
    899 		if (dv->dv_class == DV_IFNET &&
    900 		    strcmp(str, dv->dv_xname) == 0) {
    901 			*devp = NODEV;
    902 			break;
    903 		}
    904 	}
    905 
    906 	*cp = c;
    907 	return (dv);
    908 }
    909 
    910 /*
    911  * XXX shouldn't this be a common function?
    912  */
    913 static int
    914 getstr(cp, size)
    915 	char *cp;
    916 	int size;
    917 {
    918 	char *lp;
    919 	int c, len;
    920 
    921 	cnpollc(1);
    922 
    923 	lp = cp;
    924 	len = 0;
    925 	for (;;) {
    926 		c = cngetc();
    927 		switch (c) {
    928 		case '\n':
    929 		case '\r':
    930 			printf("\n");
    931 			*lp++ = '\0';
    932 			cnpollc(0);
    933 			return (len);
    934 		case '\b':
    935 		case '\177':
    936 		case '#':
    937 			if (len) {
    938 				--len;
    939 				--lp;
    940 				printf("\b \b");
    941 			}
    942 			continue;
    943 		case '@':
    944 		case 'u'&037:
    945 			len = 0;
    946 			lp = cp;
    947 			printf("\n");
    948 			continue;
    949 		default:
    950 			if (len + 1 >= size || c < ' ') {
    951 				printf("\007");
    952 				continue;
    953 			}
    954 			printf("%c", c);
    955 			++len;
    956 			*lp++ = c;
    957 		}
    958 	}
    959 }
    960