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