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