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