Home | History | Annotate | Line # | Download | only in kern
kern_subr.c revision 1.50
      1 /*	$NetBSD: kern_subr.c,v 1.50 1999/06/02 05:53:56 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and by Luke Mewburn.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 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_md.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 (uio->uio_rw == UIO_READ)
    158 				error = kcopy(cp, iov->iov_base, cnt);
    159 			else
    160 				error = kcopy(iov->iov_base, cp, cnt);
    161 			if (error)
    162 				return(error);
    163 			break;
    164 		}
    165 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    166 		iov->iov_len -= cnt;
    167 		uio->uio_resid -= cnt;
    168 		uio->uio_offset += cnt;
    169 		cp += cnt;
    170 		n -= cnt;
    171 	}
    172 	return (error);
    173 }
    174 
    175 /*
    176  * Give next character to user as result of read.
    177  */
    178 int
    179 ureadc(c, uio)
    180 	register int c;
    181 	register struct uio *uio;
    182 {
    183 	register struct iovec *iov;
    184 
    185 	if (uio->uio_resid <= 0)
    186 		panic("ureadc: non-positive resid");
    187 again:
    188 	if (uio->uio_iovcnt <= 0)
    189 		panic("ureadc: non-positive iovcnt");
    190 	iov = uio->uio_iov;
    191 	if (iov->iov_len <= 0) {
    192 		uio->uio_iovcnt--;
    193 		uio->uio_iov++;
    194 		goto again;
    195 	}
    196 	switch (uio->uio_segflg) {
    197 
    198 	case UIO_USERSPACE:
    199 		if (subyte(iov->iov_base, c) < 0)
    200 			return (EFAULT);
    201 		break;
    202 
    203 	case UIO_SYSSPACE:
    204 		*(char *)iov->iov_base = c;
    205 		break;
    206 	}
    207 	iov->iov_base = (caddr_t)iov->iov_base + 1;
    208 	iov->iov_len--;
    209 	uio->uio_resid--;
    210 	uio->uio_offset++;
    211 	return (0);
    212 }
    213 
    214 /*
    215  * General routine to allocate a hash table.
    216  * Allocate enough memory to hold at least `elements' list-head pointers.
    217  * Return a pointer to the allocated space and set *hashmask to a pattern
    218  * suitable for masking a value to use as an index into the returned array.
    219  */
    220 void *
    221 hashinit(elements, type, flags, hashmask)
    222 	int elements, type, flags;
    223 	u_long *hashmask;
    224 {
    225 	long hashsize;
    226 	LIST_HEAD(generic, generic) *hashtbl;
    227 	int i;
    228 
    229 	if (elements <= 0)
    230 		panic("hashinit: bad cnt");
    231 	for (hashsize = 1; hashsize < elements; hashsize <<= 1)
    232 		continue;
    233 	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, flags);
    234 	for (i = 0; i < hashsize; i++)
    235 		LIST_INIT(&hashtbl[i]);
    236 	*hashmask = hashsize - 1;
    237 	return (hashtbl);
    238 }
    239 
    240 /*
    241  * "Shutdown hook" types, functions, and variables.
    242  */
    243 
    244 struct shutdownhook_desc {
    245 	LIST_ENTRY(shutdownhook_desc) sfd_list;
    246 	void	(*sfd_fn) __P((void *));
    247 	void	*sfd_arg;
    248 };
    249 
    250 LIST_HEAD(, shutdownhook_desc) shutdownhook_list;
    251 
    252 void *
    253 shutdownhook_establish(fn, arg)
    254 	void (*fn) __P((void *));
    255 	void *arg;
    256 {
    257 	struct shutdownhook_desc *ndp;
    258 
    259 	ndp = (struct shutdownhook_desc *)
    260 	    malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
    261 	if (ndp == NULL)
    262 		return NULL;
    263 
    264 	ndp->sfd_fn = fn;
    265 	ndp->sfd_arg = arg;
    266 	LIST_INSERT_HEAD(&shutdownhook_list, ndp, sfd_list);
    267 
    268 	return (ndp);
    269 }
    270 
    271 void
    272 shutdownhook_disestablish(vhook)
    273 	void *vhook;
    274 {
    275 #ifdef DIAGNOSTIC
    276 	struct shutdownhook_desc *dp;
    277 
    278 	for (dp = shutdownhook_list.lh_first; dp != NULL;
    279 	    dp = dp->sfd_list.le_next)
    280                 if (dp == vhook)
    281 			break;
    282 	if (dp == NULL)
    283 		panic("shutdownhook_disestablish: hook not established");
    284 #endif
    285 
    286 	LIST_REMOVE((struct shutdownhook_desc *)vhook, sfd_list);
    287 	free(vhook, M_DEVBUF);
    288 }
    289 
    290 /*
    291  * Run shutdown hooks.  Should be invoked immediately before the
    292  * system is halted or rebooted, i.e. after file systems unmounted,
    293  * after crash dump done, etc.
    294  *
    295  * Each shutdown hook is removed from the list before it's run, so that
    296  * it won't be run again.
    297  */
    298 void
    299 doshutdownhooks()
    300 {
    301 	struct shutdownhook_desc *dp;
    302 
    303 	while ((dp = shutdownhook_list.lh_first) != NULL) {
    304 		LIST_REMOVE(dp, sfd_list);
    305 		(*dp->sfd_fn)(dp->sfd_arg);
    306 #if 0
    307 		/*
    308 		 * Don't bother freeing the hook structure,, since we may
    309 		 * be rebooting because of a memory corruption problem,
    310 		 * and this might only make things worse.  It doesn't
    311 		 * matter, anyway, since the system is just about to
    312 		 * reboot.
    313 		 */
    314 		free(dp, M_DEVBUF);
    315 #endif
    316 	}
    317 }
    318 
    319 /*
    320  * "Mountroot hook" types, functions, and variables.
    321  */
    322 
    323 struct mountroothook_desc {
    324 	LIST_ENTRY(mountroothook_desc) mrd_list;
    325 	struct	device *mrd_device;
    326 	void 	(*mrd_func) __P((struct device *));
    327 };
    328 
    329 LIST_HEAD(, mountroothook_desc) mountroothook_list;
    330 
    331 void *
    332 mountroothook_establish(func, dev)
    333 	void (*func) __P((struct device *));
    334 	struct device *dev;
    335 {
    336 	struct mountroothook_desc *mrd;
    337 
    338 	mrd = (struct mountroothook_desc *)
    339 	    malloc(sizeof(*mrd), M_DEVBUF, M_NOWAIT);
    340 	if (mrd == NULL)
    341 		return (NULL);
    342 
    343 	mrd->mrd_device = dev;
    344 	mrd->mrd_func = func;
    345 	LIST_INSERT_HEAD(&mountroothook_list, mrd, mrd_list);
    346 
    347 	return (mrd);
    348 }
    349 
    350 void
    351 mountroothook_disestablish(vhook)
    352 	void *vhook;
    353 {
    354 #ifdef DIAGNOSTIC
    355 	struct mountroothook_desc *mrd;
    356 
    357 	for (mrd = mountroothook_list.lh_first; mrd != NULL;
    358 	    mrd = mrd->mrd_list.le_next)
    359                 if (mrd == vhook)
    360 			break;
    361 	if (mrd == NULL)
    362 		panic("mountroothook_disestablish: hook not established");
    363 #endif
    364 
    365 	LIST_REMOVE((struct mountroothook_desc *)vhook, mrd_list);
    366 	free(vhook, M_DEVBUF);
    367 }
    368 
    369 void
    370 mountroothook_destroy()
    371 {
    372 	struct mountroothook_desc *mrd;
    373 
    374 	while ((mrd = mountroothook_list.lh_first) != NULL) {
    375 		LIST_REMOVE(mrd, mrd_list);
    376 		free(mrd, M_DEVBUF);
    377 	}
    378 }
    379 
    380 void
    381 domountroothook()
    382 {
    383 	struct mountroothook_desc *mrd;
    384 
    385 	for (mrd = mountroothook_list.lh_first; mrd != NULL;
    386 	    mrd = mrd->mrd_list.le_next) {
    387 		if (mrd->mrd_device == root_device) {
    388 			(*mrd->mrd_func)(root_device);
    389 			return;
    390 		}
    391 	}
    392 }
    393 
    394 /*
    395  * Determine the root device and, if instructed to, the root file system.
    396  */
    397 
    398 #include "md.h"
    399 #if NMD == 0
    400 #undef MEMORY_DISK_HOOKS
    401 #endif
    402 
    403 #ifdef MEMORY_DISK_HOOKS
    404 static struct device fakemdrootdev[NMD];
    405 #endif
    406 
    407 void
    408 setroot(bootdv, bootpartition, nam2blk)
    409 	struct device *bootdv;
    410 	int bootpartition;
    411 	struct devnametobdevmaj *nam2blk;
    412 {
    413 	struct device *dv;
    414 	int len, print_newline = 0;
    415 #ifdef MEMORY_DISK_HOOKS
    416 	int i;
    417 #endif
    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 (vops = LIST_FIRST(&vfs_list); vops != NULL;
    572 		     vops = LIST_NEXT(vops, vfs_list)) {
    573 			if (vops->vfs_mountroot != NULL &&
    574 			    vops->vfs_mountroot == mountroot)
    575 			break;
    576 		}
    577 
    578 		if (vops == NULL) {
    579 			mountroot = NULL;
    580 			deffsname = "generic";
    581 		} else
    582 			deffsname = vops->vfs_name;
    583 
    584 		for (;;) {
    585 			printf("file system (default %s): ", deffsname);
    586 			len = getstr(buf, sizeof(buf));
    587 			if (len == 0)
    588 				break;
    589 			if (len == 4 && strcmp(buf, "halt") == 0)
    590 				cpu_reboot(RB_HALT, NULL);
    591 			else if (len == 7 && strcmp(buf, "generic") == 0) {
    592 				mountroot = NULL;
    593 				break;
    594 			}
    595 			vops = vfs_getopsbyname(buf);
    596 			if (vops == NULL || vops->vfs_mountroot == NULL) {
    597 				printf("use one of: generic");
    598 				for (vops = LIST_FIRST(&vfs_list);
    599 				     vops != NULL;
    600 				     vops = LIST_NEXT(vops, vfs_list)) {
    601 					if (vops->vfs_mountroot != NULL)
    602 						printf(" %s", vops->vfs_name);
    603 				}
    604 				printf(" halt\n");
    605 			} else {
    606 				mountroot = vops->vfs_mountroot;
    607 				break;
    608 			}
    609 		}
    610 
    611 	} else if (rootspec == NULL) {
    612 		int majdev;
    613 
    614 		/*
    615 		 * Wildcarded root; use the boot device.
    616 		 */
    617 		rootdv = bootdv;
    618 
    619 		majdev = findblkmajor(bootdv->dv_xname, nam2blk);
    620 		if (majdev >= 0) {
    621 			/*
    622 			 * Root is on a disk.  `bootpartition' is root.
    623 			 */
    624 			rootdev = MAKEDISKDEV(majdev, bootdv->dv_unit,
    625 			    bootpartition);
    626 		}
    627 	} else {
    628 
    629 		/*
    630 		 * `root on <dev> ...'
    631 		 */
    632 
    633 		/*
    634 		 * If it's a network interface, we can bail out
    635 		 * early.
    636 		 */
    637 		for (dv = alldevs.tqh_first; dv != NULL;
    638 		    dv = dv->dv_list.tqe_next)
    639 			if (strcmp(dv->dv_xname, rootspec) == 0)
    640 				break;
    641 		if (dv != NULL && dv->dv_class == DV_IFNET) {
    642 			rootdv = dv;
    643 			goto haveroot;
    644 		}
    645 
    646 		rootdevname = findblkname(major(rootdev), nam2blk);
    647 		if (rootdevname == NULL) {
    648 			printf("unknown device major 0x%x\n", rootdev);
    649 			boothowto |= RB_ASKNAME;
    650 			goto top;
    651 		}
    652 		memset(buf, 0, sizeof(buf));
    653 		sprintf(buf, "%s%d", rootdevname, DISKUNIT(rootdev));
    654 
    655 		for (dv = alldevs.tqh_first; dv != NULL;
    656 		    dv = dv->dv_list.tqe_next) {
    657 			if (strcmp(buf, dv->dv_xname) == 0) {
    658 				rootdv = dv;
    659 				break;
    660 			}
    661 		}
    662 		if (rootdv == NULL) {
    663 			printf("device %s (0x%x) not configured\n",
    664 			    buf, rootdev);
    665 			boothowto |= RB_ASKNAME;
    666 			goto top;
    667 		}
    668 	}
    669 
    670  haveroot:
    671 
    672 	root_device = rootdv;
    673 
    674 	switch (rootdv->dv_class) {
    675 	case DV_IFNET:
    676 		/* Nothing. */
    677 		break;
    678 
    679 	case DV_DISK:
    680 		printf("root on %s%c", rootdv->dv_xname,
    681 		    DISKPART(rootdev) + 'a');
    682 		print_newline = 1;
    683 		break;
    684 
    685 	default:
    686 		printf("can't determine root device\n");
    687 		boothowto |= RB_ASKNAME;
    688 		goto top;
    689 	}
    690 
    691 	/*
    692 	 * Now configure the dump device.
    693 	 */
    694 
    695 	if (dumpspec != NULL && strcmp(dumpspec, "none") == 0) {
    696 		/*
    697 		 * Operator doesn't want a dump device.
    698 		 */
    699 		goto nodumpdev;
    700 	}
    701 
    702 	/*
    703 	 * If we haven't figured out the dump device, do so, with
    704 	 * the following rules:
    705 	 *
    706 	 *	(a) We already know dumpdv in the RB_ASKNAME case.
    707 	 *
    708 	 *	(b) If dumpspec is set, try to use it.  If the device
    709 	 *	    is not available, punt.
    710 	 *
    711 	 *	(c) If dumpspec is not set, the dump device is
    712 	 *	    wildcarded or unspecified.  If the root device
    713 	 *	    is DV_IFNET, punt.  Otherwise, use partition b
    714 	 *	    of the root device.
    715 	 */
    716 
    717 	if (boothowto & RB_ASKNAME) {
    718 		if (dumpdv == NULL) {
    719 			/*
    720 			 * Just return; dumpdev is already set to NODEV
    721 			 * and we don't want to print a newline in this
    722 			 * case.
    723 			 */
    724 			return;
    725 		}
    726 		goto out;
    727 	}
    728 
    729 	if (dumpspec != NULL) {
    730 		if (dumpdev == NODEV) {
    731 			/*
    732 			 * Looks like they tried to pick a network
    733 			 * device.  Oops.
    734 			 */
    735 			goto nodumpdev;
    736 		}
    737 
    738 		dumpdevname = findblkname(major(dumpdev), nam2blk);
    739 		if (dumpdevname == NULL)
    740 			goto nodumpdev;
    741 		memset(buf, 0, sizeof(buf));
    742 		sprintf(buf, "%s%d", dumpdevname, DISKUNIT(dumpdev));
    743 
    744 		for (dv = alldevs.tqh_first; dv != NULL;
    745 		    dv = dv->dv_list.tqe_next) {
    746 			if (strcmp(buf, dv->dv_xname) == 0) {
    747 				dumpdv = dv;
    748 				break;
    749 			}
    750 		}
    751 		if (dv == NULL) {
    752 			/*
    753 			 * Device not configured.
    754 			 */
    755 			goto nodumpdev;
    756 		}
    757 	} else if (rootdv->dv_class == DV_IFNET)
    758 		goto nodumpdev;
    759 	else {
    760 		dumpdv = rootdv;
    761 		dumpdev = MAKEDISKDEV(major(rootdev), dumpdv->dv_unit, 1);
    762 	}
    763 
    764  out:
    765 	printf(" dumps on %s%c\n", dumpdv->dv_xname, DISKPART(dumpdev) + 'a');
    766 	return;
    767 
    768  nodumpdev:
    769 	dumpdev = NODEV;
    770 	if (print_newline)
    771 		printf("\n");
    772 }
    773 
    774 static int
    775 findblkmajor(name, nam2blk)
    776 	const char *name;
    777 	struct devnametobdevmaj *nam2blk;
    778 {
    779 	int i;
    780 
    781 	if (nam2blk == NULL)
    782 		return (-1);
    783 
    784 	for (i = 0; nam2blk[i].d_name != NULL; i++)
    785 		if (strncmp(name, nam2blk[i].d_name,
    786 		    strlen(nam2blk[i].d_name)) == 0)
    787 			return (nam2blk[i].d_maj);
    788 	return (-1);
    789 }
    790 
    791 const char *
    792 findblkname(maj, nam2blk)
    793 	int maj;
    794 	struct devnametobdevmaj *nam2blk;
    795 {
    796 	int i;
    797 
    798 	if (nam2blk == NULL)
    799 		return (NULL);
    800 
    801 	for (i = 0; nam2blk[i].d_name != NULL; i++)
    802 		if (nam2blk[i].d_maj == maj)
    803 			return (nam2blk[i].d_name);
    804 	return (NULL);
    805 }
    806 
    807 static struct device *
    808 getdisk(str, len, defpart, nam2blk, devp, isdump)
    809 	char *str;
    810 	int len, defpart;
    811 	struct devnametobdevmaj *nam2blk;
    812 	dev_t *devp;
    813 	int isdump;
    814 {
    815 	struct device	*dv;
    816 #ifdef MEMORY_DISK_HOOKS
    817 	int		i;
    818 #endif
    819 
    820 	if ((dv = parsedisk(str, len, defpart, nam2blk, devp)) == NULL) {
    821 		printf("use one of:");
    822 #ifdef MEMORY_DISK_HOOKS
    823 		if (isdump == 0)
    824 			for (i = 0; i < NMD; i++)
    825 				printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
    826 				    'a' + MAXPARTITIONS - 1);
    827 #endif
    828 		for (dv = alldevs.tqh_first; dv != NULL;
    829 		    dv = dv->dv_list.tqe_next) {
    830 			if (dv->dv_class == DV_DISK)
    831 				printf(" %s[a-%c]", dv->dv_xname,
    832 				    'a' + MAXPARTITIONS - 1);
    833 			if (isdump == 0 && dv->dv_class == DV_IFNET)
    834 				printf(" %s", dv->dv_xname);
    835 		}
    836 		if (isdump)
    837 			printf(" none");
    838 		printf(" halt\n");
    839 	}
    840 	return (dv);
    841 }
    842 
    843 static struct device *
    844 parsedisk(str, len, defpart, nam2blk, devp)
    845 	char *str;
    846 	int len, defpart;
    847 	struct devnametobdevmaj *nam2blk;
    848 	dev_t *devp;
    849 {
    850 	struct device *dv;
    851 	char *cp, c;
    852 	int majdev, part;
    853 #ifdef MEMORY_DISK_HOOKS
    854 	int i;
    855 #endif
    856 
    857 	if (len == 0)
    858 		return (NULL);
    859 
    860 	if (len == 4 && strcmp(str, "halt") == 0)
    861 		cpu_reboot(RB_HALT, NULL);
    862 
    863 	cp = str + len - 1;
    864 	c = *cp;
    865 	if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
    866 		part = c - 'a';
    867 		*cp = '\0';
    868 	} else
    869 		part = defpart;
    870 
    871 #ifdef MEMORY_DISK_HOOKS
    872 	for (i = 0; i < NMD; i++)
    873 		if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) {
    874 			dv = &fakemdrootdev[i];
    875 			goto gotdisk;
    876 		}
    877 #endif
    878 
    879 	for (dv = alldevs.tqh_first; dv != NULL; dv = dv->dv_list.tqe_next) {
    880 		if (dv->dv_class == DV_DISK &&
    881 		    strcmp(str, dv->dv_xname) == 0) {
    882 #ifdef MEMORY_DISK_HOOKS
    883  gotdisk:
    884 #endif
    885 			majdev = findblkmajor(dv->dv_xname, nam2blk);
    886 			if (majdev < 0)
    887 				panic("parsedisk");
    888 			*devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
    889 			break;
    890 		}
    891 
    892 		if (dv->dv_class == DV_IFNET &&
    893 		    strcmp(str, dv->dv_xname) == 0) {
    894 			*devp = NODEV;
    895 			break;
    896 		}
    897 	}
    898 
    899 	*cp = c;
    900 	return (dv);
    901 }
    902 
    903 /*
    904  * XXX shouldn't this be a common function?
    905  */
    906 static int
    907 getstr(cp, size)
    908 	char *cp;
    909 	int size;
    910 {
    911 	char *lp;
    912 	int c, len;
    913 
    914 	cnpollc(1);
    915 
    916 	lp = cp;
    917 	len = 0;
    918 	for (;;) {
    919 		c = cngetc();
    920 		switch (c) {
    921 		case '\n':
    922 		case '\r':
    923 			printf("\n");
    924 			*lp++ = '\0';
    925 			cnpollc(0);
    926 			return (len);
    927 		case '\b':
    928 		case '\177':
    929 		case '#':
    930 			if (len) {
    931 				--len;
    932 				--lp;
    933 				printf("\b \b");
    934 			}
    935 			continue;
    936 		case '@':
    937 		case 'u'&037:
    938 			len = 0;
    939 			lp = cp;
    940 			printf("\n");
    941 			continue;
    942 		default:
    943 			if (len + 1 >= size || c < ' ') {
    944 				printf("\007");
    945 				continue;
    946 			}
    947 			printf("%c", c);
    948 			++len;
    949 			*lp++ = c;
    950 		}
    951 	}
    952 }
    953 
    954 /*
    955  * snprintf() `bytes' into `buf', reformatting it so that the number,
    956  * plus a possible `x' + suffix extension) fits into len bytes (including
    957  * the terminating NUL).
    958  * Returns the number of bytes stored in buf, or -1 * if there was a problem.
    959  * E.g, given a len of 9 and a suffix of `B':
    960  *	bytes		result
    961  *	-----		------
    962  *	99999		`99999 B'
    963  *	100000		`97 KB'
    964  *	66715648	`65152 KB'
    965  *	252215296	`240 MB'
    966  */
    967 int
    968 humanize_number(buf, len, bytes, suffix)
    969 	char		*buf;
    970 	size_t		 len;
    971 	u_int64_t	 bytes;
    972 	const char	*suffix;
    973 {
    974 		/* prefixes are: (none), Kilo, Mega, Giga, Tera, Peta, Exa */
    975 	static const char prefixes[] = " KMGTPE";
    976 
    977 	int		i, r;
    978 	u_int64_t	max;
    979 	size_t		suffixlen;
    980 
    981 	if (buf == NULL || suffix == NULL)
    982 		return (-1);
    983 	if (len > 0)
    984 		buf[0] = '\0';
    985 	suffixlen = strlen(suffix);
    986 			/* check if enough room for `x y' + suffix + `\0' */
    987 	if (len < 4 + suffixlen)
    988 		return (-1);
    989 
    990 	max = 1;
    991 	for (i = 0; i < len - suffixlen - 3; i++)
    992 		max *= 10;
    993 	for (i = 0; bytes >= max && i < sizeof(prefixes); i++)
    994 		bytes /= 1024;
    995 
    996 	r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
    997 	    i == 0 ? "" : " ", prefixes[i], suffix);
    998 
    999 	return (r);
   1000 }
   1001 
   1002 int
   1003 format_bytes(buf, len, bytes)
   1004 	char		*buf;
   1005 	size_t		 len;
   1006 	u_int64_t	 bytes;
   1007 {
   1008 	int	rv;
   1009 	size_t	nlen;
   1010 
   1011 	rv = humanize_number(buf, len, bytes, "B");
   1012 	if (rv != -1) {
   1013 			/* nuke the trailing ` B' if it exists */
   1014 		nlen = strlen(buf) - 2;
   1015 		if (strcmp(&buf[nlen], " B") == 0)
   1016 			buf[nlen] = '\0';
   1017 	}
   1018 	return (rv);
   1019 }
   1020