xref: /titanic_50/usr/src/cmd/boot/bootadm/bootadm.c (revision 1130146c106aae1a60b30bcc30521647a9474892)
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 /*
22eff168e0SEnrico Perla - Sun Microsystems  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * bootadm(1M) is a new utility for managing bootability of
287c478bd9Sstevel@tonic-gate  * Solaris *Newboot* environments. It has two primary tasks:
297c478bd9Sstevel@tonic-gate  * 	- Allow end users to manage bootability of Newboot Solaris instances
307c478bd9Sstevel@tonic-gate  *	- Provide services to other subsystems in Solaris (primarily Install)
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /* Headers */
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
4144da779fSWilliam Kucharski #include <alloca.h>
427c478bd9Sstevel@tonic-gate #include <stdarg.h>
437c478bd9Sstevel@tonic-gate #include <limits.h>
447c478bd9Sstevel@tonic-gate #include <signal.h>
457c478bd9Sstevel@tonic-gate #include <sys/wait.h>
467c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
47f904d32dSJerry Gilliam #include <sys/mntent.h>
487c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
497c478bd9Sstevel@tonic-gate #include <libnvpair.h>
507c478bd9Sstevel@tonic-gate #include <ftw.h>
517c478bd9Sstevel@tonic-gate #include <fcntl.h>
527c478bd9Sstevel@tonic-gate #include <strings.h>
532449e17fSsherrym #include <utime.h>
547c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
557c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
5658091fd8Ssetje #include <sys/param.h>
57eb2bd662Svikram #include <dirent.h>
58eb2bd662Svikram #include <ctype.h>
59eb2bd662Svikram #include <libgen.h>
60e7cbe64fSgw25295 #include <sys/sysmacros.h>
6148847494SEnrico Perla - Sun Microsystems #include <sys/elf.h>
62963390b4Svikram #include <libscf.h>
6348847494SEnrico Perla - Sun Microsystems #include <zlib.h>
6448847494SEnrico Perla - Sun Microsystems #include <sys/lockfs.h>
6548847494SEnrico Perla - Sun Microsystems #include <sys/filio.h>
66e998e519SSheshadri Vasudevan #ifdef i386
67e998e519SSheshadri Vasudevan #include <libfdisk.h>
68e998e519SSheshadri Vasudevan #endif
69986fd29aSsetje 
70986fd29aSsetje #if !defined(_OPB)
712449e17fSsherrym #include <sys/ucode.h>
722449e17fSsherrym #endif
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #include <pwd.h>
757c478bd9Sstevel@tonic-gate #include <grp.h>
767c478bd9Sstevel@tonic-gate #include <device_info.h>
77eb2bd662Svikram #include <sys/vtoc.h>
78eb2bd662Svikram #include <sys/efi_partition.h>
7989c3ee43SGangadhar Mylapuram #include <regex.h>
807c478bd9Sstevel@tonic-gate #include <locale.h>
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #include "message.h"
83ae115bc7Smrj #include "bootadm.h"
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
867c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
877c478bd9Sstevel@tonic-gate #endif	/* TEXT_DOMAIN */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /* Type definitions */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /* Primary subcmds */
927c478bd9Sstevel@tonic-gate typedef enum {
937c478bd9Sstevel@tonic-gate 	BAM_MENU = 3,
947c478bd9Sstevel@tonic-gate 	BAM_ARCHIVE
957c478bd9Sstevel@tonic-gate } subcmd_t;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate typedef enum {
987c478bd9Sstevel@tonic-gate     OPT_ABSENT = 0,	/* No option */
997c478bd9Sstevel@tonic-gate     OPT_REQ,		/* option required */
1007c478bd9Sstevel@tonic-gate     OPT_OPTIONAL	/* option may or may not be present */
1017c478bd9Sstevel@tonic-gate } option_t;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate typedef struct {
1047c478bd9Sstevel@tonic-gate 	char	*subcmd;
1057c478bd9Sstevel@tonic-gate 	option_t option;
1067c478bd9Sstevel@tonic-gate 	error_t (*handler)();
1071a97e40eSvikram 	int	unpriv;			/* is this an unprivileged command */
1087c478bd9Sstevel@tonic-gate } subcmd_defn_t;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
1117c478bd9Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
1127c478bd9Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #define	GRUB_DIR		"/boot/grub"
115963390b4Svikram #define	GRUB_STAGE2		GRUB_DIR "/stage2"
1167c478bd9Sstevel@tonic-gate #define	GRUB_MENU		"/boot/grub/menu.lst"
1177c478bd9Sstevel@tonic-gate #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
118963390b4Svikram #define	GRUB_BACKUP_MENU	"/etc/lu/GRUB_backup_menu"
1197c478bd9Sstevel@tonic-gate #define	RAMDISK_SPECIAL		"/ramdisk"
12040541d5dSvikram #define	STUBBOOT		"/stubboot"
121eb2bd662Svikram #define	MULTIBOOT		"/platform/i86pc/multiboot"
122eb2bd662Svikram #define	GRUBSIGN_DIR		"/boot/grub/bootsign"
123eb2bd662Svikram #define	GRUBSIGN_BACKUP		"/etc/bootsign"
124eb2bd662Svikram #define	GRUBSIGN_UFS_PREFIX	"rootfs"
125eb2bd662Svikram #define	GRUBSIGN_ZFS_PREFIX	"pool_"
126eb2bd662Svikram #define	GRUBSIGN_LU_PREFIX	"BE_"
127eb2bd662Svikram #define	UFS_SIGNATURE_LIST	"/var/run/grub_ufs_signatures"
128eb2bd662Svikram #define	ZFS_LEGACY_MNTPT	"/tmp/bootadm_mnt_zfs_legacy"
129eb2bd662Svikram 
130eb2bd662Svikram #define	BOOTADM_RDONLY_TEST	"BOOTADM_RDONLY_TEST"
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /* lock related */
1337c478bd9Sstevel@tonic-gate #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
1347c478bd9Sstevel@tonic-gate #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1357c478bd9Sstevel@tonic-gate 
136986fd29aSsetje #define	CREATE_RAMDISK		"boot/solaris/bin/create_ramdisk"
137986fd29aSsetje #define	CREATE_DISKMAP		"boot/solaris/bin/create_diskmap"
138986fd29aSsetje #define	EXTRACT_BOOT_FILELIST	"boot/solaris/bin/extract_boot_filelist"
1397c478bd9Sstevel@tonic-gate #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
1407c478bd9Sstevel@tonic-gate 
141b610f78eSvikram #define	GRUB_slice		"/etc/lu/GRUB_slice"
142b610f78eSvikram #define	GRUB_root		"/etc/lu/GRUB_root"
143fbac2b2bSvikram #define	GRUB_fdisk		"/etc/lu/GRUB_fdisk"
144fbac2b2bSvikram #define	GRUB_fdisk_target	"/etc/lu/GRUB_fdisk_target"
145963390b4Svikram #define	FINDROOT_INSTALLGRUB	"/etc/lu/installgrub.findroot"
146963390b4Svikram #define	LULIB			"/usr/lib/lu/lulib"
147963390b4Svikram #define	LULIB_PROPAGATE_FILE	"lulib_propagate_file"
148963390b4Svikram #define	CKSUM			"/usr/bin/cksum"
149963390b4Svikram #define	LU_MENU_CKSUM		"/etc/lu/menu.cksum"
150963390b4Svikram #define	BOOTADM			"/sbin/bootadm"
151b610f78eSvikram 
152b610f78eSvikram #define	INSTALLGRUB		"/sbin/installgrub"
153b610f78eSvikram #define	STAGE1			"/boot/grub/stage1"
154b610f78eSvikram #define	STAGE2			"/boot/grub/stage2"
155b610f78eSvikram 
156eb2bd662Svikram typedef enum zfs_mnted {
157eb2bd662Svikram 	ZFS_MNT_ERROR = -1,
158eb2bd662Svikram 	LEGACY_MOUNTED = 1,
159eb2bd662Svikram 	LEGACY_ALREADY,
160eb2bd662Svikram 	ZFS_MOUNTED,
161eb2bd662Svikram 	ZFS_ALREADY
162eb2bd662Svikram } zfs_mnted_t;
163eb2bd662Svikram 
1647c478bd9Sstevel@tonic-gate /*
16598892a30Snadkarni  * The following two defines are used to detect and create the correct
16698892a30Snadkarni  * boot archive  when safemode patching is underway.  LOFS_PATCH_FILE is a
16798892a30Snadkarni  * contracted private interface between bootadm and the install
16898892a30Snadkarni  * consolidation.  It is set by pdo.c when a patch with SUNW_PATCH_SAFEMODE
16998892a30Snadkarni  * is applied.
17098892a30Snadkarni  */
17198892a30Snadkarni #define	LOFS_PATCH_FILE		"/var/run/.patch_loopback_mode"
17298892a30Snadkarni #define	LOFS_PATCH_MNT		"/var/run/.patch_root_loopbackmnt"
17398892a30Snadkarni 
17498892a30Snadkarni /*
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;
2387c478bd9Sstevel@tonic-gate static int bam_argc;
2397c478bd9Sstevel@tonic-gate static int bam_check;
2405eea6091SEnrico Perla - Sun Microsystems static int bam_saved_check;
2417c478bd9Sstevel@tonic-gate static int bam_smf_check;
2427c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
243e7cbe64fSgw25295 static int bam_zfs;
2447c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
245b610f78eSvikram static int bam_update_all;
246d876c67dSjg static int bam_alt_platform;
247d876c67dSjg static char *bam_platform;
248cedc7e57SEnrico Perla - Sun Microsystems static char *bam_home_env = NULL;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /* function prototypes */
251986fd29aSsetje static void parse_args_internal(int, char *[]);
252986fd29aSsetje static void parse_args(int, char *argv[]);
253986fd29aSsetje static error_t bam_menu(char *, char *, int, char *[]);
254986fd29aSsetje static error_t bam_archive(char *, char *);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static void bam_lock(void);
2577c478bd9Sstevel@tonic-gate static void bam_unlock(void);
2587c478bd9Sstevel@tonic-gate 
259986fd29aSsetje static int exec_cmd(char *, filelist_t *);
260986fd29aSsetje static error_t read_globals(menu_t *, char *, char *, int);
261eb2bd662Svikram static int menu_on_bootdisk(char *os_root, char *menu_root);
262986fd29aSsetje static menu_t *menu_read(char *);
263986fd29aSsetje static error_t menu_write(char *, menu_t *);
264986fd29aSsetje static void linelist_free(line_t *);
265986fd29aSsetje static void menu_free(menu_t *);
266986fd29aSsetje static void filelist_free(filelist_t *);
267986fd29aSsetje static error_t list2file(char *, char *, char *, line_t *);
268986fd29aSsetje static error_t list_entry(menu_t *, char *, char *);
26944da779fSWilliam Kucharski static error_t list_setting(menu_t *, char *, char *);
270986fd29aSsetje static error_t delete_all_entries(menu_t *, char *, char *);
271eb2bd662Svikram static error_t update_entry(menu_t *mp, char *menu_root, char *opt);
272eb2bd662Svikram static error_t update_temp(menu_t *mp, char *dummy, char *opt);
2737c478bd9Sstevel@tonic-gate 
274986fd29aSsetje static error_t update_archive(char *, char *);
275986fd29aSsetje static error_t list_archive(char *, char *);
276986fd29aSsetje static error_t update_all(char *, char *);
277986fd29aSsetje static error_t read_list(char *, filelist_t *);
278986fd29aSsetje static error_t set_option(menu_t *, char *, char *);
279986fd29aSsetje static error_t set_kernel(menu_t *, menu_cmd_t, char *, char *, size_t);
280eb2bd662Svikram static error_t get_kernel(menu_t *, menu_cmd_t, char *, size_t);
281986fd29aSsetje static char *expand_path(const char *);
2827c478bd9Sstevel@tonic-gate 
283986fd29aSsetje static long s_strtol(char *);
284986fd29aSsetje static int s_fputs(char *, FILE *);
2857c478bd9Sstevel@tonic-gate 
286eb2bd662Svikram static int is_zfs(char *root);
287eb2bd662Svikram static int is_ufs(char *root);
288eb2bd662Svikram static int is_pcfs(char *root);
2897c478bd9Sstevel@tonic-gate static int is_amd64(void);
29079755401Ssetje static char *get_machine(void);
2917c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
292eb2bd662Svikram static char *mount_top_dataset(char *pool, zfs_mnted_t *mnted);
293eb2bd662Svikram static int umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt);
294eb2bd662Svikram static int ufs_add_to_sign_list(char *sign);
295963390b4Svikram static error_t synchronize_BE_menu(void);
2967c478bd9Sstevel@tonic-gate 
297986fd29aSsetje #if !defined(_OPB)
2982449e17fSsherrym static void ucode_install();
2992449e17fSsherrym #endif
3002449e17fSsherrym 
3017c478bd9Sstevel@tonic-gate /* Menu related sub commands */
3027c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
303eb2bd662Svikram 	"set_option",		OPT_ABSENT,	set_option, 0,	/* PUB */
3041a97e40eSvikram 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
3051a97e40eSvikram 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
3061a97e40eSvikram 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
3071a97e40eSvikram 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
308ae115bc7Smrj 	"upgrade",		OPT_ABSENT,	upgrade_menu, 0, /* menu */
30944da779fSWilliam Kucharski 	"list_setting",		OPT_OPTIONAL,	list_setting, 1, /* menu */
31044da779fSWilliam Kucharski 	"disable_hypervisor",	OPT_ABSENT,	cvt_to_metal, 0, /* menu */
31144da779fSWilliam Kucharski 	"enable_hypervisor",	OPT_ABSENT,	cvt_to_hyper, 0, /* menu */
3121a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
3137c478bd9Sstevel@tonic-gate };
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate /* Archive related sub commands */
3167c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
3171a97e40eSvikram 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
3181a97e40eSvikram 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
3191a97e40eSvikram 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
3201a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
3217c478bd9Sstevel@tonic-gate };
3227c478bd9Sstevel@tonic-gate 
32348847494SEnrico Perla - Sun Microsystems enum dircache_copy_opt {
32448847494SEnrico Perla - Sun Microsystems 	FILE32 = 0,
32548847494SEnrico Perla - Sun Microsystems 	FILE64,
32648847494SEnrico Perla - Sun Microsystems 	CACHEDIR_NUM
32748847494SEnrico Perla - Sun Microsystems };
32848847494SEnrico Perla - Sun Microsystems 
32948847494SEnrico Perla - Sun Microsystems /*
33048847494SEnrico Perla - Sun Microsystems  * Directory specific flags:
33148847494SEnrico Perla - Sun Microsystems  * NEED_UPDATE : the specified archive needs to be updated
33248847494SEnrico Perla - Sun Microsystems  * NO_MULTI : don't extend the specified archive, but recreate it
33348847494SEnrico Perla - Sun Microsystems  */
33448847494SEnrico Perla - Sun Microsystems #define	NEED_UPDATE		0x00000001
33548847494SEnrico Perla - Sun Microsystems #define	NO_MULTI		0x00000002
33648847494SEnrico Perla - Sun Microsystems 
33748847494SEnrico Perla - Sun Microsystems #define	set_dir_flag(id, f)	(walk_arg.dirinfo[id].flags |= f)
33848847494SEnrico Perla - Sun Microsystems #define	unset_dir_flag(id, f)	(walk_arg.dirinfo[id].flags &= ~f)
33948847494SEnrico Perla - Sun Microsystems #define	is_dir_flag_on(id, f)	(walk_arg.dirinfo[id].flags & f ? 1 : 0)
34048847494SEnrico Perla - Sun Microsystems 
34148847494SEnrico Perla - Sun Microsystems #define	get_cachedir(id)	(walk_arg.dirinfo[id].cdir_path)
34248847494SEnrico Perla - Sun Microsystems #define	get_updatedir(id)	(walk_arg.dirinfo[id].update_path)
34348847494SEnrico Perla - Sun Microsystems #define	get_count(id)		(walk_arg.dirinfo[id].count)
34448847494SEnrico Perla - Sun Microsystems #define	has_cachedir(id)	(walk_arg.dirinfo[id].has_dir)
34548847494SEnrico Perla - Sun Microsystems #define	set_dir_present(id)	(walk_arg.dirinfo[id].has_dir = 1)
34648847494SEnrico Perla - Sun Microsystems 
34748847494SEnrico Perla - Sun Microsystems /*
34848847494SEnrico Perla - Sun Microsystems  * dirinfo_t (specific cache directory information):
34948847494SEnrico Perla - Sun Microsystems  * cdir_path:   path to the archive cache directory
35048847494SEnrico Perla - Sun Microsystems  * update_path: path to the update directory (contains the files that will be
35148847494SEnrico Perla - Sun Microsystems  *              used to extend the archive)
35248847494SEnrico Perla - Sun Microsystems  * has_dir:	the specified cache directory is active
35348847494SEnrico Perla - Sun Microsystems  * count:	the number of files to update
35448847494SEnrico Perla - Sun Microsystems  * flags:	directory specific flags
35548847494SEnrico Perla - Sun Microsystems  */
35648847494SEnrico Perla - Sun Microsystems typedef struct _dirinfo {
35748847494SEnrico Perla - Sun Microsystems 	char	cdir_path[PATH_MAX];
35848847494SEnrico Perla - Sun Microsystems 	char	update_path[PATH_MAX];
35948847494SEnrico Perla - Sun Microsystems 	int	has_dir;
36048847494SEnrico Perla - Sun Microsystems 	int	count;
36148847494SEnrico Perla - Sun Microsystems 	int	flags;
36248847494SEnrico Perla - Sun Microsystems } dirinfo_t;
36348847494SEnrico Perla - Sun Microsystems 
36448847494SEnrico Perla - Sun Microsystems /*
36548847494SEnrico Perla - Sun Microsystems  * Update flags:
36648847494SEnrico Perla - Sun Microsystems  * NEED_CACHE_DIR : cache directory is missing and needs to be created
36748847494SEnrico Perla - Sun Microsystems  * IS_SPARC_TARGET : the target mountpoint is a SPARC environment
36848847494SEnrico Perla - Sun Microsystems  * UPDATE_ERROR : an error occourred while traversing the list of files
36948847494SEnrico Perla - Sun Microsystems  * RDONLY_FSCHK : the target filesystem is read-only
37048847494SEnrico Perla - Sun Microsystems  * RAMDSK_FSCHK : the target filesystem is on a ramdisk
37148847494SEnrico Perla - Sun Microsystems  */
37248847494SEnrico Perla - Sun Microsystems #define	NEED_CACHE_DIR		0x00000001
37348847494SEnrico Perla - Sun Microsystems #define	IS_SPARC_TARGET		0x00000002
37448847494SEnrico Perla - Sun Microsystems #define	UPDATE_ERROR		0x00000004
37548847494SEnrico Perla - Sun Microsystems #define	RDONLY_FSCHK		0x00000008
3764a9df875SEnrico Perla - Sun Microsystems #define	INVALIDATE_CACHE	0x00000010
37748847494SEnrico Perla - Sun Microsystems 
37848847494SEnrico Perla - Sun Microsystems #define	is_flag_on(flag)	(walk_arg.update_flags & flag ? 1 : 0)
37948847494SEnrico Perla - Sun Microsystems #define	set_flag(flag)		(walk_arg.update_flags |= flag)
38048847494SEnrico Perla - Sun Microsystems #define	unset_flag(flag)	(walk_arg.update_flags &= ~flag)
38148847494SEnrico Perla - Sun Microsystems 
38248847494SEnrico Perla - Sun Microsystems /*
38348847494SEnrico Perla - Sun Microsystems  * struct walk_arg :
38448847494SEnrico Perla - Sun Microsystems  * update_flags: flags related to the current updating process
38548847494SEnrico Perla - Sun Microsystems  * new_nvlp/old_nvlp: new and old list of archive-files / attributes pairs
38648847494SEnrico Perla - Sun Microsystems  * sparcfile: list of file paths for mkisofs -path-list (SPARC only)
38748847494SEnrico Perla - Sun Microsystems  */
3887c478bd9Sstevel@tonic-gate static struct {
38948847494SEnrico Perla - Sun Microsystems 	int 		update_flags;
3907c478bd9Sstevel@tonic-gate 	nvlist_t 	*new_nvlp;
3917c478bd9Sstevel@tonic-gate 	nvlist_t 	*old_nvlp;
39248847494SEnrico Perla - Sun Microsystems 	FILE 		*sparcfile;
39348847494SEnrico Perla - Sun Microsystems 	dirinfo_t	dirinfo[CACHEDIR_NUM];
3947c478bd9Sstevel@tonic-gate } walk_arg;
3957c478bd9Sstevel@tonic-gate 
3969632cfadSsetje struct safefile {
39758091fd8Ssetje 	char *name;
39858091fd8Ssetje 	struct safefile *next;
39958091fd8Ssetje };
40058091fd8Ssetje 
401ae115bc7Smrj static struct safefile *safefiles = NULL;
40258091fd8Ssetje #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
40358091fd8Ssetje 
40448847494SEnrico Perla - Sun Microsystems /* Thanks growisofs */
40548847494SEnrico Perla - Sun Microsystems #define	CD_BLOCK	((off64_t)2048)
40648847494SEnrico Perla - Sun Microsystems #define	VOLDESC_OFF	16
40748847494SEnrico Perla - Sun Microsystems #define	DVD_BLOCK	(32*1024)
40848847494SEnrico Perla - Sun Microsystems #define	MAX_IVDs	16
40948847494SEnrico Perla - Sun Microsystems 
41048847494SEnrico Perla - Sun Microsystems struct iso_pdesc {
41148847494SEnrico Perla - Sun Microsystems     unsigned char type	[1];
41248847494SEnrico Perla - Sun Microsystems     unsigned char id	[5];
41348847494SEnrico Perla - Sun Microsystems     unsigned char void1	[80-5-1];
41448847494SEnrico Perla - Sun Microsystems     unsigned char volume_space_size [8];
41548847494SEnrico Perla - Sun Microsystems     unsigned char void2	[2048-80-8];
41648847494SEnrico Perla - Sun Microsystems };
41748847494SEnrico Perla - Sun Microsystems 
41848847494SEnrico Perla - Sun Microsystems /*
41948847494SEnrico Perla - Sun Microsystems  * COUNT_MAX:	maximum number of changed files to justify a multisession update
42048847494SEnrico Perla - Sun Microsystems  * BA_SIZE_MAX:	maximum size of the boot_archive to justify a multisession
42148847494SEnrico Perla - Sun Microsystems  * 		update
42248847494SEnrico Perla - Sun Microsystems  */
42348847494SEnrico Perla - Sun Microsystems #define	COUNT_MAX		50
42448847494SEnrico Perla - Sun Microsystems #define	BA_SIZE_MAX		(50 * 1024 * 1024)
42548847494SEnrico Perla - Sun Microsystems 
42648847494SEnrico Perla - Sun Microsystems #define	bam_nowrite()		(bam_check || bam_smf_check)
42748847494SEnrico Perla - Sun Microsystems 
42819397407SSherry Moore static int sync_menu = 1;	/* whether we need to sync the BE menus */
42919397407SSherry Moore 
4307c478bd9Sstevel@tonic-gate static void
4317c478bd9Sstevel@tonic-gate usage(void)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/* archive usage */
436d876c67dSjg 	(void) fprintf(stderr,
437d876c67dSjg 	    "\t%s update-archive [-vn] [-R altroot [-p platform>]]\n", prog);
438d876c67dSjg 	(void) fprintf(stderr,
439d876c67dSjg 	    "\t%s list-archive [-R altroot [-p platform>]]\n", prog);
440986fd29aSsetje #if !defined(_OPB)
4417c478bd9Sstevel@tonic-gate 	/* x86 only */
4427c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
4437c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
4447c478bd9Sstevel@tonic-gate #endif
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
447cedc7e57SEnrico Perla - Sun Microsystems /*
448cedc7e57SEnrico Perla - Sun Microsystems  * Best effort attempt to restore the $HOME value.
449cedc7e57SEnrico Perla - Sun Microsystems  */
450cedc7e57SEnrico Perla - Sun Microsystems static void
451cedc7e57SEnrico Perla - Sun Microsystems restore_env()
452cedc7e57SEnrico Perla - Sun Microsystems {
453cedc7e57SEnrico Perla - Sun Microsystems 	char	home_env[PATH_MAX];
454cedc7e57SEnrico Perla - Sun Microsystems 
455cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_home_env) {
456cedc7e57SEnrico Perla - Sun Microsystems 		(void) snprintf(home_env, sizeof (home_env), "HOME=%s",
457cedc7e57SEnrico Perla - Sun Microsystems 		    bam_home_env);
458cedc7e57SEnrico Perla - Sun Microsystems 		(void) putenv(home_env);
459cedc7e57SEnrico Perla - Sun Microsystems 	}
460cedc7e57SEnrico Perla - Sun Microsystems }
461cedc7e57SEnrico Perla - Sun Microsystems 
462cedc7e57SEnrico Perla - Sun Microsystems 
463cedc7e57SEnrico Perla - Sun Microsystems #define		SLEEP_TIME	5
464cedc7e57SEnrico Perla - Sun Microsystems #define		MAX_TRIES	4
465cedc7e57SEnrico Perla - Sun Microsystems 
466cedc7e57SEnrico Perla - Sun Microsystems /*
467cedc7e57SEnrico Perla - Sun Microsystems  * Sanitize the environment in which bootadm will execute its sub-processes
468cedc7e57SEnrico Perla - Sun Microsystems  * (ex. mkisofs). This is done to prevent those processes from attempting
469cedc7e57SEnrico Perla - Sun Microsystems  * to access files (ex. .mkisofsrc) or stat paths that might be on NFS
470cedc7e57SEnrico Perla - Sun Microsystems  * or, potentially, insecure.
471cedc7e57SEnrico Perla - Sun Microsystems  */
472cedc7e57SEnrico Perla - Sun Microsystems static void
473cedc7e57SEnrico Perla - Sun Microsystems sanitize_env()
474cedc7e57SEnrico Perla - Sun Microsystems {
475cedc7e57SEnrico Perla - Sun Microsystems 	int	stry = 0;
476cedc7e57SEnrico Perla - Sun Microsystems 
477cedc7e57SEnrico Perla - Sun Microsystems 	/* don't depend on caller umask */
478cedc7e57SEnrico Perla - Sun Microsystems 	(void) umask(0022);
479cedc7e57SEnrico Perla - Sun Microsystems 
480cedc7e57SEnrico Perla - Sun Microsystems 	/* move away from a potential unsafe current working directory */
481cedc7e57SEnrico Perla - Sun Microsystems 	while (chdir("/") == -1) {
482cedc7e57SEnrico Perla - Sun Microsystems 		if (errno != EINTR) {
483cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to chdir to /");
484cedc7e57SEnrico Perla - Sun Microsystems 			break;
485cedc7e57SEnrico Perla - Sun Microsystems 		}
486cedc7e57SEnrico Perla - Sun Microsystems 	}
487cedc7e57SEnrico Perla - Sun Microsystems 
488cedc7e57SEnrico Perla - Sun Microsystems 	bam_home_env = getenv("HOME");
489cedc7e57SEnrico Perla - Sun Microsystems 	while (bam_home_env != NULL && putenv("HOME=/") == -1) {
490cedc7e57SEnrico Perla - Sun Microsystems 		if (errno == ENOMEM) {
491cedc7e57SEnrico Perla - Sun Microsystems 			/* retry no more than MAX_TRIES times */
492cedc7e57SEnrico Perla - Sun Microsystems 			if (++stry > MAX_TRIES) {
493cedc7e57SEnrico Perla - Sun Microsystems 				bam_print("WARNING: unable to recover from "
494cedc7e57SEnrico Perla - Sun Microsystems 				    "system memory pressure... aborting \n");
495cedc7e57SEnrico Perla - Sun Microsystems 				bam_exit(EXIT_FAILURE);
496cedc7e57SEnrico Perla - Sun Microsystems 			}
497cedc7e57SEnrico Perla - Sun Microsystems 			/* memory is tight, try to sleep */
498cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("Attempting to recover from memory pressure: "
499cedc7e57SEnrico Perla - Sun Microsystems 			    "sleeping for %d seconds\n", SLEEP_TIME * stry);
500cedc7e57SEnrico Perla - Sun Microsystems 			(void) sleep(SLEEP_TIME * stry);
501cedc7e57SEnrico Perla - Sun Microsystems 		} else {
502cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to sanitize HOME\n");
503cedc7e57SEnrico Perla - Sun Microsystems 		}
504cedc7e57SEnrico Perla - Sun Microsystems 	}
505cedc7e57SEnrico Perla - Sun Microsystems }
506cedc7e57SEnrico Perla - Sun Microsystems 
5077c478bd9Sstevel@tonic-gate int
5087c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	error_t ret;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5137c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
5167c478bd9Sstevel@tonic-gate 		prog = argv[0];
5177c478bd9Sstevel@tonic-gate 	} else {
5187c478bd9Sstevel@tonic-gate 		prog++;
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
521eb2bd662Svikram 	INJECT_ERROR1("ASSERT_ON", assert(0))
5227c478bd9Sstevel@tonic-gate 
523cedc7e57SEnrico Perla - Sun Microsystems 	sanitize_env();
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
5287c478bd9Sstevel@tonic-gate 		case BAM_MENU:
5297c478bd9Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
5307c478bd9Sstevel@tonic-gate 			break;
5317c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
5327c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
5337c478bd9Sstevel@tonic-gate 			break;
5347c478bd9Sstevel@tonic-gate 		default:
5357c478bd9Sstevel@tonic-gate 			usage();
5367c478bd9Sstevel@tonic-gate 			bam_exit(1);
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
54044da779fSWilliam Kucharski 		bam_exit((ret == BAM_NOCHANGE) ? 2 : 1);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	bam_unlock();
5437c478bd9Sstevel@tonic-gate 	return (0);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
5487c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
5497c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
5507c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
5517c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
5527c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
5537c478bd9Sstevel@tonic-gate  */
5547c478bd9Sstevel@tonic-gate static struct cmd_map {
5557c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
5567c478bd9Sstevel@tonic-gate 	int bam_cmd;
5577c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
5587c478bd9Sstevel@tonic-gate } cmd_map[] = {
5597c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
5607c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
5617c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
5627c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
5637c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
5647c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
5657c478bd9Sstevel@tonic-gate };
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate static void
5717c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
5767c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
5777c478bd9Sstevel@tonic-gate 		/*
5787c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
5797c478bd9Sstevel@tonic-gate 		 */
5807c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
5817c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
5827c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
5837c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
5847c478bd9Sstevel@tonic-gate 				break;
5857c478bd9Sstevel@tonic-gate 			}
5867c478bd9Sstevel@tonic-gate 			cmp++;
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
5897c478bd9Sstevel@tonic-gate 			usage();
5907c478bd9Sstevel@tonic-gate 			bam_exit(1);
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 		argc--;
5937c478bd9Sstevel@tonic-gate 		argv++;
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate /*
6007c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
6017c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
6027c478bd9Sstevel@tonic-gate  *	-a update			-- update-archive
6037c478bd9Sstevel@tonic-gate  *	-a list				-- list-archive
60444da779fSWilliam Kucharski  *	-a update-all			-- (reboot to sync all mnted OS archive)
6057c478bd9Sstevel@tonic-gate  *	-m update_entry			-- update-menu
6067c478bd9Sstevel@tonic-gate  *	-m list_entry			-- list-menu
6077c478bd9Sstevel@tonic-gate  *	-m update_temp			-- (reboot -- [boot-args])
6087c478bd9Sstevel@tonic-gate  *	-m delete_all_entries		-- (called from install)
60944da779fSWilliam Kucharski  *	-m enable_hypervisor [args]	-- cvt_to_hyper
61044da779fSWilliam Kucharski  *	-m disable_hypervisor		-- cvt_to_metal
61144da779fSWilliam Kucharski  *	-m list_setting [entry] [value]	-- list_setting
61244da779fSWilliam Kucharski  *
61348847494SEnrico Perla - Sun Microsystems  * A set of private flags is there too:
61448847494SEnrico Perla - Sun Microsystems  *	-F		-- purge the cache directories and rebuild them
61548847494SEnrico Perla - Sun Microsystems  *	-e		-- use the (faster) archive update approach (used by
61648847494SEnrico Perla - Sun Microsystems  *			   reboot)
6177c478bd9Sstevel@tonic-gate  */
6187c478bd9Sstevel@tonic-gate static void
6197c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	int c, error;
6227c478bd9Sstevel@tonic-gate 	extern char *optarg;
6237c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
6267c478bd9Sstevel@tonic-gate 	opterr = 0;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	error = 0;
6293028dfd6SFrank Van Der Linden 	while ((c = getopt(argc, argv, "a:d:fm:no:veFCR:p:XZ")) != -1) {
6307c478bd9Sstevel@tonic-gate 		switch (c) {
6317c478bd9Sstevel@tonic-gate 		case 'a':
6327c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
6337c478bd9Sstevel@tonic-gate 				error = 1;
6347c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
6357c478bd9Sstevel@tonic-gate 			}
6367c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
6377c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
6387c478bd9Sstevel@tonic-gate 			break;
6397c478bd9Sstevel@tonic-gate 		case 'd':
6407c478bd9Sstevel@tonic-gate 			if (bam_debug) {
6417c478bd9Sstevel@tonic-gate 				error = 1;
6427c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
6437c478bd9Sstevel@tonic-gate 			}
6447c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
6457c478bd9Sstevel@tonic-gate 			break;
6467c478bd9Sstevel@tonic-gate 		case 'f':
6477c478bd9Sstevel@tonic-gate 			bam_force = 1;
6487c478bd9Sstevel@tonic-gate 			break;
64948847494SEnrico Perla - Sun Microsystems 		case 'F':
65048847494SEnrico Perla - Sun Microsystems 			bam_purge = 1;
65148847494SEnrico Perla - Sun Microsystems 			break;
6527c478bd9Sstevel@tonic-gate 		case 'm':
6537c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
6547c478bd9Sstevel@tonic-gate 				error = 1;
6557c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
6567c478bd9Sstevel@tonic-gate 			}
6577c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
6587c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
6597c478bd9Sstevel@tonic-gate 			break;
6607c478bd9Sstevel@tonic-gate 		case 'n':
6617c478bd9Sstevel@tonic-gate 			bam_check = 1;
6625eea6091SEnrico Perla - Sun Microsystems 			/*
6635eea6091SEnrico Perla - Sun Microsystems 			 * We save the original value of bam_check. The new
6645eea6091SEnrico Perla - Sun Microsystems 			 * approach in case of a read-only filesystem is to
6655eea6091SEnrico Perla - Sun Microsystems 			 * behave as a check, so we need a way to restore the
6665eea6091SEnrico Perla - Sun Microsystems 			 * original value after the evaluation of the read-only
6675eea6091SEnrico Perla - Sun Microsystems 			 * filesystem has been done.
6685eea6091SEnrico Perla - Sun Microsystems 			 * Even if we don't allow at the moment a check with
6695eea6091SEnrico Perla - Sun Microsystems 			 * update_all, this approach is more robust than
6705eea6091SEnrico Perla - Sun Microsystems 			 * simply resetting bam_check to zero.
6715eea6091SEnrico Perla - Sun Microsystems 			 */
6725eea6091SEnrico Perla - Sun Microsystems 			bam_saved_check = 1;
6737c478bd9Sstevel@tonic-gate 			break;
6747c478bd9Sstevel@tonic-gate 		case 'o':
6757c478bd9Sstevel@tonic-gate 			if (bam_opt) {
6767c478bd9Sstevel@tonic-gate 				error = 1;
6777c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
6787c478bd9Sstevel@tonic-gate 			}
6797c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
6807c478bd9Sstevel@tonic-gate 			break;
6817c478bd9Sstevel@tonic-gate 		case 'v':
6827c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
6837c478bd9Sstevel@tonic-gate 			break;
6848c1b6884Sszhou 		case 'C':
6858c1b6884Sszhou 			bam_smf_check = 1;
6868c1b6884Sszhou 			break;
6877c478bd9Sstevel@tonic-gate 		case 'R':
6887c478bd9Sstevel@tonic-gate 			if (bam_root) {
6897c478bd9Sstevel@tonic-gate 				error = 1;
6907c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
6917c478bd9Sstevel@tonic-gate 				break;
6927c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
6937c478bd9Sstevel@tonic-gate 				error = 1;
6947c478bd9Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
6957c478bd9Sstevel@tonic-gate 				    strerror(errno));
6967c478bd9Sstevel@tonic-gate 				break;
6977c478bd9Sstevel@tonic-gate 			}
69840541d5dSvikram 			bam_alt_root = 1;
6997c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
7007c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
7017c478bd9Sstevel@tonic-gate 			break;
702d876c67dSjg 		case 'p':
703d876c67dSjg 			bam_alt_platform = 1;
704d876c67dSjg 			bam_platform = optarg;
705d876c67dSjg 			if ((strcmp(bam_platform, "i86pc") != 0) &&
706d876c67dSjg 			    (strcmp(bam_platform, "sun4u") != 0) &&
707d876c67dSjg 			    (strcmp(bam_platform, "sun4v") != 0)) {
708d876c67dSjg 				error = 1;
709d876c67dSjg 				bam_error(INVALID_PLAT, bam_platform);
710d876c67dSjg 			}
711d876c67dSjg 			break;
7123028dfd6SFrank Van Der Linden 		case 'X':
7133028dfd6SFrank Van Der Linden 			bam_is_hv = BAM_HV_PRESENT;
7143028dfd6SFrank Van Der Linden 			break;
715e7cbe64fSgw25295 		case 'Z':
716e7cbe64fSgw25295 			bam_zfs = 1;
717e7cbe64fSgw25295 			break;
71848847494SEnrico Perla - Sun Microsystems 		case 'e':
71948847494SEnrico Perla - Sun Microsystems 			bam_extend = 1;
72048847494SEnrico Perla - Sun Microsystems 			break;
7217c478bd9Sstevel@tonic-gate 		case '?':
7227c478bd9Sstevel@tonic-gate 			error = 1;
7237c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
7247c478bd9Sstevel@tonic-gate 			break;
7257c478bd9Sstevel@tonic-gate 		default :
7267c478bd9Sstevel@tonic-gate 			error = 1;
7277c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
7287c478bd9Sstevel@tonic-gate 			break;
7297c478bd9Sstevel@tonic-gate 		}
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/*
733d876c67dSjg 	 * An alternate platform requires an alternate root
734d876c67dSjg 	 */
735d876c67dSjg 	if (bam_alt_platform && bam_alt_root == 0) {
736d876c67dSjg 		usage();
737d876c67dSjg 		bam_exit(0);
738d876c67dSjg 	}
739d876c67dSjg 
740d876c67dSjg 	/*
7417c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
7427c478bd9Sstevel@tonic-gate 	 */
7437c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
7447c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
7457c478bd9Sstevel@tonic-gate 			usage();
7467c478bd9Sstevel@tonic-gate 			bam_exit(0);
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 		bam_error(NEED_CMD);
7497c478bd9Sstevel@tonic-gate 		error = 1;
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	if (error) {
7537c478bd9Sstevel@tonic-gate 		usage();
7547c478bd9Sstevel@tonic-gate 		bam_exit(1);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (optind > argc) {
7587c478bd9Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
7597c478bd9Sstevel@tonic-gate 		bam_exit(1);
7607c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
7617c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
7627c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	/*
7667c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
7677c478bd9Sstevel@tonic-gate 	 */
7687c478bd9Sstevel@tonic-gate 	if (bam_check)
7697c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate static error_t
7737c478bd9Sstevel@tonic-gate check_subcmd_and_options(
7747c478bd9Sstevel@tonic-gate 	char *subcmd,
7757c478bd9Sstevel@tonic-gate 	char *opt,
7767c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
7777c478bd9Sstevel@tonic-gate 	error_t (**fp)())
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate 	int i;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
7827c478bd9Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
7837c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 
786eb2bd662Svikram 	if (strcmp(subcmd, "set_option") == 0) {
787eb2bd662Svikram 		if (bam_argc == 0 || bam_argv == NULL || bam_argv[0] == NULL) {
788eb2bd662Svikram 			bam_error(MISSING_ARG);
789eb2bd662Svikram 			usage();
790eb2bd662Svikram 			return (BAM_ERROR);
791eb2bd662Svikram 		} else if (bam_argc > 1 || bam_argv[1] != NULL) {
7921a97e40eSvikram 			bam_error(TRAILING_ARGS);
7931a97e40eSvikram 			usage();
7941a97e40eSvikram 			return (BAM_ERROR);
7951a97e40eSvikram 		}
79619397407SSherry Moore 	} else if (strcmp(subcmd, "update_all") == 0) {
79719397407SSherry Moore 		/*
79819397407SSherry Moore 		 * The only option we accept for the "update_all"
79919397407SSherry Moore 		 * subcmd is "fastboot".
80019397407SSherry Moore 		 */
80119397407SSherry Moore 		if (bam_argc > 1 || (bam_argc == 1 &&
80219397407SSherry Moore 		    strcmp(bam_argv[0], "fastboot") != 0)) {
80319397407SSherry Moore 			bam_error(TRAILING_ARGS);
80419397407SSherry Moore 			usage();
80519397407SSherry Moore 			return (BAM_ERROR);
80619397407SSherry Moore 		}
80719397407SSherry Moore 		if (bam_argc == 1)
80819397407SSherry Moore 			sync_menu = 0;
80944da779fSWilliam Kucharski 	} else if (((strcmp(subcmd, "enable_hypervisor") != 0) &&
81044da779fSWilliam Kucharski 	    (strcmp(subcmd, "list_setting") != 0)) && (bam_argc || bam_argv)) {
81144da779fSWilliam Kucharski 		/*
81244da779fSWilliam Kucharski 		 * Of the remaining subcommands, only "enable_hypervisor" and
81344da779fSWilliam Kucharski 		 * "list_setting" take trailing arguments.
81444da779fSWilliam Kucharski 		 */
815eb2bd662Svikram 		bam_error(TRAILING_ARGS);
816eb2bd662Svikram 		usage();
817eb2bd662Svikram 		return (BAM_ERROR);
8181a97e40eSvikram 	}
8191a97e40eSvikram 
8207c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
8217c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
8227c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
8267c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
8277c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
8287c478bd9Sstevel@tonic-gate 			break;
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
8327c478bd9Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
8337c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 
8361a97e40eSvikram 	if (table[i].unpriv == 0 && geteuid() != 0) {
8371a97e40eSvikram 		bam_error(MUST_BE_ROOT);
8381a97e40eSvikram 		return (BAM_ERROR);
8391a97e40eSvikram 	}
8401a97e40eSvikram 
8411a97e40eSvikram 	/*
8421a97e40eSvikram 	 * Currently only privileged commands need a lock
8431a97e40eSvikram 	 */
8441a97e40eSvikram 	if (table[i].unpriv == 0)
8451a97e40eSvikram 		bam_lock();
8461a97e40eSvikram 
8477c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
8487c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
8497c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
8507c478bd9Sstevel@tonic-gate 			if (opt)
8517c478bd9Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
8527c478bd9Sstevel@tonic-gate 			else
8537c478bd9Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
8547c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
8557c478bd9Sstevel@tonic-gate 		}
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate 
86340541d5dSvikram /*
86440541d5dSvikram  * NOTE: A single "/" is also considered a trailing slash and will
86540541d5dSvikram  * be deleted.
86640541d5dSvikram  */
86740541d5dSvikram static void
86840541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
86940541d5dSvikram {
87040541d5dSvikram 	size_t dstlen;
87140541d5dSvikram 
87240541d5dSvikram 	assert(src);
87340541d5dSvikram 	assert(dst);
87440541d5dSvikram 
87540541d5dSvikram 	(void) strlcpy(dst, src, dstsize);
87640541d5dSvikram 
87740541d5dSvikram 	dstlen = strlen(dst);
87840541d5dSvikram 	if (dst[dstlen - 1] == '/') {
87940541d5dSvikram 		dst[dstlen - 1] = '\0';
88040541d5dSvikram 	}
88140541d5dSvikram }
88240541d5dSvikram 
883cedc7e57SEnrico Perla - Sun Microsystems static int
884cedc7e57SEnrico Perla - Sun Microsystems is_safe_exec(char *path)
885cedc7e57SEnrico Perla - Sun Microsystems {
886cedc7e57SEnrico Perla - Sun Microsystems 	struct stat	sb;
887cedc7e57SEnrico Perla - Sun Microsystems 
888cedc7e57SEnrico Perla - Sun Microsystems 	if (lstat(path, &sb) != 0) {
889cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(STAT_FAIL, path, strerror(errno));
890cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
891cedc7e57SEnrico Perla - Sun Microsystems 	}
892cedc7e57SEnrico Perla - Sun Microsystems 
893cedc7e57SEnrico Perla - Sun Microsystems 	if (!S_ISREG(sb.st_mode)) {
894cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_LINK, path);
895cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
896cedc7e57SEnrico Perla - Sun Microsystems 	}
897cedc7e57SEnrico Perla - Sun Microsystems 
898cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_uid != getuid()) {
899cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_OWNER, path, getuid());
900cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
901cedc7e57SEnrico Perla - Sun Microsystems 	}
902cedc7e57SEnrico Perla - Sun Microsystems 
903cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_mode & S_IWOTH || sb.st_mode & S_IWGRP) {
904cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_PERMS, path);
905cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
906cedc7e57SEnrico Perla - Sun Microsystems 	}
907cedc7e57SEnrico Perla - Sun Microsystems 
908cedc7e57SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
909cedc7e57SEnrico Perla - Sun Microsystems }
910cedc7e57SEnrico Perla - Sun Microsystems 
9117c478bd9Sstevel@tonic-gate static error_t
91244da779fSWilliam Kucharski list_setting(menu_t *mp, char *which, char *setting)
91344da779fSWilliam Kucharski {
91444da779fSWilliam Kucharski 	line_t	*lp;
91544da779fSWilliam Kucharski 	entry_t	*ent;
91644da779fSWilliam Kucharski 
91744da779fSWilliam Kucharski 	char	*p = which;
91844da779fSWilliam Kucharski 	int	entry;
91944da779fSWilliam Kucharski 
92044da779fSWilliam Kucharski 	int	found;
92144da779fSWilliam Kucharski 
92244da779fSWilliam Kucharski 	assert(which);
92344da779fSWilliam Kucharski 	assert(setting);
92444da779fSWilliam Kucharski 
92544da779fSWilliam Kucharski 	if (*which != NULL) {
92644da779fSWilliam Kucharski 		/*
92744da779fSWilliam Kucharski 		 * If "which" is not a number, assume it's a setting we want
92844da779fSWilliam Kucharski 		 * to look for and so set up the routine to look for "which"
92944da779fSWilliam Kucharski 		 * in the default entry.
93044da779fSWilliam Kucharski 		 */
93144da779fSWilliam Kucharski 		while (*p != NULL)
93244da779fSWilliam Kucharski 			if (!(isdigit((int)*p++))) {
93344da779fSWilliam Kucharski 				setting = which;
93444da779fSWilliam Kucharski 				which = mp->curdefault->arg;
93544da779fSWilliam Kucharski 				break;
93644da779fSWilliam Kucharski 			}
93744da779fSWilliam Kucharski 	} else {
93844da779fSWilliam Kucharski 		which = mp->curdefault->arg;
93944da779fSWilliam Kucharski 	}
94044da779fSWilliam Kucharski 
94144da779fSWilliam Kucharski 	entry = atoi(which);
94244da779fSWilliam Kucharski 
94344da779fSWilliam Kucharski 	for (ent = mp->entries; ((ent != NULL) && (ent->entryNum != entry));
94444da779fSWilliam Kucharski 	    ent = ent->next)
94544da779fSWilliam Kucharski 		;
94644da779fSWilliam Kucharski 
94744da779fSWilliam Kucharski 	if (!ent) {
94844da779fSWilliam Kucharski 		bam_error(NO_MATCH_ENTRY);
94944da779fSWilliam Kucharski 		return (BAM_ERROR);
95044da779fSWilliam Kucharski 	}
95144da779fSWilliam Kucharski 
95244da779fSWilliam Kucharski 	found = (*setting == NULL);
95344da779fSWilliam Kucharski 
95444da779fSWilliam Kucharski 	for (lp = ent->start; lp != NULL; lp = lp->next) {
95544da779fSWilliam Kucharski 		if ((*setting == NULL) && (lp->flags != BAM_COMMENT))
95644da779fSWilliam Kucharski 			bam_print(PRINT, lp->line);
957eac223ccSWilliam Kucharski 		else if (lp->cmd != NULL && strcmp(setting, lp->cmd) == 0) {
95844da779fSWilliam Kucharski 			bam_print(PRINT, lp->arg);
95944da779fSWilliam Kucharski 			found = 1;
96044da779fSWilliam Kucharski 		}
96144da779fSWilliam Kucharski 
96244da779fSWilliam Kucharski 		if (lp == ent->end)
96344da779fSWilliam Kucharski 			break;
96444da779fSWilliam Kucharski 	}
96544da779fSWilliam Kucharski 
96644da779fSWilliam Kucharski 	if (!found) {
96744da779fSWilliam Kucharski 		bam_error(NO_MATCH_ENTRY);
96844da779fSWilliam Kucharski 		return (BAM_ERROR);
96944da779fSWilliam Kucharski 	}
97044da779fSWilliam Kucharski 
97144da779fSWilliam Kucharski 	return (BAM_SUCCESS);
97244da779fSWilliam Kucharski }
97344da779fSWilliam Kucharski 
97444da779fSWilliam Kucharski static error_t
9757c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	error_t			ret;
9787c478bd9Sstevel@tonic-gate 	char			menu_path[PATH_MAX];
979eb2bd662Svikram 	char			clean_menu_root[PATH_MAX];
980ae115bc7Smrj 	char			path[PATH_MAX];
9817c478bd9Sstevel@tonic-gate 	menu_t			*menu;
982eb2bd662Svikram 	char			menu_root[PATH_MAX];
983b610f78eSvikram 	struct stat		sb;
9847c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
985eb2bd662Svikram 	char			*special;
986eb2bd662Svikram 	char			*pool = NULL;
987eb2bd662Svikram 	zfs_mnted_t		zmnted;
988eb2bd662Svikram 	char			*zmntpt;
989eb2bd662Svikram 	char			*osdev;
990eb2bd662Svikram 	char			*osroot;
991eb2bd662Svikram 	const char		*fcn = "bam_menu()";
992986fd29aSsetje 
993986fd29aSsetje 	/*
994986fd29aSsetje 	 * Menu sub-command only applies to GRUB (i.e. x86)
995986fd29aSsetje 	 */
996eb2bd662Svikram 	if (!is_grub(bam_alt_root ? bam_root : "/")) {
997eb2bd662Svikram 		bam_error(NOT_GRUB_BOOT);
998986fd29aSsetje 		return (BAM_ERROR);
999986fd29aSsetje 	}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	/*
10027c478bd9Sstevel@tonic-gate 	 * Check arguments
10037c478bd9Sstevel@tonic-gate 	 */
10047c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
10057c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
10067c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 
1009eb2bd662Svikram 	assert(bam_root);
1010eb2bd662Svikram 
1011eb2bd662Svikram 	(void) strlcpy(menu_root, bam_root, sizeof (menu_root));
1012eb2bd662Svikram 	osdev = osroot = NULL;
1013eb2bd662Svikram 
1014eb2bd662Svikram 	if (strcmp(subcmd, "update_entry") == 0) {
1015eb2bd662Svikram 		assert(opt);
1016eb2bd662Svikram 
1017eb2bd662Svikram 		osdev = strtok(opt, ",");
1018eb2bd662Svikram 		assert(osdev);
1019eb2bd662Svikram 		osroot = strtok(NULL, ",");
1020eb2bd662Svikram 		if (osroot) {
1021eb2bd662Svikram 			/* fixup bam_root so that it points at osroot */
1022eb2bd662Svikram 			if (realpath(osroot, rootbuf) == NULL) {
1023eb2bd662Svikram 				bam_error(CANT_RESOLVE, osroot,
1024eb2bd662Svikram 				    strerror(errno));
1025eb2bd662Svikram 				return (BAM_ERROR);
1026eb2bd662Svikram 			}
1027eb2bd662Svikram 			bam_alt_root = 1;
1028eb2bd662Svikram 			bam_root  = rootbuf;
1029eb2bd662Svikram 			bam_rootlen = strlen(rootbuf);
1030eb2bd662Svikram 		}
1031eb2bd662Svikram 	}
103240541d5dSvikram 
103340541d5dSvikram 	/*
1034eb2bd662Svikram 	 * We support menu on PCFS (under certain conditions), but
1035eb2bd662Svikram 	 * not the OS root
103640541d5dSvikram 	 */
1037eb2bd662Svikram 	if (is_pcfs(bam_root)) {
1038eb2bd662Svikram 		bam_error(PCFS_ROOT_NOTSUP, bam_root);
1039eb2bd662Svikram 		return (BAM_ERROR);
1040ae115bc7Smrj 	}
1041ae115bc7Smrj 
1042eb2bd662Svikram 	if (stat(menu_root, &sb) == -1) {
104340541d5dSvikram 		bam_error(CANNOT_LOCATE_GRUB_MENU);
104440541d5dSvikram 		return (BAM_ERROR);
104540541d5dSvikram 	}
104640541d5dSvikram 
1047eb2bd662Svikram 	BAM_DPRINTF((D_MENU_ROOT, fcn, menu_root));
1048e7cbe64fSgw25295 
1049eb2bd662Svikram 	/*
1050eb2bd662Svikram 	 * We no longer use the GRUB slice file. If it exists, then
1051eb2bd662Svikram 	 * the user is doing something that is unsupported (such as
1052eb2bd662Svikram 	 * standard upgrading an old Live Upgrade BE). If that
1053eb2bd662Svikram 	 * happens, mimic existing behavior i.e. pretend that it is
1054eb2bd662Svikram 	 * not a BE. Emit a warning though.
1055eb2bd662Svikram 	 */
1056eb2bd662Svikram 	if (bam_alt_root) {
1057eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s%s", bam_root,
1058eb2bd662Svikram 		    GRUB_slice);
1059eb2bd662Svikram 	} else {
1060eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s", GRUB_slice);
1061e7cbe64fSgw25295 	}
1062e7cbe64fSgw25295 
106337eb779cSVikram Hegde 	if (bam_verbose && stat(path, &sb) == 0)
1064eb2bd662Svikram 		bam_error(GRUB_SLICE_FILE_EXISTS, path);
1065eb2bd662Svikram 
1066eb2bd662Svikram 	if (is_zfs(menu_root)) {
1067eb2bd662Svikram 		assert(strcmp(menu_root, bam_root) == 0);
1068eb2bd662Svikram 		special = get_special(menu_root);
1069eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_GET_SPECIAL", special = NULL);
1070eb2bd662Svikram 		if (special == NULL) {
1071eb2bd662Svikram 			bam_error(CANT_FIND_SPECIAL, menu_root);
1072eb2bd662Svikram 			return (BAM_ERROR);
1073eb2bd662Svikram 		}
1074eb2bd662Svikram 		pool = strtok(special, "/");
1075eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_GET_POOL", pool = NULL);
1076eb2bd662Svikram 		if (pool == NULL) {
1077eb2bd662Svikram 			free(special);
1078eb2bd662Svikram 			bam_error(CANT_FIND_POOL, menu_root);
1079eb2bd662Svikram 			return (BAM_ERROR);
1080eb2bd662Svikram 		}
1081eb2bd662Svikram 		BAM_DPRINTF((D_Z_MENU_GET_POOL_FROM_SPECIAL, fcn, pool));
1082eb2bd662Svikram 
1083eb2bd662Svikram 		zmntpt = mount_top_dataset(pool, &zmnted);
1084eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_MOUNT_TOP_DATASET", zmntpt = NULL);
1085eb2bd662Svikram 		if (zmntpt == NULL) {
1086eb2bd662Svikram 			bam_error(CANT_MOUNT_POOL_DATASET, pool);
1087eb2bd662Svikram 			free(special);
1088eb2bd662Svikram 			return (BAM_ERROR);
1089eb2bd662Svikram 		}
1090eb2bd662Svikram 		BAM_DPRINTF((D_Z_GET_MENU_MOUNT_TOP_DATASET, fcn, zmntpt));
1091eb2bd662Svikram 
1092eb2bd662Svikram 		(void) strlcpy(menu_root, zmntpt, sizeof (menu_root));
1093eb2bd662Svikram 		BAM_DPRINTF((D_Z_GET_MENU_MENU_ROOT, fcn, menu_root));
1094eb2bd662Svikram 	}
1095eb2bd662Svikram 
1096eb2bd662Svikram 	elide_trailing_slash(menu_root, clean_menu_root,
1097eb2bd662Svikram 	    sizeof (clean_menu_root));
1098eb2bd662Svikram 
1099eb2bd662Svikram 	BAM_DPRINTF((D_CLEAN_MENU_ROOT, fcn, clean_menu_root));
1100eb2bd662Svikram 
1101eb2bd662Svikram 	(void) strlcpy(menu_path, clean_menu_root, sizeof (menu_path));
110240541d5dSvikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
110340541d5dSvikram 
1104eb2bd662Svikram 	BAM_DPRINTF((D_MENU_PATH, fcn, menu_path));
1105eb2bd662Svikram 
110640541d5dSvikram 	/*
1107eb2bd662Svikram 	 * If listing the menu, display the menu location
110840541d5dSvikram 	 */
110944da779fSWilliam Kucharski 	if (strcmp(subcmd, "list_entry") == 0)
1110eb2bd662Svikram 		bam_print(GRUB_MENU_PATH, menu_path);
111144da779fSWilliam Kucharski 
111244da779fSWilliam Kucharski 	if ((menu = menu_read(menu_path)) == NULL) {
111344da779fSWilliam Kucharski 		bam_error(CANNOT_LOCATE_GRUB_MENU_FILE, menu_path);
111444da779fSWilliam Kucharski 		if (special != NULL)
111544da779fSWilliam Kucharski 			free(special);
111644da779fSWilliam Kucharski 
111744da779fSWilliam Kucharski 		return (BAM_ERROR);
111840541d5dSvikram 	}
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	/*
1121eb2bd662Svikram 	 * We already checked the following case in
1122eb2bd662Svikram 	 * check_subcmd_and_suboptions() above. Complete the
1123eb2bd662Svikram 	 * final step now.
11247c478bd9Sstevel@tonic-gate 	 */
11257c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
1126eb2bd662Svikram 		assert(largc == 1 && largv[0] && largv[1] == NULL);
11277c478bd9Sstevel@tonic-gate 		opt = largv[0];
112844da779fSWilliam Kucharski 	} else if ((strcmp(subcmd, "enable_hypervisor") != 0) &&
112944da779fSWilliam Kucharski 	    (strcmp(subcmd, "list_setting") != 0)) {
1130eb2bd662Svikram 		assert(largc == 0 && largv == NULL);
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 
1133eb2bd662Svikram 	ret = get_boot_cap(bam_root);
1134eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1135eb2bd662Svikram 		BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn));
1136eb2bd662Svikram 		goto out;
1137eb2bd662Svikram 	}
1138ae115bc7Smrj 
11397c478bd9Sstevel@tonic-gate 	/*
11407c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
11417c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
11427c478bd9Sstevel@tonic-gate 	 */
114344da779fSWilliam Kucharski 	if (strcmp(subcmd, "update_entry") == 0) {
1144eb2bd662Svikram 		ret = f(menu, menu_root, osdev);
114544da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "upgrade") == 0) {
1146eb2bd662Svikram 		ret = f(menu, bam_root, menu_root);
114744da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "list_entry") == 0) {
11487c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
114944da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "list_setting") == 0) {
115044da779fSWilliam Kucharski 		ret = f(menu, ((largc > 0) ? largv[0] : ""),
115144da779fSWilliam Kucharski 		    ((largc > 1) ? largv[1] : ""));
115244da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "disable_hypervisor") == 0) {
115344da779fSWilliam Kucharski 		if (is_sparc()) {
115444da779fSWilliam Kucharski 			bam_error(NO_SPARC, subcmd);
115544da779fSWilliam Kucharski 			ret = BAM_ERROR;
115644da779fSWilliam Kucharski 		} else {
115744da779fSWilliam Kucharski 			ret = f(menu, bam_root, NULL);
115844da779fSWilliam Kucharski 		}
115944da779fSWilliam Kucharski 	} else if (strcmp(subcmd, "enable_hypervisor") == 0) {
116044da779fSWilliam Kucharski 		if (is_sparc()) {
116144da779fSWilliam Kucharski 			bam_error(NO_SPARC, subcmd);
116244da779fSWilliam Kucharski 			ret = BAM_ERROR;
116344da779fSWilliam Kucharski 		} else {
116444da779fSWilliam Kucharski 			char *extra_args = NULL;
116544da779fSWilliam Kucharski 
116644da779fSWilliam Kucharski 			/*
116744da779fSWilliam Kucharski 			 * Compress all arguments passed in the largv[] array
116844da779fSWilliam Kucharski 			 * into one string that can then be appended to the
116944da779fSWilliam Kucharski 			 * end of the kernel$ string the routine to enable the
117044da779fSWilliam Kucharski 			 * hypervisor will build.
117144da779fSWilliam Kucharski 			 *
117244da779fSWilliam Kucharski 			 * This allows the caller to supply arbitrary unparsed
117344da779fSWilliam Kucharski 			 * arguments, such as dom0 memory settings or APIC
117444da779fSWilliam Kucharski 			 * options.
117544da779fSWilliam Kucharski 			 *
117644da779fSWilliam Kucharski 			 * This concatenation will be done without ANY syntax
117744da779fSWilliam Kucharski 			 * checking whatsoever, so it's the responsibility of
117844da779fSWilliam Kucharski 			 * the caller to make sure the arguments are valid and
117944da779fSWilliam Kucharski 			 * do not duplicate arguments the conversion routines
118044da779fSWilliam Kucharski 			 * may create.
118144da779fSWilliam Kucharski 			 */
118244da779fSWilliam Kucharski 			if (largc > 0) {
118344da779fSWilliam Kucharski 				int extra_len, i;
118444da779fSWilliam Kucharski 
118544da779fSWilliam Kucharski 				for (extra_len = 0, i = 0; i < largc; i++)
118644da779fSWilliam Kucharski 					extra_len += strlen(largv[i]);
118744da779fSWilliam Kucharski 
118844da779fSWilliam Kucharski 				/*
118944da779fSWilliam Kucharski 				 * Allocate space for argument strings,
119044da779fSWilliam Kucharski 				 * intervening spaces and terminating NULL.
119144da779fSWilliam Kucharski 				 */
119244da779fSWilliam Kucharski 				extra_args = alloca(extra_len + largc);
119344da779fSWilliam Kucharski 
119444da779fSWilliam Kucharski 				(void) strcpy(extra_args, largv[0]);
119544da779fSWilliam Kucharski 
119644da779fSWilliam Kucharski 				for (i = 1; i < largc; i++) {
119744da779fSWilliam Kucharski 					(void) strcat(extra_args, " ");
119844da779fSWilliam Kucharski 					(void) strcat(extra_args, largv[i]);
119944da779fSWilliam Kucharski 				}
120044da779fSWilliam Kucharski 			}
120144da779fSWilliam Kucharski 
120244da779fSWilliam Kucharski 			ret = f(menu, bam_root, extra_args);
120344da779fSWilliam Kucharski 		}
120444da779fSWilliam Kucharski 	} else
1205eb2bd662Svikram 		ret = f(menu, NULL, opt);
1206eb2bd662Svikram 
12077c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
1208eb2bd662Svikram 		BAM_DPRINTF((D_WRITING_MENU_ROOT, fcn, clean_menu_root));
1209eb2bd662Svikram 		ret = menu_write(clean_menu_root, menu);
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 
1212eb2bd662Svikram out:
1213eb2bd662Svikram 	INJECT_ERROR1("POOL_SET", pool = "/pooldata");
1214eb2bd662Svikram 	assert((is_zfs(menu_root)) ^ (pool == NULL));
1215eb2bd662Svikram 	if (pool) {
1216eb2bd662Svikram 		(void) umount_top_dataset(pool, zmnted, zmntpt);
1217eb2bd662Svikram 		free(special);
1218eb2bd662Svikram 	}
12197c478bd9Sstevel@tonic-gate 	menu_free(menu);
12207c478bd9Sstevel@tonic-gate 	return (ret);
12217c478bd9Sstevel@tonic-gate }
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate static error_t
12257c478bd9Sstevel@tonic-gate bam_archive(
12267c478bd9Sstevel@tonic-gate 	char *subcmd,
12277c478bd9Sstevel@tonic-gate 	char *opt)
12287c478bd9Sstevel@tonic-gate {
12297c478bd9Sstevel@tonic-gate 	error_t			ret;
12307c478bd9Sstevel@tonic-gate 	error_t			(*f)(char *root, char *opt);
1231eb2bd662Svikram 	const char		*fcn = "bam_archive()";
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	/*
12348c1b6884Sszhou 	 * Add trailing / for archive subcommands
12358c1b6884Sszhou 	 */
12368c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
12378c1b6884Sszhou 		(void) strcat(rootbuf, "/");
12388c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
12398c1b6884Sszhou 
12408c1b6884Sszhou 	/*
12417c478bd9Sstevel@tonic-gate 	 * Check arguments
12427c478bd9Sstevel@tonic-gate 	 */
12437c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
12447c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
12457c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate 
1248eb2bd662Svikram 	ret = get_boot_cap(rootbuf);
1249eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1250eb2bd662Svikram 		BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn));
1251ae115bc7Smrj 		return (ret);
1252eb2bd662Svikram 	}
1253ae115bc7Smrj 
12547c478bd9Sstevel@tonic-gate 	/*
12557c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
12567c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
12577c478bd9Sstevel@tonic-gate 	 * information for each BE.
12587c478bd9Sstevel@tonic-gate 	 */
12597c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
12607c478bd9Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
12617c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 
1264b610f78eSvikram 	if (strcmp(subcmd, "update_all") == 0)
1265b610f78eSvikram 		bam_update_all = 1;
1266b610f78eSvikram 
1267986fd29aSsetje #if !defined(_OPB)
12682449e17fSsherrym 	ucode_install(bam_root);
12692449e17fSsherrym #endif
12702449e17fSsherrym 
1271b610f78eSvikram 	ret = f(bam_root, opt);
1272b610f78eSvikram 
1273b610f78eSvikram 	bam_update_all = 0;
1274b610f78eSvikram 
1275b610f78eSvikram 	return (ret);
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1279ae115bc7Smrj void
12807c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
12817c478bd9Sstevel@tonic-gate {
12827c478bd9Sstevel@tonic-gate 	va_list ap;
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	va_start(ap, format);
12857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
12867c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
12877c478bd9Sstevel@tonic-gate 	va_end(ap);
12887c478bd9Sstevel@tonic-gate }
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1291eb2bd662Svikram void
1292eb2bd662Svikram bam_derror(char *format, ...)
1293eb2bd662Svikram {
1294eb2bd662Svikram 	va_list ap;
1295eb2bd662Svikram 
1296eb2bd662Svikram 	assert(bam_debug);
1297eb2bd662Svikram 
1298eb2bd662Svikram 	va_start(ap, format);
1299eb2bd662Svikram 	(void) fprintf(stderr, "DEBUG: ");
1300eb2bd662Svikram 	(void) vfprintf(stderr, format, ap);
1301eb2bd662Svikram 	va_end(ap);
1302eb2bd662Svikram }
1303eb2bd662Svikram 
1304eb2bd662Svikram /*PRINTFLIKE1*/
1305eb2bd662Svikram void
13067c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
13077c478bd9Sstevel@tonic-gate {
13087c478bd9Sstevel@tonic-gate 	va_list ap;
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	va_start(ap, format);
13117c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
13127c478bd9Sstevel@tonic-gate 	va_end(ap);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
1315ae115bc7Smrj /*PRINTFLIKE1*/
1316ae115bc7Smrj void
1317ae115bc7Smrj bam_print_stderr(char *format, ...)
1318ae115bc7Smrj {
1319ae115bc7Smrj 	va_list ap;
1320ae115bc7Smrj 
1321ae115bc7Smrj 	va_start(ap, format);
1322ae115bc7Smrj 	(void) vfprintf(stderr, format, ap);
1323ae115bc7Smrj 	va_end(ap);
1324ae115bc7Smrj }
1325ae115bc7Smrj 
132666b6aef6SWilliam Kucharski void
13277c478bd9Sstevel@tonic-gate bam_exit(int excode)
13287c478bd9Sstevel@tonic-gate {
1329cedc7e57SEnrico Perla - Sun Microsystems 	restore_env();
13307c478bd9Sstevel@tonic-gate 	bam_unlock();
13317c478bd9Sstevel@tonic-gate 	exit(excode);
13327c478bd9Sstevel@tonic-gate }
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate static void
13357c478bd9Sstevel@tonic-gate bam_lock(void)
13367c478bd9Sstevel@tonic-gate {
13377c478bd9Sstevel@tonic-gate 	struct flock lock;
13387c478bd9Sstevel@tonic-gate 	pid_t pid;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
13417c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
13427c478bd9Sstevel@tonic-gate 		/*
13437c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
13447c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
13457c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
13467c478bd9Sstevel@tonic-gate 		 */
13477c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
13487c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
13497c478bd9Sstevel@tonic-gate 			return;
13507c478bd9Sstevel@tonic-gate 		}
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
13537c478bd9Sstevel@tonic-gate 		bam_exit(1);
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
13577c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
13587c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
13597c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
13627c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
13637c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
13647c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
13657c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
13667c478bd9Sstevel@tonic-gate 			bam_exit(1);
13677c478bd9Sstevel@tonic-gate 		}
13687c478bd9Sstevel@tonic-gate 		pid = 0;
13697c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
13707c478bd9Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
13737c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
13747c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
13757c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
13767c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
13777c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
13787c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
13797c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
13807c478bd9Sstevel@tonic-gate 			bam_exit(1);
13817c478bd9Sstevel@tonic-gate 		}
13827c478bd9Sstevel@tonic-gate 	}
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	/* We own the lock now */
13857c478bd9Sstevel@tonic-gate 	pid = getpid();
13867c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
13877c478bd9Sstevel@tonic-gate }
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate static void
13907c478bd9Sstevel@tonic-gate bam_unlock(void)
13917c478bd9Sstevel@tonic-gate {
13927c478bd9Sstevel@tonic-gate 	struct flock unlock;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	/*
13957c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
13967c478bd9Sstevel@tonic-gate 	 */
13977c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
13987c478bd9Sstevel@tonic-gate 		return;
13997c478bd9Sstevel@tonic-gate 	}
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
14027c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
14037c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
14047c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
14077c478bd9Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
14087c478bd9Sstevel@tonic-gate 	}
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
14117c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
14127c478bd9Sstevel@tonic-gate 	}
14137c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
14147c478bd9Sstevel@tonic-gate }
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate static error_t
14177c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
14187c478bd9Sstevel@tonic-gate {
14197c478bd9Sstevel@tonic-gate 	filelist_t flist;
14207c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
14217c478bd9Sstevel@tonic-gate 	line_t *lp;
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	assert(root);
14247c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
14277c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
14287c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14297c478bd9Sstevel@tonic-gate 	}
14307c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
14337c478bd9Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
14397c478bd9Sstevel@tonic-gate }
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate /*
14427c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
14437c478bd9Sstevel@tonic-gate  * The list is *not* freed
14447c478bd9Sstevel@tonic-gate  */
14457c478bd9Sstevel@tonic-gate static error_t
14467c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
14477c478bd9Sstevel@tonic-gate {
14487c478bd9Sstevel@tonic-gate 	char		tmpfile[PATH_MAX];
14497c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
14507c478bd9Sstevel@tonic-gate 	FILE		*fp;
14517c478bd9Sstevel@tonic-gate 	int		ret;
14527c478bd9Sstevel@tonic-gate 	struct stat	sb;
14537c478bd9Sstevel@tonic-gate 	mode_t		mode;
14547c478bd9Sstevel@tonic-gate 	uid_t		root_uid;
14557c478bd9Sstevel@tonic-gate 	gid_t		sys_gid;
14567c478bd9Sstevel@tonic-gate 	struct passwd	*pw;
14577c478bd9Sstevel@tonic-gate 	struct group	*gp;
1458eb2bd662Svikram 	const char	*fcn = "list2file()";
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	if (start == NULL) {
1463eb2bd662Svikram 		/* Empty GRUB menu */
14647c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
14657c478bd9Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
14667c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
14677c478bd9Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
14687c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
14697c478bd9Sstevel@tonic-gate 			} else {
14707c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
14717c478bd9Sstevel@tonic-gate 			}
14727c478bd9Sstevel@tonic-gate 		}
1473eb2bd662Svikram 		return (BAM_SUCCESS);
14747c478bd9Sstevel@tonic-gate 	}
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	/*
14777c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
14787c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
14797c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
14807c478bd9Sstevel@tonic-gate 	 */
14817c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
14827c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
14837c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
14847c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
14857c478bd9Sstevel@tonic-gate 	} else {
14867c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
14877c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
14887c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
14897c478bd9Sstevel@tonic-gate 		} else {
14907c478bd9Sstevel@tonic-gate 			bam_error(CANT_FIND_USER,
14917c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
14927c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
14937c478bd9Sstevel@tonic-gate 		}
14947c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
14957c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
14967c478bd9Sstevel@tonic-gate 		} else {
14977c478bd9Sstevel@tonic-gate 			bam_error(CANT_FIND_GROUP,
14987c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
14997c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate 	}
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
15067c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
15077c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
15087c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
15097c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15107c478bd9Sstevel@tonic-gate 	}
1511963390b4Svikram 	ret = fclose(fp);
1512963390b4Svikram 	INJECT_ERROR1("LIST2FILE_TRUNC_FCLOSE", ret = EOF);
1513963390b4Svikram 	if (ret == EOF) {
15147c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
15157c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15167c478bd9Sstevel@tonic-gate 	}
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
15197c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
15207c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
15217c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
15227c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15237c478bd9Sstevel@tonic-gate 	}
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
1526963390b4Svikram 		ret = s_fputs(start->line, fp);
1527963390b4Svikram 		INJECT_ERROR1("LIST2FILE_FPUTS", ret = EOF);
1528963390b4Svikram 		if (ret == EOF) {
15297c478bd9Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
15307c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
15317c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
15327c478bd9Sstevel@tonic-gate 		}
15337c478bd9Sstevel@tonic-gate 	}
15347c478bd9Sstevel@tonic-gate 
1535963390b4Svikram 	ret = fclose(fp);
1536963390b4Svikram 	INJECT_ERROR1("LIST2FILE_APPEND_FCLOSE", ret = EOF);
1537963390b4Svikram 	if (ret == EOF) {
15387c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
15397c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15407c478bd9Sstevel@tonic-gate 	}
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	/*
1543de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1544de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
1545de531be4Sjg 	 * operations as EINVAL.
15467c478bd9Sstevel@tonic-gate 	 */
15477c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1548de531be4Sjg 	if (ret == -1 &&
1549de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
15507c478bd9Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
15517c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1555de531be4Sjg 	if (ret == -1 &&
1556de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
15577c478bd9Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
15587c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	/*
15627c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
15637c478bd9Sstevel@tonic-gate 	 */
15647c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
1565963390b4Svikram 	INJECT_ERROR1("LIST2FILE_RENAME", ret = -1);
15667c478bd9Sstevel@tonic-gate 	if (ret != 0) {
15677c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
15687c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
15697c478bd9Sstevel@tonic-gate 	}
15707c478bd9Sstevel@tonic-gate 
1571eb2bd662Svikram 	BAM_DPRINTF((D_WROTE_FILE, fcn, path));
15727c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
15737c478bd9Sstevel@tonic-gate }
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate /*
157648847494SEnrico Perla - Sun Microsystems  * Checks if the path specified (without the file name at the end) exists
157748847494SEnrico Perla - Sun Microsystems  * and creates it if not. If the path exists and is not a directory, an attempt
157848847494SEnrico Perla - Sun Microsystems  * to unlink is made.
15797c478bd9Sstevel@tonic-gate  */
158048847494SEnrico Perla - Sun Microsystems static int
158148847494SEnrico Perla - Sun Microsystems setup_path(char *path)
158248847494SEnrico Perla - Sun Microsystems {
158348847494SEnrico Perla - Sun Microsystems 	char 		*p;
158448847494SEnrico Perla - Sun Microsystems 	int		ret;
158548847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
158648847494SEnrico Perla - Sun Microsystems 
158748847494SEnrico Perla - Sun Microsystems 	p = strrchr(path, '/');
158848847494SEnrico Perla - Sun Microsystems 	if (p != NULL) {
158948847494SEnrico Perla - Sun Microsystems 		*p = '\0';
159048847494SEnrico Perla - Sun Microsystems 		if (stat(path, &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
159148847494SEnrico Perla - Sun Microsystems 			/* best effort attempt, mkdirp will catch the error */
159248847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
159348847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
159448847494SEnrico Perla - Sun Microsystems 				bam_print(NEED_DIRPATH, path);
159548847494SEnrico Perla - Sun Microsystems 			ret = mkdirp(path, DIR_PERMS);
159648847494SEnrico Perla - Sun Microsystems 			if (ret == -1) {
159748847494SEnrico Perla - Sun Microsystems 				bam_error(MKDIR_FAILED, path, strerror(errno));
159848847494SEnrico Perla - Sun Microsystems 				*p = '/';
159948847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
160048847494SEnrico Perla - Sun Microsystems 			}
160148847494SEnrico Perla - Sun Microsystems 		}
160248847494SEnrico Perla - Sun Microsystems 		*p = '/';
160348847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
160448847494SEnrico Perla - Sun Microsystems 	}
160548847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
160648847494SEnrico Perla - Sun Microsystems }
160748847494SEnrico Perla - Sun Microsystems 
160848847494SEnrico Perla - Sun Microsystems typedef union {
160948847494SEnrico Perla - Sun Microsystems 	gzFile	gzfile;
161048847494SEnrico Perla - Sun Microsystems 	int	fdfile;
161148847494SEnrico Perla - Sun Microsystems } outfile;
161248847494SEnrico Perla - Sun Microsystems 
161348847494SEnrico Perla - Sun Microsystems typedef struct {
161448847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
161548847494SEnrico Perla - Sun Microsystems 	outfile		out;
161648847494SEnrico Perla - Sun Microsystems } cachefile;
161748847494SEnrico Perla - Sun Microsystems 
161848847494SEnrico Perla - Sun Microsystems static int
161948847494SEnrico Perla - Sun Microsystems setup_file(char *base, const char *path, cachefile *cf)
162048847494SEnrico Perla - Sun Microsystems {
162148847494SEnrico Perla - Sun Microsystems 	int	ret;
162248847494SEnrico Perla - Sun Microsystems 	char	*strip;
162348847494SEnrico Perla - Sun Microsystems 
162448847494SEnrico Perla - Sun Microsystems 	/* init gzfile or fdfile in case we fail before opening */
162548847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT)
162648847494SEnrico Perla - Sun Microsystems 		cf->out.gzfile = NULL;
162748847494SEnrico Perla - Sun Microsystems 	else
162848847494SEnrico Perla - Sun Microsystems 		cf->out.fdfile = -1;
162948847494SEnrico Perla - Sun Microsystems 
163048847494SEnrico Perla - Sun Microsystems 	/* strip the trailing altroot path */
163148847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
163248847494SEnrico Perla - Sun Microsystems 
163348847494SEnrico Perla - Sun Microsystems 	ret = snprintf(cf->path, sizeof (cf->path), "%s/%s", base, strip);
163448847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (cf->path)) {
163548847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
163648847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
163748847494SEnrico Perla - Sun Microsystems 	}
163848847494SEnrico Perla - Sun Microsystems 
163948847494SEnrico Perla - Sun Microsystems 	/* Check if path is present in the archive cache directory */
164048847494SEnrico Perla - Sun Microsystems 	if (setup_path(cf->path) == BAM_ERROR)
164148847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
164248847494SEnrico Perla - Sun Microsystems 
164348847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
164448847494SEnrico Perla - Sun Microsystems 		if ((cf->out.gzfile = gzopen(cf->path, "wb")) == NULL) {
1645cedc7e57SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, cf->path, strerror(errno));
164648847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
164748847494SEnrico Perla - Sun Microsystems 		}
164848847494SEnrico Perla - Sun Microsystems 		(void) gzsetparams(cf->out.gzfile, Z_BEST_SPEED,
164948847494SEnrico Perla - Sun Microsystems 		    Z_DEFAULT_STRATEGY);
165048847494SEnrico Perla - Sun Microsystems 	} else {
165148847494SEnrico Perla - Sun Microsystems 		if ((cf->out.fdfile = open(cf->path, O_WRONLY | O_CREAT, 0644))
165248847494SEnrico Perla - Sun Microsystems 		    == -1) {
165348847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, cf->path, strerror(errno));
165448847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
165548847494SEnrico Perla - Sun Microsystems 		}
165648847494SEnrico Perla - Sun Microsystems 	}
165748847494SEnrico Perla - Sun Microsystems 
165848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
165948847494SEnrico Perla - Sun Microsystems }
166048847494SEnrico Perla - Sun Microsystems 
166148847494SEnrico Perla - Sun Microsystems static int
166248847494SEnrico Perla - Sun Microsystems cache_write(cachefile cf, char *buf, int size)
166348847494SEnrico Perla - Sun Microsystems {
166448847494SEnrico Perla - Sun Microsystems 	int	err;
166548847494SEnrico Perla - Sun Microsystems 
166648847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
166748847494SEnrico Perla - Sun Microsystems 		if (gzwrite(cf.out.gzfile, buf, size) < 1) {
166848847494SEnrico Perla - Sun Microsystems 			bam_error(GZ_WRITE_FAIL, gzerror(cf.out.gzfile, &err));
1669cedc7e57SEnrico Perla - Sun Microsystems 			if (err == Z_ERRNO && bam_verbose) {
167048847494SEnrico Perla - Sun Microsystems 				bam_error(WRITE_FAIL, cf.path, strerror(errno));
1671cedc7e57SEnrico Perla - Sun Microsystems 			}
167248847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
167348847494SEnrico Perla - Sun Microsystems 		}
167448847494SEnrico Perla - Sun Microsystems 	} else {
167548847494SEnrico Perla - Sun Microsystems 		if (write(cf.out.fdfile, buf, size) < 1) {
167648847494SEnrico Perla - Sun Microsystems 			bam_error(WRITE_FAIL, cf.path, strerror(errno));
167748847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
167848847494SEnrico Perla - Sun Microsystems 		}
167948847494SEnrico Perla - Sun Microsystems 	}
168048847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
168148847494SEnrico Perla - Sun Microsystems }
168248847494SEnrico Perla - Sun Microsystems 
168348847494SEnrico Perla - Sun Microsystems static int
168448847494SEnrico Perla - Sun Microsystems cache_close(cachefile cf)
168548847494SEnrico Perla - Sun Microsystems {
168648847494SEnrico Perla - Sun Microsystems 	int	ret;
168748847494SEnrico Perla - Sun Microsystems 
168848847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
168948847494SEnrico Perla - Sun Microsystems 		if (cf.out.gzfile) {
169048847494SEnrico Perla - Sun Microsystems 			ret = gzclose(cf.out.gzfile);
1691cedc7e57SEnrico Perla - Sun Microsystems 			if (ret != Z_OK) {
169248847494SEnrico Perla - Sun Microsystems 				bam_error(CLOSE_FAIL, cf.path, strerror(errno));
169348847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
169448847494SEnrico Perla - Sun Microsystems 			}
1695cedc7e57SEnrico Perla - Sun Microsystems 		}
169648847494SEnrico Perla - Sun Microsystems 	} else {
169748847494SEnrico Perla - Sun Microsystems 		if (cf.out.fdfile != -1) {
169848847494SEnrico Perla - Sun Microsystems 			ret = close(cf.out.fdfile);
169948847494SEnrico Perla - Sun Microsystems 			if (ret != 0) {
170048847494SEnrico Perla - Sun Microsystems 				bam_error(CLOSE_FAIL, cf.path, strerror(errno));
170148847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
170248847494SEnrico Perla - Sun Microsystems 			}
170348847494SEnrico Perla - Sun Microsystems 		}
170448847494SEnrico Perla - Sun Microsystems 	}
170548847494SEnrico Perla - Sun Microsystems 
170648847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
170748847494SEnrico Perla - Sun Microsystems }
170848847494SEnrico Perla - Sun Microsystems 
170948847494SEnrico Perla - Sun Microsystems static int
171048847494SEnrico Perla - Sun Microsystems dircache_updatefile(const char *path, int what)
171148847494SEnrico Perla - Sun Microsystems {
171248847494SEnrico Perla - Sun Microsystems 	int 		ret, exitcode;
171348847494SEnrico Perla - Sun Microsystems 	char 		buf[4096 * 4];
171448847494SEnrico Perla - Sun Microsystems 	FILE 		*infile;
171548847494SEnrico Perla - Sun Microsystems 	cachefile 	outfile, outupdt;
171648847494SEnrico Perla - Sun Microsystems 
1717cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
1718cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
1719cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
1720cedc7e57SEnrico Perla - Sun Microsystems 	}
1721cedc7e57SEnrico Perla - Sun Microsystems 
1722cedc7e57SEnrico Perla - Sun Microsystems 	if (!has_cachedir(what))
172348847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
172448847494SEnrico Perla - Sun Microsystems 
172548847494SEnrico Perla - Sun Microsystems 	if ((infile = fopen(path, "rb")) == NULL) {
172648847494SEnrico Perla - Sun Microsystems 		bam_error(OPEN_FAIL, path, strerror(errno));
172748847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
172848847494SEnrico Perla - Sun Microsystems 	}
172948847494SEnrico Perla - Sun Microsystems 
173048847494SEnrico Perla - Sun Microsystems 	ret = setup_file(get_cachedir(what), path, &outfile);
173148847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
173248847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
173348847494SEnrico Perla - Sun Microsystems 		goto out;
173448847494SEnrico Perla - Sun Microsystems 	}
173548847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI)) {
173648847494SEnrico Perla - Sun Microsystems 		ret = setup_file(get_updatedir(what), path, &outupdt);
173748847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
173848847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
173948847494SEnrico Perla - Sun Microsystems 	}
174048847494SEnrico Perla - Sun Microsystems 
174148847494SEnrico Perla - Sun Microsystems 	while ((ret = fread(buf, 1, sizeof (buf), infile)) > 0) {
174248847494SEnrico Perla - Sun Microsystems 		if (cache_write(outfile, buf, ret) == BAM_ERROR) {
174348847494SEnrico Perla - Sun Microsystems 			exitcode = BAM_ERROR;
174448847494SEnrico Perla - Sun Microsystems 			goto out;
174548847494SEnrico Perla - Sun Microsystems 		}
174648847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
174748847494SEnrico Perla - Sun Microsystems 			if (cache_write(outupdt, buf, ret) == BAM_ERROR)
174848847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
174948847494SEnrico Perla - Sun Microsystems 	}
175048847494SEnrico Perla - Sun Microsystems 
175148847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
175248847494SEnrico Perla - Sun Microsystems 	get_count(what)++;
1753cedc7e57SEnrico Perla - Sun Microsystems 	if (get_count(what) > COUNT_MAX)
1754cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
175548847494SEnrico Perla - Sun Microsystems 	exitcode = BAM_SUCCESS;
175648847494SEnrico Perla - Sun Microsystems out:
175748847494SEnrico Perla - Sun Microsystems 	(void) fclose(infile);
175848847494SEnrico Perla - Sun Microsystems 	if (cache_close(outfile) == BAM_ERROR)
175948847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
176048847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI) &&
176148847494SEnrico Perla - Sun Microsystems 	    cache_close(outupdt) == BAM_ERROR)
176248847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
176348847494SEnrico Perla - Sun Microsystems 	if (exitcode == BAM_ERROR)
176448847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
176548847494SEnrico Perla - Sun Microsystems 	return (exitcode);
176648847494SEnrico Perla - Sun Microsystems }
176748847494SEnrico Perla - Sun Microsystems 
176848847494SEnrico Perla - Sun Microsystems static int
176948847494SEnrico Perla - Sun Microsystems dircache_updatedir(const char *path, int what, int updt)
177048847494SEnrico Perla - Sun Microsystems {
177148847494SEnrico Perla - Sun Microsystems 	int		ret;
177248847494SEnrico Perla - Sun Microsystems 	char		dpath[PATH_MAX];
177348847494SEnrico Perla - Sun Microsystems 	char		*strip;
177448847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
177548847494SEnrico Perla - Sun Microsystems 
177648847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
177748847494SEnrico Perla - Sun Microsystems 
177848847494SEnrico Perla - Sun Microsystems 	ret = snprintf(dpath, sizeof (dpath), "%s/%s", updt ?
177948847494SEnrico Perla - Sun Microsystems 	    get_updatedir(what) : get_cachedir(what), strip);
178048847494SEnrico Perla - Sun Microsystems 
178148847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (dpath)) {
178248847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
178348847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
178448847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
178548847494SEnrico Perla - Sun Microsystems 	}
178648847494SEnrico Perla - Sun Microsystems 
178748847494SEnrico Perla - Sun Microsystems 	if (stat(dpath, &sb) == 0 && S_ISDIR(sb.st_mode))
178848847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
178948847494SEnrico Perla - Sun Microsystems 
179048847494SEnrico Perla - Sun Microsystems 	if (updt) {
179148847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
179248847494SEnrico Perla - Sun Microsystems 			if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1)
179348847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
179448847494SEnrico Perla - Sun Microsystems 	} else {
179548847494SEnrico Perla - Sun Microsystems 		if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1) {
179648847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
179748847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
179848847494SEnrico Perla - Sun Microsystems 		}
179948847494SEnrico Perla - Sun Microsystems 	}
180048847494SEnrico Perla - Sun Microsystems 
180148847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
180248847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
180348847494SEnrico Perla - Sun Microsystems }
180448847494SEnrico Perla - Sun Microsystems 
180548847494SEnrico Perla - Sun Microsystems #define	DO_CACHE_DIR	0
180648847494SEnrico Perla - Sun Microsystems #define	DO_UPDATE_DIR	1
180748847494SEnrico Perla - Sun Microsystems 
180848847494SEnrico Perla - Sun Microsystems #if defined(_LP64) || defined(_LONGLONG_TYPE)
180948847494SEnrico Perla - Sun Microsystems typedef		Elf64_Ehdr	_elfhdr;
181048847494SEnrico Perla - Sun Microsystems #else
181148847494SEnrico Perla - Sun Microsystems typedef		Elf32_Ehdr	_elfhdr;
181248847494SEnrico Perla - Sun Microsystems #endif
181348847494SEnrico Perla - Sun Microsystems 
181448847494SEnrico Perla - Sun Microsystems /*
181548847494SEnrico Perla - Sun Microsystems  * This routine updates the contents of the cache directory
181648847494SEnrico Perla - Sun Microsystems  */
181748847494SEnrico Perla - Sun Microsystems static int
181848847494SEnrico Perla - Sun Microsystems update_dircache(const char *path, int flags)
181948847494SEnrico Perla - Sun Microsystems {
182048847494SEnrico Perla - Sun Microsystems 	int rc = BAM_SUCCESS;
182148847494SEnrico Perla - Sun Microsystems 
182248847494SEnrico Perla - Sun Microsystems 	switch (flags) {
182348847494SEnrico Perla - Sun Microsystems 	case FTW_F:
182448847494SEnrico Perla - Sun Microsystems 		{
182548847494SEnrico Perla - Sun Microsystems 		int	fd;
182648847494SEnrico Perla - Sun Microsystems 		_elfhdr	elf;
182748847494SEnrico Perla - Sun Microsystems 
182848847494SEnrico Perla - Sun Microsystems 		if ((fd = open(path, O_RDONLY)) < 0) {
182948847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, path, strerror(errno));
183048847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
183148847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
183248847494SEnrico Perla - Sun Microsystems 			break;
183348847494SEnrico Perla - Sun Microsystems 		}
183448847494SEnrico Perla - Sun Microsystems 
183548847494SEnrico Perla - Sun Microsystems 		/*
183648847494SEnrico Perla - Sun Microsystems 		 * libelf and gelf would be a cleaner and easier way to handle
183748847494SEnrico Perla - Sun Microsystems 		 * this, but libelf fails compilation if _ILP32 is defined &&
183848847494SEnrico Perla - Sun Microsystems 		 * _FILE_OFFSET_BITS is != 32 ...
183948847494SEnrico Perla - Sun Microsystems 		 */
184048847494SEnrico Perla - Sun Microsystems 		if (read(fd, (void *)&elf, sizeof (_elfhdr)) < 0) {
184148847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, path, strerror(errno));
184248847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
184348847494SEnrico Perla - Sun Microsystems 			(void) close(fd);
184448847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
184548847494SEnrico Perla - Sun Microsystems 			break;
184648847494SEnrico Perla - Sun Microsystems 		}
184748847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
184848847494SEnrico Perla - Sun Microsystems 
184948847494SEnrico Perla - Sun Microsystems 		/*
185048847494SEnrico Perla - Sun Microsystems 		 * If the file is not an executable and is not inside an amd64
185148847494SEnrico Perla - Sun Microsystems 		 * directory, we copy it in both the cache directories,
185248847494SEnrico Perla - Sun Microsystems 		 * otherwise, we only copy it inside the 64-bit one.
185348847494SEnrico Perla - Sun Microsystems 		 */
185448847494SEnrico Perla - Sun Microsystems 		if (memcmp(elf.e_ident, ELFMAG, 4) != 0) {
185548847494SEnrico Perla - Sun Microsystems 			if (strstr(path, "/amd64")) {
185648847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
185748847494SEnrico Perla - Sun Microsystems 			} else {
185848847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
1859cedc7e57SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
1860cedc7e57SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
186148847494SEnrico Perla - Sun Microsystems 			}
186248847494SEnrico Perla - Sun Microsystems 		} else {
186348847494SEnrico Perla - Sun Microsystems 			/*
186448847494SEnrico Perla - Sun Microsystems 			 * Based on the ELF class we copy the file in the 32-bit
186548847494SEnrico Perla - Sun Microsystems 			 * or the 64-bit cache directory.
186648847494SEnrico Perla - Sun Microsystems 			 */
1867cedc7e57SEnrico Perla - Sun Microsystems 			if (elf.e_ident[EI_CLASS] == ELFCLASS32) {
186848847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
1869cedc7e57SEnrico Perla - Sun Microsystems 			} else if (elf.e_ident[EI_CLASS] == ELFCLASS64) {
187048847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
1871cedc7e57SEnrico Perla - Sun Microsystems 			} else {
187248847494SEnrico Perla - Sun Microsystems 				bam_print(NO3264ELF, path);
187348847494SEnrico Perla - Sun Microsystems 				/* paranoid */
187448847494SEnrico Perla - Sun Microsystems 				rc  = dircache_updatefile(path, FILE32);
187548847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
187648847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
187748847494SEnrico Perla - Sun Microsystems 			}
187848847494SEnrico Perla - Sun Microsystems 		}
187948847494SEnrico Perla - Sun Microsystems 		break;
188048847494SEnrico Perla - Sun Microsystems 		}
188148847494SEnrico Perla - Sun Microsystems 	case FTW_D:
188248847494SEnrico Perla - Sun Microsystems 		if (strstr(path, "/amd64") == NULL) {
188348847494SEnrico Perla - Sun Microsystems 			rc = dircache_updatedir(path, FILE32, DO_UPDATE_DIR);
188448847494SEnrico Perla - Sun Microsystems 			if (rc == BAM_SUCCESS)
188548847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE32,
188648847494SEnrico Perla - Sun Microsystems 				    DO_CACHE_DIR);
188748847494SEnrico Perla - Sun Microsystems 		} else {
188848847494SEnrico Perla - Sun Microsystems 			if (has_cachedir(FILE64)) {
188948847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE64,
189048847494SEnrico Perla - Sun Microsystems 				    DO_UPDATE_DIR);
189148847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
189248847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatedir(path, FILE64,
189348847494SEnrico Perla - Sun Microsystems 					    DO_CACHE_DIR);
189448847494SEnrico Perla - Sun Microsystems 			}
189548847494SEnrico Perla - Sun Microsystems 		}
189648847494SEnrico Perla - Sun Microsystems 		break;
189748847494SEnrico Perla - Sun Microsystems 	default:
189848847494SEnrico Perla - Sun Microsystems 		rc = BAM_ERROR;
189948847494SEnrico Perla - Sun Microsystems 		break;
190048847494SEnrico Perla - Sun Microsystems 	}
190148847494SEnrico Perla - Sun Microsystems 
190248847494SEnrico Perla - Sun Microsystems 	return (rc);
190348847494SEnrico Perla - Sun Microsystems }
190448847494SEnrico Perla - Sun Microsystems 
19057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19067c478bd9Sstevel@tonic-gate static int
19077c478bd9Sstevel@tonic-gate cmpstat(
19087c478bd9Sstevel@tonic-gate 	const char *file,
190948847494SEnrico Perla - Sun Microsystems 	const struct stat *st,
19107c478bd9Sstevel@tonic-gate 	int flags,
19117c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
19127c478bd9Sstevel@tonic-gate {
19137c478bd9Sstevel@tonic-gate 	uint_t 		sz;
19147c478bd9Sstevel@tonic-gate 	uint64_t 	*value;
19157c478bd9Sstevel@tonic-gate 	uint64_t 	filestat[2];
191689c3ee43SGangadhar Mylapuram 	int 		error, ret, status;
19177c478bd9Sstevel@tonic-gate 
191858091fd8Ssetje 	struct safefile *safefilep;
191958091fd8Ssetje 	FILE 		*fp;
192048847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
192189c3ee43SGangadhar Mylapuram 	regex_t re;
192258091fd8Ssetje 
19237c478bd9Sstevel@tonic-gate 	/*
192448847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create/update links too.
19257c478bd9Sstevel@tonic-gate 	 */
192648847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_F && flags != FTW_D && (flags == FTW_SL &&
192748847494SEnrico Perla - Sun Microsystems 	    !is_flag_on(IS_SPARC_TARGET)))
192848847494SEnrico Perla - Sun Microsystems 		return (0);
192948847494SEnrico Perla - Sun Microsystems 
193048847494SEnrico Perla - Sun Microsystems 	/*
193148847494SEnrico Perla - Sun Microsystems 	 * Ignore broken links
193248847494SEnrico Perla - Sun Microsystems 	 */
193348847494SEnrico Perla - Sun Microsystems 	if (flags == FTW_SL && stat(file, &sb) < 0)
19347c478bd9Sstevel@tonic-gate 		return (0);
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	/*
19377c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
19387c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
19397c478bd9Sstevel@tonic-gate 	 */
19407c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
194148847494SEnrico Perla - Sun Microsystems 		filestat[0] = st->st_size;
194248847494SEnrico Perla - Sun Microsystems 		filestat[1] = st->st_mtime;
19437c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
19447c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
19457c478bd9Sstevel@tonic-gate 		if (error)
19467c478bd9Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
19477c478bd9Sstevel@tonic-gate 	}
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	/*
1950d876c67dSjg 	 * If we are invoked as part of system/filesystem/boot-archive, then
195158091fd8Ssetje 	 * there are a number of things we should not worry about
19527c478bd9Sstevel@tonic-gate 	 */
195358091fd8Ssetje 	if (bam_smf_check) {
195458091fd8Ssetje 		/* ignore amd64 modules unless we are booted amd64. */
195558091fd8Ssetje 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
19567c478bd9Sstevel@tonic-gate 			return (0);
19577c478bd9Sstevel@tonic-gate 
195858091fd8Ssetje 		/* read in list of safe files */
195958091fd8Ssetje 		if (safefiles == NULL)
196058091fd8Ssetje 			if (fp = fopen("/boot/solaris/filelist.safe", "r")) {
196158091fd8Ssetje 				safefiles = s_calloc(1,
196258091fd8Ssetje 				    sizeof (struct safefile));
196358091fd8Ssetje 				safefilep = safefiles;
196458091fd8Ssetje 				safefilep->name = s_calloc(1, MAXPATHLEN +
196558091fd8Ssetje 				    MAXNAMELEN);
196658091fd8Ssetje 				safefilep->next = NULL;
196758091fd8Ssetje 				while (s_fgets(safefilep->name, MAXPATHLEN +
196858091fd8Ssetje 				    MAXNAMELEN, fp) != NULL) {
196958091fd8Ssetje 					safefilep->next = s_calloc(1,
197058091fd8Ssetje 					    sizeof (struct safefile));
197158091fd8Ssetje 					safefilep = safefilep->next;
197258091fd8Ssetje 					safefilep->name = s_calloc(1,
197358091fd8Ssetje 					    MAXPATHLEN + MAXNAMELEN);
197458091fd8Ssetje 					safefilep->next = NULL;
197558091fd8Ssetje 				}
197658091fd8Ssetje 				(void) fclose(fp);
197758091fd8Ssetje 			}
197858091fd8Ssetje 	}
197958091fd8Ssetje 
19807c478bd9Sstevel@tonic-gate 	/*
198148847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create a -path-list file for mkisofs
198248847494SEnrico Perla - Sun Microsystems 	 */
198348847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) && !bam_nowrite()) {
198448847494SEnrico Perla - Sun Microsystems 		if (flags != FTW_D) {
198548847494SEnrico Perla - Sun Microsystems 			char	*strip;
198648847494SEnrico Perla - Sun Microsystems 
198748847494SEnrico Perla - Sun Microsystems 			strip = (char *)file + strlen(rootbuf);
198848847494SEnrico Perla - Sun Microsystems 			(void) fprintf(walk_arg.sparcfile, "/%s=%s\n", strip,
198948847494SEnrico Perla - Sun Microsystems 			    file);
199048847494SEnrico Perla - Sun Microsystems 		}
199148847494SEnrico Perla - Sun Microsystems 	}
199248847494SEnrico Perla - Sun Microsystems 
199348847494SEnrico Perla - Sun Microsystems 	/*
199448847494SEnrico Perla - Sun Microsystems 	 * We are transitioning from the old model to the dircache or the cache
199548847494SEnrico Perla - Sun Microsystems 	 * directory was removed: create the entry without further checkings.
199648847494SEnrico Perla - Sun Microsystems 	 */
199748847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR)) {
199848847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
199948847494SEnrico Perla - Sun Microsystems 			bam_print(PARSEABLE_NEW_FILE, file);
200048847494SEnrico Perla - Sun Microsystems 
200148847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
200248847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
200348847494SEnrico Perla - Sun Microsystems 			return (0);
200448847494SEnrico Perla - Sun Microsystems 		}
200548847494SEnrico Perla - Sun Microsystems 
200648847494SEnrico Perla - Sun Microsystems 		ret = update_dircache(file, flags);
200748847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR) {
200848847494SEnrico Perla - Sun Microsystems 			bam_error(UPDT_CACHE_FAIL, file);
200948847494SEnrico Perla - Sun Microsystems 			return (-1);
201048847494SEnrico Perla - Sun Microsystems 		}
201148847494SEnrico Perla - Sun Microsystems 
201248847494SEnrico Perla - Sun Microsystems 		return (0);
201348847494SEnrico Perla - Sun Microsystems 	}
201448847494SEnrico Perla - Sun Microsystems 
201548847494SEnrico Perla - Sun Microsystems 	/*
20167c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
20177c478bd9Sstevel@tonic-gate 	 */
20187c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
20197c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
20207c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
20217c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
20227c478bd9Sstevel@tonic-gate 			return (0);
202348847494SEnrico Perla - Sun Microsystems 
202448847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
202548847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
202648847494SEnrico Perla - Sun Microsystems 		} else {
202748847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
202848847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
202948847494SEnrico Perla - Sun Microsystems 				bam_error(UPDT_CACHE_FAIL, file);
203048847494SEnrico Perla - Sun Microsystems 				return (-1);
203148847494SEnrico Perla - Sun Microsystems 			}
203248847494SEnrico Perla - Sun Microsystems 		}
203348847494SEnrico Perla - Sun Microsystems 
20347c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20357c478bd9Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
20367c478bd9Sstevel@tonic-gate 		return (0);
20377c478bd9Sstevel@tonic-gate 	}
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	/*
204048847494SEnrico Perla - Sun Microsystems 	 * If we got there, the file is already listed as to be included in the
204148847494SEnrico Perla - Sun Microsystems 	 * iso image. We just need to know if we are going to rebuild it or not
204248847494SEnrico Perla - Sun Microsystems 	 */
204348847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) &&
2044cedc7e57SEnrico Perla - Sun Microsystems 	    is_dir_flag_on(FILE64, NEED_UPDATE) && !bam_nowrite())
204548847494SEnrico Perla - Sun Microsystems 		return (0);
204648847494SEnrico Perla - Sun Microsystems 	/*
20477c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
20487c478bd9Sstevel@tonic-gate 	 */
20497c478bd9Sstevel@tonic-gate 	assert(sz == 2);
20507c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
20517c478bd9Sstevel@tonic-gate 
205248847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_D && (filestat[0] != st->st_size ||
205348847494SEnrico Perla - Sun Microsystems 	    filestat[1] != st->st_mtime)) {
20547c609327Ssetje 		if (bam_smf_check) {
20557c609327Ssetje 			safefilep = safefiles;
205689c3ee43SGangadhar Mylapuram 			while (safefilep != NULL &&
205789c3ee43SGangadhar Mylapuram 			    safefilep->name[0] != '\0') {
205889c3ee43SGangadhar Mylapuram 				if (regcomp(&re, safefilep->name,
205989c3ee43SGangadhar Mylapuram 				    REG_EXTENDED|REG_NOSUB) == 0) {
206089c3ee43SGangadhar Mylapuram 					status = regexec(&re,
206189c3ee43SGangadhar Mylapuram 					    file + bam_rootlen, 0, NULL, 0);
206289c3ee43SGangadhar Mylapuram 					regfree(&re);
206389c3ee43SGangadhar Mylapuram 					if (status == 0) {
206489c3ee43SGangadhar Mylapuram 						(void) creat(NEED_UPDATE_FILE,
206589c3ee43SGangadhar Mylapuram 						    0644);
20667c609327Ssetje 						return (0);
20677c609327Ssetje 					}
206889c3ee43SGangadhar Mylapuram 				}
20697c609327Ssetje 				safefilep = safefilep->next;
20707c609327Ssetje 			}
20717c609327Ssetje 		}
207248847494SEnrico Perla - Sun Microsystems 
207348847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
207448847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
207548847494SEnrico Perla - Sun Microsystems 		} else {
207648847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
207748847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
207848847494SEnrico Perla - Sun Microsystems 				bam_error(UPDT_CACHE_FAIL, file);
207948847494SEnrico Perla - Sun Microsystems 				return (-1);
208048847494SEnrico Perla - Sun Microsystems 			}
208148847494SEnrico Perla - Sun Microsystems 		}
208248847494SEnrico Perla - Sun Microsystems 
20837c478bd9Sstevel@tonic-gate 		if (bam_verbose)
20847c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
20857c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
20867c478bd9Sstevel@tonic-gate 			else
20877c478bd9Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
20887c478bd9Sstevel@tonic-gate 	}
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	return (0);
20917c478bd9Sstevel@tonic-gate }
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate /*
209448847494SEnrico Perla - Sun Microsystems  * Remove a directory path recursively
209548847494SEnrico Perla - Sun Microsystems  */
209648847494SEnrico Perla - Sun Microsystems static int
209748847494SEnrico Perla - Sun Microsystems rmdir_r(char *path)
209848847494SEnrico Perla - Sun Microsystems {
209948847494SEnrico Perla - Sun Microsystems 	struct dirent 	*d = NULL;
210048847494SEnrico Perla - Sun Microsystems 	DIR 		*dir = NULL;
210148847494SEnrico Perla - Sun Microsystems 	char 		tpath[PATH_MAX];
210248847494SEnrico Perla - Sun Microsystems 	struct stat 	sb;
210348847494SEnrico Perla - Sun Microsystems 
210448847494SEnrico Perla - Sun Microsystems 	if ((dir = opendir(path)) == NULL)
210548847494SEnrico Perla - Sun Microsystems 		return (-1);
210648847494SEnrico Perla - Sun Microsystems 
210748847494SEnrico Perla - Sun Microsystems 	while (d = readdir(dir)) {
210848847494SEnrico Perla - Sun Microsystems 		if ((strcmp(d->d_name, ".") != 0) &&
210948847494SEnrico Perla - Sun Microsystems 		    (strcmp(d->d_name, "..") != 0)) {
211048847494SEnrico Perla - Sun Microsystems 			(void) snprintf(tpath, sizeof (tpath), "%s/%s",
211148847494SEnrico Perla - Sun Microsystems 			    path, d->d_name);
211248847494SEnrico Perla - Sun Microsystems 			if (stat(tpath, &sb) == 0) {
211348847494SEnrico Perla - Sun Microsystems 				if (sb.st_mode & S_IFDIR)
211448847494SEnrico Perla - Sun Microsystems 					(void) rmdir_r(tpath);
211548847494SEnrico Perla - Sun Microsystems 				else
211648847494SEnrico Perla - Sun Microsystems 					(void) remove(tpath);
211748847494SEnrico Perla - Sun Microsystems 			}
211848847494SEnrico Perla - Sun Microsystems 		}
211948847494SEnrico Perla - Sun Microsystems 	}
212048847494SEnrico Perla - Sun Microsystems 	return (remove(path));
212148847494SEnrico Perla - Sun Microsystems }
212248847494SEnrico Perla - Sun Microsystems 
212348847494SEnrico Perla - Sun Microsystems /*
212448847494SEnrico Perla - Sun Microsystems  * Check if cache directory exists and, if not, create it and update flags
212548847494SEnrico Perla - Sun Microsystems  * accordingly. If the path exists, but it's not a directory, a best effort
212648847494SEnrico Perla - Sun Microsystems  * attempt to remove and recreate it is made.
212748847494SEnrico Perla - Sun Microsystems  * If the user requested a 'purge', always recreate the directory from scratch.
212848847494SEnrico Perla - Sun Microsystems  */
212948847494SEnrico Perla - Sun Microsystems static int
213048847494SEnrico Perla - Sun Microsystems set_cache_dir(char *root, int what)
213148847494SEnrico Perla - Sun Microsystems {
213248847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
213348847494SEnrico Perla - Sun Microsystems 	int		ret = 0;
213448847494SEnrico Perla - Sun Microsystems 
213548847494SEnrico Perla - Sun Microsystems 	ret = snprintf(get_cachedir(what), sizeof (get_cachedir(what)),
213648847494SEnrico Perla - Sun Microsystems 	    "%s%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(), what == FILE64 ?
213748847494SEnrico Perla - Sun Microsystems 	    "/amd64" : "", CACHEDIR_SUFFIX);
213848847494SEnrico Perla - Sun Microsystems 
213948847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_cachedir(what))) {
214048847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
214148847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
214248847494SEnrico Perla - Sun Microsystems 	}
214348847494SEnrico Perla - Sun Microsystems 
21444a9df875SEnrico Perla - Sun Microsystems 	if (bam_purge || is_flag_on(INVALIDATE_CACHE))
214548847494SEnrico Perla - Sun Microsystems 		(void) rmdir_r(get_cachedir(what));
214648847494SEnrico Perla - Sun Microsystems 
214748847494SEnrico Perla - Sun Microsystems 	if (stat(get_cachedir(what), &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
214848847494SEnrico Perla - Sun Microsystems 		/* best effort unlink attempt, mkdir will catch errors */
214948847494SEnrico Perla - Sun Microsystems 		(void) unlink(get_cachedir(what));
215048847494SEnrico Perla - Sun Microsystems 
215148847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
215248847494SEnrico Perla - Sun Microsystems 			bam_print(UPDATE_CDIR_MISS, get_cachedir(what));
215348847494SEnrico Perla - Sun Microsystems 		ret = mkdir(get_cachedir(what), DIR_PERMS);
215448847494SEnrico Perla - Sun Microsystems 		if (ret < 0) {
215548847494SEnrico Perla - Sun Microsystems 			bam_error(MKDIR_FAILED, get_cachedir(what),
215648847494SEnrico Perla - Sun Microsystems 			    strerror(errno));
215748847494SEnrico Perla - Sun Microsystems 			get_cachedir(what)[0] = '\0';
215848847494SEnrico Perla - Sun Microsystems 			return (ret);
215948847494SEnrico Perla - Sun Microsystems 		}
216048847494SEnrico Perla - Sun Microsystems 		set_flag(NEED_CACHE_DIR);
216148847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
216248847494SEnrico Perla - Sun Microsystems 	}
216348847494SEnrico Perla - Sun Microsystems 
216448847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
216548847494SEnrico Perla - Sun Microsystems }
216648847494SEnrico Perla - Sun Microsystems 
216748847494SEnrico Perla - Sun Microsystems static int
216848847494SEnrico Perla - Sun Microsystems set_update_dir(char *root, int what)
216948847494SEnrico Perla - Sun Microsystems {
217048847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
217148847494SEnrico Perla - Sun Microsystems 	int		ret;
217248847494SEnrico Perla - Sun Microsystems 
217348847494SEnrico Perla - Sun Microsystems 	if (is_dir_flag_on(what, NO_MULTI))
217448847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
217548847494SEnrico Perla - Sun Microsystems 
217648847494SEnrico Perla - Sun Microsystems 	if (!bam_extend) {
217748847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
217848847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
217948847494SEnrico Perla - Sun Microsystems 	}
218048847494SEnrico Perla - Sun Microsystems 
218148847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
218248847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
218348847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s/amd64%s", root,
218448847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
218548847494SEnrico Perla - Sun Microsystems 	else
218648847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
218748847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s%s", root,
218848847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
218948847494SEnrico Perla - Sun Microsystems 
219048847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_updatedir(what))) {
219148847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
219248847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
219348847494SEnrico Perla - Sun Microsystems 	}
219448847494SEnrico Perla - Sun Microsystems 
219548847494SEnrico Perla - Sun Microsystems 	if (stat(get_updatedir(what), &sb) == 0) {
219648847494SEnrico Perla - Sun Microsystems 		if (S_ISDIR(sb.st_mode))
219748847494SEnrico Perla - Sun Microsystems 			ret = rmdir_r(get_updatedir(what));
219848847494SEnrico Perla - Sun Microsystems 		else
219948847494SEnrico Perla - Sun Microsystems 			ret = unlink(get_updatedir(what));
220048847494SEnrico Perla - Sun Microsystems 
220148847494SEnrico Perla - Sun Microsystems 		if (ret != 0)
220248847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
220348847494SEnrico Perla - Sun Microsystems 	}
220448847494SEnrico Perla - Sun Microsystems 
220548847494SEnrico Perla - Sun Microsystems 	if (mkdir(get_updatedir(what), DIR_PERMS) < 0)
220648847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
220748847494SEnrico Perla - Sun Microsystems 
220848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
220948847494SEnrico Perla - Sun Microsystems }
221048847494SEnrico Perla - Sun Microsystems 
221148847494SEnrico Perla - Sun Microsystems static int
221248847494SEnrico Perla - Sun Microsystems is_valid_archive(char *root, int what)
221348847494SEnrico Perla - Sun Microsystems {
221448847494SEnrico Perla - Sun Microsystems 	char 		archive_path[PATH_MAX];
22154a9df875SEnrico Perla - Sun Microsystems 	char		timestamp_path[PATH_MAX];
22164a9df875SEnrico Perla - Sun Microsystems 	struct stat 	sb, timestamp;
221748847494SEnrico Perla - Sun Microsystems 	int 		ret;
221848847494SEnrico Perla - Sun Microsystems 
221948847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
222048847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path),
222148847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
222248847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
222348847494SEnrico Perla - Sun Microsystems 	else
222448847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path), "%s%s%s%s",
222548847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), ARCHIVE_SUFFIX);
222648847494SEnrico Perla - Sun Microsystems 
222748847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (archive_path)) {
222848847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
222948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
223048847494SEnrico Perla - Sun Microsystems 	}
223148847494SEnrico Perla - Sun Microsystems 
223248847494SEnrico Perla - Sun Microsystems 	if (stat(archive_path, &sb) != 0) {
223348847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
223448847494SEnrico Perla - Sun Microsystems 			bam_print(UPDATE_ARCH_MISS, archive_path);
223548847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
223648847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
223748847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
223848847494SEnrico Perla - Sun Microsystems 	}
223948847494SEnrico Perla - Sun Microsystems 
22404a9df875SEnrico Perla - Sun Microsystems 	/*
22414a9df875SEnrico Perla - Sun Microsystems 	 * The timestamp file is used to prevent stale files in the archive
22424a9df875SEnrico Perla - Sun Microsystems 	 * cache.
22434a9df875SEnrico Perla - Sun Microsystems 	 * Stale files can happen if the system is booted back and forth across
22444a9df875SEnrico Perla - Sun Microsystems 	 * the transition from bootadm-before-the-cache to
22454a9df875SEnrico Perla - Sun Microsystems 	 * bootadm-after-the-cache, since older versions of bootadm don't know
22464a9df875SEnrico Perla - Sun Microsystems 	 * about the existence of the archive cache.
22474a9df875SEnrico Perla - Sun Microsystems 	 *
22484a9df875SEnrico Perla - Sun Microsystems 	 * Since only bootadm-after-the-cache versions know about about this
22494a9df875SEnrico Perla - Sun Microsystems 	 * file, we require that the boot archive be older than this file.
22504a9df875SEnrico Perla - Sun Microsystems 	 */
22514a9df875SEnrico Perla - Sun Microsystems 	ret = snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root,
22524a9df875SEnrico Perla - Sun Microsystems 	    FILE_STAT_TIMESTAMP);
22534a9df875SEnrico Perla - Sun Microsystems 
22544a9df875SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (timestamp_path)) {
22554a9df875SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
22564a9df875SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
22574a9df875SEnrico Perla - Sun Microsystems 	}
22584a9df875SEnrico Perla - Sun Microsystems 
22594a9df875SEnrico Perla - Sun Microsystems 	if (stat(timestamp_path, &timestamp) != 0 ||
22604a9df875SEnrico Perla - Sun Microsystems 	    sb.st_mtime > timestamp.st_mtime) {
22614a9df875SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
22624a9df875SEnrico Perla - Sun Microsystems 			bam_print(UPDATE_CACHE_OLD, timestamp);
22634a9df875SEnrico Perla - Sun Microsystems 		/*
22644a9df875SEnrico Perla - Sun Microsystems 		 * Don't generate a false positive for the boot-archive service
22654a9df875SEnrico Perla - Sun Microsystems 		 * but trigger an update of the archive cache in
22664a9df875SEnrico Perla - Sun Microsystems 		 * boot-archive-update.
22674a9df875SEnrico Perla - Sun Microsystems 		 */
22684a9df875SEnrico Perla - Sun Microsystems 		if (bam_smf_check) {
22694a9df875SEnrico Perla - Sun Microsystems 			(void) creat(NEED_UPDATE_FILE, 0644);
22704a9df875SEnrico Perla - Sun Microsystems 			return (BAM_SUCCESS);
22714a9df875SEnrico Perla - Sun Microsystems 		}
22724a9df875SEnrico Perla - Sun Microsystems 
22734a9df875SEnrico Perla - Sun Microsystems 		set_flag(INVALIDATE_CACHE);
22744a9df875SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
22754a9df875SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
22764a9df875SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
22774a9df875SEnrico Perla - Sun Microsystems 	}
22784a9df875SEnrico Perla - Sun Microsystems 
227948847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET))
228048847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
228148847494SEnrico Perla - Sun Microsystems 
228248847494SEnrico Perla - Sun Microsystems 	if (bam_extend && sb.st_size > BA_SIZE_MAX) {
228348847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
228448847494SEnrico Perla - Sun Microsystems 			bam_print(MULTI_SIZE, archive_path, BA_SIZE_MAX);
228548847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
228648847494SEnrico Perla - Sun Microsystems 	}
228748847494SEnrico Perla - Sun Microsystems 
228848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
228948847494SEnrico Perla - Sun Microsystems }
229048847494SEnrico Perla - Sun Microsystems 
229148847494SEnrico Perla - Sun Microsystems /*
229248847494SEnrico Perla - Sun Microsystems  * Check flags and presence of required files and directories.
22937c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
22947c478bd9Sstevel@tonic-gate  * trigger an update.
22957c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
22967c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
22977c478bd9Sstevel@tonic-gate  */
229848847494SEnrico Perla - Sun Microsystems static int
22997c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
23007c478bd9Sstevel@tonic-gate {
230148847494SEnrico Perla - Sun Microsystems 
23027c478bd9Sstevel@tonic-gate 	struct stat 	sb;
230348847494SEnrico Perla - Sun Microsystems 	int 		ret;
230448847494SEnrico Perla - Sun Microsystems 
230548847494SEnrico Perla - Sun Microsystems 	/*
230648847494SEnrico Perla - Sun Microsystems 	 * If archive is missing, create archive
230748847494SEnrico Perla - Sun Microsystems 	 */
230848847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
230948847494SEnrico Perla - Sun Microsystems 		ret = is_valid_archive(root, FILE64);
2310cedc7e57SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
2311cedc7e57SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
231248847494SEnrico Perla - Sun Microsystems 	} else {
231348847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
231448847494SEnrico Perla - Sun Microsystems 		do {
231548847494SEnrico Perla - Sun Microsystems 			ret = is_valid_archive(root, what);
231648847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR)
231748847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
231848847494SEnrico Perla - Sun Microsystems 			what++;
231948847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
232048847494SEnrico Perla - Sun Microsystems 	}
232148847494SEnrico Perla - Sun Microsystems 
232248847494SEnrico Perla - Sun Microsystems 	if (bam_nowrite())
232348847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
232448847494SEnrico Perla - Sun Microsystems 
232548847494SEnrico Perla - Sun Microsystems 
232648847494SEnrico Perla - Sun Microsystems 	/*
232748847494SEnrico Perla - Sun Microsystems 	 * check if cache directories exist on x86.
232848847494SEnrico Perla - Sun Microsystems 	 * check (and always open) the cache file on SPARC.
232948847494SEnrico Perla - Sun Microsystems 	 */
233048847494SEnrico Perla - Sun Microsystems 	if (is_sparc()) {
233148847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_cachedir(FILE64),
233248847494SEnrico Perla - Sun Microsystems 		    sizeof (get_cachedir(FILE64)), "%s%s%s/%s", root,
233348847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), CACHEDIR_SUFFIX);
233448847494SEnrico Perla - Sun Microsystems 
233548847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (get_cachedir(FILE64))) {
233648847494SEnrico Perla - Sun Microsystems 			bam_error(PATH_TOO_LONG, rootbuf);
233748847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
233848847494SEnrico Perla - Sun Microsystems 		}
233948847494SEnrico Perla - Sun Microsystems 
234048847494SEnrico Perla - Sun Microsystems 		if (stat(get_cachedir(FILE64), &sb) != 0) {
234148847494SEnrico Perla - Sun Microsystems 			set_flag(NEED_CACHE_DIR);
234248847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
234348847494SEnrico Perla - Sun Microsystems 		}
234448847494SEnrico Perla - Sun Microsystems 
234548847494SEnrico Perla - Sun Microsystems 		walk_arg.sparcfile = fopen(get_cachedir(FILE64), "w");
234648847494SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile == NULL) {
234748847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, get_cachedir(FILE64),
234848847494SEnrico Perla - Sun Microsystems 			    strerror(errno));
234948847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
235048847494SEnrico Perla - Sun Microsystems 		}
235148847494SEnrico Perla - Sun Microsystems 
235248847494SEnrico Perla - Sun Microsystems 		set_dir_present(FILE64);
235348847494SEnrico Perla - Sun Microsystems 	} else {
235448847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
235548847494SEnrico Perla - Sun Microsystems 
235648847494SEnrico Perla - Sun Microsystems 		do {
235748847494SEnrico Perla - Sun Microsystems 			if (set_cache_dir(root, what) != 0)
235848847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
235948847494SEnrico Perla - Sun Microsystems 
236048847494SEnrico Perla - Sun Microsystems 			set_dir_present(what);
236148847494SEnrico Perla - Sun Microsystems 
236248847494SEnrico Perla - Sun Microsystems 			if (set_update_dir(root, what) != 0)
236348847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
236448847494SEnrico Perla - Sun Microsystems 			what++;
236548847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
236648847494SEnrico Perla - Sun Microsystems 	}
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	/*
23697c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
23707c478bd9Sstevel@tonic-gate 	 */
23717c478bd9Sstevel@tonic-gate 	if (bam_force) {
237248847494SEnrico Perla - Sun Microsystems 		if (!is_sparc())
237348847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE32, NEED_UPDATE);
237448847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE64, NEED_UPDATE);
237548847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
23767c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
237748847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
23787c478bd9Sstevel@tonic-gate 	}
23797c478bd9Sstevel@tonic-gate 
238048847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
23817c478bd9Sstevel@tonic-gate }
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate static error_t
23847c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
23857c478bd9Sstevel@tonic-gate {
23867c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
23877c478bd9Sstevel@tonic-gate 	FILE 		*fp;
23887c478bd9Sstevel@tonic-gate 	char 		buf[BAM_MAXLINE];
2389eb2bd662Svikram 	const char 	*fcn = "read_one_list()";
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
23947c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
2395eb2bd662Svikram 		BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno)));
23967c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
23977c478bd9Sstevel@tonic-gate 	}
23987c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
2399b610f78eSvikram 		/* skip blank lines */
2400b610f78eSvikram 		if (strspn(buf, " \t") == strlen(buf))
2401b610f78eSvikram 			continue;
24027c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
24037c478bd9Sstevel@tonic-gate 	}
24047c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
24057c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
24067c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
24077c478bd9Sstevel@tonic-gate 	}
24087c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
24097c478bd9Sstevel@tonic-gate }
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate static error_t
24127c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
24137c478bd9Sstevel@tonic-gate {
2414986fd29aSsetje 	char 		path[PATH_MAX];
2415986fd29aSsetje 	char 		cmd[PATH_MAX];
2416986fd29aSsetje 	struct stat 	sb;
2417986fd29aSsetje 	int 		n, rval;
2418eb2bd662Svikram 	const char 	*fcn = "read_list()";
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate 	/*
2423986fd29aSsetje 	 * build and check path to extract_boot_filelist.ksh
2424986fd29aSsetje 	 */
2425986fd29aSsetje 	n = snprintf(path, sizeof (path), "%s%s", root, EXTRACT_BOOT_FILELIST);
2426986fd29aSsetje 	if (n >= sizeof (path)) {
2427986fd29aSsetje 		bam_error(NO_FLIST);
2428986fd29aSsetje 		return (BAM_ERROR);
2429986fd29aSsetje 	}
2430986fd29aSsetje 
2431cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
2432cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
2433cedc7e57SEnrico Perla - Sun Microsystems 
2434986fd29aSsetje 	/*
2435986fd29aSsetje 	 * If extract_boot_filelist is present, exec it, otherwise read
2436986fd29aSsetje 	 * the filelists directly, for compatibility with older images.
2437986fd29aSsetje 	 */
2438986fd29aSsetje 	if (stat(path, &sb) == 0) {
2439986fd29aSsetje 		/*
2440986fd29aSsetje 		 * build arguments to exec extract_boot_filelist.ksh
2441986fd29aSsetje 		 */
2442d876c67dSjg 		char *rootarg, *platarg;
2443d876c67dSjg 		int platarglen = 1, rootarglen = 1;
2444d876c67dSjg 		if (strlen(root) > 1)
2445d876c67dSjg 			rootarglen += strlen(root) + strlen("-R ");
2446d876c67dSjg 		if (bam_alt_platform)
2447d876c67dSjg 			platarglen += strlen(bam_platform) + strlen("-p ");
2448d876c67dSjg 		platarg = s_calloc(1, platarglen);
2449d876c67dSjg 		rootarg = s_calloc(1, rootarglen);
2450d876c67dSjg 		*platarg = 0;
2451d876c67dSjg 		*rootarg = 0;
2452d876c67dSjg 
2453986fd29aSsetje 		if (strlen(root) > 1) {
2454d876c67dSjg 			(void) snprintf(rootarg, rootarglen,
2455d876c67dSjg 			    "-R %s", root);
2456986fd29aSsetje 		}
2457d876c67dSjg 		if (bam_alt_platform) {
2458d876c67dSjg 			(void) snprintf(platarg, platarglen,
2459d876c67dSjg 			    "-p %s", bam_platform);
2460d876c67dSjg 		}
2461d876c67dSjg 		n = snprintf(cmd, sizeof (cmd), "%s %s %s /%s /%s",
2462d876c67dSjg 		    path, rootarg, platarg, BOOT_FILE_LIST, ETC_FILE_LIST);
2463d876c67dSjg 		free(platarg);
2464d876c67dSjg 		free(rootarg);
2465986fd29aSsetje 		if (n >= sizeof (cmd)) {
2466986fd29aSsetje 			bam_error(NO_FLIST);
2467986fd29aSsetje 			return (BAM_ERROR);
2468986fd29aSsetje 		}
2469986fd29aSsetje 		if (exec_cmd(cmd, flistp) != 0) {
2470eb2bd662Svikram 			BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno)));
2471986fd29aSsetje 			return (BAM_ERROR);
2472986fd29aSsetje 		}
2473986fd29aSsetje 	} else {
2474986fd29aSsetje 		/*
24757c478bd9Sstevel@tonic-gate 		 * Read current lists of files - only the first is mandatory
24767c478bd9Sstevel@tonic-gate 		 */
24777c478bd9Sstevel@tonic-gate 		rval = read_one_list(root, flistp, BOOT_FILE_LIST);
24787c478bd9Sstevel@tonic-gate 		if (rval != BAM_SUCCESS)
24797c478bd9Sstevel@tonic-gate 			return (rval);
24807c478bd9Sstevel@tonic-gate 		(void) read_one_list(root, flistp, ETC_FILE_LIST);
2481986fd29aSsetje 	}
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
24847c478bd9Sstevel@tonic-gate 		bam_error(NO_FLIST);
24857c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
24867c478bd9Sstevel@tonic-gate 	}
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
24897c478bd9Sstevel@tonic-gate }
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate static void
24927c478bd9Sstevel@tonic-gate getoldstat(char *root)
24937c478bd9Sstevel@tonic-gate {
24947c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
24957c478bd9Sstevel@tonic-gate 	int 		fd, error;
24967c478bd9Sstevel@tonic-gate 	struct stat 	sb;
24977c478bd9Sstevel@tonic-gate 	char 		*ostat;
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
25007c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
25017c478bd9Sstevel@tonic-gate 	if (fd == -1) {
25027c478bd9Sstevel@tonic-gate 		if (bam_verbose)
25037c478bd9Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
250448847494SEnrico Perla - Sun Microsystems 		goto out_err;
25057c478bd9Sstevel@tonic-gate 	}
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
25087c478bd9Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
250948847494SEnrico Perla - Sun Microsystems 		goto out_err;
25107c478bd9Sstevel@tonic-gate 	}
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
25157c478bd9Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
25167c478bd9Sstevel@tonic-gate 		free(ostat);
251748847494SEnrico Perla - Sun Microsystems 		goto out_err;
25187c478bd9Sstevel@tonic-gate 	}
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	(void) close(fd);
252148847494SEnrico Perla - Sun Microsystems 	fd = -1;
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
25247c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	free(ostat);
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	if (error) {
25297c478bd9Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
25307c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
253148847494SEnrico Perla - Sun Microsystems 		goto out_err;
253248847494SEnrico Perla - Sun Microsystems 	} else {
25337c478bd9Sstevel@tonic-gate 		return;
25347c478bd9Sstevel@tonic-gate 	}
253548847494SEnrico Perla - Sun Microsystems 
253648847494SEnrico Perla - Sun Microsystems out_err:
253748847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
253848847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
2539cedc7e57SEnrico Perla - Sun Microsystems 	if (!is_flag_on(IS_SPARC_TARGET))
254048847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE32, NEED_UPDATE);
254148847494SEnrico Perla - Sun Microsystems 	set_dir_flag(FILE64, NEED_UPDATE);
254248847494SEnrico Perla - Sun Microsystems }
254348847494SEnrico Perla - Sun Microsystems 
254448847494SEnrico Perla - Sun Microsystems /* Best effort stale entry removal */
254548847494SEnrico Perla - Sun Microsystems static void
254648847494SEnrico Perla - Sun Microsystems delete_stale(char *file, int what)
254748847494SEnrico Perla - Sun Microsystems {
254848847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
254948847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
255048847494SEnrico Perla - Sun Microsystems 
255148847494SEnrico Perla - Sun Microsystems 	(void) snprintf(path, sizeof (path), "%s/%s", get_cachedir(what), file);
255248847494SEnrico Perla - Sun Microsystems 	if (!bam_check && stat(path, &sb) == 0) {
255348847494SEnrico Perla - Sun Microsystems 		if (sb.st_mode & S_IFDIR)
255448847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(path);
255548847494SEnrico Perla - Sun Microsystems 		else
255648847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
255748847494SEnrico Perla - Sun Microsystems 
255848847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, (NEED_UPDATE | NO_MULTI));
255948847494SEnrico Perla - Sun Microsystems 	}
25607c478bd9Sstevel@tonic-gate }
25617c478bd9Sstevel@tonic-gate 
2562a28d77b8Svikram /*
2563a28d77b8Svikram  * Checks if a file in the current (old) archive has
2564a28d77b8Svikram  * been deleted from the root filesystem. This is needed for
2565a28d77b8Svikram  * software like Trusted Extensions (TX) that switch early
2566a28d77b8Svikram  * in boot based on presence/absence of a kernel module.
2567a28d77b8Svikram  */
2568a28d77b8Svikram static void
2569a28d77b8Svikram check4stale(char *root)
2570a28d77b8Svikram {
2571a28d77b8Svikram 	nvpair_t	*nvp;
2572a28d77b8Svikram 	nvlist_t	*nvlp;
2573a28d77b8Svikram 	char 		*file;
2574a28d77b8Svikram 	char		path[PATH_MAX];
2575a28d77b8Svikram 
2576a28d77b8Svikram 	/*
2577a28d77b8Svikram 	 * Skip stale file check during smf check
2578a28d77b8Svikram 	 */
2579a28d77b8Svikram 	if (bam_smf_check)
2580a28d77b8Svikram 		return;
2581a28d77b8Svikram 
258248847494SEnrico Perla - Sun Microsystems 	/*
258348847494SEnrico Perla - Sun Microsystems 	 * If we need to (re)create the cache, there's no need to check for
258448847494SEnrico Perla - Sun Microsystems 	 * stale files
258548847494SEnrico Perla - Sun Microsystems 	 */
258648847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR))
258748847494SEnrico Perla - Sun Microsystems 		return;
258848847494SEnrico Perla - Sun Microsystems 
2589a28d77b8Svikram 	/* Nothing to do if no old stats */
2590a28d77b8Svikram 	if ((nvlp = walk_arg.old_nvlp) == NULL)
2591a28d77b8Svikram 		return;
2592a28d77b8Svikram 
2593a28d77b8Svikram 	for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp;
2594a28d77b8Svikram 	    nvp = nvlist_next_nvpair(nvlp, nvp)) {
2595a28d77b8Svikram 		file = nvpair_name(nvp);
2596a28d77b8Svikram 		if (file == NULL)
2597a28d77b8Svikram 			continue;
2598a28d77b8Svikram 		(void) snprintf(path, sizeof (path), "%s/%s",
2599a28d77b8Svikram 		    root, file);
260048847494SEnrico Perla - Sun Microsystems 		if (access(path, F_OK) < 0) {
260148847494SEnrico Perla - Sun Microsystems 			int	what;
260248847494SEnrico Perla - Sun Microsystems 
2603cedc7e57SEnrico Perla - Sun Microsystems 			if (bam_verbose)
2604cedc7e57SEnrico Perla - Sun Microsystems 				bam_print(PARSEABLE_STALE_FILE, path);
260548847494SEnrico Perla - Sun Microsystems 
2606cedc7e57SEnrico Perla - Sun Microsystems 			if (is_flag_on(IS_SPARC_TARGET)) {
2607cedc7e57SEnrico Perla - Sun Microsystems 				set_dir_flag(FILE64, NEED_UPDATE);
2608cedc7e57SEnrico Perla - Sun Microsystems 			} else {
260948847494SEnrico Perla - Sun Microsystems 				for (what = FILE32; what < CACHEDIR_NUM; what++)
261048847494SEnrico Perla - Sun Microsystems 					if (has_cachedir(what))
261148847494SEnrico Perla - Sun Microsystems 						delete_stale(file, what);
2612a28d77b8Svikram 			}
2613a28d77b8Svikram 		}
2614a28d77b8Svikram 	}
2615cedc7e57SEnrico Perla - Sun Microsystems }
2616a28d77b8Svikram 
26177c478bd9Sstevel@tonic-gate static void
26187c478bd9Sstevel@tonic-gate create_newstat(void)
26197c478bd9Sstevel@tonic-gate {
26207c478bd9Sstevel@tonic-gate 	int error;
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
26237c478bd9Sstevel@tonic-gate 	if (error) {
26247c478bd9Sstevel@tonic-gate 		/*
26257c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
26267c478bd9Sstevel@tonic-gate 		 */
26277c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
26287c478bd9Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
26297c478bd9Sstevel@tonic-gate 	}
26307c478bd9Sstevel@tonic-gate }
26317c478bd9Sstevel@tonic-gate 
263248847494SEnrico Perla - Sun Microsystems static int
26337c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
26347c478bd9Sstevel@tonic-gate {
26357c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
26367c478bd9Sstevel@tonic-gate 	line_t *lp;
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
2639986fd29aSsetje 		/*
2640986fd29aSsetje 		 * Don't follow symlinks.  A symlink must refer to
2641986fd29aSsetje 		 * a file that would appear in the archive through
2642986fd29aSsetje 		 * a direct reference.  This matches the archive
2643986fd29aSsetje 		 * construction behavior.
2644986fd29aSsetje 		 */
26457c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
2646986fd29aSsetje 		if (nftw(path, cmpstat, 20, FTW_PHYS) == -1) {
264748847494SEnrico Perla - Sun Microsystems 			if (is_flag_on(UPDATE_ERROR))
264848847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
26497c478bd9Sstevel@tonic-gate 			/*
26507c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
26517c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
26527c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
26537c478bd9Sstevel@tonic-gate 			 */
26547c478bd9Sstevel@tonic-gate 			if (bam_verbose)
26557c478bd9Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
26567c478bd9Sstevel@tonic-gate 		}
26577c478bd9Sstevel@tonic-gate 	}
265848847494SEnrico Perla - Sun Microsystems 
265948847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
26607c478bd9Sstevel@tonic-gate }
26617c478bd9Sstevel@tonic-gate 
26624a9df875SEnrico Perla - Sun Microsystems /*
26634a9df875SEnrico Perla - Sun Microsystems  * Update the timestamp file.
26644a9df875SEnrico Perla - Sun Microsystems  */
26654a9df875SEnrico Perla - Sun Microsystems static void
26664a9df875SEnrico Perla - Sun Microsystems update_timestamp(char *root)
26674a9df875SEnrico Perla - Sun Microsystems {
26684a9df875SEnrico Perla - Sun Microsystems 	char	timestamp_path[PATH_MAX];
26694a9df875SEnrico Perla - Sun Microsystems 
26704a9df875SEnrico Perla - Sun Microsystems 	/* this path length has already been checked in check_flags_and_files */
26714a9df875SEnrico Perla - Sun Microsystems 	(void) snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root,
26724a9df875SEnrico Perla - Sun Microsystems 	    FILE_STAT_TIMESTAMP);
26734a9df875SEnrico Perla - Sun Microsystems 
26744a9df875SEnrico Perla - Sun Microsystems 	/*
26754a9df875SEnrico Perla - Sun Microsystems 	 * recreate the timestamp file. Since an outdated or absent timestamp
26764a9df875SEnrico Perla - Sun Microsystems 	 * file translates in a complete rebuild of the archive cache, notify
26774a9df875SEnrico Perla - Sun Microsystems 	 * the user of the performance issue.
26784a9df875SEnrico Perla - Sun Microsystems 	 */
26794a9df875SEnrico Perla - Sun Microsystems 	if (creat(timestamp_path, FILE_STAT_MODE) < 0) {
26804a9df875SEnrico Perla - Sun Microsystems 		bam_error(OPEN_FAIL, timestamp_path, strerror(errno));
26814a9df875SEnrico Perla - Sun Microsystems 		bam_error(TIMESTAMP_FAIL, rootbuf);
26824a9df875SEnrico Perla - Sun Microsystems 	}
26834a9df875SEnrico Perla - Sun Microsystems }
26844a9df875SEnrico Perla - Sun Microsystems 
26854a9df875SEnrico Perla - Sun Microsystems 
26867c478bd9Sstevel@tonic-gate static void
26877c478bd9Sstevel@tonic-gate savenew(char *root)
26887c478bd9Sstevel@tonic-gate {
26897c478bd9Sstevel@tonic-gate 	char 	path[PATH_MAX];
26907c478bd9Sstevel@tonic-gate 	char 	path2[PATH_MAX];
26917c478bd9Sstevel@tonic-gate 	size_t 	sz;
26927c478bd9Sstevel@tonic-gate 	char 	*nstat;
26937c478bd9Sstevel@tonic-gate 	int 	fd, wrote, error;
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 	nstat = NULL;
26967c478bd9Sstevel@tonic-gate 	sz = 0;
26977c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
26987c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
26997c478bd9Sstevel@tonic-gate 	if (error) {
27007c478bd9Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
27017c478bd9Sstevel@tonic-gate 		return;
27027c478bd9Sstevel@tonic-gate 	}
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
27057c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
27067c478bd9Sstevel@tonic-gate 	if (fd == -1) {
27077c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
27087c478bd9Sstevel@tonic-gate 		free(nstat);
27097c478bd9Sstevel@tonic-gate 		return;
27107c478bd9Sstevel@tonic-gate 	}
27117c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
27127c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
27137c478bd9Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
27147c478bd9Sstevel@tonic-gate 		(void) close(fd);
27157c478bd9Sstevel@tonic-gate 		free(nstat);
27167c478bd9Sstevel@tonic-gate 		return;
27177c478bd9Sstevel@tonic-gate 	}
27187c478bd9Sstevel@tonic-gate 	(void) close(fd);
27197c478bd9Sstevel@tonic-gate 	free(nstat);
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
27227c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
27237c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
27247c478bd9Sstevel@tonic-gate 	}
27257c478bd9Sstevel@tonic-gate }
27267c478bd9Sstevel@tonic-gate 
272748847494SEnrico Perla - Sun Microsystems #define	init_walk_args()	bzero(&walk_arg, sizeof (walk_arg))
272848847494SEnrico Perla - Sun Microsystems 
27297c478bd9Sstevel@tonic-gate static void
27307c478bd9Sstevel@tonic-gate clear_walk_args(void)
27317c478bd9Sstevel@tonic-gate {
27327c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
27337c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
27347c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
27357c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
273648847494SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile)
273748847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
27387c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
27397c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
274048847494SEnrico Perla - Sun Microsystems 	walk_arg.sparcfile = NULL;
27417c478bd9Sstevel@tonic-gate }
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate /*
27447c478bd9Sstevel@tonic-gate  * Returns:
27457c478bd9Sstevel@tonic-gate  *	0 - no update necessary
27467c478bd9Sstevel@tonic-gate  *	1 - update required.
27477c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
27487c478bd9Sstevel@tonic-gate  *
27497c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
27507c478bd9Sstevel@tonic-gate  * ================================
27517c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
27527c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
27537c478bd9Sstevel@tonic-gate  * to out of sync files.
27547c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
27557c478bd9Sstevel@tonic-gate  *
27567c478bd9Sstevel@tonic-gate  */
27577c478bd9Sstevel@tonic-gate static int
27587c478bd9Sstevel@tonic-gate update_required(char *root)
27597c478bd9Sstevel@tonic-gate {
27607c478bd9Sstevel@tonic-gate 	struct stat 	sb;
27617c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
27627c478bd9Sstevel@tonic-gate 	filelist_t 	flist;
27637c478bd9Sstevel@tonic-gate 	filelist_t 	*flistp = &flist;
276448847494SEnrico Perla - Sun Microsystems 	int 		ret;
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
27677c478bd9Sstevel@tonic-gate 
276848847494SEnrico Perla - Sun Microsystems 	if (is_sparc())
276948847494SEnrico Perla - Sun Microsystems 		set_flag(IS_SPARC_TARGET);
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	/*
277248847494SEnrico Perla - Sun Microsystems 	 * Check if cache directories and archives are present
27737c478bd9Sstevel@tonic-gate 	 */
277448847494SEnrico Perla - Sun Microsystems 
277548847494SEnrico Perla - Sun Microsystems 	ret = check_flags_and_files(root);
277648847494SEnrico Perla - Sun Microsystems 	if (ret < 0)
277748847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 	/*
27807c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
2781eff168e0SEnrico Perla - Sun Microsystems 	 * exist. Do not stop the boot process, but trigger an update
2782eff168e0SEnrico Perla - Sun Microsystems 	 * of the archives (which will recreate filestat.ramdisk).
27837c478bd9Sstevel@tonic-gate 	 */
27847c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
27857c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
2786eff168e0SEnrico Perla - Sun Microsystems 		if (stat(path, &sb) != 0) {
2787eff168e0SEnrico Perla - Sun Microsystems 			(void) creat(NEED_UPDATE_FILE, 0644);
27887c478bd9Sstevel@tonic-gate 			return (0);
27897c478bd9Sstevel@tonic-gate 		}
2790eff168e0SEnrico Perla - Sun Microsystems 	}
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	getoldstat(root);
27937c478bd9Sstevel@tonic-gate 
27947c478bd9Sstevel@tonic-gate 	/*
2795a28d77b8Svikram 	 * Check if the archive contains files that are no longer
2796a28d77b8Svikram 	 * present on the root filesystem.
2797a28d77b8Svikram 	 */
2798a28d77b8Svikram 	check4stale(root);
2799a28d77b8Svikram 
2800a28d77b8Svikram 	/*
28017c478bd9Sstevel@tonic-gate 	 * read list of files
28027c478bd9Sstevel@tonic-gate 	 */
28037c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
28047c478bd9Sstevel@tonic-gate 		clear_walk_args();
28057c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
28067c478bd9Sstevel@tonic-gate 	}
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 	/*
28117c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
28127c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
28137c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
28147c478bd9Sstevel@tonic-gate 	 */
28157c478bd9Sstevel@tonic-gate 	create_newstat();
28167c478bd9Sstevel@tonic-gate 	/*
28177c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
28187c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
28197c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
28207c478bd9Sstevel@tonic-gate 	 */
282148847494SEnrico Perla - Sun Microsystems 	ret = walk_list(root, &flist);
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	/* done with the file list */
28247c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
28257c478bd9Sstevel@tonic-gate 
282648847494SEnrico Perla - Sun Microsystems 	/* something went wrong */
282748847494SEnrico Perla - Sun Microsystems 
282848847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
282948847494SEnrico Perla - Sun Microsystems 		bam_error(CACHE_FAIL);
283048847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
28317c478bd9Sstevel@tonic-gate 	}
28327c478bd9Sstevel@tonic-gate 
283348847494SEnrico Perla - Sun Microsystems 	if (walk_arg.new_nvlp == NULL) {
2834cedc7e57SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile != NULL)
283548847494SEnrico Perla - Sun Microsystems 			(void) fclose(walk_arg.sparcfile);
283648847494SEnrico Perla - Sun Microsystems 		bam_error(NO_NEW_STAT);
283748847494SEnrico Perla - Sun Microsystems 	}
28387c478bd9Sstevel@tonic-gate 
283948847494SEnrico Perla - Sun Microsystems 	/* If nothing was updated, discard newstat. */
284048847494SEnrico Perla - Sun Microsystems 
284148847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(FILE32, NEED_UPDATE) &&
284248847494SEnrico Perla - Sun Microsystems 	    !is_dir_flag_on(FILE64, NEED_UPDATE)) {
28437c478bd9Sstevel@tonic-gate 		clear_walk_args();
28447c478bd9Sstevel@tonic-gate 		return (0);
28457c478bd9Sstevel@tonic-gate 	}
28467c478bd9Sstevel@tonic-gate 
2847cedc7e57SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile != NULL)
284848847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
284948847494SEnrico Perla - Sun Microsystems 
28507c478bd9Sstevel@tonic-gate 	return (1);
28517c478bd9Sstevel@tonic-gate }
28527c478bd9Sstevel@tonic-gate 
285348847494SEnrico Perla - Sun Microsystems static int
285448847494SEnrico Perla - Sun Microsystems flushfs(char *root)
285548847494SEnrico Perla - Sun Microsystems {
285648847494SEnrico Perla - Sun Microsystems 	char	cmd[PATH_MAX + 30];
285748847494SEnrico Perla - Sun Microsystems 
285848847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmd, sizeof (cmd), "%s -f \"%s\" 2>/dev/null",
285948847494SEnrico Perla - Sun Microsystems 	    LOCKFS_PATH, root);
286048847494SEnrico Perla - Sun Microsystems 
286148847494SEnrico Perla - Sun Microsystems 	return (exec_cmd(cmd, NULL));
286248847494SEnrico Perla - Sun Microsystems }
286348847494SEnrico Perla - Sun Microsystems 
286448847494SEnrico Perla - Sun Microsystems static int
286548847494SEnrico Perla - Sun Microsystems do_archive_copy(char *source, char *dest)
286648847494SEnrico Perla - Sun Microsystems {
286748847494SEnrico Perla - Sun Microsystems 
2868cedc7e57SEnrico Perla - Sun Microsystems 	sync();
286948847494SEnrico Perla - Sun Microsystems 
287048847494SEnrico Perla - Sun Microsystems 	/* the equivalent of mv archive-new-$pid boot_archive */
2871cedc7e57SEnrico Perla - Sun Microsystems 	if (rename(source, dest) != 0) {
2872cedc7e57SEnrico Perla - Sun Microsystems 		(void) unlink(source);
287348847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
2874cedc7e57SEnrico Perla - Sun Microsystems 	}
287548847494SEnrico Perla - Sun Microsystems 
287648847494SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
2877cedc7e57SEnrico Perla - Sun Microsystems 		sync();
287848847494SEnrico Perla - Sun Microsystems 
287948847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
288048847494SEnrico Perla - Sun Microsystems }
288148847494SEnrico Perla - Sun Microsystems 
288248847494SEnrico Perla - Sun Microsystems static int
288348847494SEnrico Perla - Sun Microsystems check_cmdline(filelist_t flist)
288448847494SEnrico Perla - Sun Microsystems {
288548847494SEnrico Perla - Sun Microsystems 	line_t	*lp;
288648847494SEnrico Perla - Sun Microsystems 
288748847494SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next) {
288848847494SEnrico Perla - Sun Microsystems 		if (strstr(lp->line, "Error:") != NULL ||
288948847494SEnrico Perla - Sun Microsystems 		    strstr(lp->line, "Inode number overflow") != NULL) {
2890cedc7e57SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, "%s\n", lp->line);
289148847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
289248847494SEnrico Perla - Sun Microsystems 		}
289348847494SEnrico Perla - Sun Microsystems 	}
289448847494SEnrico Perla - Sun Microsystems 
289548847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
289648847494SEnrico Perla - Sun Microsystems }
289748847494SEnrico Perla - Sun Microsystems 
2898cedc7e57SEnrico Perla - Sun Microsystems static void
2899cedc7e57SEnrico Perla - Sun Microsystems dump_errormsg(filelist_t flist)
2900cedc7e57SEnrico Perla - Sun Microsystems {
2901cedc7e57SEnrico Perla - Sun Microsystems 	line_t	*lp;
2902cedc7e57SEnrico Perla - Sun Microsystems 
2903cedc7e57SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next)
2904cedc7e57SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, "%s\n", lp->line);
2905cedc7e57SEnrico Perla - Sun Microsystems }
2906cedc7e57SEnrico Perla - Sun Microsystems 
290748847494SEnrico Perla - Sun Microsystems static int
290848847494SEnrico Perla - Sun Microsystems check_archive(char *dest)
290948847494SEnrico Perla - Sun Microsystems {
291048847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
291148847494SEnrico Perla - Sun Microsystems 
291248847494SEnrico Perla - Sun Microsystems 	if (stat(dest, &sb) != 0 || !S_ISREG(sb.st_mode) ||
291348847494SEnrico Perla - Sun Microsystems 	    sb.st_size < 10000) {
291448847494SEnrico Perla - Sun Microsystems 		bam_error(ARCHIVE_BAD, dest);
291548847494SEnrico Perla - Sun Microsystems 		(void) unlink(dest);
291648847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
291748847494SEnrico Perla - Sun Microsystems 	}
291848847494SEnrico Perla - Sun Microsystems 
291948847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
292048847494SEnrico Perla - Sun Microsystems }
292148847494SEnrico Perla - Sun Microsystems 
292248847494SEnrico Perla - Sun Microsystems /*
292348847494SEnrico Perla - Sun Microsystems  * Returns 1 if mkiso is in the expected PATH, 0 otherwise
292448847494SEnrico Perla - Sun Microsystems  */
292548847494SEnrico Perla - Sun Microsystems static int
292648847494SEnrico Perla - Sun Microsystems is_mkisofs()
292748847494SEnrico Perla - Sun Microsystems {
2928cedc7e57SEnrico Perla - Sun Microsystems 	if (access(MKISOFS_PATH, X_OK) == 0)
292948847494SEnrico Perla - Sun Microsystems 		return (1);
293048847494SEnrico Perla - Sun Microsystems 	return (0);
293148847494SEnrico Perla - Sun Microsystems }
293248847494SEnrico Perla - Sun Microsystems 
293348847494SEnrico Perla - Sun Microsystems #define	MKISO_PARAMS	" -quiet -graft-points -dlrDJN -relaxed-filenames "
293448847494SEnrico Perla - Sun Microsystems 
293548847494SEnrico Perla - Sun Microsystems static int
293648847494SEnrico Perla - Sun Microsystems create_sparc_archive(char *archive, char *tempname, char *bootblk, char *list)
293748847494SEnrico Perla - Sun Microsystems {
293848847494SEnrico Perla - Sun Microsystems 	int		ret;
293948847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
294048847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
294148847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_sparc_archive()";
294248847494SEnrico Perla - Sun Microsystems 
294348847494SEnrico Perla - Sun Microsystems 	if (access(bootblk, R_OK) == 1) {
294448847494SEnrico Perla - Sun Microsystems 		bam_error(BOOTBLK_FAIL, bootblk);
294548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
294648847494SEnrico Perla - Sun Microsystems 	}
294748847494SEnrico Perla - Sun Microsystems 
294848847494SEnrico Perla - Sun Microsystems 	/*
294948847494SEnrico Perla - Sun Microsystems 	 * Prepare mkisofs command line and execute it
295048847494SEnrico Perla - Sun Microsystems 	 */
295148847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -G %s -o \"%s\" "
2952cedc7e57SEnrico Perla - Sun Microsystems 	    "-path-list \"%s\" 2>&1", MKISOFS_PATH, MKISO_PARAMS, bootblk,
295348847494SEnrico Perla - Sun Microsystems 	    tempname, list);
295448847494SEnrico Perla - Sun Microsystems 
295548847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
295648847494SEnrico Perla - Sun Microsystems 
295748847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
2958cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
2959cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
296048847494SEnrico Perla - Sun Microsystems 		goto out_err;
2961cedc7e57SEnrico Perla - Sun Microsystems 	}
296248847494SEnrico Perla - Sun Microsystems 
296348847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
296448847494SEnrico Perla - Sun Microsystems 
296548847494SEnrico Perla - Sun Microsystems 	/*
296648847494SEnrico Perla - Sun Microsystems 	 * Prepare dd command line to copy the bootblk on the new archive and
296748847494SEnrico Perla - Sun Microsystems 	 * execute it
296848847494SEnrico Perla - Sun Microsystems 	 */
296948847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=\"%s\" of=\"%s\""
2970cedc7e57SEnrico Perla - Sun Microsystems 	    " bs=1b oseek=1 count=15 conv=notrunc conv=sync 2>&1", DD_PATH_USR,
297148847494SEnrico Perla - Sun Microsystems 	    bootblk, tempname);
297248847494SEnrico Perla - Sun Microsystems 
297348847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
297448847494SEnrico Perla - Sun Microsystems 
297548847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
297648847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR)
297748847494SEnrico Perla - Sun Microsystems 		goto out_err;
297848847494SEnrico Perla - Sun Microsystems 
297948847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
298048847494SEnrico Perla - Sun Microsystems 
298148847494SEnrico Perla - Sun Microsystems 	/* Did we get a valid archive ? */
298248847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
298348847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
298448847494SEnrico Perla - Sun Microsystems 
298548847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
298648847494SEnrico Perla - Sun Microsystems 
298748847494SEnrico Perla - Sun Microsystems out_err:
298848847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
298948847494SEnrico Perla - Sun Microsystems 	bam_error(ARCHIVE_FAIL, cmdline);
299048847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
299148847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
299248847494SEnrico Perla - Sun Microsystems }
299348847494SEnrico Perla - Sun Microsystems 
299448847494SEnrico Perla - Sun Microsystems static unsigned int
299548847494SEnrico Perla - Sun Microsystems from_733(unsigned char *s)
299648847494SEnrico Perla - Sun Microsystems {
299748847494SEnrico Perla - Sun Microsystems 	int		i;
299848847494SEnrico Perla - Sun Microsystems 	unsigned int	ret = 0;
299948847494SEnrico Perla - Sun Microsystems 
300048847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
300148847494SEnrico Perla - Sun Microsystems 		ret |= s[i] << (8 * i);
300248847494SEnrico Perla - Sun Microsystems 
300348847494SEnrico Perla - Sun Microsystems 	return (ret);
300448847494SEnrico Perla - Sun Microsystems }
300548847494SEnrico Perla - Sun Microsystems 
300648847494SEnrico Perla - Sun Microsystems static void
300748847494SEnrico Perla - Sun Microsystems to_733(unsigned char *s, unsigned int val)
300848847494SEnrico Perla - Sun Microsystems {
300948847494SEnrico Perla - Sun Microsystems 	int	i;
301048847494SEnrico Perla - Sun Microsystems 
301148847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
301248847494SEnrico Perla - Sun Microsystems 		s[i] = s[7-i] = (val >> (8 * i)) & 0xFF;
301348847494SEnrico Perla - Sun Microsystems }
301448847494SEnrico Perla - Sun Microsystems 
301548847494SEnrico Perla - Sun Microsystems /*
301648847494SEnrico Perla - Sun Microsystems  * Extends the current boot archive without recreating it from scratch
301748847494SEnrico Perla - Sun Microsystems  */
301848847494SEnrico Perla - Sun Microsystems static int
301948847494SEnrico Perla - Sun Microsystems extend_iso_archive(char *archive, char *tempname, char *update_dir)
302048847494SEnrico Perla - Sun Microsystems {
302148847494SEnrico Perla - Sun Microsystems 	int			fd = -1, newfd = -1, ret, i;
302248847494SEnrico Perla - Sun Microsystems 	int			next_session = 0, new_size = 0;
302348847494SEnrico Perla - Sun Microsystems 	char			cmdline[3 * PATH_MAX + 64];
302448847494SEnrico Perla - Sun Microsystems 	const char		*func = "extend_iso_archive()";
302548847494SEnrico Perla - Sun Microsystems 	filelist_t		flist = {0};
302648847494SEnrico Perla - Sun Microsystems 	struct iso_pdesc	saved_desc[MAX_IVDs];
302748847494SEnrico Perla - Sun Microsystems 
302848847494SEnrico Perla - Sun Microsystems 	fd = open(archive, O_RDWR);
302948847494SEnrico Perla - Sun Microsystems 	if (fd == -1) {
3030cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
303148847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, archive, strerror(errno));
303248847494SEnrico Perla - Sun Microsystems 		goto out_err;
303348847494SEnrico Perla - Sun Microsystems 	}
303448847494SEnrico Perla - Sun Microsystems 
303548847494SEnrico Perla - Sun Microsystems 	/*
303648847494SEnrico Perla - Sun Microsystems 	 * A partial read is likely due to a corrupted file
303748847494SEnrico Perla - Sun Microsystems 	 */
303848847494SEnrico Perla - Sun Microsystems 	ret = pread64(fd, saved_desc, sizeof (saved_desc),
303948847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
304048847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
3041cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
304248847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, archive, strerror(errno));
304348847494SEnrico Perla - Sun Microsystems 		goto out_err;
304448847494SEnrico Perla - Sun Microsystems 	}
304548847494SEnrico Perla - Sun Microsystems 
304648847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
3047cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
304848847494SEnrico Perla - Sun Microsystems 			bam_error(SIGN_FAIL, archive);
304948847494SEnrico Perla - Sun Microsystems 		goto out_err;
305048847494SEnrico Perla - Sun Microsystems 	}
305148847494SEnrico Perla - Sun Microsystems 
305248847494SEnrico Perla - Sun Microsystems 	/*
305348847494SEnrico Perla - Sun Microsystems 	 * Read primary descriptor and locate next_session offset (it should
305448847494SEnrico Perla - Sun Microsystems 	 * point to the end of the archive)
305548847494SEnrico Perla - Sun Microsystems 	 */
305648847494SEnrico Perla - Sun Microsystems 	next_session = P2ROUNDUP(from_733(saved_desc[0].volume_space_size), 16);
305748847494SEnrico Perla - Sun Microsystems 
305848847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s -C 16,%d -M %s %s -o \""
3059cedc7e57SEnrico Perla - Sun Microsystems 	    "%s\" \"%s\" 2>&1", MKISOFS_PATH, next_session, archive,
3060cedc7e57SEnrico Perla - Sun Microsystems 	    MKISO_PARAMS, tempname, update_dir);
306148847494SEnrico Perla - Sun Microsystems 
306248847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
306348847494SEnrico Perla - Sun Microsystems 
306448847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
306548847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3066cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose) {
306748847494SEnrico Perla - Sun Microsystems 			bam_error(MULTI_FAIL, cmdline);
3068cedc7e57SEnrico Perla - Sun Microsystems 			dump_errormsg(flist);
3069cedc7e57SEnrico Perla - Sun Microsystems 		}
307048847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
307148847494SEnrico Perla - Sun Microsystems 	}
307248847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
307348847494SEnrico Perla - Sun Microsystems 
307448847494SEnrico Perla - Sun Microsystems 	newfd = open(tempname, O_RDONLY);
307548847494SEnrico Perla - Sun Microsystems 	if (newfd == -1) {
3076cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
307748847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, archive, strerror(errno));
307848847494SEnrico Perla - Sun Microsystems 		goto out_err;
307948847494SEnrico Perla - Sun Microsystems 	}
308048847494SEnrico Perla - Sun Microsystems 
308148847494SEnrico Perla - Sun Microsystems 	ret = pread64(newfd, saved_desc, sizeof (saved_desc),
308248847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
308348847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
3084cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
308548847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, archive, strerror(errno));
308648847494SEnrico Perla - Sun Microsystems 		goto out_err;
308748847494SEnrico Perla - Sun Microsystems 	}
308848847494SEnrico Perla - Sun Microsystems 
308948847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
3090cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
309148847494SEnrico Perla - Sun Microsystems 			bam_error(SIGN_FAIL, archive);
309248847494SEnrico Perla - Sun Microsystems 		goto out_err;
309348847494SEnrico Perla - Sun Microsystems 	}
309448847494SEnrico Perla - Sun Microsystems 
309548847494SEnrico Perla - Sun Microsystems 	new_size = from_733(saved_desc[0].volume_space_size) + next_session;
309648847494SEnrico Perla - Sun Microsystems 	to_733(saved_desc[0].volume_space_size, new_size);
309748847494SEnrico Perla - Sun Microsystems 
309848847494SEnrico Perla - Sun Microsystems 	for (i = 1; i < MAX_IVDs; i++) {
309948847494SEnrico Perla - Sun Microsystems 		if (saved_desc[i].type[0] == (unsigned char)255)
310048847494SEnrico Perla - Sun Microsystems 			break;
310148847494SEnrico Perla - Sun Microsystems 		if (memcmp(saved_desc[i].id, "CD001", 5))
310248847494SEnrico Perla - Sun Microsystems 			break;
310348847494SEnrico Perla - Sun Microsystems 
310448847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
310548847494SEnrico Perla - Sun Microsystems 			bam_print("%s: Updating descriptor entry [%d]\n", func,
310648847494SEnrico Perla - Sun Microsystems 			    i);
310748847494SEnrico Perla - Sun Microsystems 
310848847494SEnrico Perla - Sun Microsystems 		to_733(saved_desc[i].volume_space_size, new_size);
310948847494SEnrico Perla - Sun Microsystems 	}
311048847494SEnrico Perla - Sun Microsystems 
311148847494SEnrico Perla - Sun Microsystems 	ret = pwrite64(fd, saved_desc, DVD_BLOCK, VOLDESC_OFF*CD_BLOCK);
311248847494SEnrico Perla - Sun Microsystems 	if (ret != DVD_BLOCK) {
3113cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
311448847494SEnrico Perla - Sun Microsystems 			bam_error(WRITE_FAIL, archive, strerror(errno));
311548847494SEnrico Perla - Sun Microsystems 		goto out_err;
311648847494SEnrico Perla - Sun Microsystems 	}
311748847494SEnrico Perla - Sun Microsystems 	(void) close(newfd);
311848847494SEnrico Perla - Sun Microsystems 	newfd = -1;
311948847494SEnrico Perla - Sun Microsystems 
3120cedc7e57SEnrico Perla - Sun Microsystems 	ret = fsync(fd);
3121cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0)
3122cedc7e57SEnrico Perla - Sun Microsystems 		sync();
3123cedc7e57SEnrico Perla - Sun Microsystems 
312448847494SEnrico Perla - Sun Microsystems 	ret = close(fd);
312548847494SEnrico Perla - Sun Microsystems 	if (ret != 0) {
3126cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
312748847494SEnrico Perla - Sun Microsystems 			bam_error(CLOSE_FAIL, archive, strerror(errno));
312848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
312948847494SEnrico Perla - Sun Microsystems 	}
313048847494SEnrico Perla - Sun Microsystems 	fd = -1;
313148847494SEnrico Perla - Sun Microsystems 
313248847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=%s of=%s bs=32k "
3133cedc7e57SEnrico Perla - Sun Microsystems 	    "seek=%d conv=sync 2>&1", DD_PATH_USR, tempname, archive,
313448847494SEnrico Perla - Sun Microsystems 	    (next_session/16));
313548847494SEnrico Perla - Sun Microsystems 
313648847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
313748847494SEnrico Perla - Sun Microsystems 
313848847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
313948847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
3140cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
314148847494SEnrico Perla - Sun Microsystems 			bam_error(MULTI_FAIL, cmdline);
314248847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
314348847494SEnrico Perla - Sun Microsystems 	}
314448847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
314548847494SEnrico Perla - Sun Microsystems 
314648847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
314748847494SEnrico Perla - Sun Microsystems 
3148cedc7e57SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
3149cedc7e57SEnrico Perla - Sun Microsystems 		sync();
3150cedc7e57SEnrico Perla - Sun Microsystems 
315148847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
315248847494SEnrico Perla - Sun Microsystems 		bam_print("boot archive updated successfully\n");
315348847494SEnrico Perla - Sun Microsystems 
315448847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
315548847494SEnrico Perla - Sun Microsystems 
315648847494SEnrico Perla - Sun Microsystems out_flist_err:
315748847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
315848847494SEnrico Perla - Sun Microsystems out_err:
315948847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
316048847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
316148847494SEnrico Perla - Sun Microsystems 	if (newfd != -1)
316248847494SEnrico Perla - Sun Microsystems 		(void) close(newfd);
316348847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
316448847494SEnrico Perla - Sun Microsystems }
316548847494SEnrico Perla - Sun Microsystems 
316648847494SEnrico Perla - Sun Microsystems static int
316748847494SEnrico Perla - Sun Microsystems create_x86_archive(char *archive, char *tempname, char *update_dir)
316848847494SEnrico Perla - Sun Microsystems {
316948847494SEnrico Perla - Sun Microsystems 	int		ret;
317048847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
317148847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
317248847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_x86_archive()";
317348847494SEnrico Perla - Sun Microsystems 
317448847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -o \"%s\" \"%s\" "
3175cedc7e57SEnrico Perla - Sun Microsystems 	    "2>&1", MKISOFS_PATH, MKISO_PARAMS, tempname, update_dir);
317648847494SEnrico Perla - Sun Microsystems 
317748847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
317848847494SEnrico Perla - Sun Microsystems 
317948847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
318048847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
318148847494SEnrico Perla - Sun Microsystems 		bam_error(ARCHIVE_FAIL, cmdline);
3182cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
318348847494SEnrico Perla - Sun Microsystems 		filelist_free(&flist);
318448847494SEnrico Perla - Sun Microsystems 		(void) unlink(tempname);
318548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
318648847494SEnrico Perla - Sun Microsystems 	}
318748847494SEnrico Perla - Sun Microsystems 
318848847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
318948847494SEnrico Perla - Sun Microsystems 
319048847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
319148847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
319248847494SEnrico Perla - Sun Microsystems 
319348847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
319448847494SEnrico Perla - Sun Microsystems }
319548847494SEnrico Perla - Sun Microsystems 
319648847494SEnrico Perla - Sun Microsystems static int
319748847494SEnrico Perla - Sun Microsystems mkisofs_archive(char *root, int what)
319848847494SEnrico Perla - Sun Microsystems {
319948847494SEnrico Perla - Sun Microsystems 	int		ret;
320048847494SEnrico Perla - Sun Microsystems 	char		temp[PATH_MAX];
320148847494SEnrico Perla - Sun Microsystems 	char 		bootblk[PATH_MAX];
320248847494SEnrico Perla - Sun Microsystems 	char		boot_archive[PATH_MAX];
320348847494SEnrico Perla - Sun Microsystems 
320448847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
320548847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp),
320648847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64/archive-new-%d", root, ARCHIVE_PREFIX,
320748847494SEnrico Perla - Sun Microsystems 		    get_machine(), getpid());
320848847494SEnrico Perla - Sun Microsystems 	else
320948847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp), "%s%s%s/archive-new-%d",
321048847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), getpid());
321148847494SEnrico Perla - Sun Microsystems 
321248847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (temp))
321348847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
321448847494SEnrico Perla - Sun Microsystems 
321548847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
321648847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
321748847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
321848847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
321948847494SEnrico Perla - Sun Microsystems 	else
322048847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
322148847494SEnrico Perla - Sun Microsystems 		    "%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(),
322248847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
322348847494SEnrico Perla - Sun Microsystems 
322448847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (boot_archive))
322548847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
322648847494SEnrico Perla - Sun Microsystems 
322748847494SEnrico Perla - Sun Microsystems 	bam_print("updating %s\n", boot_archive);
322848847494SEnrico Perla - Sun Microsystems 
322948847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
323048847494SEnrico Perla - Sun Microsystems 		ret = snprintf(bootblk, sizeof (bootblk),
323148847494SEnrico Perla - Sun Microsystems 		    "%s/platform/%s/lib/fs/hsfs/bootblk", root, get_machine());
323248847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (bootblk))
323348847494SEnrico Perla - Sun Microsystems 			goto out_path_err;
323448847494SEnrico Perla - Sun Microsystems 
323548847494SEnrico Perla - Sun Microsystems 		ret = create_sparc_archive(boot_archive, temp, bootblk,
323648847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
323748847494SEnrico Perla - Sun Microsystems 	} else {
323848847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI)) {
323948847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
324048847494SEnrico Perla - Sun Microsystems 				bam_print("Attempting to extend x86 archive: "
324148847494SEnrico Perla - Sun Microsystems 				    "%s\n", boot_archive);
324248847494SEnrico Perla - Sun Microsystems 
324348847494SEnrico Perla - Sun Microsystems 			ret = extend_iso_archive(boot_archive, temp,
324448847494SEnrico Perla - Sun Microsystems 			    get_updatedir(what));
324548847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_SUCCESS) {
324648847494SEnrico Perla - Sun Microsystems 				if (bam_verbose)
324748847494SEnrico Perla - Sun Microsystems 					bam_print("Successfully extended %s\n",
324848847494SEnrico Perla - Sun Microsystems 					    boot_archive);
324948847494SEnrico Perla - Sun Microsystems 
325048847494SEnrico Perla - Sun Microsystems 				(void) rmdir_r(get_updatedir(what));
325148847494SEnrico Perla - Sun Microsystems 				return (BAM_SUCCESS);
325248847494SEnrico Perla - Sun Microsystems 			}
325348847494SEnrico Perla - Sun Microsystems 		}
325448847494SEnrico Perla - Sun Microsystems 		/*
325548847494SEnrico Perla - Sun Microsystems 		 * The boot archive will be recreated from scratch. We get here
325648847494SEnrico Perla - Sun Microsystems 		 * if at least one of these conditions is true:
325748847494SEnrico Perla - Sun Microsystems 		 * - bootadm was called without the -e switch
325848847494SEnrico Perla - Sun Microsystems 		 * - the archive (or the archive cache) doesn't exist
325948847494SEnrico Perla - Sun Microsystems 		 * - archive size is bigger than BA_SIZE_MAX
326048847494SEnrico Perla - Sun Microsystems 		 * - more than COUNT_MAX files need to be updated
326148847494SEnrico Perla - Sun Microsystems 		 * - an error occourred either populating the /updates directory
326248847494SEnrico Perla - Sun Microsystems 		 *   or extend_iso_archive() failed
326348847494SEnrico Perla - Sun Microsystems 		 */
326448847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
326548847494SEnrico Perla - Sun Microsystems 			bam_print("Unable to extend %s... rebuilding archive\n",
326648847494SEnrico Perla - Sun Microsystems 			    boot_archive);
326748847494SEnrico Perla - Sun Microsystems 
326848847494SEnrico Perla - Sun Microsystems 		if (get_updatedir(what)[0] != '\0')
326948847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(get_updatedir(what));
327048847494SEnrico Perla - Sun Microsystems 
327148847494SEnrico Perla - Sun Microsystems 
327248847494SEnrico Perla - Sun Microsystems 		ret = create_x86_archive(boot_archive, temp,
327348847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
327448847494SEnrico Perla - Sun Microsystems 	}
327548847494SEnrico Perla - Sun Microsystems 
327648847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_SUCCESS && bam_verbose)
327748847494SEnrico Perla - Sun Microsystems 		bam_print("Successfully created %s\n", boot_archive);
327848847494SEnrico Perla - Sun Microsystems 
327948847494SEnrico Perla - Sun Microsystems 	return (ret);
328048847494SEnrico Perla - Sun Microsystems 
328148847494SEnrico Perla - Sun Microsystems out_path_err:
328248847494SEnrico Perla - Sun Microsystems 	bam_error(PATH_TOO_LONG, root);
328348847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
328448847494SEnrico Perla - Sun Microsystems }
328548847494SEnrico Perla - Sun Microsystems 
32867c478bd9Sstevel@tonic-gate static error_t
32877c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
32887c478bd9Sstevel@tonic-gate {
32897c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
32907c478bd9Sstevel@tonic-gate 	size_t len;
32917c478bd9Sstevel@tonic-gate 	struct stat sb;
3292cedc7e57SEnrico Perla - Sun Microsystems 	int ret, what, status = BAM_SUCCESS;
329348847494SEnrico Perla - Sun Microsystems 
329448847494SEnrico Perla - Sun Microsystems 	/* If there is mkisofs, use it to create the required archives */
329548847494SEnrico Perla - Sun Microsystems 	if (is_mkisofs()) {
329648847494SEnrico Perla - Sun Microsystems 		for (what = FILE32; what < CACHEDIR_NUM; what++) {
3297cedc7e57SEnrico Perla - Sun Microsystems 			if (has_cachedir(what) && is_dir_flag_on(what,
3298cedc7e57SEnrico Perla - Sun Microsystems 			    NEED_UPDATE)) {
329948847494SEnrico Perla - Sun Microsystems 				ret = mkisofs_archive(root, what);
330048847494SEnrico Perla - Sun Microsystems 				if (ret != 0)
3301cedc7e57SEnrico Perla - Sun Microsystems 					status = BAM_ERROR;
330248847494SEnrico Perla - Sun Microsystems 			}
330348847494SEnrico Perla - Sun Microsystems 		}
3304cedc7e57SEnrico Perla - Sun Microsystems 		return (status);
330548847494SEnrico Perla - Sun Microsystems 	}
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate 	/*
330848847494SEnrico Perla - Sun Microsystems 	 * Else setup command args for create_ramdisk.ksh for the UFS archives
33097c478bd9Sstevel@tonic-gate 	 */
331048847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
331148847494SEnrico Perla - Sun Microsystems 		bam_print("mkisofs not found, creating UFS archive\n");
331248847494SEnrico Perla - Sun Microsystems 
3313986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
33147c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
33157c478bd9Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
33167c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33177c478bd9Sstevel@tonic-gate 	}
33187c478bd9Sstevel@tonic-gate 
3319cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
3320cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
3321cedc7e57SEnrico Perla - Sun Microsystems 
33227c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
3323d876c67dSjg 	if (bam_alt_platform)
3324d876c67dSjg 		len += strlen(bam_platform) + strlen("-p ");
33257c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
33267c478bd9Sstevel@tonic-gate 
3327d876c67dSjg 	if (bam_alt_platform) {
3328d876c67dSjg 		assert(strlen(root) > 1);
3329d876c67dSjg 		(void) snprintf(cmdline, len, "%s -p %s -R %s",
3330d876c67dSjg 		    path, bam_platform, root);
3331d876c67dSjg 		/* chop off / at the end */
3332d876c67dSjg 		cmdline[strlen(cmdline) - 1] = '\0';
3333d876c67dSjg 	} else if (strlen(root) > 1) {
33347c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
33357c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
33367c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
33377c478bd9Sstevel@tonic-gate 	} else
33387c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
33397c478bd9Sstevel@tonic-gate 
3340986fd29aSsetje 	if (exec_cmd(cmdline, NULL) != 0) {
33417c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
33427c478bd9Sstevel@tonic-gate 		free(cmdline);
33437c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
33447c478bd9Sstevel@tonic-gate 	}
33457c478bd9Sstevel@tonic-gate 	free(cmdline);
33467c478bd9Sstevel@tonic-gate 	/*
3347986fd29aSsetje 	 * The existence of the expected archives used to be
3348986fd29aSsetje 	 * verified here. This check is done in create_ramdisk as
3349986fd29aSsetje 	 * it needs to be in sync with the altroot operated upon.
33507c478bd9Sstevel@tonic-gate 	 */
33517c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
33527c478bd9Sstevel@tonic-gate }
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate /*
33557c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
33567c478bd9Sstevel@tonic-gate  * 1 - is miniroot
33577c478bd9Sstevel@tonic-gate  * 0 - is not
33587c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
33597c478bd9Sstevel@tonic-gate  */
33607c478bd9Sstevel@tonic-gate static int
33617c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
33627c478bd9Sstevel@tonic-gate {
33637c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
33647c478bd9Sstevel@tonic-gate 	FILE *fp;
33657c478bd9Sstevel@tonic-gate 	int found;
3366b610f78eSvikram 	char mntpt[PATH_MAX];
3367b610f78eSvikram 	char *cp;
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate 	/*
33707c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
33717c478bd9Sstevel@tonic-gate 	 * of dubious value:
3372b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
33737c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
33747c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
33757c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
33767c478bd9Sstevel@tonic-gate 	 * worth it.
33777c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
33787c478bd9Sstevel@tonic-gate 	 */
33797c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
33807c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
33817c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
33827c478bd9Sstevel@tonic-gate 		return (0);
33837c478bd9Sstevel@tonic-gate 	}
33847c478bd9Sstevel@tonic-gate 
33857c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
33867c478bd9Sstevel@tonic-gate 
3387b610f78eSvikram 	/*
3388b610f78eSvikram 	 * Remove any trailing / from the mount point
3389b610f78eSvikram 	 */
3390b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
3391b610f78eSvikram 	if (strcmp(root, "/") != 0) {
3392b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
3393b610f78eSvikram 		if (*cp == '/')
3394b610f78eSvikram 			*cp = '\0';
3395b610f78eSvikram 	}
33967c478bd9Sstevel@tonic-gate 	found = 0;
33977c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
3398b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
33997c478bd9Sstevel@tonic-gate 			found = 1;
34007c478bd9Sstevel@tonic-gate 			break;
34017c478bd9Sstevel@tonic-gate 		}
34027c478bd9Sstevel@tonic-gate 	}
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate 	if (!found) {
34057c478bd9Sstevel@tonic-gate 		if (bam_verbose)
3406b610f78eSvikram 			bam_error(NOT_IN_MNTTAB, mntpt);
34077c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
34087c478bd9Sstevel@tonic-gate 		return (0);
34097c478bd9Sstevel@tonic-gate 	}
34107c478bd9Sstevel@tonic-gate 
34117c478bd9Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
34127c478bd9Sstevel@tonic-gate 		if (bam_verbose)
34137c478bd9Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
34147c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
34157c478bd9Sstevel@tonic-gate 		return (1);
34167c478bd9Sstevel@tonic-gate 	}
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 	return (0);
34217c478bd9Sstevel@tonic-gate }
34227c478bd9Sstevel@tonic-gate 
34237c478bd9Sstevel@tonic-gate static int
3424986fd29aSsetje is_boot_archive(char *root)
34257c478bd9Sstevel@tonic-gate {
34267c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
34277c478bd9Sstevel@tonic-gate 	struct stat	sb;
3428eb2bd662Svikram 	int		error;
3429eb2bd662Svikram 	const char	*fcn = "is_boot_archive()";
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate 	/*
3432986fd29aSsetje 	 * We can't create an archive without the create_ramdisk script
34337c478bd9Sstevel@tonic-gate 	 */
3434986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
3435eb2bd662Svikram 	error = stat(path, &sb);
3436eb2bd662Svikram 	INJECT_ERROR1("NOT_ARCHIVE_BASED", error = -1);
3437eb2bd662Svikram 	if (error == -1) {
34387c478bd9Sstevel@tonic-gate 		if (bam_verbose)
34397c478bd9Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
3440eb2bd662Svikram 		BAM_DPRINTF((D_NOT_ARCHIVE_BOOT, fcn, root));
34417c478bd9Sstevel@tonic-gate 		return (0);
34427c478bd9Sstevel@tonic-gate 	}
34437c478bd9Sstevel@tonic-gate 
3444eb2bd662Svikram 	BAM_DPRINTF((D_IS_ARCHIVE_BOOT, fcn, root));
3445986fd29aSsetje 	return (1);
3446986fd29aSsetje }
3447986fd29aSsetje 
34487c478bd9Sstevel@tonic-gate /*
3449986fd29aSsetje  * Need to call this for anything that operates on the GRUB menu
3450963390b4Svikram  * In the x86 live upgrade case the directory /boot/grub may be present
3451963390b4Svikram  * even on pre-newboot BEs. The authoritative way to check for a GRUB target
3452963390b4Svikram  * is to check for the presence of the stage2 binary which is present
3453963390b4Svikram  * only on GRUB targets (even on x86 boot partitions). Checking for the
3454963390b4Svikram  * presence of the multiboot binary is not correct as it is not present
3455963390b4Svikram  * on x86 boot partitions.
3456986fd29aSsetje  */
3457986fd29aSsetje int
3458986fd29aSsetje is_grub(const char *root)
3459986fd29aSsetje {
3460986fd29aSsetje 	char path[PATH_MAX];
3461986fd29aSsetje 	struct stat sb;
3462eb2bd662Svikram 	const char *fcn = "is_grub()";
3463986fd29aSsetje 
3464963390b4Svikram 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_STAGE2);
34657c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
3466eb2bd662Svikram 		BAM_DPRINTF((D_NO_GRUB_DIR, fcn, path));
34677c478bd9Sstevel@tonic-gate 		return (0);
34687c478bd9Sstevel@tonic-gate 	}
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 	return (1);
34717c478bd9Sstevel@tonic-gate }
34727c478bd9Sstevel@tonic-gate 
34737c478bd9Sstevel@tonic-gate static int
3474eb2bd662Svikram is_zfs(char *root)
3475eb2bd662Svikram {
3476eb2bd662Svikram 	struct statvfs		vfs;
3477eb2bd662Svikram 	int			ret;
3478eb2bd662Svikram 	const char		*fcn = "is_zfs()";
3479eb2bd662Svikram 
3480eb2bd662Svikram 	ret = statvfs(root, &vfs);
3481eb2bd662Svikram 	INJECT_ERROR1("STATVFS_ZFS", ret = 1);
3482eb2bd662Svikram 	if (ret != 0) {
3483eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3484eb2bd662Svikram 		return (0);
3485eb2bd662Svikram 	}
3486eb2bd662Svikram 
3487eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "zfs", strlen("zfs")) == 0) {
3488eb2bd662Svikram 		BAM_DPRINTF((D_IS_ZFS, fcn, root));
3489eb2bd662Svikram 		return (1);
3490eb2bd662Svikram 	} else {
3491eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_ZFS, fcn, root));
3492eb2bd662Svikram 		return (0);
3493eb2bd662Svikram 	}
3494eb2bd662Svikram }
3495eb2bd662Svikram 
3496eb2bd662Svikram static int
3497eb2bd662Svikram is_ufs(char *root)
3498eb2bd662Svikram {
3499eb2bd662Svikram 	struct statvfs		vfs;
3500eb2bd662Svikram 	int			ret;
3501eb2bd662Svikram 	const char		*fcn = "is_ufs()";
3502eb2bd662Svikram 
3503eb2bd662Svikram 	ret = statvfs(root, &vfs);
3504eb2bd662Svikram 	INJECT_ERROR1("STATVFS_UFS", ret = 1);
3505eb2bd662Svikram 	if (ret != 0) {
3506eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3507eb2bd662Svikram 		return (0);
3508eb2bd662Svikram 	}
3509eb2bd662Svikram 
3510eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "ufs", strlen("ufs")) == 0) {
3511eb2bd662Svikram 		BAM_DPRINTF((D_IS_UFS, fcn, root));
3512eb2bd662Svikram 		return (1);
3513eb2bd662Svikram 	} else {
3514eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_UFS, fcn, root));
3515eb2bd662Svikram 		return (0);
3516eb2bd662Svikram 	}
3517eb2bd662Svikram }
3518eb2bd662Svikram 
3519eb2bd662Svikram static int
3520eb2bd662Svikram is_pcfs(char *root)
3521eb2bd662Svikram {
3522eb2bd662Svikram 	struct statvfs		vfs;
3523eb2bd662Svikram 	int			ret;
3524eb2bd662Svikram 	const char		*fcn = "is_pcfs()";
3525eb2bd662Svikram 
3526eb2bd662Svikram 	ret = statvfs(root, &vfs);
3527eb2bd662Svikram 	INJECT_ERROR1("STATVFS_PCFS", ret = 1);
3528eb2bd662Svikram 	if (ret != 0) {
3529eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3530eb2bd662Svikram 		return (0);
3531eb2bd662Svikram 	}
3532eb2bd662Svikram 
3533eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "pcfs", strlen("pcfs")) == 0) {
3534eb2bd662Svikram 		BAM_DPRINTF((D_IS_PCFS, fcn, root));
3535eb2bd662Svikram 		return (1);
3536eb2bd662Svikram 	} else {
3537eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_PCFS, fcn, root));
3538eb2bd662Svikram 		return (0);
3539eb2bd662Svikram 	}
3540eb2bd662Svikram }
3541eb2bd662Svikram 
3542eb2bd662Svikram static int
35437c478bd9Sstevel@tonic-gate is_readonly(char *root)
35447c478bd9Sstevel@tonic-gate {
3545eb2bd662Svikram 	int		fd;
3546eb2bd662Svikram 	int		error;
3547eb2bd662Svikram 	char		testfile[PATH_MAX];
3548eb2bd662Svikram 	const char	*fcn = "is_readonly()";
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 	/*
3551eb2bd662Svikram 	 * Using statvfs() to check for a read-only filesystem is not
3552eb2bd662Svikram 	 * reliable. The only way to reliably test is to attempt to
3553eb2bd662Svikram 	 * create a file
35547c478bd9Sstevel@tonic-gate 	 */
3555eb2bd662Svikram 	(void) snprintf(testfile, sizeof (testfile), "%s/%s.%d",
3556eb2bd662Svikram 	    root, BOOTADM_RDONLY_TEST, getpid());
35577c478bd9Sstevel@tonic-gate 
3558eb2bd662Svikram 	(void) unlink(testfile);
3559eb2bd662Svikram 
3560eb2bd662Svikram 	errno = 0;
3561eb2bd662Svikram 	fd = open(testfile, O_RDWR|O_CREAT|O_EXCL, 0644);
3562eb2bd662Svikram 	error = errno;
3563eb2bd662Svikram 	INJECT_ERROR2("RDONLY_TEST_ERROR", fd = -1, error = EACCES);
3564eb2bd662Svikram 	if (fd == -1 && error == EROFS) {
3565eb2bd662Svikram 		BAM_DPRINTF((D_RDONLY_FS, fcn, root));
35667c478bd9Sstevel@tonic-gate 		return (1);
3567eb2bd662Svikram 	} else if (fd == -1) {
3568eb2bd662Svikram 		bam_error(RDONLY_TEST_ERROR, root, strerror(error));
35697c478bd9Sstevel@tonic-gate 	}
35707c478bd9Sstevel@tonic-gate 
3571eb2bd662Svikram 	(void) close(fd);
3572eb2bd662Svikram 	(void) unlink(testfile);
35737c478bd9Sstevel@tonic-gate 
3574eb2bd662Svikram 	BAM_DPRINTF((D_RDWR_FS, fcn, root));
3575e7cbe64fSgw25295 	return (0);
3576e7cbe64fSgw25295 }
3577e7cbe64fSgw25295 
35787c478bd9Sstevel@tonic-gate static error_t
35797c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
35807c478bd9Sstevel@tonic-gate {
35817c478bd9Sstevel@tonic-gate 	error_t ret;
35827c478bd9Sstevel@tonic-gate 
35837c478bd9Sstevel@tonic-gate 	assert(root);
35847c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
35857c478bd9Sstevel@tonic-gate 
358648847494SEnrico Perla - Sun Microsystems 	init_walk_args();
358748847494SEnrico Perla - Sun Microsystems 	(void) umask(022);
358848847494SEnrico Perla - Sun Microsystems 
35897c478bd9Sstevel@tonic-gate 	/*
3590986fd29aSsetje 	 * root must belong to a boot archive based OS,
35917c478bd9Sstevel@tonic-gate 	 */
3592986fd29aSsetje 	if (!is_boot_archive(root)) {
3593b610f78eSvikram 		/*
3594b610f78eSvikram 		 * Emit message only if not in context of update_all.
3595b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
3596b610f78eSvikram 		 */
3597b610f78eSvikram 		if (!bam_update_all || bam_verbose)
3598eb2bd662Svikram 			bam_print(NOT_ARCHIVE_BOOT, root);
359948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
36007c478bd9Sstevel@tonic-gate 	}
36017c478bd9Sstevel@tonic-gate 
36027c478bd9Sstevel@tonic-gate 	/*
36038c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
36048c1b6884Sszhou 	 * on first reboot following an upgrade because service
36058c1b6884Sszhou 	 * dependency is messed up), skip the check.
36068c1b6884Sszhou 	 */
360748847494SEnrico Perla - Sun Microsystems 	if (bam_smf_check && !bam_root_readonly && !is_zfs(root))
36088c1b6884Sszhou 		return (BAM_SUCCESS);
36098c1b6884Sszhou 
36108c1b6884Sszhou 	/*
36115eea6091SEnrico Perla - Sun Microsystems 	 * Don't generate archive on ramdisk.
36125eea6091SEnrico Perla - Sun Microsystems 	 */
36135eea6091SEnrico Perla - Sun Microsystems 	if (is_ramdisk(root))
36145eea6091SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
36155eea6091SEnrico Perla - Sun Microsystems 
36165eea6091SEnrico Perla - Sun Microsystems 	/*
36178c1b6884Sszhou 	 * root must be writable. This check applies to alternate
36188c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
361948847494SEnrico Perla - Sun Microsystems 	 * The behaviour translates into being the one of a 'check'.
36207c478bd9Sstevel@tonic-gate 	 */
362161cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
362248847494SEnrico Perla - Sun Microsystems 		set_flag(RDONLY_FSCHK);
362348847494SEnrico Perla - Sun Microsystems 		bam_check = 1;
36247c478bd9Sstevel@tonic-gate 	}
36257c478bd9Sstevel@tonic-gate 
36267c478bd9Sstevel@tonic-gate 	/*
3627cedc7e57SEnrico Perla - Sun Microsystems 	 * Now check if an update is really needed.
36287c478bd9Sstevel@tonic-gate 	 */
36297c478bd9Sstevel@tonic-gate 	ret = update_required(root);
36307c478bd9Sstevel@tonic-gate 
36317c478bd9Sstevel@tonic-gate 	/*
3632cedc7e57SEnrico Perla - Sun Microsystems 	 * The check command (-n) is *not* a dry run.
36337c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
3634cedc7e57SEnrico Perla - Sun Microsystems 	 * A readonly filesystem has to be considered an error only if an update
3635cedc7e57SEnrico Perla - Sun Microsystems 	 * is required.
36367c478bd9Sstevel@tonic-gate 	 */
3637cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
363848847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(RDONLY_FSCHK)) {
36395eea6091SEnrico Perla - Sun Microsystems 			bam_check = bam_saved_check;
364048847494SEnrico Perla - Sun Microsystems 			if (ret > 0)
364148847494SEnrico Perla - Sun Microsystems 				bam_error(RDONLY_FS, root);
364248847494SEnrico Perla - Sun Microsystems 			if (bam_update_all)
364348847494SEnrico Perla - Sun Microsystems 				return ((ret != 0) ? BAM_ERROR : BAM_SUCCESS);
364448847494SEnrico Perla - Sun Microsystems 		}
364548847494SEnrico Perla - Sun Microsystems 
36467c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
36477c478bd9Sstevel@tonic-gate 	}
36487c478bd9Sstevel@tonic-gate 
36497c478bd9Sstevel@tonic-gate 	if (ret == 1) {
36507c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
36517c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
36527c478bd9Sstevel@tonic-gate 	}
3653986fd29aSsetje 
36544a9df875SEnrico Perla - Sun Microsystems 	/*
36554a9df875SEnrico Perla - Sun Microsystems 	 * if the archive is updated, save the new stat data and update the
36564a9df875SEnrico Perla - Sun Microsystems 	 * timestamp file
36574a9df875SEnrico Perla - Sun Microsystems 	 */
3658986fd29aSsetje 	if (ret == 0 && walk_arg.new_nvlp != NULL) {
3659986fd29aSsetje 		savenew(root);
36604a9df875SEnrico Perla - Sun Microsystems 		update_timestamp(root);
3661986fd29aSsetje 	}
3662986fd29aSsetje 
3663986fd29aSsetje 	clear_walk_args();
3664986fd29aSsetje 
36657c478bd9Sstevel@tonic-gate 	return (ret);
36667c478bd9Sstevel@tonic-gate }
36677c478bd9Sstevel@tonic-gate 
366840b108d7SMark J Musante static char *
366940b108d7SMark J Musante find_root_pool()
367040b108d7SMark J Musante {
367140b108d7SMark J Musante 	char *special = get_special("/");
367240b108d7SMark J Musante 	char *p;
367340b108d7SMark J Musante 
367440b108d7SMark J Musante 	if (special == NULL)
367540b108d7SMark J Musante 		return (NULL);
367640b108d7SMark J Musante 
367740b108d7SMark J Musante 	if (*special == '/') {
367840b108d7SMark J Musante 		free(special);
367940b108d7SMark J Musante 		return (NULL);
368040b108d7SMark J Musante 	}
368140b108d7SMark J Musante 
368240b108d7SMark J Musante 	if ((p = strchr(special, '/')) != NULL)
368340b108d7SMark J Musante 		*p = '\0';
368440b108d7SMark J Musante 
368540b108d7SMark J Musante 	return (special);
368640b108d7SMark J Musante }
368740b108d7SMark J Musante 
3688963390b4Svikram static error_t
3689963390b4Svikram synchronize_BE_menu(void)
3690fbac2b2bSvikram {
3691fbac2b2bSvikram 	struct stat	sb;
3692963390b4Svikram 	char		cmdline[PATH_MAX];
3693963390b4Svikram 	char		cksum_line[PATH_MAX];
3694963390b4Svikram 	filelist_t	flist = {0};
3695963390b4Svikram 	char		*old_cksum_str;
3696963390b4Svikram 	char		*old_size_str;
3697963390b4Svikram 	char		*old_file;
3698963390b4Svikram 	char		*curr_cksum_str;
3699963390b4Svikram 	char		*curr_size_str;
3700963390b4Svikram 	char		*curr_file;
3701*1130146cSGangadhar Mylapuram 	char		*pool = NULL;
3702*1130146cSGangadhar Mylapuram 	char		*mntpt = NULL;
3703*1130146cSGangadhar Mylapuram 	zfs_mnted_t	mnted;
3704963390b4Svikram 	FILE		*cfp;
3705963390b4Svikram 	int		found;
3706963390b4Svikram 	int		ret;
3707963390b4Svikram 	const char	*fcn = "synchronize_BE_menu()";
3708fbac2b2bSvikram 
3709963390b4Svikram 	BAM_DPRINTF((D_FUNC_ENTRY0, fcn));
3710fbac2b2bSvikram 
3711963390b4Svikram 	/* Check if findroot enabled LU BE */
3712963390b4Svikram 	if (stat(FINDROOT_INSTALLGRUB, &sb) != 0) {
3713963390b4Svikram 		BAM_DPRINTF((D_NOT_LU_BE, fcn));
3714963390b4Svikram 		return (BAM_SUCCESS);
3715fbac2b2bSvikram 	}
3716fbac2b2bSvikram 
3717963390b4Svikram 	if (stat(LU_MENU_CKSUM, &sb) != 0) {
3718963390b4Svikram 		BAM_DPRINTF((D_NO_CKSUM_FILE, fcn, LU_MENU_CKSUM));
3719963390b4Svikram 		goto menu_sync;
3720fbac2b2bSvikram 	}
3721963390b4Svikram 
3722963390b4Svikram 	cfp = fopen(LU_MENU_CKSUM, "r");
3723963390b4Svikram 	INJECT_ERROR1("CKSUM_FILE_MISSING", cfp = NULL);
3724963390b4Svikram 	if (cfp == NULL) {
3725963390b4Svikram 		bam_error(CANNOT_READ_LU_CKSUM, LU_MENU_CKSUM);
3726963390b4Svikram 		goto menu_sync;
3727963390b4Svikram 	}
3728963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_OPENED, fcn, LU_MENU_CKSUM));
3729963390b4Svikram 
3730963390b4Svikram 	found = 0;
3731963390b4Svikram 	while (s_fgets(cksum_line, sizeof (cksum_line), cfp) != NULL) {
3732963390b4Svikram 		INJECT_ERROR1("MULTIPLE_CKSUM", found = 1);
3733963390b4Svikram 		if (found) {
3734963390b4Svikram 			bam_error(MULTIPLE_LU_CKSUM, LU_MENU_CKSUM);
3735963390b4Svikram 			(void) fclose(cfp);
3736963390b4Svikram 			goto menu_sync;
3737963390b4Svikram 		}
3738963390b4Svikram 		found = 1;
3739963390b4Svikram 	}
3740963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_READ, fcn, LU_MENU_CKSUM));
3741963390b4Svikram 
3742963390b4Svikram 
3743963390b4Svikram 	old_cksum_str = strtok(cksum_line, " \t");
3744963390b4Svikram 	old_size_str = strtok(NULL, " \t");
3745963390b4Svikram 	old_file = strtok(NULL, " \t");
3746963390b4Svikram 
3747963390b4Svikram 	INJECT_ERROR1("OLD_CKSUM_NULL", old_cksum_str = NULL);
3748963390b4Svikram 	INJECT_ERROR1("OLD_SIZE_NULL", old_size_str = NULL);
3749963390b4Svikram 	INJECT_ERROR1("OLD_FILE_NULL", old_file = NULL);
3750963390b4Svikram 	if (old_cksum_str == NULL || old_size_str == NULL || old_file == NULL) {
3751963390b4Svikram 		bam_error(CANNOT_PARSE_LU_CKSUM, LU_MENU_CKSUM);
3752963390b4Svikram 		goto menu_sync;
3753963390b4Svikram 	}
3754963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_PARSED, fcn, LU_MENU_CKSUM));
3755963390b4Svikram 
3756963390b4Svikram 	/* Get checksum of current menu */
375740b108d7SMark J Musante 	pool = find_root_pool();
3758*1130146cSGangadhar Mylapuram 	if (pool) {
3759*1130146cSGangadhar Mylapuram 		mntpt = mount_top_dataset(pool, &mnted);
3760*1130146cSGangadhar Mylapuram 		if (mntpt == NULL) {
3761*1130146cSGangadhar Mylapuram 			bam_error(FAIL_MNT_TOP_DATASET, pool);
376240b108d7SMark J Musante 			free(pool);
3763*1130146cSGangadhar Mylapuram 			return (BAM_ERROR);
3764*1130146cSGangadhar Mylapuram 		}
3765*1130146cSGangadhar Mylapuram 		(void) snprintf(cmdline, sizeof (cmdline), "%s %s%s",
3766*1130146cSGangadhar Mylapuram 		    CKSUM, mntpt, GRUB_MENU);
3767*1130146cSGangadhar Mylapuram 	} else {
3768*1130146cSGangadhar Mylapuram 		(void) snprintf(cmdline, sizeof (cmdline), "%s %s",
3769*1130146cSGangadhar Mylapuram 		    CKSUM, GRUB_MENU);
3770*1130146cSGangadhar Mylapuram 	}
3771963390b4Svikram 	ret = exec_cmd(cmdline, &flist);
3772*1130146cSGangadhar Mylapuram 	if (pool) {
3773*1130146cSGangadhar Mylapuram 		(void) umount_top_dataset(pool, mnted, mntpt);
3774*1130146cSGangadhar Mylapuram 		free(pool);
3775*1130146cSGangadhar Mylapuram 	}
3776963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM", ret = 1);
3777963390b4Svikram 	if (ret != 0) {
3778963390b4Svikram 		bam_error(MENU_CKSUM_FAIL);
3779963390b4Svikram 		return (BAM_ERROR);
3780963390b4Svikram 	}
3781963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_SUCCESS, fcn));
3782963390b4Svikram 
3783963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM_OUTPUT", flist.head = NULL);
3784963390b4Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
3785963390b4Svikram 		bam_error(BAD_CKSUM);
3786963390b4Svikram 		filelist_free(&flist);
3787963390b4Svikram 		return (BAM_ERROR);
3788963390b4Svikram 	}
3789963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_OUTPUT_VALID, fcn));
3790963390b4Svikram 
3791963390b4Svikram 	curr_cksum_str = strtok(flist.head->line, " \t");
3792963390b4Svikram 	curr_size_str = strtok(NULL, " \t");
3793963390b4Svikram 	curr_file = strtok(NULL, " \t");
3794963390b4Svikram 
3795963390b4Svikram 	INJECT_ERROR1("CURR_CKSUM_NULL", curr_cksum_str = NULL);
3796963390b4Svikram 	INJECT_ERROR1("CURR_SIZE_NULL", curr_size_str = NULL);
3797963390b4Svikram 	INJECT_ERROR1("CURR_FILE_NULL", curr_file = NULL);
3798963390b4Svikram 	if (curr_cksum_str == NULL || curr_size_str == NULL ||
3799963390b4Svikram 	    curr_file == NULL) {
3800963390b4Svikram 		bam_error(BAD_CKSUM_PARSE);
3801963390b4Svikram 		filelist_free(&flist);
3802963390b4Svikram 		return (BAM_ERROR);
3803963390b4Svikram 	}
3804963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_PARSED, fcn));
3805963390b4Svikram 
3806963390b4Svikram 	if (strcmp(old_cksum_str, curr_cksum_str) == 0 &&
3807963390b4Svikram 	    strcmp(old_size_str, curr_size_str) == 0 &&
3808963390b4Svikram 	    strcmp(old_file, curr_file) == 0) {
3809963390b4Svikram 		filelist_free(&flist);
3810963390b4Svikram 		BAM_DPRINTF((D_CKSUM_NO_CHANGE, fcn));
3811963390b4Svikram 		return (BAM_SUCCESS);
3812963390b4Svikram 	}
3813963390b4Svikram 
3814963390b4Svikram 	filelist_free(&flist);
3815963390b4Svikram 
3816963390b4Svikram 	/* cksum doesn't match - the menu has changed */
3817963390b4Svikram 	BAM_DPRINTF((D_CKSUM_HAS_CHANGED, fcn));
3818963390b4Svikram 
3819963390b4Svikram menu_sync:
3820963390b4Svikram 	bam_print(PROP_GRUB_MENU);
3821963390b4Svikram 
3822963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
3823a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s yes > /dev/null'",
3824963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_MENU);
3825963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3826963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU", ret = 1);
3827963390b4Svikram 	if (ret != 0) {
3828963390b4Svikram 		bam_error(MENU_PROP_FAIL);
3829963390b4Svikram 		return (BAM_ERROR);
3830963390b4Svikram 	}
3831963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_MENU, fcn));
3832963390b4Svikram 
3833a6968364Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "/bin/cp %s %s > /dev/null",
3834963390b4Svikram 	    GRUB_MENU, GRUB_BACKUP_MENU);
3835963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3836963390b4Svikram 	INJECT_ERROR1("CREATE_BACKUP", ret = 1);
3837963390b4Svikram 	if (ret != 0) {
3838963390b4Svikram 		bam_error(MENU_BACKUP_FAIL, GRUB_BACKUP_MENU);
3839963390b4Svikram 		return (BAM_ERROR);
3840963390b4Svikram 	}
3841963390b4Svikram 	BAM_DPRINTF((D_CREATED_BACKUP, fcn, GRUB_BACKUP_MENU));
3842963390b4Svikram 
3843963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
3844a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
3845963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_BACKUP_MENU);
3846963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3847963390b4Svikram 	INJECT_ERROR1("PROPAGATE_BACKUP", ret = 1);
3848963390b4Svikram 	if (ret != 0) {
3849963390b4Svikram 		bam_error(BACKUP_PROP_FAIL, GRUB_BACKUP_MENU);
3850963390b4Svikram 		return (BAM_ERROR);
3851963390b4Svikram 	}
3852963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_BACKUP, fcn, GRUB_BACKUP_MENU));
3853963390b4Svikram 
3854963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s > %s",
3855963390b4Svikram 	    CKSUM, GRUB_MENU, LU_MENU_CKSUM);
3856963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3857963390b4Svikram 	INJECT_ERROR1("CREATE_CKSUM_FILE", ret = 1);
3858963390b4Svikram 	if (ret != 0) {
3859963390b4Svikram 		bam_error(MENU_CKSUM_WRITE_FAIL, LU_MENU_CKSUM);
3860963390b4Svikram 		return (BAM_ERROR);
3861963390b4Svikram 	}
3862963390b4Svikram 	BAM_DPRINTF((D_CREATED_CKSUM_FILE, fcn, LU_MENU_CKSUM));
3863963390b4Svikram 
3864963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
3865a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
3866963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, LU_MENU_CKSUM);
3867963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3868963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU_CKSUM_FILE", ret = 1);
3869963390b4Svikram 	if (ret != 0) {
3870963390b4Svikram 		bam_error(MENU_CKSUM_PROP_FAIL, LU_MENU_CKSUM);
3871963390b4Svikram 		return (BAM_ERROR);
3872963390b4Svikram 	}
3873963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_CKSUM_FILE, fcn, LU_MENU_CKSUM));
3874963390b4Svikram 
3875963390b4Svikram 	return (BAM_SUCCESS);
3876fbac2b2bSvikram }
3877fbac2b2bSvikram 
38787c478bd9Sstevel@tonic-gate static error_t
38797c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
38807c478bd9Sstevel@tonic-gate {
38817c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
38827c478bd9Sstevel@tonic-gate 	struct stat sb;
38837c478bd9Sstevel@tonic-gate 	FILE *fp;
38847c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
3885986fd29aSsetje 	char creatram[PATH_MAX];
38867c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
38877c478bd9Sstevel@tonic-gate 
388840541d5dSvikram 	assert(root);
38897c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
38907c478bd9Sstevel@tonic-gate 
389140541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
389240541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
389340541d5dSvikram 		bam_error(ALT_ROOT_INVALID, multibt);
389440541d5dSvikram 		return (BAM_ERROR);
389540541d5dSvikram 	}
389640541d5dSvikram 
38977c478bd9Sstevel@tonic-gate 	/*
389898892a30Snadkarni 	 * Check to see if we are in the midst of safemode patching
389998892a30Snadkarni 	 * If so skip building the archive for /. Instead build it
390098892a30Snadkarni 	 * against the latest bits obtained by creating a fresh lofs
390198892a30Snadkarni 	 * mount of root.
390298892a30Snadkarni 	 */
390398892a30Snadkarni 	if (stat(LOFS_PATCH_FILE, &sb) == 0)  {
390448847494SEnrico Perla - Sun Microsystems 		if (mkdir(LOFS_PATCH_MNT, DIR_PERMS) == -1 &&
390598892a30Snadkarni 		    errno != EEXIST) {
390698892a30Snadkarni 			bam_error(MKDIR_FAILED, "%s", LOFS_PATCH_MNT,
390798892a30Snadkarni 			    strerror(errno));
390898892a30Snadkarni 			ret = BAM_ERROR;
390998892a30Snadkarni 			goto out;
391098892a30Snadkarni 		}
391198892a30Snadkarni 		(void) snprintf(multibt, sizeof (multibt),
391298892a30Snadkarni 		    "/sbin/mount -F lofs -o nosub /  %s", LOFS_PATCH_MNT);
3913986fd29aSsetje 		if (exec_cmd(multibt, NULL) != 0) {
391498892a30Snadkarni 			bam_error(MOUNT_FAILED, LOFS_PATCH_MNT, "lofs");
391598892a30Snadkarni 			ret = BAM_ERROR;
391698892a30Snadkarni 		}
391798892a30Snadkarni 		if (ret != BAM_ERROR) {
391898892a30Snadkarni 			(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
391998892a30Snadkarni 			    LOFS_PATCH_MNT);
392098892a30Snadkarni 			bam_rootlen = strlen(rootbuf);
392198892a30Snadkarni 			if (update_archive(rootbuf, opt) != BAM_SUCCESS)
392298892a30Snadkarni 				ret = BAM_ERROR;
392395b1e0e9Snadkarni 			/*
392495b1e0e9Snadkarni 			 * unmount the lofs mount since there could be
392595b1e0e9Snadkarni 			 * multiple invocations of bootadm -a update_all
392695b1e0e9Snadkarni 			 */
392795b1e0e9Snadkarni 			(void) snprintf(multibt, sizeof (multibt),
392895b1e0e9Snadkarni 			    "/sbin/umount %s", LOFS_PATCH_MNT);
3929986fd29aSsetje 			if (exec_cmd(multibt, NULL) != 0) {
393095b1e0e9Snadkarni 				bam_error(UMOUNT_FAILED, LOFS_PATCH_MNT);
393195b1e0e9Snadkarni 				ret = BAM_ERROR;
393295b1e0e9Snadkarni 			}
393398892a30Snadkarni 		}
393498892a30Snadkarni 	} else {
393598892a30Snadkarni 		/*
39367c478bd9Sstevel@tonic-gate 		 * First update archive for current root
39377c478bd9Sstevel@tonic-gate 		 */
39387c478bd9Sstevel@tonic-gate 		if (update_archive(root, opt) != BAM_SUCCESS)
39397c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
394098892a30Snadkarni 	}
394198892a30Snadkarni 
394298892a30Snadkarni 	if (ret == BAM_ERROR)
394398892a30Snadkarni 		goto out;
39447c478bd9Sstevel@tonic-gate 
39457c478bd9Sstevel@tonic-gate 	/*
39467c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
39477c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
39487c478bd9Sstevel@tonic-gate 	 */
39497c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
39507c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
39517c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
3952b610f78eSvikram 		ret = BAM_ERROR;
3953b610f78eSvikram 		goto out;
39547c478bd9Sstevel@tonic-gate 	}
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
39577c478bd9Sstevel@tonic-gate 
39587c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
39597c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
39607c478bd9Sstevel@tonic-gate 			continue;
3961f904d32dSJerry Gilliam 		if ((strcmp(mnt.mnt_fstype, MNTTYPE_ZFS) != 0) &&
3962f904d32dSJerry Gilliam 		    (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0))
39637c478bd9Sstevel@tonic-gate 			continue;
39647c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
39657c478bd9Sstevel@tonic-gate 			continue;
39667c478bd9Sstevel@tonic-gate 
3967986fd29aSsetje 		(void) snprintf(creatram, sizeof (creatram), "%s/%s",
3968986fd29aSsetje 		    mnt.mnt_mountp, CREATE_RAMDISK);
39697c478bd9Sstevel@tonic-gate 
3970986fd29aSsetje 		if (stat(creatram, &sb) == -1)
39717c478bd9Sstevel@tonic-gate 			continue;
39727c478bd9Sstevel@tonic-gate 
39737c478bd9Sstevel@tonic-gate 		/*
39747c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
39757c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
39767c478bd9Sstevel@tonic-gate 		 */
39777c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
39787c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
39797c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
3980ae115bc7Smrj 
3981ae115bc7Smrj 		/*
3982ae115bc7Smrj 		 * It's possible that other mounts may be an alternate boot
3983ae115bc7Smrj 		 * architecture, so check it again.
3984ae115bc7Smrj 		 */
3985eb2bd662Svikram 		if ((get_boot_cap(rootbuf) != BAM_SUCCESS) ||
3986ae115bc7Smrj 		    (update_archive(rootbuf, opt) != BAM_SUCCESS))
39877c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
39887c478bd9Sstevel@tonic-gate 	}
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
39917c478bd9Sstevel@tonic-gate 
3992b610f78eSvikram out:
3993fbac2b2bSvikram 	/*
3994963390b4Svikram 	 * We no longer use biosdev for Live Upgrade. Hence
3995963390b4Svikram 	 * there is no need to defer (to shutdown time) any fdisk
3996963390b4Svikram 	 * updates
3997fbac2b2bSvikram 	 */
3998963390b4Svikram 	if (stat(GRUB_fdisk, &sb) == 0 || stat(GRUB_fdisk_target, &sb) == 0) {
3999963390b4Svikram 		bam_error(FDISK_FILES_FOUND, GRUB_fdisk, GRUB_fdisk_target);
4000fbac2b2bSvikram 	}
4001fbac2b2bSvikram 
4002963390b4Svikram 	/*
4003963390b4Svikram 	 * If user has updated menu in current BE, propagate the
4004963390b4Svikram 	 * updates to all BEs.
4005963390b4Svikram 	 */
400619397407SSherry Moore 	if (sync_menu && synchronize_BE_menu() != BAM_SUCCESS)
4007963390b4Svikram 		ret = BAM_ERROR;
4008963390b4Svikram 
40097c478bd9Sstevel@tonic-gate 	return (ret);
40107c478bd9Sstevel@tonic-gate }
40117c478bd9Sstevel@tonic-gate 
40127c478bd9Sstevel@tonic-gate static void
40137c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
40147c478bd9Sstevel@tonic-gate {
40157c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
40167c478bd9Sstevel@tonic-gate 		mp->start = lp;
40177c478bd9Sstevel@tonic-gate 	} else {
40187c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
40198c1b6884Sszhou 		lp->prev = mp->end;
40207c478bd9Sstevel@tonic-gate 	}
40217c478bd9Sstevel@tonic-gate 	mp->end = lp;
40227c478bd9Sstevel@tonic-gate }
40237c478bd9Sstevel@tonic-gate 
4024eb2bd662Svikram void
40258c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
40268c1b6884Sszhou {
40278c1b6884Sszhou 	/* unlink from list */
40288c1b6884Sszhou 	if (lp->prev)
40298c1b6884Sszhou 		lp->prev->next = lp->next;
40308c1b6884Sszhou 	else
40318c1b6884Sszhou 		mp->start = lp->next;
40328c1b6884Sszhou 	if (lp->next)
40338c1b6884Sszhou 		lp->next->prev = lp->prev;
40348c1b6884Sszhou 	else
40358c1b6884Sszhou 		mp->end = lp->prev;
40368c1b6884Sszhou }
40378c1b6884Sszhou 
40388c1b6884Sszhou static entry_t *
40398c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
40408c1b6884Sszhou {
40418c1b6884Sszhou 	entry_t *ent, *prev;
4042eb2bd662Svikram 	const char *fcn = "boot_entry_new()";
4043eb2bd662Svikram 
4044eb2bd662Svikram 	assert(mp);
4045eb2bd662Svikram 	assert(start);
4046eb2bd662Svikram 	assert(end);
40478c1b6884Sszhou 
40488c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
4049eb2bd662Svikram 	BAM_DPRINTF((D_ENTRY_NEW, fcn));
40508c1b6884Sszhou 	ent->start = start;
40518c1b6884Sszhou 	ent->end = end;
40528c1b6884Sszhou 
40538c1b6884Sszhou 	if (mp->entries == NULL) {
40548c1b6884Sszhou 		mp->entries = ent;
4055eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_NEW_FIRST, fcn));
40568c1b6884Sszhou 		return (ent);
40578c1b6884Sszhou 	}
40588c1b6884Sszhou 
40598c1b6884Sszhou 	prev = mp->entries;
40608c1b6884Sszhou 	while (prev->next)
40618c1b6884Sszhou 		prev = prev->next;
40628c1b6884Sszhou 	prev->next = ent;
40638c1b6884Sszhou 	ent->prev = prev;
4064eb2bd662Svikram 	BAM_DPRINTF((D_ENTRY_NEW_LINKED, fcn));
40658c1b6884Sszhou 	return (ent);
40668c1b6884Sszhou }
40678c1b6884Sszhou 
40688c1b6884Sszhou static void
40698c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
40708c1b6884Sszhou {
40718c1b6884Sszhou 	if (ent)
40728c1b6884Sszhou 		ent->end = lp;
40738c1b6884Sszhou }
40748c1b6884Sszhou 
40757c478bd9Sstevel@tonic-gate /*
4076843e1988Sjohnlev  * Check whether cmd matches the one indexed by which, and whether arg matches
4077843e1988Sjohnlev  * str.  which must be either KERNEL_CMD or MODULE_CMD, and a match to the
4078843e1988Sjohnlev  * respective *_DOLLAR_CMD is also acceptable.  The arg is searched using
4079843e1988Sjohnlev  * strstr(), so it can be a partial match.
4080843e1988Sjohnlev  */
4081843e1988Sjohnlev static int
4082843e1988Sjohnlev check_cmd(const char *cmd, const int which, const char *arg, const char *str)
4083843e1988Sjohnlev {
4084eb2bd662Svikram 	int			ret;
4085eb2bd662Svikram 	const char		*fcn = "check_cmd()";
4086eb2bd662Svikram 
4087eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, arg, str));
4088eb2bd662Svikram 
4089eac223ccSWilliam Kucharski 	if (cmd != NULL) {
4090843e1988Sjohnlev 		if ((strcmp(cmd, menu_cmds[which]) != 0) &&
4091843e1988Sjohnlev 		    (strcmp(cmd, menu_cmds[which + 1]) != 0)) {
4092eb2bd662Svikram 			BAM_DPRINTF((D_CHECK_CMD_CMD_NOMATCH,
4093eb2bd662Svikram 			    fcn, cmd, menu_cmds[which]));
4094843e1988Sjohnlev 			return (0);
4095843e1988Sjohnlev 		}
4096eb2bd662Svikram 		ret = (strstr(arg, str) != NULL);
4097eac223ccSWilliam Kucharski 	} else
4098eac223ccSWilliam Kucharski 		ret = 0;
4099eb2bd662Svikram 
4100eb2bd662Svikram 	if (ret) {
4101eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
4102eb2bd662Svikram 	} else {
4103eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
4104eb2bd662Svikram 	}
4105eb2bd662Svikram 
4106eb2bd662Svikram 	return (ret);
4107eb2bd662Svikram }
4108eb2bd662Svikram 
4109eb2bd662Svikram static error_t
4110eb2bd662Svikram kernel_parser(entry_t *entry, char *cmd, char *arg, int linenum)
4111eb2bd662Svikram {
4112eb2bd662Svikram 	const char		*fcn  = "kernel_parser()";
4113eb2bd662Svikram 
4114eb2bd662Svikram 	assert(entry);
4115eb2bd662Svikram 	assert(cmd);
4116eb2bd662Svikram 	assert(arg);
4117eb2bd662Svikram 
4118eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[KERNEL_CMD]) != 0 &&
4119eb2bd662Svikram 	    strcmp(cmd, menu_cmds[KERNEL_DOLLAR_CMD]) != 0) {
4120eb2bd662Svikram 		BAM_DPRINTF((D_NOT_KERNEL_CMD, fcn, cmd));
4121eb2bd662Svikram 		return (BAM_ERROR);
4122eb2bd662Svikram 	}
4123eb2bd662Svikram 
4124eb2bd662Svikram 	if (strncmp(arg, DIRECT_BOOT_32, sizeof (DIRECT_BOOT_32) - 1) == 0) {
4125eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_32, fcn, arg));
4126eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
4127eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_KERNEL,
4128eb2bd662Svikram 	    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0) {
4129eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT, fcn, arg));
4130eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT;
4131eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_64,
4132eb2bd662Svikram 	    sizeof (DIRECT_BOOT_64) - 1) == 0) {
4133eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_64, fcn, arg));
4134eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
4135eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_KERNEL,
4136eb2bd662Svikram 	    sizeof (DIRECT_BOOT_FAILSAFE_KERNEL) - 1) == 0) {
4137eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE, fcn, arg));
4138eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE;
4139bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_32,
4140bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_32) - 1) == 0) {
4141bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE_32, fcn, arg));
4142bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
4143bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_32BIT;
4144bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_64,
4145bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_64) - 1) == 0) {
4146bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE_64, fcn, arg));
4147bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
4148bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_64BIT;
4149eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT, sizeof (MULTI_BOOT) - 1) == 0) {
4150eb2bd662Svikram 		BAM_DPRINTF((D_SET_MULTIBOOT, fcn, arg));
4151eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT;
4152eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT_FAILSAFE,
4153eb2bd662Svikram 	    sizeof (MULTI_BOOT_FAILSAFE) - 1) == 0) {
4154eb2bd662Svikram 		BAM_DPRINTF((D_SET_MULTIBOOT_FAILSAFE, fcn, arg));
4155eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT | BAM_ENTRY_FAILSAFE;
4156eb2bd662Svikram 	} else if (strstr(arg, XEN_KERNEL_SUBSTR)) {
4157eb2bd662Svikram 		BAM_DPRINTF((D_SET_HV, fcn, arg));
4158eb2bd662Svikram 		entry->flags |= BAM_ENTRY_HV;
4159eb2bd662Svikram 	} else if (!(entry->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU))) {
4160eb2bd662Svikram 		BAM_DPRINTF((D_SET_HAND_KERNEL, fcn, arg));
4161eb2bd662Svikram 		return (BAM_ERROR);
416237eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
416337eb779cSVikram Hegde 	    strstr(arg, UNIX_SPACE)) {
416437eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
416537eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
416637eb779cSVikram Hegde 	    strstr(arg, AMD_UNIX_SPACE)) {
416737eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
4168eb2bd662Svikram 	} else {
4169eb2bd662Svikram 		BAM_DPRINTF((D_IS_UNKNOWN_KERNEL, fcn, arg));
4170eb2bd662Svikram 		bam_error(UNKNOWN_KERNEL_LINE, linenum);
4171eb2bd662Svikram 		return (BAM_ERROR);
4172eb2bd662Svikram 	}
4173eb2bd662Svikram 
4174eb2bd662Svikram 	return (BAM_SUCCESS);
4175eb2bd662Svikram }
4176eb2bd662Svikram 
4177eb2bd662Svikram static error_t
4178eb2bd662Svikram module_parser(entry_t *entry, char *cmd, char *arg, int linenum)
4179eb2bd662Svikram {
4180eb2bd662Svikram 	const char		*fcn = "module_parser()";
4181eb2bd662Svikram 
4182eb2bd662Svikram 	assert(entry);
4183eb2bd662Svikram 	assert(cmd);
4184eb2bd662Svikram 	assert(arg);
4185eb2bd662Svikram 
4186eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[MODULE_CMD]) != 0 &&
4187eb2bd662Svikram 	    strcmp(cmd, menu_cmds[MODULE_DOLLAR_CMD]) != 0) {
4188eb2bd662Svikram 		BAM_DPRINTF((D_NOT_MODULE_CMD, fcn, cmd));
4189eb2bd662Svikram 		return (BAM_ERROR);
4190eb2bd662Svikram 	}
4191eb2bd662Svikram 
4192eb2bd662Svikram 	if (strcmp(arg, DIRECT_BOOT_ARCHIVE) == 0 ||
4193eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_32) == 0 ||
4194eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_64) == 0 ||
4195eb2bd662Svikram 	    strcmp(arg, MULTIBOOT_ARCHIVE) == 0 ||
4196eb2bd662Svikram 	    strcmp(arg, FAILSAFE_ARCHIVE) == 0 ||
4197bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_32) == 0 ||
4198bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_64) == 0 ||
4199eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE) == 0 ||
4200eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE_ZFS) == 0) {
4201eb2bd662Svikram 		BAM_DPRINTF((D_BOOTADM_LU_MODULE, fcn, arg));
4202eb2bd662Svikram 		return (BAM_SUCCESS);
4203eb2bd662Svikram 	} else if (!(entry->flags & BAM_ENTRY_BOOTADM) &&
4204eb2bd662Svikram 	    !(entry->flags & BAM_ENTRY_LU)) {
4205eb2bd662Svikram 		/* don't emit warning for hand entries */
4206eb2bd662Svikram 		BAM_DPRINTF((D_IS_HAND_MODULE, fcn, arg));
4207eb2bd662Svikram 		return (BAM_ERROR);
4208eb2bd662Svikram 	} else {
4209eb2bd662Svikram 		BAM_DPRINTF((D_IS_UNKNOWN_MODULE, fcn, arg));
4210eb2bd662Svikram 		bam_error(UNKNOWN_MODULE_LINE, linenum);
4211eb2bd662Svikram 		return (BAM_ERROR);
4212eb2bd662Svikram 	}
4213843e1988Sjohnlev }
4214843e1988Sjohnlev 
4215843e1988Sjohnlev /*
42167c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
42177c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
42187c478bd9Sstevel@tonic-gate  */
42197c478bd9Sstevel@tonic-gate static void
42207c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
42217c478bd9Sstevel@tonic-gate {
42227c478bd9Sstevel@tonic-gate 	/*
42237c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
42247c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
42257c478bd9Sstevel@tonic-gate 	 * been processed
42267c478bd9Sstevel@tonic-gate 	 */
42278c1b6884Sszhou 	static line_t *prev = NULL;
42288c1b6884Sszhou 	static entry_t *curr_ent = NULL;
4229ae115bc7Smrj 	static int in_liveupgrade = 0;
4230772d6a58SWilliam Kucharski 	static int is_libbe_ent = 0;
42317c478bd9Sstevel@tonic-gate 
42327c478bd9Sstevel@tonic-gate 	line_t	*lp;
42337c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
42347c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
42357c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
4236eb2bd662Svikram 	const char *fcn = "line_parser()";
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate 	if (str == NULL) {
42397c478bd9Sstevel@tonic-gate 		return;
42407c478bd9Sstevel@tonic-gate 	}
42417c478bd9Sstevel@tonic-gate 
42427c478bd9Sstevel@tonic-gate 	/*
42437c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
42447c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
42457c478bd9Sstevel@tonic-gate 	 */
42467c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
42477c478bd9Sstevel@tonic-gate 
42487c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
42497c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
42507c478bd9Sstevel@tonic-gate 		str++;
42517c478bd9Sstevel@tonic-gate 
42527c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
42537c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
42547c478bd9Sstevel@tonic-gate 		sep = NULL;
42557c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
42567c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
4257ae115bc7Smrj 		if (strstr(arg, BAM_LU_HDR) != NULL) {
4258ae115bc7Smrj 			in_liveupgrade = 1;
4259ae115bc7Smrj 		} else if (strstr(arg, BAM_LU_FTR) != NULL) {
4260ae115bc7Smrj 			in_liveupgrade = 0;
4261772d6a58SWilliam Kucharski 		} else if (strstr(arg, BAM_LIBBE_FTR) != NULL) {
4262772d6a58SWilliam Kucharski 			is_libbe_ent = 1;
4263ae115bc7Smrj 		}
42647c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
42657c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
42667c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
42677c478bd9Sstevel@tonic-gate 	} else {
42687c478bd9Sstevel@tonic-gate 		/*
42697c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
42707c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
42717c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
42727c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
42737c478bd9Sstevel@tonic-gate 		 */
42747c478bd9Sstevel@tonic-gate 		cp = str;
42757c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
42767c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
42777c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
42787c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
42797c478bd9Sstevel@tonic-gate 				break;
42807c478bd9Sstevel@tonic-gate 			}
42817c478bd9Sstevel@tonic-gate 			str++;
42827c478bd9Sstevel@tonic-gate 		}
42837c478bd9Sstevel@tonic-gate 
42847c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
42857c478bd9Sstevel@tonic-gate 			save = *str;
42867c478bd9Sstevel@tonic-gate 			*str = '\0';
42877c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
42887c478bd9Sstevel@tonic-gate 			*str = save;
42897c478bd9Sstevel@tonic-gate 
42907c478bd9Sstevel@tonic-gate 			str++;
42917c478bd9Sstevel@tonic-gate 			save = *str;
42927c478bd9Sstevel@tonic-gate 			*str = '\0';
42937c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
42947c478bd9Sstevel@tonic-gate 			*str = save;
42957c478bd9Sstevel@tonic-gate 
42967c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
42977c478bd9Sstevel@tonic-gate 				str++;
42987c478bd9Sstevel@tonic-gate 			if (*str == '\0')
42997c478bd9Sstevel@tonic-gate 				arg = NULL;
43007c478bd9Sstevel@tonic-gate 			else
43017c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
43027c478bd9Sstevel@tonic-gate 		}
43037c478bd9Sstevel@tonic-gate 	}
43047c478bd9Sstevel@tonic-gate 
43057c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
43067c478bd9Sstevel@tonic-gate 
43077c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
43087c478bd9Sstevel@tonic-gate 	lp->sep = sep;
43097c478bd9Sstevel@tonic-gate 	lp->arg = arg;
43107c478bd9Sstevel@tonic-gate 	lp->line = line;
43117c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
43127c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
43137c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
43147c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
43157c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
4316ae115bc7Smrj 		    prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
43177c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
43188c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
4319eb2bd662Svikram 			curr_ent->flags |= BAM_ENTRY_BOOTADM;
4320eb2bd662Svikram 			BAM_DPRINTF((D_IS_BOOTADM_ENTRY, fcn, arg));
43218c1b6884Sszhou 		} else {
43228c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
4323ae115bc7Smrj 			if (in_liveupgrade) {
4324eb2bd662Svikram 				curr_ent->flags |= BAM_ENTRY_LU;
4325eb2bd662Svikram 				BAM_DPRINTF((D_IS_LU_ENTRY, fcn, arg));
43268c1b6884Sszhou 			}
4327ae115bc7Smrj 		}
4328ae115bc7Smrj 		curr_ent->entryNum = *entryNum;
43297c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
43307c478bd9Sstevel@tonic-gate 		/*
43317c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
43327c478bd9Sstevel@tonic-gate 		 * by the subsequent title
43337c478bd9Sstevel@tonic-gate 		 */
43347c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
43357c478bd9Sstevel@tonic-gate 		lp->flags = flag;
43367c478bd9Sstevel@tonic-gate 	} else {
43377c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
4338ae115bc7Smrj 
4339ae115bc7Smrj 		if (*entryNum == ENTRY_INIT) {
4340ae115bc7Smrj 			lp->flags = BAM_GLOBAL;
4341ae115bc7Smrj 		} else {
4342ae115bc7Smrj 			lp->flags = BAM_ENTRY;
4343ae115bc7Smrj 
4344ae115bc7Smrj 			if (cmd && arg) {
4345eb2bd662Svikram 				if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0) {
4346eb2bd662Svikram 					BAM_DPRINTF((D_IS_ROOT_CMD, fcn, arg));
4347ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_ROOT;
4348eb2bd662Svikram 				} else if (strcmp(cmd, menu_cmds[FINDROOT_CMD])
4349eb2bd662Svikram 				    == 0) {
4350eb2bd662Svikram 					BAM_DPRINTF((D_IS_FINDROOT_CMD, fcn,
4351eb2bd662Svikram 					    arg));
4352eb2bd662Svikram 					curr_ent->flags |= BAM_ENTRY_FINDROOT;
4353eb2bd662Svikram 				} else if (strcmp(cmd,
4354eb2bd662Svikram 				    menu_cmds[CHAINLOADER_CMD]) == 0) {
4355eb2bd662Svikram 					BAM_DPRINTF((D_IS_CHAINLOADER_CMD, fcn,
4356eb2bd662Svikram 					    arg));
4357ae115bc7Smrj 					curr_ent->flags |=
4358ae115bc7Smrj 					    BAM_ENTRY_CHAINLOADER;
4359eb2bd662Svikram 				} else if (kernel_parser(curr_ent, cmd, arg,
4360eb2bd662Svikram 				    lp->lineNum) != BAM_SUCCESS) {
4361eb2bd662Svikram 					(void) module_parser(curr_ent, cmd,
4362eb2bd662Svikram 					    arg, lp->lineNum);
4363eb2bd662Svikram 				}
4364ae115bc7Smrj 			}
4365ae115bc7Smrj 		}
43667c478bd9Sstevel@tonic-gate 	}
43677c478bd9Sstevel@tonic-gate 
43688c1b6884Sszhou 	/* record default, old default, and entry line ranges */
4369eac223ccSWilliam Kucharski 	if (lp->flags == BAM_GLOBAL && lp->cmd != NULL &&
43708c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
43718c1b6884Sszhou 		mp->curdefault = lp;
43728c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
43738c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
43748c1b6884Sszhou 		mp->olddefault = lp;
4375ae115bc7Smrj 	} else if (lp->flags == BAM_COMMENT &&
4376ae115bc7Smrj 	    strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) {
4377ae115bc7Smrj 		mp->old_rc_default = lp;
43788c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
4379ae115bc7Smrj 	    (lp->flags == BAM_COMMENT &&
4380772d6a58SWilliam Kucharski 	    ((strcmp(lp->arg, BAM_BOOTADM_FTR) == 0) || is_libbe_ent))) {
4381772d6a58SWilliam Kucharski 		if (is_libbe_ent) {
4382772d6a58SWilliam Kucharski 			curr_ent->flags |= BAM_ENTRY_LIBBE;
4383772d6a58SWilliam Kucharski 			is_libbe_ent = 0;
4384772d6a58SWilliam Kucharski 		}
4385772d6a58SWilliam Kucharski 
43868c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
43878c1b6884Sszhou 	}
43887c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
43897c478bd9Sstevel@tonic-gate 
43907c478bd9Sstevel@tonic-gate 	prev = lp;
43917c478bd9Sstevel@tonic-gate }
43927c478bd9Sstevel@tonic-gate 
4393eb2bd662Svikram void
439440541d5dSvikram update_numbering(menu_t *mp)
439540541d5dSvikram {
439640541d5dSvikram 	int lineNum;
439740541d5dSvikram 	int entryNum;
439840541d5dSvikram 	int old_default_value;
439940541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
440040541d5dSvikram 	char buf[PATH_MAX];
440140541d5dSvikram 
440240541d5dSvikram 	if (mp->start == NULL) {
440340541d5dSvikram 		return;
440440541d5dSvikram 	}
440540541d5dSvikram 
440640541d5dSvikram 	lineNum = LINE_INIT;
440740541d5dSvikram 	entryNum = ENTRY_INIT;
440840541d5dSvikram 	old_default_value = ENTRY_INIT;
440940541d5dSvikram 	lp = default_lp = default_entry = NULL;
441040541d5dSvikram 
441140541d5dSvikram 	prev = NULL;
441240541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
441340541d5dSvikram 		lp->lineNum = ++lineNum;
441440541d5dSvikram 
441540541d5dSvikram 		/*
441640541d5dSvikram 		 * Get the value of the default command
441740541d5dSvikram 		 */
4418eac223ccSWilliam Kucharski 		if (lp->entryNum == ENTRY_INIT && lp->cmd != NULL &&
441940541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
442040541d5dSvikram 		    lp->arg) {
442140541d5dSvikram 			old_default_value = atoi(lp->arg);
442240541d5dSvikram 			default_lp = lp;
442340541d5dSvikram 		}
442440541d5dSvikram 
442540541d5dSvikram 		/*
4426eb2bd662Svikram 		 * If not a booting entry, nothing else to fix for this
442740541d5dSvikram 		 * entry
442840541d5dSvikram 		 */
442940541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
443040541d5dSvikram 			continue;
443140541d5dSvikram 
443240541d5dSvikram 		/*
443340541d5dSvikram 		 * Record the position of the default entry.
443440541d5dSvikram 		 * The following works because global
443540541d5dSvikram 		 * commands like default and timeout should precede
443640541d5dSvikram 		 * actual boot entries, so old_default_value
443740541d5dSvikram 		 * is already known (or default cmd is missing).
443840541d5dSvikram 		 */
443940541d5dSvikram 		if (default_entry == NULL &&
444040541d5dSvikram 		    old_default_value != ENTRY_INIT &&
444140541d5dSvikram 		    lp->entryNum == old_default_value) {
444240541d5dSvikram 			default_entry = lp;
444340541d5dSvikram 		}
444440541d5dSvikram 
444540541d5dSvikram 		/*
444640541d5dSvikram 		 * Now fixup the entry number
444740541d5dSvikram 		 */
4448eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
4449eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
445040541d5dSvikram 			lp->entryNum = ++entryNum;
445140541d5dSvikram 			/* fixup the bootadm header */
445240541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
4453ae115bc7Smrj 			    prev->arg &&
4454ae115bc7Smrj 			    strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
445540541d5dSvikram 				prev->entryNum = lp->entryNum;
445640541d5dSvikram 			}
445740541d5dSvikram 		} else {
445840541d5dSvikram 			lp->entryNum = entryNum;
445940541d5dSvikram 		}
446040541d5dSvikram 	}
446140541d5dSvikram 
446240541d5dSvikram 	/*
446340541d5dSvikram 	 * No default command in menu, simply return
446440541d5dSvikram 	 */
446540541d5dSvikram 	if (default_lp == NULL) {
446640541d5dSvikram 		return;
446740541d5dSvikram 	}
446840541d5dSvikram 
446940541d5dSvikram 	free(default_lp->arg);
447040541d5dSvikram 	free(default_lp->line);
447140541d5dSvikram 
447240541d5dSvikram 	if (default_entry == NULL) {
447340541d5dSvikram 		default_lp->arg = s_strdup("0");
447440541d5dSvikram 	} else {
447540541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
447640541d5dSvikram 		    default_entry->entryNum);
447740541d5dSvikram 		default_lp->arg = s_strdup(buf);
447840541d5dSvikram 	}
447940541d5dSvikram 
448040541d5dSvikram 	/*
448140541d5dSvikram 	 * The following is required since only the line field gets
448240541d5dSvikram 	 * written back to menu.lst
448340541d5dSvikram 	 */
448440541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
448540541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
448640541d5dSvikram 	default_lp->line = s_strdup(buf);
448740541d5dSvikram }
448840541d5dSvikram 
448940541d5dSvikram 
44907c478bd9Sstevel@tonic-gate static menu_t *
44917c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
44927c478bd9Sstevel@tonic-gate {
44937c478bd9Sstevel@tonic-gate 	FILE *fp;
44947c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
44957c478bd9Sstevel@tonic-gate 	menu_t *mp;
44967c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
44977c478bd9Sstevel@tonic-gate 
44987c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
45017c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
450244da779fSWilliam Kucharski 		free(mp);
450344da779fSWilliam Kucharski 		return (NULL);
45047c478bd9Sstevel@tonic-gate 	}
45057c478bd9Sstevel@tonic-gate 
45067c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
45077c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
45087c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
45097c478bd9Sstevel@tonic-gate 	cp = buf;
45107c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
45117c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
45127c478bd9Sstevel@tonic-gate 		n = strlen(cp);
45137c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
45147c478bd9Sstevel@tonic-gate 			len -= n - 1;
45157c478bd9Sstevel@tonic-gate 			assert(len >= 2);
45167c478bd9Sstevel@tonic-gate 			cp += n - 1;
45177c478bd9Sstevel@tonic-gate 			continue;
45187c478bd9Sstevel@tonic-gate 		}
45197c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
45207c478bd9Sstevel@tonic-gate 		cp = buf;
45217c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
45227c478bd9Sstevel@tonic-gate 	}
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
45257c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
45267c478bd9Sstevel@tonic-gate 	}
45277c478bd9Sstevel@tonic-gate 
45287c478bd9Sstevel@tonic-gate 	return (mp);
45297c478bd9Sstevel@tonic-gate }
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate static error_t
45327c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
45337c478bd9Sstevel@tonic-gate {
45347c478bd9Sstevel@tonic-gate 	char *eq;
45357c478bd9Sstevel@tonic-gate 	char *opt_dup;
45367c478bd9Sstevel@tonic-gate 	int entryNum;
45377c478bd9Sstevel@tonic-gate 
45387c478bd9Sstevel@tonic-gate 	assert(mp);
45397c478bd9Sstevel@tonic-gate 	assert(mp->start);
45407c478bd9Sstevel@tonic-gate 	assert(opt);
45417c478bd9Sstevel@tonic-gate 
45427c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
45437c478bd9Sstevel@tonic-gate 
45447c478bd9Sstevel@tonic-gate 	if (entry)
45457c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
45467c478bd9Sstevel@tonic-gate 	if (title)
45477c478bd9Sstevel@tonic-gate 		*title = NULL;
45487c478bd9Sstevel@tonic-gate 
45497c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
45507c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
45517c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
45527c478bd9Sstevel@tonic-gate 		free(opt_dup);
45537c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
45547c478bd9Sstevel@tonic-gate 	}
45557c478bd9Sstevel@tonic-gate 
45567c478bd9Sstevel@tonic-gate 	*eq = '\0';
45577c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
45587c478bd9Sstevel@tonic-gate 		assert(mp->end);
45597c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
45607c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
45617c478bd9Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
45627c478bd9Sstevel@tonic-gate 			free(opt_dup);
45637c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
45647c478bd9Sstevel@tonic-gate 		}
45657c478bd9Sstevel@tonic-gate 		*entry = entryNum;
45667c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
45677c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
45687c478bd9Sstevel@tonic-gate 	} else {
45697c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
45707c478bd9Sstevel@tonic-gate 		free(opt_dup);
45717c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
45727c478bd9Sstevel@tonic-gate 	}
45737c478bd9Sstevel@tonic-gate 
45747c478bd9Sstevel@tonic-gate 	free(opt_dup);
45757c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
45767c478bd9Sstevel@tonic-gate }
45777c478bd9Sstevel@tonic-gate 
45787c478bd9Sstevel@tonic-gate /*
45797c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
45807c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
45817c478bd9Sstevel@tonic-gate  *
45827c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
45837c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
45847c478bd9Sstevel@tonic-gate  */
45857c478bd9Sstevel@tonic-gate static error_t
45867c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
45877c478bd9Sstevel@tonic-gate {
45887c478bd9Sstevel@tonic-gate 	line_t *lp;
45897c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
45907c478bd9Sstevel@tonic-gate 	int found;
45917c478bd9Sstevel@tonic-gate 	char *title = NULL;
45927c478bd9Sstevel@tonic-gate 
45937c478bd9Sstevel@tonic-gate 	assert(mp);
45947c478bd9Sstevel@tonic-gate 	assert(menu_path);
45957c478bd9Sstevel@tonic-gate 
4596eb2bd662Svikram 	/* opt is optional */
4597eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, "list_entry", menu_path,
4598eb2bd662Svikram 	    opt ? opt : "<NULL>"));
4599eb2bd662Svikram 
46007c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
46017c478bd9Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
46027c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
46037c478bd9Sstevel@tonic-gate 	}
46047c478bd9Sstevel@tonic-gate 
46057c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
46067c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
46077c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
46087c478bd9Sstevel@tonic-gate 		}
46097c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
46107c478bd9Sstevel@tonic-gate 	} else {
46117c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
46127c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
46137c478bd9Sstevel@tonic-gate 	}
46147c478bd9Sstevel@tonic-gate 
46157c478bd9Sstevel@tonic-gate 	found = 0;
46167c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
46177c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
46187c478bd9Sstevel@tonic-gate 			continue;
46197c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
46207c478bd9Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
46217c478bd9Sstevel@tonic-gate 			    lp->arg);
46227c478bd9Sstevel@tonic-gate 			found = 1;
46237c478bd9Sstevel@tonic-gate 			continue;
46247c478bd9Sstevel@tonic-gate 		}
46257c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
46267c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
46277c478bd9Sstevel@tonic-gate 			found = 1;
46287c478bd9Sstevel@tonic-gate 			continue;
46297c478bd9Sstevel@tonic-gate 		}
46307c478bd9Sstevel@tonic-gate 
46317c478bd9Sstevel@tonic-gate 		/*
46327c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
46337c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
46347c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
46357c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
46367c478bd9Sstevel@tonic-gate 		 */
46377c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
46387c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
46397c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
46407c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
46417c478bd9Sstevel@tonic-gate 			found = 1;
46427c478bd9Sstevel@tonic-gate 			continue;
46437c478bd9Sstevel@tonic-gate 		}
46447c478bd9Sstevel@tonic-gate 	}
46457c478bd9Sstevel@tonic-gate 
46467c478bd9Sstevel@tonic-gate 	if (!found) {
46477c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
46487c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
46497c478bd9Sstevel@tonic-gate 	}
46507c478bd9Sstevel@tonic-gate 
46517c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
46527c478bd9Sstevel@tonic-gate }
46537c478bd9Sstevel@tonic-gate 
4654843e1988Sjohnlev int
46557c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
46567c478bd9Sstevel@tonic-gate 	char *title,
4657eb2bd662Svikram 	char *findroot,
46587c478bd9Sstevel@tonic-gate 	char *kernel,
4659843e1988Sjohnlev 	char *mod_kernel,
466044da779fSWilliam Kucharski 	char *module,
466144da779fSWilliam Kucharski 	char *bootfs)
46627c478bd9Sstevel@tonic-gate {
4663eb2bd662Svikram 	int		lineNum;
4664eb2bd662Svikram 	int		entryNum;
46657c478bd9Sstevel@tonic-gate 	char		linebuf[BAM_MAXLINE];
4666eb2bd662Svikram 	menu_cmd_t	k_cmd;
4667eb2bd662Svikram 	menu_cmd_t	m_cmd;
4668eb2bd662Svikram 	const char	*fcn = "add_boot_entry()";
46697c478bd9Sstevel@tonic-gate 
46707c478bd9Sstevel@tonic-gate 	assert(mp);
46717c478bd9Sstevel@tonic-gate 
4672eb2bd662Svikram 	INJECT_ERROR1("ADD_BOOT_ENTRY_FINDROOT_NULL", findroot = NULL);
4673eb2bd662Svikram 	if (findroot == NULL) {
4674eb2bd662Svikram 		bam_error(NULL_FINDROOT);
4675eb2bd662Svikram 		return (BAM_ERROR);
4676eb2bd662Svikram 	}
4677eb2bd662Svikram 
46787c478bd9Sstevel@tonic-gate 	if (title == NULL) {
4679ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
46807c478bd9Sstevel@tonic-gate 	}
46817c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
46827c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
46837c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
46847c478bd9Sstevel@tonic-gate 	}
46857c478bd9Sstevel@tonic-gate 	if (module == NULL) {
4686ae115bc7Smrj 		if (bam_direct != BAM_DIRECT_DBOOT) {
46877c478bd9Sstevel@tonic-gate 			bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
46887c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
46897c478bd9Sstevel@tonic-gate 		}
46907c478bd9Sstevel@tonic-gate 
4691ae115bc7Smrj 		/* Figure the commands out from the kernel line */
4692ae115bc7Smrj 		if (strstr(kernel, "$ISADIR") != NULL) {
4693ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE;
4694ae115bc7Smrj 			k_cmd = KERNEL_DOLLAR_CMD;
4695ae115bc7Smrj 			m_cmd = MODULE_DOLLAR_CMD;
4696ae115bc7Smrj 		} else if (strstr(kernel, "amd64") != NULL) {
4697ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_64;
4698ae115bc7Smrj 			k_cmd = KERNEL_CMD;
4699ae115bc7Smrj 			m_cmd = MODULE_CMD;
4700ae115bc7Smrj 		} else {
4701ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_32;
4702ae115bc7Smrj 			k_cmd = KERNEL_CMD;
4703ae115bc7Smrj 			m_cmd = MODULE_CMD;
4704ae115bc7Smrj 		}
4705ae115bc7Smrj 	} else if ((bam_direct == BAM_DIRECT_DBOOT) &&
4706ae115bc7Smrj 	    (strstr(kernel, "$ISADIR") != NULL)) {
4707ae115bc7Smrj 		/*
4708ae115bc7Smrj 		 * If it's a non-failsafe dboot kernel, use the "kernel$"
4709ae115bc7Smrj 		 * command.  Otherwise, use "kernel".
4710ae115bc7Smrj 		 */
4711ae115bc7Smrj 		k_cmd = KERNEL_DOLLAR_CMD;
4712ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
4713ae115bc7Smrj 	} else {
4714ae115bc7Smrj 		k_cmd = KERNEL_CMD;
4715ae115bc7Smrj 		m_cmd = MODULE_CMD;
4716ae115bc7Smrj 	}
4717ae115bc7Smrj 
47187c478bd9Sstevel@tonic-gate 	if (mp->start) {
47197c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
47207c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
47217c478bd9Sstevel@tonic-gate 	} else {
47227c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
47237c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
47247c478bd9Sstevel@tonic-gate 	}
47257c478bd9Sstevel@tonic-gate 
47267c478bd9Sstevel@tonic-gate 	/*
47277c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
47287c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
47297c478bd9Sstevel@tonic-gate 	 */
47307c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
4731ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR);
47328c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
47337c478bd9Sstevel@tonic-gate 
47347c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
47357c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
47368c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
47377c478bd9Sstevel@tonic-gate 
47387c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4739eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
47408c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
4741eb2bd662Svikram 	BAM_DPRINTF((D_ADD_FINDROOT_NUM, fcn, lineNum, entryNum));
47427c478bd9Sstevel@tonic-gate 
474344da779fSWilliam Kucharski 	if (bootfs != NULL) {
474444da779fSWilliam Kucharski 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
474544da779fSWilliam Kucharski 		    menu_cmds[BOOTFS_CMD], menu_cmds[SEP_CMD], bootfs);
474644da779fSWilliam Kucharski 		line_parser(mp, linebuf, &lineNum, &entryNum);
474744da779fSWilliam Kucharski 	}
474844da779fSWilliam Kucharski 
47497c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4750ae115bc7Smrj 	    menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel);
47518c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
47527c478bd9Sstevel@tonic-gate 
4753843e1988Sjohnlev 	if (mod_kernel != NULL) {
4754843e1988Sjohnlev 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4755843e1988Sjohnlev 		    menu_cmds[m_cmd], menu_cmds[SEP_CMD], mod_kernel);
4756843e1988Sjohnlev 		line_parser(mp, linebuf, &lineNum, &entryNum);
4757843e1988Sjohnlev 	}
4758843e1988Sjohnlev 
47597c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4760ae115bc7Smrj 	    menu_cmds[m_cmd], menu_cmds[SEP_CMD], module);
47618c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
47627c478bd9Sstevel@tonic-gate 
47637c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
4764ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR);
47658c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
47667c478bd9Sstevel@tonic-gate 
47677c478bd9Sstevel@tonic-gate 	return (entryNum);
47687c478bd9Sstevel@tonic-gate }
47697c478bd9Sstevel@tonic-gate 
477044da779fSWilliam Kucharski error_t
477144da779fSWilliam Kucharski delete_boot_entry(menu_t *mp, int entryNum, int quiet)
47727c478bd9Sstevel@tonic-gate {
4773eb2bd662Svikram 	line_t		*lp;
4774eb2bd662Svikram 	line_t		*freed;
4775eb2bd662Svikram 	entry_t		*ent;
4776eb2bd662Svikram 	entry_t		*tmp;
477744da779fSWilliam Kucharski 	int		deleted = 0;
477844da779fSWilliam Kucharski 	const char	*fcn = "delete_boot_entry()";
47797c478bd9Sstevel@tonic-gate 
47807c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
47817c478bd9Sstevel@tonic-gate 
4782eb2bd662Svikram 	tmp = NULL;
4783eb2bd662Svikram 
47848c1b6884Sszhou 	ent = mp->entries;
47858c1b6884Sszhou 	while (ent) {
47868c1b6884Sszhou 		lp = ent->start;
4787772d6a58SWilliam Kucharski 
4788772d6a58SWilliam Kucharski 		/*
4789772d6a58SWilliam Kucharski 		 * Check entry number and make sure it's a modifiable entry.
4790772d6a58SWilliam Kucharski 		 *
4791772d6a58SWilliam Kucharski 		 * Guidelines:
4792772d6a58SWilliam Kucharski 		 *	+ We can modify a bootadm-created entry
4793772d6a58SWilliam Kucharski 		 *	+ We can modify a libbe-created entry
4794772d6a58SWilliam Kucharski 		 */
4795772d6a58SWilliam Kucharski 		if ((lp->flags != BAM_COMMENT &&
4796772d6a58SWilliam Kucharski 		    (((ent->flags & BAM_ENTRY_LIBBE) == 0) &&
4797772d6a58SWilliam Kucharski 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0)) ||
47988c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
47998c1b6884Sszhou 			ent = ent->next;
48007c478bd9Sstevel@tonic-gate 			continue;
48017c478bd9Sstevel@tonic-gate 		}
48027c478bd9Sstevel@tonic-gate 
48038c1b6884Sszhou 		/* free the entry content */
48048c1b6884Sszhou 		do {
48058c1b6884Sszhou 			freed = lp;
48068c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
4807eb2bd662Svikram 			BAM_DPRINTF((D_FREEING_LINE, fcn, freed->lineNum));
48088c1b6884Sszhou 			unlink_line(mp, freed);
48098c1b6884Sszhou 			line_free(freed);
48108c1b6884Sszhou 		} while (freed != ent->end);
48117c478bd9Sstevel@tonic-gate 
48128c1b6884Sszhou 		/* free the entry_t structure */
4813eb2bd662Svikram 		assert(tmp == NULL);
48148c1b6884Sszhou 		tmp = ent;
48158c1b6884Sszhou 		ent = ent->next;
48168c1b6884Sszhou 		if (tmp->prev)
48178c1b6884Sszhou 			tmp->prev->next = ent;
48187c478bd9Sstevel@tonic-gate 		else
48198c1b6884Sszhou 			mp->entries = ent;
48208c1b6884Sszhou 		if (ent)
48218c1b6884Sszhou 			ent->prev = tmp->prev;
4822eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_ENTRY, fcn, tmp->entryNum));
4823eb2bd662Svikram 		free(tmp);
4824eb2bd662Svikram 		tmp = NULL;
48257c478bd9Sstevel@tonic-gate 		deleted = 1;
48267c478bd9Sstevel@tonic-gate 	}
48277c478bd9Sstevel@tonic-gate 
4828eb2bd662Svikram 	assert(tmp == NULL);
4829eb2bd662Svikram 
48307c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
483144da779fSWilliam Kucharski 		if (quiet == DBE_PRINTERR)
48327c478bd9Sstevel@tonic-gate 			bam_error(NO_BOOTADM_MATCH);
48337c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
48347c478bd9Sstevel@tonic-gate 	}
48357c478bd9Sstevel@tonic-gate 
483640541d5dSvikram 	/*
483740541d5dSvikram 	 * Now that we have deleted an entry, update
483840541d5dSvikram 	 * the entry numbering and the default cmd.
483940541d5dSvikram 	 */
484040541d5dSvikram 	update_numbering(mp);
484140541d5dSvikram 
48427c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
48437c478bd9Sstevel@tonic-gate }
48447c478bd9Sstevel@tonic-gate 
48457c478bd9Sstevel@tonic-gate static error_t
4846eb2bd662Svikram delete_all_entries(menu_t *mp, char *dummy, char *opt)
48477c478bd9Sstevel@tonic-gate {
48487c478bd9Sstevel@tonic-gate 	assert(mp);
4849eb2bd662Svikram 	assert(dummy == NULL);
48507c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
48517c478bd9Sstevel@tonic-gate 
4852eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY0, "delete_all_entries"));
4853eb2bd662Svikram 
48547c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
4855eb2bd662Svikram 		bam_print(EMPTY_MENU);
48567c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
48577c478bd9Sstevel@tonic-gate 	}
48587c478bd9Sstevel@tonic-gate 
485944da779fSWilliam Kucharski 	if (delete_boot_entry(mp, ALL_ENTRIES, DBE_PRINTERR) != BAM_SUCCESS) {
48607c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
48617c478bd9Sstevel@tonic-gate 	}
48627c478bd9Sstevel@tonic-gate 
48637c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
48647c478bd9Sstevel@tonic-gate }
48657c478bd9Sstevel@tonic-gate 
48667c478bd9Sstevel@tonic-gate static FILE *
4867eb2bd662Svikram create_diskmap(char *osroot)
48687c478bd9Sstevel@tonic-gate {
48697c478bd9Sstevel@tonic-gate 	FILE *fp;
4870cedc7e57SEnrico Perla - Sun Microsystems 	char cmd[PATH_MAX + 16];
4871cedc7e57SEnrico Perla - Sun Microsystems 	char path[PATH_MAX];
4872eb2bd662Svikram 	const char *fcn = "create_diskmap()";
48737c478bd9Sstevel@tonic-gate 
48747c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
48757c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
48767c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
4877cedc7e57SEnrico Perla - Sun Microsystems 		int	ret;
4878cedc7e57SEnrico Perla - Sun Microsystems 
4879cedc7e57SEnrico Perla - Sun Microsystems 		ret = snprintf(path, sizeof (path), "%s/%s", osroot,
4880cedc7e57SEnrico Perla - Sun Microsystems 		    CREATE_DISKMAP);
4881cedc7e57SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (path)) {
4882cedc7e57SEnrico Perla - Sun Microsystems 			bam_error(PATH_TOO_LONG, osroot);
4883cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
4884cedc7e57SEnrico Perla - Sun Microsystems 		}
4885cedc7e57SEnrico Perla - Sun Microsystems 		if (is_safe_exec(path) == BAM_ERROR)
4886cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
4887cedc7e57SEnrico Perla - Sun Microsystems 
48887c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
4889eb2bd662Svikram 		    "%s/%s > /dev/null", osroot, CREATE_DISKMAP);
4890eb2bd662Svikram 		if (exec_cmd(cmd, NULL) != 0)
4891eb2bd662Svikram 			return (NULL);
48927c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
4893eb2bd662Svikram 		INJECT_ERROR1("DISKMAP_CREATE_FAIL", fp = NULL);
4894eb2bd662Svikram 		if (fp) {
4895eb2bd662Svikram 			BAM_DPRINTF((D_CREATED_DISKMAP, fcn, GRUBDISK_MAP));
4896eb2bd662Svikram 		} else {
4897eb2bd662Svikram 			BAM_DPRINTF((D_CREATE_DISKMAP_FAIL, fcn, GRUBDISK_MAP));
4898eb2bd662Svikram 		}
48997c478bd9Sstevel@tonic-gate 	}
49007c478bd9Sstevel@tonic-gate 	return (fp);
49017c478bd9Sstevel@tonic-gate }
49027c478bd9Sstevel@tonic-gate 
49037c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
49047c478bd9Sstevel@tonic-gate 
49057c478bd9Sstevel@tonic-gate static int
49067c478bd9Sstevel@tonic-gate get_partition(char *device)
49077c478bd9Sstevel@tonic-gate {
49087c478bd9Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
49097c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
49107c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
49117c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
4912e998e519SSheshadri Vasudevan #ifdef i386
4913e998e519SSheshadri Vasudevan 	ext_part_t *epp;
4914e998e519SSheshadri Vasudevan 	uint32_t secnum, numsec;
491528074f82SSheshadri Vasudevan 	int rval, pno, ext_partno = -1;
4916e998e519SSheshadri Vasudevan #endif
49177c478bd9Sstevel@tonic-gate 
49187c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
49197c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
49207c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
49217c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
49227c478bd9Sstevel@tonic-gate 		*slice = '\0';
49237c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
49247c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
49257c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
49267c478bd9Sstevel@tonic-gate 		*slice = 's';
49277c478bd9Sstevel@tonic-gate 
49287c478bd9Sstevel@tonic-gate 	/* read boot sector */
49297c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
49307c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
49317c478bd9Sstevel@tonic-gate 		return (partno);
49327c478bd9Sstevel@tonic-gate 	}
49337c478bd9Sstevel@tonic-gate 	(void) close(fd);
49347c478bd9Sstevel@tonic-gate 
4935e998e519SSheshadri Vasudevan #ifdef i386
4936e998e519SSheshadri Vasudevan 	/* Read/Initialize extended partition information */
4937e998e519SSheshadri Vasudevan 	if ((rval = libfdisk_init(&epp, wholedisk, NULL, FDISK_READ_DISK))
4938e998e519SSheshadri Vasudevan 	    != FDISK_SUCCESS) {
4939e998e519SSheshadri Vasudevan 		switch (rval) {
4940e998e519SSheshadri Vasudevan 			/*
4941e998e519SSheshadri Vasudevan 			 * FDISK_EBADLOGDRIVE and FDISK_ENOLOGDRIVE can
4942e998e519SSheshadri Vasudevan 			 * be considered as soft errors and hence
4943e998e519SSheshadri Vasudevan 			 * we do not return
4944e998e519SSheshadri Vasudevan 			 */
4945e998e519SSheshadri Vasudevan 			case FDISK_EBADLOGDRIVE:
4946e998e519SSheshadri Vasudevan 				break;
4947e998e519SSheshadri Vasudevan 			case FDISK_ENOLOGDRIVE:
4948e998e519SSheshadri Vasudevan 				break;
49496cb5747bSSharath M Srinivasan 			case FDISK_EBADMAGIC:
49506cb5747bSSharath M Srinivasan 				/*FALLTHROUGH*/
4951e998e519SSheshadri Vasudevan 			default:
4952e998e519SSheshadri Vasudevan 				free(wholedisk);
49536cb5747bSSharath M Srinivasan 				libfdisk_fini(&epp);
4954e998e519SSheshadri Vasudevan 				return (partno);
4955e998e519SSheshadri Vasudevan 				break;
4956e998e519SSheshadri Vasudevan 		}
4957e998e519SSheshadri Vasudevan 	}
4958e998e519SSheshadri Vasudevan #endif
4959e998e519SSheshadri Vasudevan 	free(wholedisk);
4960e998e519SSheshadri Vasudevan 
49617c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
49627c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
49637c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
49647c478bd9Sstevel@tonic-gate 		struct ipart *part =
49657c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
49667c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
49677c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
49687c478bd9Sstevel@tonic-gate 				partno = i;
49697c478bd9Sstevel@tonic-gate 				break;
49707c478bd9Sstevel@tonic-gate 			}
49717c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
4972e998e519SSheshadri Vasudevan #ifdef i386
4973e998e519SSheshadri Vasudevan 			if ((part->systid == SUNIXOS &&
4974e998e519SSheshadri Vasudevan 			    (fdisk_is_linux_swap(epp, part->relsect,
4975e998e519SSheshadri Vasudevan 			    NULL) != 0)) || part->systid == SUNIXOS2) {
4976e998e519SSheshadri Vasudevan #else
49777c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
49787c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
4979e998e519SSheshadri Vasudevan #endif
49807c478bd9Sstevel@tonic-gate 				partno = i;
49817c478bd9Sstevel@tonic-gate 				break;
49827c478bd9Sstevel@tonic-gate 			}
498328074f82SSheshadri Vasudevan 
498428074f82SSheshadri Vasudevan #ifdef i386
498528074f82SSheshadri Vasudevan 			if (fdisk_is_dos_extended(part->systid))
498628074f82SSheshadri Vasudevan 				ext_partno = i;
498728074f82SSheshadri Vasudevan #endif
49887c478bd9Sstevel@tonic-gate 		}
49897c478bd9Sstevel@tonic-gate 	}
4990e998e519SSheshadri Vasudevan #ifdef i386
499128074f82SSheshadri Vasudevan 	/* If no primary solaris partition, check extended partition */
499228074f82SSheshadri Vasudevan 	if ((partno == -1) && (ext_partno != -1)) {
499328074f82SSheshadri Vasudevan 		rval = fdisk_get_solaris_part(epp, &pno, &secnum, &numsec);
499428074f82SSheshadri Vasudevan 		if (rval == FDISK_SUCCESS) {
499528074f82SSheshadri Vasudevan 			partno = pno - 1;
499628074f82SSheshadri Vasudevan 		}
499728074f82SSheshadri Vasudevan 	}
4998e998e519SSheshadri Vasudevan 	libfdisk_fini(&epp);
4999e998e519SSheshadri Vasudevan #endif
50007c478bd9Sstevel@tonic-gate 	return (partno);
50017c478bd9Sstevel@tonic-gate }
50027c478bd9Sstevel@tonic-gate 
5003eb2bd662Svikram char *
5004eb2bd662Svikram get_grubroot(char *osroot, char *osdev, char *menu_root)
50057c478bd9Sstevel@tonic-gate {
5006eb2bd662Svikram 	char		*grubroot;	/* (hd#,#,#) */
50077c478bd9Sstevel@tonic-gate 	char		*slice;
50087c478bd9Sstevel@tonic-gate 	char		*grubhd;
50097c478bd9Sstevel@tonic-gate 	int		fdiskpart;
50107c478bd9Sstevel@tonic-gate 	int		found = 0;
5011eb2bd662Svikram 	char		*devname;
5012eb2bd662Svikram 	char		*ctdname = strstr(osdev, "dsk/");
50137c478bd9Sstevel@tonic-gate 	char		linebuf[PATH_MAX];
5014eb2bd662Svikram 	FILE		*fp;
50157c478bd9Sstevel@tonic-gate 
5016eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_INVALID_OSDEV", ctdname = NULL);
5017eb2bd662Svikram 	if (ctdname == NULL) {
5018eb2bd662Svikram 		bam_error(INVALID_DEV_DSK, osdev);
50197c478bd9Sstevel@tonic-gate 		return (NULL);
5020eb2bd662Svikram 	}
5021eb2bd662Svikram 
5022eb2bd662Svikram 	if (menu_root && !menu_on_bootdisk(osroot, menu_root)) {
5023eb2bd662Svikram 		/* menu bears no resemblance to our reality */
502437eb779cSVikram Hegde 		bam_error(CANNOT_GRUBROOT_BOOTDISK, osdev);
5025eb2bd662Svikram 		return (NULL);
5026eb2bd662Svikram 	}
50277c478bd9Sstevel@tonic-gate 
50287c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
50297c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
50307c478bd9Sstevel@tonic-gate 	if (slice)
50317c478bd9Sstevel@tonic-gate 		*slice = '\0';
50327c478bd9Sstevel@tonic-gate 
5033eb2bd662Svikram 	fp = create_diskmap(osroot);
5034eb2bd662Svikram 	if (fp == NULL) {
5035eb2bd662Svikram 		bam_error(DISKMAP_FAIL, osroot);
5036eb2bd662Svikram 		return (NULL);
5037eb2bd662Svikram 	}
5038eb2bd662Svikram 
50397c478bd9Sstevel@tonic-gate 	rewind(fp);
50407c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
50417c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
50427c478bd9Sstevel@tonic-gate 		if (grubhd)
50437c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
50447c478bd9Sstevel@tonic-gate 		else
50457c478bd9Sstevel@tonic-gate 			devname = NULL;
50467c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
50477c478bd9Sstevel@tonic-gate 			found = 1;
50487c478bd9Sstevel@tonic-gate 			break;
50497c478bd9Sstevel@tonic-gate 		}
50507c478bd9Sstevel@tonic-gate 	}
50517c478bd9Sstevel@tonic-gate 
50527c478bd9Sstevel@tonic-gate 	if (slice)
50537c478bd9Sstevel@tonic-gate 		*slice = 's';
50547c478bd9Sstevel@tonic-gate 
5055eb2bd662Svikram 	(void) fclose(fp);
5056eb2bd662Svikram 	fp = NULL;
5057eb2bd662Svikram 
5058eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_BIOSDEV_FAIL", found = 0);
50597c478bd9Sstevel@tonic-gate 	if (found == 0) {
506037eb779cSVikram Hegde 		bam_error(BIOSDEV_SKIP, osdev);
5061eb2bd662Svikram 		return (NULL);
50627c478bd9Sstevel@tonic-gate 	}
50637c478bd9Sstevel@tonic-gate 
5064eb2bd662Svikram 	fdiskpart = get_partition(osdev);
5065eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_FDISK_FAIL", fdiskpart = -1);
5066eb2bd662Svikram 	if (fdiskpart == -1) {
5067eb2bd662Svikram 		bam_error(FDISKPART_FAIL, osdev);
50687c478bd9Sstevel@tonic-gate 		return (NULL);
5069eb2bd662Svikram 	}
50707c478bd9Sstevel@tonic-gate 
5071eb2bd662Svikram 	grubroot = s_calloc(1, 10);
50727c478bd9Sstevel@tonic-gate 	if (slice) {
5073eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d,%c)",
50747c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
50757c478bd9Sstevel@tonic-gate 	} else
5076eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d)",
50777c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
50787c478bd9Sstevel@tonic-gate 
5079eb2bd662Svikram 	assert(fp == NULL);
5080eb2bd662Svikram 	assert(strncmp(grubroot, "(hd", strlen("(hd")) == 0);
5081eb2bd662Svikram 	return (grubroot);
5082eb2bd662Svikram }
5083eb2bd662Svikram 
5084eb2bd662Svikram static char *
5085eb2bd662Svikram find_primary_common(char *mntpt, char *fstype)
5086eb2bd662Svikram {
5087eb2bd662Svikram 	char		signdir[PATH_MAX];
5088eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
5089eb2bd662Svikram 	char		*lu;
5090eb2bd662Svikram 	char		*ufs;
5091eb2bd662Svikram 	char		*zfs;
5092eb2bd662Svikram 	DIR		*dirp = NULL;
5093eb2bd662Svikram 	struct dirent	*entp;
5094eb2bd662Svikram 	struct stat	sb;
5095eb2bd662Svikram 	const char	*fcn = "find_primary_common()";
5096eb2bd662Svikram 
5097eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
5098eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
5099eb2bd662Svikram 
5100eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
5101eb2bd662Svikram 		BAM_DPRINTF((D_NO_SIGNDIR, fcn, signdir));
5102eb2bd662Svikram 		return (NULL);
5103eb2bd662Svikram 	}
5104eb2bd662Svikram 
5105eb2bd662Svikram 	dirp = opendir(signdir);
5106eb2bd662Svikram 	INJECT_ERROR1("SIGNDIR_OPENDIR_FAIL", dirp = NULL);
5107eb2bd662Svikram 	if (dirp == NULL) {
5108eb2bd662Svikram 		bam_error(OPENDIR_FAILED, signdir, strerror(errno));
5109eb2bd662Svikram 		return (NULL);
5110eb2bd662Svikram 	}
5111eb2bd662Svikram 
5112eb2bd662Svikram 	ufs = zfs = lu = NULL;
5113eb2bd662Svikram 
5114eb2bd662Svikram 	while (entp = readdir(dirp)) {
5115eb2bd662Svikram 		if (strcmp(entp->d_name, ".") == 0 ||
5116eb2bd662Svikram 		    strcmp(entp->d_name, "..") == 0)
5117eb2bd662Svikram 			continue;
5118eb2bd662Svikram 
5119eb2bd662Svikram 		(void) snprintf(tmpsign, sizeof (tmpsign), "%s", entp->d_name);
5120eb2bd662Svikram 
5121eb2bd662Svikram 		if (lu == NULL &&
5122eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
5123eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
5124eb2bd662Svikram 			lu = s_strdup(tmpsign);
5125eb2bd662Svikram 		}
5126eb2bd662Svikram 
5127eb2bd662Svikram 		if (ufs == NULL &&
5128eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
5129eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
5130eb2bd662Svikram 			ufs = s_strdup(tmpsign);
5131eb2bd662Svikram 		}
5132eb2bd662Svikram 
5133eb2bd662Svikram 		if (zfs == NULL &&
5134eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
5135eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
5136eb2bd662Svikram 			zfs = s_strdup(tmpsign);
5137eb2bd662Svikram 		}
5138eb2bd662Svikram 	}
5139eb2bd662Svikram 
5140eb2bd662Svikram 	BAM_DPRINTF((D_EXIST_PRIMARY_SIGNS, fcn,
5141eb2bd662Svikram 	    zfs ? zfs : "NULL",
5142eb2bd662Svikram 	    ufs ? ufs : "NULL",
5143eb2bd662Svikram 	    lu ? lu : "NULL"));
5144eb2bd662Svikram 
5145eb2bd662Svikram 	if (dirp) {
5146eb2bd662Svikram 		(void) closedir(dirp);
5147eb2bd662Svikram 		dirp = NULL;
5148eb2bd662Svikram 	}
5149eb2bd662Svikram 
5150eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
5151eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs");
5152eb2bd662Svikram 		free(zfs);
5153eb2bd662Svikram 		zfs = NULL;
5154eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
5155eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs");
5156eb2bd662Svikram 		free(ufs);
5157eb2bd662Svikram 		ufs = NULL;
5158eb2bd662Svikram 	}
5159eb2bd662Svikram 
5160eb2bd662Svikram 	assert(dirp == NULL);
5161eb2bd662Svikram 
5162eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
5163eb2bd662Svikram 	if (lu) {
5164eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu));
5165eb2bd662Svikram 		free(lu);
5166eb2bd662Svikram 		lu = NULL;
5167eb2bd662Svikram 	}
5168eb2bd662Svikram 
5169eb2bd662Svikram 	return (zfs ? zfs : ufs);
5170eb2bd662Svikram }
5171eb2bd662Svikram 
5172eb2bd662Svikram static char *
5173eb2bd662Svikram find_backup_common(char *mntpt, char *fstype)
5174eb2bd662Svikram {
5175eb2bd662Svikram 	FILE		*bfp = NULL;
5176eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
5177eb2bd662Svikram 	char		backup[PATH_MAX];
5178eb2bd662Svikram 	char		*ufs;
5179eb2bd662Svikram 	char		*zfs;
5180eb2bd662Svikram 	char		*lu;
5181eb2bd662Svikram 	int		error;
5182eb2bd662Svikram 	const char	*fcn = "find_backup_common()";
5183eb2bd662Svikram 
5184eb2bd662Svikram 	/*
5185eb2bd662Svikram 	 * We didn't find it in the primary directory.
5186eb2bd662Svikram 	 * Look at the backup
5187eb2bd662Svikram 	 */
5188eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
5189eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
5190eb2bd662Svikram 
5191eb2bd662Svikram 	bfp = fopen(backup, "r");
5192eb2bd662Svikram 	if (bfp == NULL) {
5193eb2bd662Svikram 		error = errno;
5194eb2bd662Svikram 		if (bam_verbose) {
5195eb2bd662Svikram 			bam_error(OPEN_FAIL, backup, strerror(error));
5196eb2bd662Svikram 		}
5197eb2bd662Svikram 		BAM_DPRINTF((D_OPEN_FAIL, fcn, backup, strerror(error)));
5198eb2bd662Svikram 		return (NULL);
5199eb2bd662Svikram 	}
5200eb2bd662Svikram 
5201eb2bd662Svikram 	ufs = zfs = lu = NULL;
5202eb2bd662Svikram 
5203eb2bd662Svikram 	while (s_fgets(tmpsign, sizeof (tmpsign), bfp) != NULL) {
5204eb2bd662Svikram 
5205eb2bd662Svikram 		if (lu == NULL &&
5206eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
5207eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
5208eb2bd662Svikram 			lu = s_strdup(tmpsign);
5209eb2bd662Svikram 		}
5210eb2bd662Svikram 
5211eb2bd662Svikram 		if (ufs == NULL &&
5212eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
5213eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
5214eb2bd662Svikram 			ufs = s_strdup(tmpsign);
5215eb2bd662Svikram 		}
5216eb2bd662Svikram 
5217eb2bd662Svikram 		if (zfs == NULL &&
5218eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
5219eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
5220eb2bd662Svikram 			zfs = s_strdup(tmpsign);
5221eb2bd662Svikram 		}
5222eb2bd662Svikram 	}
5223eb2bd662Svikram 
5224eb2bd662Svikram 	BAM_DPRINTF((D_EXIST_BACKUP_SIGNS, fcn,
5225eb2bd662Svikram 	    zfs ? zfs : "NULL",
5226eb2bd662Svikram 	    ufs ? ufs : "NULL",
5227eb2bd662Svikram 	    lu ? lu : "NULL"));
5228eb2bd662Svikram 
5229eb2bd662Svikram 	if (bfp) {
5230eb2bd662Svikram 		(void) fclose(bfp);
5231eb2bd662Svikram 		bfp = NULL;
5232eb2bd662Svikram 	}
5233eb2bd662Svikram 
5234eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
5235eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs");
5236eb2bd662Svikram 		free(zfs);
5237eb2bd662Svikram 		zfs = NULL;
5238eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
5239eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs");
5240eb2bd662Svikram 		free(ufs);
5241eb2bd662Svikram 		ufs = NULL;
5242eb2bd662Svikram 	}
5243eb2bd662Svikram 
5244eb2bd662Svikram 	assert(bfp == NULL);
5245eb2bd662Svikram 
5246eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
5247eb2bd662Svikram 	if (lu) {
5248eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu));
5249eb2bd662Svikram 		free(lu);
5250eb2bd662Svikram 		lu = NULL;
5251eb2bd662Svikram 	}
5252eb2bd662Svikram 
5253eb2bd662Svikram 	return (zfs ? zfs : ufs);
5254eb2bd662Svikram }
5255eb2bd662Svikram 
5256eb2bd662Svikram static char *
5257eb2bd662Svikram find_ufs_existing(char *osroot)
5258eb2bd662Svikram {
5259eb2bd662Svikram 	char		*sign;
5260eb2bd662Svikram 	const char	*fcn = "find_ufs_existing()";
5261eb2bd662Svikram 
5262eb2bd662Svikram 	sign = find_primary_common(osroot, "ufs");
5263eb2bd662Svikram 	if (sign == NULL) {
5264eb2bd662Svikram 		sign = find_backup_common(osroot, "ufs");
5265eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL"));
5266eb2bd662Svikram 	} else {
5267eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign));
5268eb2bd662Svikram 	}
5269eb2bd662Svikram 
5270eb2bd662Svikram 	return (sign);
5271eb2bd662Svikram }
5272eb2bd662Svikram 
5273eb2bd662Svikram char *
5274eb2bd662Svikram get_mountpoint(char *special, char *fstype)
5275eb2bd662Svikram {
5276eb2bd662Svikram 	FILE		*mntfp;
5277eb2bd662Svikram 	struct mnttab	mp = {0};
5278eb2bd662Svikram 	struct mnttab	mpref = {0};
5279eb2bd662Svikram 	int		error;
5280eb2bd662Svikram 	int		ret;
5281eb2bd662Svikram 	const char	*fcn = "get_mountpoint()";
5282eb2bd662Svikram 
5283eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, special, fstype));
5284eb2bd662Svikram 
5285eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
5286eb2bd662Svikram 	error = errno;
5287eb2bd662Svikram 	INJECT_ERROR1("MNTTAB_ERR_GET_MNTPT", mntfp = NULL);
5288eb2bd662Svikram 	if (mntfp == NULL) {
5289eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
5290eb2bd662Svikram 		return (NULL);
5291eb2bd662Svikram 	}
5292eb2bd662Svikram 
5293eb2bd662Svikram 	mpref.mnt_special = special;
5294eb2bd662Svikram 	mpref.mnt_fstype = fstype;
5295eb2bd662Svikram 
5296eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
5297eb2bd662Svikram 	INJECT_ERROR1("GET_MOUNTPOINT_MNTANY", ret = 1);
5298eb2bd662Svikram 	if (ret != 0) {
5299eb2bd662Svikram 		(void) fclose(mntfp);
5300eb2bd662Svikram 		BAM_DPRINTF((D_NO_MNTPT, fcn, special, fstype));
5301eb2bd662Svikram 		return (NULL);
5302eb2bd662Svikram 	}
5303eb2bd662Svikram 	(void) fclose(mntfp);
5304eb2bd662Svikram 
5305eb2bd662Svikram 	assert(mp.mnt_mountp);
5306eb2bd662Svikram 
5307eb2bd662Svikram 	BAM_DPRINTF((D_GET_MOUNTPOINT_RET, fcn, special, mp.mnt_mountp));
5308eb2bd662Svikram 
5309eb2bd662Svikram 	return (s_strdup(mp.mnt_mountp));
5310eb2bd662Svikram }
5311eb2bd662Svikram 
5312eb2bd662Svikram /*
5313eb2bd662Svikram  * Mounts a "legacy" top dataset (if needed)
5314eb2bd662Svikram  * Returns:	The mountpoint of the legacy top dataset or NULL on error
5315eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
5316eb2bd662Svikram  */
5317eb2bd662Svikram static char *
5318eb2bd662Svikram mount_legacy_dataset(char *pool, zfs_mnted_t *mnted)
5319eb2bd662Svikram {
5320eb2bd662Svikram 	char		cmd[PATH_MAX];
5321eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
5322eb2bd662Svikram 	filelist_t	flist = {0};
5323eb2bd662Svikram 	char		*is_mounted;
5324eb2bd662Svikram 	struct stat	sb;
5325eb2bd662Svikram 	int		ret;
5326eb2bd662Svikram 	const char	*fcn = "mount_legacy_dataset()";
5327eb2bd662Svikram 
5328eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool));
5329eb2bd662Svikram 
5330eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
5331eb2bd662Svikram 
5332eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5333eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
5334eb2bd662Svikram 	    pool);
5335eb2bd662Svikram 
5336eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5337eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_CMD", ret = 1);
5338eb2bd662Svikram 	if (ret != 0) {
5339eb2bd662Svikram 		bam_error(ZFS_MNTED_FAILED, pool);
5340eb2bd662Svikram 		return (NULL);
5341eb2bd662Svikram 	}
5342eb2bd662Svikram 
5343eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_OUT", flist.head = NULL);
5344eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5345eb2bd662Svikram 		bam_error(BAD_ZFS_MNTED, pool);
5346eb2bd662Svikram 		filelist_free(&flist);
5347eb2bd662Svikram 		return (NULL);
5348eb2bd662Svikram 	}
5349eb2bd662Svikram 
5350eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5351eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_YES", is_mounted = "yes");
5352eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_NO", is_mounted = "no");
5353eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5354eb2bd662Svikram 		filelist_free(&flist);
5355eb2bd662Svikram 		*mnted = LEGACY_ALREADY;
5356eb2bd662Svikram 		/* get_mountpoint returns a strdup'ed string */
5357eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_ALREADY, fcn, pool));
5358eb2bd662Svikram 		return (get_mountpoint(pool, "zfs"));
5359eb2bd662Svikram 	}
5360eb2bd662Svikram 
5361eb2bd662Svikram 	filelist_free(&flist);
5362eb2bd662Svikram 
5363eb2bd662Svikram 	/*
5364eb2bd662Svikram 	 * legacy top dataset is not mounted. Mount it now
5365eb2bd662Svikram 	 * First create a mountpoint.
5366eb2bd662Svikram 	 */
5367eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt), "%s.%d",
5368eb2bd662Svikram 	    ZFS_LEGACY_MNTPT, getpid());
5369eb2bd662Svikram 
5370eb2bd662Svikram 	ret = stat(tmpmnt, &sb);
5371eb2bd662Svikram 	if (ret == -1) {
5372eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_ABS, fcn, pool, tmpmnt));
537348847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(tmpmnt, DIR_PERMS);
5374eb2bd662Svikram 		INJECT_ERROR1("Z_MOUNT_TOP_LEG_MNTPT_MKDIRP", ret = -1);
5375eb2bd662Svikram 		if (ret == -1) {
5376eb2bd662Svikram 			bam_error(MKDIR_FAILED, tmpmnt, strerror(errno));
5377eb2bd662Svikram 			return (NULL);
5378eb2bd662Svikram 		}
5379eb2bd662Svikram 	} else {
5380eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_PRES, fcn, pool, tmpmnt));
5381eb2bd662Svikram 	}
5382eb2bd662Svikram 
5383eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5384eb2bd662Svikram 	    "/sbin/mount -F zfs %s %s",
5385eb2bd662Svikram 	    pool, tmpmnt);
5386eb2bd662Svikram 
5387eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5388eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_LEG_MOUNT_CMD", ret = 1);
5389eb2bd662Svikram 	if (ret != 0) {
5390eb2bd662Svikram 		bam_error(ZFS_MOUNT_FAILED, pool);
5391eb2bd662Svikram 		(void) rmdir(tmpmnt);
5392eb2bd662Svikram 		return (NULL);
5393eb2bd662Svikram 	}
5394eb2bd662Svikram 
5395eb2bd662Svikram 	*mnted = LEGACY_MOUNTED;
5396eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MOUNTED, fcn, pool, tmpmnt));
5397eb2bd662Svikram 	return (s_strdup(tmpmnt));
5398eb2bd662Svikram }
5399eb2bd662Svikram 
5400eb2bd662Svikram /*
5401eb2bd662Svikram  * Mounts the top dataset (if needed)
5402eb2bd662Svikram  * Returns:	The mountpoint of the top dataset or NULL on error
5403eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
5404eb2bd662Svikram  */
5405eb2bd662Svikram static char *
5406eb2bd662Svikram mount_top_dataset(char *pool, zfs_mnted_t *mnted)
5407eb2bd662Svikram {
5408eb2bd662Svikram 	char		cmd[PATH_MAX];
5409eb2bd662Svikram 	filelist_t	flist = {0};
5410eb2bd662Svikram 	char		*is_mounted;
5411eb2bd662Svikram 	char		*mntpt;
5412eb2bd662Svikram 	char		*zmntpt;
5413eb2bd662Svikram 	int		ret;
5414eb2bd662Svikram 	const char	*fcn = "mount_top_dataset()";
5415eb2bd662Svikram 
5416eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
5417eb2bd662Svikram 
5418eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool));
5419eb2bd662Svikram 
5420eb2bd662Svikram 	/*
5421eb2bd662Svikram 	 * First check if the top dataset is a "legacy" dataset
5422eb2bd662Svikram 	 */
5423eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5424eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
5425eb2bd662Svikram 	    pool);
5426eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5427eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_GET_MNTPT", ret = 1);
5428eb2bd662Svikram 	if (ret != 0) {
5429eb2bd662Svikram 		bam_error(ZFS_MNTPT_FAILED, pool);
5430eb2bd662Svikram 		return (NULL);
5431eb2bd662Svikram 	}
5432eb2bd662Svikram 
5433eb2bd662Svikram 	if (flist.head && (flist.head == flist.tail)) {
5434eb2bd662Svikram 		char *legacy = strtok(flist.head->line, " \t\n");
5435eb2bd662Svikram 		if (legacy && strcmp(legacy, "legacy") == 0) {
5436eb2bd662Svikram 			filelist_free(&flist);
5437eb2bd662Svikram 			BAM_DPRINTF((D_Z_IS_LEGACY, fcn, pool));
5438eb2bd662Svikram 			return (mount_legacy_dataset(pool, mnted));
5439eb2bd662Svikram 		}
5440eb2bd662Svikram 	}
5441eb2bd662Svikram 
5442eb2bd662Svikram 	filelist_free(&flist);
5443eb2bd662Svikram 
5444eb2bd662Svikram 	BAM_DPRINTF((D_Z_IS_NOT_LEGACY, fcn, pool));
5445eb2bd662Svikram 
5446eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5447eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
5448eb2bd662Svikram 	    pool);
5449eb2bd662Svikram 
5450eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5451eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED", ret = 1);
5452eb2bd662Svikram 	if (ret != 0) {
5453eb2bd662Svikram 		bam_error(ZFS_MNTED_FAILED, pool);
5454eb2bd662Svikram 		return (NULL);
5455eb2bd662Svikram 	}
5456eb2bd662Svikram 
5457eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_VAL", flist.head = NULL);
5458eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5459eb2bd662Svikram 		bam_error(BAD_ZFS_MNTED, pool);
5460eb2bd662Svikram 		filelist_free(&flist);
5461eb2bd662Svikram 		return (NULL);
5462eb2bd662Svikram 	}
5463eb2bd662Svikram 
5464eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5465eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_YES", is_mounted = "yes");
5466eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_NO", is_mounted = "no");
5467eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5468eb2bd662Svikram 		filelist_free(&flist);
5469eb2bd662Svikram 		*mnted = ZFS_ALREADY;
5470eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_ALREADY, fcn, pool));
5471eb2bd662Svikram 		goto mounted;
5472eb2bd662Svikram 	}
5473eb2bd662Svikram 
5474eb2bd662Svikram 	filelist_free(&flist);
5475eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOT_ALREADY, fcn, pool));
5476eb2bd662Svikram 
5477eb2bd662Svikram 	/* top dataset is not mounted. Mount it now */
5478eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5479eb2bd662Svikram 	    "/sbin/zfs mount %s", pool);
5480eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5481eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_MOUNT_CMD", ret = 1);
5482eb2bd662Svikram 	if (ret != 0) {
5483eb2bd662Svikram 		bam_error(ZFS_MOUNT_FAILED, pool);
5484eb2bd662Svikram 		return (NULL);
5485eb2bd662Svikram 	}
5486eb2bd662Svikram 	*mnted = ZFS_MOUNTED;
5487eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOW, fcn, pool));
5488eb2bd662Svikram 	/*FALLTHRU*/
5489eb2bd662Svikram mounted:
5490eb2bd662Svikram 	/*
5491eb2bd662Svikram 	 * Now get the mountpoint
5492eb2bd662Svikram 	 */
5493eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5494eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
5495eb2bd662Svikram 	    pool);
5496eb2bd662Svikram 
5497eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5498eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_CMD", ret = 1);
5499eb2bd662Svikram 	if (ret != 0) {
5500eb2bd662Svikram 		bam_error(ZFS_MNTPT_FAILED, pool);
5501eb2bd662Svikram 		goto error;
5502eb2bd662Svikram 	}
5503eb2bd662Svikram 
5504eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_OUT", flist.head = NULL);
5505eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5506eb2bd662Svikram 		bam_error(NULL_ZFS_MNTPT, pool);
5507eb2bd662Svikram 		goto error;
5508eb2bd662Svikram 	}
5509eb2bd662Svikram 
5510eb2bd662Svikram 	mntpt = strtok(flist.head->line, " \t\n");
5511eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_STRTOK", mntpt = "foo");
5512eb2bd662Svikram 	if (*mntpt != '/') {
5513eb2bd662Svikram 		bam_error(BAD_ZFS_MNTPT, pool, mntpt);
5514eb2bd662Svikram 		goto error;
5515eb2bd662Svikram 	}
5516eb2bd662Svikram 	zmntpt = s_strdup(mntpt);
5517eb2bd662Svikram 
5518eb2bd662Svikram 	filelist_free(&flist);
5519eb2bd662Svikram 
5520eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MNTPT, fcn, pool, zmntpt));
5521eb2bd662Svikram 
5522eb2bd662Svikram 	return (zmntpt);
5523eb2bd662Svikram 
5524eb2bd662Svikram error:
5525eb2bd662Svikram 	filelist_free(&flist);
5526eb2bd662Svikram 	(void) umount_top_dataset(pool, *mnted, NULL);
5527eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
5528eb2bd662Svikram 	return (NULL);
5529eb2bd662Svikram }
5530eb2bd662Svikram 
5531eb2bd662Svikram static int
5532eb2bd662Svikram umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt)
5533eb2bd662Svikram {
5534eb2bd662Svikram 	char		cmd[PATH_MAX];
5535eb2bd662Svikram 	int		ret;
5536eb2bd662Svikram 	const char	*fcn = "umount_top_dataset()";
5537eb2bd662Svikram 
5538eb2bd662Svikram 	INJECT_ERROR1("Z_UMOUNT_TOP_INVALID_STATE", mnted = ZFS_MNT_ERROR);
5539eb2bd662Svikram 	switch (mnted) {
5540eb2bd662Svikram 	case LEGACY_ALREADY:
5541eb2bd662Svikram 	case ZFS_ALREADY:
5542eb2bd662Svikram 		/* nothing to do */
5543eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_ALREADY_NOP, fcn, pool,
5544eb2bd662Svikram 		    mntpt ? mntpt : "NULL"));
5545eb2bd662Svikram 		free(mntpt);
5546eb2bd662Svikram 		return (BAM_SUCCESS);
5547eb2bd662Svikram 	case LEGACY_MOUNTED:
5548eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
5549eb2bd662Svikram 		    "/sbin/umount %s", pool);
5550eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
5551eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_LEGACY_UMOUNT_FAIL", ret = 1);
5552eb2bd662Svikram 		if (ret != 0) {
5553eb2bd662Svikram 			bam_error(UMOUNT_FAILED, pool);
5554eb2bd662Svikram 			free(mntpt);
5555eb2bd662Svikram 			return (BAM_ERROR);
5556eb2bd662Svikram 		}
5557eb2bd662Svikram 		if (mntpt)
5558eb2bd662Svikram 			(void) rmdir(mntpt);
5559eb2bd662Svikram 		free(mntpt);
5560eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_LEGACY, fcn, pool));
5561eb2bd662Svikram 		return (BAM_SUCCESS);
5562eb2bd662Svikram 	case ZFS_MOUNTED:
5563eb2bd662Svikram 		free(mntpt);
5564eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
5565eb2bd662Svikram 		    "/sbin/zfs unmount %s", pool);
5566eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
5567eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_NONLEG_UMOUNT_FAIL", ret = 1);
5568eb2bd662Svikram 		if (ret != 0) {
5569eb2bd662Svikram 			bam_error(UMOUNT_FAILED, pool);
5570eb2bd662Svikram 			return (BAM_ERROR);
5571eb2bd662Svikram 		}
5572eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_NONLEG, fcn, pool));
5573eb2bd662Svikram 		return (BAM_SUCCESS);
5574eb2bd662Svikram 	default:
5575eb2bd662Svikram 		bam_error(INT_BAD_MNTSTATE, pool);
5576eb2bd662Svikram 		return (BAM_ERROR);
5577eb2bd662Svikram 	}
5578eb2bd662Svikram 	/*NOTREACHED*/
5579eb2bd662Svikram }
5580eb2bd662Svikram 
5581eb2bd662Svikram /*
5582eb2bd662Svikram  * For ZFS, osdev can be one of two forms
5583eb2bd662Svikram  * It can be a "special" file as seen in mnttab: rpool/ROOT/szboot_0402
5584eb2bd662Svikram  * It can be a /dev/[r]dsk special file. We handle both instances
5585eb2bd662Svikram  */
5586eb2bd662Svikram static char *
5587eb2bd662Svikram get_pool(char *osdev)
5588eb2bd662Svikram {
5589eb2bd662Svikram 	char		cmd[PATH_MAX];
5590eb2bd662Svikram 	char		buf[PATH_MAX];
5591eb2bd662Svikram 	filelist_t	flist = {0};
5592eb2bd662Svikram 	char		*pool;
5593eb2bd662Svikram 	char		*cp;
5594eb2bd662Svikram 	char		*slash;
5595eb2bd662Svikram 	int		ret;
5596eb2bd662Svikram 	const char	*fcn = "get_pool()";
5597eb2bd662Svikram 
5598eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_OSDEV", osdev = NULL);
5599eb2bd662Svikram 	if (osdev == NULL) {
5600eb2bd662Svikram 		bam_error(GET_POOL_OSDEV_NULL);
5601eb2bd662Svikram 		return (NULL);
5602eb2bd662Svikram 	}
5603eb2bd662Svikram 
5604eb2bd662Svikram 	BAM_DPRINTF((D_GET_POOL_OSDEV, fcn, osdev));
5605eb2bd662Svikram 
5606eb2bd662Svikram 	if (osdev[0] != '/') {
5607eb2bd662Svikram 		(void) strlcpy(buf, osdev, sizeof (buf));
5608eb2bd662Svikram 		slash = strchr(buf, '/');
5609eb2bd662Svikram 		if (slash)
5610eb2bd662Svikram 			*slash = '\0';
5611eb2bd662Svikram 		pool = s_strdup(buf);
5612eb2bd662Svikram 		BAM_DPRINTF((D_GET_POOL_RET, fcn, pool));
5613eb2bd662Svikram 		return (pool);
5614eb2bd662Svikram 	} else if (strncmp(osdev, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
5615eb2bd662Svikram 	    strncmp(osdev, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0) {
5616eb2bd662Svikram 		bam_error(GET_POOL_BAD_OSDEV, osdev);
5617eb2bd662Svikram 		return (NULL);
5618eb2bd662Svikram 	}
5619eb2bd662Svikram 
5620d5cb36d3SJan Setje-Eilers 	/*
5621d5cb36d3SJan Setje-Eilers 	 * Call the zfs fstyp directly since this is a zpool. This avoids
5622d5cb36d3SJan Setje-Eilers 	 * potential pcfs conflicts if the first block wasn't cleared.
5623d5cb36d3SJan Setje-Eilers 	 */
5624eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5625d5cb36d3SJan Setje-Eilers 	    "/usr/lib/fs/zfs/fstyp -a %s 2>/dev/null | /bin/grep '^name:'",
5626eb2bd662Svikram 	    osdev);
5627eb2bd662Svikram 
5628eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5629eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP", ret = 1);
5630eb2bd662Svikram 	if (ret != 0) {
5631eb2bd662Svikram 		bam_error(FSTYP_A_FAILED, osdev);
5632eb2bd662Svikram 		return (NULL);
5633eb2bd662Svikram 	}
5634eb2bd662Svikram 
5635eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_OUT", flist.head = NULL);
5636eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5637eb2bd662Svikram 		bam_error(NULL_FSTYP_A, osdev);
5638eb2bd662Svikram 		filelist_free(&flist);
5639eb2bd662Svikram 		return (NULL);
5640eb2bd662Svikram 	}
5641eb2bd662Svikram 
5642eb2bd662Svikram 	(void) strtok(flist.head->line, "'");
5643eb2bd662Svikram 	cp = strtok(NULL, "'");
5644eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_STRTOK", cp = NULL);
5645eb2bd662Svikram 	if (cp == NULL) {
5646eb2bd662Svikram 		bam_error(BAD_FSTYP_A, osdev);
5647eb2bd662Svikram 		filelist_free(&flist);
5648eb2bd662Svikram 		return (NULL);
5649eb2bd662Svikram 	}
5650eb2bd662Svikram 
5651eb2bd662Svikram 	pool = s_strdup(cp);
5652eb2bd662Svikram 
5653eb2bd662Svikram 	filelist_free(&flist);
5654eb2bd662Svikram 
5655eb2bd662Svikram 	BAM_DPRINTF((D_GET_POOL_RET, fcn, pool));
5656eb2bd662Svikram 
5657eb2bd662Svikram 	return (pool);
5658eb2bd662Svikram }
5659eb2bd662Svikram 
5660eb2bd662Svikram static char *
5661eb2bd662Svikram find_zfs_existing(char *osdev)
5662eb2bd662Svikram {
5663eb2bd662Svikram 	char		*pool;
5664eb2bd662Svikram 	zfs_mnted_t	mnted;
5665eb2bd662Svikram 	char		*mntpt;
5666eb2bd662Svikram 	char		*sign;
5667eb2bd662Svikram 	const char	*fcn = "find_zfs_existing()";
5668eb2bd662Svikram 
5669eb2bd662Svikram 	pool = get_pool(osdev);
5670eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_POOL", pool = NULL);
5671eb2bd662Svikram 	if (pool == NULL) {
5672eb2bd662Svikram 		bam_error(ZFS_GET_POOL_FAILED, osdev);
5673eb2bd662Svikram 		return (NULL);
5674eb2bd662Svikram 	}
5675eb2bd662Svikram 
5676eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
5677eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_MOUNT_TOP", mntpt = NULL);
5678eb2bd662Svikram 	if (mntpt == NULL) {
5679eb2bd662Svikram 		bam_error(ZFS_MOUNT_TOP_DATASET_FAILED, pool);
5680eb2bd662Svikram 		free(pool);
5681eb2bd662Svikram 		return (NULL);
5682eb2bd662Svikram 	}
5683eb2bd662Svikram 
5684eb2bd662Svikram 	sign = find_primary_common(mntpt, "zfs");
5685eb2bd662Svikram 	if (sign == NULL) {
5686eb2bd662Svikram 		sign = find_backup_common(mntpt, "zfs");
5687eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL"));
5688eb2bd662Svikram 	} else {
5689eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign));
5690eb2bd662Svikram 	}
5691eb2bd662Svikram 
5692eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
5693eb2bd662Svikram 
5694eb2bd662Svikram 	free(pool);
5695eb2bd662Svikram 
5696eb2bd662Svikram 	return (sign);
5697eb2bd662Svikram }
5698eb2bd662Svikram 
5699eb2bd662Svikram static char *
5700eb2bd662Svikram find_existing_sign(char *osroot, char *osdev, char *fstype)
5701eb2bd662Svikram {
5702eb2bd662Svikram 	const char		*fcn = "find_existing_sign()";
5703eb2bd662Svikram 
5704eb2bd662Svikram 	INJECT_ERROR1("FIND_EXIST_NOTSUP_FS", fstype = "foofs");
5705eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
5706eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_UFS_EXIST_SIGN, fcn));
5707eb2bd662Svikram 		return (find_ufs_existing(osroot));
5708eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
5709eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_ZFS_EXIST_SIGN, fcn));
5710eb2bd662Svikram 		return (find_zfs_existing(osdev));
5711eb2bd662Svikram 	} else {
5712eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
5713eb2bd662Svikram 		return (NULL);
5714eb2bd662Svikram 	}
5715eb2bd662Svikram }
5716eb2bd662Svikram 
5717eb2bd662Svikram #define	MH_HASH_SZ	16
5718eb2bd662Svikram 
5719eb2bd662Svikram typedef enum {
5720eb2bd662Svikram 	MH_ERROR = -1,
5721eb2bd662Svikram 	MH_NOMATCH,
5722eb2bd662Svikram 	MH_MATCH
5723eb2bd662Svikram } mh_search_t;
5724eb2bd662Svikram 
5725eb2bd662Svikram typedef struct mcache {
5726eb2bd662Svikram 	char	*mc_special;
5727eb2bd662Svikram 	char	*mc_mntpt;
5728eb2bd662Svikram 	char	*mc_fstype;
5729eb2bd662Svikram 	struct mcache *mc_next;
5730eb2bd662Svikram } mcache_t;
5731eb2bd662Svikram 
5732eb2bd662Svikram typedef struct mhash {
5733eb2bd662Svikram 	mcache_t *mh_hash[MH_HASH_SZ];
5734eb2bd662Svikram } mhash_t;
5735eb2bd662Svikram 
5736eb2bd662Svikram static int
5737eb2bd662Svikram mhash_fcn(char *key)
5738eb2bd662Svikram {
5739eb2bd662Svikram 	int		i;
5740eb2bd662Svikram 	uint64_t	sum = 0;
5741eb2bd662Svikram 
5742eb2bd662Svikram 	for (i = 0; key[i] != '\0'; i++) {
5743eb2bd662Svikram 		sum += (uchar_t)key[i];
5744eb2bd662Svikram 	}
5745eb2bd662Svikram 
5746eb2bd662Svikram 	sum %= MH_HASH_SZ;
5747eb2bd662Svikram 
5748eb2bd662Svikram 	assert(sum < MH_HASH_SZ);
5749eb2bd662Svikram 
5750eb2bd662Svikram 	return (sum);
5751eb2bd662Svikram }
5752eb2bd662Svikram 
5753eb2bd662Svikram static mhash_t *
5754eb2bd662Svikram cache_mnttab(void)
5755eb2bd662Svikram {
5756eb2bd662Svikram 	FILE		*mfp;
5757eb2bd662Svikram 	struct extmnttab mnt;
5758eb2bd662Svikram 	mcache_t	*mcp;
5759eb2bd662Svikram 	mhash_t		*mhp;
5760eb2bd662Svikram 	char		*ctds;
5761eb2bd662Svikram 	int		idx;
5762eb2bd662Svikram 	int		error;
5763eb2bd662Svikram 	char		*special_dup;
5764eb2bd662Svikram 	const char	*fcn = "cache_mnttab()";
5765eb2bd662Svikram 
5766eb2bd662Svikram 	mfp = fopen(MNTTAB, "r");
5767eb2bd662Svikram 	error = errno;
5768eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_MNTTAB_ERR", mfp = NULL);
5769eb2bd662Svikram 	if (mfp == NULL) {
5770eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
5771eb2bd662Svikram 		return (NULL);
5772eb2bd662Svikram 	}
5773eb2bd662Svikram 
5774eb2bd662Svikram 	mhp = s_calloc(1, sizeof (mhash_t));
5775eb2bd662Svikram 
5776eb2bd662Svikram 	resetmnttab(mfp);
5777eb2bd662Svikram 
5778eb2bd662Svikram 	while (getextmntent(mfp, &mnt, sizeof (mnt)) == 0) {
5779eb2bd662Svikram 		/* only cache ufs */
5780eb2bd662Svikram 		if (strcmp(mnt.mnt_fstype, "ufs") != 0)
5781eb2bd662Svikram 			continue;
5782eb2bd662Svikram 
5783eb2bd662Svikram 		/* basename() modifies its arg, so dup it */
5784eb2bd662Svikram 		special_dup = s_strdup(mnt.mnt_special);
5785eb2bd662Svikram 		ctds = basename(special_dup);
5786eb2bd662Svikram 
5787eb2bd662Svikram 		mcp = s_calloc(1, sizeof (mcache_t));
5788eb2bd662Svikram 		mcp->mc_special = s_strdup(ctds);
5789eb2bd662Svikram 		mcp->mc_mntpt = s_strdup(mnt.mnt_mountp);
5790eb2bd662Svikram 		mcp->mc_fstype = s_strdup(mnt.mnt_fstype);
5791eb2bd662Svikram 		BAM_DPRINTF((D_CACHE_MNTS, fcn, ctds,
5792eb2bd662Svikram 		    mnt.mnt_mountp, mnt.mnt_fstype));
5793eb2bd662Svikram 		idx = mhash_fcn(ctds);
5794eb2bd662Svikram 		mcp->mc_next = mhp->mh_hash[idx];
5795eb2bd662Svikram 		mhp->mh_hash[idx] = mcp;
5796eb2bd662Svikram 		free(special_dup);
5797eb2bd662Svikram 	}
5798eb2bd662Svikram 
5799eb2bd662Svikram 	(void) fclose(mfp);
5800eb2bd662Svikram 
5801eb2bd662Svikram 	return (mhp);
5802eb2bd662Svikram }
5803eb2bd662Svikram 
5804eb2bd662Svikram static void
5805eb2bd662Svikram free_mnttab(mhash_t *mhp)
5806eb2bd662Svikram {
5807eb2bd662Svikram 	mcache_t	*mcp;
5808eb2bd662Svikram 	int		i;
5809eb2bd662Svikram 
5810eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
5811eb2bd662Svikram 		/*LINTED*/
5812eb2bd662Svikram 		while (mcp = mhp->mh_hash[i]) {
5813eb2bd662Svikram 			mhp->mh_hash[i] = mcp->mc_next;
5814eb2bd662Svikram 			free(mcp->mc_special);
5815eb2bd662Svikram 			free(mcp->mc_mntpt);
5816eb2bd662Svikram 			free(mcp->mc_fstype);
5817eb2bd662Svikram 			free(mcp);
5818eb2bd662Svikram 		}
5819eb2bd662Svikram 	}
5820eb2bd662Svikram 
5821eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
5822eb2bd662Svikram 		assert(mhp->mh_hash[i] == NULL);
5823eb2bd662Svikram 	}
5824eb2bd662Svikram 	free(mhp);
5825eb2bd662Svikram }
5826eb2bd662Svikram 
5827eb2bd662Svikram static mh_search_t
5828eb2bd662Svikram search_hash(mhash_t *mhp, char *special, char **mntpt)
5829eb2bd662Svikram {
5830eb2bd662Svikram 	int		idx;
5831eb2bd662Svikram 	mcache_t	*mcp;
5832eb2bd662Svikram 	const char 	*fcn = "search_hash()";
5833eb2bd662Svikram 
5834eb2bd662Svikram 	assert(mntpt);
5835eb2bd662Svikram 
5836eb2bd662Svikram 	*mntpt = NULL;
5837eb2bd662Svikram 
5838eb2bd662Svikram 	INJECT_ERROR1("SEARCH_HASH_FULL_PATH", special = "/foo");
5839eb2bd662Svikram 	if (strchr(special, '/')) {
5840eb2bd662Svikram 		bam_error(INVALID_MHASH_KEY, special);
5841eb2bd662Svikram 		return (MH_ERROR);
5842eb2bd662Svikram 	}
5843eb2bd662Svikram 
5844eb2bd662Svikram 	idx = mhash_fcn(special);
5845eb2bd662Svikram 
5846eb2bd662Svikram 	for (mcp = mhp->mh_hash[idx]; mcp; mcp = mcp->mc_next) {
5847eb2bd662Svikram 		if (strcmp(mcp->mc_special, special) == 0)
5848eb2bd662Svikram 			break;
5849eb2bd662Svikram 	}
5850eb2bd662Svikram 
5851eb2bd662Svikram 	if (mcp == NULL) {
5852eb2bd662Svikram 		BAM_DPRINTF((D_MNTTAB_HASH_NOMATCH, fcn, special));
5853eb2bd662Svikram 		return (MH_NOMATCH);
5854eb2bd662Svikram 	}
5855eb2bd662Svikram 
5856eb2bd662Svikram 	assert(strcmp(mcp->mc_fstype, "ufs") == 0);
5857eb2bd662Svikram 	*mntpt = mcp->mc_mntpt;
5858eb2bd662Svikram 	BAM_DPRINTF((D_MNTTAB_HASH_MATCH, fcn, special));
5859eb2bd662Svikram 	return (MH_MATCH);
5860eb2bd662Svikram }
5861eb2bd662Svikram 
5862eb2bd662Svikram static int
5863eb2bd662Svikram check_add_ufs_sign_to_list(FILE *tfp, char *mntpt)
5864eb2bd662Svikram {
5865eb2bd662Svikram 	char		*sign;
5866eb2bd662Svikram 	char		*signline;
5867eb2bd662Svikram 	char		signbuf[MAXNAMELEN];
5868eb2bd662Svikram 	int		len;
5869eb2bd662Svikram 	int		error;
5870eb2bd662Svikram 	const char	*fcn = "check_add_ufs_sign_to_list()";
5871eb2bd662Svikram 
5872eb2bd662Svikram 	/* safe to specify NULL as "osdev" arg for UFS */
5873eb2bd662Svikram 	sign = find_existing_sign(mntpt, NULL, "ufs");
5874eb2bd662Svikram 	if (sign == NULL) {
5875eb2bd662Svikram 		/* No existing signature, nothing to add to list */
5876eb2bd662Svikram 		BAM_DPRINTF((D_NO_SIGN_TO_LIST, fcn, mntpt));
5877eb2bd662Svikram 		return (0);
5878eb2bd662Svikram 	}
5879eb2bd662Svikram 
5880eb2bd662Svikram 	(void) snprintf(signbuf, sizeof (signbuf), "%s\n", sign);
5881eb2bd662Svikram 	signline = signbuf;
5882eb2bd662Svikram 
5883eb2bd662Svikram 	INJECT_ERROR1("UFS_MNTPT_SIGN_NOTUFS", signline = "pool_rpool10\n");
5884eb2bd662Svikram 	if (strncmp(signline, GRUBSIGN_UFS_PREFIX,
5885eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX))) {
5886eb2bd662Svikram 		bam_error(INVALID_UFS_SIGNATURE, sign);
5887eb2bd662Svikram 		free(sign);
5888eb2bd662Svikram 		/* ignore invalid signatures */
5889eb2bd662Svikram 		return (0);
5890eb2bd662Svikram 	}
5891eb2bd662Svikram 
5892eb2bd662Svikram 	len = fputs(signline, tfp);
5893eb2bd662Svikram 	error = errno;
5894eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_PUTS_ERROR", len = 0);
5895eb2bd662Svikram 	if (len != strlen(signline)) {
5896eb2bd662Svikram 		bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error));
5897eb2bd662Svikram 		free(sign);
5898eb2bd662Svikram 		return (-1);
5899eb2bd662Svikram 	}
5900eb2bd662Svikram 
5901eb2bd662Svikram 	free(sign);
5902eb2bd662Svikram 
5903eb2bd662Svikram 	BAM_DPRINTF((D_SIGN_LIST_PUTS_DONE, fcn, mntpt));
5904eb2bd662Svikram 	return (0);
5905eb2bd662Svikram }
5906eb2bd662Svikram 
5907eb2bd662Svikram /*
5908eb2bd662Svikram  * slice is a basename not a full pathname
5909eb2bd662Svikram  */
5910eb2bd662Svikram static int
5911eb2bd662Svikram process_slice_common(char *slice, FILE *tfp, mhash_t *mhp, char *tmpmnt)
5912eb2bd662Svikram {
5913eb2bd662Svikram 	int		ret;
5914eb2bd662Svikram 	char		cmd[PATH_MAX];
5915eb2bd662Svikram 	char		path[PATH_MAX];
5916eb2bd662Svikram 	struct stat	sbuf;
5917eb2bd662Svikram 	char		*mntpt;
5918eb2bd662Svikram 	filelist_t	flist = {0};
5919eb2bd662Svikram 	char		*fstype;
5920eb2bd662Svikram 	char		blkslice[PATH_MAX];
5921eb2bd662Svikram 	const char	*fcn = "process_slice_common()";
5922eb2bd662Svikram 
5923eb2bd662Svikram 
5924eb2bd662Svikram 	ret = search_hash(mhp, slice, &mntpt);
5925eb2bd662Svikram 	switch (ret) {
5926eb2bd662Svikram 		case MH_MATCH:
5927eb2bd662Svikram 			if (check_add_ufs_sign_to_list(tfp, mntpt) == -1)
5928eb2bd662Svikram 				return (-1);
5929eb2bd662Svikram 			else
5930eb2bd662Svikram 				return (0);
5931eb2bd662Svikram 		case MH_NOMATCH:
5932eb2bd662Svikram 			break;
5933eb2bd662Svikram 		case MH_ERROR:
5934eb2bd662Svikram 		default:
5935eb2bd662Svikram 			return (-1);
5936eb2bd662Svikram 	}
5937eb2bd662Svikram 
5938eb2bd662Svikram 	(void) snprintf(path, sizeof (path), "/dev/rdsk/%s", slice);
5939eb2bd662Svikram 	if (stat(path, &sbuf) == -1) {
5940eb2bd662Svikram 		BAM_DPRINTF((D_SLICE_ENOENT, fcn, path));
5941eb2bd662Svikram 		return (0);
5942eb2bd662Svikram 	}
5943eb2bd662Svikram 
5944d5cb36d3SJan Setje-Eilers 	/* Check if ufs. Call ufs fstyp directly to avoid pcfs conflicts. */
5945eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5946d5cb36d3SJan Setje-Eilers 	    "/usr/lib/fs/ufs/fstyp /dev/rdsk/%s 2>/dev/null",
5947eb2bd662Svikram 	    slice);
5948eb2bd662Svikram 
5949eb2bd662Svikram 	if (exec_cmd(cmd, &flist) != 0) {
5950eb2bd662Svikram 		if (bam_verbose)
5951eb2bd662Svikram 			bam_print(FSTYP_FAILED, slice);
5952eb2bd662Svikram 		return (0);
5953eb2bd662Svikram 	}
5954eb2bd662Svikram 
5955eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5956eb2bd662Svikram 		if (bam_verbose)
5957eb2bd662Svikram 			bam_print(FSTYP_BAD, slice);
5958eb2bd662Svikram 		filelist_free(&flist);
5959eb2bd662Svikram 		return (0);
5960eb2bd662Svikram 	}
5961eb2bd662Svikram 
5962eb2bd662Svikram 	fstype = strtok(flist.head->line, " \t\n");
5963eb2bd662Svikram 	if (fstype == NULL || strcmp(fstype, "ufs") != 0) {
5964eb2bd662Svikram 		if (bam_verbose)
5965eb2bd662Svikram 			bam_print(NOT_UFS_SLICE, slice, fstype);
5966eb2bd662Svikram 		filelist_free(&flist);
5967eb2bd662Svikram 		return (0);
5968eb2bd662Svikram 	}
5969eb2bd662Svikram 
5970eb2bd662Svikram 	filelist_free(&flist);
5971eb2bd662Svikram 
5972eb2bd662Svikram 	/*
5973eb2bd662Svikram 	 * Since we are mounting the filesystem read-only, the
5974eb2bd662Svikram 	 * the last mount field of the superblock is unchanged
5975eb2bd662Svikram 	 * and does not need to be fixed up post-mount;
5976eb2bd662Svikram 	 */
5977eb2bd662Svikram 
5978eb2bd662Svikram 	(void) snprintf(blkslice, sizeof (blkslice), "/dev/dsk/%s",
5979eb2bd662Svikram 	    slice);
5980eb2bd662Svikram 
5981eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5982eb2bd662Svikram 	    "/usr/sbin/mount -F ufs -o ro %s %s "
5983eb2bd662Svikram 	    "> /dev/null 2>&1", blkslice, tmpmnt);
5984eb2bd662Svikram 
5985eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
5986eb2bd662Svikram 		if (bam_verbose)
5987eb2bd662Svikram 			bam_print(MOUNT_FAILED, blkslice, "ufs");
5988eb2bd662Svikram 		return (0);
5989eb2bd662Svikram 	}
5990eb2bd662Svikram 
5991eb2bd662Svikram 	ret = check_add_ufs_sign_to_list(tfp, tmpmnt);
5992eb2bd662Svikram 
5993eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5994eb2bd662Svikram 	    "/usr/sbin/umount -f %s > /dev/null 2>&1",
5995eb2bd662Svikram 	    tmpmnt);
5996eb2bd662Svikram 
5997eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
5998eb2bd662Svikram 		bam_print(UMOUNT_FAILED, slice);
5999eb2bd662Svikram 		return (0);
6000eb2bd662Svikram 	}
6001eb2bd662Svikram 
6002eb2bd662Svikram 	return (ret);
6003eb2bd662Svikram }
6004eb2bd662Svikram 
6005eb2bd662Svikram static int
6006eb2bd662Svikram process_vtoc_slices(
6007eb2bd662Svikram 	char *s0,
6008eb2bd662Svikram 	struct vtoc *vtoc,
6009eb2bd662Svikram 	FILE *tfp,
6010eb2bd662Svikram 	mhash_t *mhp,
6011eb2bd662Svikram 	char *tmpmnt)
6012eb2bd662Svikram {
6013eb2bd662Svikram 	int		idx;
6014eb2bd662Svikram 	char		slice[PATH_MAX];
6015eb2bd662Svikram 	size_t		len;
6016eb2bd662Svikram 	char		*cp;
6017eb2bd662Svikram 	const char	*fcn = "process_vtoc_slices()";
6018eb2bd662Svikram 
6019eb2bd662Svikram 	len = strlen(s0);
6020eb2bd662Svikram 
6021eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
6022eb2bd662Svikram 
6023eb2bd662Svikram 	s0[len - 1] = '\0';
6024eb2bd662Svikram 
6025eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
6026eb2bd662Svikram 
6027eb2bd662Svikram 	s0[len - 1] = '0';
6028eb2bd662Svikram 
6029eb2bd662Svikram 	cp = slice + len - 1;
6030eb2bd662Svikram 
6031eb2bd662Svikram 	for (idx = 0; idx < vtoc->v_nparts; idx++) {
6032eb2bd662Svikram 
6033eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
6034eb2bd662Svikram 
6035eb2bd662Svikram 		if (vtoc->v_part[idx].p_size == 0) {
6036eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_SIZE_ZERO, fcn, slice));
6037eb2bd662Svikram 			continue;
6038eb2bd662Svikram 		}
6039eb2bd662Svikram 
6040eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
6041eb2bd662Svikram 		switch (vtoc->v_part[idx].p_tag) {
6042eb2bd662Svikram 		case V_SWAP:
6043eb2bd662Svikram 		case V_USR:
6044eb2bd662Svikram 		case V_BACKUP:
6045eb2bd662Svikram 		case V_VAR:
6046eb2bd662Svikram 		case V_HOME:
6047eb2bd662Svikram 		case V_ALTSCTR:
6048eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOT_ROOT_TAG, fcn, slice));
6049eb2bd662Svikram 			continue;
6050eb2bd662Svikram 		default:
6051eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_ROOT_TAG, fcn, slice));
6052eb2bd662Svikram 			break;
6053eb2bd662Svikram 		}
6054eb2bd662Svikram 
6055eb2bd662Svikram 		/* skip unmountable and readonly slices */
6056eb2bd662Svikram 		switch (vtoc->v_part[idx].p_flag) {
6057eb2bd662Svikram 		case V_UNMNT:
6058eb2bd662Svikram 		case V_RONLY:
6059eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOT_RDWR_FLAG, fcn, slice));
6060eb2bd662Svikram 			continue;
6061eb2bd662Svikram 		default:
6062eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_RDWR_FLAG, fcn, slice));
6063eb2bd662Svikram 			break;
6064eb2bd662Svikram 		}
6065eb2bd662Svikram 
6066eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
6067eb2bd662Svikram 			return (-1);
6068eb2bd662Svikram 		}
6069eb2bd662Svikram 	}
6070eb2bd662Svikram 
6071eb2bd662Svikram 	return (0);
6072eb2bd662Svikram }
6073eb2bd662Svikram 
6074eb2bd662Svikram static int
6075eb2bd662Svikram process_efi_slices(
6076eb2bd662Svikram 	char *s0,
6077eb2bd662Svikram 	struct dk_gpt *efi,
6078eb2bd662Svikram 	FILE *tfp,
6079eb2bd662Svikram 	mhash_t *mhp,
6080eb2bd662Svikram 	char *tmpmnt)
6081eb2bd662Svikram {
6082eb2bd662Svikram 	int		idx;
6083eb2bd662Svikram 	char		slice[PATH_MAX];
6084eb2bd662Svikram 	size_t		len;
6085eb2bd662Svikram 	char		*cp;
6086eb2bd662Svikram 	const char	*fcn = "process_efi_slices()";
6087eb2bd662Svikram 
6088eb2bd662Svikram 	len = strlen(s0);
6089eb2bd662Svikram 
6090eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
6091eb2bd662Svikram 
6092eb2bd662Svikram 	s0[len - 1] = '\0';
6093eb2bd662Svikram 
6094eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
6095eb2bd662Svikram 
6096eb2bd662Svikram 	s0[len - 1] = '0';
6097eb2bd662Svikram 
6098eb2bd662Svikram 	cp = slice + len - 1;
6099eb2bd662Svikram 
6100eb2bd662Svikram 	for (idx = 0; idx < efi->efi_nparts; idx++) {
6101eb2bd662Svikram 
6102eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
6103eb2bd662Svikram 
6104eb2bd662Svikram 		if (efi->efi_parts[idx].p_size == 0) {
6105eb2bd662Svikram 			BAM_DPRINTF((D_EFI_SIZE_ZERO, fcn, slice));
6106eb2bd662Svikram 			continue;
6107eb2bd662Svikram 		}
6108eb2bd662Svikram 
6109eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
6110eb2bd662Svikram 		switch (efi->efi_parts[idx].p_tag) {
6111eb2bd662Svikram 		case V_SWAP:
6112eb2bd662Svikram 		case V_USR:
6113eb2bd662Svikram 		case V_BACKUP:
6114eb2bd662Svikram 		case V_VAR:
6115eb2bd662Svikram 		case V_HOME:
6116eb2bd662Svikram 		case V_ALTSCTR:
6117eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOT_ROOT_TAG, fcn, slice));
6118eb2bd662Svikram 			continue;
6119eb2bd662Svikram 		default:
6120eb2bd662Svikram 			BAM_DPRINTF((D_EFI_ROOT_TAG, fcn, slice));
6121eb2bd662Svikram 			break;
6122eb2bd662Svikram 		}
6123eb2bd662Svikram 
6124eb2bd662Svikram 		/* skip unmountable and readonly slices */
6125eb2bd662Svikram 		switch (efi->efi_parts[idx].p_flag) {
6126eb2bd662Svikram 		case V_UNMNT:
6127eb2bd662Svikram 		case V_RONLY:
6128eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOT_RDWR_FLAG, fcn, slice));
6129eb2bd662Svikram 			continue;
6130eb2bd662Svikram 		default:
6131eb2bd662Svikram 			BAM_DPRINTF((D_EFI_RDWR_FLAG, fcn, slice));
6132eb2bd662Svikram 			break;
6133eb2bd662Svikram 		}
6134eb2bd662Svikram 
6135eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
6136eb2bd662Svikram 			return (-1);
6137eb2bd662Svikram 		}
6138eb2bd662Svikram 	}
6139eb2bd662Svikram 
6140eb2bd662Svikram 	return (0);
6141eb2bd662Svikram }
6142eb2bd662Svikram 
6143eb2bd662Svikram /*
6144eb2bd662Svikram  * s0 is a basename not a full path
6145eb2bd662Svikram  */
6146eb2bd662Svikram static int
6147eb2bd662Svikram process_slice0(char *s0, FILE *tfp, mhash_t *mhp, char *tmpmnt)
6148eb2bd662Svikram {
6149eb2bd662Svikram 	struct vtoc		vtoc;
6150eb2bd662Svikram 	struct dk_gpt		*efi;
6151eb2bd662Svikram 	char			s0path[PATH_MAX];
6152eb2bd662Svikram 	struct stat		sbuf;
6153eb2bd662Svikram 	int			e_flag;
6154eb2bd662Svikram 	int			v_flag;
6155eb2bd662Svikram 	int			retval;
6156eb2bd662Svikram 	int			err;
6157eb2bd662Svikram 	int			fd;
6158eb2bd662Svikram 	const char		*fcn = "process_slice0()";
6159eb2bd662Svikram 
6160eb2bd662Svikram 	(void) snprintf(s0path, sizeof (s0path), "/dev/rdsk/%s", s0);
6161eb2bd662Svikram 
6162eb2bd662Svikram 	if (stat(s0path, &sbuf) == -1) {
6163eb2bd662Svikram 		BAM_DPRINTF((D_SLICE0_ENOENT, fcn, s0path));
6164eb2bd662Svikram 		return (0);
6165eb2bd662Svikram 	}
6166eb2bd662Svikram 
6167eb2bd662Svikram 	fd = open(s0path, O_NONBLOCK|O_RDONLY);
6168eb2bd662Svikram 	if (fd == -1) {
6169eb2bd662Svikram 		bam_error(OPEN_FAIL, s0path, strerror(errno));
6170eb2bd662Svikram 		return (0);
6171eb2bd662Svikram 	}
6172eb2bd662Svikram 
6173eb2bd662Svikram 	e_flag = v_flag = 0;
6174eb2bd662Svikram 	retval = ((err = read_vtoc(fd, &vtoc)) >= 0) ? 0 : err;
6175eb2bd662Svikram 	switch (retval) {
6176eb2bd662Svikram 		case VT_EIO:
6177eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_READ_FAIL, fcn, s0path));
6178eb2bd662Svikram 			break;
6179eb2bd662Svikram 		case VT_EINVAL:
6180eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_INVALID, fcn, s0path));
6181eb2bd662Svikram 			break;
6182eb2bd662Svikram 		case VT_ERROR:
6183eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_UNKNOWN_ERR, fcn, s0path));
6184eb2bd662Svikram 			break;
6185eb2bd662Svikram 		case VT_ENOTSUP:
6186eb2bd662Svikram 			e_flag = 1;
6187eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOTSUP, fcn, s0path));
6188eb2bd662Svikram 			break;
6189eb2bd662Svikram 		case 0:
6190eb2bd662Svikram 			v_flag = 1;
6191eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_READ_SUCCESS, fcn, s0path));
6192eb2bd662Svikram 			break;
6193eb2bd662Svikram 		default:
6194eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_UNKNOWN_RETCODE, fcn, s0path));
6195eb2bd662Svikram 			break;
6196eb2bd662Svikram 	}
6197eb2bd662Svikram 
6198eb2bd662Svikram 
6199eb2bd662Svikram 	if (e_flag) {
6200eb2bd662Svikram 		e_flag = 0;
6201eb2bd662Svikram 		retval = ((err = efi_alloc_and_read(fd, &efi)) >= 0) ? 0 : err;
6202eb2bd662Svikram 		switch (retval) {
6203eb2bd662Svikram 		case VT_EIO:
6204eb2bd662Svikram 			BAM_DPRINTF((D_EFI_READ_FAIL, fcn, s0path));
6205eb2bd662Svikram 			break;
6206eb2bd662Svikram 		case VT_EINVAL:
6207eb2bd662Svikram 			BAM_DPRINTF((D_EFI_INVALID, fcn, s0path));
6208eb2bd662Svikram 			break;
6209eb2bd662Svikram 		case VT_ERROR:
6210eb2bd662Svikram 			BAM_DPRINTF((D_EFI_UNKNOWN_ERR, fcn, s0path));
6211eb2bd662Svikram 			break;
6212eb2bd662Svikram 		case VT_ENOTSUP:
6213eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOTSUP, fcn, s0path));
6214eb2bd662Svikram 			break;
6215eb2bd662Svikram 		case 0:
6216eb2bd662Svikram 			e_flag = 1;
6217eb2bd662Svikram 			BAM_DPRINTF((D_EFI_READ_SUCCESS, fcn, s0path));
6218eb2bd662Svikram 			break;
6219eb2bd662Svikram 		default:
6220eb2bd662Svikram 			BAM_DPRINTF((D_EFI_UNKNOWN_RETCODE, fcn, s0path));
6221eb2bd662Svikram 			break;
6222eb2bd662Svikram 		}
6223eb2bd662Svikram 	}
6224eb2bd662Svikram 
6225eb2bd662Svikram 	(void) close(fd);
6226eb2bd662Svikram 
6227eb2bd662Svikram 	if (v_flag) {
6228eb2bd662Svikram 		retval = process_vtoc_slices(s0,
6229eb2bd662Svikram 		    &vtoc, tfp, mhp, tmpmnt);
6230eb2bd662Svikram 	} else if (e_flag) {
6231eb2bd662Svikram 		retval = process_efi_slices(s0,
6232eb2bd662Svikram 		    efi, tfp, mhp, tmpmnt);
6233eb2bd662Svikram 	} else {
6234eb2bd662Svikram 		BAM_DPRINTF((D_NOT_VTOC_OR_EFI, fcn, s0path));
6235eb2bd662Svikram 		return (0);
6236eb2bd662Svikram 	}
6237eb2bd662Svikram 
6238eb2bd662Svikram 	return (retval);
6239eb2bd662Svikram }
6240eb2bd662Svikram 
6241eb2bd662Svikram /*
6242eb2bd662Svikram  * Find and create a list of all existing UFS boot signatures
6243eb2bd662Svikram  */
6244eb2bd662Svikram static int
6245eb2bd662Svikram FindAllUfsSignatures(void)
6246eb2bd662Svikram {
6247eb2bd662Svikram 	mhash_t		*mnttab_hash;
6248eb2bd662Svikram 	DIR		*dirp = NULL;
6249eb2bd662Svikram 	struct dirent	*dp;
6250eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
6251eb2bd662Svikram 	char		cmd[PATH_MAX];
6252eb2bd662Svikram 	struct stat	sb;
6253eb2bd662Svikram 	int		fd;
6254eb2bd662Svikram 	FILE		*tfp;
6255eb2bd662Svikram 	size_t		len;
6256eb2bd662Svikram 	int		ret;
6257eb2bd662Svikram 	int		error;
6258eb2bd662Svikram 	const char	*fcn = "FindAllUfsSignatures()";
6259eb2bd662Svikram 
6260eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) != -1)  {
6261eb2bd662Svikram 		bam_print(SIGNATURE_LIST_EXISTS, UFS_SIGNATURE_LIST);
6262eb2bd662Svikram 		return (0);
6263eb2bd662Svikram 	}
6264eb2bd662Svikram 
6265eb2bd662Svikram 	fd = open(UFS_SIGNATURE_LIST".tmp",
6266eb2bd662Svikram 	    O_RDWR|O_CREAT|O_TRUNC, 0644);
6267eb2bd662Svikram 	error = errno;
6268eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_TRUNC", fd = -1);
6269eb2bd662Svikram 	if (fd == -1) {
6270eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
6271eb2bd662Svikram 		return (-1);
6272eb2bd662Svikram 	}
6273eb2bd662Svikram 
6274eb2bd662Svikram 	ret = close(fd);
6275eb2bd662Svikram 	error = errno;
6276eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_CLOSE", ret = -1);
6277eb2bd662Svikram 	if (ret == -1) {
6278eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
6279eb2bd662Svikram 		    strerror(error));
6280eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6281eb2bd662Svikram 		return (-1);
6282eb2bd662Svikram 	}
6283eb2bd662Svikram 
6284eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
6285eb2bd662Svikram 	error = errno;
6286eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_APPEND_FOPEN", tfp = NULL);
6287eb2bd662Svikram 	if (tfp == NULL) {
6288eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
6289eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6290eb2bd662Svikram 		return (-1);
6291eb2bd662Svikram 	}
6292eb2bd662Svikram 
6293eb2bd662Svikram 	mnttab_hash = cache_mnttab();
6294eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_ERROR", mnttab_hash = NULL);
6295eb2bd662Svikram 	if (mnttab_hash == NULL) {
6296eb2bd662Svikram 		(void) fclose(tfp);
6297eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6298eb2bd662Svikram 		bam_error(CACHE_MNTTAB_FAIL, fcn);
6299eb2bd662Svikram 		return (-1);
6300eb2bd662Svikram 	}
6301eb2bd662Svikram 
6302eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt),
6303eb2bd662Svikram 	    "/tmp/bootadm_ufs_sign_mnt.%d", getpid());
6304eb2bd662Svikram 	(void) unlink(tmpmnt);
6305eb2bd662Svikram 
630648847494SEnrico Perla - Sun Microsystems 	ret = mkdirp(tmpmnt, DIR_PERMS);
6307eb2bd662Svikram 	error = errno;
6308eb2bd662Svikram 	INJECT_ERROR1("MKDIRP_SIGN_MNT", ret = -1);
6309eb2bd662Svikram 	if (ret == -1) {
6310eb2bd662Svikram 		bam_error(MKDIR_FAILED, tmpmnt, strerror(error));
6311eb2bd662Svikram 		free_mnttab(mnttab_hash);
6312eb2bd662Svikram 		(void) fclose(tfp);
6313eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6314eb2bd662Svikram 		return (-1);
6315eb2bd662Svikram 	}
6316eb2bd662Svikram 
6317eb2bd662Svikram 	dirp = opendir("/dev/rdsk");
6318eb2bd662Svikram 	error = errno;
6319eb2bd662Svikram 	INJECT_ERROR1("OPENDIR_DEV_RDSK", dirp = NULL);
6320eb2bd662Svikram 	if (dirp == NULL) {
6321eb2bd662Svikram 		bam_error(OPENDIR_FAILED, "/dev/rdsk", strerror(error));
6322eb2bd662Svikram 		goto fail;
6323eb2bd662Svikram 	}
6324eb2bd662Svikram 
6325eb2bd662Svikram 	while (dp = readdir(dirp)) {
6326eb2bd662Svikram 		if (strcmp(dp->d_name, ".") == 0 ||
6327eb2bd662Svikram 		    strcmp(dp->d_name, "..") == 0)
6328eb2bd662Svikram 			continue;
6329eb2bd662Svikram 
6330eb2bd662Svikram 		/*
6331eb2bd662Svikram 		 * we only look for the s0 slice. This is guranteed to
6332eb2bd662Svikram 		 * have 's' at len - 2.
6333eb2bd662Svikram 		 */
6334eb2bd662Svikram 		len = strlen(dp->d_name);
6335eb2bd662Svikram 		if (dp->d_name[len - 2 ] != 's' || dp->d_name[len - 1] != '0') {
6336eb2bd662Svikram 			BAM_DPRINTF((D_SKIP_SLICE_NOTZERO, fcn, dp->d_name));
6337eb2bd662Svikram 			continue;
6338eb2bd662Svikram 		}
6339eb2bd662Svikram 
6340eb2bd662Svikram 		ret = process_slice0(dp->d_name, tfp, mnttab_hash, tmpmnt);
6341eb2bd662Svikram 		INJECT_ERROR1("PROCESS_S0_FAIL", ret = -1);
6342eb2bd662Svikram 		if (ret == -1)
6343eb2bd662Svikram 			goto fail;
6344eb2bd662Svikram 	}
6345eb2bd662Svikram 
6346eb2bd662Svikram 	(void) closedir(dirp);
6347eb2bd662Svikram 	free_mnttab(mnttab_hash);
6348eb2bd662Svikram 	(void) rmdir(tmpmnt);
6349eb2bd662Svikram 
6350eb2bd662Svikram 	ret = fclose(tfp);
6351eb2bd662Svikram 	error = errno;
6352eb2bd662Svikram 	INJECT_ERROR1("FCLOSE_SIGNLIST_TMP", ret = EOF);
6353eb2bd662Svikram 	if (ret == EOF) {
6354eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
6355eb2bd662Svikram 		    strerror(error));
6356eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6357eb2bd662Svikram 		return (-1);
6358eb2bd662Svikram 	}
6359eb2bd662Svikram 
6360eb2bd662Svikram 	/* We have a list of existing GRUB signatures. Sort it first */
6361eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6362eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
6363eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
6364eb2bd662Svikram 
6365eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
6366eb2bd662Svikram 	INJECT_ERROR1("SORT_SIGN_LIST", ret = 1);
6367eb2bd662Svikram 	if (ret != 0) {
6368eb2bd662Svikram 		bam_error(GRUBSIGN_SORT_FAILED);
6369eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6370eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6371eb2bd662Svikram 		return (-1);
6372eb2bd662Svikram 	}
6373eb2bd662Svikram 
6374eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6375eb2bd662Svikram 
6376eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
6377eb2bd662Svikram 	error = errno;
6378eb2bd662Svikram 	INJECT_ERROR1("RENAME_TMP_SIGNLIST", ret = -1);
6379eb2bd662Svikram 	if (ret == -1) {
6380eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error));
6381eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6382eb2bd662Svikram 		return (-1);
6383eb2bd662Svikram 	}
6384eb2bd662Svikram 
6385eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == 0 && sb.st_size == 0) {
6386eb2bd662Svikram 		BAM_DPRINTF((D_ZERO_LEN_SIGNLIST, fcn, UFS_SIGNATURE_LIST));
6387eb2bd662Svikram 	}
6388eb2bd662Svikram 
6389eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6390eb2bd662Svikram 	return (0);
6391eb2bd662Svikram 
6392eb2bd662Svikram fail:
6393eb2bd662Svikram 	if (dirp)
6394eb2bd662Svikram 		(void) closedir(dirp);
6395eb2bd662Svikram 	free_mnttab(mnttab_hash);
6396eb2bd662Svikram 	(void) rmdir(tmpmnt);
6397eb2bd662Svikram 	(void) fclose(tfp);
6398eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6399eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6400eb2bd662Svikram 	return (-1);
6401eb2bd662Svikram }
6402eb2bd662Svikram 
6403eb2bd662Svikram static char *
6404eb2bd662Svikram create_ufs_sign(void)
6405eb2bd662Svikram {
6406eb2bd662Svikram 	struct stat	sb;
6407eb2bd662Svikram 	int		signnum = -1;
6408eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
6409eb2bd662Svikram 	char		*numstr;
6410eb2bd662Svikram 	int		i;
6411eb2bd662Svikram 	FILE		*tfp;
6412eb2bd662Svikram 	int		ret;
6413eb2bd662Svikram 	int		error;
6414eb2bd662Svikram 	const char	*fcn = "create_ufs_sign()";
6415eb2bd662Svikram 
6416eb2bd662Svikram 	bam_print(SEARCHING_UFS_SIGN);
6417eb2bd662Svikram 
6418eb2bd662Svikram 	ret = FindAllUfsSignatures();
6419eb2bd662Svikram 	INJECT_ERROR1("FIND_ALL_UFS", ret = -1);
6420eb2bd662Svikram 	if (ret == -1) {
6421eb2bd662Svikram 		bam_error(ERR_FIND_UFS_SIGN);
6422eb2bd662Svikram 		return (NULL);
6423eb2bd662Svikram 	}
6424eb2bd662Svikram 
6425eb2bd662Svikram 	/* Make sure the list exists and is owned by root */
6426eb2bd662Svikram 	INJECT_ERROR1("SIGNLIST_NOT_CREATED",
6427eb2bd662Svikram 	    (void) unlink(UFS_SIGNATURE_LIST));
6428eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == -1 || sb.st_uid != 0) {
6429eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6430eb2bd662Svikram 		bam_error(UFS_SIGNATURE_LIST_MISS, UFS_SIGNATURE_LIST);
6431eb2bd662Svikram 		return (NULL);
6432eb2bd662Svikram 	}
6433eb2bd662Svikram 
6434eb2bd662Svikram 	if (sb.st_size == 0) {
6435eb2bd662Svikram 		bam_print(GRUBSIGN_UFS_NONE);
6436eb2bd662Svikram 		i = 0;
6437eb2bd662Svikram 		goto found;
6438eb2bd662Svikram 	}
6439eb2bd662Svikram 
6440eb2bd662Svikram 	/* The signature list was sorted when it was created */
6441eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST, "r");
6442eb2bd662Svikram 	error = errno;
6443eb2bd662Svikram 	INJECT_ERROR1("FOPEN_SIGN_LIST", tfp = NULL);
6444eb2bd662Svikram 	if (tfp == NULL) {
6445eb2bd662Svikram 		bam_error(UFS_SIGNATURE_LIST_OPENERR,
6446eb2bd662Svikram 		    UFS_SIGNATURE_LIST, strerror(error));
6447eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6448eb2bd662Svikram 		return (NULL);
6449eb2bd662Svikram 	}
6450eb2bd662Svikram 
6451eb2bd662Svikram 	for (i = 0; s_fgets(tmpsign, sizeof (tmpsign), tfp); i++) {
6452eb2bd662Svikram 
6453eb2bd662Svikram 		if (strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
6454eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
6455eb2bd662Svikram 			(void) fclose(tfp);
6456eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6457eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6458eb2bd662Svikram 			return (NULL);
6459eb2bd662Svikram 		}
6460eb2bd662Svikram 		numstr = tmpsign + strlen(GRUBSIGN_UFS_PREFIX);
6461eb2bd662Svikram 
6462eb2bd662Svikram 		if (numstr[0] == '\0' || !isdigit(numstr[0])) {
6463eb2bd662Svikram 			(void) fclose(tfp);
6464eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6465eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6466eb2bd662Svikram 			return (NULL);
6467eb2bd662Svikram 		}
6468eb2bd662Svikram 
6469eb2bd662Svikram 		signnum = atoi(numstr);
6470eb2bd662Svikram 		INJECT_ERROR1("NEGATIVE_SIGN", signnum = -1);
6471eb2bd662Svikram 		if (signnum < 0) {
6472eb2bd662Svikram 			(void) fclose(tfp);
6473eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6474eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6475eb2bd662Svikram 			return (NULL);
6476eb2bd662Svikram 		}
6477eb2bd662Svikram 
6478eb2bd662Svikram 		if (i != signnum) {
6479eb2bd662Svikram 			BAM_DPRINTF((D_FOUND_HOLE_SIGNLIST, fcn, i));
6480eb2bd662Svikram 			break;
6481eb2bd662Svikram 		}
6482eb2bd662Svikram 	}
6483eb2bd662Svikram 
6484eb2bd662Svikram 	(void) fclose(tfp);
6485eb2bd662Svikram 
6486eb2bd662Svikram found:
6487eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "rootfs%d", i);
6488eb2bd662Svikram 
6489eb2bd662Svikram 	/* add the ufs signature to the /var/run list of signatures */
6490eb2bd662Svikram 	ret = ufs_add_to_sign_list(tmpsign);
6491eb2bd662Svikram 	INJECT_ERROR1("UFS_ADD_TO_SIGN_LIST", ret = -1);
6492eb2bd662Svikram 	if (ret == -1) {
6493eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6494eb2bd662Svikram 		bam_error(FAILED_ADD_SIGNLIST, tmpsign);
6495eb2bd662Svikram 		return (NULL);
6496eb2bd662Svikram 	}
6497eb2bd662Svikram 
6498eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6499eb2bd662Svikram 
6500eb2bd662Svikram 	return (s_strdup(tmpsign));
6501eb2bd662Svikram }
6502eb2bd662Svikram 
6503eb2bd662Svikram static char *
6504eb2bd662Svikram get_fstype(char *osroot)
6505eb2bd662Svikram {
6506eb2bd662Svikram 	FILE		*mntfp;
6507eb2bd662Svikram 	struct mnttab	mp = {0};
6508eb2bd662Svikram 	struct mnttab	mpref = {0};
6509eb2bd662Svikram 	int		error;
6510eb2bd662Svikram 	int		ret;
6511eb2bd662Svikram 	const char	*fcn = "get_fstype()";
6512eb2bd662Svikram 
6513eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_OSROOT", osroot = NULL);
6514eb2bd662Svikram 	if (osroot == NULL) {
6515eb2bd662Svikram 		bam_error(GET_FSTYPE_ARGS);
6516eb2bd662Svikram 		return (NULL);
6517eb2bd662Svikram 	}
6518eb2bd662Svikram 
6519eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
6520eb2bd662Svikram 	error = errno;
6521eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_FOPEN", mntfp = NULL);
6522eb2bd662Svikram 	if (mntfp == NULL) {
6523eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
6524eb2bd662Svikram 		return (NULL);
6525eb2bd662Svikram 	}
6526eb2bd662Svikram 
6527eb2bd662Svikram 	if (*osroot == '\0')
6528eb2bd662Svikram 		mpref.mnt_mountp = "/";
6529eb2bd662Svikram 	else
6530eb2bd662Svikram 		mpref.mnt_mountp = osroot;
6531eb2bd662Svikram 
6532eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
6533eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_GETMNTANY", ret = 1);
6534eb2bd662Svikram 	if (ret != 0) {
6535eb2bd662Svikram 		bam_error(MNTTAB_MNTPT_NOT_FOUND, osroot, MNTTAB);
6536eb2bd662Svikram 		(void) fclose(mntfp);
6537eb2bd662Svikram 		return (NULL);
6538eb2bd662Svikram 	}
6539eb2bd662Svikram 	(void) fclose(mntfp);
6540eb2bd662Svikram 
6541eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_NULL", mp.mnt_fstype = NULL);
6542eb2bd662Svikram 	if (mp.mnt_fstype == NULL) {
6543eb2bd662Svikram 		bam_error(MNTTAB_FSTYPE_NULL, osroot);
6544eb2bd662Svikram 		return (NULL);
6545eb2bd662Svikram 	}
6546eb2bd662Svikram 
6547eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6548eb2bd662Svikram 
6549eb2bd662Svikram 	return (s_strdup(mp.mnt_fstype));
6550eb2bd662Svikram }
6551eb2bd662Svikram 
6552eb2bd662Svikram static char *
6553eb2bd662Svikram create_zfs_sign(char *osdev)
6554eb2bd662Svikram {
6555eb2bd662Svikram 	char		tmpsign[PATH_MAX];
6556eb2bd662Svikram 	char		*pool;
6557eb2bd662Svikram 	const char	*fcn = "create_zfs_sign()";
6558eb2bd662Svikram 
6559eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, osdev));
6560eb2bd662Svikram 
6561eb2bd662Svikram 	/*
6562eb2bd662Svikram 	 * First find the pool name
6563eb2bd662Svikram 	 */
6564eb2bd662Svikram 	pool = get_pool(osdev);
6565eb2bd662Svikram 	INJECT_ERROR1("CREATE_ZFS_SIGN_GET_POOL", pool = NULL);
6566eb2bd662Svikram 	if (pool == NULL) {
6567eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
6568eb2bd662Svikram 		return (NULL);
6569eb2bd662Svikram 	}
6570eb2bd662Svikram 
6571eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "pool_%s", pool);
6572eb2bd662Svikram 
6573eb2bd662Svikram 	BAM_DPRINTF((D_CREATED_ZFS_SIGN, fcn, tmpsign));
6574eb2bd662Svikram 
6575eb2bd662Svikram 	free(pool);
6576eb2bd662Svikram 
6577eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6578eb2bd662Svikram 
6579eb2bd662Svikram 	return (s_strdup(tmpsign));
6580eb2bd662Svikram }
6581eb2bd662Svikram 
6582eb2bd662Svikram static char *
6583eb2bd662Svikram create_new_sign(char *osdev, char *fstype)
6584eb2bd662Svikram {
6585eb2bd662Svikram 	char		*sign;
6586eb2bd662Svikram 	const char	*fcn = "create_new_sign()";
6587eb2bd662Svikram 
6588eb2bd662Svikram 	INJECT_ERROR1("NEW_SIGN_FSTYPE", fstype = "foofs");
6589eb2bd662Svikram 
6590eb2bd662Svikram 	if (strcmp(fstype, "zfs") == 0) {
6591eb2bd662Svikram 		BAM_DPRINTF((D_CREATE_NEW_ZFS, fcn));
6592eb2bd662Svikram 		sign = create_zfs_sign(osdev);
6593eb2bd662Svikram 	} else if (strcmp(fstype, "ufs") == 0) {
6594eb2bd662Svikram 		BAM_DPRINTF((D_CREATE_NEW_UFS, fcn));
6595eb2bd662Svikram 		sign = create_ufs_sign();
6596eb2bd662Svikram 	} else {
6597eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
6598eb2bd662Svikram 		sign = NULL;
6599eb2bd662Svikram 	}
6600eb2bd662Svikram 
6601eb2bd662Svikram 	BAM_DPRINTF((D_CREATED_NEW_SIGN, fcn, sign ? sign : "<NULL>"));
6602eb2bd662Svikram 	return (sign);
6603eb2bd662Svikram }
6604eb2bd662Svikram 
6605eb2bd662Svikram static int
6606eb2bd662Svikram set_backup_common(char *mntpt, char *sign)
6607eb2bd662Svikram {
6608eb2bd662Svikram 	FILE		*bfp;
6609eb2bd662Svikram 	char		backup[PATH_MAX];
6610eb2bd662Svikram 	char		tmpsign[PATH_MAX];
6611eb2bd662Svikram 	int		error;
6612eb2bd662Svikram 	char		*bdir;
6613eb2bd662Svikram 	char		*backup_dup;
6614eb2bd662Svikram 	struct stat	sb;
6615eb2bd662Svikram 	int		ret;
6616eb2bd662Svikram 	const char	*fcn = "set_backup_common()";
6617eb2bd662Svikram 
6618eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
6619eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
6620eb2bd662Svikram 
6621eb2bd662Svikram 	/* First read the backup */
6622eb2bd662Svikram 	bfp = fopen(backup, "r");
6623eb2bd662Svikram 	if (bfp != NULL) {
6624eb2bd662Svikram 		while (s_fgets(tmpsign, sizeof (tmpsign), bfp)) {
6625eb2bd662Svikram 			if (strcmp(tmpsign, sign) == 0) {
6626eb2bd662Svikram 				BAM_DPRINTF((D_FOUND_IN_BACKUP, fcn, sign));
6627eb2bd662Svikram 				(void) fclose(bfp);
6628eb2bd662Svikram 				return (0);
6629eb2bd662Svikram 			}
6630eb2bd662Svikram 		}
6631eb2bd662Svikram 		(void) fclose(bfp);
6632eb2bd662Svikram 		BAM_DPRINTF((D_NOT_FOUND_IN_EXIST_BACKUP, fcn, sign));
6633eb2bd662Svikram 	} else {
6634eb2bd662Svikram 		BAM_DPRINTF((D_BACKUP_NOT_EXIST, fcn, backup));
6635eb2bd662Svikram 	}
6636eb2bd662Svikram 
6637eb2bd662Svikram 	/*
6638eb2bd662Svikram 	 * Didn't find the correct signature. First create
6639eb2bd662Svikram 	 * the directory if necessary.
6640eb2bd662Svikram 	 */
6641eb2bd662Svikram 
6642eb2bd662Svikram 	/* dirname() modifies its argument so dup it */
6643eb2bd662Svikram 	backup_dup = s_strdup(backup);
6644eb2bd662Svikram 	bdir = dirname(backup_dup);
6645eb2bd662Svikram 	assert(bdir);
6646eb2bd662Svikram 
6647eb2bd662Svikram 	ret = stat(bdir, &sb);
6648eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_STAT", ret = -1);
6649eb2bd662Svikram 	if (ret == -1) {
6650eb2bd662Svikram 		BAM_DPRINTF((D_BACKUP_DIR_NOEXIST, fcn, bdir));
665148847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(bdir, DIR_PERMS);
6652eb2bd662Svikram 		error = errno;
6653eb2bd662Svikram 		INJECT_ERROR1("SET_BACKUP_MKDIRP", ret = -1);
6654eb2bd662Svikram 		if (ret == -1) {
6655eb2bd662Svikram 			bam_error(GRUBSIGN_BACKUP_MKDIRERR,
6656eb2bd662Svikram 			    GRUBSIGN_BACKUP, strerror(error));
6657eb2bd662Svikram 			free(backup_dup);
6658eb2bd662Svikram 			return (-1);
6659eb2bd662Svikram 		}
6660eb2bd662Svikram 	}
6661eb2bd662Svikram 	free(backup_dup);
6662eb2bd662Svikram 
6663eb2bd662Svikram 	/*
6664eb2bd662Svikram 	 * Open the backup in append mode to add the correct
6665eb2bd662Svikram 	 * signature;
6666eb2bd662Svikram 	 */
6667eb2bd662Svikram 	bfp = fopen(backup, "a");
6668eb2bd662Svikram 	error = errno;
6669eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FOPEN_A", bfp = NULL);
6670eb2bd662Svikram 	if (bfp == NULL) {
6671eb2bd662Svikram 		bam_error(GRUBSIGN_BACKUP_OPENERR,
6672eb2bd662Svikram 		    GRUBSIGN_BACKUP, strerror(error));
6673eb2bd662Svikram 		return (-1);
6674eb2bd662Svikram 	}
6675eb2bd662Svikram 
6676eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "%s\n", sign);
6677eb2bd662Svikram 
6678eb2bd662Svikram 	ret = fputs(tmpsign, bfp);
6679eb2bd662Svikram 	error = errno;
6680eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FPUTS", ret = 0);
6681eb2bd662Svikram 	if (ret != strlen(tmpsign)) {
6682eb2bd662Svikram 		bam_error(GRUBSIGN_BACKUP_WRITEERR,
6683eb2bd662Svikram 		    GRUBSIGN_BACKUP, strerror(error));
6684eb2bd662Svikram 		(void) fclose(bfp);
6685eb2bd662Svikram 		return (-1);
6686eb2bd662Svikram 	}
6687eb2bd662Svikram 
6688eb2bd662Svikram 	(void) fclose(bfp);
6689eb2bd662Svikram 
6690eb2bd662Svikram 	if (bam_verbose)
6691eb2bd662Svikram 		bam_print(GRUBSIGN_BACKUP_UPDATED, GRUBSIGN_BACKUP);
6692eb2bd662Svikram 
6693eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6694eb2bd662Svikram 
6695eb2bd662Svikram 	return (0);
6696eb2bd662Svikram }
6697eb2bd662Svikram 
6698eb2bd662Svikram static int
6699eb2bd662Svikram set_backup_ufs(char *osroot, char *sign)
6700eb2bd662Svikram {
6701eb2bd662Svikram 	const char	*fcn = "set_backup_ufs()";
6702eb2bd662Svikram 
6703eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign));
6704eb2bd662Svikram 	return (set_backup_common(osroot, sign));
6705eb2bd662Svikram }
6706eb2bd662Svikram 
6707eb2bd662Svikram static int
6708eb2bd662Svikram set_backup_zfs(char *osdev, char *sign)
6709eb2bd662Svikram {
6710eb2bd662Svikram 	char		*pool;
6711eb2bd662Svikram 	char		*mntpt;
6712eb2bd662Svikram 	zfs_mnted_t	mnted;
6713eb2bd662Svikram 	int		ret;
6714eb2bd662Svikram 	const char	*fcn = "set_backup_zfs()";
6715eb2bd662Svikram 
6716eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign));
6717eb2bd662Svikram 
6718eb2bd662Svikram 	pool = get_pool(osdev);
6719eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_GET_POOL", pool = NULL);
6720eb2bd662Svikram 	if (pool == NULL) {
6721eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
6722eb2bd662Svikram 		return (-1);
6723eb2bd662Svikram 	}
6724eb2bd662Svikram 
6725eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
6726eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_MOUNT_DATASET", mntpt = NULL);
6727eb2bd662Svikram 	if (mntpt == NULL) {
6728eb2bd662Svikram 		bam_error(FAIL_MNT_TOP_DATASET, pool);
6729eb2bd662Svikram 		free(pool);
6730eb2bd662Svikram 		return (-1);
6731eb2bd662Svikram 	}
6732eb2bd662Svikram 
6733eb2bd662Svikram 	ret = set_backup_common(mntpt, sign);
6734eb2bd662Svikram 
6735eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
6736eb2bd662Svikram 
6737eb2bd662Svikram 	free(pool);
6738eb2bd662Svikram 
6739eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_ZFS_FAIL", ret = 1);
6740eb2bd662Svikram 	if (ret == 0) {
6741eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6742eb2bd662Svikram 	} else {
6743eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6744eb2bd662Svikram 	}
6745eb2bd662Svikram 
6746eb2bd662Svikram 	return (ret);
6747eb2bd662Svikram }
6748eb2bd662Svikram 
6749eb2bd662Svikram static int
6750eb2bd662Svikram set_backup(char *osroot, char *osdev, char *sign, char *fstype)
6751eb2bd662Svikram {
6752eb2bd662Svikram 	const char	*fcn = "set_backup()";
6753eb2bd662Svikram 	int		ret;
6754eb2bd662Svikram 
6755eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FSTYPE", fstype = "foofs");
6756eb2bd662Svikram 
6757eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
6758eb2bd662Svikram 		BAM_DPRINTF((D_SET_BACKUP_UFS, fcn));
6759eb2bd662Svikram 		ret = set_backup_ufs(osroot, sign);
6760eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
6761eb2bd662Svikram 		BAM_DPRINTF((D_SET_BACKUP_ZFS, fcn));
6762eb2bd662Svikram 		ret = set_backup_zfs(osdev, sign);
6763eb2bd662Svikram 	} else {
6764eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
6765eb2bd662Svikram 		ret = -1;
6766eb2bd662Svikram 	}
6767eb2bd662Svikram 
6768eb2bd662Svikram 	if (ret == 0) {
6769eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6770eb2bd662Svikram 	} else {
6771eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6772eb2bd662Svikram 	}
6773eb2bd662Svikram 
6774eb2bd662Svikram 	return (ret);
6775eb2bd662Svikram }
6776eb2bd662Svikram 
6777eb2bd662Svikram static int
6778eb2bd662Svikram set_primary_common(char *mntpt, char *sign)
6779eb2bd662Svikram {
6780eb2bd662Svikram 	char		signfile[PATH_MAX];
6781eb2bd662Svikram 	char		signdir[PATH_MAX];
6782eb2bd662Svikram 	struct stat	sb;
6783eb2bd662Svikram 	int		fd;
6784eb2bd662Svikram 	int		error;
6785eb2bd662Svikram 	int		ret;
6786eb2bd662Svikram 	const char	*fcn = "set_primary_common()";
6787eb2bd662Svikram 
6788eb2bd662Svikram 	(void) snprintf(signfile, sizeof (signfile), "%s/%s/%s",
6789eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR, sign);
6790eb2bd662Svikram 
6791eb2bd662Svikram 	if (stat(signfile, &sb) != -1) {
6792eb2bd662Svikram 		if (bam_verbose)
6793eb2bd662Svikram 			bam_print(PRIMARY_SIGN_EXISTS, sign);
6794eb2bd662Svikram 		return (0);
6795eb2bd662Svikram 	} else {
6796eb2bd662Svikram 		BAM_DPRINTF((D_PRIMARY_NOT_EXIST, fcn, signfile));
6797eb2bd662Svikram 	}
6798eb2bd662Svikram 
6799eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
6800eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
6801eb2bd662Svikram 
6802eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
6803eb2bd662Svikram 		BAM_DPRINTF((D_PRIMARY_DIR_NOEXIST, fcn, signdir));
680448847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(signdir, DIR_PERMS);
6805eb2bd662Svikram 		error = errno;
6806eb2bd662Svikram 		INJECT_ERROR1("SET_PRIMARY_MKDIRP", ret = -1);
6807eb2bd662Svikram 		if (ret == -1) {
6808eb2bd662Svikram 			bam_error(GRUBSIGN_MKDIR_ERR, signdir, strerror(errno));
6809eb2bd662Svikram 			return (-1);
6810eb2bd662Svikram 		}
6811eb2bd662Svikram 	}
6812eb2bd662Svikram 
6813eb2bd662Svikram 	fd = open(signfile, O_RDWR|O_CREAT|O_TRUNC, 0444);
6814eb2bd662Svikram 	error = errno;
6815eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_SIGN_CREAT", fd = -1);
6816eb2bd662Svikram 	if (fd == -1) {
6817eb2bd662Svikram 		bam_error(GRUBSIGN_PRIMARY_CREATERR, signfile, strerror(error));
6818eb2bd662Svikram 		return (-1);
6819eb2bd662Svikram 	}
6820eb2bd662Svikram 
6821eb2bd662Svikram 	ret = fsync(fd);
6822eb2bd662Svikram 	error = errno;
6823eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_FSYNC", ret = -1);
6824eb2bd662Svikram 	if (ret != 0) {
6825eb2bd662Svikram 		bam_error(GRUBSIGN_PRIMARY_SYNCERR, signfile, strerror(error));
6826eb2bd662Svikram 	}
6827eb2bd662Svikram 
6828eb2bd662Svikram 	(void) close(fd);
6829eb2bd662Svikram 
6830eb2bd662Svikram 	if (bam_verbose)
6831eb2bd662Svikram 		bam_print(GRUBSIGN_CREATED_PRIMARY, signfile);
6832eb2bd662Svikram 
6833eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6834eb2bd662Svikram 
6835eb2bd662Svikram 	return (0);
6836eb2bd662Svikram }
6837eb2bd662Svikram 
6838eb2bd662Svikram static int
6839eb2bd662Svikram set_primary_ufs(char *osroot, char *sign)
6840eb2bd662Svikram {
6841eb2bd662Svikram 	const char	*fcn = "set_primary_ufs()";
6842eb2bd662Svikram 
6843eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign));
6844eb2bd662Svikram 	return (set_primary_common(osroot, sign));
6845eb2bd662Svikram }
6846eb2bd662Svikram 
6847eb2bd662Svikram static int
6848eb2bd662Svikram set_primary_zfs(char *osdev, char *sign)
6849eb2bd662Svikram {
6850eb2bd662Svikram 	char		*pool;
6851eb2bd662Svikram 	char		*mntpt;
6852eb2bd662Svikram 	zfs_mnted_t	mnted;
6853eb2bd662Svikram 	int		ret;
6854eb2bd662Svikram 	const char	*fcn = "set_primary_zfs()";
6855eb2bd662Svikram 
6856eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign));
6857eb2bd662Svikram 
6858eb2bd662Svikram 	pool = get_pool(osdev);
6859eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_GET_POOL", pool = NULL);
6860eb2bd662Svikram 	if (pool == NULL) {
6861eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
6862eb2bd662Svikram 		return (-1);
6863eb2bd662Svikram 	}
6864eb2bd662Svikram 
6865eb2bd662Svikram 	/* Pool name must exist in the sign */
6866eb2bd662Svikram 	ret = (strstr(sign, pool) != NULL);
6867eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_POOL_SIGN_INCOMPAT", ret = 0);
6868eb2bd662Svikram 	if (ret == 0) {
6869eb2bd662Svikram 		bam_error(POOL_SIGN_INCOMPAT, pool, sign);
6870eb2bd662Svikram 		free(pool);
6871eb2bd662Svikram 		return (-1);
6872eb2bd662Svikram 	}
6873eb2bd662Svikram 
6874eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
6875eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_MOUNT_DATASET", mntpt = NULL);
6876eb2bd662Svikram 	if (mntpt == NULL) {
6877eb2bd662Svikram 		bam_error(FAIL_MNT_TOP_DATASET, pool);
6878eb2bd662Svikram 		free(pool);
6879eb2bd662Svikram 		return (-1);
6880eb2bd662Svikram 	}
6881eb2bd662Svikram 
6882eb2bd662Svikram 	ret = set_primary_common(mntpt, sign);
6883eb2bd662Svikram 
6884eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
6885eb2bd662Svikram 
6886eb2bd662Svikram 	free(pool);
6887eb2bd662Svikram 
6888eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_FAIL", ret = 1);
6889eb2bd662Svikram 	if (ret == 0) {
6890eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6891eb2bd662Svikram 	} else {
6892eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6893eb2bd662Svikram 	}
6894eb2bd662Svikram 
6895eb2bd662Svikram 	return (ret);
6896eb2bd662Svikram }
6897eb2bd662Svikram 
6898eb2bd662Svikram static int
6899eb2bd662Svikram set_primary(char *osroot, char *osdev, char *sign, char *fstype)
6900eb2bd662Svikram {
6901eb2bd662Svikram 	const char	*fcn = "set_primary()";
6902eb2bd662Svikram 	int		ret;
6903eb2bd662Svikram 
6904eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_FSTYPE", fstype = "foofs");
6905eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
6906eb2bd662Svikram 		BAM_DPRINTF((D_SET_PRIMARY_UFS, fcn));
6907eb2bd662Svikram 		ret = set_primary_ufs(osroot, sign);
6908eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
6909eb2bd662Svikram 		BAM_DPRINTF((D_SET_PRIMARY_ZFS, fcn));
6910eb2bd662Svikram 		ret = set_primary_zfs(osdev, sign);
6911eb2bd662Svikram 	} else {
6912eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
6913eb2bd662Svikram 		ret = -1;
6914eb2bd662Svikram 	}
6915eb2bd662Svikram 
6916eb2bd662Svikram 	if (ret == 0) {
6917eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6918eb2bd662Svikram 	} else {
6919eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6920eb2bd662Svikram 	}
6921eb2bd662Svikram 
6922eb2bd662Svikram 	return (ret);
6923eb2bd662Svikram }
6924eb2bd662Svikram 
6925eb2bd662Svikram static int
6926eb2bd662Svikram ufs_add_to_sign_list(char *sign)
6927eb2bd662Svikram {
6928eb2bd662Svikram 	FILE		*tfp;
6929eb2bd662Svikram 	char		signline[MAXNAMELEN];
6930eb2bd662Svikram 	char		cmd[PATH_MAX];
6931eb2bd662Svikram 	int		ret;
6932eb2bd662Svikram 	int		error;
6933eb2bd662Svikram 	const char	*fcn = "ufs_add_to_sign_list()";
6934eb2bd662Svikram 
6935eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_NOT_UFS", sign = "pool_rpool5");
6936eb2bd662Svikram 	if (strncmp(sign, GRUBSIGN_UFS_PREFIX,
6937eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
6938eb2bd662Svikram 		bam_error(INVALID_UFS_SIGN, sign);
6939eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6940eb2bd662Svikram 		return (-1);
6941eb2bd662Svikram 	}
6942eb2bd662Svikram 
6943eb2bd662Svikram 	/*
6944eb2bd662Svikram 	 * most failures in this routine are not a fatal error
6945eb2bd662Svikram 	 * We simply unlink the /var/run file and continue
6946eb2bd662Svikram 	 */
6947eb2bd662Svikram 
6948eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST".tmp");
6949eb2bd662Svikram 	error = errno;
6950eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME", ret = -1);
6951eb2bd662Svikram 	if (ret == -1) {
6952eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST".tmp",
6953eb2bd662Svikram 		    strerror(error));
6954eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6955eb2bd662Svikram 		return (0);
6956eb2bd662Svikram 	}
6957eb2bd662Svikram 
6958eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
6959eb2bd662Svikram 	error = errno;
6960eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FOPEN", tfp = NULL);
6961eb2bd662Svikram 	if (tfp == NULL) {
6962eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
6963eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6964eb2bd662Svikram 		return (0);
6965eb2bd662Svikram 	}
6966eb2bd662Svikram 
6967eb2bd662Svikram 	(void) snprintf(signline, sizeof (signline), "%s\n", sign);
6968eb2bd662Svikram 
6969eb2bd662Svikram 	ret = fputs(signline, tfp);
6970eb2bd662Svikram 	error = errno;
6971eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FPUTS", ret = 0);
6972eb2bd662Svikram 	if (ret != strlen(signline)) {
6973eb2bd662Svikram 		bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error));
6974eb2bd662Svikram 		(void) fclose(tfp);
6975eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6976eb2bd662Svikram 		return (0);
6977eb2bd662Svikram 	}
6978eb2bd662Svikram 
6979eb2bd662Svikram 	ret = fclose(tfp);
6980eb2bd662Svikram 	error = errno;
6981eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FCLOSE", ret = EOF);
6982eb2bd662Svikram 	if (ret == EOF) {
6983eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
6984eb2bd662Svikram 		    strerror(error));
6985eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6986eb2bd662Svikram 		return (0);
6987eb2bd662Svikram 	}
6988eb2bd662Svikram 
6989eb2bd662Svikram 	/* Sort the list again */
6990eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6991eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
6992eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
6993eb2bd662Svikram 
6994eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
6995eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_SORT", ret = 1);
6996eb2bd662Svikram 	if (ret != 0) {
6997eb2bd662Svikram 		bam_error(GRUBSIGN_SORT_FAILED);
6998eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6999eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
7000eb2bd662Svikram 		return (0);
7001eb2bd662Svikram 	}
7002eb2bd662Svikram 
7003eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
7004eb2bd662Svikram 
7005eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
7006eb2bd662Svikram 	error = errno;
7007eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME2", ret = -1);
7008eb2bd662Svikram 	if (ret == -1) {
7009eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error));
7010eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
7011eb2bd662Svikram 		return (0);
7012eb2bd662Svikram 	}
7013eb2bd662Svikram 
7014eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7015eb2bd662Svikram 
7016eb2bd662Svikram 	return (0);
7017eb2bd662Svikram }
7018eb2bd662Svikram 
7019eb2bd662Svikram static int
7020eb2bd662Svikram set_signature(char *osroot, char *osdev, char *sign, char *fstype)
7021eb2bd662Svikram {
7022eb2bd662Svikram 	int		ret;
7023eb2bd662Svikram 	const char	*fcn = "set_signature()";
7024eb2bd662Svikram 
7025eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY4, fcn, osroot, osdev, sign, fstype));
7026eb2bd662Svikram 
7027eb2bd662Svikram 	ret = set_backup(osroot, osdev, sign, fstype);
7028eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_BACKUP", ret = -1);
7029eb2bd662Svikram 	if (ret == -1) {
7030eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7031eb2bd662Svikram 		bam_error(SET_BACKUP_FAILED, sign, osroot, osdev);
7032eb2bd662Svikram 		return (-1);
7033eb2bd662Svikram 	}
7034eb2bd662Svikram 
7035eb2bd662Svikram 	ret = set_primary(osroot, osdev, sign, fstype);
7036eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_PRIMARY", ret = -1);
7037eb2bd662Svikram 
7038eb2bd662Svikram 	if (ret == 0) {
7039eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7040eb2bd662Svikram 	} else {
7041eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7042eb2bd662Svikram 		bam_error(SET_PRIMARY_FAILED, sign, osroot, osdev);
7043eb2bd662Svikram 
7044eb2bd662Svikram 	}
7045eb2bd662Svikram 	return (ret);
7046eb2bd662Svikram }
7047eb2bd662Svikram 
7048eb2bd662Svikram char *
7049eb2bd662Svikram get_grubsign(char *osroot, char *osdev)
7050eb2bd662Svikram {
7051eb2bd662Svikram 	char		*grubsign;	/* (<sign>,#,#) */
7052eb2bd662Svikram 	char		*slice;
7053eb2bd662Svikram 	int		fdiskpart;
7054eb2bd662Svikram 	char		*sign;
7055eb2bd662Svikram 	char		*fstype;
7056eb2bd662Svikram 	int		ret;
7057eb2bd662Svikram 	const char	*fcn = "get_grubsign()";
7058eb2bd662Svikram 
7059eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, osdev));
7060eb2bd662Svikram 	fstype = get_fstype(osroot);
7061eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FSTYPE", fstype = NULL);
7062eb2bd662Svikram 	if (fstype == NULL) {
7063eb2bd662Svikram 		bam_error(GET_FSTYPE_FAILED, osroot);
7064eb2bd662Svikram 		return (NULL);
7065eb2bd662Svikram 	}
7066eb2bd662Svikram 
7067eb2bd662Svikram 	sign = find_existing_sign(osroot, osdev, fstype);
7068eb2bd662Svikram 	INJECT_ERROR1("FIND_EXISTING_SIGN", sign = NULL);
7069eb2bd662Svikram 	if (sign == NULL) {
7070eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBSIGN_NO_EXISTING, fcn, osroot, osdev));
7071eb2bd662Svikram 		sign = create_new_sign(osdev, fstype);
7072eb2bd662Svikram 		INJECT_ERROR1("CREATE_NEW_SIGN", sign = NULL);
7073eb2bd662Svikram 		if (sign == NULL) {
7074eb2bd662Svikram 			bam_error(GRUBSIGN_CREATE_FAIL, osdev);
7075eb2bd662Svikram 			free(fstype);
7076eb2bd662Svikram 			return (NULL);
7077eb2bd662Svikram 		}
7078eb2bd662Svikram 	}
7079eb2bd662Svikram 
7080eb2bd662Svikram 	ret = set_signature(osroot, osdev, sign, fstype);
7081eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_FAIL", ret = -1);
7082eb2bd662Svikram 	if (ret == -1) {
7083eb2bd662Svikram 		bam_error(GRUBSIGN_WRITE_FAIL, osdev);
7084eb2bd662Svikram 		free(sign);
7085eb2bd662Svikram 		free(fstype);
7086eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
7087eb2bd662Svikram 		return (NULL);
7088eb2bd662Svikram 	}
7089eb2bd662Svikram 
7090eb2bd662Svikram 	free(fstype);
7091eb2bd662Svikram 
7092eb2bd662Svikram 	if (bam_verbose)
7093eb2bd662Svikram 		bam_print(GRUBSIGN_FOUND_OR_CREATED, sign, osdev);
7094eb2bd662Svikram 
7095eb2bd662Svikram 	fdiskpart = get_partition(osdev);
7096eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FDISK", fdiskpart = -1);
7097eb2bd662Svikram 	if (fdiskpart == -1) {
7098eb2bd662Svikram 		bam_error(FDISKPART_FAIL, osdev);
7099eb2bd662Svikram 		free(sign);
7100eb2bd662Svikram 		return (NULL);
7101eb2bd662Svikram 	}
7102eb2bd662Svikram 
7103eb2bd662Svikram 	slice = strrchr(osdev, 's');
7104eb2bd662Svikram 
7105eb2bd662Svikram 	grubsign = s_calloc(1, MAXNAMELEN + 10);
7106eb2bd662Svikram 	if (slice) {
7107eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d,%c)",
7108eb2bd662Svikram 		    sign, fdiskpart, slice[1] + 'a' - '0');
7109eb2bd662Svikram 	} else
7110eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d)",
7111eb2bd662Svikram 		    sign, fdiskpart);
7112eb2bd662Svikram 
7113eb2bd662Svikram 	free(sign);
7114eb2bd662Svikram 
7115eb2bd662Svikram 	BAM_DPRINTF((D_GET_GRUBSIGN_SUCCESS, fcn, grubsign));
7116eb2bd662Svikram 
7117eb2bd662Svikram 	return (grubsign);
71187c478bd9Sstevel@tonic-gate }
71197c478bd9Sstevel@tonic-gate 
7120ee3b8144Sszhou static char *
7121ee3b8144Sszhou get_title(char *rootdir)
71227c478bd9Sstevel@tonic-gate {
7123eb2bd662Svikram 	static char	title[80];
7124eb2bd662Svikram 	char		*cp = NULL;
7125eb2bd662Svikram 	char		release[PATH_MAX];
71267c478bd9Sstevel@tonic-gate 	FILE		*fp;
7127eb2bd662Svikram 	const char	*fcn = "get_title()";
71287c478bd9Sstevel@tonic-gate 
71297c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
71307c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
71317c478bd9Sstevel@tonic-gate 
71327c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
7133eb2bd662Svikram 	if (fp == NULL) {
7134eb2bd662Svikram 		bam_error(OPEN_FAIL, release, strerror(errno));
7135eb2bd662Svikram 		cp = NULL;
7136eb2bd662Svikram 		goto out;
7137eb2bd662Svikram 	}
71387c478bd9Sstevel@tonic-gate 
71397c478bd9Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
71407c478bd9Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
71417c478bd9Sstevel@tonic-gate 		if (cp)
71427c478bd9Sstevel@tonic-gate 			break;
71437c478bd9Sstevel@tonic-gate 	}
71447c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
7145eb2bd662Svikram 
7146eb2bd662Svikram out:
7147eb2bd662Svikram 	cp = cp ? cp : "Solaris";
7148eb2bd662Svikram 
7149eb2bd662Svikram 	BAM_DPRINTF((D_GET_TITLE, fcn, cp));
7150eb2bd662Svikram 
7151eb2bd662Svikram 	return (cp);
71527c478bd9Sstevel@tonic-gate }
71537c478bd9Sstevel@tonic-gate 
7154ae115bc7Smrj char *
71557c478bd9Sstevel@tonic-gate get_special(char *mountp)
71567c478bd9Sstevel@tonic-gate {
71577c478bd9Sstevel@tonic-gate 	FILE		*mntfp;
7158eb2bd662Svikram 	struct mnttab	mp = {0};
7159eb2bd662Svikram 	struct mnttab	mpref = {0};
7160eb2bd662Svikram 	int		error;
7161eb2bd662Svikram 	int		ret;
7162eb2bd662Svikram 	const char 	*fcn = "get_special()";
7163eb2bd662Svikram 
7164eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTPT", mountp = NULL);
7165eb2bd662Svikram 	if (mountp == NULL) {
7166eb2bd662Svikram 		bam_error(GET_SPECIAL_NULL_MNTPT);
7167eb2bd662Svikram 		return (NULL);
7168eb2bd662Svikram 	}
71697c478bd9Sstevel@tonic-gate 
71707c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
7171eb2bd662Svikram 	error = errno;
7172eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_OPEN", mntfp = NULL);
71737c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
7174eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
7175eb2bd662Svikram 		return (NULL);
71767c478bd9Sstevel@tonic-gate 	}
71777c478bd9Sstevel@tonic-gate 
71787c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
71797c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
71807c478bd9Sstevel@tonic-gate 	else
71817c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
7182eb2bd662Svikram 
7183eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
7184eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_SEARCH", ret = 1);
7185eb2bd662Svikram 	if (ret != 0) {
71867c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
7187eb2bd662Svikram 		BAM_DPRINTF((D_GET_SPECIAL_NOT_IN_MNTTAB, fcn, mountp));
71887c478bd9Sstevel@tonic-gate 		return (NULL);
71897c478bd9Sstevel@tonic-gate 	}
71907c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
71917c478bd9Sstevel@tonic-gate 
7192eb2bd662Svikram 	BAM_DPRINTF((D_GET_SPECIAL, fcn, mp.mnt_special));
7193eb2bd662Svikram 
71947c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
71957c478bd9Sstevel@tonic-gate }
71967c478bd9Sstevel@tonic-gate 
7197eb2bd662Svikram static void
7198eb2bd662Svikram free_physarray(char **physarray, int n)
71997c478bd9Sstevel@tonic-gate {
7200eb2bd662Svikram 	int			i;
7201eb2bd662Svikram 	const char		*fcn = "free_physarray()";
72027c478bd9Sstevel@tonic-gate 
7203eb2bd662Svikram 	assert(physarray);
7204eb2bd662Svikram 	assert(n);
7205eb2bd662Svikram 
7206eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY_N1, fcn, n));
7207eb2bd662Svikram 
7208eb2bd662Svikram 	for (i = 0; i < n; i++) {
7209eb2bd662Svikram 		free(physarray[i]);
72107c478bd9Sstevel@tonic-gate 	}
7211eb2bd662Svikram 	free(physarray);
7212eb2bd662Svikram 
7213eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7214eb2bd662Svikram }
7215eb2bd662Svikram 
7216eb2bd662Svikram static int
7217eb2bd662Svikram zfs_get_physical(char *special, char ***physarray, int *n)
7218eb2bd662Svikram {
7219eb2bd662Svikram 	char			sdup[PATH_MAX];
7220eb2bd662Svikram 	char			cmd[PATH_MAX];
7221eb2bd662Svikram 	char			dsk[PATH_MAX];
7222eb2bd662Svikram 	char			*pool;
7223eb2bd662Svikram 	filelist_t		flist = {0};
7224eb2bd662Svikram 	line_t			*lp;
7225eb2bd662Svikram 	line_t			*startlp;
7226eb2bd662Svikram 	char			*comp1;
7227eb2bd662Svikram 	int			i;
7228eb2bd662Svikram 	int			ret;
7229eb2bd662Svikram 	const char		*fcn = "zfs_get_physical()";
7230eb2bd662Svikram 
7231eb2bd662Svikram 	assert(special);
7232eb2bd662Svikram 
7233eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, special));
7234eb2bd662Svikram 
7235eb2bd662Svikram 	INJECT_ERROR1("INVALID_ZFS_SPECIAL", special = "/foo");
7236eb2bd662Svikram 	if (special[0] == '/') {
7237eb2bd662Svikram 		bam_error(INVALID_ZFS_SPECIAL, special);
7238eb2bd662Svikram 		return (-1);
7239eb2bd662Svikram 	}
7240eb2bd662Svikram 
7241eb2bd662Svikram 	(void) strlcpy(sdup, special, sizeof (sdup));
7242eb2bd662Svikram 
7243eb2bd662Svikram 	pool = strtok(sdup, "/");
7244eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_POOL", pool = NULL);
7245eb2bd662Svikram 	if (pool == NULL) {
7246eb2bd662Svikram 		bam_error(CANT_FIND_POOL_FROM_SPECIAL, special);
7247eb2bd662Svikram 		return (-1);
7248eb2bd662Svikram 	}
7249eb2bd662Svikram 
7250eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/zpool status %s", pool);
7251eb2bd662Svikram 
7252eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
7253eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS", ret = 1);
7254eb2bd662Svikram 	if (ret != 0) {
7255eb2bd662Svikram 		bam_error(ZFS_GET_POOL_STATUS, pool);
7256eb2bd662Svikram 		return (-1);
7257eb2bd662Svikram 	}
7258eb2bd662Svikram 
7259eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS_OUT", flist.head = NULL);
7260eb2bd662Svikram 	if (flist.head == NULL) {
7261eb2bd662Svikram 		bam_error(BAD_ZPOOL_STATUS, pool);
7262eb2bd662Svikram 		filelist_free(&flist);
7263eb2bd662Svikram 		return (-1);
7264eb2bd662Svikram 	}
7265eb2bd662Svikram 
7266eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
7267eb2bd662Svikram 		BAM_DPRINTF((D_STRTOK_ZPOOL_STATUS, fcn, lp->line));
7268eb2bd662Svikram 		comp1 = strtok(lp->line, " \t");
7269eb2bd662Svikram 		if (comp1 == NULL) {
7270eb2bd662Svikram 			free(lp->line);
7271eb2bd662Svikram 			lp->line = NULL;
7272eb2bd662Svikram 		} else {
7273eb2bd662Svikram 			comp1 = s_strdup(comp1);
7274eb2bd662Svikram 			free(lp->line);
7275eb2bd662Svikram 			lp->line = comp1;
7276eb2bd662Svikram 		}
7277eb2bd662Svikram 	}
7278eb2bd662Svikram 
7279eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
7280eb2bd662Svikram 		if (lp->line == NULL)
7281eb2bd662Svikram 			continue;
7282eb2bd662Svikram 		if (strcmp(lp->line, pool) == 0) {
7283eb2bd662Svikram 			BAM_DPRINTF((D_FOUND_POOL_IN_ZPOOL_STATUS, fcn, pool));
7284eb2bd662Svikram 			break;
7285eb2bd662Svikram 		}
7286eb2bd662Svikram 	}
7287eb2bd662Svikram 
7288eb2bd662Svikram 	if (lp == NULL) {
7289eb2bd662Svikram 		bam_error(NO_POOL_IN_ZPOOL_STATUS, pool);
7290eb2bd662Svikram 		filelist_free(&flist);
7291eb2bd662Svikram 		return (-1);
7292eb2bd662Svikram 	}
7293eb2bd662Svikram 
7294eb2bd662Svikram 	startlp = lp->next;
7295eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
7296eb2bd662Svikram 		if (lp->line == NULL)
7297eb2bd662Svikram 			continue;
7298eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
7299eb2bd662Svikram 			continue;
7300eb2bd662Svikram 		if (lp->line[0] == '\0' || strcmp(lp->line, "errors:") == 0)
7301eb2bd662Svikram 			break;
7302eb2bd662Svikram 		i++;
7303eb2bd662Svikram 		BAM_DPRINTF((D_COUNTING_ZFS_PHYS, fcn, i));
7304eb2bd662Svikram 	}
7305eb2bd662Svikram 
7306eb2bd662Svikram 	if (i == 0) {
7307eb2bd662Svikram 		bam_error(NO_PHYS_IN_ZPOOL_STATUS, pool);
7308eb2bd662Svikram 		filelist_free(&flist);
7309eb2bd662Svikram 		return (-1);
7310eb2bd662Svikram 	}
7311eb2bd662Svikram 
7312eb2bd662Svikram 	*n = i;
7313eb2bd662Svikram 	*physarray = s_calloc(*n, sizeof (char *));
7314eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
7315eb2bd662Svikram 		if (lp->line == NULL)
7316eb2bd662Svikram 			continue;
7317eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
7318eb2bd662Svikram 			continue;
7319eb2bd662Svikram 		if (strcmp(lp->line, "errors:") == 0)
7320eb2bd662Svikram 			break;
7321eb2bd662Svikram 		if (strncmp(lp->line, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
7322eb2bd662Svikram 		    strncmp(lp->line, "/dev/rdsk/",
7323eb2bd662Svikram 		    strlen("/dev/rdsk/")) != 0)  {
7324e998e519SSheshadri Vasudevan 			(void) snprintf(dsk, sizeof (dsk), "/dev/rdsk/%s",
7325eb2bd662Svikram 			    lp->line);
7326eb2bd662Svikram 		} else {
7327eb2bd662Svikram 			(void) strlcpy(dsk, lp->line, sizeof (dsk));
7328eb2bd662Svikram 		}
7329eb2bd662Svikram 		BAM_DPRINTF((D_ADDING_ZFS_PHYS, fcn, dsk, pool));
7330eb2bd662Svikram 		(*physarray)[i++] = s_strdup(dsk);
7331eb2bd662Svikram 	}
7332eb2bd662Svikram 
7333eb2bd662Svikram 	assert(i == *n);
7334eb2bd662Svikram 
7335eb2bd662Svikram 	filelist_free(&flist);
7336eb2bd662Svikram 
7337eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7338eb2bd662Svikram 	return (0);
7339eb2bd662Svikram }
7340eb2bd662Svikram 
7341963390b4Svikram /*
7342963390b4Svikram  * Certain services needed to run metastat successfully may not
7343963390b4Svikram  * be enabled. Enable them now.
7344963390b4Svikram  */
7345963390b4Svikram /*
7346963390b4Svikram  * Checks if the specified service is online
7347963390b4Svikram  * Returns: 	1 if the service is online
7348963390b4Svikram  *		0 if the service is not online
7349963390b4Svikram  *		-1 on error
7350963390b4Svikram  */
7351963390b4Svikram static int
7352963390b4Svikram is_svc_online(char *svc)
7353963390b4Svikram {
7354963390b4Svikram 	char			*state;
7355963390b4Svikram 	const char		*fcn = "is_svc_online()";
7356963390b4Svikram 
7357963390b4Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, svc));
7358963390b4Svikram 
7359963390b4Svikram 	state = smf_get_state(svc);
7360963390b4Svikram 	INJECT_ERROR2("GET_SVC_STATE", free(state), state = NULL);
7361963390b4Svikram 	if (state == NULL) {
7362963390b4Svikram 		bam_error(GET_SVC_STATE_ERR, svc);
7363963390b4Svikram 		return (-1);
7364963390b4Svikram 	}
7365963390b4Svikram 	BAM_DPRINTF((D_GOT_SVC_STATUS, fcn, svc));
7366963390b4Svikram 
7367963390b4Svikram 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
7368963390b4Svikram 		BAM_DPRINTF((D_SVC_ONLINE, fcn, svc));
7369963390b4Svikram 		free(state);
7370963390b4Svikram 		return (1);
7371963390b4Svikram 	}
7372963390b4Svikram 
7373963390b4Svikram 	BAM_DPRINTF((D_SVC_NOT_ONLINE, fcn, state, svc));
7374963390b4Svikram 
7375963390b4Svikram 	free(state);
7376963390b4Svikram 
7377963390b4Svikram 	return (0);
7378963390b4Svikram }
7379963390b4Svikram 
7380963390b4Svikram static int
7381963390b4Svikram enable_svc(char *svc)
7382963390b4Svikram {
7383963390b4Svikram 	int			ret;
7384963390b4Svikram 	int			sleeptime;
7385963390b4Svikram 	const char		*fcn = "enable_svc()";
7386963390b4Svikram 
7387963390b4Svikram 	ret = is_svc_online(svc);
7388963390b4Svikram 	if (ret == -1) {
7389963390b4Svikram 		bam_error(SVC_IS_ONLINE_FAILED, svc);
7390963390b4Svikram 		return (-1);
7391963390b4Svikram 	} else if (ret == 1) {
7392963390b4Svikram 		BAM_DPRINTF((D_SVC_ALREADY_ONLINE, fcn, svc));
7393963390b4Svikram 		return (0);
7394963390b4Svikram 	}
7395963390b4Svikram 
7396963390b4Svikram 	/* Service is not enabled. Enable it now. */
7397963390b4Svikram 	ret = smf_enable_instance(svc, 0);
7398963390b4Svikram 	INJECT_ERROR1("ENABLE_SVC_FAILED", ret = -1);
7399963390b4Svikram 	if (ret != 0) {
7400963390b4Svikram 		bam_error(ENABLE_SVC_FAILED, svc);
7401963390b4Svikram 		return (-1);
7402963390b4Svikram 	}
7403963390b4Svikram 
7404963390b4Svikram 	BAM_DPRINTF((D_SVC_ONLINE_INITIATED, fcn, svc));
7405963390b4Svikram 
7406963390b4Svikram 	sleeptime = 0;
7407963390b4Svikram 	do {
7408963390b4Svikram 		ret = is_svc_online(svc);
7409963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_SUCCESS", ret = 1);
7410963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_FAILURE", ret = -1);
7411963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_NOTYET", ret = 0);
7412963390b4Svikram 		if (ret == -1) {
7413963390b4Svikram 			bam_error(ERR_SVC_GET_ONLINE, svc);
7414963390b4Svikram 			return (-1);
7415963390b4Svikram 		} else if (ret == 1) {
7416963390b4Svikram 			BAM_DPRINTF((D_SVC_NOW_ONLINE, fcn, svc));
7417963390b4Svikram 			return (1);
7418963390b4Svikram 		}
7419963390b4Svikram 		(void) sleep(1);
7420589ac1f4SRobert Harris 	} while (++sleeptime < 60);
7421963390b4Svikram 
7422963390b4Svikram 	bam_error(TIMEOUT_ENABLE_SVC, svc);
7423963390b4Svikram 
7424963390b4Svikram 	return (-1);
7425963390b4Svikram }
7426963390b4Svikram 
7427eb2bd662Svikram static int
7428eb2bd662Svikram ufs_get_physical(char *special, char ***physarray, int *n)
7429eb2bd662Svikram {
7430eb2bd662Svikram 	char			cmd[PATH_MAX];
7431eb2bd662Svikram 	char			*shortname;
7432eb2bd662Svikram 	filelist_t		flist = {0};
7433eb2bd662Svikram 	char			*meta;
7434eb2bd662Svikram 	char			*type;
7435eb2bd662Svikram 	char			*comp1;
7436eb2bd662Svikram 	char			*comp2;
7437eb2bd662Svikram 	char			*comp3;
7438eb2bd662Svikram 	char			*comp4;
7439eb2bd662Svikram 	int			i;
7440eb2bd662Svikram 	line_t			*lp;
7441eb2bd662Svikram 	int			ret;
7442963390b4Svikram 	char			*svc;
7443eb2bd662Svikram 	const char		*fcn = "ufs_get_physical()";
7444eb2bd662Svikram 
7445eb2bd662Svikram 	assert(special);
7446eb2bd662Svikram 
7447eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, special));
7448eb2bd662Svikram 
7449eb2bd662Svikram 	if (strncmp(special, "/dev/md/", strlen("/dev/md/")) != 0) {
7450eb2bd662Svikram 		bam_error(UFS_GET_PHYS_NOT_SVM, special);
7451eb2bd662Svikram 		return (-1);
7452eb2bd662Svikram 	}
7453eb2bd662Svikram 
7454eb2bd662Svikram 	if (strncmp(special, "/dev/md/dsk/", strlen("/dev/md/dsk/")) == 0) {
7455eb2bd662Svikram 		shortname = special + strlen("/dev/md/dsk/");
7456eb2bd662Svikram 	} else if (strncmp(special, "/dev/md/rdsk/",
7457eb2bd662Svikram 	    strlen("/dev/md/rdsk/")) == 0) {
7458eb2bd662Svikram 		shortname = special + strlen("/dev/md/rdsk");
7459eb2bd662Svikram 	} else {
7460eb2bd662Svikram 		bam_error(UFS_GET_PHYS_INVALID_SVM, special);
7461eb2bd662Svikram 		return (-1);
7462eb2bd662Svikram 	}
7463eb2bd662Svikram 
7464eb2bd662Svikram 	BAM_DPRINTF((D_UFS_SVM_SHORT, fcn, special, shortname));
7465eb2bd662Svikram 
7466963390b4Svikram 	svc = "network/rpc/meta:default";
7467963390b4Svikram 	if (enable_svc(svc) == -1) {
7468963390b4Svikram 		bam_error(UFS_SVM_METASTAT_SVC_ERR, svc);
7469963390b4Svikram 	}
7470963390b4Svikram 
7471eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/metastat -p %s", shortname);
7472eb2bd662Svikram 
7473eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
7474eb2bd662Svikram 	INJECT_ERROR1("UFS_SVM_METASTAT", ret = 1);
7475eb2bd662Svikram 	if (ret != 0) {
7476eb2bd662Svikram 		bam_error(UFS_SVM_METASTAT_ERR, shortname);
7477eb2bd662Svikram 		return (-1);
7478eb2bd662Svikram 	}
7479eb2bd662Svikram 
7480eb2bd662Svikram 	INJECT_ERROR1("UFS_SVM_METASTAT_OUT", flist.head = NULL);
7481eb2bd662Svikram 	if (flist.head == NULL) {
7482eb2bd662Svikram 		bam_error(BAD_UFS_SVM_METASTAT, shortname);
7483eb2bd662Svikram 		filelist_free(&flist);
7484eb2bd662Svikram 		return (-1);
74857c478bd9Sstevel@tonic-gate 	}
74867c478bd9Sstevel@tonic-gate 
74877c478bd9Sstevel@tonic-gate 	/*
7488eb2bd662Svikram 	 * Check if not a mirror. We only parse a single metadevice
7489eb2bd662Svikram 	 * if not a mirror
7490eb2bd662Svikram 	 */
7491eb2bd662Svikram 	meta = strtok(flist.head->line, " \t");
7492eb2bd662Svikram 	type = strtok(NULL, " \t");
7493eb2bd662Svikram 	if (meta == NULL || type == NULL) {
7494eb2bd662Svikram 		bam_error(ERROR_PARSE_UFS_SVM_METASTAT, shortname);
7495eb2bd662Svikram 		filelist_free(&flist);
7496eb2bd662Svikram 		return (-1);
7497eb2bd662Svikram 	}
7498eb2bd662Svikram 	if (strcmp(type, "-m") != 0) {
7499eb2bd662Svikram 		comp1 = strtok(NULL, " \t");
7500eb2bd662Svikram 		comp2 = strtok(NULL, " \t");
7501eb2bd662Svikram 		if (comp1 == NULL || comp2 != NULL) {
7502eb2bd662Svikram 			bam_error(INVALID_UFS_SVM_METASTAT, shortname);
7503eb2bd662Svikram 			filelist_free(&flist);
7504eb2bd662Svikram 			return (-1);
7505eb2bd662Svikram 		}
7506eb2bd662Svikram 		BAM_DPRINTF((D_UFS_SVM_ONE_COMP, fcn, comp1, shortname));
7507eb2bd662Svikram 		*physarray = s_calloc(1, sizeof (char *));
7508eb2bd662Svikram 		(*physarray)[0] = s_strdup(comp1);
7509eb2bd662Svikram 		*n = 1;
7510eb2bd662Svikram 		filelist_free(&flist);
7511eb2bd662Svikram 		return (0);
7512eb2bd662Svikram 	}
7513eb2bd662Svikram 
7514eb2bd662Svikram 	/*
7515eb2bd662Svikram 	 * Okay we have a mirror. Everything after the first line
7516eb2bd662Svikram 	 * is a submirror
7517eb2bd662Svikram 	 */
7518eb2bd662Svikram 	for (i = 0, lp = flist.head->next; lp; lp = lp->next) {
7519eb2bd662Svikram 		if (strstr(lp->line, "/dev/dsk/") == NULL &&
7520eb2bd662Svikram 		    strstr(lp->line, "/dev/rdsk/") == NULL) {
7521eb2bd662Svikram 			bam_error(CANNOT_PARSE_UFS_SVM_METASTAT, shortname);
7522eb2bd662Svikram 			filelist_free(&flist);
7523eb2bd662Svikram 			return (-1);
7524eb2bd662Svikram 		}
7525eb2bd662Svikram 		i++;
7526eb2bd662Svikram 	}
7527eb2bd662Svikram 
7528eb2bd662Svikram 	*physarray = s_calloc(i, sizeof (char *));
7529eb2bd662Svikram 	*n = i;
7530eb2bd662Svikram 
7531eb2bd662Svikram 	for (i = 0, lp = flist.head->next; lp; lp = lp->next) {
7532eb2bd662Svikram 		comp1 = strtok(lp->line, " \t");
7533eb2bd662Svikram 		comp2 = strtok(NULL, " \t");
7534eb2bd662Svikram 		comp3 = strtok(NULL, " \t");
7535eb2bd662Svikram 		comp4 = strtok(NULL, " \t");
7536eb2bd662Svikram 
7537eb2bd662Svikram 		if (comp3 == NULL || comp4 == NULL ||
7538eb2bd662Svikram 		    (strncmp(comp4, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
7539eb2bd662Svikram 		    strncmp(comp4, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0)) {
7540eb2bd662Svikram 			bam_error(CANNOT_PARSE_UFS_SVM_SUBMIRROR, shortname);
7541eb2bd662Svikram 			filelist_free(&flist);
7542eb2bd662Svikram 			free_physarray(*physarray, *n);
7543eb2bd662Svikram 			return (-1);
7544eb2bd662Svikram 		}
7545eb2bd662Svikram 
7546eb2bd662Svikram 		(*physarray)[i++] = s_strdup(comp4);
7547eb2bd662Svikram 	}
7548eb2bd662Svikram 
7549eb2bd662Svikram 	assert(i == *n);
7550eb2bd662Svikram 
7551eb2bd662Svikram 	filelist_free(&flist);
7552eb2bd662Svikram 
7553eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7554eb2bd662Svikram 	return (0);
7555eb2bd662Svikram }
7556eb2bd662Svikram 
7557eb2bd662Svikram static int
7558eb2bd662Svikram get_physical(char *menu_root, char ***physarray, int *n)
7559eb2bd662Svikram {
7560eb2bd662Svikram 	char			*special;
7561eb2bd662Svikram 	int			ret;
7562eb2bd662Svikram 	const char		*fcn = "get_physical()";
7563eb2bd662Svikram 
7564eb2bd662Svikram 	assert(menu_root);
7565eb2bd662Svikram 	assert(physarray);
7566eb2bd662Svikram 	assert(n);
7567eb2bd662Svikram 
7568eb2bd662Svikram 	*physarray = NULL;
7569eb2bd662Svikram 	*n = 0;
7570eb2bd662Svikram 
7571eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_root));
7572eb2bd662Svikram 
7573eb2bd662Svikram 	/* First get the device special file from /etc/mnttab */
7574eb2bd662Svikram 	special = get_special(menu_root);
7575eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_SPECIAL", special = NULL);
7576eb2bd662Svikram 	if (special == NULL) {
7577eb2bd662Svikram 		bam_error(GET_SPECIAL_NULL, menu_root);
7578eb2bd662Svikram 		return (-1);
7579eb2bd662Svikram 	}
7580eb2bd662Svikram 
7581eb2bd662Svikram 	/* If already a physical device nothing to do */
7582eb2bd662Svikram 	if (strncmp(special, "/dev/dsk/", strlen("/dev/dsk/")) == 0 ||
7583eb2bd662Svikram 	    strncmp(special, "/dev/rdsk/", strlen("/dev/rdsk/")) == 0) {
7584eb2bd662Svikram 		BAM_DPRINTF((D_GET_PHYSICAL_ALREADY, fcn, menu_root, special));
7585eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7586eb2bd662Svikram 		*physarray = s_calloc(1, sizeof (char *));
7587eb2bd662Svikram 		(*physarray)[0] = special;
7588eb2bd662Svikram 		*n = 1;
7589eb2bd662Svikram 		return (0);
7590eb2bd662Svikram 	}
7591eb2bd662Svikram 
7592eb2bd662Svikram 	if (is_zfs(menu_root)) {
7593eb2bd662Svikram 		ret = zfs_get_physical(special, physarray, n);
7594eb2bd662Svikram 	} else if (is_ufs(menu_root)) {
7595eb2bd662Svikram 		ret = ufs_get_physical(special, physarray, n);
7596eb2bd662Svikram 	} else {
7597eb2bd662Svikram 		bam_error(GET_PHYSICAL_NOTSUP_FSTYPE, menu_root, special);
7598eb2bd662Svikram 		ret = -1;
7599eb2bd662Svikram 	}
7600eb2bd662Svikram 
7601eb2bd662Svikram 	free(special);
7602eb2bd662Svikram 
7603eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_RET", ret = -1);
7604eb2bd662Svikram 	if (ret == -1) {
7605eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7606eb2bd662Svikram 	} else {
7607eb2bd662Svikram 		int	i;
7608eb2bd662Svikram 		assert (*n > 0);
7609eb2bd662Svikram 		for (i = 0; i < *n; i++) {
7610eb2bd662Svikram 			BAM_DPRINTF((D_GET_PHYSICAL_RET, fcn, (*physarray)[i]));
7611eb2bd662Svikram 		}
7612eb2bd662Svikram 	}
7613eb2bd662Svikram 
7614eb2bd662Svikram 	return (ret);
7615eb2bd662Svikram }
7616eb2bd662Svikram 
7617eb2bd662Svikram static int
7618eb2bd662Svikram is_bootdisk(char *osroot, char *physical)
7619eb2bd662Svikram {
7620eb2bd662Svikram 	int			ret;
7621eb2bd662Svikram 	char			*grubroot;
7622eb2bd662Svikram 	char			*bootp;
7623eb2bd662Svikram 	const char		*fcn = "is_bootdisk()";
7624eb2bd662Svikram 
7625eb2bd662Svikram 	assert(osroot);
7626eb2bd662Svikram 	assert(physical);
7627eb2bd662Svikram 
7628eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, physical));
7629eb2bd662Svikram 
7630eb2bd662Svikram 	bootp = strstr(physical, "p0:boot");
7631eb2bd662Svikram 	if (bootp)
7632eb2bd662Svikram 		*bootp = '\0';
7633eb2bd662Svikram 	/*
7634eb2bd662Svikram 	 * We just want the BIOS mapping for menu disk.
7635eb2bd662Svikram 	 * Don't pass menu_root to get_grubroot() as the
7636eb2bd662Svikram 	 * check that it is used for is not relevant here.
7637eb2bd662Svikram 	 * The osroot is immaterial as well - it is only used to
7638eb2bd662Svikram 	 * to find create_diskmap script. Everything hinges on
7639eb2bd662Svikram 	 * "physical"
7640eb2bd662Svikram 	 */
7641eb2bd662Svikram 	grubroot = get_grubroot(osroot, physical, NULL);
7642eb2bd662Svikram 
7643eb2bd662Svikram 	INJECT_ERROR1("IS_BOOTDISK_GRUBROOT", grubroot = NULL);
7644eb2bd662Svikram 	if (grubroot == NULL) {
764537eb779cSVikram Hegde 		if (bam_verbose)
764637eb779cSVikram Hegde 			bam_error(NO_GRUBROOT_FOR_DISK, physical);
7647eb2bd662Svikram 		return (0);
7648eb2bd662Svikram 	}
7649eb2bd662Svikram 	ret = grubroot[3] == '0';
7650eb2bd662Svikram 	free(grubroot);
7651eb2bd662Svikram 
7652eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_RET, fcn, ret));
7653eb2bd662Svikram 
7654eb2bd662Svikram 	return (ret);
7655eb2bd662Svikram }
7656eb2bd662Svikram 
7657eb2bd662Svikram /*
7658eb2bd662Svikram  * Check if menu is on the boot device
76597c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
76607c478bd9Sstevel@tonic-gate  */
76617c478bd9Sstevel@tonic-gate static int
7662eb2bd662Svikram menu_on_bootdisk(char *osroot, char *menu_root)
76637c478bd9Sstevel@tonic-gate {
7664eb2bd662Svikram 	char		**physarray;
76657c478bd9Sstevel@tonic-gate 	int		ret;
7666eb2bd662Svikram 	int		n;
7667eb2bd662Svikram 	int		i;
7668eb2bd662Svikram 	int		on_bootdisk;
7669eb2bd662Svikram 	const char	*fcn = "menu_on_bootdisk()";
76707c478bd9Sstevel@tonic-gate 
7671eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root));
76727c478bd9Sstevel@tonic-gate 
7673eb2bd662Svikram 	ret = get_physical(menu_root, &physarray, &n);
7674eb2bd662Svikram 	INJECT_ERROR1("MENU_ON_BOOTDISK_PHYSICAL", ret = -1);
7675eb2bd662Svikram 	if (ret != 0) {
7676eb2bd662Svikram 		bam_error(GET_PHYSICAL_MENU_NULL, menu_root);
76777c478bd9Sstevel@tonic-gate 		return (0);
7678eb2bd662Svikram 	}
7679eb2bd662Svikram 
7680eb2bd662Svikram 	assert(physarray);
7681eb2bd662Svikram 	assert(n > 0);
7682eb2bd662Svikram 
7683eb2bd662Svikram 	on_bootdisk = 0;
7684eb2bd662Svikram 	for (i = 0; i < n; i++) {
7685eb2bd662Svikram 		assert(strncmp(physarray[i], "/dev/dsk/",
7686eb2bd662Svikram 		    strlen("/dev/dsk/")) == 0 ||
7687eb2bd662Svikram 		    strncmp(physarray[i], "/dev/rdsk/",
7688eb2bd662Svikram 		    strlen("/dev/rdsk/")) == 0);
7689eb2bd662Svikram 
7690eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_ON_BOOTDISK, fcn, physarray[i]));
7691eb2bd662Svikram 		if (is_bootdisk(osroot, physarray[i])) {
7692eb2bd662Svikram 			on_bootdisk = 1;
7693eb2bd662Svikram 			BAM_DPRINTF((D_IS_ON_BOOTDISK, fcn, physarray[i]));
7694eb2bd662Svikram 		}
7695eb2bd662Svikram 	}
7696eb2bd662Svikram 
7697eb2bd662Svikram 	free_physarray(physarray, n);
7698eb2bd662Svikram 
7699eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_YES", on_bootdisk = 1);
7700eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_NO", on_bootdisk = 0);
7701eb2bd662Svikram 	if (on_bootdisk) {
7702eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7703eb2bd662Svikram 	} else {
7704eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7705eb2bd662Svikram 	}
7706eb2bd662Svikram 
7707eb2bd662Svikram 	return (on_bootdisk);
7708eb2bd662Svikram }
7709eb2bd662Svikram 
7710eb2bd662Svikram void
7711eb2bd662Svikram bam_add_line(menu_t *mp, entry_t *entry, line_t *prev, line_t *lp)
7712eb2bd662Svikram {
7713eb2bd662Svikram 	const char	*fcn = "bam_add_line()";
7714eb2bd662Svikram 
7715eb2bd662Svikram 	assert(mp);
7716eb2bd662Svikram 	assert(entry);
7717eb2bd662Svikram 	assert(prev);
7718eb2bd662Svikram 	assert(lp);
7719eb2bd662Svikram 
7720eb2bd662Svikram 	lp->next = prev->next;
7721eb2bd662Svikram 	if (prev->next) {
7722eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_PREV_NEXT, fcn));
7723eb2bd662Svikram 		prev->next->prev = lp;
7724eb2bd662Svikram 	} else {
7725eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_NOT_PREV_NEXT, fcn));
7726eb2bd662Svikram 	}
7727eb2bd662Svikram 	prev->next = lp;
7728eb2bd662Svikram 	lp->prev = prev;
7729eb2bd662Svikram 
7730eb2bd662Svikram 	if (entry->end == prev) {
7731eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_ENTRY, fcn));
7732eb2bd662Svikram 		entry->end = lp;
7733eb2bd662Svikram 	}
7734eb2bd662Svikram 	if (mp->end == prev) {
7735eb2bd662Svikram 		assert(lp->next == NULL);
7736eb2bd662Svikram 		mp->end = lp;
7737eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_MENU, fcn));
7738eb2bd662Svikram 	}
77397c478bd9Sstevel@tonic-gate }
77407c478bd9Sstevel@tonic-gate 
77418c1b6884Sszhou /*
77428c1b6884Sszhou  * look for matching bootadm entry with specified parameters
77438c1b6884Sszhou  * Here are the rules (based on existing usage):
77448c1b6884Sszhou  * - If title is specified, match on title only
7745eb2bd662Svikram  * - Else, match on root/findroot, kernel, and module.
7746eb2bd662Svikram  *   Note that, if root_opt is non-zero, the absence of
7747eb2bd662Svikram  *   root line is considered a match.
77488c1b6884Sszhou  */
77498c1b6884Sszhou static entry_t *
7750eb2bd662Svikram find_boot_entry(
7751eb2bd662Svikram 	menu_t *mp,
7752eb2bd662Svikram 	char *title,
7753eb2bd662Svikram 	char *kernel,
7754eb2bd662Svikram 	char *findroot,
7755eb2bd662Svikram 	char *root,
7756eb2bd662Svikram 	char *module,
7757eb2bd662Svikram 	int root_opt,
7758eb2bd662Svikram 	int *entry_num)
77598c1b6884Sszhou {
77608c1b6884Sszhou 	int		i;
77618c1b6884Sszhou 	line_t		*lp;
77628c1b6884Sszhou 	entry_t		*ent;
7763eb2bd662Svikram 	const char	*fcn = "find_boot_entry()";
7764eb2bd662Svikram 
7765eb2bd662Svikram 	if (entry_num)
7766eb2bd662Svikram 		*entry_num = BAM_ERROR;
77678c1b6884Sszhou 
77688c1b6884Sszhou 	/* find matching entry */
77698c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
77708c1b6884Sszhou 		lp = ent->start;
77718c1b6884Sszhou 
77728c1b6884Sszhou 		/* first line of entry must be bootadm comment */
77738c1b6884Sszhou 		lp = ent->start;
7774ae115bc7Smrj 		if (lp->flags != BAM_COMMENT ||
7775ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) {
77768c1b6884Sszhou 			continue;
77778c1b6884Sszhou 		}
77788c1b6884Sszhou 
77798c1b6884Sszhou 		/* advance to title line */
77808c1b6884Sszhou 		lp = lp->next;
77818c1b6884Sszhou 		if (title) {
77828c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
7783eb2bd662Svikram 			    strcmp(lp->arg, title) == 0) {
7784eb2bd662Svikram 				BAM_DPRINTF((D_MATCHED_TITLE, fcn, title));
77858c1b6884Sszhou 				break;
7786eb2bd662Svikram 			}
7787eb2bd662Svikram 			BAM_DPRINTF((D_NOMATCH_TITLE, fcn, title, lp->arg));
77888c1b6884Sszhou 			continue;	/* check title only */
77898c1b6884Sszhou 		}
77908c1b6884Sszhou 
77918c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
7792843e1988Sjohnlev 		if (lp == NULL) {
7793843e1988Sjohnlev 			continue;
7794eac223ccSWilliam Kucharski 		} else if (lp->cmd != NULL &&
7795eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) == 0) {
7796eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_FINDROOT",
7797eb2bd662Svikram 			    findroot = NULL);
7798eb2bd662Svikram 			if (findroot == NULL) {
7799eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_FINDROOT_NULL,
7800eb2bd662Svikram 				    fcn, lp->arg));
78018c1b6884Sszhou 				continue;
78028c1b6884Sszhou 			}
7803eb2bd662Svikram 			/* findroot command found, try match  */
7804eb2bd662Svikram 			if (strcmp(lp->arg, findroot) != 0) {
7805eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_FINDROOT,
7806eb2bd662Svikram 				    fcn, findroot, lp->arg));
7807eb2bd662Svikram 				continue;
7808eb2bd662Svikram 			}
7809eb2bd662Svikram 			BAM_DPRINTF((D_MATCHED_FINDROOT, fcn, findroot));
7810eb2bd662Svikram 			lp = lp->next;	/* advance to kernel line */
7811eac223ccSWilliam Kucharski 		} else if (lp->cmd != NULL &&
7812eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
7813eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_ROOT", root = NULL);
7814eb2bd662Svikram 			if (root == NULL) {
7815eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_ROOT_NULL,
7816eb2bd662Svikram 				    fcn, lp->arg));
7817eb2bd662Svikram 				continue;
7818eb2bd662Svikram 			}
7819eb2bd662Svikram 			/* root cmd found, try match */
7820eb2bd662Svikram 			if (strcmp(lp->arg, root) != 0) {
7821eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_ROOT,
7822eb2bd662Svikram 				    fcn, root, lp->arg));
7823eb2bd662Svikram 				continue;
7824eb2bd662Svikram 			}
7825eb2bd662Svikram 			BAM_DPRINTF((D_MATCHED_ROOT, fcn, root));
78268c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
78278c1b6884Sszhou 		} else {
7828eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_NO",
7829eb2bd662Svikram 			    root_opt = 0);
7830eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_YES",
7831eb2bd662Svikram 			    root_opt = 1);
78328c1b6884Sszhou 			/* no root command, see if root is optional */
78338c1b6884Sszhou 			if (root_opt == 0) {
7834eb2bd662Svikram 				BAM_DPRINTF((D_NO_ROOT_OPT, fcn));
78358c1b6884Sszhou 				continue;
78368c1b6884Sszhou 			}
7837eb2bd662Svikram 			BAM_DPRINTF((D_ROOT_OPT, fcn));
78388c1b6884Sszhou 		}
78398c1b6884Sszhou 
78408c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
78418c1b6884Sszhou 			continue;
78428c1b6884Sszhou 		}
78438c1b6884Sszhou 
7844843e1988Sjohnlev 		if (kernel &&
7845843e1988Sjohnlev 		    (!check_cmd(lp->cmd, KERNEL_CMD, lp->arg, kernel))) {
7846bbcc54bdSEnrico Perla - Sun Microsystems 			if (!(ent->flags & BAM_ENTRY_FAILSAFE) ||
7847bbcc54bdSEnrico Perla - Sun Microsystems 			    !(ent->flags & BAM_ENTRY_DBOOT) ||
7848bbcc54bdSEnrico Perla - Sun Microsystems 			    strcmp(kernel, DIRECT_BOOT_FAILSAFE_LINE) != 0)
78498c1b6884Sszhou 				continue;
7850bbcc54bdSEnrico Perla - Sun Microsystems 
7851bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSKERNEL;
7852bbcc54bdSEnrico Perla - Sun Microsystems 
78538c1b6884Sszhou 		}
7854eb2bd662Svikram 		BAM_DPRINTF((D_KERNEL_MATCH, fcn, kernel, lp->arg));
7855843e1988Sjohnlev 
7856843e1988Sjohnlev 		/*
7857843e1988Sjohnlev 		 * Check for matching module entry (failsafe or normal).
7858843e1988Sjohnlev 		 * If it fails to match, we go around the loop again.
7859843e1988Sjohnlev 		 * For xpv entries, there are two module lines, so we
7860843e1988Sjohnlev 		 * do the check twice.
7861843e1988Sjohnlev 		 */
7862843e1988Sjohnlev 		lp = lp->next;	/* advance to module line */
7863843e1988Sjohnlev 		if (check_cmd(lp->cmd, MODULE_CMD, lp->arg, module) ||
7864843e1988Sjohnlev 		    (((lp = lp->next) != NULL) &&
7865843e1988Sjohnlev 		    check_cmd(lp->cmd, MODULE_CMD, lp->arg, module))) {
7866843e1988Sjohnlev 			/* match found */
7867eb2bd662Svikram 			BAM_DPRINTF((D_MODULE_MATCH, fcn, module, lp->arg));
7868843e1988Sjohnlev 			break;
7869843e1988Sjohnlev 		}
7870bbcc54bdSEnrico Perla - Sun Microsystems 
7871bbcc54bdSEnrico Perla - Sun Microsystems 		if (strcmp(module, FAILSAFE_ARCHIVE) == 0 &&
7872bbcc54bdSEnrico Perla - Sun Microsystems 		    (strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_32) == 0 ||
7873bbcc54bdSEnrico Perla - Sun Microsystems 		    strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_64) == 0)) {
7874bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSMODULE;
7875bbcc54bdSEnrico Perla - Sun Microsystems 			break;
7876bbcc54bdSEnrico Perla - Sun Microsystems 		}
7877bbcc54bdSEnrico Perla - Sun Microsystems 
78788c1b6884Sszhou 	}
78798c1b6884Sszhou 
7880eb2bd662Svikram 	if (ent && entry_num) {
78818c1b6884Sszhou 		*entry_num = i;
7882843e1988Sjohnlev 	}
7883eb2bd662Svikram 
7884eb2bd662Svikram 	if (ent) {
7885eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_RET, fcn, i));
7886eb2bd662Svikram 	} else {
7887eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_RET, fcn, BAM_ERROR));
7888eb2bd662Svikram 	}
78898c1b6884Sszhou 	return (ent);
78908c1b6884Sszhou }
78918c1b6884Sszhou 
78928c1b6884Sszhou static int
7893eb2bd662Svikram update_boot_entry(menu_t *mp, char *title, char *findroot, char *root,
7894eb2bd662Svikram     char *kernel, char *mod_kernel, char *module, int root_opt)
78958c1b6884Sszhou {
7896eb2bd662Svikram 	int		i;
7897eb2bd662Svikram 	int		change_kernel = 0;
78988c1b6884Sszhou 	entry_t		*ent;
78998c1b6884Sszhou 	line_t		*lp;
7900eb2bd662Svikram 	line_t		*tlp;
79018c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
7902eb2bd662Svikram 	const char	*fcn = "update_boot_entry()";
79038c1b6884Sszhou 
79048c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
7905eb2bd662Svikram 	ent = find_boot_entry(mp, NULL, kernel, findroot, root, module,
7906eb2bd662Svikram 	    root_opt, &i);
7907ae115bc7Smrj 	if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) {
7908ae115bc7Smrj 		/*
7909ae115bc7Smrj 		 * We may be upgrading a kernel from multiboot to
7910eb2bd662Svikram 		 * directboot.  Look for a multiboot entry. A multiboot
7911eb2bd662Svikram 		 * entry will not have a findroot line.
7912ae115bc7Smrj 		 */
7913eb2bd662Svikram 		ent = find_boot_entry(mp, NULL, "multiboot", NULL, root,
7914eb2bd662Svikram 		    MULTIBOOT_ARCHIVE, root_opt, &i);
7915ae115bc7Smrj 		if (ent != NULL) {
7916eb2bd662Svikram 			BAM_DPRINTF((D_UPGRADE_FROM_MULTIBOOT, fcn, root));
7917ae115bc7Smrj 			change_kernel = 1;
7918ae115bc7Smrj 		}
7919eb2bd662Svikram 	} else if (ent) {
7920eb2bd662Svikram 		BAM_DPRINTF((D_FOUND_FINDROOT, fcn, findroot));
7921ae115bc7Smrj 	}
79228c1b6884Sszhou 
7923eb2bd662Svikram 	if (ent == NULL) {
7924eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_NOT_FOUND_CREATING, fcn, findroot));
7925eb2bd662Svikram 		return (add_boot_entry(mp, title, findroot,
792644da779fSWilliam Kucharski 		    kernel, mod_kernel, module, NULL));
7927eb2bd662Svikram 	}
7928eb2bd662Svikram 
7929eb2bd662Svikram 	/* replace title of existing entry and update findroot line */
79308c1b6884Sszhou 	lp = ent->start;
79318c1b6884Sszhou 	lp = lp->next;	/* title line */
79328c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
79338c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
79348c1b6884Sszhou 	free(lp->arg);
79358c1b6884Sszhou 	free(lp->line);
79368c1b6884Sszhou 	lp->arg = s_strdup(title);
79378c1b6884Sszhou 	lp->line = s_strdup(linebuf);
7938eb2bd662Svikram 	BAM_DPRINTF((D_CHANGING_TITLE, fcn, title));
79398c1b6884Sszhou 
7940eb2bd662Svikram 	tlp = lp;	/* title line */
79418c1b6884Sszhou 	lp = lp->next;	/* root line */
7942eb2bd662Svikram 
7943eb2bd662Svikram 	/* if no root or findroot command, create a new line_t */
7944eac223ccSWilliam Kucharski 	if ((lp->cmd != NULL) && (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) != 0 &&
7945eac223ccSWilliam Kucharski 	    strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) != 0)) {
7946eb2bd662Svikram 		lp = s_calloc(1, sizeof (line_t));
7947eb2bd662Svikram 		bam_add_line(mp, ent, tlp, lp);
7948eb2bd662Svikram 	} else {
7949eac223ccSWilliam Kucharski 		if (lp->cmd != NULL)
7950eb2bd662Svikram 			free(lp->cmd);
7951eac223ccSWilliam Kucharski 
7952eb2bd662Svikram 		free(lp->sep);
7953eb2bd662Svikram 		free(lp->arg);
7954eb2bd662Svikram 		free(lp->line);
79558c1b6884Sszhou 	}
7956ae115bc7Smrj 
7957eb2bd662Svikram 	lp->cmd = s_strdup(menu_cmds[FINDROOT_CMD]);
7958eb2bd662Svikram 	lp->sep = s_strdup(menu_cmds[SEP_CMD]);
7959eb2bd662Svikram 	lp->arg = s_strdup(findroot);
7960eb2bd662Svikram 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7961eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
7962eb2bd662Svikram 	lp->line = s_strdup(linebuf);
7963eb2bd662Svikram 	BAM_DPRINTF((D_ADDING_FINDROOT_LINE, fcn, findroot));
7964eb2bd662Svikram 
7965eb2bd662Svikram 	/* kernel line */
7966eb2bd662Svikram 	lp = lp->next;
7967eb2bd662Svikram 
7968bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSKERNEL) {
7969bbcc54bdSEnrico Perla - Sun Microsystems 		char		*params = NULL;
7970bbcc54bdSEnrico Perla - Sun Microsystems 
7971bbcc54bdSEnrico Perla - Sun Microsystems 		params = strstr(lp->line, "-s");
7972bbcc54bdSEnrico Perla - Sun Microsystems 		if (params != NULL)
7973bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s%s",
7974bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
7975bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel, params+2);
7976bbcc54bdSEnrico Perla - Sun Microsystems 		else
7977bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7978bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
7979bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel);
7980bbcc54bdSEnrico Perla - Sun Microsystems 
7981eac223ccSWilliam Kucharski 		if (lp->cmd != NULL)
7982bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->cmd);
7983eac223ccSWilliam Kucharski 
7984bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->arg);
7985bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->line);
7986bbcc54bdSEnrico Perla - Sun Microsystems 		lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
7987bbcc54bdSEnrico Perla - Sun Microsystems 		lp->arg = s_strdup(strstr(linebuf, "/"));
7988bbcc54bdSEnrico Perla - Sun Microsystems 		lp->line = s_strdup(linebuf);
7989bbcc54bdSEnrico Perla - Sun Microsystems 		ent->flags &= ~BAM_ENTRY_UPGFSKERNEL;
7990bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_ADDING_KERNEL_DOLLAR, fcn, lp->prev->cmd));
7991bbcc54bdSEnrico Perla - Sun Microsystems 	}
7992bbcc54bdSEnrico Perla - Sun Microsystems 
7993ae115bc7Smrj 	if (change_kernel) {
7994ae115bc7Smrj 		/*
7995ae115bc7Smrj 		 * We're upgrading from multiboot to directboot.
7996ae115bc7Smrj 		 */
7997eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
7998eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) {
7999ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8000ae115bc7Smrj 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
8001ae115bc7Smrj 			    kernel);
8002eb2bd662Svikram 			free(lp->cmd);
8003ae115bc7Smrj 			free(lp->arg);
8004ae115bc7Smrj 			free(lp->line);
8005eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
8006ae115bc7Smrj 			lp->arg = s_strdup(kernel);
8007ae115bc7Smrj 			lp->line = s_strdup(linebuf);
8008ae115bc7Smrj 			lp = lp->next;
8009eb2bd662Svikram 			BAM_DPRINTF((D_ADDING_KERNEL_DOLLAR, fcn, kernel));
8010ae115bc7Smrj 		}
8011eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
8012eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
8013ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8014ae115bc7Smrj 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
8015ae115bc7Smrj 			    module);
8016eb2bd662Svikram 			free(lp->cmd);
8017ae115bc7Smrj 			free(lp->arg);
8018ae115bc7Smrj 			free(lp->line);
8019eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
8020ae115bc7Smrj 			lp->arg = s_strdup(module);
8021ae115bc7Smrj 			lp->line = s_strdup(linebuf);
8022ae115bc7Smrj 			lp = lp->next;
8023eb2bd662Svikram 			BAM_DPRINTF((D_ADDING_MODULE_DOLLAR, fcn, module));
8024ae115bc7Smrj 		}
8025ae115bc7Smrj 	}
8026bbcc54bdSEnrico Perla - Sun Microsystems 
8027bbcc54bdSEnrico Perla - Sun Microsystems 	/* module line */
8028bbcc54bdSEnrico Perla - Sun Microsystems 	lp = lp->next;
8029bbcc54bdSEnrico Perla - Sun Microsystems 
8030bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSMODULE) {
8031eac223ccSWilliam Kucharski 		if (lp->cmd != NULL &&
8032eac223ccSWilliam Kucharski 		    strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
8033bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
8034bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
8035bbcc54bdSEnrico Perla - Sun Microsystems 			    module);
8036bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->cmd);
8037bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->arg);
8038bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->line);
8039bbcc54bdSEnrico Perla - Sun Microsystems 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
8040bbcc54bdSEnrico Perla - Sun Microsystems 			lp->arg = s_strdup(module);
8041bbcc54bdSEnrico Perla - Sun Microsystems 			lp->line = s_strdup(linebuf);
8042bbcc54bdSEnrico Perla - Sun Microsystems 			lp = lp->next;
8043bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags &= ~BAM_ENTRY_UPGFSMODULE;
8044bbcc54bdSEnrico Perla - Sun Microsystems 			BAM_DPRINTF((D_ADDING_MODULE_DOLLAR, fcn, module));
8045bbcc54bdSEnrico Perla - Sun Microsystems 		}
8046bbcc54bdSEnrico Perla - Sun Microsystems 	}
8047bbcc54bdSEnrico Perla - Sun Microsystems 
8048eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_RET, fcn, i));
80498c1b6884Sszhou 	return (i);
80508c1b6884Sszhou }
80518c1b6884Sszhou 
8052eb2bd662Svikram int
8053eb2bd662Svikram root_optional(char *osroot, char *menu_root)
8054eb2bd662Svikram {
8055eb2bd662Svikram 	char			*ospecial;
8056eb2bd662Svikram 	char			*mspecial;
8057eb2bd662Svikram 	char			*slash;
8058eb2bd662Svikram 	int			root_opt;
8059eb2bd662Svikram 	int			ret1;
8060eb2bd662Svikram 	int			ret2;
8061eb2bd662Svikram 	const char		*fcn = "root_optional()";
8062eb2bd662Svikram 
8063eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root));
8064eb2bd662Svikram 
8065eb2bd662Svikram 	/*
8066eb2bd662Svikram 	 * For all filesystems except ZFS, a straight compare of osroot
8067eb2bd662Svikram 	 * and menu_root will tell us if root is optional.
8068eb2bd662Svikram 	 * For ZFS, the situation is complicated by the fact that
8069eb2bd662Svikram 	 * menu_root and osroot are always different
8070eb2bd662Svikram 	 */
8071eb2bd662Svikram 	ret1 = is_zfs(osroot);
8072eb2bd662Svikram 	ret2 = is_zfs(menu_root);
8073eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPT_NOT_ZFS", ret1 = 0);
8074eb2bd662Svikram 	if (!ret1 || !ret2) {
8075eb2bd662Svikram 		BAM_DPRINTF((D_ROOT_OPT_NOT_ZFS, fcn, osroot, menu_root));
8076eb2bd662Svikram 		root_opt = (strcmp(osroot, menu_root) == 0);
8077eb2bd662Svikram 		goto out;
8078eb2bd662Svikram 	}
8079eb2bd662Svikram 
8080eb2bd662Svikram 	ospecial = get_special(osroot);
8081eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_OSPECIAL", ospecial = NULL);
8082eb2bd662Svikram 	if (ospecial == NULL) {
8083eb2bd662Svikram 		bam_error(GET_OSROOT_SPECIAL_ERR, osroot);
8084eb2bd662Svikram 		return (0);
8085eb2bd662Svikram 	}
8086eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_OSPECIAL, fcn, ospecial, osroot));
8087eb2bd662Svikram 
8088eb2bd662Svikram 	mspecial = get_special(menu_root);
8089eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_MSPECIAL", mspecial = NULL);
8090eb2bd662Svikram 	if (mspecial == NULL) {
8091eb2bd662Svikram 		bam_error(GET_MENU_ROOT_SPECIAL_ERR, menu_root);
8092eb2bd662Svikram 		free(ospecial);
8093eb2bd662Svikram 		return (0);
8094eb2bd662Svikram 	}
8095eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_MSPECIAL, fcn, mspecial, menu_root));
8096eb2bd662Svikram 
8097eb2bd662Svikram 	slash = strchr(ospecial, '/');
8098eb2bd662Svikram 	if (slash)
8099eb2bd662Svikram 		*slash = '\0';
8100eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_FIXED_OSPECIAL, fcn, ospecial, osroot));
8101eb2bd662Svikram 
8102eb2bd662Svikram 	root_opt = (strcmp(ospecial, mspecial) == 0);
8103eb2bd662Svikram 
8104eb2bd662Svikram 	free(ospecial);
8105eb2bd662Svikram 	free(mspecial);
8106eb2bd662Svikram 
8107eb2bd662Svikram out:
8108eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_NO", root_opt = 0);
8109eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_YES", root_opt = 1);
8110eb2bd662Svikram 	if (root_opt) {
8111eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8112eb2bd662Svikram 	} else {
8113eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8114eb2bd662Svikram 	}
8115eb2bd662Svikram 
8116eb2bd662Svikram 	return (root_opt);
8117eb2bd662Svikram }
8118eb2bd662Svikram 
81197c478bd9Sstevel@tonic-gate /*ARGSUSED*/
81207c478bd9Sstevel@tonic-gate static error_t
8121eb2bd662Svikram update_entry(menu_t *mp, char *menu_root, char *osdev)
81227c478bd9Sstevel@tonic-gate {
81237c478bd9Sstevel@tonic-gate 	int		entry;
8124eb2bd662Svikram 	char		*grubsign;
8125eb2bd662Svikram 	char		*grubroot;
8126eb2bd662Svikram 	char		*title;
8127eb2bd662Svikram 	char		osroot[PATH_MAX];
8128eb2bd662Svikram 	char		*failsafe_kernel = NULL;
81298c1b6884Sszhou 	struct stat	sbuf;
81308c1b6884Sszhou 	char		failsafe[256];
8131bbcc54bdSEnrico Perla - Sun Microsystems 	char		failsafe_64[256];
8132eb2bd662Svikram 	int		ret;
8133eb2bd662Svikram 	const char	*fcn = "update_entry()";
81347c478bd9Sstevel@tonic-gate 
81357c478bd9Sstevel@tonic-gate 	assert(mp);
8136eb2bd662Svikram 	assert(menu_root);
8137eb2bd662Svikram 	assert(osdev);
8138eb2bd662Svikram 	assert(bam_root);
81397c478bd9Sstevel@tonic-gate 
8140eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY3, fcn, menu_root, osdev, bam_root));
8141eb2bd662Svikram 
8142eb2bd662Svikram 	(void) strlcpy(osroot, bam_root, sizeof (osroot));
8143eb2bd662Svikram 
81447c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
8145eb2bd662Svikram 	assert(title);
81467c478bd9Sstevel@tonic-gate 
8147eb2bd662Svikram 	grubsign = get_grubsign(osroot, osdev);
8148eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FAIL", grubsign = NULL);
8149eb2bd662Svikram 	if (grubsign == NULL) {
8150eb2bd662Svikram 		bam_error(GET_GRUBSIGN_ERROR, osroot, osdev);
81517c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
81527c478bd9Sstevel@tonic-gate 	}
8153eb2bd662Svikram 
8154eb2bd662Svikram 	/*
8155eb2bd662Svikram 	 * It is not a fatal error if get_grubroot() fails
8156eb2bd662Svikram 	 * We no longer rely on biosdev to populate the
8157eb2bd662Svikram 	 * menu
8158eb2bd662Svikram 	 */
8159eb2bd662Svikram 	grubroot = get_grubroot(osroot, osdev, menu_root);
8160eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBROOT_FAIL", grubroot = NULL);
8161eb2bd662Svikram 	if (grubroot) {
8162eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBROOT_SUCCESS,
8163eb2bd662Svikram 		    fcn, osroot, osdev, menu_root));
8164eb2bd662Svikram 	} else {
8165eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBROOT_FAILURE,
8166eb2bd662Svikram 		    fcn, osroot, osdev, menu_root));
81677c478bd9Sstevel@tonic-gate 	}
81687c478bd9Sstevel@tonic-gate 
81697c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
8170eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_MULTIBOOT",
8171eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
8172ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
8173eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
8174e7cbe64fSgw25295 		    (bam_zfs ? DIRECT_BOOT_KERNEL_ZFS : DIRECT_BOOT_KERNEL),
8175eb2bd662Svikram 		    NULL, DIRECT_BOOT_ARCHIVE,
8176eb2bd662Svikram 		    root_optional(osroot, menu_root));
8177eb2bd662Svikram 		BAM_DPRINTF((D_UPDATED_BOOT_ENTRY, fcn, bam_zfs, grubsign));
8178843e1988Sjohnlev 		if ((entry != BAM_ERROR) && (bam_is_hv == BAM_HV_PRESENT)) {
8179eb2bd662Svikram 			(void) update_boot_entry(mp, NEW_HV_ENTRY, grubsign,
8180eb2bd662Svikram 			    grubroot, XEN_MENU, bam_zfs ?
8181eb2bd662Svikram 			    XEN_KERNEL_MODULE_LINE_ZFS : XEN_KERNEL_MODULE_LINE,
8182eb2bd662Svikram 			    DIRECT_BOOT_ARCHIVE,
8183eb2bd662Svikram 			    root_optional(osroot, menu_root));
8184eb2bd662Svikram 			BAM_DPRINTF((D_UPDATED_HV_ENTRY,
8185eb2bd662Svikram 			    fcn, bam_zfs, grubsign));
8186ae115bc7Smrj 		}
8187843e1988Sjohnlev 	} else {
8188eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
8189eb2bd662Svikram 		    MULTI_BOOT, NULL, MULTIBOOT_ARCHIVE,
8190eb2bd662Svikram 		    root_optional(osroot, menu_root));
8191eb2bd662Svikram 
8192eb2bd662Svikram 		BAM_DPRINTF((D_UPDATED_MULTIBOOT_ENTRY, fcn, grubsign));
8193843e1988Sjohnlev 	}
81947c478bd9Sstevel@tonic-gate 
8195843e1988Sjohnlev 	/*
8196843e1988Sjohnlev 	 * Add the entry for failsafe archive.  On a bfu'd system, the
8197843e1988Sjohnlev 	 * failsafe may be different than the installed kernel.
8198843e1988Sjohnlev 	 */
8199eb2bd662Svikram 	(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
8200bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_32);
8201bbcc54bdSEnrico Perla - Sun Microsystems 	(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
8202bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_64);
8203bbcc54bdSEnrico Perla - Sun Microsystems 
8204bbcc54bdSEnrico Perla - Sun Microsystems 	/*
8205bbcc54bdSEnrico Perla - Sun Microsystems 	 * Check if at least one of the two archives exists
8206bbcc54bdSEnrico Perla - Sun Microsystems 	 * Using $ISADIR as the default line, we have an entry which works
8207bbcc54bdSEnrico Perla - Sun Microsystems 	 * for both the cases.
8208bbcc54bdSEnrico Perla - Sun Microsystems 	 */
8209bbcc54bdSEnrico Perla - Sun Microsystems 
8210bbcc54bdSEnrico Perla - Sun Microsystems 	if (stat(failsafe, &sbuf) == 0 || stat(failsafe_64, &sbuf) == 0) {
821160d0a590Srscott 
821260d0a590Srscott 		/* Figure out where the kernel line should point */
821360d0a590Srscott 		(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot,
8214bbcc54bdSEnrico Perla - Sun Microsystems 		    DIRECT_BOOT_FAILSAFE_32);
8215bbcc54bdSEnrico Perla - Sun Microsystems 		(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
8216bbcc54bdSEnrico Perla - Sun Microsystems 		    osroot, DIRECT_BOOT_FAILSAFE_64);
8217bbcc54bdSEnrico Perla - Sun Microsystems 		if (stat(failsafe, &sbuf) == 0 ||
8218bbcc54bdSEnrico Perla - Sun Microsystems 		    stat(failsafe_64, &sbuf) == 0) {
8219963390b4Svikram 			failsafe_kernel = DIRECT_BOOT_FAILSAFE_LINE;
822060d0a590Srscott 		} else {
822160d0a590Srscott 			(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
822260d0a590Srscott 			    osroot, MULTI_BOOT_FAILSAFE);
822360d0a590Srscott 			if (stat(failsafe, &sbuf) == 0) {
822460d0a590Srscott 				failsafe_kernel = MULTI_BOOT_FAILSAFE_LINE;
822560d0a590Srscott 			}
822660d0a590Srscott 		}
822760d0a590Srscott 		if (failsafe_kernel != NULL) {
8228eb2bd662Svikram 			(void) update_boot_entry(mp, FAILSAFE_TITLE, grubsign,
8229eb2bd662Svikram 			    grubroot, failsafe_kernel, NULL, FAILSAFE_ARCHIVE,
8230eb2bd662Svikram 			    root_optional(osroot, menu_root));
8231eb2bd662Svikram 			BAM_DPRINTF((D_UPDATED_FAILSAFE_ENTRY, fcn,
8232eb2bd662Svikram 			    failsafe_kernel));
823360d0a590Srscott 		}
8234ae115bc7Smrj 	}
8235eb2bd662Svikram 	free(grubroot);
82367c478bd9Sstevel@tonic-gate 
8237eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_ERROR", entry = BAM_ERROR);
82387c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
8239eb2bd662Svikram 		bam_error(FAILED_TO_ADD_BOOT_ENTRY, title, grubsign);
8240eb2bd662Svikram 		free(grubsign);
82417c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
82427c478bd9Sstevel@tonic-gate 	}
8243eb2bd662Svikram 	free(grubsign);
8244eb2bd662Svikram 
8245eb2bd662Svikram 	update_numbering(mp);
8246eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8247eb2bd662Svikram 	INJECT_ERROR1("SET_DEFAULT_ERROR", ret = BAM_ERROR);
8248eb2bd662Svikram 	if (ret == BAM_ERROR) {
8249eb2bd662Svikram 		bam_error(SET_DEFAULT_FAILED, entry);
8250eb2bd662Svikram 	}
8251eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
82527c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
82537c478bd9Sstevel@tonic-gate }
82547c478bd9Sstevel@tonic-gate 
82558c1b6884Sszhou static void
8256ae115bc7Smrj save_default_entry(menu_t *mp, const char *which)
82578c1b6884Sszhou {
8258eb2bd662Svikram 	int		lineNum;
8259eb2bd662Svikram 	int		entryNum;
82608c1b6884Sszhou 	int		entry = 0;	/* default is 0 */
82618c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
82628c1b6884Sszhou 	line_t		*lp = mp->curdefault;
8263eb2bd662Svikram 	const char	*fcn = "save_default_entry()";
82648c1b6884Sszhou 
8265bff269d0Svikram 	if (mp->start) {
8266bff269d0Svikram 		lineNum = mp->end->lineNum;
8267bff269d0Svikram 		entryNum = mp->end->entryNum;
8268bff269d0Svikram 	} else {
8269bff269d0Svikram 		lineNum = LINE_INIT;
8270bff269d0Svikram 		entryNum = ENTRY_INIT;
8271bff269d0Svikram 	}
8272bff269d0Svikram 
82738c1b6884Sszhou 	if (lp)
82748c1b6884Sszhou 		entry = s_strtol(lp->arg);
82758c1b6884Sszhou 
8276ae115bc7Smrj 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry);
8277eb2bd662Svikram 	BAM_DPRINTF((D_SAVING_DEFAULT_TO, fcn, linebuf));
82788c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
8279eb2bd662Svikram 	BAM_DPRINTF((D_SAVED_DEFAULT_TO, fcn, lineNum, entryNum));
82808c1b6884Sszhou }
82818c1b6884Sszhou 
82828c1b6884Sszhou static void
8283ae115bc7Smrj restore_default_entry(menu_t *mp, const char *which, line_t *lp)
82848c1b6884Sszhou {
82858c1b6884Sszhou 	int		entry;
82868c1b6884Sszhou 	char		*str;
8287eb2bd662Svikram 	const char	*fcn = "restore_default_entry()";
82888c1b6884Sszhou 
8289eb2bd662Svikram 	if (lp == NULL) {
8290eb2bd662Svikram 		BAM_DPRINTF((D_RESTORE_DEFAULT_NULL, fcn));
82918c1b6884Sszhou 		return;		/* nothing to restore */
8292eb2bd662Svikram 	}
8293eb2bd662Svikram 
8294eb2bd662Svikram 	BAM_DPRINTF((D_RESTORE_DEFAULT_STR, fcn, which));
82958c1b6884Sszhou 
8296ae115bc7Smrj 	str = lp->arg + strlen(which);
82978c1b6884Sszhou 	entry = s_strtol(str);
82988c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
82998c1b6884Sszhou 
8300eb2bd662Svikram 	BAM_DPRINTF((D_RESTORED_DEFAULT_TO, fcn, entry));
8301eb2bd662Svikram 
83028c1b6884Sszhou 	/* delete saved old default line */
83038c1b6884Sszhou 	unlink_line(mp, lp);
83048c1b6884Sszhou 	line_free(lp);
83058c1b6884Sszhou }
83068c1b6884Sszhou 
83077c478bd9Sstevel@tonic-gate /*
83087c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
83097c478bd9Sstevel@tonic-gate  * The opt value can be:
83107c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
8311eb2bd662Svikram  * entry=<n>	switches default entry to <n>
83127c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
83137c478bd9Sstevel@tonic-gate  *		and make it the default
8314eb2bd662Svikram  * Note that we are always rebooting the current OS instance
8315eb2bd662Svikram  * so osroot == / always.
83167c478bd9Sstevel@tonic-gate  */
83177c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
83187c478bd9Sstevel@tonic-gate 
83198c1b6884Sszhou /*ARGSUSED*/
83207c478bd9Sstevel@tonic-gate static error_t
8321eb2bd662Svikram update_temp(menu_t *mp, char *dummy, char *opt)
83227c478bd9Sstevel@tonic-gate {
83237c478bd9Sstevel@tonic-gate 	int		entry;
8324eb2bd662Svikram 	char		*osdev;
8325eb2bd662Svikram 	char		*fstype;
8326eb2bd662Svikram 	char		*sign;
8327eb2bd662Svikram 	char		*opt_ptr;
8328eb2bd662Svikram 	char		*path;
8329ae115bc7Smrj 	char		kernbuf[BUFSIZ];
8330ae115bc7Smrj 	char		args_buf[BUFSIZ];
8331eb2bd662Svikram 	char		signbuf[PATH_MAX];
8332eb2bd662Svikram 	int		ret;
8333eb2bd662Svikram 	const char	*fcn = "update_temp()";
83347c478bd9Sstevel@tonic-gate 
83357c478bd9Sstevel@tonic-gate 	assert(mp);
8336eb2bd662Svikram 	assert(dummy == NULL);
8337eb2bd662Svikram 
8338eb2bd662Svikram 	/* opt can be NULL */
8339eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt ? opt : "<NULL>"));
8340eb2bd662Svikram 	BAM_DPRINTF((D_BAM_ROOT, fcn, bam_alt_root, bam_root));
8341eb2bd662Svikram 
8342eb2bd662Svikram 	if (bam_alt_root || bam_rootlen != 1 ||
8343eb2bd662Svikram 	    strcmp(bam_root, "/") != 0 ||
8344eb2bd662Svikram 	    strcmp(rootbuf, "/") != 0) {
8345eb2bd662Svikram 		bam_error(ALT_ROOT_INVALID, bam_root);
8346eb2bd662Svikram 		return (BAM_ERROR);
8347eb2bd662Svikram 	}
83487c478bd9Sstevel@tonic-gate 
83498c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
83508c1b6884Sszhou 	if (opt == NULL) {
8351eb2bd662Svikram 		entry_t		*ent;
8352eb2bd662Svikram 		BAM_DPRINTF((D_OPT_NULL, fcn));
8353eb2bd662Svikram 		ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
8354eb2bd662Svikram 		    NULL, NULL, 0, &entry);
8355eb2bd662Svikram 		if (ent == NULL) {	/* not found is ok */
8356eb2bd662Svikram 			BAM_DPRINTF((D_TRANSIENT_NOTFOUND, fcn));
83578c1b6884Sszhou 			return (BAM_SUCCESS);
8358eb2bd662Svikram 		}
835944da779fSWilliam Kucharski 		(void) delete_boot_entry(mp, entry, DBE_PRINTERR);
8360ae115bc7Smrj 		restore_default_entry(mp, BAM_OLDDEF, mp->olddefault);
8361ae115bc7Smrj 		mp->olddefault = NULL;
8362eb2bd662Svikram 		BAM_DPRINTF((D_RESTORED_DEFAULT, fcn));
8363eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
83648c1b6884Sszhou 		return (BAM_WRITE);
83658c1b6884Sszhou 	}
83668c1b6884Sszhou 
83678c1b6884Sszhou 	/* if entry= is specified, set the default entry */
8368eb2bd662Svikram 	if (strncmp(opt, "entry=", strlen("entry=")) == 0) {
8369eb2bd662Svikram 		int entryNum = s_strtol(opt + strlen("entry="));
8370eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_EQUALS, fcn, opt));
8371eb2bd662Svikram 		if (selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
83727c478bd9Sstevel@tonic-gate 			/* this is entry=# option */
8373eb2bd662Svikram 			ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8374eb2bd662Svikram 			BAM_DPRINTF((D_ENTRY_SET_IS, fcn, entry, ret));
8375eb2bd662Svikram 			return (ret);
8376eb2bd662Svikram 		} else {
8377eb2bd662Svikram 			bam_error(SET_DEFAULT_FAILED, entryNum);
8378eb2bd662Svikram 			return (BAM_ERROR);
8379eb2bd662Svikram 		}
83807c478bd9Sstevel@tonic-gate 	}
83817c478bd9Sstevel@tonic-gate 
83827c478bd9Sstevel@tonic-gate 	/*
8383eb2bd662Svikram 	 * add a new menu entry based on opt and make it the default
8384b610f78eSvikram 	 */
8385eb2bd662Svikram 
8386eb2bd662Svikram 	fstype = get_fstype("/");
8387eb2bd662Svikram 	INJECT_ERROR1("REBOOT_FSTYPE_NULL", fstype = NULL);
8388eb2bd662Svikram 	if (fstype == NULL) {
8389eb2bd662Svikram 		bam_error(REBOOT_FSTYPE_FAILED);
8390eb2bd662Svikram 		return (BAM_ERROR);
83917c478bd9Sstevel@tonic-gate 	}
8392eb2bd662Svikram 
8393eb2bd662Svikram 	osdev = get_special("/");
8394eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SPECIAL_NULL", osdev = NULL);
8395eb2bd662Svikram 	if (osdev == NULL) {
8396eb2bd662Svikram 		free(fstype);
8397eb2bd662Svikram 		bam_error(REBOOT_SPECIAL_FAILED);
8398eb2bd662Svikram 		return (BAM_ERROR);
8399b610f78eSvikram 	}
8400eb2bd662Svikram 
8401eb2bd662Svikram 	sign = find_existing_sign("/", osdev, fstype);
8402eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SIGN_NULL", sign = NULL);
8403eb2bd662Svikram 	if (sign == NULL) {
8404eb2bd662Svikram 		free(fstype);
8405eb2bd662Svikram 		free(osdev);
8406eb2bd662Svikram 		bam_error(REBOOT_SIGN_FAILED);
8407eb2bd662Svikram 		return (BAM_ERROR);
8408eb2bd662Svikram 	}
8409eb2bd662Svikram 
8410eb2bd662Svikram 	free(osdev);
8411eb2bd662Svikram 	(void) strlcpy(signbuf, sign, sizeof (signbuf));
8412eb2bd662Svikram 	free(sign);
8413eb2bd662Svikram 
8414eb2bd662Svikram 	assert(strchr(signbuf, '(') == NULL && strchr(signbuf, ',') == NULL &&
8415eb2bd662Svikram 	    strchr(signbuf, ')') == NULL);
8416eb2bd662Svikram 
8417eb2bd662Svikram 	/*
8418eb2bd662Svikram 	 * There is no alternate root while doing reboot with args
8419eb2bd662Svikram 	 * This version of bootadm is only delivered with a DBOOT
8420eb2bd662Svikram 	 * version of Solaris.
8421eb2bd662Svikram 	 */
8422eb2bd662Svikram 	INJECT_ERROR1("REBOOT_NOT_DBOOT", bam_direct = BAM_DIRECT_MULTIBOOT);
8423eb2bd662Svikram 	if (bam_direct != BAM_DIRECT_DBOOT) {
84249bd6d269SLin Ling 		free(fstype);
8425eb2bd662Svikram 		bam_error(REBOOT_DIRECT_FAILED);
84267c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
84277c478bd9Sstevel@tonic-gate 	}
84287c478bd9Sstevel@tonic-gate 
84297c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
8430ae115bc7Smrj 	if (opt[0] == '-') {
8431ae115bc7Smrj 		/* It's an option - first see if boot-file is set */
8432eb2bd662Svikram 		ret = get_kernel(mp, KERNEL_CMD, kernbuf, sizeof (kernbuf));
8433eb2bd662Svikram 		INJECT_ERROR1("REBOOT_GET_KERNEL", ret = BAM_ERROR);
8434eb2bd662Svikram 		if (ret != BAM_SUCCESS) {
84359bd6d269SLin Ling 			free(fstype);
8436eb2bd662Svikram 			bam_error(REBOOT_GET_KERNEL_FAILED);
8437ae115bc7Smrj 			return (BAM_ERROR);
8438eb2bd662Svikram 		}
8439ae115bc7Smrj 		if (kernbuf[0] == '\0')
8440eb2bd662Svikram 			(void) strlcpy(kernbuf, DIRECT_BOOT_KERNEL,
8441eb2bd662Svikram 			    sizeof (kernbuf));
84429bd6d269SLin Ling 		/*
84439bd6d269SLin Ling 		 * If this is a zfs file system and kernbuf does not
84449bd6d269SLin Ling 		 * have "-B $ZFS-BOOTFS" string yet, add it.
84459bd6d269SLin Ling 		 */
84469bd6d269SLin Ling 		if (strcmp(fstype, "zfs") == 0 && !strstr(kernbuf, ZFS_BOOT)) {
84479bd6d269SLin Ling 			(void) strlcat(kernbuf, " ", sizeof (kernbuf));
84489bd6d269SLin Ling 			(void) strlcat(kernbuf, ZFS_BOOT, sizeof (kernbuf));
84499bd6d269SLin Ling 		}
8450eb2bd662Svikram 		(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8451eb2bd662Svikram 		(void) strlcat(kernbuf, opt, sizeof (kernbuf));
8452eb2bd662Svikram 		BAM_DPRINTF((D_REBOOT_OPTION, fcn, kernbuf));
8453ae115bc7Smrj 	} else if (opt[0] == '/') {
8454455710d3Srscott 		/* It's a full path, so write it out. */
8455eb2bd662Svikram 		(void) strlcpy(kernbuf, opt, sizeof (kernbuf));
8456455710d3Srscott 
8457455710d3Srscott 		/*
8458455710d3Srscott 		 * If someone runs:
8459455710d3Srscott 		 *
8460455710d3Srscott 		 *	# eeprom boot-args='-kd'
8461455710d3Srscott 		 *	# reboot /platform/i86pc/kernel/unix
8462455710d3Srscott 		 *
8463455710d3Srscott 		 * we want to use the boot-args as part of the boot
8464455710d3Srscott 		 * line.  On the other hand, if someone runs:
8465455710d3Srscott 		 *
8466455710d3Srscott 		 *	# reboot "/platform/i86pc/kernel/unix -kd"
8467455710d3Srscott 		 *
8468455710d3Srscott 		 * we don't need to mess with boot-args.  If there's
8469455710d3Srscott 		 * no space in the options string, assume we're in the
8470455710d3Srscott 		 * first case.
8471455710d3Srscott 		 */
8472455710d3Srscott 		if (strchr(opt, ' ') == NULL) {
8473eb2bd662Svikram 			ret = get_kernel(mp, ARGS_CMD, args_buf,
8474eb2bd662Svikram 			    sizeof (args_buf));
8475eb2bd662Svikram 			INJECT_ERROR1("REBOOT_GET_ARGS", ret = BAM_ERROR);
8476eb2bd662Svikram 			if (ret != BAM_SUCCESS) {
84779bd6d269SLin Ling 				free(fstype);
8478eb2bd662Svikram 				bam_error(REBOOT_GET_ARGS_FAILED);
8479455710d3Srscott 				return (BAM_ERROR);
8480eb2bd662Svikram 			}
8481455710d3Srscott 
8482455710d3Srscott 			if (args_buf[0] != '\0') {
8483eb2bd662Svikram 				(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8484455710d3Srscott 				(void) strlcat(kernbuf, args_buf,
8485eb2bd662Svikram 				    sizeof (kernbuf));
8486455710d3Srscott 			}
8487455710d3Srscott 		}
8488eb2bd662Svikram 		BAM_DPRINTF((D_REBOOT_ABSPATH, fcn, kernbuf));
8489ae115bc7Smrj 	} else {
8490455710d3Srscott 		/*
8491455710d3Srscott 		 * It may be a partial path, or it may be a partial
8492455710d3Srscott 		 * path followed by options.  Assume that only options
8493455710d3Srscott 		 * follow a space.  If someone sends us a kernel path
8494455710d3Srscott 		 * that includes a space, they deserve to be broken.
8495455710d3Srscott 		 */
8496455710d3Srscott 		opt_ptr = strchr(opt, ' ');
8497455710d3Srscott 		if (opt_ptr != NULL) {
8498455710d3Srscott 			*opt_ptr = '\0';
8499455710d3Srscott 		}
8500455710d3Srscott 
8501ae115bc7Smrj 		path = expand_path(opt);
8502ae115bc7Smrj 		if (path != NULL) {
8503eb2bd662Svikram 			(void) strlcpy(kernbuf, path, sizeof (kernbuf));
8504ae115bc7Smrj 			free(path);
8505455710d3Srscott 
8506455710d3Srscott 			/*
8507455710d3Srscott 			 * If there were options given, use those.
8508455710d3Srscott 			 * Otherwise, copy over the default options.
8509455710d3Srscott 			 */
8510455710d3Srscott 			if (opt_ptr != NULL) {
8511455710d3Srscott 				/* Restore the space in opt string */
8512455710d3Srscott 				*opt_ptr = ' ';
8513455710d3Srscott 				(void) strlcat(kernbuf, opt_ptr,
8514eb2bd662Svikram 				    sizeof (kernbuf));
8515455710d3Srscott 			} else {
8516eb2bd662Svikram 				ret = get_kernel(mp, ARGS_CMD, args_buf,
8517eb2bd662Svikram 				    sizeof (args_buf));
8518eb2bd662Svikram 				INJECT_ERROR1("UPDATE_TEMP_PARTIAL_ARGS",
8519eb2bd662Svikram 				    ret = BAM_ERROR);
8520eb2bd662Svikram 				if (ret != BAM_SUCCESS) {
85219bd6d269SLin Ling 					free(fstype);
8522eb2bd662Svikram 					bam_error(REBOOT_GET_ARGS_FAILED);
8523ae115bc7Smrj 					return (BAM_ERROR);
8524eb2bd662Svikram 				}
8525ae115bc7Smrj 
8526ae115bc7Smrj 				if (args_buf[0] != '\0') {
8527ae115bc7Smrj 					(void) strlcat(kernbuf, " ",
8528eb2bd662Svikram 					    sizeof (kernbuf));
8529ae115bc7Smrj 					(void) strlcat(kernbuf,
8530eb2bd662Svikram 					    args_buf, sizeof (kernbuf));
8531ae115bc7Smrj 				}
8532ae115bc7Smrj 			}
8533eb2bd662Svikram 			BAM_DPRINTF((D_REBOOT_RESOLVED_PARTIAL, fcn, kernbuf));
8534455710d3Srscott 		} else {
85359bd6d269SLin Ling 			free(fstype);
8536455710d3Srscott 			bam_error(UNKNOWN_KERNEL, opt);
8537455710d3Srscott 			bam_print_stderr(UNKNOWN_KERNEL_REBOOT);
8538455710d3Srscott 			return (BAM_ERROR);
8539ae115bc7Smrj 		}
8540ae115bc7Smrj 	}
85419bd6d269SLin Ling 	free(fstype);
8542eb2bd662Svikram 	entry = add_boot_entry(mp, REBOOT_TITLE, signbuf, kernbuf,
854344da779fSWilliam Kucharski 	    NULL, NULL, NULL);
8544eb2bd662Svikram 	INJECT_ERROR1("REBOOT_ADD_BOOT_ENTRY", entry = BAM_ERROR);
85457c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
8546eb2bd662Svikram 		bam_error(REBOOT_WITH_ARGS_ADD_ENTRY_FAILED);
85477c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
85487c478bd9Sstevel@tonic-gate 	}
85498c1b6884Sszhou 
8550ae115bc7Smrj 	save_default_entry(mp, BAM_OLDDEF);
8551eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8552eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SET_GLOBAL", ret = BAM_ERROR);
8553eb2bd662Svikram 	if (ret == BAM_ERROR) {
8554eb2bd662Svikram 		bam_error(REBOOT_SET_DEFAULT_FAILED, entry);
8555eb2bd662Svikram 	}
8556eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
85577c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
85587c478bd9Sstevel@tonic-gate }
85597c478bd9Sstevel@tonic-gate 
856044da779fSWilliam Kucharski error_t
85617c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
85627c478bd9Sstevel@tonic-gate {
8563eb2bd662Svikram 	line_t		*lp;
8564eb2bd662Svikram 	line_t		*found;
8565eb2bd662Svikram 	line_t		*last;
8566eb2bd662Svikram 	char		*cp;
8567eb2bd662Svikram 	char		*str;
85687c478bd9Sstevel@tonic-gate 	char		prefix[BAM_MAXLINE];
85697c478bd9Sstevel@tonic-gate 	size_t		len;
8570eb2bd662Svikram 	const char	*fcn = "set_global()";
85717c478bd9Sstevel@tonic-gate 
85727c478bd9Sstevel@tonic-gate 	assert(mp);
85737c478bd9Sstevel@tonic-gate 	assert(globalcmd);
85747c478bd9Sstevel@tonic-gate 
8575b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
8576eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_NEG", val = -1);
8577eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_MENU_EMPTY", mp->end = NULL);
8578eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_TOO_BIG", val = 100);
8579b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
8580b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
8581b610f78eSvikram 			bam_error(INVALID_ENTRY, prefix);
8582b610f78eSvikram 			return (BAM_ERROR);
8583b610f78eSvikram 		}
8584b610f78eSvikram 	}
8585b610f78eSvikram 
85867c478bd9Sstevel@tonic-gate 	found = last = NULL;
85877c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
85887c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
85897c478bd9Sstevel@tonic-gate 			continue;
85907c478bd9Sstevel@tonic-gate 
85917c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
85927c478bd9Sstevel@tonic-gate 
8593eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_NULL_CMD", lp->cmd = NULL);
85947c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
85957c478bd9Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
85967c478bd9Sstevel@tonic-gate 			continue;
85977c478bd9Sstevel@tonic-gate 		}
85987c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
85997c478bd9Sstevel@tonic-gate 			continue;
86007c478bd9Sstevel@tonic-gate 
8601eb2bd662Svikram 		BAM_DPRINTF((D_FOUND_GLOBAL, fcn, globalcmd));
8602eb2bd662Svikram 
86037c478bd9Sstevel@tonic-gate 		if (found) {
86047c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
86057c478bd9Sstevel@tonic-gate 		}
86067c478bd9Sstevel@tonic-gate 		found = lp;
86077c478bd9Sstevel@tonic-gate 	}
86087c478bd9Sstevel@tonic-gate 
86097c478bd9Sstevel@tonic-gate 	if (found == NULL) {
86107c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
86117c478bd9Sstevel@tonic-gate 		if (last == NULL) {
86127c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
86137c478bd9Sstevel@tonic-gate 			mp->start = lp;
86147c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
86157c478bd9Sstevel@tonic-gate 		} else {
86167c478bd9Sstevel@tonic-gate 			lp->next = last->next;
86177c478bd9Sstevel@tonic-gate 			last->next = lp;
86187c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
86197c478bd9Sstevel@tonic-gate 				mp->end = lp;
86207c478bd9Sstevel@tonic-gate 		}
86217c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
86227c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
86237c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
86247c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
86257c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
86267c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
8627eb2bd662Svikram 		BAM_DPRINTF((D_SET_GLOBAL_WROTE_NEW, fcn, lp->line));
8628eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
86297c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
86307c478bd9Sstevel@tonic-gate 	}
86317c478bd9Sstevel@tonic-gate 
86327c478bd9Sstevel@tonic-gate 	/*
86337c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
86347c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
86357c478bd9Sstevel@tonic-gate 	 * readability.
86367c478bd9Sstevel@tonic-gate 	 */
86377c478bd9Sstevel@tonic-gate 	str = found->line;
86387c478bd9Sstevel@tonic-gate 	cp = prefix;
86397c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
86407c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
86417c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
86427c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
86437c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
86447c478bd9Sstevel@tonic-gate 
86457c478bd9Sstevel@tonic-gate 	free(found->line);
86467c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
86477c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
86487c478bd9Sstevel@tonic-gate 	    "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
86497c478bd9Sstevel@tonic-gate 
8650eb2bd662Svikram 	BAM_DPRINTF((D_SET_GLOBAL_REPLACED, fcn, found->line));
8651eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
86527c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
86537c478bd9Sstevel@tonic-gate }
86547c478bd9Sstevel@tonic-gate 
8655ae115bc7Smrj /*
8656ae115bc7Smrj  * partial_path may be anything like "kernel/unix" or "kmdb".  Try to
8657455710d3Srscott  * expand it to a full unix path.  The calling function is expected to
8658455710d3Srscott  * output a message if an error occurs and NULL is returned.
8659ae115bc7Smrj  */
8660ae115bc7Smrj static char *
8661ae115bc7Smrj expand_path(const char *partial_path)
8662ae115bc7Smrj {
8663ae115bc7Smrj 	int		new_path_len;
8664eb2bd662Svikram 	char		*new_path;
8665eb2bd662Svikram 	char		new_path2[PATH_MAX];
8666ae115bc7Smrj 	struct stat	sb;
8667eb2bd662Svikram 	const char	*fcn = "expand_path()";
8668ae115bc7Smrj 
8669ae115bc7Smrj 	new_path_len = strlen(partial_path) + 64;
8670ae115bc7Smrj 	new_path = s_calloc(1, new_path_len);
8671ae115bc7Smrj 
8672ae115bc7Smrj 	/* First, try the simplest case - something like "kernel/unix" */
8673ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s",
8674ae115bc7Smrj 	    partial_path);
8675ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
8676eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
8677ae115bc7Smrj 		return (new_path);
8678ae115bc7Smrj 	}
8679ae115bc7Smrj 
8680ae115bc7Smrj 	if (strcmp(partial_path, "kmdb") == 0) {
8681ae115bc7Smrj 		(void) snprintf(new_path, new_path_len, "%s -k",
8682ae115bc7Smrj 		    DIRECT_BOOT_KERNEL);
8683eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
8684ae115bc7Smrj 		return (new_path);
8685ae115bc7Smrj 	}
8686ae115bc7Smrj 
8687ae115bc7Smrj 	/*
8688ae115bc7Smrj 	 * We've quickly reached unsupported usage.  Try once more to
8689ae115bc7Smrj 	 * see if we were just given a glom name.
8690ae115bc7Smrj 	 */
8691ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix",
8692ae115bc7Smrj 	    partial_path);
8693ae115bc7Smrj 	(void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix",
8694ae115bc7Smrj 	    partial_path);
8695ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
8696ae115bc7Smrj 		if (stat(new_path2, &sb) == 0) {
8697ae115bc7Smrj 			/*
8698ae115bc7Smrj 			 * We matched both, so we actually
8699ae115bc7Smrj 			 * want to write the $ISADIR version.
8700ae115bc7Smrj 			 */
8701ae115bc7Smrj 			(void) snprintf(new_path, new_path_len,
8702ae115bc7Smrj 			    "/platform/i86pc/kernel/%s/$ISADIR/unix",
8703ae115bc7Smrj 			    partial_path);
8704ae115bc7Smrj 		}
8705eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
8706ae115bc7Smrj 		return (new_path);
8707ae115bc7Smrj 	}
8708ae115bc7Smrj 
8709ae115bc7Smrj 	free(new_path);
8710eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8711ae115bc7Smrj 	return (NULL);
8712ae115bc7Smrj }
8713ae115bc7Smrj 
8714ae115bc7Smrj /*
8715ae115bc7Smrj  * The kernel cmd and arg have been changed, so
8716ae115bc7Smrj  * check whether the archive line needs to change.
8717ae115bc7Smrj  */
8718ae115bc7Smrj static void
8719ae115bc7Smrj set_archive_line(entry_t *entryp, line_t *kernelp)
8720ae115bc7Smrj {
8721ae115bc7Smrj 	line_t		*lp = entryp->start;
8722ae115bc7Smrj 	char		*new_archive;
8723ae115bc7Smrj 	menu_cmd_t	m_cmd;
8724eb2bd662Svikram 	const char	*fcn = "set_archive_line()";
8725ae115bc7Smrj 
8726ae115bc7Smrj 	for (; lp != NULL; lp = lp->next) {
8727eac223ccSWilliam Kucharski 		if (lp->cmd != NULL && strncmp(lp->cmd, menu_cmds[MODULE_CMD],
8728ae115bc7Smrj 		    sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) {
8729ae115bc7Smrj 			break;
8730ae115bc7Smrj 		}
8731eb2bd662Svikram 
8732eb2bd662Svikram 		INJECT_ERROR1("SET_ARCHIVE_LINE_END_ENTRY", lp = entryp->end);
8733eb2bd662Svikram 		if (lp == entryp->end) {
8734eb2bd662Svikram 			BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn,
8735eb2bd662Svikram 			    entryp->entryNum));
8736ae115bc7Smrj 			return;
8737ae115bc7Smrj 		}
8738eb2bd662Svikram 	}
8739eb2bd662Svikram 	INJECT_ERROR1("SET_ARCHIVE_LINE_END_MENU", lp = NULL);
8740eb2bd662Svikram 	if (lp == NULL) {
8741eb2bd662Svikram 		BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn, entryp->entryNum));
8742ae115bc7Smrj 		return;
8743eb2bd662Svikram 	}
8744ae115bc7Smrj 
8745ae115bc7Smrj 	if (strstr(kernelp->arg, "$ISADIR") != NULL) {
8746ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE;
8747ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
8748ae115bc7Smrj 	} else if (strstr(kernelp->arg, "amd64") != NULL) {
8749ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_64;
8750ae115bc7Smrj 		m_cmd = MODULE_CMD;
8751ae115bc7Smrj 	} else {
8752ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_32;
8753ae115bc7Smrj 		m_cmd = MODULE_CMD;
8754ae115bc7Smrj 	}
8755ae115bc7Smrj 
8756eb2bd662Svikram 	if (strcmp(lp->arg, new_archive) == 0) {
8757eb2bd662Svikram 		BAM_DPRINTF((D_ARCHIVE_LINE_NOCHANGE, fcn, lp->arg));
8758ae115bc7Smrj 		return;
8759eb2bd662Svikram 	}
8760ae115bc7Smrj 
8761eac223ccSWilliam Kucharski 	if (lp->cmd != NULL && strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) {
8762ae115bc7Smrj 		free(lp->cmd);
8763ae115bc7Smrj 		lp->cmd = s_strdup(menu_cmds[m_cmd]);
8764ae115bc7Smrj 	}
8765ae115bc7Smrj 
8766ae115bc7Smrj 	free(lp->arg);
8767ae115bc7Smrj 	lp->arg = s_strdup(new_archive);
8768ae115bc7Smrj 	update_line(lp);
8769eb2bd662Svikram 	BAM_DPRINTF((D_ARCHIVE_LINE_REPLACED, fcn, lp->line));
8770ae115bc7Smrj }
8771ae115bc7Smrj 
8772ae115bc7Smrj /*
8773ae115bc7Smrj  * Title for an entry to set properties that once went in bootenv.rc.
8774ae115bc7Smrj  */
8775ae115bc7Smrj #define	BOOTENV_RC_TITLE	"Solaris bootenv rc"
8776ae115bc7Smrj 
8777ae115bc7Smrj /*
8778ae115bc7Smrj  * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments
8779ae115bc7Smrj  * (optnum == ARGS_CMD) in the argument buf.  If path is a zero-length
8780ae115bc7Smrj  * string, reset the value to the default.  If path is a non-zero-length
8781ae115bc7Smrj  * string, set the kernel or arguments.
8782ae115bc7Smrj  */
8783ae115bc7Smrj static error_t
8784eb2bd662Svikram get_set_kernel(
8785eb2bd662Svikram 	menu_t *mp,
8786eb2bd662Svikram 	menu_cmd_t optnum,
8787eb2bd662Svikram 	char *path,
8788eb2bd662Svikram 	char *buf,
8789eb2bd662Svikram 	size_t bufsize)
8790ae115bc7Smrj {
8791eb2bd662Svikram 	int		entryNum;
8792eb2bd662Svikram 	int		rv = BAM_SUCCESS;
8793eb2bd662Svikram 	int		free_new_path = 0;
8794ae115bc7Smrj 	entry_t		*entryp;
8795eb2bd662Svikram 	line_t		*ptr;
8796eb2bd662Svikram 	line_t		*kernelp;
8797eb2bd662Svikram 	char		*new_arg;
8798eb2bd662Svikram 	char		*old_args;
8799eb2bd662Svikram 	char		*space;
8800eb2bd662Svikram 	char		*new_path;
8801ae115bc7Smrj 	char		old_space;
8802eb2bd662Svikram 	size_t		old_kernel_len;
8803eb2bd662Svikram 	size_t		new_str_len;
8804eb2bd662Svikram 	char		*fstype;
8805eb2bd662Svikram 	char		*osdev;
8806eb2bd662Svikram 	char		*sign;
8807eb2bd662Svikram 	char		signbuf[PATH_MAX];
8808eb2bd662Svikram 	int		ret;
8809eb2bd662Svikram 	const char	*fcn = "get_set_kernel()";
8810ae115bc7Smrj 
8811ae115bc7Smrj 	assert(bufsize > 0);
8812ae115bc7Smrj 
8813ae115bc7Smrj 	ptr = kernelp = NULL;
8814ae115bc7Smrj 	new_arg = old_args = space = NULL;
8815eb2bd662Svikram 	new_path = NULL;
8816ae115bc7Smrj 	buf[0] = '\0';
8817ae115bc7Smrj 
8818eb2bd662Svikram 	INJECT_ERROR1("GET_SET_KERNEL_NOT_DBOOT",
8819eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
8820ae115bc7Smrj 	if (bam_direct != BAM_DIRECT_DBOOT) {
8821ae115bc7Smrj 		bam_error(NOT_DBOOT, optnum == KERNEL_CMD ? "kernel" : "args");
8822ae115bc7Smrj 		return (BAM_ERROR);
8823ae115bc7Smrj 	}
8824ae115bc7Smrj 
8825ae115bc7Smrj 	/*
8826ae115bc7Smrj 	 * If a user changed the default entry to a non-bootadm controlled
8827ae115bc7Smrj 	 * one, we don't want to mess with it.  Just print an error and
8828ae115bc7Smrj 	 * return.
8829ae115bc7Smrj 	 */
8830ae115bc7Smrj 	if (mp->curdefault) {
8831ae115bc7Smrj 		entryNum = s_strtol(mp->curdefault->arg);
8832ae115bc7Smrj 		for (entryp = mp->entries; entryp; entryp = entryp->next) {
8833ae115bc7Smrj 			if (entryp->entryNum == entryNum)
8834ae115bc7Smrj 				break;
8835ae115bc7Smrj 		}
8836ae115bc7Smrj 		if ((entryp != NULL) &&
8837ae115bc7Smrj 		    ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) {
8838ae115bc7Smrj 			bam_error(DEFAULT_NOT_BAM);
8839ae115bc7Smrj 			return (BAM_ERROR);
8840ae115bc7Smrj 		}
8841ae115bc7Smrj 	}
8842ae115bc7Smrj 
8843eb2bd662Svikram 	entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, NULL, NULL,
8844eb2bd662Svikram 	    0, &entryNum);
8845ae115bc7Smrj 
8846ae115bc7Smrj 	if (entryp != NULL) {
8847ae115bc7Smrj 		for (ptr = entryp->start; ptr && ptr != entryp->end;
8848ae115bc7Smrj 		    ptr = ptr->next) {
8849ae115bc7Smrj 			if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD],
8850ae115bc7Smrj 			    sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) {
8851ae115bc7Smrj 				kernelp = ptr;
8852ae115bc7Smrj 				break;
8853ae115bc7Smrj 			}
8854ae115bc7Smrj 		}
8855ae115bc7Smrj 		if (kernelp == NULL) {
8856ae115bc7Smrj 			bam_error(NO_KERNEL, entryNum);
8857ae115bc7Smrj 			return (BAM_ERROR);
8858ae115bc7Smrj 		}
8859ae115bc7Smrj 
8860ae115bc7Smrj 		old_kernel_len = strcspn(kernelp->arg, " \t");
8861ae115bc7Smrj 		space = old_args = kernelp->arg + old_kernel_len;
8862ae115bc7Smrj 		while ((*old_args == ' ') || (*old_args == '\t'))
8863ae115bc7Smrj 			old_args++;
8864ae115bc7Smrj 	}
8865ae115bc7Smrj 
8866ae115bc7Smrj 	if (path == NULL) {
8867eb2bd662Svikram 		if (entryp == NULL) {
8868eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_NO_RC, fcn));
8869eb2bd662Svikram 			BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8870ae115bc7Smrj 			return (BAM_SUCCESS);
8871eb2bd662Svikram 		}
8872eb2bd662Svikram 		assert(kernelp);
8873ae115bc7Smrj 		if (optnum == ARGS_CMD) {
8874eb2bd662Svikram 			if (old_args[0] != '\0') {
8875ae115bc7Smrj 				(void) strlcpy(buf, old_args, bufsize);
8876eb2bd662Svikram 				BAM_DPRINTF((D_GET_SET_KERNEL_ARGS, fcn, buf));
8877eb2bd662Svikram 			}
8878ae115bc7Smrj 		} else {
8879ae115bc7Smrj 			/*
8880ae115bc7Smrj 			 * We need to print the kernel, so we just turn the
8881ae115bc7Smrj 			 * first space into a '\0' and print the beginning.
8882ae115bc7Smrj 			 * We don't print anything if it's the default kernel.
8883ae115bc7Smrj 			 */
8884ae115bc7Smrj 			old_space = *space;
8885ae115bc7Smrj 			*space = '\0';
8886eb2bd662Svikram 			if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0) {
8887ae115bc7Smrj 				(void) strlcpy(buf, kernelp->arg, bufsize);
8888eb2bd662Svikram 				BAM_DPRINTF((D_GET_SET_KERNEL_KERN, fcn, buf));
8889eb2bd662Svikram 			}
8890ae115bc7Smrj 			*space = old_space;
8891ae115bc7Smrj 		}
8892eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8893ae115bc7Smrj 		return (BAM_SUCCESS);
8894ae115bc7Smrj 	}
8895ae115bc7Smrj 
8896ae115bc7Smrj 	/*
8897ae115bc7Smrj 	 * First, check if we're resetting an entry to the default.
8898ae115bc7Smrj 	 */
8899ae115bc7Smrj 	if ((path[0] == '\0') ||
8900ae115bc7Smrj 	    ((optnum == KERNEL_CMD) &&
8901ae115bc7Smrj 	    (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) {
8902ae115bc7Smrj 		if ((entryp == NULL) || (kernelp == NULL)) {
8903ae115bc7Smrj 			/* No previous entry, it's already the default */
8904eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_ALREADY, fcn));
8905ae115bc7Smrj 			return (BAM_SUCCESS);
8906ae115bc7Smrj 		}
8907ae115bc7Smrj 
8908ae115bc7Smrj 		/*
8909ae115bc7Smrj 		 * Check if we can delete the entry.  If we're resetting the
8910ae115bc7Smrj 		 * kernel command, and the args is already empty, or if we're
8911ae115bc7Smrj 		 * resetting the args command, and the kernel is already the
8912ae115bc7Smrj 		 * default, we can restore the old default and delete the entry.
8913ae115bc7Smrj 		 */
8914ae115bc7Smrj 		if (((optnum == KERNEL_CMD) &&
8915ae115bc7Smrj 		    ((old_args == NULL) || (old_args[0] == '\0'))) ||
8916ae115bc7Smrj 		    ((optnum == ARGS_CMD) &&
8917ae115bc7Smrj 		    (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL,
8918ae115bc7Smrj 		    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) {
8919ae115bc7Smrj 			kernelp = NULL;
892044da779fSWilliam Kucharski 			(void) delete_boot_entry(mp, entryNum, DBE_PRINTERR);
8921ae115bc7Smrj 			restore_default_entry(mp, BAM_OLD_RC_DEF,
8922ae115bc7Smrj 			    mp->old_rc_default);
8923ae115bc7Smrj 			mp->old_rc_default = NULL;
8924ae115bc7Smrj 			rv = BAM_WRITE;
8925eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESTORE_DEFAULT, fcn));
8926ae115bc7Smrj 			goto done;
8927ae115bc7Smrj 		}
8928ae115bc7Smrj 
8929ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
8930ae115bc7Smrj 			/*
8931ae115bc7Smrj 			 * At this point, we've already checked that old_args
8932ae115bc7Smrj 			 * and entryp are valid pointers.  The "+ 2" is for
8933ae115bc7Smrj 			 * a space a the string termination character.
8934ae115bc7Smrj 			 */
8935ae115bc7Smrj 			new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) +
8936ae115bc7Smrj 			    strlen(old_args) + 2;
8937ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
8938ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
8939ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, old_args);
8940ae115bc7Smrj 			free(kernelp->arg);
8941ae115bc7Smrj 			kernelp->arg = new_arg;
8942ae115bc7Smrj 
8943ae115bc7Smrj 			/*
8944ae115bc7Smrj 			 * We have changed the kernel line, so we may need
8945ae115bc7Smrj 			 * to update the archive line as well.
8946ae115bc7Smrj 			 */
8947ae115bc7Smrj 			set_archive_line(entryp, kernelp);
8948eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESET_KERNEL_SET_ARG,
8949eb2bd662Svikram 			    fcn, kernelp->arg));
8950ae115bc7Smrj 		} else {
8951ae115bc7Smrj 			/*
8952ae115bc7Smrj 			 * We're resetting the boot args to nothing, so
8953ae115bc7Smrj 			 * we only need to copy the kernel.  We've already
8954ae115bc7Smrj 			 * checked that the kernel is not the default.
8955ae115bc7Smrj 			 */
8956ae115bc7Smrj 			new_arg = s_calloc(1, old_kernel_len + 1);
8957ae115bc7Smrj 			(void) snprintf(new_arg, old_kernel_len + 1, "%s",
8958ae115bc7Smrj 			    kernelp->arg);
8959ae115bc7Smrj 			free(kernelp->arg);
8960ae115bc7Smrj 			kernelp->arg = new_arg;
8961eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESET_ARG_SET_KERNEL,
8962eb2bd662Svikram 			    fcn, kernelp->arg));
8963ae115bc7Smrj 		}
8964ae115bc7Smrj 		rv = BAM_WRITE;
8965ae115bc7Smrj 		goto done;
8966ae115bc7Smrj 	}
8967ae115bc7Smrj 
8968ae115bc7Smrj 	/*
8969ae115bc7Smrj 	 * Expand the kernel file to a full path, if necessary
8970ae115bc7Smrj 	 */
8971ae115bc7Smrj 	if ((optnum == KERNEL_CMD) && (path[0] != '/')) {
8972ae115bc7Smrj 		new_path = expand_path(path);
8973ae115bc7Smrj 		if (new_path == NULL) {
8974455710d3Srscott 			bam_error(UNKNOWN_KERNEL, path);
8975eb2bd662Svikram 			BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8976ae115bc7Smrj 			return (BAM_ERROR);
8977ae115bc7Smrj 		}
8978ae115bc7Smrj 		free_new_path = 1;
8979ae115bc7Smrj 	} else {
8980ae115bc7Smrj 		new_path = path;
8981ae115bc7Smrj 		free_new_path = 0;
8982ae115bc7Smrj 	}
8983ae115bc7Smrj 
8984ae115bc7Smrj 	/*
8985ae115bc7Smrj 	 * At this point, we know we're setting a new value.  First, take care
8986ae115bc7Smrj 	 * of the case where there was no previous entry.
8987ae115bc7Smrj 	 */
8988ae115bc7Smrj 	if (entryp == NULL) {
8989eb2bd662Svikram 
8990ae115bc7Smrj 		/* Similar to code in update_temp */
8991eb2bd662Svikram 		fstype = get_fstype("/");
8992eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_FSTYPE", fstype = NULL);
8993eb2bd662Svikram 		if (fstype == NULL) {
8994eb2bd662Svikram 			bam_error(BOOTENV_FSTYPE_FAILED);
8995ae115bc7Smrj 			rv = BAM_ERROR;
8996ae115bc7Smrj 			goto done;
8997ae115bc7Smrj 		}
8998eb2bd662Svikram 
8999eb2bd662Svikram 		osdev = get_special("/");
9000eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SPECIAL", osdev = NULL);
9001eb2bd662Svikram 		if (osdev == NULL) {
9002eb2bd662Svikram 			free(fstype);
9003eb2bd662Svikram 			bam_error(BOOTENV_SPECIAL_FAILED);
9004eb2bd662Svikram 			rv = BAM_ERROR;
9005eb2bd662Svikram 			goto done;
9006eb2bd662Svikram 		}
9007eb2bd662Svikram 
9008eb2bd662Svikram 		sign = find_existing_sign("/", osdev, fstype);
9009eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SIGN", sign = NULL);
9010eb2bd662Svikram 		if (sign == NULL) {
9011eb2bd662Svikram 			free(fstype);
9012eb2bd662Svikram 			free(osdev);
9013eb2bd662Svikram 			bam_error(BOOTENV_SIGN_FAILED);
9014eb2bd662Svikram 			rv = BAM_ERROR;
9015eb2bd662Svikram 			goto done;
9016eb2bd662Svikram 		}
9017eb2bd662Svikram 
9018eb2bd662Svikram 		free(osdev);
9019eb2bd662Svikram 		(void) strlcpy(signbuf, sign, sizeof (signbuf));
9020eb2bd662Svikram 		free(sign);
9021eb2bd662Svikram 		assert(strchr(signbuf, '(') == NULL &&
9022eb2bd662Svikram 		    strchr(signbuf, ',') == NULL &&
9023eb2bd662Svikram 		    strchr(signbuf, ')') == NULL);
9024eb2bd662Svikram 
9025ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
90264e483a70SGangadhar Mylapuram 			if (strcmp(fstype, "zfs") == 0) {
90274e483a70SGangadhar Mylapuram 				new_str_len = strlen(new_path) +
90284e483a70SGangadhar Mylapuram 				    strlen(ZFS_BOOT) + 8;
90294e483a70SGangadhar Mylapuram 				new_arg = s_calloc(1, new_str_len);
90304e483a70SGangadhar Mylapuram 				(void) snprintf(new_arg, new_str_len, "%s %s",
90314e483a70SGangadhar Mylapuram 				    new_path, ZFS_BOOT);
90324e483a70SGangadhar Mylapuram 				BAM_DPRINTF((D_GET_SET_KERNEL_NEW_KERN, fcn,
90334e483a70SGangadhar Mylapuram 				    new_arg));
90344e483a70SGangadhar Mylapuram 				entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
90354e483a70SGangadhar Mylapuram 				    signbuf, new_arg, NULL, NULL, NULL);
90364e483a70SGangadhar Mylapuram 				free(new_arg);
90374e483a70SGangadhar Mylapuram 			} else {
90384e483a70SGangadhar Mylapuram 				BAM_DPRINTF((D_GET_SET_KERNEL_NEW_KERN, fcn,
90394e483a70SGangadhar Mylapuram 				    new_path));
9040ae115bc7Smrj 				entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
904144da779fSWilliam Kucharski 				    signbuf, new_path, NULL, NULL, NULL);
90424e483a70SGangadhar Mylapuram 			}
9043ae115bc7Smrj 		} else {
90444487ce1dSGangadhar Mylapuram 			new_str_len = strlen(path) + 8;
90454487ce1dSGangadhar Mylapuram 			if (strcmp(fstype, "zfs") == 0) {
90464487ce1dSGangadhar Mylapuram 				new_str_len += strlen(DIRECT_BOOT_KERNEL_ZFS);
9047ae115bc7Smrj 				new_arg = s_calloc(1, new_str_len);
90484487ce1dSGangadhar Mylapuram 				(void) snprintf(new_arg, new_str_len, "%s %s",
90494487ce1dSGangadhar Mylapuram 				    DIRECT_BOOT_KERNEL_ZFS, path);
90504487ce1dSGangadhar Mylapuram 			} else {
90514487ce1dSGangadhar Mylapuram 				new_str_len += strlen(DIRECT_BOOT_KERNEL);
90524487ce1dSGangadhar Mylapuram 				new_arg = s_calloc(1, new_str_len);
9053ae115bc7Smrj 				(void) snprintf(new_arg, new_str_len, "%s %s",
9054ae115bc7Smrj 				    DIRECT_BOOT_KERNEL, path);
90554487ce1dSGangadhar Mylapuram 			}
90564487ce1dSGangadhar Mylapuram 
9057eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_NEW_ARG, fcn, new_arg));
9058ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
905944da779fSWilliam Kucharski 			    signbuf, new_arg, NULL, DIRECT_BOOT_ARCHIVE, NULL);
9060eb2bd662Svikram 			free(new_arg);
9061eb2bd662Svikram 		}
90624487ce1dSGangadhar Mylapuram 		free(fstype);
9063eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_ADD_BOOT_ENTRY",
9064eb2bd662Svikram 		    entryNum = BAM_ERROR);
9065eb2bd662Svikram 		if (entryNum == BAM_ERROR) {
9066eb2bd662Svikram 			bam_error(GET_SET_KERNEL_ADD_BOOT_ENTRY,
9067eb2bd662Svikram 			    BOOTENV_RC_TITLE);
9068eb2bd662Svikram 			rv = BAM_ERROR;
9069eb2bd662Svikram 			goto done;
9070ae115bc7Smrj 		}
9071ae115bc7Smrj 		save_default_entry(mp, BAM_OLD_RC_DEF);
9072eb2bd662Svikram 		ret = set_global(mp, menu_cmds[DEFAULT_CMD], entryNum);
9073eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SET_GLOBAL", ret = BAM_ERROR);
9074eb2bd662Svikram 		if (ret == BAM_ERROR) {
9075eb2bd662Svikram 			bam_error(GET_SET_KERNEL_SET_GLOBAL, entryNum);
9076eb2bd662Svikram 		}
9077ae115bc7Smrj 		rv = BAM_WRITE;
9078ae115bc7Smrj 		goto done;
9079ae115bc7Smrj 	}
9080ae115bc7Smrj 
9081ae115bc7Smrj 	/*
9082ae115bc7Smrj 	 * There was already an bootenv entry which we need to edit.
9083ae115bc7Smrj 	 */
9084ae115bc7Smrj 	if (optnum == KERNEL_CMD) {
9085ae115bc7Smrj 		new_str_len = strlen(new_path) + strlen(old_args) + 2;
9086ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
9087ae115bc7Smrj 		(void) snprintf(new_arg, new_str_len, "%s %s", new_path,
9088ae115bc7Smrj 		    old_args);
9089ae115bc7Smrj 		free(kernelp->arg);
9090ae115bc7Smrj 		kernelp->arg = new_arg;
9091ae115bc7Smrj 
9092ae115bc7Smrj 		/*
9093ae115bc7Smrj 		 * If we have changed the kernel line, we may need to update
9094ae115bc7Smrj 		 * the archive line as well.
9095ae115bc7Smrj 		 */
9096ae115bc7Smrj 		set_archive_line(entryp, kernelp);
9097eb2bd662Svikram 		BAM_DPRINTF((D_GET_SET_KERNEL_REPLACED_KERNEL_SAME_ARG, fcn,
9098eb2bd662Svikram 		    kernelp->arg));
9099ae115bc7Smrj 	} else {
9100ae115bc7Smrj 		new_str_len = old_kernel_len + strlen(path) + 8;
9101ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
9102ae115bc7Smrj 		(void) strncpy(new_arg, kernelp->arg, old_kernel_len);
9103ae115bc7Smrj 		(void) strlcat(new_arg, " ", new_str_len);
9104ae115bc7Smrj 		(void) strlcat(new_arg, path, new_str_len);
9105ae115bc7Smrj 		free(kernelp->arg);
9106ae115bc7Smrj 		kernelp->arg = new_arg;
9107eb2bd662Svikram 		BAM_DPRINTF((D_GET_SET_KERNEL_SAME_KERNEL_REPLACED_ARG, fcn,
9108eb2bd662Svikram 		    kernelp->arg));
9109ae115bc7Smrj 	}
9110ae115bc7Smrj 	rv = BAM_WRITE;
9111ae115bc7Smrj 
9112ae115bc7Smrj done:
9113ae115bc7Smrj 	if ((rv == BAM_WRITE) && kernelp)
9114ae115bc7Smrj 		update_line(kernelp);
9115ae115bc7Smrj 	if (free_new_path)
9116ae115bc7Smrj 		free(new_path);
9117eb2bd662Svikram 	if (rv == BAM_WRITE) {
9118eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
9119eb2bd662Svikram 	} else {
9120eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
9121eb2bd662Svikram 	}
9122ae115bc7Smrj 	return (rv);
9123ae115bc7Smrj }
9124ae115bc7Smrj 
9125eb2bd662Svikram static error_t
9126eb2bd662Svikram get_kernel(menu_t *mp, menu_cmd_t optnum, char *buf, size_t bufsize)
9127eb2bd662Svikram {
9128eb2bd662Svikram 	const char	*fcn = "get_kernel()";
9129eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_cmds[optnum]));
9130eb2bd662Svikram 	return (get_set_kernel(mp, optnum, NULL, buf, bufsize));
9131eb2bd662Svikram }
9132eb2bd662Svikram 
9133eb2bd662Svikram static error_t
9134eb2bd662Svikram set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize)
9135eb2bd662Svikram {
9136eb2bd662Svikram 	const char	*fcn = "set_kernel()";
9137eb2bd662Svikram 	assert(path != NULL);
9138eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, menu_cmds[optnum], path));
9139eb2bd662Svikram 	return (get_set_kernel(mp, optnum, path, buf, bufsize));
9140eb2bd662Svikram }
9141eb2bd662Svikram 
91427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
91437c478bd9Sstevel@tonic-gate static error_t
9144eb2bd662Svikram set_option(menu_t *mp, char *dummy, char *opt)
91457c478bd9Sstevel@tonic-gate {
9146eb2bd662Svikram 	int		optnum;
9147eb2bd662Svikram 	int		optval;
91487c478bd9Sstevel@tonic-gate 	char		*val;
9149ae115bc7Smrj 	char		buf[BUFSIZ] = "";
9150ae115bc7Smrj 	error_t		rv;
9151eb2bd662Svikram 	const char	*fcn = "set_option()";
91527c478bd9Sstevel@tonic-gate 
91537c478bd9Sstevel@tonic-gate 	assert(mp);
91547c478bd9Sstevel@tonic-gate 	assert(opt);
9155eb2bd662Svikram 	assert(dummy == NULL);
9156eb2bd662Svikram 
9157eb2bd662Svikram 	/* opt is set from bam_argv[0] and is always non-NULL */
9158eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt));
91597c478bd9Sstevel@tonic-gate 
91607c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
9161ae115bc7Smrj 	if (val != NULL) {
9162ae115bc7Smrj 		*val = '\0';
91637c478bd9Sstevel@tonic-gate 	}
91647c478bd9Sstevel@tonic-gate 
91657c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
91667c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
91677c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
91687c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
9169ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) {
9170ae115bc7Smrj 		optnum = KERNEL_CMD;
9171ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) {
9172ae115bc7Smrj 		optnum = ARGS_CMD;
91737c478bd9Sstevel@tonic-gate 	} else {
9174eb2bd662Svikram 		bam_error(INVALID_OPTION, opt);
91757c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
91767c478bd9Sstevel@tonic-gate 	}
91777c478bd9Sstevel@tonic-gate 
9178ae115bc7Smrj 	/*
9179ae115bc7Smrj 	 * kernel and args are allowed without "=new_value" strings.  All
9180ae115bc7Smrj 	 * others cause errors
9181ae115bc7Smrj 	 */
9182ae115bc7Smrj 	if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) {
9183eb2bd662Svikram 		bam_error(NO_OPTION_ARG, opt);
9184ae115bc7Smrj 		return (BAM_ERROR);
9185ae115bc7Smrj 	} else if (val != NULL) {
91867c478bd9Sstevel@tonic-gate 		*val = '=';
9187ae115bc7Smrj 	}
9188ae115bc7Smrj 
9189ae115bc7Smrj 	if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) {
9190eb2bd662Svikram 		BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum],
9191eb2bd662Svikram 		    val ? val + 1 : "NULL"));
9192eb2bd662Svikram 
9193eb2bd662Svikram 		if (val)
9194eb2bd662Svikram 			rv = set_kernel(mp, optnum, val + 1, buf, sizeof (buf));
9195eb2bd662Svikram 		else
9196eb2bd662Svikram 			rv = get_kernel(mp, optnum, buf, sizeof (buf));
9197ae115bc7Smrj 		if ((rv == BAM_SUCCESS) && (buf[0] != '\0'))
9198ae115bc7Smrj 			(void) printf("%s\n", buf);
9199ae115bc7Smrj 	} else {
9200ae115bc7Smrj 		optval = s_strtol(val + 1);
9201eb2bd662Svikram 		BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum], val + 1));
9202eb2bd662Svikram 		rv = set_global(mp, menu_cmds[optnum], optval);
92037c478bd9Sstevel@tonic-gate 	}
9204eb2bd662Svikram 
9205eb2bd662Svikram 	if (rv == BAM_WRITE || rv == BAM_SUCCESS) {
9206eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
9207eb2bd662Svikram 	} else {
9208eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
9209eb2bd662Svikram 	}
9210eb2bd662Svikram 
9211eb2bd662Svikram 	return (rv);
9212ae115bc7Smrj }
92137c478bd9Sstevel@tonic-gate 
92147c478bd9Sstevel@tonic-gate /*
92157c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
92167c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
92177c478bd9Sstevel@tonic-gate  */
92187c478bd9Sstevel@tonic-gate static error_t
92197c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
92207c478bd9Sstevel@tonic-gate {
92217c478bd9Sstevel@tonic-gate 	line_t *lp;
92227c478bd9Sstevel@tonic-gate 	char *arg;
92237c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
92247c478bd9Sstevel@tonic-gate 
92257c478bd9Sstevel@tonic-gate 	assert(mp);
92267c478bd9Sstevel@tonic-gate 	assert(menu_path);
92277c478bd9Sstevel@tonic-gate 	assert(globalcmd);
92287c478bd9Sstevel@tonic-gate 
92297c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
92307c478bd9Sstevel@tonic-gate 		if (!quiet)
92317c478bd9Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
92327c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
92337c478bd9Sstevel@tonic-gate 	}
92347c478bd9Sstevel@tonic-gate 
92357c478bd9Sstevel@tonic-gate 	done = 0;
92367c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
92377c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
92387c478bd9Sstevel@tonic-gate 			continue;
92397c478bd9Sstevel@tonic-gate 
92407c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
92417c478bd9Sstevel@tonic-gate 			if (!quiet)
92427c478bd9Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
92437c478bd9Sstevel@tonic-gate 			continue;
92447c478bd9Sstevel@tonic-gate 		}
92457c478bd9Sstevel@tonic-gate 
92467c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
92477c478bd9Sstevel@tonic-gate 			continue;
92487c478bd9Sstevel@tonic-gate 
92497c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
92507c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
92517c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
92527c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
92537c478bd9Sstevel@tonic-gate 		}
92547c478bd9Sstevel@tonic-gate 
92557c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
92567c478bd9Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
92577c478bd9Sstevel@tonic-gate 		done = 1;
92587c478bd9Sstevel@tonic-gate 	}
92597c478bd9Sstevel@tonic-gate 
92607c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
92617c478bd9Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
92627c478bd9Sstevel@tonic-gate 
92637c478bd9Sstevel@tonic-gate 	return (ret);
92647c478bd9Sstevel@tonic-gate }
92657c478bd9Sstevel@tonic-gate 
92667c478bd9Sstevel@tonic-gate static error_t
92677c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
92687c478bd9Sstevel@tonic-gate {
9269eb2bd662Svikram 	const char *fcn = "menu_write()";
9270eb2bd662Svikram 
9271eb2bd662Svikram 	BAM_DPRINTF((D_MENU_WRITE_ENTER, fcn, root));
92727c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
92737c478bd9Sstevel@tonic-gate }
92747c478bd9Sstevel@tonic-gate 
9275eb2bd662Svikram void
92767c478bd9Sstevel@tonic-gate line_free(line_t *lp)
92777c478bd9Sstevel@tonic-gate {
92787c478bd9Sstevel@tonic-gate 	if (lp == NULL)
92797c478bd9Sstevel@tonic-gate 		return;
92807c478bd9Sstevel@tonic-gate 
9281eac223ccSWilliam Kucharski 	if (lp->cmd != NULL)
92827c478bd9Sstevel@tonic-gate 		free(lp->cmd);
92837c478bd9Sstevel@tonic-gate 	if (lp->sep)
92847c478bd9Sstevel@tonic-gate 		free(lp->sep);
92857c478bd9Sstevel@tonic-gate 	if (lp->arg)
92867c478bd9Sstevel@tonic-gate 		free(lp->arg);
92877c478bd9Sstevel@tonic-gate 	if (lp->line)
92887c478bd9Sstevel@tonic-gate 		free(lp->line);
92897c478bd9Sstevel@tonic-gate 	free(lp);
92907c478bd9Sstevel@tonic-gate }
92917c478bd9Sstevel@tonic-gate 
92927c478bd9Sstevel@tonic-gate static void
92937c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
92947c478bd9Sstevel@tonic-gate {
92957c478bd9Sstevel@tonic-gate 	line_t *lp;
92967c478bd9Sstevel@tonic-gate 
92977c478bd9Sstevel@tonic-gate 	while (start) {
92987c478bd9Sstevel@tonic-gate 		lp = start;
92997c478bd9Sstevel@tonic-gate 		start = start->next;
93007c478bd9Sstevel@tonic-gate 		line_free(lp);
93017c478bd9Sstevel@tonic-gate 	}
93027c478bd9Sstevel@tonic-gate }
93037c478bd9Sstevel@tonic-gate 
93047c478bd9Sstevel@tonic-gate static void
93057c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
93067c478bd9Sstevel@tonic-gate {
93077c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
93087c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
93097c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
93107c478bd9Sstevel@tonic-gate }
93117c478bd9Sstevel@tonic-gate 
93127c478bd9Sstevel@tonic-gate static void
93137c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
93147c478bd9Sstevel@tonic-gate {
93158c1b6884Sszhou 	entry_t *ent, *tmp;
93167c478bd9Sstevel@tonic-gate 	assert(mp);
93177c478bd9Sstevel@tonic-gate 
93187c478bd9Sstevel@tonic-gate 	if (mp->start)
93197c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
93208c1b6884Sszhou 	ent = mp->entries;
93218c1b6884Sszhou 	while (ent) {
93228c1b6884Sszhou 		tmp = ent;
93238c1b6884Sszhou 		ent = tmp->next;
93248c1b6884Sszhou 		free(tmp);
93258c1b6884Sszhou 	}
93267c478bd9Sstevel@tonic-gate 
93278c1b6884Sszhou 	free(mp);
93287c478bd9Sstevel@tonic-gate }
93297c478bd9Sstevel@tonic-gate 
93307c478bd9Sstevel@tonic-gate /*
93317c478bd9Sstevel@tonic-gate  * Utility routines
93327c478bd9Sstevel@tonic-gate  */
93337c478bd9Sstevel@tonic-gate 
93347c478bd9Sstevel@tonic-gate 
93357c478bd9Sstevel@tonic-gate /*
93367c478bd9Sstevel@tonic-gate  * Returns 0 on success
93377c478bd9Sstevel@tonic-gate  * Any other value indicates an error
93387c478bd9Sstevel@tonic-gate  */
93397c478bd9Sstevel@tonic-gate static int
9340986fd29aSsetje exec_cmd(char *cmdline, filelist_t *flistp)
93417c478bd9Sstevel@tonic-gate {
93427c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
93437c478bd9Sstevel@tonic-gate 	int ret;
93447c478bd9Sstevel@tonic-gate 	FILE *ptr;
93457c478bd9Sstevel@tonic-gate 	sigset_t set;
93467c478bd9Sstevel@tonic-gate 	void (*disp)(int);
93477c478bd9Sstevel@tonic-gate 
93487c478bd9Sstevel@tonic-gate 	/*
93497c478bd9Sstevel@tonic-gate 	 * For security
93507c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
93517c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
93527c478bd9Sstevel@tonic-gate 	 */
93537c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
93547c478bd9Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
93557c478bd9Sstevel@tonic-gate 		return (-1);
93567c478bd9Sstevel@tonic-gate 	}
93577c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
93587c478bd9Sstevel@tonic-gate 
93597c478bd9Sstevel@tonic-gate 	/*
93607c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
93617c478bd9Sstevel@tonic-gate 	 * unblock it here
93627c478bd9Sstevel@tonic-gate 	 */
93637c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
93647c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
93657c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
93667c478bd9Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
93677c478bd9Sstevel@tonic-gate 		return (-1);
93687c478bd9Sstevel@tonic-gate 	}
93697c478bd9Sstevel@tonic-gate 
93707c478bd9Sstevel@tonic-gate 	/*
93717c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
93727c478bd9Sstevel@tonic-gate 	 */
93737c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
93747c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
93757c478bd9Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
93767c478bd9Sstevel@tonic-gate 		return (-1);
93777c478bd9Sstevel@tonic-gate 	}
93787c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
93797c478bd9Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
93807c478bd9Sstevel@tonic-gate 		return (-1);
93817c478bd9Sstevel@tonic-gate 	}
93827c478bd9Sstevel@tonic-gate 
93837c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
93847c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
93857c478bd9Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
93867c478bd9Sstevel@tonic-gate 		return (-1);
93877c478bd9Sstevel@tonic-gate 	}
93887c478bd9Sstevel@tonic-gate 
93897c478bd9Sstevel@tonic-gate 	/*
93907c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
93917c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
93927c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
93937c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
93947c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
93957c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
93967c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
93977c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
93987c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
93997c478bd9Sstevel@tonic-gate 	 * pclose().
94007c478bd9Sstevel@tonic-gate 	 *
94017c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
94027c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
94037c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
94047c478bd9Sstevel@tonic-gate 	 */
9405986fd29aSsetje 	while (s_fgets(buf, sizeof (buf), ptr) != NULL) {
9406986fd29aSsetje 		if (flistp == NULL) {
9407986fd29aSsetje 			/* s_fgets strips newlines, so insert them at the end */
9408986fd29aSsetje 			bam_print(PRINT, buf);
9409986fd29aSsetje 		} else {
9410986fd29aSsetje 			append_to_flist(flistp, buf);
94117c478bd9Sstevel@tonic-gate 		}
94127c478bd9Sstevel@tonic-gate 	}
94137c478bd9Sstevel@tonic-gate 
94147c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
94157c478bd9Sstevel@tonic-gate 	if (ret == -1) {
94167c478bd9Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
94177c478bd9Sstevel@tonic-gate 		return (-1);
94187c478bd9Sstevel@tonic-gate 	}
94197c478bd9Sstevel@tonic-gate 
94207c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
94217c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
94227c478bd9Sstevel@tonic-gate 	} else {
94237c478bd9Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
94247c478bd9Sstevel@tonic-gate 		return (-1);
94257c478bd9Sstevel@tonic-gate 	}
94267c478bd9Sstevel@tonic-gate }
94277c478bd9Sstevel@tonic-gate 
94287c478bd9Sstevel@tonic-gate /*
94297c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
94307c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
94317c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
94327c478bd9Sstevel@tonic-gate  */
94337c478bd9Sstevel@tonic-gate static long
94347c478bd9Sstevel@tonic-gate s_strtol(char *str)
94357c478bd9Sstevel@tonic-gate {
94367c478bd9Sstevel@tonic-gate 	long l;
94377c478bd9Sstevel@tonic-gate 	char *res = NULL;
94387c478bd9Sstevel@tonic-gate 
94397c478bd9Sstevel@tonic-gate 	if (str == NULL) {
94407c478bd9Sstevel@tonic-gate 		return (-1);
94417c478bd9Sstevel@tonic-gate 	}
94427c478bd9Sstevel@tonic-gate 
94437c478bd9Sstevel@tonic-gate 	errno = 0;
94447c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
94457c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
94467c478bd9Sstevel@tonic-gate 		return (-1);
94477c478bd9Sstevel@tonic-gate 	}
94487c478bd9Sstevel@tonic-gate 
94497c478bd9Sstevel@tonic-gate 	return (l);
94507c478bd9Sstevel@tonic-gate }
94517c478bd9Sstevel@tonic-gate 
94527c478bd9Sstevel@tonic-gate /*
94537c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
94547c478bd9Sstevel@tonic-gate  */
94557c478bd9Sstevel@tonic-gate static int
94567c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
94577c478bd9Sstevel@tonic-gate {
94587c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
94597c478bd9Sstevel@tonic-gate 
94607c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
94617c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
94627c478bd9Sstevel@tonic-gate }
94637c478bd9Sstevel@tonic-gate 
94647c478bd9Sstevel@tonic-gate /*
94657c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
94667c478bd9Sstevel@tonic-gate  */
9467ae115bc7Smrj char *
94687c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
94697c478bd9Sstevel@tonic-gate {
94707c478bd9Sstevel@tonic-gate 	int n;
94717c478bd9Sstevel@tonic-gate 
94727c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
94737c478bd9Sstevel@tonic-gate 	if (buf) {
94747c478bd9Sstevel@tonic-gate 		n = strlen(buf);
94757c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
94767c478bd9Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
94777c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
94787c478bd9Sstevel@tonic-gate 	}
94797c478bd9Sstevel@tonic-gate 
94807c478bd9Sstevel@tonic-gate 	return (buf);
94817c478bd9Sstevel@tonic-gate }
94827c478bd9Sstevel@tonic-gate 
9483ae115bc7Smrj void *
94847c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
94857c478bd9Sstevel@tonic-gate {
94867c478bd9Sstevel@tonic-gate 	void *ptr;
94877c478bd9Sstevel@tonic-gate 
94887c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
94897c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
94907c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
94917c478bd9Sstevel@tonic-gate 		bam_exit(1);
94927c478bd9Sstevel@tonic-gate 	}
94937c478bd9Sstevel@tonic-gate 	return (ptr);
94947c478bd9Sstevel@tonic-gate }
94957c478bd9Sstevel@tonic-gate 
9496ae115bc7Smrj void *
9497ae115bc7Smrj s_realloc(void *ptr, size_t sz)
9498ae115bc7Smrj {
9499ae115bc7Smrj 	ptr = realloc(ptr, sz);
9500ae115bc7Smrj 	if (ptr == NULL) {
9501ae115bc7Smrj 		bam_error(NO_MEM, sz);
9502ae115bc7Smrj 		bam_exit(1);
9503ae115bc7Smrj 	}
9504ae115bc7Smrj 	return (ptr);
9505ae115bc7Smrj }
9506ae115bc7Smrj 
9507eb2bd662Svikram char *
95087c478bd9Sstevel@tonic-gate s_strdup(char *str)
95097c478bd9Sstevel@tonic-gate {
95107c478bd9Sstevel@tonic-gate 	char *ptr;
95117c478bd9Sstevel@tonic-gate 
95127c478bd9Sstevel@tonic-gate 	if (str == NULL)
95137c478bd9Sstevel@tonic-gate 		return (NULL);
95147c478bd9Sstevel@tonic-gate 
95157c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
95167c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
95177c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
95187c478bd9Sstevel@tonic-gate 		bam_exit(1);
95197c478bd9Sstevel@tonic-gate 	}
95207c478bd9Sstevel@tonic-gate 	return (ptr);
95217c478bd9Sstevel@tonic-gate }
95227c478bd9Sstevel@tonic-gate 
95237c478bd9Sstevel@tonic-gate /*
95247c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
95257c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
95267c478bd9Sstevel@tonic-gate  */
95277c478bd9Sstevel@tonic-gate static int
95287c478bd9Sstevel@tonic-gate is_amd64(void)
95297c478bd9Sstevel@tonic-gate {
95307c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
95317c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
95327c478bd9Sstevel@tonic-gate 
95337c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
95347c478bd9Sstevel@tonic-gate 		return (amd64);
95357c478bd9Sstevel@tonic-gate 
9536d876c67dSjg 	if (bam_alt_platform) {
9537d876c67dSjg 		if (strcmp(bam_platform, "i86pc") == 0) {
95387c478bd9Sstevel@tonic-gate 			amd64 = 1;		/* diskless server */
9539d876c67dSjg 		}
9540d876c67dSjg 	} else {
9541d876c67dSjg 		if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
9542d876c67dSjg 		    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) {
9543d876c67dSjg 			amd64 = 1;
9544d876c67dSjg 		} else if (strstr(isabuf, "i386") == NULL) {
9545d876c67dSjg 			amd64 = 1;		/* diskless server */
9546d876c67dSjg 		}
9547d876c67dSjg 	}
9548d876c67dSjg 	if (amd64 == -1)
95497c478bd9Sstevel@tonic-gate 		amd64 = 0;
95507c478bd9Sstevel@tonic-gate 
95517c478bd9Sstevel@tonic-gate 	return (amd64);
95527c478bd9Sstevel@tonic-gate }
95537c478bd9Sstevel@tonic-gate 
955479755401Ssetje static char *
955579755401Ssetje get_machine(void)
9556986fd29aSsetje {
955779755401Ssetje 	static int cached = -1;
955879755401Ssetje 	static char mbuf[257];	/* from sysinfo(2) manpage */
9559986fd29aSsetje 
956079755401Ssetje 	if (cached == 0)
956179755401Ssetje 		return (mbuf);
9562d876c67dSjg 
9563d876c67dSjg 	if (bam_alt_platform) {
956479755401Ssetje 		return (bam_platform);
9565d876c67dSjg 	} else {
956679755401Ssetje 		if (sysinfo(SI_MACHINE, mbuf, sizeof (mbuf)) > 0) {
956779755401Ssetje 			cached = 1;
9568d876c67dSjg 		}
9569d876c67dSjg 	}
957079755401Ssetje 	if (cached == -1) {
957179755401Ssetje 		mbuf[0] = '\0';
957279755401Ssetje 		cached = 0;
9573986fd29aSsetje 	}
9574986fd29aSsetje 
957579755401Ssetje 	return (mbuf);
9576986fd29aSsetje }
9577986fd29aSsetje 
9578eb2bd662Svikram int
9579eb2bd662Svikram is_sparc(void)
9580eb2bd662Svikram {
958179755401Ssetje 	static int issparc = -1;
958279755401Ssetje 	char mbuf[257];	/* from sysinfo(2) manpage */
958379755401Ssetje 
958479755401Ssetje 	if (issparc != -1)
958579755401Ssetje 		return (issparc);
958679755401Ssetje 
958779755401Ssetje 	if (bam_alt_platform) {
958879755401Ssetje 		if (strncmp(bam_platform, "sun4", 4) == 0) {
958979755401Ssetje 			issparc = 1;
959079755401Ssetje 		}
959179755401Ssetje 	} else {
959279755401Ssetje 		if (sysinfo(SI_ARCHITECTURE, mbuf, sizeof (mbuf)) > 0 &&
9593d7d0f93bSjg 		    strcmp(mbuf, "sparc") == 0) {
959479755401Ssetje 			issparc = 1;
959579755401Ssetje 		}
9596d7d0f93bSjg 	}
9597d7d0f93bSjg 	if (issparc == -1)
9598d7d0f93bSjg 		issparc = 0;
959979755401Ssetje 
960079755401Ssetje 	return (issparc);
9601eb2bd662Svikram }
9602986fd29aSsetje 
96037c478bd9Sstevel@tonic-gate static void
96047c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
96057c478bd9Sstevel@tonic-gate {
96067c478bd9Sstevel@tonic-gate 	line_t *lp;
96077c478bd9Sstevel@tonic-gate 
96087c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
96097c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
96107c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
96117c478bd9Sstevel@tonic-gate 		flistp->head = lp;
96127c478bd9Sstevel@tonic-gate 	else
96137c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
96147c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
96157c478bd9Sstevel@tonic-gate }
96162449e17fSsherrym 
9617986fd29aSsetje #if !defined(_OPB)
96182449e17fSsherrym 
96192449e17fSsherrym UCODE_VENDORS;
96202449e17fSsherrym 
96212449e17fSsherrym /*ARGSUSED*/
96222449e17fSsherrym static void
96232449e17fSsherrym ucode_install(char *root)
96242449e17fSsherrym {
96252449e17fSsherrym 	int i;
96262449e17fSsherrym 
96272449e17fSsherrym 	for (i = 0; ucode_vendors[i].filestr != NULL; i++) {
96282449e17fSsherrym 		int cmd_len = PATH_MAX + 256;
96292449e17fSsherrym 		char cmd[PATH_MAX + 256];
96302449e17fSsherrym 		char file[PATH_MAX];
96312449e17fSsherrym 		char timestamp[PATH_MAX];
96322449e17fSsherrym 		struct stat fstatus, tstatus;
96332449e17fSsherrym 		struct utimbuf u_times;
96342449e17fSsherrym 
9635adc586deSMark Johnson 		(void) snprintf(file, PATH_MAX, "%s/%s/%s-ucode.%s",
9636adc586deSMark Johnson 		    bam_root, UCODE_INSTALL_PATH, ucode_vendors[i].filestr,
9637adc586deSMark Johnson 		    ucode_vendors[i].extstr);
96382449e17fSsherrym 
96392449e17fSsherrym 		if (stat(file, &fstatus) != 0 || !(S_ISREG(fstatus.st_mode)))
96402449e17fSsherrym 			continue;
96412449e17fSsherrym 
96422449e17fSsherrym 		(void) snprintf(timestamp, PATH_MAX, "%s.ts", file);
96432449e17fSsherrym 
96442449e17fSsherrym 		if (stat(timestamp, &tstatus) == 0 &&
96452449e17fSsherrym 		    fstatus.st_mtime <= tstatus.st_mtime)
96462449e17fSsherrym 			continue;
96472449e17fSsherrym 
96482449e17fSsherrym 		(void) snprintf(cmd, cmd_len, "/usr/sbin/ucodeadm -i -R "
96492449e17fSsherrym 		    "%s/%s/%s %s > /dev/null 2>&1", bam_root,
96502449e17fSsherrym 		    UCODE_INSTALL_PATH, ucode_vendors[i].vendorstr, file);
96512449e17fSsherrym 		if (system(cmd) != 0)
96522449e17fSsherrym 			return;
96532449e17fSsherrym 
96542449e17fSsherrym 		if (creat(timestamp, S_IRUSR | S_IWUSR) == -1)
96552449e17fSsherrym 			return;
96562449e17fSsherrym 
96572449e17fSsherrym 		u_times.actime = fstatus.st_atime;
96582449e17fSsherrym 		u_times.modtime = fstatus.st_mtime;
96592449e17fSsherrym 		(void) utime(timestamp, &u_times);
96602449e17fSsherrym 	}
96612449e17fSsherrym }
96622449e17fSsherrym #endif
9663