xref: /titanic_52/usr/src/cmd/boot/bootadm/bootadm.c (revision 4a9df8756d3c341d4c88dbbb02d744b9cd3d3dbe)
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 /*
2237eb779cSVikram Hegde  * Copyright 2009 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>
417c478bd9Sstevel@tonic-gate #include <stdarg.h>
427c478bd9Sstevel@tonic-gate #include <limits.h>
437c478bd9Sstevel@tonic-gate #include <signal.h>
447c478bd9Sstevel@tonic-gate #include <sys/wait.h>
457c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
46f904d32dSJerry Gilliam #include <sys/mntent.h>
477c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
487c478bd9Sstevel@tonic-gate #include <libnvpair.h>
497c478bd9Sstevel@tonic-gate #include <ftw.h>
507c478bd9Sstevel@tonic-gate #include <fcntl.h>
517c478bd9Sstevel@tonic-gate #include <strings.h>
522449e17fSsherrym #include <utime.h>
537c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
547c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
5558091fd8Ssetje #include <sys/param.h>
56eb2bd662Svikram #include <dirent.h>
57eb2bd662Svikram #include <ctype.h>
58eb2bd662Svikram #include <libgen.h>
59e7cbe64fSgw25295 #include <sys/sysmacros.h>
6048847494SEnrico Perla - Sun Microsystems #include <sys/elf.h>
61963390b4Svikram #include <libscf.h>
6248847494SEnrico Perla - Sun Microsystems #include <zlib.h>
6348847494SEnrico Perla - Sun Microsystems #include <sys/lockfs.h>
6448847494SEnrico Perla - Sun Microsystems #include <sys/filio.h>
65986fd29aSsetje 
66986fd29aSsetje #if !defined(_OPB)
672449e17fSsherrym #include <sys/ucode.h>
682449e17fSsherrym #endif
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <pwd.h>
717c478bd9Sstevel@tonic-gate #include <grp.h>
727c478bd9Sstevel@tonic-gate #include <device_info.h>
73eb2bd662Svikram #include <sys/vtoc.h>
74eb2bd662Svikram #include <sys/efi_partition.h>
75eb2bd662Svikram 
767c478bd9Sstevel@tonic-gate #include <locale.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #include "message.h"
79ae115bc7Smrj #include "bootadm.h"
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
827c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
837c478bd9Sstevel@tonic-gate #endif	/* TEXT_DOMAIN */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /* Type definitions */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* Primary subcmds */
887c478bd9Sstevel@tonic-gate typedef enum {
897c478bd9Sstevel@tonic-gate 	BAM_MENU = 3,
907c478bd9Sstevel@tonic-gate 	BAM_ARCHIVE
917c478bd9Sstevel@tonic-gate } subcmd_t;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate typedef enum {
947c478bd9Sstevel@tonic-gate     OPT_ABSENT = 0,	/* No option */
957c478bd9Sstevel@tonic-gate     OPT_REQ,		/* option required */
967c478bd9Sstevel@tonic-gate     OPT_OPTIONAL	/* option may or may not be present */
977c478bd9Sstevel@tonic-gate } option_t;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate typedef struct {
1007c478bd9Sstevel@tonic-gate 	char	*subcmd;
1017c478bd9Sstevel@tonic-gate 	option_t option;
1027c478bd9Sstevel@tonic-gate 	error_t (*handler)();
1031a97e40eSvikram 	int	unpriv;			/* is this an unprivileged command */
1047c478bd9Sstevel@tonic-gate } subcmd_defn_t;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define	LINE_INIT	0	/* lineNum initial value */
1077c478bd9Sstevel@tonic-gate #define	ENTRY_INIT	-1	/* entryNum initial value */
1087c478bd9Sstevel@tonic-gate #define	ALL_ENTRIES	-2	/* selects all boot entries */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	GRUB_DIR		"/boot/grub"
111963390b4Svikram #define	GRUB_STAGE2		GRUB_DIR "/stage2"
1127c478bd9Sstevel@tonic-gate #define	GRUB_MENU		"/boot/grub/menu.lst"
1137c478bd9Sstevel@tonic-gate #define	MENU_TMP		"/boot/grub/menu.lst.tmp"
114963390b4Svikram #define	GRUB_BACKUP_MENU	"/etc/lu/GRUB_backup_menu"
1157c478bd9Sstevel@tonic-gate #define	RAMDISK_SPECIAL		"/ramdisk"
11640541d5dSvikram #define	STUBBOOT		"/stubboot"
117eb2bd662Svikram #define	MULTIBOOT		"/platform/i86pc/multiboot"
118eb2bd662Svikram #define	GRUBSIGN_DIR		"/boot/grub/bootsign"
119eb2bd662Svikram #define	GRUBSIGN_BACKUP		"/etc/bootsign"
120eb2bd662Svikram #define	GRUBSIGN_UFS_PREFIX	"rootfs"
121eb2bd662Svikram #define	GRUBSIGN_ZFS_PREFIX	"pool_"
122eb2bd662Svikram #define	GRUBSIGN_LU_PREFIX	"BE_"
123eb2bd662Svikram #define	UFS_SIGNATURE_LIST	"/var/run/grub_ufs_signatures"
124eb2bd662Svikram #define	ZFS_LEGACY_MNTPT	"/tmp/bootadm_mnt_zfs_legacy"
125eb2bd662Svikram 
126eb2bd662Svikram #define	BOOTADM_RDONLY_TEST	"BOOTADM_RDONLY_TEST"
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /* lock related */
1297c478bd9Sstevel@tonic-gate #define	BAM_LOCK_FILE		"/var/run/bootadm.lock"
1307c478bd9Sstevel@tonic-gate #define	LOCK_FILE_PERMS		(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1317c478bd9Sstevel@tonic-gate 
132986fd29aSsetje #define	CREATE_RAMDISK		"boot/solaris/bin/create_ramdisk"
133986fd29aSsetje #define	CREATE_DISKMAP		"boot/solaris/bin/create_diskmap"
134986fd29aSsetje #define	EXTRACT_BOOT_FILELIST	"boot/solaris/bin/extract_boot_filelist"
1357c478bd9Sstevel@tonic-gate #define	GRUBDISK_MAP		"/var/run/solaris_grubdisk.map"
1367c478bd9Sstevel@tonic-gate 
137b610f78eSvikram #define	GRUB_slice		"/etc/lu/GRUB_slice"
138b610f78eSvikram #define	GRUB_root		"/etc/lu/GRUB_root"
139fbac2b2bSvikram #define	GRUB_fdisk		"/etc/lu/GRUB_fdisk"
140fbac2b2bSvikram #define	GRUB_fdisk_target	"/etc/lu/GRUB_fdisk_target"
141963390b4Svikram #define	FINDROOT_INSTALLGRUB	"/etc/lu/installgrub.findroot"
142963390b4Svikram #define	LULIB			"/usr/lib/lu/lulib"
143963390b4Svikram #define	LULIB_PROPAGATE_FILE	"lulib_propagate_file"
144963390b4Svikram #define	CKSUM			"/usr/bin/cksum"
145963390b4Svikram #define	LU_MENU_CKSUM		"/etc/lu/menu.cksum"
146963390b4Svikram #define	BOOTADM			"/sbin/bootadm"
147b610f78eSvikram 
148b610f78eSvikram #define	INSTALLGRUB		"/sbin/installgrub"
149b610f78eSvikram #define	STAGE1			"/boot/grub/stage1"
150b610f78eSvikram #define	STAGE2			"/boot/grub/stage2"
151b610f78eSvikram 
152eb2bd662Svikram typedef enum zfs_mnted {
153eb2bd662Svikram 	ZFS_MNT_ERROR = -1,
154eb2bd662Svikram 	LEGACY_MOUNTED = 1,
155eb2bd662Svikram 	LEGACY_ALREADY,
156eb2bd662Svikram 	ZFS_MOUNTED,
157eb2bd662Svikram 	ZFS_ALREADY
158eb2bd662Svikram } zfs_mnted_t;
159eb2bd662Svikram 
1607c478bd9Sstevel@tonic-gate /*
16198892a30Snadkarni  * The following two defines are used to detect and create the correct
16298892a30Snadkarni  * boot archive  when safemode patching is underway.  LOFS_PATCH_FILE is a
16398892a30Snadkarni  * contracted private interface between bootadm and the install
16498892a30Snadkarni  * consolidation.  It is set by pdo.c when a patch with SUNW_PATCH_SAFEMODE
16598892a30Snadkarni  * is applied.
16698892a30Snadkarni  */
16798892a30Snadkarni #define	LOFS_PATCH_FILE		"/var/run/.patch_loopback_mode"
16898892a30Snadkarni #define	LOFS_PATCH_MNT		"/var/run/.patch_root_loopbackmnt"
16998892a30Snadkarni 
17098892a30Snadkarni /*
1717c478bd9Sstevel@tonic-gate  * Default file attributes
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1747c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1757c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * Menu related
1797c478bd9Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1807c478bd9Sstevel@tonic-gate  */
181ae115bc7Smrj char *menu_cmds[] = {
1827c478bd9Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1837c478bd9Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1847c478bd9Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1857c478bd9Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1867c478bd9Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
187ae115bc7Smrj 	"kernel$",	/* KERNEL_DOLLAR_CMD */
1887c478bd9Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
189ae115bc7Smrj 	"module$",	/* MODULE_DOLLAR_CMD */
1907c478bd9Sstevel@tonic-gate 	" ",		/* SEP_CMD */
1917c478bd9Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
192ae115bc7Smrj 	"chainloader",	/* CHAINLOADER_CMD */
193ae115bc7Smrj 	"args",		/* ARGS_CMD */
194eb2bd662Svikram 	"findroot",	/* FINDROOT_CMD */
1957c478bd9Sstevel@tonic-gate 	NULL
1967c478bd9Sstevel@tonic-gate };
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
201eb2bd662Svikram  * exec_cmd related
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate typedef struct {
2047c478bd9Sstevel@tonic-gate 	line_t *head;
2057c478bd9Sstevel@tonic-gate 	line_t *tail;
2067c478bd9Sstevel@tonic-gate } filelist_t;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
2097c478bd9Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
2127c478bd9Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
2137c478bd9Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
2147c478bd9Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
2157c478bd9Sstevel@tonic-gate 
216*4a9df875SEnrico Perla - Sun Microsystems #define	FILE_STAT_TIMESTAMP	"boot/solaris/timestamp.cache"
217*4a9df875SEnrico Perla - Sun Microsystems 
2187c478bd9Sstevel@tonic-gate /* Globals */
219ae115bc7Smrj int bam_verbose;
220ae115bc7Smrj int bam_force;
221eb2bd662Svikram int bam_debug;
2227c478bd9Sstevel@tonic-gate static char *prog;
2237c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd;
2247c478bd9Sstevel@tonic-gate static char *bam_root;
2257c478bd9Sstevel@tonic-gate static int bam_rootlen;
2267c478bd9Sstevel@tonic-gate static int bam_root_readonly;
22740541d5dSvikram static int bam_alt_root;
22848847494SEnrico Perla - Sun Microsystems static int bam_extend = 0;
22948847494SEnrico Perla - Sun Microsystems static int bam_purge = 0;
2307c478bd9Sstevel@tonic-gate static char *bam_subcmd;
2317c478bd9Sstevel@tonic-gate static char *bam_opt;
2327c478bd9Sstevel@tonic-gate static char **bam_argv;
2337c478bd9Sstevel@tonic-gate static int bam_argc;
2347c478bd9Sstevel@tonic-gate static int bam_check;
2355eea6091SEnrico Perla - Sun Microsystems static int bam_saved_check;
2367c478bd9Sstevel@tonic-gate static int bam_smf_check;
2377c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
238e7cbe64fSgw25295 static int bam_zfs;
2397c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
240b610f78eSvikram static int bam_update_all;
241d876c67dSjg static int bam_alt_platform;
242d876c67dSjg static char *bam_platform;
243cedc7e57SEnrico Perla - Sun Microsystems static char *bam_home_env = NULL;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /* function prototypes */
246986fd29aSsetje static void parse_args_internal(int, char *[]);
247986fd29aSsetje static void parse_args(int, char *argv[]);
248986fd29aSsetje static error_t bam_menu(char *, char *, int, char *[]);
249986fd29aSsetje static error_t bam_archive(char *, char *);
2507c478bd9Sstevel@tonic-gate 
251986fd29aSsetje static void bam_exit(int);
2527c478bd9Sstevel@tonic-gate static void bam_lock(void);
2537c478bd9Sstevel@tonic-gate static void bam_unlock(void);
2547c478bd9Sstevel@tonic-gate 
255986fd29aSsetje static int exec_cmd(char *, filelist_t *);
256986fd29aSsetje static error_t read_globals(menu_t *, char *, char *, int);
257eb2bd662Svikram static int menu_on_bootdisk(char *os_root, char *menu_root);
258986fd29aSsetje static menu_t *menu_read(char *);
259986fd29aSsetje static error_t menu_write(char *, menu_t *);
260986fd29aSsetje static void linelist_free(line_t *);
261986fd29aSsetje static void menu_free(menu_t *);
262986fd29aSsetje static void filelist_free(filelist_t *);
263986fd29aSsetje static error_t list2file(char *, char *, char *, line_t *);
264986fd29aSsetje static error_t list_entry(menu_t *, char *, char *);
265986fd29aSsetje static error_t delete_all_entries(menu_t *, char *, char *);
266eb2bd662Svikram static error_t update_entry(menu_t *mp, char *menu_root, char *opt);
267eb2bd662Svikram static error_t update_temp(menu_t *mp, char *dummy, char *opt);
2687c478bd9Sstevel@tonic-gate 
269986fd29aSsetje static error_t update_archive(char *, char *);
270986fd29aSsetje static error_t list_archive(char *, char *);
271986fd29aSsetje static error_t update_all(char *, char *);
272986fd29aSsetje static error_t read_list(char *, filelist_t *);
273986fd29aSsetje static error_t set_global(menu_t *, char *, int);
274986fd29aSsetje static error_t set_option(menu_t *, char *, char *);
275986fd29aSsetje static error_t set_kernel(menu_t *, menu_cmd_t, char *, char *, size_t);
276eb2bd662Svikram static error_t get_kernel(menu_t *, menu_cmd_t, char *, size_t);
277986fd29aSsetje static char *expand_path(const char *);
2787c478bd9Sstevel@tonic-gate 
279986fd29aSsetje static long s_strtol(char *);
280986fd29aSsetje static int s_fputs(char *, FILE *);
2817c478bd9Sstevel@tonic-gate 
282eb2bd662Svikram static int is_zfs(char *root);
283eb2bd662Svikram static int is_ufs(char *root);
284eb2bd662Svikram static int is_pcfs(char *root);
2857c478bd9Sstevel@tonic-gate static int is_amd64(void);
28679755401Ssetje static char *get_machine(void);
2877c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
288eb2bd662Svikram static char *mount_top_dataset(char *pool, zfs_mnted_t *mnted);
289eb2bd662Svikram static int umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt);
290eb2bd662Svikram static int ufs_add_to_sign_list(char *sign);
291963390b4Svikram static error_t synchronize_BE_menu(void);
2927c478bd9Sstevel@tonic-gate 
293986fd29aSsetje #if !defined(_OPB)
2942449e17fSsherrym static void ucode_install();
2952449e17fSsherrym #endif
2962449e17fSsherrym 
2977c478bd9Sstevel@tonic-gate /* Menu related sub commands */
2987c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
299eb2bd662Svikram 	"set_option",		OPT_ABSENT,	set_option, 0,	/* PUB */
3001a97e40eSvikram 	"list_entry",		OPT_OPTIONAL,	list_entry, 1,	/* PUB */
3011a97e40eSvikram 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, 0, /* PVT */
3021a97e40eSvikram 	"update_entry",		OPT_REQ,	update_entry, 0, /* menu */
3031a97e40eSvikram 	"update_temp",		OPT_OPTIONAL,	update_temp, 0,	/* reboot */
304ae115bc7Smrj 	"upgrade",		OPT_ABSENT,	upgrade_menu, 0, /* menu */
3051a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
3067c478bd9Sstevel@tonic-gate };
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /* Archive related sub commands */
3097c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
3101a97e40eSvikram 	"update",		OPT_ABSENT,	update_archive, 0, /* PUB */
3111a97e40eSvikram 	"update_all",		OPT_ABSENT,	update_all, 0,	/* PVT */
3121a97e40eSvikram 	"list",			OPT_OPTIONAL,	list_archive, 1, /* PUB */
3131a97e40eSvikram 	NULL,			0,		NULL, 0	/* must be last */
3147c478bd9Sstevel@tonic-gate };
3157c478bd9Sstevel@tonic-gate 
31648847494SEnrico Perla - Sun Microsystems enum dircache_copy_opt {
31748847494SEnrico Perla - Sun Microsystems 	FILE32 = 0,
31848847494SEnrico Perla - Sun Microsystems 	FILE64,
31948847494SEnrico Perla - Sun Microsystems 	CACHEDIR_NUM
32048847494SEnrico Perla - Sun Microsystems };
32148847494SEnrico Perla - Sun Microsystems 
32248847494SEnrico Perla - Sun Microsystems /*
32348847494SEnrico Perla - Sun Microsystems  * Directory specific flags:
32448847494SEnrico Perla - Sun Microsystems  * NEED_UPDATE : the specified archive needs to be updated
32548847494SEnrico Perla - Sun Microsystems  * NO_MULTI : don't extend the specified archive, but recreate it
32648847494SEnrico Perla - Sun Microsystems  */
32748847494SEnrico Perla - Sun Microsystems #define	NEED_UPDATE		0x00000001
32848847494SEnrico Perla - Sun Microsystems #define	NO_MULTI		0x00000002
32948847494SEnrico Perla - Sun Microsystems 
33048847494SEnrico Perla - Sun Microsystems #define	set_dir_flag(id, f)	(walk_arg.dirinfo[id].flags |= f)
33148847494SEnrico Perla - Sun Microsystems #define	unset_dir_flag(id, f)	(walk_arg.dirinfo[id].flags &= ~f)
33248847494SEnrico Perla - Sun Microsystems #define	is_dir_flag_on(id, f)	(walk_arg.dirinfo[id].flags & f ? 1 : 0)
33348847494SEnrico Perla - Sun Microsystems 
33448847494SEnrico Perla - Sun Microsystems #define	get_cachedir(id)	(walk_arg.dirinfo[id].cdir_path)
33548847494SEnrico Perla - Sun Microsystems #define	get_updatedir(id)	(walk_arg.dirinfo[id].update_path)
33648847494SEnrico Perla - Sun Microsystems #define	get_count(id)		(walk_arg.dirinfo[id].count)
33748847494SEnrico Perla - Sun Microsystems #define	has_cachedir(id)	(walk_arg.dirinfo[id].has_dir)
33848847494SEnrico Perla - Sun Microsystems #define	set_dir_present(id)	(walk_arg.dirinfo[id].has_dir = 1)
33948847494SEnrico Perla - Sun Microsystems 
34048847494SEnrico Perla - Sun Microsystems /*
34148847494SEnrico Perla - Sun Microsystems  * dirinfo_t (specific cache directory information):
34248847494SEnrico Perla - Sun Microsystems  * cdir_path:   path to the archive cache directory
34348847494SEnrico Perla - Sun Microsystems  * update_path: path to the update directory (contains the files that will be
34448847494SEnrico Perla - Sun Microsystems  *              used to extend the archive)
34548847494SEnrico Perla - Sun Microsystems  * has_dir:	the specified cache directory is active
34648847494SEnrico Perla - Sun Microsystems  * count:	the number of files to update
34748847494SEnrico Perla - Sun Microsystems  * flags:	directory specific flags
34848847494SEnrico Perla - Sun Microsystems  */
34948847494SEnrico Perla - Sun Microsystems typedef struct _dirinfo {
35048847494SEnrico Perla - Sun Microsystems 	char	cdir_path[PATH_MAX];
35148847494SEnrico Perla - Sun Microsystems 	char	update_path[PATH_MAX];
35248847494SEnrico Perla - Sun Microsystems 	int	has_dir;
35348847494SEnrico Perla - Sun Microsystems 	int	count;
35448847494SEnrico Perla - Sun Microsystems 	int	flags;
35548847494SEnrico Perla - Sun Microsystems } dirinfo_t;
35648847494SEnrico Perla - Sun Microsystems 
35748847494SEnrico Perla - Sun Microsystems /*
35848847494SEnrico Perla - Sun Microsystems  * Update flags:
35948847494SEnrico Perla - Sun Microsystems  * NEED_CACHE_DIR : cache directory is missing and needs to be created
36048847494SEnrico Perla - Sun Microsystems  * IS_SPARC_TARGET : the target mountpoint is a SPARC environment
36148847494SEnrico Perla - Sun Microsystems  * UPDATE_ERROR : an error occourred while traversing the list of files
36248847494SEnrico Perla - Sun Microsystems  * RDONLY_FSCHK : the target filesystem is read-only
36348847494SEnrico Perla - Sun Microsystems  * RAMDSK_FSCHK : the target filesystem is on a ramdisk
36448847494SEnrico Perla - Sun Microsystems  */
36548847494SEnrico Perla - Sun Microsystems #define	NEED_CACHE_DIR		0x00000001
36648847494SEnrico Perla - Sun Microsystems #define	IS_SPARC_TARGET		0x00000002
36748847494SEnrico Perla - Sun Microsystems #define	UPDATE_ERROR		0x00000004
36848847494SEnrico Perla - Sun Microsystems #define	RDONLY_FSCHK		0x00000008
369*4a9df875SEnrico Perla - Sun Microsystems #define	INVALIDATE_CACHE	0x00000010
37048847494SEnrico Perla - Sun Microsystems 
37148847494SEnrico Perla - Sun Microsystems #define	is_flag_on(flag)	(walk_arg.update_flags & flag ? 1 : 0)
37248847494SEnrico Perla - Sun Microsystems #define	set_flag(flag)		(walk_arg.update_flags |= flag)
37348847494SEnrico Perla - Sun Microsystems #define	unset_flag(flag)	(walk_arg.update_flags &= ~flag)
37448847494SEnrico Perla - Sun Microsystems 
37548847494SEnrico Perla - Sun Microsystems /*
37648847494SEnrico Perla - Sun Microsystems  * struct walk_arg :
37748847494SEnrico Perla - Sun Microsystems  * update_flags: flags related to the current updating process
37848847494SEnrico Perla - Sun Microsystems  * new_nvlp/old_nvlp: new and old list of archive-files / attributes pairs
37948847494SEnrico Perla - Sun Microsystems  * sparcfile: list of file paths for mkisofs -path-list (SPARC only)
38048847494SEnrico Perla - Sun Microsystems  */
3817c478bd9Sstevel@tonic-gate static struct {
38248847494SEnrico Perla - Sun Microsystems 	int 		update_flags;
3837c478bd9Sstevel@tonic-gate 	nvlist_t 	*new_nvlp;
3847c478bd9Sstevel@tonic-gate 	nvlist_t 	*old_nvlp;
38548847494SEnrico Perla - Sun Microsystems 	FILE 		*sparcfile;
38648847494SEnrico Perla - Sun Microsystems 	dirinfo_t	dirinfo[CACHEDIR_NUM];
3877c478bd9Sstevel@tonic-gate } walk_arg;
3887c478bd9Sstevel@tonic-gate 
3899632cfadSsetje struct safefile {
39058091fd8Ssetje 	char *name;
39158091fd8Ssetje 	struct safefile *next;
39258091fd8Ssetje };
39358091fd8Ssetje 
394ae115bc7Smrj static struct safefile *safefiles = NULL;
39558091fd8Ssetje #define	NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update"
39658091fd8Ssetje 
39748847494SEnrico Perla - Sun Microsystems /* Thanks growisofs */
39848847494SEnrico Perla - Sun Microsystems #define	CD_BLOCK	((off64_t)2048)
39948847494SEnrico Perla - Sun Microsystems #define	VOLDESC_OFF	16
40048847494SEnrico Perla - Sun Microsystems #define	DVD_BLOCK	(32*1024)
40148847494SEnrico Perla - Sun Microsystems #define	MAX_IVDs	16
40248847494SEnrico Perla - Sun Microsystems 
40348847494SEnrico Perla - Sun Microsystems struct iso_pdesc {
40448847494SEnrico Perla - Sun Microsystems     unsigned char type	[1];
40548847494SEnrico Perla - Sun Microsystems     unsigned char id	[5];
40648847494SEnrico Perla - Sun Microsystems     unsigned char void1	[80-5-1];
40748847494SEnrico Perla - Sun Microsystems     unsigned char volume_space_size [8];
40848847494SEnrico Perla - Sun Microsystems     unsigned char void2	[2048-80-8];
40948847494SEnrico Perla - Sun Microsystems };
41048847494SEnrico Perla - Sun Microsystems 
41148847494SEnrico Perla - Sun Microsystems /*
41248847494SEnrico Perla - Sun Microsystems  * COUNT_MAX:	maximum number of changed files to justify a multisession update
41348847494SEnrico Perla - Sun Microsystems  * BA_SIZE_MAX:	maximum size of the boot_archive to justify a multisession
41448847494SEnrico Perla - Sun Microsystems  * 		update
41548847494SEnrico Perla - Sun Microsystems  */
41648847494SEnrico Perla - Sun Microsystems #define	COUNT_MAX		50
41748847494SEnrico Perla - Sun Microsystems #define	BA_SIZE_MAX		(50 * 1024 * 1024)
41848847494SEnrico Perla - Sun Microsystems 
41948847494SEnrico Perla - Sun Microsystems #define	bam_nowrite()		(bam_check || bam_smf_check)
42048847494SEnrico Perla - Sun Microsystems 
42119397407SSherry Moore static int sync_menu = 1;	/* whether we need to sync the BE menus */
42219397407SSherry Moore 
4237c478bd9Sstevel@tonic-gate static void
4247c478bd9Sstevel@tonic-gate usage(void)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/* archive usage */
429d876c67dSjg 	(void) fprintf(stderr,
430d876c67dSjg 	    "\t%s update-archive [-vn] [-R altroot [-p platform>]]\n", prog);
431d876c67dSjg 	(void) fprintf(stderr,
432d876c67dSjg 	    "\t%s list-archive [-R altroot [-p platform>]]\n", prog);
433986fd29aSsetje #if !defined(_OPB)
4347c478bd9Sstevel@tonic-gate 	/* x86 only */
4357c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
4367c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
4377c478bd9Sstevel@tonic-gate #endif
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
440cedc7e57SEnrico Perla - Sun Microsystems /*
441cedc7e57SEnrico Perla - Sun Microsystems  * Best effort attempt to restore the $HOME value.
442cedc7e57SEnrico Perla - Sun Microsystems  */
443cedc7e57SEnrico Perla - Sun Microsystems static void
444cedc7e57SEnrico Perla - Sun Microsystems restore_env()
445cedc7e57SEnrico Perla - Sun Microsystems {
446cedc7e57SEnrico Perla - Sun Microsystems 	char	home_env[PATH_MAX];
447cedc7e57SEnrico Perla - Sun Microsystems 
448cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_home_env) {
449cedc7e57SEnrico Perla - Sun Microsystems 		(void) snprintf(home_env, sizeof (home_env), "HOME=%s",
450cedc7e57SEnrico Perla - Sun Microsystems 		    bam_home_env);
451cedc7e57SEnrico Perla - Sun Microsystems 		(void) putenv(home_env);
452cedc7e57SEnrico Perla - Sun Microsystems 	}
453cedc7e57SEnrico Perla - Sun Microsystems }
454cedc7e57SEnrico Perla - Sun Microsystems 
455cedc7e57SEnrico Perla - Sun Microsystems 
456cedc7e57SEnrico Perla - Sun Microsystems #define		SLEEP_TIME	5
457cedc7e57SEnrico Perla - Sun Microsystems #define		MAX_TRIES	4
458cedc7e57SEnrico Perla - Sun Microsystems 
459cedc7e57SEnrico Perla - Sun Microsystems /*
460cedc7e57SEnrico Perla - Sun Microsystems  * Sanitize the environment in which bootadm will execute its sub-processes
461cedc7e57SEnrico Perla - Sun Microsystems  * (ex. mkisofs). This is done to prevent those processes from attempting
462cedc7e57SEnrico Perla - Sun Microsystems  * to access files (ex. .mkisofsrc) or stat paths that might be on NFS
463cedc7e57SEnrico Perla - Sun Microsystems  * or, potentially, insecure.
464cedc7e57SEnrico Perla - Sun Microsystems  */
465cedc7e57SEnrico Perla - Sun Microsystems static void
466cedc7e57SEnrico Perla - Sun Microsystems sanitize_env()
467cedc7e57SEnrico Perla - Sun Microsystems {
468cedc7e57SEnrico Perla - Sun Microsystems 	int	stry = 0;
469cedc7e57SEnrico Perla - Sun Microsystems 
470cedc7e57SEnrico Perla - Sun Microsystems 	/* don't depend on caller umask */
471cedc7e57SEnrico Perla - Sun Microsystems 	(void) umask(0022);
472cedc7e57SEnrico Perla - Sun Microsystems 
473cedc7e57SEnrico Perla - Sun Microsystems 	/* move away from a potential unsafe current working directory */
474cedc7e57SEnrico Perla - Sun Microsystems 	while (chdir("/") == -1) {
475cedc7e57SEnrico Perla - Sun Microsystems 		if (errno != EINTR) {
476cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to chdir to /");
477cedc7e57SEnrico Perla - Sun Microsystems 			break;
478cedc7e57SEnrico Perla - Sun Microsystems 		}
479cedc7e57SEnrico Perla - Sun Microsystems 	}
480cedc7e57SEnrico Perla - Sun Microsystems 
481cedc7e57SEnrico Perla - Sun Microsystems 	bam_home_env = getenv("HOME");
482cedc7e57SEnrico Perla - Sun Microsystems 	while (bam_home_env != NULL && putenv("HOME=/") == -1) {
483cedc7e57SEnrico Perla - Sun Microsystems 		if (errno == ENOMEM) {
484cedc7e57SEnrico Perla - Sun Microsystems 			/* retry no more than MAX_TRIES times */
485cedc7e57SEnrico Perla - Sun Microsystems 			if (++stry > MAX_TRIES) {
486cedc7e57SEnrico Perla - Sun Microsystems 				bam_print("WARNING: unable to recover from "
487cedc7e57SEnrico Perla - Sun Microsystems 				    "system memory pressure... aborting \n");
488cedc7e57SEnrico Perla - Sun Microsystems 				bam_exit(EXIT_FAILURE);
489cedc7e57SEnrico Perla - Sun Microsystems 			}
490cedc7e57SEnrico Perla - Sun Microsystems 			/* memory is tight, try to sleep */
491cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("Attempting to recover from memory pressure: "
492cedc7e57SEnrico Perla - Sun Microsystems 			    "sleeping for %d seconds\n", SLEEP_TIME * stry);
493cedc7e57SEnrico Perla - Sun Microsystems 			(void) sleep(SLEEP_TIME * stry);
494cedc7e57SEnrico Perla - Sun Microsystems 		} else {
495cedc7e57SEnrico Perla - Sun Microsystems 			bam_print("WARNING: unable to sanitize HOME\n");
496cedc7e57SEnrico Perla - Sun Microsystems 		}
497cedc7e57SEnrico Perla - Sun Microsystems 	}
498cedc7e57SEnrico Perla - Sun Microsystems }
499cedc7e57SEnrico Perla - Sun Microsystems 
5007c478bd9Sstevel@tonic-gate int
5017c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	error_t ret;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5067c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
5097c478bd9Sstevel@tonic-gate 		prog = argv[0];
5107c478bd9Sstevel@tonic-gate 	} else {
5117c478bd9Sstevel@tonic-gate 		prog++;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
514eb2bd662Svikram 	INJECT_ERROR1("ASSERT_ON", assert(0))
5157c478bd9Sstevel@tonic-gate 
516cedc7e57SEnrico Perla - Sun Microsystems 	sanitize_env();
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
5217c478bd9Sstevel@tonic-gate 		case BAM_MENU:
5227c478bd9Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
5237c478bd9Sstevel@tonic-gate 			break;
5247c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
5257c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
5267c478bd9Sstevel@tonic-gate 			break;
5277c478bd9Sstevel@tonic-gate 		default:
5287c478bd9Sstevel@tonic-gate 			usage();
5297c478bd9Sstevel@tonic-gate 			bam_exit(1);
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
5337c478bd9Sstevel@tonic-gate 		bam_exit(1);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	bam_unlock();
5367c478bd9Sstevel@tonic-gate 	return (0);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
5417c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
5427c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
5437c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
5447c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
5457c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate static struct cmd_map {
5487c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
5497c478bd9Sstevel@tonic-gate 	int bam_cmd;
5507c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
5517c478bd9Sstevel@tonic-gate } cmd_map[] = {
5527c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
5537c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
5547c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
5557c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
5567c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
5577c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
5587c478bd9Sstevel@tonic-gate };
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
5627c478bd9Sstevel@tonic-gate  */
5637c478bd9Sstevel@tonic-gate static void
5647c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
5697c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
5707c478bd9Sstevel@tonic-gate 		/*
5717c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
5727c478bd9Sstevel@tonic-gate 		 */
5737c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
5747c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
5757c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
5767c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
5777c478bd9Sstevel@tonic-gate 				break;
5787c478bd9Sstevel@tonic-gate 			}
5797c478bd9Sstevel@tonic-gate 			cmp++;
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
5827c478bd9Sstevel@tonic-gate 			usage();
5837c478bd9Sstevel@tonic-gate 			bam_exit(1);
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 		argc--;
5867c478bd9Sstevel@tonic-gate 		argv++;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate /*
5937c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
5947c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
5957c478bd9Sstevel@tonic-gate  *	-a update	-- update-archive
5967c478bd9Sstevel@tonic-gate  *	-a list		-- list-archive
5977c478bd9Sstevel@tonic-gate  *	-a update-all	-- (reboot to sync all mounted OS archive)
5987c478bd9Sstevel@tonic-gate  *	-m update_entry	-- update-menu
5997c478bd9Sstevel@tonic-gate  *	-m list_entry	-- list-menu
6007c478bd9Sstevel@tonic-gate  *	-m update_temp	-- (reboot -- [boot-args])
6017c478bd9Sstevel@tonic-gate  *	-m delete_all_entries -- (called from install)
60248847494SEnrico Perla - Sun Microsystems  * A set of private flags is there too:
60348847494SEnrico Perla - Sun Microsystems  *	-F		-- purge the cache directories and rebuild them
60448847494SEnrico Perla - Sun Microsystems  *	-e		-- use the (faster) archive update approach (used by
60548847494SEnrico Perla - Sun Microsystems  *			   reboot)
6067c478bd9Sstevel@tonic-gate  */
6077c478bd9Sstevel@tonic-gate static void
6087c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate 	int c, error;
6117c478bd9Sstevel@tonic-gate 	extern char *optarg;
6127c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
6157c478bd9Sstevel@tonic-gate 	opterr = 0;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	error = 0;
61848847494SEnrico Perla - Sun Microsystems 	while ((c = getopt(argc, argv, "a:d:fm:no:veFCR:p:Z")) != -1) {
6197c478bd9Sstevel@tonic-gate 		switch (c) {
6207c478bd9Sstevel@tonic-gate 		case 'a':
6217c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
6227c478bd9Sstevel@tonic-gate 				error = 1;
6237c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
6247c478bd9Sstevel@tonic-gate 			}
6257c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
6267c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
6277c478bd9Sstevel@tonic-gate 			break;
6287c478bd9Sstevel@tonic-gate 		case 'd':
6297c478bd9Sstevel@tonic-gate 			if (bam_debug) {
6307c478bd9Sstevel@tonic-gate 				error = 1;
6317c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
6327c478bd9Sstevel@tonic-gate 			}
6337c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
6347c478bd9Sstevel@tonic-gate 			break;
6357c478bd9Sstevel@tonic-gate 		case 'f':
6367c478bd9Sstevel@tonic-gate 			bam_force = 1;
6377c478bd9Sstevel@tonic-gate 			break;
63848847494SEnrico Perla - Sun Microsystems 		case 'F':
63948847494SEnrico Perla - Sun Microsystems 			bam_purge = 1;
64048847494SEnrico Perla - Sun Microsystems 			break;
6417c478bd9Sstevel@tonic-gate 		case 'm':
6427c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
6437c478bd9Sstevel@tonic-gate 				error = 1;
6447c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
6457c478bd9Sstevel@tonic-gate 			}
6467c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
6477c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
6487c478bd9Sstevel@tonic-gate 			break;
6497c478bd9Sstevel@tonic-gate 		case 'n':
6507c478bd9Sstevel@tonic-gate 			bam_check = 1;
6515eea6091SEnrico Perla - Sun Microsystems 			/*
6525eea6091SEnrico Perla - Sun Microsystems 			 * We save the original value of bam_check. The new
6535eea6091SEnrico Perla - Sun Microsystems 			 * approach in case of a read-only filesystem is to
6545eea6091SEnrico Perla - Sun Microsystems 			 * behave as a check, so we need a way to restore the
6555eea6091SEnrico Perla - Sun Microsystems 			 * original value after the evaluation of the read-only
6565eea6091SEnrico Perla - Sun Microsystems 			 * filesystem has been done.
6575eea6091SEnrico Perla - Sun Microsystems 			 * Even if we don't allow at the moment a check with
6585eea6091SEnrico Perla - Sun Microsystems 			 * update_all, this approach is more robust than
6595eea6091SEnrico Perla - Sun Microsystems 			 * simply resetting bam_check to zero.
6605eea6091SEnrico Perla - Sun Microsystems 			 */
6615eea6091SEnrico Perla - Sun Microsystems 			bam_saved_check = 1;
6627c478bd9Sstevel@tonic-gate 			break;
6637c478bd9Sstevel@tonic-gate 		case 'o':
6647c478bd9Sstevel@tonic-gate 			if (bam_opt) {
6657c478bd9Sstevel@tonic-gate 				error = 1;
6667c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
6677c478bd9Sstevel@tonic-gate 			}
6687c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
6697c478bd9Sstevel@tonic-gate 			break;
6707c478bd9Sstevel@tonic-gate 		case 'v':
6717c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
6727c478bd9Sstevel@tonic-gate 			break;
6738c1b6884Sszhou 		case 'C':
6748c1b6884Sszhou 			bam_smf_check = 1;
6758c1b6884Sszhou 			break;
6767c478bd9Sstevel@tonic-gate 		case 'R':
6777c478bd9Sstevel@tonic-gate 			if (bam_root) {
6787c478bd9Sstevel@tonic-gate 				error = 1;
6797c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
6807c478bd9Sstevel@tonic-gate 				break;
6817c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
6827c478bd9Sstevel@tonic-gate 				error = 1;
6837c478bd9Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
6847c478bd9Sstevel@tonic-gate 				    strerror(errno));
6857c478bd9Sstevel@tonic-gate 				break;
6867c478bd9Sstevel@tonic-gate 			}
68740541d5dSvikram 			bam_alt_root = 1;
6887c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
6897c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
6907c478bd9Sstevel@tonic-gate 			break;
691d876c67dSjg 		case 'p':
692d876c67dSjg 			bam_alt_platform = 1;
693d876c67dSjg 			bam_platform = optarg;
694d876c67dSjg 			if ((strcmp(bam_platform, "i86pc") != 0) &&
695d876c67dSjg 			    (strcmp(bam_platform, "sun4u") != 0) &&
696d876c67dSjg 			    (strcmp(bam_platform, "sun4v") != 0)) {
697d876c67dSjg 				error = 1;
698d876c67dSjg 				bam_error(INVALID_PLAT, bam_platform);
699d876c67dSjg 			}
700d876c67dSjg 			break;
701e7cbe64fSgw25295 		case 'Z':
702e7cbe64fSgw25295 			bam_zfs = 1;
703e7cbe64fSgw25295 			break;
70448847494SEnrico Perla - Sun Microsystems 		case 'e':
70548847494SEnrico Perla - Sun Microsystems 			bam_extend = 1;
70648847494SEnrico Perla - Sun Microsystems 			break;
7077c478bd9Sstevel@tonic-gate 		case '?':
7087c478bd9Sstevel@tonic-gate 			error = 1;
7097c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
7107c478bd9Sstevel@tonic-gate 			break;
7117c478bd9Sstevel@tonic-gate 		default :
7127c478bd9Sstevel@tonic-gate 			error = 1;
7137c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
7147c478bd9Sstevel@tonic-gate 			break;
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/*
719d876c67dSjg 	 * An alternate platform requires an alternate root
720d876c67dSjg 	 */
721d876c67dSjg 	if (bam_alt_platform && bam_alt_root == 0) {
722d876c67dSjg 		usage();
723d876c67dSjg 		bam_exit(0);
724d876c67dSjg 	}
725d876c67dSjg 
726d876c67dSjg 	/*
7277c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
7287c478bd9Sstevel@tonic-gate 	 */
7297c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
7307c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
7317c478bd9Sstevel@tonic-gate 			usage();
7327c478bd9Sstevel@tonic-gate 			bam_exit(0);
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 		bam_error(NEED_CMD);
7357c478bd9Sstevel@tonic-gate 		error = 1;
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	if (error) {
7397c478bd9Sstevel@tonic-gate 		usage();
7407c478bd9Sstevel@tonic-gate 		bam_exit(1);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	if (optind > argc) {
7447c478bd9Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
7457c478bd9Sstevel@tonic-gate 		bam_exit(1);
7467c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
7477c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
7487c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/*
7527c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
7537c478bd9Sstevel@tonic-gate 	 */
7547c478bd9Sstevel@tonic-gate 	if (bam_check)
7557c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate static error_t
7597c478bd9Sstevel@tonic-gate check_subcmd_and_options(
7607c478bd9Sstevel@tonic-gate 	char *subcmd,
7617c478bd9Sstevel@tonic-gate 	char *opt,
7627c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
7637c478bd9Sstevel@tonic-gate 	error_t (**fp)())
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate 	int i;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
7687c478bd9Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
7697c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 
772eb2bd662Svikram 	if (strcmp(subcmd, "set_option") == 0) {
773eb2bd662Svikram 		if (bam_argc == 0 || bam_argv == NULL || bam_argv[0] == NULL) {
774eb2bd662Svikram 			bam_error(MISSING_ARG);
775eb2bd662Svikram 			usage();
776eb2bd662Svikram 			return (BAM_ERROR);
777eb2bd662Svikram 		} else if (bam_argc > 1 || bam_argv[1] != NULL) {
7781a97e40eSvikram 			bam_error(TRAILING_ARGS);
7791a97e40eSvikram 			usage();
7801a97e40eSvikram 			return (BAM_ERROR);
7811a97e40eSvikram 		}
78219397407SSherry Moore 	} else if (strcmp(subcmd, "update_all") == 0) {
78319397407SSherry Moore 		/*
78419397407SSherry Moore 		 * The only option we accept for the "update_all"
78519397407SSherry Moore 		 * subcmd is "fastboot".
78619397407SSherry Moore 		 */
78719397407SSherry Moore 		if (bam_argc > 1 || (bam_argc == 1 &&
78819397407SSherry Moore 		    strcmp(bam_argv[0], "fastboot") != 0)) {
78919397407SSherry Moore 			bam_error(TRAILING_ARGS);
79019397407SSherry Moore 			usage();
79119397407SSherry Moore 			return (BAM_ERROR);
79219397407SSherry Moore 		}
79319397407SSherry Moore 		if (bam_argc == 1)
79419397407SSherry Moore 			sync_menu = 0;
795eb2bd662Svikram 	} else if (bam_argc || bam_argv) {
796eb2bd662Svikram 		bam_error(TRAILING_ARGS);
797eb2bd662Svikram 		usage();
798eb2bd662Svikram 		return (BAM_ERROR);
7991a97e40eSvikram 	}
8001a97e40eSvikram 
8017c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
8027c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
8037c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
8077c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
8087c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
8097c478bd9Sstevel@tonic-gate 			break;
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
8137c478bd9Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
8147c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 
8171a97e40eSvikram 	if (table[i].unpriv == 0 && geteuid() != 0) {
8181a97e40eSvikram 		bam_error(MUST_BE_ROOT);
8191a97e40eSvikram 		return (BAM_ERROR);
8201a97e40eSvikram 	}
8211a97e40eSvikram 
8221a97e40eSvikram 	/*
8231a97e40eSvikram 	 * Currently only privileged commands need a lock
8241a97e40eSvikram 	 */
8251a97e40eSvikram 	if (table[i].unpriv == 0)
8261a97e40eSvikram 		bam_lock();
8271a97e40eSvikram 
8287c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
8297c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
8307c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
8317c478bd9Sstevel@tonic-gate 			if (opt)
8327c478bd9Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
8337c478bd9Sstevel@tonic-gate 			else
8347c478bd9Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
8357c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
8367c478bd9Sstevel@tonic-gate 		}
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
84440541d5dSvikram /*
84540541d5dSvikram  * NOTE: A single "/" is also considered a trailing slash and will
84640541d5dSvikram  * be deleted.
84740541d5dSvikram  */
84840541d5dSvikram static void
84940541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize)
85040541d5dSvikram {
85140541d5dSvikram 	size_t dstlen;
85240541d5dSvikram 
85340541d5dSvikram 	assert(src);
85440541d5dSvikram 	assert(dst);
85540541d5dSvikram 
85640541d5dSvikram 	(void) strlcpy(dst, src, dstsize);
85740541d5dSvikram 
85840541d5dSvikram 	dstlen = strlen(dst);
85940541d5dSvikram 	if (dst[dstlen - 1] == '/') {
86040541d5dSvikram 		dst[dstlen - 1] = '\0';
86140541d5dSvikram 	}
86240541d5dSvikram }
86340541d5dSvikram 
864cedc7e57SEnrico Perla - Sun Microsystems static int
865cedc7e57SEnrico Perla - Sun Microsystems is_safe_exec(char *path)
866cedc7e57SEnrico Perla - Sun Microsystems {
867cedc7e57SEnrico Perla - Sun Microsystems 	struct stat	sb;
868cedc7e57SEnrico Perla - Sun Microsystems 
869cedc7e57SEnrico Perla - Sun Microsystems 	if (lstat(path, &sb) != 0) {
870cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(STAT_FAIL, path, strerror(errno));
871cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
872cedc7e57SEnrico Perla - Sun Microsystems 	}
873cedc7e57SEnrico Perla - Sun Microsystems 
874cedc7e57SEnrico Perla - Sun Microsystems 	if (!S_ISREG(sb.st_mode)) {
875cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_LINK, path);
876cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
877cedc7e57SEnrico Perla - Sun Microsystems 	}
878cedc7e57SEnrico Perla - Sun Microsystems 
879cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_uid != getuid()) {
880cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_OWNER, path, getuid());
881cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
882cedc7e57SEnrico Perla - Sun Microsystems 	}
883cedc7e57SEnrico Perla - Sun Microsystems 
884cedc7e57SEnrico Perla - Sun Microsystems 	if (sb.st_mode & S_IWOTH || sb.st_mode & S_IWGRP) {
885cedc7e57SEnrico Perla - Sun Microsystems 		bam_error(PATH_EXEC_PERMS, path);
886cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
887cedc7e57SEnrico Perla - Sun Microsystems 	}
888cedc7e57SEnrico Perla - Sun Microsystems 
889cedc7e57SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
890cedc7e57SEnrico Perla - Sun Microsystems }
891cedc7e57SEnrico Perla - Sun Microsystems 
8927c478bd9Sstevel@tonic-gate static error_t
8937c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
8947c478bd9Sstevel@tonic-gate {
8957c478bd9Sstevel@tonic-gate 	error_t			ret;
8967c478bd9Sstevel@tonic-gate 	char			menu_path[PATH_MAX];
897eb2bd662Svikram 	char			clean_menu_root[PATH_MAX];
898ae115bc7Smrj 	char			path[PATH_MAX];
8997c478bd9Sstevel@tonic-gate 	menu_t			*menu;
900eb2bd662Svikram 	char			menu_root[PATH_MAX];
901b610f78eSvikram 	struct stat		sb;
9027c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
903eb2bd662Svikram 	char			*special;
904eb2bd662Svikram 	char			*pool = NULL;
905eb2bd662Svikram 	zfs_mnted_t		zmnted;
906eb2bd662Svikram 	char			*zmntpt;
907eb2bd662Svikram 	char			*osdev;
908eb2bd662Svikram 	char			*osroot;
909eb2bd662Svikram 	const char		*fcn = "bam_menu()";
910986fd29aSsetje 
911986fd29aSsetje 	/*
912986fd29aSsetje 	 * Menu sub-command only applies to GRUB (i.e. x86)
913986fd29aSsetje 	 */
914eb2bd662Svikram 	if (!is_grub(bam_alt_root ? bam_root : "/")) {
915eb2bd662Svikram 		bam_error(NOT_GRUB_BOOT);
916986fd29aSsetje 		return (BAM_ERROR);
917986fd29aSsetje 	}
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	/*
9207c478bd9Sstevel@tonic-gate 	 * Check arguments
9217c478bd9Sstevel@tonic-gate 	 */
9227c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
9237c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
9247c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
927eb2bd662Svikram 	assert(bam_root);
928eb2bd662Svikram 
929eb2bd662Svikram 	(void) strlcpy(menu_root, bam_root, sizeof (menu_root));
930eb2bd662Svikram 	osdev = osroot = NULL;
931eb2bd662Svikram 
932eb2bd662Svikram 	if (strcmp(subcmd, "update_entry") == 0) {
933eb2bd662Svikram 		assert(opt);
934eb2bd662Svikram 
935eb2bd662Svikram 		osdev = strtok(opt, ",");
936eb2bd662Svikram 		assert(osdev);
937eb2bd662Svikram 		osroot = strtok(NULL, ",");
938eb2bd662Svikram 		if (osroot) {
939eb2bd662Svikram 			/* fixup bam_root so that it points at osroot */
940eb2bd662Svikram 			if (realpath(osroot, rootbuf) == NULL) {
941eb2bd662Svikram 				bam_error(CANT_RESOLVE, osroot,
942eb2bd662Svikram 				    strerror(errno));
943eb2bd662Svikram 				return (BAM_ERROR);
944eb2bd662Svikram 			}
945eb2bd662Svikram 			bam_alt_root = 1;
946eb2bd662Svikram 			bam_root  = rootbuf;
947eb2bd662Svikram 			bam_rootlen = strlen(rootbuf);
948eb2bd662Svikram 		}
949eb2bd662Svikram 	}
95040541d5dSvikram 
95140541d5dSvikram 	/*
952eb2bd662Svikram 	 * We support menu on PCFS (under certain conditions), but
953eb2bd662Svikram 	 * not the OS root
95440541d5dSvikram 	 */
955eb2bd662Svikram 	if (is_pcfs(bam_root)) {
956eb2bd662Svikram 		bam_error(PCFS_ROOT_NOTSUP, bam_root);
957eb2bd662Svikram 		return (BAM_ERROR);
958ae115bc7Smrj 	}
959ae115bc7Smrj 
960eb2bd662Svikram 	if (stat(menu_root, &sb) == -1) {
96140541d5dSvikram 		bam_error(CANNOT_LOCATE_GRUB_MENU);
96240541d5dSvikram 		return (BAM_ERROR);
96340541d5dSvikram 	}
96440541d5dSvikram 
965eb2bd662Svikram 	BAM_DPRINTF((D_MENU_ROOT, fcn, menu_root));
966e7cbe64fSgw25295 
967eb2bd662Svikram 	/*
968eb2bd662Svikram 	 * We no longer use the GRUB slice file. If it exists, then
969eb2bd662Svikram 	 * the user is doing something that is unsupported (such as
970eb2bd662Svikram 	 * standard upgrading an old Live Upgrade BE). If that
971eb2bd662Svikram 	 * happens, mimic existing behavior i.e. pretend that it is
972eb2bd662Svikram 	 * not a BE. Emit a warning though.
973eb2bd662Svikram 	 */
974eb2bd662Svikram 	if (bam_alt_root) {
975eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s%s", bam_root,
976eb2bd662Svikram 		    GRUB_slice);
977eb2bd662Svikram 	} else {
978eb2bd662Svikram 		(void) snprintf(path, sizeof (path), "%s", GRUB_slice);
979e7cbe64fSgw25295 	}
980e7cbe64fSgw25295 
98137eb779cSVikram Hegde 	if (bam_verbose && stat(path, &sb) == 0)
982eb2bd662Svikram 		bam_error(GRUB_SLICE_FILE_EXISTS, path);
983eb2bd662Svikram 
984eb2bd662Svikram 	if (is_zfs(menu_root)) {
985eb2bd662Svikram 		assert(strcmp(menu_root, bam_root) == 0);
986eb2bd662Svikram 		special = get_special(menu_root);
987eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_GET_SPECIAL", special = NULL);
988eb2bd662Svikram 		if (special == NULL) {
989eb2bd662Svikram 			bam_error(CANT_FIND_SPECIAL, menu_root);
990eb2bd662Svikram 			return (BAM_ERROR);
991eb2bd662Svikram 		}
992eb2bd662Svikram 		pool = strtok(special, "/");
993eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_GET_POOL", pool = NULL);
994eb2bd662Svikram 		if (pool == NULL) {
995eb2bd662Svikram 			free(special);
996eb2bd662Svikram 			bam_error(CANT_FIND_POOL, menu_root);
997eb2bd662Svikram 			return (BAM_ERROR);
998eb2bd662Svikram 		}
999eb2bd662Svikram 		BAM_DPRINTF((D_Z_MENU_GET_POOL_FROM_SPECIAL, fcn, pool));
1000eb2bd662Svikram 
1001eb2bd662Svikram 		zmntpt = mount_top_dataset(pool, &zmnted);
1002eb2bd662Svikram 		INJECT_ERROR1("Z_MENU_MOUNT_TOP_DATASET", zmntpt = NULL);
1003eb2bd662Svikram 		if (zmntpt == NULL) {
1004eb2bd662Svikram 			bam_error(CANT_MOUNT_POOL_DATASET, pool);
1005eb2bd662Svikram 			free(special);
1006eb2bd662Svikram 			return (BAM_ERROR);
1007eb2bd662Svikram 		}
1008eb2bd662Svikram 		BAM_DPRINTF((D_Z_GET_MENU_MOUNT_TOP_DATASET, fcn, zmntpt));
1009eb2bd662Svikram 
1010eb2bd662Svikram 		(void) strlcpy(menu_root, zmntpt, sizeof (menu_root));
1011eb2bd662Svikram 		BAM_DPRINTF((D_Z_GET_MENU_MENU_ROOT, fcn, menu_root));
1012eb2bd662Svikram 	}
1013eb2bd662Svikram 
1014eb2bd662Svikram 	elide_trailing_slash(menu_root, clean_menu_root,
1015eb2bd662Svikram 	    sizeof (clean_menu_root));
1016eb2bd662Svikram 
1017eb2bd662Svikram 	BAM_DPRINTF((D_CLEAN_MENU_ROOT, fcn, clean_menu_root));
1018eb2bd662Svikram 
1019eb2bd662Svikram 	(void) strlcpy(menu_path, clean_menu_root, sizeof (menu_path));
102040541d5dSvikram 	(void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path));
102140541d5dSvikram 
1022eb2bd662Svikram 	BAM_DPRINTF((D_MENU_PATH, fcn, menu_path));
1023eb2bd662Svikram 
102440541d5dSvikram 	/*
1025eb2bd662Svikram 	 * If listing the menu, display the menu location
102640541d5dSvikram 	 */
102740541d5dSvikram 	if (strcmp(subcmd, "list_entry") == 0) {
1028eb2bd662Svikram 		bam_print(GRUB_MENU_PATH, menu_path);
102940541d5dSvikram 	}
10307c478bd9Sstevel@tonic-gate 
1031eb2bd662Svikram 
10327c478bd9Sstevel@tonic-gate 	menu = menu_read(menu_path);
10337c478bd9Sstevel@tonic-gate 	assert(menu);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/*
1036eb2bd662Svikram 	 * We already checked the following case in
1037eb2bd662Svikram 	 * check_subcmd_and_suboptions() above. Complete the
1038eb2bd662Svikram 	 * final step now.
10397c478bd9Sstevel@tonic-gate 	 */
10407c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
1041eb2bd662Svikram 		assert(largc == 1 && largv[0] && largv[1] == NULL);
10427c478bd9Sstevel@tonic-gate 		opt = largv[0];
1043eb2bd662Svikram 	} else {
1044eb2bd662Svikram 		assert(largc == 0 && largv == NULL);
10457c478bd9Sstevel@tonic-gate 	}
10467c478bd9Sstevel@tonic-gate 
1047eb2bd662Svikram 	ret = get_boot_cap(bam_root);
1048eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1049eb2bd662Svikram 		BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn));
1050eb2bd662Svikram 		goto out;
1051eb2bd662Svikram 	}
1052ae115bc7Smrj 
10537c478bd9Sstevel@tonic-gate 	/*
10547c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
10557c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
10567c478bd9Sstevel@tonic-gate 	 */
1057eb2bd662Svikram 	if (strcmp(subcmd, "update_entry") == 0)
1058eb2bd662Svikram 		ret = f(menu, menu_root, osdev);
1059eb2bd662Svikram 	else if (strcmp(subcmd, "upgrade") == 0)
1060eb2bd662Svikram 		ret = f(menu, bam_root, menu_root);
1061eb2bd662Svikram 	else if (strcmp(subcmd, "list_entry") == 0)
10627c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
1063eb2bd662Svikram 	else
1064eb2bd662Svikram 		ret = f(menu, NULL, opt);
1065eb2bd662Svikram 
10667c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
1067eb2bd662Svikram 		BAM_DPRINTF((D_WRITING_MENU_ROOT, fcn, clean_menu_root));
1068eb2bd662Svikram 		ret = menu_write(clean_menu_root, menu);
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 
1071eb2bd662Svikram out:
1072eb2bd662Svikram 	INJECT_ERROR1("POOL_SET", pool = "/pooldata");
1073eb2bd662Svikram 	assert((is_zfs(menu_root)) ^ (pool == NULL));
1074eb2bd662Svikram 	if (pool) {
1075eb2bd662Svikram 		(void) umount_top_dataset(pool, zmnted, zmntpt);
1076eb2bd662Svikram 		free(special);
1077eb2bd662Svikram 	}
10787c478bd9Sstevel@tonic-gate 	menu_free(menu);
10797c478bd9Sstevel@tonic-gate 	return (ret);
10807c478bd9Sstevel@tonic-gate }
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate static error_t
10847c478bd9Sstevel@tonic-gate bam_archive(
10857c478bd9Sstevel@tonic-gate 	char *subcmd,
10867c478bd9Sstevel@tonic-gate 	char *opt)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate 	error_t			ret;
10897c478bd9Sstevel@tonic-gate 	error_t			(*f)(char *root, char *opt);
1090eb2bd662Svikram 	const char		*fcn = "bam_archive()";
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/*
10938c1b6884Sszhou 	 * Add trailing / for archive subcommands
10948c1b6884Sszhou 	 */
10958c1b6884Sszhou 	if (rootbuf[strlen(rootbuf) - 1] != '/')
10968c1b6884Sszhou 		(void) strcat(rootbuf, "/");
10978c1b6884Sszhou 	bam_rootlen = strlen(rootbuf);
10988c1b6884Sszhou 
10998c1b6884Sszhou 	/*
11007c478bd9Sstevel@tonic-gate 	 * Check arguments
11017c478bd9Sstevel@tonic-gate 	 */
11027c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
11037c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
11047c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 
1107eb2bd662Svikram 	ret = get_boot_cap(rootbuf);
1108eb2bd662Svikram 	if (ret != BAM_SUCCESS) {
1109eb2bd662Svikram 		BAM_DPRINTF((D_BOOT_GET_CAP_FAILED, fcn));
1110ae115bc7Smrj 		return (ret);
1111eb2bd662Svikram 	}
1112ae115bc7Smrj 
11137c478bd9Sstevel@tonic-gate 	/*
11147c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
11157c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
11167c478bd9Sstevel@tonic-gate 	 * information for each BE.
11177c478bd9Sstevel@tonic-gate 	 */
11187c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
11197c478bd9Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
11207c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 
1123b610f78eSvikram 	if (strcmp(subcmd, "update_all") == 0)
1124b610f78eSvikram 		bam_update_all = 1;
1125b610f78eSvikram 
1126986fd29aSsetje #if !defined(_OPB)
11272449e17fSsherrym 	ucode_install(bam_root);
11282449e17fSsherrym #endif
11292449e17fSsherrym 
1130b610f78eSvikram 	ret = f(bam_root, opt);
1131b610f78eSvikram 
1132b610f78eSvikram 	bam_update_all = 0;
1133b610f78eSvikram 
1134b610f78eSvikram 	return (ret);
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1138ae115bc7Smrj void
11397c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 	va_list ap;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	va_start(ap, format);
11447c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
11457c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
11467c478bd9Sstevel@tonic-gate 	va_end(ap);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1150eb2bd662Svikram void
1151eb2bd662Svikram bam_derror(char *format, ...)
1152eb2bd662Svikram {
1153eb2bd662Svikram 	va_list ap;
1154eb2bd662Svikram 
1155eb2bd662Svikram 	assert(bam_debug);
1156eb2bd662Svikram 
1157eb2bd662Svikram 	va_start(ap, format);
1158eb2bd662Svikram 	(void) fprintf(stderr, "DEBUG: ");
1159eb2bd662Svikram 	(void) vfprintf(stderr, format, ap);
1160eb2bd662Svikram 	va_end(ap);
1161eb2bd662Svikram }
1162eb2bd662Svikram 
1163eb2bd662Svikram /*PRINTFLIKE1*/
1164eb2bd662Svikram void
11657c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
11667c478bd9Sstevel@tonic-gate {
11677c478bd9Sstevel@tonic-gate 	va_list ap;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	va_start(ap, format);
11707c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
11717c478bd9Sstevel@tonic-gate 	va_end(ap);
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate 
1174ae115bc7Smrj /*PRINTFLIKE1*/
1175ae115bc7Smrj void
1176ae115bc7Smrj bam_print_stderr(char *format, ...)
1177ae115bc7Smrj {
1178ae115bc7Smrj 	va_list ap;
1179ae115bc7Smrj 
1180ae115bc7Smrj 	va_start(ap, format);
1181ae115bc7Smrj 	(void) vfprintf(stderr, format, ap);
1182ae115bc7Smrj 	va_end(ap);
1183ae115bc7Smrj }
1184ae115bc7Smrj 
11857c478bd9Sstevel@tonic-gate static void
11867c478bd9Sstevel@tonic-gate bam_exit(int excode)
11877c478bd9Sstevel@tonic-gate {
1188cedc7e57SEnrico Perla - Sun Microsystems 	restore_env();
11897c478bd9Sstevel@tonic-gate 	bam_unlock();
11907c478bd9Sstevel@tonic-gate 	exit(excode);
11917c478bd9Sstevel@tonic-gate }
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate static void
11947c478bd9Sstevel@tonic-gate bam_lock(void)
11957c478bd9Sstevel@tonic-gate {
11967c478bd9Sstevel@tonic-gate 	struct flock lock;
11977c478bd9Sstevel@tonic-gate 	pid_t pid;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
12007c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
12017c478bd9Sstevel@tonic-gate 		/*
12027c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
12037c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
12047c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
12057c478bd9Sstevel@tonic-gate 		 */
12067c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
12077c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
12087c478bd9Sstevel@tonic-gate 			return;
12097c478bd9Sstevel@tonic-gate 		}
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
12127c478bd9Sstevel@tonic-gate 		bam_exit(1);
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
12167c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
12177c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
12187c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
12217c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
12227c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
12237c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
12247c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
12257c478bd9Sstevel@tonic-gate 			bam_exit(1);
12267c478bd9Sstevel@tonic-gate 		}
12277c478bd9Sstevel@tonic-gate 		pid = 0;
12287c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
12297c478bd9Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
12327c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
12337c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
12347c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
12357c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
12367c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
12377c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
12387c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
12397c478bd9Sstevel@tonic-gate 			bam_exit(1);
12407c478bd9Sstevel@tonic-gate 		}
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/* We own the lock now */
12447c478bd9Sstevel@tonic-gate 	pid = getpid();
12457c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate static void
12497c478bd9Sstevel@tonic-gate bam_unlock(void)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate 	struct flock unlock;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	/*
12547c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
12557c478bd9Sstevel@tonic-gate 	 */
12567c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
12577c478bd9Sstevel@tonic-gate 		return;
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
12617c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
12627c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
12637c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
12667c478bd9Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
12677c478bd9Sstevel@tonic-gate 	}
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
12707c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
12717c478bd9Sstevel@tonic-gate 	}
12727c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
12737c478bd9Sstevel@tonic-gate }
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate static error_t
12767c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
12777c478bd9Sstevel@tonic-gate {
12787c478bd9Sstevel@tonic-gate 	filelist_t flist;
12797c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
12807c478bd9Sstevel@tonic-gate 	line_t *lp;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	assert(root);
12837c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
12867c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
12877c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
12887c478bd9Sstevel@tonic-gate 	}
12897c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
12927c478bd9Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate /*
13017c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
13027c478bd9Sstevel@tonic-gate  * The list is *not* freed
13037c478bd9Sstevel@tonic-gate  */
13047c478bd9Sstevel@tonic-gate static error_t
13057c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
13067c478bd9Sstevel@tonic-gate {
13077c478bd9Sstevel@tonic-gate 	char		tmpfile[PATH_MAX];
13087c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
13097c478bd9Sstevel@tonic-gate 	FILE		*fp;
13107c478bd9Sstevel@tonic-gate 	int		ret;
13117c478bd9Sstevel@tonic-gate 	struct stat	sb;
13127c478bd9Sstevel@tonic-gate 	mode_t		mode;
13137c478bd9Sstevel@tonic-gate 	uid_t		root_uid;
13147c478bd9Sstevel@tonic-gate 	gid_t		sys_gid;
13157c478bd9Sstevel@tonic-gate 	struct passwd	*pw;
13167c478bd9Sstevel@tonic-gate 	struct group	*gp;
1317eb2bd662Svikram 	const char	*fcn = "list2file()";
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	if (start == NULL) {
1322eb2bd662Svikram 		/* Empty GRUB menu */
13237c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
13247c478bd9Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
13257c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
13267c478bd9Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
13277c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
13287c478bd9Sstevel@tonic-gate 			} else {
13297c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
13307c478bd9Sstevel@tonic-gate 			}
13317c478bd9Sstevel@tonic-gate 		}
1332eb2bd662Svikram 		return (BAM_SUCCESS);
13337c478bd9Sstevel@tonic-gate 	}
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	/*
13367c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
13377c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
13387c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
13397c478bd9Sstevel@tonic-gate 	 */
13407c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
13417c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
13427c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
13437c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
13447c478bd9Sstevel@tonic-gate 	} else {
13457c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
13467c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
13477c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
13487c478bd9Sstevel@tonic-gate 		} else {
13497c478bd9Sstevel@tonic-gate 			bam_error(CANT_FIND_USER,
13507c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
13517c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
13527c478bd9Sstevel@tonic-gate 		}
13537c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
13547c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
13557c478bd9Sstevel@tonic-gate 		} else {
13567c478bd9Sstevel@tonic-gate 			bam_error(CANT_FIND_GROUP,
13577c478bd9Sstevel@tonic-gate 			    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
13587c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
13597c478bd9Sstevel@tonic-gate 		}
13607c478bd9Sstevel@tonic-gate 	}
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
13657c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
13667c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
13677c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
13687c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13697c478bd9Sstevel@tonic-gate 	}
1370963390b4Svikram 	ret = fclose(fp);
1371963390b4Svikram 	INJECT_ERROR1("LIST2FILE_TRUNC_FCLOSE", ret = EOF);
1372963390b4Svikram 	if (ret == EOF) {
13737c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
13747c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13757c478bd9Sstevel@tonic-gate 	}
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
13787c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
13797c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
13807c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
13817c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13827c478bd9Sstevel@tonic-gate 	}
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
1385963390b4Svikram 		ret = s_fputs(start->line, fp);
1386963390b4Svikram 		INJECT_ERROR1("LIST2FILE_FPUTS", ret = EOF);
1387963390b4Svikram 		if (ret == EOF) {
13887c478bd9Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
13897c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
13907c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
13917c478bd9Sstevel@tonic-gate 		}
13927c478bd9Sstevel@tonic-gate 	}
13937c478bd9Sstevel@tonic-gate 
1394963390b4Svikram 	ret = fclose(fp);
1395963390b4Svikram 	INJECT_ERROR1("LIST2FILE_APPEND_FCLOSE", ret = EOF);
1396963390b4Svikram 	if (ret == EOF) {
13977c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
13987c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13997c478bd9Sstevel@tonic-gate 	}
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	/*
1402de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
1403de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
1404de531be4Sjg 	 * operations as EINVAL.
14057c478bd9Sstevel@tonic-gate 	 */
14067c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
1407de531be4Sjg 	if (ret == -1 &&
1408de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
14097c478bd9Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
14107c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
1414de531be4Sjg 	if (ret == -1 &&
1415de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
14167c478bd9Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
14177c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14187c478bd9Sstevel@tonic-gate 	}
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	/*
14227c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
14237c478bd9Sstevel@tonic-gate 	 */
14247c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
1425963390b4Svikram 	INJECT_ERROR1("LIST2FILE_RENAME", ret = -1);
14267c478bd9Sstevel@tonic-gate 	if (ret != 0) {
14277c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
14287c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14297c478bd9Sstevel@tonic-gate 	}
14307c478bd9Sstevel@tonic-gate 
1431eb2bd662Svikram 	BAM_DPRINTF((D_WROTE_FILE, fcn, path));
14327c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
14337c478bd9Sstevel@tonic-gate }
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate /*
143648847494SEnrico Perla - Sun Microsystems  * Checks if the path specified (without the file name at the end) exists
143748847494SEnrico Perla - Sun Microsystems  * and creates it if not. If the path exists and is not a directory, an attempt
143848847494SEnrico Perla - Sun Microsystems  * to unlink is made.
14397c478bd9Sstevel@tonic-gate  */
144048847494SEnrico Perla - Sun Microsystems static int
144148847494SEnrico Perla - Sun Microsystems setup_path(char *path)
144248847494SEnrico Perla - Sun Microsystems {
144348847494SEnrico Perla - Sun Microsystems 	char 		*p;
144448847494SEnrico Perla - Sun Microsystems 	int		ret;
144548847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
144648847494SEnrico Perla - Sun Microsystems 
144748847494SEnrico Perla - Sun Microsystems 	p = strrchr(path, '/');
144848847494SEnrico Perla - Sun Microsystems 	if (p != NULL) {
144948847494SEnrico Perla - Sun Microsystems 		*p = '\0';
145048847494SEnrico Perla - Sun Microsystems 		if (stat(path, &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
145148847494SEnrico Perla - Sun Microsystems 			/* best effort attempt, mkdirp will catch the error */
145248847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
145348847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
145448847494SEnrico Perla - Sun Microsystems 				bam_print(NEED_DIRPATH, path);
145548847494SEnrico Perla - Sun Microsystems 			ret = mkdirp(path, DIR_PERMS);
145648847494SEnrico Perla - Sun Microsystems 			if (ret == -1) {
145748847494SEnrico Perla - Sun Microsystems 				bam_error(MKDIR_FAILED, path, strerror(errno));
145848847494SEnrico Perla - Sun Microsystems 				*p = '/';
145948847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
146048847494SEnrico Perla - Sun Microsystems 			}
146148847494SEnrico Perla - Sun Microsystems 		}
146248847494SEnrico Perla - Sun Microsystems 		*p = '/';
146348847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
146448847494SEnrico Perla - Sun Microsystems 	}
146548847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
146648847494SEnrico Perla - Sun Microsystems }
146748847494SEnrico Perla - Sun Microsystems 
146848847494SEnrico Perla - Sun Microsystems typedef union {
146948847494SEnrico Perla - Sun Microsystems 	gzFile	gzfile;
147048847494SEnrico Perla - Sun Microsystems 	int	fdfile;
147148847494SEnrico Perla - Sun Microsystems } outfile;
147248847494SEnrico Perla - Sun Microsystems 
147348847494SEnrico Perla - Sun Microsystems typedef struct {
147448847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
147548847494SEnrico Perla - Sun Microsystems 	outfile		out;
147648847494SEnrico Perla - Sun Microsystems } cachefile;
147748847494SEnrico Perla - Sun Microsystems 
147848847494SEnrico Perla - Sun Microsystems static int
147948847494SEnrico Perla - Sun Microsystems setup_file(char *base, const char *path, cachefile *cf)
148048847494SEnrico Perla - Sun Microsystems {
148148847494SEnrico Perla - Sun Microsystems 	int	ret;
148248847494SEnrico Perla - Sun Microsystems 	char	*strip;
148348847494SEnrico Perla - Sun Microsystems 
148448847494SEnrico Perla - Sun Microsystems 	/* init gzfile or fdfile in case we fail before opening */
148548847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT)
148648847494SEnrico Perla - Sun Microsystems 		cf->out.gzfile = NULL;
148748847494SEnrico Perla - Sun Microsystems 	else
148848847494SEnrico Perla - Sun Microsystems 		cf->out.fdfile = -1;
148948847494SEnrico Perla - Sun Microsystems 
149048847494SEnrico Perla - Sun Microsystems 	/* strip the trailing altroot path */
149148847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
149248847494SEnrico Perla - Sun Microsystems 
149348847494SEnrico Perla - Sun Microsystems 	ret = snprintf(cf->path, sizeof (cf->path), "%s/%s", base, strip);
149448847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (cf->path)) {
149548847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
149648847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
149748847494SEnrico Perla - Sun Microsystems 	}
149848847494SEnrico Perla - Sun Microsystems 
149948847494SEnrico Perla - Sun Microsystems 	/* Check if path is present in the archive cache directory */
150048847494SEnrico Perla - Sun Microsystems 	if (setup_path(cf->path) == BAM_ERROR)
150148847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
150248847494SEnrico Perla - Sun Microsystems 
150348847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
150448847494SEnrico Perla - Sun Microsystems 		if ((cf->out.gzfile = gzopen(cf->path, "wb")) == NULL) {
1505cedc7e57SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, cf->path, strerror(errno));
150648847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
150748847494SEnrico Perla - Sun Microsystems 		}
150848847494SEnrico Perla - Sun Microsystems 		(void) gzsetparams(cf->out.gzfile, Z_BEST_SPEED,
150948847494SEnrico Perla - Sun Microsystems 		    Z_DEFAULT_STRATEGY);
151048847494SEnrico Perla - Sun Microsystems 	} else {
151148847494SEnrico Perla - Sun Microsystems 		if ((cf->out.fdfile = open(cf->path, O_WRONLY | O_CREAT, 0644))
151248847494SEnrico Perla - Sun Microsystems 		    == -1) {
151348847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, cf->path, strerror(errno));
151448847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
151548847494SEnrico Perla - Sun Microsystems 		}
151648847494SEnrico Perla - Sun Microsystems 	}
151748847494SEnrico Perla - Sun Microsystems 
151848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
151948847494SEnrico Perla - Sun Microsystems }
152048847494SEnrico Perla - Sun Microsystems 
152148847494SEnrico Perla - Sun Microsystems static int
152248847494SEnrico Perla - Sun Microsystems cache_write(cachefile cf, char *buf, int size)
152348847494SEnrico Perla - Sun Microsystems {
152448847494SEnrico Perla - Sun Microsystems 	int	err;
152548847494SEnrico Perla - Sun Microsystems 
152648847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
152748847494SEnrico Perla - Sun Microsystems 		if (gzwrite(cf.out.gzfile, buf, size) < 1) {
152848847494SEnrico Perla - Sun Microsystems 			bam_error(GZ_WRITE_FAIL, gzerror(cf.out.gzfile, &err));
1529cedc7e57SEnrico Perla - Sun Microsystems 			if (err == Z_ERRNO && bam_verbose) {
153048847494SEnrico Perla - Sun Microsystems 				bam_error(WRITE_FAIL, cf.path, strerror(errno));
1531cedc7e57SEnrico Perla - Sun Microsystems 			}
153248847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
153348847494SEnrico Perla - Sun Microsystems 		}
153448847494SEnrico Perla - Sun Microsystems 	} else {
153548847494SEnrico Perla - Sun Microsystems 		if (write(cf.out.fdfile, buf, size) < 1) {
153648847494SEnrico Perla - Sun Microsystems 			bam_error(WRITE_FAIL, cf.path, strerror(errno));
153748847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
153848847494SEnrico Perla - Sun Microsystems 		}
153948847494SEnrico Perla - Sun Microsystems 	}
154048847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
154148847494SEnrico Perla - Sun Microsystems }
154248847494SEnrico Perla - Sun Microsystems 
154348847494SEnrico Perla - Sun Microsystems static int
154448847494SEnrico Perla - Sun Microsystems cache_close(cachefile cf)
154548847494SEnrico Perla - Sun Microsystems {
154648847494SEnrico Perla - Sun Microsystems 	int	ret;
154748847494SEnrico Perla - Sun Microsystems 
154848847494SEnrico Perla - Sun Microsystems 	if (bam_direct == BAM_DIRECT_DBOOT) {
154948847494SEnrico Perla - Sun Microsystems 		if (cf.out.gzfile) {
155048847494SEnrico Perla - Sun Microsystems 			ret = gzclose(cf.out.gzfile);
1551cedc7e57SEnrico Perla - Sun Microsystems 			if (ret != Z_OK) {
155248847494SEnrico Perla - Sun Microsystems 				bam_error(CLOSE_FAIL, cf.path, strerror(errno));
155348847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
155448847494SEnrico Perla - Sun Microsystems 			}
1555cedc7e57SEnrico Perla - Sun Microsystems 		}
155648847494SEnrico Perla - Sun Microsystems 	} else {
155748847494SEnrico Perla - Sun Microsystems 		if (cf.out.fdfile != -1) {
155848847494SEnrico Perla - Sun Microsystems 			ret = close(cf.out.fdfile);
155948847494SEnrico Perla - Sun Microsystems 			if (ret != 0) {
156048847494SEnrico Perla - Sun Microsystems 				bam_error(CLOSE_FAIL, cf.path, strerror(errno));
156148847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
156248847494SEnrico Perla - Sun Microsystems 			}
156348847494SEnrico Perla - Sun Microsystems 		}
156448847494SEnrico Perla - Sun Microsystems 	}
156548847494SEnrico Perla - Sun Microsystems 
156648847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
156748847494SEnrico Perla - Sun Microsystems }
156848847494SEnrico Perla - Sun Microsystems 
156948847494SEnrico Perla - Sun Microsystems static int
157048847494SEnrico Perla - Sun Microsystems dircache_updatefile(const char *path, int what)
157148847494SEnrico Perla - Sun Microsystems {
157248847494SEnrico Perla - Sun Microsystems 	int 		ret, exitcode;
157348847494SEnrico Perla - Sun Microsystems 	char 		buf[4096 * 4];
157448847494SEnrico Perla - Sun Microsystems 	FILE 		*infile;
157548847494SEnrico Perla - Sun Microsystems 	cachefile 	outfile, outupdt;
157648847494SEnrico Perla - Sun Microsystems 
1577cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
1578cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
1579cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
1580cedc7e57SEnrico Perla - Sun Microsystems 	}
1581cedc7e57SEnrico Perla - Sun Microsystems 
1582cedc7e57SEnrico Perla - Sun Microsystems 	if (!has_cachedir(what))
158348847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
158448847494SEnrico Perla - Sun Microsystems 
158548847494SEnrico Perla - Sun Microsystems 	if ((infile = fopen(path, "rb")) == NULL) {
158648847494SEnrico Perla - Sun Microsystems 		bam_error(OPEN_FAIL, path, strerror(errno));
158748847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
158848847494SEnrico Perla - Sun Microsystems 	}
158948847494SEnrico Perla - Sun Microsystems 
159048847494SEnrico Perla - Sun Microsystems 	ret = setup_file(get_cachedir(what), path, &outfile);
159148847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
159248847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
159348847494SEnrico Perla - Sun Microsystems 		goto out;
159448847494SEnrico Perla - Sun Microsystems 	}
159548847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI)) {
159648847494SEnrico Perla - Sun Microsystems 		ret = setup_file(get_updatedir(what), path, &outupdt);
159748847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
159848847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
159948847494SEnrico Perla - Sun Microsystems 	}
160048847494SEnrico Perla - Sun Microsystems 
160148847494SEnrico Perla - Sun Microsystems 	while ((ret = fread(buf, 1, sizeof (buf), infile)) > 0) {
160248847494SEnrico Perla - Sun Microsystems 		if (cache_write(outfile, buf, ret) == BAM_ERROR) {
160348847494SEnrico Perla - Sun Microsystems 			exitcode = BAM_ERROR;
160448847494SEnrico Perla - Sun Microsystems 			goto out;
160548847494SEnrico Perla - Sun Microsystems 		}
160648847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
160748847494SEnrico Perla - Sun Microsystems 			if (cache_write(outupdt, buf, ret) == BAM_ERROR)
160848847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
160948847494SEnrico Perla - Sun Microsystems 	}
161048847494SEnrico Perla - Sun Microsystems 
161148847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
161248847494SEnrico Perla - Sun Microsystems 	get_count(what)++;
1613cedc7e57SEnrico Perla - Sun Microsystems 	if (get_count(what) > COUNT_MAX)
1614cedc7e57SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
161548847494SEnrico Perla - Sun Microsystems 	exitcode = BAM_SUCCESS;
161648847494SEnrico Perla - Sun Microsystems out:
161748847494SEnrico Perla - Sun Microsystems 	(void) fclose(infile);
161848847494SEnrico Perla - Sun Microsystems 	if (cache_close(outfile) == BAM_ERROR)
161948847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
162048847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(what, NO_MULTI) &&
162148847494SEnrico Perla - Sun Microsystems 	    cache_close(outupdt) == BAM_ERROR)
162248847494SEnrico Perla - Sun Microsystems 		exitcode = BAM_ERROR;
162348847494SEnrico Perla - Sun Microsystems 	if (exitcode == BAM_ERROR)
162448847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
162548847494SEnrico Perla - Sun Microsystems 	return (exitcode);
162648847494SEnrico Perla - Sun Microsystems }
162748847494SEnrico Perla - Sun Microsystems 
162848847494SEnrico Perla - Sun Microsystems static int
162948847494SEnrico Perla - Sun Microsystems dircache_updatedir(const char *path, int what, int updt)
163048847494SEnrico Perla - Sun Microsystems {
163148847494SEnrico Perla - Sun Microsystems 	int		ret;
163248847494SEnrico Perla - Sun Microsystems 	char		dpath[PATH_MAX];
163348847494SEnrico Perla - Sun Microsystems 	char		*strip;
163448847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
163548847494SEnrico Perla - Sun Microsystems 
163648847494SEnrico Perla - Sun Microsystems 	strip = (char *)path + strlen(rootbuf);
163748847494SEnrico Perla - Sun Microsystems 
163848847494SEnrico Perla - Sun Microsystems 	ret = snprintf(dpath, sizeof (dpath), "%s/%s", updt ?
163948847494SEnrico Perla - Sun Microsystems 	    get_updatedir(what) : get_cachedir(what), strip);
164048847494SEnrico Perla - Sun Microsystems 
164148847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (dpath)) {
164248847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
164348847494SEnrico Perla - Sun Microsystems 		set_flag(UPDATE_ERROR);
164448847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
164548847494SEnrico Perla - Sun Microsystems 	}
164648847494SEnrico Perla - Sun Microsystems 
164748847494SEnrico Perla - Sun Microsystems 	if (stat(dpath, &sb) == 0 && S_ISDIR(sb.st_mode))
164848847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
164948847494SEnrico Perla - Sun Microsystems 
165048847494SEnrico Perla - Sun Microsystems 	if (updt) {
165148847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI))
165248847494SEnrico Perla - Sun Microsystems 			if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1)
165348847494SEnrico Perla - Sun Microsystems 				set_dir_flag(what, NO_MULTI);
165448847494SEnrico Perla - Sun Microsystems 	} else {
165548847494SEnrico Perla - Sun Microsystems 		if (!bam_nowrite() && mkdirp(dpath, DIR_PERMS) == -1) {
165648847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
165748847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
165848847494SEnrico Perla - Sun Microsystems 		}
165948847494SEnrico Perla - Sun Microsystems 	}
166048847494SEnrico Perla - Sun Microsystems 
166148847494SEnrico Perla - Sun Microsystems 	set_dir_flag(what, NEED_UPDATE);
166248847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
166348847494SEnrico Perla - Sun Microsystems }
166448847494SEnrico Perla - Sun Microsystems 
166548847494SEnrico Perla - Sun Microsystems #define	DO_CACHE_DIR	0
166648847494SEnrico Perla - Sun Microsystems #define	DO_UPDATE_DIR	1
166748847494SEnrico Perla - Sun Microsystems 
166848847494SEnrico Perla - Sun Microsystems #if defined(_LP64) || defined(_LONGLONG_TYPE)
166948847494SEnrico Perla - Sun Microsystems typedef		Elf64_Ehdr	_elfhdr;
167048847494SEnrico Perla - Sun Microsystems #else
167148847494SEnrico Perla - Sun Microsystems typedef		Elf32_Ehdr	_elfhdr;
167248847494SEnrico Perla - Sun Microsystems #endif
167348847494SEnrico Perla - Sun Microsystems 
167448847494SEnrico Perla - Sun Microsystems /*
167548847494SEnrico Perla - Sun Microsystems  * This routine updates the contents of the cache directory
167648847494SEnrico Perla - Sun Microsystems  */
167748847494SEnrico Perla - Sun Microsystems static int
167848847494SEnrico Perla - Sun Microsystems update_dircache(const char *path, int flags)
167948847494SEnrico Perla - Sun Microsystems {
168048847494SEnrico Perla - Sun Microsystems 	int rc = BAM_SUCCESS;
168148847494SEnrico Perla - Sun Microsystems 
168248847494SEnrico Perla - Sun Microsystems 	switch (flags) {
168348847494SEnrico Perla - Sun Microsystems 	case FTW_F:
168448847494SEnrico Perla - Sun Microsystems 		{
168548847494SEnrico Perla - Sun Microsystems 		int	fd;
168648847494SEnrico Perla - Sun Microsystems 		_elfhdr	elf;
168748847494SEnrico Perla - Sun Microsystems 
168848847494SEnrico Perla - Sun Microsystems 		if ((fd = open(path, O_RDONLY)) < 0) {
168948847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, path, strerror(errno));
169048847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
169148847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
169248847494SEnrico Perla - Sun Microsystems 			break;
169348847494SEnrico Perla - Sun Microsystems 		}
169448847494SEnrico Perla - Sun Microsystems 
169548847494SEnrico Perla - Sun Microsystems 		/*
169648847494SEnrico Perla - Sun Microsystems 		 * libelf and gelf would be a cleaner and easier way to handle
169748847494SEnrico Perla - Sun Microsystems 		 * this, but libelf fails compilation if _ILP32 is defined &&
169848847494SEnrico Perla - Sun Microsystems 		 * _FILE_OFFSET_BITS is != 32 ...
169948847494SEnrico Perla - Sun Microsystems 		 */
170048847494SEnrico Perla - Sun Microsystems 		if (read(fd, (void *)&elf, sizeof (_elfhdr)) < 0) {
170148847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, path, strerror(errno));
170248847494SEnrico Perla - Sun Microsystems 			set_flag(UPDATE_ERROR);
170348847494SEnrico Perla - Sun Microsystems 			(void) close(fd);
170448847494SEnrico Perla - Sun Microsystems 			rc = BAM_ERROR;
170548847494SEnrico Perla - Sun Microsystems 			break;
170648847494SEnrico Perla - Sun Microsystems 		}
170748847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
170848847494SEnrico Perla - Sun Microsystems 
170948847494SEnrico Perla - Sun Microsystems 		/*
171048847494SEnrico Perla - Sun Microsystems 		 * If the file is not an executable and is not inside an amd64
171148847494SEnrico Perla - Sun Microsystems 		 * directory, we copy it in both the cache directories,
171248847494SEnrico Perla - Sun Microsystems 		 * otherwise, we only copy it inside the 64-bit one.
171348847494SEnrico Perla - Sun Microsystems 		 */
171448847494SEnrico Perla - Sun Microsystems 		if (memcmp(elf.e_ident, ELFMAG, 4) != 0) {
171548847494SEnrico Perla - Sun Microsystems 			if (strstr(path, "/amd64")) {
171648847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
171748847494SEnrico Perla - Sun Microsystems 			} else {
171848847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
1719cedc7e57SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
1720cedc7e57SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
172148847494SEnrico Perla - Sun Microsystems 			}
172248847494SEnrico Perla - Sun Microsystems 		} else {
172348847494SEnrico Perla - Sun Microsystems 			/*
172448847494SEnrico Perla - Sun Microsystems 			 * Based on the ELF class we copy the file in the 32-bit
172548847494SEnrico Perla - Sun Microsystems 			 * or the 64-bit cache directory.
172648847494SEnrico Perla - Sun Microsystems 			 */
1727cedc7e57SEnrico Perla - Sun Microsystems 			if (elf.e_ident[EI_CLASS] == ELFCLASS32) {
172848847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE32);
1729cedc7e57SEnrico Perla - Sun Microsystems 			} else if (elf.e_ident[EI_CLASS] == ELFCLASS64) {
173048847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatefile(path, FILE64);
1731cedc7e57SEnrico Perla - Sun Microsystems 			} else {
173248847494SEnrico Perla - Sun Microsystems 				bam_print(NO3264ELF, path);
173348847494SEnrico Perla - Sun Microsystems 				/* paranoid */
173448847494SEnrico Perla - Sun Microsystems 				rc  = dircache_updatefile(path, FILE32);
173548847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
173648847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatefile(path, FILE64);
173748847494SEnrico Perla - Sun Microsystems 			}
173848847494SEnrico Perla - Sun Microsystems 		}
173948847494SEnrico Perla - Sun Microsystems 		break;
174048847494SEnrico Perla - Sun Microsystems 		}
174148847494SEnrico Perla - Sun Microsystems 	case FTW_D:
174248847494SEnrico Perla - Sun Microsystems 		if (strstr(path, "/amd64") == NULL) {
174348847494SEnrico Perla - Sun Microsystems 			rc = dircache_updatedir(path, FILE32, DO_UPDATE_DIR);
174448847494SEnrico Perla - Sun Microsystems 			if (rc == BAM_SUCCESS)
174548847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE32,
174648847494SEnrico Perla - Sun Microsystems 				    DO_CACHE_DIR);
174748847494SEnrico Perla - Sun Microsystems 		} else {
174848847494SEnrico Perla - Sun Microsystems 			if (has_cachedir(FILE64)) {
174948847494SEnrico Perla - Sun Microsystems 				rc = dircache_updatedir(path, FILE64,
175048847494SEnrico Perla - Sun Microsystems 				    DO_UPDATE_DIR);
175148847494SEnrico Perla - Sun Microsystems 				if (rc == BAM_SUCCESS)
175248847494SEnrico Perla - Sun Microsystems 					rc = dircache_updatedir(path, FILE64,
175348847494SEnrico Perla - Sun Microsystems 					    DO_CACHE_DIR);
175448847494SEnrico Perla - Sun Microsystems 			}
175548847494SEnrico Perla - Sun Microsystems 		}
175648847494SEnrico Perla - Sun Microsystems 		break;
175748847494SEnrico Perla - Sun Microsystems 	default:
175848847494SEnrico Perla - Sun Microsystems 		rc = BAM_ERROR;
175948847494SEnrico Perla - Sun Microsystems 		break;
176048847494SEnrico Perla - Sun Microsystems 	}
176148847494SEnrico Perla - Sun Microsystems 
176248847494SEnrico Perla - Sun Microsystems 	return (rc);
176348847494SEnrico Perla - Sun Microsystems }
176448847494SEnrico Perla - Sun Microsystems 
17657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17667c478bd9Sstevel@tonic-gate static int
17677c478bd9Sstevel@tonic-gate cmpstat(
17687c478bd9Sstevel@tonic-gate 	const char *file,
176948847494SEnrico Perla - Sun Microsystems 	const struct stat *st,
17707c478bd9Sstevel@tonic-gate 	int flags,
17717c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
17727c478bd9Sstevel@tonic-gate {
17737c478bd9Sstevel@tonic-gate 	uint_t 		sz;
17747c478bd9Sstevel@tonic-gate 	uint64_t 	*value;
17757c478bd9Sstevel@tonic-gate 	uint64_t 	filestat[2];
177648847494SEnrico Perla - Sun Microsystems 	int 		error, ret;
17777c478bd9Sstevel@tonic-gate 
177858091fd8Ssetje 	struct safefile *safefilep;
177958091fd8Ssetje 	FILE 		*fp;
178048847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
178158091fd8Ssetje 
17827c478bd9Sstevel@tonic-gate 	/*
178348847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create/update links too.
17847c478bd9Sstevel@tonic-gate 	 */
178548847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_F && flags != FTW_D && (flags == FTW_SL &&
178648847494SEnrico Perla - Sun Microsystems 	    !is_flag_on(IS_SPARC_TARGET)))
178748847494SEnrico Perla - Sun Microsystems 		return (0);
178848847494SEnrico Perla - Sun Microsystems 
178948847494SEnrico Perla - Sun Microsystems 	/*
179048847494SEnrico Perla - Sun Microsystems 	 * Ignore broken links
179148847494SEnrico Perla - Sun Microsystems 	 */
179248847494SEnrico Perla - Sun Microsystems 	if (flags == FTW_SL && stat(file, &sb) < 0)
17937c478bd9Sstevel@tonic-gate 		return (0);
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	/*
17967c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
17977c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
17987c478bd9Sstevel@tonic-gate 	 */
17997c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
180048847494SEnrico Perla - Sun Microsystems 		filestat[0] = st->st_size;
180148847494SEnrico Perla - Sun Microsystems 		filestat[1] = st->st_mtime;
18027c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
18037c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
18047c478bd9Sstevel@tonic-gate 		if (error)
18057c478bd9Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
18067c478bd9Sstevel@tonic-gate 	}
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	/*
1809d876c67dSjg 	 * If we are invoked as part of system/filesystem/boot-archive, then
181058091fd8Ssetje 	 * there are a number of things we should not worry about
18117c478bd9Sstevel@tonic-gate 	 */
181258091fd8Ssetje 	if (bam_smf_check) {
181358091fd8Ssetje 		/* ignore amd64 modules unless we are booted amd64. */
181458091fd8Ssetje 		if (!is_amd64() && strstr(file, "/amd64/") != 0)
18157c478bd9Sstevel@tonic-gate 			return (0);
18167c478bd9Sstevel@tonic-gate 
181758091fd8Ssetje 		/* read in list of safe files */
181858091fd8Ssetje 		if (safefiles == NULL)
181958091fd8Ssetje 			if (fp = fopen("/boot/solaris/filelist.safe", "r")) {
182058091fd8Ssetje 				safefiles = s_calloc(1,
182158091fd8Ssetje 				    sizeof (struct safefile));
182258091fd8Ssetje 				safefilep = safefiles;
182358091fd8Ssetje 				safefilep->name = s_calloc(1, MAXPATHLEN +
182458091fd8Ssetje 				    MAXNAMELEN);
182558091fd8Ssetje 				safefilep->next = NULL;
182658091fd8Ssetje 				while (s_fgets(safefilep->name, MAXPATHLEN +
182758091fd8Ssetje 				    MAXNAMELEN, fp) != NULL) {
182858091fd8Ssetje 					safefilep->next = s_calloc(1,
182958091fd8Ssetje 					    sizeof (struct safefile));
183058091fd8Ssetje 					safefilep = safefilep->next;
183158091fd8Ssetje 					safefilep->name = s_calloc(1,
183258091fd8Ssetje 					    MAXPATHLEN + MAXNAMELEN);
183358091fd8Ssetje 					safefilep->next = NULL;
183458091fd8Ssetje 				}
183558091fd8Ssetje 				(void) fclose(fp);
183658091fd8Ssetje 			}
183758091fd8Ssetje 	}
183858091fd8Ssetje 
18397c478bd9Sstevel@tonic-gate 	/*
184048847494SEnrico Perla - Sun Microsystems 	 * On SPARC we create a -path-list file for mkisofs
184148847494SEnrico Perla - Sun Microsystems 	 */
184248847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) && !bam_nowrite()) {
184348847494SEnrico Perla - Sun Microsystems 		if (flags != FTW_D) {
184448847494SEnrico Perla - Sun Microsystems 			char	*strip;
184548847494SEnrico Perla - Sun Microsystems 
184648847494SEnrico Perla - Sun Microsystems 			strip = (char *)file + strlen(rootbuf);
184748847494SEnrico Perla - Sun Microsystems 			(void) fprintf(walk_arg.sparcfile, "/%s=%s\n", strip,
184848847494SEnrico Perla - Sun Microsystems 			    file);
184948847494SEnrico Perla - Sun Microsystems 		}
185048847494SEnrico Perla - Sun Microsystems 	}
185148847494SEnrico Perla - Sun Microsystems 
185248847494SEnrico Perla - Sun Microsystems 	/*
185348847494SEnrico Perla - Sun Microsystems 	 * We are transitioning from the old model to the dircache or the cache
185448847494SEnrico Perla - Sun Microsystems 	 * directory was removed: create the entry without further checkings.
185548847494SEnrico Perla - Sun Microsystems 	 */
185648847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR)) {
185748847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
185848847494SEnrico Perla - Sun Microsystems 			bam_print(PARSEABLE_NEW_FILE, file);
185948847494SEnrico Perla - Sun Microsystems 
186048847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
186148847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
186248847494SEnrico Perla - Sun Microsystems 			return (0);
186348847494SEnrico Perla - Sun Microsystems 		}
186448847494SEnrico Perla - Sun Microsystems 
186548847494SEnrico Perla - Sun Microsystems 		ret = update_dircache(file, flags);
186648847494SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR) {
186748847494SEnrico Perla - Sun Microsystems 			bam_error(UPDT_CACHE_FAIL, file);
186848847494SEnrico Perla - Sun Microsystems 			return (-1);
186948847494SEnrico Perla - Sun Microsystems 		}
187048847494SEnrico Perla - Sun Microsystems 
187148847494SEnrico Perla - Sun Microsystems 		return (0);
187248847494SEnrico Perla - Sun Microsystems 	}
187348847494SEnrico Perla - Sun Microsystems 
187448847494SEnrico Perla - Sun Microsystems 	/*
18757c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
18767c478bd9Sstevel@tonic-gate 	 */
18777c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
18787c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
18797c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
18807c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
18817c478bd9Sstevel@tonic-gate 			return (0);
188248847494SEnrico Perla - Sun Microsystems 
188348847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
188448847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
188548847494SEnrico Perla - Sun Microsystems 		} else {
188648847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
188748847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
188848847494SEnrico Perla - Sun Microsystems 				bam_error(UPDT_CACHE_FAIL, file);
188948847494SEnrico Perla - Sun Microsystems 				return (-1);
189048847494SEnrico Perla - Sun Microsystems 			}
189148847494SEnrico Perla - Sun Microsystems 		}
189248847494SEnrico Perla - Sun Microsystems 
18937c478bd9Sstevel@tonic-gate 		if (bam_verbose)
18947c478bd9Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
18957c478bd9Sstevel@tonic-gate 		return (0);
18967c478bd9Sstevel@tonic-gate 	}
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	/*
189948847494SEnrico Perla - Sun Microsystems 	 * If we got there, the file is already listed as to be included in the
190048847494SEnrico Perla - Sun Microsystems 	 * iso image. We just need to know if we are going to rebuild it or not
190148847494SEnrico Perla - Sun Microsystems 	 */
190248847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET) &&
1903cedc7e57SEnrico Perla - Sun Microsystems 	    is_dir_flag_on(FILE64, NEED_UPDATE) && !bam_nowrite())
190448847494SEnrico Perla - Sun Microsystems 		return (0);
190548847494SEnrico Perla - Sun Microsystems 	/*
19067c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
19077c478bd9Sstevel@tonic-gate 	 */
19087c478bd9Sstevel@tonic-gate 	assert(sz == 2);
19097c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
19107c478bd9Sstevel@tonic-gate 
191148847494SEnrico Perla - Sun Microsystems 	if (flags != FTW_D && (filestat[0] != st->st_size ||
191248847494SEnrico Perla - Sun Microsystems 	    filestat[1] != st->st_mtime)) {
19137c609327Ssetje 		if (bam_smf_check) {
19147c609327Ssetje 			safefilep = safefiles;
19157c609327Ssetje 			while (safefilep != NULL) {
19167c609327Ssetje 				if (strcmp(file + bam_rootlen,
19177c609327Ssetje 				    safefilep->name) == 0) {
19187c609327Ssetje 					(void) creat(NEED_UPDATE_FILE, 0644);
19197c609327Ssetje 					return (0);
19207c609327Ssetje 				}
19217c609327Ssetje 				safefilep = safefilep->next;
19227c609327Ssetje 			}
19237c609327Ssetje 		}
192448847494SEnrico Perla - Sun Microsystems 
192548847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(IS_SPARC_TARGET)) {
192648847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
192748847494SEnrico Perla - Sun Microsystems 		} else {
192848847494SEnrico Perla - Sun Microsystems 			ret = update_dircache(file, flags);
192948847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR) {
193048847494SEnrico Perla - Sun Microsystems 				bam_error(UPDT_CACHE_FAIL, file);
193148847494SEnrico Perla - Sun Microsystems 				return (-1);
193248847494SEnrico Perla - Sun Microsystems 			}
193348847494SEnrico Perla - Sun Microsystems 		}
193448847494SEnrico Perla - Sun Microsystems 
19357c478bd9Sstevel@tonic-gate 		if (bam_verbose)
19367c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
19377c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
19387c478bd9Sstevel@tonic-gate 			else
19397c478bd9Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
19407c478bd9Sstevel@tonic-gate 	}
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 	return (0);
19437c478bd9Sstevel@tonic-gate }
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate /*
194648847494SEnrico Perla - Sun Microsystems  * Remove a directory path recursively
194748847494SEnrico Perla - Sun Microsystems  */
194848847494SEnrico Perla - Sun Microsystems static int
194948847494SEnrico Perla - Sun Microsystems rmdir_r(char *path)
195048847494SEnrico Perla - Sun Microsystems {
195148847494SEnrico Perla - Sun Microsystems 	struct dirent 	*d = NULL;
195248847494SEnrico Perla - Sun Microsystems 	DIR 		*dir = NULL;
195348847494SEnrico Perla - Sun Microsystems 	char 		tpath[PATH_MAX];
195448847494SEnrico Perla - Sun Microsystems 	struct stat 	sb;
195548847494SEnrico Perla - Sun Microsystems 
195648847494SEnrico Perla - Sun Microsystems 	if ((dir = opendir(path)) == NULL)
195748847494SEnrico Perla - Sun Microsystems 		return (-1);
195848847494SEnrico Perla - Sun Microsystems 
195948847494SEnrico Perla - Sun Microsystems 	while (d = readdir(dir)) {
196048847494SEnrico Perla - Sun Microsystems 		if ((strcmp(d->d_name, ".") != 0) &&
196148847494SEnrico Perla - Sun Microsystems 		    (strcmp(d->d_name, "..") != 0)) {
196248847494SEnrico Perla - Sun Microsystems 			(void) snprintf(tpath, sizeof (tpath), "%s/%s",
196348847494SEnrico Perla - Sun Microsystems 			    path, d->d_name);
196448847494SEnrico Perla - Sun Microsystems 			if (stat(tpath, &sb) == 0) {
196548847494SEnrico Perla - Sun Microsystems 				if (sb.st_mode & S_IFDIR)
196648847494SEnrico Perla - Sun Microsystems 					(void) rmdir_r(tpath);
196748847494SEnrico Perla - Sun Microsystems 				else
196848847494SEnrico Perla - Sun Microsystems 					(void) remove(tpath);
196948847494SEnrico Perla - Sun Microsystems 			}
197048847494SEnrico Perla - Sun Microsystems 		}
197148847494SEnrico Perla - Sun Microsystems 	}
197248847494SEnrico Perla - Sun Microsystems 	return (remove(path));
197348847494SEnrico Perla - Sun Microsystems }
197448847494SEnrico Perla - Sun Microsystems 
197548847494SEnrico Perla - Sun Microsystems /*
197648847494SEnrico Perla - Sun Microsystems  * Check if cache directory exists and, if not, create it and update flags
197748847494SEnrico Perla - Sun Microsystems  * accordingly. If the path exists, but it's not a directory, a best effort
197848847494SEnrico Perla - Sun Microsystems  * attempt to remove and recreate it is made.
197948847494SEnrico Perla - Sun Microsystems  * If the user requested a 'purge', always recreate the directory from scratch.
198048847494SEnrico Perla - Sun Microsystems  */
198148847494SEnrico Perla - Sun Microsystems static int
198248847494SEnrico Perla - Sun Microsystems set_cache_dir(char *root, int what)
198348847494SEnrico Perla - Sun Microsystems {
198448847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
198548847494SEnrico Perla - Sun Microsystems 	int		ret = 0;
198648847494SEnrico Perla - Sun Microsystems 
198748847494SEnrico Perla - Sun Microsystems 	ret = snprintf(get_cachedir(what), sizeof (get_cachedir(what)),
198848847494SEnrico Perla - Sun Microsystems 	    "%s%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(), what == FILE64 ?
198948847494SEnrico Perla - Sun Microsystems 	    "/amd64" : "", CACHEDIR_SUFFIX);
199048847494SEnrico Perla - Sun Microsystems 
199148847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_cachedir(what))) {
199248847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
199348847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
199448847494SEnrico Perla - Sun Microsystems 	}
199548847494SEnrico Perla - Sun Microsystems 
1996*4a9df875SEnrico Perla - Sun Microsystems 	if (bam_purge || is_flag_on(INVALIDATE_CACHE))
199748847494SEnrico Perla - Sun Microsystems 		(void) rmdir_r(get_cachedir(what));
199848847494SEnrico Perla - Sun Microsystems 
199948847494SEnrico Perla - Sun Microsystems 	if (stat(get_cachedir(what), &sb) != 0 || !(S_ISDIR(sb.st_mode))) {
200048847494SEnrico Perla - Sun Microsystems 		/* best effort unlink attempt, mkdir will catch errors */
200148847494SEnrico Perla - Sun Microsystems 		(void) unlink(get_cachedir(what));
200248847494SEnrico Perla - Sun Microsystems 
200348847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
200448847494SEnrico Perla - Sun Microsystems 			bam_print(UPDATE_CDIR_MISS, get_cachedir(what));
200548847494SEnrico Perla - Sun Microsystems 		ret = mkdir(get_cachedir(what), DIR_PERMS);
200648847494SEnrico Perla - Sun Microsystems 		if (ret < 0) {
200748847494SEnrico Perla - Sun Microsystems 			bam_error(MKDIR_FAILED, get_cachedir(what),
200848847494SEnrico Perla - Sun Microsystems 			    strerror(errno));
200948847494SEnrico Perla - Sun Microsystems 			get_cachedir(what)[0] = '\0';
201048847494SEnrico Perla - Sun Microsystems 			return (ret);
201148847494SEnrico Perla - Sun Microsystems 		}
201248847494SEnrico Perla - Sun Microsystems 		set_flag(NEED_CACHE_DIR);
201348847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
201448847494SEnrico Perla - Sun Microsystems 	}
201548847494SEnrico Perla - Sun Microsystems 
201648847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
201748847494SEnrico Perla - Sun Microsystems }
201848847494SEnrico Perla - Sun Microsystems 
201948847494SEnrico Perla - Sun Microsystems static int
202048847494SEnrico Perla - Sun Microsystems set_update_dir(char *root, int what)
202148847494SEnrico Perla - Sun Microsystems {
202248847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
202348847494SEnrico Perla - Sun Microsystems 	int		ret;
202448847494SEnrico Perla - Sun Microsystems 
202548847494SEnrico Perla - Sun Microsystems 	if (is_dir_flag_on(what, NO_MULTI))
202648847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
202748847494SEnrico Perla - Sun Microsystems 
202848847494SEnrico Perla - Sun Microsystems 	if (!bam_extend) {
202948847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
203048847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
203148847494SEnrico Perla - Sun Microsystems 	}
203248847494SEnrico Perla - Sun Microsystems 
203348847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
203448847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
203548847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s/amd64%s", root,
203648847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
203748847494SEnrico Perla - Sun Microsystems 	else
203848847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_updatedir(what),
203948847494SEnrico Perla - Sun Microsystems 		    sizeof (get_updatedir(what)), "%s%s%s%s", root,
204048847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), UPDATEDIR_SUFFIX);
204148847494SEnrico Perla - Sun Microsystems 
204248847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (get_updatedir(what))) {
204348847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
204448847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
204548847494SEnrico Perla - Sun Microsystems 	}
204648847494SEnrico Perla - Sun Microsystems 
204748847494SEnrico Perla - Sun Microsystems 	if (stat(get_updatedir(what), &sb) == 0) {
204848847494SEnrico Perla - Sun Microsystems 		if (S_ISDIR(sb.st_mode))
204948847494SEnrico Perla - Sun Microsystems 			ret = rmdir_r(get_updatedir(what));
205048847494SEnrico Perla - Sun Microsystems 		else
205148847494SEnrico Perla - Sun Microsystems 			ret = unlink(get_updatedir(what));
205248847494SEnrico Perla - Sun Microsystems 
205348847494SEnrico Perla - Sun Microsystems 		if (ret != 0)
205448847494SEnrico Perla - Sun Microsystems 			set_dir_flag(what, NO_MULTI);
205548847494SEnrico Perla - Sun Microsystems 	}
205648847494SEnrico Perla - Sun Microsystems 
205748847494SEnrico Perla - Sun Microsystems 	if (mkdir(get_updatedir(what), DIR_PERMS) < 0)
205848847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
205948847494SEnrico Perla - Sun Microsystems 
206048847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
206148847494SEnrico Perla - Sun Microsystems }
206248847494SEnrico Perla - Sun Microsystems 
206348847494SEnrico Perla - Sun Microsystems static int
206448847494SEnrico Perla - Sun Microsystems is_valid_archive(char *root, int what)
206548847494SEnrico Perla - Sun Microsystems {
206648847494SEnrico Perla - Sun Microsystems 	char 		archive_path[PATH_MAX];
2067*4a9df875SEnrico Perla - Sun Microsystems 	char		timestamp_path[PATH_MAX];
2068*4a9df875SEnrico Perla - Sun Microsystems 	struct stat 	sb, timestamp;
206948847494SEnrico Perla - Sun Microsystems 	int 		ret;
207048847494SEnrico Perla - Sun Microsystems 
207148847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
207248847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path),
207348847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
207448847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
207548847494SEnrico Perla - Sun Microsystems 	else
207648847494SEnrico Perla - Sun Microsystems 		ret = snprintf(archive_path, sizeof (archive_path), "%s%s%s%s",
207748847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), ARCHIVE_SUFFIX);
207848847494SEnrico Perla - Sun Microsystems 
207948847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (archive_path)) {
208048847494SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
208148847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
208248847494SEnrico Perla - Sun Microsystems 	}
208348847494SEnrico Perla - Sun Microsystems 
208448847494SEnrico Perla - Sun Microsystems 	if (stat(archive_path, &sb) != 0) {
208548847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
208648847494SEnrico Perla - Sun Microsystems 			bam_print(UPDATE_ARCH_MISS, archive_path);
208748847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
208848847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
208948847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
209048847494SEnrico Perla - Sun Microsystems 	}
209148847494SEnrico Perla - Sun Microsystems 
2092*4a9df875SEnrico Perla - Sun Microsystems 	/*
2093*4a9df875SEnrico Perla - Sun Microsystems 	 * The timestamp file is used to prevent stale files in the archive
2094*4a9df875SEnrico Perla - Sun Microsystems 	 * cache.
2095*4a9df875SEnrico Perla - Sun Microsystems 	 * Stale files can happen if the system is booted back and forth across
2096*4a9df875SEnrico Perla - Sun Microsystems 	 * the transition from bootadm-before-the-cache to
2097*4a9df875SEnrico Perla - Sun Microsystems 	 * bootadm-after-the-cache, since older versions of bootadm don't know
2098*4a9df875SEnrico Perla - Sun Microsystems 	 * about the existence of the archive cache.
2099*4a9df875SEnrico Perla - Sun Microsystems 	 *
2100*4a9df875SEnrico Perla - Sun Microsystems 	 * Since only bootadm-after-the-cache versions know about about this
2101*4a9df875SEnrico Perla - Sun Microsystems 	 * file, we require that the boot archive be older than this file.
2102*4a9df875SEnrico Perla - Sun Microsystems 	 */
2103*4a9df875SEnrico Perla - Sun Microsystems 	ret = snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root,
2104*4a9df875SEnrico Perla - Sun Microsystems 	    FILE_STAT_TIMESTAMP);
2105*4a9df875SEnrico Perla - Sun Microsystems 
2106*4a9df875SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (timestamp_path)) {
2107*4a9df875SEnrico Perla - Sun Microsystems 		bam_error(PATH_TOO_LONG, rootbuf);
2108*4a9df875SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
2109*4a9df875SEnrico Perla - Sun Microsystems 	}
2110*4a9df875SEnrico Perla - Sun Microsystems 
2111*4a9df875SEnrico Perla - Sun Microsystems 	if (stat(timestamp_path, &timestamp) != 0 ||
2112*4a9df875SEnrico Perla - Sun Microsystems 	    sb.st_mtime > timestamp.st_mtime) {
2113*4a9df875SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
2114*4a9df875SEnrico Perla - Sun Microsystems 			bam_print(UPDATE_CACHE_OLD, timestamp);
2115*4a9df875SEnrico Perla - Sun Microsystems 		/*
2116*4a9df875SEnrico Perla - Sun Microsystems 		 * Don't generate a false positive for the boot-archive service
2117*4a9df875SEnrico Perla - Sun Microsystems 		 * but trigger an update of the archive cache in
2118*4a9df875SEnrico Perla - Sun Microsystems 		 * boot-archive-update.
2119*4a9df875SEnrico Perla - Sun Microsystems 		 */
2120*4a9df875SEnrico Perla - Sun Microsystems 		if (bam_smf_check) {
2121*4a9df875SEnrico Perla - Sun Microsystems 			(void) creat(NEED_UPDATE_FILE, 0644);
2122*4a9df875SEnrico Perla - Sun Microsystems 			return (BAM_SUCCESS);
2123*4a9df875SEnrico Perla - Sun Microsystems 		}
2124*4a9df875SEnrico Perla - Sun Microsystems 
2125*4a9df875SEnrico Perla - Sun Microsystems 		set_flag(INVALIDATE_CACHE);
2126*4a9df875SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NEED_UPDATE);
2127*4a9df875SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
2128*4a9df875SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
2129*4a9df875SEnrico Perla - Sun Microsystems 	}
2130*4a9df875SEnrico Perla - Sun Microsystems 
213148847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET))
213248847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
213348847494SEnrico Perla - Sun Microsystems 
213448847494SEnrico Perla - Sun Microsystems 	if (bam_extend && sb.st_size > BA_SIZE_MAX) {
213548847494SEnrico Perla - Sun Microsystems 		if (bam_verbose && !bam_check)
213648847494SEnrico Perla - Sun Microsystems 			bam_print(MULTI_SIZE, archive_path, BA_SIZE_MAX);
213748847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, NO_MULTI);
213848847494SEnrico Perla - Sun Microsystems 	}
213948847494SEnrico Perla - Sun Microsystems 
214048847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
214148847494SEnrico Perla - Sun Microsystems }
214248847494SEnrico Perla - Sun Microsystems 
214348847494SEnrico Perla - Sun Microsystems /*
214448847494SEnrico Perla - Sun Microsystems  * Check flags and presence of required files and directories.
21457c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
21467c478bd9Sstevel@tonic-gate  * trigger an update.
21477c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
21487c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
21497c478bd9Sstevel@tonic-gate  */
215048847494SEnrico Perla - Sun Microsystems static int
21517c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
21527c478bd9Sstevel@tonic-gate {
215348847494SEnrico Perla - Sun Microsystems 
21547c478bd9Sstevel@tonic-gate 	struct stat 	sb;
215548847494SEnrico Perla - Sun Microsystems 	int 		ret;
215648847494SEnrico Perla - Sun Microsystems 
215748847494SEnrico Perla - Sun Microsystems 	/*
215848847494SEnrico Perla - Sun Microsystems 	 * If archive is missing, create archive
215948847494SEnrico Perla - Sun Microsystems 	 */
216048847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
216148847494SEnrico Perla - Sun Microsystems 		ret = is_valid_archive(root, FILE64);
2162cedc7e57SEnrico Perla - Sun Microsystems 		if (ret == BAM_ERROR)
2163cedc7e57SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
216448847494SEnrico Perla - Sun Microsystems 	} else {
216548847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
216648847494SEnrico Perla - Sun Microsystems 		do {
216748847494SEnrico Perla - Sun Microsystems 			ret = is_valid_archive(root, what);
216848847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_ERROR)
216948847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
217048847494SEnrico Perla - Sun Microsystems 			what++;
217148847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
217248847494SEnrico Perla - Sun Microsystems 	}
217348847494SEnrico Perla - Sun Microsystems 
217448847494SEnrico Perla - Sun Microsystems 	if (bam_nowrite())
217548847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
217648847494SEnrico Perla - Sun Microsystems 
217748847494SEnrico Perla - Sun Microsystems 
217848847494SEnrico Perla - Sun Microsystems 	/*
217948847494SEnrico Perla - Sun Microsystems 	 * check if cache directories exist on x86.
218048847494SEnrico Perla - Sun Microsystems 	 * check (and always open) the cache file on SPARC.
218148847494SEnrico Perla - Sun Microsystems 	 */
218248847494SEnrico Perla - Sun Microsystems 	if (is_sparc()) {
218348847494SEnrico Perla - Sun Microsystems 		ret = snprintf(get_cachedir(FILE64),
218448847494SEnrico Perla - Sun Microsystems 		    sizeof (get_cachedir(FILE64)), "%s%s%s/%s", root,
218548847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_PREFIX, get_machine(), CACHEDIR_SUFFIX);
218648847494SEnrico Perla - Sun Microsystems 
218748847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (get_cachedir(FILE64))) {
218848847494SEnrico Perla - Sun Microsystems 			bam_error(PATH_TOO_LONG, rootbuf);
218948847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
219048847494SEnrico Perla - Sun Microsystems 		}
219148847494SEnrico Perla - Sun Microsystems 
219248847494SEnrico Perla - Sun Microsystems 		if (stat(get_cachedir(FILE64), &sb) != 0) {
219348847494SEnrico Perla - Sun Microsystems 			set_flag(NEED_CACHE_DIR);
219448847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE64, NEED_UPDATE);
219548847494SEnrico Perla - Sun Microsystems 		}
219648847494SEnrico Perla - Sun Microsystems 
219748847494SEnrico Perla - Sun Microsystems 		walk_arg.sparcfile = fopen(get_cachedir(FILE64), "w");
219848847494SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile == NULL) {
219948847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, get_cachedir(FILE64),
220048847494SEnrico Perla - Sun Microsystems 			    strerror(errno));
220148847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
220248847494SEnrico Perla - Sun Microsystems 		}
220348847494SEnrico Perla - Sun Microsystems 
220448847494SEnrico Perla - Sun Microsystems 		set_dir_present(FILE64);
220548847494SEnrico Perla - Sun Microsystems 	} else {
220648847494SEnrico Perla - Sun Microsystems 		int	what = FILE32;
220748847494SEnrico Perla - Sun Microsystems 
220848847494SEnrico Perla - Sun Microsystems 		do {
220948847494SEnrico Perla - Sun Microsystems 			if (set_cache_dir(root, what) != 0)
221048847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
221148847494SEnrico Perla - Sun Microsystems 
221248847494SEnrico Perla - Sun Microsystems 			set_dir_present(what);
221348847494SEnrico Perla - Sun Microsystems 
221448847494SEnrico Perla - Sun Microsystems 			if (set_update_dir(root, what) != 0)
221548847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
221648847494SEnrico Perla - Sun Microsystems 			what++;
221748847494SEnrico Perla - Sun Microsystems 		} while (bam_direct == BAM_DIRECT_DBOOT && what < CACHEDIR_NUM);
221848847494SEnrico Perla - Sun Microsystems 	}
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	/*
22217c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
22227c478bd9Sstevel@tonic-gate 	 */
22237c478bd9Sstevel@tonic-gate 	if (bam_force) {
222448847494SEnrico Perla - Sun Microsystems 		if (!is_sparc())
222548847494SEnrico Perla - Sun Microsystems 			set_dir_flag(FILE32, NEED_UPDATE);
222648847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE64, NEED_UPDATE);
222748847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
22287c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
222948847494SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
22307c478bd9Sstevel@tonic-gate 	}
22317c478bd9Sstevel@tonic-gate 
223248847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
22337c478bd9Sstevel@tonic-gate }
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate static error_t
22367c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
22377c478bd9Sstevel@tonic-gate {
22387c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
22397c478bd9Sstevel@tonic-gate 	FILE 		*fp;
22407c478bd9Sstevel@tonic-gate 	char 		buf[BAM_MAXLINE];
2241eb2bd662Svikram 	const char 	*fcn = "read_one_list()";
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
22467c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
2247eb2bd662Svikram 		BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno)));
22487c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
22497c478bd9Sstevel@tonic-gate 	}
22507c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
2251b610f78eSvikram 		/* skip blank lines */
2252b610f78eSvikram 		if (strspn(buf, " \t") == strlen(buf))
2253b610f78eSvikram 			continue;
22547c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
22557c478bd9Sstevel@tonic-gate 	}
22567c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
22577c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
22587c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
22597c478bd9Sstevel@tonic-gate 	}
22607c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
22617c478bd9Sstevel@tonic-gate }
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate static error_t
22647c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
22657c478bd9Sstevel@tonic-gate {
2266986fd29aSsetje 	char 		path[PATH_MAX];
2267986fd29aSsetje 	char 		cmd[PATH_MAX];
2268986fd29aSsetje 	struct stat 	sb;
2269986fd29aSsetje 	int 		n, rval;
2270eb2bd662Svikram 	const char 	*fcn = "read_list()";
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 	/*
2275986fd29aSsetje 	 * build and check path to extract_boot_filelist.ksh
2276986fd29aSsetje 	 */
2277986fd29aSsetje 	n = snprintf(path, sizeof (path), "%s%s", root, EXTRACT_BOOT_FILELIST);
2278986fd29aSsetje 	if (n >= sizeof (path)) {
2279986fd29aSsetje 		bam_error(NO_FLIST);
2280986fd29aSsetje 		return (BAM_ERROR);
2281986fd29aSsetje 	}
2282986fd29aSsetje 
2283cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
2284cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
2285cedc7e57SEnrico Perla - Sun Microsystems 
2286986fd29aSsetje 	/*
2287986fd29aSsetje 	 * If extract_boot_filelist is present, exec it, otherwise read
2288986fd29aSsetje 	 * the filelists directly, for compatibility with older images.
2289986fd29aSsetje 	 */
2290986fd29aSsetje 	if (stat(path, &sb) == 0) {
2291986fd29aSsetje 		/*
2292986fd29aSsetje 		 * build arguments to exec extract_boot_filelist.ksh
2293986fd29aSsetje 		 */
2294d876c67dSjg 		char *rootarg, *platarg;
2295d876c67dSjg 		int platarglen = 1, rootarglen = 1;
2296d876c67dSjg 		if (strlen(root) > 1)
2297d876c67dSjg 			rootarglen += strlen(root) + strlen("-R ");
2298d876c67dSjg 		if (bam_alt_platform)
2299d876c67dSjg 			platarglen += strlen(bam_platform) + strlen("-p ");
2300d876c67dSjg 		platarg = s_calloc(1, platarglen);
2301d876c67dSjg 		rootarg = s_calloc(1, rootarglen);
2302d876c67dSjg 		*platarg = 0;
2303d876c67dSjg 		*rootarg = 0;
2304d876c67dSjg 
2305986fd29aSsetje 		if (strlen(root) > 1) {
2306d876c67dSjg 			(void) snprintf(rootarg, rootarglen,
2307d876c67dSjg 			    "-R %s", root);
2308986fd29aSsetje 		}
2309d876c67dSjg 		if (bam_alt_platform) {
2310d876c67dSjg 			(void) snprintf(platarg, platarglen,
2311d876c67dSjg 			    "-p %s", bam_platform);
2312d876c67dSjg 		}
2313d876c67dSjg 		n = snprintf(cmd, sizeof (cmd), "%s %s %s /%s /%s",
2314d876c67dSjg 		    path, rootarg, platarg, BOOT_FILE_LIST, ETC_FILE_LIST);
2315d876c67dSjg 		free(platarg);
2316d876c67dSjg 		free(rootarg);
2317986fd29aSsetje 		if (n >= sizeof (cmd)) {
2318986fd29aSsetje 			bam_error(NO_FLIST);
2319986fd29aSsetje 			return (BAM_ERROR);
2320986fd29aSsetje 		}
2321986fd29aSsetje 		if (exec_cmd(cmd, flistp) != 0) {
2322eb2bd662Svikram 			BAM_DPRINTF((D_FLIST_FAIL, fcn, path, strerror(errno)));
2323986fd29aSsetje 			return (BAM_ERROR);
2324986fd29aSsetje 		}
2325986fd29aSsetje 	} else {
2326986fd29aSsetje 		/*
23277c478bd9Sstevel@tonic-gate 		 * Read current lists of files - only the first is mandatory
23287c478bd9Sstevel@tonic-gate 		 */
23297c478bd9Sstevel@tonic-gate 		rval = read_one_list(root, flistp, BOOT_FILE_LIST);
23307c478bd9Sstevel@tonic-gate 		if (rval != BAM_SUCCESS)
23317c478bd9Sstevel@tonic-gate 			return (rval);
23327c478bd9Sstevel@tonic-gate 		(void) read_one_list(root, flistp, ETC_FILE_LIST);
2333986fd29aSsetje 	}
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
23367c478bd9Sstevel@tonic-gate 		bam_error(NO_FLIST);
23377c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
23387c478bd9Sstevel@tonic-gate 	}
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
23417c478bd9Sstevel@tonic-gate }
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate static void
23447c478bd9Sstevel@tonic-gate getoldstat(char *root)
23457c478bd9Sstevel@tonic-gate {
23467c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
23477c478bd9Sstevel@tonic-gate 	int 		fd, error;
23487c478bd9Sstevel@tonic-gate 	struct stat 	sb;
23497c478bd9Sstevel@tonic-gate 	char 		*ostat;
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
23527c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
23537c478bd9Sstevel@tonic-gate 	if (fd == -1) {
23547c478bd9Sstevel@tonic-gate 		if (bam_verbose)
23557c478bd9Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
235648847494SEnrico Perla - Sun Microsystems 		goto out_err;
23577c478bd9Sstevel@tonic-gate 	}
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
23607c478bd9Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
236148847494SEnrico Perla - Sun Microsystems 		goto out_err;
23627c478bd9Sstevel@tonic-gate 	}
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
23677c478bd9Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
23687c478bd9Sstevel@tonic-gate 		free(ostat);
236948847494SEnrico Perla - Sun Microsystems 		goto out_err;
23707c478bd9Sstevel@tonic-gate 	}
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 	(void) close(fd);
237348847494SEnrico Perla - Sun Microsystems 	fd = -1;
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
23767c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 	free(ostat);
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 	if (error) {
23817c478bd9Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
23827c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
238348847494SEnrico Perla - Sun Microsystems 		goto out_err;
238448847494SEnrico Perla - Sun Microsystems 	} else {
23857c478bd9Sstevel@tonic-gate 		return;
23867c478bd9Sstevel@tonic-gate 	}
238748847494SEnrico Perla - Sun Microsystems 
238848847494SEnrico Perla - Sun Microsystems out_err:
238948847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
239048847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
2391cedc7e57SEnrico Perla - Sun Microsystems 	if (!is_flag_on(IS_SPARC_TARGET))
239248847494SEnrico Perla - Sun Microsystems 		set_dir_flag(FILE32, NEED_UPDATE);
239348847494SEnrico Perla - Sun Microsystems 	set_dir_flag(FILE64, NEED_UPDATE);
239448847494SEnrico Perla - Sun Microsystems }
239548847494SEnrico Perla - Sun Microsystems 
239648847494SEnrico Perla - Sun Microsystems /* Best effort stale entry removal */
239748847494SEnrico Perla - Sun Microsystems static void
239848847494SEnrico Perla - Sun Microsystems delete_stale(char *file, int what)
239948847494SEnrico Perla - Sun Microsystems {
240048847494SEnrico Perla - Sun Microsystems 	char		path[PATH_MAX];
240148847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
240248847494SEnrico Perla - Sun Microsystems 
240348847494SEnrico Perla - Sun Microsystems 	(void) snprintf(path, sizeof (path), "%s/%s", get_cachedir(what), file);
240448847494SEnrico Perla - Sun Microsystems 	if (!bam_check && stat(path, &sb) == 0) {
240548847494SEnrico Perla - Sun Microsystems 		if (sb.st_mode & S_IFDIR)
240648847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(path);
240748847494SEnrico Perla - Sun Microsystems 		else
240848847494SEnrico Perla - Sun Microsystems 			(void) unlink(path);
240948847494SEnrico Perla - Sun Microsystems 
241048847494SEnrico Perla - Sun Microsystems 		set_dir_flag(what, (NEED_UPDATE | NO_MULTI));
241148847494SEnrico Perla - Sun Microsystems 	}
24127c478bd9Sstevel@tonic-gate }
24137c478bd9Sstevel@tonic-gate 
2414a28d77b8Svikram /*
2415a28d77b8Svikram  * Checks if a file in the current (old) archive has
2416a28d77b8Svikram  * been deleted from the root filesystem. This is needed for
2417a28d77b8Svikram  * software like Trusted Extensions (TX) that switch early
2418a28d77b8Svikram  * in boot based on presence/absence of a kernel module.
2419a28d77b8Svikram  */
2420a28d77b8Svikram static void
2421a28d77b8Svikram check4stale(char *root)
2422a28d77b8Svikram {
2423a28d77b8Svikram 	nvpair_t	*nvp;
2424a28d77b8Svikram 	nvlist_t	*nvlp;
2425a28d77b8Svikram 	char 		*file;
2426a28d77b8Svikram 	char		path[PATH_MAX];
2427a28d77b8Svikram 
2428a28d77b8Svikram 	/*
2429a28d77b8Svikram 	 * Skip stale file check during smf check
2430a28d77b8Svikram 	 */
2431a28d77b8Svikram 	if (bam_smf_check)
2432a28d77b8Svikram 		return;
2433a28d77b8Svikram 
243448847494SEnrico Perla - Sun Microsystems 	/*
243548847494SEnrico Perla - Sun Microsystems 	 * If we need to (re)create the cache, there's no need to check for
243648847494SEnrico Perla - Sun Microsystems 	 * stale files
243748847494SEnrico Perla - Sun Microsystems 	 */
243848847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(NEED_CACHE_DIR))
243948847494SEnrico Perla - Sun Microsystems 		return;
244048847494SEnrico Perla - Sun Microsystems 
2441a28d77b8Svikram 	/* Nothing to do if no old stats */
2442a28d77b8Svikram 	if ((nvlp = walk_arg.old_nvlp) == NULL)
2443a28d77b8Svikram 		return;
2444a28d77b8Svikram 
2445a28d77b8Svikram 	for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp;
2446a28d77b8Svikram 	    nvp = nvlist_next_nvpair(nvlp, nvp)) {
2447a28d77b8Svikram 		file = nvpair_name(nvp);
2448a28d77b8Svikram 		if (file == NULL)
2449a28d77b8Svikram 			continue;
2450a28d77b8Svikram 		(void) snprintf(path, sizeof (path), "%s/%s",
2451a28d77b8Svikram 		    root, file);
245248847494SEnrico Perla - Sun Microsystems 		if (access(path, F_OK) < 0) {
245348847494SEnrico Perla - Sun Microsystems 			int	what;
245448847494SEnrico Perla - Sun Microsystems 
2455cedc7e57SEnrico Perla - Sun Microsystems 			if (bam_verbose)
2456cedc7e57SEnrico Perla - Sun Microsystems 				bam_print(PARSEABLE_STALE_FILE, path);
245748847494SEnrico Perla - Sun Microsystems 
2458cedc7e57SEnrico Perla - Sun Microsystems 			if (is_flag_on(IS_SPARC_TARGET)) {
2459cedc7e57SEnrico Perla - Sun Microsystems 				set_dir_flag(FILE64, NEED_UPDATE);
2460cedc7e57SEnrico Perla - Sun Microsystems 			} else {
246148847494SEnrico Perla - Sun Microsystems 				for (what = FILE32; what < CACHEDIR_NUM; what++)
246248847494SEnrico Perla - Sun Microsystems 					if (has_cachedir(what))
246348847494SEnrico Perla - Sun Microsystems 						delete_stale(file, what);
2464a28d77b8Svikram 			}
2465a28d77b8Svikram 		}
2466a28d77b8Svikram 	}
2467cedc7e57SEnrico Perla - Sun Microsystems }
2468a28d77b8Svikram 
24697c478bd9Sstevel@tonic-gate static void
24707c478bd9Sstevel@tonic-gate create_newstat(void)
24717c478bd9Sstevel@tonic-gate {
24727c478bd9Sstevel@tonic-gate 	int error;
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
24757c478bd9Sstevel@tonic-gate 	if (error) {
24767c478bd9Sstevel@tonic-gate 		/*
24777c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
24787c478bd9Sstevel@tonic-gate 		 */
24797c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
24807c478bd9Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
24817c478bd9Sstevel@tonic-gate 	}
24827c478bd9Sstevel@tonic-gate }
24837c478bd9Sstevel@tonic-gate 
248448847494SEnrico Perla - Sun Microsystems static int
24857c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
24867c478bd9Sstevel@tonic-gate {
24877c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
24887c478bd9Sstevel@tonic-gate 	line_t *lp;
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
2491986fd29aSsetje 		/*
2492986fd29aSsetje 		 * Don't follow symlinks.  A symlink must refer to
2493986fd29aSsetje 		 * a file that would appear in the archive through
2494986fd29aSsetje 		 * a direct reference.  This matches the archive
2495986fd29aSsetje 		 * construction behavior.
2496986fd29aSsetje 		 */
24977c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
2498986fd29aSsetje 		if (nftw(path, cmpstat, 20, FTW_PHYS) == -1) {
249948847494SEnrico Perla - Sun Microsystems 			if (is_flag_on(UPDATE_ERROR))
250048847494SEnrico Perla - Sun Microsystems 				return (BAM_ERROR);
25017c478bd9Sstevel@tonic-gate 			/*
25027c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
25037c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
25047c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
25057c478bd9Sstevel@tonic-gate 			 */
25067c478bd9Sstevel@tonic-gate 			if (bam_verbose)
25077c478bd9Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
25087c478bd9Sstevel@tonic-gate 		}
25097c478bd9Sstevel@tonic-gate 	}
251048847494SEnrico Perla - Sun Microsystems 
251148847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
25127c478bd9Sstevel@tonic-gate }
25137c478bd9Sstevel@tonic-gate 
2514*4a9df875SEnrico Perla - Sun Microsystems /*
2515*4a9df875SEnrico Perla - Sun Microsystems  * Update the timestamp file.
2516*4a9df875SEnrico Perla - Sun Microsystems  */
2517*4a9df875SEnrico Perla - Sun Microsystems static void
2518*4a9df875SEnrico Perla - Sun Microsystems update_timestamp(char *root)
2519*4a9df875SEnrico Perla - Sun Microsystems {
2520*4a9df875SEnrico Perla - Sun Microsystems 	char	timestamp_path[PATH_MAX];
2521*4a9df875SEnrico Perla - Sun Microsystems 
2522*4a9df875SEnrico Perla - Sun Microsystems 	/* this path length has already been checked in check_flags_and_files */
2523*4a9df875SEnrico Perla - Sun Microsystems 	(void) snprintf(timestamp_path, sizeof (timestamp_path), "%s%s", root,
2524*4a9df875SEnrico Perla - Sun Microsystems 	    FILE_STAT_TIMESTAMP);
2525*4a9df875SEnrico Perla - Sun Microsystems 
2526*4a9df875SEnrico Perla - Sun Microsystems 	/*
2527*4a9df875SEnrico Perla - Sun Microsystems 	 * recreate the timestamp file. Since an outdated or absent timestamp
2528*4a9df875SEnrico Perla - Sun Microsystems 	 * file translates in a complete rebuild of the archive cache, notify
2529*4a9df875SEnrico Perla - Sun Microsystems 	 * the user of the performance issue.
2530*4a9df875SEnrico Perla - Sun Microsystems 	 */
2531*4a9df875SEnrico Perla - Sun Microsystems 	if (creat(timestamp_path, FILE_STAT_MODE) < 0) {
2532*4a9df875SEnrico Perla - Sun Microsystems 		bam_error(OPEN_FAIL, timestamp_path, strerror(errno));
2533*4a9df875SEnrico Perla - Sun Microsystems 		bam_error(TIMESTAMP_FAIL, rootbuf);
2534*4a9df875SEnrico Perla - Sun Microsystems 	}
2535*4a9df875SEnrico Perla - Sun Microsystems }
2536*4a9df875SEnrico Perla - Sun Microsystems 
2537*4a9df875SEnrico Perla - Sun Microsystems 
25387c478bd9Sstevel@tonic-gate static void
25397c478bd9Sstevel@tonic-gate savenew(char *root)
25407c478bd9Sstevel@tonic-gate {
25417c478bd9Sstevel@tonic-gate 	char 	path[PATH_MAX];
25427c478bd9Sstevel@tonic-gate 	char 	path2[PATH_MAX];
25437c478bd9Sstevel@tonic-gate 	size_t 	sz;
25447c478bd9Sstevel@tonic-gate 	char 	*nstat;
25457c478bd9Sstevel@tonic-gate 	int 	fd, wrote, error;
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	nstat = NULL;
25487c478bd9Sstevel@tonic-gate 	sz = 0;
25497c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
25507c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
25517c478bd9Sstevel@tonic-gate 	if (error) {
25527c478bd9Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
25537c478bd9Sstevel@tonic-gate 		return;
25547c478bd9Sstevel@tonic-gate 	}
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
25577c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
25587c478bd9Sstevel@tonic-gate 	if (fd == -1) {
25597c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
25607c478bd9Sstevel@tonic-gate 		free(nstat);
25617c478bd9Sstevel@tonic-gate 		return;
25627c478bd9Sstevel@tonic-gate 	}
25637c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
25647c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
25657c478bd9Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
25667c478bd9Sstevel@tonic-gate 		(void) close(fd);
25677c478bd9Sstevel@tonic-gate 		free(nstat);
25687c478bd9Sstevel@tonic-gate 		return;
25697c478bd9Sstevel@tonic-gate 	}
25707c478bd9Sstevel@tonic-gate 	(void) close(fd);
25717c478bd9Sstevel@tonic-gate 	free(nstat);
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
25747c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
25757c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
25767c478bd9Sstevel@tonic-gate 	}
25777c478bd9Sstevel@tonic-gate }
25787c478bd9Sstevel@tonic-gate 
257948847494SEnrico Perla - Sun Microsystems #define	init_walk_args()	bzero(&walk_arg, sizeof (walk_arg))
258048847494SEnrico Perla - Sun Microsystems 
25817c478bd9Sstevel@tonic-gate static void
25827c478bd9Sstevel@tonic-gate clear_walk_args(void)
25837c478bd9Sstevel@tonic-gate {
25847c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
25857c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
25867c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
25877c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
258848847494SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile)
258948847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
25907c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
25917c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
259248847494SEnrico Perla - Sun Microsystems 	walk_arg.sparcfile = NULL;
25937c478bd9Sstevel@tonic-gate }
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate /*
25967c478bd9Sstevel@tonic-gate  * Returns:
25977c478bd9Sstevel@tonic-gate  *	0 - no update necessary
25987c478bd9Sstevel@tonic-gate  *	1 - update required.
25997c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
26007c478bd9Sstevel@tonic-gate  *
26017c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
26027c478bd9Sstevel@tonic-gate  * ================================
26037c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
26047c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
26057c478bd9Sstevel@tonic-gate  * to out of sync files.
26067c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
26077c478bd9Sstevel@tonic-gate  *
26087c478bd9Sstevel@tonic-gate  */
26097c478bd9Sstevel@tonic-gate static int
26107c478bd9Sstevel@tonic-gate update_required(char *root)
26117c478bd9Sstevel@tonic-gate {
26127c478bd9Sstevel@tonic-gate 	struct stat 	sb;
26137c478bd9Sstevel@tonic-gate 	char 		path[PATH_MAX];
26147c478bd9Sstevel@tonic-gate 	filelist_t 	flist;
26157c478bd9Sstevel@tonic-gate 	filelist_t 	*flistp = &flist;
261648847494SEnrico Perla - Sun Microsystems 	int 		ret;
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
26197c478bd9Sstevel@tonic-gate 
262048847494SEnrico Perla - Sun Microsystems 	if (is_sparc())
262148847494SEnrico Perla - Sun Microsystems 		set_flag(IS_SPARC_TARGET);
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	/*
262448847494SEnrico Perla - Sun Microsystems 	 * Check if cache directories and archives are present
26257c478bd9Sstevel@tonic-gate 	 */
262648847494SEnrico Perla - Sun Microsystems 
262748847494SEnrico Perla - Sun Microsystems 	ret = check_flags_and_files(root);
262848847494SEnrico Perla - Sun Microsystems 	if (ret < 0)
262948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	/*
26327c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
26337c478bd9Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
26347c478bd9Sstevel@tonic-gate 	 */
26357c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
26367c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
26377c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
26387c478bd9Sstevel@tonic-gate 			return (0);
26397c478bd9Sstevel@tonic-gate 	}
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 	getoldstat(root);
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 	/*
2644a28d77b8Svikram 	 * Check if the archive contains files that are no longer
2645a28d77b8Svikram 	 * present on the root filesystem.
2646a28d77b8Svikram 	 */
2647a28d77b8Svikram 	check4stale(root);
2648a28d77b8Svikram 
2649a28d77b8Svikram 	/*
26507c478bd9Sstevel@tonic-gate 	 * read list of files
26517c478bd9Sstevel@tonic-gate 	 */
26527c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
26537c478bd9Sstevel@tonic-gate 		clear_walk_args();
26547c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26557c478bd9Sstevel@tonic-gate 	}
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	/*
26607c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
26617c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
26627c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
26637c478bd9Sstevel@tonic-gate 	 */
26647c478bd9Sstevel@tonic-gate 	create_newstat();
26657c478bd9Sstevel@tonic-gate 	/*
26667c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
26677c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
26687c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
26697c478bd9Sstevel@tonic-gate 	 */
267048847494SEnrico Perla - Sun Microsystems 	ret = walk_list(root, &flist);
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	/* done with the file list */
26737c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
26747c478bd9Sstevel@tonic-gate 
267548847494SEnrico Perla - Sun Microsystems 	/* something went wrong */
267648847494SEnrico Perla - Sun Microsystems 
267748847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_ERROR) {
267848847494SEnrico Perla - Sun Microsystems 		bam_error(CACHE_FAIL);
267948847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
26807c478bd9Sstevel@tonic-gate 	}
26817c478bd9Sstevel@tonic-gate 
268248847494SEnrico Perla - Sun Microsystems 	if (walk_arg.new_nvlp == NULL) {
2683cedc7e57SEnrico Perla - Sun Microsystems 		if (walk_arg.sparcfile != NULL)
268448847494SEnrico Perla - Sun Microsystems 			(void) fclose(walk_arg.sparcfile);
268548847494SEnrico Perla - Sun Microsystems 		bam_error(NO_NEW_STAT);
268648847494SEnrico Perla - Sun Microsystems 	}
26877c478bd9Sstevel@tonic-gate 
268848847494SEnrico Perla - Sun Microsystems 	/* If nothing was updated, discard newstat. */
268948847494SEnrico Perla - Sun Microsystems 
269048847494SEnrico Perla - Sun Microsystems 	if (!is_dir_flag_on(FILE32, NEED_UPDATE) &&
269148847494SEnrico Perla - Sun Microsystems 	    !is_dir_flag_on(FILE64, NEED_UPDATE)) {
26927c478bd9Sstevel@tonic-gate 		clear_walk_args();
26937c478bd9Sstevel@tonic-gate 		return (0);
26947c478bd9Sstevel@tonic-gate 	}
26957c478bd9Sstevel@tonic-gate 
2696cedc7e57SEnrico Perla - Sun Microsystems 	if (walk_arg.sparcfile != NULL)
269748847494SEnrico Perla - Sun Microsystems 		(void) fclose(walk_arg.sparcfile);
269848847494SEnrico Perla - Sun Microsystems 
26997c478bd9Sstevel@tonic-gate 	return (1);
27007c478bd9Sstevel@tonic-gate }
27017c478bd9Sstevel@tonic-gate 
270248847494SEnrico Perla - Sun Microsystems static int
270348847494SEnrico Perla - Sun Microsystems flushfs(char *root)
270448847494SEnrico Perla - Sun Microsystems {
270548847494SEnrico Perla - Sun Microsystems 	char	cmd[PATH_MAX + 30];
270648847494SEnrico Perla - Sun Microsystems 
270748847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmd, sizeof (cmd), "%s -f \"%s\" 2>/dev/null",
270848847494SEnrico Perla - Sun Microsystems 	    LOCKFS_PATH, root);
270948847494SEnrico Perla - Sun Microsystems 
271048847494SEnrico Perla - Sun Microsystems 	return (exec_cmd(cmd, NULL));
271148847494SEnrico Perla - Sun Microsystems }
271248847494SEnrico Perla - Sun Microsystems 
271348847494SEnrico Perla - Sun Microsystems static int
271448847494SEnrico Perla - Sun Microsystems do_archive_copy(char *source, char *dest)
271548847494SEnrico Perla - Sun Microsystems {
271648847494SEnrico Perla - Sun Microsystems 
2717cedc7e57SEnrico Perla - Sun Microsystems 	sync();
271848847494SEnrico Perla - Sun Microsystems 
271948847494SEnrico Perla - Sun Microsystems 	/* the equivalent of mv archive-new-$pid boot_archive */
2720cedc7e57SEnrico Perla - Sun Microsystems 	if (rename(source, dest) != 0) {
2721cedc7e57SEnrico Perla - Sun Microsystems 		(void) unlink(source);
272248847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
2723cedc7e57SEnrico Perla - Sun Microsystems 	}
272448847494SEnrico Perla - Sun Microsystems 
272548847494SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
2726cedc7e57SEnrico Perla - Sun Microsystems 		sync();
272748847494SEnrico Perla - Sun Microsystems 
272848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
272948847494SEnrico Perla - Sun Microsystems }
273048847494SEnrico Perla - Sun Microsystems 
273148847494SEnrico Perla - Sun Microsystems static int
273248847494SEnrico Perla - Sun Microsystems check_cmdline(filelist_t flist)
273348847494SEnrico Perla - Sun Microsystems {
273448847494SEnrico Perla - Sun Microsystems 	line_t	*lp;
273548847494SEnrico Perla - Sun Microsystems 
273648847494SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next) {
273748847494SEnrico Perla - Sun Microsystems 		if (strstr(lp->line, "Error:") != NULL ||
273848847494SEnrico Perla - Sun Microsystems 		    strstr(lp->line, "Inode number overflow") != NULL) {
2739cedc7e57SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, "%s\n", lp->line);
274048847494SEnrico Perla - Sun Microsystems 			return (BAM_ERROR);
274148847494SEnrico Perla - Sun Microsystems 		}
274248847494SEnrico Perla - Sun Microsystems 	}
274348847494SEnrico Perla - Sun Microsystems 
274448847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
274548847494SEnrico Perla - Sun Microsystems }
274648847494SEnrico Perla - Sun Microsystems 
2747cedc7e57SEnrico Perla - Sun Microsystems static void
2748cedc7e57SEnrico Perla - Sun Microsystems dump_errormsg(filelist_t flist)
2749cedc7e57SEnrico Perla - Sun Microsystems {
2750cedc7e57SEnrico Perla - Sun Microsystems 	line_t	*lp;
2751cedc7e57SEnrico Perla - Sun Microsystems 
2752cedc7e57SEnrico Perla - Sun Microsystems 	for (lp = flist.head; lp; lp = lp->next)
2753cedc7e57SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, "%s\n", lp->line);
2754cedc7e57SEnrico Perla - Sun Microsystems }
2755cedc7e57SEnrico Perla - Sun Microsystems 
275648847494SEnrico Perla - Sun Microsystems static int
275748847494SEnrico Perla - Sun Microsystems check_archive(char *dest)
275848847494SEnrico Perla - Sun Microsystems {
275948847494SEnrico Perla - Sun Microsystems 	struct stat	sb;
276048847494SEnrico Perla - Sun Microsystems 
276148847494SEnrico Perla - Sun Microsystems 	if (stat(dest, &sb) != 0 || !S_ISREG(sb.st_mode) ||
276248847494SEnrico Perla - Sun Microsystems 	    sb.st_size < 10000) {
276348847494SEnrico Perla - Sun Microsystems 		bam_error(ARCHIVE_BAD, dest);
276448847494SEnrico Perla - Sun Microsystems 		(void) unlink(dest);
276548847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
276648847494SEnrico Perla - Sun Microsystems 	}
276748847494SEnrico Perla - Sun Microsystems 
276848847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
276948847494SEnrico Perla - Sun Microsystems }
277048847494SEnrico Perla - Sun Microsystems 
277148847494SEnrico Perla - Sun Microsystems /*
277248847494SEnrico Perla - Sun Microsystems  * Returns 1 if mkiso is in the expected PATH, 0 otherwise
277348847494SEnrico Perla - Sun Microsystems  */
277448847494SEnrico Perla - Sun Microsystems static int
277548847494SEnrico Perla - Sun Microsystems is_mkisofs()
277648847494SEnrico Perla - Sun Microsystems {
2777cedc7e57SEnrico Perla - Sun Microsystems 	if (access(MKISOFS_PATH, X_OK) == 0)
277848847494SEnrico Perla - Sun Microsystems 		return (1);
277948847494SEnrico Perla - Sun Microsystems 	return (0);
278048847494SEnrico Perla - Sun Microsystems }
278148847494SEnrico Perla - Sun Microsystems 
278248847494SEnrico Perla - Sun Microsystems #define	MKISO_PARAMS	" -quiet -graft-points -dlrDJN -relaxed-filenames "
278348847494SEnrico Perla - Sun Microsystems 
278448847494SEnrico Perla - Sun Microsystems static int
278548847494SEnrico Perla - Sun Microsystems create_sparc_archive(char *archive, char *tempname, char *bootblk, char *list)
278648847494SEnrico Perla - Sun Microsystems {
278748847494SEnrico Perla - Sun Microsystems 	int		ret;
278848847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
278948847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
279048847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_sparc_archive()";
279148847494SEnrico Perla - Sun Microsystems 
279248847494SEnrico Perla - Sun Microsystems 	if (access(bootblk, R_OK) == 1) {
279348847494SEnrico Perla - Sun Microsystems 		bam_error(BOOTBLK_FAIL, bootblk);
279448847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
279548847494SEnrico Perla - Sun Microsystems 	}
279648847494SEnrico Perla - Sun Microsystems 
279748847494SEnrico Perla - Sun Microsystems 	/*
279848847494SEnrico Perla - Sun Microsystems 	 * Prepare mkisofs command line and execute it
279948847494SEnrico Perla - Sun Microsystems 	 */
280048847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -G %s -o \"%s\" "
2801cedc7e57SEnrico Perla - Sun Microsystems 	    "-path-list \"%s\" 2>&1", MKISOFS_PATH, MKISO_PARAMS, bootblk,
280248847494SEnrico Perla - Sun Microsystems 	    tempname, list);
280348847494SEnrico Perla - Sun Microsystems 
280448847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
280548847494SEnrico Perla - Sun Microsystems 
280648847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
2807cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
2808cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
280948847494SEnrico Perla - Sun Microsystems 		goto out_err;
2810cedc7e57SEnrico Perla - Sun Microsystems 	}
281148847494SEnrico Perla - Sun Microsystems 
281248847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
281348847494SEnrico Perla - Sun Microsystems 
281448847494SEnrico Perla - Sun Microsystems 	/*
281548847494SEnrico Perla - Sun Microsystems 	 * Prepare dd command line to copy the bootblk on the new archive and
281648847494SEnrico Perla - Sun Microsystems 	 * execute it
281748847494SEnrico Perla - Sun Microsystems 	 */
281848847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=\"%s\" of=\"%s\""
2819cedc7e57SEnrico Perla - Sun Microsystems 	    " bs=1b oseek=1 count=15 conv=notrunc conv=sync 2>&1", DD_PATH_USR,
282048847494SEnrico Perla - Sun Microsystems 	    bootblk, tempname);
282148847494SEnrico Perla - Sun Microsystems 
282248847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
282348847494SEnrico Perla - Sun Microsystems 
282448847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
282548847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR)
282648847494SEnrico Perla - Sun Microsystems 		goto out_err;
282748847494SEnrico Perla - Sun Microsystems 
282848847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
282948847494SEnrico Perla - Sun Microsystems 
283048847494SEnrico Perla - Sun Microsystems 	/* Did we get a valid archive ? */
283148847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
283248847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
283348847494SEnrico Perla - Sun Microsystems 
283448847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
283548847494SEnrico Perla - Sun Microsystems 
283648847494SEnrico Perla - Sun Microsystems out_err:
283748847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
283848847494SEnrico Perla - Sun Microsystems 	bam_error(ARCHIVE_FAIL, cmdline);
283948847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
284048847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
284148847494SEnrico Perla - Sun Microsystems }
284248847494SEnrico Perla - Sun Microsystems 
284348847494SEnrico Perla - Sun Microsystems static unsigned int
284448847494SEnrico Perla - Sun Microsystems from_733(unsigned char *s)
284548847494SEnrico Perla - Sun Microsystems {
284648847494SEnrico Perla - Sun Microsystems 	int		i;
284748847494SEnrico Perla - Sun Microsystems 	unsigned int	ret = 0;
284848847494SEnrico Perla - Sun Microsystems 
284948847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
285048847494SEnrico Perla - Sun Microsystems 		ret |= s[i] << (8 * i);
285148847494SEnrico Perla - Sun Microsystems 
285248847494SEnrico Perla - Sun Microsystems 	return (ret);
285348847494SEnrico Perla - Sun Microsystems }
285448847494SEnrico Perla - Sun Microsystems 
285548847494SEnrico Perla - Sun Microsystems static void
285648847494SEnrico Perla - Sun Microsystems to_733(unsigned char *s, unsigned int val)
285748847494SEnrico Perla - Sun Microsystems {
285848847494SEnrico Perla - Sun Microsystems 	int	i;
285948847494SEnrico Perla - Sun Microsystems 
286048847494SEnrico Perla - Sun Microsystems 	for (i = 0; i < 4; i++)
286148847494SEnrico Perla - Sun Microsystems 		s[i] = s[7-i] = (val >> (8 * i)) & 0xFF;
286248847494SEnrico Perla - Sun Microsystems }
286348847494SEnrico Perla - Sun Microsystems 
286448847494SEnrico Perla - Sun Microsystems /*
286548847494SEnrico Perla - Sun Microsystems  * Extends the current boot archive without recreating it from scratch
286648847494SEnrico Perla - Sun Microsystems  */
286748847494SEnrico Perla - Sun Microsystems static int
286848847494SEnrico Perla - Sun Microsystems extend_iso_archive(char *archive, char *tempname, char *update_dir)
286948847494SEnrico Perla - Sun Microsystems {
287048847494SEnrico Perla - Sun Microsystems 	int			fd = -1, newfd = -1, ret, i;
287148847494SEnrico Perla - Sun Microsystems 	int			next_session = 0, new_size = 0;
287248847494SEnrico Perla - Sun Microsystems 	char			cmdline[3 * PATH_MAX + 64];
287348847494SEnrico Perla - Sun Microsystems 	const char		*func = "extend_iso_archive()";
287448847494SEnrico Perla - Sun Microsystems 	filelist_t		flist = {0};
287548847494SEnrico Perla - Sun Microsystems 	struct iso_pdesc	saved_desc[MAX_IVDs];
287648847494SEnrico Perla - Sun Microsystems 
287748847494SEnrico Perla - Sun Microsystems 	fd = open(archive, O_RDWR);
287848847494SEnrico Perla - Sun Microsystems 	if (fd == -1) {
2879cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
288048847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, archive, strerror(errno));
288148847494SEnrico Perla - Sun Microsystems 		goto out_err;
288248847494SEnrico Perla - Sun Microsystems 	}
288348847494SEnrico Perla - Sun Microsystems 
288448847494SEnrico Perla - Sun Microsystems 	/*
288548847494SEnrico Perla - Sun Microsystems 	 * A partial read is likely due to a corrupted file
288648847494SEnrico Perla - Sun Microsystems 	 */
288748847494SEnrico Perla - Sun Microsystems 	ret = pread64(fd, saved_desc, sizeof (saved_desc),
288848847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
288948847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
2890cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
289148847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, archive, strerror(errno));
289248847494SEnrico Perla - Sun Microsystems 		goto out_err;
289348847494SEnrico Perla - Sun Microsystems 	}
289448847494SEnrico Perla - Sun Microsystems 
289548847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
2896cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
289748847494SEnrico Perla - Sun Microsystems 			bam_error(SIGN_FAIL, archive);
289848847494SEnrico Perla - Sun Microsystems 		goto out_err;
289948847494SEnrico Perla - Sun Microsystems 	}
290048847494SEnrico Perla - Sun Microsystems 
290148847494SEnrico Perla - Sun Microsystems 	/*
290248847494SEnrico Perla - Sun Microsystems 	 * Read primary descriptor and locate next_session offset (it should
290348847494SEnrico Perla - Sun Microsystems 	 * point to the end of the archive)
290448847494SEnrico Perla - Sun Microsystems 	 */
290548847494SEnrico Perla - Sun Microsystems 	next_session = P2ROUNDUP(from_733(saved_desc[0].volume_space_size), 16);
290648847494SEnrico Perla - Sun Microsystems 
290748847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s -C 16,%d -M %s %s -o \""
2908cedc7e57SEnrico Perla - Sun Microsystems 	    "%s\" \"%s\" 2>&1", MKISOFS_PATH, next_session, archive,
2909cedc7e57SEnrico Perla - Sun Microsystems 	    MKISO_PARAMS, tempname, update_dir);
291048847494SEnrico Perla - Sun Microsystems 
291148847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
291248847494SEnrico Perla - Sun Microsystems 
291348847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
291448847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
2915cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose) {
291648847494SEnrico Perla - Sun Microsystems 			bam_error(MULTI_FAIL, cmdline);
2917cedc7e57SEnrico Perla - Sun Microsystems 			dump_errormsg(flist);
2918cedc7e57SEnrico Perla - Sun Microsystems 		}
291948847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
292048847494SEnrico Perla - Sun Microsystems 	}
292148847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
292248847494SEnrico Perla - Sun Microsystems 
292348847494SEnrico Perla - Sun Microsystems 	newfd = open(tempname, O_RDONLY);
292448847494SEnrico Perla - Sun Microsystems 	if (newfd == -1) {
2925cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
292648847494SEnrico Perla - Sun Microsystems 			bam_error(OPEN_FAIL, archive, strerror(errno));
292748847494SEnrico Perla - Sun Microsystems 		goto out_err;
292848847494SEnrico Perla - Sun Microsystems 	}
292948847494SEnrico Perla - Sun Microsystems 
293048847494SEnrico Perla - Sun Microsystems 	ret = pread64(newfd, saved_desc, sizeof (saved_desc),
293148847494SEnrico Perla - Sun Microsystems 	    VOLDESC_OFF * CD_BLOCK);
293248847494SEnrico Perla - Sun Microsystems 	if (ret != sizeof (saved_desc)) {
2933cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
293448847494SEnrico Perla - Sun Microsystems 			bam_error(READ_FAIL, archive, strerror(errno));
293548847494SEnrico Perla - Sun Microsystems 		goto out_err;
293648847494SEnrico Perla - Sun Microsystems 	}
293748847494SEnrico Perla - Sun Microsystems 
293848847494SEnrico Perla - Sun Microsystems 	if (memcmp(saved_desc[0].type, "\1CD001", 6)) {
2939cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
294048847494SEnrico Perla - Sun Microsystems 			bam_error(SIGN_FAIL, archive);
294148847494SEnrico Perla - Sun Microsystems 		goto out_err;
294248847494SEnrico Perla - Sun Microsystems 	}
294348847494SEnrico Perla - Sun Microsystems 
294448847494SEnrico Perla - Sun Microsystems 	new_size = from_733(saved_desc[0].volume_space_size) + next_session;
294548847494SEnrico Perla - Sun Microsystems 	to_733(saved_desc[0].volume_space_size, new_size);
294648847494SEnrico Perla - Sun Microsystems 
294748847494SEnrico Perla - Sun Microsystems 	for (i = 1; i < MAX_IVDs; i++) {
294848847494SEnrico Perla - Sun Microsystems 		if (saved_desc[i].type[0] == (unsigned char)255)
294948847494SEnrico Perla - Sun Microsystems 			break;
295048847494SEnrico Perla - Sun Microsystems 		if (memcmp(saved_desc[i].id, "CD001", 5))
295148847494SEnrico Perla - Sun Microsystems 			break;
295248847494SEnrico Perla - Sun Microsystems 
295348847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
295448847494SEnrico Perla - Sun Microsystems 			bam_print("%s: Updating descriptor entry [%d]\n", func,
295548847494SEnrico Perla - Sun Microsystems 			    i);
295648847494SEnrico Perla - Sun Microsystems 
295748847494SEnrico Perla - Sun Microsystems 		to_733(saved_desc[i].volume_space_size, new_size);
295848847494SEnrico Perla - Sun Microsystems 	}
295948847494SEnrico Perla - Sun Microsystems 
296048847494SEnrico Perla - Sun Microsystems 	ret = pwrite64(fd, saved_desc, DVD_BLOCK, VOLDESC_OFF*CD_BLOCK);
296148847494SEnrico Perla - Sun Microsystems 	if (ret != DVD_BLOCK) {
2962cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
296348847494SEnrico Perla - Sun Microsystems 			bam_error(WRITE_FAIL, archive, strerror(errno));
296448847494SEnrico Perla - Sun Microsystems 		goto out_err;
296548847494SEnrico Perla - Sun Microsystems 	}
296648847494SEnrico Perla - Sun Microsystems 	(void) close(newfd);
296748847494SEnrico Perla - Sun Microsystems 	newfd = -1;
296848847494SEnrico Perla - Sun Microsystems 
2969cedc7e57SEnrico Perla - Sun Microsystems 	ret = fsync(fd);
2970cedc7e57SEnrico Perla - Sun Microsystems 	if (ret != 0)
2971cedc7e57SEnrico Perla - Sun Microsystems 		sync();
2972cedc7e57SEnrico Perla - Sun Microsystems 
297348847494SEnrico Perla - Sun Microsystems 	ret = close(fd);
297448847494SEnrico Perla - Sun Microsystems 	if (ret != 0) {
2975cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
297648847494SEnrico Perla - Sun Microsystems 			bam_error(CLOSE_FAIL, archive, strerror(errno));
297748847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
297848847494SEnrico Perla - Sun Microsystems 	}
297948847494SEnrico Perla - Sun Microsystems 	fd = -1;
298048847494SEnrico Perla - Sun Microsystems 
298148847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s if=%s of=%s bs=32k "
2982cedc7e57SEnrico Perla - Sun Microsystems 	    "seek=%d conv=sync 2>&1", DD_PATH_USR, tempname, archive,
298348847494SEnrico Perla - Sun Microsystems 	    (next_session/16));
298448847494SEnrico Perla - Sun Microsystems 
298548847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
298648847494SEnrico Perla - Sun Microsystems 
298748847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
298848847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
2989cedc7e57SEnrico Perla - Sun Microsystems 		if (bam_verbose)
299048847494SEnrico Perla - Sun Microsystems 			bam_error(MULTI_FAIL, cmdline);
299148847494SEnrico Perla - Sun Microsystems 		goto out_flist_err;
299248847494SEnrico Perla - Sun Microsystems 	}
299348847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
299448847494SEnrico Perla - Sun Microsystems 
299548847494SEnrico Perla - Sun Microsystems 	(void) unlink(tempname);
299648847494SEnrico Perla - Sun Microsystems 
2997cedc7e57SEnrico Perla - Sun Microsystems 	if (flushfs(bam_root) != 0)
2998cedc7e57SEnrico Perla - Sun Microsystems 		sync();
2999cedc7e57SEnrico Perla - Sun Microsystems 
300048847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
300148847494SEnrico Perla - Sun Microsystems 		bam_print("boot archive updated successfully\n");
300248847494SEnrico Perla - Sun Microsystems 
300348847494SEnrico Perla - Sun Microsystems 	return (BAM_SUCCESS);
300448847494SEnrico Perla - Sun Microsystems 
300548847494SEnrico Perla - Sun Microsystems out_flist_err:
300648847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
300748847494SEnrico Perla - Sun Microsystems out_err:
300848847494SEnrico Perla - Sun Microsystems 	if (fd != -1)
300948847494SEnrico Perla - Sun Microsystems 		(void) close(fd);
301048847494SEnrico Perla - Sun Microsystems 	if (newfd != -1)
301148847494SEnrico Perla - Sun Microsystems 		(void) close(newfd);
301248847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
301348847494SEnrico Perla - Sun Microsystems }
301448847494SEnrico Perla - Sun Microsystems 
301548847494SEnrico Perla - Sun Microsystems static int
301648847494SEnrico Perla - Sun Microsystems create_x86_archive(char *archive, char *tempname, char *update_dir)
301748847494SEnrico Perla - Sun Microsystems {
301848847494SEnrico Perla - Sun Microsystems 	int		ret;
301948847494SEnrico Perla - Sun Microsystems 	char		cmdline[3 * PATH_MAX + 64];
302048847494SEnrico Perla - Sun Microsystems 	filelist_t	flist = {0};
302148847494SEnrico Perla - Sun Microsystems 	const char	*func = "create_x86_archive()";
302248847494SEnrico Perla - Sun Microsystems 
302348847494SEnrico Perla - Sun Microsystems 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s -o \"%s\" \"%s\" "
3024cedc7e57SEnrico Perla - Sun Microsystems 	    "2>&1", MKISOFS_PATH, MKISO_PARAMS, tempname, update_dir);
302548847494SEnrico Perla - Sun Microsystems 
302648847494SEnrico Perla - Sun Microsystems 	BAM_DPRINTF((D_CMDLINE, func, cmdline));
302748847494SEnrico Perla - Sun Microsystems 
302848847494SEnrico Perla - Sun Microsystems 	ret = exec_cmd(cmdline, &flist);
302948847494SEnrico Perla - Sun Microsystems 	if (ret != 0 || check_cmdline(flist) == BAM_ERROR) {
303048847494SEnrico Perla - Sun Microsystems 		bam_error(ARCHIVE_FAIL, cmdline);
3031cedc7e57SEnrico Perla - Sun Microsystems 		dump_errormsg(flist);
303248847494SEnrico Perla - Sun Microsystems 		filelist_free(&flist);
303348847494SEnrico Perla - Sun Microsystems 		(void) unlink(tempname);
303448847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
303548847494SEnrico Perla - Sun Microsystems 	}
303648847494SEnrico Perla - Sun Microsystems 
303748847494SEnrico Perla - Sun Microsystems 	filelist_free(&flist);
303848847494SEnrico Perla - Sun Microsystems 
303948847494SEnrico Perla - Sun Microsystems 	if (check_archive(tempname) == BAM_ERROR)
304048847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
304148847494SEnrico Perla - Sun Microsystems 
304248847494SEnrico Perla - Sun Microsystems 	return (do_archive_copy(tempname, archive));
304348847494SEnrico Perla - Sun Microsystems }
304448847494SEnrico Perla - Sun Microsystems 
304548847494SEnrico Perla - Sun Microsystems static int
304648847494SEnrico Perla - Sun Microsystems mkisofs_archive(char *root, int what)
304748847494SEnrico Perla - Sun Microsystems {
304848847494SEnrico Perla - Sun Microsystems 	int		ret;
304948847494SEnrico Perla - Sun Microsystems 	char		temp[PATH_MAX];
305048847494SEnrico Perla - Sun Microsystems 	char 		bootblk[PATH_MAX];
305148847494SEnrico Perla - Sun Microsystems 	char		boot_archive[PATH_MAX];
305248847494SEnrico Perla - Sun Microsystems 
305348847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
305448847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp),
305548847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64/archive-new-%d", root, ARCHIVE_PREFIX,
305648847494SEnrico Perla - Sun Microsystems 		    get_machine(), getpid());
305748847494SEnrico Perla - Sun Microsystems 	else
305848847494SEnrico Perla - Sun Microsystems 		ret = snprintf(temp, sizeof (temp), "%s%s%s/archive-new-%d",
305948847494SEnrico Perla - Sun Microsystems 		    root, ARCHIVE_PREFIX, get_machine(), getpid());
306048847494SEnrico Perla - Sun Microsystems 
306148847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (temp))
306248847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
306348847494SEnrico Perla - Sun Microsystems 
306448847494SEnrico Perla - Sun Microsystems 	if (what == FILE64 && !is_flag_on(IS_SPARC_TARGET))
306548847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
306648847494SEnrico Perla - Sun Microsystems 		    "%s%s%s/amd64%s", root, ARCHIVE_PREFIX, get_machine(),
306748847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
306848847494SEnrico Perla - Sun Microsystems 	else
306948847494SEnrico Perla - Sun Microsystems 		ret = snprintf(boot_archive, sizeof (boot_archive),
307048847494SEnrico Perla - Sun Microsystems 		    "%s%s%s%s", root, ARCHIVE_PREFIX, get_machine(),
307148847494SEnrico Perla - Sun Microsystems 		    ARCHIVE_SUFFIX);
307248847494SEnrico Perla - Sun Microsystems 
307348847494SEnrico Perla - Sun Microsystems 	if (ret >= sizeof (boot_archive))
307448847494SEnrico Perla - Sun Microsystems 		goto out_path_err;
307548847494SEnrico Perla - Sun Microsystems 
307648847494SEnrico Perla - Sun Microsystems 	bam_print("updating %s\n", boot_archive);
307748847494SEnrico Perla - Sun Microsystems 
307848847494SEnrico Perla - Sun Microsystems 	if (is_flag_on(IS_SPARC_TARGET)) {
307948847494SEnrico Perla - Sun Microsystems 		ret = snprintf(bootblk, sizeof (bootblk),
308048847494SEnrico Perla - Sun Microsystems 		    "%s/platform/%s/lib/fs/hsfs/bootblk", root, get_machine());
308148847494SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (bootblk))
308248847494SEnrico Perla - Sun Microsystems 			goto out_path_err;
308348847494SEnrico Perla - Sun Microsystems 
308448847494SEnrico Perla - Sun Microsystems 		ret = create_sparc_archive(boot_archive, temp, bootblk,
308548847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
308648847494SEnrico Perla - Sun Microsystems 	} else {
308748847494SEnrico Perla - Sun Microsystems 		if (!is_dir_flag_on(what, NO_MULTI)) {
308848847494SEnrico Perla - Sun Microsystems 			if (bam_verbose)
308948847494SEnrico Perla - Sun Microsystems 				bam_print("Attempting to extend x86 archive: "
309048847494SEnrico Perla - Sun Microsystems 				    "%s\n", boot_archive);
309148847494SEnrico Perla - Sun Microsystems 
309248847494SEnrico Perla - Sun Microsystems 			ret = extend_iso_archive(boot_archive, temp,
309348847494SEnrico Perla - Sun Microsystems 			    get_updatedir(what));
309448847494SEnrico Perla - Sun Microsystems 			if (ret == BAM_SUCCESS) {
309548847494SEnrico Perla - Sun Microsystems 				if (bam_verbose)
309648847494SEnrico Perla - Sun Microsystems 					bam_print("Successfully extended %s\n",
309748847494SEnrico Perla - Sun Microsystems 					    boot_archive);
309848847494SEnrico Perla - Sun Microsystems 
309948847494SEnrico Perla - Sun Microsystems 				(void) rmdir_r(get_updatedir(what));
310048847494SEnrico Perla - Sun Microsystems 				return (BAM_SUCCESS);
310148847494SEnrico Perla - Sun Microsystems 			}
310248847494SEnrico Perla - Sun Microsystems 		}
310348847494SEnrico Perla - Sun Microsystems 		/*
310448847494SEnrico Perla - Sun Microsystems 		 * The boot archive will be recreated from scratch. We get here
310548847494SEnrico Perla - Sun Microsystems 		 * if at least one of these conditions is true:
310648847494SEnrico Perla - Sun Microsystems 		 * - bootadm was called without the -e switch
310748847494SEnrico Perla - Sun Microsystems 		 * - the archive (or the archive cache) doesn't exist
310848847494SEnrico Perla - Sun Microsystems 		 * - archive size is bigger than BA_SIZE_MAX
310948847494SEnrico Perla - Sun Microsystems 		 * - more than COUNT_MAX files need to be updated
311048847494SEnrico Perla - Sun Microsystems 		 * - an error occourred either populating the /updates directory
311148847494SEnrico Perla - Sun Microsystems 		 *   or extend_iso_archive() failed
311248847494SEnrico Perla - Sun Microsystems 		 */
311348847494SEnrico Perla - Sun Microsystems 		if (bam_verbose)
311448847494SEnrico Perla - Sun Microsystems 			bam_print("Unable to extend %s... rebuilding archive\n",
311548847494SEnrico Perla - Sun Microsystems 			    boot_archive);
311648847494SEnrico Perla - Sun Microsystems 
311748847494SEnrico Perla - Sun Microsystems 		if (get_updatedir(what)[0] != '\0')
311848847494SEnrico Perla - Sun Microsystems 			(void) rmdir_r(get_updatedir(what));
311948847494SEnrico Perla - Sun Microsystems 
312048847494SEnrico Perla - Sun Microsystems 
312148847494SEnrico Perla - Sun Microsystems 		ret = create_x86_archive(boot_archive, temp,
312248847494SEnrico Perla - Sun Microsystems 		    get_cachedir(what));
312348847494SEnrico Perla - Sun Microsystems 	}
312448847494SEnrico Perla - Sun Microsystems 
312548847494SEnrico Perla - Sun Microsystems 	if (ret == BAM_SUCCESS && bam_verbose)
312648847494SEnrico Perla - Sun Microsystems 		bam_print("Successfully created %s\n", boot_archive);
312748847494SEnrico Perla - Sun Microsystems 
312848847494SEnrico Perla - Sun Microsystems 	return (ret);
312948847494SEnrico Perla - Sun Microsystems 
313048847494SEnrico Perla - Sun Microsystems out_path_err:
313148847494SEnrico Perla - Sun Microsystems 	bam_error(PATH_TOO_LONG, root);
313248847494SEnrico Perla - Sun Microsystems 	return (BAM_ERROR);
313348847494SEnrico Perla - Sun Microsystems }
313448847494SEnrico Perla - Sun Microsystems 
31357c478bd9Sstevel@tonic-gate static error_t
31367c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
31377c478bd9Sstevel@tonic-gate {
31387c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
31397c478bd9Sstevel@tonic-gate 	size_t len;
31407c478bd9Sstevel@tonic-gate 	struct stat sb;
3141cedc7e57SEnrico Perla - Sun Microsystems 	int ret, what, status = BAM_SUCCESS;
314248847494SEnrico Perla - Sun Microsystems 
314348847494SEnrico Perla - Sun Microsystems 	/* If there is mkisofs, use it to create the required archives */
314448847494SEnrico Perla - Sun Microsystems 	if (is_mkisofs()) {
314548847494SEnrico Perla - Sun Microsystems 		for (what = FILE32; what < CACHEDIR_NUM; what++) {
3146cedc7e57SEnrico Perla - Sun Microsystems 			if (has_cachedir(what) && is_dir_flag_on(what,
3147cedc7e57SEnrico Perla - Sun Microsystems 			    NEED_UPDATE)) {
314848847494SEnrico Perla - Sun Microsystems 				ret = mkisofs_archive(root, what);
314948847494SEnrico Perla - Sun Microsystems 				if (ret != 0)
3150cedc7e57SEnrico Perla - Sun Microsystems 					status = BAM_ERROR;
315148847494SEnrico Perla - Sun Microsystems 			}
315248847494SEnrico Perla - Sun Microsystems 		}
3153cedc7e57SEnrico Perla - Sun Microsystems 		return (status);
315448847494SEnrico Perla - Sun Microsystems 	}
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 	/*
315748847494SEnrico Perla - Sun Microsystems 	 * Else setup command args for create_ramdisk.ksh for the UFS archives
31587c478bd9Sstevel@tonic-gate 	 */
315948847494SEnrico Perla - Sun Microsystems 	if (bam_verbose)
316048847494SEnrico Perla - Sun Microsystems 		bam_print("mkisofs not found, creating UFS archive\n");
316148847494SEnrico Perla - Sun Microsystems 
3162986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
31637c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
31647c478bd9Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
31657c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
31667c478bd9Sstevel@tonic-gate 	}
31677c478bd9Sstevel@tonic-gate 
3168cedc7e57SEnrico Perla - Sun Microsystems 	if (is_safe_exec(path) == BAM_ERROR)
3169cedc7e57SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
3170cedc7e57SEnrico Perla - Sun Microsystems 
31717c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
3172d876c67dSjg 	if (bam_alt_platform)
3173d876c67dSjg 		len += strlen(bam_platform) + strlen("-p ");
31747c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
31757c478bd9Sstevel@tonic-gate 
3176d876c67dSjg 	if (bam_alt_platform) {
3177d876c67dSjg 		assert(strlen(root) > 1);
3178d876c67dSjg 		(void) snprintf(cmdline, len, "%s -p %s -R %s",
3179d876c67dSjg 		    path, bam_platform, root);
3180d876c67dSjg 		/* chop off / at the end */
3181d876c67dSjg 		cmdline[strlen(cmdline) - 1] = '\0';
3182d876c67dSjg 	} else if (strlen(root) > 1) {
31837c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
31847c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
31857c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
31867c478bd9Sstevel@tonic-gate 	} else
31877c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
31887c478bd9Sstevel@tonic-gate 
3189986fd29aSsetje 	if (exec_cmd(cmdline, NULL) != 0) {
31907c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
31917c478bd9Sstevel@tonic-gate 		free(cmdline);
31927c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
31937c478bd9Sstevel@tonic-gate 	}
31947c478bd9Sstevel@tonic-gate 	free(cmdline);
31957c478bd9Sstevel@tonic-gate 	/*
3196986fd29aSsetje 	 * The existence of the expected archives used to be
3197986fd29aSsetje 	 * verified here. This check is done in create_ramdisk as
3198986fd29aSsetje 	 * it needs to be in sync with the altroot operated upon.
31997c478bd9Sstevel@tonic-gate 	 */
32007c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
32017c478bd9Sstevel@tonic-gate }
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate /*
32047c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
32057c478bd9Sstevel@tonic-gate  * 1 - is miniroot
32067c478bd9Sstevel@tonic-gate  * 0 - is not
32077c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
32087c478bd9Sstevel@tonic-gate  */
32097c478bd9Sstevel@tonic-gate static int
32107c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
32117c478bd9Sstevel@tonic-gate {
32127c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
32137c478bd9Sstevel@tonic-gate 	FILE *fp;
32147c478bd9Sstevel@tonic-gate 	int found;
3215b610f78eSvikram 	char mntpt[PATH_MAX];
3216b610f78eSvikram 	char *cp;
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate 	/*
32197c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
32207c478bd9Sstevel@tonic-gate 	 * of dubious value:
3221b610f78eSvikram 	 *	- create boot_archive on a lofi-mounted boot_archive
32227c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
32237c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
32247c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
32257c478bd9Sstevel@tonic-gate 	 * worth it.
32267c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
32277c478bd9Sstevel@tonic-gate 	 */
32287c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
32297c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
32307c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
32317c478bd9Sstevel@tonic-gate 		return (0);
32327c478bd9Sstevel@tonic-gate 	}
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
32357c478bd9Sstevel@tonic-gate 
3236b610f78eSvikram 	/*
3237b610f78eSvikram 	 * Remove any trailing / from the mount point
3238b610f78eSvikram 	 */
3239b610f78eSvikram 	(void) strlcpy(mntpt, root, sizeof (mntpt));
3240b610f78eSvikram 	if (strcmp(root, "/") != 0) {
3241b610f78eSvikram 		cp = mntpt + strlen(mntpt) - 1;
3242b610f78eSvikram 		if (*cp == '/')
3243b610f78eSvikram 			*cp = '\0';
3244b610f78eSvikram 	}
32457c478bd9Sstevel@tonic-gate 	found = 0;
32467c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
3247b610f78eSvikram 		if (strcmp(mnt.mnt_mountp, mntpt) == 0) {
32487c478bd9Sstevel@tonic-gate 			found = 1;
32497c478bd9Sstevel@tonic-gate 			break;
32507c478bd9Sstevel@tonic-gate 		}
32517c478bd9Sstevel@tonic-gate 	}
32527c478bd9Sstevel@tonic-gate 
32537c478bd9Sstevel@tonic-gate 	if (!found) {
32547c478bd9Sstevel@tonic-gate 		if (bam_verbose)
3255b610f78eSvikram 			bam_error(NOT_IN_MNTTAB, mntpt);
32567c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
32577c478bd9Sstevel@tonic-gate 		return (0);
32587c478bd9Sstevel@tonic-gate 	}
32597c478bd9Sstevel@tonic-gate 
32607c478bd9Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
32617c478bd9Sstevel@tonic-gate 		if (bam_verbose)
32627c478bd9Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
32637c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
32647c478bd9Sstevel@tonic-gate 		return (1);
32657c478bd9Sstevel@tonic-gate 	}
32667c478bd9Sstevel@tonic-gate 
32677c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 	return (0);
32707c478bd9Sstevel@tonic-gate }
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate static int
3273986fd29aSsetje is_boot_archive(char *root)
32747c478bd9Sstevel@tonic-gate {
32757c478bd9Sstevel@tonic-gate 	char		path[PATH_MAX];
32767c478bd9Sstevel@tonic-gate 	struct stat	sb;
3277eb2bd662Svikram 	int		error;
3278eb2bd662Svikram 	const char	*fcn = "is_boot_archive()";
32797c478bd9Sstevel@tonic-gate 
32807c478bd9Sstevel@tonic-gate 	/*
3281986fd29aSsetje 	 * We can't create an archive without the create_ramdisk script
32827c478bd9Sstevel@tonic-gate 	 */
3283986fd29aSsetje 	(void) snprintf(path, sizeof (path), "%s/%s", root, CREATE_RAMDISK);
3284eb2bd662Svikram 	error = stat(path, &sb);
3285eb2bd662Svikram 	INJECT_ERROR1("NOT_ARCHIVE_BASED", error = -1);
3286eb2bd662Svikram 	if (error == -1) {
32877c478bd9Sstevel@tonic-gate 		if (bam_verbose)
32887c478bd9Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
3289eb2bd662Svikram 		BAM_DPRINTF((D_NOT_ARCHIVE_BOOT, fcn, root));
32907c478bd9Sstevel@tonic-gate 		return (0);
32917c478bd9Sstevel@tonic-gate 	}
32927c478bd9Sstevel@tonic-gate 
3293eb2bd662Svikram 	BAM_DPRINTF((D_IS_ARCHIVE_BOOT, fcn, root));
3294986fd29aSsetje 	return (1);
3295986fd29aSsetje }
3296986fd29aSsetje 
32977c478bd9Sstevel@tonic-gate /*
3298986fd29aSsetje  * Need to call this for anything that operates on the GRUB menu
3299963390b4Svikram  * In the x86 live upgrade case the directory /boot/grub may be present
3300963390b4Svikram  * even on pre-newboot BEs. The authoritative way to check for a GRUB target
3301963390b4Svikram  * is to check for the presence of the stage2 binary which is present
3302963390b4Svikram  * only on GRUB targets (even on x86 boot partitions). Checking for the
3303963390b4Svikram  * presence of the multiboot binary is not correct as it is not present
3304963390b4Svikram  * on x86 boot partitions.
3305986fd29aSsetje  */
3306986fd29aSsetje int
3307986fd29aSsetje is_grub(const char *root)
3308986fd29aSsetje {
3309986fd29aSsetje 	char path[PATH_MAX];
3310986fd29aSsetje 	struct stat sb;
3311eb2bd662Svikram 	const char *fcn = "is_grub()";
3312986fd29aSsetje 
3313963390b4Svikram 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_STAGE2);
33147c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
3315eb2bd662Svikram 		BAM_DPRINTF((D_NO_GRUB_DIR, fcn, path));
33167c478bd9Sstevel@tonic-gate 		return (0);
33177c478bd9Sstevel@tonic-gate 	}
33187c478bd9Sstevel@tonic-gate 
33197c478bd9Sstevel@tonic-gate 	return (1);
33207c478bd9Sstevel@tonic-gate }
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate static int
3323eb2bd662Svikram is_zfs(char *root)
3324eb2bd662Svikram {
3325eb2bd662Svikram 	struct statvfs		vfs;
3326eb2bd662Svikram 	int			ret;
3327eb2bd662Svikram 	const char		*fcn = "is_zfs()";
3328eb2bd662Svikram 
3329eb2bd662Svikram 	ret = statvfs(root, &vfs);
3330eb2bd662Svikram 	INJECT_ERROR1("STATVFS_ZFS", ret = 1);
3331eb2bd662Svikram 	if (ret != 0) {
3332eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3333eb2bd662Svikram 		return (0);
3334eb2bd662Svikram 	}
3335eb2bd662Svikram 
3336eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "zfs", strlen("zfs")) == 0) {
3337eb2bd662Svikram 		BAM_DPRINTF((D_IS_ZFS, fcn, root));
3338eb2bd662Svikram 		return (1);
3339eb2bd662Svikram 	} else {
3340eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_ZFS, fcn, root));
3341eb2bd662Svikram 		return (0);
3342eb2bd662Svikram 	}
3343eb2bd662Svikram }
3344eb2bd662Svikram 
3345eb2bd662Svikram static int
3346eb2bd662Svikram is_ufs(char *root)
3347eb2bd662Svikram {
3348eb2bd662Svikram 	struct statvfs		vfs;
3349eb2bd662Svikram 	int			ret;
3350eb2bd662Svikram 	const char		*fcn = "is_ufs()";
3351eb2bd662Svikram 
3352eb2bd662Svikram 	ret = statvfs(root, &vfs);
3353eb2bd662Svikram 	INJECT_ERROR1("STATVFS_UFS", ret = 1);
3354eb2bd662Svikram 	if (ret != 0) {
3355eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3356eb2bd662Svikram 		return (0);
3357eb2bd662Svikram 	}
3358eb2bd662Svikram 
3359eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "ufs", strlen("ufs")) == 0) {
3360eb2bd662Svikram 		BAM_DPRINTF((D_IS_UFS, fcn, root));
3361eb2bd662Svikram 		return (1);
3362eb2bd662Svikram 	} else {
3363eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_UFS, fcn, root));
3364eb2bd662Svikram 		return (0);
3365eb2bd662Svikram 	}
3366eb2bd662Svikram }
3367eb2bd662Svikram 
3368eb2bd662Svikram static int
3369eb2bd662Svikram is_pcfs(char *root)
3370eb2bd662Svikram {
3371eb2bd662Svikram 	struct statvfs		vfs;
3372eb2bd662Svikram 	int			ret;
3373eb2bd662Svikram 	const char		*fcn = "is_pcfs()";
3374eb2bd662Svikram 
3375eb2bd662Svikram 	ret = statvfs(root, &vfs);
3376eb2bd662Svikram 	INJECT_ERROR1("STATVFS_PCFS", ret = 1);
3377eb2bd662Svikram 	if (ret != 0) {
3378eb2bd662Svikram 		bam_error(STATVFS_FAIL, root, strerror(errno));
3379eb2bd662Svikram 		return (0);
3380eb2bd662Svikram 	}
3381eb2bd662Svikram 
3382eb2bd662Svikram 	if (strncmp(vfs.f_basetype, "pcfs", strlen("pcfs")) == 0) {
3383eb2bd662Svikram 		BAM_DPRINTF((D_IS_PCFS, fcn, root));
3384eb2bd662Svikram 		return (1);
3385eb2bd662Svikram 	} else {
3386eb2bd662Svikram 		BAM_DPRINTF((D_IS_NOT_PCFS, fcn, root));
3387eb2bd662Svikram 		return (0);
3388eb2bd662Svikram 	}
3389eb2bd662Svikram }
3390eb2bd662Svikram 
3391eb2bd662Svikram static int
33927c478bd9Sstevel@tonic-gate is_readonly(char *root)
33937c478bd9Sstevel@tonic-gate {
3394eb2bd662Svikram 	int		fd;
3395eb2bd662Svikram 	int		error;
3396eb2bd662Svikram 	char		testfile[PATH_MAX];
3397eb2bd662Svikram 	const char	*fcn = "is_readonly()";
33987c478bd9Sstevel@tonic-gate 
33997c478bd9Sstevel@tonic-gate 	/*
3400eb2bd662Svikram 	 * Using statvfs() to check for a read-only filesystem is not
3401eb2bd662Svikram 	 * reliable. The only way to reliably test is to attempt to
3402eb2bd662Svikram 	 * create a file
34037c478bd9Sstevel@tonic-gate 	 */
3404eb2bd662Svikram 	(void) snprintf(testfile, sizeof (testfile), "%s/%s.%d",
3405eb2bd662Svikram 	    root, BOOTADM_RDONLY_TEST, getpid());
34067c478bd9Sstevel@tonic-gate 
3407eb2bd662Svikram 	(void) unlink(testfile);
3408eb2bd662Svikram 
3409eb2bd662Svikram 	errno = 0;
3410eb2bd662Svikram 	fd = open(testfile, O_RDWR|O_CREAT|O_EXCL, 0644);
3411eb2bd662Svikram 	error = errno;
3412eb2bd662Svikram 	INJECT_ERROR2("RDONLY_TEST_ERROR", fd = -1, error = EACCES);
3413eb2bd662Svikram 	if (fd == -1 && error == EROFS) {
3414eb2bd662Svikram 		BAM_DPRINTF((D_RDONLY_FS, fcn, root));
34157c478bd9Sstevel@tonic-gate 		return (1);
3416eb2bd662Svikram 	} else if (fd == -1) {
3417eb2bd662Svikram 		bam_error(RDONLY_TEST_ERROR, root, strerror(error));
34187c478bd9Sstevel@tonic-gate 	}
34197c478bd9Sstevel@tonic-gate 
3420eb2bd662Svikram 	(void) close(fd);
3421eb2bd662Svikram 	(void) unlink(testfile);
34227c478bd9Sstevel@tonic-gate 
3423eb2bd662Svikram 	BAM_DPRINTF((D_RDWR_FS, fcn, root));
3424e7cbe64fSgw25295 	return (0);
3425e7cbe64fSgw25295 }
3426e7cbe64fSgw25295 
34277c478bd9Sstevel@tonic-gate static error_t
34287c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
34297c478bd9Sstevel@tonic-gate {
34307c478bd9Sstevel@tonic-gate 	error_t ret;
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate 	assert(root);
34337c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
34347c478bd9Sstevel@tonic-gate 
343548847494SEnrico Perla - Sun Microsystems 	init_walk_args();
343648847494SEnrico Perla - Sun Microsystems 	(void) umask(022);
343748847494SEnrico Perla - Sun Microsystems 
34387c478bd9Sstevel@tonic-gate 	/*
3439986fd29aSsetje 	 * root must belong to a boot archive based OS,
34407c478bd9Sstevel@tonic-gate 	 */
3441986fd29aSsetje 	if (!is_boot_archive(root)) {
3442b610f78eSvikram 		/*
3443b610f78eSvikram 		 * Emit message only if not in context of update_all.
3444b610f78eSvikram 		 * If in update_all, emit only if verbose flag is set.
3445b610f78eSvikram 		 */
3446b610f78eSvikram 		if (!bam_update_all || bam_verbose)
3447eb2bd662Svikram 			bam_print(NOT_ARCHIVE_BOOT, root);
344848847494SEnrico Perla - Sun Microsystems 		return (BAM_ERROR);
34497c478bd9Sstevel@tonic-gate 	}
34507c478bd9Sstevel@tonic-gate 
34517c478bd9Sstevel@tonic-gate 	/*
34528c1b6884Sszhou 	 * If smf check is requested when / is writable (can happen
34538c1b6884Sszhou 	 * on first reboot following an upgrade because service
34548c1b6884Sszhou 	 * dependency is messed up), skip the check.
34558c1b6884Sszhou 	 */
345648847494SEnrico Perla - Sun Microsystems 	if (bam_smf_check && !bam_root_readonly && !is_zfs(root))
34578c1b6884Sszhou 		return (BAM_SUCCESS);
34588c1b6884Sszhou 
34598c1b6884Sszhou 	/*
34605eea6091SEnrico Perla - Sun Microsystems 	 * Don't generate archive on ramdisk.
34615eea6091SEnrico Perla - Sun Microsystems 	 */
34625eea6091SEnrico Perla - Sun Microsystems 	if (is_ramdisk(root))
34635eea6091SEnrico Perla - Sun Microsystems 		return (BAM_SUCCESS);
34645eea6091SEnrico Perla - Sun Microsystems 
34655eea6091SEnrico Perla - Sun Microsystems 	/*
34668c1b6884Sszhou 	 * root must be writable. This check applies to alternate
34678c1b6884Sszhou 	 * root (-R option); bam_root_readonly applies to '/' only.
346848847494SEnrico Perla - Sun Microsystems 	 * The behaviour translates into being the one of a 'check'.
34697c478bd9Sstevel@tonic-gate 	 */
347061cb17bdSsetje 	if (!bam_smf_check && !bam_check && is_readonly(root)) {
347148847494SEnrico Perla - Sun Microsystems 		set_flag(RDONLY_FSCHK);
347248847494SEnrico Perla - Sun Microsystems 		bam_check = 1;
34737c478bd9Sstevel@tonic-gate 	}
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate 	/*
3476cedc7e57SEnrico Perla - Sun Microsystems 	 * Now check if an update is really needed.
34777c478bd9Sstevel@tonic-gate 	 */
34787c478bd9Sstevel@tonic-gate 	ret = update_required(root);
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 	/*
3481cedc7e57SEnrico Perla - Sun Microsystems 	 * The check command (-n) is *not* a dry run.
34827c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
3483cedc7e57SEnrico Perla - Sun Microsystems 	 * A readonly filesystem has to be considered an error only if an update
3484cedc7e57SEnrico Perla - Sun Microsystems 	 * is required.
34857c478bd9Sstevel@tonic-gate 	 */
3486cedc7e57SEnrico Perla - Sun Microsystems 	if (bam_nowrite()) {
348748847494SEnrico Perla - Sun Microsystems 		if (is_flag_on(RDONLY_FSCHK)) {
34885eea6091SEnrico Perla - Sun Microsystems 			bam_check = bam_saved_check;
348948847494SEnrico Perla - Sun Microsystems 			if (ret > 0)
349048847494SEnrico Perla - Sun Microsystems 				bam_error(RDONLY_FS, root);
349148847494SEnrico Perla - Sun Microsystems 			if (bam_update_all)
349248847494SEnrico Perla - Sun Microsystems 				return ((ret != 0) ? BAM_ERROR : BAM_SUCCESS);
349348847494SEnrico Perla - Sun Microsystems 		}
349448847494SEnrico Perla - Sun Microsystems 
34957c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
34967c478bd9Sstevel@tonic-gate 	}
34977c478bd9Sstevel@tonic-gate 
34987c478bd9Sstevel@tonic-gate 	if (ret == 1) {
34997c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
35007c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
35017c478bd9Sstevel@tonic-gate 	}
3502986fd29aSsetje 
3503*4a9df875SEnrico Perla - Sun Microsystems 	/*
3504*4a9df875SEnrico Perla - Sun Microsystems 	 * if the archive is updated, save the new stat data and update the
3505*4a9df875SEnrico Perla - Sun Microsystems 	 * timestamp file
3506*4a9df875SEnrico Perla - Sun Microsystems 	 */
3507986fd29aSsetje 	if (ret == 0 && walk_arg.new_nvlp != NULL) {
3508986fd29aSsetje 		savenew(root);
3509*4a9df875SEnrico Perla - Sun Microsystems 		update_timestamp(root);
3510986fd29aSsetje 	}
3511986fd29aSsetje 
3512986fd29aSsetje 	clear_walk_args();
3513986fd29aSsetje 
35147c478bd9Sstevel@tonic-gate 	return (ret);
35157c478bd9Sstevel@tonic-gate }
35167c478bd9Sstevel@tonic-gate 
3517963390b4Svikram static error_t
3518963390b4Svikram synchronize_BE_menu(void)
3519fbac2b2bSvikram {
3520fbac2b2bSvikram 	struct stat	sb;
3521963390b4Svikram 	char		cmdline[PATH_MAX];
3522963390b4Svikram 	char		cksum_line[PATH_MAX];
3523963390b4Svikram 	filelist_t	flist = {0};
3524963390b4Svikram 	char		*old_cksum_str;
3525963390b4Svikram 	char		*old_size_str;
3526963390b4Svikram 	char		*old_file;
3527963390b4Svikram 	char		*curr_cksum_str;
3528963390b4Svikram 	char		*curr_size_str;
3529963390b4Svikram 	char		*curr_file;
3530963390b4Svikram 	FILE		*cfp;
3531963390b4Svikram 	int		found;
3532963390b4Svikram 	int		ret;
3533963390b4Svikram 	const char	*fcn = "synchronize_BE_menu()";
3534fbac2b2bSvikram 
3535963390b4Svikram 	BAM_DPRINTF((D_FUNC_ENTRY0, fcn));
3536fbac2b2bSvikram 
3537963390b4Svikram 	/* Check if findroot enabled LU BE */
3538963390b4Svikram 	if (stat(FINDROOT_INSTALLGRUB, &sb) != 0) {
3539963390b4Svikram 		BAM_DPRINTF((D_NOT_LU_BE, fcn));
3540963390b4Svikram 		return (BAM_SUCCESS);
3541fbac2b2bSvikram 	}
3542fbac2b2bSvikram 
3543963390b4Svikram 	if (stat(LU_MENU_CKSUM, &sb) != 0) {
3544963390b4Svikram 		BAM_DPRINTF((D_NO_CKSUM_FILE, fcn, LU_MENU_CKSUM));
3545963390b4Svikram 		goto menu_sync;
3546fbac2b2bSvikram 	}
3547963390b4Svikram 
3548963390b4Svikram 	cfp = fopen(LU_MENU_CKSUM, "r");
3549963390b4Svikram 	INJECT_ERROR1("CKSUM_FILE_MISSING", cfp = NULL);
3550963390b4Svikram 	if (cfp == NULL) {
3551963390b4Svikram 		bam_error(CANNOT_READ_LU_CKSUM, LU_MENU_CKSUM);
3552963390b4Svikram 		goto menu_sync;
3553963390b4Svikram 	}
3554963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_OPENED, fcn, LU_MENU_CKSUM));
3555963390b4Svikram 
3556963390b4Svikram 	found = 0;
3557963390b4Svikram 	while (s_fgets(cksum_line, sizeof (cksum_line), cfp) != NULL) {
3558963390b4Svikram 		INJECT_ERROR1("MULTIPLE_CKSUM", found = 1);
3559963390b4Svikram 		if (found) {
3560963390b4Svikram 			bam_error(MULTIPLE_LU_CKSUM, LU_MENU_CKSUM);
3561963390b4Svikram 			(void) fclose(cfp);
3562963390b4Svikram 			goto menu_sync;
3563963390b4Svikram 		}
3564963390b4Svikram 		found = 1;
3565963390b4Svikram 	}
3566963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_READ, fcn, LU_MENU_CKSUM));
3567963390b4Svikram 
3568963390b4Svikram 
3569963390b4Svikram 	old_cksum_str = strtok(cksum_line, " \t");
3570963390b4Svikram 	old_size_str = strtok(NULL, " \t");
3571963390b4Svikram 	old_file = strtok(NULL, " \t");
3572963390b4Svikram 
3573963390b4Svikram 	INJECT_ERROR1("OLD_CKSUM_NULL", old_cksum_str = NULL);
3574963390b4Svikram 	INJECT_ERROR1("OLD_SIZE_NULL", old_size_str = NULL);
3575963390b4Svikram 	INJECT_ERROR1("OLD_FILE_NULL", old_file = NULL);
3576963390b4Svikram 	if (old_cksum_str == NULL || old_size_str == NULL || old_file == NULL) {
3577963390b4Svikram 		bam_error(CANNOT_PARSE_LU_CKSUM, LU_MENU_CKSUM);
3578963390b4Svikram 		goto menu_sync;
3579963390b4Svikram 	}
3580963390b4Svikram 	BAM_DPRINTF((D_CKSUM_FILE_PARSED, fcn, LU_MENU_CKSUM));
3581963390b4Svikram 
3582963390b4Svikram 	/* Get checksum of current menu */
3583963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s",
3584963390b4Svikram 	    CKSUM, GRUB_MENU);
3585963390b4Svikram 	ret = exec_cmd(cmdline, &flist);
3586963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM", ret = 1);
3587963390b4Svikram 	if (ret != 0) {
3588963390b4Svikram 		bam_error(MENU_CKSUM_FAIL);
3589963390b4Svikram 		return (BAM_ERROR);
3590963390b4Svikram 	}
3591963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_SUCCESS, fcn));
3592963390b4Svikram 
3593963390b4Svikram 	INJECT_ERROR1("GET_CURR_CKSUM_OUTPUT", flist.head = NULL);
3594963390b4Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
3595963390b4Svikram 		bam_error(BAD_CKSUM);
3596963390b4Svikram 		filelist_free(&flist);
3597963390b4Svikram 		return (BAM_ERROR);
3598963390b4Svikram 	}
3599963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_OUTPUT_VALID, fcn));
3600963390b4Svikram 
3601963390b4Svikram 	curr_cksum_str = strtok(flist.head->line, " \t");
3602963390b4Svikram 	curr_size_str = strtok(NULL, " \t");
3603963390b4Svikram 	curr_file = strtok(NULL, " \t");
3604963390b4Svikram 
3605963390b4Svikram 	INJECT_ERROR1("CURR_CKSUM_NULL", curr_cksum_str = NULL);
3606963390b4Svikram 	INJECT_ERROR1("CURR_SIZE_NULL", curr_size_str = NULL);
3607963390b4Svikram 	INJECT_ERROR1("CURR_FILE_NULL", curr_file = NULL);
3608963390b4Svikram 	if (curr_cksum_str == NULL || curr_size_str == NULL ||
3609963390b4Svikram 	    curr_file == NULL) {
3610963390b4Svikram 		bam_error(BAD_CKSUM_PARSE);
3611963390b4Svikram 		filelist_free(&flist);
3612963390b4Svikram 		return (BAM_ERROR);
3613963390b4Svikram 	}
3614963390b4Svikram 	BAM_DPRINTF((D_CKSUM_GEN_PARSED, fcn));
3615963390b4Svikram 
3616963390b4Svikram 	if (strcmp(old_cksum_str, curr_cksum_str) == 0 &&
3617963390b4Svikram 	    strcmp(old_size_str, curr_size_str) == 0 &&
3618963390b4Svikram 	    strcmp(old_file, curr_file) == 0) {
3619963390b4Svikram 		filelist_free(&flist);
3620963390b4Svikram 		BAM_DPRINTF((D_CKSUM_NO_CHANGE, fcn));
3621963390b4Svikram 		return (BAM_SUCCESS);
3622963390b4Svikram 	}
3623963390b4Svikram 
3624963390b4Svikram 	filelist_free(&flist);
3625963390b4Svikram 
3626963390b4Svikram 	/* cksum doesn't match - the menu has changed */
3627963390b4Svikram 	BAM_DPRINTF((D_CKSUM_HAS_CHANGED, fcn));
3628963390b4Svikram 
3629963390b4Svikram menu_sync:
3630963390b4Svikram 	bam_print(PROP_GRUB_MENU);
3631963390b4Svikram 
3632963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
3633a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s yes > /dev/null'",
3634963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_MENU);
3635963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3636963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU", ret = 1);
3637963390b4Svikram 	if (ret != 0) {
3638963390b4Svikram 		bam_error(MENU_PROP_FAIL);
3639963390b4Svikram 		return (BAM_ERROR);
3640963390b4Svikram 	}
3641963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_MENU, fcn));
3642963390b4Svikram 
3643a6968364Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "/bin/cp %s %s > /dev/null",
3644963390b4Svikram 	    GRUB_MENU, GRUB_BACKUP_MENU);
3645963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3646963390b4Svikram 	INJECT_ERROR1("CREATE_BACKUP", ret = 1);
3647963390b4Svikram 	if (ret != 0) {
3648963390b4Svikram 		bam_error(MENU_BACKUP_FAIL, GRUB_BACKUP_MENU);
3649963390b4Svikram 		return (BAM_ERROR);
3650963390b4Svikram 	}
3651963390b4Svikram 	BAM_DPRINTF((D_CREATED_BACKUP, fcn, GRUB_BACKUP_MENU));
3652963390b4Svikram 
3653963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
3654a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
3655963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, GRUB_BACKUP_MENU);
3656963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3657963390b4Svikram 	INJECT_ERROR1("PROPAGATE_BACKUP", ret = 1);
3658963390b4Svikram 	if (ret != 0) {
3659963390b4Svikram 		bam_error(BACKUP_PROP_FAIL, GRUB_BACKUP_MENU);
3660963390b4Svikram 		return (BAM_ERROR);
3661963390b4Svikram 	}
3662963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_BACKUP, fcn, GRUB_BACKUP_MENU));
3663963390b4Svikram 
3664963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline), "%s %s > %s",
3665963390b4Svikram 	    CKSUM, GRUB_MENU, LU_MENU_CKSUM);
3666963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3667963390b4Svikram 	INJECT_ERROR1("CREATE_CKSUM_FILE", ret = 1);
3668963390b4Svikram 	if (ret != 0) {
3669963390b4Svikram 		bam_error(MENU_CKSUM_WRITE_FAIL, LU_MENU_CKSUM);
3670963390b4Svikram 		return (BAM_ERROR);
3671963390b4Svikram 	}
3672963390b4Svikram 	BAM_DPRINTF((D_CREATED_CKSUM_FILE, fcn, LU_MENU_CKSUM));
3673963390b4Svikram 
3674963390b4Svikram 	(void) snprintf(cmdline, sizeof (cmdline),
3675a6968364Svikram 	    "/bin/sh -c '. %s > /dev/null; %s %s no > /dev/null'",
3676963390b4Svikram 	    LULIB, LULIB_PROPAGATE_FILE, LU_MENU_CKSUM);
3677963390b4Svikram 	ret = exec_cmd(cmdline, NULL);
3678963390b4Svikram 	INJECT_ERROR1("PROPAGATE_MENU_CKSUM_FILE", ret = 1);
3679963390b4Svikram 	if (ret != 0) {
3680963390b4Svikram 		bam_error(MENU_CKSUM_PROP_FAIL, LU_MENU_CKSUM);
3681963390b4Svikram 		return (BAM_ERROR);
3682963390b4Svikram 	}
3683963390b4Svikram 	BAM_DPRINTF((D_PROPAGATED_CKSUM_FILE, fcn, LU_MENU_CKSUM));
3684963390b4Svikram 
3685963390b4Svikram 	return (BAM_SUCCESS);
3686fbac2b2bSvikram }
3687fbac2b2bSvikram 
36887c478bd9Sstevel@tonic-gate static error_t
36897c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
36907c478bd9Sstevel@tonic-gate {
36917c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
36927c478bd9Sstevel@tonic-gate 	struct stat sb;
36937c478bd9Sstevel@tonic-gate 	FILE *fp;
36947c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
3695986fd29aSsetje 	char creatram[PATH_MAX];
36967c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
36977c478bd9Sstevel@tonic-gate 
369840541d5dSvikram 	assert(root);
36997c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
37007c478bd9Sstevel@tonic-gate 
370140541d5dSvikram 	if (bam_rootlen != 1 || *root != '/') {
370240541d5dSvikram 		elide_trailing_slash(root, multibt, sizeof (multibt));
370340541d5dSvikram 		bam_error(ALT_ROOT_INVALID, multibt);
370440541d5dSvikram 		return (BAM_ERROR);
370540541d5dSvikram 	}
370640541d5dSvikram 
37077c478bd9Sstevel@tonic-gate 	/*
370898892a30Snadkarni 	 * Check to see if we are in the midst of safemode patching
370998892a30Snadkarni 	 * If so skip building the archive for /. Instead build it
371098892a30Snadkarni 	 * against the latest bits obtained by creating a fresh lofs
371198892a30Snadkarni 	 * mount of root.
371298892a30Snadkarni 	 */
371398892a30Snadkarni 	if (stat(LOFS_PATCH_FILE, &sb) == 0)  {
371448847494SEnrico Perla - Sun Microsystems 		if (mkdir(LOFS_PATCH_MNT, DIR_PERMS) == -1 &&
371598892a30Snadkarni 		    errno != EEXIST) {
371698892a30Snadkarni 			bam_error(MKDIR_FAILED, "%s", LOFS_PATCH_MNT,
371798892a30Snadkarni 			    strerror(errno));
371898892a30Snadkarni 			ret = BAM_ERROR;
371998892a30Snadkarni 			goto out;
372098892a30Snadkarni 		}
372198892a30Snadkarni 		(void) snprintf(multibt, sizeof (multibt),
372298892a30Snadkarni 		    "/sbin/mount -F lofs -o nosub /  %s", LOFS_PATCH_MNT);
3723986fd29aSsetje 		if (exec_cmd(multibt, NULL) != 0) {
372498892a30Snadkarni 			bam_error(MOUNT_FAILED, LOFS_PATCH_MNT, "lofs");
372598892a30Snadkarni 			ret = BAM_ERROR;
372698892a30Snadkarni 		}
372798892a30Snadkarni 		if (ret != BAM_ERROR) {
372898892a30Snadkarni 			(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
372998892a30Snadkarni 			    LOFS_PATCH_MNT);
373098892a30Snadkarni 			bam_rootlen = strlen(rootbuf);
373198892a30Snadkarni 			if (update_archive(rootbuf, opt) != BAM_SUCCESS)
373298892a30Snadkarni 				ret = BAM_ERROR;
373395b1e0e9Snadkarni 			/*
373495b1e0e9Snadkarni 			 * unmount the lofs mount since there could be
373595b1e0e9Snadkarni 			 * multiple invocations of bootadm -a update_all
373695b1e0e9Snadkarni 			 */
373795b1e0e9Snadkarni 			(void) snprintf(multibt, sizeof (multibt),
373895b1e0e9Snadkarni 			    "/sbin/umount %s", LOFS_PATCH_MNT);
3739986fd29aSsetje 			if (exec_cmd(multibt, NULL) != 0) {
374095b1e0e9Snadkarni 				bam_error(UMOUNT_FAILED, LOFS_PATCH_MNT);
374195b1e0e9Snadkarni 				ret = BAM_ERROR;
374295b1e0e9Snadkarni 			}
374398892a30Snadkarni 		}
374498892a30Snadkarni 	} else {
374598892a30Snadkarni 		/*
37467c478bd9Sstevel@tonic-gate 		 * First update archive for current root
37477c478bd9Sstevel@tonic-gate 		 */
37487c478bd9Sstevel@tonic-gate 		if (update_archive(root, opt) != BAM_SUCCESS)
37497c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
375098892a30Snadkarni 	}
375198892a30Snadkarni 
375298892a30Snadkarni 	if (ret == BAM_ERROR)
375398892a30Snadkarni 		goto out;
37547c478bd9Sstevel@tonic-gate 
37557c478bd9Sstevel@tonic-gate 	/*
37567c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
37577c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
37587c478bd9Sstevel@tonic-gate 	 */
37597c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
37607c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
37617c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
3762b610f78eSvikram 		ret = BAM_ERROR;
3763b610f78eSvikram 		goto out;
37647c478bd9Sstevel@tonic-gate 	}
37657c478bd9Sstevel@tonic-gate 
37667c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
37677c478bd9Sstevel@tonic-gate 
37687c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
37697c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
37707c478bd9Sstevel@tonic-gate 			continue;
3771f904d32dSJerry Gilliam 		if ((strcmp(mnt.mnt_fstype, MNTTYPE_ZFS) != 0) &&
3772f904d32dSJerry Gilliam 		    (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0))
37737c478bd9Sstevel@tonic-gate 			continue;
37747c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
37757c478bd9Sstevel@tonic-gate 			continue;
37767c478bd9Sstevel@tonic-gate 
3777986fd29aSsetje 		(void) snprintf(creatram, sizeof (creatram), "%s/%s",
3778986fd29aSsetje 		    mnt.mnt_mountp, CREATE_RAMDISK);
37797c478bd9Sstevel@tonic-gate 
3780986fd29aSsetje 		if (stat(creatram, &sb) == -1)
37817c478bd9Sstevel@tonic-gate 			continue;
37827c478bd9Sstevel@tonic-gate 
37837c478bd9Sstevel@tonic-gate 		/*
37847c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
37857c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
37867c478bd9Sstevel@tonic-gate 		 */
37877c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
37887c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
37897c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
3790ae115bc7Smrj 
3791ae115bc7Smrj 		/*
3792ae115bc7Smrj 		 * It's possible that other mounts may be an alternate boot
3793ae115bc7Smrj 		 * architecture, so check it again.
3794ae115bc7Smrj 		 */
3795eb2bd662Svikram 		if ((get_boot_cap(rootbuf) != BAM_SUCCESS) ||
3796ae115bc7Smrj 		    (update_archive(rootbuf, opt) != BAM_SUCCESS))
37977c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
37987c478bd9Sstevel@tonic-gate 	}
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
38017c478bd9Sstevel@tonic-gate 
3802b610f78eSvikram out:
3803fbac2b2bSvikram 	/*
3804963390b4Svikram 	 * We no longer use biosdev for Live Upgrade. Hence
3805963390b4Svikram 	 * there is no need to defer (to shutdown time) any fdisk
3806963390b4Svikram 	 * updates
3807fbac2b2bSvikram 	 */
3808963390b4Svikram 	if (stat(GRUB_fdisk, &sb) == 0 || stat(GRUB_fdisk_target, &sb) == 0) {
3809963390b4Svikram 		bam_error(FDISK_FILES_FOUND, GRUB_fdisk, GRUB_fdisk_target);
3810fbac2b2bSvikram 	}
3811fbac2b2bSvikram 
3812963390b4Svikram 	/*
3813963390b4Svikram 	 * If user has updated menu in current BE, propagate the
3814963390b4Svikram 	 * updates to all BEs.
3815963390b4Svikram 	 */
381619397407SSherry Moore 	if (sync_menu && synchronize_BE_menu() != BAM_SUCCESS)
3817963390b4Svikram 		ret = BAM_ERROR;
3818963390b4Svikram 
38197c478bd9Sstevel@tonic-gate 	return (ret);
38207c478bd9Sstevel@tonic-gate }
38217c478bd9Sstevel@tonic-gate 
38227c478bd9Sstevel@tonic-gate static void
38237c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
38247c478bd9Sstevel@tonic-gate {
38257c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
38267c478bd9Sstevel@tonic-gate 		mp->start = lp;
38277c478bd9Sstevel@tonic-gate 	} else {
38287c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
38298c1b6884Sszhou 		lp->prev = mp->end;
38307c478bd9Sstevel@tonic-gate 	}
38317c478bd9Sstevel@tonic-gate 	mp->end = lp;
38327c478bd9Sstevel@tonic-gate }
38337c478bd9Sstevel@tonic-gate 
3834eb2bd662Svikram void
38358c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp)
38368c1b6884Sszhou {
38378c1b6884Sszhou 	/* unlink from list */
38388c1b6884Sszhou 	if (lp->prev)
38398c1b6884Sszhou 		lp->prev->next = lp->next;
38408c1b6884Sszhou 	else
38418c1b6884Sszhou 		mp->start = lp->next;
38428c1b6884Sszhou 	if (lp->next)
38438c1b6884Sszhou 		lp->next->prev = lp->prev;
38448c1b6884Sszhou 	else
38458c1b6884Sszhou 		mp->end = lp->prev;
38468c1b6884Sszhou }
38478c1b6884Sszhou 
38488c1b6884Sszhou static entry_t *
38498c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end)
38508c1b6884Sszhou {
38518c1b6884Sszhou 	entry_t *ent, *prev;
3852eb2bd662Svikram 	const char *fcn = "boot_entry_new()";
3853eb2bd662Svikram 
3854eb2bd662Svikram 	assert(mp);
3855eb2bd662Svikram 	assert(start);
3856eb2bd662Svikram 	assert(end);
38578c1b6884Sszhou 
38588c1b6884Sszhou 	ent = s_calloc(1, sizeof (entry_t));
3859eb2bd662Svikram 	BAM_DPRINTF((D_ENTRY_NEW, fcn));
38608c1b6884Sszhou 	ent->start = start;
38618c1b6884Sszhou 	ent->end = end;
38628c1b6884Sszhou 
38638c1b6884Sszhou 	if (mp->entries == NULL) {
38648c1b6884Sszhou 		mp->entries = ent;
3865eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_NEW_FIRST, fcn));
38668c1b6884Sszhou 		return (ent);
38678c1b6884Sszhou 	}
38688c1b6884Sszhou 
38698c1b6884Sszhou 	prev = mp->entries;
38708c1b6884Sszhou 	while (prev->next)
38718c1b6884Sszhou 		prev = prev->next;
38728c1b6884Sszhou 	prev->next = ent;
38738c1b6884Sszhou 	ent->prev = prev;
3874eb2bd662Svikram 	BAM_DPRINTF((D_ENTRY_NEW_LINKED, fcn));
38758c1b6884Sszhou 	return (ent);
38768c1b6884Sszhou }
38778c1b6884Sszhou 
38788c1b6884Sszhou static void
38798c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp)
38808c1b6884Sszhou {
38818c1b6884Sszhou 	if (ent)
38828c1b6884Sszhou 		ent->end = lp;
38838c1b6884Sszhou }
38848c1b6884Sszhou 
38857c478bd9Sstevel@tonic-gate /*
3886843e1988Sjohnlev  * Check whether cmd matches the one indexed by which, and whether arg matches
3887843e1988Sjohnlev  * str.  which must be either KERNEL_CMD or MODULE_CMD, and a match to the
3888843e1988Sjohnlev  * respective *_DOLLAR_CMD is also acceptable.  The arg is searched using
3889843e1988Sjohnlev  * strstr(), so it can be a partial match.
3890843e1988Sjohnlev  */
3891843e1988Sjohnlev static int
3892843e1988Sjohnlev check_cmd(const char *cmd, const int which, const char *arg, const char *str)
3893843e1988Sjohnlev {
3894eb2bd662Svikram 	int			ret;
3895eb2bd662Svikram 	const char		*fcn = "check_cmd()";
3896eb2bd662Svikram 
3897eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, arg, str));
3898eb2bd662Svikram 
3899843e1988Sjohnlev 	if ((strcmp(cmd, menu_cmds[which]) != 0) &&
3900843e1988Sjohnlev 	    (strcmp(cmd, menu_cmds[which + 1]) != 0)) {
3901eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_CMD_CMD_NOMATCH,
3902eb2bd662Svikram 		    fcn, cmd, menu_cmds[which]));
3903843e1988Sjohnlev 		return (0);
3904843e1988Sjohnlev 	}
3905eb2bd662Svikram 	ret = (strstr(arg, str) != NULL);
3906eb2bd662Svikram 
3907eb2bd662Svikram 	if (ret) {
3908eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
3909eb2bd662Svikram 	} else {
3910eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
3911eb2bd662Svikram 	}
3912eb2bd662Svikram 
3913eb2bd662Svikram 	return (ret);
3914eb2bd662Svikram }
3915eb2bd662Svikram 
3916eb2bd662Svikram static error_t
3917eb2bd662Svikram kernel_parser(entry_t *entry, char *cmd, char *arg, int linenum)
3918eb2bd662Svikram {
3919eb2bd662Svikram 	const char		*fcn  = "kernel_parser()";
3920eb2bd662Svikram 
3921eb2bd662Svikram 	assert(entry);
3922eb2bd662Svikram 	assert(cmd);
3923eb2bd662Svikram 	assert(arg);
3924eb2bd662Svikram 
3925eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[KERNEL_CMD]) != 0 &&
3926eb2bd662Svikram 	    strcmp(cmd, menu_cmds[KERNEL_DOLLAR_CMD]) != 0) {
3927eb2bd662Svikram 		BAM_DPRINTF((D_NOT_KERNEL_CMD, fcn, cmd));
3928eb2bd662Svikram 		return (BAM_ERROR);
3929eb2bd662Svikram 	}
3930eb2bd662Svikram 
3931eb2bd662Svikram 	if (strncmp(arg, DIRECT_BOOT_32, sizeof (DIRECT_BOOT_32) - 1) == 0) {
3932eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_32, fcn, arg));
3933eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
3934eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_KERNEL,
3935eb2bd662Svikram 	    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0) {
3936eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT, fcn, arg));
3937eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT;
3938eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_64,
3939eb2bd662Svikram 	    sizeof (DIRECT_BOOT_64) - 1) == 0) {
3940eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_64, fcn, arg));
3941eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
3942eb2bd662Svikram 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_KERNEL,
3943eb2bd662Svikram 	    sizeof (DIRECT_BOOT_FAILSAFE_KERNEL) - 1) == 0) {
3944eb2bd662Svikram 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE, fcn, arg));
3945eb2bd662Svikram 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE;
3946bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_32,
3947bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_32) - 1) == 0) {
3948bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE_32, fcn, arg));
3949bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
3950bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_32BIT;
3951bbcc54bdSEnrico Perla - Sun Microsystems 	} else if (strncmp(arg, DIRECT_BOOT_FAILSAFE_64,
3952bbcc54bdSEnrico Perla - Sun Microsystems 	    sizeof (DIRECT_BOOT_FAILSAFE_64) - 1) == 0) {
3953bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_SET_DBOOT_FAILSAFE_64, fcn, arg));
3954bbcc54bdSEnrico Perla - Sun Microsystems 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_FAILSAFE
3955bbcc54bdSEnrico Perla - Sun Microsystems 		    | BAM_ENTRY_64BIT;
3956eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT, sizeof (MULTI_BOOT) - 1) == 0) {
3957eb2bd662Svikram 		BAM_DPRINTF((D_SET_MULTIBOOT, fcn, arg));
3958eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT;
3959eb2bd662Svikram 	} else if (strncmp(arg, MULTI_BOOT_FAILSAFE,
3960eb2bd662Svikram 	    sizeof (MULTI_BOOT_FAILSAFE) - 1) == 0) {
3961eb2bd662Svikram 		BAM_DPRINTF((D_SET_MULTIBOOT_FAILSAFE, fcn, arg));
3962eb2bd662Svikram 		entry->flags |= BAM_ENTRY_MULTIBOOT | BAM_ENTRY_FAILSAFE;
3963eb2bd662Svikram 	} else if (strstr(arg, XEN_KERNEL_SUBSTR)) {
3964eb2bd662Svikram 		BAM_DPRINTF((D_SET_HV, fcn, arg));
3965eb2bd662Svikram 		entry->flags |= BAM_ENTRY_HV;
3966eb2bd662Svikram 	} else if (!(entry->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU))) {
3967eb2bd662Svikram 		BAM_DPRINTF((D_SET_HAND_KERNEL, fcn, arg));
3968eb2bd662Svikram 		return (BAM_ERROR);
396937eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
397037eb779cSVikram Hegde 	    strstr(arg, UNIX_SPACE)) {
397137eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_32BIT;
397237eb779cSVikram Hegde 	} else if (strncmp(arg, KERNEL_PREFIX, strlen(KERNEL_PREFIX)) == 0 &&
397337eb779cSVikram Hegde 	    strstr(arg, AMD_UNIX_SPACE)) {
397437eb779cSVikram Hegde 		entry->flags |= BAM_ENTRY_DBOOT | BAM_ENTRY_64BIT;
3975eb2bd662Svikram 	} else {
3976eb2bd662Svikram 		BAM_DPRINTF((D_IS_UNKNOWN_KERNEL, fcn, arg));
3977eb2bd662Svikram 		bam_error(UNKNOWN_KERNEL_LINE, linenum);
3978eb2bd662Svikram 		return (BAM_ERROR);
3979eb2bd662Svikram 	}
3980eb2bd662Svikram 
3981eb2bd662Svikram 	return (BAM_SUCCESS);
3982eb2bd662Svikram }
3983eb2bd662Svikram 
3984eb2bd662Svikram static error_t
3985eb2bd662Svikram module_parser(entry_t *entry, char *cmd, char *arg, int linenum)
3986eb2bd662Svikram {
3987eb2bd662Svikram 	const char		*fcn = "module_parser()";
3988eb2bd662Svikram 
3989eb2bd662Svikram 	assert(entry);
3990eb2bd662Svikram 	assert(cmd);
3991eb2bd662Svikram 	assert(arg);
3992eb2bd662Svikram 
3993eb2bd662Svikram 	if (strcmp(cmd, menu_cmds[MODULE_CMD]) != 0 &&
3994eb2bd662Svikram 	    strcmp(cmd, menu_cmds[MODULE_DOLLAR_CMD]) != 0) {
3995eb2bd662Svikram 		BAM_DPRINTF((D_NOT_MODULE_CMD, fcn, cmd));
3996eb2bd662Svikram 		return (BAM_ERROR);
3997eb2bd662Svikram 	}
3998eb2bd662Svikram 
3999eb2bd662Svikram 	if (strcmp(arg, DIRECT_BOOT_ARCHIVE) == 0 ||
4000eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_32) == 0 ||
4001eb2bd662Svikram 	    strcmp(arg, DIRECT_BOOT_ARCHIVE_64) == 0 ||
4002eb2bd662Svikram 	    strcmp(arg, MULTIBOOT_ARCHIVE) == 0 ||
4003eb2bd662Svikram 	    strcmp(arg, FAILSAFE_ARCHIVE) == 0 ||
4004bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_32) == 0 ||
4005bbcc54bdSEnrico Perla - Sun Microsystems 	    strcmp(arg, FAILSAFE_ARCHIVE_64) == 0 ||
4006eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE) == 0 ||
4007eb2bd662Svikram 	    strcmp(arg, XEN_KERNEL_MODULE_LINE_ZFS) == 0) {
4008eb2bd662Svikram 		BAM_DPRINTF((D_BOOTADM_LU_MODULE, fcn, arg));
4009eb2bd662Svikram 		return (BAM_SUCCESS);
4010eb2bd662Svikram 	} else if (!(entry->flags & BAM_ENTRY_BOOTADM) &&
4011eb2bd662Svikram 	    !(entry->flags & BAM_ENTRY_LU)) {
4012eb2bd662Svikram 		/* don't emit warning for hand entries */
4013eb2bd662Svikram 		BAM_DPRINTF((D_IS_HAND_MODULE, fcn, arg));
4014eb2bd662Svikram 		return (BAM_ERROR);
4015eb2bd662Svikram 	} else {
4016eb2bd662Svikram 		BAM_DPRINTF((D_IS_UNKNOWN_MODULE, fcn, arg));
4017eb2bd662Svikram 		bam_error(UNKNOWN_MODULE_LINE, linenum);
4018eb2bd662Svikram 		return (BAM_ERROR);
4019eb2bd662Svikram 	}
4020843e1988Sjohnlev }
4021843e1988Sjohnlev 
4022843e1988Sjohnlev /*
40237c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
40247c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
40257c478bd9Sstevel@tonic-gate  */
40267c478bd9Sstevel@tonic-gate static void
40277c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
40287c478bd9Sstevel@tonic-gate {
40297c478bd9Sstevel@tonic-gate 	/*
40307c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
40317c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
40327c478bd9Sstevel@tonic-gate 	 * been processed
40337c478bd9Sstevel@tonic-gate 	 */
40348c1b6884Sszhou 	static line_t *prev = NULL;
40358c1b6884Sszhou 	static entry_t *curr_ent = NULL;
4036ae115bc7Smrj 	static int in_liveupgrade = 0;
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate 	line_t	*lp;
40397c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
40407c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
40417c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
4042eb2bd662Svikram 	const char *fcn = "line_parser()";
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate 	if (str == NULL) {
40457c478bd9Sstevel@tonic-gate 		return;
40467c478bd9Sstevel@tonic-gate 	}
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate 	/*
40497c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
40507c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
40517c478bd9Sstevel@tonic-gate 	 */
40527c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
40537c478bd9Sstevel@tonic-gate 
40547c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
40557c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
40567c478bd9Sstevel@tonic-gate 		str++;
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
40597c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
40607c478bd9Sstevel@tonic-gate 		sep = NULL;
40617c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
40627c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
4063ae115bc7Smrj 		if (strstr(arg, BAM_LU_HDR) != NULL) {
4064ae115bc7Smrj 			in_liveupgrade = 1;
4065ae115bc7Smrj 		} else if (strstr(arg, BAM_LU_FTR) != NULL) {
4066ae115bc7Smrj 			in_liveupgrade = 0;
4067ae115bc7Smrj 		}
40687c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
40697c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
40707c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
40717c478bd9Sstevel@tonic-gate 	} else {
40727c478bd9Sstevel@tonic-gate 		/*
40737c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
40747c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
40757c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
40767c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
40777c478bd9Sstevel@tonic-gate 		 */
40787c478bd9Sstevel@tonic-gate 		cp = str;
40797c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
40807c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
40817c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
40827c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
40837c478bd9Sstevel@tonic-gate 				break;
40847c478bd9Sstevel@tonic-gate 			}
40857c478bd9Sstevel@tonic-gate 			str++;
40867c478bd9Sstevel@tonic-gate 		}
40877c478bd9Sstevel@tonic-gate 
40887c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
40897c478bd9Sstevel@tonic-gate 			save = *str;
40907c478bd9Sstevel@tonic-gate 			*str = '\0';
40917c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
40927c478bd9Sstevel@tonic-gate 			*str = save;
40937c478bd9Sstevel@tonic-gate 
40947c478bd9Sstevel@tonic-gate 			str++;
40957c478bd9Sstevel@tonic-gate 			save = *str;
40967c478bd9Sstevel@tonic-gate 			*str = '\0';
40977c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
40987c478bd9Sstevel@tonic-gate 			*str = save;
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
41017c478bd9Sstevel@tonic-gate 				str++;
41027c478bd9Sstevel@tonic-gate 			if (*str == '\0')
41037c478bd9Sstevel@tonic-gate 				arg = NULL;
41047c478bd9Sstevel@tonic-gate 			else
41057c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
41067c478bd9Sstevel@tonic-gate 		}
41077c478bd9Sstevel@tonic-gate 	}
41087c478bd9Sstevel@tonic-gate 
41097c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
41107c478bd9Sstevel@tonic-gate 
41117c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
41127c478bd9Sstevel@tonic-gate 	lp->sep = sep;
41137c478bd9Sstevel@tonic-gate 	lp->arg = arg;
41147c478bd9Sstevel@tonic-gate 	lp->line = line;
41157c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
41167c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
41177c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
41187c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
41197c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
4120ae115bc7Smrj 		    prev->arg && strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
41217c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
41228c1b6884Sszhou 			curr_ent = boot_entry_new(mp, prev, lp);
4123eb2bd662Svikram 			curr_ent->flags |= BAM_ENTRY_BOOTADM;
4124eb2bd662Svikram 			BAM_DPRINTF((D_IS_BOOTADM_ENTRY, fcn, arg));
41258c1b6884Sszhou 		} else {
41268c1b6884Sszhou 			curr_ent = boot_entry_new(mp, lp, lp);
4127ae115bc7Smrj 			if (in_liveupgrade) {
4128eb2bd662Svikram 				curr_ent->flags |= BAM_ENTRY_LU;
4129eb2bd662Svikram 				BAM_DPRINTF((D_IS_LU_ENTRY, fcn, arg));
41308c1b6884Sszhou 			}
4131ae115bc7Smrj 		}
4132ae115bc7Smrj 		curr_ent->entryNum = *entryNum;
41337c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
41347c478bd9Sstevel@tonic-gate 		/*
41357c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
41367c478bd9Sstevel@tonic-gate 		 * by the subsequent title
41377c478bd9Sstevel@tonic-gate 		 */
41387c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
41397c478bd9Sstevel@tonic-gate 		lp->flags = flag;
41407c478bd9Sstevel@tonic-gate 	} else {
41417c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
4142ae115bc7Smrj 
4143ae115bc7Smrj 		if (*entryNum == ENTRY_INIT) {
4144ae115bc7Smrj 			lp->flags = BAM_GLOBAL;
4145ae115bc7Smrj 		} else {
4146ae115bc7Smrj 			lp->flags = BAM_ENTRY;
4147ae115bc7Smrj 
4148ae115bc7Smrj 			if (cmd && arg) {
4149eb2bd662Svikram 				if (strcmp(cmd, menu_cmds[ROOT_CMD]) == 0) {
4150eb2bd662Svikram 					BAM_DPRINTF((D_IS_ROOT_CMD, fcn, arg));
4151ae115bc7Smrj 					curr_ent->flags |= BAM_ENTRY_ROOT;
4152eb2bd662Svikram 				} else if (strcmp(cmd, menu_cmds[FINDROOT_CMD])
4153eb2bd662Svikram 				    == 0) {
4154eb2bd662Svikram 					BAM_DPRINTF((D_IS_FINDROOT_CMD, fcn,
4155eb2bd662Svikram 					    arg));
4156eb2bd662Svikram 					curr_ent->flags |= BAM_ENTRY_FINDROOT;
4157eb2bd662Svikram 				} else if (strcmp(cmd,
4158eb2bd662Svikram 				    menu_cmds[CHAINLOADER_CMD]) == 0) {
4159eb2bd662Svikram 					BAM_DPRINTF((D_IS_CHAINLOADER_CMD, fcn,
4160eb2bd662Svikram 					    arg));
4161ae115bc7Smrj 					curr_ent->flags |=
4162ae115bc7Smrj 					    BAM_ENTRY_CHAINLOADER;
4163eb2bd662Svikram 				} else if (kernel_parser(curr_ent, cmd, arg,
4164eb2bd662Svikram 				    lp->lineNum) != BAM_SUCCESS) {
4165eb2bd662Svikram 					(void) module_parser(curr_ent, cmd,
4166eb2bd662Svikram 					    arg, lp->lineNum);
4167eb2bd662Svikram 				}
4168ae115bc7Smrj 			}
4169ae115bc7Smrj 		}
41707c478bd9Sstevel@tonic-gate 	}
41717c478bd9Sstevel@tonic-gate 
41728c1b6884Sszhou 	/* record default, old default, and entry line ranges */
41738c1b6884Sszhou 	if (lp->flags == BAM_GLOBAL &&
41748c1b6884Sszhou 	    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) {
41758c1b6884Sszhou 		mp->curdefault = lp;
41768c1b6884Sszhou 	} else if (lp->flags == BAM_COMMENT &&
41778c1b6884Sszhou 	    strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) {
41788c1b6884Sszhou 		mp->olddefault = lp;
4179ae115bc7Smrj 	} else if (lp->flags == BAM_COMMENT &&
4180ae115bc7Smrj 	    strncmp(lp->arg, BAM_OLD_RC_DEF, strlen(BAM_OLD_RC_DEF)) == 0) {
4181ae115bc7Smrj 		mp->old_rc_default = lp;
41828c1b6884Sszhou 	} else if (lp->flags == BAM_ENTRY ||
4183ae115bc7Smrj 	    (lp->flags == BAM_COMMENT &&
4184ae115bc7Smrj 	    strcmp(lp->arg, BAM_BOOTADM_FTR) == 0)) {
41858c1b6884Sszhou 		boot_entry_addline(curr_ent, lp);
41868c1b6884Sszhou 	}
41877c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
41887c478bd9Sstevel@tonic-gate 
41897c478bd9Sstevel@tonic-gate 	prev = lp;
41907c478bd9Sstevel@tonic-gate }
41917c478bd9Sstevel@tonic-gate 
4192eb2bd662Svikram void
419340541d5dSvikram update_numbering(menu_t *mp)
419440541d5dSvikram {
419540541d5dSvikram 	int lineNum;
419640541d5dSvikram 	int entryNum;
419740541d5dSvikram 	int old_default_value;
419840541d5dSvikram 	line_t *lp, *prev, *default_lp, *default_entry;
419940541d5dSvikram 	char buf[PATH_MAX];
420040541d5dSvikram 
420140541d5dSvikram 	if (mp->start == NULL) {
420240541d5dSvikram 		return;
420340541d5dSvikram 	}
420440541d5dSvikram 
420540541d5dSvikram 	lineNum = LINE_INIT;
420640541d5dSvikram 	entryNum = ENTRY_INIT;
420740541d5dSvikram 	old_default_value = ENTRY_INIT;
420840541d5dSvikram 	lp = default_lp = default_entry = NULL;
420940541d5dSvikram 
421040541d5dSvikram 	prev = NULL;
421140541d5dSvikram 	for (lp = mp->start; lp; prev = lp, lp = lp->next) {
421240541d5dSvikram 		lp->lineNum = ++lineNum;
421340541d5dSvikram 
421440541d5dSvikram 		/*
421540541d5dSvikram 		 * Get the value of the default command
421640541d5dSvikram 		 */
421740541d5dSvikram 		if (lp->entryNum == ENTRY_INIT && lp->cmd &&
421840541d5dSvikram 		    strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 &&
421940541d5dSvikram 		    lp->arg) {
422040541d5dSvikram 			old_default_value = atoi(lp->arg);
422140541d5dSvikram 			default_lp = lp;
422240541d5dSvikram 		}
422340541d5dSvikram 
422440541d5dSvikram 		/*
4225eb2bd662Svikram 		 * If not a booting entry, nothing else to fix for this
422640541d5dSvikram 		 * entry
422740541d5dSvikram 		 */
422840541d5dSvikram 		if (lp->entryNum == ENTRY_INIT)
422940541d5dSvikram 			continue;
423040541d5dSvikram 
423140541d5dSvikram 		/*
423240541d5dSvikram 		 * Record the position of the default entry.
423340541d5dSvikram 		 * The following works because global
423440541d5dSvikram 		 * commands like default and timeout should precede
423540541d5dSvikram 		 * actual boot entries, so old_default_value
423640541d5dSvikram 		 * is already known (or default cmd is missing).
423740541d5dSvikram 		 */
423840541d5dSvikram 		if (default_entry == NULL &&
423940541d5dSvikram 		    old_default_value != ENTRY_INIT &&
424040541d5dSvikram 		    lp->entryNum == old_default_value) {
424140541d5dSvikram 			default_entry = lp;
424240541d5dSvikram 		}
424340541d5dSvikram 
424440541d5dSvikram 		/*
424540541d5dSvikram 		 * Now fixup the entry number
424640541d5dSvikram 		 */
424740541d5dSvikram 		if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) {
424840541d5dSvikram 			lp->entryNum = ++entryNum;
424940541d5dSvikram 			/* fixup the bootadm header */
425040541d5dSvikram 			if (prev && prev->flags == BAM_COMMENT &&
4251ae115bc7Smrj 			    prev->arg &&
4252ae115bc7Smrj 			    strcmp(prev->arg, BAM_BOOTADM_HDR) == 0) {
425340541d5dSvikram 				prev->entryNum = lp->entryNum;
425440541d5dSvikram 			}
425540541d5dSvikram 		} else {
425640541d5dSvikram 			lp->entryNum = entryNum;
425740541d5dSvikram 		}
425840541d5dSvikram 	}
425940541d5dSvikram 
426040541d5dSvikram 	/*
426140541d5dSvikram 	 * No default command in menu, simply return
426240541d5dSvikram 	 */
426340541d5dSvikram 	if (default_lp == NULL) {
426440541d5dSvikram 		return;
426540541d5dSvikram 	}
426640541d5dSvikram 
426740541d5dSvikram 	free(default_lp->arg);
426840541d5dSvikram 	free(default_lp->line);
426940541d5dSvikram 
427040541d5dSvikram 	if (default_entry == NULL) {
427140541d5dSvikram 		default_lp->arg = s_strdup("0");
427240541d5dSvikram 	} else {
427340541d5dSvikram 		(void) snprintf(buf, sizeof (buf), "%d",
427440541d5dSvikram 		    default_entry->entryNum);
427540541d5dSvikram 		default_lp->arg = s_strdup(buf);
427640541d5dSvikram 	}
427740541d5dSvikram 
427840541d5dSvikram 	/*
427940541d5dSvikram 	 * The following is required since only the line field gets
428040541d5dSvikram 	 * written back to menu.lst
428140541d5dSvikram 	 */
428240541d5dSvikram 	(void) snprintf(buf, sizeof (buf), "%s%s%s",
428340541d5dSvikram 	    menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg);
428440541d5dSvikram 	default_lp->line = s_strdup(buf);
428540541d5dSvikram }
428640541d5dSvikram 
428740541d5dSvikram 
42887c478bd9Sstevel@tonic-gate static menu_t *
42897c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
42907c478bd9Sstevel@tonic-gate {
42917c478bd9Sstevel@tonic-gate 	FILE *fp;
42927c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
42937c478bd9Sstevel@tonic-gate 	menu_t *mp;
42947c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
42957c478bd9Sstevel@tonic-gate 
42967c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
42977c478bd9Sstevel@tonic-gate 
42987c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
42997c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
43007c478bd9Sstevel@tonic-gate 		return (mp);
43017c478bd9Sstevel@tonic-gate 	}
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate 
43047c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
43057c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
43067c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
43077c478bd9Sstevel@tonic-gate 	cp = buf;
43087c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
43097c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
43107c478bd9Sstevel@tonic-gate 		n = strlen(cp);
43117c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
43127c478bd9Sstevel@tonic-gate 			len -= n - 1;
43137c478bd9Sstevel@tonic-gate 			assert(len >= 2);
43147c478bd9Sstevel@tonic-gate 			cp += n - 1;
43157c478bd9Sstevel@tonic-gate 			continue;
43167c478bd9Sstevel@tonic-gate 		}
43177c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
43187c478bd9Sstevel@tonic-gate 		cp = buf;
43197c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
43207c478bd9Sstevel@tonic-gate 	}
43217c478bd9Sstevel@tonic-gate 
43227c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
43237c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
43247c478bd9Sstevel@tonic-gate 	}
43257c478bd9Sstevel@tonic-gate 
43267c478bd9Sstevel@tonic-gate 	return (mp);
43277c478bd9Sstevel@tonic-gate }
43287c478bd9Sstevel@tonic-gate 
43297c478bd9Sstevel@tonic-gate static error_t
43307c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
43317c478bd9Sstevel@tonic-gate {
43327c478bd9Sstevel@tonic-gate 	char *eq;
43337c478bd9Sstevel@tonic-gate 	char *opt_dup;
43347c478bd9Sstevel@tonic-gate 	int entryNum;
43357c478bd9Sstevel@tonic-gate 
43367c478bd9Sstevel@tonic-gate 	assert(mp);
43377c478bd9Sstevel@tonic-gate 	assert(mp->start);
43387c478bd9Sstevel@tonic-gate 	assert(opt);
43397c478bd9Sstevel@tonic-gate 
43407c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
43417c478bd9Sstevel@tonic-gate 
43427c478bd9Sstevel@tonic-gate 	if (entry)
43437c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
43447c478bd9Sstevel@tonic-gate 	if (title)
43457c478bd9Sstevel@tonic-gate 		*title = NULL;
43467c478bd9Sstevel@tonic-gate 
43477c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
43487c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
43497c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
43507c478bd9Sstevel@tonic-gate 		free(opt_dup);
43517c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
43527c478bd9Sstevel@tonic-gate 	}
43537c478bd9Sstevel@tonic-gate 
43547c478bd9Sstevel@tonic-gate 	*eq = '\0';
43557c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
43567c478bd9Sstevel@tonic-gate 		assert(mp->end);
43577c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
43587c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
43597c478bd9Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
43607c478bd9Sstevel@tonic-gate 			free(opt_dup);
43617c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
43627c478bd9Sstevel@tonic-gate 		}
43637c478bd9Sstevel@tonic-gate 		*entry = entryNum;
43647c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
43657c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
43667c478bd9Sstevel@tonic-gate 	} else {
43677c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
43687c478bd9Sstevel@tonic-gate 		free(opt_dup);
43697c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
43707c478bd9Sstevel@tonic-gate 	}
43717c478bd9Sstevel@tonic-gate 
43727c478bd9Sstevel@tonic-gate 	free(opt_dup);
43737c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
43747c478bd9Sstevel@tonic-gate }
43757c478bd9Sstevel@tonic-gate 
43767c478bd9Sstevel@tonic-gate /*
43777c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
43787c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
43797c478bd9Sstevel@tonic-gate  *
43807c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
43817c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
43827c478bd9Sstevel@tonic-gate  */
43837c478bd9Sstevel@tonic-gate static error_t
43847c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
43857c478bd9Sstevel@tonic-gate {
43867c478bd9Sstevel@tonic-gate 	line_t *lp;
43877c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
43887c478bd9Sstevel@tonic-gate 	int found;
43897c478bd9Sstevel@tonic-gate 	char *title = NULL;
43907c478bd9Sstevel@tonic-gate 
43917c478bd9Sstevel@tonic-gate 	assert(mp);
43927c478bd9Sstevel@tonic-gate 	assert(menu_path);
43937c478bd9Sstevel@tonic-gate 
4394eb2bd662Svikram 	/* opt is optional */
4395eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, "list_entry", menu_path,
4396eb2bd662Svikram 	    opt ? opt : "<NULL>"));
4397eb2bd662Svikram 
43987c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
43997c478bd9Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
44007c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
44017c478bd9Sstevel@tonic-gate 	}
44027c478bd9Sstevel@tonic-gate 
44037c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
44047c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
44057c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
44067c478bd9Sstevel@tonic-gate 		}
44077c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
44087c478bd9Sstevel@tonic-gate 	} else {
44097c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
44107c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
44117c478bd9Sstevel@tonic-gate 	}
44127c478bd9Sstevel@tonic-gate 
44137c478bd9Sstevel@tonic-gate 	found = 0;
44147c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
44157c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
44167c478bd9Sstevel@tonic-gate 			continue;
44177c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
44187c478bd9Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
44197c478bd9Sstevel@tonic-gate 			    lp->arg);
44207c478bd9Sstevel@tonic-gate 			found = 1;
44217c478bd9Sstevel@tonic-gate 			continue;
44227c478bd9Sstevel@tonic-gate 		}
44237c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
44247c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
44257c478bd9Sstevel@tonic-gate 			found = 1;
44267c478bd9Sstevel@tonic-gate 			continue;
44277c478bd9Sstevel@tonic-gate 		}
44287c478bd9Sstevel@tonic-gate 
44297c478bd9Sstevel@tonic-gate 		/*
44307c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
44317c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
44327c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
44337c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
44347c478bd9Sstevel@tonic-gate 		 */
44357c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
44367c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
44377c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
44387c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
44397c478bd9Sstevel@tonic-gate 			found = 1;
44407c478bd9Sstevel@tonic-gate 			continue;
44417c478bd9Sstevel@tonic-gate 		}
44427c478bd9Sstevel@tonic-gate 	}
44437c478bd9Sstevel@tonic-gate 
44447c478bd9Sstevel@tonic-gate 	if (!found) {
44457c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
44467c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
44477c478bd9Sstevel@tonic-gate 	}
44487c478bd9Sstevel@tonic-gate 
44497c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
44507c478bd9Sstevel@tonic-gate }
44517c478bd9Sstevel@tonic-gate 
4452843e1988Sjohnlev int
44537c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
44547c478bd9Sstevel@tonic-gate 	char *title,
4455eb2bd662Svikram 	char *findroot,
44567c478bd9Sstevel@tonic-gate 	char *kernel,
4457843e1988Sjohnlev 	char *mod_kernel,
44587c478bd9Sstevel@tonic-gate 	char *module)
44597c478bd9Sstevel@tonic-gate {
4460eb2bd662Svikram 	int		lineNum;
4461eb2bd662Svikram 	int		entryNum;
44627c478bd9Sstevel@tonic-gate 	char		linebuf[BAM_MAXLINE];
4463eb2bd662Svikram 	menu_cmd_t	k_cmd;
4464eb2bd662Svikram 	menu_cmd_t	m_cmd;
4465eb2bd662Svikram 	const char	*fcn = "add_boot_entry()";
44667c478bd9Sstevel@tonic-gate 
44677c478bd9Sstevel@tonic-gate 	assert(mp);
44687c478bd9Sstevel@tonic-gate 
4469eb2bd662Svikram 	INJECT_ERROR1("ADD_BOOT_ENTRY_FINDROOT_NULL", findroot = NULL);
4470eb2bd662Svikram 	if (findroot == NULL) {
4471eb2bd662Svikram 		bam_error(NULL_FINDROOT);
4472eb2bd662Svikram 		return (BAM_ERROR);
4473eb2bd662Svikram 	}
4474eb2bd662Svikram 
44757c478bd9Sstevel@tonic-gate 	if (title == NULL) {
4476ee3b8144Sszhou 		title = "Solaris";	/* default to Solaris */
44777c478bd9Sstevel@tonic-gate 	}
44787c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
44797c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
44807c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
44817c478bd9Sstevel@tonic-gate 	}
44827c478bd9Sstevel@tonic-gate 	if (module == NULL) {
4483ae115bc7Smrj 		if (bam_direct != BAM_DIRECT_DBOOT) {
44847c478bd9Sstevel@tonic-gate 			bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
44857c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
44867c478bd9Sstevel@tonic-gate 		}
44877c478bd9Sstevel@tonic-gate 
4488ae115bc7Smrj 		/* Figure the commands out from the kernel line */
4489ae115bc7Smrj 		if (strstr(kernel, "$ISADIR") != NULL) {
4490ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE;
4491ae115bc7Smrj 			k_cmd = KERNEL_DOLLAR_CMD;
4492ae115bc7Smrj 			m_cmd = MODULE_DOLLAR_CMD;
4493ae115bc7Smrj 		} else if (strstr(kernel, "amd64") != NULL) {
4494ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_64;
4495ae115bc7Smrj 			k_cmd = KERNEL_CMD;
4496ae115bc7Smrj 			m_cmd = MODULE_CMD;
4497ae115bc7Smrj 		} else {
4498ae115bc7Smrj 			module = DIRECT_BOOT_ARCHIVE_32;
4499ae115bc7Smrj 			k_cmd = KERNEL_CMD;
4500ae115bc7Smrj 			m_cmd = MODULE_CMD;
4501ae115bc7Smrj 		}
4502ae115bc7Smrj 	} else if ((bam_direct == BAM_DIRECT_DBOOT) &&
4503ae115bc7Smrj 	    (strstr(kernel, "$ISADIR") != NULL)) {
4504ae115bc7Smrj 		/*
4505ae115bc7Smrj 		 * If it's a non-failsafe dboot kernel, use the "kernel$"
4506ae115bc7Smrj 		 * command.  Otherwise, use "kernel".
4507ae115bc7Smrj 		 */
4508ae115bc7Smrj 		k_cmd = KERNEL_DOLLAR_CMD;
4509ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
4510ae115bc7Smrj 	} else {
4511ae115bc7Smrj 		k_cmd = KERNEL_CMD;
4512ae115bc7Smrj 		m_cmd = MODULE_CMD;
4513ae115bc7Smrj 	}
4514ae115bc7Smrj 
45157c478bd9Sstevel@tonic-gate 	if (mp->start) {
45167c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
45177c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
45187c478bd9Sstevel@tonic-gate 	} else {
45197c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
45207c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
45217c478bd9Sstevel@tonic-gate 	}
45227c478bd9Sstevel@tonic-gate 
45237c478bd9Sstevel@tonic-gate 	/*
45247c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
45257c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
45267c478bd9Sstevel@tonic-gate 	 */
45277c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
4528ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_HDR);
45298c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
45327c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
45338c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4536eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
45378c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
4538eb2bd662Svikram 	BAM_DPRINTF((D_ADD_FINDROOT_NUM, fcn, lineNum, entryNum));
45397c478bd9Sstevel@tonic-gate 
45407c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4541ae115bc7Smrj 	    menu_cmds[k_cmd], menu_cmds[SEP_CMD], kernel);
45428c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
45437c478bd9Sstevel@tonic-gate 
4544843e1988Sjohnlev 	if (mod_kernel != NULL) {
4545843e1988Sjohnlev 		(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4546843e1988Sjohnlev 		    menu_cmds[m_cmd], menu_cmds[SEP_CMD], mod_kernel);
4547843e1988Sjohnlev 		line_parser(mp, linebuf, &lineNum, &entryNum);
4548843e1988Sjohnlev 	}
4549843e1988Sjohnlev 
45507c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
4551ae115bc7Smrj 	    menu_cmds[m_cmd], menu_cmds[SEP_CMD], module);
45528c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
45537c478bd9Sstevel@tonic-gate 
45547c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
4555ae115bc7Smrj 	    menu_cmds[COMMENT_CMD], BAM_BOOTADM_FTR);
45568c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
45577c478bd9Sstevel@tonic-gate 
45587c478bd9Sstevel@tonic-gate 	return (entryNum);
45597c478bd9Sstevel@tonic-gate }
45607c478bd9Sstevel@tonic-gate 
45617c478bd9Sstevel@tonic-gate static error_t
45627c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
45637c478bd9Sstevel@tonic-gate {
4564eb2bd662Svikram 	line_t		*lp;
4565eb2bd662Svikram 	line_t		*freed;
4566eb2bd662Svikram 	entry_t		*ent;
4567eb2bd662Svikram 	entry_t		*tmp;
45687c478bd9Sstevel@tonic-gate 	int		deleted;
4569eb2bd662Svikram 	const char	*fcn = "do_delete()";
45707c478bd9Sstevel@tonic-gate 
45717c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
45727c478bd9Sstevel@tonic-gate 
4573eb2bd662Svikram 	tmp = NULL;
4574eb2bd662Svikram 
45758c1b6884Sszhou 	ent = mp->entries;
45768c1b6884Sszhou 	while (ent) {
45778c1b6884Sszhou 		lp = ent->start;
45788c1b6884Sszhou 		/* check entry number and make sure it's a bootadm entry */
45798c1b6884Sszhou 		if (lp->flags != BAM_COMMENT ||
4580ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0 ||
45818c1b6884Sszhou 		    (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) {
45828c1b6884Sszhou 			ent = ent->next;
45837c478bd9Sstevel@tonic-gate 			continue;
45847c478bd9Sstevel@tonic-gate 		}
45857c478bd9Sstevel@tonic-gate 
45868c1b6884Sszhou 		/* free the entry content */
45878c1b6884Sszhou 		do {
45888c1b6884Sszhou 			freed = lp;
45898c1b6884Sszhou 			lp = lp->next;	/* prev stays the same */
4590eb2bd662Svikram 			BAM_DPRINTF((D_FREEING_LINE, fcn, freed->lineNum));
45918c1b6884Sszhou 			unlink_line(mp, freed);
45928c1b6884Sszhou 			line_free(freed);
45938c1b6884Sszhou 		} while (freed != ent->end);
45947c478bd9Sstevel@tonic-gate 
45958c1b6884Sszhou 		/* free the entry_t structure */
4596eb2bd662Svikram 		assert(tmp == NULL);
45978c1b6884Sszhou 		tmp = ent;
45988c1b6884Sszhou 		ent = ent->next;
45998c1b6884Sszhou 		if (tmp->prev)
46008c1b6884Sszhou 			tmp->prev->next = ent;
46017c478bd9Sstevel@tonic-gate 		else
46028c1b6884Sszhou 			mp->entries = ent;
46038c1b6884Sszhou 		if (ent)
46048c1b6884Sszhou 			ent->prev = tmp->prev;
4605eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_ENTRY, fcn, tmp->entryNum));
4606eb2bd662Svikram 		free(tmp);
4607eb2bd662Svikram 		tmp = NULL;
46087c478bd9Sstevel@tonic-gate 		deleted = 1;
46097c478bd9Sstevel@tonic-gate 	}
46107c478bd9Sstevel@tonic-gate 
4611eb2bd662Svikram 	assert(tmp == NULL);
4612eb2bd662Svikram 
46137c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
46147c478bd9Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
46157c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
46167c478bd9Sstevel@tonic-gate 	}
46177c478bd9Sstevel@tonic-gate 
461840541d5dSvikram 	/*
461940541d5dSvikram 	 * Now that we have deleted an entry, update
462040541d5dSvikram 	 * the entry numbering and the default cmd.
462140541d5dSvikram 	 */
462240541d5dSvikram 	update_numbering(mp);
462340541d5dSvikram 
46247c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
46257c478bd9Sstevel@tonic-gate }
46267c478bd9Sstevel@tonic-gate 
46277c478bd9Sstevel@tonic-gate static error_t
4628eb2bd662Svikram delete_all_entries(menu_t *mp, char *dummy, char *opt)
46297c478bd9Sstevel@tonic-gate {
46307c478bd9Sstevel@tonic-gate 	assert(mp);
4631eb2bd662Svikram 	assert(dummy == NULL);
46327c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
46337c478bd9Sstevel@tonic-gate 
4634eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY0, "delete_all_entries"));
4635eb2bd662Svikram 
46367c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
4637eb2bd662Svikram 		bam_print(EMPTY_MENU);
46387c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
46397c478bd9Sstevel@tonic-gate 	}
46407c478bd9Sstevel@tonic-gate 
46417c478bd9Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
46427c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
46437c478bd9Sstevel@tonic-gate 	}
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
46467c478bd9Sstevel@tonic-gate }
46477c478bd9Sstevel@tonic-gate 
46487c478bd9Sstevel@tonic-gate static FILE *
4649eb2bd662Svikram create_diskmap(char *osroot)
46507c478bd9Sstevel@tonic-gate {
46517c478bd9Sstevel@tonic-gate 	FILE *fp;
4652cedc7e57SEnrico Perla - Sun Microsystems 	char cmd[PATH_MAX + 16];
4653cedc7e57SEnrico Perla - Sun Microsystems 	char path[PATH_MAX];
4654eb2bd662Svikram 	const char *fcn = "create_diskmap()";
46557c478bd9Sstevel@tonic-gate 
46567c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
46577c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
46587c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
4659cedc7e57SEnrico Perla - Sun Microsystems 		int	ret;
4660cedc7e57SEnrico Perla - Sun Microsystems 
4661cedc7e57SEnrico Perla - Sun Microsystems 		ret = snprintf(path, sizeof (path), "%s/%s", osroot,
4662cedc7e57SEnrico Perla - Sun Microsystems 		    CREATE_DISKMAP);
4663cedc7e57SEnrico Perla - Sun Microsystems 		if (ret >= sizeof (path)) {
4664cedc7e57SEnrico Perla - Sun Microsystems 			bam_error(PATH_TOO_LONG, osroot);
4665cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
4666cedc7e57SEnrico Perla - Sun Microsystems 		}
4667cedc7e57SEnrico Perla - Sun Microsystems 		if (is_safe_exec(path) == BAM_ERROR)
4668cedc7e57SEnrico Perla - Sun Microsystems 			return (NULL);
4669cedc7e57SEnrico Perla - Sun Microsystems 
46707c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
4671eb2bd662Svikram 		    "%s/%s > /dev/null", osroot, CREATE_DISKMAP);
4672eb2bd662Svikram 		if (exec_cmd(cmd, NULL) != 0)
4673eb2bd662Svikram 			return (NULL);
46747c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
4675eb2bd662Svikram 		INJECT_ERROR1("DISKMAP_CREATE_FAIL", fp = NULL);
4676eb2bd662Svikram 		if (fp) {
4677eb2bd662Svikram 			BAM_DPRINTF((D_CREATED_DISKMAP, fcn, GRUBDISK_MAP));
4678eb2bd662Svikram 		} else {
4679eb2bd662Svikram 			BAM_DPRINTF((D_CREATE_DISKMAP_FAIL, fcn, GRUBDISK_MAP));
4680eb2bd662Svikram 		}
46817c478bd9Sstevel@tonic-gate 	}
46827c478bd9Sstevel@tonic-gate 	return (fp);
46837c478bd9Sstevel@tonic-gate }
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
46867c478bd9Sstevel@tonic-gate 
46877c478bd9Sstevel@tonic-gate static int
46887c478bd9Sstevel@tonic-gate get_partition(char *device)
46897c478bd9Sstevel@tonic-gate {
46907c478bd9Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
46917c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
46927c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
46937c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
46947c478bd9Sstevel@tonic-gate 
46957c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
46967c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
46977c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
46987c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
46997c478bd9Sstevel@tonic-gate 		*slice = '\0';
47007c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
47017c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
47027c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
47037c478bd9Sstevel@tonic-gate 		*slice = 's';
47047c478bd9Sstevel@tonic-gate 
47057c478bd9Sstevel@tonic-gate 	/* read boot sector */
47067c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
47077c478bd9Sstevel@tonic-gate 	free(wholedisk);
47087c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
47097c478bd9Sstevel@tonic-gate 		return (partno);
47107c478bd9Sstevel@tonic-gate 	}
47117c478bd9Sstevel@tonic-gate 	(void) close(fd);
47127c478bd9Sstevel@tonic-gate 
47137c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
47147c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
47157c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
47167c478bd9Sstevel@tonic-gate 		struct ipart *part =
47177c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
47187c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
47197c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
47207c478bd9Sstevel@tonic-gate 				partno = i;
47217c478bd9Sstevel@tonic-gate 				break;
47227c478bd9Sstevel@tonic-gate 			}
47237c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
47247c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
47257c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
47267c478bd9Sstevel@tonic-gate 				partno = i;
47277c478bd9Sstevel@tonic-gate 				break;
47287c478bd9Sstevel@tonic-gate 			}
47297c478bd9Sstevel@tonic-gate 		}
47307c478bd9Sstevel@tonic-gate 	}
47317c478bd9Sstevel@tonic-gate 	return (partno);
47327c478bd9Sstevel@tonic-gate }
47337c478bd9Sstevel@tonic-gate 
4734eb2bd662Svikram char *
4735eb2bd662Svikram get_grubroot(char *osroot, char *osdev, char *menu_root)
47367c478bd9Sstevel@tonic-gate {
4737eb2bd662Svikram 	char		*grubroot;	/* (hd#,#,#) */
47387c478bd9Sstevel@tonic-gate 	char		*slice;
47397c478bd9Sstevel@tonic-gate 	char		*grubhd;
47407c478bd9Sstevel@tonic-gate 	int		fdiskpart;
47417c478bd9Sstevel@tonic-gate 	int		found = 0;
4742eb2bd662Svikram 	char		*devname;
4743eb2bd662Svikram 	char		*ctdname = strstr(osdev, "dsk/");
47447c478bd9Sstevel@tonic-gate 	char		linebuf[PATH_MAX];
4745eb2bd662Svikram 	FILE		*fp;
47467c478bd9Sstevel@tonic-gate 
4747eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_INVALID_OSDEV", ctdname = NULL);
4748eb2bd662Svikram 	if (ctdname == NULL) {
4749eb2bd662Svikram 		bam_error(INVALID_DEV_DSK, osdev);
47507c478bd9Sstevel@tonic-gate 		return (NULL);
4751eb2bd662Svikram 	}
4752eb2bd662Svikram 
4753eb2bd662Svikram 	if (menu_root && !menu_on_bootdisk(osroot, menu_root)) {
4754eb2bd662Svikram 		/* menu bears no resemblance to our reality */
475537eb779cSVikram Hegde 		bam_error(CANNOT_GRUBROOT_BOOTDISK, osdev);
4756eb2bd662Svikram 		return (NULL);
4757eb2bd662Svikram 	}
47587c478bd9Sstevel@tonic-gate 
47597c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
47607c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
47617c478bd9Sstevel@tonic-gate 	if (slice)
47627c478bd9Sstevel@tonic-gate 		*slice = '\0';
47637c478bd9Sstevel@tonic-gate 
4764eb2bd662Svikram 	fp = create_diskmap(osroot);
4765eb2bd662Svikram 	if (fp == NULL) {
4766eb2bd662Svikram 		bam_error(DISKMAP_FAIL, osroot);
4767eb2bd662Svikram 		return (NULL);
4768eb2bd662Svikram 	}
4769eb2bd662Svikram 
47707c478bd9Sstevel@tonic-gate 	rewind(fp);
47717c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
47727c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
47737c478bd9Sstevel@tonic-gate 		if (grubhd)
47747c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
47757c478bd9Sstevel@tonic-gate 		else
47767c478bd9Sstevel@tonic-gate 			devname = NULL;
47777c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
47787c478bd9Sstevel@tonic-gate 			found = 1;
47797c478bd9Sstevel@tonic-gate 			break;
47807c478bd9Sstevel@tonic-gate 		}
47817c478bd9Sstevel@tonic-gate 	}
47827c478bd9Sstevel@tonic-gate 
47837c478bd9Sstevel@tonic-gate 	if (slice)
47847c478bd9Sstevel@tonic-gate 		*slice = 's';
47857c478bd9Sstevel@tonic-gate 
4786eb2bd662Svikram 	(void) fclose(fp);
4787eb2bd662Svikram 	fp = NULL;
4788eb2bd662Svikram 
4789eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_BIOSDEV_FAIL", found = 0);
47907c478bd9Sstevel@tonic-gate 	if (found == 0) {
479137eb779cSVikram Hegde 		bam_error(BIOSDEV_SKIP, osdev);
4792eb2bd662Svikram 		return (NULL);
47937c478bd9Sstevel@tonic-gate 	}
47947c478bd9Sstevel@tonic-gate 
4795eb2bd662Svikram 	fdiskpart = get_partition(osdev);
4796eb2bd662Svikram 	INJECT_ERROR1("GRUBROOT_FDISK_FAIL", fdiskpart = -1);
4797eb2bd662Svikram 	if (fdiskpart == -1) {
4798eb2bd662Svikram 		bam_error(FDISKPART_FAIL, osdev);
47997c478bd9Sstevel@tonic-gate 		return (NULL);
4800eb2bd662Svikram 	}
48017c478bd9Sstevel@tonic-gate 
4802eb2bd662Svikram 	grubroot = s_calloc(1, 10);
48037c478bd9Sstevel@tonic-gate 	if (slice) {
4804eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d,%c)",
48057c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
48067c478bd9Sstevel@tonic-gate 	} else
4807eb2bd662Svikram 		(void) snprintf(grubroot, 10, "(hd%s,%d)",
48087c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
48097c478bd9Sstevel@tonic-gate 
4810eb2bd662Svikram 	assert(fp == NULL);
4811eb2bd662Svikram 	assert(strncmp(grubroot, "(hd", strlen("(hd")) == 0);
4812eb2bd662Svikram 	return (grubroot);
4813eb2bd662Svikram }
4814eb2bd662Svikram 
4815eb2bd662Svikram static char *
4816eb2bd662Svikram find_primary_common(char *mntpt, char *fstype)
4817eb2bd662Svikram {
4818eb2bd662Svikram 	char		signdir[PATH_MAX];
4819eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
4820eb2bd662Svikram 	char		*lu;
4821eb2bd662Svikram 	char		*ufs;
4822eb2bd662Svikram 	char		*zfs;
4823eb2bd662Svikram 	DIR		*dirp = NULL;
4824eb2bd662Svikram 	struct dirent	*entp;
4825eb2bd662Svikram 	struct stat	sb;
4826eb2bd662Svikram 	const char	*fcn = "find_primary_common()";
4827eb2bd662Svikram 
4828eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
4829eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
4830eb2bd662Svikram 
4831eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
4832eb2bd662Svikram 		BAM_DPRINTF((D_NO_SIGNDIR, fcn, signdir));
4833eb2bd662Svikram 		return (NULL);
4834eb2bd662Svikram 	}
4835eb2bd662Svikram 
4836eb2bd662Svikram 	dirp = opendir(signdir);
4837eb2bd662Svikram 	INJECT_ERROR1("SIGNDIR_OPENDIR_FAIL", dirp = NULL);
4838eb2bd662Svikram 	if (dirp == NULL) {
4839eb2bd662Svikram 		bam_error(OPENDIR_FAILED, signdir, strerror(errno));
4840eb2bd662Svikram 		return (NULL);
4841eb2bd662Svikram 	}
4842eb2bd662Svikram 
4843eb2bd662Svikram 	ufs = zfs = lu = NULL;
4844eb2bd662Svikram 
4845eb2bd662Svikram 	while (entp = readdir(dirp)) {
4846eb2bd662Svikram 		if (strcmp(entp->d_name, ".") == 0 ||
4847eb2bd662Svikram 		    strcmp(entp->d_name, "..") == 0)
4848eb2bd662Svikram 			continue;
4849eb2bd662Svikram 
4850eb2bd662Svikram 		(void) snprintf(tmpsign, sizeof (tmpsign), "%s", entp->d_name);
4851eb2bd662Svikram 
4852eb2bd662Svikram 		if (lu == NULL &&
4853eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
4854eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
4855eb2bd662Svikram 			lu = s_strdup(tmpsign);
4856eb2bd662Svikram 		}
4857eb2bd662Svikram 
4858eb2bd662Svikram 		if (ufs == NULL &&
4859eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
4860eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
4861eb2bd662Svikram 			ufs = s_strdup(tmpsign);
4862eb2bd662Svikram 		}
4863eb2bd662Svikram 
4864eb2bd662Svikram 		if (zfs == NULL &&
4865eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
4866eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
4867eb2bd662Svikram 			zfs = s_strdup(tmpsign);
4868eb2bd662Svikram 		}
4869eb2bd662Svikram 	}
4870eb2bd662Svikram 
4871eb2bd662Svikram 	BAM_DPRINTF((D_EXIST_PRIMARY_SIGNS, fcn,
4872eb2bd662Svikram 	    zfs ? zfs : "NULL",
4873eb2bd662Svikram 	    ufs ? ufs : "NULL",
4874eb2bd662Svikram 	    lu ? lu : "NULL"));
4875eb2bd662Svikram 
4876eb2bd662Svikram 	if (dirp) {
4877eb2bd662Svikram 		(void) closedir(dirp);
4878eb2bd662Svikram 		dirp = NULL;
4879eb2bd662Svikram 	}
4880eb2bd662Svikram 
4881eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
4882eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs");
4883eb2bd662Svikram 		free(zfs);
4884eb2bd662Svikram 		zfs = NULL;
4885eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
4886eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs");
4887eb2bd662Svikram 		free(ufs);
4888eb2bd662Svikram 		ufs = NULL;
4889eb2bd662Svikram 	}
4890eb2bd662Svikram 
4891eb2bd662Svikram 	assert(dirp == NULL);
4892eb2bd662Svikram 
4893eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
4894eb2bd662Svikram 	if (lu) {
4895eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu));
4896eb2bd662Svikram 		free(lu);
4897eb2bd662Svikram 		lu = NULL;
4898eb2bd662Svikram 	}
4899eb2bd662Svikram 
4900eb2bd662Svikram 	return (zfs ? zfs : ufs);
4901eb2bd662Svikram }
4902eb2bd662Svikram 
4903eb2bd662Svikram static char *
4904eb2bd662Svikram find_backup_common(char *mntpt, char *fstype)
4905eb2bd662Svikram {
4906eb2bd662Svikram 	FILE		*bfp = NULL;
4907eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
4908eb2bd662Svikram 	char		backup[PATH_MAX];
4909eb2bd662Svikram 	char		*ufs;
4910eb2bd662Svikram 	char		*zfs;
4911eb2bd662Svikram 	char		*lu;
4912eb2bd662Svikram 	int		error;
4913eb2bd662Svikram 	const char	*fcn = "find_backup_common()";
4914eb2bd662Svikram 
4915eb2bd662Svikram 	/*
4916eb2bd662Svikram 	 * We didn't find it in the primary directory.
4917eb2bd662Svikram 	 * Look at the backup
4918eb2bd662Svikram 	 */
4919eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
4920eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
4921eb2bd662Svikram 
4922eb2bd662Svikram 	bfp = fopen(backup, "r");
4923eb2bd662Svikram 	if (bfp == NULL) {
4924eb2bd662Svikram 		error = errno;
4925eb2bd662Svikram 		if (bam_verbose) {
4926eb2bd662Svikram 			bam_error(OPEN_FAIL, backup, strerror(error));
4927eb2bd662Svikram 		}
4928eb2bd662Svikram 		BAM_DPRINTF((D_OPEN_FAIL, fcn, backup, strerror(error)));
4929eb2bd662Svikram 		return (NULL);
4930eb2bd662Svikram 	}
4931eb2bd662Svikram 
4932eb2bd662Svikram 	ufs = zfs = lu = NULL;
4933eb2bd662Svikram 
4934eb2bd662Svikram 	while (s_fgets(tmpsign, sizeof (tmpsign), bfp) != NULL) {
4935eb2bd662Svikram 
4936eb2bd662Svikram 		if (lu == NULL &&
4937eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_LU_PREFIX,
4938eb2bd662Svikram 		    strlen(GRUBSIGN_LU_PREFIX)) == 0) {
4939eb2bd662Svikram 			lu = s_strdup(tmpsign);
4940eb2bd662Svikram 		}
4941eb2bd662Svikram 
4942eb2bd662Svikram 		if (ufs == NULL &&
4943eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
4944eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) == 0) {
4945eb2bd662Svikram 			ufs = s_strdup(tmpsign);
4946eb2bd662Svikram 		}
4947eb2bd662Svikram 
4948eb2bd662Svikram 		if (zfs == NULL &&
4949eb2bd662Svikram 		    strncmp(tmpsign, GRUBSIGN_ZFS_PREFIX,
4950eb2bd662Svikram 		    strlen(GRUBSIGN_ZFS_PREFIX)) == 0) {
4951eb2bd662Svikram 			zfs = s_strdup(tmpsign);
4952eb2bd662Svikram 		}
4953eb2bd662Svikram 	}
4954eb2bd662Svikram 
4955eb2bd662Svikram 	BAM_DPRINTF((D_EXIST_BACKUP_SIGNS, fcn,
4956eb2bd662Svikram 	    zfs ? zfs : "NULL",
4957eb2bd662Svikram 	    ufs ? ufs : "NULL",
4958eb2bd662Svikram 	    lu ? lu : "NULL"));
4959eb2bd662Svikram 
4960eb2bd662Svikram 	if (bfp) {
4961eb2bd662Svikram 		(void) fclose(bfp);
4962eb2bd662Svikram 		bfp = NULL;
4963eb2bd662Svikram 	}
4964eb2bd662Svikram 
4965eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0 && zfs) {
4966eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, zfs, "ufs");
4967eb2bd662Svikram 		free(zfs);
4968eb2bd662Svikram 		zfs = NULL;
4969eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0 && ufs) {
4970eb2bd662Svikram 		bam_error(SIGN_FSTYPE_MISMATCH, ufs, "zfs");
4971eb2bd662Svikram 		free(ufs);
4972eb2bd662Svikram 		ufs = NULL;
4973eb2bd662Svikram 	}
4974eb2bd662Svikram 
4975eb2bd662Svikram 	assert(bfp == NULL);
4976eb2bd662Svikram 
4977eb2bd662Svikram 	/* For now, we let Live Upgrade take care of its signature itself */
4978eb2bd662Svikram 	if (lu) {
4979eb2bd662Svikram 		BAM_DPRINTF((D_FREEING_LU_SIGNS, fcn, lu));
4980eb2bd662Svikram 		free(lu);
4981eb2bd662Svikram 		lu = NULL;
4982eb2bd662Svikram 	}
4983eb2bd662Svikram 
4984eb2bd662Svikram 	return (zfs ? zfs : ufs);
4985eb2bd662Svikram }
4986eb2bd662Svikram 
4987eb2bd662Svikram static char *
4988eb2bd662Svikram find_ufs_existing(char *osroot)
4989eb2bd662Svikram {
4990eb2bd662Svikram 	char		*sign;
4991eb2bd662Svikram 	const char	*fcn = "find_ufs_existing()";
4992eb2bd662Svikram 
4993eb2bd662Svikram 	sign = find_primary_common(osroot, "ufs");
4994eb2bd662Svikram 	if (sign == NULL) {
4995eb2bd662Svikram 		sign = find_backup_common(osroot, "ufs");
4996eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL"));
4997eb2bd662Svikram 	} else {
4998eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign));
4999eb2bd662Svikram 	}
5000eb2bd662Svikram 
5001eb2bd662Svikram 	return (sign);
5002eb2bd662Svikram }
5003eb2bd662Svikram 
5004eb2bd662Svikram char *
5005eb2bd662Svikram get_mountpoint(char *special, char *fstype)
5006eb2bd662Svikram {
5007eb2bd662Svikram 	FILE		*mntfp;
5008eb2bd662Svikram 	struct mnttab	mp = {0};
5009eb2bd662Svikram 	struct mnttab	mpref = {0};
5010eb2bd662Svikram 	int		error;
5011eb2bd662Svikram 	int		ret;
5012eb2bd662Svikram 	const char	*fcn = "get_mountpoint()";
5013eb2bd662Svikram 
5014eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, special, fstype));
5015eb2bd662Svikram 
5016eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
5017eb2bd662Svikram 	error = errno;
5018eb2bd662Svikram 	INJECT_ERROR1("MNTTAB_ERR_GET_MNTPT", mntfp = NULL);
5019eb2bd662Svikram 	if (mntfp == NULL) {
5020eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
5021eb2bd662Svikram 		return (NULL);
5022eb2bd662Svikram 	}
5023eb2bd662Svikram 
5024eb2bd662Svikram 	mpref.mnt_special = special;
5025eb2bd662Svikram 	mpref.mnt_fstype = fstype;
5026eb2bd662Svikram 
5027eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
5028eb2bd662Svikram 	INJECT_ERROR1("GET_MOUNTPOINT_MNTANY", ret = 1);
5029eb2bd662Svikram 	if (ret != 0) {
5030eb2bd662Svikram 		(void) fclose(mntfp);
5031eb2bd662Svikram 		BAM_DPRINTF((D_NO_MNTPT, fcn, special, fstype));
5032eb2bd662Svikram 		return (NULL);
5033eb2bd662Svikram 	}
5034eb2bd662Svikram 	(void) fclose(mntfp);
5035eb2bd662Svikram 
5036eb2bd662Svikram 	assert(mp.mnt_mountp);
5037eb2bd662Svikram 
5038eb2bd662Svikram 	BAM_DPRINTF((D_GET_MOUNTPOINT_RET, fcn, special, mp.mnt_mountp));
5039eb2bd662Svikram 
5040eb2bd662Svikram 	return (s_strdup(mp.mnt_mountp));
5041eb2bd662Svikram }
5042eb2bd662Svikram 
5043eb2bd662Svikram /*
5044eb2bd662Svikram  * Mounts a "legacy" top dataset (if needed)
5045eb2bd662Svikram  * Returns:	The mountpoint of the legacy top dataset or NULL on error
5046eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
5047eb2bd662Svikram  */
5048eb2bd662Svikram static char *
5049eb2bd662Svikram mount_legacy_dataset(char *pool, zfs_mnted_t *mnted)
5050eb2bd662Svikram {
5051eb2bd662Svikram 	char		cmd[PATH_MAX];
5052eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
5053eb2bd662Svikram 	filelist_t	flist = {0};
5054eb2bd662Svikram 	char		*is_mounted;
5055eb2bd662Svikram 	struct stat	sb;
5056eb2bd662Svikram 	int		ret;
5057eb2bd662Svikram 	const char	*fcn = "mount_legacy_dataset()";
5058eb2bd662Svikram 
5059eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool));
5060eb2bd662Svikram 
5061eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
5062eb2bd662Svikram 
5063eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5064eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
5065eb2bd662Svikram 	    pool);
5066eb2bd662Svikram 
5067eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5068eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_CMD", ret = 1);
5069eb2bd662Svikram 	if (ret != 0) {
5070eb2bd662Svikram 		bam_error(ZFS_MNTED_FAILED, pool);
5071eb2bd662Svikram 		return (NULL);
5072eb2bd662Svikram 	}
5073eb2bd662Svikram 
5074eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_OUT", flist.head = NULL);
5075eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5076eb2bd662Svikram 		bam_error(BAD_ZFS_MNTED, pool);
5077eb2bd662Svikram 		filelist_free(&flist);
5078eb2bd662Svikram 		return (NULL);
5079eb2bd662Svikram 	}
5080eb2bd662Svikram 
5081eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5082eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_YES", is_mounted = "yes");
5083eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_LEG_GET_MOUNTED_STRTOK_NO", is_mounted = "no");
5084eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5085eb2bd662Svikram 		filelist_free(&flist);
5086eb2bd662Svikram 		*mnted = LEGACY_ALREADY;
5087eb2bd662Svikram 		/* get_mountpoint returns a strdup'ed string */
5088eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_ALREADY, fcn, pool));
5089eb2bd662Svikram 		return (get_mountpoint(pool, "zfs"));
5090eb2bd662Svikram 	}
5091eb2bd662Svikram 
5092eb2bd662Svikram 	filelist_free(&flist);
5093eb2bd662Svikram 
5094eb2bd662Svikram 	/*
5095eb2bd662Svikram 	 * legacy top dataset is not mounted. Mount it now
5096eb2bd662Svikram 	 * First create a mountpoint.
5097eb2bd662Svikram 	 */
5098eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt), "%s.%d",
5099eb2bd662Svikram 	    ZFS_LEGACY_MNTPT, getpid());
5100eb2bd662Svikram 
5101eb2bd662Svikram 	ret = stat(tmpmnt, &sb);
5102eb2bd662Svikram 	if (ret == -1) {
5103eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_ABS, fcn, pool, tmpmnt));
510448847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(tmpmnt, DIR_PERMS);
5105eb2bd662Svikram 		INJECT_ERROR1("Z_MOUNT_TOP_LEG_MNTPT_MKDIRP", ret = -1);
5106eb2bd662Svikram 		if (ret == -1) {
5107eb2bd662Svikram 			bam_error(MKDIR_FAILED, tmpmnt, strerror(errno));
5108eb2bd662Svikram 			return (NULL);
5109eb2bd662Svikram 		}
5110eb2bd662Svikram 	} else {
5111eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MNTPT_PRES, fcn, pool, tmpmnt));
5112eb2bd662Svikram 	}
5113eb2bd662Svikram 
5114eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5115eb2bd662Svikram 	    "/sbin/mount -F zfs %s %s",
5116eb2bd662Svikram 	    pool, tmpmnt);
5117eb2bd662Svikram 
5118eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5119eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_LEG_MOUNT_CMD", ret = 1);
5120eb2bd662Svikram 	if (ret != 0) {
5121eb2bd662Svikram 		bam_error(ZFS_MOUNT_FAILED, pool);
5122eb2bd662Svikram 		(void) rmdir(tmpmnt);
5123eb2bd662Svikram 		return (NULL);
5124eb2bd662Svikram 	}
5125eb2bd662Svikram 
5126eb2bd662Svikram 	*mnted = LEGACY_MOUNTED;
5127eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_LEG_MOUNTED, fcn, pool, tmpmnt));
5128eb2bd662Svikram 	return (s_strdup(tmpmnt));
5129eb2bd662Svikram }
5130eb2bd662Svikram 
5131eb2bd662Svikram /*
5132eb2bd662Svikram  * Mounts the top dataset (if needed)
5133eb2bd662Svikram  * Returns:	The mountpoint of the top dataset or NULL on error
5134eb2bd662Svikram  * 		mnted returns one of the above values defined for zfs_mnted_t
5135eb2bd662Svikram  */
5136eb2bd662Svikram static char *
5137eb2bd662Svikram mount_top_dataset(char *pool, zfs_mnted_t *mnted)
5138eb2bd662Svikram {
5139eb2bd662Svikram 	char		cmd[PATH_MAX];
5140eb2bd662Svikram 	filelist_t	flist = {0};
5141eb2bd662Svikram 	char		*is_mounted;
5142eb2bd662Svikram 	char		*mntpt;
5143eb2bd662Svikram 	char		*zmntpt;
5144eb2bd662Svikram 	int		ret;
5145eb2bd662Svikram 	const char	*fcn = "mount_top_dataset()";
5146eb2bd662Svikram 
5147eb2bd662Svikram 	*mnted = ZFS_MNT_ERROR;
5148eb2bd662Svikram 
5149eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, pool));
5150eb2bd662Svikram 
5151eb2bd662Svikram 	/*
5152eb2bd662Svikram 	 * First check if the top dataset is a "legacy" dataset
5153eb2bd662Svikram 	 */
5154eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5155eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
5156eb2bd662Svikram 	    pool);
5157eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5158eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_GET_MNTPT", ret = 1);
5159eb2bd662Svikram 	if (ret != 0) {
5160eb2bd662Svikram 		bam_error(ZFS_MNTPT_FAILED, pool);
5161eb2bd662Svikram 		return (NULL);
5162eb2bd662Svikram 	}
5163eb2bd662Svikram 
5164eb2bd662Svikram 	if (flist.head && (flist.head == flist.tail)) {
5165eb2bd662Svikram 		char *legacy = strtok(flist.head->line, " \t\n");
5166eb2bd662Svikram 		if (legacy && strcmp(legacy, "legacy") == 0) {
5167eb2bd662Svikram 			filelist_free(&flist);
5168eb2bd662Svikram 			BAM_DPRINTF((D_Z_IS_LEGACY, fcn, pool));
5169eb2bd662Svikram 			return (mount_legacy_dataset(pool, mnted));
5170eb2bd662Svikram 		}
5171eb2bd662Svikram 	}
5172eb2bd662Svikram 
5173eb2bd662Svikram 	filelist_free(&flist);
5174eb2bd662Svikram 
5175eb2bd662Svikram 	BAM_DPRINTF((D_Z_IS_NOT_LEGACY, fcn, pool));
5176eb2bd662Svikram 
5177eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5178eb2bd662Svikram 	    "/sbin/zfs get -Ho value mounted %s",
5179eb2bd662Svikram 	    pool);
5180eb2bd662Svikram 
5181eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5182eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED", ret = 1);
5183eb2bd662Svikram 	if (ret != 0) {
5184eb2bd662Svikram 		bam_error(ZFS_MNTED_FAILED, pool);
5185eb2bd662Svikram 		return (NULL);
5186eb2bd662Svikram 	}
5187eb2bd662Svikram 
5188eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_VAL", flist.head = NULL);
5189eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5190eb2bd662Svikram 		bam_error(BAD_ZFS_MNTED, pool);
5191eb2bd662Svikram 		filelist_free(&flist);
5192eb2bd662Svikram 		return (NULL);
5193eb2bd662Svikram 	}
5194eb2bd662Svikram 
5195eb2bd662Svikram 	is_mounted = strtok(flist.head->line, " \t\n");
5196eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_YES", is_mounted = "yes");
5197eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MOUNTED_NO", is_mounted = "no");
5198eb2bd662Svikram 	if (strcmp(is_mounted, "no") != 0) {
5199eb2bd662Svikram 		filelist_free(&flist);
5200eb2bd662Svikram 		*mnted = ZFS_ALREADY;
5201eb2bd662Svikram 		BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_ALREADY, fcn, pool));
5202eb2bd662Svikram 		goto mounted;
5203eb2bd662Svikram 	}
5204eb2bd662Svikram 
5205eb2bd662Svikram 	filelist_free(&flist);
5206eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOT_ALREADY, fcn, pool));
5207eb2bd662Svikram 
5208eb2bd662Svikram 	/* top dataset is not mounted. Mount it now */
5209eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5210eb2bd662Svikram 	    "/sbin/zfs mount %s", pool);
5211eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
5212eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_MOUNT_CMD", ret = 1);
5213eb2bd662Svikram 	if (ret != 0) {
5214eb2bd662Svikram 		bam_error(ZFS_MOUNT_FAILED, pool);
5215eb2bd662Svikram 		return (NULL);
5216eb2bd662Svikram 	}
5217eb2bd662Svikram 	*mnted = ZFS_MOUNTED;
5218eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MOUNTED_NOW, fcn, pool));
5219eb2bd662Svikram 	/*FALLTHRU*/
5220eb2bd662Svikram mounted:
5221eb2bd662Svikram 	/*
5222eb2bd662Svikram 	 * Now get the mountpoint
5223eb2bd662Svikram 	 */
5224eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5225eb2bd662Svikram 	    "/sbin/zfs get -Ho value mountpoint %s",
5226eb2bd662Svikram 	    pool);
5227eb2bd662Svikram 
5228eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5229eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_CMD", ret = 1);
5230eb2bd662Svikram 	if (ret != 0) {
5231eb2bd662Svikram 		bam_error(ZFS_MNTPT_FAILED, pool);
5232eb2bd662Svikram 		goto error;
5233eb2bd662Svikram 	}
5234eb2bd662Svikram 
5235eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_OUT", flist.head = NULL);
5236eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5237eb2bd662Svikram 		bam_error(NULL_ZFS_MNTPT, pool);
5238eb2bd662Svikram 		goto error;
5239eb2bd662Svikram 	}
5240eb2bd662Svikram 
5241eb2bd662Svikram 	mntpt = strtok(flist.head->line, " \t\n");
5242eb2bd662Svikram 	INJECT_ERROR1("Z_MOUNT_TOP_NONLEG_GET_MNTPT_STRTOK", mntpt = "foo");
5243eb2bd662Svikram 	if (*mntpt != '/') {
5244eb2bd662Svikram 		bam_error(BAD_ZFS_MNTPT, pool, mntpt);
5245eb2bd662Svikram 		goto error;
5246eb2bd662Svikram 	}
5247eb2bd662Svikram 	zmntpt = s_strdup(mntpt);
5248eb2bd662Svikram 
5249eb2bd662Svikram 	filelist_free(&flist);
5250eb2bd662Svikram 
5251eb2bd662Svikram 	BAM_DPRINTF((D_Z_MOUNT_TOP_NONLEG_MNTPT, fcn, pool, zmntpt));
5252eb2bd662Svikram 
5253eb2bd662Svikram 	return (zmntpt);
5254eb2bd662Svikram 
5255eb2bd662Svikram error:
5256eb2bd662Svikram 	filelist_free(&flist);
5257eb2bd662Svikram 	(void) umount_top_dataset(pool, *mnted, NULL);
5258eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
5259eb2bd662Svikram 	return (NULL);
5260eb2bd662Svikram }
5261eb2bd662Svikram 
5262eb2bd662Svikram static int
5263eb2bd662Svikram umount_top_dataset(char *pool, zfs_mnted_t mnted, char *mntpt)
5264eb2bd662Svikram {
5265eb2bd662Svikram 	char		cmd[PATH_MAX];
5266eb2bd662Svikram 	int		ret;
5267eb2bd662Svikram 	const char	*fcn = "umount_top_dataset()";
5268eb2bd662Svikram 
5269eb2bd662Svikram 	INJECT_ERROR1("Z_UMOUNT_TOP_INVALID_STATE", mnted = ZFS_MNT_ERROR);
5270eb2bd662Svikram 	switch (mnted) {
5271eb2bd662Svikram 	case LEGACY_ALREADY:
5272eb2bd662Svikram 	case ZFS_ALREADY:
5273eb2bd662Svikram 		/* nothing to do */
5274eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_ALREADY_NOP, fcn, pool,
5275eb2bd662Svikram 		    mntpt ? mntpt : "NULL"));
5276eb2bd662Svikram 		free(mntpt);
5277eb2bd662Svikram 		return (BAM_SUCCESS);
5278eb2bd662Svikram 	case LEGACY_MOUNTED:
5279eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
5280eb2bd662Svikram 		    "/sbin/umount %s", pool);
5281eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
5282eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_LEGACY_UMOUNT_FAIL", ret = 1);
5283eb2bd662Svikram 		if (ret != 0) {
5284eb2bd662Svikram 			bam_error(UMOUNT_FAILED, pool);
5285eb2bd662Svikram 			free(mntpt);
5286eb2bd662Svikram 			return (BAM_ERROR);
5287eb2bd662Svikram 		}
5288eb2bd662Svikram 		if (mntpt)
5289eb2bd662Svikram 			(void) rmdir(mntpt);
5290eb2bd662Svikram 		free(mntpt);
5291eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_LEGACY, fcn, pool));
5292eb2bd662Svikram 		return (BAM_SUCCESS);
5293eb2bd662Svikram 	case ZFS_MOUNTED:
5294eb2bd662Svikram 		free(mntpt);
5295eb2bd662Svikram 		(void) snprintf(cmd, sizeof (cmd),
5296eb2bd662Svikram 		    "/sbin/zfs unmount %s", pool);
5297eb2bd662Svikram 		ret = exec_cmd(cmd, NULL);
5298eb2bd662Svikram 		INJECT_ERROR1("Z_UMOUNT_TOP_NONLEG_UMOUNT_FAIL", ret = 1);
5299eb2bd662Svikram 		if (ret != 0) {
5300eb2bd662Svikram 			bam_error(UMOUNT_FAILED, pool);
5301eb2bd662Svikram 			return (BAM_ERROR);
5302eb2bd662Svikram 		}
5303eb2bd662Svikram 		BAM_DPRINTF((D_Z_UMOUNT_TOP_NONLEG, fcn, pool));
5304eb2bd662Svikram 		return (BAM_SUCCESS);
5305eb2bd662Svikram 	default:
5306eb2bd662Svikram 		bam_error(INT_BAD_MNTSTATE, pool);
5307eb2bd662Svikram 		return (BAM_ERROR);
5308eb2bd662Svikram 	}
5309eb2bd662Svikram 	/*NOTREACHED*/
5310eb2bd662Svikram }
5311eb2bd662Svikram 
5312eb2bd662Svikram /*
5313eb2bd662Svikram  * For ZFS, osdev can be one of two forms
5314eb2bd662Svikram  * It can be a "special" file as seen in mnttab: rpool/ROOT/szboot_0402
5315eb2bd662Svikram  * It can be a /dev/[r]dsk special file. We handle both instances
5316eb2bd662Svikram  */
5317eb2bd662Svikram static char *
5318eb2bd662Svikram get_pool(char *osdev)
5319eb2bd662Svikram {
5320eb2bd662Svikram 	char		cmd[PATH_MAX];
5321eb2bd662Svikram 	char		buf[PATH_MAX];
5322eb2bd662Svikram 	filelist_t	flist = {0};
5323eb2bd662Svikram 	char		*pool;
5324eb2bd662Svikram 	char		*cp;
5325eb2bd662Svikram 	char		*slash;
5326eb2bd662Svikram 	int		ret;
5327eb2bd662Svikram 	const char	*fcn = "get_pool()";
5328eb2bd662Svikram 
5329eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_OSDEV", osdev = NULL);
5330eb2bd662Svikram 	if (osdev == NULL) {
5331eb2bd662Svikram 		bam_error(GET_POOL_OSDEV_NULL);
5332eb2bd662Svikram 		return (NULL);
5333eb2bd662Svikram 	}
5334eb2bd662Svikram 
5335eb2bd662Svikram 	BAM_DPRINTF((D_GET_POOL_OSDEV, fcn, osdev));
5336eb2bd662Svikram 
5337eb2bd662Svikram 	if (osdev[0] != '/') {
5338eb2bd662Svikram 		(void) strlcpy(buf, osdev, sizeof (buf));
5339eb2bd662Svikram 		slash = strchr(buf, '/');
5340eb2bd662Svikram 		if (slash)
5341eb2bd662Svikram 			*slash = '\0';
5342eb2bd662Svikram 		pool = s_strdup(buf);
5343eb2bd662Svikram 		BAM_DPRINTF((D_GET_POOL_RET, fcn, pool));
5344eb2bd662Svikram 		return (pool);
5345eb2bd662Svikram 	} else if (strncmp(osdev, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
5346eb2bd662Svikram 	    strncmp(osdev, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0) {
5347eb2bd662Svikram 		bam_error(GET_POOL_BAD_OSDEV, osdev);
5348eb2bd662Svikram 		return (NULL);
5349eb2bd662Svikram 	}
5350eb2bd662Svikram 
5351eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5352eb2bd662Svikram 	    "/usr/sbin/fstyp -a %s 2>/dev/null | /bin/grep '^name:'",
5353eb2bd662Svikram 	    osdev);
5354eb2bd662Svikram 
5355eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
5356eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP", ret = 1);
5357eb2bd662Svikram 	if (ret != 0) {
5358eb2bd662Svikram 		bam_error(FSTYP_A_FAILED, osdev);
5359eb2bd662Svikram 		return (NULL);
5360eb2bd662Svikram 	}
5361eb2bd662Svikram 
5362eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_OUT", flist.head = NULL);
5363eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5364eb2bd662Svikram 		bam_error(NULL_FSTYP_A, osdev);
5365eb2bd662Svikram 		filelist_free(&flist);
5366eb2bd662Svikram 		return (NULL);
5367eb2bd662Svikram 	}
5368eb2bd662Svikram 
5369eb2bd662Svikram 	(void) strtok(flist.head->line, "'");
5370eb2bd662Svikram 	cp = strtok(NULL, "'");
5371eb2bd662Svikram 	INJECT_ERROR1("GET_POOL_FSTYP_STRTOK", cp = NULL);
5372eb2bd662Svikram 	if (cp == NULL) {
5373eb2bd662Svikram 		bam_error(BAD_FSTYP_A, osdev);
5374eb2bd662Svikram 		filelist_free(&flist);
5375eb2bd662Svikram 		return (NULL);
5376eb2bd662Svikram 	}
5377eb2bd662Svikram 
5378eb2bd662Svikram 	pool = s_strdup(cp);
5379eb2bd662Svikram 
5380eb2bd662Svikram 	filelist_free(&flist);
5381eb2bd662Svikram 
5382eb2bd662Svikram 	BAM_DPRINTF((D_GET_POOL_RET, fcn, pool));
5383eb2bd662Svikram 
5384eb2bd662Svikram 	return (pool);
5385eb2bd662Svikram }
5386eb2bd662Svikram 
5387eb2bd662Svikram static char *
5388eb2bd662Svikram find_zfs_existing(char *osdev)
5389eb2bd662Svikram {
5390eb2bd662Svikram 	char		*pool;
5391eb2bd662Svikram 	zfs_mnted_t	mnted;
5392eb2bd662Svikram 	char		*mntpt;
5393eb2bd662Svikram 	char		*sign;
5394eb2bd662Svikram 	const char	*fcn = "find_zfs_existing()";
5395eb2bd662Svikram 
5396eb2bd662Svikram 	pool = get_pool(osdev);
5397eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_POOL", pool = NULL);
5398eb2bd662Svikram 	if (pool == NULL) {
5399eb2bd662Svikram 		bam_error(ZFS_GET_POOL_FAILED, osdev);
5400eb2bd662Svikram 		return (NULL);
5401eb2bd662Svikram 	}
5402eb2bd662Svikram 
5403eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
5404eb2bd662Svikram 	INJECT_ERROR1("ZFS_FIND_EXIST_MOUNT_TOP", mntpt = NULL);
5405eb2bd662Svikram 	if (mntpt == NULL) {
5406eb2bd662Svikram 		bam_error(ZFS_MOUNT_TOP_DATASET_FAILED, pool);
5407eb2bd662Svikram 		free(pool);
5408eb2bd662Svikram 		return (NULL);
5409eb2bd662Svikram 	}
5410eb2bd662Svikram 
5411eb2bd662Svikram 	sign = find_primary_common(mntpt, "zfs");
5412eb2bd662Svikram 	if (sign == NULL) {
5413eb2bd662Svikram 		sign = find_backup_common(mntpt, "zfs");
5414eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_BACKUP_SIGN, fcn, sign ? sign : "NULL"));
5415eb2bd662Svikram 	} else {
5416eb2bd662Svikram 		BAM_DPRINTF((D_EXIST_PRIMARY_SIGN, fcn, sign));
5417eb2bd662Svikram 	}
5418eb2bd662Svikram 
5419eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
5420eb2bd662Svikram 
5421eb2bd662Svikram 	free(pool);
5422eb2bd662Svikram 
5423eb2bd662Svikram 	return (sign);
5424eb2bd662Svikram }
5425eb2bd662Svikram 
5426eb2bd662Svikram static char *
5427eb2bd662Svikram find_existing_sign(char *osroot, char *osdev, char *fstype)
5428eb2bd662Svikram {
5429eb2bd662Svikram 	const char		*fcn = "find_existing_sign()";
5430eb2bd662Svikram 
5431eb2bd662Svikram 	INJECT_ERROR1("FIND_EXIST_NOTSUP_FS", fstype = "foofs");
5432eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
5433eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_UFS_EXIST_SIGN, fcn));
5434eb2bd662Svikram 		return (find_ufs_existing(osroot));
5435eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
5436eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_ZFS_EXIST_SIGN, fcn));
5437eb2bd662Svikram 		return (find_zfs_existing(osdev));
5438eb2bd662Svikram 	} else {
5439eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
5440eb2bd662Svikram 		return (NULL);
5441eb2bd662Svikram 	}
5442eb2bd662Svikram }
5443eb2bd662Svikram 
5444eb2bd662Svikram #define	MH_HASH_SZ	16
5445eb2bd662Svikram 
5446eb2bd662Svikram typedef enum {
5447eb2bd662Svikram 	MH_ERROR = -1,
5448eb2bd662Svikram 	MH_NOMATCH,
5449eb2bd662Svikram 	MH_MATCH
5450eb2bd662Svikram } mh_search_t;
5451eb2bd662Svikram 
5452eb2bd662Svikram typedef struct mcache {
5453eb2bd662Svikram 	char	*mc_special;
5454eb2bd662Svikram 	char	*mc_mntpt;
5455eb2bd662Svikram 	char	*mc_fstype;
5456eb2bd662Svikram 	struct mcache *mc_next;
5457eb2bd662Svikram } mcache_t;
5458eb2bd662Svikram 
5459eb2bd662Svikram typedef struct mhash {
5460eb2bd662Svikram 	mcache_t *mh_hash[MH_HASH_SZ];
5461eb2bd662Svikram } mhash_t;
5462eb2bd662Svikram 
5463eb2bd662Svikram static int
5464eb2bd662Svikram mhash_fcn(char *key)
5465eb2bd662Svikram {
5466eb2bd662Svikram 	int		i;
5467eb2bd662Svikram 	uint64_t	sum = 0;
5468eb2bd662Svikram 
5469eb2bd662Svikram 	for (i = 0; key[i] != '\0'; i++) {
5470eb2bd662Svikram 		sum += (uchar_t)key[i];
5471eb2bd662Svikram 	}
5472eb2bd662Svikram 
5473eb2bd662Svikram 	sum %= MH_HASH_SZ;
5474eb2bd662Svikram 
5475eb2bd662Svikram 	assert(sum < MH_HASH_SZ);
5476eb2bd662Svikram 
5477eb2bd662Svikram 	return (sum);
5478eb2bd662Svikram }
5479eb2bd662Svikram 
5480eb2bd662Svikram static mhash_t *
5481eb2bd662Svikram cache_mnttab(void)
5482eb2bd662Svikram {
5483eb2bd662Svikram 	FILE		*mfp;
5484eb2bd662Svikram 	struct extmnttab mnt;
5485eb2bd662Svikram 	mcache_t	*mcp;
5486eb2bd662Svikram 	mhash_t		*mhp;
5487eb2bd662Svikram 	char		*ctds;
5488eb2bd662Svikram 	int		idx;
5489eb2bd662Svikram 	int		error;
5490eb2bd662Svikram 	char		*special_dup;
5491eb2bd662Svikram 	const char	*fcn = "cache_mnttab()";
5492eb2bd662Svikram 
5493eb2bd662Svikram 	mfp = fopen(MNTTAB, "r");
5494eb2bd662Svikram 	error = errno;
5495eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_MNTTAB_ERR", mfp = NULL);
5496eb2bd662Svikram 	if (mfp == NULL) {
5497eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
5498eb2bd662Svikram 		return (NULL);
5499eb2bd662Svikram 	}
5500eb2bd662Svikram 
5501eb2bd662Svikram 	mhp = s_calloc(1, sizeof (mhash_t));
5502eb2bd662Svikram 
5503eb2bd662Svikram 	resetmnttab(mfp);
5504eb2bd662Svikram 
5505eb2bd662Svikram 	while (getextmntent(mfp, &mnt, sizeof (mnt)) == 0) {
5506eb2bd662Svikram 		/* only cache ufs */
5507eb2bd662Svikram 		if (strcmp(mnt.mnt_fstype, "ufs") != 0)
5508eb2bd662Svikram 			continue;
5509eb2bd662Svikram 
5510eb2bd662Svikram 		/* basename() modifies its arg, so dup it */
5511eb2bd662Svikram 		special_dup = s_strdup(mnt.mnt_special);
5512eb2bd662Svikram 		ctds = basename(special_dup);
5513eb2bd662Svikram 
5514eb2bd662Svikram 		mcp = s_calloc(1, sizeof (mcache_t));
5515eb2bd662Svikram 		mcp->mc_special = s_strdup(ctds);
5516eb2bd662Svikram 		mcp->mc_mntpt = s_strdup(mnt.mnt_mountp);
5517eb2bd662Svikram 		mcp->mc_fstype = s_strdup(mnt.mnt_fstype);
5518eb2bd662Svikram 		BAM_DPRINTF((D_CACHE_MNTS, fcn, ctds,
5519eb2bd662Svikram 		    mnt.mnt_mountp, mnt.mnt_fstype));
5520eb2bd662Svikram 		idx = mhash_fcn(ctds);
5521eb2bd662Svikram 		mcp->mc_next = mhp->mh_hash[idx];
5522eb2bd662Svikram 		mhp->mh_hash[idx] = mcp;
5523eb2bd662Svikram 		free(special_dup);
5524eb2bd662Svikram 	}
5525eb2bd662Svikram 
5526eb2bd662Svikram 	(void) fclose(mfp);
5527eb2bd662Svikram 
5528eb2bd662Svikram 	return (mhp);
5529eb2bd662Svikram }
5530eb2bd662Svikram 
5531eb2bd662Svikram static void
5532eb2bd662Svikram free_mnttab(mhash_t *mhp)
5533eb2bd662Svikram {
5534eb2bd662Svikram 	mcache_t	*mcp;
5535eb2bd662Svikram 	int		i;
5536eb2bd662Svikram 
5537eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
5538eb2bd662Svikram 		/*LINTED*/
5539eb2bd662Svikram 		while (mcp = mhp->mh_hash[i]) {
5540eb2bd662Svikram 			mhp->mh_hash[i] = mcp->mc_next;
5541eb2bd662Svikram 			free(mcp->mc_special);
5542eb2bd662Svikram 			free(mcp->mc_mntpt);
5543eb2bd662Svikram 			free(mcp->mc_fstype);
5544eb2bd662Svikram 			free(mcp);
5545eb2bd662Svikram 		}
5546eb2bd662Svikram 	}
5547eb2bd662Svikram 
5548eb2bd662Svikram 	for (i = 0; i < MH_HASH_SZ; i++) {
5549eb2bd662Svikram 		assert(mhp->mh_hash[i] == NULL);
5550eb2bd662Svikram 	}
5551eb2bd662Svikram 	free(mhp);
5552eb2bd662Svikram }
5553eb2bd662Svikram 
5554eb2bd662Svikram static mh_search_t
5555eb2bd662Svikram search_hash(mhash_t *mhp, char *special, char **mntpt)
5556eb2bd662Svikram {
5557eb2bd662Svikram 	int		idx;
5558eb2bd662Svikram 	mcache_t	*mcp;
5559eb2bd662Svikram 	const char 	*fcn = "search_hash()";
5560eb2bd662Svikram 
5561eb2bd662Svikram 	assert(mntpt);
5562eb2bd662Svikram 
5563eb2bd662Svikram 	*mntpt = NULL;
5564eb2bd662Svikram 
5565eb2bd662Svikram 	INJECT_ERROR1("SEARCH_HASH_FULL_PATH", special = "/foo");
5566eb2bd662Svikram 	if (strchr(special, '/')) {
5567eb2bd662Svikram 		bam_error(INVALID_MHASH_KEY, special);
5568eb2bd662Svikram 		return (MH_ERROR);
5569eb2bd662Svikram 	}
5570eb2bd662Svikram 
5571eb2bd662Svikram 	idx = mhash_fcn(special);
5572eb2bd662Svikram 
5573eb2bd662Svikram 	for (mcp = mhp->mh_hash[idx]; mcp; mcp = mcp->mc_next) {
5574eb2bd662Svikram 		if (strcmp(mcp->mc_special, special) == 0)
5575eb2bd662Svikram 			break;
5576eb2bd662Svikram 	}
5577eb2bd662Svikram 
5578eb2bd662Svikram 	if (mcp == NULL) {
5579eb2bd662Svikram 		BAM_DPRINTF((D_MNTTAB_HASH_NOMATCH, fcn, special));
5580eb2bd662Svikram 		return (MH_NOMATCH);
5581eb2bd662Svikram 	}
5582eb2bd662Svikram 
5583eb2bd662Svikram 	assert(strcmp(mcp->mc_fstype, "ufs") == 0);
5584eb2bd662Svikram 	*mntpt = mcp->mc_mntpt;
5585eb2bd662Svikram 	BAM_DPRINTF((D_MNTTAB_HASH_MATCH, fcn, special));
5586eb2bd662Svikram 	return (MH_MATCH);
5587eb2bd662Svikram }
5588eb2bd662Svikram 
5589eb2bd662Svikram static int
5590eb2bd662Svikram check_add_ufs_sign_to_list(FILE *tfp, char *mntpt)
5591eb2bd662Svikram {
5592eb2bd662Svikram 	char		*sign;
5593eb2bd662Svikram 	char		*signline;
5594eb2bd662Svikram 	char		signbuf[MAXNAMELEN];
5595eb2bd662Svikram 	int		len;
5596eb2bd662Svikram 	int		error;
5597eb2bd662Svikram 	const char	*fcn = "check_add_ufs_sign_to_list()";
5598eb2bd662Svikram 
5599eb2bd662Svikram 	/* safe to specify NULL as "osdev" arg for UFS */
5600eb2bd662Svikram 	sign = find_existing_sign(mntpt, NULL, "ufs");
5601eb2bd662Svikram 	if (sign == NULL) {
5602eb2bd662Svikram 		/* No existing signature, nothing to add to list */
5603eb2bd662Svikram 		BAM_DPRINTF((D_NO_SIGN_TO_LIST, fcn, mntpt));
5604eb2bd662Svikram 		return (0);
5605eb2bd662Svikram 	}
5606eb2bd662Svikram 
5607eb2bd662Svikram 	(void) snprintf(signbuf, sizeof (signbuf), "%s\n", sign);
5608eb2bd662Svikram 	signline = signbuf;
5609eb2bd662Svikram 
5610eb2bd662Svikram 	INJECT_ERROR1("UFS_MNTPT_SIGN_NOTUFS", signline = "pool_rpool10\n");
5611eb2bd662Svikram 	if (strncmp(signline, GRUBSIGN_UFS_PREFIX,
5612eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX))) {
5613eb2bd662Svikram 		bam_error(INVALID_UFS_SIGNATURE, sign);
5614eb2bd662Svikram 		free(sign);
5615eb2bd662Svikram 		/* ignore invalid signatures */
5616eb2bd662Svikram 		return (0);
5617eb2bd662Svikram 	}
5618eb2bd662Svikram 
5619eb2bd662Svikram 	len = fputs(signline, tfp);
5620eb2bd662Svikram 	error = errno;
5621eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_PUTS_ERROR", len = 0);
5622eb2bd662Svikram 	if (len != strlen(signline)) {
5623eb2bd662Svikram 		bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error));
5624eb2bd662Svikram 		free(sign);
5625eb2bd662Svikram 		return (-1);
5626eb2bd662Svikram 	}
5627eb2bd662Svikram 
5628eb2bd662Svikram 	free(sign);
5629eb2bd662Svikram 
5630eb2bd662Svikram 	BAM_DPRINTF((D_SIGN_LIST_PUTS_DONE, fcn, mntpt));
5631eb2bd662Svikram 	return (0);
5632eb2bd662Svikram }
5633eb2bd662Svikram 
5634eb2bd662Svikram /*
5635eb2bd662Svikram  * slice is a basename not a full pathname
5636eb2bd662Svikram  */
5637eb2bd662Svikram static int
5638eb2bd662Svikram process_slice_common(char *slice, FILE *tfp, mhash_t *mhp, char *tmpmnt)
5639eb2bd662Svikram {
5640eb2bd662Svikram 	int		ret;
5641eb2bd662Svikram 	char		cmd[PATH_MAX];
5642eb2bd662Svikram 	char		path[PATH_MAX];
5643eb2bd662Svikram 	struct stat	sbuf;
5644eb2bd662Svikram 	char		*mntpt;
5645eb2bd662Svikram 	filelist_t	flist = {0};
5646eb2bd662Svikram 	char		*fstype;
5647eb2bd662Svikram 	char		blkslice[PATH_MAX];
5648eb2bd662Svikram 	const char	*fcn = "process_slice_common()";
5649eb2bd662Svikram 
5650eb2bd662Svikram 
5651eb2bd662Svikram 	ret = search_hash(mhp, slice, &mntpt);
5652eb2bd662Svikram 	switch (ret) {
5653eb2bd662Svikram 		case MH_MATCH:
5654eb2bd662Svikram 			if (check_add_ufs_sign_to_list(tfp, mntpt) == -1)
5655eb2bd662Svikram 				return (-1);
5656eb2bd662Svikram 			else
5657eb2bd662Svikram 				return (0);
5658eb2bd662Svikram 		case MH_NOMATCH:
5659eb2bd662Svikram 			break;
5660eb2bd662Svikram 		case MH_ERROR:
5661eb2bd662Svikram 		default:
5662eb2bd662Svikram 			return (-1);
5663eb2bd662Svikram 	}
5664eb2bd662Svikram 
5665eb2bd662Svikram 	(void) snprintf(path, sizeof (path), "/dev/rdsk/%s", slice);
5666eb2bd662Svikram 	if (stat(path, &sbuf) == -1) {
5667eb2bd662Svikram 		BAM_DPRINTF((D_SLICE_ENOENT, fcn, path));
5668eb2bd662Svikram 		return (0);
5669eb2bd662Svikram 	}
5670eb2bd662Svikram 
5671eb2bd662Svikram 	/* Check if ufs */
5672eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5673eb2bd662Svikram 	    "/usr/sbin/fstyp /dev/rdsk/%s 2>/dev/null",
5674eb2bd662Svikram 	    slice);
5675eb2bd662Svikram 
5676eb2bd662Svikram 	if (exec_cmd(cmd, &flist) != 0) {
5677eb2bd662Svikram 		if (bam_verbose)
5678eb2bd662Svikram 			bam_print(FSTYP_FAILED, slice);
5679eb2bd662Svikram 		return (0);
5680eb2bd662Svikram 	}
5681eb2bd662Svikram 
5682eb2bd662Svikram 	if ((flist.head == NULL) || (flist.head != flist.tail)) {
5683eb2bd662Svikram 		if (bam_verbose)
5684eb2bd662Svikram 			bam_print(FSTYP_BAD, slice);
5685eb2bd662Svikram 		filelist_free(&flist);
5686eb2bd662Svikram 		return (0);
5687eb2bd662Svikram 	}
5688eb2bd662Svikram 
5689eb2bd662Svikram 	fstype = strtok(flist.head->line, " \t\n");
5690eb2bd662Svikram 	if (fstype == NULL || strcmp(fstype, "ufs") != 0) {
5691eb2bd662Svikram 		if (bam_verbose)
5692eb2bd662Svikram 			bam_print(NOT_UFS_SLICE, slice, fstype);
5693eb2bd662Svikram 		filelist_free(&flist);
5694eb2bd662Svikram 		return (0);
5695eb2bd662Svikram 	}
5696eb2bd662Svikram 
5697eb2bd662Svikram 	filelist_free(&flist);
5698eb2bd662Svikram 
5699eb2bd662Svikram 	/*
5700eb2bd662Svikram 	 * Since we are mounting the filesystem read-only, the
5701eb2bd662Svikram 	 * the last mount field of the superblock is unchanged
5702eb2bd662Svikram 	 * and does not need to be fixed up post-mount;
5703eb2bd662Svikram 	 */
5704eb2bd662Svikram 
5705eb2bd662Svikram 	(void) snprintf(blkslice, sizeof (blkslice), "/dev/dsk/%s",
5706eb2bd662Svikram 	    slice);
5707eb2bd662Svikram 
5708eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5709eb2bd662Svikram 	    "/usr/sbin/mount -F ufs -o ro %s %s "
5710eb2bd662Svikram 	    "> /dev/null 2>&1", blkslice, tmpmnt);
5711eb2bd662Svikram 
5712eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
5713eb2bd662Svikram 		if (bam_verbose)
5714eb2bd662Svikram 			bam_print(MOUNT_FAILED, blkslice, "ufs");
5715eb2bd662Svikram 		return (0);
5716eb2bd662Svikram 	}
5717eb2bd662Svikram 
5718eb2bd662Svikram 	ret = check_add_ufs_sign_to_list(tfp, tmpmnt);
5719eb2bd662Svikram 
5720eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
5721eb2bd662Svikram 	    "/usr/sbin/umount -f %s > /dev/null 2>&1",
5722eb2bd662Svikram 	    tmpmnt);
5723eb2bd662Svikram 
5724eb2bd662Svikram 	if (exec_cmd(cmd, NULL) != 0) {
5725eb2bd662Svikram 		bam_print(UMOUNT_FAILED, slice);
5726eb2bd662Svikram 		return (0);
5727eb2bd662Svikram 	}
5728eb2bd662Svikram 
5729eb2bd662Svikram 	return (ret);
5730eb2bd662Svikram }
5731eb2bd662Svikram 
5732eb2bd662Svikram static int
5733eb2bd662Svikram process_vtoc_slices(
5734eb2bd662Svikram 	char *s0,
5735eb2bd662Svikram 	struct vtoc *vtoc,
5736eb2bd662Svikram 	FILE *tfp,
5737eb2bd662Svikram 	mhash_t *mhp,
5738eb2bd662Svikram 	char *tmpmnt)
5739eb2bd662Svikram {
5740eb2bd662Svikram 	int		idx;
5741eb2bd662Svikram 	char		slice[PATH_MAX];
5742eb2bd662Svikram 	size_t		len;
5743eb2bd662Svikram 	char		*cp;
5744eb2bd662Svikram 	const char	*fcn = "process_vtoc_slices()";
5745eb2bd662Svikram 
5746eb2bd662Svikram 	len = strlen(s0);
5747eb2bd662Svikram 
5748eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
5749eb2bd662Svikram 
5750eb2bd662Svikram 	s0[len - 1] = '\0';
5751eb2bd662Svikram 
5752eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
5753eb2bd662Svikram 
5754eb2bd662Svikram 	s0[len - 1] = '0';
5755eb2bd662Svikram 
5756eb2bd662Svikram 	cp = slice + len - 1;
5757eb2bd662Svikram 
5758eb2bd662Svikram 	for (idx = 0; idx < vtoc->v_nparts; idx++) {
5759eb2bd662Svikram 
5760eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
5761eb2bd662Svikram 
5762eb2bd662Svikram 		if (vtoc->v_part[idx].p_size == 0) {
5763eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_SIZE_ZERO, fcn, slice));
5764eb2bd662Svikram 			continue;
5765eb2bd662Svikram 		}
5766eb2bd662Svikram 
5767eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
5768eb2bd662Svikram 		switch (vtoc->v_part[idx].p_tag) {
5769eb2bd662Svikram 		case V_SWAP:
5770eb2bd662Svikram 		case V_USR:
5771eb2bd662Svikram 		case V_BACKUP:
5772eb2bd662Svikram 		case V_VAR:
5773eb2bd662Svikram 		case V_HOME:
5774eb2bd662Svikram 		case V_ALTSCTR:
5775eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOT_ROOT_TAG, fcn, slice));
5776eb2bd662Svikram 			continue;
5777eb2bd662Svikram 		default:
5778eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_ROOT_TAG, fcn, slice));
5779eb2bd662Svikram 			break;
5780eb2bd662Svikram 		}
5781eb2bd662Svikram 
5782eb2bd662Svikram 		/* skip unmountable and readonly slices */
5783eb2bd662Svikram 		switch (vtoc->v_part[idx].p_flag) {
5784eb2bd662Svikram 		case V_UNMNT:
5785eb2bd662Svikram 		case V_RONLY:
5786eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOT_RDWR_FLAG, fcn, slice));
5787eb2bd662Svikram 			continue;
5788eb2bd662Svikram 		default:
5789eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_RDWR_FLAG, fcn, slice));
5790eb2bd662Svikram 			break;
5791eb2bd662Svikram 		}
5792eb2bd662Svikram 
5793eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
5794eb2bd662Svikram 			return (-1);
5795eb2bd662Svikram 		}
5796eb2bd662Svikram 	}
5797eb2bd662Svikram 
5798eb2bd662Svikram 	return (0);
5799eb2bd662Svikram }
5800eb2bd662Svikram 
5801eb2bd662Svikram static int
5802eb2bd662Svikram process_efi_slices(
5803eb2bd662Svikram 	char *s0,
5804eb2bd662Svikram 	struct dk_gpt *efi,
5805eb2bd662Svikram 	FILE *tfp,
5806eb2bd662Svikram 	mhash_t *mhp,
5807eb2bd662Svikram 	char *tmpmnt)
5808eb2bd662Svikram {
5809eb2bd662Svikram 	int		idx;
5810eb2bd662Svikram 	char		slice[PATH_MAX];
5811eb2bd662Svikram 	size_t		len;
5812eb2bd662Svikram 	char		*cp;
5813eb2bd662Svikram 	const char	*fcn = "process_efi_slices()";
5814eb2bd662Svikram 
5815eb2bd662Svikram 	len = strlen(s0);
5816eb2bd662Svikram 
5817eb2bd662Svikram 	assert(s0[len - 2] == 's' && s0[len - 1] == '0');
5818eb2bd662Svikram 
5819eb2bd662Svikram 	s0[len - 1] = '\0';
5820eb2bd662Svikram 
5821eb2bd662Svikram 	(void) strlcpy(slice, s0, sizeof (slice));
5822eb2bd662Svikram 
5823eb2bd662Svikram 	s0[len - 1] = '0';
5824eb2bd662Svikram 
5825eb2bd662Svikram 	cp = slice + len - 1;
5826eb2bd662Svikram 
5827eb2bd662Svikram 	for (idx = 0; idx < efi->efi_nparts; idx++) {
5828eb2bd662Svikram 
5829eb2bd662Svikram 		(void) snprintf(cp, sizeof (slice) - (len - 1), "%u", idx);
5830eb2bd662Svikram 
5831eb2bd662Svikram 		if (efi->efi_parts[idx].p_size == 0) {
5832eb2bd662Svikram 			BAM_DPRINTF((D_EFI_SIZE_ZERO, fcn, slice));
5833eb2bd662Svikram 			continue;
5834eb2bd662Svikram 		}
5835eb2bd662Svikram 
5836eb2bd662Svikram 		/* Skip "SWAP", "USR", "BACKUP", "VAR", "HOME", "ALTSCTR" */
5837eb2bd662Svikram 		switch (efi->efi_parts[idx].p_tag) {
5838eb2bd662Svikram 		case V_SWAP:
5839eb2bd662Svikram 		case V_USR:
5840eb2bd662Svikram 		case V_BACKUP:
5841eb2bd662Svikram 		case V_VAR:
5842eb2bd662Svikram 		case V_HOME:
5843eb2bd662Svikram 		case V_ALTSCTR:
5844eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOT_ROOT_TAG, fcn, slice));
5845eb2bd662Svikram 			continue;
5846eb2bd662Svikram 		default:
5847eb2bd662Svikram 			BAM_DPRINTF((D_EFI_ROOT_TAG, fcn, slice));
5848eb2bd662Svikram 			break;
5849eb2bd662Svikram 		}
5850eb2bd662Svikram 
5851eb2bd662Svikram 		/* skip unmountable and readonly slices */
5852eb2bd662Svikram 		switch (efi->efi_parts[idx].p_flag) {
5853eb2bd662Svikram 		case V_UNMNT:
5854eb2bd662Svikram 		case V_RONLY:
5855eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOT_RDWR_FLAG, fcn, slice));
5856eb2bd662Svikram 			continue;
5857eb2bd662Svikram 		default:
5858eb2bd662Svikram 			BAM_DPRINTF((D_EFI_RDWR_FLAG, fcn, slice));
5859eb2bd662Svikram 			break;
5860eb2bd662Svikram 		}
5861eb2bd662Svikram 
5862eb2bd662Svikram 		if (process_slice_common(slice, tfp, mhp, tmpmnt) == -1) {
5863eb2bd662Svikram 			return (-1);
5864eb2bd662Svikram 		}
5865eb2bd662Svikram 	}
5866eb2bd662Svikram 
5867eb2bd662Svikram 	return (0);
5868eb2bd662Svikram }
5869eb2bd662Svikram 
5870eb2bd662Svikram /*
5871eb2bd662Svikram  * s0 is a basename not a full path
5872eb2bd662Svikram  */
5873eb2bd662Svikram static int
5874eb2bd662Svikram process_slice0(char *s0, FILE *tfp, mhash_t *mhp, char *tmpmnt)
5875eb2bd662Svikram {
5876eb2bd662Svikram 	struct vtoc		vtoc;
5877eb2bd662Svikram 	struct dk_gpt		*efi;
5878eb2bd662Svikram 	char			s0path[PATH_MAX];
5879eb2bd662Svikram 	struct stat		sbuf;
5880eb2bd662Svikram 	int			e_flag;
5881eb2bd662Svikram 	int			v_flag;
5882eb2bd662Svikram 	int			retval;
5883eb2bd662Svikram 	int			err;
5884eb2bd662Svikram 	int			fd;
5885eb2bd662Svikram 	const char		*fcn = "process_slice0()";
5886eb2bd662Svikram 
5887eb2bd662Svikram 	(void) snprintf(s0path, sizeof (s0path), "/dev/rdsk/%s", s0);
5888eb2bd662Svikram 
5889eb2bd662Svikram 	if (stat(s0path, &sbuf) == -1) {
5890eb2bd662Svikram 		BAM_DPRINTF((D_SLICE0_ENOENT, fcn, s0path));
5891eb2bd662Svikram 		return (0);
5892eb2bd662Svikram 	}
5893eb2bd662Svikram 
5894eb2bd662Svikram 	fd = open(s0path, O_NONBLOCK|O_RDONLY);
5895eb2bd662Svikram 	if (fd == -1) {
5896eb2bd662Svikram 		bam_error(OPEN_FAIL, s0path, strerror(errno));
5897eb2bd662Svikram 		return (0);
5898eb2bd662Svikram 	}
5899eb2bd662Svikram 
5900eb2bd662Svikram 	e_flag = v_flag = 0;
5901eb2bd662Svikram 	retval = ((err = read_vtoc(fd, &vtoc)) >= 0) ? 0 : err;
5902eb2bd662Svikram 	switch (retval) {
5903eb2bd662Svikram 		case VT_EIO:
5904eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_READ_FAIL, fcn, s0path));
5905eb2bd662Svikram 			break;
5906eb2bd662Svikram 		case VT_EINVAL:
5907eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_INVALID, fcn, s0path));
5908eb2bd662Svikram 			break;
5909eb2bd662Svikram 		case VT_ERROR:
5910eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_UNKNOWN_ERR, fcn, s0path));
5911eb2bd662Svikram 			break;
5912eb2bd662Svikram 		case VT_ENOTSUP:
5913eb2bd662Svikram 			e_flag = 1;
5914eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_NOTSUP, fcn, s0path));
5915eb2bd662Svikram 			break;
5916eb2bd662Svikram 		case 0:
5917eb2bd662Svikram 			v_flag = 1;
5918eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_READ_SUCCESS, fcn, s0path));
5919eb2bd662Svikram 			break;
5920eb2bd662Svikram 		default:
5921eb2bd662Svikram 			BAM_DPRINTF((D_VTOC_UNKNOWN_RETCODE, fcn, s0path));
5922eb2bd662Svikram 			break;
5923eb2bd662Svikram 	}
5924eb2bd662Svikram 
5925eb2bd662Svikram 
5926eb2bd662Svikram 	if (e_flag) {
5927eb2bd662Svikram 		e_flag = 0;
5928eb2bd662Svikram 		retval = ((err = efi_alloc_and_read(fd, &efi)) >= 0) ? 0 : err;
5929eb2bd662Svikram 		switch (retval) {
5930eb2bd662Svikram 		case VT_EIO:
5931eb2bd662Svikram 			BAM_DPRINTF((D_EFI_READ_FAIL, fcn, s0path));
5932eb2bd662Svikram 			break;
5933eb2bd662Svikram 		case VT_EINVAL:
5934eb2bd662Svikram 			BAM_DPRINTF((D_EFI_INVALID, fcn, s0path));
5935eb2bd662Svikram 			break;
5936eb2bd662Svikram 		case VT_ERROR:
5937eb2bd662Svikram 			BAM_DPRINTF((D_EFI_UNKNOWN_ERR, fcn, s0path));
5938eb2bd662Svikram 			break;
5939eb2bd662Svikram 		case VT_ENOTSUP:
5940eb2bd662Svikram 			BAM_DPRINTF((D_EFI_NOTSUP, fcn, s0path));
5941eb2bd662Svikram 			break;
5942eb2bd662Svikram 		case 0:
5943eb2bd662Svikram 			e_flag = 1;
5944eb2bd662Svikram 			BAM_DPRINTF((D_EFI_READ_SUCCESS, fcn, s0path));
5945eb2bd662Svikram 			break;
5946eb2bd662Svikram 		default:
5947eb2bd662Svikram 			BAM_DPRINTF((D_EFI_UNKNOWN_RETCODE, fcn, s0path));
5948eb2bd662Svikram 			break;
5949eb2bd662Svikram 		}
5950eb2bd662Svikram 	}
5951eb2bd662Svikram 
5952eb2bd662Svikram 	(void) close(fd);
5953eb2bd662Svikram 
5954eb2bd662Svikram 	if (v_flag) {
5955eb2bd662Svikram 		retval = process_vtoc_slices(s0,
5956eb2bd662Svikram 		    &vtoc, tfp, mhp, tmpmnt);
5957eb2bd662Svikram 	} else if (e_flag) {
5958eb2bd662Svikram 		retval = process_efi_slices(s0,
5959eb2bd662Svikram 		    efi, tfp, mhp, tmpmnt);
5960eb2bd662Svikram 	} else {
5961eb2bd662Svikram 		BAM_DPRINTF((D_NOT_VTOC_OR_EFI, fcn, s0path));
5962eb2bd662Svikram 		return (0);
5963eb2bd662Svikram 	}
5964eb2bd662Svikram 
5965eb2bd662Svikram 	return (retval);
5966eb2bd662Svikram }
5967eb2bd662Svikram 
5968eb2bd662Svikram /*
5969eb2bd662Svikram  * Find and create a list of all existing UFS boot signatures
5970eb2bd662Svikram  */
5971eb2bd662Svikram static int
5972eb2bd662Svikram FindAllUfsSignatures(void)
5973eb2bd662Svikram {
5974eb2bd662Svikram 	mhash_t		*mnttab_hash;
5975eb2bd662Svikram 	DIR		*dirp = NULL;
5976eb2bd662Svikram 	struct dirent	*dp;
5977eb2bd662Svikram 	char		tmpmnt[PATH_MAX];
5978eb2bd662Svikram 	char		cmd[PATH_MAX];
5979eb2bd662Svikram 	struct stat	sb;
5980eb2bd662Svikram 	int		fd;
5981eb2bd662Svikram 	FILE		*tfp;
5982eb2bd662Svikram 	size_t		len;
5983eb2bd662Svikram 	int		ret;
5984eb2bd662Svikram 	int		error;
5985eb2bd662Svikram 	const char	*fcn = "FindAllUfsSignatures()";
5986eb2bd662Svikram 
5987eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) != -1)  {
5988eb2bd662Svikram 		bam_print(SIGNATURE_LIST_EXISTS, UFS_SIGNATURE_LIST);
5989eb2bd662Svikram 		return (0);
5990eb2bd662Svikram 	}
5991eb2bd662Svikram 
5992eb2bd662Svikram 	fd = open(UFS_SIGNATURE_LIST".tmp",
5993eb2bd662Svikram 	    O_RDWR|O_CREAT|O_TRUNC, 0644);
5994eb2bd662Svikram 	error = errno;
5995eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_TRUNC", fd = -1);
5996eb2bd662Svikram 	if (fd == -1) {
5997eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
5998eb2bd662Svikram 		return (-1);
5999eb2bd662Svikram 	}
6000eb2bd662Svikram 
6001eb2bd662Svikram 	ret = close(fd);
6002eb2bd662Svikram 	error = errno;
6003eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_TMP_CLOSE", ret = -1);
6004eb2bd662Svikram 	if (ret == -1) {
6005eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
6006eb2bd662Svikram 		    strerror(error));
6007eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6008eb2bd662Svikram 		return (-1);
6009eb2bd662Svikram 	}
6010eb2bd662Svikram 
6011eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
6012eb2bd662Svikram 	error = errno;
6013eb2bd662Svikram 	INJECT_ERROR1("SIGN_LIST_APPEND_FOPEN", tfp = NULL);
6014eb2bd662Svikram 	if (tfp == NULL) {
6015eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
6016eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6017eb2bd662Svikram 		return (-1);
6018eb2bd662Svikram 	}
6019eb2bd662Svikram 
6020eb2bd662Svikram 	mnttab_hash = cache_mnttab();
6021eb2bd662Svikram 	INJECT_ERROR1("CACHE_MNTTAB_ERROR", mnttab_hash = NULL);
6022eb2bd662Svikram 	if (mnttab_hash == NULL) {
6023eb2bd662Svikram 		(void) fclose(tfp);
6024eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6025eb2bd662Svikram 		bam_error(CACHE_MNTTAB_FAIL, fcn);
6026eb2bd662Svikram 		return (-1);
6027eb2bd662Svikram 	}
6028eb2bd662Svikram 
6029eb2bd662Svikram 	(void) snprintf(tmpmnt, sizeof (tmpmnt),
6030eb2bd662Svikram 	    "/tmp/bootadm_ufs_sign_mnt.%d", getpid());
6031eb2bd662Svikram 	(void) unlink(tmpmnt);
6032eb2bd662Svikram 
603348847494SEnrico Perla - Sun Microsystems 	ret = mkdirp(tmpmnt, DIR_PERMS);
6034eb2bd662Svikram 	error = errno;
6035eb2bd662Svikram 	INJECT_ERROR1("MKDIRP_SIGN_MNT", ret = -1);
6036eb2bd662Svikram 	if (ret == -1) {
6037eb2bd662Svikram 		bam_error(MKDIR_FAILED, tmpmnt, strerror(error));
6038eb2bd662Svikram 		free_mnttab(mnttab_hash);
6039eb2bd662Svikram 		(void) fclose(tfp);
6040eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6041eb2bd662Svikram 		return (-1);
6042eb2bd662Svikram 	}
6043eb2bd662Svikram 
6044eb2bd662Svikram 	dirp = opendir("/dev/rdsk");
6045eb2bd662Svikram 	error = errno;
6046eb2bd662Svikram 	INJECT_ERROR1("OPENDIR_DEV_RDSK", dirp = NULL);
6047eb2bd662Svikram 	if (dirp == NULL) {
6048eb2bd662Svikram 		bam_error(OPENDIR_FAILED, "/dev/rdsk", strerror(error));
6049eb2bd662Svikram 		goto fail;
6050eb2bd662Svikram 	}
6051eb2bd662Svikram 
6052eb2bd662Svikram 	while (dp = readdir(dirp)) {
6053eb2bd662Svikram 		if (strcmp(dp->d_name, ".") == 0 ||
6054eb2bd662Svikram 		    strcmp(dp->d_name, "..") == 0)
6055eb2bd662Svikram 			continue;
6056eb2bd662Svikram 
6057eb2bd662Svikram 		/*
6058eb2bd662Svikram 		 * we only look for the s0 slice. This is guranteed to
6059eb2bd662Svikram 		 * have 's' at len - 2.
6060eb2bd662Svikram 		 */
6061eb2bd662Svikram 		len = strlen(dp->d_name);
6062eb2bd662Svikram 		if (dp->d_name[len - 2 ] != 's' || dp->d_name[len - 1] != '0') {
6063eb2bd662Svikram 			BAM_DPRINTF((D_SKIP_SLICE_NOTZERO, fcn, dp->d_name));
6064eb2bd662Svikram 			continue;
6065eb2bd662Svikram 		}
6066eb2bd662Svikram 
6067eb2bd662Svikram 		ret = process_slice0(dp->d_name, tfp, mnttab_hash, tmpmnt);
6068eb2bd662Svikram 		INJECT_ERROR1("PROCESS_S0_FAIL", ret = -1);
6069eb2bd662Svikram 		if (ret == -1)
6070eb2bd662Svikram 			goto fail;
6071eb2bd662Svikram 	}
6072eb2bd662Svikram 
6073eb2bd662Svikram 	(void) closedir(dirp);
6074eb2bd662Svikram 	free_mnttab(mnttab_hash);
6075eb2bd662Svikram 	(void) rmdir(tmpmnt);
6076eb2bd662Svikram 
6077eb2bd662Svikram 	ret = fclose(tfp);
6078eb2bd662Svikram 	error = errno;
6079eb2bd662Svikram 	INJECT_ERROR1("FCLOSE_SIGNLIST_TMP", ret = EOF);
6080eb2bd662Svikram 	if (ret == EOF) {
6081eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
6082eb2bd662Svikram 		    strerror(error));
6083eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6084eb2bd662Svikram 		return (-1);
6085eb2bd662Svikram 	}
6086eb2bd662Svikram 
6087eb2bd662Svikram 	/* We have a list of existing GRUB signatures. Sort it first */
6088eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6089eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
6090eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
6091eb2bd662Svikram 
6092eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
6093eb2bd662Svikram 	INJECT_ERROR1("SORT_SIGN_LIST", ret = 1);
6094eb2bd662Svikram 	if (ret != 0) {
6095eb2bd662Svikram 		bam_error(GRUBSIGN_SORT_FAILED);
6096eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6097eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6098eb2bd662Svikram 		return (-1);
6099eb2bd662Svikram 	}
6100eb2bd662Svikram 
6101eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6102eb2bd662Svikram 
6103eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
6104eb2bd662Svikram 	error = errno;
6105eb2bd662Svikram 	INJECT_ERROR1("RENAME_TMP_SIGNLIST", ret = -1);
6106eb2bd662Svikram 	if (ret == -1) {
6107eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error));
6108eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6109eb2bd662Svikram 		return (-1);
6110eb2bd662Svikram 	}
6111eb2bd662Svikram 
6112eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == 0 && sb.st_size == 0) {
6113eb2bd662Svikram 		BAM_DPRINTF((D_ZERO_LEN_SIGNLIST, fcn, UFS_SIGNATURE_LIST));
6114eb2bd662Svikram 	}
6115eb2bd662Svikram 
6116eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6117eb2bd662Svikram 	return (0);
6118eb2bd662Svikram 
6119eb2bd662Svikram fail:
6120eb2bd662Svikram 	if (dirp)
6121eb2bd662Svikram 		(void) closedir(dirp);
6122eb2bd662Svikram 	free_mnttab(mnttab_hash);
6123eb2bd662Svikram 	(void) rmdir(tmpmnt);
6124eb2bd662Svikram 	(void) fclose(tfp);
6125eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6126eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6127eb2bd662Svikram 	return (-1);
6128eb2bd662Svikram }
6129eb2bd662Svikram 
6130eb2bd662Svikram static char *
6131eb2bd662Svikram create_ufs_sign(void)
6132eb2bd662Svikram {
6133eb2bd662Svikram 	struct stat	sb;
6134eb2bd662Svikram 	int		signnum = -1;
6135eb2bd662Svikram 	char		tmpsign[MAXNAMELEN + 1];
6136eb2bd662Svikram 	char		*numstr;
6137eb2bd662Svikram 	int		i;
6138eb2bd662Svikram 	FILE		*tfp;
6139eb2bd662Svikram 	int		ret;
6140eb2bd662Svikram 	int		error;
6141eb2bd662Svikram 	const char	*fcn = "create_ufs_sign()";
6142eb2bd662Svikram 
6143eb2bd662Svikram 	bam_print(SEARCHING_UFS_SIGN);
6144eb2bd662Svikram 
6145eb2bd662Svikram 	ret = FindAllUfsSignatures();
6146eb2bd662Svikram 	INJECT_ERROR1("FIND_ALL_UFS", ret = -1);
6147eb2bd662Svikram 	if (ret == -1) {
6148eb2bd662Svikram 		bam_error(ERR_FIND_UFS_SIGN);
6149eb2bd662Svikram 		return (NULL);
6150eb2bd662Svikram 	}
6151eb2bd662Svikram 
6152eb2bd662Svikram 	/* Make sure the list exists and is owned by root */
6153eb2bd662Svikram 	INJECT_ERROR1("SIGNLIST_NOT_CREATED",
6154eb2bd662Svikram 	    (void) unlink(UFS_SIGNATURE_LIST));
6155eb2bd662Svikram 	if (stat(UFS_SIGNATURE_LIST, &sb) == -1 || sb.st_uid != 0) {
6156eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6157eb2bd662Svikram 		bam_error(UFS_SIGNATURE_LIST_MISS, UFS_SIGNATURE_LIST);
6158eb2bd662Svikram 		return (NULL);
6159eb2bd662Svikram 	}
6160eb2bd662Svikram 
6161eb2bd662Svikram 	if (sb.st_size == 0) {
6162eb2bd662Svikram 		bam_print(GRUBSIGN_UFS_NONE);
6163eb2bd662Svikram 		i = 0;
6164eb2bd662Svikram 		goto found;
6165eb2bd662Svikram 	}
6166eb2bd662Svikram 
6167eb2bd662Svikram 	/* The signature list was sorted when it was created */
6168eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST, "r");
6169eb2bd662Svikram 	error = errno;
6170eb2bd662Svikram 	INJECT_ERROR1("FOPEN_SIGN_LIST", tfp = NULL);
6171eb2bd662Svikram 	if (tfp == NULL) {
6172eb2bd662Svikram 		bam_error(UFS_SIGNATURE_LIST_OPENERR,
6173eb2bd662Svikram 		    UFS_SIGNATURE_LIST, strerror(error));
6174eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6175eb2bd662Svikram 		return (NULL);
6176eb2bd662Svikram 	}
6177eb2bd662Svikram 
6178eb2bd662Svikram 	for (i = 0; s_fgets(tmpsign, sizeof (tmpsign), tfp); i++) {
6179eb2bd662Svikram 
6180eb2bd662Svikram 		if (strncmp(tmpsign, GRUBSIGN_UFS_PREFIX,
6181eb2bd662Svikram 		    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
6182eb2bd662Svikram 			(void) fclose(tfp);
6183eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6184eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6185eb2bd662Svikram 			return (NULL);
6186eb2bd662Svikram 		}
6187eb2bd662Svikram 		numstr = tmpsign + strlen(GRUBSIGN_UFS_PREFIX);
6188eb2bd662Svikram 
6189eb2bd662Svikram 		if (numstr[0] == '\0' || !isdigit(numstr[0])) {
6190eb2bd662Svikram 			(void) fclose(tfp);
6191eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6192eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6193eb2bd662Svikram 			return (NULL);
6194eb2bd662Svikram 		}
6195eb2bd662Svikram 
6196eb2bd662Svikram 		signnum = atoi(numstr);
6197eb2bd662Svikram 		INJECT_ERROR1("NEGATIVE_SIGN", signnum = -1);
6198eb2bd662Svikram 		if (signnum < 0) {
6199eb2bd662Svikram 			(void) fclose(tfp);
6200eb2bd662Svikram 			(void) unlink(UFS_SIGNATURE_LIST);
6201eb2bd662Svikram 			bam_error(UFS_BADSIGN, tmpsign);
6202eb2bd662Svikram 			return (NULL);
6203eb2bd662Svikram 		}
6204eb2bd662Svikram 
6205eb2bd662Svikram 		if (i != signnum) {
6206eb2bd662Svikram 			BAM_DPRINTF((D_FOUND_HOLE_SIGNLIST, fcn, i));
6207eb2bd662Svikram 			break;
6208eb2bd662Svikram 		}
6209eb2bd662Svikram 	}
6210eb2bd662Svikram 
6211eb2bd662Svikram 	(void) fclose(tfp);
6212eb2bd662Svikram 
6213eb2bd662Svikram found:
6214eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "rootfs%d", i);
6215eb2bd662Svikram 
6216eb2bd662Svikram 	/* add the ufs signature to the /var/run list of signatures */
6217eb2bd662Svikram 	ret = ufs_add_to_sign_list(tmpsign);
6218eb2bd662Svikram 	INJECT_ERROR1("UFS_ADD_TO_SIGN_LIST", ret = -1);
6219eb2bd662Svikram 	if (ret == -1) {
6220eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6221eb2bd662Svikram 		bam_error(FAILED_ADD_SIGNLIST, tmpsign);
6222eb2bd662Svikram 		return (NULL);
6223eb2bd662Svikram 	}
6224eb2bd662Svikram 
6225eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6226eb2bd662Svikram 
6227eb2bd662Svikram 	return (s_strdup(tmpsign));
6228eb2bd662Svikram }
6229eb2bd662Svikram 
6230eb2bd662Svikram static char *
6231eb2bd662Svikram get_fstype(char *osroot)
6232eb2bd662Svikram {
6233eb2bd662Svikram 	FILE		*mntfp;
6234eb2bd662Svikram 	struct mnttab	mp = {0};
6235eb2bd662Svikram 	struct mnttab	mpref = {0};
6236eb2bd662Svikram 	int		error;
6237eb2bd662Svikram 	int		ret;
6238eb2bd662Svikram 	const char	*fcn = "get_fstype()";
6239eb2bd662Svikram 
6240eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_OSROOT", osroot = NULL);
6241eb2bd662Svikram 	if (osroot == NULL) {
6242eb2bd662Svikram 		bam_error(GET_FSTYPE_ARGS);
6243eb2bd662Svikram 		return (NULL);
6244eb2bd662Svikram 	}
6245eb2bd662Svikram 
6246eb2bd662Svikram 	mntfp = fopen(MNTTAB, "r");
6247eb2bd662Svikram 	error = errno;
6248eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_FOPEN", mntfp = NULL);
6249eb2bd662Svikram 	if (mntfp == NULL) {
6250eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
6251eb2bd662Svikram 		return (NULL);
6252eb2bd662Svikram 	}
6253eb2bd662Svikram 
6254eb2bd662Svikram 	if (*osroot == '\0')
6255eb2bd662Svikram 		mpref.mnt_mountp = "/";
6256eb2bd662Svikram 	else
6257eb2bd662Svikram 		mpref.mnt_mountp = osroot;
6258eb2bd662Svikram 
6259eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
6260eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_GETMNTANY", ret = 1);
6261eb2bd662Svikram 	if (ret != 0) {
6262eb2bd662Svikram 		bam_error(MNTTAB_MNTPT_NOT_FOUND, osroot, MNTTAB);
6263eb2bd662Svikram 		(void) fclose(mntfp);
6264eb2bd662Svikram 		return (NULL);
6265eb2bd662Svikram 	}
6266eb2bd662Svikram 	(void) fclose(mntfp);
6267eb2bd662Svikram 
6268eb2bd662Svikram 	INJECT_ERROR1("GET_FSTYPE_NULL", mp.mnt_fstype = NULL);
6269eb2bd662Svikram 	if (mp.mnt_fstype == NULL) {
6270eb2bd662Svikram 		bam_error(MNTTAB_FSTYPE_NULL, osroot);
6271eb2bd662Svikram 		return (NULL);
6272eb2bd662Svikram 	}
6273eb2bd662Svikram 
6274eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6275eb2bd662Svikram 
6276eb2bd662Svikram 	return (s_strdup(mp.mnt_fstype));
6277eb2bd662Svikram }
6278eb2bd662Svikram 
6279eb2bd662Svikram static char *
6280eb2bd662Svikram create_zfs_sign(char *osdev)
6281eb2bd662Svikram {
6282eb2bd662Svikram 	char		tmpsign[PATH_MAX];
6283eb2bd662Svikram 	char		*pool;
6284eb2bd662Svikram 	const char	*fcn = "create_zfs_sign()";
6285eb2bd662Svikram 
6286eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, osdev));
6287eb2bd662Svikram 
6288eb2bd662Svikram 	/*
6289eb2bd662Svikram 	 * First find the pool name
6290eb2bd662Svikram 	 */
6291eb2bd662Svikram 	pool = get_pool(osdev);
6292eb2bd662Svikram 	INJECT_ERROR1("CREATE_ZFS_SIGN_GET_POOL", pool = NULL);
6293eb2bd662Svikram 	if (pool == NULL) {
6294eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
6295eb2bd662Svikram 		return (NULL);
6296eb2bd662Svikram 	}
6297eb2bd662Svikram 
6298eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "pool_%s", pool);
6299eb2bd662Svikram 
6300eb2bd662Svikram 	BAM_DPRINTF((D_CREATED_ZFS_SIGN, fcn, tmpsign));
6301eb2bd662Svikram 
6302eb2bd662Svikram 	free(pool);
6303eb2bd662Svikram 
6304eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6305eb2bd662Svikram 
6306eb2bd662Svikram 	return (s_strdup(tmpsign));
6307eb2bd662Svikram }
6308eb2bd662Svikram 
6309eb2bd662Svikram static char *
6310eb2bd662Svikram create_new_sign(char *osdev, char *fstype)
6311eb2bd662Svikram {
6312eb2bd662Svikram 	char		*sign;
6313eb2bd662Svikram 	const char	*fcn = "create_new_sign()";
6314eb2bd662Svikram 
6315eb2bd662Svikram 	INJECT_ERROR1("NEW_SIGN_FSTYPE", fstype = "foofs");
6316eb2bd662Svikram 
6317eb2bd662Svikram 	if (strcmp(fstype, "zfs") == 0) {
6318eb2bd662Svikram 		BAM_DPRINTF((D_CREATE_NEW_ZFS, fcn));
6319eb2bd662Svikram 		sign = create_zfs_sign(osdev);
6320eb2bd662Svikram 	} else if (strcmp(fstype, "ufs") == 0) {
6321eb2bd662Svikram 		BAM_DPRINTF((D_CREATE_NEW_UFS, fcn));
6322eb2bd662Svikram 		sign = create_ufs_sign();
6323eb2bd662Svikram 	} else {
6324eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
6325eb2bd662Svikram 		sign = NULL;
6326eb2bd662Svikram 	}
6327eb2bd662Svikram 
6328eb2bd662Svikram 	BAM_DPRINTF((D_CREATED_NEW_SIGN, fcn, sign ? sign : "<NULL>"));
6329eb2bd662Svikram 	return (sign);
6330eb2bd662Svikram }
6331eb2bd662Svikram 
6332eb2bd662Svikram static int
6333eb2bd662Svikram set_backup_common(char *mntpt, char *sign)
6334eb2bd662Svikram {
6335eb2bd662Svikram 	FILE		*bfp;
6336eb2bd662Svikram 	char		backup[PATH_MAX];
6337eb2bd662Svikram 	char		tmpsign[PATH_MAX];
6338eb2bd662Svikram 	int		error;
6339eb2bd662Svikram 	char		*bdir;
6340eb2bd662Svikram 	char		*backup_dup;
6341eb2bd662Svikram 	struct stat	sb;
6342eb2bd662Svikram 	int		ret;
6343eb2bd662Svikram 	const char	*fcn = "set_backup_common()";
6344eb2bd662Svikram 
6345eb2bd662Svikram 	(void) snprintf(backup, sizeof (backup), "%s%s",
6346eb2bd662Svikram 	    mntpt, GRUBSIGN_BACKUP);
6347eb2bd662Svikram 
6348eb2bd662Svikram 	/* First read the backup */
6349eb2bd662Svikram 	bfp = fopen(backup, "r");
6350eb2bd662Svikram 	if (bfp != NULL) {
6351eb2bd662Svikram 		while (s_fgets(tmpsign, sizeof (tmpsign), bfp)) {
6352eb2bd662Svikram 			if (strcmp(tmpsign, sign) == 0) {
6353eb2bd662Svikram 				BAM_DPRINTF((D_FOUND_IN_BACKUP, fcn, sign));
6354eb2bd662Svikram 				(void) fclose(bfp);
6355eb2bd662Svikram 				return (0);
6356eb2bd662Svikram 			}
6357eb2bd662Svikram 		}
6358eb2bd662Svikram 		(void) fclose(bfp);
6359eb2bd662Svikram 		BAM_DPRINTF((D_NOT_FOUND_IN_EXIST_BACKUP, fcn, sign));
6360eb2bd662Svikram 	} else {
6361eb2bd662Svikram 		BAM_DPRINTF((D_BACKUP_NOT_EXIST, fcn, backup));
6362eb2bd662Svikram 	}
6363eb2bd662Svikram 
6364eb2bd662Svikram 	/*
6365eb2bd662Svikram 	 * Didn't find the correct signature. First create
6366eb2bd662Svikram 	 * the directory if necessary.
6367eb2bd662Svikram 	 */
6368eb2bd662Svikram 
6369eb2bd662Svikram 	/* dirname() modifies its argument so dup it */
6370eb2bd662Svikram 	backup_dup = s_strdup(backup);
6371eb2bd662Svikram 	bdir = dirname(backup_dup);
6372eb2bd662Svikram 	assert(bdir);
6373eb2bd662Svikram 
6374eb2bd662Svikram 	ret = stat(bdir, &sb);
6375eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_STAT", ret = -1);
6376eb2bd662Svikram 	if (ret == -1) {
6377eb2bd662Svikram 		BAM_DPRINTF((D_BACKUP_DIR_NOEXIST, fcn, bdir));
637848847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(bdir, DIR_PERMS);
6379eb2bd662Svikram 		error = errno;
6380eb2bd662Svikram 		INJECT_ERROR1("SET_BACKUP_MKDIRP", ret = -1);
6381eb2bd662Svikram 		if (ret == -1) {
6382eb2bd662Svikram 			bam_error(GRUBSIGN_BACKUP_MKDIRERR,
6383eb2bd662Svikram 			    GRUBSIGN_BACKUP, strerror(error));
6384eb2bd662Svikram 			free(backup_dup);
6385eb2bd662Svikram 			return (-1);
6386eb2bd662Svikram 		}
6387eb2bd662Svikram 	}
6388eb2bd662Svikram 	free(backup_dup);
6389eb2bd662Svikram 
6390eb2bd662Svikram 	/*
6391eb2bd662Svikram 	 * Open the backup in append mode to add the correct
6392eb2bd662Svikram 	 * signature;
6393eb2bd662Svikram 	 */
6394eb2bd662Svikram 	bfp = fopen(backup, "a");
6395eb2bd662Svikram 	error = errno;
6396eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FOPEN_A", bfp = NULL);
6397eb2bd662Svikram 	if (bfp == NULL) {
6398eb2bd662Svikram 		bam_error(GRUBSIGN_BACKUP_OPENERR,
6399eb2bd662Svikram 		    GRUBSIGN_BACKUP, strerror(error));
6400eb2bd662Svikram 		return (-1);
6401eb2bd662Svikram 	}
6402eb2bd662Svikram 
6403eb2bd662Svikram 	(void) snprintf(tmpsign, sizeof (tmpsign), "%s\n", sign);
6404eb2bd662Svikram 
6405eb2bd662Svikram 	ret = fputs(tmpsign, bfp);
6406eb2bd662Svikram 	error = errno;
6407eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FPUTS", ret = 0);
6408eb2bd662Svikram 	if (ret != strlen(tmpsign)) {
6409eb2bd662Svikram 		bam_error(GRUBSIGN_BACKUP_WRITEERR,
6410eb2bd662Svikram 		    GRUBSIGN_BACKUP, strerror(error));
6411eb2bd662Svikram 		(void) fclose(bfp);
6412eb2bd662Svikram 		return (-1);
6413eb2bd662Svikram 	}
6414eb2bd662Svikram 
6415eb2bd662Svikram 	(void) fclose(bfp);
6416eb2bd662Svikram 
6417eb2bd662Svikram 	if (bam_verbose)
6418eb2bd662Svikram 		bam_print(GRUBSIGN_BACKUP_UPDATED, GRUBSIGN_BACKUP);
6419eb2bd662Svikram 
6420eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6421eb2bd662Svikram 
6422eb2bd662Svikram 	return (0);
6423eb2bd662Svikram }
6424eb2bd662Svikram 
6425eb2bd662Svikram static int
6426eb2bd662Svikram set_backup_ufs(char *osroot, char *sign)
6427eb2bd662Svikram {
6428eb2bd662Svikram 	const char	*fcn = "set_backup_ufs()";
6429eb2bd662Svikram 
6430eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign));
6431eb2bd662Svikram 	return (set_backup_common(osroot, sign));
6432eb2bd662Svikram }
6433eb2bd662Svikram 
6434eb2bd662Svikram static int
6435eb2bd662Svikram set_backup_zfs(char *osdev, char *sign)
6436eb2bd662Svikram {
6437eb2bd662Svikram 	char		*pool;
6438eb2bd662Svikram 	char		*mntpt;
6439eb2bd662Svikram 	zfs_mnted_t	mnted;
6440eb2bd662Svikram 	int		ret;
6441eb2bd662Svikram 	const char	*fcn = "set_backup_zfs()";
6442eb2bd662Svikram 
6443eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign));
6444eb2bd662Svikram 
6445eb2bd662Svikram 	pool = get_pool(osdev);
6446eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_GET_POOL", pool = NULL);
6447eb2bd662Svikram 	if (pool == NULL) {
6448eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
6449eb2bd662Svikram 		return (-1);
6450eb2bd662Svikram 	}
6451eb2bd662Svikram 
6452eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
6453eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_MOUNT_DATASET", mntpt = NULL);
6454eb2bd662Svikram 	if (mntpt == NULL) {
6455eb2bd662Svikram 		bam_error(FAIL_MNT_TOP_DATASET, pool);
6456eb2bd662Svikram 		free(pool);
6457eb2bd662Svikram 		return (-1);
6458eb2bd662Svikram 	}
6459eb2bd662Svikram 
6460eb2bd662Svikram 	ret = set_backup_common(mntpt, sign);
6461eb2bd662Svikram 
6462eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
6463eb2bd662Svikram 
6464eb2bd662Svikram 	free(pool);
6465eb2bd662Svikram 
6466eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_ZFS_FAIL", ret = 1);
6467eb2bd662Svikram 	if (ret == 0) {
6468eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6469eb2bd662Svikram 	} else {
6470eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6471eb2bd662Svikram 	}
6472eb2bd662Svikram 
6473eb2bd662Svikram 	return (ret);
6474eb2bd662Svikram }
6475eb2bd662Svikram 
6476eb2bd662Svikram static int
6477eb2bd662Svikram set_backup(char *osroot, char *osdev, char *sign, char *fstype)
6478eb2bd662Svikram {
6479eb2bd662Svikram 	const char	*fcn = "set_backup()";
6480eb2bd662Svikram 	int		ret;
6481eb2bd662Svikram 
6482eb2bd662Svikram 	INJECT_ERROR1("SET_BACKUP_FSTYPE", fstype = "foofs");
6483eb2bd662Svikram 
6484eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
6485eb2bd662Svikram 		BAM_DPRINTF((D_SET_BACKUP_UFS, fcn));
6486eb2bd662Svikram 		ret = set_backup_ufs(osroot, sign);
6487eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
6488eb2bd662Svikram 		BAM_DPRINTF((D_SET_BACKUP_ZFS, fcn));
6489eb2bd662Svikram 		ret = set_backup_zfs(osdev, sign);
6490eb2bd662Svikram 	} else {
6491eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
6492eb2bd662Svikram 		ret = -1;
6493eb2bd662Svikram 	}
6494eb2bd662Svikram 
6495eb2bd662Svikram 	if (ret == 0) {
6496eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6497eb2bd662Svikram 	} else {
6498eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6499eb2bd662Svikram 	}
6500eb2bd662Svikram 
6501eb2bd662Svikram 	return (ret);
6502eb2bd662Svikram }
6503eb2bd662Svikram 
6504eb2bd662Svikram static int
6505eb2bd662Svikram set_primary_common(char *mntpt, char *sign)
6506eb2bd662Svikram {
6507eb2bd662Svikram 	char		signfile[PATH_MAX];
6508eb2bd662Svikram 	char		signdir[PATH_MAX];
6509eb2bd662Svikram 	struct stat	sb;
6510eb2bd662Svikram 	int		fd;
6511eb2bd662Svikram 	int		error;
6512eb2bd662Svikram 	int		ret;
6513eb2bd662Svikram 	const char	*fcn = "set_primary_common()";
6514eb2bd662Svikram 
6515eb2bd662Svikram 	(void) snprintf(signfile, sizeof (signfile), "%s/%s/%s",
6516eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR, sign);
6517eb2bd662Svikram 
6518eb2bd662Svikram 	if (stat(signfile, &sb) != -1) {
6519eb2bd662Svikram 		if (bam_verbose)
6520eb2bd662Svikram 			bam_print(PRIMARY_SIGN_EXISTS, sign);
6521eb2bd662Svikram 		return (0);
6522eb2bd662Svikram 	} else {
6523eb2bd662Svikram 		BAM_DPRINTF((D_PRIMARY_NOT_EXIST, fcn, signfile));
6524eb2bd662Svikram 	}
6525eb2bd662Svikram 
6526eb2bd662Svikram 	(void) snprintf(signdir, sizeof (signdir), "%s/%s",
6527eb2bd662Svikram 	    mntpt, GRUBSIGN_DIR);
6528eb2bd662Svikram 
6529eb2bd662Svikram 	if (stat(signdir, &sb) == -1) {
6530eb2bd662Svikram 		BAM_DPRINTF((D_PRIMARY_DIR_NOEXIST, fcn, signdir));
653148847494SEnrico Perla - Sun Microsystems 		ret = mkdirp(signdir, DIR_PERMS);
6532eb2bd662Svikram 		error = errno;
6533eb2bd662Svikram 		INJECT_ERROR1("SET_PRIMARY_MKDIRP", ret = -1);
6534eb2bd662Svikram 		if (ret == -1) {
6535eb2bd662Svikram 			bam_error(GRUBSIGN_MKDIR_ERR, signdir, strerror(errno));
6536eb2bd662Svikram 			return (-1);
6537eb2bd662Svikram 		}
6538eb2bd662Svikram 	}
6539eb2bd662Svikram 
6540eb2bd662Svikram 	fd = open(signfile, O_RDWR|O_CREAT|O_TRUNC, 0444);
6541eb2bd662Svikram 	error = errno;
6542eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_SIGN_CREAT", fd = -1);
6543eb2bd662Svikram 	if (fd == -1) {
6544eb2bd662Svikram 		bam_error(GRUBSIGN_PRIMARY_CREATERR, signfile, strerror(error));
6545eb2bd662Svikram 		return (-1);
6546eb2bd662Svikram 	}
6547eb2bd662Svikram 
6548eb2bd662Svikram 	ret = fsync(fd);
6549eb2bd662Svikram 	error = errno;
6550eb2bd662Svikram 	INJECT_ERROR1("PRIMARY_FSYNC", ret = -1);
6551eb2bd662Svikram 	if (ret != 0) {
6552eb2bd662Svikram 		bam_error(GRUBSIGN_PRIMARY_SYNCERR, signfile, strerror(error));
6553eb2bd662Svikram 	}
6554eb2bd662Svikram 
6555eb2bd662Svikram 	(void) close(fd);
6556eb2bd662Svikram 
6557eb2bd662Svikram 	if (bam_verbose)
6558eb2bd662Svikram 		bam_print(GRUBSIGN_CREATED_PRIMARY, signfile);
6559eb2bd662Svikram 
6560eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6561eb2bd662Svikram 
6562eb2bd662Svikram 	return (0);
6563eb2bd662Svikram }
6564eb2bd662Svikram 
6565eb2bd662Svikram static int
6566eb2bd662Svikram set_primary_ufs(char *osroot, char *sign)
6567eb2bd662Svikram {
6568eb2bd662Svikram 	const char	*fcn = "set_primary_ufs()";
6569eb2bd662Svikram 
6570eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, sign));
6571eb2bd662Svikram 	return (set_primary_common(osroot, sign));
6572eb2bd662Svikram }
6573eb2bd662Svikram 
6574eb2bd662Svikram static int
6575eb2bd662Svikram set_primary_zfs(char *osdev, char *sign)
6576eb2bd662Svikram {
6577eb2bd662Svikram 	char		*pool;
6578eb2bd662Svikram 	char		*mntpt;
6579eb2bd662Svikram 	zfs_mnted_t	mnted;
6580eb2bd662Svikram 	int		ret;
6581eb2bd662Svikram 	const char	*fcn = "set_primary_zfs()";
6582eb2bd662Svikram 
6583eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osdev, sign));
6584eb2bd662Svikram 
6585eb2bd662Svikram 	pool = get_pool(osdev);
6586eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_GET_POOL", pool = NULL);
6587eb2bd662Svikram 	if (pool == NULL) {
6588eb2bd662Svikram 		bam_error(GET_POOL_FAILED, osdev);
6589eb2bd662Svikram 		return (-1);
6590eb2bd662Svikram 	}
6591eb2bd662Svikram 
6592eb2bd662Svikram 	/* Pool name must exist in the sign */
6593eb2bd662Svikram 	ret = (strstr(sign, pool) != NULL);
6594eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_POOL_SIGN_INCOMPAT", ret = 0);
6595eb2bd662Svikram 	if (ret == 0) {
6596eb2bd662Svikram 		bam_error(POOL_SIGN_INCOMPAT, pool, sign);
6597eb2bd662Svikram 		free(pool);
6598eb2bd662Svikram 		return (-1);
6599eb2bd662Svikram 	}
6600eb2bd662Svikram 
6601eb2bd662Svikram 	mntpt = mount_top_dataset(pool, &mnted);
6602eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_MOUNT_DATASET", mntpt = NULL);
6603eb2bd662Svikram 	if (mntpt == NULL) {
6604eb2bd662Svikram 		bam_error(FAIL_MNT_TOP_DATASET, pool);
6605eb2bd662Svikram 		free(pool);
6606eb2bd662Svikram 		return (-1);
6607eb2bd662Svikram 	}
6608eb2bd662Svikram 
6609eb2bd662Svikram 	ret = set_primary_common(mntpt, sign);
6610eb2bd662Svikram 
6611eb2bd662Svikram 	(void) umount_top_dataset(pool, mnted, mntpt);
6612eb2bd662Svikram 
6613eb2bd662Svikram 	free(pool);
6614eb2bd662Svikram 
6615eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_ZFS_FAIL", ret = 1);
6616eb2bd662Svikram 	if (ret == 0) {
6617eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6618eb2bd662Svikram 	} else {
6619eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6620eb2bd662Svikram 	}
6621eb2bd662Svikram 
6622eb2bd662Svikram 	return (ret);
6623eb2bd662Svikram }
6624eb2bd662Svikram 
6625eb2bd662Svikram static int
6626eb2bd662Svikram set_primary(char *osroot, char *osdev, char *sign, char *fstype)
6627eb2bd662Svikram {
6628eb2bd662Svikram 	const char	*fcn = "set_primary()";
6629eb2bd662Svikram 	int		ret;
6630eb2bd662Svikram 
6631eb2bd662Svikram 	INJECT_ERROR1("SET_PRIMARY_FSTYPE", fstype = "foofs");
6632eb2bd662Svikram 	if (strcmp(fstype, "ufs") == 0) {
6633eb2bd662Svikram 		BAM_DPRINTF((D_SET_PRIMARY_UFS, fcn));
6634eb2bd662Svikram 		ret = set_primary_ufs(osroot, sign);
6635eb2bd662Svikram 	} else if (strcmp(fstype, "zfs") == 0) {
6636eb2bd662Svikram 		BAM_DPRINTF((D_SET_PRIMARY_ZFS, fcn));
6637eb2bd662Svikram 		ret = set_primary_zfs(osdev, sign);
6638eb2bd662Svikram 	} else {
6639eb2bd662Svikram 		bam_error(GRUBSIGN_NOTSUP, fstype);
6640eb2bd662Svikram 		ret = -1;
6641eb2bd662Svikram 	}
6642eb2bd662Svikram 
6643eb2bd662Svikram 	if (ret == 0) {
6644eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6645eb2bd662Svikram 	} else {
6646eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6647eb2bd662Svikram 	}
6648eb2bd662Svikram 
6649eb2bd662Svikram 	return (ret);
6650eb2bd662Svikram }
6651eb2bd662Svikram 
6652eb2bd662Svikram static int
6653eb2bd662Svikram ufs_add_to_sign_list(char *sign)
6654eb2bd662Svikram {
6655eb2bd662Svikram 	FILE		*tfp;
6656eb2bd662Svikram 	char		signline[MAXNAMELEN];
6657eb2bd662Svikram 	char		cmd[PATH_MAX];
6658eb2bd662Svikram 	int		ret;
6659eb2bd662Svikram 	int		error;
6660eb2bd662Svikram 	const char	*fcn = "ufs_add_to_sign_list()";
6661eb2bd662Svikram 
6662eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_NOT_UFS", sign = "pool_rpool5");
6663eb2bd662Svikram 	if (strncmp(sign, GRUBSIGN_UFS_PREFIX,
6664eb2bd662Svikram 	    strlen(GRUBSIGN_UFS_PREFIX)) != 0) {
6665eb2bd662Svikram 		bam_error(INVALID_UFS_SIGN, sign);
6666eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6667eb2bd662Svikram 		return (-1);
6668eb2bd662Svikram 	}
6669eb2bd662Svikram 
6670eb2bd662Svikram 	/*
6671eb2bd662Svikram 	 * most failures in this routine are not a fatal error
6672eb2bd662Svikram 	 * We simply unlink the /var/run file and continue
6673eb2bd662Svikram 	 */
6674eb2bd662Svikram 
6675eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST".tmp");
6676eb2bd662Svikram 	error = errno;
6677eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME", ret = -1);
6678eb2bd662Svikram 	if (ret == -1) {
6679eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST".tmp",
6680eb2bd662Svikram 		    strerror(error));
6681eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6682eb2bd662Svikram 		return (0);
6683eb2bd662Svikram 	}
6684eb2bd662Svikram 
6685eb2bd662Svikram 	tfp = fopen(UFS_SIGNATURE_LIST".tmp", "a");
6686eb2bd662Svikram 	error = errno;
6687eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FOPEN", tfp = NULL);
6688eb2bd662Svikram 	if (tfp == NULL) {
6689eb2bd662Svikram 		bam_error(OPEN_FAIL, UFS_SIGNATURE_LIST".tmp", strerror(error));
6690eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6691eb2bd662Svikram 		return (0);
6692eb2bd662Svikram 	}
6693eb2bd662Svikram 
6694eb2bd662Svikram 	(void) snprintf(signline, sizeof (signline), "%s\n", sign);
6695eb2bd662Svikram 
6696eb2bd662Svikram 	ret = fputs(signline, tfp);
6697eb2bd662Svikram 	error = errno;
6698eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FPUTS", ret = 0);
6699eb2bd662Svikram 	if (ret != strlen(signline)) {
6700eb2bd662Svikram 		bam_error(SIGN_LIST_FPUTS_ERR, sign, strerror(error));
6701eb2bd662Svikram 		(void) fclose(tfp);
6702eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6703eb2bd662Svikram 		return (0);
6704eb2bd662Svikram 	}
6705eb2bd662Svikram 
6706eb2bd662Svikram 	ret = fclose(tfp);
6707eb2bd662Svikram 	error = errno;
6708eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_FCLOSE", ret = EOF);
6709eb2bd662Svikram 	if (ret == EOF) {
6710eb2bd662Svikram 		bam_error(CLOSE_FAIL, UFS_SIGNATURE_LIST".tmp",
6711eb2bd662Svikram 		    strerror(error));
6712eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6713eb2bd662Svikram 		return (0);
6714eb2bd662Svikram 	}
6715eb2bd662Svikram 
6716eb2bd662Svikram 	/* Sort the list again */
6717eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd),
6718eb2bd662Svikram 	    "/usr/bin/sort -u %s.tmp > %s.sorted",
6719eb2bd662Svikram 	    UFS_SIGNATURE_LIST, UFS_SIGNATURE_LIST);
6720eb2bd662Svikram 
6721eb2bd662Svikram 	ret = exec_cmd(cmd, NULL);
6722eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_SORT", ret = 1);
6723eb2bd662Svikram 	if (ret != 0) {
6724eb2bd662Svikram 		bam_error(GRUBSIGN_SORT_FAILED);
6725eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6726eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".tmp");
6727eb2bd662Svikram 		return (0);
6728eb2bd662Svikram 	}
6729eb2bd662Svikram 
6730eb2bd662Svikram 	(void) unlink(UFS_SIGNATURE_LIST".tmp");
6731eb2bd662Svikram 
6732eb2bd662Svikram 	ret = rename(UFS_SIGNATURE_LIST".sorted", UFS_SIGNATURE_LIST);
6733eb2bd662Svikram 	error = errno;
6734eb2bd662Svikram 	INJECT_ERROR1("ADD_TO_SIGN_LIST_RENAME2", ret = -1);
6735eb2bd662Svikram 	if (ret == -1) {
6736eb2bd662Svikram 		bam_error(RENAME_FAIL, UFS_SIGNATURE_LIST, strerror(error));
6737eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST".sorted");
6738eb2bd662Svikram 		return (0);
6739eb2bd662Svikram 	}
6740eb2bd662Svikram 
6741eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6742eb2bd662Svikram 
6743eb2bd662Svikram 	return (0);
6744eb2bd662Svikram }
6745eb2bd662Svikram 
6746eb2bd662Svikram static int
6747eb2bd662Svikram set_signature(char *osroot, char *osdev, char *sign, char *fstype)
6748eb2bd662Svikram {
6749eb2bd662Svikram 	int		ret;
6750eb2bd662Svikram 	const char	*fcn = "set_signature()";
6751eb2bd662Svikram 
6752eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY4, fcn, osroot, osdev, sign, fstype));
6753eb2bd662Svikram 
6754eb2bd662Svikram 	ret = set_backup(osroot, osdev, sign, fstype);
6755eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_BACKUP", ret = -1);
6756eb2bd662Svikram 	if (ret == -1) {
6757eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6758eb2bd662Svikram 		bam_error(SET_BACKUP_FAILED, sign, osroot, osdev);
6759eb2bd662Svikram 		return (-1);
6760eb2bd662Svikram 	}
6761eb2bd662Svikram 
6762eb2bd662Svikram 	ret = set_primary(osroot, osdev, sign, fstype);
6763eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_PRIMARY", ret = -1);
6764eb2bd662Svikram 
6765eb2bd662Svikram 	if (ret == 0) {
6766eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6767eb2bd662Svikram 	} else {
6768eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
6769eb2bd662Svikram 		bam_error(SET_PRIMARY_FAILED, sign, osroot, osdev);
6770eb2bd662Svikram 
6771eb2bd662Svikram 	}
6772eb2bd662Svikram 	return (ret);
6773eb2bd662Svikram }
6774eb2bd662Svikram 
6775eb2bd662Svikram char *
6776eb2bd662Svikram get_grubsign(char *osroot, char *osdev)
6777eb2bd662Svikram {
6778eb2bd662Svikram 	char		*grubsign;	/* (<sign>,#,#) */
6779eb2bd662Svikram 	char		*slice;
6780eb2bd662Svikram 	int		fdiskpart;
6781eb2bd662Svikram 	char		*sign;
6782eb2bd662Svikram 	char		*fstype;
6783eb2bd662Svikram 	int		ret;
6784eb2bd662Svikram 	const char	*fcn = "get_grubsign()";
6785eb2bd662Svikram 
6786eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, osdev));
6787eb2bd662Svikram 
6788eb2bd662Svikram 	fstype = get_fstype(osroot);
6789eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FSTYPE", fstype = NULL);
6790eb2bd662Svikram 	if (fstype == NULL) {
6791eb2bd662Svikram 		bam_error(GET_FSTYPE_FAILED, osroot);
6792eb2bd662Svikram 		return (NULL);
6793eb2bd662Svikram 	}
6794eb2bd662Svikram 
6795eb2bd662Svikram 	sign = find_existing_sign(osroot, osdev, fstype);
6796eb2bd662Svikram 	INJECT_ERROR1("FIND_EXISTING_SIGN", sign = NULL);
6797eb2bd662Svikram 	if (sign == NULL) {
6798eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBSIGN_NO_EXISTING, fcn, osroot, osdev));
6799eb2bd662Svikram 		sign = create_new_sign(osdev, fstype);
6800eb2bd662Svikram 		INJECT_ERROR1("CREATE_NEW_SIGN", sign = NULL);
6801eb2bd662Svikram 		if (sign == NULL) {
6802eb2bd662Svikram 			bam_error(GRUBSIGN_CREATE_FAIL, osdev);
6803eb2bd662Svikram 			free(fstype);
6804eb2bd662Svikram 			return (NULL);
6805eb2bd662Svikram 		}
6806eb2bd662Svikram 	}
6807eb2bd662Svikram 
6808eb2bd662Svikram 	ret = set_signature(osroot, osdev, sign, fstype);
6809eb2bd662Svikram 	INJECT_ERROR1("SET_SIGNATURE_FAIL", ret = -1);
6810eb2bd662Svikram 	if (ret == -1) {
6811eb2bd662Svikram 		bam_error(GRUBSIGN_WRITE_FAIL, osdev);
6812eb2bd662Svikram 		free(sign);
6813eb2bd662Svikram 		free(fstype);
6814eb2bd662Svikram 		(void) unlink(UFS_SIGNATURE_LIST);
6815eb2bd662Svikram 		return (NULL);
6816eb2bd662Svikram 	}
6817eb2bd662Svikram 
6818eb2bd662Svikram 	free(fstype);
6819eb2bd662Svikram 
6820eb2bd662Svikram 	if (bam_verbose)
6821eb2bd662Svikram 		bam_print(GRUBSIGN_FOUND_OR_CREATED, sign, osdev);
6822eb2bd662Svikram 
6823eb2bd662Svikram 	fdiskpart = get_partition(osdev);
6824eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FDISK", fdiskpart = -1);
6825eb2bd662Svikram 	if (fdiskpart == -1) {
6826eb2bd662Svikram 		bam_error(FDISKPART_FAIL, osdev);
6827eb2bd662Svikram 		free(sign);
6828eb2bd662Svikram 		return (NULL);
6829eb2bd662Svikram 	}
6830eb2bd662Svikram 
6831eb2bd662Svikram 	slice = strrchr(osdev, 's');
6832eb2bd662Svikram 
6833eb2bd662Svikram 	grubsign = s_calloc(1, MAXNAMELEN + 10);
6834eb2bd662Svikram 	if (slice) {
6835eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d,%c)",
6836eb2bd662Svikram 		    sign, fdiskpart, slice[1] + 'a' - '0');
6837eb2bd662Svikram 	} else
6838eb2bd662Svikram 		(void) snprintf(grubsign, MAXNAMELEN + 10, "(%s,%d)",
6839eb2bd662Svikram 		    sign, fdiskpart);
6840eb2bd662Svikram 
6841eb2bd662Svikram 	free(sign);
6842eb2bd662Svikram 
6843eb2bd662Svikram 	BAM_DPRINTF((D_GET_GRUBSIGN_SUCCESS, fcn, grubsign));
6844eb2bd662Svikram 
6845eb2bd662Svikram 	return (grubsign);
68467c478bd9Sstevel@tonic-gate }
68477c478bd9Sstevel@tonic-gate 
6848ee3b8144Sszhou static char *
6849ee3b8144Sszhou get_title(char *rootdir)
68507c478bd9Sstevel@tonic-gate {
6851eb2bd662Svikram 	static char	title[80];
6852eb2bd662Svikram 	char		*cp = NULL;
6853eb2bd662Svikram 	char		release[PATH_MAX];
68547c478bd9Sstevel@tonic-gate 	FILE		*fp;
6855eb2bd662Svikram 	const char	*fcn = "get_title()";
68567c478bd9Sstevel@tonic-gate 
68577c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
68587c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
68597c478bd9Sstevel@tonic-gate 
68607c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
6861eb2bd662Svikram 	if (fp == NULL) {
6862eb2bd662Svikram 		bam_error(OPEN_FAIL, release, strerror(errno));
6863eb2bd662Svikram 		cp = NULL;
6864eb2bd662Svikram 		goto out;
6865eb2bd662Svikram 	}
68667c478bd9Sstevel@tonic-gate 
68677c478bd9Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
68687c478bd9Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
68697c478bd9Sstevel@tonic-gate 		if (cp)
68707c478bd9Sstevel@tonic-gate 			break;
68717c478bd9Sstevel@tonic-gate 	}
68727c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
6873eb2bd662Svikram 
6874eb2bd662Svikram out:
6875eb2bd662Svikram 	cp = cp ? cp : "Solaris";
6876eb2bd662Svikram 
6877eb2bd662Svikram 	BAM_DPRINTF((D_GET_TITLE, fcn, cp));
6878eb2bd662Svikram 
6879eb2bd662Svikram 	return (cp);
68807c478bd9Sstevel@tonic-gate }
68817c478bd9Sstevel@tonic-gate 
6882ae115bc7Smrj char *
68837c478bd9Sstevel@tonic-gate get_special(char *mountp)
68847c478bd9Sstevel@tonic-gate {
68857c478bd9Sstevel@tonic-gate 	FILE		*mntfp;
6886eb2bd662Svikram 	struct mnttab	mp = {0};
6887eb2bd662Svikram 	struct mnttab	mpref = {0};
6888eb2bd662Svikram 	int		error;
6889eb2bd662Svikram 	int		ret;
6890eb2bd662Svikram 	const char 	*fcn = "get_special()";
6891eb2bd662Svikram 
6892eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTPT", mountp = NULL);
6893eb2bd662Svikram 	if (mountp == NULL) {
6894eb2bd662Svikram 		bam_error(GET_SPECIAL_NULL_MNTPT);
6895eb2bd662Svikram 		return (NULL);
6896eb2bd662Svikram 	}
68977c478bd9Sstevel@tonic-gate 
68987c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
6899eb2bd662Svikram 	error = errno;
6900eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_OPEN", mntfp = NULL);
69017c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
6902eb2bd662Svikram 		bam_error(OPEN_FAIL, MNTTAB, strerror(error));
6903eb2bd662Svikram 		return (NULL);
69047c478bd9Sstevel@tonic-gate 	}
69057c478bd9Sstevel@tonic-gate 
69067c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
69077c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
69087c478bd9Sstevel@tonic-gate 	else
69097c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
6910eb2bd662Svikram 
6911eb2bd662Svikram 	ret = getmntany(mntfp, &mp, &mpref);
6912eb2bd662Svikram 	INJECT_ERROR1("GET_SPECIAL_MNTTAB_SEARCH", ret = 1);
6913eb2bd662Svikram 	if (ret != 0) {
69147c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
6915eb2bd662Svikram 		BAM_DPRINTF((D_GET_SPECIAL_NOT_IN_MNTTAB, fcn, mountp));
69167c478bd9Sstevel@tonic-gate 		return (NULL);
69177c478bd9Sstevel@tonic-gate 	}
69187c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
69197c478bd9Sstevel@tonic-gate 
6920eb2bd662Svikram 	BAM_DPRINTF((D_GET_SPECIAL, fcn, mp.mnt_special));
6921eb2bd662Svikram 
69227c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
69237c478bd9Sstevel@tonic-gate }
69247c478bd9Sstevel@tonic-gate 
6925eb2bd662Svikram static void
6926eb2bd662Svikram free_physarray(char **physarray, int n)
69277c478bd9Sstevel@tonic-gate {
6928eb2bd662Svikram 	int			i;
6929eb2bd662Svikram 	const char		*fcn = "free_physarray()";
69307c478bd9Sstevel@tonic-gate 
6931eb2bd662Svikram 	assert(physarray);
6932eb2bd662Svikram 	assert(n);
6933eb2bd662Svikram 
6934eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY_N1, fcn, n));
6935eb2bd662Svikram 
6936eb2bd662Svikram 	for (i = 0; i < n; i++) {
6937eb2bd662Svikram 		free(physarray[i]);
69387c478bd9Sstevel@tonic-gate 	}
6939eb2bd662Svikram 	free(physarray);
6940eb2bd662Svikram 
6941eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
6942eb2bd662Svikram }
6943eb2bd662Svikram 
6944eb2bd662Svikram static int
6945eb2bd662Svikram zfs_get_physical(char *special, char ***physarray, int *n)
6946eb2bd662Svikram {
6947eb2bd662Svikram 	char			sdup[PATH_MAX];
6948eb2bd662Svikram 	char			cmd[PATH_MAX];
6949eb2bd662Svikram 	char			dsk[PATH_MAX];
6950eb2bd662Svikram 	char			*pool;
6951eb2bd662Svikram 	filelist_t		flist = {0};
6952eb2bd662Svikram 	line_t			*lp;
6953eb2bd662Svikram 	line_t			*startlp;
6954eb2bd662Svikram 	char			*comp1;
6955eb2bd662Svikram 	int			i;
6956eb2bd662Svikram 	int			ret;
6957eb2bd662Svikram 	const char		*fcn = "zfs_get_physical()";
6958eb2bd662Svikram 
6959eb2bd662Svikram 	assert(special);
6960eb2bd662Svikram 
6961eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, special));
6962eb2bd662Svikram 
6963eb2bd662Svikram 	INJECT_ERROR1("INVALID_ZFS_SPECIAL", special = "/foo");
6964eb2bd662Svikram 	if (special[0] == '/') {
6965eb2bd662Svikram 		bam_error(INVALID_ZFS_SPECIAL, special);
6966eb2bd662Svikram 		return (-1);
6967eb2bd662Svikram 	}
6968eb2bd662Svikram 
6969eb2bd662Svikram 	(void) strlcpy(sdup, special, sizeof (sdup));
6970eb2bd662Svikram 
6971eb2bd662Svikram 	pool = strtok(sdup, "/");
6972eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_POOL", pool = NULL);
6973eb2bd662Svikram 	if (pool == NULL) {
6974eb2bd662Svikram 		bam_error(CANT_FIND_POOL_FROM_SPECIAL, special);
6975eb2bd662Svikram 		return (-1);
6976eb2bd662Svikram 	}
6977eb2bd662Svikram 
6978eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/zpool status %s", pool);
6979eb2bd662Svikram 
6980eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
6981eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS", ret = 1);
6982eb2bd662Svikram 	if (ret != 0) {
6983eb2bd662Svikram 		bam_error(ZFS_GET_POOL_STATUS, pool);
6984eb2bd662Svikram 		return (-1);
6985eb2bd662Svikram 	}
6986eb2bd662Svikram 
6987eb2bd662Svikram 	INJECT_ERROR1("ZFS_GET_PHYS_STATUS_OUT", flist.head = NULL);
6988eb2bd662Svikram 	if (flist.head == NULL) {
6989eb2bd662Svikram 		bam_error(BAD_ZPOOL_STATUS, pool);
6990eb2bd662Svikram 		filelist_free(&flist);
6991eb2bd662Svikram 		return (-1);
6992eb2bd662Svikram 	}
6993eb2bd662Svikram 
6994eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
6995eb2bd662Svikram 		BAM_DPRINTF((D_STRTOK_ZPOOL_STATUS, fcn, lp->line));
6996eb2bd662Svikram 		comp1 = strtok(lp->line, " \t");
6997eb2bd662Svikram 		if (comp1 == NULL) {
6998eb2bd662Svikram 			free(lp->line);
6999eb2bd662Svikram 			lp->line = NULL;
7000eb2bd662Svikram 		} else {
7001eb2bd662Svikram 			comp1 = s_strdup(comp1);
7002eb2bd662Svikram 			free(lp->line);
7003eb2bd662Svikram 			lp->line = comp1;
7004eb2bd662Svikram 		}
7005eb2bd662Svikram 	}
7006eb2bd662Svikram 
7007eb2bd662Svikram 	for (lp = flist.head; lp; lp = lp->next) {
7008eb2bd662Svikram 		if (lp->line == NULL)
7009eb2bd662Svikram 			continue;
7010eb2bd662Svikram 		if (strcmp(lp->line, pool) == 0) {
7011eb2bd662Svikram 			BAM_DPRINTF((D_FOUND_POOL_IN_ZPOOL_STATUS, fcn, pool));
7012eb2bd662Svikram 			break;
7013eb2bd662Svikram 		}
7014eb2bd662Svikram 	}
7015eb2bd662Svikram 
7016eb2bd662Svikram 	if (lp == NULL) {
7017eb2bd662Svikram 		bam_error(NO_POOL_IN_ZPOOL_STATUS, pool);
7018eb2bd662Svikram 		filelist_free(&flist);
7019eb2bd662Svikram 		return (-1);
7020eb2bd662Svikram 	}
7021eb2bd662Svikram 
7022eb2bd662Svikram 	startlp = lp->next;
7023eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
7024eb2bd662Svikram 		if (lp->line == NULL)
7025eb2bd662Svikram 			continue;
7026eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
7027eb2bd662Svikram 			continue;
7028eb2bd662Svikram 		if (lp->line[0] == '\0' || strcmp(lp->line, "errors:") == 0)
7029eb2bd662Svikram 			break;
7030eb2bd662Svikram 		i++;
7031eb2bd662Svikram 		BAM_DPRINTF((D_COUNTING_ZFS_PHYS, fcn, i));
7032eb2bd662Svikram 	}
7033eb2bd662Svikram 
7034eb2bd662Svikram 	if (i == 0) {
7035eb2bd662Svikram 		bam_error(NO_PHYS_IN_ZPOOL_STATUS, pool);
7036eb2bd662Svikram 		filelist_free(&flist);
7037eb2bd662Svikram 		return (-1);
7038eb2bd662Svikram 	}
7039eb2bd662Svikram 
7040eb2bd662Svikram 	*n = i;
7041eb2bd662Svikram 	*physarray = s_calloc(*n, sizeof (char *));
7042eb2bd662Svikram 	for (i = 0, lp = startlp; lp; lp = lp->next) {
7043eb2bd662Svikram 		if (lp->line == NULL)
7044eb2bd662Svikram 			continue;
7045eb2bd662Svikram 		if (strcmp(lp->line, "mirror") == 0)
7046eb2bd662Svikram 			continue;
7047eb2bd662Svikram 		if (strcmp(lp->line, "errors:") == 0)
7048eb2bd662Svikram 			break;
7049eb2bd662Svikram 		if (strncmp(lp->line, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
7050eb2bd662Svikram 		    strncmp(lp->line, "/dev/rdsk/",
7051eb2bd662Svikram 		    strlen("/dev/rdsk/")) != 0)  {
7052eb2bd662Svikram 			(void) snprintf(dsk, sizeof (dsk), "/dev/dsk/%s",
7053eb2bd662Svikram 			    lp->line);
7054eb2bd662Svikram 		} else {
7055eb2bd662Svikram 			(void) strlcpy(dsk, lp->line, sizeof (dsk));
7056eb2bd662Svikram 		}
7057eb2bd662Svikram 		BAM_DPRINTF((D_ADDING_ZFS_PHYS, fcn, dsk, pool));
7058eb2bd662Svikram 		(*physarray)[i++] = s_strdup(dsk);
7059eb2bd662Svikram 	}
7060eb2bd662Svikram 
7061eb2bd662Svikram 	assert(i == *n);
7062eb2bd662Svikram 
7063eb2bd662Svikram 	filelist_free(&flist);
7064eb2bd662Svikram 
7065eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7066eb2bd662Svikram 	return (0);
7067eb2bd662Svikram }
7068eb2bd662Svikram 
7069963390b4Svikram /*
7070963390b4Svikram  * Certain services needed to run metastat successfully may not
7071963390b4Svikram  * be enabled. Enable them now.
7072963390b4Svikram  */
7073963390b4Svikram /*
7074963390b4Svikram  * Checks if the specified service is online
7075963390b4Svikram  * Returns: 	1 if the service is online
7076963390b4Svikram  *		0 if the service is not online
7077963390b4Svikram  *		-1 on error
7078963390b4Svikram  */
7079963390b4Svikram static int
7080963390b4Svikram is_svc_online(char *svc)
7081963390b4Svikram {
7082963390b4Svikram 	char			*state;
7083963390b4Svikram 	const char		*fcn = "is_svc_online()";
7084963390b4Svikram 
7085963390b4Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, svc));
7086963390b4Svikram 
7087963390b4Svikram 	state = smf_get_state(svc);
7088963390b4Svikram 	INJECT_ERROR2("GET_SVC_STATE", free(state), state = NULL);
7089963390b4Svikram 	if (state == NULL) {
7090963390b4Svikram 		bam_error(GET_SVC_STATE_ERR, svc);
7091963390b4Svikram 		return (-1);
7092963390b4Svikram 	}
7093963390b4Svikram 	BAM_DPRINTF((D_GOT_SVC_STATUS, fcn, svc));
7094963390b4Svikram 
7095963390b4Svikram 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
7096963390b4Svikram 		BAM_DPRINTF((D_SVC_ONLINE, fcn, svc));
7097963390b4Svikram 		free(state);
7098963390b4Svikram 		return (1);
7099963390b4Svikram 	}
7100963390b4Svikram 
7101963390b4Svikram 	BAM_DPRINTF((D_SVC_NOT_ONLINE, fcn, state, svc));
7102963390b4Svikram 
7103963390b4Svikram 	free(state);
7104963390b4Svikram 
7105963390b4Svikram 	return (0);
7106963390b4Svikram }
7107963390b4Svikram 
7108963390b4Svikram static int
7109963390b4Svikram enable_svc(char *svc)
7110963390b4Svikram {
7111963390b4Svikram 	int			ret;
7112963390b4Svikram 	int			sleeptime;
7113963390b4Svikram 	const char		*fcn = "enable_svc()";
7114963390b4Svikram 
7115963390b4Svikram 	ret = is_svc_online(svc);
7116963390b4Svikram 	if (ret == -1) {
7117963390b4Svikram 		bam_error(SVC_IS_ONLINE_FAILED, svc);
7118963390b4Svikram 		return (-1);
7119963390b4Svikram 	} else if (ret == 1) {
7120963390b4Svikram 		BAM_DPRINTF((D_SVC_ALREADY_ONLINE, fcn, svc));
7121963390b4Svikram 		return (0);
7122963390b4Svikram 	}
7123963390b4Svikram 
7124963390b4Svikram 	/* Service is not enabled. Enable it now. */
7125963390b4Svikram 	ret = smf_enable_instance(svc, 0);
7126963390b4Svikram 	INJECT_ERROR1("ENABLE_SVC_FAILED", ret = -1);
7127963390b4Svikram 	if (ret != 0) {
7128963390b4Svikram 		bam_error(ENABLE_SVC_FAILED, svc);
7129963390b4Svikram 		return (-1);
7130963390b4Svikram 	}
7131963390b4Svikram 
7132963390b4Svikram 	BAM_DPRINTF((D_SVC_ONLINE_INITIATED, fcn, svc));
7133963390b4Svikram 
7134963390b4Svikram 	sleeptime = 0;
7135963390b4Svikram 	do {
7136963390b4Svikram 		ret = is_svc_online(svc);
7137963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_SUCCESS", ret = 1);
7138963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_FAILURE", ret = -1);
7139963390b4Svikram 		INJECT_ERROR1("SVC_ONLINE_NOTYET", ret = 0);
7140963390b4Svikram 		if (ret == -1) {
7141963390b4Svikram 			bam_error(ERR_SVC_GET_ONLINE, svc);
7142963390b4Svikram 			return (-1);
7143963390b4Svikram 		} else if (ret == 1) {
7144963390b4Svikram 			BAM_DPRINTF((D_SVC_NOW_ONLINE, fcn, svc));
7145963390b4Svikram 			return (1);
7146963390b4Svikram 		}
7147963390b4Svikram 		(void) sleep(1);
7148589ac1f4SRobert Harris 	} while (++sleeptime < 60);
7149963390b4Svikram 
7150963390b4Svikram 	bam_error(TIMEOUT_ENABLE_SVC, svc);
7151963390b4Svikram 
7152963390b4Svikram 	return (-1);
7153963390b4Svikram }
7154963390b4Svikram 
7155eb2bd662Svikram static int
7156eb2bd662Svikram ufs_get_physical(char *special, char ***physarray, int *n)
7157eb2bd662Svikram {
7158eb2bd662Svikram 	char			cmd[PATH_MAX];
7159eb2bd662Svikram 	char			*shortname;
7160eb2bd662Svikram 	filelist_t		flist = {0};
7161eb2bd662Svikram 	char			*meta;
7162eb2bd662Svikram 	char			*type;
7163eb2bd662Svikram 	char			*comp1;
7164eb2bd662Svikram 	char			*comp2;
7165eb2bd662Svikram 	char			*comp3;
7166eb2bd662Svikram 	char			*comp4;
7167eb2bd662Svikram 	int			i;
7168eb2bd662Svikram 	line_t			*lp;
7169eb2bd662Svikram 	int			ret;
7170963390b4Svikram 	char			*svc;
7171eb2bd662Svikram 	const char		*fcn = "ufs_get_physical()";
7172eb2bd662Svikram 
7173eb2bd662Svikram 	assert(special);
7174eb2bd662Svikram 
7175eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, special));
7176eb2bd662Svikram 
7177eb2bd662Svikram 	if (strncmp(special, "/dev/md/", strlen("/dev/md/")) != 0) {
7178eb2bd662Svikram 		bam_error(UFS_GET_PHYS_NOT_SVM, special);
7179eb2bd662Svikram 		return (-1);
7180eb2bd662Svikram 	}
7181eb2bd662Svikram 
7182eb2bd662Svikram 	if (strncmp(special, "/dev/md/dsk/", strlen("/dev/md/dsk/")) == 0) {
7183eb2bd662Svikram 		shortname = special + strlen("/dev/md/dsk/");
7184eb2bd662Svikram 	} else if (strncmp(special, "/dev/md/rdsk/",
7185eb2bd662Svikram 	    strlen("/dev/md/rdsk/")) == 0) {
7186eb2bd662Svikram 		shortname = special + strlen("/dev/md/rdsk");
7187eb2bd662Svikram 	} else {
7188eb2bd662Svikram 		bam_error(UFS_GET_PHYS_INVALID_SVM, special);
7189eb2bd662Svikram 		return (-1);
7190eb2bd662Svikram 	}
7191eb2bd662Svikram 
7192eb2bd662Svikram 	BAM_DPRINTF((D_UFS_SVM_SHORT, fcn, special, shortname));
7193eb2bd662Svikram 
7194963390b4Svikram 	svc = "network/rpc/meta:default";
7195963390b4Svikram 	if (enable_svc(svc) == -1) {
7196963390b4Svikram 		bam_error(UFS_SVM_METASTAT_SVC_ERR, svc);
7197963390b4Svikram 	}
7198963390b4Svikram 
7199eb2bd662Svikram 	(void) snprintf(cmd, sizeof (cmd), "/sbin/metastat -p %s", shortname);
7200eb2bd662Svikram 
7201eb2bd662Svikram 	ret = exec_cmd(cmd, &flist);
7202eb2bd662Svikram 	INJECT_ERROR1("UFS_SVM_METASTAT", ret = 1);
7203eb2bd662Svikram 	if (ret != 0) {
7204eb2bd662Svikram 		bam_error(UFS_SVM_METASTAT_ERR, shortname);
7205eb2bd662Svikram 		return (-1);
7206eb2bd662Svikram 	}
7207eb2bd662Svikram 
7208eb2bd662Svikram 	INJECT_ERROR1("UFS_SVM_METASTAT_OUT", flist.head = NULL);
7209eb2bd662Svikram 	if (flist.head == NULL) {
7210eb2bd662Svikram 		bam_error(BAD_UFS_SVM_METASTAT, shortname);
7211eb2bd662Svikram 		filelist_free(&flist);
7212eb2bd662Svikram 		return (-1);
72137c478bd9Sstevel@tonic-gate 	}
72147c478bd9Sstevel@tonic-gate 
72157c478bd9Sstevel@tonic-gate 	/*
7216eb2bd662Svikram 	 * Check if not a mirror. We only parse a single metadevice
7217eb2bd662Svikram 	 * if not a mirror
7218eb2bd662Svikram 	 */
7219eb2bd662Svikram 	meta = strtok(flist.head->line, " \t");
7220eb2bd662Svikram 	type = strtok(NULL, " \t");
7221eb2bd662Svikram 	if (meta == NULL || type == NULL) {
7222eb2bd662Svikram 		bam_error(ERROR_PARSE_UFS_SVM_METASTAT, shortname);
7223eb2bd662Svikram 		filelist_free(&flist);
7224eb2bd662Svikram 		return (-1);
7225eb2bd662Svikram 	}
7226eb2bd662Svikram 	if (strcmp(type, "-m") != 0) {
7227eb2bd662Svikram 		comp1 = strtok(NULL, " \t");
7228eb2bd662Svikram 		comp2 = strtok(NULL, " \t");
7229eb2bd662Svikram 		if (comp1 == NULL || comp2 != NULL) {
7230eb2bd662Svikram 			bam_error(INVALID_UFS_SVM_METASTAT, shortname);
7231eb2bd662Svikram 			filelist_free(&flist);
7232eb2bd662Svikram 			return (-1);
7233eb2bd662Svikram 		}
7234eb2bd662Svikram 		BAM_DPRINTF((D_UFS_SVM_ONE_COMP, fcn, comp1, shortname));
7235eb2bd662Svikram 		*physarray = s_calloc(1, sizeof (char *));
7236eb2bd662Svikram 		(*physarray)[0] = s_strdup(comp1);
7237eb2bd662Svikram 		*n = 1;
7238eb2bd662Svikram 		filelist_free(&flist);
7239eb2bd662Svikram 		return (0);
7240eb2bd662Svikram 	}
7241eb2bd662Svikram 
7242eb2bd662Svikram 	/*
7243eb2bd662Svikram 	 * Okay we have a mirror. Everything after the first line
7244eb2bd662Svikram 	 * is a submirror
7245eb2bd662Svikram 	 */
7246eb2bd662Svikram 	for (i = 0, lp = flist.head->next; lp; lp = lp->next) {
7247eb2bd662Svikram 		if (strstr(lp->line, "/dev/dsk/") == NULL &&
7248eb2bd662Svikram 		    strstr(lp->line, "/dev/rdsk/") == NULL) {
7249eb2bd662Svikram 			bam_error(CANNOT_PARSE_UFS_SVM_METASTAT, shortname);
7250eb2bd662Svikram 			filelist_free(&flist);
7251eb2bd662Svikram 			return (-1);
7252eb2bd662Svikram 		}
7253eb2bd662Svikram 		i++;
7254eb2bd662Svikram 	}
7255eb2bd662Svikram 
7256eb2bd662Svikram 	*physarray = s_calloc(i, sizeof (char *));
7257eb2bd662Svikram 	*n = i;
7258eb2bd662Svikram 
7259eb2bd662Svikram 	for (i = 0, lp = flist.head->next; lp; lp = lp->next) {
7260eb2bd662Svikram 		comp1 = strtok(lp->line, " \t");
7261eb2bd662Svikram 		comp2 = strtok(NULL, " \t");
7262eb2bd662Svikram 		comp3 = strtok(NULL, " \t");
7263eb2bd662Svikram 		comp4 = strtok(NULL, " \t");
7264eb2bd662Svikram 
7265eb2bd662Svikram 		if (comp3 == NULL || comp4 == NULL ||
7266eb2bd662Svikram 		    (strncmp(comp4, "/dev/dsk/", strlen("/dev/dsk/")) != 0 &&
7267eb2bd662Svikram 		    strncmp(comp4, "/dev/rdsk/", strlen("/dev/rdsk/")) != 0)) {
7268eb2bd662Svikram 			bam_error(CANNOT_PARSE_UFS_SVM_SUBMIRROR, shortname);
7269eb2bd662Svikram 			filelist_free(&flist);
7270eb2bd662Svikram 			free_physarray(*physarray, *n);
7271eb2bd662Svikram 			return (-1);
7272eb2bd662Svikram 		}
7273eb2bd662Svikram 
7274eb2bd662Svikram 		(*physarray)[i++] = s_strdup(comp4);
7275eb2bd662Svikram 	}
7276eb2bd662Svikram 
7277eb2bd662Svikram 	assert(i == *n);
7278eb2bd662Svikram 
7279eb2bd662Svikram 	filelist_free(&flist);
7280eb2bd662Svikram 
7281eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7282eb2bd662Svikram 	return (0);
7283eb2bd662Svikram }
7284eb2bd662Svikram 
7285eb2bd662Svikram static int
7286eb2bd662Svikram get_physical(char *menu_root, char ***physarray, int *n)
7287eb2bd662Svikram {
7288eb2bd662Svikram 	char			*special;
7289eb2bd662Svikram 	int			ret;
7290eb2bd662Svikram 	const char		*fcn = "get_physical()";
7291eb2bd662Svikram 
7292eb2bd662Svikram 	assert(menu_root);
7293eb2bd662Svikram 	assert(physarray);
7294eb2bd662Svikram 	assert(n);
7295eb2bd662Svikram 
7296eb2bd662Svikram 	*physarray = NULL;
7297eb2bd662Svikram 	*n = 0;
7298eb2bd662Svikram 
7299eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_root));
7300eb2bd662Svikram 
7301eb2bd662Svikram 	/* First get the device special file from /etc/mnttab */
7302eb2bd662Svikram 	special = get_special(menu_root);
7303eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_SPECIAL", special = NULL);
7304eb2bd662Svikram 	if (special == NULL) {
7305eb2bd662Svikram 		bam_error(GET_SPECIAL_NULL, menu_root);
7306eb2bd662Svikram 		return (-1);
7307eb2bd662Svikram 	}
7308eb2bd662Svikram 
7309eb2bd662Svikram 	/* If already a physical device nothing to do */
7310eb2bd662Svikram 	if (strncmp(special, "/dev/dsk/", strlen("/dev/dsk/")) == 0 ||
7311eb2bd662Svikram 	    strncmp(special, "/dev/rdsk/", strlen("/dev/rdsk/")) == 0) {
7312eb2bd662Svikram 		BAM_DPRINTF((D_GET_PHYSICAL_ALREADY, fcn, menu_root, special));
7313eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7314eb2bd662Svikram 		*physarray = s_calloc(1, sizeof (char *));
7315eb2bd662Svikram 		(*physarray)[0] = special;
7316eb2bd662Svikram 		*n = 1;
7317eb2bd662Svikram 		return (0);
7318eb2bd662Svikram 	}
7319eb2bd662Svikram 
7320eb2bd662Svikram 	if (is_zfs(menu_root)) {
7321eb2bd662Svikram 		ret = zfs_get_physical(special, physarray, n);
7322eb2bd662Svikram 	} else if (is_ufs(menu_root)) {
7323eb2bd662Svikram 		ret = ufs_get_physical(special, physarray, n);
7324eb2bd662Svikram 	} else {
7325eb2bd662Svikram 		bam_error(GET_PHYSICAL_NOTSUP_FSTYPE, menu_root, special);
7326eb2bd662Svikram 		ret = -1;
7327eb2bd662Svikram 	}
7328eb2bd662Svikram 
7329eb2bd662Svikram 	free(special);
7330eb2bd662Svikram 
7331eb2bd662Svikram 	INJECT_ERROR1("GET_PHYSICAL_RET", ret = -1);
7332eb2bd662Svikram 	if (ret == -1) {
7333eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7334eb2bd662Svikram 	} else {
7335eb2bd662Svikram 		int	i;
7336eb2bd662Svikram 		assert (*n > 0);
7337eb2bd662Svikram 		for (i = 0; i < *n; i++) {
7338eb2bd662Svikram 			BAM_DPRINTF((D_GET_PHYSICAL_RET, fcn, (*physarray)[i]));
7339eb2bd662Svikram 		}
7340eb2bd662Svikram 	}
7341eb2bd662Svikram 
7342eb2bd662Svikram 	return (ret);
7343eb2bd662Svikram }
7344eb2bd662Svikram 
7345eb2bd662Svikram static int
7346eb2bd662Svikram is_bootdisk(char *osroot, char *physical)
7347eb2bd662Svikram {
7348eb2bd662Svikram 	int			ret;
7349eb2bd662Svikram 	char			*grubroot;
7350eb2bd662Svikram 	char			*bootp;
7351eb2bd662Svikram 	const char		*fcn = "is_bootdisk()";
7352eb2bd662Svikram 
7353eb2bd662Svikram 	assert(osroot);
7354eb2bd662Svikram 	assert(physical);
7355eb2bd662Svikram 
7356eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, physical));
7357eb2bd662Svikram 
7358eb2bd662Svikram 	bootp = strstr(physical, "p0:boot");
7359eb2bd662Svikram 	if (bootp)
7360eb2bd662Svikram 		*bootp = '\0';
7361eb2bd662Svikram 	/*
7362eb2bd662Svikram 	 * We just want the BIOS mapping for menu disk.
7363eb2bd662Svikram 	 * Don't pass menu_root to get_grubroot() as the
7364eb2bd662Svikram 	 * check that it is used for is not relevant here.
7365eb2bd662Svikram 	 * The osroot is immaterial as well - it is only used to
7366eb2bd662Svikram 	 * to find create_diskmap script. Everything hinges on
7367eb2bd662Svikram 	 * "physical"
7368eb2bd662Svikram 	 */
7369eb2bd662Svikram 	grubroot = get_grubroot(osroot, physical, NULL);
7370eb2bd662Svikram 
7371eb2bd662Svikram 	INJECT_ERROR1("IS_BOOTDISK_GRUBROOT", grubroot = NULL);
7372eb2bd662Svikram 	if (grubroot == NULL) {
737337eb779cSVikram Hegde 		if (bam_verbose)
737437eb779cSVikram Hegde 			bam_error(NO_GRUBROOT_FOR_DISK, physical);
7375eb2bd662Svikram 		return (0);
7376eb2bd662Svikram 	}
7377eb2bd662Svikram 	ret = grubroot[3] == '0';
7378eb2bd662Svikram 	free(grubroot);
7379eb2bd662Svikram 
7380eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_RET, fcn, ret));
7381eb2bd662Svikram 
7382eb2bd662Svikram 	return (ret);
7383eb2bd662Svikram }
7384eb2bd662Svikram 
7385eb2bd662Svikram /*
7386eb2bd662Svikram  * Check if menu is on the boot device
73877c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
73887c478bd9Sstevel@tonic-gate  */
73897c478bd9Sstevel@tonic-gate static int
7390eb2bd662Svikram menu_on_bootdisk(char *osroot, char *menu_root)
73917c478bd9Sstevel@tonic-gate {
7392eb2bd662Svikram 	char		**physarray;
73937c478bd9Sstevel@tonic-gate 	int		ret;
7394eb2bd662Svikram 	int		n;
7395eb2bd662Svikram 	int		i;
7396eb2bd662Svikram 	int		on_bootdisk;
7397eb2bd662Svikram 	const char	*fcn = "menu_on_bootdisk()";
73987c478bd9Sstevel@tonic-gate 
7399eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root));
74007c478bd9Sstevel@tonic-gate 
7401eb2bd662Svikram 	ret = get_physical(menu_root, &physarray, &n);
7402eb2bd662Svikram 	INJECT_ERROR1("MENU_ON_BOOTDISK_PHYSICAL", ret = -1);
7403eb2bd662Svikram 	if (ret != 0) {
7404eb2bd662Svikram 		bam_error(GET_PHYSICAL_MENU_NULL, menu_root);
74057c478bd9Sstevel@tonic-gate 		return (0);
7406eb2bd662Svikram 	}
7407eb2bd662Svikram 
7408eb2bd662Svikram 	assert(physarray);
7409eb2bd662Svikram 	assert(n > 0);
7410eb2bd662Svikram 
7411eb2bd662Svikram 	on_bootdisk = 0;
7412eb2bd662Svikram 	for (i = 0; i < n; i++) {
7413eb2bd662Svikram 		assert(strncmp(physarray[i], "/dev/dsk/",
7414eb2bd662Svikram 		    strlen("/dev/dsk/")) == 0 ||
7415eb2bd662Svikram 		    strncmp(physarray[i], "/dev/rdsk/",
7416eb2bd662Svikram 		    strlen("/dev/rdsk/")) == 0);
7417eb2bd662Svikram 
7418eb2bd662Svikram 		BAM_DPRINTF((D_CHECK_ON_BOOTDISK, fcn, physarray[i]));
7419eb2bd662Svikram 		if (is_bootdisk(osroot, physarray[i])) {
7420eb2bd662Svikram 			on_bootdisk = 1;
7421eb2bd662Svikram 			BAM_DPRINTF((D_IS_ON_BOOTDISK, fcn, physarray[i]));
7422eb2bd662Svikram 		}
7423eb2bd662Svikram 	}
7424eb2bd662Svikram 
7425eb2bd662Svikram 	free_physarray(physarray, n);
7426eb2bd662Svikram 
7427eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_YES", on_bootdisk = 1);
7428eb2bd662Svikram 	INJECT_ERROR1("ON_BOOTDISK_NO", on_bootdisk = 0);
7429eb2bd662Svikram 	if (on_bootdisk) {
7430eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7431eb2bd662Svikram 	} else {
7432eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7433eb2bd662Svikram 	}
7434eb2bd662Svikram 
7435eb2bd662Svikram 	return (on_bootdisk);
7436eb2bd662Svikram }
7437eb2bd662Svikram 
7438eb2bd662Svikram void
7439eb2bd662Svikram bam_add_line(menu_t *mp, entry_t *entry, line_t *prev, line_t *lp)
7440eb2bd662Svikram {
7441eb2bd662Svikram 	const char	*fcn = "bam_add_line()";
7442eb2bd662Svikram 
7443eb2bd662Svikram 	assert(mp);
7444eb2bd662Svikram 	assert(entry);
7445eb2bd662Svikram 	assert(prev);
7446eb2bd662Svikram 	assert(lp);
7447eb2bd662Svikram 
7448eb2bd662Svikram 	lp->next = prev->next;
7449eb2bd662Svikram 	if (prev->next) {
7450eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_PREV_NEXT, fcn));
7451eb2bd662Svikram 		prev->next->prev = lp;
7452eb2bd662Svikram 	} else {
7453eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_NOT_PREV_NEXT, fcn));
7454eb2bd662Svikram 	}
7455eb2bd662Svikram 	prev->next = lp;
7456eb2bd662Svikram 	lp->prev = prev;
7457eb2bd662Svikram 
7458eb2bd662Svikram 	if (entry->end == prev) {
7459eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_ENTRY, fcn));
7460eb2bd662Svikram 		entry->end = lp;
7461eb2bd662Svikram 	}
7462eb2bd662Svikram 	if (mp->end == prev) {
7463eb2bd662Svikram 		assert(lp->next == NULL);
7464eb2bd662Svikram 		mp->end = lp;
7465eb2bd662Svikram 		BAM_DPRINTF((D_ADD_LINE_LAST_LINE_IN_MENU, fcn));
7466eb2bd662Svikram 	}
74677c478bd9Sstevel@tonic-gate }
74687c478bd9Sstevel@tonic-gate 
74698c1b6884Sszhou /*
74708c1b6884Sszhou  * look for matching bootadm entry with specified parameters
74718c1b6884Sszhou  * Here are the rules (based on existing usage):
74728c1b6884Sszhou  * - If title is specified, match on title only
7473eb2bd662Svikram  * - Else, match on root/findroot, kernel, and module.
7474eb2bd662Svikram  *   Note that, if root_opt is non-zero, the absence of
7475eb2bd662Svikram  *   root line is considered a match.
74768c1b6884Sszhou  */
74778c1b6884Sszhou static entry_t *
7478eb2bd662Svikram find_boot_entry(
7479eb2bd662Svikram 	menu_t *mp,
7480eb2bd662Svikram 	char *title,
7481eb2bd662Svikram 	char *kernel,
7482eb2bd662Svikram 	char *findroot,
7483eb2bd662Svikram 	char *root,
7484eb2bd662Svikram 	char *module,
7485eb2bd662Svikram 	int root_opt,
7486eb2bd662Svikram 	int *entry_num)
74878c1b6884Sszhou {
74888c1b6884Sszhou 	int		i;
74898c1b6884Sszhou 	line_t		*lp;
74908c1b6884Sszhou 	entry_t		*ent;
7491eb2bd662Svikram 	const char	*fcn = "find_boot_entry()";
7492eb2bd662Svikram 
7493eb2bd662Svikram 	if (entry_num)
7494eb2bd662Svikram 		*entry_num = BAM_ERROR;
74958c1b6884Sszhou 
74968c1b6884Sszhou 	/* find matching entry */
74978c1b6884Sszhou 	for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) {
74988c1b6884Sszhou 		lp = ent->start;
74998c1b6884Sszhou 
75008c1b6884Sszhou 		/* first line of entry must be bootadm comment */
75018c1b6884Sszhou 		lp = ent->start;
7502ae115bc7Smrj 		if (lp->flags != BAM_COMMENT ||
7503ae115bc7Smrj 		    strcmp(lp->arg, BAM_BOOTADM_HDR) != 0) {
75048c1b6884Sszhou 			continue;
75058c1b6884Sszhou 		}
75068c1b6884Sszhou 
75078c1b6884Sszhou 		/* advance to title line */
75088c1b6884Sszhou 		lp = lp->next;
75098c1b6884Sszhou 		if (title) {
75108c1b6884Sszhou 			if (lp->flags == BAM_TITLE && lp->arg &&
7511eb2bd662Svikram 			    strcmp(lp->arg, title) == 0) {
7512eb2bd662Svikram 				BAM_DPRINTF((D_MATCHED_TITLE, fcn, title));
75138c1b6884Sszhou 				break;
7514eb2bd662Svikram 			}
7515eb2bd662Svikram 			BAM_DPRINTF((D_NOMATCH_TITLE, fcn, title, lp->arg));
75168c1b6884Sszhou 			continue;	/* check title only */
75178c1b6884Sszhou 		}
75188c1b6884Sszhou 
75198c1b6884Sszhou 		lp = lp->next;	/* advance to root line */
7520843e1988Sjohnlev 		if (lp == NULL) {
7521843e1988Sjohnlev 			continue;
7522eb2bd662Svikram 		} else if (strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) == 0) {
7523eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_FINDROOT",
7524eb2bd662Svikram 			    findroot = NULL);
7525eb2bd662Svikram 			if (findroot == NULL) {
7526eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_FINDROOT_NULL,
7527eb2bd662Svikram 				    fcn, lp->arg));
75288c1b6884Sszhou 				continue;
75298c1b6884Sszhou 			}
7530eb2bd662Svikram 			/* findroot command found, try match  */
7531eb2bd662Svikram 			if (strcmp(lp->arg, findroot) != 0) {
7532eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_FINDROOT,
7533eb2bd662Svikram 				    fcn, findroot, lp->arg));
7534eb2bd662Svikram 				continue;
7535eb2bd662Svikram 			}
7536eb2bd662Svikram 			BAM_DPRINTF((D_MATCHED_FINDROOT, fcn, findroot));
7537eb2bd662Svikram 			lp = lp->next;	/* advance to kernel line */
7538eb2bd662Svikram 		} else if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) {
7539eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_NULL_ROOT", root = NULL);
7540eb2bd662Svikram 			if (root == NULL) {
7541eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_ROOT_NULL,
7542eb2bd662Svikram 				    fcn, lp->arg));
7543eb2bd662Svikram 				continue;
7544eb2bd662Svikram 			}
7545eb2bd662Svikram 			/* root cmd found, try match */
7546eb2bd662Svikram 			if (strcmp(lp->arg, root) != 0) {
7547eb2bd662Svikram 				BAM_DPRINTF((D_NOMATCH_ROOT,
7548eb2bd662Svikram 				    fcn, root, lp->arg));
7549eb2bd662Svikram 				continue;
7550eb2bd662Svikram 			}
7551eb2bd662Svikram 			BAM_DPRINTF((D_MATCHED_ROOT, fcn, root));
75528c1b6884Sszhou 			lp = lp->next;	/* advance to kernel line */
75538c1b6884Sszhou 		} else {
7554eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_NO",
7555eb2bd662Svikram 			    root_opt = 0);
7556eb2bd662Svikram 			INJECT_ERROR1("FIND_BOOT_ENTRY_ROOT_OPT_YES",
7557eb2bd662Svikram 			    root_opt = 1);
75588c1b6884Sszhou 			/* no root command, see if root is optional */
75598c1b6884Sszhou 			if (root_opt == 0) {
7560eb2bd662Svikram 				BAM_DPRINTF((D_NO_ROOT_OPT, fcn));
75618c1b6884Sszhou 				continue;
75628c1b6884Sszhou 			}
7563eb2bd662Svikram 			BAM_DPRINTF((D_ROOT_OPT, fcn));
75648c1b6884Sszhou 		}
75658c1b6884Sszhou 
75668c1b6884Sszhou 		if (lp == NULL || lp->next == NULL) {
75678c1b6884Sszhou 			continue;
75688c1b6884Sszhou 		}
75698c1b6884Sszhou 
7570843e1988Sjohnlev 		if (kernel &&
7571843e1988Sjohnlev 		    (!check_cmd(lp->cmd, KERNEL_CMD, lp->arg, kernel))) {
7572bbcc54bdSEnrico Perla - Sun Microsystems 			if (!(ent->flags & BAM_ENTRY_FAILSAFE) ||
7573bbcc54bdSEnrico Perla - Sun Microsystems 			    !(ent->flags & BAM_ENTRY_DBOOT) ||
7574bbcc54bdSEnrico Perla - Sun Microsystems 			    strcmp(kernel, DIRECT_BOOT_FAILSAFE_LINE) != 0)
75758c1b6884Sszhou 				continue;
7576bbcc54bdSEnrico Perla - Sun Microsystems 
7577bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSKERNEL;
7578bbcc54bdSEnrico Perla - Sun Microsystems 
75798c1b6884Sszhou 		}
7580eb2bd662Svikram 		BAM_DPRINTF((D_KERNEL_MATCH, fcn, kernel, lp->arg));
7581843e1988Sjohnlev 
7582843e1988Sjohnlev 		/*
7583843e1988Sjohnlev 		 * Check for matching module entry (failsafe or normal).
7584843e1988Sjohnlev 		 * If it fails to match, we go around the loop again.
7585843e1988Sjohnlev 		 * For xpv entries, there are two module lines, so we
7586843e1988Sjohnlev 		 * do the check twice.
7587843e1988Sjohnlev 		 */
7588843e1988Sjohnlev 		lp = lp->next;	/* advance to module line */
7589843e1988Sjohnlev 		if (check_cmd(lp->cmd, MODULE_CMD, lp->arg, module) ||
7590843e1988Sjohnlev 		    (((lp = lp->next) != NULL) &&
7591843e1988Sjohnlev 		    check_cmd(lp->cmd, MODULE_CMD, lp->arg, module))) {
7592843e1988Sjohnlev 			/* match found */
7593eb2bd662Svikram 			BAM_DPRINTF((D_MODULE_MATCH, fcn, module, lp->arg));
7594843e1988Sjohnlev 			break;
7595843e1988Sjohnlev 		}
7596bbcc54bdSEnrico Perla - Sun Microsystems 
7597bbcc54bdSEnrico Perla - Sun Microsystems 		if (strcmp(module, FAILSAFE_ARCHIVE) == 0 &&
7598bbcc54bdSEnrico Perla - Sun Microsystems 		    (strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_32) == 0 ||
7599bbcc54bdSEnrico Perla - Sun Microsystems 		    strcmp(lp->prev->arg, FAILSAFE_ARCHIVE_64) == 0)) {
7600bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags |= BAM_ENTRY_UPGFSMODULE;
7601bbcc54bdSEnrico Perla - Sun Microsystems 			break;
7602bbcc54bdSEnrico Perla - Sun Microsystems 		}
7603bbcc54bdSEnrico Perla - Sun Microsystems 
76048c1b6884Sszhou 	}
76058c1b6884Sszhou 
7606eb2bd662Svikram 	if (ent && entry_num) {
76078c1b6884Sszhou 		*entry_num = i;
7608843e1988Sjohnlev 	}
7609eb2bd662Svikram 
7610eb2bd662Svikram 	if (ent) {
7611eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_RET, fcn, i));
7612eb2bd662Svikram 	} else {
7613eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_RET, fcn, BAM_ERROR));
7614eb2bd662Svikram 	}
76158c1b6884Sszhou 	return (ent);
76168c1b6884Sszhou }
76178c1b6884Sszhou 
76188c1b6884Sszhou static int
7619eb2bd662Svikram update_boot_entry(menu_t *mp, char *title, char *findroot, char *root,
7620eb2bd662Svikram     char *kernel, char *mod_kernel, char *module, int root_opt)
76218c1b6884Sszhou {
7622eb2bd662Svikram 	int		i;
7623eb2bd662Svikram 	int		change_kernel = 0;
76248c1b6884Sszhou 	entry_t		*ent;
76258c1b6884Sszhou 	line_t		*lp;
7626eb2bd662Svikram 	line_t		*tlp;
76278c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
7628eb2bd662Svikram 	const char	*fcn = "update_boot_entry()";
76298c1b6884Sszhou 
76308c1b6884Sszhou 	/* note: don't match on title, it's updated on upgrade */
7631eb2bd662Svikram 	ent = find_boot_entry(mp, NULL, kernel, findroot, root, module,
7632eb2bd662Svikram 	    root_opt, &i);
7633ae115bc7Smrj 	if ((ent == NULL) && (bam_direct == BAM_DIRECT_DBOOT)) {
7634ae115bc7Smrj 		/*
7635ae115bc7Smrj 		 * We may be upgrading a kernel from multiboot to
7636eb2bd662Svikram 		 * directboot.  Look for a multiboot entry. A multiboot
7637eb2bd662Svikram 		 * entry will not have a findroot line.
7638ae115bc7Smrj 		 */
7639eb2bd662Svikram 		ent = find_boot_entry(mp, NULL, "multiboot", NULL, root,
7640eb2bd662Svikram 		    MULTIBOOT_ARCHIVE, root_opt, &i);
7641ae115bc7Smrj 		if (ent != NULL) {
7642eb2bd662Svikram 			BAM_DPRINTF((D_UPGRADE_FROM_MULTIBOOT, fcn, root));
7643ae115bc7Smrj 			change_kernel = 1;
7644ae115bc7Smrj 		}
7645eb2bd662Svikram 	} else if (ent) {
7646eb2bd662Svikram 		BAM_DPRINTF((D_FOUND_FINDROOT, fcn, findroot));
7647ae115bc7Smrj 	}
76488c1b6884Sszhou 
7649eb2bd662Svikram 	if (ent == NULL) {
7650eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_NOT_FOUND_CREATING, fcn, findroot));
7651eb2bd662Svikram 		return (add_boot_entry(mp, title, findroot,
7652eb2bd662Svikram 		    kernel, mod_kernel, module));
7653eb2bd662Svikram 	}
7654eb2bd662Svikram 
7655eb2bd662Svikram 	/* replace title of existing entry and update findroot line */
76568c1b6884Sszhou 	lp = ent->start;
76578c1b6884Sszhou 	lp = lp->next;	/* title line */
76588c1b6884Sszhou 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
76598c1b6884Sszhou 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
76608c1b6884Sszhou 	free(lp->arg);
76618c1b6884Sszhou 	free(lp->line);
76628c1b6884Sszhou 	lp->arg = s_strdup(title);
76638c1b6884Sszhou 	lp->line = s_strdup(linebuf);
7664eb2bd662Svikram 	BAM_DPRINTF((D_CHANGING_TITLE, fcn, title));
76658c1b6884Sszhou 
7666eb2bd662Svikram 	tlp = lp;	/* title line */
76678c1b6884Sszhou 	lp = lp->next;	/* root line */
7668eb2bd662Svikram 
7669eb2bd662Svikram 	/* if no root or findroot command, create a new line_t */
7670eb2bd662Svikram 	if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) != 0 &&
7671eb2bd662Svikram 	    strcmp(lp->cmd, menu_cmds[FINDROOT_CMD]) != 0) {
7672eb2bd662Svikram 		lp = s_calloc(1, sizeof (line_t));
7673eb2bd662Svikram 		bam_add_line(mp, ent, tlp, lp);
7674eb2bd662Svikram 	} else {
7675eb2bd662Svikram 		free(lp->cmd);
7676eb2bd662Svikram 		free(lp->sep);
7677eb2bd662Svikram 		free(lp->arg);
7678eb2bd662Svikram 		free(lp->line);
76798c1b6884Sszhou 	}
7680ae115bc7Smrj 
7681eb2bd662Svikram 	lp->cmd = s_strdup(menu_cmds[FINDROOT_CMD]);
7682eb2bd662Svikram 	lp->sep = s_strdup(menu_cmds[SEP_CMD]);
7683eb2bd662Svikram 	lp->arg = s_strdup(findroot);
7684eb2bd662Svikram 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7685eb2bd662Svikram 	    menu_cmds[FINDROOT_CMD], menu_cmds[SEP_CMD], findroot);
7686eb2bd662Svikram 	lp->line = s_strdup(linebuf);
7687eb2bd662Svikram 	BAM_DPRINTF((D_ADDING_FINDROOT_LINE, fcn, findroot));
7688eb2bd662Svikram 
7689eb2bd662Svikram 	/* kernel line */
7690eb2bd662Svikram 	lp = lp->next;
7691eb2bd662Svikram 
7692bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSKERNEL) {
7693bbcc54bdSEnrico Perla - Sun Microsystems 		char		*params = NULL;
7694bbcc54bdSEnrico Perla - Sun Microsystems 
7695bbcc54bdSEnrico Perla - Sun Microsystems 		params = strstr(lp->line, "-s");
7696bbcc54bdSEnrico Perla - Sun Microsystems 		if (params != NULL)
7697bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s%s",
7698bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
7699bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel, params+2);
7700bbcc54bdSEnrico Perla - Sun Microsystems 		else
7701bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7702bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
7703bbcc54bdSEnrico Perla - Sun Microsystems 			    kernel);
7704bbcc54bdSEnrico Perla - Sun Microsystems 
7705bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->cmd);
7706bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->arg);
7707bbcc54bdSEnrico Perla - Sun Microsystems 		free(lp->line);
7708bbcc54bdSEnrico Perla - Sun Microsystems 		lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
7709bbcc54bdSEnrico Perla - Sun Microsystems 		lp->arg = s_strdup(strstr(linebuf, "/"));
7710bbcc54bdSEnrico Perla - Sun Microsystems 		lp->line = s_strdup(linebuf);
7711bbcc54bdSEnrico Perla - Sun Microsystems 		ent->flags &= ~BAM_ENTRY_UPGFSKERNEL;
7712bbcc54bdSEnrico Perla - Sun Microsystems 		BAM_DPRINTF((D_ADDING_KERNEL_DOLLAR, fcn, lp->prev->cmd));
7713bbcc54bdSEnrico Perla - Sun Microsystems 	}
7714bbcc54bdSEnrico Perla - Sun Microsystems 
7715ae115bc7Smrj 	if (change_kernel) {
7716ae115bc7Smrj 		/*
7717ae115bc7Smrj 		 * We're upgrading from multiboot to directboot.
7718ae115bc7Smrj 		 */
7719ae115bc7Smrj 		if (strcmp(lp->cmd, menu_cmds[KERNEL_CMD]) == 0) {
7720ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7721ae115bc7Smrj 			    menu_cmds[KERNEL_DOLLAR_CMD], menu_cmds[SEP_CMD],
7722ae115bc7Smrj 			    kernel);
7723eb2bd662Svikram 			free(lp->cmd);
7724ae115bc7Smrj 			free(lp->arg);
7725ae115bc7Smrj 			free(lp->line);
7726eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[KERNEL_DOLLAR_CMD]);
7727ae115bc7Smrj 			lp->arg = s_strdup(kernel);
7728ae115bc7Smrj 			lp->line = s_strdup(linebuf);
7729ae115bc7Smrj 			lp = lp->next;
7730eb2bd662Svikram 			BAM_DPRINTF((D_ADDING_KERNEL_DOLLAR, fcn, kernel));
7731ae115bc7Smrj 		}
7732ae115bc7Smrj 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
7733ae115bc7Smrj 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7734ae115bc7Smrj 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
7735ae115bc7Smrj 			    module);
7736eb2bd662Svikram 			free(lp->cmd);
7737ae115bc7Smrj 			free(lp->arg);
7738ae115bc7Smrj 			free(lp->line);
7739eb2bd662Svikram 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
7740ae115bc7Smrj 			lp->arg = s_strdup(module);
7741ae115bc7Smrj 			lp->line = s_strdup(linebuf);
7742ae115bc7Smrj 			lp = lp->next;
7743eb2bd662Svikram 			BAM_DPRINTF((D_ADDING_MODULE_DOLLAR, fcn, module));
7744ae115bc7Smrj 		}
7745ae115bc7Smrj 	}
7746bbcc54bdSEnrico Perla - Sun Microsystems 
7747bbcc54bdSEnrico Perla - Sun Microsystems 	/* module line */
7748bbcc54bdSEnrico Perla - Sun Microsystems 	lp = lp->next;
7749bbcc54bdSEnrico Perla - Sun Microsystems 
7750bbcc54bdSEnrico Perla - Sun Microsystems 	if (ent->flags & BAM_ENTRY_UPGFSMODULE) {
7751bbcc54bdSEnrico Perla - Sun Microsystems 		if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) == 0) {
7752bbcc54bdSEnrico Perla - Sun Microsystems 			(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
7753bbcc54bdSEnrico Perla - Sun Microsystems 			    menu_cmds[MODULE_DOLLAR_CMD], menu_cmds[SEP_CMD],
7754bbcc54bdSEnrico Perla - Sun Microsystems 			    module);
7755bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->cmd);
7756bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->arg);
7757bbcc54bdSEnrico Perla - Sun Microsystems 			free(lp->line);
7758bbcc54bdSEnrico Perla - Sun Microsystems 			lp->cmd = s_strdup(menu_cmds[MODULE_DOLLAR_CMD]);
7759bbcc54bdSEnrico Perla - Sun Microsystems 			lp->arg = s_strdup(module);
7760bbcc54bdSEnrico Perla - Sun Microsystems 			lp->line = s_strdup(linebuf);
7761bbcc54bdSEnrico Perla - Sun Microsystems 			lp = lp->next;
7762bbcc54bdSEnrico Perla - Sun Microsystems 			ent->flags &= ~BAM_ENTRY_UPGFSMODULE;
7763bbcc54bdSEnrico Perla - Sun Microsystems 			BAM_DPRINTF((D_ADDING_MODULE_DOLLAR, fcn, module));
7764bbcc54bdSEnrico Perla - Sun Microsystems 		}
7765bbcc54bdSEnrico Perla - Sun Microsystems 	}
7766bbcc54bdSEnrico Perla - Sun Microsystems 
7767eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_RET, fcn, i));
77688c1b6884Sszhou 	return (i);
77698c1b6884Sszhou }
77708c1b6884Sszhou 
7771eb2bd662Svikram int
7772eb2bd662Svikram root_optional(char *osroot, char *menu_root)
7773eb2bd662Svikram {
7774eb2bd662Svikram 	char			*ospecial;
7775eb2bd662Svikram 	char			*mspecial;
7776eb2bd662Svikram 	char			*slash;
7777eb2bd662Svikram 	int			root_opt;
7778eb2bd662Svikram 	int			ret1;
7779eb2bd662Svikram 	int			ret2;
7780eb2bd662Svikram 	const char		*fcn = "root_optional()";
7781eb2bd662Svikram 
7782eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, osroot, menu_root));
7783eb2bd662Svikram 
7784eb2bd662Svikram 	/*
7785eb2bd662Svikram 	 * For all filesystems except ZFS, a straight compare of osroot
7786eb2bd662Svikram 	 * and menu_root will tell us if root is optional.
7787eb2bd662Svikram 	 * For ZFS, the situation is complicated by the fact that
7788eb2bd662Svikram 	 * menu_root and osroot are always different
7789eb2bd662Svikram 	 */
7790eb2bd662Svikram 	ret1 = is_zfs(osroot);
7791eb2bd662Svikram 	ret2 = is_zfs(menu_root);
7792eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPT_NOT_ZFS", ret1 = 0);
7793eb2bd662Svikram 	if (!ret1 || !ret2) {
7794eb2bd662Svikram 		BAM_DPRINTF((D_ROOT_OPT_NOT_ZFS, fcn, osroot, menu_root));
7795eb2bd662Svikram 		root_opt = (strcmp(osroot, menu_root) == 0);
7796eb2bd662Svikram 		goto out;
7797eb2bd662Svikram 	}
7798eb2bd662Svikram 
7799eb2bd662Svikram 	ospecial = get_special(osroot);
7800eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_OSPECIAL", ospecial = NULL);
7801eb2bd662Svikram 	if (ospecial == NULL) {
7802eb2bd662Svikram 		bam_error(GET_OSROOT_SPECIAL_ERR, osroot);
7803eb2bd662Svikram 		return (0);
7804eb2bd662Svikram 	}
7805eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_OSPECIAL, fcn, ospecial, osroot));
7806eb2bd662Svikram 
7807eb2bd662Svikram 	mspecial = get_special(menu_root);
7808eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_MSPECIAL", mspecial = NULL);
7809eb2bd662Svikram 	if (mspecial == NULL) {
7810eb2bd662Svikram 		bam_error(GET_MENU_ROOT_SPECIAL_ERR, menu_root);
7811eb2bd662Svikram 		free(ospecial);
7812eb2bd662Svikram 		return (0);
7813eb2bd662Svikram 	}
7814eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_MSPECIAL, fcn, mspecial, menu_root));
7815eb2bd662Svikram 
7816eb2bd662Svikram 	slash = strchr(ospecial, '/');
7817eb2bd662Svikram 	if (slash)
7818eb2bd662Svikram 		*slash = '\0';
7819eb2bd662Svikram 	BAM_DPRINTF((D_ROOT_OPTIONAL_FIXED_OSPECIAL, fcn, ospecial, osroot));
7820eb2bd662Svikram 
7821eb2bd662Svikram 	root_opt = (strcmp(ospecial, mspecial) == 0);
7822eb2bd662Svikram 
7823eb2bd662Svikram 	free(ospecial);
7824eb2bd662Svikram 	free(mspecial);
7825eb2bd662Svikram 
7826eb2bd662Svikram out:
7827eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_NO", root_opt = 0);
7828eb2bd662Svikram 	INJECT_ERROR1("ROOT_OPTIONAL_YES", root_opt = 1);
7829eb2bd662Svikram 	if (root_opt) {
7830eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
7831eb2bd662Svikram 	} else {
7832eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
7833eb2bd662Svikram 	}
7834eb2bd662Svikram 
7835eb2bd662Svikram 	return (root_opt);
7836eb2bd662Svikram }
7837eb2bd662Svikram 
78387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
78397c478bd9Sstevel@tonic-gate static error_t
7840eb2bd662Svikram update_entry(menu_t *mp, char *menu_root, char *osdev)
78417c478bd9Sstevel@tonic-gate {
78427c478bd9Sstevel@tonic-gate 	int		entry;
7843eb2bd662Svikram 	char		*grubsign;
7844eb2bd662Svikram 	char		*grubroot;
7845eb2bd662Svikram 	char		*title;
7846eb2bd662Svikram 	char		osroot[PATH_MAX];
7847eb2bd662Svikram 	char		*failsafe_kernel = NULL;
78488c1b6884Sszhou 	struct stat	sbuf;
78498c1b6884Sszhou 	char		failsafe[256];
7850bbcc54bdSEnrico Perla - Sun Microsystems 	char		failsafe_64[256];
7851eb2bd662Svikram 	int		ret;
7852eb2bd662Svikram 	const char	*fcn = "update_entry()";
78537c478bd9Sstevel@tonic-gate 
78547c478bd9Sstevel@tonic-gate 	assert(mp);
7855eb2bd662Svikram 	assert(menu_root);
7856eb2bd662Svikram 	assert(osdev);
7857eb2bd662Svikram 	assert(bam_root);
78587c478bd9Sstevel@tonic-gate 
7859eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY3, fcn, menu_root, osdev, bam_root));
7860eb2bd662Svikram 
7861eb2bd662Svikram 	(void) strlcpy(osroot, bam_root, sizeof (osroot));
7862eb2bd662Svikram 
78637c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
7864eb2bd662Svikram 	assert(title);
78657c478bd9Sstevel@tonic-gate 
7866eb2bd662Svikram 	grubsign = get_grubsign(osroot, osdev);
7867eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBSIGN_FAIL", grubsign = NULL);
7868eb2bd662Svikram 	if (grubsign == NULL) {
7869eb2bd662Svikram 		bam_error(GET_GRUBSIGN_ERROR, osroot, osdev);
78707c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
78717c478bd9Sstevel@tonic-gate 	}
7872eb2bd662Svikram 
7873eb2bd662Svikram 	/*
7874eb2bd662Svikram 	 * It is not a fatal error if get_grubroot() fails
7875eb2bd662Svikram 	 * We no longer rely on biosdev to populate the
7876eb2bd662Svikram 	 * menu
7877eb2bd662Svikram 	 */
7878eb2bd662Svikram 	grubroot = get_grubroot(osroot, osdev, menu_root);
7879eb2bd662Svikram 	INJECT_ERROR1("GET_GRUBROOT_FAIL", grubroot = NULL);
7880eb2bd662Svikram 	if (grubroot) {
7881eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBROOT_SUCCESS,
7882eb2bd662Svikram 		    fcn, osroot, osdev, menu_root));
7883eb2bd662Svikram 	} else {
7884eb2bd662Svikram 		BAM_DPRINTF((D_GET_GRUBROOT_FAILURE,
7885eb2bd662Svikram 		    fcn, osroot, osdev, menu_root));
78867c478bd9Sstevel@tonic-gate 	}
78877c478bd9Sstevel@tonic-gate 
78887c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
7889eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_MULTIBOOT",
7890eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
7891ae115bc7Smrj 	if (bam_direct == BAM_DIRECT_DBOOT) {
7892eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
7893e7cbe64fSgw25295 		    (bam_zfs ? DIRECT_BOOT_KERNEL_ZFS : DIRECT_BOOT_KERNEL),
7894eb2bd662Svikram 		    NULL, DIRECT_BOOT_ARCHIVE,
7895eb2bd662Svikram 		    root_optional(osroot, menu_root));
7896eb2bd662Svikram 		BAM_DPRINTF((D_UPDATED_BOOT_ENTRY, fcn, bam_zfs, grubsign));
7897843e1988Sjohnlev 		if ((entry != BAM_ERROR) && (bam_is_hv == BAM_HV_PRESENT)) {
7898eb2bd662Svikram 			(void) update_boot_entry(mp, NEW_HV_ENTRY, grubsign,
7899eb2bd662Svikram 			    grubroot, XEN_MENU, bam_zfs ?
7900eb2bd662Svikram 			    XEN_KERNEL_MODULE_LINE_ZFS : XEN_KERNEL_MODULE_LINE,
7901eb2bd662Svikram 			    DIRECT_BOOT_ARCHIVE,
7902eb2bd662Svikram 			    root_optional(osroot, menu_root));
7903eb2bd662Svikram 			BAM_DPRINTF((D_UPDATED_HV_ENTRY,
7904eb2bd662Svikram 			    fcn, bam_zfs, grubsign));
7905ae115bc7Smrj 		}
7906843e1988Sjohnlev 	} else {
7907eb2bd662Svikram 		entry = update_boot_entry(mp, title, grubsign, grubroot,
7908eb2bd662Svikram 		    MULTI_BOOT, NULL, MULTIBOOT_ARCHIVE,
7909eb2bd662Svikram 		    root_optional(osroot, menu_root));
7910eb2bd662Svikram 
7911eb2bd662Svikram 		BAM_DPRINTF((D_UPDATED_MULTIBOOT_ENTRY, fcn, grubsign));
7912843e1988Sjohnlev 	}
79137c478bd9Sstevel@tonic-gate 
7914843e1988Sjohnlev 	/*
7915843e1988Sjohnlev 	 * Add the entry for failsafe archive.  On a bfu'd system, the
7916843e1988Sjohnlev 	 * failsafe may be different than the installed kernel.
7917843e1988Sjohnlev 	 */
7918eb2bd662Svikram 	(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
7919bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_32);
7920bbcc54bdSEnrico Perla - Sun Microsystems 	(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
7921bbcc54bdSEnrico Perla - Sun Microsystems 	    osroot, FAILSAFE_ARCHIVE_64);
7922bbcc54bdSEnrico Perla - Sun Microsystems 
7923bbcc54bdSEnrico Perla - Sun Microsystems 	/*
7924bbcc54bdSEnrico Perla - Sun Microsystems 	 * Check if at least one of the two archives exists
7925bbcc54bdSEnrico Perla - Sun Microsystems 	 * Using $ISADIR as the default line, we have an entry which works
7926bbcc54bdSEnrico Perla - Sun Microsystems 	 * for both the cases.
7927bbcc54bdSEnrico Perla - Sun Microsystems 	 */
7928bbcc54bdSEnrico Perla - Sun Microsystems 
7929bbcc54bdSEnrico Perla - Sun Microsystems 	if (stat(failsafe, &sbuf) == 0 || stat(failsafe_64, &sbuf) == 0) {
793060d0a590Srscott 
793160d0a590Srscott 		/* Figure out where the kernel line should point */
793260d0a590Srscott 		(void) snprintf(failsafe, sizeof (failsafe), "%s%s", osroot,
7933bbcc54bdSEnrico Perla - Sun Microsystems 		    DIRECT_BOOT_FAILSAFE_32);
7934bbcc54bdSEnrico Perla - Sun Microsystems 		(void) snprintf(failsafe_64, sizeof (failsafe_64), "%s%s",
7935bbcc54bdSEnrico Perla - Sun Microsystems 		    osroot, DIRECT_BOOT_FAILSAFE_64);
7936bbcc54bdSEnrico Perla - Sun Microsystems 		if (stat(failsafe, &sbuf) == 0 ||
7937bbcc54bdSEnrico Perla - Sun Microsystems 		    stat(failsafe_64, &sbuf) == 0) {
7938963390b4Svikram 			failsafe_kernel = DIRECT_BOOT_FAILSAFE_LINE;
793960d0a590Srscott 		} else {
794060d0a590Srscott 			(void) snprintf(failsafe, sizeof (failsafe), "%s%s",
794160d0a590Srscott 			    osroot, MULTI_BOOT_FAILSAFE);
794260d0a590Srscott 			if (stat(failsafe, &sbuf) == 0) {
794360d0a590Srscott 				failsafe_kernel = MULTI_BOOT_FAILSAFE_LINE;
794460d0a590Srscott 			}
794560d0a590Srscott 		}
794660d0a590Srscott 		if (failsafe_kernel != NULL) {
7947eb2bd662Svikram 			(void) update_boot_entry(mp, FAILSAFE_TITLE, grubsign,
7948eb2bd662Svikram 			    grubroot, failsafe_kernel, NULL, FAILSAFE_ARCHIVE,
7949eb2bd662Svikram 			    root_optional(osroot, menu_root));
7950eb2bd662Svikram 			BAM_DPRINTF((D_UPDATED_FAILSAFE_ENTRY, fcn,
7951eb2bd662Svikram 			    failsafe_kernel));
795260d0a590Srscott 		}
7953ae115bc7Smrj 	}
7954eb2bd662Svikram 	free(grubroot);
79557c478bd9Sstevel@tonic-gate 
7956eb2bd662Svikram 	INJECT_ERROR1("UPDATE_ENTRY_ERROR", entry = BAM_ERROR);
79577c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
7958eb2bd662Svikram 		bam_error(FAILED_TO_ADD_BOOT_ENTRY, title, grubsign);
7959eb2bd662Svikram 		free(grubsign);
79607c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
79617c478bd9Sstevel@tonic-gate 	}
7962eb2bd662Svikram 	free(grubsign);
7963eb2bd662Svikram 
7964eb2bd662Svikram 	update_numbering(mp);
7965eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
7966eb2bd662Svikram 	INJECT_ERROR1("SET_DEFAULT_ERROR", ret = BAM_ERROR);
7967eb2bd662Svikram 	if (ret == BAM_ERROR) {
7968eb2bd662Svikram 		bam_error(SET_DEFAULT_FAILED, entry);
7969eb2bd662Svikram 	}
7970eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
79717c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
79727c478bd9Sstevel@tonic-gate }
79737c478bd9Sstevel@tonic-gate 
79748c1b6884Sszhou static void
7975ae115bc7Smrj save_default_entry(menu_t *mp, const char *which)
79768c1b6884Sszhou {
7977eb2bd662Svikram 	int		lineNum;
7978eb2bd662Svikram 	int		entryNum;
79798c1b6884Sszhou 	int		entry = 0;	/* default is 0 */
79808c1b6884Sszhou 	char		linebuf[BAM_MAXLINE];
79818c1b6884Sszhou 	line_t		*lp = mp->curdefault;
7982eb2bd662Svikram 	const char	*fcn = "save_default_entry()";
79838c1b6884Sszhou 
7984bff269d0Svikram 	if (mp->start) {
7985bff269d0Svikram 		lineNum = mp->end->lineNum;
7986bff269d0Svikram 		entryNum = mp->end->entryNum;
7987bff269d0Svikram 	} else {
7988bff269d0Svikram 		lineNum = LINE_INIT;
7989bff269d0Svikram 		entryNum = ENTRY_INIT;
7990bff269d0Svikram 	}
7991bff269d0Svikram 
79928c1b6884Sszhou 	if (lp)
79938c1b6884Sszhou 		entry = s_strtol(lp->arg);
79948c1b6884Sszhou 
7995ae115bc7Smrj 	(void) snprintf(linebuf, sizeof (linebuf), "#%s%d", which, entry);
7996eb2bd662Svikram 	BAM_DPRINTF((D_SAVING_DEFAULT_TO, fcn, linebuf));
79978c1b6884Sszhou 	line_parser(mp, linebuf, &lineNum, &entryNum);
7998eb2bd662Svikram 	BAM_DPRINTF((D_SAVED_DEFAULT_TO, fcn, lineNum, entryNum));
79998c1b6884Sszhou }
80008c1b6884Sszhou 
80018c1b6884Sszhou static void
8002ae115bc7Smrj restore_default_entry(menu_t *mp, const char *which, line_t *lp)
80038c1b6884Sszhou {
80048c1b6884Sszhou 	int		entry;
80058c1b6884Sszhou 	char		*str;
8006eb2bd662Svikram 	const char	*fcn = "restore_default_entry()";
80078c1b6884Sszhou 
8008eb2bd662Svikram 	if (lp == NULL) {
8009eb2bd662Svikram 		BAM_DPRINTF((D_RESTORE_DEFAULT_NULL, fcn));
80108c1b6884Sszhou 		return;		/* nothing to restore */
8011eb2bd662Svikram 	}
8012eb2bd662Svikram 
8013eb2bd662Svikram 	BAM_DPRINTF((D_RESTORE_DEFAULT_STR, fcn, which));
80148c1b6884Sszhou 
8015ae115bc7Smrj 	str = lp->arg + strlen(which);
80168c1b6884Sszhou 	entry = s_strtol(str);
80178c1b6884Sszhou 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
80188c1b6884Sszhou 
8019eb2bd662Svikram 	BAM_DPRINTF((D_RESTORED_DEFAULT_TO, fcn, entry));
8020eb2bd662Svikram 
80218c1b6884Sszhou 	/* delete saved old default line */
80228c1b6884Sszhou 	unlink_line(mp, lp);
80238c1b6884Sszhou 	line_free(lp);
80248c1b6884Sszhou }
80258c1b6884Sszhou 
80267c478bd9Sstevel@tonic-gate /*
80277c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
80287c478bd9Sstevel@tonic-gate  * The opt value can be:
80297c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
8030eb2bd662Svikram  * entry=<n>	switches default entry to <n>
80317c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
80327c478bd9Sstevel@tonic-gate  *		and make it the default
8033eb2bd662Svikram  * Note that we are always rebooting the current OS instance
8034eb2bd662Svikram  * so osroot == / always.
80357c478bd9Sstevel@tonic-gate  */
80367c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
80377c478bd9Sstevel@tonic-gate 
80388c1b6884Sszhou /*ARGSUSED*/
80397c478bd9Sstevel@tonic-gate static error_t
8040eb2bd662Svikram update_temp(menu_t *mp, char *dummy, char *opt)
80417c478bd9Sstevel@tonic-gate {
80427c478bd9Sstevel@tonic-gate 	int		entry;
8043eb2bd662Svikram 	char		*osdev;
8044eb2bd662Svikram 	char		*fstype;
8045eb2bd662Svikram 	char		*sign;
8046eb2bd662Svikram 	char		*opt_ptr;
8047eb2bd662Svikram 	char		*path;
8048ae115bc7Smrj 	char		kernbuf[BUFSIZ];
8049ae115bc7Smrj 	char		args_buf[BUFSIZ];
8050eb2bd662Svikram 	char		signbuf[PATH_MAX];
8051eb2bd662Svikram 	int		ret;
8052eb2bd662Svikram 	const char	*fcn = "update_temp()";
80537c478bd9Sstevel@tonic-gate 
80547c478bd9Sstevel@tonic-gate 	assert(mp);
8055eb2bd662Svikram 	assert(dummy == NULL);
8056eb2bd662Svikram 
8057eb2bd662Svikram 	/* opt can be NULL */
8058eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt ? opt : "<NULL>"));
8059eb2bd662Svikram 	BAM_DPRINTF((D_BAM_ROOT, fcn, bam_alt_root, bam_root));
8060eb2bd662Svikram 
8061eb2bd662Svikram 	if (bam_alt_root || bam_rootlen != 1 ||
8062eb2bd662Svikram 	    strcmp(bam_root, "/") != 0 ||
8063eb2bd662Svikram 	    strcmp(rootbuf, "/") != 0) {
8064eb2bd662Svikram 		bam_error(ALT_ROOT_INVALID, bam_root);
8065eb2bd662Svikram 		return (BAM_ERROR);
8066eb2bd662Svikram 	}
80677c478bd9Sstevel@tonic-gate 
80688c1b6884Sszhou 	/* If no option, delete exiting reboot menu entry */
80698c1b6884Sszhou 	if (opt == NULL) {
8070eb2bd662Svikram 		entry_t		*ent;
8071eb2bd662Svikram 		BAM_DPRINTF((D_OPT_NULL, fcn));
8072eb2bd662Svikram 		ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL,
8073eb2bd662Svikram 		    NULL, NULL, 0, &entry);
8074eb2bd662Svikram 		if (ent == NULL) {	/* not found is ok */
8075eb2bd662Svikram 			BAM_DPRINTF((D_TRANSIENT_NOTFOUND, fcn));
80768c1b6884Sszhou 			return (BAM_SUCCESS);
8077eb2bd662Svikram 		}
80788c1b6884Sszhou 		(void) do_delete(mp, entry);
8079ae115bc7Smrj 		restore_default_entry(mp, BAM_OLDDEF, mp->olddefault);
8080ae115bc7Smrj 		mp->olddefault = NULL;
8081eb2bd662Svikram 		BAM_DPRINTF((D_RESTORED_DEFAULT, fcn));
8082eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
80838c1b6884Sszhou 		return (BAM_WRITE);
80848c1b6884Sszhou 	}
80858c1b6884Sszhou 
80868c1b6884Sszhou 	/* if entry= is specified, set the default entry */
8087eb2bd662Svikram 	if (strncmp(opt, "entry=", strlen("entry=")) == 0) {
8088eb2bd662Svikram 		int entryNum = s_strtol(opt + strlen("entry="));
8089eb2bd662Svikram 		BAM_DPRINTF((D_ENTRY_EQUALS, fcn, opt));
8090eb2bd662Svikram 		if (selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
80917c478bd9Sstevel@tonic-gate 			/* this is entry=# option */
8092eb2bd662Svikram 			ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8093eb2bd662Svikram 			BAM_DPRINTF((D_ENTRY_SET_IS, fcn, entry, ret));
8094eb2bd662Svikram 			return (ret);
8095eb2bd662Svikram 		} else {
8096eb2bd662Svikram 			bam_error(SET_DEFAULT_FAILED, entryNum);
8097eb2bd662Svikram 			return (BAM_ERROR);
8098eb2bd662Svikram 		}
80997c478bd9Sstevel@tonic-gate 	}
81007c478bd9Sstevel@tonic-gate 
81017c478bd9Sstevel@tonic-gate 	/*
8102eb2bd662Svikram 	 * add a new menu entry based on opt and make it the default
8103b610f78eSvikram 	 */
8104eb2bd662Svikram 
8105eb2bd662Svikram 	fstype = get_fstype("/");
8106eb2bd662Svikram 	INJECT_ERROR1("REBOOT_FSTYPE_NULL", fstype = NULL);
8107eb2bd662Svikram 	if (fstype == NULL) {
8108eb2bd662Svikram 		bam_error(REBOOT_FSTYPE_FAILED);
8109eb2bd662Svikram 		return (BAM_ERROR);
81107c478bd9Sstevel@tonic-gate 	}
8111eb2bd662Svikram 
8112eb2bd662Svikram 	osdev = get_special("/");
8113eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SPECIAL_NULL", osdev = NULL);
8114eb2bd662Svikram 	if (osdev == NULL) {
8115eb2bd662Svikram 		free(fstype);
8116eb2bd662Svikram 		bam_error(REBOOT_SPECIAL_FAILED);
8117eb2bd662Svikram 		return (BAM_ERROR);
8118b610f78eSvikram 	}
8119eb2bd662Svikram 
8120eb2bd662Svikram 	sign = find_existing_sign("/", osdev, fstype);
8121eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SIGN_NULL", sign = NULL);
8122eb2bd662Svikram 	if (sign == NULL) {
8123eb2bd662Svikram 		free(fstype);
8124eb2bd662Svikram 		free(osdev);
8125eb2bd662Svikram 		bam_error(REBOOT_SIGN_FAILED);
8126eb2bd662Svikram 		return (BAM_ERROR);
8127eb2bd662Svikram 	}
8128eb2bd662Svikram 
8129eb2bd662Svikram 	free(osdev);
8130eb2bd662Svikram 	(void) strlcpy(signbuf, sign, sizeof (signbuf));
8131eb2bd662Svikram 	free(sign);
8132eb2bd662Svikram 
8133eb2bd662Svikram 	assert(strchr(signbuf, '(') == NULL && strchr(signbuf, ',') == NULL &&
8134eb2bd662Svikram 	    strchr(signbuf, ')') == NULL);
8135eb2bd662Svikram 
8136eb2bd662Svikram 	/*
8137eb2bd662Svikram 	 * There is no alternate root while doing reboot with args
8138eb2bd662Svikram 	 * This version of bootadm is only delivered with a DBOOT
8139eb2bd662Svikram 	 * version of Solaris.
8140eb2bd662Svikram 	 */
8141eb2bd662Svikram 	INJECT_ERROR1("REBOOT_NOT_DBOOT", bam_direct = BAM_DIRECT_MULTIBOOT);
8142eb2bd662Svikram 	if (bam_direct != BAM_DIRECT_DBOOT) {
81439bd6d269SLin Ling 		free(fstype);
8144eb2bd662Svikram 		bam_error(REBOOT_DIRECT_FAILED);
81457c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
81467c478bd9Sstevel@tonic-gate 	}
81477c478bd9Sstevel@tonic-gate 
81487c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
8149ae115bc7Smrj 	if (opt[0] == '-') {
8150ae115bc7Smrj 		/* It's an option - first see if boot-file is set */
8151eb2bd662Svikram 		ret = get_kernel(mp, KERNEL_CMD, kernbuf, sizeof (kernbuf));
8152eb2bd662Svikram 		INJECT_ERROR1("REBOOT_GET_KERNEL", ret = BAM_ERROR);
8153eb2bd662Svikram 		if (ret != BAM_SUCCESS) {
81549bd6d269SLin Ling 			free(fstype);
8155eb2bd662Svikram 			bam_error(REBOOT_GET_KERNEL_FAILED);
8156ae115bc7Smrj 			return (BAM_ERROR);
8157eb2bd662Svikram 		}
8158ae115bc7Smrj 		if (kernbuf[0] == '\0')
8159eb2bd662Svikram 			(void) strlcpy(kernbuf, DIRECT_BOOT_KERNEL,
8160eb2bd662Svikram 			    sizeof (kernbuf));
81619bd6d269SLin Ling 		/*
81629bd6d269SLin Ling 		 * If this is a zfs file system and kernbuf does not
81639bd6d269SLin Ling 		 * have "-B $ZFS-BOOTFS" string yet, add it.
81649bd6d269SLin Ling 		 */
81659bd6d269SLin Ling 		if (strcmp(fstype, "zfs") == 0 && !strstr(kernbuf, ZFS_BOOT)) {
81669bd6d269SLin Ling 			(void) strlcat(kernbuf, " ", sizeof (kernbuf));
81679bd6d269SLin Ling 			(void) strlcat(kernbuf, ZFS_BOOT, sizeof (kernbuf));
81689bd6d269SLin Ling 		}
8169eb2bd662Svikram 		(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8170eb2bd662Svikram 		(void) strlcat(kernbuf, opt, sizeof (kernbuf));
8171eb2bd662Svikram 		BAM_DPRINTF((D_REBOOT_OPTION, fcn, kernbuf));
8172ae115bc7Smrj 	} else if (opt[0] == '/') {
8173455710d3Srscott 		/* It's a full path, so write it out. */
8174eb2bd662Svikram 		(void) strlcpy(kernbuf, opt, sizeof (kernbuf));
8175455710d3Srscott 
8176455710d3Srscott 		/*
8177455710d3Srscott 		 * If someone runs:
8178455710d3Srscott 		 *
8179455710d3Srscott 		 *	# eeprom boot-args='-kd'
8180455710d3Srscott 		 *	# reboot /platform/i86pc/kernel/unix
8181455710d3Srscott 		 *
8182455710d3Srscott 		 * we want to use the boot-args as part of the boot
8183455710d3Srscott 		 * line.  On the other hand, if someone runs:
8184455710d3Srscott 		 *
8185455710d3Srscott 		 *	# reboot "/platform/i86pc/kernel/unix -kd"
8186455710d3Srscott 		 *
8187455710d3Srscott 		 * we don't need to mess with boot-args.  If there's
8188455710d3Srscott 		 * no space in the options string, assume we're in the
8189455710d3Srscott 		 * first case.
8190455710d3Srscott 		 */
8191455710d3Srscott 		if (strchr(opt, ' ') == NULL) {
8192eb2bd662Svikram 			ret = get_kernel(mp, ARGS_CMD, args_buf,
8193eb2bd662Svikram 			    sizeof (args_buf));
8194eb2bd662Svikram 			INJECT_ERROR1("REBOOT_GET_ARGS", ret = BAM_ERROR);
8195eb2bd662Svikram 			if (ret != BAM_SUCCESS) {
81969bd6d269SLin Ling 				free(fstype);
8197eb2bd662Svikram 				bam_error(REBOOT_GET_ARGS_FAILED);
8198455710d3Srscott 				return (BAM_ERROR);
8199eb2bd662Svikram 			}
8200455710d3Srscott 
8201455710d3Srscott 			if (args_buf[0] != '\0') {
8202eb2bd662Svikram 				(void) strlcat(kernbuf, " ", sizeof (kernbuf));
8203455710d3Srscott 				(void) strlcat(kernbuf, args_buf,
8204eb2bd662Svikram 				    sizeof (kernbuf));
8205455710d3Srscott 			}
8206455710d3Srscott 		}
8207eb2bd662Svikram 		BAM_DPRINTF((D_REBOOT_ABSPATH, fcn, kernbuf));
8208ae115bc7Smrj 	} else {
8209455710d3Srscott 		/*
8210455710d3Srscott 		 * It may be a partial path, or it may be a partial
8211455710d3Srscott 		 * path followed by options.  Assume that only options
8212455710d3Srscott 		 * follow a space.  If someone sends us a kernel path
8213455710d3Srscott 		 * that includes a space, they deserve to be broken.
8214455710d3Srscott 		 */
8215455710d3Srscott 		opt_ptr = strchr(opt, ' ');
8216455710d3Srscott 		if (opt_ptr != NULL) {
8217455710d3Srscott 			*opt_ptr = '\0';
8218455710d3Srscott 		}
8219455710d3Srscott 
8220ae115bc7Smrj 		path = expand_path(opt);
8221ae115bc7Smrj 		if (path != NULL) {
8222eb2bd662Svikram 			(void) strlcpy(kernbuf, path, sizeof (kernbuf));
8223ae115bc7Smrj 			free(path);
8224455710d3Srscott 
8225455710d3Srscott 			/*
8226455710d3Srscott 			 * If there were options given, use those.
8227455710d3Srscott 			 * Otherwise, copy over the default options.
8228455710d3Srscott 			 */
8229455710d3Srscott 			if (opt_ptr != NULL) {
8230455710d3Srscott 				/* Restore the space in opt string */
8231455710d3Srscott 				*opt_ptr = ' ';
8232455710d3Srscott 				(void) strlcat(kernbuf, opt_ptr,
8233eb2bd662Svikram 				    sizeof (kernbuf));
8234455710d3Srscott 			} else {
8235eb2bd662Svikram 				ret = get_kernel(mp, ARGS_CMD, args_buf,
8236eb2bd662Svikram 				    sizeof (args_buf));
8237eb2bd662Svikram 				INJECT_ERROR1("UPDATE_TEMP_PARTIAL_ARGS",
8238eb2bd662Svikram 				    ret = BAM_ERROR);
8239eb2bd662Svikram 				if (ret != BAM_SUCCESS) {
82409bd6d269SLin Ling 					free(fstype);
8241eb2bd662Svikram 					bam_error(REBOOT_GET_ARGS_FAILED);
8242ae115bc7Smrj 					return (BAM_ERROR);
8243eb2bd662Svikram 				}
8244ae115bc7Smrj 
8245ae115bc7Smrj 				if (args_buf[0] != '\0') {
8246ae115bc7Smrj 					(void) strlcat(kernbuf, " ",
8247eb2bd662Svikram 					    sizeof (kernbuf));
8248ae115bc7Smrj 					(void) strlcat(kernbuf,
8249eb2bd662Svikram 					    args_buf, sizeof (kernbuf));
8250ae115bc7Smrj 				}
8251ae115bc7Smrj 			}
8252eb2bd662Svikram 			BAM_DPRINTF((D_REBOOT_RESOLVED_PARTIAL, fcn, kernbuf));
8253455710d3Srscott 		} else {
82549bd6d269SLin Ling 			free(fstype);
8255455710d3Srscott 			bam_error(UNKNOWN_KERNEL, opt);
8256455710d3Srscott 			bam_print_stderr(UNKNOWN_KERNEL_REBOOT);
8257455710d3Srscott 			return (BAM_ERROR);
8258ae115bc7Smrj 		}
8259ae115bc7Smrj 	}
82609bd6d269SLin Ling 	free(fstype);
8261eb2bd662Svikram 	entry = add_boot_entry(mp, REBOOT_TITLE, signbuf, kernbuf,
8262843e1988Sjohnlev 	    NULL, NULL);
8263eb2bd662Svikram 	INJECT_ERROR1("REBOOT_ADD_BOOT_ENTRY", entry = BAM_ERROR);
82647c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
8265eb2bd662Svikram 		bam_error(REBOOT_WITH_ARGS_ADD_ENTRY_FAILED);
82667c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
82677c478bd9Sstevel@tonic-gate 	}
82688c1b6884Sszhou 
8269ae115bc7Smrj 	save_default_entry(mp, BAM_OLDDEF);
8270eb2bd662Svikram 	ret = set_global(mp, menu_cmds[DEFAULT_CMD], entry);
8271eb2bd662Svikram 	INJECT_ERROR1("REBOOT_SET_GLOBAL", ret = BAM_ERROR);
8272eb2bd662Svikram 	if (ret == BAM_ERROR) {
8273eb2bd662Svikram 		bam_error(REBOOT_SET_DEFAULT_FAILED, entry);
8274eb2bd662Svikram 	}
8275eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
82767c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
82777c478bd9Sstevel@tonic-gate }
82787c478bd9Sstevel@tonic-gate 
82797c478bd9Sstevel@tonic-gate static error_t
82807c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
82817c478bd9Sstevel@tonic-gate {
8282eb2bd662Svikram 	line_t		*lp;
8283eb2bd662Svikram 	line_t		*found;
8284eb2bd662Svikram 	line_t		*last;
8285eb2bd662Svikram 	char		*cp;
8286eb2bd662Svikram 	char		*str;
82877c478bd9Sstevel@tonic-gate 	char		prefix[BAM_MAXLINE];
82887c478bd9Sstevel@tonic-gate 	size_t		len;
8289eb2bd662Svikram 	const char	*fcn = "set_global()";
82907c478bd9Sstevel@tonic-gate 
82917c478bd9Sstevel@tonic-gate 	assert(mp);
82927c478bd9Sstevel@tonic-gate 	assert(globalcmd);
82937c478bd9Sstevel@tonic-gate 
8294b610f78eSvikram 	if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) {
8295eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_NEG", val = -1);
8296eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_MENU_EMPTY", mp->end = NULL);
8297eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_VAL_TOO_BIG", val = 100);
8298b610f78eSvikram 		if (val < 0 || mp->end == NULL || val > mp->end->entryNum) {
8299b610f78eSvikram 			(void) snprintf(prefix, sizeof (prefix), "%d", val);
8300b610f78eSvikram 			bam_error(INVALID_ENTRY, prefix);
8301b610f78eSvikram 			return (BAM_ERROR);
8302b610f78eSvikram 		}
8303b610f78eSvikram 	}
8304b610f78eSvikram 
83057c478bd9Sstevel@tonic-gate 	found = last = NULL;
83067c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
83077c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
83087c478bd9Sstevel@tonic-gate 			continue;
83097c478bd9Sstevel@tonic-gate 
83107c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
83117c478bd9Sstevel@tonic-gate 
8312eb2bd662Svikram 		INJECT_ERROR1("SET_GLOBAL_NULL_CMD", lp->cmd = NULL);
83137c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
83147c478bd9Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
83157c478bd9Sstevel@tonic-gate 			continue;
83167c478bd9Sstevel@tonic-gate 		}
83177c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
83187c478bd9Sstevel@tonic-gate 			continue;
83197c478bd9Sstevel@tonic-gate 
8320eb2bd662Svikram 		BAM_DPRINTF((D_FOUND_GLOBAL, fcn, globalcmd));
8321eb2bd662Svikram 
83227c478bd9Sstevel@tonic-gate 		if (found) {
83237c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
83247c478bd9Sstevel@tonic-gate 		}
83257c478bd9Sstevel@tonic-gate 		found = lp;
83267c478bd9Sstevel@tonic-gate 	}
83277c478bd9Sstevel@tonic-gate 
83287c478bd9Sstevel@tonic-gate 	if (found == NULL) {
83297c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
83307c478bd9Sstevel@tonic-gate 		if (last == NULL) {
83317c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
83327c478bd9Sstevel@tonic-gate 			mp->start = lp;
83337c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
83347c478bd9Sstevel@tonic-gate 		} else {
83357c478bd9Sstevel@tonic-gate 			lp->next = last->next;
83367c478bd9Sstevel@tonic-gate 			last->next = lp;
83377c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
83387c478bd9Sstevel@tonic-gate 				mp->end = lp;
83397c478bd9Sstevel@tonic-gate 		}
83407c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
83417c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
83427c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
83437c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
83447c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
83457c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
8346eb2bd662Svikram 		BAM_DPRINTF((D_SET_GLOBAL_WROTE_NEW, fcn, lp->line));
8347eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
83487c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
83497c478bd9Sstevel@tonic-gate 	}
83507c478bd9Sstevel@tonic-gate 
83517c478bd9Sstevel@tonic-gate 	/*
83527c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
83537c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
83547c478bd9Sstevel@tonic-gate 	 * readability.
83557c478bd9Sstevel@tonic-gate 	 */
83567c478bd9Sstevel@tonic-gate 	str = found->line;
83577c478bd9Sstevel@tonic-gate 	cp = prefix;
83587c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
83597c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
83607c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
83617c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
83627c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
83637c478bd9Sstevel@tonic-gate 
83647c478bd9Sstevel@tonic-gate 	free(found->line);
83657c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
83667c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
83677c478bd9Sstevel@tonic-gate 	    "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
83687c478bd9Sstevel@tonic-gate 
8369eb2bd662Svikram 	BAM_DPRINTF((D_SET_GLOBAL_REPLACED, fcn, found->line));
8370eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
83717c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
83727c478bd9Sstevel@tonic-gate }
83737c478bd9Sstevel@tonic-gate 
8374ae115bc7Smrj /*
8375ae115bc7Smrj  * partial_path may be anything like "kernel/unix" or "kmdb".  Try to
8376455710d3Srscott  * expand it to a full unix path.  The calling function is expected to
8377455710d3Srscott  * output a message if an error occurs and NULL is returned.
8378ae115bc7Smrj  */
8379ae115bc7Smrj static char *
8380ae115bc7Smrj expand_path(const char *partial_path)
8381ae115bc7Smrj {
8382ae115bc7Smrj 	int		new_path_len;
8383eb2bd662Svikram 	char		*new_path;
8384eb2bd662Svikram 	char		new_path2[PATH_MAX];
8385ae115bc7Smrj 	struct stat	sb;
8386eb2bd662Svikram 	const char	*fcn = "expand_path()";
8387ae115bc7Smrj 
8388ae115bc7Smrj 	new_path_len = strlen(partial_path) + 64;
8389ae115bc7Smrj 	new_path = s_calloc(1, new_path_len);
8390ae115bc7Smrj 
8391ae115bc7Smrj 	/* First, try the simplest case - something like "kernel/unix" */
8392ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s",
8393ae115bc7Smrj 	    partial_path);
8394ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
8395eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
8396ae115bc7Smrj 		return (new_path);
8397ae115bc7Smrj 	}
8398ae115bc7Smrj 
8399ae115bc7Smrj 	if (strcmp(partial_path, "kmdb") == 0) {
8400ae115bc7Smrj 		(void) snprintf(new_path, new_path_len, "%s -k",
8401ae115bc7Smrj 		    DIRECT_BOOT_KERNEL);
8402eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
8403ae115bc7Smrj 		return (new_path);
8404ae115bc7Smrj 	}
8405ae115bc7Smrj 
8406ae115bc7Smrj 	/*
8407ae115bc7Smrj 	 * We've quickly reached unsupported usage.  Try once more to
8408ae115bc7Smrj 	 * see if we were just given a glom name.
8409ae115bc7Smrj 	 */
8410ae115bc7Smrj 	(void) snprintf(new_path, new_path_len, "/platform/i86pc/%s/unix",
8411ae115bc7Smrj 	    partial_path);
8412ae115bc7Smrj 	(void) snprintf(new_path2, PATH_MAX, "/platform/i86pc/%s/amd64/unix",
8413ae115bc7Smrj 	    partial_path);
8414ae115bc7Smrj 	if (stat(new_path, &sb) == 0) {
8415ae115bc7Smrj 		if (stat(new_path2, &sb) == 0) {
8416ae115bc7Smrj 			/*
8417ae115bc7Smrj 			 * We matched both, so we actually
8418ae115bc7Smrj 			 * want to write the $ISADIR version.
8419ae115bc7Smrj 			 */
8420ae115bc7Smrj 			(void) snprintf(new_path, new_path_len,
8421ae115bc7Smrj 			    "/platform/i86pc/kernel/%s/$ISADIR/unix",
8422ae115bc7Smrj 			    partial_path);
8423ae115bc7Smrj 		}
8424eb2bd662Svikram 		BAM_DPRINTF((D_EXPAND_PATH, fcn, new_path));
8425ae115bc7Smrj 		return (new_path);
8426ae115bc7Smrj 	}
8427ae115bc7Smrj 
8428ae115bc7Smrj 	free(new_path);
8429eb2bd662Svikram 	BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8430ae115bc7Smrj 	return (NULL);
8431ae115bc7Smrj }
8432ae115bc7Smrj 
8433ae115bc7Smrj /*
8434ae115bc7Smrj  * The kernel cmd and arg have been changed, so
8435ae115bc7Smrj  * check whether the archive line needs to change.
8436ae115bc7Smrj  */
8437ae115bc7Smrj static void
8438ae115bc7Smrj set_archive_line(entry_t *entryp, line_t *kernelp)
8439ae115bc7Smrj {
8440ae115bc7Smrj 	line_t		*lp = entryp->start;
8441ae115bc7Smrj 	char		*new_archive;
8442ae115bc7Smrj 	menu_cmd_t	m_cmd;
8443eb2bd662Svikram 	const char	*fcn = "set_archive_line()";
8444ae115bc7Smrj 
8445ae115bc7Smrj 	for (; lp != NULL; lp = lp->next) {
8446ae115bc7Smrj 		if (strncmp(lp->cmd, menu_cmds[MODULE_CMD],
8447ae115bc7Smrj 		    sizeof (menu_cmds[MODULE_CMD]) - 1) == 0) {
8448ae115bc7Smrj 			break;
8449ae115bc7Smrj 		}
8450eb2bd662Svikram 
8451eb2bd662Svikram 		INJECT_ERROR1("SET_ARCHIVE_LINE_END_ENTRY", lp = entryp->end);
8452eb2bd662Svikram 		if (lp == entryp->end) {
8453eb2bd662Svikram 			BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn,
8454eb2bd662Svikram 			    entryp->entryNum));
8455ae115bc7Smrj 			return;
8456ae115bc7Smrj 		}
8457eb2bd662Svikram 	}
8458eb2bd662Svikram 	INJECT_ERROR1("SET_ARCHIVE_LINE_END_MENU", lp = NULL);
8459eb2bd662Svikram 	if (lp == NULL) {
8460eb2bd662Svikram 		BAM_DPRINTF((D_ARCHIVE_LINE_NONE, fcn, entryp->entryNum));
8461ae115bc7Smrj 		return;
8462eb2bd662Svikram 	}
8463ae115bc7Smrj 
8464ae115bc7Smrj 	if (strstr(kernelp->arg, "$ISADIR") != NULL) {
8465ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE;
8466ae115bc7Smrj 		m_cmd = MODULE_DOLLAR_CMD;
8467ae115bc7Smrj 	} else if (strstr(kernelp->arg, "amd64") != NULL) {
8468ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_64;
8469ae115bc7Smrj 		m_cmd = MODULE_CMD;
8470ae115bc7Smrj 	} else {
8471ae115bc7Smrj 		new_archive = DIRECT_BOOT_ARCHIVE_32;
8472ae115bc7Smrj 		m_cmd = MODULE_CMD;
8473ae115bc7Smrj 	}
8474ae115bc7Smrj 
8475eb2bd662Svikram 	if (strcmp(lp->arg, new_archive) == 0) {
8476eb2bd662Svikram 		BAM_DPRINTF((D_ARCHIVE_LINE_NOCHANGE, fcn, lp->arg));
8477ae115bc7Smrj 		return;
8478eb2bd662Svikram 	}
8479ae115bc7Smrj 
8480ae115bc7Smrj 	if (strcmp(lp->cmd, menu_cmds[m_cmd]) != 0) {
8481ae115bc7Smrj 		free(lp->cmd);
8482ae115bc7Smrj 		lp->cmd = s_strdup(menu_cmds[m_cmd]);
8483ae115bc7Smrj 	}
8484ae115bc7Smrj 
8485ae115bc7Smrj 	free(lp->arg);
8486ae115bc7Smrj 	lp->arg = s_strdup(new_archive);
8487ae115bc7Smrj 	update_line(lp);
8488eb2bd662Svikram 	BAM_DPRINTF((D_ARCHIVE_LINE_REPLACED, fcn, lp->line));
8489ae115bc7Smrj }
8490ae115bc7Smrj 
8491ae115bc7Smrj /*
8492ae115bc7Smrj  * Title for an entry to set properties that once went in bootenv.rc.
8493ae115bc7Smrj  */
8494ae115bc7Smrj #define	BOOTENV_RC_TITLE	"Solaris bootenv rc"
8495ae115bc7Smrj 
8496ae115bc7Smrj /*
8497ae115bc7Smrj  * If path is NULL, return the kernel (optnum == KERNEL_CMD) or arguments
8498ae115bc7Smrj  * (optnum == ARGS_CMD) in the argument buf.  If path is a zero-length
8499ae115bc7Smrj  * string, reset the value to the default.  If path is a non-zero-length
8500ae115bc7Smrj  * string, set the kernel or arguments.
8501ae115bc7Smrj  */
8502ae115bc7Smrj static error_t
8503eb2bd662Svikram get_set_kernel(
8504eb2bd662Svikram 	menu_t *mp,
8505eb2bd662Svikram 	menu_cmd_t optnum,
8506eb2bd662Svikram 	char *path,
8507eb2bd662Svikram 	char *buf,
8508eb2bd662Svikram 	size_t bufsize)
8509ae115bc7Smrj {
8510eb2bd662Svikram 	int		entryNum;
8511eb2bd662Svikram 	int		rv = BAM_SUCCESS;
8512eb2bd662Svikram 	int		free_new_path = 0;
8513ae115bc7Smrj 	entry_t		*entryp;
8514eb2bd662Svikram 	line_t		*ptr;
8515eb2bd662Svikram 	line_t		*kernelp;
8516eb2bd662Svikram 	char		*new_arg;
8517eb2bd662Svikram 	char		*old_args;
8518eb2bd662Svikram 	char		*space;
8519eb2bd662Svikram 	char		*new_path;
8520ae115bc7Smrj 	char		old_space;
8521eb2bd662Svikram 	size_t		old_kernel_len;
8522eb2bd662Svikram 	size_t		new_str_len;
8523eb2bd662Svikram 	char		*fstype;
8524eb2bd662Svikram 	char		*osdev;
8525eb2bd662Svikram 	char		*sign;
8526eb2bd662Svikram 	char		signbuf[PATH_MAX];
8527eb2bd662Svikram 	int		ret;
8528eb2bd662Svikram 	const char	*fcn = "get_set_kernel()";
8529ae115bc7Smrj 
8530ae115bc7Smrj 	assert(bufsize > 0);
8531ae115bc7Smrj 
8532ae115bc7Smrj 	ptr = kernelp = NULL;
8533ae115bc7Smrj 	new_arg = old_args = space = NULL;
8534eb2bd662Svikram 	new_path = NULL;
8535ae115bc7Smrj 	buf[0] = '\0';
8536ae115bc7Smrj 
8537eb2bd662Svikram 	INJECT_ERROR1("GET_SET_KERNEL_NOT_DBOOT",
8538eb2bd662Svikram 	    bam_direct = BAM_DIRECT_MULTIBOOT);
8539ae115bc7Smrj 	if (bam_direct != BAM_DIRECT_DBOOT) {
8540ae115bc7Smrj 		bam_error(NOT_DBOOT, optnum == KERNEL_CMD ? "kernel" : "args");
8541ae115bc7Smrj 		return (BAM_ERROR);
8542ae115bc7Smrj 	}
8543ae115bc7Smrj 
8544ae115bc7Smrj 	/*
8545ae115bc7Smrj 	 * If a user changed the default entry to a non-bootadm controlled
8546ae115bc7Smrj 	 * one, we don't want to mess with it.  Just print an error and
8547ae115bc7Smrj 	 * return.
8548ae115bc7Smrj 	 */
8549ae115bc7Smrj 	if (mp->curdefault) {
8550ae115bc7Smrj 		entryNum = s_strtol(mp->curdefault->arg);
8551ae115bc7Smrj 		for (entryp = mp->entries; entryp; entryp = entryp->next) {
8552ae115bc7Smrj 			if (entryp->entryNum == entryNum)
8553ae115bc7Smrj 				break;
8554ae115bc7Smrj 		}
8555ae115bc7Smrj 		if ((entryp != NULL) &&
8556ae115bc7Smrj 		    ((entryp->flags & (BAM_ENTRY_BOOTADM|BAM_ENTRY_LU)) == 0)) {
8557ae115bc7Smrj 			bam_error(DEFAULT_NOT_BAM);
8558ae115bc7Smrj 			return (BAM_ERROR);
8559ae115bc7Smrj 		}
8560ae115bc7Smrj 	}
8561ae115bc7Smrj 
8562eb2bd662Svikram 	entryp = find_boot_entry(mp, BOOTENV_RC_TITLE, NULL, NULL, NULL, NULL,
8563eb2bd662Svikram 	    0, &entryNum);
8564ae115bc7Smrj 
8565ae115bc7Smrj 	if (entryp != NULL) {
8566ae115bc7Smrj 		for (ptr = entryp->start; ptr && ptr != entryp->end;
8567ae115bc7Smrj 		    ptr = ptr->next) {
8568ae115bc7Smrj 			if (strncmp(ptr->cmd, menu_cmds[KERNEL_CMD],
8569ae115bc7Smrj 			    sizeof (menu_cmds[KERNEL_CMD]) - 1) == 0) {
8570ae115bc7Smrj 				kernelp = ptr;
8571ae115bc7Smrj 				break;
8572ae115bc7Smrj 			}
8573ae115bc7Smrj 		}
8574ae115bc7Smrj 		if (kernelp == NULL) {
8575ae115bc7Smrj 			bam_error(NO_KERNEL, entryNum);
8576ae115bc7Smrj 			return (BAM_ERROR);
8577ae115bc7Smrj 		}
8578ae115bc7Smrj 
8579ae115bc7Smrj 		old_kernel_len = strcspn(kernelp->arg, " \t");
8580ae115bc7Smrj 		space = old_args = kernelp->arg + old_kernel_len;
8581ae115bc7Smrj 		while ((*old_args == ' ') || (*old_args == '\t'))
8582ae115bc7Smrj 			old_args++;
8583ae115bc7Smrj 	}
8584ae115bc7Smrj 
8585ae115bc7Smrj 	if (path == NULL) {
8586eb2bd662Svikram 		if (entryp == NULL) {
8587eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_NO_RC, fcn));
8588eb2bd662Svikram 			BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8589ae115bc7Smrj 			return (BAM_SUCCESS);
8590eb2bd662Svikram 		}
8591eb2bd662Svikram 		assert(kernelp);
8592ae115bc7Smrj 		if (optnum == ARGS_CMD) {
8593eb2bd662Svikram 			if (old_args[0] != '\0') {
8594ae115bc7Smrj 				(void) strlcpy(buf, old_args, bufsize);
8595eb2bd662Svikram 				BAM_DPRINTF((D_GET_SET_KERNEL_ARGS, fcn, buf));
8596eb2bd662Svikram 			}
8597ae115bc7Smrj 		} else {
8598ae115bc7Smrj 			/*
8599ae115bc7Smrj 			 * We need to print the kernel, so we just turn the
8600ae115bc7Smrj 			 * first space into a '\0' and print the beginning.
8601ae115bc7Smrj 			 * We don't print anything if it's the default kernel.
8602ae115bc7Smrj 			 */
8603ae115bc7Smrj 			old_space = *space;
8604ae115bc7Smrj 			*space = '\0';
8605eb2bd662Svikram 			if (strcmp(kernelp->arg, DIRECT_BOOT_KERNEL) != 0) {
8606ae115bc7Smrj 				(void) strlcpy(buf, kernelp->arg, bufsize);
8607eb2bd662Svikram 				BAM_DPRINTF((D_GET_SET_KERNEL_KERN, fcn, buf));
8608eb2bd662Svikram 			}
8609ae115bc7Smrj 			*space = old_space;
8610ae115bc7Smrj 		}
8611eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8612ae115bc7Smrj 		return (BAM_SUCCESS);
8613ae115bc7Smrj 	}
8614ae115bc7Smrj 
8615ae115bc7Smrj 	/*
8616ae115bc7Smrj 	 * First, check if we're resetting an entry to the default.
8617ae115bc7Smrj 	 */
8618ae115bc7Smrj 	if ((path[0] == '\0') ||
8619ae115bc7Smrj 	    ((optnum == KERNEL_CMD) &&
8620ae115bc7Smrj 	    (strcmp(path, DIRECT_BOOT_KERNEL) == 0))) {
8621ae115bc7Smrj 		if ((entryp == NULL) || (kernelp == NULL)) {
8622ae115bc7Smrj 			/* No previous entry, it's already the default */
8623eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_ALREADY, fcn));
8624ae115bc7Smrj 			return (BAM_SUCCESS);
8625ae115bc7Smrj 		}
8626ae115bc7Smrj 
8627ae115bc7Smrj 		/*
8628ae115bc7Smrj 		 * Check if we can delete the entry.  If we're resetting the
8629ae115bc7Smrj 		 * kernel command, and the args is already empty, or if we're
8630ae115bc7Smrj 		 * resetting the args command, and the kernel is already the
8631ae115bc7Smrj 		 * default, we can restore the old default and delete the entry.
8632ae115bc7Smrj 		 */
8633ae115bc7Smrj 		if (((optnum == KERNEL_CMD) &&
8634ae115bc7Smrj 		    ((old_args == NULL) || (old_args[0] == '\0'))) ||
8635ae115bc7Smrj 		    ((optnum == ARGS_CMD) &&
8636ae115bc7Smrj 		    (strncmp(kernelp->arg, DIRECT_BOOT_KERNEL,
8637ae115bc7Smrj 		    sizeof (DIRECT_BOOT_KERNEL) - 1) == 0))) {
8638ae115bc7Smrj 			kernelp = NULL;
8639ae115bc7Smrj 			(void) do_delete(mp, entryNum);
8640ae115bc7Smrj 			restore_default_entry(mp, BAM_OLD_RC_DEF,
8641ae115bc7Smrj 			    mp->old_rc_default);
8642ae115bc7Smrj 			mp->old_rc_default = NULL;
8643ae115bc7Smrj 			rv = BAM_WRITE;
8644eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESTORE_DEFAULT, fcn));
8645ae115bc7Smrj 			goto done;
8646ae115bc7Smrj 		}
8647ae115bc7Smrj 
8648ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
8649ae115bc7Smrj 			/*
8650ae115bc7Smrj 			 * At this point, we've already checked that old_args
8651ae115bc7Smrj 			 * and entryp are valid pointers.  The "+ 2" is for
8652ae115bc7Smrj 			 * a space a the string termination character.
8653ae115bc7Smrj 			 */
8654ae115bc7Smrj 			new_str_len = (sizeof (DIRECT_BOOT_KERNEL) - 1) +
8655ae115bc7Smrj 			    strlen(old_args) + 2;
8656ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
8657ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
8658ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, old_args);
8659ae115bc7Smrj 			free(kernelp->arg);
8660ae115bc7Smrj 			kernelp->arg = new_arg;
8661ae115bc7Smrj 
8662ae115bc7Smrj 			/*
8663ae115bc7Smrj 			 * We have changed the kernel line, so we may need
8664ae115bc7Smrj 			 * to update the archive line as well.
8665ae115bc7Smrj 			 */
8666ae115bc7Smrj 			set_archive_line(entryp, kernelp);
8667eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESET_KERNEL_SET_ARG,
8668eb2bd662Svikram 			    fcn, kernelp->arg));
8669ae115bc7Smrj 		} else {
8670ae115bc7Smrj 			/*
8671ae115bc7Smrj 			 * We're resetting the boot args to nothing, so
8672ae115bc7Smrj 			 * we only need to copy the kernel.  We've already
8673ae115bc7Smrj 			 * checked that the kernel is not the default.
8674ae115bc7Smrj 			 */
8675ae115bc7Smrj 			new_arg = s_calloc(1, old_kernel_len + 1);
8676ae115bc7Smrj 			(void) snprintf(new_arg, old_kernel_len + 1, "%s",
8677ae115bc7Smrj 			    kernelp->arg);
8678ae115bc7Smrj 			free(kernelp->arg);
8679ae115bc7Smrj 			kernelp->arg = new_arg;
8680eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_RESET_ARG_SET_KERNEL,
8681eb2bd662Svikram 			    fcn, kernelp->arg));
8682ae115bc7Smrj 		}
8683ae115bc7Smrj 		rv = BAM_WRITE;
8684ae115bc7Smrj 		goto done;
8685ae115bc7Smrj 	}
8686ae115bc7Smrj 
8687ae115bc7Smrj 	/*
8688ae115bc7Smrj 	 * Expand the kernel file to a full path, if necessary
8689ae115bc7Smrj 	 */
8690ae115bc7Smrj 	if ((optnum == KERNEL_CMD) && (path[0] != '/')) {
8691ae115bc7Smrj 		new_path = expand_path(path);
8692ae115bc7Smrj 		if (new_path == NULL) {
8693455710d3Srscott 			bam_error(UNKNOWN_KERNEL, path);
8694eb2bd662Svikram 			BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8695ae115bc7Smrj 			return (BAM_ERROR);
8696ae115bc7Smrj 		}
8697ae115bc7Smrj 		free_new_path = 1;
8698ae115bc7Smrj 	} else {
8699ae115bc7Smrj 		new_path = path;
8700ae115bc7Smrj 		free_new_path = 0;
8701ae115bc7Smrj 	}
8702ae115bc7Smrj 
8703ae115bc7Smrj 	/*
8704ae115bc7Smrj 	 * At this point, we know we're setting a new value.  First, take care
8705ae115bc7Smrj 	 * of the case where there was no previous entry.
8706ae115bc7Smrj 	 */
8707ae115bc7Smrj 	if (entryp == NULL) {
8708eb2bd662Svikram 
8709ae115bc7Smrj 		/* Similar to code in update_temp */
8710eb2bd662Svikram 		fstype = get_fstype("/");
8711eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_FSTYPE", fstype = NULL);
8712eb2bd662Svikram 		if (fstype == NULL) {
8713eb2bd662Svikram 			bam_error(BOOTENV_FSTYPE_FAILED);
8714ae115bc7Smrj 			rv = BAM_ERROR;
8715ae115bc7Smrj 			goto done;
8716ae115bc7Smrj 		}
8717eb2bd662Svikram 
8718eb2bd662Svikram 		osdev = get_special("/");
8719eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SPECIAL", osdev = NULL);
8720eb2bd662Svikram 		if (osdev == NULL) {
8721eb2bd662Svikram 			free(fstype);
8722eb2bd662Svikram 			bam_error(BOOTENV_SPECIAL_FAILED);
8723eb2bd662Svikram 			rv = BAM_ERROR;
8724eb2bd662Svikram 			goto done;
8725eb2bd662Svikram 		}
8726eb2bd662Svikram 
8727eb2bd662Svikram 		sign = find_existing_sign("/", osdev, fstype);
8728eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SIGN", sign = NULL);
8729eb2bd662Svikram 		if (sign == NULL) {
8730eb2bd662Svikram 			free(fstype);
8731eb2bd662Svikram 			free(osdev);
8732eb2bd662Svikram 			bam_error(BOOTENV_SIGN_FAILED);
8733eb2bd662Svikram 			rv = BAM_ERROR;
8734eb2bd662Svikram 			goto done;
8735eb2bd662Svikram 		}
8736eb2bd662Svikram 
8737eb2bd662Svikram 		free(fstype);
8738eb2bd662Svikram 		free(osdev);
8739eb2bd662Svikram 		(void) strlcpy(signbuf, sign, sizeof (signbuf));
8740eb2bd662Svikram 		free(sign);
8741eb2bd662Svikram 		assert(strchr(signbuf, '(') == NULL &&
8742eb2bd662Svikram 		    strchr(signbuf, ',') == NULL &&
8743eb2bd662Svikram 		    strchr(signbuf, ')') == NULL);
8744eb2bd662Svikram 
8745ae115bc7Smrj 		if (optnum == KERNEL_CMD) {
8746eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_NEW_KERN, fcn, new_path));
8747ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
8748eb2bd662Svikram 			    signbuf, new_path, NULL, NULL);
8749ae115bc7Smrj 		} else {
8750ae115bc7Smrj 			new_str_len = strlen(DIRECT_BOOT_KERNEL) +
8751ae115bc7Smrj 			    strlen(path) + 8;
8752ae115bc7Smrj 			new_arg = s_calloc(1, new_str_len);
8753ae115bc7Smrj 
8754ae115bc7Smrj 			(void) snprintf(new_arg, new_str_len, "%s %s",
8755ae115bc7Smrj 			    DIRECT_BOOT_KERNEL, path);
8756eb2bd662Svikram 			BAM_DPRINTF((D_GET_SET_KERNEL_NEW_ARG, fcn, new_arg));
8757ae115bc7Smrj 			entryNum = add_boot_entry(mp, BOOTENV_RC_TITLE,
8758eb2bd662Svikram 			    signbuf, new_arg, NULL, DIRECT_BOOT_ARCHIVE);
8759eb2bd662Svikram 			free(new_arg);
8760eb2bd662Svikram 		}
8761eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_ADD_BOOT_ENTRY",
8762eb2bd662Svikram 		    entryNum = BAM_ERROR);
8763eb2bd662Svikram 		if (entryNum == BAM_ERROR) {
8764eb2bd662Svikram 			bam_error(GET_SET_KERNEL_ADD_BOOT_ENTRY,
8765eb2bd662Svikram 			    BOOTENV_RC_TITLE);
8766eb2bd662Svikram 			rv = BAM_ERROR;
8767eb2bd662Svikram 			goto done;
8768ae115bc7Smrj 		}
8769ae115bc7Smrj 		save_default_entry(mp, BAM_OLD_RC_DEF);
8770eb2bd662Svikram 		ret = set_global(mp, menu_cmds[DEFAULT_CMD], entryNum);
8771eb2bd662Svikram 		INJECT_ERROR1("GET_SET_KERNEL_SET_GLOBAL", ret = BAM_ERROR);
8772eb2bd662Svikram 		if (ret == BAM_ERROR) {
8773eb2bd662Svikram 			bam_error(GET_SET_KERNEL_SET_GLOBAL, entryNum);
8774eb2bd662Svikram 		}
8775ae115bc7Smrj 		rv = BAM_WRITE;
8776ae115bc7Smrj 		goto done;
8777ae115bc7Smrj 	}
8778ae115bc7Smrj 
8779ae115bc7Smrj 	/*
8780ae115bc7Smrj 	 * There was already an bootenv entry which we need to edit.
8781ae115bc7Smrj 	 */
8782ae115bc7Smrj 	if (optnum == KERNEL_CMD) {
8783ae115bc7Smrj 		new_str_len = strlen(new_path) + strlen(old_args) + 2;
8784ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
8785ae115bc7Smrj 		(void) snprintf(new_arg, new_str_len, "%s %s", new_path,
8786ae115bc7Smrj 		    old_args);
8787ae115bc7Smrj 		free(kernelp->arg);
8788ae115bc7Smrj 		kernelp->arg = new_arg;
8789ae115bc7Smrj 
8790ae115bc7Smrj 		/*
8791ae115bc7Smrj 		 * If we have changed the kernel line, we may need to update
8792ae115bc7Smrj 		 * the archive line as well.
8793ae115bc7Smrj 		 */
8794ae115bc7Smrj 		set_archive_line(entryp, kernelp);
8795eb2bd662Svikram 		BAM_DPRINTF((D_GET_SET_KERNEL_REPLACED_KERNEL_SAME_ARG, fcn,
8796eb2bd662Svikram 		    kernelp->arg));
8797ae115bc7Smrj 	} else {
8798ae115bc7Smrj 		new_str_len = old_kernel_len + strlen(path) + 8;
8799ae115bc7Smrj 		new_arg = s_calloc(1, new_str_len);
8800ae115bc7Smrj 		(void) strncpy(new_arg, kernelp->arg, old_kernel_len);
8801ae115bc7Smrj 		(void) strlcat(new_arg, " ", new_str_len);
8802ae115bc7Smrj 		(void) strlcat(new_arg, path, new_str_len);
8803ae115bc7Smrj 		free(kernelp->arg);
8804ae115bc7Smrj 		kernelp->arg = new_arg;
8805eb2bd662Svikram 		BAM_DPRINTF((D_GET_SET_KERNEL_SAME_KERNEL_REPLACED_ARG, fcn,
8806eb2bd662Svikram 		    kernelp->arg));
8807ae115bc7Smrj 	}
8808ae115bc7Smrj 	rv = BAM_WRITE;
8809ae115bc7Smrj 
8810ae115bc7Smrj done:
8811ae115bc7Smrj 	if ((rv == BAM_WRITE) && kernelp)
8812ae115bc7Smrj 		update_line(kernelp);
8813ae115bc7Smrj 	if (free_new_path)
8814ae115bc7Smrj 		free(new_path);
8815eb2bd662Svikram 	if (rv == BAM_WRITE) {
8816eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8817eb2bd662Svikram 	} else {
8818eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8819eb2bd662Svikram 	}
8820ae115bc7Smrj 	return (rv);
8821ae115bc7Smrj }
8822ae115bc7Smrj 
8823eb2bd662Svikram static error_t
8824eb2bd662Svikram get_kernel(menu_t *mp, menu_cmd_t optnum, char *buf, size_t bufsize)
8825eb2bd662Svikram {
8826eb2bd662Svikram 	const char	*fcn = "get_kernel()";
8827eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, menu_cmds[optnum]));
8828eb2bd662Svikram 	return (get_set_kernel(mp, optnum, NULL, buf, bufsize));
8829eb2bd662Svikram }
8830eb2bd662Svikram 
8831eb2bd662Svikram static error_t
8832eb2bd662Svikram set_kernel(menu_t *mp, menu_cmd_t optnum, char *path, char *buf, size_t bufsize)
8833eb2bd662Svikram {
8834eb2bd662Svikram 	const char	*fcn = "set_kernel()";
8835eb2bd662Svikram 	assert(path != NULL);
8836eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY2, fcn, menu_cmds[optnum], path));
8837eb2bd662Svikram 	return (get_set_kernel(mp, optnum, path, buf, bufsize));
8838eb2bd662Svikram }
8839eb2bd662Svikram 
88407c478bd9Sstevel@tonic-gate /*ARGSUSED*/
88417c478bd9Sstevel@tonic-gate static error_t
8842eb2bd662Svikram set_option(menu_t *mp, char *dummy, char *opt)
88437c478bd9Sstevel@tonic-gate {
8844eb2bd662Svikram 	int		optnum;
8845eb2bd662Svikram 	int		optval;
88467c478bd9Sstevel@tonic-gate 	char		*val;
8847ae115bc7Smrj 	char		buf[BUFSIZ] = "";
8848ae115bc7Smrj 	error_t		rv;
8849eb2bd662Svikram 	const char	*fcn = "set_option()";
88507c478bd9Sstevel@tonic-gate 
88517c478bd9Sstevel@tonic-gate 	assert(mp);
88527c478bd9Sstevel@tonic-gate 	assert(opt);
8853eb2bd662Svikram 	assert(dummy == NULL);
8854eb2bd662Svikram 
8855eb2bd662Svikram 	/* opt is set from bam_argv[0] and is always non-NULL */
8856eb2bd662Svikram 	BAM_DPRINTF((D_FUNC_ENTRY1, fcn, opt));
88577c478bd9Sstevel@tonic-gate 
88587c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
8859ae115bc7Smrj 	if (val != NULL) {
8860ae115bc7Smrj 		*val = '\0';
88617c478bd9Sstevel@tonic-gate 	}
88627c478bd9Sstevel@tonic-gate 
88637c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
88647c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
88657c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
88667c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
8867ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[KERNEL_CMD]) == 0) {
8868ae115bc7Smrj 		optnum = KERNEL_CMD;
8869ae115bc7Smrj 	} else if (strcmp(opt, menu_cmds[ARGS_CMD]) == 0) {
8870ae115bc7Smrj 		optnum = ARGS_CMD;
88717c478bd9Sstevel@tonic-gate 	} else {
8872eb2bd662Svikram 		bam_error(INVALID_OPTION, opt);
88737c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
88747c478bd9Sstevel@tonic-gate 	}
88757c478bd9Sstevel@tonic-gate 
8876ae115bc7Smrj 	/*
8877ae115bc7Smrj 	 * kernel and args are allowed without "=new_value" strings.  All
8878ae115bc7Smrj 	 * others cause errors
8879ae115bc7Smrj 	 */
8880ae115bc7Smrj 	if ((val == NULL) && (optnum != KERNEL_CMD) && (optnum != ARGS_CMD)) {
8881eb2bd662Svikram 		bam_error(NO_OPTION_ARG, opt);
8882ae115bc7Smrj 		return (BAM_ERROR);
8883ae115bc7Smrj 	} else if (val != NULL) {
88847c478bd9Sstevel@tonic-gate 		*val = '=';
8885ae115bc7Smrj 	}
8886ae115bc7Smrj 
8887ae115bc7Smrj 	if ((optnum == KERNEL_CMD) || (optnum == ARGS_CMD)) {
8888eb2bd662Svikram 		BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum],
8889eb2bd662Svikram 		    val ? val + 1 : "NULL"));
8890eb2bd662Svikram 
8891eb2bd662Svikram 		if (val)
8892eb2bd662Svikram 			rv = set_kernel(mp, optnum, val + 1, buf, sizeof (buf));
8893eb2bd662Svikram 		else
8894eb2bd662Svikram 			rv = get_kernel(mp, optnum, buf, sizeof (buf));
8895ae115bc7Smrj 		if ((rv == BAM_SUCCESS) && (buf[0] != '\0'))
8896ae115bc7Smrj 			(void) printf("%s\n", buf);
8897ae115bc7Smrj 	} else {
8898ae115bc7Smrj 		optval = s_strtol(val + 1);
8899eb2bd662Svikram 		BAM_DPRINTF((D_SET_OPTION, fcn, menu_cmds[optnum], val + 1));
8900eb2bd662Svikram 		rv = set_global(mp, menu_cmds[optnum], optval);
89017c478bd9Sstevel@tonic-gate 	}
8902eb2bd662Svikram 
8903eb2bd662Svikram 	if (rv == BAM_WRITE || rv == BAM_SUCCESS) {
8904eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
8905eb2bd662Svikram 	} else {
8906eb2bd662Svikram 		BAM_DPRINTF((D_RETURN_FAILURE, fcn));
8907eb2bd662Svikram 	}
8908eb2bd662Svikram 
8909eb2bd662Svikram 	return (rv);
8910ae115bc7Smrj }
89117c478bd9Sstevel@tonic-gate 
89127c478bd9Sstevel@tonic-gate /*
89137c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
89147c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
89157c478bd9Sstevel@tonic-gate  */
89167c478bd9Sstevel@tonic-gate static error_t
89177c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
89187c478bd9Sstevel@tonic-gate {
89197c478bd9Sstevel@tonic-gate 	line_t *lp;
89207c478bd9Sstevel@tonic-gate 	char *arg;
89217c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
89227c478bd9Sstevel@tonic-gate 
89237c478bd9Sstevel@tonic-gate 	assert(mp);
89247c478bd9Sstevel@tonic-gate 	assert(menu_path);
89257c478bd9Sstevel@tonic-gate 	assert(globalcmd);
89267c478bd9Sstevel@tonic-gate 
89277c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
89287c478bd9Sstevel@tonic-gate 		if (!quiet)
89297c478bd9Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
89307c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
89317c478bd9Sstevel@tonic-gate 	}
89327c478bd9Sstevel@tonic-gate 
89337c478bd9Sstevel@tonic-gate 	done = 0;
89347c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
89357c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
89367c478bd9Sstevel@tonic-gate 			continue;
89377c478bd9Sstevel@tonic-gate 
89387c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
89397c478bd9Sstevel@tonic-gate 			if (!quiet)
89407c478bd9Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
89417c478bd9Sstevel@tonic-gate 			continue;
89427c478bd9Sstevel@tonic-gate 		}
89437c478bd9Sstevel@tonic-gate 
89447c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
89457c478bd9Sstevel@tonic-gate 			continue;
89467c478bd9Sstevel@tonic-gate 
89477c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
89487c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
89497c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
89507c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
89517c478bd9Sstevel@tonic-gate 		}
89527c478bd9Sstevel@tonic-gate 
89537c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
89547c478bd9Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
89557c478bd9Sstevel@tonic-gate 		done = 1;
89567c478bd9Sstevel@tonic-gate 	}
89577c478bd9Sstevel@tonic-gate 
89587c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
89597c478bd9Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
89607c478bd9Sstevel@tonic-gate 
89617c478bd9Sstevel@tonic-gate 	return (ret);
89627c478bd9Sstevel@tonic-gate }
89637c478bd9Sstevel@tonic-gate 
89647c478bd9Sstevel@tonic-gate static error_t
89657c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
89667c478bd9Sstevel@tonic-gate {
8967eb2bd662Svikram 	const char *fcn = "menu_write()";
8968eb2bd662Svikram 
8969eb2bd662Svikram 	BAM_DPRINTF((D_MENU_WRITE_ENTER, fcn, root));
89707c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
89717c478bd9Sstevel@tonic-gate }
89727c478bd9Sstevel@tonic-gate 
8973eb2bd662Svikram void
89747c478bd9Sstevel@tonic-gate line_free(line_t *lp)
89757c478bd9Sstevel@tonic-gate {
89767c478bd9Sstevel@tonic-gate 	if (lp == NULL)
89777c478bd9Sstevel@tonic-gate 		return;
89787c478bd9Sstevel@tonic-gate 
89797c478bd9Sstevel@tonic-gate 	if (lp->cmd)
89807c478bd9Sstevel@tonic-gate 		free(lp->cmd);
89817c478bd9Sstevel@tonic-gate 	if (lp->sep)
89827c478bd9Sstevel@tonic-gate 		free(lp->sep);
89837c478bd9Sstevel@tonic-gate 	if (lp->arg)
89847c478bd9Sstevel@tonic-gate 		free(lp->arg);
89857c478bd9Sstevel@tonic-gate 	if (lp->line)
89867c478bd9Sstevel@tonic-gate 		free(lp->line);
89877c478bd9Sstevel@tonic-gate 	free(lp);
89887c478bd9Sstevel@tonic-gate }
89897c478bd9Sstevel@tonic-gate 
89907c478bd9Sstevel@tonic-gate static void
89917c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
89927c478bd9Sstevel@tonic-gate {
89937c478bd9Sstevel@tonic-gate 	line_t *lp;
89947c478bd9Sstevel@tonic-gate 
89957c478bd9Sstevel@tonic-gate 	while (start) {
89967c478bd9Sstevel@tonic-gate 		lp = start;
89977c478bd9Sstevel@tonic-gate 		start = start->next;
89987c478bd9Sstevel@tonic-gate 		line_free(lp);
89997c478bd9Sstevel@tonic-gate 	}
90007c478bd9Sstevel@tonic-gate }
90017c478bd9Sstevel@tonic-gate 
90027c478bd9Sstevel@tonic-gate static void
90037c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
90047c478bd9Sstevel@tonic-gate {
90057c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
90067c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
90077c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
90087c478bd9Sstevel@tonic-gate }
90097c478bd9Sstevel@tonic-gate 
90107c478bd9Sstevel@tonic-gate static void
90117c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
90127c478bd9Sstevel@tonic-gate {
90138c1b6884Sszhou 	entry_t *ent, *tmp;
90147c478bd9Sstevel@tonic-gate 	assert(mp);
90157c478bd9Sstevel@tonic-gate 
90167c478bd9Sstevel@tonic-gate 	if (mp->start)
90177c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
90188c1b6884Sszhou 	ent = mp->entries;
90198c1b6884Sszhou 	while (ent) {
90208c1b6884Sszhou 		tmp = ent;
90218c1b6884Sszhou 		ent = tmp->next;
90228c1b6884Sszhou 		free(tmp);
90238c1b6884Sszhou 	}
90247c478bd9Sstevel@tonic-gate 
90258c1b6884Sszhou 	free(mp);
90267c478bd9Sstevel@tonic-gate }
90277c478bd9Sstevel@tonic-gate 
90287c478bd9Sstevel@tonic-gate /*
90297c478bd9Sstevel@tonic-gate  * Utility routines
90307c478bd9Sstevel@tonic-gate  */
90317c478bd9Sstevel@tonic-gate 
90327c478bd9Sstevel@tonic-gate 
90337c478bd9Sstevel@tonic-gate /*
90347c478bd9Sstevel@tonic-gate  * Returns 0 on success
90357c478bd9Sstevel@tonic-gate  * Any other value indicates an error
90367c478bd9Sstevel@tonic-gate  */
90377c478bd9Sstevel@tonic-gate static int
9038986fd29aSsetje exec_cmd(char *cmdline, filelist_t *flistp)
90397c478bd9Sstevel@tonic-gate {
90407c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
90417c478bd9Sstevel@tonic-gate 	int ret;
90427c478bd9Sstevel@tonic-gate 	FILE *ptr;
90437c478bd9Sstevel@tonic-gate 	sigset_t set;
90447c478bd9Sstevel@tonic-gate 	void (*disp)(int);
90457c478bd9Sstevel@tonic-gate 
90467c478bd9Sstevel@tonic-gate 	/*
90477c478bd9Sstevel@tonic-gate 	 * For security
90487c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
90497c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
90507c478bd9Sstevel@tonic-gate 	 */
90517c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
90527c478bd9Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
90537c478bd9Sstevel@tonic-gate 		return (-1);
90547c478bd9Sstevel@tonic-gate 	}
90557c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
90567c478bd9Sstevel@tonic-gate 
90577c478bd9Sstevel@tonic-gate 	/*
90587c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
90597c478bd9Sstevel@tonic-gate 	 * unblock it here
90607c478bd9Sstevel@tonic-gate 	 */
90617c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
90627c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
90637c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
90647c478bd9Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
90657c478bd9Sstevel@tonic-gate 		return (-1);
90667c478bd9Sstevel@tonic-gate 	}
90677c478bd9Sstevel@tonic-gate 
90687c478bd9Sstevel@tonic-gate 	/*
90697c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
90707c478bd9Sstevel@tonic-gate 	 */
90717c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
90727c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
90737c478bd9Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
90747c478bd9Sstevel@tonic-gate 		return (-1);
90757c478bd9Sstevel@tonic-gate 	}
90767c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
90777c478bd9Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
90787c478bd9Sstevel@tonic-gate 		return (-1);
90797c478bd9Sstevel@tonic-gate 	}
90807c478bd9Sstevel@tonic-gate 
90817c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
90827c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
90837c478bd9Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
90847c478bd9Sstevel@tonic-gate 		return (-1);
90857c478bd9Sstevel@tonic-gate 	}
90867c478bd9Sstevel@tonic-gate 
90877c478bd9Sstevel@tonic-gate 	/*
90887c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
90897c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
90907c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
90917c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
90927c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
90937c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
90947c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
90957c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
90967c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
90977c478bd9Sstevel@tonic-gate 	 * pclose().
90987c478bd9Sstevel@tonic-gate 	 *
90997c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
91007c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
91017c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
91027c478bd9Sstevel@tonic-gate 	 */
9103986fd29aSsetje 	while (s_fgets(buf, sizeof (buf), ptr) != NULL) {
9104986fd29aSsetje 		if (flistp == NULL) {
9105986fd29aSsetje 			/* s_fgets strips newlines, so insert them at the end */
9106986fd29aSsetje 			bam_print(PRINT, buf);
9107986fd29aSsetje 		} else {
9108986fd29aSsetje 			append_to_flist(flistp, buf);
91097c478bd9Sstevel@tonic-gate 		}
91107c478bd9Sstevel@tonic-gate 	}
91117c478bd9Sstevel@tonic-gate 
91127c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
91137c478bd9Sstevel@tonic-gate 	if (ret == -1) {
91147c478bd9Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
91157c478bd9Sstevel@tonic-gate 		return (-1);
91167c478bd9Sstevel@tonic-gate 	}
91177c478bd9Sstevel@tonic-gate 
91187c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
91197c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
91207c478bd9Sstevel@tonic-gate 	} else {
91217c478bd9Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
91227c478bd9Sstevel@tonic-gate 		return (-1);
91237c478bd9Sstevel@tonic-gate 	}
91247c478bd9Sstevel@tonic-gate }
91257c478bd9Sstevel@tonic-gate 
91267c478bd9Sstevel@tonic-gate /*
91277c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
91287c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
91297c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
91307c478bd9Sstevel@tonic-gate  */
91317c478bd9Sstevel@tonic-gate static long
91327c478bd9Sstevel@tonic-gate s_strtol(char *str)
91337c478bd9Sstevel@tonic-gate {
91347c478bd9Sstevel@tonic-gate 	long l;
91357c478bd9Sstevel@tonic-gate 	char *res = NULL;
91367c478bd9Sstevel@tonic-gate 
91377c478bd9Sstevel@tonic-gate 	if (str == NULL) {
91387c478bd9Sstevel@tonic-gate 		return (-1);
91397c478bd9Sstevel@tonic-gate 	}
91407c478bd9Sstevel@tonic-gate 
91417c478bd9Sstevel@tonic-gate 	errno = 0;
91427c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
91437c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
91447c478bd9Sstevel@tonic-gate 		return (-1);
91457c478bd9Sstevel@tonic-gate 	}
91467c478bd9Sstevel@tonic-gate 
91477c478bd9Sstevel@tonic-gate 	return (l);
91487c478bd9Sstevel@tonic-gate }
91497c478bd9Sstevel@tonic-gate 
91507c478bd9Sstevel@tonic-gate /*
91517c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
91527c478bd9Sstevel@tonic-gate  */
91537c478bd9Sstevel@tonic-gate static int
91547c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
91557c478bd9Sstevel@tonic-gate {
91567c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
91577c478bd9Sstevel@tonic-gate 
91587c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
91597c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
91607c478bd9Sstevel@tonic-gate }
91617c478bd9Sstevel@tonic-gate 
91627c478bd9Sstevel@tonic-gate /*
91637c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
91647c478bd9Sstevel@tonic-gate  */
9165ae115bc7Smrj char *
91667c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
91677c478bd9Sstevel@tonic-gate {
91687c478bd9Sstevel@tonic-gate 	int n;
91697c478bd9Sstevel@tonic-gate 
91707c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
91717c478bd9Sstevel@tonic-gate 	if (buf) {
91727c478bd9Sstevel@tonic-gate 		n = strlen(buf);
91737c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
91747c478bd9Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
91757c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
91767c478bd9Sstevel@tonic-gate 	}
91777c478bd9Sstevel@tonic-gate 
91787c478bd9Sstevel@tonic-gate 	return (buf);
91797c478bd9Sstevel@tonic-gate }
91807c478bd9Sstevel@tonic-gate 
9181ae115bc7Smrj void *
91827c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
91837c478bd9Sstevel@tonic-gate {
91847c478bd9Sstevel@tonic-gate 	void *ptr;
91857c478bd9Sstevel@tonic-gate 
91867c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
91877c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
91887c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
91897c478bd9Sstevel@tonic-gate 		bam_exit(1);
91907c478bd9Sstevel@tonic-gate 	}
91917c478bd9Sstevel@tonic-gate 	return (ptr);
91927c478bd9Sstevel@tonic-gate }
91937c478bd9Sstevel@tonic-gate 
9194ae115bc7Smrj void *
9195ae115bc7Smrj s_realloc(void *ptr, size_t sz)
9196ae115bc7Smrj {
9197ae115bc7Smrj 	ptr = realloc(ptr, sz);
9198ae115bc7Smrj 	if (ptr == NULL) {
9199ae115bc7Smrj 		bam_error(NO_MEM, sz);
9200ae115bc7Smrj 		bam_exit(1);
9201ae115bc7Smrj 	}
9202ae115bc7Smrj 	return (ptr);
9203ae115bc7Smrj }
9204ae115bc7Smrj 
9205eb2bd662Svikram char *
92067c478bd9Sstevel@tonic-gate s_strdup(char *str)
92077c478bd9Sstevel@tonic-gate {
92087c478bd9Sstevel@tonic-gate 	char *ptr;
92097c478bd9Sstevel@tonic-gate 
92107c478bd9Sstevel@tonic-gate 	if (str == NULL)
92117c478bd9Sstevel@tonic-gate 		return (NULL);
92127c478bd9Sstevel@tonic-gate 
92137c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
92147c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
92157c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
92167c478bd9Sstevel@tonic-gate 		bam_exit(1);
92177c478bd9Sstevel@tonic-gate 	}
92187c478bd9Sstevel@tonic-gate 	return (ptr);
92197c478bd9Sstevel@tonic-gate }
92207c478bd9Sstevel@tonic-gate 
92217c478bd9Sstevel@tonic-gate /*
92227c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
92237c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
92247c478bd9Sstevel@tonic-gate  */
92257c478bd9Sstevel@tonic-gate static int
92267c478bd9Sstevel@tonic-gate is_amd64(void)
92277c478bd9Sstevel@tonic-gate {
92287c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
92297c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
92307c478bd9Sstevel@tonic-gate 
92317c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
92327c478bd9Sstevel@tonic-gate 		return (amd64);
92337c478bd9Sstevel@tonic-gate 
9234d876c67dSjg 	if (bam_alt_platform) {
9235d876c67dSjg 		if (strcmp(bam_platform, "i86pc") == 0) {
92367c478bd9Sstevel@tonic-gate 			amd64 = 1;		/* diskless server */
9237d876c67dSjg 		}
9238d876c67dSjg 	} else {
9239d876c67dSjg 		if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
9240d876c67dSjg 		    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) {
9241d876c67dSjg 			amd64 = 1;
9242d876c67dSjg 		} else if (strstr(isabuf, "i386") == NULL) {
9243d876c67dSjg 			amd64 = 1;		/* diskless server */
9244d876c67dSjg 		}
9245d876c67dSjg 	}
9246d876c67dSjg 	if (amd64 == -1)
92477c478bd9Sstevel@tonic-gate 		amd64 = 0;
92487c478bd9Sstevel@tonic-gate 
92497c478bd9Sstevel@tonic-gate 	return (amd64);
92507c478bd9Sstevel@tonic-gate }
92517c478bd9Sstevel@tonic-gate 
925279755401Ssetje static char *
925379755401Ssetje get_machine(void)
9254986fd29aSsetje {
925579755401Ssetje 	static int cached = -1;
925679755401Ssetje 	static char mbuf[257];	/* from sysinfo(2) manpage */
9257986fd29aSsetje 
925879755401Ssetje 	if (cached == 0)
925979755401Ssetje 		return (mbuf);
9260d876c67dSjg 
9261d876c67dSjg 	if (bam_alt_platform) {
926279755401Ssetje 		return (bam_platform);
9263d876c67dSjg 	} else {
926479755401Ssetje 		if (sysinfo(SI_MACHINE, mbuf, sizeof (mbuf)) > 0) {
926579755401Ssetje 			cached = 1;
9266d876c67dSjg 		}
9267d876c67dSjg 	}
926879755401Ssetje 	if (cached == -1) {
926979755401Ssetje 		mbuf[0] = '\0';
927079755401Ssetje 		cached = 0;
9271986fd29aSsetje 	}
9272986fd29aSsetje 
927379755401Ssetje 	return (mbuf);
9274986fd29aSsetje }
9275986fd29aSsetje 
9276eb2bd662Svikram int
9277eb2bd662Svikram is_sparc(void)
9278eb2bd662Svikram {
927979755401Ssetje 	static int issparc = -1;
928079755401Ssetje 	char mbuf[257];	/* from sysinfo(2) manpage */
928179755401Ssetje 
928279755401Ssetje 	if (issparc != -1)
928379755401Ssetje 		return (issparc);
928479755401Ssetje 
928579755401Ssetje 	if (bam_alt_platform) {
928679755401Ssetje 		if (strncmp(bam_platform, "sun4", 4) == 0) {
928779755401Ssetje 			issparc = 1;
928879755401Ssetje 		}
928979755401Ssetje 	} else {
929079755401Ssetje 		if (sysinfo(SI_ARCHITECTURE, mbuf, sizeof (mbuf)) > 0 &&
9291d7d0f93bSjg 		    strcmp(mbuf, "sparc") == 0) {
929279755401Ssetje 			issparc = 1;
929379755401Ssetje 		}
9294d7d0f93bSjg 	}
9295d7d0f93bSjg 	if (issparc == -1)
9296d7d0f93bSjg 		issparc = 0;
929779755401Ssetje 
929879755401Ssetje 	return (issparc);
9299eb2bd662Svikram }
9300986fd29aSsetje 
93017c478bd9Sstevel@tonic-gate static void
93027c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
93037c478bd9Sstevel@tonic-gate {
93047c478bd9Sstevel@tonic-gate 	line_t *lp;
93057c478bd9Sstevel@tonic-gate 
93067c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
93077c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
93087c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
93097c478bd9Sstevel@tonic-gate 		flistp->head = lp;
93107c478bd9Sstevel@tonic-gate 	else
93117c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
93127c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
93137c478bd9Sstevel@tonic-gate }
93142449e17fSsherrym 
9315986fd29aSsetje #if !defined(_OPB)
93162449e17fSsherrym 
93172449e17fSsherrym UCODE_VENDORS;
93182449e17fSsherrym 
93192449e17fSsherrym /*ARGSUSED*/
93202449e17fSsherrym static void
93212449e17fSsherrym ucode_install(char *root)
93222449e17fSsherrym {
93232449e17fSsherrym 	int i;
93242449e17fSsherrym 
93252449e17fSsherrym 	for (i = 0; ucode_vendors[i].filestr != NULL; i++) {
93262449e17fSsherrym 		int cmd_len = PATH_MAX + 256;
93272449e17fSsherrym 		char cmd[PATH_MAX + 256];
93282449e17fSsherrym 		char file[PATH_MAX];
93292449e17fSsherrym 		char timestamp[PATH_MAX];
93302449e17fSsherrym 		struct stat fstatus, tstatus;
93312449e17fSsherrym 		struct utimbuf u_times;
93322449e17fSsherrym 
9333adc586deSMark Johnson 		(void) snprintf(file, PATH_MAX, "%s/%s/%s-ucode.%s",
9334adc586deSMark Johnson 		    bam_root, UCODE_INSTALL_PATH, ucode_vendors[i].filestr,
9335adc586deSMark Johnson 		    ucode_vendors[i].extstr);
93362449e17fSsherrym 
93372449e17fSsherrym 		if (stat(file, &fstatus) != 0 || !(S_ISREG(fstatus.st_mode)))
93382449e17fSsherrym 			continue;
93392449e17fSsherrym 
93402449e17fSsherrym 		(void) snprintf(timestamp, PATH_MAX, "%s.ts", file);
93412449e17fSsherrym 
93422449e17fSsherrym 		if (stat(timestamp, &tstatus) == 0 &&
93432449e17fSsherrym 		    fstatus.st_mtime <= tstatus.st_mtime)
93442449e17fSsherrym 			continue;
93452449e17fSsherrym 
93462449e17fSsherrym 		(void) snprintf(cmd, cmd_len, "/usr/sbin/ucodeadm -i -R "
93472449e17fSsherrym 		    "%s/%s/%s %s > /dev/null 2>&1", bam_root,
93482449e17fSsherrym 		    UCODE_INSTALL_PATH, ucode_vendors[i].vendorstr, file);
93492449e17fSsherrym 		if (system(cmd) != 0)
93502449e17fSsherrym 			return;
93512449e17fSsherrym 
93522449e17fSsherrym 		if (creat(timestamp, S_IRUSR | S_IWUSR) == -1)
93532449e17fSsherrym 			return;
93542449e17fSsherrym 
93552449e17fSsherrym 		u_times.actime = fstatus.st_atime;
93562449e17fSsherrym 		u_times.modtime = fstatus.st_mtime;
93572449e17fSsherrym 		(void) utime(timestamp, &u_times);
93582449e17fSsherrym 	}
93592449e17fSsherrym }
93602449e17fSsherrym #endif
9361