1 /* $NetBSD: kern_subr.c,v 1.220 2018/10/07 11:24:16 mlelstv 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.220 2018/10/07 11:24:16 mlelstv 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 or flash. 167 * XXX Check for wedge/flash is kinda gross. 168 */ 169 #define DEV_USES_PARTITIONS(dv) \ 170 (device_class((dv)) == DV_DISK && \ 171 !device_is_a((dv), "dk") && \ 172 !device_is_a((dv), "flash")) 173 174 void 175 setroot(device_t bootdv, int bootpartition) 176 { 177 device_t dv; 178 deviter_t di; 179 int len, majdev; 180 dev_t nrootdev; 181 dev_t ndumpdev = NODEV; 182 char buf[128]; 183 const char *rootdevname; 184 const char *dumpdevname; 185 device_t rootdv = NULL; /* XXX gcc -Wuninitialized */ 186 device_t dumpdv = NULL; 187 struct ifnet *ifp; 188 const char *deffsname; 189 struct vfsops *vops; 190 191 #ifdef TFTPROOT 192 if (tftproot_dhcpboot(bootdv) != 0) 193 boothowto |= RB_ASKNAME; 194 #endif 195 196 /* 197 * For root on md0 we have to force the attachment of md0. 198 */ 199 if (md_is_root) { 200 int md_major; 201 dev_t md_dev; 202 203 bootdv = NULL; 204 md_major = devsw_name2blk("md", NULL, 0); 205 if (md_major >= 0) { 206 md_dev = MAKEDISKDEV(md_major, 0, RAW_PART); 207 if (bdev_open(md_dev, FREAD, S_IFBLK, curlwp) == 0) 208 bootdv = device_find_by_xname("md0"); 209 } 210 if (bootdv == NULL) 211 panic("Cannot open \"md0\" (root)"); 212 } 213 214 /* 215 * Let bootcode augment "rootspec". 216 */ 217 if (rootspec == NULL) 218 rootspec = bootspec; 219 220 /* 221 * If NFS is specified as the file system, and we found 222 * a DV_DISK boot device (or no boot device at all), then 223 * find a reasonable network interface for "rootspec". 224 */ 225 vops = vfs_getopsbyname(MOUNT_NFS); 226 if (vops != NULL && strcmp(rootfstype, MOUNT_NFS) == 0 && 227 rootspec == NULL && 228 (bootdv == NULL || device_class(bootdv) != DV_IFNET)) { 229 int s = pserialize_read_enter(); 230 IFNET_READER_FOREACH(ifp) { 231 if ((ifp->if_flags & 232 (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0) 233 break; 234 } 235 if (ifp == NULL) { 236 /* 237 * Can't find a suitable interface; ask the 238 * user. 239 */ 240 boothowto |= RB_ASKNAME; 241 } else { 242 /* 243 * Have a suitable interface; behave as if 244 * the user specified this interface. 245 */ 246 rootspec = (const char *)ifp->if_xname; 247 } 248 pserialize_read_exit(s); 249 } 250 if (vops != NULL) 251 vfs_delref(vops); 252 253 /* 254 * If wildcarded root and we the boot device wasn't determined, 255 * ask the user. 256 */ 257 if (rootspec == NULL && bootdv == NULL) 258 boothowto |= RB_ASKNAME; 259 260 top: 261 if (boothowto & RB_ASKNAME) { 262 device_t defdumpdv; 263 264 for (;;) { 265 printf("root device"); 266 if (bootdv != NULL) { 267 printf(" (default %s", device_xname(bootdv)); 268 if (DEV_USES_PARTITIONS(bootdv)) 269 printf("%c", bootpartition + 'a'); 270 printf(")"); 271 } 272 printf(": "); 273 len = cngetsn(buf, sizeof(buf)); 274 if (len == 0 && bootdv != NULL) { 275 strlcpy(buf, device_xname(bootdv), sizeof(buf)); 276 len = strlen(buf); 277 } 278 if (len > 0 && buf[len - 1] == '*') { 279 buf[--len] = '\0'; 280 dv = getdisk(buf, len, 1, &nrootdev, 0); 281 if (dv != NULL) { 282 rootdv = dv; 283 break; 284 } 285 } 286 dv = getdisk(buf, len, bootpartition, &nrootdev, 0); 287 if (dv != NULL) { 288 rootdv = dv; 289 break; 290 } 291 } 292 293 /* 294 * Set up the default dump device. If root is on 295 * a network device, there is no default dump 296 * device, since we don't support dumps to the 297 * network. 298 */ 299 if (DEV_USES_PARTITIONS(rootdv) == 0) 300 defdumpdv = NULL; 301 else 302 defdumpdv = rootdv; 303 304 for (;;) { 305 printf("dump device"); 306 if (defdumpdv != NULL) { 307 /* 308 * Note, we know it's a disk if we get here. 309 */ 310 printf(" (default %sb)", device_xname(defdumpdv)); 311 } 312 printf(": "); 313 len = cngetsn(buf, sizeof(buf)); 314 if (len == 0) { 315 if (defdumpdv != NULL) { 316 ndumpdev = MAKEDISKDEV(major(nrootdev), 317 DISKUNIT(nrootdev), 1); 318 } 319 dumpdv = defdumpdv; 320 break; 321 } 322 if (len == 4 && strcmp(buf, "none") == 0) { 323 dumpdv = NULL; 324 break; 325 } 326 dv = getdisk(buf, len, 1, &ndumpdev, 1); 327 if (dv != NULL) { 328 dumpdv = dv; 329 break; 330 } 331 } 332 333 rootdev = nrootdev; 334 dumpdev = ndumpdev; 335 336 for (vops = LIST_FIRST(&vfs_list); vops != NULL; 337 vops = LIST_NEXT(vops, vfs_list)) { 338 if (vops->vfs_mountroot != NULL && 339 strcmp(rootfstype, vops->vfs_name) == 0) 340 break; 341 } 342 343 if (vops == NULL) { 344 deffsname = "generic"; 345 } else 346 deffsname = vops->vfs_name; 347 348 for (;;) { 349 printf("file system (default %s): ", deffsname); 350 len = cngetsn(buf, sizeof(buf)); 351 if (len == 0) { 352 if (strcmp(deffsname, "generic") == 0) 353 rootfstype = ROOT_FSTYPE_ANY; 354 break; 355 } 356 if (len == 4 && strcmp(buf, "halt") == 0) 357 cpu_reboot(RB_HALT, NULL); 358 else if (len == 6 && strcmp(buf, "reboot") == 0) 359 cpu_reboot(0, NULL); 360 #if defined(DDB) 361 else if (len == 3 && strcmp(buf, "ddb") == 0) { 362 console_debugger(); 363 } 364 #endif 365 else if (len == 7 && strcmp(buf, "generic") == 0) { 366 rootfstype = ROOT_FSTYPE_ANY; 367 break; 368 } 369 vops = vfs_getopsbyname(buf); 370 if (vops == NULL || vops->vfs_mountroot == NULL) { 371 printf("use one of: generic"); 372 for (vops = LIST_FIRST(&vfs_list); 373 vops != NULL; 374 vops = LIST_NEXT(vops, vfs_list)) { 375 if (vops->vfs_mountroot != NULL) 376 printf(" %s", vops->vfs_name); 377 } 378 if (vops != NULL) 379 vfs_delref(vops); 380 #if defined(DDB) 381 printf(" ddb"); 382 #endif 383 printf(" halt reboot\n"); 384 } else { 385 /* 386 * XXX If *vops gets freed between here and 387 * the call to mountroot(), rootfstype will 388 * point to something unexpected. But in 389 * this case the system will fail anyway. 390 */ 391 rootfstype = vops->vfs_name; 392 vfs_delref(vops); 393 break; 394 } 395 } 396 397 } else if (rootspec == NULL) { 398 /* 399 * Wildcarded root; use the boot device. 400 */ 401 rootdv = bootdv; 402 403 if (bootdv) 404 majdev = devsw_name2blk(device_xname(bootdv), NULL, 0); 405 else 406 majdev = -1; 407 if (majdev >= 0) { 408 /* 409 * Root is on a disk. `bootpartition' is root, 410 * unless the device does not use partitions. 411 */ 412 if (DEV_USES_PARTITIONS(bootdv)) 413 rootdev = MAKEDISKDEV(majdev, 414 device_unit(bootdv), 415 bootpartition); 416 else 417 rootdev = makedev(majdev, device_unit(bootdv)); 418 } 419 } else { 420 421 /* 422 * `root on <dev> ...' 423 */ 424 425 /* 426 * If it's a network interface, we can bail out 427 * early. 428 */ 429 dv = finddevice(rootspec); 430 if (dv != NULL && device_class(dv) == DV_IFNET) { 431 rootdv = dv; 432 goto haveroot; 433 } 434 435 if (dv != NULL && device_class(dv) == DV_DISK && 436 !DEV_USES_PARTITIONS(dv) && 437 (majdev = devsw_name2blk(device_xname(dv), NULL, 0)) >= 0) { 438 rootdv = dv; 439 rootdev = makedev(majdev, device_unit(dv)); 440 goto haveroot; 441 } 442 443 rootdevname = devsw_blk2name(major(rootdev)); 444 if (rootdevname == NULL) { 445 printf("unknown device major 0x%llx\n", 446 (unsigned long long)rootdev); 447 boothowto |= RB_ASKNAME; 448 goto top; 449 } 450 memset(buf, 0, sizeof(buf)); 451 snprintf(buf, sizeof(buf), "%s%llu", rootdevname, 452 (unsigned long long)DISKUNIT(rootdev)); 453 454 rootdv = finddevice(buf); 455 if (rootdv == NULL) { 456 printf("device %s (0x%llx) not configured\n", 457 buf, (unsigned long long)rootdev); 458 boothowto |= RB_ASKNAME; 459 goto top; 460 } 461 } 462 463 haveroot: 464 465 root_device = rootdv; 466 467 switch (device_class(rootdv)) { 468 case DV_IFNET: 469 case DV_DISK: 470 aprint_normal("root on %s", device_xname(rootdv)); 471 if (DEV_USES_PARTITIONS(rootdv)) 472 aprint_normal("%c", (int)DISKPART(rootdev) + 'a'); 473 break; 474 475 default: 476 printf("can't determine root device\n"); 477 boothowto |= RB_ASKNAME; 478 goto top; 479 } 480 481 /* 482 * Now configure the dump device. 483 * 484 * If we haven't figured out the dump device, do so, with 485 * the following rules: 486 * 487 * (a) We already know dumpdv in the RB_ASKNAME case. 488 * 489 * (b) If dumpspec is set, try to use it. If the device 490 * is not available, punt. 491 * 492 * (c) If dumpspec is not set, the dump device is 493 * wildcarded or unspecified. If the root device 494 * is DV_IFNET, punt. Otherwise, use partition b 495 * of the root device. 496 */ 497 498 if (boothowto & RB_ASKNAME) { /* (a) */ 499 if (dumpdv == NULL) 500 goto nodumpdev; 501 } else if (dumpspec != NULL) { /* (b) */ 502 if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) { 503 /* 504 * Operator doesn't want a dump device. 505 * Or looks like they tried to pick a network 506 * device. Oops. 507 */ 508 goto nodumpdev; 509 } 510 511 dumpdevname = devsw_blk2name(major(dumpdev)); 512 if (dumpdevname == NULL) 513 goto nodumpdev; 514 memset(buf, 0, sizeof(buf)); 515 snprintf(buf, sizeof(buf), "%s%llu", dumpdevname, 516 (unsigned long long)DISKUNIT(dumpdev)); 517 518 dumpdv = finddevice(buf); 519 if (dumpdv == NULL) { 520 /* 521 * Device not configured. 522 */ 523 goto nodumpdev; 524 } 525 } else { /* (c) */ 526 if (DEV_USES_PARTITIONS(rootdv) == 0) { 527 for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); 528 dv != NULL; 529 dv = deviter_next(&di)) 530 if (isswap(dv)) 531 break; 532 deviter_release(&di); 533 if (dv == NULL) 534 goto nodumpdev; 535 536 majdev = devsw_name2blk(device_xname(dv), NULL, 0); 537 if (majdev < 0) 538 goto nodumpdev; 539 dumpdv = dv; 540 dumpdev = makedev(majdev, device_unit(dumpdv)); 541 } else { 542 dumpdv = rootdv; 543 dumpdev = MAKEDISKDEV(major(rootdev), 544 device_unit(dumpdv), 1); 545 } 546 } 547 548 dumpcdev = devsw_blk2chr(dumpdev); 549 aprint_normal(" dumps on %s", device_xname(dumpdv)); 550 if (DEV_USES_PARTITIONS(dumpdv)) 551 aprint_normal("%c", (int)DISKPART(dumpdev) + 'a'); 552 aprint_normal("\n"); 553 return; 554 555 nodumpdev: 556 dumpdev = NODEV; 557 dumpcdev = NODEV; 558 aprint_normal("\n"); 559 } 560 561 static device_t 562 finddevice(const char *name) 563 { 564 const char *wname; 565 566 if ((wname = getwedgename(name, strlen(name))) != NULL) 567 return dkwedge_find_by_wname(wname); 568 569 return device_find_by_xname(name); 570 } 571 572 static device_t 573 getdisk(char *str, int len, int defpart, dev_t *devp, int isdump) 574 { 575 device_t dv; 576 deviter_t di; 577 578 if ((dv = parsedisk(str, len, defpart, devp)) == NULL) { 579 printf("use one of:"); 580 for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL; 581 dv = deviter_next(&di)) { 582 if (DEV_USES_PARTITIONS(dv)) 583 printf(" %s[a-%c]", device_xname(dv), 584 'a' + MAXPARTITIONS - 1); 585 else if (device_class(dv) == DV_DISK) 586 printf(" %s", device_xname(dv)); 587 if (isdump == 0 && device_class(dv) == DV_IFNET) 588 printf(" %s", device_xname(dv)); 589 } 590 deviter_release(&di); 591 dkwedge_print_wnames(); 592 if (isdump) 593 printf(" none"); 594 #if defined(DDB) 595 printf(" ddb"); 596 #endif 597 printf(" halt reboot\n"); 598 } 599 return dv; 600 } 601 602 static const char * 603 getwedgename(const char *name, int namelen) 604 { 605 const char *wpfx = "wedge:"; 606 const int wpfxlen = strlen(wpfx); 607 608 if (namelen < wpfxlen || strncmp(name, wpfx, wpfxlen) != 0) 609 return NULL; 610 611 return name + wpfxlen; 612 } 613 614 static device_t 615 parsedisk(char *str, int len, int defpart, dev_t *devp) 616 { 617 device_t dv; 618 const char *wname; 619 char *cp, c; 620 int majdev, part; 621 if (len == 0) 622 return (NULL); 623 624 if (len == 4 && strcmp(str, "halt") == 0) 625 cpu_reboot(RB_HALT, NULL); 626 else if (len == 6 && strcmp(str, "reboot") == 0) 627 cpu_reboot(0, NULL); 628 #if defined(DDB) 629 else if (len == 3 && strcmp(str, "ddb") == 0) 630 console_debugger(); 631 #endif 632 633 cp = str + len - 1; 634 c = *cp; 635 636 if ((wname = getwedgename(str, len)) != NULL) { 637 if ((dv = dkwedge_find_by_wname(wname)) == NULL) 638 return NULL; 639 part = defpart; 640 goto gotdisk; 641 } else if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) { 642 part = c - 'a'; 643 *cp = '\0'; 644 } else 645 part = defpart; 646 647 dv = finddevice(str); 648 if (dv != NULL) { 649 if (device_class(dv) == DV_DISK) { 650 gotdisk: 651 majdev = devsw_name2blk(device_xname(dv), NULL, 0); 652 if (majdev < 0) 653 panic("parsedisk"); 654 if (DEV_USES_PARTITIONS(dv)) 655 *devp = MAKEDISKDEV(majdev, device_unit(dv), 656 part); 657 else 658 *devp = makedev(majdev, device_unit(dv)); 659 } 660 661 if (device_class(dv) == DV_IFNET) 662 *devp = NODEV; 663 } 664 665 *cp = c; 666 return (dv); 667 } 668