kern_subr.c revision 1.54 1 /* $NetBSD: kern_subr.c,v 1.54 2000/01/25 03:42:36 enami 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 *));
111 static const char *findblkname __P((int));
112 static struct device *getdisk __P((char *, int, int, dev_t *, int));
113 static struct device *parsedisk __P((char *, int, int, 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 (uio->uio_rw == UIO_READ)
156 error = kcopy(cp, iov->iov_base, cnt);
157 else
158 error = kcopy(iov->iov_base, cp, cnt);
159 if (error)
160 return(error);
161 break;
162 }
163 iov->iov_base = (caddr_t)iov->iov_base + cnt;
164 iov->iov_len -= cnt;
165 uio->uio_resid -= cnt;
166 uio->uio_offset += cnt;
167 cp += cnt;
168 n -= cnt;
169 }
170 return (error);
171 }
172
173 /*
174 * Give next character to user as result of read.
175 */
176 int
177 ureadc(c, uio)
178 register int c;
179 register struct uio *uio;
180 {
181 register struct iovec *iov;
182
183 if (uio->uio_resid <= 0)
184 panic("ureadc: non-positive resid");
185 again:
186 if (uio->uio_iovcnt <= 0)
187 panic("ureadc: non-positive iovcnt");
188 iov = uio->uio_iov;
189 if (iov->iov_len <= 0) {
190 uio->uio_iovcnt--;
191 uio->uio_iov++;
192 goto again;
193 }
194 switch (uio->uio_segflg) {
195
196 case UIO_USERSPACE:
197 if (subyte(iov->iov_base, c) < 0)
198 return (EFAULT);
199 break;
200
201 case UIO_SYSSPACE:
202 *(char *)iov->iov_base = c;
203 break;
204 }
205 iov->iov_base = (caddr_t)iov->iov_base + 1;
206 iov->iov_len--;
207 uio->uio_resid--;
208 uio->uio_offset++;
209 return (0);
210 }
211
212 /*
213 * General routine to allocate a hash table.
214 * Allocate enough memory to hold at least `elements' list-head pointers.
215 * Return a pointer to the allocated space and set *hashmask to a pattern
216 * suitable for masking a value to use as an index into the returned array.
217 */
218 void *
219 hashinit(elements, type, flags, hashmask)
220 int elements, type, flags;
221 u_long *hashmask;
222 {
223 long hashsize;
224 LIST_HEAD(generic, generic) *hashtbl;
225 int i;
226
227 if (elements <= 0)
228 panic("hashinit: bad cnt");
229 for (hashsize = 1; hashsize < elements; hashsize <<= 1)
230 continue;
231 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, flags);
232 for (i = 0; i < hashsize; i++)
233 LIST_INIT(&hashtbl[i]);
234 *hashmask = hashsize - 1;
235 return (hashtbl);
236 }
237
238 /*
239 * "Shutdown hook" types, functions, and variables.
240 */
241
242 struct shutdownhook_desc {
243 LIST_ENTRY(shutdownhook_desc) sfd_list;
244 void (*sfd_fn) __P((void *));
245 void *sfd_arg;
246 };
247
248 LIST_HEAD(, shutdownhook_desc) shutdownhook_list;
249
250 void *
251 shutdownhook_establish(fn, arg)
252 void (*fn) __P((void *));
253 void *arg;
254 {
255 struct shutdownhook_desc *ndp;
256
257 ndp = (struct shutdownhook_desc *)
258 malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
259 if (ndp == NULL)
260 return NULL;
261
262 ndp->sfd_fn = fn;
263 ndp->sfd_arg = arg;
264 LIST_INSERT_HEAD(&shutdownhook_list, ndp, sfd_list);
265
266 return (ndp);
267 }
268
269 void
270 shutdownhook_disestablish(vhook)
271 void *vhook;
272 {
273 #ifdef DIAGNOSTIC
274 struct shutdownhook_desc *dp;
275
276 for (dp = shutdownhook_list.lh_first; dp != NULL;
277 dp = dp->sfd_list.le_next)
278 if (dp == vhook)
279 break;
280 if (dp == NULL)
281 panic("shutdownhook_disestablish: hook not established");
282 #endif
283
284 LIST_REMOVE((struct shutdownhook_desc *)vhook, sfd_list);
285 free(vhook, M_DEVBUF);
286 }
287
288 /*
289 * Run shutdown hooks. Should be invoked immediately before the
290 * system is halted or rebooted, i.e. after file systems unmounted,
291 * after crash dump done, etc.
292 *
293 * Each shutdown hook is removed from the list before it's run, so that
294 * it won't be run again.
295 */
296 void
297 doshutdownhooks()
298 {
299 struct shutdownhook_desc *dp;
300
301 while ((dp = shutdownhook_list.lh_first) != NULL) {
302 LIST_REMOVE(dp, sfd_list);
303 (*dp->sfd_fn)(dp->sfd_arg);
304 #if 0
305 /*
306 * Don't bother freeing the hook structure,, since we may
307 * be rebooting because of a memory corruption problem,
308 * and this might only make things worse. It doesn't
309 * matter, anyway, since the system is just about to
310 * reboot.
311 */
312 free(dp, M_DEVBUF);
313 #endif
314 }
315 }
316
317 /*
318 * "Power hook" types, functions, and variables.
319 */
320
321 struct powerhook_desc {
322 LIST_ENTRY(powerhook_desc) sfd_list;
323 void (*sfd_fn) __P((int, void *));
324 void *sfd_arg;
325 };
326
327 LIST_HEAD(, powerhook_desc) powerhook_list;
328
329 void *
330 powerhook_establish(fn, arg)
331 void (*fn) __P((int, void *));
332 void *arg;
333 {
334 struct powerhook_desc *ndp;
335
336 ndp = (struct powerhook_desc *)
337 malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
338 if (ndp == NULL)
339 return NULL;
340
341 ndp->sfd_fn = fn;
342 ndp->sfd_arg = arg;
343 LIST_INSERT_HEAD(&powerhook_list, ndp, sfd_list);
344
345 return (ndp);
346 }
347
348 void
349 powerhook_disestablish(vhook)
350 void *vhook;
351 {
352 #ifdef DIAGNOSTIC
353 struct powerhook_desc *dp;
354
355 for (dp = powerhook_list.lh_first; dp != NULL;
356 dp = dp->sfd_list.le_next)
357 if (dp == vhook)
358 break;
359 if (dp == NULL)
360 panic("powerhook_disestablish: hook not established");
361 #endif
362
363 LIST_REMOVE((struct powerhook_desc *)vhook, sfd_list);
364 free(vhook, M_DEVBUF);
365 }
366
367 /*
368 * Run power hooks.
369 */
370 void
371 dopowerhooks(why)
372 int why;
373 {
374 struct powerhook_desc *dp;
375
376 for (dp = LIST_FIRST(&powerhook_list);
377 dp != NULL;
378 dp = LIST_NEXT(dp, sfd_list)) {
379 (*dp->sfd_fn)(why, dp->sfd_arg);
380 }
381 }
382
383 /*
384 * "Mountroot hook" types, functions, and variables.
385 */
386
387 struct mountroothook_desc {
388 LIST_ENTRY(mountroothook_desc) mrd_list;
389 struct device *mrd_device;
390 void (*mrd_func) __P((struct device *));
391 };
392
393 LIST_HEAD(, mountroothook_desc) mountroothook_list;
394
395 void *
396 mountroothook_establish(func, dev)
397 void (*func) __P((struct device *));
398 struct device *dev;
399 {
400 struct mountroothook_desc *mrd;
401
402 mrd = (struct mountroothook_desc *)
403 malloc(sizeof(*mrd), M_DEVBUF, M_NOWAIT);
404 if (mrd == NULL)
405 return (NULL);
406
407 mrd->mrd_device = dev;
408 mrd->mrd_func = func;
409 LIST_INSERT_HEAD(&mountroothook_list, mrd, mrd_list);
410
411 return (mrd);
412 }
413
414 void
415 mountroothook_disestablish(vhook)
416 void *vhook;
417 {
418 #ifdef DIAGNOSTIC
419 struct mountroothook_desc *mrd;
420
421 for (mrd = mountroothook_list.lh_first; mrd != NULL;
422 mrd = mrd->mrd_list.le_next)
423 if (mrd == vhook)
424 break;
425 if (mrd == NULL)
426 panic("mountroothook_disestablish: hook not established");
427 #endif
428
429 LIST_REMOVE((struct mountroothook_desc *)vhook, mrd_list);
430 free(vhook, M_DEVBUF);
431 }
432
433 void
434 mountroothook_destroy()
435 {
436 struct mountroothook_desc *mrd;
437
438 while ((mrd = mountroothook_list.lh_first) != NULL) {
439 LIST_REMOVE(mrd, mrd_list);
440 free(mrd, M_DEVBUF);
441 }
442 }
443
444 void
445 domountroothook()
446 {
447 struct mountroothook_desc *mrd;
448
449 for (mrd = mountroothook_list.lh_first; mrd != NULL;
450 mrd = mrd->mrd_list.le_next) {
451 if (mrd->mrd_device == root_device) {
452 (*mrd->mrd_func)(root_device);
453 return;
454 }
455 }
456 }
457
458 /*
459 * Exec hook code.
460 */
461
462 struct exechook_desc {
463 LIST_ENTRY(exechook_desc) ehk_list;
464 void (*ehk_fn) __P((struct proc *, void *));
465 void *ehk_arg;
466 };
467
468 LIST_HEAD(, exechook_desc) exechook_list;
469
470 void *
471 exechook_establish(fn, arg)
472 void (*fn) __P((struct proc *, void *));
473 void *arg;
474 {
475 struct exechook_desc *edp;
476
477 edp = (struct exechook_desc *)
478 malloc(sizeof(*edp), M_DEVBUF, M_NOWAIT);
479 if (edp == NULL)
480 return NULL;
481
482 edp->ehk_fn = fn;
483 edp->ehk_arg = arg;
484 LIST_INSERT_HEAD(&exechook_list, edp, ehk_list);
485
486 return (edp);
487 }
488
489 void
490 exechook_disestablish(vhook)
491 void *vhook;
492 {
493 #ifdef DIAGNOSTIC
494 struct exechook_desc *edp;
495
496 for (edp = exechook_list.lh_first; edp != NULL;
497 edp = edp->ehk_list.le_next)
498 if (edp == vhook)
499 break;
500 if (edp == NULL)
501 panic("exechook_disestablish: hook not established");
502 #endif
503
504 LIST_REMOVE((struct exechook_desc *)vhook, ehk_list);
505 free(vhook, M_DEVBUF);
506 }
507
508 /*
509 * Run exec hooks.
510 */
511 void
512 doexechooks(p)
513 struct proc *p;
514 {
515 struct exechook_desc *edp;
516
517 for (edp = LIST_FIRST(&exechook_list);
518 edp != NULL;
519 edp = LIST_NEXT(edp, ehk_list)) {
520 (*edp->ehk_fn)(p, edp->ehk_arg);
521 }
522 }
523
524 /*
525 * Determine the root device and, if instructed to, the root file system.
526 */
527
528 #include "md.h"
529 #if NMD == 0
530 #undef MEMORY_DISK_HOOKS
531 #endif
532
533 #ifdef MEMORY_DISK_HOOKS
534 static struct device fakemdrootdev[NMD];
535 #endif
536
537 void
538 setroot(bootdv, bootpartition)
539 struct device *bootdv;
540 int bootpartition;
541 {
542 struct device *dv;
543 int len, print_newline = 0;
544 #ifdef MEMORY_DISK_HOOKS
545 int i;
546 #endif
547 dev_t nrootdev;
548 dev_t ndumpdev = NODEV;
549 char buf[128];
550 const char *rootdevname;
551 const char *dumpdevname;
552 struct device *rootdv = NULL; /* XXX gcc -Wuninitialized */
553 struct device *dumpdv = NULL;
554 struct ifnet *ifp;
555 const char *deffsname;
556 struct vfsops *vops;
557 extern int (*mountroot) __P((void));
558
559 #ifdef MEMORY_DISK_HOOKS
560 for (i = 0; i < NMD; i++) {
561 fakemdrootdev[i].dv_class = DV_DISK;
562 fakemdrootdev[i].dv_cfdata = NULL;
563 fakemdrootdev[i].dv_unit = i;
564 fakemdrootdev[i].dv_parent = NULL;
565 sprintf(fakemdrootdev[i].dv_xname, "md%d", i);
566 }
567 #endif /* MEMORY_DISK_HOOKS */
568
569 #ifdef MEMORY_DISK_IS_ROOT
570 bootdv = &fakemdrootdev[0];
571 bootpartition = 0;
572 #endif
573
574 /*
575 * If NFS is specified as the file system, and we found
576 * a DV_DISK boot device (or no boot device at all), then
577 * find a reasonable network interface for "rootspec".
578 */
579 vops = vfs_getopsbyname("nfs");
580 if (vops != NULL && vops->vfs_mountroot == mountroot &&
581 rootspec == NULL &&
582 (bootdv == NULL || bootdv->dv_class != DV_IFNET)) {
583 for (ifp = ifnet.tqh_first; ifp != NULL;
584 ifp = ifp->if_list.tqe_next)
585 if ((ifp->if_flags &
586 (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
587 break;
588 if (ifp == NULL) {
589 /*
590 * Can't find a suitable interface; ask the
591 * user.
592 */
593 boothowto |= RB_ASKNAME;
594 } else {
595 /*
596 * Have a suitable interface; behave as if
597 * the user specified this interface.
598 */
599 rootspec = (const char *)ifp->if_xname;
600 }
601 }
602
603 /*
604 * If wildcarded root and we the boot device wasn't determined,
605 * ask the user.
606 */
607 if (rootspec == NULL && bootdv == NULL)
608 boothowto |= RB_ASKNAME;
609
610 top:
611 if (boothowto & RB_ASKNAME) {
612 struct device *defdumpdv;
613
614 for (;;) {
615 printf("root device");
616 if (bootdv != NULL) {
617 printf(" (default %s", bootdv->dv_xname);
618 if (bootdv->dv_class == DV_DISK)
619 printf("%c", bootpartition + 'a');
620 printf(")");
621 }
622 printf(": ");
623 len = getstr(buf, sizeof(buf));
624 if (len == 0 && bootdv != NULL) {
625 strcpy(buf, bootdv->dv_xname);
626 len = strlen(buf);
627 }
628 if (len > 0 && buf[len - 1] == '*') {
629 buf[--len] = '\0';
630 dv = getdisk(buf, len, 1, &nrootdev, 0);
631 if (dv != NULL) {
632 rootdv = dv;
633 break;
634 }
635 }
636 dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
637 if (dv != NULL) {
638 rootdv = dv;
639 break;
640 }
641 }
642
643 /*
644 * Set up the default dump device. If root is on
645 * a network device, there is no default dump
646 * device, since we don't support dumps to the
647 * network.
648 */
649 if (rootdv->dv_class == DV_IFNET)
650 defdumpdv = NULL;
651 else
652 defdumpdv = rootdv;
653
654 for (;;) {
655 printf("dump device");
656 if (defdumpdv != NULL) {
657 /*
658 * Note, we know it's a disk if we get here.
659 */
660 printf(" (default %sb)", defdumpdv->dv_xname);
661 }
662 printf(": ");
663 len = getstr(buf, sizeof(buf));
664 if (len == 0) {
665 if (defdumpdv != NULL) {
666 ndumpdev = MAKEDISKDEV(major(nrootdev),
667 DISKUNIT(nrootdev), 1);
668 }
669 dumpdv = defdumpdv;
670 break;
671 }
672 if (len == 4 && strcmp(buf, "none") == 0) {
673 dumpspec = "none";
674 break;
675 }
676 dv = getdisk(buf, len, 1, &ndumpdev, 1);
677 if (dv) {
678 dumpdv = dv;
679 break;
680 }
681 }
682
683 rootdev = nrootdev;
684 dumpdev = ndumpdev;
685
686 for (vops = LIST_FIRST(&vfs_list); vops != NULL;
687 vops = LIST_NEXT(vops, vfs_list)) {
688 if (vops->vfs_mountroot != NULL &&
689 vops->vfs_mountroot == mountroot)
690 break;
691 }
692
693 if (vops == NULL) {
694 mountroot = NULL;
695 deffsname = "generic";
696 } else
697 deffsname = vops->vfs_name;
698
699 for (;;) {
700 printf("file system (default %s): ", deffsname);
701 len = getstr(buf, sizeof(buf));
702 if (len == 0)
703 break;
704 if (len == 4 && strcmp(buf, "halt") == 0)
705 cpu_reboot(RB_HALT, NULL);
706 else if (len == 7 && strcmp(buf, "generic") == 0) {
707 mountroot = NULL;
708 break;
709 }
710 vops = vfs_getopsbyname(buf);
711 if (vops == NULL || vops->vfs_mountroot == NULL) {
712 printf("use one of: generic");
713 for (vops = LIST_FIRST(&vfs_list);
714 vops != NULL;
715 vops = LIST_NEXT(vops, vfs_list)) {
716 if (vops->vfs_mountroot != NULL)
717 printf(" %s", vops->vfs_name);
718 }
719 printf(" halt\n");
720 } else {
721 mountroot = vops->vfs_mountroot;
722 break;
723 }
724 }
725
726 } else if (rootspec == NULL) {
727 int majdev;
728
729 /*
730 * Wildcarded root; use the boot device.
731 */
732 rootdv = bootdv;
733
734 majdev = findblkmajor(bootdv->dv_xname);
735 if (majdev >= 0) {
736 /*
737 * Root is on a disk. `bootpartition' is root.
738 */
739 rootdev = MAKEDISKDEV(majdev, bootdv->dv_unit,
740 bootpartition);
741 }
742 } else {
743
744 /*
745 * `root on <dev> ...'
746 */
747
748 /*
749 * If it's a network interface, we can bail out
750 * early.
751 */
752 for (dv = alldevs.tqh_first; dv != NULL;
753 dv = dv->dv_list.tqe_next)
754 if (strcmp(dv->dv_xname, rootspec) == 0)
755 break;
756 if (dv != NULL && dv->dv_class == DV_IFNET) {
757 rootdv = dv;
758 goto haveroot;
759 }
760
761 rootdevname = findblkname(major(rootdev));
762 if (rootdevname == NULL) {
763 printf("unknown device major 0x%x\n", rootdev);
764 boothowto |= RB_ASKNAME;
765 goto top;
766 }
767 memset(buf, 0, sizeof(buf));
768 sprintf(buf, "%s%d", rootdevname, DISKUNIT(rootdev));
769
770 for (dv = alldevs.tqh_first; dv != NULL;
771 dv = dv->dv_list.tqe_next) {
772 if (strcmp(buf, dv->dv_xname) == 0) {
773 rootdv = dv;
774 break;
775 }
776 }
777 if (rootdv == NULL) {
778 printf("device %s (0x%x) not configured\n",
779 buf, rootdev);
780 boothowto |= RB_ASKNAME;
781 goto top;
782 }
783 }
784
785 haveroot:
786
787 root_device = rootdv;
788
789 switch (rootdv->dv_class) {
790 case DV_IFNET:
791 /* Nothing. */
792 break;
793
794 case DV_DISK:
795 printf("root on %s%c", rootdv->dv_xname,
796 DISKPART(rootdev) + 'a');
797 print_newline = 1;
798 break;
799
800 default:
801 printf("can't determine root device\n");
802 boothowto |= RB_ASKNAME;
803 goto top;
804 }
805
806 /*
807 * Now configure the dump device.
808 */
809
810 if (dumpspec != NULL && strcmp(dumpspec, "none") == 0) {
811 /*
812 * Operator doesn't want a dump device.
813 */
814 goto nodumpdev;
815 }
816
817 /*
818 * If we haven't figured out the dump device, do so, with
819 * the following rules:
820 *
821 * (a) We already know dumpdv in the RB_ASKNAME case.
822 *
823 * (b) If dumpspec is set, try to use it. If the device
824 * is not available, punt.
825 *
826 * (c) If dumpspec is not set, the dump device is
827 * wildcarded or unspecified. If the root device
828 * is DV_IFNET, punt. Otherwise, use partition b
829 * of the root device.
830 */
831
832 if (boothowto & RB_ASKNAME) {
833 if (dumpdv == NULL) {
834 /*
835 * Just return; dumpdev is already set to NODEV
836 * and we don't want to print a newline in this
837 * case.
838 */
839 return;
840 }
841 goto out;
842 }
843
844 if (dumpspec != NULL) {
845 if (dumpdev == NODEV) {
846 /*
847 * Looks like they tried to pick a network
848 * device. Oops.
849 */
850 goto nodumpdev;
851 }
852
853 dumpdevname = findblkname(major(dumpdev));
854 if (dumpdevname == NULL)
855 goto nodumpdev;
856 memset(buf, 0, sizeof(buf));
857 sprintf(buf, "%s%d", dumpdevname, DISKUNIT(dumpdev));
858
859 for (dv = alldevs.tqh_first; dv != NULL;
860 dv = dv->dv_list.tqe_next) {
861 if (strcmp(buf, dv->dv_xname) == 0) {
862 dumpdv = dv;
863 break;
864 }
865 }
866 if (dv == NULL) {
867 /*
868 * Device not configured.
869 */
870 goto nodumpdev;
871 }
872 } else if (rootdv->dv_class == DV_IFNET)
873 goto nodumpdev;
874 else {
875 dumpdv = rootdv;
876 dumpdev = MAKEDISKDEV(major(rootdev), dumpdv->dv_unit, 1);
877 }
878
879 out:
880 printf(" dumps on %s%c\n", dumpdv->dv_xname, DISKPART(dumpdev) + 'a');
881 return;
882
883 nodumpdev:
884 dumpdev = NODEV;
885 if (print_newline)
886 printf("\n");
887 }
888
889 static int
890 findblkmajor(name)
891 const char *name;
892 {
893 int i;
894
895 for (i = 0; dev_name2blk[i].d_name != NULL; i++)
896 if (strncmp(name, dev_name2blk[i].d_name,
897 strlen(dev_name2blk[i].d_name)) == 0)
898 return (dev_name2blk[i].d_maj);
899 return (-1);
900 }
901
902 const char *
903 findblkname(maj)
904 int maj;
905 {
906 int i;
907
908 for (i = 0; dev_name2blk[i].d_name != NULL; i++)
909 if (dev_name2blk[i].d_maj == maj)
910 return (dev_name2blk[i].d_name);
911 return (NULL);
912 }
913
914 static struct device *
915 getdisk(str, len, defpart, devp, isdump)
916 char *str;
917 int len, defpart;
918 dev_t *devp;
919 int isdump;
920 {
921 struct device *dv;
922 #ifdef MEMORY_DISK_HOOKS
923 int i;
924 #endif
925
926 if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
927 printf("use one of:");
928 #ifdef MEMORY_DISK_HOOKS
929 if (isdump == 0)
930 for (i = 0; i < NMD; i++)
931 printf(" %s[a-%c]", fakemdrootdev[i].dv_xname,
932 'a' + MAXPARTITIONS - 1);
933 #endif
934 for (dv = alldevs.tqh_first; dv != NULL;
935 dv = dv->dv_list.tqe_next) {
936 if (dv->dv_class == DV_DISK)
937 printf(" %s[a-%c]", dv->dv_xname,
938 'a' + MAXPARTITIONS - 1);
939 if (isdump == 0 && dv->dv_class == DV_IFNET)
940 printf(" %s", dv->dv_xname);
941 }
942 if (isdump)
943 printf(" none");
944 printf(" halt\n");
945 }
946 return (dv);
947 }
948
949 static struct device *
950 parsedisk(str, len, defpart, devp)
951 char *str;
952 int len, defpart;
953 dev_t *devp;
954 {
955 struct device *dv;
956 char *cp, c;
957 int majdev, part;
958 #ifdef MEMORY_DISK_HOOKS
959 int i;
960 #endif
961
962 if (len == 0)
963 return (NULL);
964
965 if (len == 4 && strcmp(str, "halt") == 0)
966 cpu_reboot(RB_HALT, NULL);
967
968 cp = str + len - 1;
969 c = *cp;
970 if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
971 part = c - 'a';
972 *cp = '\0';
973 } else
974 part = defpart;
975
976 #ifdef MEMORY_DISK_HOOKS
977 for (i = 0; i < NMD; i++)
978 if (strcmp(str, fakemdrootdev[i].dv_xname) == 0) {
979 dv = &fakemdrootdev[i];
980 goto gotdisk;
981 }
982 #endif
983
984 for (dv = alldevs.tqh_first; dv != NULL; dv = dv->dv_list.tqe_next) {
985 if (dv->dv_class == DV_DISK &&
986 strcmp(str, dv->dv_xname) == 0) {
987 #ifdef MEMORY_DISK_HOOKS
988 gotdisk:
989 #endif
990 majdev = findblkmajor(dv->dv_xname);
991 if (majdev < 0)
992 panic("parsedisk");
993 *devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
994 break;
995 }
996
997 if (dv->dv_class == DV_IFNET &&
998 strcmp(str, dv->dv_xname) == 0) {
999 *devp = NODEV;
1000 break;
1001 }
1002 }
1003
1004 *cp = c;
1005 return (dv);
1006 }
1007
1008 /*
1009 * XXX shouldn't this be a common function?
1010 */
1011 static int
1012 getstr(cp, size)
1013 char *cp;
1014 int size;
1015 {
1016 char *lp;
1017 int c, len;
1018
1019 cnpollc(1);
1020
1021 lp = cp;
1022 len = 0;
1023 for (;;) {
1024 c = cngetc();
1025 switch (c) {
1026 case '\n':
1027 case '\r':
1028 printf("\n");
1029 *lp++ = '\0';
1030 cnpollc(0);
1031 return (len);
1032 case '\b':
1033 case '\177':
1034 case '#':
1035 if (len) {
1036 --len;
1037 --lp;
1038 printf("\b \b");
1039 }
1040 continue;
1041 case '@':
1042 case 'u'&037:
1043 len = 0;
1044 lp = cp;
1045 printf("\n");
1046 continue;
1047 default:
1048 if (len + 1 >= size || c < ' ') {
1049 printf("\007");
1050 continue;
1051 }
1052 printf("%c", c);
1053 ++len;
1054 *lp++ = c;
1055 }
1056 }
1057 }
1058
1059 /*
1060 * snprintf() `bytes' into `buf', reformatting it so that the number,
1061 * plus a possible `x' + suffix extension) fits into len bytes (including
1062 * the terminating NUL).
1063 * Returns the number of bytes stored in buf, or -1 * if there was a problem.
1064 * E.g, given a len of 9 and a suffix of `B':
1065 * bytes result
1066 * ----- ------
1067 * 99999 `99999 B'
1068 * 100000 `97 KB'
1069 * 66715648 `65152 KB'
1070 * 252215296 `240 MB'
1071 */
1072 int
1073 humanize_number(buf, len, bytes, suffix)
1074 char *buf;
1075 size_t len;
1076 u_int64_t bytes;
1077 const char *suffix;
1078 {
1079 /* prefixes are: (none), Kilo, Mega, Giga, Tera, Peta, Exa */
1080 static const char prefixes[] = " KMGTPE";
1081
1082 int i, r;
1083 u_int64_t max;
1084 size_t suffixlen;
1085
1086 if (buf == NULL || suffix == NULL)
1087 return (-1);
1088 if (len > 0)
1089 buf[0] = '\0';
1090 suffixlen = strlen(suffix);
1091 /* check if enough room for `x y' + suffix + `\0' */
1092 if (len < 4 + suffixlen)
1093 return (-1);
1094
1095 max = 1;
1096 for (i = 0; i < len - suffixlen - 3; i++)
1097 max *= 10;
1098 for (i = 0; bytes >= max && i < sizeof(prefixes); i++)
1099 bytes /= 1024;
1100
1101 r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
1102 i == 0 ? "" : " ", prefixes[i], suffix);
1103
1104 return (r);
1105 }
1106
1107 int
1108 format_bytes(buf, len, bytes)
1109 char *buf;
1110 size_t len;
1111 u_int64_t bytes;
1112 {
1113 int rv;
1114 size_t nlen;
1115
1116 rv = humanize_number(buf, len, bytes, "B");
1117 if (rv != -1) {
1118 /* nuke the trailing ` B' if it exists */
1119 nlen = strlen(buf) - 2;
1120 if (strcmp(&buf[nlen], " B") == 0)
1121 buf[nlen] = '\0';
1122 }
1123 return (rv);
1124 }
1125