kern_subr.c revision 1.218 1 /* $NetBSD: kern_subr.c,v 1.218 2017/11/09 01:02:55 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
41 *
42 * Copyright (c) 1992, 1993
43 * The Regents of the University of California. All rights reserved.
44 *
45 * This software was developed by the Computer Systems Engineering group
46 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
47 * contributed to Berkeley.
48 *
49 * All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Lawrence Berkeley Laboratory.
53 *
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
56 * are met:
57 * 1. Redistributions of source code must retain the above copyright
58 * notice, this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright
60 * notice, this list of conditions and the following disclaimer in the
61 * documentation and/or other materials provided with the distribution.
62 * 3. Neither the name of the University nor the names of its contributors
63 * may be used to endorse or promote products derived from this software
64 * without specific prior written permission.
65 *
66 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 * SUCH DAMAGE.
77 *
78 * @(#)kern_subr.c 8.4 (Berkeley) 2/14/95
79 */
80
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.218 2017/11/09 01:02:55 christos Exp $");
83
84 #include "opt_ddb.h"
85 #include "opt_md.h"
86 #include "opt_tftproot.h"
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/proc.h>
91 #include <sys/mount.h>
92 #include <sys/device.h>
93 #include <sys/reboot.h>
94 #include <sys/conf.h>
95 #include <sys/disk.h>
96 #include <sys/disklabel.h>
97 #include <sys/queue.h>
98 #include <sys/fcntl.h>
99 #include <sys/kauth.h>
100 #include <sys/stat.h>
101 #include <sys/vnode.h>
102 #include <sys/module.h>
103
104 #include <dev/cons.h>
105
106 #include <net/if.h>
107
108 /* XXX these should eventually move to subr_autoconf.c */
109 static device_t finddevice(const char *);
110 static device_t getdisk(char *, int, int, dev_t *, int);
111 static device_t parsedisk(char *, int, int, dev_t *);
112 static const char *getwedgename(const char *, int);
113
114 #ifdef TFTPROOT
115 int tftproot_dhcpboot(device_t);
116 #endif
117
118 dev_t dumpcdev; /* for savecore */
119
120 static int
121 isswap(device_t dv)
122 {
123 struct dkwedge_info wi;
124 struct vnode *vn;
125 int error;
126
127 if (device_class(dv) != DV_DISK || !device_is_a(dv, "dk"))
128 return 0;
129
130 if ((vn = opendisk(dv)) == NULL)
131 return 0;
132
133 error = VOP_IOCTL(vn, DIOCGWEDGEINFO, &wi, FREAD, NOCRED);
134 VOP_CLOSE(vn, FREAD, NOCRED);
135 vput(vn);
136 if (error) {
137 #ifdef DEBUG_WEDGE
138 printf("%s: Get wedge info returned %d\n", device_xname(dv), error);
139 #endif
140 return 0;
141 }
142 return strcmp(wi.dkw_ptype, DKW_PTYPE_SWAP) == 0;
143 }
144
145 /*
146 * Determine the root device and, if instructed to, the root file system.
147 */
148
149 #ifdef MEMORY_DISK_IS_ROOT
150 int md_is_root = 1;
151 #else
152 int md_is_root = 0;
153 #endif
154
155 /*
156 * The device and partition that we booted from.
157 */
158 device_t booted_device;
159 const char *booted_method;
160 int booted_partition;
161 daddr_t booted_startblk;
162 uint64_t booted_nblks;
163 char *bootspec;
164
165 /*
166 * Use partition letters if it's a disk class but not a wedge.
167 * XXX Check for wedge is kinda gross.
168 */
169 #define DEV_USES_PARTITIONS(dv) \
170 (device_class((dv)) == DV_DISK && \
171 !device_is_a((dv), "dk"))
172
173 void
174 setroot(device_t bootdv, int bootpartition)
175 {
176 device_t dv;
177 deviter_t di;
178 int len, majdev;
179 dev_t nrootdev;
180 dev_t ndumpdev = NODEV;
181 char buf[128];
182 const char *rootdevname;
183 const char *dumpdevname;
184 device_t rootdv = NULL; /* XXX gcc -Wuninitialized */
185 device_t dumpdv = NULL;
186 struct ifnet *ifp;
187 const char *deffsname;
188 struct vfsops *vops;
189
190 #ifdef TFTPROOT
191 if (tftproot_dhcpboot(bootdv) != 0)
192 boothowto |= RB_ASKNAME;
193 #endif
194
195 /*
196 * For root on md0 we have to force the attachment of md0.
197 */
198 if (md_is_root) {
199 int md_major;
200 dev_t md_dev;
201
202 bootdv = NULL;
203 md_major = devsw_name2blk("md", NULL, 0);
204 if (md_major >= 0) {
205 md_dev = MAKEDISKDEV(md_major, 0, RAW_PART);
206 if (bdev_open(md_dev, FREAD, S_IFBLK, curlwp) == 0)
207 bootdv = device_find_by_xname("md0");
208 }
209 if (bootdv == NULL)
210 panic("Cannot open \"md0\" (root)");
211 }
212
213 /*
214 * Let bootcode augment "rootspec".
215 */
216 if (rootspec == NULL)
217 rootspec = bootspec;
218
219 /*
220 * If NFS is specified as the file system, and we found
221 * a DV_DISK boot device (or no boot device at all), then
222 * find a reasonable network interface for "rootspec".
223 */
224 vops = vfs_getopsbyname(MOUNT_NFS);
225 if (vops != NULL && strcmp(rootfstype, MOUNT_NFS) == 0 &&
226 rootspec == NULL &&
227 (bootdv == NULL || device_class(bootdv) != DV_IFNET)) {
228 int s = pserialize_read_enter();
229 IFNET_READER_FOREACH(ifp) {
230 if ((ifp->if_flags &
231 (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
232 break;
233 }
234 if (ifp == NULL) {
235 /*
236 * Can't find a suitable interface; ask the
237 * user.
238 */
239 boothowto |= RB_ASKNAME;
240 } else {
241 /*
242 * Have a suitable interface; behave as if
243 * the user specified this interface.
244 */
245 rootspec = (const char *)ifp->if_xname;
246 }
247 pserialize_read_exit(s);
248 }
249 if (vops != NULL)
250 vfs_delref(vops);
251
252 /*
253 * If wildcarded root and we the boot device wasn't determined,
254 * ask the user.
255 */
256 if (rootspec == NULL && bootdv == NULL)
257 boothowto |= RB_ASKNAME;
258
259 top:
260 if (boothowto & RB_ASKNAME) {
261 device_t defdumpdv;
262
263 for (;;) {
264 printf("root device");
265 if (bootdv != NULL) {
266 printf(" (default %s", device_xname(bootdv));
267 if (DEV_USES_PARTITIONS(bootdv))
268 printf("%c", bootpartition + 'a');
269 printf(")");
270 }
271 printf(": ");
272 len = cngetsn(buf, sizeof(buf));
273 if (len == 0 && bootdv != NULL) {
274 strlcpy(buf, device_xname(bootdv), sizeof(buf));
275 len = strlen(buf);
276 }
277 if (len > 0 && buf[len - 1] == '*') {
278 buf[--len] = '\0';
279 dv = getdisk(buf, len, 1, &nrootdev, 0);
280 if (dv != NULL) {
281 rootdv = dv;
282 break;
283 }
284 }
285 dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
286 if (dv != NULL) {
287 rootdv = dv;
288 break;
289 }
290 }
291
292 /*
293 * Set up the default dump device. If root is on
294 * a network device, there is no default dump
295 * device, since we don't support dumps to the
296 * network.
297 */
298 if (DEV_USES_PARTITIONS(rootdv) == 0)
299 defdumpdv = NULL;
300 else
301 defdumpdv = rootdv;
302
303 for (;;) {
304 printf("dump device");
305 if (defdumpdv != NULL) {
306 /*
307 * Note, we know it's a disk if we get here.
308 */
309 printf(" (default %sb)", device_xname(defdumpdv));
310 }
311 printf(": ");
312 len = cngetsn(buf, sizeof(buf));
313 if (len == 0) {
314 if (defdumpdv != NULL) {
315 ndumpdev = MAKEDISKDEV(major(nrootdev),
316 DISKUNIT(nrootdev), 1);
317 }
318 dumpdv = defdumpdv;
319 break;
320 }
321 if (len == 4 && strcmp(buf, "none") == 0) {
322 dumpdv = NULL;
323 break;
324 }
325 dv = getdisk(buf, len, 1, &ndumpdev, 1);
326 if (dv != NULL) {
327 dumpdv = dv;
328 break;
329 }
330 }
331
332 rootdev = nrootdev;
333 dumpdev = ndumpdev;
334
335 for (vops = LIST_FIRST(&vfs_list); vops != NULL;
336 vops = LIST_NEXT(vops, vfs_list)) {
337 if (vops->vfs_mountroot != NULL &&
338 strcmp(rootfstype, vops->vfs_name) == 0)
339 break;
340 }
341
342 if (vops == NULL) {
343 deffsname = "generic";
344 } else
345 deffsname = vops->vfs_name;
346
347 for (;;) {
348 printf("file system (default %s): ", deffsname);
349 len = cngetsn(buf, sizeof(buf));
350 if (len == 0) {
351 if (strcmp(deffsname, "generic") == 0)
352 rootfstype = ROOT_FSTYPE_ANY;
353 break;
354 }
355 if (len == 4 && strcmp(buf, "halt") == 0)
356 cpu_reboot(RB_HALT, NULL);
357 else if (len == 6 && strcmp(buf, "reboot") == 0)
358 cpu_reboot(0, NULL);
359 #if defined(DDB)
360 else if (len == 3 && strcmp(buf, "ddb") == 0) {
361 console_debugger();
362 }
363 #endif
364 else if (len == 7 && strcmp(buf, "generic") == 0) {
365 rootfstype = ROOT_FSTYPE_ANY;
366 break;
367 }
368 vops = vfs_getopsbyname(buf);
369 if (vops == NULL || vops->vfs_mountroot == NULL) {
370 printf("use one of: generic");
371 for (vops = LIST_FIRST(&vfs_list);
372 vops != NULL;
373 vops = LIST_NEXT(vops, vfs_list)) {
374 if (vops->vfs_mountroot != NULL)
375 printf(" %s", vops->vfs_name);
376 }
377 if (vops != NULL)
378 vfs_delref(vops);
379 #if defined(DDB)
380 printf(" ddb");
381 #endif
382 printf(" halt reboot\n");
383 } else {
384 /*
385 * XXX If *vops gets freed between here and
386 * the call to mountroot(), rootfstype will
387 * point to something unexpected. But in
388 * this case the system will fail anyway.
389 */
390 rootfstype = vops->vfs_name;
391 vfs_delref(vops);
392 break;
393 }
394 }
395
396 } else if (rootspec == NULL) {
397 /*
398 * Wildcarded root; use the boot device.
399 */
400 rootdv = bootdv;
401
402 if (bootdv)
403 majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
404 else
405 majdev = -1;
406 if (majdev >= 0) {
407 /*
408 * Root is on a disk. `bootpartition' is root,
409 * unless the device does not use partitions.
410 */
411 if (DEV_USES_PARTITIONS(bootdv))
412 rootdev = MAKEDISKDEV(majdev,
413 device_unit(bootdv),
414 bootpartition);
415 else
416 rootdev = makedev(majdev, device_unit(bootdv));
417 }
418 } else {
419
420 /*
421 * `root on <dev> ...'
422 */
423
424 /*
425 * If it's a network interface, we can bail out
426 * early.
427 */
428 dv = finddevice(rootspec);
429 if (dv != NULL && device_class(dv) == DV_IFNET) {
430 rootdv = dv;
431 goto haveroot;
432 }
433
434 if (rootdev == NODEV &&
435 dv != NULL && device_class(dv) == DV_DISK &&
436 device_is_a(dv, "dk") &&
437 (majdev = devsw_name2blk(device_xname(dv), NULL, 0)) >= 0)
438 rootdev = makedev(majdev, device_unit(dv));
439
440 rootdevname = devsw_blk2name(major(rootdev));
441 if (rootdevname == NULL) {
442 printf("unknown device major 0x%llx\n",
443 (unsigned long long)rootdev);
444 boothowto |= RB_ASKNAME;
445 goto top;
446 }
447 memset(buf, 0, sizeof(buf));
448 snprintf(buf, sizeof(buf), "%s%llu", rootdevname,
449 (unsigned long long)DISKUNIT(rootdev));
450
451 rootdv = finddevice(buf);
452 if (rootdv == NULL) {
453 printf("device %s (0x%llx) not configured\n",
454 buf, (unsigned long long)rootdev);
455 boothowto |= RB_ASKNAME;
456 goto top;
457 }
458 }
459
460 haveroot:
461
462 root_device = rootdv;
463
464 switch (device_class(rootdv)) {
465 case DV_IFNET:
466 case DV_DISK:
467 aprint_normal("root on %s", device_xname(rootdv));
468 if (DEV_USES_PARTITIONS(rootdv))
469 aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
470 break;
471
472 default:
473 printf("can't determine root device\n");
474 boothowto |= RB_ASKNAME;
475 goto top;
476 }
477
478 /*
479 * Now configure the dump device.
480 *
481 * If we haven't figured out the dump device, do so, with
482 * the following rules:
483 *
484 * (a) We already know dumpdv in the RB_ASKNAME case.
485 *
486 * (b) If dumpspec is set, try to use it. If the device
487 * is not available, punt.
488 *
489 * (c) If dumpspec is not set, the dump device is
490 * wildcarded or unspecified. If the root device
491 * is DV_IFNET, punt. Otherwise, use partition b
492 * of the root device.
493 */
494
495 if (boothowto & RB_ASKNAME) { /* (a) */
496 if (dumpdv == NULL)
497 goto nodumpdev;
498 } else if (dumpspec != NULL) { /* (b) */
499 if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
500 /*
501 * Operator doesn't want a dump device.
502 * Or looks like they tried to pick a network
503 * device. Oops.
504 */
505 goto nodumpdev;
506 }
507
508 dumpdevname = devsw_blk2name(major(dumpdev));
509 if (dumpdevname == NULL)
510 goto nodumpdev;
511 memset(buf, 0, sizeof(buf));
512 snprintf(buf, sizeof(buf), "%s%llu", dumpdevname,
513 (unsigned long long)DISKUNIT(dumpdev));
514
515 dumpdv = finddevice(buf);
516 if (dumpdv == NULL) {
517 /*
518 * Device not configured.
519 */
520 goto nodumpdev;
521 }
522 } else { /* (c) */
523 if (DEV_USES_PARTITIONS(rootdv) == 0) {
524 for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
525 dv != NULL;
526 dv = deviter_next(&di))
527 if (isswap(dv))
528 break;
529 deviter_release(&di);
530 if (dv == NULL)
531 goto nodumpdev;
532
533 majdev = devsw_name2blk(device_xname(dv), NULL, 0);
534 if (majdev < 0)
535 goto nodumpdev;
536 dumpdv = dv;
537 dumpdev = makedev(majdev, device_unit(dumpdv));
538 } else {
539 dumpdv = rootdv;
540 dumpdev = MAKEDISKDEV(major(rootdev),
541 device_unit(dumpdv), 1);
542 }
543 }
544
545 dumpcdev = devsw_blk2chr(dumpdev);
546 aprint_normal(" dumps on %s", device_xname(dumpdv));
547 if (DEV_USES_PARTITIONS(dumpdv))
548 aprint_normal("%c", (int)DISKPART(dumpdev) + 'a');
549 aprint_normal("\n");
550 return;
551
552 nodumpdev:
553 dumpdev = NODEV;
554 dumpcdev = NODEV;
555 aprint_normal("\n");
556 }
557
558 static device_t
559 finddevice(const char *name)
560 {
561 const char *wname;
562
563 if ((wname = getwedgename(name, strlen(name))) != NULL)
564 return dkwedge_find_by_wname(wname);
565
566 return device_find_by_xname(name);
567 }
568
569 static device_t
570 getdisk(char *str, int len, int defpart, dev_t *devp, int isdump)
571 {
572 device_t dv;
573 deviter_t di;
574
575 if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
576 printf("use one of:");
577 for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
578 dv = deviter_next(&di)) {
579 if (DEV_USES_PARTITIONS(dv))
580 printf(" %s[a-%c]", device_xname(dv),
581 'a' + MAXPARTITIONS - 1);
582 else if (device_class(dv) == DV_DISK)
583 printf(" %s", device_xname(dv));
584 if (isdump == 0 && device_class(dv) == DV_IFNET)
585 printf(" %s", device_xname(dv));
586 }
587 deviter_release(&di);
588 dkwedge_print_wnames();
589 if (isdump)
590 printf(" none");
591 #if defined(DDB)
592 printf(" ddb");
593 #endif
594 printf(" halt reboot\n");
595 }
596 return dv;
597 }
598
599 static const char *
600 getwedgename(const char *name, int namelen)
601 {
602 const char *wpfx = "wedge:";
603 const int wpfxlen = strlen(wpfx);
604
605 if (namelen < wpfxlen || strncmp(name, wpfx, wpfxlen) != 0)
606 return NULL;
607
608 return name + wpfxlen;
609 }
610
611 static device_t
612 parsedisk(char *str, int len, int defpart, dev_t *devp)
613 {
614 device_t dv;
615 const char *wname;
616 char *cp, c;
617 int majdev, part;
618 if (len == 0)
619 return (NULL);
620
621 if (len == 4 && strcmp(str, "halt") == 0)
622 cpu_reboot(RB_HALT, NULL);
623 else if (len == 6 && strcmp(str, "reboot") == 0)
624 cpu_reboot(0, NULL);
625 #if defined(DDB)
626 else if (len == 3 && strcmp(str, "ddb") == 0)
627 console_debugger();
628 #endif
629
630 cp = str + len - 1;
631 c = *cp;
632
633 if ((wname = getwedgename(str, len)) != NULL) {
634 if ((dv = dkwedge_find_by_wname(wname)) == NULL)
635 return NULL;
636 part = defpart;
637 goto gotdisk;
638 } else if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
639 part = c - 'a';
640 *cp = '\0';
641 } else
642 part = defpart;
643
644 dv = finddevice(str);
645 if (dv != NULL) {
646 if (device_class(dv) == DV_DISK) {
647 gotdisk:
648 majdev = devsw_name2blk(device_xname(dv), NULL, 0);
649 if (majdev < 0)
650 panic("parsedisk");
651 if (DEV_USES_PARTITIONS(dv))
652 *devp = MAKEDISKDEV(majdev, device_unit(dv),
653 part);
654 else
655 *devp = makedev(majdev, device_unit(dv));
656 }
657
658 if (device_class(dv) == DV_IFNET)
659 *devp = NODEV;
660 }
661
662 *cp = c;
663 return (dv);
664 }
665