17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5fbac2b2bSvikram * Common Development and Distribution License (the "License"). 6fbac2b2bSvikram * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate /* 23e373b6e4SYuri Pankov * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 2440a5c998SMatthew Ahrens * Copyright 2012 Milan Jurik. All rights reserved. 2540a5c998SMatthew Ahrens * Copyright (c) 2015 by Delphix. All rights reserved. 26*eeb2c267SToomas Soome * Copyright 2016 Toomas Soome <tsoome@me.com> 27e373b6e4SYuri Pankov * Copyright 2016 Nexenta Systems, Inc. 286debd3f5SAlexander Eremin */ 296debd3f5SAlexander Eremin 306debd3f5SAlexander Eremin /* 317c478bd9Sstevel@tonic-gate * bootadm(1M) is a new utility for managing bootability of 327c478bd9Sstevel@tonic-gate * Solaris *Newboot* environments. It has two primary tasks: 337c478bd9Sstevel@tonic-gate * - Allow end users to manage bootability of Newboot Solaris instances 347c478bd9Sstevel@tonic-gate * - Provide services to other subsystems in Solaris (primarily Install) 357c478bd9Sstevel@tonic-gate */ 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* Headers */ 387c478bd9Sstevel@tonic-gate #include <stdio.h> 397c478bd9Sstevel@tonic-gate #include <errno.h> 407c478bd9Sstevel@tonic-gate #include <stdlib.h> 417c478bd9Sstevel@tonic-gate #include <string.h> 427c478bd9Sstevel@tonic-gate #include <unistd.h> 437c478bd9Sstevel@tonic-gate #include <sys/types.h> 447c478bd9Sstevel@tonic-gate #include <sys/stat.h> 4544da779fSWilliam Kucharski #include <alloca.h> 467c478bd9Sstevel@tonic-gate #include <stdarg.h> 477c478bd9Sstevel@tonic-gate #include <limits.h> 487c478bd9Sstevel@tonic-gate #include <signal.h> 497c478bd9Sstevel@tonic-gate #include <sys/wait.h> 507c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 51f904d32dSJerry Gilliam #include <sys/mntent.h> 527c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 537c478bd9Sstevel@tonic-gate #include <libnvpair.h> 547c478bd9Sstevel@tonic-gate #include <ftw.h> 557c478bd9Sstevel@tonic-gate #include <fcntl.h> 567c478bd9Sstevel@tonic-gate #include <strings.h> 572449e17fSsherrym #include <utime.h> 587c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 597c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 6058091fd8Ssetje #include <sys/param.h> 61eb2bd662Svikram #include <dirent.h> 62eb2bd662Svikram #include <ctype.h> 63eb2bd662Svikram #include <libgen.h> 64e7cbe64fSgw25295 #include <sys/sysmacros.h> 6548847494SEnrico Perla - Sun Microsystems #include <sys/elf.h> 66963390b4Svikram #include <libscf.h> 6748847494SEnrico Perla - Sun Microsystems #include <zlib.h> 6848847494SEnrico Perla - Sun Microsystems #include <sys/lockfs.h> 6948847494SEnrico Perla - Sun Microsystems #include <sys/filio.h> 706debd3f5SAlexander Eremin #include <libbe.h> 71e998e519SSheshadri Vasudevan #ifdef i386 72e998e519SSheshadri Vasudevan #include <libfdisk.h> 73e998e519SSheshadri Vasudevan #endif 74986fd29aSsetje 7579c28b70SToomas Soome #if !defined(_OBP) 762449e17fSsherrym #include <sys/ucode.h> 772449e17fSsherrym #endif 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate #include <pwd.h> 807c478bd9Sstevel@tonic-gate #include <grp.h> 817c478bd9Sstevel@tonic-gate #include <device_info.h> 82eb2bd662Svikram #include <sys/vtoc.h> 83eb2bd662Svikram #include <sys/efi_partition.h> 8489c3ee43SGangadhar Mylapuram #include <regex.h> 857c478bd9Sstevel@tonic-gate #include <locale.h> 86c7c0ceafSToomas Soome #include <sys/mkdev.h> 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate #include "message.h" 89ae115bc7Smrj #include "bootadm.h" 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN 927c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 937c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */ 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* Type definitions */ 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* Primary subcmds */ 987c478bd9Sstevel@tonic-gate typedef enum { 997c478bd9Sstevel@tonic-gate BAM_MENU = 3, 100c7c0ceafSToomas Soome BAM_ARCHIVE, 101c7c0ceafSToomas Soome BAM_INSTALL 1027c478bd9Sstevel@tonic-gate } subcmd_t; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate typedef enum { 1057c478bd9Sstevel@tonic-gate OPT_ABSENT = 0, /* No option */ 1067c478bd9Sstevel@tonic-gate OPT_REQ, /* option required */ 1077c478bd9Sstevel@tonic-gate OPT_OPTIONAL /* option may or may not be present */ 1087c478bd9Sstevel@tonic-gate } option_t; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate typedef struct { 1117c478bd9Sstevel@tonic-gate char *subcmd; 1127c478bd9Sstevel@tonic-gate option_t option; 1137c478bd9Sstevel@tonic-gate error_t (*handler)(); 1141a97e40eSvikram int unpriv; /* is this an unprivileged command */ 1157c478bd9Sstevel@tonic-gate } subcmd_defn_t; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate #define LINE_INIT 0 /* lineNum initial value */ 1187c478bd9Sstevel@tonic-gate #define ENTRY_INIT -1 /* entryNum initial value */ 1197c478bd9Sstevel@tonic-gate #define ALL_ENTRIES -2 /* selects all boot entries */ 1207c478bd9Sstevel@tonic-gate 1211a902ef8SHans Rosenfeld #define PARTNO_NOTFOUND -1 /* Solaris partition not found */ 1221a902ef8SHans Rosenfeld #define PARTNO_EFI -2 /* EFI partition table found */ 1231a902ef8SHans Rosenfeld 1247c478bd9Sstevel@tonic-gate #define GRUB_DIR "/boot/grub" 125963390b4Svikram #define GRUB_STAGE2 GRUB_DIR "/stage2" 1267c478bd9Sstevel@tonic-gate #define GRUB_MENU "/boot/grub/menu.lst" 1277c478bd9Sstevel@tonic-gate #define MENU_TMP "/boot/grub/menu.lst.tmp" 128963390b4Svikram #define GRUB_BACKUP_MENU "/etc/lu/GRUB_backup_menu" 12908db0dbcSAlexander Eremin #define RAMDISK_SPECIAL "/dev/ramdisk/" 13040541d5dSvikram #define STUBBOOT "/stubboot" 131eb2bd662Svikram #define MULTIBOOT "/platform/i86pc/multiboot" 132eb2bd662Svikram #define GRUBSIGN_DIR "/boot/grub/bootsign" 133eb2bd662Svikram #define GRUBSIGN_BACKUP "/etc/bootsign" 134eb2bd662Svikram #define GRUBSIGN_UFS_PREFIX "rootfs" 135eb2bd662Svikram #define GRUBSIGN_ZFS_PREFIX "pool_" 136eb2bd662Svikram #define GRUBSIGN_LU_PREFIX "BE_" 137eb2bd662Svikram #define UFS_SIGNATURE_LIST "/var/run/grub_ufs_signatures" 138eb2bd662Svikram #define ZFS_LEGACY_MNTPT "/tmp/bootadm_mnt_zfs_legacy" 139eb2bd662Svikram 140eb2bd662Svikram #define BOOTADM_RDONLY_TEST "BOOTADM_RDONLY_TEST" 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate /* lock related */ 1437c478bd9Sstevel@tonic-gate #define BAM_LOCK_FILE "/var/run/bootadm.lock" 1447c478bd9Sstevel@tonic-gate #define LOCK_FILE_PERMS (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 1457c478bd9Sstevel@tonic-gate 146986fd29aSsetje #define CREATE_RAMDISK "boot/solaris/bin/create_ramdisk" 147986fd29aSsetje #define CREATE_DISKMAP "boot/solaris/bin/create_diskmap" 148986fd29aSsetje #define EXTRACT_BOOT_FILELIST "boot/solaris/bin/extract_boot_filelist" 1497c478bd9Sstevel@tonic-gate #define GRUBDISK_MAP "/var/run/solaris_grubdisk.map" 1507c478bd9Sstevel@tonic-gate 151b610f78eSvikram #define GRUB_slice "/etc/lu/GRUB_slice" 152b610f78eSvikram #define GRUB_root "/etc/lu/GRUB_root" 153fbac2b2bSvikram #define GRUB_fdisk "/etc/lu/GRUB_fdisk" 154fbac2b2bSvikram #define GRUB_fdisk_target "/etc/lu/GRUB_fdisk_target" 155963390b4Svikram #define FINDROOT_INSTALLGRUB "/etc/lu/installgrub.findroot" 156963390b4Svikram #define LULIB "/usr/lib/lu/lulib" 157963390b4Svikram #define LULIB_PROPAGATE_FILE "lulib_propagate_file" 158963390b4Svikram #define CKSUM "/usr/bin/cksum" 159963390b4Svikram #define LU_MENU_CKSUM "/etc/lu/menu.cksum" 160963390b4Svikram #define BOOTADM "/sbin/bootadm" 161b610f78eSvikram 162b610f78eSvikram #define INSTALLGRUB "/sbin/installgrub" 163b610f78eSvikram #define STAGE1 "/boot/grub/stage1" 164b610f78eSvikram #define STAGE2 "/boot/grub/stage2" 165b610f78eSvikram 166eb2bd662Svikram typedef enum zfs_mnted { 167eb2bd662Svikram ZFS_MNT_ERROR = -1, 168eb2bd662Svikram LEGACY_MOUNTED = 1, 169eb2bd662Svikram LEGACY_ALREADY, 170eb2bd662Svikram ZFS_MOUNTED, 171eb2bd662Svikram ZFS_ALREADY 172eb2bd662Svikram } zfs_mnted_t; 173eb2bd662Svikram 1747c478bd9Sstevel@tonic-gate /* 1757c478bd9Sstevel@tonic-gate * Default file attributes 1767c478bd9Sstevel@tonic-gate */ 1777c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_MODE 0644 /* default permissions */ 1787c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_UID 0 /* user root */ 1797c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_GID 3 /* group sys */ 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /* 1827c478bd9Sstevel@tonic-gate * Menu related 1837c478bd9Sstevel@tonic-gate * menu_cmd_t and menu_cmds must be kept in sync 1847c478bd9Sstevel@tonic-gate */ 185ae115bc7Smrj char *menu_cmds[] = { 1867c478bd9Sstevel@tonic-gate "default", /* DEFAULT_CMD */ 1877c478bd9Sstevel@tonic-gate "timeout", /* TIMEOUT_CMD */ 1887c478bd9Sstevel@tonic-gate "title", /* TITLE_CMD */ 1897c478bd9Sstevel@tonic-gate "root", /* ROOT_CMD */ 1907c478bd9Sstevel@tonic-gate "kernel", /* KERNEL_CMD */ 191ae115bc7Smrj "kernel$", /* KERNEL_DOLLAR_CMD */ 1927c478bd9Sstevel@tonic-gate "module", /* MODULE_CMD */ 193ae115bc7Smrj "module$", /* MODULE_DOLLAR_CMD */ 1947c478bd9Sstevel@tonic-gate " ", /* SEP_CMD */ 1957c478bd9Sstevel@tonic-gate "#", /* COMMENT_CMD */ 196ae115bc7Smrj "chainloader", /* CHAINLOADER_CMD */ 197ae115bc7Smrj "args", /* ARGS_CMD */ 198eb2bd662Svikram "findroot", /* FINDROOT_CMD */ 19944da779fSWilliam Kucharski "bootfs", /* BOOTFS_CMD */ 2007c478bd9Sstevel@tonic-gate NULL 2017c478bd9Sstevel@tonic-gate }; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate #define OPT_ENTRY_NUM "entry" 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate /* 206eb2bd662Svikram * exec_cmd related 2077c478bd9Sstevel@tonic-gate */ 2087c478bd9Sstevel@tonic-gate typedef struct { 2097c478bd9Sstevel@tonic-gate line_t *head; 2107c478bd9Sstevel@tonic-gate line_t *tail; 2117c478bd9Sstevel@tonic-gate } filelist_t; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate #define BOOT_FILE_LIST "boot/solaris/filelist.ramdisk" 2147c478bd9Sstevel@tonic-gate #define ETC_FILE_LIST "etc/boot/solaris/filelist.ramdisk" 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate #define FILE_STAT "boot/solaris/filestat.ramdisk" 2177c478bd9Sstevel@tonic-gate #define FILE_STAT_TMP "boot/solaris/filestat.ramdisk.tmp" 2187c478bd9Sstevel@tonic-gate #define DIR_PERMS (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) 2197c478bd9Sstevel@tonic-gate #define FILE_STAT_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 2207c478bd9Sstevel@tonic-gate 2214a9df875SEnrico Perla - Sun Microsystems #define FILE_STAT_TIMESTAMP "boot/solaris/timestamp.cache" 2224a9df875SEnrico Perla - Sun Microsystems 2237c478bd9Sstevel@tonic-gate /* Globals */ 224ae115bc7Smrj int bam_verbose; 225ae115bc7Smrj int bam_force; 226eb2bd662Svikram int bam_debug; 2277c478bd9Sstevel@tonic-gate static char *prog; 2287c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd; 2297c478bd9Sstevel@tonic-gate static char *bam_root; 2307c478bd9Sstevel@tonic-gate static int bam_rootlen; 2317c478bd9Sstevel@tonic-gate static int bam_root_readonly; 23240541d5dSvikram static int bam_alt_root; 23348847494SEnrico Perla - Sun Microsystems static int bam_extend = 0; 23448847494SEnrico Perla - Sun Microsystems static int bam_purge = 0; 2357c478bd9Sstevel@tonic-gate static char *bam_subcmd; 2367c478bd9Sstevel@tonic-gate static char *bam_opt; 2377c478bd9Sstevel@tonic-gate static char **bam_argv; 238c7c0ceafSToomas Soome static char *bam_pool; 2397c478bd9Sstevel@tonic-gate static int bam_argc; 2407c478bd9Sstevel@tonic-gate static int bam_check; 2415eea6091SEnrico Perla - Sun Microsystems static int bam_saved_check; 2427c478bd9Sstevel@tonic-gate static int bam_smf_check; 2437c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1; 244e7cbe64fSgw25295 static int bam_zfs; 245c7c0ceafSToomas Soome static int bam_mbr; 2467c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/"; 247b610f78eSvikram static int bam_update_all; 248d876c67dSjg static int bam_alt_platform; 249d876c67dSjg static char *bam_platform; 250cedc7e57SEnrico Perla - Sun Microsystems static char *bam_home_env = NULL; 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate /* function prototypes */ 253986fd29aSsetje static void parse_args_internal(int, char *[]); 254986fd29aSsetje static void parse_args(int, char *argv[]); 255986fd29aSsetje static error_t bam_menu(char *, char *, int, char *[]); 256c7c0ceafSToomas Soome static error_t bam_install(char *, char *); 257986fd29aSsetje static error_t bam_archive(char *, char *); 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate static void bam_lock(void); 2607c478bd9Sstevel@tonic-gate static void bam_unlock(void); 2617c478bd9Sstevel@tonic-gate 262986fd29aSsetje static int exec_cmd(char *, filelist_t *); 263986fd29aSsetje static error_t read_globals(menu_t *, char *, char *, int); 264eb2bd662Svikram static int menu_on_bootdisk(char *os_root, char *menu_root); 265986fd29aSsetje static menu_t *menu_read(char *); 266986fd29aSsetje static error_t menu_write(char *, menu_t *); 267986fd29aSsetje static void linelist_free(line_t *); 268986fd29aSsetje static void menu_free(menu_t *); 269986fd29aSsetje static void filelist_free(filelist_t *); 270986fd29aSsetje static error_t list2file(char *, char *, char *, line_t *); 271986fd29aSsetje static error_t list_entry(menu_t *, char *, char *); 27244da779fSWilliam Kucharski static error_t list_setting(menu_t *, char *, char *); 273986fd29aSsetje static error_t delete_all_entries(menu_t *, char *, char *); 274eb2bd662Svikram static error_t update_entry(menu_t *mp, char *menu_root, char *opt); 275eb2bd662Svikram static error_t update_temp(menu_t *mp, char *dummy, char *opt); 2767c478bd9Sstevel@tonic-gate 277c7c0ceafSToomas Soome static error_t install_bootloader(void); 278986fd29aSsetje static error_t update_archive(char *, char *); 279986fd29aSsetje static error_t list_archive(char *, char *); 280986fd29aSsetje static error_t update_all(char *, char *); 281986fd29aSsetje static error_t read_list(char *, filelist_t *); 282986fd29aSsetje static error_t set_option(menu_t *, char *, char *); 283986fd29aSsetje static error_t set_kernel(menu_t *, menu_cmd_t, char *, char *, size_t); 284eb2bd662Svikram static error_t get_kernel(menu_t *, menu_cmd_t, char *, size_t); 285986fd29aSsetje static char *expand_path(const char *); 2867c478bd9Sstevel@tonic-gate 287986fd29aSsetje static long s_strtol(char *); 288986fd29aSsetje static int s_fputs(char *, FILE *); 2897c478bd9Sstevel@tonic-gate 290eb2bd662Svikram static int is_zfs(char *root); 291eb2bd662Svikram static int is_pcfs(char *root); 2927c478bd9Sstevel@tonic-gate static int is_amd64(void); 29379755401Ssetje static char *get_machine(void); 2947c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *); 295eb2bd662Svikram static char *mount_top_dataset(char *pool, zfs_mnted_t *mnted); 296eb2bd662Svikram static int umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt); 297eb2bd662Svikram static int ufs_add_to_sign_list(char *sign); 298963390b4Svikram static error_t synchronize_BE_menu(void); 2997c478bd9Sstevel@tonic-gate 30079c28b70SToomas Soome #if !defined(_OBP) 3012449e17fSsherrym static void ucode_install(); 3022449e17fSsherrym #endif 3032449e17fSsherrym 3047c478bd9Sstevel@tonic-gate /* Menu related sub commands */ 3057c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = { 306eb2bd662Svikram "set_option", OPT_ABSENT, set_option, 0, /* PUB */ 3071a97e40eSvikram "list_entry", OPT_OPTIONAL, list_entry, 1, /* PUB */ 3081a97e40eSvikram "delete_all_entries", OPT_ABSENT, delete_all_entries, 0, /* PVT */ 3091a97e40eSvikram "update_entry", OPT_REQ, update_entry, 0, /* menu */ 3101a97e40eSvikram "update_temp", OPT_OPTIONAL, update_temp, 0, /* reboot */ 311ae115bc7Smrj "upgrade", OPT_ABSENT, upgrade_menu, 0, /* menu */ 31244da779fSWilliam Kucharski "list_setting", OPT_OPTIONAL, list_setting, 1, /* menu */ 31344da779fSWilliam Kucharski "disable_hypervisor", OPT_ABSENT, cvt_to_metal, 0, /* menu */ 31444da779fSWilliam Kucharski "enable_hypervisor", OPT_ABSENT, cvt_to_hyper, 0, /* menu */ 3151a97e40eSvikram NULL, 0, NULL, 0 /* must be last */ 3167c478bd9Sstevel@tonic-gate }; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /* Archive related sub commands */ 3197c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = { 3201a97e40eSvikram "update", OPT_ABSENT, update_archive, 0, /* PUB */ 3211a97e40eSvikram "update_all", OPT_ABSENT, update_all, 0, /* PVT */ 3221a97e40eSvikram "list", OPT_OPTIONAL, list_archive, 1, /* PUB */ 3231a97e40eSvikram NULL, 0, NULL, 0 /* must be last */ 3247c478bd9Sstevel@tonic-gate }; 3257c478bd9Sstevel@tonic-gate 326c7c0ceafSToomas Soome /* Install related sub commands */ 327c7c0ceafSToomas Soome static subcmd_defn_t inst_subcmds[] = { 328c7c0ceafSToomas Soome "install_bootloader", OPT_ABSENT, install_bootloader, 0, /* PUB */ 329c7c0ceafSToomas Soome NULL, 0, NULL, 0 /* must be last */ 330c7c0ceafSToomas Soome }; 331c7c0ceafSToomas Soome 33248847494SEnrico Perla - Sun Microsystems enum dircache_copy_opt { 33348847494SEnrico Perla - Sun Microsystems FILE32 = 0, 33448847494SEnrico Perla - Sun Microsystems FILE64, 33548847494SEnrico Perla - Sun Microsystems CACHEDIR_NUM 33648847494SEnrico Perla - Sun Microsystems }; 33748847494SEnrico Perla - Sun Microsystems 33848847494SEnrico Perla - Sun Microsystems /* 33948847494SEnrico Perla - Sun Microsystems * Directory specific flags: 34048847494SEnrico Perla - Sun Microsystems * NEED_UPDATE : the specified archive needs to be updated 34148847494SEnrico Perla - Sun Microsystems * NO_MULTI : don't extend the specified archive, but recreate it 34248847494SEnrico Perla - Sun Microsystems */ 34348847494SEnrico Perla - Sun Microsystems #define NEED_UPDATE 0x00000001 34448847494SEnrico Perla - Sun Microsystems #define NO_MULTI 0x00000002 34548847494SEnrico Perla - Sun Microsystems 34648847494SEnrico Perla - Sun Microsystems #define set_dir_flag(id, f) (walk_arg.dirinfo[id].flags |= f) 34748847494SEnrico Perla - Sun Microsystems #define unset_dir_flag(id, f) (walk_arg.dirinfo[id].flags &= ~f) 34848847494SEnrico Perla - Sun Microsystems #define is_dir_flag_on(id, f) (walk_arg.dirinfo[id].flags & f ? 1 : 0) 34948847494SEnrico Perla - Sun Microsystems 35048847494SEnrico Perla - Sun Microsystems #define get_cachedir(id) (walk_arg.dirinfo[id].cdir_path) 35148847494SEnrico Perla - Sun Microsystems #define get_updatedir(id) (walk_arg.dirinfo[id].update_path) 35248847494SEnrico Perla - Sun Microsystems #define get_count(id) (walk_arg.dirinfo[id].count) 35348847494SEnrico Perla - Sun Microsystems #define has_cachedir(id) (walk_arg.dirinfo[id].has_dir) 35448847494SEnrico Perla - Sun Microsystems #define set_dir_present(id) (walk_arg.dirinfo[id].has_dir = 1) 35548847494SEnrico Perla - Sun Microsystems 35648847494SEnrico Perla - Sun Microsystems /* 35748847494SEnrico Perla - Sun Microsystems * dirinfo_t (specific cache directory information): 35848847494SEnrico Perla - Sun Microsystems * cdir_path: path to the archive cache directory 35948847494SEnrico Perla - Sun Microsystems * update_path: path to the update directory (contains the files that will be 36048847494SEnrico Perla - Sun Microsystems * used to extend the archive) 36148847494SEnrico Perla - Sun Microsystems * has_dir: the specified cache directory is active 36248847494SEnrico Perla - Sun Microsystems * count: the number of files to update 36348847494SEnrico Perla - Sun Microsystems * flags: directory specific flags 36448847494SEnrico Perla - Sun Microsystems */ 36548847494SEnrico Perla - Sun Microsystems typedef struct _dirinfo { 36648847494SEnrico Perla - Sun Microsystems char cdir_path[PATH_MAX]; 36748847494SEnrico Perla - Sun Microsystems char update_path[PATH_MAX]; 36848847494SEnrico Perla - Sun Microsystems int has_dir; 36948847494SEnrico Perla - Sun Microsystems int count; 37048847494SEnrico Perla - Sun Microsystems int flags; 37148847494SEnrico Perla - Sun Microsystems } dirinfo_t; 37248847494SEnrico Perla - Sun Microsystems 37348847494SEnrico Perla - Sun Microsystems /* 37448847494SEnrico Perla - Sun Microsystems * Update flags: 37548847494SEnrico Perla - Sun Microsystems * NEED_CACHE_DIR : cache directory is missing and needs to be created 37648847494SEnrico Perla - Sun Microsystems * IS_SPARC_TARGET : the target mountpoint is a SPARC environment 37748847494SEnrico Perla - Sun Microsystems * UPDATE_ERROR : an error occourred while traversing the list of files 37848847494SEnrico Perla - Sun Microsystems * RDONLY_FSCHK : the target filesystem is read-only 37948847494SEnrico Perla - Sun Microsystems * RAMDSK_FSCHK : the target filesystem is on a ramdisk 38048847494SEnrico Perla - Sun Microsystems */ 38148847494SEnrico Perla - Sun Microsystems #define NEED_CACHE_DIR 0x00000001 38248847494SEnrico Perla - Sun Microsystems #define IS_SPARC_TARGET 0x00000002 38348847494SEnrico Perla - Sun Microsystems #define UPDATE_ERROR 0x00000004 38448847494SEnrico Perla - Sun Microsystems #define RDONLY_FSCHK 0x00000008 3854a9df875SEnrico Perla - Sun Microsystems #define INVALIDATE_CACHE 0x00000010 38648847494SEnrico Perla - Sun Microsystems 38748847494SEnrico Perla - Sun Microsystems #define is_flag_on(flag) (walk_arg.update_flags & flag ? 1 : 0) 38848847494SEnrico Perla - Sun Microsystems #define set_flag(flag) (walk_arg.update_flags |= flag) 38948847494SEnrico Perla - Sun Microsystems #define unset_flag(flag) (walk_arg.update_flags &= ~flag) 39048847494SEnrico Perla - Sun Microsystems 39148847494SEnrico Perla - Sun Microsystems /* 39248847494SEnrico Perla - Sun Microsystems * struct walk_arg : 39348847494SEnrico Perla - Sun Microsystems * update_flags: flags related to the current updating process 39448847494SEnrico Perla - Sun Microsystems * new_nvlp/old_nvlp: new and old list of archive-files / attributes pairs 39548847494SEnrico Perla - Sun Microsystems * sparcfile: list of file paths for mkisofs -path-list (SPARC only) 39648847494SEnrico Perla - Sun Microsystems */ 3977c478bd9Sstevel@tonic-gate static struct { 39848847494SEnrico Perla - Sun Microsystems int update_flags; 3997c478bd9Sstevel@tonic-gate nvlist_t *new_nvlp; 4007c478bd9Sstevel@tonic-gate nvlist_t *old_nvlp; 40148847494SEnrico Perla - Sun Microsystems FILE *sparcfile; 40248847494SEnrico Perla - Sun Microsystems dirinfo_t dirinfo[CACHEDIR_NUM]; 4037c478bd9Sstevel@tonic-gate } walk_arg; 4047c478bd9Sstevel@tonic-gate 4059632cfadSsetje struct safefile { 40658091fd8Ssetje char *name; 40758091fd8Ssetje struct safefile *next; 40858091fd8Ssetje }; 40958091fd8Ssetje 410ae115bc7Smrj static struct safefile *safefiles = NULL; 4113b133becSGangadhar Mylapuram 4123b133becSGangadhar Mylapuram /* 4133b133becSGangadhar Mylapuram * svc:/system/filesystem/usr:default service checks for this file and 4143b133becSGangadhar Mylapuram * does a boot archive update and then reboot the system. 4153b133becSGangadhar Mylapuram */ 41658091fd8Ssetje #define NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update" 41758091fd8Ssetje 4183b133becSGangadhar Mylapuram /* 4193b133becSGangadhar Mylapuram * svc:/system/boot-archive-update:default checks for this file and 4203b133becSGangadhar Mylapuram * updates the boot archive. 4213b133becSGangadhar Mylapuram */ 4223b133becSGangadhar Mylapuram #define NEED_UPDATE_SAFE_FILE "/etc/svc/volatile/boot_archive_safefile_update" 4233b133becSGangadhar Mylapuram 42448847494SEnrico Perla - Sun Microsystems /* Thanks growisofs */ 42548847494SEnrico Perla - Sun Microsystems #define CD_BLOCK ((off64_t)2048) 42648847494SEnrico Perla - Sun Microsystems #define VOLDESC_OFF 16 42748847494SEnrico Perla - Sun Microsystems #define DVD_BLOCK (32*1024) 42848847494SEnrico Perla - Sun Microsystems #define MAX_IVDs 16 42948847494SEnrico Perla - Sun Microsystems 43048847494SEnrico Perla - Sun Microsystems struct iso_pdesc { 43148847494SEnrico Perla - Sun Microsystems unsigned char type [1]; 43248847494SEnrico Perla - Sun Microsystems unsigned char id [5]; 43348847494SEnrico Perla - Sun Microsystems unsigned char void1 [80-5-1]; 43448847494SEnrico Perla - Sun Microsystems unsigned char volume_space_size [8]; 43548847494SEnrico Perla - Sun Microsystems unsigned char void2 [2048-80-8]; 43648847494SEnrico Perla - Sun Microsystems }; 43748847494SEnrico Perla - Sun Microsystems 43848847494SEnrico Perla - Sun Microsystems /* 43948847494SEnrico Perla - Sun Microsystems * COUNT_MAX: maximum number of changed files to justify a multisession update 44048847494SEnrico Perla - Sun Microsystems * BA_SIZE_MAX: maximum size of the boot_archive to justify a multisession 44148847494SEnrico Perla - Sun Microsystems * update 44248847494SEnrico Perla - Sun Microsystems */ 44348847494SEnrico Perla - Sun Microsystems #define COUNT_MAX 50 44448847494SEnrico Perla - Sun Microsystems #define BA_SIZE_MAX (50 * 1024 * 1024) 44548847494SEnrico Perla - Sun Microsystems 44648847494SEnrico Perla - Sun Microsystems #define bam_nowrite() (bam_check || bam_smf_check) 44748847494SEnrico Perla - Sun Microsystems 44819397407SSherry Moore static int sync_menu = 1; /* whether we need to sync the BE menus */ 44919397407SSherry Moore 4507c478bd9Sstevel@tonic-gate static void 4517c478bd9Sstevel@tonic-gate usage(void) 4527c478bd9Sstevel@tonic-gate { 4537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "USAGE:\n"); 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate /* archive usage */ 456d876c67dSjg (void) fprintf(stderr, 457c365cc98SToomas Soome "\t%s update-archive [-vn] [-R altroot [-p platform]]\n", prog); 458d876c67dSjg (void) fprintf(stderr, 459c365cc98SToomas Soome "\t%s list-archive [-R altroot [-p platform]]\n", prog); 46079c28b70SToomas Soome #if defined(_OBP) 461c7c0ceafSToomas Soome (void) fprintf(stderr, 462c7c0ceafSToomas Soome "\t%s install-bootloader [-fv] [-R altroot] [-P pool]\n", prog); 463c7c0ceafSToomas Soome #else 464c7c0ceafSToomas Soome (void) fprintf(stderr, 465c7c0ceafSToomas Soome "\t%s install-bootloader [-Mfv] [-R altroot] [-P pool]\n", prog); 466c7c0ceafSToomas Soome #endif 46779c28b70SToomas Soome #if !defined(_OBP) 4687c478bd9Sstevel@tonic-gate /* x86 only */ 4697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog); 4707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog); 4717c478bd9Sstevel@tonic-gate #endif 4727c478bd9Sstevel@tonic-gate } 4737c478bd9Sstevel@tonic-gate 474cedc7e57SEnrico Perla - Sun Microsystems /* 475cedc7e57SEnrico Perla - Sun Microsystems * Best effort attempt to restore the $HOME value. 476cedc7e57SEnrico Perla - Sun Microsystems */ 477cedc7e57SEnrico Perla - Sun Microsystems static void 478cedc7e57SEnrico Perla - Sun Microsystems restore_env() 479cedc7e57SEnrico Perla - Sun Microsystems { 480cedc7e57SEnrico Perla - Sun Microsystems char home_env[PATH_MAX]; 481cedc7e57SEnrico Perla - Sun Microsystems 482cedc7e57SEnrico Perla - Sun Microsystems if (bam_home_env) { 483cedc7e57SEnrico Perla - Sun Microsystems (void) snprintf(home_env, sizeof (home_env), "HOME=%s", 484cedc7e57SEnrico Perla - Sun Microsystems bam_home_env); 485cedc7e57SEnrico Perla - Sun Microsystems (void) putenv(home_env); 486cedc7e57SEnrico Perla - Sun Microsystems } 487cedc7e57SEnrico Perla - Sun Microsystems } 488cedc7e57SEnrico Perla - Sun Microsystems 489cedc7e57SEnrico Perla - Sun Microsystems 490cedc7e57SEnrico Perla - Sun Microsystems #define SLEEP_TIME 5 491cedc7e57SEnrico Perla - Sun Microsystems #define MAX_TRIES 4 492cedc7e57SEnrico Perla - Sun Microsystems 493cedc7e57SEnrico Perla - Sun Microsystems /* 494cedc7e57SEnrico Perla - Sun Microsystems * Sanitize the environment in which bootadm will execute its sub-processes 495cedc7e57SEnrico Perla - Sun Microsystems * (ex. mkisofs). This is done to prevent those processes from attempting 496cedc7e57SEnrico Perla - Sun Microsystems * to access files (ex. .mkisofsrc) or stat paths that might be on NFS 497cedc7e57SEnrico Perla - Sun Microsystems * or, potentially, insecure. 498cedc7e57SEnrico Perla - Sun Microsystems */ 499cedc7e57SEnrico Perla - Sun Microsystems static void 500cedc7e57SEnrico Perla - Sun Microsystems sanitize_env() 501cedc7e57SEnrico Perla - Sun Microsystems { 502cedc7e57SEnrico Perla - Sun Microsystems int stry = 0; 503cedc7e57SEnrico Perla - Sun Microsystems 504cedc7e57SEnrico Perla - Sun Microsystems /* don't depend on caller umask */ 505cedc7e57SEnrico Perla - Sun Microsystems (void) umask(0022); 506cedc7e57SEnrico Perla - Sun Microsystems 507cedc7e57SEnrico Perla - Sun Microsystems /* move away from a potential unsafe current working directory */ 508cedc7e57SEnrico Perla - Sun Microsystems while (chdir("/") == -1) { 509cedc7e57SEnrico Perla - Sun Microsystems if (errno != EINTR) { 510cedc7e57SEnrico Perla - Sun Microsystems bam_print("WARNING: unable to chdir to /"); 511cedc7e57SEnrico Perla - Sun Microsystems break; 512cedc7e57SEnrico Perla - Sun Microsystems } 513cedc7e57SEnrico Perla - Sun Microsystems } 514cedc7e57SEnrico Perla - Sun Microsystems 515cedc7e57SEnrico Perla - Sun Microsystems bam_home_env = getenv("HOME"); 516cedc7e57SEnrico Perla - Sun Microsystems while (bam_home_env != NULL && putenv("HOME=/") == -1) { 517cedc7e57SEnrico Perla - Sun Microsystems if (errno == ENOMEM) { 518cedc7e57SEnrico Perla - Sun Microsystems /* retry no more than MAX_TRIES times */ 519cedc7e57SEnrico Perla - Sun Microsystems if (++stry > MAX_TRIES) { 520cedc7e57SEnrico Perla - Sun Microsystems bam_print("WARNING: unable to recover from " 521cedc7e57SEnrico Perla - Sun Microsystems "system memory pressure... aborting \n"); 522cedc7e57SEnrico Perla - Sun Microsystems bam_exit(EXIT_FAILURE); 523cedc7e57SEnrico Perla - Sun Microsystems } 524cedc7e57SEnrico Perla - Sun Microsystems /* memory is tight, try to sleep */ 525cedc7e57SEnrico Perla - Sun Microsystems bam_print("Attempting to recover from memory pressure: " 526cedc7e57SEnrico Perla - Sun Microsystems "sleeping for %d seconds\n", SLEEP_TIME * stry); 527cedc7e57SEnrico Perla - Sun Microsystems (void) sleep(SLEEP_TIME * stry); 528cedc7e57SEnrico Perla - Sun Microsystems } else { 529cedc7e57SEnrico Perla - Sun Microsystems bam_print("WARNING: unable to sanitize HOME\n"); 530cedc7e57SEnrico Perla - Sun Microsystems } 531cedc7e57SEnrico Perla - Sun Microsystems } 532cedc7e57SEnrico Perla - Sun Microsystems } 533cedc7e57SEnrico Perla - Sun Microsystems 5347c478bd9Sstevel@tonic-gate int 5357c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 5367c478bd9Sstevel@tonic-gate { 537110570ceSToomas Soome error_t ret = BAM_SUCCESS; 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 5407c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate if ((prog = strrchr(argv[0], '/')) == NULL) { 5437c478bd9Sstevel@tonic-gate prog = argv[0]; 5447c478bd9Sstevel@tonic-gate } else { 5457c478bd9Sstevel@tonic-gate prog++; 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate 548eb2bd662Svikram INJECT_ERROR1("ASSERT_ON", assert(0)) 5497c478bd9Sstevel@tonic-gate 550cedc7e57SEnrico Perla - Sun Microsystems sanitize_env(); 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate parse_args(argc, argv); 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate switch (bam_cmd) { 5557c478bd9Sstevel@tonic-gate case BAM_MENU: 5567c478bd9Sstevel@tonic-gate ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv); 5577c478bd9Sstevel@tonic-gate break; 5587c478bd9Sstevel@tonic-gate case BAM_ARCHIVE: 5597c478bd9Sstevel@tonic-gate ret = bam_archive(bam_subcmd, bam_opt); 5607c478bd9Sstevel@tonic-gate break; 561c7c0ceafSToomas Soome case BAM_INSTALL: 562c7c0ceafSToomas Soome ret = bam_install(bam_subcmd, bam_opt); 563c7c0ceafSToomas Soome break; 5647c478bd9Sstevel@tonic-gate default: 5657c478bd9Sstevel@tonic-gate usage(); 5667c478bd9Sstevel@tonic-gate bam_exit(1); 5677c478bd9Sstevel@tonic-gate } 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate if (ret != BAM_SUCCESS) 57044da779fSWilliam Kucharski bam_exit((ret == BAM_NOCHANGE) ? 2 : 1); 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate bam_unlock(); 5737c478bd9Sstevel@tonic-gate return (0); 5747c478bd9Sstevel@tonic-gate } 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate /* 5777c478bd9Sstevel@tonic-gate * Equivalence of public and internal commands: 5787c478bd9Sstevel@tonic-gate * update-archive -- -a update 5797c478bd9Sstevel@tonic-gate * list-archive -- -a list 5807c478bd9Sstevel@tonic-gate * set-menu -- -m set_option 5817c478bd9Sstevel@tonic-gate * list-menu -- -m list_entry 5827c478bd9Sstevel@tonic-gate * update-menu -- -m update_entry 583c7c0ceafSToomas Soome * install-bootloader -- -i install_bootloader 5847c478bd9Sstevel@tonic-gate */ 5857c478bd9Sstevel@tonic-gate static struct cmd_map { 5867c478bd9Sstevel@tonic-gate char *bam_cmdname; 5877c478bd9Sstevel@tonic-gate int bam_cmd; 5887c478bd9Sstevel@tonic-gate char *bam_subcmd; 5897c478bd9Sstevel@tonic-gate } cmd_map[] = { 5907c478bd9Sstevel@tonic-gate { "update-archive", BAM_ARCHIVE, "update"}, 5917c478bd9Sstevel@tonic-gate { "list-archive", BAM_ARCHIVE, "list"}, 5927c478bd9Sstevel@tonic-gate { "set-menu", BAM_MENU, "set_option"}, 5937c478bd9Sstevel@tonic-gate { "list-menu", BAM_MENU, "list_entry"}, 5947c478bd9Sstevel@tonic-gate { "update-menu", BAM_MENU, "update_entry"}, 595c7c0ceafSToomas Soome { "install-bootloader", BAM_INSTALL, "install_bootloader"}, 5967c478bd9Sstevel@tonic-gate { NULL, 0, NULL} 5977c478bd9Sstevel@tonic-gate }; 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate /* 6007c478bd9Sstevel@tonic-gate * Commands syntax published in bootadm(1M) are parsed here 6017c478bd9Sstevel@tonic-gate */ 6027c478bd9Sstevel@tonic-gate static void 6037c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[]) 6047c478bd9Sstevel@tonic-gate { 6057c478bd9Sstevel@tonic-gate struct cmd_map *cmp = cmd_map; 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate /* command conforming to the final spec */ 6087c478bd9Sstevel@tonic-gate if (argc > 1 && argv[1][0] != '-') { 6097c478bd9Sstevel@tonic-gate /* 6107c478bd9Sstevel@tonic-gate * Map commands to internal table. 6117c478bd9Sstevel@tonic-gate */ 6127c478bd9Sstevel@tonic-gate while (cmp->bam_cmdname) { 6137c478bd9Sstevel@tonic-gate if (strcmp(argv[1], cmp->bam_cmdname) == 0) { 6147c478bd9Sstevel@tonic-gate bam_cmd = cmp->bam_cmd; 6157c478bd9Sstevel@tonic-gate bam_subcmd = cmp->bam_subcmd; 6167c478bd9Sstevel@tonic-gate break; 6177c478bd9Sstevel@tonic-gate } 6187c478bd9Sstevel@tonic-gate cmp++; 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate if (cmp->bam_cmdname == NULL) { 6217c478bd9Sstevel@tonic-gate usage(); 6227c478bd9Sstevel@tonic-gate bam_exit(1); 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate argc--; 6257c478bd9Sstevel@tonic-gate argv++; 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate parse_args_internal(argc, argv); 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate /* 6327c478bd9Sstevel@tonic-gate * A combination of public and private commands are parsed here. 6337c478bd9Sstevel@tonic-gate * The internal syntax and the corresponding functionality are: 6347c478bd9Sstevel@tonic-gate * -a update -- update-archive 6357c478bd9Sstevel@tonic-gate * -a list -- list-archive 63644da779fSWilliam Kucharski * -a update-all -- (reboot to sync all mnted OS archive) 637c7c0ceafSToomas Soome * -i install_bootloader -- install-bootloader 6387c478bd9Sstevel@tonic-gate * -m update_entry -- update-menu 6397c478bd9Sstevel@tonic-gate * -m list_entry -- list-menu 6407c478bd9Sstevel@tonic-gate * -m update_temp -- (reboot -- [boot-args]) 6417c478bd9Sstevel@tonic-gate * -m delete_all_entries -- (called from install) 64244da779fSWilliam Kucharski * -m enable_hypervisor [args] -- cvt_to_hyper 64344da779fSWilliam Kucharski * -m disable_hypervisor -- cvt_to_metal 64444da779fSWilliam Kucharski * -m list_setting [entry] [value] -- list_setting 64544da779fSWilliam Kucharski * 64648847494SEnrico Perla - Sun Microsystems * A set of private flags is there too: 64748847494SEnrico Perla - Sun Microsystems * -F -- purge the cache directories and rebuild them 64848847494SEnrico Perla - Sun Microsystems * -e -- use the (faster) archive update approach (used by 64948847494SEnrico Perla - Sun Microsystems * reboot) 6507c478bd9Sstevel@tonic-gate */ 6517c478bd9Sstevel@tonic-gate static void 6527c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[]) 6537c478bd9Sstevel@tonic-gate { 6547c478bd9Sstevel@tonic-gate int c, error; 6557c478bd9Sstevel@tonic-gate extern char *optarg; 6567c478bd9Sstevel@tonic-gate extern int optind, opterr; 65779c28b70SToomas Soome #if defined(_OBP) 658c7c0ceafSToomas Soome const char *optstring = "a:d:fi:m:no:veFCR:p:P:XZ"; 659c7c0ceafSToomas Soome #else 660c7c0ceafSToomas Soome const char *optstring = "a:d:fi:m:no:veFCMR:p:P:XZ"; 661c7c0ceafSToomas Soome #endif 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate /* Suppress error message from getopt */ 6647c478bd9Sstevel@tonic-gate opterr = 0; 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate error = 0; 667c7c0ceafSToomas Soome while ((c = getopt(argc, argv, optstring)) != -1) { 6687c478bd9Sstevel@tonic-gate switch (c) { 6697c478bd9Sstevel@tonic-gate case 'a': 6707c478bd9Sstevel@tonic-gate if (bam_cmd) { 6717c478bd9Sstevel@tonic-gate error = 1; 6727c478bd9Sstevel@tonic-gate bam_error(MULT_CMDS, c); 6737c478bd9Sstevel@tonic-gate } 6747c478bd9Sstevel@tonic-gate bam_cmd = BAM_ARCHIVE; 6757c478bd9Sstevel@tonic-gate bam_subcmd = optarg; 6767c478bd9Sstevel@tonic-gate break; 6777c478bd9Sstevel@tonic-gate case 'd': 6787c478bd9Sstevel@tonic-gate if (bam_debug) { 6797c478bd9Sstevel@tonic-gate error = 1; 6807c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate bam_debug = s_strtol(optarg); 6837c478bd9Sstevel@tonic-gate break; 6847c478bd9Sstevel@tonic-gate case 'f': 6857c478bd9Sstevel@tonic-gate bam_force = 1; 6867c478bd9Sstevel@tonic-gate break; 68748847494SEnrico Perla - Sun Microsystems case 'F': 68848847494SEnrico Perla - Sun Microsystems bam_purge = 1; 68948847494SEnrico Perla - Sun Microsystems break; 690c7c0ceafSToomas Soome case 'i': 691c7c0ceafSToomas Soome if (bam_cmd) { 692c7c0ceafSToomas Soome error = 1; 693c7c0ceafSToomas Soome bam_error(MULT_CMDS, c); 694c7c0ceafSToomas Soome } 695c7c0ceafSToomas Soome bam_cmd = BAM_INSTALL; 696c7c0ceafSToomas Soome bam_subcmd = optarg; 697c7c0ceafSToomas Soome break; 6987c478bd9Sstevel@tonic-gate case 'm': 6997c478bd9Sstevel@tonic-gate if (bam_cmd) { 7007c478bd9Sstevel@tonic-gate error = 1; 7017c478bd9Sstevel@tonic-gate bam_error(MULT_CMDS, c); 7027c478bd9Sstevel@tonic-gate } 7037c478bd9Sstevel@tonic-gate bam_cmd = BAM_MENU; 7047c478bd9Sstevel@tonic-gate bam_subcmd = optarg; 7057c478bd9Sstevel@tonic-gate break; 70679c28b70SToomas Soome #if !defined(_OBP) 707c7c0ceafSToomas Soome case 'M': 708c7c0ceafSToomas Soome bam_mbr = 1; 709c7c0ceafSToomas Soome break; 710c7c0ceafSToomas Soome #endif 7117c478bd9Sstevel@tonic-gate case 'n': 7127c478bd9Sstevel@tonic-gate bam_check = 1; 7135eea6091SEnrico Perla - Sun Microsystems /* 7145eea6091SEnrico Perla - Sun Microsystems * We save the original value of bam_check. The new 7155eea6091SEnrico Perla - Sun Microsystems * approach in case of a read-only filesystem is to 7165eea6091SEnrico Perla - Sun Microsystems * behave as a check, so we need a way to restore the 7175eea6091SEnrico Perla - Sun Microsystems * original value after the evaluation of the read-only 7185eea6091SEnrico Perla - Sun Microsystems * filesystem has been done. 7195eea6091SEnrico Perla - Sun Microsystems * Even if we don't allow at the moment a check with 7205eea6091SEnrico Perla - Sun Microsystems * update_all, this approach is more robust than 7215eea6091SEnrico Perla - Sun Microsystems * simply resetting bam_check to zero. 7225eea6091SEnrico Perla - Sun Microsystems */ 7235eea6091SEnrico Perla - Sun Microsystems bam_saved_check = 1; 7247c478bd9Sstevel@tonic-gate break; 7257c478bd9Sstevel@tonic-gate case 'o': 7267c478bd9Sstevel@tonic-gate if (bam_opt) { 7277c478bd9Sstevel@tonic-gate error = 1; 7287c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 7297c478bd9Sstevel@tonic-gate } 7307c478bd9Sstevel@tonic-gate bam_opt = optarg; 7317c478bd9Sstevel@tonic-gate break; 7327c478bd9Sstevel@tonic-gate case 'v': 7337c478bd9Sstevel@tonic-gate bam_verbose = 1; 7347c478bd9Sstevel@tonic-gate break; 7358c1b6884Sszhou case 'C': 7368c1b6884Sszhou bam_smf_check = 1; 7378c1b6884Sszhou break; 738c7c0ceafSToomas Soome case 'P': 739c7c0ceafSToomas Soome if (bam_pool != NULL) { 740c7c0ceafSToomas Soome error = 1; 741c7c0ceafSToomas Soome bam_error(DUP_OPT, c); 742c7c0ceafSToomas Soome } 743c7c0ceafSToomas Soome bam_pool = optarg; 744c7c0ceafSToomas Soome break; 7457c478bd9Sstevel@tonic-gate case 'R': 7467c478bd9Sstevel@tonic-gate if (bam_root) { 7477c478bd9Sstevel@tonic-gate error = 1; 7487c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 7497c478bd9Sstevel@tonic-gate break; 7507c478bd9Sstevel@tonic-gate } else if (realpath(optarg, rootbuf) == NULL) { 7517c478bd9Sstevel@tonic-gate error = 1; 7527c478bd9Sstevel@tonic-gate bam_error(CANT_RESOLVE, optarg, 7537c478bd9Sstevel@tonic-gate strerror(errno)); 7547c478bd9Sstevel@tonic-gate break; 7557c478bd9Sstevel@tonic-gate } 75640541d5dSvikram bam_alt_root = 1; 7577c478bd9Sstevel@tonic-gate bam_root = rootbuf; 7587c478bd9Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 7597c478bd9Sstevel@tonic-gate break; 760d876c67dSjg case 'p': 761d876c67dSjg bam_alt_platform = 1; 762d876c67dSjg bam_platform = optarg; 763d876c67dSjg if ((strcmp(bam_platform, "i86pc") != 0) && 764d876c67dSjg (strcmp(bam_platform, "sun4u") != 0) && 765d876c67dSjg (strcmp(bam_platform, "sun4v") != 0)) { 766d876c67dSjg error = 1; 767d876c67dSjg bam_error(INVALID_PLAT, bam_platform); 768d876c67dSjg } 769d876c67dSjg break; 7703028dfd6SFrank Van Der Linden case 'X': 7713028dfd6SFrank Van Der Linden bam_is_hv = BAM_HV_PRESENT; 7723028dfd6SFrank Van Der Linden break; 773e7cbe64fSgw25295 case 'Z': 774e7cbe64fSgw25295 bam_zfs = 1; 775e7cbe64fSgw25295 break; 77648847494SEnrico Perla - Sun Microsystems case 'e': 77748847494SEnrico Perla - Sun Microsystems bam_extend = 1; 77848847494SEnrico Perla - Sun Microsystems break; 7797c478bd9Sstevel@tonic-gate case '?': 7807c478bd9Sstevel@tonic-gate error = 1; 7817c478bd9Sstevel@tonic-gate bam_error(BAD_OPT, optopt); 7827c478bd9Sstevel@tonic-gate break; 7837c478bd9Sstevel@tonic-gate default : 7847c478bd9Sstevel@tonic-gate error = 1; 7857c478bd9Sstevel@tonic-gate bam_error(BAD_OPT, c); 7867c478bd9Sstevel@tonic-gate break; 7877c478bd9Sstevel@tonic-gate } 7887c478bd9Sstevel@tonic-gate } 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate /* 791d876c67dSjg * An alternate platform requires an alternate root 792d876c67dSjg */ 793d876c67dSjg if (bam_alt_platform && bam_alt_root == 0) { 794d876c67dSjg usage(); 795d876c67dSjg bam_exit(0); 796d876c67dSjg } 797d876c67dSjg 798d876c67dSjg /* 7997c478bd9Sstevel@tonic-gate * A command option must be specfied 8007c478bd9Sstevel@tonic-gate */ 8017c478bd9Sstevel@tonic-gate if (!bam_cmd) { 8027c478bd9Sstevel@tonic-gate if (bam_opt && strcmp(bam_opt, "all") == 0) { 8037c478bd9Sstevel@tonic-gate usage(); 8047c478bd9Sstevel@tonic-gate bam_exit(0); 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate bam_error(NEED_CMD); 8077c478bd9Sstevel@tonic-gate error = 1; 8087c478bd9Sstevel@tonic-gate } 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate if (error) { 8117c478bd9Sstevel@tonic-gate usage(); 8127c478bd9Sstevel@tonic-gate bam_exit(1); 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate if (optind > argc) { 8167c478bd9Sstevel@tonic-gate bam_error(INT_ERROR, "parse_args"); 8177c478bd9Sstevel@tonic-gate bam_exit(1); 8187c478bd9Sstevel@tonic-gate } else if (optind < argc) { 8197c478bd9Sstevel@tonic-gate bam_argv = &argv[optind]; 8207c478bd9Sstevel@tonic-gate bam_argc = argc - optind; 8217c478bd9Sstevel@tonic-gate } 8227c478bd9Sstevel@tonic-gate 8237c478bd9Sstevel@tonic-gate /* 824c7c0ceafSToomas Soome * mbr and pool are options for install_bootloader 825c7c0ceafSToomas Soome */ 826c7c0ceafSToomas Soome if (bam_cmd != BAM_INSTALL && (bam_mbr || bam_pool != NULL)) { 827c7c0ceafSToomas Soome usage(); 828c7c0ceafSToomas Soome bam_exit(0); 829c7c0ceafSToomas Soome } 830c7c0ceafSToomas Soome 831c7c0ceafSToomas Soome /* 8327c478bd9Sstevel@tonic-gate * -n implies verbose mode 8337c478bd9Sstevel@tonic-gate */ 8347c478bd9Sstevel@tonic-gate if (bam_check) 8357c478bd9Sstevel@tonic-gate bam_verbose = 1; 8367c478bd9Sstevel@tonic-gate } 8377c478bd9Sstevel@tonic-gate 8387c478bd9Sstevel@tonic-gate static error_t 8397c478bd9Sstevel@tonic-gate check_subcmd_and_options( 8407c478bd9Sstevel@tonic-gate char *subcmd, 8417c478bd9Sstevel@tonic-gate char *opt, 8427c478bd9Sstevel@tonic-gate subcmd_defn_t *table, 8437c478bd9Sstevel@tonic-gate error_t (**fp)()) 8447c478bd9Sstevel@tonic-gate { 8457c478bd9Sstevel@tonic-gate int i; 8467c478bd9Sstevel@tonic-gate 8477c478bd9Sstevel@tonic-gate if (subcmd == NULL) { 8487c478bd9Sstevel@tonic-gate bam_error(NEED_SUBCMD); 8497c478bd9Sstevel@tonic-gate return (BAM_ERROR); 8507c478bd9Sstevel@tonic-gate } 8517c478bd9Sstevel@tonic-gate 852eb2bd662Svikram if (strcmp(subcmd, "set_option") == 0) { 853eb2bd662Svikram if (bam_argc == 0 || bam_argv == NULL || bam_argv[0] == NULL) { 854eb2bd662Svikram bam_error(MISSING_ARG); 855eb2bd662Svikram usage(); 856eb2bd662Svikram return (BAM_ERROR); 857eb2bd662Svikram } else if (bam_argc > 1 || bam_argv[1] != NULL) { 8581a97e40eSvikram bam_error(TRAILING_ARGS); 8591a97e40eSvikram usage(); 8601a97e40eSvikram return (BAM_ERROR); 8611a97e40eSvikram } 86219397407SSherry Moore } else if (strcmp(subcmd, "update_all") == 0) { 86319397407SSherry Moore /* 86419397407SSherry Moore * The only option we accept for the "update_all" 86519397407SSherry Moore * subcmd is "fastboot". 86619397407SSherry Moore */ 86719397407SSherry Moore if (bam_argc > 1 || (bam_argc == 1 && 86819397407SSherry Moore strcmp(bam_argv[0], "fastboot") != 0)) { 86919397407SSherry Moore bam_error(TRAILING_ARGS); 87019397407SSherry Moore usage(); 87119397407SSherry Moore return (BAM_ERROR); 87219397407SSherry Moore } 87319397407SSherry Moore if (bam_argc == 1) 87419397407SSherry Moore sync_menu = 0; 87544da779fSWilliam Kucharski } else if (((strcmp(subcmd, "enable_hypervisor") != 0) && 87644da779fSWilliam Kucharski (strcmp(subcmd, "list_setting") != 0)) && (bam_argc || bam_argv)) { 87744da779fSWilliam Kucharski /* 87844da779fSWilliam Kucharski * Of the remaining subcommands, only "enable_hypervisor" and 87944da779fSWilliam Kucharski * "list_setting" take trailing arguments. 88044da779fSWilliam Kucharski */ 881eb2bd662Svikram bam_error(TRAILING_ARGS); 882eb2bd662Svikram usage(); 883eb2bd662Svikram return (BAM_ERROR); 8841a97e40eSvikram } 8851a97e40eSvikram 8867c478bd9Sstevel@tonic-gate if (bam_root == NULL) { 8877c478bd9Sstevel@tonic-gate bam_root = rootbuf; 8887c478bd9Sstevel@tonic-gate bam_rootlen = 1; 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate /* verify that subcmd is valid */ 8927c478bd9Sstevel@tonic-gate for (i = 0; table[i].subcmd != NULL; i++) { 8937c478bd9Sstevel@tonic-gate if (strcmp(table[i].subcmd, subcmd) == 0) 8947c478bd9Sstevel@tonic-gate break; 8957c478bd9Sstevel@tonic-gate } 8967c478bd9Sstevel@tonic-gate 8977c478bd9Sstevel@tonic-gate if (table[i].subcmd == NULL) { 8987c478bd9Sstevel@tonic-gate bam_error(INVALID_SUBCMD, subcmd); 8997c478bd9Sstevel@tonic-gate return (BAM_ERROR); 9007c478bd9Sstevel@tonic-gate } 9017c478bd9Sstevel@tonic-gate 9021a97e40eSvikram if (table[i].unpriv == 0 && geteuid() != 0) { 9031a97e40eSvikram bam_error(MUST_BE_ROOT); 9041a97e40eSvikram return (BAM_ERROR); 9051a97e40eSvikram } 9061a97e40eSvikram 9071a97e40eSvikram /* 9081a97e40eSvikram * Currently only privileged commands need a lock 9091a97e40eSvikram */ 9101a97e40eSvikram if (table[i].unpriv == 0) 9111a97e40eSvikram bam_lock(); 9121a97e40eSvikram 9137c478bd9Sstevel@tonic-gate /* subcmd verifies that opt is appropriate */ 9147c478bd9Sstevel@tonic-gate if (table[i].option != OPT_OPTIONAL) { 9157c478bd9Sstevel@tonic-gate if ((table[i].option == OPT_REQ) ^ (opt != NULL)) { 9167c478bd9Sstevel@tonic-gate if (opt) 9177c478bd9Sstevel@tonic-gate bam_error(NO_OPT_REQ, subcmd); 9187c478bd9Sstevel@tonic-gate else 9197c478bd9Sstevel@tonic-gate bam_error(MISS_OPT, subcmd); 9207c478bd9Sstevel@tonic-gate return (BAM_ERROR); 9217c478bd9Sstevel@tonic-gate } 9227c478bd9Sstevel@tonic-gate } 9237c478bd9Sstevel@tonic-gate 9247c478bd9Sstevel@tonic-gate *fp = table[i].handler; 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 9277c478bd9Sstevel@tonic-gate } 9287c478bd9Sstevel@tonic-gate 92940541d5dSvikram /* 93040541d5dSvikram * NOTE: A single "/" is also considered a trailing slash and will 93140541d5dSvikram * be deleted. 93240541d5dSvikram */ 93340541d5dSvikram static void 93440541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize) 93540541d5dSvikram { 93640541d5dSvikram size_t dstlen; 93740541d5dSvikram 93840541d5dSvikram assert(src); 93940541d5dSvikram assert(dst); 94040541d5dSvikram 94140541d5dSvikram (void) strlcpy(dst, src, dstsize); 94240541d5dSvikram 94340541d5dSvikram dstlen = strlen(dst); 94440541d5dSvikram if (dst[dstlen - 1] == '/') { 94540541d5dSvikram dst[dstlen - 1] = '\0'; 94640541d5dSvikram } 94740541d5dSvikram } 94840541d5dSvikram 949cedc7e57SEnrico Perla - Sun Microsystems static int 950cedc7e57SEnrico Perla - Sun Microsystems is_safe_exec(char *path) 951cedc7e57SEnrico Perla - Sun Microsystems { 952cedc7e57SEnrico Perla - Sun Microsystems struct stat sb; 953cedc7e57SEnrico Perla - Sun Microsystems 954cedc7e57SEnrico Perla - Sun Microsystems if (lstat(path, &sb) != 0) { 955cedc7e57SEnrico Perla - Sun Microsystems bam_error(STAT_FAIL, path, strerror(errno)); 956cedc7e57SEnrico Perla - Sun Microsystems return (BAM_ERROR); 957cedc7e57SEnrico Perla - Sun Microsystems } 958cedc7e57SEnrico Perla - Sun Microsystems 959cedc7e57SEnrico Perla - Sun Microsystems if (!S_ISREG(sb.st_mode)) { 960cedc7e57SEnrico Perla - Sun Microsystems bam_error(PATH_EXEC_LINK, path); 961cedc7e57SEnrico Perla - Sun Microsystems return (BAM_ERROR); 962cedc7e57SEnrico Perla - Sun Microsystems } 963cedc7e57SEnrico Perla - Sun Microsystems 964cedc7e57SEnrico Perla - Sun Microsystems if (sb.st_uid != getuid()) { 965cedc7e57SEnrico Perla - Sun Microsystems bam_error(PATH_EXEC_OWNER, path, getuid()); 966cedc7e57SEnrico Perla - Sun Microsystems return (BAM_ERROR); 967cedc7e57SEnrico Perla - Sun Microsystems } 968cedc7e57SEnrico Perla - Sun Microsystems 969cedc7e57SEnrico Perla - Sun Microsystems if (sb.st_mode & S_IWOTH || sb.st_mode & S_IWGRP) { 970cedc7e57SEnrico Perla - Sun Microsystems bam_error(PATH_EXEC_PERMS, path); 971cedc7e57SEnrico Perla - Sun Microsystems return (BAM_ERROR); 972cedc7e57SEnrico Perla - Sun Microsystems } 973cedc7e57SEnrico Perla - Sun Microsystems 974cedc7e57SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 975cedc7e57SEnrico Perla - Sun Microsystems } 976cedc7e57SEnrico Perla - Sun Microsystems 9777c478bd9Sstevel@tonic-gate static error_t 97844da779fSWilliam Kucharski list_setting(menu_t *mp, char *which, char *setting) 97944da779fSWilliam Kucharski { 98044da779fSWilliam Kucharski line_t *lp; 98144da779fSWilliam Kucharski entry_t *ent; 98244da779fSWilliam Kucharski 98344da779fSWilliam Kucharski char *p = which; 98444da779fSWilliam Kucharski int entry; 98544da779fSWilliam Kucharski 98644da779fSWilliam Kucharski int found; 98744da779fSWilliam Kucharski 98844da779fSWilliam Kucharski assert(which); 98944da779fSWilliam Kucharski assert(setting); 99044da779fSWilliam Kucharski 99144da779fSWilliam Kucharski if (*which != NULL) { 99244da779fSWilliam Kucharski /* 99344da779fSWilliam Kucharski * If "which" is not a number, assume it's a setting we want 99444da779fSWilliam Kucharski * to look for and so set up the routine to look for "which" 99544da779fSWilliam Kucharski * in the default entry. 99644da779fSWilliam Kucharski */ 99744da779fSWilliam Kucharski while (*p != NULL) 99844da779fSWilliam Kucharski if (!(isdigit((int)*p++))) { 99944da779fSWilliam Kucharski setting = which; 100044da779fSWilliam Kucharski which = mp->curdefault->arg; 100144da779fSWilliam Kucharski break; 100244da779fSWilliam Kucharski } 100344da779fSWilliam Kucharski } else { 100444da779fSWilliam Kucharski which = mp->curdefault->arg; 100544da779fSWilliam Kucharski } 100644da779fSWilliam Kucharski 100744da779fSWilliam Kucharski entry = atoi(which); 100844da779fSWilliam Kucharski 100944da779fSWilliam Kucharski for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != entry)); 101044da779fSWilliam Kucharski ent = ent->next) 101144da779fSWilliam Kucharski ; 101244da779fSWilliam Kucharski 101344da779fSWilliam Kucharski if (!ent) { 101444da779fSWilliam Kucharski bam_error(NO_MATCH_ENTRY); 101544da779fSWilliam Kucharski return (BAM_ERROR); 101644da779fSWilliam Kucharski } 101744da779fSWilliam Kucharski 101844da779fSWilliam Kucharski found = (*setting == NULL); 101944da779fSWilliam Kucharski 102044da779fSWilliam Kucharski for (lp = ent->start; lp != NULL; lp = lp->next) { 102144da779fSWilliam Kucharski if ((*setting == NULL) && (lp->flags != BAM_COMMENT)) 102244da779fSWilliam Kucharski bam_print(PRINT, lp->line); 1023eac223ccSWilliam Kucharski else if (lp->cmd != NULL && strcmp(setting, lp->cmd) == 0) { 102444da779fSWilliam Kucharski bam_print(PRINT, lp->arg); 102544da779fSWilliam Kucharski found = 1; 102644da779fSWilliam Kucharski } 102744da779fSWilliam Kucharski 102844da779fSWilliam Kucharski if (lp == ent->end) 102944da779fSWilliam Kucharski break; 103044da779fSWilliam Kucharski } 103144da779fSWilliam Kucharski 103244da779fSWilliam Kucharski if (!found) { 103344da779fSWilliam Kucharski bam_error(NO_MATCH_ENTRY); 103444da779fSWilliam Kucharski return (BAM_ERROR); 103544da779fSWilliam Kucharski } 103644da779fSWilliam Kucharski 103744da779fSWilliam Kucharski return (BAM_SUCCESS); 103844da779fSWilliam Kucharski } 103944da779fSWilliam Kucharski 104044da779fSWilliam Kucharski static error_t 1041c7c0ceafSToomas Soome install_bootloader(void) 1042c7c0ceafSToomas Soome { 1043c7c0ceafSToomas Soome nvlist_t *nvl; 1044c7c0ceafSToomas Soome uint16_t flags = 0; 1045c7c0ceafSToomas Soome int found = 0; 1046c7c0ceafSToomas Soome struct extmnttab mnt; 1047c7c0ceafSToomas Soome struct stat statbuf = {0}; 1048c7c0ceafSToomas Soome be_node_list_t *be_nodes, *node; 1049c7c0ceafSToomas Soome FILE *fp; 1050c7c0ceafSToomas Soome char *root_ds = NULL; 1051110570ceSToomas Soome int ret = BAM_ERROR; 1052c7c0ceafSToomas Soome 1053c7c0ceafSToomas Soome if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 1054c7c0ceafSToomas Soome bam_error(_("out of memory\n")); 1055110570ceSToomas Soome return (ret); 1056c7c0ceafSToomas Soome } 1057c7c0ceafSToomas Soome 1058c7c0ceafSToomas Soome /* 1059c7c0ceafSToomas Soome * if bam_alt_root is set, the stage files are used from alt root. 1060c7c0ceafSToomas Soome * if pool is set, the target devices are pool devices, stage files 1061c7c0ceafSToomas Soome * are read from pool bootfs unless alt root is set. 1062c7c0ceafSToomas Soome * 1063c7c0ceafSToomas Soome * use arguments as targets, stage files are from alt or current root 1064c7c0ceafSToomas Soome * if no arguments and no pool, install on current boot pool. 1065c7c0ceafSToomas Soome */ 1066c7c0ceafSToomas Soome 1067c7c0ceafSToomas Soome if (bam_alt_root) { 1068c7c0ceafSToomas Soome if (stat(bam_root, &statbuf) != 0) { 1069c7c0ceafSToomas Soome bam_error(STAT_FAIL, bam_root, strerror(errno)); 1070c7c0ceafSToomas Soome goto done; 1071c7c0ceafSToomas Soome } 1072c7c0ceafSToomas Soome if ((fp = fopen(MNTTAB, "r")) == NULL) { 1073c7c0ceafSToomas Soome bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 1074c7c0ceafSToomas Soome goto done; 1075c7c0ceafSToomas Soome } 1076c7c0ceafSToomas Soome resetmnttab(fp); 1077c7c0ceafSToomas Soome while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 1078c7c0ceafSToomas Soome if (mnt.mnt_major == major(statbuf.st_dev) && 1079c7c0ceafSToomas Soome mnt.mnt_minor == minor(statbuf.st_dev)) { 1080c7c0ceafSToomas Soome found = 1; 1081c7c0ceafSToomas Soome root_ds = strdup(mnt.mnt_special); 1082c7c0ceafSToomas Soome break; 1083c7c0ceafSToomas Soome } 1084c7c0ceafSToomas Soome } 1085c7c0ceafSToomas Soome (void) fclose(fp); 1086c7c0ceafSToomas Soome 1087c7c0ceafSToomas Soome if (found == 0) { 1088c7c0ceafSToomas Soome bam_error(NOT_IN_MNTTAB, bam_root); 1089c7c0ceafSToomas Soome goto done; 1090c7c0ceafSToomas Soome } 1091c7c0ceafSToomas Soome if (root_ds == NULL) { 1092c7c0ceafSToomas Soome bam_error(_("out of memory\n")); 1093c7c0ceafSToomas Soome goto done; 1094c7c0ceafSToomas Soome } 1095c7c0ceafSToomas Soome 1096c7c0ceafSToomas Soome if (be_list(NULL, &be_nodes) != BE_SUCCESS) { 1097c7c0ceafSToomas Soome bam_error(_("No BE's found\n")); 1098c7c0ceafSToomas Soome goto done; 1099c7c0ceafSToomas Soome } 1100c7c0ceafSToomas Soome for (node = be_nodes; node != NULL; node = node->be_next_node) 1101c7c0ceafSToomas Soome if (strcmp(root_ds, node->be_root_ds) == 0) 1102c7c0ceafSToomas Soome break; 1103c7c0ceafSToomas Soome 1104c7c0ceafSToomas Soome if (node == NULL) 1105c7c0ceafSToomas Soome bam_error(_("BE (%s) does not exist\n"), root_ds); 1106c7c0ceafSToomas Soome 1107c7c0ceafSToomas Soome free(root_ds); 1108c7c0ceafSToomas Soome root_ds = NULL; 1109c7c0ceafSToomas Soome if (node == NULL) { 1110c7c0ceafSToomas Soome be_free_list(be_nodes); 1111c7c0ceafSToomas Soome goto done; 1112c7c0ceafSToomas Soome } 1113c7c0ceafSToomas Soome ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME, 1114c7c0ceafSToomas Soome node->be_node_name); 1115c7c0ceafSToomas Soome ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT, 1116c7c0ceafSToomas Soome node->be_root_ds); 1117c7c0ceafSToomas Soome be_free_list(be_nodes); 1118110570ceSToomas Soome if (ret != 0) { 1119c7c0ceafSToomas Soome ret = BAM_ERROR; 1120c7c0ceafSToomas Soome goto done; 1121c7c0ceafSToomas Soome } 1122c7c0ceafSToomas Soome } 1123c7c0ceafSToomas Soome 1124c7c0ceafSToomas Soome if (bam_force) 1125c7c0ceafSToomas Soome flags |= BE_INSTALLBOOT_FLAG_FORCE; 1126c7c0ceafSToomas Soome if (bam_mbr) 1127c7c0ceafSToomas Soome flags |= BE_INSTALLBOOT_FLAG_MBR; 1128c7c0ceafSToomas Soome if (bam_verbose) 1129c7c0ceafSToomas Soome flags |= BE_INSTALLBOOT_FLAG_VERBOSE; 1130c7c0ceafSToomas Soome 1131c7c0ceafSToomas Soome if (nvlist_add_uint16(nvl, BE_ATTR_INSTALL_FLAGS, flags) != 0) { 1132c7c0ceafSToomas Soome bam_error(_("out of memory\n")); 1133110570ceSToomas Soome ret = BAM_ERROR; 1134c7c0ceafSToomas Soome goto done; 1135c7c0ceafSToomas Soome } 1136c7c0ceafSToomas Soome 1137c7c0ceafSToomas Soome /* 1138c7c0ceafSToomas Soome * if altroot was set, we got be name and be root, only need 1139c7c0ceafSToomas Soome * to set pool name as target. 1140c7c0ceafSToomas Soome * if no altroot, need to find be name and root from pool. 1141c7c0ceafSToomas Soome */ 1142c7c0ceafSToomas Soome if (bam_pool != NULL) { 1143c7c0ceafSToomas Soome ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_POOL, bam_pool); 1144110570ceSToomas Soome if (ret != 0) { 1145c7c0ceafSToomas Soome ret = BAM_ERROR; 1146c7c0ceafSToomas Soome goto done; 1147c7c0ceafSToomas Soome } 1148c7c0ceafSToomas Soome if (found) { 1149c7c0ceafSToomas Soome ret = be_installboot(nvl); 1150110570ceSToomas Soome if (ret != 0) 1151c7c0ceafSToomas Soome ret = BAM_ERROR; 1152c7c0ceafSToomas Soome goto done; 1153c7c0ceafSToomas Soome } 1154c7c0ceafSToomas Soome } 1155c7c0ceafSToomas Soome 1156c7c0ceafSToomas Soome if (be_list(NULL, &be_nodes) != BE_SUCCESS) { 1157c7c0ceafSToomas Soome bam_error(_("No BE's found\n")); 1158c7c0ceafSToomas Soome ret = BAM_ERROR; 1159c7c0ceafSToomas Soome goto done; 1160c7c0ceafSToomas Soome } 1161c7c0ceafSToomas Soome 1162c7c0ceafSToomas Soome if (bam_pool != NULL) { 1163c7c0ceafSToomas Soome /* 1164c7c0ceafSToomas Soome * find active be_node in bam_pool 1165c7c0ceafSToomas Soome */ 1166c7c0ceafSToomas Soome for (node = be_nodes; node != NULL; node = node->be_next_node) { 1167c7c0ceafSToomas Soome if (strcmp(bam_pool, node->be_rpool) != 0) 1168c7c0ceafSToomas Soome continue; 1169c7c0ceafSToomas Soome if (node->be_active_on_boot) 1170c7c0ceafSToomas Soome break; 1171c7c0ceafSToomas Soome } 1172c7c0ceafSToomas Soome if (node == NULL) { 1173c7c0ceafSToomas Soome bam_error(_("No active BE in %s\n"), bam_pool); 1174c7c0ceafSToomas Soome be_free_list(be_nodes); 1175c7c0ceafSToomas Soome ret = BAM_ERROR; 1176c7c0ceafSToomas Soome goto done; 1177c7c0ceafSToomas Soome } 1178c7c0ceafSToomas Soome ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME, 1179c7c0ceafSToomas Soome node->be_node_name); 1180c7c0ceafSToomas Soome ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT, 1181c7c0ceafSToomas Soome node->be_root_ds); 1182c7c0ceafSToomas Soome be_free_list(be_nodes); 1183110570ceSToomas Soome if (ret != 0) { 1184c7c0ceafSToomas Soome ret = BAM_ERROR; 1185c7c0ceafSToomas Soome goto done; 1186c7c0ceafSToomas Soome } 1187c7c0ceafSToomas Soome ret = be_installboot(nvl); 1188110570ceSToomas Soome if (ret != 0) 1189c7c0ceafSToomas Soome ret = BAM_ERROR; 1190c7c0ceafSToomas Soome goto done; 1191c7c0ceafSToomas Soome } 1192c7c0ceafSToomas Soome 1193c7c0ceafSToomas Soome /* 1194c7c0ceafSToomas Soome * get dataset for "/" and fill up the args. 1195c7c0ceafSToomas Soome */ 1196c7c0ceafSToomas Soome if ((fp = fopen(MNTTAB, "r")) == NULL) { 1197c7c0ceafSToomas Soome bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 1198c7c0ceafSToomas Soome ret = BAM_ERROR; 1199c7c0ceafSToomas Soome be_free_list(be_nodes); 1200c7c0ceafSToomas Soome goto done; 1201c7c0ceafSToomas Soome } 1202c7c0ceafSToomas Soome resetmnttab(fp); 1203c7c0ceafSToomas Soome found = 0; 1204c7c0ceafSToomas Soome while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 1205c7c0ceafSToomas Soome if (strcmp(mnt.mnt_mountp, "/") == 0) { 1206c7c0ceafSToomas Soome found = 1; 1207c7c0ceafSToomas Soome root_ds = strdup(mnt.mnt_special); 1208c7c0ceafSToomas Soome break; 1209c7c0ceafSToomas Soome } 1210c7c0ceafSToomas Soome } 1211c7c0ceafSToomas Soome (void) fclose(fp); 1212c7c0ceafSToomas Soome 1213c7c0ceafSToomas Soome if (found == 0) { 1214c7c0ceafSToomas Soome bam_error(NOT_IN_MNTTAB, "/"); 1215c7c0ceafSToomas Soome ret = BAM_ERROR; 1216c7c0ceafSToomas Soome be_free_list(be_nodes); 1217c7c0ceafSToomas Soome goto done; 1218c7c0ceafSToomas Soome } 1219c7c0ceafSToomas Soome if (root_ds == NULL) { 1220c7c0ceafSToomas Soome bam_error(_("out of memory\n")); 1221c7c0ceafSToomas Soome ret = BAM_ERROR; 1222c7c0ceafSToomas Soome be_free_list(be_nodes); 1223c7c0ceafSToomas Soome goto done; 1224c7c0ceafSToomas Soome } 1225c7c0ceafSToomas Soome 1226c7c0ceafSToomas Soome for (node = be_nodes; node != NULL; node = node->be_next_node) { 1227c7c0ceafSToomas Soome if (strcmp(root_ds, node->be_root_ds) == 0) 1228c7c0ceafSToomas Soome break; 1229c7c0ceafSToomas Soome } 1230c7c0ceafSToomas Soome 1231c7c0ceafSToomas Soome if (node == NULL) { 1232c7c0ceafSToomas Soome bam_error(_("No such BE: %s\n"), root_ds); 1233c7c0ceafSToomas Soome free(root_ds); 1234c7c0ceafSToomas Soome be_free_list(be_nodes); 1235c7c0ceafSToomas Soome ret = BAM_ERROR; 1236c7c0ceafSToomas Soome goto done; 1237c7c0ceafSToomas Soome } 1238c7c0ceafSToomas Soome free(root_ds); 1239c7c0ceafSToomas Soome 1240c7c0ceafSToomas Soome ret = nvlist_add_string(nvl, BE_ATTR_ORIG_BE_NAME, node->be_node_name); 1241c7c0ceafSToomas Soome ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_ROOT, node->be_root_ds); 1242c7c0ceafSToomas Soome ret |= nvlist_add_string(nvl, BE_ATTR_ORIG_BE_POOL, node->be_rpool); 1243c7c0ceafSToomas Soome be_free_list(be_nodes); 1244c7c0ceafSToomas Soome 1245110570ceSToomas Soome if (ret != 0) 1246c7c0ceafSToomas Soome ret = BAM_ERROR; 1247c7c0ceafSToomas Soome else 1248c7c0ceafSToomas Soome ret = be_installboot(nvl) ? BAM_ERROR : 0; 1249c7c0ceafSToomas Soome done: 1250c7c0ceafSToomas Soome nvlist_free(nvl); 1251c7c0ceafSToomas Soome 1252c7c0ceafSToomas Soome return (ret); 1253c7c0ceafSToomas Soome } 1254c7c0ceafSToomas Soome 1255c7c0ceafSToomas Soome static error_t 1256c7c0ceafSToomas Soome bam_install(char *subcmd, char *opt) 1257c7c0ceafSToomas Soome { 1258c7c0ceafSToomas Soome error_t (*f)(void); 1259c7c0ceafSToomas Soome 1260c7c0ceafSToomas Soome /* 1261c7c0ceafSToomas Soome * Check arguments 1262c7c0ceafSToomas Soome */ 1263c7c0ceafSToomas Soome if (check_subcmd_and_options(subcmd, opt, inst_subcmds, &f) == 1264c7c0ceafSToomas Soome BAM_ERROR) 1265c7c0ceafSToomas Soome return (BAM_ERROR); 1266c7c0ceafSToomas Soome 1267c7c0ceafSToomas Soome return (f()); 1268c7c0ceafSToomas Soome } 1269c7c0ceafSToomas Soome 1270c7c0ceafSToomas Soome static error_t 12717c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[]) 12727c478bd9Sstevel@tonic-gate { 12737c478bd9Sstevel@tonic-gate error_t ret; 12747c478bd9Sstevel@tonic-gate char menu_path[PATH_MAX]; 1275eb2bd662Svikram char clean_menu_root[PATH_MAX]; 1276ae115bc7Smrj char path[PATH_MAX]; 12777c478bd9Sstevel@tonic-gate menu_t *menu; 1278eb2bd662Svikram char menu_root[PATH_MAX]; 1279b610f78eSvikram struct stat sb; 12807c478bd9Sstevel@tonic-gate error_t (*f)(menu_t *mp, char *menu_path, char *opt); 1281110570ceSToomas Soome char *special = NULL; 1282eb2bd662Svikram char *pool = NULL; 1283eb2bd662Svikram zfs_mnted_t zmnted; 1284110570ceSToomas Soome char *zmntpt = NULL; 1285eb2bd662Svikram char *osdev; 1286eb2bd662Svikram char *osroot; 1287eb2bd662Svikram const char *fcn = "bam_menu()"; 1288986fd29aSsetje 1289986fd29aSsetje /* 1290986fd29aSsetje * Menu sub-command only applies to GRUB (i.e. x86) 1291986fd29aSsetje */ 1292eb2bd662Svikram if (!is_grub(bam_alt_root ? bam_root : "/")) { 1293eb2bd662Svikram bam_error(NOT_GRUB_BOOT); 1294986fd29aSsetje return (BAM_ERROR); 1295986fd29aSsetje } 12967c478bd9Sstevel@tonic-gate 12977c478bd9Sstevel@tonic-gate /* 12987c478bd9Sstevel@tonic-gate * Check arguments 12997c478bd9Sstevel@tonic-gate */ 13007c478bd9Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f); 13017c478bd9Sstevel@tonic-gate if (ret == BAM_ERROR) { 13027c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13037c478bd9Sstevel@tonic-gate } 13047c478bd9Sstevel@tonic-gate 1305eb2bd662Svikram assert(bam_root); 1306eb2bd662Svikram 1307eb2bd662Svikram (void) strlcpy(menu_root, bam_root, sizeof (menu_root)); 1308eb2bd662Svikram osdev = osroot = NULL; 1309eb2bd662Svikram 1310eb2bd662Svikram if (strcmp(subcmd, "update_entry") == 0) { 1311eb2bd662Svikram assert(opt); 1312eb2bd662Svikram 1313eb2bd662Svikram osdev = strtok(opt, ","); 1314eb2bd662Svikram assert(osdev); 1315eb2bd662Svikram osroot = strtok(NULL, ","); 1316eb2bd662Svikram if (osroot) { 1317eb2bd662Svikram /* fixup bam_root so that it points at osroot */ 1318eb2bd662Svikram if (realpath(osroot, rootbuf) == NULL) { 1319eb2bd662Svikram bam_error(CANT_RESOLVE, osroot, 1320eb2bd662Svikram strerror(errno)); 1321eb2bd662Svikram return (BAM_ERROR); 1322eb2bd662Svikram } 1323eb2bd662Svikram bam_alt_root = 1; 1324eb2bd662Svikram bam_root = rootbuf; 1325eb2bd662Svikram bam_rootlen = strlen(rootbuf); 1326eb2bd662Svikram } 1327eb2bd662Svikram } 132840541d5dSvikram 132940541d5dSvikram /* 1330eb2bd662Svikram * We support menu on PCFS (under certain conditions), but 1331eb2bd662Svikram * not the OS root 133240541d5dSvikram */ 1333eb2bd662Svikram if (is_pcfs(bam_root)) { 1334eb2bd662Svikram bam_error(PCFS_ROOT_NOTSUP, bam_root); 1335eb2bd662Svikram return (BAM_ERROR); 1336ae115bc7Smrj } 1337ae115bc7Smrj 1338eb2bd662Svikram if (stat(menu_root, &sb) == -1) { 133940541d5dSvikram bam_error(CANNOT_LOCATE_GRUB_MENU); 134040541d5dSvikram return (BAM_ERROR); 134140541d5dSvikram } 134240541d5dSvikram 1343eb2bd662Svikram BAM_DPRINTF((D_MENU_ROOT, fcn, menu_root)); 1344e7cbe64fSgw25295 1345eb2bd662Svikram /* 1346eb2bd662Svikram * We no longer use the GRUB slice file. If it exists, then 1347eb2bd662Svikram * the user is doing something that is unsupported (such as 1348eb2bd662Svikram * standard upgrading an old Live Upgrade BE). If that 1349eb2bd662Svikram * happens, mimic existing behavior i.e. pretend that it is 1350eb2bd662Svikram * not a BE. Emit a warning though. 1351eb2bd662Svikram */ 1352eb2bd662Svikram if (bam_alt_root) { 1353eb2bd662Svikram (void) snprintf(path, sizeof (path), "%s%s", bam_root, 1354eb2bd662Svikram GRUB_slice); 1355eb2bd662Svikram } else { 1356eb2bd662Svikram (void) snprintf(path, sizeof (path), "%s", GRUB_slice); 1357e7cbe64fSgw25295 } 1358e7cbe64fSgw25295 135937eb779cSVikram Hegde if (bam_verbose && stat(path, &sb) == 0) 1360eb2bd662Svikram bam_error(GRUB_SLICE_FILE_EXISTS, path); 1361eb2bd662Svikram 1362eb2bd662Svikram if (is_zfs(menu_root)) { 1363eb2bd662Svikram assert(strcmp(menu_root, bam_root) == 0); 1364eb2bd662Svikram special = get_special(menu_root); 1365eb2bd662Svikram INJECT_ERROR1("Z_MENU_GET_SPECIAL", special = NULL); 1366eb2bd662Svikram if (special == NULL) { 1367eb2bd662Svikram bam_error(CANT_FIND_SPECIAL, menu_root); 1368eb2bd662Svikram return (BAM_ERROR); 1369eb2bd662Svikram } 1370eb2bd662Svikram pool = strtok(special, "/"); 1371eb2bd662Svikram INJECT_ERROR1("Z_MENU_GET_POOL", pool = NULL); 1372eb2bd662Svikram if (pool == NULL) { 1373eb2bd662Svikram free(special); 1374eb2bd662Svikram bam_error(CANT_FIND_POOL, menu_root); 1375eb2bd662Svikram return (BAM_ERROR); 1376eb2bd662Svikram } 1377eb2bd662Svikram BAM_DPRINTF((D_Z_MENU_GET_POOL_FROM_SPECIAL, fcn, pool)); 1378eb2bd662Svikram 1379eb2bd662Svikram zmntpt = mount_top_dataset(pool, &zmnted); 1380eb2bd662Svikram INJECT_ERROR1("Z_MENU_MOUNT_TOP_DATASET", zmntpt = NULL); 1381eb2bd662Svikram if (zmntpt == NULL) { 1382eb2bd662Svikram bam_error(CANT_MOUNT_POOL_DATASET, pool); 1383eb2bd662Svikram free(special); 1384eb2bd662Svikram return (BAM_ERROR); 1385eb2bd662Svikram } 1386eb2bd662Svikram BAM_DPRINTF((D_Z_GET_MENU_MOUNT_TOP_DATASET, fcn, zmntpt)); 1387eb2bd662Svikram 1388eb2bd662Svikram (void) strlcpy(menu_root, zmntpt, sizeof (menu_root)); 1389eb2bd662Svikram BAM_DPRINTF((D_Z_GET_MENU_MENU_ROOT, fcn, menu_root)); 1390eb2bd662Svikram } 1391eb2bd662Svikram 1392eb2bd662Svikram elide_trailing_slash(menu_root, clean_menu_root, 1393eb2bd662Svikram sizeof (clean_menu_root)); 1394eb2bd662Svikram 1395eb2bd662Svikram BAM_DPRINTF((D_CLEAN_MENU_ROOT, fcn, clean_menu_root)); 1396eb2bd662Svikram 1397eb2bd662Svikram (void) strlcpy(menu_path, clean_menu_root, sizeof (menu_path)); 139840541d5dSvikram (void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path)); 139940541d5dSvikram 1400eb2bd662Svikram BAM_DPRINTF((D_MENU_PATH, fcn, menu_path)); 1401eb2bd662Svikram 140240541d5dSvikram /* 1403eb2bd662Svikram * If listing the menu, display the menu location 140440541d5dSvikram */ 140544da779fSWilliam Kucharski if (strcmp(subcmd, "list_entry") == 0) 1406eb2bd662Svikram bam_print(GRUB_MENU_PATH, menu_path); 140744da779fSWilliam Kucharski 140844da779fSWilliam Kucharski if ((menu = menu_read(menu_path)) == NULL) { 140944da779fSWilliam Kucharski bam_error(CANNOT_LOCATE_GRUB_MENU_FILE, menu_path); 141044da779fSWilliam Kucharski free(special); 141144da779fSWilliam Kucharski 141244da779fSWilliam Kucharski return (BAM_ERROR); 141340541d5dSvikram } 14147c478bd9Sstevel@tonic-gate 14157c478bd9Sstevel@tonic-gate /* 1416eb2bd662Svikram * We already checked the following case in 1417eb2bd662Svikram * check_subcmd_and_suboptions() above. Complete the 1418eb2bd662Svikram * final step now. 14197c478bd9Sstevel@tonic-gate */ 14207c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "set_option") == 0) { 1421eb2bd662Svikram assert(largc == 1 && largv[0] && largv[1] == NULL); 14227c478bd9Sstevel@tonic-gate opt = largv[0]; 142344da779fSWilliam Kucharski } else if ((strcmp(subcmd, "enable_hypervisor") != 0) && 142444da779fSWilliam Kucharski (strcmp(subcmd, "list_setting") != 0)) { 1425eb2bd662Svikram assert(largc == 0 && largv == NULL); 14267c478bd9Sstevel@tonic-gate } 14277c478bd9Sstevel@tonic-gate 1428eb2bd662Svikram ret = get_boot_cap(bam_root); 1429eb2bd662Svikram if (ret != BAM_SUCCESS) { 1430eb2bd662Svikram BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn)); 1431eb2bd662Svikram goto out; 1432eb2bd662Svikram } 1433ae115bc7Smrj 14347c478bd9Sstevel@tonic-gate /* 14357c478bd9Sstevel@tonic-gate * Once the sub-cmd handler has run 14367c478bd9Sstevel@tonic-gate * only the line field is guaranteed to have valid values 14377c478bd9Sstevel@tonic-gate */ 143844da779fSWilliam Kucharski if (strcmp(subcmd, "update_entry") == 0) { 1439eb2bd662Svikram ret = f(menu, menu_root, osdev); 144044da779fSWilliam Kucharski } else if (strcmp(subcmd, "upgrade") == 0) { 1441eb2bd662Svikram ret = f(menu, bam_root, menu_root); 144244da779fSWilliam Kucharski } else if (strcmp(subcmd, "list_entry") == 0) { 14437c478bd9Sstevel@tonic-gate ret = f(menu, menu_path, opt); 144444da779fSWilliam Kucharski } else if (strcmp(subcmd, "list_setting") == 0) { 144544da779fSWilliam Kucharski ret = f(menu, ((largc > 0) ? largv[0] : ""), 144644da779fSWilliam Kucharski ((largc > 1) ? largv[1] : "")); 144744da779fSWilliam Kucharski } else if (strcmp(subcmd, "disable_hypervisor") == 0) { 144844da779fSWilliam Kucharski if (is_sparc()) { 144944da779fSWilliam Kucharski bam_error(NO_SPARC, subcmd); 145044da779fSWilliam Kucharski ret = BAM_ERROR; 145144da779fSWilliam Kucharski } else { 145244da779fSWilliam Kucharski ret = f(menu, bam_root, NULL); 145344da779fSWilliam Kucharski } 145444da779fSWilliam Kucharski } else if (strcmp(subcmd, "enable_hypervisor") == 0) { 145544da779fSWilliam Kucharski if (is_sparc()) { 145644da779fSWilliam Kucharski bam_error(NO_SPARC, subcmd); 145744da779fSWilliam Kucharski ret = BAM_ERROR; 145844da779fSWilliam Kucharski } else { 145944da779fSWilliam Kucharski char *extra_args = NULL; 146044da779fSWilliam Kucharski 146144da779fSWilliam Kucharski /* 146244da779fSWilliam Kucharski * Compress all arguments passed in the largv[] array 146344da779fSWilliam Kucharski * into one string that can then be appended to the 146444da779fSWilliam Kucharski * end of the kernel$ string the routine to enable the 146544da779fSWilliam Kucharski * hypervisor will build. 146644da779fSWilliam Kucharski * 146744da779fSWilliam Kucharski * This allows the caller to supply arbitrary unparsed 146844da779fSWilliam Kucharski * arguments, such as dom0 memory settings or APIC 146944da779fSWilliam Kucharski * options. 147044da779fSWilliam Kucharski * 147144da779fSWilliam Kucharski * This concatenation will be done without ANY syntax 147244da779fSWilliam Kucharski * checking whatsoever, so it's the responsibility of 147344da779fSWilliam Kucharski * the caller to make sure the arguments are valid and 147444da779fSWilliam Kucharski * do not duplicate arguments the conversion routines 147544da779fSWilliam Kucharski * may create. 147644da779fSWilliam Kucharski */ 147744da779fSWilliam Kucharski if (largc > 0) { 147844da779fSWilliam Kucharski int extra_len, i; 147944da779fSWilliam Kucharski 148044da779fSWilliam Kucharski for (extra_len = 0, i = 0; i < largc; i++) 148144da779fSWilliam Kucharski extra_len += strlen(largv[i]); 148244da779fSWilliam Kucharski 148344da779fSWilliam Kucharski /* 148444da779fSWilliam Kucharski * Allocate space for argument strings, 148544da779fSWilliam Kucharski * intervening spaces and terminating NULL. 148644da779fSWilliam Kucharski */ 148744da779fSWilliam Kucharski extra_args = alloca(extra_len + largc); 148844da779fSWilliam Kucharski 148944da779fSWilliam Kucharski (void) strcpy(extra_args, largv[0]); 149044da779fSWilliam Kucharski 149144da779fSWilliam Kucharski for (i = 1; i < largc; i++) { 149244da779fSWilliam Kucharski (void) strcat(extra_args, " "); 149344da779fSWilliam Kucharski (void) strcat(extra_args, largv[i]); 149444da779fSWilliam Kucharski } 149544da779fSWilliam Kucharski } 149644da779fSWilliam Kucharski 149744da779fSWilliam Kucharski ret = f(menu, bam_root, extra_args); 149844da779fSWilliam Kucharski } 149944da779fSWilliam Kucharski } else 1500eb2bd662Svikram ret = f(menu, NULL, opt); 1501eb2bd662Svikram 15027c478bd9Sstevel@tonic-gate if (ret == BAM_WRITE) { 1503eb2bd662Svikram BAM_DPRINTF((D_WRITING_MENU_ROOT, fcn, clean_menu_root)); 1504eb2bd662Svikram ret = menu_write(clean_menu_root, menu); 15057c478bd9Sstevel@tonic-gate } 15067c478bd9Sstevel@tonic-gate 1507eb2bd662Svikram out: 1508eb2bd662Svikram INJECT_ERROR1("POOL_SET", pool = "/pooldata"); 1509eb2bd662Svikram assert((is_zfs(menu_root)) ^ (pool == NULL)); 1510eb2bd662Svikram if (pool) { 1511eb2bd662Svikram (void) umount_top_dataset(pool, zmnted, zmntpt); 1512eb2bd662Svikram free(special); 1513eb2bd662Svikram } 15147c478bd9Sstevel@tonic-gate menu_free(menu); 15157c478bd9Sstevel@tonic-gate return (ret); 15167c478bd9Sstevel@tonic-gate } 15177c478bd9Sstevel@tonic-gate 15187c478bd9Sstevel@tonic-gate 15197c478bd9Sstevel@tonic-gate static error_t 15207c478bd9Sstevel@tonic-gate bam_archive( 15217c478bd9Sstevel@tonic-gate char *subcmd, 15227c478bd9Sstevel@tonic-gate char *opt) 15237c478bd9Sstevel@tonic-gate { 15247c478bd9Sstevel@tonic-gate error_t ret; 15257c478bd9Sstevel@tonic-gate error_t (*f)(char *root, char *opt); 1526eb2bd662Svikram const char *fcn = "bam_archive()"; 15277c478bd9Sstevel@tonic-gate 15287c478bd9Sstevel@tonic-gate /* 15298c1b6884Sszhou * Add trailing / for archive subcommands 15308c1b6884Sszhou */ 15318c1b6884Sszhou if (rootbuf[strlen(rootbuf) - 1] != '/') 15328c1b6884Sszhou (void) strcat(rootbuf, "/"); 15338c1b6884Sszhou bam_rootlen = strlen(rootbuf); 15348c1b6884Sszhou 15358c1b6884Sszhou /* 15367c478bd9Sstevel@tonic-gate * Check arguments 15377c478bd9Sstevel@tonic-gate */ 15387c478bd9Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f); 15397c478bd9Sstevel@tonic-gate if (ret != BAM_SUCCESS) { 15407c478bd9Sstevel@tonic-gate return (BAM_ERROR); 15417c478bd9Sstevel@tonic-gate } 15427c478bd9Sstevel@tonic-gate 1543eb2bd662Svikram ret = get_boot_cap(rootbuf); 1544eb2bd662Svikram if (ret != BAM_SUCCESS) { 1545eb2bd662Svikram BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn)); 1546ae115bc7Smrj return (ret); 1547eb2bd662Svikram } 1548ae115bc7Smrj 15497c478bd9Sstevel@tonic-gate /* 15507c478bd9Sstevel@tonic-gate * Check archive not supported with update_all 15517c478bd9Sstevel@tonic-gate * since it is awkward to display out-of-sync 15527c478bd9Sstevel@tonic-gate * information for each BE. 15537c478bd9Sstevel@tonic-gate */ 15547c478bd9Sstevel@tonic-gate if (bam_check && strcmp(subcmd, "update_all") == 0) { 15557c478bd9Sstevel@tonic-gate bam_error(CHECK_NOT_SUPPORTED, subcmd); 15567c478bd9Sstevel@tonic-gate return (BAM_ERROR); 15577c478bd9Sstevel@tonic-gate } 15587c478bd9Sstevel@tonic-gate 1559b610f78eSvikram if (strcmp(subcmd, "update_all") == 0) 1560b610f78eSvikram bam_update_all = 1; 1561b610f78eSvikram 156279c28b70SToomas Soome #if !defined(_OBP) 15632449e17fSsherrym ucode_install(bam_root); 15642449e17fSsherrym #endif 15652449e17fSsherrym 1566b610f78eSvikram ret = f(bam_root, opt); 1567b610f78eSvikram 1568b610f78eSvikram bam_update_all = 0; 1569b610f78eSvikram 1570b610f78eSvikram return (ret); 15717c478bd9Sstevel@tonic-gate } 15727c478bd9Sstevel@tonic-gate 15737c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 1574ae115bc7Smrj void 15757c478bd9Sstevel@tonic-gate bam_error(char *format, ...) 15767c478bd9Sstevel@tonic-gate { 15777c478bd9Sstevel@tonic-gate va_list ap; 15787c478bd9Sstevel@tonic-gate 15797c478bd9Sstevel@tonic-gate va_start(ap, format); 15807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", prog); 15817c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, ap); 15827c478bd9Sstevel@tonic-gate va_end(ap); 15837c478bd9Sstevel@tonic-gate } 15847c478bd9Sstevel@tonic-gate 15857c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 1586eb2bd662Svikram void 1587eb2bd662Svikram bam_derror(char *format, ...) 1588eb2bd662Svikram { 1589eb2bd662Svikram va_list ap; 1590eb2bd662Svikram 1591eb2bd662Svikram assert(bam_debug); 1592eb2bd662Svikram 1593eb2bd662Svikram va_start(ap, format); 1594eb2bd662Svikram (void) fprintf(stderr, "DEBUG: "); 1595eb2bd662Svikram (void) vfprintf(stderr, format, ap); 1596eb2bd662Svikram va_end(ap); 1597eb2bd662Svikram } 1598eb2bd662Svikram 1599eb2bd662Svikram /*PRINTFLIKE1*/ 1600eb2bd662Svikram void 16017c478bd9Sstevel@tonic-gate bam_print(char *format, ...) 16027c478bd9Sstevel@tonic-gate { 16037c478bd9Sstevel@tonic-gate va_list ap; 16047c478bd9Sstevel@tonic-gate 16057c478bd9Sstevel@tonic-gate va_start(ap, format); 16067c478bd9Sstevel@tonic-gate (void) vfprintf(stdout, format, ap); 16077c478bd9Sstevel@tonic-gate va_end(ap); 16087c478bd9Sstevel@tonic-gate } 16097c478bd9Sstevel@tonic-gate 1610ae115bc7Smrj /*PRINTFLIKE1*/ 1611ae115bc7Smrj void 1612ae115bc7Smrj bam_print_stderr(char *format, ...) 1613ae115bc7Smrj { 1614ae115bc7Smrj va_list ap; 1615ae115bc7Smrj 1616ae115bc7Smrj va_start(ap, format); 1617ae115bc7Smrj (void) vfprintf(stderr, format, ap); 1618ae115bc7Smrj va_end(ap); 1619ae115bc7Smrj } 1620ae115bc7Smrj 162166b6aef6SWilliam Kucharski void 16227c478bd9Sstevel@tonic-gate bam_exit(int excode) 16237c478bd9Sstevel@tonic-gate { 1624cedc7e57SEnrico Perla - Sun Microsystems restore_env(); 16257c478bd9Sstevel@tonic-gate bam_unlock(); 16267c478bd9Sstevel@tonic-gate exit(excode); 16277c478bd9Sstevel@tonic-gate } 16287c478bd9Sstevel@tonic-gate 16297c478bd9Sstevel@tonic-gate static void 16307c478bd9Sstevel@tonic-gate bam_lock(void) 16317c478bd9Sstevel@tonic-gate { 16327c478bd9Sstevel@tonic-gate struct flock lock; 16337c478bd9Sstevel@tonic-gate pid_t pid; 16347c478bd9Sstevel@tonic-gate 16357c478bd9Sstevel@tonic-gate bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS); 16367c478bd9Sstevel@tonic-gate if (bam_lock_fd < 0) { 16377c478bd9Sstevel@tonic-gate /* 16387c478bd9Sstevel@tonic-gate * We may be invoked early in boot for archive verification. 16397c478bd9Sstevel@tonic-gate * In this case, root is readonly and /var/run may not exist. 16407c478bd9Sstevel@tonic-gate * Proceed without the lock 16417c478bd9Sstevel@tonic-gate */ 16427c478bd9Sstevel@tonic-gate if (errno == EROFS || errno == ENOENT) { 16437c478bd9Sstevel@tonic-gate bam_root_readonly = 1; 16447c478bd9Sstevel@tonic-gate return; 16457c478bd9Sstevel@tonic-gate } 16467c478bd9Sstevel@tonic-gate 16477c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno)); 16487c478bd9Sstevel@tonic-gate bam_exit(1); 16497c478bd9Sstevel@tonic-gate } 16507c478bd9Sstevel@tonic-gate 16517c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 16527c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 16537c478bd9Sstevel@tonic-gate lock.l_start = 0; 16547c478bd9Sstevel@tonic-gate lock.l_len = 0; 16557c478bd9Sstevel@tonic-gate 16567c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) { 16577c478bd9Sstevel@tonic-gate if (errno != EACCES && errno != EAGAIN) { 16587c478bd9Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 16597c478bd9Sstevel@tonic-gate (void) close(bam_lock_fd); 16607c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 16617c478bd9Sstevel@tonic-gate bam_exit(1); 16627c478bd9Sstevel@tonic-gate } 16637c478bd9Sstevel@tonic-gate pid = 0; 16647c478bd9Sstevel@tonic-gate (void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0); 16657c478bd9Sstevel@tonic-gate bam_print(FILE_LOCKED, pid); 16667c478bd9Sstevel@tonic-gate 16677c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 16687c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 16697c478bd9Sstevel@tonic-gate lock.l_start = 0; 16707c478bd9Sstevel@tonic-gate lock.l_len = 0; 16717c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) { 16727c478bd9Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 16737c478bd9Sstevel@tonic-gate (void) close(bam_lock_fd); 16747c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 16757c478bd9Sstevel@tonic-gate bam_exit(1); 16767c478bd9Sstevel@tonic-gate } 16777c478bd9Sstevel@tonic-gate } 16787c478bd9Sstevel@tonic-gate 16797c478bd9Sstevel@tonic-gate /* We own the lock now */ 16807c478bd9Sstevel@tonic-gate pid = getpid(); 16817c478bd9Sstevel@tonic-gate (void) write(bam_lock_fd, &pid, sizeof (pid)); 16827c478bd9Sstevel@tonic-gate } 16837c478bd9Sstevel@tonic-gate 16847c478bd9Sstevel@tonic-gate static void 16857c478bd9Sstevel@tonic-gate bam_unlock(void) 16867c478bd9Sstevel@tonic-gate { 16877c478bd9Sstevel@tonic-gate struct flock unlock; 16887c478bd9Sstevel@tonic-gate 16897c478bd9Sstevel@tonic-gate /* 16907c478bd9Sstevel@tonic-gate * NOP if we don't hold the lock 16917c478bd9Sstevel@tonic-gate */ 16927c478bd9Sstevel@tonic-gate if (bam_lock_fd < 0) { 16937c478bd9Sstevel@tonic-gate return; 16947c478bd9Sstevel@tonic-gate } 16957c478bd9Sstevel@tonic-gate 16967c478bd9Sstevel@tonic-gate unlock.l_type = F_UNLCK; 16977c478bd9Sstevel@tonic-gate unlock.l_whence = SEEK_SET; 16987c478bd9Sstevel@tonic-gate unlock.l_start = 0; 16997c478bd9Sstevel@tonic-gate unlock.l_len = 0; 17007c478bd9Sstevel@tonic-gate 17017c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) { 17027c478bd9Sstevel@tonic-gate bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 17037c478bd9Sstevel@tonic-gate } 17047c478bd9Sstevel@tonic-gate 17057c478bd9Sstevel@tonic-gate if (close(bam_lock_fd) == -1) { 17067c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno)); 17077c478bd9Sstevel@tonic-gate } 17087c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 17097c478bd9Sstevel@tonic-gate } 17107c478bd9Sstevel@tonic-gate 17117c478bd9Sstevel@tonic-gate static error_t 17127c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt) 17137c478bd9Sstevel@tonic-gate { 17147c478bd9Sstevel@tonic-gate filelist_t flist; 17157c478bd9Sstevel@tonic-gate filelist_t *flistp = &flist; 17167c478bd9Sstevel@tonic-gate line_t *lp; 17177c478bd9Sstevel@tonic-gate 17187c478bd9Sstevel@tonic-gate assert(root); 17197c478bd9Sstevel@tonic-gate assert(opt == NULL); 17207c478bd9Sstevel@tonic-gate 17217c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 17227c478bd9Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 17237c478bd9Sstevel@tonic-gate return (BAM_ERROR); 17247c478bd9Sstevel@tonic-gate } 17257c478bd9Sstevel@tonic-gate assert(flistp->head && flistp->tail); 17267c478bd9Sstevel@tonic-gate 17277c478bd9Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 17287c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 17297c478bd9Sstevel@tonic-gate } 17307c478bd9Sstevel@tonic-gate 17317c478bd9Sstevel@tonic-gate filelist_free(flistp); 17327c478bd9Sstevel@tonic-gate 17337c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 17347c478bd9Sstevel@tonic-gate } 17357c478bd9Sstevel@tonic-gate 17367c478bd9Sstevel@tonic-gate /* 17377c478bd9Sstevel@tonic-gate * This routine writes a list of lines to a file. 17387c478bd9Sstevel@tonic-gate * The list is *not* freed 17397c478bd9Sstevel@tonic-gate */ 17407c478bd9Sstevel@tonic-gate static error_t 17417c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start) 17427c478bd9Sstevel@tonic-gate { 17437c478bd9Sstevel@tonic-gate char tmpfile[PATH_MAX]; 17447c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 17457c478bd9Sstevel@tonic-gate FILE *fp; 17467c478bd9Sstevel@tonic-gate int ret; 17477c478bd9Sstevel@tonic-gate struct stat sb; 17487c478bd9Sstevel@tonic-gate mode_t mode; 17497c478bd9Sstevel@tonic-gate uid_t root_uid; 17507c478bd9Sstevel@tonic-gate gid_t sys_gid; 17517c478bd9Sstevel@tonic-gate struct passwd *pw; 17527c478bd9Sstevel@tonic-gate struct group *gp; 1753eb2bd662Svikram const char *fcn = "list2file()"; 17547c478bd9Sstevel@tonic-gate 17557c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, final); 17567c478bd9Sstevel@tonic-gate 17577c478bd9Sstevel@tonic-gate if (start == NULL) { 1758eb2bd662Svikram /* Empty GRUB menu */ 17597c478bd9Sstevel@tonic-gate if (stat(path, &sb) != -1) { 17607c478bd9Sstevel@tonic-gate bam_print(UNLINK_EMPTY, path); 17617c478bd9Sstevel@tonic-gate if (unlink(path) != 0) { 17627c478bd9Sstevel@tonic-gate bam_error(UNLINK_FAIL, path, strerror(errno)); 17637c478bd9Sstevel@tonic-gate return (BAM_ERROR); 17647c478bd9Sstevel@tonic-gate } else { 17657c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 17667c478bd9Sstevel@tonic-gate } 17677c478bd9Sstevel@tonic-gate } 1768eb2bd662Svikram return (BAM_SUCCESS); 17697c478bd9Sstevel@tonic-gate } 17707c478bd9Sstevel@tonic-gate 17717c478bd9Sstevel@tonic-gate /* 17727c478bd9Sstevel@tonic-gate * Preserve attributes of existing file if possible, 17737c478bd9Sstevel@tonic-gate * otherwise ask the system for uid/gid of root/sys. 17747c478bd9Sstevel@tonic-gate * If all fails, fall back on hard-coded defaults. 17757c478bd9Sstevel@tonic-gate */ 17767c478bd9Sstevel@tonic-gate if (stat(path, &sb) != -1) { 17777c478bd9Sstevel@tonic-gate mode = sb.st_mode; 17787c478bd9Sstevel@tonic-gate root_uid = sb.st_uid; 17797c478bd9Sstevel@tonic-gate sys_gid = sb.st_gid; 17807c478bd9Sstevel@tonic-gate } else { 17817c478bd9Sstevel@tonic-gate mode = DEFAULT_DEV_MODE; 17827c478bd9Sstevel@tonic-gate if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) { 17837c478bd9Sstevel@tonic-gate root_uid = pw->pw_uid; 17847c478bd9Sstevel@tonic-gate } else { 17857c478bd9Sstevel@tonic-gate bam_error(CANT_FIND_USER, 17867c478bd9Sstevel@tonic-gate DEFAULT_DEV_USER, DEFAULT_DEV_UID); 17877c478bd9Sstevel@tonic-gate root_uid = (uid_t)DEFAULT_DEV_UID; 17887c478bd9Sstevel@tonic-gate } 17897c478bd9Sstevel@tonic-gate if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) { 17907c478bd9Sstevel@tonic-gate sys_gid = gp->gr_gid; 17917c478bd9Sstevel@tonic-gate } else { 17927c478bd9Sstevel@tonic-gate bam_error(CANT_FIND_GROUP, 17937c478bd9Sstevel@tonic-gate DEFAULT_DEV_GROUP, DEFAULT_DEV_GID); 17947c478bd9Sstevel@tonic-gate sys_gid = (gid_t)DEFAULT_DEV_GID; 17957c478bd9Sstevel@tonic-gate } 17967c478bd9Sstevel@tonic-gate } 17977c478bd9Sstevel@tonic-gate 17987c478bd9Sstevel@tonic-gate (void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp); 17997c478bd9Sstevel@tonic-gate 18007c478bd9Sstevel@tonic-gate /* Truncate tmpfile first */ 18017c478bd9Sstevel@tonic-gate fp = fopen(tmpfile, "w"); 18027c478bd9Sstevel@tonic-gate if (fp == NULL) { 18037c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 18047c478bd9Sstevel@tonic-gate return (BAM_ERROR); 18057c478bd9Sstevel@tonic-gate } 1806963390b4Svikram ret = fclose(fp); 1807963390b4Svikram INJECT_ERROR1("LIST2FILE_TRUNC_FCLOSE", ret = EOF); 1808963390b4Svikram if (ret == EOF) { 18097c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 18107c478bd9Sstevel@tonic-gate return (BAM_ERROR); 18117c478bd9Sstevel@tonic-gate } 18127c478bd9Sstevel@tonic-gate 18137c478bd9Sstevel@tonic-gate /* Now open it in append mode */ 18147c478bd9Sstevel@tonic-gate fp = fopen(tmpfile, "a"); 18157c478bd9Sstevel@tonic-gate if (fp == NULL) { 18167c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 18177c478bd9Sstevel@tonic-gate return (BAM_ERROR); 18187c478bd9Sstevel@tonic-gate } 18197c478bd9Sstevel@tonic-gate 18207c478bd9Sstevel@tonic-gate for (; start; start = start->next) { 1821963390b4Svikram ret = s_fputs(start->line, fp); 1822963390b4Svikram INJECT_ERROR1("LIST2FILE_FPUTS", ret = EOF); 1823963390b4Svikram if (ret == EOF) { 18247c478bd9Sstevel@tonic-gate bam_error(WRITE_FAIL, tmpfile, strerror(errno)); 18257c478bd9Sstevel@tonic-gate (void) fclose(fp); 18267c478bd9Sstevel@tonic-gate return (BAM_ERROR); 18277c478bd9Sstevel@tonic-gate } 18287c478bd9Sstevel@tonic-gate } 18297c478bd9Sstevel@tonic-gate 1830963390b4Svikram ret = fclose(fp); 1831963390b4Svikram INJECT_ERROR1("LIST2FILE_APPEND_FCLOSE", ret = EOF); 1832963390b4Svikram if (ret == EOF) { 18337c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 18347c478bd9Sstevel@tonic-gate return (BAM_ERROR); 18357c478bd9Sstevel@tonic-gate } 18367c478bd9Sstevel@tonic-gate 18377c478bd9Sstevel@tonic-gate /* 1838de531be4Sjg * Set up desired attributes. Ignore failures on filesystems 1839de531be4Sjg * not supporting these operations - pcfs reports unsupported 1840de531be4Sjg * operations as EINVAL. 18417c478bd9Sstevel@tonic-gate */ 18427c478bd9Sstevel@tonic-gate ret = chmod(tmpfile, mode); 1843de531be4Sjg if (ret == -1 && 1844de531be4Sjg errno != EINVAL && errno != ENOTSUP) { 18457c478bd9Sstevel@tonic-gate bam_error(CHMOD_FAIL, tmpfile, strerror(errno)); 18467c478bd9Sstevel@tonic-gate return (BAM_ERROR); 18477c478bd9Sstevel@tonic-gate } 18487c478bd9Sstevel@tonic-gate 18497c478bd9Sstevel@tonic-gate ret = chown(tmpfile, root_uid, sys_gid); 1850de531be4Sjg if (ret == -1 && 1851de531be4Sjg errno != EINVAL && errno != ENOTSUP) { 18527c478bd9Sstevel@tonic-gate bam_error(CHOWN_FAIL, tmpfile, strerror(errno)); 18537c478bd9Sstevel@tonic-gate return (BAM_ERROR); 18547c478bd9Sstevel@tonic-gate } 18557c478bd9Sstevel@tonic-gate 18567c478bd9Sstevel@tonic-gate /* 18577c478bd9Sstevel@tonic-gate * Do an atomic rename 18587c478bd9Sstevel@tonic-gate */ 18597c478bd9Sstevel@tonic-gate ret = rename(tmpfile, path); 1860963390b4Svikram INJECT_ERROR1("LIST2FILE_RENAME", ret = -1); 18617c478bd9Sstevel@tonic-gate if (ret != 0) { 18627c478bd9Sstevel@tonic-gate bam_error(RENAME_FAIL, path, strerror(errno)); 18637c478bd9Sstevel@tonic-gate return (BAM_ERROR); 18647c478bd9Sstevel@tonic-gate } 18657c478bd9Sstevel@tonic-gate 1866eb2bd662Svikram BAM_DPRINTF((D_WROTE_FILE, fcn, path)); 18677c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 18687c478bd9Sstevel@tonic-gate } 18697c478bd9Sstevel@tonic-gate 18707c478bd9Sstevel@tonic-gate /* 187148847494SEnrico Perla - Sun Microsystems * Checks if the path specified (without the file name at the end) exists 187248847494SEnrico Perla - Sun Microsystems * and creates it if not. If the path exists and is not a directory, an attempt 187348847494SEnrico Perla - Sun Microsystems * to unlink is made. 18747c478bd9Sstevel@tonic-gate */ 187548847494SEnrico Perla - Sun Microsystems static int 187648847494SEnrico Perla - Sun Microsystems setup_path(char *path) 187748847494SEnrico Perla - Sun Microsystems { 187848847494SEnrico Perla - Sun Microsystems char *p; 187948847494SEnrico Perla - Sun Microsystems int ret; 188048847494SEnrico Perla - Sun Microsystems struct stat sb; 188148847494SEnrico Perla - Sun Microsystems 188248847494SEnrico Perla - Sun Microsystems p = strrchr(path, '/'); 188348847494SEnrico Perla - Sun Microsystems if (p != NULL) { 188448847494SEnrico Perla - Sun Microsystems *p = '\0'; 188548847494SEnrico Perla - Sun Microsystems if (stat(path, &sb) != 0 || !(S_ISDIR(sb.st_mode))) { 188648847494SEnrico Perla - Sun Microsystems /* best effort attempt, mkdirp will catch the error */ 188748847494SEnrico Perla - Sun Microsystems (void) unlink(path); 188848847494SEnrico Perla - Sun Microsystems if (bam_verbose) 188948847494SEnrico Perla - Sun Microsystems bam_print(NEED_DIRPATH, path); 189048847494SEnrico Perla - Sun Microsystems ret = mkdirp(path, DIR_PERMS); 189148847494SEnrico Perla - Sun Microsystems if (ret == -1) { 189248847494SEnrico Perla - Sun Microsystems bam_error(MKDIR_FAILED, path, strerror(errno)); 189348847494SEnrico Perla - Sun Microsystems *p = '/'; 189448847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 189548847494SEnrico Perla - Sun Microsystems } 189648847494SEnrico Perla - Sun Microsystems } 189748847494SEnrico Perla - Sun Microsystems *p = '/'; 189848847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 189948847494SEnrico Perla - Sun Microsystems } 190048847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 190148847494SEnrico Perla - Sun Microsystems } 190248847494SEnrico Perla - Sun Microsystems 190348847494SEnrico Perla - Sun Microsystems typedef union { 190448847494SEnrico Perla - Sun Microsystems gzFile gzfile; 190548847494SEnrico Perla - Sun Microsystems int fdfile; 190648847494SEnrico Perla - Sun Microsystems } outfile; 190748847494SEnrico Perla - Sun Microsystems 190848847494SEnrico Perla - Sun Microsystems typedef struct { 190948847494SEnrico Perla - Sun Microsystems char path[PATH_MAX]; 191048847494SEnrico Perla - Sun Microsystems outfile out; 191148847494SEnrico Perla - Sun Microsystems } cachefile; 191248847494SEnrico Perla - Sun Microsystems 191348847494SEnrico Perla - Sun Microsystems static int 191448847494SEnrico Perla - Sun Microsystems setup_file(char *base, const char *path, cachefile *cf) 191548847494SEnrico Perla - Sun Microsystems { 191648847494SEnrico Perla - Sun Microsystems int ret; 191748847494SEnrico Perla - Sun Microsystems char *strip; 191848847494SEnrico Perla - Sun Microsystems 191948847494SEnrico Perla - Sun Microsystems /* init gzfile or fdfile in case we fail before opening */ 192048847494SEnrico Perla - Sun Microsystems if (bam_direct == BAM_DIRECT_DBOOT) 192148847494SEnrico Perla - Sun Microsystems cf->out.gzfile = NULL; 192248847494SEnrico Perla - Sun Microsystems else 192348847494SEnrico Perla - Sun Microsystems cf->out.fdfile = -1; 192448847494SEnrico Perla - Sun Microsystems 192548847494SEnrico Perla - Sun Microsystems /* strip the trailing altroot path */ 192648847494SEnrico Perla - Sun Microsystems strip = (char *)path + strlen(rootbuf); 192748847494SEnrico Perla - Sun Microsystems 192848847494SEnrico Perla - Sun Microsystems ret = snprintf(cf->path, sizeof (cf->path), "%s/%s", base, strip); 192948847494SEnrico Perla - Sun Microsystems if (ret >= sizeof (cf->path)) { 193048847494SEnrico Perla - Sun Microsystems bam_error(PATH_TOO_LONG, rootbuf); 193148847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 193248847494SEnrico Perla - Sun Microsystems } 193348847494SEnrico Perla - Sun Microsystems 193448847494SEnrico Perla - Sun Microsystems /* Check if path is present in the archive cache directory */ 193548847494SEnrico Perla - Sun Microsystems if (setup_path(cf->path) == BAM_ERROR) 193648847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 193748847494SEnrico Perla - Sun Microsystems 193848847494SEnrico Perla - Sun Microsystems if (bam_direct == BAM_DIRECT_DBOOT) { 193948847494SEnrico Perla - Sun Microsystems if ((cf->out.gzfile = gzopen(cf->path, "wb")) == NULL) { 1940cedc7e57SEnrico Perla - Sun Microsystems bam_error(OPEN_FAIL, cf->path, strerror(errno)); 194148847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 194248847494SEnrico Perla - Sun Microsystems } 194348847494SEnrico Perla - Sun Microsystems (void) gzsetparams(cf->out.gzfile, Z_BEST_SPEED, 194448847494SEnrico Perla - Sun Microsystems Z_DEFAULT_STRATEGY); 194548847494SEnrico Perla - Sun Microsystems } else { 194648847494SEnrico Perla - Sun Microsystems if ((cf->out.fdfile = open(cf->path, O_WRONLY | O_CREAT, 0644)) 194748847494SEnrico Perla - Sun Microsystems == -1) { 194848847494SEnrico Perla - Sun Microsystems bam_error(OPEN_FAIL, cf->path, strerror(errno)); 194948847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 195048847494SEnrico Perla - Sun Microsystems } 195148847494SEnrico Perla - Sun Microsystems } 195248847494SEnrico Perla - Sun Microsystems 195348847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 195448847494SEnrico Perla - Sun Microsystems } 195548847494SEnrico Perla - Sun Microsystems 195648847494SEnrico Perla - Sun Microsystems static int 195748847494SEnrico Perla - Sun Microsystems cache_write(cachefile cf, char *buf, int size) 195848847494SEnrico Perla - Sun Microsystems { 195948847494SEnrico Perla - Sun Microsystems int err; 196048847494SEnrico Perla - Sun Microsystems 196148847494SEnrico Perla - Sun Microsystems if (bam_direct == BAM_DIRECT_DBOOT) { 196248847494SEnrico Perla - Sun Microsystems if (gzwrite(cf.out.gzfile, buf, size) < 1) { 196348847494SEnrico Perla - Sun Microsystems bam_error(GZ_WRITE_FAIL, gzerror(cf.out.gzfile, &err)); 1964cedc7e57SEnrico Perla - Sun Microsystems if (err == Z_ERRNO && bam_verbose) { 196548847494SEnrico Perla - Sun Microsystems bam_error(WRITE_FAIL, cf.path, strerror(errno)); 1966cedc7e57SEnrico Perla - Sun Microsystems } 196748847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 196848847494SEnrico Perla - Sun Microsystems } 196948847494SEnrico Perla - Sun Microsystems } else { 197048847494SEnrico Perla - Sun Microsystems if (write(cf.out.fdfile, buf, size) < 1) { 197148847494SEnrico Perla - Sun Microsystems bam_error(WRITE_FAIL, cf.path, strerror(errno)); 197248847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 197348847494SEnrico Perla - Sun Microsystems } 197448847494SEnrico Perla - Sun Microsystems } 197548847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 197648847494SEnrico Perla - Sun Microsystems } 197748847494SEnrico Perla - Sun Microsystems 197848847494SEnrico Perla - Sun Microsystems static int 197948847494SEnrico Perla - Sun Microsystems cache_close(cachefile cf) 198048847494SEnrico Perla - Sun Microsystems { 198148847494SEnrico Perla - Sun Microsystems int ret; 198248847494SEnrico Perla - Sun Microsystems 198348847494SEnrico Perla - Sun Microsystems if (bam_direct == BAM_DIRECT_DBOOT) { 198448847494SEnrico Perla - Sun Microsystems if (cf.out.gzfile) { 198548847494SEnrico Perla - Sun Microsystems ret = gzclose(cf.out.gzfile); 1986cedc7e57SEnrico Perla - Sun Microsystems if (ret != Z_OK) { 198748847494SEnrico Perla - Sun Microsystems bam_error(CLOSE_FAIL, cf.path, strerror(errno)); 198848847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 198948847494SEnrico Perla - Sun Microsystems } 1990cedc7e57SEnrico Perla - Sun Microsystems } 199148847494SEnrico Perla - Sun Microsystems } else { 199248847494SEnrico Perla - Sun Microsystems if (cf.out.fdfile != -1) { 199348847494SEnrico Perla - Sun Microsystems ret = close(cf.out.fdfile); 199448847494SEnrico Perla - Sun Microsystems if (ret != 0) { 199548847494SEnrico Perla - Sun Microsystems bam_error(CLOSE_FAIL, cf.path, strerror(errno)); 199648847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 199748847494SEnrico Perla - Sun Microsystems } 199848847494SEnrico Perla - Sun Microsystems } 199948847494SEnrico Perla - Sun Microsystems } 200048847494SEnrico Perla - Sun Microsystems 200148847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 200248847494SEnrico Perla - Sun Microsystems } 200348847494SEnrico Perla - Sun Microsystems 200448847494SEnrico Perla - Sun Microsystems static int 200548847494SEnrico Perla - Sun Microsystems dircache_updatefile(const char *path, int what) 200648847494SEnrico Perla - Sun Microsystems { 200748847494SEnrico Perla - Sun Microsystems int ret, exitcode; 200848847494SEnrico Perla - Sun Microsystems char buf[4096 * 4]; 200948847494SEnrico Perla - Sun Microsystems FILE *infile; 201048847494SEnrico Perla - Sun Microsystems cachefile outfile, outupdt; 201148847494SEnrico Perla - Sun Microsystems 2012cedc7e57SEnrico Perla - Sun Microsystems if (bam_nowrite()) { 2013cedc7e57SEnrico Perla - Sun Microsystems set_dir_flag(what, NEED_UPDATE); 2014cedc7e57SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 2015cedc7e57SEnrico Perla - Sun Microsystems } 2016cedc7e57SEnrico Perla - Sun Microsystems 2017cedc7e57SEnrico Perla - Sun Microsystems if (!has_cachedir(what)) 201848847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 201948847494SEnrico Perla - Sun Microsystems 202048847494SEnrico Perla - Sun Microsystems if ((infile = fopen(path, "rb")) == NULL) { 202148847494SEnrico Perla - Sun Microsystems bam_error(OPEN_FAIL, path, strerror(errno)); 202248847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 202348847494SEnrico Perla - Sun Microsystems } 202448847494SEnrico Perla - Sun Microsystems 202548847494SEnrico Perla - Sun Microsystems ret = setup_file(get_cachedir(what), path, &outfile); 202648847494SEnrico Perla - Sun Microsystems if (ret == BAM_ERROR) { 202748847494SEnrico Perla - Sun Microsystems exitcode = BAM_ERROR; 202848847494SEnrico Perla - Sun Microsystems goto out; 202948847494SEnrico Perla - Sun Microsystems } 203048847494SEnrico Perla - Sun Microsystems if (!is_dir_flag_on(what, NO_MULTI)) { 203148847494SEnrico Perla - Sun Microsystems ret = setup_file(get_updatedir(what), path, &outupdt); 203248847494SEnrico Perla - Sun Microsystems if (ret == BAM_ERROR) 203348847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NO_MULTI); 203448847494SEnrico Perla - Sun Microsystems } 203548847494SEnrico Perla - Sun Microsystems 203648847494SEnrico Perla - Sun Microsystems while ((ret = fread(buf, 1, sizeof (buf), infile)) > 0) { 203748847494SEnrico Perla - Sun Microsystems if (cache_write(outfile, buf, ret) == BAM_ERROR) { 203848847494SEnrico Perla - Sun Microsystems exitcode = BAM_ERROR; 203948847494SEnrico Perla - Sun Microsystems goto out; 204048847494SEnrico Perla - Sun Microsystems } 204148847494SEnrico Perla - Sun Microsystems if (!is_dir_flag_on(what, NO_MULTI)) 204248847494SEnrico Perla - Sun Microsystems if (cache_write(outupdt, buf, ret) == BAM_ERROR) 204348847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NO_MULTI); 204448847494SEnrico Perla - Sun Microsystems } 204548847494SEnrico Perla - Sun Microsystems 204648847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NEED_UPDATE); 204748847494SEnrico Perla - Sun Microsystems get_count(what)++; 2048cedc7e57SEnrico Perla - Sun Microsystems if (get_count(what) > COUNT_MAX) 2049cedc7e57SEnrico Perla - Sun Microsystems set_dir_flag(what, NO_MULTI); 205048847494SEnrico Perla - Sun Microsystems exitcode = BAM_SUCCESS; 205148847494SEnrico Perla - Sun Microsystems out: 205248847494SEnrico Perla - Sun Microsystems (void) fclose(infile); 205348847494SEnrico Perla - Sun Microsystems if (cache_close(outfile) == BAM_ERROR) 205448847494SEnrico Perla - Sun Microsystems exitcode = BAM_ERROR; 205548847494SEnrico Perla - Sun Microsystems if (!is_dir_flag_on(what, NO_MULTI) && 205648847494SEnrico Perla - Sun Microsystems cache_close(outupdt) == BAM_ERROR) 205748847494SEnrico Perla - Sun Microsystems exitcode = BAM_ERROR; 205848847494SEnrico Perla - Sun Microsystems if (exitcode == BAM_ERROR) 205948847494SEnrico Perla - Sun Microsystems set_flag(UPDATE_ERROR); 206048847494SEnrico Perla - Sun Microsystems return (exitcode); 206148847494SEnrico Perla - Sun Microsystems } 206248847494SEnrico Perla - Sun Microsystems 206348847494SEnrico Perla - Sun Microsystems static int 206448847494SEnrico Perla - Sun Microsystems dircache_updatedir(const char *path, int what, int updt) 206548847494SEnrico Perla - Sun Microsystems { 206648847494SEnrico Perla - Sun Microsystems int ret; 206748847494SEnrico Perla - Sun Microsystems char dpath[PATH_MAX]; 206848847494SEnrico Perla - Sun Microsystems char *strip; 206948847494SEnrico Perla - Sun Microsystems struct stat sb; 207048847494SEnrico Perla - Sun Microsystems 207148847494SEnrico Perla - Sun Microsystems strip = (char *)path + strlen(rootbuf); 207248847494SEnrico Perla - Sun Microsystems 207348847494SEnrico Perla - Sun Microsystems ret = snprintf(dpath, sizeof (dpath), "%s/%s", updt ? 207448847494SEnrico Perla - Sun Microsystems get_updatedir(what) : get_cachedir(what), strip); 207548847494SEnrico Perla - Sun Microsystems 207648847494SEnrico Perla - Sun Microsystems if (ret >= sizeof (dpath)) { 207748847494SEnrico Perla - Sun Microsystems bam_error(PATH_TOO_LONG, rootbuf); 207848847494SEnrico Perla - Sun Microsystems set_flag(UPDATE_ERROR); 207948847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 208048847494SEnrico Perla - Sun Microsystems } 208148847494SEnrico Perla - Sun Microsystems 208248847494SEnrico Perla - Sun Microsystems if (stat(dpath, &sb) == 0 && S_ISDIR(sb.st_mode)) 208348847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 208448847494SEnrico Perla - Sun Microsystems 208548847494SEnrico Perla - Sun Microsystems if (updt) { 208648847494SEnrico Perla - Sun Microsystems if (!is_dir_flag_on(what, NO_MULTI)) 208748847494SEnrico Perla - Sun Microsystems if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1) 208848847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NO_MULTI); 208948847494SEnrico Perla - Sun Microsystems } else { 209048847494SEnrico Perla - Sun Microsystems if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1) { 209148847494SEnrico Perla - Sun Microsystems set_flag(UPDATE_ERROR); 209248847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 209348847494SEnrico Perla - Sun Microsystems } 209448847494SEnrico Perla - Sun Microsystems } 209548847494SEnrico Perla - Sun Microsystems 209648847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NEED_UPDATE); 209748847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 209848847494SEnrico Perla - Sun Microsystems } 209948847494SEnrico Perla - Sun Microsystems 210048847494SEnrico Perla - Sun Microsystems #define DO_CACHE_DIR 0 210148847494SEnrico Perla - Sun Microsystems #define DO_UPDATE_DIR 1 210248847494SEnrico Perla - Sun Microsystems 210348847494SEnrico Perla - Sun Microsystems #if defined(_LP64) || defined(_LONGLONG_TYPE) 210448847494SEnrico Perla - Sun Microsystems typedef Elf64_Ehdr _elfhdr; 210548847494SEnrico Perla - Sun Microsystems #else 210648847494SEnrico Perla - Sun Microsystems typedef Elf32_Ehdr _elfhdr; 210748847494SEnrico Perla - Sun Microsystems #endif 210848847494SEnrico Perla - Sun Microsystems 210948847494SEnrico Perla - Sun Microsystems /* 211048847494SEnrico Perla - Sun Microsystems * This routine updates the contents of the cache directory 211148847494SEnrico Perla - Sun Microsystems */ 211248847494SEnrico Perla - Sun Microsystems static int 211348847494SEnrico Perla - Sun Microsystems update_dircache(const char *path, int flags) 211448847494SEnrico Perla - Sun Microsystems { 211548847494SEnrico Perla - Sun Microsystems int rc = BAM_SUCCESS; 211648847494SEnrico Perla - Sun Microsystems 211748847494SEnrico Perla - Sun Microsystems switch (flags) { 211848847494SEnrico Perla - Sun Microsystems case FTW_F: 211948847494SEnrico Perla - Sun Microsystems { 212048847494SEnrico Perla - Sun Microsystems int fd; 212148847494SEnrico Perla - Sun Microsystems _elfhdr elf; 212248847494SEnrico Perla - Sun Microsystems 212348847494SEnrico Perla - Sun Microsystems if ((fd = open(path, O_RDONLY)) < 0) { 212448847494SEnrico Perla - Sun Microsystems bam_error(OPEN_FAIL, path, strerror(errno)); 212548847494SEnrico Perla - Sun Microsystems set_flag(UPDATE_ERROR); 212648847494SEnrico Perla - Sun Microsystems rc = BAM_ERROR; 212748847494SEnrico Perla - Sun Microsystems break; 212848847494SEnrico Perla - Sun Microsystems } 212948847494SEnrico Perla - Sun Microsystems 213048847494SEnrico Perla - Sun Microsystems /* 213148847494SEnrico Perla - Sun Microsystems * libelf and gelf would be a cleaner and easier way to handle 213248847494SEnrico Perla - Sun Microsystems * this, but libelf fails compilation if _ILP32 is defined && 213348847494SEnrico Perla - Sun Microsystems * _FILE_OFFSET_BITS is != 32 ... 213448847494SEnrico Perla - Sun Microsystems */ 213548847494SEnrico Perla - Sun Microsystems if (read(fd, (void *)&elf, sizeof (_elfhdr)) < 0) { 213648847494SEnrico Perla - Sun Microsystems bam_error(READ_FAIL, path, strerror(errno)); 213748847494SEnrico Perla - Sun Microsystems set_flag(UPDATE_ERROR); 213848847494SEnrico Perla - Sun Microsystems (void) close(fd); 213948847494SEnrico Perla - Sun Microsystems rc = BAM_ERROR; 214048847494SEnrico Perla - Sun Microsystems break; 214148847494SEnrico Perla - Sun Microsystems } 214248847494SEnrico Perla - Sun Microsystems (void) close(fd); 214348847494SEnrico Perla - Sun Microsystems 214448847494SEnrico Perla - Sun Microsystems /* 214548847494SEnrico Perla - Sun Microsystems * If the file is not an executable and is not inside an amd64 214648847494SEnrico Perla - Sun Microsystems * directory, we copy it in both the cache directories, 214748847494SEnrico Perla - Sun Microsystems * otherwise, we only copy it inside the 64-bit one. 214848847494SEnrico Perla - Sun Microsystems */ 214948847494SEnrico Perla - Sun Microsystems if (memcmp(elf.e_ident, ELFMAG, 4) != 0) { 215048847494SEnrico Perla - Sun Microsystems if (strstr(path, "/amd64")) { 215148847494SEnrico Perla - Sun Microsystems rc = dircache_updatefile(path, FILE64); 215248847494SEnrico Perla - Sun Microsystems } else { 215348847494SEnrico Perla - Sun Microsystems rc = dircache_updatefile(path, FILE32); 2154cedc7e57SEnrico Perla - Sun Microsystems if (rc == BAM_SUCCESS) 2155cedc7e57SEnrico Perla - Sun Microsystems rc = dircache_updatefile(path, FILE64); 215648847494SEnrico Perla - Sun Microsystems } 215748847494SEnrico Perla - Sun Microsystems } else { 215848847494SEnrico Perla - Sun Microsystems /* 215948847494SEnrico Perla - Sun Microsystems * Based on the ELF class we copy the file in the 32-bit 216048847494SEnrico Perla - Sun Microsystems * or the 64-bit cache directory. 216148847494SEnrico Perla - Sun Microsystems */ 2162cedc7e57SEnrico Perla - Sun Microsystems if (elf.e_ident[EI_CLASS] == ELFCLASS32) { 216348847494SEnrico Perla - Sun Microsystems rc = dircache_updatefile(path, FILE32); 2164cedc7e57SEnrico Perla - Sun Microsystems } else if (elf.e_ident[EI_CLASS] == ELFCLASS64) { 216548847494SEnrico Perla - Sun Microsystems rc = dircache_updatefile(path, FILE64); 2166cedc7e57SEnrico Perla - Sun Microsystems } else { 216748847494SEnrico Perla - Sun Microsystems bam_print(NO3264ELF, path); 216848847494SEnrico Perla - Sun Microsystems /* paranoid */ 216948847494SEnrico Perla - Sun Microsystems rc = dircache_updatefile(path, FILE32); 217048847494SEnrico Perla - Sun Microsystems if (rc == BAM_SUCCESS) 217148847494SEnrico Perla - Sun Microsystems rc = dircache_updatefile(path, FILE64); 217248847494SEnrico Perla - Sun Microsystems } 217348847494SEnrico Perla - Sun Microsystems } 217448847494SEnrico Perla - Sun Microsystems break; 217548847494SEnrico Perla - Sun Microsystems } 217648847494SEnrico Perla - Sun Microsystems case FTW_D: 217748847494SEnrico Perla - Sun Microsystems if (strstr(path, "/amd64") == NULL) { 217848847494SEnrico Perla - Sun Microsystems rc = dircache_updatedir(path, FILE32, DO_UPDATE_DIR); 217948847494SEnrico Perla - Sun Microsystems if (rc == BAM_SUCCESS) 218048847494SEnrico Perla - Sun Microsystems rc = dircache_updatedir(path, FILE32, 218148847494SEnrico Perla - Sun Microsystems DO_CACHE_DIR); 218248847494SEnrico Perla - Sun Microsystems } else { 218348847494SEnrico Perla - Sun Microsystems if (has_cachedir(FILE64)) { 218448847494SEnrico Perla - Sun Microsystems rc = dircache_updatedir(path, FILE64, 218548847494SEnrico Perla - Sun Microsystems DO_UPDATE_DIR); 218648847494SEnrico Perla - Sun Microsystems if (rc == BAM_SUCCESS) 218748847494SEnrico Perla - Sun Microsystems rc = dircache_updatedir(path, FILE64, 218848847494SEnrico Perla - Sun Microsystems DO_CACHE_DIR); 218948847494SEnrico Perla - Sun Microsystems } 219048847494SEnrico Perla - Sun Microsystems } 219148847494SEnrico Perla - Sun Microsystems break; 219248847494SEnrico Perla - Sun Microsystems default: 219348847494SEnrico Perla - Sun Microsystems rc = BAM_ERROR; 219448847494SEnrico Perla - Sun Microsystems break; 219548847494SEnrico Perla - Sun Microsystems } 219648847494SEnrico Perla - Sun Microsystems 219748847494SEnrico Perla - Sun Microsystems return (rc); 219848847494SEnrico Perla - Sun Microsystems } 219948847494SEnrico Perla - Sun Microsystems 22007c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 22017c478bd9Sstevel@tonic-gate static int 22027c478bd9Sstevel@tonic-gate cmpstat( 22037c478bd9Sstevel@tonic-gate const char *file, 220448847494SEnrico Perla - Sun Microsystems const struct stat *st, 22057c478bd9Sstevel@tonic-gate int flags, 22067c478bd9Sstevel@tonic-gate struct FTW *ftw) 22077c478bd9Sstevel@tonic-gate { 22087c478bd9Sstevel@tonic-gate uint_t sz; 22097c478bd9Sstevel@tonic-gate uint64_t *value; 22107c478bd9Sstevel@tonic-gate uint64_t filestat[2]; 221189c3ee43SGangadhar Mylapuram int error, ret, status; 22127c478bd9Sstevel@tonic-gate 221358091fd8Ssetje struct safefile *safefilep; 221458091fd8Ssetje FILE *fp; 221548847494SEnrico Perla - Sun Microsystems struct stat sb; 221689c3ee43SGangadhar Mylapuram regex_t re; 221758091fd8Ssetje 22187c478bd9Sstevel@tonic-gate /* 221948847494SEnrico Perla - Sun Microsystems * On SPARC we create/update links too. 22207c478bd9Sstevel@tonic-gate */ 222148847494SEnrico Perla - Sun Microsystems if (flags != FTW_F && flags != FTW_D && (flags == FTW_SL && 222248847494SEnrico Perla - Sun Microsystems !is_flag_on(IS_SPARC_TARGET))) 222348847494SEnrico Perla - Sun Microsystems return (0); 222448847494SEnrico Perla - Sun Microsystems 222548847494SEnrico Perla - Sun Microsystems /* 222648847494SEnrico Perla - Sun Microsystems * Ignore broken links 222748847494SEnrico Perla - Sun Microsystems */ 222848847494SEnrico Perla - Sun Microsystems if (flags == FTW_SL && stat(file, &sb) < 0) 22297c478bd9Sstevel@tonic-gate return (0); 22307c478bd9Sstevel@tonic-gate 22317c478bd9Sstevel@tonic-gate /* 22327c478bd9Sstevel@tonic-gate * new_nvlp may be NULL if there were errors earlier 22337c478bd9Sstevel@tonic-gate * but this is not fatal to update determination. 22347c478bd9Sstevel@tonic-gate */ 22357c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp) { 223648847494SEnrico Perla - Sun Microsystems filestat[0] = st->st_size; 223748847494SEnrico Perla - Sun Microsystems filestat[1] = st->st_mtime; 22387c478bd9Sstevel@tonic-gate error = nvlist_add_uint64_array(walk_arg.new_nvlp, 22397c478bd9Sstevel@tonic-gate file + bam_rootlen, filestat, 2); 22407c478bd9Sstevel@tonic-gate if (error) 22417c478bd9Sstevel@tonic-gate bam_error(NVADD_FAIL, file, strerror(error)); 22427c478bd9Sstevel@tonic-gate } 22437c478bd9Sstevel@tonic-gate 22447c478bd9Sstevel@tonic-gate /* 2245d876c67dSjg * If we are invoked as part of system/filesystem/boot-archive, then 224658091fd8Ssetje * there are a number of things we should not worry about 22477c478bd9Sstevel@tonic-gate */ 224858091fd8Ssetje if (bam_smf_check) { 224958091fd8Ssetje /* ignore amd64 modules unless we are booted amd64. */ 225058091fd8Ssetje if (!is_amd64() && strstr(file, "/amd64/") != 0) 22517c478bd9Sstevel@tonic-gate return (0); 22527c478bd9Sstevel@tonic-gate 225358091fd8Ssetje /* read in list of safe files */ 2254110570ceSToomas Soome if (safefiles == NULL) { 2255110570ceSToomas Soome fp = fopen("/boot/solaris/filelist.safe", "r"); 2256110570ceSToomas Soome if (fp != NULL) { 225758091fd8Ssetje safefiles = s_calloc(1, 225858091fd8Ssetje sizeof (struct safefile)); 225958091fd8Ssetje safefilep = safefiles; 226058091fd8Ssetje safefilep->name = s_calloc(1, MAXPATHLEN + 226158091fd8Ssetje MAXNAMELEN); 226258091fd8Ssetje safefilep->next = NULL; 226358091fd8Ssetje while (s_fgets(safefilep->name, MAXPATHLEN + 226458091fd8Ssetje MAXNAMELEN, fp) != NULL) { 226558091fd8Ssetje safefilep->next = s_calloc(1, 226658091fd8Ssetje sizeof (struct safefile)); 226758091fd8Ssetje safefilep = safefilep->next; 226858091fd8Ssetje safefilep->name = s_calloc(1, 226958091fd8Ssetje MAXPATHLEN + MAXNAMELEN); 227058091fd8Ssetje safefilep->next = NULL; 227158091fd8Ssetje } 227258091fd8Ssetje (void) fclose(fp); 227358091fd8Ssetje } 227458091fd8Ssetje } 2275110570ceSToomas Soome } 227658091fd8Ssetje 22777c478bd9Sstevel@tonic-gate /* 227848847494SEnrico Perla - Sun Microsystems * On SPARC we create a -path-list file for mkisofs 227948847494SEnrico Perla - Sun Microsystems */ 228048847494SEnrico Perla - Sun Microsystems if (is_flag_on(IS_SPARC_TARGET) && !bam_nowrite()) { 228148847494SEnrico Perla - Sun Microsystems if (flags != FTW_D) { 228248847494SEnrico Perla - Sun Microsystems char *strip; 228348847494SEnrico Perla - Sun Microsystems 228448847494SEnrico Perla - Sun Microsystems strip = (char *)file + strlen(rootbuf); 228548847494SEnrico Perla - Sun Microsystems (void) fprintf(walk_arg.sparcfile, "/%s=%s\n", strip, 228648847494SEnrico Perla - Sun Microsystems file); 228748847494SEnrico Perla - Sun Microsystems } 228848847494SEnrico Perla - Sun Microsystems } 228948847494SEnrico Perla - Sun Microsystems 229048847494SEnrico Perla - Sun Microsystems /* 229148847494SEnrico Perla - Sun Microsystems * We are transitioning from the old model to the dircache or the cache 229248847494SEnrico Perla - Sun Microsystems * directory was removed: create the entry without further checkings. 229348847494SEnrico Perla - Sun Microsystems */ 229448847494SEnrico Perla - Sun Microsystems if (is_flag_on(NEED_CACHE_DIR)) { 229548847494SEnrico Perla - Sun Microsystems if (bam_verbose) 229648847494SEnrico Perla - Sun Microsystems bam_print(PARSEABLE_NEW_FILE, file); 229748847494SEnrico Perla - Sun Microsystems 229848847494SEnrico Perla - Sun Microsystems if (is_flag_on(IS_SPARC_TARGET)) { 229948847494SEnrico Perla - Sun Microsystems set_dir_flag(FILE64, NEED_UPDATE); 230048847494SEnrico Perla - Sun Microsystems return (0); 230148847494SEnrico Perla - Sun Microsystems } 230248847494SEnrico Perla - Sun Microsystems 230348847494SEnrico Perla - Sun Microsystems ret = update_dircache(file, flags); 230448847494SEnrico Perla - Sun Microsystems if (ret == BAM_ERROR) { 230548847494SEnrico Perla - Sun Microsystems bam_error(UPDT_CACHE_FAIL, file); 230648847494SEnrico Perla - Sun Microsystems return (-1); 230748847494SEnrico Perla - Sun Microsystems } 230848847494SEnrico Perla - Sun Microsystems 230948847494SEnrico Perla - Sun Microsystems return (0); 231048847494SEnrico Perla - Sun Microsystems } 231148847494SEnrico Perla - Sun Microsystems 231248847494SEnrico Perla - Sun Microsystems /* 23137c478bd9Sstevel@tonic-gate * We need an update if file doesn't exist in old archive 23147c478bd9Sstevel@tonic-gate */ 23157c478bd9Sstevel@tonic-gate if (walk_arg.old_nvlp == NULL || 23167c478bd9Sstevel@tonic-gate nvlist_lookup_uint64_array(walk_arg.old_nvlp, 23177c478bd9Sstevel@tonic-gate file + bam_rootlen, &value, &sz) != 0) { 23187c478bd9Sstevel@tonic-gate if (bam_smf_check) /* ignore new during smf check */ 23197c478bd9Sstevel@tonic-gate return (0); 232048847494SEnrico Perla - Sun Microsystems 232148847494SEnrico Perla - Sun Microsystems if (is_flag_on(IS_SPARC_TARGET)) { 232248847494SEnrico Perla - Sun Microsystems set_dir_flag(FILE64, NEED_UPDATE); 232348847494SEnrico Perla - Sun Microsystems } else { 232448847494SEnrico Perla - Sun Microsystems ret = update_dircache(file, flags); 232548847494SEnrico Perla - Sun Microsystems if (ret == BAM_ERROR) { 232648847494SEnrico Perla - Sun Microsystems bam_error(UPDT_CACHE_FAIL, file); 232748847494SEnrico Perla - Sun Microsystems return (-1); 232848847494SEnrico Perla - Sun Microsystems } 232948847494SEnrico Perla - Sun Microsystems } 233048847494SEnrico Perla - Sun Microsystems 23317c478bd9Sstevel@tonic-gate if (bam_verbose) 23327c478bd9Sstevel@tonic-gate bam_print(PARSEABLE_NEW_FILE, file); 23337c478bd9Sstevel@tonic-gate return (0); 23347c478bd9Sstevel@tonic-gate } 23357c478bd9Sstevel@tonic-gate 23367c478bd9Sstevel@tonic-gate /* 233748847494SEnrico Perla - Sun Microsystems * If we got there, the file is already listed as to be included in the 233848847494SEnrico Perla - Sun Microsystems * iso image. We just need to know if we are going to rebuild it or not 233948847494SEnrico Perla - Sun Microsystems */ 234048847494SEnrico Perla - Sun Microsystems if (is_flag_on(IS_SPARC_TARGET) && 2341cedc7e57SEnrico Perla - Sun Microsystems is_dir_flag_on(FILE64, NEED_UPDATE) && !bam_nowrite()) 234248847494SEnrico Perla - Sun Microsystems return (0); 234348847494SEnrico Perla - Sun Microsystems /* 23447c478bd9Sstevel@tonic-gate * File exists in old archive. Check if file has changed 23457c478bd9Sstevel@tonic-gate */ 23467c478bd9Sstevel@tonic-gate assert(sz == 2); 23477c478bd9Sstevel@tonic-gate bcopy(value, filestat, sizeof (filestat)); 23487c478bd9Sstevel@tonic-gate 234948847494SEnrico Perla - Sun Microsystems if (flags != FTW_D && (filestat[0] != st->st_size || 235048847494SEnrico Perla - Sun Microsystems filestat[1] != st->st_mtime)) { 23517c609327Ssetje if (bam_smf_check) { 23527c609327Ssetje safefilep = safefiles; 235389c3ee43SGangadhar Mylapuram while (safefilep != NULL && 235489c3ee43SGangadhar Mylapuram safefilep->name[0] != '\0') { 235589c3ee43SGangadhar Mylapuram if (regcomp(&re, safefilep->name, 235689c3ee43SGangadhar Mylapuram REG_EXTENDED|REG_NOSUB) == 0) { 235789c3ee43SGangadhar Mylapuram status = regexec(&re, 235889c3ee43SGangadhar Mylapuram file + bam_rootlen, 0, NULL, 0); 235989c3ee43SGangadhar Mylapuram regfree(&re); 236089c3ee43SGangadhar Mylapuram if (status == 0) { 23613b133becSGangadhar Mylapuram (void) creat( 23623b133becSGangadhar Mylapuram NEED_UPDATE_SAFE_FILE, 236389c3ee43SGangadhar Mylapuram 0644); 23647c609327Ssetje return (0); 23657c609327Ssetje } 236689c3ee43SGangadhar Mylapuram } 23677c609327Ssetje safefilep = safefilep->next; 23687c609327Ssetje } 23697c609327Ssetje } 237048847494SEnrico Perla - Sun Microsystems 237148847494SEnrico Perla - Sun Microsystems if (is_flag_on(IS_SPARC_TARGET)) { 237248847494SEnrico Perla - Sun Microsystems set_dir_flag(FILE64, NEED_UPDATE); 237348847494SEnrico Perla - Sun Microsystems } else { 237448847494SEnrico Perla - Sun Microsystems ret = update_dircache(file, flags); 237548847494SEnrico Perla - Sun Microsystems if (ret == BAM_ERROR) { 237648847494SEnrico Perla - Sun Microsystems bam_error(UPDT_CACHE_FAIL, file); 237748847494SEnrico Perla - Sun Microsystems return (-1); 237848847494SEnrico Perla - Sun Microsystems } 237948847494SEnrico Perla - Sun Microsystems } 238048847494SEnrico Perla - Sun Microsystems 2381110570ceSToomas Soome if (bam_verbose) { 23827c478bd9Sstevel@tonic-gate if (bam_smf_check) 23837c478bd9Sstevel@tonic-gate bam_print(" %s\n", file); 23847c478bd9Sstevel@tonic-gate else 23857c478bd9Sstevel@tonic-gate bam_print(PARSEABLE_OUT_DATE, file); 23867c478bd9Sstevel@tonic-gate } 2387110570ceSToomas Soome } 23887c478bd9Sstevel@tonic-gate 23897c478bd9Sstevel@tonic-gate return (0); 23907c478bd9Sstevel@tonic-gate } 23917c478bd9Sstevel@tonic-gate 23927c478bd9Sstevel@tonic-gate /* 239348847494SEnrico Perla - Sun Microsystems * Remove a directory path recursively 239448847494SEnrico Perla - Sun Microsystems */ 239548847494SEnrico Perla - Sun Microsystems static int 239648847494SEnrico Perla - Sun Microsystems rmdir_r(char *path) 239748847494SEnrico Perla - Sun Microsystems { 239848847494SEnrico Perla - Sun Microsystems struct dirent *d = NULL; 239948847494SEnrico Perla - Sun Microsystems DIR *dir = NULL; 240048847494SEnrico Perla - Sun Microsystems char tpath[PATH_MAX]; 240148847494SEnrico Perla - Sun Microsystems struct stat sb; 240248847494SEnrico Perla - Sun Microsystems 240348847494SEnrico Perla - Sun Microsystems if ((dir = opendir(path)) == NULL) 240448847494SEnrico Perla - Sun Microsystems return (-1); 240548847494SEnrico Perla - Sun Microsystems 2406110570ceSToomas Soome while ((d = readdir(dir)) != NULL) { 240748847494SEnrico Perla - Sun Microsystems if ((strcmp(d->d_name, ".") != 0) && 240848847494SEnrico Perla - Sun Microsystems (strcmp(d->d_name, "..") != 0)) { 240948847494SEnrico Perla - Sun Microsystems (void) snprintf(tpath, sizeof (tpath), "%s/%s", 241048847494SEnrico Perla - Sun Microsystems path, d->d_name); 241148847494SEnrico Perla - Sun Microsystems if (stat(tpath, &sb) == 0) { 241248847494SEnrico Perla - Sun Microsystems if (sb.st_mode & S_IFDIR) 241348847494SEnrico Perla - Sun Microsystems (void) rmdir_r(tpath); 241448847494SEnrico Perla - Sun Microsystems else 241548847494SEnrico Perla - Sun Microsystems (void) remove(tpath); 241648847494SEnrico Perla - Sun Microsystems } 241748847494SEnrico Perla - Sun Microsystems } 241848847494SEnrico Perla - Sun Microsystems } 241948847494SEnrico Perla - Sun Microsystems return (remove(path)); 242048847494SEnrico Perla - Sun Microsystems } 242148847494SEnrico Perla - Sun Microsystems 242248847494SEnrico Perla - Sun Microsystems /* 242348847494SEnrico Perla - Sun Microsystems * Check if cache directory exists and, if not, create it and update flags 242448847494SEnrico Perla - Sun Microsystems * accordingly. If the path exists, but it's not a directory, a best effort 242548847494SEnrico Perla - Sun Microsystems * attempt to remove and recreate it is made. 242648847494SEnrico Perla - Sun Microsystems * If the user requested a 'purge', always recreate the directory from scratch. 242748847494SEnrico Perla - Sun Microsystems */ 242848847494SEnrico Perla - Sun Microsystems static int 242948847494SEnrico Perla - Sun Microsystems set_cache_dir(char *root, int what) 243048847494SEnrico Perla - Sun Microsystems { 243148847494SEnrico Perla - Sun Microsystems struct stat sb; 243248847494SEnrico Perla - Sun Microsystems int ret = 0; 243348847494SEnrico Perla - Sun Microsystems 243448847494SEnrico Perla - Sun Microsystems ret = snprintf(get_cachedir(what), sizeof (get_cachedir(what)), 243548847494SEnrico Perla - Sun Microsystems "%s%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(), what == FILE64 ? 243648847494SEnrico Perla - Sun Microsystems "/amd64" : "", CACHEDIR_SUFFIX); 243748847494SEnrico Perla - Sun Microsystems 243848847494SEnrico Perla - Sun Microsystems if (ret >= sizeof (get_cachedir(what))) { 243948847494SEnrico Perla - Sun Microsystems bam_error(PATH_TOO_LONG, rootbuf); 244048847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 244148847494SEnrico Perla - Sun Microsystems } 244248847494SEnrico Perla - Sun Microsystems 24434a9df875SEnrico Perla - Sun Microsystems if (bam_purge || is_flag_on(INVALIDATE_CACHE)) 244448847494SEnrico Perla - Sun Microsystems (void) rmdir_r(get_cachedir(what)); 244548847494SEnrico Perla - Sun Microsystems 244648847494SEnrico Perla - Sun Microsystems if (stat(get_cachedir(what), &sb) != 0 || !(S_ISDIR(sb.st_mode))) { 244748847494SEnrico Perla - Sun Microsystems /* best effort unlink attempt, mkdir will catch errors */ 244848847494SEnrico Perla - Sun Microsystems (void) unlink(get_cachedir(what)); 244948847494SEnrico Perla - Sun Microsystems 245048847494SEnrico Perla - Sun Microsystems if (bam_verbose) 245148847494SEnrico Perla - Sun Microsystems bam_print(UPDATE_CDIR_MISS, get_cachedir(what)); 245248847494SEnrico Perla - Sun Microsystems ret = mkdir(get_cachedir(what), DIR_PERMS); 245348847494SEnrico Perla - Sun Microsystems if (ret < 0) { 245448847494SEnrico Perla - Sun Microsystems bam_error(MKDIR_FAILED, get_cachedir(what), 245548847494SEnrico Perla - Sun Microsystems strerror(errno)); 245648847494SEnrico Perla - Sun Microsystems get_cachedir(what)[0] = '\0'; 245748847494SEnrico Perla - Sun Microsystems return (ret); 245848847494SEnrico Perla - Sun Microsystems } 245948847494SEnrico Perla - Sun Microsystems set_flag(NEED_CACHE_DIR); 246048847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NO_MULTI); 246148847494SEnrico Perla - Sun Microsystems } 246248847494SEnrico Perla - Sun Microsystems 246348847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 246448847494SEnrico Perla - Sun Microsystems } 246548847494SEnrico Perla - Sun Microsystems 246648847494SEnrico Perla - Sun Microsystems static int 246748847494SEnrico Perla - Sun Microsystems set_update_dir(char *root, int what) 246848847494SEnrico Perla - Sun Microsystems { 246948847494SEnrico Perla - Sun Microsystems struct stat sb; 247048847494SEnrico Perla - Sun Microsystems int ret; 247148847494SEnrico Perla - Sun Microsystems 247248847494SEnrico Perla - Sun Microsystems if (is_dir_flag_on(what, NO_MULTI)) 247348847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 247448847494SEnrico Perla - Sun Microsystems 247548847494SEnrico Perla - Sun Microsystems if (!bam_extend) { 247648847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NO_MULTI); 247748847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 247848847494SEnrico Perla - Sun Microsystems } 247948847494SEnrico Perla - Sun Microsystems 248048847494SEnrico Perla - Sun Microsystems if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET)) 248148847494SEnrico Perla - Sun Microsystems ret = snprintf(get_updatedir(what), 248248847494SEnrico Perla - Sun Microsystems sizeof (get_updatedir(what)), "%s%s%s/amd64%s", root, 248348847494SEnrico Perla - Sun Microsystems ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX); 248448847494SEnrico Perla - Sun Microsystems else 248548847494SEnrico Perla - Sun Microsystems ret = snprintf(get_updatedir(what), 248648847494SEnrico Perla - Sun Microsystems sizeof (get_updatedir(what)), "%s%s%s%s", root, 248748847494SEnrico Perla - Sun Microsystems ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX); 248848847494SEnrico Perla - Sun Microsystems 248948847494SEnrico Perla - Sun Microsystems if (ret >= sizeof (get_updatedir(what))) { 249048847494SEnrico Perla - Sun Microsystems bam_error(PATH_TOO_LONG, rootbuf); 249148847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 249248847494SEnrico Perla - Sun Microsystems } 249348847494SEnrico Perla - Sun Microsystems 249448847494SEnrico Perla - Sun Microsystems if (stat(get_updatedir(what), &sb) == 0) { 249548847494SEnrico Perla - Sun Microsystems if (S_ISDIR(sb.st_mode)) 249648847494SEnrico Perla - Sun Microsystems ret = rmdir_r(get_updatedir(what)); 249748847494SEnrico Perla - Sun Microsystems else 249848847494SEnrico Perla - Sun Microsystems ret = unlink(get_updatedir(what)); 249948847494SEnrico Perla - Sun Microsystems 250048847494SEnrico Perla - Sun Microsystems if (ret != 0) 250148847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NO_MULTI); 250248847494SEnrico Perla - Sun Microsystems } 250348847494SEnrico Perla - Sun Microsystems 250448847494SEnrico Perla - Sun Microsystems if (mkdir(get_updatedir(what), DIR_PERMS) < 0) 250548847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NO_MULTI); 250648847494SEnrico Perla - Sun Microsystems 250748847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 250848847494SEnrico Perla - Sun Microsystems } 250948847494SEnrico Perla - Sun Microsystems 251048847494SEnrico Perla - Sun Microsystems static int 251148847494SEnrico Perla - Sun Microsystems is_valid_archive(char *root, int what) 251248847494SEnrico Perla - Sun Microsystems { 251348847494SEnrico Perla - Sun Microsystems char archive_path[PATH_MAX]; 25144a9df875SEnrico Perla - Sun Microsystems char timestamp_path[PATH_MAX]; 25154a9df875SEnrico Perla - Sun Microsystems struct stat sb, timestamp; 251648847494SEnrico Perla - Sun Microsystems int ret; 251748847494SEnrico Perla - Sun Microsystems 251848847494SEnrico Perla - Sun Microsystems if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET)) 251948847494SEnrico Perla - Sun Microsystems ret = snprintf(archive_path, sizeof (archive_path), 252048847494SEnrico Perla - Sun Microsystems "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(), 252148847494SEnrico Perla - Sun Microsystems ARCHIVE_SUFFIX); 252248847494SEnrico Perla - Sun Microsystems else 252348847494SEnrico Perla - Sun Microsystems ret = snprintf(archive_path, sizeof (archive_path), "%s%s%s%s", 252448847494SEnrico Perla - Sun Microsystems root, ARCHIVE_PREFIX, get_machine(), ARCHIVE_SUFFIX); 252548847494SEnrico Perla - Sun Microsystems 252648847494SEnrico Perla - Sun Microsystems if (ret >= sizeof (archive_path)) { 252748847494SEnrico Perla - Sun Microsystems bam_error(PATH_TOO_LONG, rootbuf); 252848847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 252948847494SEnrico Perla - Sun Microsystems } 253048847494SEnrico Perla - Sun Microsystems 253148847494SEnrico Perla - Sun Microsystems if (stat(archive_path, &sb) != 0) { 253248847494SEnrico Perla - Sun Microsystems if (bam_verbose && !bam_check) 253348847494SEnrico Perla - Sun Microsystems bam_print(UPDATE_ARCH_MISS, archive_path); 253448847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NEED_UPDATE); 253548847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NO_MULTI); 253648847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 253748847494SEnrico Perla - Sun Microsystems } 253848847494SEnrico Perla - Sun Microsystems 25394a9df875SEnrico Perla - Sun Microsystems /* 25404a9df875SEnrico Perla - Sun Microsystems * The timestamp file is used to prevent stale files in the archive 25414a9df875SEnrico Perla - Sun Microsystems * cache. 25424a9df875SEnrico Perla - Sun Microsystems * Stale files can happen if the system is booted back and forth across 25434a9df875SEnrico Perla - Sun Microsystems * the transition from bootadm-before-the-cache to 25444a9df875SEnrico Perla - Sun Microsystems * bootadm-after-the-cache, since older versions of bootadm don't know 25454a9df875SEnrico Perla - Sun Microsystems * about the existence of the archive cache. 25464a9df875SEnrico Perla - Sun Microsystems * 25474a9df875SEnrico Perla - Sun Microsystems * Since only bootadm-after-the-cache versions know about about this 25484a9df875SEnrico Perla - Sun Microsystems * file, we require that the boot archive be older than this file. 25494a9df875SEnrico Perla - Sun Microsystems */ 25504a9df875SEnrico Perla - Sun Microsystems ret = snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root, 25514a9df875SEnrico Perla - Sun Microsystems FILE_STAT_TIMESTAMP); 25524a9df875SEnrico Perla - Sun Microsystems 25534a9df875SEnrico Perla - Sun Microsystems if (ret >= sizeof (timestamp_path)) { 25544a9df875SEnrico Perla - Sun Microsystems bam_error(PATH_TOO_LONG, rootbuf); 25554a9df875SEnrico Perla - Sun Microsystems return (BAM_ERROR); 25564a9df875SEnrico Perla - Sun Microsystems } 25574a9df875SEnrico Perla - Sun Microsystems 25584a9df875SEnrico Perla - Sun Microsystems if (stat(timestamp_path, ×tamp) != 0 || 25594a9df875SEnrico Perla - Sun Microsystems sb.st_mtime > timestamp.st_mtime) { 25604a9df875SEnrico Perla - Sun Microsystems if (bam_verbose && !bam_check) 256118961ad2SToomas Soome bam_print(UPDATE_CACHE_OLD); 25624a9df875SEnrico Perla - Sun Microsystems /* 25634a9df875SEnrico Perla - Sun Microsystems * Don't generate a false positive for the boot-archive service 25644a9df875SEnrico Perla - Sun Microsystems * but trigger an update of the archive cache in 25654a9df875SEnrico Perla - Sun Microsystems * boot-archive-update. 25664a9df875SEnrico Perla - Sun Microsystems */ 25674a9df875SEnrico Perla - Sun Microsystems if (bam_smf_check) { 25684a9df875SEnrico Perla - Sun Microsystems (void) creat(NEED_UPDATE_FILE, 0644); 25694a9df875SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 25704a9df875SEnrico Perla - Sun Microsystems } 25714a9df875SEnrico Perla - Sun Microsystems 25724a9df875SEnrico Perla - Sun Microsystems set_flag(INVALIDATE_CACHE); 25734a9df875SEnrico Perla - Sun Microsystems set_dir_flag(what, NEED_UPDATE); 25744a9df875SEnrico Perla - Sun Microsystems set_dir_flag(what, NO_MULTI); 25754a9df875SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 25764a9df875SEnrico Perla - Sun Microsystems } 25774a9df875SEnrico Perla - Sun Microsystems 257848847494SEnrico Perla - Sun Microsystems if (is_flag_on(IS_SPARC_TARGET)) 257948847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 258048847494SEnrico Perla - Sun Microsystems 258148847494SEnrico Perla - Sun Microsystems if (bam_extend && sb.st_size > BA_SIZE_MAX) { 258248847494SEnrico Perla - Sun Microsystems if (bam_verbose && !bam_check) 258348847494SEnrico Perla - Sun Microsystems bam_print(MULTI_SIZE, archive_path, BA_SIZE_MAX); 258448847494SEnrico Perla - Sun Microsystems set_dir_flag(what, NO_MULTI); 258548847494SEnrico Perla - Sun Microsystems } 258648847494SEnrico Perla - Sun Microsystems 258748847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 258848847494SEnrico Perla - Sun Microsystems } 258948847494SEnrico Perla - Sun Microsystems 259048847494SEnrico Perla - Sun Microsystems /* 259148847494SEnrico Perla - Sun Microsystems * Check flags and presence of required files and directories. 25927c478bd9Sstevel@tonic-gate * The force flag and/or absence of files should 25937c478bd9Sstevel@tonic-gate * trigger an update. 25947c478bd9Sstevel@tonic-gate * Suppress stdout output if check (-n) option is set 25957c478bd9Sstevel@tonic-gate * (as -n should only produce parseable output.) 25967c478bd9Sstevel@tonic-gate */ 259748847494SEnrico Perla - Sun Microsystems static int 25987c478bd9Sstevel@tonic-gate check_flags_and_files(char *root) 25997c478bd9Sstevel@tonic-gate { 260048847494SEnrico Perla - Sun Microsystems 26017c478bd9Sstevel@tonic-gate struct stat sb; 260248847494SEnrico Perla - Sun Microsystems int ret; 260348847494SEnrico Perla - Sun Microsystems 260448847494SEnrico Perla - Sun Microsystems /* 260548847494SEnrico Perla - Sun Microsystems * If archive is missing, create archive 260648847494SEnrico Perla - Sun Microsystems */ 260748847494SEnrico Perla - Sun Microsystems if (is_flag_on(IS_SPARC_TARGET)) { 260848847494SEnrico Perla - Sun Microsystems ret = is_valid_archive(root, FILE64); 2609cedc7e57SEnrico Perla - Sun Microsystems if (ret == BAM_ERROR) 2610cedc7e57SEnrico Perla - Sun Microsystems return (BAM_ERROR); 261148847494SEnrico Perla - Sun Microsystems } else { 261248847494SEnrico Perla - Sun Microsystems int what = FILE32; 261348847494SEnrico Perla - Sun Microsystems do { 261448847494SEnrico Perla - Sun Microsystems ret = is_valid_archive(root, what); 261548847494SEnrico Perla - Sun Microsystems if (ret == BAM_ERROR) 261648847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 261748847494SEnrico Perla - Sun Microsystems what++; 261848847494SEnrico Perla - Sun Microsystems } while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM); 261948847494SEnrico Perla - Sun Microsystems } 262048847494SEnrico Perla - Sun Microsystems 262148847494SEnrico Perla - Sun Microsystems if (bam_nowrite()) 262248847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 262348847494SEnrico Perla - Sun Microsystems 262448847494SEnrico Perla - Sun Microsystems 262548847494SEnrico Perla - Sun Microsystems /* 262648847494SEnrico Perla - Sun Microsystems * check if cache directories exist on x86. 262748847494SEnrico Perla - Sun Microsystems * check (and always open) the cache file on SPARC. 262848847494SEnrico Perla - Sun Microsystems */ 262948847494SEnrico Perla - Sun Microsystems if (is_sparc()) { 263048847494SEnrico Perla - Sun Microsystems ret = snprintf(get_cachedir(FILE64), 263148847494SEnrico Perla - Sun Microsystems sizeof (get_cachedir(FILE64)), "%s%s%s/%s", root, 263248847494SEnrico Perla - Sun Microsystems ARCHIVE_PREFIX, get_machine(), CACHEDIR_SUFFIX); 263348847494SEnrico Perla - Sun Microsystems 263448847494SEnrico Perla - Sun Microsystems if (ret >= sizeof (get_cachedir(FILE64))) { 263548847494SEnrico Perla - Sun Microsystems bam_error(PATH_TOO_LONG, rootbuf); 263648847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 263748847494SEnrico Perla - Sun Microsystems } 263848847494SEnrico Perla - Sun Microsystems 263948847494SEnrico Perla - Sun Microsystems if (stat(get_cachedir(FILE64), &sb) != 0) { 264048847494SEnrico Perla - Sun Microsystems set_flag(NEED_CACHE_DIR); 264148847494SEnrico Perla - Sun Microsystems set_dir_flag(FILE64, NEED_UPDATE); 264248847494SEnrico Perla - Sun Microsystems } 264348847494SEnrico Perla - Sun Microsystems 264448847494SEnrico Perla - Sun Microsystems walk_arg.sparcfile = fopen(get_cachedir(FILE64), "w"); 264548847494SEnrico Perla - Sun Microsystems if (walk_arg.sparcfile == NULL) { 264648847494SEnrico Perla - Sun Microsystems bam_error(OPEN_FAIL, get_cachedir(FILE64), 264748847494SEnrico Perla - Sun Microsystems strerror(errno)); 264848847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 264948847494SEnrico Perla - Sun Microsystems } 265048847494SEnrico Perla - Sun Microsystems 265148847494SEnrico Perla - Sun Microsystems set_dir_present(FILE64); 265248847494SEnrico Perla - Sun Microsystems } else { 265348847494SEnrico Perla - Sun Microsystems int what = FILE32; 265448847494SEnrico Perla - Sun Microsystems 265548847494SEnrico Perla - Sun Microsystems do { 265648847494SEnrico Perla - Sun Microsystems if (set_cache_dir(root, what) != 0) 265748847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 265848847494SEnrico Perla - Sun Microsystems 265948847494SEnrico Perla - Sun Microsystems set_dir_present(what); 266048847494SEnrico Perla - Sun Microsystems 266148847494SEnrico Perla - Sun Microsystems if (set_update_dir(root, what) != 0) 266248847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 266348847494SEnrico Perla - Sun Microsystems what++; 266448847494SEnrico Perla - Sun Microsystems } while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM); 266548847494SEnrico Perla - Sun Microsystems } 26667c478bd9Sstevel@tonic-gate 26677c478bd9Sstevel@tonic-gate /* 26687c478bd9Sstevel@tonic-gate * if force, create archive unconditionally 26697c478bd9Sstevel@tonic-gate */ 26707c478bd9Sstevel@tonic-gate if (bam_force) { 267148847494SEnrico Perla - Sun Microsystems if (!is_sparc()) 267248847494SEnrico Perla - Sun Microsystems set_dir_flag(FILE32, NEED_UPDATE); 267348847494SEnrico Perla - Sun Microsystems set_dir_flag(FILE64, NEED_UPDATE); 267448847494SEnrico Perla - Sun Microsystems if (bam_verbose) 26757c478bd9Sstevel@tonic-gate bam_print(UPDATE_FORCE); 267648847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 26777c478bd9Sstevel@tonic-gate } 26787c478bd9Sstevel@tonic-gate 267948847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 26807c478bd9Sstevel@tonic-gate } 26817c478bd9Sstevel@tonic-gate 26827c478bd9Sstevel@tonic-gate static error_t 26837c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t *flistp, char *filelist) 26847c478bd9Sstevel@tonic-gate { 26857c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 26867c478bd9Sstevel@tonic-gate FILE *fp; 26877c478bd9Sstevel@tonic-gate char buf[BAM_MAXLINE]; 2688eb2bd662Svikram const char *fcn = "read_one_list()"; 26897c478bd9Sstevel@tonic-gate 26907c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, filelist); 26917c478bd9Sstevel@tonic-gate 26927c478bd9Sstevel@tonic-gate fp = fopen(path, "r"); 26937c478bd9Sstevel@tonic-gate if (fp == NULL) { 2694eb2bd662Svikram BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno))); 26957c478bd9Sstevel@tonic-gate return (BAM_ERROR); 26967c478bd9Sstevel@tonic-gate } 26977c478bd9Sstevel@tonic-gate while (s_fgets(buf, sizeof (buf), fp) != NULL) { 2698b610f78eSvikram /* skip blank lines */ 2699b610f78eSvikram if (strspn(buf, " \t") == strlen(buf)) 2700b610f78eSvikram continue; 27017c478bd9Sstevel@tonic-gate append_to_flist(flistp, buf); 27027c478bd9Sstevel@tonic-gate } 27037c478bd9Sstevel@tonic-gate if (fclose(fp) != 0) { 27047c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, path, strerror(errno)); 27057c478bd9Sstevel@tonic-gate return (BAM_ERROR); 27067c478bd9Sstevel@tonic-gate } 27077c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 27087c478bd9Sstevel@tonic-gate } 27097c478bd9Sstevel@tonic-gate 27107c478bd9Sstevel@tonic-gate static error_t 27117c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t *flistp) 27127c478bd9Sstevel@tonic-gate { 2713986fd29aSsetje char path[PATH_MAX]; 2714986fd29aSsetje char cmd[PATH_MAX]; 2715986fd29aSsetje struct stat sb; 2716986fd29aSsetje int n, rval; 2717eb2bd662Svikram const char *fcn = "read_list()"; 27187c478bd9Sstevel@tonic-gate 27197c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 27207c478bd9Sstevel@tonic-gate 27217c478bd9Sstevel@tonic-gate /* 2722986fd29aSsetje * build and check path to extract_boot_filelist.ksh 2723986fd29aSsetje */ 2724986fd29aSsetje n = snprintf(path, sizeof (path), "%s%s", root, EXTRACT_BOOT_FILELIST); 2725986fd29aSsetje if (n >= sizeof (path)) { 2726986fd29aSsetje bam_error(NO_FLIST); 2727986fd29aSsetje return (BAM_ERROR); 2728986fd29aSsetje } 2729986fd29aSsetje 2730cedc7e57SEnrico Perla - Sun Microsystems if (is_safe_exec(path) == BAM_ERROR) 2731cedc7e57SEnrico Perla - Sun Microsystems return (BAM_ERROR); 2732cedc7e57SEnrico Perla - Sun Microsystems 2733986fd29aSsetje /* 2734986fd29aSsetje * If extract_boot_filelist is present, exec it, otherwise read 2735986fd29aSsetje * the filelists directly, for compatibility with older images. 2736986fd29aSsetje */ 2737986fd29aSsetje if (stat(path, &sb) == 0) { 2738986fd29aSsetje /* 2739986fd29aSsetje * build arguments to exec extract_boot_filelist.ksh 2740986fd29aSsetje */ 2741d876c67dSjg char *rootarg, *platarg; 2742d876c67dSjg int platarglen = 1, rootarglen = 1; 2743d876c67dSjg if (strlen(root) > 1) 2744d876c67dSjg rootarglen += strlen(root) + strlen("-R "); 2745d876c67dSjg if (bam_alt_platform) 2746d876c67dSjg platarglen += strlen(bam_platform) + strlen("-p "); 2747d876c67dSjg platarg = s_calloc(1, platarglen); 2748d876c67dSjg rootarg = s_calloc(1, rootarglen); 2749d876c67dSjg *platarg = 0; 2750d876c67dSjg *rootarg = 0; 2751d876c67dSjg 2752986fd29aSsetje if (strlen(root) > 1) { 2753d876c67dSjg (void) snprintf(rootarg, rootarglen, 2754d876c67dSjg "-R %s", root); 2755986fd29aSsetje } 2756d876c67dSjg if (bam_alt_platform) { 2757d876c67dSjg (void) snprintf(platarg, platarglen, 2758d876c67dSjg "-p %s", bam_platform); 2759d876c67dSjg } 2760d876c67dSjg n = snprintf(cmd, sizeof (cmd), "%s %s %s /%s /%s", 2761d876c67dSjg path, rootarg, platarg, BOOT_FILE_LIST, ETC_FILE_LIST); 2762d876c67dSjg free(platarg); 2763d876c67dSjg free(rootarg); 2764986fd29aSsetje if (n >= sizeof (cmd)) { 2765986fd29aSsetje bam_error(NO_FLIST); 2766986fd29aSsetje return (BAM_ERROR); 2767986fd29aSsetje } 2768986fd29aSsetje if (exec_cmd(cmd, flistp) != 0) { 2769eb2bd662Svikram BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno))); 2770986fd29aSsetje return (BAM_ERROR); 2771986fd29aSsetje } 2772986fd29aSsetje } else { 2773986fd29aSsetje /* 27747c478bd9Sstevel@tonic-gate * Read current lists of files - only the first is mandatory 27757c478bd9Sstevel@tonic-gate */ 27767c478bd9Sstevel@tonic-gate rval = read_one_list(root, flistp, BOOT_FILE_LIST); 27777c478bd9Sstevel@tonic-gate if (rval != BAM_SUCCESS) 27787c478bd9Sstevel@tonic-gate return (rval); 27797c478bd9Sstevel@tonic-gate (void) read_one_list(root, flistp, ETC_FILE_LIST); 2780986fd29aSsetje } 27817c478bd9Sstevel@tonic-gate 27827c478bd9Sstevel@tonic-gate if (flistp->head == NULL) { 27837c478bd9Sstevel@tonic-gate bam_error(NO_FLIST); 27847c478bd9Sstevel@tonic-gate return (BAM_ERROR); 27857c478bd9Sstevel@tonic-gate } 27867c478bd9Sstevel@tonic-gate 27877c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 27887c478bd9Sstevel@tonic-gate } 27897c478bd9Sstevel@tonic-gate 27907c478bd9Sstevel@tonic-gate static void 27917c478bd9Sstevel@tonic-gate getoldstat(char *root) 27927c478bd9Sstevel@tonic-gate { 27937c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 27947c478bd9Sstevel@tonic-gate int fd, error; 27957c478bd9Sstevel@tonic-gate struct stat sb; 27967c478bd9Sstevel@tonic-gate char *ostat; 27977c478bd9Sstevel@tonic-gate 27987c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 27997c478bd9Sstevel@tonic-gate fd = open(path, O_RDONLY); 28007c478bd9Sstevel@tonic-gate if (fd == -1) { 28017c478bd9Sstevel@tonic-gate if (bam_verbose) 28027c478bd9Sstevel@tonic-gate bam_print(OPEN_FAIL, path, strerror(errno)); 280348847494SEnrico Perla - Sun Microsystems goto out_err; 28047c478bd9Sstevel@tonic-gate } 28057c478bd9Sstevel@tonic-gate 28067c478bd9Sstevel@tonic-gate if (fstat(fd, &sb) != 0) { 28077c478bd9Sstevel@tonic-gate bam_error(STAT_FAIL, path, strerror(errno)); 280848847494SEnrico Perla - Sun Microsystems goto out_err; 28097c478bd9Sstevel@tonic-gate } 28107c478bd9Sstevel@tonic-gate 28117c478bd9Sstevel@tonic-gate ostat = s_calloc(1, sb.st_size); 28127c478bd9Sstevel@tonic-gate 28137c478bd9Sstevel@tonic-gate if (read(fd, ostat, sb.st_size) != sb.st_size) { 28147c478bd9Sstevel@tonic-gate bam_error(READ_FAIL, path, strerror(errno)); 28157c478bd9Sstevel@tonic-gate free(ostat); 281648847494SEnrico Perla - Sun Microsystems goto out_err; 28177c478bd9Sstevel@tonic-gate } 28187c478bd9Sstevel@tonic-gate 28197c478bd9Sstevel@tonic-gate (void) close(fd); 282048847494SEnrico Perla - Sun Microsystems fd = -1; 28217c478bd9Sstevel@tonic-gate 28227c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 28237c478bd9Sstevel@tonic-gate error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0); 28247c478bd9Sstevel@tonic-gate 28257c478bd9Sstevel@tonic-gate free(ostat); 28267c478bd9Sstevel@tonic-gate 28277c478bd9Sstevel@tonic-gate if (error) { 28287c478bd9Sstevel@tonic-gate bam_error(UNPACK_FAIL, path, strerror(error)); 28297c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 283048847494SEnrico Perla - Sun Microsystems goto out_err; 283148847494SEnrico Perla - Sun Microsystems } else { 28327c478bd9Sstevel@tonic-gate return; 28337c478bd9Sstevel@tonic-gate } 283448847494SEnrico Perla - Sun Microsystems 283548847494SEnrico Perla - Sun Microsystems out_err: 283648847494SEnrico Perla - Sun Microsystems if (fd != -1) 283748847494SEnrico Perla - Sun Microsystems (void) close(fd); 2838cedc7e57SEnrico Perla - Sun Microsystems if (!is_flag_on(IS_SPARC_TARGET)) 283948847494SEnrico Perla - Sun Microsystems set_dir_flag(FILE32, NEED_UPDATE); 284048847494SEnrico Perla - Sun Microsystems set_dir_flag(FILE64, NEED_UPDATE); 284148847494SEnrico Perla - Sun Microsystems } 284248847494SEnrico Perla - Sun Microsystems 284348847494SEnrico Perla - Sun Microsystems /* Best effort stale entry removal */ 284448847494SEnrico Perla - Sun Microsystems static void 284548847494SEnrico Perla - Sun Microsystems delete_stale(char *file, int what) 284648847494SEnrico Perla - Sun Microsystems { 284748847494SEnrico Perla - Sun Microsystems char path[PATH_MAX]; 284848847494SEnrico Perla - Sun Microsystems struct stat sb; 284948847494SEnrico Perla - Sun Microsystems 285048847494SEnrico Perla - Sun Microsystems (void) snprintf(path, sizeof (path), "%s/%s", get_cachedir(what), file); 285148847494SEnrico Perla - Sun Microsystems if (!bam_check && stat(path, &sb) == 0) { 285248847494SEnrico Perla - Sun Microsystems if (sb.st_mode & S_IFDIR) 285348847494SEnrico Perla - Sun Microsystems (void) rmdir_r(path); 285448847494SEnrico Perla - Sun Microsystems else 285548847494SEnrico Perla - Sun Microsystems (void) unlink(path); 285648847494SEnrico Perla - Sun Microsystems 285748847494SEnrico Perla - Sun Microsystems set_dir_flag(what, (NEED_UPDATE | NO_MULTI)); 285848847494SEnrico Perla - Sun Microsystems } 28597c478bd9Sstevel@tonic-gate } 28607c478bd9Sstevel@tonic-gate 2861a28d77b8Svikram /* 2862a28d77b8Svikram * Checks if a file in the current (old) archive has 2863a28d77b8Svikram * been deleted from the root filesystem. This is needed for 2864a28d77b8Svikram * software like Trusted Extensions (TX) that switch early 2865a28d77b8Svikram * in boot based on presence/absence of a kernel module. 2866a28d77b8Svikram */ 2867a28d77b8Svikram static void 2868a28d77b8Svikram check4stale(char *root) 2869a28d77b8Svikram { 2870a28d77b8Svikram nvpair_t *nvp; 2871a28d77b8Svikram nvlist_t *nvlp; 2872a28d77b8Svikram char *file; 2873a28d77b8Svikram char path[PATH_MAX]; 2874a28d77b8Svikram 2875a28d77b8Svikram /* 2876a28d77b8Svikram * Skip stale file check during smf check 2877a28d77b8Svikram */ 2878a28d77b8Svikram if (bam_smf_check) 2879a28d77b8Svikram return; 2880a28d77b8Svikram 288148847494SEnrico Perla - Sun Microsystems /* 288248847494SEnrico Perla - Sun Microsystems * If we need to (re)create the cache, there's no need to check for 288348847494SEnrico Perla - Sun Microsystems * stale files 288448847494SEnrico Perla - Sun Microsystems */ 288548847494SEnrico Perla - Sun Microsystems if (is_flag_on(NEED_CACHE_DIR)) 288648847494SEnrico Perla - Sun Microsystems return; 288748847494SEnrico Perla - Sun Microsystems 2888a28d77b8Svikram /* Nothing to do if no old stats */ 2889a28d77b8Svikram if ((nvlp = walk_arg.old_nvlp) == NULL) 2890a28d77b8Svikram return; 2891a28d77b8Svikram 2892a28d77b8Svikram for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp; 2893a28d77b8Svikram nvp = nvlist_next_nvpair(nvlp, nvp)) { 2894a28d77b8Svikram file = nvpair_name(nvp); 2895a28d77b8Svikram if (file == NULL) 2896a28d77b8Svikram continue; 2897a28d77b8Svikram (void) snprintf(path, sizeof (path), "%s/%s", 2898a28d77b8Svikram root, file); 289948847494SEnrico Perla - Sun Microsystems if (access(path, F_OK) < 0) { 290048847494SEnrico Perla - Sun Microsystems int what; 290148847494SEnrico Perla - Sun Microsystems 2902cedc7e57SEnrico Perla - Sun Microsystems if (bam_verbose) 2903cedc7e57SEnrico Perla - Sun Microsystems bam_print(PARSEABLE_STALE_FILE, path); 290448847494SEnrico Perla - Sun Microsystems 2905cedc7e57SEnrico Perla - Sun Microsystems if (is_flag_on(IS_SPARC_TARGET)) { 2906cedc7e57SEnrico Perla - Sun Microsystems set_dir_flag(FILE64, NEED_UPDATE); 2907cedc7e57SEnrico Perla - Sun Microsystems } else { 290848847494SEnrico Perla - Sun Microsystems for (what = FILE32; what < CACHEDIR_NUM; what++) 290948847494SEnrico Perla - Sun Microsystems if (has_cachedir(what)) 291048847494SEnrico Perla - Sun Microsystems delete_stale(file, what); 2911a28d77b8Svikram } 2912a28d77b8Svikram } 2913a28d77b8Svikram } 2914cedc7e57SEnrico Perla - Sun Microsystems } 2915a28d77b8Svikram 29167c478bd9Sstevel@tonic-gate static void 29177c478bd9Sstevel@tonic-gate create_newstat(void) 29187c478bd9Sstevel@tonic-gate { 29197c478bd9Sstevel@tonic-gate int error; 29207c478bd9Sstevel@tonic-gate 29217c478bd9Sstevel@tonic-gate error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0); 29227c478bd9Sstevel@tonic-gate if (error) { 29237c478bd9Sstevel@tonic-gate /* 29247c478bd9Sstevel@tonic-gate * Not fatal - we can still create archive 29257c478bd9Sstevel@tonic-gate */ 29267c478bd9Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 29277c478bd9Sstevel@tonic-gate bam_error(NVALLOC_FAIL, strerror(error)); 29287c478bd9Sstevel@tonic-gate } 29297c478bd9Sstevel@tonic-gate } 29307c478bd9Sstevel@tonic-gate 293148847494SEnrico Perla - Sun Microsystems static int 29327c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp) 29337c478bd9Sstevel@tonic-gate { 29347c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 29357c478bd9Sstevel@tonic-gate line_t *lp; 29367c478bd9Sstevel@tonic-gate 29377c478bd9Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 2938986fd29aSsetje /* 2939986fd29aSsetje * Don't follow symlinks. A symlink must refer to 2940986fd29aSsetje * a file that would appear in the archive through 2941986fd29aSsetje * a direct reference. This matches the archive 2942986fd29aSsetje * construction behavior. 2943986fd29aSsetje */ 29447c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, lp->line); 2945986fd29aSsetje if (nftw(path, cmpstat, 20, FTW_PHYS) == -1) { 294648847494SEnrico Perla - Sun Microsystems if (is_flag_on(UPDATE_ERROR)) 294748847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 29487c478bd9Sstevel@tonic-gate /* 29497c478bd9Sstevel@tonic-gate * Some files may not exist. 29507c478bd9Sstevel@tonic-gate * For example: etc/rtc_config on a x86 diskless system 29517c478bd9Sstevel@tonic-gate * Emit verbose message only 29527c478bd9Sstevel@tonic-gate */ 29537c478bd9Sstevel@tonic-gate if (bam_verbose) 29547c478bd9Sstevel@tonic-gate bam_print(NFTW_FAIL, path, strerror(errno)); 29557c478bd9Sstevel@tonic-gate } 29567c478bd9Sstevel@tonic-gate } 295748847494SEnrico Perla - Sun Microsystems 295848847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 29597c478bd9Sstevel@tonic-gate } 29607c478bd9Sstevel@tonic-gate 29614a9df875SEnrico Perla - Sun Microsystems /* 29624a9df875SEnrico Perla - Sun Microsystems * Update the timestamp file. 29634a9df875SEnrico Perla - Sun Microsystems */ 29644a9df875SEnrico Perla - Sun Microsystems static void 29654a9df875SEnrico Perla - Sun Microsystems update_timestamp(char *root) 29664a9df875SEnrico Perla - Sun Microsystems { 29674a9df875SEnrico Perla - Sun Microsystems char timestamp_path[PATH_MAX]; 29684a9df875SEnrico Perla - Sun Microsystems 29694a9df875SEnrico Perla - Sun Microsystems /* this path length has already been checked in check_flags_and_files */ 29704a9df875SEnrico Perla - Sun Microsystems (void) snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root, 29714a9df875SEnrico Perla - Sun Microsystems FILE_STAT_TIMESTAMP); 29724a9df875SEnrico Perla - Sun Microsystems 29734a9df875SEnrico Perla - Sun Microsystems /* 29744a9df875SEnrico Perla - Sun Microsystems * recreate the timestamp file. Since an outdated or absent timestamp 29754a9df875SEnrico Perla - Sun Microsystems * file translates in a complete rebuild of the archive cache, notify 29764a9df875SEnrico Perla - Sun Microsystems * the user of the performance issue. 29774a9df875SEnrico Perla - Sun Microsystems */ 29784a9df875SEnrico Perla - Sun Microsystems if (creat(timestamp_path, FILE_STAT_MODE) < 0) { 29794a9df875SEnrico Perla - Sun Microsystems bam_error(OPEN_FAIL, timestamp_path, strerror(errno)); 298018961ad2SToomas Soome bam_error(TIMESTAMP_FAIL); 29814a9df875SEnrico Perla - Sun Microsystems } 29824a9df875SEnrico Perla - Sun Microsystems } 29834a9df875SEnrico Perla - Sun Microsystems 29844a9df875SEnrico Perla - Sun Microsystems 29857c478bd9Sstevel@tonic-gate static void 29867c478bd9Sstevel@tonic-gate savenew(char *root) 29877c478bd9Sstevel@tonic-gate { 29887c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 29897c478bd9Sstevel@tonic-gate char path2[PATH_MAX]; 29907c478bd9Sstevel@tonic-gate size_t sz; 29917c478bd9Sstevel@tonic-gate char *nstat; 29927c478bd9Sstevel@tonic-gate int fd, wrote, error; 29937c478bd9Sstevel@tonic-gate 29947c478bd9Sstevel@tonic-gate nstat = NULL; 29957c478bd9Sstevel@tonic-gate sz = 0; 29967c478bd9Sstevel@tonic-gate error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz, 29977c478bd9Sstevel@tonic-gate NV_ENCODE_XDR, 0); 29987c478bd9Sstevel@tonic-gate if (error) { 29997c478bd9Sstevel@tonic-gate bam_error(PACK_FAIL, strerror(error)); 30007c478bd9Sstevel@tonic-gate return; 30017c478bd9Sstevel@tonic-gate } 30027c478bd9Sstevel@tonic-gate 30037c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP); 30047c478bd9Sstevel@tonic-gate fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE); 30057c478bd9Sstevel@tonic-gate if (fd == -1) { 30067c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, path, strerror(errno)); 30077c478bd9Sstevel@tonic-gate free(nstat); 30087c478bd9Sstevel@tonic-gate return; 30097c478bd9Sstevel@tonic-gate } 30107c478bd9Sstevel@tonic-gate wrote = write(fd, nstat, sz); 30117c478bd9Sstevel@tonic-gate if (wrote != sz) { 30127c478bd9Sstevel@tonic-gate bam_error(WRITE_FAIL, path, strerror(errno)); 30137c478bd9Sstevel@tonic-gate (void) close(fd); 30147c478bd9Sstevel@tonic-gate free(nstat); 30157c478bd9Sstevel@tonic-gate return; 30167c478bd9Sstevel@tonic-gate } 30177c478bd9Sstevel@tonic-gate (void) close(fd); 30187c478bd9Sstevel@tonic-gate free(nstat); 30197c478bd9Sstevel@tonic-gate 30207c478bd9Sstevel@tonic-gate (void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT); 30217c478bd9Sstevel@tonic-gate if (rename(path, path2) != 0) { 30227c478bd9Sstevel@tonic-gate bam_error(RENAME_FAIL, path2, strerror(errno)); 30237c478bd9Sstevel@tonic-gate } 30247c478bd9Sstevel@tonic-gate } 30257c478bd9Sstevel@tonic-gate 302648847494SEnrico Perla - Sun Microsystems #define init_walk_args() bzero(&walk_arg, sizeof (walk_arg)) 302748847494SEnrico Perla - Sun Microsystems 30287c478bd9Sstevel@tonic-gate static void 30297c478bd9Sstevel@tonic-gate clear_walk_args(void) 30307c478bd9Sstevel@tonic-gate { 30317c478bd9Sstevel@tonic-gate nvlist_free(walk_arg.old_nvlp); 30327c478bd9Sstevel@tonic-gate nvlist_free(walk_arg.new_nvlp); 303348847494SEnrico Perla - Sun Microsystems if (walk_arg.sparcfile) 303448847494SEnrico Perla - Sun Microsystems (void) fclose(walk_arg.sparcfile); 30357c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 30367c478bd9Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 303748847494SEnrico Perla - Sun Microsystems walk_arg.sparcfile = NULL; 30387c478bd9Sstevel@tonic-gate } 30397c478bd9Sstevel@tonic-gate 30407c478bd9Sstevel@tonic-gate /* 30417c478bd9Sstevel@tonic-gate * Returns: 30427c478bd9Sstevel@tonic-gate * 0 - no update necessary 30437c478bd9Sstevel@tonic-gate * 1 - update required. 30447c478bd9Sstevel@tonic-gate * BAM_ERROR (-1) - An error occurred 30457c478bd9Sstevel@tonic-gate * 30467c478bd9Sstevel@tonic-gate * Special handling for check (-n): 30477c478bd9Sstevel@tonic-gate * ================================ 30487c478bd9Sstevel@tonic-gate * The check (-n) option produces parseable output. 30497c478bd9Sstevel@tonic-gate * To do this, we suppress all stdout messages unrelated 30507c478bd9Sstevel@tonic-gate * to out of sync files. 30517c478bd9Sstevel@tonic-gate * All stderr messages are still printed though. 30527c478bd9Sstevel@tonic-gate * 30537c478bd9Sstevel@tonic-gate */ 30547c478bd9Sstevel@tonic-gate static int 30557c478bd9Sstevel@tonic-gate update_required(char *root) 30567c478bd9Sstevel@tonic-gate { 30577c478bd9Sstevel@tonic-gate struct stat sb; 30587c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 30597c478bd9Sstevel@tonic-gate filelist_t flist; 30607c478bd9Sstevel@tonic-gate filelist_t *flistp = &flist; 306148847494SEnrico Perla - Sun Microsystems int ret; 30627c478bd9Sstevel@tonic-gate 30637c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 30647c478bd9Sstevel@tonic-gate 306548847494SEnrico Perla - Sun Microsystems if (is_sparc()) 306648847494SEnrico Perla - Sun Microsystems set_flag(IS_SPARC_TARGET); 30677c478bd9Sstevel@tonic-gate 30687c478bd9Sstevel@tonic-gate /* 306948847494SEnrico Perla - Sun Microsystems * Check if cache directories and archives are present 30707c478bd9Sstevel@tonic-gate */ 307148847494SEnrico Perla - Sun Microsystems 307248847494SEnrico Perla - Sun Microsystems ret = check_flags_and_files(root); 307348847494SEnrico Perla - Sun Microsystems if (ret < 0) 307448847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 30757c478bd9Sstevel@tonic-gate 30767c478bd9Sstevel@tonic-gate /* 30777c478bd9Sstevel@tonic-gate * In certain deployment scenarios, filestat may not 3078eff168e0SEnrico Perla - Sun Microsystems * exist. Do not stop the boot process, but trigger an update 3079eff168e0SEnrico Perla - Sun Microsystems * of the archives (which will recreate filestat.ramdisk). 30807c478bd9Sstevel@tonic-gate */ 30817c478bd9Sstevel@tonic-gate if (bam_smf_check) { 30827c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 3083eff168e0SEnrico Perla - Sun Microsystems if (stat(path, &sb) != 0) { 3084eff168e0SEnrico Perla - Sun Microsystems (void) creat(NEED_UPDATE_FILE, 0644); 30857c478bd9Sstevel@tonic-gate return (0); 30867c478bd9Sstevel@tonic-gate } 3087eff168e0SEnrico Perla - Sun Microsystems } 30887c478bd9Sstevel@tonic-gate 30897c478bd9Sstevel@tonic-gate getoldstat(root); 30907c478bd9Sstevel@tonic-gate 30917c478bd9Sstevel@tonic-gate /* 3092a28d77b8Svikram * Check if the archive contains files that are no longer 3093a28d77b8Svikram * present on the root filesystem. 3094a28d77b8Svikram */ 3095a28d77b8Svikram check4stale(root); 3096a28d77b8Svikram 3097a28d77b8Svikram /* 30987c478bd9Sstevel@tonic-gate * read list of files 30997c478bd9Sstevel@tonic-gate */ 31007c478bd9Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 31017c478bd9Sstevel@tonic-gate clear_walk_args(); 31027c478bd9Sstevel@tonic-gate return (BAM_ERROR); 31037c478bd9Sstevel@tonic-gate } 31047c478bd9Sstevel@tonic-gate 31057c478bd9Sstevel@tonic-gate assert(flistp->head && flistp->tail); 31067c478bd9Sstevel@tonic-gate 31077c478bd9Sstevel@tonic-gate /* 31087c478bd9Sstevel@tonic-gate * At this point either the update is required 31097c478bd9Sstevel@tonic-gate * or the decision is pending. In either case 31107c478bd9Sstevel@tonic-gate * we need to create new stat nvlist 31117c478bd9Sstevel@tonic-gate */ 31127c478bd9Sstevel@tonic-gate create_newstat(); 31137c478bd9Sstevel@tonic-gate /* 31147c478bd9Sstevel@tonic-gate * This walk does 2 things: 31157c478bd9Sstevel@tonic-gate * - gets new stat data for every file 31167c478bd9Sstevel@tonic-gate * - (optional) compare old and new stat data 31177c478bd9Sstevel@tonic-gate */ 311848847494SEnrico Perla - Sun Microsystems ret = walk_list(root, &flist); 31197c478bd9Sstevel@tonic-gate 31207c478bd9Sstevel@tonic-gate /* done with the file list */ 31217c478bd9Sstevel@tonic-gate filelist_free(flistp); 31227c478bd9Sstevel@tonic-gate 312348847494SEnrico Perla - Sun Microsystems /* something went wrong */ 312448847494SEnrico Perla - Sun Microsystems 312548847494SEnrico Perla - Sun Microsystems if (ret == BAM_ERROR) { 312648847494SEnrico Perla - Sun Microsystems bam_error(CACHE_FAIL); 312748847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 31287c478bd9Sstevel@tonic-gate } 31297c478bd9Sstevel@tonic-gate 313048847494SEnrico Perla - Sun Microsystems if (walk_arg.new_nvlp == NULL) { 3131cedc7e57SEnrico Perla - Sun Microsystems if (walk_arg.sparcfile != NULL) 313248847494SEnrico Perla - Sun Microsystems (void) fclose(walk_arg.sparcfile); 313348847494SEnrico Perla - Sun Microsystems bam_error(NO_NEW_STAT); 313448847494SEnrico Perla - Sun Microsystems } 31357c478bd9Sstevel@tonic-gate 313648847494SEnrico Perla - Sun Microsystems /* If nothing was updated, discard newstat. */ 313748847494SEnrico Perla - Sun Microsystems 313848847494SEnrico Perla - Sun Microsystems if (!is_dir_flag_on(FILE32, NEED_UPDATE) && 313948847494SEnrico Perla - Sun Microsystems !is_dir_flag_on(FILE64, NEED_UPDATE)) { 31407c478bd9Sstevel@tonic-gate clear_walk_args(); 31417c478bd9Sstevel@tonic-gate return (0); 31427c478bd9Sstevel@tonic-gate } 31437c478bd9Sstevel@tonic-gate 3144cedc7e57SEnrico Perla - Sun Microsystems if (walk_arg.sparcfile != NULL) 314548847494SEnrico Perla - Sun Microsystems (void) fclose(walk_arg.sparcfile); 314648847494SEnrico Perla - Sun Microsystems 31477c478bd9Sstevel@tonic-gate return (1); 31487c478bd9Sstevel@tonic-gate } 31497c478bd9Sstevel@tonic-gate 315048847494SEnrico Perla - Sun Microsystems static int 315148847494SEnrico Perla - Sun Microsystems flushfs(char *root) 315248847494SEnrico Perla - Sun Microsystems { 315348847494SEnrico Perla - Sun Microsystems char cmd[PATH_MAX + 30]; 315448847494SEnrico Perla - Sun Microsystems 315548847494SEnrico Perla - Sun Microsystems (void) snprintf(cmd, sizeof (cmd), "%s -f \"%s\" 2>/dev/null", 315648847494SEnrico Perla - Sun Microsystems LOCKFS_PATH, root); 315748847494SEnrico Perla - Sun Microsystems 315848847494SEnrico Perla - Sun Microsystems return (exec_cmd(cmd, NULL)); 315948847494SEnrico Perla - Sun Microsystems } 316048847494SEnrico Perla - Sun Microsystems 316148847494SEnrico Perla - Sun Microsystems static int 316248847494SEnrico Perla - Sun Microsystems do_archive_copy(char *source, char *dest) 316348847494SEnrico Perla - Sun Microsystems { 316448847494SEnrico Perla - Sun Microsystems 3165cedc7e57SEnrico Perla - Sun Microsystems sync(); 316648847494SEnrico Perla - Sun Microsystems 316748847494SEnrico Perla - Sun Microsystems /* the equivalent of mv archive-new-$pid boot_archive */ 3168cedc7e57SEnrico Perla - Sun Microsystems if (rename(source, dest) != 0) { 3169cedc7e57SEnrico Perla - Sun Microsystems (void) unlink(source); 317048847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 3171cedc7e57SEnrico Perla - Sun Microsystems } 317248847494SEnrico Perla - Sun Microsystems 317348847494SEnrico Perla - Sun Microsystems if (flushfs(bam_root) != 0) 3174cedc7e57SEnrico Perla - Sun Microsystems sync(); 317548847494SEnrico Perla - Sun Microsystems 317648847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 317748847494SEnrico Perla - Sun Microsystems } 317848847494SEnrico Perla - Sun Microsystems 317948847494SEnrico Perla - Sun Microsystems static int 318048847494SEnrico Perla - Sun Microsystems check_cmdline(filelist_t flist) 318148847494SEnrico Perla - Sun Microsystems { 318248847494SEnrico Perla - Sun Microsystems line_t *lp; 318348847494SEnrico Perla - Sun Microsystems 318448847494SEnrico Perla - Sun Microsystems for (lp = flist.head; lp; lp = lp->next) { 318548847494SEnrico Perla - Sun Microsystems if (strstr(lp->line, "Error:") != NULL || 318648847494SEnrico Perla - Sun Microsystems strstr(lp->line, "Inode number overflow") != NULL) { 3187cedc7e57SEnrico Perla - Sun Microsystems (void) fprintf(stderr, "%s\n", lp->line); 318848847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 318948847494SEnrico Perla - Sun Microsystems } 319048847494SEnrico Perla - Sun Microsystems } 319148847494SEnrico Perla - Sun Microsystems 319248847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 319348847494SEnrico Perla - Sun Microsystems } 319448847494SEnrico Perla - Sun Microsystems 3195cedc7e57SEnrico Perla - Sun Microsystems static void 3196cedc7e57SEnrico Perla - Sun Microsystems dump_errormsg(filelist_t flist) 3197cedc7e57SEnrico Perla - Sun Microsystems { 3198cedc7e57SEnrico Perla - Sun Microsystems line_t *lp; 3199cedc7e57SEnrico Perla - Sun Microsystems 3200cedc7e57SEnrico Perla - Sun Microsystems for (lp = flist.head; lp; lp = lp->next) 3201cedc7e57SEnrico Perla - Sun Microsystems (void) fprintf(stderr, "%s\n", lp->line); 3202cedc7e57SEnrico Perla - Sun Microsystems } 3203cedc7e57SEnrico Perla - Sun Microsystems 320448847494SEnrico Perla - Sun Microsystems static int 320548847494SEnrico Perla - Sun Microsystems check_archive(char *dest) 320648847494SEnrico Perla - Sun Microsystems { 320748847494SEnrico Perla - Sun Microsystems struct stat sb; 320848847494SEnrico Perla - Sun Microsystems 320948847494SEnrico Perla - Sun Microsystems if (stat(dest, &sb) != 0 || !S_ISREG(sb.st_mode) || 321048847494SEnrico Perla - Sun Microsystems sb.st_size < 10000) { 321148847494SEnrico Perla - Sun Microsystems bam_error(ARCHIVE_BAD, dest); 321248847494SEnrico Perla - Sun Microsystems (void) unlink(dest); 321348847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 321448847494SEnrico Perla - Sun Microsystems } 321548847494SEnrico Perla - Sun Microsystems 321648847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 321748847494SEnrico Perla - Sun Microsystems } 321848847494SEnrico Perla - Sun Microsystems 32196debd3f5SAlexander Eremin static boolean_t 32206debd3f5SAlexander Eremin is_be(char *root) 32216debd3f5SAlexander Eremin { 32226debd3f5SAlexander Eremin zfs_handle_t *zhp; 32236debd3f5SAlexander Eremin libzfs_handle_t *hdl; 32246debd3f5SAlexander Eremin be_node_list_t *be_nodes = NULL; 32256debd3f5SAlexander Eremin be_node_list_t *cur_be; 32266debd3f5SAlexander Eremin boolean_t be_exist = B_FALSE; 322740a5c998SMatthew Ahrens char ds_path[ZFS_MAX_DATASET_NAME_LEN]; 32286debd3f5SAlexander Eremin 32296debd3f5SAlexander Eremin if (!is_zfs(root)) 32306debd3f5SAlexander Eremin return (B_FALSE); 32316debd3f5SAlexander Eremin /* 32326debd3f5SAlexander Eremin * Get dataset for mountpoint 32336debd3f5SAlexander Eremin */ 32346debd3f5SAlexander Eremin if ((hdl = libzfs_init()) == NULL) 32356debd3f5SAlexander Eremin return (B_FALSE); 32366debd3f5SAlexander Eremin 32376debd3f5SAlexander Eremin if ((zhp = zfs_path_to_zhandle(hdl, root, 32386debd3f5SAlexander Eremin ZFS_TYPE_FILESYSTEM)) == NULL) { 32396debd3f5SAlexander Eremin libzfs_fini(hdl); 32406debd3f5SAlexander Eremin return (B_FALSE); 32416debd3f5SAlexander Eremin } 32426debd3f5SAlexander Eremin 32436debd3f5SAlexander Eremin (void) strlcpy(ds_path, zfs_get_name(zhp), sizeof (ds_path)); 32446debd3f5SAlexander Eremin 32456debd3f5SAlexander Eremin /* 32466debd3f5SAlexander Eremin * Check if the current dataset is BE 32476debd3f5SAlexander Eremin */ 32486debd3f5SAlexander Eremin if (be_list(NULL, &be_nodes) == BE_SUCCESS) { 32496debd3f5SAlexander Eremin for (cur_be = be_nodes; cur_be != NULL; 32506debd3f5SAlexander Eremin cur_be = cur_be->be_next_node) { 32516debd3f5SAlexander Eremin 32526debd3f5SAlexander Eremin /* 32536debd3f5SAlexander Eremin * Because we guarantee that cur_be->be_root_ds 32546debd3f5SAlexander Eremin * is null-terminated by internal data structure, 32556debd3f5SAlexander Eremin * we can safely use strcmp() 32566debd3f5SAlexander Eremin */ 32576debd3f5SAlexander Eremin if (strcmp(ds_path, cur_be->be_root_ds) == 0) { 32586debd3f5SAlexander Eremin be_exist = B_TRUE; 32596debd3f5SAlexander Eremin break; 32606debd3f5SAlexander Eremin } 32616debd3f5SAlexander Eremin } 32626debd3f5SAlexander Eremin be_free_list(be_nodes); 32636debd3f5SAlexander Eremin } 32646debd3f5SAlexander Eremin zfs_close(zhp); 32656debd3f5SAlexander Eremin libzfs_fini(hdl); 32666debd3f5SAlexander Eremin 32676debd3f5SAlexander Eremin return (be_exist); 32686debd3f5SAlexander Eremin } 32696debd3f5SAlexander Eremin 327048847494SEnrico Perla - Sun Microsystems /* 327148847494SEnrico Perla - Sun Microsystems * Returns 1 if mkiso is in the expected PATH, 0 otherwise 327248847494SEnrico Perla - Sun Microsystems */ 327348847494SEnrico Perla - Sun Microsystems static int 327448847494SEnrico Perla - Sun Microsystems is_mkisofs() 327548847494SEnrico Perla - Sun Microsystems { 3276cedc7e57SEnrico Perla - Sun Microsystems if (access(MKISOFS_PATH, X_OK) == 0) 327748847494SEnrico Perla - Sun Microsystems return (1); 327848847494SEnrico Perla - Sun Microsystems return (0); 327948847494SEnrico Perla - Sun Microsystems } 328048847494SEnrico Perla - Sun Microsystems 328148847494SEnrico Perla - Sun Microsystems #define MKISO_PARAMS " -quiet -graft-points -dlrDJN -relaxed-filenames " 328248847494SEnrico Perla - Sun Microsystems 328348847494SEnrico Perla - Sun Microsystems static int 328448847494SEnrico Perla - Sun Microsystems create_sparc_archive(char *archive, char *tempname, char *bootblk, char *list) 328548847494SEnrico Perla - Sun Microsystems { 328648847494SEnrico Perla - Sun Microsystems int ret; 328748847494SEnrico Perla - Sun Microsystems char cmdline[3 * PATH_MAX + 64]; 328848847494SEnrico Perla - Sun Microsystems filelist_t flist = {0}; 328948847494SEnrico Perla - Sun Microsystems const char *func = "create_sparc_archive()"; 329048847494SEnrico Perla - Sun Microsystems 329148847494SEnrico Perla - Sun Microsystems if (access(bootblk, R_OK) == 1) { 329248847494SEnrico Perla - Sun Microsystems bam_error(BOOTBLK_FAIL, bootblk); 329348847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 329448847494SEnrico Perla - Sun Microsystems } 329548847494SEnrico Perla - Sun Microsystems 329648847494SEnrico Perla - Sun Microsystems /* 329748847494SEnrico Perla - Sun Microsystems * Prepare mkisofs command line and execute it 329848847494SEnrico Perla - Sun Microsystems */ 329948847494SEnrico Perla - Sun Microsystems (void) snprintf(cmdline, sizeof (cmdline), "%s %s -G %s -o \"%s\" " 3300cedc7e57SEnrico Perla - Sun Microsystems "-path-list \"%s\" 2>&1", MKISOFS_PATH, MKISO_PARAMS, bootblk, 330148847494SEnrico Perla - Sun Microsystems tempname, list); 330248847494SEnrico Perla - Sun Microsystems 330348847494SEnrico Perla - Sun Microsystems BAM_DPRINTF((D_CMDLINE, func, cmdline)); 330448847494SEnrico Perla - Sun Microsystems 330548847494SEnrico Perla - Sun Microsystems ret = exec_cmd(cmdline, &flist); 3306cedc7e57SEnrico Perla - Sun Microsystems if (ret != 0 || check_cmdline(flist) == BAM_ERROR) { 3307cedc7e57SEnrico Perla - Sun Microsystems dump_errormsg(flist); 330848847494SEnrico Perla - Sun Microsystems goto out_err; 3309cedc7e57SEnrico Perla - Sun Microsystems } 331048847494SEnrico Perla - Sun Microsystems 331148847494SEnrico Perla - Sun Microsystems filelist_free(&flist); 331248847494SEnrico Perla - Sun Microsystems 331348847494SEnrico Perla - Sun Microsystems /* 331448847494SEnrico Perla - Sun Microsystems * Prepare dd command line to copy the bootblk on the new archive and 331548847494SEnrico Perla - Sun Microsystems * execute it 331648847494SEnrico Perla - Sun Microsystems */ 331748847494SEnrico Perla - Sun Microsystems (void) snprintf(cmdline, sizeof (cmdline), "%s if=\"%s\" of=\"%s\"" 3318cedc7e57SEnrico Perla - Sun Microsystems " bs=1b oseek=1 count=15 conv=notrunc conv=sync 2>&1", DD_PATH_USR, 331948847494SEnrico Perla - Sun Microsystems bootblk, tempname); 332048847494SEnrico Perla - Sun Microsystems 332148847494SEnrico Perla - Sun Microsystems BAM_DPRINTF((D_CMDLINE, func, cmdline)); 332248847494SEnrico Perla - Sun Microsystems 332348847494SEnrico Perla - Sun Microsystems ret = exec_cmd(cmdline, &flist); 332448847494SEnrico Perla - Sun Microsystems if (ret != 0 || check_cmdline(flist) == BAM_ERROR) 332548847494SEnrico Perla - Sun Microsystems goto out_err; 332648847494SEnrico Perla - Sun Microsystems 332748847494SEnrico Perla - Sun Microsystems filelist_free(&flist); 332848847494SEnrico Perla - Sun Microsystems 332948847494SEnrico Perla - Sun Microsystems /* Did we get a valid archive ? */ 333048847494SEnrico Perla - Sun Microsystems if (check_archive(tempname) == BAM_ERROR) 333148847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 333248847494SEnrico Perla - Sun Microsystems 333348847494SEnrico Perla - Sun Microsystems return (do_archive_copy(tempname, archive)); 333448847494SEnrico Perla - Sun Microsystems 333548847494SEnrico Perla - Sun Microsystems out_err: 333648847494SEnrico Perla - Sun Microsystems filelist_free(&flist); 333748847494SEnrico Perla - Sun Microsystems bam_error(ARCHIVE_FAIL, cmdline); 333848847494SEnrico Perla - Sun Microsystems (void) unlink(tempname); 333948847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 334048847494SEnrico Perla - Sun Microsystems } 334148847494SEnrico Perla - Sun Microsystems 334248847494SEnrico Perla - Sun Microsystems static unsigned int 334348847494SEnrico Perla - Sun Microsystems from_733(unsigned char *s) 334448847494SEnrico Perla - Sun Microsystems { 334548847494SEnrico Perla - Sun Microsystems int i; 334648847494SEnrico Perla - Sun Microsystems unsigned int ret = 0; 334748847494SEnrico Perla - Sun Microsystems 334848847494SEnrico Perla - Sun Microsystems for (i = 0; i < 4; i++) 334948847494SEnrico Perla - Sun Microsystems ret |= s[i] << (8 * i); 335048847494SEnrico Perla - Sun Microsystems 335148847494SEnrico Perla - Sun Microsystems return (ret); 335248847494SEnrico Perla - Sun Microsystems } 335348847494SEnrico Perla - Sun Microsystems 335448847494SEnrico Perla - Sun Microsystems static void 335548847494SEnrico Perla - Sun Microsystems to_733(unsigned char *s, unsigned int val) 335648847494SEnrico Perla - Sun Microsystems { 335748847494SEnrico Perla - Sun Microsystems int i; 335848847494SEnrico Perla - Sun Microsystems 335948847494SEnrico Perla - Sun Microsystems for (i = 0; i < 4; i++) 336048847494SEnrico Perla - Sun Microsystems s[i] = s[7-i] = (val >> (8 * i)) & 0xFF; 336148847494SEnrico Perla - Sun Microsystems } 336248847494SEnrico Perla - Sun Microsystems 336348847494SEnrico Perla - Sun Microsystems /* 3364*eeb2c267SToomas Soome * creates sha1 hash of archive 3365*eeb2c267SToomas Soome */ 3366*eeb2c267SToomas Soome static int 3367*eeb2c267SToomas Soome digest_archive(const char *archive) 3368*eeb2c267SToomas Soome { 3369*eeb2c267SToomas Soome char *archive_hash; 3370*eeb2c267SToomas Soome char *hash; 3371*eeb2c267SToomas Soome int ret; 3372*eeb2c267SToomas Soome FILE *fp; 3373*eeb2c267SToomas Soome 3374*eeb2c267SToomas Soome (void) asprintf(&archive_hash, "%s.hash", archive); 3375*eeb2c267SToomas Soome if (archive_hash == NULL) 3376*eeb2c267SToomas Soome return (BAM_ERROR); 3377*eeb2c267SToomas Soome 3378*eeb2c267SToomas Soome if ((ret = bootadm_digest(archive, &hash)) == BAM_ERROR) { 3379*eeb2c267SToomas Soome free(archive_hash); 3380*eeb2c267SToomas Soome return (ret); 3381*eeb2c267SToomas Soome } 3382*eeb2c267SToomas Soome 3383*eeb2c267SToomas Soome fp = fopen(archive_hash, "w"); 3384*eeb2c267SToomas Soome if (fp == NULL) { 3385*eeb2c267SToomas Soome free(archive_hash); 3386*eeb2c267SToomas Soome free(hash); 3387*eeb2c267SToomas Soome return (BAM_ERROR); 3388*eeb2c267SToomas Soome } 3389*eeb2c267SToomas Soome 3390*eeb2c267SToomas Soome (void) fprintf(fp, "%s\n", hash); 3391*eeb2c267SToomas Soome (void) fclose(fp); 3392*eeb2c267SToomas Soome free(hash); 3393*eeb2c267SToomas Soome free(archive_hash); 3394*eeb2c267SToomas Soome return (BAM_SUCCESS); 3395*eeb2c267SToomas Soome } 3396*eeb2c267SToomas Soome 3397*eeb2c267SToomas Soome /* 339848847494SEnrico Perla - Sun Microsystems * Extends the current boot archive without recreating it from scratch 339948847494SEnrico Perla - Sun Microsystems */ 340048847494SEnrico Perla - Sun Microsystems static int 340148847494SEnrico Perla - Sun Microsystems extend_iso_archive(char *archive, char *tempname, char *update_dir) 340248847494SEnrico Perla - Sun Microsystems { 340348847494SEnrico Perla - Sun Microsystems int fd = -1, newfd = -1, ret, i; 340448847494SEnrico Perla - Sun Microsystems int next_session = 0, new_size = 0; 340548847494SEnrico Perla - Sun Microsystems char cmdline[3 * PATH_MAX + 64]; 340648847494SEnrico Perla - Sun Microsystems const char *func = "extend_iso_archive()"; 340748847494SEnrico Perla - Sun Microsystems filelist_t flist = {0}; 340848847494SEnrico Perla - Sun Microsystems struct iso_pdesc saved_desc[MAX_IVDs]; 340948847494SEnrico Perla - Sun Microsystems 341048847494SEnrico Perla - Sun Microsystems fd = open(archive, O_RDWR); 341148847494SEnrico Perla - Sun Microsystems if (fd == -1) { 3412cedc7e57SEnrico Perla - Sun Microsystems if (bam_verbose) 341348847494SEnrico Perla - Sun Microsystems bam_error(OPEN_FAIL, archive, strerror(errno)); 341448847494SEnrico Perla - Sun Microsystems goto out_err; 341548847494SEnrico Perla - Sun Microsystems } 341648847494SEnrico Perla - Sun Microsystems 341748847494SEnrico Perla - Sun Microsystems /* 341848847494SEnrico Perla - Sun Microsystems * A partial read is likely due to a corrupted file 341948847494SEnrico Perla - Sun Microsystems */ 342048847494SEnrico Perla - Sun Microsystems ret = pread64(fd, saved_desc, sizeof (saved_desc), 342148847494SEnrico Perla - Sun Microsystems VOLDESC_OFF * CD_BLOCK); 342248847494SEnrico Perla - Sun Microsystems if (ret != sizeof (saved_desc)) { 3423cedc7e57SEnrico Perla - Sun Microsystems if (bam_verbose) 342448847494SEnrico Perla - Sun Microsystems bam_error(READ_FAIL, archive, strerror(errno)); 342548847494SEnrico Perla - Sun Microsystems goto out_err; 342648847494SEnrico Perla - Sun Microsystems } 342748847494SEnrico Perla - Sun Microsystems 342848847494SEnrico Perla - Sun Microsystems if (memcmp(saved_desc[0].type, "\1CD001", 6)) { 3429cedc7e57SEnrico Perla - Sun Microsystems if (bam_verbose) 343048847494SEnrico Perla - Sun Microsystems bam_error(SIGN_FAIL, archive); 343148847494SEnrico Perla - Sun Microsystems goto out_err; 343248847494SEnrico Perla - Sun Microsystems } 343348847494SEnrico Perla - Sun Microsystems 343448847494SEnrico Perla - Sun Microsystems /* 343548847494SEnrico Perla - Sun Microsystems * Read primary descriptor and locate next_session offset (it should 343648847494SEnrico Perla - Sun Microsystems * point to the end of the archive) 343748847494SEnrico Perla - Sun Microsystems */ 343848847494SEnrico Perla - Sun Microsystems next_session = P2ROUNDUP(from_733(saved_desc[0].volume_space_size), 16); 343948847494SEnrico Perla - Sun Microsystems 344048847494SEnrico Perla - Sun Microsystems (void) snprintf(cmdline, sizeof (cmdline), "%s -C 16,%d -M %s %s -o \"" 3441cedc7e57SEnrico Perla - Sun Microsystems "%s\" \"%s\" 2>&1", MKISOFS_PATH, next_session, archive, 3442cedc7e57SEnrico Perla - Sun Microsystems MKISO_PARAMS, tempname, update_dir); 344348847494SEnrico Perla - Sun Microsystems 344448847494SEnrico Perla - Sun Microsystems BAM_DPRINTF((D_CMDLINE, func, cmdline)); 344548847494SEnrico Perla - Sun Microsystems 344648847494SEnrico Perla - Sun Microsystems ret = exec_cmd(cmdline, &flist); 344748847494SEnrico Perla - Sun Microsystems if (ret != 0 || check_cmdline(flist) == BAM_ERROR) { 3448cedc7e57SEnrico Perla - Sun Microsystems if (bam_verbose) { 344948847494SEnrico Perla - Sun Microsystems bam_error(MULTI_FAIL, cmdline); 3450cedc7e57SEnrico Perla - Sun Microsystems dump_errormsg(flist); 3451cedc7e57SEnrico Perla - Sun Microsystems } 345248847494SEnrico Perla - Sun Microsystems goto out_flist_err; 345348847494SEnrico Perla - Sun Microsystems } 345448847494SEnrico Perla - Sun Microsystems filelist_free(&flist); 345548847494SEnrico Perla - Sun Microsystems 345648847494SEnrico Perla - Sun Microsystems newfd = open(tempname, O_RDONLY); 345748847494SEnrico Perla - Sun Microsystems if (newfd == -1) { 3458cedc7e57SEnrico Perla - Sun Microsystems if (bam_verbose) 345948847494SEnrico Perla - Sun Microsystems bam_error(OPEN_FAIL, archive, strerror(errno)); 346048847494SEnrico Perla - Sun Microsystems goto out_err; 346148847494SEnrico Perla - Sun Microsystems } 346248847494SEnrico Perla - Sun Microsystems 346348847494SEnrico Perla - Sun Microsystems ret = pread64(newfd, saved_desc, sizeof (saved_desc), 346448847494SEnrico Perla - Sun Microsystems VOLDESC_OFF * CD_BLOCK); 346548847494SEnrico Perla - Sun Microsystems if (ret != sizeof (saved_desc)) { 3466cedc7e57SEnrico Perla - Sun Microsystems if (bam_verbose) 346748847494SEnrico Perla - Sun Microsystems bam_error(READ_FAIL, archive, strerror(errno)); 346848847494SEnrico Perla - Sun Microsystems goto out_err; 346948847494SEnrico Perla - Sun Microsystems } 347048847494SEnrico Perla - Sun Microsystems 347148847494SEnrico Perla - Sun Microsystems if (memcmp(saved_desc[0].type, "\1CD001", 6)) { 3472cedc7e57SEnrico Perla - Sun Microsystems if (bam_verbose) 347348847494SEnrico Perla - Sun Microsystems bam_error(SIGN_FAIL, archive); 347448847494SEnrico Perla - Sun Microsystems goto out_err; 347548847494SEnrico Perla - Sun Microsystems } 347648847494SEnrico Perla - Sun Microsystems 347748847494SEnrico Perla - Sun Microsystems new_size = from_733(saved_desc[0].volume_space_size) + next_session; 347848847494SEnrico Perla - Sun Microsystems to_733(saved_desc[0].volume_space_size, new_size); 347948847494SEnrico Perla - Sun Microsystems 348048847494SEnrico Perla - Sun Microsystems for (i = 1; i < MAX_IVDs; i++) { 348148847494SEnrico Perla - Sun Microsystems if (saved_desc[i].type[0] == (unsigned char)255) 348248847494SEnrico Perla - Sun Microsystems break; 348348847494SEnrico Perla - Sun Microsystems if (memcmp(saved_desc[i].id, "CD001", 5)) 348448847494SEnrico Perla - Sun Microsystems break; 348548847494SEnrico Perla - Sun Microsystems 348648847494SEnrico Perla - Sun Microsystems if (bam_verbose) 348748847494SEnrico Perla - Sun Microsystems bam_print("%s: Updating descriptor entry [%d]\n", func, 348848847494SEnrico Perla - Sun Microsystems i); 348948847494SEnrico Perla - Sun Microsystems 349048847494SEnrico Perla - Sun Microsystems to_733(saved_desc[i].volume_space_size, new_size); 349148847494SEnrico Perla - Sun Microsystems } 349248847494SEnrico Perla - Sun Microsystems 349348847494SEnrico Perla - Sun Microsystems ret = pwrite64(fd, saved_desc, DVD_BLOCK, VOLDESC_OFF*CD_BLOCK); 349448847494SEnrico Perla - Sun Microsystems if (ret != DVD_BLOCK) { 3495cedc7e57SEnrico Perla - Sun Microsystems if (bam_verbose) 349648847494SEnrico Perla - Sun Microsystems bam_error(WRITE_FAIL, archive, strerror(errno)); 349748847494SEnrico Perla - Sun Microsystems goto out_err; 349848847494SEnrico Perla - Sun Microsystems } 349948847494SEnrico Perla - Sun Microsystems (void) close(newfd); 350048847494SEnrico Perla - Sun Microsystems newfd = -1; 350148847494SEnrico Perla - Sun Microsystems 3502cedc7e57SEnrico Perla - Sun Microsystems ret = fsync(fd); 3503cedc7e57SEnrico Perla - Sun Microsystems if (ret != 0) 3504cedc7e57SEnrico Perla - Sun Microsystems sync(); 3505cedc7e57SEnrico Perla - Sun Microsystems 350648847494SEnrico Perla - Sun Microsystems ret = close(fd); 350748847494SEnrico Perla - Sun Microsystems if (ret != 0) { 3508cedc7e57SEnrico Perla - Sun Microsystems if (bam_verbose) 350948847494SEnrico Perla - Sun Microsystems bam_error(CLOSE_FAIL, archive, strerror(errno)); 351048847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 351148847494SEnrico Perla - Sun Microsystems } 351248847494SEnrico Perla - Sun Microsystems fd = -1; 351348847494SEnrico Perla - Sun Microsystems 351448847494SEnrico Perla - Sun Microsystems (void) snprintf(cmdline, sizeof (cmdline), "%s if=%s of=%s bs=32k " 3515cedc7e57SEnrico Perla - Sun Microsystems "seek=%d conv=sync 2>&1", DD_PATH_USR, tempname, archive, 351648847494SEnrico Perla - Sun Microsystems (next_session/16)); 351748847494SEnrico Perla - Sun Microsystems 351848847494SEnrico Perla - Sun Microsystems BAM_DPRINTF((D_CMDLINE, func, cmdline)); 351948847494SEnrico Perla - Sun Microsystems 352048847494SEnrico Perla - Sun Microsystems ret = exec_cmd(cmdline, &flist); 352148847494SEnrico Perla - Sun Microsystems if (ret != 0 || check_cmdline(flist) == BAM_ERROR) { 3522cedc7e57SEnrico Perla - Sun Microsystems if (bam_verbose) 352348847494SEnrico Perla - Sun Microsystems bam_error(MULTI_FAIL, cmdline); 352448847494SEnrico Perla - Sun Microsystems goto out_flist_err; 352548847494SEnrico Perla - Sun Microsystems } 352648847494SEnrico Perla - Sun Microsystems filelist_free(&flist); 352748847494SEnrico Perla - Sun Microsystems 352848847494SEnrico Perla - Sun Microsystems (void) unlink(tempname); 352948847494SEnrico Perla - Sun Microsystems 3530*eeb2c267SToomas Soome if (digest_archive(archive) == BAM_ERROR && bam_verbose) 3531*eeb2c267SToomas Soome bam_print("boot archive hashing failed\n"); 3532*eeb2c267SToomas Soome 3533cedc7e57SEnrico Perla - Sun Microsystems if (flushfs(bam_root) != 0) 3534cedc7e57SEnrico Perla - Sun Microsystems sync(); 3535cedc7e57SEnrico Perla - Sun Microsystems 353648847494SEnrico Perla - Sun Microsystems if (bam_verbose) 353748847494SEnrico Perla - Sun Microsystems bam_print("boot archive updated successfully\n"); 353848847494SEnrico Perla - Sun Microsystems 353948847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 354048847494SEnrico Perla - Sun Microsystems 354148847494SEnrico Perla - Sun Microsystems out_flist_err: 354248847494SEnrico Perla - Sun Microsystems filelist_free(&flist); 354348847494SEnrico Perla - Sun Microsystems out_err: 354448847494SEnrico Perla - Sun Microsystems if (fd != -1) 354548847494SEnrico Perla - Sun Microsystems (void) close(fd); 354648847494SEnrico Perla - Sun Microsystems if (newfd != -1) 354748847494SEnrico Perla - Sun Microsystems (void) close(newfd); 354848847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 354948847494SEnrico Perla - Sun Microsystems } 355048847494SEnrico Perla - Sun Microsystems 355148847494SEnrico Perla - Sun Microsystems static int 355248847494SEnrico Perla - Sun Microsystems create_x86_archive(char *archive, char *tempname, char *update_dir) 355348847494SEnrico Perla - Sun Microsystems { 355448847494SEnrico Perla - Sun Microsystems int ret; 355548847494SEnrico Perla - Sun Microsystems char cmdline[3 * PATH_MAX + 64]; 355648847494SEnrico Perla - Sun Microsystems filelist_t flist = {0}; 355748847494SEnrico Perla - Sun Microsystems const char *func = "create_x86_archive()"; 355848847494SEnrico Perla - Sun Microsystems 355948847494SEnrico Perla - Sun Microsystems (void) snprintf(cmdline, sizeof (cmdline), "%s %s -o \"%s\" \"%s\" " 3560cedc7e57SEnrico Perla - Sun Microsystems "2>&1", MKISOFS_PATH, MKISO_PARAMS, tempname, update_dir); 356148847494SEnrico Perla - Sun Microsystems 356248847494SEnrico Perla - Sun Microsystems BAM_DPRINTF((D_CMDLINE, func, cmdline)); 356348847494SEnrico Perla - Sun Microsystems 356448847494SEnrico Perla - Sun Microsystems ret = exec_cmd(cmdline, &flist); 356548847494SEnrico Perla - Sun Microsystems if (ret != 0 || check_cmdline(flist) == BAM_ERROR) { 356648847494SEnrico Perla - Sun Microsystems bam_error(ARCHIVE_FAIL, cmdline); 3567cedc7e57SEnrico Perla - Sun Microsystems dump_errormsg(flist); 356848847494SEnrico Perla - Sun Microsystems filelist_free(&flist); 356948847494SEnrico Perla - Sun Microsystems (void) unlink(tempname); 357048847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 357148847494SEnrico Perla - Sun Microsystems } 357248847494SEnrico Perla - Sun Microsystems 357348847494SEnrico Perla - Sun Microsystems filelist_free(&flist); 357448847494SEnrico Perla - Sun Microsystems 357548847494SEnrico Perla - Sun Microsystems if (check_archive(tempname) == BAM_ERROR) 357648847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 357748847494SEnrico Perla - Sun Microsystems 357848847494SEnrico Perla - Sun Microsystems return (do_archive_copy(tempname, archive)); 357948847494SEnrico Perla - Sun Microsystems } 358048847494SEnrico Perla - Sun Microsystems 358148847494SEnrico Perla - Sun Microsystems static int 358248847494SEnrico Perla - Sun Microsystems mkisofs_archive(char *root, int what) 358348847494SEnrico Perla - Sun Microsystems { 358448847494SEnrico Perla - Sun Microsystems int ret; 358548847494SEnrico Perla - Sun Microsystems char temp[PATH_MAX]; 358648847494SEnrico Perla - Sun Microsystems char bootblk[PATH_MAX]; 358748847494SEnrico Perla - Sun Microsystems char boot_archive[PATH_MAX]; 358848847494SEnrico Perla - Sun Microsystems 358948847494SEnrico Perla - Sun Microsystems if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET)) 359048847494SEnrico Perla - Sun Microsystems ret = snprintf(temp, sizeof (temp), 359148847494SEnrico Perla - Sun Microsystems "%s%s%s/amd64/archive-new-%d", root, ARCHIVE_PREFIX, 359248847494SEnrico Perla - Sun Microsystems get_machine(), getpid()); 359348847494SEnrico Perla - Sun Microsystems else 359448847494SEnrico Perla - Sun Microsystems ret = snprintf(temp, sizeof (temp), "%s%s%s/archive-new-%d", 359548847494SEnrico Perla - Sun Microsystems root, ARCHIVE_PREFIX, get_machine(), getpid()); 359648847494SEnrico Perla - Sun Microsystems 359748847494SEnrico Perla - Sun Microsystems if (ret >= sizeof (temp)) 359848847494SEnrico Perla - Sun Microsystems goto out_path_err; 359948847494SEnrico Perla - Sun Microsystems 360048847494SEnrico Perla - Sun Microsystems if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET)) 360148847494SEnrico Perla - Sun Microsystems ret = snprintf(boot_archive, sizeof (boot_archive), 360248847494SEnrico Perla - Sun Microsystems "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(), 360348847494SEnrico Perla - Sun Microsystems ARCHIVE_SUFFIX); 360448847494SEnrico Perla - Sun Microsystems else 360548847494SEnrico Perla - Sun Microsystems ret = snprintf(boot_archive, sizeof (boot_archive), 360648847494SEnrico Perla - Sun Microsystems "%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(), 360748847494SEnrico Perla - Sun Microsystems ARCHIVE_SUFFIX); 360848847494SEnrico Perla - Sun Microsystems 360948847494SEnrico Perla - Sun Microsystems if (ret >= sizeof (boot_archive)) 361048847494SEnrico Perla - Sun Microsystems goto out_path_err; 361148847494SEnrico Perla - Sun Microsystems 361248847494SEnrico Perla - Sun Microsystems bam_print("updating %s\n", boot_archive); 361348847494SEnrico Perla - Sun Microsystems 361448847494SEnrico Perla - Sun Microsystems if (is_flag_on(IS_SPARC_TARGET)) { 361548847494SEnrico Perla - Sun Microsystems ret = snprintf(bootblk, sizeof (bootblk), 361648847494SEnrico Perla - Sun Microsystems "%s/platform/%s/lib/fs/hsfs/bootblk", root, get_machine()); 361748847494SEnrico Perla - Sun Microsystems if (ret >= sizeof (bootblk)) 361848847494SEnrico Perla - Sun Microsystems goto out_path_err; 361948847494SEnrico Perla - Sun Microsystems 362048847494SEnrico Perla - Sun Microsystems ret = create_sparc_archive(boot_archive, temp, bootblk, 362148847494SEnrico Perla - Sun Microsystems get_cachedir(what)); 362248847494SEnrico Perla - Sun Microsystems } else { 362348847494SEnrico Perla - Sun Microsystems if (!is_dir_flag_on(what, NO_MULTI)) { 362448847494SEnrico Perla - Sun Microsystems if (bam_verbose) 362548847494SEnrico Perla - Sun Microsystems bam_print("Attempting to extend x86 archive: " 362648847494SEnrico Perla - Sun Microsystems "%s\n", boot_archive); 362748847494SEnrico Perla - Sun Microsystems 362848847494SEnrico Perla - Sun Microsystems ret = extend_iso_archive(boot_archive, temp, 362948847494SEnrico Perla - Sun Microsystems get_updatedir(what)); 363048847494SEnrico Perla - Sun Microsystems if (ret == BAM_SUCCESS) { 363148847494SEnrico Perla - Sun Microsystems if (bam_verbose) 363248847494SEnrico Perla - Sun Microsystems bam_print("Successfully extended %s\n", 363348847494SEnrico Perla - Sun Microsystems boot_archive); 363448847494SEnrico Perla - Sun Microsystems 363548847494SEnrico Perla - Sun Microsystems (void) rmdir_r(get_updatedir(what)); 363648847494SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 363748847494SEnrico Perla - Sun Microsystems } 363848847494SEnrico Perla - Sun Microsystems } 363948847494SEnrico Perla - Sun Microsystems /* 364048847494SEnrico Perla - Sun Microsystems * The boot archive will be recreated from scratch. We get here 364148847494SEnrico Perla - Sun Microsystems * if at least one of these conditions is true: 364248847494SEnrico Perla - Sun Microsystems * - bootadm was called without the -e switch 364348847494SEnrico Perla - Sun Microsystems * - the archive (or the archive cache) doesn't exist 364448847494SEnrico Perla - Sun Microsystems * - archive size is bigger than BA_SIZE_MAX 364548847494SEnrico Perla - Sun Microsystems * - more than COUNT_MAX files need to be updated 364648847494SEnrico Perla - Sun Microsystems * - an error occourred either populating the /updates directory 364748847494SEnrico Perla - Sun Microsystems * or extend_iso_archive() failed 364848847494SEnrico Perla - Sun Microsystems */ 364948847494SEnrico Perla - Sun Microsystems if (bam_verbose) 365048847494SEnrico Perla - Sun Microsystems bam_print("Unable to extend %s... rebuilding archive\n", 365148847494SEnrico Perla - Sun Microsystems boot_archive); 365248847494SEnrico Perla - Sun Microsystems 365348847494SEnrico Perla - Sun Microsystems if (get_updatedir(what)[0] != '\0') 365448847494SEnrico Perla - Sun Microsystems (void) rmdir_r(get_updatedir(what)); 365548847494SEnrico Perla - Sun Microsystems 365648847494SEnrico Perla - Sun Microsystems 365748847494SEnrico Perla - Sun Microsystems ret = create_x86_archive(boot_archive, temp, 365848847494SEnrico Perla - Sun Microsystems get_cachedir(what)); 365948847494SEnrico Perla - Sun Microsystems } 366048847494SEnrico Perla - Sun Microsystems 3661*eeb2c267SToomas Soome if (digest_archive(boot_archive) == BAM_ERROR && bam_verbose) 3662*eeb2c267SToomas Soome bam_print("boot archive hashing failed\n"); 3663*eeb2c267SToomas Soome 366448847494SEnrico Perla - Sun Microsystems if (ret == BAM_SUCCESS && bam_verbose) 366548847494SEnrico Perla - Sun Microsystems bam_print("Successfully created %s\n", boot_archive); 366648847494SEnrico Perla - Sun Microsystems 366748847494SEnrico Perla - Sun Microsystems return (ret); 366848847494SEnrico Perla - Sun Microsystems 366948847494SEnrico Perla - Sun Microsystems out_path_err: 367048847494SEnrico Perla - Sun Microsystems bam_error(PATH_TOO_LONG, root); 367148847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 367248847494SEnrico Perla - Sun Microsystems } 367348847494SEnrico Perla - Sun Microsystems 36747c478bd9Sstevel@tonic-gate static error_t 36757c478bd9Sstevel@tonic-gate create_ramdisk(char *root) 36767c478bd9Sstevel@tonic-gate { 36777c478bd9Sstevel@tonic-gate char *cmdline, path[PATH_MAX]; 36787c478bd9Sstevel@tonic-gate size_t len; 36797c478bd9Sstevel@tonic-gate struct stat sb; 3680cedc7e57SEnrico Perla - Sun Microsystems int ret, what, status = BAM_SUCCESS; 368148847494SEnrico Perla - Sun Microsystems 368248847494SEnrico Perla - Sun Microsystems /* If there is mkisofs, use it to create the required archives */ 368348847494SEnrico Perla - Sun Microsystems if (is_mkisofs()) { 368448847494SEnrico Perla - Sun Microsystems for (what = FILE32; what < CACHEDIR_NUM; what++) { 3685cedc7e57SEnrico Perla - Sun Microsystems if (has_cachedir(what) && is_dir_flag_on(what, 3686cedc7e57SEnrico Perla - Sun Microsystems NEED_UPDATE)) { 368748847494SEnrico Perla - Sun Microsystems ret = mkisofs_archive(root, what); 368848847494SEnrico Perla - Sun Microsystems if (ret != 0) 3689cedc7e57SEnrico Perla - Sun Microsystems status = BAM_ERROR; 369048847494SEnrico Perla - Sun Microsystems } 369148847494SEnrico Perla - Sun Microsystems } 3692cedc7e57SEnrico Perla - Sun Microsystems return (status); 369348847494SEnrico Perla - Sun Microsystems } 36947c478bd9Sstevel@tonic-gate 36957c478bd9Sstevel@tonic-gate /* 369648847494SEnrico Perla - Sun Microsystems * Else setup command args for create_ramdisk.ksh for the UFS archives 3697*eeb2c267SToomas Soome * Note: we will not create hash here, CREATE_RAMDISK should create it. 36987c478bd9Sstevel@tonic-gate */ 369948847494SEnrico Perla - Sun Microsystems if (bam_verbose) 370048847494SEnrico Perla - Sun Microsystems bam_print("mkisofs not found, creating UFS archive\n"); 370148847494SEnrico Perla - Sun Microsystems 3702986fd29aSsetje (void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK); 37037c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 37047c478bd9Sstevel@tonic-gate bam_error(ARCH_EXEC_MISS, path, strerror(errno)); 37057c478bd9Sstevel@tonic-gate return (BAM_ERROR); 37067c478bd9Sstevel@tonic-gate } 37077c478bd9Sstevel@tonic-gate 3708cedc7e57SEnrico Perla - Sun Microsystems if (is_safe_exec(path) == BAM_ERROR) 3709cedc7e57SEnrico Perla - Sun Microsystems return (BAM_ERROR); 3710cedc7e57SEnrico Perla - Sun Microsystems 37117c478bd9Sstevel@tonic-gate len = strlen(path) + strlen(root) + 10; /* room for space + -R */ 3712d876c67dSjg if (bam_alt_platform) 3713d876c67dSjg len += strlen(bam_platform) + strlen("-p "); 37147c478bd9Sstevel@tonic-gate cmdline = s_calloc(1, len); 37157c478bd9Sstevel@tonic-gate 3716d876c67dSjg if (bam_alt_platform) { 3717d876c67dSjg assert(strlen(root) > 1); 3718d876c67dSjg (void) snprintf(cmdline, len, "%s -p %s -R %s", 3719d876c67dSjg path, bam_platform, root); 3720d876c67dSjg /* chop off / at the end */ 3721d876c67dSjg cmdline[strlen(cmdline) - 1] = '\0'; 3722d876c67dSjg } else if (strlen(root) > 1) { 37237c478bd9Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s -R %s", path, root); 37247c478bd9Sstevel@tonic-gate /* chop off / at the end */ 37257c478bd9Sstevel@tonic-gate cmdline[strlen(cmdline) - 1] = '\0'; 37267c478bd9Sstevel@tonic-gate } else 37277c478bd9Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s", path); 37287c478bd9Sstevel@tonic-gate 3729986fd29aSsetje if (exec_cmd(cmdline, NULL) != 0) { 37307c478bd9Sstevel@tonic-gate bam_error(ARCHIVE_FAIL, cmdline); 37317c478bd9Sstevel@tonic-gate free(cmdline); 37327c478bd9Sstevel@tonic-gate return (BAM_ERROR); 37337c478bd9Sstevel@tonic-gate } 37347c478bd9Sstevel@tonic-gate free(cmdline); 37357c478bd9Sstevel@tonic-gate /* 3736986fd29aSsetje * The existence of the expected archives used to be 3737986fd29aSsetje * verified here. This check is done in create_ramdisk as 3738986fd29aSsetje * it needs to be in sync with the altroot operated upon. 37397c478bd9Sstevel@tonic-gate */ 37407c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 37417c478bd9Sstevel@tonic-gate } 37427c478bd9Sstevel@tonic-gate 37437c478bd9Sstevel@tonic-gate /* 37447c478bd9Sstevel@tonic-gate * Checks if target filesystem is on a ramdisk 37457c478bd9Sstevel@tonic-gate * 1 - is miniroot 37467c478bd9Sstevel@tonic-gate * 0 - is not 37477c478bd9Sstevel@tonic-gate * When in doubt assume it is not a ramdisk. 37487c478bd9Sstevel@tonic-gate */ 37497c478bd9Sstevel@tonic-gate static int 37507c478bd9Sstevel@tonic-gate is_ramdisk(char *root) 37517c478bd9Sstevel@tonic-gate { 37527c478bd9Sstevel@tonic-gate struct extmnttab mnt; 37537c478bd9Sstevel@tonic-gate FILE *fp; 37547c478bd9Sstevel@tonic-gate int found; 3755b610f78eSvikram char mntpt[PATH_MAX]; 3756b610f78eSvikram char *cp; 37577c478bd9Sstevel@tonic-gate 37587c478bd9Sstevel@tonic-gate /* 37597c478bd9Sstevel@tonic-gate * There are 3 situations where creating archive is 37607c478bd9Sstevel@tonic-gate * of dubious value: 3761b610f78eSvikram * - create boot_archive on a lofi-mounted boot_archive 37627c478bd9Sstevel@tonic-gate * - create it on a ramdisk which is the root filesystem 37637c478bd9Sstevel@tonic-gate * - create it on a ramdisk mounted somewhere else 37647c478bd9Sstevel@tonic-gate * The first is not easy to detect and checking for it is not 37657c478bd9Sstevel@tonic-gate * worth it. 37667c478bd9Sstevel@tonic-gate * The other two conditions are handled here 37677c478bd9Sstevel@tonic-gate */ 37687c478bd9Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 37697c478bd9Sstevel@tonic-gate if (fp == NULL) { 37707c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 37717c478bd9Sstevel@tonic-gate return (0); 37727c478bd9Sstevel@tonic-gate } 37737c478bd9Sstevel@tonic-gate 37747c478bd9Sstevel@tonic-gate resetmnttab(fp); 37757c478bd9Sstevel@tonic-gate 3776b610f78eSvikram /* 3777b610f78eSvikram * Remove any trailing / from the mount point 3778b610f78eSvikram */ 3779b610f78eSvikram (void) strlcpy(mntpt, root, sizeof (mntpt)); 3780b610f78eSvikram if (strcmp(root, "/") != 0) { 3781b610f78eSvikram cp = mntpt + strlen(mntpt) - 1; 3782b610f78eSvikram if (*cp == '/') 3783b610f78eSvikram *cp = '\0'; 3784b610f78eSvikram } 37857c478bd9Sstevel@tonic-gate found = 0; 37867c478bd9Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 3787b610f78eSvikram if (strcmp(mnt.mnt_mountp, mntpt) == 0) { 37887c478bd9Sstevel@tonic-gate found = 1; 37897c478bd9Sstevel@tonic-gate break; 37907c478bd9Sstevel@tonic-gate } 37917c478bd9Sstevel@tonic-gate } 37927c478bd9Sstevel@tonic-gate 37937c478bd9Sstevel@tonic-gate if (!found) { 37947c478bd9Sstevel@tonic-gate if (bam_verbose) 3795b610f78eSvikram bam_error(NOT_IN_MNTTAB, mntpt); 37967c478bd9Sstevel@tonic-gate (void) fclose(fp); 37977c478bd9Sstevel@tonic-gate return (0); 37987c478bd9Sstevel@tonic-gate } 37997c478bd9Sstevel@tonic-gate 380008db0dbcSAlexander Eremin if (strncmp(mnt.mnt_special, RAMDISK_SPECIAL, 380108db0dbcSAlexander Eremin strlen(RAMDISK_SPECIAL)) == 0) { 38027c478bd9Sstevel@tonic-gate if (bam_verbose) 38037c478bd9Sstevel@tonic-gate bam_error(IS_RAMDISK, bam_root); 38047c478bd9Sstevel@tonic-gate (void) fclose(fp); 38057c478bd9Sstevel@tonic-gate return (1); 38067c478bd9Sstevel@tonic-gate } 38077c478bd9Sstevel@tonic-gate 38087c478bd9Sstevel@tonic-gate (void) fclose(fp); 38097c478bd9Sstevel@tonic-gate 38107c478bd9Sstevel@tonic-gate return (0); 38117c478bd9Sstevel@tonic-gate } 38127c478bd9Sstevel@tonic-gate 38137c478bd9Sstevel@tonic-gate static int 3814986fd29aSsetje is_boot_archive(char *root) 38157c478bd9Sstevel@tonic-gate { 38167c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 38177c478bd9Sstevel@tonic-gate struct stat sb; 3818eb2bd662Svikram int error; 3819eb2bd662Svikram const char *fcn = "is_boot_archive()"; 38207c478bd9Sstevel@tonic-gate 38217c478bd9Sstevel@tonic-gate /* 3822986fd29aSsetje * We can't create an archive without the create_ramdisk script 38237c478bd9Sstevel@tonic-gate */ 3824986fd29aSsetje (void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK); 3825eb2bd662Svikram error = stat(path, &sb); 3826eb2bd662Svikram INJECT_ERROR1("NOT_ARCHIVE_BASED", error = -1); 3827eb2bd662Svikram if (error == -1) { 38287c478bd9Sstevel@tonic-gate if (bam_verbose) 38297c478bd9Sstevel@tonic-gate bam_print(FILE_MISS, path); 3830eb2bd662Svikram BAM_DPRINTF((D_NOT_ARCHIVE_BOOT, fcn, root)); 38317c478bd9Sstevel@tonic-gate return (0); 38327c478bd9Sstevel@tonic-gate } 38337c478bd9Sstevel@tonic-gate 3834eb2bd662Svikram BAM_DPRINTF((D_IS_ARCHIVE_BOOT, fcn, root)); 3835986fd29aSsetje return (1); 3836986fd29aSsetje } 3837986fd29aSsetje 38387c478bd9Sstevel@tonic-gate /* 3839986fd29aSsetje * Need to call this for anything that operates on the GRUB menu 3840963390b4Svikram * In the x86 live upgrade case the directory /boot/grub may be present 3841963390b4Svikram * even on pre-newboot BEs. The authoritative way to check for a GRUB target 3842963390b4Svikram * is to check for the presence of the stage2 binary which is present 3843963390b4Svikram * only on GRUB targets (even on x86 boot partitions). Checking for the 3844963390b4Svikram * presence of the multiboot binary is not correct as it is not present 3845963390b4Svikram * on x86 boot partitions. 3846986fd29aSsetje */ 3847986fd29aSsetje int 3848986fd29aSsetje is_grub(const char *root) 3849986fd29aSsetje { 3850986fd29aSsetje char path[PATH_MAX]; 3851986fd29aSsetje struct stat sb; 3852eb2bd662Svikram const char *fcn = "is_grub()"; 3853986fd29aSsetje 3854963390b4Svikram (void) snprintf(path, sizeof (path), "%s%s", root, GRUB_STAGE2); 38557c478bd9Sstevel@tonic-gate if (stat(path, &sb) == -1) { 3856eb2bd662Svikram BAM_DPRINTF((D_NO_GRUB_DIR, fcn, path)); 38577c478bd9Sstevel@tonic-gate return (0); 38587c478bd9Sstevel@tonic-gate } 38597c478bd9Sstevel@tonic-gate 38607c478bd9Sstevel@tonic-gate return (1); 38617c478bd9Sstevel@tonic-gate } 38627c478bd9Sstevel@tonic-gate 38637c478bd9Sstevel@tonic-gate static int 3864eb2bd662Svikram is_zfs(char *root) 3865eb2bd662Svikram { 3866eb2bd662Svikram struct statvfs vfs; 3867eb2bd662Svikram int ret; 3868eb2bd662Svikram const char *fcn = "is_zfs()"; 3869eb2bd662Svikram 3870eb2bd662Svikram ret = statvfs(root, &vfs); 3871eb2bd662Svikram INJECT_ERROR1("STATVFS_ZFS", ret = 1); 3872eb2bd662Svikram if (ret != 0) { 3873eb2bd662Svikram bam_error(STATVFS_FAIL, root, strerror(errno)); 3874eb2bd662Svikram return (0); 3875eb2bd662Svikram } 3876eb2bd662Svikram 3877eb2bd662Svikram if (strncmp(vfs.f_basetype, "zfs", strlen("zfs")) == 0) { 3878eb2bd662Svikram BAM_DPRINTF((D_IS_ZFS, fcn, root)); 3879eb2bd662Svikram return (1); 3880eb2bd662Svikram } else { 3881eb2bd662Svikram BAM_DPRINTF((D_IS_NOT_ZFS, fcn, root)); 3882eb2bd662Svikram return (0); 3883eb2bd662Svikram } 3884eb2bd662Svikram } 3885eb2bd662Svikram 3886eb2bd662Svikram static int 3887eb2bd662Svikram is_pcfs(char *root) 3888eb2bd662Svikram { 3889eb2bd662Svikram struct statvfs vfs; 3890eb2bd662Svikram int ret; 3891eb2bd662Svikram const char *fcn = "is_pcfs()"; 3892eb2bd662Svikram 3893eb2bd662Svikram ret = statvfs(root, &vfs); 3894eb2bd662Svikram INJECT_ERROR1("STATVFS_PCFS", ret = 1); 3895eb2bd662Svikram if (ret != 0) { 3896eb2bd662Svikram bam_error(STATVFS_FAIL, root, strerror(errno)); 3897eb2bd662Svikram return (0); 3898eb2bd662Svikram } 3899eb2bd662Svikram 3900eb2bd662Svikram if (strncmp(vfs.f_basetype, "pcfs", strlen("pcfs")) == 0) { 3901eb2bd662Svikram BAM_DPRINTF((D_IS_PCFS, fcn, root)); 3902eb2bd662Svikram return (1); 3903eb2bd662Svikram } else { 3904eb2bd662Svikram BAM_DPRINTF((D_IS_NOT_PCFS, fcn, root)); 3905eb2bd662Svikram return (0); 3906eb2bd662Svikram } 3907eb2bd662Svikram } 3908eb2bd662Svikram 3909eb2bd662Svikram static int 39107c478bd9Sstevel@tonic-gate is_readonly(char *root) 39117c478bd9Sstevel@tonic-gate { 3912eb2bd662Svikram int fd; 3913eb2bd662Svikram int error; 3914eb2bd662Svikram char testfile[PATH_MAX]; 3915eb2bd662Svikram const char *fcn = "is_readonly()"; 39167c478bd9Sstevel@tonic-gate 39177c478bd9Sstevel@tonic-gate /* 3918eb2bd662Svikram * Using statvfs() to check for a read-only filesystem is not 3919eb2bd662Svikram * reliable. The only way to reliably test is to attempt to 3920eb2bd662Svikram * create a file 39217c478bd9Sstevel@tonic-gate */ 3922eb2bd662Svikram (void) snprintf(testfile, sizeof (testfile), "%s/%s.%d", 3923eb2bd662Svikram root, BOOTADM_RDONLY_TEST, getpid()); 39247c478bd9Sstevel@tonic-gate 3925eb2bd662Svikram (void) unlink(testfile); 3926eb2bd662Svikram 3927eb2bd662Svikram errno = 0; 3928eb2bd662Svikram fd = open(testfile, O_RDWR|O_CREAT|O_EXCL, 0644); 3929eb2bd662Svikram error = errno; 3930eb2bd662Svikram INJECT_ERROR2("RDONLY_TEST_ERROR", fd = -1, error = EACCES); 3931eb2bd662Svikram if (fd == -1 && error == EROFS) { 3932eb2bd662Svikram BAM_DPRINTF((D_RDONLY_FS, fcn, root)); 39337c478bd9Sstevel@tonic-gate return (1); 3934eb2bd662Svikram } else if (fd == -1) { 3935eb2bd662Svikram bam_error(RDONLY_TEST_ERROR, root, strerror(error)); 39367c478bd9Sstevel@tonic-gate } 39377c478bd9Sstevel@tonic-gate 3938eb2bd662Svikram (void) close(fd); 3939eb2bd662Svikram (void) unlink(testfile); 39407c478bd9Sstevel@tonic-gate 3941eb2bd662Svikram BAM_DPRINTF((D_RDWR_FS, fcn, root)); 3942e7cbe64fSgw25295 return (0); 3943e7cbe64fSgw25295 } 3944e7cbe64fSgw25295 39457c478bd9Sstevel@tonic-gate static error_t 39467c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt) 39477c478bd9Sstevel@tonic-gate { 39487c478bd9Sstevel@tonic-gate error_t ret; 39497c478bd9Sstevel@tonic-gate 39507c478bd9Sstevel@tonic-gate assert(root); 39517c478bd9Sstevel@tonic-gate assert(opt == NULL); 39527c478bd9Sstevel@tonic-gate 395348847494SEnrico Perla - Sun Microsystems init_walk_args(); 395448847494SEnrico Perla - Sun Microsystems (void) umask(022); 395548847494SEnrico Perla - Sun Microsystems 39567c478bd9Sstevel@tonic-gate /* 39576debd3f5SAlexander Eremin * Never update non-BE root in update_all 39586debd3f5SAlexander Eremin */ 39596debd3f5SAlexander Eremin if (!is_be(root) && bam_update_all) 39606debd3f5SAlexander Eremin return (BAM_SUCCESS); 39616debd3f5SAlexander Eremin /* 3962986fd29aSsetje * root must belong to a boot archive based OS, 39637c478bd9Sstevel@tonic-gate */ 3964986fd29aSsetje if (!is_boot_archive(root)) { 3965b610f78eSvikram /* 3966b610f78eSvikram * Emit message only if not in context of update_all. 3967b610f78eSvikram * If in update_all, emit only if verbose flag is set. 3968b610f78eSvikram */ 3969b610f78eSvikram if (!bam_update_all || bam_verbose) 3970eb2bd662Svikram bam_print(NOT_ARCHIVE_BOOT, root); 397148847494SEnrico Perla - Sun Microsystems return (BAM_ERROR); 39727c478bd9Sstevel@tonic-gate } 39737c478bd9Sstevel@tonic-gate 39747c478bd9Sstevel@tonic-gate /* 39758c1b6884Sszhou * If smf check is requested when / is writable (can happen 39768c1b6884Sszhou * on first reboot following an upgrade because service 39778c1b6884Sszhou * dependency is messed up), skip the check. 39788c1b6884Sszhou */ 397948847494SEnrico Perla - Sun Microsystems if (bam_smf_check && !bam_root_readonly && !is_zfs(root)) 39808c1b6884Sszhou return (BAM_SUCCESS); 39818c1b6884Sszhou 39828c1b6884Sszhou /* 39835eea6091SEnrico Perla - Sun Microsystems * Don't generate archive on ramdisk. 39845eea6091SEnrico Perla - Sun Microsystems */ 39855eea6091SEnrico Perla - Sun Microsystems if (is_ramdisk(root)) 39865eea6091SEnrico Perla - Sun Microsystems return (BAM_SUCCESS); 39875eea6091SEnrico Perla - Sun Microsystems 39885eea6091SEnrico Perla - Sun Microsystems /* 39898c1b6884Sszhou * root must be writable. This check applies to alternate 39908c1b6884Sszhou * root (-R option); bam_root_readonly applies to '/' only. 399148847494SEnrico Perla - Sun Microsystems * The behaviour translates into being the one of a 'check'. 39927c478bd9Sstevel@tonic-gate */ 399361cb17bdSsetje if (!bam_smf_check && !bam_check && is_readonly(root)) { 399448847494SEnrico Perla - Sun Microsystems set_flag(RDONLY_FSCHK); 399548847494SEnrico Perla - Sun Microsystems bam_check = 1; 39967c478bd9Sstevel@tonic-gate } 39977c478bd9Sstevel@tonic-gate 39987c478bd9Sstevel@tonic-gate /* 3999cedc7e57SEnrico Perla - Sun Microsystems * Now check if an update is really needed. 40007c478bd9Sstevel@tonic-gate */ 40017c478bd9Sstevel@tonic-gate ret = update_required(root); 40027c478bd9Sstevel@tonic-gate 40037c478bd9Sstevel@tonic-gate /* 4004cedc7e57SEnrico Perla - Sun Microsystems * The check command (-n) is *not* a dry run. 40057c478bd9Sstevel@tonic-gate * It only checks if the archive is in sync. 4006cedc7e57SEnrico Perla - Sun Microsystems * A readonly filesystem has to be considered an error only if an update 4007cedc7e57SEnrico Perla - Sun Microsystems * is required. 40087c478bd9Sstevel@tonic-gate */ 4009cedc7e57SEnrico Perla - Sun Microsystems if (bam_nowrite()) { 401048847494SEnrico Perla - Sun Microsystems if (is_flag_on(RDONLY_FSCHK)) { 40115eea6091SEnrico Perla - Sun Microsystems bam_check = bam_saved_check; 401248847494SEnrico Perla - Sun Microsystems if (ret > 0) 401348847494SEnrico Perla - Sun Microsystems bam_error(RDONLY_FS, root); 401448847494SEnrico Perla - Sun Microsystems if (bam_update_all) 401548847494SEnrico Perla - Sun Microsystems return ((ret != 0) ? BAM_ERROR : BAM_SUCCESS); 401648847494SEnrico Perla - Sun Microsystems } 401748847494SEnrico Perla - Sun Microsystems 40187c478bd9Sstevel@tonic-gate bam_exit((ret != 0) ? 1 : 0); 40197c478bd9Sstevel@tonic-gate } 40207c478bd9Sstevel@tonic-gate 40217c478bd9Sstevel@tonic-gate if (ret == 1) { 40227c478bd9Sstevel@tonic-gate /* create the ramdisk */ 40237c478bd9Sstevel@tonic-gate ret = create_ramdisk(root); 40247c478bd9Sstevel@tonic-gate } 4025986fd29aSsetje 40264a9df875SEnrico Perla - Sun Microsystems /* 40274a9df875SEnrico Perla - Sun Microsystems * if the archive is updated, save the new stat data and update the 40284a9df875SEnrico Perla - Sun Microsystems * timestamp file 40294a9df875SEnrico Perla - Sun Microsystems */ 4030986fd29aSsetje if (ret == 0 && walk_arg.new_nvlp != NULL) { 4031986fd29aSsetje savenew(root); 40324a9df875SEnrico Perla - Sun Microsystems update_timestamp(root); 4033986fd29aSsetje } 4034986fd29aSsetje 4035986fd29aSsetje clear_walk_args(); 4036986fd29aSsetje 40377c478bd9Sstevel@tonic-gate return (ret); 40387c478bd9Sstevel@tonic-gate } 40397c478bd9Sstevel@tonic-gate 404040b108d7SMark J Musante static char * 404140b108d7SMark J Musante find_root_pool() 404240b108d7SMark J Musante { 404340b108d7SMark J Musante char *special = get_special("/"); 404440b108d7SMark J Musante char *p; 404540b108d7SMark J Musante 404640b108d7SMark J Musante if (special == NULL) 404740b108d7SMark J Musante return (NULL); 404840b108d7SMark J Musante 404940b108d7SMark J Musante if (*special == '/') { 405040b108d7SMark J Musante free(special); 405140b108d7SMark J Musante return (NULL); 405240b108d7SMark J Musante } 405340b108d7SMark J Musante 405440b108d7SMark J Musante if ((p = strchr(special, '/')) != NULL) 405540b108d7SMark J Musante *p = '\0'; 405640b108d7SMark J Musante 405740b108d7SMark J Musante return (special); 405840b108d7SMark J Musante } 405940b108d7SMark J Musante 4060963390b4Svikram static error_t 4061963390b4Svikram synchronize_BE_menu(void) 4062fbac2b2bSvikram { 4063fbac2b2bSvikram struct stat sb; 4064963390b4Svikram char cmdline[PATH_MAX]; 4065963390b4Svikram char cksum_line[PATH_MAX]; 4066963390b4Svikram filelist_t flist = {0}; 4067963390b4Svikram char *old_cksum_str; 4068963390b4Svikram char *old_size_str; 4069963390b4Svikram char *old_file; 4070963390b4Svikram char *curr_cksum_str; 4071963390b4Svikram char *curr_size_str; 4072963390b4Svikram char *curr_file; 40731130146cSGangadhar Mylapuram char *pool = NULL; 40741130146cSGangadhar Mylapuram char *mntpt = NULL; 40751130146cSGangadhar Mylapuram zfs_mnted_t mnted; 4076963390b4Svikram FILE *cfp; 4077963390b4Svikram int found; 4078963390b4Svikram int ret; 4079963390b4Svikram const char *fcn = "synchronize_BE_menu()"; 4080fbac2b2bSvikram 4081963390b4Svikram BAM_DPRINTF((D_FUNC_ENTRY0, fcn)); 4082fbac2b2bSvikram 4083963390b4Svikram /* Check if findroot enabled LU BE */ 4084963390b4Svikram if (stat(FINDROOT_INSTALLGRUB, &sb) != 0) { 4085963390b4Svikram BAM_DPRINTF((D_NOT_LU_BE, fcn)); 4086963390b4Svikram return (BAM_SUCCESS); 4087fbac2b2bSvikram } 4088fbac2b2bSvikram 4089963390b4Svikram if (stat(LU_MENU_CKSUM, &sb) != 0) { 4090963390b4Svikram BAM_DPRINTF((D_NO_CKSUM_FILE, fcn, LU_MENU_CKSUM)); 4091963390b4Svikram goto menu_sync; 4092fbac2b2bSvikram } 4093963390b4Svikram 4094963390b4Svikram cfp = fopen(LU_MENU_CKSUM, "r"); 4095963390b4Svikram INJECT_ERROR1("CKSUM_FILE_MISSING", cfp = NULL); 4096963390b4Svikram if (cfp == NULL) { 4097963390b4Svikram bam_error(CANNOT_READ_LU_CKSUM, LU_MENU_CKSUM); 4098963390b4Svikram goto menu_sync; 4099963390b4Svikram } 4100963390b4Svikram BAM_DPRINTF((D_CKSUM_FILE_OPENED, fcn, LU_MENU_CKSUM)); 4101963390b4Svikram 4102963390b4Svikram found = 0; 4103963390b4Svikram while (s_fgets(cksum_line, sizeof (cksum_line), cfp) != NULL) { 4104963390b4Svikram INJECT_ERROR1("MULTIPLE_CKSUM", found = 1); 4105963390b4Svikram if (found) { 4106963390b4Svikram bam_error(MULTIPLE_LU_CKSUM, LU_MENU_CKSUM); 4107963390b4Svikram (void) fclose(cfp); 4108963390b4Svikram goto menu_sync; 4109963390b4Svikram } 4110963390b4Svikram found = 1; 4111963390b4Svikram } 4112963390b4Svikram BAM_DPRINTF((D_CKSUM_FILE_READ, fcn, LU_MENU_CKSUM)); 4113963390b4Svikram 4114963390b4Svikram 4115963390b4Svikram old_cksum_str = strtok(cksum_line, " \t"); 4116963390b4Svikram old_size_str = strtok(NULL, " \t"); 4117963390b4Svikram old_file = strtok(NULL, " \t"); 4118963390b4Svikram 4119963390b4Svikram INJECT_ERROR1("OLD_CKSUM_NULL", old_cksum_str = NULL); 4120963390b4Svikram INJECT_ERROR1("OLD_SIZE_NULL", old_size_str = NULL); 4121963390b4Svikram INJECT_ERROR1("OLD_FILE_NULL", old_file = NULL); 4122963390b4Svikram if (old_cksum_str == NULL || old_size_str == NULL || old_file == NULL) { 4123963390b4Svikram bam_error(CANNOT_PARSE_LU_CKSUM, LU_MENU_CKSUM); 4124963390b4Svikram goto menu_sync; 4125963390b4Svikram } 4126963390b4Svikram BAM_DPRINTF((D_CKSUM_FILE_PARSED, fcn, LU_MENU_CKSUM)); 4127963390b4Svikram 4128963390b4Svikram /* Get checksum of current menu */ 412940b108d7SMark J Musante pool = find_root_pool(); 41301130146cSGangadhar Mylapuram if (pool) { 41311130146cSGangadhar Mylapuram mntpt = mount_top_dataset(pool, &mnted); 41321130146cSGangadhar Mylapuram if (mntpt == NULL) { 41331130146cSGangadhar Mylapuram bam_error(FAIL_MNT_TOP_DATASET, pool); 413440b108d7SMark J Musante free(pool); 41351130146cSGangadhar Mylapuram return (BAM_ERROR); 41361130146cSGangadhar Mylapuram } 41371130146cSGangadhar Mylapuram (void) snprintf(cmdline, sizeof (cmdline), "%s %s%s", 41381130146cSGangadhar Mylapuram CKSUM, mntpt, GRUB_MENU); 41391130146cSGangadhar Mylapuram } else { 41401130146cSGangadhar Mylapuram (void) snprintf(cmdline, sizeof (cmdline), "%s %s", 41411130146cSGangadhar Mylapuram CKSUM, GRUB_MENU); 41421130146cSGangadhar Mylapuram } 4143963390b4Svikram ret = exec_cmd(cmdline, &flist); 41441130146cSGangadhar Mylapuram if (pool) { 41451130146cSGangadhar Mylapuram (void) umount_top_dataset(pool, mnted, mntpt); 41461130146cSGangadhar Mylapuram free(pool); 41471130146cSGangadhar Mylapuram } 4148963390b4Svikram INJECT_ERROR1("GET_CURR_CKSUM", ret = 1); 4149963390b4Svikram if (ret != 0) { 4150963390b4Svikram bam_error(MENU_CKSUM_FAIL); 4151963390b4Svikram return (BAM_ERROR); 4152963390b4Svikram } 4153963390b4Svikram BAM_DPRINTF((D_CKSUM_GEN_SUCCESS, fcn)); 4154963390b4Svikram 4155963390b4Svikram INJECT_ERROR1("GET_CURR_CKSUM_OUTPUT", flist.head = NULL); 4156963390b4Svikram if ((flist.head == NULL) || (flist.head != flist.tail)) { 4157963390b4Svikram bam_error(BAD_CKSUM); 4158963390b4Svikram filelist_free(&flist); 4159963390b4Svikram return (BAM_ERROR); 4160963390b4Svikram } 4161963390b4Svikram BAM_DPRINTF((D_CKSUM_GEN_OUTPUT_VALID, fcn)); 4162963390b4Svikram 4163963390b4Svikram curr_cksum_str = strtok(flist.head->line, " \t"); 4164963390b4Svikram curr_size_str = strtok(NULL, " \t"); 4165963390b4Svikram curr_file = strtok(NULL, " \t"); 4166963390b4Svikram 4167963390b4Svikram INJECT_ERROR1("CURR_CKSUM_NULL", curr_cksum_str = NULL); 4168963390b4Svikram INJECT_ERROR1("CURR_SIZE_NULL", curr_size_str = NULL); 4169963390b4Svikram INJECT_ERROR1("CURR_FILE_NULL", curr_file = NULL); 4170963390b4Svikram if (curr_cksum_str == NULL || curr_size_str == NULL || 4171963390b4Svikram curr_file == NULL) { 4172963390b4Svikram bam_error(BAD_CKSUM_PARSE); 4173963390b4Svikram filelist_free(&flist); 4174963390b4Svikram return (BAM_ERROR); 4175963390b4Svikram } 4176963390b4Svikram BAM_DPRINTF((D_CKSUM_GEN_PARSED, fcn)); 4177963390b4Svikram 4178963390b4Svikram if (strcmp(old_cksum_str, curr_cksum_str) == 0 && 4179963390b4Svikram strcmp(old_size_str, curr_size_str) == 0 && 4180963390b4Svikram strcmp(old_file, curr_file) == 0) { 4181963390b4Svikram filelist_free(&flist); 4182963390b4Svikram BAM_DPRINTF((D_CKSUM_NO_CHANGE, fcn)); 4183963390b4Svikram return (BAM_SUCCESS); 4184963390b4Svikram } 4185963390b4Svikram 4186963390b4Svikram filelist_free(&flist); 4187963390b4Svikram 4188963390b4Svikram /* cksum doesn't match - the menu has changed */ 4189963390b4Svikram BAM_DPRINTF((D_CKSUM_HAS_CHANGED, fcn)); 4190963390b4Svikram 4191963390b4Svikram menu_sync: 4192963390b4Svikram bam_print(PROP_GRUB_MENU); 4193963390b4Svikram 4194963390b4Svikram (void) snprintf(cmdline, sizeof (cmdline), 4195a6968364Svikram "/bin/sh -c '. %s > /dev/null; %s %s yes > /dev/null'", 4196963390b4Svikram LULIB, LULIB_PROPAGATE_FILE, GRUB_MENU); 4197963390b4Svikram ret = exec_cmd(cmdline, NULL); 4198963390b4Svikram INJECT_ERROR1("PROPAGATE_MENU", ret = 1); 4199963390b4Svikram if (ret != 0) { 4200963390b4Svikram bam_error(MENU_PROP_FAIL); 4201963390b4Svikram return (BAM_ERROR); 4202963390b4Svikram } 4203963390b4Svikram BAM_DPRINTF((D_PROPAGATED_MENU, fcn)); 4204963390b4Svikram 4205a6968364Svikram (void) snprintf(cmdline, sizeof (cmdline), "/bin/cp %s %s > /dev/null", 4206963390b4Svikram GRUB_MENU, GRUB_BACKUP_MENU); 4207963390b4Svikram ret = exec_cmd(cmdline, NULL); 4208963390b4Svikram INJECT_ERROR1("CREATE_BACKUP", ret = 1); 4209963390b4Svikram if (ret != 0) { 4210963390b4Svikram bam_error(MENU_BACKUP_FAIL, GRUB_BACKUP_MENU); 4211963390b4Svikram return (BAM_ERROR); 4212963390b4Svikram } 4213963390b4Svikram BAM_DPRINTF((D_CREATED_BACKUP, fcn, GRUB_BACKUP_MENU)); 4214963390b4Svikram 4215963390b4Svikram (void) snprintf(cmdline, sizeof (cmdline), 4216a6968364Svikram "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'", 4217963390b4Svikram LULIB, LULIB_PROPAGATE_FILE, GRUB_BACKUP_MENU); 4218963390b4Svikram ret = exec_cmd(cmdline, NULL); 4219963390b4Svikram INJECT_ERROR1("PROPAGATE_BACKUP", ret = 1); 4220963390b4Svikram if (ret != 0) { 4221963390b4Svikram bam_error(BACKUP_PROP_FAIL, GRUB_BACKUP_MENU); 4222963390b4Svikram return (BAM_ERROR); 4223963390b4Svikram } 4224963390b4Svikram BAM_DPRINTF((D_PROPAGATED_BACKUP, fcn, GRUB_BACKUP_MENU)); 4225963390b4Svikram 4226963390b4Svikram (void) snprintf(cmdline, sizeof (cmdline), "%s %s > %s", 4227963390b4Svikram CKSUM, GRUB_MENU, LU_MENU_CKSUM); 4228963390b4Svikram ret = exec_cmd(cmdline, NULL); 4229963390b4Svikram INJECT_ERROR1("CREATE_CKSUM_FILE", ret = 1); 4230963390b4Svikram if (ret != 0) { 4231963390b4Svikram bam_error(MENU_CKSUM_WRITE_FAIL, LU_MENU_CKSUM); 4232963390b4Svikram return (BAM_ERROR); 4233963390b4Svikram } 4234963390b4Svikram BAM_DPRINTF((D_CREATED_CKSUM_FILE, fcn, LU_MENU_CKSUM)); 4235963390b4Svikram 4236963390b4Svikram (void) snprintf(cmdline, sizeof (cmdline), 4237a6968364Svikram "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'", 4238963390b4Svikram LULIB, LULIB_PROPAGATE_FILE, LU_MENU_CKSUM); 4239963390b4Svikram ret = exec_cmd(cmdline, NULL); 4240963390b4Svikram INJECT_ERROR1("PROPAGATE_MENU_CKSUM_FILE", ret = 1); 4241963390b4Svikram if (ret != 0) { 4242963390b4Svikram bam_error(MENU_CKSUM_PROP_FAIL, LU_MENU_CKSUM); 4243963390b4Svikram return (BAM_ERROR); 4244963390b4Svikram } 4245963390b4Svikram BAM_DPRINTF((D_PROPAGATED_CKSUM_FILE, fcn, LU_MENU_CKSUM)); 4246963390b4Svikram 4247963390b4Svikram return (BAM_SUCCESS); 4248fbac2b2bSvikram } 4249fbac2b2bSvikram 42507c478bd9Sstevel@tonic-gate static error_t 42517c478bd9Sstevel@tonic-gate update_all(char *root, char *opt) 42527c478bd9Sstevel@tonic-gate { 42537c478bd9Sstevel@tonic-gate struct extmnttab mnt; 42547c478bd9Sstevel@tonic-gate struct stat sb; 42557c478bd9Sstevel@tonic-gate FILE *fp; 42567c478bd9Sstevel@tonic-gate char multibt[PATH_MAX]; 4257986fd29aSsetje char creatram[PATH_MAX]; 42587c478bd9Sstevel@tonic-gate error_t ret = BAM_SUCCESS; 42597c478bd9Sstevel@tonic-gate 426040541d5dSvikram assert(root); 42617c478bd9Sstevel@tonic-gate assert(opt == NULL); 42627c478bd9Sstevel@tonic-gate 426340541d5dSvikram if (bam_rootlen != 1 || *root != '/') { 426440541d5dSvikram elide_trailing_slash(root, multibt, sizeof (multibt)); 426540541d5dSvikram bam_error(ALT_ROOT_INVALID, multibt); 426640541d5dSvikram return (BAM_ERROR); 426740541d5dSvikram } 426840541d5dSvikram 42697c478bd9Sstevel@tonic-gate /* 42707c478bd9Sstevel@tonic-gate * First update archive for current root 42717c478bd9Sstevel@tonic-gate */ 42727c478bd9Sstevel@tonic-gate if (update_archive(root, opt) != BAM_SUCCESS) 42737c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 427498892a30Snadkarni 427598892a30Snadkarni if (ret == BAM_ERROR) 427698892a30Snadkarni goto out; 42777c478bd9Sstevel@tonic-gate 42787c478bd9Sstevel@tonic-gate /* 42797c478bd9Sstevel@tonic-gate * Now walk the mount table, performing archive update 42807c478bd9Sstevel@tonic-gate * for all mounted Newboot root filesystems 42817c478bd9Sstevel@tonic-gate */ 42827c478bd9Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 42837c478bd9Sstevel@tonic-gate if (fp == NULL) { 42847c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 4285b610f78eSvikram ret = BAM_ERROR; 4286b610f78eSvikram goto out; 42877c478bd9Sstevel@tonic-gate } 42887c478bd9Sstevel@tonic-gate 42897c478bd9Sstevel@tonic-gate resetmnttab(fp); 42907c478bd9Sstevel@tonic-gate 42917c478bd9Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 42927c478bd9Sstevel@tonic-gate if (mnt.mnt_special == NULL) 42937c478bd9Sstevel@tonic-gate continue; 4294f904d32dSJerry Gilliam if ((strcmp(mnt.mnt_fstype, MNTTYPE_ZFS) != 0) && 4295f904d32dSJerry Gilliam (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0)) 42967c478bd9Sstevel@tonic-gate continue; 42977c478bd9Sstevel@tonic-gate if (strcmp(mnt.mnt_mountp, "/") == 0) 42987c478bd9Sstevel@tonic-gate continue; 42997c478bd9Sstevel@tonic-gate 4300986fd29aSsetje (void) snprintf(creatram, sizeof (creatram), "%s/%s", 4301986fd29aSsetje mnt.mnt_mountp, CREATE_RAMDISK); 43027c478bd9Sstevel@tonic-gate 4303986fd29aSsetje if (stat(creatram, &sb) == -1) 43047c478bd9Sstevel@tonic-gate continue; 43057c478bd9Sstevel@tonic-gate 43067c478bd9Sstevel@tonic-gate /* 43077c478bd9Sstevel@tonic-gate * We put a trailing slash to be consistent with root = "/" 43087c478bd9Sstevel@tonic-gate * case, such that we don't have to print // in some cases. 43097c478bd9Sstevel@tonic-gate */ 43107c478bd9Sstevel@tonic-gate (void) snprintf(rootbuf, sizeof (rootbuf), "%s/", 43117c478bd9Sstevel@tonic-gate mnt.mnt_mountp); 43127c478bd9Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 4313ae115bc7Smrj 4314ae115bc7Smrj /* 4315ae115bc7Smrj * It's possible that other mounts may be an alternate boot 4316ae115bc7Smrj * architecture, so check it again. 4317ae115bc7Smrj */ 4318eb2bd662Svikram if ((get_boot_cap(rootbuf) != BAM_SUCCESS) || 4319ae115bc7Smrj (update_archive(rootbuf, opt) != BAM_SUCCESS)) 43207c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 43217c478bd9Sstevel@tonic-gate } 43227c478bd9Sstevel@tonic-gate 43237c478bd9Sstevel@tonic-gate (void) fclose(fp); 43247c478bd9Sstevel@tonic-gate 4325b610f78eSvikram out: 4326fbac2b2bSvikram /* 4327963390b4Svikram * We no longer use biosdev for Live Upgrade. Hence 4328963390b4Svikram * there is no need to defer (to shutdown time) any fdisk 4329963390b4Svikram * updates 4330fbac2b2bSvikram */ 4331963390b4Svikram if (stat(GRUB_fdisk, &sb) == 0 || stat(GRUB_fdisk_target, &sb) == 0) { 4332963390b4Svikram bam_error(FDISK_FILES_FOUND, GRUB_fdisk, GRUB_fdisk_target); 4333fbac2b2bSvikram } 4334fbac2b2bSvikram 4335963390b4Svikram /* 4336963390b4Svikram * If user has updated menu in current BE, propagate the 4337963390b4Svikram * updates to all BEs. 4338963390b4Svikram */ 433919397407SSherry Moore if (sync_menu && synchronize_BE_menu() != BAM_SUCCESS) 4340963390b4Svikram ret = BAM_ERROR; 4341963390b4Svikram 43427c478bd9Sstevel@tonic-gate return (ret); 43437c478bd9Sstevel@tonic-gate } 43447c478bd9Sstevel@tonic-gate 43457c478bd9Sstevel@tonic-gate static void 43467c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp) 43477c478bd9Sstevel@tonic-gate { 43487c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 43497c478bd9Sstevel@tonic-gate mp->start = lp; 43507c478bd9Sstevel@tonic-gate } else { 43517c478bd9Sstevel@tonic-gate mp->end->next = lp; 43528c1b6884Sszhou lp->prev = mp->end; 43537c478bd9Sstevel@tonic-gate } 43547c478bd9Sstevel@tonic-gate mp->end = lp; 43557c478bd9Sstevel@tonic-gate } 43567c478bd9Sstevel@tonic-gate 4357eb2bd662Svikram void 43588c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp) 43598c1b6884Sszhou { 43608c1b6884Sszhou /* unlink from list */ 43618c1b6884Sszhou if (lp->prev) 43628c1b6884Sszhou lp->prev->next = lp->next; 43638c1b6884Sszhou else 43648c1b6884Sszhou mp->start = lp->next; 43658c1b6884Sszhou if (lp->next) 43668c1b6884Sszhou lp->next->prev = lp->prev; 43678c1b6884Sszhou else 43688c1b6884Sszhou mp->end = lp->prev; 43698c1b6884Sszhou } 43708c1b6884Sszhou 43718c1b6884Sszhou static entry_t * 43728c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end) 43738c1b6884Sszhou { 43748c1b6884Sszhou entry_t *ent, *prev; 4375eb2bd662Svikram const char *fcn = "boot_entry_new()"; 4376eb2bd662Svikram 4377eb2bd662Svikram assert(mp); 4378eb2bd662Svikram assert(start); 4379eb2bd662Svikram assert(end); 43808c1b6884Sszhou 43818c1b6884Sszhou ent = s_calloc(1, sizeof (entry_t)); 4382eb2bd662Svikram BAM_DPRINTF((D_ENTRY_NEW, fcn)); 43838c1b6884Sszhou ent->start = start; 43848c1b6884Sszhou ent->end = end; 43858c1b6884Sszhou 43868c1b6884Sszhou if (mp->entries == NULL) { 43878c1b6884Sszhou mp->entries = ent; 4388eb2bd662Svikram BAM_DPRINTF((D_ENTRY_NEW_FIRST, fcn)); 43898c1b6884Sszhou return (ent); 43908c1b6884Sszhou } 43918c1b6884Sszhou 43928c1b6884Sszhou prev = mp->entries; 43938c1b6884Sszhou while (prev->next) 43948c1b6884Sszhou prev = prev->next; 43958c1b6884Sszhou prev->next = ent; 43968c1b6884Sszhou ent->prev = prev; 4397eb2bd662Svikram BAM_DPRINTF((D_ENTRY_NEW_LINKED, fcn)); 43988c1b6884Sszhou return (ent); 43998c1b6884Sszhou } 44008c1b6884Sszhou 44018c1b6884Sszhou static void 44028c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp) 44038c1b6884Sszhou { 44048c1b6884Sszhou if (ent) 44058c1b6884Sszhou ent->end = lp; 44068c1b6884Sszhou } 44078c1b6884Sszhou 44087c478bd9Sstevel@tonic-gate /* 4409843e1988Sjohnlev * Check whether cmd matches the one indexed by which, and whether arg matches 4410843e1988Sjohnlev * str. which must be either KERNEL_CMD or MODULE_CMD, and a match to the 4411843e1988Sjohnlev * respective *_DOLLAR_CMD is also acceptable. The arg is searched using 4412843e1988Sjohnlev * strstr(), so it can be a partial match. 4413843e1988Sjohnlev */ 4414843e1988Sjohnlev static int 4415843e1988Sjohnlev check_cmd(const char *cmd, const int which, const char *arg, const char *str) 4416843e1988Sjohnlev { 4417eb2bd662Svikram int ret; 4418eb2bd662Svikram const char *fcn = "check_cmd()"; 4419eb2bd662Svikram 4420eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, fcn, arg, str)); 4421eb2bd662Svikram 4422eac223ccSWilliam Kucharski if (cmd != NULL) { 4423843e1988Sjohnlev if ((strcmp(cmd, menu_cmds[which]) != 0) && 4424843e1988Sjohnlev (strcmp(cmd, menu_cmds[which + 1]) != 0)) { 4425eb2bd662Svikram BAM_DPRINTF((D_CHECK_CMD_CMD_NOMATCH, 4426eb2bd662Svikram fcn, cmd, menu_cmds[which])); 4427843e1988Sjohnlev return (0); 4428843e1988Sjohnlev } 4429eb2bd662Svikram ret = (strstr(arg, str) != NULL); 4430eac223ccSWilliam Kucharski } else 4431eac223ccSWilliam Kucharski ret = 0; 4432eb2bd662Svikram 4433eb2bd662Svikram if (ret) { 4434eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 4435eb2bd662Svikram } else { 4436eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 4437eb2bd662Svikram } 4438eb2bd662Svikram 4439eb2bd662Svikram return (ret); 4440eb2bd662Svikram } 4441eb2bd662Svikram 4442eb2bd662Svikram static error_t 4443eb2bd662Svikram kernel_parser(entry_t *entry, char *cmd, char *arg, int linenum) 4444eb2bd662Svikram { 4445eb2bd662Svikram const char *fcn = "kernel_parser()"; 4446eb2bd662Svikram 4447eb2bd662Svikram assert(entry); 4448eb2bd662Svikram assert(cmd); 4449eb2bd662Svikram assert(arg); 4450eb2bd662Svikram 4451eb2bd662Svikram if (strcmp(cmd, menu_cmds[KERNEL_CMD]) != 0 && 4452eb2bd662Svikram strcmp(cmd, menu_cmds[KERNEL_DOLLAR_CMD]) != 0) { 4453eb2bd662Svikram BAM_DPRINTF((D_NOT_KERNEL_CMD, fcn, cmd)); 4454eb2bd662Svikram return (BAM_ERROR); 4455eb2bd662Svikram } 4456eb2bd662Svikram 4457eb2bd662Svikram if (strncmp(arg, DIRECT_BOOT_32, sizeof (DIRECT_BOOT_32) - 1) == 0) { 4458eb2bd662Svikram BAM_DPRINTF((D_SET_DBOOT_32, fcn, arg)); 4459eb2bd662Svikram entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT; 4460eb2bd662Svikram } else if (strncmp(arg, DIRECT_BOOT_KERNEL, 4461eb2bd662Svikram sizeof (DIRECT_BOOT_KERNEL) - 1) == 0) { 4462eb2bd662Svikram BAM_DPRINTF((D_SET_DBOOT, fcn, arg)); 4463eb2bd662Svikram entry->flags |= BAM_ENTRY_DBOOT; 4464eb2bd662Svikram } else if (strncmp(arg, DIRECT_BOOT_64, 4465eb2bd662Svikram sizeof (DIRECT_BOOT_64) - 1) == 0) { 4466eb2bd662Svikram BAM_DPRINTF((D_SET_DBOOT_64, fcn, arg)); 4467eb2bd662Svikram entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT; 4468eb2bd662Svikram } else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_KERNEL, 4469eb2bd662Svikram sizeof (DIRECT_BOOT_FAILSAFE_KERNEL) - 1) == 0) { 4470eb2bd662Svikram BAM_DPRINTF((D_SET_DBOOT_FAILSAFE, fcn, arg)); 4471eb2bd662Svikram entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE; 4472bbcc54bdSEnrico Perla - Sun Microsystems } else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_32, 4473bbcc54bdSEnrico Perla - Sun Microsystems sizeof (DIRECT_BOOT_FAILSAFE_32) - 1) == 0) { 4474bbcc54bdSEnrico Perla - Sun Microsystems BAM_DPRINTF((D_SET_DBOOT_FAILSAFE_32, fcn, arg)); 4475bbcc54bdSEnrico Perla - Sun Microsystems entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE 4476bbcc54bdSEnrico Perla - Sun Microsystems | BAM_ENTRY_32BIT; 4477bbcc54bdSEnrico Perla - Sun Microsystems } else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_64, 4478bbcc54bdSEnrico Perla - Sun Microsystems sizeof (DIRECT_BOOT_FAILSAFE_64) - 1) == 0) { 4479bbcc54bdSEnrico Perla - Sun Microsystems BAM_DPRINTF((D_SET_DBOOT_FAILSAFE_64, fcn, arg)); 4480bbcc54bdSEnrico Perla - Sun Microsystems entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE 4481bbcc54bdSEnrico Perla - Sun Microsystems | BAM_ENTRY_64BIT; 4482eb2bd662Svikram } else if (strncmp(arg, MULTI_BOOT, sizeof (MULTI_BOOT) - 1) == 0) { 4483eb2bd662Svikram BAM_DPRINTF((D_SET_MULTIBOOT, fcn, arg)); 4484eb2bd662Svikram entry->flags |= BAM_ENTRY_MULTIBOOT; 4485eb2bd662Svikram } else if (strncmp(arg, MULTI_BOOT_FAILSAFE, 4486eb2bd662Svikram sizeof (MULTI_BOOT_FAILSAFE) - 1) == 0) { 4487eb2bd662Svikram BAM_DPRINTF((D_SET_MULTIBOOT_FAILSAFE, fcn, arg)); 4488eb2bd662Svikram entry->flags |= BAM_ENTRY_MULTIBOOT | BAM_ENTRY_FAILSAFE; 4489eb2bd662Svikram } else if (strstr(arg, XEN_KERNEL_SUBSTR)) { 4490eb2bd662Svikram BAM_DPRINTF((D_SET_HV, fcn, arg)); 4491eb2bd662Svikram entry->flags |= BAM_ENTRY_HV; 4492eb2bd662Svikram } else if (!(entry->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU))) { 4493eb2bd662Svikram BAM_DPRINTF((D_SET_HAND_KERNEL, fcn, arg)); 4494eb2bd662Svikram return (BAM_ERROR); 449537eb779cSVikram Hegde } else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 && 449637eb779cSVikram Hegde strstr(arg, UNIX_SPACE)) { 449737eb779cSVikram Hegde entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT; 449837eb779cSVikram Hegde } else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 && 449937eb779cSVikram Hegde strstr(arg, AMD_UNIX_SPACE)) { 450037eb779cSVikram Hegde entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT; 4501eb2bd662Svikram } else { 4502eb2bd662Svikram BAM_DPRINTF((D_IS_UNKNOWN_KERNEL, fcn, arg)); 4503eb2bd662Svikram bam_error(UNKNOWN_KERNEL_LINE, linenum); 4504eb2bd662Svikram return (BAM_ERROR); 4505eb2bd662Svikram } 4506eb2bd662Svikram 4507eb2bd662Svikram return (BAM_SUCCESS); 4508eb2bd662Svikram } 4509eb2bd662Svikram 4510eb2bd662Svikram static error_t 4511eb2bd662Svikram module_parser(entry_t *entry, char *cmd, char *arg, int linenum) 4512eb2bd662Svikram { 4513eb2bd662Svikram const char *fcn = "module_parser()"; 4514eb2bd662Svikram 4515eb2bd662Svikram assert(entry); 4516eb2bd662Svikram assert(cmd); 4517eb2bd662Svikram assert(arg); 4518eb2bd662Svikram 4519eb2bd662Svikram if (strcmp(cmd, menu_cmds[MODULE_CMD]) != 0 && 4520eb2bd662Svikram strcmp(cmd, menu_cmds[MODULE_DOLLAR_CMD]) != 0) { 4521eb2bd662Svikram BAM_DPRINTF((D_NOT_MODULE_CMD, fcn, cmd)); 4522eb2bd662Svikram return (BAM_ERROR); 4523eb2bd662Svikram } 4524eb2bd662Svikram 4525eb2bd662Svikram if (strcmp(arg, DIRECT_BOOT_ARCHIVE) == 0 || 4526eb2bd662Svikram strcmp(arg, DIRECT_BOOT_ARCHIVE_32) == 0 || 4527eb2bd662Svikram strcmp(arg, DIRECT_BOOT_ARCHIVE_64) == 0 || 4528eb2bd662Svikram strcmp(arg, MULTIBOOT_ARCHIVE) == 0 || 4529eb2bd662Svikram strcmp(arg, FAILSAFE_ARCHIVE) == 0 || 4530bbcc54bdSEnrico Perla - Sun Microsystems strcmp(arg, FAILSAFE_ARCHIVE_32) == 0 || 4531bbcc54bdSEnrico Perla - Sun Microsystems strcmp(arg, FAILSAFE_ARCHIVE_64) == 0 || 4532eb2bd662Svikram strcmp(arg, XEN_KERNEL_MODULE_LINE) == 0 || 4533eb2bd662Svikram strcmp(arg, XEN_KERNEL_MODULE_LINE_ZFS) == 0) { 4534eb2bd662Svikram BAM_DPRINTF((D_BOOTADM_LU_MODULE, fcn, arg)); 4535eb2bd662Svikram return (BAM_SUCCESS); 4536eb2bd662Svikram } else if (!(entry->flags & BAM_ENTRY_BOOTADM) && 4537eb2bd662Svikram !(entry->flags & BAM_ENTRY_LU)) { 4538eb2bd662Svikram /* don't emit warning for hand entries */ 4539eb2bd662Svikram BAM_DPRINTF((D_IS_HAND_MODULE, fcn, arg)); 4540eb2bd662Svikram return (BAM_ERROR); 4541eb2bd662Svikram } else { 4542eb2bd662Svikram BAM_DPRINTF((D_IS_UNKNOWN_MODULE, fcn, arg)); 4543eb2bd662Svikram bam_error(UNKNOWN_MODULE_LINE, linenum); 4544eb2bd662Svikram return (BAM_ERROR); 4545eb2bd662Svikram } 4546843e1988Sjohnlev } 4547843e1988Sjohnlev 4548843e1988Sjohnlev /* 45497c478bd9Sstevel@tonic-gate * A line in menu.lst looks like 45507c478bd9Sstevel@tonic-gate * [ ]*<cmd>[ \t=]*<arg>* 45517c478bd9Sstevel@tonic-gate */ 45527c478bd9Sstevel@tonic-gate static void 45537c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum) 45547c478bd9Sstevel@tonic-gate { 45557c478bd9Sstevel@tonic-gate /* 45567c478bd9Sstevel@tonic-gate * save state across calls. This is so that 45577c478bd9Sstevel@tonic-gate * header gets the right entry# after title has 45587c478bd9Sstevel@tonic-gate * been processed 45597c478bd9Sstevel@tonic-gate */ 45608c1b6884Sszhou static line_t *prev = NULL; 45618c1b6884Sszhou static entry_t *curr_ent = NULL; 4562ae115bc7Smrj static int in_liveupgrade = 0; 4563772d6a58SWilliam Kucharski static int is_libbe_ent = 0; 45647c478bd9Sstevel@tonic-gate 45657c478bd9Sstevel@tonic-gate line_t *lp; 45667c478bd9Sstevel@tonic-gate char *cmd, *sep, *arg; 45677c478bd9Sstevel@tonic-gate char save, *cp, *line; 45687c478bd9Sstevel@tonic-gate menu_flag_t flag = BAM_INVALID; 4569eb2bd662Svikram const char *fcn = "line_parser()"; 45707c478bd9Sstevel@tonic-gate 4571110570ceSToomas Soome cmd = NULL; 45727c478bd9Sstevel@tonic-gate if (str == NULL) { 45737c478bd9Sstevel@tonic-gate return; 45747c478bd9Sstevel@tonic-gate } 45757c478bd9Sstevel@tonic-gate 45767c478bd9Sstevel@tonic-gate /* 45777c478bd9Sstevel@tonic-gate * First save a copy of the entire line. 45787c478bd9Sstevel@tonic-gate * We use this later to set the line field. 45797c478bd9Sstevel@tonic-gate */ 45807c478bd9Sstevel@tonic-gate line = s_strdup(str); 45817c478bd9Sstevel@tonic-gate 45827c478bd9Sstevel@tonic-gate /* Eat up leading whitespace */ 45837c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 45847c478bd9Sstevel@tonic-gate str++; 45857c478bd9Sstevel@tonic-gate 45867c478bd9Sstevel@tonic-gate if (*str == '#') { /* comment */ 45877c478bd9Sstevel@tonic-gate cmd = s_strdup("#"); 45887c478bd9Sstevel@tonic-gate sep = NULL; 45897c478bd9Sstevel@tonic-gate arg = s_strdup(str + 1); 45907c478bd9Sstevel@tonic-gate flag = BAM_COMMENT; 4591ae115bc7Smrj if (strstr(arg, BAM_LU_HDR) != NULL) { 4592ae115bc7Smrj in_liveupgrade = 1; 4593ae115bc7Smrj } else if (strstr(arg, BAM_LU_FTR) != NULL) { 4594ae115bc7Smrj in_liveupgrade = 0; 4595772d6a58SWilliam Kucharski } else if (strstr(arg, BAM_LIBBE_FTR) != NULL) { 4596772d6a58SWilliam Kucharski is_libbe_ent = 1; 4597ae115bc7Smrj } 45987c478bd9Sstevel@tonic-gate } else if (*str == '\0') { /* blank line */ 45997c478bd9Sstevel@tonic-gate cmd = sep = arg = NULL; 46007c478bd9Sstevel@tonic-gate flag = BAM_EMPTY; 46017c478bd9Sstevel@tonic-gate } else { 46027c478bd9Sstevel@tonic-gate /* 46037c478bd9Sstevel@tonic-gate * '=' is not a documented separator in grub syntax. 46047c478bd9Sstevel@tonic-gate * However various development bits use '=' as a 46057c478bd9Sstevel@tonic-gate * separator. In addition, external users also 46067c478bd9Sstevel@tonic-gate * use = as a separator. So we will allow that usage. 46077c478bd9Sstevel@tonic-gate */ 46087c478bd9Sstevel@tonic-gate cp = str; 46097c478bd9Sstevel@tonic-gate while (*str != ' ' && *str != '\t' && *str != '=') { 46107c478bd9Sstevel@tonic-gate if (*str == '\0') { 46117c478bd9Sstevel@tonic-gate cmd = s_strdup(cp); 46127c478bd9Sstevel@tonic-gate sep = arg = NULL; 46137c478bd9Sstevel@tonic-gate break; 46147c478bd9Sstevel@tonic-gate } 46157c478bd9Sstevel@tonic-gate str++; 46167c478bd9Sstevel@tonic-gate } 46177c478bd9Sstevel@tonic-gate 46187c478bd9Sstevel@tonic-gate if (*str != '\0') { 46197c478bd9Sstevel@tonic-gate save = *str; 46207c478bd9Sstevel@tonic-gate *str = '\0'; 46217c478bd9Sstevel@tonic-gate cmd = s_strdup(cp); 46227c478bd9Sstevel@tonic-gate *str = save; 46237c478bd9Sstevel@tonic-gate 46247c478bd9Sstevel@tonic-gate str++; 46257c478bd9Sstevel@tonic-gate save = *str; 46267c478bd9Sstevel@tonic-gate *str = '\0'; 46277c478bd9Sstevel@tonic-gate sep = s_strdup(str - 1); 46287c478bd9Sstevel@tonic-gate *str = save; 46297c478bd9Sstevel@tonic-gate 46307c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 46317c478bd9Sstevel@tonic-gate str++; 46327c478bd9Sstevel@tonic-gate if (*str == '\0') 46337c478bd9Sstevel@tonic-gate arg = NULL; 46347c478bd9Sstevel@tonic-gate else 46357c478bd9Sstevel@tonic-gate arg = s_strdup(str); 46367c478bd9Sstevel@tonic-gate } 46377c478bd9Sstevel@tonic-gate } 46387c478bd9Sstevel@tonic-gate 46397c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 46407c478bd9Sstevel@tonic-gate 46417c478bd9Sstevel@tonic-gate lp->cmd = cmd; 46427c478bd9Sstevel@tonic-gate lp->sep = sep; 46437c478bd9Sstevel@tonic-gate lp->arg = arg; 46447c478bd9Sstevel@tonic-gate lp->line = line; 46457c478bd9Sstevel@tonic-gate lp->lineNum = ++(*lineNum); 46467c478bd9Sstevel@tonic-gate if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) { 46477c478bd9Sstevel@tonic-gate lp->entryNum = ++(*entryNum); 46487c478bd9Sstevel@tonic-gate lp->flags = BAM_TITLE; 46497c478bd9Sstevel@tonic-gate if (prev && prev->flags == BAM_COMMENT && 4650ae115bc7Smrj prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) { 46517c478bd9Sstevel@tonic-gate prev->entryNum = lp->entryNum; 46528c1b6884Sszhou curr_ent = boot_entry_new(mp, prev, lp); 4653eb2bd662Svikram curr_ent->flags |= BAM_ENTRY_BOOTADM; 4654eb2bd662Svikram BAM_DPRINTF((D_IS_BOOTADM_ENTRY, fcn, arg)); 46558c1b6884Sszhou } else { 46568c1b6884Sszhou curr_ent = boot_entry_new(mp, lp, lp); 4657ae115bc7Smrj if (in_liveupgrade) { 4658eb2bd662Svikram curr_ent->flags |= BAM_ENTRY_LU; 4659eb2bd662Svikram BAM_DPRINTF((D_IS_LU_ENTRY, fcn, arg)); 46608c1b6884Sszhou } 4661ae115bc7Smrj } 4662ae115bc7Smrj curr_ent->entryNum = *entryNum; 46637c478bd9Sstevel@tonic-gate } else if (flag != BAM_INVALID) { 46647c478bd9Sstevel@tonic-gate /* 46657c478bd9Sstevel@tonic-gate * For header comments, the entry# is "fixed up" 46667c478bd9Sstevel@tonic-gate * by the subsequent title 46677c478bd9Sstevel@tonic-gate */ 46687c478bd9Sstevel@tonic-gate lp->entryNum = *entryNum; 46697c478bd9Sstevel@tonic-gate lp->flags = flag; 46707c478bd9Sstevel@tonic-gate } else { 46717c478bd9Sstevel@tonic-gate lp->entryNum = *entryNum; 4672ae115bc7Smrj 4673ae115bc7Smrj if (*entryNum == ENTRY_INIT) { 4674ae115bc7Smrj lp->flags = BAM_GLOBAL; 4675ae115bc7Smrj } else { 4676ae115bc7Smrj lp->flags = BAM_ENTRY; 4677ae115bc7Smrj 4678ae115bc7Smrj if (cmd && arg) { 4679eb2bd662Svikram if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0) { 4680eb2bd662Svikram BAM_DPRINTF((D_IS_ROOT_CMD, fcn, arg)); 4681ae115bc7Smrj curr_ent->flags |= BAM_ENTRY_ROOT; 4682eb2bd662Svikram } else if (strcmp(cmd, menu_cmds[FINDROOT_CMD]) 4683eb2bd662Svikram == 0) { 4684eb2bd662Svikram BAM_DPRINTF((D_IS_FINDROOT_CMD, fcn, 4685eb2bd662Svikram arg)); 4686eb2bd662Svikram curr_ent->flags |= BAM_ENTRY_FINDROOT; 4687eb2bd662Svikram } else if (strcmp(cmd, 4688eb2bd662Svikram menu_cmds[CHAINLOADER_CMD]) == 0) { 4689eb2bd662Svikram BAM_DPRINTF((D_IS_CHAINLOADER_CMD, fcn, 4690eb2bd662Svikram arg)); 4691ae115bc7Smrj curr_ent->flags |= 4692ae115bc7Smrj BAM_ENTRY_CHAINLOADER; 4693eb2bd662Svikram } else if (kernel_parser(curr_ent, cmd, arg, 4694eb2bd662Svikram lp->lineNum) != BAM_SUCCESS) { 4695eb2bd662Svikram (void) module_parser(curr_ent, cmd, 4696eb2bd662Svikram arg, lp->lineNum); 4697eb2bd662Svikram } 4698ae115bc7Smrj } 4699ae115bc7Smrj } 47007c478bd9Sstevel@tonic-gate } 47017c478bd9Sstevel@tonic-gate 47028c1b6884Sszhou /* record default, old default, and entry line ranges */ 4703eac223ccSWilliam Kucharski if (lp->flags == BAM_GLOBAL && lp->cmd != NULL && 47048c1b6884Sszhou strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) { 47058c1b6884Sszhou mp->curdefault = lp; 47068c1b6884Sszhou } else if (lp->flags == BAM_COMMENT && 47078c1b6884Sszhou strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) { 47088c1b6884Sszhou mp->olddefault = lp; 4709ae115bc7Smrj } else if (lp->flags == BAM_COMMENT && 4710ae115bc7Smrj strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) { 4711ae115bc7Smrj mp->old_rc_default = lp; 47128c1b6884Sszhou } else if (lp->flags == BAM_ENTRY || 4713ae115bc7Smrj (lp->flags == BAM_COMMENT && 4714772d6a58SWilliam Kucharski ((strcmp(lp->arg, BAM_BOOTADM_FTR) == 0) || is_libbe_ent))) { 4715772d6a58SWilliam Kucharski if (is_libbe_ent) { 4716772d6a58SWilliam Kucharski curr_ent->flags |= BAM_ENTRY_LIBBE; 4717772d6a58SWilliam Kucharski is_libbe_ent = 0; 4718772d6a58SWilliam Kucharski } 4719772d6a58SWilliam Kucharski 47208c1b6884Sszhou boot_entry_addline(curr_ent, lp); 47218c1b6884Sszhou } 47227c478bd9Sstevel@tonic-gate append_line(mp, lp); 47237c478bd9Sstevel@tonic-gate 47247c478bd9Sstevel@tonic-gate prev = lp; 47257c478bd9Sstevel@tonic-gate } 47267c478bd9Sstevel@tonic-gate 4727eb2bd662Svikram void 472840541d5dSvikram update_numbering(menu_t *mp) 472940541d5dSvikram { 473040541d5dSvikram int lineNum; 473140541d5dSvikram int entryNum; 473240541d5dSvikram int old_default_value; 473340541d5dSvikram line_t *lp, *prev, *default_lp, *default_entry; 473440541d5dSvikram char buf[PATH_MAX]; 473540541d5dSvikram 473640541d5dSvikram if (mp->start == NULL) { 473740541d5dSvikram return; 473840541d5dSvikram } 473940541d5dSvikram 474040541d5dSvikram lineNum = LINE_INIT; 474140541d5dSvikram entryNum = ENTRY_INIT; 474240541d5dSvikram old_default_value = ENTRY_INIT; 474340541d5dSvikram lp = default_lp = default_entry = NULL; 474440541d5dSvikram 474540541d5dSvikram prev = NULL; 474640541d5dSvikram for (lp = mp->start; lp; prev = lp, lp = lp->next) { 474740541d5dSvikram lp->lineNum = ++lineNum; 474840541d5dSvikram 474940541d5dSvikram /* 475040541d5dSvikram * Get the value of the default command 475140541d5dSvikram */ 4752eac223ccSWilliam Kucharski if (lp->entryNum == ENTRY_INIT && lp->cmd != NULL && 475340541d5dSvikram strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 && 475440541d5dSvikram lp->arg) { 475540541d5dSvikram old_default_value = atoi(lp->arg); 475640541d5dSvikram default_lp = lp; 475740541d5dSvikram } 475840541d5dSvikram 475940541d5dSvikram /* 4760eb2bd662Svikram * If not a booting entry, nothing else to fix for this 476140541d5dSvikram * entry 476240541d5dSvikram */ 476340541d5dSvikram if (lp->entryNum == ENTRY_INIT) 476440541d5dSvikram continue; 476540541d5dSvikram 476640541d5dSvikram /* 476740541d5dSvikram * Record the position of the default entry. 476840541d5dSvikram * The following works because global 476940541d5dSvikram * commands like default and timeout should precede 477040541d5dSvikram * actual boot entries, so old_default_value 477140541d5dSvikram * is already known (or default cmd is missing). 477240541d5dSvikram */ 477340541d5dSvikram if (default_entry == NULL && 477440541d5dSvikram old_default_value != ENTRY_INIT && 477540541d5dSvikram lp->entryNum == old_default_value) { 477640541d5dSvikram default_entry = lp; 477740541d5dSvikram } 477840541d5dSvikram 477940541d5dSvikram /* 478040541d5dSvikram * Now fixup the entry number 478140541d5dSvikram */ 4782eac223ccSWilliam Kucharski if (lp->cmd != NULL && 4783eac223ccSWilliam Kucharski strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) { 478440541d5dSvikram lp->entryNum = ++entryNum; 478540541d5dSvikram /* fixup the bootadm header */ 478640541d5dSvikram if (prev && prev->flags == BAM_COMMENT && 4787ae115bc7Smrj prev->arg && 4788ae115bc7Smrj strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) { 478940541d5dSvikram prev->entryNum = lp->entryNum; 479040541d5dSvikram } 479140541d5dSvikram } else { 479240541d5dSvikram lp->entryNum = entryNum; 479340541d5dSvikram } 479440541d5dSvikram } 479540541d5dSvikram 479640541d5dSvikram /* 479740541d5dSvikram * No default command in menu, simply return 479840541d5dSvikram */ 479940541d5dSvikram if (default_lp == NULL) { 480040541d5dSvikram return; 480140541d5dSvikram } 480240541d5dSvikram 480340541d5dSvikram free(default_lp->arg); 480440541d5dSvikram free(default_lp->line); 480540541d5dSvikram 480640541d5dSvikram if (default_entry == NULL) { 480740541d5dSvikram default_lp->arg = s_strdup("0"); 480840541d5dSvikram } else { 480940541d5dSvikram (void) snprintf(buf, sizeof (buf), "%d", 481040541d5dSvikram default_entry->entryNum); 481140541d5dSvikram default_lp->arg = s_strdup(buf); 481240541d5dSvikram } 481340541d5dSvikram 481440541d5dSvikram /* 481540541d5dSvikram * The following is required since only the line field gets 481640541d5dSvikram * written back to menu.lst 481740541d5dSvikram */ 481840541d5dSvikram (void) snprintf(buf, sizeof (buf), "%s%s%s", 481940541d5dSvikram menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg); 482040541d5dSvikram default_lp->line = s_strdup(buf); 482140541d5dSvikram } 482240541d5dSvikram 482340541d5dSvikram 48247c478bd9Sstevel@tonic-gate static menu_t * 48257c478bd9Sstevel@tonic-gate menu_read(char *menu_path) 48267c478bd9Sstevel@tonic-gate { 48277c478bd9Sstevel@tonic-gate FILE *fp; 48287c478bd9Sstevel@tonic-gate char buf[BAM_MAXLINE], *cp; 48297c478bd9Sstevel@tonic-gate menu_t *mp; 48307c478bd9Sstevel@tonic-gate int line, entry, len, n; 48317c478bd9Sstevel@tonic-gate 48327c478bd9Sstevel@tonic-gate mp = s_calloc(1, sizeof (menu_t)); 48337c478bd9Sstevel@tonic-gate 48347c478bd9Sstevel@tonic-gate fp = fopen(menu_path, "r"); 48357c478bd9Sstevel@tonic-gate if (fp == NULL) { /* Let the caller handle this error */ 483644da779fSWilliam Kucharski free(mp); 483744da779fSWilliam Kucharski return (NULL); 48387c478bd9Sstevel@tonic-gate } 48397c478bd9Sstevel@tonic-gate 48407c478bd9Sstevel@tonic-gate /* Note: GRUB boot entry number starts with 0 */ 48417c478bd9Sstevel@tonic-gate line = LINE_INIT; 48427c478bd9Sstevel@tonic-gate entry = ENTRY_INIT; 48437c478bd9Sstevel@tonic-gate cp = buf; 48447c478bd9Sstevel@tonic-gate len = sizeof (buf); 48457c478bd9Sstevel@tonic-gate while (s_fgets(cp, len, fp) != NULL) { 48467c478bd9Sstevel@tonic-gate n = strlen(cp); 48477c478bd9Sstevel@tonic-gate if (cp[n - 1] == '\\') { 48487c478bd9Sstevel@tonic-gate len -= n - 1; 48497c478bd9Sstevel@tonic-gate assert(len >= 2); 48507c478bd9Sstevel@tonic-gate cp += n - 1; 48517c478bd9Sstevel@tonic-gate continue; 48527c478bd9Sstevel@tonic-gate } 48537c478bd9Sstevel@tonic-gate line_parser(mp, buf, &line, &entry); 48547c478bd9Sstevel@tonic-gate cp = buf; 48557c478bd9Sstevel@tonic-gate len = sizeof (buf); 48567c478bd9Sstevel@tonic-gate } 48577c478bd9Sstevel@tonic-gate 48587c478bd9Sstevel@tonic-gate if (fclose(fp) == EOF) { 48597c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, menu_path, strerror(errno)); 48607c478bd9Sstevel@tonic-gate } 48617c478bd9Sstevel@tonic-gate 48627c478bd9Sstevel@tonic-gate return (mp); 48637c478bd9Sstevel@tonic-gate } 48647c478bd9Sstevel@tonic-gate 48657c478bd9Sstevel@tonic-gate static error_t 48667c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title) 48677c478bd9Sstevel@tonic-gate { 48687c478bd9Sstevel@tonic-gate char *eq; 48697c478bd9Sstevel@tonic-gate char *opt_dup; 48707c478bd9Sstevel@tonic-gate int entryNum; 48717c478bd9Sstevel@tonic-gate 48727c478bd9Sstevel@tonic-gate assert(mp); 48737c478bd9Sstevel@tonic-gate assert(mp->start); 48747c478bd9Sstevel@tonic-gate assert(opt); 48757c478bd9Sstevel@tonic-gate 48767c478bd9Sstevel@tonic-gate opt_dup = s_strdup(opt); 48777c478bd9Sstevel@tonic-gate 48787c478bd9Sstevel@tonic-gate if (entry) 48797c478bd9Sstevel@tonic-gate *entry = ENTRY_INIT; 48807c478bd9Sstevel@tonic-gate if (title) 48817c478bd9Sstevel@tonic-gate *title = NULL; 48827c478bd9Sstevel@tonic-gate 48837c478bd9Sstevel@tonic-gate eq = strchr(opt_dup, '='); 48847c478bd9Sstevel@tonic-gate if (eq == NULL) { 48857c478bd9Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 48867c478bd9Sstevel@tonic-gate free(opt_dup); 48877c478bd9Sstevel@tonic-gate return (BAM_ERROR); 48887c478bd9Sstevel@tonic-gate } 48897c478bd9Sstevel@tonic-gate 48907c478bd9Sstevel@tonic-gate *eq = '\0'; 48917c478bd9Sstevel@tonic-gate if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) { 48927c478bd9Sstevel@tonic-gate assert(mp->end); 48937c478bd9Sstevel@tonic-gate entryNum = s_strtol(eq + 1); 48947c478bd9Sstevel@tonic-gate if (entryNum < 0 || entryNum > mp->end->entryNum) { 48957c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, eq + 1); 48967c478bd9Sstevel@tonic-gate free(opt_dup); 48977c478bd9Sstevel@tonic-gate return (BAM_ERROR); 48987c478bd9Sstevel@tonic-gate } 48997c478bd9Sstevel@tonic-gate *entry = entryNum; 49007c478bd9Sstevel@tonic-gate } else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) { 49017c478bd9Sstevel@tonic-gate *title = opt + (eq - opt_dup) + 1; 49027c478bd9Sstevel@tonic-gate } else { 49037c478bd9Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 49047c478bd9Sstevel@tonic-gate free(opt_dup); 49057c478bd9Sstevel@tonic-gate return (BAM_ERROR); 49067c478bd9Sstevel@tonic-gate } 49077c478bd9Sstevel@tonic-gate 49087c478bd9Sstevel@tonic-gate free(opt_dup); 49097c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 49107c478bd9Sstevel@tonic-gate } 49117c478bd9Sstevel@tonic-gate 49127c478bd9Sstevel@tonic-gate /* 49137c478bd9Sstevel@tonic-gate * If invoked with no titles/entries (opt == NULL) 49147c478bd9Sstevel@tonic-gate * only title lines in file are printed. 49157c478bd9Sstevel@tonic-gate * 49167c478bd9Sstevel@tonic-gate * If invoked with a title or entry #, all 49177c478bd9Sstevel@tonic-gate * lines in *every* matching entry are listed 49187c478bd9Sstevel@tonic-gate */ 49197c478bd9Sstevel@tonic-gate static error_t 49207c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt) 49217c478bd9Sstevel@tonic-gate { 49227c478bd9Sstevel@tonic-gate line_t *lp; 49237c478bd9Sstevel@tonic-gate int entry = ENTRY_INIT; 49247c478bd9Sstevel@tonic-gate int found; 49257c478bd9Sstevel@tonic-gate char *title = NULL; 49267c478bd9Sstevel@tonic-gate 49277c478bd9Sstevel@tonic-gate assert(mp); 49287c478bd9Sstevel@tonic-gate assert(menu_path); 49297c478bd9Sstevel@tonic-gate 4930eb2bd662Svikram /* opt is optional */ 4931eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, "list_entry", menu_path, 4932eb2bd662Svikram opt ? opt : "<NULL>")); 4933eb2bd662Svikram 49347c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 49357c478bd9Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 49367c478bd9Sstevel@tonic-gate return (BAM_ERROR); 49377c478bd9Sstevel@tonic-gate } 49387c478bd9Sstevel@tonic-gate 49397c478bd9Sstevel@tonic-gate if (opt != NULL) { 49407c478bd9Sstevel@tonic-gate if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) { 49417c478bd9Sstevel@tonic-gate return (BAM_ERROR); 49427c478bd9Sstevel@tonic-gate } 49437c478bd9Sstevel@tonic-gate assert((entry != ENTRY_INIT) ^ (title != NULL)); 49447c478bd9Sstevel@tonic-gate } else { 49457c478bd9Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0); 49467c478bd9Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0); 49477c478bd9Sstevel@tonic-gate } 49487c478bd9Sstevel@tonic-gate 49497c478bd9Sstevel@tonic-gate found = 0; 49507c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 49517c478bd9Sstevel@tonic-gate if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY) 49527c478bd9Sstevel@tonic-gate continue; 49537c478bd9Sstevel@tonic-gate if (opt == NULL && lp->flags == BAM_TITLE) { 49547c478bd9Sstevel@tonic-gate bam_print(PRINT_TITLE, lp->entryNum, 49557c478bd9Sstevel@tonic-gate lp->arg); 49567c478bd9Sstevel@tonic-gate found = 1; 49577c478bd9Sstevel@tonic-gate continue; 49587c478bd9Sstevel@tonic-gate } 49597c478bd9Sstevel@tonic-gate if (entry != ENTRY_INIT && lp->entryNum == entry) { 49607c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 49617c478bd9Sstevel@tonic-gate found = 1; 49627c478bd9Sstevel@tonic-gate continue; 49637c478bd9Sstevel@tonic-gate } 49647c478bd9Sstevel@tonic-gate 49657c478bd9Sstevel@tonic-gate /* 49667c478bd9Sstevel@tonic-gate * We set the entry value here so that all lines 49677c478bd9Sstevel@tonic-gate * in entry get printed. If we subsequently match 49687c478bd9Sstevel@tonic-gate * title in other entries, all lines in those 49697c478bd9Sstevel@tonic-gate * entries get printed as well. 49707c478bd9Sstevel@tonic-gate */ 49717c478bd9Sstevel@tonic-gate if (title && lp->flags == BAM_TITLE && lp->arg && 49727c478bd9Sstevel@tonic-gate strncmp(title, lp->arg, strlen(title)) == 0) { 49737c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 49747c478bd9Sstevel@tonic-gate entry = lp->entryNum; 49757c478bd9Sstevel@tonic-gate found = 1; 49767c478bd9Sstevel@tonic-gate continue; 49777c478bd9Sstevel@tonic-gate } 49787c478bd9Sstevel@tonic-gate } 49797c478bd9Sstevel@tonic-gate 49807c478bd9Sstevel@tonic-gate if (!found) { 49817c478bd9Sstevel@tonic-gate bam_error(NO_MATCH_ENTRY); 49827c478bd9Sstevel@tonic-gate return (BAM_ERROR); 49837c478bd9Sstevel@tonic-gate } 49847c478bd9Sstevel@tonic-gate 49857c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 49867c478bd9Sstevel@tonic-gate } 49877c478bd9Sstevel@tonic-gate 4988843e1988Sjohnlev int 49897c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp, 49907c478bd9Sstevel@tonic-gate char *title, 4991eb2bd662Svikram char *findroot, 49927c478bd9Sstevel@tonic-gate char *kernel, 4993843e1988Sjohnlev char *mod_kernel, 499444da779fSWilliam Kucharski char *module, 499544da779fSWilliam Kucharski char *bootfs) 49967c478bd9Sstevel@tonic-gate { 4997eb2bd662Svikram int lineNum; 4998eb2bd662Svikram int entryNum; 49997c478bd9Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 5000eb2bd662Svikram menu_cmd_t k_cmd; 5001eb2bd662Svikram menu_cmd_t m_cmd; 5002eb2bd662Svikram const char *fcn = "add_boot_entry()"; 50037c478bd9Sstevel@tonic-gate 50047c478bd9Sstevel@tonic-gate assert(mp); 50057c478bd9Sstevel@tonic-gate 5006eb2bd662Svikram INJECT_ERROR1("ADD_BOOT_ENTRY_FINDROOT_NULL", findroot = NULL); 5007eb2bd662Svikram if (findroot == NULL) { 5008eb2bd662Svikram bam_error(NULL_FINDROOT); 5009eb2bd662Svikram return (BAM_ERROR); 5010eb2bd662Svikram } 5011eb2bd662Svikram 50127c478bd9Sstevel@tonic-gate if (title == NULL) { 5013ee3b8144Sszhou title = "Solaris"; /* default to Solaris */ 50147c478bd9Sstevel@tonic-gate } 50157c478bd9Sstevel@tonic-gate if (kernel == NULL) { 50167c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]); 50177c478bd9Sstevel@tonic-gate return (BAM_ERROR); 50187c478bd9Sstevel@tonic-gate } 50197c478bd9Sstevel@tonic-gate if (module == NULL) { 5020ae115bc7Smrj if (bam_direct != BAM_DIRECT_DBOOT) { 50217c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]); 50227c478bd9Sstevel@tonic-gate return (BAM_ERROR); 50237c478bd9Sstevel@tonic-gate } 50247c478bd9Sstevel@tonic-gate 5025ae115bc7Smrj /* Figure the commands out from the kernel line */ 5026ae115bc7Smrj if (strstr(kernel, "$ISADIR") != NULL) { 5027ae115bc7Smrj module = DIRECT_BOOT_ARCHIVE; 5028ae115bc7Smrj } else if (strstr(kernel, "amd64") != NULL) { 5029ae115bc7Smrj module = DIRECT_BOOT_ARCHIVE_64; 5030ae115bc7Smrj } else { 5031ae115bc7Smrj module = DIRECT_BOOT_ARCHIVE_32; 5032ae115bc7Smrj } 50339adc5a4eSEdward Gillett } 50349adc5a4eSEdward Gillett 5035ae115bc7Smrj k_cmd = KERNEL_DOLLAR_CMD; 5036ae115bc7Smrj m_cmd = MODULE_DOLLAR_CMD; 5037ae115bc7Smrj 50387c478bd9Sstevel@tonic-gate if (mp->start) { 50397c478bd9Sstevel@tonic-gate lineNum = mp->end->lineNum; 50407c478bd9Sstevel@tonic-gate entryNum = mp->end->entryNum; 50417c478bd9Sstevel@tonic-gate } else { 50427c478bd9Sstevel@tonic-gate lineNum = LINE_INIT; 50437c478bd9Sstevel@tonic-gate entryNum = ENTRY_INIT; 50447c478bd9Sstevel@tonic-gate } 50457c478bd9Sstevel@tonic-gate 50467c478bd9Sstevel@tonic-gate /* 50477c478bd9Sstevel@tonic-gate * No separator for comment (HDR/FTR) commands 50487c478bd9Sstevel@tonic-gate * The syntax for comments is #<comment> 50497c478bd9Sstevel@tonic-gate */ 50507c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 5051ae115bc7Smrj menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR); 50528c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 50537c478bd9Sstevel@tonic-gate 50547c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 50557c478bd9Sstevel@tonic-gate menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 50568c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 50577c478bd9Sstevel@tonic-gate 50587c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 5059eb2bd662Svikram menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot); 50608c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 5061eb2bd662Svikram BAM_DPRINTF((D_ADD_FINDROOT_NUM, fcn, lineNum, entryNum)); 50627c478bd9Sstevel@tonic-gate 506344da779fSWilliam Kucharski if (bootfs != NULL) { 506444da779fSWilliam Kucharski (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 506544da779fSWilliam Kucharski menu_cmds[BOOTFS_CMD], menu_cmds[SEP_CMD], bootfs); 506644da779fSWilliam Kucharski line_parser(mp, linebuf, &lineNum, &entryNum); 506744da779fSWilliam Kucharski } 506844da779fSWilliam Kucharski 50697c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 5070ae115bc7Smrj menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel); 50718c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 50727c478bd9Sstevel@tonic-gate 5073843e1988Sjohnlev if (mod_kernel != NULL) { 5074843e1988Sjohnlev (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 5075843e1988Sjohnlev menu_cmds[m_cmd], menu_cmds[SEP_CMD], mod_kernel); 5076843e1988Sjohnlev line_parser(mp, linebuf, &lineNum, &entryNum); 5077843e1988Sjohnlev } 5078843e1988Sjohnlev 50797c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 5080ae115bc7Smrj menu_cmds[m_cmd], menu_cmds[SEP_CMD], module); 50818c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 50827c478bd9Sstevel@tonic-gate 50837c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 5084ae115bc7Smrj menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR); 50858c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 50867c478bd9Sstevel@tonic-gate 50877c478bd9Sstevel@tonic-gate return (entryNum); 50887c478bd9Sstevel@tonic-gate } 50897c478bd9Sstevel@tonic-gate 509044da779fSWilliam Kucharski error_t 509144da779fSWilliam Kucharski delete_boot_entry(menu_t *mp, int entryNum, int quiet) 50927c478bd9Sstevel@tonic-gate { 5093eb2bd662Svikram line_t *lp; 5094eb2bd662Svikram line_t *freed; 5095eb2bd662Svikram entry_t *ent; 5096eb2bd662Svikram entry_t *tmp; 509744da779fSWilliam Kucharski int deleted = 0; 509844da779fSWilliam Kucharski const char *fcn = "delete_boot_entry()"; 50997c478bd9Sstevel@tonic-gate 51007c478bd9Sstevel@tonic-gate assert(entryNum != ENTRY_INIT); 51017c478bd9Sstevel@tonic-gate 5102eb2bd662Svikram tmp = NULL; 5103eb2bd662Svikram 51048c1b6884Sszhou ent = mp->entries; 51058c1b6884Sszhou while (ent) { 51068c1b6884Sszhou lp = ent->start; 5107772d6a58SWilliam Kucharski 5108772d6a58SWilliam Kucharski /* 5109772d6a58SWilliam Kucharski * Check entry number and make sure it's a modifiable entry. 5110772d6a58SWilliam Kucharski * 5111772d6a58SWilliam Kucharski * Guidelines: 5112772d6a58SWilliam Kucharski * + We can modify a bootadm-created entry 5113772d6a58SWilliam Kucharski * + We can modify a libbe-created entry 5114772d6a58SWilliam Kucharski */ 5115772d6a58SWilliam Kucharski if ((lp->flags != BAM_COMMENT && 5116772d6a58SWilliam Kucharski (((ent->flags & BAM_ENTRY_LIBBE) == 0) && 5117772d6a58SWilliam Kucharski strcmp(lp->arg, BAM_BOOTADM_HDR) != 0)) || 51188c1b6884Sszhou (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) { 51198c1b6884Sszhou ent = ent->next; 51207c478bd9Sstevel@tonic-gate continue; 51217c478bd9Sstevel@tonic-gate } 51227c478bd9Sstevel@tonic-gate 51238c1b6884Sszhou /* free the entry content */ 51248c1b6884Sszhou do { 51258c1b6884Sszhou freed = lp; 51268c1b6884Sszhou lp = lp->next; /* prev stays the same */ 5127eb2bd662Svikram BAM_DPRINTF((D_FREEING_LINE, fcn, freed->lineNum)); 51288c1b6884Sszhou unlink_line(mp, freed); 51298c1b6884Sszhou line_free(freed); 51308c1b6884Sszhou } while (freed != ent->end); 51317c478bd9Sstevel@tonic-gate 51328c1b6884Sszhou /* free the entry_t structure */ 5133eb2bd662Svikram assert(tmp == NULL); 51348c1b6884Sszhou tmp = ent; 51358c1b6884Sszhou ent = ent->next; 51368c1b6884Sszhou if (tmp->prev) 51378c1b6884Sszhou tmp->prev->next = ent; 51387c478bd9Sstevel@tonic-gate else 51398c1b6884Sszhou mp->entries = ent; 51408c1b6884Sszhou if (ent) 51418c1b6884Sszhou ent->prev = tmp->prev; 5142eb2bd662Svikram BAM_DPRINTF((D_FREEING_ENTRY, fcn, tmp->entryNum)); 5143eb2bd662Svikram free(tmp); 5144eb2bd662Svikram tmp = NULL; 51457c478bd9Sstevel@tonic-gate deleted = 1; 51467c478bd9Sstevel@tonic-gate } 51477c478bd9Sstevel@tonic-gate 5148eb2bd662Svikram assert(tmp == NULL); 5149eb2bd662Svikram 51507c478bd9Sstevel@tonic-gate if (!deleted && entryNum != ALL_ENTRIES) { 515144da779fSWilliam Kucharski if (quiet == DBE_PRINTERR) 51527c478bd9Sstevel@tonic-gate bam_error(NO_BOOTADM_MATCH); 51537c478bd9Sstevel@tonic-gate return (BAM_ERROR); 51547c478bd9Sstevel@tonic-gate } 51557c478bd9Sstevel@tonic-gate 515640541d5dSvikram /* 515740541d5dSvikram * Now that we have deleted an entry, update 515840541d5dSvikram * the entry numbering and the default cmd. 515940541d5dSvikram */ 516040541d5dSvikram update_numbering(mp); 516140541d5dSvikram 51627c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 51637c478bd9Sstevel@tonic-gate } 51647c478bd9Sstevel@tonic-gate 51657c478bd9Sstevel@tonic-gate static error_t 5166eb2bd662Svikram delete_all_entries(menu_t *mp, char *dummy, char *opt) 51677c478bd9Sstevel@tonic-gate { 51687c478bd9Sstevel@tonic-gate assert(mp); 5169eb2bd662Svikram assert(dummy == NULL); 51707c478bd9Sstevel@tonic-gate assert(opt == NULL); 51717c478bd9Sstevel@tonic-gate 5172eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY0, "delete_all_entries")); 5173eb2bd662Svikram 51747c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 5175eb2bd662Svikram bam_print(EMPTY_MENU); 51767c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 51777c478bd9Sstevel@tonic-gate } 51787c478bd9Sstevel@tonic-gate 517944da779fSWilliam Kucharski if (delete_boot_entry(mp, ALL_ENTRIES, DBE_PRINTERR) != BAM_SUCCESS) { 51807c478bd9Sstevel@tonic-gate return (BAM_ERROR); 51817c478bd9Sstevel@tonic-gate } 51827c478bd9Sstevel@tonic-gate 51837c478bd9Sstevel@tonic-gate return (BAM_WRITE); 51847c478bd9Sstevel@tonic-gate } 51857c478bd9Sstevel@tonic-gate 51867c478bd9Sstevel@tonic-gate static FILE * 5187eb2bd662Svikram create_diskmap(char *osroot) 51887c478bd9Sstevel@tonic-gate { 51897c478bd9Sstevel@tonic-gate FILE *fp; 5190cedc7e57SEnrico Perla - Sun Microsystems char cmd[PATH_MAX + 16]; 5191cedc7e57SEnrico Perla - Sun Microsystems char path[PATH_MAX]; 5192eb2bd662Svikram const char *fcn = "create_diskmap()"; 51937c478bd9Sstevel@tonic-gate 51947c478bd9Sstevel@tonic-gate /* make sure we have a map file */ 51957c478bd9Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 51967c478bd9Sstevel@tonic-gate if (fp == NULL) { 5197cedc7e57SEnrico Perla - Sun Microsystems int ret; 5198cedc7e57SEnrico Perla - Sun Microsystems 5199cedc7e57SEnrico Perla - Sun Microsystems ret = snprintf(path, sizeof (path), "%s/%s", osroot, 5200cedc7e57SEnrico Perla - Sun Microsystems CREATE_DISKMAP); 5201cedc7e57SEnrico Perla - Sun Microsystems if (ret >= sizeof (path)) { 5202cedc7e57SEnrico Perla - Sun Microsystems bam_error(PATH_TOO_LONG, osroot); 5203cedc7e57SEnrico Perla - Sun Microsystems return (NULL); 5204cedc7e57SEnrico Perla - Sun Microsystems } 5205cedc7e57SEnrico Perla - Sun Microsystems if (is_safe_exec(path) == BAM_ERROR) 5206cedc7e57SEnrico Perla - Sun Microsystems return (NULL); 5207cedc7e57SEnrico Perla - Sun Microsystems 52087c478bd9Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), 5209eb2bd662Svikram "%s/%s > /dev/null", osroot, CREATE_DISKMAP); 5210eb2bd662Svikram if (exec_cmd(cmd, NULL) != 0) 5211eb2bd662Svikram return (NULL); 52127c478bd9Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 5213eb2bd662Svikram INJECT_ERROR1("DISKMAP_CREATE_FAIL", fp = NULL); 5214eb2bd662Svikram if (fp) { 5215eb2bd662Svikram BAM_DPRINTF((D_CREATED_DISKMAP, fcn, GRUBDISK_MAP)); 5216eb2bd662Svikram } else { 5217eb2bd662Svikram BAM_DPRINTF((D_CREATE_DISKMAP_FAIL, fcn, GRUBDISK_MAP)); 5218eb2bd662Svikram } 52197c478bd9Sstevel@tonic-gate } 52207c478bd9Sstevel@tonic-gate return (fp); 52217c478bd9Sstevel@tonic-gate } 52227c478bd9Sstevel@tonic-gate 52237c478bd9Sstevel@tonic-gate #define SECTOR_SIZE 512 52247c478bd9Sstevel@tonic-gate 52257c478bd9Sstevel@tonic-gate static int 52267c478bd9Sstevel@tonic-gate get_partition(char *device) 52277c478bd9Sstevel@tonic-gate { 52281a902ef8SHans Rosenfeld int i, fd, is_pcfs, partno = PARTNO_NOTFOUND; 52297c478bd9Sstevel@tonic-gate struct mboot *mboot; 52307c478bd9Sstevel@tonic-gate char boot_sect[SECTOR_SIZE]; 52317c478bd9Sstevel@tonic-gate char *wholedisk, *slice; 5232e998e519SSheshadri Vasudevan #ifdef i386 5233e998e519SSheshadri Vasudevan ext_part_t *epp; 5234e998e519SSheshadri Vasudevan uint32_t secnum, numsec; 52351a902ef8SHans Rosenfeld int rval, pno, ext_partno = PARTNO_NOTFOUND; 5236e998e519SSheshadri Vasudevan #endif 52377c478bd9Sstevel@tonic-gate 52387c478bd9Sstevel@tonic-gate /* form whole disk (p0) */ 52397c478bd9Sstevel@tonic-gate slice = device + strlen(device) - 2; 52407c478bd9Sstevel@tonic-gate is_pcfs = (*slice != 's'); 52417c478bd9Sstevel@tonic-gate if (!is_pcfs) 52427c478bd9Sstevel@tonic-gate *slice = '\0'; 52437c478bd9Sstevel@tonic-gate wholedisk = s_calloc(1, strlen(device) + 3); 52447c478bd9Sstevel@tonic-gate (void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device); 52457c478bd9Sstevel@tonic-gate if (!is_pcfs) 52467c478bd9Sstevel@tonic-gate *slice = 's'; 52477c478bd9Sstevel@tonic-gate 52487c478bd9Sstevel@tonic-gate /* read boot sector */ 52497c478bd9Sstevel@tonic-gate fd = open(wholedisk, O_RDONLY); 52507c478bd9Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 52517c478bd9Sstevel@tonic-gate return (partno); 52527c478bd9Sstevel@tonic-gate } 52537c478bd9Sstevel@tonic-gate (void) close(fd); 52547c478bd9Sstevel@tonic-gate 5255e998e519SSheshadri Vasudevan #ifdef i386 5256e998e519SSheshadri Vasudevan /* Read/Initialize extended partition information */ 5257e998e519SSheshadri Vasudevan if ((rval = libfdisk_init(&epp, wholedisk, NULL, FDISK_READ_DISK)) 5258e998e519SSheshadri Vasudevan != FDISK_SUCCESS) { 5259e998e519SSheshadri Vasudevan switch (rval) { 5260e998e519SSheshadri Vasudevan /* 5261e998e519SSheshadri Vasudevan * FDISK_EBADLOGDRIVE and FDISK_ENOLOGDRIVE can 5262e998e519SSheshadri Vasudevan * be considered as soft errors and hence 5263e998e519SSheshadri Vasudevan * we do not return 5264e998e519SSheshadri Vasudevan */ 5265e998e519SSheshadri Vasudevan case FDISK_EBADLOGDRIVE: 5266e998e519SSheshadri Vasudevan break; 5267e998e519SSheshadri Vasudevan case FDISK_ENOLOGDRIVE: 5268e998e519SSheshadri Vasudevan break; 52696cb5747bSSharath M Srinivasan case FDISK_EBADMAGIC: 52706cb5747bSSharath M Srinivasan /*FALLTHROUGH*/ 5271e998e519SSheshadri Vasudevan default: 5272e998e519SSheshadri Vasudevan free(wholedisk); 52736cb5747bSSharath M Srinivasan libfdisk_fini(&epp); 5274e998e519SSheshadri Vasudevan return (partno); 5275e998e519SSheshadri Vasudevan } 5276e998e519SSheshadri Vasudevan } 5277e998e519SSheshadri Vasudevan #endif 5278e998e519SSheshadri Vasudevan free(wholedisk); 5279e998e519SSheshadri Vasudevan 52807c478bd9Sstevel@tonic-gate /* parse fdisk table */ 52817c478bd9Sstevel@tonic-gate mboot = (struct mboot *)((void *)boot_sect); 52827c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 52837c478bd9Sstevel@tonic-gate struct ipart *part = 52847c478bd9Sstevel@tonic-gate (struct ipart *)(uintptr_t)mboot->parts + i; 52857c478bd9Sstevel@tonic-gate if (is_pcfs) { /* looking for solaris boot part */ 52867c478bd9Sstevel@tonic-gate if (part->systid == 0xbe) { 52877c478bd9Sstevel@tonic-gate partno = i; 52887c478bd9Sstevel@tonic-gate break; 52897c478bd9Sstevel@tonic-gate } 52907c478bd9Sstevel@tonic-gate } else { /* look for solaris partition, old and new */ 52911a902ef8SHans Rosenfeld if (part->systid == EFI_PMBR) { 52921a902ef8SHans Rosenfeld partno = PARTNO_EFI; 52931a902ef8SHans Rosenfeld break; 52941a902ef8SHans Rosenfeld } 52951a902ef8SHans Rosenfeld 5296e998e519SSheshadri Vasudevan #ifdef i386 5297e998e519SSheshadri Vasudevan if ((part->systid == SUNIXOS && 5298e998e519SSheshadri Vasudevan (fdisk_is_linux_swap(epp, part->relsect, 5299e998e519SSheshadri Vasudevan NULL) != 0)) || part->systid == SUNIXOS2) { 5300e998e519SSheshadri Vasudevan #else 53017c478bd9Sstevel@tonic-gate if (part->systid == SUNIXOS || 53027c478bd9Sstevel@tonic-gate part->systid == SUNIXOS2) { 5303e998e519SSheshadri Vasudevan #endif 53047c478bd9Sstevel@tonic-gate partno = i; 53057c478bd9Sstevel@tonic-gate break; 53067c478bd9Sstevel@tonic-gate } 530728074f82SSheshadri Vasudevan 530828074f82SSheshadri Vasudevan #ifdef i386 530928074f82SSheshadri Vasudevan if (fdisk_is_dos_extended(part->systid)) 531028074f82SSheshadri Vasudevan ext_partno = i; 531128074f82SSheshadri Vasudevan #endif 53127c478bd9Sstevel@tonic-gate } 53137c478bd9Sstevel@tonic-gate } 5314e998e519SSheshadri Vasudevan #ifdef i386 531528074f82SSheshadri Vasudevan /* If no primary solaris partition, check extended partition */ 53161a902ef8SHans Rosenfeld if ((partno == PARTNO_NOTFOUND) && (ext_partno != PARTNO_NOTFOUND)) { 531728074f82SSheshadri Vasudevan rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec); 531828074f82SSheshadri Vasudevan if (rval == FDISK_SUCCESS) { 531928074f82SSheshadri Vasudevan partno = pno - 1; 532028074f82SSheshadri Vasudevan } 532128074f82SSheshadri Vasudevan } 5322e998e519SSheshadri Vasudevan libfdisk_fini(&epp); 5323e998e519SSheshadri Vasudevan #endif 53247c478bd9Sstevel@tonic-gate return (partno); 53257c478bd9Sstevel@tonic-gate } 53267c478bd9Sstevel@tonic-gate 5327eb2bd662Svikram char * 5328eb2bd662Svikram get_grubroot(char *osroot, char *osdev, char *menu_root) 53297c478bd9Sstevel@tonic-gate { 5330eb2bd662Svikram char *grubroot; /* (hd#,#,#) */ 53317c478bd9Sstevel@tonic-gate char *slice; 5332110570ceSToomas Soome char *grubhd = NULL; 53337c478bd9Sstevel@tonic-gate int fdiskpart; 53347c478bd9Sstevel@tonic-gate int found = 0; 5335eb2bd662Svikram char *devname; 5336eb2bd662Svikram char *ctdname = strstr(osdev, "dsk/"); 53377c478bd9Sstevel@tonic-gate char linebuf[PATH_MAX]; 5338eb2bd662Svikram FILE *fp; 53397c478bd9Sstevel@tonic-gate 5340eb2bd662Svikram INJECT_ERROR1("GRUBROOT_INVALID_OSDEV", ctdname = NULL); 5341eb2bd662Svikram if (ctdname == NULL) { 5342eb2bd662Svikram bam_error(INVALID_DEV_DSK, osdev); 53437c478bd9Sstevel@tonic-gate return (NULL); 5344eb2bd662Svikram } 5345eb2bd662Svikram 5346eb2bd662Svikram if (menu_root && !menu_on_bootdisk(osroot, menu_root)) { 5347eb2bd662Svikram /* menu bears no resemblance to our reality */ 534837eb779cSVikram Hegde bam_error(CANNOT_GRUBROOT_BOOTDISK, osdev); 5349eb2bd662Svikram return (NULL); 5350eb2bd662Svikram } 53517c478bd9Sstevel@tonic-gate 53527c478bd9Sstevel@tonic-gate ctdname += strlen("dsk/"); 53537c478bd9Sstevel@tonic-gate slice = strrchr(ctdname, 's'); 53547c478bd9Sstevel@tonic-gate if (slice) 53557c478bd9Sstevel@tonic-gate *slice = '\0'; 53567c478bd9Sstevel@tonic-gate 5357eb2bd662Svikram fp = create_diskmap(osroot); 5358eb2bd662Svikram if (fp == NULL) { 5359eb2bd662Svikram bam_error(DISKMAP_FAIL, osroot); 5360eb2bd662Svikram return (NULL); 5361eb2bd662Svikram } 5362eb2bd662Svikram 53637c478bd9Sstevel@tonic-gate rewind(fp); 53647c478bd9Sstevel@tonic-gate while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) { 53657c478bd9Sstevel@tonic-gate grubhd = strtok(linebuf, " \t\n"); 53667c478bd9Sstevel@tonic-gate if (grubhd) 53677c478bd9Sstevel@tonic-gate devname = strtok(NULL, " \t\n"); 53687c478bd9Sstevel@tonic-gate else 53697c478bd9Sstevel@tonic-gate devname = NULL; 53707c478bd9Sstevel@tonic-gate if (devname && strcmp(devname, ctdname) == 0) { 53717c478bd9Sstevel@tonic-gate found = 1; 53727c478bd9Sstevel@tonic-gate break; 53737c478bd9Sstevel@tonic-gate } 53747c478bd9Sstevel@tonic-gate } 53757c478bd9Sstevel@tonic-gate 53767c478bd9Sstevel@tonic-gate if (slice) 53777c478bd9Sstevel@tonic-gate *slice = 's'; 53787c478bd9Sstevel@tonic-gate 5379eb2bd662Svikram (void) fclose(fp); 5380eb2bd662Svikram fp = NULL; 5381eb2bd662Svikram 5382eb2bd662Svikram INJECT_ERROR1("GRUBROOT_BIOSDEV_FAIL", found = 0); 53837c478bd9Sstevel@tonic-gate if (found == 0) { 538437eb779cSVikram Hegde bam_error(BIOSDEV_SKIP, osdev); 5385eb2bd662Svikram return (NULL); 53867c478bd9Sstevel@tonic-gate } 53877c478bd9Sstevel@tonic-gate 5388eb2bd662Svikram fdiskpart = get_partition(osdev); 53891a902ef8SHans Rosenfeld INJECT_ERROR1("GRUBROOT_FDISK_FAIL", fdiskpart = PARTNO_NOTFOUND); 53901a902ef8SHans Rosenfeld if (fdiskpart == PARTNO_NOTFOUND) { 5391eb2bd662Svikram bam_error(FDISKPART_FAIL, osdev); 53927c478bd9Sstevel@tonic-gate return (NULL); 5393eb2bd662Svikram } 53947c478bd9Sstevel@tonic-gate 5395eb2bd662Svikram grubroot = s_calloc(1, 10); 53961a902ef8SHans Rosenfeld if (fdiskpart == PARTNO_EFI) { 53971a902ef8SHans Rosenfeld fdiskpart = atoi(&slice[1]); 53981a902ef8SHans Rosenfeld slice = NULL; 53991a902ef8SHans Rosenfeld } 54001a902ef8SHans Rosenfeld 54017c478bd9Sstevel@tonic-gate if (slice) { 5402eb2bd662Svikram (void) snprintf(grubroot, 10, "(hd%s,%d,%c)", 54037c478bd9Sstevel@tonic-gate grubhd, fdiskpart, slice[1] + 'a' - '0'); 54047c478bd9Sstevel@tonic-gate } else 5405eb2bd662Svikram (void) snprintf(grubroot, 10, "(hd%s,%d)", 54067c478bd9Sstevel@tonic-gate grubhd, fdiskpart); 54077c478bd9Sstevel@tonic-gate 5408eb2bd662Svikram assert(fp == NULL); 5409eb2bd662Svikram assert(strncmp(grubroot, "(hd", strlen("(hd")) == 0); 5410eb2bd662Svikram return (grubroot); 5411eb2bd662Svikram } 5412eb2bd662Svikram 5413eb2bd662Svikram static char * 5414eb2bd662Svikram find_primary_common(char *mntpt, char *fstype) 5415eb2bd662Svikram { 5416eb2bd662Svikram char signdir[PATH_MAX]; 5417eb2bd662Svikram char tmpsign[MAXNAMELEN + 1]; 5418eb2bd662Svikram char *lu; 5419eb2bd662Svikram char *ufs; 5420eb2bd662Svikram char *zfs; 5421eb2bd662Svikram DIR *dirp = NULL; 5422eb2bd662Svikram struct dirent *entp; 5423eb2bd662Svikram struct stat sb; 5424eb2bd662Svikram const char *fcn = "find_primary_common()"; 5425eb2bd662Svikram 5426eb2bd662Svikram (void) snprintf(signdir, sizeof (signdir), "%s/%s", 5427eb2bd662Svikram mntpt, GRUBSIGN_DIR); 5428eb2bd662Svikram 5429eb2bd662Svikram if (stat(signdir, &sb) == -1) { 5430eb2bd662Svikram BAM_DPRINTF((D_NO_SIGNDIR, fcn, signdir)); 5431eb2bd662Svikram return (NULL); 5432eb2bd662Svikram } 5433eb2bd662Svikram 5434eb2bd662Svikram dirp = opendir(signdir); 5435eb2bd662Svikram INJECT_ERROR1("SIGNDIR_OPENDIR_FAIL", dirp = NULL); 5436eb2bd662Svikram if (dirp == NULL) { 5437eb2bd662Svikram bam_error(OPENDIR_FAILED, signdir, strerror(errno)); 5438eb2bd662Svikram return (NULL); 5439eb2bd662Svikram } 5440eb2bd662Svikram 5441eb2bd662Svikram ufs = zfs = lu = NULL; 5442eb2bd662Svikram 5443110570ceSToomas Soome while ((entp = readdir(dirp)) != NULL) { 5444eb2bd662Svikram if (strcmp(entp->d_name, ".") == 0 || 5445eb2bd662Svikram strcmp(entp->d_name, "..") == 0) 5446eb2bd662Svikram continue; 5447eb2bd662Svikram 5448eb2bd662Svikram (void) snprintf(tmpsign, sizeof (tmpsign), "%s", entp->d_name); 5449eb2bd662Svikram 5450eb2bd662Svikram if (lu == NULL && 5451eb2bd662Svikram strncmp(tmpsign, GRUBSIGN_LU_PREFIX, 5452eb2bd662Svikram strlen(GRUBSIGN_LU_PREFIX)) == 0) { 5453eb2bd662Svikram lu = s_strdup(tmpsign); 5454eb2bd662Svikram } 5455eb2bd662Svikram 5456eb2bd662Svikram if (ufs == NULL && 5457eb2bd662Svikram strncmp(tmpsign, GRUBSIGN_UFS_PREFIX, 5458eb2bd662Svikram strlen(GRUBSIGN_UFS_PREFIX)) == 0) { 5459eb2bd662Svikram ufs = s_strdup(tmpsign); 5460eb2bd662Svikram } 5461eb2bd662Svikram 5462eb2bd662Svikram if (zfs == NULL && 5463eb2bd662Svikram strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX, 5464eb2bd662Svikram strlen(GRUBSIGN_ZFS_PREFIX)) == 0) { 5465eb2bd662Svikram zfs = s_strdup(tmpsign); 5466eb2bd662Svikram } 5467eb2bd662Svikram } 5468eb2bd662Svikram 5469eb2bd662Svikram BAM_DPRINTF((D_EXIST_PRIMARY_SIGNS, fcn, 5470eb2bd662Svikram zfs ? zfs : "NULL", 5471eb2bd662Svikram ufs ? ufs : "NULL", 5472eb2bd662Svikram lu ? lu : "NULL")); 5473eb2bd662Svikram 5474eb2bd662Svikram if (dirp) { 5475eb2bd662Svikram (void) closedir(dirp); 5476eb2bd662Svikram dirp = NULL; 5477eb2bd662Svikram } 5478eb2bd662Svikram 5479eb2bd662Svikram if (strcmp(fstype, "ufs") == 0 && zfs) { 5480eb2bd662Svikram bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs"); 5481eb2bd662Svikram free(zfs); 5482eb2bd662Svikram zfs = NULL; 5483eb2bd662Svikram } else if (strcmp(fstype, "zfs") == 0 && ufs) { 5484eb2bd662Svikram bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs"); 5485eb2bd662Svikram free(ufs); 5486eb2bd662Svikram ufs = NULL; 5487eb2bd662Svikram } 5488eb2bd662Svikram 5489eb2bd662Svikram assert(dirp == NULL); 5490eb2bd662Svikram 5491eb2bd662Svikram /* For now, we let Live Upgrade take care of its signature itself */ 5492eb2bd662Svikram if (lu) { 5493eb2bd662Svikram BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu)); 5494eb2bd662Svikram free(lu); 5495eb2bd662Svikram lu = NULL; 5496eb2bd662Svikram } 5497eb2bd662Svikram 5498eb2bd662Svikram return (zfs ? zfs : ufs); 5499eb2bd662Svikram } 5500eb2bd662Svikram 5501eb2bd662Svikram static char * 5502eb2bd662Svikram find_backup_common(char *mntpt, char *fstype) 5503eb2bd662Svikram { 5504eb2bd662Svikram FILE *bfp = NULL; 5505eb2bd662Svikram char tmpsign[MAXNAMELEN + 1]; 5506eb2bd662Svikram char backup[PATH_MAX]; 5507eb2bd662Svikram char *ufs; 5508eb2bd662Svikram char *zfs; 5509eb2bd662Svikram char *lu; 5510eb2bd662Svikram int error; 5511eb2bd662Svikram const char *fcn = "find_backup_common()"; 5512eb2bd662Svikram 5513eb2bd662Svikram /* 5514eb2bd662Svikram * We didn't find it in the primary directory. 5515eb2bd662Svikram * Look at the backup 5516eb2bd662Svikram */ 5517eb2bd662Svikram (void) snprintf(backup, sizeof (backup), "%s%s", 5518eb2bd662Svikram mntpt, GRUBSIGN_BACKUP); 5519eb2bd662Svikram 5520eb2bd662Svikram bfp = fopen(backup, "r"); 5521eb2bd662Svikram if (bfp == NULL) { 5522eb2bd662Svikram error = errno; 5523eb2bd662Svikram if (bam_verbose) { 5524eb2bd662Svikram bam_error(OPEN_FAIL, backup, strerror(error)); 5525eb2bd662Svikram } 5526eb2bd662Svikram BAM_DPRINTF((D_OPEN_FAIL, fcn, backup, strerror(error))); 5527eb2bd662Svikram return (NULL); 5528eb2bd662Svikram } 5529eb2bd662Svikram 5530eb2bd662Svikram ufs = zfs = lu = NULL; 5531eb2bd662Svikram 5532eb2bd662Svikram while (s_fgets(tmpsign, sizeof (tmpsign), bfp) != NULL) { 5533eb2bd662Svikram 5534eb2bd662Svikram if (lu == NULL && 5535eb2bd662Svikram strncmp(tmpsign, GRUBSIGN_LU_PREFIX, 5536eb2bd662Svikram strlen(GRUBSIGN_LU_PREFIX)) == 0) { 5537eb2bd662Svikram lu = s_strdup(tmpsign); 5538eb2bd662Svikram } 5539eb2bd662Svikram 5540eb2bd662Svikram if (ufs == NULL && 5541eb2bd662Svikram strncmp(tmpsign, GRUBSIGN_UFS_PREFIX, 5542eb2bd662Svikram strlen(GRUBSIGN_UFS_PREFIX)) == 0) { 5543eb2bd662Svikram ufs = s_strdup(tmpsign); 5544eb2bd662Svikram } 5545eb2bd662Svikram 5546eb2bd662Svikram if (zfs == NULL && 5547eb2bd662Svikram strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX, 5548eb2bd662Svikram strlen(GRUBSIGN_ZFS_PREFIX)) == 0) { 5549eb2bd662Svikram zfs = s_strdup(tmpsign); 5550eb2bd662Svikram } 5551eb2bd662Svikram } 5552eb2bd662Svikram 5553eb2bd662Svikram BAM_DPRINTF((D_EXIST_BACKUP_SIGNS, fcn, 5554eb2bd662Svikram zfs ? zfs : "NULL", 5555eb2bd662Svikram ufs ? ufs : "NULL", 5556eb2bd662Svikram lu ? lu : "NULL")); 5557eb2bd662Svikram 5558eb2bd662Svikram if (bfp) { 5559eb2bd662Svikram (void) fclose(bfp); 5560eb2bd662Svikram bfp = NULL; 5561eb2bd662Svikram } 5562eb2bd662Svikram 5563eb2bd662Svikram if (strcmp(fstype, "ufs") == 0 && zfs) { 5564eb2bd662Svikram bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs"); 5565eb2bd662Svikram free(zfs); 5566eb2bd662Svikram zfs = NULL; 5567eb2bd662Svikram } else if (strcmp(fstype, "zfs") == 0 && ufs) { 5568eb2bd662Svikram bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs"); 5569eb2bd662Svikram free(ufs); 5570eb2bd662Svikram ufs = NULL; 5571eb2bd662Svikram } 5572eb2bd662Svikram 5573eb2bd662Svikram assert(bfp == NULL); 5574eb2bd662Svikram 5575eb2bd662Svikram /* For now, we let Live Upgrade take care of its signature itself */ 5576eb2bd662Svikram if (lu) { 5577eb2bd662Svikram BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu)); 5578eb2bd662Svikram free(lu); 5579eb2bd662Svikram lu = NULL; 5580eb2bd662Svikram } 5581eb2bd662Svikram 5582eb2bd662Svikram return (zfs ? zfs : ufs); 5583eb2bd662Svikram } 5584eb2bd662Svikram 5585eb2bd662Svikram static char * 5586eb2bd662Svikram find_ufs_existing(char *osroot) 5587eb2bd662Svikram { 5588eb2bd662Svikram char *sign; 5589eb2bd662Svikram const char *fcn = "find_ufs_existing()"; 5590eb2bd662Svikram 5591eb2bd662Svikram sign = find_primary_common(osroot, "ufs"); 5592eb2bd662Svikram if (sign == NULL) { 5593eb2bd662Svikram sign = find_backup_common(osroot, "ufs"); 5594eb2bd662Svikram BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL")); 5595eb2bd662Svikram } else { 5596eb2bd662Svikram BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign)); 5597eb2bd662Svikram } 5598eb2bd662Svikram 5599eb2bd662Svikram return (sign); 5600eb2bd662Svikram } 5601eb2bd662Svikram 5602eb2bd662Svikram char * 5603eb2bd662Svikram get_mountpoint(char *special, char *fstype) 5604eb2bd662Svikram { 5605eb2bd662Svikram FILE *mntfp; 5606eb2bd662Svikram struct mnttab mp = {0}; 5607eb2bd662Svikram struct mnttab mpref = {0}; 5608eb2bd662Svikram int error; 5609eb2bd662Svikram int ret; 5610eb2bd662Svikram const char *fcn = "get_mountpoint()"; 5611eb2bd662Svikram 5612eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, fcn, special, fstype)); 5613eb2bd662Svikram 5614eb2bd662Svikram mntfp = fopen(MNTTAB, "r"); 5615eb2bd662Svikram error = errno; 5616eb2bd662Svikram INJECT_ERROR1("MNTTAB_ERR_GET_MNTPT", mntfp = NULL); 5617eb2bd662Svikram if (mntfp == NULL) { 5618eb2bd662Svikram bam_error(OPEN_FAIL, MNTTAB, strerror(error)); 5619eb2bd662Svikram return (NULL); 5620eb2bd662Svikram } 5621eb2bd662Svikram 5622eb2bd662Svikram mpref.mnt_special = special; 5623eb2bd662Svikram mpref.mnt_fstype = fstype; 5624eb2bd662Svikram 5625eb2bd662Svikram ret = getmntany(mntfp, &mp, &mpref); 5626eb2bd662Svikram INJECT_ERROR1("GET_MOUNTPOINT_MNTANY", ret = 1); 5627eb2bd662Svikram if (ret != 0) { 5628eb2bd662Svikram (void) fclose(mntfp); 5629eb2bd662Svikram BAM_DPRINTF((D_NO_MNTPT, fcn, special, fstype)); 5630eb2bd662Svikram return (NULL); 5631eb2bd662Svikram } 5632eb2bd662Svikram (void) fclose(mntfp); 5633eb2bd662Svikram 5634eb2bd662Svikram assert(mp.mnt_mountp); 5635eb2bd662Svikram 5636eb2bd662Svikram BAM_DPRINTF((D_GET_MOUNTPOINT_RET, fcn, special, mp.mnt_mountp)); 5637eb2bd662Svikram 5638eb2bd662Svikram return (s_strdup(mp.mnt_mountp)); 5639eb2bd662Svikram } 5640eb2bd662Svikram 5641eb2bd662Svikram /* 5642eb2bd662Svikram * Mounts a "legacy" top dataset (if needed) 5643eb2bd662Svikram * Returns: The mountpoint of the legacy top dataset or NULL on error 5644eb2bd662Svikram * mnted returns one of the above values defined for zfs_mnted_t 5645eb2bd662Svikram */ 5646eb2bd662Svikram static char * 5647eb2bd662Svikram mount_legacy_dataset(char *pool, zfs_mnted_t *mnted) 5648eb2bd662Svikram { 5649eb2bd662Svikram char cmd[PATH_MAX]; 5650eb2bd662Svikram char tmpmnt[PATH_MAX]; 5651eb2bd662Svikram filelist_t flist = {0}; 5652eb2bd662Svikram char *is_mounted; 5653eb2bd662Svikram struct stat sb; 5654eb2bd662Svikram int ret; 5655eb2bd662Svikram const char *fcn = "mount_legacy_dataset()"; 5656eb2bd662Svikram 5657eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool)); 5658eb2bd662Svikram 5659eb2bd662Svikram *mnted = ZFS_MNT_ERROR; 5660eb2bd662Svikram 5661eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 5662eb2bd662Svikram "/sbin/zfs get -Ho value mounted %s", 5663eb2bd662Svikram pool); 5664eb2bd662Svikram 5665eb2bd662Svikram ret = exec_cmd(cmd, &flist); 5666eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_CMD", ret = 1); 5667eb2bd662Svikram if (ret != 0) { 5668eb2bd662Svikram bam_error(ZFS_MNTED_FAILED, pool); 5669eb2bd662Svikram return (NULL); 5670eb2bd662Svikram } 5671eb2bd662Svikram 5672eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_OUT", flist.head = NULL); 5673eb2bd662Svikram if ((flist.head == NULL) || (flist.head != flist.tail)) { 5674eb2bd662Svikram bam_error(BAD_ZFS_MNTED, pool); 5675eb2bd662Svikram filelist_free(&flist); 5676eb2bd662Svikram return (NULL); 5677eb2bd662Svikram } 5678eb2bd662Svikram 5679eb2bd662Svikram is_mounted = strtok(flist.head->line, " \t\n"); 5680eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_YES", is_mounted = "yes"); 5681eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_NO", is_mounted = "no"); 5682eb2bd662Svikram if (strcmp(is_mounted, "no") != 0) { 5683eb2bd662Svikram filelist_free(&flist); 5684eb2bd662Svikram *mnted = LEGACY_ALREADY; 5685eb2bd662Svikram /* get_mountpoint returns a strdup'ed string */ 5686eb2bd662Svikram BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_ALREADY, fcn, pool)); 5687eb2bd662Svikram return (get_mountpoint(pool, "zfs")); 5688eb2bd662Svikram } 5689eb2bd662Svikram 5690eb2bd662Svikram filelist_free(&flist); 5691eb2bd662Svikram 5692eb2bd662Svikram /* 5693eb2bd662Svikram * legacy top dataset is not mounted. Mount it now 5694eb2bd662Svikram * First create a mountpoint. 5695eb2bd662Svikram */ 5696eb2bd662Svikram (void) snprintf(tmpmnt, sizeof (tmpmnt), "%s.%d", 5697eb2bd662Svikram ZFS_LEGACY_MNTPT, getpid()); 5698eb2bd662Svikram 5699eb2bd662Svikram ret = stat(tmpmnt, &sb); 5700eb2bd662Svikram if (ret == -1) { 5701eb2bd662Svikram BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_ABS, fcn, pool, tmpmnt)); 570248847494SEnrico Perla - Sun Microsystems ret = mkdirp(tmpmnt, DIR_PERMS); 5703eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_TOP_LEG_MNTPT_MKDIRP", ret = -1); 5704eb2bd662Svikram if (ret == -1) { 5705eb2bd662Svikram bam_error(MKDIR_FAILED, tmpmnt, strerror(errno)); 5706eb2bd662Svikram return (NULL); 5707eb2bd662Svikram } 5708eb2bd662Svikram } else { 5709eb2bd662Svikram BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_PRES, fcn, pool, tmpmnt)); 5710eb2bd662Svikram } 5711eb2bd662Svikram 5712eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 5713eb2bd662Svikram "/sbin/mount -F zfs %s %s", 5714eb2bd662Svikram pool, tmpmnt); 5715eb2bd662Svikram 5716eb2bd662Svikram ret = exec_cmd(cmd, NULL); 5717eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_TOP_LEG_MOUNT_CMD", ret = 1); 5718eb2bd662Svikram if (ret != 0) { 5719eb2bd662Svikram bam_error(ZFS_MOUNT_FAILED, pool); 5720eb2bd662Svikram (void) rmdir(tmpmnt); 5721eb2bd662Svikram return (NULL); 5722eb2bd662Svikram } 5723eb2bd662Svikram 5724eb2bd662Svikram *mnted = LEGACY_MOUNTED; 5725eb2bd662Svikram BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MOUNTED, fcn, pool, tmpmnt)); 5726eb2bd662Svikram return (s_strdup(tmpmnt)); 5727eb2bd662Svikram } 5728eb2bd662Svikram 5729eb2bd662Svikram /* 5730eb2bd662Svikram * Mounts the top dataset (if needed) 5731eb2bd662Svikram * Returns: The mountpoint of the top dataset or NULL on error 5732eb2bd662Svikram * mnted returns one of the above values defined for zfs_mnted_t 5733eb2bd662Svikram */ 5734eb2bd662Svikram static char * 5735eb2bd662Svikram mount_top_dataset(char *pool, zfs_mnted_t *mnted) 5736eb2bd662Svikram { 5737eb2bd662Svikram char cmd[PATH_MAX]; 5738eb2bd662Svikram filelist_t flist = {0}; 5739eb2bd662Svikram char *is_mounted; 5740eb2bd662Svikram char *mntpt; 5741eb2bd662Svikram char *zmntpt; 5742eb2bd662Svikram int ret; 5743eb2bd662Svikram const char *fcn = "mount_top_dataset()"; 5744eb2bd662Svikram 5745eb2bd662Svikram *mnted = ZFS_MNT_ERROR; 5746eb2bd662Svikram 5747eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool)); 5748eb2bd662Svikram 5749eb2bd662Svikram /* 5750eb2bd662Svikram * First check if the top dataset is a "legacy" dataset 5751eb2bd662Svikram */ 5752eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 5753eb2bd662Svikram "/sbin/zfs get -Ho value mountpoint %s", 5754eb2bd662Svikram pool); 5755eb2bd662Svikram ret = exec_cmd(cmd, &flist); 5756eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_TOP_GET_MNTPT", ret = 1); 5757eb2bd662Svikram if (ret != 0) { 5758eb2bd662Svikram bam_error(ZFS_MNTPT_FAILED, pool); 5759eb2bd662Svikram return (NULL); 5760eb2bd662Svikram } 5761eb2bd662Svikram 5762eb2bd662Svikram if (flist.head && (flist.head == flist.tail)) { 5763eb2bd662Svikram char *legacy = strtok(flist.head->line, " \t\n"); 5764eb2bd662Svikram if (legacy && strcmp(legacy, "legacy") == 0) { 5765eb2bd662Svikram filelist_free(&flist); 5766eb2bd662Svikram BAM_DPRINTF((D_Z_IS_LEGACY, fcn, pool)); 5767eb2bd662Svikram return (mount_legacy_dataset(pool, mnted)); 5768eb2bd662Svikram } 5769eb2bd662Svikram } 5770eb2bd662Svikram 5771eb2bd662Svikram filelist_free(&flist); 5772eb2bd662Svikram 5773eb2bd662Svikram BAM_DPRINTF((D_Z_IS_NOT_LEGACY, fcn, pool)); 5774eb2bd662Svikram 5775eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 5776eb2bd662Svikram "/sbin/zfs get -Ho value mounted %s", 5777eb2bd662Svikram pool); 5778eb2bd662Svikram 5779eb2bd662Svikram ret = exec_cmd(cmd, &flist); 5780eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED", ret = 1); 5781eb2bd662Svikram if (ret != 0) { 5782eb2bd662Svikram bam_error(ZFS_MNTED_FAILED, pool); 5783eb2bd662Svikram return (NULL); 5784eb2bd662Svikram } 5785eb2bd662Svikram 5786eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_VAL", flist.head = NULL); 5787eb2bd662Svikram if ((flist.head == NULL) || (flist.head != flist.tail)) { 5788eb2bd662Svikram bam_error(BAD_ZFS_MNTED, pool); 5789eb2bd662Svikram filelist_free(&flist); 5790eb2bd662Svikram return (NULL); 5791eb2bd662Svikram } 5792eb2bd662Svikram 5793eb2bd662Svikram is_mounted = strtok(flist.head->line, " \t\n"); 5794eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_YES", is_mounted = "yes"); 5795eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_NO", is_mounted = "no"); 5796eb2bd662Svikram if (strcmp(is_mounted, "no") != 0) { 5797eb2bd662Svikram filelist_free(&flist); 5798eb2bd662Svikram *mnted = ZFS_ALREADY; 5799eb2bd662Svikram BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_ALREADY, fcn, pool)); 5800eb2bd662Svikram goto mounted; 5801eb2bd662Svikram } 5802eb2bd662Svikram 5803eb2bd662Svikram filelist_free(&flist); 5804eb2bd662Svikram BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOT_ALREADY, fcn, pool)); 5805eb2bd662Svikram 5806eb2bd662Svikram /* top dataset is not mounted. Mount it now */ 5807eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 5808eb2bd662Svikram "/sbin/zfs mount %s", pool); 5809eb2bd662Svikram ret = exec_cmd(cmd, NULL); 5810eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_MOUNT_CMD", ret = 1); 5811eb2bd662Svikram if (ret != 0) { 5812eb2bd662Svikram bam_error(ZFS_MOUNT_FAILED, pool); 5813eb2bd662Svikram return (NULL); 5814eb2bd662Svikram } 5815eb2bd662Svikram *mnted = ZFS_MOUNTED; 5816eb2bd662Svikram BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOW, fcn, pool)); 5817eb2bd662Svikram /*FALLTHRU*/ 5818eb2bd662Svikram mounted: 5819eb2bd662Svikram /* 5820eb2bd662Svikram * Now get the mountpoint 5821eb2bd662Svikram */ 5822eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 5823eb2bd662Svikram "/sbin/zfs get -Ho value mountpoint %s", 5824eb2bd662Svikram pool); 5825eb2bd662Svikram 5826eb2bd662Svikram ret = exec_cmd(cmd, &flist); 5827eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_CMD", ret = 1); 5828eb2bd662Svikram if (ret != 0) { 5829eb2bd662Svikram bam_error(ZFS_MNTPT_FAILED, pool); 5830eb2bd662Svikram goto error; 5831eb2bd662Svikram } 5832eb2bd662Svikram 5833eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_OUT", flist.head = NULL); 5834eb2bd662Svikram if ((flist.head == NULL) || (flist.head != flist.tail)) { 5835eb2bd662Svikram bam_error(NULL_ZFS_MNTPT, pool); 5836eb2bd662Svikram goto error; 5837eb2bd662Svikram } 5838eb2bd662Svikram 5839eb2bd662Svikram mntpt = strtok(flist.head->line, " \t\n"); 5840eb2bd662Svikram INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_STRTOK", mntpt = "foo"); 5841eb2bd662Svikram if (*mntpt != '/') { 5842eb2bd662Svikram bam_error(BAD_ZFS_MNTPT, pool, mntpt); 5843eb2bd662Svikram goto error; 5844eb2bd662Svikram } 5845eb2bd662Svikram zmntpt = s_strdup(mntpt); 5846eb2bd662Svikram 5847eb2bd662Svikram filelist_free(&flist); 5848eb2bd662Svikram 5849eb2bd662Svikram BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MNTPT, fcn, pool, zmntpt)); 5850eb2bd662Svikram 5851eb2bd662Svikram return (zmntpt); 5852eb2bd662Svikram 5853eb2bd662Svikram error: 5854eb2bd662Svikram filelist_free(&flist); 5855eb2bd662Svikram (void) umount_top_dataset(pool, *mnted, NULL); 5856eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 5857eb2bd662Svikram return (NULL); 5858eb2bd662Svikram } 5859eb2bd662Svikram 5860eb2bd662Svikram static int 5861eb2bd662Svikram umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt) 5862eb2bd662Svikram { 5863eb2bd662Svikram char cmd[PATH_MAX]; 5864eb2bd662Svikram int ret; 5865eb2bd662Svikram const char *fcn = "umount_top_dataset()"; 5866eb2bd662Svikram 5867eb2bd662Svikram INJECT_ERROR1("Z_UMOUNT_TOP_INVALID_STATE", mnted = ZFS_MNT_ERROR); 5868eb2bd662Svikram switch (mnted) { 5869eb2bd662Svikram case LEGACY_ALREADY: 5870eb2bd662Svikram case ZFS_ALREADY: 5871eb2bd662Svikram /* nothing to do */ 5872eb2bd662Svikram BAM_DPRINTF((D_Z_UMOUNT_TOP_ALREADY_NOP, fcn, pool, 5873eb2bd662Svikram mntpt ? mntpt : "NULL")); 5874eb2bd662Svikram free(mntpt); 5875eb2bd662Svikram return (BAM_SUCCESS); 5876eb2bd662Svikram case LEGACY_MOUNTED: 5877eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 5878eb2bd662Svikram "/sbin/umount %s", pool); 5879eb2bd662Svikram ret = exec_cmd(cmd, NULL); 5880eb2bd662Svikram INJECT_ERROR1("Z_UMOUNT_TOP_LEGACY_UMOUNT_FAIL", ret = 1); 5881eb2bd662Svikram if (ret != 0) { 5882eb2bd662Svikram bam_error(UMOUNT_FAILED, pool); 5883eb2bd662Svikram free(mntpt); 5884eb2bd662Svikram return (BAM_ERROR); 5885eb2bd662Svikram } 5886eb2bd662Svikram if (mntpt) 5887eb2bd662Svikram (void) rmdir(mntpt); 5888eb2bd662Svikram free(mntpt); 5889eb2bd662Svikram BAM_DPRINTF((D_Z_UMOUNT_TOP_LEGACY, fcn, pool)); 5890eb2bd662Svikram return (BAM_SUCCESS); 5891eb2bd662Svikram case ZFS_MOUNTED: 5892eb2bd662Svikram free(mntpt); 5893eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 5894eb2bd662Svikram "/sbin/zfs unmount %s", pool); 5895eb2bd662Svikram ret = exec_cmd(cmd, NULL); 5896eb2bd662Svikram INJECT_ERROR1("Z_UMOUNT_TOP_NONLEG_UMOUNT_FAIL", ret = 1); 5897eb2bd662Svikram if (ret != 0) { 5898eb2bd662Svikram bam_error(UMOUNT_FAILED, pool); 5899eb2bd662Svikram return (BAM_ERROR); 5900eb2bd662Svikram } 5901eb2bd662Svikram BAM_DPRINTF((D_Z_UMOUNT_TOP_NONLEG, fcn, pool)); 5902eb2bd662Svikram return (BAM_SUCCESS); 5903eb2bd662Svikram default: 5904eb2bd662Svikram bam_error(INT_BAD_MNTSTATE, pool); 5905eb2bd662Svikram return (BAM_ERROR); 5906eb2bd662Svikram } 5907eb2bd662Svikram /*NOTREACHED*/ 5908eb2bd662Svikram } 5909eb2bd662Svikram 5910eb2bd662Svikram /* 5911eb2bd662Svikram * For ZFS, osdev can be one of two forms 5912eb2bd662Svikram * It can be a "special" file as seen in mnttab: rpool/ROOT/szboot_0402 5913eb2bd662Svikram * It can be a /dev/[r]dsk special file. We handle both instances 5914eb2bd662Svikram */ 5915eb2bd662Svikram static char * 5916eb2bd662Svikram get_pool(char *osdev) 5917eb2bd662Svikram { 5918eb2bd662Svikram char cmd[PATH_MAX]; 5919eb2bd662Svikram char buf[PATH_MAX]; 5920eb2bd662Svikram filelist_t flist = {0}; 5921eb2bd662Svikram char *pool; 5922eb2bd662Svikram char *cp; 5923eb2bd662Svikram char *slash; 5924eb2bd662Svikram int ret; 5925eb2bd662Svikram const char *fcn = "get_pool()"; 5926eb2bd662Svikram 5927eb2bd662Svikram INJECT_ERROR1("GET_POOL_OSDEV", osdev = NULL); 5928eb2bd662Svikram if (osdev == NULL) { 5929eb2bd662Svikram bam_error(GET_POOL_OSDEV_NULL); 5930eb2bd662Svikram return (NULL); 5931eb2bd662Svikram } 5932eb2bd662Svikram 5933eb2bd662Svikram BAM_DPRINTF((D_GET_POOL_OSDEV, fcn, osdev)); 5934eb2bd662Svikram 5935eb2bd662Svikram if (osdev[0] != '/') { 5936eb2bd662Svikram (void) strlcpy(buf, osdev, sizeof (buf)); 5937eb2bd662Svikram slash = strchr(buf, '/'); 5938eb2bd662Svikram if (slash) 5939eb2bd662Svikram *slash = '\0'; 5940eb2bd662Svikram pool = s_strdup(buf); 5941eb2bd662Svikram BAM_DPRINTF((D_GET_POOL_RET, fcn, pool)); 5942eb2bd662Svikram return (pool); 5943eb2bd662Svikram } else if (strncmp(osdev, "/dev/dsk/", strlen("/dev/dsk/")) != 0 && 5944eb2bd662Svikram strncmp(osdev, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0) { 5945eb2bd662Svikram bam_error(GET_POOL_BAD_OSDEV, osdev); 5946eb2bd662Svikram return (NULL); 5947eb2bd662Svikram } 5948eb2bd662Svikram 5949d5cb36d3SJan Setje-Eilers /* 5950d5cb36d3SJan Setje-Eilers * Call the zfs fstyp directly since this is a zpool. This avoids 5951d5cb36d3SJan Setje-Eilers * potential pcfs conflicts if the first block wasn't cleared. 5952d5cb36d3SJan Setje-Eilers */ 5953eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 5954d5cb36d3SJan Setje-Eilers "/usr/lib/fs/zfs/fstyp -a %s 2>/dev/null | /bin/grep '^name:'", 5955eb2bd662Svikram osdev); 5956eb2bd662Svikram 5957eb2bd662Svikram ret = exec_cmd(cmd, &flist); 5958eb2bd662Svikram INJECT_ERROR1("GET_POOL_FSTYP", ret = 1); 5959eb2bd662Svikram if (ret != 0) { 5960eb2bd662Svikram bam_error(FSTYP_A_FAILED, osdev); 5961eb2bd662Svikram return (NULL); 5962eb2bd662Svikram } 5963eb2bd662Svikram 5964eb2bd662Svikram INJECT_ERROR1("GET_POOL_FSTYP_OUT", flist.head = NULL); 5965eb2bd662Svikram if ((flist.head == NULL) || (flist.head != flist.tail)) { 5966eb2bd662Svikram bam_error(NULL_FSTYP_A, osdev); 5967eb2bd662Svikram filelist_free(&flist); 5968eb2bd662Svikram return (NULL); 5969eb2bd662Svikram } 5970eb2bd662Svikram 5971eb2bd662Svikram (void) strtok(flist.head->line, "'"); 5972eb2bd662Svikram cp = strtok(NULL, "'"); 5973eb2bd662Svikram INJECT_ERROR1("GET_POOL_FSTYP_STRTOK", cp = NULL); 5974eb2bd662Svikram if (cp == NULL) { 5975eb2bd662Svikram bam_error(BAD_FSTYP_A, osdev); 5976eb2bd662Svikram filelist_free(&flist); 5977eb2bd662Svikram return (NULL); 5978eb2bd662Svikram } 5979eb2bd662Svikram 5980eb2bd662Svikram pool = s_strdup(cp); 5981eb2bd662Svikram 5982eb2bd662Svikram filelist_free(&flist); 5983eb2bd662Svikram 5984eb2bd662Svikram BAM_DPRINTF((D_GET_POOL_RET, fcn, pool)); 5985eb2bd662Svikram 5986eb2bd662Svikram return (pool); 5987eb2bd662Svikram } 5988eb2bd662Svikram 5989eb2bd662Svikram static char * 5990eb2bd662Svikram find_zfs_existing(char *osdev) 5991eb2bd662Svikram { 5992eb2bd662Svikram char *pool; 5993eb2bd662Svikram zfs_mnted_t mnted; 5994eb2bd662Svikram char *mntpt; 5995eb2bd662Svikram char *sign; 5996eb2bd662Svikram const char *fcn = "find_zfs_existing()"; 5997eb2bd662Svikram 5998eb2bd662Svikram pool = get_pool(osdev); 5999eb2bd662Svikram INJECT_ERROR1("ZFS_FIND_EXIST_POOL", pool = NULL); 6000eb2bd662Svikram if (pool == NULL) { 6001eb2bd662Svikram bam_error(ZFS_GET_POOL_FAILED, osdev); 6002eb2bd662Svikram return (NULL); 6003eb2bd662Svikram } 6004eb2bd662Svikram 6005eb2bd662Svikram mntpt = mount_top_dataset(pool, &mnted); 6006eb2bd662Svikram INJECT_ERROR1("ZFS_FIND_EXIST_MOUNT_TOP", mntpt = NULL); 6007eb2bd662Svikram if (mntpt == NULL) { 6008eb2bd662Svikram bam_error(ZFS_MOUNT_TOP_DATASET_FAILED, pool); 6009eb2bd662Svikram free(pool); 6010eb2bd662Svikram return (NULL); 6011eb2bd662Svikram } 6012eb2bd662Svikram 6013eb2bd662Svikram sign = find_primary_common(mntpt, "zfs"); 6014eb2bd662Svikram if (sign == NULL) { 6015eb2bd662Svikram sign = find_backup_common(mntpt, "zfs"); 6016eb2bd662Svikram BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL")); 6017eb2bd662Svikram } else { 6018eb2bd662Svikram BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign)); 6019eb2bd662Svikram } 6020eb2bd662Svikram 6021eb2bd662Svikram (void) umount_top_dataset(pool, mnted, mntpt); 6022eb2bd662Svikram 6023eb2bd662Svikram free(pool); 6024eb2bd662Svikram 6025eb2bd662Svikram return (sign); 6026eb2bd662Svikram } 6027eb2bd662Svikram 6028eb2bd662Svikram static char * 6029eb2bd662Svikram find_existing_sign(char *osroot, char *osdev, char *fstype) 6030eb2bd662Svikram { 6031eb2bd662Svikram const char *fcn = "find_existing_sign()"; 6032eb2bd662Svikram 6033eb2bd662Svikram INJECT_ERROR1("FIND_EXIST_NOTSUP_FS", fstype = "foofs"); 6034eb2bd662Svikram if (strcmp(fstype, "ufs") == 0) { 6035eb2bd662Svikram BAM_DPRINTF((D_CHECK_UFS_EXIST_SIGN, fcn)); 6036eb2bd662Svikram return (find_ufs_existing(osroot)); 6037eb2bd662Svikram } else if (strcmp(fstype, "zfs") == 0) { 6038eb2bd662Svikram BAM_DPRINTF((D_CHECK_ZFS_EXIST_SIGN, fcn)); 6039eb2bd662Svikram return (find_zfs_existing(osdev)); 6040eb2bd662Svikram } else { 6041eb2bd662Svikram bam_error(GRUBSIGN_NOTSUP, fstype); 6042eb2bd662Svikram return (NULL); 6043eb2bd662Svikram } 6044eb2bd662Svikram } 6045eb2bd662Svikram 6046eb2bd662Svikram #define MH_HASH_SZ 16 6047eb2bd662Svikram 6048eb2bd662Svikram typedef enum { 6049eb2bd662Svikram MH_ERROR = -1, 6050eb2bd662Svikram MH_NOMATCH, 6051eb2bd662Svikram MH_MATCH 6052eb2bd662Svikram } mh_search_t; 6053eb2bd662Svikram 6054eb2bd662Svikram typedef struct mcache { 6055eb2bd662Svikram char *mc_special; 6056eb2bd662Svikram char *mc_mntpt; 6057eb2bd662Svikram char *mc_fstype; 6058eb2bd662Svikram struct mcache *mc_next; 6059eb2bd662Svikram } mcache_t; 6060eb2bd662Svikram 6061eb2bd662Svikram typedef struct mhash { 6062eb2bd662Svikram mcache_t *mh_hash[MH_HASH_SZ]; 6063eb2bd662Svikram } mhash_t; 6064eb2bd662Svikram 6065eb2bd662Svikram static int 6066eb2bd662Svikram mhash_fcn(char *key) 6067eb2bd662Svikram { 6068eb2bd662Svikram int i; 6069eb2bd662Svikram uint64_t sum = 0; 6070eb2bd662Svikram 6071eb2bd662Svikram for (i = 0; key[i] != '\0'; i++) { 6072eb2bd662Svikram sum += (uchar_t)key[i]; 6073eb2bd662Svikram } 6074eb2bd662Svikram 6075eb2bd662Svikram sum %= MH_HASH_SZ; 6076eb2bd662Svikram 6077eb2bd662Svikram assert(sum < MH_HASH_SZ); 6078eb2bd662Svikram 6079eb2bd662Svikram return (sum); 6080eb2bd662Svikram } 6081eb2bd662Svikram 6082eb2bd662Svikram static mhash_t * 6083eb2bd662Svikram cache_mnttab(void) 6084eb2bd662Svikram { 6085eb2bd662Svikram FILE *mfp; 6086eb2bd662Svikram struct extmnttab mnt; 6087eb2bd662Svikram mcache_t *mcp; 6088eb2bd662Svikram mhash_t *mhp; 6089eb2bd662Svikram char *ctds; 6090eb2bd662Svikram int idx; 6091eb2bd662Svikram int error; 6092eb2bd662Svikram char *special_dup; 6093eb2bd662Svikram const char *fcn = "cache_mnttab()"; 6094eb2bd662Svikram 6095eb2bd662Svikram mfp = fopen(MNTTAB, "r"); 6096eb2bd662Svikram error = errno; 6097eb2bd662Svikram INJECT_ERROR1("CACHE_MNTTAB_MNTTAB_ERR", mfp = NULL); 6098eb2bd662Svikram if (mfp == NULL) { 6099eb2bd662Svikram bam_error(OPEN_FAIL, MNTTAB, strerror(error)); 6100eb2bd662Svikram return (NULL); 6101eb2bd662Svikram } 6102eb2bd662Svikram 6103eb2bd662Svikram mhp = s_calloc(1, sizeof (mhash_t)); 6104eb2bd662Svikram 6105eb2bd662Svikram resetmnttab(mfp); 6106eb2bd662Svikram 6107eb2bd662Svikram while (getextmntent(mfp, &mnt, sizeof (mnt)) == 0) { 6108eb2bd662Svikram /* only cache ufs */ 6109eb2bd662Svikram if (strcmp(mnt.mnt_fstype, "ufs") != 0) 6110eb2bd662Svikram continue; 6111eb2bd662Svikram 6112eb2bd662Svikram /* basename() modifies its arg, so dup it */ 6113eb2bd662Svikram special_dup = s_strdup(mnt.mnt_special); 6114eb2bd662Svikram ctds = basename(special_dup); 6115eb2bd662Svikram 6116eb2bd662Svikram mcp = s_calloc(1, sizeof (mcache_t)); 6117eb2bd662Svikram mcp->mc_special = s_strdup(ctds); 6118eb2bd662Svikram mcp->mc_mntpt = s_strdup(mnt.mnt_mountp); 6119eb2bd662Svikram mcp->mc_fstype = s_strdup(mnt.mnt_fstype); 6120eb2bd662Svikram BAM_DPRINTF((D_CACHE_MNTS, fcn, ctds, 6121eb2bd662Svikram mnt.mnt_mountp, mnt.mnt_fstype)); 6122eb2bd662Svikram idx = mhash_fcn(ctds); 6123eb2bd662Svikram mcp->mc_next = mhp->mh_hash[idx]; 6124eb2bd662Svikram mhp->mh_hash[idx] = mcp; 6125eb2bd662Svikram free(special_dup); 6126eb2bd662Svikram } 6127eb2bd662Svikram 6128eb2bd662Svikram (void) fclose(mfp); 6129eb2bd662Svikram 6130eb2bd662Svikram return (mhp); 6131eb2bd662Svikram } 6132eb2bd662Svikram 6133eb2bd662Svikram static void 6134eb2bd662Svikram free_mnttab(mhash_t *mhp) 6135eb2bd662Svikram { 6136eb2bd662Svikram mcache_t *mcp; 6137eb2bd662Svikram int i; 6138eb2bd662Svikram 6139eb2bd662Svikram for (i = 0; i < MH_HASH_SZ; i++) { 6140110570ceSToomas Soome while ((mcp = mhp->mh_hash[i]) != NULL) { 6141eb2bd662Svikram mhp->mh_hash[i] = mcp->mc_next; 6142eb2bd662Svikram free(mcp->mc_special); 6143eb2bd662Svikram free(mcp->mc_mntpt); 6144eb2bd662Svikram free(mcp->mc_fstype); 6145eb2bd662Svikram free(mcp); 6146eb2bd662Svikram } 6147eb2bd662Svikram } 6148eb2bd662Svikram 6149eb2bd662Svikram for (i = 0; i < MH_HASH_SZ; i++) { 6150eb2bd662Svikram assert(mhp->mh_hash[i] == NULL); 6151eb2bd662Svikram } 6152eb2bd662Svikram free(mhp); 6153eb2bd662Svikram } 6154eb2bd662Svikram 6155eb2bd662Svikram static mh_search_t 6156eb2bd662Svikram search_hash(mhash_t *mhp, char *special, char **mntpt) 6157eb2bd662Svikram { 6158eb2bd662Svikram int idx; 6159eb2bd662Svikram mcache_t *mcp; 6160eb2bd662Svikram const char *fcn = "search_hash()"; 6161eb2bd662Svikram 6162eb2bd662Svikram assert(mntpt); 6163eb2bd662Svikram 6164eb2bd662Svikram *mntpt = NULL; 6165eb2bd662Svikram 6166eb2bd662Svikram INJECT_ERROR1("SEARCH_HASH_FULL_PATH", special = "/foo"); 6167eb2bd662Svikram if (strchr(special, '/')) { 6168eb2bd662Svikram bam_error(INVALID_MHASH_KEY, special); 6169eb2bd662Svikram return (MH_ERROR); 6170eb2bd662Svikram } 6171eb2bd662Svikram 6172eb2bd662Svikram idx = mhash_fcn(special); 6173eb2bd662Svikram 6174eb2bd662Svikram for (mcp = mhp->mh_hash[idx]; mcp; mcp = mcp->mc_next) { 6175eb2bd662Svikram if (strcmp(mcp->mc_special, special) == 0) 6176eb2bd662Svikram break; 6177eb2bd662Svikram } 6178eb2bd662Svikram 6179eb2bd662Svikram if (mcp == NULL) { 6180eb2bd662Svikram BAM_DPRINTF((D_MNTTAB_HASH_NOMATCH, fcn, special)); 6181eb2bd662Svikram return (MH_NOMATCH); 6182eb2bd662Svikram } 6183eb2bd662Svikram 6184eb2bd662Svikram assert(strcmp(mcp->mc_fstype, "ufs") == 0); 6185eb2bd662Svikram *mntpt = mcp->mc_mntpt; 6186eb2bd662Svikram BAM_DPRINTF((D_MNTTAB_HASH_MATCH, fcn, special)); 6187eb2bd662Svikram return (MH_MATCH); 6188eb2bd662Svikram } 6189eb2bd662Svikram 6190eb2bd662Svikram static int 6191eb2bd662Svikram check_add_ufs_sign_to_list(FILE *tfp, char *mntpt) 6192eb2bd662Svikram { 6193eb2bd662Svikram char *sign; 6194eb2bd662Svikram char *signline; 6195eb2bd662Svikram char signbuf[MAXNAMELEN]; 6196eb2bd662Svikram int len; 6197eb2bd662Svikram int error; 6198eb2bd662Svikram const char *fcn = "check_add_ufs_sign_to_list()"; 6199eb2bd662Svikram 6200eb2bd662Svikram /* safe to specify NULL as "osdev" arg for UFS */ 6201eb2bd662Svikram sign = find_existing_sign(mntpt, NULL, "ufs"); 6202eb2bd662Svikram if (sign == NULL) { 6203eb2bd662Svikram /* No existing signature, nothing to add to list */ 6204eb2bd662Svikram BAM_DPRINTF((D_NO_SIGN_TO_LIST, fcn, mntpt)); 6205eb2bd662Svikram return (0); 6206eb2bd662Svikram } 6207eb2bd662Svikram 6208eb2bd662Svikram (void) snprintf(signbuf, sizeof (signbuf), "%s\n", sign); 6209eb2bd662Svikram signline = signbuf; 6210eb2bd662Svikram 6211eb2bd662Svikram INJECT_ERROR1("UFS_MNTPT_SIGN_NOTUFS", signline = "pool_rpool10\n"); 6212eb2bd662Svikram if (strncmp(signline, GRUBSIGN_UFS_PREFIX, 6213eb2bd662Svikram strlen(GRUBSIGN_UFS_PREFIX))) { 6214eb2bd662Svikram bam_error(INVALID_UFS_SIGNATURE, sign); 6215eb2bd662Svikram free(sign); 6216eb2bd662Svikram /* ignore invalid signatures */ 6217eb2bd662Svikram return (0); 6218eb2bd662Svikram } 6219eb2bd662Svikram 6220eb2bd662Svikram len = fputs(signline, tfp); 6221eb2bd662Svikram error = errno; 6222eb2bd662Svikram INJECT_ERROR1("SIGN_LIST_PUTS_ERROR", len = 0); 6223eb2bd662Svikram if (len != strlen(signline)) { 6224eb2bd662Svikram bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error)); 6225eb2bd662Svikram free(sign); 6226eb2bd662Svikram return (-1); 6227eb2bd662Svikram } 6228eb2bd662Svikram 6229eb2bd662Svikram free(sign); 6230eb2bd662Svikram 6231eb2bd662Svikram BAM_DPRINTF((D_SIGN_LIST_PUTS_DONE, fcn, mntpt)); 6232eb2bd662Svikram return (0); 6233eb2bd662Svikram } 6234eb2bd662Svikram 6235eb2bd662Svikram /* 6236eb2bd662Svikram * slice is a basename not a full pathname 6237eb2bd662Svikram */ 6238eb2bd662Svikram static int 6239eb2bd662Svikram process_slice_common(char *slice, FILE *tfp, mhash_t *mhp, char *tmpmnt) 6240eb2bd662Svikram { 6241eb2bd662Svikram int ret; 6242eb2bd662Svikram char cmd[PATH_MAX]; 6243eb2bd662Svikram char path[PATH_MAX]; 6244eb2bd662Svikram struct stat sbuf; 6245eb2bd662Svikram char *mntpt; 6246eb2bd662Svikram filelist_t flist = {0}; 6247eb2bd662Svikram char *fstype; 6248eb2bd662Svikram char blkslice[PATH_MAX]; 6249eb2bd662Svikram const char *fcn = "process_slice_common()"; 6250eb2bd662Svikram 6251eb2bd662Svikram 6252eb2bd662Svikram ret = search_hash(mhp, slice, &mntpt); 6253eb2bd662Svikram switch (ret) { 6254eb2bd662Svikram case MH_MATCH: 6255eb2bd662Svikram if (check_add_ufs_sign_to_list(tfp, mntpt) == -1) 6256eb2bd662Svikram return (-1); 6257eb2bd662Svikram else 6258eb2bd662Svikram return (0); 6259eb2bd662Svikram case MH_NOMATCH: 6260eb2bd662Svikram break; 6261eb2bd662Svikram case MH_ERROR: 6262eb2bd662Svikram default: 6263eb2bd662Svikram return (-1); 6264eb2bd662Svikram } 6265eb2bd662Svikram 6266eb2bd662Svikram (void) snprintf(path, sizeof (path), "/dev/rdsk/%s", slice); 6267eb2bd662Svikram if (stat(path, &sbuf) == -1) { 6268eb2bd662Svikram BAM_DPRINTF((D_SLICE_ENOENT, fcn, path)); 6269eb2bd662Svikram return (0); 6270eb2bd662Svikram } 6271eb2bd662Svikram 6272d5cb36d3SJan Setje-Eilers /* Check if ufs. Call ufs fstyp directly to avoid pcfs conflicts. */ 6273eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 6274d5cb36d3SJan Setje-Eilers "/usr/lib/fs/ufs/fstyp /dev/rdsk/%s 2>/dev/null", 6275eb2bd662Svikram slice); 6276eb2bd662Svikram 6277eb2bd662Svikram if (exec_cmd(cmd, &flist) != 0) { 6278eb2bd662Svikram if (bam_verbose) 6279eb2bd662Svikram bam_print(FSTYP_FAILED, slice); 6280eb2bd662Svikram return (0); 6281eb2bd662Svikram } 6282eb2bd662Svikram 6283eb2bd662Svikram if ((flist.head == NULL) || (flist.head != flist.tail)) { 6284eb2bd662Svikram if (bam_verbose) 6285eb2bd662Svikram bam_print(FSTYP_BAD, slice); 6286eb2bd662Svikram filelist_free(&flist); 6287eb2bd662Svikram return (0); 6288eb2bd662Svikram } 6289eb2bd662Svikram 6290eb2bd662Svikram fstype = strtok(flist.head->line, " \t\n"); 6291eb2bd662Svikram if (fstype == NULL || strcmp(fstype, "ufs") != 0) { 6292eb2bd662Svikram if (bam_verbose) 6293eb2bd662Svikram bam_print(NOT_UFS_SLICE, slice, fstype); 6294eb2bd662Svikram filelist_free(&flist); 6295eb2bd662Svikram return (0); 6296eb2bd662Svikram } 6297eb2bd662Svikram 6298eb2bd662Svikram filelist_free(&flist); 6299eb2bd662Svikram 6300eb2bd662Svikram /* 6301eb2bd662Svikram * Since we are mounting the filesystem read-only, the 6302eb2bd662Svikram * the last mount field of the superblock is unchanged 6303eb2bd662Svikram * and does not need to be fixed up post-mount; 6304eb2bd662Svikram */ 6305eb2bd662Svikram 6306eb2bd662Svikram (void) snprintf(blkslice, sizeof (blkslice), "/dev/dsk/%s", 6307eb2bd662Svikram slice); 6308eb2bd662Svikram 6309eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 6310eb2bd662Svikram "/usr/sbin/mount -F ufs -o ro %s %s " 6311eb2bd662Svikram "> /dev/null 2>&1", blkslice, tmpmnt); 6312eb2bd662Svikram 6313eb2bd662Svikram if (exec_cmd(cmd, NULL) != 0) { 6314eb2bd662Svikram if (bam_verbose) 6315eb2bd662Svikram bam_print(MOUNT_FAILED, blkslice, "ufs"); 6316eb2bd662Svikram return (0); 6317eb2bd662Svikram } 6318eb2bd662Svikram 6319eb2bd662Svikram ret = check_add_ufs_sign_to_list(tfp, tmpmnt); 6320eb2bd662Svikram 6321eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 6322eb2bd662Svikram "/usr/sbin/umount -f %s > /dev/null 2>&1", 6323eb2bd662Svikram tmpmnt); 6324eb2bd662Svikram 6325eb2bd662Svikram if (exec_cmd(cmd, NULL) != 0) { 6326eb2bd662Svikram bam_print(UMOUNT_FAILED, slice); 6327eb2bd662Svikram return (0); 6328eb2bd662Svikram } 6329eb2bd662Svikram 6330eb2bd662Svikram return (ret); 6331eb2bd662Svikram } 6332eb2bd662Svikram 6333eb2bd662Svikram static int 6334eb2bd662Svikram process_vtoc_slices( 6335eb2bd662Svikram char *s0, 6336eb2bd662Svikram struct vtoc *vtoc, 6337eb2bd662Svikram FILE *tfp, 6338eb2bd662Svikram mhash_t *mhp, 6339eb2bd662Svikram char *tmpmnt) 6340eb2bd662Svikram { 6341eb2bd662Svikram int idx; 6342eb2bd662Svikram char slice[PATH_MAX]; 6343eb2bd662Svikram size_t len; 6344eb2bd662Svikram char *cp; 6345eb2bd662Svikram const char *fcn = "process_vtoc_slices()"; 6346eb2bd662Svikram 6347eb2bd662Svikram len = strlen(s0); 6348eb2bd662Svikram 6349eb2bd662Svikram assert(s0[len - 2] == 's' && s0[len - 1] == '0'); 6350eb2bd662Svikram 6351eb2bd662Svikram s0[len - 1] = '\0'; 6352eb2bd662Svikram 6353eb2bd662Svikram (void) strlcpy(slice, s0, sizeof (slice)); 6354eb2bd662Svikram 6355eb2bd662Svikram s0[len - 1] = '0'; 6356eb2bd662Svikram 6357eb2bd662Svikram cp = slice + len - 1; 6358eb2bd662Svikram 6359eb2bd662Svikram for (idx = 0; idx < vtoc->v_nparts; idx++) { 6360eb2bd662Svikram 6361eb2bd662Svikram (void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx); 6362eb2bd662Svikram 6363eb2bd662Svikram if (vtoc->v_part[idx].p_size == 0) { 6364eb2bd662Svikram BAM_DPRINTF((D_VTOC_SIZE_ZERO, fcn, slice)); 6365eb2bd662Svikram continue; 6366eb2bd662Svikram } 6367eb2bd662Svikram 6368eb2bd662Svikram /* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */ 6369eb2bd662Svikram switch (vtoc->v_part[idx].p_tag) { 6370eb2bd662Svikram case V_SWAP: 6371eb2bd662Svikram case V_USR: 6372eb2bd662Svikram case V_BACKUP: 6373eb2bd662Svikram case V_VAR: 6374eb2bd662Svikram case V_HOME: 6375eb2bd662Svikram case V_ALTSCTR: 6376eb2bd662Svikram BAM_DPRINTF((D_VTOC_NOT_ROOT_TAG, fcn, slice)); 6377eb2bd662Svikram continue; 6378eb2bd662Svikram default: 6379eb2bd662Svikram BAM_DPRINTF((D_VTOC_ROOT_TAG, fcn, slice)); 6380eb2bd662Svikram break; 6381eb2bd662Svikram } 6382eb2bd662Svikram 6383eb2bd662Svikram /* skip unmountable and readonly slices */ 6384eb2bd662Svikram switch (vtoc->v_part[idx].p_flag) { 6385eb2bd662Svikram case V_UNMNT: 6386eb2bd662Svikram case V_RONLY: 6387eb2bd662Svikram BAM_DPRINTF((D_VTOC_NOT_RDWR_FLAG, fcn, slice)); 6388eb2bd662Svikram continue; 6389eb2bd662Svikram default: 6390eb2bd662Svikram BAM_DPRINTF((D_VTOC_RDWR_FLAG, fcn, slice)); 6391eb2bd662Svikram break; 6392eb2bd662Svikram } 6393eb2bd662Svikram 6394eb2bd662Svikram if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) { 6395eb2bd662Svikram return (-1); 6396eb2bd662Svikram } 6397eb2bd662Svikram } 6398eb2bd662Svikram 6399eb2bd662Svikram return (0); 6400eb2bd662Svikram } 6401eb2bd662Svikram 6402eb2bd662Svikram static int 6403eb2bd662Svikram process_efi_slices( 6404eb2bd662Svikram char *s0, 6405eb2bd662Svikram struct dk_gpt *efi, 6406eb2bd662Svikram FILE *tfp, 6407eb2bd662Svikram mhash_t *mhp, 6408eb2bd662Svikram char *tmpmnt) 6409eb2bd662Svikram { 6410eb2bd662Svikram int idx; 6411eb2bd662Svikram char slice[PATH_MAX]; 6412eb2bd662Svikram size_t len; 6413eb2bd662Svikram char *cp; 6414eb2bd662Svikram const char *fcn = "process_efi_slices()"; 6415eb2bd662Svikram 6416eb2bd662Svikram len = strlen(s0); 6417eb2bd662Svikram 6418eb2bd662Svikram assert(s0[len - 2] == 's' && s0[len - 1] == '0'); 6419eb2bd662Svikram 6420eb2bd662Svikram s0[len - 1] = '\0'; 6421eb2bd662Svikram 6422eb2bd662Svikram (void) strlcpy(slice, s0, sizeof (slice)); 6423eb2bd662Svikram 6424eb2bd662Svikram s0[len - 1] = '0'; 6425eb2bd662Svikram 6426eb2bd662Svikram cp = slice + len - 1; 6427eb2bd662Svikram 6428eb2bd662Svikram for (idx = 0; idx < efi->efi_nparts; idx++) { 6429eb2bd662Svikram 6430eb2bd662Svikram (void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx); 6431eb2bd662Svikram 6432eb2bd662Svikram if (efi->efi_parts[idx].p_size == 0) { 6433eb2bd662Svikram BAM_DPRINTF((D_EFI_SIZE_ZERO, fcn, slice)); 6434eb2bd662Svikram continue; 6435eb2bd662Svikram } 6436eb2bd662Svikram 6437eb2bd662Svikram /* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */ 6438eb2bd662Svikram switch (efi->efi_parts[idx].p_tag) { 6439eb2bd662Svikram case V_SWAP: 6440eb2bd662Svikram case V_USR: 6441eb2bd662Svikram case V_BACKUP: 6442eb2bd662Svikram case V_VAR: 6443eb2bd662Svikram case V_HOME: 6444eb2bd662Svikram case V_ALTSCTR: 6445eb2bd662Svikram BAM_DPRINTF((D_EFI_NOT_ROOT_TAG, fcn, slice)); 6446eb2bd662Svikram continue; 6447eb2bd662Svikram default: 6448eb2bd662Svikram BAM_DPRINTF((D_EFI_ROOT_TAG, fcn, slice)); 6449eb2bd662Svikram break; 6450eb2bd662Svikram } 6451eb2bd662Svikram 6452eb2bd662Svikram /* skip unmountable and readonly slices */ 6453eb2bd662Svikram switch (efi->efi_parts[idx].p_flag) { 6454eb2bd662Svikram case V_UNMNT: 6455eb2bd662Svikram case V_RONLY: 6456eb2bd662Svikram BAM_DPRINTF((D_EFI_NOT_RDWR_FLAG, fcn, slice)); 6457eb2bd662Svikram continue; 6458eb2bd662Svikram default: 6459eb2bd662Svikram BAM_DPRINTF((D_EFI_RDWR_FLAG, fcn, slice)); 6460eb2bd662Svikram break; 6461eb2bd662Svikram } 6462eb2bd662Svikram 6463eb2bd662Svikram if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) { 6464eb2bd662Svikram return (-1); 6465eb2bd662Svikram } 6466eb2bd662Svikram } 6467eb2bd662Svikram 6468eb2bd662Svikram return (0); 6469eb2bd662Svikram } 6470eb2bd662Svikram 6471eb2bd662Svikram /* 6472eb2bd662Svikram * s0 is a basename not a full path 6473eb2bd662Svikram */ 6474eb2bd662Svikram static int 6475eb2bd662Svikram process_slice0(char *s0, FILE *tfp, mhash_t *mhp, char *tmpmnt) 6476eb2bd662Svikram { 6477eb2bd662Svikram struct vtoc vtoc; 6478eb2bd662Svikram struct dk_gpt *efi; 6479eb2bd662Svikram char s0path[PATH_MAX]; 6480eb2bd662Svikram struct stat sbuf; 6481eb2bd662Svikram int e_flag; 6482eb2bd662Svikram int v_flag; 6483eb2bd662Svikram int retval; 6484eb2bd662Svikram int err; 6485eb2bd662Svikram int fd; 6486eb2bd662Svikram const char *fcn = "process_slice0()"; 6487eb2bd662Svikram 6488eb2bd662Svikram (void) snprintf(s0path, sizeof (s0path), "/dev/rdsk/%s", s0); 6489eb2bd662Svikram 6490eb2bd662Svikram if (stat(s0path, &sbuf) == -1) { 6491eb2bd662Svikram BAM_DPRINTF((D_SLICE0_ENOENT, fcn, s0path)); 6492eb2bd662Svikram return (0); 6493eb2bd662Svikram } 6494eb2bd662Svikram 6495eb2bd662Svikram fd = open(s0path, O_NONBLOCK|O_RDONLY); 6496eb2bd662Svikram if (fd == -1) { 6497eb2bd662Svikram bam_error(OPEN_FAIL, s0path, strerror(errno)); 6498eb2bd662Svikram return (0); 6499eb2bd662Svikram } 6500eb2bd662Svikram 6501eb2bd662Svikram e_flag = v_flag = 0; 6502eb2bd662Svikram retval = ((err = read_vtoc(fd, &vtoc)) >= 0) ? 0 : err; 6503eb2bd662Svikram switch (retval) { 6504eb2bd662Svikram case VT_EIO: 6505eb2bd662Svikram BAM_DPRINTF((D_VTOC_READ_FAIL, fcn, s0path)); 6506eb2bd662Svikram break; 6507eb2bd662Svikram case VT_EINVAL: 6508eb2bd662Svikram BAM_DPRINTF((D_VTOC_INVALID, fcn, s0path)); 6509eb2bd662Svikram break; 6510eb2bd662Svikram case VT_ERROR: 6511eb2bd662Svikram BAM_DPRINTF((D_VTOC_UNKNOWN_ERR, fcn, s0path)); 6512eb2bd662Svikram break; 6513eb2bd662Svikram case VT_ENOTSUP: 6514eb2bd662Svikram e_flag = 1; 6515eb2bd662Svikram BAM_DPRINTF((D_VTOC_NOTSUP, fcn, s0path)); 6516eb2bd662Svikram break; 6517eb2bd662Svikram case 0: 6518eb2bd662Svikram v_flag = 1; 6519eb2bd662Svikram BAM_DPRINTF((D_VTOC_READ_SUCCESS, fcn, s0path)); 6520eb2bd662Svikram break; 6521eb2bd662Svikram default: 6522eb2bd662Svikram BAM_DPRINTF((D_VTOC_UNKNOWN_RETCODE, fcn, s0path)); 6523eb2bd662Svikram break; 6524eb2bd662Svikram } 6525eb2bd662Svikram 6526eb2bd662Svikram 6527eb2bd662Svikram if (e_flag) { 6528eb2bd662Svikram e_flag = 0; 6529eb2bd662Svikram retval = ((err = efi_alloc_and_read(fd, &efi)) >= 0) ? 0 : err; 6530eb2bd662Svikram switch (retval) { 6531eb2bd662Svikram case VT_EIO: 6532eb2bd662Svikram BAM_DPRINTF((D_EFI_READ_FAIL, fcn, s0path)); 6533eb2bd662Svikram break; 6534eb2bd662Svikram case VT_EINVAL: 6535eb2bd662Svikram BAM_DPRINTF((D_EFI_INVALID, fcn, s0path)); 6536eb2bd662Svikram break; 6537eb2bd662Svikram case VT_ERROR: 6538eb2bd662Svikram BAM_DPRINTF((D_EFI_UNKNOWN_ERR, fcn, s0path)); 6539eb2bd662Svikram break; 6540eb2bd662Svikram case VT_ENOTSUP: 6541eb2bd662Svikram BAM_DPRINTF((D_EFI_NOTSUP, fcn, s0path)); 6542eb2bd662Svikram break; 6543eb2bd662Svikram case 0: 6544eb2bd662Svikram e_flag = 1; 6545eb2bd662Svikram BAM_DPRINTF((D_EFI_READ_SUCCESS, fcn, s0path)); 6546eb2bd662Svikram break; 6547eb2bd662Svikram default: 6548eb2bd662Svikram BAM_DPRINTF((D_EFI_UNKNOWN_RETCODE, fcn, s0path)); 6549eb2bd662Svikram break; 6550eb2bd662Svikram } 6551eb2bd662Svikram } 6552eb2bd662Svikram 6553eb2bd662Svikram (void) close(fd); 6554eb2bd662Svikram 6555eb2bd662Svikram if (v_flag) { 6556eb2bd662Svikram retval = process_vtoc_slices(s0, 6557eb2bd662Svikram &vtoc, tfp, mhp, tmpmnt); 6558eb2bd662Svikram } else if (e_flag) { 6559eb2bd662Svikram retval = process_efi_slices(s0, 6560eb2bd662Svikram efi, tfp, mhp, tmpmnt); 6561eb2bd662Svikram } else { 6562eb2bd662Svikram BAM_DPRINTF((D_NOT_VTOC_OR_EFI, fcn, s0path)); 6563eb2bd662Svikram return (0); 6564eb2bd662Svikram } 6565eb2bd662Svikram 6566eb2bd662Svikram return (retval); 6567eb2bd662Svikram } 6568eb2bd662Svikram 6569eb2bd662Svikram /* 6570eb2bd662Svikram * Find and create a list of all existing UFS boot signatures 6571eb2bd662Svikram */ 6572eb2bd662Svikram static int 6573eb2bd662Svikram FindAllUfsSignatures(void) 6574eb2bd662Svikram { 6575eb2bd662Svikram mhash_t *mnttab_hash; 6576eb2bd662Svikram DIR *dirp = NULL; 6577eb2bd662Svikram struct dirent *dp; 6578eb2bd662Svikram char tmpmnt[PATH_MAX]; 6579eb2bd662Svikram char cmd[PATH_MAX]; 6580eb2bd662Svikram struct stat sb; 6581eb2bd662Svikram int fd; 6582eb2bd662Svikram FILE *tfp; 6583eb2bd662Svikram size_t len; 6584eb2bd662Svikram int ret; 6585eb2bd662Svikram int error; 6586eb2bd662Svikram const char *fcn = "FindAllUfsSignatures()"; 6587eb2bd662Svikram 6588eb2bd662Svikram if (stat(UFS_SIGNATURE_LIST, &sb) != -1) { 6589eb2bd662Svikram bam_print(SIGNATURE_LIST_EXISTS, UFS_SIGNATURE_LIST); 6590eb2bd662Svikram return (0); 6591eb2bd662Svikram } 6592eb2bd662Svikram 6593eb2bd662Svikram fd = open(UFS_SIGNATURE_LIST".tmp", 6594eb2bd662Svikram O_RDWR|O_CREAT|O_TRUNC, 0644); 6595eb2bd662Svikram error = errno; 6596eb2bd662Svikram INJECT_ERROR1("SIGN_LIST_TMP_TRUNC", fd = -1); 6597eb2bd662Svikram if (fd == -1) { 6598eb2bd662Svikram bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error)); 6599eb2bd662Svikram return (-1); 6600eb2bd662Svikram } 6601eb2bd662Svikram 6602eb2bd662Svikram ret = close(fd); 6603eb2bd662Svikram error = errno; 6604eb2bd662Svikram INJECT_ERROR1("SIGN_LIST_TMP_CLOSE", ret = -1); 6605eb2bd662Svikram if (ret == -1) { 6606eb2bd662Svikram bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp", 6607eb2bd662Svikram strerror(error)); 6608eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 6609eb2bd662Svikram return (-1); 6610eb2bd662Svikram } 6611eb2bd662Svikram 6612eb2bd662Svikram tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a"); 6613eb2bd662Svikram error = errno; 6614eb2bd662Svikram INJECT_ERROR1("SIGN_LIST_APPEND_FOPEN", tfp = NULL); 6615eb2bd662Svikram if (tfp == NULL) { 6616eb2bd662Svikram bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error)); 6617eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 6618eb2bd662Svikram return (-1); 6619eb2bd662Svikram } 6620eb2bd662Svikram 6621eb2bd662Svikram mnttab_hash = cache_mnttab(); 6622eb2bd662Svikram INJECT_ERROR1("CACHE_MNTTAB_ERROR", mnttab_hash = NULL); 6623eb2bd662Svikram if (mnttab_hash == NULL) { 6624eb2bd662Svikram (void) fclose(tfp); 6625eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 6626eb2bd662Svikram bam_error(CACHE_MNTTAB_FAIL, fcn); 6627eb2bd662Svikram return (-1); 6628eb2bd662Svikram } 6629eb2bd662Svikram 6630eb2bd662Svikram (void) snprintf(tmpmnt, sizeof (tmpmnt), 6631eb2bd662Svikram "/tmp/bootadm_ufs_sign_mnt.%d", getpid()); 6632eb2bd662Svikram (void) unlink(tmpmnt); 6633eb2bd662Svikram 663448847494SEnrico Perla - Sun Microsystems ret = mkdirp(tmpmnt, DIR_PERMS); 6635eb2bd662Svikram error = errno; 6636eb2bd662Svikram INJECT_ERROR1("MKDIRP_SIGN_MNT", ret = -1); 6637eb2bd662Svikram if (ret == -1) { 6638eb2bd662Svikram bam_error(MKDIR_FAILED, tmpmnt, strerror(error)); 6639eb2bd662Svikram free_mnttab(mnttab_hash); 6640eb2bd662Svikram (void) fclose(tfp); 6641eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 6642eb2bd662Svikram return (-1); 6643eb2bd662Svikram } 6644eb2bd662Svikram 6645eb2bd662Svikram dirp = opendir("/dev/rdsk"); 6646eb2bd662Svikram error = errno; 6647eb2bd662Svikram INJECT_ERROR1("OPENDIR_DEV_RDSK", dirp = NULL); 6648eb2bd662Svikram if (dirp == NULL) { 6649eb2bd662Svikram bam_error(OPENDIR_FAILED, "/dev/rdsk", strerror(error)); 6650eb2bd662Svikram goto fail; 6651eb2bd662Svikram } 6652eb2bd662Svikram 6653110570ceSToomas Soome while ((dp = readdir(dirp)) != NULL) { 6654eb2bd662Svikram if (strcmp(dp->d_name, ".") == 0 || 6655eb2bd662Svikram strcmp(dp->d_name, "..") == 0) 6656eb2bd662Svikram continue; 6657eb2bd662Svikram 6658eb2bd662Svikram /* 6659eb2bd662Svikram * we only look for the s0 slice. This is guranteed to 6660eb2bd662Svikram * have 's' at len - 2. 6661eb2bd662Svikram */ 6662eb2bd662Svikram len = strlen(dp->d_name); 6663eb2bd662Svikram if (dp->d_name[len - 2 ] != 's' || dp->d_name[len - 1] != '0') { 6664eb2bd662Svikram BAM_DPRINTF((D_SKIP_SLICE_NOTZERO, fcn, dp->d_name)); 6665eb2bd662Svikram continue; 6666eb2bd662Svikram } 6667eb2bd662Svikram 6668eb2bd662Svikram ret = process_slice0(dp->d_name, tfp, mnttab_hash, tmpmnt); 6669eb2bd662Svikram INJECT_ERROR1("PROCESS_S0_FAIL", ret = -1); 6670eb2bd662Svikram if (ret == -1) 6671eb2bd662Svikram goto fail; 6672eb2bd662Svikram } 6673eb2bd662Svikram 6674eb2bd662Svikram (void) closedir(dirp); 6675eb2bd662Svikram free_mnttab(mnttab_hash); 6676eb2bd662Svikram (void) rmdir(tmpmnt); 6677eb2bd662Svikram 6678eb2bd662Svikram ret = fclose(tfp); 6679eb2bd662Svikram error = errno; 6680eb2bd662Svikram INJECT_ERROR1("FCLOSE_SIGNLIST_TMP", ret = EOF); 6681eb2bd662Svikram if (ret == EOF) { 6682eb2bd662Svikram bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp", 6683eb2bd662Svikram strerror(error)); 6684eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 6685eb2bd662Svikram return (-1); 6686eb2bd662Svikram } 6687eb2bd662Svikram 6688eb2bd662Svikram /* We have a list of existing GRUB signatures. Sort it first */ 6689eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 6690eb2bd662Svikram "/usr/bin/sort -u %s.tmp > %s.sorted", 6691eb2bd662Svikram UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST); 6692eb2bd662Svikram 6693eb2bd662Svikram ret = exec_cmd(cmd, NULL); 6694eb2bd662Svikram INJECT_ERROR1("SORT_SIGN_LIST", ret = 1); 6695eb2bd662Svikram if (ret != 0) { 6696eb2bd662Svikram bam_error(GRUBSIGN_SORT_FAILED); 6697eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".sorted"); 6698eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 6699eb2bd662Svikram return (-1); 6700eb2bd662Svikram } 6701eb2bd662Svikram 6702eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 6703eb2bd662Svikram 6704eb2bd662Svikram ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST); 6705eb2bd662Svikram error = errno; 6706eb2bd662Svikram INJECT_ERROR1("RENAME_TMP_SIGNLIST", ret = -1); 6707eb2bd662Svikram if (ret == -1) { 6708eb2bd662Svikram bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error)); 6709eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".sorted"); 6710eb2bd662Svikram return (-1); 6711eb2bd662Svikram } 6712eb2bd662Svikram 6713eb2bd662Svikram if (stat(UFS_SIGNATURE_LIST, &sb) == 0 && sb.st_size == 0) { 6714eb2bd662Svikram BAM_DPRINTF((D_ZERO_LEN_SIGNLIST, fcn, UFS_SIGNATURE_LIST)); 6715eb2bd662Svikram } 6716eb2bd662Svikram 6717eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 6718eb2bd662Svikram return (0); 6719eb2bd662Svikram 6720eb2bd662Svikram fail: 6721eb2bd662Svikram if (dirp) 6722eb2bd662Svikram (void) closedir(dirp); 6723eb2bd662Svikram free_mnttab(mnttab_hash); 6724eb2bd662Svikram (void) rmdir(tmpmnt); 6725eb2bd662Svikram (void) fclose(tfp); 6726eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 6727eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 6728eb2bd662Svikram return (-1); 6729eb2bd662Svikram } 6730eb2bd662Svikram 6731eb2bd662Svikram static char * 6732eb2bd662Svikram create_ufs_sign(void) 6733eb2bd662Svikram { 6734eb2bd662Svikram struct stat sb; 6735eb2bd662Svikram int signnum = -1; 6736eb2bd662Svikram char tmpsign[MAXNAMELEN + 1]; 6737eb2bd662Svikram char *numstr; 6738eb2bd662Svikram int i; 6739eb2bd662Svikram FILE *tfp; 6740eb2bd662Svikram int ret; 6741eb2bd662Svikram int error; 6742eb2bd662Svikram const char *fcn = "create_ufs_sign()"; 6743eb2bd662Svikram 6744eb2bd662Svikram bam_print(SEARCHING_UFS_SIGN); 6745eb2bd662Svikram 6746eb2bd662Svikram ret = FindAllUfsSignatures(); 6747eb2bd662Svikram INJECT_ERROR1("FIND_ALL_UFS", ret = -1); 6748eb2bd662Svikram if (ret == -1) { 6749eb2bd662Svikram bam_error(ERR_FIND_UFS_SIGN); 6750eb2bd662Svikram return (NULL); 6751eb2bd662Svikram } 6752eb2bd662Svikram 6753eb2bd662Svikram /* Make sure the list exists and is owned by root */ 6754eb2bd662Svikram INJECT_ERROR1("SIGNLIST_NOT_CREATED", 6755eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST)); 6756eb2bd662Svikram if (stat(UFS_SIGNATURE_LIST, &sb) == -1 || sb.st_uid != 0) { 6757eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST); 6758eb2bd662Svikram bam_error(UFS_SIGNATURE_LIST_MISS, UFS_SIGNATURE_LIST); 6759eb2bd662Svikram return (NULL); 6760eb2bd662Svikram } 6761eb2bd662Svikram 6762eb2bd662Svikram if (sb.st_size == 0) { 6763eb2bd662Svikram bam_print(GRUBSIGN_UFS_NONE); 6764eb2bd662Svikram i = 0; 6765eb2bd662Svikram goto found; 6766eb2bd662Svikram } 6767eb2bd662Svikram 6768eb2bd662Svikram /* The signature list was sorted when it was created */ 6769eb2bd662Svikram tfp = fopen(UFS_SIGNATURE_LIST, "r"); 6770eb2bd662Svikram error = errno; 6771eb2bd662Svikram INJECT_ERROR1("FOPEN_SIGN_LIST", tfp = NULL); 6772eb2bd662Svikram if (tfp == NULL) { 6773eb2bd662Svikram bam_error(UFS_SIGNATURE_LIST_OPENERR, 6774eb2bd662Svikram UFS_SIGNATURE_LIST, strerror(error)); 6775eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST); 6776eb2bd662Svikram return (NULL); 6777eb2bd662Svikram } 6778eb2bd662Svikram 6779eb2bd662Svikram for (i = 0; s_fgets(tmpsign, sizeof (tmpsign), tfp); i++) { 6780eb2bd662Svikram 6781eb2bd662Svikram if (strncmp(tmpsign, GRUBSIGN_UFS_PREFIX, 6782eb2bd662Svikram strlen(GRUBSIGN_UFS_PREFIX)) != 0) { 6783eb2bd662Svikram (void) fclose(tfp); 6784eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST); 6785eb2bd662Svikram bam_error(UFS_BADSIGN, tmpsign); 6786eb2bd662Svikram return (NULL); 6787eb2bd662Svikram } 6788eb2bd662Svikram numstr = tmpsign + strlen(GRUBSIGN_UFS_PREFIX); 6789eb2bd662Svikram 6790eb2bd662Svikram if (numstr[0] == '\0' || !isdigit(numstr[0])) { 6791eb2bd662Svikram (void) fclose(tfp); 6792eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST); 6793eb2bd662Svikram bam_error(UFS_BADSIGN, tmpsign); 6794eb2bd662Svikram return (NULL); 6795eb2bd662Svikram } 6796eb2bd662Svikram 6797eb2bd662Svikram signnum = atoi(numstr); 6798eb2bd662Svikram INJECT_ERROR1("NEGATIVE_SIGN", signnum = -1); 6799eb2bd662Svikram if (signnum < 0) { 6800eb2bd662Svikram (void) fclose(tfp); 6801eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST); 6802eb2bd662Svikram bam_error(UFS_BADSIGN, tmpsign); 6803eb2bd662Svikram return (NULL); 6804eb2bd662Svikram } 6805eb2bd662Svikram 6806eb2bd662Svikram if (i != signnum) { 6807eb2bd662Svikram BAM_DPRINTF((D_FOUND_HOLE_SIGNLIST, fcn, i)); 6808eb2bd662Svikram break; 6809eb2bd662Svikram } 6810eb2bd662Svikram } 6811eb2bd662Svikram 6812eb2bd662Svikram (void) fclose(tfp); 6813eb2bd662Svikram 6814eb2bd662Svikram found: 6815eb2bd662Svikram (void) snprintf(tmpsign, sizeof (tmpsign), "rootfs%d", i); 6816eb2bd662Svikram 6817eb2bd662Svikram /* add the ufs signature to the /var/run list of signatures */ 6818eb2bd662Svikram ret = ufs_add_to_sign_list(tmpsign); 6819eb2bd662Svikram INJECT_ERROR1("UFS_ADD_TO_SIGN_LIST", ret = -1); 6820eb2bd662Svikram if (ret == -1) { 6821eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST); 6822eb2bd662Svikram bam_error(FAILED_ADD_SIGNLIST, tmpsign); 6823eb2bd662Svikram return (NULL); 6824eb2bd662Svikram } 6825eb2bd662Svikram 6826eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 6827eb2bd662Svikram 6828eb2bd662Svikram return (s_strdup(tmpsign)); 6829eb2bd662Svikram } 6830eb2bd662Svikram 6831eb2bd662Svikram static char * 6832eb2bd662Svikram get_fstype(char *osroot) 6833eb2bd662Svikram { 6834eb2bd662Svikram FILE *mntfp; 6835eb2bd662Svikram struct mnttab mp = {0}; 6836eb2bd662Svikram struct mnttab mpref = {0}; 6837eb2bd662Svikram int error; 6838eb2bd662Svikram int ret; 6839eb2bd662Svikram const char *fcn = "get_fstype()"; 6840eb2bd662Svikram 6841eb2bd662Svikram INJECT_ERROR1("GET_FSTYPE_OSROOT", osroot = NULL); 6842eb2bd662Svikram if (osroot == NULL) { 6843eb2bd662Svikram bam_error(GET_FSTYPE_ARGS); 6844eb2bd662Svikram return (NULL); 6845eb2bd662Svikram } 6846eb2bd662Svikram 6847eb2bd662Svikram mntfp = fopen(MNTTAB, "r"); 6848eb2bd662Svikram error = errno; 6849eb2bd662Svikram INJECT_ERROR1("GET_FSTYPE_FOPEN", mntfp = NULL); 6850eb2bd662Svikram if (mntfp == NULL) { 6851eb2bd662Svikram bam_error(OPEN_FAIL, MNTTAB, strerror(error)); 6852eb2bd662Svikram return (NULL); 6853eb2bd662Svikram } 6854eb2bd662Svikram 6855eb2bd662Svikram if (*osroot == '\0') 6856eb2bd662Svikram mpref.mnt_mountp = "/"; 6857eb2bd662Svikram else 6858eb2bd662Svikram mpref.mnt_mountp = osroot; 6859eb2bd662Svikram 6860eb2bd662Svikram ret = getmntany(mntfp, &mp, &mpref); 6861eb2bd662Svikram INJECT_ERROR1("GET_FSTYPE_GETMNTANY", ret = 1); 6862eb2bd662Svikram if (ret != 0) { 6863eb2bd662Svikram bam_error(MNTTAB_MNTPT_NOT_FOUND, osroot, MNTTAB); 6864eb2bd662Svikram (void) fclose(mntfp); 6865eb2bd662Svikram return (NULL); 6866eb2bd662Svikram } 6867eb2bd662Svikram (void) fclose(mntfp); 6868eb2bd662Svikram 6869eb2bd662Svikram INJECT_ERROR1("GET_FSTYPE_NULL", mp.mnt_fstype = NULL); 6870eb2bd662Svikram if (mp.mnt_fstype == NULL) { 6871eb2bd662Svikram bam_error(MNTTAB_FSTYPE_NULL, osroot); 6872eb2bd662Svikram return (NULL); 6873eb2bd662Svikram } 6874eb2bd662Svikram 6875eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 6876eb2bd662Svikram 6877eb2bd662Svikram return (s_strdup(mp.mnt_fstype)); 6878eb2bd662Svikram } 6879eb2bd662Svikram 6880eb2bd662Svikram static char * 6881eb2bd662Svikram create_zfs_sign(char *osdev) 6882eb2bd662Svikram { 6883eb2bd662Svikram char tmpsign[PATH_MAX]; 6884eb2bd662Svikram char *pool; 6885eb2bd662Svikram const char *fcn = "create_zfs_sign()"; 6886eb2bd662Svikram 6887eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY1, fcn, osdev)); 6888eb2bd662Svikram 6889eb2bd662Svikram /* 6890eb2bd662Svikram * First find the pool name 6891eb2bd662Svikram */ 6892eb2bd662Svikram pool = get_pool(osdev); 6893eb2bd662Svikram INJECT_ERROR1("CREATE_ZFS_SIGN_GET_POOL", pool = NULL); 6894eb2bd662Svikram if (pool == NULL) { 6895eb2bd662Svikram bam_error(GET_POOL_FAILED, osdev); 6896eb2bd662Svikram return (NULL); 6897eb2bd662Svikram } 6898eb2bd662Svikram 6899eb2bd662Svikram (void) snprintf(tmpsign, sizeof (tmpsign), "pool_%s", pool); 6900eb2bd662Svikram 6901eb2bd662Svikram BAM_DPRINTF((D_CREATED_ZFS_SIGN, fcn, tmpsign)); 6902eb2bd662Svikram 6903eb2bd662Svikram free(pool); 6904eb2bd662Svikram 6905eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 6906eb2bd662Svikram 6907eb2bd662Svikram return (s_strdup(tmpsign)); 6908eb2bd662Svikram } 6909eb2bd662Svikram 6910eb2bd662Svikram static char * 6911eb2bd662Svikram create_new_sign(char *osdev, char *fstype) 6912eb2bd662Svikram { 6913eb2bd662Svikram char *sign; 6914eb2bd662Svikram const char *fcn = "create_new_sign()"; 6915eb2bd662Svikram 6916eb2bd662Svikram INJECT_ERROR1("NEW_SIGN_FSTYPE", fstype = "foofs"); 6917eb2bd662Svikram 6918eb2bd662Svikram if (strcmp(fstype, "zfs") == 0) { 6919eb2bd662Svikram BAM_DPRINTF((D_CREATE_NEW_ZFS, fcn)); 6920eb2bd662Svikram sign = create_zfs_sign(osdev); 6921eb2bd662Svikram } else if (strcmp(fstype, "ufs") == 0) { 6922eb2bd662Svikram BAM_DPRINTF((D_CREATE_NEW_UFS, fcn)); 6923eb2bd662Svikram sign = create_ufs_sign(); 6924eb2bd662Svikram } else { 6925eb2bd662Svikram bam_error(GRUBSIGN_NOTSUP, fstype); 6926eb2bd662Svikram sign = NULL; 6927eb2bd662Svikram } 6928eb2bd662Svikram 6929eb2bd662Svikram BAM_DPRINTF((D_CREATED_NEW_SIGN, fcn, sign ? sign : "<NULL>")); 6930eb2bd662Svikram return (sign); 6931eb2bd662Svikram } 6932eb2bd662Svikram 6933eb2bd662Svikram static int 6934eb2bd662Svikram set_backup_common(char *mntpt, char *sign) 6935eb2bd662Svikram { 6936eb2bd662Svikram FILE *bfp; 6937eb2bd662Svikram char backup[PATH_MAX]; 6938eb2bd662Svikram char tmpsign[PATH_MAX]; 6939eb2bd662Svikram int error; 6940eb2bd662Svikram char *bdir; 6941eb2bd662Svikram char *backup_dup; 6942eb2bd662Svikram struct stat sb; 6943eb2bd662Svikram int ret; 6944eb2bd662Svikram const char *fcn = "set_backup_common()"; 6945eb2bd662Svikram 6946eb2bd662Svikram (void) snprintf(backup, sizeof (backup), "%s%s", 6947eb2bd662Svikram mntpt, GRUBSIGN_BACKUP); 6948eb2bd662Svikram 6949eb2bd662Svikram /* First read the backup */ 6950eb2bd662Svikram bfp = fopen(backup, "r"); 6951eb2bd662Svikram if (bfp != NULL) { 6952eb2bd662Svikram while (s_fgets(tmpsign, sizeof (tmpsign), bfp)) { 6953eb2bd662Svikram if (strcmp(tmpsign, sign) == 0) { 6954eb2bd662Svikram BAM_DPRINTF((D_FOUND_IN_BACKUP, fcn, sign)); 6955eb2bd662Svikram (void) fclose(bfp); 6956eb2bd662Svikram return (0); 6957eb2bd662Svikram } 6958eb2bd662Svikram } 6959eb2bd662Svikram (void) fclose(bfp); 6960eb2bd662Svikram BAM_DPRINTF((D_NOT_FOUND_IN_EXIST_BACKUP, fcn, sign)); 6961eb2bd662Svikram } else { 6962eb2bd662Svikram BAM_DPRINTF((D_BACKUP_NOT_EXIST, fcn, backup)); 6963eb2bd662Svikram } 6964eb2bd662Svikram 6965eb2bd662Svikram /* 6966eb2bd662Svikram * Didn't find the correct signature. First create 6967eb2bd662Svikram * the directory if necessary. 6968eb2bd662Svikram */ 6969eb2bd662Svikram 6970eb2bd662Svikram /* dirname() modifies its argument so dup it */ 6971eb2bd662Svikram backup_dup = s_strdup(backup); 6972eb2bd662Svikram bdir = dirname(backup_dup); 6973eb2bd662Svikram assert(bdir); 6974eb2bd662Svikram 6975eb2bd662Svikram ret = stat(bdir, &sb); 6976eb2bd662Svikram INJECT_ERROR1("SET_BACKUP_STAT", ret = -1); 6977eb2bd662Svikram if (ret == -1) { 6978eb2bd662Svikram BAM_DPRINTF((D_BACKUP_DIR_NOEXIST, fcn, bdir)); 697948847494SEnrico Perla - Sun Microsystems ret = mkdirp(bdir, DIR_PERMS); 6980eb2bd662Svikram error = errno; 6981eb2bd662Svikram INJECT_ERROR1("SET_BACKUP_MKDIRP", ret = -1); 6982eb2bd662Svikram if (ret == -1) { 6983eb2bd662Svikram bam_error(GRUBSIGN_BACKUP_MKDIRERR, 6984eb2bd662Svikram GRUBSIGN_BACKUP, strerror(error)); 6985eb2bd662Svikram free(backup_dup); 6986eb2bd662Svikram return (-1); 6987eb2bd662Svikram } 6988eb2bd662Svikram } 6989eb2bd662Svikram free(backup_dup); 6990eb2bd662Svikram 6991eb2bd662Svikram /* 6992eb2bd662Svikram * Open the backup in append mode to add the correct 6993eb2bd662Svikram * signature; 6994eb2bd662Svikram */ 6995eb2bd662Svikram bfp = fopen(backup, "a"); 6996eb2bd662Svikram error = errno; 6997eb2bd662Svikram INJECT_ERROR1("SET_BACKUP_FOPEN_A", bfp = NULL); 6998eb2bd662Svikram if (bfp == NULL) { 6999eb2bd662Svikram bam_error(GRUBSIGN_BACKUP_OPENERR, 7000eb2bd662Svikram GRUBSIGN_BACKUP, strerror(error)); 7001eb2bd662Svikram return (-1); 7002eb2bd662Svikram } 7003eb2bd662Svikram 7004eb2bd662Svikram (void) snprintf(tmpsign, sizeof (tmpsign), "%s\n", sign); 7005eb2bd662Svikram 7006eb2bd662Svikram ret = fputs(tmpsign, bfp); 7007eb2bd662Svikram error = errno; 7008eb2bd662Svikram INJECT_ERROR1("SET_BACKUP_FPUTS", ret = 0); 7009eb2bd662Svikram if (ret != strlen(tmpsign)) { 7010eb2bd662Svikram bam_error(GRUBSIGN_BACKUP_WRITEERR, 7011eb2bd662Svikram GRUBSIGN_BACKUP, strerror(error)); 7012eb2bd662Svikram (void) fclose(bfp); 7013eb2bd662Svikram return (-1); 7014eb2bd662Svikram } 7015eb2bd662Svikram 7016eb2bd662Svikram (void) fclose(bfp); 7017eb2bd662Svikram 7018eb2bd662Svikram if (bam_verbose) 7019eb2bd662Svikram bam_print(GRUBSIGN_BACKUP_UPDATED, GRUBSIGN_BACKUP); 7020eb2bd662Svikram 7021eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7022eb2bd662Svikram 7023eb2bd662Svikram return (0); 7024eb2bd662Svikram } 7025eb2bd662Svikram 7026eb2bd662Svikram static int 7027eb2bd662Svikram set_backup_ufs(char *osroot, char *sign) 7028eb2bd662Svikram { 7029eb2bd662Svikram const char *fcn = "set_backup_ufs()"; 7030eb2bd662Svikram 7031eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign)); 7032eb2bd662Svikram return (set_backup_common(osroot, sign)); 7033eb2bd662Svikram } 7034eb2bd662Svikram 7035eb2bd662Svikram static int 7036eb2bd662Svikram set_backup_zfs(char *osdev, char *sign) 7037eb2bd662Svikram { 7038eb2bd662Svikram char *pool; 7039eb2bd662Svikram char *mntpt; 7040eb2bd662Svikram zfs_mnted_t mnted; 7041eb2bd662Svikram int ret; 7042eb2bd662Svikram const char *fcn = "set_backup_zfs()"; 7043eb2bd662Svikram 7044eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign)); 7045eb2bd662Svikram 7046eb2bd662Svikram pool = get_pool(osdev); 7047eb2bd662Svikram INJECT_ERROR1("SET_BACKUP_GET_POOL", pool = NULL); 7048eb2bd662Svikram if (pool == NULL) { 7049eb2bd662Svikram bam_error(GET_POOL_FAILED, osdev); 7050eb2bd662Svikram return (-1); 7051eb2bd662Svikram } 7052eb2bd662Svikram 7053eb2bd662Svikram mntpt = mount_top_dataset(pool, &mnted); 7054eb2bd662Svikram INJECT_ERROR1("SET_BACKUP_MOUNT_DATASET", mntpt = NULL); 7055eb2bd662Svikram if (mntpt == NULL) { 7056eb2bd662Svikram bam_error(FAIL_MNT_TOP_DATASET, pool); 7057eb2bd662Svikram free(pool); 7058eb2bd662Svikram return (-1); 7059eb2bd662Svikram } 7060eb2bd662Svikram 7061eb2bd662Svikram ret = set_backup_common(mntpt, sign); 7062eb2bd662Svikram 7063eb2bd662Svikram (void) umount_top_dataset(pool, mnted, mntpt); 7064eb2bd662Svikram 7065eb2bd662Svikram free(pool); 7066eb2bd662Svikram 7067eb2bd662Svikram INJECT_ERROR1("SET_BACKUP_ZFS_FAIL", ret = 1); 7068eb2bd662Svikram if (ret == 0) { 7069eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7070eb2bd662Svikram } else { 7071eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 7072eb2bd662Svikram } 7073eb2bd662Svikram 7074eb2bd662Svikram return (ret); 7075eb2bd662Svikram } 7076eb2bd662Svikram 7077eb2bd662Svikram static int 7078eb2bd662Svikram set_backup(char *osroot, char *osdev, char *sign, char *fstype) 7079eb2bd662Svikram { 7080eb2bd662Svikram const char *fcn = "set_backup()"; 7081eb2bd662Svikram int ret; 7082eb2bd662Svikram 7083eb2bd662Svikram INJECT_ERROR1("SET_BACKUP_FSTYPE", fstype = "foofs"); 7084eb2bd662Svikram 7085eb2bd662Svikram if (strcmp(fstype, "ufs") == 0) { 7086eb2bd662Svikram BAM_DPRINTF((D_SET_BACKUP_UFS, fcn)); 7087eb2bd662Svikram ret = set_backup_ufs(osroot, sign); 7088eb2bd662Svikram } else if (strcmp(fstype, "zfs") == 0) { 7089eb2bd662Svikram BAM_DPRINTF((D_SET_BACKUP_ZFS, fcn)); 7090eb2bd662Svikram ret = set_backup_zfs(osdev, sign); 7091eb2bd662Svikram } else { 7092eb2bd662Svikram bam_error(GRUBSIGN_NOTSUP, fstype); 7093eb2bd662Svikram ret = -1; 7094eb2bd662Svikram } 7095eb2bd662Svikram 7096eb2bd662Svikram if (ret == 0) { 7097eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7098eb2bd662Svikram } else { 7099eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 7100eb2bd662Svikram } 7101eb2bd662Svikram 7102eb2bd662Svikram return (ret); 7103eb2bd662Svikram } 7104eb2bd662Svikram 7105eb2bd662Svikram static int 7106eb2bd662Svikram set_primary_common(char *mntpt, char *sign) 7107eb2bd662Svikram { 7108eb2bd662Svikram char signfile[PATH_MAX]; 7109eb2bd662Svikram char signdir[PATH_MAX]; 7110eb2bd662Svikram struct stat sb; 7111eb2bd662Svikram int fd; 7112eb2bd662Svikram int error; 7113eb2bd662Svikram int ret; 7114eb2bd662Svikram const char *fcn = "set_primary_common()"; 7115eb2bd662Svikram 7116eb2bd662Svikram (void) snprintf(signfile, sizeof (signfile), "%s/%s/%s", 7117eb2bd662Svikram mntpt, GRUBSIGN_DIR, sign); 7118eb2bd662Svikram 7119eb2bd662Svikram if (stat(signfile, &sb) != -1) { 7120eb2bd662Svikram if (bam_verbose) 7121eb2bd662Svikram bam_print(PRIMARY_SIGN_EXISTS, sign); 7122eb2bd662Svikram return (0); 7123eb2bd662Svikram } else { 7124eb2bd662Svikram BAM_DPRINTF((D_PRIMARY_NOT_EXIST, fcn, signfile)); 7125eb2bd662Svikram } 7126eb2bd662Svikram 7127eb2bd662Svikram (void) snprintf(signdir, sizeof (signdir), "%s/%s", 7128eb2bd662Svikram mntpt, GRUBSIGN_DIR); 7129eb2bd662Svikram 7130eb2bd662Svikram if (stat(signdir, &sb) == -1) { 7131eb2bd662Svikram BAM_DPRINTF((D_PRIMARY_DIR_NOEXIST, fcn, signdir)); 713248847494SEnrico Perla - Sun Microsystems ret = mkdirp(signdir, DIR_PERMS); 7133eb2bd662Svikram error = errno; 7134eb2bd662Svikram INJECT_ERROR1("SET_PRIMARY_MKDIRP", ret = -1); 7135eb2bd662Svikram if (ret == -1) { 7136eb2bd662Svikram bam_error(GRUBSIGN_MKDIR_ERR, signdir, strerror(errno)); 7137eb2bd662Svikram return (-1); 7138eb2bd662Svikram } 7139eb2bd662Svikram } 7140eb2bd662Svikram 7141eb2bd662Svikram fd = open(signfile, O_RDWR|O_CREAT|O_TRUNC, 0444); 7142eb2bd662Svikram error = errno; 7143eb2bd662Svikram INJECT_ERROR1("PRIMARY_SIGN_CREAT", fd = -1); 7144eb2bd662Svikram if (fd == -1) { 7145eb2bd662Svikram bam_error(GRUBSIGN_PRIMARY_CREATERR, signfile, strerror(error)); 7146eb2bd662Svikram return (-1); 7147eb2bd662Svikram } 7148eb2bd662Svikram 7149eb2bd662Svikram ret = fsync(fd); 7150eb2bd662Svikram error = errno; 7151eb2bd662Svikram INJECT_ERROR1("PRIMARY_FSYNC", ret = -1); 7152eb2bd662Svikram if (ret != 0) { 7153eb2bd662Svikram bam_error(GRUBSIGN_PRIMARY_SYNCERR, signfile, strerror(error)); 7154eb2bd662Svikram } 7155eb2bd662Svikram 7156eb2bd662Svikram (void) close(fd); 7157eb2bd662Svikram 7158eb2bd662Svikram if (bam_verbose) 7159eb2bd662Svikram bam_print(GRUBSIGN_CREATED_PRIMARY, signfile); 7160eb2bd662Svikram 7161eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7162eb2bd662Svikram 7163eb2bd662Svikram return (0); 7164eb2bd662Svikram } 7165eb2bd662Svikram 7166eb2bd662Svikram static int 7167eb2bd662Svikram set_primary_ufs(char *osroot, char *sign) 7168eb2bd662Svikram { 7169eb2bd662Svikram const char *fcn = "set_primary_ufs()"; 7170eb2bd662Svikram 7171eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign)); 7172eb2bd662Svikram return (set_primary_common(osroot, sign)); 7173eb2bd662Svikram } 7174eb2bd662Svikram 7175eb2bd662Svikram static int 7176eb2bd662Svikram set_primary_zfs(char *osdev, char *sign) 7177eb2bd662Svikram { 7178eb2bd662Svikram char *pool; 7179eb2bd662Svikram char *mntpt; 7180eb2bd662Svikram zfs_mnted_t mnted; 7181eb2bd662Svikram int ret; 7182eb2bd662Svikram const char *fcn = "set_primary_zfs()"; 7183eb2bd662Svikram 7184eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign)); 7185eb2bd662Svikram 7186eb2bd662Svikram pool = get_pool(osdev); 7187eb2bd662Svikram INJECT_ERROR1("SET_PRIMARY_ZFS_GET_POOL", pool = NULL); 7188eb2bd662Svikram if (pool == NULL) { 7189eb2bd662Svikram bam_error(GET_POOL_FAILED, osdev); 7190eb2bd662Svikram return (-1); 7191eb2bd662Svikram } 7192eb2bd662Svikram 7193eb2bd662Svikram /* Pool name must exist in the sign */ 7194eb2bd662Svikram ret = (strstr(sign, pool) != NULL); 7195eb2bd662Svikram INJECT_ERROR1("SET_PRIMARY_ZFS_POOL_SIGN_INCOMPAT", ret = 0); 7196eb2bd662Svikram if (ret == 0) { 7197eb2bd662Svikram bam_error(POOL_SIGN_INCOMPAT, pool, sign); 7198eb2bd662Svikram free(pool); 7199eb2bd662Svikram return (-1); 7200eb2bd662Svikram } 7201eb2bd662Svikram 7202eb2bd662Svikram mntpt = mount_top_dataset(pool, &mnted); 7203eb2bd662Svikram INJECT_ERROR1("SET_PRIMARY_ZFS_MOUNT_DATASET", mntpt = NULL); 7204eb2bd662Svikram if (mntpt == NULL) { 7205eb2bd662Svikram bam_error(FAIL_MNT_TOP_DATASET, pool); 7206eb2bd662Svikram free(pool); 7207eb2bd662Svikram return (-1); 7208eb2bd662Svikram } 7209eb2bd662Svikram 7210eb2bd662Svikram ret = set_primary_common(mntpt, sign); 7211eb2bd662Svikram 7212eb2bd662Svikram (void) umount_top_dataset(pool, mnted, mntpt); 7213eb2bd662Svikram 7214eb2bd662Svikram free(pool); 7215eb2bd662Svikram 7216eb2bd662Svikram INJECT_ERROR1("SET_PRIMARY_ZFS_FAIL", ret = 1); 7217eb2bd662Svikram if (ret == 0) { 7218eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7219eb2bd662Svikram } else { 7220eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 7221eb2bd662Svikram } 7222eb2bd662Svikram 7223eb2bd662Svikram return (ret); 7224eb2bd662Svikram } 7225eb2bd662Svikram 7226eb2bd662Svikram static int 7227eb2bd662Svikram set_primary(char *osroot, char *osdev, char *sign, char *fstype) 7228eb2bd662Svikram { 7229eb2bd662Svikram const char *fcn = "set_primary()"; 7230eb2bd662Svikram int ret; 7231eb2bd662Svikram 7232eb2bd662Svikram INJECT_ERROR1("SET_PRIMARY_FSTYPE", fstype = "foofs"); 7233eb2bd662Svikram if (strcmp(fstype, "ufs") == 0) { 7234eb2bd662Svikram BAM_DPRINTF((D_SET_PRIMARY_UFS, fcn)); 7235eb2bd662Svikram ret = set_primary_ufs(osroot, sign); 7236eb2bd662Svikram } else if (strcmp(fstype, "zfs") == 0) { 7237eb2bd662Svikram BAM_DPRINTF((D_SET_PRIMARY_ZFS, fcn)); 7238eb2bd662Svikram ret = set_primary_zfs(osdev, sign); 7239eb2bd662Svikram } else { 7240eb2bd662Svikram bam_error(GRUBSIGN_NOTSUP, fstype); 7241eb2bd662Svikram ret = -1; 7242eb2bd662Svikram } 7243eb2bd662Svikram 7244eb2bd662Svikram if (ret == 0) { 7245eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7246eb2bd662Svikram } else { 7247eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 7248eb2bd662Svikram } 7249eb2bd662Svikram 7250eb2bd662Svikram return (ret); 7251eb2bd662Svikram } 7252eb2bd662Svikram 7253eb2bd662Svikram static int 7254eb2bd662Svikram ufs_add_to_sign_list(char *sign) 7255eb2bd662Svikram { 7256eb2bd662Svikram FILE *tfp; 7257eb2bd662Svikram char signline[MAXNAMELEN]; 7258eb2bd662Svikram char cmd[PATH_MAX]; 7259eb2bd662Svikram int ret; 7260eb2bd662Svikram int error; 7261eb2bd662Svikram const char *fcn = "ufs_add_to_sign_list()"; 7262eb2bd662Svikram 7263eb2bd662Svikram INJECT_ERROR1("ADD_TO_SIGN_LIST_NOT_UFS", sign = "pool_rpool5"); 7264eb2bd662Svikram if (strncmp(sign, GRUBSIGN_UFS_PREFIX, 7265eb2bd662Svikram strlen(GRUBSIGN_UFS_PREFIX)) != 0) { 7266eb2bd662Svikram bam_error(INVALID_UFS_SIGN, sign); 7267eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST); 7268eb2bd662Svikram return (-1); 7269eb2bd662Svikram } 7270eb2bd662Svikram 7271eb2bd662Svikram /* 7272eb2bd662Svikram * most failures in this routine are not a fatal error 7273eb2bd662Svikram * We simply unlink the /var/run file and continue 7274eb2bd662Svikram */ 7275eb2bd662Svikram 7276eb2bd662Svikram ret = rename(UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST".tmp"); 7277eb2bd662Svikram error = errno; 7278eb2bd662Svikram INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME", ret = -1); 7279eb2bd662Svikram if (ret == -1) { 7280eb2bd662Svikram bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST".tmp", 7281eb2bd662Svikram strerror(error)); 7282eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST); 7283eb2bd662Svikram return (0); 7284eb2bd662Svikram } 7285eb2bd662Svikram 7286eb2bd662Svikram tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a"); 7287eb2bd662Svikram error = errno; 7288eb2bd662Svikram INJECT_ERROR1("ADD_TO_SIGN_LIST_FOPEN", tfp = NULL); 7289eb2bd662Svikram if (tfp == NULL) { 7290eb2bd662Svikram bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error)); 7291eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 7292eb2bd662Svikram return (0); 7293eb2bd662Svikram } 7294eb2bd662Svikram 7295eb2bd662Svikram (void) snprintf(signline, sizeof (signline), "%s\n", sign); 7296eb2bd662Svikram 7297eb2bd662Svikram ret = fputs(signline, tfp); 7298eb2bd662Svikram error = errno; 7299eb2bd662Svikram INJECT_ERROR1("ADD_TO_SIGN_LIST_FPUTS", ret = 0); 7300eb2bd662Svikram if (ret != strlen(signline)) { 7301eb2bd662Svikram bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error)); 7302eb2bd662Svikram (void) fclose(tfp); 7303eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 7304eb2bd662Svikram return (0); 7305eb2bd662Svikram } 7306eb2bd662Svikram 7307eb2bd662Svikram ret = fclose(tfp); 7308eb2bd662Svikram error = errno; 7309eb2bd662Svikram INJECT_ERROR1("ADD_TO_SIGN_LIST_FCLOSE", ret = EOF); 7310eb2bd662Svikram if (ret == EOF) { 7311eb2bd662Svikram bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp", 7312eb2bd662Svikram strerror(error)); 7313eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 7314eb2bd662Svikram return (0); 7315eb2bd662Svikram } 7316eb2bd662Svikram 7317eb2bd662Svikram /* Sort the list again */ 7318eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), 7319eb2bd662Svikram "/usr/bin/sort -u %s.tmp > %s.sorted", 7320eb2bd662Svikram UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST); 7321eb2bd662Svikram 7322eb2bd662Svikram ret = exec_cmd(cmd, NULL); 7323eb2bd662Svikram INJECT_ERROR1("ADD_TO_SIGN_LIST_SORT", ret = 1); 7324eb2bd662Svikram if (ret != 0) { 7325eb2bd662Svikram bam_error(GRUBSIGN_SORT_FAILED); 7326eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".sorted"); 7327eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 7328eb2bd662Svikram return (0); 7329eb2bd662Svikram } 7330eb2bd662Svikram 7331eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".tmp"); 7332eb2bd662Svikram 7333eb2bd662Svikram ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST); 7334eb2bd662Svikram error = errno; 7335eb2bd662Svikram INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME2", ret = -1); 7336eb2bd662Svikram if (ret == -1) { 7337eb2bd662Svikram bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error)); 7338eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST".sorted"); 7339eb2bd662Svikram return (0); 7340eb2bd662Svikram } 7341eb2bd662Svikram 7342eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7343eb2bd662Svikram 7344eb2bd662Svikram return (0); 7345eb2bd662Svikram } 7346eb2bd662Svikram 7347eb2bd662Svikram static int 7348eb2bd662Svikram set_signature(char *osroot, char *osdev, char *sign, char *fstype) 7349eb2bd662Svikram { 7350eb2bd662Svikram int ret; 7351eb2bd662Svikram const char *fcn = "set_signature()"; 7352eb2bd662Svikram 7353eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY4, fcn, osroot, osdev, sign, fstype)); 7354eb2bd662Svikram 7355eb2bd662Svikram ret = set_backup(osroot, osdev, sign, fstype); 7356eb2bd662Svikram INJECT_ERROR1("SET_SIGNATURE_BACKUP", ret = -1); 7357eb2bd662Svikram if (ret == -1) { 7358eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 7359eb2bd662Svikram bam_error(SET_BACKUP_FAILED, sign, osroot, osdev); 7360eb2bd662Svikram return (-1); 7361eb2bd662Svikram } 7362eb2bd662Svikram 7363eb2bd662Svikram ret = set_primary(osroot, osdev, sign, fstype); 7364eb2bd662Svikram INJECT_ERROR1("SET_SIGNATURE_PRIMARY", ret = -1); 7365eb2bd662Svikram 7366eb2bd662Svikram if (ret == 0) { 7367eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7368eb2bd662Svikram } else { 7369eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 7370eb2bd662Svikram bam_error(SET_PRIMARY_FAILED, sign, osroot, osdev); 7371eb2bd662Svikram 7372eb2bd662Svikram } 7373eb2bd662Svikram return (ret); 7374eb2bd662Svikram } 7375eb2bd662Svikram 7376eb2bd662Svikram char * 7377eb2bd662Svikram get_grubsign(char *osroot, char *osdev) 7378eb2bd662Svikram { 7379eb2bd662Svikram char *grubsign; /* (<sign>,#,#) */ 7380eb2bd662Svikram char *slice; 7381eb2bd662Svikram int fdiskpart; 7382eb2bd662Svikram char *sign; 7383eb2bd662Svikram char *fstype; 7384eb2bd662Svikram int ret; 7385eb2bd662Svikram const char *fcn = "get_grubsign()"; 7386eb2bd662Svikram 7387eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, osdev)); 7388eb2bd662Svikram fstype = get_fstype(osroot); 7389eb2bd662Svikram INJECT_ERROR1("GET_GRUBSIGN_FSTYPE", fstype = NULL); 7390eb2bd662Svikram if (fstype == NULL) { 7391eb2bd662Svikram bam_error(GET_FSTYPE_FAILED, osroot); 7392eb2bd662Svikram return (NULL); 7393eb2bd662Svikram } 7394eb2bd662Svikram 7395eb2bd662Svikram sign = find_existing_sign(osroot, osdev, fstype); 7396eb2bd662Svikram INJECT_ERROR1("FIND_EXISTING_SIGN", sign = NULL); 7397eb2bd662Svikram if (sign == NULL) { 7398eb2bd662Svikram BAM_DPRINTF((D_GET_GRUBSIGN_NO_EXISTING, fcn, osroot, osdev)); 7399eb2bd662Svikram sign = create_new_sign(osdev, fstype); 7400eb2bd662Svikram INJECT_ERROR1("CREATE_NEW_SIGN", sign = NULL); 7401eb2bd662Svikram if (sign == NULL) { 7402eb2bd662Svikram bam_error(GRUBSIGN_CREATE_FAIL, osdev); 7403eb2bd662Svikram free(fstype); 7404eb2bd662Svikram return (NULL); 7405eb2bd662Svikram } 7406eb2bd662Svikram } 7407eb2bd662Svikram 7408eb2bd662Svikram ret = set_signature(osroot, osdev, sign, fstype); 7409eb2bd662Svikram INJECT_ERROR1("SET_SIGNATURE_FAIL", ret = -1); 7410eb2bd662Svikram if (ret == -1) { 7411eb2bd662Svikram bam_error(GRUBSIGN_WRITE_FAIL, osdev); 7412eb2bd662Svikram free(sign); 7413eb2bd662Svikram free(fstype); 7414eb2bd662Svikram (void) unlink(UFS_SIGNATURE_LIST); 7415eb2bd662Svikram return (NULL); 7416eb2bd662Svikram } 7417eb2bd662Svikram 7418eb2bd662Svikram free(fstype); 7419eb2bd662Svikram 7420eb2bd662Svikram if (bam_verbose) 7421eb2bd662Svikram bam_print(GRUBSIGN_FOUND_OR_CREATED, sign, osdev); 7422eb2bd662Svikram 7423eb2bd662Svikram fdiskpart = get_partition(osdev); 74241a902ef8SHans Rosenfeld INJECT_ERROR1("GET_GRUBSIGN_FDISK", fdiskpart = PARTNO_NOTFOUND); 74251a902ef8SHans Rosenfeld if (fdiskpart == PARTNO_NOTFOUND) { 7426eb2bd662Svikram bam_error(FDISKPART_FAIL, osdev); 7427eb2bd662Svikram free(sign); 7428eb2bd662Svikram return (NULL); 7429eb2bd662Svikram } 7430eb2bd662Svikram 7431eb2bd662Svikram slice = strrchr(osdev, 's'); 7432eb2bd662Svikram 74331a902ef8SHans Rosenfeld if (fdiskpart == PARTNO_EFI) { 74341a902ef8SHans Rosenfeld fdiskpart = atoi(&slice[1]); 74351a902ef8SHans Rosenfeld slice = NULL; 74361a902ef8SHans Rosenfeld } 74371a902ef8SHans Rosenfeld 7438eb2bd662Svikram grubsign = s_calloc(1, MAXNAMELEN + 10); 7439eb2bd662Svikram if (slice) { 7440eb2bd662Svikram (void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d,%c)", 7441eb2bd662Svikram sign, fdiskpart, slice[1] + 'a' - '0'); 7442eb2bd662Svikram } else 7443eb2bd662Svikram (void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d)", 7444eb2bd662Svikram sign, fdiskpart); 7445eb2bd662Svikram 7446eb2bd662Svikram free(sign); 7447eb2bd662Svikram 7448eb2bd662Svikram BAM_DPRINTF((D_GET_GRUBSIGN_SUCCESS, fcn, grubsign)); 7449eb2bd662Svikram 7450eb2bd662Svikram return (grubsign); 74517c478bd9Sstevel@tonic-gate } 74527c478bd9Sstevel@tonic-gate 7453ee3b8144Sszhou static char * 7454ee3b8144Sszhou get_title(char *rootdir) 74557c478bd9Sstevel@tonic-gate { 7456eb2bd662Svikram static char title[80]; 7457eb2bd662Svikram char *cp = NULL; 7458eb2bd662Svikram char release[PATH_MAX]; 74597c478bd9Sstevel@tonic-gate FILE *fp; 7460eb2bd662Svikram const char *fcn = "get_title()"; 74617c478bd9Sstevel@tonic-gate 74627c478bd9Sstevel@tonic-gate /* open the /etc/release file */ 74637c478bd9Sstevel@tonic-gate (void) snprintf(release, sizeof (release), "%s/etc/release", rootdir); 74647c478bd9Sstevel@tonic-gate 74657c478bd9Sstevel@tonic-gate fp = fopen(release, "r"); 7466eb2bd662Svikram if (fp == NULL) { 7467eb2bd662Svikram bam_error(OPEN_FAIL, release, strerror(errno)); 7468eb2bd662Svikram cp = NULL; 7469eb2bd662Svikram goto out; 7470eb2bd662Svikram } 74717c478bd9Sstevel@tonic-gate 7472655ac28eSGeorge Asaad /* grab first line of /etc/release */ 7473655ac28eSGeorge Asaad cp = s_fgets(title, sizeof (title), fp); 7474655ac28eSGeorge Asaad if (cp) { 7475655ac28eSGeorge Asaad while (isspace(*cp)) /* remove leading spaces */ 7476655ac28eSGeorge Asaad cp++; 74777c478bd9Sstevel@tonic-gate } 7478655ac28eSGeorge Asaad 74797c478bd9Sstevel@tonic-gate (void) fclose(fp); 7480eb2bd662Svikram 7481eb2bd662Svikram out: 7482655ac28eSGeorge Asaad cp = cp ? cp : "Oracle Solaris"; 7483eb2bd662Svikram 7484eb2bd662Svikram BAM_DPRINTF((D_GET_TITLE, fcn, cp)); 7485eb2bd662Svikram 7486eb2bd662Svikram return (cp); 74877c478bd9Sstevel@tonic-gate } 74887c478bd9Sstevel@tonic-gate 7489ae115bc7Smrj char * 74907c478bd9Sstevel@tonic-gate get_special(char *mountp) 74917c478bd9Sstevel@tonic-gate { 74927c478bd9Sstevel@tonic-gate FILE *mntfp; 7493eb2bd662Svikram struct mnttab mp = {0}; 7494eb2bd662Svikram struct mnttab mpref = {0}; 7495eb2bd662Svikram int error; 7496eb2bd662Svikram int ret; 7497eb2bd662Svikram const char *fcn = "get_special()"; 7498eb2bd662Svikram 7499eb2bd662Svikram INJECT_ERROR1("GET_SPECIAL_MNTPT", mountp = NULL); 7500eb2bd662Svikram if (mountp == NULL) { 7501eb2bd662Svikram bam_error(GET_SPECIAL_NULL_MNTPT); 7502eb2bd662Svikram return (NULL); 7503eb2bd662Svikram } 75047c478bd9Sstevel@tonic-gate 75057c478bd9Sstevel@tonic-gate mntfp = fopen(MNTTAB, "r"); 7506eb2bd662Svikram error = errno; 7507eb2bd662Svikram INJECT_ERROR1("GET_SPECIAL_MNTTAB_OPEN", mntfp = NULL); 75087c478bd9Sstevel@tonic-gate if (mntfp == NULL) { 7509eb2bd662Svikram bam_error(OPEN_FAIL, MNTTAB, strerror(error)); 7510eb2bd662Svikram return (NULL); 75117c478bd9Sstevel@tonic-gate } 75127c478bd9Sstevel@tonic-gate 75137c478bd9Sstevel@tonic-gate if (*mountp == '\0') 75147c478bd9Sstevel@tonic-gate mpref.mnt_mountp = "/"; 75157c478bd9Sstevel@tonic-gate else 75167c478bd9Sstevel@tonic-gate mpref.mnt_mountp = mountp; 7517eb2bd662Svikram 7518eb2bd662Svikram ret = getmntany(mntfp, &mp, &mpref); 7519eb2bd662Svikram INJECT_ERROR1("GET_SPECIAL_MNTTAB_SEARCH", ret = 1); 7520eb2bd662Svikram if (ret != 0) { 75217c478bd9Sstevel@tonic-gate (void) fclose(mntfp); 7522eb2bd662Svikram BAM_DPRINTF((D_GET_SPECIAL_NOT_IN_MNTTAB, fcn, mountp)); 75237c478bd9Sstevel@tonic-gate return (NULL); 75247c478bd9Sstevel@tonic-gate } 75257c478bd9Sstevel@tonic-gate (void) fclose(mntfp); 75267c478bd9Sstevel@tonic-gate 7527eb2bd662Svikram BAM_DPRINTF((D_GET_SPECIAL, fcn, mp.mnt_special)); 7528eb2bd662Svikram 75297c478bd9Sstevel@tonic-gate return (s_strdup(mp.mnt_special)); 75307c478bd9Sstevel@tonic-gate } 75317c478bd9Sstevel@tonic-gate 7532eb2bd662Svikram static void 7533eb2bd662Svikram free_physarray(char **physarray, int n) 75347c478bd9Sstevel@tonic-gate { 7535eb2bd662Svikram int i; 7536eb2bd662Svikram const char *fcn = "free_physarray()"; 75377c478bd9Sstevel@tonic-gate 7538eb2bd662Svikram assert(physarray); 7539eb2bd662Svikram assert(n); 7540eb2bd662Svikram 7541eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY_N1, fcn, n)); 7542eb2bd662Svikram 7543eb2bd662Svikram for (i = 0; i < n; i++) { 7544eb2bd662Svikram free(physarray[i]); 75457c478bd9Sstevel@tonic-gate } 7546eb2bd662Svikram free(physarray); 7547eb2bd662Svikram 7548eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7549eb2bd662Svikram } 7550eb2bd662Svikram 7551eb2bd662Svikram static int 7552eb2bd662Svikram zfs_get_physical(char *special, char ***physarray, int *n) 7553eb2bd662Svikram { 7554eb2bd662Svikram char sdup[PATH_MAX]; 7555eb2bd662Svikram char cmd[PATH_MAX]; 7556eb2bd662Svikram char dsk[PATH_MAX]; 7557eb2bd662Svikram char *pool; 7558eb2bd662Svikram filelist_t flist = {0}; 7559eb2bd662Svikram line_t *lp; 7560eb2bd662Svikram line_t *startlp; 7561eb2bd662Svikram char *comp1; 7562eb2bd662Svikram int i; 7563eb2bd662Svikram int ret; 7564eb2bd662Svikram const char *fcn = "zfs_get_physical()"; 7565eb2bd662Svikram 7566eb2bd662Svikram assert(special); 7567eb2bd662Svikram 7568eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY1, fcn, special)); 7569eb2bd662Svikram 7570eb2bd662Svikram INJECT_ERROR1("INVALID_ZFS_SPECIAL", special = "/foo"); 7571eb2bd662Svikram if (special[0] == '/') { 7572eb2bd662Svikram bam_error(INVALID_ZFS_SPECIAL, special); 7573eb2bd662Svikram return (-1); 7574eb2bd662Svikram } 7575eb2bd662Svikram 7576eb2bd662Svikram (void) strlcpy(sdup, special, sizeof (sdup)); 7577eb2bd662Svikram 7578eb2bd662Svikram pool = strtok(sdup, "/"); 7579eb2bd662Svikram INJECT_ERROR1("ZFS_GET_PHYS_POOL", pool = NULL); 7580eb2bd662Svikram if (pool == NULL) { 7581eb2bd662Svikram bam_error(CANT_FIND_POOL_FROM_SPECIAL, special); 7582eb2bd662Svikram return (-1); 7583eb2bd662Svikram } 7584eb2bd662Svikram 7585eb2bd662Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/zpool status %s", pool); 7586eb2bd662Svikram 7587eb2bd662Svikram ret = exec_cmd(cmd, &flist); 7588eb2bd662Svikram INJECT_ERROR1("ZFS_GET_PHYS_STATUS", ret = 1); 7589eb2bd662Svikram if (ret != 0) { 7590eb2bd662Svikram bam_error(ZFS_GET_POOL_STATUS, pool); 7591eb2bd662Svikram return (-1); 7592eb2bd662Svikram } 7593eb2bd662Svikram 7594eb2bd662Svikram INJECT_ERROR1("ZFS_GET_PHYS_STATUS_OUT", flist.head = NULL); 7595eb2bd662Svikram if (flist.head == NULL) { 7596eb2bd662Svikram bam_error(BAD_ZPOOL_STATUS, pool); 7597eb2bd662Svikram filelist_free(&flist); 7598eb2bd662Svikram return (-1); 7599eb2bd662Svikram } 7600eb2bd662Svikram 7601eb2bd662Svikram for (lp = flist.head; lp; lp = lp->next) { 7602eb2bd662Svikram BAM_DPRINTF((D_STRTOK_ZPOOL_STATUS, fcn, lp->line)); 7603eb2bd662Svikram comp1 = strtok(lp->line, " \t"); 7604eb2bd662Svikram if (comp1 == NULL) { 7605eb2bd662Svikram free(lp->line); 7606eb2bd662Svikram lp->line = NULL; 7607eb2bd662Svikram } else { 7608eb2bd662Svikram comp1 = s_strdup(comp1); 7609eb2bd662Svikram free(lp->line); 7610eb2bd662Svikram lp->line = comp1; 7611eb2bd662Svikram } 7612eb2bd662Svikram } 7613eb2bd662Svikram 7614eb2bd662Svikram for (lp = flist.head; lp; lp = lp->next) { 7615eb2bd662Svikram if (lp->line == NULL) 7616eb2bd662Svikram continue; 7617eb2bd662Svikram if (strcmp(lp->line, pool) == 0) { 7618eb2bd662Svikram BAM_DPRINTF((D_FOUND_POOL_IN_ZPOOL_STATUS, fcn, pool)); 7619eb2bd662Svikram break; 7620eb2bd662Svikram } 7621eb2bd662Svikram } 7622eb2bd662Svikram 7623eb2bd662Svikram if (lp == NULL) { 7624eb2bd662Svikram bam_error(NO_POOL_IN_ZPOOL_STATUS, pool); 7625eb2bd662Svikram filelist_free(&flist); 7626eb2bd662Svikram return (-1); 7627eb2bd662Svikram } 7628eb2bd662Svikram 7629eb2bd662Svikram startlp = lp->next; 7630eb2bd662Svikram for (i = 0, lp = startlp; lp; lp = lp->next) { 7631eb2bd662Svikram if (lp->line == NULL) 7632eb2bd662Svikram continue; 7633eb2bd662Svikram if (strcmp(lp->line, "mirror") == 0) 7634eb2bd662Svikram continue; 7635eb2bd662Svikram if (lp->line[0] == '\0' || strcmp(lp->line, "errors:") == 0) 7636eb2bd662Svikram break; 7637eb2bd662Svikram i++; 7638eb2bd662Svikram BAM_DPRINTF((D_COUNTING_ZFS_PHYS, fcn, i)); 7639eb2bd662Svikram } 7640eb2bd662Svikram 7641eb2bd662Svikram if (i == 0) { 7642eb2bd662Svikram bam_error(NO_PHYS_IN_ZPOOL_STATUS, pool); 7643eb2bd662Svikram filelist_free(&flist); 7644eb2bd662Svikram return (-1); 7645eb2bd662Svikram } 7646eb2bd662Svikram 7647eb2bd662Svikram *n = i; 7648eb2bd662Svikram *physarray = s_calloc(*n, sizeof (char *)); 7649eb2bd662Svikram for (i = 0, lp = startlp; lp; lp = lp->next) { 7650eb2bd662Svikram if (lp->line == NULL) 7651eb2bd662Svikram continue; 7652eb2bd662Svikram if (strcmp(lp->line, "mirror") == 0) 7653eb2bd662Svikram continue; 7654eb2bd662Svikram if (strcmp(lp->line, "errors:") == 0) 7655eb2bd662Svikram break; 7656eb2bd662Svikram if (strncmp(lp->line, "/dev/dsk/", strlen("/dev/dsk/")) != 0 && 7657eb2bd662Svikram strncmp(lp->line, "/dev/rdsk/", 7658eb2bd662Svikram strlen("/dev/rdsk/")) != 0) { 7659e998e519SSheshadri Vasudevan (void) snprintf(dsk, sizeof (dsk), "/dev/rdsk/%s", 7660eb2bd662Svikram lp->line); 7661eb2bd662Svikram } else { 7662eb2bd662Svikram (void) strlcpy(dsk, lp->line, sizeof (dsk)); 7663eb2bd662Svikram } 7664eb2bd662Svikram BAM_DPRINTF((D_ADDING_ZFS_PHYS, fcn, dsk, pool)); 7665eb2bd662Svikram (*physarray)[i++] = s_strdup(dsk); 7666eb2bd662Svikram } 7667eb2bd662Svikram 7668eb2bd662Svikram assert(i == *n); 7669eb2bd662Svikram 7670eb2bd662Svikram filelist_free(&flist); 7671eb2bd662Svikram 7672eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7673eb2bd662Svikram return (0); 7674eb2bd662Svikram } 7675eb2bd662Svikram 7676eb2bd662Svikram static int 7677eb2bd662Svikram get_physical(char *menu_root, char ***physarray, int *n) 7678eb2bd662Svikram { 7679eb2bd662Svikram char *special; 7680eb2bd662Svikram int ret; 7681eb2bd662Svikram const char *fcn = "get_physical()"; 7682eb2bd662Svikram 7683eb2bd662Svikram assert(menu_root); 7684eb2bd662Svikram assert(physarray); 7685eb2bd662Svikram assert(n); 7686eb2bd662Svikram 7687eb2bd662Svikram *physarray = NULL; 7688eb2bd662Svikram *n = 0; 7689eb2bd662Svikram 7690eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_root)); 7691eb2bd662Svikram 7692eb2bd662Svikram /* First get the device special file from /etc/mnttab */ 7693eb2bd662Svikram special = get_special(menu_root); 7694eb2bd662Svikram INJECT_ERROR1("GET_PHYSICAL_SPECIAL", special = NULL); 7695eb2bd662Svikram if (special == NULL) { 7696eb2bd662Svikram bam_error(GET_SPECIAL_NULL, menu_root); 7697eb2bd662Svikram return (-1); 7698eb2bd662Svikram } 7699eb2bd662Svikram 7700eb2bd662Svikram /* If already a physical device nothing to do */ 7701eb2bd662Svikram if (strncmp(special, "/dev/dsk/", strlen("/dev/dsk/")) == 0 || 7702eb2bd662Svikram strncmp(special, "/dev/rdsk/", strlen("/dev/rdsk/")) == 0) { 7703eb2bd662Svikram BAM_DPRINTF((D_GET_PHYSICAL_ALREADY, fcn, menu_root, special)); 7704eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7705eb2bd662Svikram *physarray = s_calloc(1, sizeof (char *)); 7706eb2bd662Svikram (*physarray)[0] = special; 7707eb2bd662Svikram *n = 1; 7708eb2bd662Svikram return (0); 7709eb2bd662Svikram } 7710eb2bd662Svikram 7711eb2bd662Svikram if (is_zfs(menu_root)) { 7712eb2bd662Svikram ret = zfs_get_physical(special, physarray, n); 7713eb2bd662Svikram } else { 7714eb2bd662Svikram bam_error(GET_PHYSICAL_NOTSUP_FSTYPE, menu_root, special); 7715eb2bd662Svikram ret = -1; 7716eb2bd662Svikram } 7717eb2bd662Svikram 7718eb2bd662Svikram free(special); 7719eb2bd662Svikram 7720eb2bd662Svikram INJECT_ERROR1("GET_PHYSICAL_RET", ret = -1); 7721eb2bd662Svikram if (ret == -1) { 7722eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 7723eb2bd662Svikram } else { 7724eb2bd662Svikram int i; 7725eb2bd662Svikram assert (*n > 0); 7726eb2bd662Svikram for (i = 0; i < *n; i++) { 7727eb2bd662Svikram BAM_DPRINTF((D_GET_PHYSICAL_RET, fcn, (*physarray)[i])); 7728eb2bd662Svikram } 7729eb2bd662Svikram } 7730eb2bd662Svikram 7731eb2bd662Svikram return (ret); 7732eb2bd662Svikram } 7733eb2bd662Svikram 7734eb2bd662Svikram static int 7735eb2bd662Svikram is_bootdisk(char *osroot, char *physical) 7736eb2bd662Svikram { 7737eb2bd662Svikram int ret; 7738eb2bd662Svikram char *grubroot; 7739eb2bd662Svikram char *bootp; 7740eb2bd662Svikram const char *fcn = "is_bootdisk()"; 7741eb2bd662Svikram 7742eb2bd662Svikram assert(osroot); 7743eb2bd662Svikram assert(physical); 7744eb2bd662Svikram 7745eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, physical)); 7746eb2bd662Svikram 7747eb2bd662Svikram bootp = strstr(physical, "p0:boot"); 7748eb2bd662Svikram if (bootp) 7749eb2bd662Svikram *bootp = '\0'; 7750eb2bd662Svikram /* 7751eb2bd662Svikram * We just want the BIOS mapping for menu disk. 7752eb2bd662Svikram * Don't pass menu_root to get_grubroot() as the 7753eb2bd662Svikram * check that it is used for is not relevant here. 7754eb2bd662Svikram * The osroot is immaterial as well - it is only used to 7755eb2bd662Svikram * to find create_diskmap script. Everything hinges on 7756eb2bd662Svikram * "physical" 7757eb2bd662Svikram */ 7758eb2bd662Svikram grubroot = get_grubroot(osroot, physical, NULL); 7759eb2bd662Svikram 7760eb2bd662Svikram INJECT_ERROR1("IS_BOOTDISK_GRUBROOT", grubroot = NULL); 7761eb2bd662Svikram if (grubroot == NULL) { 776237eb779cSVikram Hegde if (bam_verbose) 776337eb779cSVikram Hegde bam_error(NO_GRUBROOT_FOR_DISK, physical); 7764eb2bd662Svikram return (0); 7765eb2bd662Svikram } 7766eb2bd662Svikram ret = grubroot[3] == '0'; 7767eb2bd662Svikram free(grubroot); 7768eb2bd662Svikram 7769eb2bd662Svikram BAM_DPRINTF((D_RETURN_RET, fcn, ret)); 7770eb2bd662Svikram 7771eb2bd662Svikram return (ret); 7772eb2bd662Svikram } 7773eb2bd662Svikram 7774eb2bd662Svikram /* 7775eb2bd662Svikram * Check if menu is on the boot device 77767c478bd9Sstevel@tonic-gate * Return 0 (false) on error 77777c478bd9Sstevel@tonic-gate */ 77787c478bd9Sstevel@tonic-gate static int 7779eb2bd662Svikram menu_on_bootdisk(char *osroot, char *menu_root) 77807c478bd9Sstevel@tonic-gate { 7781eb2bd662Svikram char **physarray; 77827c478bd9Sstevel@tonic-gate int ret; 7783eb2bd662Svikram int n; 7784eb2bd662Svikram int i; 7785eb2bd662Svikram int on_bootdisk; 7786eb2bd662Svikram const char *fcn = "menu_on_bootdisk()"; 77877c478bd9Sstevel@tonic-gate 7788eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root)); 77897c478bd9Sstevel@tonic-gate 7790eb2bd662Svikram ret = get_physical(menu_root, &physarray, &n); 7791eb2bd662Svikram INJECT_ERROR1("MENU_ON_BOOTDISK_PHYSICAL", ret = -1); 7792eb2bd662Svikram if (ret != 0) { 7793eb2bd662Svikram bam_error(GET_PHYSICAL_MENU_NULL, menu_root); 77947c478bd9Sstevel@tonic-gate return (0); 7795eb2bd662Svikram } 7796eb2bd662Svikram 7797eb2bd662Svikram assert(physarray); 7798eb2bd662Svikram assert(n > 0); 7799eb2bd662Svikram 7800eb2bd662Svikram on_bootdisk = 0; 7801eb2bd662Svikram for (i = 0; i < n; i++) { 7802eb2bd662Svikram assert(strncmp(physarray[i], "/dev/dsk/", 7803eb2bd662Svikram strlen("/dev/dsk/")) == 0 || 7804eb2bd662Svikram strncmp(physarray[i], "/dev/rdsk/", 7805eb2bd662Svikram strlen("/dev/rdsk/")) == 0); 7806eb2bd662Svikram 7807eb2bd662Svikram BAM_DPRINTF((D_CHECK_ON_BOOTDISK, fcn, physarray[i])); 7808eb2bd662Svikram if (is_bootdisk(osroot, physarray[i])) { 7809eb2bd662Svikram on_bootdisk = 1; 7810eb2bd662Svikram BAM_DPRINTF((D_IS_ON_BOOTDISK, fcn, physarray[i])); 7811eb2bd662Svikram } 7812eb2bd662Svikram } 7813eb2bd662Svikram 7814eb2bd662Svikram free_physarray(physarray, n); 7815eb2bd662Svikram 7816eb2bd662Svikram INJECT_ERROR1("ON_BOOTDISK_YES", on_bootdisk = 1); 7817eb2bd662Svikram INJECT_ERROR1("ON_BOOTDISK_NO", on_bootdisk = 0); 7818eb2bd662Svikram if (on_bootdisk) { 7819eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 7820eb2bd662Svikram } else { 7821eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 7822eb2bd662Svikram } 7823eb2bd662Svikram 7824eb2bd662Svikram return (on_bootdisk); 7825eb2bd662Svikram } 7826eb2bd662Svikram 7827eb2bd662Svikram void 7828eb2bd662Svikram bam_add_line(menu_t *mp, entry_t *entry, line_t *prev, line_t *lp) 7829eb2bd662Svikram { 7830eb2bd662Svikram const char *fcn = "bam_add_line()"; 7831eb2bd662Svikram 7832eb2bd662Svikram assert(mp); 7833eb2bd662Svikram assert(entry); 7834eb2bd662Svikram assert(prev); 7835eb2bd662Svikram assert(lp); 7836eb2bd662Svikram 7837eb2bd662Svikram lp->next = prev->next; 7838eb2bd662Svikram if (prev->next) { 7839eb2bd662Svikram BAM_DPRINTF((D_ADD_LINE_PREV_NEXT, fcn)); 7840eb2bd662Svikram prev->next->prev = lp; 7841eb2bd662Svikram } else { 7842eb2bd662Svikram BAM_DPRINTF((D_ADD_LINE_NOT_PREV_NEXT, fcn)); 7843eb2bd662Svikram } 7844eb2bd662Svikram prev->next = lp; 7845eb2bd662Svikram lp->prev = prev; 7846eb2bd662Svikram 7847eb2bd662Svikram if (entry->end == prev) { 7848eb2bd662Svikram BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_ENTRY, fcn)); 7849eb2bd662Svikram entry->end = lp; 7850eb2bd662Svikram } 7851eb2bd662Svikram if (mp->end == prev) { 7852eb2bd662Svikram assert(lp->next == NULL); 7853eb2bd662Svikram mp->end = lp; 7854eb2bd662Svikram BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_MENU, fcn)); 7855eb2bd662Svikram } 78567c478bd9Sstevel@tonic-gate } 78577c478bd9Sstevel@tonic-gate 78588c1b6884Sszhou /* 78598c1b6884Sszhou * look for matching bootadm entry with specified parameters 78608c1b6884Sszhou * Here are the rules (based on existing usage): 78618c1b6884Sszhou * - If title is specified, match on title only 7862eb2bd662Svikram * - Else, match on root/findroot, kernel, and module. 7863eb2bd662Svikram * Note that, if root_opt is non-zero, the absence of 7864eb2bd662Svikram * root line is considered a match. 78658c1b6884Sszhou */ 78668c1b6884Sszhou static entry_t * 7867eb2bd662Svikram find_boot_entry( 7868eb2bd662Svikram menu_t *mp, 7869eb2bd662Svikram char *title, 7870eb2bd662Svikram char *kernel, 7871eb2bd662Svikram char *findroot, 7872eb2bd662Svikram char *root, 7873eb2bd662Svikram char *module, 7874eb2bd662Svikram int root_opt, 7875eb2bd662Svikram int *entry_num) 78768c1b6884Sszhou { 78778c1b6884Sszhou int i; 78788c1b6884Sszhou line_t *lp; 78798c1b6884Sszhou entry_t *ent; 7880eb2bd662Svikram const char *fcn = "find_boot_entry()"; 7881eb2bd662Svikram 7882eb2bd662Svikram if (entry_num) 7883eb2bd662Svikram *entry_num = BAM_ERROR; 78848c1b6884Sszhou 78858c1b6884Sszhou /* find matching entry */ 78868c1b6884Sszhou for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) { 78878c1b6884Sszhou lp = ent->start; 78888c1b6884Sszhou 78898c1b6884Sszhou /* first line of entry must be bootadm comment */ 78908c1b6884Sszhou lp = ent->start; 7891ae115bc7Smrj if (lp->flags != BAM_COMMENT || 7892ae115bc7Smrj strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) { 78938c1b6884Sszhou continue; 78948c1b6884Sszhou } 78958c1b6884Sszhou 78968c1b6884Sszhou /* advance to title line */ 78978c1b6884Sszhou lp = lp->next; 78988c1b6884Sszhou if (title) { 78998c1b6884Sszhou if (lp->flags == BAM_TITLE && lp->arg && 7900eb2bd662Svikram strcmp(lp->arg, title) == 0) { 7901eb2bd662Svikram BAM_DPRINTF((D_MATCHED_TITLE, fcn, title)); 79028c1b6884Sszhou break; 7903eb2bd662Svikram } 7904eb2bd662Svikram BAM_DPRINTF((D_NOMATCH_TITLE, fcn, title, lp->arg)); 79058c1b6884Sszhou continue; /* check title only */ 79068c1b6884Sszhou } 79078c1b6884Sszhou 79088c1b6884Sszhou lp = lp->next; /* advance to root line */ 7909843e1988Sjohnlev if (lp == NULL) { 7910843e1988Sjohnlev continue; 7911eac223ccSWilliam Kucharski } else if (lp->cmd != NULL && 7912eac223ccSWilliam Kucharski strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) == 0) { 7913eb2bd662Svikram INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_FINDROOT", 7914eb2bd662Svikram findroot = NULL); 7915eb2bd662Svikram if (findroot == NULL) { 7916eb2bd662Svikram BAM_DPRINTF((D_NOMATCH_FINDROOT_NULL, 7917eb2bd662Svikram fcn, lp->arg)); 79188c1b6884Sszhou continue; 79198c1b6884Sszhou } 7920eb2bd662Svikram /* findroot command found, try match */ 7921eb2bd662Svikram if (strcmp(lp->arg, findroot) != 0) { 7922eb2bd662Svikram BAM_DPRINTF((D_NOMATCH_FINDROOT, 7923eb2bd662Svikram fcn, findroot, lp->arg)); 7924eb2bd662Svikram continue; 7925eb2bd662Svikram } 7926eb2bd662Svikram BAM_DPRINTF((D_MATCHED_FINDROOT, fcn, findroot)); 7927eb2bd662Svikram lp = lp->next; /* advance to kernel line */ 7928eac223ccSWilliam Kucharski } else if (lp->cmd != NULL && 7929eac223ccSWilliam Kucharski strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) { 7930eb2bd662Svikram INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_ROOT", root = NULL); 7931eb2bd662Svikram if (root == NULL) { 7932eb2bd662Svikram BAM_DPRINTF((D_NOMATCH_ROOT_NULL, 7933eb2bd662Svikram fcn, lp->arg)); 7934eb2bd662Svikram continue; 7935eb2bd662Svikram } 7936eb2bd662Svikram /* root cmd found, try match */ 7937eb2bd662Svikram if (strcmp(lp->arg, root) != 0) { 7938eb2bd662Svikram BAM_DPRINTF((D_NOMATCH_ROOT, 7939eb2bd662Svikram fcn, root, lp->arg)); 7940eb2bd662Svikram continue; 7941eb2bd662Svikram } 7942eb2bd662Svikram BAM_DPRINTF((D_MATCHED_ROOT, fcn, root)); 79438c1b6884Sszhou lp = lp->next; /* advance to kernel line */ 79448c1b6884Sszhou } else { 7945eb2bd662Svikram INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_NO", 7946eb2bd662Svikram root_opt = 0); 7947eb2bd662Svikram INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_YES", 7948eb2bd662Svikram root_opt = 1); 79498c1b6884Sszhou /* no root command, see if root is optional */ 79508c1b6884Sszhou if (root_opt == 0) { 7951eb2bd662Svikram BAM_DPRINTF((D_NO_ROOT_OPT, fcn)); 79528c1b6884Sszhou continue; 79538c1b6884Sszhou } 7954eb2bd662Svikram BAM_DPRINTF((D_ROOT_OPT, fcn)); 79558c1b6884Sszhou } 79568c1b6884Sszhou 79578c1b6884Sszhou if (lp == NULL || lp->next == NULL) { 79588c1b6884Sszhou continue; 79598c1b6884Sszhou } 79608c1b6884Sszhou 7961843e1988Sjohnlev if (kernel && 7962843e1988Sjohnlev (!check_cmd(lp->cmd, KERNEL_CMD, lp->arg, kernel))) { 7963bbcc54bdSEnrico Perla - Sun Microsystems if (!(ent->flags & BAM_ENTRY_FAILSAFE) || 7964bbcc54bdSEnrico Perla - Sun Microsystems !(ent->flags & BAM_ENTRY_DBOOT) || 7965bbcc54bdSEnrico Perla - Sun Microsystems strcmp(kernel, DIRECT_BOOT_FAILSAFE_LINE) != 0) 79668c1b6884Sszhou continue; 7967bbcc54bdSEnrico Perla - Sun Microsystems 7968bbcc54bdSEnrico Perla - Sun Microsystems ent->flags |= BAM_ENTRY_UPGFSKERNEL; 7969bbcc54bdSEnrico Perla - Sun Microsystems 79708c1b6884Sszhou } 7971eb2bd662Svikram BAM_DPRINTF((D_KERNEL_MATCH, fcn, kernel, lp->arg)); 7972843e1988Sjohnlev 7973843e1988Sjohnlev /* 7974843e1988Sjohnlev * Check for matching module entry (failsafe or normal). 7975843e1988Sjohnlev * If it fails to match, we go around the loop again. 7976843e1988Sjohnlev * For xpv entries, there are two module lines, so we 7977843e1988Sjohnlev * do the check twice. 7978843e1988Sjohnlev */ 7979843e1988Sjohnlev lp = lp->next; /* advance to module line */ 7980843e1988Sjohnlev if (check_cmd(lp->cmd, MODULE_CMD, lp->arg, module) || 7981843e1988Sjohnlev (((lp = lp->next) != NULL) && 7982843e1988Sjohnlev check_cmd(lp->cmd, MODULE_CMD, lp->arg, module))) { 7983843e1988Sjohnlev /* match found */ 7984eb2bd662Svikram BAM_DPRINTF((D_MODULE_MATCH, fcn, module, lp->arg)); 7985843e1988Sjohnlev break; 7986843e1988Sjohnlev } 7987bbcc54bdSEnrico Perla - Sun Microsystems 7988bbcc54bdSEnrico Perla - Sun Microsystems if (strcmp(module, FAILSAFE_ARCHIVE) == 0 && 7989bbcc54bdSEnrico Perla - Sun Microsystems (strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_32) == 0 || 7990bbcc54bdSEnrico Perla - Sun Microsystems strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_64) == 0)) { 7991bbcc54bdSEnrico Perla - Sun Microsystems ent->flags |= BAM_ENTRY_UPGFSMODULE; 7992bbcc54bdSEnrico Perla - Sun Microsystems break; 7993bbcc54bdSEnrico Perla - Sun Microsystems } 7994bbcc54bdSEnrico Perla - Sun Microsystems 79958c1b6884Sszhou } 79968c1b6884Sszhou 7997eb2bd662Svikram if (ent && entry_num) { 79988c1b6884Sszhou *entry_num = i; 7999843e1988Sjohnlev } 8000eb2bd662Svikram 8001eb2bd662Svikram if (ent) { 8002eb2bd662Svikram BAM_DPRINTF((D_RETURN_RET, fcn, i)); 8003eb2bd662Svikram } else { 8004eb2bd662Svikram BAM_DPRINTF((D_RETURN_RET, fcn, BAM_ERROR)); 8005eb2bd662Svikram } 80068c1b6884Sszhou return (ent); 80078c1b6884Sszhou } 80088c1b6884Sszhou 80098c1b6884Sszhou static int 8010eb2bd662Svikram update_boot_entry(menu_t *mp, char *title, char *findroot, char *root, 8011eb2bd662Svikram char *kernel, char *mod_kernel, char *module, int root_opt) 80128c1b6884Sszhou { 8013eb2bd662Svikram int i; 8014eb2bd662Svikram int change_kernel = 0; 80158c1b6884Sszhou entry_t *ent; 80168c1b6884Sszhou line_t *lp; 8017eb2bd662Svikram line_t *tlp; 80188c1b6884Sszhou char linebuf[BAM_MAXLINE]; 8019eb2bd662Svikram const char *fcn = "update_boot_entry()"; 80208c1b6884Sszhou 80218c1b6884Sszhou /* note: don't match on title, it's updated on upgrade */ 8022eb2bd662Svikram ent = find_boot_entry(mp, NULL, kernel, findroot, root, module, 8023eb2bd662Svikram root_opt, &i); 8024ae115bc7Smrj if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) { 8025ae115bc7Smrj /* 8026ae115bc7Smrj * We may be upgrading a kernel from multiboot to 8027eb2bd662Svikram * directboot. Look for a multiboot entry. A multiboot 8028eb2bd662Svikram * entry will not have a findroot line. 8029ae115bc7Smrj */ 8030eb2bd662Svikram ent = find_boot_entry(mp, NULL, "multiboot", NULL, root, 8031eb2bd662Svikram MULTIBOOT_ARCHIVE, root_opt, &i); 8032ae115bc7Smrj if (ent != NULL) { 8033eb2bd662Svikram BAM_DPRINTF((D_UPGRADE_FROM_MULTIBOOT, fcn, root)); 8034ae115bc7Smrj change_kernel = 1; 8035ae115bc7Smrj } 8036eb2bd662Svikram } else if (ent) { 8037eb2bd662Svikram BAM_DPRINTF((D_FOUND_FINDROOT, fcn, findroot)); 8038ae115bc7Smrj } 80398c1b6884Sszhou 8040eb2bd662Svikram if (ent == NULL) { 8041eb2bd662Svikram BAM_DPRINTF((D_ENTRY_NOT_FOUND_CREATING, fcn, findroot)); 8042eb2bd662Svikram return (add_boot_entry(mp, title, findroot, 804344da779fSWilliam Kucharski kernel, mod_kernel, module, NULL)); 8044eb2bd662Svikram } 8045eb2bd662Svikram 8046eb2bd662Svikram /* replace title of existing entry and update findroot line */ 80478c1b6884Sszhou lp = ent->start; 80488c1b6884Sszhou lp = lp->next; /* title line */ 80498c1b6884Sszhou (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 80508c1b6884Sszhou menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 80518c1b6884Sszhou free(lp->arg); 80528c1b6884Sszhou free(lp->line); 80538c1b6884Sszhou lp->arg = s_strdup(title); 80548c1b6884Sszhou lp->line = s_strdup(linebuf); 8055eb2bd662Svikram BAM_DPRINTF((D_CHANGING_TITLE, fcn, title)); 80568c1b6884Sszhou 8057eb2bd662Svikram tlp = lp; /* title line */ 80588c1b6884Sszhou lp = lp->next; /* root line */ 8059eb2bd662Svikram 8060eb2bd662Svikram /* if no root or findroot command, create a new line_t */ 8061eac223ccSWilliam Kucharski if ((lp->cmd != NULL) && (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) != 0 && 8062eac223ccSWilliam Kucharski strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) != 0)) { 8063eb2bd662Svikram lp = s_calloc(1, sizeof (line_t)); 8064eb2bd662Svikram bam_add_line(mp, ent, tlp, lp); 8065eb2bd662Svikram } else { 8066eac223ccSWilliam Kucharski if (lp->cmd != NULL) 8067eb2bd662Svikram free(lp->cmd); 8068eac223ccSWilliam Kucharski 8069eb2bd662Svikram free(lp->sep); 8070eb2bd662Svikram free(lp->arg); 8071eb2bd662Svikram free(lp->line); 80728c1b6884Sszhou } 8073ae115bc7Smrj 8074eb2bd662Svikram lp->cmd = s_strdup(menu_cmds[FINDROOT_CMD]); 8075eb2bd662Svikram lp->sep = s_strdup(menu_cmds[SEP_CMD]); 8076eb2bd662Svikram lp->arg = s_strdup(findroot); 8077eb2bd662Svikram (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 8078eb2bd662Svikram menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot); 8079eb2bd662Svikram lp->line = s_strdup(linebuf); 8080eb2bd662Svikram BAM_DPRINTF((D_ADDING_FINDROOT_LINE, fcn, findroot)); 8081eb2bd662Svikram 8082eb2bd662Svikram /* kernel line */ 8083eb2bd662Svikram lp = lp->next; 8084eb2bd662Svikram 8085bbcc54bdSEnrico Perla - Sun Microsystems if (ent->flags & BAM_ENTRY_UPGFSKERNEL) { 8086bbcc54bdSEnrico Perla - Sun Microsystems char *params = NULL; 8087bbcc54bdSEnrico Perla - Sun Microsystems 8088bbcc54bdSEnrico Perla - Sun Microsystems params = strstr(lp->line, "-s"); 8089bbcc54bdSEnrico Perla - Sun Microsystems if (params != NULL) 8090bbcc54bdSEnrico Perla - Sun Microsystems (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s%s", 8091bbcc54bdSEnrico Perla - Sun Microsystems menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD], 8092bbcc54bdSEnrico Perla - Sun Microsystems kernel, params+2); 8093bbcc54bdSEnrico Perla - Sun Microsystems else 8094bbcc54bdSEnrico Perla - Sun Microsystems (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 8095bbcc54bdSEnrico Perla - Sun Microsystems menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD], 8096bbcc54bdSEnrico Perla - Sun Microsystems kernel); 8097bbcc54bdSEnrico Perla - Sun Microsystems 8098eac223ccSWilliam Kucharski if (lp->cmd != NULL) 8099bbcc54bdSEnrico Perla - Sun Microsystems free(lp->cmd); 8100eac223ccSWilliam Kucharski 8101bbcc54bdSEnrico Perla - Sun Microsystems free(lp->arg); 8102bbcc54bdSEnrico Perla - Sun Microsystems free(lp->line); 8103bbcc54bdSEnrico Perla - Sun Microsystems lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]); 8104bbcc54bdSEnrico Perla - Sun Microsystems lp->arg = s_strdup(strstr(linebuf, "/")); 8105bbcc54bdSEnrico Perla - Sun Microsystems lp->line = s_strdup(linebuf); 8106bbcc54bdSEnrico Perla - Sun Microsystems ent->flags &= ~BAM_ENTRY_UPGFSKERNEL; 8107bbcc54bdSEnrico Perla - Sun Microsystems BAM_DPRINTF((D_ADDING_KERNEL_DOLLAR, fcn, lp->prev->cmd)); 8108bbcc54bdSEnrico Perla - Sun Microsystems } 8109bbcc54bdSEnrico Perla - Sun Microsystems 8110ae115bc7Smrj if (change_kernel) { 8111ae115bc7Smrj /* 8112ae115bc7Smrj * We're upgrading from multiboot to directboot. 8113ae115bc7Smrj */ 8114eac223ccSWilliam Kucharski if (lp->cmd != NULL && 8115eac223ccSWilliam Kucharski strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) { 8116ae115bc7Smrj (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 8117ae115bc7Smrj menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD], 8118ae115bc7Smrj kernel); 8119eb2bd662Svikram free(lp->cmd); 8120ae115bc7Smrj free(lp->arg); 8121ae115bc7Smrj free(lp->line); 8122eb2bd662Svikram lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]); 8123ae115bc7Smrj lp->arg = s_strdup(kernel); 8124ae115bc7Smrj lp->line = s_strdup(linebuf); 8125ae115bc7Smrj lp = lp->next; 8126eb2bd662Svikram BAM_DPRINTF((D_ADDING_KERNEL_DOLLAR, fcn, kernel)); 8127ae115bc7Smrj } 8128eac223ccSWilliam Kucharski if (lp->cmd != NULL && 8129eac223ccSWilliam Kucharski strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) { 8130ae115bc7Smrj (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 8131ae115bc7Smrj menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD], 8132ae115bc7Smrj module); 8133eb2bd662Svikram free(lp->cmd); 8134ae115bc7Smrj free(lp->arg); 8135ae115bc7Smrj free(lp->line); 8136eb2bd662Svikram lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]); 8137ae115bc7Smrj lp->arg = s_strdup(module); 8138ae115bc7Smrj lp->line = s_strdup(linebuf); 8139ae115bc7Smrj lp = lp->next; 8140eb2bd662Svikram BAM_DPRINTF((D_ADDING_MODULE_DOLLAR, fcn, module)); 8141ae115bc7Smrj } 8142ae115bc7Smrj } 8143bbcc54bdSEnrico Perla - Sun Microsystems 8144bbcc54bdSEnrico Perla - Sun Microsystems /* module line */ 8145bbcc54bdSEnrico Perla - Sun Microsystems lp = lp->next; 8146bbcc54bdSEnrico Perla - Sun Microsystems 8147bbcc54bdSEnrico Perla - Sun Microsystems if (ent->flags & BAM_ENTRY_UPGFSMODULE) { 8148eac223ccSWilliam Kucharski if (lp->cmd != NULL && 8149eac223ccSWilliam Kucharski strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) { 8150bbcc54bdSEnrico Perla - Sun Microsystems (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 8151bbcc54bdSEnrico Perla - Sun Microsystems menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD], 8152bbcc54bdSEnrico Perla - Sun Microsystems module); 8153bbcc54bdSEnrico Perla - Sun Microsystems free(lp->cmd); 8154bbcc54bdSEnrico Perla - Sun Microsystems free(lp->arg); 8155bbcc54bdSEnrico Perla - Sun Microsystems free(lp->line); 8156bbcc54bdSEnrico Perla - Sun Microsystems lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]); 8157bbcc54bdSEnrico Perla - Sun Microsystems lp->arg = s_strdup(module); 8158bbcc54bdSEnrico Perla - Sun Microsystems lp->line = s_strdup(linebuf); 8159bbcc54bdSEnrico Perla - Sun Microsystems lp = lp->next; 8160bbcc54bdSEnrico Perla - Sun Microsystems ent->flags &= ~BAM_ENTRY_UPGFSMODULE; 8161bbcc54bdSEnrico Perla - Sun Microsystems BAM_DPRINTF((D_ADDING_MODULE_DOLLAR, fcn, module)); 8162bbcc54bdSEnrico Perla - Sun Microsystems } 8163bbcc54bdSEnrico Perla - Sun Microsystems } 8164bbcc54bdSEnrico Perla - Sun Microsystems 8165eb2bd662Svikram BAM_DPRINTF((D_RETURN_RET, fcn, i)); 81668c1b6884Sszhou return (i); 81678c1b6884Sszhou } 81688c1b6884Sszhou 8169eb2bd662Svikram int 8170eb2bd662Svikram root_optional(char *osroot, char *menu_root) 8171eb2bd662Svikram { 8172eb2bd662Svikram char *ospecial; 8173eb2bd662Svikram char *mspecial; 8174eb2bd662Svikram char *slash; 8175eb2bd662Svikram int root_opt; 8176eb2bd662Svikram int ret1; 8177eb2bd662Svikram int ret2; 8178eb2bd662Svikram const char *fcn = "root_optional()"; 8179eb2bd662Svikram 8180eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root)); 8181eb2bd662Svikram 8182eb2bd662Svikram /* 8183eb2bd662Svikram * For all filesystems except ZFS, a straight compare of osroot 8184eb2bd662Svikram * and menu_root will tell us if root is optional. 8185eb2bd662Svikram * For ZFS, the situation is complicated by the fact that 8186eb2bd662Svikram * menu_root and osroot are always different 8187eb2bd662Svikram */ 8188eb2bd662Svikram ret1 = is_zfs(osroot); 8189eb2bd662Svikram ret2 = is_zfs(menu_root); 8190eb2bd662Svikram INJECT_ERROR1("ROOT_OPT_NOT_ZFS", ret1 = 0); 8191eb2bd662Svikram if (!ret1 || !ret2) { 8192eb2bd662Svikram BAM_DPRINTF((D_ROOT_OPT_NOT_ZFS, fcn, osroot, menu_root)); 8193eb2bd662Svikram root_opt = (strcmp(osroot, menu_root) == 0); 8194eb2bd662Svikram goto out; 8195eb2bd662Svikram } 8196eb2bd662Svikram 8197eb2bd662Svikram ospecial = get_special(osroot); 8198eb2bd662Svikram INJECT_ERROR1("ROOT_OPTIONAL_OSPECIAL", ospecial = NULL); 8199eb2bd662Svikram if (ospecial == NULL) { 8200eb2bd662Svikram bam_error(GET_OSROOT_SPECIAL_ERR, osroot); 8201eb2bd662Svikram return (0); 8202eb2bd662Svikram } 8203eb2bd662Svikram BAM_DPRINTF((D_ROOT_OPTIONAL_OSPECIAL, fcn, ospecial, osroot)); 8204eb2bd662Svikram 8205eb2bd662Svikram mspecial = get_special(menu_root); 8206eb2bd662Svikram INJECT_ERROR1("ROOT_OPTIONAL_MSPECIAL", mspecial = NULL); 8207eb2bd662Svikram if (mspecial == NULL) { 8208eb2bd662Svikram bam_error(GET_MENU_ROOT_SPECIAL_ERR, menu_root); 8209eb2bd662Svikram free(ospecial); 8210eb2bd662Svikram return (0); 8211eb2bd662Svikram } 8212eb2bd662Svikram BAM_DPRINTF((D_ROOT_OPTIONAL_MSPECIAL, fcn, mspecial, menu_root)); 8213eb2bd662Svikram 8214eb2bd662Svikram slash = strchr(ospecial, '/'); 8215eb2bd662Svikram if (slash) 8216eb2bd662Svikram *slash = '\0'; 8217eb2bd662Svikram BAM_DPRINTF((D_ROOT_OPTIONAL_FIXED_OSPECIAL, fcn, ospecial, osroot)); 8218eb2bd662Svikram 8219eb2bd662Svikram root_opt = (strcmp(ospecial, mspecial) == 0); 8220eb2bd662Svikram 8221eb2bd662Svikram free(ospecial); 8222eb2bd662Svikram free(mspecial); 8223eb2bd662Svikram 8224eb2bd662Svikram out: 8225eb2bd662Svikram INJECT_ERROR1("ROOT_OPTIONAL_NO", root_opt = 0); 8226eb2bd662Svikram INJECT_ERROR1("ROOT_OPTIONAL_YES", root_opt = 1); 8227eb2bd662Svikram if (root_opt) { 8228eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 8229eb2bd662Svikram } else { 8230eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 8231eb2bd662Svikram } 8232eb2bd662Svikram 8233eb2bd662Svikram return (root_opt); 8234eb2bd662Svikram } 8235eb2bd662Svikram 82367c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 82377c478bd9Sstevel@tonic-gate static error_t 8238eb2bd662Svikram update_entry(menu_t *mp, char *menu_root, char *osdev) 82397c478bd9Sstevel@tonic-gate { 82407c478bd9Sstevel@tonic-gate int entry; 8241eb2bd662Svikram char *grubsign; 8242eb2bd662Svikram char *grubroot; 8243eb2bd662Svikram char *title; 8244eb2bd662Svikram char osroot[PATH_MAX]; 8245eb2bd662Svikram char *failsafe_kernel = NULL; 82468c1b6884Sszhou struct stat sbuf; 82478c1b6884Sszhou char failsafe[256]; 8248bbcc54bdSEnrico Perla - Sun Microsystems char failsafe_64[256]; 8249eb2bd662Svikram int ret; 8250eb2bd662Svikram const char *fcn = "update_entry()"; 82517c478bd9Sstevel@tonic-gate 82527c478bd9Sstevel@tonic-gate assert(mp); 8253eb2bd662Svikram assert(menu_root); 8254eb2bd662Svikram assert(osdev); 8255eb2bd662Svikram assert(bam_root); 82567c478bd9Sstevel@tonic-gate 8257eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY3, fcn, menu_root, osdev, bam_root)); 8258eb2bd662Svikram 8259eb2bd662Svikram (void) strlcpy(osroot, bam_root, sizeof (osroot)); 8260eb2bd662Svikram 82617c478bd9Sstevel@tonic-gate title = get_title(osroot); 8262eb2bd662Svikram assert(title); 82637c478bd9Sstevel@tonic-gate 8264eb2bd662Svikram grubsign = get_grubsign(osroot, osdev); 8265eb2bd662Svikram INJECT_ERROR1("GET_GRUBSIGN_FAIL", grubsign = NULL); 8266eb2bd662Svikram if (grubsign == NULL) { 8267eb2bd662Svikram bam_error(GET_GRUBSIGN_ERROR, osroot, osdev); 82687c478bd9Sstevel@tonic-gate return (BAM_ERROR); 82697c478bd9Sstevel@tonic-gate } 8270eb2bd662Svikram 8271eb2bd662Svikram /* 8272eb2bd662Svikram * It is not a fatal error if get_grubroot() fails 8273eb2bd662Svikram * We no longer rely on biosdev to populate the 8274eb2bd662Svikram * menu 8275eb2bd662Svikram */ 8276eb2bd662Svikram grubroot = get_grubroot(osroot, osdev, menu_root); 8277eb2bd662Svikram INJECT_ERROR1("GET_GRUBROOT_FAIL", grubroot = NULL); 8278eb2bd662Svikram if (grubroot) { 8279eb2bd662Svikram BAM_DPRINTF((D_GET_GRUBROOT_SUCCESS, 8280eb2bd662Svikram fcn, osroot, osdev, menu_root)); 8281eb2bd662Svikram } else { 8282eb2bd662Svikram BAM_DPRINTF((D_GET_GRUBROOT_FAILURE, 8283eb2bd662Svikram fcn, osroot, osdev, menu_root)); 82847c478bd9Sstevel@tonic-gate } 82857c478bd9Sstevel@tonic-gate 82867c478bd9Sstevel@tonic-gate /* add the entry for normal Solaris */ 8287eb2bd662Svikram INJECT_ERROR1("UPDATE_ENTRY_MULTIBOOT", 8288eb2bd662Svikram bam_direct = BAM_DIRECT_MULTIBOOT); 8289ae115bc7Smrj if (bam_direct == BAM_DIRECT_DBOOT) { 8290eb2bd662Svikram entry = update_boot_entry(mp, title, grubsign, grubroot, 8291e7cbe64fSgw25295 (bam_zfs ? DIRECT_BOOT_KERNEL_ZFS : DIRECT_BOOT_KERNEL), 8292eb2bd662Svikram NULL, DIRECT_BOOT_ARCHIVE, 8293eb2bd662Svikram root_optional(osroot, menu_root)); 8294eb2bd662Svikram BAM_DPRINTF((D_UPDATED_BOOT_ENTRY, fcn, bam_zfs, grubsign)); 8295843e1988Sjohnlev if ((entry != BAM_ERROR) && (bam_is_hv == BAM_HV_PRESENT)) { 8296eb2bd662Svikram (void) update_boot_entry(mp, NEW_HV_ENTRY, grubsign, 8297eb2bd662Svikram grubroot, XEN_MENU, bam_zfs ? 8298eb2bd662Svikram XEN_KERNEL_MODULE_LINE_ZFS : XEN_KERNEL_MODULE_LINE, 8299eb2bd662Svikram DIRECT_BOOT_ARCHIVE, 8300eb2bd662Svikram root_optional(osroot, menu_root)); 8301eb2bd662Svikram BAM_DPRINTF((D_UPDATED_HV_ENTRY, 8302eb2bd662Svikram fcn, bam_zfs, grubsign)); 8303ae115bc7Smrj } 8304843e1988Sjohnlev } else { 8305eb2bd662Svikram entry = update_boot_entry(mp, title, grubsign, grubroot, 8306eb2bd662Svikram MULTI_BOOT, NULL, MULTIBOOT_ARCHIVE, 8307eb2bd662Svikram root_optional(osroot, menu_root)); 8308eb2bd662Svikram 8309eb2bd662Svikram BAM_DPRINTF((D_UPDATED_MULTIBOOT_ENTRY, fcn, grubsign)); 8310843e1988Sjohnlev } 83117c478bd9Sstevel@tonic-gate 8312843e1988Sjohnlev /* 8313843e1988Sjohnlev * Add the entry for failsafe archive. On a bfu'd system, the 8314843e1988Sjohnlev * failsafe may be different than the installed kernel. 8315843e1988Sjohnlev */ 8316eb2bd662Svikram (void) snprintf(failsafe, sizeof (failsafe), "%s%s", 8317bbcc54bdSEnrico Perla - Sun Microsystems osroot, FAILSAFE_ARCHIVE_32); 8318bbcc54bdSEnrico Perla - Sun Microsystems (void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s", 8319bbcc54bdSEnrico Perla - Sun Microsystems osroot, FAILSAFE_ARCHIVE_64); 8320bbcc54bdSEnrico Perla - Sun Microsystems 8321bbcc54bdSEnrico Perla - Sun Microsystems /* 8322bbcc54bdSEnrico Perla - Sun Microsystems * Check if at least one of the two archives exists 8323bbcc54bdSEnrico Perla - Sun Microsystems * Using $ISADIR as the default line, we have an entry which works 8324bbcc54bdSEnrico Perla - Sun Microsystems * for both the cases. 8325bbcc54bdSEnrico Perla - Sun Microsystems */ 8326bbcc54bdSEnrico Perla - Sun Microsystems 8327bbcc54bdSEnrico Perla - Sun Microsystems if (stat(failsafe, &sbuf) == 0 || stat(failsafe_64, &sbuf) == 0) { 832860d0a590Srscott 832960d0a590Srscott /* Figure out where the kernel line should point */ 833060d0a590Srscott (void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot, 8331bbcc54bdSEnrico Perla - Sun Microsystems DIRECT_BOOT_FAILSAFE_32); 8332bbcc54bdSEnrico Perla - Sun Microsystems (void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s", 8333bbcc54bdSEnrico Perla - Sun Microsystems osroot, DIRECT_BOOT_FAILSAFE_64); 8334bbcc54bdSEnrico Perla - Sun Microsystems if (stat(failsafe, &sbuf) == 0 || 8335bbcc54bdSEnrico Perla - Sun Microsystems stat(failsafe_64, &sbuf) == 0) { 8336963390b4Svikram failsafe_kernel = DIRECT_BOOT_FAILSAFE_LINE; 833760d0a590Srscott } else { 833860d0a590Srscott (void) snprintf(failsafe, sizeof (failsafe), "%s%s", 833960d0a590Srscott osroot, MULTI_BOOT_FAILSAFE); 834060d0a590Srscott if (stat(failsafe, &sbuf) == 0) { 834160d0a590Srscott failsafe_kernel = MULTI_BOOT_FAILSAFE_LINE; 834260d0a590Srscott } 834360d0a590Srscott } 834460d0a590Srscott if (failsafe_kernel != NULL) { 8345eb2bd662Svikram (void) update_boot_entry(mp, FAILSAFE_TITLE, grubsign, 8346eb2bd662Svikram grubroot, failsafe_kernel, NULL, FAILSAFE_ARCHIVE, 8347eb2bd662Svikram root_optional(osroot, menu_root)); 8348eb2bd662Svikram BAM_DPRINTF((D_UPDATED_FAILSAFE_ENTRY, fcn, 8349eb2bd662Svikram failsafe_kernel)); 835060d0a590Srscott } 8351ae115bc7Smrj } 8352eb2bd662Svikram free(grubroot); 83537c478bd9Sstevel@tonic-gate 8354eb2bd662Svikram INJECT_ERROR1("UPDATE_ENTRY_ERROR", entry = BAM_ERROR); 83557c478bd9Sstevel@tonic-gate if (entry == BAM_ERROR) { 8356eb2bd662Svikram bam_error(FAILED_TO_ADD_BOOT_ENTRY, title, grubsign); 8357eb2bd662Svikram free(grubsign); 83587c478bd9Sstevel@tonic-gate return (BAM_ERROR); 83597c478bd9Sstevel@tonic-gate } 8360eb2bd662Svikram free(grubsign); 8361eb2bd662Svikram 8362eb2bd662Svikram update_numbering(mp); 8363eb2bd662Svikram ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry); 8364eb2bd662Svikram INJECT_ERROR1("SET_DEFAULT_ERROR", ret = BAM_ERROR); 8365eb2bd662Svikram if (ret == BAM_ERROR) { 8366eb2bd662Svikram bam_error(SET_DEFAULT_FAILED, entry); 8367eb2bd662Svikram } 8368eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 83697c478bd9Sstevel@tonic-gate return (BAM_WRITE); 83707c478bd9Sstevel@tonic-gate } 83717c478bd9Sstevel@tonic-gate 83728c1b6884Sszhou static void 8373ae115bc7Smrj save_default_entry(menu_t *mp, const char *which) 83748c1b6884Sszhou { 8375eb2bd662Svikram int lineNum; 8376eb2bd662Svikram int entryNum; 83778c1b6884Sszhou int entry = 0; /* default is 0 */ 83788c1b6884Sszhou char linebuf[BAM_MAXLINE]; 83798c1b6884Sszhou line_t *lp = mp->curdefault; 8380eb2bd662Svikram const char *fcn = "save_default_entry()"; 83818c1b6884Sszhou 8382bff269d0Svikram if (mp->start) { 8383bff269d0Svikram lineNum = mp->end->lineNum; 8384bff269d0Svikram entryNum = mp->end->entryNum; 8385bff269d0Svikram } else { 8386bff269d0Svikram lineNum = LINE_INIT; 8387bff269d0Svikram entryNum = ENTRY_INIT; 8388bff269d0Svikram } 8389bff269d0Svikram 83908c1b6884Sszhou if (lp) 83918c1b6884Sszhou entry = s_strtol(lp->arg); 83928c1b6884Sszhou 8393ae115bc7Smrj (void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry); 8394eb2bd662Svikram BAM_DPRINTF((D_SAVING_DEFAULT_TO, fcn, linebuf)); 83958c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 8396eb2bd662Svikram BAM_DPRINTF((D_SAVED_DEFAULT_TO, fcn, lineNum, entryNum)); 83978c1b6884Sszhou } 83988c1b6884Sszhou 83998c1b6884Sszhou static void 8400ae115bc7Smrj restore_default_entry(menu_t *mp, const char *which, line_t *lp) 84018c1b6884Sszhou { 84028c1b6884Sszhou int entry; 84038c1b6884Sszhou char *str; 8404eb2bd662Svikram const char *fcn = "restore_default_entry()"; 84058c1b6884Sszhou 8406eb2bd662Svikram if (lp == NULL) { 8407eb2bd662Svikram BAM_DPRINTF((D_RESTORE_DEFAULT_NULL, fcn)); 84088c1b6884Sszhou return; /* nothing to restore */ 8409eb2bd662Svikram } 8410eb2bd662Svikram 8411eb2bd662Svikram BAM_DPRINTF((D_RESTORE_DEFAULT_STR, fcn, which)); 84128c1b6884Sszhou 8413ae115bc7Smrj str = lp->arg + strlen(which); 84148c1b6884Sszhou entry = s_strtol(str); 84158c1b6884Sszhou (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 84168c1b6884Sszhou 8417eb2bd662Svikram BAM_DPRINTF((D_RESTORED_DEFAULT_TO, fcn, entry)); 8418eb2bd662Svikram 84198c1b6884Sszhou /* delete saved old default line */ 84208c1b6884Sszhou unlink_line(mp, lp); 84218c1b6884Sszhou line_free(lp); 84228c1b6884Sszhou } 84238c1b6884Sszhou 84247c478bd9Sstevel@tonic-gate /* 84257c478bd9Sstevel@tonic-gate * This function is for supporting reboot with args. 84267c478bd9Sstevel@tonic-gate * The opt value can be: 84277c478bd9Sstevel@tonic-gate * NULL delete temp entry, if present 8428eb2bd662Svikram * entry=<n> switches default entry to <n> 84297c478bd9Sstevel@tonic-gate * else treated as boot-args and setup a temperary menu entry 84307c478bd9Sstevel@tonic-gate * and make it the default 8431eb2bd662Svikram * Note that we are always rebooting the current OS instance 8432eb2bd662Svikram * so osroot == / always. 84337c478bd9Sstevel@tonic-gate */ 84347c478bd9Sstevel@tonic-gate #define REBOOT_TITLE "Solaris_reboot_transient" 84357c478bd9Sstevel@tonic-gate 84368c1b6884Sszhou /*ARGSUSED*/ 84377c478bd9Sstevel@tonic-gate static error_t 8438eb2bd662Svikram update_temp(menu_t *mp, char *dummy, char *opt) 84397c478bd9Sstevel@tonic-gate { 84407c478bd9Sstevel@tonic-gate int entry; 8441eb2bd662Svikram char *osdev; 8442eb2bd662Svikram char *fstype; 8443eb2bd662Svikram char *sign; 8444eb2bd662Svikram char *opt_ptr; 8445eb2bd662Svikram char *path; 8446ae115bc7Smrj char kernbuf[BUFSIZ]; 8447ae115bc7Smrj char args_buf[BUFSIZ]; 8448eb2bd662Svikram char signbuf[PATH_MAX]; 8449eb2bd662Svikram int ret; 8450eb2bd662Svikram const char *fcn = "update_temp()"; 84517c478bd9Sstevel@tonic-gate 84527c478bd9Sstevel@tonic-gate assert(mp); 8453eb2bd662Svikram assert(dummy == NULL); 8454eb2bd662Svikram 8455eb2bd662Svikram /* opt can be NULL */ 8456eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt ? opt : "<NULL>")); 8457eb2bd662Svikram BAM_DPRINTF((D_BAM_ROOT, fcn, bam_alt_root, bam_root)); 8458eb2bd662Svikram 8459eb2bd662Svikram if (bam_alt_root || bam_rootlen != 1 || 8460eb2bd662Svikram strcmp(bam_root, "/") != 0 || 8461eb2bd662Svikram strcmp(rootbuf, "/") != 0) { 8462eb2bd662Svikram bam_error(ALT_ROOT_INVALID, bam_root); 8463eb2bd662Svikram return (BAM_ERROR); 8464eb2bd662Svikram } 84657c478bd9Sstevel@tonic-gate 84668c1b6884Sszhou /* If no option, delete exiting reboot menu entry */ 84678c1b6884Sszhou if (opt == NULL) { 8468eb2bd662Svikram entry_t *ent; 8469eb2bd662Svikram BAM_DPRINTF((D_OPT_NULL, fcn)); 8470eb2bd662Svikram ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL, 8471eb2bd662Svikram NULL, NULL, 0, &entry); 8472eb2bd662Svikram if (ent == NULL) { /* not found is ok */ 8473eb2bd662Svikram BAM_DPRINTF((D_TRANSIENT_NOTFOUND, fcn)); 84748c1b6884Sszhou return (BAM_SUCCESS); 8475eb2bd662Svikram } 847644da779fSWilliam Kucharski (void) delete_boot_entry(mp, entry, DBE_PRINTERR); 8477ae115bc7Smrj restore_default_entry(mp, BAM_OLDDEF, mp->olddefault); 8478ae115bc7Smrj mp->olddefault = NULL; 8479eb2bd662Svikram BAM_DPRINTF((D_RESTORED_DEFAULT, fcn)); 8480eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 84818c1b6884Sszhou return (BAM_WRITE); 84828c1b6884Sszhou } 84838c1b6884Sszhou 84848c1b6884Sszhou /* if entry= is specified, set the default entry */ 8485eb2bd662Svikram if (strncmp(opt, "entry=", strlen("entry=")) == 0) { 8486eb2bd662Svikram int entryNum = s_strtol(opt + strlen("entry=")); 8487eb2bd662Svikram BAM_DPRINTF((D_ENTRY_EQUALS, fcn, opt)); 8488eb2bd662Svikram if (selector(mp, opt, &entry, NULL) == BAM_SUCCESS) { 84897c478bd9Sstevel@tonic-gate /* this is entry=# option */ 8490eb2bd662Svikram ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry); 8491eb2bd662Svikram BAM_DPRINTF((D_ENTRY_SET_IS, fcn, entry, ret)); 8492eb2bd662Svikram return (ret); 8493eb2bd662Svikram } else { 8494eb2bd662Svikram bam_error(SET_DEFAULT_FAILED, entryNum); 8495eb2bd662Svikram return (BAM_ERROR); 8496eb2bd662Svikram } 84977c478bd9Sstevel@tonic-gate } 84987c478bd9Sstevel@tonic-gate 84997c478bd9Sstevel@tonic-gate /* 8500eb2bd662Svikram * add a new menu entry based on opt and make it the default 8501b610f78eSvikram */ 8502eb2bd662Svikram 8503eb2bd662Svikram fstype = get_fstype("/"); 8504eb2bd662Svikram INJECT_ERROR1("REBOOT_FSTYPE_NULL", fstype = NULL); 8505eb2bd662Svikram if (fstype == NULL) { 8506eb2bd662Svikram bam_error(REBOOT_FSTYPE_FAILED); 8507eb2bd662Svikram return (BAM_ERROR); 85087c478bd9Sstevel@tonic-gate } 8509eb2bd662Svikram 8510eb2bd662Svikram osdev = get_special("/"); 8511eb2bd662Svikram INJECT_ERROR1("REBOOT_SPECIAL_NULL", osdev = NULL); 8512eb2bd662Svikram if (osdev == NULL) { 8513eb2bd662Svikram free(fstype); 8514eb2bd662Svikram bam_error(REBOOT_SPECIAL_FAILED); 8515eb2bd662Svikram return (BAM_ERROR); 8516b610f78eSvikram } 8517eb2bd662Svikram 8518eb2bd662Svikram sign = find_existing_sign("/", osdev, fstype); 8519eb2bd662Svikram INJECT_ERROR1("REBOOT_SIGN_NULL", sign = NULL); 8520eb2bd662Svikram if (sign == NULL) { 8521eb2bd662Svikram free(fstype); 8522eb2bd662Svikram free(osdev); 8523eb2bd662Svikram bam_error(REBOOT_SIGN_FAILED); 8524eb2bd662Svikram return (BAM_ERROR); 8525eb2bd662Svikram } 8526eb2bd662Svikram 8527eb2bd662Svikram free(osdev); 8528eb2bd662Svikram (void) strlcpy(signbuf, sign, sizeof (signbuf)); 8529eb2bd662Svikram free(sign); 8530eb2bd662Svikram 8531eb2bd662Svikram assert(strchr(signbuf, '(') == NULL && strchr(signbuf, ',') == NULL && 8532eb2bd662Svikram strchr(signbuf, ')') == NULL); 8533eb2bd662Svikram 8534eb2bd662Svikram /* 8535eb2bd662Svikram * There is no alternate root while doing reboot with args 8536eb2bd662Svikram * This version of bootadm is only delivered with a DBOOT 8537eb2bd662Svikram * version of Solaris. 8538eb2bd662Svikram */ 8539eb2bd662Svikram INJECT_ERROR1("REBOOT_NOT_DBOOT", bam_direct = BAM_DIRECT_MULTIBOOT); 8540eb2bd662Svikram if (bam_direct != BAM_DIRECT_DBOOT) { 85419bd6d269SLin Ling free(fstype); 8542eb2bd662Svikram bam_error(REBOOT_DIRECT_FAILED); 85437c478bd9Sstevel@tonic-gate return (BAM_ERROR); 85447c478bd9Sstevel@tonic-gate } 85457c478bd9Sstevel@tonic-gate 85467c478bd9Sstevel@tonic-gate /* add an entry for Solaris reboot */ 8547ae115bc7Smrj if (opt[0] == '-') { 8548ae115bc7Smrj /* It's an option - first see if boot-file is set */ 8549eb2bd662Svikram ret = get_kernel(mp, KERNEL_CMD, kernbuf, sizeof (kernbuf)); 8550eb2bd662Svikram INJECT_ERROR1("REBOOT_GET_KERNEL", ret = BAM_ERROR); 8551eb2bd662Svikram if (ret != BAM_SUCCESS) { 85529bd6d269SLin Ling free(fstype); 8553eb2bd662Svikram bam_error(REBOOT_GET_KERNEL_FAILED); 8554ae115bc7Smrj return (BAM_ERROR); 8555eb2bd662Svikram } 8556ae115bc7Smrj if (kernbuf[0] == '\0') 8557eb2bd662Svikram (void) strlcpy(kernbuf, DIRECT_BOOT_KERNEL, 8558eb2bd662Svikram sizeof (kernbuf)); 85599bd6d269SLin Ling /* 85609bd6d269SLin Ling * If this is a zfs file system and kernbuf does not 85619bd6d269SLin Ling * have "-B $ZFS-BOOTFS" string yet, add it. 85629bd6d269SLin Ling */ 85639bd6d269SLin Ling if (strcmp(fstype, "zfs") == 0 && !strstr(kernbuf, ZFS_BOOT)) { 85649bd6d269SLin Ling (void) strlcat(kernbuf, " ", sizeof (kernbuf)); 85659bd6d269SLin Ling (void) strlcat(kernbuf, ZFS_BOOT, sizeof (kernbuf)); 85669bd6d269SLin Ling } 8567eb2bd662Svikram (void) strlcat(kernbuf, " ", sizeof (kernbuf)); 8568eb2bd662Svikram (void) strlcat(kernbuf, opt, sizeof (kernbuf)); 8569eb2bd662Svikram BAM_DPRINTF((D_REBOOT_OPTION, fcn, kernbuf)); 8570ae115bc7Smrj } else if (opt[0] == '/') { 8571455710d3Srscott /* It's a full path, so write it out. */ 8572eb2bd662Svikram (void) strlcpy(kernbuf, opt, sizeof (kernbuf)); 8573455710d3Srscott 8574455710d3Srscott /* 8575455710d3Srscott * If someone runs: 8576455710d3Srscott * 8577455710d3Srscott * # eeprom boot-args='-kd' 8578455710d3Srscott * # reboot /platform/i86pc/kernel/unix 8579455710d3Srscott * 8580455710d3Srscott * we want to use the boot-args as part of the boot 8581455710d3Srscott * line. On the other hand, if someone runs: 8582455710d3Srscott * 8583455710d3Srscott * # reboot "/platform/i86pc/kernel/unix -kd" 8584455710d3Srscott * 8585455710d3Srscott * we don't need to mess with boot-args. If there's 8586455710d3Srscott * no space in the options string, assume we're in the 8587455710d3Srscott * first case. 8588455710d3Srscott */ 8589455710d3Srscott if (strchr(opt, ' ') == NULL) { 8590eb2bd662Svikram ret = get_kernel(mp, ARGS_CMD, args_buf, 8591eb2bd662Svikram sizeof (args_buf)); 8592eb2bd662Svikram INJECT_ERROR1("REBOOT_GET_ARGS", ret = BAM_ERROR); 8593eb2bd662Svikram if (ret != BAM_SUCCESS) { 85949bd6d269SLin Ling free(fstype); 8595eb2bd662Svikram bam_error(REBOOT_GET_ARGS_FAILED); 8596455710d3Srscott return (BAM_ERROR); 8597eb2bd662Svikram } 8598455710d3Srscott 8599455710d3Srscott if (args_buf[0] != '\0') { 8600eb2bd662Svikram (void) strlcat(kernbuf, " ", sizeof (kernbuf)); 8601455710d3Srscott (void) strlcat(kernbuf, args_buf, 8602eb2bd662Svikram sizeof (kernbuf)); 8603455710d3Srscott } 8604455710d3Srscott } 8605eb2bd662Svikram BAM_DPRINTF((D_REBOOT_ABSPATH, fcn, kernbuf)); 8606ae115bc7Smrj } else { 8607455710d3Srscott /* 8608455710d3Srscott * It may be a partial path, or it may be a partial 8609455710d3Srscott * path followed by options. Assume that only options 8610455710d3Srscott * follow a space. If someone sends us a kernel path 8611455710d3Srscott * that includes a space, they deserve to be broken. 8612455710d3Srscott */ 8613455710d3Srscott opt_ptr = strchr(opt, ' '); 8614455710d3Srscott if (opt_ptr != NULL) { 8615455710d3Srscott *opt_ptr = '\0'; 8616455710d3Srscott } 8617455710d3Srscott 8618ae115bc7Smrj path = expand_path(opt); 8619ae115bc7Smrj if (path != NULL) { 8620eb2bd662Svikram (void) strlcpy(kernbuf, path, sizeof (kernbuf)); 8621ae115bc7Smrj free(path); 8622455710d3Srscott 8623455710d3Srscott /* 8624455710d3Srscott * If there were options given, use those. 8625455710d3Srscott * Otherwise, copy over the default options. 8626455710d3Srscott */ 8627455710d3Srscott if (opt_ptr != NULL) { 8628455710d3Srscott /* Restore the space in opt string */ 8629455710d3Srscott *opt_ptr = ' '; 8630455710d3Srscott (void) strlcat(kernbuf, opt_ptr, 8631eb2bd662Svikram sizeof (kernbuf)); 8632455710d3Srscott } else { 8633eb2bd662Svikram ret = get_kernel(mp, ARGS_CMD, args_buf, 8634eb2bd662Svikram sizeof (args_buf)); 8635eb2bd662Svikram INJECT_ERROR1("UPDATE_TEMP_PARTIAL_ARGS", 8636eb2bd662Svikram ret = BAM_ERROR); 8637eb2bd662Svikram if (ret != BAM_SUCCESS) { 86389bd6d269SLin Ling free(fstype); 8639eb2bd662Svikram bam_error(REBOOT_GET_ARGS_FAILED); 8640ae115bc7Smrj return (BAM_ERROR); 8641eb2bd662Svikram } 8642ae115bc7Smrj 8643ae115bc7Smrj if (args_buf[0] != '\0') { 8644ae115bc7Smrj (void) strlcat(kernbuf, " ", 8645eb2bd662Svikram sizeof (kernbuf)); 8646ae115bc7Smrj (void) strlcat(kernbuf, 8647eb2bd662Svikram args_buf, sizeof (kernbuf)); 8648ae115bc7Smrj } 8649ae115bc7Smrj } 8650eb2bd662Svikram BAM_DPRINTF((D_REBOOT_RESOLVED_PARTIAL, fcn, kernbuf)); 8651455710d3Srscott } else { 86529bd6d269SLin Ling free(fstype); 8653455710d3Srscott bam_error(UNKNOWN_KERNEL, opt); 8654455710d3Srscott bam_print_stderr(UNKNOWN_KERNEL_REBOOT); 8655455710d3Srscott return (BAM_ERROR); 8656ae115bc7Smrj } 8657ae115bc7Smrj } 86589bd6d269SLin Ling free(fstype); 8659eb2bd662Svikram entry = add_boot_entry(mp, REBOOT_TITLE, signbuf, kernbuf, 866044da779fSWilliam Kucharski NULL, NULL, NULL); 8661eb2bd662Svikram INJECT_ERROR1("REBOOT_ADD_BOOT_ENTRY", entry = BAM_ERROR); 86627c478bd9Sstevel@tonic-gate if (entry == BAM_ERROR) { 8663eb2bd662Svikram bam_error(REBOOT_WITH_ARGS_ADD_ENTRY_FAILED); 86647c478bd9Sstevel@tonic-gate return (BAM_ERROR); 86657c478bd9Sstevel@tonic-gate } 86668c1b6884Sszhou 8667ae115bc7Smrj save_default_entry(mp, BAM_OLDDEF); 8668eb2bd662Svikram ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry); 8669eb2bd662Svikram INJECT_ERROR1("REBOOT_SET_GLOBAL", ret = BAM_ERROR); 8670eb2bd662Svikram if (ret == BAM_ERROR) { 8671eb2bd662Svikram bam_error(REBOOT_SET_DEFAULT_FAILED, entry); 8672eb2bd662Svikram } 8673eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 86747c478bd9Sstevel@tonic-gate return (BAM_WRITE); 86757c478bd9Sstevel@tonic-gate } 86767c478bd9Sstevel@tonic-gate 867744da779fSWilliam Kucharski error_t 86787c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val) 86797c478bd9Sstevel@tonic-gate { 8680eb2bd662Svikram line_t *lp; 8681eb2bd662Svikram line_t *found; 8682eb2bd662Svikram line_t *last; 8683eb2bd662Svikram char *cp; 8684eb2bd662Svikram char *str; 86857c478bd9Sstevel@tonic-gate char prefix[BAM_MAXLINE]; 86867c478bd9Sstevel@tonic-gate size_t len; 8687eb2bd662Svikram const char *fcn = "set_global()"; 86887c478bd9Sstevel@tonic-gate 86897c478bd9Sstevel@tonic-gate assert(mp); 86907c478bd9Sstevel@tonic-gate assert(globalcmd); 86917c478bd9Sstevel@tonic-gate 8692b610f78eSvikram if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) { 8693eb2bd662Svikram INJECT_ERROR1("SET_GLOBAL_VAL_NEG", val = -1); 8694eb2bd662Svikram INJECT_ERROR1("SET_GLOBAL_MENU_EMPTY", mp->end = NULL); 8695eb2bd662Svikram INJECT_ERROR1("SET_GLOBAL_VAL_TOO_BIG", val = 100); 8696b610f78eSvikram if (val < 0 || mp->end == NULL || val > mp->end->entryNum) { 8697b610f78eSvikram (void) snprintf(prefix, sizeof (prefix), "%d", val); 8698b610f78eSvikram bam_error(INVALID_ENTRY, prefix); 8699b610f78eSvikram return (BAM_ERROR); 8700b610f78eSvikram } 8701b610f78eSvikram } 8702b610f78eSvikram 87037c478bd9Sstevel@tonic-gate found = last = NULL; 87047c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 87057c478bd9Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 87067c478bd9Sstevel@tonic-gate continue; 87077c478bd9Sstevel@tonic-gate 87087c478bd9Sstevel@tonic-gate last = lp; /* track the last global found */ 87097c478bd9Sstevel@tonic-gate 8710eb2bd662Svikram INJECT_ERROR1("SET_GLOBAL_NULL_CMD", lp->cmd = NULL); 87117c478bd9Sstevel@tonic-gate if (lp->cmd == NULL) { 87127c478bd9Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 87137c478bd9Sstevel@tonic-gate continue; 87147c478bd9Sstevel@tonic-gate } 87157c478bd9Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 87167c478bd9Sstevel@tonic-gate continue; 87177c478bd9Sstevel@tonic-gate 8718eb2bd662Svikram BAM_DPRINTF((D_FOUND_GLOBAL, fcn, globalcmd)); 8719eb2bd662Svikram 87207c478bd9Sstevel@tonic-gate if (found) { 87217c478bd9Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 87227c478bd9Sstevel@tonic-gate } 87237c478bd9Sstevel@tonic-gate found = lp; 87247c478bd9Sstevel@tonic-gate } 87257c478bd9Sstevel@tonic-gate 87267c478bd9Sstevel@tonic-gate if (found == NULL) { 87277c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 87287c478bd9Sstevel@tonic-gate if (last == NULL) { 87297c478bd9Sstevel@tonic-gate lp->next = mp->start; 87307c478bd9Sstevel@tonic-gate mp->start = lp; 87317c478bd9Sstevel@tonic-gate mp->end = (mp->end) ? mp->end : lp; 87327c478bd9Sstevel@tonic-gate } else { 87337c478bd9Sstevel@tonic-gate lp->next = last->next; 87347c478bd9Sstevel@tonic-gate last->next = lp; 87357c478bd9Sstevel@tonic-gate if (lp->next == NULL) 87367c478bd9Sstevel@tonic-gate mp->end = lp; 87377c478bd9Sstevel@tonic-gate } 87387c478bd9Sstevel@tonic-gate lp->flags = BAM_GLOBAL; /* other fields not needed for writes */ 87397c478bd9Sstevel@tonic-gate len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]); 87407c478bd9Sstevel@tonic-gate len += 10; /* val < 10 digits */ 87417c478bd9Sstevel@tonic-gate lp->line = s_calloc(1, len); 87427c478bd9Sstevel@tonic-gate (void) snprintf(lp->line, len, "%s%s%d", 87437c478bd9Sstevel@tonic-gate globalcmd, menu_cmds[SEP_CMD], val); 8744eb2bd662Svikram BAM_DPRINTF((D_SET_GLOBAL_WROTE_NEW, fcn, lp->line)); 8745eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 87467c478bd9Sstevel@tonic-gate return (BAM_WRITE); 87477c478bd9Sstevel@tonic-gate } 87487c478bd9Sstevel@tonic-gate 87497c478bd9Sstevel@tonic-gate /* 87507c478bd9Sstevel@tonic-gate * We are changing an existing entry. Retain any prefix whitespace, 87517c478bd9Sstevel@tonic-gate * but overwrite everything else. This preserves tabs added for 87527c478bd9Sstevel@tonic-gate * readability. 87537c478bd9Sstevel@tonic-gate */ 87547c478bd9Sstevel@tonic-gate str = found->line; 87557c478bd9Sstevel@tonic-gate cp = prefix; 87567c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 87577c478bd9Sstevel@tonic-gate *(cp++) = *(str++); 87587c478bd9Sstevel@tonic-gate *cp = '\0'; /* Terminate prefix */ 87597c478bd9Sstevel@tonic-gate len = strlen(prefix) + strlen(globalcmd); 87607c478bd9Sstevel@tonic-gate len += strlen(menu_cmds[SEP_CMD]) + 10; 87617c478bd9Sstevel@tonic-gate 87627c478bd9Sstevel@tonic-gate free(found->line); 87637c478bd9Sstevel@tonic-gate found->line = s_calloc(1, len); 87647c478bd9Sstevel@tonic-gate (void) snprintf(found->line, len, 87657c478bd9Sstevel@tonic-gate "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val); 87667c478bd9Sstevel@tonic-gate 8767eb2bd662Svikram BAM_DPRINTF((D_SET_GLOBAL_REPLACED, fcn, found->line)); 8768eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 87697c478bd9Sstevel@tonic-gate return (BAM_WRITE); /* need a write to menu */ 87707c478bd9Sstevel@tonic-gate } 87717c478bd9Sstevel@tonic-gate 8772ae115bc7Smrj /* 8773ae115bc7Smrj * partial_path may be anything like "kernel/unix" or "kmdb". Try to 8774455710d3Srscott * expand it to a full unix path. The calling function is expected to 8775455710d3Srscott * output a message if an error occurs and NULL is returned. 8776ae115bc7Smrj */ 8777ae115bc7Smrj static char * 8778ae115bc7Smrj expand_path(const char *partial_path) 8779ae115bc7Smrj { 8780ae115bc7Smrj int new_path_len; 8781eb2bd662Svikram char *new_path; 8782eb2bd662Svikram char new_path2[PATH_MAX]; 8783ae115bc7Smrj struct stat sb; 8784eb2bd662Svikram const char *fcn = "expand_path()"; 8785ae115bc7Smrj 8786ae115bc7Smrj new_path_len = strlen(partial_path) + 64; 8787ae115bc7Smrj new_path = s_calloc(1, new_path_len); 8788ae115bc7Smrj 8789ae115bc7Smrj /* First, try the simplest case - something like "kernel/unix" */ 8790ae115bc7Smrj (void) snprintf(new_path, new_path_len, "/platform/i86pc/%s", 8791ae115bc7Smrj partial_path); 8792ae115bc7Smrj if (stat(new_path, &sb) == 0) { 8793eb2bd662Svikram BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path)); 8794ae115bc7Smrj return (new_path); 8795ae115bc7Smrj } 8796ae115bc7Smrj 8797ae115bc7Smrj if (strcmp(partial_path, "kmdb") == 0) { 8798ae115bc7Smrj (void) snprintf(new_path, new_path_len, "%s -k", 8799ae115bc7Smrj DIRECT_BOOT_KERNEL); 8800eb2bd662Svikram BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path)); 8801ae115bc7Smrj return (new_path); 8802ae115bc7Smrj } 8803ae115bc7Smrj 8804ae115bc7Smrj /* 8805ae115bc7Smrj * We've quickly reached unsupported usage. Try once more to 8806ae115bc7Smrj * see if we were just given a glom name. 8807ae115bc7Smrj */ 8808ae115bc7Smrj (void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix", 8809ae115bc7Smrj partial_path); 8810ae115bc7Smrj (void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix", 8811ae115bc7Smrj partial_path); 8812ae115bc7Smrj if (stat(new_path, &sb) == 0) { 8813ae115bc7Smrj if (stat(new_path2, &sb) == 0) { 8814ae115bc7Smrj /* 8815ae115bc7Smrj * We matched both, so we actually 8816ae115bc7Smrj * want to write the $ISADIR version. 8817ae115bc7Smrj */ 8818ae115bc7Smrj (void) snprintf(new_path, new_path_len, 8819ae115bc7Smrj "/platform/i86pc/kernel/%s/$ISADIR/unix", 8820ae115bc7Smrj partial_path); 8821ae115bc7Smrj } 8822eb2bd662Svikram BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path)); 8823ae115bc7Smrj return (new_path); 8824ae115bc7Smrj } 8825ae115bc7Smrj 8826ae115bc7Smrj free(new_path); 8827eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 8828ae115bc7Smrj return (NULL); 8829ae115bc7Smrj } 8830ae115bc7Smrj 8831ae115bc7Smrj /* 8832ae115bc7Smrj * The kernel cmd and arg have been changed, so 8833ae115bc7Smrj * check whether the archive line needs to change. 8834ae115bc7Smrj */ 8835ae115bc7Smrj static void 8836ae115bc7Smrj set_archive_line(entry_t *entryp, line_t *kernelp) 8837ae115bc7Smrj { 8838ae115bc7Smrj line_t *lp = entryp->start; 8839ae115bc7Smrj char *new_archive; 8840ae115bc7Smrj menu_cmd_t m_cmd; 8841eb2bd662Svikram const char *fcn = "set_archive_line()"; 8842ae115bc7Smrj 8843ae115bc7Smrj for (; lp != NULL; lp = lp->next) { 8844eac223ccSWilliam Kucharski if (lp->cmd != NULL && strncmp(lp->cmd, menu_cmds[MODULE_CMD], 8845ae115bc7Smrj sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) { 8846ae115bc7Smrj break; 8847ae115bc7Smrj } 8848eb2bd662Svikram 8849eb2bd662Svikram INJECT_ERROR1("SET_ARCHIVE_LINE_END_ENTRY", lp = entryp->end); 8850eb2bd662Svikram if (lp == entryp->end) { 8851eb2bd662Svikram BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn, 8852eb2bd662Svikram entryp->entryNum)); 8853ae115bc7Smrj return; 8854ae115bc7Smrj } 8855eb2bd662Svikram } 8856eb2bd662Svikram INJECT_ERROR1("SET_ARCHIVE_LINE_END_MENU", lp = NULL); 8857eb2bd662Svikram if (lp == NULL) { 8858eb2bd662Svikram BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn, entryp->entryNum)); 8859ae115bc7Smrj return; 8860eb2bd662Svikram } 8861ae115bc7Smrj 8862ae115bc7Smrj if (strstr(kernelp->arg, "$ISADIR") != NULL) { 8863ae115bc7Smrj new_archive = DIRECT_BOOT_ARCHIVE; 8864ae115bc7Smrj m_cmd = MODULE_DOLLAR_CMD; 8865ae115bc7Smrj } else if (strstr(kernelp->arg, "amd64") != NULL) { 8866ae115bc7Smrj new_archive = DIRECT_BOOT_ARCHIVE_64; 8867ae115bc7Smrj m_cmd = MODULE_CMD; 8868ae115bc7Smrj } else { 8869ae115bc7Smrj new_archive = DIRECT_BOOT_ARCHIVE_32; 8870ae115bc7Smrj m_cmd = MODULE_CMD; 8871ae115bc7Smrj } 8872ae115bc7Smrj 8873eb2bd662Svikram if (strcmp(lp->arg, new_archive) == 0) { 8874eb2bd662Svikram BAM_DPRINTF((D_ARCHIVE_LINE_NOCHANGE, fcn, lp->arg)); 8875ae115bc7Smrj return; 8876eb2bd662Svikram } 8877ae115bc7Smrj 8878eac223ccSWilliam Kucharski if (lp->cmd != NULL && strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) { 8879ae115bc7Smrj free(lp->cmd); 8880ae115bc7Smrj lp->cmd = s_strdup(menu_cmds[m_cmd]); 8881ae115bc7Smrj } 8882ae115bc7Smrj 8883ae115bc7Smrj free(lp->arg); 8884ae115bc7Smrj lp->arg = s_strdup(new_archive); 8885ae115bc7Smrj update_line(lp); 8886eb2bd662Svikram BAM_DPRINTF((D_ARCHIVE_LINE_REPLACED, fcn, lp->line)); 8887ae115bc7Smrj } 8888ae115bc7Smrj 8889ae115bc7Smrj /* 8890ae115bc7Smrj * Title for an entry to set properties that once went in bootenv.rc. 8891ae115bc7Smrj */ 8892ae115bc7Smrj #define BOOTENV_RC_TITLE "Solaris bootenv rc" 8893ae115bc7Smrj 8894ae115bc7Smrj /* 8895ae115bc7Smrj * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments 8896ae115bc7Smrj * (optnum == ARGS_CMD) in the argument buf. If path is a zero-length 8897ae115bc7Smrj * string, reset the value to the default. If path is a non-zero-length 8898ae115bc7Smrj * string, set the kernel or arguments. 8899ae115bc7Smrj */ 8900ae115bc7Smrj static error_t 8901eb2bd662Svikram get_set_kernel( 8902eb2bd662Svikram menu_t *mp, 8903eb2bd662Svikram menu_cmd_t optnum, 8904eb2bd662Svikram char *path, 8905eb2bd662Svikram char *buf, 8906eb2bd662Svikram size_t bufsize) 8907ae115bc7Smrj { 8908eb2bd662Svikram int entryNum; 8909eb2bd662Svikram int rv = BAM_SUCCESS; 8910eb2bd662Svikram int free_new_path = 0; 8911ae115bc7Smrj entry_t *entryp; 8912eb2bd662Svikram line_t *ptr; 8913eb2bd662Svikram line_t *kernelp; 8914eb2bd662Svikram char *new_arg; 8915eb2bd662Svikram char *old_args; 8916eb2bd662Svikram char *space; 8917eb2bd662Svikram char *new_path; 8918ae115bc7Smrj char old_space; 8919110570ceSToomas Soome size_t old_kernel_len = 0; 8920eb2bd662Svikram size_t new_str_len; 8921eb2bd662Svikram char *fstype; 8922eb2bd662Svikram char *osdev; 8923eb2bd662Svikram char *sign; 8924eb2bd662Svikram char signbuf[PATH_MAX]; 8925eb2bd662Svikram int ret; 8926eb2bd662Svikram const char *fcn = "get_set_kernel()"; 8927ae115bc7Smrj 8928ae115bc7Smrj assert(bufsize > 0); 8929ae115bc7Smrj 8930ae115bc7Smrj ptr = kernelp = NULL; 8931ae115bc7Smrj new_arg = old_args = space = NULL; 8932eb2bd662Svikram new_path = NULL; 8933ae115bc7Smrj buf[0] = '\0'; 8934ae115bc7Smrj 8935eb2bd662Svikram INJECT_ERROR1("GET_SET_KERNEL_NOT_DBOOT", 8936eb2bd662Svikram bam_direct = BAM_DIRECT_MULTIBOOT); 8937ae115bc7Smrj if (bam_direct != BAM_DIRECT_DBOOT) { 8938ae115bc7Smrj bam_error(NOT_DBOOT, optnum == KERNEL_CMD ? "kernel" : "args"); 8939ae115bc7Smrj return (BAM_ERROR); 8940ae115bc7Smrj } 8941ae115bc7Smrj 8942ae115bc7Smrj /* 8943ae115bc7Smrj * If a user changed the default entry to a non-bootadm controlled 8944ae115bc7Smrj * one, we don't want to mess with it. Just print an error and 8945ae115bc7Smrj * return. 8946ae115bc7Smrj */ 8947ae115bc7Smrj if (mp->curdefault) { 8948ae115bc7Smrj entryNum = s_strtol(mp->curdefault->arg); 8949ae115bc7Smrj for (entryp = mp->entries; entryp; entryp = entryp->next) { 8950ae115bc7Smrj if (entryp->entryNum == entryNum) 8951ae115bc7Smrj break; 8952ae115bc7Smrj } 8953ae115bc7Smrj if ((entryp != NULL) && 8954ae115bc7Smrj ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) { 8955ae115bc7Smrj bam_error(DEFAULT_NOT_BAM); 8956ae115bc7Smrj return (BAM_ERROR); 8957ae115bc7Smrj } 8958ae115bc7Smrj } 8959ae115bc7Smrj 8960eb2bd662Svikram entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, NULL, NULL, 8961eb2bd662Svikram 0, &entryNum); 8962ae115bc7Smrj 8963ae115bc7Smrj if (entryp != NULL) { 8964ae115bc7Smrj for (ptr = entryp->start; ptr && ptr != entryp->end; 8965ae115bc7Smrj ptr = ptr->next) { 8966ae115bc7Smrj if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD], 8967ae115bc7Smrj sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) { 8968ae115bc7Smrj kernelp = ptr; 8969ae115bc7Smrj break; 8970ae115bc7Smrj } 8971ae115bc7Smrj } 8972ae115bc7Smrj if (kernelp == NULL) { 8973ae115bc7Smrj bam_error(NO_KERNEL, entryNum); 8974ae115bc7Smrj return (BAM_ERROR); 8975ae115bc7Smrj } 8976ae115bc7Smrj 8977ae115bc7Smrj old_kernel_len = strcspn(kernelp->arg, " \t"); 8978ae115bc7Smrj space = old_args = kernelp->arg + old_kernel_len; 8979ae115bc7Smrj while ((*old_args == ' ') || (*old_args == '\t')) 8980ae115bc7Smrj old_args++; 8981ae115bc7Smrj } 8982ae115bc7Smrj 8983ae115bc7Smrj if (path == NULL) { 8984eb2bd662Svikram if (entryp == NULL) { 8985eb2bd662Svikram BAM_DPRINTF((D_GET_SET_KERNEL_NO_RC, fcn)); 8986eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 8987ae115bc7Smrj return (BAM_SUCCESS); 8988eb2bd662Svikram } 8989eb2bd662Svikram assert(kernelp); 8990ae115bc7Smrj if (optnum == ARGS_CMD) { 8991eb2bd662Svikram if (old_args[0] != '\0') { 8992ae115bc7Smrj (void) strlcpy(buf, old_args, bufsize); 8993eb2bd662Svikram BAM_DPRINTF((D_GET_SET_KERNEL_ARGS, fcn, buf)); 8994eb2bd662Svikram } 8995ae115bc7Smrj } else { 8996ae115bc7Smrj /* 8997ae115bc7Smrj * We need to print the kernel, so we just turn the 8998ae115bc7Smrj * first space into a '\0' and print the beginning. 8999ae115bc7Smrj * We don't print anything if it's the default kernel. 9000ae115bc7Smrj */ 9001ae115bc7Smrj old_space = *space; 9002ae115bc7Smrj *space = '\0'; 9003eb2bd662Svikram if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0) { 9004ae115bc7Smrj (void) strlcpy(buf, kernelp->arg, bufsize); 9005eb2bd662Svikram BAM_DPRINTF((D_GET_SET_KERNEL_KERN, fcn, buf)); 9006eb2bd662Svikram } 9007ae115bc7Smrj *space = old_space; 9008ae115bc7Smrj } 9009eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 9010ae115bc7Smrj return (BAM_SUCCESS); 9011ae115bc7Smrj } 9012ae115bc7Smrj 9013ae115bc7Smrj /* 9014ae115bc7Smrj * First, check if we're resetting an entry to the default. 9015ae115bc7Smrj */ 9016ae115bc7Smrj if ((path[0] == '\0') || 9017ae115bc7Smrj ((optnum == KERNEL_CMD) && 9018ae115bc7Smrj (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) { 9019ae115bc7Smrj if ((entryp == NULL) || (kernelp == NULL)) { 9020ae115bc7Smrj /* No previous entry, it's already the default */ 9021eb2bd662Svikram BAM_DPRINTF((D_GET_SET_KERNEL_ALREADY, fcn)); 9022ae115bc7Smrj return (BAM_SUCCESS); 9023ae115bc7Smrj } 9024ae115bc7Smrj 9025ae115bc7Smrj /* 9026ae115bc7Smrj * Check if we can delete the entry. If we're resetting the 9027ae115bc7Smrj * kernel command, and the args is already empty, or if we're 9028ae115bc7Smrj * resetting the args command, and the kernel is already the 9029ae115bc7Smrj * default, we can restore the old default and delete the entry. 9030ae115bc7Smrj */ 9031ae115bc7Smrj if (((optnum == KERNEL_CMD) && 9032ae115bc7Smrj ((old_args == NULL) || (old_args[0] == '\0'))) || 9033ae115bc7Smrj ((optnum == ARGS_CMD) && 9034ae115bc7Smrj (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL, 9035ae115bc7Smrj sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) { 9036ae115bc7Smrj kernelp = NULL; 903744da779fSWilliam Kucharski (void) delete_boot_entry(mp, entryNum, DBE_PRINTERR); 9038ae115bc7Smrj restore_default_entry(mp, BAM_OLD_RC_DEF, 9039ae115bc7Smrj mp->old_rc_default); 9040ae115bc7Smrj mp->old_rc_default = NULL; 9041ae115bc7Smrj rv = BAM_WRITE; 9042eb2bd662Svikram BAM_DPRINTF((D_GET_SET_KERNEL_RESTORE_DEFAULT, fcn)); 9043ae115bc7Smrj goto done; 9044ae115bc7Smrj } 9045ae115bc7Smrj 9046ae115bc7Smrj if (optnum == KERNEL_CMD) { 9047ae115bc7Smrj /* 9048ae115bc7Smrj * At this point, we've already checked that old_args 9049ae115bc7Smrj * and entryp are valid pointers. The "+ 2" is for 9050ae115bc7Smrj * a space a the string termination character. 9051ae115bc7Smrj */ 9052ae115bc7Smrj new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) + 9053ae115bc7Smrj strlen(old_args) + 2; 9054ae115bc7Smrj new_arg = s_calloc(1, new_str_len); 9055ae115bc7Smrj (void) snprintf(new_arg, new_str_len, "%s %s", 9056ae115bc7Smrj DIRECT_BOOT_KERNEL, old_args); 9057ae115bc7Smrj free(kernelp->arg); 9058ae115bc7Smrj kernelp->arg = new_arg; 9059ae115bc7Smrj 9060ae115bc7Smrj /* 9061ae115bc7Smrj * We have changed the kernel line, so we may need 9062ae115bc7Smrj * to update the archive line as well. 9063ae115bc7Smrj */ 9064ae115bc7Smrj set_archive_line(entryp, kernelp); 9065eb2bd662Svikram BAM_DPRINTF((D_GET_SET_KERNEL_RESET_KERNEL_SET_ARG, 9066eb2bd662Svikram fcn, kernelp->arg)); 9067ae115bc7Smrj } else { 9068ae115bc7Smrj /* 9069ae115bc7Smrj * We're resetting the boot args to nothing, so 9070ae115bc7Smrj * we only need to copy the kernel. We've already 9071ae115bc7Smrj * checked that the kernel is not the default. 9072ae115bc7Smrj */ 9073ae115bc7Smrj new_arg = s_calloc(1, old_kernel_len + 1); 9074ae115bc7Smrj (void) snprintf(new_arg, old_kernel_len + 1, "%s", 9075ae115bc7Smrj kernelp->arg); 9076ae115bc7Smrj free(kernelp->arg); 9077ae115bc7Smrj kernelp->arg = new_arg; 9078eb2bd662Svikram BAM_DPRINTF((D_GET_SET_KERNEL_RESET_ARG_SET_KERNEL, 9079eb2bd662Svikram fcn, kernelp->arg)); 9080ae115bc7Smrj } 9081ae115bc7Smrj rv = BAM_WRITE; 9082ae115bc7Smrj goto done; 9083ae115bc7Smrj } 9084ae115bc7Smrj 9085ae115bc7Smrj /* 9086ae115bc7Smrj * Expand the kernel file to a full path, if necessary 9087ae115bc7Smrj */ 9088ae115bc7Smrj if ((optnum == KERNEL_CMD) && (path[0] != '/')) { 9089ae115bc7Smrj new_path = expand_path(path); 9090ae115bc7Smrj if (new_path == NULL) { 9091455710d3Srscott bam_error(UNKNOWN_KERNEL, path); 9092eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 9093ae115bc7Smrj return (BAM_ERROR); 9094ae115bc7Smrj } 9095ae115bc7Smrj free_new_path = 1; 9096ae115bc7Smrj } else { 9097ae115bc7Smrj new_path = path; 9098ae115bc7Smrj free_new_path = 0; 9099ae115bc7Smrj } 9100ae115bc7Smrj 9101ae115bc7Smrj /* 9102ae115bc7Smrj * At this point, we know we're setting a new value. First, take care 9103ae115bc7Smrj * of the case where there was no previous entry. 9104ae115bc7Smrj */ 9105ae115bc7Smrj if (entryp == NULL) { 9106eb2bd662Svikram 9107ae115bc7Smrj /* Similar to code in update_temp */ 9108eb2bd662Svikram fstype = get_fstype("/"); 9109eb2bd662Svikram INJECT_ERROR1("GET_SET_KERNEL_FSTYPE", fstype = NULL); 9110eb2bd662Svikram if (fstype == NULL) { 9111eb2bd662Svikram bam_error(BOOTENV_FSTYPE_FAILED); 9112ae115bc7Smrj rv = BAM_ERROR; 9113ae115bc7Smrj goto done; 9114ae115bc7Smrj } 9115eb2bd662Svikram 9116eb2bd662Svikram osdev = get_special("/"); 9117eb2bd662Svikram INJECT_ERROR1("GET_SET_KERNEL_SPECIAL", osdev = NULL); 9118eb2bd662Svikram if (osdev == NULL) { 9119eb2bd662Svikram free(fstype); 9120eb2bd662Svikram bam_error(BOOTENV_SPECIAL_FAILED); 9121eb2bd662Svikram rv = BAM_ERROR; 9122eb2bd662Svikram goto done; 9123eb2bd662Svikram } 9124eb2bd662Svikram 9125eb2bd662Svikram sign = find_existing_sign("/", osdev, fstype); 9126eb2bd662Svikram INJECT_ERROR1("GET_SET_KERNEL_SIGN", sign = NULL); 9127eb2bd662Svikram if (sign == NULL) { 9128eb2bd662Svikram free(fstype); 9129eb2bd662Svikram free(osdev); 9130eb2bd662Svikram bam_error(BOOTENV_SIGN_FAILED); 9131eb2bd662Svikram rv = BAM_ERROR; 9132eb2bd662Svikram goto done; 9133eb2bd662Svikram } 9134eb2bd662Svikram 9135eb2bd662Svikram free(osdev); 9136eb2bd662Svikram (void) strlcpy(signbuf, sign, sizeof (signbuf)); 9137eb2bd662Svikram free(sign); 9138eb2bd662Svikram assert(strchr(signbuf, '(') == NULL && 9139eb2bd662Svikram strchr(signbuf, ',') == NULL && 9140eb2bd662Svikram strchr(signbuf, ')') == NULL); 9141eb2bd662Svikram 9142ae115bc7Smrj if (optnum == KERNEL_CMD) { 91434e483a70SGangadhar Mylapuram if (strcmp(fstype, "zfs") == 0) { 91444e483a70SGangadhar Mylapuram new_str_len = strlen(new_path) + 91454e483a70SGangadhar Mylapuram strlen(ZFS_BOOT) + 8; 91464e483a70SGangadhar Mylapuram new_arg = s_calloc(1, new_str_len); 91474e483a70SGangadhar Mylapuram (void) snprintf(new_arg, new_str_len, "%s %s", 91484e483a70SGangadhar Mylapuram new_path, ZFS_BOOT); 91494e483a70SGangadhar Mylapuram BAM_DPRINTF((D_GET_SET_KERNEL_NEW_KERN, fcn, 91504e483a70SGangadhar Mylapuram new_arg)); 91514e483a70SGangadhar Mylapuram entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE, 91524e483a70SGangadhar Mylapuram signbuf, new_arg, NULL, NULL, NULL); 91534e483a70SGangadhar Mylapuram free(new_arg); 91544e483a70SGangadhar Mylapuram } else { 91554e483a70SGangadhar Mylapuram BAM_DPRINTF((D_GET_SET_KERNEL_NEW_KERN, fcn, 91564e483a70SGangadhar Mylapuram new_path)); 9157ae115bc7Smrj entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE, 915844da779fSWilliam Kucharski signbuf, new_path, NULL, NULL, NULL); 91594e483a70SGangadhar Mylapuram } 9160ae115bc7Smrj } else { 91614487ce1dSGangadhar Mylapuram new_str_len = strlen(path) + 8; 91624487ce1dSGangadhar Mylapuram if (strcmp(fstype, "zfs") == 0) { 91634487ce1dSGangadhar Mylapuram new_str_len += strlen(DIRECT_BOOT_KERNEL_ZFS); 9164ae115bc7Smrj new_arg = s_calloc(1, new_str_len); 91654487ce1dSGangadhar Mylapuram (void) snprintf(new_arg, new_str_len, "%s %s", 91664487ce1dSGangadhar Mylapuram DIRECT_BOOT_KERNEL_ZFS, path); 91674487ce1dSGangadhar Mylapuram } else { 91684487ce1dSGangadhar Mylapuram new_str_len += strlen(DIRECT_BOOT_KERNEL); 91694487ce1dSGangadhar Mylapuram new_arg = s_calloc(1, new_str_len); 9170ae115bc7Smrj (void) snprintf(new_arg, new_str_len, "%s %s", 9171ae115bc7Smrj DIRECT_BOOT_KERNEL, path); 91724487ce1dSGangadhar Mylapuram } 91734487ce1dSGangadhar Mylapuram 9174eb2bd662Svikram BAM_DPRINTF((D_GET_SET_KERNEL_NEW_ARG, fcn, new_arg)); 9175ae115bc7Smrj entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE, 917644da779fSWilliam Kucharski signbuf, new_arg, NULL, DIRECT_BOOT_ARCHIVE, NULL); 9177eb2bd662Svikram free(new_arg); 9178eb2bd662Svikram } 91794487ce1dSGangadhar Mylapuram free(fstype); 9180eb2bd662Svikram INJECT_ERROR1("GET_SET_KERNEL_ADD_BOOT_ENTRY", 9181eb2bd662Svikram entryNum = BAM_ERROR); 9182eb2bd662Svikram if (entryNum == BAM_ERROR) { 9183eb2bd662Svikram bam_error(GET_SET_KERNEL_ADD_BOOT_ENTRY, 9184eb2bd662Svikram BOOTENV_RC_TITLE); 9185eb2bd662Svikram rv = BAM_ERROR; 9186eb2bd662Svikram goto done; 9187ae115bc7Smrj } 9188ae115bc7Smrj save_default_entry(mp, BAM_OLD_RC_DEF); 9189eb2bd662Svikram ret = set_global(mp, menu_cmds[DEFAULT_CMD], entryNum); 9190eb2bd662Svikram INJECT_ERROR1("GET_SET_KERNEL_SET_GLOBAL", ret = BAM_ERROR); 9191eb2bd662Svikram if (ret == BAM_ERROR) { 9192eb2bd662Svikram bam_error(GET_SET_KERNEL_SET_GLOBAL, entryNum); 9193eb2bd662Svikram } 9194ae115bc7Smrj rv = BAM_WRITE; 9195ae115bc7Smrj goto done; 9196ae115bc7Smrj } 9197ae115bc7Smrj 9198ae115bc7Smrj /* 9199ae115bc7Smrj * There was already an bootenv entry which we need to edit. 9200ae115bc7Smrj */ 9201ae115bc7Smrj if (optnum == KERNEL_CMD) { 9202ae115bc7Smrj new_str_len = strlen(new_path) + strlen(old_args) + 2; 9203ae115bc7Smrj new_arg = s_calloc(1, new_str_len); 9204ae115bc7Smrj (void) snprintf(new_arg, new_str_len, "%s %s", new_path, 9205ae115bc7Smrj old_args); 9206ae115bc7Smrj free(kernelp->arg); 9207ae115bc7Smrj kernelp->arg = new_arg; 9208ae115bc7Smrj 9209ae115bc7Smrj /* 9210ae115bc7Smrj * If we have changed the kernel line, we may need to update 9211ae115bc7Smrj * the archive line as well. 9212ae115bc7Smrj */ 9213ae115bc7Smrj set_archive_line(entryp, kernelp); 9214eb2bd662Svikram BAM_DPRINTF((D_GET_SET_KERNEL_REPLACED_KERNEL_SAME_ARG, fcn, 9215eb2bd662Svikram kernelp->arg)); 9216ae115bc7Smrj } else { 9217ae115bc7Smrj new_str_len = old_kernel_len + strlen(path) + 8; 9218ae115bc7Smrj new_arg = s_calloc(1, new_str_len); 9219ae115bc7Smrj (void) strncpy(new_arg, kernelp->arg, old_kernel_len); 9220ae115bc7Smrj (void) strlcat(new_arg, " ", new_str_len); 9221ae115bc7Smrj (void) strlcat(new_arg, path, new_str_len); 9222ae115bc7Smrj free(kernelp->arg); 9223ae115bc7Smrj kernelp->arg = new_arg; 9224eb2bd662Svikram BAM_DPRINTF((D_GET_SET_KERNEL_SAME_KERNEL_REPLACED_ARG, fcn, 9225eb2bd662Svikram kernelp->arg)); 9226ae115bc7Smrj } 9227ae115bc7Smrj rv = BAM_WRITE; 9228ae115bc7Smrj 9229ae115bc7Smrj done: 9230ae115bc7Smrj if ((rv == BAM_WRITE) && kernelp) 9231ae115bc7Smrj update_line(kernelp); 9232ae115bc7Smrj if (free_new_path) 9233ae115bc7Smrj free(new_path); 9234eb2bd662Svikram if (rv == BAM_WRITE) { 9235eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 9236eb2bd662Svikram } else { 9237eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 9238eb2bd662Svikram } 9239ae115bc7Smrj return (rv); 9240ae115bc7Smrj } 9241ae115bc7Smrj 9242eb2bd662Svikram static error_t 9243eb2bd662Svikram get_kernel(menu_t *mp, menu_cmd_t optnum, char *buf, size_t bufsize) 9244eb2bd662Svikram { 9245eb2bd662Svikram const char *fcn = "get_kernel()"; 9246eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_cmds[optnum])); 9247eb2bd662Svikram return (get_set_kernel(mp, optnum, NULL, buf, bufsize)); 9248eb2bd662Svikram } 9249eb2bd662Svikram 9250eb2bd662Svikram static error_t 9251eb2bd662Svikram set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize) 9252eb2bd662Svikram { 9253eb2bd662Svikram const char *fcn = "set_kernel()"; 9254eb2bd662Svikram assert(path != NULL); 9255eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY2, fcn, menu_cmds[optnum], path)); 9256eb2bd662Svikram return (get_set_kernel(mp, optnum, path, buf, bufsize)); 9257eb2bd662Svikram } 9258eb2bd662Svikram 92597c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 92607c478bd9Sstevel@tonic-gate static error_t 9261eb2bd662Svikram set_option(menu_t *mp, char *dummy, char *opt) 92627c478bd9Sstevel@tonic-gate { 9263eb2bd662Svikram int optnum; 9264eb2bd662Svikram int optval; 92657c478bd9Sstevel@tonic-gate char *val; 9266ae115bc7Smrj char buf[BUFSIZ] = ""; 9267ae115bc7Smrj error_t rv; 9268eb2bd662Svikram const char *fcn = "set_option()"; 92697c478bd9Sstevel@tonic-gate 92707c478bd9Sstevel@tonic-gate assert(mp); 92717c478bd9Sstevel@tonic-gate assert(opt); 9272eb2bd662Svikram assert(dummy == NULL); 9273eb2bd662Svikram 9274eb2bd662Svikram /* opt is set from bam_argv[0] and is always non-NULL */ 9275eb2bd662Svikram BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt)); 92767c478bd9Sstevel@tonic-gate 92777c478bd9Sstevel@tonic-gate val = strchr(opt, '='); 9278ae115bc7Smrj if (val != NULL) { 9279ae115bc7Smrj *val = '\0'; 92807c478bd9Sstevel@tonic-gate } 92817c478bd9Sstevel@tonic-gate 92827c478bd9Sstevel@tonic-gate if (strcmp(opt, "default") == 0) { 92837c478bd9Sstevel@tonic-gate optnum = DEFAULT_CMD; 92847c478bd9Sstevel@tonic-gate } else if (strcmp(opt, "timeout") == 0) { 92857c478bd9Sstevel@tonic-gate optnum = TIMEOUT_CMD; 9286ae115bc7Smrj } else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) { 9287ae115bc7Smrj optnum = KERNEL_CMD; 9288ae115bc7Smrj } else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) { 9289ae115bc7Smrj optnum = ARGS_CMD; 92907c478bd9Sstevel@tonic-gate } else { 9291eb2bd662Svikram bam_error(INVALID_OPTION, opt); 92927c478bd9Sstevel@tonic-gate return (BAM_ERROR); 92937c478bd9Sstevel@tonic-gate } 92947c478bd9Sstevel@tonic-gate 9295ae115bc7Smrj /* 9296ae115bc7Smrj * kernel and args are allowed without "=new_value" strings. All 9297ae115bc7Smrj * others cause errors 9298ae115bc7Smrj */ 9299ae115bc7Smrj if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) { 9300eb2bd662Svikram bam_error(NO_OPTION_ARG, opt); 9301ae115bc7Smrj return (BAM_ERROR); 9302ae115bc7Smrj } else if (val != NULL) { 93037c478bd9Sstevel@tonic-gate *val = '='; 9304ae115bc7Smrj } 9305ae115bc7Smrj 9306ae115bc7Smrj if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) { 9307eb2bd662Svikram BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum], 9308eb2bd662Svikram val ? val + 1 : "NULL")); 9309eb2bd662Svikram 9310eb2bd662Svikram if (val) 9311eb2bd662Svikram rv = set_kernel(mp, optnum, val + 1, buf, sizeof (buf)); 9312eb2bd662Svikram else 9313eb2bd662Svikram rv = get_kernel(mp, optnum, buf, sizeof (buf)); 9314ae115bc7Smrj if ((rv == BAM_SUCCESS) && (buf[0] != '\0')) 9315ae115bc7Smrj (void) printf("%s\n", buf); 9316ae115bc7Smrj } else { 9317ae115bc7Smrj optval = s_strtol(val + 1); 9318eb2bd662Svikram BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum], val + 1)); 9319eb2bd662Svikram rv = set_global(mp, menu_cmds[optnum], optval); 93207c478bd9Sstevel@tonic-gate } 9321eb2bd662Svikram 9322eb2bd662Svikram if (rv == BAM_WRITE || rv == BAM_SUCCESS) { 9323eb2bd662Svikram BAM_DPRINTF((D_RETURN_SUCCESS, fcn)); 9324eb2bd662Svikram } else { 9325eb2bd662Svikram BAM_DPRINTF((D_RETURN_FAILURE, fcn)); 9326eb2bd662Svikram } 9327eb2bd662Svikram 9328eb2bd662Svikram return (rv); 9329ae115bc7Smrj } 93307c478bd9Sstevel@tonic-gate 93317c478bd9Sstevel@tonic-gate /* 93327c478bd9Sstevel@tonic-gate * The quiet argument suppresses messages. This is used 93337c478bd9Sstevel@tonic-gate * when invoked in the context of other commands (e.g. list_entry) 93347c478bd9Sstevel@tonic-gate */ 93357c478bd9Sstevel@tonic-gate static error_t 93367c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet) 93377c478bd9Sstevel@tonic-gate { 93387c478bd9Sstevel@tonic-gate line_t *lp; 93397c478bd9Sstevel@tonic-gate char *arg; 93407c478bd9Sstevel@tonic-gate int done, ret = BAM_SUCCESS; 93417c478bd9Sstevel@tonic-gate 93427c478bd9Sstevel@tonic-gate assert(mp); 93437c478bd9Sstevel@tonic-gate assert(menu_path); 93447c478bd9Sstevel@tonic-gate assert(globalcmd); 93457c478bd9Sstevel@tonic-gate 93467c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 93477c478bd9Sstevel@tonic-gate if (!quiet) 93487c478bd9Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 93497c478bd9Sstevel@tonic-gate return (BAM_ERROR); 93507c478bd9Sstevel@tonic-gate } 93517c478bd9Sstevel@tonic-gate 93527c478bd9Sstevel@tonic-gate done = 0; 93537c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 93547c478bd9Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 93557c478bd9Sstevel@tonic-gate continue; 93567c478bd9Sstevel@tonic-gate 93577c478bd9Sstevel@tonic-gate if (lp->cmd == NULL) { 93587c478bd9Sstevel@tonic-gate if (!quiet) 93597c478bd9Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 93607c478bd9Sstevel@tonic-gate continue; 93617c478bd9Sstevel@tonic-gate } 93627c478bd9Sstevel@tonic-gate 93637c478bd9Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 93647c478bd9Sstevel@tonic-gate continue; 93657c478bd9Sstevel@tonic-gate 93667c478bd9Sstevel@tonic-gate /* Found global. Check for duplicates */ 93677c478bd9Sstevel@tonic-gate if (done && !quiet) { 93687c478bd9Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 93697c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 93707c478bd9Sstevel@tonic-gate } 93717c478bd9Sstevel@tonic-gate 93727c478bd9Sstevel@tonic-gate arg = lp->arg ? lp->arg : ""; 93737c478bd9Sstevel@tonic-gate bam_print(GLOBAL_CMD, globalcmd, arg); 93747c478bd9Sstevel@tonic-gate done = 1; 93757c478bd9Sstevel@tonic-gate } 93767c478bd9Sstevel@tonic-gate 93777c478bd9Sstevel@tonic-gate if (!done && bam_verbose) 93787c478bd9Sstevel@tonic-gate bam_print(NO_ENTRY, globalcmd); 93797c478bd9Sstevel@tonic-gate 93807c478bd9Sstevel@tonic-gate return (ret); 93817c478bd9Sstevel@tonic-gate } 93827c478bd9Sstevel@tonic-gate 93837c478bd9Sstevel@tonic-gate static error_t 93847c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp) 93857c478bd9Sstevel@tonic-gate { 9386eb2bd662Svikram const char *fcn = "menu_write()"; 9387eb2bd662Svikram 9388eb2bd662Svikram BAM_DPRINTF((D_MENU_WRITE_ENTER, fcn, root)); 93897c478bd9Sstevel@tonic-gate return (list2file(root, MENU_TMP, GRUB_MENU, mp->start)); 93907c478bd9Sstevel@tonic-gate } 93917c478bd9Sstevel@tonic-gate 9392eb2bd662Svikram void 93937c478bd9Sstevel@tonic-gate line_free(line_t *lp) 93947c478bd9Sstevel@tonic-gate { 93957c478bd9Sstevel@tonic-gate if (lp == NULL) 93967c478bd9Sstevel@tonic-gate return; 93977c478bd9Sstevel@tonic-gate 9398eac223ccSWilliam Kucharski if (lp->cmd != NULL) 93997c478bd9Sstevel@tonic-gate free(lp->cmd); 94007c478bd9Sstevel@tonic-gate if (lp->sep) 94017c478bd9Sstevel@tonic-gate free(lp->sep); 94027c478bd9Sstevel@tonic-gate if (lp->arg) 94037c478bd9Sstevel@tonic-gate free(lp->arg); 94047c478bd9Sstevel@tonic-gate if (lp->line) 94057c478bd9Sstevel@tonic-gate free(lp->line); 94067c478bd9Sstevel@tonic-gate free(lp); 94077c478bd9Sstevel@tonic-gate } 94087c478bd9Sstevel@tonic-gate 94097c478bd9Sstevel@tonic-gate static void 94107c478bd9Sstevel@tonic-gate linelist_free(line_t *start) 94117c478bd9Sstevel@tonic-gate { 94127c478bd9Sstevel@tonic-gate line_t *lp; 94137c478bd9Sstevel@tonic-gate 94147c478bd9Sstevel@tonic-gate while (start) { 94157c478bd9Sstevel@tonic-gate lp = start; 94167c478bd9Sstevel@tonic-gate start = start->next; 94177c478bd9Sstevel@tonic-gate line_free(lp); 94187c478bd9Sstevel@tonic-gate } 94197c478bd9Sstevel@tonic-gate } 94207c478bd9Sstevel@tonic-gate 94217c478bd9Sstevel@tonic-gate static void 94227c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp) 94237c478bd9Sstevel@tonic-gate { 94247c478bd9Sstevel@tonic-gate linelist_free(flistp->head); 94257c478bd9Sstevel@tonic-gate flistp->head = NULL; 94267c478bd9Sstevel@tonic-gate flistp->tail = NULL; 94277c478bd9Sstevel@tonic-gate } 94287c478bd9Sstevel@tonic-gate 94297c478bd9Sstevel@tonic-gate static void 94307c478bd9Sstevel@tonic-gate menu_free(menu_t *mp) 94317c478bd9Sstevel@tonic-gate { 94328c1b6884Sszhou entry_t *ent, *tmp; 94337c478bd9Sstevel@tonic-gate assert(mp); 94347c478bd9Sstevel@tonic-gate 94357c478bd9Sstevel@tonic-gate if (mp->start) 94367c478bd9Sstevel@tonic-gate linelist_free(mp->start); 94378c1b6884Sszhou ent = mp->entries; 94388c1b6884Sszhou while (ent) { 94398c1b6884Sszhou tmp = ent; 94408c1b6884Sszhou ent = tmp->next; 94418c1b6884Sszhou free(tmp); 94428c1b6884Sszhou } 94437c478bd9Sstevel@tonic-gate 94448c1b6884Sszhou free(mp); 94457c478bd9Sstevel@tonic-gate } 94467c478bd9Sstevel@tonic-gate 94477c478bd9Sstevel@tonic-gate /* 94487c478bd9Sstevel@tonic-gate * Utility routines 94497c478bd9Sstevel@tonic-gate */ 94507c478bd9Sstevel@tonic-gate 94517c478bd9Sstevel@tonic-gate 94527c478bd9Sstevel@tonic-gate /* 94537c478bd9Sstevel@tonic-gate * Returns 0 on success 94547c478bd9Sstevel@tonic-gate * Any other value indicates an error 94557c478bd9Sstevel@tonic-gate */ 94567c478bd9Sstevel@tonic-gate static int 9457986fd29aSsetje exec_cmd(char *cmdline, filelist_t *flistp) 94587c478bd9Sstevel@tonic-gate { 94597c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 94607c478bd9Sstevel@tonic-gate int ret; 94617c478bd9Sstevel@tonic-gate FILE *ptr; 94627c478bd9Sstevel@tonic-gate sigset_t set; 94637c478bd9Sstevel@tonic-gate void (*disp)(int); 94647c478bd9Sstevel@tonic-gate 94657c478bd9Sstevel@tonic-gate /* 94667c478bd9Sstevel@tonic-gate * For security 94677c478bd9Sstevel@tonic-gate * - only absolute paths are allowed 94687c478bd9Sstevel@tonic-gate * - set IFS to space and tab 94697c478bd9Sstevel@tonic-gate */ 94707c478bd9Sstevel@tonic-gate if (*cmdline != '/') { 94717c478bd9Sstevel@tonic-gate bam_error(ABS_PATH_REQ, cmdline); 94727c478bd9Sstevel@tonic-gate return (-1); 94737c478bd9Sstevel@tonic-gate } 94747c478bd9Sstevel@tonic-gate (void) putenv("IFS= \t"); 94757c478bd9Sstevel@tonic-gate 94767c478bd9Sstevel@tonic-gate /* 94777c478bd9Sstevel@tonic-gate * We may have been exec'ed with SIGCHLD blocked 94787c478bd9Sstevel@tonic-gate * unblock it here 94797c478bd9Sstevel@tonic-gate */ 94807c478bd9Sstevel@tonic-gate (void) sigemptyset(&set); 94817c478bd9Sstevel@tonic-gate (void) sigaddset(&set, SIGCHLD); 94827c478bd9Sstevel@tonic-gate if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { 94837c478bd9Sstevel@tonic-gate bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno)); 94847c478bd9Sstevel@tonic-gate return (-1); 94857c478bd9Sstevel@tonic-gate } 94867c478bd9Sstevel@tonic-gate 94877c478bd9Sstevel@tonic-gate /* 94887c478bd9Sstevel@tonic-gate * Set SIGCHLD disposition to SIG_DFL for popen/pclose 94897c478bd9Sstevel@tonic-gate */ 94907c478bd9Sstevel@tonic-gate disp = sigset(SIGCHLD, SIG_DFL); 94917c478bd9Sstevel@tonic-gate if (disp == SIG_ERR) { 94927c478bd9Sstevel@tonic-gate bam_error(FAILED_SIG, strerror(errno)); 94937c478bd9Sstevel@tonic-gate return (-1); 94947c478bd9Sstevel@tonic-gate } 94957c478bd9Sstevel@tonic-gate if (disp == SIG_HOLD) { 94967c478bd9Sstevel@tonic-gate bam_error(BLOCKED_SIG, cmdline); 94977c478bd9Sstevel@tonic-gate return (-1); 94987c478bd9Sstevel@tonic-gate } 94997c478bd9Sstevel@tonic-gate 95007c478bd9Sstevel@tonic-gate ptr = popen(cmdline, "r"); 95017c478bd9Sstevel@tonic-gate if (ptr == NULL) { 95027c478bd9Sstevel@tonic-gate bam_error(POPEN_FAIL, cmdline, strerror(errno)); 95037c478bd9Sstevel@tonic-gate return (-1); 95047c478bd9Sstevel@tonic-gate } 95057c478bd9Sstevel@tonic-gate 95067c478bd9Sstevel@tonic-gate /* 95077c478bd9Sstevel@tonic-gate * If we simply do a pclose() following a popen(), pclose() 95087c478bd9Sstevel@tonic-gate * will close the reader end of the pipe immediately even 95097c478bd9Sstevel@tonic-gate * if the child process has not started/exited. pclose() 95107c478bd9Sstevel@tonic-gate * does wait for cmd to terminate before returning though. 95117c478bd9Sstevel@tonic-gate * When the executed command writes its output to the pipe 95127c478bd9Sstevel@tonic-gate * there is no reader process and the command dies with 95137c478bd9Sstevel@tonic-gate * SIGPIPE. To avoid this we read repeatedly until read 95147c478bd9Sstevel@tonic-gate * terminates with EOF. This indicates that the command 95157c478bd9Sstevel@tonic-gate * (writer) has closed the pipe and we can safely do a 95167c478bd9Sstevel@tonic-gate * pclose(). 95177c478bd9Sstevel@tonic-gate * 95187c478bd9Sstevel@tonic-gate * Since pclose() does wait for the command to exit, 95197c478bd9Sstevel@tonic-gate * we can safely reap the exit status of the command 95207c478bd9Sstevel@tonic-gate * from the value returned by pclose() 95217c478bd9Sstevel@tonic-gate */ 9522986fd29aSsetje while (s_fgets(buf, sizeof (buf), ptr) != NULL) { 9523986fd29aSsetje if (flistp == NULL) { 9524986fd29aSsetje /* s_fgets strips newlines, so insert them at the end */ 9525986fd29aSsetje bam_print(PRINT, buf); 9526986fd29aSsetje } else { 9527986fd29aSsetje append_to_flist(flistp, buf); 95287c478bd9Sstevel@tonic-gate } 95297c478bd9Sstevel@tonic-gate } 95307c478bd9Sstevel@tonic-gate 95317c478bd9Sstevel@tonic-gate ret = pclose(ptr); 95327c478bd9Sstevel@tonic-gate if (ret == -1) { 95337c478bd9Sstevel@tonic-gate bam_error(PCLOSE_FAIL, cmdline, strerror(errno)); 95347c478bd9Sstevel@tonic-gate return (-1); 95357c478bd9Sstevel@tonic-gate } 95367c478bd9Sstevel@tonic-gate 95377c478bd9Sstevel@tonic-gate if (WIFEXITED(ret)) { 95387c478bd9Sstevel@tonic-gate return (WEXITSTATUS(ret)); 95397c478bd9Sstevel@tonic-gate } else { 95407c478bd9Sstevel@tonic-gate bam_error(EXEC_FAIL, cmdline, ret); 95417c478bd9Sstevel@tonic-gate return (-1); 95427c478bd9Sstevel@tonic-gate } 95437c478bd9Sstevel@tonic-gate } 95447c478bd9Sstevel@tonic-gate 95457c478bd9Sstevel@tonic-gate /* 95467c478bd9Sstevel@tonic-gate * Since this function returns -1 on error 95477c478bd9Sstevel@tonic-gate * it cannot be used to convert -1. However, 95487c478bd9Sstevel@tonic-gate * that is sufficient for what we need. 95497c478bd9Sstevel@tonic-gate */ 95507c478bd9Sstevel@tonic-gate static long 95517c478bd9Sstevel@tonic-gate s_strtol(char *str) 95527c478bd9Sstevel@tonic-gate { 95537c478bd9Sstevel@tonic-gate long l; 95547c478bd9Sstevel@tonic-gate char *res = NULL; 95557c478bd9Sstevel@tonic-gate 95567c478bd9Sstevel@tonic-gate if (str == NULL) { 95577c478bd9Sstevel@tonic-gate return (-1); 95587c478bd9Sstevel@tonic-gate } 95597c478bd9Sstevel@tonic-gate 95607c478bd9Sstevel@tonic-gate errno = 0; 95617c478bd9Sstevel@tonic-gate l = strtol(str, &res, 10); 95627c478bd9Sstevel@tonic-gate if (errno || *res != '\0') { 95637c478bd9Sstevel@tonic-gate return (-1); 95647c478bd9Sstevel@tonic-gate } 95657c478bd9Sstevel@tonic-gate 95667c478bd9Sstevel@tonic-gate return (l); 95677c478bd9Sstevel@tonic-gate } 95687c478bd9Sstevel@tonic-gate 95697c478bd9Sstevel@tonic-gate /* 95707c478bd9Sstevel@tonic-gate * Wrapper around fputs, that adds a newline (since fputs doesn't) 95717c478bd9Sstevel@tonic-gate */ 95727c478bd9Sstevel@tonic-gate static int 95737c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp) 95747c478bd9Sstevel@tonic-gate { 95757c478bd9Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 95767c478bd9Sstevel@tonic-gate 95777c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s\n", str); 95787c478bd9Sstevel@tonic-gate return (fputs(linebuf, fp)); 95797c478bd9Sstevel@tonic-gate } 95807c478bd9Sstevel@tonic-gate 95817c478bd9Sstevel@tonic-gate /* 95827c478bd9Sstevel@tonic-gate * Wrapper around fgets, that strips newlines returned by fgets 95837c478bd9Sstevel@tonic-gate */ 9584ae115bc7Smrj char * 95857c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp) 95867c478bd9Sstevel@tonic-gate { 95877c478bd9Sstevel@tonic-gate int n; 95887c478bd9Sstevel@tonic-gate 95897c478bd9Sstevel@tonic-gate buf = fgets(buf, buflen, fp); 95907c478bd9Sstevel@tonic-gate if (buf) { 95917c478bd9Sstevel@tonic-gate n = strlen(buf); 95927c478bd9Sstevel@tonic-gate if (n == buflen - 1 && buf[n-1] != '\n') 95937c478bd9Sstevel@tonic-gate bam_error(TOO_LONG, buflen - 1, buf); 95947c478bd9Sstevel@tonic-gate buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1]; 95957c478bd9Sstevel@tonic-gate } 95967c478bd9Sstevel@tonic-gate 95977c478bd9Sstevel@tonic-gate return (buf); 95987c478bd9Sstevel@tonic-gate } 95997c478bd9Sstevel@tonic-gate 9600ae115bc7Smrj void * 96017c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz) 96027c478bd9Sstevel@tonic-gate { 96037c478bd9Sstevel@tonic-gate void *ptr; 96047c478bd9Sstevel@tonic-gate 96057c478bd9Sstevel@tonic-gate ptr = calloc(nelem, sz); 96067c478bd9Sstevel@tonic-gate if (ptr == NULL) { 96077c478bd9Sstevel@tonic-gate bam_error(NO_MEM, nelem*sz); 96087c478bd9Sstevel@tonic-gate bam_exit(1); 96097c478bd9Sstevel@tonic-gate } 96107c478bd9Sstevel@tonic-gate return (ptr); 96117c478bd9Sstevel@tonic-gate } 96127c478bd9Sstevel@tonic-gate 9613ae115bc7Smrj void * 9614ae115bc7Smrj s_realloc(void *ptr, size_t sz) 9615ae115bc7Smrj { 9616ae115bc7Smrj ptr = realloc(ptr, sz); 9617ae115bc7Smrj if (ptr == NULL) { 9618ae115bc7Smrj bam_error(NO_MEM, sz); 9619ae115bc7Smrj bam_exit(1); 9620ae115bc7Smrj } 9621ae115bc7Smrj return (ptr); 9622ae115bc7Smrj } 9623ae115bc7Smrj 9624eb2bd662Svikram char * 96257c478bd9Sstevel@tonic-gate s_strdup(char *str) 96267c478bd9Sstevel@tonic-gate { 96277c478bd9Sstevel@tonic-gate char *ptr; 96287c478bd9Sstevel@tonic-gate 96297c478bd9Sstevel@tonic-gate if (str == NULL) 96307c478bd9Sstevel@tonic-gate return (NULL); 96317c478bd9Sstevel@tonic-gate 96327c478bd9Sstevel@tonic-gate ptr = strdup(str); 96337c478bd9Sstevel@tonic-gate if (ptr == NULL) { 96347c478bd9Sstevel@tonic-gate bam_error(NO_MEM, strlen(str) + 1); 96357c478bd9Sstevel@tonic-gate bam_exit(1); 96367c478bd9Sstevel@tonic-gate } 96377c478bd9Sstevel@tonic-gate return (ptr); 96387c478bd9Sstevel@tonic-gate } 96397c478bd9Sstevel@tonic-gate 96407c478bd9Sstevel@tonic-gate /* 96417c478bd9Sstevel@tonic-gate * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients) 96427c478bd9Sstevel@tonic-gate * Returns 0 otherwise 96437c478bd9Sstevel@tonic-gate */ 96447c478bd9Sstevel@tonic-gate static int 96457c478bd9Sstevel@tonic-gate is_amd64(void) 96467c478bd9Sstevel@tonic-gate { 96477c478bd9Sstevel@tonic-gate static int amd64 = -1; 96487c478bd9Sstevel@tonic-gate char isabuf[257]; /* from sysinfo(2) manpage */ 96497c478bd9Sstevel@tonic-gate 96507c478bd9Sstevel@tonic-gate if (amd64 != -1) 96517c478bd9Sstevel@tonic-gate return (amd64); 96527c478bd9Sstevel@tonic-gate 9653d876c67dSjg if (bam_alt_platform) { 9654d876c67dSjg if (strcmp(bam_platform, "i86pc") == 0) { 96557c478bd9Sstevel@tonic-gate amd64 = 1; /* diskless server */ 9656d876c67dSjg } 9657d876c67dSjg } else { 9658d876c67dSjg if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 && 9659d876c67dSjg strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) { 9660d876c67dSjg amd64 = 1; 9661d876c67dSjg } else if (strstr(isabuf, "i386") == NULL) { 9662d876c67dSjg amd64 = 1; /* diskless server */ 9663d876c67dSjg } 9664d876c67dSjg } 9665d876c67dSjg if (amd64 == -1) 96667c478bd9Sstevel@tonic-gate amd64 = 0; 96677c478bd9Sstevel@tonic-gate 96687c478bd9Sstevel@tonic-gate return (amd64); 96697c478bd9Sstevel@tonic-gate } 96707c478bd9Sstevel@tonic-gate 967179755401Ssetje static char * 967279755401Ssetje get_machine(void) 9673986fd29aSsetje { 967479755401Ssetje static int cached = -1; 967579755401Ssetje static char mbuf[257]; /* from sysinfo(2) manpage */ 9676986fd29aSsetje 967779755401Ssetje if (cached == 0) 967879755401Ssetje return (mbuf); 9679d876c67dSjg 9680d876c67dSjg if (bam_alt_platform) { 968179755401Ssetje return (bam_platform); 9682d876c67dSjg } else { 968379755401Ssetje if (sysinfo(SI_MACHINE, mbuf, sizeof (mbuf)) > 0) { 968479755401Ssetje cached = 1; 9685d876c67dSjg } 9686d876c67dSjg } 968779755401Ssetje if (cached == -1) { 968879755401Ssetje mbuf[0] = '\0'; 968979755401Ssetje cached = 0; 9690986fd29aSsetje } 9691986fd29aSsetje 969279755401Ssetje return (mbuf); 9693986fd29aSsetje } 9694986fd29aSsetje 9695eb2bd662Svikram int 9696eb2bd662Svikram is_sparc(void) 9697eb2bd662Svikram { 969879755401Ssetje static int issparc = -1; 969979755401Ssetje char mbuf[257]; /* from sysinfo(2) manpage */ 970079755401Ssetje 970179755401Ssetje if (issparc != -1) 970279755401Ssetje return (issparc); 970379755401Ssetje 970479755401Ssetje if (bam_alt_platform) { 970579755401Ssetje if (strncmp(bam_platform, "sun4", 4) == 0) { 970679755401Ssetje issparc = 1; 970779755401Ssetje } 970879755401Ssetje } else { 970979755401Ssetje if (sysinfo(SI_ARCHITECTURE, mbuf, sizeof (mbuf)) > 0 && 9710d7d0f93bSjg strcmp(mbuf, "sparc") == 0) { 971179755401Ssetje issparc = 1; 971279755401Ssetje } 9713d7d0f93bSjg } 9714d7d0f93bSjg if (issparc == -1) 9715d7d0f93bSjg issparc = 0; 971679755401Ssetje 971779755401Ssetje return (issparc); 9718eb2bd662Svikram } 9719986fd29aSsetje 97207c478bd9Sstevel@tonic-gate static void 97217c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s) 97227c478bd9Sstevel@tonic-gate { 97237c478bd9Sstevel@tonic-gate line_t *lp; 97247c478bd9Sstevel@tonic-gate 97257c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 97267c478bd9Sstevel@tonic-gate lp->line = s_strdup(s); 97277c478bd9Sstevel@tonic-gate if (flistp->head == NULL) 97287c478bd9Sstevel@tonic-gate flistp->head = lp; 97297c478bd9Sstevel@tonic-gate else 97307c478bd9Sstevel@tonic-gate flistp->tail->next = lp; 97317c478bd9Sstevel@tonic-gate flistp->tail = lp; 97327c478bd9Sstevel@tonic-gate } 97332449e17fSsherrym 973479c28b70SToomas Soome #if !defined(_OBP) 97352449e17fSsherrym 97362449e17fSsherrym UCODE_VENDORS; 97372449e17fSsherrym 97382449e17fSsherrym /*ARGSUSED*/ 97392449e17fSsherrym static void 97402449e17fSsherrym ucode_install(char *root) 97412449e17fSsherrym { 97422449e17fSsherrym int i; 97432449e17fSsherrym 97442449e17fSsherrym for (i = 0; ucode_vendors[i].filestr != NULL; i++) { 97452449e17fSsherrym int cmd_len = PATH_MAX + 256; 97462449e17fSsherrym char cmd[PATH_MAX + 256]; 97472449e17fSsherrym char file[PATH_MAX]; 97482449e17fSsherrym char timestamp[PATH_MAX]; 97492449e17fSsherrym struct stat fstatus, tstatus; 97502449e17fSsherrym struct utimbuf u_times; 97512449e17fSsherrym 9752adc586deSMark Johnson (void) snprintf(file, PATH_MAX, "%s/%s/%s-ucode.%s", 9753adc586deSMark Johnson bam_root, UCODE_INSTALL_PATH, ucode_vendors[i].filestr, 9754adc586deSMark Johnson ucode_vendors[i].extstr); 97552449e17fSsherrym 97562449e17fSsherrym if (stat(file, &fstatus) != 0 || !(S_ISREG(fstatus.st_mode))) 97572449e17fSsherrym continue; 97582449e17fSsherrym 97592449e17fSsherrym (void) snprintf(timestamp, PATH_MAX, "%s.ts", file); 97602449e17fSsherrym 97612449e17fSsherrym if (stat(timestamp, &tstatus) == 0 && 97622449e17fSsherrym fstatus.st_mtime <= tstatus.st_mtime) 97632449e17fSsherrym continue; 97642449e17fSsherrym 97652449e17fSsherrym (void) snprintf(cmd, cmd_len, "/usr/sbin/ucodeadm -i -R " 97662449e17fSsherrym "%s/%s/%s %s > /dev/null 2>&1", bam_root, 97672449e17fSsherrym UCODE_INSTALL_PATH, ucode_vendors[i].vendorstr, file); 97682449e17fSsherrym if (system(cmd) != 0) 97692449e17fSsherrym return; 97702449e17fSsherrym 97712449e17fSsherrym if (creat(timestamp, S_IRUSR | S_IWUSR) == -1) 97722449e17fSsherrym return; 97732449e17fSsherrym 97742449e17fSsherrym u_times.actime = fstatus.st_atime; 97752449e17fSsherrym u_times.modtime = fstatus.st_mtime; 97762449e17fSsherrym (void) utime(timestamp, &u_times); 97772449e17fSsherrym } 97782449e17fSsherrym } 97792449e17fSsherrym #endif 9780