10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51746Svikram * Common Development and Distribution License (the "License"). 61746Svikram * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 223381Svikram * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * bootadm(1M) is a new utility for managing bootability of 300Sstevel@tonic-gate * Solaris *Newboot* environments. It has two primary tasks: 310Sstevel@tonic-gate * - Allow end users to manage bootability of Newboot Solaris instances 320Sstevel@tonic-gate * - Provide services to other subsystems in Solaris (primarily Install) 330Sstevel@tonic-gate */ 340Sstevel@tonic-gate 350Sstevel@tonic-gate /* Headers */ 360Sstevel@tonic-gate #include <stdio.h> 370Sstevel@tonic-gate #include <errno.h> 380Sstevel@tonic-gate #include <stdlib.h> 390Sstevel@tonic-gate #include <string.h> 400Sstevel@tonic-gate #include <unistd.h> 410Sstevel@tonic-gate #include <sys/types.h> 420Sstevel@tonic-gate #include <sys/stat.h> 430Sstevel@tonic-gate #include <stdarg.h> 440Sstevel@tonic-gate #include <limits.h> 450Sstevel@tonic-gate #include <signal.h> 460Sstevel@tonic-gate #include <sys/wait.h> 470Sstevel@tonic-gate #include <sys/mnttab.h> 480Sstevel@tonic-gate #include <sys/statvfs.h> 490Sstevel@tonic-gate #include <libnvpair.h> 500Sstevel@tonic-gate #include <ftw.h> 510Sstevel@tonic-gate #include <fcntl.h> 520Sstevel@tonic-gate #include <strings.h> 530Sstevel@tonic-gate #include <sys/systeminfo.h> 540Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 552334Ssetje #include <sys/param.h> 560Sstevel@tonic-gate 570Sstevel@tonic-gate #include <pwd.h> 580Sstevel@tonic-gate #include <grp.h> 590Sstevel@tonic-gate #include <device_info.h> 600Sstevel@tonic-gate 610Sstevel@tonic-gate #include <locale.h> 620Sstevel@tonic-gate 630Sstevel@tonic-gate #include <assert.h> 640Sstevel@tonic-gate 650Sstevel@tonic-gate #include "message.h" 663446Smrj #include "bootadm.h" 670Sstevel@tonic-gate 680Sstevel@tonic-gate #ifndef TEXT_DOMAIN 690Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 700Sstevel@tonic-gate #endif /* TEXT_DOMAIN */ 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* Type definitions */ 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* Primary subcmds */ 750Sstevel@tonic-gate typedef enum { 760Sstevel@tonic-gate BAM_MENU = 3, 770Sstevel@tonic-gate BAM_ARCHIVE 780Sstevel@tonic-gate } subcmd_t; 790Sstevel@tonic-gate 800Sstevel@tonic-gate typedef enum { 810Sstevel@tonic-gate OPT_ABSENT = 0, /* No option */ 820Sstevel@tonic-gate OPT_REQ, /* option required */ 830Sstevel@tonic-gate OPT_OPTIONAL /* option may or may not be present */ 840Sstevel@tonic-gate } option_t; 850Sstevel@tonic-gate 860Sstevel@tonic-gate typedef struct { 870Sstevel@tonic-gate char *subcmd; 880Sstevel@tonic-gate option_t option; 890Sstevel@tonic-gate error_t (*handler)(); 902115Svikram int unpriv; /* is this an unprivileged command */ 910Sstevel@tonic-gate } subcmd_defn_t; 920Sstevel@tonic-gate 930Sstevel@tonic-gate #define LINE_INIT 0 /* lineNum initial value */ 940Sstevel@tonic-gate #define ENTRY_INIT -1 /* entryNum initial value */ 950Sstevel@tonic-gate #define ALL_ENTRIES -2 /* selects all boot entries */ 960Sstevel@tonic-gate 970Sstevel@tonic-gate #define GRUB_DIR "/boot/grub" 980Sstevel@tonic-gate #define GRUB_MENU "/boot/grub/menu.lst" 990Sstevel@tonic-gate #define MENU_TMP "/boot/grub/menu.lst.tmp" 1000Sstevel@tonic-gate #define RAMDISK_SPECIAL "/ramdisk" 101621Svikram #define STUBBOOT "/stubboot" 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate /* lock related */ 1040Sstevel@tonic-gate #define BAM_LOCK_FILE "/var/run/bootadm.lock" 1050Sstevel@tonic-gate #define LOCK_FILE_PERMS (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate #define CREATE_RAMDISK "/boot/solaris/bin/create_ramdisk" 1080Sstevel@tonic-gate #define CREATE_DISKMAP "/boot/solaris/bin/create_diskmap" 1090Sstevel@tonic-gate #define GRUBDISK_MAP "/var/run/solaris_grubdisk.map" 1100Sstevel@tonic-gate 111316Svikram #define GRUB_slice "/etc/lu/GRUB_slice" 112316Svikram #define GRUB_root "/etc/lu/GRUB_root" 113316Svikram #define GRUB_backup_menu "/etc/lu/GRUB_backup_menu" 114316Svikram #define GRUB_slice_mntpt "/tmp/GRUB_slice_mntpt" 115316Svikram #define LU_ACTIVATE_FILE "/etc/lu/DelayUpdate/activate.sh" 1161746Svikram #define GRUB_fdisk "/etc/lu/GRUB_fdisk" 1171746Svikram #define GRUB_fdisk_target "/etc/lu/GRUB_fdisk_target" 118316Svikram 119316Svikram #define INSTALLGRUB "/sbin/installgrub" 120316Svikram #define STAGE1 "/boot/grub/stage1" 121316Svikram #define STAGE2 "/boot/grub/stage2" 122316Svikram 1230Sstevel@tonic-gate /* 1244493Snadkarni * The following two defines are used to detect and create the correct 1254493Snadkarni * boot archive when safemode patching is underway. LOFS_PATCH_FILE is a 1264493Snadkarni * contracted private interface between bootadm and the install 1274493Snadkarni * consolidation. It is set by pdo.c when a patch with SUNW_PATCH_SAFEMODE 1284493Snadkarni * is applied. 1294493Snadkarni */ 1304493Snadkarni 1314493Snadkarni #define LOFS_PATCH_FILE "/var/run/.patch_loopback_mode" 1324493Snadkarni #define LOFS_PATCH_MNT "/var/run/.patch_root_loopbackmnt" 1334493Snadkarni 1344493Snadkarni /* 1350Sstevel@tonic-gate * Default file attributes 1360Sstevel@tonic-gate */ 1370Sstevel@tonic-gate #define DEFAULT_DEV_MODE 0644 /* default permissions */ 1380Sstevel@tonic-gate #define DEFAULT_DEV_UID 0 /* user root */ 1390Sstevel@tonic-gate #define DEFAULT_DEV_GID 3 /* group sys */ 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate /* 1420Sstevel@tonic-gate * Menu related 1430Sstevel@tonic-gate * menu_cmd_t and menu_cmds must be kept in sync 1440Sstevel@tonic-gate */ 1453446Smrj char *menu_cmds[] = { 1460Sstevel@tonic-gate "default", /* DEFAULT_CMD */ 1470Sstevel@tonic-gate "timeout", /* TIMEOUT_CMD */ 1480Sstevel@tonic-gate "title", /* TITLE_CMD */ 1490Sstevel@tonic-gate "root", /* ROOT_CMD */ 1500Sstevel@tonic-gate "kernel", /* KERNEL_CMD */ 1513446Smrj "kernel$", /* KERNEL_DOLLAR_CMD */ 1520Sstevel@tonic-gate "module", /* MODULE_CMD */ 1533446Smrj "module$", /* MODULE_DOLLAR_CMD */ 1540Sstevel@tonic-gate " ", /* SEP_CMD */ 1550Sstevel@tonic-gate "#", /* COMMENT_CMD */ 1563446Smrj "chainloader", /* CHAINLOADER_CMD */ 1573446Smrj "args", /* ARGS_CMD */ 1580Sstevel@tonic-gate NULL 1590Sstevel@tonic-gate }; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate #define OPT_ENTRY_NUM "entry" 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate /* 1640Sstevel@tonic-gate * archive related 1650Sstevel@tonic-gate */ 1660Sstevel@tonic-gate typedef struct { 1670Sstevel@tonic-gate line_t *head; 1680Sstevel@tonic-gate line_t *tail; 1690Sstevel@tonic-gate } filelist_t; 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate #define BOOT_FILE_LIST "boot/solaris/filelist.ramdisk" 1720Sstevel@tonic-gate #define ETC_FILE_LIST "etc/boot/solaris/filelist.ramdisk" 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate #define FILE_STAT "boot/solaris/filestat.ramdisk" 1750Sstevel@tonic-gate #define FILE_STAT_TMP "boot/solaris/filestat.ramdisk.tmp" 1760Sstevel@tonic-gate #define DIR_PERMS (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) 1770Sstevel@tonic-gate #define FILE_STAT_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate /* Globals */ 1803446Smrj int bam_verbose; 1813446Smrj int bam_force; 1820Sstevel@tonic-gate static char *prog; 1830Sstevel@tonic-gate static subcmd_t bam_cmd; 1840Sstevel@tonic-gate static char *bam_root; 1850Sstevel@tonic-gate static int bam_rootlen; 1860Sstevel@tonic-gate static int bam_root_readonly; 187621Svikram static int bam_alt_root; 1880Sstevel@tonic-gate static char *bam_subcmd; 1890Sstevel@tonic-gate static char *bam_opt; 1900Sstevel@tonic-gate static int bam_debug; 1910Sstevel@tonic-gate static char **bam_argv; 1920Sstevel@tonic-gate static int bam_argc; 1930Sstevel@tonic-gate static int bam_check; 1940Sstevel@tonic-gate static int bam_smf_check; 1950Sstevel@tonic-gate static int bam_lock_fd = -1; 1960Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/"; 197316Svikram static int bam_update_all; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* function prototypes */ 2000Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]); 2010Sstevel@tonic-gate static void parse_args(int argc, char *argv[]); 2020Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]); 2030Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt); 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate static void bam_print(char *format, ...); 2060Sstevel@tonic-gate static void bam_exit(int excode); 2070Sstevel@tonic-gate static void bam_lock(void); 2080Sstevel@tonic-gate static void bam_unlock(void); 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize); 2110Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path, 2120Sstevel@tonic-gate char *globalcmd, int quiet); 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate static menu_t *menu_read(char *menu_path); 2150Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp); 2160Sstevel@tonic-gate static void linelist_free(line_t *start); 2170Sstevel@tonic-gate static void menu_free(menu_t *mp); 2180Sstevel@tonic-gate static void line_free(line_t *lp); 2190Sstevel@tonic-gate static void filelist_free(filelist_t *flistp); 2200Sstevel@tonic-gate static error_t list2file(char *root, char *tmp, 2210Sstevel@tonic-gate char *final, line_t *start); 2220Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt); 2230Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt); 2240Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt); 2250Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt); 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate static error_t update_archive(char *root, char *opt); 2280Sstevel@tonic-gate static error_t list_archive(char *root, char *opt); 2290Sstevel@tonic-gate static error_t update_all(char *root, char *opt); 2300Sstevel@tonic-gate static error_t read_list(char *root, filelist_t *flistp); 2310Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val); 2320Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt); 2333446Smrj static error_t set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, 2343446Smrj char *buf, size_t bufsize); 2353446Smrj static char *expand_path(const char *partial_path); 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate static long s_strtol(char *str); 2380Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp); 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate static char *s_strdup(char *str); 2410Sstevel@tonic-gate static int is_readonly(char *); 2420Sstevel@tonic-gate static int is_amd64(void); 2430Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *); 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate #if defined(__sparc) 2460Sstevel@tonic-gate static void sparc_abort(void); 2470Sstevel@tonic-gate #endif 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* Menu related sub commands */ 2500Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = { 2512115Svikram "set_option", OPT_OPTIONAL, set_option, 0, /* PUB */ 2522115Svikram "list_entry", OPT_OPTIONAL, list_entry, 1, /* PUB */ 2532115Svikram "delete_all_entries", OPT_ABSENT, delete_all_entries, 0, /* PVT */ 2542115Svikram "update_entry", OPT_REQ, update_entry, 0, /* menu */ 2552115Svikram "update_temp", OPT_OPTIONAL, update_temp, 0, /* reboot */ 2563446Smrj "upgrade", OPT_ABSENT, upgrade_menu, 0, /* menu */ 2572115Svikram NULL, 0, NULL, 0 /* must be last */ 2580Sstevel@tonic-gate }; 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate /* Archive related sub commands */ 2610Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = { 2622115Svikram "update", OPT_ABSENT, update_archive, 0, /* PUB */ 2632115Svikram "update_all", OPT_ABSENT, update_all, 0, /* PVT */ 2642115Svikram "list", OPT_OPTIONAL, list_archive, 1, /* PUB */ 2652115Svikram NULL, 0, NULL, 0 /* must be last */ 2660Sstevel@tonic-gate }; 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate static struct { 2690Sstevel@tonic-gate nvlist_t *new_nvlp; 2700Sstevel@tonic-gate nvlist_t *old_nvlp; 2710Sstevel@tonic-gate int need_update; 2720Sstevel@tonic-gate } walk_arg; 2730Sstevel@tonic-gate 2742334Ssetje 2752345Ssetje struct safefile { 2762334Ssetje char *name; 2772334Ssetje struct safefile *next; 2782334Ssetje }; 2792334Ssetje 2803446Smrj static struct safefile *safefiles = NULL; 2812334Ssetje #define NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update" 2822334Ssetje 2830Sstevel@tonic-gate static void 2840Sstevel@tonic-gate usage(void) 2850Sstevel@tonic-gate { 2860Sstevel@tonic-gate (void) fprintf(stderr, "USAGE:\n"); 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate /* archive usage */ 2900Sstevel@tonic-gate (void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n", 2910Sstevel@tonic-gate prog); 2920Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog); 2930Sstevel@tonic-gate #ifndef __sparc 2940Sstevel@tonic-gate /* x86 only */ 2950Sstevel@tonic-gate (void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog); 2960Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog); 2970Sstevel@tonic-gate #endif 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate int 3010Sstevel@tonic-gate main(int argc, char *argv[]) 3020Sstevel@tonic-gate { 3030Sstevel@tonic-gate error_t ret; 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3060Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate if ((prog = strrchr(argv[0], '/')) == NULL) { 3090Sstevel@tonic-gate prog = argv[0]; 3100Sstevel@tonic-gate } else { 3110Sstevel@tonic-gate prog++; 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /* 3160Sstevel@tonic-gate * Don't depend on caller's umask 3170Sstevel@tonic-gate */ 3180Sstevel@tonic-gate (void) umask(0022); 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate parse_args(argc, argv); 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate #if defined(__sparc) 3230Sstevel@tonic-gate /* 3240Sstevel@tonic-gate * There are only two valid invocations of bootadm 3250Sstevel@tonic-gate * on SPARC: 3260Sstevel@tonic-gate * 3270Sstevel@tonic-gate * - SPARC diskless server creating boot_archive for i386 clients 3280Sstevel@tonic-gate * - archive creation call during reboot of a SPARC system 3290Sstevel@tonic-gate * 3300Sstevel@tonic-gate * The latter should be a NOP 3310Sstevel@tonic-gate */ 3320Sstevel@tonic-gate if (bam_cmd != BAM_ARCHIVE) { 3330Sstevel@tonic-gate sparc_abort(); 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate #endif 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate switch (bam_cmd) { 3380Sstevel@tonic-gate case BAM_MENU: 3390Sstevel@tonic-gate ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv); 3400Sstevel@tonic-gate break; 3410Sstevel@tonic-gate case BAM_ARCHIVE: 3420Sstevel@tonic-gate ret = bam_archive(bam_subcmd, bam_opt); 3430Sstevel@tonic-gate break; 3440Sstevel@tonic-gate default: 3450Sstevel@tonic-gate usage(); 3460Sstevel@tonic-gate bam_exit(1); 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate if (ret != BAM_SUCCESS) 3500Sstevel@tonic-gate bam_exit(1); 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate bam_unlock(); 3530Sstevel@tonic-gate return (0); 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate #if defined(__sparc) 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate static void 3590Sstevel@tonic-gate sparc_abort(void) 3600Sstevel@tonic-gate { 3610Sstevel@tonic-gate bam_error(NOT_ON_SPARC); 3620Sstevel@tonic-gate bam_exit(1); 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate #endif 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate /* 3680Sstevel@tonic-gate * Equivalence of public and internal commands: 3690Sstevel@tonic-gate * update-archive -- -a update 3700Sstevel@tonic-gate * list-archive -- -a list 3710Sstevel@tonic-gate * set-menu -- -m set_option 3720Sstevel@tonic-gate * list-menu -- -m list_entry 3730Sstevel@tonic-gate * update-menu -- -m update_entry 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate static struct cmd_map { 3760Sstevel@tonic-gate char *bam_cmdname; 3770Sstevel@tonic-gate int bam_cmd; 3780Sstevel@tonic-gate char *bam_subcmd; 3790Sstevel@tonic-gate } cmd_map[] = { 3800Sstevel@tonic-gate { "update-archive", BAM_ARCHIVE, "update"}, 3810Sstevel@tonic-gate { "list-archive", BAM_ARCHIVE, "list"}, 3820Sstevel@tonic-gate { "set-menu", BAM_MENU, "set_option"}, 3830Sstevel@tonic-gate { "list-menu", BAM_MENU, "list_entry"}, 3840Sstevel@tonic-gate { "update-menu", BAM_MENU, "update_entry"}, 3850Sstevel@tonic-gate { NULL, 0, NULL} 3860Sstevel@tonic-gate }; 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate /* 3890Sstevel@tonic-gate * Commands syntax published in bootadm(1M) are parsed here 3900Sstevel@tonic-gate */ 3910Sstevel@tonic-gate static void 3920Sstevel@tonic-gate parse_args(int argc, char *argv[]) 3930Sstevel@tonic-gate { 3940Sstevel@tonic-gate struct cmd_map *cmp = cmd_map; 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate /* command conforming to the final spec */ 3970Sstevel@tonic-gate if (argc > 1 && argv[1][0] != '-') { 3980Sstevel@tonic-gate /* 3990Sstevel@tonic-gate * Map commands to internal table. 4000Sstevel@tonic-gate */ 4010Sstevel@tonic-gate while (cmp->bam_cmdname) { 4020Sstevel@tonic-gate if (strcmp(argv[1], cmp->bam_cmdname) == 0) { 4030Sstevel@tonic-gate bam_cmd = cmp->bam_cmd; 4040Sstevel@tonic-gate bam_subcmd = cmp->bam_subcmd; 4050Sstevel@tonic-gate break; 4060Sstevel@tonic-gate } 4070Sstevel@tonic-gate cmp++; 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate if (cmp->bam_cmdname == NULL) { 4100Sstevel@tonic-gate usage(); 4110Sstevel@tonic-gate bam_exit(1); 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate argc--; 4140Sstevel@tonic-gate argv++; 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate parse_args_internal(argc, argv); 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate /* 4210Sstevel@tonic-gate * A combination of public and private commands are parsed here. 4220Sstevel@tonic-gate * The internal syntax and the corresponding functionality are: 4230Sstevel@tonic-gate * -a update -- update-archive 4240Sstevel@tonic-gate * -a list -- list-archive 4250Sstevel@tonic-gate * -a update-all -- (reboot to sync all mounted OS archive) 4260Sstevel@tonic-gate * -m update_entry -- update-menu 4270Sstevel@tonic-gate * -m list_entry -- list-menu 4280Sstevel@tonic-gate * -m update_temp -- (reboot -- [boot-args]) 4290Sstevel@tonic-gate * -m delete_all_entries -- (called from install) 4300Sstevel@tonic-gate */ 4310Sstevel@tonic-gate static void 4320Sstevel@tonic-gate parse_args_internal(int argc, char *argv[]) 4330Sstevel@tonic-gate { 4340Sstevel@tonic-gate int c, error; 4350Sstevel@tonic-gate extern char *optarg; 4360Sstevel@tonic-gate extern int optind, opterr; 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate /* Suppress error message from getopt */ 4390Sstevel@tonic-gate opterr = 0; 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate error = 0; 4423467Srscott while ((c = getopt(argc, argv, "a:d:fm:no:vCR:")) != -1) { 4430Sstevel@tonic-gate switch (c) { 4440Sstevel@tonic-gate case 'a': 4450Sstevel@tonic-gate if (bam_cmd) { 4460Sstevel@tonic-gate error = 1; 4470Sstevel@tonic-gate bam_error(MULT_CMDS, c); 4480Sstevel@tonic-gate } 4490Sstevel@tonic-gate bam_cmd = BAM_ARCHIVE; 4500Sstevel@tonic-gate bam_subcmd = optarg; 4510Sstevel@tonic-gate break; 4520Sstevel@tonic-gate case 'd': 4530Sstevel@tonic-gate if (bam_debug) { 4540Sstevel@tonic-gate error = 1; 4550Sstevel@tonic-gate bam_error(DUP_OPT, c); 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate bam_debug = s_strtol(optarg); 4580Sstevel@tonic-gate break; 4590Sstevel@tonic-gate case 'f': 4600Sstevel@tonic-gate if (bam_force) { 4610Sstevel@tonic-gate error = 1; 4620Sstevel@tonic-gate bam_error(DUP_OPT, c); 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate bam_force = 1; 4650Sstevel@tonic-gate break; 4660Sstevel@tonic-gate case 'm': 4670Sstevel@tonic-gate if (bam_cmd) { 4680Sstevel@tonic-gate error = 1; 4690Sstevel@tonic-gate bam_error(MULT_CMDS, c); 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate bam_cmd = BAM_MENU; 4720Sstevel@tonic-gate bam_subcmd = optarg; 4730Sstevel@tonic-gate break; 4740Sstevel@tonic-gate case 'n': 4750Sstevel@tonic-gate if (bam_check) { 4760Sstevel@tonic-gate error = 1; 4770Sstevel@tonic-gate bam_error(DUP_OPT, c); 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate bam_check = 1; 4800Sstevel@tonic-gate break; 4810Sstevel@tonic-gate case 'o': 4820Sstevel@tonic-gate if (bam_opt) { 4830Sstevel@tonic-gate error = 1; 4840Sstevel@tonic-gate bam_error(DUP_OPT, c); 4850Sstevel@tonic-gate } 4860Sstevel@tonic-gate bam_opt = optarg; 4870Sstevel@tonic-gate break; 4880Sstevel@tonic-gate case 'v': 4890Sstevel@tonic-gate if (bam_verbose) { 4900Sstevel@tonic-gate error = 1; 4910Sstevel@tonic-gate bam_error(DUP_OPT, c); 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate bam_verbose = 1; 4940Sstevel@tonic-gate break; 495662Sszhou case 'C': 496662Sszhou bam_smf_check = 1; 497662Sszhou break; 4980Sstevel@tonic-gate case 'R': 4990Sstevel@tonic-gate if (bam_root) { 5000Sstevel@tonic-gate error = 1; 5010Sstevel@tonic-gate bam_error(DUP_OPT, c); 5020Sstevel@tonic-gate break; 5030Sstevel@tonic-gate } else if (realpath(optarg, rootbuf) == NULL) { 5040Sstevel@tonic-gate error = 1; 5050Sstevel@tonic-gate bam_error(CANT_RESOLVE, optarg, 5060Sstevel@tonic-gate strerror(errno)); 5070Sstevel@tonic-gate break; 5080Sstevel@tonic-gate } 509621Svikram bam_alt_root = 1; 5100Sstevel@tonic-gate bam_root = rootbuf; 5110Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 5120Sstevel@tonic-gate break; 5130Sstevel@tonic-gate case '?': 5140Sstevel@tonic-gate error = 1; 5150Sstevel@tonic-gate bam_error(BAD_OPT, optopt); 5160Sstevel@tonic-gate break; 5170Sstevel@tonic-gate default : 5180Sstevel@tonic-gate error = 1; 5190Sstevel@tonic-gate bam_error(BAD_OPT, c); 5200Sstevel@tonic-gate break; 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate /* 5250Sstevel@tonic-gate * A command option must be specfied 5260Sstevel@tonic-gate */ 5270Sstevel@tonic-gate if (!bam_cmd) { 5280Sstevel@tonic-gate if (bam_opt && strcmp(bam_opt, "all") == 0) { 5290Sstevel@tonic-gate usage(); 5300Sstevel@tonic-gate bam_exit(0); 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate bam_error(NEED_CMD); 5330Sstevel@tonic-gate error = 1; 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate if (error) { 5370Sstevel@tonic-gate usage(); 5380Sstevel@tonic-gate bam_exit(1); 5390Sstevel@tonic-gate } 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate if (optind > argc) { 5420Sstevel@tonic-gate bam_error(INT_ERROR, "parse_args"); 5430Sstevel@tonic-gate bam_exit(1); 5440Sstevel@tonic-gate } else if (optind < argc) { 5450Sstevel@tonic-gate bam_argv = &argv[optind]; 5460Sstevel@tonic-gate bam_argc = argc - optind; 5470Sstevel@tonic-gate } 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate /* 5500Sstevel@tonic-gate * -n implies verbose mode 5510Sstevel@tonic-gate */ 5520Sstevel@tonic-gate if (bam_check) 5530Sstevel@tonic-gate bam_verbose = 1; 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate static error_t 5570Sstevel@tonic-gate check_subcmd_and_options( 5580Sstevel@tonic-gate char *subcmd, 5590Sstevel@tonic-gate char *opt, 5600Sstevel@tonic-gate subcmd_defn_t *table, 5610Sstevel@tonic-gate error_t (**fp)()) 5620Sstevel@tonic-gate { 5630Sstevel@tonic-gate int i; 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate if (subcmd == NULL) { 5660Sstevel@tonic-gate bam_error(NEED_SUBCMD); 5670Sstevel@tonic-gate return (BAM_ERROR); 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate 5702115Svikram if (bam_argc != 0 || bam_argv) { 5712115Svikram if (strcmp(subcmd, "set_option") != 0 || bam_argc != 1) { 5722115Svikram bam_error(TRAILING_ARGS); 5732115Svikram usage(); 5742115Svikram return (BAM_ERROR); 5752115Svikram } 5762115Svikram } 5772115Svikram 5780Sstevel@tonic-gate if (bam_root == NULL) { 5790Sstevel@tonic-gate bam_root = rootbuf; 5800Sstevel@tonic-gate bam_rootlen = 1; 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate /* verify that subcmd is valid */ 5840Sstevel@tonic-gate for (i = 0; table[i].subcmd != NULL; i++) { 5850Sstevel@tonic-gate if (strcmp(table[i].subcmd, subcmd) == 0) 5860Sstevel@tonic-gate break; 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate if (table[i].subcmd == NULL) { 5900Sstevel@tonic-gate bam_error(INVALID_SUBCMD, subcmd); 5910Sstevel@tonic-gate return (BAM_ERROR); 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate 5942115Svikram if (table[i].unpriv == 0 && geteuid() != 0) { 5952115Svikram bam_error(MUST_BE_ROOT); 5962115Svikram return (BAM_ERROR); 5972115Svikram } 5982115Svikram 5992115Svikram /* 6002115Svikram * Currently only privileged commands need a lock 6012115Svikram */ 6022115Svikram if (table[i].unpriv == 0) 6032115Svikram bam_lock(); 6042115Svikram 6050Sstevel@tonic-gate /* subcmd verifies that opt is appropriate */ 6060Sstevel@tonic-gate if (table[i].option != OPT_OPTIONAL) { 6070Sstevel@tonic-gate if ((table[i].option == OPT_REQ) ^ (opt != NULL)) { 6080Sstevel@tonic-gate if (opt) 6090Sstevel@tonic-gate bam_error(NO_OPT_REQ, subcmd); 6100Sstevel@tonic-gate else 6110Sstevel@tonic-gate bam_error(MISS_OPT, subcmd); 6120Sstevel@tonic-gate return (BAM_ERROR); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate *fp = table[i].handler; 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate return (BAM_SUCCESS); 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate 621316Svikram 622316Svikram static char * 623621Svikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type) 624316Svikram { 625316Svikram struct extmnttab mnt; 626316Svikram struct stat sb; 627316Svikram char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32]; 628348Svikram char cmd[PATH_MAX]; 629316Svikram char *mntpt; 630316Svikram int p, l, f; 631316Svikram FILE *fp; 632316Svikram 633316Svikram assert(mnted); 634316Svikram *mnted = 0; 635316Svikram 636316Svikram /* 637621Svikram * physlice, logslice, fs_type args may be NULL 638316Svikram */ 639316Svikram if (physlice) 640316Svikram *physlice = NULL; 641621Svikram if (logslice) 642621Svikram *logslice = NULL; 643621Svikram if (fs_type) 644621Svikram *fs_type = NULL; 645316Svikram 646316Svikram if (stat(GRUB_slice, &sb) != 0) { 647316Svikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 648316Svikram return (NULL); 649316Svikram } 650316Svikram 651316Svikram fp = fopen(GRUB_slice, "r"); 652316Svikram if (fp == NULL) { 653316Svikram bam_error(OPEN_FAIL, GRUB_slice, strerror(errno)); 654316Svikram return (NULL); 655316Svikram } 656316Svikram 657316Svikram dev[0] = fstype[0] = phys[0] = '\0'; 658316Svikram p = sizeof ("PHYS_SLICE=") - 1; 659316Svikram l = sizeof ("LOG_SLICE=") - 1; 660316Svikram f = sizeof ("LOG_FSTYP=") - 1; 661316Svikram while (s_fgets(buf, sizeof (buf), fp) != NULL) { 662316Svikram if (strncmp(buf, "PHYS_SLICE=", p) == 0) { 663316Svikram (void) strlcpy(phys, buf + p, sizeof (phys)); 664316Svikram continue; 665316Svikram } 666316Svikram if (strncmp(buf, "LOG_SLICE=", l) == 0) { 667316Svikram (void) strlcpy(dev, buf + l, sizeof (dev)); 668316Svikram continue; 669316Svikram } 670316Svikram if (strncmp(buf, "LOG_FSTYP=", f) == 0) { 671316Svikram (void) strlcpy(fstype, buf + f, sizeof (fstype)); 672316Svikram continue; 673316Svikram } 674316Svikram } 675316Svikram (void) fclose(fp); 676316Svikram 677316Svikram if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') { 678316Svikram bam_error(BAD_SLICE_FILE, GRUB_slice); 679316Svikram return (NULL); 680316Svikram } 681316Svikram 682316Svikram if (physlice) { 683316Svikram *physlice = s_strdup(phys); 684316Svikram } 685621Svikram if (logslice) { 686621Svikram *logslice = s_strdup(dev); 687621Svikram } 688621Svikram if (fs_type) { 689621Svikram *fs_type = s_strdup(fstype); 690621Svikram } 691316Svikram 692316Svikram /* 693316Svikram * Check if the slice is already mounted 694316Svikram */ 695316Svikram fp = fopen(MNTTAB, "r"); 696316Svikram if (fp == NULL) { 697316Svikram bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 698621Svikram goto error; 699316Svikram } 700316Svikram 701316Svikram resetmnttab(fp); 702316Svikram 703316Svikram mntpt = NULL; 704316Svikram while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 705316Svikram if (strcmp(mnt.mnt_special, dev) == 0) { 706316Svikram mntpt = s_strdup(mnt.mnt_mountp); 707316Svikram break; 708316Svikram } 709316Svikram } 710316Svikram 711316Svikram (void) fclose(fp); 712316Svikram 713316Svikram if (mntpt) { 714316Svikram return (mntpt); 715316Svikram } 716316Svikram 717316Svikram 718316Svikram /* 719316Svikram * GRUB slice is not mounted, we need to mount it now. 720316Svikram * First create the mountpoint 721316Svikram */ 722316Svikram mntpt = s_calloc(1, PATH_MAX); 723316Svikram (void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid()); 724316Svikram if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) { 725316Svikram bam_error(MKDIR_FAILED, mntpt, strerror(errno)); 726316Svikram free(mntpt); 727621Svikram goto error; 728316Svikram } 729316Svikram 730348Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s", 731348Svikram fstype, dev, mntpt); 732348Svikram 733348Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 734621Svikram bam_error(MOUNT_FAILED, dev, fstype); 735316Svikram if (rmdir(mntpt) != 0) { 736316Svikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 737316Svikram } 738316Svikram free(mntpt); 739621Svikram goto error; 740316Svikram } 741316Svikram 742316Svikram *mnted = 1; 743316Svikram return (mntpt); 744621Svikram 745621Svikram error: 746621Svikram if (physlice) { 747621Svikram free(*physlice); 748621Svikram *physlice = NULL; 749621Svikram } 750621Svikram if (logslice) { 751621Svikram free(*logslice); 752621Svikram *logslice = NULL; 753621Svikram } 754621Svikram if (fs_type) { 755621Svikram free(*fs_type); 756621Svikram *fs_type = NULL; 757621Svikram } 758621Svikram return (NULL); 759316Svikram } 760316Svikram 761316Svikram static void 762621Svikram umount_grub_slice( 763621Svikram int mnted, 764621Svikram char *mntpt, 765621Svikram char *physlice, 766621Svikram char *logslice, 767621Svikram char *fs_type) 768316Svikram { 769348Svikram char cmd[PATH_MAX]; 770348Svikram 771316Svikram /* 772316Svikram * If we have not dealt with GRUB slice 773316Svikram * we have nothing to do - just return. 774316Svikram */ 775316Svikram if (mntpt == NULL) 776316Svikram return; 777316Svikram 778316Svikram 779316Svikram /* 780316Svikram * If we mounted the filesystem earlier in mount_grub_slice() 781316Svikram * unmount it now. 782316Svikram */ 783316Svikram if (mnted) { 784348Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s", 785348Svikram mntpt); 786348Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 787348Svikram bam_error(UMOUNT_FAILED, mntpt); 788316Svikram } 789316Svikram if (rmdir(mntpt) != 0) { 790316Svikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 791316Svikram } 792316Svikram } 793621Svikram 794316Svikram if (physlice) 795316Svikram free(physlice); 796621Svikram if (logslice) 797621Svikram free(logslice); 798621Svikram if (fs_type) 799621Svikram free(fs_type); 800621Svikram 801316Svikram free(mntpt); 802316Svikram } 803316Svikram 804621Svikram static char * 805621Svikram use_stubboot(void) 806621Svikram { 807621Svikram int mnted; 808621Svikram struct stat sb; 809621Svikram struct extmnttab mnt; 810621Svikram FILE *fp; 811621Svikram char cmd[PATH_MAX]; 812621Svikram 813621Svikram if (stat(STUBBOOT, &sb) != 0) { 814621Svikram bam_error(STUBBOOT_DIR_NOT_FOUND); 815621Svikram return (NULL); 816621Svikram } 817621Svikram 818621Svikram /* 819621Svikram * Check if stubboot is mounted. If not, mount it 820621Svikram */ 821621Svikram fp = fopen(MNTTAB, "r"); 822621Svikram if (fp == NULL) { 823621Svikram bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 824621Svikram return (NULL); 825621Svikram } 826621Svikram 827621Svikram resetmnttab(fp); 828621Svikram 829621Svikram mnted = 0; 830621Svikram while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 831621Svikram if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) { 832621Svikram mnted = 1; 833621Svikram break; 834621Svikram } 835621Svikram } 836621Svikram 837621Svikram (void) fclose(fp); 838621Svikram 839621Svikram if (mnted) 840621Svikram return (STUBBOOT); 841621Svikram 842621Svikram /* 843621Svikram * Stubboot is not mounted, mount it now. 844621Svikram * It should exist in /etc/vfstab 845621Svikram */ 846621Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s", 847621Svikram STUBBOOT); 848621Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 849621Svikram bam_error(MOUNT_MNTPT_FAILED, STUBBOOT); 850621Svikram return (NULL); 851621Svikram } 852621Svikram 853621Svikram return (STUBBOOT); 854621Svikram } 855621Svikram 856621Svikram static void 857621Svikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted) 858621Svikram { 859621Svikram /* 860621Svikram * Check if we did a temp mount of an unmounted device. 861621Svikram * If yes, print the block device and fstype for that device 862621Svikram * else it is already mounted, so we print the path to the GRUB menu. 863621Svikram */ 864621Svikram if (mnted) { 865621Svikram bam_print(GRUB_MENU_DEVICE, logslice); 866621Svikram bam_print(GRUB_MENU_FSTYPE, fstype); 867621Svikram } else { 868621Svikram bam_print(GRUB_MENU_PATH, menu_path); 869621Svikram } 870621Svikram } 871621Svikram 872621Svikram /* 873621Svikram * NOTE: A single "/" is also considered a trailing slash and will 874621Svikram * be deleted. 875621Svikram */ 876621Svikram static void 877621Svikram elide_trailing_slash(const char *src, char *dst, size_t dstsize) 878621Svikram { 879621Svikram size_t dstlen; 880621Svikram 881621Svikram assert(src); 882621Svikram assert(dst); 883621Svikram 884621Svikram (void) strlcpy(dst, src, dstsize); 885621Svikram 886621Svikram dstlen = strlen(dst); 887621Svikram if (dst[dstlen - 1] == '/') { 888621Svikram dst[dstlen - 1] = '\0'; 889621Svikram } 890621Svikram } 891621Svikram 8920Sstevel@tonic-gate static error_t 8930Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[]) 8940Sstevel@tonic-gate { 8950Sstevel@tonic-gate error_t ret; 8960Sstevel@tonic-gate char menu_path[PATH_MAX]; 8973446Smrj char path[PATH_MAX]; 8980Sstevel@tonic-gate menu_t *menu; 899621Svikram char *mntpt, *menu_root, *logslice, *fstype; 900316Svikram struct stat sb; 901316Svikram int mnted; /* set if we did a mount */ 9020Sstevel@tonic-gate error_t (*f)(menu_t *mp, char *menu_path, char *opt); 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate /* 9050Sstevel@tonic-gate * Check arguments 9060Sstevel@tonic-gate */ 9070Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f); 9080Sstevel@tonic-gate if (ret == BAM_ERROR) { 9090Sstevel@tonic-gate return (BAM_ERROR); 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate 912316Svikram mntpt = NULL; 913316Svikram mnted = 0; 914621Svikram logslice = fstype = NULL; 915621Svikram 916621Svikram /* 9173446Smrj * Check for the menu.list file: 9183446Smrj * 9193446Smrj * 1. Check for a GRUB_slice file, be it on / or 9203446Smrj * on the user-provided alternate root. 9213446Smrj * 2. Use the alternate root, if given. 9223446Smrj * 3. Check /stubboot 9233446Smrj * 4. Use / 924621Svikram */ 925621Svikram if (bam_alt_root) { 9263446Smrj (void) snprintf(path, sizeof (path), "%s%s", bam_root, 9273446Smrj GRUB_slice); 9283446Smrj } else { 9293446Smrj (void) snprintf(path, sizeof (path), "%s", GRUB_slice); 9303446Smrj } 9313446Smrj 9323446Smrj if (stat(path, &sb) == 0) { 933621Svikram mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype); 934316Svikram menu_root = mntpt; 9353446Smrj } else if (bam_alt_root) { 9363446Smrj menu_root = bam_root; 937621Svikram } else if (stat(STUBBOOT, &sb) == 0) { 938621Svikram menu_root = use_stubboot(); 939316Svikram } else { 940316Svikram menu_root = bam_root; 941316Svikram } 942316Svikram 943621Svikram if (menu_root == NULL) { 944621Svikram bam_error(CANNOT_LOCATE_GRUB_MENU); 945621Svikram return (BAM_ERROR); 946621Svikram } 947621Svikram 948621Svikram elide_trailing_slash(menu_root, menu_path, sizeof (menu_path)); 949621Svikram (void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path)); 950621Svikram 951621Svikram /* 952621Svikram * If listing the menu, display the active menu 953621Svikram * location 954621Svikram */ 955621Svikram if (strcmp(subcmd, "list_entry") == 0) { 956621Svikram disp_active_menu_locn(menu_path, logslice, fstype, mnted); 957621Svikram } 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate menu = menu_read(menu_path); 9600Sstevel@tonic-gate assert(menu); 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate /* 9630Sstevel@tonic-gate * Special handling for setting timeout and default 9640Sstevel@tonic-gate */ 9650Sstevel@tonic-gate if (strcmp(subcmd, "set_option") == 0) { 9660Sstevel@tonic-gate if (largc != 1 || largv[0] == NULL) { 9670Sstevel@tonic-gate usage(); 968621Svikram menu_free(menu); 969621Svikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 9700Sstevel@tonic-gate return (BAM_ERROR); 9710Sstevel@tonic-gate } 9720Sstevel@tonic-gate opt = largv[0]; 9730Sstevel@tonic-gate } else if (largc != 0) { 9740Sstevel@tonic-gate usage(); 975621Svikram menu_free(menu); 976621Svikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 9770Sstevel@tonic-gate return (BAM_ERROR); 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate 9803446Smrj ret = dboot_or_multiboot(bam_root); 9813446Smrj if (ret != BAM_SUCCESS) 9823446Smrj return (ret); 9833446Smrj 9840Sstevel@tonic-gate /* 9850Sstevel@tonic-gate * Once the sub-cmd handler has run 9860Sstevel@tonic-gate * only the line field is guaranteed to have valid values 9870Sstevel@tonic-gate */ 9883446Smrj if ((strcmp(subcmd, "update_entry") == 0) || 9893446Smrj (strcmp(subcmd, "upgrade") == 0)) 9900Sstevel@tonic-gate ret = f(menu, bam_root, opt); 9910Sstevel@tonic-gate else 9920Sstevel@tonic-gate ret = f(menu, menu_path, opt); 9930Sstevel@tonic-gate if (ret == BAM_WRITE) { 994316Svikram ret = menu_write(menu_root, menu); 9950Sstevel@tonic-gate } 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate menu_free(menu); 9980Sstevel@tonic-gate 999621Svikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 1000316Svikram 10010Sstevel@tonic-gate return (ret); 10020Sstevel@tonic-gate } 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate static error_t 10060Sstevel@tonic-gate bam_archive( 10070Sstevel@tonic-gate char *subcmd, 10080Sstevel@tonic-gate char *opt) 10090Sstevel@tonic-gate { 10100Sstevel@tonic-gate error_t ret; 10110Sstevel@tonic-gate error_t (*f)(char *root, char *opt); 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate /* 1014662Sszhou * Add trailing / for archive subcommands 1015662Sszhou */ 1016662Sszhou if (rootbuf[strlen(rootbuf) - 1] != '/') 1017662Sszhou (void) strcat(rootbuf, "/"); 1018662Sszhou bam_rootlen = strlen(rootbuf); 1019662Sszhou 1020662Sszhou /* 10210Sstevel@tonic-gate * Check arguments 10220Sstevel@tonic-gate */ 10230Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f); 10240Sstevel@tonic-gate if (ret != BAM_SUCCESS) { 10250Sstevel@tonic-gate return (BAM_ERROR); 10260Sstevel@tonic-gate } 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate #if defined(__sparc) 10290Sstevel@tonic-gate /* 10300Sstevel@tonic-gate * A NOP if called on SPARC during reboot 10310Sstevel@tonic-gate */ 10320Sstevel@tonic-gate if (strcmp(subcmd, "update_all") == 0) 10330Sstevel@tonic-gate return (BAM_SUCCESS); 10340Sstevel@tonic-gate else if (strcmp(subcmd, "update") != 0) 10350Sstevel@tonic-gate sparc_abort(); 10360Sstevel@tonic-gate #endif 10370Sstevel@tonic-gate 10383446Smrj ret = dboot_or_multiboot(rootbuf); 10393446Smrj if (ret != BAM_SUCCESS) 10403446Smrj return (ret); 10413446Smrj 10420Sstevel@tonic-gate /* 10430Sstevel@tonic-gate * Check archive not supported with update_all 10440Sstevel@tonic-gate * since it is awkward to display out-of-sync 10450Sstevel@tonic-gate * information for each BE. 10460Sstevel@tonic-gate */ 10470Sstevel@tonic-gate if (bam_check && strcmp(subcmd, "update_all") == 0) { 10480Sstevel@tonic-gate bam_error(CHECK_NOT_SUPPORTED, subcmd); 10490Sstevel@tonic-gate return (BAM_ERROR); 10500Sstevel@tonic-gate } 10510Sstevel@tonic-gate 1052316Svikram if (strcmp(subcmd, "update_all") == 0) 1053316Svikram bam_update_all = 1; 1054316Svikram 1055316Svikram ret = f(bam_root, opt); 1056316Svikram 1057316Svikram bam_update_all = 0; 1058316Svikram 1059316Svikram return (ret); 10600Sstevel@tonic-gate } 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate /*PRINTFLIKE1*/ 10633446Smrj void 10640Sstevel@tonic-gate bam_error(char *format, ...) 10650Sstevel@tonic-gate { 10660Sstevel@tonic-gate va_list ap; 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate va_start(ap, format); 10690Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", prog); 10700Sstevel@tonic-gate (void) vfprintf(stderr, format, ap); 10710Sstevel@tonic-gate va_end(ap); 10720Sstevel@tonic-gate } 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate /*PRINTFLIKE1*/ 10750Sstevel@tonic-gate static void 10760Sstevel@tonic-gate bam_print(char *format, ...) 10770Sstevel@tonic-gate { 10780Sstevel@tonic-gate va_list ap; 10790Sstevel@tonic-gate 10800Sstevel@tonic-gate va_start(ap, format); 10810Sstevel@tonic-gate (void) vfprintf(stdout, format, ap); 10820Sstevel@tonic-gate va_end(ap); 10830Sstevel@tonic-gate } 10840Sstevel@tonic-gate 10853446Smrj /*PRINTFLIKE1*/ 10863446Smrj void 10873446Smrj bam_print_stderr(char *format, ...) 10883446Smrj { 10893446Smrj va_list ap; 10903446Smrj 10913446Smrj va_start(ap, format); 10923446Smrj (void) vfprintf(stderr, format, ap); 10933446Smrj va_end(ap); 10943446Smrj } 10953446Smrj 10960Sstevel@tonic-gate static void 10970Sstevel@tonic-gate bam_exit(int excode) 10980Sstevel@tonic-gate { 10990Sstevel@tonic-gate bam_unlock(); 11000Sstevel@tonic-gate exit(excode); 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate static void 11040Sstevel@tonic-gate bam_lock(void) 11050Sstevel@tonic-gate { 11060Sstevel@tonic-gate struct flock lock; 11070Sstevel@tonic-gate pid_t pid; 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS); 11100Sstevel@tonic-gate if (bam_lock_fd < 0) { 11110Sstevel@tonic-gate /* 11120Sstevel@tonic-gate * We may be invoked early in boot for archive verification. 11130Sstevel@tonic-gate * In this case, root is readonly and /var/run may not exist. 11140Sstevel@tonic-gate * Proceed without the lock 11150Sstevel@tonic-gate */ 11160Sstevel@tonic-gate if (errno == EROFS || errno == ENOENT) { 11170Sstevel@tonic-gate bam_root_readonly = 1; 11180Sstevel@tonic-gate return; 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno)); 11220Sstevel@tonic-gate bam_exit(1); 11230Sstevel@tonic-gate } 11240Sstevel@tonic-gate 11250Sstevel@tonic-gate lock.l_type = F_WRLCK; 11260Sstevel@tonic-gate lock.l_whence = SEEK_SET; 11270Sstevel@tonic-gate lock.l_start = 0; 11280Sstevel@tonic-gate lock.l_len = 0; 11290Sstevel@tonic-gate 11300Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) { 11310Sstevel@tonic-gate if (errno != EACCES && errno != EAGAIN) { 11320Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11330Sstevel@tonic-gate (void) close(bam_lock_fd); 11340Sstevel@tonic-gate bam_lock_fd = -1; 11350Sstevel@tonic-gate bam_exit(1); 11360Sstevel@tonic-gate } 11370Sstevel@tonic-gate pid = 0; 11380Sstevel@tonic-gate (void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0); 11390Sstevel@tonic-gate bam_print(FILE_LOCKED, pid); 11400Sstevel@tonic-gate 11410Sstevel@tonic-gate lock.l_type = F_WRLCK; 11420Sstevel@tonic-gate lock.l_whence = SEEK_SET; 11430Sstevel@tonic-gate lock.l_start = 0; 11440Sstevel@tonic-gate lock.l_len = 0; 11450Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) { 11460Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11470Sstevel@tonic-gate (void) close(bam_lock_fd); 11480Sstevel@tonic-gate bam_lock_fd = -1; 11490Sstevel@tonic-gate bam_exit(1); 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate } 11520Sstevel@tonic-gate 11530Sstevel@tonic-gate /* We own the lock now */ 11540Sstevel@tonic-gate pid = getpid(); 11550Sstevel@tonic-gate (void) write(bam_lock_fd, &pid, sizeof (pid)); 11560Sstevel@tonic-gate } 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate static void 11590Sstevel@tonic-gate bam_unlock(void) 11600Sstevel@tonic-gate { 11610Sstevel@tonic-gate struct flock unlock; 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate /* 11640Sstevel@tonic-gate * NOP if we don't hold the lock 11650Sstevel@tonic-gate */ 11660Sstevel@tonic-gate if (bam_lock_fd < 0) { 11670Sstevel@tonic-gate return; 11680Sstevel@tonic-gate } 11690Sstevel@tonic-gate 11700Sstevel@tonic-gate unlock.l_type = F_UNLCK; 11710Sstevel@tonic-gate unlock.l_whence = SEEK_SET; 11720Sstevel@tonic-gate unlock.l_start = 0; 11730Sstevel@tonic-gate unlock.l_len = 0; 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) { 11760Sstevel@tonic-gate bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11770Sstevel@tonic-gate } 11780Sstevel@tonic-gate 11790Sstevel@tonic-gate if (close(bam_lock_fd) == -1) { 11800Sstevel@tonic-gate bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno)); 11810Sstevel@tonic-gate } 11820Sstevel@tonic-gate bam_lock_fd = -1; 11830Sstevel@tonic-gate } 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate static error_t 11860Sstevel@tonic-gate list_archive(char *root, char *opt) 11870Sstevel@tonic-gate { 11880Sstevel@tonic-gate filelist_t flist; 11890Sstevel@tonic-gate filelist_t *flistp = &flist; 11900Sstevel@tonic-gate line_t *lp; 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate assert(root); 11930Sstevel@tonic-gate assert(opt == NULL); 11940Sstevel@tonic-gate 11950Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 11960Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 11970Sstevel@tonic-gate return (BAM_ERROR); 11980Sstevel@tonic-gate } 11990Sstevel@tonic-gate assert(flistp->head && flistp->tail); 12000Sstevel@tonic-gate 12010Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 12020Sstevel@tonic-gate bam_print(PRINT, lp->line); 12030Sstevel@tonic-gate } 12040Sstevel@tonic-gate 12050Sstevel@tonic-gate filelist_free(flistp); 12060Sstevel@tonic-gate 12070Sstevel@tonic-gate return (BAM_SUCCESS); 12080Sstevel@tonic-gate } 12090Sstevel@tonic-gate 12100Sstevel@tonic-gate /* 12110Sstevel@tonic-gate * This routine writes a list of lines to a file. 12120Sstevel@tonic-gate * The list is *not* freed 12130Sstevel@tonic-gate */ 12140Sstevel@tonic-gate static error_t 12150Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start) 12160Sstevel@tonic-gate { 12170Sstevel@tonic-gate char tmpfile[PATH_MAX]; 12180Sstevel@tonic-gate char path[PATH_MAX]; 12190Sstevel@tonic-gate FILE *fp; 12200Sstevel@tonic-gate int ret; 12210Sstevel@tonic-gate struct stat sb; 12220Sstevel@tonic-gate mode_t mode; 12230Sstevel@tonic-gate uid_t root_uid; 12240Sstevel@tonic-gate gid_t sys_gid; 12250Sstevel@tonic-gate struct passwd *pw; 12260Sstevel@tonic-gate struct group *gp; 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, final); 12300Sstevel@tonic-gate 12310Sstevel@tonic-gate if (start == NULL) { 12320Sstevel@tonic-gate if (stat(path, &sb) != -1) { 12330Sstevel@tonic-gate bam_print(UNLINK_EMPTY, path); 12340Sstevel@tonic-gate if (unlink(path) != 0) { 12350Sstevel@tonic-gate bam_error(UNLINK_FAIL, path, strerror(errno)); 12360Sstevel@tonic-gate return (BAM_ERROR); 12370Sstevel@tonic-gate } else { 12380Sstevel@tonic-gate return (BAM_SUCCESS); 12390Sstevel@tonic-gate } 12400Sstevel@tonic-gate } 12410Sstevel@tonic-gate } 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate /* 12440Sstevel@tonic-gate * Preserve attributes of existing file if possible, 12450Sstevel@tonic-gate * otherwise ask the system for uid/gid of root/sys. 12460Sstevel@tonic-gate * If all fails, fall back on hard-coded defaults. 12470Sstevel@tonic-gate */ 12480Sstevel@tonic-gate if (stat(path, &sb) != -1) { 12490Sstevel@tonic-gate mode = sb.st_mode; 12500Sstevel@tonic-gate root_uid = sb.st_uid; 12510Sstevel@tonic-gate sys_gid = sb.st_gid; 12520Sstevel@tonic-gate } else { 12530Sstevel@tonic-gate mode = DEFAULT_DEV_MODE; 12540Sstevel@tonic-gate if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) { 12550Sstevel@tonic-gate root_uid = pw->pw_uid; 12560Sstevel@tonic-gate } else { 12570Sstevel@tonic-gate if (bam_verbose) 12580Sstevel@tonic-gate bam_error(CANT_FIND_USER, 12590Sstevel@tonic-gate DEFAULT_DEV_USER, DEFAULT_DEV_UID); 12600Sstevel@tonic-gate root_uid = (uid_t)DEFAULT_DEV_UID; 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) { 12630Sstevel@tonic-gate sys_gid = gp->gr_gid; 12640Sstevel@tonic-gate } else { 12650Sstevel@tonic-gate if (bam_verbose) 12660Sstevel@tonic-gate bam_error(CANT_FIND_GROUP, 12670Sstevel@tonic-gate DEFAULT_DEV_GROUP, DEFAULT_DEV_GID); 12680Sstevel@tonic-gate sys_gid = (gid_t)DEFAULT_DEV_GID; 12690Sstevel@tonic-gate } 12700Sstevel@tonic-gate } 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate (void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp); 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate /* Truncate tmpfile first */ 12750Sstevel@tonic-gate fp = fopen(tmpfile, "w"); 12760Sstevel@tonic-gate if (fp == NULL) { 12770Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 12780Sstevel@tonic-gate return (BAM_ERROR); 12790Sstevel@tonic-gate } 12800Sstevel@tonic-gate ret = fclose(fp); 12810Sstevel@tonic-gate if (ret == EOF) { 12820Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 12830Sstevel@tonic-gate return (BAM_ERROR); 12840Sstevel@tonic-gate } 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate /* Now open it in append mode */ 12870Sstevel@tonic-gate fp = fopen(tmpfile, "a"); 12880Sstevel@tonic-gate if (fp == NULL) { 12890Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 12900Sstevel@tonic-gate return (BAM_ERROR); 12910Sstevel@tonic-gate } 12920Sstevel@tonic-gate 12930Sstevel@tonic-gate for (; start; start = start->next) { 12940Sstevel@tonic-gate ret = s_fputs(start->line, fp); 12950Sstevel@tonic-gate if (ret == EOF) { 12960Sstevel@tonic-gate bam_error(WRITE_FAIL, tmpfile, strerror(errno)); 12970Sstevel@tonic-gate (void) fclose(fp); 12980Sstevel@tonic-gate return (BAM_ERROR); 12990Sstevel@tonic-gate } 13000Sstevel@tonic-gate } 13010Sstevel@tonic-gate 13020Sstevel@tonic-gate ret = fclose(fp); 13030Sstevel@tonic-gate if (ret == EOF) { 13040Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 13050Sstevel@tonic-gate return (BAM_ERROR); 13060Sstevel@tonic-gate } 13070Sstevel@tonic-gate 13080Sstevel@tonic-gate /* 1309271Sjg * Set up desired attributes. Ignore failures on filesystems 1310271Sjg * not supporting these operations - pcfs reports unsupported 1311271Sjg * operations as EINVAL. 13120Sstevel@tonic-gate */ 13130Sstevel@tonic-gate ret = chmod(tmpfile, mode); 1314271Sjg if (ret == -1 && 1315271Sjg errno != EINVAL && errno != ENOTSUP) { 13160Sstevel@tonic-gate bam_error(CHMOD_FAIL, tmpfile, strerror(errno)); 13170Sstevel@tonic-gate return (BAM_ERROR); 13180Sstevel@tonic-gate } 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate ret = chown(tmpfile, root_uid, sys_gid); 1321271Sjg if (ret == -1 && 1322271Sjg errno != EINVAL && errno != ENOTSUP) { 13230Sstevel@tonic-gate bam_error(CHOWN_FAIL, tmpfile, strerror(errno)); 13240Sstevel@tonic-gate return (BAM_ERROR); 13250Sstevel@tonic-gate } 13260Sstevel@tonic-gate 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate /* 13290Sstevel@tonic-gate * Do an atomic rename 13300Sstevel@tonic-gate */ 13310Sstevel@tonic-gate ret = rename(tmpfile, path); 13320Sstevel@tonic-gate if (ret != 0) { 13330Sstevel@tonic-gate bam_error(RENAME_FAIL, path, strerror(errno)); 13340Sstevel@tonic-gate return (BAM_ERROR); 13350Sstevel@tonic-gate } 13360Sstevel@tonic-gate 13370Sstevel@tonic-gate return (BAM_SUCCESS); 13380Sstevel@tonic-gate } 13390Sstevel@tonic-gate 13400Sstevel@tonic-gate /* 13410Sstevel@tonic-gate * This function should always return 0 - since we want 13420Sstevel@tonic-gate * to create stat data for *all* files in the list. 13430Sstevel@tonic-gate */ 13440Sstevel@tonic-gate /*ARGSUSED*/ 13450Sstevel@tonic-gate static int 13460Sstevel@tonic-gate cmpstat( 13470Sstevel@tonic-gate const char *file, 13480Sstevel@tonic-gate const struct stat *stat, 13490Sstevel@tonic-gate int flags, 13500Sstevel@tonic-gate struct FTW *ftw) 13510Sstevel@tonic-gate { 13520Sstevel@tonic-gate uint_t sz; 13530Sstevel@tonic-gate uint64_t *value; 13540Sstevel@tonic-gate uint64_t filestat[2]; 13550Sstevel@tonic-gate int error; 13560Sstevel@tonic-gate 13572334Ssetje struct safefile *safefilep; 13582334Ssetje FILE *fp; 13592334Ssetje 13600Sstevel@tonic-gate /* 13610Sstevel@tonic-gate * We only want regular files 13620Sstevel@tonic-gate */ 13630Sstevel@tonic-gate if (!S_ISREG(stat->st_mode)) 13640Sstevel@tonic-gate return (0); 13650Sstevel@tonic-gate 13660Sstevel@tonic-gate /* 13670Sstevel@tonic-gate * new_nvlp may be NULL if there were errors earlier 13680Sstevel@tonic-gate * but this is not fatal to update determination. 13690Sstevel@tonic-gate */ 13700Sstevel@tonic-gate if (walk_arg.new_nvlp) { 13710Sstevel@tonic-gate filestat[0] = stat->st_size; 13720Sstevel@tonic-gate filestat[1] = stat->st_mtime; 13730Sstevel@tonic-gate error = nvlist_add_uint64_array(walk_arg.new_nvlp, 13740Sstevel@tonic-gate file + bam_rootlen, filestat, 2); 13750Sstevel@tonic-gate if (error) 13760Sstevel@tonic-gate bam_error(NVADD_FAIL, file, strerror(error)); 13770Sstevel@tonic-gate } 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate /* 13800Sstevel@tonic-gate * The remaining steps are only required if we haven't made a 13810Sstevel@tonic-gate * decision about update or if we are checking (-n) 13820Sstevel@tonic-gate */ 13830Sstevel@tonic-gate if (walk_arg.need_update && !bam_check) 13840Sstevel@tonic-gate return (0); 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate /* 13872334Ssetje * If we are invoked as part of system/filesyste/boot-archive, then 13882334Ssetje * there are a number of things we should not worry about 13890Sstevel@tonic-gate */ 13902334Ssetje if (bam_smf_check) { 13912334Ssetje /* ignore amd64 modules unless we are booted amd64. */ 13922334Ssetje if (!is_amd64() && strstr(file, "/amd64/") != 0) 13932334Ssetje return (0); 13942334Ssetje 13952334Ssetje /* read in list of safe files */ 13962334Ssetje if (safefiles == NULL) 13972334Ssetje if (fp = fopen("/boot/solaris/filelist.safe", "r")) { 13982334Ssetje safefiles = s_calloc(1, 13992334Ssetje sizeof (struct safefile)); 14002334Ssetje safefilep = safefiles; 14012334Ssetje safefilep->name = s_calloc(1, MAXPATHLEN + 14022334Ssetje MAXNAMELEN); 14032334Ssetje safefilep->next = NULL; 14042334Ssetje while (s_fgets(safefilep->name, MAXPATHLEN + 14052334Ssetje MAXNAMELEN, fp) != NULL) { 14062334Ssetje safefilep->next = s_calloc(1, 14072334Ssetje sizeof (struct safefile)); 14082334Ssetje safefilep = safefilep->next; 14092334Ssetje safefilep->name = s_calloc(1, 14102334Ssetje MAXPATHLEN + MAXNAMELEN); 14112334Ssetje safefilep->next = NULL; 14122334Ssetje } 14132334Ssetje (void) fclose(fp); 14142334Ssetje } 14152334Ssetje } 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate /* 14180Sstevel@tonic-gate * We need an update if file doesn't exist in old archive 14190Sstevel@tonic-gate */ 14200Sstevel@tonic-gate if (walk_arg.old_nvlp == NULL || 14210Sstevel@tonic-gate nvlist_lookup_uint64_array(walk_arg.old_nvlp, 14220Sstevel@tonic-gate file + bam_rootlen, &value, &sz) != 0) { 14230Sstevel@tonic-gate if (bam_smf_check) /* ignore new during smf check */ 14240Sstevel@tonic-gate return (0); 14250Sstevel@tonic-gate walk_arg.need_update = 1; 14260Sstevel@tonic-gate if (bam_verbose) 14270Sstevel@tonic-gate bam_print(PARSEABLE_NEW_FILE, file); 14280Sstevel@tonic-gate return (0); 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate /* 14320Sstevel@tonic-gate * File exists in old archive. Check if file has changed 14330Sstevel@tonic-gate */ 14340Sstevel@tonic-gate assert(sz == 2); 14350Sstevel@tonic-gate bcopy(value, filestat, sizeof (filestat)); 14360Sstevel@tonic-gate 14370Sstevel@tonic-gate if (filestat[0] != stat->st_size || 14380Sstevel@tonic-gate filestat[1] != stat->st_mtime) { 14393615Ssetje if (bam_smf_check) { 14403615Ssetje safefilep = safefiles; 14413615Ssetje while (safefilep != NULL) { 14423615Ssetje if (strcmp(file + bam_rootlen, 14433615Ssetje safefilep->name) == 0) { 14443615Ssetje (void) creat(NEED_UPDATE_FILE, 0644); 14453615Ssetje return (0); 14463615Ssetje } 14473615Ssetje safefilep = safefilep->next; 14483615Ssetje } 14493615Ssetje } 14500Sstevel@tonic-gate walk_arg.need_update = 1; 14510Sstevel@tonic-gate if (bam_verbose) 14520Sstevel@tonic-gate if (bam_smf_check) 14530Sstevel@tonic-gate bam_print(" %s\n", file); 14540Sstevel@tonic-gate else 14550Sstevel@tonic-gate bam_print(PARSEABLE_OUT_DATE, file); 14560Sstevel@tonic-gate } 14570Sstevel@tonic-gate 14580Sstevel@tonic-gate return (0); 14590Sstevel@tonic-gate } 14600Sstevel@tonic-gate 14610Sstevel@tonic-gate /* 14620Sstevel@tonic-gate * Check flags and presence of required files. 14630Sstevel@tonic-gate * The force flag and/or absence of files should 14640Sstevel@tonic-gate * trigger an update. 14650Sstevel@tonic-gate * Suppress stdout output if check (-n) option is set 14660Sstevel@tonic-gate * (as -n should only produce parseable output.) 14670Sstevel@tonic-gate */ 14680Sstevel@tonic-gate static void 14690Sstevel@tonic-gate check_flags_and_files(char *root) 14700Sstevel@tonic-gate { 14710Sstevel@tonic-gate char path[PATH_MAX]; 14720Sstevel@tonic-gate struct stat sb; 14730Sstevel@tonic-gate 14740Sstevel@tonic-gate /* 14750Sstevel@tonic-gate * if force, create archive unconditionally 14760Sstevel@tonic-gate */ 14770Sstevel@tonic-gate if (bam_force) { 14780Sstevel@tonic-gate walk_arg.need_update = 1; 14790Sstevel@tonic-gate if (bam_verbose && !bam_check) 14800Sstevel@tonic-gate bam_print(UPDATE_FORCE); 14810Sstevel@tonic-gate return; 14820Sstevel@tonic-gate } 14830Sstevel@tonic-gate 14840Sstevel@tonic-gate /* 14850Sstevel@tonic-gate * If archive is missing, create archive 14860Sstevel@tonic-gate */ 14873446Smrj (void) snprintf(path, sizeof (path), "%s%s", root, 14883446Smrj DIRECT_BOOT_ARCHIVE_32); 14890Sstevel@tonic-gate if (stat(path, &sb) != 0) { 14900Sstevel@tonic-gate if (bam_verbose && !bam_check) 14910Sstevel@tonic-gate bam_print(UPDATE_ARCH_MISS, path); 14920Sstevel@tonic-gate walk_arg.need_update = 1; 14930Sstevel@tonic-gate return; 14940Sstevel@tonic-gate } 14953446Smrj if (bam_direct == BAM_DIRECT_DBOOT) { 14963446Smrj (void) snprintf(path, sizeof (path), "%s%s", root, 14973446Smrj DIRECT_BOOT_ARCHIVE_64); 14983446Smrj if (stat(path, &sb) != 0) { 14993446Smrj if (bam_verbose && !bam_check) 15003446Smrj bam_print(UPDATE_ARCH_MISS, path); 15013446Smrj walk_arg.need_update = 1; 15023446Smrj return; 15033446Smrj } 15043446Smrj } 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate 15070Sstevel@tonic-gate static error_t 15080Sstevel@tonic-gate read_one_list(char *root, filelist_t *flistp, char *filelist) 15090Sstevel@tonic-gate { 15100Sstevel@tonic-gate char path[PATH_MAX]; 15110Sstevel@tonic-gate FILE *fp; 15120Sstevel@tonic-gate char buf[BAM_MAXLINE]; 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, filelist); 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate fp = fopen(path, "r"); 15170Sstevel@tonic-gate if (fp == NULL) { 15180Sstevel@tonic-gate if (bam_debug) 15190Sstevel@tonic-gate bam_error(FLIST_FAIL, path, strerror(errno)); 15200Sstevel@tonic-gate return (BAM_ERROR); 15210Sstevel@tonic-gate } 15220Sstevel@tonic-gate while (s_fgets(buf, sizeof (buf), fp) != NULL) { 1523316Svikram /* skip blank lines */ 1524316Svikram if (strspn(buf, " \t") == strlen(buf)) 1525316Svikram continue; 15260Sstevel@tonic-gate append_to_flist(flistp, buf); 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate if (fclose(fp) != 0) { 15290Sstevel@tonic-gate bam_error(CLOSE_FAIL, path, strerror(errno)); 15300Sstevel@tonic-gate return (BAM_ERROR); 15310Sstevel@tonic-gate } 15320Sstevel@tonic-gate return (BAM_SUCCESS); 15330Sstevel@tonic-gate } 15340Sstevel@tonic-gate 15350Sstevel@tonic-gate static error_t 15360Sstevel@tonic-gate read_list(char *root, filelist_t *flistp) 15370Sstevel@tonic-gate { 15380Sstevel@tonic-gate int rval; 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate /* 15430Sstevel@tonic-gate * Read current lists of files - only the first is mandatory 15440Sstevel@tonic-gate */ 15450Sstevel@tonic-gate rval = read_one_list(root, flistp, BOOT_FILE_LIST); 15460Sstevel@tonic-gate if (rval != BAM_SUCCESS) 15470Sstevel@tonic-gate return (rval); 15480Sstevel@tonic-gate (void) read_one_list(root, flistp, ETC_FILE_LIST); 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate if (flistp->head == NULL) { 15510Sstevel@tonic-gate bam_error(NO_FLIST); 15520Sstevel@tonic-gate return (BAM_ERROR); 15530Sstevel@tonic-gate } 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate return (BAM_SUCCESS); 15560Sstevel@tonic-gate } 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate static void 15590Sstevel@tonic-gate getoldstat(char *root) 15600Sstevel@tonic-gate { 15610Sstevel@tonic-gate char path[PATH_MAX]; 15620Sstevel@tonic-gate int fd, error; 15630Sstevel@tonic-gate struct stat sb; 15640Sstevel@tonic-gate char *ostat; 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 15670Sstevel@tonic-gate fd = open(path, O_RDONLY); 15680Sstevel@tonic-gate if (fd == -1) { 15690Sstevel@tonic-gate if (bam_verbose) 15700Sstevel@tonic-gate bam_print(OPEN_FAIL, path, strerror(errno)); 15710Sstevel@tonic-gate walk_arg.need_update = 1; 15720Sstevel@tonic-gate return; 15730Sstevel@tonic-gate } 15740Sstevel@tonic-gate 15750Sstevel@tonic-gate if (fstat(fd, &sb) != 0) { 15760Sstevel@tonic-gate bam_error(STAT_FAIL, path, strerror(errno)); 15770Sstevel@tonic-gate (void) close(fd); 15780Sstevel@tonic-gate walk_arg.need_update = 1; 15790Sstevel@tonic-gate return; 15800Sstevel@tonic-gate } 15810Sstevel@tonic-gate 15820Sstevel@tonic-gate ostat = s_calloc(1, sb.st_size); 15830Sstevel@tonic-gate 15840Sstevel@tonic-gate if (read(fd, ostat, sb.st_size) != sb.st_size) { 15850Sstevel@tonic-gate bam_error(READ_FAIL, path, strerror(errno)); 15860Sstevel@tonic-gate (void) close(fd); 15870Sstevel@tonic-gate free(ostat); 15880Sstevel@tonic-gate walk_arg.need_update = 1; 15890Sstevel@tonic-gate return; 15900Sstevel@tonic-gate } 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate (void) close(fd); 15930Sstevel@tonic-gate 15940Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 15950Sstevel@tonic-gate error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0); 15960Sstevel@tonic-gate 15970Sstevel@tonic-gate free(ostat); 15980Sstevel@tonic-gate 15990Sstevel@tonic-gate if (error) { 16000Sstevel@tonic-gate bam_error(UNPACK_FAIL, path, strerror(error)); 16010Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 16020Sstevel@tonic-gate walk_arg.need_update = 1; 16030Sstevel@tonic-gate return; 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate } 16060Sstevel@tonic-gate 16072583Svikram /* 16082583Svikram * Checks if a file in the current (old) archive has 16092583Svikram * been deleted from the root filesystem. This is needed for 16102583Svikram * software like Trusted Extensions (TX) that switch early 16112583Svikram * in boot based on presence/absence of a kernel module. 16122583Svikram */ 16132583Svikram static void 16142583Svikram check4stale(char *root) 16152583Svikram { 16162583Svikram nvpair_t *nvp; 16172583Svikram nvlist_t *nvlp; 16182583Svikram char *file; 16192583Svikram char path[PATH_MAX]; 16202583Svikram struct stat sb; 16212583Svikram 16222583Svikram /* 16232583Svikram * Skip stale file check during smf check 16242583Svikram */ 16252583Svikram if (bam_smf_check) 16262583Svikram return; 16272583Svikram 16282583Svikram /* Nothing to do if no old stats */ 16292583Svikram if ((nvlp = walk_arg.old_nvlp) == NULL) 16302583Svikram return; 16312583Svikram 16322583Svikram for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp; 16332583Svikram nvp = nvlist_next_nvpair(nvlp, nvp)) { 16342583Svikram file = nvpair_name(nvp); 16352583Svikram if (file == NULL) 16362583Svikram continue; 16372583Svikram (void) snprintf(path, sizeof (path), "%s/%s", 16382583Svikram root, file); 16392583Svikram if (stat(path, &sb) == -1) { 16402583Svikram walk_arg.need_update = 1; 16412583Svikram if (bam_verbose) 16422583Svikram bam_print(PARSEABLE_STALE_FILE, path); 16432583Svikram } 16442583Svikram } 16452583Svikram } 16462583Svikram 16470Sstevel@tonic-gate static void 16480Sstevel@tonic-gate create_newstat(void) 16490Sstevel@tonic-gate { 16500Sstevel@tonic-gate int error; 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0); 16530Sstevel@tonic-gate if (error) { 16540Sstevel@tonic-gate /* 16550Sstevel@tonic-gate * Not fatal - we can still create archive 16560Sstevel@tonic-gate */ 16570Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 16580Sstevel@tonic-gate bam_error(NVALLOC_FAIL, strerror(error)); 16590Sstevel@tonic-gate } 16600Sstevel@tonic-gate } 16610Sstevel@tonic-gate 16620Sstevel@tonic-gate static void 16630Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp) 16640Sstevel@tonic-gate { 16650Sstevel@tonic-gate char path[PATH_MAX]; 16660Sstevel@tonic-gate line_t *lp; 16670Sstevel@tonic-gate 16680Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 16690Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, lp->line); 16700Sstevel@tonic-gate /* XXX shouldn't we use FTW_MOUNT ? */ 16710Sstevel@tonic-gate if (nftw(path, cmpstat, 20, 0) == -1) { 16720Sstevel@tonic-gate /* 16730Sstevel@tonic-gate * Some files may not exist. 16740Sstevel@tonic-gate * For example: etc/rtc_config on a x86 diskless system 16750Sstevel@tonic-gate * Emit verbose message only 16760Sstevel@tonic-gate */ 16770Sstevel@tonic-gate if (bam_verbose) 16780Sstevel@tonic-gate bam_print(NFTW_FAIL, path, strerror(errno)); 16790Sstevel@tonic-gate } 16800Sstevel@tonic-gate } 16810Sstevel@tonic-gate } 16820Sstevel@tonic-gate 16830Sstevel@tonic-gate static void 16840Sstevel@tonic-gate savenew(char *root) 16850Sstevel@tonic-gate { 16860Sstevel@tonic-gate char path[PATH_MAX]; 16870Sstevel@tonic-gate char path2[PATH_MAX]; 16880Sstevel@tonic-gate size_t sz; 16890Sstevel@tonic-gate char *nstat; 16900Sstevel@tonic-gate int fd, wrote, error; 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate nstat = NULL; 16930Sstevel@tonic-gate sz = 0; 16940Sstevel@tonic-gate error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz, 16950Sstevel@tonic-gate NV_ENCODE_XDR, 0); 16960Sstevel@tonic-gate if (error) { 16970Sstevel@tonic-gate bam_error(PACK_FAIL, strerror(error)); 16980Sstevel@tonic-gate return; 16990Sstevel@tonic-gate } 17000Sstevel@tonic-gate 17010Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP); 17020Sstevel@tonic-gate fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE); 17030Sstevel@tonic-gate if (fd == -1) { 17040Sstevel@tonic-gate bam_error(OPEN_FAIL, path, strerror(errno)); 17050Sstevel@tonic-gate free(nstat); 17060Sstevel@tonic-gate return; 17070Sstevel@tonic-gate } 17080Sstevel@tonic-gate wrote = write(fd, nstat, sz); 17090Sstevel@tonic-gate if (wrote != sz) { 17100Sstevel@tonic-gate bam_error(WRITE_FAIL, path, strerror(errno)); 17110Sstevel@tonic-gate (void) close(fd); 17120Sstevel@tonic-gate free(nstat); 17130Sstevel@tonic-gate return; 17140Sstevel@tonic-gate } 17150Sstevel@tonic-gate (void) close(fd); 17160Sstevel@tonic-gate free(nstat); 17170Sstevel@tonic-gate 17180Sstevel@tonic-gate (void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT); 17190Sstevel@tonic-gate if (rename(path, path2) != 0) { 17200Sstevel@tonic-gate bam_error(RENAME_FAIL, path2, strerror(errno)); 17210Sstevel@tonic-gate } 17220Sstevel@tonic-gate } 17230Sstevel@tonic-gate 17240Sstevel@tonic-gate static void 17250Sstevel@tonic-gate clear_walk_args(void) 17260Sstevel@tonic-gate { 17270Sstevel@tonic-gate if (walk_arg.old_nvlp) 17280Sstevel@tonic-gate nvlist_free(walk_arg.old_nvlp); 17290Sstevel@tonic-gate if (walk_arg.new_nvlp) 17300Sstevel@tonic-gate nvlist_free(walk_arg.new_nvlp); 17310Sstevel@tonic-gate walk_arg.need_update = 0; 17320Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 17330Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 17340Sstevel@tonic-gate } 17350Sstevel@tonic-gate 17360Sstevel@tonic-gate /* 17370Sstevel@tonic-gate * Returns: 17380Sstevel@tonic-gate * 0 - no update necessary 17390Sstevel@tonic-gate * 1 - update required. 17400Sstevel@tonic-gate * BAM_ERROR (-1) - An error occurred 17410Sstevel@tonic-gate * 17420Sstevel@tonic-gate * Special handling for check (-n): 17430Sstevel@tonic-gate * ================================ 17440Sstevel@tonic-gate * The check (-n) option produces parseable output. 17450Sstevel@tonic-gate * To do this, we suppress all stdout messages unrelated 17460Sstevel@tonic-gate * to out of sync files. 17470Sstevel@tonic-gate * All stderr messages are still printed though. 17480Sstevel@tonic-gate * 17490Sstevel@tonic-gate */ 17500Sstevel@tonic-gate static int 17510Sstevel@tonic-gate update_required(char *root) 17520Sstevel@tonic-gate { 17530Sstevel@tonic-gate struct stat sb; 17540Sstevel@tonic-gate char path[PATH_MAX]; 17550Sstevel@tonic-gate filelist_t flist; 17560Sstevel@tonic-gate filelist_t *flistp = &flist; 17570Sstevel@tonic-gate int need_update; 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 17600Sstevel@tonic-gate 17610Sstevel@tonic-gate walk_arg.need_update = 0; 17620Sstevel@tonic-gate 17630Sstevel@tonic-gate /* 17640Sstevel@tonic-gate * Without consulting stat data, check if we need update 17650Sstevel@tonic-gate */ 17660Sstevel@tonic-gate check_flags_and_files(root); 17670Sstevel@tonic-gate 17680Sstevel@tonic-gate /* 17690Sstevel@tonic-gate * In certain deployment scenarios, filestat may not 17700Sstevel@tonic-gate * exist. Ignore it during boot-archive SMF check. 17710Sstevel@tonic-gate */ 17720Sstevel@tonic-gate if (bam_smf_check) { 17730Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 17740Sstevel@tonic-gate if (stat(path, &sb) != 0) 17750Sstevel@tonic-gate return (0); 17760Sstevel@tonic-gate } 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate /* 17790Sstevel@tonic-gate * consult stat data only if we haven't made a decision 17800Sstevel@tonic-gate * about update. If checking (-n) however, we always 17810Sstevel@tonic-gate * need stat data (since we want to compare old and new) 17820Sstevel@tonic-gate */ 17830Sstevel@tonic-gate if (!walk_arg.need_update || bam_check) 17840Sstevel@tonic-gate getoldstat(root); 17850Sstevel@tonic-gate 17860Sstevel@tonic-gate /* 17872583Svikram * Check if the archive contains files that are no longer 17882583Svikram * present on the root filesystem. 17892583Svikram */ 17902583Svikram if (!walk_arg.need_update || bam_check) 17912583Svikram check4stale(root); 17922583Svikram 17932583Svikram /* 17940Sstevel@tonic-gate * read list of files 17950Sstevel@tonic-gate */ 17960Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 17970Sstevel@tonic-gate clear_walk_args(); 17980Sstevel@tonic-gate return (BAM_ERROR); 17990Sstevel@tonic-gate } 18000Sstevel@tonic-gate 18010Sstevel@tonic-gate assert(flistp->head && flistp->tail); 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate /* 18040Sstevel@tonic-gate * At this point either the update is required 18050Sstevel@tonic-gate * or the decision is pending. In either case 18060Sstevel@tonic-gate * we need to create new stat nvlist 18070Sstevel@tonic-gate */ 18080Sstevel@tonic-gate create_newstat(); 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate /* 18110Sstevel@tonic-gate * This walk does 2 things: 18120Sstevel@tonic-gate * - gets new stat data for every file 18130Sstevel@tonic-gate * - (optional) compare old and new stat data 18140Sstevel@tonic-gate */ 18150Sstevel@tonic-gate walk_list(root, &flist); 18160Sstevel@tonic-gate 18170Sstevel@tonic-gate /* done with the file list */ 18180Sstevel@tonic-gate filelist_free(flistp); 18190Sstevel@tonic-gate 18200Sstevel@tonic-gate /* 18210Sstevel@tonic-gate * if we didn't succeed in creating new stat data above 18220Sstevel@tonic-gate * just return result of update check so that archive is built. 18230Sstevel@tonic-gate */ 18240Sstevel@tonic-gate if (walk_arg.new_nvlp == NULL) { 18250Sstevel@tonic-gate bam_error(NO_NEW_STAT); 18260Sstevel@tonic-gate need_update = walk_arg.need_update; 18270Sstevel@tonic-gate clear_walk_args(); 18280Sstevel@tonic-gate return (need_update ? 1 : 0); 18290Sstevel@tonic-gate } 18300Sstevel@tonic-gate 18310Sstevel@tonic-gate 18320Sstevel@tonic-gate /* 18330Sstevel@tonic-gate * If no update required, discard newstat 18340Sstevel@tonic-gate */ 18350Sstevel@tonic-gate if (!walk_arg.need_update) { 18360Sstevel@tonic-gate clear_walk_args(); 18370Sstevel@tonic-gate return (0); 18380Sstevel@tonic-gate } 18390Sstevel@tonic-gate 18400Sstevel@tonic-gate /* 18410Sstevel@tonic-gate * At this point we need an update - so save new stat data 18420Sstevel@tonic-gate * However, if only checking (-n), don't save new stat data. 18430Sstevel@tonic-gate */ 18440Sstevel@tonic-gate if (!bam_check) 18450Sstevel@tonic-gate savenew(root); 18460Sstevel@tonic-gate 18470Sstevel@tonic-gate clear_walk_args(); 18480Sstevel@tonic-gate 18490Sstevel@tonic-gate return (1); 18500Sstevel@tonic-gate } 18510Sstevel@tonic-gate 18520Sstevel@tonic-gate static error_t 18530Sstevel@tonic-gate create_ramdisk(char *root) 18540Sstevel@tonic-gate { 18550Sstevel@tonic-gate char *cmdline, path[PATH_MAX]; 18560Sstevel@tonic-gate size_t len; 18570Sstevel@tonic-gate struct stat sb; 18580Sstevel@tonic-gate 18590Sstevel@tonic-gate /* 18600Sstevel@tonic-gate * Setup command args for create_ramdisk.ksh 18610Sstevel@tonic-gate */ 18620Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK); 18630Sstevel@tonic-gate if (stat(path, &sb) != 0) { 18640Sstevel@tonic-gate bam_error(ARCH_EXEC_MISS, path, strerror(errno)); 18650Sstevel@tonic-gate return (BAM_ERROR); 18660Sstevel@tonic-gate } 18670Sstevel@tonic-gate 18680Sstevel@tonic-gate len = strlen(path) + strlen(root) + 10; /* room for space + -R */ 18690Sstevel@tonic-gate cmdline = s_calloc(1, len); 18700Sstevel@tonic-gate 18710Sstevel@tonic-gate if (strlen(root) > 1) { 18720Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s -R %s", path, root); 18730Sstevel@tonic-gate /* chop off / at the end */ 18740Sstevel@tonic-gate cmdline[strlen(cmdline) - 1] = '\0'; 18750Sstevel@tonic-gate } else 18760Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s", path); 18770Sstevel@tonic-gate 18780Sstevel@tonic-gate if (exec_cmd(cmdline, NULL, 0) != 0) { 18790Sstevel@tonic-gate bam_error(ARCHIVE_FAIL, cmdline); 18800Sstevel@tonic-gate free(cmdline); 18810Sstevel@tonic-gate return (BAM_ERROR); 18820Sstevel@tonic-gate } 18830Sstevel@tonic-gate free(cmdline); 18840Sstevel@tonic-gate 18850Sstevel@tonic-gate /* 18860Sstevel@tonic-gate * Verify that the archive has been created 18870Sstevel@tonic-gate */ 18883446Smrj (void) snprintf(path, sizeof (path), "%s%s", root, 18893446Smrj DIRECT_BOOT_ARCHIVE_32); 18900Sstevel@tonic-gate if (stat(path, &sb) != 0) { 18910Sstevel@tonic-gate bam_error(ARCHIVE_NOT_CREATED, path); 18920Sstevel@tonic-gate return (BAM_ERROR); 18930Sstevel@tonic-gate } 18943446Smrj if (bam_direct == BAM_DIRECT_DBOOT) { 18953446Smrj (void) snprintf(path, sizeof (path), "%s%s", root, 18963446Smrj DIRECT_BOOT_ARCHIVE_64); 18973446Smrj if (stat(path, &sb) != 0) { 18983446Smrj bam_error(ARCHIVE_NOT_CREATED, path); 18993446Smrj return (BAM_ERROR); 19003446Smrj } 19013446Smrj } 19020Sstevel@tonic-gate 19030Sstevel@tonic-gate return (BAM_SUCCESS); 19040Sstevel@tonic-gate } 19050Sstevel@tonic-gate 19060Sstevel@tonic-gate /* 19070Sstevel@tonic-gate * Checks if target filesystem is on a ramdisk 19080Sstevel@tonic-gate * 1 - is miniroot 19090Sstevel@tonic-gate * 0 - is not 19100Sstevel@tonic-gate * When in doubt assume it is not a ramdisk. 19110Sstevel@tonic-gate */ 19120Sstevel@tonic-gate static int 19130Sstevel@tonic-gate is_ramdisk(char *root) 19140Sstevel@tonic-gate { 19150Sstevel@tonic-gate struct extmnttab mnt; 19160Sstevel@tonic-gate FILE *fp; 19170Sstevel@tonic-gate int found; 1918316Svikram char mntpt[PATH_MAX]; 1919316Svikram char *cp; 19200Sstevel@tonic-gate 19210Sstevel@tonic-gate /* 19220Sstevel@tonic-gate * There are 3 situations where creating archive is 19230Sstevel@tonic-gate * of dubious value: 1924316Svikram * - create boot_archive on a lofi-mounted boot_archive 19250Sstevel@tonic-gate * - create it on a ramdisk which is the root filesystem 19260Sstevel@tonic-gate * - create it on a ramdisk mounted somewhere else 19270Sstevel@tonic-gate * The first is not easy to detect and checking for it is not 19280Sstevel@tonic-gate * worth it. 19290Sstevel@tonic-gate * The other two conditions are handled here 19300Sstevel@tonic-gate */ 19310Sstevel@tonic-gate 19320Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 19330Sstevel@tonic-gate if (fp == NULL) { 19340Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 19350Sstevel@tonic-gate return (0); 19360Sstevel@tonic-gate } 19370Sstevel@tonic-gate 19380Sstevel@tonic-gate resetmnttab(fp); 19390Sstevel@tonic-gate 1940316Svikram /* 1941316Svikram * Remove any trailing / from the mount point 1942316Svikram */ 1943316Svikram (void) strlcpy(mntpt, root, sizeof (mntpt)); 1944316Svikram if (strcmp(root, "/") != 0) { 1945316Svikram cp = mntpt + strlen(mntpt) - 1; 1946316Svikram if (*cp == '/') 1947316Svikram *cp = '\0'; 1948316Svikram } 19490Sstevel@tonic-gate found = 0; 19500Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 1951316Svikram if (strcmp(mnt.mnt_mountp, mntpt) == 0) { 19520Sstevel@tonic-gate found = 1; 19530Sstevel@tonic-gate break; 19540Sstevel@tonic-gate } 19550Sstevel@tonic-gate } 19560Sstevel@tonic-gate 19570Sstevel@tonic-gate if (!found) { 19580Sstevel@tonic-gate if (bam_verbose) 1959316Svikram bam_error(NOT_IN_MNTTAB, mntpt); 19600Sstevel@tonic-gate (void) fclose(fp); 19610Sstevel@tonic-gate return (0); 19620Sstevel@tonic-gate } 19630Sstevel@tonic-gate 19640Sstevel@tonic-gate if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) { 19650Sstevel@tonic-gate if (bam_verbose) 19660Sstevel@tonic-gate bam_error(IS_RAMDISK, bam_root); 19670Sstevel@tonic-gate (void) fclose(fp); 19680Sstevel@tonic-gate return (1); 19690Sstevel@tonic-gate } 19700Sstevel@tonic-gate 19710Sstevel@tonic-gate (void) fclose(fp); 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate return (0); 19740Sstevel@tonic-gate } 19750Sstevel@tonic-gate 19760Sstevel@tonic-gate static int 19770Sstevel@tonic-gate is_newboot(char *root) 19780Sstevel@tonic-gate { 19790Sstevel@tonic-gate char path[PATH_MAX]; 19800Sstevel@tonic-gate struct stat sb; 19810Sstevel@tonic-gate 19820Sstevel@tonic-gate /* 19830Sstevel@tonic-gate * We can't boot without MULTI_BOOT 19840Sstevel@tonic-gate */ 19850Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT); 19860Sstevel@tonic-gate if (stat(path, &sb) == -1) { 19870Sstevel@tonic-gate if (bam_verbose) 19880Sstevel@tonic-gate bam_print(FILE_MISS, path); 19890Sstevel@tonic-gate return (0); 19900Sstevel@tonic-gate } 19910Sstevel@tonic-gate 19920Sstevel@tonic-gate /* 19930Sstevel@tonic-gate * We can't generate archive without GRUB_DIR 19940Sstevel@tonic-gate */ 19950Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR); 19960Sstevel@tonic-gate if (stat(path, &sb) == -1) { 19970Sstevel@tonic-gate if (bam_verbose) 19980Sstevel@tonic-gate bam_print(DIR_MISS, path); 19990Sstevel@tonic-gate return (0); 20000Sstevel@tonic-gate } 20010Sstevel@tonic-gate 20020Sstevel@tonic-gate return (1); 20030Sstevel@tonic-gate } 20040Sstevel@tonic-gate 20050Sstevel@tonic-gate static int 20060Sstevel@tonic-gate is_readonly(char *root) 20070Sstevel@tonic-gate { 20080Sstevel@tonic-gate struct statvfs vfs; 20090Sstevel@tonic-gate 20100Sstevel@tonic-gate /* 20110Sstevel@tonic-gate * Check for RDONLY filesystem 20120Sstevel@tonic-gate * When in doubt assume it is not readonly 20130Sstevel@tonic-gate */ 20140Sstevel@tonic-gate if (statvfs(root, &vfs) != 0) { 20150Sstevel@tonic-gate if (bam_verbose) 20160Sstevel@tonic-gate bam_error(STATVFS_FAIL, root, strerror(errno)); 20170Sstevel@tonic-gate return (0); 20180Sstevel@tonic-gate } 20190Sstevel@tonic-gate 20200Sstevel@tonic-gate if (vfs.f_flag & ST_RDONLY) { 20210Sstevel@tonic-gate return (1); 20220Sstevel@tonic-gate } 20230Sstevel@tonic-gate 20240Sstevel@tonic-gate return (0); 20250Sstevel@tonic-gate } 20260Sstevel@tonic-gate 20270Sstevel@tonic-gate static error_t 20280Sstevel@tonic-gate update_archive(char *root, char *opt) 20290Sstevel@tonic-gate { 20300Sstevel@tonic-gate error_t ret; 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate assert(root); 20330Sstevel@tonic-gate assert(opt == NULL); 20340Sstevel@tonic-gate 20350Sstevel@tonic-gate /* 2036316Svikram * root must belong to a GRUB boot OS, 20370Sstevel@tonic-gate * don't care on sparc except for diskless clients 20380Sstevel@tonic-gate */ 20390Sstevel@tonic-gate if (!is_newboot(root)) { 2040316Svikram /* 2041316Svikram * Emit message only if not in context of update_all. 2042316Svikram * If in update_all, emit only if verbose flag is set. 2043316Svikram */ 2044316Svikram if (!bam_update_all || bam_verbose) 2045316Svikram bam_print(NOT_GRUB_BOOT, root); 20460Sstevel@tonic-gate return (BAM_SUCCESS); 20470Sstevel@tonic-gate } 20480Sstevel@tonic-gate 20490Sstevel@tonic-gate /* 2050662Sszhou * If smf check is requested when / is writable (can happen 2051662Sszhou * on first reboot following an upgrade because service 2052662Sszhou * dependency is messed up), skip the check. 2053662Sszhou */ 2054662Sszhou if (bam_smf_check && !bam_root_readonly) 2055662Sszhou return (BAM_SUCCESS); 2056662Sszhou 2057662Sszhou /* 2058662Sszhou * root must be writable. This check applies to alternate 2059662Sszhou * root (-R option); bam_root_readonly applies to '/' only. 20600Sstevel@tonic-gate * Note: statvfs() does not always report the truth 20610Sstevel@tonic-gate */ 2062756Ssetje if (!bam_smf_check && !bam_check && is_readonly(root)) { 2063662Sszhou if (bam_verbose) 20640Sstevel@tonic-gate bam_print(RDONLY_FS, root); 20650Sstevel@tonic-gate return (BAM_SUCCESS); 20660Sstevel@tonic-gate } 20670Sstevel@tonic-gate 20680Sstevel@tonic-gate /* 20690Sstevel@tonic-gate * Don't generate archive on ramdisk 20700Sstevel@tonic-gate */ 20710Sstevel@tonic-gate if (is_ramdisk(root)) { 20720Sstevel@tonic-gate if (bam_verbose) 20730Sstevel@tonic-gate bam_print(SKIP_RAMDISK); 20740Sstevel@tonic-gate return (BAM_SUCCESS); 20750Sstevel@tonic-gate } 20760Sstevel@tonic-gate 20770Sstevel@tonic-gate /* 20780Sstevel@tonic-gate * Now check if updated is really needed 20790Sstevel@tonic-gate */ 20800Sstevel@tonic-gate ret = update_required(root); 20810Sstevel@tonic-gate 20820Sstevel@tonic-gate /* 20830Sstevel@tonic-gate * The check command (-n) is *not* a dry run 20840Sstevel@tonic-gate * It only checks if the archive is in sync. 20850Sstevel@tonic-gate */ 20860Sstevel@tonic-gate if (bam_check) { 20870Sstevel@tonic-gate bam_exit((ret != 0) ? 1 : 0); 20880Sstevel@tonic-gate } 20890Sstevel@tonic-gate 20900Sstevel@tonic-gate if (ret == 1) { 20910Sstevel@tonic-gate /* create the ramdisk */ 20920Sstevel@tonic-gate ret = create_ramdisk(root); 20930Sstevel@tonic-gate } 20940Sstevel@tonic-gate return (ret); 20950Sstevel@tonic-gate } 20960Sstevel@tonic-gate 2097316Svikram static void 20981746Svikram update_fdisk(void) 20991746Svikram { 21001746Svikram struct stat sb; 21011746Svikram char cmd[PATH_MAX]; 21021746Svikram int ret1, ret2; 21031746Svikram 21041746Svikram assert(stat(GRUB_fdisk, &sb) == 0); 21051746Svikram assert(stat(GRUB_fdisk_target, &sb) == 0); 21061746Svikram 21071746Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`", 21081746Svikram GRUB_fdisk, GRUB_fdisk_target); 21091746Svikram 21101746Svikram bam_print(UPDATING_FDISK); 21111746Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 21121746Svikram bam_error(FDISK_UPDATE_FAILED); 21131746Svikram } 21141746Svikram 21151746Svikram /* 21161746Svikram * We are done, remove the files. 21171746Svikram */ 21181746Svikram ret1 = unlink(GRUB_fdisk); 21191746Svikram ret2 = unlink(GRUB_fdisk_target); 21201746Svikram if (ret1 != 0 || ret2 != 0) { 21211746Svikram bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target); 21221746Svikram } 21231746Svikram } 21241746Svikram 21251746Svikram static void 2126316Svikram restore_grub_slice(void) 2127316Svikram { 2128316Svikram struct stat sb; 2129316Svikram char *mntpt, *physlice; 2130316Svikram int mnted; /* set if we did a mount */ 2131316Svikram char menupath[PATH_MAX], cmd[PATH_MAX]; 2132316Svikram 2133316Svikram if (stat(GRUB_slice, &sb) != 0) { 2134316Svikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 2135316Svikram return; 2136316Svikram } 2137316Svikram 2138316Svikram /* 2139316Svikram * If we are doing an luactivate, don't attempt to restore GRUB or else 2140316Svikram * we may not be able to get to DCA boot environments. Let luactivate 2141316Svikram * handle GRUB/DCA installation 2142316Svikram */ 2143316Svikram if (stat(LU_ACTIVATE_FILE, &sb) == 0) { 2144316Svikram return; 2145316Svikram } 2146316Svikram 2147316Svikram mnted = 0; 2148316Svikram physlice = NULL; 2149621Svikram mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL); 2150316Svikram if (mntpt == NULL) { 2151316Svikram bam_error(CANNOT_RESTORE_GRUB_SLICE); 2152316Svikram return; 2153316Svikram } 2154316Svikram 2155316Svikram (void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU); 2156316Svikram if (stat(menupath, &sb) == 0) { 2157621Svikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2158316Svikram return; 2159316Svikram } 2160316Svikram 2161316Svikram /* 2162316Svikram * The menu is missing - we need to do a restore 2163316Svikram */ 2164316Svikram bam_print(RESTORING_GRUB); 2165316Svikram 2166316Svikram (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s", 2167316Svikram INSTALLGRUB, STAGE1, STAGE2, physlice); 2168316Svikram 2169316Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 2170316Svikram bam_error(RESTORE_GRUB_FAILED); 2171621Svikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2172316Svikram return; 2173316Svikram } 2174316Svikram 2175316Svikram if (stat(GRUB_backup_menu, &sb) != 0) { 2176316Svikram bam_error(MISSING_BACKUP_MENU, 2177316Svikram GRUB_backup_menu, strerror(errno)); 2178621Svikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2179316Svikram return; 2180316Svikram } 2181316Svikram 2182316Svikram (void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s", 2183316Svikram GRUB_backup_menu, menupath); 2184316Svikram 2185316Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 2186316Svikram bam_error(RESTORE_MENU_FAILED, menupath); 2187621Svikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2188316Svikram return; 2189316Svikram } 2190316Svikram 2191316Svikram /* Success */ 2192621Svikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2193316Svikram } 2194316Svikram 21950Sstevel@tonic-gate static error_t 21960Sstevel@tonic-gate update_all(char *root, char *opt) 21970Sstevel@tonic-gate { 21980Sstevel@tonic-gate struct extmnttab mnt; 21990Sstevel@tonic-gate struct stat sb; 22000Sstevel@tonic-gate FILE *fp; 22010Sstevel@tonic-gate char multibt[PATH_MAX]; 22020Sstevel@tonic-gate error_t ret = BAM_SUCCESS; 22031746Svikram int ret1, ret2; 22040Sstevel@tonic-gate 2205621Svikram assert(root); 22060Sstevel@tonic-gate assert(opt == NULL); 22070Sstevel@tonic-gate 2208621Svikram if (bam_rootlen != 1 || *root != '/') { 2209621Svikram elide_trailing_slash(root, multibt, sizeof (multibt)); 2210621Svikram bam_error(ALT_ROOT_INVALID, multibt); 2211621Svikram return (BAM_ERROR); 2212621Svikram } 2213621Svikram 22140Sstevel@tonic-gate /* 22154493Snadkarni * Check to see if we are in the midst of safemode patching 22164493Snadkarni * If so skip building the archive for /. Instead build it 22174493Snadkarni * against the latest bits obtained by creating a fresh lofs 22184493Snadkarni * mount of root. 22190Sstevel@tonic-gate */ 22204493Snadkarni if (stat(LOFS_PATCH_FILE, &sb) == 0) { 22214493Snadkarni if (mkdir(LOFS_PATCH_MNT, 0755) == -1 && 22224493Snadkarni errno != EEXIST) { 22234493Snadkarni bam_error(MKDIR_FAILED, "%s", LOFS_PATCH_MNT, 22244493Snadkarni strerror(errno)); 22254493Snadkarni ret = BAM_ERROR; 22264493Snadkarni goto out; 22274493Snadkarni } 22284493Snadkarni (void) snprintf(multibt, sizeof (multibt), 22294493Snadkarni "/sbin/mount -F lofs -o nosub / %s", LOFS_PATCH_MNT); 22304493Snadkarni if (exec_cmd(multibt, NULL, 0) != 0) { 22314493Snadkarni bam_error(MOUNT_FAILED, LOFS_PATCH_MNT, "lofs"); 22324493Snadkarni ret = BAM_ERROR; 22334493Snadkarni } 22344493Snadkarni if (ret != BAM_ERROR) { 22354493Snadkarni (void) snprintf(rootbuf, sizeof (rootbuf), "%s/", 22364493Snadkarni LOFS_PATCH_MNT); 22374493Snadkarni bam_rootlen = strlen(rootbuf); 22384493Snadkarni if (update_archive(rootbuf, opt) != BAM_SUCCESS) 22394493Snadkarni ret = BAM_ERROR; 2240*4550Snadkarni /* 2241*4550Snadkarni * unmount the lofs mount since there could be 2242*4550Snadkarni * multiple invocations of bootadm -a update_all 2243*4550Snadkarni */ 2244*4550Snadkarni (void) snprintf(multibt, sizeof (multibt), 2245*4550Snadkarni "/sbin/umount %s", LOFS_PATCH_MNT); 2246*4550Snadkarni if (exec_cmd(multibt, NULL, 0) != 0) { 2247*4550Snadkarni bam_error(UMOUNT_FAILED, LOFS_PATCH_MNT); 2248*4550Snadkarni ret = BAM_ERROR; 2249*4550Snadkarni } 22504493Snadkarni } 22514493Snadkarni } else { 22524493Snadkarni /* 22534493Snadkarni * First update archive for current root 22544493Snadkarni */ 22554493Snadkarni if (update_archive(root, opt) != BAM_SUCCESS) 22564493Snadkarni ret = BAM_ERROR; 22574493Snadkarni } 22584493Snadkarni 22594493Snadkarni if (ret == BAM_ERROR) 22604493Snadkarni goto out; 22610Sstevel@tonic-gate 22620Sstevel@tonic-gate /* 22630Sstevel@tonic-gate * Now walk the mount table, performing archive update 22640Sstevel@tonic-gate * for all mounted Newboot root filesystems 22650Sstevel@tonic-gate */ 22660Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 22670Sstevel@tonic-gate if (fp == NULL) { 22680Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 2269316Svikram ret = BAM_ERROR; 2270316Svikram goto out; 22710Sstevel@tonic-gate } 22720Sstevel@tonic-gate 22730Sstevel@tonic-gate resetmnttab(fp); 22740Sstevel@tonic-gate 22750Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 22760Sstevel@tonic-gate if (mnt.mnt_special == NULL) 22770Sstevel@tonic-gate continue; 22780Sstevel@tonic-gate if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0) 22790Sstevel@tonic-gate continue; 22800Sstevel@tonic-gate if (strcmp(mnt.mnt_mountp, "/") == 0) 22810Sstevel@tonic-gate continue; 22820Sstevel@tonic-gate 22830Sstevel@tonic-gate (void) snprintf(multibt, sizeof (multibt), "%s%s", 22840Sstevel@tonic-gate mnt.mnt_mountp, MULTI_BOOT); 22850Sstevel@tonic-gate 22860Sstevel@tonic-gate if (stat(multibt, &sb) == -1) 22870Sstevel@tonic-gate continue; 22880Sstevel@tonic-gate 22890Sstevel@tonic-gate /* 22900Sstevel@tonic-gate * We put a trailing slash to be consistent with root = "/" 22910Sstevel@tonic-gate * case, such that we don't have to print // in some cases. 22920Sstevel@tonic-gate */ 22930Sstevel@tonic-gate (void) snprintf(rootbuf, sizeof (rootbuf), "%s/", 22940Sstevel@tonic-gate mnt.mnt_mountp); 22950Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 22963446Smrj 22973446Smrj /* 22983446Smrj * It's possible that other mounts may be an alternate boot 22993446Smrj * architecture, so check it again. 23003446Smrj */ 23013446Smrj if ((dboot_or_multiboot(rootbuf) != BAM_SUCCESS) || 23023446Smrj (update_archive(rootbuf, opt) != BAM_SUCCESS)) 23030Sstevel@tonic-gate ret = BAM_ERROR; 23040Sstevel@tonic-gate } 23050Sstevel@tonic-gate 23060Sstevel@tonic-gate (void) fclose(fp); 23070Sstevel@tonic-gate 2308316Svikram out: 2309316Svikram if (stat(GRUB_slice, &sb) == 0) { 2310316Svikram restore_grub_slice(); 2311316Svikram } 2312316Svikram 23131746Svikram /* 23141746Svikram * Update fdisk table as we go down. Updating it when 23151746Svikram * the system is running will confuse biosdev. 23161746Svikram */ 23171746Svikram ret1 = stat(GRUB_fdisk, &sb); 23181746Svikram ret2 = stat(GRUB_fdisk_target, &sb); 23191746Svikram if ((ret1 == 0) && (ret2 == 0)) { 23201746Svikram update_fdisk(); 23211746Svikram } else if ((ret1 == 0) ^ (ret2 == 0)) { 23221746Svikram /* 23231746Svikram * It is an error for one file to be 23241746Svikram * present and the other absent. 23251746Svikram * It is normal for both files to be 23261746Svikram * absent - it indicates that no fdisk 23271746Svikram * update is required. 23281746Svikram */ 23291746Svikram bam_error(MISSING_FDISK_FILE, 23301746Svikram ret1 ? GRUB_fdisk : GRUB_fdisk_target); 23311746Svikram ret = BAM_ERROR; 23321746Svikram } 23331746Svikram 23340Sstevel@tonic-gate return (ret); 23350Sstevel@tonic-gate } 23360Sstevel@tonic-gate 23370Sstevel@tonic-gate static void 23380Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp) 23390Sstevel@tonic-gate { 23400Sstevel@tonic-gate if (mp->start == NULL) { 23410Sstevel@tonic-gate mp->start = lp; 23420Sstevel@tonic-gate } else { 23430Sstevel@tonic-gate mp->end->next = lp; 2344662Sszhou lp->prev = mp->end; 23450Sstevel@tonic-gate } 23460Sstevel@tonic-gate mp->end = lp; 23470Sstevel@tonic-gate } 23480Sstevel@tonic-gate 2349662Sszhou static void 2350662Sszhou unlink_line(menu_t *mp, line_t *lp) 2351662Sszhou { 2352662Sszhou /* unlink from list */ 2353662Sszhou if (lp->prev) 2354662Sszhou lp->prev->next = lp->next; 2355662Sszhou else 2356662Sszhou mp->start = lp->next; 2357662Sszhou if (lp->next) 2358662Sszhou lp->next->prev = lp->prev; 2359662Sszhou else 2360662Sszhou mp->end = lp->prev; 2361662Sszhou } 2362662Sszhou 2363662Sszhou static entry_t * 2364662Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end) 2365662Sszhou { 2366662Sszhou entry_t *ent, *prev; 2367662Sszhou 2368662Sszhou ent = s_calloc(1, sizeof (entry_t)); 2369662Sszhou ent->start = start; 2370662Sszhou ent->end = end; 2371662Sszhou 2372662Sszhou if (mp->entries == NULL) { 2373662Sszhou mp->entries = ent; 2374662Sszhou return (ent); 2375662Sszhou } 2376662Sszhou 2377662Sszhou prev = mp->entries; 2378662Sszhou while (prev->next) 2379662Sszhou prev = prev-> next; 2380662Sszhou prev->next = ent; 2381662Sszhou ent->prev = prev; 2382662Sszhou return (ent); 2383662Sszhou } 2384662Sszhou 2385662Sszhou static void 2386662Sszhou boot_entry_addline(entry_t *ent, line_t *lp) 2387662Sszhou { 2388662Sszhou if (ent) 2389662Sszhou ent->end = lp; 2390662Sszhou } 2391662Sszhou 23920Sstevel@tonic-gate /* 23930Sstevel@tonic-gate * A line in menu.lst looks like 23940Sstevel@tonic-gate * [ ]*<cmd>[ \t=]*<arg>* 23950Sstevel@tonic-gate */ 23960Sstevel@tonic-gate static void 23970Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum) 23980Sstevel@tonic-gate { 23990Sstevel@tonic-gate /* 24000Sstevel@tonic-gate * save state across calls. This is so that 24010Sstevel@tonic-gate * header gets the right entry# after title has 24020Sstevel@tonic-gate * been processed 24030Sstevel@tonic-gate */ 2404662Sszhou static line_t *prev = NULL; 2405662Sszhou static entry_t *curr_ent = NULL; 24063446Smrj static int in_liveupgrade = 0; 24070Sstevel@tonic-gate 24080Sstevel@tonic-gate line_t *lp; 24090Sstevel@tonic-gate char *cmd, *sep, *arg; 24100Sstevel@tonic-gate char save, *cp, *line; 24110Sstevel@tonic-gate menu_flag_t flag = BAM_INVALID; 24120Sstevel@tonic-gate 24130Sstevel@tonic-gate if (str == NULL) { 24140Sstevel@tonic-gate return; 24150Sstevel@tonic-gate } 24160Sstevel@tonic-gate 24170Sstevel@tonic-gate /* 24180Sstevel@tonic-gate * First save a copy of the entire line. 24190Sstevel@tonic-gate * We use this later to set the line field. 24200Sstevel@tonic-gate */ 24210Sstevel@tonic-gate line = s_strdup(str); 24220Sstevel@tonic-gate 24230Sstevel@tonic-gate /* Eat up leading whitespace */ 24240Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 24250Sstevel@tonic-gate str++; 24260Sstevel@tonic-gate 24270Sstevel@tonic-gate if (*str == '#') { /* comment */ 24280Sstevel@tonic-gate cmd = s_strdup("#"); 24290Sstevel@tonic-gate sep = NULL; 24300Sstevel@tonic-gate arg = s_strdup(str + 1); 24310Sstevel@tonic-gate flag = BAM_COMMENT; 24323446Smrj if (strstr(arg, BAM_LU_HDR) != NULL) { 24333446Smrj in_liveupgrade = 1; 24343446Smrj } else if (strstr(arg, BAM_LU_FTR) != NULL) { 24353446Smrj in_liveupgrade = 0; 24363446Smrj } 24370Sstevel@tonic-gate } else if (*str == '\0') { /* blank line */ 24380Sstevel@tonic-gate cmd = sep = arg = NULL; 24390Sstevel@tonic-gate flag = BAM_EMPTY; 24400Sstevel@tonic-gate } else { 24410Sstevel@tonic-gate /* 24420Sstevel@tonic-gate * '=' is not a documented separator in grub syntax. 24430Sstevel@tonic-gate * However various development bits use '=' as a 24440Sstevel@tonic-gate * separator. In addition, external users also 24450Sstevel@tonic-gate * use = as a separator. So we will allow that usage. 24460Sstevel@tonic-gate */ 24470Sstevel@tonic-gate cp = str; 24480Sstevel@tonic-gate while (*str != ' ' && *str != '\t' && *str != '=') { 24490Sstevel@tonic-gate if (*str == '\0') { 24500Sstevel@tonic-gate cmd = s_strdup(cp); 24510Sstevel@tonic-gate sep = arg = NULL; 24520Sstevel@tonic-gate break; 24530Sstevel@tonic-gate } 24540Sstevel@tonic-gate str++; 24550Sstevel@tonic-gate } 24560Sstevel@tonic-gate 24570Sstevel@tonic-gate if (*str != '\0') { 24580Sstevel@tonic-gate save = *str; 24590Sstevel@tonic-gate *str = '\0'; 24600Sstevel@tonic-gate cmd = s_strdup(cp); 24610Sstevel@tonic-gate *str = save; 24620Sstevel@tonic-gate 24630Sstevel@tonic-gate str++; 24640Sstevel@tonic-gate save = *str; 24650Sstevel@tonic-gate *str = '\0'; 24660Sstevel@tonic-gate sep = s_strdup(str - 1); 24670Sstevel@tonic-gate *str = save; 24680Sstevel@tonic-gate 24690Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 24700Sstevel@tonic-gate str++; 24710Sstevel@tonic-gate if (*str == '\0') 24720Sstevel@tonic-gate arg = NULL; 24730Sstevel@tonic-gate else 24740Sstevel@tonic-gate arg = s_strdup(str); 24750Sstevel@tonic-gate } 24760Sstevel@tonic-gate } 24770Sstevel@tonic-gate 24780Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 24790Sstevel@tonic-gate 24800Sstevel@tonic-gate lp->cmd = cmd; 24810Sstevel@tonic-gate lp->sep = sep; 24820Sstevel@tonic-gate lp->arg = arg; 24830Sstevel@tonic-gate lp->line = line; 24840Sstevel@tonic-gate lp->lineNum = ++(*lineNum); 24850Sstevel@tonic-gate if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) { 24860Sstevel@tonic-gate lp->entryNum = ++(*entryNum); 24870Sstevel@tonic-gate lp->flags = BAM_TITLE; 24880Sstevel@tonic-gate if (prev && prev->flags == BAM_COMMENT && 24893446Smrj prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) { 24900Sstevel@tonic-gate prev->entryNum = lp->entryNum; 2491662Sszhou curr_ent = boot_entry_new(mp, prev, lp); 24923446Smrj curr_ent->flags = BAM_ENTRY_BOOTADM; 2493662Sszhou } else { 2494662Sszhou curr_ent = boot_entry_new(mp, lp, lp); 24953446Smrj if (in_liveupgrade) { 24963446Smrj curr_ent->flags = BAM_ENTRY_LU; 24973446Smrj } 2498662Sszhou } 24993446Smrj curr_ent->entryNum = *entryNum; 25000Sstevel@tonic-gate } else if (flag != BAM_INVALID) { 25010Sstevel@tonic-gate /* 25020Sstevel@tonic-gate * For header comments, the entry# is "fixed up" 25030Sstevel@tonic-gate * by the subsequent title 25040Sstevel@tonic-gate */ 25050Sstevel@tonic-gate lp->entryNum = *entryNum; 25060Sstevel@tonic-gate lp->flags = flag; 25070Sstevel@tonic-gate } else { 25080Sstevel@tonic-gate lp->entryNum = *entryNum; 25093446Smrj 25103446Smrj if (*entryNum == ENTRY_INIT) { 25113446Smrj lp->flags = BAM_GLOBAL; 25123446Smrj } else { 25133446Smrj lp->flags = BAM_ENTRY; 25143446Smrj 25153446Smrj if (cmd && arg) { 25163446Smrj /* 25173446Smrj * We only compare for the length of "module" 25183446Smrj * so that "module$" will also match. 25193446Smrj */ 25203446Smrj if ((strncmp(cmd, menu_cmds[MODULE_CMD], 25213446Smrj strlen(menu_cmds[MODULE_CMD])) == 0) && 25223446Smrj (strcmp(arg, MINIROOT) == 0)) 25233446Smrj curr_ent->flags |= BAM_ENTRY_MINIROOT; 25243446Smrj else if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0) 25253446Smrj curr_ent->flags |= BAM_ENTRY_ROOT; 25263446Smrj else if (strcmp(cmd, 25273446Smrj menu_cmds[CHAINLOADER_CMD]) == 0) 25283446Smrj curr_ent->flags |= 25293446Smrj BAM_ENTRY_CHAINLOADER; 25303446Smrj } 25313446Smrj } 25320Sstevel@tonic-gate } 25330Sstevel@tonic-gate 2534662Sszhou /* record default, old default, and entry line ranges */ 2535662Sszhou if (lp->flags == BAM_GLOBAL && 2536662Sszhou strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) { 2537662Sszhou mp->curdefault = lp; 2538662Sszhou } else if (lp->flags == BAM_COMMENT && 2539662Sszhou strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) { 2540662Sszhou mp->olddefault = lp; 25413446Smrj } else if (lp->flags == BAM_COMMENT && 25423446Smrj strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) { 25433446Smrj mp->old_rc_default = lp; 2544662Sszhou } else if (lp->flags == BAM_ENTRY || 25453446Smrj (lp->flags == BAM_COMMENT && 25463446Smrj strcmp(lp->arg, BAM_BOOTADM_FTR) == 0)) { 2547662Sszhou boot_entry_addline(curr_ent, lp); 2548662Sszhou } 25490Sstevel@tonic-gate append_line(mp, lp); 25500Sstevel@tonic-gate 25510Sstevel@tonic-gate prev = lp; 25520Sstevel@tonic-gate } 25530Sstevel@tonic-gate 2554621Svikram static void 2555621Svikram update_numbering(menu_t *mp) 2556621Svikram { 2557621Svikram int lineNum; 2558621Svikram int entryNum; 2559621Svikram int old_default_value; 2560621Svikram line_t *lp, *prev, *default_lp, *default_entry; 2561621Svikram char buf[PATH_MAX]; 2562621Svikram 2563621Svikram if (mp->start == NULL) { 2564621Svikram return; 2565621Svikram } 2566621Svikram 2567621Svikram lineNum = LINE_INIT; 2568621Svikram entryNum = ENTRY_INIT; 2569621Svikram old_default_value = ENTRY_INIT; 2570621Svikram lp = default_lp = default_entry = NULL; 2571621Svikram 2572621Svikram prev = NULL; 2573621Svikram for (lp = mp->start; lp; prev = lp, lp = lp->next) { 2574621Svikram lp->lineNum = ++lineNum; 2575621Svikram 2576621Svikram /* 2577621Svikram * Get the value of the default command 2578621Svikram */ 2579621Svikram if (lp->entryNum == ENTRY_INIT && lp->cmd && 2580621Svikram strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 && 2581621Svikram lp->arg) { 2582621Svikram old_default_value = atoi(lp->arg); 2583621Svikram default_lp = lp; 2584621Svikram } 2585621Svikram 2586621Svikram /* 2587621Svikram * If not boot entry, nothing else to fix for this 2588621Svikram * entry 2589621Svikram */ 2590621Svikram if (lp->entryNum == ENTRY_INIT) 2591621Svikram continue; 2592621Svikram 2593621Svikram /* 2594621Svikram * Record the position of the default entry. 2595621Svikram * The following works because global 2596621Svikram * commands like default and timeout should precede 2597621Svikram * actual boot entries, so old_default_value 2598621Svikram * is already known (or default cmd is missing). 2599621Svikram */ 2600621Svikram if (default_entry == NULL && 2601621Svikram old_default_value != ENTRY_INIT && 2602621Svikram lp->entryNum == old_default_value) { 2603621Svikram default_entry = lp; 2604621Svikram } 2605621Svikram 2606621Svikram /* 2607621Svikram * Now fixup the entry number 2608621Svikram */ 2609621Svikram if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) { 2610621Svikram lp->entryNum = ++entryNum; 2611621Svikram /* fixup the bootadm header */ 2612621Svikram if (prev && prev->flags == BAM_COMMENT && 26133446Smrj prev->arg && 26143446Smrj strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) { 2615621Svikram prev->entryNum = lp->entryNum; 2616621Svikram } 2617621Svikram } else { 2618621Svikram lp->entryNum = entryNum; 2619621Svikram } 2620621Svikram } 2621621Svikram 2622621Svikram /* 2623621Svikram * No default command in menu, simply return 2624621Svikram */ 2625621Svikram if (default_lp == NULL) { 2626621Svikram return; 2627621Svikram } 2628621Svikram 2629621Svikram free(default_lp->arg); 2630621Svikram free(default_lp->line); 2631621Svikram 2632621Svikram if (default_entry == NULL) { 2633621Svikram default_lp->arg = s_strdup("0"); 2634621Svikram } else { 2635621Svikram (void) snprintf(buf, sizeof (buf), "%d", 2636621Svikram default_entry->entryNum); 2637621Svikram default_lp->arg = s_strdup(buf); 2638621Svikram } 2639621Svikram 2640621Svikram /* 2641621Svikram * The following is required since only the line field gets 2642621Svikram * written back to menu.lst 2643621Svikram */ 2644621Svikram (void) snprintf(buf, sizeof (buf), "%s%s%s", 2645621Svikram menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg); 2646621Svikram default_lp->line = s_strdup(buf); 2647621Svikram } 2648621Svikram 2649621Svikram 26500Sstevel@tonic-gate static menu_t * 26510Sstevel@tonic-gate menu_read(char *menu_path) 26520Sstevel@tonic-gate { 26530Sstevel@tonic-gate FILE *fp; 26540Sstevel@tonic-gate char buf[BAM_MAXLINE], *cp; 26550Sstevel@tonic-gate menu_t *mp; 26560Sstevel@tonic-gate int line, entry, len, n; 26570Sstevel@tonic-gate 26580Sstevel@tonic-gate mp = s_calloc(1, sizeof (menu_t)); 26590Sstevel@tonic-gate 26600Sstevel@tonic-gate fp = fopen(menu_path, "r"); 26610Sstevel@tonic-gate if (fp == NULL) { /* Let the caller handle this error */ 26620Sstevel@tonic-gate return (mp); 26630Sstevel@tonic-gate } 26640Sstevel@tonic-gate 26650Sstevel@tonic-gate 26660Sstevel@tonic-gate /* Note: GRUB boot entry number starts with 0 */ 26670Sstevel@tonic-gate line = LINE_INIT; 26680Sstevel@tonic-gate entry = ENTRY_INIT; 26690Sstevel@tonic-gate cp = buf; 26700Sstevel@tonic-gate len = sizeof (buf); 26710Sstevel@tonic-gate while (s_fgets(cp, len, fp) != NULL) { 26720Sstevel@tonic-gate n = strlen(cp); 26730Sstevel@tonic-gate if (cp[n - 1] == '\\') { 26740Sstevel@tonic-gate len -= n - 1; 26750Sstevel@tonic-gate assert(len >= 2); 26760Sstevel@tonic-gate cp += n - 1; 26770Sstevel@tonic-gate continue; 26780Sstevel@tonic-gate } 26790Sstevel@tonic-gate line_parser(mp, buf, &line, &entry); 26800Sstevel@tonic-gate cp = buf; 26810Sstevel@tonic-gate len = sizeof (buf); 26820Sstevel@tonic-gate } 26830Sstevel@tonic-gate 26840Sstevel@tonic-gate if (fclose(fp) == EOF) { 26850Sstevel@tonic-gate bam_error(CLOSE_FAIL, menu_path, strerror(errno)); 26860Sstevel@tonic-gate } 26870Sstevel@tonic-gate 26880Sstevel@tonic-gate return (mp); 26890Sstevel@tonic-gate } 26900Sstevel@tonic-gate 26910Sstevel@tonic-gate static error_t 26920Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title) 26930Sstevel@tonic-gate { 26940Sstevel@tonic-gate char *eq; 26950Sstevel@tonic-gate char *opt_dup; 26960Sstevel@tonic-gate int entryNum; 26970Sstevel@tonic-gate 26980Sstevel@tonic-gate assert(mp); 26990Sstevel@tonic-gate assert(mp->start); 27000Sstevel@tonic-gate assert(opt); 27010Sstevel@tonic-gate 27020Sstevel@tonic-gate opt_dup = s_strdup(opt); 27030Sstevel@tonic-gate 27040Sstevel@tonic-gate if (entry) 27050Sstevel@tonic-gate *entry = ENTRY_INIT; 27060Sstevel@tonic-gate if (title) 27070Sstevel@tonic-gate *title = NULL; 27080Sstevel@tonic-gate 27090Sstevel@tonic-gate eq = strchr(opt_dup, '='); 27100Sstevel@tonic-gate if (eq == NULL) { 27110Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 27120Sstevel@tonic-gate free(opt_dup); 27130Sstevel@tonic-gate return (BAM_ERROR); 27140Sstevel@tonic-gate } 27150Sstevel@tonic-gate 27160Sstevel@tonic-gate *eq = '\0'; 27170Sstevel@tonic-gate if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) { 27180Sstevel@tonic-gate assert(mp->end); 27190Sstevel@tonic-gate entryNum = s_strtol(eq + 1); 27200Sstevel@tonic-gate if (entryNum < 0 || entryNum > mp->end->entryNum) { 27210Sstevel@tonic-gate bam_error(INVALID_ENTRY, eq + 1); 27220Sstevel@tonic-gate free(opt_dup); 27230Sstevel@tonic-gate return (BAM_ERROR); 27240Sstevel@tonic-gate } 27250Sstevel@tonic-gate *entry = entryNum; 27260Sstevel@tonic-gate } else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) { 27270Sstevel@tonic-gate *title = opt + (eq - opt_dup) + 1; 27280Sstevel@tonic-gate } else { 27290Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 27300Sstevel@tonic-gate free(opt_dup); 27310Sstevel@tonic-gate return (BAM_ERROR); 27320Sstevel@tonic-gate } 27330Sstevel@tonic-gate 27340Sstevel@tonic-gate free(opt_dup); 27350Sstevel@tonic-gate return (BAM_SUCCESS); 27360Sstevel@tonic-gate } 27370Sstevel@tonic-gate 27380Sstevel@tonic-gate /* 27390Sstevel@tonic-gate * If invoked with no titles/entries (opt == NULL) 27400Sstevel@tonic-gate * only title lines in file are printed. 27410Sstevel@tonic-gate * 27420Sstevel@tonic-gate * If invoked with a title or entry #, all 27430Sstevel@tonic-gate * lines in *every* matching entry are listed 27440Sstevel@tonic-gate */ 27450Sstevel@tonic-gate static error_t 27460Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt) 27470Sstevel@tonic-gate { 27480Sstevel@tonic-gate line_t *lp; 27490Sstevel@tonic-gate int entry = ENTRY_INIT; 27500Sstevel@tonic-gate int found; 27510Sstevel@tonic-gate char *title = NULL; 27520Sstevel@tonic-gate 27530Sstevel@tonic-gate assert(mp); 27540Sstevel@tonic-gate assert(menu_path); 27550Sstevel@tonic-gate 27560Sstevel@tonic-gate if (mp->start == NULL) { 27570Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 27580Sstevel@tonic-gate return (BAM_ERROR); 27590Sstevel@tonic-gate } 27600Sstevel@tonic-gate 27610Sstevel@tonic-gate if (opt != NULL) { 27620Sstevel@tonic-gate if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) { 27630Sstevel@tonic-gate return (BAM_ERROR); 27640Sstevel@tonic-gate } 27650Sstevel@tonic-gate assert((entry != ENTRY_INIT) ^ (title != NULL)); 27660Sstevel@tonic-gate } else { 27670Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0); 27680Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0); 27690Sstevel@tonic-gate } 27700Sstevel@tonic-gate 27710Sstevel@tonic-gate found = 0; 27720Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 27730Sstevel@tonic-gate if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY) 27740Sstevel@tonic-gate continue; 27750Sstevel@tonic-gate if (opt == NULL && lp->flags == BAM_TITLE) { 27760Sstevel@tonic-gate bam_print(PRINT_TITLE, lp->entryNum, 27770Sstevel@tonic-gate lp->arg); 27780Sstevel@tonic-gate found = 1; 27790Sstevel@tonic-gate continue; 27800Sstevel@tonic-gate } 27810Sstevel@tonic-gate if (entry != ENTRY_INIT && lp->entryNum == entry) { 27820Sstevel@tonic-gate bam_print(PRINT, lp->line); 27830Sstevel@tonic-gate found = 1; 27840Sstevel@tonic-gate continue; 27850Sstevel@tonic-gate } 27860Sstevel@tonic-gate 27870Sstevel@tonic-gate /* 27880Sstevel@tonic-gate * We set the entry value here so that all lines 27890Sstevel@tonic-gate * in entry get printed. If we subsequently match 27900Sstevel@tonic-gate * title in other entries, all lines in those 27910Sstevel@tonic-gate * entries get printed as well. 27920Sstevel@tonic-gate */ 27930Sstevel@tonic-gate if (title && lp->flags == BAM_TITLE && lp->arg && 27940Sstevel@tonic-gate strncmp(title, lp->arg, strlen(title)) == 0) { 27950Sstevel@tonic-gate bam_print(PRINT, lp->line); 27960Sstevel@tonic-gate entry = lp->entryNum; 27970Sstevel@tonic-gate found = 1; 27980Sstevel@tonic-gate continue; 27990Sstevel@tonic-gate } 28000Sstevel@tonic-gate } 28010Sstevel@tonic-gate 28020Sstevel@tonic-gate if (!found) { 28030Sstevel@tonic-gate bam_error(NO_MATCH_ENTRY); 28040Sstevel@tonic-gate return (BAM_ERROR); 28050Sstevel@tonic-gate } 28060Sstevel@tonic-gate 28070Sstevel@tonic-gate return (BAM_SUCCESS); 28080Sstevel@tonic-gate } 28090Sstevel@tonic-gate 28100Sstevel@tonic-gate static int 28110Sstevel@tonic-gate add_boot_entry(menu_t *mp, 28120Sstevel@tonic-gate char *title, 28130Sstevel@tonic-gate char *root, 28140Sstevel@tonic-gate char *kernel, 28150Sstevel@tonic-gate char *module) 28160Sstevel@tonic-gate { 28170Sstevel@tonic-gate int lineNum, entryNum; 28180Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 28193446Smrj menu_cmd_t k_cmd, m_cmd; 28200Sstevel@tonic-gate 28210Sstevel@tonic-gate assert(mp); 28220Sstevel@tonic-gate 28230Sstevel@tonic-gate if (title == NULL) { 2824656Sszhou title = "Solaris"; /* default to Solaris */ 28250Sstevel@tonic-gate } 28260Sstevel@tonic-gate if (kernel == NULL) { 28270Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]); 28280Sstevel@tonic-gate return (BAM_ERROR); 28290Sstevel@tonic-gate } 28300Sstevel@tonic-gate if (module == NULL) { 28313446Smrj if (bam_direct != BAM_DIRECT_DBOOT) { 28323446Smrj bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]); 28333446Smrj return (BAM_ERROR); 28343446Smrj } 28353446Smrj 28363446Smrj /* Figure the commands out from the kernel line */ 28373446Smrj if (strstr(kernel, "$ISADIR") != NULL) { 28383446Smrj module = DIRECT_BOOT_ARCHIVE; 28393446Smrj k_cmd = KERNEL_DOLLAR_CMD; 28403446Smrj m_cmd = MODULE_DOLLAR_CMD; 28413446Smrj } else if (strstr(kernel, "amd64") != NULL) { 28423446Smrj module = DIRECT_BOOT_ARCHIVE_64; 28433446Smrj k_cmd = KERNEL_CMD; 28443446Smrj m_cmd = MODULE_CMD; 28453446Smrj } else { 28463446Smrj module = DIRECT_BOOT_ARCHIVE_32; 28473446Smrj k_cmd = KERNEL_CMD; 28483446Smrj m_cmd = MODULE_CMD; 28493446Smrj } 28503446Smrj } else if ((bam_direct == BAM_DIRECT_DBOOT) && 28513446Smrj (strstr(kernel, "$ISADIR") != NULL)) { 28523446Smrj /* 28533446Smrj * If it's a non-failsafe dboot kernel, use the "kernel$" 28543446Smrj * command. Otherwise, use "kernel". 28553446Smrj */ 28563446Smrj k_cmd = KERNEL_DOLLAR_CMD; 28573446Smrj m_cmd = MODULE_DOLLAR_CMD; 28583446Smrj } else { 28593446Smrj k_cmd = KERNEL_CMD; 28603446Smrj m_cmd = MODULE_CMD; 28610Sstevel@tonic-gate } 28620Sstevel@tonic-gate 28630Sstevel@tonic-gate if (mp->start) { 28640Sstevel@tonic-gate lineNum = mp->end->lineNum; 28650Sstevel@tonic-gate entryNum = mp->end->entryNum; 28660Sstevel@tonic-gate } else { 28670Sstevel@tonic-gate lineNum = LINE_INIT; 28680Sstevel@tonic-gate entryNum = ENTRY_INIT; 28690Sstevel@tonic-gate } 28700Sstevel@tonic-gate 28710Sstevel@tonic-gate /* 28720Sstevel@tonic-gate * No separator for comment (HDR/FTR) commands 28730Sstevel@tonic-gate * The syntax for comments is #<comment> 28740Sstevel@tonic-gate */ 28750Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 28763446Smrj menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR); 2877662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 28780Sstevel@tonic-gate 28790Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 28800Sstevel@tonic-gate menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 2881662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 2882662Sszhou 2883662Sszhou if (root) { 2884662Sszhou (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 2885662Sszhou menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root); 2886662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 28870Sstevel@tonic-gate } 28880Sstevel@tonic-gate 28890Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 28903446Smrj menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel); 2891662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 28920Sstevel@tonic-gate 28930Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 28943446Smrj menu_cmds[m_cmd], menu_cmds[SEP_CMD], module); 2895662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 28960Sstevel@tonic-gate 28970Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 28983446Smrj menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR); 2899662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 29000Sstevel@tonic-gate 29010Sstevel@tonic-gate return (entryNum); 29020Sstevel@tonic-gate } 29030Sstevel@tonic-gate 29040Sstevel@tonic-gate static error_t 29050Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum) 29060Sstevel@tonic-gate { 2907662Sszhou line_t *lp, *freed; 2908662Sszhou entry_t *ent, *tmp; 29090Sstevel@tonic-gate int deleted; 29100Sstevel@tonic-gate 29110Sstevel@tonic-gate assert(entryNum != ENTRY_INIT); 29120Sstevel@tonic-gate 2913662Sszhou ent = mp->entries; 2914662Sszhou while (ent) { 2915662Sszhou lp = ent->start; 2916662Sszhou /* check entry number and make sure it's a bootadm entry */ 2917662Sszhou if (lp->flags != BAM_COMMENT || 29183446Smrj strcmp(lp->arg, BAM_BOOTADM_HDR) != 0 || 2919662Sszhou (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) { 2920662Sszhou ent = ent->next; 29210Sstevel@tonic-gate continue; 29220Sstevel@tonic-gate } 29230Sstevel@tonic-gate 2924662Sszhou /* free the entry content */ 2925662Sszhou do { 2926662Sszhou freed = lp; 2927662Sszhou lp = lp->next; /* prev stays the same */ 2928662Sszhou unlink_line(mp, freed); 2929662Sszhou line_free(freed); 2930662Sszhou } while (freed != ent->end); 2931662Sszhou 2932662Sszhou /* free the entry_t structure */ 2933662Sszhou tmp = ent; 2934662Sszhou ent = ent->next; 2935662Sszhou if (tmp->prev) 2936662Sszhou tmp->prev->next = ent; 29370Sstevel@tonic-gate else 2938662Sszhou mp->entries = ent; 2939662Sszhou if (ent) 2940662Sszhou ent->prev = tmp->prev; 29410Sstevel@tonic-gate deleted = 1; 29420Sstevel@tonic-gate } 29430Sstevel@tonic-gate 29440Sstevel@tonic-gate if (!deleted && entryNum != ALL_ENTRIES) { 29450Sstevel@tonic-gate bam_error(NO_BOOTADM_MATCH); 29460Sstevel@tonic-gate return (BAM_ERROR); 29470Sstevel@tonic-gate } 29480Sstevel@tonic-gate 2949621Svikram /* 2950621Svikram * Now that we have deleted an entry, update 2951621Svikram * the entry numbering and the default cmd. 2952621Svikram */ 2953621Svikram update_numbering(mp); 2954621Svikram 29550Sstevel@tonic-gate return (BAM_SUCCESS); 29560Sstevel@tonic-gate } 29570Sstevel@tonic-gate 29580Sstevel@tonic-gate static error_t 29590Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt) 29600Sstevel@tonic-gate { 29610Sstevel@tonic-gate assert(mp); 29620Sstevel@tonic-gate assert(opt == NULL); 29630Sstevel@tonic-gate 29640Sstevel@tonic-gate if (mp->start == NULL) { 29650Sstevel@tonic-gate bam_print(EMPTY_FILE, menu_path); 29660Sstevel@tonic-gate return (BAM_SUCCESS); 29670Sstevel@tonic-gate } 29680Sstevel@tonic-gate 29690Sstevel@tonic-gate if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) { 29700Sstevel@tonic-gate return (BAM_ERROR); 29710Sstevel@tonic-gate } 29720Sstevel@tonic-gate 29730Sstevel@tonic-gate return (BAM_WRITE); 29740Sstevel@tonic-gate } 29750Sstevel@tonic-gate 29760Sstevel@tonic-gate static FILE * 2977662Sszhou open_diskmap(char *root) 29780Sstevel@tonic-gate { 29790Sstevel@tonic-gate FILE *fp; 29800Sstevel@tonic-gate char cmd[PATH_MAX]; 29810Sstevel@tonic-gate 29820Sstevel@tonic-gate /* make sure we have a map file */ 29830Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 29840Sstevel@tonic-gate if (fp == NULL) { 29850Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), 2986662Sszhou "%s%s > /dev/null", root, CREATE_DISKMAP); 29870Sstevel@tonic-gate (void) system(cmd); 29880Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 29890Sstevel@tonic-gate } 29900Sstevel@tonic-gate return (fp); 29910Sstevel@tonic-gate } 29920Sstevel@tonic-gate 29930Sstevel@tonic-gate #define SECTOR_SIZE 512 29940Sstevel@tonic-gate 29950Sstevel@tonic-gate static int 29960Sstevel@tonic-gate get_partition(char *device) 29970Sstevel@tonic-gate { 29980Sstevel@tonic-gate int i, fd, is_pcfs, partno = -1; 29990Sstevel@tonic-gate struct mboot *mboot; 30000Sstevel@tonic-gate char boot_sect[SECTOR_SIZE]; 30010Sstevel@tonic-gate char *wholedisk, *slice; 30020Sstevel@tonic-gate 30030Sstevel@tonic-gate /* form whole disk (p0) */ 30040Sstevel@tonic-gate slice = device + strlen(device) - 2; 30050Sstevel@tonic-gate is_pcfs = (*slice != 's'); 30060Sstevel@tonic-gate if (!is_pcfs) 30070Sstevel@tonic-gate *slice = '\0'; 30080Sstevel@tonic-gate wholedisk = s_calloc(1, strlen(device) + 3); 30090Sstevel@tonic-gate (void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device); 30100Sstevel@tonic-gate if (!is_pcfs) 30110Sstevel@tonic-gate *slice = 's'; 30120Sstevel@tonic-gate 30130Sstevel@tonic-gate /* read boot sector */ 30140Sstevel@tonic-gate fd = open(wholedisk, O_RDONLY); 30150Sstevel@tonic-gate free(wholedisk); 30160Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 30170Sstevel@tonic-gate return (partno); 30180Sstevel@tonic-gate } 30190Sstevel@tonic-gate (void) close(fd); 30200Sstevel@tonic-gate 30210Sstevel@tonic-gate /* parse fdisk table */ 30220Sstevel@tonic-gate mboot = (struct mboot *)((void *)boot_sect); 30230Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 30240Sstevel@tonic-gate struct ipart *part = 30250Sstevel@tonic-gate (struct ipart *)(uintptr_t)mboot->parts + i; 30260Sstevel@tonic-gate if (is_pcfs) { /* looking for solaris boot part */ 30270Sstevel@tonic-gate if (part->systid == 0xbe) { 30280Sstevel@tonic-gate partno = i; 30290Sstevel@tonic-gate break; 30300Sstevel@tonic-gate } 30310Sstevel@tonic-gate } else { /* look for solaris partition, old and new */ 30320Sstevel@tonic-gate if (part->systid == SUNIXOS || 30330Sstevel@tonic-gate part->systid == SUNIXOS2) { 30340Sstevel@tonic-gate partno = i; 30350Sstevel@tonic-gate break; 30360Sstevel@tonic-gate } 30370Sstevel@tonic-gate } 30380Sstevel@tonic-gate } 30390Sstevel@tonic-gate return (partno); 30400Sstevel@tonic-gate } 30410Sstevel@tonic-gate 30420Sstevel@tonic-gate static char * 30430Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev) 30440Sstevel@tonic-gate { 30450Sstevel@tonic-gate char *grubdisk; /* (hd#,#,#) */ 30460Sstevel@tonic-gate char *slice; 30470Sstevel@tonic-gate char *grubhd; 30480Sstevel@tonic-gate int fdiskpart; 30490Sstevel@tonic-gate int found = 0; 30500Sstevel@tonic-gate char *devname, *ctdname = strstr(rootdev, "dsk/"); 30510Sstevel@tonic-gate char linebuf[PATH_MAX]; 30520Sstevel@tonic-gate 30530Sstevel@tonic-gate if (ctdname == NULL) 30540Sstevel@tonic-gate return (NULL); 30550Sstevel@tonic-gate 30560Sstevel@tonic-gate ctdname += strlen("dsk/"); 30570Sstevel@tonic-gate slice = strrchr(ctdname, 's'); 30580Sstevel@tonic-gate if (slice) 30590Sstevel@tonic-gate *slice = '\0'; 30600Sstevel@tonic-gate 30610Sstevel@tonic-gate rewind(fp); 30620Sstevel@tonic-gate while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) { 30630Sstevel@tonic-gate grubhd = strtok(linebuf, " \t\n"); 30640Sstevel@tonic-gate if (grubhd) 30650Sstevel@tonic-gate devname = strtok(NULL, " \t\n"); 30660Sstevel@tonic-gate else 30670Sstevel@tonic-gate devname = NULL; 30680Sstevel@tonic-gate if (devname && strcmp(devname, ctdname) == 0) { 30690Sstevel@tonic-gate found = 1; 30700Sstevel@tonic-gate break; 30710Sstevel@tonic-gate } 30720Sstevel@tonic-gate } 30730Sstevel@tonic-gate 30740Sstevel@tonic-gate if (slice) 30750Sstevel@tonic-gate *slice = 's'; 30760Sstevel@tonic-gate 30770Sstevel@tonic-gate if (found == 0) { 30780Sstevel@tonic-gate if (bam_verbose) 30790Sstevel@tonic-gate bam_print(DISKMAP_FAIL_NONFATAL, rootdev); 30800Sstevel@tonic-gate grubhd = "0"; /* assume disk 0 if can't match */ 30810Sstevel@tonic-gate } 30820Sstevel@tonic-gate 30830Sstevel@tonic-gate fdiskpart = get_partition(rootdev); 30840Sstevel@tonic-gate if (fdiskpart == -1) 30850Sstevel@tonic-gate return (NULL); 30860Sstevel@tonic-gate 30870Sstevel@tonic-gate grubdisk = s_calloc(1, 10); 30880Sstevel@tonic-gate if (slice) { 30890Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d,%c)", 30900Sstevel@tonic-gate grubhd, fdiskpart, slice[1] + 'a' - '0'); 30910Sstevel@tonic-gate } else 30920Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d)", 30930Sstevel@tonic-gate grubhd, fdiskpart); 30940Sstevel@tonic-gate 30950Sstevel@tonic-gate /* if root not on bootdev, change GRUB disk to 0 */ 30960Sstevel@tonic-gate if (!on_bootdev) 30970Sstevel@tonic-gate grubdisk[3] = '0'; 30980Sstevel@tonic-gate return (grubdisk); 30990Sstevel@tonic-gate } 31000Sstevel@tonic-gate 3101656Sszhou static char * 3102656Sszhou get_title(char *rootdir) 31030Sstevel@tonic-gate { 31040Sstevel@tonic-gate static char title[80]; /* from /etc/release */ 3105662Sszhou char *cp = NULL, release[PATH_MAX]; 31060Sstevel@tonic-gate FILE *fp; 31070Sstevel@tonic-gate 31080Sstevel@tonic-gate /* open the /etc/release file */ 31090Sstevel@tonic-gate (void) snprintf(release, sizeof (release), "%s/etc/release", rootdir); 31100Sstevel@tonic-gate 31110Sstevel@tonic-gate fp = fopen(release, "r"); 31120Sstevel@tonic-gate if (fp == NULL) 3113656Sszhou return (NULL); 31140Sstevel@tonic-gate 31150Sstevel@tonic-gate while (s_fgets(title, sizeof (title), fp) != NULL) { 31160Sstevel@tonic-gate cp = strstr(title, "Solaris"); 31170Sstevel@tonic-gate if (cp) 31180Sstevel@tonic-gate break; 31190Sstevel@tonic-gate } 31200Sstevel@tonic-gate (void) fclose(fp); 3121662Sszhou return (cp == NULL ? "Solaris" : cp); 31220Sstevel@tonic-gate } 31230Sstevel@tonic-gate 31243446Smrj char * 31250Sstevel@tonic-gate get_special(char *mountp) 31260Sstevel@tonic-gate { 31270Sstevel@tonic-gate FILE *mntfp; 31280Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 31290Sstevel@tonic-gate 31300Sstevel@tonic-gate mntfp = fopen(MNTTAB, "r"); 31310Sstevel@tonic-gate if (mntfp == NULL) { 31320Sstevel@tonic-gate return (0); 31330Sstevel@tonic-gate } 31340Sstevel@tonic-gate 31350Sstevel@tonic-gate if (*mountp == '\0') 31360Sstevel@tonic-gate mpref.mnt_mountp = "/"; 31370Sstevel@tonic-gate else 31380Sstevel@tonic-gate mpref.mnt_mountp = mountp; 31390Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 31400Sstevel@tonic-gate (void) fclose(mntfp); 31410Sstevel@tonic-gate return (NULL); 31420Sstevel@tonic-gate } 31430Sstevel@tonic-gate (void) fclose(mntfp); 31440Sstevel@tonic-gate 31450Sstevel@tonic-gate return (s_strdup(mp.mnt_special)); 31460Sstevel@tonic-gate } 31470Sstevel@tonic-gate 31483446Smrj char * 31490Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev) 31500Sstevel@tonic-gate { 31510Sstevel@tonic-gate FILE *fp; 31520Sstevel@tonic-gate char *grubdisk; 31530Sstevel@tonic-gate 31540Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 3155662Sszhou fp = open_diskmap(""); 31560Sstevel@tonic-gate if (fp == NULL) { 31570Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdisk); 31580Sstevel@tonic-gate return (NULL); 31590Sstevel@tonic-gate } 31600Sstevel@tonic-gate grubdisk = get_grubdisk(osdisk, fp, on_bootdev); 31610Sstevel@tonic-gate (void) fclose(fp); 31620Sstevel@tonic-gate return (grubdisk); 31630Sstevel@tonic-gate } 31640Sstevel@tonic-gate 31650Sstevel@tonic-gate /* 31660Sstevel@tonic-gate * Check if root is on the boot device 31670Sstevel@tonic-gate * Return 0 (false) on error 31680Sstevel@tonic-gate */ 31690Sstevel@tonic-gate static int 31700Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp) 31710Sstevel@tonic-gate { 31720Sstevel@tonic-gate int ret; 31730Sstevel@tonic-gate char *grubhd, *bootp, *special; 31740Sstevel@tonic-gate 31750Sstevel@tonic-gate special = get_special(menu_root); 31760Sstevel@tonic-gate if (special == NULL) 31770Sstevel@tonic-gate return (0); 31780Sstevel@tonic-gate bootp = strstr(special, "p0:boot"); 31790Sstevel@tonic-gate if (bootp) 31800Sstevel@tonic-gate *bootp = '\0'; 31810Sstevel@tonic-gate grubhd = get_grubdisk(special, fp, 1); 31820Sstevel@tonic-gate free(special); 31830Sstevel@tonic-gate 31840Sstevel@tonic-gate if (grubhd == NULL) 31850Sstevel@tonic-gate return (0); 31860Sstevel@tonic-gate ret = grubhd[3] == '0'; 31870Sstevel@tonic-gate free(grubhd); 31880Sstevel@tonic-gate return (ret); 31890Sstevel@tonic-gate } 31900Sstevel@tonic-gate 3191662Sszhou /* 3192662Sszhou * look for matching bootadm entry with specified parameters 3193662Sszhou * Here are the rules (based on existing usage): 3194662Sszhou * - If title is specified, match on title only 3195662Sszhou * - Else, match on grubdisk and module (don't care about kernel line). 3196662Sszhou * note that, if root_opt is non-zero, the absence of root line is 3197662Sszhou * considered a match. 3198662Sszhou */ 3199662Sszhou static entry_t * 3200662Sszhou find_boot_entry(menu_t *mp, char *title, char *root, char *module, 3201662Sszhou int root_opt, int *entry_num) 3202662Sszhou { 3203662Sszhou int i; 3204662Sszhou line_t *lp; 3205662Sszhou entry_t *ent; 3206662Sszhou 3207662Sszhou /* find matching entry */ 3208662Sszhou for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) { 3209662Sszhou lp = ent->start; 3210662Sszhou 3211662Sszhou /* first line of entry must be bootadm comment */ 3212662Sszhou lp = ent->start; 32133446Smrj if (lp->flags != BAM_COMMENT || 32143446Smrj strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) { 3215662Sszhou continue; 3216662Sszhou } 3217662Sszhou 3218662Sszhou /* advance to title line */ 3219662Sszhou lp = lp->next; 3220662Sszhou if (title) { 3221662Sszhou if (lp->flags == BAM_TITLE && lp->arg && 3222662Sszhou strcmp(lp->arg, title) == 0) 3223662Sszhou break; 3224662Sszhou continue; /* check title only */ 3225662Sszhou } 3226662Sszhou 3227662Sszhou lp = lp->next; /* advance to root line */ 3228662Sszhou if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) { 3229662Sszhou /* root command found, match grub disk */ 3230662Sszhou if (strcmp(lp->arg, root) != 0) { 3231662Sszhou continue; 3232662Sszhou } 3233662Sszhou lp = lp->next; /* advance to kernel line */ 3234662Sszhou } else { 3235662Sszhou /* no root command, see if root is optional */ 3236662Sszhou if (root_opt == 0) { 3237662Sszhou continue; 3238662Sszhou } 3239662Sszhou } 3240662Sszhou 3241662Sszhou if (lp == NULL || lp->next == NULL) { 3242662Sszhou continue; 3243662Sszhou } 3244662Sszhou 32453467Srscott /* 32463467Srscott * Check for matching module entry (failsafe or normal). We 32473467Srscott * use a strncmp to match "module" or "module$", since we 32483467Srscott * don't know which one it should be. If it fails to match, 32493467Srscott * we go around the loop again. 32503467Srscott */ 3251662Sszhou lp = lp->next; /* advance to module line */ 32523446Smrj if ((strncmp(lp->cmd, menu_cmds[MODULE_CMD], 32533446Smrj strlen(menu_cmds[MODULE_CMD])) != 0) || 32543446Smrj (strcmp(lp->arg, module) != 0)) { 3255662Sszhou continue; 3256662Sszhou } 3257662Sszhou break; /* match found */ 3258662Sszhou } 3259662Sszhou 3260662Sszhou *entry_num = i; 3261662Sszhou return (ent); 3262662Sszhou } 3263662Sszhou 3264662Sszhou static int 3265662Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel, 3266662Sszhou char *module, int root_opt) 3267662Sszhou { 32683446Smrj int i, change_kernel = 0; 3269662Sszhou entry_t *ent; 3270662Sszhou line_t *lp; 3271662Sszhou char linebuf[BAM_MAXLINE]; 3272662Sszhou 3273662Sszhou /* note: don't match on title, it's updated on upgrade */ 3274662Sszhou ent = find_boot_entry(mp, NULL, root, module, root_opt, &i); 32753446Smrj if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) { 32763446Smrj /* 32773446Smrj * We may be upgrading a kernel from multiboot to 32783446Smrj * directboot. Look for a multiboot entry. 32793446Smrj */ 32803446Smrj ent = find_boot_entry(mp, NULL, root, MULTI_BOOT_ARCHIVE, 32813446Smrj root_opt, &i); 32823446Smrj if (ent != NULL) { 32833446Smrj change_kernel = 1; 32843446Smrj } 32853446Smrj } 3286662Sszhou if (ent == NULL) 3287662Sszhou return (add_boot_entry(mp, title, root_opt ? NULL : root, 3288662Sszhou kernel, module)); 3289662Sszhou 3290662Sszhou /* replace title of exiting entry and delete root line */ 3291662Sszhou lp = ent->start; 3292662Sszhou lp = lp->next; /* title line */ 3293662Sszhou (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 3294662Sszhou menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 3295662Sszhou free(lp->arg); 3296662Sszhou free(lp->line); 3297662Sszhou lp->arg = s_strdup(title); 3298662Sszhou lp->line = s_strdup(linebuf); 3299662Sszhou 3300662Sszhou lp = lp->next; /* root line */ 3301662Sszhou if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) { 3302662Sszhou if (root_opt) { /* root line not needed */ 3303662Sszhou line_t *tmp = lp; 3304662Sszhou lp = lp->next; 3305662Sszhou unlink_line(mp, tmp); 3306662Sszhou line_free(tmp); 3307662Sszhou } else 3308662Sszhou lp = lp->next; 3309662Sszhou } 33103446Smrj 33113446Smrj if (change_kernel) { 33123446Smrj /* 33133446Smrj * We're upgrading from multiboot to directboot. 33143446Smrj */ 33153446Smrj if (strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) { 33163446Smrj (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 33173446Smrj menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD], 33183446Smrj kernel); 33193446Smrj free(lp->arg); 33203446Smrj free(lp->line); 33213446Smrj lp->arg = s_strdup(kernel); 33223446Smrj lp->line = s_strdup(linebuf); 33233446Smrj lp = lp->next; 33243446Smrj } 33253446Smrj if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) { 33263446Smrj (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 33273446Smrj menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD], 33283446Smrj module); 33293446Smrj free(lp->arg); 33303446Smrj free(lp->line); 33313446Smrj lp->arg = s_strdup(module); 33323446Smrj lp->line = s_strdup(linebuf); 33333446Smrj lp = lp->next; 33343446Smrj } 33353446Smrj } 3336662Sszhou return (i); 3337662Sszhou } 3338662Sszhou 33390Sstevel@tonic-gate /*ARGSUSED*/ 33400Sstevel@tonic-gate static error_t 33410Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt) 33420Sstevel@tonic-gate { 33430Sstevel@tonic-gate FILE *fp; 33440Sstevel@tonic-gate int entry; 33453449Srscott char *grubdisk, *title, *osdev, *osroot, *failsafe_kernel = NULL; 3346662Sszhou struct stat sbuf; 3347662Sszhou char failsafe[256]; 33480Sstevel@tonic-gate 33490Sstevel@tonic-gate assert(mp); 33500Sstevel@tonic-gate assert(opt); 33510Sstevel@tonic-gate 33520Sstevel@tonic-gate osdev = strtok(opt, ","); 33530Sstevel@tonic-gate osroot = strtok(NULL, ","); 33540Sstevel@tonic-gate if (osroot == NULL) 33550Sstevel@tonic-gate osroot = menu_root; 33560Sstevel@tonic-gate title = get_title(osroot); 33570Sstevel@tonic-gate 33580Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 3359662Sszhou fp = open_diskmap(osroot); 33600Sstevel@tonic-gate if (fp == NULL) { 33610Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 33620Sstevel@tonic-gate return (BAM_ERROR); 33630Sstevel@tonic-gate } 33640Sstevel@tonic-gate grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp)); 33650Sstevel@tonic-gate (void) fclose(fp); 33660Sstevel@tonic-gate if (grubdisk == NULL) { 33670Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 33680Sstevel@tonic-gate return (BAM_ERROR); 33690Sstevel@tonic-gate } 33700Sstevel@tonic-gate 33710Sstevel@tonic-gate /* add the entry for normal Solaris */ 33723446Smrj if (bam_direct == BAM_DIRECT_DBOOT) { 33733446Smrj entry = update_boot_entry(mp, title, grubdisk, 33743446Smrj DIRECT_BOOT_KERNEL, DIRECT_BOOT_ARCHIVE, 33753446Smrj osroot == menu_root); 33763446Smrj } else { 33773446Smrj entry = update_boot_entry(mp, title, grubdisk, 33783446Smrj MULTI_BOOT, MULTI_BOOT_ARCHIVE, 33793446Smrj osroot == menu_root); 33803446Smrj } 33810Sstevel@tonic-gate 33820Sstevel@tonic-gate /* add the entry for failsafe archive */ 33833446Smrj (void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot, MINIROOT); 33843446Smrj if (stat(failsafe, &sbuf) == 0) { 33853449Srscott 33863449Srscott /* Figure out where the kernel line should point */ 33873449Srscott (void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot, 33883449Srscott DIRECT_BOOT_FAILSAFE_KERNEL); 33893449Srscott if (stat(failsafe, &sbuf) == 0) { 33903449Srscott failsafe_kernel = DIRECT_BOOT_FAILSAFE_LINE; 33913449Srscott } else { 33923449Srscott (void) snprintf(failsafe, sizeof (failsafe), "%s%s", 33933449Srscott osroot, MULTI_BOOT_FAILSAFE); 33943449Srscott if (stat(failsafe, &sbuf) == 0) { 33953449Srscott failsafe_kernel = MULTI_BOOT_FAILSAFE_LINE; 33963449Srscott } 33973449Srscott } 33983449Srscott if (failsafe_kernel != NULL) { 33993449Srscott (void) update_boot_entry(mp, FAILSAFE_TITLE, grubdisk, 34003449Srscott failsafe_kernel, MINIROOT, osroot == menu_root); 34013449Srscott } else { 34023449Srscott bam_error(NO_FAILSAFE_KERNEL); 34033449Srscott } 34043446Smrj } 34050Sstevel@tonic-gate free(grubdisk); 34060Sstevel@tonic-gate 34070Sstevel@tonic-gate if (entry == BAM_ERROR) { 34080Sstevel@tonic-gate return (BAM_ERROR); 34090Sstevel@tonic-gate } 34100Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 34110Sstevel@tonic-gate return (BAM_WRITE); 34120Sstevel@tonic-gate } 34130Sstevel@tonic-gate 3414316Svikram static char * 3415316Svikram read_grub_root(void) 3416316Svikram { 3417316Svikram FILE *fp; 3418316Svikram struct stat sb; 3419316Svikram char buf[BAM_MAXLINE]; 3420316Svikram char *rootstr; 3421316Svikram 3422316Svikram if (stat(GRUB_slice, &sb) != 0) { 3423316Svikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 3424316Svikram return (NULL); 3425316Svikram } 3426316Svikram 3427316Svikram if (stat(GRUB_root, &sb) != 0) { 3428316Svikram bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno)); 3429316Svikram return (NULL); 3430316Svikram } 3431316Svikram 3432316Svikram fp = fopen(GRUB_root, "r"); 3433316Svikram if (fp == NULL) { 3434316Svikram bam_error(OPEN_FAIL, GRUB_root, strerror(errno)); 3435316Svikram return (NULL); 3436316Svikram } 3437316Svikram 3438316Svikram if (s_fgets(buf, sizeof (buf), fp) == NULL) { 3439316Svikram bam_error(EMPTY_FILE, GRUB_root, strerror(errno)); 3440316Svikram (void) fclose(fp); 3441316Svikram return (NULL); 3442316Svikram } 3443316Svikram 3444316Svikram /* 3445316Svikram * Copy buf here as check below may trash the buffer 3446316Svikram */ 3447316Svikram rootstr = s_strdup(buf); 3448316Svikram 3449316Svikram if (s_fgets(buf, sizeof (buf), fp) != NULL) { 3450316Svikram bam_error(BAD_ROOT_FILE, GRUB_root); 3451316Svikram free(rootstr); 3452316Svikram rootstr = NULL; 3453316Svikram } 3454316Svikram 3455316Svikram (void) fclose(fp); 3456316Svikram 3457316Svikram return (rootstr); 3458316Svikram } 3459316Svikram 3460662Sszhou static void 34613446Smrj save_default_entry(menu_t *mp, const char *which) 3462662Sszhou { 3463662Sszhou int lineNum, entryNum; 3464662Sszhou int entry = 0; /* default is 0 */ 3465662Sszhou char linebuf[BAM_MAXLINE]; 3466662Sszhou line_t *lp = mp->curdefault; 3467662Sszhou 34683381Svikram if (mp->start) { 34693381Svikram lineNum = mp->end->lineNum; 34703381Svikram entryNum = mp->end->entryNum; 34713381Svikram } else { 34723381Svikram lineNum = LINE_INIT; 34733381Svikram entryNum = ENTRY_INIT; 34743381Svikram } 34753381Svikram 3476662Sszhou if (lp) 3477662Sszhou entry = s_strtol(lp->arg); 3478662Sszhou 34793446Smrj (void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry); 3480662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 3481662Sszhou } 3482662Sszhou 3483662Sszhou static void 34843446Smrj restore_default_entry(menu_t *mp, const char *which, line_t *lp) 3485662Sszhou { 3486662Sszhou int entry; 3487662Sszhou char *str; 3488662Sszhou 3489662Sszhou if (lp == NULL) 3490662Sszhou return; /* nothing to restore */ 3491662Sszhou 34923446Smrj str = lp->arg + strlen(which); 3493662Sszhou entry = s_strtol(str); 3494662Sszhou (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 3495662Sszhou 3496662Sszhou /* delete saved old default line */ 3497662Sszhou unlink_line(mp, lp); 3498662Sszhou line_free(lp); 3499662Sszhou } 3500662Sszhou 35010Sstevel@tonic-gate /* 35020Sstevel@tonic-gate * This function is for supporting reboot with args. 35030Sstevel@tonic-gate * The opt value can be: 35040Sstevel@tonic-gate * NULL delete temp entry, if present 35050Sstevel@tonic-gate * entry=# switches default entry to 1 35060Sstevel@tonic-gate * else treated as boot-args and setup a temperary menu entry 35070Sstevel@tonic-gate * and make it the default 35080Sstevel@tonic-gate */ 35090Sstevel@tonic-gate #define REBOOT_TITLE "Solaris_reboot_transient" 35100Sstevel@tonic-gate 3511662Sszhou /*ARGSUSED*/ 35120Sstevel@tonic-gate static error_t 35130Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt) 35140Sstevel@tonic-gate { 35150Sstevel@tonic-gate int entry; 35164346Srscott char *grubdisk, *rootdev, *path, *opt_ptr; 35173446Smrj char kernbuf[BUFSIZ]; 35183446Smrj char args_buf[BUFSIZ]; 3519316Svikram struct stat sb; 35200Sstevel@tonic-gate 35210Sstevel@tonic-gate assert(mp); 35220Sstevel@tonic-gate 3523662Sszhou /* If no option, delete exiting reboot menu entry */ 3524662Sszhou if (opt == NULL) { 3525662Sszhou entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL, 3526662Sszhou 0, &entry); 3527662Sszhou if (ent == NULL) /* not found is ok */ 3528662Sszhou return (BAM_SUCCESS); 3529662Sszhou (void) do_delete(mp, entry); 35303446Smrj restore_default_entry(mp, BAM_OLDDEF, mp->olddefault); 35313446Smrj mp->olddefault = NULL; 3532662Sszhou return (BAM_WRITE); 3533662Sszhou } 3534662Sszhou 3535662Sszhou /* if entry= is specified, set the default entry */ 3536662Sszhou if (strncmp(opt, "entry=", strlen("entry=")) == 0 && 35370Sstevel@tonic-gate selector(mp, opt, &entry, NULL) == BAM_SUCCESS) { 35380Sstevel@tonic-gate /* this is entry=# option */ 35390Sstevel@tonic-gate return (set_global(mp, menu_cmds[DEFAULT_CMD], entry)); 35400Sstevel@tonic-gate } 35410Sstevel@tonic-gate 35420Sstevel@tonic-gate /* 35430Sstevel@tonic-gate * add a new menu entry base on opt and make it the default 35440Sstevel@tonic-gate */ 3545316Svikram grubdisk = NULL; 3546316Svikram if (stat(GRUB_slice, &sb) != 0) { 3547316Svikram /* 3548316Svikram * 1. First get root disk name from mnttab 3549316Svikram * 2. Translate disk name to grub name 3550316Svikram * 3. Add the new menu entry 3551316Svikram */ 3552316Svikram rootdev = get_special("/"); 3553316Svikram if (rootdev) { 3554316Svikram grubdisk = os_to_grubdisk(rootdev, 1); 3555316Svikram free(rootdev); 3556316Svikram } 3557316Svikram } else { 3558316Svikram /* 3559316Svikram * This is an LU BE. The GRUB_root file 3560316Svikram * contains entry for GRUB's "root" cmd. 3561316Svikram */ 3562316Svikram grubdisk = read_grub_root(); 35630Sstevel@tonic-gate } 35640Sstevel@tonic-gate if (grubdisk == NULL) { 3565316Svikram bam_error(REBOOT_WITH_ARGS_FAILED); 35660Sstevel@tonic-gate return (BAM_ERROR); 35670Sstevel@tonic-gate } 35680Sstevel@tonic-gate 35690Sstevel@tonic-gate /* add an entry for Solaris reboot */ 35703446Smrj if (bam_direct == BAM_DIRECT_DBOOT) { 35713446Smrj if (opt[0] == '-') { 35723446Smrj /* It's an option - first see if boot-file is set */ 35733446Smrj if (set_kernel(mp, KERNEL_CMD, NULL, kernbuf, BUFSIZ) 35743446Smrj != BAM_SUCCESS) 35753446Smrj return (BAM_ERROR); 35763446Smrj if (kernbuf[0] == '\0') 35773446Smrj (void) strncpy(kernbuf, DIRECT_BOOT_KERNEL, 35783446Smrj BUFSIZ); 35793446Smrj (void) strlcat(kernbuf, " ", BUFSIZ); 35803446Smrj (void) strlcat(kernbuf, opt, BUFSIZ); 35813446Smrj } else if (opt[0] == '/') { 35824346Srscott /* It's a full path, so write it out. */ 35833446Smrj (void) strlcpy(kernbuf, opt, BUFSIZ); 35844346Srscott 35854346Srscott /* 35864346Srscott * If someone runs: 35874346Srscott * 35884346Srscott * # eeprom boot-args='-kd' 35894346Srscott * # reboot /platform/i86pc/kernel/unix 35904346Srscott * 35914346Srscott * we want to use the boot-args as part of the boot 35924346Srscott * line. On the other hand, if someone runs: 35934346Srscott * 35944346Srscott * # reboot "/platform/i86pc/kernel/unix -kd" 35954346Srscott * 35964346Srscott * we don't need to mess with boot-args. If there's 35974346Srscott * no space in the options string, assume we're in the 35984346Srscott * first case. 35994346Srscott */ 36004346Srscott if (strchr(opt, ' ') == NULL) { 36014346Srscott if (set_kernel(mp, ARGS_CMD, NULL, args_buf, 36024346Srscott BUFSIZ) != BAM_SUCCESS) 36034346Srscott return (BAM_ERROR); 36044346Srscott 36054346Srscott if (args_buf[0] != '\0') { 36064346Srscott (void) strlcat(kernbuf, " ", BUFSIZ); 36074346Srscott (void) strlcat(kernbuf, args_buf, 36084346Srscott BUFSIZ); 36094346Srscott } 36104346Srscott } 36113446Smrj } else { 36124346Srscott /* 36134346Srscott * It may be a partial path, or it may be a partial 36144346Srscott * path followed by options. Assume that only options 36154346Srscott * follow a space. If someone sends us a kernel path 36164346Srscott * that includes a space, they deserve to be broken. 36174346Srscott */ 36184346Srscott opt_ptr = strchr(opt, ' '); 36194346Srscott if (opt_ptr != NULL) { 36204346Srscott *opt_ptr = '\0'; 36214346Srscott } 36224346Srscott 36233446Smrj path = expand_path(opt); 36243446Smrj if (path != NULL) { 36253446Smrj (void) strlcpy(kernbuf, path, BUFSIZ); 36263446Smrj free(path); 36274346Srscott 36284346Srscott /* 36294346Srscott * If there were options given, use those. 36304346Srscott * Otherwise, copy over the default options. 36314346Srscott */ 36324346Srscott if (opt_ptr != NULL) { 36334346Srscott /* Restore the space in opt string */ 36344346Srscott *opt_ptr = ' '; 36354346Srscott (void) strlcat(kernbuf, opt_ptr, 36364346Srscott BUFSIZ); 36374346Srscott } else { 36383446Smrj if (set_kernel(mp, ARGS_CMD, NULL, 36393446Smrj args_buf, BUFSIZ) != BAM_SUCCESS) 36403446Smrj return (BAM_ERROR); 36413446Smrj 36423446Smrj if (args_buf[0] != '\0') { 36433446Smrj (void) strlcat(kernbuf, " ", 36443446Smrj BUFSIZ); 36453446Smrj (void) strlcat(kernbuf, 36463446Smrj args_buf, BUFSIZ); 36473446Smrj } 36483446Smrj } 36494346Srscott } else { 36504346Srscott bam_error(UNKNOWN_KERNEL, opt); 36514346Srscott bam_print_stderr(UNKNOWN_KERNEL_REBOOT); 36524346Srscott return (BAM_ERROR); 36533446Smrj } 36543446Smrj } 36553446Smrj entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf, 36563446Smrj NULL); 36573446Smrj } else { 36583446Smrj (void) snprintf(kernbuf, sizeof (kernbuf), "%s %s", 36593446Smrj MULTI_BOOT, opt); 36603446Smrj entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf, 36613446Smrj MULTI_BOOT_ARCHIVE); 36623446Smrj } 36630Sstevel@tonic-gate free(grubdisk); 36640Sstevel@tonic-gate 36650Sstevel@tonic-gate if (entry == BAM_ERROR) { 3666316Svikram bam_error(REBOOT_WITH_ARGS_FAILED); 36670Sstevel@tonic-gate return (BAM_ERROR); 36680Sstevel@tonic-gate } 3669662Sszhou 36703446Smrj save_default_entry(mp, BAM_OLDDEF); 36710Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 36720Sstevel@tonic-gate return (BAM_WRITE); 36730Sstevel@tonic-gate } 36740Sstevel@tonic-gate 36750Sstevel@tonic-gate static error_t 36760Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val) 36770Sstevel@tonic-gate { 36780Sstevel@tonic-gate line_t *lp, *found, *last; 36790Sstevel@tonic-gate char *cp, *str; 36800Sstevel@tonic-gate char prefix[BAM_MAXLINE]; 36810Sstevel@tonic-gate size_t len; 36820Sstevel@tonic-gate 36830Sstevel@tonic-gate assert(mp); 36840Sstevel@tonic-gate assert(globalcmd); 36850Sstevel@tonic-gate 3686316Svikram if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) { 3687316Svikram if (val < 0 || mp->end == NULL || val > mp->end->entryNum) { 3688316Svikram (void) snprintf(prefix, sizeof (prefix), "%d", val); 3689316Svikram bam_error(INVALID_ENTRY, prefix); 3690316Svikram return (BAM_ERROR); 3691316Svikram } 3692316Svikram } 3693316Svikram 36940Sstevel@tonic-gate found = last = NULL; 36950Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 36960Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 36970Sstevel@tonic-gate continue; 36980Sstevel@tonic-gate 36990Sstevel@tonic-gate last = lp; /* track the last global found */ 37000Sstevel@tonic-gate 37010Sstevel@tonic-gate if (lp->cmd == NULL) { 37020Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 37030Sstevel@tonic-gate continue; 37040Sstevel@tonic-gate } 37050Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 37060Sstevel@tonic-gate continue; 37070Sstevel@tonic-gate 37080Sstevel@tonic-gate if (found) { 37090Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 37100Sstevel@tonic-gate } 37110Sstevel@tonic-gate found = lp; 37120Sstevel@tonic-gate } 37130Sstevel@tonic-gate 37140Sstevel@tonic-gate if (found == NULL) { 37150Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 37160Sstevel@tonic-gate if (last == NULL) { 37170Sstevel@tonic-gate lp->next = mp->start; 37180Sstevel@tonic-gate mp->start = lp; 37190Sstevel@tonic-gate mp->end = (mp->end) ? mp->end : lp; 37200Sstevel@tonic-gate } else { 37210Sstevel@tonic-gate lp->next = last->next; 37220Sstevel@tonic-gate last->next = lp; 37230Sstevel@tonic-gate if (lp->next == NULL) 37240Sstevel@tonic-gate mp->end = lp; 37250Sstevel@tonic-gate } 37260Sstevel@tonic-gate lp->flags = BAM_GLOBAL; /* other fields not needed for writes */ 37270Sstevel@tonic-gate len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]); 37280Sstevel@tonic-gate len += 10; /* val < 10 digits */ 37290Sstevel@tonic-gate lp->line = s_calloc(1, len); 37300Sstevel@tonic-gate (void) snprintf(lp->line, len, "%s%s%d", 37310Sstevel@tonic-gate globalcmd, menu_cmds[SEP_CMD], val); 37320Sstevel@tonic-gate return (BAM_WRITE); 37330Sstevel@tonic-gate } 37340Sstevel@tonic-gate 37350Sstevel@tonic-gate /* 37360Sstevel@tonic-gate * We are changing an existing entry. Retain any prefix whitespace, 37370Sstevel@tonic-gate * but overwrite everything else. This preserves tabs added for 37380Sstevel@tonic-gate * readability. 37390Sstevel@tonic-gate */ 37400Sstevel@tonic-gate str = found->line; 37410Sstevel@tonic-gate cp = prefix; 37420Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 37430Sstevel@tonic-gate *(cp++) = *(str++); 37440Sstevel@tonic-gate *cp = '\0'; /* Terminate prefix */ 37450Sstevel@tonic-gate len = strlen(prefix) + strlen(globalcmd); 37460Sstevel@tonic-gate len += strlen(menu_cmds[SEP_CMD]) + 10; 37470Sstevel@tonic-gate 37480Sstevel@tonic-gate free(found->line); 37490Sstevel@tonic-gate found->line = s_calloc(1, len); 37500Sstevel@tonic-gate (void) snprintf(found->line, len, 37514346Srscott "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val); 37520Sstevel@tonic-gate 37530Sstevel@tonic-gate return (BAM_WRITE); /* need a write to menu */ 37540Sstevel@tonic-gate } 37550Sstevel@tonic-gate 37563446Smrj /* 37573446Smrj * partial_path may be anything like "kernel/unix" or "kmdb". Try to 37584346Srscott * expand it to a full unix path. The calling function is expected to 37594346Srscott * output a message if an error occurs and NULL is returned. 37603446Smrj */ 37613446Smrj static char * 37623446Smrj expand_path(const char *partial_path) 37633446Smrj { 37643446Smrj int new_path_len; 37653446Smrj char *new_path, new_path2[PATH_MAX]; 37663446Smrj struct stat sb; 37673446Smrj 37683446Smrj new_path_len = strlen(partial_path) + 64; 37693446Smrj new_path = s_calloc(1, new_path_len); 37703446Smrj 37713446Smrj /* First, try the simplest case - something like "kernel/unix" */ 37723446Smrj (void) snprintf(new_path, new_path_len, "/platform/i86pc/%s", 37733446Smrj partial_path); 37743446Smrj if (stat(new_path, &sb) == 0) { 37753446Smrj return (new_path); 37763446Smrj } 37773446Smrj 37783446Smrj if (strcmp(partial_path, "kmdb") == 0) { 37793446Smrj (void) snprintf(new_path, new_path_len, "%s -k", 37803446Smrj DIRECT_BOOT_KERNEL); 37813446Smrj return (new_path); 37823446Smrj } 37833446Smrj 37843446Smrj /* 37853446Smrj * We've quickly reached unsupported usage. Try once more to 37863446Smrj * see if we were just given a glom name. 37873446Smrj */ 37883446Smrj (void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix", 37893446Smrj partial_path); 37903446Smrj (void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix", 37913446Smrj partial_path); 37923446Smrj if (stat(new_path, &sb) == 0) { 37933446Smrj if (stat(new_path2, &sb) == 0) { 37943446Smrj /* 37953446Smrj * We matched both, so we actually 37963446Smrj * want to write the $ISADIR version. 37973446Smrj */ 37983446Smrj (void) snprintf(new_path, new_path_len, 37993446Smrj "/platform/i86pc/kernel/%s/$ISADIR/unix", 38003446Smrj partial_path); 38013446Smrj } 38023446Smrj return (new_path); 38033446Smrj } 38043446Smrj 38053446Smrj free(new_path); 38063446Smrj return (NULL); 38073446Smrj } 38083446Smrj 38093446Smrj /* 38103446Smrj * The kernel cmd and arg have been changed, so 38113446Smrj * check whether the archive line needs to change. 38123446Smrj */ 38133446Smrj static void 38143446Smrj set_archive_line(entry_t *entryp, line_t *kernelp) 38153446Smrj { 38163446Smrj line_t *lp = entryp->start; 38173446Smrj char *new_archive; 38183446Smrj menu_cmd_t m_cmd; 38193446Smrj 38203446Smrj for (; lp != NULL; lp = lp->next) { 38213446Smrj if (strncmp(lp->cmd, menu_cmds[MODULE_CMD], 38223446Smrj sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) { 38233446Smrj break; 38243446Smrj } 38253446Smrj if (lp == entryp->end) 38263446Smrj return; 38273446Smrj } 38283446Smrj if (lp == NULL) 38293446Smrj return; 38303446Smrj 38313446Smrj if (strstr(kernelp->arg, "$ISADIR") != NULL) { 38323446Smrj new_archive = DIRECT_BOOT_ARCHIVE; 38333446Smrj m_cmd = MODULE_DOLLAR_CMD; 38343446Smrj } else if (strstr(kernelp->arg, "amd64") != NULL) { 38353446Smrj new_archive = DIRECT_BOOT_ARCHIVE_64; 38363446Smrj m_cmd = MODULE_CMD; 38373446Smrj } else { 38383446Smrj new_archive = DIRECT_BOOT_ARCHIVE_32; 38393446Smrj m_cmd = MODULE_CMD; 38403446Smrj } 38413446Smrj 38423446Smrj if (strcmp(lp->arg, new_archive) == 0) 38433446Smrj return; 38443446Smrj 38453446Smrj if (strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) { 38463446Smrj free(lp->cmd); 38473446Smrj lp->cmd = s_strdup(menu_cmds[m_cmd]); 38483446Smrj } 38493446Smrj 38503446Smrj free(lp->arg); 38513446Smrj lp->arg = s_strdup(new_archive); 38523446Smrj update_line(lp); 38533446Smrj } 38543446Smrj 38553446Smrj /* 38563446Smrj * Title for an entry to set properties that once went in bootenv.rc. 38573446Smrj */ 38583446Smrj #define BOOTENV_RC_TITLE "Solaris bootenv rc" 38593446Smrj 38603446Smrj /* 38613446Smrj * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments 38623446Smrj * (optnum == ARGS_CMD) in the argument buf. If path is a zero-length 38633446Smrj * string, reset the value to the default. If path is a non-zero-length 38643446Smrj * string, set the kernel or arguments. 38653446Smrj */ 38663446Smrj static error_t 38673446Smrj set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize) 38683446Smrj { 38693446Smrj int entryNum, rv = BAM_SUCCESS, free_new_path = 0; 38703446Smrj entry_t *entryp; 38713446Smrj line_t *ptr, *kernelp; 38723446Smrj char *new_arg, *old_args, *space; 38733446Smrj char *grubdisk, *rootdev, *new_path; 38743446Smrj char old_space; 38753446Smrj size_t old_kernel_len, new_str_len; 38763446Smrj struct stat sb; 38773446Smrj 38783446Smrj assert(bufsize > 0); 38793446Smrj 38803446Smrj ptr = kernelp = NULL; 38813446Smrj new_arg = old_args = space = NULL; 38823446Smrj grubdisk = rootdev = new_path = NULL; 38833446Smrj buf[0] = '\0'; 38843446Smrj 38853446Smrj if (bam_direct != BAM_DIRECT_DBOOT) { 38863446Smrj bam_error(NOT_DBOOT, optnum == KERNEL_CMD ? "kernel" : "args"); 38873446Smrj return (BAM_ERROR); 38883446Smrj } 38893446Smrj 38903446Smrj /* 38913446Smrj * If a user changed the default entry to a non-bootadm controlled 38923446Smrj * one, we don't want to mess with it. Just print an error and 38933446Smrj * return. 38943446Smrj */ 38953446Smrj if (mp->curdefault) { 38963446Smrj entryNum = s_strtol(mp->curdefault->arg); 38973446Smrj for (entryp = mp->entries; entryp; entryp = entryp->next) { 38983446Smrj if (entryp->entryNum == entryNum) 38993446Smrj break; 39003446Smrj } 39013446Smrj if ((entryp != NULL) && 39023446Smrj ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) { 39033446Smrj bam_error(DEFAULT_NOT_BAM); 39043446Smrj return (BAM_ERROR); 39053446Smrj } 39063446Smrj } 39073446Smrj 39083446Smrj entryNum = -1; 39093446Smrj entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, 0, 39103446Smrj &entryNum); 39113446Smrj 39123446Smrj if (entryp != NULL) { 39133446Smrj for (ptr = entryp->start; ptr && ptr != entryp->end; 39143446Smrj ptr = ptr->next) { 39153446Smrj if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD], 39163446Smrj sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) { 39173446Smrj kernelp = ptr; 39183446Smrj break; 39193446Smrj } 39203446Smrj } 39213446Smrj if (kernelp == NULL) { 39223446Smrj bam_error(NO_KERNEL, entryNum); 39233446Smrj return (BAM_ERROR); 39243446Smrj } 39253446Smrj 39263446Smrj old_kernel_len = strcspn(kernelp->arg, " \t"); 39273446Smrj space = old_args = kernelp->arg + old_kernel_len; 39283446Smrj while ((*old_args == ' ') || (*old_args == '\t')) 39293446Smrj old_args++; 39303446Smrj } 39313446Smrj 39323446Smrj if (path == NULL) { 39333446Smrj /* Simply report what was found */ 39343446Smrj if (kernelp == NULL) 39353446Smrj return (BAM_SUCCESS); 39363446Smrj 39373446Smrj if (optnum == ARGS_CMD) { 39383446Smrj if (old_args[0] != '\0') 39393446Smrj (void) strlcpy(buf, old_args, bufsize); 39403446Smrj } else { 39413446Smrj /* 39423446Smrj * We need to print the kernel, so we just turn the 39433446Smrj * first space into a '\0' and print the beginning. 39443446Smrj * We don't print anything if it's the default kernel. 39453446Smrj */ 39463446Smrj old_space = *space; 39473446Smrj *space = '\0'; 39483446Smrj if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0) 39493446Smrj (void) strlcpy(buf, kernelp->arg, bufsize); 39503446Smrj *space = old_space; 39513446Smrj } 39523446Smrj return (BAM_SUCCESS); 39533446Smrj } 39543446Smrj 39553446Smrj /* 39563446Smrj * First, check if we're resetting an entry to the default. 39573446Smrj */ 39583446Smrj if ((path[0] == '\0') || 39593446Smrj ((optnum == KERNEL_CMD) && 39603446Smrj (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) { 39613446Smrj if ((entryp == NULL) || (kernelp == NULL)) { 39623446Smrj /* No previous entry, it's already the default */ 39633446Smrj return (BAM_SUCCESS); 39643446Smrj } 39653446Smrj 39663446Smrj /* 39673446Smrj * Check if we can delete the entry. If we're resetting the 39683446Smrj * kernel command, and the args is already empty, or if we're 39693446Smrj * resetting the args command, and the kernel is already the 39703446Smrj * default, we can restore the old default and delete the entry. 39713446Smrj */ 39723446Smrj if (((optnum == KERNEL_CMD) && 39733446Smrj ((old_args == NULL) || (old_args[0] == '\0'))) || 39743446Smrj ((optnum == ARGS_CMD) && 39753446Smrj (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL, 39763446Smrj sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) { 39773446Smrj kernelp = NULL; 39783446Smrj (void) do_delete(mp, entryNum); 39793446Smrj restore_default_entry(mp, BAM_OLD_RC_DEF, 39803446Smrj mp->old_rc_default); 39813446Smrj mp->old_rc_default = NULL; 39823446Smrj rv = BAM_WRITE; 39833446Smrj goto done; 39843446Smrj } 39853446Smrj 39863446Smrj if (optnum == KERNEL_CMD) { 39873446Smrj /* 39883446Smrj * At this point, we've already checked that old_args 39893446Smrj * and entryp are valid pointers. The "+ 2" is for 39903446Smrj * a space a the string termination character. 39913446Smrj */ 39923446Smrj new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) + 39933446Smrj strlen(old_args) + 2; 39943446Smrj new_arg = s_calloc(1, new_str_len); 39953446Smrj (void) snprintf(new_arg, new_str_len, "%s %s", 39963446Smrj DIRECT_BOOT_KERNEL, old_args); 39973446Smrj free(kernelp->arg); 39983446Smrj kernelp->arg = new_arg; 39993446Smrj 40003446Smrj /* 40013446Smrj * We have changed the kernel line, so we may need 40023446Smrj * to update the archive line as well. 40033446Smrj */ 40043446Smrj set_archive_line(entryp, kernelp); 40053446Smrj } else { 40063446Smrj /* 40073446Smrj * We're resetting the boot args to nothing, so 40083446Smrj * we only need to copy the kernel. We've already 40093446Smrj * checked that the kernel is not the default. 40103446Smrj */ 40113446Smrj new_arg = s_calloc(1, old_kernel_len + 1); 40123446Smrj (void) snprintf(new_arg, old_kernel_len + 1, "%s", 40133446Smrj kernelp->arg); 40143446Smrj free(kernelp->arg); 40153446Smrj kernelp->arg = new_arg; 40163446Smrj } 40173446Smrj rv = BAM_WRITE; 40183446Smrj goto done; 40193446Smrj } 40203446Smrj 40213446Smrj /* 40223446Smrj * Expand the kernel file to a full path, if necessary 40233446Smrj */ 40243446Smrj if ((optnum == KERNEL_CMD) && (path[0] != '/')) { 40253446Smrj new_path = expand_path(path); 40263446Smrj if (new_path == NULL) { 40274346Srscott bam_error(UNKNOWN_KERNEL, path); 40283446Smrj return (BAM_ERROR); 40293446Smrj } 40303446Smrj free_new_path = 1; 40313446Smrj } else { 40323446Smrj new_path = path; 40333446Smrj free_new_path = 0; 40343446Smrj } 40353446Smrj 40363446Smrj /* 40373446Smrj * At this point, we know we're setting a new value. First, take care 40383446Smrj * of the case where there was no previous entry. 40393446Smrj */ 40403446Smrj if (entryp == NULL) { 40413446Smrj /* Similar to code in update_temp */ 40423446Smrj if (stat(GRUB_slice, &sb) != 0) { 40433446Smrj /* 40443446Smrj * 1. First get root disk name from mnttab 40453446Smrj * 2. Translate disk name to grub name 40463446Smrj * 3. Add the new menu entry 40473446Smrj */ 40483446Smrj rootdev = get_special("/"); 40493446Smrj if (rootdev) { 40503446Smrj grubdisk = os_to_grubdisk(rootdev, 1); 40513446Smrj free(rootdev); 40523446Smrj } 40533446Smrj } else { 40543446Smrj /* 40553446Smrj * This is an LU BE. The GRUB_root file 40563446Smrj * contains entry for GRUB's "root" cmd. 40573446Smrj */ 40583446Smrj grubdisk = read_grub_root(); 40593446Smrj } 40603446Smrj if (grubdisk == NULL) { 40613446Smrj bam_error(REBOOT_WITH_ARGS_FAILED); 40623446Smrj rv = BAM_ERROR; 40633446Smrj goto done; 40643446Smrj } 40653446Smrj if (optnum == KERNEL_CMD) { 40663446Smrj entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE, 40673446Smrj grubdisk, new_path, NULL); 40683446Smrj } else { 40693446Smrj new_str_len = strlen(DIRECT_BOOT_KERNEL) + 40703446Smrj strlen(path) + 8; 40713446Smrj new_arg = s_calloc(1, new_str_len); 40723446Smrj 40733446Smrj (void) snprintf(new_arg, new_str_len, "%s %s", 40743446Smrj DIRECT_BOOT_KERNEL, path); 40753446Smrj entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE, 40763446Smrj grubdisk, new_arg, DIRECT_BOOT_ARCHIVE); 40773446Smrj } 40783446Smrj save_default_entry(mp, BAM_OLD_RC_DEF); 40793446Smrj (void) set_global(mp, menu_cmds[DEFAULT_CMD], entryNum); 40803446Smrj rv = BAM_WRITE; 40813446Smrj goto done; 40823446Smrj } 40833446Smrj 40843446Smrj /* 40853446Smrj * There was already an bootenv entry which we need to edit. 40863446Smrj */ 40873446Smrj if (optnum == KERNEL_CMD) { 40883446Smrj new_str_len = strlen(new_path) + strlen(old_args) + 2; 40893446Smrj new_arg = s_calloc(1, new_str_len); 40903446Smrj (void) snprintf(new_arg, new_str_len, "%s %s", new_path, 40913446Smrj old_args); 40923446Smrj free(kernelp->arg); 40933446Smrj kernelp->arg = new_arg; 40943446Smrj 40953446Smrj /* 40963446Smrj * If we have changed the kernel line, we may need to update 40973446Smrj * the archive line as well. 40983446Smrj */ 40993446Smrj set_archive_line(entryp, kernelp); 41003446Smrj } else { 41013446Smrj new_str_len = old_kernel_len + strlen(path) + 8; 41023446Smrj new_arg = s_calloc(1, new_str_len); 41033446Smrj (void) strncpy(new_arg, kernelp->arg, old_kernel_len); 41043446Smrj (void) strlcat(new_arg, " ", new_str_len); 41053446Smrj (void) strlcat(new_arg, path, new_str_len); 41063446Smrj free(kernelp->arg); 41073446Smrj kernelp->arg = new_arg; 41083446Smrj } 41093446Smrj rv = BAM_WRITE; 41103446Smrj 41113446Smrj done: 41123446Smrj if ((rv == BAM_WRITE) && kernelp) 41133446Smrj update_line(kernelp); 41143446Smrj if (free_new_path) 41153446Smrj free(new_path); 41163446Smrj return (rv); 41173446Smrj } 41183446Smrj 41190Sstevel@tonic-gate /*ARGSUSED*/ 41200Sstevel@tonic-gate static error_t 41210Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt) 41220Sstevel@tonic-gate { 41230Sstevel@tonic-gate int optnum, optval; 41240Sstevel@tonic-gate char *val; 41253446Smrj char buf[BUFSIZ] = ""; 41263446Smrj error_t rv; 41270Sstevel@tonic-gate 41280Sstevel@tonic-gate assert(mp); 41290Sstevel@tonic-gate assert(opt); 41300Sstevel@tonic-gate 41310Sstevel@tonic-gate val = strchr(opt, '='); 41323446Smrj if (val != NULL) { 41333446Smrj *val = '\0'; 41340Sstevel@tonic-gate } 41350Sstevel@tonic-gate 41360Sstevel@tonic-gate if (strcmp(opt, "default") == 0) { 41370Sstevel@tonic-gate optnum = DEFAULT_CMD; 41380Sstevel@tonic-gate } else if (strcmp(opt, "timeout") == 0) { 41390Sstevel@tonic-gate optnum = TIMEOUT_CMD; 41403446Smrj } else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) { 41413446Smrj optnum = KERNEL_CMD; 41423446Smrj } else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) { 41433446Smrj optnum = ARGS_CMD; 41440Sstevel@tonic-gate } else { 41450Sstevel@tonic-gate bam_error(INVALID_ENTRY, opt); 41460Sstevel@tonic-gate return (BAM_ERROR); 41470Sstevel@tonic-gate } 41480Sstevel@tonic-gate 41493446Smrj /* 41503446Smrj * kernel and args are allowed without "=new_value" strings. All 41513446Smrj * others cause errors 41523446Smrj */ 41533446Smrj if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) { 41543446Smrj bam_error(INVALID_ENTRY, opt); 41553446Smrj return (BAM_ERROR); 41563446Smrj } else if (val != NULL) { 41573446Smrj *val = '='; 41583446Smrj } 41593446Smrj 41603446Smrj if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) { 41613446Smrj rv = set_kernel(mp, optnum, val ? val + 1 : NULL, buf, BUFSIZ); 41623446Smrj if ((rv == BAM_SUCCESS) && (buf[0] != '\0')) 41633446Smrj (void) printf("%s\n", buf); 41643446Smrj return (rv); 41653446Smrj } else { 41663446Smrj optval = s_strtol(val + 1); 41673446Smrj return (set_global(mp, menu_cmds[optnum], optval)); 41683446Smrj } 41690Sstevel@tonic-gate } 41700Sstevel@tonic-gate 41710Sstevel@tonic-gate /* 41720Sstevel@tonic-gate * The quiet argument suppresses messages. This is used 41730Sstevel@tonic-gate * when invoked in the context of other commands (e.g. list_entry) 41740Sstevel@tonic-gate */ 41750Sstevel@tonic-gate static error_t 41760Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet) 41770Sstevel@tonic-gate { 41780Sstevel@tonic-gate line_t *lp; 41790Sstevel@tonic-gate char *arg; 41800Sstevel@tonic-gate int done, ret = BAM_SUCCESS; 41810Sstevel@tonic-gate 41820Sstevel@tonic-gate assert(mp); 41830Sstevel@tonic-gate assert(menu_path); 41840Sstevel@tonic-gate assert(globalcmd); 41850Sstevel@tonic-gate 41860Sstevel@tonic-gate if (mp->start == NULL) { 41870Sstevel@tonic-gate if (!quiet) 41880Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 41890Sstevel@tonic-gate return (BAM_ERROR); 41900Sstevel@tonic-gate } 41910Sstevel@tonic-gate 41920Sstevel@tonic-gate done = 0; 41930Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 41940Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 41950Sstevel@tonic-gate continue; 41960Sstevel@tonic-gate 41970Sstevel@tonic-gate if (lp->cmd == NULL) { 41980Sstevel@tonic-gate if (!quiet) 41990Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 42000Sstevel@tonic-gate continue; 42010Sstevel@tonic-gate } 42020Sstevel@tonic-gate 42030Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 42040Sstevel@tonic-gate continue; 42050Sstevel@tonic-gate 42060Sstevel@tonic-gate /* Found global. Check for duplicates */ 42070Sstevel@tonic-gate if (done && !quiet) { 42080Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 42090Sstevel@tonic-gate ret = BAM_ERROR; 42100Sstevel@tonic-gate } 42110Sstevel@tonic-gate 42120Sstevel@tonic-gate arg = lp->arg ? lp->arg : ""; 42130Sstevel@tonic-gate bam_print(GLOBAL_CMD, globalcmd, arg); 42140Sstevel@tonic-gate done = 1; 42150Sstevel@tonic-gate } 42160Sstevel@tonic-gate 42170Sstevel@tonic-gate if (!done && bam_verbose) 42180Sstevel@tonic-gate bam_print(NO_ENTRY, globalcmd); 42190Sstevel@tonic-gate 42200Sstevel@tonic-gate return (ret); 42210Sstevel@tonic-gate } 42220Sstevel@tonic-gate 42230Sstevel@tonic-gate static error_t 42240Sstevel@tonic-gate menu_write(char *root, menu_t *mp) 42250Sstevel@tonic-gate { 42260Sstevel@tonic-gate return (list2file(root, MENU_TMP, GRUB_MENU, mp->start)); 42270Sstevel@tonic-gate } 42280Sstevel@tonic-gate 42290Sstevel@tonic-gate static void 42300Sstevel@tonic-gate line_free(line_t *lp) 42310Sstevel@tonic-gate { 42320Sstevel@tonic-gate if (lp == NULL) 42330Sstevel@tonic-gate return; 42340Sstevel@tonic-gate 42350Sstevel@tonic-gate if (lp->cmd) 42360Sstevel@tonic-gate free(lp->cmd); 42370Sstevel@tonic-gate if (lp->sep) 42380Sstevel@tonic-gate free(lp->sep); 42390Sstevel@tonic-gate if (lp->arg) 42400Sstevel@tonic-gate free(lp->arg); 42410Sstevel@tonic-gate if (lp->line) 42420Sstevel@tonic-gate free(lp->line); 42430Sstevel@tonic-gate free(lp); 42440Sstevel@tonic-gate } 42450Sstevel@tonic-gate 42460Sstevel@tonic-gate static void 42470Sstevel@tonic-gate linelist_free(line_t *start) 42480Sstevel@tonic-gate { 42490Sstevel@tonic-gate line_t *lp; 42500Sstevel@tonic-gate 42510Sstevel@tonic-gate while (start) { 42520Sstevel@tonic-gate lp = start; 42530Sstevel@tonic-gate start = start->next; 42540Sstevel@tonic-gate line_free(lp); 42550Sstevel@tonic-gate } 42560Sstevel@tonic-gate } 42570Sstevel@tonic-gate 42580Sstevel@tonic-gate static void 42590Sstevel@tonic-gate filelist_free(filelist_t *flistp) 42600Sstevel@tonic-gate { 42610Sstevel@tonic-gate linelist_free(flistp->head); 42620Sstevel@tonic-gate flistp->head = NULL; 42630Sstevel@tonic-gate flistp->tail = NULL; 42640Sstevel@tonic-gate } 42650Sstevel@tonic-gate 42660Sstevel@tonic-gate static void 42670Sstevel@tonic-gate menu_free(menu_t *mp) 42680Sstevel@tonic-gate { 4269662Sszhou entry_t *ent, *tmp; 42700Sstevel@tonic-gate assert(mp); 42710Sstevel@tonic-gate 42720Sstevel@tonic-gate if (mp->start) 42730Sstevel@tonic-gate linelist_free(mp->start); 4274662Sszhou ent = mp->entries; 4275662Sszhou while (ent) { 4276662Sszhou tmp = ent; 4277662Sszhou ent = tmp->next; 4278662Sszhou free(tmp); 4279662Sszhou } 4280662Sszhou 42810Sstevel@tonic-gate free(mp); 42820Sstevel@tonic-gate } 42830Sstevel@tonic-gate 42840Sstevel@tonic-gate /* 42850Sstevel@tonic-gate * Utility routines 42860Sstevel@tonic-gate */ 42870Sstevel@tonic-gate 42880Sstevel@tonic-gate 42890Sstevel@tonic-gate /* 42900Sstevel@tonic-gate * Returns 0 on success 42910Sstevel@tonic-gate * Any other value indicates an error 42920Sstevel@tonic-gate */ 42930Sstevel@tonic-gate static int 42940Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize) 42950Sstevel@tonic-gate { 42960Sstevel@tonic-gate char buf[BUFSIZ]; 42970Sstevel@tonic-gate int ret; 42980Sstevel@tonic-gate FILE *ptr; 42990Sstevel@tonic-gate size_t len; 43000Sstevel@tonic-gate sigset_t set; 43010Sstevel@tonic-gate void (*disp)(int); 43020Sstevel@tonic-gate 43030Sstevel@tonic-gate /* 43040Sstevel@tonic-gate * For security 43050Sstevel@tonic-gate * - only absolute paths are allowed 43060Sstevel@tonic-gate * - set IFS to space and tab 43070Sstevel@tonic-gate */ 43080Sstevel@tonic-gate if (*cmdline != '/') { 43090Sstevel@tonic-gate bam_error(ABS_PATH_REQ, cmdline); 43100Sstevel@tonic-gate return (-1); 43110Sstevel@tonic-gate } 43120Sstevel@tonic-gate (void) putenv("IFS= \t"); 43130Sstevel@tonic-gate 43140Sstevel@tonic-gate /* 43150Sstevel@tonic-gate * We may have been exec'ed with SIGCHLD blocked 43160Sstevel@tonic-gate * unblock it here 43170Sstevel@tonic-gate */ 43180Sstevel@tonic-gate (void) sigemptyset(&set); 43190Sstevel@tonic-gate (void) sigaddset(&set, SIGCHLD); 43200Sstevel@tonic-gate if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { 43210Sstevel@tonic-gate bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno)); 43220Sstevel@tonic-gate return (-1); 43230Sstevel@tonic-gate } 43240Sstevel@tonic-gate 43250Sstevel@tonic-gate /* 43260Sstevel@tonic-gate * Set SIGCHLD disposition to SIG_DFL for popen/pclose 43270Sstevel@tonic-gate */ 43280Sstevel@tonic-gate disp = sigset(SIGCHLD, SIG_DFL); 43290Sstevel@tonic-gate if (disp == SIG_ERR) { 43300Sstevel@tonic-gate bam_error(FAILED_SIG, strerror(errno)); 43310Sstevel@tonic-gate return (-1); 43320Sstevel@tonic-gate } 43330Sstevel@tonic-gate if (disp == SIG_HOLD) { 43340Sstevel@tonic-gate bam_error(BLOCKED_SIG, cmdline); 43350Sstevel@tonic-gate return (-1); 43360Sstevel@tonic-gate } 43370Sstevel@tonic-gate 43380Sstevel@tonic-gate ptr = popen(cmdline, "r"); 43390Sstevel@tonic-gate if (ptr == NULL) { 43400Sstevel@tonic-gate bam_error(POPEN_FAIL, cmdline, strerror(errno)); 43410Sstevel@tonic-gate return (-1); 43420Sstevel@tonic-gate } 43430Sstevel@tonic-gate 43440Sstevel@tonic-gate /* 43450Sstevel@tonic-gate * If we simply do a pclose() following a popen(), pclose() 43460Sstevel@tonic-gate * will close the reader end of the pipe immediately even 43470Sstevel@tonic-gate * if the child process has not started/exited. pclose() 43480Sstevel@tonic-gate * does wait for cmd to terminate before returning though. 43490Sstevel@tonic-gate * When the executed command writes its output to the pipe 43500Sstevel@tonic-gate * there is no reader process and the command dies with 43510Sstevel@tonic-gate * SIGPIPE. To avoid this we read repeatedly until read 43520Sstevel@tonic-gate * terminates with EOF. This indicates that the command 43530Sstevel@tonic-gate * (writer) has closed the pipe and we can safely do a 43540Sstevel@tonic-gate * pclose(). 43550Sstevel@tonic-gate * 43560Sstevel@tonic-gate * Since pclose() does wait for the command to exit, 43570Sstevel@tonic-gate * we can safely reap the exit status of the command 43580Sstevel@tonic-gate * from the value returned by pclose() 43590Sstevel@tonic-gate */ 43600Sstevel@tonic-gate while (fgets(buf, sizeof (buf), ptr) != NULL) { 43610Sstevel@tonic-gate /* if (bam_verbose) XXX */ 43620Sstevel@tonic-gate bam_print(PRINT_NO_NEWLINE, buf); 43630Sstevel@tonic-gate if (output && osize > 0) { 43640Sstevel@tonic-gate (void) snprintf(output, osize, "%s", buf); 43650Sstevel@tonic-gate len = strlen(buf); 43660Sstevel@tonic-gate output += len; 43670Sstevel@tonic-gate osize -= len; 43680Sstevel@tonic-gate } 43690Sstevel@tonic-gate } 43700Sstevel@tonic-gate 43710Sstevel@tonic-gate ret = pclose(ptr); 43720Sstevel@tonic-gate if (ret == -1) { 43730Sstevel@tonic-gate bam_error(PCLOSE_FAIL, cmdline, strerror(errno)); 43740Sstevel@tonic-gate return (-1); 43750Sstevel@tonic-gate } 43760Sstevel@tonic-gate 43770Sstevel@tonic-gate if (WIFEXITED(ret)) { 43780Sstevel@tonic-gate return (WEXITSTATUS(ret)); 43790Sstevel@tonic-gate } else { 43800Sstevel@tonic-gate bam_error(EXEC_FAIL, cmdline, ret); 43810Sstevel@tonic-gate return (-1); 43820Sstevel@tonic-gate } 43830Sstevel@tonic-gate } 43840Sstevel@tonic-gate 43850Sstevel@tonic-gate /* 43860Sstevel@tonic-gate * Since this function returns -1 on error 43870Sstevel@tonic-gate * it cannot be used to convert -1. However, 43880Sstevel@tonic-gate * that is sufficient for what we need. 43890Sstevel@tonic-gate */ 43900Sstevel@tonic-gate static long 43910Sstevel@tonic-gate s_strtol(char *str) 43920Sstevel@tonic-gate { 43930Sstevel@tonic-gate long l; 43940Sstevel@tonic-gate char *res = NULL; 43950Sstevel@tonic-gate 43960Sstevel@tonic-gate if (str == NULL) { 43970Sstevel@tonic-gate return (-1); 43980Sstevel@tonic-gate } 43990Sstevel@tonic-gate 44000Sstevel@tonic-gate errno = 0; 44010Sstevel@tonic-gate l = strtol(str, &res, 10); 44020Sstevel@tonic-gate if (errno || *res != '\0') { 44030Sstevel@tonic-gate return (-1); 44040Sstevel@tonic-gate } 44050Sstevel@tonic-gate 44060Sstevel@tonic-gate return (l); 44070Sstevel@tonic-gate } 44080Sstevel@tonic-gate 44090Sstevel@tonic-gate /* 44100Sstevel@tonic-gate * Wrapper around fputs, that adds a newline (since fputs doesn't) 44110Sstevel@tonic-gate */ 44120Sstevel@tonic-gate static int 44130Sstevel@tonic-gate s_fputs(char *str, FILE *fp) 44140Sstevel@tonic-gate { 44150Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 44160Sstevel@tonic-gate 44170Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s\n", str); 44180Sstevel@tonic-gate return (fputs(linebuf, fp)); 44190Sstevel@tonic-gate } 44200Sstevel@tonic-gate 44210Sstevel@tonic-gate /* 44220Sstevel@tonic-gate * Wrapper around fgets, that strips newlines returned by fgets 44230Sstevel@tonic-gate */ 44243446Smrj char * 44250Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp) 44260Sstevel@tonic-gate { 44270Sstevel@tonic-gate int n; 44280Sstevel@tonic-gate 44290Sstevel@tonic-gate buf = fgets(buf, buflen, fp); 44300Sstevel@tonic-gate if (buf) { 44310Sstevel@tonic-gate n = strlen(buf); 44320Sstevel@tonic-gate if (n == buflen - 1 && buf[n-1] != '\n') 44330Sstevel@tonic-gate bam_error(TOO_LONG, buflen - 1, buf); 44340Sstevel@tonic-gate buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1]; 44350Sstevel@tonic-gate } 44360Sstevel@tonic-gate 44370Sstevel@tonic-gate return (buf); 44380Sstevel@tonic-gate } 44390Sstevel@tonic-gate 44403446Smrj void * 44410Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz) 44420Sstevel@tonic-gate { 44430Sstevel@tonic-gate void *ptr; 44440Sstevel@tonic-gate 44450Sstevel@tonic-gate ptr = calloc(nelem, sz); 44460Sstevel@tonic-gate if (ptr == NULL) { 44470Sstevel@tonic-gate bam_error(NO_MEM, nelem*sz); 44480Sstevel@tonic-gate bam_exit(1); 44490Sstevel@tonic-gate } 44500Sstevel@tonic-gate return (ptr); 44510Sstevel@tonic-gate } 44520Sstevel@tonic-gate 44533446Smrj void * 44543446Smrj s_realloc(void *ptr, size_t sz) 44553446Smrj { 44563446Smrj ptr = realloc(ptr, sz); 44573446Smrj if (ptr == NULL) { 44583446Smrj bam_error(NO_MEM, sz); 44593446Smrj bam_exit(1); 44603446Smrj } 44613446Smrj return (ptr); 44623446Smrj } 44633446Smrj 44640Sstevel@tonic-gate static char * 44650Sstevel@tonic-gate s_strdup(char *str) 44660Sstevel@tonic-gate { 44670Sstevel@tonic-gate char *ptr; 44680Sstevel@tonic-gate 44690Sstevel@tonic-gate if (str == NULL) 44700Sstevel@tonic-gate return (NULL); 44710Sstevel@tonic-gate 44720Sstevel@tonic-gate ptr = strdup(str); 44730Sstevel@tonic-gate if (ptr == NULL) { 44740Sstevel@tonic-gate bam_error(NO_MEM, strlen(str) + 1); 44750Sstevel@tonic-gate bam_exit(1); 44760Sstevel@tonic-gate } 44770Sstevel@tonic-gate return (ptr); 44780Sstevel@tonic-gate } 44790Sstevel@tonic-gate 44800Sstevel@tonic-gate /* 44810Sstevel@tonic-gate * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients) 44820Sstevel@tonic-gate * Returns 0 otherwise 44830Sstevel@tonic-gate */ 44840Sstevel@tonic-gate static int 44850Sstevel@tonic-gate is_amd64(void) 44860Sstevel@tonic-gate { 44870Sstevel@tonic-gate static int amd64 = -1; 44880Sstevel@tonic-gate char isabuf[257]; /* from sysinfo(2) manpage */ 44890Sstevel@tonic-gate 44900Sstevel@tonic-gate if (amd64 != -1) 44910Sstevel@tonic-gate return (amd64); 44920Sstevel@tonic-gate 44930Sstevel@tonic-gate if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 && 44940Sstevel@tonic-gate strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) 44950Sstevel@tonic-gate amd64 = 1; 44960Sstevel@tonic-gate else if (strstr(isabuf, "i386") == NULL) 44970Sstevel@tonic-gate amd64 = 1; /* diskless server */ 44980Sstevel@tonic-gate else 44990Sstevel@tonic-gate amd64 = 0; 45000Sstevel@tonic-gate 45010Sstevel@tonic-gate return (amd64); 45020Sstevel@tonic-gate } 45030Sstevel@tonic-gate 45040Sstevel@tonic-gate static void 45050Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s) 45060Sstevel@tonic-gate { 45070Sstevel@tonic-gate line_t *lp; 45080Sstevel@tonic-gate 45090Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 45100Sstevel@tonic-gate lp->line = s_strdup(s); 45110Sstevel@tonic-gate if (flistp->head == NULL) 45120Sstevel@tonic-gate flistp->head = lp; 45130Sstevel@tonic-gate else 45140Sstevel@tonic-gate flistp->tail->next = lp; 45150Sstevel@tonic-gate flistp->tail = lp; 45160Sstevel@tonic-gate } 4517