1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * bootadm(1M) is a new utility for managing bootability of 30 * Solaris *Newboot* environments. It has two primary tasks: 31 * - Allow end users to manage bootability of Newboot Solaris instances 32 * - Provide services to other subsystems in Solaris (primarily Install) 33 */ 34 35 /* Headers */ 36 #include <stdio.h> 37 #include <errno.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <stdarg.h> 44 #include <limits.h> 45 #include <signal.h> 46 #include <sys/wait.h> 47 #include <sys/mnttab.h> 48 #include <sys/statvfs.h> 49 #include <libnvpair.h> 50 #include <ftw.h> 51 #include <fcntl.h> 52 #include <strings.h> 53 #include <sys/systeminfo.h> 54 #include <sys/dktp/fdisk.h> 55 #include <sys/param.h> 56 57 #include <pwd.h> 58 #include <grp.h> 59 #include <device_info.h> 60 61 #include <locale.h> 62 63 #include <assert.h> 64 65 #include "message.h" 66 #include "bootadm.h" 67 68 #ifndef TEXT_DOMAIN 69 #define TEXT_DOMAIN "SUNW_OST_OSCMD" 70 #endif /* TEXT_DOMAIN */ 71 72 /* Type definitions */ 73 74 /* Primary subcmds */ 75 typedef enum { 76 BAM_MENU = 3, 77 BAM_ARCHIVE 78 } subcmd_t; 79 80 typedef enum { 81 OPT_ABSENT = 0, /* No option */ 82 OPT_REQ, /* option required */ 83 OPT_OPTIONAL /* option may or may not be present */ 84 } option_t; 85 86 typedef struct { 87 char *subcmd; 88 option_t option; 89 error_t (*handler)(); 90 int unpriv; /* is this an unprivileged command */ 91 } subcmd_defn_t; 92 93 #define LINE_INIT 0 /* lineNum initial value */ 94 #define ENTRY_INIT -1 /* entryNum initial value */ 95 #define ALL_ENTRIES -2 /* selects all boot entries */ 96 97 #define GRUB_DIR "/boot/grub" 98 #define GRUB_MENU "/boot/grub/menu.lst" 99 #define MENU_TMP "/boot/grub/menu.lst.tmp" 100 #define RAMDISK_SPECIAL "/ramdisk" 101 #define STUBBOOT "/stubboot" 102 103 /* lock related */ 104 #define BAM_LOCK_FILE "/var/run/bootadm.lock" 105 #define LOCK_FILE_PERMS (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 106 107 #define CREATE_RAMDISK "/boot/solaris/bin/create_ramdisk" 108 #define CREATE_DISKMAP "/boot/solaris/bin/create_diskmap" 109 #define GRUBDISK_MAP "/var/run/solaris_grubdisk.map" 110 111 #define GRUB_slice "/etc/lu/GRUB_slice" 112 #define GRUB_root "/etc/lu/GRUB_root" 113 #define GRUB_backup_menu "/etc/lu/GRUB_backup_menu" 114 #define GRUB_slice_mntpt "/tmp/GRUB_slice_mntpt" 115 #define LU_ACTIVATE_FILE "/etc/lu/DelayUpdate/activate.sh" 116 #define GRUB_fdisk "/etc/lu/GRUB_fdisk" 117 #define GRUB_fdisk_target "/etc/lu/GRUB_fdisk_target" 118 119 #define INSTALLGRUB "/sbin/installgrub" 120 #define STAGE1 "/boot/grub/stage1" 121 #define STAGE2 "/boot/grub/stage2" 122 123 /* 124 * Default file attributes 125 */ 126 #define DEFAULT_DEV_MODE 0644 /* default permissions */ 127 #define DEFAULT_DEV_UID 0 /* user root */ 128 #define DEFAULT_DEV_GID 3 /* group sys */ 129 130 /* 131 * Menu related 132 * menu_cmd_t and menu_cmds must be kept in sync 133 */ 134 char *menu_cmds[] = { 135 "default", /* DEFAULT_CMD */ 136 "timeout", /* TIMEOUT_CMD */ 137 "title", /* TITLE_CMD */ 138 "root", /* ROOT_CMD */ 139 "kernel", /* KERNEL_CMD */ 140 "kernel$", /* KERNEL_DOLLAR_CMD */ 141 "module", /* MODULE_CMD */ 142 "module$", /* MODULE_DOLLAR_CMD */ 143 " ", /* SEP_CMD */ 144 "#", /* COMMENT_CMD */ 145 "chainloader", /* CHAINLOADER_CMD */ 146 "args", /* ARGS_CMD */ 147 NULL 148 }; 149 150 #define OPT_ENTRY_NUM "entry" 151 152 /* 153 * archive related 154 */ 155 typedef struct { 156 line_t *head; 157 line_t *tail; 158 } filelist_t; 159 160 #define BOOT_FILE_LIST "boot/solaris/filelist.ramdisk" 161 #define ETC_FILE_LIST "etc/boot/solaris/filelist.ramdisk" 162 163 #define FILE_STAT "boot/solaris/filestat.ramdisk" 164 #define FILE_STAT_TMP "boot/solaris/filestat.ramdisk.tmp" 165 #define DIR_PERMS (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) 166 #define FILE_STAT_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 167 168 /* Globals */ 169 int bam_verbose; 170 int bam_force; 171 static char *prog; 172 static subcmd_t bam_cmd; 173 static char *bam_root; 174 static int bam_rootlen; 175 static int bam_root_readonly; 176 static int bam_alt_root; 177 static char *bam_subcmd; 178 static char *bam_opt; 179 static int bam_debug; 180 static char **bam_argv; 181 static int bam_argc; 182 static int bam_check; 183 static int bam_smf_check; 184 static int bam_lock_fd = -1; 185 static char rootbuf[PATH_MAX] = "/"; 186 static int bam_update_all; 187 188 /* function prototypes */ 189 static void parse_args_internal(int argc, char *argv[]); 190 static void parse_args(int argc, char *argv[]); 191 static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]); 192 static error_t bam_archive(char *subcmd, char *opt); 193 194 static void bam_print(char *format, ...); 195 static void bam_exit(int excode); 196 static void bam_lock(void); 197 static void bam_unlock(void); 198 199 static int exec_cmd(char *cmdline, char *output, int64_t osize); 200 static error_t read_globals(menu_t *mp, char *menu_path, 201 char *globalcmd, int quiet); 202 203 static menu_t *menu_read(char *menu_path); 204 static error_t menu_write(char *root, menu_t *mp); 205 static void linelist_free(line_t *start); 206 static void menu_free(menu_t *mp); 207 static void line_free(line_t *lp); 208 static void filelist_free(filelist_t *flistp); 209 static error_t list2file(char *root, char *tmp, 210 char *final, line_t *start); 211 static error_t list_entry(menu_t *mp, char *menu_path, char *opt); 212 static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt); 213 static error_t update_entry(menu_t *mp, char *root, char *opt); 214 static error_t update_temp(menu_t *mp, char *root, char *opt); 215 216 static error_t update_archive(char *root, char *opt); 217 static error_t list_archive(char *root, char *opt); 218 static error_t update_all(char *root, char *opt); 219 static error_t read_list(char *root, filelist_t *flistp); 220 static error_t set_global(menu_t *mp, char *globalcmd, int val); 221 static error_t set_option(menu_t *mp, char *globalcmd, char *opt); 222 static error_t set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, 223 char *buf, size_t bufsize); 224 static char *expand_path(const char *partial_path); 225 226 static long s_strtol(char *str); 227 static int s_fputs(char *str, FILE *fp); 228 229 static char *s_strdup(char *str); 230 static int is_readonly(char *); 231 static int is_amd64(void); 232 static void append_to_flist(filelist_t *, char *); 233 234 #if defined(__sparc) 235 static void sparc_abort(void); 236 #endif 237 238 /* Menu related sub commands */ 239 static subcmd_defn_t menu_subcmds[] = { 240 "set_option", OPT_OPTIONAL, set_option, 0, /* PUB */ 241 "list_entry", OPT_OPTIONAL, list_entry, 1, /* PUB */ 242 "delete_all_entries", OPT_ABSENT, delete_all_entries, 0, /* PVT */ 243 "update_entry", OPT_REQ, update_entry, 0, /* menu */ 244 "update_temp", OPT_OPTIONAL, update_temp, 0, /* reboot */ 245 "upgrade", OPT_ABSENT, upgrade_menu, 0, /* menu */ 246 NULL, 0, NULL, 0 /* must be last */ 247 }; 248 249 /* Archive related sub commands */ 250 static subcmd_defn_t arch_subcmds[] = { 251 "update", OPT_ABSENT, update_archive, 0, /* PUB */ 252 "update_all", OPT_ABSENT, update_all, 0, /* PVT */ 253 "list", OPT_OPTIONAL, list_archive, 1, /* PUB */ 254 NULL, 0, NULL, 0 /* must be last */ 255 }; 256 257 static struct { 258 nvlist_t *new_nvlp; 259 nvlist_t *old_nvlp; 260 int need_update; 261 } walk_arg; 262 263 264 struct safefile { 265 char *name; 266 struct safefile *next; 267 }; 268 269 static struct safefile *safefiles = NULL; 270 #define NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update" 271 272 static void 273 usage(void) 274 { 275 (void) fprintf(stderr, "USAGE:\n"); 276 277 278 /* archive usage */ 279 (void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n", 280 prog); 281 (void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog); 282 #ifndef __sparc 283 /* x86 only */ 284 (void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog); 285 (void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog); 286 #endif 287 } 288 289 int 290 main(int argc, char *argv[]) 291 { 292 error_t ret; 293 294 (void) setlocale(LC_ALL, ""); 295 (void) textdomain(TEXT_DOMAIN); 296 297 if ((prog = strrchr(argv[0], '/')) == NULL) { 298 prog = argv[0]; 299 } else { 300 prog++; 301 } 302 303 304 /* 305 * Don't depend on caller's umask 306 */ 307 (void) umask(0022); 308 309 parse_args(argc, argv); 310 311 #if defined(__sparc) 312 /* 313 * There are only two valid invocations of bootadm 314 * on SPARC: 315 * 316 * - SPARC diskless server creating boot_archive for i386 clients 317 * - archive creation call during reboot of a SPARC system 318 * 319 * The latter should be a NOP 320 */ 321 if (bam_cmd != BAM_ARCHIVE) { 322 sparc_abort(); 323 } 324 #endif 325 326 switch (bam_cmd) { 327 case BAM_MENU: 328 ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv); 329 break; 330 case BAM_ARCHIVE: 331 ret = bam_archive(bam_subcmd, bam_opt); 332 break; 333 default: 334 usage(); 335 bam_exit(1); 336 } 337 338 if (ret != BAM_SUCCESS) 339 bam_exit(1); 340 341 bam_unlock(); 342 return (0); 343 } 344 345 #if defined(__sparc) 346 347 static void 348 sparc_abort(void) 349 { 350 bam_error(NOT_ON_SPARC); 351 bam_exit(1); 352 } 353 354 #endif 355 356 /* 357 * Equivalence of public and internal commands: 358 * update-archive -- -a update 359 * list-archive -- -a list 360 * set-menu -- -m set_option 361 * list-menu -- -m list_entry 362 * update-menu -- -m update_entry 363 */ 364 static struct cmd_map { 365 char *bam_cmdname; 366 int bam_cmd; 367 char *bam_subcmd; 368 } cmd_map[] = { 369 { "update-archive", BAM_ARCHIVE, "update"}, 370 { "list-archive", BAM_ARCHIVE, "list"}, 371 { "set-menu", BAM_MENU, "set_option"}, 372 { "list-menu", BAM_MENU, "list_entry"}, 373 { "update-menu", BAM_MENU, "update_entry"}, 374 { NULL, 0, NULL} 375 }; 376 377 /* 378 * Commands syntax published in bootadm(1M) are parsed here 379 */ 380 static void 381 parse_args(int argc, char *argv[]) 382 { 383 struct cmd_map *cmp = cmd_map; 384 385 /* command conforming to the final spec */ 386 if (argc > 1 && argv[1][0] != '-') { 387 /* 388 * Map commands to internal table. 389 */ 390 while (cmp->bam_cmdname) { 391 if (strcmp(argv[1], cmp->bam_cmdname) == 0) { 392 bam_cmd = cmp->bam_cmd; 393 bam_subcmd = cmp->bam_subcmd; 394 break; 395 } 396 cmp++; 397 } 398 if (cmp->bam_cmdname == NULL) { 399 usage(); 400 bam_exit(1); 401 } 402 argc--; 403 argv++; 404 } 405 406 parse_args_internal(argc, argv); 407 } 408 409 /* 410 * A combination of public and private commands are parsed here. 411 * The internal syntax and the corresponding functionality are: 412 * -a update -- update-archive 413 * -a list -- list-archive 414 * -a update-all -- (reboot to sync all mounted OS archive) 415 * -m update_entry -- update-menu 416 * -m list_entry -- list-menu 417 * -m update_temp -- (reboot -- [boot-args]) 418 * -m delete_all_entries -- (called from install) 419 */ 420 static void 421 parse_args_internal(int argc, char *argv[]) 422 { 423 int c, error; 424 extern char *optarg; 425 extern int optind, opterr; 426 427 /* Suppress error message from getopt */ 428 opterr = 0; 429 430 error = 0; 431 while ((c = getopt(argc, argv, "a:d:fm:no:vCR:xX")) != -1) { 432 switch (c) { 433 case 'a': 434 if (bam_cmd) { 435 error = 1; 436 bam_error(MULT_CMDS, c); 437 } 438 bam_cmd = BAM_ARCHIVE; 439 bam_subcmd = optarg; 440 break; 441 case 'd': 442 if (bam_debug) { 443 error = 1; 444 bam_error(DUP_OPT, c); 445 } 446 bam_debug = s_strtol(optarg); 447 break; 448 case 'f': 449 if (bam_force) { 450 error = 1; 451 bam_error(DUP_OPT, c); 452 } 453 bam_force = 1; 454 break; 455 case 'm': 456 if (bam_cmd) { 457 error = 1; 458 bam_error(MULT_CMDS, c); 459 } 460 bam_cmd = BAM_MENU; 461 bam_subcmd = optarg; 462 break; 463 case 'n': 464 if (bam_check) { 465 error = 1; 466 bam_error(DUP_OPT, c); 467 } 468 bam_check = 1; 469 break; 470 case 'o': 471 if (bam_opt) { 472 error = 1; 473 bam_error(DUP_OPT, c); 474 } 475 bam_opt = optarg; 476 break; 477 case 'v': 478 if (bam_verbose) { 479 error = 1; 480 bam_error(DUP_OPT, c); 481 } 482 bam_verbose = 1; 483 break; 484 case 'C': 485 bam_smf_check = 1; 486 break; 487 case 'R': 488 if (bam_root) { 489 error = 1; 490 bam_error(DUP_OPT, c); 491 break; 492 } else if (realpath(optarg, rootbuf) == NULL) { 493 error = 1; 494 bam_error(CANT_RESOLVE, optarg, 495 strerror(errno)); 496 break; 497 } 498 bam_alt_root = 1; 499 bam_root = rootbuf; 500 bam_rootlen = strlen(rootbuf); 501 break; 502 case '?': 503 error = 1; 504 bam_error(BAD_OPT, optopt); 505 break; 506 default : 507 error = 1; 508 bam_error(BAD_OPT, c); 509 break; 510 } 511 } 512 513 /* 514 * A command option must be specfied 515 */ 516 if (!bam_cmd) { 517 if (bam_opt && strcmp(bam_opt, "all") == 0) { 518 usage(); 519 bam_exit(0); 520 } 521 bam_error(NEED_CMD); 522 error = 1; 523 } 524 525 if (error) { 526 usage(); 527 bam_exit(1); 528 } 529 530 if (optind > argc) { 531 bam_error(INT_ERROR, "parse_args"); 532 bam_exit(1); 533 } else if (optind < argc) { 534 bam_argv = &argv[optind]; 535 bam_argc = argc - optind; 536 } 537 538 /* 539 * -n implies verbose mode 540 */ 541 if (bam_check) 542 bam_verbose = 1; 543 } 544 545 static error_t 546 check_subcmd_and_options( 547 char *subcmd, 548 char *opt, 549 subcmd_defn_t *table, 550 error_t (**fp)()) 551 { 552 int i; 553 554 if (subcmd == NULL) { 555 bam_error(NEED_SUBCMD); 556 return (BAM_ERROR); 557 } 558 559 if (bam_argc != 0 || bam_argv) { 560 if (strcmp(subcmd, "set_option") != 0 || bam_argc != 1) { 561 bam_error(TRAILING_ARGS); 562 usage(); 563 return (BAM_ERROR); 564 } 565 } 566 567 if (bam_root == NULL) { 568 bam_root = rootbuf; 569 bam_rootlen = 1; 570 } 571 572 /* verify that subcmd is valid */ 573 for (i = 0; table[i].subcmd != NULL; i++) { 574 if (strcmp(table[i].subcmd, subcmd) == 0) 575 break; 576 } 577 578 if (table[i].subcmd == NULL) { 579 bam_error(INVALID_SUBCMD, subcmd); 580 return (BAM_ERROR); 581 } 582 583 if (table[i].unpriv == 0 && geteuid() != 0) { 584 bam_error(MUST_BE_ROOT); 585 return (BAM_ERROR); 586 } 587 588 /* 589 * Currently only privileged commands need a lock 590 */ 591 if (table[i].unpriv == 0) 592 bam_lock(); 593 594 /* subcmd verifies that opt is appropriate */ 595 if (table[i].option != OPT_OPTIONAL) { 596 if ((table[i].option == OPT_REQ) ^ (opt != NULL)) { 597 if (opt) 598 bam_error(NO_OPT_REQ, subcmd); 599 else 600 bam_error(MISS_OPT, subcmd); 601 return (BAM_ERROR); 602 } 603 } 604 605 *fp = table[i].handler; 606 607 return (BAM_SUCCESS); 608 } 609 610 611 static char * 612 mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type) 613 { 614 struct extmnttab mnt; 615 struct stat sb; 616 char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32]; 617 char cmd[PATH_MAX]; 618 char *mntpt; 619 int p, l, f; 620 FILE *fp; 621 622 assert(mnted); 623 *mnted = 0; 624 625 /* 626 * physlice, logslice, fs_type args may be NULL 627 */ 628 if (physlice) 629 *physlice = NULL; 630 if (logslice) 631 *logslice = NULL; 632 if (fs_type) 633 *fs_type = NULL; 634 635 if (stat(GRUB_slice, &sb) != 0) { 636 bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 637 return (NULL); 638 } 639 640 fp = fopen(GRUB_slice, "r"); 641 if (fp == NULL) { 642 bam_error(OPEN_FAIL, GRUB_slice, strerror(errno)); 643 return (NULL); 644 } 645 646 dev[0] = fstype[0] = phys[0] = '\0'; 647 p = sizeof ("PHYS_SLICE=") - 1; 648 l = sizeof ("LOG_SLICE=") - 1; 649 f = sizeof ("LOG_FSTYP=") - 1; 650 while (s_fgets(buf, sizeof (buf), fp) != NULL) { 651 if (strncmp(buf, "PHYS_SLICE=", p) == 0) { 652 (void) strlcpy(phys, buf + p, sizeof (phys)); 653 continue; 654 } 655 if (strncmp(buf, "LOG_SLICE=", l) == 0) { 656 (void) strlcpy(dev, buf + l, sizeof (dev)); 657 continue; 658 } 659 if (strncmp(buf, "LOG_FSTYP=", f) == 0) { 660 (void) strlcpy(fstype, buf + f, sizeof (fstype)); 661 continue; 662 } 663 } 664 (void) fclose(fp); 665 666 if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') { 667 bam_error(BAD_SLICE_FILE, GRUB_slice); 668 return (NULL); 669 } 670 671 if (physlice) { 672 *physlice = s_strdup(phys); 673 } 674 if (logslice) { 675 *logslice = s_strdup(dev); 676 } 677 if (fs_type) { 678 *fs_type = s_strdup(fstype); 679 } 680 681 /* 682 * Check if the slice is already mounted 683 */ 684 fp = fopen(MNTTAB, "r"); 685 if (fp == NULL) { 686 bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 687 goto error; 688 } 689 690 resetmnttab(fp); 691 692 mntpt = NULL; 693 while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 694 if (strcmp(mnt.mnt_special, dev) == 0) { 695 mntpt = s_strdup(mnt.mnt_mountp); 696 break; 697 } 698 } 699 700 (void) fclose(fp); 701 702 if (mntpt) { 703 return (mntpt); 704 } 705 706 707 /* 708 * GRUB slice is not mounted, we need to mount it now. 709 * First create the mountpoint 710 */ 711 mntpt = s_calloc(1, PATH_MAX); 712 (void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid()); 713 if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) { 714 bam_error(MKDIR_FAILED, mntpt, strerror(errno)); 715 free(mntpt); 716 goto error; 717 } 718 719 (void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s", 720 fstype, dev, mntpt); 721 722 if (exec_cmd(cmd, NULL, 0) != 0) { 723 bam_error(MOUNT_FAILED, dev, fstype); 724 if (rmdir(mntpt) != 0) { 725 bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 726 } 727 free(mntpt); 728 goto error; 729 } 730 731 *mnted = 1; 732 return (mntpt); 733 734 error: 735 if (physlice) { 736 free(*physlice); 737 *physlice = NULL; 738 } 739 if (logslice) { 740 free(*logslice); 741 *logslice = NULL; 742 } 743 if (fs_type) { 744 free(*fs_type); 745 *fs_type = NULL; 746 } 747 return (NULL); 748 } 749 750 static void 751 umount_grub_slice( 752 int mnted, 753 char *mntpt, 754 char *physlice, 755 char *logslice, 756 char *fs_type) 757 { 758 char cmd[PATH_MAX]; 759 760 /* 761 * If we have not dealt with GRUB slice 762 * we have nothing to do - just return. 763 */ 764 if (mntpt == NULL) 765 return; 766 767 768 /* 769 * If we mounted the filesystem earlier in mount_grub_slice() 770 * unmount it now. 771 */ 772 if (mnted) { 773 (void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s", 774 mntpt); 775 if (exec_cmd(cmd, NULL, 0) != 0) { 776 bam_error(UMOUNT_FAILED, mntpt); 777 } 778 if (rmdir(mntpt) != 0) { 779 bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 780 } 781 } 782 783 if (physlice) 784 free(physlice); 785 if (logslice) 786 free(logslice); 787 if (fs_type) 788 free(fs_type); 789 790 free(mntpt); 791 } 792 793 static char * 794 use_stubboot(void) 795 { 796 int mnted; 797 struct stat sb; 798 struct extmnttab mnt; 799 FILE *fp; 800 char cmd[PATH_MAX]; 801 802 if (stat(STUBBOOT, &sb) != 0) { 803 bam_error(STUBBOOT_DIR_NOT_FOUND); 804 return (NULL); 805 } 806 807 /* 808 * Check if stubboot is mounted. If not, mount it 809 */ 810 fp = fopen(MNTTAB, "r"); 811 if (fp == NULL) { 812 bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 813 return (NULL); 814 } 815 816 resetmnttab(fp); 817 818 mnted = 0; 819 while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 820 if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) { 821 mnted = 1; 822 break; 823 } 824 } 825 826 (void) fclose(fp); 827 828 if (mnted) 829 return (STUBBOOT); 830 831 /* 832 * Stubboot is not mounted, mount it now. 833 * It should exist in /etc/vfstab 834 */ 835 (void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s", 836 STUBBOOT); 837 if (exec_cmd(cmd, NULL, 0) != 0) { 838 bam_error(MOUNT_MNTPT_FAILED, STUBBOOT); 839 return (NULL); 840 } 841 842 return (STUBBOOT); 843 } 844 845 static void 846 disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted) 847 { 848 /* 849 * Check if we did a temp mount of an unmounted device. 850 * If yes, print the block device and fstype for that device 851 * else it is already mounted, so we print the path to the GRUB menu. 852 */ 853 if (mnted) { 854 bam_print(GRUB_MENU_DEVICE, logslice); 855 bam_print(GRUB_MENU_FSTYPE, fstype); 856 } else { 857 bam_print(GRUB_MENU_PATH, menu_path); 858 } 859 } 860 861 /* 862 * NOTE: A single "/" is also considered a trailing slash and will 863 * be deleted. 864 */ 865 static void 866 elide_trailing_slash(const char *src, char *dst, size_t dstsize) 867 { 868 size_t dstlen; 869 870 assert(src); 871 assert(dst); 872 873 (void) strlcpy(dst, src, dstsize); 874 875 dstlen = strlen(dst); 876 if (dst[dstlen - 1] == '/') { 877 dst[dstlen - 1] = '\0'; 878 } 879 } 880 881 static error_t 882 bam_menu(char *subcmd, char *opt, int largc, char *largv[]) 883 { 884 error_t ret; 885 char menu_path[PATH_MAX]; 886 char path[PATH_MAX]; 887 menu_t *menu; 888 char *mntpt, *menu_root, *logslice, *fstype; 889 struct stat sb; 890 int mnted; /* set if we did a mount */ 891 error_t (*f)(menu_t *mp, char *menu_path, char *opt); 892 893 /* 894 * Check arguments 895 */ 896 ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f); 897 if (ret == BAM_ERROR) { 898 return (BAM_ERROR); 899 } 900 901 mntpt = NULL; 902 mnted = 0; 903 logslice = fstype = NULL; 904 905 /* 906 * Check for the menu.list file: 907 * 908 * 1. Check for a GRUB_slice file, be it on / or 909 * on the user-provided alternate root. 910 * 2. Use the alternate root, if given. 911 * 3. Check /stubboot 912 * 4. Use / 913 */ 914 if (bam_alt_root) { 915 (void) snprintf(path, sizeof (path), "%s%s", bam_root, 916 GRUB_slice); 917 } else { 918 (void) snprintf(path, sizeof (path), "%s", GRUB_slice); 919 } 920 921 if (stat(path, &sb) == 0) { 922 mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype); 923 menu_root = mntpt; 924 } else if (bam_alt_root) { 925 menu_root = bam_root; 926 } else if (stat(STUBBOOT, &sb) == 0) { 927 menu_root = use_stubboot(); 928 } else { 929 menu_root = bam_root; 930 } 931 932 if (menu_root == NULL) { 933 bam_error(CANNOT_LOCATE_GRUB_MENU); 934 return (BAM_ERROR); 935 } 936 937 elide_trailing_slash(menu_root, menu_path, sizeof (menu_path)); 938 (void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path)); 939 940 /* 941 * If listing the menu, display the active menu 942 * location 943 */ 944 if (strcmp(subcmd, "list_entry") == 0) { 945 disp_active_menu_locn(menu_path, logslice, fstype, mnted); 946 } 947 948 menu = menu_read(menu_path); 949 assert(menu); 950 951 /* 952 * Special handling for setting timeout and default 953 */ 954 if (strcmp(subcmd, "set_option") == 0) { 955 if (largc != 1 || largv[0] == NULL) { 956 usage(); 957 menu_free(menu); 958 umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 959 return (BAM_ERROR); 960 } 961 opt = largv[0]; 962 } else if (largc != 0) { 963 usage(); 964 menu_free(menu); 965 umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 966 return (BAM_ERROR); 967 } 968 969 ret = dboot_or_multiboot(bam_root); 970 if (ret != BAM_SUCCESS) 971 return (ret); 972 973 /* 974 * Once the sub-cmd handler has run 975 * only the line field is guaranteed to have valid values 976 */ 977 if ((strcmp(subcmd, "update_entry") == 0) || 978 (strcmp(subcmd, "upgrade") == 0)) 979 ret = f(menu, bam_root, opt); 980 else 981 ret = f(menu, menu_path, opt); 982 if (ret == BAM_WRITE) { 983 ret = menu_write(menu_root, menu); 984 } 985 986 menu_free(menu); 987 988 umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 989 990 return (ret); 991 } 992 993 994 static error_t 995 bam_archive( 996 char *subcmd, 997 char *opt) 998 { 999 error_t ret; 1000 error_t (*f)(char *root, char *opt); 1001 1002 /* 1003 * Add trailing / for archive subcommands 1004 */ 1005 if (rootbuf[strlen(rootbuf) - 1] != '/') 1006 (void) strcat(rootbuf, "/"); 1007 bam_rootlen = strlen(rootbuf); 1008 1009 /* 1010 * Check arguments 1011 */ 1012 ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f); 1013 if (ret != BAM_SUCCESS) { 1014 return (BAM_ERROR); 1015 } 1016 1017 #if defined(__sparc) 1018 /* 1019 * A NOP if called on SPARC during reboot 1020 */ 1021 if (strcmp(subcmd, "update_all") == 0) 1022 return (BAM_SUCCESS); 1023 else if (strcmp(subcmd, "update") != 0) 1024 sparc_abort(); 1025 #endif 1026 1027 ret = dboot_or_multiboot(rootbuf); 1028 if (ret != BAM_SUCCESS) 1029 return (ret); 1030 1031 /* 1032 * Check archive not supported with update_all 1033 * since it is awkward to display out-of-sync 1034 * information for each BE. 1035 */ 1036 if (bam_check && strcmp(subcmd, "update_all") == 0) { 1037 bam_error(CHECK_NOT_SUPPORTED, subcmd); 1038 return (BAM_ERROR); 1039 } 1040 1041 if (strcmp(subcmd, "update_all") == 0) 1042 bam_update_all = 1; 1043 1044 ret = f(bam_root, opt); 1045 1046 bam_update_all = 0; 1047 1048 return (ret); 1049 } 1050 1051 /*PRINTFLIKE1*/ 1052 void 1053 bam_error(char *format, ...) 1054 { 1055 va_list ap; 1056 1057 va_start(ap, format); 1058 (void) fprintf(stderr, "%s: ", prog); 1059 (void) vfprintf(stderr, format, ap); 1060 va_end(ap); 1061 } 1062 1063 /*PRINTFLIKE1*/ 1064 static void 1065 bam_print(char *format, ...) 1066 { 1067 va_list ap; 1068 1069 va_start(ap, format); 1070 (void) vfprintf(stdout, format, ap); 1071 va_end(ap); 1072 } 1073 1074 /*PRINTFLIKE1*/ 1075 void 1076 bam_print_stderr(char *format, ...) 1077 { 1078 va_list ap; 1079 1080 va_start(ap, format); 1081 (void) vfprintf(stderr, format, ap); 1082 va_end(ap); 1083 } 1084 1085 static void 1086 bam_exit(int excode) 1087 { 1088 bam_unlock(); 1089 exit(excode); 1090 } 1091 1092 static void 1093 bam_lock(void) 1094 { 1095 struct flock lock; 1096 pid_t pid; 1097 1098 bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS); 1099 if (bam_lock_fd < 0) { 1100 /* 1101 * We may be invoked early in boot for archive verification. 1102 * In this case, root is readonly and /var/run may not exist. 1103 * Proceed without the lock 1104 */ 1105 if (errno == EROFS || errno == ENOENT) { 1106 bam_root_readonly = 1; 1107 return; 1108 } 1109 1110 bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno)); 1111 bam_exit(1); 1112 } 1113 1114 lock.l_type = F_WRLCK; 1115 lock.l_whence = SEEK_SET; 1116 lock.l_start = 0; 1117 lock.l_len = 0; 1118 1119 if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) { 1120 if (errno != EACCES && errno != EAGAIN) { 1121 bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 1122 (void) close(bam_lock_fd); 1123 bam_lock_fd = -1; 1124 bam_exit(1); 1125 } 1126 pid = 0; 1127 (void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0); 1128 bam_print(FILE_LOCKED, pid); 1129 1130 lock.l_type = F_WRLCK; 1131 lock.l_whence = SEEK_SET; 1132 lock.l_start = 0; 1133 lock.l_len = 0; 1134 if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) { 1135 bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 1136 (void) close(bam_lock_fd); 1137 bam_lock_fd = -1; 1138 bam_exit(1); 1139 } 1140 } 1141 1142 /* We own the lock now */ 1143 pid = getpid(); 1144 (void) write(bam_lock_fd, &pid, sizeof (pid)); 1145 } 1146 1147 static void 1148 bam_unlock(void) 1149 { 1150 struct flock unlock; 1151 1152 /* 1153 * NOP if we don't hold the lock 1154 */ 1155 if (bam_lock_fd < 0) { 1156 return; 1157 } 1158 1159 unlock.l_type = F_UNLCK; 1160 unlock.l_whence = SEEK_SET; 1161 unlock.l_start = 0; 1162 unlock.l_len = 0; 1163 1164 if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) { 1165 bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 1166 } 1167 1168 if (close(bam_lock_fd) == -1) { 1169 bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno)); 1170 } 1171 bam_lock_fd = -1; 1172 } 1173 1174 static error_t 1175 list_archive(char *root, char *opt) 1176 { 1177 filelist_t flist; 1178 filelist_t *flistp = &flist; 1179 line_t *lp; 1180 1181 assert(root); 1182 assert(opt == NULL); 1183 1184 flistp->head = flistp->tail = NULL; 1185 if (read_list(root, flistp) != BAM_SUCCESS) { 1186 return (BAM_ERROR); 1187 } 1188 assert(flistp->head && flistp->tail); 1189 1190 for (lp = flistp->head; lp; lp = lp->next) { 1191 bam_print(PRINT, lp->line); 1192 } 1193 1194 filelist_free(flistp); 1195 1196 return (BAM_SUCCESS); 1197 } 1198 1199 /* 1200 * This routine writes a list of lines to a file. 1201 * The list is *not* freed 1202 */ 1203 static error_t 1204 list2file(char *root, char *tmp, char *final, line_t *start) 1205 { 1206 char tmpfile[PATH_MAX]; 1207 char path[PATH_MAX]; 1208 FILE *fp; 1209 int ret; 1210 struct stat sb; 1211 mode_t mode; 1212 uid_t root_uid; 1213 gid_t sys_gid; 1214 struct passwd *pw; 1215 struct group *gp; 1216 1217 1218 (void) snprintf(path, sizeof (path), "%s%s", root, final); 1219 1220 if (start == NULL) { 1221 if (stat(path, &sb) != -1) { 1222 bam_print(UNLINK_EMPTY, path); 1223 if (unlink(path) != 0) { 1224 bam_error(UNLINK_FAIL, path, strerror(errno)); 1225 return (BAM_ERROR); 1226 } else { 1227 return (BAM_SUCCESS); 1228 } 1229 } 1230 } 1231 1232 /* 1233 * Preserve attributes of existing file if possible, 1234 * otherwise ask the system for uid/gid of root/sys. 1235 * If all fails, fall back on hard-coded defaults. 1236 */ 1237 if (stat(path, &sb) != -1) { 1238 mode = sb.st_mode; 1239 root_uid = sb.st_uid; 1240 sys_gid = sb.st_gid; 1241 } else { 1242 mode = DEFAULT_DEV_MODE; 1243 if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) { 1244 root_uid = pw->pw_uid; 1245 } else { 1246 if (bam_verbose) 1247 bam_error(CANT_FIND_USER, 1248 DEFAULT_DEV_USER, DEFAULT_DEV_UID); 1249 root_uid = (uid_t)DEFAULT_DEV_UID; 1250 } 1251 if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) { 1252 sys_gid = gp->gr_gid; 1253 } else { 1254 if (bam_verbose) 1255 bam_error(CANT_FIND_GROUP, 1256 DEFAULT_DEV_GROUP, DEFAULT_DEV_GID); 1257 sys_gid = (gid_t)DEFAULT_DEV_GID; 1258 } 1259 } 1260 1261 (void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp); 1262 1263 /* Truncate tmpfile first */ 1264 fp = fopen(tmpfile, "w"); 1265 if (fp == NULL) { 1266 bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 1267 return (BAM_ERROR); 1268 } 1269 ret = fclose(fp); 1270 if (ret == EOF) { 1271 bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 1272 return (BAM_ERROR); 1273 } 1274 1275 /* Now open it in append mode */ 1276 fp = fopen(tmpfile, "a"); 1277 if (fp == NULL) { 1278 bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 1279 return (BAM_ERROR); 1280 } 1281 1282 for (; start; start = start->next) { 1283 ret = s_fputs(start->line, fp); 1284 if (ret == EOF) { 1285 bam_error(WRITE_FAIL, tmpfile, strerror(errno)); 1286 (void) fclose(fp); 1287 return (BAM_ERROR); 1288 } 1289 } 1290 1291 ret = fclose(fp); 1292 if (ret == EOF) { 1293 bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 1294 return (BAM_ERROR); 1295 } 1296 1297 /* 1298 * Set up desired attributes. Ignore failures on filesystems 1299 * not supporting these operations - pcfs reports unsupported 1300 * operations as EINVAL. 1301 */ 1302 ret = chmod(tmpfile, mode); 1303 if (ret == -1 && 1304 errno != EINVAL && errno != ENOTSUP) { 1305 bam_error(CHMOD_FAIL, tmpfile, strerror(errno)); 1306 return (BAM_ERROR); 1307 } 1308 1309 ret = chown(tmpfile, root_uid, sys_gid); 1310 if (ret == -1 && 1311 errno != EINVAL && errno != ENOTSUP) { 1312 bam_error(CHOWN_FAIL, tmpfile, strerror(errno)); 1313 return (BAM_ERROR); 1314 } 1315 1316 1317 /* 1318 * Do an atomic rename 1319 */ 1320 ret = rename(tmpfile, path); 1321 if (ret != 0) { 1322 bam_error(RENAME_FAIL, path, strerror(errno)); 1323 return (BAM_ERROR); 1324 } 1325 1326 return (BAM_SUCCESS); 1327 } 1328 1329 /* 1330 * This function should always return 0 - since we want 1331 * to create stat data for *all* files in the list. 1332 */ 1333 /*ARGSUSED*/ 1334 static int 1335 cmpstat( 1336 const char *file, 1337 const struct stat *stat, 1338 int flags, 1339 struct FTW *ftw) 1340 { 1341 uint_t sz; 1342 uint64_t *value; 1343 uint64_t filestat[2]; 1344 int error; 1345 1346 struct safefile *safefilep; 1347 FILE *fp; 1348 1349 /* 1350 * We only want regular files 1351 */ 1352 if (!S_ISREG(stat->st_mode)) 1353 return (0); 1354 1355 /* 1356 * new_nvlp may be NULL if there were errors earlier 1357 * but this is not fatal to update determination. 1358 */ 1359 if (walk_arg.new_nvlp) { 1360 filestat[0] = stat->st_size; 1361 filestat[1] = stat->st_mtime; 1362 error = nvlist_add_uint64_array(walk_arg.new_nvlp, 1363 file + bam_rootlen, filestat, 2); 1364 if (error) 1365 bam_error(NVADD_FAIL, file, strerror(error)); 1366 } 1367 1368 /* 1369 * The remaining steps are only required if we haven't made a 1370 * decision about update or if we are checking (-n) 1371 */ 1372 if (walk_arg.need_update && !bam_check) 1373 return (0); 1374 1375 /* 1376 * If we are invoked as part of system/filesyste/boot-archive, then 1377 * there are a number of things we should not worry about 1378 */ 1379 if (bam_smf_check) { 1380 /* ignore amd64 modules unless we are booted amd64. */ 1381 if (!is_amd64() && strstr(file, "/amd64/") != 0) 1382 return (0); 1383 1384 /* read in list of safe files */ 1385 if (safefiles == NULL) 1386 if (fp = fopen("/boot/solaris/filelist.safe", "r")) { 1387 safefiles = s_calloc(1, 1388 sizeof (struct safefile)); 1389 safefilep = safefiles; 1390 safefilep->name = s_calloc(1, MAXPATHLEN + 1391 MAXNAMELEN); 1392 safefilep->next = NULL; 1393 while (s_fgets(safefilep->name, MAXPATHLEN + 1394 MAXNAMELEN, fp) != NULL) { 1395 safefilep->next = s_calloc(1, 1396 sizeof (struct safefile)); 1397 safefilep = safefilep->next; 1398 safefilep->name = s_calloc(1, 1399 MAXPATHLEN + MAXNAMELEN); 1400 safefilep->next = NULL; 1401 } 1402 (void) fclose(fp); 1403 } 1404 1405 safefilep = safefiles; 1406 while (safefilep->next != NULL) 1407 if (strcmp(file, safefilep->name) != 0) { 1408 fp = fopen(NEED_UPDATE_FILE, "w"); 1409 if (fclose(fp) != 0) 1410 bam_error(CLOSE_FAIL, NEED_UPDATE_FILE, 1411 strerror(errno)); 1412 return (0); 1413 } 1414 } 1415 1416 /* 1417 * We need an update if file doesn't exist in old archive 1418 */ 1419 if (walk_arg.old_nvlp == NULL || 1420 nvlist_lookup_uint64_array(walk_arg.old_nvlp, 1421 file + bam_rootlen, &value, &sz) != 0) { 1422 if (bam_smf_check) /* ignore new during smf check */ 1423 return (0); 1424 walk_arg.need_update = 1; 1425 if (bam_verbose) 1426 bam_print(PARSEABLE_NEW_FILE, file); 1427 return (0); 1428 } 1429 1430 /* 1431 * File exists in old archive. Check if file has changed 1432 */ 1433 assert(sz == 2); 1434 bcopy(value, filestat, sizeof (filestat)); 1435 1436 if (filestat[0] != stat->st_size || 1437 filestat[1] != stat->st_mtime) { 1438 walk_arg.need_update = 1; 1439 if (bam_verbose) 1440 if (bam_smf_check) 1441 bam_print(" %s\n", file); 1442 else 1443 bam_print(PARSEABLE_OUT_DATE, file); 1444 } 1445 1446 return (0); 1447 } 1448 1449 /* 1450 * Check flags and presence of required files. 1451 * The force flag and/or absence of files should 1452 * trigger an update. 1453 * Suppress stdout output if check (-n) option is set 1454 * (as -n should only produce parseable output.) 1455 */ 1456 static void 1457 check_flags_and_files(char *root) 1458 { 1459 char path[PATH_MAX]; 1460 struct stat sb; 1461 1462 /* 1463 * if force, create archive unconditionally 1464 */ 1465 if (bam_force) { 1466 walk_arg.need_update = 1; 1467 if (bam_verbose && !bam_check) 1468 bam_print(UPDATE_FORCE); 1469 return; 1470 } 1471 1472 /* 1473 * If archive is missing, create archive 1474 */ 1475 (void) snprintf(path, sizeof (path), "%s%s", root, 1476 DIRECT_BOOT_ARCHIVE_32); 1477 if (stat(path, &sb) != 0) { 1478 if (bam_verbose && !bam_check) 1479 bam_print(UPDATE_ARCH_MISS, path); 1480 walk_arg.need_update = 1; 1481 return; 1482 } 1483 if (bam_direct == BAM_DIRECT_DBOOT) { 1484 (void) snprintf(path, sizeof (path), "%s%s", root, 1485 DIRECT_BOOT_ARCHIVE_64); 1486 if (stat(path, &sb) != 0) { 1487 if (bam_verbose && !bam_check) 1488 bam_print(UPDATE_ARCH_MISS, path); 1489 walk_arg.need_update = 1; 1490 return; 1491 } 1492 } 1493 } 1494 1495 static error_t 1496 read_one_list(char *root, filelist_t *flistp, char *filelist) 1497 { 1498 char path[PATH_MAX]; 1499 FILE *fp; 1500 char buf[BAM_MAXLINE]; 1501 1502 (void) snprintf(path, sizeof (path), "%s%s", root, filelist); 1503 1504 fp = fopen(path, "r"); 1505 if (fp == NULL) { 1506 if (bam_debug) 1507 bam_error(FLIST_FAIL, path, strerror(errno)); 1508 return (BAM_ERROR); 1509 } 1510 while (s_fgets(buf, sizeof (buf), fp) != NULL) { 1511 /* skip blank lines */ 1512 if (strspn(buf, " \t") == strlen(buf)) 1513 continue; 1514 append_to_flist(flistp, buf); 1515 } 1516 if (fclose(fp) != 0) { 1517 bam_error(CLOSE_FAIL, path, strerror(errno)); 1518 return (BAM_ERROR); 1519 } 1520 return (BAM_SUCCESS); 1521 } 1522 1523 static error_t 1524 read_list(char *root, filelist_t *flistp) 1525 { 1526 int rval; 1527 1528 flistp->head = flistp->tail = NULL; 1529 1530 /* 1531 * Read current lists of files - only the first is mandatory 1532 */ 1533 rval = read_one_list(root, flistp, BOOT_FILE_LIST); 1534 if (rval != BAM_SUCCESS) 1535 return (rval); 1536 (void) read_one_list(root, flistp, ETC_FILE_LIST); 1537 1538 if (flistp->head == NULL) { 1539 bam_error(NO_FLIST); 1540 return (BAM_ERROR); 1541 } 1542 1543 return (BAM_SUCCESS); 1544 } 1545 1546 static void 1547 getoldstat(char *root) 1548 { 1549 char path[PATH_MAX]; 1550 int fd, error; 1551 struct stat sb; 1552 char *ostat; 1553 1554 (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 1555 fd = open(path, O_RDONLY); 1556 if (fd == -1) { 1557 if (bam_verbose) 1558 bam_print(OPEN_FAIL, path, strerror(errno)); 1559 walk_arg.need_update = 1; 1560 return; 1561 } 1562 1563 if (fstat(fd, &sb) != 0) { 1564 bam_error(STAT_FAIL, path, strerror(errno)); 1565 (void) close(fd); 1566 walk_arg.need_update = 1; 1567 return; 1568 } 1569 1570 ostat = s_calloc(1, sb.st_size); 1571 1572 if (read(fd, ostat, sb.st_size) != sb.st_size) { 1573 bam_error(READ_FAIL, path, strerror(errno)); 1574 (void) close(fd); 1575 free(ostat); 1576 walk_arg.need_update = 1; 1577 return; 1578 } 1579 1580 (void) close(fd); 1581 1582 walk_arg.old_nvlp = NULL; 1583 error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0); 1584 1585 free(ostat); 1586 1587 if (error) { 1588 bam_error(UNPACK_FAIL, path, strerror(error)); 1589 walk_arg.old_nvlp = NULL; 1590 walk_arg.need_update = 1; 1591 return; 1592 } 1593 } 1594 1595 /* 1596 * Checks if a file in the current (old) archive has 1597 * been deleted from the root filesystem. This is needed for 1598 * software like Trusted Extensions (TX) that switch early 1599 * in boot based on presence/absence of a kernel module. 1600 */ 1601 static void 1602 check4stale(char *root) 1603 { 1604 nvpair_t *nvp; 1605 nvlist_t *nvlp; 1606 char *file; 1607 char path[PATH_MAX]; 1608 struct stat sb; 1609 1610 /* 1611 * Skip stale file check during smf check 1612 */ 1613 if (bam_smf_check) 1614 return; 1615 1616 /* Nothing to do if no old stats */ 1617 if ((nvlp = walk_arg.old_nvlp) == NULL) 1618 return; 1619 1620 for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp; 1621 nvp = nvlist_next_nvpair(nvlp, nvp)) { 1622 file = nvpair_name(nvp); 1623 if (file == NULL) 1624 continue; 1625 (void) snprintf(path, sizeof (path), "%s/%s", 1626 root, file); 1627 if (stat(path, &sb) == -1) { 1628 walk_arg.need_update = 1; 1629 if (bam_verbose) 1630 bam_print(PARSEABLE_STALE_FILE, path); 1631 } 1632 } 1633 } 1634 1635 static void 1636 create_newstat(void) 1637 { 1638 int error; 1639 1640 error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0); 1641 if (error) { 1642 /* 1643 * Not fatal - we can still create archive 1644 */ 1645 walk_arg.new_nvlp = NULL; 1646 bam_error(NVALLOC_FAIL, strerror(error)); 1647 } 1648 } 1649 1650 static void 1651 walk_list(char *root, filelist_t *flistp) 1652 { 1653 char path[PATH_MAX]; 1654 line_t *lp; 1655 1656 for (lp = flistp->head; lp; lp = lp->next) { 1657 (void) snprintf(path, sizeof (path), "%s%s", root, lp->line); 1658 /* XXX shouldn't we use FTW_MOUNT ? */ 1659 if (nftw(path, cmpstat, 20, 0) == -1) { 1660 /* 1661 * Some files may not exist. 1662 * For example: etc/rtc_config on a x86 diskless system 1663 * Emit verbose message only 1664 */ 1665 if (bam_verbose) 1666 bam_print(NFTW_FAIL, path, strerror(errno)); 1667 } 1668 } 1669 } 1670 1671 static void 1672 savenew(char *root) 1673 { 1674 char path[PATH_MAX]; 1675 char path2[PATH_MAX]; 1676 size_t sz; 1677 char *nstat; 1678 int fd, wrote, error; 1679 1680 nstat = NULL; 1681 sz = 0; 1682 error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz, 1683 NV_ENCODE_XDR, 0); 1684 if (error) { 1685 bam_error(PACK_FAIL, strerror(error)); 1686 return; 1687 } 1688 1689 (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP); 1690 fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE); 1691 if (fd == -1) { 1692 bam_error(OPEN_FAIL, path, strerror(errno)); 1693 free(nstat); 1694 return; 1695 } 1696 wrote = write(fd, nstat, sz); 1697 if (wrote != sz) { 1698 bam_error(WRITE_FAIL, path, strerror(errno)); 1699 (void) close(fd); 1700 free(nstat); 1701 return; 1702 } 1703 (void) close(fd); 1704 free(nstat); 1705 1706 (void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT); 1707 if (rename(path, path2) != 0) { 1708 bam_error(RENAME_FAIL, path2, strerror(errno)); 1709 } 1710 } 1711 1712 static void 1713 clear_walk_args(void) 1714 { 1715 if (walk_arg.old_nvlp) 1716 nvlist_free(walk_arg.old_nvlp); 1717 if (walk_arg.new_nvlp) 1718 nvlist_free(walk_arg.new_nvlp); 1719 walk_arg.need_update = 0; 1720 walk_arg.old_nvlp = NULL; 1721 walk_arg.new_nvlp = NULL; 1722 } 1723 1724 /* 1725 * Returns: 1726 * 0 - no update necessary 1727 * 1 - update required. 1728 * BAM_ERROR (-1) - An error occurred 1729 * 1730 * Special handling for check (-n): 1731 * ================================ 1732 * The check (-n) option produces parseable output. 1733 * To do this, we suppress all stdout messages unrelated 1734 * to out of sync files. 1735 * All stderr messages are still printed though. 1736 * 1737 */ 1738 static int 1739 update_required(char *root) 1740 { 1741 struct stat sb; 1742 char path[PATH_MAX]; 1743 filelist_t flist; 1744 filelist_t *flistp = &flist; 1745 int need_update; 1746 1747 flistp->head = flistp->tail = NULL; 1748 1749 walk_arg.need_update = 0; 1750 1751 /* 1752 * Without consulting stat data, check if we need update 1753 */ 1754 check_flags_and_files(root); 1755 1756 /* 1757 * In certain deployment scenarios, filestat may not 1758 * exist. Ignore it during boot-archive SMF check. 1759 */ 1760 if (bam_smf_check) { 1761 (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 1762 if (stat(path, &sb) != 0) 1763 return (0); 1764 } 1765 1766 /* 1767 * consult stat data only if we haven't made a decision 1768 * about update. If checking (-n) however, we always 1769 * need stat data (since we want to compare old and new) 1770 */ 1771 if (!walk_arg.need_update || bam_check) 1772 getoldstat(root); 1773 1774 /* 1775 * Check if the archive contains files that are no longer 1776 * present on the root filesystem. 1777 */ 1778 if (!walk_arg.need_update || bam_check) 1779 check4stale(root); 1780 1781 /* 1782 * read list of files 1783 */ 1784 if (read_list(root, flistp) != BAM_SUCCESS) { 1785 clear_walk_args(); 1786 return (BAM_ERROR); 1787 } 1788 1789 assert(flistp->head && flistp->tail); 1790 1791 /* 1792 * At this point either the update is required 1793 * or the decision is pending. In either case 1794 * we need to create new stat nvlist 1795 */ 1796 create_newstat(); 1797 1798 /* 1799 * This walk does 2 things: 1800 * - gets new stat data for every file 1801 * - (optional) compare old and new stat data 1802 */ 1803 walk_list(root, &flist); 1804 1805 /* done with the file list */ 1806 filelist_free(flistp); 1807 1808 /* 1809 * if we didn't succeed in creating new stat data above 1810 * just return result of update check so that archive is built. 1811 */ 1812 if (walk_arg.new_nvlp == NULL) { 1813 bam_error(NO_NEW_STAT); 1814 need_update = walk_arg.need_update; 1815 clear_walk_args(); 1816 return (need_update ? 1 : 0); 1817 } 1818 1819 1820 /* 1821 * If no update required, discard newstat 1822 */ 1823 if (!walk_arg.need_update) { 1824 clear_walk_args(); 1825 return (0); 1826 } 1827 1828 /* 1829 * At this point we need an update - so save new stat data 1830 * However, if only checking (-n), don't save new stat data. 1831 */ 1832 if (!bam_check) 1833 savenew(root); 1834 1835 clear_walk_args(); 1836 1837 return (1); 1838 } 1839 1840 static error_t 1841 create_ramdisk(char *root) 1842 { 1843 char *cmdline, path[PATH_MAX]; 1844 size_t len; 1845 struct stat sb; 1846 1847 /* 1848 * Setup command args for create_ramdisk.ksh 1849 */ 1850 (void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK); 1851 if (stat(path, &sb) != 0) { 1852 bam_error(ARCH_EXEC_MISS, path, strerror(errno)); 1853 return (BAM_ERROR); 1854 } 1855 1856 len = strlen(path) + strlen(root) + 10; /* room for space + -R */ 1857 cmdline = s_calloc(1, len); 1858 1859 if (strlen(root) > 1) { 1860 (void) snprintf(cmdline, len, "%s -R %s", path, root); 1861 /* chop off / at the end */ 1862 cmdline[strlen(cmdline) - 1] = '\0'; 1863 } else 1864 (void) snprintf(cmdline, len, "%s", path); 1865 1866 if (exec_cmd(cmdline, NULL, 0) != 0) { 1867 bam_error(ARCHIVE_FAIL, cmdline); 1868 free(cmdline); 1869 return (BAM_ERROR); 1870 } 1871 free(cmdline); 1872 1873 /* 1874 * Verify that the archive has been created 1875 */ 1876 (void) snprintf(path, sizeof (path), "%s%s", root, 1877 DIRECT_BOOT_ARCHIVE_32); 1878 if (stat(path, &sb) != 0) { 1879 bam_error(ARCHIVE_NOT_CREATED, path); 1880 return (BAM_ERROR); 1881 } 1882 if (bam_direct == BAM_DIRECT_DBOOT) { 1883 (void) snprintf(path, sizeof (path), "%s%s", root, 1884 DIRECT_BOOT_ARCHIVE_64); 1885 if (stat(path, &sb) != 0) { 1886 bam_error(ARCHIVE_NOT_CREATED, path); 1887 return (BAM_ERROR); 1888 } 1889 } 1890 1891 return (BAM_SUCCESS); 1892 } 1893 1894 /* 1895 * Checks if target filesystem is on a ramdisk 1896 * 1 - is miniroot 1897 * 0 - is not 1898 * When in doubt assume it is not a ramdisk. 1899 */ 1900 static int 1901 is_ramdisk(char *root) 1902 { 1903 struct extmnttab mnt; 1904 FILE *fp; 1905 int found; 1906 char mntpt[PATH_MAX]; 1907 char *cp; 1908 1909 /* 1910 * There are 3 situations where creating archive is 1911 * of dubious value: 1912 * - create boot_archive on a lofi-mounted boot_archive 1913 * - create it on a ramdisk which is the root filesystem 1914 * - create it on a ramdisk mounted somewhere else 1915 * The first is not easy to detect and checking for it is not 1916 * worth it. 1917 * The other two conditions are handled here 1918 */ 1919 1920 fp = fopen(MNTTAB, "r"); 1921 if (fp == NULL) { 1922 bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 1923 return (0); 1924 } 1925 1926 resetmnttab(fp); 1927 1928 /* 1929 * Remove any trailing / from the mount point 1930 */ 1931 (void) strlcpy(mntpt, root, sizeof (mntpt)); 1932 if (strcmp(root, "/") != 0) { 1933 cp = mntpt + strlen(mntpt) - 1; 1934 if (*cp == '/') 1935 *cp = '\0'; 1936 } 1937 found = 0; 1938 while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 1939 if (strcmp(mnt.mnt_mountp, mntpt) == 0) { 1940 found = 1; 1941 break; 1942 } 1943 } 1944 1945 if (!found) { 1946 if (bam_verbose) 1947 bam_error(NOT_IN_MNTTAB, mntpt); 1948 (void) fclose(fp); 1949 return (0); 1950 } 1951 1952 if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) { 1953 if (bam_verbose) 1954 bam_error(IS_RAMDISK, bam_root); 1955 (void) fclose(fp); 1956 return (1); 1957 } 1958 1959 (void) fclose(fp); 1960 1961 return (0); 1962 } 1963 1964 static int 1965 is_newboot(char *root) 1966 { 1967 char path[PATH_MAX]; 1968 struct stat sb; 1969 1970 /* 1971 * We can't boot without MULTI_BOOT 1972 */ 1973 (void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT); 1974 if (stat(path, &sb) == -1) { 1975 if (bam_verbose) 1976 bam_print(FILE_MISS, path); 1977 return (0); 1978 } 1979 1980 /* 1981 * We can't generate archive without GRUB_DIR 1982 */ 1983 (void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR); 1984 if (stat(path, &sb) == -1) { 1985 if (bam_verbose) 1986 bam_print(DIR_MISS, path); 1987 return (0); 1988 } 1989 1990 return (1); 1991 } 1992 1993 static int 1994 is_readonly(char *root) 1995 { 1996 struct statvfs vfs; 1997 1998 /* 1999 * Check for RDONLY filesystem 2000 * When in doubt assume it is not readonly 2001 */ 2002 if (statvfs(root, &vfs) != 0) { 2003 if (bam_verbose) 2004 bam_error(STATVFS_FAIL, root, strerror(errno)); 2005 return (0); 2006 } 2007 2008 if (vfs.f_flag & ST_RDONLY) { 2009 return (1); 2010 } 2011 2012 return (0); 2013 } 2014 2015 static error_t 2016 update_archive(char *root, char *opt) 2017 { 2018 error_t ret; 2019 2020 assert(root); 2021 assert(opt == NULL); 2022 2023 /* 2024 * root must belong to a GRUB boot OS, 2025 * don't care on sparc except for diskless clients 2026 */ 2027 if (!is_newboot(root)) { 2028 /* 2029 * Emit message only if not in context of update_all. 2030 * If in update_all, emit only if verbose flag is set. 2031 */ 2032 if (!bam_update_all || bam_verbose) 2033 bam_print(NOT_GRUB_BOOT, root); 2034 return (BAM_SUCCESS); 2035 } 2036 2037 /* 2038 * If smf check is requested when / is writable (can happen 2039 * on first reboot following an upgrade because service 2040 * dependency is messed up), skip the check. 2041 */ 2042 if (bam_smf_check && !bam_root_readonly) 2043 return (BAM_SUCCESS); 2044 2045 /* 2046 * root must be writable. This check applies to alternate 2047 * root (-R option); bam_root_readonly applies to '/' only. 2048 * Note: statvfs() does not always report the truth 2049 */ 2050 if (!bam_smf_check && !bam_check && is_readonly(root)) { 2051 if (bam_verbose) 2052 bam_print(RDONLY_FS, root); 2053 return (BAM_SUCCESS); 2054 } 2055 2056 /* 2057 * Don't generate archive on ramdisk 2058 */ 2059 if (is_ramdisk(root)) { 2060 if (bam_verbose) 2061 bam_print(SKIP_RAMDISK); 2062 return (BAM_SUCCESS); 2063 } 2064 2065 /* 2066 * Now check if updated is really needed 2067 */ 2068 ret = update_required(root); 2069 2070 /* 2071 * The check command (-n) is *not* a dry run 2072 * It only checks if the archive is in sync. 2073 */ 2074 if (bam_check) { 2075 bam_exit((ret != 0) ? 1 : 0); 2076 } 2077 2078 if (ret == 1) { 2079 /* create the ramdisk */ 2080 ret = create_ramdisk(root); 2081 } 2082 return (ret); 2083 } 2084 2085 static void 2086 update_fdisk(void) 2087 { 2088 struct stat sb; 2089 char cmd[PATH_MAX]; 2090 int ret1, ret2; 2091 2092 assert(stat(GRUB_fdisk, &sb) == 0); 2093 assert(stat(GRUB_fdisk_target, &sb) == 0); 2094 2095 (void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`", 2096 GRUB_fdisk, GRUB_fdisk_target); 2097 2098 bam_print(UPDATING_FDISK); 2099 if (exec_cmd(cmd, NULL, 0) != 0) { 2100 bam_error(FDISK_UPDATE_FAILED); 2101 } 2102 2103 /* 2104 * We are done, remove the files. 2105 */ 2106 ret1 = unlink(GRUB_fdisk); 2107 ret2 = unlink(GRUB_fdisk_target); 2108 if (ret1 != 0 || ret2 != 0) { 2109 bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target); 2110 } 2111 } 2112 2113 static void 2114 restore_grub_slice(void) 2115 { 2116 struct stat sb; 2117 char *mntpt, *physlice; 2118 int mnted; /* set if we did a mount */ 2119 char menupath[PATH_MAX], cmd[PATH_MAX]; 2120 2121 if (stat(GRUB_slice, &sb) != 0) { 2122 bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 2123 return; 2124 } 2125 2126 /* 2127 * If we are doing an luactivate, don't attempt to restore GRUB or else 2128 * we may not be able to get to DCA boot environments. Let luactivate 2129 * handle GRUB/DCA installation 2130 */ 2131 if (stat(LU_ACTIVATE_FILE, &sb) == 0) { 2132 return; 2133 } 2134 2135 mnted = 0; 2136 physlice = NULL; 2137 mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL); 2138 if (mntpt == NULL) { 2139 bam_error(CANNOT_RESTORE_GRUB_SLICE); 2140 return; 2141 } 2142 2143 (void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU); 2144 if (stat(menupath, &sb) == 0) { 2145 umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2146 return; 2147 } 2148 2149 /* 2150 * The menu is missing - we need to do a restore 2151 */ 2152 bam_print(RESTORING_GRUB); 2153 2154 (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s", 2155 INSTALLGRUB, STAGE1, STAGE2, physlice); 2156 2157 if (exec_cmd(cmd, NULL, 0) != 0) { 2158 bam_error(RESTORE_GRUB_FAILED); 2159 umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2160 return; 2161 } 2162 2163 if (stat(GRUB_backup_menu, &sb) != 0) { 2164 bam_error(MISSING_BACKUP_MENU, 2165 GRUB_backup_menu, strerror(errno)); 2166 umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2167 return; 2168 } 2169 2170 (void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s", 2171 GRUB_backup_menu, menupath); 2172 2173 if (exec_cmd(cmd, NULL, 0) != 0) { 2174 bam_error(RESTORE_MENU_FAILED, menupath); 2175 umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2176 return; 2177 } 2178 2179 /* Success */ 2180 umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2181 } 2182 2183 static error_t 2184 update_all(char *root, char *opt) 2185 { 2186 struct extmnttab mnt; 2187 struct stat sb; 2188 FILE *fp; 2189 char multibt[PATH_MAX]; 2190 error_t ret = BAM_SUCCESS; 2191 int ret1, ret2; 2192 2193 assert(root); 2194 assert(opt == NULL); 2195 2196 if (bam_rootlen != 1 || *root != '/') { 2197 elide_trailing_slash(root, multibt, sizeof (multibt)); 2198 bam_error(ALT_ROOT_INVALID, multibt); 2199 return (BAM_ERROR); 2200 } 2201 2202 /* 2203 * First update archive for current root 2204 */ 2205 if (update_archive(root, opt) != BAM_SUCCESS) 2206 ret = BAM_ERROR; 2207 2208 /* 2209 * Now walk the mount table, performing archive update 2210 * for all mounted Newboot root filesystems 2211 */ 2212 fp = fopen(MNTTAB, "r"); 2213 if (fp == NULL) { 2214 bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 2215 ret = BAM_ERROR; 2216 goto out; 2217 } 2218 2219 resetmnttab(fp); 2220 2221 while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 2222 if (mnt.mnt_special == NULL) 2223 continue; 2224 if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0) 2225 continue; 2226 if (strcmp(mnt.mnt_mountp, "/") == 0) 2227 continue; 2228 2229 (void) snprintf(multibt, sizeof (multibt), "%s%s", 2230 mnt.mnt_mountp, MULTI_BOOT); 2231 2232 if (stat(multibt, &sb) == -1) 2233 continue; 2234 2235 /* 2236 * We put a trailing slash to be consistent with root = "/" 2237 * case, such that we don't have to print // in some cases. 2238 */ 2239 (void) snprintf(rootbuf, sizeof (rootbuf), "%s/", 2240 mnt.mnt_mountp); 2241 bam_rootlen = strlen(rootbuf); 2242 2243 /* 2244 * It's possible that other mounts may be an alternate boot 2245 * architecture, so check it again. 2246 */ 2247 if ((dboot_or_multiboot(rootbuf) != BAM_SUCCESS) || 2248 (update_archive(rootbuf, opt) != BAM_SUCCESS)) 2249 ret = BAM_ERROR; 2250 } 2251 2252 (void) fclose(fp); 2253 2254 out: 2255 if (stat(GRUB_slice, &sb) == 0) { 2256 restore_grub_slice(); 2257 } 2258 2259 /* 2260 * Update fdisk table as we go down. Updating it when 2261 * the system is running will confuse biosdev. 2262 */ 2263 ret1 = stat(GRUB_fdisk, &sb); 2264 ret2 = stat(GRUB_fdisk_target, &sb); 2265 if ((ret1 == 0) && (ret2 == 0)) { 2266 update_fdisk(); 2267 } else if ((ret1 == 0) ^ (ret2 == 0)) { 2268 /* 2269 * It is an error for one file to be 2270 * present and the other absent. 2271 * It is normal for both files to be 2272 * absent - it indicates that no fdisk 2273 * update is required. 2274 */ 2275 bam_error(MISSING_FDISK_FILE, 2276 ret1 ? GRUB_fdisk : GRUB_fdisk_target); 2277 ret = BAM_ERROR; 2278 } 2279 2280 return (ret); 2281 } 2282 2283 static void 2284 append_line(menu_t *mp, line_t *lp) 2285 { 2286 if (mp->start == NULL) { 2287 mp->start = lp; 2288 } else { 2289 mp->end->next = lp; 2290 lp->prev = mp->end; 2291 } 2292 mp->end = lp; 2293 } 2294 2295 static void 2296 unlink_line(menu_t *mp, line_t *lp) 2297 { 2298 /* unlink from list */ 2299 if (lp->prev) 2300 lp->prev->next = lp->next; 2301 else 2302 mp->start = lp->next; 2303 if (lp->next) 2304 lp->next->prev = lp->prev; 2305 else 2306 mp->end = lp->prev; 2307 } 2308 2309 static entry_t * 2310 boot_entry_new(menu_t *mp, line_t *start, line_t *end) 2311 { 2312 entry_t *ent, *prev; 2313 2314 ent = s_calloc(1, sizeof (entry_t)); 2315 ent->start = start; 2316 ent->end = end; 2317 2318 if (mp->entries == NULL) { 2319 mp->entries = ent; 2320 return (ent); 2321 } 2322 2323 prev = mp->entries; 2324 while (prev->next) 2325 prev = prev-> next; 2326 prev->next = ent; 2327 ent->prev = prev; 2328 return (ent); 2329 } 2330 2331 static void 2332 boot_entry_addline(entry_t *ent, line_t *lp) 2333 { 2334 if (ent) 2335 ent->end = lp; 2336 } 2337 2338 /* 2339 * A line in menu.lst looks like 2340 * [ ]*<cmd>[ \t=]*<arg>* 2341 */ 2342 static void 2343 line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum) 2344 { 2345 /* 2346 * save state across calls. This is so that 2347 * header gets the right entry# after title has 2348 * been processed 2349 */ 2350 static line_t *prev = NULL; 2351 static entry_t *curr_ent = NULL; 2352 static int in_liveupgrade = 0; 2353 2354 line_t *lp; 2355 char *cmd, *sep, *arg; 2356 char save, *cp, *line; 2357 menu_flag_t flag = BAM_INVALID; 2358 2359 if (str == NULL) { 2360 return; 2361 } 2362 2363 /* 2364 * First save a copy of the entire line. 2365 * We use this later to set the line field. 2366 */ 2367 line = s_strdup(str); 2368 2369 /* Eat up leading whitespace */ 2370 while (*str == ' ' || *str == '\t') 2371 str++; 2372 2373 if (*str == '#') { /* comment */ 2374 cmd = s_strdup("#"); 2375 sep = NULL; 2376 arg = s_strdup(str + 1); 2377 flag = BAM_COMMENT; 2378 if (strstr(arg, BAM_LU_HDR) != NULL) { 2379 in_liveupgrade = 1; 2380 } else if (strstr(arg, BAM_LU_FTR) != NULL) { 2381 in_liveupgrade = 0; 2382 } 2383 } else if (*str == '\0') { /* blank line */ 2384 cmd = sep = arg = NULL; 2385 flag = BAM_EMPTY; 2386 } else { 2387 /* 2388 * '=' is not a documented separator in grub syntax. 2389 * However various development bits use '=' as a 2390 * separator. In addition, external users also 2391 * use = as a separator. So we will allow that usage. 2392 */ 2393 cp = str; 2394 while (*str != ' ' && *str != '\t' && *str != '=') { 2395 if (*str == '\0') { 2396 cmd = s_strdup(cp); 2397 sep = arg = NULL; 2398 break; 2399 } 2400 str++; 2401 } 2402 2403 if (*str != '\0') { 2404 save = *str; 2405 *str = '\0'; 2406 cmd = s_strdup(cp); 2407 *str = save; 2408 2409 str++; 2410 save = *str; 2411 *str = '\0'; 2412 sep = s_strdup(str - 1); 2413 *str = save; 2414 2415 while (*str == ' ' || *str == '\t') 2416 str++; 2417 if (*str == '\0') 2418 arg = NULL; 2419 else 2420 arg = s_strdup(str); 2421 } 2422 } 2423 2424 lp = s_calloc(1, sizeof (line_t)); 2425 2426 lp->cmd = cmd; 2427 lp->sep = sep; 2428 lp->arg = arg; 2429 lp->line = line; 2430 lp->lineNum = ++(*lineNum); 2431 if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) { 2432 lp->entryNum = ++(*entryNum); 2433 lp->flags = BAM_TITLE; 2434 if (prev && prev->flags == BAM_COMMENT && 2435 prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) { 2436 prev->entryNum = lp->entryNum; 2437 curr_ent = boot_entry_new(mp, prev, lp); 2438 curr_ent->flags = BAM_ENTRY_BOOTADM; 2439 } else { 2440 curr_ent = boot_entry_new(mp, lp, lp); 2441 if (in_liveupgrade) { 2442 curr_ent->flags = BAM_ENTRY_LU; 2443 } 2444 } 2445 curr_ent->entryNum = *entryNum; 2446 } else if (flag != BAM_INVALID) { 2447 /* 2448 * For header comments, the entry# is "fixed up" 2449 * by the subsequent title 2450 */ 2451 lp->entryNum = *entryNum; 2452 lp->flags = flag; 2453 } else { 2454 lp->entryNum = *entryNum; 2455 2456 if (*entryNum == ENTRY_INIT) { 2457 lp->flags = BAM_GLOBAL; 2458 } else { 2459 lp->flags = BAM_ENTRY; 2460 2461 if (cmd && arg) { 2462 /* 2463 * We only compare for the length of "module" 2464 * so that "module$" will also match. 2465 */ 2466 if ((strncmp(cmd, menu_cmds[MODULE_CMD], 2467 strlen(menu_cmds[MODULE_CMD])) == 0) && 2468 (strcmp(arg, MINIROOT) == 0)) 2469 curr_ent->flags |= BAM_ENTRY_MINIROOT; 2470 else if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0) 2471 curr_ent->flags |= BAM_ENTRY_ROOT; 2472 else if (strcmp(cmd, 2473 menu_cmds[CHAINLOADER_CMD]) == 0) 2474 curr_ent->flags |= 2475 BAM_ENTRY_CHAINLOADER; 2476 } 2477 } 2478 } 2479 2480 /* record default, old default, and entry line ranges */ 2481 if (lp->flags == BAM_GLOBAL && 2482 strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) { 2483 mp->curdefault = lp; 2484 } else if (lp->flags == BAM_COMMENT && 2485 strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) { 2486 mp->olddefault = lp; 2487 } else if (lp->flags == BAM_COMMENT && 2488 strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) { 2489 mp->old_rc_default = lp; 2490 } else if (lp->flags == BAM_ENTRY || 2491 (lp->flags == BAM_COMMENT && 2492 strcmp(lp->arg, BAM_BOOTADM_FTR) == 0)) { 2493 boot_entry_addline(curr_ent, lp); 2494 } 2495 append_line(mp, lp); 2496 2497 prev = lp; 2498 } 2499 2500 static void 2501 update_numbering(menu_t *mp) 2502 { 2503 int lineNum; 2504 int entryNum; 2505 int old_default_value; 2506 line_t *lp, *prev, *default_lp, *default_entry; 2507 char buf[PATH_MAX]; 2508 2509 if (mp->start == NULL) { 2510 return; 2511 } 2512 2513 lineNum = LINE_INIT; 2514 entryNum = ENTRY_INIT; 2515 old_default_value = ENTRY_INIT; 2516 lp = default_lp = default_entry = NULL; 2517 2518 prev = NULL; 2519 for (lp = mp->start; lp; prev = lp, lp = lp->next) { 2520 lp->lineNum = ++lineNum; 2521 2522 /* 2523 * Get the value of the default command 2524 */ 2525 if (lp->entryNum == ENTRY_INIT && lp->cmd && 2526 strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 && 2527 lp->arg) { 2528 old_default_value = atoi(lp->arg); 2529 default_lp = lp; 2530 } 2531 2532 /* 2533 * If not boot entry, nothing else to fix for this 2534 * entry 2535 */ 2536 if (lp->entryNum == ENTRY_INIT) 2537 continue; 2538 2539 /* 2540 * Record the position of the default entry. 2541 * The following works because global 2542 * commands like default and timeout should precede 2543 * actual boot entries, so old_default_value 2544 * is already known (or default cmd is missing). 2545 */ 2546 if (default_entry == NULL && 2547 old_default_value != ENTRY_INIT && 2548 lp->entryNum == old_default_value) { 2549 default_entry = lp; 2550 } 2551 2552 /* 2553 * Now fixup the entry number 2554 */ 2555 if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) { 2556 lp->entryNum = ++entryNum; 2557 /* fixup the bootadm header */ 2558 if (prev && prev->flags == BAM_COMMENT && 2559 prev->arg && 2560 strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) { 2561 prev->entryNum = lp->entryNum; 2562 } 2563 } else { 2564 lp->entryNum = entryNum; 2565 } 2566 } 2567 2568 /* 2569 * No default command in menu, simply return 2570 */ 2571 if (default_lp == NULL) { 2572 return; 2573 } 2574 2575 free(default_lp->arg); 2576 free(default_lp->line); 2577 2578 if (default_entry == NULL) { 2579 default_lp->arg = s_strdup("0"); 2580 } else { 2581 (void) snprintf(buf, sizeof (buf), "%d", 2582 default_entry->entryNum); 2583 default_lp->arg = s_strdup(buf); 2584 } 2585 2586 /* 2587 * The following is required since only the line field gets 2588 * written back to menu.lst 2589 */ 2590 (void) snprintf(buf, sizeof (buf), "%s%s%s", 2591 menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg); 2592 default_lp->line = s_strdup(buf); 2593 } 2594 2595 2596 static menu_t * 2597 menu_read(char *menu_path) 2598 { 2599 FILE *fp; 2600 char buf[BAM_MAXLINE], *cp; 2601 menu_t *mp; 2602 int line, entry, len, n; 2603 2604 mp = s_calloc(1, sizeof (menu_t)); 2605 2606 fp = fopen(menu_path, "r"); 2607 if (fp == NULL) { /* Let the caller handle this error */ 2608 return (mp); 2609 } 2610 2611 2612 /* Note: GRUB boot entry number starts with 0 */ 2613 line = LINE_INIT; 2614 entry = ENTRY_INIT; 2615 cp = buf; 2616 len = sizeof (buf); 2617 while (s_fgets(cp, len, fp) != NULL) { 2618 n = strlen(cp); 2619 if (cp[n - 1] == '\\') { 2620 len -= n - 1; 2621 assert(len >= 2); 2622 cp += n - 1; 2623 continue; 2624 } 2625 line_parser(mp, buf, &line, &entry); 2626 cp = buf; 2627 len = sizeof (buf); 2628 } 2629 2630 if (fclose(fp) == EOF) { 2631 bam_error(CLOSE_FAIL, menu_path, strerror(errno)); 2632 } 2633 2634 return (mp); 2635 } 2636 2637 static error_t 2638 selector(menu_t *mp, char *opt, int *entry, char **title) 2639 { 2640 char *eq; 2641 char *opt_dup; 2642 int entryNum; 2643 2644 assert(mp); 2645 assert(mp->start); 2646 assert(opt); 2647 2648 opt_dup = s_strdup(opt); 2649 2650 if (entry) 2651 *entry = ENTRY_INIT; 2652 if (title) 2653 *title = NULL; 2654 2655 eq = strchr(opt_dup, '='); 2656 if (eq == NULL) { 2657 bam_error(INVALID_OPT, opt); 2658 free(opt_dup); 2659 return (BAM_ERROR); 2660 } 2661 2662 *eq = '\0'; 2663 if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) { 2664 assert(mp->end); 2665 entryNum = s_strtol(eq + 1); 2666 if (entryNum < 0 || entryNum > mp->end->entryNum) { 2667 bam_error(INVALID_ENTRY, eq + 1); 2668 free(opt_dup); 2669 return (BAM_ERROR); 2670 } 2671 *entry = entryNum; 2672 } else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) { 2673 *title = opt + (eq - opt_dup) + 1; 2674 } else { 2675 bam_error(INVALID_OPT, opt); 2676 free(opt_dup); 2677 return (BAM_ERROR); 2678 } 2679 2680 free(opt_dup); 2681 return (BAM_SUCCESS); 2682 } 2683 2684 /* 2685 * If invoked with no titles/entries (opt == NULL) 2686 * only title lines in file are printed. 2687 * 2688 * If invoked with a title or entry #, all 2689 * lines in *every* matching entry are listed 2690 */ 2691 static error_t 2692 list_entry(menu_t *mp, char *menu_path, char *opt) 2693 { 2694 line_t *lp; 2695 int entry = ENTRY_INIT; 2696 int found; 2697 char *title = NULL; 2698 2699 assert(mp); 2700 assert(menu_path); 2701 2702 if (mp->start == NULL) { 2703 bam_error(NO_MENU, menu_path); 2704 return (BAM_ERROR); 2705 } 2706 2707 if (opt != NULL) { 2708 if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) { 2709 return (BAM_ERROR); 2710 } 2711 assert((entry != ENTRY_INIT) ^ (title != NULL)); 2712 } else { 2713 (void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0); 2714 (void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0); 2715 } 2716 2717 found = 0; 2718 for (lp = mp->start; lp; lp = lp->next) { 2719 if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY) 2720 continue; 2721 if (opt == NULL && lp->flags == BAM_TITLE) { 2722 bam_print(PRINT_TITLE, lp->entryNum, 2723 lp->arg); 2724 found = 1; 2725 continue; 2726 } 2727 if (entry != ENTRY_INIT && lp->entryNum == entry) { 2728 bam_print(PRINT, lp->line); 2729 found = 1; 2730 continue; 2731 } 2732 2733 /* 2734 * We set the entry value here so that all lines 2735 * in entry get printed. If we subsequently match 2736 * title in other entries, all lines in those 2737 * entries get printed as well. 2738 */ 2739 if (title && lp->flags == BAM_TITLE && lp->arg && 2740 strncmp(title, lp->arg, strlen(title)) == 0) { 2741 bam_print(PRINT, lp->line); 2742 entry = lp->entryNum; 2743 found = 1; 2744 continue; 2745 } 2746 } 2747 2748 if (!found) { 2749 bam_error(NO_MATCH_ENTRY); 2750 return (BAM_ERROR); 2751 } 2752 2753 return (BAM_SUCCESS); 2754 } 2755 2756 static int 2757 add_boot_entry(menu_t *mp, 2758 char *title, 2759 char *root, 2760 char *kernel, 2761 char *module) 2762 { 2763 int lineNum, entryNum; 2764 char linebuf[BAM_MAXLINE]; 2765 menu_cmd_t k_cmd, m_cmd; 2766 2767 assert(mp); 2768 2769 if (title == NULL) { 2770 title = "Solaris"; /* default to Solaris */ 2771 } 2772 if (kernel == NULL) { 2773 bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]); 2774 return (BAM_ERROR); 2775 } 2776 if (module == NULL) { 2777 if (bam_direct != BAM_DIRECT_DBOOT) { 2778 bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]); 2779 return (BAM_ERROR); 2780 } 2781 2782 /* Figure the commands out from the kernel line */ 2783 if (strstr(kernel, "$ISADIR") != NULL) { 2784 module = DIRECT_BOOT_ARCHIVE; 2785 k_cmd = KERNEL_DOLLAR_CMD; 2786 m_cmd = MODULE_DOLLAR_CMD; 2787 } else if (strstr(kernel, "amd64") != NULL) { 2788 module = DIRECT_BOOT_ARCHIVE_64; 2789 k_cmd = KERNEL_CMD; 2790 m_cmd = MODULE_CMD; 2791 } else { 2792 module = DIRECT_BOOT_ARCHIVE_32; 2793 k_cmd = KERNEL_CMD; 2794 m_cmd = MODULE_CMD; 2795 } 2796 } else if ((bam_direct == BAM_DIRECT_DBOOT) && 2797 (strstr(kernel, "$ISADIR") != NULL)) { 2798 /* 2799 * If it's a non-failsafe dboot kernel, use the "kernel$" 2800 * command. Otherwise, use "kernel". 2801 */ 2802 k_cmd = KERNEL_DOLLAR_CMD; 2803 m_cmd = MODULE_DOLLAR_CMD; 2804 } else { 2805 k_cmd = KERNEL_CMD; 2806 m_cmd = MODULE_CMD; 2807 } 2808 2809 if (mp->start) { 2810 lineNum = mp->end->lineNum; 2811 entryNum = mp->end->entryNum; 2812 } else { 2813 lineNum = LINE_INIT; 2814 entryNum = ENTRY_INIT; 2815 } 2816 2817 /* 2818 * No separator for comment (HDR/FTR) commands 2819 * The syntax for comments is #<comment> 2820 */ 2821 (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 2822 menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR); 2823 line_parser(mp, linebuf, &lineNum, &entryNum); 2824 2825 (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 2826 menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 2827 line_parser(mp, linebuf, &lineNum, &entryNum); 2828 2829 if (root) { 2830 (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 2831 menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root); 2832 line_parser(mp, linebuf, &lineNum, &entryNum); 2833 } 2834 2835 (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 2836 menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel); 2837 line_parser(mp, linebuf, &lineNum, &entryNum); 2838 2839 (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 2840 menu_cmds[m_cmd], menu_cmds[SEP_CMD], module); 2841 line_parser(mp, linebuf, &lineNum, &entryNum); 2842 2843 (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 2844 menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR); 2845 line_parser(mp, linebuf, &lineNum, &entryNum); 2846 2847 return (entryNum); 2848 } 2849 2850 static error_t 2851 do_delete(menu_t *mp, int entryNum) 2852 { 2853 line_t *lp, *freed; 2854 entry_t *ent, *tmp; 2855 int deleted; 2856 2857 assert(entryNum != ENTRY_INIT); 2858 2859 ent = mp->entries; 2860 while (ent) { 2861 lp = ent->start; 2862 /* check entry number and make sure it's a bootadm entry */ 2863 if (lp->flags != BAM_COMMENT || 2864 strcmp(lp->arg, BAM_BOOTADM_HDR) != 0 || 2865 (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) { 2866 ent = ent->next; 2867 continue; 2868 } 2869 2870 /* free the entry content */ 2871 do { 2872 freed = lp; 2873 lp = lp->next; /* prev stays the same */ 2874 unlink_line(mp, freed); 2875 line_free(freed); 2876 } while (freed != ent->end); 2877 2878 /* free the entry_t structure */ 2879 tmp = ent; 2880 ent = ent->next; 2881 if (tmp->prev) 2882 tmp->prev->next = ent; 2883 else 2884 mp->entries = ent; 2885 if (ent) 2886 ent->prev = tmp->prev; 2887 deleted = 1; 2888 } 2889 2890 if (!deleted && entryNum != ALL_ENTRIES) { 2891 bam_error(NO_BOOTADM_MATCH); 2892 return (BAM_ERROR); 2893 } 2894 2895 /* 2896 * Now that we have deleted an entry, update 2897 * the entry numbering and the default cmd. 2898 */ 2899 update_numbering(mp); 2900 2901 return (BAM_SUCCESS); 2902 } 2903 2904 static error_t 2905 delete_all_entries(menu_t *mp, char *menu_path, char *opt) 2906 { 2907 assert(mp); 2908 assert(opt == NULL); 2909 2910 if (mp->start == NULL) { 2911 bam_print(EMPTY_FILE, menu_path); 2912 return (BAM_SUCCESS); 2913 } 2914 2915 if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) { 2916 return (BAM_ERROR); 2917 } 2918 2919 return (BAM_WRITE); 2920 } 2921 2922 static FILE * 2923 open_diskmap(char *root) 2924 { 2925 FILE *fp; 2926 char cmd[PATH_MAX]; 2927 2928 /* make sure we have a map file */ 2929 fp = fopen(GRUBDISK_MAP, "r"); 2930 if (fp == NULL) { 2931 (void) snprintf(cmd, sizeof (cmd), 2932 "%s%s > /dev/null", root, CREATE_DISKMAP); 2933 (void) system(cmd); 2934 fp = fopen(GRUBDISK_MAP, "r"); 2935 } 2936 return (fp); 2937 } 2938 2939 #define SECTOR_SIZE 512 2940 2941 static int 2942 get_partition(char *device) 2943 { 2944 int i, fd, is_pcfs, partno = -1; 2945 struct mboot *mboot; 2946 char boot_sect[SECTOR_SIZE]; 2947 char *wholedisk, *slice; 2948 2949 /* form whole disk (p0) */ 2950 slice = device + strlen(device) - 2; 2951 is_pcfs = (*slice != 's'); 2952 if (!is_pcfs) 2953 *slice = '\0'; 2954 wholedisk = s_calloc(1, strlen(device) + 3); 2955 (void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device); 2956 if (!is_pcfs) 2957 *slice = 's'; 2958 2959 /* read boot sector */ 2960 fd = open(wholedisk, O_RDONLY); 2961 free(wholedisk); 2962 if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 2963 return (partno); 2964 } 2965 (void) close(fd); 2966 2967 /* parse fdisk table */ 2968 mboot = (struct mboot *)((void *)boot_sect); 2969 for (i = 0; i < FD_NUMPART; i++) { 2970 struct ipart *part = 2971 (struct ipart *)(uintptr_t)mboot->parts + i; 2972 if (is_pcfs) { /* looking for solaris boot part */ 2973 if (part->systid == 0xbe) { 2974 partno = i; 2975 break; 2976 } 2977 } else { /* look for solaris partition, old and new */ 2978 if (part->systid == SUNIXOS || 2979 part->systid == SUNIXOS2) { 2980 partno = i; 2981 break; 2982 } 2983 } 2984 } 2985 return (partno); 2986 } 2987 2988 static char * 2989 get_grubdisk(char *rootdev, FILE *fp, int on_bootdev) 2990 { 2991 char *grubdisk; /* (hd#,#,#) */ 2992 char *slice; 2993 char *grubhd; 2994 int fdiskpart; 2995 int found = 0; 2996 char *devname, *ctdname = strstr(rootdev, "dsk/"); 2997 char linebuf[PATH_MAX]; 2998 2999 if (ctdname == NULL) 3000 return (NULL); 3001 3002 ctdname += strlen("dsk/"); 3003 slice = strrchr(ctdname, 's'); 3004 if (slice) 3005 *slice = '\0'; 3006 3007 rewind(fp); 3008 while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) { 3009 grubhd = strtok(linebuf, " \t\n"); 3010 if (grubhd) 3011 devname = strtok(NULL, " \t\n"); 3012 else 3013 devname = NULL; 3014 if (devname && strcmp(devname, ctdname) == 0) { 3015 found = 1; 3016 break; 3017 } 3018 } 3019 3020 if (slice) 3021 *slice = 's'; 3022 3023 if (found == 0) { 3024 if (bam_verbose) 3025 bam_print(DISKMAP_FAIL_NONFATAL, rootdev); 3026 grubhd = "0"; /* assume disk 0 if can't match */ 3027 } 3028 3029 fdiskpart = get_partition(rootdev); 3030 if (fdiskpart == -1) 3031 return (NULL); 3032 3033 grubdisk = s_calloc(1, 10); 3034 if (slice) { 3035 (void) snprintf(grubdisk, 10, "(hd%s,%d,%c)", 3036 grubhd, fdiskpart, slice[1] + 'a' - '0'); 3037 } else 3038 (void) snprintf(grubdisk, 10, "(hd%s,%d)", 3039 grubhd, fdiskpart); 3040 3041 /* if root not on bootdev, change GRUB disk to 0 */ 3042 if (!on_bootdev) 3043 grubdisk[3] = '0'; 3044 return (grubdisk); 3045 } 3046 3047 static char * 3048 get_title(char *rootdir) 3049 { 3050 static char title[80]; /* from /etc/release */ 3051 char *cp = NULL, release[PATH_MAX]; 3052 FILE *fp; 3053 3054 /* open the /etc/release file */ 3055 (void) snprintf(release, sizeof (release), "%s/etc/release", rootdir); 3056 3057 fp = fopen(release, "r"); 3058 if (fp == NULL) 3059 return (NULL); 3060 3061 while (s_fgets(title, sizeof (title), fp) != NULL) { 3062 cp = strstr(title, "Solaris"); 3063 if (cp) 3064 break; 3065 } 3066 (void) fclose(fp); 3067 return (cp == NULL ? "Solaris" : cp); 3068 } 3069 3070 char * 3071 get_special(char *mountp) 3072 { 3073 FILE *mntfp; 3074 struct mnttab mp = {0}, mpref = {0}; 3075 3076 mntfp = fopen(MNTTAB, "r"); 3077 if (mntfp == NULL) { 3078 return (0); 3079 } 3080 3081 if (*mountp == '\0') 3082 mpref.mnt_mountp = "/"; 3083 else 3084 mpref.mnt_mountp = mountp; 3085 if (getmntany(mntfp, &mp, &mpref) != 0) { 3086 (void) fclose(mntfp); 3087 return (NULL); 3088 } 3089 (void) fclose(mntfp); 3090 3091 return (s_strdup(mp.mnt_special)); 3092 } 3093 3094 char * 3095 os_to_grubdisk(char *osdisk, int on_bootdev) 3096 { 3097 FILE *fp; 3098 char *grubdisk; 3099 3100 /* translate /dev/dsk name to grub disk name */ 3101 fp = open_diskmap(""); 3102 if (fp == NULL) { 3103 bam_error(DISKMAP_FAIL, osdisk); 3104 return (NULL); 3105 } 3106 grubdisk = get_grubdisk(osdisk, fp, on_bootdev); 3107 (void) fclose(fp); 3108 return (grubdisk); 3109 } 3110 3111 /* 3112 * Check if root is on the boot device 3113 * Return 0 (false) on error 3114 */ 3115 static int 3116 menu_on_bootdev(char *menu_root, FILE *fp) 3117 { 3118 int ret; 3119 char *grubhd, *bootp, *special; 3120 3121 special = get_special(menu_root); 3122 if (special == NULL) 3123 return (0); 3124 bootp = strstr(special, "p0:boot"); 3125 if (bootp) 3126 *bootp = '\0'; 3127 grubhd = get_grubdisk(special, fp, 1); 3128 free(special); 3129 3130 if (grubhd == NULL) 3131 return (0); 3132 ret = grubhd[3] == '0'; 3133 free(grubhd); 3134 return (ret); 3135 } 3136 3137 /* 3138 * look for matching bootadm entry with specified parameters 3139 * Here are the rules (based on existing usage): 3140 * - If title is specified, match on title only 3141 * - Else, match on grubdisk and module (don't care about kernel line). 3142 * note that, if root_opt is non-zero, the absence of root line is 3143 * considered a match. 3144 */ 3145 static entry_t * 3146 find_boot_entry(menu_t *mp, char *title, char *root, char *module, 3147 int root_opt, int *entry_num) 3148 { 3149 int i; 3150 line_t *lp; 3151 entry_t *ent; 3152 3153 /* find matching entry */ 3154 for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) { 3155 lp = ent->start; 3156 3157 /* first line of entry must be bootadm comment */ 3158 lp = ent->start; 3159 if (lp->flags != BAM_COMMENT || 3160 strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) { 3161 continue; 3162 } 3163 3164 /* advance to title line */ 3165 lp = lp->next; 3166 if (title) { 3167 if (lp->flags == BAM_TITLE && lp->arg && 3168 strcmp(lp->arg, title) == 0) 3169 break; 3170 continue; /* check title only */ 3171 } 3172 3173 lp = lp->next; /* advance to root line */ 3174 if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) { 3175 /* root command found, match grub disk */ 3176 if (strcmp(lp->arg, root) != 0) { 3177 continue; 3178 } 3179 lp = lp->next; /* advance to kernel line */ 3180 } else { 3181 /* no root command, see if root is optional */ 3182 if (root_opt == 0) { 3183 continue; 3184 } 3185 } 3186 3187 if (lp == NULL || lp->next == NULL) { 3188 continue; 3189 } 3190 3191 /* check for matching module entry (failsafe or normal) */ 3192 lp = lp->next; /* advance to module line */ 3193 if ((strncmp(lp->cmd, menu_cmds[MODULE_CMD], 3194 strlen(menu_cmds[MODULE_CMD])) != 0) || 3195 (strcmp(lp->arg, module) != 0)) { 3196 continue; 3197 } 3198 break; /* match found */ 3199 } 3200 3201 *entry_num = i; 3202 return (ent); 3203 } 3204 3205 static int 3206 update_boot_entry(menu_t *mp, char *title, char *root, char *kernel, 3207 char *module, int root_opt) 3208 { 3209 int i, change_kernel = 0; 3210 entry_t *ent; 3211 line_t *lp; 3212 char linebuf[BAM_MAXLINE]; 3213 3214 /* note: don't match on title, it's updated on upgrade */ 3215 ent = find_boot_entry(mp, NULL, root, module, root_opt, &i); 3216 if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) { 3217 /* 3218 * We may be upgrading a kernel from multiboot to 3219 * directboot. Look for a multiboot entry. 3220 */ 3221 ent = find_boot_entry(mp, NULL, root, MULTI_BOOT_ARCHIVE, 3222 root_opt, &i); 3223 if (ent != NULL) { 3224 change_kernel = 1; 3225 } 3226 } 3227 if (ent == NULL) 3228 return (add_boot_entry(mp, title, root_opt ? NULL : root, 3229 kernel, module)); 3230 3231 /* replace title of exiting entry and delete root line */ 3232 lp = ent->start; 3233 lp = lp->next; /* title line */ 3234 (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 3235 menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 3236 free(lp->arg); 3237 free(lp->line); 3238 lp->arg = s_strdup(title); 3239 lp->line = s_strdup(linebuf); 3240 3241 lp = lp->next; /* root line */ 3242 if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) { 3243 if (root_opt) { /* root line not needed */ 3244 line_t *tmp = lp; 3245 lp = lp->next; 3246 unlink_line(mp, tmp); 3247 line_free(tmp); 3248 } else 3249 lp = lp->next; 3250 } 3251 3252 if (change_kernel) { 3253 /* 3254 * We're upgrading from multiboot to directboot. 3255 */ 3256 if (strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) { 3257 (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 3258 menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD], 3259 kernel); 3260 free(lp->arg); 3261 free(lp->line); 3262 lp->arg = s_strdup(kernel); 3263 lp->line = s_strdup(linebuf); 3264 lp = lp->next; 3265 } 3266 if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) { 3267 (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 3268 menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD], 3269 module); 3270 free(lp->arg); 3271 free(lp->line); 3272 lp->arg = s_strdup(module); 3273 lp->line = s_strdup(linebuf); 3274 lp = lp->next; 3275 } 3276 } 3277 return (i); 3278 } 3279 3280 /*ARGSUSED*/ 3281 static error_t 3282 update_entry(menu_t *mp, char *menu_root, char *opt) 3283 { 3284 FILE *fp; 3285 int entry; 3286 char *grubdisk, *title, *osdev, *osroot; 3287 struct stat sbuf; 3288 char failsafe[256]; 3289 3290 assert(mp); 3291 assert(opt); 3292 3293 osdev = strtok(opt, ","); 3294 osroot = strtok(NULL, ","); 3295 if (osroot == NULL) 3296 osroot = menu_root; 3297 title = get_title(osroot); 3298 3299 /* translate /dev/dsk name to grub disk name */ 3300 fp = open_diskmap(osroot); 3301 if (fp == NULL) { 3302 bam_error(DISKMAP_FAIL, osdev); 3303 return (BAM_ERROR); 3304 } 3305 grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp)); 3306 (void) fclose(fp); 3307 if (grubdisk == NULL) { 3308 bam_error(DISKMAP_FAIL, osdev); 3309 return (BAM_ERROR); 3310 } 3311 3312 /* add the entry for normal Solaris */ 3313 if (bam_direct == BAM_DIRECT_DBOOT) { 3314 entry = update_boot_entry(mp, title, grubdisk, 3315 DIRECT_BOOT_KERNEL, DIRECT_BOOT_ARCHIVE, 3316 osroot == menu_root); 3317 } else { 3318 entry = update_boot_entry(mp, title, grubdisk, 3319 MULTI_BOOT, MULTI_BOOT_ARCHIVE, 3320 osroot == menu_root); 3321 } 3322 3323 /* add the entry for failsafe archive */ 3324 (void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot, MINIROOT); 3325 if (stat(failsafe, &sbuf) == 0) { 3326 (void) update_boot_entry(mp, FAILSAFE_TITLE, grubdisk, 3327 (bam_direct == BAM_DIRECT_DBOOT) ? 3328 DIRECT_BOOT_FAILSAFE_LINE : MULTI_BOOT_FAILSAFE_LINE, 3329 MINIROOT, osroot == menu_root); 3330 } 3331 free(grubdisk); 3332 3333 if (entry == BAM_ERROR) { 3334 return (BAM_ERROR); 3335 } 3336 (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 3337 return (BAM_WRITE); 3338 } 3339 3340 static char * 3341 read_grub_root(void) 3342 { 3343 FILE *fp; 3344 struct stat sb; 3345 char buf[BAM_MAXLINE]; 3346 char *rootstr; 3347 3348 if (stat(GRUB_slice, &sb) != 0) { 3349 bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 3350 return (NULL); 3351 } 3352 3353 if (stat(GRUB_root, &sb) != 0) { 3354 bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno)); 3355 return (NULL); 3356 } 3357 3358 fp = fopen(GRUB_root, "r"); 3359 if (fp == NULL) { 3360 bam_error(OPEN_FAIL, GRUB_root, strerror(errno)); 3361 return (NULL); 3362 } 3363 3364 if (s_fgets(buf, sizeof (buf), fp) == NULL) { 3365 bam_error(EMPTY_FILE, GRUB_root, strerror(errno)); 3366 (void) fclose(fp); 3367 return (NULL); 3368 } 3369 3370 /* 3371 * Copy buf here as check below may trash the buffer 3372 */ 3373 rootstr = s_strdup(buf); 3374 3375 if (s_fgets(buf, sizeof (buf), fp) != NULL) { 3376 bam_error(BAD_ROOT_FILE, GRUB_root); 3377 free(rootstr); 3378 rootstr = NULL; 3379 } 3380 3381 (void) fclose(fp); 3382 3383 return (rootstr); 3384 } 3385 3386 static void 3387 save_default_entry(menu_t *mp, const char *which) 3388 { 3389 int lineNum, entryNum; 3390 int entry = 0; /* default is 0 */ 3391 char linebuf[BAM_MAXLINE]; 3392 line_t *lp = mp->curdefault; 3393 3394 if (mp->start) { 3395 lineNum = mp->end->lineNum; 3396 entryNum = mp->end->entryNum; 3397 } else { 3398 lineNum = LINE_INIT; 3399 entryNum = ENTRY_INIT; 3400 } 3401 3402 if (lp) 3403 entry = s_strtol(lp->arg); 3404 3405 (void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry); 3406 line_parser(mp, linebuf, &lineNum, &entryNum); 3407 } 3408 3409 static void 3410 restore_default_entry(menu_t *mp, const char *which, line_t *lp) 3411 { 3412 int entry; 3413 char *str; 3414 3415 if (lp == NULL) 3416 return; /* nothing to restore */ 3417 3418 str = lp->arg + strlen(which); 3419 entry = s_strtol(str); 3420 (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 3421 3422 /* delete saved old default line */ 3423 unlink_line(mp, lp); 3424 line_free(lp); 3425 } 3426 3427 /* 3428 * This function is for supporting reboot with args. 3429 * The opt value can be: 3430 * NULL delete temp entry, if present 3431 * entry=# switches default entry to 1 3432 * else treated as boot-args and setup a temperary menu entry 3433 * and make it the default 3434 */ 3435 #define REBOOT_TITLE "Solaris_reboot_transient" 3436 3437 /*ARGSUSED*/ 3438 static error_t 3439 update_temp(menu_t *mp, char *menupath, char *opt) 3440 { 3441 int entry; 3442 char *grubdisk, *rootdev, *path; 3443 char kernbuf[BUFSIZ]; 3444 char args_buf[BUFSIZ]; 3445 struct stat sb; 3446 3447 assert(mp); 3448 3449 /* If no option, delete exiting reboot menu entry */ 3450 if (opt == NULL) { 3451 entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL, 3452 0, &entry); 3453 if (ent == NULL) /* not found is ok */ 3454 return (BAM_SUCCESS); 3455 (void) do_delete(mp, entry); 3456 restore_default_entry(mp, BAM_OLDDEF, mp->olddefault); 3457 mp->olddefault = NULL; 3458 return (BAM_WRITE); 3459 } 3460 3461 /* if entry= is specified, set the default entry */ 3462 if (strncmp(opt, "entry=", strlen("entry=")) == 0 && 3463 selector(mp, opt, &entry, NULL) == BAM_SUCCESS) { 3464 /* this is entry=# option */ 3465 return (set_global(mp, menu_cmds[DEFAULT_CMD], entry)); 3466 } 3467 3468 /* 3469 * add a new menu entry base on opt and make it the default 3470 */ 3471 grubdisk = NULL; 3472 if (stat(GRUB_slice, &sb) != 0) { 3473 /* 3474 * 1. First get root disk name from mnttab 3475 * 2. Translate disk name to grub name 3476 * 3. Add the new menu entry 3477 */ 3478 rootdev = get_special("/"); 3479 if (rootdev) { 3480 grubdisk = os_to_grubdisk(rootdev, 1); 3481 free(rootdev); 3482 } 3483 } else { 3484 /* 3485 * This is an LU BE. The GRUB_root file 3486 * contains entry for GRUB's "root" cmd. 3487 */ 3488 grubdisk = read_grub_root(); 3489 } 3490 if (grubdisk == NULL) { 3491 bam_error(REBOOT_WITH_ARGS_FAILED); 3492 return (BAM_ERROR); 3493 } 3494 3495 /* add an entry for Solaris reboot */ 3496 if (bam_direct == BAM_DIRECT_DBOOT) { 3497 if (opt[0] == '-') { 3498 /* It's an option - first see if boot-file is set */ 3499 if (set_kernel(mp, KERNEL_CMD, NULL, kernbuf, BUFSIZ) 3500 != BAM_SUCCESS) 3501 return (BAM_ERROR); 3502 if (kernbuf[0] == '\0') 3503 (void) strncpy(kernbuf, DIRECT_BOOT_KERNEL, 3504 BUFSIZ); 3505 (void) strlcat(kernbuf, " ", BUFSIZ); 3506 (void) strlcat(kernbuf, opt, BUFSIZ); 3507 } else if (opt[0] == '/') { 3508 /* It's a full path - write it out and go home */ 3509 (void) strlcpy(kernbuf, opt, BUFSIZ); 3510 } else { 3511 path = expand_path(opt); 3512 if (path != NULL) { 3513 (void) strlcpy(kernbuf, path, BUFSIZ); 3514 free(path); 3515 if (strcmp(opt, "kmdb") == 0) { 3516 if (set_kernel(mp, ARGS_CMD, NULL, 3517 args_buf, BUFSIZ) != BAM_SUCCESS) 3518 return (BAM_ERROR); 3519 3520 if (args_buf[0] != '\0') { 3521 (void) strlcat(kernbuf, " ", 3522 BUFSIZ); 3523 (void) strlcat(kernbuf, 3524 args_buf, BUFSIZ); 3525 } 3526 } 3527 } 3528 } 3529 entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf, 3530 NULL); 3531 } else { 3532 (void) snprintf(kernbuf, sizeof (kernbuf), "%s %s", 3533 MULTI_BOOT, opt); 3534 entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf, 3535 MULTI_BOOT_ARCHIVE); 3536 } 3537 free(grubdisk); 3538 3539 if (entry == BAM_ERROR) { 3540 bam_error(REBOOT_WITH_ARGS_FAILED); 3541 return (BAM_ERROR); 3542 } 3543 3544 save_default_entry(mp, BAM_OLDDEF); 3545 (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 3546 return (BAM_WRITE); 3547 } 3548 3549 static error_t 3550 set_global(menu_t *mp, char *globalcmd, int val) 3551 { 3552 line_t *lp, *found, *last; 3553 char *cp, *str; 3554 char prefix[BAM_MAXLINE]; 3555 size_t len; 3556 3557 assert(mp); 3558 assert(globalcmd); 3559 3560 if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) { 3561 if (val < 0 || mp->end == NULL || val > mp->end->entryNum) { 3562 (void) snprintf(prefix, sizeof (prefix), "%d", val); 3563 bam_error(INVALID_ENTRY, prefix); 3564 return (BAM_ERROR); 3565 } 3566 } 3567 3568 found = last = NULL; 3569 for (lp = mp->start; lp; lp = lp->next) { 3570 if (lp->flags != BAM_GLOBAL) 3571 continue; 3572 3573 last = lp; /* track the last global found */ 3574 3575 if (lp->cmd == NULL) { 3576 bam_error(NO_CMD, lp->lineNum); 3577 continue; 3578 } 3579 if (strcmp(globalcmd, lp->cmd) != 0) 3580 continue; 3581 3582 if (found) { 3583 bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 3584 } 3585 found = lp; 3586 } 3587 3588 if (found == NULL) { 3589 lp = s_calloc(1, sizeof (line_t)); 3590 if (last == NULL) { 3591 lp->next = mp->start; 3592 mp->start = lp; 3593 mp->end = (mp->end) ? mp->end : lp; 3594 } else { 3595 lp->next = last->next; 3596 last->next = lp; 3597 if (lp->next == NULL) 3598 mp->end = lp; 3599 } 3600 lp->flags = BAM_GLOBAL; /* other fields not needed for writes */ 3601 len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]); 3602 len += 10; /* val < 10 digits */ 3603 lp->line = s_calloc(1, len); 3604 (void) snprintf(lp->line, len, "%s%s%d", 3605 globalcmd, menu_cmds[SEP_CMD], val); 3606 return (BAM_WRITE); 3607 } 3608 3609 /* 3610 * We are changing an existing entry. Retain any prefix whitespace, 3611 * but overwrite everything else. This preserves tabs added for 3612 * readability. 3613 */ 3614 str = found->line; 3615 cp = prefix; 3616 while (*str == ' ' || *str == '\t') 3617 *(cp++) = *(str++); 3618 *cp = '\0'; /* Terminate prefix */ 3619 len = strlen(prefix) + strlen(globalcmd); 3620 len += strlen(menu_cmds[SEP_CMD]) + 10; 3621 3622 free(found->line); 3623 found->line = s_calloc(1, len); 3624 (void) snprintf(found->line, len, 3625 "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val); 3626 3627 return (BAM_WRITE); /* need a write to menu */ 3628 } 3629 3630 /* 3631 * partial_path may be anything like "kernel/unix" or "kmdb". Try to 3632 * expand it to a full unix path. 3633 */ 3634 static char * 3635 expand_path(const char *partial_path) 3636 { 3637 int new_path_len; 3638 char *new_path, new_path2[PATH_MAX]; 3639 struct stat sb; 3640 3641 new_path_len = strlen(partial_path) + 64; 3642 new_path = s_calloc(1, new_path_len); 3643 3644 /* First, try the simplest case - something like "kernel/unix" */ 3645 (void) snprintf(new_path, new_path_len, "/platform/i86pc/%s", 3646 partial_path); 3647 if (stat(new_path, &sb) == 0) { 3648 return (new_path); 3649 } 3650 3651 if (strcmp(partial_path, "kmdb") == 0) { 3652 (void) snprintf(new_path, new_path_len, "%s -k", 3653 DIRECT_BOOT_KERNEL); 3654 return (new_path); 3655 } 3656 3657 /* 3658 * We've quickly reached unsupported usage. Try once more to 3659 * see if we were just given a glom name. 3660 */ 3661 (void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix", 3662 partial_path); 3663 (void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix", 3664 partial_path); 3665 if (stat(new_path, &sb) == 0) { 3666 if (stat(new_path2, &sb) == 0) { 3667 /* 3668 * We matched both, so we actually 3669 * want to write the $ISADIR version. 3670 */ 3671 (void) snprintf(new_path, new_path_len, 3672 "/platform/i86pc/kernel/%s/$ISADIR/unix", 3673 partial_path); 3674 } 3675 return (new_path); 3676 } 3677 3678 bam_error(UNKNOWN_KERNEL, partial_path); 3679 free(new_path); 3680 return (NULL); 3681 } 3682 3683 /* 3684 * The kernel cmd and arg have been changed, so 3685 * check whether the archive line needs to change. 3686 */ 3687 static void 3688 set_archive_line(entry_t *entryp, line_t *kernelp) 3689 { 3690 line_t *lp = entryp->start; 3691 char *new_archive; 3692 menu_cmd_t m_cmd; 3693 3694 for (; lp != NULL; lp = lp->next) { 3695 if (strncmp(lp->cmd, menu_cmds[MODULE_CMD], 3696 sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) { 3697 break; 3698 } 3699 if (lp == entryp->end) 3700 return; 3701 } 3702 if (lp == NULL) 3703 return; 3704 3705 if (strstr(kernelp->arg, "$ISADIR") != NULL) { 3706 new_archive = DIRECT_BOOT_ARCHIVE; 3707 m_cmd = MODULE_DOLLAR_CMD; 3708 } else if (strstr(kernelp->arg, "amd64") != NULL) { 3709 new_archive = DIRECT_BOOT_ARCHIVE_64; 3710 m_cmd = MODULE_CMD; 3711 } else { 3712 new_archive = DIRECT_BOOT_ARCHIVE_32; 3713 m_cmd = MODULE_CMD; 3714 } 3715 3716 if (strcmp(lp->arg, new_archive) == 0) 3717 return; 3718 3719 if (strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) { 3720 free(lp->cmd); 3721 lp->cmd = s_strdup(menu_cmds[m_cmd]); 3722 } 3723 3724 free(lp->arg); 3725 lp->arg = s_strdup(new_archive); 3726 update_line(lp); 3727 } 3728 3729 /* 3730 * Title for an entry to set properties that once went in bootenv.rc. 3731 */ 3732 #define BOOTENV_RC_TITLE "Solaris bootenv rc" 3733 3734 /* 3735 * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments 3736 * (optnum == ARGS_CMD) in the argument buf. If path is a zero-length 3737 * string, reset the value to the default. If path is a non-zero-length 3738 * string, set the kernel or arguments. 3739 */ 3740 static error_t 3741 set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize) 3742 { 3743 int entryNum, rv = BAM_SUCCESS, free_new_path = 0; 3744 entry_t *entryp; 3745 line_t *ptr, *kernelp; 3746 char *new_arg, *old_args, *space; 3747 char *grubdisk, *rootdev, *new_path; 3748 char old_space; 3749 size_t old_kernel_len, new_str_len; 3750 struct stat sb; 3751 3752 assert(bufsize > 0); 3753 3754 ptr = kernelp = NULL; 3755 new_arg = old_args = space = NULL; 3756 grubdisk = rootdev = new_path = NULL; 3757 buf[0] = '\0'; 3758 3759 if (bam_direct != BAM_DIRECT_DBOOT) { 3760 bam_error(NOT_DBOOT, optnum == KERNEL_CMD ? "kernel" : "args"); 3761 return (BAM_ERROR); 3762 } 3763 3764 /* 3765 * If a user changed the default entry to a non-bootadm controlled 3766 * one, we don't want to mess with it. Just print an error and 3767 * return. 3768 */ 3769 if (mp->curdefault) { 3770 entryNum = s_strtol(mp->curdefault->arg); 3771 for (entryp = mp->entries; entryp; entryp = entryp->next) { 3772 if (entryp->entryNum == entryNum) 3773 break; 3774 } 3775 if ((entryp != NULL) && 3776 ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) { 3777 bam_error(DEFAULT_NOT_BAM); 3778 return (BAM_ERROR); 3779 } 3780 } 3781 3782 entryNum = -1; 3783 entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, 0, 3784 &entryNum); 3785 3786 if (entryp != NULL) { 3787 for (ptr = entryp->start; ptr && ptr != entryp->end; 3788 ptr = ptr->next) { 3789 if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD], 3790 sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) { 3791 kernelp = ptr; 3792 break; 3793 } 3794 } 3795 if (kernelp == NULL) { 3796 bam_error(NO_KERNEL, entryNum); 3797 return (BAM_ERROR); 3798 } 3799 3800 old_kernel_len = strcspn(kernelp->arg, " \t"); 3801 space = old_args = kernelp->arg + old_kernel_len; 3802 while ((*old_args == ' ') || (*old_args == '\t')) 3803 old_args++; 3804 } 3805 3806 if (path == NULL) { 3807 /* Simply report what was found */ 3808 if (kernelp == NULL) 3809 return (BAM_SUCCESS); 3810 3811 if (optnum == ARGS_CMD) { 3812 if (old_args[0] != '\0') 3813 (void) strlcpy(buf, old_args, bufsize); 3814 } else { 3815 /* 3816 * We need to print the kernel, so we just turn the 3817 * first space into a '\0' and print the beginning. 3818 * We don't print anything if it's the default kernel. 3819 */ 3820 old_space = *space; 3821 *space = '\0'; 3822 if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0) 3823 (void) strlcpy(buf, kernelp->arg, bufsize); 3824 *space = old_space; 3825 } 3826 return (BAM_SUCCESS); 3827 } 3828 3829 /* 3830 * First, check if we're resetting an entry to the default. 3831 */ 3832 if ((path[0] == '\0') || 3833 ((optnum == KERNEL_CMD) && 3834 (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) { 3835 if ((entryp == NULL) || (kernelp == NULL)) { 3836 /* No previous entry, it's already the default */ 3837 return (BAM_SUCCESS); 3838 } 3839 3840 /* 3841 * Check if we can delete the entry. If we're resetting the 3842 * kernel command, and the args is already empty, or if we're 3843 * resetting the args command, and the kernel is already the 3844 * default, we can restore the old default and delete the entry. 3845 */ 3846 if (((optnum == KERNEL_CMD) && 3847 ((old_args == NULL) || (old_args[0] == '\0'))) || 3848 ((optnum == ARGS_CMD) && 3849 (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL, 3850 sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) { 3851 kernelp = NULL; 3852 (void) do_delete(mp, entryNum); 3853 restore_default_entry(mp, BAM_OLD_RC_DEF, 3854 mp->old_rc_default); 3855 mp->old_rc_default = NULL; 3856 rv = BAM_WRITE; 3857 goto done; 3858 } 3859 3860 if (optnum == KERNEL_CMD) { 3861 /* 3862 * At this point, we've already checked that old_args 3863 * and entryp are valid pointers. The "+ 2" is for 3864 * a space a the string termination character. 3865 */ 3866 new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) + 3867 strlen(old_args) + 2; 3868 new_arg = s_calloc(1, new_str_len); 3869 (void) snprintf(new_arg, new_str_len, "%s %s", 3870 DIRECT_BOOT_KERNEL, old_args); 3871 free(kernelp->arg); 3872 kernelp->arg = new_arg; 3873 3874 /* 3875 * We have changed the kernel line, so we may need 3876 * to update the archive line as well. 3877 */ 3878 set_archive_line(entryp, kernelp); 3879 } else { 3880 /* 3881 * We're resetting the boot args to nothing, so 3882 * we only need to copy the kernel. We've already 3883 * checked that the kernel is not the default. 3884 */ 3885 new_arg = s_calloc(1, old_kernel_len + 1); 3886 (void) snprintf(new_arg, old_kernel_len + 1, "%s", 3887 kernelp->arg); 3888 free(kernelp->arg); 3889 kernelp->arg = new_arg; 3890 } 3891 rv = BAM_WRITE; 3892 goto done; 3893 } 3894 3895 /* 3896 * Expand the kernel file to a full path, if necessary 3897 */ 3898 if ((optnum == KERNEL_CMD) && (path[0] != '/')) { 3899 new_path = expand_path(path); 3900 if (new_path == NULL) { 3901 return (BAM_ERROR); 3902 } 3903 free_new_path = 1; 3904 } else { 3905 new_path = path; 3906 free_new_path = 0; 3907 } 3908 3909 /* 3910 * At this point, we know we're setting a new value. First, take care 3911 * of the case where there was no previous entry. 3912 */ 3913 if (entryp == NULL) { 3914 /* Similar to code in update_temp */ 3915 if (stat(GRUB_slice, &sb) != 0) { 3916 /* 3917 * 1. First get root disk name from mnttab 3918 * 2. Translate disk name to grub name 3919 * 3. Add the new menu entry 3920 */ 3921 rootdev = get_special("/"); 3922 if (rootdev) { 3923 grubdisk = os_to_grubdisk(rootdev, 1); 3924 free(rootdev); 3925 } 3926 } else { 3927 /* 3928 * This is an LU BE. The GRUB_root file 3929 * contains entry for GRUB's "root" cmd. 3930 */ 3931 grubdisk = read_grub_root(); 3932 } 3933 if (grubdisk == NULL) { 3934 bam_error(REBOOT_WITH_ARGS_FAILED); 3935 rv = BAM_ERROR; 3936 goto done; 3937 } 3938 if (optnum == KERNEL_CMD) { 3939 entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE, 3940 grubdisk, new_path, NULL); 3941 } else { 3942 new_str_len = strlen(DIRECT_BOOT_KERNEL) + 3943 strlen(path) + 8; 3944 new_arg = s_calloc(1, new_str_len); 3945 3946 (void) snprintf(new_arg, new_str_len, "%s %s", 3947 DIRECT_BOOT_KERNEL, path); 3948 entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE, 3949 grubdisk, new_arg, DIRECT_BOOT_ARCHIVE); 3950 } 3951 save_default_entry(mp, BAM_OLD_RC_DEF); 3952 (void) set_global(mp, menu_cmds[DEFAULT_CMD], entryNum); 3953 rv = BAM_WRITE; 3954 goto done; 3955 } 3956 3957 /* 3958 * There was already an bootenv entry which we need to edit. 3959 */ 3960 if (optnum == KERNEL_CMD) { 3961 new_str_len = strlen(new_path) + strlen(old_args) + 2; 3962 new_arg = s_calloc(1, new_str_len); 3963 (void) snprintf(new_arg, new_str_len, "%s %s", new_path, 3964 old_args); 3965 free(kernelp->arg); 3966 kernelp->arg = new_arg; 3967 3968 /* 3969 * If we have changed the kernel line, we may need to update 3970 * the archive line as well. 3971 */ 3972 set_archive_line(entryp, kernelp); 3973 } else { 3974 new_str_len = old_kernel_len + strlen(path) + 8; 3975 new_arg = s_calloc(1, new_str_len); 3976 (void) strncpy(new_arg, kernelp->arg, old_kernel_len); 3977 (void) strlcat(new_arg, " ", new_str_len); 3978 (void) strlcat(new_arg, path, new_str_len); 3979 free(kernelp->arg); 3980 kernelp->arg = new_arg; 3981 } 3982 rv = BAM_WRITE; 3983 3984 done: 3985 if ((rv == BAM_WRITE) && kernelp) 3986 update_line(kernelp); 3987 if (free_new_path) 3988 free(new_path); 3989 return (rv); 3990 } 3991 3992 /*ARGSUSED*/ 3993 static error_t 3994 set_option(menu_t *mp, char *menu_path, char *opt) 3995 { 3996 int optnum, optval; 3997 char *val; 3998 char buf[BUFSIZ] = ""; 3999 error_t rv; 4000 4001 assert(mp); 4002 assert(opt); 4003 4004 val = strchr(opt, '='); 4005 if (val != NULL) { 4006 *val = '\0'; 4007 } 4008 4009 if (strcmp(opt, "default") == 0) { 4010 optnum = DEFAULT_CMD; 4011 } else if (strcmp(opt, "timeout") == 0) { 4012 optnum = TIMEOUT_CMD; 4013 } else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) { 4014 optnum = KERNEL_CMD; 4015 } else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) { 4016 optnum = ARGS_CMD; 4017 } else { 4018 bam_error(INVALID_ENTRY, opt); 4019 return (BAM_ERROR); 4020 } 4021 4022 /* 4023 * kernel and args are allowed without "=new_value" strings. All 4024 * others cause errors 4025 */ 4026 if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) { 4027 bam_error(INVALID_ENTRY, opt); 4028 return (BAM_ERROR); 4029 } else if (val != NULL) { 4030 *val = '='; 4031 } 4032 4033 if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) { 4034 rv = set_kernel(mp, optnum, val ? val + 1 : NULL, buf, BUFSIZ); 4035 if ((rv == BAM_SUCCESS) && (buf[0] != '\0')) 4036 (void) printf("%s\n", buf); 4037 return (rv); 4038 } else { 4039 optval = s_strtol(val + 1); 4040 return (set_global(mp, menu_cmds[optnum], optval)); 4041 } 4042 } 4043 4044 /* 4045 * The quiet argument suppresses messages. This is used 4046 * when invoked in the context of other commands (e.g. list_entry) 4047 */ 4048 static error_t 4049 read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet) 4050 { 4051 line_t *lp; 4052 char *arg; 4053 int done, ret = BAM_SUCCESS; 4054 4055 assert(mp); 4056 assert(menu_path); 4057 assert(globalcmd); 4058 4059 if (mp->start == NULL) { 4060 if (!quiet) 4061 bam_error(NO_MENU, menu_path); 4062 return (BAM_ERROR); 4063 } 4064 4065 done = 0; 4066 for (lp = mp->start; lp; lp = lp->next) { 4067 if (lp->flags != BAM_GLOBAL) 4068 continue; 4069 4070 if (lp->cmd == NULL) { 4071 if (!quiet) 4072 bam_error(NO_CMD, lp->lineNum); 4073 continue; 4074 } 4075 4076 if (strcmp(globalcmd, lp->cmd) != 0) 4077 continue; 4078 4079 /* Found global. Check for duplicates */ 4080 if (done && !quiet) { 4081 bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 4082 ret = BAM_ERROR; 4083 } 4084 4085 arg = lp->arg ? lp->arg : ""; 4086 bam_print(GLOBAL_CMD, globalcmd, arg); 4087 done = 1; 4088 } 4089 4090 if (!done && bam_verbose) 4091 bam_print(NO_ENTRY, globalcmd); 4092 4093 return (ret); 4094 } 4095 4096 static error_t 4097 menu_write(char *root, menu_t *mp) 4098 { 4099 return (list2file(root, MENU_TMP, GRUB_MENU, mp->start)); 4100 } 4101 4102 static void 4103 line_free(line_t *lp) 4104 { 4105 if (lp == NULL) 4106 return; 4107 4108 if (lp->cmd) 4109 free(lp->cmd); 4110 if (lp->sep) 4111 free(lp->sep); 4112 if (lp->arg) 4113 free(lp->arg); 4114 if (lp->line) 4115 free(lp->line); 4116 free(lp); 4117 } 4118 4119 static void 4120 linelist_free(line_t *start) 4121 { 4122 line_t *lp; 4123 4124 while (start) { 4125 lp = start; 4126 start = start->next; 4127 line_free(lp); 4128 } 4129 } 4130 4131 static void 4132 filelist_free(filelist_t *flistp) 4133 { 4134 linelist_free(flistp->head); 4135 flistp->head = NULL; 4136 flistp->tail = NULL; 4137 } 4138 4139 static void 4140 menu_free(menu_t *mp) 4141 { 4142 entry_t *ent, *tmp; 4143 assert(mp); 4144 4145 if (mp->start) 4146 linelist_free(mp->start); 4147 ent = mp->entries; 4148 while (ent) { 4149 tmp = ent; 4150 ent = tmp->next; 4151 free(tmp); 4152 } 4153 4154 free(mp); 4155 } 4156 4157 /* 4158 * Utility routines 4159 */ 4160 4161 4162 /* 4163 * Returns 0 on success 4164 * Any other value indicates an error 4165 */ 4166 static int 4167 exec_cmd(char *cmdline, char *output, int64_t osize) 4168 { 4169 char buf[BUFSIZ]; 4170 int ret; 4171 FILE *ptr; 4172 size_t len; 4173 sigset_t set; 4174 void (*disp)(int); 4175 4176 /* 4177 * For security 4178 * - only absolute paths are allowed 4179 * - set IFS to space and tab 4180 */ 4181 if (*cmdline != '/') { 4182 bam_error(ABS_PATH_REQ, cmdline); 4183 return (-1); 4184 } 4185 (void) putenv("IFS= \t"); 4186 4187 /* 4188 * We may have been exec'ed with SIGCHLD blocked 4189 * unblock it here 4190 */ 4191 (void) sigemptyset(&set); 4192 (void) sigaddset(&set, SIGCHLD); 4193 if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { 4194 bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno)); 4195 return (-1); 4196 } 4197 4198 /* 4199 * Set SIGCHLD disposition to SIG_DFL for popen/pclose 4200 */ 4201 disp = sigset(SIGCHLD, SIG_DFL); 4202 if (disp == SIG_ERR) { 4203 bam_error(FAILED_SIG, strerror(errno)); 4204 return (-1); 4205 } 4206 if (disp == SIG_HOLD) { 4207 bam_error(BLOCKED_SIG, cmdline); 4208 return (-1); 4209 } 4210 4211 ptr = popen(cmdline, "r"); 4212 if (ptr == NULL) { 4213 bam_error(POPEN_FAIL, cmdline, strerror(errno)); 4214 return (-1); 4215 } 4216 4217 /* 4218 * If we simply do a pclose() following a popen(), pclose() 4219 * will close the reader end of the pipe immediately even 4220 * if the child process has not started/exited. pclose() 4221 * does wait for cmd to terminate before returning though. 4222 * When the executed command writes its output to the pipe 4223 * there is no reader process and the command dies with 4224 * SIGPIPE. To avoid this we read repeatedly until read 4225 * terminates with EOF. This indicates that the command 4226 * (writer) has closed the pipe and we can safely do a 4227 * pclose(). 4228 * 4229 * Since pclose() does wait for the command to exit, 4230 * we can safely reap the exit status of the command 4231 * from the value returned by pclose() 4232 */ 4233 while (fgets(buf, sizeof (buf), ptr) != NULL) { 4234 /* if (bam_verbose) XXX */ 4235 bam_print(PRINT_NO_NEWLINE, buf); 4236 if (output && osize > 0) { 4237 (void) snprintf(output, osize, "%s", buf); 4238 len = strlen(buf); 4239 output += len; 4240 osize -= len; 4241 } 4242 } 4243 4244 ret = pclose(ptr); 4245 if (ret == -1) { 4246 bam_error(PCLOSE_FAIL, cmdline, strerror(errno)); 4247 return (-1); 4248 } 4249 4250 if (WIFEXITED(ret)) { 4251 return (WEXITSTATUS(ret)); 4252 } else { 4253 bam_error(EXEC_FAIL, cmdline, ret); 4254 return (-1); 4255 } 4256 } 4257 4258 /* 4259 * Since this function returns -1 on error 4260 * it cannot be used to convert -1. However, 4261 * that is sufficient for what we need. 4262 */ 4263 static long 4264 s_strtol(char *str) 4265 { 4266 long l; 4267 char *res = NULL; 4268 4269 if (str == NULL) { 4270 return (-1); 4271 } 4272 4273 errno = 0; 4274 l = strtol(str, &res, 10); 4275 if (errno || *res != '\0') { 4276 return (-1); 4277 } 4278 4279 return (l); 4280 } 4281 4282 /* 4283 * Wrapper around fputs, that adds a newline (since fputs doesn't) 4284 */ 4285 static int 4286 s_fputs(char *str, FILE *fp) 4287 { 4288 char linebuf[BAM_MAXLINE]; 4289 4290 (void) snprintf(linebuf, sizeof (linebuf), "%s\n", str); 4291 return (fputs(linebuf, fp)); 4292 } 4293 4294 /* 4295 * Wrapper around fgets, that strips newlines returned by fgets 4296 */ 4297 char * 4298 s_fgets(char *buf, int buflen, FILE *fp) 4299 { 4300 int n; 4301 4302 buf = fgets(buf, buflen, fp); 4303 if (buf) { 4304 n = strlen(buf); 4305 if (n == buflen - 1 && buf[n-1] != '\n') 4306 bam_error(TOO_LONG, buflen - 1, buf); 4307 buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1]; 4308 } 4309 4310 return (buf); 4311 } 4312 4313 void * 4314 s_calloc(size_t nelem, size_t sz) 4315 { 4316 void *ptr; 4317 4318 ptr = calloc(nelem, sz); 4319 if (ptr == NULL) { 4320 bam_error(NO_MEM, nelem*sz); 4321 bam_exit(1); 4322 } 4323 return (ptr); 4324 } 4325 4326 void * 4327 s_realloc(void *ptr, size_t sz) 4328 { 4329 ptr = realloc(ptr, sz); 4330 if (ptr == NULL) { 4331 bam_error(NO_MEM, sz); 4332 bam_exit(1); 4333 } 4334 return (ptr); 4335 } 4336 4337 static char * 4338 s_strdup(char *str) 4339 { 4340 char *ptr; 4341 4342 if (str == NULL) 4343 return (NULL); 4344 4345 ptr = strdup(str); 4346 if (ptr == NULL) { 4347 bam_error(NO_MEM, strlen(str) + 1); 4348 bam_exit(1); 4349 } 4350 return (ptr); 4351 } 4352 4353 /* 4354 * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients) 4355 * Returns 0 otherwise 4356 */ 4357 static int 4358 is_amd64(void) 4359 { 4360 static int amd64 = -1; 4361 char isabuf[257]; /* from sysinfo(2) manpage */ 4362 4363 if (amd64 != -1) 4364 return (amd64); 4365 4366 if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 && 4367 strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) 4368 amd64 = 1; 4369 else if (strstr(isabuf, "i386") == NULL) 4370 amd64 = 1; /* diskless server */ 4371 else 4372 amd64 = 0; 4373 4374 return (amd64); 4375 } 4376 4377 static void 4378 append_to_flist(filelist_t *flistp, char *s) 4379 { 4380 line_t *lp; 4381 4382 lp = s_calloc(1, sizeof (line_t)); 4383 lp->line = s_strdup(s); 4384 if (flistp->head == NULL) 4385 flistp->head = lp; 4386 else 4387 flistp->tail->next = lp; 4388 flistp->tail = lp; 4389 } 4390